blob: 1cf28086d998280c5fc46e9125d81dd731d02e57 [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
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000108/* When the first completion is done "compl_started" is set. When it's
109 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000111
Bram Moolenaar572cb562005-08-05 21:35:02 +0000112static int compl_matches = 0;
113static char_u *compl_pattern = NULL;
114static int compl_direction = FORWARD;
115static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000116static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static pos_T compl_startpos;
118static colnr_T compl_col = 0; /* column where the text starts
119 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000120static char_u *compl_orig_text = NULL; /* text as it was before
121 * completion started */
122static int compl_cont_mode = 0;
123static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125static void ins_ctrl_x __ARGS((void));
126static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000127static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int dup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000128static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000129static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000130static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000132static void ins_compl_upd_pum __ARGS((void));
133static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000134static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000135static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000136static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000137static 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 +0000138static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void ins_compl_free __ARGS((void));
140static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000141static int ins_compl_bs __ARGS((void));
142static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000143static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000144static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000145static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000147#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
148static void ins_compl_add_list __ARGS((list_T *list));
149#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000150static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void ins_compl_delete __ARGS((void));
152static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000153static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000154static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000156static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000157static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static int ins_complete __ARGS((int c));
159static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
160#endif /* FEAT_INS_EXPAND */
161
162#define BACKSPACE_CHAR 1
163#define BACKSPACE_WORD 2
164#define BACKSPACE_WORD_NOT_SPACE 3
165#define BACKSPACE_LINE 4
166
Bram Moolenaar754b5602006-02-09 23:53:20 +0000167static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168static void ins_ctrl_v __ARGS((void));
169static void undisplay_dollar __ARGS((void));
170static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000171static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void check_auto_format __ARGS((int));
173static void redo_literal __ARGS((int c));
174static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000175#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000176static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000177static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000178static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
181static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000182#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static int replace_pop __ARGS((void));
186static void replace_join __ARGS((int off));
187static void replace_pop_ins __ARGS((void));
188#ifdef FEAT_MBYTE
189static void mb_replace_pop_ins __ARGS((int cc));
190#endif
191static void replace_flush __ARGS((void));
192static void replace_do_bs __ARGS((void));
193#ifdef FEAT_CINDENT
194static int cindent_on __ARGS((void));
195#endif
196static void ins_reg __ARGS((void));
197static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000198static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000199static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#ifdef FEAT_RIGHTLEFT
201static void ins_ctrl_ __ARGS((void));
202#endif
203#ifdef FEAT_VISUAL
204static int ins_start_select __ARGS((int c));
205#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000206static void ins_insert __ARGS((int replaceState));
207static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ins_shift __ARGS((int c, int lastc));
209static void ins_del __ARGS((void));
210static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
211#ifdef FEAT_MOUSE
212static void ins_mouse __ARGS((int c));
213static void ins_mousescroll __ARGS((int up));
214#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000215#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
216static void ins_tabline __ARGS((int c));
217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static void ins_left __ARGS((void));
219static void ins_home __ARGS((int c));
220static void ins_end __ARGS((int c));
221static void ins_s_left __ARGS((void));
222static void ins_right __ARGS((void));
223static void ins_s_right __ARGS((void));
224static void ins_up __ARGS((int startcol));
225static void ins_pageup __ARGS((void));
226static void ins_down __ARGS((int startcol));
227static void ins_pagedown __ARGS((void));
228#ifdef FEAT_DND
229static void ins_drop __ARGS((void));
230#endif
231static int ins_tab __ARGS((void));
232static int ins_eol __ARGS((int c));
233#ifdef FEAT_DIGRAPHS
234static int ins_digraph __ARGS((void));
235#endif
236static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000237static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#ifdef FEAT_SMARTINDENT
239static void ins_try_si __ARGS((int c));
240#endif
241static colnr_T get_nolist_virtcol __ARGS((void));
242
243static colnr_T Insstart_textlen; /* length of line when insert started */
244static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
245
246static char_u *last_insert = NULL; /* the text of the previous insert,
247 K_SPECIAL and CSI are escaped */
248static int last_insert_skip; /* nr of chars in front of previous insert */
249static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000250static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251
252#ifdef FEAT_CINDENT
253static int can_cindent; /* may do cindenting on this line */
254#endif
255
256static int old_indent = 0; /* for ^^D command in insert mode */
257
258#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000259static int revins_on; /* reverse insert mode on */
260static int revins_chars; /* how much to skip after edit */
261static int revins_legal; /* was the last char 'legal'? */
262static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#endif
264
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265static int ins_need_undo; /* call u_save() before inserting a
266 char. Set when edit() is called.
267 after that arrow_used is used. */
268
269static int did_add_space = FALSE; /* auto_format() added an extra space
270 under the cursor */
271
272/*
273 * edit(): Start inserting text.
274 *
275 * "cmdchar" can be:
276 * 'i' normal insert command
277 * 'a' normal append command
278 * 'R' replace command
279 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
280 * but still only one <CR> is inserted. The <Esc> is not used for redo.
281 * 'g' "gI" command.
282 * 'V' "gR" command for Virtual Replace mode.
283 * 'v' "gr" command for single character Virtual Replace mode.
284 *
285 * This function is not called recursively. For CTRL-O commands, it returns
286 * and lets the caller handle the Normal-mode command.
287 *
288 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
289 */
290 int
291edit(cmdchar, startln, count)
292 int cmdchar;
293 int startln; /* if set, insert at start of line */
294 long count;
295{
296 int c = 0;
297 char_u *ptr;
298 int lastc;
299 colnr_T mincol;
300 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 int i;
302 int did_backspace = TRUE; /* previous char was backspace */
303#ifdef FEAT_CINDENT
304 int line_is_white = FALSE; /* line is empty before insert */
305#endif
306 linenr_T old_topline = 0; /* topline before insertion */
307#ifdef FEAT_DIFF
308 int old_topfill = -1;
309#endif
310 int inserted_space = FALSE; /* just inserted a space */
311 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000312 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000314 /* Remember whether editing was restarted after CTRL-O. */
315 did_restart_edit = restart_edit;
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 /* sleep before redrawing, needed for "CTRL-O :" that results in an
318 * error message */
319 check_for_delay(TRUE);
320
321#ifdef HAVE_SANDBOX
322 /* Don't allow inserting in the sandbox. */
323 if (sandbox != 0)
324 {
325 EMSG(_(e_sandbox));
326 return FALSE;
327 }
328#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000329 /* Don't allow changes in the buffer while editing the cmdline. The
330 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000331 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000332 {
333 EMSG(_(e_secure));
334 return FALSE;
335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000338 /* Don't allow recursive insert mode when busy with completion. */
339 if (compl_started || pum_visible())
340 {
341 EMSG(_(e_secure));
342 return FALSE;
343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 ins_compl_clear(); /* clear stuff for CTRL-X mode */
345#endif
346
Bram Moolenaar843ee412004-06-30 16:16:41 +0000347#ifdef FEAT_AUTOCMD
348 /*
349 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
350 */
351 if (cmdchar != 'r' && cmdchar != 'v')
352 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000353# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000354 if (cmdchar == 'R')
355 ptr = (char_u *)"r";
356 else if (cmdchar == 'V')
357 ptr = (char_u *)"v";
358 else
359 ptr = (char_u *)"i";
360 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000361# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000362 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
363 }
364#endif
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#ifdef FEAT_MOUSE
367 /*
368 * When doing a paste with the middle mouse button, Insstart is set to
369 * where the paste started.
370 */
371 if (where_paste_started.lnum != 0)
372 Insstart = where_paste_started;
373 else
374#endif
375 {
376 Insstart = curwin->w_cursor;
377 if (startln)
378 Insstart.col = 0;
379 }
380 Insstart_textlen = linetabsize(ml_get_curline());
381 Insstart_blank_vcol = MAXCOL;
382 if (!did_ai)
383 ai_col = 0;
384
385 if (cmdchar != NUL && restart_edit == 0)
386 {
387 ResetRedobuff();
388 AppendNumberToRedobuff(count);
389#ifdef FEAT_VREPLACE
390 if (cmdchar == 'V' || cmdchar == 'v')
391 {
392 /* "gR" or "gr" command */
393 AppendCharToRedobuff('g');
394 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
395 }
396 else
397#endif
398 {
399 AppendCharToRedobuff(cmdchar);
400 if (cmdchar == 'g') /* "gI" command */
401 AppendCharToRedobuff('I');
402 else if (cmdchar == 'r') /* "r<CR>" command */
403 count = 1; /* insert only one <CR> */
404 }
405 }
406
407 if (cmdchar == 'R')
408 {
409#ifdef FEAT_FKMAP
410 if (p_fkmap && p_ri)
411 {
412 beep_flush();
413 EMSG(farsi_text_3); /* encoded in Farsi */
414 State = INSERT;
415 }
416 else
417#endif
418 State = REPLACE;
419 }
420#ifdef FEAT_VREPLACE
421 else if (cmdchar == 'V' || cmdchar == 'v')
422 {
423 State = VREPLACE;
424 replaceState = VREPLACE;
425 orig_line_count = curbuf->b_ml.ml_line_count;
426 vr_lines_changed = 1;
427 }
428#endif
429 else
430 State = INSERT;
431
432 stop_insert_mode = FALSE;
433
434 /*
435 * Need to recompute the cursor position, it might move when the cursor is
436 * on a TAB or special character.
437 */
438 curs_columns(TRUE);
439
440 /*
441 * Enable langmap or IME, indicated by 'iminsert'.
442 * Note that IME may enabled/disabled without us noticing here, thus the
443 * 'iminsert' value may not reflect what is actually used. It is updated
444 * when hitting <Esc>.
445 */
446 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
447 State |= LANGMAP;
448#ifdef USE_IM_CONTROL
449 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
450#endif
451
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452#ifdef FEAT_MOUSE
453 setmouse();
454#endif
455#ifdef FEAT_CMDL_INFO
456 clear_showcmd();
457#endif
458#ifdef FEAT_RIGHTLEFT
459 /* there is no reverse replace mode */
460 revins_on = (State == INSERT && p_ri);
461 if (revins_on)
462 undisplay_dollar();
463 revins_chars = 0;
464 revins_legal = 0;
465 revins_scol = -1;
466#endif
467
468 /*
469 * Handle restarting Insert mode.
470 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
471 * restart_edit non-zero, and something in the stuff buffer.
472 */
473 if (restart_edit != 0 && stuff_empty())
474 {
475#ifdef FEAT_MOUSE
476 /*
477 * After a paste we consider text typed to be part of the insert for
478 * the pasted text. You can backspace over the pasted text too.
479 */
480 if (where_paste_started.lnum)
481 arrow_used = FALSE;
482 else
483#endif
484 arrow_used = TRUE;
485 restart_edit = 0;
486
487 /*
488 * If the cursor was after the end-of-line before the CTRL-O and it is
489 * now at the end-of-line, put it after the end-of-line (this is not
490 * correct in very rare cases).
491 * Also do this if curswant is greater than the current virtual
492 * column. Eg after "^O$" or "^O80|".
493 */
494 validate_virtcol();
495 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000496 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 || curwin->w_curswant > curwin->w_virtcol)
498 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
499 {
500 if (ptr[1] == NUL)
501 ++curwin->w_cursor.col;
502#ifdef FEAT_MBYTE
503 else if (has_mbyte)
504 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000505 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (ptr[i] == NUL)
507 curwin->w_cursor.col += i;
508 }
509#endif
510 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000511 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
513 else
514 arrow_used = FALSE;
515
516 /* we are in insert mode now, don't need to start it anymore */
517 need_start_insertmode = FALSE;
518
519 /* Need to save the line for undo before inserting the first char. */
520 ins_need_undo = TRUE;
521
522#ifdef FEAT_MOUSE
523 where_paste_started.lnum = 0;
524#endif
525#ifdef FEAT_CINDENT
526 can_cindent = TRUE;
527#endif
528#ifdef FEAT_FOLDING
529 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
530 * restarting. */
531 if (!p_im && did_restart_edit == 0)
532 foldOpenCursor();
533#endif
534
535 /*
536 * If 'showmode' is set, show the current (insert/replace/..) mode.
537 * A warning message for changing a readonly file is given here, before
538 * actually changing anything. It's put after the mode, if any.
539 */
540 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000541 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 i = showmode();
543
544 if (!p_im && did_restart_edit == 0)
545 change_warning(i + 1);
546
547#ifdef CURSOR_SHAPE
548 ui_cursor_shape(); /* may show different cursor shape */
549#endif
550#ifdef FEAT_DIGRAPHS
551 do_digraph(-1); /* clear digraphs */
552#endif
553
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000554 /*
555 * Get the current length of the redo buffer, those characters have to be
556 * skipped if we want to get to the inserted characters.
557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 ptr = get_inserted();
559 if (ptr == NULL)
560 new_insert_skip = 0;
561 else
562 {
563 new_insert_skip = (int)STRLEN(ptr);
564 vim_free(ptr);
565 }
566
567 old_indent = 0;
568
569 /*
570 * Main loop in Insert mode: repeat until Insert mode is left.
571 */
572 for (;;)
573 {
574#ifdef FEAT_RIGHTLEFT
575 if (!revins_legal)
576 revins_scol = -1; /* reset on illegal motions */
577 else
578 revins_legal = 0;
579#endif
580 if (arrow_used) /* don't repeat insert when arrow key used */
581 count = 0;
582
583 if (stop_insert_mode)
584 {
585 /* ":stopinsert" used or 'insertmode' reset */
586 count = 0;
587 goto doESCkey;
588 }
589
590 /* set curwin->w_curswant for next K_DOWN or K_UP */
591 if (!arrow_used)
592 curwin->w_set_curswant = TRUE;
593
594 /* If there is no typeahead may check for timestamps (e.g., for when a
595 * menu invoked a shell command). */
596 if (stuff_empty())
597 {
598 did_check_timestamps = FALSE;
599 if (need_check_timestamps)
600 check_timestamps(FALSE);
601 }
602
603 /*
604 * When emsg() was called msg_scroll will have been set.
605 */
606 msg_scroll = FALSE;
607
608#ifdef FEAT_GUI
609 /* When 'mousefocus' is set a mouse movement may have taken us to
610 * another window. "need_mouse_correct" may then be set because of an
611 * autocommand. */
612 if (need_mouse_correct)
613 gui_mouse_correct();
614#endif
615
616#ifdef FEAT_FOLDING
617 /* Open fold at the cursor line, according to 'foldopen'. */
618 if (fdo_flags & FDO_INSERT)
619 foldOpenCursor();
620 /* Close folds where the cursor isn't, according to 'foldclose' */
621 if (!char_avail())
622 foldCheckClose();
623#endif
624
625 /*
626 * If we inserted a character at the last position of the last line in
627 * the window, scroll the window one line up. This avoids an extra
628 * redraw.
629 * This is detected when the cursor column is smaller after inserting
630 * something.
631 * Don't do this when the topline changed already, it has
632 * already been adjusted (by insertchar() calling open_line())).
633 */
634 if (curbuf->b_mod_set
635 && curwin->w_p_wrap
636 && !did_backspace
637 && curwin->w_topline == old_topline
638#ifdef FEAT_DIFF
639 && curwin->w_topfill == old_topfill
640#endif
641 )
642 {
643 mincol = curwin->w_wcol;
644 validate_cursor_col();
645
646 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
647 && curwin->w_wrow == W_WINROW(curwin)
648 + curwin->w_height - 1 - p_so
649 && (curwin->w_cursor.lnum != curwin->w_topline
650#ifdef FEAT_DIFF
651 || curwin->w_topfill > 0
652#endif
653 ))
654 {
655#ifdef FEAT_DIFF
656 if (curwin->w_topfill > 0)
657 --curwin->w_topfill;
658 else
659#endif
660#ifdef FEAT_FOLDING
661 if (hasFolding(curwin->w_topline, NULL, &old_topline))
662 set_topline(curwin, old_topline + 1);
663 else
664#endif
665 set_topline(curwin, curwin->w_topline + 1);
666 }
667 }
668
669 /* May need to adjust w_topline to show the cursor. */
670 update_topline();
671
672 did_backspace = FALSE;
673
674 validate_cursor(); /* may set must_redraw */
675
676 /*
677 * Redraw the display when no characters are waiting.
678 * Also shows mode, ruler and positions cursor.
679 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000680 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682#ifdef FEAT_SCROLLBIND
683 if (curwin->w_p_scb)
684 do_check_scrollbind(TRUE);
685#endif
686
687 update_curswant();
688 old_topline = curwin->w_topline;
689#ifdef FEAT_DIFF
690 old_topfill = curwin->w_topfill;
691#endif
692
693#ifdef USE_ON_FLY_SCROLL
694 dont_scroll = FALSE; /* allow scrolling here */
695#endif
696
697 /*
698 * Get a character for Insert mode.
699 */
700 lastc = c; /* remember previous char for CTRL-D */
701 c = safe_vgetc();
702
703#ifdef FEAT_RIGHTLEFT
704 if (p_hkmap && KeyTyped)
705 c = hkmap(c); /* Hebrew mode mapping */
706#endif
707#ifdef FEAT_FKMAP
708 if (p_fkmap && KeyTyped)
709 c = fkmap(c); /* Farsi mode mapping */
710#endif
711
712#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000713 /*
714 * Special handling of keys while the popup menu is visible or wanted
715 * and the cursor is still in the completed word.
716 */
717 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000718 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000719 /* BS: Delete one character from "compl_leader". */
720 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000721 && curwin->w_cursor.col > compl_col
722 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000723 continue;
724
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000725 /* When no match was selected or it was edited. */
726 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000727 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000728 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000729 * "compl_leader". Except when at the original match and
730 * there is nothing to add, CTRL-L works like CTRL-P then. */
731 if (c == Ctrl_L
732 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
733 || STRLEN(compl_shown_match->cp_str)
734 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 {
736 ins_compl_addfrommatch();
737 continue;
738 }
739
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000740 /* A printable, non-white character: Add to "compl_leader". */
741 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000742 {
743 ins_compl_addleader(c);
744 continue;
745 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000746
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000747 /* Pressing CTRL-Y selects the current match. Shen
748 * compl_enter_selects is set the Enter key does the same. */
749 if (c == Ctrl_Y || (compl_enter_selects
750 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000751 {
752 ins_compl_delete();
753 ins_compl_insert();
754 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000755 }
756 }
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
759 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000760 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000761 if (c != K_IGNORE && ins_compl_prep(c))
762 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
764
Bram Moolenaar488c6512005-08-11 20:09:58 +0000765 /* CTRL-\ CTRL-N goes to Normal mode,
766 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
767 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (c == Ctrl_BSL)
769 {
770 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000771 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 ++no_mapping;
773 ++allow_keys;
774 c = safe_vgetc();
775 --no_mapping;
776 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000777 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000779 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 vungetc(c);
781 c = Ctrl_BSL;
782 }
783 else if (c == Ctrl_G && p_im)
784 continue;
785 else
786 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000787 if (c == Ctrl_O)
788 {
789 ins_ctrl_o();
790 ins_at_eol = FALSE; /* cursor keeps its column */
791 nomove = TRUE;
792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 count = 0;
794 goto doESCkey;
795 }
796 }
797
798#ifdef FEAT_DIGRAPHS
799 c = do_digraph(c);
800#endif
801
802#ifdef FEAT_INS_EXPAND
803 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
804 goto docomplete;
805#endif
806 if (c == Ctrl_V || c == Ctrl_Q)
807 {
808 ins_ctrl_v();
809 c = Ctrl_V; /* pretend CTRL-V is last typed character */
810 continue;
811 }
812
813#ifdef FEAT_CINDENT
814 if (cindent_on()
815# ifdef FEAT_INS_EXPAND
816 && ctrl_x_mode == 0
817# endif
818 )
819 {
820 /* A key name preceded by a bang means this key is not to be
821 * inserted. Skip ahead to the re-indenting below.
822 * A key name preceded by a star means that indenting has to be
823 * done before inserting the key. */
824 line_is_white = inindent(0);
825 if (in_cinkeys(c, '!', line_is_white))
826 goto force_cindent;
827 if (can_cindent && in_cinkeys(c, '*', line_is_white)
828 && stop_arrow() == OK)
829 do_c_expr_indent();
830 }
831#endif
832
833#ifdef FEAT_RIGHTLEFT
834 if (curwin->w_p_rl)
835 switch (c)
836 {
837 case K_LEFT: c = K_RIGHT; break;
838 case K_S_LEFT: c = K_S_RIGHT; break;
839 case K_C_LEFT: c = K_C_RIGHT; break;
840 case K_RIGHT: c = K_LEFT; break;
841 case K_S_RIGHT: c = K_S_LEFT; break;
842 case K_C_RIGHT: c = K_C_LEFT; break;
843 }
844#endif
845
846#ifdef FEAT_VISUAL
847 /*
848 * If 'keymodel' contains "startsel", may start selection. If it
849 * does, a CTRL-O and c will be stuffed, we need to get these
850 * characters.
851 */
852 if (ins_start_select(c))
853 continue;
854#endif
855
856 /*
857 * The big switch to handle a character in insert mode.
858 */
859 switch (c)
860 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000861 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (echeck_abbr(ESC + ABBR_OFF))
863 break;
864 /*FALLTHROUGH*/
865
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000866 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#ifdef FEAT_CMDWIN
868 if (c == Ctrl_C && cmdwin_type != 0)
869 {
870 /* Close the cmdline window. */
871 cmdwin_result = K_IGNORE;
872 got_int = FALSE; /* don't stop executing autocommands et al. */
873 goto doESCkey;
874 }
875#endif
876
877#ifdef UNIX
878do_intr:
879#endif
880 /* when 'insertmode' set, and not halfway a mapping, don't leave
881 * Insert mode */
882 if (goto_im())
883 {
884 if (got_int)
885 {
886 (void)vgetc(); /* flush all buffers */
887 got_int = FALSE;
888 }
889 else
890 vim_beep();
891 break;
892 }
893doESCkey:
894 /*
895 * This is the ONLY return from edit()!
896 */
897 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
898 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000899 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 o_lnum = curwin->w_cursor.lnum;
901
Bram Moolenaar488c6512005-08-11 20:09:58 +0000902 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000903 {
904#ifdef FEAT_AUTOCMD
905 if (cmdchar != 'r' && cmdchar != 'v')
906 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
907 FALSE, curbuf);
908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 continue;
912
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000913 case Ctrl_Z: /* suspend when 'insertmode' set */
914 if (!p_im)
915 goto normalchar; /* insert CTRL-Z as normal char */
916 stuffReadbuff((char_u *)":st\r");
917 c = Ctrl_O;
918 /*FALLTHROUGH*/
919
920 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000921#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000922 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000923 goto docomplete;
924#endif
925 if (echeck_abbr(Ctrl_O + ABBR_OFF))
926 break;
927 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000928
929#ifdef FEAT_VIRTUALEDIT
930 /* don't move the cursor left when 'virtualedit' has "onemore". */
931 if (ve_flags & VE_ONEMORE)
932 {
933 ins_at_eol = FALSE;
934 nomove = TRUE;
935 }
936#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 count = 0;
938 goto doESCkey;
939
Bram Moolenaar572cb562005-08-05 21:35:02 +0000940 case K_INS: /* toggle insert/replace mode */
941 case K_KINS:
942 ins_insert(replaceState);
943 break;
944
945 case K_SELECT: /* end of Select mode mapping - ignore */
946 break;
947
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000948#ifdef FEAT_SNIFF
949 case K_SNIFF: /* Sniff command received */
950 stuffcharReadbuff(K_SNIFF);
951 goto doESCkey;
952#endif
953
954 case K_HELP: /* Help key works like <ESC> <Help> */
955 case K_F1:
956 case K_XF1:
957 stuffcharReadbuff(K_HELP);
958 if (p_im)
959 need_start_insertmode = TRUE;
960 goto doESCkey;
961
962#ifdef FEAT_NETBEANS_INTG
963 case K_F21: /* NetBeans command */
964 ++no_mapping; /* don't map the next key hits */
965 i = safe_vgetc();
966 --no_mapping;
967 netbeans_keycommand(i);
968 break;
969#endif
970
971 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 case NUL:
973 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974 /* For ^@ the trailing ESC will end the insert, unless there is an
975 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
977 && c != Ctrl_A && !p_im)
978 goto doESCkey; /* quit insert mode */
979 inserted_space = FALSE;
980 break;
981
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 ins_reg();
984 auto_format(FALSE, TRUE);
985 inserted_space = FALSE;
986 break;
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 ins_ctrl_g();
990 break;
991
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 case Ctrl_HAT: /* switch input mode and/or langmap */
993 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 break;
995
996#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (!p_ari)
999 goto normalchar;
1000 ins_ctrl_();
1001 break;
1002#endif
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1006 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1007 goto docomplete;
1008#endif
1009 /* FALLTHROUGH */
1010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012# ifdef FEAT_INS_EXPAND
1013 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1014 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 if (has_compl_option(FALSE))
1016 goto docomplete;
1017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 }
1019# endif
1020 ins_shift(c, lastc);
1021 auto_format(FALSE, TRUE);
1022 inserted_space = FALSE;
1023 break;
1024
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001025 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 case K_KDEL:
1027 ins_del();
1028 auto_format(FALSE, TRUE);
1029 break;
1030
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 case Ctrl_H:
1033 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1034 auto_format(FALSE, TRUE);
1035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1039 auto_format(FALSE, TRUE);
1040 break;
1041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001043# ifdef FEAT_COMPL_FUNC
1044 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001046 goto docomplete;
1047# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1049 auto_format(FALSE, TRUE);
1050 inserted_space = FALSE;
1051 break;
1052
1053#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 case K_LEFTMOUSE_NM:
1056 case K_LEFTDRAG:
1057 case K_LEFTRELEASE:
1058 case K_LEFTRELEASE_NM:
1059 case K_MIDDLEMOUSE:
1060 case K_MIDDLEDRAG:
1061 case K_MIDDLERELEASE:
1062 case K_RIGHTMOUSE:
1063 case K_RIGHTDRAG:
1064 case K_RIGHTRELEASE:
1065 case K_X1MOUSE:
1066 case K_X1DRAG:
1067 case K_X1RELEASE:
1068 case K_X2MOUSE:
1069 case K_X2DRAG:
1070 case K_X2RELEASE:
1071 ins_mouse(c);
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 ins_mousescroll(FALSE);
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 ins_mousescroll(TRUE);
1080 break;
1081#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001082#ifdef FEAT_GUI_TABLINE
1083 case K_TABLINE:
1084 case K_TABMENU:
1085 ins_tabline(c);
1086 break;
1087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 break;
1091
Bram Moolenaar754b5602006-02-09 23:53:20 +00001092#ifdef FEAT_AUTOCMD
1093 case K_CURSORHOLD: /* Didn't type something for a while. */
1094 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1095 did_cursorhold = TRUE;
1096 break;
1097#endif
1098
Bram Moolenaar4770d092006-01-12 23:22:24 +00001099#ifdef FEAT_GUI_W32
1100 /* On Win32 ignore <M-F4>, we get it when closing the window was
1101 * cancelled. */
1102 case K_F4:
1103 if (mod_mask != MOD_MASK_ALT)
1104 goto normalchar;
1105 break;
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#ifdef FEAT_GUI
1109 case K_VER_SCROLLBAR:
1110 ins_scroll();
1111 break;
1112
1113 case K_HOR_SCROLLBAR:
1114 ins_horscroll();
1115 break;
1116#endif
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 case K_S_HOME:
1121 case K_C_HOME:
1122 ins_home(c);
1123 break;
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case K_S_END:
1128 case K_C_END:
1129 ins_end(c);
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001133 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1134 ins_s_left();
1135 else
1136 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 break;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 case K_C_LEFT:
1141 ins_s_left();
1142 break;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001145 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1146 ins_s_right();
1147 else
1148 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_C_RIGHT:
1153 ins_s_right();
1154 break;
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001157#ifdef FEAT_INS_EXPAND
1158 if (pum_visible())
1159 goto docomplete;
1160#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001161 if (mod_mask & MOD_MASK_SHIFT)
1162 ins_pageup();
1163 else
1164 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 break;
1166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001167 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 case K_PAGEUP:
1169 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001170#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001171 if (pum_visible())
1172 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 ins_pageup();
1175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001178#ifdef FEAT_INS_EXPAND
1179 if (pum_visible())
1180 goto docomplete;
1181#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001182 if (mod_mask & MOD_MASK_SHIFT)
1183 ins_pagedown();
1184 else
1185 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_PAGEDOWN:
1190 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001191#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001192 if (pum_visible())
1193 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 ins_pagedown();
1196 break;
1197
1198#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001199 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 ins_drop();
1201 break;
1202#endif
1203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 c = TAB;
1206 /* FALLTHROUGH */
1207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1210 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1211 goto docomplete;
1212#endif
1213 inserted_space = FALSE;
1214 if (ins_tab())
1215 goto normalchar; /* insert TAB as a normal char */
1216 auto_format(FALSE, TRUE);
1217 break;
1218
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001219 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 c = CAR;
1221 /* FALLTHROUGH */
1222 case CAR:
1223 case NL:
1224#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1225 /* In a quickfix window a <CR> jumps to the error under the
1226 * cursor. */
1227 if (bt_quickfix(curbuf) && c == CAR)
1228 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001229 if (curwin->w_llist_ref == NULL) /* quickfix window */
1230 do_cmdline_cmd((char_u *)".cc");
1231 else /* location list window */
1232 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 break;
1234 }
1235#endif
1236#ifdef FEAT_CMDWIN
1237 if (cmdwin_type != 0)
1238 {
1239 /* Execute the command in the cmdline window. */
1240 cmdwin_result = CAR;
1241 goto doESCkey;
1242 }
1243#endif
1244 if (ins_eol(c) && !p_im)
1245 goto doESCkey; /* out of memory */
1246 auto_format(FALSE, FALSE);
1247 inserted_space = FALSE;
1248 break;
1249
1250#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001251 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252# ifdef FEAT_INS_EXPAND
1253 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1254 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 if (has_compl_option(TRUE))
1256 goto docomplete;
1257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259# endif
1260# ifdef FEAT_DIGRAPHS
1261 c = ins_digraph();
1262 if (c == NUL)
1263 break;
1264# endif
1265 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001269 case Ctrl_X: /* Enter CTRL-X mode */
1270 ins_ctrl_x();
1271 break;
1272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 if (ctrl_x_mode != CTRL_X_TAGS)
1275 goto normalchar;
1276 goto docomplete;
1277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (ctrl_x_mode != CTRL_X_FILES)
1280 goto normalchar;
1281 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001282
1283 case 's': /* Spelling completion after ^X */
1284 case Ctrl_S:
1285 if (ctrl_x_mode != CTRL_X_SPELL)
1286 goto normalchar;
1287 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#ifdef FEAT_INS_EXPAND
1292 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1293#endif
1294 {
1295 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1296 if (p_im)
1297 {
1298 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1299 break;
1300 goto doESCkey;
1301 }
1302 goto normalchar;
1303 }
1304#ifdef FEAT_INS_EXPAND
1305 /* FALLTHROUGH */
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 case Ctrl_N:
1309 /* if 'complete' is empty then plain ^P is no longer special,
1310 * but it is under other ^X modes */
1311 if (*curbuf->b_p_cpt == NUL
1312 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 goto normalchar;
1315
1316docomplete:
1317 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 break;
1320#endif /* FEAT_INS_EXPAND */
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case Ctrl_Y: /* copy from previous line or scroll down */
1323 case Ctrl_E: /* copy from next line or scroll up */
1324 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 break;
1326
1327 default:
1328#ifdef UNIX
1329 if (c == intr_char) /* special interrupt char */
1330 goto do_intr;
1331#endif
1332
1333 /*
1334 * Insert a nomal character.
1335 */
1336normalchar:
1337#ifdef FEAT_SMARTINDENT
1338 /* Try to perform smart-indenting. */
1339 ins_try_si(c);
1340#endif
1341
1342 if (c == ' ')
1343 {
1344 inserted_space = TRUE;
1345#ifdef FEAT_CINDENT
1346 if (inindent(0))
1347 can_cindent = FALSE;
1348#endif
1349 if (Insstart_blank_vcol == MAXCOL
1350 && curwin->w_cursor.lnum == Insstart.lnum)
1351 Insstart_blank_vcol = get_nolist_virtcol();
1352 }
1353
1354 if (vim_iswordc(c) || !echeck_abbr(
1355#ifdef FEAT_MBYTE
1356 /* Add ABBR_OFF for characters above 0x100, this is
1357 * what check_abbr() expects. */
1358 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1359#endif
1360 c))
1361 {
1362 insert_special(c, FALSE, FALSE);
1363#ifdef FEAT_RIGHTLEFT
1364 revins_legal++;
1365 revins_chars++;
1366#endif
1367 }
1368
1369 auto_format(FALSE, TRUE);
1370
1371#ifdef FEAT_FOLDING
1372 /* When inserting a character the cursor line must never be in a
1373 * closed fold. */
1374 foldOpenCursor();
1375#endif
1376 break;
1377 } /* end of switch (c) */
1378
1379 /* If the cursor was moved we didn't just insert a space */
1380 if (arrow_used)
1381 inserted_space = FALSE;
1382
1383#ifdef FEAT_CINDENT
1384 if (can_cindent && cindent_on()
1385# ifdef FEAT_INS_EXPAND
1386 && ctrl_x_mode == 0
1387# endif
1388 )
1389 {
1390force_cindent:
1391 /*
1392 * Indent now if a key was typed that is in 'cinkeys'.
1393 */
1394 if (in_cinkeys(c, ' ', line_is_white))
1395 {
1396 if (stop_arrow() == OK)
1397 /* re-indent the current line */
1398 do_c_expr_indent();
1399 }
1400 }
1401#endif /* FEAT_CINDENT */
1402
1403 } /* for (;;) */
1404 /* NOTREACHED */
1405}
1406
1407/*
1408 * Redraw for Insert mode.
1409 * This is postponed until getting the next character to make '$' in the 'cpo'
1410 * option work correctly.
1411 * Only redraw when there are no characters available. This speeds up
1412 * inserting sequences of characters (e.g., for CTRL-R).
1413 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001414/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001416ins_redraw(ready)
1417 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 if (!char_avail())
1420 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001421#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001422 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1423 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001424 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001425 && !equalpos(last_cursormoved, curwin->w_cursor)
1426# ifdef FEAT_INS_EXPAND
1427 && !pum_visible()
1428# endif
1429 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001430 {
1431 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1432 last_cursormoved = curwin->w_cursor;
1433 }
1434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (must_redraw)
1436 update_screen(0);
1437 else if (clear_cmdline || redraw_cmdline)
1438 showmode(); /* clear cmdline and show mode */
1439 showruler(FALSE);
1440 setcursor();
1441 emsg_on_display = FALSE; /* may remove error message now */
1442 }
1443}
1444
1445/*
1446 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1447 */
1448 static void
1449ins_ctrl_v()
1450{
1451 int c;
1452
1453 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001454 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456 if (redrawing() && !char_avail())
1457 edit_putchar('^', TRUE);
1458 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1459
1460#ifdef FEAT_CMDL_INFO
1461 add_to_showcmd_c(Ctrl_V);
1462#endif
1463
1464 c = get_literal();
1465#ifdef FEAT_CMDL_INFO
1466 clear_showcmd();
1467#endif
1468 insert_special(c, FALSE, TRUE);
1469#ifdef FEAT_RIGHTLEFT
1470 revins_chars++;
1471 revins_legal++;
1472#endif
1473}
1474
1475/*
1476 * Put a character directly onto the screen. It's not stored in a buffer.
1477 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1478 */
1479static int pc_status;
1480#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1481#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1482#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1483#define PC_STATUS_SET 3 /* pc_bytes was filled */
1484#ifdef FEAT_MBYTE
1485static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1486#else
1487static char_u pc_bytes[2]; /* saved bytes */
1488#endif
1489static int pc_attr;
1490static int pc_row;
1491static int pc_col;
1492
1493 void
1494edit_putchar(c, highlight)
1495 int c;
1496 int highlight;
1497{
1498 int attr;
1499
1500 if (ScreenLines != NULL)
1501 {
1502 update_topline(); /* just in case w_topline isn't valid */
1503 validate_cursor();
1504 if (highlight)
1505 attr = hl_attr(HLF_8);
1506 else
1507 attr = 0;
1508 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1509 pc_col = W_WINCOL(curwin);
1510#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1511 pc_status = PC_STATUS_UNSET;
1512#endif
1513#ifdef FEAT_RIGHTLEFT
1514 if (curwin->w_p_rl)
1515 {
1516 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1517# ifdef FEAT_MBYTE
1518 if (has_mbyte)
1519 {
1520 int fix_col = mb_fix_col(pc_col, pc_row);
1521
1522 if (fix_col != pc_col)
1523 {
1524 screen_putchar(' ', pc_row, fix_col, attr);
1525 --curwin->w_wcol;
1526 pc_status = PC_STATUS_RIGHT;
1527 }
1528 }
1529# endif
1530 }
1531 else
1532#endif
1533 {
1534 pc_col += curwin->w_wcol;
1535#ifdef FEAT_MBYTE
1536 if (mb_lefthalve(pc_row, pc_col))
1537 pc_status = PC_STATUS_LEFT;
1538#endif
1539 }
1540
1541 /* save the character to be able to put it back */
1542#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1543 if (pc_status == PC_STATUS_UNSET)
1544#endif
1545 {
1546 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1547 pc_status = PC_STATUS_SET;
1548 }
1549 screen_putchar(c, pc_row, pc_col, attr);
1550 }
1551}
1552
1553/*
1554 * Undo the previous edit_putchar().
1555 */
1556 void
1557edit_unputchar()
1558{
1559 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1560 {
1561#if defined(FEAT_MBYTE)
1562 if (pc_status == PC_STATUS_RIGHT)
1563 ++curwin->w_wcol;
1564 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1565 redrawWinline(curwin->w_cursor.lnum, FALSE);
1566 else
1567#endif
1568 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1569 }
1570}
1571
1572/*
1573 * Called when p_dollar is set: display a '$' at the end of the changed text
1574 * Only works when cursor is in the line that changes.
1575 */
1576 void
1577display_dollar(col)
1578 colnr_T col;
1579{
1580 colnr_T save_col;
1581
1582 if (!redrawing())
1583 return;
1584
1585 cursor_off();
1586 save_col = curwin->w_cursor.col;
1587 curwin->w_cursor.col = col;
1588#ifdef FEAT_MBYTE
1589 if (has_mbyte)
1590 {
1591 char_u *p;
1592
1593 /* If on the last byte of a multi-byte move to the first byte. */
1594 p = ml_get_curline();
1595 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1596 }
1597#endif
1598 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1599 if (curwin->w_wcol < W_WIDTH(curwin))
1600 {
1601 edit_putchar('$', FALSE);
1602 dollar_vcol = curwin->w_virtcol;
1603 }
1604 curwin->w_cursor.col = save_col;
1605}
1606
1607/*
1608 * Call this function before moving the cursor from the normal insert position
1609 * in insert mode.
1610 */
1611 static void
1612undisplay_dollar()
1613{
1614 if (dollar_vcol)
1615 {
1616 dollar_vcol = 0;
1617 redrawWinline(curwin->w_cursor.lnum, FALSE);
1618 }
1619}
1620
1621/*
1622 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1623 * Keep the cursor on the same character.
1624 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1625 * type == INDENT_DEC decrease indent (for CTRL-D)
1626 * type == INDENT_SET set indent to "amount"
1627 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1628 */
1629 void
1630change_indent(type, amount, round, replaced)
1631 int type;
1632 int amount;
1633 int round;
1634 int replaced; /* replaced character, put on replace stack */
1635{
1636 int vcol;
1637 int last_vcol;
1638 int insstart_less; /* reduction for Insstart.col */
1639 int new_cursor_col;
1640 int i;
1641 char_u *ptr;
1642 int save_p_list;
1643 int start_col;
1644 colnr_T vc;
1645#ifdef FEAT_VREPLACE
1646 colnr_T orig_col = 0; /* init for GCC */
1647 char_u *new_line, *orig_line = NULL; /* init for GCC */
1648
1649 /* VREPLACE mode needs to know what the line was like before changing */
1650 if (State & VREPLACE_FLAG)
1651 {
1652 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1653 orig_col = curwin->w_cursor.col;
1654 }
1655#endif
1656
1657 /* for the following tricks we don't want list mode */
1658 save_p_list = curwin->w_p_list;
1659 curwin->w_p_list = FALSE;
1660 vc = getvcol_nolist(&curwin->w_cursor);
1661 vcol = vc;
1662
1663 /*
1664 * For Replace mode we need to fix the replace stack later, which is only
1665 * possible when the cursor is in the indent. Remember the number of
1666 * characters before the cursor if it's possible.
1667 */
1668 start_col = curwin->w_cursor.col;
1669
1670 /* determine offset from first non-blank */
1671 new_cursor_col = curwin->w_cursor.col;
1672 beginline(BL_WHITE);
1673 new_cursor_col -= curwin->w_cursor.col;
1674
1675 insstart_less = curwin->w_cursor.col;
1676
1677 /*
1678 * If the cursor is in the indent, compute how many screen columns the
1679 * cursor is to the left of the first non-blank.
1680 */
1681 if (new_cursor_col < 0)
1682 vcol = get_indent() - vcol;
1683
1684 if (new_cursor_col > 0) /* can't fix replace stack */
1685 start_col = -1;
1686
1687 /*
1688 * Set the new indent. The cursor will be put on the first non-blank.
1689 */
1690 if (type == INDENT_SET)
1691 (void)set_indent(amount, SIN_CHANGED);
1692 else
1693 {
1694#ifdef FEAT_VREPLACE
1695 int save_State = State;
1696
1697 /* Avoid being called recursively. */
1698 if (State & VREPLACE_FLAG)
1699 State = INSERT;
1700#endif
1701 shift_line(type == INDENT_DEC, round, 1);
1702#ifdef FEAT_VREPLACE
1703 State = save_State;
1704#endif
1705 }
1706 insstart_less -= curwin->w_cursor.col;
1707
1708 /*
1709 * Try to put cursor on same character.
1710 * If the cursor is at or after the first non-blank in the line,
1711 * compute the cursor column relative to the column of the first
1712 * non-blank character.
1713 * If we are not in insert mode, leave the cursor on the first non-blank.
1714 * If the cursor is before the first non-blank, position it relative
1715 * to the first non-blank, counted in screen columns.
1716 */
1717 if (new_cursor_col >= 0)
1718 {
1719 /*
1720 * When changing the indent while the cursor is touching it, reset
1721 * Insstart_col to 0.
1722 */
1723 if (new_cursor_col == 0)
1724 insstart_less = MAXCOL;
1725 new_cursor_col += curwin->w_cursor.col;
1726 }
1727 else if (!(State & INSERT))
1728 new_cursor_col = curwin->w_cursor.col;
1729 else
1730 {
1731 /*
1732 * Compute the screen column where the cursor should be.
1733 */
1734 vcol = get_indent() - vcol;
1735 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1736
1737 /*
1738 * Advance the cursor until we reach the right screen column.
1739 */
1740 vcol = last_vcol = 0;
1741 new_cursor_col = -1;
1742 ptr = ml_get_curline();
1743 while (vcol <= (int)curwin->w_virtcol)
1744 {
1745 last_vcol = vcol;
1746#ifdef FEAT_MBYTE
1747 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001748 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 else
1750#endif
1751 ++new_cursor_col;
1752 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1753 }
1754 vcol = last_vcol;
1755
1756 /*
1757 * May need to insert spaces to be able to position the cursor on
1758 * the right screen column.
1759 */
1760 if (vcol != (int)curwin->w_virtcol)
1761 {
1762 curwin->w_cursor.col = new_cursor_col;
1763 i = (int)curwin->w_virtcol - vcol;
1764 ptr = alloc(i + 1);
1765 if (ptr != NULL)
1766 {
1767 new_cursor_col += i;
1768 ptr[i] = NUL;
1769 while (--i >= 0)
1770 ptr[i] = ' ';
1771 ins_str(ptr);
1772 vim_free(ptr);
1773 }
1774 }
1775
1776 /*
1777 * When changing the indent while the cursor is in it, reset
1778 * Insstart_col to 0.
1779 */
1780 insstart_less = MAXCOL;
1781 }
1782
1783 curwin->w_p_list = save_p_list;
1784
1785 if (new_cursor_col <= 0)
1786 curwin->w_cursor.col = 0;
1787 else
1788 curwin->w_cursor.col = new_cursor_col;
1789 curwin->w_set_curswant = TRUE;
1790 changed_cline_bef_curs();
1791
1792 /*
1793 * May have to adjust the start of the insert.
1794 */
1795 if (State & INSERT)
1796 {
1797 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1798 {
1799 if ((int)Insstart.col <= insstart_less)
1800 Insstart.col = 0;
1801 else
1802 Insstart.col -= insstart_less;
1803 }
1804 if ((int)ai_col <= insstart_less)
1805 ai_col = 0;
1806 else
1807 ai_col -= insstart_less;
1808 }
1809
1810 /*
1811 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1812 * If the number of characters before the cursor decreased, need to pop a
1813 * few characters from the replace stack.
1814 * If the number of characters before the cursor increased, need to push a
1815 * few NULs onto the replace stack.
1816 */
1817 if (REPLACE_NORMAL(State) && start_col >= 0)
1818 {
1819 while (start_col > (int)curwin->w_cursor.col)
1820 {
1821 replace_join(0); /* remove a NUL from the replace stack */
1822 --start_col;
1823 }
1824 while (start_col < (int)curwin->w_cursor.col || replaced)
1825 {
1826 replace_push(NUL);
1827 if (replaced)
1828 {
1829 replace_push(replaced);
1830 replaced = NUL;
1831 }
1832 ++start_col;
1833 }
1834 }
1835
1836#ifdef FEAT_VREPLACE
1837 /*
1838 * For VREPLACE mode, we also have to fix the replace stack. In this case
1839 * it is always possible because we backspace over the whole line and then
1840 * put it back again the way we wanted it.
1841 */
1842 if (State & VREPLACE_FLAG)
1843 {
1844 /* If orig_line didn't allocate, just return. At least we did the job,
1845 * even if you can't backspace. */
1846 if (orig_line == NULL)
1847 return;
1848
1849 /* Save new line */
1850 new_line = vim_strsave(ml_get_curline());
1851 if (new_line == NULL)
1852 return;
1853
1854 /* We only put back the new line up to the cursor */
1855 new_line[curwin->w_cursor.col] = NUL;
1856
1857 /* Put back original line */
1858 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1859 curwin->w_cursor.col = orig_col;
1860
1861 /* Backspace from cursor to start of line */
1862 backspace_until_column(0);
1863
1864 /* Insert new stuff into line again */
1865 ins_bytes(new_line);
1866
1867 vim_free(new_line);
1868 }
1869#endif
1870}
1871
1872/*
1873 * Truncate the space at the end of a line. This is to be used only in an
1874 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1875 * modes.
1876 */
1877 void
1878truncate_spaces(line)
1879 char_u *line;
1880{
1881 int i;
1882
1883 /* find start of trailing white space */
1884 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1885 {
1886 if (State & REPLACE_FLAG)
1887 replace_join(0); /* remove a NUL from the replace stack */
1888 }
1889 line[i + 1] = NUL;
1890}
1891
1892#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1893 || defined(FEAT_COMMENTS) || defined(PROTO)
1894/*
1895 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1896 * modes correctly. May also be used when not in insert mode at all.
1897 */
1898 void
1899backspace_until_column(col)
1900 int col;
1901{
1902 while ((int)curwin->w_cursor.col > col)
1903 {
1904 curwin->w_cursor.col--;
1905 if (State & REPLACE_FLAG)
1906 replace_do_bs();
1907 else
1908 (void)del_char(FALSE);
1909 }
1910}
1911#endif
1912
1913#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1914/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001915 * CTRL-X pressed in Insert mode.
1916 */
1917 static void
1918ins_ctrl_x()
1919{
1920 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1921 * CTRL-V works like CTRL-N */
1922 if (ctrl_x_mode != CTRL_X_CMDLINE)
1923 {
1924 /* if the next ^X<> won't ADD nothing, then reset
1925 * compl_cont_status */
1926 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001927 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001928 else
1929 compl_cont_status = 0;
1930 /* We're not sure which CTRL-X mode it will be yet */
1931 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1932 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1933 edit_submode_pre = NULL;
1934 showmode();
1935 }
1936}
1937
1938/*
1939 * Return TRUE if the 'dict' or 'tsr' option can be used.
1940 */
1941 static int
1942has_compl_option(dict_opt)
1943 int dict_opt;
1944{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001945 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001946# ifdef FEAT_SPELL
1947 && !curwin->w_p_spell
1948# endif
1949 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001950 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1951 {
1952 ctrl_x_mode = 0;
1953 edit_submode = NULL;
1954 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1955 : (char_u *)_("'thesaurus' option is empty"),
1956 hl_attr(HLF_E));
1957 if (emsg_silent == 0)
1958 {
1959 vim_beep();
1960 setcursor();
1961 out_flush();
1962 ui_delay(2000L, FALSE);
1963 }
1964 return FALSE;
1965 }
1966 return TRUE;
1967}
1968
1969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1971 * This depends on the current mode.
1972 */
1973 int
1974vim_is_ctrl_x_key(c)
1975 int c;
1976{
1977 /* Always allow ^R - let it's results then be checked */
1978 if (c == Ctrl_R)
1979 return TRUE;
1980
Bram Moolenaare3226be2005-12-18 22:10:00 +00001981 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001982 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001983 return TRUE;
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 switch (ctrl_x_mode)
1986 {
1987 case 0: /* Not in any CTRL-X mode */
1988 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1989 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001990 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1992 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1993 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001994 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1995 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 case CTRL_X_SCROLL:
1997 return (c == Ctrl_Y || c == Ctrl_E);
1998 case CTRL_X_WHOLE_LINE:
1999 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2000 case CTRL_X_FILES:
2001 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2002 case CTRL_X_DICTIONARY:
2003 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2004 case CTRL_X_THESAURUS:
2005 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2006 case CTRL_X_TAGS:
2007 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2008#ifdef FEAT_FIND_ID
2009 case CTRL_X_PATH_PATTERNS:
2010 return (c == Ctrl_P || c == Ctrl_N);
2011 case CTRL_X_PATH_DEFINES:
2012 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2013#endif
2014 case CTRL_X_CMDLINE:
2015 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2016 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002017#ifdef FEAT_COMPL_FUNC
2018 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002019 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002020 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002021 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002022#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002023 case CTRL_X_SPELL:
2024 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 EMSG(_(e_internal));
2027 return FALSE;
2028}
2029
2030/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002031 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 * case of the originally typed text is used, and the case of the completed
2033 * text is infered, ie this tries to work out what case you probably wanted
2034 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002035 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 */
2037 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002038ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 char_u *str;
2040 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002041 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 char_u *fname;
2043 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002044 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 int has_lower = FALSE;
2047 int was_letter = FALSE;
2048 int idx;
2049
2050 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2051 {
2052 /* Infer case of completed part -- webb */
2053 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002054 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002057 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002059 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 has_lower = TRUE;
2062 if (isupper(IObuff[idx]))
2063 {
2064 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002065 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2067 break;
2068 }
2069 }
2070 }
2071
2072 /*
2073 * Rule 2: No lower case, 2nd consecutive letter converted to
2074 * upper case.
2075 */
2076 if (!has_lower)
2077 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002078 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002080 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 && islower(IObuff[idx]))
2082 {
2083 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002084 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2086 break;
2087 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002088 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 }
2091
2092 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002093 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002095 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2096 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002098 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099}
2100
2101/*
2102 * Add a match to the list of matches.
2103 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002104 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002105 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002107 static int
2108ins_compl_add(str, len, icase, fname, cptext, cdir, flags, dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 char_u *str;
2110 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002111 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002113 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002114 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002115 int flags;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002116 int dup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002118 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002119 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 ui_breakcheck();
2122 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (len < 0)
2125 len = (int)STRLEN(str);
2126
2127 /*
2128 * If the same match is already present, don't add it.
2129 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002130 if (compl_first_match != NULL && !dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002132 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 do
2134 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002136 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002137 && match->cp_str[len] == NUL)
2138 return NOTDONE;
2139 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002140 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002143 /* Remove any popup menu before changing the list of matches. */
2144 ins_compl_del_pum();
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 /*
2147 * Allocate a new match structure.
2148 * Copy the values to the new match structure.
2149 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002150 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002152 return FAIL;
2153 match->cp_number = -1;
2154 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002155 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002156 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002161 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002164 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2166 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002167 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002168 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002169 && compl_curr_match->cp_fname != NULL
2170 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002171 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002172 else if (fname != NULL)
2173 {
2174 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002175 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002178 match->cp_fname = NULL;
2179 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002180
2181 if (cptext != NULL)
2182 {
2183 int i;
2184
2185 for (i = 0; i < CPT_COUNT; ++i)
2186 if (cptext[i] != NULL && *cptext[i] != NUL)
2187 match->cp_text[i] = vim_strsave(cptext[i]);
2188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190 /*
2191 * Link the new match structure in the list of matches.
2192 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002193 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002194 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 else if (dir == FORWARD)
2196 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002197 match->cp_next = compl_curr_match->cp_next;
2198 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200 else /* BACKWARD */
2201 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002202 match->cp_next = compl_curr_match;
2203 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002205 if (match->cp_next)
2206 match->cp_next->cp_prev = match;
2207 if (match->cp_prev)
2208 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002210 compl_first_match = match;
2211 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002213 /*
2214 * Find the longest common string if still doing that.
2215 */
2216 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2217 ins_compl_longest_match(match);
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return OK;
2220}
2221
2222/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002223 * Return TRUE if "str[len]" matches with match->cp_str, considering
2224 * match->cp_icase.
2225 */
2226 static int
2227ins_compl_equal(match, str, len)
2228 compl_T *match;
2229 char_u *str;
2230 int len;
2231{
2232 if (match->cp_icase)
2233 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2234 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2235}
2236
2237/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002238 * Reduce the longest common string for match "match".
2239 */
2240 static void
2241ins_compl_longest_match(match)
2242 compl_T *match;
2243{
2244 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002245 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002246 int had_match;
2247
2248 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002249 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002250 /* First match, use it as a whole. */
2251 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002252 if (compl_leader != NULL)
2253 {
2254 had_match = (curwin->w_cursor.col > compl_col);
2255 ins_compl_delete();
2256 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2257 ins_redraw(FALSE);
2258
2259 /* When the match isn't there (to avoid matching itself) remove it
2260 * again after redrawing. */
2261 if (!had_match)
2262 ins_compl_delete();
2263 compl_used_match = FALSE;
2264 }
2265 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002266 else
2267 {
2268 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002269 p = compl_leader;
2270 s = match->cp_str;
2271 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002272 {
2273#ifdef FEAT_MBYTE
2274 if (has_mbyte)
2275 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002276 c1 = mb_ptr2char(p);
2277 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002278 }
2279 else
2280#endif
2281 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002282 c1 = *p;
2283 c2 = *s;
2284 }
2285 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2286 : (c1 != c2))
2287 break;
2288#ifdef FEAT_MBYTE
2289 if (has_mbyte)
2290 {
2291 mb_ptr_adv(p);
2292 mb_ptr_adv(s);
2293 }
2294 else
2295#endif
2296 {
2297 ++p;
2298 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002299 }
2300 }
2301
2302 if (*p != NUL)
2303 {
2304 /* Leader was shortened, need to change the inserted text. */
2305 *p = NUL;
2306 had_match = (curwin->w_cursor.col > compl_col);
2307 ins_compl_delete();
2308 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2309 ins_redraw(FALSE);
2310
2311 /* When the match isn't there (to avoid matching itself) remove it
2312 * again after redrawing. */
2313 if (!had_match)
2314 ins_compl_delete();
2315 }
2316
2317 compl_used_match = FALSE;
2318 }
2319}
2320
2321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 * Add an array of matches to the list of matches.
2323 * Frees matches[].
2324 */
2325 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002326ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int num_matches;
2328 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002329 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 int i;
2332 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002333 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar572cb562005-08-05 21:35:02 +00002335 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002336 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002337 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002339 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 FreeWild(num_matches, matches);
2341}
2342
2343/* Make the completion list cyclic.
2344 * Return the number of matches (excluding the original).
2345 */
2346 static int
2347ins_compl_make_cyclic()
2348{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002349 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 int count = 0;
2351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002352 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 /*
2355 * Find the end of the list.
2356 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002357 match = compl_first_match;
2358 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002359 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002361 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 ++count;
2363 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002364 match->cp_next = compl_first_match;
2365 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 return count;
2368}
2369
Bram Moolenaara94bc432006-03-10 21:42:59 +00002370/*
2371 * Start completion for the complete() function.
2372 * "startcol" is where the matched text starts (1 is first column).
2373 * "list" is the list of matches.
2374 */
2375 void
2376set_completion(startcol, list)
2377 int startcol;
2378 list_T *list;
2379{
2380 /* If already doing completions stop it. */
2381 if (ctrl_x_mode != 0)
2382 ins_compl_prep(' ');
2383 ins_compl_clear();
2384
2385 if (stop_arrow() == FAIL)
2386 return;
2387
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002388 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002389 startcol = curwin->w_cursor.col;
2390 compl_col = startcol;
2391 compl_length = curwin->w_cursor.col - startcol;
2392 /* compl_pattern doesn't need to be set */
2393 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2394 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002395 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002396 return;
2397
2398 /* Handle like dictionary completion. */
2399 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2400
2401 ins_compl_add_list(list);
2402 compl_matches = ins_compl_make_cyclic();
2403 compl_started = TRUE;
2404 compl_used_match = TRUE;
2405
2406 compl_curr_match = compl_first_match;
2407 ins_complete(Ctrl_N);
2408 out_flush();
2409}
2410
2411
Bram Moolenaar9372a112005-12-06 19:59:18 +00002412/* "compl_match_array" points the currently displayed list of entries in the
2413 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002414static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002415static int compl_match_arraysize;
2416
2417/*
2418 * Update the screen and when there is any scrolling remove the popup menu.
2419 */
2420 static void
2421ins_compl_upd_pum()
2422{
2423 int h;
2424
2425 if (compl_match_array != NULL)
2426 {
2427 h = curwin->w_cline_height;
2428 update_screen(0);
2429 if (h != curwin->w_cline_height)
2430 ins_compl_del_pum();
2431 }
2432}
2433
2434/*
2435 * Remove any popup menu.
2436 */
2437 static void
2438ins_compl_del_pum()
2439{
2440 if (compl_match_array != NULL)
2441 {
2442 pum_undisplay();
2443 vim_free(compl_match_array);
2444 compl_match_array = NULL;
2445 }
2446}
2447
2448/*
2449 * Return TRUE if the popup menu should be displayed.
2450 */
2451 static int
2452pum_wanted()
2453{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002454 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002455 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002456 return FALSE;
2457
2458 /* The display looks bad on a B&W display. */
2459 if (t_colors < 8
2460#ifdef FEAT_GUI
2461 && !gui.in_use
2462#endif
2463 )
2464 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002465 return TRUE;
2466}
2467
2468/*
2469 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002470 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002471 */
2472 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002473pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002474{
2475 compl_T *compl;
2476 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477
2478 /* Don't display the popup menu if there are no matches or there is only
2479 * one (ignoring the original text). */
2480 compl = compl_first_match;
2481 i = 0;
2482 do
2483 {
2484 if (compl == NULL
2485 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2486 break;
2487 compl = compl->cp_next;
2488 } while (compl != compl_first_match);
2489
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002490 if (strstr((char *)p_cot, "menuone") != NULL)
2491 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002492 return (i >= 2);
2493}
2494
2495/*
2496 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002497 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002498 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002499 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002500ins_compl_show_pum()
2501{
2502 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002503 compl_T *shown_compl = NULL;
2504 int did_find_shown_match = FALSE;
2505 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002506 int i;
2507 int cur = -1;
2508 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002509 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002511 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002512 return;
2513
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002514#if defined(FEAT_EVAL)
2515 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2516 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2517#endif
2518
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002519 /* Update the screen before drawing the popup menu over it. */
2520 update_screen(0);
2521
2522 if (compl_match_array == NULL)
2523 {
2524 /* Need to build the popup menu list. */
2525 compl_match_arraysize = 0;
2526 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002527 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002528 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002529 do
2530 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002531 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2532 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002534 ++compl_match_arraysize;
2535 compl = compl->cp_next;
2536 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002537 if (compl_match_arraysize == 0)
2538 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 compl_match_array = (pumitem_T *)alloc_clear(
2540 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002541 * compl_match_arraysize));
2542 if (compl_match_array != NULL)
2543 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002544 /* If the current match is the original text don't find the first
2545 * match after it, don't highlight anything. */
2546 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2547 shown_match_ok = TRUE;
2548
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002549 i = 0;
2550 compl = compl_first_match;
2551 do
2552 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002553 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2554 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002555 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002556 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 if (!shown_match_ok)
2558 {
2559 if (compl == compl_shown_match || did_find_shown_match)
2560 {
2561 /* This item is the shown match or this is the
2562 * first displayed item after the shown match. */
2563 compl_shown_match = compl;
2564 did_find_shown_match = TRUE;
2565 shown_match_ok = TRUE;
2566 }
2567 else
2568 /* Remember this displayed match for when the
2569 * shown match is just below it. */
2570 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002571 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002572 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002573
2574 if (compl->cp_text[CPT_ABBR] != NULL)
2575 compl_match_array[i].pum_text =
2576 compl->cp_text[CPT_ABBR];
2577 else
2578 compl_match_array[i].pum_text = compl->cp_str;
2579 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2580 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2581 if (compl->cp_text[CPT_MENU] != NULL)
2582 compl_match_array[i++].pum_extra =
2583 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002584 else
2585 compl_match_array[i++].pum_extra = compl->cp_fname;
2586 }
2587
2588 if (compl == compl_shown_match)
2589 {
2590 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002591
2592 /* When the original text is the shown match don't set
2593 * compl_shown_match. */
2594 if (compl->cp_flags & ORIGINAL_TEXT)
2595 shown_match_ok = TRUE;
2596
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002597 if (!shown_match_ok && shown_compl != NULL)
2598 {
2599 /* The shown match isn't displayed, set it to the
2600 * previously displayed match. */
2601 compl_shown_match = shown_compl;
2602 shown_match_ok = TRUE;
2603 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002604 }
2605 compl = compl->cp_next;
2606 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002607
2608 if (!shown_match_ok) /* no displayed match at all */
2609 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002610 }
2611 }
2612 else
2613 {
2614 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002615 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002616 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2617 || compl_match_array[i].pum_text
2618 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002619 {
2620 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002621 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002622 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002623 }
2624
2625 if (compl_match_array != NULL)
2626 {
2627 /* Compute the screen column of the start of the completed text.
2628 * Use the cursor to get all wrapping and other settings right. */
2629 col = curwin->w_cursor.col;
2630 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002631 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002632 curwin->w_cursor.col = col;
2633 }
2634}
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#define DICT_FIRST (1) /* use just first element in "dict" */
2637#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002640 * Add any identifiers that match the given pattern in the list of dictionary
2641 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 */
2643 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002644ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2645 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002647 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002648 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002650 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 char_u *ptr;
2652 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 char_u **files;
2655 int count;
2656 int i;
2657 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002658 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaar0b238792006-03-02 22:49:12 +00002660 if (*dict == NUL)
2661 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002662#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002663 /* When 'dictionary' is empty and spell checking is enabled use
2664 * "spell". */
2665 if (!thesaurus && curwin->w_p_spell)
2666 dict = (char_u *)"spell";
2667 else
2668#endif
2669 return;
2670 }
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002673 if (buf == NULL)
2674 return;
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 /* If 'infercase' is set, don't use 'smartcase' here */
2677 save_p_scs = p_scs;
2678 if (curbuf->b_p_inf)
2679 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002680
2681 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2682 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002683 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002684 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2685 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002686 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2687
2688 if (pat_esc == NULL)
2689 return ;
2690 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002691 ptr = alloc(i);
2692 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002693 {
2694 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002695 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002696 }
2697 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2698 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2699 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002700 vim_free(ptr);
2701 }
2702 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002703 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002704 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002705 if (regmatch.regprog == NULL)
2706 goto theend;
2707 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2710 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002711 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
2713 /* copy one dictionary file name into buf */
2714 if (flags == DICT_EXACT)
2715 {
2716 count = 1;
2717 files = &dict;
2718 }
2719 else
2720 {
2721 /* Expand wildcards in the dictionary name, but do not allow
2722 * backticks (for security, the 'dict' option may have been set in
2723 * a modeline). */
2724 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002725# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002726 if (!thesaurus && STRCMP(buf, "spell") == 0)
2727 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002728 else
2729# endif
2730 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 || expand_wildcards(1, &buf, &count, &files,
2732 EW_FILE|EW_SILENT) != OK)
2733 count = 0;
2734 }
2735
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002736# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002737 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002739 /* Complete from active spelling. Skip "\<" in the pattern, we
2740 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002741 if (pat[0] == '\\' && pat[1] == '<')
2742 ptr = pat + 2;
2743 else
2744 ptr = pat;
2745 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002747 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002748# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002749 {
2750 ins_compl_files(count, files, thesaurus, flags,
2751 &regmatch, buf, &dir);
2752 if (flags != DICT_EXACT)
2753 FreeWild(count, files);
2754 }
2755 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 break;
2757 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002758
2759theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 p_scs = save_p_scs;
2761 vim_free(regmatch.regprog);
2762 vim_free(buf);
2763}
2764
Bram Moolenaar0b238792006-03-02 22:49:12 +00002765 static void
2766ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2767 int count;
2768 char_u **files;
2769 int thesaurus;
2770 int flags;
2771 regmatch_T *regmatch;
2772 char_u *buf;
2773 int *dir;
2774{
2775 char_u *ptr;
2776 int i;
2777 FILE *fp;
2778 int add_r;
2779
2780 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2781 {
2782 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2783 if (flags != DICT_EXACT)
2784 {
2785 vim_snprintf((char *)IObuff, IOSIZE,
2786 _("Scanning dictionary: %s"), (char *)files[i]);
2787 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2788 }
2789
2790 if (fp != NULL)
2791 {
2792 /*
2793 * Read dictionary file line by line.
2794 * Check each line for a match.
2795 */
2796 while (!got_int && !compl_interrupted
2797 && !vim_fgets(buf, LSIZE, fp))
2798 {
2799 ptr = buf;
2800 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2801 {
2802 ptr = regmatch->startp[0];
2803 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2804 ptr = find_line_end(ptr);
2805 else
2806 ptr = find_word_end(ptr);
2807 add_r = ins_compl_add_infercase(regmatch->startp[0],
2808 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002809 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002810 if (thesaurus)
2811 {
2812 char_u *wstart;
2813
2814 /*
2815 * Add the other matches on the line
2816 */
2817 while (!got_int)
2818 {
2819 /* Find start of the next word. Skip white
2820 * space and punctuation. */
2821 ptr = find_word_start(ptr);
2822 if (*ptr == NUL || *ptr == NL)
2823 break;
2824 wstart = ptr;
2825
2826 /* Find end of the word and add it. */
2827#ifdef FEAT_MBYTE
2828 if (has_mbyte)
2829 /* Japanese words may have characters in
2830 * different classes, only separate words
2831 * with single-byte non-word characters. */
2832 while (*ptr != NUL)
2833 {
2834 int l = (*mb_ptr2len)(ptr);
2835
2836 if (l < 2 && !vim_iswordc(*ptr))
2837 break;
2838 ptr += l;
2839 }
2840 else
2841#endif
2842 ptr = find_word_end(ptr);
2843 add_r = ins_compl_add_infercase(wstart,
2844 (int)(ptr - wstart),
2845 p_ic, files[i], *dir, 0);
2846 }
2847 }
2848 if (add_r == OK)
2849 /* if dir was BACKWARD then honor it just once */
2850 *dir = FORWARD;
2851 else if (add_r == FAIL)
2852 break;
2853 /* avoid expensive call to vim_regexec() when at end
2854 * of line */
2855 if (*ptr == '\n' || got_int)
2856 break;
2857 }
2858 line_breakcheck();
2859 ins_compl_check_keys(50);
2860 }
2861 fclose(fp);
2862 }
2863 }
2864}
2865
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866/*
2867 * Find the start of the next word.
2868 * Returns a pointer to the first char of the word. Also stops at a NUL.
2869 */
2870 char_u *
2871find_word_start(ptr)
2872 char_u *ptr;
2873{
2874#ifdef FEAT_MBYTE
2875 if (has_mbyte)
2876 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002877 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else
2879#endif
2880 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2881 ++ptr;
2882 return ptr;
2883}
2884
2885/*
2886 * Find the end of the word. Assumes it starts inside a word.
2887 * Returns a pointer to just after the word.
2888 */
2889 char_u *
2890find_word_end(ptr)
2891 char_u *ptr;
2892{
2893#ifdef FEAT_MBYTE
2894 int start_class;
2895
2896 if (has_mbyte)
2897 {
2898 start_class = mb_get_class(ptr);
2899 if (start_class > 1)
2900 while (*ptr != NUL)
2901 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002902 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (mb_get_class(ptr) != start_class)
2904 break;
2905 }
2906 }
2907 else
2908#endif
2909 while (vim_iswordc(*ptr))
2910 ++ptr;
2911 return ptr;
2912}
2913
2914/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002915 * Find the end of the line, omitting CR and NL at the end.
2916 * Returns a pointer to just after the line.
2917 */
2918 static char_u *
2919find_line_end(ptr)
2920 char_u *ptr;
2921{
2922 char_u *s;
2923
2924 s = ptr + STRLEN(ptr);
2925 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2926 --s;
2927 return s;
2928}
2929
2930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 * Free the list of completions
2932 */
2933 static void
2934ins_compl_free()
2935{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002936 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002937 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002939 vim_free(compl_pattern);
2940 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002941 vim_free(compl_leader);
2942 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002944 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002946
2947 ins_compl_del_pum();
2948 pum_clear();
2949
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002950 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 do
2952 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002953 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002954 compl_curr_match = compl_curr_match->cp_next;
2955 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002957 if (match->cp_flags & FREE_FNAME)
2958 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002959 for (i = 0; i < CPT_COUNT; ++i)
2960 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002962 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2963 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966 static void
2967ins_compl_clear()
2968{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002969 compl_cont_status = 0;
2970 compl_started = FALSE;
2971 compl_matches = 0;
2972 vim_free(compl_pattern);
2973 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002974 vim_free(compl_leader);
2975 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002977 vim_free(compl_orig_text);
2978 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002979 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980}
2981
2982/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002983 * Return TRUE when Insert completion is active.
2984 */
2985 int
2986ins_compl_active()
2987{
2988 return compl_started;
2989}
2990
2991/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002992 * Delete one character before the cursor and show the subset of the matches
2993 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002994 * Returns the character to be used, NUL if the work is done and another char
2995 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00002996 */
2997 static int
2998ins_compl_bs()
2999{
3000 char_u *line;
3001 char_u *p;
3002
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003003 line = ml_get_curline();
3004 p = line + curwin->w_cursor.col;
3005 mb_ptr_back(line, p);
3006
3007 /* Stop completion when the whole word was deleted. */
3008 if ((int)(p - line) - (int)compl_col <= 0)
3009 return K_BS;
3010
Bram Moolenaara6557602006-02-04 22:43:20 +00003011 if (curwin->w_cursor.col <= compl_col + compl_length)
3012 {
3013 /* Deleted more than what was used to find matches, need to look for
3014 * matches all over again. */
3015 ins_compl_free();
3016 compl_started = FALSE;
3017 compl_matches = 0;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003018 compl_cont_status = 0;
3019 compl_cont_mode = 0;
Bram Moolenaara6557602006-02-04 22:43:20 +00003020 }
3021
Bram Moolenaara6557602006-02-04 22:43:20 +00003022 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003023 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003024 if (compl_leader != NULL)
3025 {
3026 ins_compl_del_pum();
3027 ins_compl_delete();
3028 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3029
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003030 if (compl_started)
3031 ins_compl_set_original_text(compl_leader);
3032 else
Bram Moolenaara6557602006-02-04 22:43:20 +00003033 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003034#ifdef FEAT_SPELL
3035 spell_bad_len = 0; /* need to redetect bad word */
3036#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003037 /* Matches were cleared, need to search for them now. */
3038 if (ins_complete(Ctrl_N) == FAIL)
3039 compl_cont_status = 0;
3040 else
3041 {
3042 /* Remove the completed word again. */
3043 ins_compl_delete();
3044 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3045 }
3046 }
3047
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003048 /* Go to the original text, since none of the matches is inserted. */
3049 if (compl_first_match->cp_prev != NULL
3050 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3051 compl_shown_match = compl_first_match->cp_prev;
3052 else
3053 compl_shown_match = compl_first_match;
3054 compl_curr_match = compl_shown_match;
3055 compl_shows_dir = compl_direction;
3056
Bram Moolenaara6557602006-02-04 22:43:20 +00003057 /* Show the popup menu with a different set of matches. */
3058 ins_compl_show_pum();
3059 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003060 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003061
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003062 return NUL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003063 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003064 return K_BS;
Bram Moolenaara6557602006-02-04 22:43:20 +00003065}
3066
3067/*
3068 * Append one character to the match leader. May reduce the number of
3069 * matches.
3070 */
3071 static void
3072ins_compl_addleader(c)
3073 int c;
3074{
3075#ifdef FEAT_MBYTE
3076 int cc;
3077
3078 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3079 {
3080 char_u buf[MB_MAXBYTES + 1];
3081
3082 (*mb_char2bytes)(c, buf);
3083 buf[cc] = NUL;
3084 ins_char_bytes(buf, cc);
3085 }
3086 else
3087#endif
3088 ins_char(c);
3089
3090 vim_free(compl_leader);
3091 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3092 curwin->w_cursor.col - compl_col);
3093 if (compl_leader != NULL)
3094 {
3095 /* Show the popup menu with a different set of matches. */
3096 ins_compl_del_pum();
3097 ins_compl_show_pum();
3098 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003099 compl_enter_selects = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003100 ins_compl_set_original_text(compl_leader);
3101 }
3102}
3103
3104/*
3105 * Set the first match, the original text.
3106 */
3107 static void
3108ins_compl_set_original_text(str)
3109 char_u *str;
3110{
3111 char_u *p;
3112
3113 /* Replace the original text entry. */
3114 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3115 {
3116 p = vim_strsave(str);
3117 if (p != NULL)
3118 {
3119 vim_free(compl_first_match->cp_str);
3120 compl_first_match->cp_str = p;
3121 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003122 }
3123}
3124
3125/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003126 * Append one character to the match leader. May reduce the number of
3127 * matches.
3128 */
3129 static void
3130ins_compl_addfrommatch()
3131{
3132 char_u *p;
3133 int len = curwin->w_cursor.col - compl_col;
3134 int c;
3135
3136 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003137 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003138 return;
3139 p += len;
3140#ifdef FEAT_MBYTE
3141 c = mb_ptr2char(p);
3142#else
3143 c = *p;
3144#endif
3145 ins_compl_addleader(c);
3146}
3147
3148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003150 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003153 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154ins_compl_prep(c)
3155 int c;
3156{
3157 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int temp;
3159 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003160 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 /* Forget any previous 'special' messages if this is actually
3163 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3164 */
3165 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3166 edit_submode_extra = NULL;
3167
3168 /* Ignore end of Select mode mapping */
3169 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003170 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003172 /* Set "compl_get_longest" when finding the first matches. */
3173 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3174 || (ctrl_x_mode == 0 && !compl_started))
3175 {
3176 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3177 compl_used_match = TRUE;
3178 }
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3181 {
3182 /*
3183 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3184 * it will be yet. Now we decide.
3185 */
3186 switch (c)
3187 {
3188 case Ctrl_E:
3189 case Ctrl_Y:
3190 ctrl_x_mode = CTRL_X_SCROLL;
3191 if (!(State & REPLACE_FLAG))
3192 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3193 else
3194 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3195 edit_submode_pre = NULL;
3196 showmode();
3197 break;
3198 case Ctrl_L:
3199 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3200 break;
3201 case Ctrl_F:
3202 ctrl_x_mode = CTRL_X_FILES;
3203 break;
3204 case Ctrl_K:
3205 ctrl_x_mode = CTRL_X_DICTIONARY;
3206 break;
3207 case Ctrl_R:
3208 /* Simply allow ^R to happen without affecting ^X mode */
3209 break;
3210 case Ctrl_T:
3211 ctrl_x_mode = CTRL_X_THESAURUS;
3212 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003213#ifdef FEAT_COMPL_FUNC
3214 case Ctrl_U:
3215 ctrl_x_mode = CTRL_X_FUNCTION;
3216 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003217 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003218 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003219 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003220#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003221 case 's':
3222 case Ctrl_S:
3223 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003224#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003225 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003226 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003227 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003228#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 case Ctrl_RSB:
3231 ctrl_x_mode = CTRL_X_TAGS;
3232 break;
3233#ifdef FEAT_FIND_ID
3234 case Ctrl_I:
3235 case K_S_TAB:
3236 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3237 break;
3238 case Ctrl_D:
3239 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3240 break;
3241#endif
3242 case Ctrl_V:
3243 case Ctrl_Q:
3244 ctrl_x_mode = CTRL_X_CMDLINE;
3245 break;
3246 case Ctrl_P:
3247 case Ctrl_N:
3248 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3249 * just started ^X mode, or there were enough ^X's to cancel
3250 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3251 * do normal expansion when interrupting a different mode (say
3252 * ^X^F^X^P or ^P^X^X^P, see below)
3253 * nothing changes if interrupting mode 0, (eg, the flag
3254 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003255 if (!(compl_cont_status & CONT_INTRPT))
3256 compl_cont_status |= CONT_LOCAL;
3257 else if (compl_cont_mode != 0)
3258 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 /* FALLTHROUGH */
3260 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003261 /* If we have typed at least 2 ^X's... for modes != 0, we set
3262 * compl_cont_status = 0 (eg, as if we had just started ^X
3263 * mode).
3264 * For mode 0, we set "compl_cont_mode" to an impossible
3265 * value, in both cases ^X^X can be used to restart the same
3266 * mode (avoiding ADDING mode).
3267 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3268 * 'complete' and local ^P expansions respectively.
3269 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3270 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (c == Ctrl_X)
3272 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003273 if (compl_cont_mode != 0)
3274 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003276 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 ctrl_x_mode = 0;
3279 edit_submode = NULL;
3280 showmode();
3281 break;
3282 }
3283 }
3284 else if (ctrl_x_mode != 0)
3285 {
3286 /* We're already in CTRL-X mode, do we stay in it? */
3287 if (!vim_is_ctrl_x_key(c))
3288 {
3289 if (ctrl_x_mode == CTRL_X_SCROLL)
3290 ctrl_x_mode = 0;
3291 else
3292 ctrl_x_mode = CTRL_X_FINISHED;
3293 edit_submode = NULL;
3294 }
3295 showmode();
3296 }
3297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003298 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 /* Show error message from attempted keyword completion (probably
3301 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003302 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003304 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3305 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 || ctrl_x_mode == CTRL_X_FINISHED)
3307 {
3308 /* Get here when we have finished typing a sequence of ^N and
3309 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003310 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003311 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003313 char_u *p;
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003316 * If any of the original typed text has been changed, eg when
3317 * ignorecase is set, we must add back-spaces to the redo
3318 * buffer. We add as few as necessary to delete just the part
3319 * of the original text that has changed.
3320 * When using the longest match, edited the match or used
3321 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003323 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3324 ptr = compl_curr_match->cp_str;
3325 else if (compl_leader != NULL)
3326 ptr = compl_leader;
3327 else
3328 ptr = compl_orig_text;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003329 p = compl_orig_text;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003330 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp]; ++temp)
3331 ;
3332#ifdef FEAT_MBYTE
3333 if (temp > 0)
3334 temp -= (*mb_head_off)(compl_orig_text, p + temp);
3335#endif
3336 for (p += temp; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 AppendCharToRedobuff(K_BS);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003338 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340
3341#ifdef FEAT_CINDENT
3342 want_cindent = (can_cindent && cindent_on());
3343#endif
3344 /*
3345 * When completing whole lines: fix indent for 'cindent'.
3346 * Otherwise, break line if it's too long.
3347 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003348 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350#ifdef FEAT_CINDENT
3351 /* re-indent the current line */
3352 if (want_cindent)
3353 {
3354 do_c_expr_indent();
3355 want_cindent = FALSE; /* don't do it again */
3356 }
3357#endif
3358 }
3359 else
3360 {
3361 /* put the cursor on the last char, for 'tw' formatting */
3362 curwin->w_cursor.col--;
3363 if (stop_arrow() == OK)
3364 insertchar(NUL, 0, -1);
3365 curwin->w_cursor.col++;
3366 }
3367
3368 auto_format(FALSE, TRUE);
3369
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003370 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003371 * the selection without inserting anything. When
3372 * compl_enter_selects is set the Enter key does the same. */
3373 if ((c == Ctrl_Y || (compl_enter_selects
3374 && (c == CAR || c == K_KENTER || c == NL)))
3375 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003376 retval = TRUE;
3377
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003378 /* CTRL-E means completion is Ended, go back to the typed text. */
3379 if (c == Ctrl_E)
3380 {
3381 ins_compl_delete();
3382 if (compl_leader != NULL)
3383 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3384 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003385 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003386 + curwin->w_cursor.col - compl_col);
3387 retval = TRUE;
3388 }
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003391 compl_started = FALSE;
3392 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 msg_clr_cmdline(); /* necessary for "noshowmode" */
3394 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003395 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (edit_submode != NULL)
3397 {
3398 edit_submode = NULL;
3399 showmode();
3400 }
3401
3402#ifdef FEAT_CINDENT
3403 /*
3404 * Indent now if a key was typed that is in 'cinkeys'.
3405 */
3406 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3407 do_c_expr_indent();
3408#endif
3409 }
3410 }
3411
3412 /* reset continue_* if we left expansion-mode, if we stay they'll be
3413 * (re)set properly in ins_complete() */
3414 if (!vim_is_ctrl_x_key(c))
3415 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003416 compl_cont_status = 0;
3417 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003419
3420 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421}
3422
3423/*
3424 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3425 * (depending on flag) starting from buf and looking for a non-scanned
3426 * buffer (other than curbuf). curbuf is special, if it is called with
3427 * buf=curbuf then it has to be the first call for a given flag/expansion.
3428 *
3429 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3430 */
3431 static buf_T *
3432ins_compl_next_buf(buf, flag)
3433 buf_T *buf;
3434 int flag;
3435{
3436#ifdef FEAT_WINDOWS
3437 static win_T *wp;
3438#endif
3439
3440 if (flag == 'w') /* just windows */
3441 {
3442#ifdef FEAT_WINDOWS
3443 if (buf == curbuf) /* first call for this flag/expansion */
3444 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003445 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 && wp->w_buffer->b_scanned)
3447 ;
3448 buf = wp->w_buffer;
3449#else
3450 buf = curbuf;
3451#endif
3452 }
3453 else
3454 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3455 * (unlisted buffers)
3456 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003457 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 && ((flag == 'U'
3459 ? buf->b_p_bl
3460 : (!buf->b_p_bl
3461 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003462 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 ;
3464 return buf;
3465}
3466
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003467#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003468static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003469
3470/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003471 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003472 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003473 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003474 static void
3475expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003476 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003477 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003478{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003479 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003480 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003481 char_u *funcname;
3482 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003483
Bram Moolenaare344bea2005-09-01 20:46:49 +00003484 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3485 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003486 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003487
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003488 /* Call 'completefunc' to obtain the list of matches. */
3489 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003490 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003491
Bram Moolenaare344bea2005-09-01 20:46:49 +00003492 pos = curwin->w_cursor;
3493 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3494 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003495 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003496 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003497
Bram Moolenaara94bc432006-03-10 21:42:59 +00003498 ins_compl_add_list(matchlist);
3499 list_unref(matchlist);
3500}
3501#endif /* FEAT_COMPL_FUNC */
3502
Bram Moolenaar39f05632006-03-19 22:15:26 +00003503#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003504/*
3505 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003506 */
3507 static void
3508ins_compl_add_list(list)
3509 list_T *list;
3510{
3511 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003512 int dir = compl_direction;
3513
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003514 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003515 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003516 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003517 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3518 /* if dir was BACKWARD then honor it just once */
3519 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003520 else if (did_emsg)
3521 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003522 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003523}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003524
3525/*
3526 * Add a match to the list of matches from a typeval_T.
3527 * If the given string is already in the list of completions, then return
3528 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3529 * maybe because alloc() returns NULL, then FAIL is returned.
3530 */
3531 int
3532ins_compl_add_tv(tv, dir)
3533 typval_T *tv;
3534 int dir;
3535{
3536 char_u *word;
3537 int icase = p_ic;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003538 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003539 char_u *(cptext[CPT_COUNT]);
3540
3541 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3542 {
3543 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3544 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3545 (char_u *)"abbr", FALSE);
3546 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3547 (char_u *)"menu", FALSE);
3548 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3549 (char_u *)"kind", FALSE);
3550 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3551 (char_u *)"info", FALSE);
3552 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3553 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003554 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3555 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003556 }
3557 else
3558 {
3559 word = get_tv_string_chk(tv);
3560 vim_memset(cptext, 0, sizeof(cptext));
3561 }
3562 if (word == NULL || *word == NUL)
3563 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003564 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003565}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003566#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003567
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003568/*
3569 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003570 * The search starts at position "ini" in curbuf and in the direction
3571 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003572 * When "compl_started" is FALSE start at that position, otherwise continue
3573 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003574 * This may return before finding all the matches.
3575 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 */
3577 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003578ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
3581 static pos_T first_match_pos;
3582 static pos_T last_match_pos;
3583 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003584 static int found_all = FALSE; /* Found all matches of a
3585 certain type. */
3586 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
Bram Moolenaar572cb562005-08-05 21:35:02 +00003588 pos_T *pos;
3589 char_u **matches;
3590 int save_p_scs;
3591 int save_p_ws;
3592 int save_p_ic;
3593 int i;
3594 int num_matches;
3595 int len;
3596 int found_new_match;
3597 int type = ctrl_x_mode;
3598 char_u *ptr;
3599 char_u *dict = NULL;
3600 int dict_f = 0;
3601 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003603 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3606 ins_buf->b_scanned = 0;
3607 found_all = FALSE;
3608 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003610 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 last_match_pos = first_match_pos = *ini;
3612 }
3613
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003615 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3617 for (;;)
3618 {
3619 found_new_match = FAIL;
3620
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 * or if found_all says this entry is done. For ^X^L only use the
3623 * entries from 'complete' that look in loaded buffers. */
3624 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 found_all = FALSE;
3628 while (*e_cpt == ',' || *e_cpt == ' ')
3629 e_cpt++;
3630 if (*e_cpt == '.' && !curbuf->b_scanned)
3631 {
3632 ins_buf = curbuf;
3633 first_match_pos = *ini;
3634 /* So that ^N can match word immediately after cursor */
3635 if (ctrl_x_mode == 0)
3636 dec(&first_match_pos);
3637 last_match_pos = first_match_pos;
3638 type = 0;
3639 }
3640 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3641 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3642 {
3643 /* Scan a buffer, but not the current one. */
3644 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3645 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003646 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 first_match_pos.col = last_match_pos.col = 0;
3648 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3649 last_match_pos.lnum = 0;
3650 type = 0;
3651 }
3652 else /* unloaded buffer, scan like dictionary */
3653 {
3654 found_all = TRUE;
3655 if (ins_buf->b_fname == NULL)
3656 continue;
3657 type = CTRL_X_DICTIONARY;
3658 dict = ins_buf->b_fname;
3659 dict_f = DICT_EXACT;
3660 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003661 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 ins_buf->b_fname == NULL
3663 ? buf_spname(ins_buf)
3664 : ins_buf->b_sfname == NULL
3665 ? (char *)ins_buf->b_fname
3666 : (char *)ins_buf->b_sfname);
3667 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3668 }
3669 else if (*e_cpt == NUL)
3670 break;
3671 else
3672 {
3673 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3674 type = -1;
3675 else if (*e_cpt == 'k' || *e_cpt == 's')
3676 {
3677 if (*e_cpt == 'k')
3678 type = CTRL_X_DICTIONARY;
3679 else
3680 type = CTRL_X_THESAURUS;
3681 if (*++e_cpt != ',' && *e_cpt != NUL)
3682 {
3683 dict = e_cpt;
3684 dict_f = DICT_FIRST;
3685 }
3686 }
3687#ifdef FEAT_FIND_ID
3688 else if (*e_cpt == 'i')
3689 type = CTRL_X_PATH_PATTERNS;
3690 else if (*e_cpt == 'd')
3691 type = CTRL_X_PATH_DEFINES;
3692#endif
3693 else if (*e_cpt == ']' || *e_cpt == 't')
3694 {
3695 type = CTRL_X_TAGS;
3696 sprintf((char*)IObuff, _("Scanning tags."));
3697 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3698 }
3699 else
3700 type = -1;
3701
3702 /* in any case e_cpt is advanced to the next entry */
3703 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3704
3705 found_all = TRUE;
3706 if (type == -1)
3707 continue;
3708 }
3709 }
3710
3711 switch (type)
3712 {
3713 case -1:
3714 break;
3715#ifdef FEAT_FIND_ID
3716 case CTRL_X_PATH_PATTERNS:
3717 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003718 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003719 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003721 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3723 (linenr_T)1, (linenr_T)MAXLNUM);
3724 break;
3725#endif
3726
3727 case CTRL_X_DICTIONARY:
3728 case CTRL_X_THESAURUS:
3729 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003730 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 : (type == CTRL_X_THESAURUS
3732 ? (*curbuf->b_p_tsr == NUL
3733 ? p_tsr
3734 : curbuf->b_p_tsr)
3735 : (*curbuf->b_p_dict == NUL
3736 ? p_dict
3737 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003738 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003739 dict != NULL ? dict_f
3740 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 dict = NULL;
3742 break;
3743
3744 case CTRL_X_TAGS:
3745 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3746 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003747 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003750 * of matches is found when compl_pattern is empty */
3751 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3753 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3754 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3755 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003756 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 p_ic = save_p_ic;
3759 break;
3760
3761 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3764 {
3765
3766 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003767 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003768 ins_compl_add_matches(num_matches, matches,
3769#ifdef CASE_INSENSITIVE_FILENAME
3770 TRUE
3771#else
3772 FALSE
3773#endif
3774 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 break;
3777
3778 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 if (expand_cmdline(&compl_xp, compl_pattern,
3780 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003782 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 break;
3784
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003785#ifdef FEAT_COMPL_FUNC
3786 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003787 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003788 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003789 break;
3790#endif
3791
Bram Moolenaar488c6512005-08-11 20:09:58 +00003792 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003793#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003794 num_matches = expand_spelling(first_match_pos.lnum,
3795 first_match_pos.col, compl_pattern, &matches);
3796 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003797 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003798#endif
3799 break;
3800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 default: /* normal ^P/^N and ^X^L */
3802 /*
3803 * If 'infercase' is set, don't use 'smartcase' here
3804 */
3805 save_p_scs = p_scs;
3806 if (ins_buf->b_p_inf)
3807 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 /* buffers other than curbuf are scanned from the beginning or the
3810 * end but never from the middle, thus setting nowrapscan in this
3811 * buffers is a good idea, on the other hand, we always set
3812 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3813 save_p_ws = p_ws;
3814 if (ins_buf != curbuf)
3815 p_ws = FALSE;
3816 else if (*e_cpt == '.')
3817 p_ws = TRUE;
3818 for (;;)
3819 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003820 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003822 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3823 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003825 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003827 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003829 found_new_match = searchit(NULL, ins_buf, pos,
3830 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003831 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003832 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003833 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003835 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003836 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 first_match_pos = *pos;
3838 last_match_pos = *pos;
3839 }
3840 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003841 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 found_new_match = FAIL;
3843 if (found_new_match == FAIL)
3844 {
3845 if (ins_buf == curbuf)
3846 found_all = TRUE;
3847 break;
3848 }
3849
3850 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 && ini->lnum == pos->lnum
3853 && ini->col == pos->col)
3854 continue;
3855 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3856 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3857 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003858 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3861 continue;
3862 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3863 if (!p_paste)
3864 ptr = skipwhite(ptr);
3865 }
3866 len = (int)STRLEN(ptr);
3867 }
3868 else
3869 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003870 char_u *tmp_ptr = ptr;
3871
3872 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 /* Skip if already inside a word. */
3876 if (vim_iswordp(tmp_ptr))
3877 continue;
3878 /* Find start of next word. */
3879 tmp_ptr = find_word_start(tmp_ptr);
3880 }
3881 /* Find end of this word. */
3882 tmp_ptr = find_word_end(tmp_ptr);
3883 len = (int)(tmp_ptr - ptr);
3884
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 if ((compl_cont_status & CONT_ADDING)
3886 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3889 {
3890 /* Try next line, if any. the new word will be
3891 * "join" as if the normal command "J" was used.
3892 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 * works -- Acevedo */
3895 STRNCPY(IObuff, ptr, len);
3896 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3897 tmp_ptr = ptr = skipwhite(ptr);
3898 /* Find start of next word. */
3899 tmp_ptr = find_word_start(tmp_ptr);
3900 /* Find end of next word. */
3901 tmp_ptr = find_word_end(tmp_ptr);
3902 if (tmp_ptr > ptr)
3903 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003904 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003906 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 IObuff[len++] = ' ';
3908 /* IObuf =~ "\k.* ", thus len >= 2 */
3909 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003910 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 || (vim_strchr(p_cpo, CPO_JOINSP)
3912 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003913 && (IObuff[len - 2] == '?'
3914 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 IObuff[len++] = ' ';
3916 }
3917 /* copy as much as posible of the new word */
3918 if (tmp_ptr - ptr >= IOSIZE - len)
3919 tmp_ptr = ptr + IOSIZE - len - 1;
3920 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3921 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003922 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924 IObuff[len] = NUL;
3925 ptr = IObuff;
3926 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003927 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 continue;
3929 }
3930 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003931 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003932 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003933 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
3935 found_new_match = OK;
3936 break;
3937 }
3938 }
3939 p_scs = save_p_scs;
3940 p_ws = save_p_ws;
3941 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003942
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003943 /* check if compl_curr_match has changed, (e.g. other type of
3944 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003945 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 found_new_match = OK;
3947
3948 /* break the loop for specialized modes (use 'complete' just for the
3949 * generic ctrl_x_mode == 0) or when we've found a new match */
3950 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003952 {
3953 if (got_int)
3954 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003955 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003956 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003957 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003958
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003959 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3960 || compl_interrupted)
3961 break;
3962 compl_started = TRUE;
3963 }
3964 else
3965 {
3966 /* Mark a buffer scanned when it has been scanned completely */
3967 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3968 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003970 compl_started = FALSE;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3976 && *e_cpt == NUL) /* Got to end of 'complete' */
3977 found_new_match = FAIL;
3978
3979 i = -1; /* total of matches, unknown */
3980 if (found_new_match == FAIL
3981 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3982 i = ins_compl_make_cyclic();
3983
3984 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003985 * just been made cyclic then we have to move compl_curr_match to the next
3986 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003987 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3988 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003989 if (compl_curr_match == NULL)
3990 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 return i;
3992}
3993
3994/* Delete the old text being completed. */
3995 static void
3996ins_compl_delete()
3997{
3998 int i;
3999
4000 /*
4001 * In insert mode: Delete the typed part.
4002 * In replace mode: Put the old characters back, if any.
4003 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004004 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 backspace_until_column(i);
4006 changed_cline_bef_curs();
4007}
4008
4009/* Insert the new text being completed. */
4010 static void
4011ins_compl_insert()
4012{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004013 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004014 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4015 compl_used_match = FALSE;
4016 else
4017 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018}
4019
4020/*
4021 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004022 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4023 * get more completions. If it is FALSE, then we just do nothing when there
4024 * are no more completions in a given direction. The latter case is used when
4025 * we are still in the middle of finding completions, to allow browsing
4026 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * Return the total number of matches, or -1 if still unknown -- webb.
4028 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4030 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 *
4032 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004033 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4034 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 */
4036 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004037ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004039 int count; /* repeat completion this many times; should
4040 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004041 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043 int num_matches = -1;
4044 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004045 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004046 compl_T *found_compl = NULL;
4047 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004048 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004050 if (compl_leader != NULL
4051 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004053 /* Set "compl_shown_match" to the actually shown match, it may differ
4054 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004055 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004056 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004057 && compl_shown_match->cp_next != NULL
4058 && compl_shown_match->cp_next != compl_first_match)
4059 compl_shown_match = compl_shown_match->cp_next;
4060 }
4061
4062 if (allow_get_expansion && insert_match
4063 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 /* Delete old text to be replaced */
4065 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004066
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004067 /* When finding the longest common text we stick at the original text,
4068 * don't let CTRL-N or CTRL-P move to the first match. */
4069 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4070
Bram Moolenaare3226be2005-12-18 22:10:00 +00004071 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4072 * around. */
4073 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004075 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004077 if (compl_pending != 0)
4078 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004079 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004080 found_end = (compl_first_match != NULL
4081 && (compl_shown_match->cp_next == compl_first_match
4082 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004083 }
4084 else if (compl_shows_dir == BACKWARD
4085 && compl_shown_match->cp_prev != NULL)
4086 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004087 if (compl_pending != 0)
4088 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004089 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004090 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004091 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004094 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004095 if (advance)
4096 {
4097 if (compl_shows_dir == BACKWARD)
4098 --compl_pending;
4099 else
4100 ++compl_pending;
4101 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004102 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004103 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004104
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004105 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004106 if (compl_pending != 0 && compl_direction == compl_shows_dir
4107 && advance)
Bram Moolenaara6557602006-02-04 22:43:20 +00004108 compl_shown_match = compl_curr_match;
4109 found_end = FALSE;
4110 }
4111 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4112 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004113 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004114 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004115 ++todo;
4116 else
4117 /* Remember a matching item. */
4118 found_compl = compl_shown_match;
4119
4120 /* Stop at the end of the list when we found a usable match. */
4121 if (found_end)
4122 {
4123 if (found_compl != NULL)
4124 {
4125 compl_shown_match = found_compl;
4126 break;
4127 }
4128 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004132 /* Insert the text of the new completion, or the compl_leader. */
4133 if (insert_match)
4134 {
4135 if (!compl_get_longest || compl_used_match)
4136 ins_compl_insert();
4137 else
4138 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4139 }
4140 else
4141 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 if (!allow_get_expansion)
4144 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004145 /* may undisplay the popup menu first */
4146 ins_compl_upd_pum();
4147
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004148 /* redraw to show the user what was inserted */
4149 update_screen(0);
4150
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004151 /* display the updated popup menu */
4152 ins_compl_show_pum();
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 /* Delete old text to be replaced, since we're still searching and
4155 * don't want to match ourselves! */
4156 ins_compl_delete();
4157 }
4158
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004159 /* Enter will select a match when the match wasn't inserted and the popup
4160 * menu is visislbe. */
4161 compl_enter_selects = !insert_match && compl_match_array != NULL;
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 /*
4164 * Show the file name for the match (if any)
4165 * Truncate the file name to avoid a wait for return.
4166 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004167 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
4169 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004170 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (i <= 0)
4172 i = 0;
4173 else
4174 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004175 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 msg(IObuff);
4177 redraw_cmdline = FALSE; /* don't overwrite! */
4178 }
4179
4180 return num_matches;
4181}
4182
4183/*
4184 * Call this while finding completions, to check whether the user has hit a key
4185 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004186 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004188 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
4190 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004191ins_compl_check_keys(frequency)
4192 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 static int count = 0;
4195
4196 int c;
4197
4198 /* Don't check when reading keys from a script. That would break the test
4199 * scripts */
4200 if (using_script())
4201 return;
4202
4203 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004204 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return;
4206 count = 0;
4207
4208 ++no_mapping;
4209 c = vpeekc_any();
4210 --no_mapping;
4211 if (c != NUL)
4212 {
4213 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4214 {
4215 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004216 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004217 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4218 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004223 if (compl_pending != 0 && !got_int)
4224 (void)ins_compl_next(FALSE, compl_pending > 0
4225 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004226}
4227
4228/*
4229 * Decide the direction of Insert mode complete from the key typed.
4230 * Returns BACKWARD or FORWARD.
4231 */
4232 static int
4233ins_compl_key2dir(c)
4234 int c;
4235{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004236 if (c == Ctrl_P || c == Ctrl_L
4237 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4238 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004239 return BACKWARD;
4240 return FORWARD;
4241}
4242
4243/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004244 * Return TRUE for keys that are used for completion only when the popup menu
4245 * is visible.
4246 */
4247 static int
4248ins_compl_pum_key(c)
4249 int c;
4250{
4251 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004252 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4253 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004254}
4255
4256/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004257 * Decide the number of completions to move forward.
4258 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4259 */
4260 static int
4261ins_compl_key2count(c)
4262 int c;
4263{
4264 int h;
4265
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004266 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004267 {
4268 h = pum_get_height();
4269 if (h > 3)
4270 h -= 2; /* keep some context */
4271 return h;
4272 }
4273 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274}
4275
4276/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004277 * Return TRUE if completion with "c" should insert the match, FALSE if only
4278 * to change the currently selected completion.
4279 */
4280 static int
4281ins_compl_use_match(c)
4282 int c;
4283{
4284 switch (c)
4285 {
4286 case K_UP:
4287 case K_DOWN:
4288 case K_PAGEDOWN:
4289 case K_KPAGEDOWN:
4290 case K_S_DOWN:
4291 case K_PAGEUP:
4292 case K_KPAGEUP:
4293 case K_S_UP:
4294 return FALSE;
4295 }
4296 return TRUE;
4297}
4298
4299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 * Do Insert mode completion.
4301 * Called when character "c" was typed, which has a meaning for completion.
4302 * Returns OK if completion was done, FAIL if something failed (out of mem).
4303 */
4304 static int
4305ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004306 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 char_u *line;
4309 int startcol = 0; /* column where searched text starts */
4310 colnr_T curs_col; /* cursor column */
4311 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
Bram Moolenaare3226be2005-12-18 22:10:00 +00004313 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 /* First time we hit ^N or ^P (in a row, I mean) */
4317
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 did_ai = FALSE;
4319#ifdef FEAT_SMARTINDENT
4320 did_si = FALSE;
4321 can_si = FALSE;
4322 can_si_back = FALSE;
4323#endif
4324 if (stop_arrow() == FAIL)
4325 return FAIL;
4326
4327 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004329 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 * "compl_startpos" to the cursor as a pattern to add a new word
4333 * instead of expand the one before the cursor, in word-wise if
4334 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 * is not in the same line as the cursor then fix it (the line has
4336 * been split because it was longer than 'tw'). if SOL is set then
4337 * skip the previous pattern, a word at the beginning of the line has
4338 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004339 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4340 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
4342 /*
4343 * it is a continued search
4344 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4347 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4348 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 /* line (probably) wrapped, set compl_startpos to the
4352 * first non_blank in the line, if it is not a wordchar
4353 * include it to get a better pattern, but then we don't
4354 * want the "\\<" prefix, check it bellow */
4355 compl_col = (colnr_T)(skipwhite(line) - line);
4356 compl_startpos.col = compl_col;
4357 compl_startpos.lnum = curwin->w_cursor.lnum;
4358 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 else
4361 {
4362 /* S_IPOS was set when we inserted a word that was at the
4363 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004364 * mode but first we need to redefine compl_startpos */
4365 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 compl_cont_status |= CONT_SOL;
4368 compl_startpos.col = (colnr_T)(skipwhite(
4369 line + compl_length
4370 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004375 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * have enough space? just being paranoic */
4377#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 compl_cont_status &= ~CONT_SOL;
4381 compl_length = (IOSIZE - MIN_SPACE);
4382 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4385 if (compl_length < 1)
4386 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004396 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 compl_cont_status = 0;
4401 compl_cont_status |= CONT_N_ADDS;
4402 compl_startpos = curwin->w_cursor;
4403 startcol = (int)curs_col;
4404 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406
4407 /* Work out completion pattern and original text -- webb */
4408 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4409 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4412 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 compl_col += ++startcol;
4418 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 compl_pattern = str_foldcase(line + compl_col,
4422 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 compl_pattern = vim_strnsave(line + compl_col,
4425 compl_length);
4426 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 return FAIL;
4428 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 char_u *prefix = (char_u *)"\\<";
4432
4433 /* we need 3 extra chars, 1 for the NUL and
4434 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4436 compl_length) + 3);
4437 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 if (!vim_iswordp(line + compl_col)
4440 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 && (
4442#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004445 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446#endif
4447 )))
4448 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 STRCPY((char *)compl_pattern, prefix);
4450 (void)quote_meta(compl_pattern + STRLEN(prefix),
4451 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458#endif
4459 )
4460 {
4461 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4463 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465 compl_col += curs_col;
4466 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else
4469 {
4470#ifdef FEAT_MBYTE
4471 /* Search the point of change class of multibyte character
4472 * or not a word single byte character backward. */
4473 if (has_mbyte)
4474 {
4475 int base_class;
4476 int head_off;
4477
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004478 startcol -= (*mb_head_off)(line, line + startcol);
4479 base_class = mb_get_class(line + startcol);
4480 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 head_off = (*mb_head_off)(line, line + startcol);
4483 if (base_class != mb_get_class(line + startcol
4484 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 }
4489 else
4490#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 compl_col += ++startcol;
4494 compl_length = (int)curs_col - startcol;
4495 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
4497 /* Only match word with at least two chars -- webb
4498 * there's no need to call quote_meta,
4499 * alloc(7) is enough -- Acevedo
4500 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 compl_pattern = alloc(7);
4502 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 STRCPY((char *)compl_pattern, "\\<");
4505 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4506 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 else
4509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4511 compl_length) + 3);
4512 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 STRCPY((char *)compl_pattern, "\\<");
4515 (void)quote_meta(compl_pattern + 2, line + compl_col,
4516 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518 }
4519 }
4520 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4521 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004522 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 compl_length = (int)curs_col - (int)compl_col;
4524 if (compl_length < 0) /* cursor in indent: empty pattern */
4525 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 compl_pattern = str_foldcase(line + compl_col, compl_length,
4528 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4531 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 return FAIL;
4533 }
4534 else if (ctrl_x_mode == CTRL_X_FILES)
4535 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 compl_col += ++startcol;
4539 compl_length = (int)curs_col - startcol;
4540 compl_pattern = addstar(line + compl_col, compl_length,
4541 EXPAND_FILES);
4542 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 return FAIL;
4544 }
4545 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4546 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 compl_pattern = vim_strnsave(line, curs_col);
4548 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 set_cmd_context(&compl_xp, compl_pattern,
4551 (int)STRLEN(compl_pattern), curs_col);
4552 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4553 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4556 compl_col = startcol;
4557 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004559 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004560 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004561#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004562 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004563 * Call user defined function 'completefunc' with "a:findstart"
4564 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004565 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004566 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004567 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004568 char_u *funcname;
4569 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004570
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004571 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004572 * string */
4573 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4574 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4575 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004576 {
4577 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4578 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004579 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004580 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004581
4582 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004583 args[1] = NULL;
4584 pos = curwin->w_cursor;
4585 col = call_func_retnr(funcname, 2, args, FALSE);
4586 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004587
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004588 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004589 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004590 compl_col = col;
4591 if ((colnr_T)compl_col > curs_col)
4592 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004593
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594 /* Setup variables for completion. Need to obtain "line" again,
4595 * it may have become invalid. */
4596 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004597 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4599 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004600#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 return FAIL;
4602 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004603 else if (ctrl_x_mode == CTRL_X_SPELL)
4604 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004605#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004606 if (spell_bad_len > 0)
4607 compl_col = curs_col - spell_bad_len;
4608 else
4609 compl_col = spell_word_start(startcol);
4610 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004611 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004612 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004613 /* Need to obtain "line" again, it may have become invalid. */
4614 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004615 compl_length = (int)curs_col - compl_col;
4616 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4617 if (compl_pattern == NULL)
4618#endif
4619 return FAIL;
4620 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 else
4622 {
4623 EMSG2(_(e_intern2), "ins_complete()");
4624 return FAIL;
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 edit_submode_pre = (char_u *)_(" Adding");
4630 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4631 {
4632 /* Insert a new line, keep indentation but ignore 'comments' */
4633#ifdef FEAT_COMMENTS
4634 char_u *old = curbuf->b_p_com;
4635
4636 curbuf->b_p_com = (char_u *)"";
4637#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 compl_startpos.lnum = curwin->w_cursor.lnum;
4639 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 ins_eol('\r');
4641#ifdef FEAT_COMMENTS
4642 curbuf->b_p_com = old;
4643#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 compl_length = 0;
4645 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647 }
4648 else
4649 {
4650 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 if (compl_cont_status & CONT_LOCAL)
4655 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 else
4657 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4658
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004659 /* Always add completion for the original text. */
4660 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4662 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004663 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 vim_free(compl_pattern);
4666 compl_pattern = NULL;
4667 vim_free(compl_orig_text);
4668 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return FAIL;
4670 }
4671
4672 /* showmode might reset the internal line pointers, so it must
4673 * be called before line = ml_get(), or when this address is no
4674 * longer needed. -- Acevedo.
4675 */
4676 edit_submode_extra = (char_u *)_("-- Searching...");
4677 edit_submode_highl = HLF_COUNT;
4678 showmode();
4679 edit_submode_extra = NULL;
4680 out_flush();
4681 }
4682
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 compl_shown_match = compl_curr_match;
4684 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004687 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004689 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004691 /* may undisplay the popup menu */
4692 ins_compl_upd_pum();
4693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004694 if (n > 1) /* all matches have been found */
4695 compl_matches = n;
4696 compl_curr_match = compl_shown_match;
4697 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaard68071d2006-05-02 22:08:30 +00004699 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4700 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 if (got_int && !global_busy)
4702 {
4703 (void)vgetc();
4704 got_int = FALSE;
4705 }
4706
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004707 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004708 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004710 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4711 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4713 edit_submode_highl = HLF_E;
4714 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4715 * because we couldn't expand anything at first place, but if we used
4716 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4717 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 if ( compl_length > 1
4719 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 || (ctrl_x_mode != 0
4721 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4722 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004723 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725
Bram Moolenaar572cb562005-08-05 21:35:02 +00004726 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004727 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 if (edit_submode_extra == NULL)
4732 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004733 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 edit_submode_extra = (char_u *)_("Back at original");
4736 edit_submode_highl = HLF_W;
4737 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004738 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 edit_submode_extra = (char_u *)_("Word from other line");
4741 edit_submode_highl = HLF_COUNT;
4742 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004743 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
4745 edit_submode_extra = (char_u *)_("The only match");
4746 edit_submode_highl = HLF_COUNT;
4747 }
4748 else
4749 {
4750 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004751 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004753 int number = 0;
4754 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /* search backwards for the first valid (!= -1) number.
4759 * This should normally succeed already at the first loop
4760 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004761 for (match = compl_curr_match->cp_prev; match != NULL
4762 && match != compl_first_match;
4763 match = match->cp_prev)
4764 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004766 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 break;
4768 }
4769 if (match != NULL)
4770 /* go up and assign all numbers which are not assigned
4771 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004772 for (match = match->cp_next;
4773 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004774 match = match->cp_next)
4775 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777 else /* BACKWARD */
4778 {
4779 /* search forwards (upwards) for the first valid (!= -1)
4780 * number. This should normally succeed already at the
4781 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004782 for (match = compl_curr_match->cp_next; match != NULL
4783 && match != compl_first_match;
4784 match = match->cp_next)
4785 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004787 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 break;
4789 }
4790 if (match != NULL)
4791 /* go down and assign all numbers which are not
4792 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004793 for (match = match->cp_prev; match
4794 && match->cp_number == -1;
4795 match = match->cp_prev)
4796 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 }
4799
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004800 /* The match should always have a sequence number now, this is
4801 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004802 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804 /* Space for 10 text chars. + 2x10-digit no.s */
4805 static char_u match_ref[31];
4806
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004807 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004809 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004812 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004813 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 edit_submode_extra = match_ref;
4815 edit_submode_highl = HLF_R;
4816 if (dollar_vcol)
4817 curs_columns(FALSE);
4818 }
4819 }
4820 }
4821
4822 /* Show a message about what (completion) mode we're in. */
4823 showmode();
4824 if (edit_submode_extra != NULL)
4825 {
4826 if (!p_smd)
4827 msg_attr(edit_submode_extra,
4828 edit_submode_highl < HLF_COUNT
4829 ? hl_attr(edit_submode_highl) : 0);
4830 }
4831 else
4832 msg_clr_cmdline(); /* necessary for "noshowmode" */
4833
Bram Moolenaard68071d2006-05-02 22:08:30 +00004834 /* Show the popup menu, unless we got interrupted. */
4835 if (!compl_interrupted)
4836 {
4837 /* RedrawingDisabled may be set when invoked through complete(). */
4838 n = RedrawingDisabled;
4839 RedrawingDisabled = 0;
4840 ins_compl_show_pum();
4841 setcursor();
4842 RedrawingDisabled = n;
4843 }
4844 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 return OK;
4847}
4848
4849/*
4850 * Looks in the first "len" chars. of "src" for search-metachars.
4851 * If dest is not NULL the chars. are copied there quoting (with
4852 * a backslash) the metachars, and dest would be NUL terminated.
4853 * Returns the length (needed) of dest
4854 */
4855 static int
4856quote_meta(dest, src, len)
4857 char_u *dest;
4858 char_u *src;
4859 int len;
4860{
4861 int m;
4862
4863 for (m = len; --len >= 0; src++)
4864 {
4865 switch (*src)
4866 {
4867 case '.':
4868 case '*':
4869 case '[':
4870 if (ctrl_x_mode == CTRL_X_DICTIONARY
4871 || ctrl_x_mode == CTRL_X_THESAURUS)
4872 break;
4873 case '~':
4874 if (!p_magic) /* quote these only if magic is set */
4875 break;
4876 case '\\':
4877 if (ctrl_x_mode == CTRL_X_DICTIONARY
4878 || ctrl_x_mode == CTRL_X_THESAURUS)
4879 break;
4880 case '^': /* currently it's not needed. */
4881 case '$':
4882 m++;
4883 if (dest != NULL)
4884 *dest++ = '\\';
4885 break;
4886 }
4887 if (dest != NULL)
4888 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004889# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 /* Copy remaining bytes of a multibyte character. */
4891 if (has_mbyte)
4892 {
4893 int i, mb_len;
4894
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004895 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 if (mb_len > 0 && len >= mb_len)
4897 for (i = 0; i < mb_len; ++i)
4898 {
4899 --len;
4900 ++src;
4901 if (dest != NULL)
4902 *dest++ = *src;
4903 }
4904 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 if (dest != NULL)
4908 *dest = NUL;
4909
4910 return m;
4911}
4912#endif /* FEAT_INS_EXPAND */
4913
4914/*
4915 * Next character is interpreted literally.
4916 * A one, two or three digit decimal number is interpreted as its byte value.
4917 * If one or two digits are entered, the next character is given to vungetc().
4918 * For Unicode a character > 255 may be returned.
4919 */
4920 int
4921get_literal()
4922{
4923 int cc;
4924 int nc;
4925 int i;
4926 int hex = FALSE;
4927 int octal = FALSE;
4928#ifdef FEAT_MBYTE
4929 int unicode = 0;
4930#endif
4931
4932 if (got_int)
4933 return Ctrl_C;
4934
4935#ifdef FEAT_GUI
4936 /*
4937 * In GUI there is no point inserting the internal code for a special key.
4938 * It is more useful to insert the string "<KEY>" instead. This would
4939 * probably be useful in a text window too, but it would not be
4940 * vi-compatible (maybe there should be an option for it?) -- webb
4941 */
4942 if (gui.in_use)
4943 ++allow_keys;
4944#endif
4945#ifdef USE_ON_FLY_SCROLL
4946 dont_scroll = TRUE; /* disallow scrolling here */
4947#endif
4948 ++no_mapping; /* don't map the next key hits */
4949 cc = 0;
4950 i = 0;
4951 for (;;)
4952 {
4953 do
4954 nc = safe_vgetc();
4955 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4956 || nc == K_HOR_SCROLLBAR);
4957#ifdef FEAT_CMDL_INFO
4958 if (!(State & CMDLINE)
4959# ifdef FEAT_MBYTE
4960 && MB_BYTE2LEN_CHECK(nc) == 1
4961# endif
4962 )
4963 add_to_showcmd(nc);
4964#endif
4965 if (nc == 'x' || nc == 'X')
4966 hex = TRUE;
4967 else if (nc == 'o' || nc == 'O')
4968 octal = TRUE;
4969#ifdef FEAT_MBYTE
4970 else if (nc == 'u' || nc == 'U')
4971 unicode = nc;
4972#endif
4973 else
4974 {
4975 if (hex
4976#ifdef FEAT_MBYTE
4977 || unicode != 0
4978#endif
4979 )
4980 {
4981 if (!vim_isxdigit(nc))
4982 break;
4983 cc = cc * 16 + hex2nr(nc);
4984 }
4985 else if (octal)
4986 {
4987 if (nc < '0' || nc > '7')
4988 break;
4989 cc = cc * 8 + nc - '0';
4990 }
4991 else
4992 {
4993 if (!VIM_ISDIGIT(nc))
4994 break;
4995 cc = cc * 10 + nc - '0';
4996 }
4997
4998 ++i;
4999 }
5000
5001 if (cc > 255
5002#ifdef FEAT_MBYTE
5003 && unicode == 0
5004#endif
5005 )
5006 cc = 255; /* limit range to 0-255 */
5007 nc = 0;
5008
5009 if (hex) /* hex: up to two chars */
5010 {
5011 if (i >= 2)
5012 break;
5013 }
5014#ifdef FEAT_MBYTE
5015 else if (unicode) /* Unicode: up to four or eight chars */
5016 {
5017 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5018 break;
5019 }
5020#endif
5021 else if (i >= 3) /* decimal or octal: up to three chars */
5022 break;
5023 }
5024 if (i == 0) /* no number entered */
5025 {
5026 if (nc == K_ZERO) /* NUL is stored as NL */
5027 {
5028 cc = '\n';
5029 nc = 0;
5030 }
5031 else
5032 {
5033 cc = nc;
5034 nc = 0;
5035 }
5036 }
5037
5038 if (cc == 0) /* NUL is stored as NL */
5039 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005040#ifdef FEAT_MBYTE
5041 if (enc_dbcs && (cc & 0xff) == 0)
5042 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5043 second byte will cause trouble! */
5044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
5046 --no_mapping;
5047#ifdef FEAT_GUI
5048 if (gui.in_use)
5049 --allow_keys;
5050#endif
5051 if (nc)
5052 vungetc(nc);
5053 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5054 return cc;
5055}
5056
5057/*
5058 * Insert character, taking care of special keys and mod_mask
5059 */
5060 static void
5061insert_special(c, allow_modmask, ctrlv)
5062 int c;
5063 int allow_modmask;
5064 int ctrlv; /* c was typed after CTRL-V */
5065{
5066 char_u *p;
5067 int len;
5068
5069 /*
5070 * Special function key, translate into "<Key>". Up to the last '>' is
5071 * inserted with ins_str(), so as not to replace characters in replace
5072 * mode.
5073 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5074 * unless 'allow_modmask' is TRUE.
5075 */
5076#ifdef MACOS
5077 /* Command-key never produces a normal key */
5078 if (mod_mask & MOD_MASK_CMD)
5079 allow_modmask = TRUE;
5080#endif
5081 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5082 {
5083 p = get_special_key_name(c, mod_mask);
5084 len = (int)STRLEN(p);
5085 c = p[len - 1];
5086 if (len > 2)
5087 {
5088 if (stop_arrow() == FAIL)
5089 return;
5090 p[len - 1] = NUL;
5091 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005092 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 ctrlv = FALSE;
5094 }
5095 }
5096 if (stop_arrow() == OK)
5097 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5098}
5099
5100/*
5101 * Special characters in this context are those that need processing other
5102 * than the simple insertion that can be performed here. This includes ESC
5103 * which terminates the insert, and CR/NL which need special processing to
5104 * open up a new line. This routine tries to optimize insertions performed by
5105 * the "redo", "undo" or "put" commands, so it needs to know when it should
5106 * stop and defer processing to the "normal" mechanism.
5107 * '0' and '^' are special, because they can be followed by CTRL-D.
5108 */
5109#ifdef EBCDIC
5110# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5111#else
5112# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5113#endif
5114
5115#ifdef FEAT_MBYTE
5116# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5117#else
5118# define WHITECHAR(cc) vim_iswhite(cc)
5119#endif
5120
5121 void
5122insertchar(c, flags, second_indent)
5123 int c; /* character to insert or NUL */
5124 int flags; /* INSCHAR_FORMAT, etc. */
5125 int second_indent; /* indent for second line if >= 0 */
5126{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 int textwidth;
5128#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5134 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 /*
5137 * Try to break the line in two or more pieces when:
5138 * - Always do this if we have been called to do formatting only.
5139 * - Always do this when 'formatoptions' has the 'a' flag and the line
5140 * ends in white space.
5141 * - Otherwise:
5142 * - Don't do this if inserting a blank
5143 * - Don't do this if an existing character is being replaced, unless
5144 * we're in VREPLACE mode.
5145 * - Do this if the cursor is not on the line where insert started
5146 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5147 * before the insert.
5148 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5149 * before 'textwidth'
5150 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005151 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 && ((flags & INSCHAR_FORMAT)
5153 || (!vim_iswhite(c)
5154 && !((State & REPLACE_FLAG)
5155#ifdef FEAT_VREPLACE
5156 && !(State & VREPLACE_FLAG)
5157#endif
5158 && *ml_get_cursor() != NUL)
5159 && (curwin->w_cursor.lnum != Insstart.lnum
5160 || ((!has_format_option(FO_INS_LONG)
5161 || Insstart_textlen <= (colnr_T)textwidth)
5162 && (!fo_ins_blank
5163 || Insstart_blank_vcol <= (colnr_T)textwidth
5164 ))))))
5165 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005166 /* Format with 'formatexpr' when it's set. Use internal formatting
5167 * when 'formatexpr' isn't set or it returns non-zero. */
5168#if defined(FEAT_EVAL)
5169 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005170 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005172 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (c == NUL) /* only formatting was wanted */
5176 return;
5177
5178#ifdef FEAT_COMMENTS
5179 /* Check whether this character should end a comment. */
5180 if (did_ai && (int)c == end_comment_pending)
5181 {
5182 char_u *line;
5183 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5184 int middle_len, end_len;
5185 int i;
5186
5187 /*
5188 * Need to remove existing (middle) comment leader and insert end
5189 * comment leader. First, check what comment leader we can find.
5190 */
5191 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5192 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5193 {
5194 /* Skip middle-comment string */
5195 while (*p && p[-1] != ':') /* find end of middle flags */
5196 ++p;
5197 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5198 /* Don't count trailing white space for middle_len */
5199 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5200 --middle_len;
5201
5202 /* Find the end-comment string */
5203 while (*p && p[-1] != ':') /* find end of end flags */
5204 ++p;
5205 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5206
5207 /* Skip white space before the cursor */
5208 i = curwin->w_cursor.col;
5209 while (--i >= 0 && vim_iswhite(line[i]))
5210 ;
5211 i++;
5212
5213 /* Skip to before the middle leader */
5214 i -= middle_len;
5215
5216 /* Check some expected things before we go on */
5217 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5218 {
5219 /* Backspace over all the stuff we want to replace */
5220 backspace_until_column(i);
5221
5222 /*
5223 * Insert the end-comment string, except for the last
5224 * character, which will get inserted as normal later.
5225 */
5226 ins_bytes_len(lead_end, end_len - 1);
5227 }
5228 }
5229 }
5230 end_comment_pending = NUL;
5231#endif
5232
5233 did_ai = FALSE;
5234#ifdef FEAT_SMARTINDENT
5235 did_si = FALSE;
5236 can_si = FALSE;
5237 can_si_back = FALSE;
5238#endif
5239
5240 /*
5241 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5242 * This speeds up normal text input considerably.
5243 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5244 * need to re-indent at a ':', or any other character (but not what
5245 * 'paste' is set)..
5246 */
5247#ifdef USE_ON_FLY_SCROLL
5248 dont_scroll = FALSE; /* allow scrolling here */
5249#endif
5250
5251 if ( !ISSPECIAL(c)
5252#ifdef FEAT_MBYTE
5253 && (!has_mbyte || (*mb_char2len)(c) == 1)
5254#endif
5255 && vpeekc() != NUL
5256 && !(State & REPLACE_FLAG)
5257#ifdef FEAT_CINDENT
5258 && !cindent_on()
5259#endif
5260#ifdef FEAT_RIGHTLEFT
5261 && !p_ri
5262#endif
5263 )
5264 {
5265#define INPUT_BUFLEN 100
5266 char_u buf[INPUT_BUFLEN + 1];
5267 int i;
5268 colnr_T virtcol = 0;
5269
5270 buf[0] = c;
5271 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005272 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 virtcol = get_nolist_virtcol();
5274 /*
5275 * Stop the string when:
5276 * - no more chars available
5277 * - finding a special character (command key)
5278 * - buffer is full
5279 * - running into the 'textwidth' boundary
5280 * - need to check for abbreviation: A non-word char after a word-char
5281 */
5282 while ( (c = vpeekc()) != NUL
5283 && !ISSPECIAL(c)
5284#ifdef FEAT_MBYTE
5285 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5286#endif
5287 && i < INPUT_BUFLEN
5288 && (textwidth == 0
5289 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5290 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5291 {
5292#ifdef FEAT_RIGHTLEFT
5293 c = vgetc();
5294 if (p_hkmap && KeyTyped)
5295 c = hkmap(c); /* Hebrew mode mapping */
5296# ifdef FEAT_FKMAP
5297 if (p_fkmap && KeyTyped)
5298 c = fkmap(c); /* Farsi mode mapping */
5299# endif
5300 buf[i++] = c;
5301#else
5302 buf[i++] = vgetc();
5303#endif
5304 }
5305
5306#ifdef FEAT_DIGRAPHS
5307 do_digraph(-1); /* clear digraphs */
5308 do_digraph(buf[i-1]); /* may be the start of a digraph */
5309#endif
5310 buf[i] = NUL;
5311 ins_str(buf);
5312 if (flags & INSCHAR_CTRLV)
5313 {
5314 redo_literal(*buf);
5315 i = 1;
5316 }
5317 else
5318 i = 0;
5319 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005320 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322 else
5323 {
5324#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005325 int cc;
5326
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5328 {
5329 char_u buf[MB_MAXBYTES + 1];
5330
5331 (*mb_char2bytes)(c, buf);
5332 buf[cc] = NUL;
5333 ins_char_bytes(buf, cc);
5334 AppendCharToRedobuff(c);
5335 }
5336 else
5337#endif
5338 {
5339 ins_char(c);
5340 if (flags & INSCHAR_CTRLV)
5341 redo_literal(c);
5342 else
5343 AppendCharToRedobuff(c);
5344 }
5345 }
5346}
5347
5348/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005349 * Format text at the current insert position.
5350 */
5351 static void
5352internal_format(textwidth, second_indent, flags, format_only)
5353 int textwidth;
5354 int second_indent;
5355 int flags;
5356 int format_only;
5357{
5358 int cc;
5359 int save_char = NUL;
5360 int haveto_redraw = FALSE;
5361 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5362#ifdef FEAT_MBYTE
5363 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5364#endif
5365 int fo_white_par = has_format_option(FO_WHITE_PAR);
5366 int first_line = TRUE;
5367#ifdef FEAT_COMMENTS
5368 colnr_T leader_len;
5369 int no_leader = FALSE;
5370 int do_comments = (flags & INSCHAR_DO_COM);
5371#endif
5372
5373 /*
5374 * When 'ai' is off we don't want a space under the cursor to be
5375 * deleted. Replace it with an 'x' temporarily.
5376 */
5377 if (!curbuf->b_p_ai)
5378 {
5379 cc = gchar_cursor();
5380 if (vim_iswhite(cc))
5381 {
5382 save_char = cc;
5383 pchar_cursor('x');
5384 }
5385 }
5386
5387 /*
5388 * Repeat breaking lines, until the current line is not too long.
5389 */
5390 while (!got_int)
5391 {
5392 int startcol; /* Cursor column at entry */
5393 int wantcol; /* column at textwidth border */
5394 int foundcol; /* column for start of spaces */
5395 int end_foundcol = 0; /* column for start of word */
5396 colnr_T len;
5397 colnr_T virtcol;
5398#ifdef FEAT_VREPLACE
5399 int orig_col = 0;
5400 char_u *saved_text = NULL;
5401#endif
5402 colnr_T col;
5403
5404 virtcol = get_nolist_virtcol();
5405 if (virtcol < (colnr_T)textwidth)
5406 break;
5407
5408#ifdef FEAT_COMMENTS
5409 if (no_leader)
5410 do_comments = FALSE;
5411 else if (!(flags & INSCHAR_FORMAT)
5412 && has_format_option(FO_WRAP_COMS))
5413 do_comments = TRUE;
5414
5415 /* Don't break until after the comment leader */
5416 if (do_comments)
5417 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5418 else
5419 leader_len = 0;
5420
5421 /* If the line doesn't start with a comment leader, then don't
5422 * start one in a following broken line. Avoids that a %word
5423 * moved to the start of the next line causes all following lines
5424 * to start with %. */
5425 if (leader_len == 0)
5426 no_leader = TRUE;
5427#endif
5428 if (!(flags & INSCHAR_FORMAT)
5429#ifdef FEAT_COMMENTS
5430 && leader_len == 0
5431#endif
5432 && !has_format_option(FO_WRAP))
5433
5434 {
5435 textwidth = 0;
5436 break;
5437 }
5438 if ((startcol = curwin->w_cursor.col) == 0)
5439 break;
5440
5441 /* find column of textwidth border */
5442 coladvance((colnr_T)textwidth);
5443 wantcol = curwin->w_cursor.col;
5444
5445 curwin->w_cursor.col = startcol - 1;
5446#ifdef FEAT_MBYTE
5447 /* Correct cursor for multi-byte character. */
5448 if (has_mbyte)
5449 mb_adjust_cursor();
5450#endif
5451 foundcol = 0;
5452
5453 /*
5454 * Find position to break at.
5455 * Stop at first entered white when 'formatoptions' has 'v'
5456 */
5457 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5458 || curwin->w_cursor.lnum != Insstart.lnum
5459 || curwin->w_cursor.col >= Insstart.col)
5460 {
5461 cc = gchar_cursor();
5462 if (WHITECHAR(cc))
5463 {
5464 /* remember position of blank just before text */
5465 end_foundcol = curwin->w_cursor.col;
5466
5467 /* find start of sequence of blanks */
5468 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5469 {
5470 dec_cursor();
5471 cc = gchar_cursor();
5472 }
5473 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5474 break; /* only spaces in front of text */
5475#ifdef FEAT_COMMENTS
5476 /* Don't break until after the comment leader */
5477 if (curwin->w_cursor.col < leader_len)
5478 break;
5479#endif
5480 if (has_format_option(FO_ONE_LETTER))
5481 {
5482 /* do not break after one-letter words */
5483 if (curwin->w_cursor.col == 0)
5484 break; /* one-letter word at begin */
5485
5486 col = curwin->w_cursor.col;
5487 dec_cursor();
5488 cc = gchar_cursor();
5489
5490 if (WHITECHAR(cc))
5491 continue; /* one-letter, continue */
5492 curwin->w_cursor.col = col;
5493 }
5494#ifdef FEAT_MBYTE
5495 if (has_mbyte)
5496 foundcol = curwin->w_cursor.col
5497 + (*mb_ptr2len)(ml_get_cursor());
5498 else
5499#endif
5500 foundcol = curwin->w_cursor.col + 1;
5501 if (curwin->w_cursor.col < (colnr_T)wantcol)
5502 break;
5503 }
5504#ifdef FEAT_MBYTE
5505 else if (cc >= 0x100 && fo_multibyte
5506 && curwin->w_cursor.col <= (colnr_T)wantcol)
5507 {
5508 /* Break after or before a multi-byte character. */
5509 foundcol = curwin->w_cursor.col;
5510 if (curwin->w_cursor.col < (colnr_T)wantcol)
5511 foundcol += (*mb_char2len)(cc);
5512 end_foundcol = foundcol;
5513 break;
5514 }
5515#endif
5516 if (curwin->w_cursor.col == 0)
5517 break;
5518 dec_cursor();
5519 }
5520
5521 if (foundcol == 0) /* no spaces, cannot break line */
5522 {
5523 curwin->w_cursor.col = startcol;
5524 break;
5525 }
5526
5527 /* Going to break the line, remove any "$" now. */
5528 undisplay_dollar();
5529
5530 /*
5531 * Offset between cursor position and line break is used by replace
5532 * stack functions. VREPLACE does not use this, and backspaces
5533 * over the text instead.
5534 */
5535#ifdef FEAT_VREPLACE
5536 if (State & VREPLACE_FLAG)
5537 orig_col = startcol; /* Will start backspacing from here */
5538 else
5539#endif
5540 replace_offset = startcol - end_foundcol - 1;
5541
5542 /*
5543 * adjust startcol for spaces that will be deleted and
5544 * characters that will remain on top line
5545 */
5546 curwin->w_cursor.col = foundcol;
5547 while (cc = gchar_cursor(), WHITECHAR(cc))
5548 inc_cursor();
5549 startcol -= curwin->w_cursor.col;
5550 if (startcol < 0)
5551 startcol = 0;
5552
5553#ifdef FEAT_VREPLACE
5554 if (State & VREPLACE_FLAG)
5555 {
5556 /*
5557 * In VREPLACE mode, we will backspace over the text to be
5558 * wrapped, so save a copy now to put on the next line.
5559 */
5560 saved_text = vim_strsave(ml_get_cursor());
5561 curwin->w_cursor.col = orig_col;
5562 if (saved_text == NULL)
5563 break; /* Can't do it, out of memory */
5564 saved_text[startcol] = NUL;
5565
5566 /* Backspace over characters that will move to the next line */
5567 if (!fo_white_par)
5568 backspace_until_column(foundcol);
5569 }
5570 else
5571#endif
5572 {
5573 /* put cursor after pos. to break line */
5574 if (!fo_white_par)
5575 curwin->w_cursor.col = foundcol;
5576 }
5577
5578 /*
5579 * Split the line just before the margin.
5580 * Only insert/delete lines, but don't really redraw the window.
5581 */
5582 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5583 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5584#ifdef FEAT_COMMENTS
5585 + (do_comments ? OPENLINE_DO_COM : 0)
5586#endif
5587 , old_indent);
5588 old_indent = 0;
5589
5590 replace_offset = 0;
5591 if (first_line)
5592 {
5593 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5594 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5595 if (second_indent >= 0)
5596 {
5597#ifdef FEAT_VREPLACE
5598 if (State & VREPLACE_FLAG)
5599 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5600 else
5601#endif
5602 (void)set_indent(second_indent, SIN_CHANGED);
5603 }
5604 first_line = FALSE;
5605 }
5606
5607#ifdef FEAT_VREPLACE
5608 if (State & VREPLACE_FLAG)
5609 {
5610 /*
5611 * In VREPLACE mode we have backspaced over the text to be
5612 * moved, now we re-insert it into the new line.
5613 */
5614 ins_bytes(saved_text);
5615 vim_free(saved_text);
5616 }
5617 else
5618#endif
5619 {
5620 /*
5621 * Check if cursor is not past the NUL off the line, cindent
5622 * may have added or removed indent.
5623 */
5624 curwin->w_cursor.col += startcol;
5625 len = (colnr_T)STRLEN(ml_get_curline());
5626 if (curwin->w_cursor.col > len)
5627 curwin->w_cursor.col = len;
5628 }
5629
5630 haveto_redraw = TRUE;
5631#ifdef FEAT_CINDENT
5632 can_cindent = TRUE;
5633#endif
5634 /* moved the cursor, don't autoindent or cindent now */
5635 did_ai = FALSE;
5636#ifdef FEAT_SMARTINDENT
5637 did_si = FALSE;
5638 can_si = FALSE;
5639 can_si_back = FALSE;
5640#endif
5641 line_breakcheck();
5642 }
5643
5644 if (save_char != NUL) /* put back space after cursor */
5645 pchar_cursor(save_char);
5646
5647 if (!format_only && haveto_redraw)
5648 {
5649 update_topline();
5650 redraw_curbuf_later(VALID);
5651 }
5652}
5653
5654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 * Called after inserting or deleting text: When 'formatoptions' includes the
5656 * 'a' flag format from the current line until the end of the paragraph.
5657 * Keep the cursor at the same position relative to the text.
5658 * The caller must have saved the cursor line for undo, following ones will be
5659 * saved here.
5660 */
5661 void
5662auto_format(trailblank, prev_line)
5663 int trailblank; /* when TRUE also format with trailing blank */
5664 int prev_line; /* may start in previous line */
5665{
5666 pos_T pos;
5667 colnr_T len;
5668 char_u *old;
5669 char_u *new, *pnew;
5670 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005671 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
5673 if (!has_format_option(FO_AUTO))
5674 return;
5675
5676 pos = curwin->w_cursor;
5677 old = ml_get_curline();
5678
5679 /* may remove added space */
5680 check_auto_format(FALSE);
5681
5682 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5683 * user might insert normal text next. Also skip formatting when "1" is
5684 * in 'formatoptions' and there is a single character before the cursor.
5685 * Otherwise the line would be broken and when typing another non-white
5686 * next they are not joined back together. */
5687 wasatend = (pos.col == STRLEN(old));
5688 if (*old != NUL && !trailblank && wasatend)
5689 {
5690 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005691 cc = gchar_cursor();
5692 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5693 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005695 cc = gchar_cursor();
5696 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 curwin->w_cursor = pos;
5699 return;
5700 }
5701 curwin->w_cursor = pos;
5702 }
5703
5704#ifdef FEAT_COMMENTS
5705 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5706 * comments. */
5707 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5708 && get_leader_len(old, NULL, FALSE) == 0)
5709 return;
5710#endif
5711
5712 /*
5713 * May start formatting in a previous line, so that after "x" a word is
5714 * moved to the previous line if it fits there now. Only when this is not
5715 * the start of a paragraph.
5716 */
5717 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5718 {
5719 --curwin->w_cursor.lnum;
5720 if (u_save_cursor() == FAIL)
5721 return;
5722 }
5723
5724 /*
5725 * Do the formatting and restore the cursor position. "saved_cursor" will
5726 * be adjusted for the text formatting.
5727 */
5728 saved_cursor = pos;
5729 format_lines((linenr_T)-1);
5730 curwin->w_cursor = saved_cursor;
5731 saved_cursor.lnum = 0;
5732
5733 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5734 {
5735 /* "cannot happen" */
5736 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5737 coladvance((colnr_T)MAXCOL);
5738 }
5739 else
5740 check_cursor_col();
5741
5742 /* Insert mode: If the cursor is now after the end of the line while it
5743 * previously wasn't, the line was broken. Because of the rule above we
5744 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5745 * formatted. */
5746 if (!wasatend && has_format_option(FO_WHITE_PAR))
5747 {
5748 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005749 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (curwin->w_cursor.col == len)
5751 {
5752 pnew = vim_strnsave(new, len + 2);
5753 pnew[len] = ' ';
5754 pnew[len + 1] = NUL;
5755 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5756 /* remove the space later */
5757 did_add_space = TRUE;
5758 }
5759 else
5760 /* may remove added space */
5761 check_auto_format(FALSE);
5762 }
5763
5764 check_cursor();
5765}
5766
5767/*
5768 * When an extra space was added to continue a paragraph for auto-formatting,
5769 * delete it now. The space must be under the cursor, just after the insert
5770 * position.
5771 */
5772 static void
5773check_auto_format(end_insert)
5774 int end_insert; /* TRUE when ending Insert mode */
5775{
5776 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005777 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 if (did_add_space)
5780 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005781 cc = gchar_cursor();
5782 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /* Somehow the space was removed already. */
5784 did_add_space = FALSE;
5785 else
5786 {
5787 if (!end_insert)
5788 {
5789 inc_cursor();
5790 c = gchar_cursor();
5791 dec_cursor();
5792 }
5793 if (c != NUL)
5794 {
5795 /* The space is no longer at the end of the line, delete it. */
5796 del_char(FALSE);
5797 did_add_space = FALSE;
5798 }
5799 }
5800 }
5801}
5802
5803/*
5804 * Find out textwidth to be used for formatting:
5805 * if 'textwidth' option is set, use it
5806 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5807 * if invalid value, use 0.
5808 * Set default to window width (maximum 79) for "gq" operator.
5809 */
5810 int
5811comp_textwidth(ff)
5812 int ff; /* force formatting (for "Q" command) */
5813{
5814 int textwidth;
5815
5816 textwidth = curbuf->b_p_tw;
5817 if (textwidth == 0 && curbuf->b_p_wm)
5818 {
5819 /* The width is the window width minus 'wrapmargin' minus all the
5820 * things that add to the margin. */
5821 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5822#ifdef FEAT_CMDWIN
5823 if (cmdwin_type != 0)
5824 textwidth -= 1;
5825#endif
5826#ifdef FEAT_FOLDING
5827 textwidth -= curwin->w_p_fdc;
5828#endif
5829#ifdef FEAT_SIGNS
5830 if (curwin->w_buffer->b_signlist != NULL
5831# ifdef FEAT_NETBEANS_INTG
5832 || usingNetbeans
5833# endif
5834 )
5835 textwidth -= 1;
5836#endif
5837 if (curwin->w_p_nu)
5838 textwidth -= 8;
5839 }
5840 if (textwidth < 0)
5841 textwidth = 0;
5842 if (ff && textwidth == 0)
5843 {
5844 textwidth = W_WIDTH(curwin) - 1;
5845 if (textwidth > 79)
5846 textwidth = 79;
5847 }
5848 return textwidth;
5849}
5850
5851/*
5852 * Put a character in the redo buffer, for when just after a CTRL-V.
5853 */
5854 static void
5855redo_literal(c)
5856 int c;
5857{
5858 char_u buf[10];
5859
5860 /* Only digits need special treatment. Translate them into a string of
5861 * three digits. */
5862 if (VIM_ISDIGIT(c))
5863 {
5864 sprintf((char *)buf, "%03d", c);
5865 AppendToRedobuff(buf);
5866 }
5867 else
5868 AppendCharToRedobuff(c);
5869}
5870
5871/*
5872 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005873 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 */
5875 static void
5876start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005877 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 if (!arrow_used) /* something has been inserted */
5880 {
5881 AppendToRedobuff(ESC_STR);
5882 stop_insert(end_insert_pos, FALSE);
5883 arrow_used = TRUE; /* this means we stopped the current insert */
5884 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005885#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005886 check_spell_redraw();
5887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888}
5889
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005890#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005891/*
5892 * If we skipped highlighting word at cursor, do it now.
5893 * It may be skipped again, thus reset spell_redraw_lnum first.
5894 */
5895 static void
5896check_spell_redraw()
5897{
5898 if (spell_redraw_lnum != 0)
5899 {
5900 linenr_T lnum = spell_redraw_lnum;
5901
5902 spell_redraw_lnum = 0;
5903 redrawWinline(lnum, FALSE);
5904 }
5905}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005906
5907/*
5908 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5909 * spelled word, if there is one.
5910 */
5911 static void
5912spell_back_to_badword()
5913{
5914 pos_T tpos = curwin->w_cursor;
5915
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005916 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005917 if (curwin->w_cursor.col != tpos.col)
5918 start_arrow(&tpos);
5919}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005920#endif
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922/*
5923 * stop_arrow() is called before a change is made in insert mode.
5924 * If an arrow key has been used, start a new insertion.
5925 * Returns FAIL if undo is impossible, shouldn't insert then.
5926 */
5927 int
5928stop_arrow()
5929{
5930 if (arrow_used)
5931 {
5932 if (u_save_cursor() == OK)
5933 {
5934 arrow_used = FALSE;
5935 ins_need_undo = FALSE;
5936 }
5937 Insstart = curwin->w_cursor; /* new insertion starts here */
5938 Insstart_textlen = linetabsize(ml_get_curline());
5939 ai_col = 0;
5940#ifdef FEAT_VREPLACE
5941 if (State & VREPLACE_FLAG)
5942 {
5943 orig_line_count = curbuf->b_ml.ml_line_count;
5944 vr_lines_changed = 1;
5945 }
5946#endif
5947 ResetRedobuff();
5948 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005949 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 }
5951 else if (ins_need_undo)
5952 {
5953 if (u_save_cursor() == OK)
5954 ins_need_undo = FALSE;
5955 }
5956
5957#ifdef FEAT_FOLDING
5958 /* Always open fold at the cursor line when inserting something. */
5959 foldOpenCursor();
5960#endif
5961
5962 return (arrow_used || ins_need_undo ? FAIL : OK);
5963}
5964
5965/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005966 * Do a few things to stop inserting.
5967 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
5968 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 */
5970 static void
5971stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005972 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005973 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005975 int cc;
5976 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 stop_redo_ins();
5979 replace_flush(); /* abandon replace stack */
5980
5981 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005982 * Save the inserted text for later redo with ^@ and CTRL-A.
5983 * Don't do it when "restart_edit" was set and nothing was inserted,
5984 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005986 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005987 if (did_restart_edit == 0 || (ptr != NULL
5988 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005989 {
5990 vim_free(last_insert);
5991 last_insert = ptr;
5992 last_insert_skip = new_insert_skip;
5993 }
5994 else
5995 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005997 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 {
5999 /* Auto-format now. It may seem strange to do this when stopping an
6000 * insertion (or moving the cursor), but it's required when appending
6001 * a line and having it end in a space. But only do it when something
6002 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006003 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006005 pos_T tpos = curwin->w_cursor;
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 /* When the cursor is at the end of the line after a space the
6008 * formatting will move it to the following word. Avoid that by
6009 * moving the cursor onto the space. */
6010 cc = 'x';
6011 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6012 {
6013 dec_cursor();
6014 cc = gchar_cursor();
6015 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006016 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 }
6018
6019 auto_format(TRUE, FALSE);
6020
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006021 if (vim_iswhite(cc))
6022 {
6023 if (gchar_cursor() != NUL)
6024 inc_cursor();
6025#ifdef FEAT_VIRTUALEDIT
6026 /* If the cursor is still at the same character, also keep
6027 * the "coladd". */
6028 if (gchar_cursor() == NUL
6029 && curwin->w_cursor.lnum == tpos.lnum
6030 && curwin->w_cursor.col == tpos.col)
6031 curwin->w_cursor.coladd = tpos.coladd;
6032#endif
6033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035
6036 /* If a space was inserted for auto-formatting, remove it now. */
6037 check_auto_format(TRUE);
6038
6039 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006040 * of the line, and put the cursor back.
6041 * Do this when ESC was used or moving the cursor up/down. */
6042 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006043 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006045 pos_T tpos = curwin->w_cursor;
6046
6047 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006048 for (;;)
6049 {
6050 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6051 --curwin->w_cursor.col;
6052 cc = gchar_cursor();
6053 if (!vim_iswhite(cc))
6054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006056 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006057 if (curwin->w_cursor.lnum != tpos.lnum)
6058 curwin->w_cursor = tpos;
6059 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6061
6062#ifdef FEAT_VISUAL
6063 /* <C-S-Right> may have started Visual mode, adjust the position for
6064 * deleted characters. */
6065 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6066 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006067 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (VIsual.col > (colnr_T)cc)
6069 {
6070 VIsual.col = cc;
6071# ifdef FEAT_VIRTUALEDIT
6072 VIsual.coladd = 0;
6073# endif
6074 }
6075 }
6076#endif
6077 }
6078 }
6079 did_ai = FALSE;
6080#ifdef FEAT_SMARTINDENT
6081 did_si = FALSE;
6082 can_si = FALSE;
6083 can_si_back = FALSE;
6084#endif
6085
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006086 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6087 * now in a different buffer. */
6088 if (end_insert_pos != NULL)
6089 {
6090 curbuf->b_op_start = Insstart;
6091 curbuf->b_op_end = *end_insert_pos;
6092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093}
6094
6095/*
6096 * Set the last inserted text to a single character.
6097 * Used for the replace command.
6098 */
6099 void
6100set_last_insert(c)
6101 int c;
6102{
6103 char_u *s;
6104
6105 vim_free(last_insert);
6106#ifdef FEAT_MBYTE
6107 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6108#else
6109 last_insert = alloc(6);
6110#endif
6111 if (last_insert != NULL)
6112 {
6113 s = last_insert;
6114 /* Use the CTRL-V only when entering a special char */
6115 if (c < ' ' || c == DEL)
6116 *s++ = Ctrl_V;
6117 s = add_char2buf(c, s);
6118 *s++ = ESC;
6119 *s++ = NUL;
6120 last_insert_skip = 0;
6121 }
6122}
6123
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006124#if defined(EXITFREE) || defined(PROTO)
6125 void
6126free_last_insert()
6127{
6128 vim_free(last_insert);
6129 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006130 vim_free(compl_orig_text);
6131 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006132}
6133#endif
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135/*
6136 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6137 * and CSI. Handle multi-byte characters.
6138 * Returns a pointer to after the added bytes.
6139 */
6140 char_u *
6141add_char2buf(c, s)
6142 int c;
6143 char_u *s;
6144{
6145#ifdef FEAT_MBYTE
6146 char_u temp[MB_MAXBYTES];
6147 int i;
6148 int len;
6149
6150 len = (*mb_char2bytes)(c, temp);
6151 for (i = 0; i < len; ++i)
6152 {
6153 c = temp[i];
6154#endif
6155 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6156 if (c == K_SPECIAL)
6157 {
6158 *s++ = K_SPECIAL;
6159 *s++ = KS_SPECIAL;
6160 *s++ = KE_FILLER;
6161 }
6162#ifdef FEAT_GUI
6163 else if (c == CSI)
6164 {
6165 *s++ = CSI;
6166 *s++ = KS_EXTRA;
6167 *s++ = (int)KE_CSI;
6168 }
6169#endif
6170 else
6171 *s++ = c;
6172#ifdef FEAT_MBYTE
6173 }
6174#endif
6175 return s;
6176}
6177
6178/*
6179 * move cursor to start of line
6180 * if flags & BL_WHITE move to first non-white
6181 * if flags & BL_SOL move to first non-white if startofline is set,
6182 * otherwise keep "curswant" column
6183 * if flags & BL_FIX don't leave the cursor on a NUL.
6184 */
6185 void
6186beginline(flags)
6187 int flags;
6188{
6189 if ((flags & BL_SOL) && !p_sol)
6190 coladvance(curwin->w_curswant);
6191 else
6192 {
6193 curwin->w_cursor.col = 0;
6194#ifdef FEAT_VIRTUALEDIT
6195 curwin->w_cursor.coladd = 0;
6196#endif
6197
6198 if (flags & (BL_WHITE | BL_SOL))
6199 {
6200 char_u *ptr;
6201
6202 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6203 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6204 ++curwin->w_cursor.col;
6205 }
6206 curwin->w_set_curswant = TRUE;
6207 }
6208}
6209
6210/*
6211 * oneright oneleft cursor_down cursor_up
6212 *
6213 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006214 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 * Return OK when successful, FAIL when we hit a line of file boundary.
6216 */
6217
6218 int
6219oneright()
6220{
6221 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
6224#ifdef FEAT_VIRTUALEDIT
6225 if (virtual_active())
6226 {
6227 pos_T prevpos = curwin->w_cursor;
6228
6229 /* Adjust for multi-wide char (excluding TAB) */
6230 ptr = ml_get_cursor();
6231 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006232# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006234# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 ))
6238 ? ptr2cells(ptr) : 1));
6239 curwin->w_set_curswant = TRUE;
6240 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6241 return (prevpos.col != curwin->w_cursor.col
6242 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6243 }
6244#endif
6245
6246 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006247 if (*ptr == NUL)
6248 return FAIL; /* already at the very end */
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006251 if (has_mbyte)
6252 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 else
6254#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006255 l = 1;
6256
6257 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6258 * contains "onemore". */
6259 if (ptr[l] == NUL
6260#ifdef FEAT_VIRTUALEDIT
6261 && (ve_flags & VE_ONEMORE) == 0
6262#endif
6263 )
6264 return FAIL;
6265 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 curwin->w_set_curswant = TRUE;
6268 return OK;
6269}
6270
6271 int
6272oneleft()
6273{
6274#ifdef FEAT_VIRTUALEDIT
6275 if (virtual_active())
6276 {
6277 int width;
6278 int v = getviscol();
6279
6280 if (v == 0)
6281 return FAIL;
6282
6283# ifdef FEAT_LINEBREAK
6284 /* We might get stuck on 'showbreak', skip over it. */
6285 width = 1;
6286 for (;;)
6287 {
6288 coladvance(v - width);
6289 /* getviscol() is slow, skip it when 'showbreak' is empty and
6290 * there are no multi-byte characters */
6291 if ((*p_sbr == NUL
6292# ifdef FEAT_MBYTE
6293 && !has_mbyte
6294# endif
6295 ) || getviscol() < v)
6296 break;
6297 ++width;
6298 }
6299# else
6300 coladvance(v - 1);
6301# endif
6302
6303 if (curwin->w_cursor.coladd == 1)
6304 {
6305 char_u *ptr;
6306
6307 /* Adjust for multi-wide char (not a TAB) */
6308 ptr = ml_get_cursor();
6309 if (*ptr != TAB && vim_isprintc(
6310# ifdef FEAT_MBYTE
6311 (*mb_ptr2char)(ptr)
6312# else
6313 *ptr
6314# endif
6315 ) && ptr2cells(ptr) > 1)
6316 curwin->w_cursor.coladd = 0;
6317 }
6318
6319 curwin->w_set_curswant = TRUE;
6320 return OK;
6321 }
6322#endif
6323
6324 if (curwin->w_cursor.col == 0)
6325 return FAIL;
6326
6327 curwin->w_set_curswant = TRUE;
6328 --curwin->w_cursor.col;
6329
6330#ifdef FEAT_MBYTE
6331 /* if the character on the left of the current cursor is a multi-byte
6332 * character, move to its first byte */
6333 if (has_mbyte)
6334 mb_adjust_cursor();
6335#endif
6336 return OK;
6337}
6338
6339 int
6340cursor_up(n, upd_topline)
6341 long n;
6342 int upd_topline; /* When TRUE: update topline */
6343{
6344 linenr_T lnum;
6345
6346 if (n > 0)
6347 {
6348 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006349 /* This fails if the cursor is already in the first line or the count
6350 * is larger than the line number and '-' is in 'cpoptions' */
6351 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 return FAIL;
6353 if (n >= lnum)
6354 lnum = 1;
6355 else
6356#ifdef FEAT_FOLDING
6357 if (hasAnyFolding(curwin))
6358 {
6359 /*
6360 * Count each sequence of folded lines as one logical line.
6361 */
6362 /* go to the the start of the current fold */
6363 (void)hasFolding(lnum, &lnum, NULL);
6364
6365 while (n--)
6366 {
6367 /* move up one line */
6368 --lnum;
6369 if (lnum <= 1)
6370 break;
6371 /* If we entered a fold, move to the beginning, unless in
6372 * Insert mode or when 'foldopen' contains "all": it will open
6373 * in a moment. */
6374 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6375 (void)hasFolding(lnum, &lnum, NULL);
6376 }
6377 if (lnum < 1)
6378 lnum = 1;
6379 }
6380 else
6381#endif
6382 lnum -= n;
6383 curwin->w_cursor.lnum = lnum;
6384 }
6385
6386 /* try to advance to the column we want to be at */
6387 coladvance(curwin->w_curswant);
6388
6389 if (upd_topline)
6390 update_topline(); /* make sure curwin->w_topline is valid */
6391
6392 return OK;
6393}
6394
6395/*
6396 * Cursor down a number of logical lines.
6397 */
6398 int
6399cursor_down(n, upd_topline)
6400 long n;
6401 int upd_topline; /* When TRUE: update topline */
6402{
6403 linenr_T lnum;
6404
6405 if (n > 0)
6406 {
6407 lnum = curwin->w_cursor.lnum;
6408#ifdef FEAT_FOLDING
6409 /* Move to last line of fold, will fail if it's the end-of-file. */
6410 (void)hasFolding(lnum, NULL, &lnum);
6411#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006412 /* This fails if the cursor is already in the last line or would move
6413 * beyound the last line and '-' is in 'cpoptions' */
6414 if (lnum >= curbuf->b_ml.ml_line_count
6415 || (lnum + n > curbuf->b_ml.ml_line_count
6416 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 return FAIL;
6418 if (lnum + n >= curbuf->b_ml.ml_line_count)
6419 lnum = curbuf->b_ml.ml_line_count;
6420 else
6421#ifdef FEAT_FOLDING
6422 if (hasAnyFolding(curwin))
6423 {
6424 linenr_T last;
6425
6426 /* count each sequence of folded lines as one logical line */
6427 while (n--)
6428 {
6429 if (hasFolding(lnum, NULL, &last))
6430 lnum = last + 1;
6431 else
6432 ++lnum;
6433 if (lnum >= curbuf->b_ml.ml_line_count)
6434 break;
6435 }
6436 if (lnum > curbuf->b_ml.ml_line_count)
6437 lnum = curbuf->b_ml.ml_line_count;
6438 }
6439 else
6440#endif
6441 lnum += n;
6442 curwin->w_cursor.lnum = lnum;
6443 }
6444
6445 /* try to advance to the column we want to be at */
6446 coladvance(curwin->w_curswant);
6447
6448 if (upd_topline)
6449 update_topline(); /* make sure curwin->w_topline is valid */
6450
6451 return OK;
6452}
6453
6454/*
6455 * Stuff the last inserted text in the read buffer.
6456 * Last_insert actually is a copy of the redo buffer, so we
6457 * first have to remove the command.
6458 */
6459 int
6460stuff_inserted(c, count, no_esc)
6461 int c; /* Command character to be inserted */
6462 long count; /* Repeat this many times */
6463 int no_esc; /* Don't add an ESC at the end */
6464{
6465 char_u *esc_ptr;
6466 char_u *ptr;
6467 char_u *last_ptr;
6468 char_u last = NUL;
6469
6470 ptr = get_last_insert();
6471 if (ptr == NULL)
6472 {
6473 EMSG(_(e_noinstext));
6474 return FAIL;
6475 }
6476
6477 /* may want to stuff the command character, to start Insert mode */
6478 if (c != NUL)
6479 stuffcharReadbuff(c);
6480 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6481 *esc_ptr = NUL; /* remove the ESC */
6482
6483 /* when the last char is either "0" or "^" it will be quoted if no ESC
6484 * comes after it OR if it will inserted more than once and "ptr"
6485 * starts with ^D. -- Acevedo
6486 */
6487 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6488 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6489 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6490 {
6491 last = *last_ptr;
6492 *last_ptr = NUL;
6493 }
6494
6495 do
6496 {
6497 stuffReadbuff(ptr);
6498 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6499 if (last)
6500 stuffReadbuff((char_u *)(last == '0'
6501 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6502 : IF_EB("\026^", CTRL_V_STR "^")));
6503 }
6504 while (--count > 0);
6505
6506 if (last)
6507 *last_ptr = last;
6508
6509 if (esc_ptr != NULL)
6510 *esc_ptr = ESC; /* put the ESC back */
6511
6512 /* may want to stuff a trailing ESC, to get out of Insert mode */
6513 if (!no_esc)
6514 stuffcharReadbuff(ESC);
6515
6516 return OK;
6517}
6518
6519 char_u *
6520get_last_insert()
6521{
6522 if (last_insert == NULL)
6523 return NULL;
6524 return last_insert + last_insert_skip;
6525}
6526
6527/*
6528 * Get last inserted string, and remove trailing <Esc>.
6529 * Returns pointer to allocated memory (must be freed) or NULL.
6530 */
6531 char_u *
6532get_last_insert_save()
6533{
6534 char_u *s;
6535 int len;
6536
6537 if (last_insert == NULL)
6538 return NULL;
6539 s = vim_strsave(last_insert + last_insert_skip);
6540 if (s != NULL)
6541 {
6542 len = (int)STRLEN(s);
6543 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6544 s[len - 1] = NUL;
6545 }
6546 return s;
6547}
6548
6549/*
6550 * Check the word in front of the cursor for an abbreviation.
6551 * Called when the non-id character "c" has been entered.
6552 * When an abbreviation is recognized it is removed from the text and
6553 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6554 */
6555 static int
6556echeck_abbr(c)
6557 int c;
6558{
6559 /* Don't check for abbreviation in paste mode, when disabled and just
6560 * after moving around with cursor keys. */
6561 if (p_paste || no_abbr || arrow_used)
6562 return FALSE;
6563
6564 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6565 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6566}
6567
6568/*
6569 * replace-stack functions
6570 *
6571 * When replacing characters, the replaced characters are remembered for each
6572 * new character. This is used to re-insert the old text when backspacing.
6573 *
6574 * There is a NUL headed list of characters for each character that is
6575 * currently in the file after the insertion point. When BS is used, one NUL
6576 * headed list is put back for the deleted character.
6577 *
6578 * For a newline, there are two NUL headed lists. One contains the characters
6579 * that the NL replaced. The extra one stores the characters after the cursor
6580 * that were deleted (always white space).
6581 *
6582 * Replace_offset is normally 0, in which case replace_push will add a new
6583 * character at the end of the stack. If replace_offset is not 0, that many
6584 * characters will be left on the stack above the newly inserted character.
6585 */
6586
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006587static char_u *replace_stack = NULL;
6588static long replace_stack_nr = 0; /* next entry in replace stack */
6589static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591 void
6592replace_push(c)
6593 int c; /* character that is replaced (NUL is none) */
6594{
6595 char_u *p;
6596
6597 if (replace_stack_nr < replace_offset) /* nothing to do */
6598 return;
6599 if (replace_stack_len <= replace_stack_nr)
6600 {
6601 replace_stack_len += 50;
6602 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6603 if (p == NULL) /* out of memory */
6604 {
6605 replace_stack_len -= 50;
6606 return;
6607 }
6608 if (replace_stack != NULL)
6609 {
6610 mch_memmove(p, replace_stack,
6611 (size_t)(replace_stack_nr * sizeof(char_u)));
6612 vim_free(replace_stack);
6613 }
6614 replace_stack = p;
6615 }
6616 p = replace_stack + replace_stack_nr - replace_offset;
6617 if (replace_offset)
6618 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6619 *p = c;
6620 ++replace_stack_nr;
6621}
6622
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006623#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624/*
6625 * call replace_push(c) with replace_offset set to the first NUL.
6626 */
6627 static void
6628replace_push_off(c)
6629 int c;
6630{
6631 char_u *p;
6632
6633 p = replace_stack + replace_stack_nr;
6634 for (replace_offset = 1; replace_offset < replace_stack_nr;
6635 ++replace_offset)
6636 if (*--p == NUL)
6637 break;
6638 replace_push(c);
6639 replace_offset = 0;
6640}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643/*
6644 * Pop one item from the replace stack.
6645 * return -1 if stack empty
6646 * return replaced character or NUL otherwise
6647 */
6648 static int
6649replace_pop()
6650{
6651 if (replace_stack_nr == 0)
6652 return -1;
6653 return (int)replace_stack[--replace_stack_nr];
6654}
6655
6656/*
6657 * Join the top two items on the replace stack. This removes to "off"'th NUL
6658 * encountered.
6659 */
6660 static void
6661replace_join(off)
6662 int off; /* offset for which NUL to remove */
6663{
6664 int i;
6665
6666 for (i = replace_stack_nr; --i >= 0; )
6667 if (replace_stack[i] == NUL && off-- <= 0)
6668 {
6669 --replace_stack_nr;
6670 mch_memmove(replace_stack + i, replace_stack + i + 1,
6671 (size_t)(replace_stack_nr - i));
6672 return;
6673 }
6674}
6675
6676/*
6677 * Pop bytes from the replace stack until a NUL is found, and insert them
6678 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6679 */
6680 static void
6681replace_pop_ins()
6682{
6683 int cc;
6684 int oldState = State;
6685
6686 State = NORMAL; /* don't want REPLACE here */
6687 while ((cc = replace_pop()) > 0)
6688 {
6689#ifdef FEAT_MBYTE
6690 mb_replace_pop_ins(cc);
6691#else
6692 ins_char(cc);
6693#endif
6694 dec_cursor();
6695 }
6696 State = oldState;
6697}
6698
6699#ifdef FEAT_MBYTE
6700/*
6701 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6702 * indicates a multi-byte char, pop the other bytes too.
6703 */
6704 static void
6705mb_replace_pop_ins(cc)
6706 int cc;
6707{
6708 int n;
6709 char_u buf[MB_MAXBYTES];
6710 int i;
6711 int c;
6712
6713 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6714 {
6715 buf[0] = cc;
6716 for (i = 1; i < n; ++i)
6717 buf[i] = replace_pop();
6718 ins_bytes_len(buf, n);
6719 }
6720 else
6721 ins_char(cc);
6722
6723 if (enc_utf8)
6724 /* Handle composing chars. */
6725 for (;;)
6726 {
6727 c = replace_pop();
6728 if (c == -1) /* stack empty */
6729 break;
6730 if ((n = MB_BYTE2LEN(c)) == 1)
6731 {
6732 /* Not a multi-byte char, put it back. */
6733 replace_push(c);
6734 break;
6735 }
6736 else
6737 {
6738 buf[0] = c;
6739 for (i = 1; i < n; ++i)
6740 buf[i] = replace_pop();
6741 if (utf_iscomposing(utf_ptr2char(buf)))
6742 ins_bytes_len(buf, n);
6743 else
6744 {
6745 /* Not a composing char, put it back. */
6746 for (i = n - 1; i >= 0; --i)
6747 replace_push(buf[i]);
6748 break;
6749 }
6750 }
6751 }
6752}
6753#endif
6754
6755/*
6756 * make the replace stack empty
6757 * (called when exiting replace mode)
6758 */
6759 static void
6760replace_flush()
6761{
6762 vim_free(replace_stack);
6763 replace_stack = NULL;
6764 replace_stack_len = 0;
6765 replace_stack_nr = 0;
6766}
6767
6768/*
6769 * Handle doing a BS for one character.
6770 * cc < 0: replace stack empty, just move cursor
6771 * cc == 0: character was inserted, delete it
6772 * cc > 0: character was replaced, put cc (first byte of original char) back
6773 * and check for more characters to be put back
6774 */
6775 static void
6776replace_do_bs()
6777{
6778 int cc;
6779#ifdef FEAT_VREPLACE
6780 int orig_len = 0;
6781 int ins_len;
6782 int orig_vcols = 0;
6783 colnr_T start_vcol;
6784 char_u *p;
6785 int i;
6786 int vcol;
6787#endif
6788
6789 cc = replace_pop();
6790 if (cc > 0)
6791 {
6792#ifdef FEAT_VREPLACE
6793 if (State & VREPLACE_FLAG)
6794 {
6795 /* Get the number of screen cells used by the character we are
6796 * going to delete. */
6797 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6798 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6799 }
6800#endif
6801#ifdef FEAT_MBYTE
6802 if (has_mbyte)
6803 {
6804 del_char(FALSE);
6805# ifdef FEAT_VREPLACE
6806 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006807 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808# endif
6809 replace_push(cc);
6810 }
6811 else
6812#endif
6813 {
6814 pchar_cursor(cc);
6815#ifdef FEAT_VREPLACE
6816 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006817 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818#endif
6819 }
6820 replace_pop_ins();
6821
6822#ifdef FEAT_VREPLACE
6823 if (State & VREPLACE_FLAG)
6824 {
6825 /* Get the number of screen cells used by the inserted characters */
6826 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006827 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 vcol = start_vcol;
6829 for (i = 0; i < ins_len; ++i)
6830 {
6831 vcol += chartabsize(p + i, vcol);
6832#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006833 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834#endif
6835 }
6836 vcol -= start_vcol;
6837
6838 /* Delete spaces that were inserted after the cursor to keep the
6839 * text aligned. */
6840 curwin->w_cursor.col += ins_len;
6841 while (vcol > orig_vcols && gchar_cursor() == ' ')
6842 {
6843 del_char(FALSE);
6844 ++orig_vcols;
6845 }
6846 curwin->w_cursor.col -= ins_len;
6847 }
6848#endif
6849
6850 /* mark the buffer as changed and prepare for displaying */
6851 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6852 }
6853 else if (cc == 0)
6854 (void)del_char(FALSE);
6855}
6856
6857#ifdef FEAT_CINDENT
6858/*
6859 * Return TRUE if C-indenting is on.
6860 */
6861 static int
6862cindent_on()
6863{
6864 return (!p_paste && (curbuf->b_p_cin
6865# ifdef FEAT_EVAL
6866 || *curbuf->b_p_inde != NUL
6867# endif
6868 ));
6869}
6870#endif
6871
6872#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6873/*
6874 * Re-indent the current line, based on the current contents of it and the
6875 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6876 * confused what all the part that handles Control-T is doing that I'm not.
6877 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6878 */
6879
6880 void
6881fixthisline(get_the_indent)
6882 int (*get_the_indent) __ARGS((void));
6883{
6884 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6885 if (linewhite(curwin->w_cursor.lnum))
6886 did_ai = TRUE; /* delete the indent if the line stays empty */
6887}
6888
6889 void
6890fix_indent()
6891{
6892 if (p_paste)
6893 return;
6894# ifdef FEAT_LISP
6895 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6896 fixthisline(get_lisp_indent);
6897# endif
6898# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6899 else
6900# endif
6901# ifdef FEAT_CINDENT
6902 if (cindent_on())
6903 do_c_expr_indent();
6904# endif
6905}
6906
6907#endif
6908
6909#ifdef FEAT_CINDENT
6910/*
6911 * return TRUE if 'cinkeys' contains the key "keytyped",
6912 * when == '*': Only if key is preceded with '*' (indent before insert)
6913 * when == '!': Only if key is prededed with '!' (don't insert)
6914 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6915 *
6916 * "keytyped" can have a few special values:
6917 * KEY_OPEN_FORW
6918 * KEY_OPEN_BACK
6919 * KEY_COMPLETE just finished completion.
6920 *
6921 * If line_is_empty is TRUE accept keys with '0' before them.
6922 */
6923 int
6924in_cinkeys(keytyped, when, line_is_empty)
6925 int keytyped;
6926 int when;
6927 int line_is_empty;
6928{
6929 char_u *look;
6930 int try_match;
6931 int try_match_word;
6932 char_u *p;
6933 char_u *line;
6934 int icase;
6935 int i;
6936
6937#ifdef FEAT_EVAL
6938 if (*curbuf->b_p_inde != NUL)
6939 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6940 else
6941#endif
6942 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6943 while (*look)
6944 {
6945 /*
6946 * Find out if we want to try a match with this key, depending on
6947 * 'when' and a '*' or '!' before the key.
6948 */
6949 switch (when)
6950 {
6951 case '*': try_match = (*look == '*'); break;
6952 case '!': try_match = (*look == '!'); break;
6953 default: try_match = (*look != '*'); break;
6954 }
6955 if (*look == '*' || *look == '!')
6956 ++look;
6957
6958 /*
6959 * If there is a '0', only accept a match if the line is empty.
6960 * But may still match when typing last char of a word.
6961 */
6962 if (*look == '0')
6963 {
6964 try_match_word = try_match;
6965 if (!line_is_empty)
6966 try_match = FALSE;
6967 ++look;
6968 }
6969 else
6970 try_match_word = FALSE;
6971
6972 /*
6973 * does it look like a control character?
6974 */
6975 if (*look == '^'
6976#ifdef EBCDIC
6977 && (Ctrl_chr(look[1]) != 0)
6978#else
6979 && look[1] >= '?' && look[1] <= '_'
6980#endif
6981 )
6982 {
6983 if (try_match && keytyped == Ctrl_chr(look[1]))
6984 return TRUE;
6985 look += 2;
6986 }
6987 /*
6988 * 'o' means "o" command, open forward.
6989 * 'O' means "O" command, open backward.
6990 */
6991 else if (*look == 'o')
6992 {
6993 if (try_match && keytyped == KEY_OPEN_FORW)
6994 return TRUE;
6995 ++look;
6996 }
6997 else if (*look == 'O')
6998 {
6999 if (try_match && keytyped == KEY_OPEN_BACK)
7000 return TRUE;
7001 ++look;
7002 }
7003
7004 /*
7005 * 'e' means to check for "else" at start of line and just before the
7006 * cursor.
7007 */
7008 else if (*look == 'e')
7009 {
7010 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7011 {
7012 p = ml_get_curline();
7013 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7014 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7015 return TRUE;
7016 }
7017 ++look;
7018 }
7019
7020 /*
7021 * ':' only causes an indent if it is at the end of a label or case
7022 * statement, or when it was before typing the ':' (to fix
7023 * class::method for C++).
7024 */
7025 else if (*look == ':')
7026 {
7027 if (try_match && keytyped == ':')
7028 {
7029 p = ml_get_curline();
7030 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7031 return TRUE;
7032 if (curwin->w_cursor.col > 2
7033 && p[curwin->w_cursor.col - 1] == ':'
7034 && p[curwin->w_cursor.col - 2] == ':')
7035 {
7036 p[curwin->w_cursor.col - 1] = ' ';
7037 i = (cin_iscase(p) || cin_isscopedecl(p)
7038 || cin_islabel(30));
7039 p = ml_get_curline();
7040 p[curwin->w_cursor.col - 1] = ':';
7041 if (i)
7042 return TRUE;
7043 }
7044 }
7045 ++look;
7046 }
7047
7048
7049 /*
7050 * Is it a key in <>, maybe?
7051 */
7052 else if (*look == '<')
7053 {
7054 if (try_match)
7055 {
7056 /*
7057 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7058 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7059 * >, *, : and ! keys if they really really want to.
7060 */
7061 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7062 && keytyped == look[1])
7063 return TRUE;
7064
7065 if (keytyped == get_special_key_code(look + 1))
7066 return TRUE;
7067 }
7068 while (*look && *look != '>')
7069 look++;
7070 while (*look == '>')
7071 look++;
7072 }
7073
7074 /*
7075 * Is it a word: "=word"?
7076 */
7077 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7078 {
7079 ++look;
7080 if (*look == '~')
7081 {
7082 icase = TRUE;
7083 ++look;
7084 }
7085 else
7086 icase = FALSE;
7087 p = vim_strchr(look, ',');
7088 if (p == NULL)
7089 p = look + STRLEN(look);
7090 if ((try_match || try_match_word)
7091 && curwin->w_cursor.col >= (colnr_T)(p - look))
7092 {
7093 int match = FALSE;
7094
7095#ifdef FEAT_INS_EXPAND
7096 if (keytyped == KEY_COMPLETE)
7097 {
7098 char_u *s;
7099
7100 /* Just completed a word, check if it starts with "look".
7101 * search back for the start of a word. */
7102 line = ml_get_curline();
7103# ifdef FEAT_MBYTE
7104 if (has_mbyte)
7105 {
7106 char_u *n;
7107
7108 for (s = line + curwin->w_cursor.col; s > line; s = n)
7109 {
7110 n = mb_prevptr(line, s);
7111 if (!vim_iswordp(n))
7112 break;
7113 }
7114 }
7115 else
7116# endif
7117 for (s = line + curwin->w_cursor.col; s > line; --s)
7118 if (!vim_iswordc(s[-1]))
7119 break;
7120 if (s + (p - look) <= line + curwin->w_cursor.col
7121 && (icase
7122 ? MB_STRNICMP(s, look, p - look)
7123 : STRNCMP(s, look, p - look)) == 0)
7124 match = TRUE;
7125 }
7126 else
7127#endif
7128 /* TODO: multi-byte */
7129 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7130 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7131 {
7132 line = ml_get_cursor();
7133 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7134 || !vim_iswordc(line[-(p - look) - 1]))
7135 && (icase
7136 ? MB_STRNICMP(line - (p - look), look, p - look)
7137 : STRNCMP(line - (p - look), look, p - look))
7138 == 0)
7139 match = TRUE;
7140 }
7141 if (match && try_match_word && !try_match)
7142 {
7143 /* "0=word": Check if there are only blanks before the
7144 * word. */
7145 line = ml_get_curline();
7146 if ((int)(skipwhite(line) - line) !=
7147 (int)(curwin->w_cursor.col - (p - look)))
7148 match = FALSE;
7149 }
7150 if (match)
7151 return TRUE;
7152 }
7153 look = p;
7154 }
7155
7156 /*
7157 * ok, it's a boring generic character.
7158 */
7159 else
7160 {
7161 if (try_match && *look == keytyped)
7162 return TRUE;
7163 ++look;
7164 }
7165
7166 /*
7167 * Skip over ", ".
7168 */
7169 look = skip_to_option_part(look);
7170 }
7171 return FALSE;
7172}
7173#endif /* FEAT_CINDENT */
7174
7175#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7176/*
7177 * Map Hebrew keyboard when in hkmap mode.
7178 */
7179 int
7180hkmap(c)
7181 int c;
7182{
7183 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7184 {
7185 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7186 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7187 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7188 static char_u map[26] =
7189 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7190 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7191 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7192 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7193 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7194 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7195 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7196 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7197 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7198
7199 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7200 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7201 /* '-1'='sofit' */
7202 else if (c == 'x')
7203 return 'X';
7204 else if (c == 'q')
7205 return '\''; /* {geresh}={'} */
7206 else if (c == 246)
7207 return ' '; /* \"o --> ' ' for a german keyboard */
7208 else if (c == 228)
7209 return ' '; /* \"a --> ' ' -- / -- */
7210 else if (c == 252)
7211 return ' '; /* \"u --> ' ' -- / -- */
7212#ifdef EBCDIC
7213 else if (islower(c))
7214#else
7215 /* NOTE: islower() does not do the right thing for us on Linux so we
7216 * do this the same was as 5.7 and previous, so it works correctly on
7217 * all systems. Specifically, the e.g. Delete and Arrow keys are
7218 * munged and won't work if e.g. searching for Hebrew text.
7219 */
7220 else if (c >= 'a' && c <= 'z')
7221#endif
7222 return (int)(map[CharOrdLow(c)] + p_aleph);
7223 else
7224 return c;
7225 }
7226 else
7227 {
7228 switch (c)
7229 {
7230 case '`': return ';';
7231 case '/': return '.';
7232 case '\'': return ',';
7233 case 'q': return '/';
7234 case 'w': return '\'';
7235
7236 /* Hebrew letters - set offset from 'a' */
7237 case ',': c = '{'; break;
7238 case '.': c = 'v'; break;
7239 case ';': c = 't'; break;
7240 default: {
7241 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7242
7243#ifdef EBCDIC
7244 /* see note about islower() above */
7245 if (!islower(c))
7246#else
7247 if (c < 'a' || c > 'z')
7248#endif
7249 return c;
7250 c = str[CharOrdLow(c)];
7251 break;
7252 }
7253 }
7254
7255 return (int)(CharOrdLow(c) + p_aleph);
7256 }
7257}
7258#endif
7259
7260 static void
7261ins_reg()
7262{
7263 int need_redraw = FALSE;
7264 int regname;
7265 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007266#ifdef FEAT_VISUAL
7267 int vis_active = VIsual_active;
7268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 /*
7271 * If we are going to wait for a character, show a '"'.
7272 */
7273 pc_status = PC_STATUS_UNSET;
7274 if (redrawing() && !char_avail())
7275 {
7276 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007277 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
7279 edit_putchar('"', TRUE);
7280#ifdef FEAT_CMDL_INFO
7281 add_to_showcmd_c(Ctrl_R);
7282#endif
7283 }
7284
7285#ifdef USE_ON_FLY_SCROLL
7286 dont_scroll = TRUE; /* disallow scrolling here */
7287#endif
7288
7289 /*
7290 * Don't map the register name. This also prevents the mode message to be
7291 * deleted when ESC is hit.
7292 */
7293 ++no_mapping;
7294 regname = safe_vgetc();
7295#ifdef FEAT_LANGMAP
7296 LANGMAP_ADJUST(regname, TRUE);
7297#endif
7298 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7299 {
7300 /* Get a third key for literal register insertion */
7301 literally = regname;
7302#ifdef FEAT_CMDL_INFO
7303 add_to_showcmd_c(literally);
7304#endif
7305 regname = safe_vgetc();
7306#ifdef FEAT_LANGMAP
7307 LANGMAP_ADJUST(regname, TRUE);
7308#endif
7309 }
7310 --no_mapping;
7311
7312#ifdef FEAT_EVAL
7313 /*
7314 * Don't call u_sync() while getting the expression,
7315 * evaluating it or giving an error message for it!
7316 */
7317 ++no_u_sync;
7318 if (regname == '=')
7319 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007320# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007324# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 /* Restore the Input Method. */
7326 if (im_on)
7327 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007330 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7331 {
7332 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 else
7336 {
7337#endif
7338 if (literally == Ctrl_O || literally == Ctrl_P)
7339 {
7340 /* Append the command to the redo buffer. */
7341 AppendCharToRedobuff(Ctrl_R);
7342 AppendCharToRedobuff(literally);
7343 AppendCharToRedobuff(regname);
7344
7345 do_put(regname, BACKWARD, 1L,
7346 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7347 }
7348 else if (insert_reg(regname, literally) == FAIL)
7349 {
7350 vim_beep();
7351 need_redraw = TRUE; /* remove the '"' */
7352 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007353 else if (stop_insert_mode)
7354 /* When the '=' register was used and a function was invoked that
7355 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7356 * insert anything, need to remove the '"' */
7357 need_redraw = TRUE;
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#ifdef FEAT_EVAL
7360 }
7361 --no_u_sync;
7362#endif
7363#ifdef FEAT_CMDL_INFO
7364 clear_showcmd();
7365#endif
7366
7367 /* If the inserted register is empty, we need to remove the '"' */
7368 if (need_redraw || stuff_empty())
7369 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007370
7371#ifdef FEAT_VISUAL
7372 /* Disallow starting Visual mode here, would get a weird mode. */
7373 if (!vis_active && VIsual_active)
7374 end_visual_mode();
7375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376}
7377
7378/*
7379 * CTRL-G commands in Insert mode.
7380 */
7381 static void
7382ins_ctrl_g()
7383{
7384 int c;
7385
7386#ifdef FEAT_INS_EXPAND
7387 /* Right after CTRL-X the cursor will be after the ruler. */
7388 setcursor();
7389#endif
7390
7391 /*
7392 * Don't map the second key. This also prevents the mode message to be
7393 * deleted when ESC is hit.
7394 */
7395 ++no_mapping;
7396 c = safe_vgetc();
7397 --no_mapping;
7398 switch (c)
7399 {
7400 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7401 case K_UP:
7402 case Ctrl_K:
7403 case 'k': ins_up(TRUE);
7404 break;
7405
7406 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7407 case K_DOWN:
7408 case Ctrl_J:
7409 case 'j': ins_down(TRUE);
7410 break;
7411
7412 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007413 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007415
7416 /* Need to reset Insstart, esp. because a BS that joins
7417 * aline to the previous one must save for undo. */
7418 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 break;
7420
7421 /* Unknown CTRL-G command, reserved for future expansion. */
7422 default: vim_beep();
7423 }
7424}
7425
7426/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007427 * CTRL-^ in Insert mode.
7428 */
7429 static void
7430ins_ctrl_hat()
7431{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007432 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007433 {
7434 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7435 if (State & LANGMAP)
7436 {
7437 curbuf->b_p_iminsert = B_IMODE_NONE;
7438 State &= ~LANGMAP;
7439 }
7440 else
7441 {
7442 curbuf->b_p_iminsert = B_IMODE_LMAP;
7443 State |= LANGMAP;
7444#ifdef USE_IM_CONTROL
7445 im_set_active(FALSE);
7446#endif
7447 }
7448 }
7449#ifdef USE_IM_CONTROL
7450 else
7451 {
7452 /* There are no ":lmap" mappings, toggle IM */
7453 if (im_get_status())
7454 {
7455 curbuf->b_p_iminsert = B_IMODE_NONE;
7456 im_set_active(FALSE);
7457 }
7458 else
7459 {
7460 curbuf->b_p_iminsert = B_IMODE_IM;
7461 State &= ~LANGMAP;
7462 im_set_active(TRUE);
7463 }
7464 }
7465#endif
7466 set_iminsert_global();
7467 showmode();
7468#ifdef FEAT_GUI
7469 /* may show different cursor shape or color */
7470 if (gui.in_use)
7471 gui_update_cursor(TRUE, FALSE);
7472#endif
7473#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7474 /* Show/unshow value of 'keymap' in status lines. */
7475 status_redraw_curbuf();
7476#endif
7477}
7478
7479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 * Handle ESC in insert mode.
7481 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7482 * insert.
7483 */
7484 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007485ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 long *count;
7487 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007488 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int temp;
7491 static int disabled_redraw = FALSE;
7492
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007493#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007494 check_spell_redraw();
7495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496#if defined(FEAT_HANGULIN)
7497# if defined(ESC_CHG_TO_ENG_MODE)
7498 hangul_input_state_set(0);
7499# endif
7500 if (composing_hangul)
7501 {
7502 push_raw_key(composing_hangul_buffer, 2);
7503 composing_hangul = 0;
7504 }
7505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
7507 temp = curwin->w_cursor.col;
7508 if (disabled_redraw)
7509 {
7510 --RedrawingDisabled;
7511 disabled_redraw = FALSE;
7512 }
7513 if (!arrow_used)
7514 {
7515 /*
7516 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007517 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7518 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 */
7520 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007521 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
7523 /*
7524 * Repeating insert may take a long time. Check for
7525 * interrupt now and then.
7526 */
7527 if (*count > 0)
7528 {
7529 line_breakcheck();
7530 if (got_int)
7531 *count = 0;
7532 }
7533
7534 if (--*count > 0) /* repeat what was typed */
7535 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007536 /* Vi repeats the insert without replacing characters. */
7537 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7538 State &= ~REPLACE_FLAG;
7539
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 (void)start_redo_ins();
7541 if (cmdchar == 'r' || cmdchar == 'v')
7542 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7543 ++RedrawingDisabled;
7544 disabled_redraw = TRUE;
7545 return FALSE; /* repeat the insert */
7546 }
7547 stop_insert(&curwin->w_cursor, TRUE);
7548 undisplay_dollar();
7549 }
7550
7551 /* When an autoindent was removed, curswant stays after the
7552 * indent */
7553 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7554 curwin->w_set_curswant = TRUE;
7555
7556 /* Remember the last Insert position in the '^ mark. */
7557 if (!cmdmod.keepjumps)
7558 curbuf->b_last_insert = curwin->w_cursor;
7559
7560 /*
7561 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007562 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007564 if (!nomove
7565 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566#ifdef FEAT_VIRTUALEDIT
7567 || curwin->w_cursor.coladd > 0
7568#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007569 )
7570 && (restart_edit == NUL
7571 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007573 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007575 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576#ifdef FEAT_RIGHTLEFT
7577 && !revins_on
7578#endif
7579 )
7580 {
7581#ifdef FEAT_VIRTUALEDIT
7582 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7583 {
7584 oneleft();
7585 if (restart_edit != NUL)
7586 ++curwin->w_cursor.coladd;
7587 }
7588 else
7589#endif
7590 {
7591 --curwin->w_cursor.col;
7592#ifdef FEAT_MBYTE
7593 /* Correct cursor for multi-byte character. */
7594 if (has_mbyte)
7595 mb_adjust_cursor();
7596#endif
7597 }
7598 }
7599
7600#ifdef USE_IM_CONTROL
7601 /* Disable IM to allow typing English directly for Normal mode commands.
7602 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7603 * well). */
7604 if (!(State & LANGMAP))
7605 im_save_status(&curbuf->b_p_iminsert);
7606 im_set_active(FALSE);
7607#endif
7608
7609 State = NORMAL;
7610 /* need to position cursor again (e.g. when on a TAB ) */
7611 changed_cline_bef_curs();
7612
7613#ifdef FEAT_MOUSE
7614 setmouse();
7615#endif
7616#ifdef CURSOR_SHAPE
7617 ui_cursor_shape(); /* may show different cursor shape */
7618#endif
7619
7620 /*
7621 * When recording or for CTRL-O, need to display the new mode.
7622 * Otherwise remove the mode message.
7623 */
7624 if (Recording || restart_edit != NUL)
7625 showmode();
7626 else if (p_smd)
7627 MSG("");
7628
7629 return TRUE; /* exit Insert mode */
7630}
7631
7632#ifdef FEAT_RIGHTLEFT
7633/*
7634 * Toggle language: hkmap and revins_on.
7635 * Move to end of reverse inserted text.
7636 */
7637 static void
7638ins_ctrl_()
7639{
7640 if (revins_on && revins_chars && revins_scol >= 0)
7641 {
7642 while (gchar_cursor() != NUL && revins_chars--)
7643 ++curwin->w_cursor.col;
7644 }
7645 p_ri = !p_ri;
7646 revins_on = (State == INSERT && p_ri);
7647 if (revins_on)
7648 {
7649 revins_scol = curwin->w_cursor.col;
7650 revins_legal++;
7651 revins_chars = 0;
7652 undisplay_dollar();
7653 }
7654 else
7655 revins_scol = -1;
7656#ifdef FEAT_FKMAP
7657 if (p_altkeymap)
7658 {
7659 /*
7660 * to be consistent also for redo command, using '.'
7661 * set arrow_used to true and stop it - causing to redo
7662 * characters entered in one mode (normal/reverse insert).
7663 */
7664 arrow_used = TRUE;
7665 (void)stop_arrow();
7666 p_fkmap = curwin->w_p_rl ^ p_ri;
7667 if (p_fkmap && p_ri)
7668 State = INSERT;
7669 }
7670 else
7671#endif
7672 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7673 showmode();
7674}
7675#endif
7676
7677#ifdef FEAT_VISUAL
7678/*
7679 * If 'keymodel' contains "startsel", may start selection.
7680 * Returns TRUE when a CTRL-O and other keys stuffed.
7681 */
7682 static int
7683ins_start_select(c)
7684 int c;
7685{
7686 if (km_startsel)
7687 switch (c)
7688 {
7689 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 case K_PAGEUP:
7692 case K_KPAGEUP:
7693 case K_PAGEDOWN:
7694 case K_KPAGEDOWN:
7695# ifdef MACOS
7696 case K_LEFT:
7697 case K_RIGHT:
7698 case K_UP:
7699 case K_DOWN:
7700 case K_END:
7701 case K_HOME:
7702# endif
7703 if (!(mod_mask & MOD_MASK_SHIFT))
7704 break;
7705 /* FALLTHROUGH */
7706 case K_S_LEFT:
7707 case K_S_RIGHT:
7708 case K_S_UP:
7709 case K_S_DOWN:
7710 case K_S_END:
7711 case K_S_HOME:
7712 /* Start selection right away, the cursor can move with
7713 * CTRL-O when beyond the end of the line. */
7714 start_selection();
7715
7716 /* Execute the key in (insert) Select mode. */
7717 stuffcharReadbuff(Ctrl_O);
7718 if (mod_mask)
7719 {
7720 char_u buf[4];
7721
7722 buf[0] = K_SPECIAL;
7723 buf[1] = KS_MODIFIER;
7724 buf[2] = mod_mask;
7725 buf[3] = NUL;
7726 stuffReadbuff(buf);
7727 }
7728 stuffcharReadbuff(c);
7729 return TRUE;
7730 }
7731 return FALSE;
7732}
7733#endif
7734
7735/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007736 * <Insert> key in Insert mode: toggle insert/remplace mode.
7737 */
7738 static void
7739ins_insert(replaceState)
7740 int replaceState;
7741{
7742#ifdef FEAT_FKMAP
7743 if (p_fkmap && p_ri)
7744 {
7745 beep_flush();
7746 EMSG(farsi_text_3); /* encoded in Farsi */
7747 return;
7748 }
7749#endif
7750
7751#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007752# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007753 set_vim_var_string(VV_INSERTMODE,
7754 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007755# ifdef FEAT_VREPLACE
7756 replaceState == VREPLACE ? "v" :
7757# endif
7758 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007759# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007760 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7761#endif
7762 if (State & REPLACE_FLAG)
7763 State = INSERT | (State & LANGMAP);
7764 else
7765 State = replaceState | (State & LANGMAP);
7766 AppendCharToRedobuff(K_INS);
7767 showmode();
7768#ifdef CURSOR_SHAPE
7769 ui_cursor_shape(); /* may show different cursor shape */
7770#endif
7771}
7772
7773/*
7774 * Pressed CTRL-O in Insert mode.
7775 */
7776 static void
7777ins_ctrl_o()
7778{
7779#ifdef FEAT_VREPLACE
7780 if (State & VREPLACE_FLAG)
7781 restart_edit = 'V';
7782 else
7783#endif
7784 if (State & REPLACE_FLAG)
7785 restart_edit = 'R';
7786 else
7787 restart_edit = 'I';
7788#ifdef FEAT_VIRTUALEDIT
7789 if (virtual_active())
7790 ins_at_eol = FALSE; /* cursor always keeps its column */
7791 else
7792#endif
7793 ins_at_eol = (gchar_cursor() == NUL);
7794}
7795
7796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 * If the cursor is on an indent, ^T/^D insert/delete one
7798 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7799 * Always round the indent to 'shiftwith', this is compatible
7800 * with vi. But vi only supports ^T and ^D after an
7801 * autoindent, we support it everywhere.
7802 */
7803 static void
7804ins_shift(c, lastc)
7805 int c;
7806 int lastc;
7807{
7808 if (stop_arrow() == FAIL)
7809 return;
7810 AppendCharToRedobuff(c);
7811
7812 /*
7813 * 0^D and ^^D: remove all indent.
7814 */
7815 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7816 {
7817 --curwin->w_cursor.col;
7818 (void)del_char(FALSE); /* delete the '^' or '0' */
7819 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7820 if (State & REPLACE_FLAG)
7821 replace_pop_ins();
7822 if (lastc == '^')
7823 old_indent = get_indent(); /* remember curr. indent */
7824 change_indent(INDENT_SET, 0, TRUE, 0);
7825 }
7826 else
7827 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7828
7829 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7830 did_ai = FALSE;
7831#ifdef FEAT_SMARTINDENT
7832 did_si = FALSE;
7833 can_si = FALSE;
7834 can_si_back = FALSE;
7835#endif
7836#ifdef FEAT_CINDENT
7837 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7838#endif
7839}
7840
7841 static void
7842ins_del()
7843{
7844 int temp;
7845
7846 if (stop_arrow() == FAIL)
7847 return;
7848 if (gchar_cursor() == NUL) /* delete newline */
7849 {
7850 temp = curwin->w_cursor.col;
7851 if (!can_bs(BS_EOL) /* only if "eol" included */
7852 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7853 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7854 || do_join(FALSE) == FAIL)
7855 vim_beep();
7856 else
7857 curwin->w_cursor.col = temp;
7858 }
7859 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7860 vim_beep();
7861 did_ai = FALSE;
7862#ifdef FEAT_SMARTINDENT
7863 did_si = FALSE;
7864 can_si = FALSE;
7865 can_si_back = FALSE;
7866#endif
7867 AppendCharToRedobuff(K_DEL);
7868}
7869
7870/*
7871 * Handle Backspace, delete-word and delete-line in Insert mode.
7872 * Return TRUE when backspace was actually used.
7873 */
7874 static int
7875ins_bs(c, mode, inserted_space_p)
7876 int c;
7877 int mode;
7878 int *inserted_space_p;
7879{
7880 linenr_T lnum;
7881 int cc;
7882 int temp = 0; /* init for GCC */
7883 colnr_T mincol;
7884 int did_backspace = FALSE;
7885 int in_indent;
7886 int oldState;
7887#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007888 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889#endif
7890
7891 /*
7892 * can't delete anything in an empty file
7893 * can't backup past first character in buffer
7894 * can't backup past starting point unless 'backspace' > 1
7895 * can backup to a previous line if 'backspace' == 0
7896 */
7897 if ( bufempty()
7898 || (
7899#ifdef FEAT_RIGHTLEFT
7900 !revins_on &&
7901#endif
7902 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7903 || (!can_bs(BS_START)
7904 && (arrow_used
7905 || (curwin->w_cursor.lnum == Insstart.lnum
7906 && curwin->w_cursor.col <= Insstart.col)))
7907 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7908 && curwin->w_cursor.col <= ai_col)
7909 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7910 {
7911 vim_beep();
7912 return FALSE;
7913 }
7914
7915 if (stop_arrow() == FAIL)
7916 return FALSE;
7917 in_indent = inindent(0);
7918#ifdef FEAT_CINDENT
7919 if (in_indent)
7920 can_cindent = FALSE;
7921#endif
7922#ifdef FEAT_COMMENTS
7923 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7924#endif
7925#ifdef FEAT_RIGHTLEFT
7926 if (revins_on) /* put cursor after last inserted char */
7927 inc_cursor();
7928#endif
7929
7930#ifdef FEAT_VIRTUALEDIT
7931 /* Virtualedit:
7932 * BACKSPACE_CHAR eats a virtual space
7933 * BACKSPACE_WORD eats all coladd
7934 * BACKSPACE_LINE eats all coladd and keeps going
7935 */
7936 if (curwin->w_cursor.coladd > 0)
7937 {
7938 if (mode == BACKSPACE_CHAR)
7939 {
7940 --curwin->w_cursor.coladd;
7941 return TRUE;
7942 }
7943 if (mode == BACKSPACE_WORD)
7944 {
7945 curwin->w_cursor.coladd = 0;
7946 return TRUE;
7947 }
7948 curwin->w_cursor.coladd = 0;
7949 }
7950#endif
7951
7952 /*
7953 * delete newline!
7954 */
7955 if (curwin->w_cursor.col == 0)
7956 {
7957 lnum = Insstart.lnum;
7958 if (curwin->w_cursor.lnum == Insstart.lnum
7959#ifdef FEAT_RIGHTLEFT
7960 || revins_on
7961#endif
7962 )
7963 {
7964 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7965 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7966 return FALSE;
7967 --Insstart.lnum;
7968 Insstart.col = MAXCOL;
7969 }
7970 /*
7971 * In replace mode:
7972 * cc < 0: NL was inserted, delete it
7973 * cc >= 0: NL was replaced, put original characters back
7974 */
7975 cc = -1;
7976 if (State & REPLACE_FLAG)
7977 cc = replace_pop(); /* returns -1 if NL was inserted */
7978 /*
7979 * In replace mode, in the line we started replacing, we only move the
7980 * cursor.
7981 */
7982 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7983 {
7984 dec_cursor();
7985 }
7986 else
7987 {
7988#ifdef FEAT_VREPLACE
7989 if (!(State & VREPLACE_FLAG)
7990 || curwin->w_cursor.lnum > orig_line_count)
7991#endif
7992 {
7993 temp = gchar_cursor(); /* remember current char */
7994 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007995
7996 /* When "aw" is in 'formatoptions' we must delete the space at
7997 * the end of the line, otherwise the line will be broken
7998 * again when auto-formatting. */
7999 if (has_format_option(FO_AUTO)
8000 && has_format_option(FO_WHITE_PAR))
8001 {
8002 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8003 TRUE);
8004 int len;
8005
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008006 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008007 if (len > 0 && ptr[len - 1] == ' ')
8008 ptr[len - 1] = NUL;
8009 }
8010
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 (void)do_join(FALSE);
8012 if (temp == NUL && gchar_cursor() != NUL)
8013 inc_cursor();
8014 }
8015#ifdef FEAT_VREPLACE
8016 else
8017 dec_cursor();
8018#endif
8019
8020 /*
8021 * In REPLACE mode we have to put back the text that was replaced
8022 * by the NL. On the replace stack is first a NUL-terminated
8023 * sequence of characters that were deleted and then the
8024 * characters that NL replaced.
8025 */
8026 if (State & REPLACE_FLAG)
8027 {
8028 /*
8029 * Do the next ins_char() in NORMAL state, to
8030 * prevent ins_char() from replacing characters and
8031 * avoiding showmatch().
8032 */
8033 oldState = State;
8034 State = NORMAL;
8035 /*
8036 * restore characters (blanks) deleted after cursor
8037 */
8038 while (cc > 0)
8039 {
8040 temp = curwin->w_cursor.col;
8041#ifdef FEAT_MBYTE
8042 mb_replace_pop_ins(cc);
8043#else
8044 ins_char(cc);
8045#endif
8046 curwin->w_cursor.col = temp;
8047 cc = replace_pop();
8048 }
8049 /* restore the characters that NL replaced */
8050 replace_pop_ins();
8051 State = oldState;
8052 }
8053 }
8054 did_ai = FALSE;
8055 }
8056 else
8057 {
8058 /*
8059 * Delete character(s) before the cursor.
8060 */
8061#ifdef FEAT_RIGHTLEFT
8062 if (revins_on) /* put cursor on last inserted char */
8063 dec_cursor();
8064#endif
8065 mincol = 0;
8066 /* keep indent */
8067 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8068#ifdef FEAT_RIGHTLEFT
8069 && !revins_on
8070#endif
8071 )
8072 {
8073 temp = curwin->w_cursor.col;
8074 beginline(BL_WHITE);
8075 if (curwin->w_cursor.col < (colnr_T)temp)
8076 mincol = curwin->w_cursor.col;
8077 curwin->w_cursor.col = temp;
8078 }
8079
8080 /*
8081 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8082 */
8083 if ( mode == BACKSPACE_CHAR
8084 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008085 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 && (*(ml_get_cursor() - 1) == TAB
8087 || (*(ml_get_cursor() - 1) == ' '
8088 && (!*inserted_space_p
8089 || arrow_used))))))
8090 {
8091 int ts;
8092 colnr_T vcol;
8093 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008094#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
8098 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008099 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 ts = curbuf->b_p_sw;
8101 else
8102 ts = curbuf->b_p_sts;
8103 /* Compute the virtual column where we want to be. Since
8104 * 'showbreak' may get in the way, need to get the last column of
8105 * the previous character. */
8106 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8107 dec_cursor();
8108 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8109 inc_cursor();
8110 want_vcol = (want_vcol / ts) * ts;
8111
8112 /* delete characters until we are at or before want_vcol */
8113 while (vcol > want_vcol
8114 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8115 {
8116 dec_cursor();
8117 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8118 if (State & REPLACE_FLAG)
8119 {
8120 /* Don't delete characters before the insert point when in
8121 * Replace mode */
8122 if (curwin->w_cursor.lnum != Insstart.lnum
8123 || curwin->w_cursor.col >= Insstart.col)
8124 {
8125#if 0 /* what was this for? It causes problems when sw != ts. */
8126 if (State == REPLACE && (int)vcol < want_vcol)
8127 {
8128 (void)del_char(FALSE);
8129 extra = 2; /* don't pop too much */
8130 }
8131 else
8132#endif
8133 replace_do_bs();
8134 }
8135 }
8136 else
8137 (void)del_char(FALSE);
8138 }
8139
8140 /* insert extra spaces until we are at want_vcol */
8141 while (vcol < want_vcol)
8142 {
8143 /* Remember the first char we inserted */
8144 if (curwin->w_cursor.lnum == Insstart.lnum
8145 && curwin->w_cursor.col < Insstart.col)
8146 Insstart.col = curwin->w_cursor.col;
8147
8148#ifdef FEAT_VREPLACE
8149 if (State & VREPLACE_FLAG)
8150 ins_char(' ');
8151 else
8152#endif
8153 {
8154 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008155 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008157#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 if (extra)
8159 replace_push_off(NUL);
8160 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 replace_push(NUL);
8163 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008164#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 if (extra == 2)
8166 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
8169 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8170 }
8171 }
8172
8173 /*
8174 * Delete upto starting point, start of line or previous word.
8175 */
8176 else do
8177 {
8178#ifdef FEAT_RIGHTLEFT
8179 if (!revins_on) /* put cursor on char to be deleted */
8180#endif
8181 dec_cursor();
8182
8183 /* start of word? */
8184 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8185 {
8186 mode = BACKSPACE_WORD_NOT_SPACE;
8187 temp = vim_iswordc(gchar_cursor());
8188 }
8189 /* end of word? */
8190 else if (mode == BACKSPACE_WORD_NOT_SPACE
8191 && (vim_isspace(cc = gchar_cursor())
8192 || vim_iswordc(cc) != temp))
8193 {
8194#ifdef FEAT_RIGHTLEFT
8195 if (!revins_on)
8196#endif
8197 inc_cursor();
8198#ifdef FEAT_RIGHTLEFT
8199 else if (State & REPLACE_FLAG)
8200 dec_cursor();
8201#endif
8202 break;
8203 }
8204 if (State & REPLACE_FLAG)
8205 replace_do_bs();
8206 else
8207 {
8208#ifdef FEAT_MBYTE
8209 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008210 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211#endif
8212 (void)del_char(FALSE);
8213#ifdef FEAT_MBYTE
8214 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008215 * If there are combining characters and 'delcombine' is set
8216 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 * character.
8218 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008219 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 inc_cursor();
8221#endif
8222#ifdef FEAT_RIGHTLEFT
8223 if (revins_chars)
8224 {
8225 revins_chars--;
8226 revins_legal++;
8227 }
8228 if (revins_on && gchar_cursor() == NUL)
8229 break;
8230#endif
8231 }
8232 /* Just a single backspace?: */
8233 if (mode == BACKSPACE_CHAR)
8234 break;
8235 } while (
8236#ifdef FEAT_RIGHTLEFT
8237 revins_on ||
8238#endif
8239 (curwin->w_cursor.col > mincol
8240 && (curwin->w_cursor.lnum != Insstart.lnum
8241 || curwin->w_cursor.col != Insstart.col)));
8242 did_backspace = TRUE;
8243 }
8244#ifdef FEAT_SMARTINDENT
8245 did_si = FALSE;
8246 can_si = FALSE;
8247 can_si_back = FALSE;
8248#endif
8249 if (curwin->w_cursor.col <= 1)
8250 did_ai = FALSE;
8251 /*
8252 * It's a little strange to put backspaces into the redo
8253 * buffer, but it makes auto-indent a lot easier to deal
8254 * with.
8255 */
8256 AppendCharToRedobuff(c);
8257
8258 /* If deleted before the insertion point, adjust it */
8259 if (curwin->w_cursor.lnum == Insstart.lnum
8260 && curwin->w_cursor.col < Insstart.col)
8261 Insstart.col = curwin->w_cursor.col;
8262
8263 /* vi behaviour: the cursor moves backward but the character that
8264 * was there remains visible
8265 * Vim behaviour: the cursor moves backward and the character that
8266 * was there is erased from the screen.
8267 * We can emulate the vi behaviour by pretending there is a dollar
8268 * displayed even when there isn't.
8269 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8270 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8271 dollar_vcol = curwin->w_virtcol;
8272
8273 return did_backspace;
8274}
8275
8276#ifdef FEAT_MOUSE
8277 static void
8278ins_mouse(c)
8279 int c;
8280{
8281 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008282 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
8284# ifdef FEAT_GUI
8285 /* When GUI is active, also move/paste when 'mouse' is empty */
8286 if (!gui.in_use)
8287# endif
8288 if (!mouse_has(MOUSE_INSERT))
8289 return;
8290
8291 undisplay_dollar();
8292 tpos = curwin->w_cursor;
8293 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8294 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008295#ifdef FEAT_WINDOWS
8296 win_T *new_curwin = curwin;
8297
8298 if (curwin != old_curwin && win_valid(old_curwin))
8299 {
8300 /* Mouse took us to another window. We need to go back to the
8301 * previous one to stop insert there properly. */
8302 curwin = old_curwin;
8303 curbuf = curwin->w_buffer;
8304 }
8305#endif
8306 start_arrow(curwin == old_curwin ? &tpos : NULL);
8307#ifdef FEAT_WINDOWS
8308 if (curwin != new_curwin && win_valid(new_curwin))
8309 {
8310 curwin = new_curwin;
8311 curbuf = curwin->w_buffer;
8312 }
8313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314# ifdef FEAT_CINDENT
8315 can_cindent = TRUE;
8316# endif
8317 }
8318
8319#ifdef FEAT_WINDOWS
8320 /* redraw status lines (in case another window became active) */
8321 redraw_statuslines();
8322#endif
8323}
8324
8325 static void
8326ins_mousescroll(up)
8327 int up;
8328{
8329 pos_T tpos;
8330# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8331 win_T *old_curwin;
8332# endif
8333
8334 tpos = curwin->w_cursor;
8335
8336# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8337 old_curwin = curwin;
8338
8339 /* Currently the mouse coordinates are only known in the GUI. */
8340 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8341 {
8342 int row, col;
8343
8344 row = mouse_row;
8345 col = mouse_col;
8346
8347 /* find the window at the pointer coordinates */
8348 curwin = mouse_find_win(&row, &col);
8349 curbuf = curwin->w_buffer;
8350 }
8351 if (curwin == old_curwin)
8352# endif
8353 undisplay_dollar();
8354
8355 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8356 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8357 else
8358 scroll_redraw(up, 3L);
8359
8360# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8361 curwin->w_redr_status = TRUE;
8362
8363 curwin = old_curwin;
8364 curbuf = curwin->w_buffer;
8365# endif
8366
8367 if (!equalpos(curwin->w_cursor, tpos))
8368 {
8369 start_arrow(&tpos);
8370# ifdef FEAT_CINDENT
8371 can_cindent = TRUE;
8372# endif
8373 }
8374}
8375#endif
8376
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008377#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008378 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008379ins_tabline(c)
8380 int c;
8381{
8382 /* We will be leaving the current window, unless closing another tab. */
8383 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8384 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8385 {
8386 undisplay_dollar();
8387 start_arrow(&curwin->w_cursor);
8388# ifdef FEAT_CINDENT
8389 can_cindent = TRUE;
8390# endif
8391 }
8392
8393 if (c == K_TABLINE)
8394 goto_tabpage(current_tab);
8395 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008396 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008397 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008398 redraw_statuslines(); /* will redraw the tabline when needed */
8399 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008400}
8401#endif
8402
8403#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 void
8405ins_scroll()
8406{
8407 pos_T tpos;
8408
8409 undisplay_dollar();
8410 tpos = curwin->w_cursor;
8411 if (gui_do_scroll())
8412 {
8413 start_arrow(&tpos);
8414# ifdef FEAT_CINDENT
8415 can_cindent = TRUE;
8416# endif
8417 }
8418}
8419
8420 void
8421ins_horscroll()
8422{
8423 pos_T tpos;
8424
8425 undisplay_dollar();
8426 tpos = curwin->w_cursor;
8427 if (gui_do_horiz_scroll())
8428 {
8429 start_arrow(&tpos);
8430# ifdef FEAT_CINDENT
8431 can_cindent = TRUE;
8432# endif
8433 }
8434}
8435#endif
8436
8437 static void
8438ins_left()
8439{
8440 pos_T tpos;
8441
8442#ifdef FEAT_FOLDING
8443 if ((fdo_flags & FDO_HOR) && KeyTyped)
8444 foldOpenCursor();
8445#endif
8446 undisplay_dollar();
8447 tpos = curwin->w_cursor;
8448 if (oneleft() == OK)
8449 {
8450 start_arrow(&tpos);
8451#ifdef FEAT_RIGHTLEFT
8452 /* If exit reversed string, position is fixed */
8453 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8454 revins_legal++;
8455 revins_chars++;
8456#endif
8457 }
8458
8459 /*
8460 * if 'whichwrap' set for cursor in insert mode may go to
8461 * previous line
8462 */
8463 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8464 {
8465 start_arrow(&tpos);
8466 --(curwin->w_cursor.lnum);
8467 coladvance((colnr_T)MAXCOL);
8468 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8469 }
8470 else
8471 vim_beep();
8472}
8473
8474 static void
8475ins_home(c)
8476 int c;
8477{
8478 pos_T tpos;
8479
8480#ifdef FEAT_FOLDING
8481 if ((fdo_flags & FDO_HOR) && KeyTyped)
8482 foldOpenCursor();
8483#endif
8484 undisplay_dollar();
8485 tpos = curwin->w_cursor;
8486 if (c == K_C_HOME)
8487 curwin->w_cursor.lnum = 1;
8488 curwin->w_cursor.col = 0;
8489#ifdef FEAT_VIRTUALEDIT
8490 curwin->w_cursor.coladd = 0;
8491#endif
8492 curwin->w_curswant = 0;
8493 start_arrow(&tpos);
8494}
8495
8496 static void
8497ins_end(c)
8498 int c;
8499{
8500 pos_T tpos;
8501
8502#ifdef FEAT_FOLDING
8503 if ((fdo_flags & FDO_HOR) && KeyTyped)
8504 foldOpenCursor();
8505#endif
8506 undisplay_dollar();
8507 tpos = curwin->w_cursor;
8508 if (c == K_C_END)
8509 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8510 coladvance((colnr_T)MAXCOL);
8511 curwin->w_curswant = MAXCOL;
8512
8513 start_arrow(&tpos);
8514}
8515
8516 static void
8517ins_s_left()
8518{
8519#ifdef FEAT_FOLDING
8520 if ((fdo_flags & FDO_HOR) && KeyTyped)
8521 foldOpenCursor();
8522#endif
8523 undisplay_dollar();
8524 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8525 {
8526 start_arrow(&curwin->w_cursor);
8527 (void)bck_word(1L, FALSE, FALSE);
8528 curwin->w_set_curswant = TRUE;
8529 }
8530 else
8531 vim_beep();
8532}
8533
8534 static void
8535ins_right()
8536{
8537#ifdef FEAT_FOLDING
8538 if ((fdo_flags & FDO_HOR) && KeyTyped)
8539 foldOpenCursor();
8540#endif
8541 undisplay_dollar();
8542 if (gchar_cursor() != NUL || virtual_active()
8543 )
8544 {
8545 start_arrow(&curwin->w_cursor);
8546 curwin->w_set_curswant = TRUE;
8547#ifdef FEAT_VIRTUALEDIT
8548 if (virtual_active())
8549 oneright();
8550 else
8551#endif
8552 {
8553#ifdef FEAT_MBYTE
8554 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008555 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 else
8557#endif
8558 ++curwin->w_cursor.col;
8559 }
8560
8561#ifdef FEAT_RIGHTLEFT
8562 revins_legal++;
8563 if (revins_chars)
8564 revins_chars--;
8565#endif
8566 }
8567 /* if 'whichwrap' set for cursor in insert mode, may move the
8568 * cursor to the next line */
8569 else if (vim_strchr(p_ww, ']') != NULL
8570 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8571 {
8572 start_arrow(&curwin->w_cursor);
8573 curwin->w_set_curswant = TRUE;
8574 ++curwin->w_cursor.lnum;
8575 curwin->w_cursor.col = 0;
8576 }
8577 else
8578 vim_beep();
8579}
8580
8581 static void
8582ins_s_right()
8583{
8584#ifdef FEAT_FOLDING
8585 if ((fdo_flags & FDO_HOR) && KeyTyped)
8586 foldOpenCursor();
8587#endif
8588 undisplay_dollar();
8589 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8590 || gchar_cursor() != NUL)
8591 {
8592 start_arrow(&curwin->w_cursor);
8593 (void)fwd_word(1L, FALSE, 0);
8594 curwin->w_set_curswant = TRUE;
8595 }
8596 else
8597 vim_beep();
8598}
8599
8600 static void
8601ins_up(startcol)
8602 int startcol; /* when TRUE move to Insstart.col */
8603{
8604 pos_T tpos;
8605 linenr_T old_topline = curwin->w_topline;
8606#ifdef FEAT_DIFF
8607 int old_topfill = curwin->w_topfill;
8608#endif
8609
8610 undisplay_dollar();
8611 tpos = curwin->w_cursor;
8612 if (cursor_up(1L, TRUE) == OK)
8613 {
8614 if (startcol)
8615 coladvance(getvcol_nolist(&Insstart));
8616 if (old_topline != curwin->w_topline
8617#ifdef FEAT_DIFF
8618 || old_topfill != curwin->w_topfill
8619#endif
8620 )
8621 redraw_later(VALID);
8622 start_arrow(&tpos);
8623#ifdef FEAT_CINDENT
8624 can_cindent = TRUE;
8625#endif
8626 }
8627 else
8628 vim_beep();
8629}
8630
8631 static void
8632ins_pageup()
8633{
8634 pos_T tpos;
8635
8636 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008637
8638#ifdef FEAT_WINDOWS
8639 if (mod_mask & MOD_MASK_CTRL)
8640 {
8641 /* <C-PageUp>: tab page back */
8642 goto_tabpage(-1);
8643 return;
8644 }
8645#endif
8646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 tpos = curwin->w_cursor;
8648 if (onepage(BACKWARD, 1L) == OK)
8649 {
8650 start_arrow(&tpos);
8651#ifdef FEAT_CINDENT
8652 can_cindent = TRUE;
8653#endif
8654 }
8655 else
8656 vim_beep();
8657}
8658
8659 static void
8660ins_down(startcol)
8661 int startcol; /* when TRUE move to Insstart.col */
8662{
8663 pos_T tpos;
8664 linenr_T old_topline = curwin->w_topline;
8665#ifdef FEAT_DIFF
8666 int old_topfill = curwin->w_topfill;
8667#endif
8668
8669 undisplay_dollar();
8670 tpos = curwin->w_cursor;
8671 if (cursor_down(1L, TRUE) == OK)
8672 {
8673 if (startcol)
8674 coladvance(getvcol_nolist(&Insstart));
8675 if (old_topline != curwin->w_topline
8676#ifdef FEAT_DIFF
8677 || old_topfill != curwin->w_topfill
8678#endif
8679 )
8680 redraw_later(VALID);
8681 start_arrow(&tpos);
8682#ifdef FEAT_CINDENT
8683 can_cindent = TRUE;
8684#endif
8685 }
8686 else
8687 vim_beep();
8688}
8689
8690 static void
8691ins_pagedown()
8692{
8693 pos_T tpos;
8694
8695 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008696
8697#ifdef FEAT_WINDOWS
8698 if (mod_mask & MOD_MASK_CTRL)
8699 {
8700 /* <C-PageDown>: tab page forward */
8701 goto_tabpage(0);
8702 return;
8703 }
8704#endif
8705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 tpos = curwin->w_cursor;
8707 if (onepage(FORWARD, 1L) == OK)
8708 {
8709 start_arrow(&tpos);
8710#ifdef FEAT_CINDENT
8711 can_cindent = TRUE;
8712#endif
8713 }
8714 else
8715 vim_beep();
8716}
8717
8718#ifdef FEAT_DND
8719 static void
8720ins_drop()
8721{
8722 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8723}
8724#endif
8725
8726/*
8727 * Handle TAB in Insert or Replace mode.
8728 * Return TRUE when the TAB needs to be inserted like a normal character.
8729 */
8730 static int
8731ins_tab()
8732{
8733 int ind;
8734 int i;
8735 int temp;
8736
8737 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8738 Insstart_blank_vcol = get_nolist_virtcol();
8739 if (echeck_abbr(TAB + ABBR_OFF))
8740 return FALSE;
8741
8742 ind = inindent(0);
8743#ifdef FEAT_CINDENT
8744 if (ind)
8745 can_cindent = FALSE;
8746#endif
8747
8748 /*
8749 * When nothing special, insert TAB like a normal character
8750 */
8751 if (!curbuf->b_p_et
8752 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8753 && curbuf->b_p_sts == 0)
8754 return TRUE;
8755
8756 if (stop_arrow() == FAIL)
8757 return TRUE;
8758
8759 did_ai = FALSE;
8760#ifdef FEAT_SMARTINDENT
8761 did_si = FALSE;
8762 can_si = FALSE;
8763 can_si_back = FALSE;
8764#endif
8765 AppendToRedobuff((char_u *)"\t");
8766
8767 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8768 temp = (int)curbuf->b_p_sw;
8769 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8770 temp = (int)curbuf->b_p_sts;
8771 else /* otherwise use 'tabstop' */
8772 temp = (int)curbuf->b_p_ts;
8773 temp -= get_nolist_virtcol() % temp;
8774
8775 /*
8776 * Insert the first space with ins_char(). It will delete one char in
8777 * replace mode. Insert the rest with ins_str(); it will not delete any
8778 * chars. For VREPLACE mode, we use ins_char() for all characters.
8779 */
8780 ins_char(' ');
8781 while (--temp > 0)
8782 {
8783#ifdef FEAT_VREPLACE
8784 if (State & VREPLACE_FLAG)
8785 ins_char(' ');
8786 else
8787#endif
8788 {
8789 ins_str((char_u *)" ");
8790 if (State & REPLACE_FLAG) /* no char replaced */
8791 replace_push(NUL);
8792 }
8793 }
8794
8795 /*
8796 * When 'expandtab' not set: Replace spaces by TABs where possible.
8797 */
8798 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8799 {
8800 char_u *ptr;
8801#ifdef FEAT_VREPLACE
8802 char_u *saved_line = NULL; /* init for GCC */
8803 pos_T pos;
8804#endif
8805 pos_T fpos;
8806 pos_T *cursor;
8807 colnr_T want_vcol, vcol;
8808 int change_col = -1;
8809 int save_list = curwin->w_p_list;
8810
8811 /*
8812 * Get the current line. For VREPLACE mode, don't make real changes
8813 * yet, just work on a copy of the line.
8814 */
8815#ifdef FEAT_VREPLACE
8816 if (State & VREPLACE_FLAG)
8817 {
8818 pos = curwin->w_cursor;
8819 cursor = &pos;
8820 saved_line = vim_strsave(ml_get_curline());
8821 if (saved_line == NULL)
8822 return FALSE;
8823 ptr = saved_line + pos.col;
8824 }
8825 else
8826#endif
8827 {
8828 ptr = ml_get_cursor();
8829 cursor = &curwin->w_cursor;
8830 }
8831
8832 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8833 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8834 curwin->w_p_list = FALSE;
8835
8836 /* Find first white before the cursor */
8837 fpos = curwin->w_cursor;
8838 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8839 {
8840 --fpos.col;
8841 --ptr;
8842 }
8843
8844 /* In Replace mode, don't change characters before the insert point. */
8845 if ((State & REPLACE_FLAG)
8846 && fpos.lnum == Insstart.lnum
8847 && fpos.col < Insstart.col)
8848 {
8849 ptr += Insstart.col - fpos.col;
8850 fpos.col = Insstart.col;
8851 }
8852
8853 /* compute virtual column numbers of first white and cursor */
8854 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8855 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8856
8857 /* Use as many TABs as possible. Beware of 'showbreak' and
8858 * 'linebreak' adding extra virtual columns. */
8859 while (vim_iswhite(*ptr))
8860 {
8861 i = lbr_chartabsize((char_u *)"\t", vcol);
8862 if (vcol + i > want_vcol)
8863 break;
8864 if (*ptr != TAB)
8865 {
8866 *ptr = TAB;
8867 if (change_col < 0)
8868 {
8869 change_col = fpos.col; /* Column of first change */
8870 /* May have to adjust Insstart */
8871 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8872 Insstart.col = fpos.col;
8873 }
8874 }
8875 ++fpos.col;
8876 ++ptr;
8877 vcol += i;
8878 }
8879
8880 if (change_col >= 0)
8881 {
8882 int repl_off = 0;
8883
8884 /* Skip over the spaces we need. */
8885 while (vcol < want_vcol && *ptr == ' ')
8886 {
8887 vcol += lbr_chartabsize(ptr, vcol);
8888 ++ptr;
8889 ++repl_off;
8890 }
8891 if (vcol > want_vcol)
8892 {
8893 /* Must have a char with 'showbreak' just before it. */
8894 --ptr;
8895 --repl_off;
8896 }
8897 fpos.col += repl_off;
8898
8899 /* Delete following spaces. */
8900 i = cursor->col - fpos.col;
8901 if (i > 0)
8902 {
8903 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8904 /* correct replace stack. */
8905 if ((State & REPLACE_FLAG)
8906#ifdef FEAT_VREPLACE
8907 && !(State & VREPLACE_FLAG)
8908#endif
8909 )
8910 for (temp = i; --temp >= 0; )
8911 replace_join(repl_off);
8912 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008913#ifdef FEAT_NETBEANS_INTG
8914 if (usingNetbeans)
8915 {
8916 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8917 (long)(i + 1));
8918 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8919 (char_u *)"\t", 1);
8920 }
8921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 cursor->col -= i;
8923
8924#ifdef FEAT_VREPLACE
8925 /*
8926 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8927 * backspacing over the changed spacing and then inserting the new
8928 * spacing.
8929 */
8930 if (State & VREPLACE_FLAG)
8931 {
8932 /* Backspace from real cursor to change_col */
8933 backspace_until_column(change_col);
8934
8935 /* Insert each char in saved_line from changed_col to
8936 * ptr-cursor */
8937 ins_bytes_len(saved_line + change_col,
8938 cursor->col - change_col);
8939 }
8940#endif
8941 }
8942
8943#ifdef FEAT_VREPLACE
8944 if (State & VREPLACE_FLAG)
8945 vim_free(saved_line);
8946#endif
8947 curwin->w_p_list = save_list;
8948 }
8949
8950 return FALSE;
8951}
8952
8953/*
8954 * Handle CR or NL in insert mode.
8955 * Return TRUE when out of memory or can't undo.
8956 */
8957 static int
8958ins_eol(c)
8959 int c;
8960{
8961 int i;
8962
8963 if (echeck_abbr(c + ABBR_OFF))
8964 return FALSE;
8965 if (stop_arrow() == FAIL)
8966 return TRUE;
8967 undisplay_dollar();
8968
8969 /*
8970 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8971 * character under the cursor. Only push a NUL on the replace stack,
8972 * nothing to put back when the NL is deleted.
8973 */
8974 if ((State & REPLACE_FLAG)
8975#ifdef FEAT_VREPLACE
8976 && !(State & VREPLACE_FLAG)
8977#endif
8978 )
8979 replace_push(NUL);
8980
8981 /*
8982 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8983 * replacing the next line, so we push all of the characters left on the
8984 * line onto the replace stack. This is not done here though, it is done
8985 * in open_line().
8986 */
8987
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008988#ifdef FEAT_VIRTUALEDIT
8989 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
8990 * CTRL-O). */
8991 if (virtual_active() && curwin->w_cursor.coladd > 0)
8992 coladvance(getviscol());
8993#endif
8994
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#ifdef FEAT_RIGHTLEFT
8996# ifdef FEAT_FKMAP
8997 if (p_altkeymap && p_fkmap)
8998 fkmap(NL);
8999# endif
9000 /* NL in reverse insert will always start in the end of
9001 * current line. */
9002 if (revins_on)
9003 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9004#endif
9005
9006 AppendToRedobuff(NL_STR);
9007 i = open_line(FORWARD,
9008#ifdef FEAT_COMMENTS
9009 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9010#endif
9011 0, old_indent);
9012 old_indent = 0;
9013#ifdef FEAT_CINDENT
9014 can_cindent = TRUE;
9015#endif
9016
9017 return (!i);
9018}
9019
9020#ifdef FEAT_DIGRAPHS
9021/*
9022 * Handle digraph in insert mode.
9023 * Returns character still to be inserted, or NUL when nothing remaining to be
9024 * done.
9025 */
9026 static int
9027ins_digraph()
9028{
9029 int c;
9030 int cc;
9031
9032 pc_status = PC_STATUS_UNSET;
9033 if (redrawing() && !char_avail())
9034 {
9035 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009036 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037
9038 edit_putchar('?', TRUE);
9039#ifdef FEAT_CMDL_INFO
9040 add_to_showcmd_c(Ctrl_K);
9041#endif
9042 }
9043
9044#ifdef USE_ON_FLY_SCROLL
9045 dont_scroll = TRUE; /* disallow scrolling here */
9046#endif
9047
9048 /* don't map the digraph chars. This also prevents the
9049 * mode message to be deleted when ESC is hit */
9050 ++no_mapping;
9051 ++allow_keys;
9052 c = safe_vgetc();
9053 --no_mapping;
9054 --allow_keys;
9055 if (IS_SPECIAL(c) || mod_mask) /* special key */
9056 {
9057#ifdef FEAT_CMDL_INFO
9058 clear_showcmd();
9059#endif
9060 insert_special(c, TRUE, FALSE);
9061 return NUL;
9062 }
9063 if (c != ESC)
9064 {
9065 if (redrawing() && !char_avail())
9066 {
9067 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009068 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
9070 if (char2cells(c) == 1)
9071 {
9072 /* first remove the '?', otherwise it's restored when typing
9073 * an ESC next */
9074 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009075 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 edit_putchar(c, TRUE);
9077 }
9078#ifdef FEAT_CMDL_INFO
9079 add_to_showcmd_c(c);
9080#endif
9081 }
9082 ++no_mapping;
9083 ++allow_keys;
9084 cc = safe_vgetc();
9085 --no_mapping;
9086 --allow_keys;
9087 if (cc != ESC)
9088 {
9089 AppendToRedobuff((char_u *)CTRL_V_STR);
9090 c = getdigraph(c, cc, TRUE);
9091#ifdef FEAT_CMDL_INFO
9092 clear_showcmd();
9093#endif
9094 return c;
9095 }
9096 }
9097 edit_unputchar();
9098#ifdef FEAT_CMDL_INFO
9099 clear_showcmd();
9100#endif
9101 return NUL;
9102}
9103#endif
9104
9105/*
9106 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9107 * Returns the char to be inserted, or NUL if none found.
9108 */
9109 static int
9110ins_copychar(lnum)
9111 linenr_T lnum;
9112{
9113 int c;
9114 int temp;
9115 char_u *ptr, *prev_ptr;
9116
9117 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9118 {
9119 vim_beep();
9120 return NUL;
9121 }
9122
9123 /* try to advance to the cursor column */
9124 temp = 0;
9125 ptr = ml_get(lnum);
9126 prev_ptr = ptr;
9127 validate_virtcol();
9128 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9129 {
9130 prev_ptr = ptr;
9131 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9132 }
9133 if ((colnr_T)temp > curwin->w_virtcol)
9134 ptr = prev_ptr;
9135
9136#ifdef FEAT_MBYTE
9137 c = (*mb_ptr2char)(ptr);
9138#else
9139 c = *ptr;
9140#endif
9141 if (c == NUL)
9142 vim_beep();
9143 return c;
9144}
9145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009146/*
9147 * CTRL-Y or CTRL-E typed in Insert mode.
9148 */
9149 static int
9150ins_ctrl_ey(tc)
9151 int tc;
9152{
9153 int c = tc;
9154
9155#ifdef FEAT_INS_EXPAND
9156 if (ctrl_x_mode == CTRL_X_SCROLL)
9157 {
9158 if (c == Ctrl_Y)
9159 scrolldown_clamp();
9160 else
9161 scrollup_clamp();
9162 redraw_later(VALID);
9163 }
9164 else
9165#endif
9166 {
9167 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9168 if (c != NUL)
9169 {
9170 long tw_save;
9171
9172 /* The character must be taken literally, insert like it
9173 * was typed after a CTRL-V, and pretend 'textwidth'
9174 * wasn't set. Digits, 'o' and 'x' are special after a
9175 * CTRL-V, don't use it for these. */
9176 if (c < 256 && !isalnum(c))
9177 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9178 tw_save = curbuf->b_p_tw;
9179 curbuf->b_p_tw = -1;
9180 insert_special(c, TRUE, FALSE);
9181 curbuf->b_p_tw = tw_save;
9182#ifdef FEAT_RIGHTLEFT
9183 revins_chars++;
9184 revins_legal++;
9185#endif
9186 c = Ctrl_V; /* pretend CTRL-V is last character */
9187 auto_format(FALSE, TRUE);
9188 }
9189 }
9190 return c;
9191}
9192
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#ifdef FEAT_SMARTINDENT
9194/*
9195 * Try to do some very smart auto-indenting.
9196 * Used when inserting a "normal" character.
9197 */
9198 static void
9199ins_try_si(c)
9200 int c;
9201{
9202 pos_T *pos, old_pos;
9203 char_u *ptr;
9204 int i;
9205 int temp;
9206
9207 /*
9208 * do some very smart indenting when entering '{' or '}'
9209 */
9210 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9211 {
9212 /*
9213 * for '}' set indent equal to indent of line containing matching '{'
9214 */
9215 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9216 {
9217 old_pos = curwin->w_cursor;
9218 /*
9219 * If the matching '{' has a ')' immediately before it (ignoring
9220 * white-space), then line up with the start of the line
9221 * containing the matching '(' if there is one. This handles the
9222 * case where an "if (..\n..) {" statement continues over multiple
9223 * lines -- webb
9224 */
9225 ptr = ml_get(pos->lnum);
9226 i = pos->col;
9227 if (i > 0) /* skip blanks before '{' */
9228 while (--i > 0 && vim_iswhite(ptr[i]))
9229 ;
9230 curwin->w_cursor.lnum = pos->lnum;
9231 curwin->w_cursor.col = i;
9232 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9233 curwin->w_cursor = *pos;
9234 i = get_indent();
9235 curwin->w_cursor = old_pos;
9236#ifdef FEAT_VREPLACE
9237 if (State & VREPLACE_FLAG)
9238 change_indent(INDENT_SET, i, FALSE, NUL);
9239 else
9240#endif
9241 (void)set_indent(i, SIN_CHANGED);
9242 }
9243 else if (curwin->w_cursor.col > 0)
9244 {
9245 /*
9246 * when inserting '{' after "O" reduce indent, but not
9247 * more than indent of previous line
9248 */
9249 temp = TRUE;
9250 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9251 {
9252 old_pos = curwin->w_cursor;
9253 i = get_indent();
9254 while (curwin->w_cursor.lnum > 1)
9255 {
9256 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9257
9258 /* ignore empty lines and lines starting with '#'. */
9259 if (*ptr != '#' && *ptr != NUL)
9260 break;
9261 }
9262 if (get_indent() >= i)
9263 temp = FALSE;
9264 curwin->w_cursor = old_pos;
9265 }
9266 if (temp)
9267 shift_line(TRUE, FALSE, 1);
9268 }
9269 }
9270
9271 /*
9272 * set indent of '#' always to 0
9273 */
9274 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9275 {
9276 /* remember current indent for next line */
9277 old_indent = get_indent();
9278 (void)set_indent(0, SIN_CHANGED);
9279 }
9280
9281 /* Adjust ai_col, the char at this position can be deleted. */
9282 if (ai_col > curwin->w_cursor.col)
9283 ai_col = curwin->w_cursor.col;
9284}
9285#endif
9286
9287/*
9288 * Get the value that w_virtcol would have when 'list' is off.
9289 * Unless 'cpo' contains the 'L' flag.
9290 */
9291 static colnr_T
9292get_nolist_virtcol()
9293{
9294 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9295 return getvcol_nolist(&curwin->w_cursor);
9296 validate_virtcol();
9297 return curwin->w_virtcol;
9298}