blob: a43c0a2c16b5c7b0719a70ad6e8d0c8c4b00534c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010061#ifdef FEAT_COMPL_FUNC
62static char e_complwin[] = N_("E839: Completion function changed window");
63static char e_compldel[] = N_("E840: Completion function deleted text");
64#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to store one match for insert completion.
68 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000069typedef struct compl_S compl_T;
70struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar572cb562005-08-05 21:35:02 +000072 compl_T *cp_next;
73 compl_T *cp_prev;
74 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000075 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000076 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000121/* Set when doing something for completion that may call edit() recursively,
122 * which is not allowed. */
123static int compl_busy = FALSE;
124
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static int compl_matches = 0;
126static char_u *compl_pattern = NULL;
127static int compl_direction = FORWARD;
128static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000129static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000130static pos_T compl_startpos;
131static colnr_T compl_col = 0; /* column where the text starts
132 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static char_u *compl_orig_text = NULL; /* text as it was before
134 * completion started */
135static int compl_cont_mode = 0;
136static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000138static void ins_ctrl_x __ARGS((void));
139static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000140static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000141static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000142static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000143static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000144static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000146static void ins_compl_upd_pum __ARGS((void));
147static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000148static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000149static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000150static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000151static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000152static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void ins_compl_free __ARGS((void));
154static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000155static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000156static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000157static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000158static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000159static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000160static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000161static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000162static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000164#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
165static void ins_compl_add_list __ARGS((list_T *list));
166#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000167static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168static void ins_compl_delete __ARGS((void));
169static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000170static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000171static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000173static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000174static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000176static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#endif /* FEAT_INS_EXPAND */
178
179#define BACKSPACE_CHAR 1
180#define BACKSPACE_WORD 2
181#define BACKSPACE_WORD_NOT_SPACE 3
182#define BACKSPACE_LINE 4
183
Bram Moolenaar754b5602006-02-09 23:53:20 +0000184static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void ins_ctrl_v __ARGS((void));
186static void undisplay_dollar __ARGS((void));
187static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000188static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189static void check_auto_format __ARGS((int));
190static void redo_literal __ARGS((int c));
191static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000192#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000193static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000194static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000195static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
198static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199static int replace_pop __ARGS((void));
200static void replace_join __ARGS((int off));
201static void replace_pop_ins __ARGS((void));
202#ifdef FEAT_MBYTE
203static void mb_replace_pop_ins __ARGS((int cc));
204#endif
205static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000206static void replace_do_bs __ARGS((int limit_col));
207static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#ifdef FEAT_CINDENT
209static int cindent_on __ARGS((void));
210#endif
211static void ins_reg __ARGS((void));
212static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000214static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifdef FEAT_RIGHTLEFT
216static void ins_ctrl_ __ARGS((void));
217#endif
218#ifdef FEAT_VISUAL
219static int ins_start_select __ARGS((int c));
220#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000221static void ins_insert __ARGS((int replaceState));
222static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static void ins_shift __ARGS((int c, int lastc));
224static void ins_del __ARGS((void));
225static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
226#ifdef FEAT_MOUSE
227static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200228static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000230#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
231static void ins_tabline __ARGS((int c));
232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233static void ins_left __ARGS((void));
234static void ins_home __ARGS((int c));
235static void ins_end __ARGS((int c));
236static void ins_s_left __ARGS((void));
237static void ins_right __ARGS((void));
238static void ins_s_right __ARGS((void));
239static void ins_up __ARGS((int startcol));
240static void ins_pageup __ARGS((void));
241static void ins_down __ARGS((int startcol));
242static void ins_pagedown __ARGS((void));
243#ifdef FEAT_DND
244static void ins_drop __ARGS((void));
245#endif
246static int ins_tab __ARGS((void));
247static int ins_eol __ARGS((int c));
248#ifdef FEAT_DIGRAPHS
249static int ins_digraph __ARGS((void));
250#endif
251static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000252static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_SMARTINDENT
254static void ins_try_si __ARGS((int c));
255#endif
256static colnr_T get_nolist_virtcol __ARGS((void));
257
258static colnr_T Insstart_textlen; /* length of line when insert started */
259static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
260
261static char_u *last_insert = NULL; /* the text of the previous insert,
262 K_SPECIAL and CSI are escaped */
263static int last_insert_skip; /* nr of chars in front of previous insert */
264static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000265static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267#ifdef FEAT_CINDENT
268static int can_cindent; /* may do cindenting on this line */
269#endif
270
271static int old_indent = 0; /* for ^^D command in insert mode */
272
273#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000274static int revins_on; /* reverse insert mode on */
275static int revins_chars; /* how much to skip after edit */
276static int revins_legal; /* was the last char 'legal'? */
277static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#endif
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280static int ins_need_undo; /* call u_save() before inserting a
281 char. Set when edit() is called.
282 after that arrow_used is used. */
283
284static int did_add_space = FALSE; /* auto_format() added an extra space
285 under the cursor */
286
287/*
288 * edit(): Start inserting text.
289 *
290 * "cmdchar" can be:
291 * 'i' normal insert command
292 * 'a' normal append command
293 * 'R' replace command
294 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
295 * but still only one <CR> is inserted. The <Esc> is not used for redo.
296 * 'g' "gI" command.
297 * 'V' "gR" command for Virtual Replace mode.
298 * 'v' "gr" command for single character Virtual Replace mode.
299 *
300 * This function is not called recursively. For CTRL-O commands, it returns
301 * and lets the caller handle the Normal-mode command.
302 *
303 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
304 */
305 int
306edit(cmdchar, startln, count)
307 int cmdchar;
308 int startln; /* if set, insert at start of line */
309 long count;
310{
311 int c = 0;
312 char_u *ptr;
313 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000314 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 int i;
317 int did_backspace = TRUE; /* previous char was backspace */
318#ifdef FEAT_CINDENT
319 int line_is_white = FALSE; /* line is empty before insert */
320#endif
321 linenr_T old_topline = 0; /* topline before insertion */
322#ifdef FEAT_DIFF
323 int old_topfill = -1;
324#endif
325 int inserted_space = FALSE; /* just inserted a space */
326 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000327 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000329 /* Remember whether editing was restarted after CTRL-O. */
330 did_restart_edit = restart_edit;
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 /* sleep before redrawing, needed for "CTRL-O :" that results in an
333 * error message */
334 check_for_delay(TRUE);
335
336#ifdef HAVE_SANDBOX
337 /* Don't allow inserting in the sandbox. */
338 if (sandbox != 0)
339 {
340 EMSG(_(e_sandbox));
341 return FALSE;
342 }
343#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000344 /* Don't allow changes in the buffer while editing the cmdline. The
345 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000346 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000353 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000354 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000355 {
356 EMSG(_(e_secure));
357 return FALSE;
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 ins_compl_clear(); /* clear stuff for CTRL-X mode */
360#endif
361
Bram Moolenaar843ee412004-06-30 16:16:41 +0000362#ifdef FEAT_AUTOCMD
363 /*
364 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
365 */
366 if (cmdchar != 'r' && cmdchar != 'v')
367 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 if (cmdchar == 'R')
370 ptr = (char_u *)"r";
371 else if (cmdchar == 'V')
372 ptr = (char_u *)"v";
373 else
374 ptr = (char_u *)"i";
375 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000376# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000377 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
378 }
379#endif
380
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200381#ifdef FEAT_CONCEAL
382 /* Check if the cursor line needs redrawing before changing State. If
383 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200384 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200385#endif
386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_MOUSE
388 /*
389 * When doing a paste with the middle mouse button, Insstart is set to
390 * where the paste started.
391 */
392 if (where_paste_started.lnum != 0)
393 Insstart = where_paste_started;
394 else
395#endif
396 {
397 Insstart = curwin->w_cursor;
398 if (startln)
399 Insstart.col = 0;
400 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000401 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 Insstart_blank_vcol = MAXCOL;
403 if (!did_ai)
404 ai_col = 0;
405
406 if (cmdchar != NUL && restart_edit == 0)
407 {
408 ResetRedobuff();
409 AppendNumberToRedobuff(count);
410#ifdef FEAT_VREPLACE
411 if (cmdchar == 'V' || cmdchar == 'v')
412 {
413 /* "gR" or "gr" command */
414 AppendCharToRedobuff('g');
415 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
416 }
417 else
418#endif
419 {
420 AppendCharToRedobuff(cmdchar);
421 if (cmdchar == 'g') /* "gI" command */
422 AppendCharToRedobuff('I');
423 else if (cmdchar == 'r') /* "r<CR>" command */
424 count = 1; /* insert only one <CR> */
425 }
426 }
427
428 if (cmdchar == 'R')
429 {
430#ifdef FEAT_FKMAP
431 if (p_fkmap && p_ri)
432 {
433 beep_flush();
434 EMSG(farsi_text_3); /* encoded in Farsi */
435 State = INSERT;
436 }
437 else
438#endif
439 State = REPLACE;
440 }
441#ifdef FEAT_VREPLACE
442 else if (cmdchar == 'V' || cmdchar == 'v')
443 {
444 State = VREPLACE;
445 replaceState = VREPLACE;
446 orig_line_count = curbuf->b_ml.ml_line_count;
447 vr_lines_changed = 1;
448 }
449#endif
450 else
451 State = INSERT;
452
453 stop_insert_mode = FALSE;
454
455 /*
456 * Need to recompute the cursor position, it might move when the cursor is
457 * on a TAB or special character.
458 */
459 curs_columns(TRUE);
460
461 /*
462 * Enable langmap or IME, indicated by 'iminsert'.
463 * Note that IME may enabled/disabled without us noticing here, thus the
464 * 'iminsert' value may not reflect what is actually used. It is updated
465 * when hitting <Esc>.
466 */
467 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
468 State |= LANGMAP;
469#ifdef USE_IM_CONTROL
470 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
471#endif
472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_MOUSE
474 setmouse();
475#endif
476#ifdef FEAT_CMDL_INFO
477 clear_showcmd();
478#endif
479#ifdef FEAT_RIGHTLEFT
480 /* there is no reverse replace mode */
481 revins_on = (State == INSERT && p_ri);
482 if (revins_on)
483 undisplay_dollar();
484 revins_chars = 0;
485 revins_legal = 0;
486 revins_scol = -1;
487#endif
488
489 /*
490 * Handle restarting Insert mode.
491 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
492 * restart_edit non-zero, and something in the stuff buffer.
493 */
494 if (restart_edit != 0 && stuff_empty())
495 {
496#ifdef FEAT_MOUSE
497 /*
498 * After a paste we consider text typed to be part of the insert for
499 * the pasted text. You can backspace over the pasted text too.
500 */
501 if (where_paste_started.lnum)
502 arrow_used = FALSE;
503 else
504#endif
505 arrow_used = TRUE;
506 restart_edit = 0;
507
508 /*
509 * If the cursor was after the end-of-line before the CTRL-O and it is
510 * now at the end-of-line, put it after the end-of-line (this is not
511 * correct in very rare cases).
512 * Also do this if curswant is greater than the current virtual
513 * column. Eg after "^O$" or "^O80|".
514 */
515 validate_virtcol();
516 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000517 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 || curwin->w_curswant > curwin->w_virtcol)
519 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
520 {
521 if (ptr[1] == NUL)
522 ++curwin->w_cursor.col;
523#ifdef FEAT_MBYTE
524 else if (has_mbyte)
525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000526 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (ptr[i] == NUL)
528 curwin->w_cursor.col += i;
529 }
530#endif
531 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000532 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
534 else
535 arrow_used = FALSE;
536
537 /* we are in insert mode now, don't need to start it anymore */
538 need_start_insertmode = FALSE;
539
540 /* Need to save the line for undo before inserting the first char. */
541 ins_need_undo = TRUE;
542
543#ifdef FEAT_MOUSE
544 where_paste_started.lnum = 0;
545#endif
546#ifdef FEAT_CINDENT
547 can_cindent = TRUE;
548#endif
549#ifdef FEAT_FOLDING
550 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
551 * restarting. */
552 if (!p_im && did_restart_edit == 0)
553 foldOpenCursor();
554#endif
555
556 /*
557 * If 'showmode' is set, show the current (insert/replace/..) mode.
558 * A warning message for changing a readonly file is given here, before
559 * actually changing anything. It's put after the mode, if any.
560 */
561 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000562 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 i = showmode();
564
565 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000566 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568#ifdef CURSOR_SHAPE
569 ui_cursor_shape(); /* may show different cursor shape */
570#endif
571#ifdef FEAT_DIGRAPHS
572 do_digraph(-1); /* clear digraphs */
573#endif
574
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000575 /*
576 * Get the current length of the redo buffer, those characters have to be
577 * skipped if we want to get to the inserted characters.
578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 ptr = get_inserted();
580 if (ptr == NULL)
581 new_insert_skip = 0;
582 else
583 {
584 new_insert_skip = (int)STRLEN(ptr);
585 vim_free(ptr);
586 }
587
588 old_indent = 0;
589
590 /*
591 * Main loop in Insert mode: repeat until Insert mode is left.
592 */
593 for (;;)
594 {
595#ifdef FEAT_RIGHTLEFT
596 if (!revins_legal)
597 revins_scol = -1; /* reset on illegal motions */
598 else
599 revins_legal = 0;
600#endif
601 if (arrow_used) /* don't repeat insert when arrow key used */
602 count = 0;
603
604 if (stop_insert_mode)
605 {
606 /* ":stopinsert" used or 'insertmode' reset */
607 count = 0;
608 goto doESCkey;
609 }
610
611 /* set curwin->w_curswant for next K_DOWN or K_UP */
612 if (!arrow_used)
613 curwin->w_set_curswant = TRUE;
614
615 /* If there is no typeahead may check for timestamps (e.g., for when a
616 * menu invoked a shell command). */
617 if (stuff_empty())
618 {
619 did_check_timestamps = FALSE;
620 if (need_check_timestamps)
621 check_timestamps(FALSE);
622 }
623
624 /*
625 * When emsg() was called msg_scroll will have been set.
626 */
627 msg_scroll = FALSE;
628
629#ifdef FEAT_GUI
630 /* When 'mousefocus' is set a mouse movement may have taken us to
631 * another window. "need_mouse_correct" may then be set because of an
632 * autocommand. */
633 if (need_mouse_correct)
634 gui_mouse_correct();
635#endif
636
637#ifdef FEAT_FOLDING
638 /* Open fold at the cursor line, according to 'foldopen'. */
639 if (fdo_flags & FDO_INSERT)
640 foldOpenCursor();
641 /* Close folds where the cursor isn't, according to 'foldclose' */
642 if (!char_avail())
643 foldCheckClose();
644#endif
645
646 /*
647 * If we inserted a character at the last position of the last line in
648 * the window, scroll the window one line up. This avoids an extra
649 * redraw.
650 * This is detected when the cursor column is smaller after inserting
651 * something.
652 * Don't do this when the topline changed already, it has
653 * already been adjusted (by insertchar() calling open_line())).
654 */
655 if (curbuf->b_mod_set
656 && curwin->w_p_wrap
657 && !did_backspace
658 && curwin->w_topline == old_topline
659#ifdef FEAT_DIFF
660 && curwin->w_topfill == old_topfill
661#endif
662 )
663 {
664 mincol = curwin->w_wcol;
665 validate_cursor_col();
666
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000667 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 && curwin->w_wrow == W_WINROW(curwin)
669 + curwin->w_height - 1 - p_so
670 && (curwin->w_cursor.lnum != curwin->w_topline
671#ifdef FEAT_DIFF
672 || curwin->w_topfill > 0
673#endif
674 ))
675 {
676#ifdef FEAT_DIFF
677 if (curwin->w_topfill > 0)
678 --curwin->w_topfill;
679 else
680#endif
681#ifdef FEAT_FOLDING
682 if (hasFolding(curwin->w_topline, NULL, &old_topline))
683 set_topline(curwin, old_topline + 1);
684 else
685#endif
686 set_topline(curwin, curwin->w_topline + 1);
687 }
688 }
689
690 /* May need to adjust w_topline to show the cursor. */
691 update_topline();
692
693 did_backspace = FALSE;
694
695 validate_cursor(); /* may set must_redraw */
696
697 /*
698 * Redraw the display when no characters are waiting.
699 * Also shows mode, ruler and positions cursor.
700 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000701 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703#ifdef FEAT_SCROLLBIND
704 if (curwin->w_p_scb)
705 do_check_scrollbind(TRUE);
706#endif
707
Bram Moolenaar860cae12010-06-05 23:22:07 +0200708#ifdef FEAT_CURSORBIND
709 if (curwin->w_p_crb)
710 do_check_cursorbind();
711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 update_curswant();
713 old_topline = curwin->w_topline;
714#ifdef FEAT_DIFF
715 old_topfill = curwin->w_topfill;
716#endif
717
718#ifdef USE_ON_FLY_SCROLL
719 dont_scroll = FALSE; /* allow scrolling here */
720#endif
721
722 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000723 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
725 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000726 do
727 {
728 c = safe_vgetc();
729 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000731#ifdef FEAT_AUTOCMD
732 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
733 did_cursorhold = TRUE;
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef FEAT_RIGHTLEFT
737 if (p_hkmap && KeyTyped)
738 c = hkmap(c); /* Hebrew mode mapping */
739#endif
740#ifdef FEAT_FKMAP
741 if (p_fkmap && KeyTyped)
742 c = fkmap(c); /* Farsi mode mapping */
743#endif
744
745#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /*
747 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000748 * and the cursor is still in the completed word. Only when there is
749 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000750 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000751 if (compl_started
752 && pum_wanted()
753 && curwin->w_cursor.col >= compl_col
754 && (compl_shown_match == NULL
755 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000756 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000757 /* BS: Delete one character from "compl_leader". */
758 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000759 && curwin->w_cursor.col > compl_col
760 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000761 continue;
762
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000763 /* When no match was selected or it was edited. */
764 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000765 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000766 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000767 * "compl_leader". Except when at the original match and
768 * there is nothing to add, CTRL-L works like CTRL-P then. */
769 if (c == Ctrl_L
770 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000771 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000772 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000773 {
774 ins_compl_addfrommatch();
775 continue;
776 }
777
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000778 /* A non-white character that fits in with the current
779 * completion: Add to "compl_leader". */
780 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000781 {
782 ins_compl_addleader(c);
783 continue;
784 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000785
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000786 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000787 * compl_enter_selects is set the Enter key does the same. */
788 if (c == Ctrl_Y || (compl_enter_selects
789 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000790 {
791 ins_compl_delete();
792 ins_compl_insert();
793 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000794 }
795 }
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
798 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000799 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000800 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000801 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#endif
803
Bram Moolenaar488c6512005-08-11 20:09:58 +0000804 /* CTRL-\ CTRL-N goes to Normal mode,
805 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
806 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (c == Ctrl_BSL)
808 {
809 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000810 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 ++no_mapping;
812 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000813 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 --no_mapping;
815 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000816 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000818 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 vungetc(c);
820 c = Ctrl_BSL;
821 }
822 else if (c == Ctrl_G && p_im)
823 continue;
824 else
825 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000826 if (c == Ctrl_O)
827 {
828 ins_ctrl_o();
829 ins_at_eol = FALSE; /* cursor keeps its column */
830 nomove = TRUE;
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 count = 0;
833 goto doESCkey;
834 }
835 }
836
837#ifdef FEAT_DIGRAPHS
838 c = do_digraph(c);
839#endif
840
841#ifdef FEAT_INS_EXPAND
842 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
843 goto docomplete;
844#endif
845 if (c == Ctrl_V || c == Ctrl_Q)
846 {
847 ins_ctrl_v();
848 c = Ctrl_V; /* pretend CTRL-V is last typed character */
849 continue;
850 }
851
852#ifdef FEAT_CINDENT
853 if (cindent_on()
854# ifdef FEAT_INS_EXPAND
855 && ctrl_x_mode == 0
856# endif
857 )
858 {
859 /* A key name preceded by a bang means this key is not to be
860 * inserted. Skip ahead to the re-indenting below.
861 * A key name preceded by a star means that indenting has to be
862 * done before inserting the key. */
863 line_is_white = inindent(0);
864 if (in_cinkeys(c, '!', line_is_white))
865 goto force_cindent;
866 if (can_cindent && in_cinkeys(c, '*', line_is_white)
867 && stop_arrow() == OK)
868 do_c_expr_indent();
869 }
870#endif
871
872#ifdef FEAT_RIGHTLEFT
873 if (curwin->w_p_rl)
874 switch (c)
875 {
876 case K_LEFT: c = K_RIGHT; break;
877 case K_S_LEFT: c = K_S_RIGHT; break;
878 case K_C_LEFT: c = K_C_RIGHT; break;
879 case K_RIGHT: c = K_LEFT; break;
880 case K_S_RIGHT: c = K_S_LEFT; break;
881 case K_C_RIGHT: c = K_C_LEFT; break;
882 }
883#endif
884
885#ifdef FEAT_VISUAL
886 /*
887 * If 'keymodel' contains "startsel", may start selection. If it
888 * does, a CTRL-O and c will be stuffed, we need to get these
889 * characters.
890 */
891 if (ins_start_select(c))
892 continue;
893#endif
894
895 /*
896 * The big switch to handle a character in insert mode.
897 */
898 switch (c)
899 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000900 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (echeck_abbr(ESC + ABBR_OFF))
902 break;
903 /*FALLTHROUGH*/
904
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000905 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#ifdef FEAT_CMDWIN
907 if (c == Ctrl_C && cmdwin_type != 0)
908 {
909 /* Close the cmdline window. */
910 cmdwin_result = K_IGNORE;
911 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000912 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 goto doESCkey;
914 }
915#endif
916
917#ifdef UNIX
918do_intr:
919#endif
920 /* when 'insertmode' set, and not halfway a mapping, don't leave
921 * Insert mode */
922 if (goto_im())
923 {
924 if (got_int)
925 {
926 (void)vgetc(); /* flush all buffers */
927 got_int = FALSE;
928 }
929 else
930 vim_beep();
931 break;
932 }
933doESCkey:
934 /*
935 * This is the ONLY return from edit()!
936 */
937 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
938 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000939 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 o_lnum = curwin->w_cursor.lnum;
941
Bram Moolenaar488c6512005-08-11 20:09:58 +0000942 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000943 {
944#ifdef FEAT_AUTOCMD
945 if (cmdchar != 'r' && cmdchar != 'v')
946 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
947 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000948 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 continue;
953
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000954 case Ctrl_Z: /* suspend when 'insertmode' set */
955 if (!p_im)
956 goto normalchar; /* insert CTRL-Z as normal char */
957 stuffReadbuff((char_u *)":st\r");
958 c = Ctrl_O;
959 /*FALLTHROUGH*/
960
961 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000962#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000963 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000964 goto docomplete;
965#endif
966 if (echeck_abbr(Ctrl_O + ABBR_OFF))
967 break;
968 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000969
970#ifdef FEAT_VIRTUALEDIT
971 /* don't move the cursor left when 'virtualedit' has "onemore". */
972 if (ve_flags & VE_ONEMORE)
973 {
974 ins_at_eol = FALSE;
975 nomove = TRUE;
976 }
977#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978 count = 0;
979 goto doESCkey;
980
Bram Moolenaar572cb562005-08-05 21:35:02 +0000981 case K_INS: /* toggle insert/replace mode */
982 case K_KINS:
983 ins_insert(replaceState);
984 break;
985
986 case K_SELECT: /* end of Select mode mapping - ignore */
987 break;
988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989#ifdef FEAT_SNIFF
990 case K_SNIFF: /* Sniff command received */
991 stuffcharReadbuff(K_SNIFF);
992 goto doESCkey;
993#endif
994
995 case K_HELP: /* Help key works like <ESC> <Help> */
996 case K_F1:
997 case K_XF1:
998 stuffcharReadbuff(K_HELP);
999 if (p_im)
1000 need_start_insertmode = TRUE;
1001 goto doESCkey;
1002
1003#ifdef FEAT_NETBEANS_INTG
1004 case K_F21: /* NetBeans command */
1005 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001006 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 --no_mapping;
1008 netbeans_keycommand(i);
1009 break;
1010#endif
1011
1012 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 case NUL:
1014 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 /* For ^@ the trailing ESC will end the insert, unless there is an
1016 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1018 && c != Ctrl_A && !p_im)
1019 goto doESCkey; /* quit insert mode */
1020 inserted_space = FALSE;
1021 break;
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 ins_reg();
1025 auto_format(FALSE, TRUE);
1026 inserted_space = FALSE;
1027 break;
1028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 ins_ctrl_g();
1031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case Ctrl_HAT: /* switch input mode and/or langmap */
1034 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 break;
1036
1037#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (!p_ari)
1040 goto normalchar;
1041 ins_ctrl_();
1042 break;
1043#endif
1044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1047 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1048 goto docomplete;
1049#endif
1050 /* FALLTHROUGH */
1051
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001052 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053# ifdef FEAT_INS_EXPAND
1054 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1055 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001056 if (has_compl_option(FALSE))
1057 goto docomplete;
1058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060# endif
1061 ins_shift(c, lastc);
1062 auto_format(FALSE, TRUE);
1063 inserted_space = FALSE;
1064 break;
1065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_KDEL:
1068 ins_del();
1069 auto_format(FALSE, TRUE);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case Ctrl_H:
1074 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1075 auto_format(FALSE, TRUE);
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1080 auto_format(FALSE, TRUE);
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001084# ifdef FEAT_COMPL_FUNC
1085 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001087 goto docomplete;
1088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1090 auto_format(FALSE, TRUE);
1091 inserted_space = FALSE;
1092 break;
1093
1094#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 case K_LEFTMOUSE_NM:
1097 case K_LEFTDRAG:
1098 case K_LEFTRELEASE:
1099 case K_LEFTRELEASE_NM:
1100 case K_MIDDLEMOUSE:
1101 case K_MIDDLEDRAG:
1102 case K_MIDDLERELEASE:
1103 case K_RIGHTMOUSE:
1104 case K_RIGHTDRAG:
1105 case K_RIGHTRELEASE:
1106 case K_X1MOUSE:
1107 case K_X1DRAG:
1108 case K_X1RELEASE:
1109 case K_X2MOUSE:
1110 case K_X2DRAG:
1111 case K_X2RELEASE:
1112 ins_mouse(c);
1113 break;
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001116 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 break;
1118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001120 ins_mousescroll(MSCR_UP);
1121 break;
1122
1123 case K_MOUSELEFT: /* Scroll wheel left */
1124 ins_mousescroll(MSCR_LEFT);
1125 break;
1126
1127 case K_MOUSERIGHT: /* Scroll wheel right */
1128 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 break;
1130#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001131#ifdef FEAT_GUI_TABLINE
1132 case K_TABLINE:
1133 case K_TABMENU:
1134 ins_tabline(c);
1135 break;
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 break;
1140
Bram Moolenaar754b5602006-02-09 23:53:20 +00001141#ifdef FEAT_AUTOCMD
1142 case K_CURSORHOLD: /* Didn't type something for a while. */
1143 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1144 did_cursorhold = TRUE;
1145 break;
1146#endif
1147
Bram Moolenaar4770d092006-01-12 23:22:24 +00001148#ifdef FEAT_GUI_W32
1149 /* On Win32 ignore <M-F4>, we get it when closing the window was
1150 * cancelled. */
1151 case K_F4:
1152 if (mod_mask != MOD_MASK_ALT)
1153 goto normalchar;
1154 break;
1155#endif
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#ifdef FEAT_GUI
1158 case K_VER_SCROLLBAR:
1159 ins_scroll();
1160 break;
1161
1162 case K_HOR_SCROLLBAR:
1163 ins_horscroll();
1164 break;
1165#endif
1166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001167 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 case K_S_HOME:
1170 case K_C_HOME:
1171 ins_home(c);
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 case K_S_END:
1177 case K_C_END:
1178 ins_end(c);
1179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001182 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1183 ins_s_left();
1184 else
1185 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_C_LEFT:
1190 ins_s_left();
1191 break;
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1195 ins_s_right();
1196 else
1197 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 case K_C_RIGHT:
1202 ins_s_right();
1203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001206#ifdef FEAT_INS_EXPAND
1207 if (pum_visible())
1208 goto docomplete;
1209#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001210 if (mod_mask & MOD_MASK_SHIFT)
1211 ins_pageup();
1212 else
1213 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 break;
1215
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 case K_PAGEUP:
1218 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001219#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001220 if (pum_visible())
1221 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 ins_pageup();
1224 break;
1225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001227#ifdef FEAT_INS_EXPAND
1228 if (pum_visible())
1229 goto docomplete;
1230#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001231 if (mod_mask & MOD_MASK_SHIFT)
1232 ins_pagedown();
1233 else
1234 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001237 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 case K_PAGEDOWN:
1239 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001240#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001241 if (pum_visible())
1242 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ins_pagedown();
1245 break;
1246
1247#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 ins_drop();
1250 break;
1251#endif
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 c = TAB;
1255 /* FALLTHROUGH */
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1259 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1260 goto docomplete;
1261#endif
1262 inserted_space = FALSE;
1263 if (ins_tab())
1264 goto normalchar; /* insert TAB as a normal char */
1265 auto_format(FALSE, TRUE);
1266 break;
1267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 c = CAR;
1270 /* FALLTHROUGH */
1271 case CAR:
1272 case NL:
1273#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1274 /* In a quickfix window a <CR> jumps to the error under the
1275 * cursor. */
1276 if (bt_quickfix(curbuf) && c == CAR)
1277 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001278 if (curwin->w_llist_ref == NULL) /* quickfix window */
1279 do_cmdline_cmd((char_u *)".cc");
1280 else /* location list window */
1281 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283 }
1284#endif
1285#ifdef FEAT_CMDWIN
1286 if (cmdwin_type != 0)
1287 {
1288 /* Execute the command in the cmdline window. */
1289 cmdwin_result = CAR;
1290 goto doESCkey;
1291 }
1292#endif
1293 if (ins_eol(c) && !p_im)
1294 goto doESCkey; /* out of memory */
1295 auto_format(FALSE, FALSE);
1296 inserted_space = FALSE;
1297 break;
1298
Bram Moolenaar860cae12010-06-05 23:22:07 +02001299#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301# ifdef FEAT_INS_EXPAND
1302 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1303 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 if (has_compl_option(TRUE))
1305 goto docomplete;
1306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308# endif
1309# ifdef FEAT_DIGRAPHS
1310 c = ins_digraph();
1311 if (c == NUL)
1312 break;
1313# endif
1314 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001318 case Ctrl_X: /* Enter CTRL-X mode */
1319 ins_ctrl_x();
1320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (ctrl_x_mode != CTRL_X_TAGS)
1324 goto normalchar;
1325 goto docomplete;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (ctrl_x_mode != CTRL_X_FILES)
1329 goto normalchar;
1330 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001331
1332 case 's': /* Spelling completion after ^X */
1333 case Ctrl_S:
1334 if (ctrl_x_mode != CTRL_X_SPELL)
1335 goto normalchar;
1336 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#ifdef FEAT_INS_EXPAND
1341 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1342#endif
1343 {
1344 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1345 if (p_im)
1346 {
1347 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1348 break;
1349 goto doESCkey;
1350 }
1351 goto normalchar;
1352 }
1353#ifdef FEAT_INS_EXPAND
1354 /* FALLTHROUGH */
1355
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001356 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 case Ctrl_N:
1358 /* if 'complete' is empty then plain ^P is no longer special,
1359 * but it is under other ^X modes */
1360 if (*curbuf->b_p_cpt == NUL
1361 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001362 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 goto normalchar;
1364
1365docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001366 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001369 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 break;
1371#endif /* FEAT_INS_EXPAND */
1372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001373 case Ctrl_Y: /* copy from previous line or scroll down */
1374 case Ctrl_E: /* copy from next line or scroll up */
1375 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 break;
1377
1378 default:
1379#ifdef UNIX
1380 if (c == intr_char) /* special interrupt char */
1381 goto do_intr;
1382#endif
1383
1384 /*
1385 * Insert a nomal character.
1386 */
1387normalchar:
1388#ifdef FEAT_SMARTINDENT
1389 /* Try to perform smart-indenting. */
1390 ins_try_si(c);
1391#endif
1392
1393 if (c == ' ')
1394 {
1395 inserted_space = TRUE;
1396#ifdef FEAT_CINDENT
1397 if (inindent(0))
1398 can_cindent = FALSE;
1399#endif
1400 if (Insstart_blank_vcol == MAXCOL
1401 && curwin->w_cursor.lnum == Insstart.lnum)
1402 Insstart_blank_vcol = get_nolist_virtcol();
1403 }
1404
1405 if (vim_iswordc(c) || !echeck_abbr(
1406#ifdef FEAT_MBYTE
1407 /* Add ABBR_OFF for characters above 0x100, this is
1408 * what check_abbr() expects. */
1409 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1410#endif
1411 c))
1412 {
1413 insert_special(c, FALSE, FALSE);
1414#ifdef FEAT_RIGHTLEFT
1415 revins_legal++;
1416 revins_chars++;
1417#endif
1418 }
1419
1420 auto_format(FALSE, TRUE);
1421
1422#ifdef FEAT_FOLDING
1423 /* When inserting a character the cursor line must never be in a
1424 * closed fold. */
1425 foldOpenCursor();
1426#endif
1427 break;
1428 } /* end of switch (c) */
1429
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001430#ifdef FEAT_AUTOCMD
1431 /* If typed something may trigger CursorHoldI again. */
1432 if (c != K_CURSORHOLD)
1433 did_cursorhold = FALSE;
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 /* If the cursor was moved we didn't just insert a space */
1437 if (arrow_used)
1438 inserted_space = FALSE;
1439
1440#ifdef FEAT_CINDENT
1441 if (can_cindent && cindent_on()
1442# ifdef FEAT_INS_EXPAND
1443 && ctrl_x_mode == 0
1444# endif
1445 )
1446 {
1447force_cindent:
1448 /*
1449 * Indent now if a key was typed that is in 'cinkeys'.
1450 */
1451 if (in_cinkeys(c, ' ', line_is_white))
1452 {
1453 if (stop_arrow() == OK)
1454 /* re-indent the current line */
1455 do_c_expr_indent();
1456 }
1457 }
1458#endif /* FEAT_CINDENT */
1459
1460 } /* for (;;) */
1461 /* NOTREACHED */
1462}
1463
1464/*
1465 * Redraw for Insert mode.
1466 * This is postponed until getting the next character to make '$' in the 'cpo'
1467 * option work correctly.
1468 * Only redraw when there are no characters available. This speeds up
1469 * inserting sequences of characters (e.g., for CTRL-R).
1470 */
1471 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001472ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001473 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001475#ifdef FEAT_CONCEAL
1476 linenr_T conceal_old_cursor_line = 0;
1477 linenr_T conceal_new_cursor_line = 0;
1478 int conceal_update_lines = FALSE;
1479#endif
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (!char_avail())
1482 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001483#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001484 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1485 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001486 if (ready && (
1487# ifdef FEAT_AUTOCMD
1488 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001489# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001490# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1491 ||
1492# endif
1493# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001494 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001495# endif
1496 )
1497 && !equalpos(last_cursormoved, curwin->w_cursor)
1498# ifdef FEAT_INS_EXPAND
1499 && !pum_visible()
1500# endif
1501 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001502 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001503# ifdef FEAT_SYN_HL
1504 /* Need to update the screen first, to make sure syntax
1505 * highlighting is correct after making a change (e.g., inserting
1506 * a "(". The autocommand may also require a redraw, so it's done
1507 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001508 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001509 update_screen(0);
1510# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001511# ifdef FEAT_AUTOCMD
1512 if (has_cursormovedI())
1513 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1514# endif
1515# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001516 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001517 {
1518 conceal_old_cursor_line = last_cursormoved.lnum;
1519 conceal_new_cursor_line = curwin->w_cursor.lnum;
1520 conceal_update_lines = TRUE;
1521 }
1522# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001523 last_cursormoved = curwin->w_cursor;
1524 }
1525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (must_redraw)
1527 update_screen(0);
1528 else if (clear_cmdline || redraw_cmdline)
1529 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001530# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001531 if ((conceal_update_lines
1532 && (conceal_old_cursor_line != conceal_new_cursor_line
1533 || conceal_cursor_line(curwin)))
1534 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001535 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001536 if (conceal_old_cursor_line != conceal_new_cursor_line)
1537 update_single_line(curwin, conceal_old_cursor_line);
1538 update_single_line(curwin, conceal_new_cursor_line == 0
1539 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001540 curwin->w_valid &= ~VALID_CROW;
1541 }
1542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 showruler(FALSE);
1544 setcursor();
1545 emsg_on_display = FALSE; /* may remove error message now */
1546 }
1547}
1548
1549/*
1550 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1551 */
1552 static void
1553ins_ctrl_v()
1554{
1555 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001556 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001559 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
1561 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001564 did_putchar = TRUE;
1565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1567
1568#ifdef FEAT_CMDL_INFO
1569 add_to_showcmd_c(Ctrl_V);
1570#endif
1571
1572 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001573 if (did_putchar)
1574 /* when the line fits in 'columns' the '^' is at the start of the next
1575 * line and will not removed by the redraw */
1576 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#ifdef FEAT_CMDL_INFO
1578 clear_showcmd();
1579#endif
1580 insert_special(c, FALSE, TRUE);
1581#ifdef FEAT_RIGHTLEFT
1582 revins_chars++;
1583 revins_legal++;
1584#endif
1585}
1586
1587/*
1588 * Put a character directly onto the screen. It's not stored in a buffer.
1589 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1590 */
1591static int pc_status;
1592#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1593#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1594#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1595#define PC_STATUS_SET 3 /* pc_bytes was filled */
1596#ifdef FEAT_MBYTE
1597static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1598#else
1599static char_u pc_bytes[2]; /* saved bytes */
1600#endif
1601static int pc_attr;
1602static int pc_row;
1603static int pc_col;
1604
1605 void
1606edit_putchar(c, highlight)
1607 int c;
1608 int highlight;
1609{
1610 int attr;
1611
1612 if (ScreenLines != NULL)
1613 {
1614 update_topline(); /* just in case w_topline isn't valid */
1615 validate_cursor();
1616 if (highlight)
1617 attr = hl_attr(HLF_8);
1618 else
1619 attr = 0;
1620 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1621 pc_col = W_WINCOL(curwin);
1622#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1623 pc_status = PC_STATUS_UNSET;
1624#endif
1625#ifdef FEAT_RIGHTLEFT
1626 if (curwin->w_p_rl)
1627 {
1628 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1629# ifdef FEAT_MBYTE
1630 if (has_mbyte)
1631 {
1632 int fix_col = mb_fix_col(pc_col, pc_row);
1633
1634 if (fix_col != pc_col)
1635 {
1636 screen_putchar(' ', pc_row, fix_col, attr);
1637 --curwin->w_wcol;
1638 pc_status = PC_STATUS_RIGHT;
1639 }
1640 }
1641# endif
1642 }
1643 else
1644#endif
1645 {
1646 pc_col += curwin->w_wcol;
1647#ifdef FEAT_MBYTE
1648 if (mb_lefthalve(pc_row, pc_col))
1649 pc_status = PC_STATUS_LEFT;
1650#endif
1651 }
1652
1653 /* save the character to be able to put it back */
1654#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1655 if (pc_status == PC_STATUS_UNSET)
1656#endif
1657 {
1658 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1659 pc_status = PC_STATUS_SET;
1660 }
1661 screen_putchar(c, pc_row, pc_col, attr);
1662 }
1663}
1664
1665/*
1666 * Undo the previous edit_putchar().
1667 */
1668 void
1669edit_unputchar()
1670{
1671 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1672 {
1673#if defined(FEAT_MBYTE)
1674 if (pc_status == PC_STATUS_RIGHT)
1675 ++curwin->w_wcol;
1676 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1677 redrawWinline(curwin->w_cursor.lnum, FALSE);
1678 else
1679#endif
1680 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1681 }
1682}
1683
1684/*
1685 * Called when p_dollar is set: display a '$' at the end of the changed text
1686 * Only works when cursor is in the line that changes.
1687 */
1688 void
1689display_dollar(col)
1690 colnr_T col;
1691{
1692 colnr_T save_col;
1693
1694 if (!redrawing())
1695 return;
1696
1697 cursor_off();
1698 save_col = curwin->w_cursor.col;
1699 curwin->w_cursor.col = col;
1700#ifdef FEAT_MBYTE
1701 if (has_mbyte)
1702 {
1703 char_u *p;
1704
1705 /* If on the last byte of a multi-byte move to the first byte. */
1706 p = ml_get_curline();
1707 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1708 }
1709#endif
1710 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1711 if (curwin->w_wcol < W_WIDTH(curwin))
1712 {
1713 edit_putchar('$', FALSE);
1714 dollar_vcol = curwin->w_virtcol;
1715 }
1716 curwin->w_cursor.col = save_col;
1717}
1718
1719/*
1720 * Call this function before moving the cursor from the normal insert position
1721 * in insert mode.
1722 */
1723 static void
1724undisplay_dollar()
1725{
1726 if (dollar_vcol)
1727 {
1728 dollar_vcol = 0;
1729 redrawWinline(curwin->w_cursor.lnum, FALSE);
1730 }
1731}
1732
1733/*
1734 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1735 * Keep the cursor on the same character.
1736 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1737 * type == INDENT_DEC decrease indent (for CTRL-D)
1738 * type == INDENT_SET set indent to "amount"
1739 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1740 */
1741 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001742change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 int type;
1744 int amount;
1745 int round;
1746 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001747 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 int vcol;
1750 int last_vcol;
1751 int insstart_less; /* reduction for Insstart.col */
1752 int new_cursor_col;
1753 int i;
1754 char_u *ptr;
1755 int save_p_list;
1756 int start_col;
1757 colnr_T vc;
1758#ifdef FEAT_VREPLACE
1759 colnr_T orig_col = 0; /* init for GCC */
1760 char_u *new_line, *orig_line = NULL; /* init for GCC */
1761
1762 /* VREPLACE mode needs to know what the line was like before changing */
1763 if (State & VREPLACE_FLAG)
1764 {
1765 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1766 orig_col = curwin->w_cursor.col;
1767 }
1768#endif
1769
1770 /* for the following tricks we don't want list mode */
1771 save_p_list = curwin->w_p_list;
1772 curwin->w_p_list = FALSE;
1773 vc = getvcol_nolist(&curwin->w_cursor);
1774 vcol = vc;
1775
1776 /*
1777 * For Replace mode we need to fix the replace stack later, which is only
1778 * possible when the cursor is in the indent. Remember the number of
1779 * characters before the cursor if it's possible.
1780 */
1781 start_col = curwin->w_cursor.col;
1782
1783 /* determine offset from first non-blank */
1784 new_cursor_col = curwin->w_cursor.col;
1785 beginline(BL_WHITE);
1786 new_cursor_col -= curwin->w_cursor.col;
1787
1788 insstart_less = curwin->w_cursor.col;
1789
1790 /*
1791 * If the cursor is in the indent, compute how many screen columns the
1792 * cursor is to the left of the first non-blank.
1793 */
1794 if (new_cursor_col < 0)
1795 vcol = get_indent() - vcol;
1796
1797 if (new_cursor_col > 0) /* can't fix replace stack */
1798 start_col = -1;
1799
1800 /*
1801 * Set the new indent. The cursor will be put on the first non-blank.
1802 */
1803 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001804 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 else
1806 {
1807#ifdef FEAT_VREPLACE
1808 int save_State = State;
1809
1810 /* Avoid being called recursively. */
1811 if (State & VREPLACE_FLAG)
1812 State = INSERT;
1813#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001814 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#ifdef FEAT_VREPLACE
1816 State = save_State;
1817#endif
1818 }
1819 insstart_less -= curwin->w_cursor.col;
1820
1821 /*
1822 * Try to put cursor on same character.
1823 * If the cursor is at or after the first non-blank in the line,
1824 * compute the cursor column relative to the column of the first
1825 * non-blank character.
1826 * If we are not in insert mode, leave the cursor on the first non-blank.
1827 * If the cursor is before the first non-blank, position it relative
1828 * to the first non-blank, counted in screen columns.
1829 */
1830 if (new_cursor_col >= 0)
1831 {
1832 /*
1833 * When changing the indent while the cursor is touching it, reset
1834 * Insstart_col to 0.
1835 */
1836 if (new_cursor_col == 0)
1837 insstart_less = MAXCOL;
1838 new_cursor_col += curwin->w_cursor.col;
1839 }
1840 else if (!(State & INSERT))
1841 new_cursor_col = curwin->w_cursor.col;
1842 else
1843 {
1844 /*
1845 * Compute the screen column where the cursor should be.
1846 */
1847 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001848 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850 /*
1851 * Advance the cursor until we reach the right screen column.
1852 */
1853 vcol = last_vcol = 0;
1854 new_cursor_col = -1;
1855 ptr = ml_get_curline();
1856 while (vcol <= (int)curwin->w_virtcol)
1857 {
1858 last_vcol = vcol;
1859#ifdef FEAT_MBYTE
1860 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001861 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 else
1863#endif
1864 ++new_cursor_col;
1865 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1866 }
1867 vcol = last_vcol;
1868
1869 /*
1870 * May need to insert spaces to be able to position the cursor on
1871 * the right screen column.
1872 */
1873 if (vcol != (int)curwin->w_virtcol)
1874 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001875 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001877 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (ptr != NULL)
1879 {
1880 new_cursor_col += i;
1881 ptr[i] = NUL;
1882 while (--i >= 0)
1883 ptr[i] = ' ';
1884 ins_str(ptr);
1885 vim_free(ptr);
1886 }
1887 }
1888
1889 /*
1890 * When changing the indent while the cursor is in it, reset
1891 * Insstart_col to 0.
1892 */
1893 insstart_less = MAXCOL;
1894 }
1895
1896 curwin->w_p_list = save_p_list;
1897
1898 if (new_cursor_col <= 0)
1899 curwin->w_cursor.col = 0;
1900 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001901 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 curwin->w_set_curswant = TRUE;
1903 changed_cline_bef_curs();
1904
1905 /*
1906 * May have to adjust the start of the insert.
1907 */
1908 if (State & INSERT)
1909 {
1910 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1911 {
1912 if ((int)Insstart.col <= insstart_less)
1913 Insstart.col = 0;
1914 else
1915 Insstart.col -= insstart_less;
1916 }
1917 if ((int)ai_col <= insstart_less)
1918 ai_col = 0;
1919 else
1920 ai_col -= insstart_less;
1921 }
1922
1923 /*
1924 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1925 * If the number of characters before the cursor decreased, need to pop a
1926 * few characters from the replace stack.
1927 * If the number of characters before the cursor increased, need to push a
1928 * few NULs onto the replace stack.
1929 */
1930 if (REPLACE_NORMAL(State) && start_col >= 0)
1931 {
1932 while (start_col > (int)curwin->w_cursor.col)
1933 {
1934 replace_join(0); /* remove a NUL from the replace stack */
1935 --start_col;
1936 }
1937 while (start_col < (int)curwin->w_cursor.col || replaced)
1938 {
1939 replace_push(NUL);
1940 if (replaced)
1941 {
1942 replace_push(replaced);
1943 replaced = NUL;
1944 }
1945 ++start_col;
1946 }
1947 }
1948
1949#ifdef FEAT_VREPLACE
1950 /*
1951 * For VREPLACE mode, we also have to fix the replace stack. In this case
1952 * it is always possible because we backspace over the whole line and then
1953 * put it back again the way we wanted it.
1954 */
1955 if (State & VREPLACE_FLAG)
1956 {
1957 /* If orig_line didn't allocate, just return. At least we did the job,
1958 * even if you can't backspace. */
1959 if (orig_line == NULL)
1960 return;
1961
1962 /* Save new line */
1963 new_line = vim_strsave(ml_get_curline());
1964 if (new_line == NULL)
1965 return;
1966
1967 /* We only put back the new line up to the cursor */
1968 new_line[curwin->w_cursor.col] = NUL;
1969
1970 /* Put back original line */
1971 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1972 curwin->w_cursor.col = orig_col;
1973
1974 /* Backspace from cursor to start of line */
1975 backspace_until_column(0);
1976
1977 /* Insert new stuff into line again */
1978 ins_bytes(new_line);
1979
1980 vim_free(new_line);
1981 }
1982#endif
1983}
1984
1985/*
1986 * Truncate the space at the end of a line. This is to be used only in an
1987 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1988 * modes.
1989 */
1990 void
1991truncate_spaces(line)
1992 char_u *line;
1993{
1994 int i;
1995
1996 /* find start of trailing white space */
1997 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1998 {
1999 if (State & REPLACE_FLAG)
2000 replace_join(0); /* remove a NUL from the replace stack */
2001 }
2002 line[i + 1] = NUL;
2003}
2004
2005#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2006 || defined(FEAT_COMMENTS) || defined(PROTO)
2007/*
2008 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2009 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002010 * Will attempt not to go before "col" even when there is a composing
2011 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 */
2013 void
2014backspace_until_column(col)
2015 int col;
2016{
2017 while ((int)curwin->w_cursor.col > col)
2018 {
2019 curwin->w_cursor.col--;
2020 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002021 replace_do_bs(col);
2022 else if (!del_char_after_col(col))
2023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025}
2026#endif
2027
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002028/*
2029 * Like del_char(), but make sure not to go before column "limit_col".
2030 * Only matters when there are composing characters.
2031 * Return TRUE when something was deleted.
2032 */
2033 static int
2034del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002035 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002036{
2037#ifdef FEAT_MBYTE
2038 if (enc_utf8 && limit_col >= 0)
2039 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002040 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002041
2042 /* Make sure the cursor is at the start of a character, but
2043 * skip forward again when going too far back because of a
2044 * composing character. */
2045 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002046 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002047 {
2048 int l = utf_ptr2len(ml_get_cursor());
2049
2050 if (l == 0) /* end of line */
2051 break;
2052 curwin->w_cursor.col += l;
2053 }
2054 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2055 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002056 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002057 }
2058 else
2059#endif
2060 (void)del_char(FALSE);
2061 return TRUE;
2062}
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2065/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002066 * CTRL-X pressed in Insert mode.
2067 */
2068 static void
2069ins_ctrl_x()
2070{
2071 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2072 * CTRL-V works like CTRL-N */
2073 if (ctrl_x_mode != CTRL_X_CMDLINE)
2074 {
2075 /* if the next ^X<> won't ADD nothing, then reset
2076 * compl_cont_status */
2077 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002078 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002079 else
2080 compl_cont_status = 0;
2081 /* We're not sure which CTRL-X mode it will be yet */
2082 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2083 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2084 edit_submode_pre = NULL;
2085 showmode();
2086 }
2087}
2088
2089/*
2090 * Return TRUE if the 'dict' or 'tsr' option can be used.
2091 */
2092 static int
2093has_compl_option(dict_opt)
2094 int dict_opt;
2095{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002096 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002097# ifdef FEAT_SPELL
2098 && !curwin->w_p_spell
2099# endif
2100 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002101 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2102 {
2103 ctrl_x_mode = 0;
2104 edit_submode = NULL;
2105 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2106 : (char_u *)_("'thesaurus' option is empty"),
2107 hl_attr(HLF_E));
2108 if (emsg_silent == 0)
2109 {
2110 vim_beep();
2111 setcursor();
2112 out_flush();
2113 ui_delay(2000L, FALSE);
2114 }
2115 return FALSE;
2116 }
2117 return TRUE;
2118}
2119
2120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2122 * This depends on the current mode.
2123 */
2124 int
2125vim_is_ctrl_x_key(c)
2126 int c;
2127{
2128 /* Always allow ^R - let it's results then be checked */
2129 if (c == Ctrl_R)
2130 return TRUE;
2131
Bram Moolenaare3226be2005-12-18 22:10:00 +00002132 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002133 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002134 return TRUE;
2135
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 switch (ctrl_x_mode)
2137 {
2138 case 0: /* Not in any CTRL-X mode */
2139 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2140 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002141 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2143 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2144 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002145 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2146 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 case CTRL_X_SCROLL:
2148 return (c == Ctrl_Y || c == Ctrl_E);
2149 case CTRL_X_WHOLE_LINE:
2150 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2151 case CTRL_X_FILES:
2152 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2153 case CTRL_X_DICTIONARY:
2154 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2155 case CTRL_X_THESAURUS:
2156 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2157 case CTRL_X_TAGS:
2158 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2159#ifdef FEAT_FIND_ID
2160 case CTRL_X_PATH_PATTERNS:
2161 return (c == Ctrl_P || c == Ctrl_N);
2162 case CTRL_X_PATH_DEFINES:
2163 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2164#endif
2165 case CTRL_X_CMDLINE:
2166 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2167 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002168#ifdef FEAT_COMPL_FUNC
2169 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002170 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002171 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002172 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002173#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002174 case CTRL_X_SPELL:
2175 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 EMSG(_(e_internal));
2178 return FALSE;
2179}
2180
2181/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002182 * Return TRUE when character "c" is part of the item currently being
2183 * completed. Used to decide whether to abandon complete mode when the menu
2184 * is visible.
2185 */
2186 static int
2187ins_compl_accept_char(c)
2188 int c;
2189{
2190 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2191 /* When expanding an identifier only accept identifier chars. */
2192 return vim_isIDc(c);
2193
2194 switch (ctrl_x_mode)
2195 {
2196 case CTRL_X_FILES:
2197 /* When expanding file name only accept file name chars. But not
2198 * path separators, so that "proto/<Tab>" expands files in
2199 * "proto", not "proto/" as a whole */
2200 return vim_isfilec(c) && !vim_ispathsep(c);
2201
2202 case CTRL_X_CMDLINE:
2203 case CTRL_X_OMNI:
2204 /* Command line and Omni completion can work with just about any
2205 * printable character, but do stop at white space. */
2206 return vim_isprintc(c) && !vim_iswhite(c);
2207
2208 case CTRL_X_WHOLE_LINE:
2209 /* For while line completion a space can be part of the line. */
2210 return vim_isprintc(c);
2211 }
2212 return vim_iswordc(c);
2213}
2214
2215/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002216 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002218 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * the rest of the word to be in -- webb
2220 */
2221 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002222ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 char_u *str;
2224 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002225 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 char_u *fname;
2227 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002228 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002230 char_u *p;
2231 int i, c;
2232 int actual_len; /* Take multi-byte characters */
2233 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002234 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002235 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 int has_lower = FALSE;
2237 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002239 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002241 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
Bram Moolenaara2993e12007-08-12 14:38:46 +00002243 /* Find actual length of completion. */
2244#ifdef FEAT_MBYTE
2245 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002247 p = str;
2248 actual_len = 0;
2249 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002251 mb_ptr_adv(p);
2252 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002255 else
2256#endif
2257 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
Bram Moolenaara2993e12007-08-12 14:38:46 +00002259 /* Find actual length of original text. */
2260#ifdef FEAT_MBYTE
2261 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002263 p = compl_orig_text;
2264 actual_compl_length = 0;
2265 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002267 mb_ptr_adv(p);
2268 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002271 else
2272#endif
2273 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002275 /* "actual_len" may be smaller than "actual_compl_length" when using
2276 * thesaurus, only use the minimum when comparing. */
2277 min_len = actual_len < actual_compl_length
2278 ? actual_len : actual_compl_length;
2279
Bram Moolenaara2993e12007-08-12 14:38:46 +00002280 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002281 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002282 if (wca != NULL)
2283 {
2284 p = str;
2285 for (i = 0; i < actual_len; ++i)
2286#ifdef FEAT_MBYTE
2287 if (has_mbyte)
2288 wca[i] = mb_ptr2char_adv(&p);
2289 else
2290#endif
2291 wca[i] = *(p++);
2292
2293 /* Rule 1: Were any chars converted to lower? */
2294 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002295 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002296 {
2297#ifdef FEAT_MBYTE
2298 if (has_mbyte)
2299 c = mb_ptr2char_adv(&p);
2300 else
2301#endif
2302 c = *(p++);
2303 if (MB_ISLOWER(c))
2304 {
2305 has_lower = TRUE;
2306 if (MB_ISUPPER(wca[i]))
2307 {
2308 /* Rule 1 is satisfied. */
2309 for (i = actual_compl_length; i < actual_len; ++i)
2310 wca[i] = MB_TOLOWER(wca[i]);
2311 break;
2312 }
2313 }
2314 }
2315
2316 /*
2317 * Rule 2: No lower case, 2nd consecutive letter converted to
2318 * upper case.
2319 */
2320 if (!has_lower)
2321 {
2322 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002323 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002324 {
2325#ifdef FEAT_MBYTE
2326 if (has_mbyte)
2327 c = mb_ptr2char_adv(&p);
2328 else
2329#endif
2330 c = *(p++);
2331 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2332 {
2333 /* Rule 2 is satisfied. */
2334 for (i = actual_compl_length; i < actual_len; ++i)
2335 wca[i] = MB_TOUPPER(wca[i]);
2336 break;
2337 }
2338 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2339 }
2340 }
2341
2342 /* Copy the original case of the part we typed. */
2343 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002344 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002345 {
2346#ifdef FEAT_MBYTE
2347 if (has_mbyte)
2348 c = mb_ptr2char_adv(&p);
2349 else
2350#endif
2351 c = *(p++);
2352 if (MB_ISLOWER(c))
2353 wca[i] = MB_TOLOWER(wca[i]);
2354 else if (MB_ISUPPER(c))
2355 wca[i] = MB_TOUPPER(wca[i]);
2356 }
2357
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002358 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002359 * Generate encoding specific output from wide character array.
2360 * Multi-byte characters can occupy up to five bytes more than
2361 * ASCII characters, and we also need one byte for NUL, so stay
2362 * six bytes away from the edge of IObuff.
2363 */
2364 p = IObuff;
2365 i = 0;
2366 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2367#ifdef FEAT_MBYTE
2368 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002369 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002370 else
2371#endif
2372 *(p++) = wca[i++];
2373 *p = NUL;
2374
2375 vim_free(wca);
2376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002378 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2379 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002381 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382}
2383
2384/*
2385 * Add a match to the list of matches.
2386 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002387 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002388 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002390 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002391ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 char_u *str;
2393 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002394 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002396 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002397 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002398 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002399 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002401 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002402 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 ui_breakcheck();
2405 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (len < 0)
2408 len = (int)STRLEN(str);
2409
2410 /*
2411 * If the same match is already present, don't add it.
2412 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002413 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002415 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 do
2417 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002418 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002419 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002420 && match->cp_str[len] == NUL)
2421 return NOTDONE;
2422 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002423 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002426 /* Remove any popup menu before changing the list of matches. */
2427 ins_compl_del_pum();
2428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 /*
2430 * Allocate a new match structure.
2431 * Copy the values to the new match structure.
2432 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002433 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002435 return FAIL;
2436 match->cp_number = -1;
2437 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002438 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002439 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
2441 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002445
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002447 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2449 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002450 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002451 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002452 && compl_curr_match->cp_fname != NULL
2453 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002454 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002455 else if (fname != NULL)
2456 {
2457 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002458 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002461 match->cp_fname = NULL;
2462 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002463
2464 if (cptext != NULL)
2465 {
2466 int i;
2467
2468 for (i = 0; i < CPT_COUNT; ++i)
2469 if (cptext[i] != NULL && *cptext[i] != NUL)
2470 match->cp_text[i] = vim_strsave(cptext[i]);
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 /*
2474 * Link the new match structure in the list of matches.
2475 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002476 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002477 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 else if (dir == FORWARD)
2479 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002480 match->cp_next = compl_curr_match->cp_next;
2481 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483 else /* BACKWARD */
2484 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002485 match->cp_next = compl_curr_match;
2486 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002488 if (match->cp_next)
2489 match->cp_next->cp_prev = match;
2490 if (match->cp_prev)
2491 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002493 compl_first_match = match;
2494 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002496 /*
2497 * Find the longest common string if still doing that.
2498 */
2499 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2500 ins_compl_longest_match(match);
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 return OK;
2503}
2504
2505/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002506 * Return TRUE if "str[len]" matches with match->cp_str, considering
2507 * match->cp_icase.
2508 */
2509 static int
2510ins_compl_equal(match, str, len)
2511 compl_T *match;
2512 char_u *str;
2513 int len;
2514{
2515 if (match->cp_icase)
2516 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2517 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2518}
2519
2520/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002521 * Reduce the longest common string for match "match".
2522 */
2523 static void
2524ins_compl_longest_match(match)
2525 compl_T *match;
2526{
2527 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002529 int had_match;
2530
2531 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002532 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002533 /* First match, use it as a whole. */
2534 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002535 if (compl_leader != NULL)
2536 {
2537 had_match = (curwin->w_cursor.col > compl_col);
2538 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002539 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002540 ins_redraw(FALSE);
2541
2542 /* When the match isn't there (to avoid matching itself) remove it
2543 * again after redrawing. */
2544 if (!had_match)
2545 ins_compl_delete();
2546 compl_used_match = FALSE;
2547 }
2548 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002549 else
2550 {
2551 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552 p = compl_leader;
2553 s = match->cp_str;
2554 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002555 {
2556#ifdef FEAT_MBYTE
2557 if (has_mbyte)
2558 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559 c1 = mb_ptr2char(p);
2560 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002561 }
2562 else
2563#endif
2564 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002565 c1 = *p;
2566 c2 = *s;
2567 }
2568 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2569 : (c1 != c2))
2570 break;
2571#ifdef FEAT_MBYTE
2572 if (has_mbyte)
2573 {
2574 mb_ptr_adv(p);
2575 mb_ptr_adv(s);
2576 }
2577 else
2578#endif
2579 {
2580 ++p;
2581 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002582 }
2583 }
2584
2585 if (*p != NUL)
2586 {
2587 /* Leader was shortened, need to change the inserted text. */
2588 *p = NUL;
2589 had_match = (curwin->w_cursor.col > compl_col);
2590 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002591 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002592 ins_redraw(FALSE);
2593
2594 /* When the match isn't there (to avoid matching itself) remove it
2595 * again after redrawing. */
2596 if (!had_match)
2597 ins_compl_delete();
2598 }
2599
2600 compl_used_match = FALSE;
2601 }
2602}
2603
2604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 * Add an array of matches to the list of matches.
2606 * Frees matches[].
2607 */
2608 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002609ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 int num_matches;
2611 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
2614 int i;
2615 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002616 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaar572cb562005-08-05 21:35:02 +00002618 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002619 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002620 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002622 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 FreeWild(num_matches, matches);
2624}
2625
2626/* Make the completion list cyclic.
2627 * Return the number of matches (excluding the original).
2628 */
2629 static int
2630ins_compl_make_cyclic()
2631{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002632 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 int count = 0;
2634
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002635 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
2637 /*
2638 * Find the end of the list.
2639 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002640 match = compl_first_match;
2641 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002642 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002644 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 ++count;
2646 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002647 match->cp_next = compl_first_match;
2648 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 return count;
2651}
2652
Bram Moolenaara94bc432006-03-10 21:42:59 +00002653/*
2654 * Start completion for the complete() function.
2655 * "startcol" is where the matched text starts (1 is first column).
2656 * "list" is the list of matches.
2657 */
2658 void
2659set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002660 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002661 list_T *list;
2662{
2663 /* If already doing completions stop it. */
2664 if (ctrl_x_mode != 0)
2665 ins_compl_prep(' ');
2666 ins_compl_clear();
2667
2668 if (stop_arrow() == FAIL)
2669 return;
2670
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002671 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002672 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002673 startcol = curwin->w_cursor.col;
2674 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002675 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002676 /* compl_pattern doesn't need to be set */
2677 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2678 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002679 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002680 return;
2681
2682 /* Handle like dictionary completion. */
2683 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2684
2685 ins_compl_add_list(list);
2686 compl_matches = ins_compl_make_cyclic();
2687 compl_started = TRUE;
2688 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002689 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002690
2691 compl_curr_match = compl_first_match;
2692 ins_complete(Ctrl_N);
2693 out_flush();
2694}
2695
2696
Bram Moolenaar9372a112005-12-06 19:59:18 +00002697/* "compl_match_array" points the currently displayed list of entries in the
2698 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002699static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002700static int compl_match_arraysize;
2701
2702/*
2703 * Update the screen and when there is any scrolling remove the popup menu.
2704 */
2705 static void
2706ins_compl_upd_pum()
2707{
2708 int h;
2709
2710 if (compl_match_array != NULL)
2711 {
2712 h = curwin->w_cline_height;
2713 update_screen(0);
2714 if (h != curwin->w_cline_height)
2715 ins_compl_del_pum();
2716 }
2717}
2718
2719/*
2720 * Remove any popup menu.
2721 */
2722 static void
2723ins_compl_del_pum()
2724{
2725 if (compl_match_array != NULL)
2726 {
2727 pum_undisplay();
2728 vim_free(compl_match_array);
2729 compl_match_array = NULL;
2730 }
2731}
2732
2733/*
2734 * Return TRUE if the popup menu should be displayed.
2735 */
2736 static int
2737pum_wanted()
2738{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002739 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002740 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002741 return FALSE;
2742
2743 /* The display looks bad on a B&W display. */
2744 if (t_colors < 8
2745#ifdef FEAT_GUI
2746 && !gui.in_use
2747#endif
2748 )
2749 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002750 return TRUE;
2751}
2752
2753/*
2754 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002755 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002756 */
2757 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002758pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002759{
2760 compl_T *compl;
2761 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002762
2763 /* Don't display the popup menu if there are no matches or there is only
2764 * one (ignoring the original text). */
2765 compl = compl_first_match;
2766 i = 0;
2767 do
2768 {
2769 if (compl == NULL
2770 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2771 break;
2772 compl = compl->cp_next;
2773 } while (compl != compl_first_match);
2774
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002775 if (strstr((char *)p_cot, "menuone") != NULL)
2776 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002777 return (i >= 2);
2778}
2779
2780/*
2781 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002782 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002783 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002784 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002785ins_compl_show_pum()
2786{
2787 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002788 compl_T *shown_compl = NULL;
2789 int did_find_shown_match = FALSE;
2790 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002791 int i;
2792 int cur = -1;
2793 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002794 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002795
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002796 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002797 return;
2798
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002799#if defined(FEAT_EVAL)
2800 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2801 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2802#endif
2803
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002804 /* Update the screen before drawing the popup menu over it. */
2805 update_screen(0);
2806
2807 if (compl_match_array == NULL)
2808 {
2809 /* Need to build the popup menu list. */
2810 compl_match_arraysize = 0;
2811 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002812 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002813 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002814 do
2815 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002816 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2817 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002818 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002819 ++compl_match_arraysize;
2820 compl = compl->cp_next;
2821 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002822 if (compl_match_arraysize == 0)
2823 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002824 compl_match_array = (pumitem_T *)alloc_clear(
2825 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002826 * compl_match_arraysize));
2827 if (compl_match_array != NULL)
2828 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002829 /* If the current match is the original text don't find the first
2830 * match after it, don't highlight anything. */
2831 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2832 shown_match_ok = TRUE;
2833
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002834 i = 0;
2835 compl = compl_first_match;
2836 do
2837 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002838 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2839 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002840 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002841 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002842 if (!shown_match_ok)
2843 {
2844 if (compl == compl_shown_match || did_find_shown_match)
2845 {
2846 /* This item is the shown match or this is the
2847 * first displayed item after the shown match. */
2848 compl_shown_match = compl;
2849 did_find_shown_match = TRUE;
2850 shown_match_ok = TRUE;
2851 }
2852 else
2853 /* Remember this displayed match for when the
2854 * shown match is just below it. */
2855 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002856 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002857 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002858
2859 if (compl->cp_text[CPT_ABBR] != NULL)
2860 compl_match_array[i].pum_text =
2861 compl->cp_text[CPT_ABBR];
2862 else
2863 compl_match_array[i].pum_text = compl->cp_str;
2864 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2865 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2866 if (compl->cp_text[CPT_MENU] != NULL)
2867 compl_match_array[i++].pum_extra =
2868 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002869 else
2870 compl_match_array[i++].pum_extra = compl->cp_fname;
2871 }
2872
2873 if (compl == compl_shown_match)
2874 {
2875 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002876
2877 /* When the original text is the shown match don't set
2878 * compl_shown_match. */
2879 if (compl->cp_flags & ORIGINAL_TEXT)
2880 shown_match_ok = TRUE;
2881
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002882 if (!shown_match_ok && shown_compl != NULL)
2883 {
2884 /* The shown match isn't displayed, set it to the
2885 * previously displayed match. */
2886 compl_shown_match = shown_compl;
2887 shown_match_ok = TRUE;
2888 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002889 }
2890 compl = compl->cp_next;
2891 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002892
2893 if (!shown_match_ok) /* no displayed match at all */
2894 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895 }
2896 }
2897 else
2898 {
2899 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002900 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002901 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2902 || compl_match_array[i].pum_text
2903 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002904 {
2905 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002906 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002907 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908 }
2909
2910 if (compl_match_array != NULL)
2911 {
2912 /* Compute the screen column of the start of the completed text.
2913 * Use the cursor to get all wrapping and other settings right. */
2914 col = curwin->w_cursor.col;
2915 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002916 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002917 curwin->w_cursor.col = col;
2918 }
2919}
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#define DICT_FIRST (1) /* use just first element in "dict" */
2922#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002925 * Add any identifiers that match the given pattern in the list of dictionary
2926 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
2928 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002929ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2930 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002932 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002933 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002935 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 char_u *ptr;
2937 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 char_u **files;
2940 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002942 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaar0b238792006-03-02 22:49:12 +00002944 if (*dict == NUL)
2945 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002946#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002947 /* When 'dictionary' is empty and spell checking is enabled use
2948 * "spell". */
2949 if (!thesaurus && curwin->w_p_spell)
2950 dict = (char_u *)"spell";
2951 else
2952#endif
2953 return;
2954 }
2955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002957 if (buf == NULL)
2958 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002959 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 /* If 'infercase' is set, don't use 'smartcase' here */
2962 save_p_scs = p_scs;
2963 if (curbuf->b_p_inf)
2964 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002965
2966 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2967 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002968 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002969 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2970 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002971 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002972 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002973
2974 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002975 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002976 len = STRLEN(pat_esc) + 10;
2977 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002978 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002979 {
2980 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002981 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002982 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002983 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002984 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2985 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002986 vim_free(ptr);
2987 }
2988 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002989 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002990 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002991 if (regmatch.regprog == NULL)
2992 goto theend;
2993 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2996 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002997 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 {
2999 /* copy one dictionary file name into buf */
3000 if (flags == DICT_EXACT)
3001 {
3002 count = 1;
3003 files = &dict;
3004 }
3005 else
3006 {
3007 /* Expand wildcards in the dictionary name, but do not allow
3008 * backticks (for security, the 'dict' option may have been set in
3009 * a modeline). */
3010 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003011# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003012 if (!thesaurus && STRCMP(buf, "spell") == 0)
3013 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003014 else
3015# endif
3016 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 || expand_wildcards(1, &buf, &count, &files,
3018 EW_FILE|EW_SILENT) != OK)
3019 count = 0;
3020 }
3021
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003022# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003023 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003025 /* Complete from active spelling. Skip "\<" in the pattern, we
3026 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003027 if (pat[0] == '\\' && pat[1] == '<')
3028 ptr = pat + 2;
3029 else
3030 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003031 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003033 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003034# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003035 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003036 {
3037 ins_compl_files(count, files, thesaurus, flags,
3038 &regmatch, buf, &dir);
3039 if (flags != DICT_EXACT)
3040 FreeWild(count, files);
3041 }
3042 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 break;
3044 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003045
3046theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 p_scs = save_p_scs;
3048 vim_free(regmatch.regprog);
3049 vim_free(buf);
3050}
3051
Bram Moolenaar0b238792006-03-02 22:49:12 +00003052 static void
3053ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3054 int count;
3055 char_u **files;
3056 int thesaurus;
3057 int flags;
3058 regmatch_T *regmatch;
3059 char_u *buf;
3060 int *dir;
3061{
3062 char_u *ptr;
3063 int i;
3064 FILE *fp;
3065 int add_r;
3066
3067 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3068 {
3069 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3070 if (flags != DICT_EXACT)
3071 {
3072 vim_snprintf((char *)IObuff, IOSIZE,
3073 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003074 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003075 }
3076
3077 if (fp != NULL)
3078 {
3079 /*
3080 * Read dictionary file line by line.
3081 * Check each line for a match.
3082 */
3083 while (!got_int && !compl_interrupted
3084 && !vim_fgets(buf, LSIZE, fp))
3085 {
3086 ptr = buf;
3087 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3088 {
3089 ptr = regmatch->startp[0];
3090 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3091 ptr = find_line_end(ptr);
3092 else
3093 ptr = find_word_end(ptr);
3094 add_r = ins_compl_add_infercase(regmatch->startp[0],
3095 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003096 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003097 if (thesaurus)
3098 {
3099 char_u *wstart;
3100
3101 /*
3102 * Add the other matches on the line
3103 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003104 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003105 while (!got_int)
3106 {
3107 /* Find start of the next word. Skip white
3108 * space and punctuation. */
3109 ptr = find_word_start(ptr);
3110 if (*ptr == NUL || *ptr == NL)
3111 break;
3112 wstart = ptr;
3113
Bram Moolenaara2993e12007-08-12 14:38:46 +00003114 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003115#ifdef FEAT_MBYTE
3116 if (has_mbyte)
3117 /* Japanese words may have characters in
3118 * different classes, only separate words
3119 * with single-byte non-word characters. */
3120 while (*ptr != NUL)
3121 {
3122 int l = (*mb_ptr2len)(ptr);
3123
3124 if (l < 2 && !vim_iswordc(*ptr))
3125 break;
3126 ptr += l;
3127 }
3128 else
3129#endif
3130 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003131
3132 /* Add the word. Skip the regexp match. */
3133 if (wstart != regmatch->startp[0])
3134 add_r = ins_compl_add_infercase(wstart,
3135 (int)(ptr - wstart),
3136 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003137 }
3138 }
3139 if (add_r == OK)
3140 /* if dir was BACKWARD then honor it just once */
3141 *dir = FORWARD;
3142 else if (add_r == FAIL)
3143 break;
3144 /* avoid expensive call to vim_regexec() when at end
3145 * of line */
3146 if (*ptr == '\n' || got_int)
3147 break;
3148 }
3149 line_breakcheck();
3150 ins_compl_check_keys(50);
3151 }
3152 fclose(fp);
3153 }
3154 }
3155}
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157/*
3158 * Find the start of the next word.
3159 * Returns a pointer to the first char of the word. Also stops at a NUL.
3160 */
3161 char_u *
3162find_word_start(ptr)
3163 char_u *ptr;
3164{
3165#ifdef FEAT_MBYTE
3166 if (has_mbyte)
3167 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003168 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 else
3170#endif
3171 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3172 ++ptr;
3173 return ptr;
3174}
3175
3176/*
3177 * Find the end of the word. Assumes it starts inside a word.
3178 * Returns a pointer to just after the word.
3179 */
3180 char_u *
3181find_word_end(ptr)
3182 char_u *ptr;
3183{
3184#ifdef FEAT_MBYTE
3185 int start_class;
3186
3187 if (has_mbyte)
3188 {
3189 start_class = mb_get_class(ptr);
3190 if (start_class > 1)
3191 while (*ptr != NUL)
3192 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003193 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (mb_get_class(ptr) != start_class)
3195 break;
3196 }
3197 }
3198 else
3199#endif
3200 while (vim_iswordc(*ptr))
3201 ++ptr;
3202 return ptr;
3203}
3204
3205/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003206 * Find the end of the line, omitting CR and NL at the end.
3207 * Returns a pointer to just after the line.
3208 */
3209 static char_u *
3210find_line_end(ptr)
3211 char_u *ptr;
3212{
3213 char_u *s;
3214
3215 s = ptr + STRLEN(ptr);
3216 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3217 --s;
3218 return s;
3219}
3220
3221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 * Free the list of completions
3223 */
3224 static void
3225ins_compl_free()
3226{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003227 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003228 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003230 vim_free(compl_pattern);
3231 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003232 vim_free(compl_leader);
3233 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003235 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003237
3238 ins_compl_del_pum();
3239 pum_clear();
3240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003241 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 do
3243 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003244 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003245 compl_curr_match = compl_curr_match->cp_next;
3246 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003248 if (match->cp_flags & FREE_FNAME)
3249 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003250 for (i = 0; i < CPT_COUNT; ++i)
3251 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003253 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3254 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003255 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256}
3257
3258 static void
3259ins_compl_clear()
3260{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003261 compl_cont_status = 0;
3262 compl_started = FALSE;
3263 compl_matches = 0;
3264 vim_free(compl_pattern);
3265 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003266 vim_free(compl_leader);
3267 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003269 vim_free(compl_orig_text);
3270 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003271 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272}
3273
3274/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003275 * Return TRUE when Insert completion is active.
3276 */
3277 int
3278ins_compl_active()
3279{
3280 return compl_started;
3281}
3282
3283/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003284 * Delete one character before the cursor and show the subset of the matches
3285 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003286 * Returns the character to be used, NUL if the work is done and another char
3287 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003288 */
3289 static int
3290ins_compl_bs()
3291{
3292 char_u *line;
3293 char_u *p;
3294
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003295 line = ml_get_curline();
3296 p = line + curwin->w_cursor.col;
3297 mb_ptr_back(line, p);
3298
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003299 /* Stop completion when the whole word was deleted. For Omni completion
3300 * allow the word to be deleted, we won't match everything. */
3301 if ((int)(p - line) - (int)compl_col < 0
3302 || ((int)(p - line) - (int)compl_col == 0
3303 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003304 return K_BS;
3305
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003306 /* Deleted more than what was used to find matches or didn't finish
3307 * finding all matches: need to look for matches all over again. */
3308 if (curwin->w_cursor.col <= compl_col + compl_length
3309 || compl_was_interrupted)
3310 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003311
Bram Moolenaara6557602006-02-04 22:43:20 +00003312 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003313 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003314 if (compl_leader != NULL)
3315 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003316 ins_compl_new_leader();
3317 return NUL;
3318 }
3319 return K_BS;
3320}
Bram Moolenaara6557602006-02-04 22:43:20 +00003321
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003322/*
3323 * Called after changing "compl_leader".
3324 * Show the popup menu with a different set of matches.
3325 * May also search for matches again if the previous search was interrupted.
3326 */
3327 static void
3328ins_compl_new_leader()
3329{
3330 ins_compl_del_pum();
3331 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003332 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003333 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003334
3335 if (compl_started)
3336 ins_compl_set_original_text(compl_leader);
3337 else
3338 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003339#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003340 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003341#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003342 /*
3343 * Matches were cleared, need to search for them now. First display
3344 * the changed text before the cursor. Set "compl_restarting" to
3345 * avoid that the first match is inserted.
3346 */
3347 update_screen(0);
3348#ifdef FEAT_GUI
3349 if (gui.in_use)
3350 {
3351 /* Show the cursor after the match, not after the redrawn text. */
3352 setcursor();
3353 out_flush();
3354 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003355 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003356#endif
3357 compl_restarting = TRUE;
3358 if (ins_complete(Ctrl_N) == FAIL)
3359 compl_cont_status = 0;
3360 compl_restarting = FALSE;
3361 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003362
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003363 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003364
3365 /* Show the popup menu with a different set of matches. */
3366 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003367
3368 /* Don't let Enter select the original text when there is no popup menu. */
3369 if (compl_match_array == NULL)
3370 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003371}
3372
3373/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003374 * Return the length of the completion, from the completion start column to
3375 * the cursor column. Making sure it never goes below zero.
3376 */
3377 static int
3378ins_compl_len()
3379{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003380 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003381
3382 if (off < 0)
3383 return 0;
3384 return off;
3385}
3386
3387/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003388 * Append one character to the match leader. May reduce the number of
3389 * matches.
3390 */
3391 static void
3392ins_compl_addleader(c)
3393 int c;
3394{
3395#ifdef FEAT_MBYTE
3396 int cc;
3397
3398 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3399 {
3400 char_u buf[MB_MAXBYTES + 1];
3401
3402 (*mb_char2bytes)(c, buf);
3403 buf[cc] = NUL;
3404 ins_char_bytes(buf, cc);
3405 }
3406 else
3407#endif
3408 ins_char(c);
3409
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003410 /* If we didn't complete finding matches we must search again. */
3411 if (compl_was_interrupted)
3412 ins_compl_restart();
3413
Bram Moolenaara6557602006-02-04 22:43:20 +00003414 vim_free(compl_leader);
3415 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003416 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003417 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003418 ins_compl_new_leader();
3419}
3420
3421/*
3422 * Setup for finding completions again without leaving CTRL-X mode. Used when
3423 * BS or a key was typed while still searching for matches.
3424 */
3425 static void
3426ins_compl_restart()
3427{
3428 ins_compl_free();
3429 compl_started = FALSE;
3430 compl_matches = 0;
3431 compl_cont_status = 0;
3432 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003433}
3434
3435/*
3436 * Set the first match, the original text.
3437 */
3438 static void
3439ins_compl_set_original_text(str)
3440 char_u *str;
3441{
3442 char_u *p;
3443
3444 /* Replace the original text entry. */
3445 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3446 {
3447 p = vim_strsave(str);
3448 if (p != NULL)
3449 {
3450 vim_free(compl_first_match->cp_str);
3451 compl_first_match->cp_str = p;
3452 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 }
3454}
3455
3456/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003457 * Append one character to the match leader. May reduce the number of
3458 * matches.
3459 */
3460 static void
3461ins_compl_addfrommatch()
3462{
3463 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003464 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003465 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003466 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003467
3468 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003469 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003470 {
3471 /* When still at the original match use the first entry that matches
3472 * the leader. */
3473 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3474 {
3475 p = NULL;
3476 for (cp = compl_shown_match->cp_next; cp != NULL
3477 && cp != compl_first_match; cp = cp->cp_next)
3478 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003479 if (compl_leader == NULL
3480 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003481 (int)STRLEN(compl_leader)))
3482 {
3483 p = cp->cp_str;
3484 break;
3485 }
3486 }
3487 if (p == NULL || (int)STRLEN(p) <= len)
3488 return;
3489 }
3490 else
3491 return;
3492 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003493 p += len;
3494#ifdef FEAT_MBYTE
3495 c = mb_ptr2char(p);
3496#else
3497 c = *p;
3498#endif
3499 ins_compl_addleader(c);
3500}
3501
3502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003504 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003505 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003507 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508ins_compl_prep(c)
3509 int c;
3510{
3511 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003513 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /* Forget any previous 'special' messages if this is actually
3516 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3517 */
3518 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3519 edit_submode_extra = NULL;
3520
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003521 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003522 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3523 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003524 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003526 /* Set "compl_get_longest" when finding the first matches. */
3527 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3528 || (ctrl_x_mode == 0 && !compl_started))
3529 {
3530 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3531 compl_used_match = TRUE;
3532 }
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3535 {
3536 /*
3537 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3538 * it will be yet. Now we decide.
3539 */
3540 switch (c)
3541 {
3542 case Ctrl_E:
3543 case Ctrl_Y:
3544 ctrl_x_mode = CTRL_X_SCROLL;
3545 if (!(State & REPLACE_FLAG))
3546 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3547 else
3548 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3549 edit_submode_pre = NULL;
3550 showmode();
3551 break;
3552 case Ctrl_L:
3553 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3554 break;
3555 case Ctrl_F:
3556 ctrl_x_mode = CTRL_X_FILES;
3557 break;
3558 case Ctrl_K:
3559 ctrl_x_mode = CTRL_X_DICTIONARY;
3560 break;
3561 case Ctrl_R:
3562 /* Simply allow ^R to happen without affecting ^X mode */
3563 break;
3564 case Ctrl_T:
3565 ctrl_x_mode = CTRL_X_THESAURUS;
3566 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003567#ifdef FEAT_COMPL_FUNC
3568 case Ctrl_U:
3569 ctrl_x_mode = CTRL_X_FUNCTION;
3570 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003572 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003573 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003574#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003575 case 's':
3576 case Ctrl_S:
3577 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003578#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003579 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003580 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003581 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003582#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003583 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 case Ctrl_RSB:
3585 ctrl_x_mode = CTRL_X_TAGS;
3586 break;
3587#ifdef FEAT_FIND_ID
3588 case Ctrl_I:
3589 case K_S_TAB:
3590 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3591 break;
3592 case Ctrl_D:
3593 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3594 break;
3595#endif
3596 case Ctrl_V:
3597 case Ctrl_Q:
3598 ctrl_x_mode = CTRL_X_CMDLINE;
3599 break;
3600 case Ctrl_P:
3601 case Ctrl_N:
3602 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3603 * just started ^X mode, or there were enough ^X's to cancel
3604 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3605 * do normal expansion when interrupting a different mode (say
3606 * ^X^F^X^P or ^P^X^X^P, see below)
3607 * nothing changes if interrupting mode 0, (eg, the flag
3608 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 if (!(compl_cont_status & CONT_INTRPT))
3610 compl_cont_status |= CONT_LOCAL;
3611 else if (compl_cont_mode != 0)
3612 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 /* FALLTHROUGH */
3614 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003615 /* If we have typed at least 2 ^X's... for modes != 0, we set
3616 * compl_cont_status = 0 (eg, as if we had just started ^X
3617 * mode).
3618 * For mode 0, we set "compl_cont_mode" to an impossible
3619 * value, in both cases ^X^X can be used to restart the same
3620 * mode (avoiding ADDING mode).
3621 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3622 * 'complete' and local ^P expansions respectively.
3623 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3624 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (c == Ctrl_X)
3626 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003627 if (compl_cont_mode != 0)
3628 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003630 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632 ctrl_x_mode = 0;
3633 edit_submode = NULL;
3634 showmode();
3635 break;
3636 }
3637 }
3638 else if (ctrl_x_mode != 0)
3639 {
3640 /* We're already in CTRL-X mode, do we stay in it? */
3641 if (!vim_is_ctrl_x_key(c))
3642 {
3643 if (ctrl_x_mode == CTRL_X_SCROLL)
3644 ctrl_x_mode = 0;
3645 else
3646 ctrl_x_mode = CTRL_X_FINISHED;
3647 edit_submode = NULL;
3648 }
3649 showmode();
3650 }
3651
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003652 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 /* Show error message from attempted keyword completion (probably
3655 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003658 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3659 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 || ctrl_x_mode == CTRL_X_FINISHED)
3661 {
3662 /* Get here when we have finished typing a sequence of ^N and
3663 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003665 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003667 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003668 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003671 * If any of the original typed text has been changed, eg when
3672 * ignorecase is set, we must add back-spaces to the redo
3673 * buffer. We add as few as necessary to delete just the part
3674 * of the original text that has changed.
3675 * When using the longest match, edited the match or used
3676 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003678 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3679 ptr = compl_curr_match->cp_str;
3680 else if (compl_leader != NULL)
3681 ptr = compl_leader;
3682 else
3683 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003684 if (compl_orig_text != NULL)
3685 {
3686 p = compl_orig_text;
3687 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3688 ++temp)
3689 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003690#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003691 if (temp > 0)
3692 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003693#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003694 for (p += temp; *p != NUL; mb_ptr_adv(p))
3695 AppendCharToRedobuff(K_BS);
3696 }
3697 if (ptr != NULL)
3698 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700
3701#ifdef FEAT_CINDENT
3702 want_cindent = (can_cindent && cindent_on());
3703#endif
3704 /*
3705 * When completing whole lines: fix indent for 'cindent'.
3706 * Otherwise, break line if it's too long.
3707 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003708 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
3710#ifdef FEAT_CINDENT
3711 /* re-indent the current line */
3712 if (want_cindent)
3713 {
3714 do_c_expr_indent();
3715 want_cindent = FALSE; /* don't do it again */
3716 }
3717#endif
3718 }
3719 else
3720 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003721 int prev_col = curwin->w_cursor.col;
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003724 if (prev_col > 0)
3725 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (stop_arrow() == OK)
3727 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003728 if (prev_col > 0
3729 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3730 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003733 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003734 * the selection without inserting anything. When
3735 * compl_enter_selects is set the Enter key does the same. */
3736 if ((c == Ctrl_Y || (compl_enter_selects
3737 && (c == CAR || c == K_KENTER || c == NL)))
3738 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003739 retval = TRUE;
3740
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003741 /* CTRL-E means completion is Ended, go back to the typed text. */
3742 if (c == Ctrl_E)
3743 {
3744 ins_compl_delete();
3745 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003746 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003747 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003748 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003749 retval = TRUE;
3750 }
3751
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003752 auto_format(FALSE, TRUE);
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003755 compl_started = FALSE;
3756 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 msg_clr_cmdline(); /* necessary for "noshowmode" */
3758 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003759 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (edit_submode != NULL)
3761 {
3762 edit_submode = NULL;
3763 showmode();
3764 }
3765
3766#ifdef FEAT_CINDENT
3767 /*
3768 * Indent now if a key was typed that is in 'cinkeys'.
3769 */
3770 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3771 do_c_expr_indent();
3772#endif
3773 }
3774 }
3775
3776 /* reset continue_* if we left expansion-mode, if we stay they'll be
3777 * (re)set properly in ins_complete() */
3778 if (!vim_is_ctrl_x_key(c))
3779 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 compl_cont_status = 0;
3781 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003783
3784 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786
3787/*
3788 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3789 * (depending on flag) starting from buf and looking for a non-scanned
3790 * buffer (other than curbuf). curbuf is special, if it is called with
3791 * buf=curbuf then it has to be the first call for a given flag/expansion.
3792 *
3793 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3794 */
3795 static buf_T *
3796ins_compl_next_buf(buf, flag)
3797 buf_T *buf;
3798 int flag;
3799{
3800#ifdef FEAT_WINDOWS
3801 static win_T *wp;
3802#endif
3803
3804 if (flag == 'w') /* just windows */
3805 {
3806#ifdef FEAT_WINDOWS
3807 if (buf == curbuf) /* first call for this flag/expansion */
3808 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003809 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 && wp->w_buffer->b_scanned)
3811 ;
3812 buf = wp->w_buffer;
3813#else
3814 buf = curbuf;
3815#endif
3816 }
3817 else
3818 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3819 * (unlisted buffers)
3820 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003821 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 && ((flag == 'U'
3823 ? buf->b_p_bl
3824 : (!buf->b_p_bl
3825 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 ;
3828 return buf;
3829}
3830
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003831#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003832static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003833
3834/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003835 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003836 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003837 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003838 static void
3839expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003840 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003841 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003842{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003843 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003844 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003845 char_u *funcname;
3846 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003847 win_T *curwin_save;
3848 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003849
Bram Moolenaare344bea2005-09-01 20:46:49 +00003850 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3851 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003852 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003853
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003854 /* Call 'completefunc' to obtain the list of matches. */
3855 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003856 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003857
Bram Moolenaare344bea2005-09-01 20:46:49 +00003858 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003859 curwin_save = curwin;
3860 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003861 matchlist = call_func_retlist(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003862 if (curwin_save != curwin || curbuf_save != curbuf)
3863 {
3864 EMSG(_(e_complwin));
3865 goto theend;
3866 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003867 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003868 check_cursor();
3869 if (!equalpos(curwin->w_cursor, pos))
3870 {
3871 EMSG(_(e_compldel));
3872 goto theend;
3873 }
3874 if (matchlist != NULL)
3875 ins_compl_add_list(matchlist);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003876
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003877theend:
3878 if (matchlist != NULL)
3879 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003880}
3881#endif /* FEAT_COMPL_FUNC */
3882
Bram Moolenaar39f05632006-03-19 22:15:26 +00003883#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003884/*
3885 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003886 */
3887 static void
3888ins_compl_add_list(list)
3889 list_T *list;
3890{
3891 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003892 int dir = compl_direction;
3893
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003894 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003895 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003896 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003897 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3898 /* if dir was BACKWARD then honor it just once */
3899 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003900 else if (did_emsg)
3901 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003902 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003903}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003904
3905/*
3906 * Add a match to the list of matches from a typeval_T.
3907 * If the given string is already in the list of completions, then return
3908 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3909 * maybe because alloc() returns NULL, then FAIL is returned.
3910 */
3911 int
3912ins_compl_add_tv(tv, dir)
3913 typval_T *tv;
3914 int dir;
3915{
3916 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003917 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003918 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01003919 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003920 char_u *(cptext[CPT_COUNT]);
3921
3922 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3923 {
3924 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3925 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3926 (char_u *)"abbr", FALSE);
3927 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3928 (char_u *)"menu", FALSE);
3929 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3930 (char_u *)"kind", FALSE);
3931 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3932 (char_u *)"info", FALSE);
3933 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3934 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003935 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003936 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01003937 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
3938 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003939 }
3940 else
3941 {
3942 word = get_tv_string_chk(tv);
3943 vim_memset(cptext, 0, sizeof(cptext));
3944 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01003945 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003946 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003947 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003948}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003949#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003950
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951/*
3952 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003953 * The search starts at position "ini" in curbuf and in the direction
3954 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003955 * When "compl_started" is FALSE start at that position, otherwise continue
3956 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957 * This may return before finding all the matches.
3958 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 */
3960 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003961ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
3964 static pos_T first_match_pos;
3965 static pos_T last_match_pos;
3966 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003967 static int found_all = FALSE; /* Found all matches of a
3968 certain type. */
3969 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
Bram Moolenaar572cb562005-08-05 21:35:02 +00003971 pos_T *pos;
3972 char_u **matches;
3973 int save_p_scs;
3974 int save_p_ws;
3975 int save_p_ic;
3976 int i;
3977 int num_matches;
3978 int len;
3979 int found_new_match;
3980 int type = ctrl_x_mode;
3981 char_u *ptr;
3982 char_u *dict = NULL;
3983 int dict_f = 0;
3984 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
3988 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3989 ins_buf->b_scanned = 0;
3990 found_all = FALSE;
3991 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003992 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003993 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 last_match_pos = first_match_pos = *ini;
3995 }
3996
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003997 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003998 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4000 for (;;)
4001 {
4002 found_new_match = FAIL;
4003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004004 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * or if found_all says this entry is done. For ^X^L only use the
4006 * entries from 'complete' that look in loaded buffers. */
4007 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004008 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
4010 found_all = FALSE;
4011 while (*e_cpt == ',' || *e_cpt == ' ')
4012 e_cpt++;
4013 if (*e_cpt == '.' && !curbuf->b_scanned)
4014 {
4015 ins_buf = curbuf;
4016 first_match_pos = *ini;
4017 /* So that ^N can match word immediately after cursor */
4018 if (ctrl_x_mode == 0)
4019 dec(&first_match_pos);
4020 last_match_pos = first_match_pos;
4021 type = 0;
4022 }
4023 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4024 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4025 {
4026 /* Scan a buffer, but not the current one. */
4027 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4028 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 first_match_pos.col = last_match_pos.col = 0;
4031 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4032 last_match_pos.lnum = 0;
4033 type = 0;
4034 }
4035 else /* unloaded buffer, scan like dictionary */
4036 {
4037 found_all = TRUE;
4038 if (ins_buf->b_fname == NULL)
4039 continue;
4040 type = CTRL_X_DICTIONARY;
4041 dict = ins_buf->b_fname;
4042 dict_f = DICT_EXACT;
4043 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004044 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 ins_buf->b_fname == NULL
4046 ? buf_spname(ins_buf)
4047 : ins_buf->b_sfname == NULL
4048 ? (char *)ins_buf->b_fname
4049 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004050 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052 else if (*e_cpt == NUL)
4053 break;
4054 else
4055 {
4056 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4057 type = -1;
4058 else if (*e_cpt == 'k' || *e_cpt == 's')
4059 {
4060 if (*e_cpt == 'k')
4061 type = CTRL_X_DICTIONARY;
4062 else
4063 type = CTRL_X_THESAURUS;
4064 if (*++e_cpt != ',' && *e_cpt != NUL)
4065 {
4066 dict = e_cpt;
4067 dict_f = DICT_FIRST;
4068 }
4069 }
4070#ifdef FEAT_FIND_ID
4071 else if (*e_cpt == 'i')
4072 type = CTRL_X_PATH_PATTERNS;
4073 else if (*e_cpt == 'd')
4074 type = CTRL_X_PATH_DEFINES;
4075#endif
4076 else if (*e_cpt == ']' || *e_cpt == 't')
4077 {
4078 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004079 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004080 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 else
4083 type = -1;
4084
4085 /* in any case e_cpt is advanced to the next entry */
4086 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4087
4088 found_all = TRUE;
4089 if (type == -1)
4090 continue;
4091 }
4092 }
4093
4094 switch (type)
4095 {
4096 case -1:
4097 break;
4098#ifdef FEAT_FIND_ID
4099 case CTRL_X_PATH_PATTERNS:
4100 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004101 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004102 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4106 (linenr_T)1, (linenr_T)MAXLNUM);
4107 break;
4108#endif
4109
4110 case CTRL_X_DICTIONARY:
4111 case CTRL_X_THESAURUS:
4112 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004113 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 : (type == CTRL_X_THESAURUS
4115 ? (*curbuf->b_p_tsr == NUL
4116 ? p_tsr
4117 : curbuf->b_p_tsr)
4118 : (*curbuf->b_p_dict == NUL
4119 ? p_dict
4120 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004121 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004122 dict != NULL ? dict_f
4123 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 dict = NULL;
4125 break;
4126
4127 case CTRL_X_TAGS:
4128 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4129 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004132 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 * of matches is found when compl_pattern is empty */
4134 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4136 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4137 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4138 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004139 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 p_ic = save_p_ic;
4142 break;
4143
4144 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4147 {
4148
4149 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004151 ins_compl_add_matches(num_matches, matches,
4152#ifdef CASE_INSENSITIVE_FILENAME
4153 TRUE
4154#else
4155 FALSE
4156#endif
4157 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 break;
4160
4161 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004162 if (expand_cmdline(&compl_xp, compl_pattern,
4163 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004165 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 break;
4167
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004168#ifdef FEAT_COMPL_FUNC
4169 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004170 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004171 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004172 break;
4173#endif
4174
Bram Moolenaar488c6512005-08-11 20:09:58 +00004175 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004176#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004177 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004178 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004179 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004180 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004181#endif
4182 break;
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 default: /* normal ^P/^N and ^X^L */
4185 /*
4186 * If 'infercase' is set, don't use 'smartcase' here
4187 */
4188 save_p_scs = p_scs;
4189 if (ins_buf->b_p_inf)
4190 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 /* buffers other than curbuf are scanned from the beginning or the
4193 * end but never from the middle, thus setting nowrapscan in this
4194 * buffers is a good idea, on the other hand, we always set
4195 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4196 save_p_ws = p_ws;
4197 if (ins_buf != curbuf)
4198 p_ws = FALSE;
4199 else if (*e_cpt == '.')
4200 p_ws = TRUE;
4201 for (;;)
4202 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004203 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004205 ++msg_silent; /* Don't want messages for wrapscan. */
4206
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004207 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4208 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004212 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004214 found_new_match = searchit(NULL, ins_buf, pos,
4215 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004217 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004218 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004221 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004222 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 first_match_pos = *pos;
4224 last_match_pos = *pos;
4225 }
4226 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004227 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 found_new_match = FAIL;
4229 if (found_new_match == FAIL)
4230 {
4231 if (ins_buf == curbuf)
4232 found_all = TRUE;
4233 break;
4234 }
4235
4236 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 && ini->lnum == pos->lnum
4239 && ini->col == pos->col)
4240 continue;
4241 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4242 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4243 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4247 continue;
4248 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4249 if (!p_paste)
4250 ptr = skipwhite(ptr);
4251 }
4252 len = (int)STRLEN(ptr);
4253 }
4254 else
4255 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 char_u *tmp_ptr = ptr;
4257
4258 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 /* Skip if already inside a word. */
4262 if (vim_iswordp(tmp_ptr))
4263 continue;
4264 /* Find start of next word. */
4265 tmp_ptr = find_word_start(tmp_ptr);
4266 }
4267 /* Find end of this word. */
4268 tmp_ptr = find_word_end(tmp_ptr);
4269 len = (int)(tmp_ptr - ptr);
4270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 if ((compl_cont_status & CONT_ADDING)
4272 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4275 {
4276 /* Try next line, if any. the new word will be
4277 * "join" as if the normal command "J" was used.
4278 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 * works -- Acevedo */
4281 STRNCPY(IObuff, ptr, len);
4282 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4283 tmp_ptr = ptr = skipwhite(ptr);
4284 /* Find start of next word. */
4285 tmp_ptr = find_word_start(tmp_ptr);
4286 /* Find end of next word. */
4287 tmp_ptr = find_word_end(tmp_ptr);
4288 if (tmp_ptr > ptr)
4289 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004290 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004292 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 IObuff[len++] = ' ';
4294 /* IObuf =~ "\k.* ", thus len >= 2 */
4295 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004296 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 || (vim_strchr(p_cpo, CPO_JOINSP)
4298 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004299 && (IObuff[len - 2] == '?'
4300 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 IObuff[len++] = ' ';
4302 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004303 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (tmp_ptr - ptr >= IOSIZE - len)
4305 tmp_ptr = ptr + IOSIZE - len - 1;
4306 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4307 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004308 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 IObuff[len] = NUL;
4311 ptr = IObuff;
4312 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 continue;
4315 }
4316 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004317 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004318 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004319 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 found_new_match = OK;
4322 break;
4323 }
4324 }
4325 p_scs = save_p_scs;
4326 p_ws = save_p_ws;
4327 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004330 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004331 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 found_new_match = OK;
4333
4334 /* break the loop for specialized modes (use 'complete' just for the
4335 * generic ctrl_x_mode == 0) or when we've found a new match */
4336 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004338 {
4339 if (got_int)
4340 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004341 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004342 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004343 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004344
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004345 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4346 || compl_interrupted)
4347 break;
4348 compl_started = TRUE;
4349 }
4350 else
4351 {
4352 /* Mark a buffer scanned when it has been scanned completely */
4353 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4354 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004356 compl_started = FALSE;
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4362 && *e_cpt == NUL) /* Got to end of 'complete' */
4363 found_new_match = FAIL;
4364
4365 i = -1; /* total of matches, unknown */
4366 if (found_new_match == FAIL
4367 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4368 i = ins_compl_make_cyclic();
4369
4370 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 * just been made cyclic then we have to move compl_curr_match to the next
4372 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004373 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4374 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 if (compl_curr_match == NULL)
4376 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 return i;
4378}
4379
4380/* Delete the old text being completed. */
4381 static void
4382ins_compl_delete()
4383{
4384 int i;
4385
4386 /*
4387 * In insert mode: Delete the typed part.
4388 * In replace mode: Put the old characters back, if any.
4389 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 backspace_until_column(i);
4392 changed_cline_bef_curs();
4393}
4394
4395/* Insert the new text being completed. */
4396 static void
4397ins_compl_insert()
4398{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004399 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004400 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4401 compl_used_match = FALSE;
4402 else
4403 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404}
4405
4406/*
4407 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004408 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4409 * get more completions. If it is FALSE, then we just do nothing when there
4410 * are no more completions in a given direction. The latter case is used when
4411 * we are still in the middle of finding completions, to allow browsing
4412 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 * Return the total number of matches, or -1 if still unknown -- webb.
4414 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4416 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 *
4418 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004419 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4420 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
4422 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004423ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004425 int count; /* repeat completion this many times; should
4426 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004427 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 int num_matches = -1;
4430 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004431 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004432 compl_T *found_compl = NULL;
4433 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004434 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004436 if (compl_leader != NULL
4437 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004439 /* Set "compl_shown_match" to the actually shown match, it may differ
4440 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004441 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004442 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004443 && compl_shown_match->cp_next != NULL
4444 && compl_shown_match->cp_next != compl_first_match)
4445 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004446
4447 /* If we didn't find it searching forward, and compl_shows_dir is
4448 * backward, find the last match. */
4449 if (compl_shows_dir == BACKWARD
4450 && !ins_compl_equal(compl_shown_match,
4451 compl_leader, (int)STRLEN(compl_leader))
4452 && (compl_shown_match->cp_next == NULL
4453 || compl_shown_match->cp_next == compl_first_match))
4454 {
4455 while (!ins_compl_equal(compl_shown_match,
4456 compl_leader, (int)STRLEN(compl_leader))
4457 && compl_shown_match->cp_prev != NULL
4458 && compl_shown_match->cp_prev != compl_first_match)
4459 compl_shown_match = compl_shown_match->cp_prev;
4460 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004461 }
4462
4463 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004464 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 /* Delete old text to be replaced */
4466 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004467
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004468 /* When finding the longest common text we stick at the original text,
4469 * don't let CTRL-N or CTRL-P move to the first match. */
4470 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4471
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004472 /* When restarting the search don't insert the first match either. */
4473 if (compl_restarting)
4474 {
4475 advance = FALSE;
4476 compl_restarting = FALSE;
4477 }
4478
Bram Moolenaare3226be2005-12-18 22:10:00 +00004479 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4480 * around. */
4481 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004483 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004485 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004486 found_end = (compl_first_match != NULL
4487 && (compl_shown_match->cp_next == compl_first_match
4488 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004489 }
4490 else if (compl_shows_dir == BACKWARD
4491 && compl_shown_match->cp_prev != NULL)
4492 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004493 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004494 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004495 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004498 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004499 if (!allow_get_expansion)
4500 {
4501 if (advance)
4502 {
4503 if (compl_shows_dir == BACKWARD)
4504 compl_pending -= todo + 1;
4505 else
4506 compl_pending += todo + 1;
4507 }
4508 return -1;
4509 }
4510
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004511 if (advance)
4512 {
4513 if (compl_shows_dir == BACKWARD)
4514 --compl_pending;
4515 else
4516 ++compl_pending;
4517 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004518
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004519 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004520 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004521
4522 /* handle any pending completions */
4523 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004524 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004525 {
4526 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4527 {
4528 compl_shown_match = compl_shown_match->cp_next;
4529 --compl_pending;
4530 }
4531 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4532 {
4533 compl_shown_match = compl_shown_match->cp_prev;
4534 ++compl_pending;
4535 }
4536 else
4537 break;
4538 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004539 found_end = FALSE;
4540 }
4541 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4542 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004543 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004544 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004545 ++todo;
4546 else
4547 /* Remember a matching item. */
4548 found_compl = compl_shown_match;
4549
4550 /* Stop at the end of the list when we found a usable match. */
4551 if (found_end)
4552 {
4553 if (found_compl != NULL)
4554 {
4555 compl_shown_match = found_compl;
4556 break;
4557 }
4558 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004562 /* Insert the text of the new completion, or the compl_leader. */
4563 if (insert_match)
4564 {
4565 if (!compl_get_longest || compl_used_match)
4566 ins_compl_insert();
4567 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004568 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004569 }
4570 else
4571 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 if (!allow_get_expansion)
4574 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004575 /* may undisplay the popup menu first */
4576 ins_compl_upd_pum();
4577
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004578 /* redraw to show the user what was inserted */
4579 update_screen(0);
4580
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004581 /* display the updated popup menu */
4582 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004583#ifdef FEAT_GUI
4584 if (gui.in_use)
4585 {
4586 /* Show the cursor after the match, not after the redrawn text. */
4587 setcursor();
4588 out_flush();
4589 gui_update_cursor(FALSE, FALSE);
4590 }
4591#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 /* Delete old text to be replaced, since we're still searching and
4594 * don't want to match ourselves! */
4595 ins_compl_delete();
4596 }
4597
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004598 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004599 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004600 compl_enter_selects = !insert_match && compl_match_array != NULL;
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 /*
4603 * Show the file name for the match (if any)
4604 * Truncate the file name to avoid a wait for return.
4605 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004606 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004609 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (i <= 0)
4611 i = 0;
4612 else
4613 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004614 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 msg(IObuff);
4616 redraw_cmdline = FALSE; /* don't overwrite! */
4617 }
4618
4619 return num_matches;
4620}
4621
4622/*
4623 * Call this while finding completions, to check whether the user has hit a key
4624 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004625 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004627 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 */
4629 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004630ins_compl_check_keys(frequency)
4631 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 static int count = 0;
4634
4635 int c;
4636
4637 /* Don't check when reading keys from a script. That would break the test
4638 * scripts */
4639 if (using_script())
4640 return;
4641
4642 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004643 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return;
4645 count = 0;
4646
Bram Moolenaara260a972006-06-23 19:36:29 +00004647 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4648 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (c != NUL)
4651 {
4652 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4653 {
4654 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004655 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004656 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4657 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004659 else
4660 {
4661 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004662 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004663 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004664 if (c != K_IGNORE)
4665 {
4666 /* Don't interrupt completion when the character wasn't typed,
4667 * e.g., when doing @q to replay keys. */
4668 if (c != Ctrl_R && KeyTyped)
4669 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004670
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004671 vungetc(c);
4672 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004675 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004676 {
4677 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4678
4679 compl_pending = 0;
4680 (void)ins_compl_next(FALSE, todo, TRUE);
4681 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004682}
4683
4684/*
4685 * Decide the direction of Insert mode complete from the key typed.
4686 * Returns BACKWARD or FORWARD.
4687 */
4688 static int
4689ins_compl_key2dir(c)
4690 int c;
4691{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004692 if (c == Ctrl_P || c == Ctrl_L
4693 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4694 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004695 return BACKWARD;
4696 return FORWARD;
4697}
4698
4699/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004700 * Return TRUE for keys that are used for completion only when the popup menu
4701 * is visible.
4702 */
4703 static int
4704ins_compl_pum_key(c)
4705 int c;
4706{
4707 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004708 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4709 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004710}
4711
4712/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004713 * Decide the number of completions to move forward.
4714 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4715 */
4716 static int
4717ins_compl_key2count(c)
4718 int c;
4719{
4720 int h;
4721
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004722 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004723 {
4724 h = pum_get_height();
4725 if (h > 3)
4726 h -= 2; /* keep some context */
4727 return h;
4728 }
4729 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
4732/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004733 * Return TRUE if completion with "c" should insert the match, FALSE if only
4734 * to change the currently selected completion.
4735 */
4736 static int
4737ins_compl_use_match(c)
4738 int c;
4739{
4740 switch (c)
4741 {
4742 case K_UP:
4743 case K_DOWN:
4744 case K_PAGEDOWN:
4745 case K_KPAGEDOWN:
4746 case K_S_DOWN:
4747 case K_PAGEUP:
4748 case K_KPAGEUP:
4749 case K_S_UP:
4750 return FALSE;
4751 }
4752 return TRUE;
4753}
4754
4755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 * Do Insert mode completion.
4757 * Called when character "c" was typed, which has a meaning for completion.
4758 * Returns OK if completion was done, FAIL if something failed (out of mem).
4759 */
4760 static int
4761ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004762 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 char_u *line;
4765 int startcol = 0; /* column where searched text starts */
4766 colnr_T curs_col; /* cursor column */
4767 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004768 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
Bram Moolenaare3226be2005-12-18 22:10:00 +00004770 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
4773 /* First time we hit ^N or ^P (in a row, I mean) */
4774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 did_ai = FALSE;
4776#ifdef FEAT_SMARTINDENT
4777 did_si = FALSE;
4778 can_si = FALSE;
4779 can_si_back = FALSE;
4780#endif
4781 if (stop_arrow() == FAIL)
4782 return FAIL;
4783
4784 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004786 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004788 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004789 * "compl_startpos" to the cursor as a pattern to add a new word
4790 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004791 * "compl_startpos" is not in the same line as the cursor then fix it
4792 * (the line has been split because it was longer than 'tw'). if SOL
4793 * is set then skip the previous pattern, a word at the beginning of
4794 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004795 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4796 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 /*
4799 * it is a continued search
4800 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4803 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4804 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004807 /* line (probably) wrapped, set compl_startpos to the
4808 * first non_blank in the line, if it is not a wordchar
4809 * include it to get a better pattern, but then we don't
4810 * want the "\\<" prefix, check it bellow */
4811 compl_col = (colnr_T)(skipwhite(line) - line);
4812 compl_startpos.col = compl_col;
4813 compl_startpos.lnum = curwin->w_cursor.lnum;
4814 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 else
4817 {
4818 /* S_IPOS was set when we inserted a word that was at the
4819 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004820 * mode but first we need to redefine compl_startpos */
4821 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 compl_cont_status |= CONT_SOL;
4824 compl_startpos.col = (colnr_T)(skipwhite(
4825 line + compl_length
4826 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004828 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004830 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004831 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004832 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004834 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 compl_cont_status &= ~CONT_SOL;
4837 compl_length = (IOSIZE - MIN_SPACE);
4838 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004840 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4841 if (compl_length < 1)
4842 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004845 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004847 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004850 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004854 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004856 compl_cont_status = 0;
4857 compl_cont_status |= CONT_N_ADDS;
4858 compl_startpos = curwin->w_cursor;
4859 startcol = (int)curs_col;
4860 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862
4863 /* Work out completion pattern and original text -- webb */
4864 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4865 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004866 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4868 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004869 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004873 compl_col += ++startcol;
4874 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 compl_pattern = str_foldcase(line + compl_col,
4878 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004880 compl_pattern = vim_strnsave(line + compl_col,
4881 compl_length);
4882 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return FAIL;
4884 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004885 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
4887 char_u *prefix = (char_u *)"\\<";
4888
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004889 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004890 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004891 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 if (!vim_iswordp(line + compl_col)
4895 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 && (
4897#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004900 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
4902 )))
4903 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 STRCPY((char *)compl_pattern, prefix);
4905 (void)quote_meta(compl_pattern + STRLEN(prefix),
4906 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004908 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004912 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
4914 )
4915 {
4916 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004917 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4918 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004920 compl_col += curs_col;
4921 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else
4924 {
4925#ifdef FEAT_MBYTE
4926 /* Search the point of change class of multibyte character
4927 * or not a word single byte character backward. */
4928 if (has_mbyte)
4929 {
4930 int base_class;
4931 int head_off;
4932
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004933 startcol -= (*mb_head_off)(line, line + startcol);
4934 base_class = mb_get_class(line + startcol);
4935 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004937 head_off = (*mb_head_off)(line, line + startcol);
4938 if (base_class != mb_get_class(line + startcol
4939 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004941 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 }
4944 else
4945#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004946 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004948 compl_col += ++startcol;
4949 compl_length = (int)curs_col - startcol;
4950 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
4952 /* Only match word with at least two chars -- webb
4953 * there's no need to call quote_meta,
4954 * alloc(7) is enough -- Acevedo
4955 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004956 compl_pattern = alloc(7);
4957 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004959 STRCPY((char *)compl_pattern, "\\<");
4960 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4961 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else
4964 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004965 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004966 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004967 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004969 STRCPY((char *)compl_pattern, "\\<");
4970 (void)quote_meta(compl_pattern + 2, line + compl_col,
4971 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 }
4974 }
4975 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4976 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004977 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004978 compl_length = (int)curs_col - (int)compl_col;
4979 if (compl_length < 0) /* cursor in indent: empty pattern */
4980 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004982 compl_pattern = str_foldcase(line + compl_col, compl_length,
4983 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004985 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4986 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 return FAIL;
4988 }
4989 else if (ctrl_x_mode == CTRL_X_FILES)
4990 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004991 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004993 compl_col += ++startcol;
4994 compl_length = (int)curs_col - startcol;
4995 compl_pattern = addstar(line + compl_col, compl_length,
4996 EXPAND_FILES);
4997 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return FAIL;
4999 }
5000 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5001 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005002 compl_pattern = vim_strnsave(line, curs_col);
5003 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005005 set_cmd_context(&compl_xp, compl_pattern,
5006 (int)STRLEN(compl_pattern), curs_col);
5007 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5008 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005009 /* No completion possible, use an empty pattern to get a
5010 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005011 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005012 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005013 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5014 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005016 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005017 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005018#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005019 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005020 * Call user defined function 'completefunc' with "a:findstart"
5021 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005022 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005023 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005024 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005025 char_u *funcname;
5026 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005027 win_T *curwin_save;
5028 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005029
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005030 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005031 * string */
5032 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5033 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5034 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005035 {
5036 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5037 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005038 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005039 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005040
5041 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005042 args[1] = NULL;
5043 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005044 curwin_save = curwin;
5045 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005046 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005047 if (curwin_save != curwin || curbuf_save != curbuf)
5048 {
5049 EMSG(_(e_complwin));
5050 return FAIL;
5051 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005052 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005053 check_cursor();
5054 if (!equalpos(curwin->w_cursor, pos))
5055 {
5056 EMSG(_(e_compldel));
5057 return FAIL;
5058 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005059
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005060 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005061 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005062 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005063 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005064 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 /* Setup variables for completion. Need to obtain "line" again,
5067 * it may have become invalid. */
5068 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005069 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5071 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005072#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 return FAIL;
5074 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005075 else if (ctrl_x_mode == CTRL_X_SPELL)
5076 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005077#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005078 if (spell_bad_len > 0)
5079 compl_col = curs_col - spell_bad_len;
5080 else
5081 compl_col = spell_word_start(startcol);
5082 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005083 {
5084 compl_length = 0;
5085 compl_col = curs_col;
5086 }
5087 else
5088 {
5089 spell_expand_check_cap(compl_col);
5090 compl_length = (int)curs_col - compl_col;
5091 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005092 /* Need to obtain "line" again, it may have become invalid. */
5093 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005094 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5095 if (compl_pattern == NULL)
5096#endif
5097 return FAIL;
5098 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 else
5100 {
5101 EMSG2(_(e_intern2), "ins_complete()");
5102 return FAIL;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
5107 edit_submode_pre = (char_u *)_(" Adding");
5108 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5109 {
5110 /* Insert a new line, keep indentation but ignore 'comments' */
5111#ifdef FEAT_COMMENTS
5112 char_u *old = curbuf->b_p_com;
5113
5114 curbuf->b_p_com = (char_u *)"";
5115#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 compl_startpos.lnum = curwin->w_cursor.lnum;
5117 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 ins_eol('\r');
5119#ifdef FEAT_COMMENTS
5120 curbuf->b_p_com = old;
5121#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_length = 0;
5123 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 }
5126 else
5127 {
5128 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 if (compl_cont_status & CONT_LOCAL)
5133 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else
5135 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5136
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005137 /* Always add completion for the original text. */
5138 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5140 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005141 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 vim_free(compl_pattern);
5144 compl_pattern = NULL;
5145 vim_free(compl_orig_text);
5146 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 return FAIL;
5148 }
5149
5150 /* showmode might reset the internal line pointers, so it must
5151 * be called before line = ml_get(), or when this address is no
5152 * longer needed. -- Acevedo.
5153 */
5154 edit_submode_extra = (char_u *)_("-- Searching...");
5155 edit_submode_highl = HLF_COUNT;
5156 showmode();
5157 edit_submode_extra = NULL;
5158 out_flush();
5159 }
5160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005161 compl_shown_match = compl_curr_match;
5162 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005165 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005167 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005168 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005170 /* may undisplay the popup menu */
5171 ins_compl_upd_pum();
5172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 if (n > 1) /* all matches have been found */
5174 compl_matches = n;
5175 compl_curr_match = compl_shown_match;
5176 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
Bram Moolenaard68071d2006-05-02 22:08:30 +00005178 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5179 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (got_int && !global_busy)
5181 {
5182 (void)vgetc();
5183 got_int = FALSE;
5184 }
5185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005187 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5190 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5192 edit_submode_highl = HLF_E;
5193 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5194 * because we couldn't expand anything at first place, but if we used
5195 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5196 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 if ( compl_length > 1
5198 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 || (ctrl_x_mode != 0
5200 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5201 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204
Bram Moolenaar572cb562005-08-05 21:35:02 +00005205 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
5210 if (edit_submode_extra == NULL)
5211 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005212 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 edit_submode_extra = (char_u *)_("Back at original");
5215 edit_submode_highl = HLF_W;
5216 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 edit_submode_extra = (char_u *)_("Word from other line");
5220 edit_submode_highl = HLF_COUNT;
5221 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005222 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 edit_submode_extra = (char_u *)_("The only match");
5225 edit_submode_highl = HLF_COUNT;
5226 }
5227 else
5228 {
5229 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005230 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005232 int number = 0;
5233 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 /* search backwards for the first valid (!= -1) number.
5238 * This should normally succeed already at the first loop
5239 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005240 for (match = compl_curr_match->cp_prev; match != NULL
5241 && match != compl_first_match;
5242 match = match->cp_prev)
5243 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005245 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 break;
5247 }
5248 if (match != NULL)
5249 /* go up and assign all numbers which are not assigned
5250 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005251 for (match = match->cp_next;
5252 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005253 match = match->cp_next)
5254 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256 else /* BACKWARD */
5257 {
5258 /* search forwards (upwards) for the first valid (!= -1)
5259 * number. This should normally succeed already at the
5260 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005261 for (match = compl_curr_match->cp_next; match != NULL
5262 && match != compl_first_match;
5263 match = match->cp_next)
5264 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005266 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 break;
5268 }
5269 if (match != NULL)
5270 /* go down and assign all numbers which are not
5271 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005272 for (match = match->cp_prev; match
5273 && match->cp_number == -1;
5274 match = match->cp_prev)
5275 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277 }
5278
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005279 /* The match should always have a sequence number now, this is
5280 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005281 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005283 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5284 * Translations may need more than twice that. */
5285 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005287 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005288 vim_snprintf((char *)match_ref, sizeof(match_ref),
5289 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005290 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005292 vim_snprintf((char *)match_ref, sizeof(match_ref),
5293 _("match %d"),
5294 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 edit_submode_extra = match_ref;
5296 edit_submode_highl = HLF_R;
5297 if (dollar_vcol)
5298 curs_columns(FALSE);
5299 }
5300 }
5301 }
5302
5303 /* Show a message about what (completion) mode we're in. */
5304 showmode();
5305 if (edit_submode_extra != NULL)
5306 {
5307 if (!p_smd)
5308 msg_attr(edit_submode_extra,
5309 edit_submode_highl < HLF_COUNT
5310 ? hl_attr(edit_submode_highl) : 0);
5311 }
5312 else
5313 msg_clr_cmdline(); /* necessary for "noshowmode" */
5314
Bram Moolenaard68071d2006-05-02 22:08:30 +00005315 /* Show the popup menu, unless we got interrupted. */
5316 if (!compl_interrupted)
5317 {
5318 /* RedrawingDisabled may be set when invoked through complete(). */
5319 n = RedrawingDisabled;
5320 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005321
5322 /* If the cursor moved we need to remove the pum first. */
5323 setcursor();
5324 if (save_w_wrow != curwin->w_wrow)
5325 ins_compl_del_pum();
5326
Bram Moolenaard68071d2006-05-02 22:08:30 +00005327 ins_compl_show_pum();
5328 setcursor();
5329 RedrawingDisabled = n;
5330 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005331 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005332 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 return OK;
5335}
5336
5337/*
5338 * Looks in the first "len" chars. of "src" for search-metachars.
5339 * If dest is not NULL the chars. are copied there quoting (with
5340 * a backslash) the metachars, and dest would be NUL terminated.
5341 * Returns the length (needed) of dest
5342 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005343 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344quote_meta(dest, src, len)
5345 char_u *dest;
5346 char_u *src;
5347 int len;
5348{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005349 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005351 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 switch (*src)
5354 {
5355 case '.':
5356 case '*':
5357 case '[':
5358 if (ctrl_x_mode == CTRL_X_DICTIONARY
5359 || ctrl_x_mode == CTRL_X_THESAURUS)
5360 break;
5361 case '~':
5362 if (!p_magic) /* quote these only if magic is set */
5363 break;
5364 case '\\':
5365 if (ctrl_x_mode == CTRL_X_DICTIONARY
5366 || ctrl_x_mode == CTRL_X_THESAURUS)
5367 break;
5368 case '^': /* currently it's not needed. */
5369 case '$':
5370 m++;
5371 if (dest != NULL)
5372 *dest++ = '\\';
5373 break;
5374 }
5375 if (dest != NULL)
5376 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005377# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 /* Copy remaining bytes of a multibyte character. */
5379 if (has_mbyte)
5380 {
5381 int i, mb_len;
5382
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005383 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 if (mb_len > 0 && len >= mb_len)
5385 for (i = 0; i < mb_len; ++i)
5386 {
5387 --len;
5388 ++src;
5389 if (dest != NULL)
5390 *dest++ = *src;
5391 }
5392 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395 if (dest != NULL)
5396 *dest = NUL;
5397
5398 return m;
5399}
5400#endif /* FEAT_INS_EXPAND */
5401
5402/*
5403 * Next character is interpreted literally.
5404 * A one, two or three digit decimal number is interpreted as its byte value.
5405 * If one or two digits are entered, the next character is given to vungetc().
5406 * For Unicode a character > 255 may be returned.
5407 */
5408 int
5409get_literal()
5410{
5411 int cc;
5412 int nc;
5413 int i;
5414 int hex = FALSE;
5415 int octal = FALSE;
5416#ifdef FEAT_MBYTE
5417 int unicode = 0;
5418#endif
5419
5420 if (got_int)
5421 return Ctrl_C;
5422
5423#ifdef FEAT_GUI
5424 /*
5425 * In GUI there is no point inserting the internal code for a special key.
5426 * It is more useful to insert the string "<KEY>" instead. This would
5427 * probably be useful in a text window too, but it would not be
5428 * vi-compatible (maybe there should be an option for it?) -- webb
5429 */
5430 if (gui.in_use)
5431 ++allow_keys;
5432#endif
5433#ifdef USE_ON_FLY_SCROLL
5434 dont_scroll = TRUE; /* disallow scrolling here */
5435#endif
5436 ++no_mapping; /* don't map the next key hits */
5437 cc = 0;
5438 i = 0;
5439 for (;;)
5440 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005441 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#ifdef FEAT_CMDL_INFO
5443 if (!(State & CMDLINE)
5444# ifdef FEAT_MBYTE
5445 && MB_BYTE2LEN_CHECK(nc) == 1
5446# endif
5447 )
5448 add_to_showcmd(nc);
5449#endif
5450 if (nc == 'x' || nc == 'X')
5451 hex = TRUE;
5452 else if (nc == 'o' || nc == 'O')
5453 octal = TRUE;
5454#ifdef FEAT_MBYTE
5455 else if (nc == 'u' || nc == 'U')
5456 unicode = nc;
5457#endif
5458 else
5459 {
5460 if (hex
5461#ifdef FEAT_MBYTE
5462 || unicode != 0
5463#endif
5464 )
5465 {
5466 if (!vim_isxdigit(nc))
5467 break;
5468 cc = cc * 16 + hex2nr(nc);
5469 }
5470 else if (octal)
5471 {
5472 if (nc < '0' || nc > '7')
5473 break;
5474 cc = cc * 8 + nc - '0';
5475 }
5476 else
5477 {
5478 if (!VIM_ISDIGIT(nc))
5479 break;
5480 cc = cc * 10 + nc - '0';
5481 }
5482
5483 ++i;
5484 }
5485
5486 if (cc > 255
5487#ifdef FEAT_MBYTE
5488 && unicode == 0
5489#endif
5490 )
5491 cc = 255; /* limit range to 0-255 */
5492 nc = 0;
5493
5494 if (hex) /* hex: up to two chars */
5495 {
5496 if (i >= 2)
5497 break;
5498 }
5499#ifdef FEAT_MBYTE
5500 else if (unicode) /* Unicode: up to four or eight chars */
5501 {
5502 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5503 break;
5504 }
5505#endif
5506 else if (i >= 3) /* decimal or octal: up to three chars */
5507 break;
5508 }
5509 if (i == 0) /* no number entered */
5510 {
5511 if (nc == K_ZERO) /* NUL is stored as NL */
5512 {
5513 cc = '\n';
5514 nc = 0;
5515 }
5516 else
5517 {
5518 cc = nc;
5519 nc = 0;
5520 }
5521 }
5522
5523 if (cc == 0) /* NUL is stored as NL */
5524 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005525#ifdef FEAT_MBYTE
5526 if (enc_dbcs && (cc & 0xff) == 0)
5527 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5528 second byte will cause trouble! */
5529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531 --no_mapping;
5532#ifdef FEAT_GUI
5533 if (gui.in_use)
5534 --allow_keys;
5535#endif
5536 if (nc)
5537 vungetc(nc);
5538 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5539 return cc;
5540}
5541
5542/*
5543 * Insert character, taking care of special keys and mod_mask
5544 */
5545 static void
5546insert_special(c, allow_modmask, ctrlv)
5547 int c;
5548 int allow_modmask;
5549 int ctrlv; /* c was typed after CTRL-V */
5550{
5551 char_u *p;
5552 int len;
5553
5554 /*
5555 * Special function key, translate into "<Key>". Up to the last '>' is
5556 * inserted with ins_str(), so as not to replace characters in replace
5557 * mode.
5558 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5559 * unless 'allow_modmask' is TRUE.
5560 */
5561#ifdef MACOS
5562 /* Command-key never produces a normal key */
5563 if (mod_mask & MOD_MASK_CMD)
5564 allow_modmask = TRUE;
5565#endif
5566 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5567 {
5568 p = get_special_key_name(c, mod_mask);
5569 len = (int)STRLEN(p);
5570 c = p[len - 1];
5571 if (len > 2)
5572 {
5573 if (stop_arrow() == FAIL)
5574 return;
5575 p[len - 1] = NUL;
5576 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005577 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 ctrlv = FALSE;
5579 }
5580 }
5581 if (stop_arrow() == OK)
5582 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5583}
5584
5585/*
5586 * Special characters in this context are those that need processing other
5587 * than the simple insertion that can be performed here. This includes ESC
5588 * which terminates the insert, and CR/NL which need special processing to
5589 * open up a new line. This routine tries to optimize insertions performed by
5590 * the "redo", "undo" or "put" commands, so it needs to know when it should
5591 * stop and defer processing to the "normal" mechanism.
5592 * '0' and '^' are special, because they can be followed by CTRL-D.
5593 */
5594#ifdef EBCDIC
5595# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5596#else
5597# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5598#endif
5599
5600#ifdef FEAT_MBYTE
5601# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5602#else
5603# define WHITECHAR(cc) vim_iswhite(cc)
5604#endif
5605
5606 void
5607insertchar(c, flags, second_indent)
5608 int c; /* character to insert or NUL */
5609 int flags; /* INSCHAR_FORMAT, etc. */
5610 int second_indent; /* indent for second line if >= 0 */
5611{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 int textwidth;
5613#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5619 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 /*
5622 * Try to break the line in two or more pieces when:
5623 * - Always do this if we have been called to do formatting only.
5624 * - Always do this when 'formatoptions' has the 'a' flag and the line
5625 * ends in white space.
5626 * - Otherwise:
5627 * - Don't do this if inserting a blank
5628 * - Don't do this if an existing character is being replaced, unless
5629 * we're in VREPLACE mode.
5630 * - Do this if the cursor is not on the line where insert started
5631 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5632 * before the insert.
5633 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5634 * before 'textwidth'
5635 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005636 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 && ((flags & INSCHAR_FORMAT)
5638 || (!vim_iswhite(c)
5639 && !((State & REPLACE_FLAG)
5640#ifdef FEAT_VREPLACE
5641 && !(State & VREPLACE_FLAG)
5642#endif
5643 && *ml_get_cursor() != NUL)
5644 && (curwin->w_cursor.lnum != Insstart.lnum
5645 || ((!has_format_option(FO_INS_LONG)
5646 || Insstart_textlen <= (colnr_T)textwidth)
5647 && (!fo_ins_blank
5648 || Insstart_blank_vcol <= (colnr_T)textwidth
5649 ))))))
5650 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005651 /* Format with 'formatexpr' when it's set. Use internal formatting
5652 * when 'formatexpr' isn't set or it returns non-zero. */
5653#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005654 int do_internal = TRUE;
5655
Bram Moolenaar81a82092008-03-12 16:27:00 +00005656 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005657 {
5658 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5659 /* It may be required to save for undo again, e.g. when setline()
5660 * was called. */
5661 ins_need_undo = TRUE;
5662 }
5663 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005665 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (c == NUL) /* only formatting was wanted */
5669 return;
5670
5671#ifdef FEAT_COMMENTS
5672 /* Check whether this character should end a comment. */
5673 if (did_ai && (int)c == end_comment_pending)
5674 {
5675 char_u *line;
5676 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5677 int middle_len, end_len;
5678 int i;
5679
5680 /*
5681 * Need to remove existing (middle) comment leader and insert end
5682 * comment leader. First, check what comment leader we can find.
5683 */
5684 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5685 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5686 {
5687 /* Skip middle-comment string */
5688 while (*p && p[-1] != ':') /* find end of middle flags */
5689 ++p;
5690 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5691 /* Don't count trailing white space for middle_len */
5692 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5693 --middle_len;
5694
5695 /* Find the end-comment string */
5696 while (*p && p[-1] != ':') /* find end of end flags */
5697 ++p;
5698 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5699
5700 /* Skip white space before the cursor */
5701 i = curwin->w_cursor.col;
5702 while (--i >= 0 && vim_iswhite(line[i]))
5703 ;
5704 i++;
5705
5706 /* Skip to before the middle leader */
5707 i -= middle_len;
5708
5709 /* Check some expected things before we go on */
5710 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5711 {
5712 /* Backspace over all the stuff we want to replace */
5713 backspace_until_column(i);
5714
5715 /*
5716 * Insert the end-comment string, except for the last
5717 * character, which will get inserted as normal later.
5718 */
5719 ins_bytes_len(lead_end, end_len - 1);
5720 }
5721 }
5722 }
5723 end_comment_pending = NUL;
5724#endif
5725
5726 did_ai = FALSE;
5727#ifdef FEAT_SMARTINDENT
5728 did_si = FALSE;
5729 can_si = FALSE;
5730 can_si_back = FALSE;
5731#endif
5732
5733 /*
5734 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5735 * This speeds up normal text input considerably.
5736 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5737 * need to re-indent at a ':', or any other character (but not what
5738 * 'paste' is set)..
5739 */
5740#ifdef USE_ON_FLY_SCROLL
5741 dont_scroll = FALSE; /* allow scrolling here */
5742#endif
5743
5744 if ( !ISSPECIAL(c)
5745#ifdef FEAT_MBYTE
5746 && (!has_mbyte || (*mb_char2len)(c) == 1)
5747#endif
5748 && vpeekc() != NUL
5749 && !(State & REPLACE_FLAG)
5750#ifdef FEAT_CINDENT
5751 && !cindent_on()
5752#endif
5753#ifdef FEAT_RIGHTLEFT
5754 && !p_ri
5755#endif
5756 )
5757 {
5758#define INPUT_BUFLEN 100
5759 char_u buf[INPUT_BUFLEN + 1];
5760 int i;
5761 colnr_T virtcol = 0;
5762
5763 buf[0] = c;
5764 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005765 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 virtcol = get_nolist_virtcol();
5767 /*
5768 * Stop the string when:
5769 * - no more chars available
5770 * - finding a special character (command key)
5771 * - buffer is full
5772 * - running into the 'textwidth' boundary
5773 * - need to check for abbreviation: A non-word char after a word-char
5774 */
5775 while ( (c = vpeekc()) != NUL
5776 && !ISSPECIAL(c)
5777#ifdef FEAT_MBYTE
5778 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5779#endif
5780 && i < INPUT_BUFLEN
5781 && (textwidth == 0
5782 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5783 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5784 {
5785#ifdef FEAT_RIGHTLEFT
5786 c = vgetc();
5787 if (p_hkmap && KeyTyped)
5788 c = hkmap(c); /* Hebrew mode mapping */
5789# ifdef FEAT_FKMAP
5790 if (p_fkmap && KeyTyped)
5791 c = fkmap(c); /* Farsi mode mapping */
5792# endif
5793 buf[i++] = c;
5794#else
5795 buf[i++] = vgetc();
5796#endif
5797 }
5798
5799#ifdef FEAT_DIGRAPHS
5800 do_digraph(-1); /* clear digraphs */
5801 do_digraph(buf[i-1]); /* may be the start of a digraph */
5802#endif
5803 buf[i] = NUL;
5804 ins_str(buf);
5805 if (flags & INSCHAR_CTRLV)
5806 {
5807 redo_literal(*buf);
5808 i = 1;
5809 }
5810 else
5811 i = 0;
5812 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005813 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
5815 else
5816 {
5817#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005818 int cc;
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5821 {
5822 char_u buf[MB_MAXBYTES + 1];
5823
5824 (*mb_char2bytes)(c, buf);
5825 buf[cc] = NUL;
5826 ins_char_bytes(buf, cc);
5827 AppendCharToRedobuff(c);
5828 }
5829 else
5830#endif
5831 {
5832 ins_char(c);
5833 if (flags & INSCHAR_CTRLV)
5834 redo_literal(c);
5835 else
5836 AppendCharToRedobuff(c);
5837 }
5838 }
5839}
5840
5841/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005842 * Format text at the current insert position.
5843 */
5844 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005845internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005846 int textwidth;
5847 int second_indent;
5848 int flags;
5849 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005850 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005851{
5852 int cc;
5853 int save_char = NUL;
5854 int haveto_redraw = FALSE;
5855 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5856#ifdef FEAT_MBYTE
5857 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5858#endif
5859 int fo_white_par = has_format_option(FO_WHITE_PAR);
5860 int first_line = TRUE;
5861#ifdef FEAT_COMMENTS
5862 colnr_T leader_len;
5863 int no_leader = FALSE;
5864 int do_comments = (flags & INSCHAR_DO_COM);
5865#endif
5866
5867 /*
5868 * When 'ai' is off we don't want a space under the cursor to be
5869 * deleted. Replace it with an 'x' temporarily.
5870 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005871 if (!curbuf->b_p_ai
5872#ifdef FEAT_VREPLACE
5873 && !(State & VREPLACE_FLAG)
5874#endif
5875 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005876 {
5877 cc = gchar_cursor();
5878 if (vim_iswhite(cc))
5879 {
5880 save_char = cc;
5881 pchar_cursor('x');
5882 }
5883 }
5884
5885 /*
5886 * Repeat breaking lines, until the current line is not too long.
5887 */
5888 while (!got_int)
5889 {
5890 int startcol; /* Cursor column at entry */
5891 int wantcol; /* column at textwidth border */
5892 int foundcol; /* column for start of spaces */
5893 int end_foundcol = 0; /* column for start of word */
5894 colnr_T len;
5895 colnr_T virtcol;
5896#ifdef FEAT_VREPLACE
5897 int orig_col = 0;
5898 char_u *saved_text = NULL;
5899#endif
5900 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005901 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005902
Bram Moolenaar97b98102009-11-17 16:41:01 +00005903 virtcol = get_nolist_virtcol()
5904 + char2cells(c != NUL ? c : gchar_cursor());
5905 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906 break;
5907
5908#ifdef FEAT_COMMENTS
5909 if (no_leader)
5910 do_comments = FALSE;
5911 else if (!(flags & INSCHAR_FORMAT)
5912 && has_format_option(FO_WRAP_COMS))
5913 do_comments = TRUE;
5914
5915 /* Don't break until after the comment leader */
5916 if (do_comments)
5917 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5918 else
5919 leader_len = 0;
5920
5921 /* If the line doesn't start with a comment leader, then don't
5922 * start one in a following broken line. Avoids that a %word
5923 * moved to the start of the next line causes all following lines
5924 * to start with %. */
5925 if (leader_len == 0)
5926 no_leader = TRUE;
5927#endif
5928 if (!(flags & INSCHAR_FORMAT)
5929#ifdef FEAT_COMMENTS
5930 && leader_len == 0
5931#endif
5932 && !has_format_option(FO_WRAP))
5933
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005934 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005935 if ((startcol = curwin->w_cursor.col) == 0)
5936 break;
5937
5938 /* find column of textwidth border */
5939 coladvance((colnr_T)textwidth);
5940 wantcol = curwin->w_cursor.col;
5941
Bram Moolenaar97b98102009-11-17 16:41:01 +00005942 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005943 foundcol = 0;
5944
5945 /*
5946 * Find position to break at.
5947 * Stop at first entered white when 'formatoptions' has 'v'
5948 */
5949 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5950 || curwin->w_cursor.lnum != Insstart.lnum
5951 || curwin->w_cursor.col >= Insstart.col)
5952 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005953 if (curwin->w_cursor.col == startcol && c != NUL)
5954 cc = c;
5955 else
5956 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005957 if (WHITECHAR(cc))
5958 {
5959 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005960 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005961
5962 /* find start of sequence of blanks */
5963 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5964 {
5965 dec_cursor();
5966 cc = gchar_cursor();
5967 }
5968 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5969 break; /* only spaces in front of text */
5970#ifdef FEAT_COMMENTS
5971 /* Don't break until after the comment leader */
5972 if (curwin->w_cursor.col < leader_len)
5973 break;
5974#endif
5975 if (has_format_option(FO_ONE_LETTER))
5976 {
5977 /* do not break after one-letter words */
5978 if (curwin->w_cursor.col == 0)
5979 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005980#ifdef FEAT_COMMENTS
5981 /* do not break "#a b" when 'tw' is 2 */
5982 if (curwin->w_cursor.col <= leader_len)
5983 break;
5984#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 col = curwin->w_cursor.col;
5986 dec_cursor();
5987 cc = gchar_cursor();
5988
5989 if (WHITECHAR(cc))
5990 continue; /* one-letter, continue */
5991 curwin->w_cursor.col = col;
5992 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005993
5994 inc_cursor();
5995
5996 end_foundcol = end_col + 1;
5997 foundcol = curwin->w_cursor.col;
5998 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 break;
6000 }
6001#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006002 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006003 {
6004 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006005 if (curwin->w_cursor.col != startcol)
6006 {
6007#ifdef FEAT_COMMENTS
6008 /* Don't break until after the comment leader */
6009 if (curwin->w_cursor.col < leader_len)
6010 break;
6011#endif
6012 col = curwin->w_cursor.col;
6013 inc_cursor();
6014 /* Don't change end_foundcol if already set. */
6015 if (foundcol != curwin->w_cursor.col)
6016 {
6017 foundcol = curwin->w_cursor.col;
6018 end_foundcol = foundcol;
6019 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6020 break;
6021 }
6022 curwin->w_cursor.col = col;
6023 }
6024
6025 if (curwin->w_cursor.col == 0)
6026 break;
6027
6028 col = curwin->w_cursor.col;
6029
6030 dec_cursor();
6031 cc = gchar_cursor();
6032
6033 if (WHITECHAR(cc))
6034 continue; /* break with space */
6035#ifdef FEAT_COMMENTS
6036 /* Don't break until after the comment leader */
6037 if (curwin->w_cursor.col < leader_len)
6038 break;
6039#endif
6040
6041 curwin->w_cursor.col = col;
6042
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006043 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006044 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006045 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6046 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047 }
6048#endif
6049 if (curwin->w_cursor.col == 0)
6050 break;
6051 dec_cursor();
6052 }
6053
6054 if (foundcol == 0) /* no spaces, cannot break line */
6055 {
6056 curwin->w_cursor.col = startcol;
6057 break;
6058 }
6059
6060 /* Going to break the line, remove any "$" now. */
6061 undisplay_dollar();
6062
6063 /*
6064 * Offset between cursor position and line break is used by replace
6065 * stack functions. VREPLACE does not use this, and backspaces
6066 * over the text instead.
6067 */
6068#ifdef FEAT_VREPLACE
6069 if (State & VREPLACE_FLAG)
6070 orig_col = startcol; /* Will start backspacing from here */
6071 else
6072#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006073 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006074
6075 /*
6076 * adjust startcol for spaces that will be deleted and
6077 * characters that will remain on top line
6078 */
6079 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006080 while ((cc = gchar_cursor(), WHITECHAR(cc))
6081 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006082 inc_cursor();
6083 startcol -= curwin->w_cursor.col;
6084 if (startcol < 0)
6085 startcol = 0;
6086
6087#ifdef FEAT_VREPLACE
6088 if (State & VREPLACE_FLAG)
6089 {
6090 /*
6091 * In VREPLACE mode, we will backspace over the text to be
6092 * wrapped, so save a copy now to put on the next line.
6093 */
6094 saved_text = vim_strsave(ml_get_cursor());
6095 curwin->w_cursor.col = orig_col;
6096 if (saved_text == NULL)
6097 break; /* Can't do it, out of memory */
6098 saved_text[startcol] = NUL;
6099
6100 /* Backspace over characters that will move to the next line */
6101 if (!fo_white_par)
6102 backspace_until_column(foundcol);
6103 }
6104 else
6105#endif
6106 {
6107 /* put cursor after pos. to break line */
6108 if (!fo_white_par)
6109 curwin->w_cursor.col = foundcol;
6110 }
6111
6112 /*
6113 * Split the line just before the margin.
6114 * Only insert/delete lines, but don't really redraw the window.
6115 */
6116 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6117 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6118#ifdef FEAT_COMMENTS
6119 + (do_comments ? OPENLINE_DO_COM : 0)
6120#endif
6121 , old_indent);
6122 old_indent = 0;
6123
6124 replace_offset = 0;
6125 if (first_line)
6126 {
6127 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6128 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6129 if (second_indent >= 0)
6130 {
6131#ifdef FEAT_VREPLACE
6132 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006133 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006134 else
6135#endif
6136 (void)set_indent(second_indent, SIN_CHANGED);
6137 }
6138 first_line = FALSE;
6139 }
6140
6141#ifdef FEAT_VREPLACE
6142 if (State & VREPLACE_FLAG)
6143 {
6144 /*
6145 * In VREPLACE mode we have backspaced over the text to be
6146 * moved, now we re-insert it into the new line.
6147 */
6148 ins_bytes(saved_text);
6149 vim_free(saved_text);
6150 }
6151 else
6152#endif
6153 {
6154 /*
6155 * Check if cursor is not past the NUL off the line, cindent
6156 * may have added or removed indent.
6157 */
6158 curwin->w_cursor.col += startcol;
6159 len = (colnr_T)STRLEN(ml_get_curline());
6160 if (curwin->w_cursor.col > len)
6161 curwin->w_cursor.col = len;
6162 }
6163
6164 haveto_redraw = TRUE;
6165#ifdef FEAT_CINDENT
6166 can_cindent = TRUE;
6167#endif
6168 /* moved the cursor, don't autoindent or cindent now */
6169 did_ai = FALSE;
6170#ifdef FEAT_SMARTINDENT
6171 did_si = FALSE;
6172 can_si = FALSE;
6173 can_si_back = FALSE;
6174#endif
6175 line_breakcheck();
6176 }
6177
6178 if (save_char != NUL) /* put back space after cursor */
6179 pchar_cursor(save_char);
6180
6181 if (!format_only && haveto_redraw)
6182 {
6183 update_topline();
6184 redraw_curbuf_later(VALID);
6185 }
6186}
6187
6188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 * Called after inserting or deleting text: When 'formatoptions' includes the
6190 * 'a' flag format from the current line until the end of the paragraph.
6191 * Keep the cursor at the same position relative to the text.
6192 * The caller must have saved the cursor line for undo, following ones will be
6193 * saved here.
6194 */
6195 void
6196auto_format(trailblank, prev_line)
6197 int trailblank; /* when TRUE also format with trailing blank */
6198 int prev_line; /* may start in previous line */
6199{
6200 pos_T pos;
6201 colnr_T len;
6202 char_u *old;
6203 char_u *new, *pnew;
6204 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006205 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 if (!has_format_option(FO_AUTO))
6208 return;
6209
6210 pos = curwin->w_cursor;
6211 old = ml_get_curline();
6212
6213 /* may remove added space */
6214 check_auto_format(FALSE);
6215
6216 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6217 * user might insert normal text next. Also skip formatting when "1" is
6218 * in 'formatoptions' and there is a single character before the cursor.
6219 * Otherwise the line would be broken and when typing another non-white
6220 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006221 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (*old != NUL && !trailblank && wasatend)
6223 {
6224 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006225 cc = gchar_cursor();
6226 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6227 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006229 cc = gchar_cursor();
6230 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 curwin->w_cursor = pos;
6233 return;
6234 }
6235 curwin->w_cursor = pos;
6236 }
6237
6238#ifdef FEAT_COMMENTS
6239 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6240 * comments. */
6241 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6242 && get_leader_len(old, NULL, FALSE) == 0)
6243 return;
6244#endif
6245
6246 /*
6247 * May start formatting in a previous line, so that after "x" a word is
6248 * moved to the previous line if it fits there now. Only when this is not
6249 * the start of a paragraph.
6250 */
6251 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6252 {
6253 --curwin->w_cursor.lnum;
6254 if (u_save_cursor() == FAIL)
6255 return;
6256 }
6257
6258 /*
6259 * Do the formatting and restore the cursor position. "saved_cursor" will
6260 * be adjusted for the text formatting.
6261 */
6262 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006263 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 curwin->w_cursor = saved_cursor;
6265 saved_cursor.lnum = 0;
6266
6267 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6268 {
6269 /* "cannot happen" */
6270 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6271 coladvance((colnr_T)MAXCOL);
6272 }
6273 else
6274 check_cursor_col();
6275
6276 /* Insert mode: If the cursor is now after the end of the line while it
6277 * previously wasn't, the line was broken. Because of the rule above we
6278 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6279 * formatted. */
6280 if (!wasatend && has_format_option(FO_WHITE_PAR))
6281 {
6282 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006283 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (curwin->w_cursor.col == len)
6285 {
6286 pnew = vim_strnsave(new, len + 2);
6287 pnew[len] = ' ';
6288 pnew[len + 1] = NUL;
6289 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6290 /* remove the space later */
6291 did_add_space = TRUE;
6292 }
6293 else
6294 /* may remove added space */
6295 check_auto_format(FALSE);
6296 }
6297
6298 check_cursor();
6299}
6300
6301/*
6302 * When an extra space was added to continue a paragraph for auto-formatting,
6303 * delete it now. The space must be under the cursor, just after the insert
6304 * position.
6305 */
6306 static void
6307check_auto_format(end_insert)
6308 int end_insert; /* TRUE when ending Insert mode */
6309{
6310 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006311 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 if (did_add_space)
6314 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006315 cc = gchar_cursor();
6316 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 /* Somehow the space was removed already. */
6318 did_add_space = FALSE;
6319 else
6320 {
6321 if (!end_insert)
6322 {
6323 inc_cursor();
6324 c = gchar_cursor();
6325 dec_cursor();
6326 }
6327 if (c != NUL)
6328 {
6329 /* The space is no longer at the end of the line, delete it. */
6330 del_char(FALSE);
6331 did_add_space = FALSE;
6332 }
6333 }
6334 }
6335}
6336
6337/*
6338 * Find out textwidth to be used for formatting:
6339 * if 'textwidth' option is set, use it
6340 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6341 * if invalid value, use 0.
6342 * Set default to window width (maximum 79) for "gq" operator.
6343 */
6344 int
6345comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006346 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 int textwidth;
6349
6350 textwidth = curbuf->b_p_tw;
6351 if (textwidth == 0 && curbuf->b_p_wm)
6352 {
6353 /* The width is the window width minus 'wrapmargin' minus all the
6354 * things that add to the margin. */
6355 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6356#ifdef FEAT_CMDWIN
6357 if (cmdwin_type != 0)
6358 textwidth -= 1;
6359#endif
6360#ifdef FEAT_FOLDING
6361 textwidth -= curwin->w_p_fdc;
6362#endif
6363#ifdef FEAT_SIGNS
6364 if (curwin->w_buffer->b_signlist != NULL
6365# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006366 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367# endif
6368 )
6369 textwidth -= 1;
6370#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006371 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 textwidth -= 8;
6373 }
6374 if (textwidth < 0)
6375 textwidth = 0;
6376 if (ff && textwidth == 0)
6377 {
6378 textwidth = W_WIDTH(curwin) - 1;
6379 if (textwidth > 79)
6380 textwidth = 79;
6381 }
6382 return textwidth;
6383}
6384
6385/*
6386 * Put a character in the redo buffer, for when just after a CTRL-V.
6387 */
6388 static void
6389redo_literal(c)
6390 int c;
6391{
6392 char_u buf[10];
6393
6394 /* Only digits need special treatment. Translate them into a string of
6395 * three digits. */
6396 if (VIM_ISDIGIT(c))
6397 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006398 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 AppendToRedobuff(buf);
6400 }
6401 else
6402 AppendCharToRedobuff(c);
6403}
6404
6405/*
6406 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006407 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
6409 static void
6410start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006411 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 if (!arrow_used) /* something has been inserted */
6414 {
6415 AppendToRedobuff(ESC_STR);
6416 stop_insert(end_insert_pos, FALSE);
6417 arrow_used = TRUE; /* this means we stopped the current insert */
6418 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006419#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006420 check_spell_redraw();
6421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422}
6423
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006424#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006425/*
6426 * If we skipped highlighting word at cursor, do it now.
6427 * It may be skipped again, thus reset spell_redraw_lnum first.
6428 */
6429 static void
6430check_spell_redraw()
6431{
6432 if (spell_redraw_lnum != 0)
6433 {
6434 linenr_T lnum = spell_redraw_lnum;
6435
6436 spell_redraw_lnum = 0;
6437 redrawWinline(lnum, FALSE);
6438 }
6439}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006440
6441/*
6442 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6443 * spelled word, if there is one.
6444 */
6445 static void
6446spell_back_to_badword()
6447{
6448 pos_T tpos = curwin->w_cursor;
6449
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006450 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006451 if (curwin->w_cursor.col != tpos.col)
6452 start_arrow(&tpos);
6453}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006454#endif
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456/*
6457 * stop_arrow() is called before a change is made in insert mode.
6458 * If an arrow key has been used, start a new insertion.
6459 * Returns FAIL if undo is impossible, shouldn't insert then.
6460 */
6461 int
6462stop_arrow()
6463{
6464 if (arrow_used)
6465 {
6466 if (u_save_cursor() == OK)
6467 {
6468 arrow_used = FALSE;
6469 ins_need_undo = FALSE;
6470 }
6471 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006472 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 ai_col = 0;
6474#ifdef FEAT_VREPLACE
6475 if (State & VREPLACE_FLAG)
6476 {
6477 orig_line_count = curbuf->b_ml.ml_line_count;
6478 vr_lines_changed = 1;
6479 }
6480#endif
6481 ResetRedobuff();
6482 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006483 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485 else if (ins_need_undo)
6486 {
6487 if (u_save_cursor() == OK)
6488 ins_need_undo = FALSE;
6489 }
6490
6491#ifdef FEAT_FOLDING
6492 /* Always open fold at the cursor line when inserting something. */
6493 foldOpenCursor();
6494#endif
6495
6496 return (arrow_used || ins_need_undo ? FAIL : OK);
6497}
6498
6499/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006500 * Do a few things to stop inserting.
6501 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6502 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 */
6504 static void
6505stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006506 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006507 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006509 int cc;
6510 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 stop_redo_ins();
6513 replace_flush(); /* abandon replace stack */
6514
6515 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006516 * Save the inserted text for later redo with ^@ and CTRL-A.
6517 * Don't do it when "restart_edit" was set and nothing was inserted,
6518 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006520 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006521 if (did_restart_edit == 0 || (ptr != NULL
6522 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006523 {
6524 vim_free(last_insert);
6525 last_insert = ptr;
6526 last_insert_skip = new_insert_skip;
6527 }
6528 else
6529 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006531 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
6533 /* Auto-format now. It may seem strange to do this when stopping an
6534 * insertion (or moving the cursor), but it's required when appending
6535 * a line and having it end in a space. But only do it when something
6536 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006537 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006539 pos_T tpos = curwin->w_cursor;
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /* When the cursor is at the end of the line after a space the
6542 * formatting will move it to the following word. Avoid that by
6543 * moving the cursor onto the space. */
6544 cc = 'x';
6545 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6546 {
6547 dec_cursor();
6548 cc = gchar_cursor();
6549 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006550 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552
6553 auto_format(TRUE, FALSE);
6554
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006555 if (vim_iswhite(cc))
6556 {
6557 if (gchar_cursor() != NUL)
6558 inc_cursor();
6559#ifdef FEAT_VIRTUALEDIT
6560 /* If the cursor is still at the same character, also keep
6561 * the "coladd". */
6562 if (gchar_cursor() == NUL
6563 && curwin->w_cursor.lnum == tpos.lnum
6564 && curwin->w_cursor.col == tpos.col)
6565 curwin->w_cursor.coladd = tpos.coladd;
6566#endif
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569
6570 /* If a space was inserted for auto-formatting, remove it now. */
6571 check_auto_format(TRUE);
6572
6573 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006574 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006575 * Do this when ESC was used or moving the cursor up/down.
6576 * Check for the old position still being valid, just in case the text
6577 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006578 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006579 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6580 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006582 pos_T tpos = curwin->w_cursor;
6583
6584 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006585 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006586 for (;;)
6587 {
6588 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6589 --curwin->w_cursor.col;
6590 cc = gchar_cursor();
6591 if (!vim_iswhite(cc))
6592 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006593 if (del_char(TRUE) == FAIL)
6594 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006595 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006596 if (curwin->w_cursor.lnum != tpos.lnum)
6597 curwin->w_cursor = tpos;
6598 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6600
6601#ifdef FEAT_VISUAL
6602 /* <C-S-Right> may have started Visual mode, adjust the position for
6603 * deleted characters. */
6604 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6605 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006606 int len = (int)STRLEN(ml_get_curline());
6607
6608 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006610 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611# ifdef FEAT_VIRTUALEDIT
6612 VIsual.coladd = 0;
6613# endif
6614 }
6615 }
6616#endif
6617 }
6618 }
6619 did_ai = FALSE;
6620#ifdef FEAT_SMARTINDENT
6621 did_si = FALSE;
6622 can_si = FALSE;
6623 can_si_back = FALSE;
6624#endif
6625
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006626 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6627 * now in a different buffer. */
6628 if (end_insert_pos != NULL)
6629 {
6630 curbuf->b_op_start = Insstart;
6631 curbuf->b_op_end = *end_insert_pos;
6632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633}
6634
6635/*
6636 * Set the last inserted text to a single character.
6637 * Used for the replace command.
6638 */
6639 void
6640set_last_insert(c)
6641 int c;
6642{
6643 char_u *s;
6644
6645 vim_free(last_insert);
6646#ifdef FEAT_MBYTE
6647 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6648#else
6649 last_insert = alloc(6);
6650#endif
6651 if (last_insert != NULL)
6652 {
6653 s = last_insert;
6654 /* Use the CTRL-V only when entering a special char */
6655 if (c < ' ' || c == DEL)
6656 *s++ = Ctrl_V;
6657 s = add_char2buf(c, s);
6658 *s++ = ESC;
6659 *s++ = NUL;
6660 last_insert_skip = 0;
6661 }
6662}
6663
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006664#if defined(EXITFREE) || defined(PROTO)
6665 void
6666free_last_insert()
6667{
6668 vim_free(last_insert);
6669 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006670# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006671 vim_free(compl_orig_text);
6672 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006673# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006674}
6675#endif
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677/*
6678 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6679 * and CSI. Handle multi-byte characters.
6680 * Returns a pointer to after the added bytes.
6681 */
6682 char_u *
6683add_char2buf(c, s)
6684 int c;
6685 char_u *s;
6686{
6687#ifdef FEAT_MBYTE
6688 char_u temp[MB_MAXBYTES];
6689 int i;
6690 int len;
6691
6692 len = (*mb_char2bytes)(c, temp);
6693 for (i = 0; i < len; ++i)
6694 {
6695 c = temp[i];
6696#endif
6697 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6698 if (c == K_SPECIAL)
6699 {
6700 *s++ = K_SPECIAL;
6701 *s++ = KS_SPECIAL;
6702 *s++ = KE_FILLER;
6703 }
6704#ifdef FEAT_GUI
6705 else if (c == CSI)
6706 {
6707 *s++ = CSI;
6708 *s++ = KS_EXTRA;
6709 *s++ = (int)KE_CSI;
6710 }
6711#endif
6712 else
6713 *s++ = c;
6714#ifdef FEAT_MBYTE
6715 }
6716#endif
6717 return s;
6718}
6719
6720/*
6721 * move cursor to start of line
6722 * if flags & BL_WHITE move to first non-white
6723 * if flags & BL_SOL move to first non-white if startofline is set,
6724 * otherwise keep "curswant" column
6725 * if flags & BL_FIX don't leave the cursor on a NUL.
6726 */
6727 void
6728beginline(flags)
6729 int flags;
6730{
6731 if ((flags & BL_SOL) && !p_sol)
6732 coladvance(curwin->w_curswant);
6733 else
6734 {
6735 curwin->w_cursor.col = 0;
6736#ifdef FEAT_VIRTUALEDIT
6737 curwin->w_cursor.coladd = 0;
6738#endif
6739
6740 if (flags & (BL_WHITE | BL_SOL))
6741 {
6742 char_u *ptr;
6743
6744 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6745 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6746 ++curwin->w_cursor.col;
6747 }
6748 curwin->w_set_curswant = TRUE;
6749 }
6750}
6751
6752/*
6753 * oneright oneleft cursor_down cursor_up
6754 *
6755 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006756 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 * Return OK when successful, FAIL when we hit a line of file boundary.
6758 */
6759
6760 int
6761oneright()
6762{
6763 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766#ifdef FEAT_VIRTUALEDIT
6767 if (virtual_active())
6768 {
6769 pos_T prevpos = curwin->w_cursor;
6770
6771 /* Adjust for multi-wide char (excluding TAB) */
6772 ptr = ml_get_cursor();
6773 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006774# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006776# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 ))
6780 ? ptr2cells(ptr) : 1));
6781 curwin->w_set_curswant = TRUE;
6782 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6783 return (prevpos.col != curwin->w_cursor.col
6784 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6785 }
6786#endif
6787
6788 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006789 if (*ptr == NUL)
6790 return FAIL; /* already at the very end */
6791
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006793 if (has_mbyte)
6794 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 else
6796#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006797 l = 1;
6798
6799 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6800 * contains "onemore". */
6801 if (ptr[l] == NUL
6802#ifdef FEAT_VIRTUALEDIT
6803 && (ve_flags & VE_ONEMORE) == 0
6804#endif
6805 )
6806 return FAIL;
6807 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
6809 curwin->w_set_curswant = TRUE;
6810 return OK;
6811}
6812
6813 int
6814oneleft()
6815{
6816#ifdef FEAT_VIRTUALEDIT
6817 if (virtual_active())
6818 {
6819 int width;
6820 int v = getviscol();
6821
6822 if (v == 0)
6823 return FAIL;
6824
6825# ifdef FEAT_LINEBREAK
6826 /* We might get stuck on 'showbreak', skip over it. */
6827 width = 1;
6828 for (;;)
6829 {
6830 coladvance(v - width);
6831 /* getviscol() is slow, skip it when 'showbreak' is empty and
6832 * there are no multi-byte characters */
6833 if ((*p_sbr == NUL
6834# ifdef FEAT_MBYTE
6835 && !has_mbyte
6836# endif
6837 ) || getviscol() < v)
6838 break;
6839 ++width;
6840 }
6841# else
6842 coladvance(v - 1);
6843# endif
6844
6845 if (curwin->w_cursor.coladd == 1)
6846 {
6847 char_u *ptr;
6848
6849 /* Adjust for multi-wide char (not a TAB) */
6850 ptr = ml_get_cursor();
6851 if (*ptr != TAB && vim_isprintc(
6852# ifdef FEAT_MBYTE
6853 (*mb_ptr2char)(ptr)
6854# else
6855 *ptr
6856# endif
6857 ) && ptr2cells(ptr) > 1)
6858 curwin->w_cursor.coladd = 0;
6859 }
6860
6861 curwin->w_set_curswant = TRUE;
6862 return OK;
6863 }
6864#endif
6865
6866 if (curwin->w_cursor.col == 0)
6867 return FAIL;
6868
6869 curwin->w_set_curswant = TRUE;
6870 --curwin->w_cursor.col;
6871
6872#ifdef FEAT_MBYTE
6873 /* if the character on the left of the current cursor is a multi-byte
6874 * character, move to its first byte */
6875 if (has_mbyte)
6876 mb_adjust_cursor();
6877#endif
6878 return OK;
6879}
6880
6881 int
6882cursor_up(n, upd_topline)
6883 long n;
6884 int upd_topline; /* When TRUE: update topline */
6885{
6886 linenr_T lnum;
6887
6888 if (n > 0)
6889 {
6890 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006891 /* This fails if the cursor is already in the first line or the count
6892 * is larger than the line number and '-' is in 'cpoptions' */
6893 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 return FAIL;
6895 if (n >= lnum)
6896 lnum = 1;
6897 else
6898#ifdef FEAT_FOLDING
6899 if (hasAnyFolding(curwin))
6900 {
6901 /*
6902 * Count each sequence of folded lines as one logical line.
6903 */
6904 /* go to the the start of the current fold */
6905 (void)hasFolding(lnum, &lnum, NULL);
6906
6907 while (n--)
6908 {
6909 /* move up one line */
6910 --lnum;
6911 if (lnum <= 1)
6912 break;
6913 /* If we entered a fold, move to the beginning, unless in
6914 * Insert mode or when 'foldopen' contains "all": it will open
6915 * in a moment. */
6916 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6917 (void)hasFolding(lnum, &lnum, NULL);
6918 }
6919 if (lnum < 1)
6920 lnum = 1;
6921 }
6922 else
6923#endif
6924 lnum -= n;
6925 curwin->w_cursor.lnum = lnum;
6926 }
6927
6928 /* try to advance to the column we want to be at */
6929 coladvance(curwin->w_curswant);
6930
6931 if (upd_topline)
6932 update_topline(); /* make sure curwin->w_topline is valid */
6933
6934 return OK;
6935}
6936
6937/*
6938 * Cursor down a number of logical lines.
6939 */
6940 int
6941cursor_down(n, upd_topline)
6942 long n;
6943 int upd_topline; /* When TRUE: update topline */
6944{
6945 linenr_T lnum;
6946
6947 if (n > 0)
6948 {
6949 lnum = curwin->w_cursor.lnum;
6950#ifdef FEAT_FOLDING
6951 /* Move to last line of fold, will fail if it's the end-of-file. */
6952 (void)hasFolding(lnum, NULL, &lnum);
6953#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006954 /* This fails if the cursor is already in the last line or would move
6955 * beyound the last line and '-' is in 'cpoptions' */
6956 if (lnum >= curbuf->b_ml.ml_line_count
6957 || (lnum + n > curbuf->b_ml.ml_line_count
6958 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 return FAIL;
6960 if (lnum + n >= curbuf->b_ml.ml_line_count)
6961 lnum = curbuf->b_ml.ml_line_count;
6962 else
6963#ifdef FEAT_FOLDING
6964 if (hasAnyFolding(curwin))
6965 {
6966 linenr_T last;
6967
6968 /* count each sequence of folded lines as one logical line */
6969 while (n--)
6970 {
6971 if (hasFolding(lnum, NULL, &last))
6972 lnum = last + 1;
6973 else
6974 ++lnum;
6975 if (lnum >= curbuf->b_ml.ml_line_count)
6976 break;
6977 }
6978 if (lnum > curbuf->b_ml.ml_line_count)
6979 lnum = curbuf->b_ml.ml_line_count;
6980 }
6981 else
6982#endif
6983 lnum += n;
6984 curwin->w_cursor.lnum = lnum;
6985 }
6986
6987 /* try to advance to the column we want to be at */
6988 coladvance(curwin->w_curswant);
6989
6990 if (upd_topline)
6991 update_topline(); /* make sure curwin->w_topline is valid */
6992
6993 return OK;
6994}
6995
6996/*
6997 * Stuff the last inserted text in the read buffer.
6998 * Last_insert actually is a copy of the redo buffer, so we
6999 * first have to remove the command.
7000 */
7001 int
7002stuff_inserted(c, count, no_esc)
7003 int c; /* Command character to be inserted */
7004 long count; /* Repeat this many times */
7005 int no_esc; /* Don't add an ESC at the end */
7006{
7007 char_u *esc_ptr;
7008 char_u *ptr;
7009 char_u *last_ptr;
7010 char_u last = NUL;
7011
7012 ptr = get_last_insert();
7013 if (ptr == NULL)
7014 {
7015 EMSG(_(e_noinstext));
7016 return FAIL;
7017 }
7018
7019 /* may want to stuff the command character, to start Insert mode */
7020 if (c != NUL)
7021 stuffcharReadbuff(c);
7022 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7023 *esc_ptr = NUL; /* remove the ESC */
7024
7025 /* when the last char is either "0" or "^" it will be quoted if no ESC
7026 * comes after it OR if it will inserted more than once and "ptr"
7027 * starts with ^D. -- Acevedo
7028 */
7029 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7030 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7031 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7032 {
7033 last = *last_ptr;
7034 *last_ptr = NUL;
7035 }
7036
7037 do
7038 {
7039 stuffReadbuff(ptr);
7040 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7041 if (last)
7042 stuffReadbuff((char_u *)(last == '0'
7043 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7044 : IF_EB("\026^", CTRL_V_STR "^")));
7045 }
7046 while (--count > 0);
7047
7048 if (last)
7049 *last_ptr = last;
7050
7051 if (esc_ptr != NULL)
7052 *esc_ptr = ESC; /* put the ESC back */
7053
7054 /* may want to stuff a trailing ESC, to get out of Insert mode */
7055 if (!no_esc)
7056 stuffcharReadbuff(ESC);
7057
7058 return OK;
7059}
7060
7061 char_u *
7062get_last_insert()
7063{
7064 if (last_insert == NULL)
7065 return NULL;
7066 return last_insert + last_insert_skip;
7067}
7068
7069/*
7070 * Get last inserted string, and remove trailing <Esc>.
7071 * Returns pointer to allocated memory (must be freed) or NULL.
7072 */
7073 char_u *
7074get_last_insert_save()
7075{
7076 char_u *s;
7077 int len;
7078
7079 if (last_insert == NULL)
7080 return NULL;
7081 s = vim_strsave(last_insert + last_insert_skip);
7082 if (s != NULL)
7083 {
7084 len = (int)STRLEN(s);
7085 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7086 s[len - 1] = NUL;
7087 }
7088 return s;
7089}
7090
7091/*
7092 * Check the word in front of the cursor for an abbreviation.
7093 * Called when the non-id character "c" has been entered.
7094 * When an abbreviation is recognized it is removed from the text and
7095 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7096 */
7097 static int
7098echeck_abbr(c)
7099 int c;
7100{
7101 /* Don't check for abbreviation in paste mode, when disabled and just
7102 * after moving around with cursor keys. */
7103 if (p_paste || no_abbr || arrow_used)
7104 return FALSE;
7105
7106 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7107 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7108}
7109
7110/*
7111 * replace-stack functions
7112 *
7113 * When replacing characters, the replaced characters are remembered for each
7114 * new character. This is used to re-insert the old text when backspacing.
7115 *
7116 * There is a NUL headed list of characters for each character that is
7117 * currently in the file after the insertion point. When BS is used, one NUL
7118 * headed list is put back for the deleted character.
7119 *
7120 * For a newline, there are two NUL headed lists. One contains the characters
7121 * that the NL replaced. The extra one stores the characters after the cursor
7122 * that were deleted (always white space).
7123 *
7124 * Replace_offset is normally 0, in which case replace_push will add a new
7125 * character at the end of the stack. If replace_offset is not 0, that many
7126 * characters will be left on the stack above the newly inserted character.
7127 */
7128
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007129static char_u *replace_stack = NULL;
7130static long replace_stack_nr = 0; /* next entry in replace stack */
7131static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
7133 void
7134replace_push(c)
7135 int c; /* character that is replaced (NUL is none) */
7136{
7137 char_u *p;
7138
7139 if (replace_stack_nr < replace_offset) /* nothing to do */
7140 return;
7141 if (replace_stack_len <= replace_stack_nr)
7142 {
7143 replace_stack_len += 50;
7144 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7145 if (p == NULL) /* out of memory */
7146 {
7147 replace_stack_len -= 50;
7148 return;
7149 }
7150 if (replace_stack != NULL)
7151 {
7152 mch_memmove(p, replace_stack,
7153 (size_t)(replace_stack_nr * sizeof(char_u)));
7154 vim_free(replace_stack);
7155 }
7156 replace_stack = p;
7157 }
7158 p = replace_stack + replace_stack_nr - replace_offset;
7159 if (replace_offset)
7160 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7161 *p = c;
7162 ++replace_stack_nr;
7163}
7164
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007165#if defined(FEAT_MBYTE) || defined(PROTO)
7166/*
7167 * Push a character onto the replace stack. Handles a multi-byte character in
7168 * reverse byte order, so that the first byte is popped off first.
7169 * Return the number of bytes done (includes composing characters).
7170 */
7171 int
7172replace_push_mb(p)
7173 char_u *p;
7174{
7175 int l = (*mb_ptr2len)(p);
7176 int j;
7177
7178 for (j = l - 1; j >= 0; --j)
7179 replace_push(p[j]);
7180 return l;
7181}
7182#endif
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184/*
7185 * Pop one item from the replace stack.
7186 * return -1 if stack empty
7187 * return replaced character or NUL otherwise
7188 */
7189 static int
7190replace_pop()
7191{
7192 if (replace_stack_nr == 0)
7193 return -1;
7194 return (int)replace_stack[--replace_stack_nr];
7195}
7196
7197/*
7198 * Join the top two items on the replace stack. This removes to "off"'th NUL
7199 * encountered.
7200 */
7201 static void
7202replace_join(off)
7203 int off; /* offset for which NUL to remove */
7204{
7205 int i;
7206
7207 for (i = replace_stack_nr; --i >= 0; )
7208 if (replace_stack[i] == NUL && off-- <= 0)
7209 {
7210 --replace_stack_nr;
7211 mch_memmove(replace_stack + i, replace_stack + i + 1,
7212 (size_t)(replace_stack_nr - i));
7213 return;
7214 }
7215}
7216
7217/*
7218 * Pop bytes from the replace stack until a NUL is found, and insert them
7219 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7220 */
7221 static void
7222replace_pop_ins()
7223{
7224 int cc;
7225 int oldState = State;
7226
7227 State = NORMAL; /* don't want REPLACE here */
7228 while ((cc = replace_pop()) > 0)
7229 {
7230#ifdef FEAT_MBYTE
7231 mb_replace_pop_ins(cc);
7232#else
7233 ins_char(cc);
7234#endif
7235 dec_cursor();
7236 }
7237 State = oldState;
7238}
7239
7240#ifdef FEAT_MBYTE
7241/*
7242 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7243 * indicates a multi-byte char, pop the other bytes too.
7244 */
7245 static void
7246mb_replace_pop_ins(cc)
7247 int cc;
7248{
7249 int n;
7250 char_u buf[MB_MAXBYTES];
7251 int i;
7252 int c;
7253
7254 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7255 {
7256 buf[0] = cc;
7257 for (i = 1; i < n; ++i)
7258 buf[i] = replace_pop();
7259 ins_bytes_len(buf, n);
7260 }
7261 else
7262 ins_char(cc);
7263
7264 if (enc_utf8)
7265 /* Handle composing chars. */
7266 for (;;)
7267 {
7268 c = replace_pop();
7269 if (c == -1) /* stack empty */
7270 break;
7271 if ((n = MB_BYTE2LEN(c)) == 1)
7272 {
7273 /* Not a multi-byte char, put it back. */
7274 replace_push(c);
7275 break;
7276 }
7277 else
7278 {
7279 buf[0] = c;
7280 for (i = 1; i < n; ++i)
7281 buf[i] = replace_pop();
7282 if (utf_iscomposing(utf_ptr2char(buf)))
7283 ins_bytes_len(buf, n);
7284 else
7285 {
7286 /* Not a composing char, put it back. */
7287 for (i = n - 1; i >= 0; --i)
7288 replace_push(buf[i]);
7289 break;
7290 }
7291 }
7292 }
7293}
7294#endif
7295
7296/*
7297 * make the replace stack empty
7298 * (called when exiting replace mode)
7299 */
7300 static void
7301replace_flush()
7302{
7303 vim_free(replace_stack);
7304 replace_stack = NULL;
7305 replace_stack_len = 0;
7306 replace_stack_nr = 0;
7307}
7308
7309/*
7310 * Handle doing a BS for one character.
7311 * cc < 0: replace stack empty, just move cursor
7312 * cc == 0: character was inserted, delete it
7313 * cc > 0: character was replaced, put cc (first byte of original char) back
7314 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007315 * When "limit_col" is >= 0, don't delete before this column. Matters when
7316 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 */
7318 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007319replace_do_bs(limit_col)
7320 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 int cc;
7323#ifdef FEAT_VREPLACE
7324 int orig_len = 0;
7325 int ins_len;
7326 int orig_vcols = 0;
7327 colnr_T start_vcol;
7328 char_u *p;
7329 int i;
7330 int vcol;
7331#endif
7332
7333 cc = replace_pop();
7334 if (cc > 0)
7335 {
7336#ifdef FEAT_VREPLACE
7337 if (State & VREPLACE_FLAG)
7338 {
7339 /* Get the number of screen cells used by the character we are
7340 * going to delete. */
7341 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7342 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7343 }
7344#endif
7345#ifdef FEAT_MBYTE
7346 if (has_mbyte)
7347 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007348 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349# ifdef FEAT_VREPLACE
7350 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007351 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352# endif
7353 replace_push(cc);
7354 }
7355 else
7356#endif
7357 {
7358 pchar_cursor(cc);
7359#ifdef FEAT_VREPLACE
7360 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362#endif
7363 }
7364 replace_pop_ins();
7365
7366#ifdef FEAT_VREPLACE
7367 if (State & VREPLACE_FLAG)
7368 {
7369 /* Get the number of screen cells used by the inserted characters */
7370 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007371 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 vcol = start_vcol;
7373 for (i = 0; i < ins_len; ++i)
7374 {
7375 vcol += chartabsize(p + i, vcol);
7376#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007377 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378#endif
7379 }
7380 vcol -= start_vcol;
7381
7382 /* Delete spaces that were inserted after the cursor to keep the
7383 * text aligned. */
7384 curwin->w_cursor.col += ins_len;
7385 while (vcol > orig_vcols && gchar_cursor() == ' ')
7386 {
7387 del_char(FALSE);
7388 ++orig_vcols;
7389 }
7390 curwin->w_cursor.col -= ins_len;
7391 }
7392#endif
7393
7394 /* mark the buffer as changed and prepare for displaying */
7395 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7396 }
7397 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007398 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401#ifdef FEAT_CINDENT
7402/*
7403 * Return TRUE if C-indenting is on.
7404 */
7405 static int
7406cindent_on()
7407{
7408 return (!p_paste && (curbuf->b_p_cin
7409# ifdef FEAT_EVAL
7410 || *curbuf->b_p_inde != NUL
7411# endif
7412 ));
7413}
7414#endif
7415
7416#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7417/*
7418 * Re-indent the current line, based on the current contents of it and the
7419 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7420 * confused what all the part that handles Control-T is doing that I'm not.
7421 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7422 */
7423
7424 void
7425fixthisline(get_the_indent)
7426 int (*get_the_indent) __ARGS((void));
7427{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007428 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (linewhite(curwin->w_cursor.lnum))
7430 did_ai = TRUE; /* delete the indent if the line stays empty */
7431}
7432
7433 void
7434fix_indent()
7435{
7436 if (p_paste)
7437 return;
7438# ifdef FEAT_LISP
7439 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7440 fixthisline(get_lisp_indent);
7441# endif
7442# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7443 else
7444# endif
7445# ifdef FEAT_CINDENT
7446 if (cindent_on())
7447 do_c_expr_indent();
7448# endif
7449}
7450
7451#endif
7452
7453#ifdef FEAT_CINDENT
7454/*
7455 * return TRUE if 'cinkeys' contains the key "keytyped",
7456 * when == '*': Only if key is preceded with '*' (indent before insert)
7457 * when == '!': Only if key is prededed with '!' (don't insert)
7458 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7459 *
7460 * "keytyped" can have a few special values:
7461 * KEY_OPEN_FORW
7462 * KEY_OPEN_BACK
7463 * KEY_COMPLETE just finished completion.
7464 *
7465 * If line_is_empty is TRUE accept keys with '0' before them.
7466 */
7467 int
7468in_cinkeys(keytyped, when, line_is_empty)
7469 int keytyped;
7470 int when;
7471 int line_is_empty;
7472{
7473 char_u *look;
7474 int try_match;
7475 int try_match_word;
7476 char_u *p;
7477 char_u *line;
7478 int icase;
7479 int i;
7480
Bram Moolenaar30848942009-12-24 14:46:12 +00007481 if (keytyped == NUL)
7482 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7483 return FALSE;
7484
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485#ifdef FEAT_EVAL
7486 if (*curbuf->b_p_inde != NUL)
7487 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7488 else
7489#endif
7490 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7491 while (*look)
7492 {
7493 /*
7494 * Find out if we want to try a match with this key, depending on
7495 * 'when' and a '*' or '!' before the key.
7496 */
7497 switch (when)
7498 {
7499 case '*': try_match = (*look == '*'); break;
7500 case '!': try_match = (*look == '!'); break;
7501 default: try_match = (*look != '*'); break;
7502 }
7503 if (*look == '*' || *look == '!')
7504 ++look;
7505
7506 /*
7507 * If there is a '0', only accept a match if the line is empty.
7508 * But may still match when typing last char of a word.
7509 */
7510 if (*look == '0')
7511 {
7512 try_match_word = try_match;
7513 if (!line_is_empty)
7514 try_match = FALSE;
7515 ++look;
7516 }
7517 else
7518 try_match_word = FALSE;
7519
7520 /*
7521 * does it look like a control character?
7522 */
7523 if (*look == '^'
7524#ifdef EBCDIC
7525 && (Ctrl_chr(look[1]) != 0)
7526#else
7527 && look[1] >= '?' && look[1] <= '_'
7528#endif
7529 )
7530 {
7531 if (try_match && keytyped == Ctrl_chr(look[1]))
7532 return TRUE;
7533 look += 2;
7534 }
7535 /*
7536 * 'o' means "o" command, open forward.
7537 * 'O' means "O" command, open backward.
7538 */
7539 else if (*look == 'o')
7540 {
7541 if (try_match && keytyped == KEY_OPEN_FORW)
7542 return TRUE;
7543 ++look;
7544 }
7545 else if (*look == 'O')
7546 {
7547 if (try_match && keytyped == KEY_OPEN_BACK)
7548 return TRUE;
7549 ++look;
7550 }
7551
7552 /*
7553 * 'e' means to check for "else" at start of line and just before the
7554 * cursor.
7555 */
7556 else if (*look == 'e')
7557 {
7558 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7559 {
7560 p = ml_get_curline();
7561 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7562 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7563 return TRUE;
7564 }
7565 ++look;
7566 }
7567
7568 /*
7569 * ':' only causes an indent if it is at the end of a label or case
7570 * statement, or when it was before typing the ':' (to fix
7571 * class::method for C++).
7572 */
7573 else if (*look == ':')
7574 {
7575 if (try_match && keytyped == ':')
7576 {
7577 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007578 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7579 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007581 /* Need to get the line again after cin_islabel(). */
7582 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (curwin->w_cursor.col > 2
7584 && p[curwin->w_cursor.col - 1] == ':'
7585 && p[curwin->w_cursor.col - 2] == ':')
7586 {
7587 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007588 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 || cin_islabel(30));
7590 p = ml_get_curline();
7591 p[curwin->w_cursor.col - 1] = ':';
7592 if (i)
7593 return TRUE;
7594 }
7595 }
7596 ++look;
7597 }
7598
7599
7600 /*
7601 * Is it a key in <>, maybe?
7602 */
7603 else if (*look == '<')
7604 {
7605 if (try_match)
7606 {
7607 /*
7608 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7609 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7610 * >, *, : and ! keys if they really really want to.
7611 */
7612 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7613 && keytyped == look[1])
7614 return TRUE;
7615
7616 if (keytyped == get_special_key_code(look + 1))
7617 return TRUE;
7618 }
7619 while (*look && *look != '>')
7620 look++;
7621 while (*look == '>')
7622 look++;
7623 }
7624
7625 /*
7626 * Is it a word: "=word"?
7627 */
7628 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7629 {
7630 ++look;
7631 if (*look == '~')
7632 {
7633 icase = TRUE;
7634 ++look;
7635 }
7636 else
7637 icase = FALSE;
7638 p = vim_strchr(look, ',');
7639 if (p == NULL)
7640 p = look + STRLEN(look);
7641 if ((try_match || try_match_word)
7642 && curwin->w_cursor.col >= (colnr_T)(p - look))
7643 {
7644 int match = FALSE;
7645
7646#ifdef FEAT_INS_EXPAND
7647 if (keytyped == KEY_COMPLETE)
7648 {
7649 char_u *s;
7650
7651 /* Just completed a word, check if it starts with "look".
7652 * search back for the start of a word. */
7653 line = ml_get_curline();
7654# ifdef FEAT_MBYTE
7655 if (has_mbyte)
7656 {
7657 char_u *n;
7658
7659 for (s = line + curwin->w_cursor.col; s > line; s = n)
7660 {
7661 n = mb_prevptr(line, s);
7662 if (!vim_iswordp(n))
7663 break;
7664 }
7665 }
7666 else
7667# endif
7668 for (s = line + curwin->w_cursor.col; s > line; --s)
7669 if (!vim_iswordc(s[-1]))
7670 break;
7671 if (s + (p - look) <= line + curwin->w_cursor.col
7672 && (icase
7673 ? MB_STRNICMP(s, look, p - look)
7674 : STRNCMP(s, look, p - look)) == 0)
7675 match = TRUE;
7676 }
7677 else
7678#endif
7679 /* TODO: multi-byte */
7680 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7681 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7682 {
7683 line = ml_get_cursor();
7684 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7685 || !vim_iswordc(line[-(p - look) - 1]))
7686 && (icase
7687 ? MB_STRNICMP(line - (p - look), look, p - look)
7688 : STRNCMP(line - (p - look), look, p - look))
7689 == 0)
7690 match = TRUE;
7691 }
7692 if (match && try_match_word && !try_match)
7693 {
7694 /* "0=word": Check if there are only blanks before the
7695 * word. */
7696 line = ml_get_curline();
7697 if ((int)(skipwhite(line) - line) !=
7698 (int)(curwin->w_cursor.col - (p - look)))
7699 match = FALSE;
7700 }
7701 if (match)
7702 return TRUE;
7703 }
7704 look = p;
7705 }
7706
7707 /*
7708 * ok, it's a boring generic character.
7709 */
7710 else
7711 {
7712 if (try_match && *look == keytyped)
7713 return TRUE;
7714 ++look;
7715 }
7716
7717 /*
7718 * Skip over ", ".
7719 */
7720 look = skip_to_option_part(look);
7721 }
7722 return FALSE;
7723}
7724#endif /* FEAT_CINDENT */
7725
7726#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7727/*
7728 * Map Hebrew keyboard when in hkmap mode.
7729 */
7730 int
7731hkmap(c)
7732 int c;
7733{
7734 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7735 {
7736 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7737 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7738 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7739 static char_u map[26] =
7740 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7741 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7742 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7743 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7744 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7745 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7746 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7747 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7748 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7749
7750 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7751 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7752 /* '-1'='sofit' */
7753 else if (c == 'x')
7754 return 'X';
7755 else if (c == 'q')
7756 return '\''; /* {geresh}={'} */
7757 else if (c == 246)
7758 return ' '; /* \"o --> ' ' for a german keyboard */
7759 else if (c == 228)
7760 return ' '; /* \"a --> ' ' -- / -- */
7761 else if (c == 252)
7762 return ' '; /* \"u --> ' ' -- / -- */
7763#ifdef EBCDIC
7764 else if (islower(c))
7765#else
7766 /* NOTE: islower() does not do the right thing for us on Linux so we
7767 * do this the same was as 5.7 and previous, so it works correctly on
7768 * all systems. Specifically, the e.g. Delete and Arrow keys are
7769 * munged and won't work if e.g. searching for Hebrew text.
7770 */
7771 else if (c >= 'a' && c <= 'z')
7772#endif
7773 return (int)(map[CharOrdLow(c)] + p_aleph);
7774 else
7775 return c;
7776 }
7777 else
7778 {
7779 switch (c)
7780 {
7781 case '`': return ';';
7782 case '/': return '.';
7783 case '\'': return ',';
7784 case 'q': return '/';
7785 case 'w': return '\'';
7786
7787 /* Hebrew letters - set offset from 'a' */
7788 case ',': c = '{'; break;
7789 case '.': c = 'v'; break;
7790 case ';': c = 't'; break;
7791 default: {
7792 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7793
7794#ifdef EBCDIC
7795 /* see note about islower() above */
7796 if (!islower(c))
7797#else
7798 if (c < 'a' || c > 'z')
7799#endif
7800 return c;
7801 c = str[CharOrdLow(c)];
7802 break;
7803 }
7804 }
7805
7806 return (int)(CharOrdLow(c) + p_aleph);
7807 }
7808}
7809#endif
7810
7811 static void
7812ins_reg()
7813{
7814 int need_redraw = FALSE;
7815 int regname;
7816 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007817#ifdef FEAT_VISUAL
7818 int vis_active = VIsual_active;
7819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821 /*
7822 * If we are going to wait for a character, show a '"'.
7823 */
7824 pc_status = PC_STATUS_UNSET;
7825 if (redrawing() && !char_avail())
7826 {
7827 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007828 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 edit_putchar('"', TRUE);
7831#ifdef FEAT_CMDL_INFO
7832 add_to_showcmd_c(Ctrl_R);
7833#endif
7834 }
7835
7836#ifdef USE_ON_FLY_SCROLL
7837 dont_scroll = TRUE; /* disallow scrolling here */
7838#endif
7839
7840 /*
7841 * Don't map the register name. This also prevents the mode message to be
7842 * deleted when ESC is hit.
7843 */
7844 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007845 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7848 {
7849 /* Get a third key for literal register insertion */
7850 literally = regname;
7851#ifdef FEAT_CMDL_INFO
7852 add_to_showcmd_c(literally);
7853#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007854 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857 --no_mapping;
7858
7859#ifdef FEAT_EVAL
7860 /*
7861 * Don't call u_sync() while getting the expression,
7862 * evaluating it or giving an error message for it!
7863 */
7864 ++no_u_sync;
7865 if (regname == '=')
7866 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007867# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007871# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 /* Restore the Input Method. */
7873 if (im_on)
7874 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007877 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7878 {
7879 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 else
7883 {
7884#endif
7885 if (literally == Ctrl_O || literally == Ctrl_P)
7886 {
7887 /* Append the command to the redo buffer. */
7888 AppendCharToRedobuff(Ctrl_R);
7889 AppendCharToRedobuff(literally);
7890 AppendCharToRedobuff(regname);
7891
7892 do_put(regname, BACKWARD, 1L,
7893 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7894 }
7895 else if (insert_reg(regname, literally) == FAIL)
7896 {
7897 vim_beep();
7898 need_redraw = TRUE; /* remove the '"' */
7899 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007900 else if (stop_insert_mode)
7901 /* When the '=' register was used and a function was invoked that
7902 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7903 * insert anything, need to remove the '"' */
7904 need_redraw = TRUE;
7905
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906#ifdef FEAT_EVAL
7907 }
7908 --no_u_sync;
7909#endif
7910#ifdef FEAT_CMDL_INFO
7911 clear_showcmd();
7912#endif
7913
7914 /* If the inserted register is empty, we need to remove the '"' */
7915 if (need_redraw || stuff_empty())
7916 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007917
7918#ifdef FEAT_VISUAL
7919 /* Disallow starting Visual mode here, would get a weird mode. */
7920 if (!vis_active && VIsual_active)
7921 end_visual_mode();
7922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923}
7924
7925/*
7926 * CTRL-G commands in Insert mode.
7927 */
7928 static void
7929ins_ctrl_g()
7930{
7931 int c;
7932
7933#ifdef FEAT_INS_EXPAND
7934 /* Right after CTRL-X the cursor will be after the ruler. */
7935 setcursor();
7936#endif
7937
7938 /*
7939 * Don't map the second key. This also prevents the mode message to be
7940 * deleted when ESC is hit.
7941 */
7942 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007943 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 --no_mapping;
7945 switch (c)
7946 {
7947 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7948 case K_UP:
7949 case Ctrl_K:
7950 case 'k': ins_up(TRUE);
7951 break;
7952
7953 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7954 case K_DOWN:
7955 case Ctrl_J:
7956 case 'j': ins_down(TRUE);
7957 break;
7958
7959 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007960 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007962
7963 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007964 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007965 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 break;
7967
7968 /* Unknown CTRL-G command, reserved for future expansion. */
7969 default: vim_beep();
7970 }
7971}
7972
7973/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007974 * CTRL-^ in Insert mode.
7975 */
7976 static void
7977ins_ctrl_hat()
7978{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007979 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007980 {
7981 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7982 if (State & LANGMAP)
7983 {
7984 curbuf->b_p_iminsert = B_IMODE_NONE;
7985 State &= ~LANGMAP;
7986 }
7987 else
7988 {
7989 curbuf->b_p_iminsert = B_IMODE_LMAP;
7990 State |= LANGMAP;
7991#ifdef USE_IM_CONTROL
7992 im_set_active(FALSE);
7993#endif
7994 }
7995 }
7996#ifdef USE_IM_CONTROL
7997 else
7998 {
7999 /* There are no ":lmap" mappings, toggle IM */
8000 if (im_get_status())
8001 {
8002 curbuf->b_p_iminsert = B_IMODE_NONE;
8003 im_set_active(FALSE);
8004 }
8005 else
8006 {
8007 curbuf->b_p_iminsert = B_IMODE_IM;
8008 State &= ~LANGMAP;
8009 im_set_active(TRUE);
8010 }
8011 }
8012#endif
8013 set_iminsert_global();
8014 showmode();
8015#ifdef FEAT_GUI
8016 /* may show different cursor shape or color */
8017 if (gui.in_use)
8018 gui_update_cursor(TRUE, FALSE);
8019#endif
8020#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8021 /* Show/unshow value of 'keymap' in status lines. */
8022 status_redraw_curbuf();
8023#endif
8024}
8025
8026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 * Handle ESC in insert mode.
8028 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8029 * insert.
8030 */
8031 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008032ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 long *count;
8034 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008035 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037 int temp;
8038 static int disabled_redraw = FALSE;
8039
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008040#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008041 check_spell_redraw();
8042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043#if defined(FEAT_HANGULIN)
8044# if defined(ESC_CHG_TO_ENG_MODE)
8045 hangul_input_state_set(0);
8046# endif
8047 if (composing_hangul)
8048 {
8049 push_raw_key(composing_hangul_buffer, 2);
8050 composing_hangul = 0;
8051 }
8052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054 temp = curwin->w_cursor.col;
8055 if (disabled_redraw)
8056 {
8057 --RedrawingDisabled;
8058 disabled_redraw = FALSE;
8059 }
8060 if (!arrow_used)
8061 {
8062 /*
8063 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008064 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8065 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 */
8067 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008068 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
8070 /*
8071 * Repeating insert may take a long time. Check for
8072 * interrupt now and then.
8073 */
8074 if (*count > 0)
8075 {
8076 line_breakcheck();
8077 if (got_int)
8078 *count = 0;
8079 }
8080
8081 if (--*count > 0) /* repeat what was typed */
8082 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008083 /* Vi repeats the insert without replacing characters. */
8084 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8085 State &= ~REPLACE_FLAG;
8086
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 (void)start_redo_ins();
8088 if (cmdchar == 'r' || cmdchar == 'v')
8089 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8090 ++RedrawingDisabled;
8091 disabled_redraw = TRUE;
8092 return FALSE; /* repeat the insert */
8093 }
8094 stop_insert(&curwin->w_cursor, TRUE);
8095 undisplay_dollar();
8096 }
8097
8098 /* When an autoindent was removed, curswant stays after the
8099 * indent */
8100 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8101 curwin->w_set_curswant = TRUE;
8102
8103 /* Remember the last Insert position in the '^ mark. */
8104 if (!cmdmod.keepjumps)
8105 curbuf->b_last_insert = curwin->w_cursor;
8106
8107 /*
8108 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008109 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008111 if (!nomove
8112 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#ifdef FEAT_VIRTUALEDIT
8114 || curwin->w_cursor.coladd > 0
8115#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008116 )
8117 && (restart_edit == NUL
8118 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008120 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008122 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123#ifdef FEAT_RIGHTLEFT
8124 && !revins_on
8125#endif
8126 )
8127 {
8128#ifdef FEAT_VIRTUALEDIT
8129 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8130 {
8131 oneleft();
8132 if (restart_edit != NUL)
8133 ++curwin->w_cursor.coladd;
8134 }
8135 else
8136#endif
8137 {
8138 --curwin->w_cursor.col;
8139#ifdef FEAT_MBYTE
8140 /* Correct cursor for multi-byte character. */
8141 if (has_mbyte)
8142 mb_adjust_cursor();
8143#endif
8144 }
8145 }
8146
8147#ifdef USE_IM_CONTROL
8148 /* Disable IM to allow typing English directly for Normal mode commands.
8149 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8150 * well). */
8151 if (!(State & LANGMAP))
8152 im_save_status(&curbuf->b_p_iminsert);
8153 im_set_active(FALSE);
8154#endif
8155
8156 State = NORMAL;
8157 /* need to position cursor again (e.g. when on a TAB ) */
8158 changed_cline_bef_curs();
8159
8160#ifdef FEAT_MOUSE
8161 setmouse();
8162#endif
8163#ifdef CURSOR_SHAPE
8164 ui_cursor_shape(); /* may show different cursor shape */
8165#endif
8166
8167 /*
8168 * When recording or for CTRL-O, need to display the new mode.
8169 * Otherwise remove the mode message.
8170 */
8171 if (Recording || restart_edit != NUL)
8172 showmode();
8173 else if (p_smd)
8174 MSG("");
8175
8176 return TRUE; /* exit Insert mode */
8177}
8178
8179#ifdef FEAT_RIGHTLEFT
8180/*
8181 * Toggle language: hkmap and revins_on.
8182 * Move to end of reverse inserted text.
8183 */
8184 static void
8185ins_ctrl_()
8186{
8187 if (revins_on && revins_chars && revins_scol >= 0)
8188 {
8189 while (gchar_cursor() != NUL && revins_chars--)
8190 ++curwin->w_cursor.col;
8191 }
8192 p_ri = !p_ri;
8193 revins_on = (State == INSERT && p_ri);
8194 if (revins_on)
8195 {
8196 revins_scol = curwin->w_cursor.col;
8197 revins_legal++;
8198 revins_chars = 0;
8199 undisplay_dollar();
8200 }
8201 else
8202 revins_scol = -1;
8203#ifdef FEAT_FKMAP
8204 if (p_altkeymap)
8205 {
8206 /*
8207 * to be consistent also for redo command, using '.'
8208 * set arrow_used to true and stop it - causing to redo
8209 * characters entered in one mode (normal/reverse insert).
8210 */
8211 arrow_used = TRUE;
8212 (void)stop_arrow();
8213 p_fkmap = curwin->w_p_rl ^ p_ri;
8214 if (p_fkmap && p_ri)
8215 State = INSERT;
8216 }
8217 else
8218#endif
8219 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8220 showmode();
8221}
8222#endif
8223
8224#ifdef FEAT_VISUAL
8225/*
8226 * If 'keymodel' contains "startsel", may start selection.
8227 * Returns TRUE when a CTRL-O and other keys stuffed.
8228 */
8229 static int
8230ins_start_select(c)
8231 int c;
8232{
8233 if (km_startsel)
8234 switch (c)
8235 {
8236 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 case K_PAGEUP:
8239 case K_KPAGEUP:
8240 case K_PAGEDOWN:
8241 case K_KPAGEDOWN:
8242# ifdef MACOS
8243 case K_LEFT:
8244 case K_RIGHT:
8245 case K_UP:
8246 case K_DOWN:
8247 case K_END:
8248 case K_HOME:
8249# endif
8250 if (!(mod_mask & MOD_MASK_SHIFT))
8251 break;
8252 /* FALLTHROUGH */
8253 case K_S_LEFT:
8254 case K_S_RIGHT:
8255 case K_S_UP:
8256 case K_S_DOWN:
8257 case K_S_END:
8258 case K_S_HOME:
8259 /* Start selection right away, the cursor can move with
8260 * CTRL-O when beyond the end of the line. */
8261 start_selection();
8262
8263 /* Execute the key in (insert) Select mode. */
8264 stuffcharReadbuff(Ctrl_O);
8265 if (mod_mask)
8266 {
8267 char_u buf[4];
8268
8269 buf[0] = K_SPECIAL;
8270 buf[1] = KS_MODIFIER;
8271 buf[2] = mod_mask;
8272 buf[3] = NUL;
8273 stuffReadbuff(buf);
8274 }
8275 stuffcharReadbuff(c);
8276 return TRUE;
8277 }
8278 return FALSE;
8279}
8280#endif
8281
8282/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008283 * <Insert> key in Insert mode: toggle insert/remplace mode.
8284 */
8285 static void
8286ins_insert(replaceState)
8287 int replaceState;
8288{
8289#ifdef FEAT_FKMAP
8290 if (p_fkmap && p_ri)
8291 {
8292 beep_flush();
8293 EMSG(farsi_text_3); /* encoded in Farsi */
8294 return;
8295 }
8296#endif
8297
8298#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008299# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008300 set_vim_var_string(VV_INSERTMODE,
8301 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008302# ifdef FEAT_VREPLACE
8303 replaceState == VREPLACE ? "v" :
8304# endif
8305 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008306# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008307 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8308#endif
8309 if (State & REPLACE_FLAG)
8310 State = INSERT | (State & LANGMAP);
8311 else
8312 State = replaceState | (State & LANGMAP);
8313 AppendCharToRedobuff(K_INS);
8314 showmode();
8315#ifdef CURSOR_SHAPE
8316 ui_cursor_shape(); /* may show different cursor shape */
8317#endif
8318}
8319
8320/*
8321 * Pressed CTRL-O in Insert mode.
8322 */
8323 static void
8324ins_ctrl_o()
8325{
8326#ifdef FEAT_VREPLACE
8327 if (State & VREPLACE_FLAG)
8328 restart_edit = 'V';
8329 else
8330#endif
8331 if (State & REPLACE_FLAG)
8332 restart_edit = 'R';
8333 else
8334 restart_edit = 'I';
8335#ifdef FEAT_VIRTUALEDIT
8336 if (virtual_active())
8337 ins_at_eol = FALSE; /* cursor always keeps its column */
8338 else
8339#endif
8340 ins_at_eol = (gchar_cursor() == NUL);
8341}
8342
8343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 * If the cursor is on an indent, ^T/^D insert/delete one
8345 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008346 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 * with vi. But vi only supports ^T and ^D after an
8348 * autoindent, we support it everywhere.
8349 */
8350 static void
8351ins_shift(c, lastc)
8352 int c;
8353 int lastc;
8354{
8355 if (stop_arrow() == FAIL)
8356 return;
8357 AppendCharToRedobuff(c);
8358
8359 /*
8360 * 0^D and ^^D: remove all indent.
8361 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008362 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8363 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
8365 --curwin->w_cursor.col;
8366 (void)del_char(FALSE); /* delete the '^' or '0' */
8367 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8368 if (State & REPLACE_FLAG)
8369 replace_pop_ins();
8370 if (lastc == '^')
8371 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008372 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 }
8374 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008375 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376
8377 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8378 did_ai = FALSE;
8379#ifdef FEAT_SMARTINDENT
8380 did_si = FALSE;
8381 can_si = FALSE;
8382 can_si_back = FALSE;
8383#endif
8384#ifdef FEAT_CINDENT
8385 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8386#endif
8387}
8388
8389 static void
8390ins_del()
8391{
8392 int temp;
8393
8394 if (stop_arrow() == FAIL)
8395 return;
8396 if (gchar_cursor() == NUL) /* delete newline */
8397 {
8398 temp = curwin->w_cursor.col;
8399 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008400 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 vim_beep();
8402 else
8403 curwin->w_cursor.col = temp;
8404 }
8405 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8406 vim_beep();
8407 did_ai = FALSE;
8408#ifdef FEAT_SMARTINDENT
8409 did_si = FALSE;
8410 can_si = FALSE;
8411 can_si_back = FALSE;
8412#endif
8413 AppendCharToRedobuff(K_DEL);
8414}
8415
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008416static void ins_bs_one __ARGS((colnr_T *vcolp));
8417
8418/*
8419 * Delete one character for ins_bs().
8420 */
8421 static void
8422ins_bs_one(vcolp)
8423 colnr_T *vcolp;
8424{
8425 dec_cursor();
8426 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8427 if (State & REPLACE_FLAG)
8428 {
8429 /* Don't delete characters before the insert point when in
8430 * Replace mode */
8431 if (curwin->w_cursor.lnum != Insstart.lnum
8432 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008433 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008434 }
8435 else
8436 (void)del_char(FALSE);
8437}
8438
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439/*
8440 * Handle Backspace, delete-word and delete-line in Insert mode.
8441 * Return TRUE when backspace was actually used.
8442 */
8443 static int
8444ins_bs(c, mode, inserted_space_p)
8445 int c;
8446 int mode;
8447 int *inserted_space_p;
8448{
8449 linenr_T lnum;
8450 int cc;
8451 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008452 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 colnr_T mincol;
8454 int did_backspace = FALSE;
8455 int in_indent;
8456 int oldState;
8457#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008458 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459#endif
8460
8461 /*
8462 * can't delete anything in an empty file
8463 * can't backup past first character in buffer
8464 * can't backup past starting point unless 'backspace' > 1
8465 * can backup to a previous line if 'backspace' == 0
8466 */
8467 if ( bufempty()
8468 || (
8469#ifdef FEAT_RIGHTLEFT
8470 !revins_on &&
8471#endif
8472 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8473 || (!can_bs(BS_START)
8474 && (arrow_used
8475 || (curwin->w_cursor.lnum == Insstart.lnum
8476 && curwin->w_cursor.col <= Insstart.col)))
8477 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8478 && curwin->w_cursor.col <= ai_col)
8479 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8480 {
8481 vim_beep();
8482 return FALSE;
8483 }
8484
8485 if (stop_arrow() == FAIL)
8486 return FALSE;
8487 in_indent = inindent(0);
8488#ifdef FEAT_CINDENT
8489 if (in_indent)
8490 can_cindent = FALSE;
8491#endif
8492#ifdef FEAT_COMMENTS
8493 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8494#endif
8495#ifdef FEAT_RIGHTLEFT
8496 if (revins_on) /* put cursor after last inserted char */
8497 inc_cursor();
8498#endif
8499
8500#ifdef FEAT_VIRTUALEDIT
8501 /* Virtualedit:
8502 * BACKSPACE_CHAR eats a virtual space
8503 * BACKSPACE_WORD eats all coladd
8504 * BACKSPACE_LINE eats all coladd and keeps going
8505 */
8506 if (curwin->w_cursor.coladd > 0)
8507 {
8508 if (mode == BACKSPACE_CHAR)
8509 {
8510 --curwin->w_cursor.coladd;
8511 return TRUE;
8512 }
8513 if (mode == BACKSPACE_WORD)
8514 {
8515 curwin->w_cursor.coladd = 0;
8516 return TRUE;
8517 }
8518 curwin->w_cursor.coladd = 0;
8519 }
8520#endif
8521
8522 /*
8523 * delete newline!
8524 */
8525 if (curwin->w_cursor.col == 0)
8526 {
8527 lnum = Insstart.lnum;
8528 if (curwin->w_cursor.lnum == Insstart.lnum
8529#ifdef FEAT_RIGHTLEFT
8530 || revins_on
8531#endif
8532 )
8533 {
8534 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8535 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8536 return FALSE;
8537 --Insstart.lnum;
8538 Insstart.col = MAXCOL;
8539 }
8540 /*
8541 * In replace mode:
8542 * cc < 0: NL was inserted, delete it
8543 * cc >= 0: NL was replaced, put original characters back
8544 */
8545 cc = -1;
8546 if (State & REPLACE_FLAG)
8547 cc = replace_pop(); /* returns -1 if NL was inserted */
8548 /*
8549 * In replace mode, in the line we started replacing, we only move the
8550 * cursor.
8551 */
8552 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8553 {
8554 dec_cursor();
8555 }
8556 else
8557 {
8558#ifdef FEAT_VREPLACE
8559 if (!(State & VREPLACE_FLAG)
8560 || curwin->w_cursor.lnum > orig_line_count)
8561#endif
8562 {
8563 temp = gchar_cursor(); /* remember current char */
8564 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008565
8566 /* When "aw" is in 'formatoptions' we must delete the space at
8567 * the end of the line, otherwise the line will be broken
8568 * again when auto-formatting. */
8569 if (has_format_option(FO_AUTO)
8570 && has_format_option(FO_WHITE_PAR))
8571 {
8572 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8573 TRUE);
8574 int len;
8575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008576 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008577 if (len > 0 && ptr[len - 1] == ' ')
8578 ptr[len - 1] = NUL;
8579 }
8580
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008581 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 if (temp == NUL && gchar_cursor() != NUL)
8583 inc_cursor();
8584 }
8585#ifdef FEAT_VREPLACE
8586 else
8587 dec_cursor();
8588#endif
8589
8590 /*
8591 * In REPLACE mode we have to put back the text that was replaced
8592 * by the NL. On the replace stack is first a NUL-terminated
8593 * sequence of characters that were deleted and then the
8594 * characters that NL replaced.
8595 */
8596 if (State & REPLACE_FLAG)
8597 {
8598 /*
8599 * Do the next ins_char() in NORMAL state, to
8600 * prevent ins_char() from replacing characters and
8601 * avoiding showmatch().
8602 */
8603 oldState = State;
8604 State = NORMAL;
8605 /*
8606 * restore characters (blanks) deleted after cursor
8607 */
8608 while (cc > 0)
8609 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008610 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611#ifdef FEAT_MBYTE
8612 mb_replace_pop_ins(cc);
8613#else
8614 ins_char(cc);
8615#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008616 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 cc = replace_pop();
8618 }
8619 /* restore the characters that NL replaced */
8620 replace_pop_ins();
8621 State = oldState;
8622 }
8623 }
8624 did_ai = FALSE;
8625 }
8626 else
8627 {
8628 /*
8629 * Delete character(s) before the cursor.
8630 */
8631#ifdef FEAT_RIGHTLEFT
8632 if (revins_on) /* put cursor on last inserted char */
8633 dec_cursor();
8634#endif
8635 mincol = 0;
8636 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008637 if (mode == BACKSPACE_LINE
8638 && (curbuf->b_p_ai
8639#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008640 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008641#endif
8642 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#ifdef FEAT_RIGHTLEFT
8644 && !revins_on
8645#endif
8646 )
8647 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008648 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008650 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008652 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
8654
8655 /*
8656 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8657 */
8658 if ( mode == BACKSPACE_CHAR
8659 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008660 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008661 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 && (*(ml_get_cursor() - 1) == TAB
8663 || (*(ml_get_cursor() - 1) == ' '
8664 && (!*inserted_space_p
8665 || arrow_used))))))
8666 {
8667 int ts;
8668 colnr_T vcol;
8669 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008670 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
8672 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008673 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 ts = curbuf->b_p_sw;
8675 else
8676 ts = curbuf->b_p_sts;
8677 /* Compute the virtual column where we want to be. Since
8678 * 'showbreak' may get in the way, need to get the last column of
8679 * the previous character. */
8680 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008681 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 dec_cursor();
8683 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8684 inc_cursor();
8685 want_vcol = (want_vcol / ts) * ts;
8686
8687 /* delete characters until we are at or before want_vcol */
8688 while (vcol > want_vcol
8689 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008690 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 /* insert extra spaces until we are at want_vcol */
8693 while (vcol < want_vcol)
8694 {
8695 /* Remember the first char we inserted */
8696 if (curwin->w_cursor.lnum == Insstart.lnum
8697 && curwin->w_cursor.col < Insstart.col)
8698 Insstart.col = curwin->w_cursor.col;
8699
8700#ifdef FEAT_VREPLACE
8701 if (State & VREPLACE_FLAG)
8702 ins_char(' ');
8703 else
8704#endif
8705 {
8706 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008707 if ((State & REPLACE_FLAG))
8708 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8711 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008712
8713 /* If we are now back where we started delete one character. Can
8714 * happen when using 'sts' and 'linebreak'. */
8715 if (vcol >= start_vcol)
8716 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 }
8718
8719 /*
8720 * Delete upto starting point, start of line or previous word.
8721 */
8722 else do
8723 {
8724#ifdef FEAT_RIGHTLEFT
8725 if (!revins_on) /* put cursor on char to be deleted */
8726#endif
8727 dec_cursor();
8728
8729 /* start of word? */
8730 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8731 {
8732 mode = BACKSPACE_WORD_NOT_SPACE;
8733 temp = vim_iswordc(gchar_cursor());
8734 }
8735 /* end of word? */
8736 else if (mode == BACKSPACE_WORD_NOT_SPACE
8737 && (vim_isspace(cc = gchar_cursor())
8738 || vim_iswordc(cc) != temp))
8739 {
8740#ifdef FEAT_RIGHTLEFT
8741 if (!revins_on)
8742#endif
8743 inc_cursor();
8744#ifdef FEAT_RIGHTLEFT
8745 else if (State & REPLACE_FLAG)
8746 dec_cursor();
8747#endif
8748 break;
8749 }
8750 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008751 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 else
8753 {
8754#ifdef FEAT_MBYTE
8755 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008756 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#endif
8758 (void)del_char(FALSE);
8759#ifdef FEAT_MBYTE
8760 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008761 * If there are combining characters and 'delcombine' is set
8762 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 * character.
8764 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008765 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 inc_cursor();
8767#endif
8768#ifdef FEAT_RIGHTLEFT
8769 if (revins_chars)
8770 {
8771 revins_chars--;
8772 revins_legal++;
8773 }
8774 if (revins_on && gchar_cursor() == NUL)
8775 break;
8776#endif
8777 }
8778 /* Just a single backspace?: */
8779 if (mode == BACKSPACE_CHAR)
8780 break;
8781 } while (
8782#ifdef FEAT_RIGHTLEFT
8783 revins_on ||
8784#endif
8785 (curwin->w_cursor.col > mincol
8786 && (curwin->w_cursor.lnum != Insstart.lnum
8787 || curwin->w_cursor.col != Insstart.col)));
8788 did_backspace = TRUE;
8789 }
8790#ifdef FEAT_SMARTINDENT
8791 did_si = FALSE;
8792 can_si = FALSE;
8793 can_si_back = FALSE;
8794#endif
8795 if (curwin->w_cursor.col <= 1)
8796 did_ai = FALSE;
8797 /*
8798 * It's a little strange to put backspaces into the redo
8799 * buffer, but it makes auto-indent a lot easier to deal
8800 * with.
8801 */
8802 AppendCharToRedobuff(c);
8803
8804 /* If deleted before the insertion point, adjust it */
8805 if (curwin->w_cursor.lnum == Insstart.lnum
8806 && curwin->w_cursor.col < Insstart.col)
8807 Insstart.col = curwin->w_cursor.col;
8808
8809 /* vi behaviour: the cursor moves backward but the character that
8810 * was there remains visible
8811 * Vim behaviour: the cursor moves backward and the character that
8812 * was there is erased from the screen.
8813 * We can emulate the vi behaviour by pretending there is a dollar
8814 * displayed even when there isn't.
8815 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8816 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8817 dollar_vcol = curwin->w_virtcol;
8818
Bram Moolenaarce3be472008-01-14 19:12:28 +00008819#ifdef FEAT_FOLDING
8820 /* When deleting a char the cursor line must never be in a closed fold.
8821 * E.g., when 'foldmethod' is indent and deleting the first non-white
8822 * char before a Tab. */
8823 if (did_backspace)
8824 foldOpenCursor();
8825#endif
8826
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 return did_backspace;
8828}
8829
8830#ifdef FEAT_MOUSE
8831 static void
8832ins_mouse(c)
8833 int c;
8834{
8835 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008836 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838# ifdef FEAT_GUI
8839 /* When GUI is active, also move/paste when 'mouse' is empty */
8840 if (!gui.in_use)
8841# endif
8842 if (!mouse_has(MOUSE_INSERT))
8843 return;
8844
8845 undisplay_dollar();
8846 tpos = curwin->w_cursor;
8847 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8848 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008849#ifdef FEAT_WINDOWS
8850 win_T *new_curwin = curwin;
8851
8852 if (curwin != old_curwin && win_valid(old_curwin))
8853 {
8854 /* Mouse took us to another window. We need to go back to the
8855 * previous one to stop insert there properly. */
8856 curwin = old_curwin;
8857 curbuf = curwin->w_buffer;
8858 }
8859#endif
8860 start_arrow(curwin == old_curwin ? &tpos : NULL);
8861#ifdef FEAT_WINDOWS
8862 if (curwin != new_curwin && win_valid(new_curwin))
8863 {
8864 curwin = new_curwin;
8865 curbuf = curwin->w_buffer;
8866 }
8867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868# ifdef FEAT_CINDENT
8869 can_cindent = TRUE;
8870# endif
8871 }
8872
8873#ifdef FEAT_WINDOWS
8874 /* redraw status lines (in case another window became active) */
8875 redraw_statuslines();
8876#endif
8877}
8878
8879 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008880ins_mousescroll(dir)
8881 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008884# if defined(FEAT_WINDOWS)
8885 win_T *old_curwin = curwin;
8886# endif
8887# ifdef FEAT_INS_EXPAND
8888 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889# endif
8890
8891 tpos = curwin->w_cursor;
8892
8893# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 /* Currently the mouse coordinates are only known in the GUI. */
8895 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8896 {
8897 int row, col;
8898
8899 row = mouse_row;
8900 col = mouse_col;
8901
8902 /* find the window at the pointer coordinates */
8903 curwin = mouse_find_win(&row, &col);
8904 curbuf = curwin->w_buffer;
8905 }
8906 if (curwin == old_curwin)
8907# endif
8908 undisplay_dollar();
8909
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008910# ifdef FEAT_INS_EXPAND
8911 /* Don't scroll the window in which completion is being done. */
8912 if (!pum_visible()
8913# if defined(FEAT_WINDOWS)
8914 || curwin != old_curwin
8915# endif
8916 )
8917# endif
8918 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008919 if (dir == MSCR_DOWN || dir == MSCR_UP)
8920 {
8921 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8922 scroll_redraw(dir,
8923 (long)(curwin->w_botline - curwin->w_topline));
8924 else
8925 scroll_redraw(dir, 3L);
8926 }
8927#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008928 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008929 {
8930 int val, step = 6;
8931
8932 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8933 step = W_WIDTH(curwin);
8934 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
8935 if (val < 0)
8936 val = 0;
8937 gui_do_horiz_scroll(val, TRUE);
8938 }
8939#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008940# ifdef FEAT_INS_EXPAND
8941 did_scroll = TRUE;
8942# endif
8943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8946 curwin->w_redr_status = TRUE;
8947
8948 curwin = old_curwin;
8949 curbuf = curwin->w_buffer;
8950# endif
8951
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008952# ifdef FEAT_INS_EXPAND
8953 /* The popup menu may overlay the window, need to redraw it.
8954 * TODO: Would be more efficient to only redraw the windows that are
8955 * overlapped by the popup menu. */
8956 if (pum_visible() && did_scroll)
8957 {
8958 redraw_all_later(NOT_VALID);
8959 ins_compl_show_pum();
8960 }
8961# endif
8962
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (!equalpos(curwin->w_cursor, tpos))
8964 {
8965 start_arrow(&tpos);
8966# ifdef FEAT_CINDENT
8967 can_cindent = TRUE;
8968# endif
8969 }
8970}
8971#endif
8972
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008973#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008974 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008975ins_tabline(c)
8976 int c;
8977{
8978 /* We will be leaving the current window, unless closing another tab. */
8979 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8980 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8981 {
8982 undisplay_dollar();
8983 start_arrow(&curwin->w_cursor);
8984# ifdef FEAT_CINDENT
8985 can_cindent = TRUE;
8986# endif
8987 }
8988
8989 if (c == K_TABLINE)
8990 goto_tabpage(current_tab);
8991 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008992 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008993 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008994 redraw_statuslines(); /* will redraw the tabline when needed */
8995 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008996}
8997#endif
8998
8999#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 void
9001ins_scroll()
9002{
9003 pos_T tpos;
9004
9005 undisplay_dollar();
9006 tpos = curwin->w_cursor;
9007 if (gui_do_scroll())
9008 {
9009 start_arrow(&tpos);
9010# ifdef FEAT_CINDENT
9011 can_cindent = TRUE;
9012# endif
9013 }
9014}
9015
9016 void
9017ins_horscroll()
9018{
9019 pos_T tpos;
9020
9021 undisplay_dollar();
9022 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009023 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 {
9025 start_arrow(&tpos);
9026# ifdef FEAT_CINDENT
9027 can_cindent = TRUE;
9028# endif
9029 }
9030}
9031#endif
9032
9033 static void
9034ins_left()
9035{
9036 pos_T tpos;
9037
9038#ifdef FEAT_FOLDING
9039 if ((fdo_flags & FDO_HOR) && KeyTyped)
9040 foldOpenCursor();
9041#endif
9042 undisplay_dollar();
9043 tpos = curwin->w_cursor;
9044 if (oneleft() == OK)
9045 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009046#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9047 /* Only call start_arrow() when not busy with preediting, it will
9048 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9049 if (!im_is_preediting())
9050#endif
9051 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052#ifdef FEAT_RIGHTLEFT
9053 /* If exit reversed string, position is fixed */
9054 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9055 revins_legal++;
9056 revins_chars++;
9057#endif
9058 }
9059
9060 /*
9061 * if 'whichwrap' set for cursor in insert mode may go to
9062 * previous line
9063 */
9064 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9065 {
9066 start_arrow(&tpos);
9067 --(curwin->w_cursor.lnum);
9068 coladvance((colnr_T)MAXCOL);
9069 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9070 }
9071 else
9072 vim_beep();
9073}
9074
9075 static void
9076ins_home(c)
9077 int c;
9078{
9079 pos_T tpos;
9080
9081#ifdef FEAT_FOLDING
9082 if ((fdo_flags & FDO_HOR) && KeyTyped)
9083 foldOpenCursor();
9084#endif
9085 undisplay_dollar();
9086 tpos = curwin->w_cursor;
9087 if (c == K_C_HOME)
9088 curwin->w_cursor.lnum = 1;
9089 curwin->w_cursor.col = 0;
9090#ifdef FEAT_VIRTUALEDIT
9091 curwin->w_cursor.coladd = 0;
9092#endif
9093 curwin->w_curswant = 0;
9094 start_arrow(&tpos);
9095}
9096
9097 static void
9098ins_end(c)
9099 int c;
9100{
9101 pos_T tpos;
9102
9103#ifdef FEAT_FOLDING
9104 if ((fdo_flags & FDO_HOR) && KeyTyped)
9105 foldOpenCursor();
9106#endif
9107 undisplay_dollar();
9108 tpos = curwin->w_cursor;
9109 if (c == K_C_END)
9110 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9111 coladvance((colnr_T)MAXCOL);
9112 curwin->w_curswant = MAXCOL;
9113
9114 start_arrow(&tpos);
9115}
9116
9117 static void
9118ins_s_left()
9119{
9120#ifdef FEAT_FOLDING
9121 if ((fdo_flags & FDO_HOR) && KeyTyped)
9122 foldOpenCursor();
9123#endif
9124 undisplay_dollar();
9125 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9126 {
9127 start_arrow(&curwin->w_cursor);
9128 (void)bck_word(1L, FALSE, FALSE);
9129 curwin->w_set_curswant = TRUE;
9130 }
9131 else
9132 vim_beep();
9133}
9134
9135 static void
9136ins_right()
9137{
9138#ifdef FEAT_FOLDING
9139 if ((fdo_flags & FDO_HOR) && KeyTyped)
9140 foldOpenCursor();
9141#endif
9142 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009143 if (gchar_cursor() != NUL
9144#ifdef FEAT_VIRTUALEDIT
9145 || virtual_active()
9146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 )
9148 {
9149 start_arrow(&curwin->w_cursor);
9150 curwin->w_set_curswant = TRUE;
9151#ifdef FEAT_VIRTUALEDIT
9152 if (virtual_active())
9153 oneright();
9154 else
9155#endif
9156 {
9157#ifdef FEAT_MBYTE
9158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009159 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 else
9161#endif
9162 ++curwin->w_cursor.col;
9163 }
9164
9165#ifdef FEAT_RIGHTLEFT
9166 revins_legal++;
9167 if (revins_chars)
9168 revins_chars--;
9169#endif
9170 }
9171 /* if 'whichwrap' set for cursor in insert mode, may move the
9172 * cursor to the next line */
9173 else if (vim_strchr(p_ww, ']') != NULL
9174 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9175 {
9176 start_arrow(&curwin->w_cursor);
9177 curwin->w_set_curswant = TRUE;
9178 ++curwin->w_cursor.lnum;
9179 curwin->w_cursor.col = 0;
9180 }
9181 else
9182 vim_beep();
9183}
9184
9185 static void
9186ins_s_right()
9187{
9188#ifdef FEAT_FOLDING
9189 if ((fdo_flags & FDO_HOR) && KeyTyped)
9190 foldOpenCursor();
9191#endif
9192 undisplay_dollar();
9193 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9194 || gchar_cursor() != NUL)
9195 {
9196 start_arrow(&curwin->w_cursor);
9197 (void)fwd_word(1L, FALSE, 0);
9198 curwin->w_set_curswant = TRUE;
9199 }
9200 else
9201 vim_beep();
9202}
9203
9204 static void
9205ins_up(startcol)
9206 int startcol; /* when TRUE move to Insstart.col */
9207{
9208 pos_T tpos;
9209 linenr_T old_topline = curwin->w_topline;
9210#ifdef FEAT_DIFF
9211 int old_topfill = curwin->w_topfill;
9212#endif
9213
9214 undisplay_dollar();
9215 tpos = curwin->w_cursor;
9216 if (cursor_up(1L, TRUE) == OK)
9217 {
9218 if (startcol)
9219 coladvance(getvcol_nolist(&Insstart));
9220 if (old_topline != curwin->w_topline
9221#ifdef FEAT_DIFF
9222 || old_topfill != curwin->w_topfill
9223#endif
9224 )
9225 redraw_later(VALID);
9226 start_arrow(&tpos);
9227#ifdef FEAT_CINDENT
9228 can_cindent = TRUE;
9229#endif
9230 }
9231 else
9232 vim_beep();
9233}
9234
9235 static void
9236ins_pageup()
9237{
9238 pos_T tpos;
9239
9240 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009241
9242#ifdef FEAT_WINDOWS
9243 if (mod_mask & MOD_MASK_CTRL)
9244 {
9245 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009246 if (first_tabpage->tp_next != NULL)
9247 {
9248 start_arrow(&curwin->w_cursor);
9249 goto_tabpage(-1);
9250 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009251 return;
9252 }
9253#endif
9254
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 tpos = curwin->w_cursor;
9256 if (onepage(BACKWARD, 1L) == OK)
9257 {
9258 start_arrow(&tpos);
9259#ifdef FEAT_CINDENT
9260 can_cindent = TRUE;
9261#endif
9262 }
9263 else
9264 vim_beep();
9265}
9266
9267 static void
9268ins_down(startcol)
9269 int startcol; /* when TRUE move to Insstart.col */
9270{
9271 pos_T tpos;
9272 linenr_T old_topline = curwin->w_topline;
9273#ifdef FEAT_DIFF
9274 int old_topfill = curwin->w_topfill;
9275#endif
9276
9277 undisplay_dollar();
9278 tpos = curwin->w_cursor;
9279 if (cursor_down(1L, TRUE) == OK)
9280 {
9281 if (startcol)
9282 coladvance(getvcol_nolist(&Insstart));
9283 if (old_topline != curwin->w_topline
9284#ifdef FEAT_DIFF
9285 || old_topfill != curwin->w_topfill
9286#endif
9287 )
9288 redraw_later(VALID);
9289 start_arrow(&tpos);
9290#ifdef FEAT_CINDENT
9291 can_cindent = TRUE;
9292#endif
9293 }
9294 else
9295 vim_beep();
9296}
9297
9298 static void
9299ins_pagedown()
9300{
9301 pos_T tpos;
9302
9303 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009304
9305#ifdef FEAT_WINDOWS
9306 if (mod_mask & MOD_MASK_CTRL)
9307 {
9308 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009309 if (first_tabpage->tp_next != NULL)
9310 {
9311 start_arrow(&curwin->w_cursor);
9312 goto_tabpage(0);
9313 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009314 return;
9315 }
9316#endif
9317
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 tpos = curwin->w_cursor;
9319 if (onepage(FORWARD, 1L) == OK)
9320 {
9321 start_arrow(&tpos);
9322#ifdef FEAT_CINDENT
9323 can_cindent = TRUE;
9324#endif
9325 }
9326 else
9327 vim_beep();
9328}
9329
9330#ifdef FEAT_DND
9331 static void
9332ins_drop()
9333{
9334 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9335}
9336#endif
9337
9338/*
9339 * Handle TAB in Insert or Replace mode.
9340 * Return TRUE when the TAB needs to be inserted like a normal character.
9341 */
9342 static int
9343ins_tab()
9344{
9345 int ind;
9346 int i;
9347 int temp;
9348
9349 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9350 Insstart_blank_vcol = get_nolist_virtcol();
9351 if (echeck_abbr(TAB + ABBR_OFF))
9352 return FALSE;
9353
9354 ind = inindent(0);
9355#ifdef FEAT_CINDENT
9356 if (ind)
9357 can_cindent = FALSE;
9358#endif
9359
9360 /*
9361 * When nothing special, insert TAB like a normal character
9362 */
9363 if (!curbuf->b_p_et
9364 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9365 && curbuf->b_p_sts == 0)
9366 return TRUE;
9367
9368 if (stop_arrow() == FAIL)
9369 return TRUE;
9370
9371 did_ai = FALSE;
9372#ifdef FEAT_SMARTINDENT
9373 did_si = FALSE;
9374 can_si = FALSE;
9375 can_si_back = FALSE;
9376#endif
9377 AppendToRedobuff((char_u *)"\t");
9378
9379 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9380 temp = (int)curbuf->b_p_sw;
9381 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9382 temp = (int)curbuf->b_p_sts;
9383 else /* otherwise use 'tabstop' */
9384 temp = (int)curbuf->b_p_ts;
9385 temp -= get_nolist_virtcol() % temp;
9386
9387 /*
9388 * Insert the first space with ins_char(). It will delete one char in
9389 * replace mode. Insert the rest with ins_str(); it will not delete any
9390 * chars. For VREPLACE mode, we use ins_char() for all characters.
9391 */
9392 ins_char(' ');
9393 while (--temp > 0)
9394 {
9395#ifdef FEAT_VREPLACE
9396 if (State & VREPLACE_FLAG)
9397 ins_char(' ');
9398 else
9399#endif
9400 {
9401 ins_str((char_u *)" ");
9402 if (State & REPLACE_FLAG) /* no char replaced */
9403 replace_push(NUL);
9404 }
9405 }
9406
9407 /*
9408 * When 'expandtab' not set: Replace spaces by TABs where possible.
9409 */
9410 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9411 {
9412 char_u *ptr;
9413#ifdef FEAT_VREPLACE
9414 char_u *saved_line = NULL; /* init for GCC */
9415 pos_T pos;
9416#endif
9417 pos_T fpos;
9418 pos_T *cursor;
9419 colnr_T want_vcol, vcol;
9420 int change_col = -1;
9421 int save_list = curwin->w_p_list;
9422
9423 /*
9424 * Get the current line. For VREPLACE mode, don't make real changes
9425 * yet, just work on a copy of the line.
9426 */
9427#ifdef FEAT_VREPLACE
9428 if (State & VREPLACE_FLAG)
9429 {
9430 pos = curwin->w_cursor;
9431 cursor = &pos;
9432 saved_line = vim_strsave(ml_get_curline());
9433 if (saved_line == NULL)
9434 return FALSE;
9435 ptr = saved_line + pos.col;
9436 }
9437 else
9438#endif
9439 {
9440 ptr = ml_get_cursor();
9441 cursor = &curwin->w_cursor;
9442 }
9443
9444 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9445 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9446 curwin->w_p_list = FALSE;
9447
9448 /* Find first white before the cursor */
9449 fpos = curwin->w_cursor;
9450 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9451 {
9452 --fpos.col;
9453 --ptr;
9454 }
9455
9456 /* In Replace mode, don't change characters before the insert point. */
9457 if ((State & REPLACE_FLAG)
9458 && fpos.lnum == Insstart.lnum
9459 && fpos.col < Insstart.col)
9460 {
9461 ptr += Insstart.col - fpos.col;
9462 fpos.col = Insstart.col;
9463 }
9464
9465 /* compute virtual column numbers of first white and cursor */
9466 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9467 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9468
9469 /* Use as many TABs as possible. Beware of 'showbreak' and
9470 * 'linebreak' adding extra virtual columns. */
9471 while (vim_iswhite(*ptr))
9472 {
9473 i = lbr_chartabsize((char_u *)"\t", vcol);
9474 if (vcol + i > want_vcol)
9475 break;
9476 if (*ptr != TAB)
9477 {
9478 *ptr = TAB;
9479 if (change_col < 0)
9480 {
9481 change_col = fpos.col; /* Column of first change */
9482 /* May have to adjust Insstart */
9483 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9484 Insstart.col = fpos.col;
9485 }
9486 }
9487 ++fpos.col;
9488 ++ptr;
9489 vcol += i;
9490 }
9491
9492 if (change_col >= 0)
9493 {
9494 int repl_off = 0;
9495
9496 /* Skip over the spaces we need. */
9497 while (vcol < want_vcol && *ptr == ' ')
9498 {
9499 vcol += lbr_chartabsize(ptr, vcol);
9500 ++ptr;
9501 ++repl_off;
9502 }
9503 if (vcol > want_vcol)
9504 {
9505 /* Must have a char with 'showbreak' just before it. */
9506 --ptr;
9507 --repl_off;
9508 }
9509 fpos.col += repl_off;
9510
9511 /* Delete following spaces. */
9512 i = cursor->col - fpos.col;
9513 if (i > 0)
9514 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009515 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 /* correct replace stack. */
9517 if ((State & REPLACE_FLAG)
9518#ifdef FEAT_VREPLACE
9519 && !(State & VREPLACE_FLAG)
9520#endif
9521 )
9522 for (temp = i; --temp >= 0; )
9523 replace_join(repl_off);
9524 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009525#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009526 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009527 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009528 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009529 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9530 (char_u *)"\t", 1);
9531 }
9532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 cursor->col -= i;
9534
9535#ifdef FEAT_VREPLACE
9536 /*
9537 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9538 * backspacing over the changed spacing and then inserting the new
9539 * spacing.
9540 */
9541 if (State & VREPLACE_FLAG)
9542 {
9543 /* Backspace from real cursor to change_col */
9544 backspace_until_column(change_col);
9545
9546 /* Insert each char in saved_line from changed_col to
9547 * ptr-cursor */
9548 ins_bytes_len(saved_line + change_col,
9549 cursor->col - change_col);
9550 }
9551#endif
9552 }
9553
9554#ifdef FEAT_VREPLACE
9555 if (State & VREPLACE_FLAG)
9556 vim_free(saved_line);
9557#endif
9558 curwin->w_p_list = save_list;
9559 }
9560
9561 return FALSE;
9562}
9563
9564/*
9565 * Handle CR or NL in insert mode.
9566 * Return TRUE when out of memory or can't undo.
9567 */
9568 static int
9569ins_eol(c)
9570 int c;
9571{
9572 int i;
9573
9574 if (echeck_abbr(c + ABBR_OFF))
9575 return FALSE;
9576 if (stop_arrow() == FAIL)
9577 return TRUE;
9578 undisplay_dollar();
9579
9580 /*
9581 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9582 * character under the cursor. Only push a NUL on the replace stack,
9583 * nothing to put back when the NL is deleted.
9584 */
9585 if ((State & REPLACE_FLAG)
9586#ifdef FEAT_VREPLACE
9587 && !(State & VREPLACE_FLAG)
9588#endif
9589 )
9590 replace_push(NUL);
9591
9592 /*
9593 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9594 * replacing the next line, so we push all of the characters left on the
9595 * line onto the replace stack. This is not done here though, it is done
9596 * in open_line().
9597 */
9598
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009599#ifdef FEAT_VIRTUALEDIT
9600 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9601 * CTRL-O). */
9602 if (virtual_active() && curwin->w_cursor.coladd > 0)
9603 coladvance(getviscol());
9604#endif
9605
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606#ifdef FEAT_RIGHTLEFT
9607# ifdef FEAT_FKMAP
9608 if (p_altkeymap && p_fkmap)
9609 fkmap(NL);
9610# endif
9611 /* NL in reverse insert will always start in the end of
9612 * current line. */
9613 if (revins_on)
9614 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9615#endif
9616
9617 AppendToRedobuff(NL_STR);
9618 i = open_line(FORWARD,
9619#ifdef FEAT_COMMENTS
9620 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9621#endif
9622 0, old_indent);
9623 old_indent = 0;
9624#ifdef FEAT_CINDENT
9625 can_cindent = TRUE;
9626#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009627#ifdef FEAT_FOLDING
9628 /* When inserting a line the cursor line must never be in a closed fold. */
9629 foldOpenCursor();
9630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
9632 return (!i);
9633}
9634
9635#ifdef FEAT_DIGRAPHS
9636/*
9637 * Handle digraph in insert mode.
9638 * Returns character still to be inserted, or NUL when nothing remaining to be
9639 * done.
9640 */
9641 static int
9642ins_digraph()
9643{
9644 int c;
9645 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009646 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647
9648 pc_status = PC_STATUS_UNSET;
9649 if (redrawing() && !char_avail())
9650 {
9651 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009652 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653
9654 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009655 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656#ifdef FEAT_CMDL_INFO
9657 add_to_showcmd_c(Ctrl_K);
9658#endif
9659 }
9660
9661#ifdef USE_ON_FLY_SCROLL
9662 dont_scroll = TRUE; /* disallow scrolling here */
9663#endif
9664
9665 /* don't map the digraph chars. This also prevents the
9666 * mode message to be deleted when ESC is hit */
9667 ++no_mapping;
9668 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009669 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 --no_mapping;
9671 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009672 if (did_putchar)
9673 /* when the line fits in 'columns' the '?' is at the start of the next
9674 * line and will not be removed by the redraw */
9675 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009676
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (IS_SPECIAL(c) || mod_mask) /* special key */
9678 {
9679#ifdef FEAT_CMDL_INFO
9680 clear_showcmd();
9681#endif
9682 insert_special(c, TRUE, FALSE);
9683 return NUL;
9684 }
9685 if (c != ESC)
9686 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009687 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 if (redrawing() && !char_avail())
9689 {
9690 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009691 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 if (char2cells(c) == 1)
9694 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009695 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009697 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 }
9699#ifdef FEAT_CMDL_INFO
9700 add_to_showcmd_c(c);
9701#endif
9702 }
9703 ++no_mapping;
9704 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009705 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 --no_mapping;
9707 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009708 if (did_putchar)
9709 /* when the line fits in 'columns' the '?' is at the start of the
9710 * next line and will not be removed by a redraw */
9711 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 if (cc != ESC)
9713 {
9714 AppendToRedobuff((char_u *)CTRL_V_STR);
9715 c = getdigraph(c, cc, TRUE);
9716#ifdef FEAT_CMDL_INFO
9717 clear_showcmd();
9718#endif
9719 return c;
9720 }
9721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722#ifdef FEAT_CMDL_INFO
9723 clear_showcmd();
9724#endif
9725 return NUL;
9726}
9727#endif
9728
9729/*
9730 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9731 * Returns the char to be inserted, or NUL if none found.
9732 */
9733 static int
9734ins_copychar(lnum)
9735 linenr_T lnum;
9736{
9737 int c;
9738 int temp;
9739 char_u *ptr, *prev_ptr;
9740
9741 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9742 {
9743 vim_beep();
9744 return NUL;
9745 }
9746
9747 /* try to advance to the cursor column */
9748 temp = 0;
9749 ptr = ml_get(lnum);
9750 prev_ptr = ptr;
9751 validate_virtcol();
9752 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9753 {
9754 prev_ptr = ptr;
9755 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9756 }
9757 if ((colnr_T)temp > curwin->w_virtcol)
9758 ptr = prev_ptr;
9759
9760#ifdef FEAT_MBYTE
9761 c = (*mb_ptr2char)(ptr);
9762#else
9763 c = *ptr;
9764#endif
9765 if (c == NUL)
9766 vim_beep();
9767 return c;
9768}
9769
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009770/*
9771 * CTRL-Y or CTRL-E typed in Insert mode.
9772 */
9773 static int
9774ins_ctrl_ey(tc)
9775 int tc;
9776{
9777 int c = tc;
9778
9779#ifdef FEAT_INS_EXPAND
9780 if (ctrl_x_mode == CTRL_X_SCROLL)
9781 {
9782 if (c == Ctrl_Y)
9783 scrolldown_clamp();
9784 else
9785 scrollup_clamp();
9786 redraw_later(VALID);
9787 }
9788 else
9789#endif
9790 {
9791 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9792 if (c != NUL)
9793 {
9794 long tw_save;
9795
9796 /* The character must be taken literally, insert like it
9797 * was typed after a CTRL-V, and pretend 'textwidth'
9798 * wasn't set. Digits, 'o' and 'x' are special after a
9799 * CTRL-V, don't use it for these. */
9800 if (c < 256 && !isalnum(c))
9801 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9802 tw_save = curbuf->b_p_tw;
9803 curbuf->b_p_tw = -1;
9804 insert_special(c, TRUE, FALSE);
9805 curbuf->b_p_tw = tw_save;
9806#ifdef FEAT_RIGHTLEFT
9807 revins_chars++;
9808 revins_legal++;
9809#endif
9810 c = Ctrl_V; /* pretend CTRL-V is last character */
9811 auto_format(FALSE, TRUE);
9812 }
9813 }
9814 return c;
9815}
9816
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817#ifdef FEAT_SMARTINDENT
9818/*
9819 * Try to do some very smart auto-indenting.
9820 * Used when inserting a "normal" character.
9821 */
9822 static void
9823ins_try_si(c)
9824 int c;
9825{
9826 pos_T *pos, old_pos;
9827 char_u *ptr;
9828 int i;
9829 int temp;
9830
9831 /*
9832 * do some very smart indenting when entering '{' or '}'
9833 */
9834 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9835 {
9836 /*
9837 * for '}' set indent equal to indent of line containing matching '{'
9838 */
9839 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9840 {
9841 old_pos = curwin->w_cursor;
9842 /*
9843 * If the matching '{' has a ')' immediately before it (ignoring
9844 * white-space), then line up with the start of the line
9845 * containing the matching '(' if there is one. This handles the
9846 * case where an "if (..\n..) {" statement continues over multiple
9847 * lines -- webb
9848 */
9849 ptr = ml_get(pos->lnum);
9850 i = pos->col;
9851 if (i > 0) /* skip blanks before '{' */
9852 while (--i > 0 && vim_iswhite(ptr[i]))
9853 ;
9854 curwin->w_cursor.lnum = pos->lnum;
9855 curwin->w_cursor.col = i;
9856 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9857 curwin->w_cursor = *pos;
9858 i = get_indent();
9859 curwin->w_cursor = old_pos;
9860#ifdef FEAT_VREPLACE
9861 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009862 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 else
9864#endif
9865 (void)set_indent(i, SIN_CHANGED);
9866 }
9867 else if (curwin->w_cursor.col > 0)
9868 {
9869 /*
9870 * when inserting '{' after "O" reduce indent, but not
9871 * more than indent of previous line
9872 */
9873 temp = TRUE;
9874 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9875 {
9876 old_pos = curwin->w_cursor;
9877 i = get_indent();
9878 while (curwin->w_cursor.lnum > 1)
9879 {
9880 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9881
9882 /* ignore empty lines and lines starting with '#'. */
9883 if (*ptr != '#' && *ptr != NUL)
9884 break;
9885 }
9886 if (get_indent() >= i)
9887 temp = FALSE;
9888 curwin->w_cursor = old_pos;
9889 }
9890 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009891 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 }
9893 }
9894
9895 /*
9896 * set indent of '#' always to 0
9897 */
9898 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9899 {
9900 /* remember current indent for next line */
9901 old_indent = get_indent();
9902 (void)set_indent(0, SIN_CHANGED);
9903 }
9904
9905 /* Adjust ai_col, the char at this position can be deleted. */
9906 if (ai_col > curwin->w_cursor.col)
9907 ai_col = curwin->w_cursor.col;
9908}
9909#endif
9910
9911/*
9912 * Get the value that w_virtcol would have when 'list' is off.
9913 * Unless 'cpo' contains the 'L' flag.
9914 */
9915 static colnr_T
9916get_nolist_virtcol()
9917{
9918 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9919 return getvcol_nolist(&curwin->w_cursor);
9920 validate_virtcol();
9921 return curwin->w_virtcol;
9922}