blob: 610d0a36bd173f4d33af7901ac584635729e5fbd [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 Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaara6557602006-02-04 22:43:20 +0000111static int compl_used_match; /* Selected one of the matches. When
112 FALSE the match was edited or using
113 the longest common string. */
114
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000115static int compl_was_interrupted = FALSE; /* didn't finish finding
116 completions. */
117
118static int compl_restarting = FALSE; /* don't insert match */
119
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120/* When the first completion is done "compl_started" is set. When it's
121 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000122static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000123
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000124/* Set when doing something for completion that may call edit() recursively,
125 * which is not allowed. */
126static int compl_busy = FALSE;
127
Bram Moolenaar572cb562005-08-05 21:35:02 +0000128static int compl_matches = 0;
129static char_u *compl_pattern = NULL;
130static int compl_direction = FORWARD;
131static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000132static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static pos_T compl_startpos;
134static colnr_T compl_col = 0; /* column where the text starts
135 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000136static char_u *compl_orig_text = NULL; /* text as it was before
137 * completion started */
138static int compl_cont_mode = 0;
139static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaar82139082011-09-14 16:52:09 +0200141static int compl_opt_refresh_always = FALSE;
142
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000143static void ins_ctrl_x __ARGS((void));
144static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000145static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000146static 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 +0000147static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000148static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000149static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000151static void ins_compl_upd_pum __ARGS((void));
152static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000153static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000154static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000155static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000156static 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 +0000157static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_compl_free __ARGS((void));
159static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000160static int ins_compl_bs __ARGS((void));
Bram Moolenaar82139082011-09-14 16:52:09 +0200161static int ins_compl_need_restart __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000162static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000163static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar82139082011-09-14 16:52:09 +0200164static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000165static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000166static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000167static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000168static int ins_compl_prep __ARGS((int c));
Bram Moolenaar62951b12011-09-21 18:23:05 +0200169static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000171#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
172static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200173static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000174#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000175static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static void ins_compl_delete __ARGS((void));
177static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000178static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000179static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000181static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000182static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000184static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif /* FEAT_INS_EXPAND */
186
187#define BACKSPACE_CHAR 1
188#define BACKSPACE_WORD 2
189#define BACKSPACE_WORD_NOT_SPACE 3
190#define BACKSPACE_LINE 4
191
Bram Moolenaar754b5602006-02-09 23:53:20 +0000192static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static void ins_ctrl_v __ARGS((void));
194static void undisplay_dollar __ARGS((void));
195static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000196static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197static void check_auto_format __ARGS((int));
198static void redo_literal __ARGS((int c));
199static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000200#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000201static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000202static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000203static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000204#endif
Bram Moolenaarf332a652013-11-04 04:20:33 +0100205static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static int replace_pop __ARGS((void));
208static void replace_join __ARGS((int off));
209static void replace_pop_ins __ARGS((void));
210#ifdef FEAT_MBYTE
211static void mb_replace_pop_ins __ARGS((int cc));
212#endif
213static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000214static void replace_do_bs __ARGS((int limit_col));
215static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifdef FEAT_CINDENT
217static int cindent_on __ARGS((void));
218#endif
219static void ins_reg __ARGS((void));
220static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000221static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000222static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_RIGHTLEFT
224static void ins_ctrl_ __ARGS((void));
225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226static int ins_start_select __ARGS((int c));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000227static void ins_insert __ARGS((int replaceState));
228static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229static void ins_shift __ARGS((int c, int lastc));
230static void ins_del __ARGS((void));
231static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
232#ifdef FEAT_MOUSE
233static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200234static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000236#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
237static void ins_tabline __ARGS((int c));
238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239static void ins_left __ARGS((void));
240static void ins_home __ARGS((int c));
241static void ins_end __ARGS((int c));
242static void ins_s_left __ARGS((void));
243static void ins_right __ARGS((void));
244static void ins_s_right __ARGS((void));
245static void ins_up __ARGS((int startcol));
246static void ins_pageup __ARGS((void));
247static void ins_down __ARGS((int startcol));
248static void ins_pagedown __ARGS((void));
249#ifdef FEAT_DND
250static void ins_drop __ARGS((void));
251#endif
252static int ins_tab __ARGS((void));
253static int ins_eol __ARGS((int c));
254#ifdef FEAT_DIGRAPHS
255static int ins_digraph __ARGS((void));
256#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000257static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_SMARTINDENT
259static void ins_try_si __ARGS((int c));
260#endif
261static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100262#ifdef FEAT_AUTOCMD
263static char_u *do_insert_char_pre __ARGS((int c));
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266static colnr_T Insstart_textlen; /* length of line when insert started */
267static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100268static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
270static char_u *last_insert = NULL; /* the text of the previous insert,
271 K_SPECIAL and CSI are escaped */
272static int last_insert_skip; /* nr of chars in front of previous insert */
273static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000274static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
276#ifdef FEAT_CINDENT
277static int can_cindent; /* may do cindenting on this line */
278#endif
279
280static int old_indent = 0; /* for ^^D command in insert mode */
281
282#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000283static int revins_on; /* reverse insert mode on */
284static int revins_chars; /* how much to skip after edit */
285static int revins_legal; /* was the last char 'legal'? */
286static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#endif
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static int ins_need_undo; /* call u_save() before inserting a
290 char. Set when edit() is called.
291 after that arrow_used is used. */
292
293static int did_add_space = FALSE; /* auto_format() added an extra space
294 under the cursor */
295
296/*
297 * edit(): Start inserting text.
298 *
299 * "cmdchar" can be:
300 * 'i' normal insert command
301 * 'a' normal append command
302 * 'R' replace command
303 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
304 * but still only one <CR> is inserted. The <Esc> is not used for redo.
305 * 'g' "gI" command.
306 * 'V' "gR" command for Virtual Replace mode.
307 * 'v' "gr" command for single character Virtual Replace mode.
308 *
309 * This function is not called recursively. For CTRL-O commands, it returns
310 * and lets the caller handle the Normal-mode command.
311 *
312 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
313 */
314 int
315edit(cmdchar, startln, count)
316 int cmdchar;
317 int startln; /* if set, insert at start of line */
318 long count;
319{
320 int c = 0;
321 char_u *ptr;
322 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000323 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 int i;
326 int did_backspace = TRUE; /* previous char was backspace */
327#ifdef FEAT_CINDENT
328 int line_is_white = FALSE; /* line is empty before insert */
329#endif
330 linenr_T old_topline = 0; /* topline before insertion */
331#ifdef FEAT_DIFF
332 int old_topfill = -1;
333#endif
334 int inserted_space = FALSE; /* just inserted a space */
335 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000336 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000338 /* Remember whether editing was restarted after CTRL-O. */
339 did_restart_edit = restart_edit;
340
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 /* sleep before redrawing, needed for "CTRL-O :" that results in an
342 * error message */
343 check_for_delay(TRUE);
344
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100345 /* set Insstart_orig to Insstart */
346 update_Insstart_orig = TRUE;
347
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifdef HAVE_SANDBOX
349 /* Don't allow inserting in the sandbox. */
350 if (sandbox != 0)
351 {
352 EMSG(_(e_sandbox));
353 return FALSE;
354 }
355#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000356 /* Don't allow changes in the buffer while editing the cmdline. The
357 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000358 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000359 {
360 EMSG(_(e_secure));
361 return FALSE;
362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000365 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000366 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000367 {
368 EMSG(_(e_secure));
369 return FALSE;
370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 ins_compl_clear(); /* clear stuff for CTRL-X mode */
372#endif
373
Bram Moolenaar843ee412004-06-30 16:16:41 +0000374#ifdef FEAT_AUTOCMD
375 /*
376 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
377 */
378 if (cmdchar != 'r' && cmdchar != 'v')
379 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100380 pos_T save_cursor = curwin->w_cursor;
381
Bram Moolenaar1e015462005-09-25 22:16:38 +0000382# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000383 if (cmdchar == 'R')
384 ptr = (char_u *)"r";
385 else if (cmdchar == 'V')
386 ptr = (char_u *)"v";
387 else
388 ptr = (char_u *)"i";
389 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200390 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000391# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000392 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100393
Bram Moolenaar097c9922013-05-19 21:15:15 +0200394 /* Make sure the cursor didn't move. Do call check_cursor_col() in
395 * case the text was modified. Since Insert mode was not started yet
396 * a call to check_cursor_col() may move the cursor, especially with
397 * the "A" command, thus set State to avoid that. Also check that the
398 * line number is still valid (lines may have been deleted).
399 * Do not restore if v:char was set to a non-empty string. */
400 if (!equalpos(curwin->w_cursor, save_cursor)
401# ifdef FEAT_EVAL
402 && *get_vim_var_str(VV_CHAR) == NUL
403# endif
404 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100405 {
406 int save_state = State;
407
408 curwin->w_cursor = save_cursor;
409 State = INSERT;
410 check_cursor_col();
411 State = save_state;
412 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000413 }
414#endif
415
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200416#ifdef FEAT_CONCEAL
417 /* Check if the cursor line needs redrawing before changing State. If
418 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200419 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200420#endif
421
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422#ifdef FEAT_MOUSE
423 /*
424 * When doing a paste with the middle mouse button, Insstart is set to
425 * where the paste started.
426 */
427 if (where_paste_started.lnum != 0)
428 Insstart = where_paste_started;
429 else
430#endif
431 {
432 Insstart = curwin->w_cursor;
433 if (startln)
434 Insstart.col = 0;
435 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000436 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 Insstart_blank_vcol = MAXCOL;
438 if (!did_ai)
439 ai_col = 0;
440
441 if (cmdchar != NUL && restart_edit == 0)
442 {
443 ResetRedobuff();
444 AppendNumberToRedobuff(count);
445#ifdef FEAT_VREPLACE
446 if (cmdchar == 'V' || cmdchar == 'v')
447 {
448 /* "gR" or "gr" command */
449 AppendCharToRedobuff('g');
450 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
451 }
452 else
453#endif
454 {
455 AppendCharToRedobuff(cmdchar);
456 if (cmdchar == 'g') /* "gI" command */
457 AppendCharToRedobuff('I');
458 else if (cmdchar == 'r') /* "r<CR>" command */
459 count = 1; /* insert only one <CR> */
460 }
461 }
462
463 if (cmdchar == 'R')
464 {
465#ifdef FEAT_FKMAP
466 if (p_fkmap && p_ri)
467 {
468 beep_flush();
469 EMSG(farsi_text_3); /* encoded in Farsi */
470 State = INSERT;
471 }
472 else
473#endif
474 State = REPLACE;
475 }
476#ifdef FEAT_VREPLACE
477 else if (cmdchar == 'V' || cmdchar == 'v')
478 {
479 State = VREPLACE;
480 replaceState = VREPLACE;
481 orig_line_count = curbuf->b_ml.ml_line_count;
482 vr_lines_changed = 1;
483 }
484#endif
485 else
486 State = INSERT;
487
488 stop_insert_mode = FALSE;
489
490 /*
491 * Need to recompute the cursor position, it might move when the cursor is
492 * on a TAB or special character.
493 */
494 curs_columns(TRUE);
495
496 /*
497 * Enable langmap or IME, indicated by 'iminsert'.
498 * Note that IME may enabled/disabled without us noticing here, thus the
499 * 'iminsert' value may not reflect what is actually used. It is updated
500 * when hitting <Esc>.
501 */
502 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
503 State |= LANGMAP;
504#ifdef USE_IM_CONTROL
505 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
506#endif
507
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508#ifdef FEAT_MOUSE
509 setmouse();
510#endif
511#ifdef FEAT_CMDL_INFO
512 clear_showcmd();
513#endif
514#ifdef FEAT_RIGHTLEFT
515 /* there is no reverse replace mode */
516 revins_on = (State == INSERT && p_ri);
517 if (revins_on)
518 undisplay_dollar();
519 revins_chars = 0;
520 revins_legal = 0;
521 revins_scol = -1;
522#endif
523
524 /*
525 * Handle restarting Insert mode.
526 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
527 * restart_edit non-zero, and something in the stuff buffer.
528 */
529 if (restart_edit != 0 && stuff_empty())
530 {
531#ifdef FEAT_MOUSE
532 /*
533 * After a paste we consider text typed to be part of the insert for
534 * the pasted text. You can backspace over the pasted text too.
535 */
536 if (where_paste_started.lnum)
537 arrow_used = FALSE;
538 else
539#endif
540 arrow_used = TRUE;
541 restart_edit = 0;
542
543 /*
544 * If the cursor was after the end-of-line before the CTRL-O and it is
545 * now at the end-of-line, put it after the end-of-line (this is not
546 * correct in very rare cases).
547 * Also do this if curswant is greater than the current virtual
548 * column. Eg after "^O$" or "^O80|".
549 */
550 validate_virtcol();
551 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000552 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 || curwin->w_curswant > curwin->w_virtcol)
554 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
555 {
556 if (ptr[1] == NUL)
557 ++curwin->w_cursor.col;
558#ifdef FEAT_MBYTE
559 else if (has_mbyte)
560 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000561 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (ptr[i] == NUL)
563 curwin->w_cursor.col += i;
564 }
565#endif
566 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000567 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
569 else
570 arrow_used = FALSE;
571
572 /* we are in insert mode now, don't need to start it anymore */
573 need_start_insertmode = FALSE;
574
575 /* Need to save the line for undo before inserting the first char. */
576 ins_need_undo = TRUE;
577
578#ifdef FEAT_MOUSE
579 where_paste_started.lnum = 0;
580#endif
581#ifdef FEAT_CINDENT
582 can_cindent = TRUE;
583#endif
584#ifdef FEAT_FOLDING
585 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
586 * restarting. */
587 if (!p_im && did_restart_edit == 0)
588 foldOpenCursor();
589#endif
590
591 /*
592 * If 'showmode' is set, show the current (insert/replace/..) mode.
593 * A warning message for changing a readonly file is given here, before
594 * actually changing anything. It's put after the mode, if any.
595 */
596 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000597 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 i = showmode();
599
600 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000601 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603#ifdef CURSOR_SHAPE
604 ui_cursor_shape(); /* may show different cursor shape */
605#endif
606#ifdef FEAT_DIGRAPHS
607 do_digraph(-1); /* clear digraphs */
608#endif
609
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000610 /*
611 * Get the current length of the redo buffer, those characters have to be
612 * skipped if we want to get to the inserted characters.
613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 ptr = get_inserted();
615 if (ptr == NULL)
616 new_insert_skip = 0;
617 else
618 {
619 new_insert_skip = (int)STRLEN(ptr);
620 vim_free(ptr);
621 }
622
623 old_indent = 0;
624
625 /*
626 * Main loop in Insert mode: repeat until Insert mode is left.
627 */
628 for (;;)
629 {
630#ifdef FEAT_RIGHTLEFT
631 if (!revins_legal)
632 revins_scol = -1; /* reset on illegal motions */
633 else
634 revins_legal = 0;
635#endif
636 if (arrow_used) /* don't repeat insert when arrow key used */
637 count = 0;
638
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100639 if (update_Insstart_orig)
640 Insstart_orig = Insstart;
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (stop_insert_mode)
643 {
644 /* ":stopinsert" used or 'insertmode' reset */
645 count = 0;
646 goto doESCkey;
647 }
648
649 /* set curwin->w_curswant for next K_DOWN or K_UP */
650 if (!arrow_used)
651 curwin->w_set_curswant = TRUE;
652
653 /* If there is no typeahead may check for timestamps (e.g., for when a
654 * menu invoked a shell command). */
655 if (stuff_empty())
656 {
657 did_check_timestamps = FALSE;
658 if (need_check_timestamps)
659 check_timestamps(FALSE);
660 }
661
662 /*
663 * When emsg() was called msg_scroll will have been set.
664 */
665 msg_scroll = FALSE;
666
667#ifdef FEAT_GUI
668 /* When 'mousefocus' is set a mouse movement may have taken us to
669 * another window. "need_mouse_correct" may then be set because of an
670 * autocommand. */
671 if (need_mouse_correct)
672 gui_mouse_correct();
673#endif
674
675#ifdef FEAT_FOLDING
676 /* Open fold at the cursor line, according to 'foldopen'. */
677 if (fdo_flags & FDO_INSERT)
678 foldOpenCursor();
679 /* Close folds where the cursor isn't, according to 'foldclose' */
680 if (!char_avail())
681 foldCheckClose();
682#endif
683
684 /*
685 * If we inserted a character at the last position of the last line in
686 * the window, scroll the window one line up. This avoids an extra
687 * redraw.
688 * This is detected when the cursor column is smaller after inserting
689 * something.
690 * Don't do this when the topline changed already, it has
691 * already been adjusted (by insertchar() calling open_line())).
692 */
693 if (curbuf->b_mod_set
694 && curwin->w_p_wrap
695 && !did_backspace
696 && curwin->w_topline == old_topline
697#ifdef FEAT_DIFF
698 && curwin->w_topfill == old_topfill
699#endif
700 )
701 {
702 mincol = curwin->w_wcol;
703 validate_cursor_col();
704
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000705 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 && curwin->w_wrow == W_WINROW(curwin)
707 + curwin->w_height - 1 - p_so
708 && (curwin->w_cursor.lnum != curwin->w_topline
709#ifdef FEAT_DIFF
710 || curwin->w_topfill > 0
711#endif
712 ))
713 {
714#ifdef FEAT_DIFF
715 if (curwin->w_topfill > 0)
716 --curwin->w_topfill;
717 else
718#endif
719#ifdef FEAT_FOLDING
720 if (hasFolding(curwin->w_topline, NULL, &old_topline))
721 set_topline(curwin, old_topline + 1);
722 else
723#endif
724 set_topline(curwin, curwin->w_topline + 1);
725 }
726 }
727
728 /* May need to adjust w_topline to show the cursor. */
729 update_topline();
730
731 did_backspace = FALSE;
732
733 validate_cursor(); /* may set must_redraw */
734
735 /*
736 * Redraw the display when no characters are waiting.
737 * Also shows mode, ruler and positions cursor.
738 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000739 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741#ifdef FEAT_SCROLLBIND
742 if (curwin->w_p_scb)
743 do_check_scrollbind(TRUE);
744#endif
745
Bram Moolenaar860cae12010-06-05 23:22:07 +0200746#ifdef FEAT_CURSORBIND
747 if (curwin->w_p_crb)
748 do_check_cursorbind();
749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 update_curswant();
751 old_topline = curwin->w_topline;
752#ifdef FEAT_DIFF
753 old_topfill = curwin->w_topfill;
754#endif
755
756#ifdef USE_ON_FLY_SCROLL
757 dont_scroll = FALSE; /* allow scrolling here */
758#endif
759
760 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000761 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100763 if (c != K_CURSORHOLD)
764 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000765 do
766 {
767 c = safe_vgetc();
768 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000770#ifdef FEAT_AUTOCMD
771 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
772 did_cursorhold = TRUE;
773#endif
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_RIGHTLEFT
776 if (p_hkmap && KeyTyped)
777 c = hkmap(c); /* Hebrew mode mapping */
778#endif
779#ifdef FEAT_FKMAP
780 if (p_fkmap && KeyTyped)
781 c = fkmap(c); /* Farsi mode mapping */
782#endif
783
784#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000785 /*
786 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000787 * and the cursor is still in the completed word. Only when there is
788 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000789 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000790 if (compl_started
791 && pum_wanted()
792 && curwin->w_cursor.col >= compl_col
793 && (compl_shown_match == NULL
794 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000795 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000796 /* BS: Delete one character from "compl_leader". */
797 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000798 && curwin->w_cursor.col > compl_col
799 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000800 continue;
801
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000802 /* When no match was selected or it was edited. */
803 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000804 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000805 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000806 * "compl_leader". Except when at the original match and
807 * there is nothing to add, CTRL-L works like CTRL-P then. */
808 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100809 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000810 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000811 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000812 {
813 ins_compl_addfrommatch();
814 continue;
815 }
816
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000817 /* A non-white character that fits in with the current
818 * completion: Add to "compl_leader". */
819 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000820 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100821#ifdef FEAT_AUTOCMD
822 /* Trigger InsertCharPre. */
823 char_u *str = do_insert_char_pre(c);
824 char_u *p;
825
826 if (str != NULL)
827 {
828 for (p = str; *p != NUL; mb_ptr_adv(p))
829 ins_compl_addleader(PTR2CHAR(p));
830 vim_free(str);
831 }
832 else
833#endif
834 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000835 continue;
836 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000837
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000838 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000839 * compl_enter_selects is set the Enter key does the same. */
840 if (c == Ctrl_Y || (compl_enter_selects
841 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000842 {
843 ins_compl_delete();
844 ins_compl_insert();
845 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000846 }
847 }
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
850 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000851 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000852 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000853 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar488c6512005-08-11 20:09:58 +0000856 /* CTRL-\ CTRL-N goes to Normal mode,
857 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
858 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (c == Ctrl_BSL)
860 {
861 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000862 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 ++no_mapping;
864 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000865 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 --no_mapping;
867 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000868 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000870 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 vungetc(c);
872 c = Ctrl_BSL;
873 }
874 else if (c == Ctrl_G && p_im)
875 continue;
876 else
877 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000878 if (c == Ctrl_O)
879 {
880 ins_ctrl_o();
881 ins_at_eol = FALSE; /* cursor keeps its column */
882 nomove = TRUE;
883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 count = 0;
885 goto doESCkey;
886 }
887 }
888
889#ifdef FEAT_DIGRAPHS
890 c = do_digraph(c);
891#endif
892
893#ifdef FEAT_INS_EXPAND
894 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
895 goto docomplete;
896#endif
897 if (c == Ctrl_V || c == Ctrl_Q)
898 {
899 ins_ctrl_v();
900 c = Ctrl_V; /* pretend CTRL-V is last typed character */
901 continue;
902 }
903
904#ifdef FEAT_CINDENT
905 if (cindent_on()
906# ifdef FEAT_INS_EXPAND
907 && ctrl_x_mode == 0
908# endif
909 )
910 {
911 /* A key name preceded by a bang means this key is not to be
912 * inserted. Skip ahead to the re-indenting below.
913 * A key name preceded by a star means that indenting has to be
914 * done before inserting the key. */
915 line_is_white = inindent(0);
916 if (in_cinkeys(c, '!', line_is_white))
917 goto force_cindent;
918 if (can_cindent && in_cinkeys(c, '*', line_is_white)
919 && stop_arrow() == OK)
920 do_c_expr_indent();
921 }
922#endif
923
924#ifdef FEAT_RIGHTLEFT
925 if (curwin->w_p_rl)
926 switch (c)
927 {
928 case K_LEFT: c = K_RIGHT; break;
929 case K_S_LEFT: c = K_S_RIGHT; break;
930 case K_C_LEFT: c = K_C_RIGHT; break;
931 case K_RIGHT: c = K_LEFT; break;
932 case K_S_RIGHT: c = K_S_LEFT; break;
933 case K_C_RIGHT: c = K_C_LEFT; break;
934 }
935#endif
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 /*
938 * If 'keymodel' contains "startsel", may start selection. If it
939 * does, a CTRL-O and c will be stuffed, we need to get these
940 * characters.
941 */
942 if (ins_start_select(c))
943 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /*
946 * The big switch to handle a character in insert mode.
947 */
948 switch (c)
949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000950 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (echeck_abbr(ESC + ABBR_OFF))
952 break;
953 /*FALLTHROUGH*/
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#ifdef FEAT_CMDWIN
957 if (c == Ctrl_C && cmdwin_type != 0)
958 {
959 /* Close the cmdline window. */
960 cmdwin_result = K_IGNORE;
961 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000962 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 goto doESCkey;
964 }
965#endif
966
967#ifdef UNIX
968do_intr:
969#endif
970 /* when 'insertmode' set, and not halfway a mapping, don't leave
971 * Insert mode */
972 if (goto_im())
973 {
974 if (got_int)
975 {
976 (void)vgetc(); /* flush all buffers */
977 got_int = FALSE;
978 }
979 else
980 vim_beep();
981 break;
982 }
983doESCkey:
984 /*
985 * This is the ONLY return from edit()!
986 */
987 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
988 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 o_lnum = curwin->w_cursor.lnum;
991
Bram Moolenaar488c6512005-08-11 20:09:58 +0000992 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000993 {
994#ifdef FEAT_AUTOCMD
995 if (cmdchar != 'r' && cmdchar != 'v')
996 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
997 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000998 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 continue;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case Ctrl_Z: /* suspend when 'insertmode' set */
1005 if (!p_im)
1006 goto normalchar; /* insert CTRL-Z as normal char */
1007 stuffReadbuff((char_u *)":st\r");
1008 c = Ctrl_O;
1009 /*FALLTHROUGH*/
1010
1011 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001012#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001013 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 goto docomplete;
1015#endif
1016 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1017 break;
1018 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001019
1020#ifdef FEAT_VIRTUALEDIT
1021 /* don't move the cursor left when 'virtualedit' has "onemore". */
1022 if (ve_flags & VE_ONEMORE)
1023 {
1024 ins_at_eol = FALSE;
1025 nomove = TRUE;
1026 }
1027#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 count = 0;
1029 goto doESCkey;
1030
Bram Moolenaar572cb562005-08-05 21:35:02 +00001031 case K_INS: /* toggle insert/replace mode */
1032 case K_KINS:
1033 ins_insert(replaceState);
1034 break;
1035
1036 case K_SELECT: /* end of Select mode mapping - ignore */
1037 break;
1038
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001039#ifdef FEAT_SNIFF
1040 case K_SNIFF: /* Sniff command received */
1041 stuffcharReadbuff(K_SNIFF);
1042 goto doESCkey;
1043#endif
1044
1045 case K_HELP: /* Help key works like <ESC> <Help> */
1046 case K_F1:
1047 case K_XF1:
1048 stuffcharReadbuff(K_HELP);
1049 if (p_im)
1050 need_start_insertmode = TRUE;
1051 goto doESCkey;
1052
1053#ifdef FEAT_NETBEANS_INTG
1054 case K_F21: /* NetBeans command */
1055 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001056 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 --no_mapping;
1058 netbeans_keycommand(i);
1059 break;
1060#endif
1061
1062 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 case NUL:
1064 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 /* For ^@ the trailing ESC will end the insert, unless there is an
1066 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1068 && c != Ctrl_A && !p_im)
1069 goto doESCkey; /* quit insert mode */
1070 inserted_space = FALSE;
1071 break;
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 ins_reg();
1075 auto_format(FALSE, TRUE);
1076 inserted_space = FALSE;
1077 break;
1078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 ins_ctrl_g();
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_HAT: /* switch input mode and/or langmap */
1084 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 break;
1086
1087#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (!p_ari)
1090 goto normalchar;
1091 ins_ctrl_();
1092 break;
1093#endif
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1097 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1098 goto docomplete;
1099#endif
1100 /* FALLTHROUGH */
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103# ifdef FEAT_INS_EXPAND
1104 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1105 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 if (has_compl_option(FALSE))
1107 goto docomplete;
1108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110# endif
1111 ins_shift(c, lastc);
1112 auto_format(FALSE, TRUE);
1113 inserted_space = FALSE;
1114 break;
1115
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 case K_KDEL:
1118 ins_del();
1119 auto_format(FALSE, TRUE);
1120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case Ctrl_H:
1124 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1125 auto_format(FALSE, TRUE);
1126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1130 auto_format(FALSE, TRUE);
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001134# ifdef FEAT_COMPL_FUNC
1135 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001137 goto docomplete;
1138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 inserted_space = FALSE;
1142 break;
1143
1144#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_LEFTMOUSE_NM:
1147 case K_LEFTDRAG:
1148 case K_LEFTRELEASE:
1149 case K_LEFTRELEASE_NM:
1150 case K_MIDDLEMOUSE:
1151 case K_MIDDLEDRAG:
1152 case K_MIDDLERELEASE:
1153 case K_RIGHTMOUSE:
1154 case K_RIGHTDRAG:
1155 case K_RIGHTRELEASE:
1156 case K_X1MOUSE:
1157 case K_X1DRAG:
1158 case K_X1RELEASE:
1159 case K_X2MOUSE:
1160 case K_X2DRAG:
1161 case K_X2RELEASE:
1162 ins_mouse(c);
1163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001166 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001170 ins_mousescroll(MSCR_UP);
1171 break;
1172
1173 case K_MOUSELEFT: /* Scroll wheel left */
1174 ins_mousescroll(MSCR_LEFT);
1175 break;
1176
1177 case K_MOUSERIGHT: /* Scroll wheel right */
1178 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 break;
1180#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001181#ifdef FEAT_GUI_TABLINE
1182 case K_TABLINE:
1183 case K_TABMENU:
1184 ins_tabline(c);
1185 break;
1186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 break;
1190
Bram Moolenaar754b5602006-02-09 23:53:20 +00001191#ifdef FEAT_AUTOCMD
1192 case K_CURSORHOLD: /* Didn't type something for a while. */
1193 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1194 did_cursorhold = TRUE;
1195 break;
1196#endif
1197
Bram Moolenaar4770d092006-01-12 23:22:24 +00001198#ifdef FEAT_GUI_W32
1199 /* On Win32 ignore <M-F4>, we get it when closing the window was
1200 * cancelled. */
1201 case K_F4:
1202 if (mod_mask != MOD_MASK_ALT)
1203 goto normalchar;
1204 break;
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_GUI
1208 case K_VER_SCROLLBAR:
1209 ins_scroll();
1210 break;
1211
1212 case K_HOR_SCROLLBAR:
1213 ins_horscroll();
1214 break;
1215#endif
1216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_S_HOME:
1220 case K_C_HOME:
1221 ins_home(c);
1222 break;
1223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 case K_S_END:
1227 case K_C_END:
1228 ins_end(c);
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001232 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1233 ins_s_left();
1234 else
1235 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 break;
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 case K_C_LEFT:
1240 ins_s_left();
1241 break;
1242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001244 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1245 ins_s_right();
1246 else
1247 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 break;
1249
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001250 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 case K_C_RIGHT:
1252 ins_s_right();
1253 break;
1254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001256#ifdef FEAT_INS_EXPAND
1257 if (pum_visible())
1258 goto docomplete;
1259#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001260 if (mod_mask & MOD_MASK_SHIFT)
1261 ins_pageup();
1262 else
1263 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 break;
1265
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001266 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 case K_PAGEUP:
1268 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001269#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001270 if (pum_visible())
1271 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 ins_pageup();
1274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001277#ifdef FEAT_INS_EXPAND
1278 if (pum_visible())
1279 goto docomplete;
1280#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001281 if (mod_mask & MOD_MASK_SHIFT)
1282 ins_pagedown();
1283 else
1284 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 case K_PAGEDOWN:
1289 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001290#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001291 if (pum_visible())
1292 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 ins_pagedown();
1295 break;
1296
1297#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001298 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 ins_drop();
1300 break;
1301#endif
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 c = TAB;
1305 /* FALLTHROUGH */
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1309 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1310 goto docomplete;
1311#endif
1312 inserted_space = FALSE;
1313 if (ins_tab())
1314 goto normalchar; /* insert TAB as a normal char */
1315 auto_format(FALSE, TRUE);
1316 break;
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 c = CAR;
1320 /* FALLTHROUGH */
1321 case CAR:
1322 case NL:
1323#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1324 /* In a quickfix window a <CR> jumps to the error under the
1325 * cursor. */
1326 if (bt_quickfix(curbuf) && c == CAR)
1327 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 if (curwin->w_llist_ref == NULL) /* quickfix window */
1329 do_cmdline_cmd((char_u *)".cc");
1330 else /* location list window */
1331 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333 }
1334#endif
1335#ifdef FEAT_CMDWIN
1336 if (cmdwin_type != 0)
1337 {
1338 /* Execute the command in the cmdline window. */
1339 cmdwin_result = CAR;
1340 goto doESCkey;
1341 }
1342#endif
1343 if (ins_eol(c) && !p_im)
1344 goto doESCkey; /* out of memory */
1345 auto_format(FALSE, FALSE);
1346 inserted_space = FALSE;
1347 break;
1348
Bram Moolenaar860cae12010-06-05 23:22:07 +02001349#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351# ifdef FEAT_INS_EXPAND
1352 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1353 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001354 if (has_compl_option(TRUE))
1355 goto docomplete;
1356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358# endif
1359# ifdef FEAT_DIGRAPHS
1360 c = ins_digraph();
1361 if (c == NUL)
1362 break;
1363# endif
1364 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001368 case Ctrl_X: /* Enter CTRL-X mode */
1369 ins_ctrl_x();
1370 break;
1371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (ctrl_x_mode != CTRL_X_TAGS)
1374 goto normalchar;
1375 goto docomplete;
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (ctrl_x_mode != CTRL_X_FILES)
1379 goto normalchar;
1380 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001381
1382 case 's': /* Spelling completion after ^X */
1383 case Ctrl_S:
1384 if (ctrl_x_mode != CTRL_X_SPELL)
1385 goto normalchar;
1386 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001389 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef FEAT_INS_EXPAND
1391 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1392#endif
1393 {
1394 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1395 if (p_im)
1396 {
1397 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1398 break;
1399 goto doESCkey;
1400 }
1401 goto normalchar;
1402 }
1403#ifdef FEAT_INS_EXPAND
1404 /* FALLTHROUGH */
1405
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001406 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 case Ctrl_N:
1408 /* if 'complete' is empty then plain ^P is no longer special,
1409 * but it is under other ^X modes */
1410 if (*curbuf->b_p_cpt == NUL
1411 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001412 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 goto normalchar;
1414
1415docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001416 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001419 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 break;
1421#endif /* FEAT_INS_EXPAND */
1422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 case Ctrl_Y: /* copy from previous line or scroll down */
1424 case Ctrl_E: /* copy from next line or scroll up */
1425 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 break;
1427
1428 default:
1429#ifdef UNIX
1430 if (c == intr_char) /* special interrupt char */
1431 goto do_intr;
1432#endif
1433
Bram Moolenaare659c952011-05-19 17:25:41 +02001434normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001436 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001438#ifdef FEAT_AUTOCMD
1439 if (!p_paste)
1440 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001441 /* Trigger InsertCharPre. */
1442 char_u *str = do_insert_char_pre(c);
1443 char_u *p;
1444
1445 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001446 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001447 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001448 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001449 /* Insert the new value of v:char literally. */
1450 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001451 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001452 c = PTR2CHAR(p);
1453 if (c == CAR || c == K_KENTER || c == NL)
1454 ins_eol(c);
1455 else
1456 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001457 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001458 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001459 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001460 vim_free(str);
1461 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001462 }
1463
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001464 /* If the new value is already inserted or an empty string
1465 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001466 if (c == NUL)
1467 break;
1468 }
1469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#ifdef FEAT_SMARTINDENT
1471 /* Try to perform smart-indenting. */
1472 ins_try_si(c);
1473#endif
1474
1475 if (c == ' ')
1476 {
1477 inserted_space = TRUE;
1478#ifdef FEAT_CINDENT
1479 if (inindent(0))
1480 can_cindent = FALSE;
1481#endif
1482 if (Insstart_blank_vcol == MAXCOL
1483 && curwin->w_cursor.lnum == Insstart.lnum)
1484 Insstart_blank_vcol = get_nolist_virtcol();
1485 }
1486
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001487 /* Insert a normal character and check for abbreviations on a
1488 * special character. Let CTRL-] expand abbreviations without
1489 * inserting it. */
1490 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#ifdef FEAT_MBYTE
1492 /* Add ABBR_OFF for characters above 0x100, this is
1493 * what check_abbr() expects. */
1494 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1495#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001496 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 insert_special(c, FALSE, FALSE);
1499#ifdef FEAT_RIGHTLEFT
1500 revins_legal++;
1501 revins_chars++;
1502#endif
1503 }
1504
1505 auto_format(FALSE, TRUE);
1506
1507#ifdef FEAT_FOLDING
1508 /* When inserting a character the cursor line must never be in a
1509 * closed fold. */
1510 foldOpenCursor();
1511#endif
1512 break;
1513 } /* end of switch (c) */
1514
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001515#ifdef FEAT_AUTOCMD
1516 /* If typed something may trigger CursorHoldI again. */
1517 if (c != K_CURSORHOLD)
1518 did_cursorhold = FALSE;
1519#endif
1520
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /* If the cursor was moved we didn't just insert a space */
1522 if (arrow_used)
1523 inserted_space = FALSE;
1524
1525#ifdef FEAT_CINDENT
1526 if (can_cindent && cindent_on()
1527# ifdef FEAT_INS_EXPAND
1528 && ctrl_x_mode == 0
1529# endif
1530 )
1531 {
1532force_cindent:
1533 /*
1534 * Indent now if a key was typed that is in 'cinkeys'.
1535 */
1536 if (in_cinkeys(c, ' ', line_is_white))
1537 {
1538 if (stop_arrow() == OK)
1539 /* re-indent the current line */
1540 do_c_expr_indent();
1541 }
1542 }
1543#endif /* FEAT_CINDENT */
1544
1545 } /* for (;;) */
1546 /* NOTREACHED */
1547}
1548
1549/*
1550 * Redraw for Insert mode.
1551 * This is postponed until getting the next character to make '$' in the 'cpo'
1552 * option work correctly.
1553 * Only redraw when there are no characters available. This speeds up
1554 * inserting sequences of characters (e.g., for CTRL-R).
1555 */
1556 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001557ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001558 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001560#ifdef FEAT_CONCEAL
1561 linenr_T conceal_old_cursor_line = 0;
1562 linenr_T conceal_new_cursor_line = 0;
1563 int conceal_update_lines = FALSE;
1564#endif
1565
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001566 if (char_avail())
1567 return;
1568
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001569#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001570 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1571 * visible, the command might delete it. */
1572 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001573# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001574 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001575# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001576# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001577 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001578# endif
1579# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001580 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001582 )
1583 && !equalpos(last_cursormoved, curwin->w_cursor)
1584# ifdef FEAT_INS_EXPAND
1585 && !pum_visible()
1586# endif
1587 )
1588 {
1589# ifdef FEAT_SYN_HL
1590 /* Need to update the screen first, to make sure syntax
1591 * highlighting is correct after making a change (e.g., inserting
1592 * a "(". The autocommand may also require a redraw, so it's done
1593 * again below, unfortunately. */
1594 if (syntax_present(curwin) && must_redraw)
1595 update_screen(0);
1596# endif
1597# ifdef FEAT_AUTOCMD
1598 if (has_cursormovedI())
1599 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1600# endif
1601# ifdef FEAT_CONCEAL
1602 if (curwin->w_p_cole > 0)
1603 {
1604 conceal_old_cursor_line = last_cursormoved.lnum;
1605 conceal_new_cursor_line = curwin->w_cursor.lnum;
1606 conceal_update_lines = TRUE;
1607 }
1608# endif
1609 last_cursormoved = curwin->w_cursor;
1610 }
1611#endif
1612
1613#ifdef FEAT_AUTOCMD
1614 /* Trigger TextChangedI if b_changedtick differs. */
1615 if (ready && has_textchangedI()
1616 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617# ifdef FEAT_INS_EXPAND
1618 && !pum_visible()
1619# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 )
1621 {
1622 if (last_changedtick_buf == curbuf)
1623 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1624 last_changedtick_buf = curbuf;
1625 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627#endif
1628
1629 if (must_redraw)
1630 update_screen(0);
1631 else if (clear_cmdline || redraw_cmdline)
1632 showmode(); /* clear cmdline and show mode */
1633# if defined(FEAT_CONCEAL)
1634 if ((conceal_update_lines
1635 && (conceal_old_cursor_line != conceal_new_cursor_line
1636 || conceal_cursor_line(curwin)))
1637 || need_cursor_line_redraw)
1638 {
1639 if (conceal_old_cursor_line != conceal_new_cursor_line)
1640 update_single_line(curwin, conceal_old_cursor_line);
1641 update_single_line(curwin, conceal_new_cursor_line == 0
1642 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1643 curwin->w_valid &= ~VALID_CROW;
1644 }
1645# endif
1646 showruler(FALSE);
1647 setcursor();
1648 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649}
1650
1651/*
1652 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1653 */
1654 static void
1655ins_ctrl_v()
1656{
1657 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001658 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001661 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001666 did_putchar = TRUE;
1667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1669
1670#ifdef FEAT_CMDL_INFO
1671 add_to_showcmd_c(Ctrl_V);
1672#endif
1673
1674 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001675 if (did_putchar)
1676 /* when the line fits in 'columns' the '^' is at the start of the next
1677 * line and will not removed by the redraw */
1678 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#ifdef FEAT_CMDL_INFO
1680 clear_showcmd();
1681#endif
1682 insert_special(c, FALSE, TRUE);
1683#ifdef FEAT_RIGHTLEFT
1684 revins_chars++;
1685 revins_legal++;
1686#endif
1687}
1688
1689/*
1690 * Put a character directly onto the screen. It's not stored in a buffer.
1691 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1692 */
1693static int pc_status;
1694#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1695#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1696#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1697#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699static int pc_attr;
1700static int pc_row;
1701static int pc_col;
1702
1703 void
1704edit_putchar(c, highlight)
1705 int c;
1706 int highlight;
1707{
1708 int attr;
1709
1710 if (ScreenLines != NULL)
1711 {
1712 update_topline(); /* just in case w_topline isn't valid */
1713 validate_cursor();
1714 if (highlight)
1715 attr = hl_attr(HLF_8);
1716 else
1717 attr = 0;
1718 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1719 pc_col = W_WINCOL(curwin);
1720#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1721 pc_status = PC_STATUS_UNSET;
1722#endif
1723#ifdef FEAT_RIGHTLEFT
1724 if (curwin->w_p_rl)
1725 {
1726 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1727# ifdef FEAT_MBYTE
1728 if (has_mbyte)
1729 {
1730 int fix_col = mb_fix_col(pc_col, pc_row);
1731
1732 if (fix_col != pc_col)
1733 {
1734 screen_putchar(' ', pc_row, fix_col, attr);
1735 --curwin->w_wcol;
1736 pc_status = PC_STATUS_RIGHT;
1737 }
1738 }
1739# endif
1740 }
1741 else
1742#endif
1743 {
1744 pc_col += curwin->w_wcol;
1745#ifdef FEAT_MBYTE
1746 if (mb_lefthalve(pc_row, pc_col))
1747 pc_status = PC_STATUS_LEFT;
1748#endif
1749 }
1750
1751 /* save the character to be able to put it back */
1752#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1753 if (pc_status == PC_STATUS_UNSET)
1754#endif
1755 {
1756 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1757 pc_status = PC_STATUS_SET;
1758 }
1759 screen_putchar(c, pc_row, pc_col, attr);
1760 }
1761}
1762
1763/*
1764 * Undo the previous edit_putchar().
1765 */
1766 void
1767edit_unputchar()
1768{
1769 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1770 {
1771#if defined(FEAT_MBYTE)
1772 if (pc_status == PC_STATUS_RIGHT)
1773 ++curwin->w_wcol;
1774 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1775 redrawWinline(curwin->w_cursor.lnum, FALSE);
1776 else
1777#endif
1778 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1779 }
1780}
1781
1782/*
1783 * Called when p_dollar is set: display a '$' at the end of the changed text
1784 * Only works when cursor is in the line that changes.
1785 */
1786 void
1787display_dollar(col)
1788 colnr_T col;
1789{
1790 colnr_T save_col;
1791
1792 if (!redrawing())
1793 return;
1794
1795 cursor_off();
1796 save_col = curwin->w_cursor.col;
1797 curwin->w_cursor.col = col;
1798#ifdef FEAT_MBYTE
1799 if (has_mbyte)
1800 {
1801 char_u *p;
1802
1803 /* If on the last byte of a multi-byte move to the first byte. */
1804 p = ml_get_curline();
1805 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1806 }
1807#endif
1808 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1809 if (curwin->w_wcol < W_WIDTH(curwin))
1810 {
1811 edit_putchar('$', FALSE);
1812 dollar_vcol = curwin->w_virtcol;
1813 }
1814 curwin->w_cursor.col = save_col;
1815}
1816
1817/*
1818 * Call this function before moving the cursor from the normal insert position
1819 * in insert mode.
1820 */
1821 static void
1822undisplay_dollar()
1823{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001824 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001826 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 redrawWinline(curwin->w_cursor.lnum, FALSE);
1828 }
1829}
1830
1831/*
1832 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1833 * Keep the cursor on the same character.
1834 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1835 * type == INDENT_DEC decrease indent (for CTRL-D)
1836 * type == INDENT_SET set indent to "amount"
1837 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1838 */
1839 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001840change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int type;
1842 int amount;
1843 int round;
1844 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001845 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 int vcol;
1848 int last_vcol;
1849 int insstart_less; /* reduction for Insstart.col */
1850 int new_cursor_col;
1851 int i;
1852 char_u *ptr;
1853 int save_p_list;
1854 int start_col;
1855 colnr_T vc;
1856#ifdef FEAT_VREPLACE
1857 colnr_T orig_col = 0; /* init for GCC */
1858 char_u *new_line, *orig_line = NULL; /* init for GCC */
1859
1860 /* VREPLACE mode needs to know what the line was like before changing */
1861 if (State & VREPLACE_FLAG)
1862 {
1863 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1864 orig_col = curwin->w_cursor.col;
1865 }
1866#endif
1867
1868 /* for the following tricks we don't want list mode */
1869 save_p_list = curwin->w_p_list;
1870 curwin->w_p_list = FALSE;
1871 vc = getvcol_nolist(&curwin->w_cursor);
1872 vcol = vc;
1873
1874 /*
1875 * For Replace mode we need to fix the replace stack later, which is only
1876 * possible when the cursor is in the indent. Remember the number of
1877 * characters before the cursor if it's possible.
1878 */
1879 start_col = curwin->w_cursor.col;
1880
1881 /* determine offset from first non-blank */
1882 new_cursor_col = curwin->w_cursor.col;
1883 beginline(BL_WHITE);
1884 new_cursor_col -= curwin->w_cursor.col;
1885
1886 insstart_less = curwin->w_cursor.col;
1887
1888 /*
1889 * If the cursor is in the indent, compute how many screen columns the
1890 * cursor is to the left of the first non-blank.
1891 */
1892 if (new_cursor_col < 0)
1893 vcol = get_indent() - vcol;
1894
1895 if (new_cursor_col > 0) /* can't fix replace stack */
1896 start_col = -1;
1897
1898 /*
1899 * Set the new indent. The cursor will be put on the first non-blank.
1900 */
1901 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001902 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 else
1904 {
1905#ifdef FEAT_VREPLACE
1906 int save_State = State;
1907
1908 /* Avoid being called recursively. */
1909 if (State & VREPLACE_FLAG)
1910 State = INSERT;
1911#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001912 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#ifdef FEAT_VREPLACE
1914 State = save_State;
1915#endif
1916 }
1917 insstart_less -= curwin->w_cursor.col;
1918
1919 /*
1920 * Try to put cursor on same character.
1921 * If the cursor is at or after the first non-blank in the line,
1922 * compute the cursor column relative to the column of the first
1923 * non-blank character.
1924 * If we are not in insert mode, leave the cursor on the first non-blank.
1925 * If the cursor is before the first non-blank, position it relative
1926 * to the first non-blank, counted in screen columns.
1927 */
1928 if (new_cursor_col >= 0)
1929 {
1930 /*
1931 * When changing the indent while the cursor is touching it, reset
1932 * Insstart_col to 0.
1933 */
1934 if (new_cursor_col == 0)
1935 insstart_less = MAXCOL;
1936 new_cursor_col += curwin->w_cursor.col;
1937 }
1938 else if (!(State & INSERT))
1939 new_cursor_col = curwin->w_cursor.col;
1940 else
1941 {
1942 /*
1943 * Compute the screen column where the cursor should be.
1944 */
1945 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001946 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 /*
1949 * Advance the cursor until we reach the right screen column.
1950 */
1951 vcol = last_vcol = 0;
1952 new_cursor_col = -1;
1953 ptr = ml_get_curline();
1954 while (vcol <= (int)curwin->w_virtcol)
1955 {
1956 last_vcol = vcol;
1957#ifdef FEAT_MBYTE
1958 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001959 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else
1961#endif
1962 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001963 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 vcol = last_vcol;
1966
1967 /*
1968 * May need to insert spaces to be able to position the cursor on
1969 * the right screen column.
1970 */
1971 if (vcol != (int)curwin->w_virtcol)
1972 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001973 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001975 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (ptr != NULL)
1977 {
1978 new_cursor_col += i;
1979 ptr[i] = NUL;
1980 while (--i >= 0)
1981 ptr[i] = ' ';
1982 ins_str(ptr);
1983 vim_free(ptr);
1984 }
1985 }
1986
1987 /*
1988 * When changing the indent while the cursor is in it, reset
1989 * Insstart_col to 0.
1990 */
1991 insstart_less = MAXCOL;
1992 }
1993
1994 curwin->w_p_list = save_p_list;
1995
1996 if (new_cursor_col <= 0)
1997 curwin->w_cursor.col = 0;
1998 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001999 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 curwin->w_set_curswant = TRUE;
2001 changed_cline_bef_curs();
2002
2003 /*
2004 * May have to adjust the start of the insert.
2005 */
2006 if (State & INSERT)
2007 {
2008 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2009 {
2010 if ((int)Insstart.col <= insstart_less)
2011 Insstart.col = 0;
2012 else
2013 Insstart.col -= insstart_less;
2014 }
2015 if ((int)ai_col <= insstart_less)
2016 ai_col = 0;
2017 else
2018 ai_col -= insstart_less;
2019 }
2020
2021 /*
2022 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2023 * If the number of characters before the cursor decreased, need to pop a
2024 * few characters from the replace stack.
2025 * If the number of characters before the cursor increased, need to push a
2026 * few NULs onto the replace stack.
2027 */
2028 if (REPLACE_NORMAL(State) && start_col >= 0)
2029 {
2030 while (start_col > (int)curwin->w_cursor.col)
2031 {
2032 replace_join(0); /* remove a NUL from the replace stack */
2033 --start_col;
2034 }
2035 while (start_col < (int)curwin->w_cursor.col || replaced)
2036 {
2037 replace_push(NUL);
2038 if (replaced)
2039 {
2040 replace_push(replaced);
2041 replaced = NUL;
2042 }
2043 ++start_col;
2044 }
2045 }
2046
2047#ifdef FEAT_VREPLACE
2048 /*
2049 * For VREPLACE mode, we also have to fix the replace stack. In this case
2050 * it is always possible because we backspace over the whole line and then
2051 * put it back again the way we wanted it.
2052 */
2053 if (State & VREPLACE_FLAG)
2054 {
2055 /* If orig_line didn't allocate, just return. At least we did the job,
2056 * even if you can't backspace. */
2057 if (orig_line == NULL)
2058 return;
2059
2060 /* Save new line */
2061 new_line = vim_strsave(ml_get_curline());
2062 if (new_line == NULL)
2063 return;
2064
2065 /* We only put back the new line up to the cursor */
2066 new_line[curwin->w_cursor.col] = NUL;
2067
2068 /* Put back original line */
2069 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2070 curwin->w_cursor.col = orig_col;
2071
2072 /* Backspace from cursor to start of line */
2073 backspace_until_column(0);
2074
2075 /* Insert new stuff into line again */
2076 ins_bytes(new_line);
2077
2078 vim_free(new_line);
2079 }
2080#endif
2081}
2082
2083/*
2084 * Truncate the space at the end of a line. This is to be used only in an
2085 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2086 * modes.
2087 */
2088 void
2089truncate_spaces(line)
2090 char_u *line;
2091{
2092 int i;
2093
2094 /* find start of trailing white space */
2095 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2096 {
2097 if (State & REPLACE_FLAG)
2098 replace_join(0); /* remove a NUL from the replace stack */
2099 }
2100 line[i + 1] = NUL;
2101}
2102
2103#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2104 || defined(FEAT_COMMENTS) || defined(PROTO)
2105/*
2106 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2107 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002108 * Will attempt not to go before "col" even when there is a composing
2109 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 */
2111 void
2112backspace_until_column(col)
2113 int col;
2114{
2115 while ((int)curwin->w_cursor.col > col)
2116 {
2117 curwin->w_cursor.col--;
2118 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002119 replace_do_bs(col);
2120 else if (!del_char_after_col(col))
2121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123}
2124#endif
2125
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002126/*
2127 * Like del_char(), but make sure not to go before column "limit_col".
2128 * Only matters when there are composing characters.
2129 * Return TRUE when something was deleted.
2130 */
2131 static int
2132del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002133 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002134{
2135#ifdef FEAT_MBYTE
2136 if (enc_utf8 && limit_col >= 0)
2137 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002138 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002139
2140 /* Make sure the cursor is at the start of a character, but
2141 * skip forward again when going too far back because of a
2142 * composing character. */
2143 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002144 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002145 {
2146 int l = utf_ptr2len(ml_get_cursor());
2147
2148 if (l == 0) /* end of line */
2149 break;
2150 curwin->w_cursor.col += l;
2151 }
2152 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2153 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002154 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002155 }
2156 else
2157#endif
2158 (void)del_char(FALSE);
2159 return TRUE;
2160}
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2163/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002164 * CTRL-X pressed in Insert mode.
2165 */
2166 static void
2167ins_ctrl_x()
2168{
2169 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2170 * CTRL-V works like CTRL-N */
2171 if (ctrl_x_mode != CTRL_X_CMDLINE)
2172 {
2173 /* if the next ^X<> won't ADD nothing, then reset
2174 * compl_cont_status */
2175 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002176 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002177 else
2178 compl_cont_status = 0;
2179 /* We're not sure which CTRL-X mode it will be yet */
2180 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2181 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2182 edit_submode_pre = NULL;
2183 showmode();
2184 }
2185}
2186
2187/*
2188 * Return TRUE if the 'dict' or 'tsr' option can be used.
2189 */
2190 static int
2191has_compl_option(dict_opt)
2192 int dict_opt;
2193{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002194 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002195# ifdef FEAT_SPELL
2196 && !curwin->w_p_spell
2197# endif
2198 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002199 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2200 {
2201 ctrl_x_mode = 0;
2202 edit_submode = NULL;
2203 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2204 : (char_u *)_("'thesaurus' option is empty"),
2205 hl_attr(HLF_E));
2206 if (emsg_silent == 0)
2207 {
2208 vim_beep();
2209 setcursor();
2210 out_flush();
2211 ui_delay(2000L, FALSE);
2212 }
2213 return FALSE;
2214 }
2215 return TRUE;
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2220 * This depends on the current mode.
2221 */
2222 int
2223vim_is_ctrl_x_key(c)
2224 int c;
2225{
2226 /* Always allow ^R - let it's results then be checked */
2227 if (c == Ctrl_R)
2228 return TRUE;
2229
Bram Moolenaare3226be2005-12-18 22:10:00 +00002230 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002232 return TRUE;
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 switch (ctrl_x_mode)
2235 {
2236 case 0: /* Not in any CTRL-X mode */
2237 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2238 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002239 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2241 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2242 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002243 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002244 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 case CTRL_X_SCROLL:
2246 return (c == Ctrl_Y || c == Ctrl_E);
2247 case CTRL_X_WHOLE_LINE:
2248 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2249 case CTRL_X_FILES:
2250 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2251 case CTRL_X_DICTIONARY:
2252 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2253 case CTRL_X_THESAURUS:
2254 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2255 case CTRL_X_TAGS:
2256 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2257#ifdef FEAT_FIND_ID
2258 case CTRL_X_PATH_PATTERNS:
2259 return (c == Ctrl_P || c == Ctrl_N);
2260 case CTRL_X_PATH_DEFINES:
2261 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2262#endif
2263 case CTRL_X_CMDLINE:
2264 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2265 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002266#ifdef FEAT_COMPL_FUNC
2267 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002268 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002269 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002270 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002271#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002272 case CTRL_X_SPELL:
2273 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002274 case CTRL_X_EVAL:
2275 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277 EMSG(_(e_internal));
2278 return FALSE;
2279}
2280
2281/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002282 * Return TRUE when character "c" is part of the item currently being
2283 * completed. Used to decide whether to abandon complete mode when the menu
2284 * is visible.
2285 */
2286 static int
2287ins_compl_accept_char(c)
2288 int c;
2289{
2290 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2291 /* When expanding an identifier only accept identifier chars. */
2292 return vim_isIDc(c);
2293
2294 switch (ctrl_x_mode)
2295 {
2296 case CTRL_X_FILES:
2297 /* When expanding file name only accept file name chars. But not
2298 * path separators, so that "proto/<Tab>" expands files in
2299 * "proto", not "proto/" as a whole */
2300 return vim_isfilec(c) && !vim_ispathsep(c);
2301
2302 case CTRL_X_CMDLINE:
2303 case CTRL_X_OMNI:
2304 /* Command line and Omni completion can work with just about any
2305 * printable character, but do stop at white space. */
2306 return vim_isprintc(c) && !vim_iswhite(c);
2307
2308 case CTRL_X_WHOLE_LINE:
2309 /* For while line completion a space can be part of the line. */
2310 return vim_isprintc(c);
2311 }
2312 return vim_iswordc(c);
2313}
2314
2315/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002316 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002318 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 * the rest of the word to be in -- webb
2320 */
2321 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002322ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 char_u *str;
2324 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002325 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 char_u *fname;
2327 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002328 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002330 char_u *p;
2331 int i, c;
2332 int actual_len; /* Take multi-byte characters */
2333 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002334 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002335 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 int has_lower = FALSE;
2337 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002339 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002341 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaara2993e12007-08-12 14:38:46 +00002343 /* Find actual length of completion. */
2344#ifdef FEAT_MBYTE
2345 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002347 p = str;
2348 actual_len = 0;
2349 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002351 mb_ptr_adv(p);
2352 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002355 else
2356#endif
2357 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
Bram Moolenaara2993e12007-08-12 14:38:46 +00002359 /* Find actual length of original text. */
2360#ifdef FEAT_MBYTE
2361 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002363 p = compl_orig_text;
2364 actual_compl_length = 0;
2365 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002367 mb_ptr_adv(p);
2368 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002371 else
2372#endif
2373 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002375 /* "actual_len" may be smaller than "actual_compl_length" when using
2376 * thesaurus, only use the minimum when comparing. */
2377 min_len = actual_len < actual_compl_length
2378 ? actual_len : actual_compl_length;
2379
Bram Moolenaara2993e12007-08-12 14:38:46 +00002380 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002381 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002382 if (wca != NULL)
2383 {
2384 p = str;
2385 for (i = 0; i < actual_len; ++i)
2386#ifdef FEAT_MBYTE
2387 if (has_mbyte)
2388 wca[i] = mb_ptr2char_adv(&p);
2389 else
2390#endif
2391 wca[i] = *(p++);
2392
2393 /* Rule 1: Were any chars converted to lower? */
2394 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002395 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002396 {
2397#ifdef FEAT_MBYTE
2398 if (has_mbyte)
2399 c = mb_ptr2char_adv(&p);
2400 else
2401#endif
2402 c = *(p++);
2403 if (MB_ISLOWER(c))
2404 {
2405 has_lower = TRUE;
2406 if (MB_ISUPPER(wca[i]))
2407 {
2408 /* Rule 1 is satisfied. */
2409 for (i = actual_compl_length; i < actual_len; ++i)
2410 wca[i] = MB_TOLOWER(wca[i]);
2411 break;
2412 }
2413 }
2414 }
2415
2416 /*
2417 * Rule 2: No lower case, 2nd consecutive letter converted to
2418 * upper case.
2419 */
2420 if (!has_lower)
2421 {
2422 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002423 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002424 {
2425#ifdef FEAT_MBYTE
2426 if (has_mbyte)
2427 c = mb_ptr2char_adv(&p);
2428 else
2429#endif
2430 c = *(p++);
2431 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2432 {
2433 /* Rule 2 is satisfied. */
2434 for (i = actual_compl_length; i < actual_len; ++i)
2435 wca[i] = MB_TOUPPER(wca[i]);
2436 break;
2437 }
2438 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2439 }
2440 }
2441
2442 /* Copy the original case of the part we typed. */
2443 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002444 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002445 {
2446#ifdef FEAT_MBYTE
2447 if (has_mbyte)
2448 c = mb_ptr2char_adv(&p);
2449 else
2450#endif
2451 c = *(p++);
2452 if (MB_ISLOWER(c))
2453 wca[i] = MB_TOLOWER(wca[i]);
2454 else if (MB_ISUPPER(c))
2455 wca[i] = MB_TOUPPER(wca[i]);
2456 }
2457
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002458 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002459 * Generate encoding specific output from wide character array.
2460 * Multi-byte characters can occupy up to five bytes more than
2461 * ASCII characters, and we also need one byte for NUL, so stay
2462 * six bytes away from the edge of IObuff.
2463 */
2464 p = IObuff;
2465 i = 0;
2466 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2467#ifdef FEAT_MBYTE
2468 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002469 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002470 else
2471#endif
2472 *(p++) = wca[i++];
2473 *p = NUL;
2474
2475 vim_free(wca);
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002478 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2479 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002481 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482}
2483
2484/*
2485 * Add a match to the list of matches.
2486 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002487 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002488 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002490 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 char_u *str;
2493 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002494 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002496 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002497 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002498 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002501 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002502 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
2504 ui_breakcheck();
2505 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002506 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (len < 0)
2508 len = (int)STRLEN(str);
2509
2510 /*
2511 * If the same match is already present, don't add it.
2512 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002513 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002515 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 do
2517 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002518 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002519 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 && match->cp_str[len] == NUL)
2521 return NOTDONE;
2522 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002523 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
2525
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002526 /* Remove any popup menu before changing the list of matches. */
2527 ins_compl_del_pum();
2528
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 /*
2530 * Allocate a new match structure.
2531 * Copy the values to the new match structure.
2532 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002533 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002535 return FAIL;
2536 match->cp_number = -1;
2537 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002538 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002539 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002542 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002544 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002547 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2549 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002551 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002552 && compl_curr_match->cp_fname != NULL
2553 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002554 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002555 else if (fname != NULL)
2556 {
2557 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002558 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 match->cp_fname = NULL;
2562 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002563
2564 if (cptext != NULL)
2565 {
2566 int i;
2567
2568 for (i = 0; i < CPT_COUNT; ++i)
2569 if (cptext[i] != NULL && *cptext[i] != NUL)
2570 match->cp_text[i] = vim_strsave(cptext[i]);
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 /*
2574 * Link the new match structure in the list of matches.
2575 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002576 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 else if (dir == FORWARD)
2579 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002580 match->cp_next = compl_curr_match->cp_next;
2581 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 else /* BACKWARD */
2584 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002585 match->cp_next = compl_curr_match;
2586 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002588 if (match->cp_next)
2589 match->cp_next->cp_prev = match;
2590 if (match->cp_prev)
2591 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002593 compl_first_match = match;
2594 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002596 /*
2597 * Find the longest common string if still doing that.
2598 */
2599 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2600 ins_compl_longest_match(match);
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return OK;
2603}
2604
2605/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002606 * Return TRUE if "str[len]" matches with match->cp_str, considering
2607 * match->cp_icase.
2608 */
2609 static int
2610ins_compl_equal(match, str, len)
2611 compl_T *match;
2612 char_u *str;
2613 int len;
2614{
2615 if (match->cp_icase)
2616 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2617 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2618}
2619
2620/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002621 * Reduce the longest common string for match "match".
2622 */
2623 static void
2624ins_compl_longest_match(match)
2625 compl_T *match;
2626{
2627 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002628 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002629 int had_match;
2630
2631 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002632 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002633 /* First match, use it as a whole. */
2634 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002635 if (compl_leader != NULL)
2636 {
2637 had_match = (curwin->w_cursor.col > compl_col);
2638 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002639 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002640 ins_redraw(FALSE);
2641
2642 /* When the match isn't there (to avoid matching itself) remove it
2643 * again after redrawing. */
2644 if (!had_match)
2645 ins_compl_delete();
2646 compl_used_match = FALSE;
2647 }
2648 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002649 else
2650 {
2651 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 p = compl_leader;
2653 s = match->cp_str;
2654 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002655 {
2656#ifdef FEAT_MBYTE
2657 if (has_mbyte)
2658 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659 c1 = mb_ptr2char(p);
2660 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002661 }
2662 else
2663#endif
2664 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002665 c1 = *p;
2666 c2 = *s;
2667 }
2668 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2669 : (c1 != c2))
2670 break;
2671#ifdef FEAT_MBYTE
2672 if (has_mbyte)
2673 {
2674 mb_ptr_adv(p);
2675 mb_ptr_adv(s);
2676 }
2677 else
2678#endif
2679 {
2680 ++p;
2681 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002682 }
2683 }
2684
2685 if (*p != NUL)
2686 {
2687 /* Leader was shortened, need to change the inserted text. */
2688 *p = NUL;
2689 had_match = (curwin->w_cursor.col > compl_col);
2690 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002691 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002692 ins_redraw(FALSE);
2693
2694 /* When the match isn't there (to avoid matching itself) remove it
2695 * again after redrawing. */
2696 if (!had_match)
2697 ins_compl_delete();
2698 }
2699
2700 compl_used_match = FALSE;
2701 }
2702}
2703
2704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 * Add an array of matches to the list of matches.
2706 * Frees matches[].
2707 */
2708 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002709ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 int num_matches;
2711 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002712 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 int i;
2715 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002716 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
Bram Moolenaar572cb562005-08-05 21:35:02 +00002718 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002719 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002720 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002722 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 FreeWild(num_matches, matches);
2724}
2725
2726/* Make the completion list cyclic.
2727 * Return the number of matches (excluding the original).
2728 */
2729 static int
2730ins_compl_make_cyclic()
2731{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002732 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 int count = 0;
2734
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002735 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
2737 /*
2738 * Find the end of the list.
2739 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002740 match = compl_first_match;
2741 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002744 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 ++count;
2746 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 match->cp_next = compl_first_match;
2748 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 return count;
2751}
2752
Bram Moolenaara94bc432006-03-10 21:42:59 +00002753/*
2754 * Start completion for the complete() function.
2755 * "startcol" is where the matched text starts (1 is first column).
2756 * "list" is the list of matches.
2757 */
2758 void
2759set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002760 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002761 list_T *list;
2762{
2763 /* If already doing completions stop it. */
2764 if (ctrl_x_mode != 0)
2765 ins_compl_prep(' ');
2766 ins_compl_clear();
2767
2768 if (stop_arrow() == FAIL)
2769 return;
2770
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002771 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002772 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002773 startcol = curwin->w_cursor.col;
2774 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002775 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002776 /* compl_pattern doesn't need to be set */
2777 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2778 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002779 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002780 return;
2781
Bram Moolenaare4214502015-03-05 18:08:43 +01002782 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002783
2784 ins_compl_add_list(list);
2785 compl_matches = ins_compl_make_cyclic();
2786 compl_started = TRUE;
2787 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002788 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002789
2790 compl_curr_match = compl_first_match;
2791 ins_complete(Ctrl_N);
2792 out_flush();
2793}
2794
2795
Bram Moolenaar9372a112005-12-06 19:59:18 +00002796/* "compl_match_array" points the currently displayed list of entries in the
2797 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002798static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002799static int compl_match_arraysize;
2800
2801/*
2802 * Update the screen and when there is any scrolling remove the popup menu.
2803 */
2804 static void
2805ins_compl_upd_pum()
2806{
2807 int h;
2808
2809 if (compl_match_array != NULL)
2810 {
2811 h = curwin->w_cline_height;
2812 update_screen(0);
2813 if (h != curwin->w_cline_height)
2814 ins_compl_del_pum();
2815 }
2816}
2817
2818/*
2819 * Remove any popup menu.
2820 */
2821 static void
2822ins_compl_del_pum()
2823{
2824 if (compl_match_array != NULL)
2825 {
2826 pum_undisplay();
2827 vim_free(compl_match_array);
2828 compl_match_array = NULL;
2829 }
2830}
2831
2832/*
2833 * Return TRUE if the popup menu should be displayed.
2834 */
2835 static int
2836pum_wanted()
2837{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002838 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002839 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002840 return FALSE;
2841
2842 /* The display looks bad on a B&W display. */
2843 if (t_colors < 8
2844#ifdef FEAT_GUI
2845 && !gui.in_use
2846#endif
2847 )
2848 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002849 return TRUE;
2850}
2851
2852/*
2853 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002854 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002855 */
2856 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002857pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002858{
2859 compl_T *compl;
2860 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002861
2862 /* Don't display the popup menu if there are no matches or there is only
2863 * one (ignoring the original text). */
2864 compl = compl_first_match;
2865 i = 0;
2866 do
2867 {
2868 if (compl == NULL
2869 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2870 break;
2871 compl = compl->cp_next;
2872 } while (compl != compl_first_match);
2873
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002874 if (strstr((char *)p_cot, "menuone") != NULL)
2875 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002876 return (i >= 2);
2877}
2878
2879/*
2880 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002881 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002882 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002883 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002884ins_compl_show_pum()
2885{
2886 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002887 compl_T *shown_compl = NULL;
2888 int did_find_shown_match = FALSE;
2889 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002890 int i;
2891 int cur = -1;
2892 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002893 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002894
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002895 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002896 return;
2897
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002898#if defined(FEAT_EVAL)
2899 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2900 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2901#endif
2902
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903 /* Update the screen before drawing the popup menu over it. */
2904 update_screen(0);
2905
2906 if (compl_match_array == NULL)
2907 {
2908 /* Need to build the popup menu list. */
2909 compl_match_arraysize = 0;
2910 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002911 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002912 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002913 do
2914 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002915 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2916 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002917 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002918 ++compl_match_arraysize;
2919 compl = compl->cp_next;
2920 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002921 if (compl_match_arraysize == 0)
2922 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002923 compl_match_array = (pumitem_T *)alloc_clear(
2924 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002925 * compl_match_arraysize));
2926 if (compl_match_array != NULL)
2927 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002928 /* If the current match is the original text don't find the first
2929 * match after it, don't highlight anything. */
2930 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2931 shown_match_ok = TRUE;
2932
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002933 i = 0;
2934 compl = compl_first_match;
2935 do
2936 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002937 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2938 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002939 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002941 if (!shown_match_ok)
2942 {
2943 if (compl == compl_shown_match || did_find_shown_match)
2944 {
2945 /* This item is the shown match or this is the
2946 * first displayed item after the shown match. */
2947 compl_shown_match = compl;
2948 did_find_shown_match = TRUE;
2949 shown_match_ok = TRUE;
2950 }
2951 else
2952 /* Remember this displayed match for when the
2953 * shown match is just below it. */
2954 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002956 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002957
2958 if (compl->cp_text[CPT_ABBR] != NULL)
2959 compl_match_array[i].pum_text =
2960 compl->cp_text[CPT_ABBR];
2961 else
2962 compl_match_array[i].pum_text = compl->cp_str;
2963 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2964 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2965 if (compl->cp_text[CPT_MENU] != NULL)
2966 compl_match_array[i++].pum_extra =
2967 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002968 else
2969 compl_match_array[i++].pum_extra = compl->cp_fname;
2970 }
2971
2972 if (compl == compl_shown_match)
2973 {
2974 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002975
2976 /* When the original text is the shown match don't set
2977 * compl_shown_match. */
2978 if (compl->cp_flags & ORIGINAL_TEXT)
2979 shown_match_ok = TRUE;
2980
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002981 if (!shown_match_ok && shown_compl != NULL)
2982 {
2983 /* The shown match isn't displayed, set it to the
2984 * previously displayed match. */
2985 compl_shown_match = shown_compl;
2986 shown_match_ok = TRUE;
2987 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002988 }
2989 compl = compl->cp_next;
2990 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002991
2992 if (!shown_match_ok) /* no displayed match at all */
2993 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002994 }
2995 }
2996 else
2997 {
2998 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002999 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003000 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3001 || compl_match_array[i].pum_text
3002 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003003 {
3004 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003005 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003006 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003007 }
3008
3009 if (compl_match_array != NULL)
3010 {
3011 /* Compute the screen column of the start of the completed text.
3012 * Use the cursor to get all wrapping and other settings right. */
3013 col = curwin->w_cursor.col;
3014 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003015 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003016 curwin->w_cursor.col = col;
3017 }
3018}
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020#define DICT_FIRST (1) /* use just first element in "dict" */
3021#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003024 * Add any identifiers that match the given pattern in the list of dictionary
3025 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
3027 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003028ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3029 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003031 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003032 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003034 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 char_u *ptr;
3036 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 char_u **files;
3039 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003041 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaar0b238792006-03-02 22:49:12 +00003043 if (*dict == NUL)
3044 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003045#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003046 /* When 'dictionary' is empty and spell checking is enabled use
3047 * "spell". */
3048 if (!thesaurus && curwin->w_p_spell)
3049 dict = (char_u *)"spell";
3050 else
3051#endif
3052 return;
3053 }
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003056 if (buf == NULL)
3057 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003058 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 /* If 'infercase' is set, don't use 'smartcase' here */
3061 save_p_scs = p_scs;
3062 if (curbuf->b_p_inf)
3063 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003064
3065 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3066 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003067 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003068 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003069 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003070 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003071 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003072
3073 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003074 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003075 len = STRLEN(pat_esc) + 10;
3076 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003077 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003078 {
3079 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003080 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003081 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003082 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003083 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3084 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003085 vim_free(ptr);
3086 }
3087 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003088 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003089 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003090 if (regmatch.regprog == NULL)
3091 goto theend;
3092 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3095 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003096 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 /* copy one dictionary file name into buf */
3099 if (flags == DICT_EXACT)
3100 {
3101 count = 1;
3102 files = &dict;
3103 }
3104 else
3105 {
3106 /* Expand wildcards in the dictionary name, but do not allow
3107 * backticks (for security, the 'dict' option may have been set in
3108 * a modeline). */
3109 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003110# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003111 if (!thesaurus && STRCMP(buf, "spell") == 0)
3112 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003113 else
3114# endif
3115 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 || expand_wildcards(1, &buf, &count, &files,
3117 EW_FILE|EW_SILENT) != OK)
3118 count = 0;
3119 }
3120
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003121# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003122 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003124 /* Complete from active spelling. Skip "\<" in the pattern, we
3125 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126 if (pat[0] == '\\' && pat[1] == '<')
3127 ptr = pat + 2;
3128 else
3129 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003130 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003132 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003133# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003134 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003135 {
3136 ins_compl_files(count, files, thesaurus, flags,
3137 &regmatch, buf, &dir);
3138 if (flags != DICT_EXACT)
3139 FreeWild(count, files);
3140 }
3141 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 break;
3143 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003144
3145theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003147 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 vim_free(buf);
3149}
3150
Bram Moolenaar0b238792006-03-02 22:49:12 +00003151 static void
3152ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3153 int count;
3154 char_u **files;
3155 int thesaurus;
3156 int flags;
3157 regmatch_T *regmatch;
3158 char_u *buf;
3159 int *dir;
3160{
3161 char_u *ptr;
3162 int i;
3163 FILE *fp;
3164 int add_r;
3165
3166 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3167 {
3168 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3169 if (flags != DICT_EXACT)
3170 {
3171 vim_snprintf((char *)IObuff, IOSIZE,
3172 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003173 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003174 }
3175
3176 if (fp != NULL)
3177 {
3178 /*
3179 * Read dictionary file line by line.
3180 * Check each line for a match.
3181 */
3182 while (!got_int && !compl_interrupted
3183 && !vim_fgets(buf, LSIZE, fp))
3184 {
3185 ptr = buf;
3186 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3187 {
3188 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003189 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003190 ptr = find_line_end(ptr);
3191 else
3192 ptr = find_word_end(ptr);
3193 add_r = ins_compl_add_infercase(regmatch->startp[0],
3194 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003195 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003196 if (thesaurus)
3197 {
3198 char_u *wstart;
3199
3200 /*
3201 * Add the other matches on the line
3202 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003203 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 while (!got_int)
3205 {
3206 /* Find start of the next word. Skip white
3207 * space and punctuation. */
3208 ptr = find_word_start(ptr);
3209 if (*ptr == NUL || *ptr == NL)
3210 break;
3211 wstart = ptr;
3212
Bram Moolenaara2993e12007-08-12 14:38:46 +00003213 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003214#ifdef FEAT_MBYTE
3215 if (has_mbyte)
3216 /* Japanese words may have characters in
3217 * different classes, only separate words
3218 * with single-byte non-word characters. */
3219 while (*ptr != NUL)
3220 {
3221 int l = (*mb_ptr2len)(ptr);
3222
3223 if (l < 2 && !vim_iswordc(*ptr))
3224 break;
3225 ptr += l;
3226 }
3227 else
3228#endif
3229 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003230
3231 /* Add the word. Skip the regexp match. */
3232 if (wstart != regmatch->startp[0])
3233 add_r = ins_compl_add_infercase(wstart,
3234 (int)(ptr - wstart),
3235 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003236 }
3237 }
3238 if (add_r == OK)
3239 /* if dir was BACKWARD then honor it just once */
3240 *dir = FORWARD;
3241 else if (add_r == FAIL)
3242 break;
3243 /* avoid expensive call to vim_regexec() when at end
3244 * of line */
3245 if (*ptr == '\n' || got_int)
3246 break;
3247 }
3248 line_breakcheck();
3249 ins_compl_check_keys(50);
3250 }
3251 fclose(fp);
3252 }
3253 }
3254}
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256/*
3257 * Find the start of the next word.
3258 * Returns a pointer to the first char of the word. Also stops at a NUL.
3259 */
3260 char_u *
3261find_word_start(ptr)
3262 char_u *ptr;
3263{
3264#ifdef FEAT_MBYTE
3265 if (has_mbyte)
3266 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003267 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else
3269#endif
3270 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3271 ++ptr;
3272 return ptr;
3273}
3274
3275/*
3276 * Find the end of the word. Assumes it starts inside a word.
3277 * Returns a pointer to just after the word.
3278 */
3279 char_u *
3280find_word_end(ptr)
3281 char_u *ptr;
3282{
3283#ifdef FEAT_MBYTE
3284 int start_class;
3285
3286 if (has_mbyte)
3287 {
3288 start_class = mb_get_class(ptr);
3289 if (start_class > 1)
3290 while (*ptr != NUL)
3291 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003292 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (mb_get_class(ptr) != start_class)
3294 break;
3295 }
3296 }
3297 else
3298#endif
3299 while (vim_iswordc(*ptr))
3300 ++ptr;
3301 return ptr;
3302}
3303
3304/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003305 * Find the end of the line, omitting CR and NL at the end.
3306 * Returns a pointer to just after the line.
3307 */
3308 static char_u *
3309find_line_end(ptr)
3310 char_u *ptr;
3311{
3312 char_u *s;
3313
3314 s = ptr + STRLEN(ptr);
3315 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3316 --s;
3317 return s;
3318}
3319
3320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * Free the list of completions
3322 */
3323 static void
3324ins_compl_free()
3325{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003326 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003327 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003329 vim_free(compl_pattern);
3330 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003331 vim_free(compl_leader);
3332 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003334 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003336
3337 ins_compl_del_pum();
3338 pum_clear();
3339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003340 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 do
3342 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003344 compl_curr_match = compl_curr_match->cp_next;
3345 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003347 if (match->cp_flags & FREE_FNAME)
3348 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003349 for (i = 0; i < CPT_COUNT; ++i)
3350 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003352 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3353 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003354 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355}
3356
3357 static void
3358ins_compl_clear()
3359{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003360 compl_cont_status = 0;
3361 compl_started = FALSE;
3362 compl_matches = 0;
3363 vim_free(compl_pattern);
3364 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003365 vim_free(compl_leader);
3366 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003368 vim_free(compl_orig_text);
3369 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003370 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003374 * Return TRUE when Insert completion is active.
3375 */
3376 int
3377ins_compl_active()
3378{
3379 return compl_started;
3380}
3381
3382/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003383 * Delete one character before the cursor and show the subset of the matches
3384 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003385 * Returns the character to be used, NUL if the work is done and another char
3386 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003387 */
3388 static int
3389ins_compl_bs()
3390{
3391 char_u *line;
3392 char_u *p;
3393
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003394 line = ml_get_curline();
3395 p = line + curwin->w_cursor.col;
3396 mb_ptr_back(line, p);
3397
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003398 /* Stop completion when the whole word was deleted. For Omni completion
3399 * allow the word to be deleted, we won't match everything. */
3400 if ((int)(p - line) - (int)compl_col < 0
3401 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003402 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003403 return K_BS;
3404
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003405 /* Deleted more than what was used to find matches or didn't finish
3406 * finding all matches: need to look for matches all over again. */
3407 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003408 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003409 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003410
Bram Moolenaara6557602006-02-04 22:43:20 +00003411 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003412 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003413 if (compl_leader != NULL)
3414 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003415 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003416 if (compl_shown_match != NULL)
3417 /* Make sure current match is not a hidden item. */
3418 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003419 return NUL;
3420 }
3421 return K_BS;
3422}
Bram Moolenaara6557602006-02-04 22:43:20 +00003423
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003424/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003425 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3426 * be called.
3427 */
3428 static int
3429ins_compl_need_restart()
3430{
3431 /* Return TRUE if we didn't complete finding matches or when the
3432 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3433 return compl_was_interrupted
3434 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3435 && compl_opt_refresh_always);
3436}
3437
3438/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003439 * Called after changing "compl_leader".
3440 * Show the popup menu with a different set of matches.
3441 * May also search for matches again if the previous search was interrupted.
3442 */
3443 static void
3444ins_compl_new_leader()
3445{
3446 ins_compl_del_pum();
3447 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003448 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003449 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003450
3451 if (compl_started)
3452 ins_compl_set_original_text(compl_leader);
3453 else
3454 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003455#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003456 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003457#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003458 /*
3459 * Matches were cleared, need to search for them now. First display
3460 * the changed text before the cursor. Set "compl_restarting" to
3461 * avoid that the first match is inserted.
3462 */
3463 update_screen(0);
3464#ifdef FEAT_GUI
3465 if (gui.in_use)
3466 {
3467 /* Show the cursor after the match, not after the redrawn text. */
3468 setcursor();
3469 out_flush();
3470 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003471 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472#endif
3473 compl_restarting = TRUE;
3474 if (ins_complete(Ctrl_N) == FAIL)
3475 compl_cont_status = 0;
3476 compl_restarting = FALSE;
3477 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003478
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003479 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003480
3481 /* Show the popup menu with a different set of matches. */
3482 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003483
3484 /* Don't let Enter select the original text when there is no popup menu. */
3485 if (compl_match_array == NULL)
3486 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003487}
3488
3489/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003490 * Return the length of the completion, from the completion start column to
3491 * the cursor column. Making sure it never goes below zero.
3492 */
3493 static int
3494ins_compl_len()
3495{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003496 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003497
3498 if (off < 0)
3499 return 0;
3500 return off;
3501}
3502
3503/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003504 * Append one character to the match leader. May reduce the number of
3505 * matches.
3506 */
3507 static void
3508ins_compl_addleader(c)
3509 int c;
3510{
3511#ifdef FEAT_MBYTE
3512 int cc;
3513
3514 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3515 {
3516 char_u buf[MB_MAXBYTES + 1];
3517
3518 (*mb_char2bytes)(c, buf);
3519 buf[cc] = NUL;
3520 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003521 if (compl_opt_refresh_always)
3522 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003523 }
3524 else
3525#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003526 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003527 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003528 if (compl_opt_refresh_always)
3529 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003530 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003531
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003532 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003533 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003534 ins_compl_restart();
3535
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003536 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003537 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003538 * break redo. */
3539 if (!compl_opt_refresh_always)
3540 {
3541 vim_free(compl_leader);
3542 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003543 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003544 if (compl_leader != NULL)
3545 ins_compl_new_leader();
3546 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003547}
3548
3549/*
3550 * Setup for finding completions again without leaving CTRL-X mode. Used when
3551 * BS or a key was typed while still searching for matches.
3552 */
3553 static void
3554ins_compl_restart()
3555{
3556 ins_compl_free();
3557 compl_started = FALSE;
3558 compl_matches = 0;
3559 compl_cont_status = 0;
3560 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003561}
3562
3563/*
3564 * Set the first match, the original text.
3565 */
3566 static void
3567ins_compl_set_original_text(str)
3568 char_u *str;
3569{
3570 char_u *p;
3571
3572 /* Replace the original text entry. */
3573 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3574 {
3575 p = vim_strsave(str);
3576 if (p != NULL)
3577 {
3578 vim_free(compl_first_match->cp_str);
3579 compl_first_match->cp_str = p;
3580 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003581 }
3582}
3583
3584/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003585 * Append one character to the match leader. May reduce the number of
3586 * matches.
3587 */
3588 static void
3589ins_compl_addfrommatch()
3590{
3591 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003592 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003593 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003594 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003595
3596 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003597 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003598 {
3599 /* When still at the original match use the first entry that matches
3600 * the leader. */
3601 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3602 {
3603 p = NULL;
3604 for (cp = compl_shown_match->cp_next; cp != NULL
3605 && cp != compl_first_match; cp = cp->cp_next)
3606 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003607 if (compl_leader == NULL
3608 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003609 (int)STRLEN(compl_leader)))
3610 {
3611 p = cp->cp_str;
3612 break;
3613 }
3614 }
3615 if (p == NULL || (int)STRLEN(p) <= len)
3616 return;
3617 }
3618 else
3619 return;
3620 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003621 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003622 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003623 ins_compl_addleader(c);
3624}
3625
3626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003628 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003629 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003631 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632ins_compl_prep(c)
3633 int c;
3634{
3635 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003637 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 /* Forget any previous 'special' messages if this is actually
3640 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3641 */
3642 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3643 edit_submode_extra = NULL;
3644
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003645 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003646 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3647 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003648 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003650 /* Set "compl_get_longest" when finding the first matches. */
3651 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3652 || (ctrl_x_mode == 0 && !compl_started))
3653 {
3654 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3655 compl_used_match = TRUE;
3656 }
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3659 {
3660 /*
3661 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3662 * it will be yet. Now we decide.
3663 */
3664 switch (c)
3665 {
3666 case Ctrl_E:
3667 case Ctrl_Y:
3668 ctrl_x_mode = CTRL_X_SCROLL;
3669 if (!(State & REPLACE_FLAG))
3670 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3671 else
3672 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3673 edit_submode_pre = NULL;
3674 showmode();
3675 break;
3676 case Ctrl_L:
3677 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3678 break;
3679 case Ctrl_F:
3680 ctrl_x_mode = CTRL_X_FILES;
3681 break;
3682 case Ctrl_K:
3683 ctrl_x_mode = CTRL_X_DICTIONARY;
3684 break;
3685 case Ctrl_R:
3686 /* Simply allow ^R to happen without affecting ^X mode */
3687 break;
3688 case Ctrl_T:
3689 ctrl_x_mode = CTRL_X_THESAURUS;
3690 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003691#ifdef FEAT_COMPL_FUNC
3692 case Ctrl_U:
3693 ctrl_x_mode = CTRL_X_FUNCTION;
3694 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003695 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003696 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003698#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003699 case 's':
3700 case Ctrl_S:
3701 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003702#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003703 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003704 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003705 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003706#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 case Ctrl_RSB:
3709 ctrl_x_mode = CTRL_X_TAGS;
3710 break;
3711#ifdef FEAT_FIND_ID
3712 case Ctrl_I:
3713 case K_S_TAB:
3714 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3715 break;
3716 case Ctrl_D:
3717 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3718 break;
3719#endif
3720 case Ctrl_V:
3721 case Ctrl_Q:
3722 ctrl_x_mode = CTRL_X_CMDLINE;
3723 break;
3724 case Ctrl_P:
3725 case Ctrl_N:
3726 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3727 * just started ^X mode, or there were enough ^X's to cancel
3728 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3729 * do normal expansion when interrupting a different mode (say
3730 * ^X^F^X^P or ^P^X^X^P, see below)
3731 * nothing changes if interrupting mode 0, (eg, the flag
3732 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 if (!(compl_cont_status & CONT_INTRPT))
3734 compl_cont_status |= CONT_LOCAL;
3735 else if (compl_cont_mode != 0)
3736 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 /* FALLTHROUGH */
3738 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003739 /* If we have typed at least 2 ^X's... for modes != 0, we set
3740 * compl_cont_status = 0 (eg, as if we had just started ^X
3741 * mode).
3742 * For mode 0, we set "compl_cont_mode" to an impossible
3743 * value, in both cases ^X^X can be used to restart the same
3744 * mode (avoiding ADDING mode).
3745 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3746 * 'complete' and local ^P expansions respectively.
3747 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3748 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (c == Ctrl_X)
3750 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003751 if (compl_cont_mode != 0)
3752 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003754 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 ctrl_x_mode = 0;
3757 edit_submode = NULL;
3758 showmode();
3759 break;
3760 }
3761 }
3762 else if (ctrl_x_mode != 0)
3763 {
3764 /* We're already in CTRL-X mode, do we stay in it? */
3765 if (!vim_is_ctrl_x_key(c))
3766 {
3767 if (ctrl_x_mode == CTRL_X_SCROLL)
3768 ctrl_x_mode = 0;
3769 else
3770 ctrl_x_mode = CTRL_X_FINISHED;
3771 edit_submode = NULL;
3772 }
3773 showmode();
3774 }
3775
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
3778 /* Show error message from attempted keyword completion (probably
3779 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003782 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3783 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 || ctrl_x_mode == CTRL_X_FINISHED)
3785 {
3786 /* Get here when we have finished typing a sequence of ^N and
3787 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003788 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003789 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
3791 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003792 * If any of the original typed text has been changed, eg when
3793 * ignorecase is set, we must add back-spaces to the redo
3794 * buffer. We add as few as necessary to delete just the part
3795 * of the original text that has changed.
3796 * When using the longest match, edited the match or used
3797 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003799 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3800 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003801 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003802 ptr = NULL;
3803 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805
3806#ifdef FEAT_CINDENT
3807 want_cindent = (can_cindent && cindent_on());
3808#endif
3809 /*
3810 * When completing whole lines: fix indent for 'cindent'.
3811 * Otherwise, break line if it's too long.
3812 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815#ifdef FEAT_CINDENT
3816 /* re-indent the current line */
3817 if (want_cindent)
3818 {
3819 do_c_expr_indent();
3820 want_cindent = FALSE; /* don't do it again */
3821 }
3822#endif
3823 }
3824 else
3825 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003826 int prev_col = curwin->w_cursor.col;
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003829 if (prev_col > 0)
3830 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (stop_arrow() == OK)
3832 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003833 if (prev_col > 0
3834 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3835 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003838 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003839 * the selection without inserting anything. When
3840 * compl_enter_selects is set the Enter key does the same. */
3841 if ((c == Ctrl_Y || (compl_enter_selects
3842 && (c == CAR || c == K_KENTER || c == NL)))
3843 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003844 retval = TRUE;
3845
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003846 /* CTRL-E means completion is Ended, go back to the typed text. */
3847 if (c == Ctrl_E)
3848 {
3849 ins_compl_delete();
3850 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003851 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003852 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003853 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003854 retval = TRUE;
3855 }
3856
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003857 auto_format(FALSE, TRUE);
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 compl_started = FALSE;
3861 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003862 if (!shortmess(SHM_COMPLETIONMENU))
3863 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003865 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (edit_submode != NULL)
3867 {
3868 edit_submode = NULL;
3869 showmode();
3870 }
3871
3872#ifdef FEAT_CINDENT
3873 /*
3874 * Indent now if a key was typed that is in 'cinkeys'.
3875 */
3876 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3877 do_c_expr_indent();
3878#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003879#ifdef FEAT_AUTOCMD
3880 /* Trigger the CompleteDone event to give scripts a chance to act
3881 * upon the completion. */
3882 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003886#ifdef FEAT_AUTOCMD
3887 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3888 /* Trigger the CompleteDone event to give scripts a chance to act
3889 * upon the (possibly failed) completion. */
3890 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893 /* reset continue_* if we left expansion-mode, if we stay they'll be
3894 * (re)set properly in ins_complete() */
3895 if (!vim_is_ctrl_x_key(c))
3896 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897 compl_cont_status = 0;
3898 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003900
3901 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902}
3903
3904/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003905 * Fix the redo buffer for the completion leader replacing some of the typed
3906 * text. This inserts backspaces and appends the changed text.
3907 * "ptr" is the known leader text or NUL.
3908 */
3909 static void
3910ins_compl_fixRedoBufForLeader(ptr_arg)
3911 char_u *ptr_arg;
3912{
3913 int len;
3914 char_u *p;
3915 char_u *ptr = ptr_arg;
3916
3917 if (ptr == NULL)
3918 {
3919 if (compl_leader != NULL)
3920 ptr = compl_leader;
3921 else
3922 return; /* nothing to do */
3923 }
3924 if (compl_orig_text != NULL)
3925 {
3926 p = compl_orig_text;
3927 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3928 ;
3929#ifdef FEAT_MBYTE
3930 if (len > 0)
3931 len -= (*mb_head_off)(p, p + len);
3932#endif
3933 for (p += len; *p != NUL; mb_ptr_adv(p))
3934 AppendCharToRedobuff(K_BS);
3935 }
3936 else
3937 len = 0;
3938 if (ptr != NULL)
3939 AppendToRedobuffLit(ptr + len, -1);
3940}
3941
3942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3944 * (depending on flag) starting from buf and looking for a non-scanned
3945 * buffer (other than curbuf). curbuf is special, if it is called with
3946 * buf=curbuf then it has to be the first call for a given flag/expansion.
3947 *
3948 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3949 */
3950 static buf_T *
3951ins_compl_next_buf(buf, flag)
3952 buf_T *buf;
3953 int flag;
3954{
3955#ifdef FEAT_WINDOWS
3956 static win_T *wp;
3957#endif
3958
3959 if (flag == 'w') /* just windows */
3960 {
3961#ifdef FEAT_WINDOWS
3962 if (buf == curbuf) /* first call for this flag/expansion */
3963 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003964 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 && wp->w_buffer->b_scanned)
3966 ;
3967 buf = wp->w_buffer;
3968#else
3969 buf = curbuf;
3970#endif
3971 }
3972 else
3973 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3974 * (unlisted buffers)
3975 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003976 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 && ((flag == 'U'
3978 ? buf->b_p_bl
3979 : (!buf->b_p_bl
3980 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003981 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 ;
3983 return buf;
3984}
3985
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003986#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003987static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003988
3989/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003990 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003991 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003992 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003993 static void
3994expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003995 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003996 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003997{
Bram Moolenaar82139082011-09-14 16:52:09 +02003998 list_T *matchlist = NULL;
3999 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004000 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004001 char_u *funcname;
4002 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004003 win_T *curwin_save;
4004 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004005 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004006
Bram Moolenaare344bea2005-09-01 20:46:49 +00004007 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4008 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004009 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004010
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004011 /* Call 'completefunc' to obtain the list of matches. */
4012 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004013 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004014
Bram Moolenaare344bea2005-09-01 20:46:49 +00004015 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004016 curwin_save = curwin;
4017 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004018
4019 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004020 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004021 {
4022 switch (rettv.v_type)
4023 {
4024 case VAR_LIST:
4025 matchlist = rettv.vval.v_list;
4026 break;
4027 case VAR_DICT:
4028 matchdict = rettv.vval.v_dict;
4029 break;
4030 default:
4031 /* TODO: Give error message? */
4032 clear_tv(&rettv);
4033 break;
4034 }
4035 }
4036
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004037 if (curwin_save != curwin || curbuf_save != curbuf)
4038 {
4039 EMSG(_(e_complwin));
4040 goto theend;
4041 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004042 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004043 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004044 if (!equalpos(curwin->w_cursor, pos))
4045 {
4046 EMSG(_(e_compldel));
4047 goto theend;
4048 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004049
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004050 if (matchlist != NULL)
4051 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004052 else if (matchdict != NULL)
4053 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004054
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004055theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004056 if (matchdict != NULL)
4057 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004058 if (matchlist != NULL)
4059 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004060}
4061#endif /* FEAT_COMPL_FUNC */
4062
Bram Moolenaar39f05632006-03-19 22:15:26 +00004063#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004064/*
4065 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004066 */
4067 static void
4068ins_compl_add_list(list)
4069 list_T *list;
4070{
4071 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004072 int dir = compl_direction;
4073
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004074 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004075 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004076 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004077 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4078 /* if dir was BACKWARD then honor it just once */
4079 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004080 else if (did_emsg)
4081 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004082 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004083}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004084
4085/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004086 * Add completions from a dict.
4087 */
4088 static void
4089ins_compl_add_dict(dict)
4090 dict_T *dict;
4091{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004092 dictitem_T *di_refresh;
4093 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004094
4095 /* Check for optional "refresh" item. */
4096 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004097 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4098 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004099 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004100 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004101
4102 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4103 compl_opt_refresh_always = TRUE;
4104 }
4105
4106 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004107 di_words = dict_find(dict, (char_u *)"words", 5);
4108 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4109 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004110}
4111
4112/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004113 * Add a match to the list of matches from a typeval_T.
4114 * If the given string is already in the list of completions, then return
4115 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4116 * maybe because alloc() returns NULL, then FAIL is returned.
4117 */
4118 int
4119ins_compl_add_tv(tv, dir)
4120 typval_T *tv;
4121 int dir;
4122{
4123 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004124 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004125 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004126 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004127 char_u *(cptext[CPT_COUNT]);
4128
4129 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4130 {
4131 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4132 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4133 (char_u *)"abbr", FALSE);
4134 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4135 (char_u *)"menu", FALSE);
4136 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4137 (char_u *)"kind", FALSE);
4138 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4139 (char_u *)"info", FALSE);
4140 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4141 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004142 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004143 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004144 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4145 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004146 }
4147 else
4148 {
4149 word = get_tv_string_chk(tv);
4150 vim_memset(cptext, 0, sizeof(cptext));
4151 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004152 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004153 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004154 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004155}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004156#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158/*
4159 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004160 * The search starts at position "ini" in curbuf and in the direction
4161 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004162 * When "compl_started" is FALSE start at that position, otherwise continue
4163 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004164 * This may return before finding all the matches.
4165 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
4167 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004168ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 static pos_T first_match_pos;
4172 static pos_T last_match_pos;
4173 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 static int found_all = FALSE; /* Found all matches of a
4175 certain type. */
4176 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Bram Moolenaar572cb562005-08-05 21:35:02 +00004178 pos_T *pos;
4179 char_u **matches;
4180 int save_p_scs;
4181 int save_p_ws;
4182 int save_p_ic;
4183 int i;
4184 int num_matches;
4185 int len;
4186 int found_new_match;
4187 int type = ctrl_x_mode;
4188 char_u *ptr;
4189 char_u *dict = NULL;
4190 int dict_f = 0;
4191 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004192 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004194 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4197 ins_buf->b_scanned = 0;
4198 found_all = FALSE;
4199 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004201 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 last_match_pos = first_match_pos = *ini;
4203 }
4204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004206 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4208 for (;;)
4209 {
4210 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004211 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004213 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 * or if found_all says this entry is done. For ^X^L only use the
4215 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004216 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
4219 found_all = FALSE;
4220 while (*e_cpt == ',' || *e_cpt == ' ')
4221 e_cpt++;
4222 if (*e_cpt == '.' && !curbuf->b_scanned)
4223 {
4224 ins_buf = curbuf;
4225 first_match_pos = *ini;
4226 /* So that ^N can match word immediately after cursor */
4227 if (ctrl_x_mode == 0)
4228 dec(&first_match_pos);
4229 last_match_pos = first_match_pos;
4230 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004231
4232 /* Remember the first match so that the loop stops when we
4233 * wrap and come back there a second time. */
4234 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4237 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4238 {
4239 /* Scan a buffer, but not the current one. */
4240 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first_match_pos.col = last_match_pos.col = 0;
4244 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4245 last_match_pos.lnum = 0;
4246 type = 0;
4247 }
4248 else /* unloaded buffer, scan like dictionary */
4249 {
4250 found_all = TRUE;
4251 if (ins_buf->b_fname == NULL)
4252 continue;
4253 type = CTRL_X_DICTIONARY;
4254 dict = ins_buf->b_fname;
4255 dict_f = DICT_EXACT;
4256 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004257 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 ins_buf->b_fname == NULL
4259 ? buf_spname(ins_buf)
4260 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004261 ? ins_buf->b_fname
4262 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004263 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 else if (*e_cpt == NUL)
4266 break;
4267 else
4268 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004269 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 type = -1;
4271 else if (*e_cpt == 'k' || *e_cpt == 's')
4272 {
4273 if (*e_cpt == 'k')
4274 type = CTRL_X_DICTIONARY;
4275 else
4276 type = CTRL_X_THESAURUS;
4277 if (*++e_cpt != ',' && *e_cpt != NUL)
4278 {
4279 dict = e_cpt;
4280 dict_f = DICT_FIRST;
4281 }
4282 }
4283#ifdef FEAT_FIND_ID
4284 else if (*e_cpt == 'i')
4285 type = CTRL_X_PATH_PATTERNS;
4286 else if (*e_cpt == 'd')
4287 type = CTRL_X_PATH_DEFINES;
4288#endif
4289 else if (*e_cpt == ']' || *e_cpt == 't')
4290 {
4291 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004292 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004293 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 else
4296 type = -1;
4297
4298 /* in any case e_cpt is advanced to the next entry */
4299 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4300
4301 found_all = TRUE;
4302 if (type == -1)
4303 continue;
4304 }
4305 }
4306
4307 switch (type)
4308 {
4309 case -1:
4310 break;
4311#ifdef FEAT_FIND_ID
4312 case CTRL_X_PATH_PATTERNS:
4313 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004314 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4319 (linenr_T)1, (linenr_T)MAXLNUM);
4320 break;
4321#endif
4322
4323 case CTRL_X_DICTIONARY:
4324 case CTRL_X_THESAURUS:
4325 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004326 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 : (type == CTRL_X_THESAURUS
4328 ? (*curbuf->b_p_tsr == NUL
4329 ? p_tsr
4330 : curbuf->b_p_tsr)
4331 : (*curbuf->b_p_dict == NUL
4332 ? p_dict
4333 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004334 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004335 dict != NULL ? dict_f
4336 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 dict = NULL;
4338 break;
4339
4340 case CTRL_X_TAGS:
4341 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4342 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004343 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004345 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004346 * of matches is found when compl_pattern is empty */
4347 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4349 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4350 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4351 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004352 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 p_ic = save_p_ic;
4355 break;
4356
4357 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4360 {
4361
4362 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004364 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 break;
4367
4368 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 if (expand_cmdline(&compl_xp, compl_pattern,
4370 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004372 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 break;
4374
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004375#ifdef FEAT_COMPL_FUNC
4376 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004377 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004378 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004379 break;
4380#endif
4381
Bram Moolenaar488c6512005-08-11 20:09:58 +00004382 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004383#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004384 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004385 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004386 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004387 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004388#endif
4389 break;
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 default: /* normal ^P/^N and ^X^L */
4392 /*
4393 * If 'infercase' is set, don't use 'smartcase' here
4394 */
4395 save_p_scs = p_scs;
4396 if (ins_buf->b_p_inf)
4397 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398
Bram Moolenaar361aa502014-01-23 22:45:58 +01004399 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 * end but never from the middle, thus setting nowrapscan in this
4401 * buffers is a good idea, on the other hand, we always set
4402 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4403 save_p_ws = p_ws;
4404 if (ins_buf != curbuf)
4405 p_ws = FALSE;
4406 else if (*e_cpt == '.')
4407 p_ws = TRUE;
4408 for (;;)
4409 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004410 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004412 ++msg_silent; /* Don't want messages for wrapscan. */
4413
Bram Moolenaare4214502015-03-05 18:08:43 +01004414 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4415 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004416 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004417 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004420 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004422 found_new_match = searchit(NULL, ins_buf, pos,
4423 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004425 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004426 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004427 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004429 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 first_match_pos = *pos;
4432 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004433 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004436 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 found_new_match = FAIL;
4438 if (found_new_match == FAIL)
4439 {
4440 if (ins_buf == curbuf)
4441 found_all = TRUE;
4442 break;
4443 }
4444
4445 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 && ini->lnum == pos->lnum
4448 && ini->col == pos->col)
4449 continue;
4450 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004451 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4456 continue;
4457 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4458 if (!p_paste)
4459 ptr = skipwhite(ptr);
4460 }
4461 len = (int)STRLEN(ptr);
4462 }
4463 else
4464 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465 char_u *tmp_ptr = ptr;
4466
4467 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 /* Skip if already inside a word. */
4471 if (vim_iswordp(tmp_ptr))
4472 continue;
4473 /* Find start of next word. */
4474 tmp_ptr = find_word_start(tmp_ptr);
4475 }
4476 /* Find end of this word. */
4477 tmp_ptr = find_word_end(tmp_ptr);
4478 len = (int)(tmp_ptr - ptr);
4479
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 if ((compl_cont_status & CONT_ADDING)
4481 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4484 {
4485 /* Try next line, if any. the new word will be
4486 * "join" as if the normal command "J" was used.
4487 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004488 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 * works -- Acevedo */
4490 STRNCPY(IObuff, ptr, len);
4491 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4492 tmp_ptr = ptr = skipwhite(ptr);
4493 /* Find start of next word. */
4494 tmp_ptr = find_word_start(tmp_ptr);
4495 /* Find end of next word. */
4496 tmp_ptr = find_word_end(tmp_ptr);
4497 if (tmp_ptr > ptr)
4498 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004499 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004501 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 IObuff[len++] = ' ';
4503 /* IObuf =~ "\k.* ", thus len >= 2 */
4504 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004505 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 || (vim_strchr(p_cpo, CPO_JOINSP)
4507 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004508 && (IObuff[len - 2] == '?'
4509 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 IObuff[len++] = ' ';
4511 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004512 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (tmp_ptr - ptr >= IOSIZE - len)
4514 tmp_ptr = ptr + IOSIZE - len - 1;
4515 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4516 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004517 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519 IObuff[len] = NUL;
4520 ptr = IObuff;
4521 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 continue;
4524 }
4525 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004526 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004527 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004528 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 found_new_match = OK;
4531 break;
4532 }
4533 }
4534 p_scs = save_p_scs;
4535 p_ws = save_p_ws;
4536 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004537
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004539 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004540 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 found_new_match = OK;
4542
4543 /* break the loop for specialized modes (use 'complete' just for the
4544 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004545 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004547 {
4548 if (got_int)
4549 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004550 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004551 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004552 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004553
Bram Moolenaare4214502015-03-05 18:08:43 +01004554 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004555 || compl_interrupted)
4556 break;
4557 compl_started = TRUE;
4558 }
4559 else
4560 {
4561 /* Mark a buffer scanned when it has been scanned completely */
4562 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4563 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004565 compl_started = FALSE;
4566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004568 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
Bram Moolenaare4214502015-03-05 18:08:43 +01004570 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 && *e_cpt == NUL) /* Got to end of 'complete' */
4572 found_new_match = FAIL;
4573
4574 i = -1; /* total of matches, unknown */
4575 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004576 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 i = ins_compl_make_cyclic();
4578
4579 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 * just been made cyclic then we have to move compl_curr_match to the next
4581 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004582 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4583 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004584 if (compl_curr_match == NULL)
4585 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 return i;
4587}
4588
4589/* Delete the old text being completed. */
4590 static void
4591ins_compl_delete()
4592{
4593 int i;
4594
4595 /*
4596 * In insert mode: Delete the typed part.
4597 * In replace mode: Put the old characters back, if any.
4598 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004599 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004601
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004602 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4603 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 changed_cline_bef_curs();
4605}
4606
4607/* Insert the new text being completed. */
4608 static void
4609ins_compl_insert()
4610{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004611 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004612 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4613 compl_used_match = FALSE;
4614 else
4615 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616}
4617
4618/*
4619 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004620 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4621 * get more completions. If it is FALSE, then we just do nothing when there
4622 * are no more completions in a given direction. The latter case is used when
4623 * we are still in the middle of finding completions, to allow browsing
4624 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 * Return the total number of matches, or -1 if still unknown -- webb.
4626 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4628 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 *
4630 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004631 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4632 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 */
4634 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004635ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004637 int count; /* repeat completion this many times; should
4638 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004639 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 int num_matches = -1;
4642 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004643 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004644 compl_T *found_compl = NULL;
4645 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004646 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004648 /* When user complete function return -1 for findstart which is next
4649 * time of 'always', compl_shown_match become NULL. */
4650 if (compl_shown_match == NULL)
4651 return -1;
4652
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004653 if (compl_leader != NULL
4654 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004656 /* Set "compl_shown_match" to the actually shown match, it may differ
4657 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004658 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004659 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004660 && compl_shown_match->cp_next != NULL
4661 && compl_shown_match->cp_next != compl_first_match)
4662 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004663
4664 /* If we didn't find it searching forward, and compl_shows_dir is
4665 * backward, find the last match. */
4666 if (compl_shows_dir == BACKWARD
4667 && !ins_compl_equal(compl_shown_match,
4668 compl_leader, (int)STRLEN(compl_leader))
4669 && (compl_shown_match->cp_next == NULL
4670 || compl_shown_match->cp_next == compl_first_match))
4671 {
4672 while (!ins_compl_equal(compl_shown_match,
4673 compl_leader, (int)STRLEN(compl_leader))
4674 && compl_shown_match->cp_prev != NULL
4675 && compl_shown_match->cp_prev != compl_first_match)
4676 compl_shown_match = compl_shown_match->cp_prev;
4677 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004678 }
4679
4680 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004681 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 /* Delete old text to be replaced */
4683 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004684
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004685 /* When finding the longest common text we stick at the original text,
4686 * don't let CTRL-N or CTRL-P move to the first match. */
4687 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4688
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004689 /* When restarting the search don't insert the first match either. */
4690 if (compl_restarting)
4691 {
4692 advance = FALSE;
4693 compl_restarting = FALSE;
4694 }
4695
Bram Moolenaare3226be2005-12-18 22:10:00 +00004696 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4697 * around. */
4698 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004700 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004702 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004703 found_end = (compl_first_match != NULL
4704 && (compl_shown_match->cp_next == compl_first_match
4705 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004706 }
4707 else if (compl_shows_dir == BACKWARD
4708 && compl_shown_match->cp_prev != NULL)
4709 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004710 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004711 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004712 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004715 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004716 if (!allow_get_expansion)
4717 {
4718 if (advance)
4719 {
4720 if (compl_shows_dir == BACKWARD)
4721 compl_pending -= todo + 1;
4722 else
4723 compl_pending += todo + 1;
4724 }
4725 return -1;
4726 }
4727
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004728 if (advance)
4729 {
4730 if (compl_shows_dir == BACKWARD)
4731 --compl_pending;
4732 else
4733 ++compl_pending;
4734 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004735
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004736 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004737 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004738
4739 /* handle any pending completions */
4740 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004741 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004742 {
4743 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4744 {
4745 compl_shown_match = compl_shown_match->cp_next;
4746 --compl_pending;
4747 }
4748 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4749 {
4750 compl_shown_match = compl_shown_match->cp_prev;
4751 ++compl_pending;
4752 }
4753 else
4754 break;
4755 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004756 found_end = FALSE;
4757 }
4758 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4759 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004760 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004761 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004762 ++todo;
4763 else
4764 /* Remember a matching item. */
4765 found_compl = compl_shown_match;
4766
4767 /* Stop at the end of the list when we found a usable match. */
4768 if (found_end)
4769 {
4770 if (found_compl != NULL)
4771 {
4772 compl_shown_match = found_compl;
4773 break;
4774 }
4775 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004779 /* Insert the text of the new completion, or the compl_leader. */
4780 if (insert_match)
4781 {
4782 if (!compl_get_longest || compl_used_match)
4783 ins_compl_insert();
4784 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004785 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004786 }
4787 else
4788 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790 if (!allow_get_expansion)
4791 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004792 /* may undisplay the popup menu first */
4793 ins_compl_upd_pum();
4794
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004795 /* redraw to show the user what was inserted */
4796 update_screen(0);
4797
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004798 /* display the updated popup menu */
4799 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004800#ifdef FEAT_GUI
4801 if (gui.in_use)
4802 {
4803 /* Show the cursor after the match, not after the redrawn text. */
4804 setcursor();
4805 out_flush();
4806 gui_update_cursor(FALSE, FALSE);
4807 }
4808#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 /* Delete old text to be replaced, since we're still searching and
4811 * don't want to match ourselves! */
4812 ins_compl_delete();
4813 }
4814
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004815 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004816 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004817 compl_enter_selects = !insert_match && compl_match_array != NULL;
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 /*
4820 * Show the file name for the match (if any)
4821 * Truncate the file name to avoid a wait for return.
4822 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004823 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
4825 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004826 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (i <= 0)
4828 i = 0;
4829 else
4830 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004831 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 msg(IObuff);
4833 redraw_cmdline = FALSE; /* don't overwrite! */
4834 }
4835
4836 return num_matches;
4837}
4838
4839/*
4840 * Call this while finding completions, to check whether the user has hit a key
4841 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004842 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004844 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 */
4846 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004847ins_compl_check_keys(frequency)
4848 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 static int count = 0;
4851
4852 int c;
4853
4854 /* Don't check when reading keys from a script. That would break the test
4855 * scripts */
4856 if (using_script())
4857 return;
4858
4859 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004860 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 return;
4862 count = 0;
4863
Bram Moolenaara260a972006-06-23 19:36:29 +00004864 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4865 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 if (c != NUL)
4868 {
4869 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4870 {
4871 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004872 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004873 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4874 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004876 else
4877 {
4878 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004879 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004880 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004881 if (c != K_IGNORE)
4882 {
4883 /* Don't interrupt completion when the character wasn't typed,
4884 * e.g., when doing @q to replay keys. */
4885 if (c != Ctrl_R && KeyTyped)
4886 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004887
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004888 vungetc(c);
4889 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004892 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004893 {
4894 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4895
4896 compl_pending = 0;
4897 (void)ins_compl_next(FALSE, todo, TRUE);
4898 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004899}
4900
4901/*
4902 * Decide the direction of Insert mode complete from the key typed.
4903 * Returns BACKWARD or FORWARD.
4904 */
4905 static int
4906ins_compl_key2dir(c)
4907 int c;
4908{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004909 if (c == Ctrl_P || c == Ctrl_L
4910 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4911 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004912 return BACKWARD;
4913 return FORWARD;
4914}
4915
4916/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004917 * Return TRUE for keys that are used for completion only when the popup menu
4918 * is visible.
4919 */
4920 static int
4921ins_compl_pum_key(c)
4922 int c;
4923{
4924 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004925 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4926 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004927}
4928
4929/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004930 * Decide the number of completions to move forward.
4931 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4932 */
4933 static int
4934ins_compl_key2count(c)
4935 int c;
4936{
4937 int h;
4938
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004939 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004940 {
4941 h = pum_get_height();
4942 if (h > 3)
4943 h -= 2; /* keep some context */
4944 return h;
4945 }
4946 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947}
4948
4949/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004950 * Return TRUE if completion with "c" should insert the match, FALSE if only
4951 * to change the currently selected completion.
4952 */
4953 static int
4954ins_compl_use_match(c)
4955 int c;
4956{
4957 switch (c)
4958 {
4959 case K_UP:
4960 case K_DOWN:
4961 case K_PAGEDOWN:
4962 case K_KPAGEDOWN:
4963 case K_S_DOWN:
4964 case K_PAGEUP:
4965 case K_KPAGEUP:
4966 case K_S_UP:
4967 return FALSE;
4968 }
4969 return TRUE;
4970}
4971
4972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 * Do Insert mode completion.
4974 * Called when character "c" was typed, which has a meaning for completion.
4975 * Returns OK if completion was done, FAIL if something failed (out of mem).
4976 */
4977 static int
4978ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004981 char_u *line;
4982 int startcol = 0; /* column where searched text starts */
4983 colnr_T curs_col; /* cursor column */
4984 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004985 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
Bram Moolenaare3226be2005-12-18 22:10:00 +00004987 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004988 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 /* First time we hit ^N or ^P (in a row, I mean) */
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 did_ai = FALSE;
4993#ifdef FEAT_SMARTINDENT
4994 did_si = FALSE;
4995 can_si = FALSE;
4996 can_si_back = FALSE;
4997#endif
4998 if (stop_arrow() == FAIL)
4999 return FAIL;
5000
5001 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005002 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005003 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005005 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005006 * "compl_startpos" to the cursor as a pattern to add a new word
5007 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005008 * "compl_startpos" is not in the same line as the cursor then fix it
5009 * (the line has been split because it was longer than 'tw'). if SOL
5010 * is set then skip the previous pattern, a word at the beginning of
5011 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005012 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5013 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
5015 /*
5016 * it is a continued search
5017 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5020 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5021 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005022 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005024 /* line (probably) wrapped, set compl_startpos to the
5025 * first non_blank in the line, if it is not a wordchar
5026 * include it to get a better pattern, but then we don't
5027 * want the "\\<" prefix, check it bellow */
5028 compl_col = (colnr_T)(skipwhite(line) - line);
5029 compl_startpos.col = compl_col;
5030 compl_startpos.lnum = curwin->w_cursor.lnum;
5031 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
5034 {
5035 /* S_IPOS was set when we inserted a word that was at the
5036 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005037 * mode but first we need to redefine compl_startpos */
5038 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005040 compl_cont_status |= CONT_SOL;
5041 compl_startpos.col = (colnr_T)(skipwhite(
5042 line + compl_length
5043 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005048 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005049 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 compl_cont_status &= ~CONT_SOL;
5054 compl_length = (IOSIZE - MIN_SPACE);
5055 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5058 if (compl_length < 1)
5059 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005061 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005069 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005071 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 compl_cont_status = 0;
5074 compl_cont_status |= CONT_N_ADDS;
5075 compl_startpos = curwin->w_cursor;
5076 startcol = (int)curs_col;
5077 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079
5080 /* Work out completion pattern and original text -- webb */
5081 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5082 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5085 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 compl_col += ++startcol;
5091 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 compl_pattern = str_foldcase(line + compl_col,
5095 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005097 compl_pattern = vim_strnsave(line + compl_col,
5098 compl_length);
5099 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 return FAIL;
5101 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 char_u *prefix = (char_u *)"\\<";
5105
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005106 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005108 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 if (!vim_iswordp(line + compl_col)
5112 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 && (
5114#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#endif
5119 )))
5120 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 STRCPY((char *)compl_pattern, prefix);
5122 (void)quote_meta(compl_pattern + STRLEN(prefix),
5123 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130#endif
5131 )
5132 {
5133 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5135 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 compl_col += curs_col;
5138 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 else
5141 {
5142#ifdef FEAT_MBYTE
5143 /* Search the point of change class of multibyte character
5144 * or not a word single byte character backward. */
5145 if (has_mbyte)
5146 {
5147 int base_class;
5148 int head_off;
5149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 startcol -= (*mb_head_off)(line, line + startcol);
5151 base_class = mb_get_class(line + startcol);
5152 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 head_off = (*mb_head_off)(line, line + startcol);
5155 if (base_class != mb_get_class(line + startcol
5156 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 }
5161 else
5162#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005165 compl_col += ++startcol;
5166 compl_length = (int)curs_col - startcol;
5167 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 /* Only match word with at least two chars -- webb
5170 * there's no need to call quote_meta,
5171 * alloc(7) is enough -- Acevedo
5172 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 compl_pattern = alloc(7);
5174 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 STRCPY((char *)compl_pattern, "\\<");
5177 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5178 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 else
5181 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005183 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 STRCPY((char *)compl_pattern, "\\<");
5187 (void)quote_meta(compl_pattern + 2, line + compl_col,
5188 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190 }
5191 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005192 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005194 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 compl_length = (int)curs_col - (int)compl_col;
5196 if (compl_length < 0) /* cursor in indent: empty pattern */
5197 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 compl_pattern = str_foldcase(line + compl_col, compl_length,
5200 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5203 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 return FAIL;
5205 }
5206 else if (ctrl_x_mode == CTRL_X_FILES)
5207 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005208 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005209 if (startcol > 0)
5210 {
5211 char_u *p = line + startcol;
5212
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005213 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005214 while (p > line && vim_isfilec(PTR2CHAR(p)))
5215 mb_ptr_back(line, p);
5216 if (p == line && vim_isfilec(PTR2CHAR(p)))
5217 startcol = 0;
5218 else
5219 startcol = (int)(p - line) + 1;
5220 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005221
Bram Moolenaar0300e462013-09-08 16:03:45 +02005222 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_length = (int)curs_col - startcol;
5224 compl_pattern = addstar(line + compl_col, compl_length,
5225 EXPAND_FILES);
5226 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return FAIL;
5228 }
5229 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5230 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 compl_pattern = vim_strnsave(line, curs_col);
5232 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 set_cmd_context(&compl_xp, compl_pattern,
5235 (int)STRLEN(compl_pattern), curs_col);
5236 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5237 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005238 /* No completion possible, use an empty pattern to get a
5239 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005240 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005241 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005242 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5243 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005245 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005246 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005247#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005248 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005249 * Call user defined function 'completefunc' with "a:findstart"
5250 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005251 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005252 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005253 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005254 char_u *funcname;
5255 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005256 win_T *curwin_save;
5257 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005258
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005259 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005260 * string */
5261 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5262 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5263 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005264 {
5265 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5266 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005267 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005268 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005269
5270 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005271 args[1] = NULL;
5272 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005273 curwin_save = curwin;
5274 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005275 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005276 if (curwin_save != curwin || curbuf_save != curbuf)
5277 {
5278 EMSG(_(e_complwin));
5279 return FAIL;
5280 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005281 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005282 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005283 if (!equalpos(curwin->w_cursor, pos))
5284 {
5285 EMSG(_(e_compldel));
5286 return FAIL;
5287 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005288
Bram Moolenaar6110a002012-01-26 18:58:38 +01005289 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005290 * cancel the complete without an error.
5291 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005292 if (col == -2)
5293 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005294 if (col == -3)
5295 {
5296 ctrl_x_mode = 0;
5297 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005298 if (!shortmess(SHM_COMPLETIONMENU))
5299 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005300 return FAIL;
5301 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005302
Bram Moolenaar82139082011-09-14 16:52:09 +02005303 /*
5304 * Reset extended parameters of completion, when start new
5305 * completion.
5306 */
5307 compl_opt_refresh_always = FALSE;
5308
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005309 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005310 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005311 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005312 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005313 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 /* Setup variables for completion. Need to obtain "line" again,
5316 * it may have become invalid. */
5317 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005318 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5320 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005321#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 return FAIL;
5323 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005324 else if (ctrl_x_mode == CTRL_X_SPELL)
5325 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005326#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005327 if (spell_bad_len > 0)
5328 compl_col = curs_col - spell_bad_len;
5329 else
5330 compl_col = spell_word_start(startcol);
5331 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005332 {
5333 compl_length = 0;
5334 compl_col = curs_col;
5335 }
5336 else
5337 {
5338 spell_expand_check_cap(compl_col);
5339 compl_length = (int)curs_col - compl_col;
5340 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005341 /* Need to obtain "line" again, it may have become invalid. */
5342 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005343 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5344 if (compl_pattern == NULL)
5345#endif
5346 return FAIL;
5347 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 else
5349 {
5350 EMSG2(_(e_intern2), "ins_complete()");
5351 return FAIL;
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005357 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
5359 /* Insert a new line, keep indentation but ignore 'comments' */
5360#ifdef FEAT_COMMENTS
5361 char_u *old = curbuf->b_p_com;
5362
5363 curbuf->b_p_com = (char_u *)"";
5364#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 compl_startpos.lnum = curwin->w_cursor.lnum;
5366 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ins_eol('\r');
5368#ifdef FEAT_COMMENTS
5369 curbuf->b_p_com = old;
5370#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 compl_length = 0;
5372 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 }
5375 else
5376 {
5377 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
5380
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005381 if (compl_cont_status & CONT_LOCAL)
5382 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else
5384 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5385
Bram Moolenaar62951b12011-09-21 18:23:05 +02005386 /* If any of the original typed text has been changed we need to fix
5387 * the redo buffer. */
5388 ins_compl_fixRedoBufForLeader(NULL);
5389
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005390 /* Always add completion for the original text. */
5391 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5393 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005394 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 vim_free(compl_pattern);
5397 compl_pattern = NULL;
5398 vim_free(compl_orig_text);
5399 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 return FAIL;
5401 }
5402
5403 /* showmode might reset the internal line pointers, so it must
5404 * be called before line = ml_get(), or when this address is no
5405 * longer needed. -- Acevedo.
5406 */
5407 edit_submode_extra = (char_u *)_("-- Searching...");
5408 edit_submode_highl = HLF_COUNT;
5409 showmode();
5410 edit_submode_extra = NULL;
5411 out_flush();
5412 }
5413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 compl_shown_match = compl_curr_match;
5415 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005418 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005420 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005421 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005423 /* may undisplay the popup menu */
5424 ins_compl_upd_pum();
5425
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 if (n > 1) /* all matches have been found */
5427 compl_matches = n;
5428 compl_curr_match = compl_shown_match;
5429 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaard68071d2006-05-02 22:08:30 +00005431 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5432 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (got_int && !global_busy)
5434 {
5435 (void)vgetc();
5436 got_int = FALSE;
5437 }
5438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005440 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5443 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5445 edit_submode_highl = HLF_E;
5446 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5447 * because we couldn't expand anything at first place, but if we used
5448 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5449 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 if ( compl_length > 1
5451 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 || (ctrl_x_mode != 0
5453 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5454 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457
Bram Moolenaar572cb562005-08-05 21:35:02 +00005458 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005459 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if (edit_submode_extra == NULL)
5464 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005465 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 edit_submode_extra = (char_u *)_("Back at original");
5468 edit_submode_highl = HLF_W;
5469 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
5472 edit_submode_extra = (char_u *)_("Word from other line");
5473 edit_submode_highl = HLF_COUNT;
5474 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005475 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 edit_submode_extra = (char_u *)_("The only match");
5478 edit_submode_highl = HLF_COUNT;
5479 }
5480 else
5481 {
5482 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005483 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005485 int number = 0;
5486 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
5490 /* search backwards for the first valid (!= -1) number.
5491 * This should normally succeed already at the first loop
5492 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005493 for (match = compl_curr_match->cp_prev; match != NULL
5494 && match != compl_first_match;
5495 match = match->cp_prev)
5496 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005498 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 break;
5500 }
5501 if (match != NULL)
5502 /* go up and assign all numbers which are not assigned
5503 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005504 for (match = match->cp_next;
5505 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005506 match = match->cp_next)
5507 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 else /* BACKWARD */
5510 {
5511 /* search forwards (upwards) for the first valid (!= -1)
5512 * number. This should normally succeed already at the
5513 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005514 for (match = compl_curr_match->cp_next; match != NULL
5515 && match != compl_first_match;
5516 match = match->cp_next)
5517 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005519 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 break;
5521 }
5522 if (match != NULL)
5523 /* go down and assign all numbers which are not
5524 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005525 for (match = match->cp_prev; match
5526 && match->cp_number == -1;
5527 match = match->cp_prev)
5528 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530 }
5531
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005532 /* The match should always have a sequence number now, this is
5533 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005534 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005536 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5537 * Translations may need more than twice that. */
5538 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005541 vim_snprintf((char *)match_ref, sizeof(match_ref),
5542 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005543 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005545 vim_snprintf((char *)match_ref, sizeof(match_ref),
5546 _("match %d"),
5547 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 edit_submode_extra = match_ref;
5549 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005550 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 curs_columns(FALSE);
5552 }
5553 }
5554 }
5555
5556 /* Show a message about what (completion) mode we're in. */
5557 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005558 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005560 if (edit_submode_extra != NULL)
5561 {
5562 if (!p_smd)
5563 msg_attr(edit_submode_extra,
5564 edit_submode_highl < HLF_COUNT
5565 ? hl_attr(edit_submode_highl) : 0);
5566 }
5567 else
5568 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
Bram Moolenaard68071d2006-05-02 22:08:30 +00005571 /* Show the popup menu, unless we got interrupted. */
5572 if (!compl_interrupted)
5573 {
5574 /* RedrawingDisabled may be set when invoked through complete(). */
5575 n = RedrawingDisabled;
5576 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005577
5578 /* If the cursor moved we need to remove the pum first. */
5579 setcursor();
5580 if (save_w_wrow != curwin->w_wrow)
5581 ins_compl_del_pum();
5582
Bram Moolenaard68071d2006-05-02 22:08:30 +00005583 ins_compl_show_pum();
5584 setcursor();
5585 RedrawingDisabled = n;
5586 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005587 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005588 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005589
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 return OK;
5591}
5592
5593/*
5594 * Looks in the first "len" chars. of "src" for search-metachars.
5595 * If dest is not NULL the chars. are copied there quoting (with
5596 * a backslash) the metachars, and dest would be NUL terminated.
5597 * Returns the length (needed) of dest
5598 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005599 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600quote_meta(dest, src, len)
5601 char_u *dest;
5602 char_u *src;
5603 int len;
5604{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005605 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005607 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 switch (*src)
5610 {
5611 case '.':
5612 case '*':
5613 case '[':
5614 if (ctrl_x_mode == CTRL_X_DICTIONARY
5615 || ctrl_x_mode == CTRL_X_THESAURUS)
5616 break;
5617 case '~':
5618 if (!p_magic) /* quote these only if magic is set */
5619 break;
5620 case '\\':
5621 if (ctrl_x_mode == CTRL_X_DICTIONARY
5622 || ctrl_x_mode == CTRL_X_THESAURUS)
5623 break;
5624 case '^': /* currently it's not needed. */
5625 case '$':
5626 m++;
5627 if (dest != NULL)
5628 *dest++ = '\\';
5629 break;
5630 }
5631 if (dest != NULL)
5632 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005633# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 /* Copy remaining bytes of a multibyte character. */
5635 if (has_mbyte)
5636 {
5637 int i, mb_len;
5638
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005639 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (mb_len > 0 && len >= mb_len)
5641 for (i = 0; i < mb_len; ++i)
5642 {
5643 --len;
5644 ++src;
5645 if (dest != NULL)
5646 *dest++ = *src;
5647 }
5648 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 if (dest != NULL)
5652 *dest = NUL;
5653
5654 return m;
5655}
5656#endif /* FEAT_INS_EXPAND */
5657
5658/*
5659 * Next character is interpreted literally.
5660 * A one, two or three digit decimal number is interpreted as its byte value.
5661 * If one or two digits are entered, the next character is given to vungetc().
5662 * For Unicode a character > 255 may be returned.
5663 */
5664 int
5665get_literal()
5666{
5667 int cc;
5668 int nc;
5669 int i;
5670 int hex = FALSE;
5671 int octal = FALSE;
5672#ifdef FEAT_MBYTE
5673 int unicode = 0;
5674#endif
5675
5676 if (got_int)
5677 return Ctrl_C;
5678
5679#ifdef FEAT_GUI
5680 /*
5681 * In GUI there is no point inserting the internal code for a special key.
5682 * It is more useful to insert the string "<KEY>" instead. This would
5683 * probably be useful in a text window too, but it would not be
5684 * vi-compatible (maybe there should be an option for it?) -- webb
5685 */
5686 if (gui.in_use)
5687 ++allow_keys;
5688#endif
5689#ifdef USE_ON_FLY_SCROLL
5690 dont_scroll = TRUE; /* disallow scrolling here */
5691#endif
5692 ++no_mapping; /* don't map the next key hits */
5693 cc = 0;
5694 i = 0;
5695 for (;;)
5696 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005697 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698#ifdef FEAT_CMDL_INFO
5699 if (!(State & CMDLINE)
5700# ifdef FEAT_MBYTE
5701 && MB_BYTE2LEN_CHECK(nc) == 1
5702# endif
5703 )
5704 add_to_showcmd(nc);
5705#endif
5706 if (nc == 'x' || nc == 'X')
5707 hex = TRUE;
5708 else if (nc == 'o' || nc == 'O')
5709 octal = TRUE;
5710#ifdef FEAT_MBYTE
5711 else if (nc == 'u' || nc == 'U')
5712 unicode = nc;
5713#endif
5714 else
5715 {
5716 if (hex
5717#ifdef FEAT_MBYTE
5718 || unicode != 0
5719#endif
5720 )
5721 {
5722 if (!vim_isxdigit(nc))
5723 break;
5724 cc = cc * 16 + hex2nr(nc);
5725 }
5726 else if (octal)
5727 {
5728 if (nc < '0' || nc > '7')
5729 break;
5730 cc = cc * 8 + nc - '0';
5731 }
5732 else
5733 {
5734 if (!VIM_ISDIGIT(nc))
5735 break;
5736 cc = cc * 10 + nc - '0';
5737 }
5738
5739 ++i;
5740 }
5741
5742 if (cc > 255
5743#ifdef FEAT_MBYTE
5744 && unicode == 0
5745#endif
5746 )
5747 cc = 255; /* limit range to 0-255 */
5748 nc = 0;
5749
5750 if (hex) /* hex: up to two chars */
5751 {
5752 if (i >= 2)
5753 break;
5754 }
5755#ifdef FEAT_MBYTE
5756 else if (unicode) /* Unicode: up to four or eight chars */
5757 {
5758 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5759 break;
5760 }
5761#endif
5762 else if (i >= 3) /* decimal or octal: up to three chars */
5763 break;
5764 }
5765 if (i == 0) /* no number entered */
5766 {
5767 if (nc == K_ZERO) /* NUL is stored as NL */
5768 {
5769 cc = '\n';
5770 nc = 0;
5771 }
5772 else
5773 {
5774 cc = nc;
5775 nc = 0;
5776 }
5777 }
5778
5779 if (cc == 0) /* NUL is stored as NL */
5780 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005781#ifdef FEAT_MBYTE
5782 if (enc_dbcs && (cc & 0xff) == 0)
5783 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5784 second byte will cause trouble! */
5785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 --no_mapping;
5788#ifdef FEAT_GUI
5789 if (gui.in_use)
5790 --allow_keys;
5791#endif
5792 if (nc)
5793 vungetc(nc);
5794 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5795 return cc;
5796}
5797
5798/*
5799 * Insert character, taking care of special keys and mod_mask
5800 */
5801 static void
5802insert_special(c, allow_modmask, ctrlv)
5803 int c;
5804 int allow_modmask;
5805 int ctrlv; /* c was typed after CTRL-V */
5806{
5807 char_u *p;
5808 int len;
5809
5810 /*
5811 * Special function key, translate into "<Key>". Up to the last '>' is
5812 * inserted with ins_str(), so as not to replace characters in replace
5813 * mode.
5814 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5815 * unless 'allow_modmask' is TRUE.
5816 */
5817#ifdef MACOS
5818 /* Command-key never produces a normal key */
5819 if (mod_mask & MOD_MASK_CMD)
5820 allow_modmask = TRUE;
5821#endif
5822 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5823 {
5824 p = get_special_key_name(c, mod_mask);
5825 len = (int)STRLEN(p);
5826 c = p[len - 1];
5827 if (len > 2)
5828 {
5829 if (stop_arrow() == FAIL)
5830 return;
5831 p[len - 1] = NUL;
5832 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005833 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 ctrlv = FALSE;
5835 }
5836 }
5837 if (stop_arrow() == OK)
5838 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5839}
5840
5841/*
5842 * Special characters in this context are those that need processing other
5843 * than the simple insertion that can be performed here. This includes ESC
5844 * which terminates the insert, and CR/NL which need special processing to
5845 * open up a new line. This routine tries to optimize insertions performed by
5846 * the "redo", "undo" or "put" commands, so it needs to know when it should
5847 * stop and defer processing to the "normal" mechanism.
5848 * '0' and '^' are special, because they can be followed by CTRL-D.
5849 */
5850#ifdef EBCDIC
5851# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5852#else
5853# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5854#endif
5855
5856#ifdef FEAT_MBYTE
5857# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5858#else
5859# define WHITECHAR(cc) vim_iswhite(cc)
5860#endif
5861
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005862/*
5863 * "flags": INSCHAR_FORMAT - force formatting
5864 * INSCHAR_CTRLV - char typed just after CTRL-V
5865 * INSCHAR_NO_FEX - don't use 'formatexpr'
5866 *
5867 * NOTE: passes the flags value straight through to internal_format() which,
5868 * beside INSCHAR_FORMAT (above), is also looking for these:
5869 * INSCHAR_DO_COM - format comments
5870 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 void
5873insertchar(c, flags, second_indent)
5874 int c; /* character to insert or NUL */
5875 int flags; /* INSCHAR_FORMAT, etc. */
5876 int second_indent; /* indent for second line if >= 0 */
5877{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 int textwidth;
5879#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005883 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005885 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
5888 /*
5889 * Try to break the line in two or more pieces when:
5890 * - Always do this if we have been called to do formatting only.
5891 * - Always do this when 'formatoptions' has the 'a' flag and the line
5892 * ends in white space.
5893 * - Otherwise:
5894 * - Don't do this if inserting a blank
5895 * - Don't do this if an existing character is being replaced, unless
5896 * we're in VREPLACE mode.
5897 * - Do this if the cursor is not on the line where insert started
5898 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5899 * before the insert.
5900 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5901 * before 'textwidth'
5902 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005903 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005904 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 || (!vim_iswhite(c)
5906 && !((State & REPLACE_FLAG)
5907#ifdef FEAT_VREPLACE
5908 && !(State & VREPLACE_FLAG)
5909#endif
5910 && *ml_get_cursor() != NUL)
5911 && (curwin->w_cursor.lnum != Insstart.lnum
5912 || ((!has_format_option(FO_INS_LONG)
5913 || Insstart_textlen <= (colnr_T)textwidth)
5914 && (!fo_ins_blank
5915 || Insstart_blank_vcol <= (colnr_T)textwidth
5916 ))))))
5917 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005918 /* Format with 'formatexpr' when it's set. Use internal formatting
5919 * when 'formatexpr' isn't set or it returns non-zero. */
5920#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005921 int do_internal = TRUE;
5922 colnr_T virtcol = get_nolist_virtcol()
5923 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005924
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005925 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5926 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005927 {
5928 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5929 /* It may be required to save for undo again, e.g. when setline()
5930 * was called. */
5931 ins_need_undo = TRUE;
5932 }
5933 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005935 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (c == NUL) /* only formatting was wanted */
5939 return;
5940
5941#ifdef FEAT_COMMENTS
5942 /* Check whether this character should end a comment. */
5943 if (did_ai && (int)c == end_comment_pending)
5944 {
5945 char_u *line;
5946 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5947 int middle_len, end_len;
5948 int i;
5949
5950 /*
5951 * Need to remove existing (middle) comment leader and insert end
5952 * comment leader. First, check what comment leader we can find.
5953 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005954 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5956 {
5957 /* Skip middle-comment string */
5958 while (*p && p[-1] != ':') /* find end of middle flags */
5959 ++p;
5960 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5961 /* Don't count trailing white space for middle_len */
5962 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5963 --middle_len;
5964
5965 /* Find the end-comment string */
5966 while (*p && p[-1] != ':') /* find end of end flags */
5967 ++p;
5968 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5969
5970 /* Skip white space before the cursor */
5971 i = curwin->w_cursor.col;
5972 while (--i >= 0 && vim_iswhite(line[i]))
5973 ;
5974 i++;
5975
5976 /* Skip to before the middle leader */
5977 i -= middle_len;
5978
5979 /* Check some expected things before we go on */
5980 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5981 {
5982 /* Backspace over all the stuff we want to replace */
5983 backspace_until_column(i);
5984
5985 /*
5986 * Insert the end-comment string, except for the last
5987 * character, which will get inserted as normal later.
5988 */
5989 ins_bytes_len(lead_end, end_len - 1);
5990 }
5991 }
5992 }
5993 end_comment_pending = NUL;
5994#endif
5995
5996 did_ai = FALSE;
5997#ifdef FEAT_SMARTINDENT
5998 did_si = FALSE;
5999 can_si = FALSE;
6000 can_si_back = FALSE;
6001#endif
6002
6003 /*
6004 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6005 * This speeds up normal text input considerably.
6006 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6007 * need to re-indent at a ':', or any other character (but not what
6008 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006009 * Don't do this when there an InsertCharPre autocommand is defined,
6010 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
6012#ifdef USE_ON_FLY_SCROLL
6013 dont_scroll = FALSE; /* allow scrolling here */
6014#endif
6015
6016 if ( !ISSPECIAL(c)
6017#ifdef FEAT_MBYTE
6018 && (!has_mbyte || (*mb_char2len)(c) == 1)
6019#endif
6020 && vpeekc() != NUL
6021 && !(State & REPLACE_FLAG)
6022#ifdef FEAT_CINDENT
6023 && !cindent_on()
6024#endif
6025#ifdef FEAT_RIGHTLEFT
6026 && !p_ri
6027#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006028#ifdef FEAT_AUTOCMD
6029 && !has_insertcharpre()
6030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 )
6032 {
6033#define INPUT_BUFLEN 100
6034 char_u buf[INPUT_BUFLEN + 1];
6035 int i;
6036 colnr_T virtcol = 0;
6037
6038 buf[0] = c;
6039 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 virtcol = get_nolist_virtcol();
6042 /*
6043 * Stop the string when:
6044 * - no more chars available
6045 * - finding a special character (command key)
6046 * - buffer is full
6047 * - running into the 'textwidth' boundary
6048 * - need to check for abbreviation: A non-word char after a word-char
6049 */
6050 while ( (c = vpeekc()) != NUL
6051 && !ISSPECIAL(c)
6052#ifdef FEAT_MBYTE
6053 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6054#endif
6055 && i < INPUT_BUFLEN
6056 && (textwidth == 0
6057 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6058 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6059 {
6060#ifdef FEAT_RIGHTLEFT
6061 c = vgetc();
6062 if (p_hkmap && KeyTyped)
6063 c = hkmap(c); /* Hebrew mode mapping */
6064# ifdef FEAT_FKMAP
6065 if (p_fkmap && KeyTyped)
6066 c = fkmap(c); /* Farsi mode mapping */
6067# endif
6068 buf[i++] = c;
6069#else
6070 buf[i++] = vgetc();
6071#endif
6072 }
6073
6074#ifdef FEAT_DIGRAPHS
6075 do_digraph(-1); /* clear digraphs */
6076 do_digraph(buf[i-1]); /* may be the start of a digraph */
6077#endif
6078 buf[i] = NUL;
6079 ins_str(buf);
6080 if (flags & INSCHAR_CTRLV)
6081 {
6082 redo_literal(*buf);
6083 i = 1;
6084 }
6085 else
6086 i = 0;
6087 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006088 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 }
6090 else
6091 {
6092#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006093 int cc;
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6096 {
6097 char_u buf[MB_MAXBYTES + 1];
6098
6099 (*mb_char2bytes)(c, buf);
6100 buf[cc] = NUL;
6101 ins_char_bytes(buf, cc);
6102 AppendCharToRedobuff(c);
6103 }
6104 else
6105#endif
6106 {
6107 ins_char(c);
6108 if (flags & INSCHAR_CTRLV)
6109 redo_literal(c);
6110 else
6111 AppendCharToRedobuff(c);
6112 }
6113 }
6114}
6115
6116/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006117 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006118 *
6119 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6120 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006121 */
6122 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006123internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006124 int textwidth;
6125 int second_indent;
6126 int flags;
6127 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006128 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006129{
6130 int cc;
6131 int save_char = NUL;
6132 int haveto_redraw = FALSE;
6133 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6134#ifdef FEAT_MBYTE
6135 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6136#endif
6137 int fo_white_par = has_format_option(FO_WHITE_PAR);
6138 int first_line = TRUE;
6139#ifdef FEAT_COMMENTS
6140 colnr_T leader_len;
6141 int no_leader = FALSE;
6142 int do_comments = (flags & INSCHAR_DO_COM);
6143#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006144#ifdef FEAT_LINEBREAK
6145 int has_lbr = curwin->w_p_lbr;
6146
6147 /* make sure win_lbr_chartabsize() counts correctly */
6148 curwin->w_p_lbr = FALSE;
6149#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006150
6151 /*
6152 * When 'ai' is off we don't want a space under the cursor to be
6153 * deleted. Replace it with an 'x' temporarily.
6154 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006155 if (!curbuf->b_p_ai
6156#ifdef FEAT_VREPLACE
6157 && !(State & VREPLACE_FLAG)
6158#endif
6159 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006160 {
6161 cc = gchar_cursor();
6162 if (vim_iswhite(cc))
6163 {
6164 save_char = cc;
6165 pchar_cursor('x');
6166 }
6167 }
6168
6169 /*
6170 * Repeat breaking lines, until the current line is not too long.
6171 */
6172 while (!got_int)
6173 {
6174 int startcol; /* Cursor column at entry */
6175 int wantcol; /* column at textwidth border */
6176 int foundcol; /* column for start of spaces */
6177 int end_foundcol = 0; /* column for start of word */
6178 colnr_T len;
6179 colnr_T virtcol;
6180#ifdef FEAT_VREPLACE
6181 int orig_col = 0;
6182 char_u *saved_text = NULL;
6183#endif
6184 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006185 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186
Bram Moolenaar97b98102009-11-17 16:41:01 +00006187 virtcol = get_nolist_virtcol()
6188 + char2cells(c != NUL ? c : gchar_cursor());
6189 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190 break;
6191
6192#ifdef FEAT_COMMENTS
6193 if (no_leader)
6194 do_comments = FALSE;
6195 else if (!(flags & INSCHAR_FORMAT)
6196 && has_format_option(FO_WRAP_COMS))
6197 do_comments = TRUE;
6198
6199 /* Don't break until after the comment leader */
6200 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006201 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202 else
6203 leader_len = 0;
6204
6205 /* If the line doesn't start with a comment leader, then don't
6206 * start one in a following broken line. Avoids that a %word
6207 * moved to the start of the next line causes all following lines
6208 * to start with %. */
6209 if (leader_len == 0)
6210 no_leader = TRUE;
6211#endif
6212 if (!(flags & INSCHAR_FORMAT)
6213#ifdef FEAT_COMMENTS
6214 && leader_len == 0
6215#endif
6216 && !has_format_option(FO_WRAP))
6217
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219 if ((startcol = curwin->w_cursor.col) == 0)
6220 break;
6221
6222 /* find column of textwidth border */
6223 coladvance((colnr_T)textwidth);
6224 wantcol = curwin->w_cursor.col;
6225
Bram Moolenaar97b98102009-11-17 16:41:01 +00006226 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227 foundcol = 0;
6228
6229 /*
6230 * Find position to break at.
6231 * Stop at first entered white when 'formatoptions' has 'v'
6232 */
6233 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006234 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006235 || curwin->w_cursor.lnum != Insstart.lnum
6236 || curwin->w_cursor.col >= Insstart.col)
6237 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006238 if (curwin->w_cursor.col == startcol && c != NUL)
6239 cc = c;
6240 else
6241 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242 if (WHITECHAR(cc))
6243 {
6244 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006245 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246
6247 /* find start of sequence of blanks */
6248 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6249 {
6250 dec_cursor();
6251 cc = gchar_cursor();
6252 }
6253 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6254 break; /* only spaces in front of text */
6255#ifdef FEAT_COMMENTS
6256 /* Don't break until after the comment leader */
6257 if (curwin->w_cursor.col < leader_len)
6258 break;
6259#endif
6260 if (has_format_option(FO_ONE_LETTER))
6261 {
6262 /* do not break after one-letter words */
6263 if (curwin->w_cursor.col == 0)
6264 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006265#ifdef FEAT_COMMENTS
6266 /* do not break "#a b" when 'tw' is 2 */
6267 if (curwin->w_cursor.col <= leader_len)
6268 break;
6269#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006270 col = curwin->w_cursor.col;
6271 dec_cursor();
6272 cc = gchar_cursor();
6273
6274 if (WHITECHAR(cc))
6275 continue; /* one-letter, continue */
6276 curwin->w_cursor.col = col;
6277 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006278
6279 inc_cursor();
6280
6281 end_foundcol = end_col + 1;
6282 foundcol = curwin->w_cursor.col;
6283 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284 break;
6285 }
6286#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006287 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288 {
6289 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006290 if (curwin->w_cursor.col != startcol)
6291 {
6292#ifdef FEAT_COMMENTS
6293 /* Don't break until after the comment leader */
6294 if (curwin->w_cursor.col < leader_len)
6295 break;
6296#endif
6297 col = curwin->w_cursor.col;
6298 inc_cursor();
6299 /* Don't change end_foundcol if already set. */
6300 if (foundcol != curwin->w_cursor.col)
6301 {
6302 foundcol = curwin->w_cursor.col;
6303 end_foundcol = foundcol;
6304 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6305 break;
6306 }
6307 curwin->w_cursor.col = col;
6308 }
6309
6310 if (curwin->w_cursor.col == 0)
6311 break;
6312
6313 col = curwin->w_cursor.col;
6314
6315 dec_cursor();
6316 cc = gchar_cursor();
6317
6318 if (WHITECHAR(cc))
6319 continue; /* break with space */
6320#ifdef FEAT_COMMENTS
6321 /* Don't break until after the comment leader */
6322 if (curwin->w_cursor.col < leader_len)
6323 break;
6324#endif
6325
6326 curwin->w_cursor.col = col;
6327
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006328 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006329 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006330 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6331 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 }
6333#endif
6334 if (curwin->w_cursor.col == 0)
6335 break;
6336 dec_cursor();
6337 }
6338
6339 if (foundcol == 0) /* no spaces, cannot break line */
6340 {
6341 curwin->w_cursor.col = startcol;
6342 break;
6343 }
6344
6345 /* Going to break the line, remove any "$" now. */
6346 undisplay_dollar();
6347
6348 /*
6349 * Offset between cursor position and line break is used by replace
6350 * stack functions. VREPLACE does not use this, and backspaces
6351 * over the text instead.
6352 */
6353#ifdef FEAT_VREPLACE
6354 if (State & VREPLACE_FLAG)
6355 orig_col = startcol; /* Will start backspacing from here */
6356 else
6357#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006358 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359
6360 /*
6361 * adjust startcol for spaces that will be deleted and
6362 * characters that will remain on top line
6363 */
6364 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006365 while ((cc = gchar_cursor(), WHITECHAR(cc))
6366 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006367 inc_cursor();
6368 startcol -= curwin->w_cursor.col;
6369 if (startcol < 0)
6370 startcol = 0;
6371
6372#ifdef FEAT_VREPLACE
6373 if (State & VREPLACE_FLAG)
6374 {
6375 /*
6376 * In VREPLACE mode, we will backspace over the text to be
6377 * wrapped, so save a copy now to put on the next line.
6378 */
6379 saved_text = vim_strsave(ml_get_cursor());
6380 curwin->w_cursor.col = orig_col;
6381 if (saved_text == NULL)
6382 break; /* Can't do it, out of memory */
6383 saved_text[startcol] = NUL;
6384
6385 /* Backspace over characters that will move to the next line */
6386 if (!fo_white_par)
6387 backspace_until_column(foundcol);
6388 }
6389 else
6390#endif
6391 {
6392 /* put cursor after pos. to break line */
6393 if (!fo_white_par)
6394 curwin->w_cursor.col = foundcol;
6395 }
6396
6397 /*
6398 * Split the line just before the margin.
6399 * Only insert/delete lines, but don't really redraw the window.
6400 */
6401 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6402 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6403#ifdef FEAT_COMMENTS
6404 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006405 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006407 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6408 if (!(flags & INSCHAR_COM_LIST))
6409 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410
6411 replace_offset = 0;
6412 if (first_line)
6413 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006414 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006415 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006416 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006417 * This section is for auto-wrap of numeric lists. When not
6418 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6419 * flag will be set and open_line() will handle it (as seen
6420 * above). The code here (and in get_number_indent()) will
6421 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006422 */
6423 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006424 second_indent =
6425 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006426 if (second_indent >= 0)
6427 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006429 if (State & VREPLACE_FLAG)
6430 change_indent(INDENT_SET, second_indent,
6431 FALSE, NUL, TRUE);
6432 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006434#ifdef FEAT_COMMENTS
6435 if (leader_len > 0 && second_indent - leader_len > 0)
6436 {
6437 int i;
6438 int padding = second_indent - leader_len;
6439
6440 /* We started at the first_line of a numbered list
6441 * that has a comment. the open_line() function has
6442 * inserted the proper comment leader and positioned
6443 * the cursor at the end of the split line. Now we
6444 * add the additional whitespace needed after the
6445 * comment leader for the numbered list. */
6446 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006447 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006448 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006449 }
6450 else
6451 {
6452#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006453 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006454#ifdef FEAT_COMMENTS
6455 }
6456#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006457 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006458 }
6459 first_line = FALSE;
6460 }
6461
6462#ifdef FEAT_VREPLACE
6463 if (State & VREPLACE_FLAG)
6464 {
6465 /*
6466 * In VREPLACE mode we have backspaced over the text to be
6467 * moved, now we re-insert it into the new line.
6468 */
6469 ins_bytes(saved_text);
6470 vim_free(saved_text);
6471 }
6472 else
6473#endif
6474 {
6475 /*
6476 * Check if cursor is not past the NUL off the line, cindent
6477 * may have added or removed indent.
6478 */
6479 curwin->w_cursor.col += startcol;
6480 len = (colnr_T)STRLEN(ml_get_curline());
6481 if (curwin->w_cursor.col > len)
6482 curwin->w_cursor.col = len;
6483 }
6484
6485 haveto_redraw = TRUE;
6486#ifdef FEAT_CINDENT
6487 can_cindent = TRUE;
6488#endif
6489 /* moved the cursor, don't autoindent or cindent now */
6490 did_ai = FALSE;
6491#ifdef FEAT_SMARTINDENT
6492 did_si = FALSE;
6493 can_si = FALSE;
6494 can_si_back = FALSE;
6495#endif
6496 line_breakcheck();
6497 }
6498
6499 if (save_char != NUL) /* put back space after cursor */
6500 pchar_cursor(save_char);
6501
Bram Moolenaar0026d472014-09-09 16:32:39 +02006502#ifdef FEAT_LINEBREAK
6503 curwin->w_p_lbr = has_lbr;
6504#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505 if (!format_only && haveto_redraw)
6506 {
6507 update_topline();
6508 redraw_curbuf_later(VALID);
6509 }
6510}
6511
6512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * Called after inserting or deleting text: When 'formatoptions' includes the
6514 * 'a' flag format from the current line until the end of the paragraph.
6515 * Keep the cursor at the same position relative to the text.
6516 * The caller must have saved the cursor line for undo, following ones will be
6517 * saved here.
6518 */
6519 void
6520auto_format(trailblank, prev_line)
6521 int trailblank; /* when TRUE also format with trailing blank */
6522 int prev_line; /* may start in previous line */
6523{
6524 pos_T pos;
6525 colnr_T len;
6526 char_u *old;
6527 char_u *new, *pnew;
6528 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006529 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531 if (!has_format_option(FO_AUTO))
6532 return;
6533
6534 pos = curwin->w_cursor;
6535 old = ml_get_curline();
6536
6537 /* may remove added space */
6538 check_auto_format(FALSE);
6539
6540 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6541 * user might insert normal text next. Also skip formatting when "1" is
6542 * in 'formatoptions' and there is a single character before the cursor.
6543 * Otherwise the line would be broken and when typing another non-white
6544 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006545 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (*old != NUL && !trailblank && wasatend)
6547 {
6548 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006549 cc = gchar_cursor();
6550 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6551 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006553 cc = gchar_cursor();
6554 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 curwin->w_cursor = pos;
6557 return;
6558 }
6559 curwin->w_cursor = pos;
6560 }
6561
6562#ifdef FEAT_COMMENTS
6563 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6564 * comments. */
6565 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006566 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 return;
6568#endif
6569
6570 /*
6571 * May start formatting in a previous line, so that after "x" a word is
6572 * moved to the previous line if it fits there now. Only when this is not
6573 * the start of a paragraph.
6574 */
6575 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6576 {
6577 --curwin->w_cursor.lnum;
6578 if (u_save_cursor() == FAIL)
6579 return;
6580 }
6581
6582 /*
6583 * Do the formatting and restore the cursor position. "saved_cursor" will
6584 * be adjusted for the text formatting.
6585 */
6586 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006587 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 curwin->w_cursor = saved_cursor;
6589 saved_cursor.lnum = 0;
6590
6591 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6592 {
6593 /* "cannot happen" */
6594 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6595 coladvance((colnr_T)MAXCOL);
6596 }
6597 else
6598 check_cursor_col();
6599
6600 /* Insert mode: If the cursor is now after the end of the line while it
6601 * previously wasn't, the line was broken. Because of the rule above we
6602 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6603 * formatted. */
6604 if (!wasatend && has_format_option(FO_WHITE_PAR))
6605 {
6606 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006607 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (curwin->w_cursor.col == len)
6609 {
6610 pnew = vim_strnsave(new, len + 2);
6611 pnew[len] = ' ';
6612 pnew[len + 1] = NUL;
6613 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6614 /* remove the space later */
6615 did_add_space = TRUE;
6616 }
6617 else
6618 /* may remove added space */
6619 check_auto_format(FALSE);
6620 }
6621
6622 check_cursor();
6623}
6624
6625/*
6626 * When an extra space was added to continue a paragraph for auto-formatting,
6627 * delete it now. The space must be under the cursor, just after the insert
6628 * position.
6629 */
6630 static void
6631check_auto_format(end_insert)
6632 int end_insert; /* TRUE when ending Insert mode */
6633{
6634 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006635 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637 if (did_add_space)
6638 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006639 cc = gchar_cursor();
6640 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 /* Somehow the space was removed already. */
6642 did_add_space = FALSE;
6643 else
6644 {
6645 if (!end_insert)
6646 {
6647 inc_cursor();
6648 c = gchar_cursor();
6649 dec_cursor();
6650 }
6651 if (c != NUL)
6652 {
6653 /* The space is no longer at the end of the line, delete it. */
6654 del_char(FALSE);
6655 did_add_space = FALSE;
6656 }
6657 }
6658 }
6659}
6660
6661/*
6662 * Find out textwidth to be used for formatting:
6663 * if 'textwidth' option is set, use it
6664 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6665 * if invalid value, use 0.
6666 * Set default to window width (maximum 79) for "gq" operator.
6667 */
6668 int
6669comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006670 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int textwidth;
6673
6674 textwidth = curbuf->b_p_tw;
6675 if (textwidth == 0 && curbuf->b_p_wm)
6676 {
6677 /* The width is the window width minus 'wrapmargin' minus all the
6678 * things that add to the margin. */
6679 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6680#ifdef FEAT_CMDWIN
6681 if (cmdwin_type != 0)
6682 textwidth -= 1;
6683#endif
6684#ifdef FEAT_FOLDING
6685 textwidth -= curwin->w_p_fdc;
6686#endif
6687#ifdef FEAT_SIGNS
6688 if (curwin->w_buffer->b_signlist != NULL
6689# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006690 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691# endif
6692 )
6693 textwidth -= 1;
6694#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006695 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 textwidth -= 8;
6697 }
6698 if (textwidth < 0)
6699 textwidth = 0;
6700 if (ff && textwidth == 0)
6701 {
6702 textwidth = W_WIDTH(curwin) - 1;
6703 if (textwidth > 79)
6704 textwidth = 79;
6705 }
6706 return textwidth;
6707}
6708
6709/*
6710 * Put a character in the redo buffer, for when just after a CTRL-V.
6711 */
6712 static void
6713redo_literal(c)
6714 int c;
6715{
6716 char_u buf[10];
6717
6718 /* Only digits need special treatment. Translate them into a string of
6719 * three digits. */
6720 if (VIM_ISDIGIT(c))
6721 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006722 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 AppendToRedobuff(buf);
6724 }
6725 else
6726 AppendCharToRedobuff(c);
6727}
6728
6729/*
6730 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006731 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 */
6733 static void
6734start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006735 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 if (!arrow_used) /* something has been inserted */
6738 {
6739 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006740 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 arrow_used = TRUE; /* this means we stopped the current insert */
6742 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006743#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006744 check_spell_redraw();
6745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746}
6747
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006748#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006749/*
6750 * If we skipped highlighting word at cursor, do it now.
6751 * It may be skipped again, thus reset spell_redraw_lnum first.
6752 */
6753 static void
6754check_spell_redraw()
6755{
6756 if (spell_redraw_lnum != 0)
6757 {
6758 linenr_T lnum = spell_redraw_lnum;
6759
6760 spell_redraw_lnum = 0;
6761 redrawWinline(lnum, FALSE);
6762 }
6763}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006764
6765/*
6766 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6767 * spelled word, if there is one.
6768 */
6769 static void
6770spell_back_to_badword()
6771{
6772 pos_T tpos = curwin->w_cursor;
6773
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006774 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006775 if (curwin->w_cursor.col != tpos.col)
6776 start_arrow(&tpos);
6777}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006778#endif
6779
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780/*
6781 * stop_arrow() is called before a change is made in insert mode.
6782 * If an arrow key has been used, start a new insertion.
6783 * Returns FAIL if undo is impossible, shouldn't insert then.
6784 */
6785 int
6786stop_arrow()
6787{
6788 if (arrow_used)
6789 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006790 Insstart = curwin->w_cursor; /* new insertion starts here */
6791 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6792 /* Don't update the original insert position when moved to the
6793 * right, except when nothing was inserted yet. */
6794 update_Insstart_orig = FALSE;
6795 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (u_save_cursor() == OK)
6798 {
6799 arrow_used = FALSE;
6800 ins_need_undo = FALSE;
6801 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 ai_col = 0;
6804#ifdef FEAT_VREPLACE
6805 if (State & VREPLACE_FLAG)
6806 {
6807 orig_line_count = curbuf->b_ml.ml_line_count;
6808 vr_lines_changed = 1;
6809 }
6810#endif
6811 ResetRedobuff();
6812 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006813 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815 else if (ins_need_undo)
6816 {
6817 if (u_save_cursor() == OK)
6818 ins_need_undo = FALSE;
6819 }
6820
6821#ifdef FEAT_FOLDING
6822 /* Always open fold at the cursor line when inserting something. */
6823 foldOpenCursor();
6824#endif
6825
6826 return (arrow_used || ins_need_undo ? FAIL : OK);
6827}
6828
6829/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006830 * Do a few things to stop inserting.
6831 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6832 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 */
6834 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006835stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006836 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006837 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006838 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006840 int cc;
6841 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
6843 stop_redo_ins();
6844 replace_flush(); /* abandon replace stack */
6845
6846 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006847 * Save the inserted text for later redo with ^@ and CTRL-A.
6848 * Don't do it when "restart_edit" was set and nothing was inserted,
6849 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006851 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006852 if (did_restart_edit == 0 || (ptr != NULL
6853 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006854 {
6855 vim_free(last_insert);
6856 last_insert = ptr;
6857 last_insert_skip = new_insert_skip;
6858 }
6859 else
6860 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006862 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 /* Auto-format now. It may seem strange to do this when stopping an
6865 * insertion (or moving the cursor), but it's required when appending
6866 * a line and having it end in a space. But only do it when something
6867 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006868 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006870 pos_T tpos = curwin->w_cursor;
6871
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 /* When the cursor is at the end of the line after a space the
6873 * formatting will move it to the following word. Avoid that by
6874 * moving the cursor onto the space. */
6875 cc = 'x';
6876 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6877 {
6878 dec_cursor();
6879 cc = gchar_cursor();
6880 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006881 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 }
6883
6884 auto_format(TRUE, FALSE);
6885
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006886 if (vim_iswhite(cc))
6887 {
6888 if (gchar_cursor() != NUL)
6889 inc_cursor();
6890#ifdef FEAT_VIRTUALEDIT
6891 /* If the cursor is still at the same character, also keep
6892 * the "coladd". */
6893 if (gchar_cursor() == NUL
6894 && curwin->w_cursor.lnum == tpos.lnum
6895 && curwin->w_cursor.col == tpos.col)
6896 curwin->w_cursor.coladd = tpos.coladd;
6897#endif
6898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900
6901 /* If a space was inserted for auto-formatting, remove it now. */
6902 check_auto_format(TRUE);
6903
6904 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006905 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006906 * Do this when ESC was used or moving the cursor up/down.
6907 * Check for the old position still being valid, just in case the text
6908 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006909 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006910 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6911 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006913 pos_T tpos = curwin->w_cursor;
6914
6915 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006916 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006917 for (;;)
6918 {
6919 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6920 --curwin->w_cursor.col;
6921 cc = gchar_cursor();
6922 if (!vim_iswhite(cc))
6923 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006924 if (del_char(TRUE) == FAIL)
6925 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006926 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006927 if (curwin->w_cursor.lnum != tpos.lnum)
6928 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006929 else
6930 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01006931 /* reset tpos, could have been invalidated in the loop above */
6932 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006933 tpos.col++;
6934 if (cc != NUL && gchar_pos(&tpos) == NUL)
6935 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 /* <C-S-Right> may have started Visual mode, adjust the position for
6939 * deleted characters. */
6940 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6941 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006942 int len = (int)STRLEN(ml_get_curline());
6943
6944 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006946 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006947#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
6951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 }
6953 }
6954 did_ai = FALSE;
6955#ifdef FEAT_SMARTINDENT
6956 did_si = FALSE;
6957 can_si = FALSE;
6958 can_si_back = FALSE;
6959#endif
6960
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006961 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6962 * now in a different buffer. */
6963 if (end_insert_pos != NULL)
6964 {
6965 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01006966 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006967 curbuf->b_op_end = *end_insert_pos;
6968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969}
6970
6971/*
6972 * Set the last inserted text to a single character.
6973 * Used for the replace command.
6974 */
6975 void
6976set_last_insert(c)
6977 int c;
6978{
6979 char_u *s;
6980
6981 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (last_insert != NULL)
6984 {
6985 s = last_insert;
6986 /* Use the CTRL-V only when entering a special char */
6987 if (c < ' ' || c == DEL)
6988 *s++ = Ctrl_V;
6989 s = add_char2buf(c, s);
6990 *s++ = ESC;
6991 *s++ = NUL;
6992 last_insert_skip = 0;
6993 }
6994}
6995
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006996#if defined(EXITFREE) || defined(PROTO)
6997 void
6998free_last_insert()
6999{
7000 vim_free(last_insert);
7001 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007002# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007003 vim_free(compl_orig_text);
7004 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007005# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007006}
7007#endif
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009/*
7010 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7011 * and CSI. Handle multi-byte characters.
7012 * Returns a pointer to after the added bytes.
7013 */
7014 char_u *
7015add_char2buf(c, s)
7016 int c;
7017 char_u *s;
7018{
7019#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007020 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 int i;
7022 int len;
7023
7024 len = (*mb_char2bytes)(c, temp);
7025 for (i = 0; i < len; ++i)
7026 {
7027 c = temp[i];
7028#endif
7029 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7030 if (c == K_SPECIAL)
7031 {
7032 *s++ = K_SPECIAL;
7033 *s++ = KS_SPECIAL;
7034 *s++ = KE_FILLER;
7035 }
7036#ifdef FEAT_GUI
7037 else if (c == CSI)
7038 {
7039 *s++ = CSI;
7040 *s++ = KS_EXTRA;
7041 *s++ = (int)KE_CSI;
7042 }
7043#endif
7044 else
7045 *s++ = c;
7046#ifdef FEAT_MBYTE
7047 }
7048#endif
7049 return s;
7050}
7051
7052/*
7053 * move cursor to start of line
7054 * if flags & BL_WHITE move to first non-white
7055 * if flags & BL_SOL move to first non-white if startofline is set,
7056 * otherwise keep "curswant" column
7057 * if flags & BL_FIX don't leave the cursor on a NUL.
7058 */
7059 void
7060beginline(flags)
7061 int flags;
7062{
7063 if ((flags & BL_SOL) && !p_sol)
7064 coladvance(curwin->w_curswant);
7065 else
7066 {
7067 curwin->w_cursor.col = 0;
7068#ifdef FEAT_VIRTUALEDIT
7069 curwin->w_cursor.coladd = 0;
7070#endif
7071
7072 if (flags & (BL_WHITE | BL_SOL))
7073 {
7074 char_u *ptr;
7075
7076 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7077 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7078 ++curwin->w_cursor.col;
7079 }
7080 curwin->w_set_curswant = TRUE;
7081 }
7082}
7083
7084/*
7085 * oneright oneleft cursor_down cursor_up
7086 *
7087 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007088 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 * Return OK when successful, FAIL when we hit a line of file boundary.
7090 */
7091
7092 int
7093oneright()
7094{
7095 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098#ifdef FEAT_VIRTUALEDIT
7099 if (virtual_active())
7100 {
7101 pos_T prevpos = curwin->w_cursor;
7102
7103 /* Adjust for multi-wide char (excluding TAB) */
7104 ptr = ml_get_cursor();
7105 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007106# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007108# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 ))
7112 ? ptr2cells(ptr) : 1));
7113 curwin->w_set_curswant = TRUE;
7114 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7115 return (prevpos.col != curwin->w_cursor.col
7116 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7117 }
7118#endif
7119
7120 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007121 if (*ptr == NUL)
7122 return FAIL; /* already at the very end */
7123
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007125 if (has_mbyte)
7126 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 else
7128#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007129 l = 1;
7130
7131 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7132 * contains "onemore". */
7133 if (ptr[l] == NUL
7134#ifdef FEAT_VIRTUALEDIT
7135 && (ve_flags & VE_ONEMORE) == 0
7136#endif
7137 )
7138 return FAIL;
7139 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140
7141 curwin->w_set_curswant = TRUE;
7142 return OK;
7143}
7144
7145 int
7146oneleft()
7147{
7148#ifdef FEAT_VIRTUALEDIT
7149 if (virtual_active())
7150 {
7151 int width;
7152 int v = getviscol();
7153
7154 if (v == 0)
7155 return FAIL;
7156
7157# ifdef FEAT_LINEBREAK
7158 /* We might get stuck on 'showbreak', skip over it. */
7159 width = 1;
7160 for (;;)
7161 {
7162 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007163 /* getviscol() is slow, skip it when 'showbreak' is empty,
7164 * 'breakindent' is not set and there are no multi-byte
7165 * characters */
7166 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167# ifdef FEAT_MBYTE
7168 && !has_mbyte
7169# endif
7170 ) || getviscol() < v)
7171 break;
7172 ++width;
7173 }
7174# else
7175 coladvance(v - 1);
7176# endif
7177
7178 if (curwin->w_cursor.coladd == 1)
7179 {
7180 char_u *ptr;
7181
7182 /* Adjust for multi-wide char (not a TAB) */
7183 ptr = ml_get_cursor();
7184 if (*ptr != TAB && vim_isprintc(
7185# ifdef FEAT_MBYTE
7186 (*mb_ptr2char)(ptr)
7187# else
7188 *ptr
7189# endif
7190 ) && ptr2cells(ptr) > 1)
7191 curwin->w_cursor.coladd = 0;
7192 }
7193
7194 curwin->w_set_curswant = TRUE;
7195 return OK;
7196 }
7197#endif
7198
7199 if (curwin->w_cursor.col == 0)
7200 return FAIL;
7201
7202 curwin->w_set_curswant = TRUE;
7203 --curwin->w_cursor.col;
7204
7205#ifdef FEAT_MBYTE
7206 /* if the character on the left of the current cursor is a multi-byte
7207 * character, move to its first byte */
7208 if (has_mbyte)
7209 mb_adjust_cursor();
7210#endif
7211 return OK;
7212}
7213
7214 int
7215cursor_up(n, upd_topline)
7216 long n;
7217 int upd_topline; /* When TRUE: update topline */
7218{
7219 linenr_T lnum;
7220
7221 if (n > 0)
7222 {
7223 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007224 /* This fails if the cursor is already in the first line or the count
7225 * is larger than the line number and '-' is in 'cpoptions' */
7226 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 return FAIL;
7228 if (n >= lnum)
7229 lnum = 1;
7230 else
7231#ifdef FEAT_FOLDING
7232 if (hasAnyFolding(curwin))
7233 {
7234 /*
7235 * Count each sequence of folded lines as one logical line.
7236 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007237 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 (void)hasFolding(lnum, &lnum, NULL);
7239
7240 while (n--)
7241 {
7242 /* move up one line */
7243 --lnum;
7244 if (lnum <= 1)
7245 break;
7246 /* If we entered a fold, move to the beginning, unless in
7247 * Insert mode or when 'foldopen' contains "all": it will open
7248 * in a moment. */
7249 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7250 (void)hasFolding(lnum, &lnum, NULL);
7251 }
7252 if (lnum < 1)
7253 lnum = 1;
7254 }
7255 else
7256#endif
7257 lnum -= n;
7258 curwin->w_cursor.lnum = lnum;
7259 }
7260
7261 /* try to advance to the column we want to be at */
7262 coladvance(curwin->w_curswant);
7263
7264 if (upd_topline)
7265 update_topline(); /* make sure curwin->w_topline is valid */
7266
7267 return OK;
7268}
7269
7270/*
7271 * Cursor down a number of logical lines.
7272 */
7273 int
7274cursor_down(n, upd_topline)
7275 long n;
7276 int upd_topline; /* When TRUE: update topline */
7277{
7278 linenr_T lnum;
7279
7280 if (n > 0)
7281 {
7282 lnum = curwin->w_cursor.lnum;
7283#ifdef FEAT_FOLDING
7284 /* Move to last line of fold, will fail if it's the end-of-file. */
7285 (void)hasFolding(lnum, NULL, &lnum);
7286#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007287 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007288 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007289 if (lnum >= curbuf->b_ml.ml_line_count
7290 || (lnum + n > curbuf->b_ml.ml_line_count
7291 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 return FAIL;
7293 if (lnum + n >= curbuf->b_ml.ml_line_count)
7294 lnum = curbuf->b_ml.ml_line_count;
7295 else
7296#ifdef FEAT_FOLDING
7297 if (hasAnyFolding(curwin))
7298 {
7299 linenr_T last;
7300
7301 /* count each sequence of folded lines as one logical line */
7302 while (n--)
7303 {
7304 if (hasFolding(lnum, NULL, &last))
7305 lnum = last + 1;
7306 else
7307 ++lnum;
7308 if (lnum >= curbuf->b_ml.ml_line_count)
7309 break;
7310 }
7311 if (lnum > curbuf->b_ml.ml_line_count)
7312 lnum = curbuf->b_ml.ml_line_count;
7313 }
7314 else
7315#endif
7316 lnum += n;
7317 curwin->w_cursor.lnum = lnum;
7318 }
7319
7320 /* try to advance to the column we want to be at */
7321 coladvance(curwin->w_curswant);
7322
7323 if (upd_topline)
7324 update_topline(); /* make sure curwin->w_topline is valid */
7325
7326 return OK;
7327}
7328
7329/*
7330 * Stuff the last inserted text in the read buffer.
7331 * Last_insert actually is a copy of the redo buffer, so we
7332 * first have to remove the command.
7333 */
7334 int
7335stuff_inserted(c, count, no_esc)
7336 int c; /* Command character to be inserted */
7337 long count; /* Repeat this many times */
7338 int no_esc; /* Don't add an ESC at the end */
7339{
7340 char_u *esc_ptr;
7341 char_u *ptr;
7342 char_u *last_ptr;
7343 char_u last = NUL;
7344
7345 ptr = get_last_insert();
7346 if (ptr == NULL)
7347 {
7348 EMSG(_(e_noinstext));
7349 return FAIL;
7350 }
7351
7352 /* may want to stuff the command character, to start Insert mode */
7353 if (c != NUL)
7354 stuffcharReadbuff(c);
7355 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7356 *esc_ptr = NUL; /* remove the ESC */
7357
7358 /* when the last char is either "0" or "^" it will be quoted if no ESC
7359 * comes after it OR if it will inserted more than once and "ptr"
7360 * starts with ^D. -- Acevedo
7361 */
7362 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7363 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7364 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7365 {
7366 last = *last_ptr;
7367 *last_ptr = NUL;
7368 }
7369
7370 do
7371 {
7372 stuffReadbuff(ptr);
7373 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7374 if (last)
7375 stuffReadbuff((char_u *)(last == '0'
7376 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7377 : IF_EB("\026^", CTRL_V_STR "^")));
7378 }
7379 while (--count > 0);
7380
7381 if (last)
7382 *last_ptr = last;
7383
7384 if (esc_ptr != NULL)
7385 *esc_ptr = ESC; /* put the ESC back */
7386
7387 /* may want to stuff a trailing ESC, to get out of Insert mode */
7388 if (!no_esc)
7389 stuffcharReadbuff(ESC);
7390
7391 return OK;
7392}
7393
7394 char_u *
7395get_last_insert()
7396{
7397 if (last_insert == NULL)
7398 return NULL;
7399 return last_insert + last_insert_skip;
7400}
7401
7402/*
7403 * Get last inserted string, and remove trailing <Esc>.
7404 * Returns pointer to allocated memory (must be freed) or NULL.
7405 */
7406 char_u *
7407get_last_insert_save()
7408{
7409 char_u *s;
7410 int len;
7411
7412 if (last_insert == NULL)
7413 return NULL;
7414 s = vim_strsave(last_insert + last_insert_skip);
7415 if (s != NULL)
7416 {
7417 len = (int)STRLEN(s);
7418 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7419 s[len - 1] = NUL;
7420 }
7421 return s;
7422}
7423
7424/*
7425 * Check the word in front of the cursor for an abbreviation.
7426 * Called when the non-id character "c" has been entered.
7427 * When an abbreviation is recognized it is removed from the text and
7428 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7429 */
7430 static int
7431echeck_abbr(c)
7432 int c;
7433{
7434 /* Don't check for abbreviation in paste mode, when disabled and just
7435 * after moving around with cursor keys. */
7436 if (p_paste || no_abbr || arrow_used)
7437 return FALSE;
7438
7439 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7440 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7441}
7442
7443/*
7444 * replace-stack functions
7445 *
7446 * When replacing characters, the replaced characters are remembered for each
7447 * new character. This is used to re-insert the old text when backspacing.
7448 *
7449 * There is a NUL headed list of characters for each character that is
7450 * currently in the file after the insertion point. When BS is used, one NUL
7451 * headed list is put back for the deleted character.
7452 *
7453 * For a newline, there are two NUL headed lists. One contains the characters
7454 * that the NL replaced. The extra one stores the characters after the cursor
7455 * that were deleted (always white space).
7456 *
7457 * Replace_offset is normally 0, in which case replace_push will add a new
7458 * character at the end of the stack. If replace_offset is not 0, that many
7459 * characters will be left on the stack above the newly inserted character.
7460 */
7461
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007462static char_u *replace_stack = NULL;
7463static long replace_stack_nr = 0; /* next entry in replace stack */
7464static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
7466 void
7467replace_push(c)
7468 int c; /* character that is replaced (NUL is none) */
7469{
7470 char_u *p;
7471
7472 if (replace_stack_nr < replace_offset) /* nothing to do */
7473 return;
7474 if (replace_stack_len <= replace_stack_nr)
7475 {
7476 replace_stack_len += 50;
7477 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7478 if (p == NULL) /* out of memory */
7479 {
7480 replace_stack_len -= 50;
7481 return;
7482 }
7483 if (replace_stack != NULL)
7484 {
7485 mch_memmove(p, replace_stack,
7486 (size_t)(replace_stack_nr * sizeof(char_u)));
7487 vim_free(replace_stack);
7488 }
7489 replace_stack = p;
7490 }
7491 p = replace_stack + replace_stack_nr - replace_offset;
7492 if (replace_offset)
7493 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7494 *p = c;
7495 ++replace_stack_nr;
7496}
7497
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007498#if defined(FEAT_MBYTE) || defined(PROTO)
7499/*
7500 * Push a character onto the replace stack. Handles a multi-byte character in
7501 * reverse byte order, so that the first byte is popped off first.
7502 * Return the number of bytes done (includes composing characters).
7503 */
7504 int
7505replace_push_mb(p)
7506 char_u *p;
7507{
7508 int l = (*mb_ptr2len)(p);
7509 int j;
7510
7511 for (j = l - 1; j >= 0; --j)
7512 replace_push(p[j]);
7513 return l;
7514}
7515#endif
7516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517/*
7518 * Pop one item from the replace stack.
7519 * return -1 if stack empty
7520 * return replaced character or NUL otherwise
7521 */
7522 static int
7523replace_pop()
7524{
7525 if (replace_stack_nr == 0)
7526 return -1;
7527 return (int)replace_stack[--replace_stack_nr];
7528}
7529
7530/*
7531 * Join the top two items on the replace stack. This removes to "off"'th NUL
7532 * encountered.
7533 */
7534 static void
7535replace_join(off)
7536 int off; /* offset for which NUL to remove */
7537{
7538 int i;
7539
7540 for (i = replace_stack_nr; --i >= 0; )
7541 if (replace_stack[i] == NUL && off-- <= 0)
7542 {
7543 --replace_stack_nr;
7544 mch_memmove(replace_stack + i, replace_stack + i + 1,
7545 (size_t)(replace_stack_nr - i));
7546 return;
7547 }
7548}
7549
7550/*
7551 * Pop bytes from the replace stack until a NUL is found, and insert them
7552 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7553 */
7554 static void
7555replace_pop_ins()
7556{
7557 int cc;
7558 int oldState = State;
7559
7560 State = NORMAL; /* don't want REPLACE here */
7561 while ((cc = replace_pop()) > 0)
7562 {
7563#ifdef FEAT_MBYTE
7564 mb_replace_pop_ins(cc);
7565#else
7566 ins_char(cc);
7567#endif
7568 dec_cursor();
7569 }
7570 State = oldState;
7571}
7572
7573#ifdef FEAT_MBYTE
7574/*
7575 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7576 * indicates a multi-byte char, pop the other bytes too.
7577 */
7578 static void
7579mb_replace_pop_ins(cc)
7580 int cc;
7581{
7582 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007583 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 int i;
7585 int c;
7586
7587 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7588 {
7589 buf[0] = cc;
7590 for (i = 1; i < n; ++i)
7591 buf[i] = replace_pop();
7592 ins_bytes_len(buf, n);
7593 }
7594 else
7595 ins_char(cc);
7596
7597 if (enc_utf8)
7598 /* Handle composing chars. */
7599 for (;;)
7600 {
7601 c = replace_pop();
7602 if (c == -1) /* stack empty */
7603 break;
7604 if ((n = MB_BYTE2LEN(c)) == 1)
7605 {
7606 /* Not a multi-byte char, put it back. */
7607 replace_push(c);
7608 break;
7609 }
7610 else
7611 {
7612 buf[0] = c;
7613 for (i = 1; i < n; ++i)
7614 buf[i] = replace_pop();
7615 if (utf_iscomposing(utf_ptr2char(buf)))
7616 ins_bytes_len(buf, n);
7617 else
7618 {
7619 /* Not a composing char, put it back. */
7620 for (i = n - 1; i >= 0; --i)
7621 replace_push(buf[i]);
7622 break;
7623 }
7624 }
7625 }
7626}
7627#endif
7628
7629/*
7630 * make the replace stack empty
7631 * (called when exiting replace mode)
7632 */
7633 static void
7634replace_flush()
7635{
7636 vim_free(replace_stack);
7637 replace_stack = NULL;
7638 replace_stack_len = 0;
7639 replace_stack_nr = 0;
7640}
7641
7642/*
7643 * Handle doing a BS for one character.
7644 * cc < 0: replace stack empty, just move cursor
7645 * cc == 0: character was inserted, delete it
7646 * cc > 0: character was replaced, put cc (first byte of original char) back
7647 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007648 * When "limit_col" is >= 0, don't delete before this column. Matters when
7649 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 */
7651 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007652replace_do_bs(limit_col)
7653 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 int cc;
7656#ifdef FEAT_VREPLACE
7657 int orig_len = 0;
7658 int ins_len;
7659 int orig_vcols = 0;
7660 colnr_T start_vcol;
7661 char_u *p;
7662 int i;
7663 int vcol;
7664#endif
7665
7666 cc = replace_pop();
7667 if (cc > 0)
7668 {
7669#ifdef FEAT_VREPLACE
7670 if (State & VREPLACE_FLAG)
7671 {
7672 /* Get the number of screen cells used by the character we are
7673 * going to delete. */
7674 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7675 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7676 }
7677#endif
7678#ifdef FEAT_MBYTE
7679 if (has_mbyte)
7680 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007681 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682# ifdef FEAT_VREPLACE
7683 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007684 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685# endif
7686 replace_push(cc);
7687 }
7688 else
7689#endif
7690 {
7691 pchar_cursor(cc);
7692#ifdef FEAT_VREPLACE
7693 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007694 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695#endif
7696 }
7697 replace_pop_ins();
7698
7699#ifdef FEAT_VREPLACE
7700 if (State & VREPLACE_FLAG)
7701 {
7702 /* Get the number of screen cells used by the inserted characters */
7703 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007704 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 vcol = start_vcol;
7706 for (i = 0; i < ins_len; ++i)
7707 {
7708 vcol += chartabsize(p + i, vcol);
7709#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007710 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711#endif
7712 }
7713 vcol -= start_vcol;
7714
7715 /* Delete spaces that were inserted after the cursor to keep the
7716 * text aligned. */
7717 curwin->w_cursor.col += ins_len;
7718 while (vcol > orig_vcols && gchar_cursor() == ' ')
7719 {
7720 del_char(FALSE);
7721 ++orig_vcols;
7722 }
7723 curwin->w_cursor.col -= ins_len;
7724 }
7725#endif
7726
7727 /* mark the buffer as changed and prepare for displaying */
7728 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7729 }
7730 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007731 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734#ifdef FEAT_CINDENT
7735/*
7736 * Return TRUE if C-indenting is on.
7737 */
7738 static int
7739cindent_on()
7740{
7741 return (!p_paste && (curbuf->b_p_cin
7742# ifdef FEAT_EVAL
7743 || *curbuf->b_p_inde != NUL
7744# endif
7745 ));
7746}
7747#endif
7748
7749#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7750/*
7751 * Re-indent the current line, based on the current contents of it and the
7752 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7753 * confused what all the part that handles Control-T is doing that I'm not.
7754 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7755 */
7756
7757 void
7758fixthisline(get_the_indent)
7759 int (*get_the_indent) __ARGS((void));
7760{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007761 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (linewhite(curwin->w_cursor.lnum))
7763 did_ai = TRUE; /* delete the indent if the line stays empty */
7764}
7765
7766 void
7767fix_indent()
7768{
7769 if (p_paste)
7770 return;
7771# ifdef FEAT_LISP
7772 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7773 fixthisline(get_lisp_indent);
7774# endif
7775# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7776 else
7777# endif
7778# ifdef FEAT_CINDENT
7779 if (cindent_on())
7780 do_c_expr_indent();
7781# endif
7782}
7783
7784#endif
7785
7786#ifdef FEAT_CINDENT
7787/*
7788 * return TRUE if 'cinkeys' contains the key "keytyped",
7789 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007790 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7792 *
7793 * "keytyped" can have a few special values:
7794 * KEY_OPEN_FORW
7795 * KEY_OPEN_BACK
7796 * KEY_COMPLETE just finished completion.
7797 *
7798 * If line_is_empty is TRUE accept keys with '0' before them.
7799 */
7800 int
7801in_cinkeys(keytyped, when, line_is_empty)
7802 int keytyped;
7803 int when;
7804 int line_is_empty;
7805{
7806 char_u *look;
7807 int try_match;
7808 int try_match_word;
7809 char_u *p;
7810 char_u *line;
7811 int icase;
7812 int i;
7813
Bram Moolenaar30848942009-12-24 14:46:12 +00007814 if (keytyped == NUL)
7815 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7816 return FALSE;
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818#ifdef FEAT_EVAL
7819 if (*curbuf->b_p_inde != NUL)
7820 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7821 else
7822#endif
7823 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7824 while (*look)
7825 {
7826 /*
7827 * Find out if we want to try a match with this key, depending on
7828 * 'when' and a '*' or '!' before the key.
7829 */
7830 switch (when)
7831 {
7832 case '*': try_match = (*look == '*'); break;
7833 case '!': try_match = (*look == '!'); break;
7834 default: try_match = (*look != '*'); break;
7835 }
7836 if (*look == '*' || *look == '!')
7837 ++look;
7838
7839 /*
7840 * If there is a '0', only accept a match if the line is empty.
7841 * But may still match when typing last char of a word.
7842 */
7843 if (*look == '0')
7844 {
7845 try_match_word = try_match;
7846 if (!line_is_empty)
7847 try_match = FALSE;
7848 ++look;
7849 }
7850 else
7851 try_match_word = FALSE;
7852
7853 /*
7854 * does it look like a control character?
7855 */
7856 if (*look == '^'
7857#ifdef EBCDIC
7858 && (Ctrl_chr(look[1]) != 0)
7859#else
7860 && look[1] >= '?' && look[1] <= '_'
7861#endif
7862 )
7863 {
7864 if (try_match && keytyped == Ctrl_chr(look[1]))
7865 return TRUE;
7866 look += 2;
7867 }
7868 /*
7869 * 'o' means "o" command, open forward.
7870 * 'O' means "O" command, open backward.
7871 */
7872 else if (*look == 'o')
7873 {
7874 if (try_match && keytyped == KEY_OPEN_FORW)
7875 return TRUE;
7876 ++look;
7877 }
7878 else if (*look == 'O')
7879 {
7880 if (try_match && keytyped == KEY_OPEN_BACK)
7881 return TRUE;
7882 ++look;
7883 }
7884
7885 /*
7886 * 'e' means to check for "else" at start of line and just before the
7887 * cursor.
7888 */
7889 else if (*look == 'e')
7890 {
7891 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7892 {
7893 p = ml_get_curline();
7894 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7895 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7896 return TRUE;
7897 }
7898 ++look;
7899 }
7900
7901 /*
7902 * ':' only causes an indent if it is at the end of a label or case
7903 * statement, or when it was before typing the ':' (to fix
7904 * class::method for C++).
7905 */
7906 else if (*look == ':')
7907 {
7908 if (try_match && keytyped == ':')
7909 {
7910 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007911 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007913 /* Need to get the line again after cin_islabel(). */
7914 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if (curwin->w_cursor.col > 2
7916 && p[curwin->w_cursor.col - 1] == ':'
7917 && p[curwin->w_cursor.col - 2] == ':')
7918 {
7919 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007920 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007921 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 p = ml_get_curline();
7923 p[curwin->w_cursor.col - 1] = ':';
7924 if (i)
7925 return TRUE;
7926 }
7927 }
7928 ++look;
7929 }
7930
7931
7932 /*
7933 * Is it a key in <>, maybe?
7934 */
7935 else if (*look == '<')
7936 {
7937 if (try_match)
7938 {
7939 /*
7940 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7941 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7942 * >, *, : and ! keys if they really really want to.
7943 */
7944 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7945 && keytyped == look[1])
7946 return TRUE;
7947
7948 if (keytyped == get_special_key_code(look + 1))
7949 return TRUE;
7950 }
7951 while (*look && *look != '>')
7952 look++;
7953 while (*look == '>')
7954 look++;
7955 }
7956
7957 /*
7958 * Is it a word: "=word"?
7959 */
7960 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7961 {
7962 ++look;
7963 if (*look == '~')
7964 {
7965 icase = TRUE;
7966 ++look;
7967 }
7968 else
7969 icase = FALSE;
7970 p = vim_strchr(look, ',');
7971 if (p == NULL)
7972 p = look + STRLEN(look);
7973 if ((try_match || try_match_word)
7974 && curwin->w_cursor.col >= (colnr_T)(p - look))
7975 {
7976 int match = FALSE;
7977
7978#ifdef FEAT_INS_EXPAND
7979 if (keytyped == KEY_COMPLETE)
7980 {
7981 char_u *s;
7982
7983 /* Just completed a word, check if it starts with "look".
7984 * search back for the start of a word. */
7985 line = ml_get_curline();
7986# ifdef FEAT_MBYTE
7987 if (has_mbyte)
7988 {
7989 char_u *n;
7990
7991 for (s = line + curwin->w_cursor.col; s > line; s = n)
7992 {
7993 n = mb_prevptr(line, s);
7994 if (!vim_iswordp(n))
7995 break;
7996 }
7997 }
7998 else
7999# endif
8000 for (s = line + curwin->w_cursor.col; s > line; --s)
8001 if (!vim_iswordc(s[-1]))
8002 break;
8003 if (s + (p - look) <= line + curwin->w_cursor.col
8004 && (icase
8005 ? MB_STRNICMP(s, look, p - look)
8006 : STRNCMP(s, look, p - look)) == 0)
8007 match = TRUE;
8008 }
8009 else
8010#endif
8011 /* TODO: multi-byte */
8012 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8013 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8014 {
8015 line = ml_get_cursor();
8016 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8017 || !vim_iswordc(line[-(p - look) - 1]))
8018 && (icase
8019 ? MB_STRNICMP(line - (p - look), look, p - look)
8020 : STRNCMP(line - (p - look), look, p - look))
8021 == 0)
8022 match = TRUE;
8023 }
8024 if (match && try_match_word && !try_match)
8025 {
8026 /* "0=word": Check if there are only blanks before the
8027 * word. */
8028 line = ml_get_curline();
8029 if ((int)(skipwhite(line) - line) !=
8030 (int)(curwin->w_cursor.col - (p - look)))
8031 match = FALSE;
8032 }
8033 if (match)
8034 return TRUE;
8035 }
8036 look = p;
8037 }
8038
8039 /*
8040 * ok, it's a boring generic character.
8041 */
8042 else
8043 {
8044 if (try_match && *look == keytyped)
8045 return TRUE;
8046 ++look;
8047 }
8048
8049 /*
8050 * Skip over ", ".
8051 */
8052 look = skip_to_option_part(look);
8053 }
8054 return FALSE;
8055}
8056#endif /* FEAT_CINDENT */
8057
8058#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8059/*
8060 * Map Hebrew keyboard when in hkmap mode.
8061 */
8062 int
8063hkmap(c)
8064 int c;
8065{
8066 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8067 {
8068 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8069 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8070 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8071 static char_u map[26] =
8072 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8073 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8074 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8075 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8076 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8077 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8078 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8079 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8080 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8081
8082 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8083 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8084 /* '-1'='sofit' */
8085 else if (c == 'x')
8086 return 'X';
8087 else if (c == 'q')
8088 return '\''; /* {geresh}={'} */
8089 else if (c == 246)
8090 return ' '; /* \"o --> ' ' for a german keyboard */
8091 else if (c == 228)
8092 return ' '; /* \"a --> ' ' -- / -- */
8093 else if (c == 252)
8094 return ' '; /* \"u --> ' ' -- / -- */
8095#ifdef EBCDIC
8096 else if (islower(c))
8097#else
8098 /* NOTE: islower() does not do the right thing for us on Linux so we
8099 * do this the same was as 5.7 and previous, so it works correctly on
8100 * all systems. Specifically, the e.g. Delete and Arrow keys are
8101 * munged and won't work if e.g. searching for Hebrew text.
8102 */
8103 else if (c >= 'a' && c <= 'z')
8104#endif
8105 return (int)(map[CharOrdLow(c)] + p_aleph);
8106 else
8107 return c;
8108 }
8109 else
8110 {
8111 switch (c)
8112 {
8113 case '`': return ';';
8114 case '/': return '.';
8115 case '\'': return ',';
8116 case 'q': return '/';
8117 case 'w': return '\'';
8118
8119 /* Hebrew letters - set offset from 'a' */
8120 case ',': c = '{'; break;
8121 case '.': c = 'v'; break;
8122 case ';': c = 't'; break;
8123 default: {
8124 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8125
8126#ifdef EBCDIC
8127 /* see note about islower() above */
8128 if (!islower(c))
8129#else
8130 if (c < 'a' || c > 'z')
8131#endif
8132 return c;
8133 c = str[CharOrdLow(c)];
8134 break;
8135 }
8136 }
8137
8138 return (int)(CharOrdLow(c) + p_aleph);
8139 }
8140}
8141#endif
8142
8143 static void
8144ins_reg()
8145{
8146 int need_redraw = FALSE;
8147 int regname;
8148 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008149 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
8151 /*
8152 * If we are going to wait for a character, show a '"'.
8153 */
8154 pc_status = PC_STATUS_UNSET;
8155 if (redrawing() && !char_avail())
8156 {
8157 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008158 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
8160 edit_putchar('"', TRUE);
8161#ifdef FEAT_CMDL_INFO
8162 add_to_showcmd_c(Ctrl_R);
8163#endif
8164 }
8165
8166#ifdef USE_ON_FLY_SCROLL
8167 dont_scroll = TRUE; /* disallow scrolling here */
8168#endif
8169
8170 /*
8171 * Don't map the register name. This also prevents the mode message to be
8172 * deleted when ESC is hit.
8173 */
8174 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008175 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8178 {
8179 /* Get a third key for literal register insertion */
8180 literally = regname;
8181#ifdef FEAT_CMDL_INFO
8182 add_to_showcmd_c(literally);
8183#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008184 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 }
8187 --no_mapping;
8188
8189#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008190 /* Don't call u_sync() while typing the expression or giving an error
8191 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 ++no_u_sync;
8193 if (regname == '=')
8194 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008195# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008197# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008198 /* Sync undo when evaluating the expression calls setline() or
8199 * append(), so that it can be undone separately. */
8200 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008203# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 /* Restore the Input Method. */
8205 if (im_on)
8206 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008209 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8210 {
8211 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 else
8215 {
8216#endif
8217 if (literally == Ctrl_O || literally == Ctrl_P)
8218 {
8219 /* Append the command to the redo buffer. */
8220 AppendCharToRedobuff(Ctrl_R);
8221 AppendCharToRedobuff(literally);
8222 AppendCharToRedobuff(regname);
8223
8224 do_put(regname, BACKWARD, 1L,
8225 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8226 }
8227 else if (insert_reg(regname, literally) == FAIL)
8228 {
8229 vim_beep();
8230 need_redraw = TRUE; /* remove the '"' */
8231 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008232 else if (stop_insert_mode)
8233 /* When the '=' register was used and a function was invoked that
8234 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8235 * insert anything, need to remove the '"' */
8236 need_redraw = TRUE;
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238#ifdef FEAT_EVAL
8239 }
8240 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008241 if (u_sync_once == 1)
8242 ins_need_undo = TRUE;
8243 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244#endif
8245#ifdef FEAT_CMDL_INFO
8246 clear_showcmd();
8247#endif
8248
8249 /* If the inserted register is empty, we need to remove the '"' */
8250 if (need_redraw || stuff_empty())
8251 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008252
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008253 /* Disallow starting Visual mode here, would get a weird mode. */
8254 if (!vis_active && VIsual_active)
8255 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
8258/*
8259 * CTRL-G commands in Insert mode.
8260 */
8261 static void
8262ins_ctrl_g()
8263{
8264 int c;
8265
8266#ifdef FEAT_INS_EXPAND
8267 /* Right after CTRL-X the cursor will be after the ruler. */
8268 setcursor();
8269#endif
8270
8271 /*
8272 * Don't map the second key. This also prevents the mode message to be
8273 * deleted when ESC is hit.
8274 */
8275 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008276 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 --no_mapping;
8278 switch (c)
8279 {
8280 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8281 case K_UP:
8282 case Ctrl_K:
8283 case 'k': ins_up(TRUE);
8284 break;
8285
8286 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8287 case K_DOWN:
8288 case Ctrl_J:
8289 case 'j': ins_down(TRUE);
8290 break;
8291
8292 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008293 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008295
8296 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008297 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008298 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008299 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 break;
8301
8302 /* Unknown CTRL-G command, reserved for future expansion. */
8303 default: vim_beep();
8304 }
8305}
8306
8307/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008308 * CTRL-^ in Insert mode.
8309 */
8310 static void
8311ins_ctrl_hat()
8312{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008313 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008314 {
8315 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8316 if (State & LANGMAP)
8317 {
8318 curbuf->b_p_iminsert = B_IMODE_NONE;
8319 State &= ~LANGMAP;
8320 }
8321 else
8322 {
8323 curbuf->b_p_iminsert = B_IMODE_LMAP;
8324 State |= LANGMAP;
8325#ifdef USE_IM_CONTROL
8326 im_set_active(FALSE);
8327#endif
8328 }
8329 }
8330#ifdef USE_IM_CONTROL
8331 else
8332 {
8333 /* There are no ":lmap" mappings, toggle IM */
8334 if (im_get_status())
8335 {
8336 curbuf->b_p_iminsert = B_IMODE_NONE;
8337 im_set_active(FALSE);
8338 }
8339 else
8340 {
8341 curbuf->b_p_iminsert = B_IMODE_IM;
8342 State &= ~LANGMAP;
8343 im_set_active(TRUE);
8344 }
8345 }
8346#endif
8347 set_iminsert_global();
8348 showmode();
8349#ifdef FEAT_GUI
8350 /* may show different cursor shape or color */
8351 if (gui.in_use)
8352 gui_update_cursor(TRUE, FALSE);
8353#endif
8354#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8355 /* Show/unshow value of 'keymap' in status lines. */
8356 status_redraw_curbuf();
8357#endif
8358}
8359
8360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 * Handle ESC in insert mode.
8362 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8363 * insert.
8364 */
8365 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008366ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 long *count;
8368 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008369 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 int temp;
8372 static int disabled_redraw = FALSE;
8373
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008374#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008375 check_spell_redraw();
8376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377#if defined(FEAT_HANGULIN)
8378# if defined(ESC_CHG_TO_ENG_MODE)
8379 hangul_input_state_set(0);
8380# endif
8381 if (composing_hangul)
8382 {
8383 push_raw_key(composing_hangul_buffer, 2);
8384 composing_hangul = 0;
8385 }
8386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
8388 temp = curwin->w_cursor.col;
8389 if (disabled_redraw)
8390 {
8391 --RedrawingDisabled;
8392 disabled_redraw = FALSE;
8393 }
8394 if (!arrow_used)
8395 {
8396 /*
8397 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008398 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8399 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 */
8401 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008402 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 /*
8405 * Repeating insert may take a long time. Check for
8406 * interrupt now and then.
8407 */
8408 if (*count > 0)
8409 {
8410 line_breakcheck();
8411 if (got_int)
8412 *count = 0;
8413 }
8414
8415 if (--*count > 0) /* repeat what was typed */
8416 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008417 /* Vi repeats the insert without replacing characters. */
8418 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8419 State &= ~REPLACE_FLAG;
8420
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 (void)start_redo_ins();
8422 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008423 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 ++RedrawingDisabled;
8425 disabled_redraw = TRUE;
8426 return FALSE; /* repeat the insert */
8427 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008428 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 undisplay_dollar();
8430 }
8431
8432 /* When an autoindent was removed, curswant stays after the
8433 * indent */
8434 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8435 curwin->w_set_curswant = TRUE;
8436
8437 /* Remember the last Insert position in the '^ mark. */
8438 if (!cmdmod.keepjumps)
8439 curbuf->b_last_insert = curwin->w_cursor;
8440
8441 /*
8442 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008443 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008445 if (!nomove
8446 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447#ifdef FEAT_VIRTUALEDIT
8448 || curwin->w_cursor.coladd > 0
8449#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008450 )
8451 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008452 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#ifdef FEAT_RIGHTLEFT
8454 && !revins_on
8455#endif
8456 )
8457 {
8458#ifdef FEAT_VIRTUALEDIT
8459 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8460 {
8461 oneleft();
8462 if (restart_edit != NUL)
8463 ++curwin->w_cursor.coladd;
8464 }
8465 else
8466#endif
8467 {
8468 --curwin->w_cursor.col;
8469#ifdef FEAT_MBYTE
8470 /* Correct cursor for multi-byte character. */
8471 if (has_mbyte)
8472 mb_adjust_cursor();
8473#endif
8474 }
8475 }
8476
8477#ifdef USE_IM_CONTROL
8478 /* Disable IM to allow typing English directly for Normal mode commands.
8479 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8480 * well). */
8481 if (!(State & LANGMAP))
8482 im_save_status(&curbuf->b_p_iminsert);
8483 im_set_active(FALSE);
8484#endif
8485
8486 State = NORMAL;
8487 /* need to position cursor again (e.g. when on a TAB ) */
8488 changed_cline_bef_curs();
8489
8490#ifdef FEAT_MOUSE
8491 setmouse();
8492#endif
8493#ifdef CURSOR_SHAPE
8494 ui_cursor_shape(); /* may show different cursor shape */
8495#endif
8496
8497 /*
8498 * When recording or for CTRL-O, need to display the new mode.
8499 * Otherwise remove the mode message.
8500 */
8501 if (Recording || restart_edit != NUL)
8502 showmode();
8503 else if (p_smd)
8504 MSG("");
8505
8506 return TRUE; /* exit Insert mode */
8507}
8508
8509#ifdef FEAT_RIGHTLEFT
8510/*
8511 * Toggle language: hkmap and revins_on.
8512 * Move to end of reverse inserted text.
8513 */
8514 static void
8515ins_ctrl_()
8516{
8517 if (revins_on && revins_chars && revins_scol >= 0)
8518 {
8519 while (gchar_cursor() != NUL && revins_chars--)
8520 ++curwin->w_cursor.col;
8521 }
8522 p_ri = !p_ri;
8523 revins_on = (State == INSERT && p_ri);
8524 if (revins_on)
8525 {
8526 revins_scol = curwin->w_cursor.col;
8527 revins_legal++;
8528 revins_chars = 0;
8529 undisplay_dollar();
8530 }
8531 else
8532 revins_scol = -1;
8533#ifdef FEAT_FKMAP
8534 if (p_altkeymap)
8535 {
8536 /*
8537 * to be consistent also for redo command, using '.'
8538 * set arrow_used to true and stop it - causing to redo
8539 * characters entered in one mode (normal/reverse insert).
8540 */
8541 arrow_used = TRUE;
8542 (void)stop_arrow();
8543 p_fkmap = curwin->w_p_rl ^ p_ri;
8544 if (p_fkmap && p_ri)
8545 State = INSERT;
8546 }
8547 else
8548#endif
8549 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8550 showmode();
8551}
8552#endif
8553
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554/*
8555 * If 'keymodel' contains "startsel", may start selection.
8556 * Returns TRUE when a CTRL-O and other keys stuffed.
8557 */
8558 static int
8559ins_start_select(c)
8560 int c;
8561{
8562 if (km_startsel)
8563 switch (c)
8564 {
8565 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 case K_PAGEUP:
8568 case K_KPAGEUP:
8569 case K_PAGEDOWN:
8570 case K_KPAGEDOWN:
8571# ifdef MACOS
8572 case K_LEFT:
8573 case K_RIGHT:
8574 case K_UP:
8575 case K_DOWN:
8576 case K_END:
8577 case K_HOME:
8578# endif
8579 if (!(mod_mask & MOD_MASK_SHIFT))
8580 break;
8581 /* FALLTHROUGH */
8582 case K_S_LEFT:
8583 case K_S_RIGHT:
8584 case K_S_UP:
8585 case K_S_DOWN:
8586 case K_S_END:
8587 case K_S_HOME:
8588 /* Start selection right away, the cursor can move with
8589 * CTRL-O when beyond the end of the line. */
8590 start_selection();
8591
8592 /* Execute the key in (insert) Select mode. */
8593 stuffcharReadbuff(Ctrl_O);
8594 if (mod_mask)
8595 {
8596 char_u buf[4];
8597
8598 buf[0] = K_SPECIAL;
8599 buf[1] = KS_MODIFIER;
8600 buf[2] = mod_mask;
8601 buf[3] = NUL;
8602 stuffReadbuff(buf);
8603 }
8604 stuffcharReadbuff(c);
8605 return TRUE;
8606 }
8607 return FALSE;
8608}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
8610/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008611 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008612 */
8613 static void
8614ins_insert(replaceState)
8615 int replaceState;
8616{
8617#ifdef FEAT_FKMAP
8618 if (p_fkmap && p_ri)
8619 {
8620 beep_flush();
8621 EMSG(farsi_text_3); /* encoded in Farsi */
8622 return;
8623 }
8624#endif
8625
8626#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008627# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008628 set_vim_var_string(VV_INSERTMODE,
8629 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008630# ifdef FEAT_VREPLACE
8631 replaceState == VREPLACE ? "v" :
8632# endif
8633 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008634# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008635 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8636#endif
8637 if (State & REPLACE_FLAG)
8638 State = INSERT | (State & LANGMAP);
8639 else
8640 State = replaceState | (State & LANGMAP);
8641 AppendCharToRedobuff(K_INS);
8642 showmode();
8643#ifdef CURSOR_SHAPE
8644 ui_cursor_shape(); /* may show different cursor shape */
8645#endif
8646}
8647
8648/*
8649 * Pressed CTRL-O in Insert mode.
8650 */
8651 static void
8652ins_ctrl_o()
8653{
8654#ifdef FEAT_VREPLACE
8655 if (State & VREPLACE_FLAG)
8656 restart_edit = 'V';
8657 else
8658#endif
8659 if (State & REPLACE_FLAG)
8660 restart_edit = 'R';
8661 else
8662 restart_edit = 'I';
8663#ifdef FEAT_VIRTUALEDIT
8664 if (virtual_active())
8665 ins_at_eol = FALSE; /* cursor always keeps its column */
8666 else
8667#endif
8668 ins_at_eol = (gchar_cursor() == NUL);
8669}
8670
8671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 * If the cursor is on an indent, ^T/^D insert/delete one
8673 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008674 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 * with vi. But vi only supports ^T and ^D after an
8676 * autoindent, we support it everywhere.
8677 */
8678 static void
8679ins_shift(c, lastc)
8680 int c;
8681 int lastc;
8682{
8683 if (stop_arrow() == FAIL)
8684 return;
8685 AppendCharToRedobuff(c);
8686
8687 /*
8688 * 0^D and ^^D: remove all indent.
8689 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008690 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8691 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
8693 --curwin->w_cursor.col;
8694 (void)del_char(FALSE); /* delete the '^' or '0' */
8695 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8696 if (State & REPLACE_FLAG)
8697 replace_pop_ins();
8698 if (lastc == '^')
8699 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008700 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 }
8702 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008703 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
8705 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8706 did_ai = FALSE;
8707#ifdef FEAT_SMARTINDENT
8708 did_si = FALSE;
8709 can_si = FALSE;
8710 can_si_back = FALSE;
8711#endif
8712#ifdef FEAT_CINDENT
8713 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8714#endif
8715}
8716
8717 static void
8718ins_del()
8719{
8720 int temp;
8721
8722 if (stop_arrow() == FAIL)
8723 return;
8724 if (gchar_cursor() == NUL) /* delete newline */
8725 {
8726 temp = curwin->w_cursor.col;
8727 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008728 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 vim_beep();
8730 else
8731 curwin->w_cursor.col = temp;
8732 }
8733 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8734 vim_beep();
8735 did_ai = FALSE;
8736#ifdef FEAT_SMARTINDENT
8737 did_si = FALSE;
8738 can_si = FALSE;
8739 can_si_back = FALSE;
8740#endif
8741 AppendCharToRedobuff(K_DEL);
8742}
8743
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008744static void ins_bs_one __ARGS((colnr_T *vcolp));
8745
8746/*
8747 * Delete one character for ins_bs().
8748 */
8749 static void
8750ins_bs_one(vcolp)
8751 colnr_T *vcolp;
8752{
8753 dec_cursor();
8754 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8755 if (State & REPLACE_FLAG)
8756 {
8757 /* Don't delete characters before the insert point when in
8758 * Replace mode */
8759 if (curwin->w_cursor.lnum != Insstart.lnum
8760 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008761 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008762 }
8763 else
8764 (void)del_char(FALSE);
8765}
8766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767/*
8768 * Handle Backspace, delete-word and delete-line in Insert mode.
8769 * Return TRUE when backspace was actually used.
8770 */
8771 static int
8772ins_bs(c, mode, inserted_space_p)
8773 int c;
8774 int mode;
8775 int *inserted_space_p;
8776{
8777 linenr_T lnum;
8778 int cc;
8779 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008780 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 colnr_T mincol;
8782 int did_backspace = FALSE;
8783 int in_indent;
8784 int oldState;
8785#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008786 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#endif
8788
8789 /*
8790 * can't delete anything in an empty file
8791 * can't backup past first character in buffer
8792 * can't backup past starting point unless 'backspace' > 1
8793 * can backup to a previous line if 'backspace' == 0
8794 */
8795 if ( bufempty()
8796 || (
8797#ifdef FEAT_RIGHTLEFT
8798 !revins_on &&
8799#endif
8800 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8801 || (!can_bs(BS_START)
8802 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008803 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8804 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8806 && curwin->w_cursor.col <= ai_col)
8807 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8808 {
8809 vim_beep();
8810 return FALSE;
8811 }
8812
8813 if (stop_arrow() == FAIL)
8814 return FALSE;
8815 in_indent = inindent(0);
8816#ifdef FEAT_CINDENT
8817 if (in_indent)
8818 can_cindent = FALSE;
8819#endif
8820#ifdef FEAT_COMMENTS
8821 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8822#endif
8823#ifdef FEAT_RIGHTLEFT
8824 if (revins_on) /* put cursor after last inserted char */
8825 inc_cursor();
8826#endif
8827
8828#ifdef FEAT_VIRTUALEDIT
8829 /* Virtualedit:
8830 * BACKSPACE_CHAR eats a virtual space
8831 * BACKSPACE_WORD eats all coladd
8832 * BACKSPACE_LINE eats all coladd and keeps going
8833 */
8834 if (curwin->w_cursor.coladd > 0)
8835 {
8836 if (mode == BACKSPACE_CHAR)
8837 {
8838 --curwin->w_cursor.coladd;
8839 return TRUE;
8840 }
8841 if (mode == BACKSPACE_WORD)
8842 {
8843 curwin->w_cursor.coladd = 0;
8844 return TRUE;
8845 }
8846 curwin->w_cursor.coladd = 0;
8847 }
8848#endif
8849
8850 /*
8851 * delete newline!
8852 */
8853 if (curwin->w_cursor.col == 0)
8854 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008855 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008856 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857#ifdef FEAT_RIGHTLEFT
8858 || revins_on
8859#endif
8860 )
8861 {
8862 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8863 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8864 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008865 --Insstart.lnum;
8866 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 }
8868 /*
8869 * In replace mode:
8870 * cc < 0: NL was inserted, delete it
8871 * cc >= 0: NL was replaced, put original characters back
8872 */
8873 cc = -1;
8874 if (State & REPLACE_FLAG)
8875 cc = replace_pop(); /* returns -1 if NL was inserted */
8876 /*
8877 * In replace mode, in the line we started replacing, we only move the
8878 * cursor.
8879 */
8880 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8881 {
8882 dec_cursor();
8883 }
8884 else
8885 {
8886#ifdef FEAT_VREPLACE
8887 if (!(State & VREPLACE_FLAG)
8888 || curwin->w_cursor.lnum > orig_line_count)
8889#endif
8890 {
8891 temp = gchar_cursor(); /* remember current char */
8892 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008893
8894 /* When "aw" is in 'formatoptions' we must delete the space at
8895 * the end of the line, otherwise the line will be broken
8896 * again when auto-formatting. */
8897 if (has_format_option(FO_AUTO)
8898 && has_format_option(FO_WHITE_PAR))
8899 {
8900 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8901 TRUE);
8902 int len;
8903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008904 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008905 if (len > 0 && ptr[len - 1] == ' ')
8906 ptr[len - 1] = NUL;
8907 }
8908
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008909 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 if (temp == NUL && gchar_cursor() != NUL)
8911 inc_cursor();
8912 }
8913#ifdef FEAT_VREPLACE
8914 else
8915 dec_cursor();
8916#endif
8917
8918 /*
8919 * In REPLACE mode we have to put back the text that was replaced
8920 * by the NL. On the replace stack is first a NUL-terminated
8921 * sequence of characters that were deleted and then the
8922 * characters that NL replaced.
8923 */
8924 if (State & REPLACE_FLAG)
8925 {
8926 /*
8927 * Do the next ins_char() in NORMAL state, to
8928 * prevent ins_char() from replacing characters and
8929 * avoiding showmatch().
8930 */
8931 oldState = State;
8932 State = NORMAL;
8933 /*
8934 * restore characters (blanks) deleted after cursor
8935 */
8936 while (cc > 0)
8937 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008938 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939#ifdef FEAT_MBYTE
8940 mb_replace_pop_ins(cc);
8941#else
8942 ins_char(cc);
8943#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008944 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 cc = replace_pop();
8946 }
8947 /* restore the characters that NL replaced */
8948 replace_pop_ins();
8949 State = oldState;
8950 }
8951 }
8952 did_ai = FALSE;
8953 }
8954 else
8955 {
8956 /*
8957 * Delete character(s) before the cursor.
8958 */
8959#ifdef FEAT_RIGHTLEFT
8960 if (revins_on) /* put cursor on last inserted char */
8961 dec_cursor();
8962#endif
8963 mincol = 0;
8964 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008965 if (mode == BACKSPACE_LINE
8966 && (curbuf->b_p_ai
8967#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008968 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008969#endif
8970 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971#ifdef FEAT_RIGHTLEFT
8972 && !revins_on
8973#endif
8974 )
8975 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008976 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008978 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008980 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 }
8982
8983 /*
8984 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8985 */
8986 if ( mode == BACKSPACE_CHAR
8987 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008988 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008989 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 && (*(ml_get_cursor() - 1) == TAB
8991 || (*(ml_get_cursor() - 1) == ' '
8992 && (!*inserted_space_p
8993 || arrow_used))))))
8994 {
8995 int ts;
8996 colnr_T vcol;
8997 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008998 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999
9000 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009001 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009002 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009004 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 /* Compute the virtual column where we want to be. Since
9006 * 'showbreak' may get in the way, need to get the last column of
9007 * the previous character. */
9008 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009009 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 dec_cursor();
9011 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9012 inc_cursor();
9013 want_vcol = (want_vcol / ts) * ts;
9014
9015 /* delete characters until we are at or before want_vcol */
9016 while (vcol > want_vcol
9017 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009018 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020 /* insert extra spaces until we are at want_vcol */
9021 while (vcol < want_vcol)
9022 {
9023 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009024 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9025 && curwin->w_cursor.col < Insstart_orig.col)
9026 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027
9028#ifdef FEAT_VREPLACE
9029 if (State & VREPLACE_FLAG)
9030 ins_char(' ');
9031 else
9032#endif
9033 {
9034 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009035 if ((State & REPLACE_FLAG))
9036 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
9038 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9039 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009040
9041 /* If we are now back where we started delete one character. Can
9042 * happen when using 'sts' and 'linebreak'. */
9043 if (vcol >= start_vcol)
9044 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
9046
9047 /*
9048 * Delete upto starting point, start of line or previous word.
9049 */
9050 else do
9051 {
9052#ifdef FEAT_RIGHTLEFT
9053 if (!revins_on) /* put cursor on char to be deleted */
9054#endif
9055 dec_cursor();
9056
9057 /* start of word? */
9058 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
9059 {
9060 mode = BACKSPACE_WORD_NOT_SPACE;
9061 temp = vim_iswordc(gchar_cursor());
9062 }
9063 /* end of word? */
9064 else if (mode == BACKSPACE_WORD_NOT_SPACE
9065 && (vim_isspace(cc = gchar_cursor())
9066 || vim_iswordc(cc) != temp))
9067 {
9068#ifdef FEAT_RIGHTLEFT
9069 if (!revins_on)
9070#endif
9071 inc_cursor();
9072#ifdef FEAT_RIGHTLEFT
9073 else if (State & REPLACE_FLAG)
9074 dec_cursor();
9075#endif
9076 break;
9077 }
9078 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009079 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else
9081 {
9082#ifdef FEAT_MBYTE
9083 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009084 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085#endif
9086 (void)del_char(FALSE);
9087#ifdef FEAT_MBYTE
9088 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009089 * If there are combining characters and 'delcombine' is set
9090 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 * character.
9092 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009093 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 inc_cursor();
9095#endif
9096#ifdef FEAT_RIGHTLEFT
9097 if (revins_chars)
9098 {
9099 revins_chars--;
9100 revins_legal++;
9101 }
9102 if (revins_on && gchar_cursor() == NUL)
9103 break;
9104#endif
9105 }
9106 /* Just a single backspace?: */
9107 if (mode == BACKSPACE_CHAR)
9108 break;
9109 } while (
9110#ifdef FEAT_RIGHTLEFT
9111 revins_on ||
9112#endif
9113 (curwin->w_cursor.col > mincol
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009114 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9115 || curwin->w_cursor.col != Insstart_orig.col)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 did_backspace = TRUE;
9117 }
9118#ifdef FEAT_SMARTINDENT
9119 did_si = FALSE;
9120 can_si = FALSE;
9121 can_si_back = FALSE;
9122#endif
9123 if (curwin->w_cursor.col <= 1)
9124 did_ai = FALSE;
9125 /*
9126 * It's a little strange to put backspaces into the redo
9127 * buffer, but it makes auto-indent a lot easier to deal
9128 * with.
9129 */
9130 AppendCharToRedobuff(c);
9131
9132 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009133 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9134 && curwin->w_cursor.col < Insstart_orig.col)
9135 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136
9137 /* vi behaviour: the cursor moves backward but the character that
9138 * was there remains visible
9139 * Vim behaviour: the cursor moves backward and the character that
9140 * was there is erased from the screen.
9141 * We can emulate the vi behaviour by pretending there is a dollar
9142 * displayed even when there isn't.
9143 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009144 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 dollar_vcol = curwin->w_virtcol;
9146
Bram Moolenaarce3be472008-01-14 19:12:28 +00009147#ifdef FEAT_FOLDING
9148 /* When deleting a char the cursor line must never be in a closed fold.
9149 * E.g., when 'foldmethod' is indent and deleting the first non-white
9150 * char before a Tab. */
9151 if (did_backspace)
9152 foldOpenCursor();
9153#endif
9154
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 return did_backspace;
9156}
9157
9158#ifdef FEAT_MOUSE
9159 static void
9160ins_mouse(c)
9161 int c;
9162{
9163 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009164 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165
9166# ifdef FEAT_GUI
9167 /* When GUI is active, also move/paste when 'mouse' is empty */
9168 if (!gui.in_use)
9169# endif
9170 if (!mouse_has(MOUSE_INSERT))
9171 return;
9172
9173 undisplay_dollar();
9174 tpos = curwin->w_cursor;
9175 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9176 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009177#ifdef FEAT_WINDOWS
9178 win_T *new_curwin = curwin;
9179
9180 if (curwin != old_curwin && win_valid(old_curwin))
9181 {
9182 /* Mouse took us to another window. We need to go back to the
9183 * previous one to stop insert there properly. */
9184 curwin = old_curwin;
9185 curbuf = curwin->w_buffer;
9186 }
9187#endif
9188 start_arrow(curwin == old_curwin ? &tpos : NULL);
9189#ifdef FEAT_WINDOWS
9190 if (curwin != new_curwin && win_valid(new_curwin))
9191 {
9192 curwin = new_curwin;
9193 curbuf = curwin->w_buffer;
9194 }
9195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196# ifdef FEAT_CINDENT
9197 can_cindent = TRUE;
9198# endif
9199 }
9200
9201#ifdef FEAT_WINDOWS
9202 /* redraw status lines (in case another window became active) */
9203 redraw_statuslines();
9204#endif
9205}
9206
9207 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009208ins_mousescroll(dir)
9209 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009212# if defined(FEAT_WINDOWS)
9213 win_T *old_curwin = curwin;
9214# endif
9215# ifdef FEAT_INS_EXPAND
9216 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217# endif
9218
9219 tpos = curwin->w_cursor;
9220
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009221# ifdef FEAT_WINDOWS
9222 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 {
9224 int row, col;
9225
9226 row = mouse_row;
9227 col = mouse_col;
9228
9229 /* find the window at the pointer coordinates */
9230 curwin = mouse_find_win(&row, &col);
9231 curbuf = curwin->w_buffer;
9232 }
9233 if (curwin == old_curwin)
9234# endif
9235 undisplay_dollar();
9236
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009237# ifdef FEAT_INS_EXPAND
9238 /* Don't scroll the window in which completion is being done. */
9239 if (!pum_visible()
9240# if defined(FEAT_WINDOWS)
9241 || curwin != old_curwin
9242# endif
9243 )
9244# endif
9245 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009246 if (dir == MSCR_DOWN || dir == MSCR_UP)
9247 {
9248 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9249 scroll_redraw(dir,
9250 (long)(curwin->w_botline - curwin->w_topline));
9251 else
9252 scroll_redraw(dir, 3L);
9253 }
9254#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009255 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009256 {
9257 int val, step = 6;
9258
9259 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9260 step = W_WIDTH(curwin);
9261 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9262 if (val < 0)
9263 val = 0;
9264 gui_do_horiz_scroll(val, TRUE);
9265 }
9266#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009267# ifdef FEAT_INS_EXPAND
9268 did_scroll = TRUE;
9269# endif
9270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009272# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 curwin->w_redr_status = TRUE;
9274
9275 curwin = old_curwin;
9276 curbuf = curwin->w_buffer;
9277# endif
9278
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009279# ifdef FEAT_INS_EXPAND
9280 /* The popup menu may overlay the window, need to redraw it.
9281 * TODO: Would be more efficient to only redraw the windows that are
9282 * overlapped by the popup menu. */
9283 if (pum_visible() && did_scroll)
9284 {
9285 redraw_all_later(NOT_VALID);
9286 ins_compl_show_pum();
9287 }
9288# endif
9289
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 if (!equalpos(curwin->w_cursor, tpos))
9291 {
9292 start_arrow(&tpos);
9293# ifdef FEAT_CINDENT
9294 can_cindent = TRUE;
9295# endif
9296 }
9297}
9298#endif
9299
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009300#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009301 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009302ins_tabline(c)
9303 int c;
9304{
9305 /* We will be leaving the current window, unless closing another tab. */
9306 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9307 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9308 {
9309 undisplay_dollar();
9310 start_arrow(&curwin->w_cursor);
9311# ifdef FEAT_CINDENT
9312 can_cindent = TRUE;
9313# endif
9314 }
9315
9316 if (c == K_TABLINE)
9317 goto_tabpage(current_tab);
9318 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009319 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009320 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009321 redraw_statuslines(); /* will redraw the tabline when needed */
9322 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009323}
9324#endif
9325
9326#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 void
9328ins_scroll()
9329{
9330 pos_T tpos;
9331
9332 undisplay_dollar();
9333 tpos = curwin->w_cursor;
9334 if (gui_do_scroll())
9335 {
9336 start_arrow(&tpos);
9337# ifdef FEAT_CINDENT
9338 can_cindent = TRUE;
9339# endif
9340 }
9341}
9342
9343 void
9344ins_horscroll()
9345{
9346 pos_T tpos;
9347
9348 undisplay_dollar();
9349 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009350 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 {
9352 start_arrow(&tpos);
9353# ifdef FEAT_CINDENT
9354 can_cindent = TRUE;
9355# endif
9356 }
9357}
9358#endif
9359
9360 static void
9361ins_left()
9362{
9363 pos_T tpos;
9364
9365#ifdef FEAT_FOLDING
9366 if ((fdo_flags & FDO_HOR) && KeyTyped)
9367 foldOpenCursor();
9368#endif
9369 undisplay_dollar();
9370 tpos = curwin->w_cursor;
9371 if (oneleft() == OK)
9372 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009373#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9374 /* Only call start_arrow() when not busy with preediting, it will
9375 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9376 if (!im_is_preediting())
9377#endif
9378 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#ifdef FEAT_RIGHTLEFT
9380 /* If exit reversed string, position is fixed */
9381 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9382 revins_legal++;
9383 revins_chars++;
9384#endif
9385 }
9386
9387 /*
9388 * if 'whichwrap' set for cursor in insert mode may go to
9389 * previous line
9390 */
9391 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9392 {
9393 start_arrow(&tpos);
9394 --(curwin->w_cursor.lnum);
9395 coladvance((colnr_T)MAXCOL);
9396 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9397 }
9398 else
9399 vim_beep();
9400}
9401
9402 static void
9403ins_home(c)
9404 int c;
9405{
9406 pos_T tpos;
9407
9408#ifdef FEAT_FOLDING
9409 if ((fdo_flags & FDO_HOR) && KeyTyped)
9410 foldOpenCursor();
9411#endif
9412 undisplay_dollar();
9413 tpos = curwin->w_cursor;
9414 if (c == K_C_HOME)
9415 curwin->w_cursor.lnum = 1;
9416 curwin->w_cursor.col = 0;
9417#ifdef FEAT_VIRTUALEDIT
9418 curwin->w_cursor.coladd = 0;
9419#endif
9420 curwin->w_curswant = 0;
9421 start_arrow(&tpos);
9422}
9423
9424 static void
9425ins_end(c)
9426 int c;
9427{
9428 pos_T tpos;
9429
9430#ifdef FEAT_FOLDING
9431 if ((fdo_flags & FDO_HOR) && KeyTyped)
9432 foldOpenCursor();
9433#endif
9434 undisplay_dollar();
9435 tpos = curwin->w_cursor;
9436 if (c == K_C_END)
9437 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9438 coladvance((colnr_T)MAXCOL);
9439 curwin->w_curswant = MAXCOL;
9440
9441 start_arrow(&tpos);
9442}
9443
9444 static void
9445ins_s_left()
9446{
9447#ifdef FEAT_FOLDING
9448 if ((fdo_flags & FDO_HOR) && KeyTyped)
9449 foldOpenCursor();
9450#endif
9451 undisplay_dollar();
9452 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9453 {
9454 start_arrow(&curwin->w_cursor);
9455 (void)bck_word(1L, FALSE, FALSE);
9456 curwin->w_set_curswant = TRUE;
9457 }
9458 else
9459 vim_beep();
9460}
9461
9462 static void
9463ins_right()
9464{
9465#ifdef FEAT_FOLDING
9466 if ((fdo_flags & FDO_HOR) && KeyTyped)
9467 foldOpenCursor();
9468#endif
9469 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009470 if (gchar_cursor() != NUL
9471#ifdef FEAT_VIRTUALEDIT
9472 || virtual_active()
9473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 )
9475 {
9476 start_arrow(&curwin->w_cursor);
9477 curwin->w_set_curswant = TRUE;
9478#ifdef FEAT_VIRTUALEDIT
9479 if (virtual_active())
9480 oneright();
9481 else
9482#endif
9483 {
9484#ifdef FEAT_MBYTE
9485 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009486 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 else
9488#endif
9489 ++curwin->w_cursor.col;
9490 }
9491
9492#ifdef FEAT_RIGHTLEFT
9493 revins_legal++;
9494 if (revins_chars)
9495 revins_chars--;
9496#endif
9497 }
9498 /* if 'whichwrap' set for cursor in insert mode, may move the
9499 * cursor to the next line */
9500 else if (vim_strchr(p_ww, ']') != NULL
9501 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9502 {
9503 start_arrow(&curwin->w_cursor);
9504 curwin->w_set_curswant = TRUE;
9505 ++curwin->w_cursor.lnum;
9506 curwin->w_cursor.col = 0;
9507 }
9508 else
9509 vim_beep();
9510}
9511
9512 static void
9513ins_s_right()
9514{
9515#ifdef FEAT_FOLDING
9516 if ((fdo_flags & FDO_HOR) && KeyTyped)
9517 foldOpenCursor();
9518#endif
9519 undisplay_dollar();
9520 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9521 || gchar_cursor() != NUL)
9522 {
9523 start_arrow(&curwin->w_cursor);
9524 (void)fwd_word(1L, FALSE, 0);
9525 curwin->w_set_curswant = TRUE;
9526 }
9527 else
9528 vim_beep();
9529}
9530
9531 static void
9532ins_up(startcol)
9533 int startcol; /* when TRUE move to Insstart.col */
9534{
9535 pos_T tpos;
9536 linenr_T old_topline = curwin->w_topline;
9537#ifdef FEAT_DIFF
9538 int old_topfill = curwin->w_topfill;
9539#endif
9540
9541 undisplay_dollar();
9542 tpos = curwin->w_cursor;
9543 if (cursor_up(1L, TRUE) == OK)
9544 {
9545 if (startcol)
9546 coladvance(getvcol_nolist(&Insstart));
9547 if (old_topline != curwin->w_topline
9548#ifdef FEAT_DIFF
9549 || old_topfill != curwin->w_topfill
9550#endif
9551 )
9552 redraw_later(VALID);
9553 start_arrow(&tpos);
9554#ifdef FEAT_CINDENT
9555 can_cindent = TRUE;
9556#endif
9557 }
9558 else
9559 vim_beep();
9560}
9561
9562 static void
9563ins_pageup()
9564{
9565 pos_T tpos;
9566
9567 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009568
9569#ifdef FEAT_WINDOWS
9570 if (mod_mask & MOD_MASK_CTRL)
9571 {
9572 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009573 if (first_tabpage->tp_next != NULL)
9574 {
9575 start_arrow(&curwin->w_cursor);
9576 goto_tabpage(-1);
9577 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009578 return;
9579 }
9580#endif
9581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 tpos = curwin->w_cursor;
9583 if (onepage(BACKWARD, 1L) == OK)
9584 {
9585 start_arrow(&tpos);
9586#ifdef FEAT_CINDENT
9587 can_cindent = TRUE;
9588#endif
9589 }
9590 else
9591 vim_beep();
9592}
9593
9594 static void
9595ins_down(startcol)
9596 int startcol; /* when TRUE move to Insstart.col */
9597{
9598 pos_T tpos;
9599 linenr_T old_topline = curwin->w_topline;
9600#ifdef FEAT_DIFF
9601 int old_topfill = curwin->w_topfill;
9602#endif
9603
9604 undisplay_dollar();
9605 tpos = curwin->w_cursor;
9606 if (cursor_down(1L, TRUE) == OK)
9607 {
9608 if (startcol)
9609 coladvance(getvcol_nolist(&Insstart));
9610 if (old_topline != curwin->w_topline
9611#ifdef FEAT_DIFF
9612 || old_topfill != curwin->w_topfill
9613#endif
9614 )
9615 redraw_later(VALID);
9616 start_arrow(&tpos);
9617#ifdef FEAT_CINDENT
9618 can_cindent = TRUE;
9619#endif
9620 }
9621 else
9622 vim_beep();
9623}
9624
9625 static void
9626ins_pagedown()
9627{
9628 pos_T tpos;
9629
9630 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009631
9632#ifdef FEAT_WINDOWS
9633 if (mod_mask & MOD_MASK_CTRL)
9634 {
9635 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009636 if (first_tabpage->tp_next != NULL)
9637 {
9638 start_arrow(&curwin->w_cursor);
9639 goto_tabpage(0);
9640 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009641 return;
9642 }
9643#endif
9644
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 tpos = curwin->w_cursor;
9646 if (onepage(FORWARD, 1L) == OK)
9647 {
9648 start_arrow(&tpos);
9649#ifdef FEAT_CINDENT
9650 can_cindent = TRUE;
9651#endif
9652 }
9653 else
9654 vim_beep();
9655}
9656
9657#ifdef FEAT_DND
9658 static void
9659ins_drop()
9660{
9661 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9662}
9663#endif
9664
9665/*
9666 * Handle TAB in Insert or Replace mode.
9667 * Return TRUE when the TAB needs to be inserted like a normal character.
9668 */
9669 static int
9670ins_tab()
9671{
9672 int ind;
9673 int i;
9674 int temp;
9675
9676 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9677 Insstart_blank_vcol = get_nolist_virtcol();
9678 if (echeck_abbr(TAB + ABBR_OFF))
9679 return FALSE;
9680
9681 ind = inindent(0);
9682#ifdef FEAT_CINDENT
9683 if (ind)
9684 can_cindent = FALSE;
9685#endif
9686
9687 /*
9688 * When nothing special, insert TAB like a normal character
9689 */
9690 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009691 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009692 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 return TRUE;
9694
9695 if (stop_arrow() == FAIL)
9696 return TRUE;
9697
9698 did_ai = FALSE;
9699#ifdef FEAT_SMARTINDENT
9700 did_si = FALSE;
9701 can_si = FALSE;
9702 can_si_back = FALSE;
9703#endif
9704 AppendToRedobuff((char_u *)"\t");
9705
9706 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009707 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009708 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9709 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 else /* otherwise use 'tabstop' */
9711 temp = (int)curbuf->b_p_ts;
9712 temp -= get_nolist_virtcol() % temp;
9713
9714 /*
9715 * Insert the first space with ins_char(). It will delete one char in
9716 * replace mode. Insert the rest with ins_str(); it will not delete any
9717 * chars. For VREPLACE mode, we use ins_char() for all characters.
9718 */
9719 ins_char(' ');
9720 while (--temp > 0)
9721 {
9722#ifdef FEAT_VREPLACE
9723 if (State & VREPLACE_FLAG)
9724 ins_char(' ');
9725 else
9726#endif
9727 {
9728 ins_str((char_u *)" ");
9729 if (State & REPLACE_FLAG) /* no char replaced */
9730 replace_push(NUL);
9731 }
9732 }
9733
9734 /*
9735 * When 'expandtab' not set: Replace spaces by TABs where possible.
9736 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009737 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 {
9739 char_u *ptr;
9740#ifdef FEAT_VREPLACE
9741 char_u *saved_line = NULL; /* init for GCC */
9742 pos_T pos;
9743#endif
9744 pos_T fpos;
9745 pos_T *cursor;
9746 colnr_T want_vcol, vcol;
9747 int change_col = -1;
9748 int save_list = curwin->w_p_list;
9749
9750 /*
9751 * Get the current line. For VREPLACE mode, don't make real changes
9752 * yet, just work on a copy of the line.
9753 */
9754#ifdef FEAT_VREPLACE
9755 if (State & VREPLACE_FLAG)
9756 {
9757 pos = curwin->w_cursor;
9758 cursor = &pos;
9759 saved_line = vim_strsave(ml_get_curline());
9760 if (saved_line == NULL)
9761 return FALSE;
9762 ptr = saved_line + pos.col;
9763 }
9764 else
9765#endif
9766 {
9767 ptr = ml_get_cursor();
9768 cursor = &curwin->w_cursor;
9769 }
9770
9771 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9772 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9773 curwin->w_p_list = FALSE;
9774
9775 /* Find first white before the cursor */
9776 fpos = curwin->w_cursor;
9777 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9778 {
9779 --fpos.col;
9780 --ptr;
9781 }
9782
9783 /* In Replace mode, don't change characters before the insert point. */
9784 if ((State & REPLACE_FLAG)
9785 && fpos.lnum == Insstart.lnum
9786 && fpos.col < Insstart.col)
9787 {
9788 ptr += Insstart.col - fpos.col;
9789 fpos.col = Insstart.col;
9790 }
9791
9792 /* compute virtual column numbers of first white and cursor */
9793 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9794 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9795
Bram Moolenaar597a4222014-06-25 14:39:50 +02009796 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9797 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 while (vim_iswhite(*ptr))
9799 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009800 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 if (vcol + i > want_vcol)
9802 break;
9803 if (*ptr != TAB)
9804 {
9805 *ptr = TAB;
9806 if (change_col < 0)
9807 {
9808 change_col = fpos.col; /* Column of first change */
9809 /* May have to adjust Insstart */
9810 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9811 Insstart.col = fpos.col;
9812 }
9813 }
9814 ++fpos.col;
9815 ++ptr;
9816 vcol += i;
9817 }
9818
9819 if (change_col >= 0)
9820 {
9821 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009822 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823
9824 /* Skip over the spaces we need. */
9825 while (vcol < want_vcol && *ptr == ' ')
9826 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009827 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 ++ptr;
9829 ++repl_off;
9830 }
9831 if (vcol > want_vcol)
9832 {
9833 /* Must have a char with 'showbreak' just before it. */
9834 --ptr;
9835 --repl_off;
9836 }
9837 fpos.col += repl_off;
9838
9839 /* Delete following spaces. */
9840 i = cursor->col - fpos.col;
9841 if (i > 0)
9842 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009843 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 /* correct replace stack. */
9845 if ((State & REPLACE_FLAG)
9846#ifdef FEAT_VREPLACE
9847 && !(State & VREPLACE_FLAG)
9848#endif
9849 )
9850 for (temp = i; --temp >= 0; )
9851 replace_join(repl_off);
9852 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009853#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009854 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009855 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009856 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009857 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9858 (char_u *)"\t", 1);
9859 }
9860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 cursor->col -= i;
9862
9863#ifdef FEAT_VREPLACE
9864 /*
9865 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9866 * backspacing over the changed spacing and then inserting the new
9867 * spacing.
9868 */
9869 if (State & VREPLACE_FLAG)
9870 {
9871 /* Backspace from real cursor to change_col */
9872 backspace_until_column(change_col);
9873
9874 /* Insert each char in saved_line from changed_col to
9875 * ptr-cursor */
9876 ins_bytes_len(saved_line + change_col,
9877 cursor->col - change_col);
9878 }
9879#endif
9880 }
9881
9882#ifdef FEAT_VREPLACE
9883 if (State & VREPLACE_FLAG)
9884 vim_free(saved_line);
9885#endif
9886 curwin->w_p_list = save_list;
9887 }
9888
9889 return FALSE;
9890}
9891
9892/*
9893 * Handle CR or NL in insert mode.
9894 * Return TRUE when out of memory or can't undo.
9895 */
9896 static int
9897ins_eol(c)
9898 int c;
9899{
9900 int i;
9901
9902 if (echeck_abbr(c + ABBR_OFF))
9903 return FALSE;
9904 if (stop_arrow() == FAIL)
9905 return TRUE;
9906 undisplay_dollar();
9907
9908 /*
9909 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9910 * character under the cursor. Only push a NUL on the replace stack,
9911 * nothing to put back when the NL is deleted.
9912 */
9913 if ((State & REPLACE_FLAG)
9914#ifdef FEAT_VREPLACE
9915 && !(State & VREPLACE_FLAG)
9916#endif
9917 )
9918 replace_push(NUL);
9919
9920 /*
9921 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9922 * replacing the next line, so we push all of the characters left on the
9923 * line onto the replace stack. This is not done here though, it is done
9924 * in open_line().
9925 */
9926
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009927#ifdef FEAT_VIRTUALEDIT
9928 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9929 * CTRL-O). */
9930 if (virtual_active() && curwin->w_cursor.coladd > 0)
9931 coladvance(getviscol());
9932#endif
9933
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934#ifdef FEAT_RIGHTLEFT
9935# ifdef FEAT_FKMAP
9936 if (p_altkeymap && p_fkmap)
9937 fkmap(NL);
9938# endif
9939 /* NL in reverse insert will always start in the end of
9940 * current line. */
9941 if (revins_on)
9942 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9943#endif
9944
9945 AppendToRedobuff(NL_STR);
9946 i = open_line(FORWARD,
9947#ifdef FEAT_COMMENTS
9948 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9949#endif
9950 0, old_indent);
9951 old_indent = 0;
9952#ifdef FEAT_CINDENT
9953 can_cindent = TRUE;
9954#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009955#ifdef FEAT_FOLDING
9956 /* When inserting a line the cursor line must never be in a closed fold. */
9957 foldOpenCursor();
9958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
9960 return (!i);
9961}
9962
9963#ifdef FEAT_DIGRAPHS
9964/*
9965 * Handle digraph in insert mode.
9966 * Returns character still to be inserted, or NUL when nothing remaining to be
9967 * done.
9968 */
9969 static int
9970ins_digraph()
9971{
9972 int c;
9973 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009974 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975
9976 pc_status = PC_STATUS_UNSET;
9977 if (redrawing() && !char_avail())
9978 {
9979 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009980 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981
9982 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009983 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984#ifdef FEAT_CMDL_INFO
9985 add_to_showcmd_c(Ctrl_K);
9986#endif
9987 }
9988
9989#ifdef USE_ON_FLY_SCROLL
9990 dont_scroll = TRUE; /* disallow scrolling here */
9991#endif
9992
9993 /* don't map the digraph chars. This also prevents the
9994 * mode message to be deleted when ESC is hit */
9995 ++no_mapping;
9996 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009997 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 --no_mapping;
9999 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010000 if (did_putchar)
10001 /* when the line fits in 'columns' the '?' is at the start of the next
10002 * line and will not be removed by the redraw */
10003 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010004
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 if (IS_SPECIAL(c) || mod_mask) /* special key */
10006 {
10007#ifdef FEAT_CMDL_INFO
10008 clear_showcmd();
10009#endif
10010 insert_special(c, TRUE, FALSE);
10011 return NUL;
10012 }
10013 if (c != ESC)
10014 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010015 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if (redrawing() && !char_avail())
10017 {
10018 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010019 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
10021 if (char2cells(c) == 1)
10022 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010023 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010025 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 }
10027#ifdef FEAT_CMDL_INFO
10028 add_to_showcmd_c(c);
10029#endif
10030 }
10031 ++no_mapping;
10032 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010033 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 --no_mapping;
10035 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010036 if (did_putchar)
10037 /* when the line fits in 'columns' the '?' is at the start of the
10038 * next line and will not be removed by a redraw */
10039 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (cc != ESC)
10041 {
10042 AppendToRedobuff((char_u *)CTRL_V_STR);
10043 c = getdigraph(c, cc, TRUE);
10044#ifdef FEAT_CMDL_INFO
10045 clear_showcmd();
10046#endif
10047 return c;
10048 }
10049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050#ifdef FEAT_CMDL_INFO
10051 clear_showcmd();
10052#endif
10053 return NUL;
10054}
10055#endif
10056
10057/*
10058 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10059 * Returns the char to be inserted, or NUL if none found.
10060 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010061 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062ins_copychar(lnum)
10063 linenr_T lnum;
10064{
10065 int c;
10066 int temp;
10067 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010068 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069
10070 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10071 {
10072 vim_beep();
10073 return NUL;
10074 }
10075
10076 /* try to advance to the cursor column */
10077 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010078 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 prev_ptr = ptr;
10080 validate_virtcol();
10081 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10082 {
10083 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010084 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 }
10086 if ((colnr_T)temp > curwin->w_virtcol)
10087 ptr = prev_ptr;
10088
10089#ifdef FEAT_MBYTE
10090 c = (*mb_ptr2char)(ptr);
10091#else
10092 c = *ptr;
10093#endif
10094 if (c == NUL)
10095 vim_beep();
10096 return c;
10097}
10098
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010099/*
10100 * CTRL-Y or CTRL-E typed in Insert mode.
10101 */
10102 static int
10103ins_ctrl_ey(tc)
10104 int tc;
10105{
10106 int c = tc;
10107
10108#ifdef FEAT_INS_EXPAND
10109 if (ctrl_x_mode == CTRL_X_SCROLL)
10110 {
10111 if (c == Ctrl_Y)
10112 scrolldown_clamp();
10113 else
10114 scrollup_clamp();
10115 redraw_later(VALID);
10116 }
10117 else
10118#endif
10119 {
10120 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10121 if (c != NUL)
10122 {
10123 long tw_save;
10124
10125 /* The character must be taken literally, insert like it
10126 * was typed after a CTRL-V, and pretend 'textwidth'
10127 * wasn't set. Digits, 'o' and 'x' are special after a
10128 * CTRL-V, don't use it for these. */
10129 if (c < 256 && !isalnum(c))
10130 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10131 tw_save = curbuf->b_p_tw;
10132 curbuf->b_p_tw = -1;
10133 insert_special(c, TRUE, FALSE);
10134 curbuf->b_p_tw = tw_save;
10135#ifdef FEAT_RIGHTLEFT
10136 revins_chars++;
10137 revins_legal++;
10138#endif
10139 c = Ctrl_V; /* pretend CTRL-V is last character */
10140 auto_format(FALSE, TRUE);
10141 }
10142 }
10143 return c;
10144}
10145
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146#ifdef FEAT_SMARTINDENT
10147/*
10148 * Try to do some very smart auto-indenting.
10149 * Used when inserting a "normal" character.
10150 */
10151 static void
10152ins_try_si(c)
10153 int c;
10154{
10155 pos_T *pos, old_pos;
10156 char_u *ptr;
10157 int i;
10158 int temp;
10159
10160 /*
10161 * do some very smart indenting when entering '{' or '}'
10162 */
10163 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10164 {
10165 /*
10166 * for '}' set indent equal to indent of line containing matching '{'
10167 */
10168 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10169 {
10170 old_pos = curwin->w_cursor;
10171 /*
10172 * If the matching '{' has a ')' immediately before it (ignoring
10173 * white-space), then line up with the start of the line
10174 * containing the matching '(' if there is one. This handles the
10175 * case where an "if (..\n..) {" statement continues over multiple
10176 * lines -- webb
10177 */
10178 ptr = ml_get(pos->lnum);
10179 i = pos->col;
10180 if (i > 0) /* skip blanks before '{' */
10181 while (--i > 0 && vim_iswhite(ptr[i]))
10182 ;
10183 curwin->w_cursor.lnum = pos->lnum;
10184 curwin->w_cursor.col = i;
10185 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10186 curwin->w_cursor = *pos;
10187 i = get_indent();
10188 curwin->w_cursor = old_pos;
10189#ifdef FEAT_VREPLACE
10190 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010191 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 else
10193#endif
10194 (void)set_indent(i, SIN_CHANGED);
10195 }
10196 else if (curwin->w_cursor.col > 0)
10197 {
10198 /*
10199 * when inserting '{' after "O" reduce indent, but not
10200 * more than indent of previous line
10201 */
10202 temp = TRUE;
10203 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10204 {
10205 old_pos = curwin->w_cursor;
10206 i = get_indent();
10207 while (curwin->w_cursor.lnum > 1)
10208 {
10209 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10210
10211 /* ignore empty lines and lines starting with '#'. */
10212 if (*ptr != '#' && *ptr != NUL)
10213 break;
10214 }
10215 if (get_indent() >= i)
10216 temp = FALSE;
10217 curwin->w_cursor = old_pos;
10218 }
10219 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010220 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 }
10222 }
10223
10224 /*
10225 * set indent of '#' always to 0
10226 */
10227 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10228 {
10229 /* remember current indent for next line */
10230 old_indent = get_indent();
10231 (void)set_indent(0, SIN_CHANGED);
10232 }
10233
10234 /* Adjust ai_col, the char at this position can be deleted. */
10235 if (ai_col > curwin->w_cursor.col)
10236 ai_col = curwin->w_cursor.col;
10237}
10238#endif
10239
10240/*
10241 * Get the value that w_virtcol would have when 'list' is off.
10242 * Unless 'cpo' contains the 'L' flag.
10243 */
10244 static colnr_T
10245get_nolist_virtcol()
10246{
10247 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10248 return getvcol_nolist(&curwin->w_cursor);
10249 validate_virtcol();
10250 return curwin->w_virtcol;
10251}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010252
10253#ifdef FEAT_AUTOCMD
10254/*
10255 * Handle the InsertCharPre autocommand.
10256 * "c" is the character that was typed.
10257 * Return a pointer to allocated memory with the replacement string.
10258 * Return NULL to continue inserting "c".
10259 */
10260 static char_u *
10261do_insert_char_pre(c)
10262 int c;
10263{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010264 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010265 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010266
10267 /* Return quickly when there is nothing to do. */
10268 if (!has_insertcharpre())
10269 return NULL;
10270
Bram Moolenaar704984a2012-06-01 14:57:51 +020010271#ifdef FEAT_MBYTE
10272 if (has_mbyte)
10273 buf[(*mb_char2bytes)(c, buf)] = NUL;
10274 else
10275#endif
10276 {
10277 buf[0] = c;
10278 buf[1] = NUL;
10279 }
10280
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010281 /* Lock the text to avoid weird things from happening. */
10282 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010283 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010284
Bram Moolenaar704984a2012-06-01 14:57:51 +020010285 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010286 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010287 {
10288 /* Get the value of v:char. It may be empty or more than one
10289 * character. Only use it when changed, otherwise continue with the
10290 * original character to avoid breaking autoindent. */
10291 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10292 res = vim_strsave(get_vim_var_str(VV_CHAR));
10293 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010294
10295 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10296 --textlock;
10297
10298 return res;
10299}
10300#endif