blob: 841144957412a8d5812c1d30810dc33b62ca13cb [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 Moolenaar0ab2a882009-05-13 10:51:08 +000072#ifdef S_SPLINT_S /* splint can't handle array of pointers */
73 char_u **cp_text; /* text for the menu */
74#else
Bram Moolenaar39f05632006-03-19 22:15:26 +000075 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000076#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar572cb562005-08-05 21:35:02 +0000121static int compl_matches = 0;
122static char_u *compl_pattern = NULL;
123static int compl_direction = FORWARD;
124static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000125static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000126static pos_T compl_startpos;
127static colnr_T compl_col = 0; /* column where the text starts
128 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000129static char_u *compl_orig_text = NULL; /* text as it was before
130 * completion started */
131static int compl_cont_mode = 0;
132static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000134static void ins_ctrl_x __ARGS((void));
135static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000136static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000137static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000138static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000139static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000140static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000142static void ins_compl_upd_pum __ARGS((void));
143static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000144static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000145static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000146static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000147static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000148static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void ins_compl_free __ARGS((void));
150static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000151static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000152static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000153static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000154static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000155static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000156static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000158static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000160#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
161static void ins_compl_add_list __ARGS((list_T *list));
162#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000163static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void ins_compl_delete __ARGS((void));
165static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000167static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000169static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000170static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000172static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif /* FEAT_INS_EXPAND */
174
175#define BACKSPACE_CHAR 1
176#define BACKSPACE_WORD 2
177#define BACKSPACE_WORD_NOT_SPACE 3
178#define BACKSPACE_LINE 4
179
Bram Moolenaar754b5602006-02-09 23:53:20 +0000180static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void ins_ctrl_v __ARGS((void));
182static void undisplay_dollar __ARGS((void));
183static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000184static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void check_auto_format __ARGS((int));
186static void redo_literal __ARGS((int c));
187static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000188#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000189static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000190static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000191static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
194static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000195#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static int replace_pop __ARGS((void));
199static void replace_join __ARGS((int off));
200static void replace_pop_ins __ARGS((void));
201#ifdef FEAT_MBYTE
202static void mb_replace_pop_ins __ARGS((int cc));
203#endif
204static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000205static void replace_do_bs __ARGS((int limit_col));
206static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_CINDENT
208static int cindent_on __ARGS((void));
209#endif
210static void ins_reg __ARGS((void));
211static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000212static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000213static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_RIGHTLEFT
215static void ins_ctrl_ __ARGS((void));
216#endif
217#ifdef FEAT_VISUAL
218static int ins_start_select __ARGS((int c));
219#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000220static void ins_insert __ARGS((int replaceState));
221static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ins_shift __ARGS((int c, int lastc));
223static void ins_del __ARGS((void));
224static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
225#ifdef FEAT_MOUSE
226static void ins_mouse __ARGS((int c));
227static void ins_mousescroll __ARGS((int up));
228#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000229#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
230static void ins_tabline __ARGS((int c));
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232static void ins_left __ARGS((void));
233static void ins_home __ARGS((int c));
234static void ins_end __ARGS((int c));
235static void ins_s_left __ARGS((void));
236static void ins_right __ARGS((void));
237static void ins_s_right __ARGS((void));
238static void ins_up __ARGS((int startcol));
239static void ins_pageup __ARGS((void));
240static void ins_down __ARGS((int startcol));
241static void ins_pagedown __ARGS((void));
242#ifdef FEAT_DND
243static void ins_drop __ARGS((void));
244#endif
245static int ins_tab __ARGS((void));
246static int ins_eol __ARGS((int c));
247#ifdef FEAT_DIGRAPHS
248static int ins_digraph __ARGS((void));
249#endif
250static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000251static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_SMARTINDENT
253static void ins_try_si __ARGS((int c));
254#endif
255static colnr_T get_nolist_virtcol __ARGS((void));
256
257static colnr_T Insstart_textlen; /* length of line when insert started */
258static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
259
260static char_u *last_insert = NULL; /* the text of the previous insert,
261 K_SPECIAL and CSI are escaped */
262static int last_insert_skip; /* nr of chars in front of previous insert */
263static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000264static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266#ifdef FEAT_CINDENT
267static int can_cindent; /* may do cindenting on this line */
268#endif
269
270static int old_indent = 0; /* for ^^D command in insert mode */
271
272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000273static int revins_on; /* reverse insert mode on */
274static int revins_chars; /* how much to skip after edit */
275static int revins_legal; /* was the last char 'legal'? */
276static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static int ins_need_undo; /* call u_save() before inserting a
280 char. Set when edit() is called.
281 after that arrow_used is used. */
282
283static int did_add_space = FALSE; /* auto_format() added an extra space
284 under the cursor */
285
286/*
287 * edit(): Start inserting text.
288 *
289 * "cmdchar" can be:
290 * 'i' normal insert command
291 * 'a' normal append command
292 * 'R' replace command
293 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
294 * but still only one <CR> is inserted. The <Esc> is not used for redo.
295 * 'g' "gI" command.
296 * 'V' "gR" command for Virtual Replace mode.
297 * 'v' "gr" command for single character Virtual Replace mode.
298 *
299 * This function is not called recursively. For CTRL-O commands, it returns
300 * and lets the caller handle the Normal-mode command.
301 *
302 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
303 */
304 int
305edit(cmdchar, startln, count)
306 int cmdchar;
307 int startln; /* if set, insert at start of line */
308 long count;
309{
310 int c = 0;
311 char_u *ptr;
312 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000313 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 int i;
316 int did_backspace = TRUE; /* previous char was backspace */
317#ifdef FEAT_CINDENT
318 int line_is_white = FALSE; /* line is empty before insert */
319#endif
320 linenr_T old_topline = 0; /* topline before insertion */
321#ifdef FEAT_DIFF
322 int old_topfill = -1;
323#endif
324 int inserted_space = FALSE; /* just inserted a space */
325 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000326 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000328 /* Remember whether editing was restarted after CTRL-O. */
329 did_restart_edit = restart_edit;
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /* sleep before redrawing, needed for "CTRL-O :" that results in an
332 * error message */
333 check_for_delay(TRUE);
334
335#ifdef HAVE_SANDBOX
336 /* Don't allow inserting in the sandbox. */
337 if (sandbox != 0)
338 {
339 EMSG(_(e_sandbox));
340 return FALSE;
341 }
342#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000343 /* Don't allow changes in the buffer while editing the cmdline. The
344 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000345 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000346 {
347 EMSG(_(e_secure));
348 return FALSE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000352 /* Don't allow recursive insert mode when busy with completion. */
353 if (compl_started || pum_visible())
354 {
355 EMSG(_(e_secure));
356 return FALSE;
357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 ins_compl_clear(); /* clear stuff for CTRL-X mode */
359#endif
360
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361#ifdef FEAT_AUTOCMD
362 /*
363 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
364 */
365 if (cmdchar != 'r' && cmdchar != 'v')
366 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000367# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000368 if (cmdchar == 'R')
369 ptr = (char_u *)"r";
370 else if (cmdchar == 'V')
371 ptr = (char_u *)"v";
372 else
373 ptr = (char_u *)"i";
374 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000375# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000376 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
377 }
378#endif
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#ifdef FEAT_MOUSE
381 /*
382 * When doing a paste with the middle mouse button, Insstart is set to
383 * where the paste started.
384 */
385 if (where_paste_started.lnum != 0)
386 Insstart = where_paste_started;
387 else
388#endif
389 {
390 Insstart = curwin->w_cursor;
391 if (startln)
392 Insstart.col = 0;
393 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000394 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 Insstart_blank_vcol = MAXCOL;
396 if (!did_ai)
397 ai_col = 0;
398
399 if (cmdchar != NUL && restart_edit == 0)
400 {
401 ResetRedobuff();
402 AppendNumberToRedobuff(count);
403#ifdef FEAT_VREPLACE
404 if (cmdchar == 'V' || cmdchar == 'v')
405 {
406 /* "gR" or "gr" command */
407 AppendCharToRedobuff('g');
408 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
409 }
410 else
411#endif
412 {
413 AppendCharToRedobuff(cmdchar);
414 if (cmdchar == 'g') /* "gI" command */
415 AppendCharToRedobuff('I');
416 else if (cmdchar == 'r') /* "r<CR>" command */
417 count = 1; /* insert only one <CR> */
418 }
419 }
420
421 if (cmdchar == 'R')
422 {
423#ifdef FEAT_FKMAP
424 if (p_fkmap && p_ri)
425 {
426 beep_flush();
427 EMSG(farsi_text_3); /* encoded in Farsi */
428 State = INSERT;
429 }
430 else
431#endif
432 State = REPLACE;
433 }
434#ifdef FEAT_VREPLACE
435 else if (cmdchar == 'V' || cmdchar == 'v')
436 {
437 State = VREPLACE;
438 replaceState = VREPLACE;
439 orig_line_count = curbuf->b_ml.ml_line_count;
440 vr_lines_changed = 1;
441 }
442#endif
443 else
444 State = INSERT;
445
446 stop_insert_mode = FALSE;
447
448 /*
449 * Need to recompute the cursor position, it might move when the cursor is
450 * on a TAB or special character.
451 */
452 curs_columns(TRUE);
453
454 /*
455 * Enable langmap or IME, indicated by 'iminsert'.
456 * Note that IME may enabled/disabled without us noticing here, thus the
457 * 'iminsert' value may not reflect what is actually used. It is updated
458 * when hitting <Esc>.
459 */
460 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
461 State |= LANGMAP;
462#ifdef USE_IM_CONTROL
463 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#ifdef FEAT_MOUSE
467 setmouse();
468#endif
469#ifdef FEAT_CMDL_INFO
470 clear_showcmd();
471#endif
472#ifdef FEAT_RIGHTLEFT
473 /* there is no reverse replace mode */
474 revins_on = (State == INSERT && p_ri);
475 if (revins_on)
476 undisplay_dollar();
477 revins_chars = 0;
478 revins_legal = 0;
479 revins_scol = -1;
480#endif
481
482 /*
483 * Handle restarting Insert mode.
484 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
485 * restart_edit non-zero, and something in the stuff buffer.
486 */
487 if (restart_edit != 0 && stuff_empty())
488 {
489#ifdef FEAT_MOUSE
490 /*
491 * After a paste we consider text typed to be part of the insert for
492 * the pasted text. You can backspace over the pasted text too.
493 */
494 if (where_paste_started.lnum)
495 arrow_used = FALSE;
496 else
497#endif
498 arrow_used = TRUE;
499 restart_edit = 0;
500
501 /*
502 * If the cursor was after the end-of-line before the CTRL-O and it is
503 * now at the end-of-line, put it after the end-of-line (this is not
504 * correct in very rare cases).
505 * Also do this if curswant is greater than the current virtual
506 * column. Eg after "^O$" or "^O80|".
507 */
508 validate_virtcol();
509 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000510 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 || curwin->w_curswant > curwin->w_virtcol)
512 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
513 {
514 if (ptr[1] == NUL)
515 ++curwin->w_cursor.col;
516#ifdef FEAT_MBYTE
517 else if (has_mbyte)
518 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000519 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr[i] == NUL)
521 curwin->w_cursor.col += i;
522 }
523#endif
524 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000525 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527 else
528 arrow_used = FALSE;
529
530 /* we are in insert mode now, don't need to start it anymore */
531 need_start_insertmode = FALSE;
532
533 /* Need to save the line for undo before inserting the first char. */
534 ins_need_undo = TRUE;
535
536#ifdef FEAT_MOUSE
537 where_paste_started.lnum = 0;
538#endif
539#ifdef FEAT_CINDENT
540 can_cindent = TRUE;
541#endif
542#ifdef FEAT_FOLDING
543 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
544 * restarting. */
545 if (!p_im && did_restart_edit == 0)
546 foldOpenCursor();
547#endif
548
549 /*
550 * If 'showmode' is set, show the current (insert/replace/..) mode.
551 * A warning message for changing a readonly file is given here, before
552 * actually changing anything. It's put after the mode, if any.
553 */
554 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000555 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 i = showmode();
557
558 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000559 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561#ifdef CURSOR_SHAPE
562 ui_cursor_shape(); /* may show different cursor shape */
563#endif
564#ifdef FEAT_DIGRAPHS
565 do_digraph(-1); /* clear digraphs */
566#endif
567
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000568 /*
569 * Get the current length of the redo buffer, those characters have to be
570 * skipped if we want to get to the inserted characters.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ptr = get_inserted();
573 if (ptr == NULL)
574 new_insert_skip = 0;
575 else
576 {
577 new_insert_skip = (int)STRLEN(ptr);
578 vim_free(ptr);
579 }
580
581 old_indent = 0;
582
583 /*
584 * Main loop in Insert mode: repeat until Insert mode is left.
585 */
586 for (;;)
587 {
588#ifdef FEAT_RIGHTLEFT
589 if (!revins_legal)
590 revins_scol = -1; /* reset on illegal motions */
591 else
592 revins_legal = 0;
593#endif
594 if (arrow_used) /* don't repeat insert when arrow key used */
595 count = 0;
596
597 if (stop_insert_mode)
598 {
599 /* ":stopinsert" used or 'insertmode' reset */
600 count = 0;
601 goto doESCkey;
602 }
603
604 /* set curwin->w_curswant for next K_DOWN or K_UP */
605 if (!arrow_used)
606 curwin->w_set_curswant = TRUE;
607
608 /* If there is no typeahead may check for timestamps (e.g., for when a
609 * menu invoked a shell command). */
610 if (stuff_empty())
611 {
612 did_check_timestamps = FALSE;
613 if (need_check_timestamps)
614 check_timestamps(FALSE);
615 }
616
617 /*
618 * When emsg() was called msg_scroll will have been set.
619 */
620 msg_scroll = FALSE;
621
622#ifdef FEAT_GUI
623 /* When 'mousefocus' is set a mouse movement may have taken us to
624 * another window. "need_mouse_correct" may then be set because of an
625 * autocommand. */
626 if (need_mouse_correct)
627 gui_mouse_correct();
628#endif
629
630#ifdef FEAT_FOLDING
631 /* Open fold at the cursor line, according to 'foldopen'. */
632 if (fdo_flags & FDO_INSERT)
633 foldOpenCursor();
634 /* Close folds where the cursor isn't, according to 'foldclose' */
635 if (!char_avail())
636 foldCheckClose();
637#endif
638
639 /*
640 * If we inserted a character at the last position of the last line in
641 * the window, scroll the window one line up. This avoids an extra
642 * redraw.
643 * This is detected when the cursor column is smaller after inserting
644 * something.
645 * Don't do this when the topline changed already, it has
646 * already been adjusted (by insertchar() calling open_line())).
647 */
648 if (curbuf->b_mod_set
649 && curwin->w_p_wrap
650 && !did_backspace
651 && curwin->w_topline == old_topline
652#ifdef FEAT_DIFF
653 && curwin->w_topfill == old_topfill
654#endif
655 )
656 {
657 mincol = curwin->w_wcol;
658 validate_cursor_col();
659
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000660 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 && curwin->w_wrow == W_WINROW(curwin)
662 + curwin->w_height - 1 - p_so
663 && (curwin->w_cursor.lnum != curwin->w_topline
664#ifdef FEAT_DIFF
665 || curwin->w_topfill > 0
666#endif
667 ))
668 {
669#ifdef FEAT_DIFF
670 if (curwin->w_topfill > 0)
671 --curwin->w_topfill;
672 else
673#endif
674#ifdef FEAT_FOLDING
675 if (hasFolding(curwin->w_topline, NULL, &old_topline))
676 set_topline(curwin, old_topline + 1);
677 else
678#endif
679 set_topline(curwin, curwin->w_topline + 1);
680 }
681 }
682
683 /* May need to adjust w_topline to show the cursor. */
684 update_topline();
685
686 did_backspace = FALSE;
687
688 validate_cursor(); /* may set must_redraw */
689
690 /*
691 * Redraw the display when no characters are waiting.
692 * Also shows mode, ruler and positions cursor.
693 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000694 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696#ifdef FEAT_SCROLLBIND
697 if (curwin->w_p_scb)
698 do_check_scrollbind(TRUE);
699#endif
700
701 update_curswant();
702 old_topline = curwin->w_topline;
703#ifdef FEAT_DIFF
704 old_topfill = curwin->w_topfill;
705#endif
706
707#ifdef USE_ON_FLY_SCROLL
708 dont_scroll = FALSE; /* allow scrolling here */
709#endif
710
711 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000712 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 */
714 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000715 do
716 {
717 c = safe_vgetc();
718 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000720#ifdef FEAT_AUTOCMD
721 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
722 did_cursorhold = TRUE;
723#endif
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_RIGHTLEFT
726 if (p_hkmap && KeyTyped)
727 c = hkmap(c); /* Hebrew mode mapping */
728#endif
729#ifdef FEAT_FKMAP
730 if (p_fkmap && KeyTyped)
731 c = fkmap(c); /* Farsi mode mapping */
732#endif
733
734#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 /*
736 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000737 * and the cursor is still in the completed word. Only when there is
738 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000739 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000740 if (compl_started
741 && pum_wanted()
742 && curwin->w_cursor.col >= compl_col
743 && (compl_shown_match == NULL
744 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000745 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /* BS: Delete one character from "compl_leader". */
747 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000748 && curwin->w_cursor.col > compl_col
749 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000750 continue;
751
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000752 /* When no match was selected or it was edited. */
753 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000754 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000755 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000756 * "compl_leader". Except when at the original match and
757 * there is nothing to add, CTRL-L works like CTRL-P then. */
758 if (c == Ctrl_L
759 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000760 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000761 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000762 {
763 ins_compl_addfrommatch();
764 continue;
765 }
766
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000767 /* A non-white character that fits in with the current
768 * completion: Add to "compl_leader". */
769 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000770 {
771 ins_compl_addleader(c);
772 continue;
773 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000774
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000775 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000776 * compl_enter_selects is set the Enter key does the same. */
777 if (c == Ctrl_Y || (compl_enter_selects
778 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000779 {
780 ins_compl_delete();
781 ins_compl_insert();
782 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000783 }
784 }
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
787 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000788 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000789 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000790 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#endif
792
Bram Moolenaar488c6512005-08-11 20:09:58 +0000793 /* CTRL-\ CTRL-N goes to Normal mode,
794 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
795 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (c == Ctrl_BSL)
797 {
798 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000799 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 ++no_mapping;
801 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000802 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 --no_mapping;
804 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000805 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000807 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 vungetc(c);
809 c = Ctrl_BSL;
810 }
811 else if (c == Ctrl_G && p_im)
812 continue;
813 else
814 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000815 if (c == Ctrl_O)
816 {
817 ins_ctrl_o();
818 ins_at_eol = FALSE; /* cursor keeps its column */
819 nomove = TRUE;
820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 count = 0;
822 goto doESCkey;
823 }
824 }
825
826#ifdef FEAT_DIGRAPHS
827 c = do_digraph(c);
828#endif
829
830#ifdef FEAT_INS_EXPAND
831 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
832 goto docomplete;
833#endif
834 if (c == Ctrl_V || c == Ctrl_Q)
835 {
836 ins_ctrl_v();
837 c = Ctrl_V; /* pretend CTRL-V is last typed character */
838 continue;
839 }
840
841#ifdef FEAT_CINDENT
842 if (cindent_on()
843# ifdef FEAT_INS_EXPAND
844 && ctrl_x_mode == 0
845# endif
846 )
847 {
848 /* A key name preceded by a bang means this key is not to be
849 * inserted. Skip ahead to the re-indenting below.
850 * A key name preceded by a star means that indenting has to be
851 * done before inserting the key. */
852 line_is_white = inindent(0);
853 if (in_cinkeys(c, '!', line_is_white))
854 goto force_cindent;
855 if (can_cindent && in_cinkeys(c, '*', line_is_white)
856 && stop_arrow() == OK)
857 do_c_expr_indent();
858 }
859#endif
860
861#ifdef FEAT_RIGHTLEFT
862 if (curwin->w_p_rl)
863 switch (c)
864 {
865 case K_LEFT: c = K_RIGHT; break;
866 case K_S_LEFT: c = K_S_RIGHT; break;
867 case K_C_LEFT: c = K_C_RIGHT; break;
868 case K_RIGHT: c = K_LEFT; break;
869 case K_S_RIGHT: c = K_S_LEFT; break;
870 case K_C_RIGHT: c = K_C_LEFT; break;
871 }
872#endif
873
874#ifdef FEAT_VISUAL
875 /*
876 * If 'keymodel' contains "startsel", may start selection. If it
877 * does, a CTRL-O and c will be stuffed, we need to get these
878 * characters.
879 */
880 if (ins_start_select(c))
881 continue;
882#endif
883
884 /*
885 * The big switch to handle a character in insert mode.
886 */
887 switch (c)
888 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000889 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (echeck_abbr(ESC + ABBR_OFF))
891 break;
892 /*FALLTHROUGH*/
893
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000894 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#ifdef FEAT_CMDWIN
896 if (c == Ctrl_C && cmdwin_type != 0)
897 {
898 /* Close the cmdline window. */
899 cmdwin_result = K_IGNORE;
900 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000901 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 goto doESCkey;
903 }
904#endif
905
906#ifdef UNIX
907do_intr:
908#endif
909 /* when 'insertmode' set, and not halfway a mapping, don't leave
910 * Insert mode */
911 if (goto_im())
912 {
913 if (got_int)
914 {
915 (void)vgetc(); /* flush all buffers */
916 got_int = FALSE;
917 }
918 else
919 vim_beep();
920 break;
921 }
922doESCkey:
923 /*
924 * This is the ONLY return from edit()!
925 */
926 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
927 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000928 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 o_lnum = curwin->w_cursor.lnum;
930
Bram Moolenaar488c6512005-08-11 20:09:58 +0000931 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000932 {
933#ifdef FEAT_AUTOCMD
934 if (cmdchar != 'r' && cmdchar != 'v')
935 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
936 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000937 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 continue;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_Z: /* suspend when 'insertmode' set */
944 if (!p_im)
945 goto normalchar; /* insert CTRL-Z as normal char */
946 stuffReadbuff((char_u *)":st\r");
947 c = Ctrl_O;
948 /*FALLTHROUGH*/
949
950 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000951#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000952 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 goto docomplete;
954#endif
955 if (echeck_abbr(Ctrl_O + ABBR_OFF))
956 break;
957 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000958
959#ifdef FEAT_VIRTUALEDIT
960 /* don't move the cursor left when 'virtualedit' has "onemore". */
961 if (ve_flags & VE_ONEMORE)
962 {
963 ins_at_eol = FALSE;
964 nomove = TRUE;
965 }
966#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000967 count = 0;
968 goto doESCkey;
969
Bram Moolenaar572cb562005-08-05 21:35:02 +0000970 case K_INS: /* toggle insert/replace mode */
971 case K_KINS:
972 ins_insert(replaceState);
973 break;
974
975 case K_SELECT: /* end of Select mode mapping - ignore */
976 break;
977
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978#ifdef FEAT_SNIFF
979 case K_SNIFF: /* Sniff command received */
980 stuffcharReadbuff(K_SNIFF);
981 goto doESCkey;
982#endif
983
984 case K_HELP: /* Help key works like <ESC> <Help> */
985 case K_F1:
986 case K_XF1:
987 stuffcharReadbuff(K_HELP);
988 if (p_im)
989 need_start_insertmode = TRUE;
990 goto doESCkey;
991
992#ifdef FEAT_NETBEANS_INTG
993 case K_F21: /* NetBeans command */
994 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000995 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000996 --no_mapping;
997 netbeans_keycommand(i);
998 break;
999#endif
1000
1001 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case NUL:
1003 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 /* For ^@ the trailing ESC will end the insert, unless there is an
1005 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1007 && c != Ctrl_A && !p_im)
1008 goto doESCkey; /* quit insert mode */
1009 inserted_space = FALSE;
1010 break;
1011
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001012 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 ins_reg();
1014 auto_format(FALSE, TRUE);
1015 inserted_space = FALSE;
1016 break;
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 ins_ctrl_g();
1020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_HAT: /* switch input mode and/or langmap */
1023 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 break;
1025
1026#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (!p_ari)
1029 goto normalchar;
1030 ins_ctrl_();
1031 break;
1032#endif
1033
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1036 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1037 goto docomplete;
1038#endif
1039 /* FALLTHROUGH */
1040
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042# ifdef FEAT_INS_EXPAND
1043 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1044 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 if (has_compl_option(FALSE))
1046 goto docomplete;
1047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049# endif
1050 ins_shift(c, lastc);
1051 auto_format(FALSE, TRUE);
1052 inserted_space = FALSE;
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 case K_KDEL:
1057 ins_del();
1058 auto_format(FALSE, TRUE);
1059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case Ctrl_H:
1063 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1064 auto_format(FALSE, TRUE);
1065 break;
1066
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1069 auto_format(FALSE, TRUE);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001073# ifdef FEAT_COMPL_FUNC
1074 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001076 goto docomplete;
1077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1079 auto_format(FALSE, TRUE);
1080 inserted_space = FALSE;
1081 break;
1082
1083#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 case K_LEFTMOUSE_NM:
1086 case K_LEFTDRAG:
1087 case K_LEFTRELEASE:
1088 case K_LEFTRELEASE_NM:
1089 case K_MIDDLEMOUSE:
1090 case K_MIDDLEDRAG:
1091 case K_MIDDLERELEASE:
1092 case K_RIGHTMOUSE:
1093 case K_RIGHTDRAG:
1094 case K_RIGHTRELEASE:
1095 case K_X1MOUSE:
1096 case K_X1DRAG:
1097 case K_X1RELEASE:
1098 case K_X2MOUSE:
1099 case K_X2DRAG:
1100 case K_X2RELEASE:
1101 ins_mouse(c);
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ins_mousescroll(FALSE);
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_mousescroll(TRUE);
1110 break;
1111#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001112#ifdef FEAT_GUI_TABLINE
1113 case K_TABLINE:
1114 case K_TABMENU:
1115 ins_tabline(c);
1116 break;
1117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar754b5602006-02-09 23:53:20 +00001122#ifdef FEAT_AUTOCMD
1123 case K_CURSORHOLD: /* Didn't type something for a while. */
1124 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1125 did_cursorhold = TRUE;
1126 break;
1127#endif
1128
Bram Moolenaar4770d092006-01-12 23:22:24 +00001129#ifdef FEAT_GUI_W32
1130 /* On Win32 ignore <M-F4>, we get it when closing the window was
1131 * cancelled. */
1132 case K_F4:
1133 if (mod_mask != MOD_MASK_ALT)
1134 goto normalchar;
1135 break;
1136#endif
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_GUI
1139 case K_VER_SCROLLBAR:
1140 ins_scroll();
1141 break;
1142
1143 case K_HOR_SCROLLBAR:
1144 ins_horscroll();
1145 break;
1146#endif
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 case K_S_HOME:
1151 case K_C_HOME:
1152 ins_home(c);
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 case K_S_END:
1158 case K_C_END:
1159 ins_end(c);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001163 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1164 ins_s_left();
1165 else
1166 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case K_C_LEFT:
1171 ins_s_left();
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001175 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1176 ins_s_right();
1177 else
1178 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 case K_C_RIGHT:
1183 ins_s_right();
1184 break;
1185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001186 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001187#ifdef FEAT_INS_EXPAND
1188 if (pum_visible())
1189 goto docomplete;
1190#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001191 if (mod_mask & MOD_MASK_SHIFT)
1192 ins_pageup();
1193 else
1194 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 break;
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 case K_PAGEUP:
1199 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001200#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001201 if (pum_visible())
1202 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 ins_pageup();
1205 break;
1206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001207 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001208#ifdef FEAT_INS_EXPAND
1209 if (pum_visible())
1210 goto docomplete;
1211#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001212 if (mod_mask & MOD_MASK_SHIFT)
1213 ins_pagedown();
1214 else
1215 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 break;
1217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001218 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_PAGEDOWN:
1220 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001221#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001222 if (pum_visible())
1223 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 ins_pagedown();
1226 break;
1227
1228#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ins_drop();
1231 break;
1232#endif
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 c = TAB;
1236 /* FALLTHROUGH */
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1240 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1241 goto docomplete;
1242#endif
1243 inserted_space = FALSE;
1244 if (ins_tab())
1245 goto normalchar; /* insert TAB as a normal char */
1246 auto_format(FALSE, TRUE);
1247 break;
1248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001249 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 c = CAR;
1251 /* FALLTHROUGH */
1252 case CAR:
1253 case NL:
1254#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1255 /* In a quickfix window a <CR> jumps to the error under the
1256 * cursor. */
1257 if (bt_quickfix(curbuf) && c == CAR)
1258 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001259 if (curwin->w_llist_ref == NULL) /* quickfix window */
1260 do_cmdline_cmd((char_u *)".cc");
1261 else /* location list window */
1262 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 break;
1264 }
1265#endif
1266#ifdef FEAT_CMDWIN
1267 if (cmdwin_type != 0)
1268 {
1269 /* Execute the command in the cmdline window. */
1270 cmdwin_result = CAR;
1271 goto doESCkey;
1272 }
1273#endif
1274 if (ins_eol(c) && !p_im)
1275 goto doESCkey; /* out of memory */
1276 auto_format(FALSE, FALSE);
1277 inserted_space = FALSE;
1278 break;
1279
1280#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282# ifdef FEAT_INS_EXPAND
1283 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1284 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 if (has_compl_option(TRUE))
1286 goto docomplete;
1287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289# endif
1290# ifdef FEAT_DIGRAPHS
1291 c = ins_digraph();
1292 if (c == NUL)
1293 break;
1294# endif
1295 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001299 case Ctrl_X: /* Enter CTRL-X mode */
1300 ins_ctrl_x();
1301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (ctrl_x_mode != CTRL_X_TAGS)
1305 goto normalchar;
1306 goto docomplete;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (ctrl_x_mode != CTRL_X_FILES)
1310 goto normalchar;
1311 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001312
1313 case 's': /* Spelling completion after ^X */
1314 case Ctrl_S:
1315 if (ctrl_x_mode != CTRL_X_SPELL)
1316 goto normalchar;
1317 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#endif
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#ifdef FEAT_INS_EXPAND
1322 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1323#endif
1324 {
1325 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1326 if (p_im)
1327 {
1328 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1329 break;
1330 goto doESCkey;
1331 }
1332 goto normalchar;
1333 }
1334#ifdef FEAT_INS_EXPAND
1335 /* FALLTHROUGH */
1336
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 case Ctrl_N:
1339 /* if 'complete' is empty then plain ^P is no longer special,
1340 * but it is under other ^X modes */
1341 if (*curbuf->b_p_cpt == NUL
1342 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 goto normalchar;
1345
1346docomplete:
1347 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 break;
1350#endif /* FEAT_INS_EXPAND */
1351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001352 case Ctrl_Y: /* copy from previous line or scroll down */
1353 case Ctrl_E: /* copy from next line or scroll up */
1354 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 break;
1356
1357 default:
1358#ifdef UNIX
1359 if (c == intr_char) /* special interrupt char */
1360 goto do_intr;
1361#endif
1362
1363 /*
1364 * Insert a nomal character.
1365 */
1366normalchar:
1367#ifdef FEAT_SMARTINDENT
1368 /* Try to perform smart-indenting. */
1369 ins_try_si(c);
1370#endif
1371
1372 if (c == ' ')
1373 {
1374 inserted_space = TRUE;
1375#ifdef FEAT_CINDENT
1376 if (inindent(0))
1377 can_cindent = FALSE;
1378#endif
1379 if (Insstart_blank_vcol == MAXCOL
1380 && curwin->w_cursor.lnum == Insstart.lnum)
1381 Insstart_blank_vcol = get_nolist_virtcol();
1382 }
1383
1384 if (vim_iswordc(c) || !echeck_abbr(
1385#ifdef FEAT_MBYTE
1386 /* Add ABBR_OFF for characters above 0x100, this is
1387 * what check_abbr() expects. */
1388 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1389#endif
1390 c))
1391 {
1392 insert_special(c, FALSE, FALSE);
1393#ifdef FEAT_RIGHTLEFT
1394 revins_legal++;
1395 revins_chars++;
1396#endif
1397 }
1398
1399 auto_format(FALSE, TRUE);
1400
1401#ifdef FEAT_FOLDING
1402 /* When inserting a character the cursor line must never be in a
1403 * closed fold. */
1404 foldOpenCursor();
1405#endif
1406 break;
1407 } /* end of switch (c) */
1408
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001409#ifdef FEAT_AUTOCMD
1410 /* If typed something may trigger CursorHoldI again. */
1411 if (c != K_CURSORHOLD)
1412 did_cursorhold = FALSE;
1413#endif
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /* If the cursor was moved we didn't just insert a space */
1416 if (arrow_used)
1417 inserted_space = FALSE;
1418
1419#ifdef FEAT_CINDENT
1420 if (can_cindent && cindent_on()
1421# ifdef FEAT_INS_EXPAND
1422 && ctrl_x_mode == 0
1423# endif
1424 )
1425 {
1426force_cindent:
1427 /*
1428 * Indent now if a key was typed that is in 'cinkeys'.
1429 */
1430 if (in_cinkeys(c, ' ', line_is_white))
1431 {
1432 if (stop_arrow() == OK)
1433 /* re-indent the current line */
1434 do_c_expr_indent();
1435 }
1436 }
1437#endif /* FEAT_CINDENT */
1438
1439 } /* for (;;) */
1440 /* NOTREACHED */
1441}
1442
1443/*
1444 * Redraw for Insert mode.
1445 * This is postponed until getting the next character to make '$' in the 'cpo'
1446 * option work correctly.
1447 * Only redraw when there are no characters available. This speeds up
1448 * inserting sequences of characters (e.g., for CTRL-R).
1449 */
1450 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001451ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001452 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 if (!char_avail())
1455 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001456#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001457 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1458 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001459 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001460 && !equalpos(last_cursormoved, curwin->w_cursor)
1461# ifdef FEAT_INS_EXPAND
1462 && !pum_visible()
1463# endif
1464 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001465 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001466# ifdef FEAT_SYN_HL
1467 /* Need to update the screen first, to make sure syntax
1468 * highlighting is correct after making a change (e.g., inserting
1469 * a "(". The autocommand may also require a redraw, so it's done
1470 * again below, unfortunately. */
1471 if (syntax_present(curbuf) && must_redraw)
1472 update_screen(0);
1473# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001474 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1475 last_cursormoved = curwin->w_cursor;
1476 }
1477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (must_redraw)
1479 update_screen(0);
1480 else if (clear_cmdline || redraw_cmdline)
1481 showmode(); /* clear cmdline and show mode */
1482 showruler(FALSE);
1483 setcursor();
1484 emsg_on_display = FALSE; /* may remove error message now */
1485 }
1486}
1487
1488/*
1489 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1490 */
1491 static void
1492ins_ctrl_v()
1493{
1494 int c;
1495
1496 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001497 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498
1499 if (redrawing() && !char_avail())
1500 edit_putchar('^', TRUE);
1501 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1502
1503#ifdef FEAT_CMDL_INFO
1504 add_to_showcmd_c(Ctrl_V);
1505#endif
1506
1507 c = get_literal();
1508#ifdef FEAT_CMDL_INFO
1509 clear_showcmd();
1510#endif
1511 insert_special(c, FALSE, TRUE);
1512#ifdef FEAT_RIGHTLEFT
1513 revins_chars++;
1514 revins_legal++;
1515#endif
1516}
1517
1518/*
1519 * Put a character directly onto the screen. It's not stored in a buffer.
1520 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1521 */
1522static int pc_status;
1523#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1524#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1525#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1526#define PC_STATUS_SET 3 /* pc_bytes was filled */
1527#ifdef FEAT_MBYTE
1528static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1529#else
1530static char_u pc_bytes[2]; /* saved bytes */
1531#endif
1532static int pc_attr;
1533static int pc_row;
1534static int pc_col;
1535
1536 void
1537edit_putchar(c, highlight)
1538 int c;
1539 int highlight;
1540{
1541 int attr;
1542
1543 if (ScreenLines != NULL)
1544 {
1545 update_topline(); /* just in case w_topline isn't valid */
1546 validate_cursor();
1547 if (highlight)
1548 attr = hl_attr(HLF_8);
1549 else
1550 attr = 0;
1551 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1552 pc_col = W_WINCOL(curwin);
1553#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1554 pc_status = PC_STATUS_UNSET;
1555#endif
1556#ifdef FEAT_RIGHTLEFT
1557 if (curwin->w_p_rl)
1558 {
1559 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1560# ifdef FEAT_MBYTE
1561 if (has_mbyte)
1562 {
1563 int fix_col = mb_fix_col(pc_col, pc_row);
1564
1565 if (fix_col != pc_col)
1566 {
1567 screen_putchar(' ', pc_row, fix_col, attr);
1568 --curwin->w_wcol;
1569 pc_status = PC_STATUS_RIGHT;
1570 }
1571 }
1572# endif
1573 }
1574 else
1575#endif
1576 {
1577 pc_col += curwin->w_wcol;
1578#ifdef FEAT_MBYTE
1579 if (mb_lefthalve(pc_row, pc_col))
1580 pc_status = PC_STATUS_LEFT;
1581#endif
1582 }
1583
1584 /* save the character to be able to put it back */
1585#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1586 if (pc_status == PC_STATUS_UNSET)
1587#endif
1588 {
1589 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1590 pc_status = PC_STATUS_SET;
1591 }
1592 screen_putchar(c, pc_row, pc_col, attr);
1593 }
1594}
1595
1596/*
1597 * Undo the previous edit_putchar().
1598 */
1599 void
1600edit_unputchar()
1601{
1602 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1603 {
1604#if defined(FEAT_MBYTE)
1605 if (pc_status == PC_STATUS_RIGHT)
1606 ++curwin->w_wcol;
1607 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1608 redrawWinline(curwin->w_cursor.lnum, FALSE);
1609 else
1610#endif
1611 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1612 }
1613}
1614
1615/*
1616 * Called when p_dollar is set: display a '$' at the end of the changed text
1617 * Only works when cursor is in the line that changes.
1618 */
1619 void
1620display_dollar(col)
1621 colnr_T col;
1622{
1623 colnr_T save_col;
1624
1625 if (!redrawing())
1626 return;
1627
1628 cursor_off();
1629 save_col = curwin->w_cursor.col;
1630 curwin->w_cursor.col = col;
1631#ifdef FEAT_MBYTE
1632 if (has_mbyte)
1633 {
1634 char_u *p;
1635
1636 /* If on the last byte of a multi-byte move to the first byte. */
1637 p = ml_get_curline();
1638 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1639 }
1640#endif
1641 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1642 if (curwin->w_wcol < W_WIDTH(curwin))
1643 {
1644 edit_putchar('$', FALSE);
1645 dollar_vcol = curwin->w_virtcol;
1646 }
1647 curwin->w_cursor.col = save_col;
1648}
1649
1650/*
1651 * Call this function before moving the cursor from the normal insert position
1652 * in insert mode.
1653 */
1654 static void
1655undisplay_dollar()
1656{
1657 if (dollar_vcol)
1658 {
1659 dollar_vcol = 0;
1660 redrawWinline(curwin->w_cursor.lnum, FALSE);
1661 }
1662}
1663
1664/*
1665 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1666 * Keep the cursor on the same character.
1667 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1668 * type == INDENT_DEC decrease indent (for CTRL-D)
1669 * type == INDENT_SET set indent to "amount"
1670 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1671 */
1672 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001673change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 int type;
1675 int amount;
1676 int round;
1677 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001678 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 int vcol;
1681 int last_vcol;
1682 int insstart_less; /* reduction for Insstart.col */
1683 int new_cursor_col;
1684 int i;
1685 char_u *ptr;
1686 int save_p_list;
1687 int start_col;
1688 colnr_T vc;
1689#ifdef FEAT_VREPLACE
1690 colnr_T orig_col = 0; /* init for GCC */
1691 char_u *new_line, *orig_line = NULL; /* init for GCC */
1692
1693 /* VREPLACE mode needs to know what the line was like before changing */
1694 if (State & VREPLACE_FLAG)
1695 {
1696 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1697 orig_col = curwin->w_cursor.col;
1698 }
1699#endif
1700
1701 /* for the following tricks we don't want list mode */
1702 save_p_list = curwin->w_p_list;
1703 curwin->w_p_list = FALSE;
1704 vc = getvcol_nolist(&curwin->w_cursor);
1705 vcol = vc;
1706
1707 /*
1708 * For Replace mode we need to fix the replace stack later, which is only
1709 * possible when the cursor is in the indent. Remember the number of
1710 * characters before the cursor if it's possible.
1711 */
1712 start_col = curwin->w_cursor.col;
1713
1714 /* determine offset from first non-blank */
1715 new_cursor_col = curwin->w_cursor.col;
1716 beginline(BL_WHITE);
1717 new_cursor_col -= curwin->w_cursor.col;
1718
1719 insstart_less = curwin->w_cursor.col;
1720
1721 /*
1722 * If the cursor is in the indent, compute how many screen columns the
1723 * cursor is to the left of the first non-blank.
1724 */
1725 if (new_cursor_col < 0)
1726 vcol = get_indent() - vcol;
1727
1728 if (new_cursor_col > 0) /* can't fix replace stack */
1729 start_col = -1;
1730
1731 /*
1732 * Set the new indent. The cursor will be put on the first non-blank.
1733 */
1734 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001735 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 else
1737 {
1738#ifdef FEAT_VREPLACE
1739 int save_State = State;
1740
1741 /* Avoid being called recursively. */
1742 if (State & VREPLACE_FLAG)
1743 State = INSERT;
1744#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001745 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#ifdef FEAT_VREPLACE
1747 State = save_State;
1748#endif
1749 }
1750 insstart_less -= curwin->w_cursor.col;
1751
1752 /*
1753 * Try to put cursor on same character.
1754 * If the cursor is at or after the first non-blank in the line,
1755 * compute the cursor column relative to the column of the first
1756 * non-blank character.
1757 * If we are not in insert mode, leave the cursor on the first non-blank.
1758 * If the cursor is before the first non-blank, position it relative
1759 * to the first non-blank, counted in screen columns.
1760 */
1761 if (new_cursor_col >= 0)
1762 {
1763 /*
1764 * When changing the indent while the cursor is touching it, reset
1765 * Insstart_col to 0.
1766 */
1767 if (new_cursor_col == 0)
1768 insstart_less = MAXCOL;
1769 new_cursor_col += curwin->w_cursor.col;
1770 }
1771 else if (!(State & INSERT))
1772 new_cursor_col = curwin->w_cursor.col;
1773 else
1774 {
1775 /*
1776 * Compute the screen column where the cursor should be.
1777 */
1778 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001779 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 /*
1782 * Advance the cursor until we reach the right screen column.
1783 */
1784 vcol = last_vcol = 0;
1785 new_cursor_col = -1;
1786 ptr = ml_get_curline();
1787 while (vcol <= (int)curwin->w_virtcol)
1788 {
1789 last_vcol = vcol;
1790#ifdef FEAT_MBYTE
1791 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001792 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 else
1794#endif
1795 ++new_cursor_col;
1796 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1797 }
1798 vcol = last_vcol;
1799
1800 /*
1801 * May need to insert spaces to be able to position the cursor on
1802 * the right screen column.
1803 */
1804 if (vcol != (int)curwin->w_virtcol)
1805 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001806 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001808 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (ptr != NULL)
1810 {
1811 new_cursor_col += i;
1812 ptr[i] = NUL;
1813 while (--i >= 0)
1814 ptr[i] = ' ';
1815 ins_str(ptr);
1816 vim_free(ptr);
1817 }
1818 }
1819
1820 /*
1821 * When changing the indent while the cursor is in it, reset
1822 * Insstart_col to 0.
1823 */
1824 insstart_less = MAXCOL;
1825 }
1826
1827 curwin->w_p_list = save_p_list;
1828
1829 if (new_cursor_col <= 0)
1830 curwin->w_cursor.col = 0;
1831 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001832 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 curwin->w_set_curswant = TRUE;
1834 changed_cline_bef_curs();
1835
1836 /*
1837 * May have to adjust the start of the insert.
1838 */
1839 if (State & INSERT)
1840 {
1841 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1842 {
1843 if ((int)Insstart.col <= insstart_less)
1844 Insstart.col = 0;
1845 else
1846 Insstart.col -= insstart_less;
1847 }
1848 if ((int)ai_col <= insstart_less)
1849 ai_col = 0;
1850 else
1851 ai_col -= insstart_less;
1852 }
1853
1854 /*
1855 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1856 * If the number of characters before the cursor decreased, need to pop a
1857 * few characters from the replace stack.
1858 * If the number of characters before the cursor increased, need to push a
1859 * few NULs onto the replace stack.
1860 */
1861 if (REPLACE_NORMAL(State) && start_col >= 0)
1862 {
1863 while (start_col > (int)curwin->w_cursor.col)
1864 {
1865 replace_join(0); /* remove a NUL from the replace stack */
1866 --start_col;
1867 }
1868 while (start_col < (int)curwin->w_cursor.col || replaced)
1869 {
1870 replace_push(NUL);
1871 if (replaced)
1872 {
1873 replace_push(replaced);
1874 replaced = NUL;
1875 }
1876 ++start_col;
1877 }
1878 }
1879
1880#ifdef FEAT_VREPLACE
1881 /*
1882 * For VREPLACE mode, we also have to fix the replace stack. In this case
1883 * it is always possible because we backspace over the whole line and then
1884 * put it back again the way we wanted it.
1885 */
1886 if (State & VREPLACE_FLAG)
1887 {
1888 /* If orig_line didn't allocate, just return. At least we did the job,
1889 * even if you can't backspace. */
1890 if (orig_line == NULL)
1891 return;
1892
1893 /* Save new line */
1894 new_line = vim_strsave(ml_get_curline());
1895 if (new_line == NULL)
1896 return;
1897
1898 /* We only put back the new line up to the cursor */
1899 new_line[curwin->w_cursor.col] = NUL;
1900
1901 /* Put back original line */
1902 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1903 curwin->w_cursor.col = orig_col;
1904
1905 /* Backspace from cursor to start of line */
1906 backspace_until_column(0);
1907
1908 /* Insert new stuff into line again */
1909 ins_bytes(new_line);
1910
1911 vim_free(new_line);
1912 }
1913#endif
1914}
1915
1916/*
1917 * Truncate the space at the end of a line. This is to be used only in an
1918 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1919 * modes.
1920 */
1921 void
1922truncate_spaces(line)
1923 char_u *line;
1924{
1925 int i;
1926
1927 /* find start of trailing white space */
1928 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1929 {
1930 if (State & REPLACE_FLAG)
1931 replace_join(0); /* remove a NUL from the replace stack */
1932 }
1933 line[i + 1] = NUL;
1934}
1935
1936#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1937 || defined(FEAT_COMMENTS) || defined(PROTO)
1938/*
1939 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1940 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001941 * Will attempt not to go before "col" even when there is a composing
1942 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944 void
1945backspace_until_column(col)
1946 int col;
1947{
1948 while ((int)curwin->w_cursor.col > col)
1949 {
1950 curwin->w_cursor.col--;
1951 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001952 replace_do_bs(col);
1953 else if (!del_char_after_col(col))
1954 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956}
1957#endif
1958
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001959/*
1960 * Like del_char(), but make sure not to go before column "limit_col".
1961 * Only matters when there are composing characters.
1962 * Return TRUE when something was deleted.
1963 */
1964 static int
1965del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001966 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001967{
1968#ifdef FEAT_MBYTE
1969 if (enc_utf8 && limit_col >= 0)
1970 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001971 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001972
1973 /* Make sure the cursor is at the start of a character, but
1974 * skip forward again when going too far back because of a
1975 * composing character. */
1976 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001977 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001978 {
1979 int l = utf_ptr2len(ml_get_cursor());
1980
1981 if (l == 0) /* end of line */
1982 break;
1983 curwin->w_cursor.col += l;
1984 }
1985 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1986 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001987 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001988 }
1989 else
1990#endif
1991 (void)del_char(FALSE);
1992 return TRUE;
1993}
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1996/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001997 * CTRL-X pressed in Insert mode.
1998 */
1999 static void
2000ins_ctrl_x()
2001{
2002 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2003 * CTRL-V works like CTRL-N */
2004 if (ctrl_x_mode != CTRL_X_CMDLINE)
2005 {
2006 /* if the next ^X<> won't ADD nothing, then reset
2007 * compl_cont_status */
2008 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002009 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002010 else
2011 compl_cont_status = 0;
2012 /* We're not sure which CTRL-X mode it will be yet */
2013 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2014 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2015 edit_submode_pre = NULL;
2016 showmode();
2017 }
2018}
2019
2020/*
2021 * Return TRUE if the 'dict' or 'tsr' option can be used.
2022 */
2023 static int
2024has_compl_option(dict_opt)
2025 int dict_opt;
2026{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002027 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002028# ifdef FEAT_SPELL
2029 && !curwin->w_p_spell
2030# endif
2031 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002032 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2033 {
2034 ctrl_x_mode = 0;
2035 edit_submode = NULL;
2036 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2037 : (char_u *)_("'thesaurus' option is empty"),
2038 hl_attr(HLF_E));
2039 if (emsg_silent == 0)
2040 {
2041 vim_beep();
2042 setcursor();
2043 out_flush();
2044 ui_delay(2000L, FALSE);
2045 }
2046 return FALSE;
2047 }
2048 return TRUE;
2049}
2050
2051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2053 * This depends on the current mode.
2054 */
2055 int
2056vim_is_ctrl_x_key(c)
2057 int c;
2058{
2059 /* Always allow ^R - let it's results then be checked */
2060 if (c == Ctrl_R)
2061 return TRUE;
2062
Bram Moolenaare3226be2005-12-18 22:10:00 +00002063 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002064 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002065 return TRUE;
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 switch (ctrl_x_mode)
2068 {
2069 case 0: /* Not in any CTRL-X mode */
2070 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2071 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002072 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2074 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2075 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002076 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2077 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 case CTRL_X_SCROLL:
2079 return (c == Ctrl_Y || c == Ctrl_E);
2080 case CTRL_X_WHOLE_LINE:
2081 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2082 case CTRL_X_FILES:
2083 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2084 case CTRL_X_DICTIONARY:
2085 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2086 case CTRL_X_THESAURUS:
2087 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2088 case CTRL_X_TAGS:
2089 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2090#ifdef FEAT_FIND_ID
2091 case CTRL_X_PATH_PATTERNS:
2092 return (c == Ctrl_P || c == Ctrl_N);
2093 case CTRL_X_PATH_DEFINES:
2094 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2095#endif
2096 case CTRL_X_CMDLINE:
2097 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2098 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002099#ifdef FEAT_COMPL_FUNC
2100 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002101 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002102 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002104#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002105 case CTRL_X_SPELL:
2106 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 EMSG(_(e_internal));
2109 return FALSE;
2110}
2111
2112/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002113 * Return TRUE when character "c" is part of the item currently being
2114 * completed. Used to decide whether to abandon complete mode when the menu
2115 * is visible.
2116 */
2117 static int
2118ins_compl_accept_char(c)
2119 int c;
2120{
2121 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2122 /* When expanding an identifier only accept identifier chars. */
2123 return vim_isIDc(c);
2124
2125 switch (ctrl_x_mode)
2126 {
2127 case CTRL_X_FILES:
2128 /* When expanding file name only accept file name chars. But not
2129 * path separators, so that "proto/<Tab>" expands files in
2130 * "proto", not "proto/" as a whole */
2131 return vim_isfilec(c) && !vim_ispathsep(c);
2132
2133 case CTRL_X_CMDLINE:
2134 case CTRL_X_OMNI:
2135 /* Command line and Omni completion can work with just about any
2136 * printable character, but do stop at white space. */
2137 return vim_isprintc(c) && !vim_iswhite(c);
2138
2139 case CTRL_X_WHOLE_LINE:
2140 /* For while line completion a space can be part of the line. */
2141 return vim_isprintc(c);
2142 }
2143 return vim_iswordc(c);
2144}
2145
2146/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002147 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002149 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 * the rest of the word to be in -- webb
2151 */
2152 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002153ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 char_u *str;
2155 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002156 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 char_u *fname;
2158 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002159 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002161 char_u *p;
2162 int i, c;
2163 int actual_len; /* Take multi-byte characters */
2164 int actual_compl_length; /* into account. */
2165 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 int has_lower = FALSE;
2167 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002169 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002171 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaara2993e12007-08-12 14:38:46 +00002173 /* Find actual length of completion. */
2174#ifdef FEAT_MBYTE
2175 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002177 p = str;
2178 actual_len = 0;
2179 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002181 mb_ptr_adv(p);
2182 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002185 else
2186#endif
2187 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
Bram Moolenaara2993e12007-08-12 14:38:46 +00002189 /* Find actual length of original text. */
2190#ifdef FEAT_MBYTE
2191 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002193 p = compl_orig_text;
2194 actual_compl_length = 0;
2195 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002197 mb_ptr_adv(p);
2198 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002201 else
2202#endif
2203 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
Bram Moolenaara2993e12007-08-12 14:38:46 +00002205 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002206 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002207 if (wca != NULL)
2208 {
2209 p = str;
2210 for (i = 0; i < actual_len; ++i)
2211#ifdef FEAT_MBYTE
2212 if (has_mbyte)
2213 wca[i] = mb_ptr2char_adv(&p);
2214 else
2215#endif
2216 wca[i] = *(p++);
2217
2218 /* Rule 1: Were any chars converted to lower? */
2219 p = compl_orig_text;
2220 for (i = 0; i < actual_compl_length; ++i)
2221 {
2222#ifdef FEAT_MBYTE
2223 if (has_mbyte)
2224 c = mb_ptr2char_adv(&p);
2225 else
2226#endif
2227 c = *(p++);
2228 if (MB_ISLOWER(c))
2229 {
2230 has_lower = TRUE;
2231 if (MB_ISUPPER(wca[i]))
2232 {
2233 /* Rule 1 is satisfied. */
2234 for (i = actual_compl_length; i < actual_len; ++i)
2235 wca[i] = MB_TOLOWER(wca[i]);
2236 break;
2237 }
2238 }
2239 }
2240
2241 /*
2242 * Rule 2: No lower case, 2nd consecutive letter converted to
2243 * upper case.
2244 */
2245 if (!has_lower)
2246 {
2247 p = compl_orig_text;
2248 for (i = 0; i < actual_compl_length; ++i)
2249 {
2250#ifdef FEAT_MBYTE
2251 if (has_mbyte)
2252 c = mb_ptr2char_adv(&p);
2253 else
2254#endif
2255 c = *(p++);
2256 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2257 {
2258 /* Rule 2 is satisfied. */
2259 for (i = actual_compl_length; i < actual_len; ++i)
2260 wca[i] = MB_TOUPPER(wca[i]);
2261 break;
2262 }
2263 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2264 }
2265 }
2266
2267 /* Copy the original case of the part we typed. */
2268 p = compl_orig_text;
2269 for (i = 0; i < actual_compl_length; ++i)
2270 {
2271#ifdef FEAT_MBYTE
2272 if (has_mbyte)
2273 c = mb_ptr2char_adv(&p);
2274 else
2275#endif
2276 c = *(p++);
2277 if (MB_ISLOWER(c))
2278 wca[i] = MB_TOLOWER(wca[i]);
2279 else if (MB_ISUPPER(c))
2280 wca[i] = MB_TOUPPER(wca[i]);
2281 }
2282
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002283 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002284 * Generate encoding specific output from wide character array.
2285 * Multi-byte characters can occupy up to five bytes more than
2286 * ASCII characters, and we also need one byte for NUL, so stay
2287 * six bytes away from the edge of IObuff.
2288 */
2289 p = IObuff;
2290 i = 0;
2291 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2292#ifdef FEAT_MBYTE
2293 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002294 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002295 else
2296#endif
2297 *(p++) = wca[i++];
2298 *p = NUL;
2299
2300 vim_free(wca);
2301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002303 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2304 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002306 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307}
2308
2309/*
2310 * Add a match to the list of matches.
2311 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002312 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002313 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002315 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002316ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 char_u *str;
2318 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002319 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002321 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002322 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002323 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002324 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002326 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002327 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 ui_breakcheck();
2330 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (len < 0)
2333 len = (int)STRLEN(str);
2334
2335 /*
2336 * If the same match is already present, don't add it.
2337 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002338 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002340 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 do
2342 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002343 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002344 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002345 && match->cp_str[len] == NUL)
2346 return NOTDONE;
2347 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002348 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002351 /* Remove any popup menu before changing the list of matches. */
2352 ins_compl_del_pum();
2353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 /*
2355 * Allocate a new match structure.
2356 * Copy the values to the new match structure.
2357 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002358 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002360 return FAIL;
2361 match->cp_number = -1;
2362 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002363 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002364 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002369 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002372 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2374 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002375 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002376 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002377 && compl_curr_match->cp_fname != NULL
2378 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002379 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002380 else if (fname != NULL)
2381 {
2382 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002383 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002386 match->cp_fname = NULL;
2387 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002388
2389 if (cptext != NULL)
2390 {
2391 int i;
2392
2393 for (i = 0; i < CPT_COUNT; ++i)
2394 if (cptext[i] != NULL && *cptext[i] != NUL)
2395 match->cp_text[i] = vim_strsave(cptext[i]);
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
2399 * Link the new match structure in the list of matches.
2400 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002401 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002402 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 else if (dir == FORWARD)
2404 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002405 match->cp_next = compl_curr_match->cp_next;
2406 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 else /* BACKWARD */
2409 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002410 match->cp_next = compl_curr_match;
2411 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002413 if (match->cp_next)
2414 match->cp_next->cp_prev = match;
2415 if (match->cp_prev)
2416 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002418 compl_first_match = match;
2419 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002421 /*
2422 * Find the longest common string if still doing that.
2423 */
2424 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2425 ins_compl_longest_match(match);
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 return OK;
2428}
2429
2430/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002431 * Return TRUE if "str[len]" matches with match->cp_str, considering
2432 * match->cp_icase.
2433 */
2434 static int
2435ins_compl_equal(match, str, len)
2436 compl_T *match;
2437 char_u *str;
2438 int len;
2439{
2440 if (match->cp_icase)
2441 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2442 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2443}
2444
2445/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002446 * Reduce the longest common string for match "match".
2447 */
2448 static void
2449ins_compl_longest_match(match)
2450 compl_T *match;
2451{
2452 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002453 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002454 int had_match;
2455
2456 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002457 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002458 /* First match, use it as a whole. */
2459 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002460 if (compl_leader != NULL)
2461 {
2462 had_match = (curwin->w_cursor.col > compl_col);
2463 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002464 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002465 ins_redraw(FALSE);
2466
2467 /* When the match isn't there (to avoid matching itself) remove it
2468 * again after redrawing. */
2469 if (!had_match)
2470 ins_compl_delete();
2471 compl_used_match = FALSE;
2472 }
2473 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002474 else
2475 {
2476 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002477 p = compl_leader;
2478 s = match->cp_str;
2479 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002480 {
2481#ifdef FEAT_MBYTE
2482 if (has_mbyte)
2483 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002484 c1 = mb_ptr2char(p);
2485 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002486 }
2487 else
2488#endif
2489 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002490 c1 = *p;
2491 c2 = *s;
2492 }
2493 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2494 : (c1 != c2))
2495 break;
2496#ifdef FEAT_MBYTE
2497 if (has_mbyte)
2498 {
2499 mb_ptr_adv(p);
2500 mb_ptr_adv(s);
2501 }
2502 else
2503#endif
2504 {
2505 ++p;
2506 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002507 }
2508 }
2509
2510 if (*p != NUL)
2511 {
2512 /* Leader was shortened, need to change the inserted text. */
2513 *p = NUL;
2514 had_match = (curwin->w_cursor.col > compl_col);
2515 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002516 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002517 ins_redraw(FALSE);
2518
2519 /* When the match isn't there (to avoid matching itself) remove it
2520 * again after redrawing. */
2521 if (!had_match)
2522 ins_compl_delete();
2523 }
2524
2525 compl_used_match = FALSE;
2526 }
2527}
2528
2529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 * Add an array of matches to the list of matches.
2531 * Frees matches[].
2532 */
2533 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 int num_matches;
2536 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002537 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 int i;
2540 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002541 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002544 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002545 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002547 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 FreeWild(num_matches, matches);
2549}
2550
2551/* Make the completion list cyclic.
2552 * Return the number of matches (excluding the original).
2553 */
2554 static int
2555ins_compl_make_cyclic()
2556{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002557 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int count = 0;
2559
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002560 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 /*
2563 * Find the end of the list.
2564 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002565 match = compl_first_match;
2566 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002567 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 ++count;
2571 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002572 match->cp_next = compl_first_match;
2573 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 return count;
2576}
2577
Bram Moolenaara94bc432006-03-10 21:42:59 +00002578/*
2579 * Start completion for the complete() function.
2580 * "startcol" is where the matched text starts (1 is first column).
2581 * "list" is the list of matches.
2582 */
2583 void
2584set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002585 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002586 list_T *list;
2587{
2588 /* If already doing completions stop it. */
2589 if (ctrl_x_mode != 0)
2590 ins_compl_prep(' ');
2591 ins_compl_clear();
2592
2593 if (stop_arrow() == FAIL)
2594 return;
2595
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002596 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002597 startcol = curwin->w_cursor.col;
2598 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002599 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002600 /* compl_pattern doesn't need to be set */
2601 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2602 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002603 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002604 return;
2605
2606 /* Handle like dictionary completion. */
2607 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2608
2609 ins_compl_add_list(list);
2610 compl_matches = ins_compl_make_cyclic();
2611 compl_started = TRUE;
2612 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002613 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002614
2615 compl_curr_match = compl_first_match;
2616 ins_complete(Ctrl_N);
2617 out_flush();
2618}
2619
2620
Bram Moolenaar9372a112005-12-06 19:59:18 +00002621/* "compl_match_array" points the currently displayed list of entries in the
2622 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002623static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002624static int compl_match_arraysize;
2625
2626/*
2627 * Update the screen and when there is any scrolling remove the popup menu.
2628 */
2629 static void
2630ins_compl_upd_pum()
2631{
2632 int h;
2633
2634 if (compl_match_array != NULL)
2635 {
2636 h = curwin->w_cline_height;
2637 update_screen(0);
2638 if (h != curwin->w_cline_height)
2639 ins_compl_del_pum();
2640 }
2641}
2642
2643/*
2644 * Remove any popup menu.
2645 */
2646 static void
2647ins_compl_del_pum()
2648{
2649 if (compl_match_array != NULL)
2650 {
2651 pum_undisplay();
2652 vim_free(compl_match_array);
2653 compl_match_array = NULL;
2654 }
2655}
2656
2657/*
2658 * Return TRUE if the popup menu should be displayed.
2659 */
2660 static int
2661pum_wanted()
2662{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002663 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002665 return FALSE;
2666
2667 /* The display looks bad on a B&W display. */
2668 if (t_colors < 8
2669#ifdef FEAT_GUI
2670 && !gui.in_use
2671#endif
2672 )
2673 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002674 return TRUE;
2675}
2676
2677/*
2678 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002679 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002680 */
2681 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002682pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002683{
2684 compl_T *compl;
2685 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002686
2687 /* Don't display the popup menu if there are no matches or there is only
2688 * one (ignoring the original text). */
2689 compl = compl_first_match;
2690 i = 0;
2691 do
2692 {
2693 if (compl == NULL
2694 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2695 break;
2696 compl = compl->cp_next;
2697 } while (compl != compl_first_match);
2698
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002699 if (strstr((char *)p_cot, "menuone") != NULL)
2700 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002701 return (i >= 2);
2702}
2703
2704/*
2705 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002706 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002707 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002708 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002709ins_compl_show_pum()
2710{
2711 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002712 compl_T *shown_compl = NULL;
2713 int did_find_shown_match = FALSE;
2714 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715 int i;
2716 int cur = -1;
2717 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002718 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002719
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002720 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002721 return;
2722
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002723#if defined(FEAT_EVAL)
2724 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2725 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2726#endif
2727
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002728 /* Update the screen before drawing the popup menu over it. */
2729 update_screen(0);
2730
2731 if (compl_match_array == NULL)
2732 {
2733 /* Need to build the popup menu list. */
2734 compl_match_arraysize = 0;
2735 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002736 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002737 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002738 do
2739 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002740 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2741 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002742 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002743 ++compl_match_arraysize;
2744 compl = compl->cp_next;
2745 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002746 if (compl_match_arraysize == 0)
2747 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002748 compl_match_array = (pumitem_T *)alloc_clear(
2749 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002750 * compl_match_arraysize));
2751 if (compl_match_array != NULL)
2752 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002753 /* If the current match is the original text don't find the first
2754 * match after it, don't highlight anything. */
2755 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2756 shown_match_ok = TRUE;
2757
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002758 i = 0;
2759 compl = compl_first_match;
2760 do
2761 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002762 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2763 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002764 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002765 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002766 if (!shown_match_ok)
2767 {
2768 if (compl == compl_shown_match || did_find_shown_match)
2769 {
2770 /* This item is the shown match or this is the
2771 * first displayed item after the shown match. */
2772 compl_shown_match = compl;
2773 did_find_shown_match = TRUE;
2774 shown_match_ok = TRUE;
2775 }
2776 else
2777 /* Remember this displayed match for when the
2778 * shown match is just below it. */
2779 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002780 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002781 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002782
2783 if (compl->cp_text[CPT_ABBR] != NULL)
2784 compl_match_array[i].pum_text =
2785 compl->cp_text[CPT_ABBR];
2786 else
2787 compl_match_array[i].pum_text = compl->cp_str;
2788 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2789 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2790 if (compl->cp_text[CPT_MENU] != NULL)
2791 compl_match_array[i++].pum_extra =
2792 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002793 else
2794 compl_match_array[i++].pum_extra = compl->cp_fname;
2795 }
2796
2797 if (compl == compl_shown_match)
2798 {
2799 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002800
2801 /* When the original text is the shown match don't set
2802 * compl_shown_match. */
2803 if (compl->cp_flags & ORIGINAL_TEXT)
2804 shown_match_ok = TRUE;
2805
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002806 if (!shown_match_ok && shown_compl != NULL)
2807 {
2808 /* The shown match isn't displayed, set it to the
2809 * previously displayed match. */
2810 compl_shown_match = shown_compl;
2811 shown_match_ok = TRUE;
2812 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002813 }
2814 compl = compl->cp_next;
2815 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002816
2817 if (!shown_match_ok) /* no displayed match at all */
2818 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002819 }
2820 }
2821 else
2822 {
2823 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002824 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002825 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2826 || compl_match_array[i].pum_text
2827 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002828 {
2829 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002830 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002831 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002832 }
2833
2834 if (compl_match_array != NULL)
2835 {
2836 /* Compute the screen column of the start of the completed text.
2837 * Use the cursor to get all wrapping and other settings right. */
2838 col = curwin->w_cursor.col;
2839 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002840 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002841 curwin->w_cursor.col = col;
2842 }
2843}
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#define DICT_FIRST (1) /* use just first element in "dict" */
2846#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002849 * Add any identifiers that match the given pattern in the list of dictionary
2850 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 */
2852 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002853ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2854 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002856 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002857 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002859 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 char_u *ptr;
2861 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 char_u **files;
2864 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar0b238792006-03-02 22:49:12 +00002868 if (*dict == NUL)
2869 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002870#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002871 /* When 'dictionary' is empty and spell checking is enabled use
2872 * "spell". */
2873 if (!thesaurus && curwin->w_p_spell)
2874 dict = (char_u *)"spell";
2875 else
2876#endif
2877 return;
2878 }
2879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002881 if (buf == NULL)
2882 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002883 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002884
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 /* If 'infercase' is set, don't use 'smartcase' here */
2886 save_p_scs = p_scs;
2887 if (curbuf->b_p_inf)
2888 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002889
2890 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2891 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002892 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002893 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2894 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002895 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002896 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002897
2898 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002899 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002900 len = STRLEN(pat_esc) + 10;
2901 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002902 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002903 {
2904 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002905 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002906 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002907 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002908 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2909 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002910 vim_free(ptr);
2911 }
2912 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002913 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002914 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002915 if (regmatch.regprog == NULL)
2916 goto theend;
2917 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2920 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002921 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 {
2923 /* copy one dictionary file name into buf */
2924 if (flags == DICT_EXACT)
2925 {
2926 count = 1;
2927 files = &dict;
2928 }
2929 else
2930 {
2931 /* Expand wildcards in the dictionary name, but do not allow
2932 * backticks (for security, the 'dict' option may have been set in
2933 * a modeline). */
2934 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002935# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002936 if (!thesaurus && STRCMP(buf, "spell") == 0)
2937 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002938 else
2939# endif
2940 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 || expand_wildcards(1, &buf, &count, &files,
2942 EW_FILE|EW_SILENT) != OK)
2943 count = 0;
2944 }
2945
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002946# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002947 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002949 /* Complete from active spelling. Skip "\<" in the pattern, we
2950 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002951 if (pat[0] == '\\' && pat[1] == '<')
2952 ptr = pat + 2;
2953 else
2954 ptr = pat;
2955 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002957 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002958# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002959 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002960 {
2961 ins_compl_files(count, files, thesaurus, flags,
2962 &regmatch, buf, &dir);
2963 if (flags != DICT_EXACT)
2964 FreeWild(count, files);
2965 }
2966 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 break;
2968 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002969
2970theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 p_scs = save_p_scs;
2972 vim_free(regmatch.regprog);
2973 vim_free(buf);
2974}
2975
Bram Moolenaar0b238792006-03-02 22:49:12 +00002976 static void
2977ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2978 int count;
2979 char_u **files;
2980 int thesaurus;
2981 int flags;
2982 regmatch_T *regmatch;
2983 char_u *buf;
2984 int *dir;
2985{
2986 char_u *ptr;
2987 int i;
2988 FILE *fp;
2989 int add_r;
2990
2991 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2992 {
2993 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2994 if (flags != DICT_EXACT)
2995 {
2996 vim_snprintf((char *)IObuff, IOSIZE,
2997 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002998 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00002999 }
3000
3001 if (fp != NULL)
3002 {
3003 /*
3004 * Read dictionary file line by line.
3005 * Check each line for a match.
3006 */
3007 while (!got_int && !compl_interrupted
3008 && !vim_fgets(buf, LSIZE, fp))
3009 {
3010 ptr = buf;
3011 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3012 {
3013 ptr = regmatch->startp[0];
3014 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3015 ptr = find_line_end(ptr);
3016 else
3017 ptr = find_word_end(ptr);
3018 add_r = ins_compl_add_infercase(regmatch->startp[0],
3019 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003020 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003021 if (thesaurus)
3022 {
3023 char_u *wstart;
3024
3025 /*
3026 * Add the other matches on the line
3027 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003028 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003029 while (!got_int)
3030 {
3031 /* Find start of the next word. Skip white
3032 * space and punctuation. */
3033 ptr = find_word_start(ptr);
3034 if (*ptr == NUL || *ptr == NL)
3035 break;
3036 wstart = ptr;
3037
Bram Moolenaara2993e12007-08-12 14:38:46 +00003038 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003039#ifdef FEAT_MBYTE
3040 if (has_mbyte)
3041 /* Japanese words may have characters in
3042 * different classes, only separate words
3043 * with single-byte non-word characters. */
3044 while (*ptr != NUL)
3045 {
3046 int l = (*mb_ptr2len)(ptr);
3047
3048 if (l < 2 && !vim_iswordc(*ptr))
3049 break;
3050 ptr += l;
3051 }
3052 else
3053#endif
3054 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003055
3056 /* Add the word. Skip the regexp match. */
3057 if (wstart != regmatch->startp[0])
3058 add_r = ins_compl_add_infercase(wstart,
3059 (int)(ptr - wstart),
3060 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003061 }
3062 }
3063 if (add_r == OK)
3064 /* if dir was BACKWARD then honor it just once */
3065 *dir = FORWARD;
3066 else if (add_r == FAIL)
3067 break;
3068 /* avoid expensive call to vim_regexec() when at end
3069 * of line */
3070 if (*ptr == '\n' || got_int)
3071 break;
3072 }
3073 line_breakcheck();
3074 ins_compl_check_keys(50);
3075 }
3076 fclose(fp);
3077 }
3078 }
3079}
3080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081/*
3082 * Find the start of the next word.
3083 * Returns a pointer to the first char of the word. Also stops at a NUL.
3084 */
3085 char_u *
3086find_word_start(ptr)
3087 char_u *ptr;
3088{
3089#ifdef FEAT_MBYTE
3090 if (has_mbyte)
3091 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003092 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else
3094#endif
3095 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3096 ++ptr;
3097 return ptr;
3098}
3099
3100/*
3101 * Find the end of the word. Assumes it starts inside a word.
3102 * Returns a pointer to just after the word.
3103 */
3104 char_u *
3105find_word_end(ptr)
3106 char_u *ptr;
3107{
3108#ifdef FEAT_MBYTE
3109 int start_class;
3110
3111 if (has_mbyte)
3112 {
3113 start_class = mb_get_class(ptr);
3114 if (start_class > 1)
3115 while (*ptr != NUL)
3116 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003117 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (mb_get_class(ptr) != start_class)
3119 break;
3120 }
3121 }
3122 else
3123#endif
3124 while (vim_iswordc(*ptr))
3125 ++ptr;
3126 return ptr;
3127}
3128
3129/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003130 * Find the end of the line, omitting CR and NL at the end.
3131 * Returns a pointer to just after the line.
3132 */
3133 static char_u *
3134find_line_end(ptr)
3135 char_u *ptr;
3136{
3137 char_u *s;
3138
3139 s = ptr + STRLEN(ptr);
3140 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3141 --s;
3142 return s;
3143}
3144
3145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * Free the list of completions
3147 */
3148 static void
3149ins_compl_free()
3150{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003151 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003152 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003154 vim_free(compl_pattern);
3155 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003156 vim_free(compl_leader);
3157 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003159 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003161
3162 ins_compl_del_pum();
3163 pum_clear();
3164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003165 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 do
3167 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003168 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003169 compl_curr_match = compl_curr_match->cp_next;
3170 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003172 if (match->cp_flags & FREE_FNAME)
3173 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003174 for (i = 0; i < CPT_COUNT; ++i)
3175 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003177 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3178 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179}
3180
3181 static void
3182ins_compl_clear()
3183{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003184 compl_cont_status = 0;
3185 compl_started = FALSE;
3186 compl_matches = 0;
3187 vim_free(compl_pattern);
3188 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003189 vim_free(compl_leader);
3190 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003192 vim_free(compl_orig_text);
3193 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003194 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195}
3196
3197/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003198 * Return TRUE when Insert completion is active.
3199 */
3200 int
3201ins_compl_active()
3202{
3203 return compl_started;
3204}
3205
3206/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 * Delete one character before the cursor and show the subset of the matches
3208 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003209 * Returns the character to be used, NUL if the work is done and another char
3210 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003211 */
3212 static int
3213ins_compl_bs()
3214{
3215 char_u *line;
3216 char_u *p;
3217
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003218 line = ml_get_curline();
3219 p = line + curwin->w_cursor.col;
3220 mb_ptr_back(line, p);
3221
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003222 /* Stop completion when the whole word was deleted. For Omni completion
3223 * allow the word to be deleted, we won't match everything. */
3224 if ((int)(p - line) - (int)compl_col < 0
3225 || ((int)(p - line) - (int)compl_col == 0
3226 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003227 return K_BS;
3228
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003229 /* Deleted more than what was used to find matches or didn't finish
3230 * finding all matches: need to look for matches all over again. */
3231 if (curwin->w_cursor.col <= compl_col + compl_length
3232 || compl_was_interrupted)
3233 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003234
Bram Moolenaara6557602006-02-04 22:43:20 +00003235 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003236 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003237 if (compl_leader != NULL)
3238 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003239 ins_compl_new_leader();
3240 return NUL;
3241 }
3242 return K_BS;
3243}
Bram Moolenaara6557602006-02-04 22:43:20 +00003244
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003245/*
3246 * Called after changing "compl_leader".
3247 * Show the popup menu with a different set of matches.
3248 * May also search for matches again if the previous search was interrupted.
3249 */
3250 static void
3251ins_compl_new_leader()
3252{
3253 ins_compl_del_pum();
3254 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003255 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003256 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003257
3258 if (compl_started)
3259 ins_compl_set_original_text(compl_leader);
3260 else
3261 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003262#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003263 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003264#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003265 /*
3266 * Matches were cleared, need to search for them now. First display
3267 * the changed text before the cursor. Set "compl_restarting" to
3268 * avoid that the first match is inserted.
3269 */
3270 update_screen(0);
3271#ifdef FEAT_GUI
3272 if (gui.in_use)
3273 {
3274 /* Show the cursor after the match, not after the redrawn text. */
3275 setcursor();
3276 out_flush();
3277 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003278 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003279#endif
3280 compl_restarting = TRUE;
3281 if (ins_complete(Ctrl_N) == FAIL)
3282 compl_cont_status = 0;
3283 compl_restarting = FALSE;
3284 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003285
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003286#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003287 if (!compl_used_match)
3288 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003289 /* Go to the original text, since none of the matches is inserted. */
3290 if (compl_first_match->cp_prev != NULL
3291 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3292 compl_shown_match = compl_first_match->cp_prev;
3293 else
3294 compl_shown_match = compl_first_match;
3295 compl_curr_match = compl_shown_match;
3296 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003297 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003298#endif
3299 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003300
3301 /* Show the popup menu with a different set of matches. */
3302 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003303
3304 /* Don't let Enter select the original text when there is no popup menu. */
3305 if (compl_match_array == NULL)
3306 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003307}
3308
3309/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003310 * Return the length of the completion, from the completion start column to
3311 * the cursor column. Making sure it never goes below zero.
3312 */
3313 static int
3314ins_compl_len()
3315{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003316 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003317
3318 if (off < 0)
3319 return 0;
3320 return off;
3321}
3322
3323/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003324 * Append one character to the match leader. May reduce the number of
3325 * matches.
3326 */
3327 static void
3328ins_compl_addleader(c)
3329 int c;
3330{
3331#ifdef FEAT_MBYTE
3332 int cc;
3333
3334 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3335 {
3336 char_u buf[MB_MAXBYTES + 1];
3337
3338 (*mb_char2bytes)(c, buf);
3339 buf[cc] = NUL;
3340 ins_char_bytes(buf, cc);
3341 }
3342 else
3343#endif
3344 ins_char(c);
3345
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003346 /* If we didn't complete finding matches we must search again. */
3347 if (compl_was_interrupted)
3348 ins_compl_restart();
3349
Bram Moolenaara6557602006-02-04 22:43:20 +00003350 vim_free(compl_leader);
3351 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003352 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003353 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003354 ins_compl_new_leader();
3355}
3356
3357/*
3358 * Setup for finding completions again without leaving CTRL-X mode. Used when
3359 * BS or a key was typed while still searching for matches.
3360 */
3361 static void
3362ins_compl_restart()
3363{
3364 ins_compl_free();
3365 compl_started = FALSE;
3366 compl_matches = 0;
3367 compl_cont_status = 0;
3368 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003369}
3370
3371/*
3372 * Set the first match, the original text.
3373 */
3374 static void
3375ins_compl_set_original_text(str)
3376 char_u *str;
3377{
3378 char_u *p;
3379
3380 /* Replace the original text entry. */
3381 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3382 {
3383 p = vim_strsave(str);
3384 if (p != NULL)
3385 {
3386 vim_free(compl_first_match->cp_str);
3387 compl_first_match->cp_str = p;
3388 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003389 }
3390}
3391
3392/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003393 * Append one character to the match leader. May reduce the number of
3394 * matches.
3395 */
3396 static void
3397ins_compl_addfrommatch()
3398{
3399 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003400 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003401 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003402 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003403
3404 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003405 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003406 {
3407 /* When still at the original match use the first entry that matches
3408 * the leader. */
3409 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3410 {
3411 p = NULL;
3412 for (cp = compl_shown_match->cp_next; cp != NULL
3413 && cp != compl_first_match; cp = cp->cp_next)
3414 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003415 if (compl_leader == NULL
3416 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003417 (int)STRLEN(compl_leader)))
3418 {
3419 p = cp->cp_str;
3420 break;
3421 }
3422 }
3423 if (p == NULL || (int)STRLEN(p) <= len)
3424 return;
3425 }
3426 else
3427 return;
3428 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003429 p += len;
3430#ifdef FEAT_MBYTE
3431 c = mb_ptr2char(p);
3432#else
3433 c = *p;
3434#endif
3435 ins_compl_addleader(c);
3436}
3437
3438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003440 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003441 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003443 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444ins_compl_prep(c)
3445 int c;
3446{
3447 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003449 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 /* Forget any previous 'special' messages if this is actually
3452 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3453 */
3454 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3455 edit_submode_extra = NULL;
3456
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003457 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3458 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003459 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003461 /* Set "compl_get_longest" when finding the first matches. */
3462 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3463 || (ctrl_x_mode == 0 && !compl_started))
3464 {
3465 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3466 compl_used_match = TRUE;
3467 }
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3470 {
3471 /*
3472 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3473 * it will be yet. Now we decide.
3474 */
3475 switch (c)
3476 {
3477 case Ctrl_E:
3478 case Ctrl_Y:
3479 ctrl_x_mode = CTRL_X_SCROLL;
3480 if (!(State & REPLACE_FLAG))
3481 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3482 else
3483 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3484 edit_submode_pre = NULL;
3485 showmode();
3486 break;
3487 case Ctrl_L:
3488 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3489 break;
3490 case Ctrl_F:
3491 ctrl_x_mode = CTRL_X_FILES;
3492 break;
3493 case Ctrl_K:
3494 ctrl_x_mode = CTRL_X_DICTIONARY;
3495 break;
3496 case Ctrl_R:
3497 /* Simply allow ^R to happen without affecting ^X mode */
3498 break;
3499 case Ctrl_T:
3500 ctrl_x_mode = CTRL_X_THESAURUS;
3501 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003502#ifdef FEAT_COMPL_FUNC
3503 case Ctrl_U:
3504 ctrl_x_mode = CTRL_X_FUNCTION;
3505 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003506 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003507 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003508 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003509#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003510 case 's':
3511 case Ctrl_S:
3512 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003513#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003514 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003515 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003516 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003517#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 case Ctrl_RSB:
3520 ctrl_x_mode = CTRL_X_TAGS;
3521 break;
3522#ifdef FEAT_FIND_ID
3523 case Ctrl_I:
3524 case K_S_TAB:
3525 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3526 break;
3527 case Ctrl_D:
3528 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3529 break;
3530#endif
3531 case Ctrl_V:
3532 case Ctrl_Q:
3533 ctrl_x_mode = CTRL_X_CMDLINE;
3534 break;
3535 case Ctrl_P:
3536 case Ctrl_N:
3537 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3538 * just started ^X mode, or there were enough ^X's to cancel
3539 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3540 * do normal expansion when interrupting a different mode (say
3541 * ^X^F^X^P or ^P^X^X^P, see below)
3542 * nothing changes if interrupting mode 0, (eg, the flag
3543 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 if (!(compl_cont_status & CONT_INTRPT))
3545 compl_cont_status |= CONT_LOCAL;
3546 else if (compl_cont_mode != 0)
3547 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 /* FALLTHROUGH */
3549 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 /* If we have typed at least 2 ^X's... for modes != 0, we set
3551 * compl_cont_status = 0 (eg, as if we had just started ^X
3552 * mode).
3553 * For mode 0, we set "compl_cont_mode" to an impossible
3554 * value, in both cases ^X^X can be used to restart the same
3555 * mode (avoiding ADDING mode).
3556 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3557 * 'complete' and local ^P expansions respectively.
3558 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3559 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (c == Ctrl_X)
3561 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 if (compl_cont_mode != 0)
3563 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 ctrl_x_mode = 0;
3568 edit_submode = NULL;
3569 showmode();
3570 break;
3571 }
3572 }
3573 else if (ctrl_x_mode != 0)
3574 {
3575 /* We're already in CTRL-X mode, do we stay in it? */
3576 if (!vim_is_ctrl_x_key(c))
3577 {
3578 if (ctrl_x_mode == CTRL_X_SCROLL)
3579 ctrl_x_mode = 0;
3580 else
3581 ctrl_x_mode = CTRL_X_FINISHED;
3582 edit_submode = NULL;
3583 }
3584 showmode();
3585 }
3586
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003587 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 /* Show error message from attempted keyword completion (probably
3590 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003593 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3594 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 || ctrl_x_mode == CTRL_X_FINISHED)
3596 {
3597 /* Get here when we have finished typing a sequence of ^N and
3598 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003600 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003603 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003606 * If any of the original typed text has been changed, eg when
3607 * ignorecase is set, we must add back-spaces to the redo
3608 * buffer. We add as few as necessary to delete just the part
3609 * of the original text that has changed.
3610 * When using the longest match, edited the match or used
3611 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003613 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3614 ptr = compl_curr_match->cp_str;
3615 else if (compl_leader != NULL)
3616 ptr = compl_leader;
3617 else
3618 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003619 if (compl_orig_text != NULL)
3620 {
3621 p = compl_orig_text;
3622 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3623 ++temp)
3624 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003625#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003626 if (temp > 0)
3627 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003628#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003629 for (p += temp; *p != NUL; mb_ptr_adv(p))
3630 AppendCharToRedobuff(K_BS);
3631 }
3632 if (ptr != NULL)
3633 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635
3636#ifdef FEAT_CINDENT
3637 want_cindent = (can_cindent && cindent_on());
3638#endif
3639 /*
3640 * When completing whole lines: fix indent for 'cindent'.
3641 * Otherwise, break line if it's too long.
3642 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003643 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645#ifdef FEAT_CINDENT
3646 /* re-indent the current line */
3647 if (want_cindent)
3648 {
3649 do_c_expr_indent();
3650 want_cindent = FALSE; /* don't do it again */
3651 }
3652#endif
3653 }
3654 else
3655 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003656 int prev_col = curwin->w_cursor.col;
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003659 if (prev_col > 0)
3660 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (stop_arrow() == OK)
3662 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003663 if (prev_col > 0
3664 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3665 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003668 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003669 * the selection without inserting anything. When
3670 * compl_enter_selects is set the Enter key does the same. */
3671 if ((c == Ctrl_Y || (compl_enter_selects
3672 && (c == CAR || c == K_KENTER || c == NL)))
3673 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003674 retval = TRUE;
3675
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003676 /* CTRL-E means completion is Ended, go back to the typed text. */
3677 if (c == Ctrl_E)
3678 {
3679 ins_compl_delete();
3680 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003681 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003682 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003683 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003684 retval = TRUE;
3685 }
3686
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003687 auto_format(FALSE, TRUE);
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003690 compl_started = FALSE;
3691 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 msg_clr_cmdline(); /* necessary for "noshowmode" */
3693 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003694 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 if (edit_submode != NULL)
3696 {
3697 edit_submode = NULL;
3698 showmode();
3699 }
3700
3701#ifdef FEAT_CINDENT
3702 /*
3703 * Indent now if a key was typed that is in 'cinkeys'.
3704 */
3705 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3706 do_c_expr_indent();
3707#endif
3708 }
3709 }
3710
3711 /* reset continue_* if we left expansion-mode, if we stay they'll be
3712 * (re)set properly in ins_complete() */
3713 if (!vim_is_ctrl_x_key(c))
3714 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003715 compl_cont_status = 0;
3716 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003718
3719 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720}
3721
3722/*
3723 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3724 * (depending on flag) starting from buf and looking for a non-scanned
3725 * buffer (other than curbuf). curbuf is special, if it is called with
3726 * buf=curbuf then it has to be the first call for a given flag/expansion.
3727 *
3728 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3729 */
3730 static buf_T *
3731ins_compl_next_buf(buf, flag)
3732 buf_T *buf;
3733 int flag;
3734{
3735#ifdef FEAT_WINDOWS
3736 static win_T *wp;
3737#endif
3738
3739 if (flag == 'w') /* just windows */
3740 {
3741#ifdef FEAT_WINDOWS
3742 if (buf == curbuf) /* first call for this flag/expansion */
3743 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003744 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 && wp->w_buffer->b_scanned)
3746 ;
3747 buf = wp->w_buffer;
3748#else
3749 buf = curbuf;
3750#endif
3751 }
3752 else
3753 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3754 * (unlisted buffers)
3755 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003756 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 && ((flag == 'U'
3758 ? buf->b_p_bl
3759 : (!buf->b_p_bl
3760 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 ;
3763 return buf;
3764}
3765
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003766#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003767static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003768
3769/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003770 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003771 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003772 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003773 static void
3774expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003775 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003776 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003777{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003778 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003779 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003780 char_u *funcname;
3781 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003782
Bram Moolenaare344bea2005-09-01 20:46:49 +00003783 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3784 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003785 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003786
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003787 /* Call 'completefunc' to obtain the list of matches. */
3788 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003789 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003790
Bram Moolenaare344bea2005-09-01 20:46:49 +00003791 pos = curwin->w_cursor;
3792 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3793 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003794 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003795 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003796
Bram Moolenaara94bc432006-03-10 21:42:59 +00003797 ins_compl_add_list(matchlist);
3798 list_unref(matchlist);
3799}
3800#endif /* FEAT_COMPL_FUNC */
3801
Bram Moolenaar39f05632006-03-19 22:15:26 +00003802#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003803/*
3804 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003805 */
3806 static void
3807ins_compl_add_list(list)
3808 list_T *list;
3809{
3810 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003811 int dir = compl_direction;
3812
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003813 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003814 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003815 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003816 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3817 /* if dir was BACKWARD then honor it just once */
3818 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003819 else if (did_emsg)
3820 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003821 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003822}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003823
3824/*
3825 * Add a match to the list of matches from a typeval_T.
3826 * If the given string is already in the list of completions, then return
3827 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3828 * maybe because alloc() returns NULL, then FAIL is returned.
3829 */
3830 int
3831ins_compl_add_tv(tv, dir)
3832 typval_T *tv;
3833 int dir;
3834{
3835 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003836 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003837 int adup = FALSE;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003838#ifdef S_SPLINT_S /* splint doesn't parse array of pointers correctly */
3839 char_u **cptext;
3840#else
Bram Moolenaar39f05632006-03-19 22:15:26 +00003841 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003842#endif
Bram Moolenaar39f05632006-03-19 22:15:26 +00003843
3844 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3845 {
3846 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3847 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3848 (char_u *)"abbr", FALSE);
3849 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3850 (char_u *)"menu", FALSE);
3851 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3852 (char_u *)"kind", FALSE);
3853 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3854 (char_u *)"info", FALSE);
3855 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3856 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003857 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003858 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003859 }
3860 else
3861 {
3862 word = get_tv_string_chk(tv);
3863 vim_memset(cptext, 0, sizeof(cptext));
3864 }
3865 if (word == NULL || *word == NUL)
3866 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003867 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003868}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003869#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003870
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871/*
3872 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003873 * The search starts at position "ini" in curbuf and in the direction
3874 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003875 * When "compl_started" is FALSE start at that position, otherwise continue
3876 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003877 * This may return before finding all the matches.
3878 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
3880 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003881ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
3884 static pos_T first_match_pos;
3885 static pos_T last_match_pos;
3886 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 static int found_all = FALSE; /* Found all matches of a
3888 certain type. */
3889 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaar572cb562005-08-05 21:35:02 +00003891 pos_T *pos;
3892 char_u **matches;
3893 int save_p_scs;
3894 int save_p_ws;
3895 int save_p_ic;
3896 int i;
3897 int num_matches;
3898 int len;
3899 int found_new_match;
3900 int type = ctrl_x_mode;
3901 char_u *ptr;
3902 char_u *dict = NULL;
3903 int dict_f = 0;
3904 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3909 ins_buf->b_scanned = 0;
3910 found_all = FALSE;
3911 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003912 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003913 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 last_match_pos = first_match_pos = *ini;
3915 }
3916
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003918 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3920 for (;;)
3921 {
3922 found_new_match = FAIL;
3923
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 * or if found_all says this entry is done. For ^X^L only use the
3926 * entries from 'complete' that look in loaded buffers. */
3927 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 found_all = FALSE;
3931 while (*e_cpt == ',' || *e_cpt == ' ')
3932 e_cpt++;
3933 if (*e_cpt == '.' && !curbuf->b_scanned)
3934 {
3935 ins_buf = curbuf;
3936 first_match_pos = *ini;
3937 /* So that ^N can match word immediately after cursor */
3938 if (ctrl_x_mode == 0)
3939 dec(&first_match_pos);
3940 last_match_pos = first_match_pos;
3941 type = 0;
3942 }
3943 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3944 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3945 {
3946 /* Scan a buffer, but not the current one. */
3947 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3948 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003949 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 first_match_pos.col = last_match_pos.col = 0;
3951 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3952 last_match_pos.lnum = 0;
3953 type = 0;
3954 }
3955 else /* unloaded buffer, scan like dictionary */
3956 {
3957 found_all = TRUE;
3958 if (ins_buf->b_fname == NULL)
3959 continue;
3960 type = CTRL_X_DICTIONARY;
3961 dict = ins_buf->b_fname;
3962 dict_f = DICT_EXACT;
3963 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003964 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 ins_buf->b_fname == NULL
3966 ? buf_spname(ins_buf)
3967 : ins_buf->b_sfname == NULL
3968 ? (char *)ins_buf->b_fname
3969 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003970 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 else if (*e_cpt == NUL)
3973 break;
3974 else
3975 {
3976 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3977 type = -1;
3978 else if (*e_cpt == 'k' || *e_cpt == 's')
3979 {
3980 if (*e_cpt == 'k')
3981 type = CTRL_X_DICTIONARY;
3982 else
3983 type = CTRL_X_THESAURUS;
3984 if (*++e_cpt != ',' && *e_cpt != NUL)
3985 {
3986 dict = e_cpt;
3987 dict_f = DICT_FIRST;
3988 }
3989 }
3990#ifdef FEAT_FIND_ID
3991 else if (*e_cpt == 'i')
3992 type = CTRL_X_PATH_PATTERNS;
3993 else if (*e_cpt == 'd')
3994 type = CTRL_X_PATH_DEFINES;
3995#endif
3996 else if (*e_cpt == ']' || *e_cpt == 't')
3997 {
3998 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003999 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004000 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002 else
4003 type = -1;
4004
4005 /* in any case e_cpt is advanced to the next entry */
4006 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4007
4008 found_all = TRUE;
4009 if (type == -1)
4010 continue;
4011 }
4012 }
4013
4014 switch (type)
4015 {
4016 case -1:
4017 break;
4018#ifdef FEAT_FIND_ID
4019 case CTRL_X_PATH_PATTERNS:
4020 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004021 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004022 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4026 (linenr_T)1, (linenr_T)MAXLNUM);
4027 break;
4028#endif
4029
4030 case CTRL_X_DICTIONARY:
4031 case CTRL_X_THESAURUS:
4032 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004033 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 : (type == CTRL_X_THESAURUS
4035 ? (*curbuf->b_p_tsr == NUL
4036 ? p_tsr
4037 : curbuf->b_p_tsr)
4038 : (*curbuf->b_p_dict == NUL
4039 ? p_dict
4040 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004041 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004042 dict != NULL ? dict_f
4043 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 dict = NULL;
4045 break;
4046
4047 case CTRL_X_TAGS:
4048 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4049 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004053 * of matches is found when compl_pattern is empty */
4054 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4056 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4057 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4058 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004059 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 p_ic = save_p_ic;
4062 break;
4063
4064 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4067 {
4068
4069 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004071 ins_compl_add_matches(num_matches, matches,
4072#ifdef CASE_INSENSITIVE_FILENAME
4073 TRUE
4074#else
4075 FALSE
4076#endif
4077 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 break;
4080
4081 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082 if (expand_cmdline(&compl_xp, compl_pattern,
4083 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004085 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 break;
4087
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004088#ifdef FEAT_COMPL_FUNC
4089 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004090 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004091 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092 break;
4093#endif
4094
Bram Moolenaar488c6512005-08-11 20:09:58 +00004095 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004096#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004097 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004098 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004099 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004100 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004101#endif
4102 break;
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 default: /* normal ^P/^N and ^X^L */
4105 /*
4106 * If 'infercase' is set, don't use 'smartcase' here
4107 */
4108 save_p_scs = p_scs;
4109 if (ins_buf->b_p_inf)
4110 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 /* buffers other than curbuf are scanned from the beginning or the
4113 * end but never from the middle, thus setting nowrapscan in this
4114 * buffers is a good idea, on the other hand, we always set
4115 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4116 save_p_ws = p_ws;
4117 if (ins_buf != curbuf)
4118 p_ws = FALSE;
4119 else if (*e_cpt == '.')
4120 p_ws = TRUE;
4121 for (;;)
4122 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004123 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004125 ++msg_silent; /* Don't want messages for wrapscan. */
4126
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004127 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4128 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004132 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004134 found_new_match = searchit(NULL, ins_buf, pos,
4135 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004137 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004138 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004141 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 first_match_pos = *pos;
4144 last_match_pos = *pos;
4145 }
4146 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004147 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 found_new_match = FAIL;
4149 if (found_new_match == FAIL)
4150 {
4151 if (ins_buf == curbuf)
4152 found_all = TRUE;
4153 break;
4154 }
4155
4156 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 && ini->lnum == pos->lnum
4159 && ini->col == pos->col)
4160 continue;
4161 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4162 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4163 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004164 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4167 continue;
4168 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4169 if (!p_paste)
4170 ptr = skipwhite(ptr);
4171 }
4172 len = (int)STRLEN(ptr);
4173 }
4174 else
4175 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004176 char_u *tmp_ptr = ptr;
4177
4178 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 /* Skip if already inside a word. */
4182 if (vim_iswordp(tmp_ptr))
4183 continue;
4184 /* Find start of next word. */
4185 tmp_ptr = find_word_start(tmp_ptr);
4186 }
4187 /* Find end of this word. */
4188 tmp_ptr = find_word_end(tmp_ptr);
4189 len = (int)(tmp_ptr - ptr);
4190
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 if ((compl_cont_status & CONT_ADDING)
4192 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
4194 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4195 {
4196 /* Try next line, if any. the new word will be
4197 * "join" as if the normal command "J" was used.
4198 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 * works -- Acevedo */
4201 STRNCPY(IObuff, ptr, len);
4202 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4203 tmp_ptr = ptr = skipwhite(ptr);
4204 /* Find start of next word. */
4205 tmp_ptr = find_word_start(tmp_ptr);
4206 /* Find end of next word. */
4207 tmp_ptr = find_word_end(tmp_ptr);
4208 if (tmp_ptr > ptr)
4209 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004210 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004212 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 IObuff[len++] = ' ';
4214 /* IObuf =~ "\k.* ", thus len >= 2 */
4215 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004216 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 || (vim_strchr(p_cpo, CPO_JOINSP)
4218 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004219 && (IObuff[len - 2] == '?'
4220 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 IObuff[len++] = ' ';
4222 }
4223 /* copy as much as posible of the new word */
4224 if (tmp_ptr - ptr >= IOSIZE - len)
4225 tmp_ptr = ptr + IOSIZE - len - 1;
4226 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4227 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004228 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230 IObuff[len] = NUL;
4231 ptr = IObuff;
4232 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 continue;
4235 }
4236 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004237 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004238 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004239 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 found_new_match = OK;
4242 break;
4243 }
4244 }
4245 p_scs = save_p_scs;
4246 p_ws = save_p_ws;
4247 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004250 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004251 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 found_new_match = OK;
4253
4254 /* break the loop for specialized modes (use 'complete' just for the
4255 * generic ctrl_x_mode == 0) or when we've found a new match */
4256 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004258 {
4259 if (got_int)
4260 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004261 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004262 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004263 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004264
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004265 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4266 || compl_interrupted)
4267 break;
4268 compl_started = TRUE;
4269 }
4270 else
4271 {
4272 /* Mark a buffer scanned when it has been scanned completely */
4273 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4274 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004276 compl_started = FALSE;
4277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4282 && *e_cpt == NUL) /* Got to end of 'complete' */
4283 found_new_match = FAIL;
4284
4285 i = -1; /* total of matches, unknown */
4286 if (found_new_match == FAIL
4287 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4288 i = ins_compl_make_cyclic();
4289
4290 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004291 * just been made cyclic then we have to move compl_curr_match to the next
4292 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004293 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4294 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004295 if (compl_curr_match == NULL)
4296 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 return i;
4298}
4299
4300/* Delete the old text being completed. */
4301 static void
4302ins_compl_delete()
4303{
4304 int i;
4305
4306 /*
4307 * In insert mode: Delete the typed part.
4308 * In replace mode: Put the old characters back, if any.
4309 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 backspace_until_column(i);
4312 changed_cline_bef_curs();
4313}
4314
4315/* Insert the new text being completed. */
4316 static void
4317ins_compl_insert()
4318{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004319 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004320 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4321 compl_used_match = FALSE;
4322 else
4323 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324}
4325
4326/*
4327 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004328 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4329 * get more completions. If it is FALSE, then we just do nothing when there
4330 * are no more completions in a given direction. The latter case is used when
4331 * we are still in the middle of finding completions, to allow browsing
4332 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 * Return the total number of matches, or -1 if still unknown -- webb.
4334 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4336 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 *
4338 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004339 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4340 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 */
4342 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004343ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004345 int count; /* repeat completion this many times; should
4346 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004347 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
4349 int num_matches = -1;
4350 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004351 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004352 compl_T *found_compl = NULL;
4353 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004354 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004356 if (compl_leader != NULL
4357 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004359 /* Set "compl_shown_match" to the actually shown match, it may differ
4360 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004361 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004362 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004363 && compl_shown_match->cp_next != NULL
4364 && compl_shown_match->cp_next != compl_first_match)
4365 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004366
4367 /* If we didn't find it searching forward, and compl_shows_dir is
4368 * backward, find the last match. */
4369 if (compl_shows_dir == BACKWARD
4370 && !ins_compl_equal(compl_shown_match,
4371 compl_leader, (int)STRLEN(compl_leader))
4372 && (compl_shown_match->cp_next == NULL
4373 || compl_shown_match->cp_next == compl_first_match))
4374 {
4375 while (!ins_compl_equal(compl_shown_match,
4376 compl_leader, (int)STRLEN(compl_leader))
4377 && compl_shown_match->cp_prev != NULL
4378 && compl_shown_match->cp_prev != compl_first_match)
4379 compl_shown_match = compl_shown_match->cp_prev;
4380 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004381 }
4382
4383 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004384 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 /* Delete old text to be replaced */
4386 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004387
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004388 /* When finding the longest common text we stick at the original text,
4389 * don't let CTRL-N or CTRL-P move to the first match. */
4390 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4391
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004392 /* When restarting the search don't insert the first match either. */
4393 if (compl_restarting)
4394 {
4395 advance = FALSE;
4396 compl_restarting = FALSE;
4397 }
4398
Bram Moolenaare3226be2005-12-18 22:10:00 +00004399 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4400 * around. */
4401 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004403 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004405 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004406 found_end = (compl_first_match != NULL
4407 && (compl_shown_match->cp_next == compl_first_match
4408 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004409 }
4410 else if (compl_shows_dir == BACKWARD
4411 && compl_shown_match->cp_prev != NULL)
4412 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004413 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004414 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004415 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004418 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004419 if (!allow_get_expansion)
4420 {
4421 if (advance)
4422 {
4423 if (compl_shows_dir == BACKWARD)
4424 compl_pending -= todo + 1;
4425 else
4426 compl_pending += todo + 1;
4427 }
4428 return -1;
4429 }
4430
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004431 if (advance)
4432 {
4433 if (compl_shows_dir == BACKWARD)
4434 --compl_pending;
4435 else
4436 ++compl_pending;
4437 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004438
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004439 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004440 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004441
4442 /* handle any pending completions */
4443 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004444 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004445 {
4446 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4447 {
4448 compl_shown_match = compl_shown_match->cp_next;
4449 --compl_pending;
4450 }
4451 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4452 {
4453 compl_shown_match = compl_shown_match->cp_prev;
4454 ++compl_pending;
4455 }
4456 else
4457 break;
4458 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004459 found_end = FALSE;
4460 }
4461 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4462 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004463 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004464 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004465 ++todo;
4466 else
4467 /* Remember a matching item. */
4468 found_compl = compl_shown_match;
4469
4470 /* Stop at the end of the list when we found a usable match. */
4471 if (found_end)
4472 {
4473 if (found_compl != NULL)
4474 {
4475 compl_shown_match = found_compl;
4476 break;
4477 }
4478 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004482 /* Insert the text of the new completion, or the compl_leader. */
4483 if (insert_match)
4484 {
4485 if (!compl_get_longest || compl_used_match)
4486 ins_compl_insert();
4487 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004488 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004489 }
4490 else
4491 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 if (!allow_get_expansion)
4494 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004495 /* may undisplay the popup menu first */
4496 ins_compl_upd_pum();
4497
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004498 /* redraw to show the user what was inserted */
4499 update_screen(0);
4500
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004501 /* display the updated popup menu */
4502 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004503#ifdef FEAT_GUI
4504 if (gui.in_use)
4505 {
4506 /* Show the cursor after the match, not after the redrawn text. */
4507 setcursor();
4508 out_flush();
4509 gui_update_cursor(FALSE, FALSE);
4510 }
4511#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* Delete old text to be replaced, since we're still searching and
4514 * don't want to match ourselves! */
4515 ins_compl_delete();
4516 }
4517
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004518 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004519 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004520 compl_enter_selects = !insert_match && compl_match_array != NULL;
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /*
4523 * Show the file name for the match (if any)
4524 * Truncate the file name to avoid a wait for return.
4525 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004526 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
4528 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004529 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (i <= 0)
4531 i = 0;
4532 else
4533 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004534 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 msg(IObuff);
4536 redraw_cmdline = FALSE; /* don't overwrite! */
4537 }
4538
4539 return num_matches;
4540}
4541
4542/*
4543 * Call this while finding completions, to check whether the user has hit a key
4544 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004545 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004547 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 */
4549 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004550ins_compl_check_keys(frequency)
4551 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
4553 static int count = 0;
4554
4555 int c;
4556
4557 /* Don't check when reading keys from a script. That would break the test
4558 * scripts */
4559 if (using_script())
4560 return;
4561
4562 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004563 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 return;
4565 count = 0;
4566
Bram Moolenaara260a972006-06-23 19:36:29 +00004567 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4568 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (c != NUL)
4571 {
4572 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4573 {
4574 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004575 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004576 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4577 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004579 else
4580 {
4581 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004582 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004583 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004584 if (c != K_IGNORE)
4585 {
4586 /* Don't interrupt completion when the character wasn't typed,
4587 * e.g., when doing @q to replay keys. */
4588 if (c != Ctrl_R && KeyTyped)
4589 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004590
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004591 vungetc(c);
4592 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004595 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004596 {
4597 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4598
4599 compl_pending = 0;
4600 (void)ins_compl_next(FALSE, todo, TRUE);
4601 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004602}
4603
4604/*
4605 * Decide the direction of Insert mode complete from the key typed.
4606 * Returns BACKWARD or FORWARD.
4607 */
4608 static int
4609ins_compl_key2dir(c)
4610 int c;
4611{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004612 if (c == Ctrl_P || c == Ctrl_L
4613 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4614 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004615 return BACKWARD;
4616 return FORWARD;
4617}
4618
4619/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004620 * Return TRUE for keys that are used for completion only when the popup menu
4621 * is visible.
4622 */
4623 static int
4624ins_compl_pum_key(c)
4625 int c;
4626{
4627 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004628 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4629 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004630}
4631
4632/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004633 * Decide the number of completions to move forward.
4634 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4635 */
4636 static int
4637ins_compl_key2count(c)
4638 int c;
4639{
4640 int h;
4641
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004642 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004643 {
4644 h = pum_get_height();
4645 if (h > 3)
4646 h -= 2; /* keep some context */
4647 return h;
4648 }
4649 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650}
4651
4652/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004653 * Return TRUE if completion with "c" should insert the match, FALSE if only
4654 * to change the currently selected completion.
4655 */
4656 static int
4657ins_compl_use_match(c)
4658 int c;
4659{
4660 switch (c)
4661 {
4662 case K_UP:
4663 case K_DOWN:
4664 case K_PAGEDOWN:
4665 case K_KPAGEDOWN:
4666 case K_S_DOWN:
4667 case K_PAGEUP:
4668 case K_KPAGEUP:
4669 case K_S_UP:
4670 return FALSE;
4671 }
4672 return TRUE;
4673}
4674
4675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 * Do Insert mode completion.
4677 * Called when character "c" was typed, which has a meaning for completion.
4678 * Returns OK if completion was done, FAIL if something failed (out of mem).
4679 */
4680 static int
4681ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 char_u *line;
4685 int startcol = 0; /* column where searched text starts */
4686 colnr_T curs_col; /* cursor column */
4687 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaare3226be2005-12-18 22:10:00 +00004689 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 /* First time we hit ^N or ^P (in a row, I mean) */
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 did_ai = FALSE;
4695#ifdef FEAT_SMARTINDENT
4696 did_si = FALSE;
4697 can_si = FALSE;
4698 can_si_back = FALSE;
4699#endif
4700 if (stop_arrow() == FAIL)
4701 return FAIL;
4702
4703 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004704 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004705 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004707 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004708 * "compl_startpos" to the cursor as a pattern to add a new word
4709 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004710 * "compl_startpos" is not in the same line as the cursor then fix it
4711 * (the line has been split because it was longer than 'tw'). if SOL
4712 * is set then skip the previous pattern, a word at the beginning of
4713 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004714 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4715 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 /*
4718 * it is a continued search
4719 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4722 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4723 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004724 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 /* line (probably) wrapped, set compl_startpos to the
4727 * first non_blank in the line, if it is not a wordchar
4728 * include it to get a better pattern, but then we don't
4729 * want the "\\<" prefix, check it bellow */
4730 compl_col = (colnr_T)(skipwhite(line) - line);
4731 compl_startpos.col = compl_col;
4732 compl_startpos.lnum = curwin->w_cursor.lnum;
4733 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 else
4736 {
4737 /* S_IPOS was set when we inserted a word that was at the
4738 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004739 * mode but first we need to redefine compl_startpos */
4740 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004742 compl_cont_status |= CONT_SOL;
4743 compl_startpos.col = (colnr_T)(skipwhite(
4744 line + compl_length
4745 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004750 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004751 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004753 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004755 compl_cont_status &= ~CONT_SOL;
4756 compl_length = (IOSIZE - MIN_SPACE);
4757 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4760 if (compl_length < 1)
4761 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004769 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004773 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 compl_cont_status = 0;
4776 compl_cont_status |= CONT_N_ADDS;
4777 compl_startpos = curwin->w_cursor;
4778 startcol = (int)curs_col;
4779 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781
4782 /* Work out completion pattern and original text -- webb */
4783 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4784 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4787 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 compl_col += ++startcol;
4793 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004796 compl_pattern = str_foldcase(line + compl_col,
4797 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004799 compl_pattern = vim_strnsave(line + compl_col,
4800 compl_length);
4801 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 return FAIL;
4803 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004804 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 char_u *prefix = (char_u *)"\\<";
4807
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004808 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004810 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 if (!vim_iswordp(line + compl_col)
4814 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 && (
4816#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
4821 )))
4822 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 STRCPY((char *)compl_pattern, prefix);
4824 (void)quote_meta(compl_pattern + STRLEN(prefix),
4825 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004827 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004829 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#endif
4833 )
4834 {
4835 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4837 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 compl_col += curs_col;
4840 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 else
4843 {
4844#ifdef FEAT_MBYTE
4845 /* Search the point of change class of multibyte character
4846 * or not a word single byte character backward. */
4847 if (has_mbyte)
4848 {
4849 int base_class;
4850 int head_off;
4851
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 startcol -= (*mb_head_off)(line, line + startcol);
4853 base_class = mb_get_class(line + startcol);
4854 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004856 head_off = (*mb_head_off)(line, line + startcol);
4857 if (base_class != mb_get_class(line + startcol
4858 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004860 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862 }
4863 else
4864#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004865 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 compl_col += ++startcol;
4868 compl_length = (int)curs_col - startcol;
4869 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
4871 /* Only match word with at least two chars -- webb
4872 * there's no need to call quote_meta,
4873 * alloc(7) is enough -- Acevedo
4874 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 compl_pattern = alloc(7);
4876 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004878 STRCPY((char *)compl_pattern, "\\<");
4879 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4880 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 else
4883 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004885 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004888 STRCPY((char *)compl_pattern, "\\<");
4889 (void)quote_meta(compl_pattern + 2, line + compl_col,
4890 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 }
4893 }
4894 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4895 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004896 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004897 compl_length = (int)curs_col - (int)compl_col;
4898 if (compl_length < 0) /* cursor in indent: empty pattern */
4899 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004901 compl_pattern = str_foldcase(line + compl_col, compl_length,
4902 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4905 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 return FAIL;
4907 }
4908 else if (ctrl_x_mode == CTRL_X_FILES)
4909 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004912 compl_col += ++startcol;
4913 compl_length = (int)curs_col - startcol;
4914 compl_pattern = addstar(line + compl_col, compl_length,
4915 EXPAND_FILES);
4916 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
4918 }
4919 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4920 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004921 compl_pattern = vim_strnsave(line, curs_col);
4922 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004924 set_cmd_context(&compl_xp, compl_pattern,
4925 (int)STRLEN(compl_pattern), curs_col);
4926 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4927 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004928 /* No completion possible, use an empty pattern to get a
4929 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004930 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004931 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004932 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4933 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004935 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004936 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004937#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004938 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004939 * Call user defined function 'completefunc' with "a:findstart"
4940 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004941 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004942 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004943 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004944 char_u *funcname;
4945 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004946
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004947 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004948 * string */
4949 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4950 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4951 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004952 {
4953 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4954 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004955 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004956 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004957
4958 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004959 args[1] = NULL;
4960 pos = curwin->w_cursor;
4961 col = call_func_retnr(funcname, 2, args, FALSE);
4962 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004963
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004964 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004965 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004966 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004967 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004968 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004969
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004970 /* Setup variables for completion. Need to obtain "line" again,
4971 * it may have become invalid. */
4972 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004973 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004974 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4975 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004976#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 return FAIL;
4978 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004979 else if (ctrl_x_mode == CTRL_X_SPELL)
4980 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004981#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004982 if (spell_bad_len > 0)
4983 compl_col = curs_col - spell_bad_len;
4984 else
4985 compl_col = spell_word_start(startcol);
4986 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004987 {
4988 compl_length = 0;
4989 compl_col = curs_col;
4990 }
4991 else
4992 {
4993 spell_expand_check_cap(compl_col);
4994 compl_length = (int)curs_col - compl_col;
4995 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004996 /* Need to obtain "line" again, it may have become invalid. */
4997 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004998 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4999 if (compl_pattern == NULL)
5000#endif
5001 return FAIL;
5002 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005003 else
5004 {
5005 EMSG2(_(e_intern2), "ins_complete()");
5006 return FAIL;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 edit_submode_pre = (char_u *)_(" Adding");
5012 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5013 {
5014 /* Insert a new line, keep indentation but ignore 'comments' */
5015#ifdef FEAT_COMMENTS
5016 char_u *old = curbuf->b_p_com;
5017
5018 curbuf->b_p_com = (char_u *)"";
5019#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005020 compl_startpos.lnum = curwin->w_cursor.lnum;
5021 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 ins_eol('\r');
5023#ifdef FEAT_COMMENTS
5024 curbuf->b_p_com = old;
5025#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005026 compl_length = 0;
5027 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 }
5030 else
5031 {
5032 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005033 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005036 if (compl_cont_status & CONT_LOCAL)
5037 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
5039 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5040
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005041 /* Always add completion for the original text. */
5042 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005043 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5044 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005045 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 vim_free(compl_pattern);
5048 compl_pattern = NULL;
5049 vim_free(compl_orig_text);
5050 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 return FAIL;
5052 }
5053
5054 /* showmode might reset the internal line pointers, so it must
5055 * be called before line = ml_get(), or when this address is no
5056 * longer needed. -- Acevedo.
5057 */
5058 edit_submode_extra = (char_u *)_("-- Searching...");
5059 edit_submode_highl = HLF_COUNT;
5060 showmode();
5061 edit_submode_extra = NULL;
5062 out_flush();
5063 }
5064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005065 compl_shown_match = compl_curr_match;
5066 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005069 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005071 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005073 /* may undisplay the popup menu */
5074 ins_compl_upd_pum();
5075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 if (n > 1) /* all matches have been found */
5077 compl_matches = n;
5078 compl_curr_match = compl_shown_match;
5079 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
Bram Moolenaard68071d2006-05-02 22:08:30 +00005081 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5082 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 if (got_int && !global_busy)
5084 {
5085 (void)vgetc();
5086 got_int = FALSE;
5087 }
5088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005090 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5093 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5095 edit_submode_highl = HLF_E;
5096 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5097 * because we couldn't expand anything at first place, but if we used
5098 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5099 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 if ( compl_length > 1
5101 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 || (ctrl_x_mode != 0
5103 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5104 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107
Bram Moolenaar572cb562005-08-05 21:35:02 +00005108 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113 if (edit_submode_extra == NULL)
5114 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005115 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 edit_submode_extra = (char_u *)_("Back at original");
5118 edit_submode_highl = HLF_W;
5119 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005120 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 edit_submode_extra = (char_u *)_("Word from other line");
5123 edit_submode_highl = HLF_COUNT;
5124 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005125 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 edit_submode_extra = (char_u *)_("The only match");
5128 edit_submode_highl = HLF_COUNT;
5129 }
5130 else
5131 {
5132 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005133 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005135 int number = 0;
5136 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 /* search backwards for the first valid (!= -1) number.
5141 * This should normally succeed already at the first loop
5142 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005143 for (match = compl_curr_match->cp_prev; match != NULL
5144 && match != compl_first_match;
5145 match = match->cp_prev)
5146 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005148 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150 }
5151 if (match != NULL)
5152 /* go up and assign all numbers which are not assigned
5153 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005154 for (match = match->cp_next;
5155 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005156 match = match->cp_next)
5157 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 else /* BACKWARD */
5160 {
5161 /* search forwards (upwards) for the first valid (!= -1)
5162 * number. This should normally succeed already at the
5163 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005164 for (match = compl_curr_match->cp_next; match != NULL
5165 && match != compl_first_match;
5166 match = match->cp_next)
5167 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005169 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171 }
5172 if (match != NULL)
5173 /* go down and assign all numbers which are not
5174 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005175 for (match = match->cp_prev; match
5176 && match->cp_number == -1;
5177 match = match->cp_prev)
5178 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 }
5181
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005182 /* The match should always have a sequence number now, this is
5183 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005184 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005186 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5187 * Translations may need more than twice that. */
5188 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005191 vim_snprintf((char *)match_ref, sizeof(match_ref),
5192 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005193 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005195 vim_snprintf((char *)match_ref, sizeof(match_ref),
5196 _("match %d"),
5197 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 edit_submode_extra = match_ref;
5199 edit_submode_highl = HLF_R;
5200 if (dollar_vcol)
5201 curs_columns(FALSE);
5202 }
5203 }
5204 }
5205
5206 /* Show a message about what (completion) mode we're in. */
5207 showmode();
5208 if (edit_submode_extra != NULL)
5209 {
5210 if (!p_smd)
5211 msg_attr(edit_submode_extra,
5212 edit_submode_highl < HLF_COUNT
5213 ? hl_attr(edit_submode_highl) : 0);
5214 }
5215 else
5216 msg_clr_cmdline(); /* necessary for "noshowmode" */
5217
Bram Moolenaard68071d2006-05-02 22:08:30 +00005218 /* Show the popup menu, unless we got interrupted. */
5219 if (!compl_interrupted)
5220 {
5221 /* RedrawingDisabled may be set when invoked through complete(). */
5222 n = RedrawingDisabled;
5223 RedrawingDisabled = 0;
5224 ins_compl_show_pum();
5225 setcursor();
5226 RedrawingDisabled = n;
5227 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005228 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005229 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 return OK;
5232}
5233
5234/*
5235 * Looks in the first "len" chars. of "src" for search-metachars.
5236 * If dest is not NULL the chars. are copied there quoting (with
5237 * a backslash) the metachars, and dest would be NUL terminated.
5238 * Returns the length (needed) of dest
5239 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005240 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241quote_meta(dest, src, len)
5242 char_u *dest;
5243 char_u *src;
5244 int len;
5245{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005246 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005248 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 switch (*src)
5251 {
5252 case '.':
5253 case '*':
5254 case '[':
5255 if (ctrl_x_mode == CTRL_X_DICTIONARY
5256 || ctrl_x_mode == CTRL_X_THESAURUS)
5257 break;
5258 case '~':
5259 if (!p_magic) /* quote these only if magic is set */
5260 break;
5261 case '\\':
5262 if (ctrl_x_mode == CTRL_X_DICTIONARY
5263 || ctrl_x_mode == CTRL_X_THESAURUS)
5264 break;
5265 case '^': /* currently it's not needed. */
5266 case '$':
5267 m++;
5268 if (dest != NULL)
5269 *dest++ = '\\';
5270 break;
5271 }
5272 if (dest != NULL)
5273 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005274# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /* Copy remaining bytes of a multibyte character. */
5276 if (has_mbyte)
5277 {
5278 int i, mb_len;
5279
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005280 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (mb_len > 0 && len >= mb_len)
5282 for (i = 0; i < mb_len; ++i)
5283 {
5284 --len;
5285 ++src;
5286 if (dest != NULL)
5287 *dest++ = *src;
5288 }
5289 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 if (dest != NULL)
5293 *dest = NUL;
5294
5295 return m;
5296}
5297#endif /* FEAT_INS_EXPAND */
5298
5299/*
5300 * Next character is interpreted literally.
5301 * A one, two or three digit decimal number is interpreted as its byte value.
5302 * If one or two digits are entered, the next character is given to vungetc().
5303 * For Unicode a character > 255 may be returned.
5304 */
5305 int
5306get_literal()
5307{
5308 int cc;
5309 int nc;
5310 int i;
5311 int hex = FALSE;
5312 int octal = FALSE;
5313#ifdef FEAT_MBYTE
5314 int unicode = 0;
5315#endif
5316
5317 if (got_int)
5318 return Ctrl_C;
5319
5320#ifdef FEAT_GUI
5321 /*
5322 * In GUI there is no point inserting the internal code for a special key.
5323 * It is more useful to insert the string "<KEY>" instead. This would
5324 * probably be useful in a text window too, but it would not be
5325 * vi-compatible (maybe there should be an option for it?) -- webb
5326 */
5327 if (gui.in_use)
5328 ++allow_keys;
5329#endif
5330#ifdef USE_ON_FLY_SCROLL
5331 dont_scroll = TRUE; /* disallow scrolling here */
5332#endif
5333 ++no_mapping; /* don't map the next key hits */
5334 cc = 0;
5335 i = 0;
5336 for (;;)
5337 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005338 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_CMDL_INFO
5340 if (!(State & CMDLINE)
5341# ifdef FEAT_MBYTE
5342 && MB_BYTE2LEN_CHECK(nc) == 1
5343# endif
5344 )
5345 add_to_showcmd(nc);
5346#endif
5347 if (nc == 'x' || nc == 'X')
5348 hex = TRUE;
5349 else if (nc == 'o' || nc == 'O')
5350 octal = TRUE;
5351#ifdef FEAT_MBYTE
5352 else if (nc == 'u' || nc == 'U')
5353 unicode = nc;
5354#endif
5355 else
5356 {
5357 if (hex
5358#ifdef FEAT_MBYTE
5359 || unicode != 0
5360#endif
5361 )
5362 {
5363 if (!vim_isxdigit(nc))
5364 break;
5365 cc = cc * 16 + hex2nr(nc);
5366 }
5367 else if (octal)
5368 {
5369 if (nc < '0' || nc > '7')
5370 break;
5371 cc = cc * 8 + nc - '0';
5372 }
5373 else
5374 {
5375 if (!VIM_ISDIGIT(nc))
5376 break;
5377 cc = cc * 10 + nc - '0';
5378 }
5379
5380 ++i;
5381 }
5382
5383 if (cc > 255
5384#ifdef FEAT_MBYTE
5385 && unicode == 0
5386#endif
5387 )
5388 cc = 255; /* limit range to 0-255 */
5389 nc = 0;
5390
5391 if (hex) /* hex: up to two chars */
5392 {
5393 if (i >= 2)
5394 break;
5395 }
5396#ifdef FEAT_MBYTE
5397 else if (unicode) /* Unicode: up to four or eight chars */
5398 {
5399 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5400 break;
5401 }
5402#endif
5403 else if (i >= 3) /* decimal or octal: up to three chars */
5404 break;
5405 }
5406 if (i == 0) /* no number entered */
5407 {
5408 if (nc == K_ZERO) /* NUL is stored as NL */
5409 {
5410 cc = '\n';
5411 nc = 0;
5412 }
5413 else
5414 {
5415 cc = nc;
5416 nc = 0;
5417 }
5418 }
5419
5420 if (cc == 0) /* NUL is stored as NL */
5421 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005422#ifdef FEAT_MBYTE
5423 if (enc_dbcs && (cc & 0xff) == 0)
5424 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5425 second byte will cause trouble! */
5426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
5428 --no_mapping;
5429#ifdef FEAT_GUI
5430 if (gui.in_use)
5431 --allow_keys;
5432#endif
5433 if (nc)
5434 vungetc(nc);
5435 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5436 return cc;
5437}
5438
5439/*
5440 * Insert character, taking care of special keys and mod_mask
5441 */
5442 static void
5443insert_special(c, allow_modmask, ctrlv)
5444 int c;
5445 int allow_modmask;
5446 int ctrlv; /* c was typed after CTRL-V */
5447{
5448 char_u *p;
5449 int len;
5450
5451 /*
5452 * Special function key, translate into "<Key>". Up to the last '>' is
5453 * inserted with ins_str(), so as not to replace characters in replace
5454 * mode.
5455 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5456 * unless 'allow_modmask' is TRUE.
5457 */
5458#ifdef MACOS
5459 /* Command-key never produces a normal key */
5460 if (mod_mask & MOD_MASK_CMD)
5461 allow_modmask = TRUE;
5462#endif
5463 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5464 {
5465 p = get_special_key_name(c, mod_mask);
5466 len = (int)STRLEN(p);
5467 c = p[len - 1];
5468 if (len > 2)
5469 {
5470 if (stop_arrow() == FAIL)
5471 return;
5472 p[len - 1] = NUL;
5473 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005474 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 ctrlv = FALSE;
5476 }
5477 }
5478 if (stop_arrow() == OK)
5479 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5480}
5481
5482/*
5483 * Special characters in this context are those that need processing other
5484 * than the simple insertion that can be performed here. This includes ESC
5485 * which terminates the insert, and CR/NL which need special processing to
5486 * open up a new line. This routine tries to optimize insertions performed by
5487 * the "redo", "undo" or "put" commands, so it needs to know when it should
5488 * stop and defer processing to the "normal" mechanism.
5489 * '0' and '^' are special, because they can be followed by CTRL-D.
5490 */
5491#ifdef EBCDIC
5492# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5493#else
5494# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5495#endif
5496
5497#ifdef FEAT_MBYTE
5498# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5499#else
5500# define WHITECHAR(cc) vim_iswhite(cc)
5501#endif
5502
5503 void
5504insertchar(c, flags, second_indent)
5505 int c; /* character to insert or NUL */
5506 int flags; /* INSCHAR_FORMAT, etc. */
5507 int second_indent; /* indent for second line if >= 0 */
5508{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 int textwidth;
5510#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5516 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518 /*
5519 * Try to break the line in two or more pieces when:
5520 * - Always do this if we have been called to do formatting only.
5521 * - Always do this when 'formatoptions' has the 'a' flag and the line
5522 * ends in white space.
5523 * - Otherwise:
5524 * - Don't do this if inserting a blank
5525 * - Don't do this if an existing character is being replaced, unless
5526 * we're in VREPLACE mode.
5527 * - Do this if the cursor is not on the line where insert started
5528 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5529 * before the insert.
5530 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5531 * before 'textwidth'
5532 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005533 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 && ((flags & INSCHAR_FORMAT)
5535 || (!vim_iswhite(c)
5536 && !((State & REPLACE_FLAG)
5537#ifdef FEAT_VREPLACE
5538 && !(State & VREPLACE_FLAG)
5539#endif
5540 && *ml_get_cursor() != NUL)
5541 && (curwin->w_cursor.lnum != Insstart.lnum
5542 || ((!has_format_option(FO_INS_LONG)
5543 || Insstart_textlen <= (colnr_T)textwidth)
5544 && (!fo_ins_blank
5545 || Insstart_blank_vcol <= (colnr_T)textwidth
5546 ))))))
5547 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005548 /* Format with 'formatexpr' when it's set. Use internal formatting
5549 * when 'formatexpr' isn't set or it returns non-zero. */
5550#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005551 int do_internal = TRUE;
5552
Bram Moolenaar81a82092008-03-12 16:27:00 +00005553 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005554 {
5555 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5556 /* It may be required to save for undo again, e.g. when setline()
5557 * was called. */
5558 ins_need_undo = TRUE;
5559 }
5560 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005562 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (c == NUL) /* only formatting was wanted */
5566 return;
5567
5568#ifdef FEAT_COMMENTS
5569 /* Check whether this character should end a comment. */
5570 if (did_ai && (int)c == end_comment_pending)
5571 {
5572 char_u *line;
5573 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5574 int middle_len, end_len;
5575 int i;
5576
5577 /*
5578 * Need to remove existing (middle) comment leader and insert end
5579 * comment leader. First, check what comment leader we can find.
5580 */
5581 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5582 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5583 {
5584 /* Skip middle-comment string */
5585 while (*p && p[-1] != ':') /* find end of middle flags */
5586 ++p;
5587 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5588 /* Don't count trailing white space for middle_len */
5589 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5590 --middle_len;
5591
5592 /* Find the end-comment string */
5593 while (*p && p[-1] != ':') /* find end of end flags */
5594 ++p;
5595 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5596
5597 /* Skip white space before the cursor */
5598 i = curwin->w_cursor.col;
5599 while (--i >= 0 && vim_iswhite(line[i]))
5600 ;
5601 i++;
5602
5603 /* Skip to before the middle leader */
5604 i -= middle_len;
5605
5606 /* Check some expected things before we go on */
5607 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5608 {
5609 /* Backspace over all the stuff we want to replace */
5610 backspace_until_column(i);
5611
5612 /*
5613 * Insert the end-comment string, except for the last
5614 * character, which will get inserted as normal later.
5615 */
5616 ins_bytes_len(lead_end, end_len - 1);
5617 }
5618 }
5619 }
5620 end_comment_pending = NUL;
5621#endif
5622
5623 did_ai = FALSE;
5624#ifdef FEAT_SMARTINDENT
5625 did_si = FALSE;
5626 can_si = FALSE;
5627 can_si_back = FALSE;
5628#endif
5629
5630 /*
5631 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5632 * This speeds up normal text input considerably.
5633 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5634 * need to re-indent at a ':', or any other character (but not what
5635 * 'paste' is set)..
5636 */
5637#ifdef USE_ON_FLY_SCROLL
5638 dont_scroll = FALSE; /* allow scrolling here */
5639#endif
5640
5641 if ( !ISSPECIAL(c)
5642#ifdef FEAT_MBYTE
5643 && (!has_mbyte || (*mb_char2len)(c) == 1)
5644#endif
5645 && vpeekc() != NUL
5646 && !(State & REPLACE_FLAG)
5647#ifdef FEAT_CINDENT
5648 && !cindent_on()
5649#endif
5650#ifdef FEAT_RIGHTLEFT
5651 && !p_ri
5652#endif
5653 )
5654 {
5655#define INPUT_BUFLEN 100
5656 char_u buf[INPUT_BUFLEN + 1];
5657 int i;
5658 colnr_T virtcol = 0;
5659
5660 buf[0] = c;
5661 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005662 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 virtcol = get_nolist_virtcol();
5664 /*
5665 * Stop the string when:
5666 * - no more chars available
5667 * - finding a special character (command key)
5668 * - buffer is full
5669 * - running into the 'textwidth' boundary
5670 * - need to check for abbreviation: A non-word char after a word-char
5671 */
5672 while ( (c = vpeekc()) != NUL
5673 && !ISSPECIAL(c)
5674#ifdef FEAT_MBYTE
5675 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5676#endif
5677 && i < INPUT_BUFLEN
5678 && (textwidth == 0
5679 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5680 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5681 {
5682#ifdef FEAT_RIGHTLEFT
5683 c = vgetc();
5684 if (p_hkmap && KeyTyped)
5685 c = hkmap(c); /* Hebrew mode mapping */
5686# ifdef FEAT_FKMAP
5687 if (p_fkmap && KeyTyped)
5688 c = fkmap(c); /* Farsi mode mapping */
5689# endif
5690 buf[i++] = c;
5691#else
5692 buf[i++] = vgetc();
5693#endif
5694 }
5695
5696#ifdef FEAT_DIGRAPHS
5697 do_digraph(-1); /* clear digraphs */
5698 do_digraph(buf[i-1]); /* may be the start of a digraph */
5699#endif
5700 buf[i] = NUL;
5701 ins_str(buf);
5702 if (flags & INSCHAR_CTRLV)
5703 {
5704 redo_literal(*buf);
5705 i = 1;
5706 }
5707 else
5708 i = 0;
5709 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005710 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 else
5713 {
5714#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005715 int cc;
5716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5718 {
5719 char_u buf[MB_MAXBYTES + 1];
5720
5721 (*mb_char2bytes)(c, buf);
5722 buf[cc] = NUL;
5723 ins_char_bytes(buf, cc);
5724 AppendCharToRedobuff(c);
5725 }
5726 else
5727#endif
5728 {
5729 ins_char(c);
5730 if (flags & INSCHAR_CTRLV)
5731 redo_literal(c);
5732 else
5733 AppendCharToRedobuff(c);
5734 }
5735 }
5736}
5737
5738/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005739 * Format text at the current insert position.
5740 */
5741 static void
5742internal_format(textwidth, second_indent, flags, format_only)
5743 int textwidth;
5744 int second_indent;
5745 int flags;
5746 int format_only;
5747{
5748 int cc;
5749 int save_char = NUL;
5750 int haveto_redraw = FALSE;
5751 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5752#ifdef FEAT_MBYTE
5753 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5754#endif
5755 int fo_white_par = has_format_option(FO_WHITE_PAR);
5756 int first_line = TRUE;
5757#ifdef FEAT_COMMENTS
5758 colnr_T leader_len;
5759 int no_leader = FALSE;
5760 int do_comments = (flags & INSCHAR_DO_COM);
5761#endif
5762
5763 /*
5764 * When 'ai' is off we don't want a space under the cursor to be
5765 * deleted. Replace it with an 'x' temporarily.
5766 */
5767 if (!curbuf->b_p_ai)
5768 {
5769 cc = gchar_cursor();
5770 if (vim_iswhite(cc))
5771 {
5772 save_char = cc;
5773 pchar_cursor('x');
5774 }
5775 }
5776
5777 /*
5778 * Repeat breaking lines, until the current line is not too long.
5779 */
5780 while (!got_int)
5781 {
5782 int startcol; /* Cursor column at entry */
5783 int wantcol; /* column at textwidth border */
5784 int foundcol; /* column for start of spaces */
5785 int end_foundcol = 0; /* column for start of word */
5786 colnr_T len;
5787 colnr_T virtcol;
5788#ifdef FEAT_VREPLACE
5789 int orig_col = 0;
5790 char_u *saved_text = NULL;
5791#endif
5792 colnr_T col;
5793
5794 virtcol = get_nolist_virtcol();
5795 if (virtcol < (colnr_T)textwidth)
5796 break;
5797
5798#ifdef FEAT_COMMENTS
5799 if (no_leader)
5800 do_comments = FALSE;
5801 else if (!(flags & INSCHAR_FORMAT)
5802 && has_format_option(FO_WRAP_COMS))
5803 do_comments = TRUE;
5804
5805 /* Don't break until after the comment leader */
5806 if (do_comments)
5807 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5808 else
5809 leader_len = 0;
5810
5811 /* If the line doesn't start with a comment leader, then don't
5812 * start one in a following broken line. Avoids that a %word
5813 * moved to the start of the next line causes all following lines
5814 * to start with %. */
5815 if (leader_len == 0)
5816 no_leader = TRUE;
5817#endif
5818 if (!(flags & INSCHAR_FORMAT)
5819#ifdef FEAT_COMMENTS
5820 && leader_len == 0
5821#endif
5822 && !has_format_option(FO_WRAP))
5823
5824 {
5825 textwidth = 0;
5826 break;
5827 }
5828 if ((startcol = curwin->w_cursor.col) == 0)
5829 break;
5830
5831 /* find column of textwidth border */
5832 coladvance((colnr_T)textwidth);
5833 wantcol = curwin->w_cursor.col;
5834
5835 curwin->w_cursor.col = startcol - 1;
5836#ifdef FEAT_MBYTE
5837 /* Correct cursor for multi-byte character. */
5838 if (has_mbyte)
5839 mb_adjust_cursor();
5840#endif
5841 foundcol = 0;
5842
5843 /*
5844 * Find position to break at.
5845 * Stop at first entered white when 'formatoptions' has 'v'
5846 */
5847 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5848 || curwin->w_cursor.lnum != Insstart.lnum
5849 || curwin->w_cursor.col >= Insstart.col)
5850 {
5851 cc = gchar_cursor();
5852 if (WHITECHAR(cc))
5853 {
5854 /* remember position of blank just before text */
5855 end_foundcol = curwin->w_cursor.col;
5856
5857 /* find start of sequence of blanks */
5858 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5859 {
5860 dec_cursor();
5861 cc = gchar_cursor();
5862 }
5863 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5864 break; /* only spaces in front of text */
5865#ifdef FEAT_COMMENTS
5866 /* Don't break until after the comment leader */
5867 if (curwin->w_cursor.col < leader_len)
5868 break;
5869#endif
5870 if (has_format_option(FO_ONE_LETTER))
5871 {
5872 /* do not break after one-letter words */
5873 if (curwin->w_cursor.col == 0)
5874 break; /* one-letter word at begin */
5875
5876 col = curwin->w_cursor.col;
5877 dec_cursor();
5878 cc = gchar_cursor();
5879
5880 if (WHITECHAR(cc))
5881 continue; /* one-letter, continue */
5882 curwin->w_cursor.col = col;
5883 }
5884#ifdef FEAT_MBYTE
5885 if (has_mbyte)
5886 foundcol = curwin->w_cursor.col
5887 + (*mb_ptr2len)(ml_get_cursor());
5888 else
5889#endif
5890 foundcol = curwin->w_cursor.col + 1;
5891 if (curwin->w_cursor.col < (colnr_T)wantcol)
5892 break;
5893 }
5894#ifdef FEAT_MBYTE
5895 else if (cc >= 0x100 && fo_multibyte
5896 && curwin->w_cursor.col <= (colnr_T)wantcol)
5897 {
5898 /* Break after or before a multi-byte character. */
5899 foundcol = curwin->w_cursor.col;
5900 if (curwin->w_cursor.col < (colnr_T)wantcol)
5901 foundcol += (*mb_char2len)(cc);
5902 end_foundcol = foundcol;
5903 break;
5904 }
5905#endif
5906 if (curwin->w_cursor.col == 0)
5907 break;
5908 dec_cursor();
5909 }
5910
5911 if (foundcol == 0) /* no spaces, cannot break line */
5912 {
5913 curwin->w_cursor.col = startcol;
5914 break;
5915 }
5916
5917 /* Going to break the line, remove any "$" now. */
5918 undisplay_dollar();
5919
5920 /*
5921 * Offset between cursor position and line break is used by replace
5922 * stack functions. VREPLACE does not use this, and backspaces
5923 * over the text instead.
5924 */
5925#ifdef FEAT_VREPLACE
5926 if (State & VREPLACE_FLAG)
5927 orig_col = startcol; /* Will start backspacing from here */
5928 else
5929#endif
5930 replace_offset = startcol - end_foundcol - 1;
5931
5932 /*
5933 * adjust startcol for spaces that will be deleted and
5934 * characters that will remain on top line
5935 */
5936 curwin->w_cursor.col = foundcol;
5937 while (cc = gchar_cursor(), WHITECHAR(cc))
5938 inc_cursor();
5939 startcol -= curwin->w_cursor.col;
5940 if (startcol < 0)
5941 startcol = 0;
5942
5943#ifdef FEAT_VREPLACE
5944 if (State & VREPLACE_FLAG)
5945 {
5946 /*
5947 * In VREPLACE mode, we will backspace over the text to be
5948 * wrapped, so save a copy now to put on the next line.
5949 */
5950 saved_text = vim_strsave(ml_get_cursor());
5951 curwin->w_cursor.col = orig_col;
5952 if (saved_text == NULL)
5953 break; /* Can't do it, out of memory */
5954 saved_text[startcol] = NUL;
5955
5956 /* Backspace over characters that will move to the next line */
5957 if (!fo_white_par)
5958 backspace_until_column(foundcol);
5959 }
5960 else
5961#endif
5962 {
5963 /* put cursor after pos. to break line */
5964 if (!fo_white_par)
5965 curwin->w_cursor.col = foundcol;
5966 }
5967
5968 /*
5969 * Split the line just before the margin.
5970 * Only insert/delete lines, but don't really redraw the window.
5971 */
5972 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5973 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5974#ifdef FEAT_COMMENTS
5975 + (do_comments ? OPENLINE_DO_COM : 0)
5976#endif
5977 , old_indent);
5978 old_indent = 0;
5979
5980 replace_offset = 0;
5981 if (first_line)
5982 {
5983 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5984 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5985 if (second_indent >= 0)
5986 {
5987#ifdef FEAT_VREPLACE
5988 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00005989 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005990 else
5991#endif
5992 (void)set_indent(second_indent, SIN_CHANGED);
5993 }
5994 first_line = FALSE;
5995 }
5996
5997#ifdef FEAT_VREPLACE
5998 if (State & VREPLACE_FLAG)
5999 {
6000 /*
6001 * In VREPLACE mode we have backspaced over the text to be
6002 * moved, now we re-insert it into the new line.
6003 */
6004 ins_bytes(saved_text);
6005 vim_free(saved_text);
6006 }
6007 else
6008#endif
6009 {
6010 /*
6011 * Check if cursor is not past the NUL off the line, cindent
6012 * may have added or removed indent.
6013 */
6014 curwin->w_cursor.col += startcol;
6015 len = (colnr_T)STRLEN(ml_get_curline());
6016 if (curwin->w_cursor.col > len)
6017 curwin->w_cursor.col = len;
6018 }
6019
6020 haveto_redraw = TRUE;
6021#ifdef FEAT_CINDENT
6022 can_cindent = TRUE;
6023#endif
6024 /* moved the cursor, don't autoindent or cindent now */
6025 did_ai = FALSE;
6026#ifdef FEAT_SMARTINDENT
6027 did_si = FALSE;
6028 can_si = FALSE;
6029 can_si_back = FALSE;
6030#endif
6031 line_breakcheck();
6032 }
6033
6034 if (save_char != NUL) /* put back space after cursor */
6035 pchar_cursor(save_char);
6036
6037 if (!format_only && haveto_redraw)
6038 {
6039 update_topline();
6040 redraw_curbuf_later(VALID);
6041 }
6042}
6043
6044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 * Called after inserting or deleting text: When 'formatoptions' includes the
6046 * 'a' flag format from the current line until the end of the paragraph.
6047 * Keep the cursor at the same position relative to the text.
6048 * The caller must have saved the cursor line for undo, following ones will be
6049 * saved here.
6050 */
6051 void
6052auto_format(trailblank, prev_line)
6053 int trailblank; /* when TRUE also format with trailing blank */
6054 int prev_line; /* may start in previous line */
6055{
6056 pos_T pos;
6057 colnr_T len;
6058 char_u *old;
6059 char_u *new, *pnew;
6060 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006061 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
6063 if (!has_format_option(FO_AUTO))
6064 return;
6065
6066 pos = curwin->w_cursor;
6067 old = ml_get_curline();
6068
6069 /* may remove added space */
6070 check_auto_format(FALSE);
6071
6072 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6073 * user might insert normal text next. Also skip formatting when "1" is
6074 * in 'formatoptions' and there is a single character before the cursor.
6075 * Otherwise the line would be broken and when typing another non-white
6076 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006077 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (*old != NUL && !trailblank && wasatend)
6079 {
6080 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006081 cc = gchar_cursor();
6082 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6083 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006085 cc = gchar_cursor();
6086 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 {
6088 curwin->w_cursor = pos;
6089 return;
6090 }
6091 curwin->w_cursor = pos;
6092 }
6093
6094#ifdef FEAT_COMMENTS
6095 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6096 * comments. */
6097 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6098 && get_leader_len(old, NULL, FALSE) == 0)
6099 return;
6100#endif
6101
6102 /*
6103 * May start formatting in a previous line, so that after "x" a word is
6104 * moved to the previous line if it fits there now. Only when this is not
6105 * the start of a paragraph.
6106 */
6107 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6108 {
6109 --curwin->w_cursor.lnum;
6110 if (u_save_cursor() == FAIL)
6111 return;
6112 }
6113
6114 /*
6115 * Do the formatting and restore the cursor position. "saved_cursor" will
6116 * be adjusted for the text formatting.
6117 */
6118 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006119 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 curwin->w_cursor = saved_cursor;
6121 saved_cursor.lnum = 0;
6122
6123 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6124 {
6125 /* "cannot happen" */
6126 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6127 coladvance((colnr_T)MAXCOL);
6128 }
6129 else
6130 check_cursor_col();
6131
6132 /* Insert mode: If the cursor is now after the end of the line while it
6133 * previously wasn't, the line was broken. Because of the rule above we
6134 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6135 * formatted. */
6136 if (!wasatend && has_format_option(FO_WHITE_PAR))
6137 {
6138 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006139 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (curwin->w_cursor.col == len)
6141 {
6142 pnew = vim_strnsave(new, len + 2);
6143 pnew[len] = ' ';
6144 pnew[len + 1] = NUL;
6145 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6146 /* remove the space later */
6147 did_add_space = TRUE;
6148 }
6149 else
6150 /* may remove added space */
6151 check_auto_format(FALSE);
6152 }
6153
6154 check_cursor();
6155}
6156
6157/*
6158 * When an extra space was added to continue a paragraph for auto-formatting,
6159 * delete it now. The space must be under the cursor, just after the insert
6160 * position.
6161 */
6162 static void
6163check_auto_format(end_insert)
6164 int end_insert; /* TRUE when ending Insert mode */
6165{
6166 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006167 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
6169 if (did_add_space)
6170 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006171 cc = gchar_cursor();
6172 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 /* Somehow the space was removed already. */
6174 did_add_space = FALSE;
6175 else
6176 {
6177 if (!end_insert)
6178 {
6179 inc_cursor();
6180 c = gchar_cursor();
6181 dec_cursor();
6182 }
6183 if (c != NUL)
6184 {
6185 /* The space is no longer at the end of the line, delete it. */
6186 del_char(FALSE);
6187 did_add_space = FALSE;
6188 }
6189 }
6190 }
6191}
6192
6193/*
6194 * Find out textwidth to be used for formatting:
6195 * if 'textwidth' option is set, use it
6196 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6197 * if invalid value, use 0.
6198 * Set default to window width (maximum 79) for "gq" operator.
6199 */
6200 int
6201comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006202 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 int textwidth;
6205
6206 textwidth = curbuf->b_p_tw;
6207 if (textwidth == 0 && curbuf->b_p_wm)
6208 {
6209 /* The width is the window width minus 'wrapmargin' minus all the
6210 * things that add to the margin. */
6211 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6212#ifdef FEAT_CMDWIN
6213 if (cmdwin_type != 0)
6214 textwidth -= 1;
6215#endif
6216#ifdef FEAT_FOLDING
6217 textwidth -= curwin->w_p_fdc;
6218#endif
6219#ifdef FEAT_SIGNS
6220 if (curwin->w_buffer->b_signlist != NULL
6221# ifdef FEAT_NETBEANS_INTG
6222 || usingNetbeans
6223# endif
6224 )
6225 textwidth -= 1;
6226#endif
6227 if (curwin->w_p_nu)
6228 textwidth -= 8;
6229 }
6230 if (textwidth < 0)
6231 textwidth = 0;
6232 if (ff && textwidth == 0)
6233 {
6234 textwidth = W_WIDTH(curwin) - 1;
6235 if (textwidth > 79)
6236 textwidth = 79;
6237 }
6238 return textwidth;
6239}
6240
6241/*
6242 * Put a character in the redo buffer, for when just after a CTRL-V.
6243 */
6244 static void
6245redo_literal(c)
6246 int c;
6247{
6248 char_u buf[10];
6249
6250 /* Only digits need special treatment. Translate them into a string of
6251 * three digits. */
6252 if (VIM_ISDIGIT(c))
6253 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006254 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 AppendToRedobuff(buf);
6256 }
6257 else
6258 AppendCharToRedobuff(c);
6259}
6260
6261/*
6262 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006263 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 */
6265 static void
6266start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006267 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268{
6269 if (!arrow_used) /* something has been inserted */
6270 {
6271 AppendToRedobuff(ESC_STR);
6272 stop_insert(end_insert_pos, FALSE);
6273 arrow_used = TRUE; /* this means we stopped the current insert */
6274 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006275#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006276 check_spell_redraw();
6277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278}
6279
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006280#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006281/*
6282 * If we skipped highlighting word at cursor, do it now.
6283 * It may be skipped again, thus reset spell_redraw_lnum first.
6284 */
6285 static void
6286check_spell_redraw()
6287{
6288 if (spell_redraw_lnum != 0)
6289 {
6290 linenr_T lnum = spell_redraw_lnum;
6291
6292 spell_redraw_lnum = 0;
6293 redrawWinline(lnum, FALSE);
6294 }
6295}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006296
6297/*
6298 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6299 * spelled word, if there is one.
6300 */
6301 static void
6302spell_back_to_badword()
6303{
6304 pos_T tpos = curwin->w_cursor;
6305
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006306 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006307 if (curwin->w_cursor.col != tpos.col)
6308 start_arrow(&tpos);
6309}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006310#endif
6311
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312/*
6313 * stop_arrow() is called before a change is made in insert mode.
6314 * If an arrow key has been used, start a new insertion.
6315 * Returns FAIL if undo is impossible, shouldn't insert then.
6316 */
6317 int
6318stop_arrow()
6319{
6320 if (arrow_used)
6321 {
6322 if (u_save_cursor() == OK)
6323 {
6324 arrow_used = FALSE;
6325 ins_need_undo = FALSE;
6326 }
6327 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006328 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 ai_col = 0;
6330#ifdef FEAT_VREPLACE
6331 if (State & VREPLACE_FLAG)
6332 {
6333 orig_line_count = curbuf->b_ml.ml_line_count;
6334 vr_lines_changed = 1;
6335 }
6336#endif
6337 ResetRedobuff();
6338 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006339 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
6341 else if (ins_need_undo)
6342 {
6343 if (u_save_cursor() == OK)
6344 ins_need_undo = FALSE;
6345 }
6346
6347#ifdef FEAT_FOLDING
6348 /* Always open fold at the cursor line when inserting something. */
6349 foldOpenCursor();
6350#endif
6351
6352 return (arrow_used || ins_need_undo ? FAIL : OK);
6353}
6354
6355/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006356 * Do a few things to stop inserting.
6357 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6358 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 */
6360 static void
6361stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006362 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006363 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006365 int cc;
6366 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368 stop_redo_ins();
6369 replace_flush(); /* abandon replace stack */
6370
6371 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006372 * Save the inserted text for later redo with ^@ and CTRL-A.
6373 * Don't do it when "restart_edit" was set and nothing was inserted,
6374 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006376 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006377 if (did_restart_edit == 0 || (ptr != NULL
6378 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006379 {
6380 vim_free(last_insert);
6381 last_insert = ptr;
6382 last_insert_skip = new_insert_skip;
6383 }
6384 else
6385 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006387 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 /* Auto-format now. It may seem strange to do this when stopping an
6390 * insertion (or moving the cursor), but it's required when appending
6391 * a line and having it end in a space. But only do it when something
6392 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006393 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006395 pos_T tpos = curwin->w_cursor;
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 /* When the cursor is at the end of the line after a space the
6398 * formatting will move it to the following word. Avoid that by
6399 * moving the cursor onto the space. */
6400 cc = 'x';
6401 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6402 {
6403 dec_cursor();
6404 cc = gchar_cursor();
6405 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006406 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408
6409 auto_format(TRUE, FALSE);
6410
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006411 if (vim_iswhite(cc))
6412 {
6413 if (gchar_cursor() != NUL)
6414 inc_cursor();
6415#ifdef FEAT_VIRTUALEDIT
6416 /* If the cursor is still at the same character, also keep
6417 * the "coladd". */
6418 if (gchar_cursor() == NUL
6419 && curwin->w_cursor.lnum == tpos.lnum
6420 && curwin->w_cursor.col == tpos.col)
6421 curwin->w_cursor.coladd = tpos.coladd;
6422#endif
6423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425
6426 /* If a space was inserted for auto-formatting, remove it now. */
6427 check_auto_format(TRUE);
6428
6429 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006430 * of the line, and put the cursor back.
6431 * Do this when ESC was used or moving the cursor up/down. */
6432 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006433 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006435 pos_T tpos = curwin->w_cursor;
6436
6437 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006438 for (;;)
6439 {
6440 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6441 --curwin->w_cursor.col;
6442 cc = gchar_cursor();
6443 if (!vim_iswhite(cc))
6444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006446 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006447 if (curwin->w_cursor.lnum != tpos.lnum)
6448 curwin->w_cursor = tpos;
6449 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6451
6452#ifdef FEAT_VISUAL
6453 /* <C-S-Right> may have started Visual mode, adjust the position for
6454 * deleted characters. */
6455 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6456 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006457 int len = (int)STRLEN(ml_get_curline());
6458
6459 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006461 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462# ifdef FEAT_VIRTUALEDIT
6463 VIsual.coladd = 0;
6464# endif
6465 }
6466 }
6467#endif
6468 }
6469 }
6470 did_ai = FALSE;
6471#ifdef FEAT_SMARTINDENT
6472 did_si = FALSE;
6473 can_si = FALSE;
6474 can_si_back = FALSE;
6475#endif
6476
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006477 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6478 * now in a different buffer. */
6479 if (end_insert_pos != NULL)
6480 {
6481 curbuf->b_op_start = Insstart;
6482 curbuf->b_op_end = *end_insert_pos;
6483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486/*
6487 * Set the last inserted text to a single character.
6488 * Used for the replace command.
6489 */
6490 void
6491set_last_insert(c)
6492 int c;
6493{
6494 char_u *s;
6495
6496 vim_free(last_insert);
6497#ifdef FEAT_MBYTE
6498 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6499#else
6500 last_insert = alloc(6);
6501#endif
6502 if (last_insert != NULL)
6503 {
6504 s = last_insert;
6505 /* Use the CTRL-V only when entering a special char */
6506 if (c < ' ' || c == DEL)
6507 *s++ = Ctrl_V;
6508 s = add_char2buf(c, s);
6509 *s++ = ESC;
6510 *s++ = NUL;
6511 last_insert_skip = 0;
6512 }
6513}
6514
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006515#if defined(EXITFREE) || defined(PROTO)
6516 void
6517free_last_insert()
6518{
6519 vim_free(last_insert);
6520 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006521# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006522 vim_free(compl_orig_text);
6523 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006524# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006525}
6526#endif
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528/*
6529 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6530 * and CSI. Handle multi-byte characters.
6531 * Returns a pointer to after the added bytes.
6532 */
6533 char_u *
6534add_char2buf(c, s)
6535 int c;
6536 char_u *s;
6537{
6538#ifdef FEAT_MBYTE
6539 char_u temp[MB_MAXBYTES];
6540 int i;
6541 int len;
6542
6543 len = (*mb_char2bytes)(c, temp);
6544 for (i = 0; i < len; ++i)
6545 {
6546 c = temp[i];
6547#endif
6548 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6549 if (c == K_SPECIAL)
6550 {
6551 *s++ = K_SPECIAL;
6552 *s++ = KS_SPECIAL;
6553 *s++ = KE_FILLER;
6554 }
6555#ifdef FEAT_GUI
6556 else if (c == CSI)
6557 {
6558 *s++ = CSI;
6559 *s++ = KS_EXTRA;
6560 *s++ = (int)KE_CSI;
6561 }
6562#endif
6563 else
6564 *s++ = c;
6565#ifdef FEAT_MBYTE
6566 }
6567#endif
6568 return s;
6569}
6570
6571/*
6572 * move cursor to start of line
6573 * if flags & BL_WHITE move to first non-white
6574 * if flags & BL_SOL move to first non-white if startofline is set,
6575 * otherwise keep "curswant" column
6576 * if flags & BL_FIX don't leave the cursor on a NUL.
6577 */
6578 void
6579beginline(flags)
6580 int flags;
6581{
6582 if ((flags & BL_SOL) && !p_sol)
6583 coladvance(curwin->w_curswant);
6584 else
6585 {
6586 curwin->w_cursor.col = 0;
6587#ifdef FEAT_VIRTUALEDIT
6588 curwin->w_cursor.coladd = 0;
6589#endif
6590
6591 if (flags & (BL_WHITE | BL_SOL))
6592 {
6593 char_u *ptr;
6594
6595 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6596 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6597 ++curwin->w_cursor.col;
6598 }
6599 curwin->w_set_curswant = TRUE;
6600 }
6601}
6602
6603/*
6604 * oneright oneleft cursor_down cursor_up
6605 *
6606 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006607 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 * Return OK when successful, FAIL when we hit a line of file boundary.
6609 */
6610
6611 int
6612oneright()
6613{
6614 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
6617#ifdef FEAT_VIRTUALEDIT
6618 if (virtual_active())
6619 {
6620 pos_T prevpos = curwin->w_cursor;
6621
6622 /* Adjust for multi-wide char (excluding TAB) */
6623 ptr = ml_get_cursor();
6624 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006625# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006627# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 ))
6631 ? ptr2cells(ptr) : 1));
6632 curwin->w_set_curswant = TRUE;
6633 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6634 return (prevpos.col != curwin->w_cursor.col
6635 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6636 }
6637#endif
6638
6639 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006640 if (*ptr == NUL)
6641 return FAIL; /* already at the very end */
6642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006644 if (has_mbyte)
6645 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 else
6647#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006648 l = 1;
6649
6650 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6651 * contains "onemore". */
6652 if (ptr[l] == NUL
6653#ifdef FEAT_VIRTUALEDIT
6654 && (ve_flags & VE_ONEMORE) == 0
6655#endif
6656 )
6657 return FAIL;
6658 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660 curwin->w_set_curswant = TRUE;
6661 return OK;
6662}
6663
6664 int
6665oneleft()
6666{
6667#ifdef FEAT_VIRTUALEDIT
6668 if (virtual_active())
6669 {
6670 int width;
6671 int v = getviscol();
6672
6673 if (v == 0)
6674 return FAIL;
6675
6676# ifdef FEAT_LINEBREAK
6677 /* We might get stuck on 'showbreak', skip over it. */
6678 width = 1;
6679 for (;;)
6680 {
6681 coladvance(v - width);
6682 /* getviscol() is slow, skip it when 'showbreak' is empty and
6683 * there are no multi-byte characters */
6684 if ((*p_sbr == NUL
6685# ifdef FEAT_MBYTE
6686 && !has_mbyte
6687# endif
6688 ) || getviscol() < v)
6689 break;
6690 ++width;
6691 }
6692# else
6693 coladvance(v - 1);
6694# endif
6695
6696 if (curwin->w_cursor.coladd == 1)
6697 {
6698 char_u *ptr;
6699
6700 /* Adjust for multi-wide char (not a TAB) */
6701 ptr = ml_get_cursor();
6702 if (*ptr != TAB && vim_isprintc(
6703# ifdef FEAT_MBYTE
6704 (*mb_ptr2char)(ptr)
6705# else
6706 *ptr
6707# endif
6708 ) && ptr2cells(ptr) > 1)
6709 curwin->w_cursor.coladd = 0;
6710 }
6711
6712 curwin->w_set_curswant = TRUE;
6713 return OK;
6714 }
6715#endif
6716
6717 if (curwin->w_cursor.col == 0)
6718 return FAIL;
6719
6720 curwin->w_set_curswant = TRUE;
6721 --curwin->w_cursor.col;
6722
6723#ifdef FEAT_MBYTE
6724 /* if the character on the left of the current cursor is a multi-byte
6725 * character, move to its first byte */
6726 if (has_mbyte)
6727 mb_adjust_cursor();
6728#endif
6729 return OK;
6730}
6731
6732 int
6733cursor_up(n, upd_topline)
6734 long n;
6735 int upd_topline; /* When TRUE: update topline */
6736{
6737 linenr_T lnum;
6738
6739 if (n > 0)
6740 {
6741 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006742 /* This fails if the cursor is already in the first line or the count
6743 * is larger than the line number and '-' is in 'cpoptions' */
6744 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 return FAIL;
6746 if (n >= lnum)
6747 lnum = 1;
6748 else
6749#ifdef FEAT_FOLDING
6750 if (hasAnyFolding(curwin))
6751 {
6752 /*
6753 * Count each sequence of folded lines as one logical line.
6754 */
6755 /* go to the the start of the current fold */
6756 (void)hasFolding(lnum, &lnum, NULL);
6757
6758 while (n--)
6759 {
6760 /* move up one line */
6761 --lnum;
6762 if (lnum <= 1)
6763 break;
6764 /* If we entered a fold, move to the beginning, unless in
6765 * Insert mode or when 'foldopen' contains "all": it will open
6766 * in a moment. */
6767 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6768 (void)hasFolding(lnum, &lnum, NULL);
6769 }
6770 if (lnum < 1)
6771 lnum = 1;
6772 }
6773 else
6774#endif
6775 lnum -= n;
6776 curwin->w_cursor.lnum = lnum;
6777 }
6778
6779 /* try to advance to the column we want to be at */
6780 coladvance(curwin->w_curswant);
6781
6782 if (upd_topline)
6783 update_topline(); /* make sure curwin->w_topline is valid */
6784
6785 return OK;
6786}
6787
6788/*
6789 * Cursor down a number of logical lines.
6790 */
6791 int
6792cursor_down(n, upd_topline)
6793 long n;
6794 int upd_topline; /* When TRUE: update topline */
6795{
6796 linenr_T lnum;
6797
6798 if (n > 0)
6799 {
6800 lnum = curwin->w_cursor.lnum;
6801#ifdef FEAT_FOLDING
6802 /* Move to last line of fold, will fail if it's the end-of-file. */
6803 (void)hasFolding(lnum, NULL, &lnum);
6804#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006805 /* This fails if the cursor is already in the last line or would move
6806 * beyound the last line and '-' is in 'cpoptions' */
6807 if (lnum >= curbuf->b_ml.ml_line_count
6808 || (lnum + n > curbuf->b_ml.ml_line_count
6809 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 return FAIL;
6811 if (lnum + n >= curbuf->b_ml.ml_line_count)
6812 lnum = curbuf->b_ml.ml_line_count;
6813 else
6814#ifdef FEAT_FOLDING
6815 if (hasAnyFolding(curwin))
6816 {
6817 linenr_T last;
6818
6819 /* count each sequence of folded lines as one logical line */
6820 while (n--)
6821 {
6822 if (hasFolding(lnum, NULL, &last))
6823 lnum = last + 1;
6824 else
6825 ++lnum;
6826 if (lnum >= curbuf->b_ml.ml_line_count)
6827 break;
6828 }
6829 if (lnum > curbuf->b_ml.ml_line_count)
6830 lnum = curbuf->b_ml.ml_line_count;
6831 }
6832 else
6833#endif
6834 lnum += n;
6835 curwin->w_cursor.lnum = lnum;
6836 }
6837
6838 /* try to advance to the column we want to be at */
6839 coladvance(curwin->w_curswant);
6840
6841 if (upd_topline)
6842 update_topline(); /* make sure curwin->w_topline is valid */
6843
6844 return OK;
6845}
6846
6847/*
6848 * Stuff the last inserted text in the read buffer.
6849 * Last_insert actually is a copy of the redo buffer, so we
6850 * first have to remove the command.
6851 */
6852 int
6853stuff_inserted(c, count, no_esc)
6854 int c; /* Command character to be inserted */
6855 long count; /* Repeat this many times */
6856 int no_esc; /* Don't add an ESC at the end */
6857{
6858 char_u *esc_ptr;
6859 char_u *ptr;
6860 char_u *last_ptr;
6861 char_u last = NUL;
6862
6863 ptr = get_last_insert();
6864 if (ptr == NULL)
6865 {
6866 EMSG(_(e_noinstext));
6867 return FAIL;
6868 }
6869
6870 /* may want to stuff the command character, to start Insert mode */
6871 if (c != NUL)
6872 stuffcharReadbuff(c);
6873 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6874 *esc_ptr = NUL; /* remove the ESC */
6875
6876 /* when the last char is either "0" or "^" it will be quoted if no ESC
6877 * comes after it OR if it will inserted more than once and "ptr"
6878 * starts with ^D. -- Acevedo
6879 */
6880 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6881 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6882 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6883 {
6884 last = *last_ptr;
6885 *last_ptr = NUL;
6886 }
6887
6888 do
6889 {
6890 stuffReadbuff(ptr);
6891 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6892 if (last)
6893 stuffReadbuff((char_u *)(last == '0'
6894 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6895 : IF_EB("\026^", CTRL_V_STR "^")));
6896 }
6897 while (--count > 0);
6898
6899 if (last)
6900 *last_ptr = last;
6901
6902 if (esc_ptr != NULL)
6903 *esc_ptr = ESC; /* put the ESC back */
6904
6905 /* may want to stuff a trailing ESC, to get out of Insert mode */
6906 if (!no_esc)
6907 stuffcharReadbuff(ESC);
6908
6909 return OK;
6910}
6911
6912 char_u *
6913get_last_insert()
6914{
6915 if (last_insert == NULL)
6916 return NULL;
6917 return last_insert + last_insert_skip;
6918}
6919
6920/*
6921 * Get last inserted string, and remove trailing <Esc>.
6922 * Returns pointer to allocated memory (must be freed) or NULL.
6923 */
6924 char_u *
6925get_last_insert_save()
6926{
6927 char_u *s;
6928 int len;
6929
6930 if (last_insert == NULL)
6931 return NULL;
6932 s = vim_strsave(last_insert + last_insert_skip);
6933 if (s != NULL)
6934 {
6935 len = (int)STRLEN(s);
6936 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6937 s[len - 1] = NUL;
6938 }
6939 return s;
6940}
6941
6942/*
6943 * Check the word in front of the cursor for an abbreviation.
6944 * Called when the non-id character "c" has been entered.
6945 * When an abbreviation is recognized it is removed from the text and
6946 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6947 */
6948 static int
6949echeck_abbr(c)
6950 int c;
6951{
6952 /* Don't check for abbreviation in paste mode, when disabled and just
6953 * after moving around with cursor keys. */
6954 if (p_paste || no_abbr || arrow_used)
6955 return FALSE;
6956
6957 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6958 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6959}
6960
6961/*
6962 * replace-stack functions
6963 *
6964 * When replacing characters, the replaced characters are remembered for each
6965 * new character. This is used to re-insert the old text when backspacing.
6966 *
6967 * There is a NUL headed list of characters for each character that is
6968 * currently in the file after the insertion point. When BS is used, one NUL
6969 * headed list is put back for the deleted character.
6970 *
6971 * For a newline, there are two NUL headed lists. One contains the characters
6972 * that the NL replaced. The extra one stores the characters after the cursor
6973 * that were deleted (always white space).
6974 *
6975 * Replace_offset is normally 0, in which case replace_push will add a new
6976 * character at the end of the stack. If replace_offset is not 0, that many
6977 * characters will be left on the stack above the newly inserted character.
6978 */
6979
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006980static char_u *replace_stack = NULL;
6981static long replace_stack_nr = 0; /* next entry in replace stack */
6982static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 void
6985replace_push(c)
6986 int c; /* character that is replaced (NUL is none) */
6987{
6988 char_u *p;
6989
6990 if (replace_stack_nr < replace_offset) /* nothing to do */
6991 return;
6992 if (replace_stack_len <= replace_stack_nr)
6993 {
6994 replace_stack_len += 50;
6995 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6996 if (p == NULL) /* out of memory */
6997 {
6998 replace_stack_len -= 50;
6999 return;
7000 }
7001 if (replace_stack != NULL)
7002 {
7003 mch_memmove(p, replace_stack,
7004 (size_t)(replace_stack_nr * sizeof(char_u)));
7005 vim_free(replace_stack);
7006 }
7007 replace_stack = p;
7008 }
7009 p = replace_stack + replace_stack_nr - replace_offset;
7010 if (replace_offset)
7011 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7012 *p = c;
7013 ++replace_stack_nr;
7014}
7015
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007016#if defined(FEAT_MBYTE) || defined(PROTO)
7017/*
7018 * Push a character onto the replace stack. Handles a multi-byte character in
7019 * reverse byte order, so that the first byte is popped off first.
7020 * Return the number of bytes done (includes composing characters).
7021 */
7022 int
7023replace_push_mb(p)
7024 char_u *p;
7025{
7026 int l = (*mb_ptr2len)(p);
7027 int j;
7028
7029 for (j = l - 1; j >= 0; --j)
7030 replace_push(p[j]);
7031 return l;
7032}
7033#endif
7034
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007035#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036/*
7037 * call replace_push(c) with replace_offset set to the first NUL.
7038 */
7039 static void
7040replace_push_off(c)
7041 int c;
7042{
7043 char_u *p;
7044
7045 p = replace_stack + replace_stack_nr;
7046 for (replace_offset = 1; replace_offset < replace_stack_nr;
7047 ++replace_offset)
7048 if (*--p == NUL)
7049 break;
7050 replace_push(c);
7051 replace_offset = 0;
7052}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055/*
7056 * Pop one item from the replace stack.
7057 * return -1 if stack empty
7058 * return replaced character or NUL otherwise
7059 */
7060 static int
7061replace_pop()
7062{
7063 if (replace_stack_nr == 0)
7064 return -1;
7065 return (int)replace_stack[--replace_stack_nr];
7066}
7067
7068/*
7069 * Join the top two items on the replace stack. This removes to "off"'th NUL
7070 * encountered.
7071 */
7072 static void
7073replace_join(off)
7074 int off; /* offset for which NUL to remove */
7075{
7076 int i;
7077
7078 for (i = replace_stack_nr; --i >= 0; )
7079 if (replace_stack[i] == NUL && off-- <= 0)
7080 {
7081 --replace_stack_nr;
7082 mch_memmove(replace_stack + i, replace_stack + i + 1,
7083 (size_t)(replace_stack_nr - i));
7084 return;
7085 }
7086}
7087
7088/*
7089 * Pop bytes from the replace stack until a NUL is found, and insert them
7090 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7091 */
7092 static void
7093replace_pop_ins()
7094{
7095 int cc;
7096 int oldState = State;
7097
7098 State = NORMAL; /* don't want REPLACE here */
7099 while ((cc = replace_pop()) > 0)
7100 {
7101#ifdef FEAT_MBYTE
7102 mb_replace_pop_ins(cc);
7103#else
7104 ins_char(cc);
7105#endif
7106 dec_cursor();
7107 }
7108 State = oldState;
7109}
7110
7111#ifdef FEAT_MBYTE
7112/*
7113 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7114 * indicates a multi-byte char, pop the other bytes too.
7115 */
7116 static void
7117mb_replace_pop_ins(cc)
7118 int cc;
7119{
7120 int n;
7121 char_u buf[MB_MAXBYTES];
7122 int i;
7123 int c;
7124
7125 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7126 {
7127 buf[0] = cc;
7128 for (i = 1; i < n; ++i)
7129 buf[i] = replace_pop();
7130 ins_bytes_len(buf, n);
7131 }
7132 else
7133 ins_char(cc);
7134
7135 if (enc_utf8)
7136 /* Handle composing chars. */
7137 for (;;)
7138 {
7139 c = replace_pop();
7140 if (c == -1) /* stack empty */
7141 break;
7142 if ((n = MB_BYTE2LEN(c)) == 1)
7143 {
7144 /* Not a multi-byte char, put it back. */
7145 replace_push(c);
7146 break;
7147 }
7148 else
7149 {
7150 buf[0] = c;
7151 for (i = 1; i < n; ++i)
7152 buf[i] = replace_pop();
7153 if (utf_iscomposing(utf_ptr2char(buf)))
7154 ins_bytes_len(buf, n);
7155 else
7156 {
7157 /* Not a composing char, put it back. */
7158 for (i = n - 1; i >= 0; --i)
7159 replace_push(buf[i]);
7160 break;
7161 }
7162 }
7163 }
7164}
7165#endif
7166
7167/*
7168 * make the replace stack empty
7169 * (called when exiting replace mode)
7170 */
7171 static void
7172replace_flush()
7173{
7174 vim_free(replace_stack);
7175 replace_stack = NULL;
7176 replace_stack_len = 0;
7177 replace_stack_nr = 0;
7178}
7179
7180/*
7181 * Handle doing a BS for one character.
7182 * cc < 0: replace stack empty, just move cursor
7183 * cc == 0: character was inserted, delete it
7184 * cc > 0: character was replaced, put cc (first byte of original char) back
7185 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007186 * When "limit_col" is >= 0, don't delete before this column. Matters when
7187 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 */
7189 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007190replace_do_bs(limit_col)
7191 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 int cc;
7194#ifdef FEAT_VREPLACE
7195 int orig_len = 0;
7196 int ins_len;
7197 int orig_vcols = 0;
7198 colnr_T start_vcol;
7199 char_u *p;
7200 int i;
7201 int vcol;
7202#endif
7203
7204 cc = replace_pop();
7205 if (cc > 0)
7206 {
7207#ifdef FEAT_VREPLACE
7208 if (State & VREPLACE_FLAG)
7209 {
7210 /* Get the number of screen cells used by the character we are
7211 * going to delete. */
7212 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7213 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7214 }
7215#endif
7216#ifdef FEAT_MBYTE
7217 if (has_mbyte)
7218 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007219 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220# ifdef FEAT_VREPLACE
7221 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007222 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223# endif
7224 replace_push(cc);
7225 }
7226 else
7227#endif
7228 {
7229 pchar_cursor(cc);
7230#ifdef FEAT_VREPLACE
7231 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007232 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#endif
7234 }
7235 replace_pop_ins();
7236
7237#ifdef FEAT_VREPLACE
7238 if (State & VREPLACE_FLAG)
7239 {
7240 /* Get the number of screen cells used by the inserted characters */
7241 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007242 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 vcol = start_vcol;
7244 for (i = 0; i < ins_len; ++i)
7245 {
7246 vcol += chartabsize(p + i, vcol);
7247#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007248 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249#endif
7250 }
7251 vcol -= start_vcol;
7252
7253 /* Delete spaces that were inserted after the cursor to keep the
7254 * text aligned. */
7255 curwin->w_cursor.col += ins_len;
7256 while (vcol > orig_vcols && gchar_cursor() == ' ')
7257 {
7258 del_char(FALSE);
7259 ++orig_vcols;
7260 }
7261 curwin->w_cursor.col -= ins_len;
7262 }
7263#endif
7264
7265 /* mark the buffer as changed and prepare for displaying */
7266 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7267 }
7268 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007269 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
7272#ifdef FEAT_CINDENT
7273/*
7274 * Return TRUE if C-indenting is on.
7275 */
7276 static int
7277cindent_on()
7278{
7279 return (!p_paste && (curbuf->b_p_cin
7280# ifdef FEAT_EVAL
7281 || *curbuf->b_p_inde != NUL
7282# endif
7283 ));
7284}
7285#endif
7286
7287#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7288/*
7289 * Re-indent the current line, based on the current contents of it and the
7290 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7291 * confused what all the part that handles Control-T is doing that I'm not.
7292 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7293 */
7294
7295 void
7296fixthisline(get_the_indent)
7297 int (*get_the_indent) __ARGS((void));
7298{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007299 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 if (linewhite(curwin->w_cursor.lnum))
7301 did_ai = TRUE; /* delete the indent if the line stays empty */
7302}
7303
7304 void
7305fix_indent()
7306{
7307 if (p_paste)
7308 return;
7309# ifdef FEAT_LISP
7310 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7311 fixthisline(get_lisp_indent);
7312# endif
7313# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7314 else
7315# endif
7316# ifdef FEAT_CINDENT
7317 if (cindent_on())
7318 do_c_expr_indent();
7319# endif
7320}
7321
7322#endif
7323
7324#ifdef FEAT_CINDENT
7325/*
7326 * return TRUE if 'cinkeys' contains the key "keytyped",
7327 * when == '*': Only if key is preceded with '*' (indent before insert)
7328 * when == '!': Only if key is prededed with '!' (don't insert)
7329 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7330 *
7331 * "keytyped" can have a few special values:
7332 * KEY_OPEN_FORW
7333 * KEY_OPEN_BACK
7334 * KEY_COMPLETE just finished completion.
7335 *
7336 * If line_is_empty is TRUE accept keys with '0' before them.
7337 */
7338 int
7339in_cinkeys(keytyped, when, line_is_empty)
7340 int keytyped;
7341 int when;
7342 int line_is_empty;
7343{
7344 char_u *look;
7345 int try_match;
7346 int try_match_word;
7347 char_u *p;
7348 char_u *line;
7349 int icase;
7350 int i;
7351
7352#ifdef FEAT_EVAL
7353 if (*curbuf->b_p_inde != NUL)
7354 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7355 else
7356#endif
7357 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7358 while (*look)
7359 {
7360 /*
7361 * Find out if we want to try a match with this key, depending on
7362 * 'when' and a '*' or '!' before the key.
7363 */
7364 switch (when)
7365 {
7366 case '*': try_match = (*look == '*'); break;
7367 case '!': try_match = (*look == '!'); break;
7368 default: try_match = (*look != '*'); break;
7369 }
7370 if (*look == '*' || *look == '!')
7371 ++look;
7372
7373 /*
7374 * If there is a '0', only accept a match if the line is empty.
7375 * But may still match when typing last char of a word.
7376 */
7377 if (*look == '0')
7378 {
7379 try_match_word = try_match;
7380 if (!line_is_empty)
7381 try_match = FALSE;
7382 ++look;
7383 }
7384 else
7385 try_match_word = FALSE;
7386
7387 /*
7388 * does it look like a control character?
7389 */
7390 if (*look == '^'
7391#ifdef EBCDIC
7392 && (Ctrl_chr(look[1]) != 0)
7393#else
7394 && look[1] >= '?' && look[1] <= '_'
7395#endif
7396 )
7397 {
7398 if (try_match && keytyped == Ctrl_chr(look[1]))
7399 return TRUE;
7400 look += 2;
7401 }
7402 /*
7403 * 'o' means "o" command, open forward.
7404 * 'O' means "O" command, open backward.
7405 */
7406 else if (*look == 'o')
7407 {
7408 if (try_match && keytyped == KEY_OPEN_FORW)
7409 return TRUE;
7410 ++look;
7411 }
7412 else if (*look == 'O')
7413 {
7414 if (try_match && keytyped == KEY_OPEN_BACK)
7415 return TRUE;
7416 ++look;
7417 }
7418
7419 /*
7420 * 'e' means to check for "else" at start of line and just before the
7421 * cursor.
7422 */
7423 else if (*look == 'e')
7424 {
7425 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7426 {
7427 p = ml_get_curline();
7428 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7429 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7430 return TRUE;
7431 }
7432 ++look;
7433 }
7434
7435 /*
7436 * ':' only causes an indent if it is at the end of a label or case
7437 * statement, or when it was before typing the ':' (to fix
7438 * class::method for C++).
7439 */
7440 else if (*look == ':')
7441 {
7442 if (try_match && keytyped == ':')
7443 {
7444 p = ml_get_curline();
7445 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7446 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007447 /* Need to get the line again after cin_islabel(). */
7448 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (curwin->w_cursor.col > 2
7450 && p[curwin->w_cursor.col - 1] == ':'
7451 && p[curwin->w_cursor.col - 2] == ':')
7452 {
7453 p[curwin->w_cursor.col - 1] = ' ';
7454 i = (cin_iscase(p) || cin_isscopedecl(p)
7455 || cin_islabel(30));
7456 p = ml_get_curline();
7457 p[curwin->w_cursor.col - 1] = ':';
7458 if (i)
7459 return TRUE;
7460 }
7461 }
7462 ++look;
7463 }
7464
7465
7466 /*
7467 * Is it a key in <>, maybe?
7468 */
7469 else if (*look == '<')
7470 {
7471 if (try_match)
7472 {
7473 /*
7474 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7475 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7476 * >, *, : and ! keys if they really really want to.
7477 */
7478 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7479 && keytyped == look[1])
7480 return TRUE;
7481
7482 if (keytyped == get_special_key_code(look + 1))
7483 return TRUE;
7484 }
7485 while (*look && *look != '>')
7486 look++;
7487 while (*look == '>')
7488 look++;
7489 }
7490
7491 /*
7492 * Is it a word: "=word"?
7493 */
7494 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7495 {
7496 ++look;
7497 if (*look == '~')
7498 {
7499 icase = TRUE;
7500 ++look;
7501 }
7502 else
7503 icase = FALSE;
7504 p = vim_strchr(look, ',');
7505 if (p == NULL)
7506 p = look + STRLEN(look);
7507 if ((try_match || try_match_word)
7508 && curwin->w_cursor.col >= (colnr_T)(p - look))
7509 {
7510 int match = FALSE;
7511
7512#ifdef FEAT_INS_EXPAND
7513 if (keytyped == KEY_COMPLETE)
7514 {
7515 char_u *s;
7516
7517 /* Just completed a word, check if it starts with "look".
7518 * search back for the start of a word. */
7519 line = ml_get_curline();
7520# ifdef FEAT_MBYTE
7521 if (has_mbyte)
7522 {
7523 char_u *n;
7524
7525 for (s = line + curwin->w_cursor.col; s > line; s = n)
7526 {
7527 n = mb_prevptr(line, s);
7528 if (!vim_iswordp(n))
7529 break;
7530 }
7531 }
7532 else
7533# endif
7534 for (s = line + curwin->w_cursor.col; s > line; --s)
7535 if (!vim_iswordc(s[-1]))
7536 break;
7537 if (s + (p - look) <= line + curwin->w_cursor.col
7538 && (icase
7539 ? MB_STRNICMP(s, look, p - look)
7540 : STRNCMP(s, look, p - look)) == 0)
7541 match = TRUE;
7542 }
7543 else
7544#endif
7545 /* TODO: multi-byte */
7546 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7547 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7548 {
7549 line = ml_get_cursor();
7550 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7551 || !vim_iswordc(line[-(p - look) - 1]))
7552 && (icase
7553 ? MB_STRNICMP(line - (p - look), look, p - look)
7554 : STRNCMP(line - (p - look), look, p - look))
7555 == 0)
7556 match = TRUE;
7557 }
7558 if (match && try_match_word && !try_match)
7559 {
7560 /* "0=word": Check if there are only blanks before the
7561 * word. */
7562 line = ml_get_curline();
7563 if ((int)(skipwhite(line) - line) !=
7564 (int)(curwin->w_cursor.col - (p - look)))
7565 match = FALSE;
7566 }
7567 if (match)
7568 return TRUE;
7569 }
7570 look = p;
7571 }
7572
7573 /*
7574 * ok, it's a boring generic character.
7575 */
7576 else
7577 {
7578 if (try_match && *look == keytyped)
7579 return TRUE;
7580 ++look;
7581 }
7582
7583 /*
7584 * Skip over ", ".
7585 */
7586 look = skip_to_option_part(look);
7587 }
7588 return FALSE;
7589}
7590#endif /* FEAT_CINDENT */
7591
7592#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7593/*
7594 * Map Hebrew keyboard when in hkmap mode.
7595 */
7596 int
7597hkmap(c)
7598 int c;
7599{
7600 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7601 {
7602 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7603 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7604 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7605 static char_u map[26] =
7606 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7607 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7608 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7609 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7610 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7611 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7612 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7613 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7614 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7615
7616 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7617 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7618 /* '-1'='sofit' */
7619 else if (c == 'x')
7620 return 'X';
7621 else if (c == 'q')
7622 return '\''; /* {geresh}={'} */
7623 else if (c == 246)
7624 return ' '; /* \"o --> ' ' for a german keyboard */
7625 else if (c == 228)
7626 return ' '; /* \"a --> ' ' -- / -- */
7627 else if (c == 252)
7628 return ' '; /* \"u --> ' ' -- / -- */
7629#ifdef EBCDIC
7630 else if (islower(c))
7631#else
7632 /* NOTE: islower() does not do the right thing for us on Linux so we
7633 * do this the same was as 5.7 and previous, so it works correctly on
7634 * all systems. Specifically, the e.g. Delete and Arrow keys are
7635 * munged and won't work if e.g. searching for Hebrew text.
7636 */
7637 else if (c >= 'a' && c <= 'z')
7638#endif
7639 return (int)(map[CharOrdLow(c)] + p_aleph);
7640 else
7641 return c;
7642 }
7643 else
7644 {
7645 switch (c)
7646 {
7647 case '`': return ';';
7648 case '/': return '.';
7649 case '\'': return ',';
7650 case 'q': return '/';
7651 case 'w': return '\'';
7652
7653 /* Hebrew letters - set offset from 'a' */
7654 case ',': c = '{'; break;
7655 case '.': c = 'v'; break;
7656 case ';': c = 't'; break;
7657 default: {
7658 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7659
7660#ifdef EBCDIC
7661 /* see note about islower() above */
7662 if (!islower(c))
7663#else
7664 if (c < 'a' || c > 'z')
7665#endif
7666 return c;
7667 c = str[CharOrdLow(c)];
7668 break;
7669 }
7670 }
7671
7672 return (int)(CharOrdLow(c) + p_aleph);
7673 }
7674}
7675#endif
7676
7677 static void
7678ins_reg()
7679{
7680 int need_redraw = FALSE;
7681 int regname;
7682 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007683#ifdef FEAT_VISUAL
7684 int vis_active = VIsual_active;
7685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686
7687 /*
7688 * If we are going to wait for a character, show a '"'.
7689 */
7690 pc_status = PC_STATUS_UNSET;
7691 if (redrawing() && !char_avail())
7692 {
7693 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007694 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
7696 edit_putchar('"', TRUE);
7697#ifdef FEAT_CMDL_INFO
7698 add_to_showcmd_c(Ctrl_R);
7699#endif
7700 }
7701
7702#ifdef USE_ON_FLY_SCROLL
7703 dont_scroll = TRUE; /* disallow scrolling here */
7704#endif
7705
7706 /*
7707 * Don't map the register name. This also prevents the mode message to be
7708 * deleted when ESC is hit.
7709 */
7710 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007711 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7714 {
7715 /* Get a third key for literal register insertion */
7716 literally = regname;
7717#ifdef FEAT_CMDL_INFO
7718 add_to_showcmd_c(literally);
7719#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007720 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723 --no_mapping;
7724
7725#ifdef FEAT_EVAL
7726 /*
7727 * Don't call u_sync() while getting the expression,
7728 * evaluating it or giving an error message for it!
7729 */
7730 ++no_u_sync;
7731 if (regname == '=')
7732 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007733# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007737# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 /* Restore the Input Method. */
7739 if (im_on)
7740 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007743 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7744 {
7745 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 else
7749 {
7750#endif
7751 if (literally == Ctrl_O || literally == Ctrl_P)
7752 {
7753 /* Append the command to the redo buffer. */
7754 AppendCharToRedobuff(Ctrl_R);
7755 AppendCharToRedobuff(literally);
7756 AppendCharToRedobuff(regname);
7757
7758 do_put(regname, BACKWARD, 1L,
7759 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7760 }
7761 else if (insert_reg(regname, literally) == FAIL)
7762 {
7763 vim_beep();
7764 need_redraw = TRUE; /* remove the '"' */
7765 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007766 else if (stop_insert_mode)
7767 /* When the '=' register was used and a function was invoked that
7768 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7769 * insert anything, need to remove the '"' */
7770 need_redraw = TRUE;
7771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772#ifdef FEAT_EVAL
7773 }
7774 --no_u_sync;
7775#endif
7776#ifdef FEAT_CMDL_INFO
7777 clear_showcmd();
7778#endif
7779
7780 /* If the inserted register is empty, we need to remove the '"' */
7781 if (need_redraw || stuff_empty())
7782 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007783
7784#ifdef FEAT_VISUAL
7785 /* Disallow starting Visual mode here, would get a weird mode. */
7786 if (!vis_active && VIsual_active)
7787 end_visual_mode();
7788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790
7791/*
7792 * CTRL-G commands in Insert mode.
7793 */
7794 static void
7795ins_ctrl_g()
7796{
7797 int c;
7798
7799#ifdef FEAT_INS_EXPAND
7800 /* Right after CTRL-X the cursor will be after the ruler. */
7801 setcursor();
7802#endif
7803
7804 /*
7805 * Don't map the second key. This also prevents the mode message to be
7806 * deleted when ESC is hit.
7807 */
7808 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007809 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 --no_mapping;
7811 switch (c)
7812 {
7813 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7814 case K_UP:
7815 case Ctrl_K:
7816 case 'k': ins_up(TRUE);
7817 break;
7818
7819 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7820 case K_DOWN:
7821 case Ctrl_J:
7822 case 'j': ins_down(TRUE);
7823 break;
7824
7825 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007826 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007828
7829 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007830 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007831 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 break;
7833
7834 /* Unknown CTRL-G command, reserved for future expansion. */
7835 default: vim_beep();
7836 }
7837}
7838
7839/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007840 * CTRL-^ in Insert mode.
7841 */
7842 static void
7843ins_ctrl_hat()
7844{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007845 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007846 {
7847 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7848 if (State & LANGMAP)
7849 {
7850 curbuf->b_p_iminsert = B_IMODE_NONE;
7851 State &= ~LANGMAP;
7852 }
7853 else
7854 {
7855 curbuf->b_p_iminsert = B_IMODE_LMAP;
7856 State |= LANGMAP;
7857#ifdef USE_IM_CONTROL
7858 im_set_active(FALSE);
7859#endif
7860 }
7861 }
7862#ifdef USE_IM_CONTROL
7863 else
7864 {
7865 /* There are no ":lmap" mappings, toggle IM */
7866 if (im_get_status())
7867 {
7868 curbuf->b_p_iminsert = B_IMODE_NONE;
7869 im_set_active(FALSE);
7870 }
7871 else
7872 {
7873 curbuf->b_p_iminsert = B_IMODE_IM;
7874 State &= ~LANGMAP;
7875 im_set_active(TRUE);
7876 }
7877 }
7878#endif
7879 set_iminsert_global();
7880 showmode();
7881#ifdef FEAT_GUI
7882 /* may show different cursor shape or color */
7883 if (gui.in_use)
7884 gui_update_cursor(TRUE, FALSE);
7885#endif
7886#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7887 /* Show/unshow value of 'keymap' in status lines. */
7888 status_redraw_curbuf();
7889#endif
7890}
7891
7892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 * Handle ESC in insert mode.
7894 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7895 * insert.
7896 */
7897 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007898ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 long *count;
7900 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007901 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 int temp;
7904 static int disabled_redraw = FALSE;
7905
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007906#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007907 check_spell_redraw();
7908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#if defined(FEAT_HANGULIN)
7910# if defined(ESC_CHG_TO_ENG_MODE)
7911 hangul_input_state_set(0);
7912# endif
7913 if (composing_hangul)
7914 {
7915 push_raw_key(composing_hangul_buffer, 2);
7916 composing_hangul = 0;
7917 }
7918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
7920 temp = curwin->w_cursor.col;
7921 if (disabled_redraw)
7922 {
7923 --RedrawingDisabled;
7924 disabled_redraw = FALSE;
7925 }
7926 if (!arrow_used)
7927 {
7928 /*
7929 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007930 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7931 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 */
7933 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007934 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
7936 /*
7937 * Repeating insert may take a long time. Check for
7938 * interrupt now and then.
7939 */
7940 if (*count > 0)
7941 {
7942 line_breakcheck();
7943 if (got_int)
7944 *count = 0;
7945 }
7946
7947 if (--*count > 0) /* repeat what was typed */
7948 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007949 /* Vi repeats the insert without replacing characters. */
7950 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7951 State &= ~REPLACE_FLAG;
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 (void)start_redo_ins();
7954 if (cmdchar == 'r' || cmdchar == 'v')
7955 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7956 ++RedrawingDisabled;
7957 disabled_redraw = TRUE;
7958 return FALSE; /* repeat the insert */
7959 }
7960 stop_insert(&curwin->w_cursor, TRUE);
7961 undisplay_dollar();
7962 }
7963
7964 /* When an autoindent was removed, curswant stays after the
7965 * indent */
7966 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7967 curwin->w_set_curswant = TRUE;
7968
7969 /* Remember the last Insert position in the '^ mark. */
7970 if (!cmdmod.keepjumps)
7971 curbuf->b_last_insert = curwin->w_cursor;
7972
7973 /*
7974 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007975 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007977 if (!nomove
7978 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979#ifdef FEAT_VIRTUALEDIT
7980 || curwin->w_cursor.coladd > 0
7981#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007982 )
7983 && (restart_edit == NUL
7984 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007986 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007988 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989#ifdef FEAT_RIGHTLEFT
7990 && !revins_on
7991#endif
7992 )
7993 {
7994#ifdef FEAT_VIRTUALEDIT
7995 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7996 {
7997 oneleft();
7998 if (restart_edit != NUL)
7999 ++curwin->w_cursor.coladd;
8000 }
8001 else
8002#endif
8003 {
8004 --curwin->w_cursor.col;
8005#ifdef FEAT_MBYTE
8006 /* Correct cursor for multi-byte character. */
8007 if (has_mbyte)
8008 mb_adjust_cursor();
8009#endif
8010 }
8011 }
8012
8013#ifdef USE_IM_CONTROL
8014 /* Disable IM to allow typing English directly for Normal mode commands.
8015 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8016 * well). */
8017 if (!(State & LANGMAP))
8018 im_save_status(&curbuf->b_p_iminsert);
8019 im_set_active(FALSE);
8020#endif
8021
8022 State = NORMAL;
8023 /* need to position cursor again (e.g. when on a TAB ) */
8024 changed_cline_bef_curs();
8025
8026#ifdef FEAT_MOUSE
8027 setmouse();
8028#endif
8029#ifdef CURSOR_SHAPE
8030 ui_cursor_shape(); /* may show different cursor shape */
8031#endif
8032
8033 /*
8034 * When recording or for CTRL-O, need to display the new mode.
8035 * Otherwise remove the mode message.
8036 */
8037 if (Recording || restart_edit != NUL)
8038 showmode();
8039 else if (p_smd)
8040 MSG("");
8041
8042 return TRUE; /* exit Insert mode */
8043}
8044
8045#ifdef FEAT_RIGHTLEFT
8046/*
8047 * Toggle language: hkmap and revins_on.
8048 * Move to end of reverse inserted text.
8049 */
8050 static void
8051ins_ctrl_()
8052{
8053 if (revins_on && revins_chars && revins_scol >= 0)
8054 {
8055 while (gchar_cursor() != NUL && revins_chars--)
8056 ++curwin->w_cursor.col;
8057 }
8058 p_ri = !p_ri;
8059 revins_on = (State == INSERT && p_ri);
8060 if (revins_on)
8061 {
8062 revins_scol = curwin->w_cursor.col;
8063 revins_legal++;
8064 revins_chars = 0;
8065 undisplay_dollar();
8066 }
8067 else
8068 revins_scol = -1;
8069#ifdef FEAT_FKMAP
8070 if (p_altkeymap)
8071 {
8072 /*
8073 * to be consistent also for redo command, using '.'
8074 * set arrow_used to true and stop it - causing to redo
8075 * characters entered in one mode (normal/reverse insert).
8076 */
8077 arrow_used = TRUE;
8078 (void)stop_arrow();
8079 p_fkmap = curwin->w_p_rl ^ p_ri;
8080 if (p_fkmap && p_ri)
8081 State = INSERT;
8082 }
8083 else
8084#endif
8085 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8086 showmode();
8087}
8088#endif
8089
8090#ifdef FEAT_VISUAL
8091/*
8092 * If 'keymodel' contains "startsel", may start selection.
8093 * Returns TRUE when a CTRL-O and other keys stuffed.
8094 */
8095 static int
8096ins_start_select(c)
8097 int c;
8098{
8099 if (km_startsel)
8100 switch (c)
8101 {
8102 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 case K_PAGEUP:
8105 case K_KPAGEUP:
8106 case K_PAGEDOWN:
8107 case K_KPAGEDOWN:
8108# ifdef MACOS
8109 case K_LEFT:
8110 case K_RIGHT:
8111 case K_UP:
8112 case K_DOWN:
8113 case K_END:
8114 case K_HOME:
8115# endif
8116 if (!(mod_mask & MOD_MASK_SHIFT))
8117 break;
8118 /* FALLTHROUGH */
8119 case K_S_LEFT:
8120 case K_S_RIGHT:
8121 case K_S_UP:
8122 case K_S_DOWN:
8123 case K_S_END:
8124 case K_S_HOME:
8125 /* Start selection right away, the cursor can move with
8126 * CTRL-O when beyond the end of the line. */
8127 start_selection();
8128
8129 /* Execute the key in (insert) Select mode. */
8130 stuffcharReadbuff(Ctrl_O);
8131 if (mod_mask)
8132 {
8133 char_u buf[4];
8134
8135 buf[0] = K_SPECIAL;
8136 buf[1] = KS_MODIFIER;
8137 buf[2] = mod_mask;
8138 buf[3] = NUL;
8139 stuffReadbuff(buf);
8140 }
8141 stuffcharReadbuff(c);
8142 return TRUE;
8143 }
8144 return FALSE;
8145}
8146#endif
8147
8148/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008149 * <Insert> key in Insert mode: toggle insert/remplace mode.
8150 */
8151 static void
8152ins_insert(replaceState)
8153 int replaceState;
8154{
8155#ifdef FEAT_FKMAP
8156 if (p_fkmap && p_ri)
8157 {
8158 beep_flush();
8159 EMSG(farsi_text_3); /* encoded in Farsi */
8160 return;
8161 }
8162#endif
8163
8164#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008165# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008166 set_vim_var_string(VV_INSERTMODE,
8167 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008168# ifdef FEAT_VREPLACE
8169 replaceState == VREPLACE ? "v" :
8170# endif
8171 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008172# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008173 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8174#endif
8175 if (State & REPLACE_FLAG)
8176 State = INSERT | (State & LANGMAP);
8177 else
8178 State = replaceState | (State & LANGMAP);
8179 AppendCharToRedobuff(K_INS);
8180 showmode();
8181#ifdef CURSOR_SHAPE
8182 ui_cursor_shape(); /* may show different cursor shape */
8183#endif
8184}
8185
8186/*
8187 * Pressed CTRL-O in Insert mode.
8188 */
8189 static void
8190ins_ctrl_o()
8191{
8192#ifdef FEAT_VREPLACE
8193 if (State & VREPLACE_FLAG)
8194 restart_edit = 'V';
8195 else
8196#endif
8197 if (State & REPLACE_FLAG)
8198 restart_edit = 'R';
8199 else
8200 restart_edit = 'I';
8201#ifdef FEAT_VIRTUALEDIT
8202 if (virtual_active())
8203 ins_at_eol = FALSE; /* cursor always keeps its column */
8204 else
8205#endif
8206 ins_at_eol = (gchar_cursor() == NUL);
8207}
8208
8209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 * If the cursor is on an indent, ^T/^D insert/delete one
8211 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008212 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 * with vi. But vi only supports ^T and ^D after an
8214 * autoindent, we support it everywhere.
8215 */
8216 static void
8217ins_shift(c, lastc)
8218 int c;
8219 int lastc;
8220{
8221 if (stop_arrow() == FAIL)
8222 return;
8223 AppendCharToRedobuff(c);
8224
8225 /*
8226 * 0^D and ^^D: remove all indent.
8227 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008228 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8229 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {
8231 --curwin->w_cursor.col;
8232 (void)del_char(FALSE); /* delete the '^' or '0' */
8233 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8234 if (State & REPLACE_FLAG)
8235 replace_pop_ins();
8236 if (lastc == '^')
8237 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008238 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 }
8240 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008241 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
8243 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8244 did_ai = FALSE;
8245#ifdef FEAT_SMARTINDENT
8246 did_si = FALSE;
8247 can_si = FALSE;
8248 can_si_back = FALSE;
8249#endif
8250#ifdef FEAT_CINDENT
8251 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8252#endif
8253}
8254
8255 static void
8256ins_del()
8257{
8258 int temp;
8259
8260 if (stop_arrow() == FAIL)
8261 return;
8262 if (gchar_cursor() == NUL) /* delete newline */
8263 {
8264 temp = curwin->w_cursor.col;
8265 if (!can_bs(BS_EOL) /* only if "eol" included */
8266 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8267 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8268 || do_join(FALSE) == FAIL)
8269 vim_beep();
8270 else
8271 curwin->w_cursor.col = temp;
8272 }
8273 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8274 vim_beep();
8275 did_ai = FALSE;
8276#ifdef FEAT_SMARTINDENT
8277 did_si = FALSE;
8278 can_si = FALSE;
8279 can_si_back = FALSE;
8280#endif
8281 AppendCharToRedobuff(K_DEL);
8282}
8283
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008284static void ins_bs_one __ARGS((colnr_T *vcolp));
8285
8286/*
8287 * Delete one character for ins_bs().
8288 */
8289 static void
8290ins_bs_one(vcolp)
8291 colnr_T *vcolp;
8292{
8293 dec_cursor();
8294 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8295 if (State & REPLACE_FLAG)
8296 {
8297 /* Don't delete characters before the insert point when in
8298 * Replace mode */
8299 if (curwin->w_cursor.lnum != Insstart.lnum
8300 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008301 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008302 }
8303 else
8304 (void)del_char(FALSE);
8305}
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307/*
8308 * Handle Backspace, delete-word and delete-line in Insert mode.
8309 * Return TRUE when backspace was actually used.
8310 */
8311 static int
8312ins_bs(c, mode, inserted_space_p)
8313 int c;
8314 int mode;
8315 int *inserted_space_p;
8316{
8317 linenr_T lnum;
8318 int cc;
8319 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008320 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 colnr_T mincol;
8322 int did_backspace = FALSE;
8323 int in_indent;
8324 int oldState;
8325#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008326 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327#endif
8328
8329 /*
8330 * can't delete anything in an empty file
8331 * can't backup past first character in buffer
8332 * can't backup past starting point unless 'backspace' > 1
8333 * can backup to a previous line if 'backspace' == 0
8334 */
8335 if ( bufempty()
8336 || (
8337#ifdef FEAT_RIGHTLEFT
8338 !revins_on &&
8339#endif
8340 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8341 || (!can_bs(BS_START)
8342 && (arrow_used
8343 || (curwin->w_cursor.lnum == Insstart.lnum
8344 && curwin->w_cursor.col <= Insstart.col)))
8345 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8346 && curwin->w_cursor.col <= ai_col)
8347 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8348 {
8349 vim_beep();
8350 return FALSE;
8351 }
8352
8353 if (stop_arrow() == FAIL)
8354 return FALSE;
8355 in_indent = inindent(0);
8356#ifdef FEAT_CINDENT
8357 if (in_indent)
8358 can_cindent = FALSE;
8359#endif
8360#ifdef FEAT_COMMENTS
8361 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8362#endif
8363#ifdef FEAT_RIGHTLEFT
8364 if (revins_on) /* put cursor after last inserted char */
8365 inc_cursor();
8366#endif
8367
8368#ifdef FEAT_VIRTUALEDIT
8369 /* Virtualedit:
8370 * BACKSPACE_CHAR eats a virtual space
8371 * BACKSPACE_WORD eats all coladd
8372 * BACKSPACE_LINE eats all coladd and keeps going
8373 */
8374 if (curwin->w_cursor.coladd > 0)
8375 {
8376 if (mode == BACKSPACE_CHAR)
8377 {
8378 --curwin->w_cursor.coladd;
8379 return TRUE;
8380 }
8381 if (mode == BACKSPACE_WORD)
8382 {
8383 curwin->w_cursor.coladd = 0;
8384 return TRUE;
8385 }
8386 curwin->w_cursor.coladd = 0;
8387 }
8388#endif
8389
8390 /*
8391 * delete newline!
8392 */
8393 if (curwin->w_cursor.col == 0)
8394 {
8395 lnum = Insstart.lnum;
8396 if (curwin->w_cursor.lnum == Insstart.lnum
8397#ifdef FEAT_RIGHTLEFT
8398 || revins_on
8399#endif
8400 )
8401 {
8402 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8403 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8404 return FALSE;
8405 --Insstart.lnum;
8406 Insstart.col = MAXCOL;
8407 }
8408 /*
8409 * In replace mode:
8410 * cc < 0: NL was inserted, delete it
8411 * cc >= 0: NL was replaced, put original characters back
8412 */
8413 cc = -1;
8414 if (State & REPLACE_FLAG)
8415 cc = replace_pop(); /* returns -1 if NL was inserted */
8416 /*
8417 * In replace mode, in the line we started replacing, we only move the
8418 * cursor.
8419 */
8420 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8421 {
8422 dec_cursor();
8423 }
8424 else
8425 {
8426#ifdef FEAT_VREPLACE
8427 if (!(State & VREPLACE_FLAG)
8428 || curwin->w_cursor.lnum > orig_line_count)
8429#endif
8430 {
8431 temp = gchar_cursor(); /* remember current char */
8432 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008433
8434 /* When "aw" is in 'formatoptions' we must delete the space at
8435 * the end of the line, otherwise the line will be broken
8436 * again when auto-formatting. */
8437 if (has_format_option(FO_AUTO)
8438 && has_format_option(FO_WHITE_PAR))
8439 {
8440 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8441 TRUE);
8442 int len;
8443
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008444 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008445 if (len > 0 && ptr[len - 1] == ' ')
8446 ptr[len - 1] = NUL;
8447 }
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 (void)do_join(FALSE);
8450 if (temp == NUL && gchar_cursor() != NUL)
8451 inc_cursor();
8452 }
8453#ifdef FEAT_VREPLACE
8454 else
8455 dec_cursor();
8456#endif
8457
8458 /*
8459 * In REPLACE mode we have to put back the text that was replaced
8460 * by the NL. On the replace stack is first a NUL-terminated
8461 * sequence of characters that were deleted and then the
8462 * characters that NL replaced.
8463 */
8464 if (State & REPLACE_FLAG)
8465 {
8466 /*
8467 * Do the next ins_char() in NORMAL state, to
8468 * prevent ins_char() from replacing characters and
8469 * avoiding showmatch().
8470 */
8471 oldState = State;
8472 State = NORMAL;
8473 /*
8474 * restore characters (blanks) deleted after cursor
8475 */
8476 while (cc > 0)
8477 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008478 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479#ifdef FEAT_MBYTE
8480 mb_replace_pop_ins(cc);
8481#else
8482 ins_char(cc);
8483#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008484 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 cc = replace_pop();
8486 }
8487 /* restore the characters that NL replaced */
8488 replace_pop_ins();
8489 State = oldState;
8490 }
8491 }
8492 did_ai = FALSE;
8493 }
8494 else
8495 {
8496 /*
8497 * Delete character(s) before the cursor.
8498 */
8499#ifdef FEAT_RIGHTLEFT
8500 if (revins_on) /* put cursor on last inserted char */
8501 dec_cursor();
8502#endif
8503 mincol = 0;
8504 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008505 if (mode == BACKSPACE_LINE
8506 && (curbuf->b_p_ai
8507#ifdef FEAT_CINDENT
8508 || cindent_on()
8509#endif
8510 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511#ifdef FEAT_RIGHTLEFT
8512 && !revins_on
8513#endif
8514 )
8515 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008516 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 beginline(BL_WHITE);
8518 if (curwin->w_cursor.col < (colnr_T)temp)
8519 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008520 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 }
8522
8523 /*
8524 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8525 */
8526 if ( mode == BACKSPACE_CHAR
8527 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008528 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008529 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 && (*(ml_get_cursor() - 1) == TAB
8531 || (*(ml_get_cursor() - 1) == ' '
8532 && (!*inserted_space_p
8533 || arrow_used))))))
8534 {
8535 int ts;
8536 colnr_T vcol;
8537 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008538 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
8540 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008541 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 ts = curbuf->b_p_sw;
8543 else
8544 ts = curbuf->b_p_sts;
8545 /* Compute the virtual column where we want to be. Since
8546 * 'showbreak' may get in the way, need to get the last column of
8547 * the previous character. */
8548 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008549 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 dec_cursor();
8551 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8552 inc_cursor();
8553 want_vcol = (want_vcol / ts) * ts;
8554
8555 /* delete characters until we are at or before want_vcol */
8556 while (vcol > want_vcol
8557 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008558 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559
8560 /* insert extra spaces until we are at want_vcol */
8561 while (vcol < want_vcol)
8562 {
8563 /* Remember the first char we inserted */
8564 if (curwin->w_cursor.lnum == Insstart.lnum
8565 && curwin->w_cursor.col < Insstart.col)
8566 Insstart.col = curwin->w_cursor.col;
8567
8568#ifdef FEAT_VREPLACE
8569 if (State & VREPLACE_FLAG)
8570 ins_char(' ');
8571 else
8572#endif
8573 {
8574 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008575 if ((State & REPLACE_FLAG))
8576 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 }
8578 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8579 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008580
8581 /* If we are now back where we started delete one character. Can
8582 * happen when using 'sts' and 'linebreak'. */
8583 if (vcol >= start_vcol)
8584 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 }
8586
8587 /*
8588 * Delete upto starting point, start of line or previous word.
8589 */
8590 else do
8591 {
8592#ifdef FEAT_RIGHTLEFT
8593 if (!revins_on) /* put cursor on char to be deleted */
8594#endif
8595 dec_cursor();
8596
8597 /* start of word? */
8598 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8599 {
8600 mode = BACKSPACE_WORD_NOT_SPACE;
8601 temp = vim_iswordc(gchar_cursor());
8602 }
8603 /* end of word? */
8604 else if (mode == BACKSPACE_WORD_NOT_SPACE
8605 && (vim_isspace(cc = gchar_cursor())
8606 || vim_iswordc(cc) != temp))
8607 {
8608#ifdef FEAT_RIGHTLEFT
8609 if (!revins_on)
8610#endif
8611 inc_cursor();
8612#ifdef FEAT_RIGHTLEFT
8613 else if (State & REPLACE_FLAG)
8614 dec_cursor();
8615#endif
8616 break;
8617 }
8618 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008619 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 else
8621 {
8622#ifdef FEAT_MBYTE
8623 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008624 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625#endif
8626 (void)del_char(FALSE);
8627#ifdef FEAT_MBYTE
8628 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008629 * If there are combining characters and 'delcombine' is set
8630 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 * character.
8632 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008633 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 inc_cursor();
8635#endif
8636#ifdef FEAT_RIGHTLEFT
8637 if (revins_chars)
8638 {
8639 revins_chars--;
8640 revins_legal++;
8641 }
8642 if (revins_on && gchar_cursor() == NUL)
8643 break;
8644#endif
8645 }
8646 /* Just a single backspace?: */
8647 if (mode == BACKSPACE_CHAR)
8648 break;
8649 } while (
8650#ifdef FEAT_RIGHTLEFT
8651 revins_on ||
8652#endif
8653 (curwin->w_cursor.col > mincol
8654 && (curwin->w_cursor.lnum != Insstart.lnum
8655 || curwin->w_cursor.col != Insstart.col)));
8656 did_backspace = TRUE;
8657 }
8658#ifdef FEAT_SMARTINDENT
8659 did_si = FALSE;
8660 can_si = FALSE;
8661 can_si_back = FALSE;
8662#endif
8663 if (curwin->w_cursor.col <= 1)
8664 did_ai = FALSE;
8665 /*
8666 * It's a little strange to put backspaces into the redo
8667 * buffer, but it makes auto-indent a lot easier to deal
8668 * with.
8669 */
8670 AppendCharToRedobuff(c);
8671
8672 /* If deleted before the insertion point, adjust it */
8673 if (curwin->w_cursor.lnum == Insstart.lnum
8674 && curwin->w_cursor.col < Insstart.col)
8675 Insstart.col = curwin->w_cursor.col;
8676
8677 /* vi behaviour: the cursor moves backward but the character that
8678 * was there remains visible
8679 * Vim behaviour: the cursor moves backward and the character that
8680 * was there is erased from the screen.
8681 * We can emulate the vi behaviour by pretending there is a dollar
8682 * displayed even when there isn't.
8683 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8684 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8685 dollar_vcol = curwin->w_virtcol;
8686
Bram Moolenaarce3be472008-01-14 19:12:28 +00008687#ifdef FEAT_FOLDING
8688 /* When deleting a char the cursor line must never be in a closed fold.
8689 * E.g., when 'foldmethod' is indent and deleting the first non-white
8690 * char before a Tab. */
8691 if (did_backspace)
8692 foldOpenCursor();
8693#endif
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 return did_backspace;
8696}
8697
8698#ifdef FEAT_MOUSE
8699 static void
8700ins_mouse(c)
8701 int c;
8702{
8703 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008704 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
8706# ifdef FEAT_GUI
8707 /* When GUI is active, also move/paste when 'mouse' is empty */
8708 if (!gui.in_use)
8709# endif
8710 if (!mouse_has(MOUSE_INSERT))
8711 return;
8712
8713 undisplay_dollar();
8714 tpos = curwin->w_cursor;
8715 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8716 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008717#ifdef FEAT_WINDOWS
8718 win_T *new_curwin = curwin;
8719
8720 if (curwin != old_curwin && win_valid(old_curwin))
8721 {
8722 /* Mouse took us to another window. We need to go back to the
8723 * previous one to stop insert there properly. */
8724 curwin = old_curwin;
8725 curbuf = curwin->w_buffer;
8726 }
8727#endif
8728 start_arrow(curwin == old_curwin ? &tpos : NULL);
8729#ifdef FEAT_WINDOWS
8730 if (curwin != new_curwin && win_valid(new_curwin))
8731 {
8732 curwin = new_curwin;
8733 curbuf = curwin->w_buffer;
8734 }
8735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736# ifdef FEAT_CINDENT
8737 can_cindent = TRUE;
8738# endif
8739 }
8740
8741#ifdef FEAT_WINDOWS
8742 /* redraw status lines (in case another window became active) */
8743 redraw_statuslines();
8744#endif
8745}
8746
8747 static void
8748ins_mousescroll(up)
8749 int up;
8750{
8751 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008752# if defined(FEAT_WINDOWS)
8753 win_T *old_curwin = curwin;
8754# endif
8755# ifdef FEAT_INS_EXPAND
8756 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757# endif
8758
8759 tpos = curwin->w_cursor;
8760
8761# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 /* Currently the mouse coordinates are only known in the GUI. */
8763 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8764 {
8765 int row, col;
8766
8767 row = mouse_row;
8768 col = mouse_col;
8769
8770 /* find the window at the pointer coordinates */
8771 curwin = mouse_find_win(&row, &col);
8772 curbuf = curwin->w_buffer;
8773 }
8774 if (curwin == old_curwin)
8775# endif
8776 undisplay_dollar();
8777
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008778# ifdef FEAT_INS_EXPAND
8779 /* Don't scroll the window in which completion is being done. */
8780 if (!pum_visible()
8781# if defined(FEAT_WINDOWS)
8782 || curwin != old_curwin
8783# endif
8784 )
8785# endif
8786 {
8787 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8788 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8789 else
8790 scroll_redraw(up, 3L);
8791# ifdef FEAT_INS_EXPAND
8792 did_scroll = TRUE;
8793# endif
8794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
8796# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8797 curwin->w_redr_status = TRUE;
8798
8799 curwin = old_curwin;
8800 curbuf = curwin->w_buffer;
8801# endif
8802
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008803# ifdef FEAT_INS_EXPAND
8804 /* The popup menu may overlay the window, need to redraw it.
8805 * TODO: Would be more efficient to only redraw the windows that are
8806 * overlapped by the popup menu. */
8807 if (pum_visible() && did_scroll)
8808 {
8809 redraw_all_later(NOT_VALID);
8810 ins_compl_show_pum();
8811 }
8812# endif
8813
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (!equalpos(curwin->w_cursor, tpos))
8815 {
8816 start_arrow(&tpos);
8817# ifdef FEAT_CINDENT
8818 can_cindent = TRUE;
8819# endif
8820 }
8821}
8822#endif
8823
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008824#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008825 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008826ins_tabline(c)
8827 int c;
8828{
8829 /* We will be leaving the current window, unless closing another tab. */
8830 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8831 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8832 {
8833 undisplay_dollar();
8834 start_arrow(&curwin->w_cursor);
8835# ifdef FEAT_CINDENT
8836 can_cindent = TRUE;
8837# endif
8838 }
8839
8840 if (c == K_TABLINE)
8841 goto_tabpage(current_tab);
8842 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008843 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008844 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008845 redraw_statuslines(); /* will redraw the tabline when needed */
8846 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008847}
8848#endif
8849
8850#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 void
8852ins_scroll()
8853{
8854 pos_T tpos;
8855
8856 undisplay_dollar();
8857 tpos = curwin->w_cursor;
8858 if (gui_do_scroll())
8859 {
8860 start_arrow(&tpos);
8861# ifdef FEAT_CINDENT
8862 can_cindent = TRUE;
8863# endif
8864 }
8865}
8866
8867 void
8868ins_horscroll()
8869{
8870 pos_T tpos;
8871
8872 undisplay_dollar();
8873 tpos = curwin->w_cursor;
8874 if (gui_do_horiz_scroll())
8875 {
8876 start_arrow(&tpos);
8877# ifdef FEAT_CINDENT
8878 can_cindent = TRUE;
8879# endif
8880 }
8881}
8882#endif
8883
8884 static void
8885ins_left()
8886{
8887 pos_T tpos;
8888
8889#ifdef FEAT_FOLDING
8890 if ((fdo_flags & FDO_HOR) && KeyTyped)
8891 foldOpenCursor();
8892#endif
8893 undisplay_dollar();
8894 tpos = curwin->w_cursor;
8895 if (oneleft() == OK)
8896 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008897#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8898 /* Only call start_arrow() when not busy with preediting, it will
8899 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8900 if (!im_is_preediting())
8901#endif
8902 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903#ifdef FEAT_RIGHTLEFT
8904 /* If exit reversed string, position is fixed */
8905 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8906 revins_legal++;
8907 revins_chars++;
8908#endif
8909 }
8910
8911 /*
8912 * if 'whichwrap' set for cursor in insert mode may go to
8913 * previous line
8914 */
8915 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8916 {
8917 start_arrow(&tpos);
8918 --(curwin->w_cursor.lnum);
8919 coladvance((colnr_T)MAXCOL);
8920 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8921 }
8922 else
8923 vim_beep();
8924}
8925
8926 static void
8927ins_home(c)
8928 int c;
8929{
8930 pos_T tpos;
8931
8932#ifdef FEAT_FOLDING
8933 if ((fdo_flags & FDO_HOR) && KeyTyped)
8934 foldOpenCursor();
8935#endif
8936 undisplay_dollar();
8937 tpos = curwin->w_cursor;
8938 if (c == K_C_HOME)
8939 curwin->w_cursor.lnum = 1;
8940 curwin->w_cursor.col = 0;
8941#ifdef FEAT_VIRTUALEDIT
8942 curwin->w_cursor.coladd = 0;
8943#endif
8944 curwin->w_curswant = 0;
8945 start_arrow(&tpos);
8946}
8947
8948 static void
8949ins_end(c)
8950 int c;
8951{
8952 pos_T tpos;
8953
8954#ifdef FEAT_FOLDING
8955 if ((fdo_flags & FDO_HOR) && KeyTyped)
8956 foldOpenCursor();
8957#endif
8958 undisplay_dollar();
8959 tpos = curwin->w_cursor;
8960 if (c == K_C_END)
8961 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8962 coladvance((colnr_T)MAXCOL);
8963 curwin->w_curswant = MAXCOL;
8964
8965 start_arrow(&tpos);
8966}
8967
8968 static void
8969ins_s_left()
8970{
8971#ifdef FEAT_FOLDING
8972 if ((fdo_flags & FDO_HOR) && KeyTyped)
8973 foldOpenCursor();
8974#endif
8975 undisplay_dollar();
8976 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8977 {
8978 start_arrow(&curwin->w_cursor);
8979 (void)bck_word(1L, FALSE, FALSE);
8980 curwin->w_set_curswant = TRUE;
8981 }
8982 else
8983 vim_beep();
8984}
8985
8986 static void
8987ins_right()
8988{
8989#ifdef FEAT_FOLDING
8990 if ((fdo_flags & FDO_HOR) && KeyTyped)
8991 foldOpenCursor();
8992#endif
8993 undisplay_dollar();
8994 if (gchar_cursor() != NUL || virtual_active()
8995 )
8996 {
8997 start_arrow(&curwin->w_cursor);
8998 curwin->w_set_curswant = TRUE;
8999#ifdef FEAT_VIRTUALEDIT
9000 if (virtual_active())
9001 oneright();
9002 else
9003#endif
9004 {
9005#ifdef FEAT_MBYTE
9006 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009007 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 else
9009#endif
9010 ++curwin->w_cursor.col;
9011 }
9012
9013#ifdef FEAT_RIGHTLEFT
9014 revins_legal++;
9015 if (revins_chars)
9016 revins_chars--;
9017#endif
9018 }
9019 /* if 'whichwrap' set for cursor in insert mode, may move the
9020 * cursor to the next line */
9021 else if (vim_strchr(p_ww, ']') != NULL
9022 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9023 {
9024 start_arrow(&curwin->w_cursor);
9025 curwin->w_set_curswant = TRUE;
9026 ++curwin->w_cursor.lnum;
9027 curwin->w_cursor.col = 0;
9028 }
9029 else
9030 vim_beep();
9031}
9032
9033 static void
9034ins_s_right()
9035{
9036#ifdef FEAT_FOLDING
9037 if ((fdo_flags & FDO_HOR) && KeyTyped)
9038 foldOpenCursor();
9039#endif
9040 undisplay_dollar();
9041 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9042 || gchar_cursor() != NUL)
9043 {
9044 start_arrow(&curwin->w_cursor);
9045 (void)fwd_word(1L, FALSE, 0);
9046 curwin->w_set_curswant = TRUE;
9047 }
9048 else
9049 vim_beep();
9050}
9051
9052 static void
9053ins_up(startcol)
9054 int startcol; /* when TRUE move to Insstart.col */
9055{
9056 pos_T tpos;
9057 linenr_T old_topline = curwin->w_topline;
9058#ifdef FEAT_DIFF
9059 int old_topfill = curwin->w_topfill;
9060#endif
9061
9062 undisplay_dollar();
9063 tpos = curwin->w_cursor;
9064 if (cursor_up(1L, TRUE) == OK)
9065 {
9066 if (startcol)
9067 coladvance(getvcol_nolist(&Insstart));
9068 if (old_topline != curwin->w_topline
9069#ifdef FEAT_DIFF
9070 || old_topfill != curwin->w_topfill
9071#endif
9072 )
9073 redraw_later(VALID);
9074 start_arrow(&tpos);
9075#ifdef FEAT_CINDENT
9076 can_cindent = TRUE;
9077#endif
9078 }
9079 else
9080 vim_beep();
9081}
9082
9083 static void
9084ins_pageup()
9085{
9086 pos_T tpos;
9087
9088 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009089
9090#ifdef FEAT_WINDOWS
9091 if (mod_mask & MOD_MASK_CTRL)
9092 {
9093 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009094 if (first_tabpage->tp_next != NULL)
9095 {
9096 start_arrow(&curwin->w_cursor);
9097 goto_tabpage(-1);
9098 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009099 return;
9100 }
9101#endif
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 tpos = curwin->w_cursor;
9104 if (onepage(BACKWARD, 1L) == OK)
9105 {
9106 start_arrow(&tpos);
9107#ifdef FEAT_CINDENT
9108 can_cindent = TRUE;
9109#endif
9110 }
9111 else
9112 vim_beep();
9113}
9114
9115 static void
9116ins_down(startcol)
9117 int startcol; /* when TRUE move to Insstart.col */
9118{
9119 pos_T tpos;
9120 linenr_T old_topline = curwin->w_topline;
9121#ifdef FEAT_DIFF
9122 int old_topfill = curwin->w_topfill;
9123#endif
9124
9125 undisplay_dollar();
9126 tpos = curwin->w_cursor;
9127 if (cursor_down(1L, TRUE) == OK)
9128 {
9129 if (startcol)
9130 coladvance(getvcol_nolist(&Insstart));
9131 if (old_topline != curwin->w_topline
9132#ifdef FEAT_DIFF
9133 || old_topfill != curwin->w_topfill
9134#endif
9135 )
9136 redraw_later(VALID);
9137 start_arrow(&tpos);
9138#ifdef FEAT_CINDENT
9139 can_cindent = TRUE;
9140#endif
9141 }
9142 else
9143 vim_beep();
9144}
9145
9146 static void
9147ins_pagedown()
9148{
9149 pos_T tpos;
9150
9151 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009152
9153#ifdef FEAT_WINDOWS
9154 if (mod_mask & MOD_MASK_CTRL)
9155 {
9156 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009157 if (first_tabpage->tp_next != NULL)
9158 {
9159 start_arrow(&curwin->w_cursor);
9160 goto_tabpage(0);
9161 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009162 return;
9163 }
9164#endif
9165
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 tpos = curwin->w_cursor;
9167 if (onepage(FORWARD, 1L) == OK)
9168 {
9169 start_arrow(&tpos);
9170#ifdef FEAT_CINDENT
9171 can_cindent = TRUE;
9172#endif
9173 }
9174 else
9175 vim_beep();
9176}
9177
9178#ifdef FEAT_DND
9179 static void
9180ins_drop()
9181{
9182 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9183}
9184#endif
9185
9186/*
9187 * Handle TAB in Insert or Replace mode.
9188 * Return TRUE when the TAB needs to be inserted like a normal character.
9189 */
9190 static int
9191ins_tab()
9192{
9193 int ind;
9194 int i;
9195 int temp;
9196
9197 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9198 Insstart_blank_vcol = get_nolist_virtcol();
9199 if (echeck_abbr(TAB + ABBR_OFF))
9200 return FALSE;
9201
9202 ind = inindent(0);
9203#ifdef FEAT_CINDENT
9204 if (ind)
9205 can_cindent = FALSE;
9206#endif
9207
9208 /*
9209 * When nothing special, insert TAB like a normal character
9210 */
9211 if (!curbuf->b_p_et
9212 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9213 && curbuf->b_p_sts == 0)
9214 return TRUE;
9215
9216 if (stop_arrow() == FAIL)
9217 return TRUE;
9218
9219 did_ai = FALSE;
9220#ifdef FEAT_SMARTINDENT
9221 did_si = FALSE;
9222 can_si = FALSE;
9223 can_si_back = FALSE;
9224#endif
9225 AppendToRedobuff((char_u *)"\t");
9226
9227 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9228 temp = (int)curbuf->b_p_sw;
9229 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9230 temp = (int)curbuf->b_p_sts;
9231 else /* otherwise use 'tabstop' */
9232 temp = (int)curbuf->b_p_ts;
9233 temp -= get_nolist_virtcol() % temp;
9234
9235 /*
9236 * Insert the first space with ins_char(). It will delete one char in
9237 * replace mode. Insert the rest with ins_str(); it will not delete any
9238 * chars. For VREPLACE mode, we use ins_char() for all characters.
9239 */
9240 ins_char(' ');
9241 while (--temp > 0)
9242 {
9243#ifdef FEAT_VREPLACE
9244 if (State & VREPLACE_FLAG)
9245 ins_char(' ');
9246 else
9247#endif
9248 {
9249 ins_str((char_u *)" ");
9250 if (State & REPLACE_FLAG) /* no char replaced */
9251 replace_push(NUL);
9252 }
9253 }
9254
9255 /*
9256 * When 'expandtab' not set: Replace spaces by TABs where possible.
9257 */
9258 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9259 {
9260 char_u *ptr;
9261#ifdef FEAT_VREPLACE
9262 char_u *saved_line = NULL; /* init for GCC */
9263 pos_T pos;
9264#endif
9265 pos_T fpos;
9266 pos_T *cursor;
9267 colnr_T want_vcol, vcol;
9268 int change_col = -1;
9269 int save_list = curwin->w_p_list;
9270
9271 /*
9272 * Get the current line. For VREPLACE mode, don't make real changes
9273 * yet, just work on a copy of the line.
9274 */
9275#ifdef FEAT_VREPLACE
9276 if (State & VREPLACE_FLAG)
9277 {
9278 pos = curwin->w_cursor;
9279 cursor = &pos;
9280 saved_line = vim_strsave(ml_get_curline());
9281 if (saved_line == NULL)
9282 return FALSE;
9283 ptr = saved_line + pos.col;
9284 }
9285 else
9286#endif
9287 {
9288 ptr = ml_get_cursor();
9289 cursor = &curwin->w_cursor;
9290 }
9291
9292 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9293 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9294 curwin->w_p_list = FALSE;
9295
9296 /* Find first white before the cursor */
9297 fpos = curwin->w_cursor;
9298 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9299 {
9300 --fpos.col;
9301 --ptr;
9302 }
9303
9304 /* In Replace mode, don't change characters before the insert point. */
9305 if ((State & REPLACE_FLAG)
9306 && fpos.lnum == Insstart.lnum
9307 && fpos.col < Insstart.col)
9308 {
9309 ptr += Insstart.col - fpos.col;
9310 fpos.col = Insstart.col;
9311 }
9312
9313 /* compute virtual column numbers of first white and cursor */
9314 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9315 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9316
9317 /* Use as many TABs as possible. Beware of 'showbreak' and
9318 * 'linebreak' adding extra virtual columns. */
9319 while (vim_iswhite(*ptr))
9320 {
9321 i = lbr_chartabsize((char_u *)"\t", vcol);
9322 if (vcol + i > want_vcol)
9323 break;
9324 if (*ptr != TAB)
9325 {
9326 *ptr = TAB;
9327 if (change_col < 0)
9328 {
9329 change_col = fpos.col; /* Column of first change */
9330 /* May have to adjust Insstart */
9331 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9332 Insstart.col = fpos.col;
9333 }
9334 }
9335 ++fpos.col;
9336 ++ptr;
9337 vcol += i;
9338 }
9339
9340 if (change_col >= 0)
9341 {
9342 int repl_off = 0;
9343
9344 /* Skip over the spaces we need. */
9345 while (vcol < want_vcol && *ptr == ' ')
9346 {
9347 vcol += lbr_chartabsize(ptr, vcol);
9348 ++ptr;
9349 ++repl_off;
9350 }
9351 if (vcol > want_vcol)
9352 {
9353 /* Must have a char with 'showbreak' just before it. */
9354 --ptr;
9355 --repl_off;
9356 }
9357 fpos.col += repl_off;
9358
9359 /* Delete following spaces. */
9360 i = cursor->col - fpos.col;
9361 if (i > 0)
9362 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009363 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 /* correct replace stack. */
9365 if ((State & REPLACE_FLAG)
9366#ifdef FEAT_VREPLACE
9367 && !(State & VREPLACE_FLAG)
9368#endif
9369 )
9370 for (temp = i; --temp >= 0; )
9371 replace_join(repl_off);
9372 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009373#ifdef FEAT_NETBEANS_INTG
9374 if (usingNetbeans)
9375 {
9376 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9377 (long)(i + 1));
9378 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9379 (char_u *)"\t", 1);
9380 }
9381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 cursor->col -= i;
9383
9384#ifdef FEAT_VREPLACE
9385 /*
9386 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9387 * backspacing over the changed spacing and then inserting the new
9388 * spacing.
9389 */
9390 if (State & VREPLACE_FLAG)
9391 {
9392 /* Backspace from real cursor to change_col */
9393 backspace_until_column(change_col);
9394
9395 /* Insert each char in saved_line from changed_col to
9396 * ptr-cursor */
9397 ins_bytes_len(saved_line + change_col,
9398 cursor->col - change_col);
9399 }
9400#endif
9401 }
9402
9403#ifdef FEAT_VREPLACE
9404 if (State & VREPLACE_FLAG)
9405 vim_free(saved_line);
9406#endif
9407 curwin->w_p_list = save_list;
9408 }
9409
9410 return FALSE;
9411}
9412
9413/*
9414 * Handle CR or NL in insert mode.
9415 * Return TRUE when out of memory or can't undo.
9416 */
9417 static int
9418ins_eol(c)
9419 int c;
9420{
9421 int i;
9422
9423 if (echeck_abbr(c + ABBR_OFF))
9424 return FALSE;
9425 if (stop_arrow() == FAIL)
9426 return TRUE;
9427 undisplay_dollar();
9428
9429 /*
9430 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9431 * character under the cursor. Only push a NUL on the replace stack,
9432 * nothing to put back when the NL is deleted.
9433 */
9434 if ((State & REPLACE_FLAG)
9435#ifdef FEAT_VREPLACE
9436 && !(State & VREPLACE_FLAG)
9437#endif
9438 )
9439 replace_push(NUL);
9440
9441 /*
9442 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9443 * replacing the next line, so we push all of the characters left on the
9444 * line onto the replace stack. This is not done here though, it is done
9445 * in open_line().
9446 */
9447
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009448#ifdef FEAT_VIRTUALEDIT
9449 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9450 * CTRL-O). */
9451 if (virtual_active() && curwin->w_cursor.coladd > 0)
9452 coladvance(getviscol());
9453#endif
9454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#ifdef FEAT_RIGHTLEFT
9456# ifdef FEAT_FKMAP
9457 if (p_altkeymap && p_fkmap)
9458 fkmap(NL);
9459# endif
9460 /* NL in reverse insert will always start in the end of
9461 * current line. */
9462 if (revins_on)
9463 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9464#endif
9465
9466 AppendToRedobuff(NL_STR);
9467 i = open_line(FORWARD,
9468#ifdef FEAT_COMMENTS
9469 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9470#endif
9471 0, old_indent);
9472 old_indent = 0;
9473#ifdef FEAT_CINDENT
9474 can_cindent = TRUE;
9475#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009476#ifdef FEAT_FOLDING
9477 /* When inserting a line the cursor line must never be in a closed fold. */
9478 foldOpenCursor();
9479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480
9481 return (!i);
9482}
9483
9484#ifdef FEAT_DIGRAPHS
9485/*
9486 * Handle digraph in insert mode.
9487 * Returns character still to be inserted, or NUL when nothing remaining to be
9488 * done.
9489 */
9490 static int
9491ins_digraph()
9492{
9493 int c;
9494 int cc;
9495
9496 pc_status = PC_STATUS_UNSET;
9497 if (redrawing() && !char_avail())
9498 {
9499 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009500 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 edit_putchar('?', TRUE);
9503#ifdef FEAT_CMDL_INFO
9504 add_to_showcmd_c(Ctrl_K);
9505#endif
9506 }
9507
9508#ifdef USE_ON_FLY_SCROLL
9509 dont_scroll = TRUE; /* disallow scrolling here */
9510#endif
9511
9512 /* don't map the digraph chars. This also prevents the
9513 * mode message to be deleted when ESC is hit */
9514 ++no_mapping;
9515 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009516 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 --no_mapping;
9518 --allow_keys;
9519 if (IS_SPECIAL(c) || mod_mask) /* special key */
9520 {
9521#ifdef FEAT_CMDL_INFO
9522 clear_showcmd();
9523#endif
9524 insert_special(c, TRUE, FALSE);
9525 return NUL;
9526 }
9527 if (c != ESC)
9528 {
9529 if (redrawing() && !char_avail())
9530 {
9531 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009532 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
9534 if (char2cells(c) == 1)
9535 {
9536 /* first remove the '?', otherwise it's restored when typing
9537 * an ESC next */
9538 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009539 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 edit_putchar(c, TRUE);
9541 }
9542#ifdef FEAT_CMDL_INFO
9543 add_to_showcmd_c(c);
9544#endif
9545 }
9546 ++no_mapping;
9547 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009548 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 --no_mapping;
9550 --allow_keys;
9551 if (cc != ESC)
9552 {
9553 AppendToRedobuff((char_u *)CTRL_V_STR);
9554 c = getdigraph(c, cc, TRUE);
9555#ifdef FEAT_CMDL_INFO
9556 clear_showcmd();
9557#endif
9558 return c;
9559 }
9560 }
9561 edit_unputchar();
9562#ifdef FEAT_CMDL_INFO
9563 clear_showcmd();
9564#endif
9565 return NUL;
9566}
9567#endif
9568
9569/*
9570 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9571 * Returns the char to be inserted, or NUL if none found.
9572 */
9573 static int
9574ins_copychar(lnum)
9575 linenr_T lnum;
9576{
9577 int c;
9578 int temp;
9579 char_u *ptr, *prev_ptr;
9580
9581 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9582 {
9583 vim_beep();
9584 return NUL;
9585 }
9586
9587 /* try to advance to the cursor column */
9588 temp = 0;
9589 ptr = ml_get(lnum);
9590 prev_ptr = ptr;
9591 validate_virtcol();
9592 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9593 {
9594 prev_ptr = ptr;
9595 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9596 }
9597 if ((colnr_T)temp > curwin->w_virtcol)
9598 ptr = prev_ptr;
9599
9600#ifdef FEAT_MBYTE
9601 c = (*mb_ptr2char)(ptr);
9602#else
9603 c = *ptr;
9604#endif
9605 if (c == NUL)
9606 vim_beep();
9607 return c;
9608}
9609
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009610/*
9611 * CTRL-Y or CTRL-E typed in Insert mode.
9612 */
9613 static int
9614ins_ctrl_ey(tc)
9615 int tc;
9616{
9617 int c = tc;
9618
9619#ifdef FEAT_INS_EXPAND
9620 if (ctrl_x_mode == CTRL_X_SCROLL)
9621 {
9622 if (c == Ctrl_Y)
9623 scrolldown_clamp();
9624 else
9625 scrollup_clamp();
9626 redraw_later(VALID);
9627 }
9628 else
9629#endif
9630 {
9631 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9632 if (c != NUL)
9633 {
9634 long tw_save;
9635
9636 /* The character must be taken literally, insert like it
9637 * was typed after a CTRL-V, and pretend 'textwidth'
9638 * wasn't set. Digits, 'o' and 'x' are special after a
9639 * CTRL-V, don't use it for these. */
9640 if (c < 256 && !isalnum(c))
9641 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9642 tw_save = curbuf->b_p_tw;
9643 curbuf->b_p_tw = -1;
9644 insert_special(c, TRUE, FALSE);
9645 curbuf->b_p_tw = tw_save;
9646#ifdef FEAT_RIGHTLEFT
9647 revins_chars++;
9648 revins_legal++;
9649#endif
9650 c = Ctrl_V; /* pretend CTRL-V is last character */
9651 auto_format(FALSE, TRUE);
9652 }
9653 }
9654 return c;
9655}
9656
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657#ifdef FEAT_SMARTINDENT
9658/*
9659 * Try to do some very smart auto-indenting.
9660 * Used when inserting a "normal" character.
9661 */
9662 static void
9663ins_try_si(c)
9664 int c;
9665{
9666 pos_T *pos, old_pos;
9667 char_u *ptr;
9668 int i;
9669 int temp;
9670
9671 /*
9672 * do some very smart indenting when entering '{' or '}'
9673 */
9674 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9675 {
9676 /*
9677 * for '}' set indent equal to indent of line containing matching '{'
9678 */
9679 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9680 {
9681 old_pos = curwin->w_cursor;
9682 /*
9683 * If the matching '{' has a ')' immediately before it (ignoring
9684 * white-space), then line up with the start of the line
9685 * containing the matching '(' if there is one. This handles the
9686 * case where an "if (..\n..) {" statement continues over multiple
9687 * lines -- webb
9688 */
9689 ptr = ml_get(pos->lnum);
9690 i = pos->col;
9691 if (i > 0) /* skip blanks before '{' */
9692 while (--i > 0 && vim_iswhite(ptr[i]))
9693 ;
9694 curwin->w_cursor.lnum = pos->lnum;
9695 curwin->w_cursor.col = i;
9696 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9697 curwin->w_cursor = *pos;
9698 i = get_indent();
9699 curwin->w_cursor = old_pos;
9700#ifdef FEAT_VREPLACE
9701 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009702 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 else
9704#endif
9705 (void)set_indent(i, SIN_CHANGED);
9706 }
9707 else if (curwin->w_cursor.col > 0)
9708 {
9709 /*
9710 * when inserting '{' after "O" reduce indent, but not
9711 * more than indent of previous line
9712 */
9713 temp = TRUE;
9714 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9715 {
9716 old_pos = curwin->w_cursor;
9717 i = get_indent();
9718 while (curwin->w_cursor.lnum > 1)
9719 {
9720 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9721
9722 /* ignore empty lines and lines starting with '#'. */
9723 if (*ptr != '#' && *ptr != NUL)
9724 break;
9725 }
9726 if (get_indent() >= i)
9727 temp = FALSE;
9728 curwin->w_cursor = old_pos;
9729 }
9730 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009731 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 }
9733 }
9734
9735 /*
9736 * set indent of '#' always to 0
9737 */
9738 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9739 {
9740 /* remember current indent for next line */
9741 old_indent = get_indent();
9742 (void)set_indent(0, SIN_CHANGED);
9743 }
9744
9745 /* Adjust ai_col, the char at this position can be deleted. */
9746 if (ai_col > curwin->w_cursor.col)
9747 ai_col = curwin->w_cursor.col;
9748}
9749#endif
9750
9751/*
9752 * Get the value that w_virtcol would have when 'list' is off.
9753 * Unless 'cpo' contains the 'L' flag.
9754 */
9755 static colnr_T
9756get_nolist_virtcol()
9757{
9758 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9759 return getvcol_nolist(&curwin->w_cursor);
9760 validate_virtcol();
9761 return curwin->w_virtcol;
9762}