blob: e298a8c389f3e223fa00bd52e537eb5ab1a9e78f [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 Moolenaar91170f82006-05-05 21:15:17 +00002809 FALSE, 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),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002845 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002846 }
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;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003537 int icase = FALSE;
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 Moolenaar91170f82006-05-05 21:15:17 +00003756 ins_compl_add_matches(num_matches, matches, FALSE);
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 Moolenaar91170f82006-05-05 21:15:17 +00003931 if (ins_compl_add_infercase(ptr, len, FALSE,
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();
Bram Moolenaar14716812006-05-04 21:54:08 +00004153#ifdef FEAT_GUI
4154 if (gui.in_use)
4155 {
4156 /* Show the cursor after the match, not after the redrawn text. */
4157 setcursor();
4158 out_flush();
4159 gui_update_cursor(FALSE, FALSE);
4160 }
4161#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 /* Delete old text to be replaced, since we're still searching and
4164 * don't want to match ourselves! */
4165 ins_compl_delete();
4166 }
4167
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004168 /* Enter will select a match when the match wasn't inserted and the popup
4169 * menu is visislbe. */
4170 compl_enter_selects = !insert_match && compl_match_array != NULL;
4171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /*
4173 * Show the file name for the match (if any)
4174 * Truncate the file name to avoid a wait for return.
4175 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004176 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004179 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if (i <= 0)
4181 i = 0;
4182 else
4183 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004184 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 msg(IObuff);
4186 redraw_cmdline = FALSE; /* don't overwrite! */
4187 }
4188
4189 return num_matches;
4190}
4191
4192/*
4193 * Call this while finding completions, to check whether the user has hit a key
4194 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004195 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004197 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
4199 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004200ins_compl_check_keys(frequency)
4201 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
4203 static int count = 0;
4204
4205 int c;
4206
4207 /* Don't check when reading keys from a script. That would break the test
4208 * scripts */
4209 if (using_script())
4210 return;
4211
4212 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004213 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return;
4215 count = 0;
4216
4217 ++no_mapping;
4218 c = vpeekc_any();
4219 --no_mapping;
4220 if (c != NUL)
4221 {
4222 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4223 {
4224 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004225 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004226 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4227 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004230 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004232 if (compl_pending != 0 && !got_int)
4233 (void)ins_compl_next(FALSE, compl_pending > 0
4234 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004235}
4236
4237/*
4238 * Decide the direction of Insert mode complete from the key typed.
4239 * Returns BACKWARD or FORWARD.
4240 */
4241 static int
4242ins_compl_key2dir(c)
4243 int c;
4244{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004245 if (c == Ctrl_P || c == Ctrl_L
4246 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4247 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004248 return BACKWARD;
4249 return FORWARD;
4250}
4251
4252/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 * Return TRUE for keys that are used for completion only when the popup menu
4254 * is visible.
4255 */
4256 static int
4257ins_compl_pum_key(c)
4258 int c;
4259{
4260 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004261 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4262 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004263}
4264
4265/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004266 * Decide the number of completions to move forward.
4267 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4268 */
4269 static int
4270ins_compl_key2count(c)
4271 int c;
4272{
4273 int h;
4274
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004275 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004276 {
4277 h = pum_get_height();
4278 if (h > 3)
4279 h -= 2; /* keep some context */
4280 return h;
4281 }
4282 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283}
4284
4285/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004286 * Return TRUE if completion with "c" should insert the match, FALSE if only
4287 * to change the currently selected completion.
4288 */
4289 static int
4290ins_compl_use_match(c)
4291 int c;
4292{
4293 switch (c)
4294 {
4295 case K_UP:
4296 case K_DOWN:
4297 case K_PAGEDOWN:
4298 case K_KPAGEDOWN:
4299 case K_S_DOWN:
4300 case K_PAGEUP:
4301 case K_KPAGEUP:
4302 case K_S_UP:
4303 return FALSE;
4304 }
4305 return TRUE;
4306}
4307
4308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 * Do Insert mode completion.
4310 * Called when character "c" was typed, which has a meaning for completion.
4311 * Returns OK if completion was done, FAIL if something failed (out of mem).
4312 */
4313 static int
4314ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317 char_u *line;
4318 int startcol = 0; /* column where searched text starts */
4319 colnr_T curs_col; /* cursor column */
4320 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
Bram Moolenaare3226be2005-12-18 22:10:00 +00004322 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
4325 /* First time we hit ^N or ^P (in a row, I mean) */
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 did_ai = FALSE;
4328#ifdef FEAT_SMARTINDENT
4329 did_si = FALSE;
4330 can_si = FALSE;
4331 can_si_back = FALSE;
4332#endif
4333 if (stop_arrow() == FAIL)
4334 return FAIL;
4335
4336 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004338 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 * "compl_startpos" to the cursor as a pattern to add a new word
4342 * instead of expand the one before the cursor, in word-wise if
4343 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 * is not in the same line as the cursor then fix it (the line has
4345 * been split because it was longer than 'tw'). if SOL is set then
4346 * skip the previous pattern, a word at the beginning of the line has
4347 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004348 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4349 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
4351 /*
4352 * it is a continued search
4353 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4356 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4357 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 /* line (probably) wrapped, set compl_startpos to the
4361 * first non_blank in the line, if it is not a wordchar
4362 * include it to get a better pattern, but then we don't
4363 * want the "\\<" prefix, check it bellow */
4364 compl_col = (colnr_T)(skipwhite(line) - line);
4365 compl_startpos.col = compl_col;
4366 compl_startpos.lnum = curwin->w_cursor.lnum;
4367 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 else
4370 {
4371 /* S_IPOS was set when we inserted a word that was at the
4372 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373 * mode but first we need to redefine compl_startpos */
4374 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004376 compl_cont_status |= CONT_SOL;
4377 compl_startpos.col = (colnr_T)(skipwhite(
4378 line + compl_length
4379 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004384 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 * have enough space? just being paranoic */
4386#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 compl_cont_status &= ~CONT_SOL;
4390 compl_length = (IOSIZE - MIN_SPACE);
4391 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4394 if (compl_length < 1)
4395 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 compl_cont_status = 0;
4410 compl_cont_status |= CONT_N_ADDS;
4411 compl_startpos = curwin->w_cursor;
4412 startcol = (int)curs_col;
4413 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415
4416 /* Work out completion pattern and original text -- webb */
4417 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4418 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4421 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 compl_col += ++startcol;
4427 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 compl_pattern = str_foldcase(line + compl_col,
4431 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 compl_pattern = vim_strnsave(line + compl_col,
4434 compl_length);
4435 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 return FAIL;
4437 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 char_u *prefix = (char_u *)"\\<";
4441
4442 /* we need 3 extra chars, 1 for the NUL and
4443 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4445 compl_length) + 3);
4446 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 if (!vim_iswordp(line + compl_col)
4449 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 && (
4451#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455#endif
4456 )))
4457 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 STRCPY((char *)compl_pattern, prefix);
4459 (void)quote_meta(compl_pattern + STRLEN(prefix),
4460 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467#endif
4468 )
4469 {
4470 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4472 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 compl_col += curs_col;
4475 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else
4478 {
4479#ifdef FEAT_MBYTE
4480 /* Search the point of change class of multibyte character
4481 * or not a word single byte character backward. */
4482 if (has_mbyte)
4483 {
4484 int base_class;
4485 int head_off;
4486
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 startcol -= (*mb_head_off)(line, line + startcol);
4488 base_class = mb_get_class(line + startcol);
4489 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 head_off = (*mb_head_off)(line, line + startcol);
4492 if (base_class != mb_get_class(line + startcol
4493 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004495 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 }
4498 else
4499#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 compl_col += ++startcol;
4503 compl_length = (int)curs_col - startcol;
4504 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 /* Only match word with at least two chars -- webb
4507 * there's no need to call quote_meta,
4508 * alloc(7) is enough -- Acevedo
4509 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 compl_pattern = alloc(7);
4511 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 STRCPY((char *)compl_pattern, "\\<");
4514 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4515 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 else
4518 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4520 compl_length) + 3);
4521 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 STRCPY((char *)compl_pattern, "\\<");
4524 (void)quote_meta(compl_pattern + 2, line + compl_col,
4525 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 }
4528 }
4529 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4530 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004531 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 compl_length = (int)curs_col - (int)compl_col;
4533 if (compl_length < 0) /* cursor in indent: empty pattern */
4534 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 compl_pattern = str_foldcase(line + compl_col, compl_length,
4537 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4540 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 return FAIL;
4542 }
4543 else if (ctrl_x_mode == CTRL_X_FILES)
4544 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 compl_col += ++startcol;
4548 compl_length = (int)curs_col - startcol;
4549 compl_pattern = addstar(line + compl_col, compl_length,
4550 EXPAND_FILES);
4551 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 return FAIL;
4553 }
4554 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4555 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 compl_pattern = vim_strnsave(line, curs_col);
4557 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 set_cmd_context(&compl_xp, compl_pattern,
4560 (int)STRLEN(compl_pattern), curs_col);
4561 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4562 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4565 compl_col = startcol;
4566 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004568 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004569 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004570#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004571 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004572 * Call user defined function 'completefunc' with "a:findstart"
4573 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004574 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004575 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004576 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004577 char_u *funcname;
4578 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004579
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004580 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004581 * string */
4582 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4583 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4584 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004585 {
4586 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4587 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004588 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004589 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004590
4591 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004592 args[1] = NULL;
4593 pos = curwin->w_cursor;
4594 col = call_func_retnr(funcname, 2, args, FALSE);
4595 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004596
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004597 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004598 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004599 compl_col = col;
4600 if ((colnr_T)compl_col > curs_col)
4601 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004602
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 /* Setup variables for completion. Need to obtain "line" again,
4604 * it may have become invalid. */
4605 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004606 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4608 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004609#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 return FAIL;
4611 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004612 else if (ctrl_x_mode == CTRL_X_SPELL)
4613 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004614#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004615 if (spell_bad_len > 0)
4616 compl_col = curs_col - spell_bad_len;
4617 else
4618 compl_col = spell_word_start(startcol);
4619 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004620 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004621 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004622 /* Need to obtain "line" again, it may have become invalid. */
4623 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004624 compl_length = (int)curs_col - compl_col;
4625 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4626 if (compl_pattern == NULL)
4627#endif
4628 return FAIL;
4629 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630 else
4631 {
4632 EMSG2(_(e_intern2), "ins_complete()");
4633 return FAIL;
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004636 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 edit_submode_pre = (char_u *)_(" Adding");
4639 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4640 {
4641 /* Insert a new line, keep indentation but ignore 'comments' */
4642#ifdef FEAT_COMMENTS
4643 char_u *old = curbuf->b_p_com;
4644
4645 curbuf->b_p_com = (char_u *)"";
4646#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 compl_startpos.lnum = curwin->w_cursor.lnum;
4648 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 ins_eol('\r');
4650#ifdef FEAT_COMMENTS
4651 curbuf->b_p_com = old;
4652#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004653 compl_length = 0;
4654 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657 else
4658 {
4659 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004663 if (compl_cont_status & CONT_LOCAL)
4664 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 else
4666 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4667
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004668 /* Always add completion for the original text. */
4669 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4671 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004672 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 vim_free(compl_pattern);
4675 compl_pattern = NULL;
4676 vim_free(compl_orig_text);
4677 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679 }
4680
4681 /* showmode might reset the internal line pointers, so it must
4682 * be called before line = ml_get(), or when this address is no
4683 * longer needed. -- Acevedo.
4684 */
4685 edit_submode_extra = (char_u *)_("-- Searching...");
4686 edit_submode_highl = HLF_COUNT;
4687 showmode();
4688 edit_submode_extra = NULL;
4689 out_flush();
4690 }
4691
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 compl_shown_match = compl_curr_match;
4693 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
4695 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004696 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004698 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004700 /* may undisplay the popup menu */
4701 ins_compl_upd_pum();
4702
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004703 if (n > 1) /* all matches have been found */
4704 compl_matches = n;
4705 compl_curr_match = compl_shown_match;
4706 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
Bram Moolenaard68071d2006-05-02 22:08:30 +00004708 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4709 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (got_int && !global_busy)
4711 {
4712 (void)vgetc();
4713 got_int = FALSE;
4714 }
4715
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004717 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004719 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4720 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4722 edit_submode_highl = HLF_E;
4723 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4724 * because we couldn't expand anything at first place, but if we used
4725 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4726 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004727 if ( compl_length > 1
4728 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 || (ctrl_x_mode != 0
4730 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4731 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004732 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734
Bram Moolenaar572cb562005-08-05 21:35:02 +00004735 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004738 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 if (edit_submode_extra == NULL)
4741 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004742 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 edit_submode_extra = (char_u *)_("Back at original");
4745 edit_submode_highl = HLF_W;
4746 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
4749 edit_submode_extra = (char_u *)_("Word from other line");
4750 edit_submode_highl = HLF_COUNT;
4751 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004752 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 edit_submode_extra = (char_u *)_("The only match");
4755 edit_submode_highl = HLF_COUNT;
4756 }
4757 else
4758 {
4759 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004760 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004762 int number = 0;
4763 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 /* search backwards for the first valid (!= -1) number.
4768 * This should normally succeed already at the first loop
4769 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004770 for (match = compl_curr_match->cp_prev; match != NULL
4771 && match != compl_first_match;
4772 match = match->cp_prev)
4773 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004775 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 break;
4777 }
4778 if (match != NULL)
4779 /* go up and assign all numbers which are not assigned
4780 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004781 for (match = match->cp_next;
4782 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004783 match = match->cp_next)
4784 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786 else /* BACKWARD */
4787 {
4788 /* search forwards (upwards) for the first valid (!= -1)
4789 * number. This should normally succeed already at the
4790 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004791 for (match = compl_curr_match->cp_next; match != NULL
4792 && match != compl_first_match;
4793 match = match->cp_next)
4794 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004796 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 break;
4798 }
4799 if (match != NULL)
4800 /* go down and assign all numbers which are not
4801 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004802 for (match = match->cp_prev; match
4803 && match->cp_number == -1;
4804 match = match->cp_prev)
4805 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 }
4808
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004809 /* The match should always have a sequence number now, this is
4810 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004811 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
4813 /* Space for 10 text chars. + 2x10-digit no.s */
4814 static char_u match_ref[31];
4815
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004816 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004818 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004820 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004821 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004822 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 edit_submode_extra = match_ref;
4824 edit_submode_highl = HLF_R;
4825 if (dollar_vcol)
4826 curs_columns(FALSE);
4827 }
4828 }
4829 }
4830
4831 /* Show a message about what (completion) mode we're in. */
4832 showmode();
4833 if (edit_submode_extra != NULL)
4834 {
4835 if (!p_smd)
4836 msg_attr(edit_submode_extra,
4837 edit_submode_highl < HLF_COUNT
4838 ? hl_attr(edit_submode_highl) : 0);
4839 }
4840 else
4841 msg_clr_cmdline(); /* necessary for "noshowmode" */
4842
Bram Moolenaard68071d2006-05-02 22:08:30 +00004843 /* Show the popup menu, unless we got interrupted. */
4844 if (!compl_interrupted)
4845 {
4846 /* RedrawingDisabled may be set when invoked through complete(). */
4847 n = RedrawingDisabled;
4848 RedrawingDisabled = 0;
4849 ins_compl_show_pum();
4850 setcursor();
4851 RedrawingDisabled = n;
4852 }
4853 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return OK;
4856}
4857
4858/*
4859 * Looks in the first "len" chars. of "src" for search-metachars.
4860 * If dest is not NULL the chars. are copied there quoting (with
4861 * a backslash) the metachars, and dest would be NUL terminated.
4862 * Returns the length (needed) of dest
4863 */
4864 static int
4865quote_meta(dest, src, len)
4866 char_u *dest;
4867 char_u *src;
4868 int len;
4869{
4870 int m;
4871
4872 for (m = len; --len >= 0; src++)
4873 {
4874 switch (*src)
4875 {
4876 case '.':
4877 case '*':
4878 case '[':
4879 if (ctrl_x_mode == CTRL_X_DICTIONARY
4880 || ctrl_x_mode == CTRL_X_THESAURUS)
4881 break;
4882 case '~':
4883 if (!p_magic) /* quote these only if magic is set */
4884 break;
4885 case '\\':
4886 if (ctrl_x_mode == CTRL_X_DICTIONARY
4887 || ctrl_x_mode == CTRL_X_THESAURUS)
4888 break;
4889 case '^': /* currently it's not needed. */
4890 case '$':
4891 m++;
4892 if (dest != NULL)
4893 *dest++ = '\\';
4894 break;
4895 }
4896 if (dest != NULL)
4897 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 /* Copy remaining bytes of a multibyte character. */
4900 if (has_mbyte)
4901 {
4902 int i, mb_len;
4903
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004904 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (mb_len > 0 && len >= mb_len)
4906 for (i = 0; i < mb_len; ++i)
4907 {
4908 --len;
4909 ++src;
4910 if (dest != NULL)
4911 *dest++ = *src;
4912 }
4913 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 if (dest != NULL)
4917 *dest = NUL;
4918
4919 return m;
4920}
4921#endif /* FEAT_INS_EXPAND */
4922
4923/*
4924 * Next character is interpreted literally.
4925 * A one, two or three digit decimal number is interpreted as its byte value.
4926 * If one or two digits are entered, the next character is given to vungetc().
4927 * For Unicode a character > 255 may be returned.
4928 */
4929 int
4930get_literal()
4931{
4932 int cc;
4933 int nc;
4934 int i;
4935 int hex = FALSE;
4936 int octal = FALSE;
4937#ifdef FEAT_MBYTE
4938 int unicode = 0;
4939#endif
4940
4941 if (got_int)
4942 return Ctrl_C;
4943
4944#ifdef FEAT_GUI
4945 /*
4946 * In GUI there is no point inserting the internal code for a special key.
4947 * It is more useful to insert the string "<KEY>" instead. This would
4948 * probably be useful in a text window too, but it would not be
4949 * vi-compatible (maybe there should be an option for it?) -- webb
4950 */
4951 if (gui.in_use)
4952 ++allow_keys;
4953#endif
4954#ifdef USE_ON_FLY_SCROLL
4955 dont_scroll = TRUE; /* disallow scrolling here */
4956#endif
4957 ++no_mapping; /* don't map the next key hits */
4958 cc = 0;
4959 i = 0;
4960 for (;;)
4961 {
4962 do
4963 nc = safe_vgetc();
4964 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4965 || nc == K_HOR_SCROLLBAR);
4966#ifdef FEAT_CMDL_INFO
4967 if (!(State & CMDLINE)
4968# ifdef FEAT_MBYTE
4969 && MB_BYTE2LEN_CHECK(nc) == 1
4970# endif
4971 )
4972 add_to_showcmd(nc);
4973#endif
4974 if (nc == 'x' || nc == 'X')
4975 hex = TRUE;
4976 else if (nc == 'o' || nc == 'O')
4977 octal = TRUE;
4978#ifdef FEAT_MBYTE
4979 else if (nc == 'u' || nc == 'U')
4980 unicode = nc;
4981#endif
4982 else
4983 {
4984 if (hex
4985#ifdef FEAT_MBYTE
4986 || unicode != 0
4987#endif
4988 )
4989 {
4990 if (!vim_isxdigit(nc))
4991 break;
4992 cc = cc * 16 + hex2nr(nc);
4993 }
4994 else if (octal)
4995 {
4996 if (nc < '0' || nc > '7')
4997 break;
4998 cc = cc * 8 + nc - '0';
4999 }
5000 else
5001 {
5002 if (!VIM_ISDIGIT(nc))
5003 break;
5004 cc = cc * 10 + nc - '0';
5005 }
5006
5007 ++i;
5008 }
5009
5010 if (cc > 255
5011#ifdef FEAT_MBYTE
5012 && unicode == 0
5013#endif
5014 )
5015 cc = 255; /* limit range to 0-255 */
5016 nc = 0;
5017
5018 if (hex) /* hex: up to two chars */
5019 {
5020 if (i >= 2)
5021 break;
5022 }
5023#ifdef FEAT_MBYTE
5024 else if (unicode) /* Unicode: up to four or eight chars */
5025 {
5026 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5027 break;
5028 }
5029#endif
5030 else if (i >= 3) /* decimal or octal: up to three chars */
5031 break;
5032 }
5033 if (i == 0) /* no number entered */
5034 {
5035 if (nc == K_ZERO) /* NUL is stored as NL */
5036 {
5037 cc = '\n';
5038 nc = 0;
5039 }
5040 else
5041 {
5042 cc = nc;
5043 nc = 0;
5044 }
5045 }
5046
5047 if (cc == 0) /* NUL is stored as NL */
5048 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005049#ifdef FEAT_MBYTE
5050 if (enc_dbcs && (cc & 0xff) == 0)
5051 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5052 second byte will cause trouble! */
5053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 --no_mapping;
5056#ifdef FEAT_GUI
5057 if (gui.in_use)
5058 --allow_keys;
5059#endif
5060 if (nc)
5061 vungetc(nc);
5062 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5063 return cc;
5064}
5065
5066/*
5067 * Insert character, taking care of special keys and mod_mask
5068 */
5069 static void
5070insert_special(c, allow_modmask, ctrlv)
5071 int c;
5072 int allow_modmask;
5073 int ctrlv; /* c was typed after CTRL-V */
5074{
5075 char_u *p;
5076 int len;
5077
5078 /*
5079 * Special function key, translate into "<Key>". Up to the last '>' is
5080 * inserted with ins_str(), so as not to replace characters in replace
5081 * mode.
5082 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5083 * unless 'allow_modmask' is TRUE.
5084 */
5085#ifdef MACOS
5086 /* Command-key never produces a normal key */
5087 if (mod_mask & MOD_MASK_CMD)
5088 allow_modmask = TRUE;
5089#endif
5090 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5091 {
5092 p = get_special_key_name(c, mod_mask);
5093 len = (int)STRLEN(p);
5094 c = p[len - 1];
5095 if (len > 2)
5096 {
5097 if (stop_arrow() == FAIL)
5098 return;
5099 p[len - 1] = NUL;
5100 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005101 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 ctrlv = FALSE;
5103 }
5104 }
5105 if (stop_arrow() == OK)
5106 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5107}
5108
5109/*
5110 * Special characters in this context are those that need processing other
5111 * than the simple insertion that can be performed here. This includes ESC
5112 * which terminates the insert, and CR/NL which need special processing to
5113 * open up a new line. This routine tries to optimize insertions performed by
5114 * the "redo", "undo" or "put" commands, so it needs to know when it should
5115 * stop and defer processing to the "normal" mechanism.
5116 * '0' and '^' are special, because they can be followed by CTRL-D.
5117 */
5118#ifdef EBCDIC
5119# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5120#else
5121# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5122#endif
5123
5124#ifdef FEAT_MBYTE
5125# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5126#else
5127# define WHITECHAR(cc) vim_iswhite(cc)
5128#endif
5129
5130 void
5131insertchar(c, flags, second_indent)
5132 int c; /* character to insert or NUL */
5133 int flags; /* INSCHAR_FORMAT, etc. */
5134 int second_indent; /* indent for second line if >= 0 */
5135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 int textwidth;
5137#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5143 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * Try to break the line in two or more pieces when:
5147 * - Always do this if we have been called to do formatting only.
5148 * - Always do this when 'formatoptions' has the 'a' flag and the line
5149 * ends in white space.
5150 * - Otherwise:
5151 * - Don't do this if inserting a blank
5152 * - Don't do this if an existing character is being replaced, unless
5153 * we're in VREPLACE mode.
5154 * - Do this if the cursor is not on the line where insert started
5155 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5156 * before the insert.
5157 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5158 * before 'textwidth'
5159 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005160 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 && ((flags & INSCHAR_FORMAT)
5162 || (!vim_iswhite(c)
5163 && !((State & REPLACE_FLAG)
5164#ifdef FEAT_VREPLACE
5165 && !(State & VREPLACE_FLAG)
5166#endif
5167 && *ml_get_cursor() != NUL)
5168 && (curwin->w_cursor.lnum != Insstart.lnum
5169 || ((!has_format_option(FO_INS_LONG)
5170 || Insstart_textlen <= (colnr_T)textwidth)
5171 && (!fo_ins_blank
5172 || Insstart_blank_vcol <= (colnr_T)textwidth
5173 ))))))
5174 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005175 /* Format with 'formatexpr' when it's set. Use internal formatting
5176 * when 'formatexpr' isn't set or it returns non-zero. */
5177#if defined(FEAT_EVAL)
5178 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005179 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005181 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (c == NUL) /* only formatting was wanted */
5185 return;
5186
5187#ifdef FEAT_COMMENTS
5188 /* Check whether this character should end a comment. */
5189 if (did_ai && (int)c == end_comment_pending)
5190 {
5191 char_u *line;
5192 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5193 int middle_len, end_len;
5194 int i;
5195
5196 /*
5197 * Need to remove existing (middle) comment leader and insert end
5198 * comment leader. First, check what comment leader we can find.
5199 */
5200 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5201 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5202 {
5203 /* Skip middle-comment string */
5204 while (*p && p[-1] != ':') /* find end of middle flags */
5205 ++p;
5206 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5207 /* Don't count trailing white space for middle_len */
5208 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5209 --middle_len;
5210
5211 /* Find the end-comment string */
5212 while (*p && p[-1] != ':') /* find end of end flags */
5213 ++p;
5214 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5215
5216 /* Skip white space before the cursor */
5217 i = curwin->w_cursor.col;
5218 while (--i >= 0 && vim_iswhite(line[i]))
5219 ;
5220 i++;
5221
5222 /* Skip to before the middle leader */
5223 i -= middle_len;
5224
5225 /* Check some expected things before we go on */
5226 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5227 {
5228 /* Backspace over all the stuff we want to replace */
5229 backspace_until_column(i);
5230
5231 /*
5232 * Insert the end-comment string, except for the last
5233 * character, which will get inserted as normal later.
5234 */
5235 ins_bytes_len(lead_end, end_len - 1);
5236 }
5237 }
5238 }
5239 end_comment_pending = NUL;
5240#endif
5241
5242 did_ai = FALSE;
5243#ifdef FEAT_SMARTINDENT
5244 did_si = FALSE;
5245 can_si = FALSE;
5246 can_si_back = FALSE;
5247#endif
5248
5249 /*
5250 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5251 * This speeds up normal text input considerably.
5252 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5253 * need to re-indent at a ':', or any other character (but not what
5254 * 'paste' is set)..
5255 */
5256#ifdef USE_ON_FLY_SCROLL
5257 dont_scroll = FALSE; /* allow scrolling here */
5258#endif
5259
5260 if ( !ISSPECIAL(c)
5261#ifdef FEAT_MBYTE
5262 && (!has_mbyte || (*mb_char2len)(c) == 1)
5263#endif
5264 && vpeekc() != NUL
5265 && !(State & REPLACE_FLAG)
5266#ifdef FEAT_CINDENT
5267 && !cindent_on()
5268#endif
5269#ifdef FEAT_RIGHTLEFT
5270 && !p_ri
5271#endif
5272 )
5273 {
5274#define INPUT_BUFLEN 100
5275 char_u buf[INPUT_BUFLEN + 1];
5276 int i;
5277 colnr_T virtcol = 0;
5278
5279 buf[0] = c;
5280 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005281 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 virtcol = get_nolist_virtcol();
5283 /*
5284 * Stop the string when:
5285 * - no more chars available
5286 * - finding a special character (command key)
5287 * - buffer is full
5288 * - running into the 'textwidth' boundary
5289 * - need to check for abbreviation: A non-word char after a word-char
5290 */
5291 while ( (c = vpeekc()) != NUL
5292 && !ISSPECIAL(c)
5293#ifdef FEAT_MBYTE
5294 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5295#endif
5296 && i < INPUT_BUFLEN
5297 && (textwidth == 0
5298 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5299 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5300 {
5301#ifdef FEAT_RIGHTLEFT
5302 c = vgetc();
5303 if (p_hkmap && KeyTyped)
5304 c = hkmap(c); /* Hebrew mode mapping */
5305# ifdef FEAT_FKMAP
5306 if (p_fkmap && KeyTyped)
5307 c = fkmap(c); /* Farsi mode mapping */
5308# endif
5309 buf[i++] = c;
5310#else
5311 buf[i++] = vgetc();
5312#endif
5313 }
5314
5315#ifdef FEAT_DIGRAPHS
5316 do_digraph(-1); /* clear digraphs */
5317 do_digraph(buf[i-1]); /* may be the start of a digraph */
5318#endif
5319 buf[i] = NUL;
5320 ins_str(buf);
5321 if (flags & INSCHAR_CTRLV)
5322 {
5323 redo_literal(*buf);
5324 i = 1;
5325 }
5326 else
5327 i = 0;
5328 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005329 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331 else
5332 {
5333#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005334 int cc;
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5337 {
5338 char_u buf[MB_MAXBYTES + 1];
5339
5340 (*mb_char2bytes)(c, buf);
5341 buf[cc] = NUL;
5342 ins_char_bytes(buf, cc);
5343 AppendCharToRedobuff(c);
5344 }
5345 else
5346#endif
5347 {
5348 ins_char(c);
5349 if (flags & INSCHAR_CTRLV)
5350 redo_literal(c);
5351 else
5352 AppendCharToRedobuff(c);
5353 }
5354 }
5355}
5356
5357/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005358 * Format text at the current insert position.
5359 */
5360 static void
5361internal_format(textwidth, second_indent, flags, format_only)
5362 int textwidth;
5363 int second_indent;
5364 int flags;
5365 int format_only;
5366{
5367 int cc;
5368 int save_char = NUL;
5369 int haveto_redraw = FALSE;
5370 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5371#ifdef FEAT_MBYTE
5372 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5373#endif
5374 int fo_white_par = has_format_option(FO_WHITE_PAR);
5375 int first_line = TRUE;
5376#ifdef FEAT_COMMENTS
5377 colnr_T leader_len;
5378 int no_leader = FALSE;
5379 int do_comments = (flags & INSCHAR_DO_COM);
5380#endif
5381
5382 /*
5383 * When 'ai' is off we don't want a space under the cursor to be
5384 * deleted. Replace it with an 'x' temporarily.
5385 */
5386 if (!curbuf->b_p_ai)
5387 {
5388 cc = gchar_cursor();
5389 if (vim_iswhite(cc))
5390 {
5391 save_char = cc;
5392 pchar_cursor('x');
5393 }
5394 }
5395
5396 /*
5397 * Repeat breaking lines, until the current line is not too long.
5398 */
5399 while (!got_int)
5400 {
5401 int startcol; /* Cursor column at entry */
5402 int wantcol; /* column at textwidth border */
5403 int foundcol; /* column for start of spaces */
5404 int end_foundcol = 0; /* column for start of word */
5405 colnr_T len;
5406 colnr_T virtcol;
5407#ifdef FEAT_VREPLACE
5408 int orig_col = 0;
5409 char_u *saved_text = NULL;
5410#endif
5411 colnr_T col;
5412
5413 virtcol = get_nolist_virtcol();
5414 if (virtcol < (colnr_T)textwidth)
5415 break;
5416
5417#ifdef FEAT_COMMENTS
5418 if (no_leader)
5419 do_comments = FALSE;
5420 else if (!(flags & INSCHAR_FORMAT)
5421 && has_format_option(FO_WRAP_COMS))
5422 do_comments = TRUE;
5423
5424 /* Don't break until after the comment leader */
5425 if (do_comments)
5426 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5427 else
5428 leader_len = 0;
5429
5430 /* If the line doesn't start with a comment leader, then don't
5431 * start one in a following broken line. Avoids that a %word
5432 * moved to the start of the next line causes all following lines
5433 * to start with %. */
5434 if (leader_len == 0)
5435 no_leader = TRUE;
5436#endif
5437 if (!(flags & INSCHAR_FORMAT)
5438#ifdef FEAT_COMMENTS
5439 && leader_len == 0
5440#endif
5441 && !has_format_option(FO_WRAP))
5442
5443 {
5444 textwidth = 0;
5445 break;
5446 }
5447 if ((startcol = curwin->w_cursor.col) == 0)
5448 break;
5449
5450 /* find column of textwidth border */
5451 coladvance((colnr_T)textwidth);
5452 wantcol = curwin->w_cursor.col;
5453
5454 curwin->w_cursor.col = startcol - 1;
5455#ifdef FEAT_MBYTE
5456 /* Correct cursor for multi-byte character. */
5457 if (has_mbyte)
5458 mb_adjust_cursor();
5459#endif
5460 foundcol = 0;
5461
5462 /*
5463 * Find position to break at.
5464 * Stop at first entered white when 'formatoptions' has 'v'
5465 */
5466 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5467 || curwin->w_cursor.lnum != Insstart.lnum
5468 || curwin->w_cursor.col >= Insstart.col)
5469 {
5470 cc = gchar_cursor();
5471 if (WHITECHAR(cc))
5472 {
5473 /* remember position of blank just before text */
5474 end_foundcol = curwin->w_cursor.col;
5475
5476 /* find start of sequence of blanks */
5477 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5478 {
5479 dec_cursor();
5480 cc = gchar_cursor();
5481 }
5482 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5483 break; /* only spaces in front of text */
5484#ifdef FEAT_COMMENTS
5485 /* Don't break until after the comment leader */
5486 if (curwin->w_cursor.col < leader_len)
5487 break;
5488#endif
5489 if (has_format_option(FO_ONE_LETTER))
5490 {
5491 /* do not break after one-letter words */
5492 if (curwin->w_cursor.col == 0)
5493 break; /* one-letter word at begin */
5494
5495 col = curwin->w_cursor.col;
5496 dec_cursor();
5497 cc = gchar_cursor();
5498
5499 if (WHITECHAR(cc))
5500 continue; /* one-letter, continue */
5501 curwin->w_cursor.col = col;
5502 }
5503#ifdef FEAT_MBYTE
5504 if (has_mbyte)
5505 foundcol = curwin->w_cursor.col
5506 + (*mb_ptr2len)(ml_get_cursor());
5507 else
5508#endif
5509 foundcol = curwin->w_cursor.col + 1;
5510 if (curwin->w_cursor.col < (colnr_T)wantcol)
5511 break;
5512 }
5513#ifdef FEAT_MBYTE
5514 else if (cc >= 0x100 && fo_multibyte
5515 && curwin->w_cursor.col <= (colnr_T)wantcol)
5516 {
5517 /* Break after or before a multi-byte character. */
5518 foundcol = curwin->w_cursor.col;
5519 if (curwin->w_cursor.col < (colnr_T)wantcol)
5520 foundcol += (*mb_char2len)(cc);
5521 end_foundcol = foundcol;
5522 break;
5523 }
5524#endif
5525 if (curwin->w_cursor.col == 0)
5526 break;
5527 dec_cursor();
5528 }
5529
5530 if (foundcol == 0) /* no spaces, cannot break line */
5531 {
5532 curwin->w_cursor.col = startcol;
5533 break;
5534 }
5535
5536 /* Going to break the line, remove any "$" now. */
5537 undisplay_dollar();
5538
5539 /*
5540 * Offset between cursor position and line break is used by replace
5541 * stack functions. VREPLACE does not use this, and backspaces
5542 * over the text instead.
5543 */
5544#ifdef FEAT_VREPLACE
5545 if (State & VREPLACE_FLAG)
5546 orig_col = startcol; /* Will start backspacing from here */
5547 else
5548#endif
5549 replace_offset = startcol - end_foundcol - 1;
5550
5551 /*
5552 * adjust startcol for spaces that will be deleted and
5553 * characters that will remain on top line
5554 */
5555 curwin->w_cursor.col = foundcol;
5556 while (cc = gchar_cursor(), WHITECHAR(cc))
5557 inc_cursor();
5558 startcol -= curwin->w_cursor.col;
5559 if (startcol < 0)
5560 startcol = 0;
5561
5562#ifdef FEAT_VREPLACE
5563 if (State & VREPLACE_FLAG)
5564 {
5565 /*
5566 * In VREPLACE mode, we will backspace over the text to be
5567 * wrapped, so save a copy now to put on the next line.
5568 */
5569 saved_text = vim_strsave(ml_get_cursor());
5570 curwin->w_cursor.col = orig_col;
5571 if (saved_text == NULL)
5572 break; /* Can't do it, out of memory */
5573 saved_text[startcol] = NUL;
5574
5575 /* Backspace over characters that will move to the next line */
5576 if (!fo_white_par)
5577 backspace_until_column(foundcol);
5578 }
5579 else
5580#endif
5581 {
5582 /* put cursor after pos. to break line */
5583 if (!fo_white_par)
5584 curwin->w_cursor.col = foundcol;
5585 }
5586
5587 /*
5588 * Split the line just before the margin.
5589 * Only insert/delete lines, but don't really redraw the window.
5590 */
5591 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5592 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5593#ifdef FEAT_COMMENTS
5594 + (do_comments ? OPENLINE_DO_COM : 0)
5595#endif
5596 , old_indent);
5597 old_indent = 0;
5598
5599 replace_offset = 0;
5600 if (first_line)
5601 {
5602 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5603 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5604 if (second_indent >= 0)
5605 {
5606#ifdef FEAT_VREPLACE
5607 if (State & VREPLACE_FLAG)
5608 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5609 else
5610#endif
5611 (void)set_indent(second_indent, SIN_CHANGED);
5612 }
5613 first_line = FALSE;
5614 }
5615
5616#ifdef FEAT_VREPLACE
5617 if (State & VREPLACE_FLAG)
5618 {
5619 /*
5620 * In VREPLACE mode we have backspaced over the text to be
5621 * moved, now we re-insert it into the new line.
5622 */
5623 ins_bytes(saved_text);
5624 vim_free(saved_text);
5625 }
5626 else
5627#endif
5628 {
5629 /*
5630 * Check if cursor is not past the NUL off the line, cindent
5631 * may have added or removed indent.
5632 */
5633 curwin->w_cursor.col += startcol;
5634 len = (colnr_T)STRLEN(ml_get_curline());
5635 if (curwin->w_cursor.col > len)
5636 curwin->w_cursor.col = len;
5637 }
5638
5639 haveto_redraw = TRUE;
5640#ifdef FEAT_CINDENT
5641 can_cindent = TRUE;
5642#endif
5643 /* moved the cursor, don't autoindent or cindent now */
5644 did_ai = FALSE;
5645#ifdef FEAT_SMARTINDENT
5646 did_si = FALSE;
5647 can_si = FALSE;
5648 can_si_back = FALSE;
5649#endif
5650 line_breakcheck();
5651 }
5652
5653 if (save_char != NUL) /* put back space after cursor */
5654 pchar_cursor(save_char);
5655
5656 if (!format_only && haveto_redraw)
5657 {
5658 update_topline();
5659 redraw_curbuf_later(VALID);
5660 }
5661}
5662
5663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 * Called after inserting or deleting text: When 'formatoptions' includes the
5665 * 'a' flag format from the current line until the end of the paragraph.
5666 * Keep the cursor at the same position relative to the text.
5667 * The caller must have saved the cursor line for undo, following ones will be
5668 * saved here.
5669 */
5670 void
5671auto_format(trailblank, prev_line)
5672 int trailblank; /* when TRUE also format with trailing blank */
5673 int prev_line; /* may start in previous line */
5674{
5675 pos_T pos;
5676 colnr_T len;
5677 char_u *old;
5678 char_u *new, *pnew;
5679 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005680 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 if (!has_format_option(FO_AUTO))
5683 return;
5684
5685 pos = curwin->w_cursor;
5686 old = ml_get_curline();
5687
5688 /* may remove added space */
5689 check_auto_format(FALSE);
5690
5691 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5692 * user might insert normal text next. Also skip formatting when "1" is
5693 * in 'formatoptions' and there is a single character before the cursor.
5694 * Otherwise the line would be broken and when typing another non-white
5695 * next they are not joined back together. */
5696 wasatend = (pos.col == STRLEN(old));
5697 if (*old != NUL && !trailblank && wasatend)
5698 {
5699 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005700 cc = gchar_cursor();
5701 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5702 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005704 cc = gchar_cursor();
5705 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 curwin->w_cursor = pos;
5708 return;
5709 }
5710 curwin->w_cursor = pos;
5711 }
5712
5713#ifdef FEAT_COMMENTS
5714 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5715 * comments. */
5716 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5717 && get_leader_len(old, NULL, FALSE) == 0)
5718 return;
5719#endif
5720
5721 /*
5722 * May start formatting in a previous line, so that after "x" a word is
5723 * moved to the previous line if it fits there now. Only when this is not
5724 * the start of a paragraph.
5725 */
5726 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5727 {
5728 --curwin->w_cursor.lnum;
5729 if (u_save_cursor() == FAIL)
5730 return;
5731 }
5732
5733 /*
5734 * Do the formatting and restore the cursor position. "saved_cursor" will
5735 * be adjusted for the text formatting.
5736 */
5737 saved_cursor = pos;
5738 format_lines((linenr_T)-1);
5739 curwin->w_cursor = saved_cursor;
5740 saved_cursor.lnum = 0;
5741
5742 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5743 {
5744 /* "cannot happen" */
5745 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5746 coladvance((colnr_T)MAXCOL);
5747 }
5748 else
5749 check_cursor_col();
5750
5751 /* Insert mode: If the cursor is now after the end of the line while it
5752 * previously wasn't, the line was broken. Because of the rule above we
5753 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5754 * formatted. */
5755 if (!wasatend && has_format_option(FO_WHITE_PAR))
5756 {
5757 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005758 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 if (curwin->w_cursor.col == len)
5760 {
5761 pnew = vim_strnsave(new, len + 2);
5762 pnew[len] = ' ';
5763 pnew[len + 1] = NUL;
5764 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5765 /* remove the space later */
5766 did_add_space = TRUE;
5767 }
5768 else
5769 /* may remove added space */
5770 check_auto_format(FALSE);
5771 }
5772
5773 check_cursor();
5774}
5775
5776/*
5777 * When an extra space was added to continue a paragraph for auto-formatting,
5778 * delete it now. The space must be under the cursor, just after the insert
5779 * position.
5780 */
5781 static void
5782check_auto_format(end_insert)
5783 int end_insert; /* TRUE when ending Insert mode */
5784{
5785 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005786 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 if (did_add_space)
5789 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005790 cc = gchar_cursor();
5791 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 /* Somehow the space was removed already. */
5793 did_add_space = FALSE;
5794 else
5795 {
5796 if (!end_insert)
5797 {
5798 inc_cursor();
5799 c = gchar_cursor();
5800 dec_cursor();
5801 }
5802 if (c != NUL)
5803 {
5804 /* The space is no longer at the end of the line, delete it. */
5805 del_char(FALSE);
5806 did_add_space = FALSE;
5807 }
5808 }
5809 }
5810}
5811
5812/*
5813 * Find out textwidth to be used for formatting:
5814 * if 'textwidth' option is set, use it
5815 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5816 * if invalid value, use 0.
5817 * Set default to window width (maximum 79) for "gq" operator.
5818 */
5819 int
5820comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005821 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 int textwidth;
5824
5825 textwidth = curbuf->b_p_tw;
5826 if (textwidth == 0 && curbuf->b_p_wm)
5827 {
5828 /* The width is the window width minus 'wrapmargin' minus all the
5829 * things that add to the margin. */
5830 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5831#ifdef FEAT_CMDWIN
5832 if (cmdwin_type != 0)
5833 textwidth -= 1;
5834#endif
5835#ifdef FEAT_FOLDING
5836 textwidth -= curwin->w_p_fdc;
5837#endif
5838#ifdef FEAT_SIGNS
5839 if (curwin->w_buffer->b_signlist != NULL
5840# ifdef FEAT_NETBEANS_INTG
5841 || usingNetbeans
5842# endif
5843 )
5844 textwidth -= 1;
5845#endif
5846 if (curwin->w_p_nu)
5847 textwidth -= 8;
5848 }
5849 if (textwidth < 0)
5850 textwidth = 0;
5851 if (ff && textwidth == 0)
5852 {
5853 textwidth = W_WIDTH(curwin) - 1;
5854 if (textwidth > 79)
5855 textwidth = 79;
5856 }
5857 return textwidth;
5858}
5859
5860/*
5861 * Put a character in the redo buffer, for when just after a CTRL-V.
5862 */
5863 static void
5864redo_literal(c)
5865 int c;
5866{
5867 char_u buf[10];
5868
5869 /* Only digits need special treatment. Translate them into a string of
5870 * three digits. */
5871 if (VIM_ISDIGIT(c))
5872 {
5873 sprintf((char *)buf, "%03d", c);
5874 AppendToRedobuff(buf);
5875 }
5876 else
5877 AppendCharToRedobuff(c);
5878}
5879
5880/*
5881 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005882 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 */
5884 static void
5885start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005886 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 if (!arrow_used) /* something has been inserted */
5889 {
5890 AppendToRedobuff(ESC_STR);
5891 stop_insert(end_insert_pos, FALSE);
5892 arrow_used = TRUE; /* this means we stopped the current insert */
5893 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005894#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005895 check_spell_redraw();
5896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005899#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005900/*
5901 * If we skipped highlighting word at cursor, do it now.
5902 * It may be skipped again, thus reset spell_redraw_lnum first.
5903 */
5904 static void
5905check_spell_redraw()
5906{
5907 if (spell_redraw_lnum != 0)
5908 {
5909 linenr_T lnum = spell_redraw_lnum;
5910
5911 spell_redraw_lnum = 0;
5912 redrawWinline(lnum, FALSE);
5913 }
5914}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005915
5916/*
5917 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5918 * spelled word, if there is one.
5919 */
5920 static void
5921spell_back_to_badword()
5922{
5923 pos_T tpos = curwin->w_cursor;
5924
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005925 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005926 if (curwin->w_cursor.col != tpos.col)
5927 start_arrow(&tpos);
5928}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005929#endif
5930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931/*
5932 * stop_arrow() is called before a change is made in insert mode.
5933 * If an arrow key has been used, start a new insertion.
5934 * Returns FAIL if undo is impossible, shouldn't insert then.
5935 */
5936 int
5937stop_arrow()
5938{
5939 if (arrow_used)
5940 {
5941 if (u_save_cursor() == OK)
5942 {
5943 arrow_used = FALSE;
5944 ins_need_undo = FALSE;
5945 }
5946 Insstart = curwin->w_cursor; /* new insertion starts here */
5947 Insstart_textlen = linetabsize(ml_get_curline());
5948 ai_col = 0;
5949#ifdef FEAT_VREPLACE
5950 if (State & VREPLACE_FLAG)
5951 {
5952 orig_line_count = curbuf->b_ml.ml_line_count;
5953 vr_lines_changed = 1;
5954 }
5955#endif
5956 ResetRedobuff();
5957 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005958 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 }
5960 else if (ins_need_undo)
5961 {
5962 if (u_save_cursor() == OK)
5963 ins_need_undo = FALSE;
5964 }
5965
5966#ifdef FEAT_FOLDING
5967 /* Always open fold at the cursor line when inserting something. */
5968 foldOpenCursor();
5969#endif
5970
5971 return (arrow_used || ins_need_undo ? FAIL : OK);
5972}
5973
5974/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005975 * Do a few things to stop inserting.
5976 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
5977 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 */
5979 static void
5980stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005981 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005982 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005984 int cc;
5985 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 stop_redo_ins();
5988 replace_flush(); /* abandon replace stack */
5989
5990 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005991 * Save the inserted text for later redo with ^@ and CTRL-A.
5992 * Don't do it when "restart_edit" was set and nothing was inserted,
5993 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005995 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005996 if (did_restart_edit == 0 || (ptr != NULL
5997 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005998 {
5999 vim_free(last_insert);
6000 last_insert = ptr;
6001 last_insert_skip = new_insert_skip;
6002 }
6003 else
6004 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006006 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {
6008 /* Auto-format now. It may seem strange to do this when stopping an
6009 * insertion (or moving the cursor), but it's required when appending
6010 * a line and having it end in a space. But only do it when something
6011 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006012 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006014 pos_T tpos = curwin->w_cursor;
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 /* When the cursor is at the end of the line after a space the
6017 * formatting will move it to the following word. Avoid that by
6018 * moving the cursor onto the space. */
6019 cc = 'x';
6020 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6021 {
6022 dec_cursor();
6023 cc = gchar_cursor();
6024 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006025 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
6027
6028 auto_format(TRUE, FALSE);
6029
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006030 if (vim_iswhite(cc))
6031 {
6032 if (gchar_cursor() != NUL)
6033 inc_cursor();
6034#ifdef FEAT_VIRTUALEDIT
6035 /* If the cursor is still at the same character, also keep
6036 * the "coladd". */
6037 if (gchar_cursor() == NUL
6038 && curwin->w_cursor.lnum == tpos.lnum
6039 && curwin->w_cursor.col == tpos.col)
6040 curwin->w_cursor.coladd = tpos.coladd;
6041#endif
6042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 }
6044
6045 /* If a space was inserted for auto-formatting, remove it now. */
6046 check_auto_format(TRUE);
6047
6048 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006049 * of the line, and put the cursor back.
6050 * Do this when ESC was used or moving the cursor up/down. */
6051 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006052 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006054 pos_T tpos = curwin->w_cursor;
6055
6056 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006057 for (;;)
6058 {
6059 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6060 --curwin->w_cursor.col;
6061 cc = gchar_cursor();
6062 if (!vim_iswhite(cc))
6063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006065 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006066 if (curwin->w_cursor.lnum != tpos.lnum)
6067 curwin->w_cursor = tpos;
6068 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6070
6071#ifdef FEAT_VISUAL
6072 /* <C-S-Right> may have started Visual mode, adjust the position for
6073 * deleted characters. */
6074 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6075 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006076 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (VIsual.col > (colnr_T)cc)
6078 {
6079 VIsual.col = cc;
6080# ifdef FEAT_VIRTUALEDIT
6081 VIsual.coladd = 0;
6082# endif
6083 }
6084 }
6085#endif
6086 }
6087 }
6088 did_ai = FALSE;
6089#ifdef FEAT_SMARTINDENT
6090 did_si = FALSE;
6091 can_si = FALSE;
6092 can_si_back = FALSE;
6093#endif
6094
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006095 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6096 * now in a different buffer. */
6097 if (end_insert_pos != NULL)
6098 {
6099 curbuf->b_op_start = Insstart;
6100 curbuf->b_op_end = *end_insert_pos;
6101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102}
6103
6104/*
6105 * Set the last inserted text to a single character.
6106 * Used for the replace command.
6107 */
6108 void
6109set_last_insert(c)
6110 int c;
6111{
6112 char_u *s;
6113
6114 vim_free(last_insert);
6115#ifdef FEAT_MBYTE
6116 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6117#else
6118 last_insert = alloc(6);
6119#endif
6120 if (last_insert != NULL)
6121 {
6122 s = last_insert;
6123 /* Use the CTRL-V only when entering a special char */
6124 if (c < ' ' || c == DEL)
6125 *s++ = Ctrl_V;
6126 s = add_char2buf(c, s);
6127 *s++ = ESC;
6128 *s++ = NUL;
6129 last_insert_skip = 0;
6130 }
6131}
6132
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006133#if defined(EXITFREE) || defined(PROTO)
6134 void
6135free_last_insert()
6136{
6137 vim_free(last_insert);
6138 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006139 vim_free(compl_orig_text);
6140 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006141}
6142#endif
6143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144/*
6145 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6146 * and CSI. Handle multi-byte characters.
6147 * Returns a pointer to after the added bytes.
6148 */
6149 char_u *
6150add_char2buf(c, s)
6151 int c;
6152 char_u *s;
6153{
6154#ifdef FEAT_MBYTE
6155 char_u temp[MB_MAXBYTES];
6156 int i;
6157 int len;
6158
6159 len = (*mb_char2bytes)(c, temp);
6160 for (i = 0; i < len; ++i)
6161 {
6162 c = temp[i];
6163#endif
6164 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6165 if (c == K_SPECIAL)
6166 {
6167 *s++ = K_SPECIAL;
6168 *s++ = KS_SPECIAL;
6169 *s++ = KE_FILLER;
6170 }
6171#ifdef FEAT_GUI
6172 else if (c == CSI)
6173 {
6174 *s++ = CSI;
6175 *s++ = KS_EXTRA;
6176 *s++ = (int)KE_CSI;
6177 }
6178#endif
6179 else
6180 *s++ = c;
6181#ifdef FEAT_MBYTE
6182 }
6183#endif
6184 return s;
6185}
6186
6187/*
6188 * move cursor to start of line
6189 * if flags & BL_WHITE move to first non-white
6190 * if flags & BL_SOL move to first non-white if startofline is set,
6191 * otherwise keep "curswant" column
6192 * if flags & BL_FIX don't leave the cursor on a NUL.
6193 */
6194 void
6195beginline(flags)
6196 int flags;
6197{
6198 if ((flags & BL_SOL) && !p_sol)
6199 coladvance(curwin->w_curswant);
6200 else
6201 {
6202 curwin->w_cursor.col = 0;
6203#ifdef FEAT_VIRTUALEDIT
6204 curwin->w_cursor.coladd = 0;
6205#endif
6206
6207 if (flags & (BL_WHITE | BL_SOL))
6208 {
6209 char_u *ptr;
6210
6211 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6212 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6213 ++curwin->w_cursor.col;
6214 }
6215 curwin->w_set_curswant = TRUE;
6216 }
6217}
6218
6219/*
6220 * oneright oneleft cursor_down cursor_up
6221 *
6222 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006223 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 * Return OK when successful, FAIL when we hit a line of file boundary.
6225 */
6226
6227 int
6228oneright()
6229{
6230 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233#ifdef FEAT_VIRTUALEDIT
6234 if (virtual_active())
6235 {
6236 pos_T prevpos = curwin->w_cursor;
6237
6238 /* Adjust for multi-wide char (excluding TAB) */
6239 ptr = ml_get_cursor();
6240 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006241# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006243# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 ))
6247 ? ptr2cells(ptr) : 1));
6248 curwin->w_set_curswant = TRUE;
6249 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6250 return (prevpos.col != curwin->w_cursor.col
6251 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6252 }
6253#endif
6254
6255 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006256 if (*ptr == NUL)
6257 return FAIL; /* already at the very end */
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006260 if (has_mbyte)
6261 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 else
6263#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006264 l = 1;
6265
6266 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6267 * contains "onemore". */
6268 if (ptr[l] == NUL
6269#ifdef FEAT_VIRTUALEDIT
6270 && (ve_flags & VE_ONEMORE) == 0
6271#endif
6272 )
6273 return FAIL;
6274 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 curwin->w_set_curswant = TRUE;
6277 return OK;
6278}
6279
6280 int
6281oneleft()
6282{
6283#ifdef FEAT_VIRTUALEDIT
6284 if (virtual_active())
6285 {
6286 int width;
6287 int v = getviscol();
6288
6289 if (v == 0)
6290 return FAIL;
6291
6292# ifdef FEAT_LINEBREAK
6293 /* We might get stuck on 'showbreak', skip over it. */
6294 width = 1;
6295 for (;;)
6296 {
6297 coladvance(v - width);
6298 /* getviscol() is slow, skip it when 'showbreak' is empty and
6299 * there are no multi-byte characters */
6300 if ((*p_sbr == NUL
6301# ifdef FEAT_MBYTE
6302 && !has_mbyte
6303# endif
6304 ) || getviscol() < v)
6305 break;
6306 ++width;
6307 }
6308# else
6309 coladvance(v - 1);
6310# endif
6311
6312 if (curwin->w_cursor.coladd == 1)
6313 {
6314 char_u *ptr;
6315
6316 /* Adjust for multi-wide char (not a TAB) */
6317 ptr = ml_get_cursor();
6318 if (*ptr != TAB && vim_isprintc(
6319# ifdef FEAT_MBYTE
6320 (*mb_ptr2char)(ptr)
6321# else
6322 *ptr
6323# endif
6324 ) && ptr2cells(ptr) > 1)
6325 curwin->w_cursor.coladd = 0;
6326 }
6327
6328 curwin->w_set_curswant = TRUE;
6329 return OK;
6330 }
6331#endif
6332
6333 if (curwin->w_cursor.col == 0)
6334 return FAIL;
6335
6336 curwin->w_set_curswant = TRUE;
6337 --curwin->w_cursor.col;
6338
6339#ifdef FEAT_MBYTE
6340 /* if the character on the left of the current cursor is a multi-byte
6341 * character, move to its first byte */
6342 if (has_mbyte)
6343 mb_adjust_cursor();
6344#endif
6345 return OK;
6346}
6347
6348 int
6349cursor_up(n, upd_topline)
6350 long n;
6351 int upd_topline; /* When TRUE: update topline */
6352{
6353 linenr_T lnum;
6354
6355 if (n > 0)
6356 {
6357 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006358 /* This fails if the cursor is already in the first line or the count
6359 * is larger than the line number and '-' is in 'cpoptions' */
6360 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 return FAIL;
6362 if (n >= lnum)
6363 lnum = 1;
6364 else
6365#ifdef FEAT_FOLDING
6366 if (hasAnyFolding(curwin))
6367 {
6368 /*
6369 * Count each sequence of folded lines as one logical line.
6370 */
6371 /* go to the the start of the current fold */
6372 (void)hasFolding(lnum, &lnum, NULL);
6373
6374 while (n--)
6375 {
6376 /* move up one line */
6377 --lnum;
6378 if (lnum <= 1)
6379 break;
6380 /* If we entered a fold, move to the beginning, unless in
6381 * Insert mode or when 'foldopen' contains "all": it will open
6382 * in a moment. */
6383 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6384 (void)hasFolding(lnum, &lnum, NULL);
6385 }
6386 if (lnum < 1)
6387 lnum = 1;
6388 }
6389 else
6390#endif
6391 lnum -= n;
6392 curwin->w_cursor.lnum = lnum;
6393 }
6394
6395 /* try to advance to the column we want to be at */
6396 coladvance(curwin->w_curswant);
6397
6398 if (upd_topline)
6399 update_topline(); /* make sure curwin->w_topline is valid */
6400
6401 return OK;
6402}
6403
6404/*
6405 * Cursor down a number of logical lines.
6406 */
6407 int
6408cursor_down(n, upd_topline)
6409 long n;
6410 int upd_topline; /* When TRUE: update topline */
6411{
6412 linenr_T lnum;
6413
6414 if (n > 0)
6415 {
6416 lnum = curwin->w_cursor.lnum;
6417#ifdef FEAT_FOLDING
6418 /* Move to last line of fold, will fail if it's the end-of-file. */
6419 (void)hasFolding(lnum, NULL, &lnum);
6420#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006421 /* This fails if the cursor is already in the last line or would move
6422 * beyound the last line and '-' is in 'cpoptions' */
6423 if (lnum >= curbuf->b_ml.ml_line_count
6424 || (lnum + n > curbuf->b_ml.ml_line_count
6425 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 return FAIL;
6427 if (lnum + n >= curbuf->b_ml.ml_line_count)
6428 lnum = curbuf->b_ml.ml_line_count;
6429 else
6430#ifdef FEAT_FOLDING
6431 if (hasAnyFolding(curwin))
6432 {
6433 linenr_T last;
6434
6435 /* count each sequence of folded lines as one logical line */
6436 while (n--)
6437 {
6438 if (hasFolding(lnum, NULL, &last))
6439 lnum = last + 1;
6440 else
6441 ++lnum;
6442 if (lnum >= curbuf->b_ml.ml_line_count)
6443 break;
6444 }
6445 if (lnum > curbuf->b_ml.ml_line_count)
6446 lnum = curbuf->b_ml.ml_line_count;
6447 }
6448 else
6449#endif
6450 lnum += n;
6451 curwin->w_cursor.lnum = lnum;
6452 }
6453
6454 /* try to advance to the column we want to be at */
6455 coladvance(curwin->w_curswant);
6456
6457 if (upd_topline)
6458 update_topline(); /* make sure curwin->w_topline is valid */
6459
6460 return OK;
6461}
6462
6463/*
6464 * Stuff the last inserted text in the read buffer.
6465 * Last_insert actually is a copy of the redo buffer, so we
6466 * first have to remove the command.
6467 */
6468 int
6469stuff_inserted(c, count, no_esc)
6470 int c; /* Command character to be inserted */
6471 long count; /* Repeat this many times */
6472 int no_esc; /* Don't add an ESC at the end */
6473{
6474 char_u *esc_ptr;
6475 char_u *ptr;
6476 char_u *last_ptr;
6477 char_u last = NUL;
6478
6479 ptr = get_last_insert();
6480 if (ptr == NULL)
6481 {
6482 EMSG(_(e_noinstext));
6483 return FAIL;
6484 }
6485
6486 /* may want to stuff the command character, to start Insert mode */
6487 if (c != NUL)
6488 stuffcharReadbuff(c);
6489 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6490 *esc_ptr = NUL; /* remove the ESC */
6491
6492 /* when the last char is either "0" or "^" it will be quoted if no ESC
6493 * comes after it OR if it will inserted more than once and "ptr"
6494 * starts with ^D. -- Acevedo
6495 */
6496 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6497 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6498 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6499 {
6500 last = *last_ptr;
6501 *last_ptr = NUL;
6502 }
6503
6504 do
6505 {
6506 stuffReadbuff(ptr);
6507 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6508 if (last)
6509 stuffReadbuff((char_u *)(last == '0'
6510 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6511 : IF_EB("\026^", CTRL_V_STR "^")));
6512 }
6513 while (--count > 0);
6514
6515 if (last)
6516 *last_ptr = last;
6517
6518 if (esc_ptr != NULL)
6519 *esc_ptr = ESC; /* put the ESC back */
6520
6521 /* may want to stuff a trailing ESC, to get out of Insert mode */
6522 if (!no_esc)
6523 stuffcharReadbuff(ESC);
6524
6525 return OK;
6526}
6527
6528 char_u *
6529get_last_insert()
6530{
6531 if (last_insert == NULL)
6532 return NULL;
6533 return last_insert + last_insert_skip;
6534}
6535
6536/*
6537 * Get last inserted string, and remove trailing <Esc>.
6538 * Returns pointer to allocated memory (must be freed) or NULL.
6539 */
6540 char_u *
6541get_last_insert_save()
6542{
6543 char_u *s;
6544 int len;
6545
6546 if (last_insert == NULL)
6547 return NULL;
6548 s = vim_strsave(last_insert + last_insert_skip);
6549 if (s != NULL)
6550 {
6551 len = (int)STRLEN(s);
6552 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6553 s[len - 1] = NUL;
6554 }
6555 return s;
6556}
6557
6558/*
6559 * Check the word in front of the cursor for an abbreviation.
6560 * Called when the non-id character "c" has been entered.
6561 * When an abbreviation is recognized it is removed from the text and
6562 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6563 */
6564 static int
6565echeck_abbr(c)
6566 int c;
6567{
6568 /* Don't check for abbreviation in paste mode, when disabled and just
6569 * after moving around with cursor keys. */
6570 if (p_paste || no_abbr || arrow_used)
6571 return FALSE;
6572
6573 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6574 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6575}
6576
6577/*
6578 * replace-stack functions
6579 *
6580 * When replacing characters, the replaced characters are remembered for each
6581 * new character. This is used to re-insert the old text when backspacing.
6582 *
6583 * There is a NUL headed list of characters for each character that is
6584 * currently in the file after the insertion point. When BS is used, one NUL
6585 * headed list is put back for the deleted character.
6586 *
6587 * For a newline, there are two NUL headed lists. One contains the characters
6588 * that the NL replaced. The extra one stores the characters after the cursor
6589 * that were deleted (always white space).
6590 *
6591 * Replace_offset is normally 0, in which case replace_push will add a new
6592 * character at the end of the stack. If replace_offset is not 0, that many
6593 * characters will be left on the stack above the newly inserted character.
6594 */
6595
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006596static char_u *replace_stack = NULL;
6597static long replace_stack_nr = 0; /* next entry in replace stack */
6598static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600 void
6601replace_push(c)
6602 int c; /* character that is replaced (NUL is none) */
6603{
6604 char_u *p;
6605
6606 if (replace_stack_nr < replace_offset) /* nothing to do */
6607 return;
6608 if (replace_stack_len <= replace_stack_nr)
6609 {
6610 replace_stack_len += 50;
6611 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6612 if (p == NULL) /* out of memory */
6613 {
6614 replace_stack_len -= 50;
6615 return;
6616 }
6617 if (replace_stack != NULL)
6618 {
6619 mch_memmove(p, replace_stack,
6620 (size_t)(replace_stack_nr * sizeof(char_u)));
6621 vim_free(replace_stack);
6622 }
6623 replace_stack = p;
6624 }
6625 p = replace_stack + replace_stack_nr - replace_offset;
6626 if (replace_offset)
6627 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6628 *p = c;
6629 ++replace_stack_nr;
6630}
6631
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006632#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633/*
6634 * call replace_push(c) with replace_offset set to the first NUL.
6635 */
6636 static void
6637replace_push_off(c)
6638 int c;
6639{
6640 char_u *p;
6641
6642 p = replace_stack + replace_stack_nr;
6643 for (replace_offset = 1; replace_offset < replace_stack_nr;
6644 ++replace_offset)
6645 if (*--p == NUL)
6646 break;
6647 replace_push(c);
6648 replace_offset = 0;
6649}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652/*
6653 * Pop one item from the replace stack.
6654 * return -1 if stack empty
6655 * return replaced character or NUL otherwise
6656 */
6657 static int
6658replace_pop()
6659{
6660 if (replace_stack_nr == 0)
6661 return -1;
6662 return (int)replace_stack[--replace_stack_nr];
6663}
6664
6665/*
6666 * Join the top two items on the replace stack. This removes to "off"'th NUL
6667 * encountered.
6668 */
6669 static void
6670replace_join(off)
6671 int off; /* offset for which NUL to remove */
6672{
6673 int i;
6674
6675 for (i = replace_stack_nr; --i >= 0; )
6676 if (replace_stack[i] == NUL && off-- <= 0)
6677 {
6678 --replace_stack_nr;
6679 mch_memmove(replace_stack + i, replace_stack + i + 1,
6680 (size_t)(replace_stack_nr - i));
6681 return;
6682 }
6683}
6684
6685/*
6686 * Pop bytes from the replace stack until a NUL is found, and insert them
6687 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6688 */
6689 static void
6690replace_pop_ins()
6691{
6692 int cc;
6693 int oldState = State;
6694
6695 State = NORMAL; /* don't want REPLACE here */
6696 while ((cc = replace_pop()) > 0)
6697 {
6698#ifdef FEAT_MBYTE
6699 mb_replace_pop_ins(cc);
6700#else
6701 ins_char(cc);
6702#endif
6703 dec_cursor();
6704 }
6705 State = oldState;
6706}
6707
6708#ifdef FEAT_MBYTE
6709/*
6710 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6711 * indicates a multi-byte char, pop the other bytes too.
6712 */
6713 static void
6714mb_replace_pop_ins(cc)
6715 int cc;
6716{
6717 int n;
6718 char_u buf[MB_MAXBYTES];
6719 int i;
6720 int c;
6721
6722 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6723 {
6724 buf[0] = cc;
6725 for (i = 1; i < n; ++i)
6726 buf[i] = replace_pop();
6727 ins_bytes_len(buf, n);
6728 }
6729 else
6730 ins_char(cc);
6731
6732 if (enc_utf8)
6733 /* Handle composing chars. */
6734 for (;;)
6735 {
6736 c = replace_pop();
6737 if (c == -1) /* stack empty */
6738 break;
6739 if ((n = MB_BYTE2LEN(c)) == 1)
6740 {
6741 /* Not a multi-byte char, put it back. */
6742 replace_push(c);
6743 break;
6744 }
6745 else
6746 {
6747 buf[0] = c;
6748 for (i = 1; i < n; ++i)
6749 buf[i] = replace_pop();
6750 if (utf_iscomposing(utf_ptr2char(buf)))
6751 ins_bytes_len(buf, n);
6752 else
6753 {
6754 /* Not a composing char, put it back. */
6755 for (i = n - 1; i >= 0; --i)
6756 replace_push(buf[i]);
6757 break;
6758 }
6759 }
6760 }
6761}
6762#endif
6763
6764/*
6765 * make the replace stack empty
6766 * (called when exiting replace mode)
6767 */
6768 static void
6769replace_flush()
6770{
6771 vim_free(replace_stack);
6772 replace_stack = NULL;
6773 replace_stack_len = 0;
6774 replace_stack_nr = 0;
6775}
6776
6777/*
6778 * Handle doing a BS for one character.
6779 * cc < 0: replace stack empty, just move cursor
6780 * cc == 0: character was inserted, delete it
6781 * cc > 0: character was replaced, put cc (first byte of original char) back
6782 * and check for more characters to be put back
6783 */
6784 static void
6785replace_do_bs()
6786{
6787 int cc;
6788#ifdef FEAT_VREPLACE
6789 int orig_len = 0;
6790 int ins_len;
6791 int orig_vcols = 0;
6792 colnr_T start_vcol;
6793 char_u *p;
6794 int i;
6795 int vcol;
6796#endif
6797
6798 cc = replace_pop();
6799 if (cc > 0)
6800 {
6801#ifdef FEAT_VREPLACE
6802 if (State & VREPLACE_FLAG)
6803 {
6804 /* Get the number of screen cells used by the character we are
6805 * going to delete. */
6806 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6807 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6808 }
6809#endif
6810#ifdef FEAT_MBYTE
6811 if (has_mbyte)
6812 {
6813 del_char(FALSE);
6814# ifdef FEAT_VREPLACE
6815 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006816 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817# endif
6818 replace_push(cc);
6819 }
6820 else
6821#endif
6822 {
6823 pchar_cursor(cc);
6824#ifdef FEAT_VREPLACE
6825 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006826 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827#endif
6828 }
6829 replace_pop_ins();
6830
6831#ifdef FEAT_VREPLACE
6832 if (State & VREPLACE_FLAG)
6833 {
6834 /* Get the number of screen cells used by the inserted characters */
6835 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006836 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 vcol = start_vcol;
6838 for (i = 0; i < ins_len; ++i)
6839 {
6840 vcol += chartabsize(p + i, vcol);
6841#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006842 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843#endif
6844 }
6845 vcol -= start_vcol;
6846
6847 /* Delete spaces that were inserted after the cursor to keep the
6848 * text aligned. */
6849 curwin->w_cursor.col += ins_len;
6850 while (vcol > orig_vcols && gchar_cursor() == ' ')
6851 {
6852 del_char(FALSE);
6853 ++orig_vcols;
6854 }
6855 curwin->w_cursor.col -= ins_len;
6856 }
6857#endif
6858
6859 /* mark the buffer as changed and prepare for displaying */
6860 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6861 }
6862 else if (cc == 0)
6863 (void)del_char(FALSE);
6864}
6865
6866#ifdef FEAT_CINDENT
6867/*
6868 * Return TRUE if C-indenting is on.
6869 */
6870 static int
6871cindent_on()
6872{
6873 return (!p_paste && (curbuf->b_p_cin
6874# ifdef FEAT_EVAL
6875 || *curbuf->b_p_inde != NUL
6876# endif
6877 ));
6878}
6879#endif
6880
6881#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6882/*
6883 * Re-indent the current line, based on the current contents of it and the
6884 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6885 * confused what all the part that handles Control-T is doing that I'm not.
6886 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6887 */
6888
6889 void
6890fixthisline(get_the_indent)
6891 int (*get_the_indent) __ARGS((void));
6892{
6893 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6894 if (linewhite(curwin->w_cursor.lnum))
6895 did_ai = TRUE; /* delete the indent if the line stays empty */
6896}
6897
6898 void
6899fix_indent()
6900{
6901 if (p_paste)
6902 return;
6903# ifdef FEAT_LISP
6904 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6905 fixthisline(get_lisp_indent);
6906# endif
6907# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6908 else
6909# endif
6910# ifdef FEAT_CINDENT
6911 if (cindent_on())
6912 do_c_expr_indent();
6913# endif
6914}
6915
6916#endif
6917
6918#ifdef FEAT_CINDENT
6919/*
6920 * return TRUE if 'cinkeys' contains the key "keytyped",
6921 * when == '*': Only if key is preceded with '*' (indent before insert)
6922 * when == '!': Only if key is prededed with '!' (don't insert)
6923 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6924 *
6925 * "keytyped" can have a few special values:
6926 * KEY_OPEN_FORW
6927 * KEY_OPEN_BACK
6928 * KEY_COMPLETE just finished completion.
6929 *
6930 * If line_is_empty is TRUE accept keys with '0' before them.
6931 */
6932 int
6933in_cinkeys(keytyped, when, line_is_empty)
6934 int keytyped;
6935 int when;
6936 int line_is_empty;
6937{
6938 char_u *look;
6939 int try_match;
6940 int try_match_word;
6941 char_u *p;
6942 char_u *line;
6943 int icase;
6944 int i;
6945
6946#ifdef FEAT_EVAL
6947 if (*curbuf->b_p_inde != NUL)
6948 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6949 else
6950#endif
6951 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6952 while (*look)
6953 {
6954 /*
6955 * Find out if we want to try a match with this key, depending on
6956 * 'when' and a '*' or '!' before the key.
6957 */
6958 switch (when)
6959 {
6960 case '*': try_match = (*look == '*'); break;
6961 case '!': try_match = (*look == '!'); break;
6962 default: try_match = (*look != '*'); break;
6963 }
6964 if (*look == '*' || *look == '!')
6965 ++look;
6966
6967 /*
6968 * If there is a '0', only accept a match if the line is empty.
6969 * But may still match when typing last char of a word.
6970 */
6971 if (*look == '0')
6972 {
6973 try_match_word = try_match;
6974 if (!line_is_empty)
6975 try_match = FALSE;
6976 ++look;
6977 }
6978 else
6979 try_match_word = FALSE;
6980
6981 /*
6982 * does it look like a control character?
6983 */
6984 if (*look == '^'
6985#ifdef EBCDIC
6986 && (Ctrl_chr(look[1]) != 0)
6987#else
6988 && look[1] >= '?' && look[1] <= '_'
6989#endif
6990 )
6991 {
6992 if (try_match && keytyped == Ctrl_chr(look[1]))
6993 return TRUE;
6994 look += 2;
6995 }
6996 /*
6997 * 'o' means "o" command, open forward.
6998 * 'O' means "O" command, open backward.
6999 */
7000 else if (*look == 'o')
7001 {
7002 if (try_match && keytyped == KEY_OPEN_FORW)
7003 return TRUE;
7004 ++look;
7005 }
7006 else if (*look == 'O')
7007 {
7008 if (try_match && keytyped == KEY_OPEN_BACK)
7009 return TRUE;
7010 ++look;
7011 }
7012
7013 /*
7014 * 'e' means to check for "else" at start of line and just before the
7015 * cursor.
7016 */
7017 else if (*look == 'e')
7018 {
7019 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7020 {
7021 p = ml_get_curline();
7022 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7023 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7024 return TRUE;
7025 }
7026 ++look;
7027 }
7028
7029 /*
7030 * ':' only causes an indent if it is at the end of a label or case
7031 * statement, or when it was before typing the ':' (to fix
7032 * class::method for C++).
7033 */
7034 else if (*look == ':')
7035 {
7036 if (try_match && keytyped == ':')
7037 {
7038 p = ml_get_curline();
7039 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7040 return TRUE;
7041 if (curwin->w_cursor.col > 2
7042 && p[curwin->w_cursor.col - 1] == ':'
7043 && p[curwin->w_cursor.col - 2] == ':')
7044 {
7045 p[curwin->w_cursor.col - 1] = ' ';
7046 i = (cin_iscase(p) || cin_isscopedecl(p)
7047 || cin_islabel(30));
7048 p = ml_get_curline();
7049 p[curwin->w_cursor.col - 1] = ':';
7050 if (i)
7051 return TRUE;
7052 }
7053 }
7054 ++look;
7055 }
7056
7057
7058 /*
7059 * Is it a key in <>, maybe?
7060 */
7061 else if (*look == '<')
7062 {
7063 if (try_match)
7064 {
7065 /*
7066 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7067 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7068 * >, *, : and ! keys if they really really want to.
7069 */
7070 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7071 && keytyped == look[1])
7072 return TRUE;
7073
7074 if (keytyped == get_special_key_code(look + 1))
7075 return TRUE;
7076 }
7077 while (*look && *look != '>')
7078 look++;
7079 while (*look == '>')
7080 look++;
7081 }
7082
7083 /*
7084 * Is it a word: "=word"?
7085 */
7086 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7087 {
7088 ++look;
7089 if (*look == '~')
7090 {
7091 icase = TRUE;
7092 ++look;
7093 }
7094 else
7095 icase = FALSE;
7096 p = vim_strchr(look, ',');
7097 if (p == NULL)
7098 p = look + STRLEN(look);
7099 if ((try_match || try_match_word)
7100 && curwin->w_cursor.col >= (colnr_T)(p - look))
7101 {
7102 int match = FALSE;
7103
7104#ifdef FEAT_INS_EXPAND
7105 if (keytyped == KEY_COMPLETE)
7106 {
7107 char_u *s;
7108
7109 /* Just completed a word, check if it starts with "look".
7110 * search back for the start of a word. */
7111 line = ml_get_curline();
7112# ifdef FEAT_MBYTE
7113 if (has_mbyte)
7114 {
7115 char_u *n;
7116
7117 for (s = line + curwin->w_cursor.col; s > line; s = n)
7118 {
7119 n = mb_prevptr(line, s);
7120 if (!vim_iswordp(n))
7121 break;
7122 }
7123 }
7124 else
7125# endif
7126 for (s = line + curwin->w_cursor.col; s > line; --s)
7127 if (!vim_iswordc(s[-1]))
7128 break;
7129 if (s + (p - look) <= line + curwin->w_cursor.col
7130 && (icase
7131 ? MB_STRNICMP(s, look, p - look)
7132 : STRNCMP(s, look, p - look)) == 0)
7133 match = TRUE;
7134 }
7135 else
7136#endif
7137 /* TODO: multi-byte */
7138 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7139 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7140 {
7141 line = ml_get_cursor();
7142 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7143 || !vim_iswordc(line[-(p - look) - 1]))
7144 && (icase
7145 ? MB_STRNICMP(line - (p - look), look, p - look)
7146 : STRNCMP(line - (p - look), look, p - look))
7147 == 0)
7148 match = TRUE;
7149 }
7150 if (match && try_match_word && !try_match)
7151 {
7152 /* "0=word": Check if there are only blanks before the
7153 * word. */
7154 line = ml_get_curline();
7155 if ((int)(skipwhite(line) - line) !=
7156 (int)(curwin->w_cursor.col - (p - look)))
7157 match = FALSE;
7158 }
7159 if (match)
7160 return TRUE;
7161 }
7162 look = p;
7163 }
7164
7165 /*
7166 * ok, it's a boring generic character.
7167 */
7168 else
7169 {
7170 if (try_match && *look == keytyped)
7171 return TRUE;
7172 ++look;
7173 }
7174
7175 /*
7176 * Skip over ", ".
7177 */
7178 look = skip_to_option_part(look);
7179 }
7180 return FALSE;
7181}
7182#endif /* FEAT_CINDENT */
7183
7184#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7185/*
7186 * Map Hebrew keyboard when in hkmap mode.
7187 */
7188 int
7189hkmap(c)
7190 int c;
7191{
7192 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7193 {
7194 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7195 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7196 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7197 static char_u map[26] =
7198 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7199 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7200 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7201 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7202 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7203 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7204 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7205 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7206 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7207
7208 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7209 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7210 /* '-1'='sofit' */
7211 else if (c == 'x')
7212 return 'X';
7213 else if (c == 'q')
7214 return '\''; /* {geresh}={'} */
7215 else if (c == 246)
7216 return ' '; /* \"o --> ' ' for a german keyboard */
7217 else if (c == 228)
7218 return ' '; /* \"a --> ' ' -- / -- */
7219 else if (c == 252)
7220 return ' '; /* \"u --> ' ' -- / -- */
7221#ifdef EBCDIC
7222 else if (islower(c))
7223#else
7224 /* NOTE: islower() does not do the right thing for us on Linux so we
7225 * do this the same was as 5.7 and previous, so it works correctly on
7226 * all systems. Specifically, the e.g. Delete and Arrow keys are
7227 * munged and won't work if e.g. searching for Hebrew text.
7228 */
7229 else if (c >= 'a' && c <= 'z')
7230#endif
7231 return (int)(map[CharOrdLow(c)] + p_aleph);
7232 else
7233 return c;
7234 }
7235 else
7236 {
7237 switch (c)
7238 {
7239 case '`': return ';';
7240 case '/': return '.';
7241 case '\'': return ',';
7242 case 'q': return '/';
7243 case 'w': return '\'';
7244
7245 /* Hebrew letters - set offset from 'a' */
7246 case ',': c = '{'; break;
7247 case '.': c = 'v'; break;
7248 case ';': c = 't'; break;
7249 default: {
7250 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7251
7252#ifdef EBCDIC
7253 /* see note about islower() above */
7254 if (!islower(c))
7255#else
7256 if (c < 'a' || c > 'z')
7257#endif
7258 return c;
7259 c = str[CharOrdLow(c)];
7260 break;
7261 }
7262 }
7263
7264 return (int)(CharOrdLow(c) + p_aleph);
7265 }
7266}
7267#endif
7268
7269 static void
7270ins_reg()
7271{
7272 int need_redraw = FALSE;
7273 int regname;
7274 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007275#ifdef FEAT_VISUAL
7276 int vis_active = VIsual_active;
7277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
7279 /*
7280 * If we are going to wait for a character, show a '"'.
7281 */
7282 pc_status = PC_STATUS_UNSET;
7283 if (redrawing() && !char_avail())
7284 {
7285 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007286 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
7288 edit_putchar('"', TRUE);
7289#ifdef FEAT_CMDL_INFO
7290 add_to_showcmd_c(Ctrl_R);
7291#endif
7292 }
7293
7294#ifdef USE_ON_FLY_SCROLL
7295 dont_scroll = TRUE; /* disallow scrolling here */
7296#endif
7297
7298 /*
7299 * Don't map the register name. This also prevents the mode message to be
7300 * deleted when ESC is hit.
7301 */
7302 ++no_mapping;
7303 regname = safe_vgetc();
7304#ifdef FEAT_LANGMAP
7305 LANGMAP_ADJUST(regname, TRUE);
7306#endif
7307 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7308 {
7309 /* Get a third key for literal register insertion */
7310 literally = regname;
7311#ifdef FEAT_CMDL_INFO
7312 add_to_showcmd_c(literally);
7313#endif
7314 regname = safe_vgetc();
7315#ifdef FEAT_LANGMAP
7316 LANGMAP_ADJUST(regname, TRUE);
7317#endif
7318 }
7319 --no_mapping;
7320
7321#ifdef FEAT_EVAL
7322 /*
7323 * Don't call u_sync() while getting the expression,
7324 * evaluating it or giving an error message for it!
7325 */
7326 ++no_u_sync;
7327 if (regname == '=')
7328 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007329# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007333# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 /* Restore the Input Method. */
7335 if (im_on)
7336 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007339 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7340 {
7341 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 else
7345 {
7346#endif
7347 if (literally == Ctrl_O || literally == Ctrl_P)
7348 {
7349 /* Append the command to the redo buffer. */
7350 AppendCharToRedobuff(Ctrl_R);
7351 AppendCharToRedobuff(literally);
7352 AppendCharToRedobuff(regname);
7353
7354 do_put(regname, BACKWARD, 1L,
7355 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7356 }
7357 else if (insert_reg(regname, literally) == FAIL)
7358 {
7359 vim_beep();
7360 need_redraw = TRUE; /* remove the '"' */
7361 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007362 else if (stop_insert_mode)
7363 /* When the '=' register was used and a function was invoked that
7364 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7365 * insert anything, need to remove the '"' */
7366 need_redraw = TRUE;
7367
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368#ifdef FEAT_EVAL
7369 }
7370 --no_u_sync;
7371#endif
7372#ifdef FEAT_CMDL_INFO
7373 clear_showcmd();
7374#endif
7375
7376 /* If the inserted register is empty, we need to remove the '"' */
7377 if (need_redraw || stuff_empty())
7378 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007379
7380#ifdef FEAT_VISUAL
7381 /* Disallow starting Visual mode here, would get a weird mode. */
7382 if (!vis_active && VIsual_active)
7383 end_visual_mode();
7384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385}
7386
7387/*
7388 * CTRL-G commands in Insert mode.
7389 */
7390 static void
7391ins_ctrl_g()
7392{
7393 int c;
7394
7395#ifdef FEAT_INS_EXPAND
7396 /* Right after CTRL-X the cursor will be after the ruler. */
7397 setcursor();
7398#endif
7399
7400 /*
7401 * Don't map the second key. This also prevents the mode message to be
7402 * deleted when ESC is hit.
7403 */
7404 ++no_mapping;
7405 c = safe_vgetc();
7406 --no_mapping;
7407 switch (c)
7408 {
7409 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7410 case K_UP:
7411 case Ctrl_K:
7412 case 'k': ins_up(TRUE);
7413 break;
7414
7415 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7416 case K_DOWN:
7417 case Ctrl_J:
7418 case 'j': ins_down(TRUE);
7419 break;
7420
7421 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007422 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007424
7425 /* Need to reset Insstart, esp. because a BS that joins
7426 * aline to the previous one must save for undo. */
7427 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 break;
7429
7430 /* Unknown CTRL-G command, reserved for future expansion. */
7431 default: vim_beep();
7432 }
7433}
7434
7435/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007436 * CTRL-^ in Insert mode.
7437 */
7438 static void
7439ins_ctrl_hat()
7440{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007441 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007442 {
7443 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7444 if (State & LANGMAP)
7445 {
7446 curbuf->b_p_iminsert = B_IMODE_NONE;
7447 State &= ~LANGMAP;
7448 }
7449 else
7450 {
7451 curbuf->b_p_iminsert = B_IMODE_LMAP;
7452 State |= LANGMAP;
7453#ifdef USE_IM_CONTROL
7454 im_set_active(FALSE);
7455#endif
7456 }
7457 }
7458#ifdef USE_IM_CONTROL
7459 else
7460 {
7461 /* There are no ":lmap" mappings, toggle IM */
7462 if (im_get_status())
7463 {
7464 curbuf->b_p_iminsert = B_IMODE_NONE;
7465 im_set_active(FALSE);
7466 }
7467 else
7468 {
7469 curbuf->b_p_iminsert = B_IMODE_IM;
7470 State &= ~LANGMAP;
7471 im_set_active(TRUE);
7472 }
7473 }
7474#endif
7475 set_iminsert_global();
7476 showmode();
7477#ifdef FEAT_GUI
7478 /* may show different cursor shape or color */
7479 if (gui.in_use)
7480 gui_update_cursor(TRUE, FALSE);
7481#endif
7482#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7483 /* Show/unshow value of 'keymap' in status lines. */
7484 status_redraw_curbuf();
7485#endif
7486}
7487
7488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 * Handle ESC in insert mode.
7490 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7491 * insert.
7492 */
7493 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007494ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 long *count;
7496 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007497 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499 int temp;
7500 static int disabled_redraw = FALSE;
7501
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007502#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007503 check_spell_redraw();
7504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505#if defined(FEAT_HANGULIN)
7506# if defined(ESC_CHG_TO_ENG_MODE)
7507 hangul_input_state_set(0);
7508# endif
7509 if (composing_hangul)
7510 {
7511 push_raw_key(composing_hangul_buffer, 2);
7512 composing_hangul = 0;
7513 }
7514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 temp = curwin->w_cursor.col;
7517 if (disabled_redraw)
7518 {
7519 --RedrawingDisabled;
7520 disabled_redraw = FALSE;
7521 }
7522 if (!arrow_used)
7523 {
7524 /*
7525 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007526 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7527 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
7529 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007530 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531
7532 /*
7533 * Repeating insert may take a long time. Check for
7534 * interrupt now and then.
7535 */
7536 if (*count > 0)
7537 {
7538 line_breakcheck();
7539 if (got_int)
7540 *count = 0;
7541 }
7542
7543 if (--*count > 0) /* repeat what was typed */
7544 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007545 /* Vi repeats the insert without replacing characters. */
7546 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7547 State &= ~REPLACE_FLAG;
7548
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 (void)start_redo_ins();
7550 if (cmdchar == 'r' || cmdchar == 'v')
7551 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7552 ++RedrawingDisabled;
7553 disabled_redraw = TRUE;
7554 return FALSE; /* repeat the insert */
7555 }
7556 stop_insert(&curwin->w_cursor, TRUE);
7557 undisplay_dollar();
7558 }
7559
7560 /* When an autoindent was removed, curswant stays after the
7561 * indent */
7562 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7563 curwin->w_set_curswant = TRUE;
7564
7565 /* Remember the last Insert position in the '^ mark. */
7566 if (!cmdmod.keepjumps)
7567 curbuf->b_last_insert = curwin->w_cursor;
7568
7569 /*
7570 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007571 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007573 if (!nomove
7574 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575#ifdef FEAT_VIRTUALEDIT
7576 || curwin->w_cursor.coladd > 0
7577#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007578 )
7579 && (restart_edit == NUL
7580 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007582 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007584 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585#ifdef FEAT_RIGHTLEFT
7586 && !revins_on
7587#endif
7588 )
7589 {
7590#ifdef FEAT_VIRTUALEDIT
7591 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7592 {
7593 oneleft();
7594 if (restart_edit != NUL)
7595 ++curwin->w_cursor.coladd;
7596 }
7597 else
7598#endif
7599 {
7600 --curwin->w_cursor.col;
7601#ifdef FEAT_MBYTE
7602 /* Correct cursor for multi-byte character. */
7603 if (has_mbyte)
7604 mb_adjust_cursor();
7605#endif
7606 }
7607 }
7608
7609#ifdef USE_IM_CONTROL
7610 /* Disable IM to allow typing English directly for Normal mode commands.
7611 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7612 * well). */
7613 if (!(State & LANGMAP))
7614 im_save_status(&curbuf->b_p_iminsert);
7615 im_set_active(FALSE);
7616#endif
7617
7618 State = NORMAL;
7619 /* need to position cursor again (e.g. when on a TAB ) */
7620 changed_cline_bef_curs();
7621
7622#ifdef FEAT_MOUSE
7623 setmouse();
7624#endif
7625#ifdef CURSOR_SHAPE
7626 ui_cursor_shape(); /* may show different cursor shape */
7627#endif
7628
7629 /*
7630 * When recording or for CTRL-O, need to display the new mode.
7631 * Otherwise remove the mode message.
7632 */
7633 if (Recording || restart_edit != NUL)
7634 showmode();
7635 else if (p_smd)
7636 MSG("");
7637
7638 return TRUE; /* exit Insert mode */
7639}
7640
7641#ifdef FEAT_RIGHTLEFT
7642/*
7643 * Toggle language: hkmap and revins_on.
7644 * Move to end of reverse inserted text.
7645 */
7646 static void
7647ins_ctrl_()
7648{
7649 if (revins_on && revins_chars && revins_scol >= 0)
7650 {
7651 while (gchar_cursor() != NUL && revins_chars--)
7652 ++curwin->w_cursor.col;
7653 }
7654 p_ri = !p_ri;
7655 revins_on = (State == INSERT && p_ri);
7656 if (revins_on)
7657 {
7658 revins_scol = curwin->w_cursor.col;
7659 revins_legal++;
7660 revins_chars = 0;
7661 undisplay_dollar();
7662 }
7663 else
7664 revins_scol = -1;
7665#ifdef FEAT_FKMAP
7666 if (p_altkeymap)
7667 {
7668 /*
7669 * to be consistent also for redo command, using '.'
7670 * set arrow_used to true and stop it - causing to redo
7671 * characters entered in one mode (normal/reverse insert).
7672 */
7673 arrow_used = TRUE;
7674 (void)stop_arrow();
7675 p_fkmap = curwin->w_p_rl ^ p_ri;
7676 if (p_fkmap && p_ri)
7677 State = INSERT;
7678 }
7679 else
7680#endif
7681 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7682 showmode();
7683}
7684#endif
7685
7686#ifdef FEAT_VISUAL
7687/*
7688 * If 'keymodel' contains "startsel", may start selection.
7689 * Returns TRUE when a CTRL-O and other keys stuffed.
7690 */
7691 static int
7692ins_start_select(c)
7693 int c;
7694{
7695 if (km_startsel)
7696 switch (c)
7697 {
7698 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 case K_PAGEUP:
7701 case K_KPAGEUP:
7702 case K_PAGEDOWN:
7703 case K_KPAGEDOWN:
7704# ifdef MACOS
7705 case K_LEFT:
7706 case K_RIGHT:
7707 case K_UP:
7708 case K_DOWN:
7709 case K_END:
7710 case K_HOME:
7711# endif
7712 if (!(mod_mask & MOD_MASK_SHIFT))
7713 break;
7714 /* FALLTHROUGH */
7715 case K_S_LEFT:
7716 case K_S_RIGHT:
7717 case K_S_UP:
7718 case K_S_DOWN:
7719 case K_S_END:
7720 case K_S_HOME:
7721 /* Start selection right away, the cursor can move with
7722 * CTRL-O when beyond the end of the line. */
7723 start_selection();
7724
7725 /* Execute the key in (insert) Select mode. */
7726 stuffcharReadbuff(Ctrl_O);
7727 if (mod_mask)
7728 {
7729 char_u buf[4];
7730
7731 buf[0] = K_SPECIAL;
7732 buf[1] = KS_MODIFIER;
7733 buf[2] = mod_mask;
7734 buf[3] = NUL;
7735 stuffReadbuff(buf);
7736 }
7737 stuffcharReadbuff(c);
7738 return TRUE;
7739 }
7740 return FALSE;
7741}
7742#endif
7743
7744/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007745 * <Insert> key in Insert mode: toggle insert/remplace mode.
7746 */
7747 static void
7748ins_insert(replaceState)
7749 int replaceState;
7750{
7751#ifdef FEAT_FKMAP
7752 if (p_fkmap && p_ri)
7753 {
7754 beep_flush();
7755 EMSG(farsi_text_3); /* encoded in Farsi */
7756 return;
7757 }
7758#endif
7759
7760#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007761# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007762 set_vim_var_string(VV_INSERTMODE,
7763 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007764# ifdef FEAT_VREPLACE
7765 replaceState == VREPLACE ? "v" :
7766# endif
7767 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007768# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007769 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7770#endif
7771 if (State & REPLACE_FLAG)
7772 State = INSERT | (State & LANGMAP);
7773 else
7774 State = replaceState | (State & LANGMAP);
7775 AppendCharToRedobuff(K_INS);
7776 showmode();
7777#ifdef CURSOR_SHAPE
7778 ui_cursor_shape(); /* may show different cursor shape */
7779#endif
7780}
7781
7782/*
7783 * Pressed CTRL-O in Insert mode.
7784 */
7785 static void
7786ins_ctrl_o()
7787{
7788#ifdef FEAT_VREPLACE
7789 if (State & VREPLACE_FLAG)
7790 restart_edit = 'V';
7791 else
7792#endif
7793 if (State & REPLACE_FLAG)
7794 restart_edit = 'R';
7795 else
7796 restart_edit = 'I';
7797#ifdef FEAT_VIRTUALEDIT
7798 if (virtual_active())
7799 ins_at_eol = FALSE; /* cursor always keeps its column */
7800 else
7801#endif
7802 ins_at_eol = (gchar_cursor() == NUL);
7803}
7804
7805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 * If the cursor is on an indent, ^T/^D insert/delete one
7807 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7808 * Always round the indent to 'shiftwith', this is compatible
7809 * with vi. But vi only supports ^T and ^D after an
7810 * autoindent, we support it everywhere.
7811 */
7812 static void
7813ins_shift(c, lastc)
7814 int c;
7815 int lastc;
7816{
7817 if (stop_arrow() == FAIL)
7818 return;
7819 AppendCharToRedobuff(c);
7820
7821 /*
7822 * 0^D and ^^D: remove all indent.
7823 */
7824 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7825 {
7826 --curwin->w_cursor.col;
7827 (void)del_char(FALSE); /* delete the '^' or '0' */
7828 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7829 if (State & REPLACE_FLAG)
7830 replace_pop_ins();
7831 if (lastc == '^')
7832 old_indent = get_indent(); /* remember curr. indent */
7833 change_indent(INDENT_SET, 0, TRUE, 0);
7834 }
7835 else
7836 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7837
7838 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7839 did_ai = FALSE;
7840#ifdef FEAT_SMARTINDENT
7841 did_si = FALSE;
7842 can_si = FALSE;
7843 can_si_back = FALSE;
7844#endif
7845#ifdef FEAT_CINDENT
7846 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7847#endif
7848}
7849
7850 static void
7851ins_del()
7852{
7853 int temp;
7854
7855 if (stop_arrow() == FAIL)
7856 return;
7857 if (gchar_cursor() == NUL) /* delete newline */
7858 {
7859 temp = curwin->w_cursor.col;
7860 if (!can_bs(BS_EOL) /* only if "eol" included */
7861 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7862 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7863 || do_join(FALSE) == FAIL)
7864 vim_beep();
7865 else
7866 curwin->w_cursor.col = temp;
7867 }
7868 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7869 vim_beep();
7870 did_ai = FALSE;
7871#ifdef FEAT_SMARTINDENT
7872 did_si = FALSE;
7873 can_si = FALSE;
7874 can_si_back = FALSE;
7875#endif
7876 AppendCharToRedobuff(K_DEL);
7877}
7878
7879/*
7880 * Handle Backspace, delete-word and delete-line in Insert mode.
7881 * Return TRUE when backspace was actually used.
7882 */
7883 static int
7884ins_bs(c, mode, inserted_space_p)
7885 int c;
7886 int mode;
7887 int *inserted_space_p;
7888{
7889 linenr_T lnum;
7890 int cc;
7891 int temp = 0; /* init for GCC */
7892 colnr_T mincol;
7893 int did_backspace = FALSE;
7894 int in_indent;
7895 int oldState;
7896#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007897 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#endif
7899
7900 /*
7901 * can't delete anything in an empty file
7902 * can't backup past first character in buffer
7903 * can't backup past starting point unless 'backspace' > 1
7904 * can backup to a previous line if 'backspace' == 0
7905 */
7906 if ( bufempty()
7907 || (
7908#ifdef FEAT_RIGHTLEFT
7909 !revins_on &&
7910#endif
7911 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7912 || (!can_bs(BS_START)
7913 && (arrow_used
7914 || (curwin->w_cursor.lnum == Insstart.lnum
7915 && curwin->w_cursor.col <= Insstart.col)))
7916 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7917 && curwin->w_cursor.col <= ai_col)
7918 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7919 {
7920 vim_beep();
7921 return FALSE;
7922 }
7923
7924 if (stop_arrow() == FAIL)
7925 return FALSE;
7926 in_indent = inindent(0);
7927#ifdef FEAT_CINDENT
7928 if (in_indent)
7929 can_cindent = FALSE;
7930#endif
7931#ifdef FEAT_COMMENTS
7932 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7933#endif
7934#ifdef FEAT_RIGHTLEFT
7935 if (revins_on) /* put cursor after last inserted char */
7936 inc_cursor();
7937#endif
7938
7939#ifdef FEAT_VIRTUALEDIT
7940 /* Virtualedit:
7941 * BACKSPACE_CHAR eats a virtual space
7942 * BACKSPACE_WORD eats all coladd
7943 * BACKSPACE_LINE eats all coladd and keeps going
7944 */
7945 if (curwin->w_cursor.coladd > 0)
7946 {
7947 if (mode == BACKSPACE_CHAR)
7948 {
7949 --curwin->w_cursor.coladd;
7950 return TRUE;
7951 }
7952 if (mode == BACKSPACE_WORD)
7953 {
7954 curwin->w_cursor.coladd = 0;
7955 return TRUE;
7956 }
7957 curwin->w_cursor.coladd = 0;
7958 }
7959#endif
7960
7961 /*
7962 * delete newline!
7963 */
7964 if (curwin->w_cursor.col == 0)
7965 {
7966 lnum = Insstart.lnum;
7967 if (curwin->w_cursor.lnum == Insstart.lnum
7968#ifdef FEAT_RIGHTLEFT
7969 || revins_on
7970#endif
7971 )
7972 {
7973 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7974 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7975 return FALSE;
7976 --Insstart.lnum;
7977 Insstart.col = MAXCOL;
7978 }
7979 /*
7980 * In replace mode:
7981 * cc < 0: NL was inserted, delete it
7982 * cc >= 0: NL was replaced, put original characters back
7983 */
7984 cc = -1;
7985 if (State & REPLACE_FLAG)
7986 cc = replace_pop(); /* returns -1 if NL was inserted */
7987 /*
7988 * In replace mode, in the line we started replacing, we only move the
7989 * cursor.
7990 */
7991 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7992 {
7993 dec_cursor();
7994 }
7995 else
7996 {
7997#ifdef FEAT_VREPLACE
7998 if (!(State & VREPLACE_FLAG)
7999 || curwin->w_cursor.lnum > orig_line_count)
8000#endif
8001 {
8002 temp = gchar_cursor(); /* remember current char */
8003 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008004
8005 /* When "aw" is in 'formatoptions' we must delete the space at
8006 * the end of the line, otherwise the line will be broken
8007 * again when auto-formatting. */
8008 if (has_format_option(FO_AUTO)
8009 && has_format_option(FO_WHITE_PAR))
8010 {
8011 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8012 TRUE);
8013 int len;
8014
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008015 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008016 if (len > 0 && ptr[len - 1] == ' ')
8017 ptr[len - 1] = NUL;
8018 }
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 (void)do_join(FALSE);
8021 if (temp == NUL && gchar_cursor() != NUL)
8022 inc_cursor();
8023 }
8024#ifdef FEAT_VREPLACE
8025 else
8026 dec_cursor();
8027#endif
8028
8029 /*
8030 * In REPLACE mode we have to put back the text that was replaced
8031 * by the NL. On the replace stack is first a NUL-terminated
8032 * sequence of characters that were deleted and then the
8033 * characters that NL replaced.
8034 */
8035 if (State & REPLACE_FLAG)
8036 {
8037 /*
8038 * Do the next ins_char() in NORMAL state, to
8039 * prevent ins_char() from replacing characters and
8040 * avoiding showmatch().
8041 */
8042 oldState = State;
8043 State = NORMAL;
8044 /*
8045 * restore characters (blanks) deleted after cursor
8046 */
8047 while (cc > 0)
8048 {
8049 temp = curwin->w_cursor.col;
8050#ifdef FEAT_MBYTE
8051 mb_replace_pop_ins(cc);
8052#else
8053 ins_char(cc);
8054#endif
8055 curwin->w_cursor.col = temp;
8056 cc = replace_pop();
8057 }
8058 /* restore the characters that NL replaced */
8059 replace_pop_ins();
8060 State = oldState;
8061 }
8062 }
8063 did_ai = FALSE;
8064 }
8065 else
8066 {
8067 /*
8068 * Delete character(s) before the cursor.
8069 */
8070#ifdef FEAT_RIGHTLEFT
8071 if (revins_on) /* put cursor on last inserted char */
8072 dec_cursor();
8073#endif
8074 mincol = 0;
8075 /* keep indent */
8076 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8077#ifdef FEAT_RIGHTLEFT
8078 && !revins_on
8079#endif
8080 )
8081 {
8082 temp = curwin->w_cursor.col;
8083 beginline(BL_WHITE);
8084 if (curwin->w_cursor.col < (colnr_T)temp)
8085 mincol = curwin->w_cursor.col;
8086 curwin->w_cursor.col = temp;
8087 }
8088
8089 /*
8090 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8091 */
8092 if ( mode == BACKSPACE_CHAR
8093 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008094 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 && (*(ml_get_cursor() - 1) == TAB
8096 || (*(ml_get_cursor() - 1) == ' '
8097 && (!*inserted_space_p
8098 || arrow_used))))))
8099 {
8100 int ts;
8101 colnr_T vcol;
8102 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008103#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
8107 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008108 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 ts = curbuf->b_p_sw;
8110 else
8111 ts = curbuf->b_p_sts;
8112 /* Compute the virtual column where we want to be. Since
8113 * 'showbreak' may get in the way, need to get the last column of
8114 * the previous character. */
8115 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8116 dec_cursor();
8117 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8118 inc_cursor();
8119 want_vcol = (want_vcol / ts) * ts;
8120
8121 /* delete characters until we are at or before want_vcol */
8122 while (vcol > want_vcol
8123 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8124 {
8125 dec_cursor();
8126 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8127 if (State & REPLACE_FLAG)
8128 {
8129 /* Don't delete characters before the insert point when in
8130 * Replace mode */
8131 if (curwin->w_cursor.lnum != Insstart.lnum
8132 || curwin->w_cursor.col >= Insstart.col)
8133 {
8134#if 0 /* what was this for? It causes problems when sw != ts. */
8135 if (State == REPLACE && (int)vcol < want_vcol)
8136 {
8137 (void)del_char(FALSE);
8138 extra = 2; /* don't pop too much */
8139 }
8140 else
8141#endif
8142 replace_do_bs();
8143 }
8144 }
8145 else
8146 (void)del_char(FALSE);
8147 }
8148
8149 /* insert extra spaces until we are at want_vcol */
8150 while (vcol < want_vcol)
8151 {
8152 /* Remember the first char we inserted */
8153 if (curwin->w_cursor.lnum == Insstart.lnum
8154 && curwin->w_cursor.col < Insstart.col)
8155 Insstart.col = curwin->w_cursor.col;
8156
8157#ifdef FEAT_VREPLACE
8158 if (State & VREPLACE_FLAG)
8159 ins_char(' ');
8160 else
8161#endif
8162 {
8163 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008164 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008166#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 if (extra)
8168 replace_push_off(NUL);
8169 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 replace_push(NUL);
8172 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008173#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 if (extra == 2)
8175 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 }
8178 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8179 }
8180 }
8181
8182 /*
8183 * Delete upto starting point, start of line or previous word.
8184 */
8185 else do
8186 {
8187#ifdef FEAT_RIGHTLEFT
8188 if (!revins_on) /* put cursor on char to be deleted */
8189#endif
8190 dec_cursor();
8191
8192 /* start of word? */
8193 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8194 {
8195 mode = BACKSPACE_WORD_NOT_SPACE;
8196 temp = vim_iswordc(gchar_cursor());
8197 }
8198 /* end of word? */
8199 else if (mode == BACKSPACE_WORD_NOT_SPACE
8200 && (vim_isspace(cc = gchar_cursor())
8201 || vim_iswordc(cc) != temp))
8202 {
8203#ifdef FEAT_RIGHTLEFT
8204 if (!revins_on)
8205#endif
8206 inc_cursor();
8207#ifdef FEAT_RIGHTLEFT
8208 else if (State & REPLACE_FLAG)
8209 dec_cursor();
8210#endif
8211 break;
8212 }
8213 if (State & REPLACE_FLAG)
8214 replace_do_bs();
8215 else
8216 {
8217#ifdef FEAT_MBYTE
8218 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008219 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220#endif
8221 (void)del_char(FALSE);
8222#ifdef FEAT_MBYTE
8223 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008224 * If there are combining characters and 'delcombine' is set
8225 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 * character.
8227 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008228 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 inc_cursor();
8230#endif
8231#ifdef FEAT_RIGHTLEFT
8232 if (revins_chars)
8233 {
8234 revins_chars--;
8235 revins_legal++;
8236 }
8237 if (revins_on && gchar_cursor() == NUL)
8238 break;
8239#endif
8240 }
8241 /* Just a single backspace?: */
8242 if (mode == BACKSPACE_CHAR)
8243 break;
8244 } while (
8245#ifdef FEAT_RIGHTLEFT
8246 revins_on ||
8247#endif
8248 (curwin->w_cursor.col > mincol
8249 && (curwin->w_cursor.lnum != Insstart.lnum
8250 || curwin->w_cursor.col != Insstart.col)));
8251 did_backspace = TRUE;
8252 }
8253#ifdef FEAT_SMARTINDENT
8254 did_si = FALSE;
8255 can_si = FALSE;
8256 can_si_back = FALSE;
8257#endif
8258 if (curwin->w_cursor.col <= 1)
8259 did_ai = FALSE;
8260 /*
8261 * It's a little strange to put backspaces into the redo
8262 * buffer, but it makes auto-indent a lot easier to deal
8263 * with.
8264 */
8265 AppendCharToRedobuff(c);
8266
8267 /* If deleted before the insertion point, adjust it */
8268 if (curwin->w_cursor.lnum == Insstart.lnum
8269 && curwin->w_cursor.col < Insstart.col)
8270 Insstart.col = curwin->w_cursor.col;
8271
8272 /* vi behaviour: the cursor moves backward but the character that
8273 * was there remains visible
8274 * Vim behaviour: the cursor moves backward and the character that
8275 * was there is erased from the screen.
8276 * We can emulate the vi behaviour by pretending there is a dollar
8277 * displayed even when there isn't.
8278 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8279 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8280 dollar_vcol = curwin->w_virtcol;
8281
8282 return did_backspace;
8283}
8284
8285#ifdef FEAT_MOUSE
8286 static void
8287ins_mouse(c)
8288 int c;
8289{
8290 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008291 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
8293# ifdef FEAT_GUI
8294 /* When GUI is active, also move/paste when 'mouse' is empty */
8295 if (!gui.in_use)
8296# endif
8297 if (!mouse_has(MOUSE_INSERT))
8298 return;
8299
8300 undisplay_dollar();
8301 tpos = curwin->w_cursor;
8302 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8303 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008304#ifdef FEAT_WINDOWS
8305 win_T *new_curwin = curwin;
8306
8307 if (curwin != old_curwin && win_valid(old_curwin))
8308 {
8309 /* Mouse took us to another window. We need to go back to the
8310 * previous one to stop insert there properly. */
8311 curwin = old_curwin;
8312 curbuf = curwin->w_buffer;
8313 }
8314#endif
8315 start_arrow(curwin == old_curwin ? &tpos : NULL);
8316#ifdef FEAT_WINDOWS
8317 if (curwin != new_curwin && win_valid(new_curwin))
8318 {
8319 curwin = new_curwin;
8320 curbuf = curwin->w_buffer;
8321 }
8322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323# ifdef FEAT_CINDENT
8324 can_cindent = TRUE;
8325# endif
8326 }
8327
8328#ifdef FEAT_WINDOWS
8329 /* redraw status lines (in case another window became active) */
8330 redraw_statuslines();
8331#endif
8332}
8333
8334 static void
8335ins_mousescroll(up)
8336 int up;
8337{
8338 pos_T tpos;
8339# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8340 win_T *old_curwin;
8341# endif
8342
8343 tpos = curwin->w_cursor;
8344
8345# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8346 old_curwin = curwin;
8347
8348 /* Currently the mouse coordinates are only known in the GUI. */
8349 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8350 {
8351 int row, col;
8352
8353 row = mouse_row;
8354 col = mouse_col;
8355
8356 /* find the window at the pointer coordinates */
8357 curwin = mouse_find_win(&row, &col);
8358 curbuf = curwin->w_buffer;
8359 }
8360 if (curwin == old_curwin)
8361# endif
8362 undisplay_dollar();
8363
8364 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8365 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8366 else
8367 scroll_redraw(up, 3L);
8368
8369# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8370 curwin->w_redr_status = TRUE;
8371
8372 curwin = old_curwin;
8373 curbuf = curwin->w_buffer;
8374# endif
8375
8376 if (!equalpos(curwin->w_cursor, tpos))
8377 {
8378 start_arrow(&tpos);
8379# ifdef FEAT_CINDENT
8380 can_cindent = TRUE;
8381# endif
8382 }
8383}
8384#endif
8385
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008386#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008387 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008388ins_tabline(c)
8389 int c;
8390{
8391 /* We will be leaving the current window, unless closing another tab. */
8392 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8393 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8394 {
8395 undisplay_dollar();
8396 start_arrow(&curwin->w_cursor);
8397# ifdef FEAT_CINDENT
8398 can_cindent = TRUE;
8399# endif
8400 }
8401
8402 if (c == K_TABLINE)
8403 goto_tabpage(current_tab);
8404 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008405 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008406 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008407 redraw_statuslines(); /* will redraw the tabline when needed */
8408 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008409}
8410#endif
8411
8412#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 void
8414ins_scroll()
8415{
8416 pos_T tpos;
8417
8418 undisplay_dollar();
8419 tpos = curwin->w_cursor;
8420 if (gui_do_scroll())
8421 {
8422 start_arrow(&tpos);
8423# ifdef FEAT_CINDENT
8424 can_cindent = TRUE;
8425# endif
8426 }
8427}
8428
8429 void
8430ins_horscroll()
8431{
8432 pos_T tpos;
8433
8434 undisplay_dollar();
8435 tpos = curwin->w_cursor;
8436 if (gui_do_horiz_scroll())
8437 {
8438 start_arrow(&tpos);
8439# ifdef FEAT_CINDENT
8440 can_cindent = TRUE;
8441# endif
8442 }
8443}
8444#endif
8445
8446 static void
8447ins_left()
8448{
8449 pos_T tpos;
8450
8451#ifdef FEAT_FOLDING
8452 if ((fdo_flags & FDO_HOR) && KeyTyped)
8453 foldOpenCursor();
8454#endif
8455 undisplay_dollar();
8456 tpos = curwin->w_cursor;
8457 if (oneleft() == OK)
8458 {
8459 start_arrow(&tpos);
8460#ifdef FEAT_RIGHTLEFT
8461 /* If exit reversed string, position is fixed */
8462 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8463 revins_legal++;
8464 revins_chars++;
8465#endif
8466 }
8467
8468 /*
8469 * if 'whichwrap' set for cursor in insert mode may go to
8470 * previous line
8471 */
8472 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8473 {
8474 start_arrow(&tpos);
8475 --(curwin->w_cursor.lnum);
8476 coladvance((colnr_T)MAXCOL);
8477 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8478 }
8479 else
8480 vim_beep();
8481}
8482
8483 static void
8484ins_home(c)
8485 int c;
8486{
8487 pos_T tpos;
8488
8489#ifdef FEAT_FOLDING
8490 if ((fdo_flags & FDO_HOR) && KeyTyped)
8491 foldOpenCursor();
8492#endif
8493 undisplay_dollar();
8494 tpos = curwin->w_cursor;
8495 if (c == K_C_HOME)
8496 curwin->w_cursor.lnum = 1;
8497 curwin->w_cursor.col = 0;
8498#ifdef FEAT_VIRTUALEDIT
8499 curwin->w_cursor.coladd = 0;
8500#endif
8501 curwin->w_curswant = 0;
8502 start_arrow(&tpos);
8503}
8504
8505 static void
8506ins_end(c)
8507 int c;
8508{
8509 pos_T tpos;
8510
8511#ifdef FEAT_FOLDING
8512 if ((fdo_flags & FDO_HOR) && KeyTyped)
8513 foldOpenCursor();
8514#endif
8515 undisplay_dollar();
8516 tpos = curwin->w_cursor;
8517 if (c == K_C_END)
8518 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8519 coladvance((colnr_T)MAXCOL);
8520 curwin->w_curswant = MAXCOL;
8521
8522 start_arrow(&tpos);
8523}
8524
8525 static void
8526ins_s_left()
8527{
8528#ifdef FEAT_FOLDING
8529 if ((fdo_flags & FDO_HOR) && KeyTyped)
8530 foldOpenCursor();
8531#endif
8532 undisplay_dollar();
8533 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8534 {
8535 start_arrow(&curwin->w_cursor);
8536 (void)bck_word(1L, FALSE, FALSE);
8537 curwin->w_set_curswant = TRUE;
8538 }
8539 else
8540 vim_beep();
8541}
8542
8543 static void
8544ins_right()
8545{
8546#ifdef FEAT_FOLDING
8547 if ((fdo_flags & FDO_HOR) && KeyTyped)
8548 foldOpenCursor();
8549#endif
8550 undisplay_dollar();
8551 if (gchar_cursor() != NUL || virtual_active()
8552 )
8553 {
8554 start_arrow(&curwin->w_cursor);
8555 curwin->w_set_curswant = TRUE;
8556#ifdef FEAT_VIRTUALEDIT
8557 if (virtual_active())
8558 oneright();
8559 else
8560#endif
8561 {
8562#ifdef FEAT_MBYTE
8563 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008564 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 else
8566#endif
8567 ++curwin->w_cursor.col;
8568 }
8569
8570#ifdef FEAT_RIGHTLEFT
8571 revins_legal++;
8572 if (revins_chars)
8573 revins_chars--;
8574#endif
8575 }
8576 /* if 'whichwrap' set for cursor in insert mode, may move the
8577 * cursor to the next line */
8578 else if (vim_strchr(p_ww, ']') != NULL
8579 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8580 {
8581 start_arrow(&curwin->w_cursor);
8582 curwin->w_set_curswant = TRUE;
8583 ++curwin->w_cursor.lnum;
8584 curwin->w_cursor.col = 0;
8585 }
8586 else
8587 vim_beep();
8588}
8589
8590 static void
8591ins_s_right()
8592{
8593#ifdef FEAT_FOLDING
8594 if ((fdo_flags & FDO_HOR) && KeyTyped)
8595 foldOpenCursor();
8596#endif
8597 undisplay_dollar();
8598 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8599 || gchar_cursor() != NUL)
8600 {
8601 start_arrow(&curwin->w_cursor);
8602 (void)fwd_word(1L, FALSE, 0);
8603 curwin->w_set_curswant = TRUE;
8604 }
8605 else
8606 vim_beep();
8607}
8608
8609 static void
8610ins_up(startcol)
8611 int startcol; /* when TRUE move to Insstart.col */
8612{
8613 pos_T tpos;
8614 linenr_T old_topline = curwin->w_topline;
8615#ifdef FEAT_DIFF
8616 int old_topfill = curwin->w_topfill;
8617#endif
8618
8619 undisplay_dollar();
8620 tpos = curwin->w_cursor;
8621 if (cursor_up(1L, TRUE) == OK)
8622 {
8623 if (startcol)
8624 coladvance(getvcol_nolist(&Insstart));
8625 if (old_topline != curwin->w_topline
8626#ifdef FEAT_DIFF
8627 || old_topfill != curwin->w_topfill
8628#endif
8629 )
8630 redraw_later(VALID);
8631 start_arrow(&tpos);
8632#ifdef FEAT_CINDENT
8633 can_cindent = TRUE;
8634#endif
8635 }
8636 else
8637 vim_beep();
8638}
8639
8640 static void
8641ins_pageup()
8642{
8643 pos_T tpos;
8644
8645 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008646
8647#ifdef FEAT_WINDOWS
8648 if (mod_mask & MOD_MASK_CTRL)
8649 {
8650 /* <C-PageUp>: tab page back */
8651 goto_tabpage(-1);
8652 return;
8653 }
8654#endif
8655
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 tpos = curwin->w_cursor;
8657 if (onepage(BACKWARD, 1L) == OK)
8658 {
8659 start_arrow(&tpos);
8660#ifdef FEAT_CINDENT
8661 can_cindent = TRUE;
8662#endif
8663 }
8664 else
8665 vim_beep();
8666}
8667
8668 static void
8669ins_down(startcol)
8670 int startcol; /* when TRUE move to Insstart.col */
8671{
8672 pos_T tpos;
8673 linenr_T old_topline = curwin->w_topline;
8674#ifdef FEAT_DIFF
8675 int old_topfill = curwin->w_topfill;
8676#endif
8677
8678 undisplay_dollar();
8679 tpos = curwin->w_cursor;
8680 if (cursor_down(1L, TRUE) == OK)
8681 {
8682 if (startcol)
8683 coladvance(getvcol_nolist(&Insstart));
8684 if (old_topline != curwin->w_topline
8685#ifdef FEAT_DIFF
8686 || old_topfill != curwin->w_topfill
8687#endif
8688 )
8689 redraw_later(VALID);
8690 start_arrow(&tpos);
8691#ifdef FEAT_CINDENT
8692 can_cindent = TRUE;
8693#endif
8694 }
8695 else
8696 vim_beep();
8697}
8698
8699 static void
8700ins_pagedown()
8701{
8702 pos_T tpos;
8703
8704 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008705
8706#ifdef FEAT_WINDOWS
8707 if (mod_mask & MOD_MASK_CTRL)
8708 {
8709 /* <C-PageDown>: tab page forward */
8710 goto_tabpage(0);
8711 return;
8712 }
8713#endif
8714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 tpos = curwin->w_cursor;
8716 if (onepage(FORWARD, 1L) == OK)
8717 {
8718 start_arrow(&tpos);
8719#ifdef FEAT_CINDENT
8720 can_cindent = TRUE;
8721#endif
8722 }
8723 else
8724 vim_beep();
8725}
8726
8727#ifdef FEAT_DND
8728 static void
8729ins_drop()
8730{
8731 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8732}
8733#endif
8734
8735/*
8736 * Handle TAB in Insert or Replace mode.
8737 * Return TRUE when the TAB needs to be inserted like a normal character.
8738 */
8739 static int
8740ins_tab()
8741{
8742 int ind;
8743 int i;
8744 int temp;
8745
8746 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8747 Insstart_blank_vcol = get_nolist_virtcol();
8748 if (echeck_abbr(TAB + ABBR_OFF))
8749 return FALSE;
8750
8751 ind = inindent(0);
8752#ifdef FEAT_CINDENT
8753 if (ind)
8754 can_cindent = FALSE;
8755#endif
8756
8757 /*
8758 * When nothing special, insert TAB like a normal character
8759 */
8760 if (!curbuf->b_p_et
8761 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8762 && curbuf->b_p_sts == 0)
8763 return TRUE;
8764
8765 if (stop_arrow() == FAIL)
8766 return TRUE;
8767
8768 did_ai = FALSE;
8769#ifdef FEAT_SMARTINDENT
8770 did_si = FALSE;
8771 can_si = FALSE;
8772 can_si_back = FALSE;
8773#endif
8774 AppendToRedobuff((char_u *)"\t");
8775
8776 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8777 temp = (int)curbuf->b_p_sw;
8778 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8779 temp = (int)curbuf->b_p_sts;
8780 else /* otherwise use 'tabstop' */
8781 temp = (int)curbuf->b_p_ts;
8782 temp -= get_nolist_virtcol() % temp;
8783
8784 /*
8785 * Insert the first space with ins_char(). It will delete one char in
8786 * replace mode. Insert the rest with ins_str(); it will not delete any
8787 * chars. For VREPLACE mode, we use ins_char() for all characters.
8788 */
8789 ins_char(' ');
8790 while (--temp > 0)
8791 {
8792#ifdef FEAT_VREPLACE
8793 if (State & VREPLACE_FLAG)
8794 ins_char(' ');
8795 else
8796#endif
8797 {
8798 ins_str((char_u *)" ");
8799 if (State & REPLACE_FLAG) /* no char replaced */
8800 replace_push(NUL);
8801 }
8802 }
8803
8804 /*
8805 * When 'expandtab' not set: Replace spaces by TABs where possible.
8806 */
8807 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8808 {
8809 char_u *ptr;
8810#ifdef FEAT_VREPLACE
8811 char_u *saved_line = NULL; /* init for GCC */
8812 pos_T pos;
8813#endif
8814 pos_T fpos;
8815 pos_T *cursor;
8816 colnr_T want_vcol, vcol;
8817 int change_col = -1;
8818 int save_list = curwin->w_p_list;
8819
8820 /*
8821 * Get the current line. For VREPLACE mode, don't make real changes
8822 * yet, just work on a copy of the line.
8823 */
8824#ifdef FEAT_VREPLACE
8825 if (State & VREPLACE_FLAG)
8826 {
8827 pos = curwin->w_cursor;
8828 cursor = &pos;
8829 saved_line = vim_strsave(ml_get_curline());
8830 if (saved_line == NULL)
8831 return FALSE;
8832 ptr = saved_line + pos.col;
8833 }
8834 else
8835#endif
8836 {
8837 ptr = ml_get_cursor();
8838 cursor = &curwin->w_cursor;
8839 }
8840
8841 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8842 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8843 curwin->w_p_list = FALSE;
8844
8845 /* Find first white before the cursor */
8846 fpos = curwin->w_cursor;
8847 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8848 {
8849 --fpos.col;
8850 --ptr;
8851 }
8852
8853 /* In Replace mode, don't change characters before the insert point. */
8854 if ((State & REPLACE_FLAG)
8855 && fpos.lnum == Insstart.lnum
8856 && fpos.col < Insstart.col)
8857 {
8858 ptr += Insstart.col - fpos.col;
8859 fpos.col = Insstart.col;
8860 }
8861
8862 /* compute virtual column numbers of first white and cursor */
8863 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8864 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8865
8866 /* Use as many TABs as possible. Beware of 'showbreak' and
8867 * 'linebreak' adding extra virtual columns. */
8868 while (vim_iswhite(*ptr))
8869 {
8870 i = lbr_chartabsize((char_u *)"\t", vcol);
8871 if (vcol + i > want_vcol)
8872 break;
8873 if (*ptr != TAB)
8874 {
8875 *ptr = TAB;
8876 if (change_col < 0)
8877 {
8878 change_col = fpos.col; /* Column of first change */
8879 /* May have to adjust Insstart */
8880 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8881 Insstart.col = fpos.col;
8882 }
8883 }
8884 ++fpos.col;
8885 ++ptr;
8886 vcol += i;
8887 }
8888
8889 if (change_col >= 0)
8890 {
8891 int repl_off = 0;
8892
8893 /* Skip over the spaces we need. */
8894 while (vcol < want_vcol && *ptr == ' ')
8895 {
8896 vcol += lbr_chartabsize(ptr, vcol);
8897 ++ptr;
8898 ++repl_off;
8899 }
8900 if (vcol > want_vcol)
8901 {
8902 /* Must have a char with 'showbreak' just before it. */
8903 --ptr;
8904 --repl_off;
8905 }
8906 fpos.col += repl_off;
8907
8908 /* Delete following spaces. */
8909 i = cursor->col - fpos.col;
8910 if (i > 0)
8911 {
8912 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8913 /* correct replace stack. */
8914 if ((State & REPLACE_FLAG)
8915#ifdef FEAT_VREPLACE
8916 && !(State & VREPLACE_FLAG)
8917#endif
8918 )
8919 for (temp = i; --temp >= 0; )
8920 replace_join(repl_off);
8921 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008922#ifdef FEAT_NETBEANS_INTG
8923 if (usingNetbeans)
8924 {
8925 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8926 (long)(i + 1));
8927 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8928 (char_u *)"\t", 1);
8929 }
8930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 cursor->col -= i;
8932
8933#ifdef FEAT_VREPLACE
8934 /*
8935 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8936 * backspacing over the changed spacing and then inserting the new
8937 * spacing.
8938 */
8939 if (State & VREPLACE_FLAG)
8940 {
8941 /* Backspace from real cursor to change_col */
8942 backspace_until_column(change_col);
8943
8944 /* Insert each char in saved_line from changed_col to
8945 * ptr-cursor */
8946 ins_bytes_len(saved_line + change_col,
8947 cursor->col - change_col);
8948 }
8949#endif
8950 }
8951
8952#ifdef FEAT_VREPLACE
8953 if (State & VREPLACE_FLAG)
8954 vim_free(saved_line);
8955#endif
8956 curwin->w_p_list = save_list;
8957 }
8958
8959 return FALSE;
8960}
8961
8962/*
8963 * Handle CR or NL in insert mode.
8964 * Return TRUE when out of memory or can't undo.
8965 */
8966 static int
8967ins_eol(c)
8968 int c;
8969{
8970 int i;
8971
8972 if (echeck_abbr(c + ABBR_OFF))
8973 return FALSE;
8974 if (stop_arrow() == FAIL)
8975 return TRUE;
8976 undisplay_dollar();
8977
8978 /*
8979 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8980 * character under the cursor. Only push a NUL on the replace stack,
8981 * nothing to put back when the NL is deleted.
8982 */
8983 if ((State & REPLACE_FLAG)
8984#ifdef FEAT_VREPLACE
8985 && !(State & VREPLACE_FLAG)
8986#endif
8987 )
8988 replace_push(NUL);
8989
8990 /*
8991 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8992 * replacing the next line, so we push all of the characters left on the
8993 * line onto the replace stack. This is not done here though, it is done
8994 * in open_line().
8995 */
8996
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008997#ifdef FEAT_VIRTUALEDIT
8998 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
8999 * CTRL-O). */
9000 if (virtual_active() && curwin->w_cursor.coladd > 0)
9001 coladvance(getviscol());
9002#endif
9003
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004#ifdef FEAT_RIGHTLEFT
9005# ifdef FEAT_FKMAP
9006 if (p_altkeymap && p_fkmap)
9007 fkmap(NL);
9008# endif
9009 /* NL in reverse insert will always start in the end of
9010 * current line. */
9011 if (revins_on)
9012 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9013#endif
9014
9015 AppendToRedobuff(NL_STR);
9016 i = open_line(FORWARD,
9017#ifdef FEAT_COMMENTS
9018 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9019#endif
9020 0, old_indent);
9021 old_indent = 0;
9022#ifdef FEAT_CINDENT
9023 can_cindent = TRUE;
9024#endif
9025
9026 return (!i);
9027}
9028
9029#ifdef FEAT_DIGRAPHS
9030/*
9031 * Handle digraph in insert mode.
9032 * Returns character still to be inserted, or NUL when nothing remaining to be
9033 * done.
9034 */
9035 static int
9036ins_digraph()
9037{
9038 int c;
9039 int cc;
9040
9041 pc_status = PC_STATUS_UNSET;
9042 if (redrawing() && !char_avail())
9043 {
9044 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009045 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 edit_putchar('?', TRUE);
9048#ifdef FEAT_CMDL_INFO
9049 add_to_showcmd_c(Ctrl_K);
9050#endif
9051 }
9052
9053#ifdef USE_ON_FLY_SCROLL
9054 dont_scroll = TRUE; /* disallow scrolling here */
9055#endif
9056
9057 /* don't map the digraph chars. This also prevents the
9058 * mode message to be deleted when ESC is hit */
9059 ++no_mapping;
9060 ++allow_keys;
9061 c = safe_vgetc();
9062 --no_mapping;
9063 --allow_keys;
9064 if (IS_SPECIAL(c) || mod_mask) /* special key */
9065 {
9066#ifdef FEAT_CMDL_INFO
9067 clear_showcmd();
9068#endif
9069 insert_special(c, TRUE, FALSE);
9070 return NUL;
9071 }
9072 if (c != ESC)
9073 {
9074 if (redrawing() && !char_avail())
9075 {
9076 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009077 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
9079 if (char2cells(c) == 1)
9080 {
9081 /* first remove the '?', otherwise it's restored when typing
9082 * an ESC next */
9083 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009084 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 edit_putchar(c, TRUE);
9086 }
9087#ifdef FEAT_CMDL_INFO
9088 add_to_showcmd_c(c);
9089#endif
9090 }
9091 ++no_mapping;
9092 ++allow_keys;
9093 cc = safe_vgetc();
9094 --no_mapping;
9095 --allow_keys;
9096 if (cc != ESC)
9097 {
9098 AppendToRedobuff((char_u *)CTRL_V_STR);
9099 c = getdigraph(c, cc, TRUE);
9100#ifdef FEAT_CMDL_INFO
9101 clear_showcmd();
9102#endif
9103 return c;
9104 }
9105 }
9106 edit_unputchar();
9107#ifdef FEAT_CMDL_INFO
9108 clear_showcmd();
9109#endif
9110 return NUL;
9111}
9112#endif
9113
9114/*
9115 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9116 * Returns the char to be inserted, or NUL if none found.
9117 */
9118 static int
9119ins_copychar(lnum)
9120 linenr_T lnum;
9121{
9122 int c;
9123 int temp;
9124 char_u *ptr, *prev_ptr;
9125
9126 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9127 {
9128 vim_beep();
9129 return NUL;
9130 }
9131
9132 /* try to advance to the cursor column */
9133 temp = 0;
9134 ptr = ml_get(lnum);
9135 prev_ptr = ptr;
9136 validate_virtcol();
9137 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9138 {
9139 prev_ptr = ptr;
9140 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9141 }
9142 if ((colnr_T)temp > curwin->w_virtcol)
9143 ptr = prev_ptr;
9144
9145#ifdef FEAT_MBYTE
9146 c = (*mb_ptr2char)(ptr);
9147#else
9148 c = *ptr;
9149#endif
9150 if (c == NUL)
9151 vim_beep();
9152 return c;
9153}
9154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009155/*
9156 * CTRL-Y or CTRL-E typed in Insert mode.
9157 */
9158 static int
9159ins_ctrl_ey(tc)
9160 int tc;
9161{
9162 int c = tc;
9163
9164#ifdef FEAT_INS_EXPAND
9165 if (ctrl_x_mode == CTRL_X_SCROLL)
9166 {
9167 if (c == Ctrl_Y)
9168 scrolldown_clamp();
9169 else
9170 scrollup_clamp();
9171 redraw_later(VALID);
9172 }
9173 else
9174#endif
9175 {
9176 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9177 if (c != NUL)
9178 {
9179 long tw_save;
9180
9181 /* The character must be taken literally, insert like it
9182 * was typed after a CTRL-V, and pretend 'textwidth'
9183 * wasn't set. Digits, 'o' and 'x' are special after a
9184 * CTRL-V, don't use it for these. */
9185 if (c < 256 && !isalnum(c))
9186 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9187 tw_save = curbuf->b_p_tw;
9188 curbuf->b_p_tw = -1;
9189 insert_special(c, TRUE, FALSE);
9190 curbuf->b_p_tw = tw_save;
9191#ifdef FEAT_RIGHTLEFT
9192 revins_chars++;
9193 revins_legal++;
9194#endif
9195 c = Ctrl_V; /* pretend CTRL-V is last character */
9196 auto_format(FALSE, TRUE);
9197 }
9198 }
9199 return c;
9200}
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202#ifdef FEAT_SMARTINDENT
9203/*
9204 * Try to do some very smart auto-indenting.
9205 * Used when inserting a "normal" character.
9206 */
9207 static void
9208ins_try_si(c)
9209 int c;
9210{
9211 pos_T *pos, old_pos;
9212 char_u *ptr;
9213 int i;
9214 int temp;
9215
9216 /*
9217 * do some very smart indenting when entering '{' or '}'
9218 */
9219 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9220 {
9221 /*
9222 * for '}' set indent equal to indent of line containing matching '{'
9223 */
9224 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9225 {
9226 old_pos = curwin->w_cursor;
9227 /*
9228 * If the matching '{' has a ')' immediately before it (ignoring
9229 * white-space), then line up with the start of the line
9230 * containing the matching '(' if there is one. This handles the
9231 * case where an "if (..\n..) {" statement continues over multiple
9232 * lines -- webb
9233 */
9234 ptr = ml_get(pos->lnum);
9235 i = pos->col;
9236 if (i > 0) /* skip blanks before '{' */
9237 while (--i > 0 && vim_iswhite(ptr[i]))
9238 ;
9239 curwin->w_cursor.lnum = pos->lnum;
9240 curwin->w_cursor.col = i;
9241 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9242 curwin->w_cursor = *pos;
9243 i = get_indent();
9244 curwin->w_cursor = old_pos;
9245#ifdef FEAT_VREPLACE
9246 if (State & VREPLACE_FLAG)
9247 change_indent(INDENT_SET, i, FALSE, NUL);
9248 else
9249#endif
9250 (void)set_indent(i, SIN_CHANGED);
9251 }
9252 else if (curwin->w_cursor.col > 0)
9253 {
9254 /*
9255 * when inserting '{' after "O" reduce indent, but not
9256 * more than indent of previous line
9257 */
9258 temp = TRUE;
9259 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9260 {
9261 old_pos = curwin->w_cursor;
9262 i = get_indent();
9263 while (curwin->w_cursor.lnum > 1)
9264 {
9265 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9266
9267 /* ignore empty lines and lines starting with '#'. */
9268 if (*ptr != '#' && *ptr != NUL)
9269 break;
9270 }
9271 if (get_indent() >= i)
9272 temp = FALSE;
9273 curwin->w_cursor = old_pos;
9274 }
9275 if (temp)
9276 shift_line(TRUE, FALSE, 1);
9277 }
9278 }
9279
9280 /*
9281 * set indent of '#' always to 0
9282 */
9283 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9284 {
9285 /* remember current indent for next line */
9286 old_indent = get_indent();
9287 (void)set_indent(0, SIN_CHANGED);
9288 }
9289
9290 /* Adjust ai_col, the char at this position can be deleted. */
9291 if (ai_col > curwin->w_cursor.col)
9292 ai_col = curwin->w_cursor.col;
9293}
9294#endif
9295
9296/*
9297 * Get the value that w_virtcol would have when 'list' is off.
9298 * Unless 'cpo' contains the 'L' flag.
9299 */
9300 static colnr_T
9301get_nolist_virtcol()
9302{
9303 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9304 return getvcol_nolist(&curwin->w_cursor);
9305 validate_virtcol();
9306 return curwin->w_virtcol;
9307}