blob: 4cb727cdc8d3b465b36f47c0b2c68aa344d04712 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000132static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000133static 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 +0000134static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000135static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000136static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000138static void ins_compl_upd_pum __ARGS((void));
139static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000140static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000141static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000142static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000143static 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 +0000144static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static void ins_compl_free __ARGS((void));
146static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000147static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000148static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000149static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000150static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000151static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000152static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000153static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000154static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000156#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
157static void ins_compl_add_list __ARGS((list_T *list));
158#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000159static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void ins_compl_delete __ARGS((void));
161static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000162static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000165static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000166static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000168static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif /* FEAT_INS_EXPAND */
170
171#define BACKSPACE_CHAR 1
172#define BACKSPACE_WORD 2
173#define BACKSPACE_WORD_NOT_SPACE 3
174#define BACKSPACE_LINE 4
175
Bram Moolenaar754b5602006-02-09 23:53:20 +0000176static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void ins_ctrl_v __ARGS((void));
178static void undisplay_dollar __ARGS((void));
179static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void check_auto_format __ARGS((int));
182static void redo_literal __ARGS((int c));
183static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000184#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000185static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000186static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000187static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
190static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static int replace_pop __ARGS((void));
195static void replace_join __ARGS((int off));
196static void replace_pop_ins __ARGS((void));
197#ifdef FEAT_MBYTE
198static void mb_replace_pop_ins __ARGS((int cc));
199#endif
200static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000201static void replace_do_bs __ARGS((int limit_col));
202static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifdef FEAT_CINDENT
204static int cindent_on __ARGS((void));
205#endif
206static void ins_reg __ARGS((void));
207static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000208static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000209static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#ifdef FEAT_RIGHTLEFT
211static void ins_ctrl_ __ARGS((void));
212#endif
213#ifdef FEAT_VISUAL
214static int ins_start_select __ARGS((int c));
215#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000216static void ins_insert __ARGS((int replaceState));
217static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static void ins_shift __ARGS((int c, int lastc));
219static void ins_del __ARGS((void));
220static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
221#ifdef FEAT_MOUSE
222static void ins_mouse __ARGS((int c));
223static void ins_mousescroll __ARGS((int up));
224#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000225#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
226static void ins_tabline __ARGS((int c));
227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ins_left __ARGS((void));
229static void ins_home __ARGS((int c));
230static void ins_end __ARGS((int c));
231static void ins_s_left __ARGS((void));
232static void ins_right __ARGS((void));
233static void ins_s_right __ARGS((void));
234static void ins_up __ARGS((int startcol));
235static void ins_pageup __ARGS((void));
236static void ins_down __ARGS((int startcol));
237static void ins_pagedown __ARGS((void));
238#ifdef FEAT_DND
239static void ins_drop __ARGS((void));
240#endif
241static int ins_tab __ARGS((void));
242static int ins_eol __ARGS((int c));
243#ifdef FEAT_DIGRAPHS
244static int ins_digraph __ARGS((void));
245#endif
246static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000247static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_SMARTINDENT
249static void ins_try_si __ARGS((int c));
250#endif
251static colnr_T get_nolist_virtcol __ARGS((void));
252
253static colnr_T Insstart_textlen; /* length of line when insert started */
254static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
255
256static char_u *last_insert = NULL; /* the text of the previous insert,
257 K_SPECIAL and CSI are escaped */
258static int last_insert_skip; /* nr of chars in front of previous insert */
259static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000260static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262#ifdef FEAT_CINDENT
263static int can_cindent; /* may do cindenting on this line */
264#endif
265
266static int old_indent = 0; /* for ^^D command in insert mode */
267
268#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000269static int revins_on; /* reverse insert mode on */
270static int revins_chars; /* how much to skip after edit */
271static int revins_legal; /* was the last char 'legal'? */
272static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275static int ins_need_undo; /* call u_save() before inserting a
276 char. Set when edit() is called.
277 after that arrow_used is used. */
278
279static int did_add_space = FALSE; /* auto_format() added an extra space
280 under the cursor */
281
282/*
283 * edit(): Start inserting text.
284 *
285 * "cmdchar" can be:
286 * 'i' normal insert command
287 * 'a' normal append command
288 * 'R' replace command
289 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
290 * but still only one <CR> is inserted. The <Esc> is not used for redo.
291 * 'g' "gI" command.
292 * 'V' "gR" command for Virtual Replace mode.
293 * 'v' "gr" command for single character Virtual Replace mode.
294 *
295 * This function is not called recursively. For CTRL-O commands, it returns
296 * and lets the caller handle the Normal-mode command.
297 *
298 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
299 */
300 int
301edit(cmdchar, startln, count)
302 int cmdchar;
303 int startln; /* if set, insert at start of line */
304 long count;
305{
306 int c = 0;
307 char_u *ptr;
308 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000309 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 int i;
312 int did_backspace = TRUE; /* previous char was backspace */
313#ifdef FEAT_CINDENT
314 int line_is_white = FALSE; /* line is empty before insert */
315#endif
316 linenr_T old_topline = 0; /* topline before insertion */
317#ifdef FEAT_DIFF
318 int old_topfill = -1;
319#endif
320 int inserted_space = FALSE; /* just inserted a space */
321 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000322 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000324 /* Remember whether editing was restarted after CTRL-O. */
325 did_restart_edit = restart_edit;
326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /* sleep before redrawing, needed for "CTRL-O :" that results in an
328 * error message */
329 check_for_delay(TRUE);
330
331#ifdef HAVE_SANDBOX
332 /* Don't allow inserting in the sandbox. */
333 if (sandbox != 0)
334 {
335 EMSG(_(e_sandbox));
336 return FALSE;
337 }
338#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 /* Don't allow changes in the buffer while editing the cmdline. The
340 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000341 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000342 {
343 EMSG(_(e_secure));
344 return FALSE;
345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000348 /* Don't allow recursive insert mode when busy with completion. */
349 if (compl_started || pum_visible())
350 {
351 EMSG(_(e_secure));
352 return FALSE;
353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 ins_compl_clear(); /* clear stuff for CTRL-X mode */
355#endif
356
Bram Moolenaar843ee412004-06-30 16:16:41 +0000357#ifdef FEAT_AUTOCMD
358 /*
359 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
360 */
361 if (cmdchar != 'r' && cmdchar != 'v')
362 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000363# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000364 if (cmdchar == 'R')
365 ptr = (char_u *)"r";
366 else if (cmdchar == 'V')
367 ptr = (char_u *)"v";
368 else
369 ptr = (char_u *)"i";
370 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000371# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000372 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
373 }
374#endif
375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#ifdef FEAT_MOUSE
377 /*
378 * When doing a paste with the middle mouse button, Insstart is set to
379 * where the paste started.
380 */
381 if (where_paste_started.lnum != 0)
382 Insstart = where_paste_started;
383 else
384#endif
385 {
386 Insstart = curwin->w_cursor;
387 if (startln)
388 Insstart.col = 0;
389 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000390 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 Insstart_blank_vcol = MAXCOL;
392 if (!did_ai)
393 ai_col = 0;
394
395 if (cmdchar != NUL && restart_edit == 0)
396 {
397 ResetRedobuff();
398 AppendNumberToRedobuff(count);
399#ifdef FEAT_VREPLACE
400 if (cmdchar == 'V' || cmdchar == 'v')
401 {
402 /* "gR" or "gr" command */
403 AppendCharToRedobuff('g');
404 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
405 }
406 else
407#endif
408 {
409 AppendCharToRedobuff(cmdchar);
410 if (cmdchar == 'g') /* "gI" command */
411 AppendCharToRedobuff('I');
412 else if (cmdchar == 'r') /* "r<CR>" command */
413 count = 1; /* insert only one <CR> */
414 }
415 }
416
417 if (cmdchar == 'R')
418 {
419#ifdef FEAT_FKMAP
420 if (p_fkmap && p_ri)
421 {
422 beep_flush();
423 EMSG(farsi_text_3); /* encoded in Farsi */
424 State = INSERT;
425 }
426 else
427#endif
428 State = REPLACE;
429 }
430#ifdef FEAT_VREPLACE
431 else if (cmdchar == 'V' || cmdchar == 'v')
432 {
433 State = VREPLACE;
434 replaceState = VREPLACE;
435 orig_line_count = curbuf->b_ml.ml_line_count;
436 vr_lines_changed = 1;
437 }
438#endif
439 else
440 State = INSERT;
441
442 stop_insert_mode = FALSE;
443
444 /*
445 * Need to recompute the cursor position, it might move when the cursor is
446 * on a TAB or special character.
447 */
448 curs_columns(TRUE);
449
450 /*
451 * Enable langmap or IME, indicated by 'iminsert'.
452 * Note that IME may enabled/disabled without us noticing here, thus the
453 * 'iminsert' value may not reflect what is actually used. It is updated
454 * when hitting <Esc>.
455 */
456 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
457 State |= LANGMAP;
458#ifdef USE_IM_CONTROL
459 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
460#endif
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#ifdef FEAT_MOUSE
463 setmouse();
464#endif
465#ifdef FEAT_CMDL_INFO
466 clear_showcmd();
467#endif
468#ifdef FEAT_RIGHTLEFT
469 /* there is no reverse replace mode */
470 revins_on = (State == INSERT && p_ri);
471 if (revins_on)
472 undisplay_dollar();
473 revins_chars = 0;
474 revins_legal = 0;
475 revins_scol = -1;
476#endif
477
478 /*
479 * Handle restarting Insert mode.
480 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
481 * restart_edit non-zero, and something in the stuff buffer.
482 */
483 if (restart_edit != 0 && stuff_empty())
484 {
485#ifdef FEAT_MOUSE
486 /*
487 * After a paste we consider text typed to be part of the insert for
488 * the pasted text. You can backspace over the pasted text too.
489 */
490 if (where_paste_started.lnum)
491 arrow_used = FALSE;
492 else
493#endif
494 arrow_used = TRUE;
495 restart_edit = 0;
496
497 /*
498 * If the cursor was after the end-of-line before the CTRL-O and it is
499 * now at the end-of-line, put it after the end-of-line (this is not
500 * correct in very rare cases).
501 * Also do this if curswant is greater than the current virtual
502 * column. Eg after "^O$" or "^O80|".
503 */
504 validate_virtcol();
505 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000506 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 || curwin->w_curswant > curwin->w_virtcol)
508 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
509 {
510 if (ptr[1] == NUL)
511 ++curwin->w_cursor.col;
512#ifdef FEAT_MBYTE
513 else if (has_mbyte)
514 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000515 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (ptr[i] == NUL)
517 curwin->w_cursor.col += i;
518 }
519#endif
520 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000521 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
523 else
524 arrow_used = FALSE;
525
526 /* we are in insert mode now, don't need to start it anymore */
527 need_start_insertmode = FALSE;
528
529 /* Need to save the line for undo before inserting the first char. */
530 ins_need_undo = TRUE;
531
532#ifdef FEAT_MOUSE
533 where_paste_started.lnum = 0;
534#endif
535#ifdef FEAT_CINDENT
536 can_cindent = TRUE;
537#endif
538#ifdef FEAT_FOLDING
539 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
540 * restarting. */
541 if (!p_im && did_restart_edit == 0)
542 foldOpenCursor();
543#endif
544
545 /*
546 * If 'showmode' is set, show the current (insert/replace/..) mode.
547 * A warning message for changing a readonly file is given here, before
548 * actually changing anything. It's put after the mode, if any.
549 */
550 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000551 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 i = showmode();
553
554 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000555 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557#ifdef CURSOR_SHAPE
558 ui_cursor_shape(); /* may show different cursor shape */
559#endif
560#ifdef FEAT_DIGRAPHS
561 do_digraph(-1); /* clear digraphs */
562#endif
563
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000564 /*
565 * Get the current length of the redo buffer, those characters have to be
566 * skipped if we want to get to the inserted characters.
567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 ptr = get_inserted();
569 if (ptr == NULL)
570 new_insert_skip = 0;
571 else
572 {
573 new_insert_skip = (int)STRLEN(ptr);
574 vim_free(ptr);
575 }
576
577 old_indent = 0;
578
579 /*
580 * Main loop in Insert mode: repeat until Insert mode is left.
581 */
582 for (;;)
583 {
584#ifdef FEAT_RIGHTLEFT
585 if (!revins_legal)
586 revins_scol = -1; /* reset on illegal motions */
587 else
588 revins_legal = 0;
589#endif
590 if (arrow_used) /* don't repeat insert when arrow key used */
591 count = 0;
592
593 if (stop_insert_mode)
594 {
595 /* ":stopinsert" used or 'insertmode' reset */
596 count = 0;
597 goto doESCkey;
598 }
599
600 /* set curwin->w_curswant for next K_DOWN or K_UP */
601 if (!arrow_used)
602 curwin->w_set_curswant = TRUE;
603
604 /* If there is no typeahead may check for timestamps (e.g., for when a
605 * menu invoked a shell command). */
606 if (stuff_empty())
607 {
608 did_check_timestamps = FALSE;
609 if (need_check_timestamps)
610 check_timestamps(FALSE);
611 }
612
613 /*
614 * When emsg() was called msg_scroll will have been set.
615 */
616 msg_scroll = FALSE;
617
618#ifdef FEAT_GUI
619 /* When 'mousefocus' is set a mouse movement may have taken us to
620 * another window. "need_mouse_correct" may then be set because of an
621 * autocommand. */
622 if (need_mouse_correct)
623 gui_mouse_correct();
624#endif
625
626#ifdef FEAT_FOLDING
627 /* Open fold at the cursor line, according to 'foldopen'. */
628 if (fdo_flags & FDO_INSERT)
629 foldOpenCursor();
630 /* Close folds where the cursor isn't, according to 'foldclose' */
631 if (!char_avail())
632 foldCheckClose();
633#endif
634
635 /*
636 * If we inserted a character at the last position of the last line in
637 * the window, scroll the window one line up. This avoids an extra
638 * redraw.
639 * This is detected when the cursor column is smaller after inserting
640 * something.
641 * Don't do this when the topline changed already, it has
642 * already been adjusted (by insertchar() calling open_line())).
643 */
644 if (curbuf->b_mod_set
645 && curwin->w_p_wrap
646 && !did_backspace
647 && curwin->w_topline == old_topline
648#ifdef FEAT_DIFF
649 && curwin->w_topfill == old_topfill
650#endif
651 )
652 {
653 mincol = curwin->w_wcol;
654 validate_cursor_col();
655
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000656 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 && curwin->w_wrow == W_WINROW(curwin)
658 + curwin->w_height - 1 - p_so
659 && (curwin->w_cursor.lnum != curwin->w_topline
660#ifdef FEAT_DIFF
661 || curwin->w_topfill > 0
662#endif
663 ))
664 {
665#ifdef FEAT_DIFF
666 if (curwin->w_topfill > 0)
667 --curwin->w_topfill;
668 else
669#endif
670#ifdef FEAT_FOLDING
671 if (hasFolding(curwin->w_topline, NULL, &old_topline))
672 set_topline(curwin, old_topline + 1);
673 else
674#endif
675 set_topline(curwin, curwin->w_topline + 1);
676 }
677 }
678
679 /* May need to adjust w_topline to show the cursor. */
680 update_topline();
681
682 did_backspace = FALSE;
683
684 validate_cursor(); /* may set must_redraw */
685
686 /*
687 * Redraw the display when no characters are waiting.
688 * Also shows mode, ruler and positions cursor.
689 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000690 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692#ifdef FEAT_SCROLLBIND
693 if (curwin->w_p_scb)
694 do_check_scrollbind(TRUE);
695#endif
696
697 update_curswant();
698 old_topline = curwin->w_topline;
699#ifdef FEAT_DIFF
700 old_topfill = curwin->w_topfill;
701#endif
702
703#ifdef USE_ON_FLY_SCROLL
704 dont_scroll = FALSE; /* allow scrolling here */
705#endif
706
707 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000708 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 */
710 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000711 do
712 {
713 c = safe_vgetc();
714 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000716#ifdef FEAT_AUTOCMD
717 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
718 did_cursorhold = TRUE;
719#endif
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_RIGHTLEFT
722 if (p_hkmap && KeyTyped)
723 c = hkmap(c); /* Hebrew mode mapping */
724#endif
725#ifdef FEAT_FKMAP
726 if (p_fkmap && KeyTyped)
727 c = fkmap(c); /* Farsi mode mapping */
728#endif
729
730#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000731 /*
732 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000733 * and the cursor is still in the completed word. Only when there is
734 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000736 if (compl_started
737 && pum_wanted()
738 && curwin->w_cursor.col >= compl_col
739 && (compl_shown_match == NULL
740 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000741 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000742 /* BS: Delete one character from "compl_leader". */
743 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000744 && curwin->w_cursor.col > compl_col
745 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000746 continue;
747
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000748 /* When no match was selected or it was edited. */
749 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000750 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000751 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000752 * "compl_leader". Except when at the original match and
753 * there is nothing to add, CTRL-L works like CTRL-P then. */
754 if (c == Ctrl_L
755 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000756 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000757 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000758 {
759 ins_compl_addfrommatch();
760 continue;
761 }
762
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000763 /* A non-white character that fits in with the current
764 * completion: Add to "compl_leader". */
765 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000766 {
767 ins_compl_addleader(c);
768 continue;
769 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000770
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000771 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000772 * compl_enter_selects is set the Enter key does the same. */
773 if (c == Ctrl_Y || (compl_enter_selects
774 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000775 {
776 ins_compl_delete();
777 ins_compl_insert();
778 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000779 }
780 }
781
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
783 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000784 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000785 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000786 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#endif
788
Bram Moolenaar488c6512005-08-11 20:09:58 +0000789 /* CTRL-\ CTRL-N goes to Normal mode,
790 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
791 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (c == Ctrl_BSL)
793 {
794 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000795 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ++no_mapping;
797 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000798 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 --no_mapping;
800 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000801 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000803 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 vungetc(c);
805 c = Ctrl_BSL;
806 }
807 else if (c == Ctrl_G && p_im)
808 continue;
809 else
810 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000811 if (c == Ctrl_O)
812 {
813 ins_ctrl_o();
814 ins_at_eol = FALSE; /* cursor keeps its column */
815 nomove = TRUE;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 count = 0;
818 goto doESCkey;
819 }
820 }
821
822#ifdef FEAT_DIGRAPHS
823 c = do_digraph(c);
824#endif
825
826#ifdef FEAT_INS_EXPAND
827 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
828 goto docomplete;
829#endif
830 if (c == Ctrl_V || c == Ctrl_Q)
831 {
832 ins_ctrl_v();
833 c = Ctrl_V; /* pretend CTRL-V is last typed character */
834 continue;
835 }
836
837#ifdef FEAT_CINDENT
838 if (cindent_on()
839# ifdef FEAT_INS_EXPAND
840 && ctrl_x_mode == 0
841# endif
842 )
843 {
844 /* A key name preceded by a bang means this key is not to be
845 * inserted. Skip ahead to the re-indenting below.
846 * A key name preceded by a star means that indenting has to be
847 * done before inserting the key. */
848 line_is_white = inindent(0);
849 if (in_cinkeys(c, '!', line_is_white))
850 goto force_cindent;
851 if (can_cindent && in_cinkeys(c, '*', line_is_white)
852 && stop_arrow() == OK)
853 do_c_expr_indent();
854 }
855#endif
856
857#ifdef FEAT_RIGHTLEFT
858 if (curwin->w_p_rl)
859 switch (c)
860 {
861 case K_LEFT: c = K_RIGHT; break;
862 case K_S_LEFT: c = K_S_RIGHT; break;
863 case K_C_LEFT: c = K_C_RIGHT; break;
864 case K_RIGHT: c = K_LEFT; break;
865 case K_S_RIGHT: c = K_S_LEFT; break;
866 case K_C_RIGHT: c = K_C_LEFT; break;
867 }
868#endif
869
870#ifdef FEAT_VISUAL
871 /*
872 * If 'keymodel' contains "startsel", may start selection. If it
873 * does, a CTRL-O and c will be stuffed, we need to get these
874 * characters.
875 */
876 if (ins_start_select(c))
877 continue;
878#endif
879
880 /*
881 * The big switch to handle a character in insert mode.
882 */
883 switch (c)
884 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (echeck_abbr(ESC + ABBR_OFF))
887 break;
888 /*FALLTHROUGH*/
889
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000890 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_CMDWIN
892 if (c == Ctrl_C && cmdwin_type != 0)
893 {
894 /* Close the cmdline window. */
895 cmdwin_result = K_IGNORE;
896 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000897 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 goto doESCkey;
899 }
900#endif
901
902#ifdef UNIX
903do_intr:
904#endif
905 /* when 'insertmode' set, and not halfway a mapping, don't leave
906 * Insert mode */
907 if (goto_im())
908 {
909 if (got_int)
910 {
911 (void)vgetc(); /* flush all buffers */
912 got_int = FALSE;
913 }
914 else
915 vim_beep();
916 break;
917 }
918doESCkey:
919 /*
920 * This is the ONLY return from edit()!
921 */
922 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
923 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000924 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 o_lnum = curwin->w_cursor.lnum;
926
Bram Moolenaar488c6512005-08-11 20:09:58 +0000927 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000928 {
929#ifdef FEAT_AUTOCMD
930 if (cmdchar != 'r' && cmdchar != 'v')
931 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
932 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000933 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 continue;
938
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000939 case Ctrl_Z: /* suspend when 'insertmode' set */
940 if (!p_im)
941 goto normalchar; /* insert CTRL-Z as normal char */
942 stuffReadbuff((char_u *)":st\r");
943 c = Ctrl_O;
944 /*FALLTHROUGH*/
945
946 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000947#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000948 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 goto docomplete;
950#endif
951 if (echeck_abbr(Ctrl_O + ABBR_OFF))
952 break;
953 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000954
955#ifdef FEAT_VIRTUALEDIT
956 /* don't move the cursor left when 'virtualedit' has "onemore". */
957 if (ve_flags & VE_ONEMORE)
958 {
959 ins_at_eol = FALSE;
960 nomove = TRUE;
961 }
962#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000963 count = 0;
964 goto doESCkey;
965
Bram Moolenaar572cb562005-08-05 21:35:02 +0000966 case K_INS: /* toggle insert/replace mode */
967 case K_KINS:
968 ins_insert(replaceState);
969 break;
970
971 case K_SELECT: /* end of Select mode mapping - ignore */
972 break;
973
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974#ifdef FEAT_SNIFF
975 case K_SNIFF: /* Sniff command received */
976 stuffcharReadbuff(K_SNIFF);
977 goto doESCkey;
978#endif
979
980 case K_HELP: /* Help key works like <ESC> <Help> */
981 case K_F1:
982 case K_XF1:
983 stuffcharReadbuff(K_HELP);
984 if (p_im)
985 need_start_insertmode = TRUE;
986 goto doESCkey;
987
988#ifdef FEAT_NETBEANS_INTG
989 case K_F21: /* NetBeans command */
990 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000991 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 --no_mapping;
993 netbeans_keycommand(i);
994 break;
995#endif
996
997 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 case NUL:
999 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 /* For ^@ the trailing ESC will end the insert, unless there is an
1001 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1003 && c != Ctrl_A && !p_im)
1004 goto doESCkey; /* quit insert mode */
1005 inserted_space = FALSE;
1006 break;
1007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 ins_reg();
1010 auto_format(FALSE, TRUE);
1011 inserted_space = FALSE;
1012 break;
1013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 ins_ctrl_g();
1016 break;
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl_HAT: /* switch input mode and/or langmap */
1019 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021
1022#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (!p_ari)
1025 goto normalchar;
1026 ins_ctrl_();
1027 break;
1028#endif
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1032 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1033 goto docomplete;
1034#endif
1035 /* FALLTHROUGH */
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038# ifdef FEAT_INS_EXPAND
1039 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1040 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 if (has_compl_option(FALSE))
1042 goto docomplete;
1043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045# endif
1046 ins_shift(c, lastc);
1047 auto_format(FALSE, TRUE);
1048 inserted_space = FALSE;
1049 break;
1050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 case K_KDEL:
1053 ins_del();
1054 auto_format(FALSE, TRUE);
1055 break;
1056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 case Ctrl_H:
1059 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1060 auto_format(FALSE, TRUE);
1061 break;
1062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1065 auto_format(FALSE, TRUE);
1066 break;
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001069# ifdef FEAT_COMPL_FUNC
1070 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001072 goto docomplete;
1073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1075 auto_format(FALSE, TRUE);
1076 inserted_space = FALSE;
1077 break;
1078
1079#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 case K_LEFTMOUSE_NM:
1082 case K_LEFTDRAG:
1083 case K_LEFTRELEASE:
1084 case K_LEFTRELEASE_NM:
1085 case K_MIDDLEMOUSE:
1086 case K_MIDDLEDRAG:
1087 case K_MIDDLERELEASE:
1088 case K_RIGHTMOUSE:
1089 case K_RIGHTDRAG:
1090 case K_RIGHTRELEASE:
1091 case K_X1MOUSE:
1092 case K_X1DRAG:
1093 case K_X1RELEASE:
1094 case K_X2MOUSE:
1095 case K_X2DRAG:
1096 case K_X2RELEASE:
1097 ins_mouse(c);
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ins_mousescroll(FALSE);
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ins_mousescroll(TRUE);
1106 break;
1107#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001108#ifdef FEAT_GUI_TABLINE
1109 case K_TABLINE:
1110 case K_TABMENU:
1111 ins_tabline(c);
1112 break;
1113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 break;
1117
Bram Moolenaar754b5602006-02-09 23:53:20 +00001118#ifdef FEAT_AUTOCMD
1119 case K_CURSORHOLD: /* Didn't type something for a while. */
1120 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1121 did_cursorhold = TRUE;
1122 break;
1123#endif
1124
Bram Moolenaar4770d092006-01-12 23:22:24 +00001125#ifdef FEAT_GUI_W32
1126 /* On Win32 ignore <M-F4>, we get it when closing the window was
1127 * cancelled. */
1128 case K_F4:
1129 if (mod_mask != MOD_MASK_ALT)
1130 goto normalchar;
1131 break;
1132#endif
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_GUI
1135 case K_VER_SCROLLBAR:
1136 ins_scroll();
1137 break;
1138
1139 case K_HOR_SCROLLBAR:
1140 ins_horscroll();
1141 break;
1142#endif
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_S_HOME:
1147 case K_C_HOME:
1148 ins_home(c);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 case K_S_END:
1154 case K_C_END:
1155 ins_end(c);
1156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001159 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1160 ins_s_left();
1161 else
1162 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 case K_C_LEFT:
1167 ins_s_left();
1168 break;
1169
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001170 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001171 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1172 ins_s_right();
1173 else
1174 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 case K_C_RIGHT:
1179 ins_s_right();
1180 break;
1181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001182 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001183#ifdef FEAT_INS_EXPAND
1184 if (pum_visible())
1185 goto docomplete;
1186#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001187 if (mod_mask & MOD_MASK_SHIFT)
1188 ins_pageup();
1189 else
1190 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 break;
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 case K_PAGEUP:
1195 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001196#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001197 if (pum_visible())
1198 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 ins_pageup();
1201 break;
1202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001204#ifdef FEAT_INS_EXPAND
1205 if (pum_visible())
1206 goto docomplete;
1207#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001208 if (mod_mask & MOD_MASK_SHIFT)
1209 ins_pagedown();
1210 else
1211 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 break;
1213
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001214 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 case K_PAGEDOWN:
1216 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001217#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001218 if (pum_visible())
1219 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 ins_pagedown();
1222 break;
1223
1224#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 ins_drop();
1227 break;
1228#endif
1229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001230 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 c = TAB;
1232 /* FALLTHROUGH */
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1236 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1237 goto docomplete;
1238#endif
1239 inserted_space = FALSE;
1240 if (ins_tab())
1241 goto normalchar; /* insert TAB as a normal char */
1242 auto_format(FALSE, TRUE);
1243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 c = CAR;
1247 /* FALLTHROUGH */
1248 case CAR:
1249 case NL:
1250#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1251 /* In a quickfix window a <CR> jumps to the error under the
1252 * cursor. */
1253 if (bt_quickfix(curbuf) && c == CAR)
1254 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001255 if (curwin->w_llist_ref == NULL) /* quickfix window */
1256 do_cmdline_cmd((char_u *)".cc");
1257 else /* location list window */
1258 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260 }
1261#endif
1262#ifdef FEAT_CMDWIN
1263 if (cmdwin_type != 0)
1264 {
1265 /* Execute the command in the cmdline window. */
1266 cmdwin_result = CAR;
1267 goto doESCkey;
1268 }
1269#endif
1270 if (ins_eol(c) && !p_im)
1271 goto doESCkey; /* out of memory */
1272 auto_format(FALSE, FALSE);
1273 inserted_space = FALSE;
1274 break;
1275
1276#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278# ifdef FEAT_INS_EXPAND
1279 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1280 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 if (has_compl_option(TRUE))
1282 goto docomplete;
1283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285# endif
1286# ifdef FEAT_DIGRAPHS
1287 c = ins_digraph();
1288 if (c == NUL)
1289 break;
1290# endif
1291 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
1294#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001295 case Ctrl_X: /* Enter CTRL-X mode */
1296 ins_ctrl_x();
1297 break;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (ctrl_x_mode != CTRL_X_TAGS)
1301 goto normalchar;
1302 goto docomplete;
1303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (ctrl_x_mode != CTRL_X_FILES)
1306 goto normalchar;
1307 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001308
1309 case 's': /* Spelling completion after ^X */
1310 case Ctrl_S:
1311 if (ctrl_x_mode != CTRL_X_SPELL)
1312 goto normalchar;
1313 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#endif
1315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001316 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#ifdef FEAT_INS_EXPAND
1318 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1319#endif
1320 {
1321 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1322 if (p_im)
1323 {
1324 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1325 break;
1326 goto doESCkey;
1327 }
1328 goto normalchar;
1329 }
1330#ifdef FEAT_INS_EXPAND
1331 /* FALLTHROUGH */
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case Ctrl_N:
1335 /* if 'complete' is empty then plain ^P is no longer special,
1336 * but it is under other ^X modes */
1337 if (*curbuf->b_p_cpt == NUL
1338 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 goto normalchar;
1341
1342docomplete:
1343 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 break;
1346#endif /* FEAT_INS_EXPAND */
1347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 case Ctrl_Y: /* copy from previous line or scroll down */
1349 case Ctrl_E: /* copy from next line or scroll up */
1350 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 break;
1352
1353 default:
1354#ifdef UNIX
1355 if (c == intr_char) /* special interrupt char */
1356 goto do_intr;
1357#endif
1358
1359 /*
1360 * Insert a nomal character.
1361 */
1362normalchar:
1363#ifdef FEAT_SMARTINDENT
1364 /* Try to perform smart-indenting. */
1365 ins_try_si(c);
1366#endif
1367
1368 if (c == ' ')
1369 {
1370 inserted_space = TRUE;
1371#ifdef FEAT_CINDENT
1372 if (inindent(0))
1373 can_cindent = FALSE;
1374#endif
1375 if (Insstart_blank_vcol == MAXCOL
1376 && curwin->w_cursor.lnum == Insstart.lnum)
1377 Insstart_blank_vcol = get_nolist_virtcol();
1378 }
1379
1380 if (vim_iswordc(c) || !echeck_abbr(
1381#ifdef FEAT_MBYTE
1382 /* Add ABBR_OFF for characters above 0x100, this is
1383 * what check_abbr() expects. */
1384 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1385#endif
1386 c))
1387 {
1388 insert_special(c, FALSE, FALSE);
1389#ifdef FEAT_RIGHTLEFT
1390 revins_legal++;
1391 revins_chars++;
1392#endif
1393 }
1394
1395 auto_format(FALSE, TRUE);
1396
1397#ifdef FEAT_FOLDING
1398 /* When inserting a character the cursor line must never be in a
1399 * closed fold. */
1400 foldOpenCursor();
1401#endif
1402 break;
1403 } /* end of switch (c) */
1404
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001405#ifdef FEAT_AUTOCMD
1406 /* If typed something may trigger CursorHoldI again. */
1407 if (c != K_CURSORHOLD)
1408 did_cursorhold = FALSE;
1409#endif
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /* If the cursor was moved we didn't just insert a space */
1412 if (arrow_used)
1413 inserted_space = FALSE;
1414
1415#ifdef FEAT_CINDENT
1416 if (can_cindent && cindent_on()
1417# ifdef FEAT_INS_EXPAND
1418 && ctrl_x_mode == 0
1419# endif
1420 )
1421 {
1422force_cindent:
1423 /*
1424 * Indent now if a key was typed that is in 'cinkeys'.
1425 */
1426 if (in_cinkeys(c, ' ', line_is_white))
1427 {
1428 if (stop_arrow() == OK)
1429 /* re-indent the current line */
1430 do_c_expr_indent();
1431 }
1432 }
1433#endif /* FEAT_CINDENT */
1434
1435 } /* for (;;) */
1436 /* NOTREACHED */
1437}
1438
1439/*
1440 * Redraw for Insert mode.
1441 * This is postponed until getting the next character to make '$' in the 'cpo'
1442 * option work correctly.
1443 * Only redraw when there are no characters available. This speeds up
1444 * inserting sequences of characters (e.g., for CTRL-R).
1445 */
1446 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001447ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001448 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 if (!char_avail())
1451 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001452#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001453 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1454 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001455 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001456 && !equalpos(last_cursormoved, curwin->w_cursor)
1457# ifdef FEAT_INS_EXPAND
1458 && !pum_visible()
1459# endif
1460 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001461 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001462# ifdef FEAT_SYN_HL
1463 /* Need to update the screen first, to make sure syntax
1464 * highlighting is correct after making a change (e.g., inserting
1465 * a "(". The autocommand may also require a redraw, so it's done
1466 * again below, unfortunately. */
1467 if (syntax_present(curbuf) && must_redraw)
1468 update_screen(0);
1469# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001470 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1471 last_cursormoved = curwin->w_cursor;
1472 }
1473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (must_redraw)
1475 update_screen(0);
1476 else if (clear_cmdline || redraw_cmdline)
1477 showmode(); /* clear cmdline and show mode */
1478 showruler(FALSE);
1479 setcursor();
1480 emsg_on_display = FALSE; /* may remove error message now */
1481 }
1482}
1483
1484/*
1485 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1486 */
1487 static void
1488ins_ctrl_v()
1489{
1490 int c;
1491
1492 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001493 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 if (redrawing() && !char_avail())
1496 edit_putchar('^', TRUE);
1497 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1498
1499#ifdef FEAT_CMDL_INFO
1500 add_to_showcmd_c(Ctrl_V);
1501#endif
1502
1503 c = get_literal();
1504#ifdef FEAT_CMDL_INFO
1505 clear_showcmd();
1506#endif
1507 insert_special(c, FALSE, TRUE);
1508#ifdef FEAT_RIGHTLEFT
1509 revins_chars++;
1510 revins_legal++;
1511#endif
1512}
1513
1514/*
1515 * Put a character directly onto the screen. It's not stored in a buffer.
1516 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1517 */
1518static int pc_status;
1519#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1520#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1521#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1522#define PC_STATUS_SET 3 /* pc_bytes was filled */
1523#ifdef FEAT_MBYTE
1524static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1525#else
1526static char_u pc_bytes[2]; /* saved bytes */
1527#endif
1528static int pc_attr;
1529static int pc_row;
1530static int pc_col;
1531
1532 void
1533edit_putchar(c, highlight)
1534 int c;
1535 int highlight;
1536{
1537 int attr;
1538
1539 if (ScreenLines != NULL)
1540 {
1541 update_topline(); /* just in case w_topline isn't valid */
1542 validate_cursor();
1543 if (highlight)
1544 attr = hl_attr(HLF_8);
1545 else
1546 attr = 0;
1547 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1548 pc_col = W_WINCOL(curwin);
1549#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1550 pc_status = PC_STATUS_UNSET;
1551#endif
1552#ifdef FEAT_RIGHTLEFT
1553 if (curwin->w_p_rl)
1554 {
1555 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1556# ifdef FEAT_MBYTE
1557 if (has_mbyte)
1558 {
1559 int fix_col = mb_fix_col(pc_col, pc_row);
1560
1561 if (fix_col != pc_col)
1562 {
1563 screen_putchar(' ', pc_row, fix_col, attr);
1564 --curwin->w_wcol;
1565 pc_status = PC_STATUS_RIGHT;
1566 }
1567 }
1568# endif
1569 }
1570 else
1571#endif
1572 {
1573 pc_col += curwin->w_wcol;
1574#ifdef FEAT_MBYTE
1575 if (mb_lefthalve(pc_row, pc_col))
1576 pc_status = PC_STATUS_LEFT;
1577#endif
1578 }
1579
1580 /* save the character to be able to put it back */
1581#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1582 if (pc_status == PC_STATUS_UNSET)
1583#endif
1584 {
1585 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1586 pc_status = PC_STATUS_SET;
1587 }
1588 screen_putchar(c, pc_row, pc_col, attr);
1589 }
1590}
1591
1592/*
1593 * Undo the previous edit_putchar().
1594 */
1595 void
1596edit_unputchar()
1597{
1598 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1599 {
1600#if defined(FEAT_MBYTE)
1601 if (pc_status == PC_STATUS_RIGHT)
1602 ++curwin->w_wcol;
1603 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1604 redrawWinline(curwin->w_cursor.lnum, FALSE);
1605 else
1606#endif
1607 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1608 }
1609}
1610
1611/*
1612 * Called when p_dollar is set: display a '$' at the end of the changed text
1613 * Only works when cursor is in the line that changes.
1614 */
1615 void
1616display_dollar(col)
1617 colnr_T col;
1618{
1619 colnr_T save_col;
1620
1621 if (!redrawing())
1622 return;
1623
1624 cursor_off();
1625 save_col = curwin->w_cursor.col;
1626 curwin->w_cursor.col = col;
1627#ifdef FEAT_MBYTE
1628 if (has_mbyte)
1629 {
1630 char_u *p;
1631
1632 /* If on the last byte of a multi-byte move to the first byte. */
1633 p = ml_get_curline();
1634 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1635 }
1636#endif
1637 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1638 if (curwin->w_wcol < W_WIDTH(curwin))
1639 {
1640 edit_putchar('$', FALSE);
1641 dollar_vcol = curwin->w_virtcol;
1642 }
1643 curwin->w_cursor.col = save_col;
1644}
1645
1646/*
1647 * Call this function before moving the cursor from the normal insert position
1648 * in insert mode.
1649 */
1650 static void
1651undisplay_dollar()
1652{
1653 if (dollar_vcol)
1654 {
1655 dollar_vcol = 0;
1656 redrawWinline(curwin->w_cursor.lnum, FALSE);
1657 }
1658}
1659
1660/*
1661 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1662 * Keep the cursor on the same character.
1663 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1664 * type == INDENT_DEC decrease indent (for CTRL-D)
1665 * type == INDENT_SET set indent to "amount"
1666 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1667 */
1668 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001669change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 int type;
1671 int amount;
1672 int round;
1673 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001674 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 int vcol;
1677 int last_vcol;
1678 int insstart_less; /* reduction for Insstart.col */
1679 int new_cursor_col;
1680 int i;
1681 char_u *ptr;
1682 int save_p_list;
1683 int start_col;
1684 colnr_T vc;
1685#ifdef FEAT_VREPLACE
1686 colnr_T orig_col = 0; /* init for GCC */
1687 char_u *new_line, *orig_line = NULL; /* init for GCC */
1688
1689 /* VREPLACE mode needs to know what the line was like before changing */
1690 if (State & VREPLACE_FLAG)
1691 {
1692 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1693 orig_col = curwin->w_cursor.col;
1694 }
1695#endif
1696
1697 /* for the following tricks we don't want list mode */
1698 save_p_list = curwin->w_p_list;
1699 curwin->w_p_list = FALSE;
1700 vc = getvcol_nolist(&curwin->w_cursor);
1701 vcol = vc;
1702
1703 /*
1704 * For Replace mode we need to fix the replace stack later, which is only
1705 * possible when the cursor is in the indent. Remember the number of
1706 * characters before the cursor if it's possible.
1707 */
1708 start_col = curwin->w_cursor.col;
1709
1710 /* determine offset from first non-blank */
1711 new_cursor_col = curwin->w_cursor.col;
1712 beginline(BL_WHITE);
1713 new_cursor_col -= curwin->w_cursor.col;
1714
1715 insstart_less = curwin->w_cursor.col;
1716
1717 /*
1718 * If the cursor is in the indent, compute how many screen columns the
1719 * cursor is to the left of the first non-blank.
1720 */
1721 if (new_cursor_col < 0)
1722 vcol = get_indent() - vcol;
1723
1724 if (new_cursor_col > 0) /* can't fix replace stack */
1725 start_col = -1;
1726
1727 /*
1728 * Set the new indent. The cursor will be put on the first non-blank.
1729 */
1730 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001731 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 else
1733 {
1734#ifdef FEAT_VREPLACE
1735 int save_State = State;
1736
1737 /* Avoid being called recursively. */
1738 if (State & VREPLACE_FLAG)
1739 State = INSERT;
1740#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001741 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#ifdef FEAT_VREPLACE
1743 State = save_State;
1744#endif
1745 }
1746 insstart_less -= curwin->w_cursor.col;
1747
1748 /*
1749 * Try to put cursor on same character.
1750 * If the cursor is at or after the first non-blank in the line,
1751 * compute the cursor column relative to the column of the first
1752 * non-blank character.
1753 * If we are not in insert mode, leave the cursor on the first non-blank.
1754 * If the cursor is before the first non-blank, position it relative
1755 * to the first non-blank, counted in screen columns.
1756 */
1757 if (new_cursor_col >= 0)
1758 {
1759 /*
1760 * When changing the indent while the cursor is touching it, reset
1761 * Insstart_col to 0.
1762 */
1763 if (new_cursor_col == 0)
1764 insstart_less = MAXCOL;
1765 new_cursor_col += curwin->w_cursor.col;
1766 }
1767 else if (!(State & INSERT))
1768 new_cursor_col = curwin->w_cursor.col;
1769 else
1770 {
1771 /*
1772 * Compute the screen column where the cursor should be.
1773 */
1774 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001775 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 /*
1778 * Advance the cursor until we reach the right screen column.
1779 */
1780 vcol = last_vcol = 0;
1781 new_cursor_col = -1;
1782 ptr = ml_get_curline();
1783 while (vcol <= (int)curwin->w_virtcol)
1784 {
1785 last_vcol = vcol;
1786#ifdef FEAT_MBYTE
1787 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001788 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 else
1790#endif
1791 ++new_cursor_col;
1792 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1793 }
1794 vcol = last_vcol;
1795
1796 /*
1797 * May need to insert spaces to be able to position the cursor on
1798 * the right screen column.
1799 */
1800 if (vcol != (int)curwin->w_virtcol)
1801 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001802 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001804 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (ptr != NULL)
1806 {
1807 new_cursor_col += i;
1808 ptr[i] = NUL;
1809 while (--i >= 0)
1810 ptr[i] = ' ';
1811 ins_str(ptr);
1812 vim_free(ptr);
1813 }
1814 }
1815
1816 /*
1817 * When changing the indent while the cursor is in it, reset
1818 * Insstart_col to 0.
1819 */
1820 insstart_less = MAXCOL;
1821 }
1822
1823 curwin->w_p_list = save_p_list;
1824
1825 if (new_cursor_col <= 0)
1826 curwin->w_cursor.col = 0;
1827 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001828 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 curwin->w_set_curswant = TRUE;
1830 changed_cline_bef_curs();
1831
1832 /*
1833 * May have to adjust the start of the insert.
1834 */
1835 if (State & INSERT)
1836 {
1837 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1838 {
1839 if ((int)Insstart.col <= insstart_less)
1840 Insstart.col = 0;
1841 else
1842 Insstart.col -= insstart_less;
1843 }
1844 if ((int)ai_col <= insstart_less)
1845 ai_col = 0;
1846 else
1847 ai_col -= insstart_less;
1848 }
1849
1850 /*
1851 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1852 * If the number of characters before the cursor decreased, need to pop a
1853 * few characters from the replace stack.
1854 * If the number of characters before the cursor increased, need to push a
1855 * few NULs onto the replace stack.
1856 */
1857 if (REPLACE_NORMAL(State) && start_col >= 0)
1858 {
1859 while (start_col > (int)curwin->w_cursor.col)
1860 {
1861 replace_join(0); /* remove a NUL from the replace stack */
1862 --start_col;
1863 }
1864 while (start_col < (int)curwin->w_cursor.col || replaced)
1865 {
1866 replace_push(NUL);
1867 if (replaced)
1868 {
1869 replace_push(replaced);
1870 replaced = NUL;
1871 }
1872 ++start_col;
1873 }
1874 }
1875
1876#ifdef FEAT_VREPLACE
1877 /*
1878 * For VREPLACE mode, we also have to fix the replace stack. In this case
1879 * it is always possible because we backspace over the whole line and then
1880 * put it back again the way we wanted it.
1881 */
1882 if (State & VREPLACE_FLAG)
1883 {
1884 /* If orig_line didn't allocate, just return. At least we did the job,
1885 * even if you can't backspace. */
1886 if (orig_line == NULL)
1887 return;
1888
1889 /* Save new line */
1890 new_line = vim_strsave(ml_get_curline());
1891 if (new_line == NULL)
1892 return;
1893
1894 /* We only put back the new line up to the cursor */
1895 new_line[curwin->w_cursor.col] = NUL;
1896
1897 /* Put back original line */
1898 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1899 curwin->w_cursor.col = orig_col;
1900
1901 /* Backspace from cursor to start of line */
1902 backspace_until_column(0);
1903
1904 /* Insert new stuff into line again */
1905 ins_bytes(new_line);
1906
1907 vim_free(new_line);
1908 }
1909#endif
1910}
1911
1912/*
1913 * Truncate the space at the end of a line. This is to be used only in an
1914 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1915 * modes.
1916 */
1917 void
1918truncate_spaces(line)
1919 char_u *line;
1920{
1921 int i;
1922
1923 /* find start of trailing white space */
1924 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1925 {
1926 if (State & REPLACE_FLAG)
1927 replace_join(0); /* remove a NUL from the replace stack */
1928 }
1929 line[i + 1] = NUL;
1930}
1931
1932#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1933 || defined(FEAT_COMMENTS) || defined(PROTO)
1934/*
1935 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1936 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001937 * Will attempt not to go before "col" even when there is a composing
1938 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940 void
1941backspace_until_column(col)
1942 int col;
1943{
1944 while ((int)curwin->w_cursor.col > col)
1945 {
1946 curwin->w_cursor.col--;
1947 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001948 replace_do_bs(col);
1949 else if (!del_char_after_col(col))
1950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
1952}
1953#endif
1954
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001955/*
1956 * Like del_char(), but make sure not to go before column "limit_col".
1957 * Only matters when there are composing characters.
1958 * Return TRUE when something was deleted.
1959 */
1960 static int
1961del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001962 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001963{
1964#ifdef FEAT_MBYTE
1965 if (enc_utf8 && limit_col >= 0)
1966 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001967 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001968
1969 /* Make sure the cursor is at the start of a character, but
1970 * skip forward again when going too far back because of a
1971 * composing character. */
1972 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001973 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001974 {
1975 int l = utf_ptr2len(ml_get_cursor());
1976
1977 if (l == 0) /* end of line */
1978 break;
1979 curwin->w_cursor.col += l;
1980 }
1981 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1982 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001983 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001984 }
1985 else
1986#endif
1987 (void)del_char(FALSE);
1988 return TRUE;
1989}
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1992/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001993 * CTRL-X pressed in Insert mode.
1994 */
1995 static void
1996ins_ctrl_x()
1997{
1998 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1999 * CTRL-V works like CTRL-N */
2000 if (ctrl_x_mode != CTRL_X_CMDLINE)
2001 {
2002 /* if the next ^X<> won't ADD nothing, then reset
2003 * compl_cont_status */
2004 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002005 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002006 else
2007 compl_cont_status = 0;
2008 /* We're not sure which CTRL-X mode it will be yet */
2009 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2010 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2011 edit_submode_pre = NULL;
2012 showmode();
2013 }
2014}
2015
2016/*
2017 * Return TRUE if the 'dict' or 'tsr' option can be used.
2018 */
2019 static int
2020has_compl_option(dict_opt)
2021 int dict_opt;
2022{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002023 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002024# ifdef FEAT_SPELL
2025 && !curwin->w_p_spell
2026# endif
2027 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002028 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2029 {
2030 ctrl_x_mode = 0;
2031 edit_submode = NULL;
2032 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2033 : (char_u *)_("'thesaurus' option is empty"),
2034 hl_attr(HLF_E));
2035 if (emsg_silent == 0)
2036 {
2037 vim_beep();
2038 setcursor();
2039 out_flush();
2040 ui_delay(2000L, FALSE);
2041 }
2042 return FALSE;
2043 }
2044 return TRUE;
2045}
2046
2047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2049 * This depends on the current mode.
2050 */
2051 int
2052vim_is_ctrl_x_key(c)
2053 int c;
2054{
2055 /* Always allow ^R - let it's results then be checked */
2056 if (c == Ctrl_R)
2057 return TRUE;
2058
Bram Moolenaare3226be2005-12-18 22:10:00 +00002059 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002060 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002061 return TRUE;
2062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 switch (ctrl_x_mode)
2064 {
2065 case 0: /* Not in any CTRL-X mode */
2066 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2067 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002068 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2070 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2071 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002072 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2073 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 case CTRL_X_SCROLL:
2075 return (c == Ctrl_Y || c == Ctrl_E);
2076 case CTRL_X_WHOLE_LINE:
2077 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2078 case CTRL_X_FILES:
2079 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2080 case CTRL_X_DICTIONARY:
2081 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2082 case CTRL_X_THESAURUS:
2083 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2084 case CTRL_X_TAGS:
2085 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2086#ifdef FEAT_FIND_ID
2087 case CTRL_X_PATH_PATTERNS:
2088 return (c == Ctrl_P || c == Ctrl_N);
2089 case CTRL_X_PATH_DEFINES:
2090 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2091#endif
2092 case CTRL_X_CMDLINE:
2093 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2094 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002095#ifdef FEAT_COMPL_FUNC
2096 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002097 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002098 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002099 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002100#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002101 case CTRL_X_SPELL:
2102 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
2104 EMSG(_(e_internal));
2105 return FALSE;
2106}
2107
2108/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002109 * Return TRUE when character "c" is part of the item currently being
2110 * completed. Used to decide whether to abandon complete mode when the menu
2111 * is visible.
2112 */
2113 static int
2114ins_compl_accept_char(c)
2115 int c;
2116{
2117 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2118 /* When expanding an identifier only accept identifier chars. */
2119 return vim_isIDc(c);
2120
2121 switch (ctrl_x_mode)
2122 {
2123 case CTRL_X_FILES:
2124 /* When expanding file name only accept file name chars. But not
2125 * path separators, so that "proto/<Tab>" expands files in
2126 * "proto", not "proto/" as a whole */
2127 return vim_isfilec(c) && !vim_ispathsep(c);
2128
2129 case CTRL_X_CMDLINE:
2130 case CTRL_X_OMNI:
2131 /* Command line and Omni completion can work with just about any
2132 * printable character, but do stop at white space. */
2133 return vim_isprintc(c) && !vim_iswhite(c);
2134
2135 case CTRL_X_WHOLE_LINE:
2136 /* For while line completion a space can be part of the line. */
2137 return vim_isprintc(c);
2138 }
2139 return vim_iswordc(c);
2140}
2141
2142/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002143 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002145 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 * the rest of the word to be in -- webb
2147 */
2148 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002149ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 char_u *str;
2151 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002152 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 char_u *fname;
2154 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002155 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002157 char_u *p;
2158 int i, c;
2159 int actual_len; /* Take multi-byte characters */
2160 int actual_compl_length; /* into account. */
2161 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 int has_lower = FALSE;
2163 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002165 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002167 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaara2993e12007-08-12 14:38:46 +00002169 /* Find actual length of completion. */
2170#ifdef FEAT_MBYTE
2171 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002173 p = str;
2174 actual_len = 0;
2175 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002177 mb_ptr_adv(p);
2178 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002181 else
2182#endif
2183 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
Bram Moolenaara2993e12007-08-12 14:38:46 +00002185 /* Find actual length of original text. */
2186#ifdef FEAT_MBYTE
2187 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002189 p = compl_orig_text;
2190 actual_compl_length = 0;
2191 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002193 mb_ptr_adv(p);
2194 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002197 else
2198#endif
2199 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Bram Moolenaara2993e12007-08-12 14:38:46 +00002201 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002202 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002203 if (wca != NULL)
2204 {
2205 p = str;
2206 for (i = 0; i < actual_len; ++i)
2207#ifdef FEAT_MBYTE
2208 if (has_mbyte)
2209 wca[i] = mb_ptr2char_adv(&p);
2210 else
2211#endif
2212 wca[i] = *(p++);
2213
2214 /* Rule 1: Were any chars converted to lower? */
2215 p = compl_orig_text;
2216 for (i = 0; i < actual_compl_length; ++i)
2217 {
2218#ifdef FEAT_MBYTE
2219 if (has_mbyte)
2220 c = mb_ptr2char_adv(&p);
2221 else
2222#endif
2223 c = *(p++);
2224 if (MB_ISLOWER(c))
2225 {
2226 has_lower = TRUE;
2227 if (MB_ISUPPER(wca[i]))
2228 {
2229 /* Rule 1 is satisfied. */
2230 for (i = actual_compl_length; i < actual_len; ++i)
2231 wca[i] = MB_TOLOWER(wca[i]);
2232 break;
2233 }
2234 }
2235 }
2236
2237 /*
2238 * Rule 2: No lower case, 2nd consecutive letter converted to
2239 * upper case.
2240 */
2241 if (!has_lower)
2242 {
2243 p = compl_orig_text;
2244 for (i = 0; i < actual_compl_length; ++i)
2245 {
2246#ifdef FEAT_MBYTE
2247 if (has_mbyte)
2248 c = mb_ptr2char_adv(&p);
2249 else
2250#endif
2251 c = *(p++);
2252 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2253 {
2254 /* Rule 2 is satisfied. */
2255 for (i = actual_compl_length; i < actual_len; ++i)
2256 wca[i] = MB_TOUPPER(wca[i]);
2257 break;
2258 }
2259 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2260 }
2261 }
2262
2263 /* Copy the original case of the part we typed. */
2264 p = compl_orig_text;
2265 for (i = 0; i < actual_compl_length; ++i)
2266 {
2267#ifdef FEAT_MBYTE
2268 if (has_mbyte)
2269 c = mb_ptr2char_adv(&p);
2270 else
2271#endif
2272 c = *(p++);
2273 if (MB_ISLOWER(c))
2274 wca[i] = MB_TOLOWER(wca[i]);
2275 else if (MB_ISUPPER(c))
2276 wca[i] = MB_TOUPPER(wca[i]);
2277 }
2278
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002279 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002280 * Generate encoding specific output from wide character array.
2281 * Multi-byte characters can occupy up to five bytes more than
2282 * ASCII characters, and we also need one byte for NUL, so stay
2283 * six bytes away from the edge of IObuff.
2284 */
2285 p = IObuff;
2286 i = 0;
2287 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2288#ifdef FEAT_MBYTE
2289 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002290 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002291 else
2292#endif
2293 *(p++) = wca[i++];
2294 *p = NUL;
2295
2296 vim_free(wca);
2297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002299 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2300 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002302 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303}
2304
2305/*
2306 * Add a match to the list of matches.
2307 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002308 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002309 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002311 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002312ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 char_u *str;
2314 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002315 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002317 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002318 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002319 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002320 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002322 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002323 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 ui_breakcheck();
2326 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (len < 0)
2329 len = (int)STRLEN(str);
2330
2331 /*
2332 * If the same match is already present, don't add it.
2333 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002334 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002336 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 do
2338 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002339 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002340 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002341 && match->cp_str[len] == NUL)
2342 return NOTDONE;
2343 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002344 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002347 /* Remove any popup menu before changing the list of matches. */
2348 ins_compl_del_pum();
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /*
2351 * Allocate a new match structure.
2352 * Copy the values to the new match structure.
2353 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002354 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002356 return FAIL;
2357 match->cp_number = -1;
2358 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002359 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002360 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002365 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002368 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2370 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002371 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002372 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002373 && compl_curr_match->cp_fname != NULL
2374 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002375 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002376 else if (fname != NULL)
2377 {
2378 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002379 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002382 match->cp_fname = NULL;
2383 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002384
2385 if (cptext != NULL)
2386 {
2387 int i;
2388
2389 for (i = 0; i < CPT_COUNT; ++i)
2390 if (cptext[i] != NULL && *cptext[i] != NUL)
2391 match->cp_text[i] = vim_strsave(cptext[i]);
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 /*
2395 * Link the new match structure in the list of matches.
2396 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002397 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002398 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 else if (dir == FORWARD)
2400 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002401 match->cp_next = compl_curr_match->cp_next;
2402 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 else /* BACKWARD */
2405 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002406 match->cp_next = compl_curr_match;
2407 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002409 if (match->cp_next)
2410 match->cp_next->cp_prev = match;
2411 if (match->cp_prev)
2412 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002414 compl_first_match = match;
2415 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002417 /*
2418 * Find the longest common string if still doing that.
2419 */
2420 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2421 ins_compl_longest_match(match);
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 return OK;
2424}
2425
2426/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002427 * Return TRUE if "str[len]" matches with match->cp_str, considering
2428 * match->cp_icase.
2429 */
2430 static int
2431ins_compl_equal(match, str, len)
2432 compl_T *match;
2433 char_u *str;
2434 int len;
2435{
2436 if (match->cp_icase)
2437 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2438 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2439}
2440
2441/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002442 * Reduce the longest common string for match "match".
2443 */
2444 static void
2445ins_compl_longest_match(match)
2446 compl_T *match;
2447{
2448 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002449 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002450 int had_match;
2451
2452 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002453 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002454 /* First match, use it as a whole. */
2455 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002456 if (compl_leader != NULL)
2457 {
2458 had_match = (curwin->w_cursor.col > compl_col);
2459 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002460 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002461 ins_redraw(FALSE);
2462
2463 /* When the match isn't there (to avoid matching itself) remove it
2464 * again after redrawing. */
2465 if (!had_match)
2466 ins_compl_delete();
2467 compl_used_match = FALSE;
2468 }
2469 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002470 else
2471 {
2472 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002473 p = compl_leader;
2474 s = match->cp_str;
2475 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002476 {
2477#ifdef FEAT_MBYTE
2478 if (has_mbyte)
2479 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002480 c1 = mb_ptr2char(p);
2481 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002482 }
2483 else
2484#endif
2485 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002486 c1 = *p;
2487 c2 = *s;
2488 }
2489 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2490 : (c1 != c2))
2491 break;
2492#ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 {
2495 mb_ptr_adv(p);
2496 mb_ptr_adv(s);
2497 }
2498 else
2499#endif
2500 {
2501 ++p;
2502 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002503 }
2504 }
2505
2506 if (*p != NUL)
2507 {
2508 /* Leader was shortened, need to change the inserted text. */
2509 *p = NUL;
2510 had_match = (curwin->w_cursor.col > compl_col);
2511 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002512 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002513 ins_redraw(FALSE);
2514
2515 /* When the match isn't there (to avoid matching itself) remove it
2516 * again after redrawing. */
2517 if (!had_match)
2518 ins_compl_delete();
2519 }
2520
2521 compl_used_match = FALSE;
2522 }
2523}
2524
2525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * Add an array of matches to the list of matches.
2527 * Frees matches[].
2528 */
2529 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 int num_matches;
2532 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 int i;
2536 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002537 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaar572cb562005-08-05 21:35:02 +00002539 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002541 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002543 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 FreeWild(num_matches, matches);
2545}
2546
2547/* Make the completion list cyclic.
2548 * Return the number of matches (excluding the original).
2549 */
2550 static int
2551ins_compl_make_cyclic()
2552{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002553 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 int count = 0;
2555
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002556 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 /*
2559 * Find the end of the list.
2560 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002561 match = compl_first_match;
2562 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002563 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002565 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 ++count;
2567 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 match->cp_next = compl_first_match;
2569 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 return count;
2572}
2573
Bram Moolenaara94bc432006-03-10 21:42:59 +00002574/*
2575 * Start completion for the complete() function.
2576 * "startcol" is where the matched text starts (1 is first column).
2577 * "list" is the list of matches.
2578 */
2579 void
2580set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002581 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002582 list_T *list;
2583{
2584 /* If already doing completions stop it. */
2585 if (ctrl_x_mode != 0)
2586 ins_compl_prep(' ');
2587 ins_compl_clear();
2588
2589 if (stop_arrow() == FAIL)
2590 return;
2591
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002592 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002593 startcol = curwin->w_cursor.col;
2594 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002595 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002596 /* compl_pattern doesn't need to be set */
2597 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2598 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002599 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002600 return;
2601
2602 /* Handle like dictionary completion. */
2603 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2604
2605 ins_compl_add_list(list);
2606 compl_matches = ins_compl_make_cyclic();
2607 compl_started = TRUE;
2608 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002609 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002610
2611 compl_curr_match = compl_first_match;
2612 ins_complete(Ctrl_N);
2613 out_flush();
2614}
2615
2616
Bram Moolenaar9372a112005-12-06 19:59:18 +00002617/* "compl_match_array" points the currently displayed list of entries in the
2618 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002619static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002620static int compl_match_arraysize;
2621
2622/*
2623 * Update the screen and when there is any scrolling remove the popup menu.
2624 */
2625 static void
2626ins_compl_upd_pum()
2627{
2628 int h;
2629
2630 if (compl_match_array != NULL)
2631 {
2632 h = curwin->w_cline_height;
2633 update_screen(0);
2634 if (h != curwin->w_cline_height)
2635 ins_compl_del_pum();
2636 }
2637}
2638
2639/*
2640 * Remove any popup menu.
2641 */
2642 static void
2643ins_compl_del_pum()
2644{
2645 if (compl_match_array != NULL)
2646 {
2647 pum_undisplay();
2648 vim_free(compl_match_array);
2649 compl_match_array = NULL;
2650 }
2651}
2652
2653/*
2654 * Return TRUE if the popup menu should be displayed.
2655 */
2656 static int
2657pum_wanted()
2658{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002659 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002660 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002661 return FALSE;
2662
2663 /* The display looks bad on a B&W display. */
2664 if (t_colors < 8
2665#ifdef FEAT_GUI
2666 && !gui.in_use
2667#endif
2668 )
2669 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002670 return TRUE;
2671}
2672
2673/*
2674 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002675 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002676 */
2677 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002678pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002679{
2680 compl_T *compl;
2681 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002682
2683 /* Don't display the popup menu if there are no matches or there is only
2684 * one (ignoring the original text). */
2685 compl = compl_first_match;
2686 i = 0;
2687 do
2688 {
2689 if (compl == NULL
2690 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2691 break;
2692 compl = compl->cp_next;
2693 } while (compl != compl_first_match);
2694
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002695 if (strstr((char *)p_cot, "menuone") != NULL)
2696 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002697 return (i >= 2);
2698}
2699
2700/*
2701 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002702 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002703 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002704 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002705ins_compl_show_pum()
2706{
2707 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002708 compl_T *shown_compl = NULL;
2709 int did_find_shown_match = FALSE;
2710 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002711 int i;
2712 int cur = -1;
2713 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002714 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002716 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002717 return;
2718
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002719#if defined(FEAT_EVAL)
2720 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2721 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2722#endif
2723
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002724 /* Update the screen before drawing the popup menu over it. */
2725 update_screen(0);
2726
2727 if (compl_match_array == NULL)
2728 {
2729 /* Need to build the popup menu list. */
2730 compl_match_arraysize = 0;
2731 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002732 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002733 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002734 do
2735 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002736 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2737 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002738 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002739 ++compl_match_arraysize;
2740 compl = compl->cp_next;
2741 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002742 if (compl_match_arraysize == 0)
2743 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002744 compl_match_array = (pumitem_T *)alloc_clear(
2745 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002746 * compl_match_arraysize));
2747 if (compl_match_array != NULL)
2748 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002749 /* If the current match is the original text don't find the first
2750 * match after it, don't highlight anything. */
2751 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2752 shown_match_ok = TRUE;
2753
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002754 i = 0;
2755 compl = compl_first_match;
2756 do
2757 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002758 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2759 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002760 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002761 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002762 if (!shown_match_ok)
2763 {
2764 if (compl == compl_shown_match || did_find_shown_match)
2765 {
2766 /* This item is the shown match or this is the
2767 * first displayed item after the shown match. */
2768 compl_shown_match = compl;
2769 did_find_shown_match = TRUE;
2770 shown_match_ok = TRUE;
2771 }
2772 else
2773 /* Remember this displayed match for when the
2774 * shown match is just below it. */
2775 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002776 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002777 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002778
2779 if (compl->cp_text[CPT_ABBR] != NULL)
2780 compl_match_array[i].pum_text =
2781 compl->cp_text[CPT_ABBR];
2782 else
2783 compl_match_array[i].pum_text = compl->cp_str;
2784 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2785 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2786 if (compl->cp_text[CPT_MENU] != NULL)
2787 compl_match_array[i++].pum_extra =
2788 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002789 else
2790 compl_match_array[i++].pum_extra = compl->cp_fname;
2791 }
2792
2793 if (compl == compl_shown_match)
2794 {
2795 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002796
2797 /* When the original text is the shown match don't set
2798 * compl_shown_match. */
2799 if (compl->cp_flags & ORIGINAL_TEXT)
2800 shown_match_ok = TRUE;
2801
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002802 if (!shown_match_ok && shown_compl != NULL)
2803 {
2804 /* The shown match isn't displayed, set it to the
2805 * previously displayed match. */
2806 compl_shown_match = shown_compl;
2807 shown_match_ok = TRUE;
2808 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002809 }
2810 compl = compl->cp_next;
2811 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002812
2813 if (!shown_match_ok) /* no displayed match at all */
2814 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002815 }
2816 }
2817 else
2818 {
2819 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002820 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002821 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2822 || compl_match_array[i].pum_text
2823 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002824 {
2825 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002826 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002827 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002828 }
2829
2830 if (compl_match_array != NULL)
2831 {
2832 /* Compute the screen column of the start of the completed text.
2833 * Use the cursor to get all wrapping and other settings right. */
2834 col = curwin->w_cursor.col;
2835 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002836 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002837 curwin->w_cursor.col = col;
2838 }
2839}
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#define DICT_FIRST (1) /* use just first element in "dict" */
2842#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002843
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002845 * Add any identifiers that match the given pattern in the list of dictionary
2846 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 */
2848 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002849ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2850 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002852 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002853 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002855 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 char_u *ptr;
2857 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 char_u **files;
2860 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002862 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaar0b238792006-03-02 22:49:12 +00002864 if (*dict == NUL)
2865 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002866#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002867 /* When 'dictionary' is empty and spell checking is enabled use
2868 * "spell". */
2869 if (!thesaurus && curwin->w_p_spell)
2870 dict = (char_u *)"spell";
2871 else
2872#endif
2873 return;
2874 }
2875
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002877 if (buf == NULL)
2878 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002879 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 /* If 'infercase' is set, don't use 'smartcase' here */
2882 save_p_scs = p_scs;
2883 if (curbuf->b_p_inf)
2884 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002885
2886 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2887 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002888 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002889 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2890 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002891 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002892 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002893
2894 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002895 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002896 len = STRLEN(pat_esc) + 10;
2897 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002898 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002899 {
2900 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002901 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002902 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002903 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002904 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2905 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002906 vim_free(ptr);
2907 }
2908 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002909 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002910 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002911 if (regmatch.regprog == NULL)
2912 goto theend;
2913 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2916 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002917 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 /* copy one dictionary file name into buf */
2920 if (flags == DICT_EXACT)
2921 {
2922 count = 1;
2923 files = &dict;
2924 }
2925 else
2926 {
2927 /* Expand wildcards in the dictionary name, but do not allow
2928 * backticks (for security, the 'dict' option may have been set in
2929 * a modeline). */
2930 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002931# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002932 if (!thesaurus && STRCMP(buf, "spell") == 0)
2933 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002934 else
2935# endif
2936 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 || expand_wildcards(1, &buf, &count, &files,
2938 EW_FILE|EW_SILENT) != OK)
2939 count = 0;
2940 }
2941
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002942# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002943 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002945 /* Complete from active spelling. Skip "\<" in the pattern, we
2946 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002947 if (pat[0] == '\\' && pat[1] == '<')
2948 ptr = pat + 2;
2949 else
2950 ptr = pat;
2951 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002953 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002954# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002955 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002956 {
2957 ins_compl_files(count, files, thesaurus, flags,
2958 &regmatch, buf, &dir);
2959 if (flags != DICT_EXACT)
2960 FreeWild(count, files);
2961 }
2962 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 break;
2964 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002965
2966theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 p_scs = save_p_scs;
2968 vim_free(regmatch.regprog);
2969 vim_free(buf);
2970}
2971
Bram Moolenaar0b238792006-03-02 22:49:12 +00002972 static void
2973ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2974 int count;
2975 char_u **files;
2976 int thesaurus;
2977 int flags;
2978 regmatch_T *regmatch;
2979 char_u *buf;
2980 int *dir;
2981{
2982 char_u *ptr;
2983 int i;
2984 FILE *fp;
2985 int add_r;
2986
2987 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2988 {
2989 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2990 if (flags != DICT_EXACT)
2991 {
2992 vim_snprintf((char *)IObuff, IOSIZE,
2993 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002994 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00002995 }
2996
2997 if (fp != NULL)
2998 {
2999 /*
3000 * Read dictionary file line by line.
3001 * Check each line for a match.
3002 */
3003 while (!got_int && !compl_interrupted
3004 && !vim_fgets(buf, LSIZE, fp))
3005 {
3006 ptr = buf;
3007 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3008 {
3009 ptr = regmatch->startp[0];
3010 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3011 ptr = find_line_end(ptr);
3012 else
3013 ptr = find_word_end(ptr);
3014 add_r = ins_compl_add_infercase(regmatch->startp[0],
3015 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003016 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003017 if (thesaurus)
3018 {
3019 char_u *wstart;
3020
3021 /*
3022 * Add the other matches on the line
3023 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003024 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003025 while (!got_int)
3026 {
3027 /* Find start of the next word. Skip white
3028 * space and punctuation. */
3029 ptr = find_word_start(ptr);
3030 if (*ptr == NUL || *ptr == NL)
3031 break;
3032 wstart = ptr;
3033
Bram Moolenaara2993e12007-08-12 14:38:46 +00003034 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003035#ifdef FEAT_MBYTE
3036 if (has_mbyte)
3037 /* Japanese words may have characters in
3038 * different classes, only separate words
3039 * with single-byte non-word characters. */
3040 while (*ptr != NUL)
3041 {
3042 int l = (*mb_ptr2len)(ptr);
3043
3044 if (l < 2 && !vim_iswordc(*ptr))
3045 break;
3046 ptr += l;
3047 }
3048 else
3049#endif
3050 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003051
3052 /* Add the word. Skip the regexp match. */
3053 if (wstart != regmatch->startp[0])
3054 add_r = ins_compl_add_infercase(wstart,
3055 (int)(ptr - wstart),
3056 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003057 }
3058 }
3059 if (add_r == OK)
3060 /* if dir was BACKWARD then honor it just once */
3061 *dir = FORWARD;
3062 else if (add_r == FAIL)
3063 break;
3064 /* avoid expensive call to vim_regexec() when at end
3065 * of line */
3066 if (*ptr == '\n' || got_int)
3067 break;
3068 }
3069 line_breakcheck();
3070 ins_compl_check_keys(50);
3071 }
3072 fclose(fp);
3073 }
3074 }
3075}
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077/*
3078 * Find the start of the next word.
3079 * Returns a pointer to the first char of the word. Also stops at a NUL.
3080 */
3081 char_u *
3082find_word_start(ptr)
3083 char_u *ptr;
3084{
3085#ifdef FEAT_MBYTE
3086 if (has_mbyte)
3087 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003088 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else
3090#endif
3091 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3092 ++ptr;
3093 return ptr;
3094}
3095
3096/*
3097 * Find the end of the word. Assumes it starts inside a word.
3098 * Returns a pointer to just after the word.
3099 */
3100 char_u *
3101find_word_end(ptr)
3102 char_u *ptr;
3103{
3104#ifdef FEAT_MBYTE
3105 int start_class;
3106
3107 if (has_mbyte)
3108 {
3109 start_class = mb_get_class(ptr);
3110 if (start_class > 1)
3111 while (*ptr != NUL)
3112 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003113 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (mb_get_class(ptr) != start_class)
3115 break;
3116 }
3117 }
3118 else
3119#endif
3120 while (vim_iswordc(*ptr))
3121 ++ptr;
3122 return ptr;
3123}
3124
3125/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126 * Find the end of the line, omitting CR and NL at the end.
3127 * Returns a pointer to just after the line.
3128 */
3129 static char_u *
3130find_line_end(ptr)
3131 char_u *ptr;
3132{
3133 char_u *s;
3134
3135 s = ptr + STRLEN(ptr);
3136 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3137 --s;
3138 return s;
3139}
3140
3141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Free the list of completions
3143 */
3144 static void
3145ins_compl_free()
3146{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003147 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003148 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003150 vim_free(compl_pattern);
3151 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003152 vim_free(compl_leader);
3153 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003155 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003157
3158 ins_compl_del_pum();
3159 pum_clear();
3160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003161 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 do
3163 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003164 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003165 compl_curr_match = compl_curr_match->cp_next;
3166 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003168 if (match->cp_flags & FREE_FNAME)
3169 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003170 for (i = 0; i < CPT_COUNT; ++i)
3171 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003173 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3174 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176
3177 static void
3178ins_compl_clear()
3179{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003180 compl_cont_status = 0;
3181 compl_started = FALSE;
3182 compl_matches = 0;
3183 vim_free(compl_pattern);
3184 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003185 vim_free(compl_leader);
3186 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003188 vim_free(compl_orig_text);
3189 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003190 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003194 * Return TRUE when Insert completion is active.
3195 */
3196 int
3197ins_compl_active()
3198{
3199 return compl_started;
3200}
3201
3202/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003203 * Delete one character before the cursor and show the subset of the matches
3204 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003205 * Returns the character to be used, NUL if the work is done and another char
3206 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003207 */
3208 static int
3209ins_compl_bs()
3210{
3211 char_u *line;
3212 char_u *p;
3213
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003214 line = ml_get_curline();
3215 p = line + curwin->w_cursor.col;
3216 mb_ptr_back(line, p);
3217
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003218 /* Stop completion when the whole word was deleted. For Omni completion
3219 * allow the word to be deleted, we won't match everything. */
3220 if ((int)(p - line) - (int)compl_col < 0
3221 || ((int)(p - line) - (int)compl_col == 0
3222 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003223 return K_BS;
3224
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003225 /* Deleted more than what was used to find matches or didn't finish
3226 * finding all matches: need to look for matches all over again. */
3227 if (curwin->w_cursor.col <= compl_col + compl_length
3228 || compl_was_interrupted)
3229 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003230
Bram Moolenaara6557602006-02-04 22:43:20 +00003231 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003232 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003233 if (compl_leader != NULL)
3234 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003235 ins_compl_new_leader();
3236 return NUL;
3237 }
3238 return K_BS;
3239}
Bram Moolenaara6557602006-02-04 22:43:20 +00003240
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003241/*
3242 * Called after changing "compl_leader".
3243 * Show the popup menu with a different set of matches.
3244 * May also search for matches again if the previous search was interrupted.
3245 */
3246 static void
3247ins_compl_new_leader()
3248{
3249 ins_compl_del_pum();
3250 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003251 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003252 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003253
3254 if (compl_started)
3255 ins_compl_set_original_text(compl_leader);
3256 else
3257 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003258#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003259 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003260#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003261 /*
3262 * Matches were cleared, need to search for them now. First display
3263 * the changed text before the cursor. Set "compl_restarting" to
3264 * avoid that the first match is inserted.
3265 */
3266 update_screen(0);
3267#ifdef FEAT_GUI
3268 if (gui.in_use)
3269 {
3270 /* Show the cursor after the match, not after the redrawn text. */
3271 setcursor();
3272 out_flush();
3273 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003274 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003275#endif
3276 compl_restarting = TRUE;
3277 if (ins_complete(Ctrl_N) == FAIL)
3278 compl_cont_status = 0;
3279 compl_restarting = FALSE;
3280 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003281
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003282#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003283 if (!compl_used_match)
3284 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003285 /* Go to the original text, since none of the matches is inserted. */
3286 if (compl_first_match->cp_prev != NULL
3287 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3288 compl_shown_match = compl_first_match->cp_prev;
3289 else
3290 compl_shown_match = compl_first_match;
3291 compl_curr_match = compl_shown_match;
3292 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003293 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003294#endif
3295 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003296
3297 /* Show the popup menu with a different set of matches. */
3298 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003299
3300 /* Don't let Enter select the original text when there is no popup menu. */
3301 if (compl_match_array == NULL)
3302 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003303}
3304
3305/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003306 * Return the length of the completion, from the completion start column to
3307 * the cursor column. Making sure it never goes below zero.
3308 */
3309 static int
3310ins_compl_len()
3311{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003312 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003313
3314 if (off < 0)
3315 return 0;
3316 return off;
3317}
3318
3319/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003320 * Append one character to the match leader. May reduce the number of
3321 * matches.
3322 */
3323 static void
3324ins_compl_addleader(c)
3325 int c;
3326{
3327#ifdef FEAT_MBYTE
3328 int cc;
3329
3330 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3331 {
3332 char_u buf[MB_MAXBYTES + 1];
3333
3334 (*mb_char2bytes)(c, buf);
3335 buf[cc] = NUL;
3336 ins_char_bytes(buf, cc);
3337 }
3338 else
3339#endif
3340 ins_char(c);
3341
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003342 /* If we didn't complete finding matches we must search again. */
3343 if (compl_was_interrupted)
3344 ins_compl_restart();
3345
Bram Moolenaara6557602006-02-04 22:43:20 +00003346 vim_free(compl_leader);
3347 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003348 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003349 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003350 ins_compl_new_leader();
3351}
3352
3353/*
3354 * Setup for finding completions again without leaving CTRL-X mode. Used when
3355 * BS or a key was typed while still searching for matches.
3356 */
3357 static void
3358ins_compl_restart()
3359{
3360 ins_compl_free();
3361 compl_started = FALSE;
3362 compl_matches = 0;
3363 compl_cont_status = 0;
3364 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003365}
3366
3367/*
3368 * Set the first match, the original text.
3369 */
3370 static void
3371ins_compl_set_original_text(str)
3372 char_u *str;
3373{
3374 char_u *p;
3375
3376 /* Replace the original text entry. */
3377 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3378 {
3379 p = vim_strsave(str);
3380 if (p != NULL)
3381 {
3382 vim_free(compl_first_match->cp_str);
3383 compl_first_match->cp_str = p;
3384 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003385 }
3386}
3387
3388/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003389 * Append one character to the match leader. May reduce the number of
3390 * matches.
3391 */
3392 static void
3393ins_compl_addfrommatch()
3394{
3395 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003396 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003397 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003398 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003399
3400 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003401 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003402 {
3403 /* When still at the original match use the first entry that matches
3404 * the leader. */
3405 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3406 {
3407 p = NULL;
3408 for (cp = compl_shown_match->cp_next; cp != NULL
3409 && cp != compl_first_match; cp = cp->cp_next)
3410 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003411 if (compl_leader == NULL
3412 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003413 (int)STRLEN(compl_leader)))
3414 {
3415 p = cp->cp_str;
3416 break;
3417 }
3418 }
3419 if (p == NULL || (int)STRLEN(p) <= len)
3420 return;
3421 }
3422 else
3423 return;
3424 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003425 p += len;
3426#ifdef FEAT_MBYTE
3427 c = mb_ptr2char(p);
3428#else
3429 c = *p;
3430#endif
3431 ins_compl_addleader(c);
3432}
3433
3434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003436 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003437 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003439 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440ins_compl_prep(c)
3441 int c;
3442{
3443 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003445 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /* Forget any previous 'special' messages if this is actually
3448 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3449 */
3450 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3451 edit_submode_extra = NULL;
3452
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003453 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3454 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003455 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003457 /* Set "compl_get_longest" when finding the first matches. */
3458 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3459 || (ctrl_x_mode == 0 && !compl_started))
3460 {
3461 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3462 compl_used_match = TRUE;
3463 }
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3466 {
3467 /*
3468 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3469 * it will be yet. Now we decide.
3470 */
3471 switch (c)
3472 {
3473 case Ctrl_E:
3474 case Ctrl_Y:
3475 ctrl_x_mode = CTRL_X_SCROLL;
3476 if (!(State & REPLACE_FLAG))
3477 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3478 else
3479 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3480 edit_submode_pre = NULL;
3481 showmode();
3482 break;
3483 case Ctrl_L:
3484 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3485 break;
3486 case Ctrl_F:
3487 ctrl_x_mode = CTRL_X_FILES;
3488 break;
3489 case Ctrl_K:
3490 ctrl_x_mode = CTRL_X_DICTIONARY;
3491 break;
3492 case Ctrl_R:
3493 /* Simply allow ^R to happen without affecting ^X mode */
3494 break;
3495 case Ctrl_T:
3496 ctrl_x_mode = CTRL_X_THESAURUS;
3497 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003498#ifdef FEAT_COMPL_FUNC
3499 case Ctrl_U:
3500 ctrl_x_mode = CTRL_X_FUNCTION;
3501 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003503 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003505#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003506 case 's':
3507 case Ctrl_S:
3508 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003509#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003510 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003511 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003512 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003513#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 case Ctrl_RSB:
3516 ctrl_x_mode = CTRL_X_TAGS;
3517 break;
3518#ifdef FEAT_FIND_ID
3519 case Ctrl_I:
3520 case K_S_TAB:
3521 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3522 break;
3523 case Ctrl_D:
3524 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3525 break;
3526#endif
3527 case Ctrl_V:
3528 case Ctrl_Q:
3529 ctrl_x_mode = CTRL_X_CMDLINE;
3530 break;
3531 case Ctrl_P:
3532 case Ctrl_N:
3533 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3534 * just started ^X mode, or there were enough ^X's to cancel
3535 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3536 * do normal expansion when interrupting a different mode (say
3537 * ^X^F^X^P or ^P^X^X^P, see below)
3538 * nothing changes if interrupting mode 0, (eg, the flag
3539 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003540 if (!(compl_cont_status & CONT_INTRPT))
3541 compl_cont_status |= CONT_LOCAL;
3542 else if (compl_cont_mode != 0)
3543 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /* FALLTHROUGH */
3545 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003546 /* If we have typed at least 2 ^X's... for modes != 0, we set
3547 * compl_cont_status = 0 (eg, as if we had just started ^X
3548 * mode).
3549 * For mode 0, we set "compl_cont_mode" to an impossible
3550 * value, in both cases ^X^X can be used to restart the same
3551 * mode (avoiding ADDING mode).
3552 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3553 * 'complete' and local ^P expansions respectively.
3554 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3555 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (c == Ctrl_X)
3557 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003558 if (compl_cont_mode != 0)
3559 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563 ctrl_x_mode = 0;
3564 edit_submode = NULL;
3565 showmode();
3566 break;
3567 }
3568 }
3569 else if (ctrl_x_mode != 0)
3570 {
3571 /* We're already in CTRL-X mode, do we stay in it? */
3572 if (!vim_is_ctrl_x_key(c))
3573 {
3574 if (ctrl_x_mode == CTRL_X_SCROLL)
3575 ctrl_x_mode = 0;
3576 else
3577 ctrl_x_mode = CTRL_X_FINISHED;
3578 edit_submode = NULL;
3579 }
3580 showmode();
3581 }
3582
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003583 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 /* Show error message from attempted keyword completion (probably
3586 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003587 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003589 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3590 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 || ctrl_x_mode == CTRL_X_FINISHED)
3592 {
3593 /* Get here when we have finished typing a sequence of ^N and
3594 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003595 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003596 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003598 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003599 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003602 * If any of the original typed text has been changed, eg when
3603 * ignorecase is set, we must add back-spaces to the redo
3604 * buffer. We add as few as necessary to delete just the part
3605 * of the original text that has changed.
3606 * When using the longest match, edited the match or used
3607 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003609 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3610 ptr = compl_curr_match->cp_str;
3611 else if (compl_leader != NULL)
3612 ptr = compl_leader;
3613 else
3614 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003615 if (compl_orig_text != NULL)
3616 {
3617 p = compl_orig_text;
3618 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3619 ++temp)
3620 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003621#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003622 if (temp > 0)
3623 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003624#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003625 for (p += temp; *p != NUL; mb_ptr_adv(p))
3626 AppendCharToRedobuff(K_BS);
3627 }
3628 if (ptr != NULL)
3629 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632#ifdef FEAT_CINDENT
3633 want_cindent = (can_cindent && cindent_on());
3634#endif
3635 /*
3636 * When completing whole lines: fix indent for 'cindent'.
3637 * Otherwise, break line if it's too long.
3638 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003639 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641#ifdef FEAT_CINDENT
3642 /* re-indent the current line */
3643 if (want_cindent)
3644 {
3645 do_c_expr_indent();
3646 want_cindent = FALSE; /* don't do it again */
3647 }
3648#endif
3649 }
3650 else
3651 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003652 int prev_col = curwin->w_cursor.col;
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003655 if (prev_col > 0)
3656 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (stop_arrow() == OK)
3658 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003659 if (prev_col > 0
3660 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3661 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003664 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003665 * the selection without inserting anything. When
3666 * compl_enter_selects is set the Enter key does the same. */
3667 if ((c == Ctrl_Y || (compl_enter_selects
3668 && (c == CAR || c == K_KENTER || c == NL)))
3669 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003670 retval = TRUE;
3671
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003672 /* CTRL-E means completion is Ended, go back to the typed text. */
3673 if (c == Ctrl_E)
3674 {
3675 ins_compl_delete();
3676 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003677 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003678 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003679 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003680 retval = TRUE;
3681 }
3682
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003683 auto_format(FALSE, TRUE);
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 compl_started = FALSE;
3687 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 msg_clr_cmdline(); /* necessary for "noshowmode" */
3689 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003690 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 if (edit_submode != NULL)
3692 {
3693 edit_submode = NULL;
3694 showmode();
3695 }
3696
3697#ifdef FEAT_CINDENT
3698 /*
3699 * Indent now if a key was typed that is in 'cinkeys'.
3700 */
3701 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3702 do_c_expr_indent();
3703#endif
3704 }
3705 }
3706
3707 /* reset continue_* if we left expansion-mode, if we stay they'll be
3708 * (re)set properly in ins_complete() */
3709 if (!vim_is_ctrl_x_key(c))
3710 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003711 compl_cont_status = 0;
3712 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003714
3715 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716}
3717
3718/*
3719 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3720 * (depending on flag) starting from buf and looking for a non-scanned
3721 * buffer (other than curbuf). curbuf is special, if it is called with
3722 * buf=curbuf then it has to be the first call for a given flag/expansion.
3723 *
3724 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3725 */
3726 static buf_T *
3727ins_compl_next_buf(buf, flag)
3728 buf_T *buf;
3729 int flag;
3730{
3731#ifdef FEAT_WINDOWS
3732 static win_T *wp;
3733#endif
3734
3735 if (flag == 'w') /* just windows */
3736 {
3737#ifdef FEAT_WINDOWS
3738 if (buf == curbuf) /* first call for this flag/expansion */
3739 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003740 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 && wp->w_buffer->b_scanned)
3742 ;
3743 buf = wp->w_buffer;
3744#else
3745 buf = curbuf;
3746#endif
3747 }
3748 else
3749 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3750 * (unlisted buffers)
3751 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003752 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 && ((flag == 'U'
3754 ? buf->b_p_bl
3755 : (!buf->b_p_bl
3756 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003757 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 ;
3759 return buf;
3760}
3761
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003762#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003763static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003764
3765/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003766 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003767 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003768 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003769 static void
3770expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003771 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003772 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003773{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003774 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003775 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003776 char_u *funcname;
3777 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003778
Bram Moolenaare344bea2005-09-01 20:46:49 +00003779 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3780 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003781 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003782
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003783 /* Call 'completefunc' to obtain the list of matches. */
3784 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003785 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003786
Bram Moolenaare344bea2005-09-01 20:46:49 +00003787 pos = curwin->w_cursor;
3788 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3789 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003790 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003791 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003792
Bram Moolenaara94bc432006-03-10 21:42:59 +00003793 ins_compl_add_list(matchlist);
3794 list_unref(matchlist);
3795}
3796#endif /* FEAT_COMPL_FUNC */
3797
Bram Moolenaar39f05632006-03-19 22:15:26 +00003798#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003799/*
3800 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003801 */
3802 static void
3803ins_compl_add_list(list)
3804 list_T *list;
3805{
3806 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003807 int dir = compl_direction;
3808
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003809 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003810 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003811 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003812 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3813 /* if dir was BACKWARD then honor it just once */
3814 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003815 else if (did_emsg)
3816 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003817 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003818}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003819
3820/*
3821 * Add a match to the list of matches from a typeval_T.
3822 * If the given string is already in the list of completions, then return
3823 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3824 * maybe because alloc() returns NULL, then FAIL is returned.
3825 */
3826 int
3827ins_compl_add_tv(tv, dir)
3828 typval_T *tv;
3829 int dir;
3830{
3831 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003832 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003833 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003834 char_u *(cptext[CPT_COUNT]);
3835
3836 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3837 {
3838 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3839 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3840 (char_u *)"abbr", FALSE);
3841 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3842 (char_u *)"menu", FALSE);
3843 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3844 (char_u *)"kind", FALSE);
3845 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3846 (char_u *)"info", FALSE);
3847 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3848 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003849 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003850 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003851 }
3852 else
3853 {
3854 word = get_tv_string_chk(tv);
3855 vim_memset(cptext, 0, sizeof(cptext));
3856 }
3857 if (word == NULL || *word == NUL)
3858 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003859 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003860}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003861#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003862
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863/*
3864 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003865 * The search starts at position "ini" in curbuf and in the direction
3866 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003867 * When "compl_started" is FALSE start at that position, otherwise continue
3868 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003869 * This may return before finding all the matches.
3870 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 */
3872 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003873ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 static pos_T first_match_pos;
3877 static pos_T last_match_pos;
3878 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 static int found_all = FALSE; /* Found all matches of a
3880 certain type. */
3881 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaar572cb562005-08-05 21:35:02 +00003883 pos_T *pos;
3884 char_u **matches;
3885 int save_p_scs;
3886 int save_p_ws;
3887 int save_p_ic;
3888 int i;
3889 int num_matches;
3890 int len;
3891 int found_new_match;
3892 int type = ctrl_x_mode;
3893 char_u *ptr;
3894 char_u *dict = NULL;
3895 int dict_f = 0;
3896 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003898 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
3900 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3901 ins_buf->b_scanned = 0;
3902 found_all = FALSE;
3903 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003905 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 last_match_pos = first_match_pos = *ini;
3907 }
3908
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003909 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003910 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3912 for (;;)
3913 {
3914 found_new_match = FAIL;
3915
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003916 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 * or if found_all says this entry is done. For ^X^L only use the
3918 * entries from 'complete' that look in loaded buffers. */
3919 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
3922 found_all = FALSE;
3923 while (*e_cpt == ',' || *e_cpt == ' ')
3924 e_cpt++;
3925 if (*e_cpt == '.' && !curbuf->b_scanned)
3926 {
3927 ins_buf = curbuf;
3928 first_match_pos = *ini;
3929 /* So that ^N can match word immediately after cursor */
3930 if (ctrl_x_mode == 0)
3931 dec(&first_match_pos);
3932 last_match_pos = first_match_pos;
3933 type = 0;
3934 }
3935 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3936 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3937 {
3938 /* Scan a buffer, but not the current one. */
3939 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3940 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 first_match_pos.col = last_match_pos.col = 0;
3943 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3944 last_match_pos.lnum = 0;
3945 type = 0;
3946 }
3947 else /* unloaded buffer, scan like dictionary */
3948 {
3949 found_all = TRUE;
3950 if (ins_buf->b_fname == NULL)
3951 continue;
3952 type = CTRL_X_DICTIONARY;
3953 dict = ins_buf->b_fname;
3954 dict_f = DICT_EXACT;
3955 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003956 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 ins_buf->b_fname == NULL
3958 ? buf_spname(ins_buf)
3959 : ins_buf->b_sfname == NULL
3960 ? (char *)ins_buf->b_fname
3961 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003962 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else if (*e_cpt == NUL)
3965 break;
3966 else
3967 {
3968 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3969 type = -1;
3970 else if (*e_cpt == 'k' || *e_cpt == 's')
3971 {
3972 if (*e_cpt == 'k')
3973 type = CTRL_X_DICTIONARY;
3974 else
3975 type = CTRL_X_THESAURUS;
3976 if (*++e_cpt != ',' && *e_cpt != NUL)
3977 {
3978 dict = e_cpt;
3979 dict_f = DICT_FIRST;
3980 }
3981 }
3982#ifdef FEAT_FIND_ID
3983 else if (*e_cpt == 'i')
3984 type = CTRL_X_PATH_PATTERNS;
3985 else if (*e_cpt == 'd')
3986 type = CTRL_X_PATH_DEFINES;
3987#endif
3988 else if (*e_cpt == ']' || *e_cpt == 't')
3989 {
3990 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003991 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003992 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 else
3995 type = -1;
3996
3997 /* in any case e_cpt is advanced to the next entry */
3998 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3999
4000 found_all = TRUE;
4001 if (type == -1)
4002 continue;
4003 }
4004 }
4005
4006 switch (type)
4007 {
4008 case -1:
4009 break;
4010#ifdef FEAT_FIND_ID
4011 case CTRL_X_PATH_PATTERNS:
4012 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004013 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4018 (linenr_T)1, (linenr_T)MAXLNUM);
4019 break;
4020#endif
4021
4022 case CTRL_X_DICTIONARY:
4023 case CTRL_X_THESAURUS:
4024 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004025 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 : (type == CTRL_X_THESAURUS
4027 ? (*curbuf->b_p_tsr == NUL
4028 ? p_tsr
4029 : curbuf->b_p_tsr)
4030 : (*curbuf->b_p_dict == NUL
4031 ? p_dict
4032 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004033 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004034 dict != NULL ? dict_f
4035 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 dict = NULL;
4037 break;
4038
4039 case CTRL_X_TAGS:
4040 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4041 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004042 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004045 * of matches is found when compl_pattern is empty */
4046 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4048 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4049 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4050 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004051 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053 p_ic = save_p_ic;
4054 break;
4055
4056 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4059 {
4060
4061 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004063 ins_compl_add_matches(num_matches, matches,
4064#ifdef CASE_INSENSITIVE_FILENAME
4065 TRUE
4066#else
4067 FALSE
4068#endif
4069 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 break;
4072
4073 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 if (expand_cmdline(&compl_xp, compl_pattern,
4075 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004077 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 break;
4079
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004080#ifdef FEAT_COMPL_FUNC
4081 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004082 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004083 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004084 break;
4085#endif
4086
Bram Moolenaar488c6512005-08-11 20:09:58 +00004087 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004088#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004089 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004090 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004091 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004092 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004093#endif
4094 break;
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 default: /* normal ^P/^N and ^X^L */
4097 /*
4098 * If 'infercase' is set, don't use 'smartcase' here
4099 */
4100 save_p_scs = p_scs;
4101 if (ins_buf->b_p_inf)
4102 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 /* buffers other than curbuf are scanned from the beginning or the
4105 * end but never from the middle, thus setting nowrapscan in this
4106 * buffers is a good idea, on the other hand, we always set
4107 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4108 save_p_ws = p_ws;
4109 if (ins_buf != curbuf)
4110 p_ws = FALSE;
4111 else if (*e_cpt == '.')
4112 p_ws = TRUE;
4113 for (;;)
4114 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004115 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004117 ++msg_silent; /* Don't want messages for wrapscan. */
4118
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004119 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4120 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004124 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004126 found_new_match = searchit(NULL, ins_buf, pos,
4127 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004129 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004130 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004133 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 first_match_pos = *pos;
4136 last_match_pos = *pos;
4137 }
4138 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004139 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 found_new_match = FAIL;
4141 if (found_new_match == FAIL)
4142 {
4143 if (ins_buf == curbuf)
4144 found_all = TRUE;
4145 break;
4146 }
4147
4148 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004149 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 && ini->lnum == pos->lnum
4151 && ini->col == pos->col)
4152 continue;
4153 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4154 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4155 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
4158 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4159 continue;
4160 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4161 if (!p_paste)
4162 ptr = skipwhite(ptr);
4163 }
4164 len = (int)STRLEN(ptr);
4165 }
4166 else
4167 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 char_u *tmp_ptr = ptr;
4169
4170 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004172 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 /* Skip if already inside a word. */
4174 if (vim_iswordp(tmp_ptr))
4175 continue;
4176 /* Find start of next word. */
4177 tmp_ptr = find_word_start(tmp_ptr);
4178 }
4179 /* Find end of this word. */
4180 tmp_ptr = find_word_end(tmp_ptr);
4181 len = (int)(tmp_ptr - ptr);
4182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 if ((compl_cont_status & CONT_ADDING)
4184 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
4186 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4187 {
4188 /* Try next line, if any. the new word will be
4189 * "join" as if the normal command "J" was used.
4190 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 * works -- Acevedo */
4193 STRNCPY(IObuff, ptr, len);
4194 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4195 tmp_ptr = ptr = skipwhite(ptr);
4196 /* Find start of next word. */
4197 tmp_ptr = find_word_start(tmp_ptr);
4198 /* Find end of next word. */
4199 tmp_ptr = find_word_end(tmp_ptr);
4200 if (tmp_ptr > ptr)
4201 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004202 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004204 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 IObuff[len++] = ' ';
4206 /* IObuf =~ "\k.* ", thus len >= 2 */
4207 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004208 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 || (vim_strchr(p_cpo, CPO_JOINSP)
4210 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004211 && (IObuff[len - 2] == '?'
4212 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 IObuff[len++] = ' ';
4214 }
4215 /* copy as much as posible of the new word */
4216 if (tmp_ptr - ptr >= IOSIZE - len)
4217 tmp_ptr = ptr + IOSIZE - len - 1;
4218 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4219 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004220 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 IObuff[len] = NUL;
4223 ptr = IObuff;
4224 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 continue;
4227 }
4228 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004229 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004230 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004231 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 found_new_match = OK;
4234 break;
4235 }
4236 }
4237 p_scs = save_p_scs;
4238 p_ws = save_p_ws;
4239 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004242 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004243 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 found_new_match = OK;
4245
4246 /* break the loop for specialized modes (use 'complete' just for the
4247 * generic ctrl_x_mode == 0) or when we've found a new match */
4248 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004250 {
4251 if (got_int)
4252 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004253 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004254 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004255 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004256
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004257 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4258 || compl_interrupted)
4259 break;
4260 compl_started = TRUE;
4261 }
4262 else
4263 {
4264 /* Mark a buffer scanned when it has been scanned completely */
4265 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4266 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004268 compl_started = FALSE;
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4274 && *e_cpt == NUL) /* Got to end of 'complete' */
4275 found_new_match = FAIL;
4276
4277 i = -1; /* total of matches, unknown */
4278 if (found_new_match == FAIL
4279 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4280 i = ins_compl_make_cyclic();
4281
4282 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 * just been made cyclic then we have to move compl_curr_match to the next
4284 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004285 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4286 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 if (compl_curr_match == NULL)
4288 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return i;
4290}
4291
4292/* Delete the old text being completed. */
4293 static void
4294ins_compl_delete()
4295{
4296 int i;
4297
4298 /*
4299 * In insert mode: Delete the typed part.
4300 * In replace mode: Put the old characters back, if any.
4301 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 backspace_until_column(i);
4304 changed_cline_bef_curs();
4305}
4306
4307/* Insert the new text being completed. */
4308 static void
4309ins_compl_insert()
4310{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004311 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004312 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4313 compl_used_match = FALSE;
4314 else
4315 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316}
4317
4318/*
4319 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004320 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4321 * get more completions. If it is FALSE, then we just do nothing when there
4322 * are no more completions in a given direction. The latter case is used when
4323 * we are still in the middle of finding completions, to allow browsing
4324 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 * Return the total number of matches, or -1 if still unknown -- webb.
4326 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004327 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4328 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 *
4330 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004331 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4332 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 */
4334 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004335ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004337 int count; /* repeat completion this many times; should
4338 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004339 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 int num_matches = -1;
4342 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004343 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004344 compl_T *found_compl = NULL;
4345 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004346 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004348 if (compl_leader != NULL
4349 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004351 /* Set "compl_shown_match" to the actually shown match, it may differ
4352 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004353 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004354 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004355 && compl_shown_match->cp_next != NULL
4356 && compl_shown_match->cp_next != compl_first_match)
4357 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004358
4359 /* If we didn't find it searching forward, and compl_shows_dir is
4360 * backward, find the last match. */
4361 if (compl_shows_dir == BACKWARD
4362 && !ins_compl_equal(compl_shown_match,
4363 compl_leader, (int)STRLEN(compl_leader))
4364 && (compl_shown_match->cp_next == NULL
4365 || compl_shown_match->cp_next == compl_first_match))
4366 {
4367 while (!ins_compl_equal(compl_shown_match,
4368 compl_leader, (int)STRLEN(compl_leader))
4369 && compl_shown_match->cp_prev != NULL
4370 && compl_shown_match->cp_prev != compl_first_match)
4371 compl_shown_match = compl_shown_match->cp_prev;
4372 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004373 }
4374
4375 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004376 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 /* Delete old text to be replaced */
4378 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004379
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004380 /* When finding the longest common text we stick at the original text,
4381 * don't let CTRL-N or CTRL-P move to the first match. */
4382 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4383
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004384 /* When restarting the search don't insert the first match either. */
4385 if (compl_restarting)
4386 {
4387 advance = FALSE;
4388 compl_restarting = FALSE;
4389 }
4390
Bram Moolenaare3226be2005-12-18 22:10:00 +00004391 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4392 * around. */
4393 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004395 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004397 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004398 found_end = (compl_first_match != NULL
4399 && (compl_shown_match->cp_next == compl_first_match
4400 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004401 }
4402 else if (compl_shows_dir == BACKWARD
4403 && compl_shown_match->cp_prev != NULL)
4404 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004405 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004406 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004407 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004410 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004411 if (!allow_get_expansion)
4412 {
4413 if (advance)
4414 {
4415 if (compl_shows_dir == BACKWARD)
4416 compl_pending -= todo + 1;
4417 else
4418 compl_pending += todo + 1;
4419 }
4420 return -1;
4421 }
4422
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004423 if (advance)
4424 {
4425 if (compl_shows_dir == BACKWARD)
4426 --compl_pending;
4427 else
4428 ++compl_pending;
4429 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004430
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004431 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004432 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004433
4434 /* handle any pending completions */
4435 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004436 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004437 {
4438 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4439 {
4440 compl_shown_match = compl_shown_match->cp_next;
4441 --compl_pending;
4442 }
4443 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4444 {
4445 compl_shown_match = compl_shown_match->cp_prev;
4446 ++compl_pending;
4447 }
4448 else
4449 break;
4450 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004451 found_end = FALSE;
4452 }
4453 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4454 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004455 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004456 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004457 ++todo;
4458 else
4459 /* Remember a matching item. */
4460 found_compl = compl_shown_match;
4461
4462 /* Stop at the end of the list when we found a usable match. */
4463 if (found_end)
4464 {
4465 if (found_compl != NULL)
4466 {
4467 compl_shown_match = found_compl;
4468 break;
4469 }
4470 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004474 /* Insert the text of the new completion, or the compl_leader. */
4475 if (insert_match)
4476 {
4477 if (!compl_get_longest || compl_used_match)
4478 ins_compl_insert();
4479 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004480 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004481 }
4482 else
4483 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 if (!allow_get_expansion)
4486 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004487 /* may undisplay the popup menu first */
4488 ins_compl_upd_pum();
4489
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004490 /* redraw to show the user what was inserted */
4491 update_screen(0);
4492
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004493 /* display the updated popup menu */
4494 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004495#ifdef FEAT_GUI
4496 if (gui.in_use)
4497 {
4498 /* Show the cursor after the match, not after the redrawn text. */
4499 setcursor();
4500 out_flush();
4501 gui_update_cursor(FALSE, FALSE);
4502 }
4503#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /* Delete old text to be replaced, since we're still searching and
4506 * don't want to match ourselves! */
4507 ins_compl_delete();
4508 }
4509
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004510 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004511 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004512 compl_enter_selects = !insert_match && compl_match_array != NULL;
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /*
4515 * Show the file name for the match (if any)
4516 * Truncate the file name to avoid a wait for return.
4517 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004518 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
4520 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004521 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (i <= 0)
4523 i = 0;
4524 else
4525 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004526 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 msg(IObuff);
4528 redraw_cmdline = FALSE; /* don't overwrite! */
4529 }
4530
4531 return num_matches;
4532}
4533
4534/*
4535 * Call this while finding completions, to check whether the user has hit a key
4536 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004537 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004539 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 */
4541 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004542ins_compl_check_keys(frequency)
4543 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
4545 static int count = 0;
4546
4547 int c;
4548
4549 /* Don't check when reading keys from a script. That would break the test
4550 * scripts */
4551 if (using_script())
4552 return;
4553
4554 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004555 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 return;
4557 count = 0;
4558
Bram Moolenaara260a972006-06-23 19:36:29 +00004559 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4560 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (c != NUL)
4563 {
4564 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4565 {
4566 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004567 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004568 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4569 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004571 else
4572 {
4573 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004574 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004575 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004576 if (c != K_IGNORE)
4577 {
4578 /* Don't interrupt completion when the character wasn't typed,
4579 * e.g., when doing @q to replay keys. */
4580 if (c != Ctrl_R && KeyTyped)
4581 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004582
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004583 vungetc(c);
4584 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004587 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004588 {
4589 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4590
4591 compl_pending = 0;
4592 (void)ins_compl_next(FALSE, todo, TRUE);
4593 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004594}
4595
4596/*
4597 * Decide the direction of Insert mode complete from the key typed.
4598 * Returns BACKWARD or FORWARD.
4599 */
4600 static int
4601ins_compl_key2dir(c)
4602 int c;
4603{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004604 if (c == Ctrl_P || c == Ctrl_L
4605 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4606 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004607 return BACKWARD;
4608 return FORWARD;
4609}
4610
4611/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004612 * Return TRUE for keys that are used for completion only when the popup menu
4613 * is visible.
4614 */
4615 static int
4616ins_compl_pum_key(c)
4617 int c;
4618{
4619 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004620 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4621 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004622}
4623
4624/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004625 * Decide the number of completions to move forward.
4626 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4627 */
4628 static int
4629ins_compl_key2count(c)
4630 int c;
4631{
4632 int h;
4633
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004634 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004635 {
4636 h = pum_get_height();
4637 if (h > 3)
4638 h -= 2; /* keep some context */
4639 return h;
4640 }
4641 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642}
4643
4644/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004645 * Return TRUE if completion with "c" should insert the match, FALSE if only
4646 * to change the currently selected completion.
4647 */
4648 static int
4649ins_compl_use_match(c)
4650 int c;
4651{
4652 switch (c)
4653 {
4654 case K_UP:
4655 case K_DOWN:
4656 case K_PAGEDOWN:
4657 case K_KPAGEDOWN:
4658 case K_S_DOWN:
4659 case K_PAGEUP:
4660 case K_KPAGEUP:
4661 case K_S_UP:
4662 return FALSE;
4663 }
4664 return TRUE;
4665}
4666
4667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 * Do Insert mode completion.
4669 * Called when character "c" was typed, which has a meaning for completion.
4670 * Returns OK if completion was done, FAIL if something failed (out of mem).
4671 */
4672 static int
4673ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 char_u *line;
4677 int startcol = 0; /* column where searched text starts */
4678 colnr_T curs_col; /* cursor column */
4679 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
Bram Moolenaare3226be2005-12-18 22:10:00 +00004681 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
4684 /* First time we hit ^N or ^P (in a row, I mean) */
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 did_ai = FALSE;
4687#ifdef FEAT_SMARTINDENT
4688 did_si = FALSE;
4689 can_si = FALSE;
4690 can_si_back = FALSE;
4691#endif
4692 if (stop_arrow() == FAIL)
4693 return FAIL;
4694
4695 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004696 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004697 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004699 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004700 * "compl_startpos" to the cursor as a pattern to add a new word
4701 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004702 * "compl_startpos" is not in the same line as the cursor then fix it
4703 * (the line has been split because it was longer than 'tw'). if SOL
4704 * is set then skip the previous pattern, a word at the beginning of
4705 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004706 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4707 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 /*
4710 * it is a continued search
4711 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4714 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 /* line (probably) wrapped, set compl_startpos to the
4719 * first non_blank in the line, if it is not a wordchar
4720 * include it to get a better pattern, but then we don't
4721 * want the "\\<" prefix, check it bellow */
4722 compl_col = (colnr_T)(skipwhite(line) - line);
4723 compl_startpos.col = compl_col;
4724 compl_startpos.lnum = curwin->w_cursor.lnum;
4725 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727 else
4728 {
4729 /* S_IPOS was set when we inserted a word that was at the
4730 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004731 * mode but first we need to redefine compl_startpos */
4732 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004734 compl_cont_status |= CONT_SOL;
4735 compl_startpos.col = (colnr_T)(skipwhite(
4736 line + compl_length
4737 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004739 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004742 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004743 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004745 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 compl_cont_status &= ~CONT_SOL;
4748 compl_length = (IOSIZE - MIN_SPACE);
4749 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004751 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4752 if (compl_length < 1)
4753 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004758 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004763 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 compl_cont_status = 0;
4768 compl_cont_status |= CONT_N_ADDS;
4769 compl_startpos = curwin->w_cursor;
4770 startcol = (int)curs_col;
4771 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773
4774 /* Work out completion pattern and original text -- webb */
4775 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4776 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004777 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4779 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004780 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004782 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004784 compl_col += ++startcol;
4785 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 compl_pattern = str_foldcase(line + compl_col,
4789 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 compl_pattern = vim_strnsave(line + compl_col,
4792 compl_length);
4793 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 return FAIL;
4795 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004796 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 char_u *prefix = (char_u *)"\\<";
4799
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004800 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004802 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 if (!vim_iswordp(line + compl_col)
4806 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 && (
4808#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812#endif
4813 )))
4814 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 STRCPY((char *)compl_pattern, prefix);
4816 (void)quote_meta(compl_pattern + STRLEN(prefix),
4817 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004821 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#endif
4825 )
4826 {
4827 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004828 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4829 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 compl_col += curs_col;
4832 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 else
4835 {
4836#ifdef FEAT_MBYTE
4837 /* Search the point of change class of multibyte character
4838 * or not a word single byte character backward. */
4839 if (has_mbyte)
4840 {
4841 int base_class;
4842 int head_off;
4843
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004844 startcol -= (*mb_head_off)(line, line + startcol);
4845 base_class = mb_get_class(line + startcol);
4846 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004848 head_off = (*mb_head_off)(line, line + startcol);
4849 if (base_class != mb_get_class(line + startcol
4850 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855 else
4856#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004857 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 compl_col += ++startcol;
4860 compl_length = (int)curs_col - startcol;
4861 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
4863 /* Only match word with at least two chars -- webb
4864 * there's no need to call quote_meta,
4865 * alloc(7) is enough -- Acevedo
4866 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 compl_pattern = alloc(7);
4868 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004870 STRCPY((char *)compl_pattern, "\\<");
4871 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4872 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 else
4875 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004876 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004877 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004878 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004880 STRCPY((char *)compl_pattern, "\\<");
4881 (void)quote_meta(compl_pattern + 2, line + compl_col,
4882 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 }
4885 }
4886 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4887 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004888 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004889 compl_length = (int)curs_col - (int)compl_col;
4890 if (compl_length < 0) /* cursor in indent: empty pattern */
4891 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004893 compl_pattern = str_foldcase(line + compl_col, compl_length,
4894 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004896 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4897 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 return FAIL;
4899 }
4900 else if (ctrl_x_mode == CTRL_X_FILES)
4901 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004902 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 compl_col += ++startcol;
4905 compl_length = (int)curs_col - startcol;
4906 compl_pattern = addstar(line + compl_col, compl_length,
4907 EXPAND_FILES);
4908 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 return FAIL;
4910 }
4911 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4912 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004913 compl_pattern = vim_strnsave(line, curs_col);
4914 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004916 set_cmd_context(&compl_xp, compl_pattern,
4917 (int)STRLEN(compl_pattern), curs_col);
4918 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4919 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004920 /* No completion possible, use an empty pattern to get a
4921 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004922 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004923 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004924 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4925 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004927 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004928 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004929#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004930 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004931 * Call user defined function 'completefunc' with "a:findstart"
4932 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004933 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004934 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004935 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004936 char_u *funcname;
4937 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004938
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004939 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004940 * string */
4941 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4942 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4943 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004944 {
4945 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4946 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004947 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004948 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004949
4950 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004951 args[1] = NULL;
4952 pos = curwin->w_cursor;
4953 col = call_func_retnr(funcname, 2, args, FALSE);
4954 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004955
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004956 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004957 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004958 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004959 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004960 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004961
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004962 /* Setup variables for completion. Need to obtain "line" again,
4963 * it may have become invalid. */
4964 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004965 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004966 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4967 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004968#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004969 return FAIL;
4970 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004971 else if (ctrl_x_mode == CTRL_X_SPELL)
4972 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004973#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004974 if (spell_bad_len > 0)
4975 compl_col = curs_col - spell_bad_len;
4976 else
4977 compl_col = spell_word_start(startcol);
4978 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004979 {
4980 compl_length = 0;
4981 compl_col = curs_col;
4982 }
4983 else
4984 {
4985 spell_expand_check_cap(compl_col);
4986 compl_length = (int)curs_col - compl_col;
4987 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004988 /* Need to obtain "line" again, it may have become invalid. */
4989 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004990 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4991 if (compl_pattern == NULL)
4992#endif
4993 return FAIL;
4994 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004995 else
4996 {
4997 EMSG2(_(e_intern2), "ins_complete()");
4998 return FAIL;
4999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005001 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
5003 edit_submode_pre = (char_u *)_(" Adding");
5004 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5005 {
5006 /* Insert a new line, keep indentation but ignore 'comments' */
5007#ifdef FEAT_COMMENTS
5008 char_u *old = curbuf->b_p_com;
5009
5010 curbuf->b_p_com = (char_u *)"";
5011#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005012 compl_startpos.lnum = curwin->w_cursor.lnum;
5013 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 ins_eol('\r');
5015#ifdef FEAT_COMMENTS
5016 curbuf->b_p_com = old;
5017#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 compl_length = 0;
5019 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 }
5022 else
5023 {
5024 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005025 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 if (compl_cont_status & CONT_LOCAL)
5029 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 else
5031 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5032
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005033 /* Always add completion for the original text. */
5034 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005035 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5036 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005037 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005039 vim_free(compl_pattern);
5040 compl_pattern = NULL;
5041 vim_free(compl_orig_text);
5042 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 return FAIL;
5044 }
5045
5046 /* showmode might reset the internal line pointers, so it must
5047 * be called before line = ml_get(), or when this address is no
5048 * longer needed. -- Acevedo.
5049 */
5050 edit_submode_extra = (char_u *)_("-- Searching...");
5051 edit_submode_highl = HLF_COUNT;
5052 showmode();
5053 edit_submode_extra = NULL;
5054 out_flush();
5055 }
5056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 compl_shown_match = compl_curr_match;
5058 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005061 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005063 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005065 /* may undisplay the popup menu */
5066 ins_compl_upd_pum();
5067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005068 if (n > 1) /* all matches have been found */
5069 compl_matches = n;
5070 compl_curr_match = compl_shown_match;
5071 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
Bram Moolenaard68071d2006-05-02 22:08:30 +00005073 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5074 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 if (got_int && !global_busy)
5076 {
5077 (void)vgetc();
5078 got_int = FALSE;
5079 }
5080
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005081 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005082 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5085 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5087 edit_submode_highl = HLF_E;
5088 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5089 * because we couldn't expand anything at first place, but if we used
5090 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5091 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 if ( compl_length > 1
5093 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 || (ctrl_x_mode != 0
5095 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5096 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005097 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099
Bram Moolenaar572cb562005-08-05 21:35:02 +00005100 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005101 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
5105 if (edit_submode_extra == NULL)
5106 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005107 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 edit_submode_extra = (char_u *)_("Back at original");
5110 edit_submode_highl = HLF_W;
5111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 edit_submode_extra = (char_u *)_("Word from other line");
5115 edit_submode_highl = HLF_COUNT;
5116 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005117 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 edit_submode_extra = (char_u *)_("The only match");
5120 edit_submode_highl = HLF_COUNT;
5121 }
5122 else
5123 {
5124 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005125 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005127 int number = 0;
5128 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005130 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
5132 /* search backwards for the first valid (!= -1) number.
5133 * This should normally succeed already at the first loop
5134 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005135 for (match = compl_curr_match->cp_prev; match != NULL
5136 && match != compl_first_match;
5137 match = match->cp_prev)
5138 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005140 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 break;
5142 }
5143 if (match != NULL)
5144 /* go up and assign all numbers which are not assigned
5145 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005146 for (match = match->cp_next;
5147 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005148 match = match->cp_next)
5149 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 else /* BACKWARD */
5152 {
5153 /* search forwards (upwards) for the first valid (!= -1)
5154 * number. This should normally succeed already at the
5155 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005156 for (match = compl_curr_match->cp_next; match != NULL
5157 && match != compl_first_match;
5158 match = match->cp_next)
5159 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005161 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163 }
5164 if (match != NULL)
5165 /* go down and assign all numbers which are not
5166 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005167 for (match = match->cp_prev; match
5168 && match->cp_number == -1;
5169 match = match->cp_prev)
5170 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 }
5173
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005174 /* The match should always have a sequence number now, this is
5175 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005176 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005178 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5179 * Translations may need more than twice that. */
5180 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005183 vim_snprintf((char *)match_ref, sizeof(match_ref),
5184 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005185 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005187 vim_snprintf((char *)match_ref, sizeof(match_ref),
5188 _("match %d"),
5189 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 edit_submode_extra = match_ref;
5191 edit_submode_highl = HLF_R;
5192 if (dollar_vcol)
5193 curs_columns(FALSE);
5194 }
5195 }
5196 }
5197
5198 /* Show a message about what (completion) mode we're in. */
5199 showmode();
5200 if (edit_submode_extra != NULL)
5201 {
5202 if (!p_smd)
5203 msg_attr(edit_submode_extra,
5204 edit_submode_highl < HLF_COUNT
5205 ? hl_attr(edit_submode_highl) : 0);
5206 }
5207 else
5208 msg_clr_cmdline(); /* necessary for "noshowmode" */
5209
Bram Moolenaard68071d2006-05-02 22:08:30 +00005210 /* Show the popup menu, unless we got interrupted. */
5211 if (!compl_interrupted)
5212 {
5213 /* RedrawingDisabled may be set when invoked through complete(). */
5214 n = RedrawingDisabled;
5215 RedrawingDisabled = 0;
5216 ins_compl_show_pum();
5217 setcursor();
5218 RedrawingDisabled = n;
5219 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005220 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005221 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 return OK;
5224}
5225
5226/*
5227 * Looks in the first "len" chars. of "src" for search-metachars.
5228 * If dest is not NULL the chars. are copied there quoting (with
5229 * a backslash) the metachars, and dest would be NUL terminated.
5230 * Returns the length (needed) of dest
5231 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005232 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233quote_meta(dest, src, len)
5234 char_u *dest;
5235 char_u *src;
5236 int len;
5237{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005238 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005240 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 switch (*src)
5243 {
5244 case '.':
5245 case '*':
5246 case '[':
5247 if (ctrl_x_mode == CTRL_X_DICTIONARY
5248 || ctrl_x_mode == CTRL_X_THESAURUS)
5249 break;
5250 case '~':
5251 if (!p_magic) /* quote these only if magic is set */
5252 break;
5253 case '\\':
5254 if (ctrl_x_mode == CTRL_X_DICTIONARY
5255 || ctrl_x_mode == CTRL_X_THESAURUS)
5256 break;
5257 case '^': /* currently it's not needed. */
5258 case '$':
5259 m++;
5260 if (dest != NULL)
5261 *dest++ = '\\';
5262 break;
5263 }
5264 if (dest != NULL)
5265 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005266# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 /* Copy remaining bytes of a multibyte character. */
5268 if (has_mbyte)
5269 {
5270 int i, mb_len;
5271
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005272 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (mb_len > 0 && len >= mb_len)
5274 for (i = 0; i < mb_len; ++i)
5275 {
5276 --len;
5277 ++src;
5278 if (dest != NULL)
5279 *dest++ = *src;
5280 }
5281 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284 if (dest != NULL)
5285 *dest = NUL;
5286
5287 return m;
5288}
5289#endif /* FEAT_INS_EXPAND */
5290
5291/*
5292 * Next character is interpreted literally.
5293 * A one, two or three digit decimal number is interpreted as its byte value.
5294 * If one or two digits are entered, the next character is given to vungetc().
5295 * For Unicode a character > 255 may be returned.
5296 */
5297 int
5298get_literal()
5299{
5300 int cc;
5301 int nc;
5302 int i;
5303 int hex = FALSE;
5304 int octal = FALSE;
5305#ifdef FEAT_MBYTE
5306 int unicode = 0;
5307#endif
5308
5309 if (got_int)
5310 return Ctrl_C;
5311
5312#ifdef FEAT_GUI
5313 /*
5314 * In GUI there is no point inserting the internal code for a special key.
5315 * It is more useful to insert the string "<KEY>" instead. This would
5316 * probably be useful in a text window too, but it would not be
5317 * vi-compatible (maybe there should be an option for it?) -- webb
5318 */
5319 if (gui.in_use)
5320 ++allow_keys;
5321#endif
5322#ifdef USE_ON_FLY_SCROLL
5323 dont_scroll = TRUE; /* disallow scrolling here */
5324#endif
5325 ++no_mapping; /* don't map the next key hits */
5326 cc = 0;
5327 i = 0;
5328 for (;;)
5329 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005330 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#ifdef FEAT_CMDL_INFO
5332 if (!(State & CMDLINE)
5333# ifdef FEAT_MBYTE
5334 && MB_BYTE2LEN_CHECK(nc) == 1
5335# endif
5336 )
5337 add_to_showcmd(nc);
5338#endif
5339 if (nc == 'x' || nc == 'X')
5340 hex = TRUE;
5341 else if (nc == 'o' || nc == 'O')
5342 octal = TRUE;
5343#ifdef FEAT_MBYTE
5344 else if (nc == 'u' || nc == 'U')
5345 unicode = nc;
5346#endif
5347 else
5348 {
5349 if (hex
5350#ifdef FEAT_MBYTE
5351 || unicode != 0
5352#endif
5353 )
5354 {
5355 if (!vim_isxdigit(nc))
5356 break;
5357 cc = cc * 16 + hex2nr(nc);
5358 }
5359 else if (octal)
5360 {
5361 if (nc < '0' || nc > '7')
5362 break;
5363 cc = cc * 8 + nc - '0';
5364 }
5365 else
5366 {
5367 if (!VIM_ISDIGIT(nc))
5368 break;
5369 cc = cc * 10 + nc - '0';
5370 }
5371
5372 ++i;
5373 }
5374
5375 if (cc > 255
5376#ifdef FEAT_MBYTE
5377 && unicode == 0
5378#endif
5379 )
5380 cc = 255; /* limit range to 0-255 */
5381 nc = 0;
5382
5383 if (hex) /* hex: up to two chars */
5384 {
5385 if (i >= 2)
5386 break;
5387 }
5388#ifdef FEAT_MBYTE
5389 else if (unicode) /* Unicode: up to four or eight chars */
5390 {
5391 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5392 break;
5393 }
5394#endif
5395 else if (i >= 3) /* decimal or octal: up to three chars */
5396 break;
5397 }
5398 if (i == 0) /* no number entered */
5399 {
5400 if (nc == K_ZERO) /* NUL is stored as NL */
5401 {
5402 cc = '\n';
5403 nc = 0;
5404 }
5405 else
5406 {
5407 cc = nc;
5408 nc = 0;
5409 }
5410 }
5411
5412 if (cc == 0) /* NUL is stored as NL */
5413 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005414#ifdef FEAT_MBYTE
5415 if (enc_dbcs && (cc & 0xff) == 0)
5416 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5417 second byte will cause trouble! */
5418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 --no_mapping;
5421#ifdef FEAT_GUI
5422 if (gui.in_use)
5423 --allow_keys;
5424#endif
5425 if (nc)
5426 vungetc(nc);
5427 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5428 return cc;
5429}
5430
5431/*
5432 * Insert character, taking care of special keys and mod_mask
5433 */
5434 static void
5435insert_special(c, allow_modmask, ctrlv)
5436 int c;
5437 int allow_modmask;
5438 int ctrlv; /* c was typed after CTRL-V */
5439{
5440 char_u *p;
5441 int len;
5442
5443 /*
5444 * Special function key, translate into "<Key>". Up to the last '>' is
5445 * inserted with ins_str(), so as not to replace characters in replace
5446 * mode.
5447 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5448 * unless 'allow_modmask' is TRUE.
5449 */
5450#ifdef MACOS
5451 /* Command-key never produces a normal key */
5452 if (mod_mask & MOD_MASK_CMD)
5453 allow_modmask = TRUE;
5454#endif
5455 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5456 {
5457 p = get_special_key_name(c, mod_mask);
5458 len = (int)STRLEN(p);
5459 c = p[len - 1];
5460 if (len > 2)
5461 {
5462 if (stop_arrow() == FAIL)
5463 return;
5464 p[len - 1] = NUL;
5465 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005466 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 ctrlv = FALSE;
5468 }
5469 }
5470 if (stop_arrow() == OK)
5471 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5472}
5473
5474/*
5475 * Special characters in this context are those that need processing other
5476 * than the simple insertion that can be performed here. This includes ESC
5477 * which terminates the insert, and CR/NL which need special processing to
5478 * open up a new line. This routine tries to optimize insertions performed by
5479 * the "redo", "undo" or "put" commands, so it needs to know when it should
5480 * stop and defer processing to the "normal" mechanism.
5481 * '0' and '^' are special, because they can be followed by CTRL-D.
5482 */
5483#ifdef EBCDIC
5484# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5485#else
5486# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5487#endif
5488
5489#ifdef FEAT_MBYTE
5490# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5491#else
5492# define WHITECHAR(cc) vim_iswhite(cc)
5493#endif
5494
5495 void
5496insertchar(c, flags, second_indent)
5497 int c; /* character to insert or NUL */
5498 int flags; /* INSCHAR_FORMAT, etc. */
5499 int second_indent; /* indent for second line if >= 0 */
5500{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 int textwidth;
5502#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5508 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510 /*
5511 * Try to break the line in two or more pieces when:
5512 * - Always do this if we have been called to do formatting only.
5513 * - Always do this when 'formatoptions' has the 'a' flag and the line
5514 * ends in white space.
5515 * - Otherwise:
5516 * - Don't do this if inserting a blank
5517 * - Don't do this if an existing character is being replaced, unless
5518 * we're in VREPLACE mode.
5519 * - Do this if the cursor is not on the line where insert started
5520 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5521 * before the insert.
5522 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5523 * before 'textwidth'
5524 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005525 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 && ((flags & INSCHAR_FORMAT)
5527 || (!vim_iswhite(c)
5528 && !((State & REPLACE_FLAG)
5529#ifdef FEAT_VREPLACE
5530 && !(State & VREPLACE_FLAG)
5531#endif
5532 && *ml_get_cursor() != NUL)
5533 && (curwin->w_cursor.lnum != Insstart.lnum
5534 || ((!has_format_option(FO_INS_LONG)
5535 || Insstart_textlen <= (colnr_T)textwidth)
5536 && (!fo_ins_blank
5537 || Insstart_blank_vcol <= (colnr_T)textwidth
5538 ))))))
5539 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005540 /* Format with 'formatexpr' when it's set. Use internal formatting
5541 * when 'formatexpr' isn't set or it returns non-zero. */
5542#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005543 int do_internal = TRUE;
5544
Bram Moolenaar81a82092008-03-12 16:27:00 +00005545 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005546 {
5547 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5548 /* It may be required to save for undo again, e.g. when setline()
5549 * was called. */
5550 ins_need_undo = TRUE;
5551 }
5552 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005554 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (c == NUL) /* only formatting was wanted */
5558 return;
5559
5560#ifdef FEAT_COMMENTS
5561 /* Check whether this character should end a comment. */
5562 if (did_ai && (int)c == end_comment_pending)
5563 {
5564 char_u *line;
5565 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5566 int middle_len, end_len;
5567 int i;
5568
5569 /*
5570 * Need to remove existing (middle) comment leader and insert end
5571 * comment leader. First, check what comment leader we can find.
5572 */
5573 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5574 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5575 {
5576 /* Skip middle-comment string */
5577 while (*p && p[-1] != ':') /* find end of middle flags */
5578 ++p;
5579 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5580 /* Don't count trailing white space for middle_len */
5581 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5582 --middle_len;
5583
5584 /* Find the end-comment string */
5585 while (*p && p[-1] != ':') /* find end of end flags */
5586 ++p;
5587 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5588
5589 /* Skip white space before the cursor */
5590 i = curwin->w_cursor.col;
5591 while (--i >= 0 && vim_iswhite(line[i]))
5592 ;
5593 i++;
5594
5595 /* Skip to before the middle leader */
5596 i -= middle_len;
5597
5598 /* Check some expected things before we go on */
5599 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5600 {
5601 /* Backspace over all the stuff we want to replace */
5602 backspace_until_column(i);
5603
5604 /*
5605 * Insert the end-comment string, except for the last
5606 * character, which will get inserted as normal later.
5607 */
5608 ins_bytes_len(lead_end, end_len - 1);
5609 }
5610 }
5611 }
5612 end_comment_pending = NUL;
5613#endif
5614
5615 did_ai = FALSE;
5616#ifdef FEAT_SMARTINDENT
5617 did_si = FALSE;
5618 can_si = FALSE;
5619 can_si_back = FALSE;
5620#endif
5621
5622 /*
5623 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5624 * This speeds up normal text input considerably.
5625 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5626 * need to re-indent at a ':', or any other character (but not what
5627 * 'paste' is set)..
5628 */
5629#ifdef USE_ON_FLY_SCROLL
5630 dont_scroll = FALSE; /* allow scrolling here */
5631#endif
5632
5633 if ( !ISSPECIAL(c)
5634#ifdef FEAT_MBYTE
5635 && (!has_mbyte || (*mb_char2len)(c) == 1)
5636#endif
5637 && vpeekc() != NUL
5638 && !(State & REPLACE_FLAG)
5639#ifdef FEAT_CINDENT
5640 && !cindent_on()
5641#endif
5642#ifdef FEAT_RIGHTLEFT
5643 && !p_ri
5644#endif
5645 )
5646 {
5647#define INPUT_BUFLEN 100
5648 char_u buf[INPUT_BUFLEN + 1];
5649 int i;
5650 colnr_T virtcol = 0;
5651
5652 buf[0] = c;
5653 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005654 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 virtcol = get_nolist_virtcol();
5656 /*
5657 * Stop the string when:
5658 * - no more chars available
5659 * - finding a special character (command key)
5660 * - buffer is full
5661 * - running into the 'textwidth' boundary
5662 * - need to check for abbreviation: A non-word char after a word-char
5663 */
5664 while ( (c = vpeekc()) != NUL
5665 && !ISSPECIAL(c)
5666#ifdef FEAT_MBYTE
5667 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5668#endif
5669 && i < INPUT_BUFLEN
5670 && (textwidth == 0
5671 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5672 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5673 {
5674#ifdef FEAT_RIGHTLEFT
5675 c = vgetc();
5676 if (p_hkmap && KeyTyped)
5677 c = hkmap(c); /* Hebrew mode mapping */
5678# ifdef FEAT_FKMAP
5679 if (p_fkmap && KeyTyped)
5680 c = fkmap(c); /* Farsi mode mapping */
5681# endif
5682 buf[i++] = c;
5683#else
5684 buf[i++] = vgetc();
5685#endif
5686 }
5687
5688#ifdef FEAT_DIGRAPHS
5689 do_digraph(-1); /* clear digraphs */
5690 do_digraph(buf[i-1]); /* may be the start of a digraph */
5691#endif
5692 buf[i] = NUL;
5693 ins_str(buf);
5694 if (flags & INSCHAR_CTRLV)
5695 {
5696 redo_literal(*buf);
5697 i = 1;
5698 }
5699 else
5700 i = 0;
5701 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005702 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else
5705 {
5706#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005707 int cc;
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5710 {
5711 char_u buf[MB_MAXBYTES + 1];
5712
5713 (*mb_char2bytes)(c, buf);
5714 buf[cc] = NUL;
5715 ins_char_bytes(buf, cc);
5716 AppendCharToRedobuff(c);
5717 }
5718 else
5719#endif
5720 {
5721 ins_char(c);
5722 if (flags & INSCHAR_CTRLV)
5723 redo_literal(c);
5724 else
5725 AppendCharToRedobuff(c);
5726 }
5727 }
5728}
5729
5730/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005731 * Format text at the current insert position.
5732 */
5733 static void
5734internal_format(textwidth, second_indent, flags, format_only)
5735 int textwidth;
5736 int second_indent;
5737 int flags;
5738 int format_only;
5739{
5740 int cc;
5741 int save_char = NUL;
5742 int haveto_redraw = FALSE;
5743 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5744#ifdef FEAT_MBYTE
5745 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5746#endif
5747 int fo_white_par = has_format_option(FO_WHITE_PAR);
5748 int first_line = TRUE;
5749#ifdef FEAT_COMMENTS
5750 colnr_T leader_len;
5751 int no_leader = FALSE;
5752 int do_comments = (flags & INSCHAR_DO_COM);
5753#endif
5754
5755 /*
5756 * When 'ai' is off we don't want a space under the cursor to be
5757 * deleted. Replace it with an 'x' temporarily.
5758 */
5759 if (!curbuf->b_p_ai)
5760 {
5761 cc = gchar_cursor();
5762 if (vim_iswhite(cc))
5763 {
5764 save_char = cc;
5765 pchar_cursor('x');
5766 }
5767 }
5768
5769 /*
5770 * Repeat breaking lines, until the current line is not too long.
5771 */
5772 while (!got_int)
5773 {
5774 int startcol; /* Cursor column at entry */
5775 int wantcol; /* column at textwidth border */
5776 int foundcol; /* column for start of spaces */
5777 int end_foundcol = 0; /* column for start of word */
5778 colnr_T len;
5779 colnr_T virtcol;
5780#ifdef FEAT_VREPLACE
5781 int orig_col = 0;
5782 char_u *saved_text = NULL;
5783#endif
5784 colnr_T col;
5785
5786 virtcol = get_nolist_virtcol();
5787 if (virtcol < (colnr_T)textwidth)
5788 break;
5789
5790#ifdef FEAT_COMMENTS
5791 if (no_leader)
5792 do_comments = FALSE;
5793 else if (!(flags & INSCHAR_FORMAT)
5794 && has_format_option(FO_WRAP_COMS))
5795 do_comments = TRUE;
5796
5797 /* Don't break until after the comment leader */
5798 if (do_comments)
5799 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5800 else
5801 leader_len = 0;
5802
5803 /* If the line doesn't start with a comment leader, then don't
5804 * start one in a following broken line. Avoids that a %word
5805 * moved to the start of the next line causes all following lines
5806 * to start with %. */
5807 if (leader_len == 0)
5808 no_leader = TRUE;
5809#endif
5810 if (!(flags & INSCHAR_FORMAT)
5811#ifdef FEAT_COMMENTS
5812 && leader_len == 0
5813#endif
5814 && !has_format_option(FO_WRAP))
5815
5816 {
5817 textwidth = 0;
5818 break;
5819 }
5820 if ((startcol = curwin->w_cursor.col) == 0)
5821 break;
5822
5823 /* find column of textwidth border */
5824 coladvance((colnr_T)textwidth);
5825 wantcol = curwin->w_cursor.col;
5826
5827 curwin->w_cursor.col = startcol - 1;
5828#ifdef FEAT_MBYTE
5829 /* Correct cursor for multi-byte character. */
5830 if (has_mbyte)
5831 mb_adjust_cursor();
5832#endif
5833 foundcol = 0;
5834
5835 /*
5836 * Find position to break at.
5837 * Stop at first entered white when 'formatoptions' has 'v'
5838 */
5839 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5840 || curwin->w_cursor.lnum != Insstart.lnum
5841 || curwin->w_cursor.col >= Insstart.col)
5842 {
5843 cc = gchar_cursor();
5844 if (WHITECHAR(cc))
5845 {
5846 /* remember position of blank just before text */
5847 end_foundcol = curwin->w_cursor.col;
5848
5849 /* find start of sequence of blanks */
5850 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5851 {
5852 dec_cursor();
5853 cc = gchar_cursor();
5854 }
5855 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5856 break; /* only spaces in front of text */
5857#ifdef FEAT_COMMENTS
5858 /* Don't break until after the comment leader */
5859 if (curwin->w_cursor.col < leader_len)
5860 break;
5861#endif
5862 if (has_format_option(FO_ONE_LETTER))
5863 {
5864 /* do not break after one-letter words */
5865 if (curwin->w_cursor.col == 0)
5866 break; /* one-letter word at begin */
5867
5868 col = curwin->w_cursor.col;
5869 dec_cursor();
5870 cc = gchar_cursor();
5871
5872 if (WHITECHAR(cc))
5873 continue; /* one-letter, continue */
5874 curwin->w_cursor.col = col;
5875 }
5876#ifdef FEAT_MBYTE
5877 if (has_mbyte)
5878 foundcol = curwin->w_cursor.col
5879 + (*mb_ptr2len)(ml_get_cursor());
5880 else
5881#endif
5882 foundcol = curwin->w_cursor.col + 1;
5883 if (curwin->w_cursor.col < (colnr_T)wantcol)
5884 break;
5885 }
5886#ifdef FEAT_MBYTE
5887 else if (cc >= 0x100 && fo_multibyte
5888 && curwin->w_cursor.col <= (colnr_T)wantcol)
5889 {
5890 /* Break after or before a multi-byte character. */
5891 foundcol = curwin->w_cursor.col;
5892 if (curwin->w_cursor.col < (colnr_T)wantcol)
5893 foundcol += (*mb_char2len)(cc);
5894 end_foundcol = foundcol;
5895 break;
5896 }
5897#endif
5898 if (curwin->w_cursor.col == 0)
5899 break;
5900 dec_cursor();
5901 }
5902
5903 if (foundcol == 0) /* no spaces, cannot break line */
5904 {
5905 curwin->w_cursor.col = startcol;
5906 break;
5907 }
5908
5909 /* Going to break the line, remove any "$" now. */
5910 undisplay_dollar();
5911
5912 /*
5913 * Offset between cursor position and line break is used by replace
5914 * stack functions. VREPLACE does not use this, and backspaces
5915 * over the text instead.
5916 */
5917#ifdef FEAT_VREPLACE
5918 if (State & VREPLACE_FLAG)
5919 orig_col = startcol; /* Will start backspacing from here */
5920 else
5921#endif
5922 replace_offset = startcol - end_foundcol - 1;
5923
5924 /*
5925 * adjust startcol for spaces that will be deleted and
5926 * characters that will remain on top line
5927 */
5928 curwin->w_cursor.col = foundcol;
5929 while (cc = gchar_cursor(), WHITECHAR(cc))
5930 inc_cursor();
5931 startcol -= curwin->w_cursor.col;
5932 if (startcol < 0)
5933 startcol = 0;
5934
5935#ifdef FEAT_VREPLACE
5936 if (State & VREPLACE_FLAG)
5937 {
5938 /*
5939 * In VREPLACE mode, we will backspace over the text to be
5940 * wrapped, so save a copy now to put on the next line.
5941 */
5942 saved_text = vim_strsave(ml_get_cursor());
5943 curwin->w_cursor.col = orig_col;
5944 if (saved_text == NULL)
5945 break; /* Can't do it, out of memory */
5946 saved_text[startcol] = NUL;
5947
5948 /* Backspace over characters that will move to the next line */
5949 if (!fo_white_par)
5950 backspace_until_column(foundcol);
5951 }
5952 else
5953#endif
5954 {
5955 /* put cursor after pos. to break line */
5956 if (!fo_white_par)
5957 curwin->w_cursor.col = foundcol;
5958 }
5959
5960 /*
5961 * Split the line just before the margin.
5962 * Only insert/delete lines, but don't really redraw the window.
5963 */
5964 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5965 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5966#ifdef FEAT_COMMENTS
5967 + (do_comments ? OPENLINE_DO_COM : 0)
5968#endif
5969 , old_indent);
5970 old_indent = 0;
5971
5972 replace_offset = 0;
5973 if (first_line)
5974 {
5975 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5976 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5977 if (second_indent >= 0)
5978 {
5979#ifdef FEAT_VREPLACE
5980 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00005981 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982 else
5983#endif
5984 (void)set_indent(second_indent, SIN_CHANGED);
5985 }
5986 first_line = FALSE;
5987 }
5988
5989#ifdef FEAT_VREPLACE
5990 if (State & VREPLACE_FLAG)
5991 {
5992 /*
5993 * In VREPLACE mode we have backspaced over the text to be
5994 * moved, now we re-insert it into the new line.
5995 */
5996 ins_bytes(saved_text);
5997 vim_free(saved_text);
5998 }
5999 else
6000#endif
6001 {
6002 /*
6003 * Check if cursor is not past the NUL off the line, cindent
6004 * may have added or removed indent.
6005 */
6006 curwin->w_cursor.col += startcol;
6007 len = (colnr_T)STRLEN(ml_get_curline());
6008 if (curwin->w_cursor.col > len)
6009 curwin->w_cursor.col = len;
6010 }
6011
6012 haveto_redraw = TRUE;
6013#ifdef FEAT_CINDENT
6014 can_cindent = TRUE;
6015#endif
6016 /* moved the cursor, don't autoindent or cindent now */
6017 did_ai = FALSE;
6018#ifdef FEAT_SMARTINDENT
6019 did_si = FALSE;
6020 can_si = FALSE;
6021 can_si_back = FALSE;
6022#endif
6023 line_breakcheck();
6024 }
6025
6026 if (save_char != NUL) /* put back space after cursor */
6027 pchar_cursor(save_char);
6028
6029 if (!format_only && haveto_redraw)
6030 {
6031 update_topline();
6032 redraw_curbuf_later(VALID);
6033 }
6034}
6035
6036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 * Called after inserting or deleting text: When 'formatoptions' includes the
6038 * 'a' flag format from the current line until the end of the paragraph.
6039 * Keep the cursor at the same position relative to the text.
6040 * The caller must have saved the cursor line for undo, following ones will be
6041 * saved here.
6042 */
6043 void
6044auto_format(trailblank, prev_line)
6045 int trailblank; /* when TRUE also format with trailing blank */
6046 int prev_line; /* may start in previous line */
6047{
6048 pos_T pos;
6049 colnr_T len;
6050 char_u *old;
6051 char_u *new, *pnew;
6052 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006053 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
6055 if (!has_format_option(FO_AUTO))
6056 return;
6057
6058 pos = curwin->w_cursor;
6059 old = ml_get_curline();
6060
6061 /* may remove added space */
6062 check_auto_format(FALSE);
6063
6064 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6065 * user might insert normal text next. Also skip formatting when "1" is
6066 * in 'formatoptions' and there is a single character before the cursor.
6067 * Otherwise the line would be broken and when typing another non-white
6068 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006069 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (*old != NUL && !trailblank && wasatend)
6071 {
6072 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006073 cc = gchar_cursor();
6074 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6075 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006077 cc = gchar_cursor();
6078 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
6080 curwin->w_cursor = pos;
6081 return;
6082 }
6083 curwin->w_cursor = pos;
6084 }
6085
6086#ifdef FEAT_COMMENTS
6087 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6088 * comments. */
6089 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6090 && get_leader_len(old, NULL, FALSE) == 0)
6091 return;
6092#endif
6093
6094 /*
6095 * May start formatting in a previous line, so that after "x" a word is
6096 * moved to the previous line if it fits there now. Only when this is not
6097 * the start of a paragraph.
6098 */
6099 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6100 {
6101 --curwin->w_cursor.lnum;
6102 if (u_save_cursor() == FAIL)
6103 return;
6104 }
6105
6106 /*
6107 * Do the formatting and restore the cursor position. "saved_cursor" will
6108 * be adjusted for the text formatting.
6109 */
6110 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006111 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 curwin->w_cursor = saved_cursor;
6113 saved_cursor.lnum = 0;
6114
6115 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6116 {
6117 /* "cannot happen" */
6118 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6119 coladvance((colnr_T)MAXCOL);
6120 }
6121 else
6122 check_cursor_col();
6123
6124 /* Insert mode: If the cursor is now after the end of the line while it
6125 * previously wasn't, the line was broken. Because of the rule above we
6126 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6127 * formatted. */
6128 if (!wasatend && has_format_option(FO_WHITE_PAR))
6129 {
6130 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006131 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (curwin->w_cursor.col == len)
6133 {
6134 pnew = vim_strnsave(new, len + 2);
6135 pnew[len] = ' ';
6136 pnew[len + 1] = NUL;
6137 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6138 /* remove the space later */
6139 did_add_space = TRUE;
6140 }
6141 else
6142 /* may remove added space */
6143 check_auto_format(FALSE);
6144 }
6145
6146 check_cursor();
6147}
6148
6149/*
6150 * When an extra space was added to continue a paragraph for auto-formatting,
6151 * delete it now. The space must be under the cursor, just after the insert
6152 * position.
6153 */
6154 static void
6155check_auto_format(end_insert)
6156 int end_insert; /* TRUE when ending Insert mode */
6157{
6158 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006159 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 if (did_add_space)
6162 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006163 cc = gchar_cursor();
6164 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 /* Somehow the space was removed already. */
6166 did_add_space = FALSE;
6167 else
6168 {
6169 if (!end_insert)
6170 {
6171 inc_cursor();
6172 c = gchar_cursor();
6173 dec_cursor();
6174 }
6175 if (c != NUL)
6176 {
6177 /* The space is no longer at the end of the line, delete it. */
6178 del_char(FALSE);
6179 did_add_space = FALSE;
6180 }
6181 }
6182 }
6183}
6184
6185/*
6186 * Find out textwidth to be used for formatting:
6187 * if 'textwidth' option is set, use it
6188 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6189 * if invalid value, use 0.
6190 * Set default to window width (maximum 79) for "gq" operator.
6191 */
6192 int
6193comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006194 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 int textwidth;
6197
6198 textwidth = curbuf->b_p_tw;
6199 if (textwidth == 0 && curbuf->b_p_wm)
6200 {
6201 /* The width is the window width minus 'wrapmargin' minus all the
6202 * things that add to the margin. */
6203 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6204#ifdef FEAT_CMDWIN
6205 if (cmdwin_type != 0)
6206 textwidth -= 1;
6207#endif
6208#ifdef FEAT_FOLDING
6209 textwidth -= curwin->w_p_fdc;
6210#endif
6211#ifdef FEAT_SIGNS
6212 if (curwin->w_buffer->b_signlist != NULL
6213# ifdef FEAT_NETBEANS_INTG
6214 || usingNetbeans
6215# endif
6216 )
6217 textwidth -= 1;
6218#endif
6219 if (curwin->w_p_nu)
6220 textwidth -= 8;
6221 }
6222 if (textwidth < 0)
6223 textwidth = 0;
6224 if (ff && textwidth == 0)
6225 {
6226 textwidth = W_WIDTH(curwin) - 1;
6227 if (textwidth > 79)
6228 textwidth = 79;
6229 }
6230 return textwidth;
6231}
6232
6233/*
6234 * Put a character in the redo buffer, for when just after a CTRL-V.
6235 */
6236 static void
6237redo_literal(c)
6238 int c;
6239{
6240 char_u buf[10];
6241
6242 /* Only digits need special treatment. Translate them into a string of
6243 * three digits. */
6244 if (VIM_ISDIGIT(c))
6245 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006246 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 AppendToRedobuff(buf);
6248 }
6249 else
6250 AppendCharToRedobuff(c);
6251}
6252
6253/*
6254 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006255 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 */
6257 static void
6258start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006259 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 if (!arrow_used) /* something has been inserted */
6262 {
6263 AppendToRedobuff(ESC_STR);
6264 stop_insert(end_insert_pos, FALSE);
6265 arrow_used = TRUE; /* this means we stopped the current insert */
6266 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006267#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006268 check_spell_redraw();
6269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006272#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006273/*
6274 * If we skipped highlighting word at cursor, do it now.
6275 * It may be skipped again, thus reset spell_redraw_lnum first.
6276 */
6277 static void
6278check_spell_redraw()
6279{
6280 if (spell_redraw_lnum != 0)
6281 {
6282 linenr_T lnum = spell_redraw_lnum;
6283
6284 spell_redraw_lnum = 0;
6285 redrawWinline(lnum, FALSE);
6286 }
6287}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006288
6289/*
6290 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6291 * spelled word, if there is one.
6292 */
6293 static void
6294spell_back_to_badword()
6295{
6296 pos_T tpos = curwin->w_cursor;
6297
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006298 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006299 if (curwin->w_cursor.col != tpos.col)
6300 start_arrow(&tpos);
6301}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006302#endif
6303
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304/*
6305 * stop_arrow() is called before a change is made in insert mode.
6306 * If an arrow key has been used, start a new insertion.
6307 * Returns FAIL if undo is impossible, shouldn't insert then.
6308 */
6309 int
6310stop_arrow()
6311{
6312 if (arrow_used)
6313 {
6314 if (u_save_cursor() == OK)
6315 {
6316 arrow_used = FALSE;
6317 ins_need_undo = FALSE;
6318 }
6319 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006320 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 ai_col = 0;
6322#ifdef FEAT_VREPLACE
6323 if (State & VREPLACE_FLAG)
6324 {
6325 orig_line_count = curbuf->b_ml.ml_line_count;
6326 vr_lines_changed = 1;
6327 }
6328#endif
6329 ResetRedobuff();
6330 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006331 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333 else if (ins_need_undo)
6334 {
6335 if (u_save_cursor() == OK)
6336 ins_need_undo = FALSE;
6337 }
6338
6339#ifdef FEAT_FOLDING
6340 /* Always open fold at the cursor line when inserting something. */
6341 foldOpenCursor();
6342#endif
6343
6344 return (arrow_used || ins_need_undo ? FAIL : OK);
6345}
6346
6347/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006348 * Do a few things to stop inserting.
6349 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6350 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 */
6352 static void
6353stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006354 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006355 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006357 int cc;
6358 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 stop_redo_ins();
6361 replace_flush(); /* abandon replace stack */
6362
6363 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006364 * Save the inserted text for later redo with ^@ and CTRL-A.
6365 * Don't do it when "restart_edit" was set and nothing was inserted,
6366 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006368 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006369 if (did_restart_edit == 0 || (ptr != NULL
6370 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006371 {
6372 vim_free(last_insert);
6373 last_insert = ptr;
6374 last_insert_skip = new_insert_skip;
6375 }
6376 else
6377 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006379 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 /* Auto-format now. It may seem strange to do this when stopping an
6382 * insertion (or moving the cursor), but it's required when appending
6383 * a line and having it end in a space. But only do it when something
6384 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006385 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006387 pos_T tpos = curwin->w_cursor;
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 /* When the cursor is at the end of the line after a space the
6390 * formatting will move it to the following word. Avoid that by
6391 * moving the cursor onto the space. */
6392 cc = 'x';
6393 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6394 {
6395 dec_cursor();
6396 cc = gchar_cursor();
6397 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006398 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400
6401 auto_format(TRUE, FALSE);
6402
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006403 if (vim_iswhite(cc))
6404 {
6405 if (gchar_cursor() != NUL)
6406 inc_cursor();
6407#ifdef FEAT_VIRTUALEDIT
6408 /* If the cursor is still at the same character, also keep
6409 * the "coladd". */
6410 if (gchar_cursor() == NUL
6411 && curwin->w_cursor.lnum == tpos.lnum
6412 && curwin->w_cursor.col == tpos.col)
6413 curwin->w_cursor.coladd = tpos.coladd;
6414#endif
6415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417
6418 /* If a space was inserted for auto-formatting, remove it now. */
6419 check_auto_format(TRUE);
6420
6421 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006422 * of the line, and put the cursor back.
6423 * Do this when ESC was used or moving the cursor up/down. */
6424 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006425 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006427 pos_T tpos = curwin->w_cursor;
6428
6429 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006430 for (;;)
6431 {
6432 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6433 --curwin->w_cursor.col;
6434 cc = gchar_cursor();
6435 if (!vim_iswhite(cc))
6436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006438 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006439 if (curwin->w_cursor.lnum != tpos.lnum)
6440 curwin->w_cursor = tpos;
6441 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6443
6444#ifdef FEAT_VISUAL
6445 /* <C-S-Right> may have started Visual mode, adjust the position for
6446 * deleted characters. */
6447 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6448 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006449 int len = (int)STRLEN(ml_get_curline());
6450
6451 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006453 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454# ifdef FEAT_VIRTUALEDIT
6455 VIsual.coladd = 0;
6456# endif
6457 }
6458 }
6459#endif
6460 }
6461 }
6462 did_ai = FALSE;
6463#ifdef FEAT_SMARTINDENT
6464 did_si = FALSE;
6465 can_si = FALSE;
6466 can_si_back = FALSE;
6467#endif
6468
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006469 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6470 * now in a different buffer. */
6471 if (end_insert_pos != NULL)
6472 {
6473 curbuf->b_op_start = Insstart;
6474 curbuf->b_op_end = *end_insert_pos;
6475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
6477
6478/*
6479 * Set the last inserted text to a single character.
6480 * Used for the replace command.
6481 */
6482 void
6483set_last_insert(c)
6484 int c;
6485{
6486 char_u *s;
6487
6488 vim_free(last_insert);
6489#ifdef FEAT_MBYTE
6490 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6491#else
6492 last_insert = alloc(6);
6493#endif
6494 if (last_insert != NULL)
6495 {
6496 s = last_insert;
6497 /* Use the CTRL-V only when entering a special char */
6498 if (c < ' ' || c == DEL)
6499 *s++ = Ctrl_V;
6500 s = add_char2buf(c, s);
6501 *s++ = ESC;
6502 *s++ = NUL;
6503 last_insert_skip = 0;
6504 }
6505}
6506
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006507#if defined(EXITFREE) || defined(PROTO)
6508 void
6509free_last_insert()
6510{
6511 vim_free(last_insert);
6512 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006513# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006514 vim_free(compl_orig_text);
6515 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006516# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006517}
6518#endif
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520/*
6521 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6522 * and CSI. Handle multi-byte characters.
6523 * Returns a pointer to after the added bytes.
6524 */
6525 char_u *
6526add_char2buf(c, s)
6527 int c;
6528 char_u *s;
6529{
6530#ifdef FEAT_MBYTE
6531 char_u temp[MB_MAXBYTES];
6532 int i;
6533 int len;
6534
6535 len = (*mb_char2bytes)(c, temp);
6536 for (i = 0; i < len; ++i)
6537 {
6538 c = temp[i];
6539#endif
6540 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6541 if (c == K_SPECIAL)
6542 {
6543 *s++ = K_SPECIAL;
6544 *s++ = KS_SPECIAL;
6545 *s++ = KE_FILLER;
6546 }
6547#ifdef FEAT_GUI
6548 else if (c == CSI)
6549 {
6550 *s++ = CSI;
6551 *s++ = KS_EXTRA;
6552 *s++ = (int)KE_CSI;
6553 }
6554#endif
6555 else
6556 *s++ = c;
6557#ifdef FEAT_MBYTE
6558 }
6559#endif
6560 return s;
6561}
6562
6563/*
6564 * move cursor to start of line
6565 * if flags & BL_WHITE move to first non-white
6566 * if flags & BL_SOL move to first non-white if startofline is set,
6567 * otherwise keep "curswant" column
6568 * if flags & BL_FIX don't leave the cursor on a NUL.
6569 */
6570 void
6571beginline(flags)
6572 int flags;
6573{
6574 if ((flags & BL_SOL) && !p_sol)
6575 coladvance(curwin->w_curswant);
6576 else
6577 {
6578 curwin->w_cursor.col = 0;
6579#ifdef FEAT_VIRTUALEDIT
6580 curwin->w_cursor.coladd = 0;
6581#endif
6582
6583 if (flags & (BL_WHITE | BL_SOL))
6584 {
6585 char_u *ptr;
6586
6587 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6588 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6589 ++curwin->w_cursor.col;
6590 }
6591 curwin->w_set_curswant = TRUE;
6592 }
6593}
6594
6595/*
6596 * oneright oneleft cursor_down cursor_up
6597 *
6598 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006599 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 * Return OK when successful, FAIL when we hit a line of file boundary.
6601 */
6602
6603 int
6604oneright()
6605{
6606 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
6609#ifdef FEAT_VIRTUALEDIT
6610 if (virtual_active())
6611 {
6612 pos_T prevpos = curwin->w_cursor;
6613
6614 /* Adjust for multi-wide char (excluding TAB) */
6615 ptr = ml_get_cursor();
6616 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006617# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006619# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 ))
6623 ? ptr2cells(ptr) : 1));
6624 curwin->w_set_curswant = TRUE;
6625 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6626 return (prevpos.col != curwin->w_cursor.col
6627 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6628 }
6629#endif
6630
6631 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006632 if (*ptr == NUL)
6633 return FAIL; /* already at the very end */
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006636 if (has_mbyte)
6637 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 else
6639#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006640 l = 1;
6641
6642 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6643 * contains "onemore". */
6644 if (ptr[l] == NUL
6645#ifdef FEAT_VIRTUALEDIT
6646 && (ve_flags & VE_ONEMORE) == 0
6647#endif
6648 )
6649 return FAIL;
6650 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 curwin->w_set_curswant = TRUE;
6653 return OK;
6654}
6655
6656 int
6657oneleft()
6658{
6659#ifdef FEAT_VIRTUALEDIT
6660 if (virtual_active())
6661 {
6662 int width;
6663 int v = getviscol();
6664
6665 if (v == 0)
6666 return FAIL;
6667
6668# ifdef FEAT_LINEBREAK
6669 /* We might get stuck on 'showbreak', skip over it. */
6670 width = 1;
6671 for (;;)
6672 {
6673 coladvance(v - width);
6674 /* getviscol() is slow, skip it when 'showbreak' is empty and
6675 * there are no multi-byte characters */
6676 if ((*p_sbr == NUL
6677# ifdef FEAT_MBYTE
6678 && !has_mbyte
6679# endif
6680 ) || getviscol() < v)
6681 break;
6682 ++width;
6683 }
6684# else
6685 coladvance(v - 1);
6686# endif
6687
6688 if (curwin->w_cursor.coladd == 1)
6689 {
6690 char_u *ptr;
6691
6692 /* Adjust for multi-wide char (not a TAB) */
6693 ptr = ml_get_cursor();
6694 if (*ptr != TAB && vim_isprintc(
6695# ifdef FEAT_MBYTE
6696 (*mb_ptr2char)(ptr)
6697# else
6698 *ptr
6699# endif
6700 ) && ptr2cells(ptr) > 1)
6701 curwin->w_cursor.coladd = 0;
6702 }
6703
6704 curwin->w_set_curswant = TRUE;
6705 return OK;
6706 }
6707#endif
6708
6709 if (curwin->w_cursor.col == 0)
6710 return FAIL;
6711
6712 curwin->w_set_curswant = TRUE;
6713 --curwin->w_cursor.col;
6714
6715#ifdef FEAT_MBYTE
6716 /* if the character on the left of the current cursor is a multi-byte
6717 * character, move to its first byte */
6718 if (has_mbyte)
6719 mb_adjust_cursor();
6720#endif
6721 return OK;
6722}
6723
6724 int
6725cursor_up(n, upd_topline)
6726 long n;
6727 int upd_topline; /* When TRUE: update topline */
6728{
6729 linenr_T lnum;
6730
6731 if (n > 0)
6732 {
6733 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006734 /* This fails if the cursor is already in the first line or the count
6735 * is larger than the line number and '-' is in 'cpoptions' */
6736 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 return FAIL;
6738 if (n >= lnum)
6739 lnum = 1;
6740 else
6741#ifdef FEAT_FOLDING
6742 if (hasAnyFolding(curwin))
6743 {
6744 /*
6745 * Count each sequence of folded lines as one logical line.
6746 */
6747 /* go to the the start of the current fold */
6748 (void)hasFolding(lnum, &lnum, NULL);
6749
6750 while (n--)
6751 {
6752 /* move up one line */
6753 --lnum;
6754 if (lnum <= 1)
6755 break;
6756 /* If we entered a fold, move to the beginning, unless in
6757 * Insert mode or when 'foldopen' contains "all": it will open
6758 * in a moment. */
6759 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6760 (void)hasFolding(lnum, &lnum, NULL);
6761 }
6762 if (lnum < 1)
6763 lnum = 1;
6764 }
6765 else
6766#endif
6767 lnum -= n;
6768 curwin->w_cursor.lnum = lnum;
6769 }
6770
6771 /* try to advance to the column we want to be at */
6772 coladvance(curwin->w_curswant);
6773
6774 if (upd_topline)
6775 update_topline(); /* make sure curwin->w_topline is valid */
6776
6777 return OK;
6778}
6779
6780/*
6781 * Cursor down a number of logical lines.
6782 */
6783 int
6784cursor_down(n, upd_topline)
6785 long n;
6786 int upd_topline; /* When TRUE: update topline */
6787{
6788 linenr_T lnum;
6789
6790 if (n > 0)
6791 {
6792 lnum = curwin->w_cursor.lnum;
6793#ifdef FEAT_FOLDING
6794 /* Move to last line of fold, will fail if it's the end-of-file. */
6795 (void)hasFolding(lnum, NULL, &lnum);
6796#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006797 /* This fails if the cursor is already in the last line or would move
6798 * beyound the last line and '-' is in 'cpoptions' */
6799 if (lnum >= curbuf->b_ml.ml_line_count
6800 || (lnum + n > curbuf->b_ml.ml_line_count
6801 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 return FAIL;
6803 if (lnum + n >= curbuf->b_ml.ml_line_count)
6804 lnum = curbuf->b_ml.ml_line_count;
6805 else
6806#ifdef FEAT_FOLDING
6807 if (hasAnyFolding(curwin))
6808 {
6809 linenr_T last;
6810
6811 /* count each sequence of folded lines as one logical line */
6812 while (n--)
6813 {
6814 if (hasFolding(lnum, NULL, &last))
6815 lnum = last + 1;
6816 else
6817 ++lnum;
6818 if (lnum >= curbuf->b_ml.ml_line_count)
6819 break;
6820 }
6821 if (lnum > curbuf->b_ml.ml_line_count)
6822 lnum = curbuf->b_ml.ml_line_count;
6823 }
6824 else
6825#endif
6826 lnum += n;
6827 curwin->w_cursor.lnum = lnum;
6828 }
6829
6830 /* try to advance to the column we want to be at */
6831 coladvance(curwin->w_curswant);
6832
6833 if (upd_topline)
6834 update_topline(); /* make sure curwin->w_topline is valid */
6835
6836 return OK;
6837}
6838
6839/*
6840 * Stuff the last inserted text in the read buffer.
6841 * Last_insert actually is a copy of the redo buffer, so we
6842 * first have to remove the command.
6843 */
6844 int
6845stuff_inserted(c, count, no_esc)
6846 int c; /* Command character to be inserted */
6847 long count; /* Repeat this many times */
6848 int no_esc; /* Don't add an ESC at the end */
6849{
6850 char_u *esc_ptr;
6851 char_u *ptr;
6852 char_u *last_ptr;
6853 char_u last = NUL;
6854
6855 ptr = get_last_insert();
6856 if (ptr == NULL)
6857 {
6858 EMSG(_(e_noinstext));
6859 return FAIL;
6860 }
6861
6862 /* may want to stuff the command character, to start Insert mode */
6863 if (c != NUL)
6864 stuffcharReadbuff(c);
6865 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6866 *esc_ptr = NUL; /* remove the ESC */
6867
6868 /* when the last char is either "0" or "^" it will be quoted if no ESC
6869 * comes after it OR if it will inserted more than once and "ptr"
6870 * starts with ^D. -- Acevedo
6871 */
6872 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6873 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6874 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6875 {
6876 last = *last_ptr;
6877 *last_ptr = NUL;
6878 }
6879
6880 do
6881 {
6882 stuffReadbuff(ptr);
6883 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6884 if (last)
6885 stuffReadbuff((char_u *)(last == '0'
6886 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6887 : IF_EB("\026^", CTRL_V_STR "^")));
6888 }
6889 while (--count > 0);
6890
6891 if (last)
6892 *last_ptr = last;
6893
6894 if (esc_ptr != NULL)
6895 *esc_ptr = ESC; /* put the ESC back */
6896
6897 /* may want to stuff a trailing ESC, to get out of Insert mode */
6898 if (!no_esc)
6899 stuffcharReadbuff(ESC);
6900
6901 return OK;
6902}
6903
6904 char_u *
6905get_last_insert()
6906{
6907 if (last_insert == NULL)
6908 return NULL;
6909 return last_insert + last_insert_skip;
6910}
6911
6912/*
6913 * Get last inserted string, and remove trailing <Esc>.
6914 * Returns pointer to allocated memory (must be freed) or NULL.
6915 */
6916 char_u *
6917get_last_insert_save()
6918{
6919 char_u *s;
6920 int len;
6921
6922 if (last_insert == NULL)
6923 return NULL;
6924 s = vim_strsave(last_insert + last_insert_skip);
6925 if (s != NULL)
6926 {
6927 len = (int)STRLEN(s);
6928 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6929 s[len - 1] = NUL;
6930 }
6931 return s;
6932}
6933
6934/*
6935 * Check the word in front of the cursor for an abbreviation.
6936 * Called when the non-id character "c" has been entered.
6937 * When an abbreviation is recognized it is removed from the text and
6938 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6939 */
6940 static int
6941echeck_abbr(c)
6942 int c;
6943{
6944 /* Don't check for abbreviation in paste mode, when disabled and just
6945 * after moving around with cursor keys. */
6946 if (p_paste || no_abbr || arrow_used)
6947 return FALSE;
6948
6949 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6950 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6951}
6952
6953/*
6954 * replace-stack functions
6955 *
6956 * When replacing characters, the replaced characters are remembered for each
6957 * new character. This is used to re-insert the old text when backspacing.
6958 *
6959 * There is a NUL headed list of characters for each character that is
6960 * currently in the file after the insertion point. When BS is used, one NUL
6961 * headed list is put back for the deleted character.
6962 *
6963 * For a newline, there are two NUL headed lists. One contains the characters
6964 * that the NL replaced. The extra one stores the characters after the cursor
6965 * that were deleted (always white space).
6966 *
6967 * Replace_offset is normally 0, in which case replace_push will add a new
6968 * character at the end of the stack. If replace_offset is not 0, that many
6969 * characters will be left on the stack above the newly inserted character.
6970 */
6971
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006972static char_u *replace_stack = NULL;
6973static long replace_stack_nr = 0; /* next entry in replace stack */
6974static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976 void
6977replace_push(c)
6978 int c; /* character that is replaced (NUL is none) */
6979{
6980 char_u *p;
6981
6982 if (replace_stack_nr < replace_offset) /* nothing to do */
6983 return;
6984 if (replace_stack_len <= replace_stack_nr)
6985 {
6986 replace_stack_len += 50;
6987 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6988 if (p == NULL) /* out of memory */
6989 {
6990 replace_stack_len -= 50;
6991 return;
6992 }
6993 if (replace_stack != NULL)
6994 {
6995 mch_memmove(p, replace_stack,
6996 (size_t)(replace_stack_nr * sizeof(char_u)));
6997 vim_free(replace_stack);
6998 }
6999 replace_stack = p;
7000 }
7001 p = replace_stack + replace_stack_nr - replace_offset;
7002 if (replace_offset)
7003 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7004 *p = c;
7005 ++replace_stack_nr;
7006}
7007
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007008#if defined(FEAT_MBYTE) || defined(PROTO)
7009/*
7010 * Push a character onto the replace stack. Handles a multi-byte character in
7011 * reverse byte order, so that the first byte is popped off first.
7012 * Return the number of bytes done (includes composing characters).
7013 */
7014 int
7015replace_push_mb(p)
7016 char_u *p;
7017{
7018 int l = (*mb_ptr2len)(p);
7019 int j;
7020
7021 for (j = l - 1; j >= 0; --j)
7022 replace_push(p[j]);
7023 return l;
7024}
7025#endif
7026
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007027#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028/*
7029 * call replace_push(c) with replace_offset set to the first NUL.
7030 */
7031 static void
7032replace_push_off(c)
7033 int c;
7034{
7035 char_u *p;
7036
7037 p = replace_stack + replace_stack_nr;
7038 for (replace_offset = 1; replace_offset < replace_stack_nr;
7039 ++replace_offset)
7040 if (*--p == NUL)
7041 break;
7042 replace_push(c);
7043 replace_offset = 0;
7044}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
7047/*
7048 * Pop one item from the replace stack.
7049 * return -1 if stack empty
7050 * return replaced character or NUL otherwise
7051 */
7052 static int
7053replace_pop()
7054{
7055 if (replace_stack_nr == 0)
7056 return -1;
7057 return (int)replace_stack[--replace_stack_nr];
7058}
7059
7060/*
7061 * Join the top two items on the replace stack. This removes to "off"'th NUL
7062 * encountered.
7063 */
7064 static void
7065replace_join(off)
7066 int off; /* offset for which NUL to remove */
7067{
7068 int i;
7069
7070 for (i = replace_stack_nr; --i >= 0; )
7071 if (replace_stack[i] == NUL && off-- <= 0)
7072 {
7073 --replace_stack_nr;
7074 mch_memmove(replace_stack + i, replace_stack + i + 1,
7075 (size_t)(replace_stack_nr - i));
7076 return;
7077 }
7078}
7079
7080/*
7081 * Pop bytes from the replace stack until a NUL is found, and insert them
7082 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7083 */
7084 static void
7085replace_pop_ins()
7086{
7087 int cc;
7088 int oldState = State;
7089
7090 State = NORMAL; /* don't want REPLACE here */
7091 while ((cc = replace_pop()) > 0)
7092 {
7093#ifdef FEAT_MBYTE
7094 mb_replace_pop_ins(cc);
7095#else
7096 ins_char(cc);
7097#endif
7098 dec_cursor();
7099 }
7100 State = oldState;
7101}
7102
7103#ifdef FEAT_MBYTE
7104/*
7105 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7106 * indicates a multi-byte char, pop the other bytes too.
7107 */
7108 static void
7109mb_replace_pop_ins(cc)
7110 int cc;
7111{
7112 int n;
7113 char_u buf[MB_MAXBYTES];
7114 int i;
7115 int c;
7116
7117 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7118 {
7119 buf[0] = cc;
7120 for (i = 1; i < n; ++i)
7121 buf[i] = replace_pop();
7122 ins_bytes_len(buf, n);
7123 }
7124 else
7125 ins_char(cc);
7126
7127 if (enc_utf8)
7128 /* Handle composing chars. */
7129 for (;;)
7130 {
7131 c = replace_pop();
7132 if (c == -1) /* stack empty */
7133 break;
7134 if ((n = MB_BYTE2LEN(c)) == 1)
7135 {
7136 /* Not a multi-byte char, put it back. */
7137 replace_push(c);
7138 break;
7139 }
7140 else
7141 {
7142 buf[0] = c;
7143 for (i = 1; i < n; ++i)
7144 buf[i] = replace_pop();
7145 if (utf_iscomposing(utf_ptr2char(buf)))
7146 ins_bytes_len(buf, n);
7147 else
7148 {
7149 /* Not a composing char, put it back. */
7150 for (i = n - 1; i >= 0; --i)
7151 replace_push(buf[i]);
7152 break;
7153 }
7154 }
7155 }
7156}
7157#endif
7158
7159/*
7160 * make the replace stack empty
7161 * (called when exiting replace mode)
7162 */
7163 static void
7164replace_flush()
7165{
7166 vim_free(replace_stack);
7167 replace_stack = NULL;
7168 replace_stack_len = 0;
7169 replace_stack_nr = 0;
7170}
7171
7172/*
7173 * Handle doing a BS for one character.
7174 * cc < 0: replace stack empty, just move cursor
7175 * cc == 0: character was inserted, delete it
7176 * cc > 0: character was replaced, put cc (first byte of original char) back
7177 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007178 * When "limit_col" is >= 0, don't delete before this column. Matters when
7179 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 */
7181 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007182replace_do_bs(limit_col)
7183 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 int cc;
7186#ifdef FEAT_VREPLACE
7187 int orig_len = 0;
7188 int ins_len;
7189 int orig_vcols = 0;
7190 colnr_T start_vcol;
7191 char_u *p;
7192 int i;
7193 int vcol;
7194#endif
7195
7196 cc = replace_pop();
7197 if (cc > 0)
7198 {
7199#ifdef FEAT_VREPLACE
7200 if (State & VREPLACE_FLAG)
7201 {
7202 /* Get the number of screen cells used by the character we are
7203 * going to delete. */
7204 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7205 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7206 }
7207#endif
7208#ifdef FEAT_MBYTE
7209 if (has_mbyte)
7210 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007211 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212# ifdef FEAT_VREPLACE
7213 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007214 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215# endif
7216 replace_push(cc);
7217 }
7218 else
7219#endif
7220 {
7221 pchar_cursor(cc);
7222#ifdef FEAT_VREPLACE
7223 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007224 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225#endif
7226 }
7227 replace_pop_ins();
7228
7229#ifdef FEAT_VREPLACE
7230 if (State & VREPLACE_FLAG)
7231 {
7232 /* Get the number of screen cells used by the inserted characters */
7233 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007234 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 vcol = start_vcol;
7236 for (i = 0; i < ins_len; ++i)
7237 {
7238 vcol += chartabsize(p + i, vcol);
7239#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007240 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241#endif
7242 }
7243 vcol -= start_vcol;
7244
7245 /* Delete spaces that were inserted after the cursor to keep the
7246 * text aligned. */
7247 curwin->w_cursor.col += ins_len;
7248 while (vcol > orig_vcols && gchar_cursor() == ' ')
7249 {
7250 del_char(FALSE);
7251 ++orig_vcols;
7252 }
7253 curwin->w_cursor.col -= ins_len;
7254 }
7255#endif
7256
7257 /* mark the buffer as changed and prepare for displaying */
7258 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7259 }
7260 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007261 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262}
7263
7264#ifdef FEAT_CINDENT
7265/*
7266 * Return TRUE if C-indenting is on.
7267 */
7268 static int
7269cindent_on()
7270{
7271 return (!p_paste && (curbuf->b_p_cin
7272# ifdef FEAT_EVAL
7273 || *curbuf->b_p_inde != NUL
7274# endif
7275 ));
7276}
7277#endif
7278
7279#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7280/*
7281 * Re-indent the current line, based on the current contents of it and the
7282 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7283 * confused what all the part that handles Control-T is doing that I'm not.
7284 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7285 */
7286
7287 void
7288fixthisline(get_the_indent)
7289 int (*get_the_indent) __ARGS((void));
7290{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007291 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 if (linewhite(curwin->w_cursor.lnum))
7293 did_ai = TRUE; /* delete the indent if the line stays empty */
7294}
7295
7296 void
7297fix_indent()
7298{
7299 if (p_paste)
7300 return;
7301# ifdef FEAT_LISP
7302 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7303 fixthisline(get_lisp_indent);
7304# endif
7305# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7306 else
7307# endif
7308# ifdef FEAT_CINDENT
7309 if (cindent_on())
7310 do_c_expr_indent();
7311# endif
7312}
7313
7314#endif
7315
7316#ifdef FEAT_CINDENT
7317/*
7318 * return TRUE if 'cinkeys' contains the key "keytyped",
7319 * when == '*': Only if key is preceded with '*' (indent before insert)
7320 * when == '!': Only if key is prededed with '!' (don't insert)
7321 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7322 *
7323 * "keytyped" can have a few special values:
7324 * KEY_OPEN_FORW
7325 * KEY_OPEN_BACK
7326 * KEY_COMPLETE just finished completion.
7327 *
7328 * If line_is_empty is TRUE accept keys with '0' before them.
7329 */
7330 int
7331in_cinkeys(keytyped, when, line_is_empty)
7332 int keytyped;
7333 int when;
7334 int line_is_empty;
7335{
7336 char_u *look;
7337 int try_match;
7338 int try_match_word;
7339 char_u *p;
7340 char_u *line;
7341 int icase;
7342 int i;
7343
7344#ifdef FEAT_EVAL
7345 if (*curbuf->b_p_inde != NUL)
7346 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7347 else
7348#endif
7349 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7350 while (*look)
7351 {
7352 /*
7353 * Find out if we want to try a match with this key, depending on
7354 * 'when' and a '*' or '!' before the key.
7355 */
7356 switch (when)
7357 {
7358 case '*': try_match = (*look == '*'); break;
7359 case '!': try_match = (*look == '!'); break;
7360 default: try_match = (*look != '*'); break;
7361 }
7362 if (*look == '*' || *look == '!')
7363 ++look;
7364
7365 /*
7366 * If there is a '0', only accept a match if the line is empty.
7367 * But may still match when typing last char of a word.
7368 */
7369 if (*look == '0')
7370 {
7371 try_match_word = try_match;
7372 if (!line_is_empty)
7373 try_match = FALSE;
7374 ++look;
7375 }
7376 else
7377 try_match_word = FALSE;
7378
7379 /*
7380 * does it look like a control character?
7381 */
7382 if (*look == '^'
7383#ifdef EBCDIC
7384 && (Ctrl_chr(look[1]) != 0)
7385#else
7386 && look[1] >= '?' && look[1] <= '_'
7387#endif
7388 )
7389 {
7390 if (try_match && keytyped == Ctrl_chr(look[1]))
7391 return TRUE;
7392 look += 2;
7393 }
7394 /*
7395 * 'o' means "o" command, open forward.
7396 * 'O' means "O" command, open backward.
7397 */
7398 else if (*look == 'o')
7399 {
7400 if (try_match && keytyped == KEY_OPEN_FORW)
7401 return TRUE;
7402 ++look;
7403 }
7404 else if (*look == 'O')
7405 {
7406 if (try_match && keytyped == KEY_OPEN_BACK)
7407 return TRUE;
7408 ++look;
7409 }
7410
7411 /*
7412 * 'e' means to check for "else" at start of line and just before the
7413 * cursor.
7414 */
7415 else if (*look == 'e')
7416 {
7417 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7418 {
7419 p = ml_get_curline();
7420 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7421 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7422 return TRUE;
7423 }
7424 ++look;
7425 }
7426
7427 /*
7428 * ':' only causes an indent if it is at the end of a label or case
7429 * statement, or when it was before typing the ':' (to fix
7430 * class::method for C++).
7431 */
7432 else if (*look == ':')
7433 {
7434 if (try_match && keytyped == ':')
7435 {
7436 p = ml_get_curline();
7437 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7438 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007439 /* Need to get the line again after cin_islabel(). */
7440 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (curwin->w_cursor.col > 2
7442 && p[curwin->w_cursor.col - 1] == ':'
7443 && p[curwin->w_cursor.col - 2] == ':')
7444 {
7445 p[curwin->w_cursor.col - 1] = ' ';
7446 i = (cin_iscase(p) || cin_isscopedecl(p)
7447 || cin_islabel(30));
7448 p = ml_get_curline();
7449 p[curwin->w_cursor.col - 1] = ':';
7450 if (i)
7451 return TRUE;
7452 }
7453 }
7454 ++look;
7455 }
7456
7457
7458 /*
7459 * Is it a key in <>, maybe?
7460 */
7461 else if (*look == '<')
7462 {
7463 if (try_match)
7464 {
7465 /*
7466 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7467 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7468 * >, *, : and ! keys if they really really want to.
7469 */
7470 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7471 && keytyped == look[1])
7472 return TRUE;
7473
7474 if (keytyped == get_special_key_code(look + 1))
7475 return TRUE;
7476 }
7477 while (*look && *look != '>')
7478 look++;
7479 while (*look == '>')
7480 look++;
7481 }
7482
7483 /*
7484 * Is it a word: "=word"?
7485 */
7486 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7487 {
7488 ++look;
7489 if (*look == '~')
7490 {
7491 icase = TRUE;
7492 ++look;
7493 }
7494 else
7495 icase = FALSE;
7496 p = vim_strchr(look, ',');
7497 if (p == NULL)
7498 p = look + STRLEN(look);
7499 if ((try_match || try_match_word)
7500 && curwin->w_cursor.col >= (colnr_T)(p - look))
7501 {
7502 int match = FALSE;
7503
7504#ifdef FEAT_INS_EXPAND
7505 if (keytyped == KEY_COMPLETE)
7506 {
7507 char_u *s;
7508
7509 /* Just completed a word, check if it starts with "look".
7510 * search back for the start of a word. */
7511 line = ml_get_curline();
7512# ifdef FEAT_MBYTE
7513 if (has_mbyte)
7514 {
7515 char_u *n;
7516
7517 for (s = line + curwin->w_cursor.col; s > line; s = n)
7518 {
7519 n = mb_prevptr(line, s);
7520 if (!vim_iswordp(n))
7521 break;
7522 }
7523 }
7524 else
7525# endif
7526 for (s = line + curwin->w_cursor.col; s > line; --s)
7527 if (!vim_iswordc(s[-1]))
7528 break;
7529 if (s + (p - look) <= line + curwin->w_cursor.col
7530 && (icase
7531 ? MB_STRNICMP(s, look, p - look)
7532 : STRNCMP(s, look, p - look)) == 0)
7533 match = TRUE;
7534 }
7535 else
7536#endif
7537 /* TODO: multi-byte */
7538 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7539 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7540 {
7541 line = ml_get_cursor();
7542 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7543 || !vim_iswordc(line[-(p - look) - 1]))
7544 && (icase
7545 ? MB_STRNICMP(line - (p - look), look, p - look)
7546 : STRNCMP(line - (p - look), look, p - look))
7547 == 0)
7548 match = TRUE;
7549 }
7550 if (match && try_match_word && !try_match)
7551 {
7552 /* "0=word": Check if there are only blanks before the
7553 * word. */
7554 line = ml_get_curline();
7555 if ((int)(skipwhite(line) - line) !=
7556 (int)(curwin->w_cursor.col - (p - look)))
7557 match = FALSE;
7558 }
7559 if (match)
7560 return TRUE;
7561 }
7562 look = p;
7563 }
7564
7565 /*
7566 * ok, it's a boring generic character.
7567 */
7568 else
7569 {
7570 if (try_match && *look == keytyped)
7571 return TRUE;
7572 ++look;
7573 }
7574
7575 /*
7576 * Skip over ", ".
7577 */
7578 look = skip_to_option_part(look);
7579 }
7580 return FALSE;
7581}
7582#endif /* FEAT_CINDENT */
7583
7584#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7585/*
7586 * Map Hebrew keyboard when in hkmap mode.
7587 */
7588 int
7589hkmap(c)
7590 int c;
7591{
7592 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7593 {
7594 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7595 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7596 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7597 static char_u map[26] =
7598 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7599 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7600 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7601 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7602 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7603 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7604 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7605 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7606 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7607
7608 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7609 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7610 /* '-1'='sofit' */
7611 else if (c == 'x')
7612 return 'X';
7613 else if (c == 'q')
7614 return '\''; /* {geresh}={'} */
7615 else if (c == 246)
7616 return ' '; /* \"o --> ' ' for a german keyboard */
7617 else if (c == 228)
7618 return ' '; /* \"a --> ' ' -- / -- */
7619 else if (c == 252)
7620 return ' '; /* \"u --> ' ' -- / -- */
7621#ifdef EBCDIC
7622 else if (islower(c))
7623#else
7624 /* NOTE: islower() does not do the right thing for us on Linux so we
7625 * do this the same was as 5.7 and previous, so it works correctly on
7626 * all systems. Specifically, the e.g. Delete and Arrow keys are
7627 * munged and won't work if e.g. searching for Hebrew text.
7628 */
7629 else if (c >= 'a' && c <= 'z')
7630#endif
7631 return (int)(map[CharOrdLow(c)] + p_aleph);
7632 else
7633 return c;
7634 }
7635 else
7636 {
7637 switch (c)
7638 {
7639 case '`': return ';';
7640 case '/': return '.';
7641 case '\'': return ',';
7642 case 'q': return '/';
7643 case 'w': return '\'';
7644
7645 /* Hebrew letters - set offset from 'a' */
7646 case ',': c = '{'; break;
7647 case '.': c = 'v'; break;
7648 case ';': c = 't'; break;
7649 default: {
7650 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7651
7652#ifdef EBCDIC
7653 /* see note about islower() above */
7654 if (!islower(c))
7655#else
7656 if (c < 'a' || c > 'z')
7657#endif
7658 return c;
7659 c = str[CharOrdLow(c)];
7660 break;
7661 }
7662 }
7663
7664 return (int)(CharOrdLow(c) + p_aleph);
7665 }
7666}
7667#endif
7668
7669 static void
7670ins_reg()
7671{
7672 int need_redraw = FALSE;
7673 int regname;
7674 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007675#ifdef FEAT_VISUAL
7676 int vis_active = VIsual_active;
7677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679 /*
7680 * If we are going to wait for a character, show a '"'.
7681 */
7682 pc_status = PC_STATUS_UNSET;
7683 if (redrawing() && !char_avail())
7684 {
7685 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007686 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687
7688 edit_putchar('"', TRUE);
7689#ifdef FEAT_CMDL_INFO
7690 add_to_showcmd_c(Ctrl_R);
7691#endif
7692 }
7693
7694#ifdef USE_ON_FLY_SCROLL
7695 dont_scroll = TRUE; /* disallow scrolling here */
7696#endif
7697
7698 /*
7699 * Don't map the register name. This also prevents the mode message to be
7700 * deleted when ESC is hit.
7701 */
7702 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007703 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7706 {
7707 /* Get a third key for literal register insertion */
7708 literally = regname;
7709#ifdef FEAT_CMDL_INFO
7710 add_to_showcmd_c(literally);
7711#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007712 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 --no_mapping;
7716
7717#ifdef FEAT_EVAL
7718 /*
7719 * Don't call u_sync() while getting the expression,
7720 * evaluating it or giving an error message for it!
7721 */
7722 ++no_u_sync;
7723 if (regname == '=')
7724 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007725# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007729# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 /* Restore the Input Method. */
7731 if (im_on)
7732 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007735 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7736 {
7737 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 else
7741 {
7742#endif
7743 if (literally == Ctrl_O || literally == Ctrl_P)
7744 {
7745 /* Append the command to the redo buffer. */
7746 AppendCharToRedobuff(Ctrl_R);
7747 AppendCharToRedobuff(literally);
7748 AppendCharToRedobuff(regname);
7749
7750 do_put(regname, BACKWARD, 1L,
7751 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7752 }
7753 else if (insert_reg(regname, literally) == FAIL)
7754 {
7755 vim_beep();
7756 need_redraw = TRUE; /* remove the '"' */
7757 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007758 else if (stop_insert_mode)
7759 /* When the '=' register was used and a function was invoked that
7760 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7761 * insert anything, need to remove the '"' */
7762 need_redraw = TRUE;
7763
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764#ifdef FEAT_EVAL
7765 }
7766 --no_u_sync;
7767#endif
7768#ifdef FEAT_CMDL_INFO
7769 clear_showcmd();
7770#endif
7771
7772 /* If the inserted register is empty, we need to remove the '"' */
7773 if (need_redraw || stuff_empty())
7774 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007775
7776#ifdef FEAT_VISUAL
7777 /* Disallow starting Visual mode here, would get a weird mode. */
7778 if (!vis_active && VIsual_active)
7779 end_visual_mode();
7780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782
7783/*
7784 * CTRL-G commands in Insert mode.
7785 */
7786 static void
7787ins_ctrl_g()
7788{
7789 int c;
7790
7791#ifdef FEAT_INS_EXPAND
7792 /* Right after CTRL-X the cursor will be after the ruler. */
7793 setcursor();
7794#endif
7795
7796 /*
7797 * Don't map the second key. This also prevents the mode message to be
7798 * deleted when ESC is hit.
7799 */
7800 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007801 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 --no_mapping;
7803 switch (c)
7804 {
7805 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7806 case K_UP:
7807 case Ctrl_K:
7808 case 'k': ins_up(TRUE);
7809 break;
7810
7811 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7812 case K_DOWN:
7813 case Ctrl_J:
7814 case 'j': ins_down(TRUE);
7815 break;
7816
7817 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007818 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007820
7821 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007822 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007823 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 break;
7825
7826 /* Unknown CTRL-G command, reserved for future expansion. */
7827 default: vim_beep();
7828 }
7829}
7830
7831/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007832 * CTRL-^ in Insert mode.
7833 */
7834 static void
7835ins_ctrl_hat()
7836{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007837 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007838 {
7839 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7840 if (State & LANGMAP)
7841 {
7842 curbuf->b_p_iminsert = B_IMODE_NONE;
7843 State &= ~LANGMAP;
7844 }
7845 else
7846 {
7847 curbuf->b_p_iminsert = B_IMODE_LMAP;
7848 State |= LANGMAP;
7849#ifdef USE_IM_CONTROL
7850 im_set_active(FALSE);
7851#endif
7852 }
7853 }
7854#ifdef USE_IM_CONTROL
7855 else
7856 {
7857 /* There are no ":lmap" mappings, toggle IM */
7858 if (im_get_status())
7859 {
7860 curbuf->b_p_iminsert = B_IMODE_NONE;
7861 im_set_active(FALSE);
7862 }
7863 else
7864 {
7865 curbuf->b_p_iminsert = B_IMODE_IM;
7866 State &= ~LANGMAP;
7867 im_set_active(TRUE);
7868 }
7869 }
7870#endif
7871 set_iminsert_global();
7872 showmode();
7873#ifdef FEAT_GUI
7874 /* may show different cursor shape or color */
7875 if (gui.in_use)
7876 gui_update_cursor(TRUE, FALSE);
7877#endif
7878#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7879 /* Show/unshow value of 'keymap' in status lines. */
7880 status_redraw_curbuf();
7881#endif
7882}
7883
7884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 * Handle ESC in insert mode.
7886 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7887 * insert.
7888 */
7889 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007890ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 long *count;
7892 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007893 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 int temp;
7896 static int disabled_redraw = FALSE;
7897
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007898#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007899 check_spell_redraw();
7900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901#if defined(FEAT_HANGULIN)
7902# if defined(ESC_CHG_TO_ENG_MODE)
7903 hangul_input_state_set(0);
7904# endif
7905 if (composing_hangul)
7906 {
7907 push_raw_key(composing_hangul_buffer, 2);
7908 composing_hangul = 0;
7909 }
7910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
7912 temp = curwin->w_cursor.col;
7913 if (disabled_redraw)
7914 {
7915 --RedrawingDisabled;
7916 disabled_redraw = FALSE;
7917 }
7918 if (!arrow_used)
7919 {
7920 /*
7921 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007922 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7923 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 */
7925 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007926 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
7928 /*
7929 * Repeating insert may take a long time. Check for
7930 * interrupt now and then.
7931 */
7932 if (*count > 0)
7933 {
7934 line_breakcheck();
7935 if (got_int)
7936 *count = 0;
7937 }
7938
7939 if (--*count > 0) /* repeat what was typed */
7940 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007941 /* Vi repeats the insert without replacing characters. */
7942 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7943 State &= ~REPLACE_FLAG;
7944
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 (void)start_redo_ins();
7946 if (cmdchar == 'r' || cmdchar == 'v')
7947 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7948 ++RedrawingDisabled;
7949 disabled_redraw = TRUE;
7950 return FALSE; /* repeat the insert */
7951 }
7952 stop_insert(&curwin->w_cursor, TRUE);
7953 undisplay_dollar();
7954 }
7955
7956 /* When an autoindent was removed, curswant stays after the
7957 * indent */
7958 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7959 curwin->w_set_curswant = TRUE;
7960
7961 /* Remember the last Insert position in the '^ mark. */
7962 if (!cmdmod.keepjumps)
7963 curbuf->b_last_insert = curwin->w_cursor;
7964
7965 /*
7966 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007967 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007969 if (!nomove
7970 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#ifdef FEAT_VIRTUALEDIT
7972 || curwin->w_cursor.coladd > 0
7973#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007974 )
7975 && (restart_edit == NUL
7976 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007978 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007980 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981#ifdef FEAT_RIGHTLEFT
7982 && !revins_on
7983#endif
7984 )
7985 {
7986#ifdef FEAT_VIRTUALEDIT
7987 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7988 {
7989 oneleft();
7990 if (restart_edit != NUL)
7991 ++curwin->w_cursor.coladd;
7992 }
7993 else
7994#endif
7995 {
7996 --curwin->w_cursor.col;
7997#ifdef FEAT_MBYTE
7998 /* Correct cursor for multi-byte character. */
7999 if (has_mbyte)
8000 mb_adjust_cursor();
8001#endif
8002 }
8003 }
8004
8005#ifdef USE_IM_CONTROL
8006 /* Disable IM to allow typing English directly for Normal mode commands.
8007 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8008 * well). */
8009 if (!(State & LANGMAP))
8010 im_save_status(&curbuf->b_p_iminsert);
8011 im_set_active(FALSE);
8012#endif
8013
8014 State = NORMAL;
8015 /* need to position cursor again (e.g. when on a TAB ) */
8016 changed_cline_bef_curs();
8017
8018#ifdef FEAT_MOUSE
8019 setmouse();
8020#endif
8021#ifdef CURSOR_SHAPE
8022 ui_cursor_shape(); /* may show different cursor shape */
8023#endif
8024
8025 /*
8026 * When recording or for CTRL-O, need to display the new mode.
8027 * Otherwise remove the mode message.
8028 */
8029 if (Recording || restart_edit != NUL)
8030 showmode();
8031 else if (p_smd)
8032 MSG("");
8033
8034 return TRUE; /* exit Insert mode */
8035}
8036
8037#ifdef FEAT_RIGHTLEFT
8038/*
8039 * Toggle language: hkmap and revins_on.
8040 * Move to end of reverse inserted text.
8041 */
8042 static void
8043ins_ctrl_()
8044{
8045 if (revins_on && revins_chars && revins_scol >= 0)
8046 {
8047 while (gchar_cursor() != NUL && revins_chars--)
8048 ++curwin->w_cursor.col;
8049 }
8050 p_ri = !p_ri;
8051 revins_on = (State == INSERT && p_ri);
8052 if (revins_on)
8053 {
8054 revins_scol = curwin->w_cursor.col;
8055 revins_legal++;
8056 revins_chars = 0;
8057 undisplay_dollar();
8058 }
8059 else
8060 revins_scol = -1;
8061#ifdef FEAT_FKMAP
8062 if (p_altkeymap)
8063 {
8064 /*
8065 * to be consistent also for redo command, using '.'
8066 * set arrow_used to true and stop it - causing to redo
8067 * characters entered in one mode (normal/reverse insert).
8068 */
8069 arrow_used = TRUE;
8070 (void)stop_arrow();
8071 p_fkmap = curwin->w_p_rl ^ p_ri;
8072 if (p_fkmap && p_ri)
8073 State = INSERT;
8074 }
8075 else
8076#endif
8077 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8078 showmode();
8079}
8080#endif
8081
8082#ifdef FEAT_VISUAL
8083/*
8084 * If 'keymodel' contains "startsel", may start selection.
8085 * Returns TRUE when a CTRL-O and other keys stuffed.
8086 */
8087 static int
8088ins_start_select(c)
8089 int c;
8090{
8091 if (km_startsel)
8092 switch (c)
8093 {
8094 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 case K_PAGEUP:
8097 case K_KPAGEUP:
8098 case K_PAGEDOWN:
8099 case K_KPAGEDOWN:
8100# ifdef MACOS
8101 case K_LEFT:
8102 case K_RIGHT:
8103 case K_UP:
8104 case K_DOWN:
8105 case K_END:
8106 case K_HOME:
8107# endif
8108 if (!(mod_mask & MOD_MASK_SHIFT))
8109 break;
8110 /* FALLTHROUGH */
8111 case K_S_LEFT:
8112 case K_S_RIGHT:
8113 case K_S_UP:
8114 case K_S_DOWN:
8115 case K_S_END:
8116 case K_S_HOME:
8117 /* Start selection right away, the cursor can move with
8118 * CTRL-O when beyond the end of the line. */
8119 start_selection();
8120
8121 /* Execute the key in (insert) Select mode. */
8122 stuffcharReadbuff(Ctrl_O);
8123 if (mod_mask)
8124 {
8125 char_u buf[4];
8126
8127 buf[0] = K_SPECIAL;
8128 buf[1] = KS_MODIFIER;
8129 buf[2] = mod_mask;
8130 buf[3] = NUL;
8131 stuffReadbuff(buf);
8132 }
8133 stuffcharReadbuff(c);
8134 return TRUE;
8135 }
8136 return FALSE;
8137}
8138#endif
8139
8140/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008141 * <Insert> key in Insert mode: toggle insert/remplace mode.
8142 */
8143 static void
8144ins_insert(replaceState)
8145 int replaceState;
8146{
8147#ifdef FEAT_FKMAP
8148 if (p_fkmap && p_ri)
8149 {
8150 beep_flush();
8151 EMSG(farsi_text_3); /* encoded in Farsi */
8152 return;
8153 }
8154#endif
8155
8156#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008157# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008158 set_vim_var_string(VV_INSERTMODE,
8159 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008160# ifdef FEAT_VREPLACE
8161 replaceState == VREPLACE ? "v" :
8162# endif
8163 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008164# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008165 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8166#endif
8167 if (State & REPLACE_FLAG)
8168 State = INSERT | (State & LANGMAP);
8169 else
8170 State = replaceState | (State & LANGMAP);
8171 AppendCharToRedobuff(K_INS);
8172 showmode();
8173#ifdef CURSOR_SHAPE
8174 ui_cursor_shape(); /* may show different cursor shape */
8175#endif
8176}
8177
8178/*
8179 * Pressed CTRL-O in Insert mode.
8180 */
8181 static void
8182ins_ctrl_o()
8183{
8184#ifdef FEAT_VREPLACE
8185 if (State & VREPLACE_FLAG)
8186 restart_edit = 'V';
8187 else
8188#endif
8189 if (State & REPLACE_FLAG)
8190 restart_edit = 'R';
8191 else
8192 restart_edit = 'I';
8193#ifdef FEAT_VIRTUALEDIT
8194 if (virtual_active())
8195 ins_at_eol = FALSE; /* cursor always keeps its column */
8196 else
8197#endif
8198 ins_at_eol = (gchar_cursor() == NUL);
8199}
8200
8201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 * If the cursor is on an indent, ^T/^D insert/delete one
8203 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008204 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 * with vi. But vi only supports ^T and ^D after an
8206 * autoindent, we support it everywhere.
8207 */
8208 static void
8209ins_shift(c, lastc)
8210 int c;
8211 int lastc;
8212{
8213 if (stop_arrow() == FAIL)
8214 return;
8215 AppendCharToRedobuff(c);
8216
8217 /*
8218 * 0^D and ^^D: remove all indent.
8219 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008220 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8221 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
8223 --curwin->w_cursor.col;
8224 (void)del_char(FALSE); /* delete the '^' or '0' */
8225 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8226 if (State & REPLACE_FLAG)
8227 replace_pop_ins();
8228 if (lastc == '^')
8229 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008230 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
8232 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008233 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
8235 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8236 did_ai = FALSE;
8237#ifdef FEAT_SMARTINDENT
8238 did_si = FALSE;
8239 can_si = FALSE;
8240 can_si_back = FALSE;
8241#endif
8242#ifdef FEAT_CINDENT
8243 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8244#endif
8245}
8246
8247 static void
8248ins_del()
8249{
8250 int temp;
8251
8252 if (stop_arrow() == FAIL)
8253 return;
8254 if (gchar_cursor() == NUL) /* delete newline */
8255 {
8256 temp = curwin->w_cursor.col;
8257 if (!can_bs(BS_EOL) /* only if "eol" included */
8258 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8259 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8260 || do_join(FALSE) == FAIL)
8261 vim_beep();
8262 else
8263 curwin->w_cursor.col = temp;
8264 }
8265 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8266 vim_beep();
8267 did_ai = FALSE;
8268#ifdef FEAT_SMARTINDENT
8269 did_si = FALSE;
8270 can_si = FALSE;
8271 can_si_back = FALSE;
8272#endif
8273 AppendCharToRedobuff(K_DEL);
8274}
8275
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008276static void ins_bs_one __ARGS((colnr_T *vcolp));
8277
8278/*
8279 * Delete one character for ins_bs().
8280 */
8281 static void
8282ins_bs_one(vcolp)
8283 colnr_T *vcolp;
8284{
8285 dec_cursor();
8286 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8287 if (State & REPLACE_FLAG)
8288 {
8289 /* Don't delete characters before the insert point when in
8290 * Replace mode */
8291 if (curwin->w_cursor.lnum != Insstart.lnum
8292 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008293 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008294 }
8295 else
8296 (void)del_char(FALSE);
8297}
8298
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299/*
8300 * Handle Backspace, delete-word and delete-line in Insert mode.
8301 * Return TRUE when backspace was actually used.
8302 */
8303 static int
8304ins_bs(c, mode, inserted_space_p)
8305 int c;
8306 int mode;
8307 int *inserted_space_p;
8308{
8309 linenr_T lnum;
8310 int cc;
8311 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008312 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 colnr_T mincol;
8314 int did_backspace = FALSE;
8315 int in_indent;
8316 int oldState;
8317#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008318 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#endif
8320
8321 /*
8322 * can't delete anything in an empty file
8323 * can't backup past first character in buffer
8324 * can't backup past starting point unless 'backspace' > 1
8325 * can backup to a previous line if 'backspace' == 0
8326 */
8327 if ( bufempty()
8328 || (
8329#ifdef FEAT_RIGHTLEFT
8330 !revins_on &&
8331#endif
8332 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8333 || (!can_bs(BS_START)
8334 && (arrow_used
8335 || (curwin->w_cursor.lnum == Insstart.lnum
8336 && curwin->w_cursor.col <= Insstart.col)))
8337 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8338 && curwin->w_cursor.col <= ai_col)
8339 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8340 {
8341 vim_beep();
8342 return FALSE;
8343 }
8344
8345 if (stop_arrow() == FAIL)
8346 return FALSE;
8347 in_indent = inindent(0);
8348#ifdef FEAT_CINDENT
8349 if (in_indent)
8350 can_cindent = FALSE;
8351#endif
8352#ifdef FEAT_COMMENTS
8353 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8354#endif
8355#ifdef FEAT_RIGHTLEFT
8356 if (revins_on) /* put cursor after last inserted char */
8357 inc_cursor();
8358#endif
8359
8360#ifdef FEAT_VIRTUALEDIT
8361 /* Virtualedit:
8362 * BACKSPACE_CHAR eats a virtual space
8363 * BACKSPACE_WORD eats all coladd
8364 * BACKSPACE_LINE eats all coladd and keeps going
8365 */
8366 if (curwin->w_cursor.coladd > 0)
8367 {
8368 if (mode == BACKSPACE_CHAR)
8369 {
8370 --curwin->w_cursor.coladd;
8371 return TRUE;
8372 }
8373 if (mode == BACKSPACE_WORD)
8374 {
8375 curwin->w_cursor.coladd = 0;
8376 return TRUE;
8377 }
8378 curwin->w_cursor.coladd = 0;
8379 }
8380#endif
8381
8382 /*
8383 * delete newline!
8384 */
8385 if (curwin->w_cursor.col == 0)
8386 {
8387 lnum = Insstart.lnum;
8388 if (curwin->w_cursor.lnum == Insstart.lnum
8389#ifdef FEAT_RIGHTLEFT
8390 || revins_on
8391#endif
8392 )
8393 {
8394 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8395 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8396 return FALSE;
8397 --Insstart.lnum;
8398 Insstart.col = MAXCOL;
8399 }
8400 /*
8401 * In replace mode:
8402 * cc < 0: NL was inserted, delete it
8403 * cc >= 0: NL was replaced, put original characters back
8404 */
8405 cc = -1;
8406 if (State & REPLACE_FLAG)
8407 cc = replace_pop(); /* returns -1 if NL was inserted */
8408 /*
8409 * In replace mode, in the line we started replacing, we only move the
8410 * cursor.
8411 */
8412 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8413 {
8414 dec_cursor();
8415 }
8416 else
8417 {
8418#ifdef FEAT_VREPLACE
8419 if (!(State & VREPLACE_FLAG)
8420 || curwin->w_cursor.lnum > orig_line_count)
8421#endif
8422 {
8423 temp = gchar_cursor(); /* remember current char */
8424 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008425
8426 /* When "aw" is in 'formatoptions' we must delete the space at
8427 * the end of the line, otherwise the line will be broken
8428 * again when auto-formatting. */
8429 if (has_format_option(FO_AUTO)
8430 && has_format_option(FO_WHITE_PAR))
8431 {
8432 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8433 TRUE);
8434 int len;
8435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008436 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008437 if (len > 0 && ptr[len - 1] == ' ')
8438 ptr[len - 1] = NUL;
8439 }
8440
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 (void)do_join(FALSE);
8442 if (temp == NUL && gchar_cursor() != NUL)
8443 inc_cursor();
8444 }
8445#ifdef FEAT_VREPLACE
8446 else
8447 dec_cursor();
8448#endif
8449
8450 /*
8451 * In REPLACE mode we have to put back the text that was replaced
8452 * by the NL. On the replace stack is first a NUL-terminated
8453 * sequence of characters that were deleted and then the
8454 * characters that NL replaced.
8455 */
8456 if (State & REPLACE_FLAG)
8457 {
8458 /*
8459 * Do the next ins_char() in NORMAL state, to
8460 * prevent ins_char() from replacing characters and
8461 * avoiding showmatch().
8462 */
8463 oldState = State;
8464 State = NORMAL;
8465 /*
8466 * restore characters (blanks) deleted after cursor
8467 */
8468 while (cc > 0)
8469 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008470 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#ifdef FEAT_MBYTE
8472 mb_replace_pop_ins(cc);
8473#else
8474 ins_char(cc);
8475#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008476 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 cc = replace_pop();
8478 }
8479 /* restore the characters that NL replaced */
8480 replace_pop_ins();
8481 State = oldState;
8482 }
8483 }
8484 did_ai = FALSE;
8485 }
8486 else
8487 {
8488 /*
8489 * Delete character(s) before the cursor.
8490 */
8491#ifdef FEAT_RIGHTLEFT
8492 if (revins_on) /* put cursor on last inserted char */
8493 dec_cursor();
8494#endif
8495 mincol = 0;
8496 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008497 if (mode == BACKSPACE_LINE
8498 && (curbuf->b_p_ai
8499#ifdef FEAT_CINDENT
8500 || cindent_on()
8501#endif
8502 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503#ifdef FEAT_RIGHTLEFT
8504 && !revins_on
8505#endif
8506 )
8507 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008508 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 beginline(BL_WHITE);
8510 if (curwin->w_cursor.col < (colnr_T)temp)
8511 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008512 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 }
8514
8515 /*
8516 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8517 */
8518 if ( mode == BACKSPACE_CHAR
8519 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008520 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008521 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 && (*(ml_get_cursor() - 1) == TAB
8523 || (*(ml_get_cursor() - 1) == ' '
8524 && (!*inserted_space_p
8525 || arrow_used))))))
8526 {
8527 int ts;
8528 colnr_T vcol;
8529 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008530 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531
8532 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008533 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 ts = curbuf->b_p_sw;
8535 else
8536 ts = curbuf->b_p_sts;
8537 /* Compute the virtual column where we want to be. Since
8538 * 'showbreak' may get in the way, need to get the last column of
8539 * the previous character. */
8540 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008541 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 dec_cursor();
8543 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8544 inc_cursor();
8545 want_vcol = (want_vcol / ts) * ts;
8546
8547 /* delete characters until we are at or before want_vcol */
8548 while (vcol > want_vcol
8549 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008550 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551
8552 /* insert extra spaces until we are at want_vcol */
8553 while (vcol < want_vcol)
8554 {
8555 /* Remember the first char we inserted */
8556 if (curwin->w_cursor.lnum == Insstart.lnum
8557 && curwin->w_cursor.col < Insstart.col)
8558 Insstart.col = curwin->w_cursor.col;
8559
8560#ifdef FEAT_VREPLACE
8561 if (State & VREPLACE_FLAG)
8562 ins_char(' ');
8563 else
8564#endif
8565 {
8566 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008567 if ((State & REPLACE_FLAG))
8568 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8571 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008572
8573 /* If we are now back where we started delete one character. Can
8574 * happen when using 'sts' and 'linebreak'. */
8575 if (vcol >= start_vcol)
8576 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 }
8578
8579 /*
8580 * Delete upto starting point, start of line or previous word.
8581 */
8582 else do
8583 {
8584#ifdef FEAT_RIGHTLEFT
8585 if (!revins_on) /* put cursor on char to be deleted */
8586#endif
8587 dec_cursor();
8588
8589 /* start of word? */
8590 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8591 {
8592 mode = BACKSPACE_WORD_NOT_SPACE;
8593 temp = vim_iswordc(gchar_cursor());
8594 }
8595 /* end of word? */
8596 else if (mode == BACKSPACE_WORD_NOT_SPACE
8597 && (vim_isspace(cc = gchar_cursor())
8598 || vim_iswordc(cc) != temp))
8599 {
8600#ifdef FEAT_RIGHTLEFT
8601 if (!revins_on)
8602#endif
8603 inc_cursor();
8604#ifdef FEAT_RIGHTLEFT
8605 else if (State & REPLACE_FLAG)
8606 dec_cursor();
8607#endif
8608 break;
8609 }
8610 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008611 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 else
8613 {
8614#ifdef FEAT_MBYTE
8615 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008616 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617#endif
8618 (void)del_char(FALSE);
8619#ifdef FEAT_MBYTE
8620 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008621 * If there are combining characters and 'delcombine' is set
8622 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 * character.
8624 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008625 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 inc_cursor();
8627#endif
8628#ifdef FEAT_RIGHTLEFT
8629 if (revins_chars)
8630 {
8631 revins_chars--;
8632 revins_legal++;
8633 }
8634 if (revins_on && gchar_cursor() == NUL)
8635 break;
8636#endif
8637 }
8638 /* Just a single backspace?: */
8639 if (mode == BACKSPACE_CHAR)
8640 break;
8641 } while (
8642#ifdef FEAT_RIGHTLEFT
8643 revins_on ||
8644#endif
8645 (curwin->w_cursor.col > mincol
8646 && (curwin->w_cursor.lnum != Insstart.lnum
8647 || curwin->w_cursor.col != Insstart.col)));
8648 did_backspace = TRUE;
8649 }
8650#ifdef FEAT_SMARTINDENT
8651 did_si = FALSE;
8652 can_si = FALSE;
8653 can_si_back = FALSE;
8654#endif
8655 if (curwin->w_cursor.col <= 1)
8656 did_ai = FALSE;
8657 /*
8658 * It's a little strange to put backspaces into the redo
8659 * buffer, but it makes auto-indent a lot easier to deal
8660 * with.
8661 */
8662 AppendCharToRedobuff(c);
8663
8664 /* If deleted before the insertion point, adjust it */
8665 if (curwin->w_cursor.lnum == Insstart.lnum
8666 && curwin->w_cursor.col < Insstart.col)
8667 Insstart.col = curwin->w_cursor.col;
8668
8669 /* vi behaviour: the cursor moves backward but the character that
8670 * was there remains visible
8671 * Vim behaviour: the cursor moves backward and the character that
8672 * was there is erased from the screen.
8673 * We can emulate the vi behaviour by pretending there is a dollar
8674 * displayed even when there isn't.
8675 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8676 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8677 dollar_vcol = curwin->w_virtcol;
8678
Bram Moolenaarce3be472008-01-14 19:12:28 +00008679#ifdef FEAT_FOLDING
8680 /* When deleting a char the cursor line must never be in a closed fold.
8681 * E.g., when 'foldmethod' is indent and deleting the first non-white
8682 * char before a Tab. */
8683 if (did_backspace)
8684 foldOpenCursor();
8685#endif
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 return did_backspace;
8688}
8689
8690#ifdef FEAT_MOUSE
8691 static void
8692ins_mouse(c)
8693 int c;
8694{
8695 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008696 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697
8698# ifdef FEAT_GUI
8699 /* When GUI is active, also move/paste when 'mouse' is empty */
8700 if (!gui.in_use)
8701# endif
8702 if (!mouse_has(MOUSE_INSERT))
8703 return;
8704
8705 undisplay_dollar();
8706 tpos = curwin->w_cursor;
8707 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8708 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008709#ifdef FEAT_WINDOWS
8710 win_T *new_curwin = curwin;
8711
8712 if (curwin != old_curwin && win_valid(old_curwin))
8713 {
8714 /* Mouse took us to another window. We need to go back to the
8715 * previous one to stop insert there properly. */
8716 curwin = old_curwin;
8717 curbuf = curwin->w_buffer;
8718 }
8719#endif
8720 start_arrow(curwin == old_curwin ? &tpos : NULL);
8721#ifdef FEAT_WINDOWS
8722 if (curwin != new_curwin && win_valid(new_curwin))
8723 {
8724 curwin = new_curwin;
8725 curbuf = curwin->w_buffer;
8726 }
8727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728# ifdef FEAT_CINDENT
8729 can_cindent = TRUE;
8730# endif
8731 }
8732
8733#ifdef FEAT_WINDOWS
8734 /* redraw status lines (in case another window became active) */
8735 redraw_statuslines();
8736#endif
8737}
8738
8739 static void
8740ins_mousescroll(up)
8741 int up;
8742{
8743 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008744# if defined(FEAT_WINDOWS)
8745 win_T *old_curwin = curwin;
8746# endif
8747# ifdef FEAT_INS_EXPAND
8748 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749# endif
8750
8751 tpos = curwin->w_cursor;
8752
8753# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 /* Currently the mouse coordinates are only known in the GUI. */
8755 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8756 {
8757 int row, col;
8758
8759 row = mouse_row;
8760 col = mouse_col;
8761
8762 /* find the window at the pointer coordinates */
8763 curwin = mouse_find_win(&row, &col);
8764 curbuf = curwin->w_buffer;
8765 }
8766 if (curwin == old_curwin)
8767# endif
8768 undisplay_dollar();
8769
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008770# ifdef FEAT_INS_EXPAND
8771 /* Don't scroll the window in which completion is being done. */
8772 if (!pum_visible()
8773# if defined(FEAT_WINDOWS)
8774 || curwin != old_curwin
8775# endif
8776 )
8777# endif
8778 {
8779 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8780 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8781 else
8782 scroll_redraw(up, 3L);
8783# ifdef FEAT_INS_EXPAND
8784 did_scroll = TRUE;
8785# endif
8786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
8788# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8789 curwin->w_redr_status = TRUE;
8790
8791 curwin = old_curwin;
8792 curbuf = curwin->w_buffer;
8793# endif
8794
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008795# ifdef FEAT_INS_EXPAND
8796 /* The popup menu may overlay the window, need to redraw it.
8797 * TODO: Would be more efficient to only redraw the windows that are
8798 * overlapped by the popup menu. */
8799 if (pum_visible() && did_scroll)
8800 {
8801 redraw_all_later(NOT_VALID);
8802 ins_compl_show_pum();
8803 }
8804# endif
8805
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 if (!equalpos(curwin->w_cursor, tpos))
8807 {
8808 start_arrow(&tpos);
8809# ifdef FEAT_CINDENT
8810 can_cindent = TRUE;
8811# endif
8812 }
8813}
8814#endif
8815
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008816#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008817 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008818ins_tabline(c)
8819 int c;
8820{
8821 /* We will be leaving the current window, unless closing another tab. */
8822 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8823 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8824 {
8825 undisplay_dollar();
8826 start_arrow(&curwin->w_cursor);
8827# ifdef FEAT_CINDENT
8828 can_cindent = TRUE;
8829# endif
8830 }
8831
8832 if (c == K_TABLINE)
8833 goto_tabpage(current_tab);
8834 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008835 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008836 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008837 redraw_statuslines(); /* will redraw the tabline when needed */
8838 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008839}
8840#endif
8841
8842#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 void
8844ins_scroll()
8845{
8846 pos_T tpos;
8847
8848 undisplay_dollar();
8849 tpos = curwin->w_cursor;
8850 if (gui_do_scroll())
8851 {
8852 start_arrow(&tpos);
8853# ifdef FEAT_CINDENT
8854 can_cindent = TRUE;
8855# endif
8856 }
8857}
8858
8859 void
8860ins_horscroll()
8861{
8862 pos_T tpos;
8863
8864 undisplay_dollar();
8865 tpos = curwin->w_cursor;
8866 if (gui_do_horiz_scroll())
8867 {
8868 start_arrow(&tpos);
8869# ifdef FEAT_CINDENT
8870 can_cindent = TRUE;
8871# endif
8872 }
8873}
8874#endif
8875
8876 static void
8877ins_left()
8878{
8879 pos_T tpos;
8880
8881#ifdef FEAT_FOLDING
8882 if ((fdo_flags & FDO_HOR) && KeyTyped)
8883 foldOpenCursor();
8884#endif
8885 undisplay_dollar();
8886 tpos = curwin->w_cursor;
8887 if (oneleft() == OK)
8888 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008889#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8890 /* Only call start_arrow() when not busy with preediting, it will
8891 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8892 if (!im_is_preediting())
8893#endif
8894 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#ifdef FEAT_RIGHTLEFT
8896 /* If exit reversed string, position is fixed */
8897 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8898 revins_legal++;
8899 revins_chars++;
8900#endif
8901 }
8902
8903 /*
8904 * if 'whichwrap' set for cursor in insert mode may go to
8905 * previous line
8906 */
8907 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8908 {
8909 start_arrow(&tpos);
8910 --(curwin->w_cursor.lnum);
8911 coladvance((colnr_T)MAXCOL);
8912 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8913 }
8914 else
8915 vim_beep();
8916}
8917
8918 static void
8919ins_home(c)
8920 int c;
8921{
8922 pos_T tpos;
8923
8924#ifdef FEAT_FOLDING
8925 if ((fdo_flags & FDO_HOR) && KeyTyped)
8926 foldOpenCursor();
8927#endif
8928 undisplay_dollar();
8929 tpos = curwin->w_cursor;
8930 if (c == K_C_HOME)
8931 curwin->w_cursor.lnum = 1;
8932 curwin->w_cursor.col = 0;
8933#ifdef FEAT_VIRTUALEDIT
8934 curwin->w_cursor.coladd = 0;
8935#endif
8936 curwin->w_curswant = 0;
8937 start_arrow(&tpos);
8938}
8939
8940 static void
8941ins_end(c)
8942 int c;
8943{
8944 pos_T tpos;
8945
8946#ifdef FEAT_FOLDING
8947 if ((fdo_flags & FDO_HOR) && KeyTyped)
8948 foldOpenCursor();
8949#endif
8950 undisplay_dollar();
8951 tpos = curwin->w_cursor;
8952 if (c == K_C_END)
8953 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8954 coladvance((colnr_T)MAXCOL);
8955 curwin->w_curswant = MAXCOL;
8956
8957 start_arrow(&tpos);
8958}
8959
8960 static void
8961ins_s_left()
8962{
8963#ifdef FEAT_FOLDING
8964 if ((fdo_flags & FDO_HOR) && KeyTyped)
8965 foldOpenCursor();
8966#endif
8967 undisplay_dollar();
8968 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8969 {
8970 start_arrow(&curwin->w_cursor);
8971 (void)bck_word(1L, FALSE, FALSE);
8972 curwin->w_set_curswant = TRUE;
8973 }
8974 else
8975 vim_beep();
8976}
8977
8978 static void
8979ins_right()
8980{
8981#ifdef FEAT_FOLDING
8982 if ((fdo_flags & FDO_HOR) && KeyTyped)
8983 foldOpenCursor();
8984#endif
8985 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00008986 if (gchar_cursor() != NUL
8987#ifdef FEAT_VIRTUALEDIT
8988 || virtual_active()
8989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 )
8991 {
8992 start_arrow(&curwin->w_cursor);
8993 curwin->w_set_curswant = TRUE;
8994#ifdef FEAT_VIRTUALEDIT
8995 if (virtual_active())
8996 oneright();
8997 else
8998#endif
8999 {
9000#ifdef FEAT_MBYTE
9001 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009002 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 else
9004#endif
9005 ++curwin->w_cursor.col;
9006 }
9007
9008#ifdef FEAT_RIGHTLEFT
9009 revins_legal++;
9010 if (revins_chars)
9011 revins_chars--;
9012#endif
9013 }
9014 /* if 'whichwrap' set for cursor in insert mode, may move the
9015 * cursor to the next line */
9016 else if (vim_strchr(p_ww, ']') != NULL
9017 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9018 {
9019 start_arrow(&curwin->w_cursor);
9020 curwin->w_set_curswant = TRUE;
9021 ++curwin->w_cursor.lnum;
9022 curwin->w_cursor.col = 0;
9023 }
9024 else
9025 vim_beep();
9026}
9027
9028 static void
9029ins_s_right()
9030{
9031#ifdef FEAT_FOLDING
9032 if ((fdo_flags & FDO_HOR) && KeyTyped)
9033 foldOpenCursor();
9034#endif
9035 undisplay_dollar();
9036 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9037 || gchar_cursor() != NUL)
9038 {
9039 start_arrow(&curwin->w_cursor);
9040 (void)fwd_word(1L, FALSE, 0);
9041 curwin->w_set_curswant = TRUE;
9042 }
9043 else
9044 vim_beep();
9045}
9046
9047 static void
9048ins_up(startcol)
9049 int startcol; /* when TRUE move to Insstart.col */
9050{
9051 pos_T tpos;
9052 linenr_T old_topline = curwin->w_topline;
9053#ifdef FEAT_DIFF
9054 int old_topfill = curwin->w_topfill;
9055#endif
9056
9057 undisplay_dollar();
9058 tpos = curwin->w_cursor;
9059 if (cursor_up(1L, TRUE) == OK)
9060 {
9061 if (startcol)
9062 coladvance(getvcol_nolist(&Insstart));
9063 if (old_topline != curwin->w_topline
9064#ifdef FEAT_DIFF
9065 || old_topfill != curwin->w_topfill
9066#endif
9067 )
9068 redraw_later(VALID);
9069 start_arrow(&tpos);
9070#ifdef FEAT_CINDENT
9071 can_cindent = TRUE;
9072#endif
9073 }
9074 else
9075 vim_beep();
9076}
9077
9078 static void
9079ins_pageup()
9080{
9081 pos_T tpos;
9082
9083 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009084
9085#ifdef FEAT_WINDOWS
9086 if (mod_mask & MOD_MASK_CTRL)
9087 {
9088 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009089 if (first_tabpage->tp_next != NULL)
9090 {
9091 start_arrow(&curwin->w_cursor);
9092 goto_tabpage(-1);
9093 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009094 return;
9095 }
9096#endif
9097
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 tpos = curwin->w_cursor;
9099 if (onepage(BACKWARD, 1L) == OK)
9100 {
9101 start_arrow(&tpos);
9102#ifdef FEAT_CINDENT
9103 can_cindent = TRUE;
9104#endif
9105 }
9106 else
9107 vim_beep();
9108}
9109
9110 static void
9111ins_down(startcol)
9112 int startcol; /* when TRUE move to Insstart.col */
9113{
9114 pos_T tpos;
9115 linenr_T old_topline = curwin->w_topline;
9116#ifdef FEAT_DIFF
9117 int old_topfill = curwin->w_topfill;
9118#endif
9119
9120 undisplay_dollar();
9121 tpos = curwin->w_cursor;
9122 if (cursor_down(1L, TRUE) == OK)
9123 {
9124 if (startcol)
9125 coladvance(getvcol_nolist(&Insstart));
9126 if (old_topline != curwin->w_topline
9127#ifdef FEAT_DIFF
9128 || old_topfill != curwin->w_topfill
9129#endif
9130 )
9131 redraw_later(VALID);
9132 start_arrow(&tpos);
9133#ifdef FEAT_CINDENT
9134 can_cindent = TRUE;
9135#endif
9136 }
9137 else
9138 vim_beep();
9139}
9140
9141 static void
9142ins_pagedown()
9143{
9144 pos_T tpos;
9145
9146 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009147
9148#ifdef FEAT_WINDOWS
9149 if (mod_mask & MOD_MASK_CTRL)
9150 {
9151 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009152 if (first_tabpage->tp_next != NULL)
9153 {
9154 start_arrow(&curwin->w_cursor);
9155 goto_tabpage(0);
9156 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009157 return;
9158 }
9159#endif
9160
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 tpos = curwin->w_cursor;
9162 if (onepage(FORWARD, 1L) == OK)
9163 {
9164 start_arrow(&tpos);
9165#ifdef FEAT_CINDENT
9166 can_cindent = TRUE;
9167#endif
9168 }
9169 else
9170 vim_beep();
9171}
9172
9173#ifdef FEAT_DND
9174 static void
9175ins_drop()
9176{
9177 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9178}
9179#endif
9180
9181/*
9182 * Handle TAB in Insert or Replace mode.
9183 * Return TRUE when the TAB needs to be inserted like a normal character.
9184 */
9185 static int
9186ins_tab()
9187{
9188 int ind;
9189 int i;
9190 int temp;
9191
9192 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9193 Insstart_blank_vcol = get_nolist_virtcol();
9194 if (echeck_abbr(TAB + ABBR_OFF))
9195 return FALSE;
9196
9197 ind = inindent(0);
9198#ifdef FEAT_CINDENT
9199 if (ind)
9200 can_cindent = FALSE;
9201#endif
9202
9203 /*
9204 * When nothing special, insert TAB like a normal character
9205 */
9206 if (!curbuf->b_p_et
9207 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9208 && curbuf->b_p_sts == 0)
9209 return TRUE;
9210
9211 if (stop_arrow() == FAIL)
9212 return TRUE;
9213
9214 did_ai = FALSE;
9215#ifdef FEAT_SMARTINDENT
9216 did_si = FALSE;
9217 can_si = FALSE;
9218 can_si_back = FALSE;
9219#endif
9220 AppendToRedobuff((char_u *)"\t");
9221
9222 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9223 temp = (int)curbuf->b_p_sw;
9224 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9225 temp = (int)curbuf->b_p_sts;
9226 else /* otherwise use 'tabstop' */
9227 temp = (int)curbuf->b_p_ts;
9228 temp -= get_nolist_virtcol() % temp;
9229
9230 /*
9231 * Insert the first space with ins_char(). It will delete one char in
9232 * replace mode. Insert the rest with ins_str(); it will not delete any
9233 * chars. For VREPLACE mode, we use ins_char() for all characters.
9234 */
9235 ins_char(' ');
9236 while (--temp > 0)
9237 {
9238#ifdef FEAT_VREPLACE
9239 if (State & VREPLACE_FLAG)
9240 ins_char(' ');
9241 else
9242#endif
9243 {
9244 ins_str((char_u *)" ");
9245 if (State & REPLACE_FLAG) /* no char replaced */
9246 replace_push(NUL);
9247 }
9248 }
9249
9250 /*
9251 * When 'expandtab' not set: Replace spaces by TABs where possible.
9252 */
9253 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9254 {
9255 char_u *ptr;
9256#ifdef FEAT_VREPLACE
9257 char_u *saved_line = NULL; /* init for GCC */
9258 pos_T pos;
9259#endif
9260 pos_T fpos;
9261 pos_T *cursor;
9262 colnr_T want_vcol, vcol;
9263 int change_col = -1;
9264 int save_list = curwin->w_p_list;
9265
9266 /*
9267 * Get the current line. For VREPLACE mode, don't make real changes
9268 * yet, just work on a copy of the line.
9269 */
9270#ifdef FEAT_VREPLACE
9271 if (State & VREPLACE_FLAG)
9272 {
9273 pos = curwin->w_cursor;
9274 cursor = &pos;
9275 saved_line = vim_strsave(ml_get_curline());
9276 if (saved_line == NULL)
9277 return FALSE;
9278 ptr = saved_line + pos.col;
9279 }
9280 else
9281#endif
9282 {
9283 ptr = ml_get_cursor();
9284 cursor = &curwin->w_cursor;
9285 }
9286
9287 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9288 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9289 curwin->w_p_list = FALSE;
9290
9291 /* Find first white before the cursor */
9292 fpos = curwin->w_cursor;
9293 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9294 {
9295 --fpos.col;
9296 --ptr;
9297 }
9298
9299 /* In Replace mode, don't change characters before the insert point. */
9300 if ((State & REPLACE_FLAG)
9301 && fpos.lnum == Insstart.lnum
9302 && fpos.col < Insstart.col)
9303 {
9304 ptr += Insstart.col - fpos.col;
9305 fpos.col = Insstart.col;
9306 }
9307
9308 /* compute virtual column numbers of first white and cursor */
9309 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9310 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9311
9312 /* Use as many TABs as possible. Beware of 'showbreak' and
9313 * 'linebreak' adding extra virtual columns. */
9314 while (vim_iswhite(*ptr))
9315 {
9316 i = lbr_chartabsize((char_u *)"\t", vcol);
9317 if (vcol + i > want_vcol)
9318 break;
9319 if (*ptr != TAB)
9320 {
9321 *ptr = TAB;
9322 if (change_col < 0)
9323 {
9324 change_col = fpos.col; /* Column of first change */
9325 /* May have to adjust Insstart */
9326 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9327 Insstart.col = fpos.col;
9328 }
9329 }
9330 ++fpos.col;
9331 ++ptr;
9332 vcol += i;
9333 }
9334
9335 if (change_col >= 0)
9336 {
9337 int repl_off = 0;
9338
9339 /* Skip over the spaces we need. */
9340 while (vcol < want_vcol && *ptr == ' ')
9341 {
9342 vcol += lbr_chartabsize(ptr, vcol);
9343 ++ptr;
9344 ++repl_off;
9345 }
9346 if (vcol > want_vcol)
9347 {
9348 /* Must have a char with 'showbreak' just before it. */
9349 --ptr;
9350 --repl_off;
9351 }
9352 fpos.col += repl_off;
9353
9354 /* Delete following spaces. */
9355 i = cursor->col - fpos.col;
9356 if (i > 0)
9357 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009358 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 /* correct replace stack. */
9360 if ((State & REPLACE_FLAG)
9361#ifdef FEAT_VREPLACE
9362 && !(State & VREPLACE_FLAG)
9363#endif
9364 )
9365 for (temp = i; --temp >= 0; )
9366 replace_join(repl_off);
9367 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009368#ifdef FEAT_NETBEANS_INTG
9369 if (usingNetbeans)
9370 {
9371 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9372 (long)(i + 1));
9373 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9374 (char_u *)"\t", 1);
9375 }
9376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 cursor->col -= i;
9378
9379#ifdef FEAT_VREPLACE
9380 /*
9381 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9382 * backspacing over the changed spacing and then inserting the new
9383 * spacing.
9384 */
9385 if (State & VREPLACE_FLAG)
9386 {
9387 /* Backspace from real cursor to change_col */
9388 backspace_until_column(change_col);
9389
9390 /* Insert each char in saved_line from changed_col to
9391 * ptr-cursor */
9392 ins_bytes_len(saved_line + change_col,
9393 cursor->col - change_col);
9394 }
9395#endif
9396 }
9397
9398#ifdef FEAT_VREPLACE
9399 if (State & VREPLACE_FLAG)
9400 vim_free(saved_line);
9401#endif
9402 curwin->w_p_list = save_list;
9403 }
9404
9405 return FALSE;
9406}
9407
9408/*
9409 * Handle CR or NL in insert mode.
9410 * Return TRUE when out of memory or can't undo.
9411 */
9412 static int
9413ins_eol(c)
9414 int c;
9415{
9416 int i;
9417
9418 if (echeck_abbr(c + ABBR_OFF))
9419 return FALSE;
9420 if (stop_arrow() == FAIL)
9421 return TRUE;
9422 undisplay_dollar();
9423
9424 /*
9425 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9426 * character under the cursor. Only push a NUL on the replace stack,
9427 * nothing to put back when the NL is deleted.
9428 */
9429 if ((State & REPLACE_FLAG)
9430#ifdef FEAT_VREPLACE
9431 && !(State & VREPLACE_FLAG)
9432#endif
9433 )
9434 replace_push(NUL);
9435
9436 /*
9437 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9438 * replacing the next line, so we push all of the characters left on the
9439 * line onto the replace stack. This is not done here though, it is done
9440 * in open_line().
9441 */
9442
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009443#ifdef FEAT_VIRTUALEDIT
9444 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9445 * CTRL-O). */
9446 if (virtual_active() && curwin->w_cursor.coladd > 0)
9447 coladvance(getviscol());
9448#endif
9449
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#ifdef FEAT_RIGHTLEFT
9451# ifdef FEAT_FKMAP
9452 if (p_altkeymap && p_fkmap)
9453 fkmap(NL);
9454# endif
9455 /* NL in reverse insert will always start in the end of
9456 * current line. */
9457 if (revins_on)
9458 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9459#endif
9460
9461 AppendToRedobuff(NL_STR);
9462 i = open_line(FORWARD,
9463#ifdef FEAT_COMMENTS
9464 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9465#endif
9466 0, old_indent);
9467 old_indent = 0;
9468#ifdef FEAT_CINDENT
9469 can_cindent = TRUE;
9470#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009471#ifdef FEAT_FOLDING
9472 /* When inserting a line the cursor line must never be in a closed fold. */
9473 foldOpenCursor();
9474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475
9476 return (!i);
9477}
9478
9479#ifdef FEAT_DIGRAPHS
9480/*
9481 * Handle digraph in insert mode.
9482 * Returns character still to be inserted, or NUL when nothing remaining to be
9483 * done.
9484 */
9485 static int
9486ins_digraph()
9487{
9488 int c;
9489 int cc;
9490
9491 pc_status = PC_STATUS_UNSET;
9492 if (redrawing() && !char_avail())
9493 {
9494 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009495 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496
9497 edit_putchar('?', TRUE);
9498#ifdef FEAT_CMDL_INFO
9499 add_to_showcmd_c(Ctrl_K);
9500#endif
9501 }
9502
9503#ifdef USE_ON_FLY_SCROLL
9504 dont_scroll = TRUE; /* disallow scrolling here */
9505#endif
9506
9507 /* don't map the digraph chars. This also prevents the
9508 * mode message to be deleted when ESC is hit */
9509 ++no_mapping;
9510 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009511 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 --no_mapping;
9513 --allow_keys;
9514 if (IS_SPECIAL(c) || mod_mask) /* special key */
9515 {
9516#ifdef FEAT_CMDL_INFO
9517 clear_showcmd();
9518#endif
9519 insert_special(c, TRUE, FALSE);
9520 return NUL;
9521 }
9522 if (c != ESC)
9523 {
9524 if (redrawing() && !char_avail())
9525 {
9526 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009527 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 if (char2cells(c) == 1)
9530 {
9531 /* first remove the '?', otherwise it's restored when typing
9532 * an ESC next */
9533 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009534 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 edit_putchar(c, TRUE);
9536 }
9537#ifdef FEAT_CMDL_INFO
9538 add_to_showcmd_c(c);
9539#endif
9540 }
9541 ++no_mapping;
9542 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009543 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 --no_mapping;
9545 --allow_keys;
9546 if (cc != ESC)
9547 {
9548 AppendToRedobuff((char_u *)CTRL_V_STR);
9549 c = getdigraph(c, cc, TRUE);
9550#ifdef FEAT_CMDL_INFO
9551 clear_showcmd();
9552#endif
9553 return c;
9554 }
9555 }
9556 edit_unputchar();
9557#ifdef FEAT_CMDL_INFO
9558 clear_showcmd();
9559#endif
9560 return NUL;
9561}
9562#endif
9563
9564/*
9565 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9566 * Returns the char to be inserted, or NUL if none found.
9567 */
9568 static int
9569ins_copychar(lnum)
9570 linenr_T lnum;
9571{
9572 int c;
9573 int temp;
9574 char_u *ptr, *prev_ptr;
9575
9576 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9577 {
9578 vim_beep();
9579 return NUL;
9580 }
9581
9582 /* try to advance to the cursor column */
9583 temp = 0;
9584 ptr = ml_get(lnum);
9585 prev_ptr = ptr;
9586 validate_virtcol();
9587 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9588 {
9589 prev_ptr = ptr;
9590 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9591 }
9592 if ((colnr_T)temp > curwin->w_virtcol)
9593 ptr = prev_ptr;
9594
9595#ifdef FEAT_MBYTE
9596 c = (*mb_ptr2char)(ptr);
9597#else
9598 c = *ptr;
9599#endif
9600 if (c == NUL)
9601 vim_beep();
9602 return c;
9603}
9604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009605/*
9606 * CTRL-Y or CTRL-E typed in Insert mode.
9607 */
9608 static int
9609ins_ctrl_ey(tc)
9610 int tc;
9611{
9612 int c = tc;
9613
9614#ifdef FEAT_INS_EXPAND
9615 if (ctrl_x_mode == CTRL_X_SCROLL)
9616 {
9617 if (c == Ctrl_Y)
9618 scrolldown_clamp();
9619 else
9620 scrollup_clamp();
9621 redraw_later(VALID);
9622 }
9623 else
9624#endif
9625 {
9626 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9627 if (c != NUL)
9628 {
9629 long tw_save;
9630
9631 /* The character must be taken literally, insert like it
9632 * was typed after a CTRL-V, and pretend 'textwidth'
9633 * wasn't set. Digits, 'o' and 'x' are special after a
9634 * CTRL-V, don't use it for these. */
9635 if (c < 256 && !isalnum(c))
9636 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9637 tw_save = curbuf->b_p_tw;
9638 curbuf->b_p_tw = -1;
9639 insert_special(c, TRUE, FALSE);
9640 curbuf->b_p_tw = tw_save;
9641#ifdef FEAT_RIGHTLEFT
9642 revins_chars++;
9643 revins_legal++;
9644#endif
9645 c = Ctrl_V; /* pretend CTRL-V is last character */
9646 auto_format(FALSE, TRUE);
9647 }
9648 }
9649 return c;
9650}
9651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652#ifdef FEAT_SMARTINDENT
9653/*
9654 * Try to do some very smart auto-indenting.
9655 * Used when inserting a "normal" character.
9656 */
9657 static void
9658ins_try_si(c)
9659 int c;
9660{
9661 pos_T *pos, old_pos;
9662 char_u *ptr;
9663 int i;
9664 int temp;
9665
9666 /*
9667 * do some very smart indenting when entering '{' or '}'
9668 */
9669 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9670 {
9671 /*
9672 * for '}' set indent equal to indent of line containing matching '{'
9673 */
9674 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9675 {
9676 old_pos = curwin->w_cursor;
9677 /*
9678 * If the matching '{' has a ')' immediately before it (ignoring
9679 * white-space), then line up with the start of the line
9680 * containing the matching '(' if there is one. This handles the
9681 * case where an "if (..\n..) {" statement continues over multiple
9682 * lines -- webb
9683 */
9684 ptr = ml_get(pos->lnum);
9685 i = pos->col;
9686 if (i > 0) /* skip blanks before '{' */
9687 while (--i > 0 && vim_iswhite(ptr[i]))
9688 ;
9689 curwin->w_cursor.lnum = pos->lnum;
9690 curwin->w_cursor.col = i;
9691 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9692 curwin->w_cursor = *pos;
9693 i = get_indent();
9694 curwin->w_cursor = old_pos;
9695#ifdef FEAT_VREPLACE
9696 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009697 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 else
9699#endif
9700 (void)set_indent(i, SIN_CHANGED);
9701 }
9702 else if (curwin->w_cursor.col > 0)
9703 {
9704 /*
9705 * when inserting '{' after "O" reduce indent, but not
9706 * more than indent of previous line
9707 */
9708 temp = TRUE;
9709 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9710 {
9711 old_pos = curwin->w_cursor;
9712 i = get_indent();
9713 while (curwin->w_cursor.lnum > 1)
9714 {
9715 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9716
9717 /* ignore empty lines and lines starting with '#'. */
9718 if (*ptr != '#' && *ptr != NUL)
9719 break;
9720 }
9721 if (get_indent() >= i)
9722 temp = FALSE;
9723 curwin->w_cursor = old_pos;
9724 }
9725 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009726 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 }
9728 }
9729
9730 /*
9731 * set indent of '#' always to 0
9732 */
9733 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9734 {
9735 /* remember current indent for next line */
9736 old_indent = get_indent();
9737 (void)set_indent(0, SIN_CHANGED);
9738 }
9739
9740 /* Adjust ai_col, the char at this position can be deleted. */
9741 if (ai_col > curwin->w_cursor.col)
9742 ai_col = curwin->w_cursor.col;
9743}
9744#endif
9745
9746/*
9747 * Get the value that w_virtcol would have when 'list' is off.
9748 * Unless 'cpo' contains the 'L' flag.
9749 */
9750 static colnr_T
9751get_nolist_virtcol()
9752{
9753 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9754 return getvcol_nolist(&curwin->w_cursor);
9755 validate_virtcol();
9756 return curwin->w_virtcol;
9757}