blob: 3f933c14c27c4b6e2e7dde740520e46ebfce766c [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 Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000132static 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 +0000133static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000134static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000135static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000137static void ins_compl_upd_pum __ARGS((void));
138static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000139static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000140static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000142static 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 +0000143static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void ins_compl_free __ARGS((void));
145static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000146static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000147static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000148static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000149static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000150static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000151static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000152static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000154#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
155static void ins_compl_add_list __ARGS((list_T *list));
156#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_compl_delete __ARGS((void));
159static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000160static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000161static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000164static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static int ins_complete __ARGS((int c));
166static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
167#endif /* FEAT_INS_EXPAND */
168
169#define BACKSPACE_CHAR 1
170#define BACKSPACE_WORD 2
171#define BACKSPACE_WORD_NOT_SPACE 3
172#define BACKSPACE_LINE 4
173
Bram Moolenaar754b5602006-02-09 23:53:20 +0000174static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void ins_ctrl_v __ARGS((void));
176static void undisplay_dollar __ARGS((void));
177static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void check_auto_format __ARGS((int));
180static void redo_literal __ARGS((int c));
181static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000182#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000183static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000184static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000185static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
188static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000189#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static int replace_pop __ARGS((void));
193static void replace_join __ARGS((int off));
194static void replace_pop_ins __ARGS((void));
195#ifdef FEAT_MBYTE
196static void mb_replace_pop_ins __ARGS((int cc));
197#endif
198static void replace_flush __ARGS((void));
199static void replace_do_bs __ARGS((void));
200#ifdef FEAT_CINDENT
201static int cindent_on __ARGS((void));
202#endif
203static void ins_reg __ARGS((void));
204static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000205static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000206static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_RIGHTLEFT
208static void ins_ctrl_ __ARGS((void));
209#endif
210#ifdef FEAT_VISUAL
211static int ins_start_select __ARGS((int c));
212#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_insert __ARGS((int replaceState));
214static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ins_shift __ARGS((int c, int lastc));
216static void ins_del __ARGS((void));
217static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
218#ifdef FEAT_MOUSE
219static void ins_mouse __ARGS((int c));
220static void ins_mousescroll __ARGS((int up));
221#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000222#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
223static void ins_tabline __ARGS((int c));
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225static void ins_left __ARGS((void));
226static void ins_home __ARGS((int c));
227static void ins_end __ARGS((int c));
228static void ins_s_left __ARGS((void));
229static void ins_right __ARGS((void));
230static void ins_s_right __ARGS((void));
231static void ins_up __ARGS((int startcol));
232static void ins_pageup __ARGS((void));
233static void ins_down __ARGS((int startcol));
234static void ins_pagedown __ARGS((void));
235#ifdef FEAT_DND
236static void ins_drop __ARGS((void));
237#endif
238static int ins_tab __ARGS((void));
239static int ins_eol __ARGS((int c));
240#ifdef FEAT_DIGRAPHS
241static int ins_digraph __ARGS((void));
242#endif
243static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000244static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_SMARTINDENT
246static void ins_try_si __ARGS((int c));
247#endif
248static colnr_T get_nolist_virtcol __ARGS((void));
249
250static colnr_T Insstart_textlen; /* length of line when insert started */
251static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
252
253static char_u *last_insert = NULL; /* the text of the previous insert,
254 K_SPECIAL and CSI are escaped */
255static int last_insert_skip; /* nr of chars in front of previous insert */
256static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000257static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259#ifdef FEAT_CINDENT
260static int can_cindent; /* may do cindenting on this line */
261#endif
262
263static int old_indent = 0; /* for ^^D command in insert mode */
264
265#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000266static int revins_on; /* reverse insert mode on */
267static int revins_chars; /* how much to skip after edit */
268static int revins_legal; /* was the last char 'legal'? */
269static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static int ins_need_undo; /* call u_save() before inserting a
273 char. Set when edit() is called.
274 after that arrow_used is used. */
275
276static int did_add_space = FALSE; /* auto_format() added an extra space
277 under the cursor */
278
279/*
280 * edit(): Start inserting text.
281 *
282 * "cmdchar" can be:
283 * 'i' normal insert command
284 * 'a' normal append command
285 * 'R' replace command
286 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
287 * but still only one <CR> is inserted. The <Esc> is not used for redo.
288 * 'g' "gI" command.
289 * 'V' "gR" command for Virtual Replace mode.
290 * 'v' "gr" command for single character Virtual Replace mode.
291 *
292 * This function is not called recursively. For CTRL-O commands, it returns
293 * and lets the caller handle the Normal-mode command.
294 *
295 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
296 */
297 int
298edit(cmdchar, startln, count)
299 int cmdchar;
300 int startln; /* if set, insert at start of line */
301 long count;
302{
303 int c = 0;
304 char_u *ptr;
305 int lastc;
306 colnr_T mincol;
307 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 int i;
309 int did_backspace = TRUE; /* previous char was backspace */
310#ifdef FEAT_CINDENT
311 int line_is_white = FALSE; /* line is empty before insert */
312#endif
313 linenr_T old_topline = 0; /* topline before insertion */
314#ifdef FEAT_DIFF
315 int old_topfill = -1;
316#endif
317 int inserted_space = FALSE; /* just inserted a space */
318 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000319 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000321 /* Remember whether editing was restarted after CTRL-O. */
322 did_restart_edit = restart_edit;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 /* sleep before redrawing, needed for "CTRL-O :" that results in an
325 * error message */
326 check_for_delay(TRUE);
327
328#ifdef HAVE_SANDBOX
329 /* Don't allow inserting in the sandbox. */
330 if (sandbox != 0)
331 {
332 EMSG(_(e_sandbox));
333 return FALSE;
334 }
335#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000336 /* Don't allow changes in the buffer while editing the cmdline. The
337 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000338 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 {
340 EMSG(_(e_secure));
341 return FALSE;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 /* Don't allow recursive insert mode when busy with completion. */
346 if (compl_started || pum_visible())
347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 ins_compl_clear(); /* clear stuff for CTRL-X mode */
352#endif
353
Bram Moolenaar843ee412004-06-30 16:16:41 +0000354#ifdef FEAT_AUTOCMD
355 /*
356 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
357 */
358 if (cmdchar != 'r' && cmdchar != 'v')
359 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000360# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361 if (cmdchar == 'R')
362 ptr = (char_u *)"r";
363 else if (cmdchar == 'V')
364 ptr = (char_u *)"v";
365 else
366 ptr = (char_u *)"i";
367 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
370 }
371#endif
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef FEAT_MOUSE
374 /*
375 * When doing a paste with the middle mouse button, Insstart is set to
376 * where the paste started.
377 */
378 if (where_paste_started.lnum != 0)
379 Insstart = where_paste_started;
380 else
381#endif
382 {
383 Insstart = curwin->w_cursor;
384 if (startln)
385 Insstart.col = 0;
386 }
387 Insstart_textlen = linetabsize(ml_get_curline());
388 Insstart_blank_vcol = MAXCOL;
389 if (!did_ai)
390 ai_col = 0;
391
392 if (cmdchar != NUL && restart_edit == 0)
393 {
394 ResetRedobuff();
395 AppendNumberToRedobuff(count);
396#ifdef FEAT_VREPLACE
397 if (cmdchar == 'V' || cmdchar == 'v')
398 {
399 /* "gR" or "gr" command */
400 AppendCharToRedobuff('g');
401 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
402 }
403 else
404#endif
405 {
406 AppendCharToRedobuff(cmdchar);
407 if (cmdchar == 'g') /* "gI" command */
408 AppendCharToRedobuff('I');
409 else if (cmdchar == 'r') /* "r<CR>" command */
410 count = 1; /* insert only one <CR> */
411 }
412 }
413
414 if (cmdchar == 'R')
415 {
416#ifdef FEAT_FKMAP
417 if (p_fkmap && p_ri)
418 {
419 beep_flush();
420 EMSG(farsi_text_3); /* encoded in Farsi */
421 State = INSERT;
422 }
423 else
424#endif
425 State = REPLACE;
426 }
427#ifdef FEAT_VREPLACE
428 else if (cmdchar == 'V' || cmdchar == 'v')
429 {
430 State = VREPLACE;
431 replaceState = VREPLACE;
432 orig_line_count = curbuf->b_ml.ml_line_count;
433 vr_lines_changed = 1;
434 }
435#endif
436 else
437 State = INSERT;
438
439 stop_insert_mode = FALSE;
440
441 /*
442 * Need to recompute the cursor position, it might move when the cursor is
443 * on a TAB or special character.
444 */
445 curs_columns(TRUE);
446
447 /*
448 * Enable langmap or IME, indicated by 'iminsert'.
449 * Note that IME may enabled/disabled without us noticing here, thus the
450 * 'iminsert' value may not reflect what is actually used. It is updated
451 * when hitting <Esc>.
452 */
453 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
454 State |= LANGMAP;
455#ifdef USE_IM_CONTROL
456 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_MOUSE
460 setmouse();
461#endif
462#ifdef FEAT_CMDL_INFO
463 clear_showcmd();
464#endif
465#ifdef FEAT_RIGHTLEFT
466 /* there is no reverse replace mode */
467 revins_on = (State == INSERT && p_ri);
468 if (revins_on)
469 undisplay_dollar();
470 revins_chars = 0;
471 revins_legal = 0;
472 revins_scol = -1;
473#endif
474
475 /*
476 * Handle restarting Insert mode.
477 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
478 * restart_edit non-zero, and something in the stuff buffer.
479 */
480 if (restart_edit != 0 && stuff_empty())
481 {
482#ifdef FEAT_MOUSE
483 /*
484 * After a paste we consider text typed to be part of the insert for
485 * the pasted text. You can backspace over the pasted text too.
486 */
487 if (where_paste_started.lnum)
488 arrow_used = FALSE;
489 else
490#endif
491 arrow_used = TRUE;
492 restart_edit = 0;
493
494 /*
495 * If the cursor was after the end-of-line before the CTRL-O and it is
496 * now at the end-of-line, put it after the end-of-line (this is not
497 * correct in very rare cases).
498 * Also do this if curswant is greater than the current virtual
499 * column. Eg after "^O$" or "^O80|".
500 */
501 validate_virtcol();
502 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000503 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 || curwin->w_curswant > curwin->w_virtcol)
505 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
506 {
507 if (ptr[1] == NUL)
508 ++curwin->w_cursor.col;
509#ifdef FEAT_MBYTE
510 else if (has_mbyte)
511 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000512 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (ptr[i] == NUL)
514 curwin->w_cursor.col += i;
515 }
516#endif
517 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 else
521 arrow_used = FALSE;
522
523 /* we are in insert mode now, don't need to start it anymore */
524 need_start_insertmode = FALSE;
525
526 /* Need to save the line for undo before inserting the first char. */
527 ins_need_undo = TRUE;
528
529#ifdef FEAT_MOUSE
530 where_paste_started.lnum = 0;
531#endif
532#ifdef FEAT_CINDENT
533 can_cindent = TRUE;
534#endif
535#ifdef FEAT_FOLDING
536 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
537 * restarting. */
538 if (!p_im && did_restart_edit == 0)
539 foldOpenCursor();
540#endif
541
542 /*
543 * If 'showmode' is set, show the current (insert/replace/..) mode.
544 * A warning message for changing a readonly file is given here, before
545 * actually changing anything. It's put after the mode, if any.
546 */
547 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000548 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 i = showmode();
550
551 if (!p_im && did_restart_edit == 0)
552 change_warning(i + 1);
553
554#ifdef CURSOR_SHAPE
555 ui_cursor_shape(); /* may show different cursor shape */
556#endif
557#ifdef FEAT_DIGRAPHS
558 do_digraph(-1); /* clear digraphs */
559#endif
560
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000561 /*
562 * Get the current length of the redo buffer, those characters have to be
563 * skipped if we want to get to the inserted characters.
564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 ptr = get_inserted();
566 if (ptr == NULL)
567 new_insert_skip = 0;
568 else
569 {
570 new_insert_skip = (int)STRLEN(ptr);
571 vim_free(ptr);
572 }
573
574 old_indent = 0;
575
576 /*
577 * Main loop in Insert mode: repeat until Insert mode is left.
578 */
579 for (;;)
580 {
581#ifdef FEAT_RIGHTLEFT
582 if (!revins_legal)
583 revins_scol = -1; /* reset on illegal motions */
584 else
585 revins_legal = 0;
586#endif
587 if (arrow_used) /* don't repeat insert when arrow key used */
588 count = 0;
589
590 if (stop_insert_mode)
591 {
592 /* ":stopinsert" used or 'insertmode' reset */
593 count = 0;
594 goto doESCkey;
595 }
596
597 /* set curwin->w_curswant for next K_DOWN or K_UP */
598 if (!arrow_used)
599 curwin->w_set_curswant = TRUE;
600
601 /* If there is no typeahead may check for timestamps (e.g., for when a
602 * menu invoked a shell command). */
603 if (stuff_empty())
604 {
605 did_check_timestamps = FALSE;
606 if (need_check_timestamps)
607 check_timestamps(FALSE);
608 }
609
610 /*
611 * When emsg() was called msg_scroll will have been set.
612 */
613 msg_scroll = FALSE;
614
615#ifdef FEAT_GUI
616 /* When 'mousefocus' is set a mouse movement may have taken us to
617 * another window. "need_mouse_correct" may then be set because of an
618 * autocommand. */
619 if (need_mouse_correct)
620 gui_mouse_correct();
621#endif
622
623#ifdef FEAT_FOLDING
624 /* Open fold at the cursor line, according to 'foldopen'. */
625 if (fdo_flags & FDO_INSERT)
626 foldOpenCursor();
627 /* Close folds where the cursor isn't, according to 'foldclose' */
628 if (!char_avail())
629 foldCheckClose();
630#endif
631
632 /*
633 * If we inserted a character at the last position of the last line in
634 * the window, scroll the window one line up. This avoids an extra
635 * redraw.
636 * This is detected when the cursor column is smaller after inserting
637 * something.
638 * Don't do this when the topline changed already, it has
639 * already been adjusted (by insertchar() calling open_line())).
640 */
641 if (curbuf->b_mod_set
642 && curwin->w_p_wrap
643 && !did_backspace
644 && curwin->w_topline == old_topline
645#ifdef FEAT_DIFF
646 && curwin->w_topfill == old_topfill
647#endif
648 )
649 {
650 mincol = curwin->w_wcol;
651 validate_cursor_col();
652
653 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
654 && curwin->w_wrow == W_WINROW(curwin)
655 + curwin->w_height - 1 - p_so
656 && (curwin->w_cursor.lnum != curwin->w_topline
657#ifdef FEAT_DIFF
658 || curwin->w_topfill > 0
659#endif
660 ))
661 {
662#ifdef FEAT_DIFF
663 if (curwin->w_topfill > 0)
664 --curwin->w_topfill;
665 else
666#endif
667#ifdef FEAT_FOLDING
668 if (hasFolding(curwin->w_topline, NULL, &old_topline))
669 set_topline(curwin, old_topline + 1);
670 else
671#endif
672 set_topline(curwin, curwin->w_topline + 1);
673 }
674 }
675
676 /* May need to adjust w_topline to show the cursor. */
677 update_topline();
678
679 did_backspace = FALSE;
680
681 validate_cursor(); /* may set must_redraw */
682
683 /*
684 * Redraw the display when no characters are waiting.
685 * Also shows mode, ruler and positions cursor.
686 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000687 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689#ifdef FEAT_SCROLLBIND
690 if (curwin->w_p_scb)
691 do_check_scrollbind(TRUE);
692#endif
693
694 update_curswant();
695 old_topline = curwin->w_topline;
696#ifdef FEAT_DIFF
697 old_topfill = curwin->w_topfill;
698#endif
699
700#ifdef USE_ON_FLY_SCROLL
701 dont_scroll = FALSE; /* allow scrolling here */
702#endif
703
704 /*
705 * Get a character for Insert mode.
706 */
707 lastc = c; /* remember previous char for CTRL-D */
708 c = safe_vgetc();
709
710#ifdef FEAT_RIGHTLEFT
711 if (p_hkmap && KeyTyped)
712 c = hkmap(c); /* Hebrew mode mapping */
713#endif
714#ifdef FEAT_FKMAP
715 if (p_fkmap && KeyTyped)
716 c = fkmap(c); /* Farsi mode mapping */
717#endif
718
719#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000720 /*
721 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000722 * and the cursor is still in the completed word. Only when there is
723 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000724 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000725 if (compl_started
726 && pum_wanted()
727 && curwin->w_cursor.col >= compl_col
728 && (compl_shown_match == NULL
729 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000730 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000731 /* BS: Delete one character from "compl_leader". */
732 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000733 && curwin->w_cursor.col > compl_col
734 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000735 continue;
736
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000737 /* When no match was selected or it was edited. */
738 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000739 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000740 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000741 * "compl_leader". Except when at the original match and
742 * there is nothing to add, CTRL-L works like CTRL-P then. */
743 if (c == Ctrl_L
744 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
745 || STRLEN(compl_shown_match->cp_str)
746 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000747 {
748 ins_compl_addfrommatch();
749 continue;
750 }
751
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000752 /* A printable, non-white character: Add to "compl_leader". */
753 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000754 {
755 ins_compl_addleader(c);
756 continue;
757 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000758
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000759 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000760 * compl_enter_selects is set the Enter key does the same. */
761 if (c == Ctrl_Y || (compl_enter_selects
762 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000763 {
764 ins_compl_delete();
765 ins_compl_insert();
766 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000767 }
768 }
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
771 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000772 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000773 if (c != K_IGNORE && ins_compl_prep(c))
774 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776
Bram Moolenaar488c6512005-08-11 20:09:58 +0000777 /* CTRL-\ CTRL-N goes to Normal mode,
778 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
779 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (c == Ctrl_BSL)
781 {
782 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000783 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 ++no_mapping;
785 ++allow_keys;
786 c = safe_vgetc();
787 --no_mapping;
788 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000789 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000791 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 vungetc(c);
793 c = Ctrl_BSL;
794 }
795 else if (c == Ctrl_G && p_im)
796 continue;
797 else
798 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000799 if (c == Ctrl_O)
800 {
801 ins_ctrl_o();
802 ins_at_eol = FALSE; /* cursor keeps its column */
803 nomove = TRUE;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 count = 0;
806 goto doESCkey;
807 }
808 }
809
810#ifdef FEAT_DIGRAPHS
811 c = do_digraph(c);
812#endif
813
814#ifdef FEAT_INS_EXPAND
815 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
816 goto docomplete;
817#endif
818 if (c == Ctrl_V || c == Ctrl_Q)
819 {
820 ins_ctrl_v();
821 c = Ctrl_V; /* pretend CTRL-V is last typed character */
822 continue;
823 }
824
825#ifdef FEAT_CINDENT
826 if (cindent_on()
827# ifdef FEAT_INS_EXPAND
828 && ctrl_x_mode == 0
829# endif
830 )
831 {
832 /* A key name preceded by a bang means this key is not to be
833 * inserted. Skip ahead to the re-indenting below.
834 * A key name preceded by a star means that indenting has to be
835 * done before inserting the key. */
836 line_is_white = inindent(0);
837 if (in_cinkeys(c, '!', line_is_white))
838 goto force_cindent;
839 if (can_cindent && in_cinkeys(c, '*', line_is_white)
840 && stop_arrow() == OK)
841 do_c_expr_indent();
842 }
843#endif
844
845#ifdef FEAT_RIGHTLEFT
846 if (curwin->w_p_rl)
847 switch (c)
848 {
849 case K_LEFT: c = K_RIGHT; break;
850 case K_S_LEFT: c = K_S_RIGHT; break;
851 case K_C_LEFT: c = K_C_RIGHT; break;
852 case K_RIGHT: c = K_LEFT; break;
853 case K_S_RIGHT: c = K_S_LEFT; break;
854 case K_C_RIGHT: c = K_C_LEFT; break;
855 }
856#endif
857
858#ifdef FEAT_VISUAL
859 /*
860 * If 'keymodel' contains "startsel", may start selection. If it
861 * does, a CTRL-O and c will be stuffed, we need to get these
862 * characters.
863 */
864 if (ins_start_select(c))
865 continue;
866#endif
867
868 /*
869 * The big switch to handle a character in insert mode.
870 */
871 switch (c)
872 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (echeck_abbr(ESC + ABBR_OFF))
875 break;
876 /*FALLTHROUGH*/
877
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000878 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#ifdef FEAT_CMDWIN
880 if (c == Ctrl_C && cmdwin_type != 0)
881 {
882 /* Close the cmdline window. */
883 cmdwin_result = K_IGNORE;
884 got_int = FALSE; /* don't stop executing autocommands et al. */
885 goto doESCkey;
886 }
887#endif
888
889#ifdef UNIX
890do_intr:
891#endif
892 /* when 'insertmode' set, and not halfway a mapping, don't leave
893 * Insert mode */
894 if (goto_im())
895 {
896 if (got_int)
897 {
898 (void)vgetc(); /* flush all buffers */
899 got_int = FALSE;
900 }
901 else
902 vim_beep();
903 break;
904 }
905doESCkey:
906 /*
907 * This is the ONLY return from edit()!
908 */
909 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
910 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000911 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 o_lnum = curwin->w_cursor.lnum;
913
Bram Moolenaar488c6512005-08-11 20:09:58 +0000914 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000915 {
916#ifdef FEAT_AUTOCMD
917 if (cmdchar != 'r' && cmdchar != 'v')
918 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
919 FALSE, curbuf);
920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 continue;
924
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000925 case Ctrl_Z: /* suspend when 'insertmode' set */
926 if (!p_im)
927 goto normalchar; /* insert CTRL-Z as normal char */
928 stuffReadbuff((char_u *)":st\r");
929 c = Ctrl_O;
930 /*FALLTHROUGH*/
931
932 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000933#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000934 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000935 goto docomplete;
936#endif
937 if (echeck_abbr(Ctrl_O + ABBR_OFF))
938 break;
939 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000940
941#ifdef FEAT_VIRTUALEDIT
942 /* don't move the cursor left when 'virtualedit' has "onemore". */
943 if (ve_flags & VE_ONEMORE)
944 {
945 ins_at_eol = FALSE;
946 nomove = TRUE;
947 }
948#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 count = 0;
950 goto doESCkey;
951
Bram Moolenaar572cb562005-08-05 21:35:02 +0000952 case K_INS: /* toggle insert/replace mode */
953 case K_KINS:
954 ins_insert(replaceState);
955 break;
956
957 case K_SELECT: /* end of Select mode mapping - ignore */
958 break;
959
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000960#ifdef FEAT_SNIFF
961 case K_SNIFF: /* Sniff command received */
962 stuffcharReadbuff(K_SNIFF);
963 goto doESCkey;
964#endif
965
966 case K_HELP: /* Help key works like <ESC> <Help> */
967 case K_F1:
968 case K_XF1:
969 stuffcharReadbuff(K_HELP);
970 if (p_im)
971 need_start_insertmode = TRUE;
972 goto doESCkey;
973
974#ifdef FEAT_NETBEANS_INTG
975 case K_F21: /* NetBeans command */
976 ++no_mapping; /* don't map the next key hits */
977 i = safe_vgetc();
978 --no_mapping;
979 netbeans_keycommand(i);
980 break;
981#endif
982
983 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 case NUL:
985 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 /* For ^@ the trailing ESC will end the insert, unless there is an
987 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
989 && c != Ctrl_A && !p_im)
990 goto doESCkey; /* quit insert mode */
991 inserted_space = FALSE;
992 break;
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 ins_reg();
996 auto_format(FALSE, TRUE);
997 inserted_space = FALSE;
998 break;
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 ins_ctrl_g();
1002 break;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case Ctrl_HAT: /* switch input mode and/or langmap */
1005 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 break;
1007
1008#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (!p_ari)
1011 goto normalchar;
1012 ins_ctrl_();
1013 break;
1014#endif
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1018 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1019 goto docomplete;
1020#endif
1021 /* FALLTHROUGH */
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024# ifdef FEAT_INS_EXPAND
1025 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 if (has_compl_option(FALSE))
1028 goto docomplete;
1029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031# endif
1032 ins_shift(c, lastc);
1033 auto_format(FALSE, TRUE);
1034 inserted_space = FALSE;
1035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 case K_KDEL:
1039 ins_del();
1040 auto_format(FALSE, TRUE);
1041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 case Ctrl_H:
1045 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1046 auto_format(FALSE, TRUE);
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1051 auto_format(FALSE, TRUE);
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001055# ifdef FEAT_COMPL_FUNC
1056 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001058 goto docomplete;
1059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1061 auto_format(FALSE, TRUE);
1062 inserted_space = FALSE;
1063 break;
1064
1065#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_LEFTMOUSE_NM:
1068 case K_LEFTDRAG:
1069 case K_LEFTRELEASE:
1070 case K_LEFTRELEASE_NM:
1071 case K_MIDDLEMOUSE:
1072 case K_MIDDLEDRAG:
1073 case K_MIDDLERELEASE:
1074 case K_RIGHTMOUSE:
1075 case K_RIGHTDRAG:
1076 case K_RIGHTRELEASE:
1077 case K_X1MOUSE:
1078 case K_X1DRAG:
1079 case K_X1RELEASE:
1080 case K_X2MOUSE:
1081 case K_X2DRAG:
1082 case K_X2RELEASE:
1083 ins_mouse(c);
1084 break;
1085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 ins_mousescroll(FALSE);
1088 break;
1089
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001090 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ins_mousescroll(TRUE);
1092 break;
1093#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001094#ifdef FEAT_GUI_TABLINE
1095 case K_TABLINE:
1096 case K_TABMENU:
1097 ins_tabline(c);
1098 break;
1099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 break;
1103
Bram Moolenaar754b5602006-02-09 23:53:20 +00001104#ifdef FEAT_AUTOCMD
1105 case K_CURSORHOLD: /* Didn't type something for a while. */
1106 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1107 did_cursorhold = TRUE;
1108 break;
1109#endif
1110
Bram Moolenaar4770d092006-01-12 23:22:24 +00001111#ifdef FEAT_GUI_W32
1112 /* On Win32 ignore <M-F4>, we get it when closing the window was
1113 * cancelled. */
1114 case K_F4:
1115 if (mod_mask != MOD_MASK_ALT)
1116 goto normalchar;
1117 break;
1118#endif
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#ifdef FEAT_GUI
1121 case K_VER_SCROLLBAR:
1122 ins_scroll();
1123 break;
1124
1125 case K_HOR_SCROLLBAR:
1126 ins_horscroll();
1127 break;
1128#endif
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 case K_S_HOME:
1133 case K_C_HOME:
1134 ins_home(c);
1135 break;
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_S_END:
1140 case K_C_END:
1141 ins_end(c);
1142 break;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001145 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1146 ins_s_left();
1147 else
1148 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_C_LEFT:
1153 ins_s_left();
1154 break;
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001157 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1158 ins_s_right();
1159 else
1160 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 case K_C_RIGHT:
1165 ins_s_right();
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001169#ifdef FEAT_INS_EXPAND
1170 if (pum_visible())
1171 goto docomplete;
1172#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001173 if (mod_mask & MOD_MASK_SHIFT)
1174 ins_pageup();
1175 else
1176 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_PAGEUP:
1181 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001182#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001183 if (pum_visible())
1184 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 ins_pageup();
1187 break;
1188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001190#ifdef FEAT_INS_EXPAND
1191 if (pum_visible())
1192 goto docomplete;
1193#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 if (mod_mask & MOD_MASK_SHIFT)
1195 ins_pagedown();
1196 else
1197 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 case K_PAGEDOWN:
1202 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001203#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001204 if (pum_visible())
1205 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 ins_pagedown();
1208 break;
1209
1210#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 ins_drop();
1213 break;
1214#endif
1215
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 c = TAB;
1218 /* FALLTHROUGH */
1219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1222 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1223 goto docomplete;
1224#endif
1225 inserted_space = FALSE;
1226 if (ins_tab())
1227 goto normalchar; /* insert TAB as a normal char */
1228 auto_format(FALSE, TRUE);
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 c = CAR;
1233 /* FALLTHROUGH */
1234 case CAR:
1235 case NL:
1236#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1237 /* In a quickfix window a <CR> jumps to the error under the
1238 * cursor. */
1239 if (bt_quickfix(curbuf) && c == CAR)
1240 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001241 if (curwin->w_llist_ref == NULL) /* quickfix window */
1242 do_cmdline_cmd((char_u *)".cc");
1243 else /* location list window */
1244 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 break;
1246 }
1247#endif
1248#ifdef FEAT_CMDWIN
1249 if (cmdwin_type != 0)
1250 {
1251 /* Execute the command in the cmdline window. */
1252 cmdwin_result = CAR;
1253 goto doESCkey;
1254 }
1255#endif
1256 if (ins_eol(c) && !p_im)
1257 goto doESCkey; /* out of memory */
1258 auto_format(FALSE, FALSE);
1259 inserted_space = FALSE;
1260 break;
1261
1262#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264# ifdef FEAT_INS_EXPAND
1265 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1266 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001267 if (has_compl_option(TRUE))
1268 goto docomplete;
1269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271# endif
1272# ifdef FEAT_DIGRAPHS
1273 c = ins_digraph();
1274 if (c == NUL)
1275 break;
1276# endif
1277 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001281 case Ctrl_X: /* Enter CTRL-X mode */
1282 ins_ctrl_x();
1283 break;
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (ctrl_x_mode != CTRL_X_TAGS)
1287 goto normalchar;
1288 goto docomplete;
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (ctrl_x_mode != CTRL_X_FILES)
1292 goto normalchar;
1293 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001294
1295 case 's': /* Spelling completion after ^X */
1296 case Ctrl_S:
1297 if (ctrl_x_mode != CTRL_X_SPELL)
1298 goto normalchar;
1299 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
1301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001302 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303#ifdef FEAT_INS_EXPAND
1304 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1305#endif
1306 {
1307 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1308 if (p_im)
1309 {
1310 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1311 break;
1312 goto doESCkey;
1313 }
1314 goto normalchar;
1315 }
1316#ifdef FEAT_INS_EXPAND
1317 /* FALLTHROUGH */
1318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001319 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 case Ctrl_N:
1321 /* if 'complete' is empty then plain ^P is no longer special,
1322 * but it is under other ^X modes */
1323 if (*curbuf->b_p_cpt == NUL
1324 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 goto normalchar;
1327
1328docomplete:
1329 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001330 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 break;
1332#endif /* FEAT_INS_EXPAND */
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case Ctrl_Y: /* copy from previous line or scroll down */
1335 case Ctrl_E: /* copy from next line or scroll up */
1336 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 break;
1338
1339 default:
1340#ifdef UNIX
1341 if (c == intr_char) /* special interrupt char */
1342 goto do_intr;
1343#endif
1344
1345 /*
1346 * Insert a nomal character.
1347 */
1348normalchar:
1349#ifdef FEAT_SMARTINDENT
1350 /* Try to perform smart-indenting. */
1351 ins_try_si(c);
1352#endif
1353
1354 if (c == ' ')
1355 {
1356 inserted_space = TRUE;
1357#ifdef FEAT_CINDENT
1358 if (inindent(0))
1359 can_cindent = FALSE;
1360#endif
1361 if (Insstart_blank_vcol == MAXCOL
1362 && curwin->w_cursor.lnum == Insstart.lnum)
1363 Insstart_blank_vcol = get_nolist_virtcol();
1364 }
1365
1366 if (vim_iswordc(c) || !echeck_abbr(
1367#ifdef FEAT_MBYTE
1368 /* Add ABBR_OFF for characters above 0x100, this is
1369 * what check_abbr() expects. */
1370 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1371#endif
1372 c))
1373 {
1374 insert_special(c, FALSE, FALSE);
1375#ifdef FEAT_RIGHTLEFT
1376 revins_legal++;
1377 revins_chars++;
1378#endif
1379 }
1380
1381 auto_format(FALSE, TRUE);
1382
1383#ifdef FEAT_FOLDING
1384 /* When inserting a character the cursor line must never be in a
1385 * closed fold. */
1386 foldOpenCursor();
1387#endif
1388 break;
1389 } /* end of switch (c) */
1390
1391 /* If the cursor was moved we didn't just insert a space */
1392 if (arrow_used)
1393 inserted_space = FALSE;
1394
1395#ifdef FEAT_CINDENT
1396 if (can_cindent && cindent_on()
1397# ifdef FEAT_INS_EXPAND
1398 && ctrl_x_mode == 0
1399# endif
1400 )
1401 {
1402force_cindent:
1403 /*
1404 * Indent now if a key was typed that is in 'cinkeys'.
1405 */
1406 if (in_cinkeys(c, ' ', line_is_white))
1407 {
1408 if (stop_arrow() == OK)
1409 /* re-indent the current line */
1410 do_c_expr_indent();
1411 }
1412 }
1413#endif /* FEAT_CINDENT */
1414
1415 } /* for (;;) */
1416 /* NOTREACHED */
1417}
1418
1419/*
1420 * Redraw for Insert mode.
1421 * This is postponed until getting the next character to make '$' in the 'cpo'
1422 * option work correctly.
1423 * Only redraw when there are no characters available. This speeds up
1424 * inserting sequences of characters (e.g., for CTRL-R).
1425 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001426/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001428ins_redraw(ready)
1429 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 if (!char_avail())
1432 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001433#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001434 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1435 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001436 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001437 && !equalpos(last_cursormoved, curwin->w_cursor)
1438# ifdef FEAT_INS_EXPAND
1439 && !pum_visible()
1440# endif
1441 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001442 {
1443 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1444 last_cursormoved = curwin->w_cursor;
1445 }
1446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (must_redraw)
1448 update_screen(0);
1449 else if (clear_cmdline || redraw_cmdline)
1450 showmode(); /* clear cmdline and show mode */
1451 showruler(FALSE);
1452 setcursor();
1453 emsg_on_display = FALSE; /* may remove error message now */
1454 }
1455}
1456
1457/*
1458 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1459 */
1460 static void
1461ins_ctrl_v()
1462{
1463 int c;
1464
1465 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001466 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
1468 if (redrawing() && !char_avail())
1469 edit_putchar('^', TRUE);
1470 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1471
1472#ifdef FEAT_CMDL_INFO
1473 add_to_showcmd_c(Ctrl_V);
1474#endif
1475
1476 c = get_literal();
1477#ifdef FEAT_CMDL_INFO
1478 clear_showcmd();
1479#endif
1480 insert_special(c, FALSE, TRUE);
1481#ifdef FEAT_RIGHTLEFT
1482 revins_chars++;
1483 revins_legal++;
1484#endif
1485}
1486
1487/*
1488 * Put a character directly onto the screen. It's not stored in a buffer.
1489 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1490 */
1491static int pc_status;
1492#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1493#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1494#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1495#define PC_STATUS_SET 3 /* pc_bytes was filled */
1496#ifdef FEAT_MBYTE
1497static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1498#else
1499static char_u pc_bytes[2]; /* saved bytes */
1500#endif
1501static int pc_attr;
1502static int pc_row;
1503static int pc_col;
1504
1505 void
1506edit_putchar(c, highlight)
1507 int c;
1508 int highlight;
1509{
1510 int attr;
1511
1512 if (ScreenLines != NULL)
1513 {
1514 update_topline(); /* just in case w_topline isn't valid */
1515 validate_cursor();
1516 if (highlight)
1517 attr = hl_attr(HLF_8);
1518 else
1519 attr = 0;
1520 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1521 pc_col = W_WINCOL(curwin);
1522#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1523 pc_status = PC_STATUS_UNSET;
1524#endif
1525#ifdef FEAT_RIGHTLEFT
1526 if (curwin->w_p_rl)
1527 {
1528 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1529# ifdef FEAT_MBYTE
1530 if (has_mbyte)
1531 {
1532 int fix_col = mb_fix_col(pc_col, pc_row);
1533
1534 if (fix_col != pc_col)
1535 {
1536 screen_putchar(' ', pc_row, fix_col, attr);
1537 --curwin->w_wcol;
1538 pc_status = PC_STATUS_RIGHT;
1539 }
1540 }
1541# endif
1542 }
1543 else
1544#endif
1545 {
1546 pc_col += curwin->w_wcol;
1547#ifdef FEAT_MBYTE
1548 if (mb_lefthalve(pc_row, pc_col))
1549 pc_status = PC_STATUS_LEFT;
1550#endif
1551 }
1552
1553 /* save the character to be able to put it back */
1554#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1555 if (pc_status == PC_STATUS_UNSET)
1556#endif
1557 {
1558 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1559 pc_status = PC_STATUS_SET;
1560 }
1561 screen_putchar(c, pc_row, pc_col, attr);
1562 }
1563}
1564
1565/*
1566 * Undo the previous edit_putchar().
1567 */
1568 void
1569edit_unputchar()
1570{
1571 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1572 {
1573#if defined(FEAT_MBYTE)
1574 if (pc_status == PC_STATUS_RIGHT)
1575 ++curwin->w_wcol;
1576 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1577 redrawWinline(curwin->w_cursor.lnum, FALSE);
1578 else
1579#endif
1580 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1581 }
1582}
1583
1584/*
1585 * Called when p_dollar is set: display a '$' at the end of the changed text
1586 * Only works when cursor is in the line that changes.
1587 */
1588 void
1589display_dollar(col)
1590 colnr_T col;
1591{
1592 colnr_T save_col;
1593
1594 if (!redrawing())
1595 return;
1596
1597 cursor_off();
1598 save_col = curwin->w_cursor.col;
1599 curwin->w_cursor.col = col;
1600#ifdef FEAT_MBYTE
1601 if (has_mbyte)
1602 {
1603 char_u *p;
1604
1605 /* If on the last byte of a multi-byte move to the first byte. */
1606 p = ml_get_curline();
1607 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1608 }
1609#endif
1610 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1611 if (curwin->w_wcol < W_WIDTH(curwin))
1612 {
1613 edit_putchar('$', FALSE);
1614 dollar_vcol = curwin->w_virtcol;
1615 }
1616 curwin->w_cursor.col = save_col;
1617}
1618
1619/*
1620 * Call this function before moving the cursor from the normal insert position
1621 * in insert mode.
1622 */
1623 static void
1624undisplay_dollar()
1625{
1626 if (dollar_vcol)
1627 {
1628 dollar_vcol = 0;
1629 redrawWinline(curwin->w_cursor.lnum, FALSE);
1630 }
1631}
1632
1633/*
1634 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1635 * Keep the cursor on the same character.
1636 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1637 * type == INDENT_DEC decrease indent (for CTRL-D)
1638 * type == INDENT_SET set indent to "amount"
1639 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1640 */
1641 void
1642change_indent(type, amount, round, replaced)
1643 int type;
1644 int amount;
1645 int round;
1646 int replaced; /* replaced character, put on replace stack */
1647{
1648 int vcol;
1649 int last_vcol;
1650 int insstart_less; /* reduction for Insstart.col */
1651 int new_cursor_col;
1652 int i;
1653 char_u *ptr;
1654 int save_p_list;
1655 int start_col;
1656 colnr_T vc;
1657#ifdef FEAT_VREPLACE
1658 colnr_T orig_col = 0; /* init for GCC */
1659 char_u *new_line, *orig_line = NULL; /* init for GCC */
1660
1661 /* VREPLACE mode needs to know what the line was like before changing */
1662 if (State & VREPLACE_FLAG)
1663 {
1664 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1665 orig_col = curwin->w_cursor.col;
1666 }
1667#endif
1668
1669 /* for the following tricks we don't want list mode */
1670 save_p_list = curwin->w_p_list;
1671 curwin->w_p_list = FALSE;
1672 vc = getvcol_nolist(&curwin->w_cursor);
1673 vcol = vc;
1674
1675 /*
1676 * For Replace mode we need to fix the replace stack later, which is only
1677 * possible when the cursor is in the indent. Remember the number of
1678 * characters before the cursor if it's possible.
1679 */
1680 start_col = curwin->w_cursor.col;
1681
1682 /* determine offset from first non-blank */
1683 new_cursor_col = curwin->w_cursor.col;
1684 beginline(BL_WHITE);
1685 new_cursor_col -= curwin->w_cursor.col;
1686
1687 insstart_less = curwin->w_cursor.col;
1688
1689 /*
1690 * If the cursor is in the indent, compute how many screen columns the
1691 * cursor is to the left of the first non-blank.
1692 */
1693 if (new_cursor_col < 0)
1694 vcol = get_indent() - vcol;
1695
1696 if (new_cursor_col > 0) /* can't fix replace stack */
1697 start_col = -1;
1698
1699 /*
1700 * Set the new indent. The cursor will be put on the first non-blank.
1701 */
1702 if (type == INDENT_SET)
1703 (void)set_indent(amount, SIN_CHANGED);
1704 else
1705 {
1706#ifdef FEAT_VREPLACE
1707 int save_State = State;
1708
1709 /* Avoid being called recursively. */
1710 if (State & VREPLACE_FLAG)
1711 State = INSERT;
1712#endif
1713 shift_line(type == INDENT_DEC, round, 1);
1714#ifdef FEAT_VREPLACE
1715 State = save_State;
1716#endif
1717 }
1718 insstart_less -= curwin->w_cursor.col;
1719
1720 /*
1721 * Try to put cursor on same character.
1722 * If the cursor is at or after the first non-blank in the line,
1723 * compute the cursor column relative to the column of the first
1724 * non-blank character.
1725 * If we are not in insert mode, leave the cursor on the first non-blank.
1726 * If the cursor is before the first non-blank, position it relative
1727 * to the first non-blank, counted in screen columns.
1728 */
1729 if (new_cursor_col >= 0)
1730 {
1731 /*
1732 * When changing the indent while the cursor is touching it, reset
1733 * Insstart_col to 0.
1734 */
1735 if (new_cursor_col == 0)
1736 insstart_less = MAXCOL;
1737 new_cursor_col += curwin->w_cursor.col;
1738 }
1739 else if (!(State & INSERT))
1740 new_cursor_col = curwin->w_cursor.col;
1741 else
1742 {
1743 /*
1744 * Compute the screen column where the cursor should be.
1745 */
1746 vcol = get_indent() - vcol;
1747 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1748
1749 /*
1750 * Advance the cursor until we reach the right screen column.
1751 */
1752 vcol = last_vcol = 0;
1753 new_cursor_col = -1;
1754 ptr = ml_get_curline();
1755 while (vcol <= (int)curwin->w_virtcol)
1756 {
1757 last_vcol = vcol;
1758#ifdef FEAT_MBYTE
1759 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001760 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 else
1762#endif
1763 ++new_cursor_col;
1764 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1765 }
1766 vcol = last_vcol;
1767
1768 /*
1769 * May need to insert spaces to be able to position the cursor on
1770 * the right screen column.
1771 */
1772 if (vcol != (int)curwin->w_virtcol)
1773 {
1774 curwin->w_cursor.col = new_cursor_col;
1775 i = (int)curwin->w_virtcol - vcol;
1776 ptr = alloc(i + 1);
1777 if (ptr != NULL)
1778 {
1779 new_cursor_col += i;
1780 ptr[i] = NUL;
1781 while (--i >= 0)
1782 ptr[i] = ' ';
1783 ins_str(ptr);
1784 vim_free(ptr);
1785 }
1786 }
1787
1788 /*
1789 * When changing the indent while the cursor is in it, reset
1790 * Insstart_col to 0.
1791 */
1792 insstart_less = MAXCOL;
1793 }
1794
1795 curwin->w_p_list = save_p_list;
1796
1797 if (new_cursor_col <= 0)
1798 curwin->w_cursor.col = 0;
1799 else
1800 curwin->w_cursor.col = new_cursor_col;
1801 curwin->w_set_curswant = TRUE;
1802 changed_cline_bef_curs();
1803
1804 /*
1805 * May have to adjust the start of the insert.
1806 */
1807 if (State & INSERT)
1808 {
1809 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1810 {
1811 if ((int)Insstart.col <= insstart_less)
1812 Insstart.col = 0;
1813 else
1814 Insstart.col -= insstart_less;
1815 }
1816 if ((int)ai_col <= insstart_less)
1817 ai_col = 0;
1818 else
1819 ai_col -= insstart_less;
1820 }
1821
1822 /*
1823 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1824 * If the number of characters before the cursor decreased, need to pop a
1825 * few characters from the replace stack.
1826 * If the number of characters before the cursor increased, need to push a
1827 * few NULs onto the replace stack.
1828 */
1829 if (REPLACE_NORMAL(State) && start_col >= 0)
1830 {
1831 while (start_col > (int)curwin->w_cursor.col)
1832 {
1833 replace_join(0); /* remove a NUL from the replace stack */
1834 --start_col;
1835 }
1836 while (start_col < (int)curwin->w_cursor.col || replaced)
1837 {
1838 replace_push(NUL);
1839 if (replaced)
1840 {
1841 replace_push(replaced);
1842 replaced = NUL;
1843 }
1844 ++start_col;
1845 }
1846 }
1847
1848#ifdef FEAT_VREPLACE
1849 /*
1850 * For VREPLACE mode, we also have to fix the replace stack. In this case
1851 * it is always possible because we backspace over the whole line and then
1852 * put it back again the way we wanted it.
1853 */
1854 if (State & VREPLACE_FLAG)
1855 {
1856 /* If orig_line didn't allocate, just return. At least we did the job,
1857 * even if you can't backspace. */
1858 if (orig_line == NULL)
1859 return;
1860
1861 /* Save new line */
1862 new_line = vim_strsave(ml_get_curline());
1863 if (new_line == NULL)
1864 return;
1865
1866 /* We only put back the new line up to the cursor */
1867 new_line[curwin->w_cursor.col] = NUL;
1868
1869 /* Put back original line */
1870 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1871 curwin->w_cursor.col = orig_col;
1872
1873 /* Backspace from cursor to start of line */
1874 backspace_until_column(0);
1875
1876 /* Insert new stuff into line again */
1877 ins_bytes(new_line);
1878
1879 vim_free(new_line);
1880 }
1881#endif
1882}
1883
1884/*
1885 * Truncate the space at the end of a line. This is to be used only in an
1886 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1887 * modes.
1888 */
1889 void
1890truncate_spaces(line)
1891 char_u *line;
1892{
1893 int i;
1894
1895 /* find start of trailing white space */
1896 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1897 {
1898 if (State & REPLACE_FLAG)
1899 replace_join(0); /* remove a NUL from the replace stack */
1900 }
1901 line[i + 1] = NUL;
1902}
1903
1904#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1905 || defined(FEAT_COMMENTS) || defined(PROTO)
1906/*
1907 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1908 * modes correctly. May also be used when not in insert mode at all.
1909 */
1910 void
1911backspace_until_column(col)
1912 int col;
1913{
1914 while ((int)curwin->w_cursor.col > col)
1915 {
1916 curwin->w_cursor.col--;
1917 if (State & REPLACE_FLAG)
1918 replace_do_bs();
1919 else
1920 (void)del_char(FALSE);
1921 }
1922}
1923#endif
1924
1925#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1926/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001927 * CTRL-X pressed in Insert mode.
1928 */
1929 static void
1930ins_ctrl_x()
1931{
1932 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1933 * CTRL-V works like CTRL-N */
1934 if (ctrl_x_mode != CTRL_X_CMDLINE)
1935 {
1936 /* if the next ^X<> won't ADD nothing, then reset
1937 * compl_cont_status */
1938 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001939 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001940 else
1941 compl_cont_status = 0;
1942 /* We're not sure which CTRL-X mode it will be yet */
1943 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1944 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1945 edit_submode_pre = NULL;
1946 showmode();
1947 }
1948}
1949
1950/*
1951 * Return TRUE if the 'dict' or 'tsr' option can be used.
1952 */
1953 static int
1954has_compl_option(dict_opt)
1955 int dict_opt;
1956{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001957 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001958# ifdef FEAT_SPELL
1959 && !curwin->w_p_spell
1960# endif
1961 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001962 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1963 {
1964 ctrl_x_mode = 0;
1965 edit_submode = NULL;
1966 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1967 : (char_u *)_("'thesaurus' option is empty"),
1968 hl_attr(HLF_E));
1969 if (emsg_silent == 0)
1970 {
1971 vim_beep();
1972 setcursor();
1973 out_flush();
1974 ui_delay(2000L, FALSE);
1975 }
1976 return FALSE;
1977 }
1978 return TRUE;
1979}
1980
1981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1983 * This depends on the current mode.
1984 */
1985 int
1986vim_is_ctrl_x_key(c)
1987 int c;
1988{
1989 /* Always allow ^R - let it's results then be checked */
1990 if (c == Ctrl_R)
1991 return TRUE;
1992
Bram Moolenaare3226be2005-12-18 22:10:00 +00001993 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001995 return TRUE;
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 switch (ctrl_x_mode)
1998 {
1999 case 0: /* Not in any CTRL-X mode */
2000 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2001 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002002 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2004 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2005 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002006 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2007 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 case CTRL_X_SCROLL:
2009 return (c == Ctrl_Y || c == Ctrl_E);
2010 case CTRL_X_WHOLE_LINE:
2011 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2012 case CTRL_X_FILES:
2013 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2014 case CTRL_X_DICTIONARY:
2015 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2016 case CTRL_X_THESAURUS:
2017 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2018 case CTRL_X_TAGS:
2019 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2020#ifdef FEAT_FIND_ID
2021 case CTRL_X_PATH_PATTERNS:
2022 return (c == Ctrl_P || c == Ctrl_N);
2023 case CTRL_X_PATH_DEFINES:
2024 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2025#endif
2026 case CTRL_X_CMDLINE:
2027 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2028 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002029#ifdef FEAT_COMPL_FUNC
2030 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002031 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002032 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002034#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002035 case CTRL_X_SPELL:
2036 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 EMSG(_(e_internal));
2039 return FALSE;
2040}
2041
2042/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002043 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * case of the originally typed text is used, and the case of the completed
2045 * text is infered, ie this tries to work out what case you probably wanted
2046 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002047 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 */
2049 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002050ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 char_u *str;
2052 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002053 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 char_u *fname;
2055 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002056 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int has_lower = FALSE;
2059 int was_letter = FALSE;
2060 int idx;
2061
2062 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2063 {
2064 /* Infer case of completed part -- webb */
2065 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002066 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002069 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002071 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 has_lower = TRUE;
2074 if (isupper(IObuff[idx]))
2075 {
2076 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002077 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2079 break;
2080 }
2081 }
2082 }
2083
2084 /*
2085 * Rule 2: No lower case, 2nd consecutive letter converted to
2086 * upper case.
2087 */
2088 if (!has_lower)
2089 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002090 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002092 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 && islower(IObuff[idx]))
2094 {
2095 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002096 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2098 break;
2099 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002100 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 }
2103
2104 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002107 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2108 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002110 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113/*
2114 * Add a match to the list of matches.
2115 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002116 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002117 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002119 static int
2120ins_compl_add(str, len, icase, fname, cptext, cdir, flags, dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 char_u *str;
2122 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002123 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002125 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002126 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002127 int flags;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002128 int dup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002130 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 ui_breakcheck();
2134 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (len < 0)
2137 len = (int)STRLEN(str);
2138
2139 /*
2140 * If the same match is already present, don't add it.
2141 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002142 if (compl_first_match != NULL && !dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002144 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 do
2146 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002148 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002149 && match->cp_str[len] == NUL)
2150 return NOTDONE;
2151 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002152 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002155 /* Remove any popup menu before changing the list of matches. */
2156 ins_compl_del_pum();
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /*
2159 * Allocate a new match structure.
2160 * Copy the values to the new match structure.
2161 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002162 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002164 return FAIL;
2165 match->cp_number = -1;
2166 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002167 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002168 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002173 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002176 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2178 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002179 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002180 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002181 && compl_curr_match->cp_fname != NULL
2182 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002183 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002184 else if (fname != NULL)
2185 {
2186 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002187 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002190 match->cp_fname = NULL;
2191 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002192
2193 if (cptext != NULL)
2194 {
2195 int i;
2196
2197 for (i = 0; i < CPT_COUNT; ++i)
2198 if (cptext[i] != NULL && *cptext[i] != NUL)
2199 match->cp_text[i] = vim_strsave(cptext[i]);
2200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 /*
2203 * Link the new match structure in the list of matches.
2204 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002205 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002206 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 else if (dir == FORWARD)
2208 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002209 match->cp_next = compl_curr_match->cp_next;
2210 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 else /* BACKWARD */
2213 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002214 match->cp_next = compl_curr_match;
2215 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002217 if (match->cp_next)
2218 match->cp_next->cp_prev = match;
2219 if (match->cp_prev)
2220 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002222 compl_first_match = match;
2223 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002225 /*
2226 * Find the longest common string if still doing that.
2227 */
2228 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2229 ins_compl_longest_match(match);
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return OK;
2232}
2233
2234/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002235 * Return TRUE if "str[len]" matches with match->cp_str, considering
2236 * match->cp_icase.
2237 */
2238 static int
2239ins_compl_equal(match, str, len)
2240 compl_T *match;
2241 char_u *str;
2242 int len;
2243{
2244 if (match->cp_icase)
2245 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2246 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2247}
2248
2249/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002250 * Reduce the longest common string for match "match".
2251 */
2252 static void
2253ins_compl_longest_match(match)
2254 compl_T *match;
2255{
2256 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002257 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002258 int had_match;
2259
2260 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002261 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002262 /* First match, use it as a whole. */
2263 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002264 if (compl_leader != NULL)
2265 {
2266 had_match = (curwin->w_cursor.col > compl_col);
2267 ins_compl_delete();
2268 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2269 ins_redraw(FALSE);
2270
2271 /* When the match isn't there (to avoid matching itself) remove it
2272 * again after redrawing. */
2273 if (!had_match)
2274 ins_compl_delete();
2275 compl_used_match = FALSE;
2276 }
2277 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002278 else
2279 {
2280 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002281 p = compl_leader;
2282 s = match->cp_str;
2283 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002284 {
2285#ifdef FEAT_MBYTE
2286 if (has_mbyte)
2287 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002288 c1 = mb_ptr2char(p);
2289 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002290 }
2291 else
2292#endif
2293 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002294 c1 = *p;
2295 c2 = *s;
2296 }
2297 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2298 : (c1 != c2))
2299 break;
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
2302 {
2303 mb_ptr_adv(p);
2304 mb_ptr_adv(s);
2305 }
2306 else
2307#endif
2308 {
2309 ++p;
2310 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002311 }
2312 }
2313
2314 if (*p != NUL)
2315 {
2316 /* Leader was shortened, need to change the inserted text. */
2317 *p = NUL;
2318 had_match = (curwin->w_cursor.col > compl_col);
2319 ins_compl_delete();
2320 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2321 ins_redraw(FALSE);
2322
2323 /* When the match isn't there (to avoid matching itself) remove it
2324 * again after redrawing. */
2325 if (!had_match)
2326 ins_compl_delete();
2327 }
2328
2329 compl_used_match = FALSE;
2330 }
2331}
2332
2333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * Add an array of matches to the list of matches.
2335 * Frees matches[].
2336 */
2337 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002338ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 int num_matches;
2340 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002341 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 int i;
2344 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002345 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaar572cb562005-08-05 21:35:02 +00002347 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002348 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002349 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002351 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 FreeWild(num_matches, matches);
2353}
2354
2355/* Make the completion list cyclic.
2356 * Return the number of matches (excluding the original).
2357 */
2358 static int
2359ins_compl_make_cyclic()
2360{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002361 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 int count = 0;
2363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002364 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 /*
2367 * Find the end of the list.
2368 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002369 match = compl_first_match;
2370 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002371 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002373 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 ++count;
2375 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002376 match->cp_next = compl_first_match;
2377 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 return count;
2380}
2381
Bram Moolenaara94bc432006-03-10 21:42:59 +00002382/*
2383 * Start completion for the complete() function.
2384 * "startcol" is where the matched text starts (1 is first column).
2385 * "list" is the list of matches.
2386 */
2387 void
2388set_completion(startcol, list)
2389 int startcol;
2390 list_T *list;
2391{
2392 /* If already doing completions stop it. */
2393 if (ctrl_x_mode != 0)
2394 ins_compl_prep(' ');
2395 ins_compl_clear();
2396
2397 if (stop_arrow() == FAIL)
2398 return;
2399
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002400 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002401 startcol = curwin->w_cursor.col;
2402 compl_col = startcol;
2403 compl_length = curwin->w_cursor.col - startcol;
2404 /* compl_pattern doesn't need to be set */
2405 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2406 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002407 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002408 return;
2409
2410 /* Handle like dictionary completion. */
2411 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2412
2413 ins_compl_add_list(list);
2414 compl_matches = ins_compl_make_cyclic();
2415 compl_started = TRUE;
2416 compl_used_match = TRUE;
2417
2418 compl_curr_match = compl_first_match;
2419 ins_complete(Ctrl_N);
2420 out_flush();
2421}
2422
2423
Bram Moolenaar9372a112005-12-06 19:59:18 +00002424/* "compl_match_array" points the currently displayed list of entries in the
2425 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002426static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002427static int compl_match_arraysize;
2428
2429/*
2430 * Update the screen and when there is any scrolling remove the popup menu.
2431 */
2432 static void
2433ins_compl_upd_pum()
2434{
2435 int h;
2436
2437 if (compl_match_array != NULL)
2438 {
2439 h = curwin->w_cline_height;
2440 update_screen(0);
2441 if (h != curwin->w_cline_height)
2442 ins_compl_del_pum();
2443 }
2444}
2445
2446/*
2447 * Remove any popup menu.
2448 */
2449 static void
2450ins_compl_del_pum()
2451{
2452 if (compl_match_array != NULL)
2453 {
2454 pum_undisplay();
2455 vim_free(compl_match_array);
2456 compl_match_array = NULL;
2457 }
2458}
2459
2460/*
2461 * Return TRUE if the popup menu should be displayed.
2462 */
2463 static int
2464pum_wanted()
2465{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002466 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002467 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002468 return FALSE;
2469
2470 /* The display looks bad on a B&W display. */
2471 if (t_colors < 8
2472#ifdef FEAT_GUI
2473 && !gui.in_use
2474#endif
2475 )
2476 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002477 return TRUE;
2478}
2479
2480/*
2481 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002482 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002483 */
2484 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002485pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002486{
2487 compl_T *compl;
2488 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002489
2490 /* Don't display the popup menu if there are no matches or there is only
2491 * one (ignoring the original text). */
2492 compl = compl_first_match;
2493 i = 0;
2494 do
2495 {
2496 if (compl == NULL
2497 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2498 break;
2499 compl = compl->cp_next;
2500 } while (compl != compl_first_match);
2501
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002502 if (strstr((char *)p_cot, "menuone") != NULL)
2503 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002504 return (i >= 2);
2505}
2506
2507/*
2508 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002509 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002511 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002512ins_compl_show_pum()
2513{
2514 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002515 compl_T *shown_compl = NULL;
2516 int did_find_shown_match = FALSE;
2517 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002518 int i;
2519 int cur = -1;
2520 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002521 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002522
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002523 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002524 return;
2525
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002526#if defined(FEAT_EVAL)
2527 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2528 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2529#endif
2530
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002531 /* Update the screen before drawing the popup menu over it. */
2532 update_screen(0);
2533
2534 if (compl_match_array == NULL)
2535 {
2536 /* Need to build the popup menu list. */
2537 compl_match_arraysize = 0;
2538 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002539 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002540 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002541 do
2542 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002543 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2544 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002546 ++compl_match_arraysize;
2547 compl = compl->cp_next;
2548 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002549 if (compl_match_arraysize == 0)
2550 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002551 compl_match_array = (pumitem_T *)alloc_clear(
2552 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002553 * compl_match_arraysize));
2554 if (compl_match_array != NULL)
2555 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002556 /* If the current match is the original text don't find the first
2557 * match after it, don't highlight anything. */
2558 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2559 shown_match_ok = TRUE;
2560
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002561 i = 0;
2562 compl = compl_first_match;
2563 do
2564 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002565 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2566 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002568 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002569 if (!shown_match_ok)
2570 {
2571 if (compl == compl_shown_match || did_find_shown_match)
2572 {
2573 /* This item is the shown match or this is the
2574 * first displayed item after the shown match. */
2575 compl_shown_match = compl;
2576 did_find_shown_match = TRUE;
2577 shown_match_ok = TRUE;
2578 }
2579 else
2580 /* Remember this displayed match for when the
2581 * shown match is just below it. */
2582 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002583 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002584 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002585
2586 if (compl->cp_text[CPT_ABBR] != NULL)
2587 compl_match_array[i].pum_text =
2588 compl->cp_text[CPT_ABBR];
2589 else
2590 compl_match_array[i].pum_text = compl->cp_str;
2591 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2592 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2593 if (compl->cp_text[CPT_MENU] != NULL)
2594 compl_match_array[i++].pum_extra =
2595 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002596 else
2597 compl_match_array[i++].pum_extra = compl->cp_fname;
2598 }
2599
2600 if (compl == compl_shown_match)
2601 {
2602 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002603
2604 /* When the original text is the shown match don't set
2605 * compl_shown_match. */
2606 if (compl->cp_flags & ORIGINAL_TEXT)
2607 shown_match_ok = TRUE;
2608
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 if (!shown_match_ok && shown_compl != NULL)
2610 {
2611 /* The shown match isn't displayed, set it to the
2612 * previously displayed match. */
2613 compl_shown_match = shown_compl;
2614 shown_match_ok = TRUE;
2615 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002616 }
2617 compl = compl->cp_next;
2618 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002619
2620 if (!shown_match_ok) /* no displayed match at all */
2621 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002622 }
2623 }
2624 else
2625 {
2626 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002627 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002628 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2629 || compl_match_array[i].pum_text
2630 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002631 {
2632 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002633 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002634 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002635 }
2636
2637 if (compl_match_array != NULL)
2638 {
2639 /* Compute the screen column of the start of the completed text.
2640 * Use the cursor to get all wrapping and other settings right. */
2641 col = curwin->w_cursor.col;
2642 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002643 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002644 curwin->w_cursor.col = col;
2645 }
2646}
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#define DICT_FIRST (1) /* use just first element in "dict" */
2649#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002652 * Add any identifiers that match the given pattern in the list of dictionary
2653 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 */
2655 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002656ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2657 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002659 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002660 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002662 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 char_u *ptr;
2664 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 char_u **files;
2667 int count;
2668 int i;
2669 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002670 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
Bram Moolenaar0b238792006-03-02 22:49:12 +00002672 if (*dict == NUL)
2673 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002674#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002675 /* When 'dictionary' is empty and spell checking is enabled use
2676 * "spell". */
2677 if (!thesaurus && curwin->w_p_spell)
2678 dict = (char_u *)"spell";
2679 else
2680#endif
2681 return;
2682 }
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002685 if (buf == NULL)
2686 return;
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /* If 'infercase' is set, don't use 'smartcase' here */
2689 save_p_scs = p_scs;
2690 if (curbuf->b_p_inf)
2691 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002692
2693 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2694 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002695 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002696 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2697 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002698 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2699
2700 if (pat_esc == NULL)
2701 return ;
2702 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002703 ptr = alloc(i);
2704 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002705 {
2706 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002707 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002708 }
2709 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2710 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2711 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002712 vim_free(ptr);
2713 }
2714 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002715 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002716 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002717 if (regmatch.regprog == NULL)
2718 goto theend;
2719 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2722 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002723 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 /* copy one dictionary file name into buf */
2726 if (flags == DICT_EXACT)
2727 {
2728 count = 1;
2729 files = &dict;
2730 }
2731 else
2732 {
2733 /* Expand wildcards in the dictionary name, but do not allow
2734 * backticks (for security, the 'dict' option may have been set in
2735 * a modeline). */
2736 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002737# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002738 if (!thesaurus && STRCMP(buf, "spell") == 0)
2739 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002740 else
2741# endif
2742 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 || expand_wildcards(1, &buf, &count, &files,
2744 EW_FILE|EW_SILENT) != OK)
2745 count = 0;
2746 }
2747
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002748# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002749 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002751 /* Complete from active spelling. Skip "\<" in the pattern, we
2752 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002753 if (pat[0] == '\\' && pat[1] == '<')
2754 ptr = pat + 2;
2755 else
2756 ptr = pat;
2757 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002759 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002760# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002761 {
2762 ins_compl_files(count, files, thesaurus, flags,
2763 &regmatch, buf, &dir);
2764 if (flags != DICT_EXACT)
2765 FreeWild(count, files);
2766 }
2767 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 break;
2769 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002770
2771theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 p_scs = save_p_scs;
2773 vim_free(regmatch.regprog);
2774 vim_free(buf);
2775}
2776
Bram Moolenaar0b238792006-03-02 22:49:12 +00002777 static void
2778ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2779 int count;
2780 char_u **files;
2781 int thesaurus;
2782 int flags;
2783 regmatch_T *regmatch;
2784 char_u *buf;
2785 int *dir;
2786{
2787 char_u *ptr;
2788 int i;
2789 FILE *fp;
2790 int add_r;
2791
2792 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2793 {
2794 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2795 if (flags != DICT_EXACT)
2796 {
2797 vim_snprintf((char *)IObuff, IOSIZE,
2798 _("Scanning dictionary: %s"), (char *)files[i]);
2799 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2800 }
2801
2802 if (fp != NULL)
2803 {
2804 /*
2805 * Read dictionary file line by line.
2806 * Check each line for a match.
2807 */
2808 while (!got_int && !compl_interrupted
2809 && !vim_fgets(buf, LSIZE, fp))
2810 {
2811 ptr = buf;
2812 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2813 {
2814 ptr = regmatch->startp[0];
2815 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2816 ptr = find_line_end(ptr);
2817 else
2818 ptr = find_word_end(ptr);
2819 add_r = ins_compl_add_infercase(regmatch->startp[0],
2820 (int)(ptr - regmatch->startp[0]),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002821 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002822 if (thesaurus)
2823 {
2824 char_u *wstart;
2825
2826 /*
2827 * Add the other matches on the line
2828 */
2829 while (!got_int)
2830 {
2831 /* Find start of the next word. Skip white
2832 * space and punctuation. */
2833 ptr = find_word_start(ptr);
2834 if (*ptr == NUL || *ptr == NL)
2835 break;
2836 wstart = ptr;
2837
2838 /* Find end of the word and add it. */
2839#ifdef FEAT_MBYTE
2840 if (has_mbyte)
2841 /* Japanese words may have characters in
2842 * different classes, only separate words
2843 * with single-byte non-word characters. */
2844 while (*ptr != NUL)
2845 {
2846 int l = (*mb_ptr2len)(ptr);
2847
2848 if (l < 2 && !vim_iswordc(*ptr))
2849 break;
2850 ptr += l;
2851 }
2852 else
2853#endif
2854 ptr = find_word_end(ptr);
2855 add_r = ins_compl_add_infercase(wstart,
2856 (int)(ptr - wstart),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002857 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002858 }
2859 }
2860 if (add_r == OK)
2861 /* if dir was BACKWARD then honor it just once */
2862 *dir = FORWARD;
2863 else if (add_r == FAIL)
2864 break;
2865 /* avoid expensive call to vim_regexec() when at end
2866 * of line */
2867 if (*ptr == '\n' || got_int)
2868 break;
2869 }
2870 line_breakcheck();
2871 ins_compl_check_keys(50);
2872 }
2873 fclose(fp);
2874 }
2875 }
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * Find the start of the next word.
2880 * Returns a pointer to the first char of the word. Also stops at a NUL.
2881 */
2882 char_u *
2883find_word_start(ptr)
2884 char_u *ptr;
2885{
2886#ifdef FEAT_MBYTE
2887 if (has_mbyte)
2888 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002889 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 else
2891#endif
2892 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2893 ++ptr;
2894 return ptr;
2895}
2896
2897/*
2898 * Find the end of the word. Assumes it starts inside a word.
2899 * Returns a pointer to just after the word.
2900 */
2901 char_u *
2902find_word_end(ptr)
2903 char_u *ptr;
2904{
2905#ifdef FEAT_MBYTE
2906 int start_class;
2907
2908 if (has_mbyte)
2909 {
2910 start_class = mb_get_class(ptr);
2911 if (start_class > 1)
2912 while (*ptr != NUL)
2913 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002914 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (mb_get_class(ptr) != start_class)
2916 break;
2917 }
2918 }
2919 else
2920#endif
2921 while (vim_iswordc(*ptr))
2922 ++ptr;
2923 return ptr;
2924}
2925
2926/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002927 * Find the end of the line, omitting CR and NL at the end.
2928 * Returns a pointer to just after the line.
2929 */
2930 static char_u *
2931find_line_end(ptr)
2932 char_u *ptr;
2933{
2934 char_u *s;
2935
2936 s = ptr + STRLEN(ptr);
2937 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2938 --s;
2939 return s;
2940}
2941
2942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 * Free the list of completions
2944 */
2945 static void
2946ins_compl_free()
2947{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002948 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002949 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002951 vim_free(compl_pattern);
2952 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002953 vim_free(compl_leader);
2954 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002956 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002958
2959 ins_compl_del_pum();
2960 pum_clear();
2961
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002962 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 do
2964 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002965 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002966 compl_curr_match = compl_curr_match->cp_next;
2967 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002969 if (match->cp_flags & FREE_FNAME)
2970 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002971 for (i = 0; i < CPT_COUNT; ++i)
2972 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002974 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2975 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978 static void
2979ins_compl_clear()
2980{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002981 compl_cont_status = 0;
2982 compl_started = FALSE;
2983 compl_matches = 0;
2984 vim_free(compl_pattern);
2985 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002986 vim_free(compl_leader);
2987 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002989 vim_free(compl_orig_text);
2990 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002991 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993
2994/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002995 * Return TRUE when Insert completion is active.
2996 */
2997 int
2998ins_compl_active()
2999{
3000 return compl_started;
3001}
3002
3003/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003004 * Delete one character before the cursor and show the subset of the matches
3005 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003006 * Returns the character to be used, NUL if the work is done and another char
3007 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003008 */
3009 static int
3010ins_compl_bs()
3011{
3012 char_u *line;
3013 char_u *p;
3014
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003015 line = ml_get_curline();
3016 p = line + curwin->w_cursor.col;
3017 mb_ptr_back(line, p);
3018
3019 /* Stop completion when the whole word was deleted. */
3020 if ((int)(p - line) - (int)compl_col <= 0)
3021 return K_BS;
3022
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003023 /* Deleted more than what was used to find matches or didn't finish
3024 * finding all matches: need to look for matches all over again. */
3025 if (curwin->w_cursor.col <= compl_col + compl_length
3026 || compl_was_interrupted)
3027 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003028
Bram Moolenaara6557602006-02-04 22:43:20 +00003029 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003030 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003031 if (compl_leader != NULL)
3032 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003033 ins_compl_new_leader();
3034 return NUL;
3035 }
3036 return K_BS;
3037}
Bram Moolenaara6557602006-02-04 22:43:20 +00003038
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003039/*
3040 * Called after changing "compl_leader".
3041 * Show the popup menu with a different set of matches.
3042 * May also search for matches again if the previous search was interrupted.
3043 */
3044 static void
3045ins_compl_new_leader()
3046{
3047 ins_compl_del_pum();
3048 ins_compl_delete();
3049 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3050 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003051
3052 if (compl_started)
3053 ins_compl_set_original_text(compl_leader);
3054 else
3055 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003056#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003057 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003058#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003059 /*
3060 * Matches were cleared, need to search for them now. First display
3061 * the changed text before the cursor. Set "compl_restarting" to
3062 * avoid that the first match is inserted.
3063 */
3064 update_screen(0);
3065#ifdef FEAT_GUI
3066 if (gui.in_use)
3067 {
3068 /* Show the cursor after the match, not after the redrawn text. */
3069 setcursor();
3070 out_flush();
3071 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003072 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003073#endif
3074 compl_restarting = TRUE;
3075 if (ins_complete(Ctrl_N) == FAIL)
3076 compl_cont_status = 0;
3077 compl_restarting = FALSE;
3078 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003079
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003080#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003081 if (!compl_used_match)
3082 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003083 /* Go to the original text, since none of the matches is inserted. */
3084 if (compl_first_match->cp_prev != NULL
3085 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3086 compl_shown_match = compl_first_match->cp_prev;
3087 else
3088 compl_shown_match = compl_first_match;
3089 compl_curr_match = compl_shown_match;
3090 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003091 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003092#endif
3093 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003094
3095 /* Show the popup menu with a different set of matches. */
3096 ins_compl_show_pum();
Bram Moolenaara6557602006-02-04 22:43:20 +00003097}
3098
3099/*
3100 * Append one character to the match leader. May reduce the number of
3101 * matches.
3102 */
3103 static void
3104ins_compl_addleader(c)
3105 int c;
3106{
3107#ifdef FEAT_MBYTE
3108 int cc;
3109
3110 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3111 {
3112 char_u buf[MB_MAXBYTES + 1];
3113
3114 (*mb_char2bytes)(c, buf);
3115 buf[cc] = NUL;
3116 ins_char_bytes(buf, cc);
3117 }
3118 else
3119#endif
3120 ins_char(c);
3121
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003122 /* If we didn't complete finding matches we must search again. */
3123 if (compl_was_interrupted)
3124 ins_compl_restart();
3125
Bram Moolenaara6557602006-02-04 22:43:20 +00003126 vim_free(compl_leader);
3127 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3128 curwin->w_cursor.col - compl_col);
3129 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003130 ins_compl_new_leader();
3131}
3132
3133/*
3134 * Setup for finding completions again without leaving CTRL-X mode. Used when
3135 * BS or a key was typed while still searching for matches.
3136 */
3137 static void
3138ins_compl_restart()
3139{
3140 ins_compl_free();
3141 compl_started = FALSE;
3142 compl_matches = 0;
3143 compl_cont_status = 0;
3144 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003145}
3146
3147/*
3148 * Set the first match, the original text.
3149 */
3150 static void
3151ins_compl_set_original_text(str)
3152 char_u *str;
3153{
3154 char_u *p;
3155
3156 /* Replace the original text entry. */
3157 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3158 {
3159 p = vim_strsave(str);
3160 if (p != NULL)
3161 {
3162 vim_free(compl_first_match->cp_str);
3163 compl_first_match->cp_str = p;
3164 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003165 }
3166}
3167
3168/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003169 * Append one character to the match leader. May reduce the number of
3170 * matches.
3171 */
3172 static void
3173ins_compl_addfrommatch()
3174{
3175 char_u *p;
3176 int len = curwin->w_cursor.col - compl_col;
3177 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003178 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003179
3180 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003181 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003182 {
3183 /* When still at the original match use the first entry that matches
3184 * the leader. */
3185 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3186 {
3187 p = NULL;
3188 for (cp = compl_shown_match->cp_next; cp != NULL
3189 && cp != compl_first_match; cp = cp->cp_next)
3190 {
3191 if (ins_compl_equal(cp, compl_leader,
3192 (int)STRLEN(compl_leader)))
3193 {
3194 p = cp->cp_str;
3195 break;
3196 }
3197 }
3198 if (p == NULL || (int)STRLEN(p) <= len)
3199 return;
3200 }
3201 else
3202 return;
3203 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003204 p += len;
3205#ifdef FEAT_MBYTE
3206 c = mb_ptr2char(p);
3207#else
3208 c = *p;
3209#endif
3210 ins_compl_addleader(c);
3211}
3212
3213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003215 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003216 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219ins_compl_prep(c)
3220 int c;
3221{
3222 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 int temp;
3224 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003225 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
3227 /* Forget any previous 'special' messages if this is actually
3228 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3229 */
3230 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3231 edit_submode_extra = NULL;
3232
3233 /* Ignore end of Select mode mapping */
3234 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003235 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003237 /* Set "compl_get_longest" when finding the first matches. */
3238 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3239 || (ctrl_x_mode == 0 && !compl_started))
3240 {
3241 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3242 compl_used_match = TRUE;
3243 }
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3246 {
3247 /*
3248 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3249 * it will be yet. Now we decide.
3250 */
3251 switch (c)
3252 {
3253 case Ctrl_E:
3254 case Ctrl_Y:
3255 ctrl_x_mode = CTRL_X_SCROLL;
3256 if (!(State & REPLACE_FLAG))
3257 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3258 else
3259 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3260 edit_submode_pre = NULL;
3261 showmode();
3262 break;
3263 case Ctrl_L:
3264 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3265 break;
3266 case Ctrl_F:
3267 ctrl_x_mode = CTRL_X_FILES;
3268 break;
3269 case Ctrl_K:
3270 ctrl_x_mode = CTRL_X_DICTIONARY;
3271 break;
3272 case Ctrl_R:
3273 /* Simply allow ^R to happen without affecting ^X mode */
3274 break;
3275 case Ctrl_T:
3276 ctrl_x_mode = CTRL_X_THESAURUS;
3277 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003278#ifdef FEAT_COMPL_FUNC
3279 case Ctrl_U:
3280 ctrl_x_mode = CTRL_X_FUNCTION;
3281 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003282 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003283 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003284 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003285#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003286 case 's':
3287 case Ctrl_S:
3288 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003289#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003291 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003292 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003293#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 case Ctrl_RSB:
3296 ctrl_x_mode = CTRL_X_TAGS;
3297 break;
3298#ifdef FEAT_FIND_ID
3299 case Ctrl_I:
3300 case K_S_TAB:
3301 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3302 break;
3303 case Ctrl_D:
3304 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3305 break;
3306#endif
3307 case Ctrl_V:
3308 case Ctrl_Q:
3309 ctrl_x_mode = CTRL_X_CMDLINE;
3310 break;
3311 case Ctrl_P:
3312 case Ctrl_N:
3313 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3314 * just started ^X mode, or there were enough ^X's to cancel
3315 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3316 * do normal expansion when interrupting a different mode (say
3317 * ^X^F^X^P or ^P^X^X^P, see below)
3318 * nothing changes if interrupting mode 0, (eg, the flag
3319 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003320 if (!(compl_cont_status & CONT_INTRPT))
3321 compl_cont_status |= CONT_LOCAL;
3322 else if (compl_cont_mode != 0)
3323 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 /* FALLTHROUGH */
3325 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 /* If we have typed at least 2 ^X's... for modes != 0, we set
3327 * compl_cont_status = 0 (eg, as if we had just started ^X
3328 * mode).
3329 * For mode 0, we set "compl_cont_mode" to an impossible
3330 * value, in both cases ^X^X can be used to restart the same
3331 * mode (avoiding ADDING mode).
3332 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3333 * 'complete' and local ^P expansions respectively.
3334 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3335 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (c == Ctrl_X)
3337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003338 if (compl_cont_mode != 0)
3339 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003341 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343 ctrl_x_mode = 0;
3344 edit_submode = NULL;
3345 showmode();
3346 break;
3347 }
3348 }
3349 else if (ctrl_x_mode != 0)
3350 {
3351 /* We're already in CTRL-X mode, do we stay in it? */
3352 if (!vim_is_ctrl_x_key(c))
3353 {
3354 if (ctrl_x_mode == CTRL_X_SCROLL)
3355 ctrl_x_mode = 0;
3356 else
3357 ctrl_x_mode = CTRL_X_FINISHED;
3358 edit_submode = NULL;
3359 }
3360 showmode();
3361 }
3362
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003363 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 /* Show error message from attempted keyword completion (probably
3366 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003367 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003369 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3370 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 || ctrl_x_mode == CTRL_X_FINISHED)
3372 {
3373 /* Get here when we have finished typing a sequence of ^N and
3374 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003375 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003376 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003378 char_u *p;
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003381 * If any of the original typed text has been changed, eg when
3382 * ignorecase is set, we must add back-spaces to the redo
3383 * buffer. We add as few as necessary to delete just the part
3384 * of the original text that has changed.
3385 * When using the longest match, edited the match or used
3386 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003388 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3389 ptr = compl_curr_match->cp_str;
3390 else if (compl_leader != NULL)
3391 ptr = compl_leader;
3392 else
3393 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003394 if (compl_orig_text != NULL)
3395 {
3396 p = compl_orig_text;
3397 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3398 ++temp)
3399 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003400#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003401 if (temp > 0)
3402 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003403#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003404 for (p += temp; *p != NUL; mb_ptr_adv(p))
3405 AppendCharToRedobuff(K_BS);
3406 }
3407 if (ptr != NULL)
3408 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410
3411#ifdef FEAT_CINDENT
3412 want_cindent = (can_cindent && cindent_on());
3413#endif
3414 /*
3415 * When completing whole lines: fix indent for 'cindent'.
3416 * Otherwise, break line if it's too long.
3417 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003418 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
3420#ifdef FEAT_CINDENT
3421 /* re-indent the current line */
3422 if (want_cindent)
3423 {
3424 do_c_expr_indent();
3425 want_cindent = FALSE; /* don't do it again */
3426 }
3427#endif
3428 }
3429 else
3430 {
3431 /* put the cursor on the last char, for 'tw' formatting */
3432 curwin->w_cursor.col--;
3433 if (stop_arrow() == OK)
3434 insertchar(NUL, 0, -1);
3435 curwin->w_cursor.col++;
3436 }
3437
3438 auto_format(FALSE, TRUE);
3439
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003440 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003441 * the selection without inserting anything. When
3442 * compl_enter_selects is set the Enter key does the same. */
3443 if ((c == Ctrl_Y || (compl_enter_selects
3444 && (c == CAR || c == K_KENTER || c == NL)))
3445 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003446 retval = TRUE;
3447
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003448 /* CTRL-E means completion is Ended, go back to the typed text. */
3449 if (c == Ctrl_E)
3450 {
3451 ins_compl_delete();
3452 if (compl_leader != NULL)
3453 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3454 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003455 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003456 + curwin->w_cursor.col - compl_col);
3457 retval = TRUE;
3458 }
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003461 compl_started = FALSE;
3462 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 msg_clr_cmdline(); /* necessary for "noshowmode" */
3464 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003465 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (edit_submode != NULL)
3467 {
3468 edit_submode = NULL;
3469 showmode();
3470 }
3471
3472#ifdef FEAT_CINDENT
3473 /*
3474 * Indent now if a key was typed that is in 'cinkeys'.
3475 */
3476 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3477 do_c_expr_indent();
3478#endif
3479 }
3480 }
3481
3482 /* reset continue_* if we left expansion-mode, if we stay they'll be
3483 * (re)set properly in ins_complete() */
3484 if (!vim_is_ctrl_x_key(c))
3485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003486 compl_cont_status = 0;
3487 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003489
3490 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491}
3492
3493/*
3494 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3495 * (depending on flag) starting from buf and looking for a non-scanned
3496 * buffer (other than curbuf). curbuf is special, if it is called with
3497 * buf=curbuf then it has to be the first call for a given flag/expansion.
3498 *
3499 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3500 */
3501 static buf_T *
3502ins_compl_next_buf(buf, flag)
3503 buf_T *buf;
3504 int flag;
3505{
3506#ifdef FEAT_WINDOWS
3507 static win_T *wp;
3508#endif
3509
3510 if (flag == 'w') /* just windows */
3511 {
3512#ifdef FEAT_WINDOWS
3513 if (buf == curbuf) /* first call for this flag/expansion */
3514 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003515 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 && wp->w_buffer->b_scanned)
3517 ;
3518 buf = wp->w_buffer;
3519#else
3520 buf = curbuf;
3521#endif
3522 }
3523 else
3524 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3525 * (unlisted buffers)
3526 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003527 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 && ((flag == 'U'
3529 ? buf->b_p_bl
3530 : (!buf->b_p_bl
3531 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 ;
3534 return buf;
3535}
3536
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003537#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003538static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003539
3540/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003541 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003542 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003543 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003544 static void
3545expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003546 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003547 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003548{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003549 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003550 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003551 char_u *funcname;
3552 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003553
Bram Moolenaare344bea2005-09-01 20:46:49 +00003554 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3555 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003556 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003557
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003558 /* Call 'completefunc' to obtain the list of matches. */
3559 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003560 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003561
Bram Moolenaare344bea2005-09-01 20:46:49 +00003562 pos = curwin->w_cursor;
3563 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3564 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003565 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003566 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003567
Bram Moolenaara94bc432006-03-10 21:42:59 +00003568 ins_compl_add_list(matchlist);
3569 list_unref(matchlist);
3570}
3571#endif /* FEAT_COMPL_FUNC */
3572
Bram Moolenaar39f05632006-03-19 22:15:26 +00003573#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003574/*
3575 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003576 */
3577 static void
3578ins_compl_add_list(list)
3579 list_T *list;
3580{
3581 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003582 int dir = compl_direction;
3583
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003584 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003585 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003586 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003587 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3588 /* if dir was BACKWARD then honor it just once */
3589 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003590 else if (did_emsg)
3591 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003592 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003593}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003594
3595/*
3596 * Add a match to the list of matches from a typeval_T.
3597 * If the given string is already in the list of completions, then return
3598 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3599 * maybe because alloc() returns NULL, then FAIL is returned.
3600 */
3601 int
3602ins_compl_add_tv(tv, dir)
3603 typval_T *tv;
3604 int dir;
3605{
3606 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003607 int icase = FALSE;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003608 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003609 char_u *(cptext[CPT_COUNT]);
3610
3611 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3612 {
3613 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3614 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3615 (char_u *)"abbr", FALSE);
3616 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3617 (char_u *)"menu", FALSE);
3618 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3619 (char_u *)"kind", FALSE);
3620 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3621 (char_u *)"info", FALSE);
3622 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3623 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003624 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3625 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003626 }
3627 else
3628 {
3629 word = get_tv_string_chk(tv);
3630 vim_memset(cptext, 0, sizeof(cptext));
3631 }
3632 if (word == NULL || *word == NUL)
3633 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003634 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003635}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003636#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003637
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638/*
3639 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003640 * The search starts at position "ini" in curbuf and in the direction
3641 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 * When "compl_started" is FALSE start at that position, otherwise continue
3643 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003644 * This may return before finding all the matches.
3645 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
3647 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003648ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 static pos_T first_match_pos;
3652 static pos_T last_match_pos;
3653 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003654 static int found_all = FALSE; /* Found all matches of a
3655 certain type. */
3656 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar572cb562005-08-05 21:35:02 +00003658 pos_T *pos;
3659 char_u **matches;
3660 int save_p_scs;
3661 int save_p_ws;
3662 int save_p_ic;
3663 int i;
3664 int num_matches;
3665 int len;
3666 int found_new_match;
3667 int type = ctrl_x_mode;
3668 char_u *ptr;
3669 char_u *dict = NULL;
3670 int dict_f = 0;
3671 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003673 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3676 ins_buf->b_scanned = 0;
3677 found_all = FALSE;
3678 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003680 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 last_match_pos = first_match_pos = *ini;
3682 }
3683
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003684 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003685 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3687 for (;;)
3688 {
3689 found_new_match = FAIL;
3690
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003691 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 * or if found_all says this entry is done. For ^X^L only use the
3693 * entries from 'complete' that look in loaded buffers. */
3694 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003695 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 found_all = FALSE;
3698 while (*e_cpt == ',' || *e_cpt == ' ')
3699 e_cpt++;
3700 if (*e_cpt == '.' && !curbuf->b_scanned)
3701 {
3702 ins_buf = curbuf;
3703 first_match_pos = *ini;
3704 /* So that ^N can match word immediately after cursor */
3705 if (ctrl_x_mode == 0)
3706 dec(&first_match_pos);
3707 last_match_pos = first_match_pos;
3708 type = 0;
3709 }
3710 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3711 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3712 {
3713 /* Scan a buffer, but not the current one. */
3714 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003716 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 first_match_pos.col = last_match_pos.col = 0;
3718 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3719 last_match_pos.lnum = 0;
3720 type = 0;
3721 }
3722 else /* unloaded buffer, scan like dictionary */
3723 {
3724 found_all = TRUE;
3725 if (ins_buf->b_fname == NULL)
3726 continue;
3727 type = CTRL_X_DICTIONARY;
3728 dict = ins_buf->b_fname;
3729 dict_f = DICT_EXACT;
3730 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003731 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 ins_buf->b_fname == NULL
3733 ? buf_spname(ins_buf)
3734 : ins_buf->b_sfname == NULL
3735 ? (char *)ins_buf->b_fname
3736 : (char *)ins_buf->b_sfname);
3737 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3738 }
3739 else if (*e_cpt == NUL)
3740 break;
3741 else
3742 {
3743 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3744 type = -1;
3745 else if (*e_cpt == 'k' || *e_cpt == 's')
3746 {
3747 if (*e_cpt == 'k')
3748 type = CTRL_X_DICTIONARY;
3749 else
3750 type = CTRL_X_THESAURUS;
3751 if (*++e_cpt != ',' && *e_cpt != NUL)
3752 {
3753 dict = e_cpt;
3754 dict_f = DICT_FIRST;
3755 }
3756 }
3757#ifdef FEAT_FIND_ID
3758 else if (*e_cpt == 'i')
3759 type = CTRL_X_PATH_PATTERNS;
3760 else if (*e_cpt == 'd')
3761 type = CTRL_X_PATH_DEFINES;
3762#endif
3763 else if (*e_cpt == ']' || *e_cpt == 't')
3764 {
3765 type = CTRL_X_TAGS;
3766 sprintf((char*)IObuff, _("Scanning tags."));
3767 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3768 }
3769 else
3770 type = -1;
3771
3772 /* in any case e_cpt is advanced to the next entry */
3773 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3774
3775 found_all = TRUE;
3776 if (type == -1)
3777 continue;
3778 }
3779 }
3780
3781 switch (type)
3782 {
3783 case -1:
3784 break;
3785#ifdef FEAT_FIND_ID
3786 case CTRL_X_PATH_PATTERNS:
3787 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003788 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003789 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3793 (linenr_T)1, (linenr_T)MAXLNUM);
3794 break;
3795#endif
3796
3797 case CTRL_X_DICTIONARY:
3798 case CTRL_X_THESAURUS:
3799 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003800 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 : (type == CTRL_X_THESAURUS
3802 ? (*curbuf->b_p_tsr == NUL
3803 ? p_tsr
3804 : curbuf->b_p_tsr)
3805 : (*curbuf->b_p_dict == NUL
3806 ? p_dict
3807 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003809 dict != NULL ? dict_f
3810 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 dict = NULL;
3812 break;
3813
3814 case CTRL_X_TAGS:
3815 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3816 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 * of matches is found when compl_pattern is empty */
3821 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3823 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3824 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3825 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00003826 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 p_ic = save_p_ic;
3829 break;
3830
3831 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3834 {
3835
3836 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003838 ins_compl_add_matches(num_matches, matches,
3839#ifdef CASE_INSENSITIVE_FILENAME
3840 TRUE
3841#else
3842 FALSE
3843#endif
3844 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 break;
3847
3848 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 if (expand_cmdline(&compl_xp, compl_pattern,
3850 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003852 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
3854
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003855#ifdef FEAT_COMPL_FUNC
3856 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003857 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003858 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003859 break;
3860#endif
3861
Bram Moolenaar488c6512005-08-11 20:09:58 +00003862 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003863#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003864 num_matches = expand_spelling(first_match_pos.lnum,
3865 first_match_pos.col, compl_pattern, &matches);
3866 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003867 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003868#endif
3869 break;
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 default: /* normal ^P/^N and ^X^L */
3872 /*
3873 * If 'infercase' is set, don't use 'smartcase' here
3874 */
3875 save_p_scs = p_scs;
3876 if (ins_buf->b_p_inf)
3877 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 /* buffers other than curbuf are scanned from the beginning or the
3880 * end but never from the middle, thus setting nowrapscan in this
3881 * buffers is a good idea, on the other hand, we always set
3882 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3883 save_p_ws = p_ws;
3884 if (ins_buf != curbuf)
3885 p_ws = FALSE;
3886 else if (*e_cpt == '.')
3887 p_ws = TRUE;
3888 for (;;)
3889 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003890 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003892 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3893 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003897 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003899 found_new_match = searchit(NULL, ins_buf, pos,
3900 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003902 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003905 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 first_match_pos = *pos;
3908 last_match_pos = *pos;
3909 }
3910 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003911 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 found_new_match = FAIL;
3913 if (found_new_match == FAIL)
3914 {
3915 if (ins_buf == curbuf)
3916 found_all = TRUE;
3917 break;
3918 }
3919
3920 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 && ini->lnum == pos->lnum
3923 && ini->col == pos->col)
3924 continue;
3925 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3926 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3931 continue;
3932 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3933 if (!p_paste)
3934 ptr = skipwhite(ptr);
3935 }
3936 len = (int)STRLEN(ptr);
3937 }
3938 else
3939 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003940 char_u *tmp_ptr = ptr;
3941
3942 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 /* Skip if already inside a word. */
3946 if (vim_iswordp(tmp_ptr))
3947 continue;
3948 /* Find start of next word. */
3949 tmp_ptr = find_word_start(tmp_ptr);
3950 }
3951 /* Find end of this word. */
3952 tmp_ptr = find_word_end(tmp_ptr);
3953 len = (int)(tmp_ptr - ptr);
3954
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003955 if ((compl_cont_status & CONT_ADDING)
3956 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
3958 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3959 {
3960 /* Try next line, if any. the new word will be
3961 * "join" as if the normal command "J" was used.
3962 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003963 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 * works -- Acevedo */
3965 STRNCPY(IObuff, ptr, len);
3966 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3967 tmp_ptr = ptr = skipwhite(ptr);
3968 /* Find start of next word. */
3969 tmp_ptr = find_word_start(tmp_ptr);
3970 /* Find end of next word. */
3971 tmp_ptr = find_word_end(tmp_ptr);
3972 if (tmp_ptr > ptr)
3973 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003974 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003976 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 IObuff[len++] = ' ';
3978 /* IObuf =~ "\k.* ", thus len >= 2 */
3979 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003980 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 || (vim_strchr(p_cpo, CPO_JOINSP)
3982 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003983 && (IObuff[len - 2] == '?'
3984 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 IObuff[len++] = ' ';
3986 }
3987 /* copy as much as posible of the new word */
3988 if (tmp_ptr - ptr >= IOSIZE - len)
3989 tmp_ptr = ptr + IOSIZE - len - 1;
3990 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3991 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003992 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 IObuff[len] = NUL;
3995 ptr = IObuff;
3996 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003997 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 continue;
3999 }
4000 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004001 if (ins_compl_add_infercase(ptr, len, FALSE,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004002 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004003 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 found_new_match = OK;
4006 break;
4007 }
4008 }
4009 p_scs = save_p_scs;
4010 p_ws = save_p_ws;
4011 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004012
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004013 /* check if compl_curr_match has changed, (e.g. other type of
4014 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004015 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 found_new_match = OK;
4017
4018 /* break the loop for specialized modes (use 'complete' just for the
4019 * generic ctrl_x_mode == 0) or when we've found a new match */
4020 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004021 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004022 {
4023 if (got_int)
4024 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004025 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004026 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004027 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004028
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004029 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4030 || compl_interrupted)
4031 break;
4032 compl_started = TRUE;
4033 }
4034 else
4035 {
4036 /* Mark a buffer scanned when it has been scanned completely */
4037 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4038 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004040 compl_started = FALSE;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004043 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4046 && *e_cpt == NUL) /* Got to end of 'complete' */
4047 found_new_match = FAIL;
4048
4049 i = -1; /* total of matches, unknown */
4050 if (found_new_match == FAIL
4051 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4052 i = ins_compl_make_cyclic();
4053
4054 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055 * just been made cyclic then we have to move compl_curr_match to the next
4056 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004057 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4058 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 if (compl_curr_match == NULL)
4060 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return i;
4062}
4063
4064/* Delete the old text being completed. */
4065 static void
4066ins_compl_delete()
4067{
4068 int i;
4069
4070 /*
4071 * In insert mode: Delete the typed part.
4072 * In replace mode: Put the old characters back, if any.
4073 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 backspace_until_column(i);
4076 changed_cline_bef_curs();
4077}
4078
4079/* Insert the new text being completed. */
4080 static void
4081ins_compl_insert()
4082{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004083 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004084 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4085 compl_used_match = FALSE;
4086 else
4087 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088}
4089
4090/*
4091 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004092 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4093 * get more completions. If it is FALSE, then we just do nothing when there
4094 * are no more completions in a given direction. The latter case is used when
4095 * we are still in the middle of finding completions, to allow browsing
4096 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 * Return the total number of matches, or -1 if still unknown -- webb.
4098 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4100 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 *
4102 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004103 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4104 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
4106 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004107ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004109 int count; /* repeat completion this many times; should
4110 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004111 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 int num_matches = -1;
4114 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004115 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004116 compl_T *found_compl = NULL;
4117 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004118 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004120 if (compl_leader != NULL
4121 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004123 /* Set "compl_shown_match" to the actually shown match, it may differ
4124 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004125 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004126 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004127 && compl_shown_match->cp_next != NULL
4128 && compl_shown_match->cp_next != compl_first_match)
4129 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004130
4131 /* If we didn't find it searching forward, and compl_shows_dir is
4132 * backward, find the last match. */
4133 if (compl_shows_dir == BACKWARD
4134 && !ins_compl_equal(compl_shown_match,
4135 compl_leader, (int)STRLEN(compl_leader))
4136 && (compl_shown_match->cp_next == NULL
4137 || compl_shown_match->cp_next == compl_first_match))
4138 {
4139 while (!ins_compl_equal(compl_shown_match,
4140 compl_leader, (int)STRLEN(compl_leader))
4141 && compl_shown_match->cp_prev != NULL
4142 && compl_shown_match->cp_prev != compl_first_match)
4143 compl_shown_match = compl_shown_match->cp_prev;
4144 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004145 }
4146
4147 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004148 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /* Delete old text to be replaced */
4150 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004151
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004152 /* When finding the longest common text we stick at the original text,
4153 * don't let CTRL-N or CTRL-P move to the first match. */
4154 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4155
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004156 /* When restarting the search don't insert the first match either. */
4157 if (compl_restarting)
4158 {
4159 advance = FALSE;
4160 compl_restarting = FALSE;
4161 }
4162
Bram Moolenaare3226be2005-12-18 22:10:00 +00004163 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4164 * around. */
4165 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004167 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004169 if (compl_pending != 0)
4170 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004171 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004172 found_end = (compl_first_match != NULL
4173 && (compl_shown_match->cp_next == compl_first_match
4174 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004175 }
4176 else if (compl_shows_dir == BACKWARD
4177 && compl_shown_match->cp_prev != NULL)
4178 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004179 if (compl_pending != 0)
4180 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004181 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004182 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004183 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004186 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004187 if (advance)
4188 {
4189 if (compl_shows_dir == BACKWARD)
4190 --compl_pending;
4191 else
4192 ++compl_pending;
4193 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004194 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004195 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004196
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004197 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004198 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004199 if (compl_pending != 0 && compl_direction == compl_shows_dir
4200 && advance)
Bram Moolenaara6557602006-02-04 22:43:20 +00004201 compl_shown_match = compl_curr_match;
4202 found_end = FALSE;
4203 }
4204 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4205 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004206 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004207 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004208 ++todo;
4209 else
4210 /* Remember a matching item. */
4211 found_compl = compl_shown_match;
4212
4213 /* Stop at the end of the list when we found a usable match. */
4214 if (found_end)
4215 {
4216 if (found_compl != NULL)
4217 {
4218 compl_shown_match = found_compl;
4219 break;
4220 }
4221 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004225 /* Insert the text of the new completion, or the compl_leader. */
4226 if (insert_match)
4227 {
4228 if (!compl_get_longest || compl_used_match)
4229 ins_compl_insert();
4230 else
4231 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4232 }
4233 else
4234 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 if (!allow_get_expansion)
4237 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004238 /* may undisplay the popup menu first */
4239 ins_compl_upd_pum();
4240
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004241 /* redraw to show the user what was inserted */
4242 update_screen(0);
4243
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004244 /* display the updated popup menu */
4245 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004246#ifdef FEAT_GUI
4247 if (gui.in_use)
4248 {
4249 /* Show the cursor after the match, not after the redrawn text. */
4250 setcursor();
4251 out_flush();
4252 gui_update_cursor(FALSE, FALSE);
4253 }
4254#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /* Delete old text to be replaced, since we're still searching and
4257 * don't want to match ourselves! */
4258 ins_compl_delete();
4259 }
4260
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004261 /* Enter will select a match when the match wasn't inserted and the popup
4262 * menu is visislbe. */
4263 compl_enter_selects = !insert_match && compl_match_array != NULL;
4264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 /*
4266 * Show the file name for the match (if any)
4267 * Truncate the file name to avoid a wait for return.
4268 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004269 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004272 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (i <= 0)
4274 i = 0;
4275 else
4276 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004277 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 msg(IObuff);
4279 redraw_cmdline = FALSE; /* don't overwrite! */
4280 }
4281
4282 return num_matches;
4283}
4284
4285/*
4286 * Call this while finding completions, to check whether the user has hit a key
4287 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004288 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004290 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 */
4292 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004293ins_compl_check_keys(frequency)
4294 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 static int count = 0;
4297
4298 int c;
4299
4300 /* Don't check when reading keys from a script. That would break the test
4301 * scripts */
4302 if (using_script())
4303 return;
4304
4305 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004306 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return;
4308 count = 0;
4309
4310 ++no_mapping;
4311 c = vpeekc_any();
4312 --no_mapping;
4313 if (c != NUL)
4314 {
4315 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4316 {
4317 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004318 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004319 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4320 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004325 if (compl_pending != 0 && !got_int)
4326 (void)ins_compl_next(FALSE, compl_pending > 0
4327 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004328}
4329
4330/*
4331 * Decide the direction of Insert mode complete from the key typed.
4332 * Returns BACKWARD or FORWARD.
4333 */
4334 static int
4335ins_compl_key2dir(c)
4336 int c;
4337{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004338 if (c == Ctrl_P || c == Ctrl_L
4339 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4340 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004341 return BACKWARD;
4342 return FORWARD;
4343}
4344
4345/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004346 * Return TRUE for keys that are used for completion only when the popup menu
4347 * is visible.
4348 */
4349 static int
4350ins_compl_pum_key(c)
4351 int c;
4352{
4353 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004354 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4355 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004356}
4357
4358/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004359 * Decide the number of completions to move forward.
4360 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4361 */
4362 static int
4363ins_compl_key2count(c)
4364 int c;
4365{
4366 int h;
4367
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004368 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004369 {
4370 h = pum_get_height();
4371 if (h > 3)
4372 h -= 2; /* keep some context */
4373 return h;
4374 }
4375 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376}
4377
4378/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004379 * Return TRUE if completion with "c" should insert the match, FALSE if only
4380 * to change the currently selected completion.
4381 */
4382 static int
4383ins_compl_use_match(c)
4384 int c;
4385{
4386 switch (c)
4387 {
4388 case K_UP:
4389 case K_DOWN:
4390 case K_PAGEDOWN:
4391 case K_KPAGEDOWN:
4392 case K_S_DOWN:
4393 case K_PAGEUP:
4394 case K_KPAGEUP:
4395 case K_S_UP:
4396 return FALSE;
4397 }
4398 return TRUE;
4399}
4400
4401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 * Do Insert mode completion.
4403 * Called when character "c" was typed, which has a meaning for completion.
4404 * Returns OK if completion was done, FAIL if something failed (out of mem).
4405 */
4406 static int
4407ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 char_u *line;
4411 int startcol = 0; /* column where searched text starts */
4412 colnr_T curs_col; /* cursor column */
4413 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Bram Moolenaare3226be2005-12-18 22:10:00 +00004415 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 /* First time we hit ^N or ^P (in a row, I mean) */
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 did_ai = FALSE;
4421#ifdef FEAT_SMARTINDENT
4422 did_si = FALSE;
4423 can_si = FALSE;
4424 can_si_back = FALSE;
4425#endif
4426 if (stop_arrow() == FAIL)
4427 return FAIL;
4428
4429 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004431 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 * "compl_startpos" to the cursor as a pattern to add a new word
4435 * instead of expand the one before the cursor, in word-wise if
4436 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * is not in the same line as the cursor then fix it (the line has
4438 * been split because it was longer than 'tw'). if SOL is set then
4439 * skip the previous pattern, a word at the beginning of the line has
4440 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004441 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4442 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 /*
4445 * it is a continued search
4446 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4449 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4450 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 /* line (probably) wrapped, set compl_startpos to the
4454 * first non_blank in the line, if it is not a wordchar
4455 * include it to get a better pattern, but then we don't
4456 * want the "\\<" prefix, check it bellow */
4457 compl_col = (colnr_T)(skipwhite(line) - line);
4458 compl_startpos.col = compl_col;
4459 compl_startpos.lnum = curwin->w_cursor.lnum;
4460 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
4463 {
4464 /* S_IPOS was set when we inserted a word that was at the
4465 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 * mode but first we need to redefine compl_startpos */
4467 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 compl_cont_status |= CONT_SOL;
4470 compl_startpos.col = (colnr_T)(skipwhite(
4471 line + compl_length
4472 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004477 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 * have enough space? just being paranoic */
4479#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 compl_cont_status &= ~CONT_SOL;
4483 compl_length = (IOSIZE - MIN_SPACE);
4484 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4487 if (compl_length < 1)
4488 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004496 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 compl_cont_status = 0;
4503 compl_cont_status |= CONT_N_ADDS;
4504 compl_startpos = curwin->w_cursor;
4505 startcol = (int)curs_col;
4506 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
4509 /* Work out completion pattern and original text -- webb */
4510 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4511 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4514 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004517 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 compl_col += ++startcol;
4520 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 compl_pattern = str_foldcase(line + compl_col,
4524 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004526 compl_pattern = vim_strnsave(line + compl_col,
4527 compl_length);
4528 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 return FAIL;
4530 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
4533 char_u *prefix = (char_u *)"\\<";
4534
4535 /* we need 3 extra chars, 1 for the NUL and
4536 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4538 compl_length) + 3);
4539 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 if (!vim_iswordp(line + compl_col)
4542 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 && (
4544#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#endif
4549 )))
4550 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 STRCPY((char *)compl_pattern, prefix);
4552 (void)quote_meta(compl_pattern + STRLEN(prefix),
4553 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#endif
4561 )
4562 {
4563 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4565 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 compl_col += curs_col;
4568 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 else
4571 {
4572#ifdef FEAT_MBYTE
4573 /* Search the point of change class of multibyte character
4574 * or not a word single byte character backward. */
4575 if (has_mbyte)
4576 {
4577 int base_class;
4578 int head_off;
4579
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 startcol -= (*mb_head_off)(line, line + startcol);
4581 base_class = mb_get_class(line + startcol);
4582 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004584 head_off = (*mb_head_off)(line, line + startcol);
4585 if (base_class != mb_get_class(line + startcol
4586 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 }
4591 else
4592#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 compl_col += ++startcol;
4596 compl_length = (int)curs_col - startcol;
4597 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 /* Only match word with at least two chars -- webb
4600 * there's no need to call quote_meta,
4601 * alloc(7) is enough -- Acevedo
4602 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 compl_pattern = alloc(7);
4604 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 STRCPY((char *)compl_pattern, "\\<");
4607 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4608 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 else
4611 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4613 compl_length) + 3);
4614 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 STRCPY((char *)compl_pattern, "\\<");
4617 (void)quote_meta(compl_pattern + 2, line + compl_col,
4618 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
4620 }
4621 }
4622 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4623 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004624 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004625 compl_length = (int)curs_col - (int)compl_col;
4626 if (compl_length < 0) /* cursor in indent: empty pattern */
4627 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004629 compl_pattern = str_foldcase(line + compl_col, compl_length,
4630 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4633 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635 }
4636 else if (ctrl_x_mode == CTRL_X_FILES)
4637 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004640 compl_col += ++startcol;
4641 compl_length = (int)curs_col - startcol;
4642 compl_pattern = addstar(line + compl_col, compl_length,
4643 EXPAND_FILES);
4644 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return FAIL;
4646 }
4647 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4648 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649 compl_pattern = vim_strnsave(line, curs_col);
4650 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 set_cmd_context(&compl_xp, compl_pattern,
4653 (int)STRLEN(compl_pattern), curs_col);
4654 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4655 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004656 {
4657 compl_col = curs_col;
4658 compl_length = 0;
4659 vim_free(compl_pattern);
4660 compl_pattern = NULL;
4661 }
4662 else
4663 {
4664 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4665 compl_col = startcol;
4666 compl_length = curs_col - startcol;
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004669 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004670 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004671#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004672 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004673 * Call user defined function 'completefunc' with "a:findstart"
4674 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004675 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004676 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004677 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004678 char_u *funcname;
4679 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004680
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004681 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004682 * string */
4683 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4684 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4685 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004686 {
4687 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4688 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004689 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004690 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004691
4692 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004693 args[1] = NULL;
4694 pos = curwin->w_cursor;
4695 col = call_func_retnr(funcname, 2, args, FALSE);
4696 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004697
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004698 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004699 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004700 compl_col = col;
4701 if ((colnr_T)compl_col > curs_col)
4702 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004703
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004704 /* Setup variables for completion. Need to obtain "line" again,
4705 * it may have become invalid. */
4706 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004707 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004708 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4709 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004710#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004711 return FAIL;
4712 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004713 else if (ctrl_x_mode == CTRL_X_SPELL)
4714 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004715#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004716 if (spell_bad_len > 0)
4717 compl_col = curs_col - spell_bad_len;
4718 else
4719 compl_col = spell_word_start(startcol);
4720 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004721 {
4722 compl_length = 0;
4723 compl_col = curs_col;
4724 }
4725 else
4726 {
4727 spell_expand_check_cap(compl_col);
4728 compl_length = (int)curs_col - compl_col;
4729 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004730 /* Need to obtain "line" again, it may have become invalid. */
4731 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004732 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4733 if (compl_pattern == NULL)
4734#endif
4735 return FAIL;
4736 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004737 else
4738 {
4739 EMSG2(_(e_intern2), "ins_complete()");
4740 return FAIL;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004743 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
4745 edit_submode_pre = (char_u *)_(" Adding");
4746 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4747 {
4748 /* Insert a new line, keep indentation but ignore 'comments' */
4749#ifdef FEAT_COMMENTS
4750 char_u *old = curbuf->b_p_com;
4751
4752 curbuf->b_p_com = (char_u *)"";
4753#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 compl_startpos.lnum = curwin->w_cursor.lnum;
4755 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 ins_eol('\r');
4757#ifdef FEAT_COMMENTS
4758 curbuf->b_p_com = old;
4759#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004760 compl_length = 0;
4761 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 }
4764 else
4765 {
4766 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 if (compl_cont_status & CONT_LOCAL)
4771 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 else
4773 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4774
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004775 /* Always add completion for the original text. */
4776 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004777 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4778 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004779 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 vim_free(compl_pattern);
4782 compl_pattern = NULL;
4783 vim_free(compl_orig_text);
4784 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return FAIL;
4786 }
4787
4788 /* showmode might reset the internal line pointers, so it must
4789 * be called before line = ml_get(), or when this address is no
4790 * longer needed. -- Acevedo.
4791 */
4792 edit_submode_extra = (char_u *)_("-- Searching...");
4793 edit_submode_highl = HLF_COUNT;
4794 showmode();
4795 edit_submode_extra = NULL;
4796 out_flush();
4797 }
4798
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004799 compl_shown_match = compl_curr_match;
4800 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004803 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004805 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004807 /* may undisplay the popup menu */
4808 ins_compl_upd_pum();
4809
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004810 if (n > 1) /* all matches have been found */
4811 compl_matches = n;
4812 compl_curr_match = compl_shown_match;
4813 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaard68071d2006-05-02 22:08:30 +00004815 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4816 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (got_int && !global_busy)
4818 {
4819 (void)vgetc();
4820 got_int = FALSE;
4821 }
4822
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004824 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004826 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4827 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4829 edit_submode_highl = HLF_E;
4830 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4831 * because we couldn't expand anything at first place, but if we used
4832 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4833 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004834 if ( compl_length > 1
4835 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 || (ctrl_x_mode != 0
4837 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4838 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841
Bram Moolenaar572cb562005-08-05 21:35:02 +00004842 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004843 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004845 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 if (edit_submode_extra == NULL)
4848 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004849 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 edit_submode_extra = (char_u *)_("Back at original");
4852 edit_submode_highl = HLF_W;
4853 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004854 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
4856 edit_submode_extra = (char_u *)_("Word from other line");
4857 edit_submode_highl = HLF_COUNT;
4858 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004859 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
4861 edit_submode_extra = (char_u *)_("The only match");
4862 edit_submode_highl = HLF_COUNT;
4863 }
4864 else
4865 {
4866 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004867 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004869 int number = 0;
4870 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 /* search backwards for the first valid (!= -1) number.
4875 * This should normally succeed already at the first loop
4876 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004877 for (match = compl_curr_match->cp_prev; match != NULL
4878 && match != compl_first_match;
4879 match = match->cp_prev)
4880 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004882 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 break;
4884 }
4885 if (match != NULL)
4886 /* go up and assign all numbers which are not assigned
4887 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004888 for (match = match->cp_next;
4889 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004890 match = match->cp_next)
4891 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 else /* BACKWARD */
4894 {
4895 /* search forwards (upwards) for the first valid (!= -1)
4896 * number. This should normally succeed already at the
4897 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 for (match = compl_curr_match->cp_next; match != NULL
4899 && match != compl_first_match;
4900 match = match->cp_next)
4901 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004903 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 break;
4905 }
4906 if (match != NULL)
4907 /* go down and assign all numbers which are not
4908 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004909 for (match = match->cp_prev; match
4910 && match->cp_number == -1;
4911 match = match->cp_prev)
4912 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 }
4915
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004916 /* The match should always have a sequence number now, this is
4917 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004918 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
4920 /* Space for 10 text chars. + 2x10-digit no.s */
4921 static char_u match_ref[31];
4922
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004923 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004925 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004927 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004928 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004929 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 edit_submode_extra = match_ref;
4931 edit_submode_highl = HLF_R;
4932 if (dollar_vcol)
4933 curs_columns(FALSE);
4934 }
4935 }
4936 }
4937
4938 /* Show a message about what (completion) mode we're in. */
4939 showmode();
4940 if (edit_submode_extra != NULL)
4941 {
4942 if (!p_smd)
4943 msg_attr(edit_submode_extra,
4944 edit_submode_highl < HLF_COUNT
4945 ? hl_attr(edit_submode_highl) : 0);
4946 }
4947 else
4948 msg_clr_cmdline(); /* necessary for "noshowmode" */
4949
Bram Moolenaard68071d2006-05-02 22:08:30 +00004950 /* Show the popup menu, unless we got interrupted. */
4951 if (!compl_interrupted)
4952 {
4953 /* RedrawingDisabled may be set when invoked through complete(). */
4954 n = RedrawingDisabled;
4955 RedrawingDisabled = 0;
4956 ins_compl_show_pum();
4957 setcursor();
4958 RedrawingDisabled = n;
4959 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004960 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004961 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return OK;
4964}
4965
4966/*
4967 * Looks in the first "len" chars. of "src" for search-metachars.
4968 * If dest is not NULL the chars. are copied there quoting (with
4969 * a backslash) the metachars, and dest would be NUL terminated.
4970 * Returns the length (needed) of dest
4971 */
4972 static int
4973quote_meta(dest, src, len)
4974 char_u *dest;
4975 char_u *src;
4976 int len;
4977{
4978 int m;
4979
4980 for (m = len; --len >= 0; src++)
4981 {
4982 switch (*src)
4983 {
4984 case '.':
4985 case '*':
4986 case '[':
4987 if (ctrl_x_mode == CTRL_X_DICTIONARY
4988 || ctrl_x_mode == CTRL_X_THESAURUS)
4989 break;
4990 case '~':
4991 if (!p_magic) /* quote these only if magic is set */
4992 break;
4993 case '\\':
4994 if (ctrl_x_mode == CTRL_X_DICTIONARY
4995 || ctrl_x_mode == CTRL_X_THESAURUS)
4996 break;
4997 case '^': /* currently it's not needed. */
4998 case '$':
4999 m++;
5000 if (dest != NULL)
5001 *dest++ = '\\';
5002 break;
5003 }
5004 if (dest != NULL)
5005 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005006# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 /* Copy remaining bytes of a multibyte character. */
5008 if (has_mbyte)
5009 {
5010 int i, mb_len;
5011
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005012 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (mb_len > 0 && len >= mb_len)
5014 for (i = 0; i < mb_len; ++i)
5015 {
5016 --len;
5017 ++src;
5018 if (dest != NULL)
5019 *dest++ = *src;
5020 }
5021 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 if (dest != NULL)
5025 *dest = NUL;
5026
5027 return m;
5028}
5029#endif /* FEAT_INS_EXPAND */
5030
5031/*
5032 * Next character is interpreted literally.
5033 * A one, two or three digit decimal number is interpreted as its byte value.
5034 * If one or two digits are entered, the next character is given to vungetc().
5035 * For Unicode a character > 255 may be returned.
5036 */
5037 int
5038get_literal()
5039{
5040 int cc;
5041 int nc;
5042 int i;
5043 int hex = FALSE;
5044 int octal = FALSE;
5045#ifdef FEAT_MBYTE
5046 int unicode = 0;
5047#endif
5048
5049 if (got_int)
5050 return Ctrl_C;
5051
5052#ifdef FEAT_GUI
5053 /*
5054 * In GUI there is no point inserting the internal code for a special key.
5055 * It is more useful to insert the string "<KEY>" instead. This would
5056 * probably be useful in a text window too, but it would not be
5057 * vi-compatible (maybe there should be an option for it?) -- webb
5058 */
5059 if (gui.in_use)
5060 ++allow_keys;
5061#endif
5062#ifdef USE_ON_FLY_SCROLL
5063 dont_scroll = TRUE; /* disallow scrolling here */
5064#endif
5065 ++no_mapping; /* don't map the next key hits */
5066 cc = 0;
5067 i = 0;
5068 for (;;)
5069 {
5070 do
5071 nc = safe_vgetc();
5072 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5073 || nc == K_HOR_SCROLLBAR);
5074#ifdef FEAT_CMDL_INFO
5075 if (!(State & CMDLINE)
5076# ifdef FEAT_MBYTE
5077 && MB_BYTE2LEN_CHECK(nc) == 1
5078# endif
5079 )
5080 add_to_showcmd(nc);
5081#endif
5082 if (nc == 'x' || nc == 'X')
5083 hex = TRUE;
5084 else if (nc == 'o' || nc == 'O')
5085 octal = TRUE;
5086#ifdef FEAT_MBYTE
5087 else if (nc == 'u' || nc == 'U')
5088 unicode = nc;
5089#endif
5090 else
5091 {
5092 if (hex
5093#ifdef FEAT_MBYTE
5094 || unicode != 0
5095#endif
5096 )
5097 {
5098 if (!vim_isxdigit(nc))
5099 break;
5100 cc = cc * 16 + hex2nr(nc);
5101 }
5102 else if (octal)
5103 {
5104 if (nc < '0' || nc > '7')
5105 break;
5106 cc = cc * 8 + nc - '0';
5107 }
5108 else
5109 {
5110 if (!VIM_ISDIGIT(nc))
5111 break;
5112 cc = cc * 10 + nc - '0';
5113 }
5114
5115 ++i;
5116 }
5117
5118 if (cc > 255
5119#ifdef FEAT_MBYTE
5120 && unicode == 0
5121#endif
5122 )
5123 cc = 255; /* limit range to 0-255 */
5124 nc = 0;
5125
5126 if (hex) /* hex: up to two chars */
5127 {
5128 if (i >= 2)
5129 break;
5130 }
5131#ifdef FEAT_MBYTE
5132 else if (unicode) /* Unicode: up to four or eight chars */
5133 {
5134 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5135 break;
5136 }
5137#endif
5138 else if (i >= 3) /* decimal or octal: up to three chars */
5139 break;
5140 }
5141 if (i == 0) /* no number entered */
5142 {
5143 if (nc == K_ZERO) /* NUL is stored as NL */
5144 {
5145 cc = '\n';
5146 nc = 0;
5147 }
5148 else
5149 {
5150 cc = nc;
5151 nc = 0;
5152 }
5153 }
5154
5155 if (cc == 0) /* NUL is stored as NL */
5156 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005157#ifdef FEAT_MBYTE
5158 if (enc_dbcs && (cc & 0xff) == 0)
5159 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5160 second byte will cause trouble! */
5161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
5163 --no_mapping;
5164#ifdef FEAT_GUI
5165 if (gui.in_use)
5166 --allow_keys;
5167#endif
5168 if (nc)
5169 vungetc(nc);
5170 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5171 return cc;
5172}
5173
5174/*
5175 * Insert character, taking care of special keys and mod_mask
5176 */
5177 static void
5178insert_special(c, allow_modmask, ctrlv)
5179 int c;
5180 int allow_modmask;
5181 int ctrlv; /* c was typed after CTRL-V */
5182{
5183 char_u *p;
5184 int len;
5185
5186 /*
5187 * Special function key, translate into "<Key>". Up to the last '>' is
5188 * inserted with ins_str(), so as not to replace characters in replace
5189 * mode.
5190 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5191 * unless 'allow_modmask' is TRUE.
5192 */
5193#ifdef MACOS
5194 /* Command-key never produces a normal key */
5195 if (mod_mask & MOD_MASK_CMD)
5196 allow_modmask = TRUE;
5197#endif
5198 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5199 {
5200 p = get_special_key_name(c, mod_mask);
5201 len = (int)STRLEN(p);
5202 c = p[len - 1];
5203 if (len > 2)
5204 {
5205 if (stop_arrow() == FAIL)
5206 return;
5207 p[len - 1] = NUL;
5208 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005209 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 ctrlv = FALSE;
5211 }
5212 }
5213 if (stop_arrow() == OK)
5214 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5215}
5216
5217/*
5218 * Special characters in this context are those that need processing other
5219 * than the simple insertion that can be performed here. This includes ESC
5220 * which terminates the insert, and CR/NL which need special processing to
5221 * open up a new line. This routine tries to optimize insertions performed by
5222 * the "redo", "undo" or "put" commands, so it needs to know when it should
5223 * stop and defer processing to the "normal" mechanism.
5224 * '0' and '^' are special, because they can be followed by CTRL-D.
5225 */
5226#ifdef EBCDIC
5227# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5228#else
5229# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5230#endif
5231
5232#ifdef FEAT_MBYTE
5233# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5234#else
5235# define WHITECHAR(cc) vim_iswhite(cc)
5236#endif
5237
5238 void
5239insertchar(c, flags, second_indent)
5240 int c; /* character to insert or NUL */
5241 int flags; /* INSCHAR_FORMAT, etc. */
5242 int second_indent; /* indent for second line if >= 0 */
5243{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 int textwidth;
5245#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5251 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 /*
5254 * Try to break the line in two or more pieces when:
5255 * - Always do this if we have been called to do formatting only.
5256 * - Always do this when 'formatoptions' has the 'a' flag and the line
5257 * ends in white space.
5258 * - Otherwise:
5259 * - Don't do this if inserting a blank
5260 * - Don't do this if an existing character is being replaced, unless
5261 * we're in VREPLACE mode.
5262 * - Do this if the cursor is not on the line where insert started
5263 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5264 * before the insert.
5265 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5266 * before 'textwidth'
5267 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005268 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 && ((flags & INSCHAR_FORMAT)
5270 || (!vim_iswhite(c)
5271 && !((State & REPLACE_FLAG)
5272#ifdef FEAT_VREPLACE
5273 && !(State & VREPLACE_FLAG)
5274#endif
5275 && *ml_get_cursor() != NUL)
5276 && (curwin->w_cursor.lnum != Insstart.lnum
5277 || ((!has_format_option(FO_INS_LONG)
5278 || Insstart_textlen <= (colnr_T)textwidth)
5279 && (!fo_ins_blank
5280 || Insstart_blank_vcol <= (colnr_T)textwidth
5281 ))))))
5282 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005283 /* Format with 'formatexpr' when it's set. Use internal formatting
5284 * when 'formatexpr' isn't set or it returns non-zero. */
5285#if defined(FEAT_EVAL)
5286 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005287 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005289 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005291
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (c == NUL) /* only formatting was wanted */
5293 return;
5294
5295#ifdef FEAT_COMMENTS
5296 /* Check whether this character should end a comment. */
5297 if (did_ai && (int)c == end_comment_pending)
5298 {
5299 char_u *line;
5300 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5301 int middle_len, end_len;
5302 int i;
5303
5304 /*
5305 * Need to remove existing (middle) comment leader and insert end
5306 * comment leader. First, check what comment leader we can find.
5307 */
5308 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5309 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5310 {
5311 /* Skip middle-comment string */
5312 while (*p && p[-1] != ':') /* find end of middle flags */
5313 ++p;
5314 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5315 /* Don't count trailing white space for middle_len */
5316 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5317 --middle_len;
5318
5319 /* Find the end-comment string */
5320 while (*p && p[-1] != ':') /* find end of end flags */
5321 ++p;
5322 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5323
5324 /* Skip white space before the cursor */
5325 i = curwin->w_cursor.col;
5326 while (--i >= 0 && vim_iswhite(line[i]))
5327 ;
5328 i++;
5329
5330 /* Skip to before the middle leader */
5331 i -= middle_len;
5332
5333 /* Check some expected things before we go on */
5334 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5335 {
5336 /* Backspace over all the stuff we want to replace */
5337 backspace_until_column(i);
5338
5339 /*
5340 * Insert the end-comment string, except for the last
5341 * character, which will get inserted as normal later.
5342 */
5343 ins_bytes_len(lead_end, end_len - 1);
5344 }
5345 }
5346 }
5347 end_comment_pending = NUL;
5348#endif
5349
5350 did_ai = FALSE;
5351#ifdef FEAT_SMARTINDENT
5352 did_si = FALSE;
5353 can_si = FALSE;
5354 can_si_back = FALSE;
5355#endif
5356
5357 /*
5358 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5359 * This speeds up normal text input considerably.
5360 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5361 * need to re-indent at a ':', or any other character (but not what
5362 * 'paste' is set)..
5363 */
5364#ifdef USE_ON_FLY_SCROLL
5365 dont_scroll = FALSE; /* allow scrolling here */
5366#endif
5367
5368 if ( !ISSPECIAL(c)
5369#ifdef FEAT_MBYTE
5370 && (!has_mbyte || (*mb_char2len)(c) == 1)
5371#endif
5372 && vpeekc() != NUL
5373 && !(State & REPLACE_FLAG)
5374#ifdef FEAT_CINDENT
5375 && !cindent_on()
5376#endif
5377#ifdef FEAT_RIGHTLEFT
5378 && !p_ri
5379#endif
5380 )
5381 {
5382#define INPUT_BUFLEN 100
5383 char_u buf[INPUT_BUFLEN + 1];
5384 int i;
5385 colnr_T virtcol = 0;
5386
5387 buf[0] = c;
5388 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005389 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 virtcol = get_nolist_virtcol();
5391 /*
5392 * Stop the string when:
5393 * - no more chars available
5394 * - finding a special character (command key)
5395 * - buffer is full
5396 * - running into the 'textwidth' boundary
5397 * - need to check for abbreviation: A non-word char after a word-char
5398 */
5399 while ( (c = vpeekc()) != NUL
5400 && !ISSPECIAL(c)
5401#ifdef FEAT_MBYTE
5402 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5403#endif
5404 && i < INPUT_BUFLEN
5405 && (textwidth == 0
5406 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5407 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5408 {
5409#ifdef FEAT_RIGHTLEFT
5410 c = vgetc();
5411 if (p_hkmap && KeyTyped)
5412 c = hkmap(c); /* Hebrew mode mapping */
5413# ifdef FEAT_FKMAP
5414 if (p_fkmap && KeyTyped)
5415 c = fkmap(c); /* Farsi mode mapping */
5416# endif
5417 buf[i++] = c;
5418#else
5419 buf[i++] = vgetc();
5420#endif
5421 }
5422
5423#ifdef FEAT_DIGRAPHS
5424 do_digraph(-1); /* clear digraphs */
5425 do_digraph(buf[i-1]); /* may be the start of a digraph */
5426#endif
5427 buf[i] = NUL;
5428 ins_str(buf);
5429 if (flags & INSCHAR_CTRLV)
5430 {
5431 redo_literal(*buf);
5432 i = 1;
5433 }
5434 else
5435 i = 0;
5436 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005437 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439 else
5440 {
5441#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005442 int cc;
5443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5445 {
5446 char_u buf[MB_MAXBYTES + 1];
5447
5448 (*mb_char2bytes)(c, buf);
5449 buf[cc] = NUL;
5450 ins_char_bytes(buf, cc);
5451 AppendCharToRedobuff(c);
5452 }
5453 else
5454#endif
5455 {
5456 ins_char(c);
5457 if (flags & INSCHAR_CTRLV)
5458 redo_literal(c);
5459 else
5460 AppendCharToRedobuff(c);
5461 }
5462 }
5463}
5464
5465/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005466 * Format text at the current insert position.
5467 */
5468 static void
5469internal_format(textwidth, second_indent, flags, format_only)
5470 int textwidth;
5471 int second_indent;
5472 int flags;
5473 int format_only;
5474{
5475 int cc;
5476 int save_char = NUL;
5477 int haveto_redraw = FALSE;
5478 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5479#ifdef FEAT_MBYTE
5480 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5481#endif
5482 int fo_white_par = has_format_option(FO_WHITE_PAR);
5483 int first_line = TRUE;
5484#ifdef FEAT_COMMENTS
5485 colnr_T leader_len;
5486 int no_leader = FALSE;
5487 int do_comments = (flags & INSCHAR_DO_COM);
5488#endif
5489
5490 /*
5491 * When 'ai' is off we don't want a space under the cursor to be
5492 * deleted. Replace it with an 'x' temporarily.
5493 */
5494 if (!curbuf->b_p_ai)
5495 {
5496 cc = gchar_cursor();
5497 if (vim_iswhite(cc))
5498 {
5499 save_char = cc;
5500 pchar_cursor('x');
5501 }
5502 }
5503
5504 /*
5505 * Repeat breaking lines, until the current line is not too long.
5506 */
5507 while (!got_int)
5508 {
5509 int startcol; /* Cursor column at entry */
5510 int wantcol; /* column at textwidth border */
5511 int foundcol; /* column for start of spaces */
5512 int end_foundcol = 0; /* column for start of word */
5513 colnr_T len;
5514 colnr_T virtcol;
5515#ifdef FEAT_VREPLACE
5516 int orig_col = 0;
5517 char_u *saved_text = NULL;
5518#endif
5519 colnr_T col;
5520
5521 virtcol = get_nolist_virtcol();
5522 if (virtcol < (colnr_T)textwidth)
5523 break;
5524
5525#ifdef FEAT_COMMENTS
5526 if (no_leader)
5527 do_comments = FALSE;
5528 else if (!(flags & INSCHAR_FORMAT)
5529 && has_format_option(FO_WRAP_COMS))
5530 do_comments = TRUE;
5531
5532 /* Don't break until after the comment leader */
5533 if (do_comments)
5534 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5535 else
5536 leader_len = 0;
5537
5538 /* If the line doesn't start with a comment leader, then don't
5539 * start one in a following broken line. Avoids that a %word
5540 * moved to the start of the next line causes all following lines
5541 * to start with %. */
5542 if (leader_len == 0)
5543 no_leader = TRUE;
5544#endif
5545 if (!(flags & INSCHAR_FORMAT)
5546#ifdef FEAT_COMMENTS
5547 && leader_len == 0
5548#endif
5549 && !has_format_option(FO_WRAP))
5550
5551 {
5552 textwidth = 0;
5553 break;
5554 }
5555 if ((startcol = curwin->w_cursor.col) == 0)
5556 break;
5557
5558 /* find column of textwidth border */
5559 coladvance((colnr_T)textwidth);
5560 wantcol = curwin->w_cursor.col;
5561
5562 curwin->w_cursor.col = startcol - 1;
5563#ifdef FEAT_MBYTE
5564 /* Correct cursor for multi-byte character. */
5565 if (has_mbyte)
5566 mb_adjust_cursor();
5567#endif
5568 foundcol = 0;
5569
5570 /*
5571 * Find position to break at.
5572 * Stop at first entered white when 'formatoptions' has 'v'
5573 */
5574 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5575 || curwin->w_cursor.lnum != Insstart.lnum
5576 || curwin->w_cursor.col >= Insstart.col)
5577 {
5578 cc = gchar_cursor();
5579 if (WHITECHAR(cc))
5580 {
5581 /* remember position of blank just before text */
5582 end_foundcol = curwin->w_cursor.col;
5583
5584 /* find start of sequence of blanks */
5585 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5586 {
5587 dec_cursor();
5588 cc = gchar_cursor();
5589 }
5590 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5591 break; /* only spaces in front of text */
5592#ifdef FEAT_COMMENTS
5593 /* Don't break until after the comment leader */
5594 if (curwin->w_cursor.col < leader_len)
5595 break;
5596#endif
5597 if (has_format_option(FO_ONE_LETTER))
5598 {
5599 /* do not break after one-letter words */
5600 if (curwin->w_cursor.col == 0)
5601 break; /* one-letter word at begin */
5602
5603 col = curwin->w_cursor.col;
5604 dec_cursor();
5605 cc = gchar_cursor();
5606
5607 if (WHITECHAR(cc))
5608 continue; /* one-letter, continue */
5609 curwin->w_cursor.col = col;
5610 }
5611#ifdef FEAT_MBYTE
5612 if (has_mbyte)
5613 foundcol = curwin->w_cursor.col
5614 + (*mb_ptr2len)(ml_get_cursor());
5615 else
5616#endif
5617 foundcol = curwin->w_cursor.col + 1;
5618 if (curwin->w_cursor.col < (colnr_T)wantcol)
5619 break;
5620 }
5621#ifdef FEAT_MBYTE
5622 else if (cc >= 0x100 && fo_multibyte
5623 && curwin->w_cursor.col <= (colnr_T)wantcol)
5624 {
5625 /* Break after or before a multi-byte character. */
5626 foundcol = curwin->w_cursor.col;
5627 if (curwin->w_cursor.col < (colnr_T)wantcol)
5628 foundcol += (*mb_char2len)(cc);
5629 end_foundcol = foundcol;
5630 break;
5631 }
5632#endif
5633 if (curwin->w_cursor.col == 0)
5634 break;
5635 dec_cursor();
5636 }
5637
5638 if (foundcol == 0) /* no spaces, cannot break line */
5639 {
5640 curwin->w_cursor.col = startcol;
5641 break;
5642 }
5643
5644 /* Going to break the line, remove any "$" now. */
5645 undisplay_dollar();
5646
5647 /*
5648 * Offset between cursor position and line break is used by replace
5649 * stack functions. VREPLACE does not use this, and backspaces
5650 * over the text instead.
5651 */
5652#ifdef FEAT_VREPLACE
5653 if (State & VREPLACE_FLAG)
5654 orig_col = startcol; /* Will start backspacing from here */
5655 else
5656#endif
5657 replace_offset = startcol - end_foundcol - 1;
5658
5659 /*
5660 * adjust startcol for spaces that will be deleted and
5661 * characters that will remain on top line
5662 */
5663 curwin->w_cursor.col = foundcol;
5664 while (cc = gchar_cursor(), WHITECHAR(cc))
5665 inc_cursor();
5666 startcol -= curwin->w_cursor.col;
5667 if (startcol < 0)
5668 startcol = 0;
5669
5670#ifdef FEAT_VREPLACE
5671 if (State & VREPLACE_FLAG)
5672 {
5673 /*
5674 * In VREPLACE mode, we will backspace over the text to be
5675 * wrapped, so save a copy now to put on the next line.
5676 */
5677 saved_text = vim_strsave(ml_get_cursor());
5678 curwin->w_cursor.col = orig_col;
5679 if (saved_text == NULL)
5680 break; /* Can't do it, out of memory */
5681 saved_text[startcol] = NUL;
5682
5683 /* Backspace over characters that will move to the next line */
5684 if (!fo_white_par)
5685 backspace_until_column(foundcol);
5686 }
5687 else
5688#endif
5689 {
5690 /* put cursor after pos. to break line */
5691 if (!fo_white_par)
5692 curwin->w_cursor.col = foundcol;
5693 }
5694
5695 /*
5696 * Split the line just before the margin.
5697 * Only insert/delete lines, but don't really redraw the window.
5698 */
5699 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5700 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5701#ifdef FEAT_COMMENTS
5702 + (do_comments ? OPENLINE_DO_COM : 0)
5703#endif
5704 , old_indent);
5705 old_indent = 0;
5706
5707 replace_offset = 0;
5708 if (first_line)
5709 {
5710 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5711 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5712 if (second_indent >= 0)
5713 {
5714#ifdef FEAT_VREPLACE
5715 if (State & VREPLACE_FLAG)
5716 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5717 else
5718#endif
5719 (void)set_indent(second_indent, SIN_CHANGED);
5720 }
5721 first_line = FALSE;
5722 }
5723
5724#ifdef FEAT_VREPLACE
5725 if (State & VREPLACE_FLAG)
5726 {
5727 /*
5728 * In VREPLACE mode we have backspaced over the text to be
5729 * moved, now we re-insert it into the new line.
5730 */
5731 ins_bytes(saved_text);
5732 vim_free(saved_text);
5733 }
5734 else
5735#endif
5736 {
5737 /*
5738 * Check if cursor is not past the NUL off the line, cindent
5739 * may have added or removed indent.
5740 */
5741 curwin->w_cursor.col += startcol;
5742 len = (colnr_T)STRLEN(ml_get_curline());
5743 if (curwin->w_cursor.col > len)
5744 curwin->w_cursor.col = len;
5745 }
5746
5747 haveto_redraw = TRUE;
5748#ifdef FEAT_CINDENT
5749 can_cindent = TRUE;
5750#endif
5751 /* moved the cursor, don't autoindent or cindent now */
5752 did_ai = FALSE;
5753#ifdef FEAT_SMARTINDENT
5754 did_si = FALSE;
5755 can_si = FALSE;
5756 can_si_back = FALSE;
5757#endif
5758 line_breakcheck();
5759 }
5760
5761 if (save_char != NUL) /* put back space after cursor */
5762 pchar_cursor(save_char);
5763
5764 if (!format_only && haveto_redraw)
5765 {
5766 update_topline();
5767 redraw_curbuf_later(VALID);
5768 }
5769}
5770
5771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 * Called after inserting or deleting text: When 'formatoptions' includes the
5773 * 'a' flag format from the current line until the end of the paragraph.
5774 * Keep the cursor at the same position relative to the text.
5775 * The caller must have saved the cursor line for undo, following ones will be
5776 * saved here.
5777 */
5778 void
5779auto_format(trailblank, prev_line)
5780 int trailblank; /* when TRUE also format with trailing blank */
5781 int prev_line; /* may start in previous line */
5782{
5783 pos_T pos;
5784 colnr_T len;
5785 char_u *old;
5786 char_u *new, *pnew;
5787 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005788 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
5790 if (!has_format_option(FO_AUTO))
5791 return;
5792
5793 pos = curwin->w_cursor;
5794 old = ml_get_curline();
5795
5796 /* may remove added space */
5797 check_auto_format(FALSE);
5798
5799 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5800 * user might insert normal text next. Also skip formatting when "1" is
5801 * in 'formatoptions' and there is a single character before the cursor.
5802 * Otherwise the line would be broken and when typing another non-white
5803 * next they are not joined back together. */
5804 wasatend = (pos.col == STRLEN(old));
5805 if (*old != NUL && !trailblank && wasatend)
5806 {
5807 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005808 cc = gchar_cursor();
5809 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5810 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005812 cc = gchar_cursor();
5813 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815 curwin->w_cursor = pos;
5816 return;
5817 }
5818 curwin->w_cursor = pos;
5819 }
5820
5821#ifdef FEAT_COMMENTS
5822 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5823 * comments. */
5824 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5825 && get_leader_len(old, NULL, FALSE) == 0)
5826 return;
5827#endif
5828
5829 /*
5830 * May start formatting in a previous line, so that after "x" a word is
5831 * moved to the previous line if it fits there now. Only when this is not
5832 * the start of a paragraph.
5833 */
5834 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5835 {
5836 --curwin->w_cursor.lnum;
5837 if (u_save_cursor() == FAIL)
5838 return;
5839 }
5840
5841 /*
5842 * Do the formatting and restore the cursor position. "saved_cursor" will
5843 * be adjusted for the text formatting.
5844 */
5845 saved_cursor = pos;
5846 format_lines((linenr_T)-1);
5847 curwin->w_cursor = saved_cursor;
5848 saved_cursor.lnum = 0;
5849
5850 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5851 {
5852 /* "cannot happen" */
5853 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5854 coladvance((colnr_T)MAXCOL);
5855 }
5856 else
5857 check_cursor_col();
5858
5859 /* Insert mode: If the cursor is now after the end of the line while it
5860 * previously wasn't, the line was broken. Because of the rule above we
5861 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5862 * formatted. */
5863 if (!wasatend && has_format_option(FO_WHITE_PAR))
5864 {
5865 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005866 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 if (curwin->w_cursor.col == len)
5868 {
5869 pnew = vim_strnsave(new, len + 2);
5870 pnew[len] = ' ';
5871 pnew[len + 1] = NUL;
5872 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5873 /* remove the space later */
5874 did_add_space = TRUE;
5875 }
5876 else
5877 /* may remove added space */
5878 check_auto_format(FALSE);
5879 }
5880
5881 check_cursor();
5882}
5883
5884/*
5885 * When an extra space was added to continue a paragraph for auto-formatting,
5886 * delete it now. The space must be under the cursor, just after the insert
5887 * position.
5888 */
5889 static void
5890check_auto_format(end_insert)
5891 int end_insert; /* TRUE when ending Insert mode */
5892{
5893 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005894 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
5896 if (did_add_space)
5897 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005898 cc = gchar_cursor();
5899 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 /* Somehow the space was removed already. */
5901 did_add_space = FALSE;
5902 else
5903 {
5904 if (!end_insert)
5905 {
5906 inc_cursor();
5907 c = gchar_cursor();
5908 dec_cursor();
5909 }
5910 if (c != NUL)
5911 {
5912 /* The space is no longer at the end of the line, delete it. */
5913 del_char(FALSE);
5914 did_add_space = FALSE;
5915 }
5916 }
5917 }
5918}
5919
5920/*
5921 * Find out textwidth to be used for formatting:
5922 * if 'textwidth' option is set, use it
5923 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5924 * if invalid value, use 0.
5925 * Set default to window width (maximum 79) for "gq" operator.
5926 */
5927 int
5928comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005929 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 int textwidth;
5932
5933 textwidth = curbuf->b_p_tw;
5934 if (textwidth == 0 && curbuf->b_p_wm)
5935 {
5936 /* The width is the window width minus 'wrapmargin' minus all the
5937 * things that add to the margin. */
5938 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5939#ifdef FEAT_CMDWIN
5940 if (cmdwin_type != 0)
5941 textwidth -= 1;
5942#endif
5943#ifdef FEAT_FOLDING
5944 textwidth -= curwin->w_p_fdc;
5945#endif
5946#ifdef FEAT_SIGNS
5947 if (curwin->w_buffer->b_signlist != NULL
5948# ifdef FEAT_NETBEANS_INTG
5949 || usingNetbeans
5950# endif
5951 )
5952 textwidth -= 1;
5953#endif
5954 if (curwin->w_p_nu)
5955 textwidth -= 8;
5956 }
5957 if (textwidth < 0)
5958 textwidth = 0;
5959 if (ff && textwidth == 0)
5960 {
5961 textwidth = W_WIDTH(curwin) - 1;
5962 if (textwidth > 79)
5963 textwidth = 79;
5964 }
5965 return textwidth;
5966}
5967
5968/*
5969 * Put a character in the redo buffer, for when just after a CTRL-V.
5970 */
5971 static void
5972redo_literal(c)
5973 int c;
5974{
5975 char_u buf[10];
5976
5977 /* Only digits need special treatment. Translate them into a string of
5978 * three digits. */
5979 if (VIM_ISDIGIT(c))
5980 {
5981 sprintf((char *)buf, "%03d", c);
5982 AppendToRedobuff(buf);
5983 }
5984 else
5985 AppendCharToRedobuff(c);
5986}
5987
5988/*
5989 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005990 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 */
5992 static void
5993start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005994 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 if (!arrow_used) /* something has been inserted */
5997 {
5998 AppendToRedobuff(ESC_STR);
5999 stop_insert(end_insert_pos, FALSE);
6000 arrow_used = TRUE; /* this means we stopped the current insert */
6001 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006002#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006003 check_spell_redraw();
6004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005}
6006
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006007#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006008/*
6009 * If we skipped highlighting word at cursor, do it now.
6010 * It may be skipped again, thus reset spell_redraw_lnum first.
6011 */
6012 static void
6013check_spell_redraw()
6014{
6015 if (spell_redraw_lnum != 0)
6016 {
6017 linenr_T lnum = spell_redraw_lnum;
6018
6019 spell_redraw_lnum = 0;
6020 redrawWinline(lnum, FALSE);
6021 }
6022}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006023
6024/*
6025 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6026 * spelled word, if there is one.
6027 */
6028 static void
6029spell_back_to_badword()
6030{
6031 pos_T tpos = curwin->w_cursor;
6032
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006033 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006034 if (curwin->w_cursor.col != tpos.col)
6035 start_arrow(&tpos);
6036}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006037#endif
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039/*
6040 * stop_arrow() is called before a change is made in insert mode.
6041 * If an arrow key has been used, start a new insertion.
6042 * Returns FAIL if undo is impossible, shouldn't insert then.
6043 */
6044 int
6045stop_arrow()
6046{
6047 if (arrow_used)
6048 {
6049 if (u_save_cursor() == OK)
6050 {
6051 arrow_used = FALSE;
6052 ins_need_undo = FALSE;
6053 }
6054 Insstart = curwin->w_cursor; /* new insertion starts here */
6055 Insstart_textlen = linetabsize(ml_get_curline());
6056 ai_col = 0;
6057#ifdef FEAT_VREPLACE
6058 if (State & VREPLACE_FLAG)
6059 {
6060 orig_line_count = curbuf->b_ml.ml_line_count;
6061 vr_lines_changed = 1;
6062 }
6063#endif
6064 ResetRedobuff();
6065 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006066 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 }
6068 else if (ins_need_undo)
6069 {
6070 if (u_save_cursor() == OK)
6071 ins_need_undo = FALSE;
6072 }
6073
6074#ifdef FEAT_FOLDING
6075 /* Always open fold at the cursor line when inserting something. */
6076 foldOpenCursor();
6077#endif
6078
6079 return (arrow_used || ins_need_undo ? FAIL : OK);
6080}
6081
6082/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006083 * Do a few things to stop inserting.
6084 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6085 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 */
6087 static void
6088stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006089 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006090 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006092 int cc;
6093 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
6095 stop_redo_ins();
6096 replace_flush(); /* abandon replace stack */
6097
6098 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006099 * Save the inserted text for later redo with ^@ and CTRL-A.
6100 * Don't do it when "restart_edit" was set and nothing was inserted,
6101 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006103 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006104 if (did_restart_edit == 0 || (ptr != NULL
6105 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006106 {
6107 vim_free(last_insert);
6108 last_insert = ptr;
6109 last_insert_skip = new_insert_skip;
6110 }
6111 else
6112 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006114 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
6116 /* Auto-format now. It may seem strange to do this when stopping an
6117 * insertion (or moving the cursor), but it's required when appending
6118 * a line and having it end in a space. But only do it when something
6119 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006120 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006122 pos_T tpos = curwin->w_cursor;
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 /* When the cursor is at the end of the line after a space the
6125 * formatting will move it to the following word. Avoid that by
6126 * moving the cursor onto the space. */
6127 cc = 'x';
6128 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6129 {
6130 dec_cursor();
6131 cc = gchar_cursor();
6132 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006133 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135
6136 auto_format(TRUE, FALSE);
6137
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006138 if (vim_iswhite(cc))
6139 {
6140 if (gchar_cursor() != NUL)
6141 inc_cursor();
6142#ifdef FEAT_VIRTUALEDIT
6143 /* If the cursor is still at the same character, also keep
6144 * the "coladd". */
6145 if (gchar_cursor() == NUL
6146 && curwin->w_cursor.lnum == tpos.lnum
6147 && curwin->w_cursor.col == tpos.col)
6148 curwin->w_cursor.coladd = tpos.coladd;
6149#endif
6150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 }
6152
6153 /* If a space was inserted for auto-formatting, remove it now. */
6154 check_auto_format(TRUE);
6155
6156 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006157 * of the line, and put the cursor back.
6158 * Do this when ESC was used or moving the cursor up/down. */
6159 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006160 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006162 pos_T tpos = curwin->w_cursor;
6163
6164 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006165 for (;;)
6166 {
6167 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6168 --curwin->w_cursor.col;
6169 cc = gchar_cursor();
6170 if (!vim_iswhite(cc))
6171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006173 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006174 if (curwin->w_cursor.lnum != tpos.lnum)
6175 curwin->w_cursor = tpos;
6176 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6178
6179#ifdef FEAT_VISUAL
6180 /* <C-S-Right> may have started Visual mode, adjust the position for
6181 * deleted characters. */
6182 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6183 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006184 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (VIsual.col > (colnr_T)cc)
6186 {
6187 VIsual.col = cc;
6188# ifdef FEAT_VIRTUALEDIT
6189 VIsual.coladd = 0;
6190# endif
6191 }
6192 }
6193#endif
6194 }
6195 }
6196 did_ai = FALSE;
6197#ifdef FEAT_SMARTINDENT
6198 did_si = FALSE;
6199 can_si = FALSE;
6200 can_si_back = FALSE;
6201#endif
6202
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006203 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6204 * now in a different buffer. */
6205 if (end_insert_pos != NULL)
6206 {
6207 curbuf->b_op_start = Insstart;
6208 curbuf->b_op_end = *end_insert_pos;
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210}
6211
6212/*
6213 * Set the last inserted text to a single character.
6214 * Used for the replace command.
6215 */
6216 void
6217set_last_insert(c)
6218 int c;
6219{
6220 char_u *s;
6221
6222 vim_free(last_insert);
6223#ifdef FEAT_MBYTE
6224 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6225#else
6226 last_insert = alloc(6);
6227#endif
6228 if (last_insert != NULL)
6229 {
6230 s = last_insert;
6231 /* Use the CTRL-V only when entering a special char */
6232 if (c < ' ' || c == DEL)
6233 *s++ = Ctrl_V;
6234 s = add_char2buf(c, s);
6235 *s++ = ESC;
6236 *s++ = NUL;
6237 last_insert_skip = 0;
6238 }
6239}
6240
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006241#if defined(EXITFREE) || defined(PROTO)
6242 void
6243free_last_insert()
6244{
6245 vim_free(last_insert);
6246 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006247 vim_free(compl_orig_text);
6248 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006249}
6250#endif
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252/*
6253 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6254 * and CSI. Handle multi-byte characters.
6255 * Returns a pointer to after the added bytes.
6256 */
6257 char_u *
6258add_char2buf(c, s)
6259 int c;
6260 char_u *s;
6261{
6262#ifdef FEAT_MBYTE
6263 char_u temp[MB_MAXBYTES];
6264 int i;
6265 int len;
6266
6267 len = (*mb_char2bytes)(c, temp);
6268 for (i = 0; i < len; ++i)
6269 {
6270 c = temp[i];
6271#endif
6272 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6273 if (c == K_SPECIAL)
6274 {
6275 *s++ = K_SPECIAL;
6276 *s++ = KS_SPECIAL;
6277 *s++ = KE_FILLER;
6278 }
6279#ifdef FEAT_GUI
6280 else if (c == CSI)
6281 {
6282 *s++ = CSI;
6283 *s++ = KS_EXTRA;
6284 *s++ = (int)KE_CSI;
6285 }
6286#endif
6287 else
6288 *s++ = c;
6289#ifdef FEAT_MBYTE
6290 }
6291#endif
6292 return s;
6293}
6294
6295/*
6296 * move cursor to start of line
6297 * if flags & BL_WHITE move to first non-white
6298 * if flags & BL_SOL move to first non-white if startofline is set,
6299 * otherwise keep "curswant" column
6300 * if flags & BL_FIX don't leave the cursor on a NUL.
6301 */
6302 void
6303beginline(flags)
6304 int flags;
6305{
6306 if ((flags & BL_SOL) && !p_sol)
6307 coladvance(curwin->w_curswant);
6308 else
6309 {
6310 curwin->w_cursor.col = 0;
6311#ifdef FEAT_VIRTUALEDIT
6312 curwin->w_cursor.coladd = 0;
6313#endif
6314
6315 if (flags & (BL_WHITE | BL_SOL))
6316 {
6317 char_u *ptr;
6318
6319 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6320 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6321 ++curwin->w_cursor.col;
6322 }
6323 curwin->w_set_curswant = TRUE;
6324 }
6325}
6326
6327/*
6328 * oneright oneleft cursor_down cursor_up
6329 *
6330 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006331 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 * Return OK when successful, FAIL when we hit a line of file boundary.
6333 */
6334
6335 int
6336oneright()
6337{
6338 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340
6341#ifdef FEAT_VIRTUALEDIT
6342 if (virtual_active())
6343 {
6344 pos_T prevpos = curwin->w_cursor;
6345
6346 /* Adjust for multi-wide char (excluding TAB) */
6347 ptr = ml_get_cursor();
6348 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006349# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006351# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ))
6355 ? ptr2cells(ptr) : 1));
6356 curwin->w_set_curswant = TRUE;
6357 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6358 return (prevpos.col != curwin->w_cursor.col
6359 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6360 }
6361#endif
6362
6363 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006364 if (*ptr == NUL)
6365 return FAIL; /* already at the very end */
6366
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006368 if (has_mbyte)
6369 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 else
6371#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006372 l = 1;
6373
6374 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6375 * contains "onemore". */
6376 if (ptr[l] == NUL
6377#ifdef FEAT_VIRTUALEDIT
6378 && (ve_flags & VE_ONEMORE) == 0
6379#endif
6380 )
6381 return FAIL;
6382 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384 curwin->w_set_curswant = TRUE;
6385 return OK;
6386}
6387
6388 int
6389oneleft()
6390{
6391#ifdef FEAT_VIRTUALEDIT
6392 if (virtual_active())
6393 {
6394 int width;
6395 int v = getviscol();
6396
6397 if (v == 0)
6398 return FAIL;
6399
6400# ifdef FEAT_LINEBREAK
6401 /* We might get stuck on 'showbreak', skip over it. */
6402 width = 1;
6403 for (;;)
6404 {
6405 coladvance(v - width);
6406 /* getviscol() is slow, skip it when 'showbreak' is empty and
6407 * there are no multi-byte characters */
6408 if ((*p_sbr == NUL
6409# ifdef FEAT_MBYTE
6410 && !has_mbyte
6411# endif
6412 ) || getviscol() < v)
6413 break;
6414 ++width;
6415 }
6416# else
6417 coladvance(v - 1);
6418# endif
6419
6420 if (curwin->w_cursor.coladd == 1)
6421 {
6422 char_u *ptr;
6423
6424 /* Adjust for multi-wide char (not a TAB) */
6425 ptr = ml_get_cursor();
6426 if (*ptr != TAB && vim_isprintc(
6427# ifdef FEAT_MBYTE
6428 (*mb_ptr2char)(ptr)
6429# else
6430 *ptr
6431# endif
6432 ) && ptr2cells(ptr) > 1)
6433 curwin->w_cursor.coladd = 0;
6434 }
6435
6436 curwin->w_set_curswant = TRUE;
6437 return OK;
6438 }
6439#endif
6440
6441 if (curwin->w_cursor.col == 0)
6442 return FAIL;
6443
6444 curwin->w_set_curswant = TRUE;
6445 --curwin->w_cursor.col;
6446
6447#ifdef FEAT_MBYTE
6448 /* if the character on the left of the current cursor is a multi-byte
6449 * character, move to its first byte */
6450 if (has_mbyte)
6451 mb_adjust_cursor();
6452#endif
6453 return OK;
6454}
6455
6456 int
6457cursor_up(n, upd_topline)
6458 long n;
6459 int upd_topline; /* When TRUE: update topline */
6460{
6461 linenr_T lnum;
6462
6463 if (n > 0)
6464 {
6465 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006466 /* This fails if the cursor is already in the first line or the count
6467 * is larger than the line number and '-' is in 'cpoptions' */
6468 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 return FAIL;
6470 if (n >= lnum)
6471 lnum = 1;
6472 else
6473#ifdef FEAT_FOLDING
6474 if (hasAnyFolding(curwin))
6475 {
6476 /*
6477 * Count each sequence of folded lines as one logical line.
6478 */
6479 /* go to the the start of the current fold */
6480 (void)hasFolding(lnum, &lnum, NULL);
6481
6482 while (n--)
6483 {
6484 /* move up one line */
6485 --lnum;
6486 if (lnum <= 1)
6487 break;
6488 /* If we entered a fold, move to the beginning, unless in
6489 * Insert mode or when 'foldopen' contains "all": it will open
6490 * in a moment. */
6491 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6492 (void)hasFolding(lnum, &lnum, NULL);
6493 }
6494 if (lnum < 1)
6495 lnum = 1;
6496 }
6497 else
6498#endif
6499 lnum -= n;
6500 curwin->w_cursor.lnum = lnum;
6501 }
6502
6503 /* try to advance to the column we want to be at */
6504 coladvance(curwin->w_curswant);
6505
6506 if (upd_topline)
6507 update_topline(); /* make sure curwin->w_topline is valid */
6508
6509 return OK;
6510}
6511
6512/*
6513 * Cursor down a number of logical lines.
6514 */
6515 int
6516cursor_down(n, upd_topline)
6517 long n;
6518 int upd_topline; /* When TRUE: update topline */
6519{
6520 linenr_T lnum;
6521
6522 if (n > 0)
6523 {
6524 lnum = curwin->w_cursor.lnum;
6525#ifdef FEAT_FOLDING
6526 /* Move to last line of fold, will fail if it's the end-of-file. */
6527 (void)hasFolding(lnum, NULL, &lnum);
6528#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006529 /* This fails if the cursor is already in the last line or would move
6530 * beyound the last line and '-' is in 'cpoptions' */
6531 if (lnum >= curbuf->b_ml.ml_line_count
6532 || (lnum + n > curbuf->b_ml.ml_line_count
6533 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 return FAIL;
6535 if (lnum + n >= curbuf->b_ml.ml_line_count)
6536 lnum = curbuf->b_ml.ml_line_count;
6537 else
6538#ifdef FEAT_FOLDING
6539 if (hasAnyFolding(curwin))
6540 {
6541 linenr_T last;
6542
6543 /* count each sequence of folded lines as one logical line */
6544 while (n--)
6545 {
6546 if (hasFolding(lnum, NULL, &last))
6547 lnum = last + 1;
6548 else
6549 ++lnum;
6550 if (lnum >= curbuf->b_ml.ml_line_count)
6551 break;
6552 }
6553 if (lnum > curbuf->b_ml.ml_line_count)
6554 lnum = curbuf->b_ml.ml_line_count;
6555 }
6556 else
6557#endif
6558 lnum += n;
6559 curwin->w_cursor.lnum = lnum;
6560 }
6561
6562 /* try to advance to the column we want to be at */
6563 coladvance(curwin->w_curswant);
6564
6565 if (upd_topline)
6566 update_topline(); /* make sure curwin->w_topline is valid */
6567
6568 return OK;
6569}
6570
6571/*
6572 * Stuff the last inserted text in the read buffer.
6573 * Last_insert actually is a copy of the redo buffer, so we
6574 * first have to remove the command.
6575 */
6576 int
6577stuff_inserted(c, count, no_esc)
6578 int c; /* Command character to be inserted */
6579 long count; /* Repeat this many times */
6580 int no_esc; /* Don't add an ESC at the end */
6581{
6582 char_u *esc_ptr;
6583 char_u *ptr;
6584 char_u *last_ptr;
6585 char_u last = NUL;
6586
6587 ptr = get_last_insert();
6588 if (ptr == NULL)
6589 {
6590 EMSG(_(e_noinstext));
6591 return FAIL;
6592 }
6593
6594 /* may want to stuff the command character, to start Insert mode */
6595 if (c != NUL)
6596 stuffcharReadbuff(c);
6597 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6598 *esc_ptr = NUL; /* remove the ESC */
6599
6600 /* when the last char is either "0" or "^" it will be quoted if no ESC
6601 * comes after it OR if it will inserted more than once and "ptr"
6602 * starts with ^D. -- Acevedo
6603 */
6604 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6605 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6606 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6607 {
6608 last = *last_ptr;
6609 *last_ptr = NUL;
6610 }
6611
6612 do
6613 {
6614 stuffReadbuff(ptr);
6615 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6616 if (last)
6617 stuffReadbuff((char_u *)(last == '0'
6618 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6619 : IF_EB("\026^", CTRL_V_STR "^")));
6620 }
6621 while (--count > 0);
6622
6623 if (last)
6624 *last_ptr = last;
6625
6626 if (esc_ptr != NULL)
6627 *esc_ptr = ESC; /* put the ESC back */
6628
6629 /* may want to stuff a trailing ESC, to get out of Insert mode */
6630 if (!no_esc)
6631 stuffcharReadbuff(ESC);
6632
6633 return OK;
6634}
6635
6636 char_u *
6637get_last_insert()
6638{
6639 if (last_insert == NULL)
6640 return NULL;
6641 return last_insert + last_insert_skip;
6642}
6643
6644/*
6645 * Get last inserted string, and remove trailing <Esc>.
6646 * Returns pointer to allocated memory (must be freed) or NULL.
6647 */
6648 char_u *
6649get_last_insert_save()
6650{
6651 char_u *s;
6652 int len;
6653
6654 if (last_insert == NULL)
6655 return NULL;
6656 s = vim_strsave(last_insert + last_insert_skip);
6657 if (s != NULL)
6658 {
6659 len = (int)STRLEN(s);
6660 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6661 s[len - 1] = NUL;
6662 }
6663 return s;
6664}
6665
6666/*
6667 * Check the word in front of the cursor for an abbreviation.
6668 * Called when the non-id character "c" has been entered.
6669 * When an abbreviation is recognized it is removed from the text and
6670 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6671 */
6672 static int
6673echeck_abbr(c)
6674 int c;
6675{
6676 /* Don't check for abbreviation in paste mode, when disabled and just
6677 * after moving around with cursor keys. */
6678 if (p_paste || no_abbr || arrow_used)
6679 return FALSE;
6680
6681 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6682 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6683}
6684
6685/*
6686 * replace-stack functions
6687 *
6688 * When replacing characters, the replaced characters are remembered for each
6689 * new character. This is used to re-insert the old text when backspacing.
6690 *
6691 * There is a NUL headed list of characters for each character that is
6692 * currently in the file after the insertion point. When BS is used, one NUL
6693 * headed list is put back for the deleted character.
6694 *
6695 * For a newline, there are two NUL headed lists. One contains the characters
6696 * that the NL replaced. The extra one stores the characters after the cursor
6697 * that were deleted (always white space).
6698 *
6699 * Replace_offset is normally 0, in which case replace_push will add a new
6700 * character at the end of the stack. If replace_offset is not 0, that many
6701 * characters will be left on the stack above the newly inserted character.
6702 */
6703
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006704static char_u *replace_stack = NULL;
6705static long replace_stack_nr = 0; /* next entry in replace stack */
6706static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708 void
6709replace_push(c)
6710 int c; /* character that is replaced (NUL is none) */
6711{
6712 char_u *p;
6713
6714 if (replace_stack_nr < replace_offset) /* nothing to do */
6715 return;
6716 if (replace_stack_len <= replace_stack_nr)
6717 {
6718 replace_stack_len += 50;
6719 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6720 if (p == NULL) /* out of memory */
6721 {
6722 replace_stack_len -= 50;
6723 return;
6724 }
6725 if (replace_stack != NULL)
6726 {
6727 mch_memmove(p, replace_stack,
6728 (size_t)(replace_stack_nr * sizeof(char_u)));
6729 vim_free(replace_stack);
6730 }
6731 replace_stack = p;
6732 }
6733 p = replace_stack + replace_stack_nr - replace_offset;
6734 if (replace_offset)
6735 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6736 *p = c;
6737 ++replace_stack_nr;
6738}
6739
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006740#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741/*
6742 * call replace_push(c) with replace_offset set to the first NUL.
6743 */
6744 static void
6745replace_push_off(c)
6746 int c;
6747{
6748 char_u *p;
6749
6750 p = replace_stack + replace_stack_nr;
6751 for (replace_offset = 1; replace_offset < replace_stack_nr;
6752 ++replace_offset)
6753 if (*--p == NUL)
6754 break;
6755 replace_push(c);
6756 replace_offset = 0;
6757}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760/*
6761 * Pop one item from the replace stack.
6762 * return -1 if stack empty
6763 * return replaced character or NUL otherwise
6764 */
6765 static int
6766replace_pop()
6767{
6768 if (replace_stack_nr == 0)
6769 return -1;
6770 return (int)replace_stack[--replace_stack_nr];
6771}
6772
6773/*
6774 * Join the top two items on the replace stack. This removes to "off"'th NUL
6775 * encountered.
6776 */
6777 static void
6778replace_join(off)
6779 int off; /* offset for which NUL to remove */
6780{
6781 int i;
6782
6783 for (i = replace_stack_nr; --i >= 0; )
6784 if (replace_stack[i] == NUL && off-- <= 0)
6785 {
6786 --replace_stack_nr;
6787 mch_memmove(replace_stack + i, replace_stack + i + 1,
6788 (size_t)(replace_stack_nr - i));
6789 return;
6790 }
6791}
6792
6793/*
6794 * Pop bytes from the replace stack until a NUL is found, and insert them
6795 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6796 */
6797 static void
6798replace_pop_ins()
6799{
6800 int cc;
6801 int oldState = State;
6802
6803 State = NORMAL; /* don't want REPLACE here */
6804 while ((cc = replace_pop()) > 0)
6805 {
6806#ifdef FEAT_MBYTE
6807 mb_replace_pop_ins(cc);
6808#else
6809 ins_char(cc);
6810#endif
6811 dec_cursor();
6812 }
6813 State = oldState;
6814}
6815
6816#ifdef FEAT_MBYTE
6817/*
6818 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6819 * indicates a multi-byte char, pop the other bytes too.
6820 */
6821 static void
6822mb_replace_pop_ins(cc)
6823 int cc;
6824{
6825 int n;
6826 char_u buf[MB_MAXBYTES];
6827 int i;
6828 int c;
6829
6830 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6831 {
6832 buf[0] = cc;
6833 for (i = 1; i < n; ++i)
6834 buf[i] = replace_pop();
6835 ins_bytes_len(buf, n);
6836 }
6837 else
6838 ins_char(cc);
6839
6840 if (enc_utf8)
6841 /* Handle composing chars. */
6842 for (;;)
6843 {
6844 c = replace_pop();
6845 if (c == -1) /* stack empty */
6846 break;
6847 if ((n = MB_BYTE2LEN(c)) == 1)
6848 {
6849 /* Not a multi-byte char, put it back. */
6850 replace_push(c);
6851 break;
6852 }
6853 else
6854 {
6855 buf[0] = c;
6856 for (i = 1; i < n; ++i)
6857 buf[i] = replace_pop();
6858 if (utf_iscomposing(utf_ptr2char(buf)))
6859 ins_bytes_len(buf, n);
6860 else
6861 {
6862 /* Not a composing char, put it back. */
6863 for (i = n - 1; i >= 0; --i)
6864 replace_push(buf[i]);
6865 break;
6866 }
6867 }
6868 }
6869}
6870#endif
6871
6872/*
6873 * make the replace stack empty
6874 * (called when exiting replace mode)
6875 */
6876 static void
6877replace_flush()
6878{
6879 vim_free(replace_stack);
6880 replace_stack = NULL;
6881 replace_stack_len = 0;
6882 replace_stack_nr = 0;
6883}
6884
6885/*
6886 * Handle doing a BS for one character.
6887 * cc < 0: replace stack empty, just move cursor
6888 * cc == 0: character was inserted, delete it
6889 * cc > 0: character was replaced, put cc (first byte of original char) back
6890 * and check for more characters to be put back
6891 */
6892 static void
6893replace_do_bs()
6894{
6895 int cc;
6896#ifdef FEAT_VREPLACE
6897 int orig_len = 0;
6898 int ins_len;
6899 int orig_vcols = 0;
6900 colnr_T start_vcol;
6901 char_u *p;
6902 int i;
6903 int vcol;
6904#endif
6905
6906 cc = replace_pop();
6907 if (cc > 0)
6908 {
6909#ifdef FEAT_VREPLACE
6910 if (State & VREPLACE_FLAG)
6911 {
6912 /* Get the number of screen cells used by the character we are
6913 * going to delete. */
6914 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6915 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6916 }
6917#endif
6918#ifdef FEAT_MBYTE
6919 if (has_mbyte)
6920 {
6921 del_char(FALSE);
6922# ifdef FEAT_VREPLACE
6923 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006924 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925# endif
6926 replace_push(cc);
6927 }
6928 else
6929#endif
6930 {
6931 pchar_cursor(cc);
6932#ifdef FEAT_VREPLACE
6933 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006934 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935#endif
6936 }
6937 replace_pop_ins();
6938
6939#ifdef FEAT_VREPLACE
6940 if (State & VREPLACE_FLAG)
6941 {
6942 /* Get the number of screen cells used by the inserted characters */
6943 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006944 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 vcol = start_vcol;
6946 for (i = 0; i < ins_len; ++i)
6947 {
6948 vcol += chartabsize(p + i, vcol);
6949#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006950 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951#endif
6952 }
6953 vcol -= start_vcol;
6954
6955 /* Delete spaces that were inserted after the cursor to keep the
6956 * text aligned. */
6957 curwin->w_cursor.col += ins_len;
6958 while (vcol > orig_vcols && gchar_cursor() == ' ')
6959 {
6960 del_char(FALSE);
6961 ++orig_vcols;
6962 }
6963 curwin->w_cursor.col -= ins_len;
6964 }
6965#endif
6966
6967 /* mark the buffer as changed and prepare for displaying */
6968 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6969 }
6970 else if (cc == 0)
6971 (void)del_char(FALSE);
6972}
6973
6974#ifdef FEAT_CINDENT
6975/*
6976 * Return TRUE if C-indenting is on.
6977 */
6978 static int
6979cindent_on()
6980{
6981 return (!p_paste && (curbuf->b_p_cin
6982# ifdef FEAT_EVAL
6983 || *curbuf->b_p_inde != NUL
6984# endif
6985 ));
6986}
6987#endif
6988
6989#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6990/*
6991 * Re-indent the current line, based on the current contents of it and the
6992 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6993 * confused what all the part that handles Control-T is doing that I'm not.
6994 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6995 */
6996
6997 void
6998fixthisline(get_the_indent)
6999 int (*get_the_indent) __ARGS((void));
7000{
7001 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7002 if (linewhite(curwin->w_cursor.lnum))
7003 did_ai = TRUE; /* delete the indent if the line stays empty */
7004}
7005
7006 void
7007fix_indent()
7008{
7009 if (p_paste)
7010 return;
7011# ifdef FEAT_LISP
7012 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7013 fixthisline(get_lisp_indent);
7014# endif
7015# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7016 else
7017# endif
7018# ifdef FEAT_CINDENT
7019 if (cindent_on())
7020 do_c_expr_indent();
7021# endif
7022}
7023
7024#endif
7025
7026#ifdef FEAT_CINDENT
7027/*
7028 * return TRUE if 'cinkeys' contains the key "keytyped",
7029 * when == '*': Only if key is preceded with '*' (indent before insert)
7030 * when == '!': Only if key is prededed with '!' (don't insert)
7031 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7032 *
7033 * "keytyped" can have a few special values:
7034 * KEY_OPEN_FORW
7035 * KEY_OPEN_BACK
7036 * KEY_COMPLETE just finished completion.
7037 *
7038 * If line_is_empty is TRUE accept keys with '0' before them.
7039 */
7040 int
7041in_cinkeys(keytyped, when, line_is_empty)
7042 int keytyped;
7043 int when;
7044 int line_is_empty;
7045{
7046 char_u *look;
7047 int try_match;
7048 int try_match_word;
7049 char_u *p;
7050 char_u *line;
7051 int icase;
7052 int i;
7053
7054#ifdef FEAT_EVAL
7055 if (*curbuf->b_p_inde != NUL)
7056 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7057 else
7058#endif
7059 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7060 while (*look)
7061 {
7062 /*
7063 * Find out if we want to try a match with this key, depending on
7064 * 'when' and a '*' or '!' before the key.
7065 */
7066 switch (when)
7067 {
7068 case '*': try_match = (*look == '*'); break;
7069 case '!': try_match = (*look == '!'); break;
7070 default: try_match = (*look != '*'); break;
7071 }
7072 if (*look == '*' || *look == '!')
7073 ++look;
7074
7075 /*
7076 * If there is a '0', only accept a match if the line is empty.
7077 * But may still match when typing last char of a word.
7078 */
7079 if (*look == '0')
7080 {
7081 try_match_word = try_match;
7082 if (!line_is_empty)
7083 try_match = FALSE;
7084 ++look;
7085 }
7086 else
7087 try_match_word = FALSE;
7088
7089 /*
7090 * does it look like a control character?
7091 */
7092 if (*look == '^'
7093#ifdef EBCDIC
7094 && (Ctrl_chr(look[1]) != 0)
7095#else
7096 && look[1] >= '?' && look[1] <= '_'
7097#endif
7098 )
7099 {
7100 if (try_match && keytyped == Ctrl_chr(look[1]))
7101 return TRUE;
7102 look += 2;
7103 }
7104 /*
7105 * 'o' means "o" command, open forward.
7106 * 'O' means "O" command, open backward.
7107 */
7108 else if (*look == 'o')
7109 {
7110 if (try_match && keytyped == KEY_OPEN_FORW)
7111 return TRUE;
7112 ++look;
7113 }
7114 else if (*look == 'O')
7115 {
7116 if (try_match && keytyped == KEY_OPEN_BACK)
7117 return TRUE;
7118 ++look;
7119 }
7120
7121 /*
7122 * 'e' means to check for "else" at start of line and just before the
7123 * cursor.
7124 */
7125 else if (*look == 'e')
7126 {
7127 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7128 {
7129 p = ml_get_curline();
7130 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7131 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7132 return TRUE;
7133 }
7134 ++look;
7135 }
7136
7137 /*
7138 * ':' only causes an indent if it is at the end of a label or case
7139 * statement, or when it was before typing the ':' (to fix
7140 * class::method for C++).
7141 */
7142 else if (*look == ':')
7143 {
7144 if (try_match && keytyped == ':')
7145 {
7146 p = ml_get_curline();
7147 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7148 return TRUE;
7149 if (curwin->w_cursor.col > 2
7150 && p[curwin->w_cursor.col - 1] == ':'
7151 && p[curwin->w_cursor.col - 2] == ':')
7152 {
7153 p[curwin->w_cursor.col - 1] = ' ';
7154 i = (cin_iscase(p) || cin_isscopedecl(p)
7155 || cin_islabel(30));
7156 p = ml_get_curline();
7157 p[curwin->w_cursor.col - 1] = ':';
7158 if (i)
7159 return TRUE;
7160 }
7161 }
7162 ++look;
7163 }
7164
7165
7166 /*
7167 * Is it a key in <>, maybe?
7168 */
7169 else if (*look == '<')
7170 {
7171 if (try_match)
7172 {
7173 /*
7174 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7175 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7176 * >, *, : and ! keys if they really really want to.
7177 */
7178 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7179 && keytyped == look[1])
7180 return TRUE;
7181
7182 if (keytyped == get_special_key_code(look + 1))
7183 return TRUE;
7184 }
7185 while (*look && *look != '>')
7186 look++;
7187 while (*look == '>')
7188 look++;
7189 }
7190
7191 /*
7192 * Is it a word: "=word"?
7193 */
7194 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7195 {
7196 ++look;
7197 if (*look == '~')
7198 {
7199 icase = TRUE;
7200 ++look;
7201 }
7202 else
7203 icase = FALSE;
7204 p = vim_strchr(look, ',');
7205 if (p == NULL)
7206 p = look + STRLEN(look);
7207 if ((try_match || try_match_word)
7208 && curwin->w_cursor.col >= (colnr_T)(p - look))
7209 {
7210 int match = FALSE;
7211
7212#ifdef FEAT_INS_EXPAND
7213 if (keytyped == KEY_COMPLETE)
7214 {
7215 char_u *s;
7216
7217 /* Just completed a word, check if it starts with "look".
7218 * search back for the start of a word. */
7219 line = ml_get_curline();
7220# ifdef FEAT_MBYTE
7221 if (has_mbyte)
7222 {
7223 char_u *n;
7224
7225 for (s = line + curwin->w_cursor.col; s > line; s = n)
7226 {
7227 n = mb_prevptr(line, s);
7228 if (!vim_iswordp(n))
7229 break;
7230 }
7231 }
7232 else
7233# endif
7234 for (s = line + curwin->w_cursor.col; s > line; --s)
7235 if (!vim_iswordc(s[-1]))
7236 break;
7237 if (s + (p - look) <= line + curwin->w_cursor.col
7238 && (icase
7239 ? MB_STRNICMP(s, look, p - look)
7240 : STRNCMP(s, look, p - look)) == 0)
7241 match = TRUE;
7242 }
7243 else
7244#endif
7245 /* TODO: multi-byte */
7246 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7247 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7248 {
7249 line = ml_get_cursor();
7250 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7251 || !vim_iswordc(line[-(p - look) - 1]))
7252 && (icase
7253 ? MB_STRNICMP(line - (p - look), look, p - look)
7254 : STRNCMP(line - (p - look), look, p - look))
7255 == 0)
7256 match = TRUE;
7257 }
7258 if (match && try_match_word && !try_match)
7259 {
7260 /* "0=word": Check if there are only blanks before the
7261 * word. */
7262 line = ml_get_curline();
7263 if ((int)(skipwhite(line) - line) !=
7264 (int)(curwin->w_cursor.col - (p - look)))
7265 match = FALSE;
7266 }
7267 if (match)
7268 return TRUE;
7269 }
7270 look = p;
7271 }
7272
7273 /*
7274 * ok, it's a boring generic character.
7275 */
7276 else
7277 {
7278 if (try_match && *look == keytyped)
7279 return TRUE;
7280 ++look;
7281 }
7282
7283 /*
7284 * Skip over ", ".
7285 */
7286 look = skip_to_option_part(look);
7287 }
7288 return FALSE;
7289}
7290#endif /* FEAT_CINDENT */
7291
7292#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7293/*
7294 * Map Hebrew keyboard when in hkmap mode.
7295 */
7296 int
7297hkmap(c)
7298 int c;
7299{
7300 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7301 {
7302 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7303 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7304 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7305 static char_u map[26] =
7306 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7307 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7308 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7309 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7310 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7311 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7312 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7313 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7314 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7315
7316 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7317 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7318 /* '-1'='sofit' */
7319 else if (c == 'x')
7320 return 'X';
7321 else if (c == 'q')
7322 return '\''; /* {geresh}={'} */
7323 else if (c == 246)
7324 return ' '; /* \"o --> ' ' for a german keyboard */
7325 else if (c == 228)
7326 return ' '; /* \"a --> ' ' -- / -- */
7327 else if (c == 252)
7328 return ' '; /* \"u --> ' ' -- / -- */
7329#ifdef EBCDIC
7330 else if (islower(c))
7331#else
7332 /* NOTE: islower() does not do the right thing for us on Linux so we
7333 * do this the same was as 5.7 and previous, so it works correctly on
7334 * all systems. Specifically, the e.g. Delete and Arrow keys are
7335 * munged and won't work if e.g. searching for Hebrew text.
7336 */
7337 else if (c >= 'a' && c <= 'z')
7338#endif
7339 return (int)(map[CharOrdLow(c)] + p_aleph);
7340 else
7341 return c;
7342 }
7343 else
7344 {
7345 switch (c)
7346 {
7347 case '`': return ';';
7348 case '/': return '.';
7349 case '\'': return ',';
7350 case 'q': return '/';
7351 case 'w': return '\'';
7352
7353 /* Hebrew letters - set offset from 'a' */
7354 case ',': c = '{'; break;
7355 case '.': c = 'v'; break;
7356 case ';': c = 't'; break;
7357 default: {
7358 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7359
7360#ifdef EBCDIC
7361 /* see note about islower() above */
7362 if (!islower(c))
7363#else
7364 if (c < 'a' || c > 'z')
7365#endif
7366 return c;
7367 c = str[CharOrdLow(c)];
7368 break;
7369 }
7370 }
7371
7372 return (int)(CharOrdLow(c) + p_aleph);
7373 }
7374}
7375#endif
7376
7377 static void
7378ins_reg()
7379{
7380 int need_redraw = FALSE;
7381 int regname;
7382 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007383#ifdef FEAT_VISUAL
7384 int vis_active = VIsual_active;
7385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386
7387 /*
7388 * If we are going to wait for a character, show a '"'.
7389 */
7390 pc_status = PC_STATUS_UNSET;
7391 if (redrawing() && !char_avail())
7392 {
7393 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007394 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 edit_putchar('"', TRUE);
7397#ifdef FEAT_CMDL_INFO
7398 add_to_showcmd_c(Ctrl_R);
7399#endif
7400 }
7401
7402#ifdef USE_ON_FLY_SCROLL
7403 dont_scroll = TRUE; /* disallow scrolling here */
7404#endif
7405
7406 /*
7407 * Don't map the register name. This also prevents the mode message to be
7408 * deleted when ESC is hit.
7409 */
7410 ++no_mapping;
7411 regname = safe_vgetc();
7412#ifdef FEAT_LANGMAP
7413 LANGMAP_ADJUST(regname, TRUE);
7414#endif
7415 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7416 {
7417 /* Get a third key for literal register insertion */
7418 literally = regname;
7419#ifdef FEAT_CMDL_INFO
7420 add_to_showcmd_c(literally);
7421#endif
7422 regname = safe_vgetc();
7423#ifdef FEAT_LANGMAP
7424 LANGMAP_ADJUST(regname, TRUE);
7425#endif
7426 }
7427 --no_mapping;
7428
7429#ifdef FEAT_EVAL
7430 /*
7431 * Don't call u_sync() while getting the expression,
7432 * evaluating it or giving an error message for it!
7433 */
7434 ++no_u_sync;
7435 if (regname == '=')
7436 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007437# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007441# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 /* Restore the Input Method. */
7443 if (im_on)
7444 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007447 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7448 {
7449 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 else
7453 {
7454#endif
7455 if (literally == Ctrl_O || literally == Ctrl_P)
7456 {
7457 /* Append the command to the redo buffer. */
7458 AppendCharToRedobuff(Ctrl_R);
7459 AppendCharToRedobuff(literally);
7460 AppendCharToRedobuff(regname);
7461
7462 do_put(regname, BACKWARD, 1L,
7463 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7464 }
7465 else if (insert_reg(regname, literally) == FAIL)
7466 {
7467 vim_beep();
7468 need_redraw = TRUE; /* remove the '"' */
7469 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007470 else if (stop_insert_mode)
7471 /* When the '=' register was used and a function was invoked that
7472 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7473 * insert anything, need to remove the '"' */
7474 need_redraw = TRUE;
7475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476#ifdef FEAT_EVAL
7477 }
7478 --no_u_sync;
7479#endif
7480#ifdef FEAT_CMDL_INFO
7481 clear_showcmd();
7482#endif
7483
7484 /* If the inserted register is empty, we need to remove the '"' */
7485 if (need_redraw || stuff_empty())
7486 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007487
7488#ifdef FEAT_VISUAL
7489 /* Disallow starting Visual mode here, would get a weird mode. */
7490 if (!vis_active && VIsual_active)
7491 end_visual_mode();
7492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493}
7494
7495/*
7496 * CTRL-G commands in Insert mode.
7497 */
7498 static void
7499ins_ctrl_g()
7500{
7501 int c;
7502
7503#ifdef FEAT_INS_EXPAND
7504 /* Right after CTRL-X the cursor will be after the ruler. */
7505 setcursor();
7506#endif
7507
7508 /*
7509 * Don't map the second key. This also prevents the mode message to be
7510 * deleted when ESC is hit.
7511 */
7512 ++no_mapping;
7513 c = safe_vgetc();
7514 --no_mapping;
7515 switch (c)
7516 {
7517 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7518 case K_UP:
7519 case Ctrl_K:
7520 case 'k': ins_up(TRUE);
7521 break;
7522
7523 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7524 case K_DOWN:
7525 case Ctrl_J:
7526 case 'j': ins_down(TRUE);
7527 break;
7528
7529 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007530 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007532
7533 /* Need to reset Insstart, esp. because a BS that joins
7534 * aline to the previous one must save for undo. */
7535 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 break;
7537
7538 /* Unknown CTRL-G command, reserved for future expansion. */
7539 default: vim_beep();
7540 }
7541}
7542
7543/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007544 * CTRL-^ in Insert mode.
7545 */
7546 static void
7547ins_ctrl_hat()
7548{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007549 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007550 {
7551 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7552 if (State & LANGMAP)
7553 {
7554 curbuf->b_p_iminsert = B_IMODE_NONE;
7555 State &= ~LANGMAP;
7556 }
7557 else
7558 {
7559 curbuf->b_p_iminsert = B_IMODE_LMAP;
7560 State |= LANGMAP;
7561#ifdef USE_IM_CONTROL
7562 im_set_active(FALSE);
7563#endif
7564 }
7565 }
7566#ifdef USE_IM_CONTROL
7567 else
7568 {
7569 /* There are no ":lmap" mappings, toggle IM */
7570 if (im_get_status())
7571 {
7572 curbuf->b_p_iminsert = B_IMODE_NONE;
7573 im_set_active(FALSE);
7574 }
7575 else
7576 {
7577 curbuf->b_p_iminsert = B_IMODE_IM;
7578 State &= ~LANGMAP;
7579 im_set_active(TRUE);
7580 }
7581 }
7582#endif
7583 set_iminsert_global();
7584 showmode();
7585#ifdef FEAT_GUI
7586 /* may show different cursor shape or color */
7587 if (gui.in_use)
7588 gui_update_cursor(TRUE, FALSE);
7589#endif
7590#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7591 /* Show/unshow value of 'keymap' in status lines. */
7592 status_redraw_curbuf();
7593#endif
7594}
7595
7596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 * Handle ESC in insert mode.
7598 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7599 * insert.
7600 */
7601 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007602ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 long *count;
7604 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007605 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 int temp;
7608 static int disabled_redraw = FALSE;
7609
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007610#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007611 check_spell_redraw();
7612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613#if defined(FEAT_HANGULIN)
7614# if defined(ESC_CHG_TO_ENG_MODE)
7615 hangul_input_state_set(0);
7616# endif
7617 if (composing_hangul)
7618 {
7619 push_raw_key(composing_hangul_buffer, 2);
7620 composing_hangul = 0;
7621 }
7622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
7624 temp = curwin->w_cursor.col;
7625 if (disabled_redraw)
7626 {
7627 --RedrawingDisabled;
7628 disabled_redraw = FALSE;
7629 }
7630 if (!arrow_used)
7631 {
7632 /*
7633 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007634 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7635 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 */
7637 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007638 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639
7640 /*
7641 * Repeating insert may take a long time. Check for
7642 * interrupt now and then.
7643 */
7644 if (*count > 0)
7645 {
7646 line_breakcheck();
7647 if (got_int)
7648 *count = 0;
7649 }
7650
7651 if (--*count > 0) /* repeat what was typed */
7652 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007653 /* Vi repeats the insert without replacing characters. */
7654 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7655 State &= ~REPLACE_FLAG;
7656
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 (void)start_redo_ins();
7658 if (cmdchar == 'r' || cmdchar == 'v')
7659 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7660 ++RedrawingDisabled;
7661 disabled_redraw = TRUE;
7662 return FALSE; /* repeat the insert */
7663 }
7664 stop_insert(&curwin->w_cursor, TRUE);
7665 undisplay_dollar();
7666 }
7667
7668 /* When an autoindent was removed, curswant stays after the
7669 * indent */
7670 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7671 curwin->w_set_curswant = TRUE;
7672
7673 /* Remember the last Insert position in the '^ mark. */
7674 if (!cmdmod.keepjumps)
7675 curbuf->b_last_insert = curwin->w_cursor;
7676
7677 /*
7678 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007679 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007681 if (!nomove
7682 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683#ifdef FEAT_VIRTUALEDIT
7684 || curwin->w_cursor.coladd > 0
7685#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007686 )
7687 && (restart_edit == NUL
7688 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007690 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007692 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693#ifdef FEAT_RIGHTLEFT
7694 && !revins_on
7695#endif
7696 )
7697 {
7698#ifdef FEAT_VIRTUALEDIT
7699 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7700 {
7701 oneleft();
7702 if (restart_edit != NUL)
7703 ++curwin->w_cursor.coladd;
7704 }
7705 else
7706#endif
7707 {
7708 --curwin->w_cursor.col;
7709#ifdef FEAT_MBYTE
7710 /* Correct cursor for multi-byte character. */
7711 if (has_mbyte)
7712 mb_adjust_cursor();
7713#endif
7714 }
7715 }
7716
7717#ifdef USE_IM_CONTROL
7718 /* Disable IM to allow typing English directly for Normal mode commands.
7719 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7720 * well). */
7721 if (!(State & LANGMAP))
7722 im_save_status(&curbuf->b_p_iminsert);
7723 im_set_active(FALSE);
7724#endif
7725
7726 State = NORMAL;
7727 /* need to position cursor again (e.g. when on a TAB ) */
7728 changed_cline_bef_curs();
7729
7730#ifdef FEAT_MOUSE
7731 setmouse();
7732#endif
7733#ifdef CURSOR_SHAPE
7734 ui_cursor_shape(); /* may show different cursor shape */
7735#endif
7736
7737 /*
7738 * When recording or for CTRL-O, need to display the new mode.
7739 * Otherwise remove the mode message.
7740 */
7741 if (Recording || restart_edit != NUL)
7742 showmode();
7743 else if (p_smd)
7744 MSG("");
7745
7746 return TRUE; /* exit Insert mode */
7747}
7748
7749#ifdef FEAT_RIGHTLEFT
7750/*
7751 * Toggle language: hkmap and revins_on.
7752 * Move to end of reverse inserted text.
7753 */
7754 static void
7755ins_ctrl_()
7756{
7757 if (revins_on && revins_chars && revins_scol >= 0)
7758 {
7759 while (gchar_cursor() != NUL && revins_chars--)
7760 ++curwin->w_cursor.col;
7761 }
7762 p_ri = !p_ri;
7763 revins_on = (State == INSERT && p_ri);
7764 if (revins_on)
7765 {
7766 revins_scol = curwin->w_cursor.col;
7767 revins_legal++;
7768 revins_chars = 0;
7769 undisplay_dollar();
7770 }
7771 else
7772 revins_scol = -1;
7773#ifdef FEAT_FKMAP
7774 if (p_altkeymap)
7775 {
7776 /*
7777 * to be consistent also for redo command, using '.'
7778 * set arrow_used to true and stop it - causing to redo
7779 * characters entered in one mode (normal/reverse insert).
7780 */
7781 arrow_used = TRUE;
7782 (void)stop_arrow();
7783 p_fkmap = curwin->w_p_rl ^ p_ri;
7784 if (p_fkmap && p_ri)
7785 State = INSERT;
7786 }
7787 else
7788#endif
7789 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7790 showmode();
7791}
7792#endif
7793
7794#ifdef FEAT_VISUAL
7795/*
7796 * If 'keymodel' contains "startsel", may start selection.
7797 * Returns TRUE when a CTRL-O and other keys stuffed.
7798 */
7799 static int
7800ins_start_select(c)
7801 int c;
7802{
7803 if (km_startsel)
7804 switch (c)
7805 {
7806 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 case K_PAGEUP:
7809 case K_KPAGEUP:
7810 case K_PAGEDOWN:
7811 case K_KPAGEDOWN:
7812# ifdef MACOS
7813 case K_LEFT:
7814 case K_RIGHT:
7815 case K_UP:
7816 case K_DOWN:
7817 case K_END:
7818 case K_HOME:
7819# endif
7820 if (!(mod_mask & MOD_MASK_SHIFT))
7821 break;
7822 /* FALLTHROUGH */
7823 case K_S_LEFT:
7824 case K_S_RIGHT:
7825 case K_S_UP:
7826 case K_S_DOWN:
7827 case K_S_END:
7828 case K_S_HOME:
7829 /* Start selection right away, the cursor can move with
7830 * CTRL-O when beyond the end of the line. */
7831 start_selection();
7832
7833 /* Execute the key in (insert) Select mode. */
7834 stuffcharReadbuff(Ctrl_O);
7835 if (mod_mask)
7836 {
7837 char_u buf[4];
7838
7839 buf[0] = K_SPECIAL;
7840 buf[1] = KS_MODIFIER;
7841 buf[2] = mod_mask;
7842 buf[3] = NUL;
7843 stuffReadbuff(buf);
7844 }
7845 stuffcharReadbuff(c);
7846 return TRUE;
7847 }
7848 return FALSE;
7849}
7850#endif
7851
7852/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007853 * <Insert> key in Insert mode: toggle insert/remplace mode.
7854 */
7855 static void
7856ins_insert(replaceState)
7857 int replaceState;
7858{
7859#ifdef FEAT_FKMAP
7860 if (p_fkmap && p_ri)
7861 {
7862 beep_flush();
7863 EMSG(farsi_text_3); /* encoded in Farsi */
7864 return;
7865 }
7866#endif
7867
7868#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007869# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007870 set_vim_var_string(VV_INSERTMODE,
7871 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007872# ifdef FEAT_VREPLACE
7873 replaceState == VREPLACE ? "v" :
7874# endif
7875 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007876# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007877 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7878#endif
7879 if (State & REPLACE_FLAG)
7880 State = INSERT | (State & LANGMAP);
7881 else
7882 State = replaceState | (State & LANGMAP);
7883 AppendCharToRedobuff(K_INS);
7884 showmode();
7885#ifdef CURSOR_SHAPE
7886 ui_cursor_shape(); /* may show different cursor shape */
7887#endif
7888}
7889
7890/*
7891 * Pressed CTRL-O in Insert mode.
7892 */
7893 static void
7894ins_ctrl_o()
7895{
7896#ifdef FEAT_VREPLACE
7897 if (State & VREPLACE_FLAG)
7898 restart_edit = 'V';
7899 else
7900#endif
7901 if (State & REPLACE_FLAG)
7902 restart_edit = 'R';
7903 else
7904 restart_edit = 'I';
7905#ifdef FEAT_VIRTUALEDIT
7906 if (virtual_active())
7907 ins_at_eol = FALSE; /* cursor always keeps its column */
7908 else
7909#endif
7910 ins_at_eol = (gchar_cursor() == NUL);
7911}
7912
7913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 * If the cursor is on an indent, ^T/^D insert/delete one
7915 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7916 * Always round the indent to 'shiftwith', this is compatible
7917 * with vi. But vi only supports ^T and ^D after an
7918 * autoindent, we support it everywhere.
7919 */
7920 static void
7921ins_shift(c, lastc)
7922 int c;
7923 int lastc;
7924{
7925 if (stop_arrow() == FAIL)
7926 return;
7927 AppendCharToRedobuff(c);
7928
7929 /*
7930 * 0^D and ^^D: remove all indent.
7931 */
7932 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7933 {
7934 --curwin->w_cursor.col;
7935 (void)del_char(FALSE); /* delete the '^' or '0' */
7936 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7937 if (State & REPLACE_FLAG)
7938 replace_pop_ins();
7939 if (lastc == '^')
7940 old_indent = get_indent(); /* remember curr. indent */
7941 change_indent(INDENT_SET, 0, TRUE, 0);
7942 }
7943 else
7944 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7945
7946 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7947 did_ai = FALSE;
7948#ifdef FEAT_SMARTINDENT
7949 did_si = FALSE;
7950 can_si = FALSE;
7951 can_si_back = FALSE;
7952#endif
7953#ifdef FEAT_CINDENT
7954 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7955#endif
7956}
7957
7958 static void
7959ins_del()
7960{
7961 int temp;
7962
7963 if (stop_arrow() == FAIL)
7964 return;
7965 if (gchar_cursor() == NUL) /* delete newline */
7966 {
7967 temp = curwin->w_cursor.col;
7968 if (!can_bs(BS_EOL) /* only if "eol" included */
7969 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7970 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7971 || do_join(FALSE) == FAIL)
7972 vim_beep();
7973 else
7974 curwin->w_cursor.col = temp;
7975 }
7976 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7977 vim_beep();
7978 did_ai = FALSE;
7979#ifdef FEAT_SMARTINDENT
7980 did_si = FALSE;
7981 can_si = FALSE;
7982 can_si_back = FALSE;
7983#endif
7984 AppendCharToRedobuff(K_DEL);
7985}
7986
7987/*
7988 * Handle Backspace, delete-word and delete-line in Insert mode.
7989 * Return TRUE when backspace was actually used.
7990 */
7991 static int
7992ins_bs(c, mode, inserted_space_p)
7993 int c;
7994 int mode;
7995 int *inserted_space_p;
7996{
7997 linenr_T lnum;
7998 int cc;
7999 int temp = 0; /* init for GCC */
8000 colnr_T mincol;
8001 int did_backspace = FALSE;
8002 int in_indent;
8003 int oldState;
8004#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008005 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006#endif
8007
8008 /*
8009 * can't delete anything in an empty file
8010 * can't backup past first character in buffer
8011 * can't backup past starting point unless 'backspace' > 1
8012 * can backup to a previous line if 'backspace' == 0
8013 */
8014 if ( bufempty()
8015 || (
8016#ifdef FEAT_RIGHTLEFT
8017 !revins_on &&
8018#endif
8019 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8020 || (!can_bs(BS_START)
8021 && (arrow_used
8022 || (curwin->w_cursor.lnum == Insstart.lnum
8023 && curwin->w_cursor.col <= Insstart.col)))
8024 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8025 && curwin->w_cursor.col <= ai_col)
8026 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8027 {
8028 vim_beep();
8029 return FALSE;
8030 }
8031
8032 if (stop_arrow() == FAIL)
8033 return FALSE;
8034 in_indent = inindent(0);
8035#ifdef FEAT_CINDENT
8036 if (in_indent)
8037 can_cindent = FALSE;
8038#endif
8039#ifdef FEAT_COMMENTS
8040 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8041#endif
8042#ifdef FEAT_RIGHTLEFT
8043 if (revins_on) /* put cursor after last inserted char */
8044 inc_cursor();
8045#endif
8046
8047#ifdef FEAT_VIRTUALEDIT
8048 /* Virtualedit:
8049 * BACKSPACE_CHAR eats a virtual space
8050 * BACKSPACE_WORD eats all coladd
8051 * BACKSPACE_LINE eats all coladd and keeps going
8052 */
8053 if (curwin->w_cursor.coladd > 0)
8054 {
8055 if (mode == BACKSPACE_CHAR)
8056 {
8057 --curwin->w_cursor.coladd;
8058 return TRUE;
8059 }
8060 if (mode == BACKSPACE_WORD)
8061 {
8062 curwin->w_cursor.coladd = 0;
8063 return TRUE;
8064 }
8065 curwin->w_cursor.coladd = 0;
8066 }
8067#endif
8068
8069 /*
8070 * delete newline!
8071 */
8072 if (curwin->w_cursor.col == 0)
8073 {
8074 lnum = Insstart.lnum;
8075 if (curwin->w_cursor.lnum == Insstart.lnum
8076#ifdef FEAT_RIGHTLEFT
8077 || revins_on
8078#endif
8079 )
8080 {
8081 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8082 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8083 return FALSE;
8084 --Insstart.lnum;
8085 Insstart.col = MAXCOL;
8086 }
8087 /*
8088 * In replace mode:
8089 * cc < 0: NL was inserted, delete it
8090 * cc >= 0: NL was replaced, put original characters back
8091 */
8092 cc = -1;
8093 if (State & REPLACE_FLAG)
8094 cc = replace_pop(); /* returns -1 if NL was inserted */
8095 /*
8096 * In replace mode, in the line we started replacing, we only move the
8097 * cursor.
8098 */
8099 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8100 {
8101 dec_cursor();
8102 }
8103 else
8104 {
8105#ifdef FEAT_VREPLACE
8106 if (!(State & VREPLACE_FLAG)
8107 || curwin->w_cursor.lnum > orig_line_count)
8108#endif
8109 {
8110 temp = gchar_cursor(); /* remember current char */
8111 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008112
8113 /* When "aw" is in 'formatoptions' we must delete the space at
8114 * the end of the line, otherwise the line will be broken
8115 * again when auto-formatting. */
8116 if (has_format_option(FO_AUTO)
8117 && has_format_option(FO_WHITE_PAR))
8118 {
8119 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8120 TRUE);
8121 int len;
8122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008123 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008124 if (len > 0 && ptr[len - 1] == ' ')
8125 ptr[len - 1] = NUL;
8126 }
8127
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 (void)do_join(FALSE);
8129 if (temp == NUL && gchar_cursor() != NUL)
8130 inc_cursor();
8131 }
8132#ifdef FEAT_VREPLACE
8133 else
8134 dec_cursor();
8135#endif
8136
8137 /*
8138 * In REPLACE mode we have to put back the text that was replaced
8139 * by the NL. On the replace stack is first a NUL-terminated
8140 * sequence of characters that were deleted and then the
8141 * characters that NL replaced.
8142 */
8143 if (State & REPLACE_FLAG)
8144 {
8145 /*
8146 * Do the next ins_char() in NORMAL state, to
8147 * prevent ins_char() from replacing characters and
8148 * avoiding showmatch().
8149 */
8150 oldState = State;
8151 State = NORMAL;
8152 /*
8153 * restore characters (blanks) deleted after cursor
8154 */
8155 while (cc > 0)
8156 {
8157 temp = curwin->w_cursor.col;
8158#ifdef FEAT_MBYTE
8159 mb_replace_pop_ins(cc);
8160#else
8161 ins_char(cc);
8162#endif
8163 curwin->w_cursor.col = temp;
8164 cc = replace_pop();
8165 }
8166 /* restore the characters that NL replaced */
8167 replace_pop_ins();
8168 State = oldState;
8169 }
8170 }
8171 did_ai = FALSE;
8172 }
8173 else
8174 {
8175 /*
8176 * Delete character(s) before the cursor.
8177 */
8178#ifdef FEAT_RIGHTLEFT
8179 if (revins_on) /* put cursor on last inserted char */
8180 dec_cursor();
8181#endif
8182 mincol = 0;
8183 /* keep indent */
8184 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8185#ifdef FEAT_RIGHTLEFT
8186 && !revins_on
8187#endif
8188 )
8189 {
8190 temp = curwin->w_cursor.col;
8191 beginline(BL_WHITE);
8192 if (curwin->w_cursor.col < (colnr_T)temp)
8193 mincol = curwin->w_cursor.col;
8194 curwin->w_cursor.col = temp;
8195 }
8196
8197 /*
8198 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8199 */
8200 if ( mode == BACKSPACE_CHAR
8201 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008202 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 && (*(ml_get_cursor() - 1) == TAB
8204 || (*(ml_get_cursor() - 1) == ' '
8205 && (!*inserted_space_p
8206 || arrow_used))))))
8207 {
8208 int ts;
8209 colnr_T vcol;
8210 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008211#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214
8215 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008216 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 ts = curbuf->b_p_sw;
8218 else
8219 ts = curbuf->b_p_sts;
8220 /* Compute the virtual column where we want to be. Since
8221 * 'showbreak' may get in the way, need to get the last column of
8222 * the previous character. */
8223 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8224 dec_cursor();
8225 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8226 inc_cursor();
8227 want_vcol = (want_vcol / ts) * ts;
8228
8229 /* delete characters until we are at or before want_vcol */
8230 while (vcol > want_vcol
8231 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8232 {
8233 dec_cursor();
8234 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8235 if (State & REPLACE_FLAG)
8236 {
8237 /* Don't delete characters before the insert point when in
8238 * Replace mode */
8239 if (curwin->w_cursor.lnum != Insstart.lnum
8240 || curwin->w_cursor.col >= Insstart.col)
8241 {
8242#if 0 /* what was this for? It causes problems when sw != ts. */
8243 if (State == REPLACE && (int)vcol < want_vcol)
8244 {
8245 (void)del_char(FALSE);
8246 extra = 2; /* don't pop too much */
8247 }
8248 else
8249#endif
8250 replace_do_bs();
8251 }
8252 }
8253 else
8254 (void)del_char(FALSE);
8255 }
8256
8257 /* insert extra spaces until we are at want_vcol */
8258 while (vcol < want_vcol)
8259 {
8260 /* Remember the first char we inserted */
8261 if (curwin->w_cursor.lnum == Insstart.lnum
8262 && curwin->w_cursor.col < Insstart.col)
8263 Insstart.col = curwin->w_cursor.col;
8264
8265#ifdef FEAT_VREPLACE
8266 if (State & VREPLACE_FLAG)
8267 ins_char(' ');
8268 else
8269#endif
8270 {
8271 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008272 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008274#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (extra)
8276 replace_push_off(NUL);
8277 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 replace_push(NUL);
8280 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008281#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 if (extra == 2)
8283 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 }
8286 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8287 }
8288 }
8289
8290 /*
8291 * Delete upto starting point, start of line or previous word.
8292 */
8293 else do
8294 {
8295#ifdef FEAT_RIGHTLEFT
8296 if (!revins_on) /* put cursor on char to be deleted */
8297#endif
8298 dec_cursor();
8299
8300 /* start of word? */
8301 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8302 {
8303 mode = BACKSPACE_WORD_NOT_SPACE;
8304 temp = vim_iswordc(gchar_cursor());
8305 }
8306 /* end of word? */
8307 else if (mode == BACKSPACE_WORD_NOT_SPACE
8308 && (vim_isspace(cc = gchar_cursor())
8309 || vim_iswordc(cc) != temp))
8310 {
8311#ifdef FEAT_RIGHTLEFT
8312 if (!revins_on)
8313#endif
8314 inc_cursor();
8315#ifdef FEAT_RIGHTLEFT
8316 else if (State & REPLACE_FLAG)
8317 dec_cursor();
8318#endif
8319 break;
8320 }
8321 if (State & REPLACE_FLAG)
8322 replace_do_bs();
8323 else
8324 {
8325#ifdef FEAT_MBYTE
8326 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008327 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328#endif
8329 (void)del_char(FALSE);
8330#ifdef FEAT_MBYTE
8331 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008332 * If there are combining characters and 'delcombine' is set
8333 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * character.
8335 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008336 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 inc_cursor();
8338#endif
8339#ifdef FEAT_RIGHTLEFT
8340 if (revins_chars)
8341 {
8342 revins_chars--;
8343 revins_legal++;
8344 }
8345 if (revins_on && gchar_cursor() == NUL)
8346 break;
8347#endif
8348 }
8349 /* Just a single backspace?: */
8350 if (mode == BACKSPACE_CHAR)
8351 break;
8352 } while (
8353#ifdef FEAT_RIGHTLEFT
8354 revins_on ||
8355#endif
8356 (curwin->w_cursor.col > mincol
8357 && (curwin->w_cursor.lnum != Insstart.lnum
8358 || curwin->w_cursor.col != Insstart.col)));
8359 did_backspace = TRUE;
8360 }
8361#ifdef FEAT_SMARTINDENT
8362 did_si = FALSE;
8363 can_si = FALSE;
8364 can_si_back = FALSE;
8365#endif
8366 if (curwin->w_cursor.col <= 1)
8367 did_ai = FALSE;
8368 /*
8369 * It's a little strange to put backspaces into the redo
8370 * buffer, but it makes auto-indent a lot easier to deal
8371 * with.
8372 */
8373 AppendCharToRedobuff(c);
8374
8375 /* If deleted before the insertion point, adjust it */
8376 if (curwin->w_cursor.lnum == Insstart.lnum
8377 && curwin->w_cursor.col < Insstart.col)
8378 Insstart.col = curwin->w_cursor.col;
8379
8380 /* vi behaviour: the cursor moves backward but the character that
8381 * was there remains visible
8382 * Vim behaviour: the cursor moves backward and the character that
8383 * was there is erased from the screen.
8384 * We can emulate the vi behaviour by pretending there is a dollar
8385 * displayed even when there isn't.
8386 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8387 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8388 dollar_vcol = curwin->w_virtcol;
8389
8390 return did_backspace;
8391}
8392
8393#ifdef FEAT_MOUSE
8394 static void
8395ins_mouse(c)
8396 int c;
8397{
8398 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008399 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400
8401# ifdef FEAT_GUI
8402 /* When GUI is active, also move/paste when 'mouse' is empty */
8403 if (!gui.in_use)
8404# endif
8405 if (!mouse_has(MOUSE_INSERT))
8406 return;
8407
8408 undisplay_dollar();
8409 tpos = curwin->w_cursor;
8410 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8411 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008412#ifdef FEAT_WINDOWS
8413 win_T *new_curwin = curwin;
8414
8415 if (curwin != old_curwin && win_valid(old_curwin))
8416 {
8417 /* Mouse took us to another window. We need to go back to the
8418 * previous one to stop insert there properly. */
8419 curwin = old_curwin;
8420 curbuf = curwin->w_buffer;
8421 }
8422#endif
8423 start_arrow(curwin == old_curwin ? &tpos : NULL);
8424#ifdef FEAT_WINDOWS
8425 if (curwin != new_curwin && win_valid(new_curwin))
8426 {
8427 curwin = new_curwin;
8428 curbuf = curwin->w_buffer;
8429 }
8430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431# ifdef FEAT_CINDENT
8432 can_cindent = TRUE;
8433# endif
8434 }
8435
8436#ifdef FEAT_WINDOWS
8437 /* redraw status lines (in case another window became active) */
8438 redraw_statuslines();
8439#endif
8440}
8441
8442 static void
8443ins_mousescroll(up)
8444 int up;
8445{
8446 pos_T tpos;
8447# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8448 win_T *old_curwin;
8449# endif
8450
8451 tpos = curwin->w_cursor;
8452
8453# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8454 old_curwin = curwin;
8455
8456 /* Currently the mouse coordinates are only known in the GUI. */
8457 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8458 {
8459 int row, col;
8460
8461 row = mouse_row;
8462 col = mouse_col;
8463
8464 /* find the window at the pointer coordinates */
8465 curwin = mouse_find_win(&row, &col);
8466 curbuf = curwin->w_buffer;
8467 }
8468 if (curwin == old_curwin)
8469# endif
8470 undisplay_dollar();
8471
8472 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8473 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8474 else
8475 scroll_redraw(up, 3L);
8476
8477# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8478 curwin->w_redr_status = TRUE;
8479
8480 curwin = old_curwin;
8481 curbuf = curwin->w_buffer;
8482# endif
8483
8484 if (!equalpos(curwin->w_cursor, tpos))
8485 {
8486 start_arrow(&tpos);
8487# ifdef FEAT_CINDENT
8488 can_cindent = TRUE;
8489# endif
8490 }
8491}
8492#endif
8493
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008494#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008495 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008496ins_tabline(c)
8497 int c;
8498{
8499 /* We will be leaving the current window, unless closing another tab. */
8500 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8501 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8502 {
8503 undisplay_dollar();
8504 start_arrow(&curwin->w_cursor);
8505# ifdef FEAT_CINDENT
8506 can_cindent = TRUE;
8507# endif
8508 }
8509
8510 if (c == K_TABLINE)
8511 goto_tabpage(current_tab);
8512 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008513 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008514 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008515 redraw_statuslines(); /* will redraw the tabline when needed */
8516 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008517}
8518#endif
8519
8520#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 void
8522ins_scroll()
8523{
8524 pos_T tpos;
8525
8526 undisplay_dollar();
8527 tpos = curwin->w_cursor;
8528 if (gui_do_scroll())
8529 {
8530 start_arrow(&tpos);
8531# ifdef FEAT_CINDENT
8532 can_cindent = TRUE;
8533# endif
8534 }
8535}
8536
8537 void
8538ins_horscroll()
8539{
8540 pos_T tpos;
8541
8542 undisplay_dollar();
8543 tpos = curwin->w_cursor;
8544 if (gui_do_horiz_scroll())
8545 {
8546 start_arrow(&tpos);
8547# ifdef FEAT_CINDENT
8548 can_cindent = TRUE;
8549# endif
8550 }
8551}
8552#endif
8553
8554 static void
8555ins_left()
8556{
8557 pos_T tpos;
8558
8559#ifdef FEAT_FOLDING
8560 if ((fdo_flags & FDO_HOR) && KeyTyped)
8561 foldOpenCursor();
8562#endif
8563 undisplay_dollar();
8564 tpos = curwin->w_cursor;
8565 if (oneleft() == OK)
8566 {
8567 start_arrow(&tpos);
8568#ifdef FEAT_RIGHTLEFT
8569 /* If exit reversed string, position is fixed */
8570 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8571 revins_legal++;
8572 revins_chars++;
8573#endif
8574 }
8575
8576 /*
8577 * if 'whichwrap' set for cursor in insert mode may go to
8578 * previous line
8579 */
8580 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8581 {
8582 start_arrow(&tpos);
8583 --(curwin->w_cursor.lnum);
8584 coladvance((colnr_T)MAXCOL);
8585 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8586 }
8587 else
8588 vim_beep();
8589}
8590
8591 static void
8592ins_home(c)
8593 int c;
8594{
8595 pos_T tpos;
8596
8597#ifdef FEAT_FOLDING
8598 if ((fdo_flags & FDO_HOR) && KeyTyped)
8599 foldOpenCursor();
8600#endif
8601 undisplay_dollar();
8602 tpos = curwin->w_cursor;
8603 if (c == K_C_HOME)
8604 curwin->w_cursor.lnum = 1;
8605 curwin->w_cursor.col = 0;
8606#ifdef FEAT_VIRTUALEDIT
8607 curwin->w_cursor.coladd = 0;
8608#endif
8609 curwin->w_curswant = 0;
8610 start_arrow(&tpos);
8611}
8612
8613 static void
8614ins_end(c)
8615 int c;
8616{
8617 pos_T tpos;
8618
8619#ifdef FEAT_FOLDING
8620 if ((fdo_flags & FDO_HOR) && KeyTyped)
8621 foldOpenCursor();
8622#endif
8623 undisplay_dollar();
8624 tpos = curwin->w_cursor;
8625 if (c == K_C_END)
8626 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8627 coladvance((colnr_T)MAXCOL);
8628 curwin->w_curswant = MAXCOL;
8629
8630 start_arrow(&tpos);
8631}
8632
8633 static void
8634ins_s_left()
8635{
8636#ifdef FEAT_FOLDING
8637 if ((fdo_flags & FDO_HOR) && KeyTyped)
8638 foldOpenCursor();
8639#endif
8640 undisplay_dollar();
8641 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8642 {
8643 start_arrow(&curwin->w_cursor);
8644 (void)bck_word(1L, FALSE, FALSE);
8645 curwin->w_set_curswant = TRUE;
8646 }
8647 else
8648 vim_beep();
8649}
8650
8651 static void
8652ins_right()
8653{
8654#ifdef FEAT_FOLDING
8655 if ((fdo_flags & FDO_HOR) && KeyTyped)
8656 foldOpenCursor();
8657#endif
8658 undisplay_dollar();
8659 if (gchar_cursor() != NUL || virtual_active()
8660 )
8661 {
8662 start_arrow(&curwin->w_cursor);
8663 curwin->w_set_curswant = TRUE;
8664#ifdef FEAT_VIRTUALEDIT
8665 if (virtual_active())
8666 oneright();
8667 else
8668#endif
8669 {
8670#ifdef FEAT_MBYTE
8671 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008672 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 else
8674#endif
8675 ++curwin->w_cursor.col;
8676 }
8677
8678#ifdef FEAT_RIGHTLEFT
8679 revins_legal++;
8680 if (revins_chars)
8681 revins_chars--;
8682#endif
8683 }
8684 /* if 'whichwrap' set for cursor in insert mode, may move the
8685 * cursor to the next line */
8686 else if (vim_strchr(p_ww, ']') != NULL
8687 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8688 {
8689 start_arrow(&curwin->w_cursor);
8690 curwin->w_set_curswant = TRUE;
8691 ++curwin->w_cursor.lnum;
8692 curwin->w_cursor.col = 0;
8693 }
8694 else
8695 vim_beep();
8696}
8697
8698 static void
8699ins_s_right()
8700{
8701#ifdef FEAT_FOLDING
8702 if ((fdo_flags & FDO_HOR) && KeyTyped)
8703 foldOpenCursor();
8704#endif
8705 undisplay_dollar();
8706 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8707 || gchar_cursor() != NUL)
8708 {
8709 start_arrow(&curwin->w_cursor);
8710 (void)fwd_word(1L, FALSE, 0);
8711 curwin->w_set_curswant = TRUE;
8712 }
8713 else
8714 vim_beep();
8715}
8716
8717 static void
8718ins_up(startcol)
8719 int startcol; /* when TRUE move to Insstart.col */
8720{
8721 pos_T tpos;
8722 linenr_T old_topline = curwin->w_topline;
8723#ifdef FEAT_DIFF
8724 int old_topfill = curwin->w_topfill;
8725#endif
8726
8727 undisplay_dollar();
8728 tpos = curwin->w_cursor;
8729 if (cursor_up(1L, TRUE) == OK)
8730 {
8731 if (startcol)
8732 coladvance(getvcol_nolist(&Insstart));
8733 if (old_topline != curwin->w_topline
8734#ifdef FEAT_DIFF
8735 || old_topfill != curwin->w_topfill
8736#endif
8737 )
8738 redraw_later(VALID);
8739 start_arrow(&tpos);
8740#ifdef FEAT_CINDENT
8741 can_cindent = TRUE;
8742#endif
8743 }
8744 else
8745 vim_beep();
8746}
8747
8748 static void
8749ins_pageup()
8750{
8751 pos_T tpos;
8752
8753 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008754
8755#ifdef FEAT_WINDOWS
8756 if (mod_mask & MOD_MASK_CTRL)
8757 {
8758 /* <C-PageUp>: tab page back */
8759 goto_tabpage(-1);
8760 return;
8761 }
8762#endif
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 tpos = curwin->w_cursor;
8765 if (onepage(BACKWARD, 1L) == OK)
8766 {
8767 start_arrow(&tpos);
8768#ifdef FEAT_CINDENT
8769 can_cindent = TRUE;
8770#endif
8771 }
8772 else
8773 vim_beep();
8774}
8775
8776 static void
8777ins_down(startcol)
8778 int startcol; /* when TRUE move to Insstart.col */
8779{
8780 pos_T tpos;
8781 linenr_T old_topline = curwin->w_topline;
8782#ifdef FEAT_DIFF
8783 int old_topfill = curwin->w_topfill;
8784#endif
8785
8786 undisplay_dollar();
8787 tpos = curwin->w_cursor;
8788 if (cursor_down(1L, TRUE) == OK)
8789 {
8790 if (startcol)
8791 coladvance(getvcol_nolist(&Insstart));
8792 if (old_topline != curwin->w_topline
8793#ifdef FEAT_DIFF
8794 || old_topfill != curwin->w_topfill
8795#endif
8796 )
8797 redraw_later(VALID);
8798 start_arrow(&tpos);
8799#ifdef FEAT_CINDENT
8800 can_cindent = TRUE;
8801#endif
8802 }
8803 else
8804 vim_beep();
8805}
8806
8807 static void
8808ins_pagedown()
8809{
8810 pos_T tpos;
8811
8812 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008813
8814#ifdef FEAT_WINDOWS
8815 if (mod_mask & MOD_MASK_CTRL)
8816 {
8817 /* <C-PageDown>: tab page forward */
8818 goto_tabpage(0);
8819 return;
8820 }
8821#endif
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 tpos = curwin->w_cursor;
8824 if (onepage(FORWARD, 1L) == OK)
8825 {
8826 start_arrow(&tpos);
8827#ifdef FEAT_CINDENT
8828 can_cindent = TRUE;
8829#endif
8830 }
8831 else
8832 vim_beep();
8833}
8834
8835#ifdef FEAT_DND
8836 static void
8837ins_drop()
8838{
8839 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8840}
8841#endif
8842
8843/*
8844 * Handle TAB in Insert or Replace mode.
8845 * Return TRUE when the TAB needs to be inserted like a normal character.
8846 */
8847 static int
8848ins_tab()
8849{
8850 int ind;
8851 int i;
8852 int temp;
8853
8854 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8855 Insstart_blank_vcol = get_nolist_virtcol();
8856 if (echeck_abbr(TAB + ABBR_OFF))
8857 return FALSE;
8858
8859 ind = inindent(0);
8860#ifdef FEAT_CINDENT
8861 if (ind)
8862 can_cindent = FALSE;
8863#endif
8864
8865 /*
8866 * When nothing special, insert TAB like a normal character
8867 */
8868 if (!curbuf->b_p_et
8869 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8870 && curbuf->b_p_sts == 0)
8871 return TRUE;
8872
8873 if (stop_arrow() == FAIL)
8874 return TRUE;
8875
8876 did_ai = FALSE;
8877#ifdef FEAT_SMARTINDENT
8878 did_si = FALSE;
8879 can_si = FALSE;
8880 can_si_back = FALSE;
8881#endif
8882 AppendToRedobuff((char_u *)"\t");
8883
8884 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8885 temp = (int)curbuf->b_p_sw;
8886 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8887 temp = (int)curbuf->b_p_sts;
8888 else /* otherwise use 'tabstop' */
8889 temp = (int)curbuf->b_p_ts;
8890 temp -= get_nolist_virtcol() % temp;
8891
8892 /*
8893 * Insert the first space with ins_char(). It will delete one char in
8894 * replace mode. Insert the rest with ins_str(); it will not delete any
8895 * chars. For VREPLACE mode, we use ins_char() for all characters.
8896 */
8897 ins_char(' ');
8898 while (--temp > 0)
8899 {
8900#ifdef FEAT_VREPLACE
8901 if (State & VREPLACE_FLAG)
8902 ins_char(' ');
8903 else
8904#endif
8905 {
8906 ins_str((char_u *)" ");
8907 if (State & REPLACE_FLAG) /* no char replaced */
8908 replace_push(NUL);
8909 }
8910 }
8911
8912 /*
8913 * When 'expandtab' not set: Replace spaces by TABs where possible.
8914 */
8915 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8916 {
8917 char_u *ptr;
8918#ifdef FEAT_VREPLACE
8919 char_u *saved_line = NULL; /* init for GCC */
8920 pos_T pos;
8921#endif
8922 pos_T fpos;
8923 pos_T *cursor;
8924 colnr_T want_vcol, vcol;
8925 int change_col = -1;
8926 int save_list = curwin->w_p_list;
8927
8928 /*
8929 * Get the current line. For VREPLACE mode, don't make real changes
8930 * yet, just work on a copy of the line.
8931 */
8932#ifdef FEAT_VREPLACE
8933 if (State & VREPLACE_FLAG)
8934 {
8935 pos = curwin->w_cursor;
8936 cursor = &pos;
8937 saved_line = vim_strsave(ml_get_curline());
8938 if (saved_line == NULL)
8939 return FALSE;
8940 ptr = saved_line + pos.col;
8941 }
8942 else
8943#endif
8944 {
8945 ptr = ml_get_cursor();
8946 cursor = &curwin->w_cursor;
8947 }
8948
8949 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8950 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8951 curwin->w_p_list = FALSE;
8952
8953 /* Find first white before the cursor */
8954 fpos = curwin->w_cursor;
8955 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8956 {
8957 --fpos.col;
8958 --ptr;
8959 }
8960
8961 /* In Replace mode, don't change characters before the insert point. */
8962 if ((State & REPLACE_FLAG)
8963 && fpos.lnum == Insstart.lnum
8964 && fpos.col < Insstart.col)
8965 {
8966 ptr += Insstart.col - fpos.col;
8967 fpos.col = Insstart.col;
8968 }
8969
8970 /* compute virtual column numbers of first white and cursor */
8971 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8972 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8973
8974 /* Use as many TABs as possible. Beware of 'showbreak' and
8975 * 'linebreak' adding extra virtual columns. */
8976 while (vim_iswhite(*ptr))
8977 {
8978 i = lbr_chartabsize((char_u *)"\t", vcol);
8979 if (vcol + i > want_vcol)
8980 break;
8981 if (*ptr != TAB)
8982 {
8983 *ptr = TAB;
8984 if (change_col < 0)
8985 {
8986 change_col = fpos.col; /* Column of first change */
8987 /* May have to adjust Insstart */
8988 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8989 Insstart.col = fpos.col;
8990 }
8991 }
8992 ++fpos.col;
8993 ++ptr;
8994 vcol += i;
8995 }
8996
8997 if (change_col >= 0)
8998 {
8999 int repl_off = 0;
9000
9001 /* Skip over the spaces we need. */
9002 while (vcol < want_vcol && *ptr == ' ')
9003 {
9004 vcol += lbr_chartabsize(ptr, vcol);
9005 ++ptr;
9006 ++repl_off;
9007 }
9008 if (vcol > want_vcol)
9009 {
9010 /* Must have a char with 'showbreak' just before it. */
9011 --ptr;
9012 --repl_off;
9013 }
9014 fpos.col += repl_off;
9015
9016 /* Delete following spaces. */
9017 i = cursor->col - fpos.col;
9018 if (i > 0)
9019 {
9020 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9021 /* correct replace stack. */
9022 if ((State & REPLACE_FLAG)
9023#ifdef FEAT_VREPLACE
9024 && !(State & VREPLACE_FLAG)
9025#endif
9026 )
9027 for (temp = i; --temp >= 0; )
9028 replace_join(repl_off);
9029 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009030#ifdef FEAT_NETBEANS_INTG
9031 if (usingNetbeans)
9032 {
9033 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9034 (long)(i + 1));
9035 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9036 (char_u *)"\t", 1);
9037 }
9038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 cursor->col -= i;
9040
9041#ifdef FEAT_VREPLACE
9042 /*
9043 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9044 * backspacing over the changed spacing and then inserting the new
9045 * spacing.
9046 */
9047 if (State & VREPLACE_FLAG)
9048 {
9049 /* Backspace from real cursor to change_col */
9050 backspace_until_column(change_col);
9051
9052 /* Insert each char in saved_line from changed_col to
9053 * ptr-cursor */
9054 ins_bytes_len(saved_line + change_col,
9055 cursor->col - change_col);
9056 }
9057#endif
9058 }
9059
9060#ifdef FEAT_VREPLACE
9061 if (State & VREPLACE_FLAG)
9062 vim_free(saved_line);
9063#endif
9064 curwin->w_p_list = save_list;
9065 }
9066
9067 return FALSE;
9068}
9069
9070/*
9071 * Handle CR or NL in insert mode.
9072 * Return TRUE when out of memory or can't undo.
9073 */
9074 static int
9075ins_eol(c)
9076 int c;
9077{
9078 int i;
9079
9080 if (echeck_abbr(c + ABBR_OFF))
9081 return FALSE;
9082 if (stop_arrow() == FAIL)
9083 return TRUE;
9084 undisplay_dollar();
9085
9086 /*
9087 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9088 * character under the cursor. Only push a NUL on the replace stack,
9089 * nothing to put back when the NL is deleted.
9090 */
9091 if ((State & REPLACE_FLAG)
9092#ifdef FEAT_VREPLACE
9093 && !(State & VREPLACE_FLAG)
9094#endif
9095 )
9096 replace_push(NUL);
9097
9098 /*
9099 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9100 * replacing the next line, so we push all of the characters left on the
9101 * line onto the replace stack. This is not done here though, it is done
9102 * in open_line().
9103 */
9104
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009105#ifdef FEAT_VIRTUALEDIT
9106 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9107 * CTRL-O). */
9108 if (virtual_active() && curwin->w_cursor.coladd > 0)
9109 coladvance(getviscol());
9110#endif
9111
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112#ifdef FEAT_RIGHTLEFT
9113# ifdef FEAT_FKMAP
9114 if (p_altkeymap && p_fkmap)
9115 fkmap(NL);
9116# endif
9117 /* NL in reverse insert will always start in the end of
9118 * current line. */
9119 if (revins_on)
9120 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9121#endif
9122
9123 AppendToRedobuff(NL_STR);
9124 i = open_line(FORWARD,
9125#ifdef FEAT_COMMENTS
9126 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9127#endif
9128 0, old_indent);
9129 old_indent = 0;
9130#ifdef FEAT_CINDENT
9131 can_cindent = TRUE;
9132#endif
9133
9134 return (!i);
9135}
9136
9137#ifdef FEAT_DIGRAPHS
9138/*
9139 * Handle digraph in insert mode.
9140 * Returns character still to be inserted, or NUL when nothing remaining to be
9141 * done.
9142 */
9143 static int
9144ins_digraph()
9145{
9146 int c;
9147 int cc;
9148
9149 pc_status = PC_STATUS_UNSET;
9150 if (redrawing() && !char_avail())
9151 {
9152 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009153 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154
9155 edit_putchar('?', TRUE);
9156#ifdef FEAT_CMDL_INFO
9157 add_to_showcmd_c(Ctrl_K);
9158#endif
9159 }
9160
9161#ifdef USE_ON_FLY_SCROLL
9162 dont_scroll = TRUE; /* disallow scrolling here */
9163#endif
9164
9165 /* don't map the digraph chars. This also prevents the
9166 * mode message to be deleted when ESC is hit */
9167 ++no_mapping;
9168 ++allow_keys;
9169 c = safe_vgetc();
9170 --no_mapping;
9171 --allow_keys;
9172 if (IS_SPECIAL(c) || mod_mask) /* special key */
9173 {
9174#ifdef FEAT_CMDL_INFO
9175 clear_showcmd();
9176#endif
9177 insert_special(c, TRUE, FALSE);
9178 return NUL;
9179 }
9180 if (c != ESC)
9181 {
9182 if (redrawing() && !char_avail())
9183 {
9184 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009185 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
9187 if (char2cells(c) == 1)
9188 {
9189 /* first remove the '?', otherwise it's restored when typing
9190 * an ESC next */
9191 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009192 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 edit_putchar(c, TRUE);
9194 }
9195#ifdef FEAT_CMDL_INFO
9196 add_to_showcmd_c(c);
9197#endif
9198 }
9199 ++no_mapping;
9200 ++allow_keys;
9201 cc = safe_vgetc();
9202 --no_mapping;
9203 --allow_keys;
9204 if (cc != ESC)
9205 {
9206 AppendToRedobuff((char_u *)CTRL_V_STR);
9207 c = getdigraph(c, cc, TRUE);
9208#ifdef FEAT_CMDL_INFO
9209 clear_showcmd();
9210#endif
9211 return c;
9212 }
9213 }
9214 edit_unputchar();
9215#ifdef FEAT_CMDL_INFO
9216 clear_showcmd();
9217#endif
9218 return NUL;
9219}
9220#endif
9221
9222/*
9223 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9224 * Returns the char to be inserted, or NUL if none found.
9225 */
9226 static int
9227ins_copychar(lnum)
9228 linenr_T lnum;
9229{
9230 int c;
9231 int temp;
9232 char_u *ptr, *prev_ptr;
9233
9234 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9235 {
9236 vim_beep();
9237 return NUL;
9238 }
9239
9240 /* try to advance to the cursor column */
9241 temp = 0;
9242 ptr = ml_get(lnum);
9243 prev_ptr = ptr;
9244 validate_virtcol();
9245 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9246 {
9247 prev_ptr = ptr;
9248 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9249 }
9250 if ((colnr_T)temp > curwin->w_virtcol)
9251 ptr = prev_ptr;
9252
9253#ifdef FEAT_MBYTE
9254 c = (*mb_ptr2char)(ptr);
9255#else
9256 c = *ptr;
9257#endif
9258 if (c == NUL)
9259 vim_beep();
9260 return c;
9261}
9262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009263/*
9264 * CTRL-Y or CTRL-E typed in Insert mode.
9265 */
9266 static int
9267ins_ctrl_ey(tc)
9268 int tc;
9269{
9270 int c = tc;
9271
9272#ifdef FEAT_INS_EXPAND
9273 if (ctrl_x_mode == CTRL_X_SCROLL)
9274 {
9275 if (c == Ctrl_Y)
9276 scrolldown_clamp();
9277 else
9278 scrollup_clamp();
9279 redraw_later(VALID);
9280 }
9281 else
9282#endif
9283 {
9284 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9285 if (c != NUL)
9286 {
9287 long tw_save;
9288
9289 /* The character must be taken literally, insert like it
9290 * was typed after a CTRL-V, and pretend 'textwidth'
9291 * wasn't set. Digits, 'o' and 'x' are special after a
9292 * CTRL-V, don't use it for these. */
9293 if (c < 256 && !isalnum(c))
9294 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9295 tw_save = curbuf->b_p_tw;
9296 curbuf->b_p_tw = -1;
9297 insert_special(c, TRUE, FALSE);
9298 curbuf->b_p_tw = tw_save;
9299#ifdef FEAT_RIGHTLEFT
9300 revins_chars++;
9301 revins_legal++;
9302#endif
9303 c = Ctrl_V; /* pretend CTRL-V is last character */
9304 auto_format(FALSE, TRUE);
9305 }
9306 }
9307 return c;
9308}
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310#ifdef FEAT_SMARTINDENT
9311/*
9312 * Try to do some very smart auto-indenting.
9313 * Used when inserting a "normal" character.
9314 */
9315 static void
9316ins_try_si(c)
9317 int c;
9318{
9319 pos_T *pos, old_pos;
9320 char_u *ptr;
9321 int i;
9322 int temp;
9323
9324 /*
9325 * do some very smart indenting when entering '{' or '}'
9326 */
9327 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9328 {
9329 /*
9330 * for '}' set indent equal to indent of line containing matching '{'
9331 */
9332 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9333 {
9334 old_pos = curwin->w_cursor;
9335 /*
9336 * If the matching '{' has a ')' immediately before it (ignoring
9337 * white-space), then line up with the start of the line
9338 * containing the matching '(' if there is one. This handles the
9339 * case where an "if (..\n..) {" statement continues over multiple
9340 * lines -- webb
9341 */
9342 ptr = ml_get(pos->lnum);
9343 i = pos->col;
9344 if (i > 0) /* skip blanks before '{' */
9345 while (--i > 0 && vim_iswhite(ptr[i]))
9346 ;
9347 curwin->w_cursor.lnum = pos->lnum;
9348 curwin->w_cursor.col = i;
9349 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9350 curwin->w_cursor = *pos;
9351 i = get_indent();
9352 curwin->w_cursor = old_pos;
9353#ifdef FEAT_VREPLACE
9354 if (State & VREPLACE_FLAG)
9355 change_indent(INDENT_SET, i, FALSE, NUL);
9356 else
9357#endif
9358 (void)set_indent(i, SIN_CHANGED);
9359 }
9360 else if (curwin->w_cursor.col > 0)
9361 {
9362 /*
9363 * when inserting '{' after "O" reduce indent, but not
9364 * more than indent of previous line
9365 */
9366 temp = TRUE;
9367 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9368 {
9369 old_pos = curwin->w_cursor;
9370 i = get_indent();
9371 while (curwin->w_cursor.lnum > 1)
9372 {
9373 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9374
9375 /* ignore empty lines and lines starting with '#'. */
9376 if (*ptr != '#' && *ptr != NUL)
9377 break;
9378 }
9379 if (get_indent() >= i)
9380 temp = FALSE;
9381 curwin->w_cursor = old_pos;
9382 }
9383 if (temp)
9384 shift_line(TRUE, FALSE, 1);
9385 }
9386 }
9387
9388 /*
9389 * set indent of '#' always to 0
9390 */
9391 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9392 {
9393 /* remember current indent for next line */
9394 old_indent = get_indent();
9395 (void)set_indent(0, SIN_CHANGED);
9396 }
9397
9398 /* Adjust ai_col, the char at this position can be deleted. */
9399 if (ai_col > curwin->w_cursor.col)
9400 ai_col = curwin->w_cursor.col;
9401}
9402#endif
9403
9404/*
9405 * Get the value that w_virtcol would have when 'list' is off.
9406 * Unless 'cpo' contains the 'L' flag.
9407 */
9408 static colnr_T
9409get_nolist_virtcol()
9410{
9411 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9412 return getvcol_nolist(&curwin->w_cursor);
9413 validate_virtcol();
9414 return curwin->w_virtcol;
9415}