blob: 8d6c32a21ddeab7704bdf0dc99a7d9fd10ecd077 [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 /* For redo we need to repeat this backspace. */
3024 AppendCharToRedobuff(K_BS);
3025
3026 /* Deleted more than what was used to find matches or didn't finish
3027 * finding all matches: need to look for matches all over again. */
3028 if (curwin->w_cursor.col <= compl_col + compl_length
3029 || compl_was_interrupted)
3030 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003031
Bram Moolenaara6557602006-02-04 22:43:20 +00003032 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003033 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003034 if (compl_leader != NULL)
3035 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003036 ins_compl_new_leader();
3037 return NUL;
3038 }
3039 return K_BS;
3040}
Bram Moolenaara6557602006-02-04 22:43:20 +00003041
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003042/*
3043 * Called after changing "compl_leader".
3044 * Show the popup menu with a different set of matches.
3045 * May also search for matches again if the previous search was interrupted.
3046 */
3047 static void
3048ins_compl_new_leader()
3049{
3050 ins_compl_del_pum();
3051 ins_compl_delete();
3052 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3053 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003054
3055 if (compl_started)
3056 ins_compl_set_original_text(compl_leader);
3057 else
3058 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003059#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003060 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003061#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003062 /*
3063 * Matches were cleared, need to search for them now. First display
3064 * the changed text before the cursor. Set "compl_restarting" to
3065 * avoid that the first match is inserted.
3066 */
3067 update_screen(0);
3068#ifdef FEAT_GUI
3069 if (gui.in_use)
3070 {
3071 /* Show the cursor after the match, not after the redrawn text. */
3072 setcursor();
3073 out_flush();
3074 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003075 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003076#endif
3077 compl_restarting = TRUE;
3078 if (ins_complete(Ctrl_N) == FAIL)
3079 compl_cont_status = 0;
3080 compl_restarting = FALSE;
3081 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003082
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003083#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003084 if (!compl_used_match)
3085 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003086 /* Go to the original text, since none of the matches is inserted. */
3087 if (compl_first_match->cp_prev != NULL
3088 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3089 compl_shown_match = compl_first_match->cp_prev;
3090 else
3091 compl_shown_match = compl_first_match;
3092 compl_curr_match = compl_shown_match;
3093 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003094 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003095#endif
3096 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003097
3098 /* Show the popup menu with a different set of matches. */
3099 ins_compl_show_pum();
Bram Moolenaara6557602006-02-04 22:43:20 +00003100}
3101
3102/*
3103 * Append one character to the match leader. May reduce the number of
3104 * matches.
3105 */
3106 static void
3107ins_compl_addleader(c)
3108 int c;
3109{
3110#ifdef FEAT_MBYTE
3111 int cc;
3112
3113 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3114 {
3115 char_u buf[MB_MAXBYTES + 1];
3116
3117 (*mb_char2bytes)(c, buf);
3118 buf[cc] = NUL;
3119 ins_char_bytes(buf, cc);
3120 }
3121 else
3122#endif
3123 ins_char(c);
3124
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003125 /* For redo we need to count this character so that the number of
3126 * backspaces is correct. */
3127 AppendCharToRedobuff(c);
3128
3129 /* If we didn't complete finding matches we must search again. */
3130 if (compl_was_interrupted)
3131 ins_compl_restart();
3132
Bram Moolenaara6557602006-02-04 22:43:20 +00003133 vim_free(compl_leader);
3134 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3135 curwin->w_cursor.col - compl_col);
3136 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003137 ins_compl_new_leader();
3138}
3139
3140/*
3141 * Setup for finding completions again without leaving CTRL-X mode. Used when
3142 * BS or a key was typed while still searching for matches.
3143 */
3144 static void
3145ins_compl_restart()
3146{
3147 ins_compl_free();
3148 compl_started = FALSE;
3149 compl_matches = 0;
3150 compl_cont_status = 0;
3151 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003152}
3153
3154/*
3155 * Set the first match, the original text.
3156 */
3157 static void
3158ins_compl_set_original_text(str)
3159 char_u *str;
3160{
3161 char_u *p;
3162
3163 /* Replace the original text entry. */
3164 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3165 {
3166 p = vim_strsave(str);
3167 if (p != NULL)
3168 {
3169 vim_free(compl_first_match->cp_str);
3170 compl_first_match->cp_str = p;
3171 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003172 }
3173}
3174
3175/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003176 * Append one character to the match leader. May reduce the number of
3177 * matches.
3178 */
3179 static void
3180ins_compl_addfrommatch()
3181{
3182 char_u *p;
3183 int len = curwin->w_cursor.col - compl_col;
3184 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003185 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003186
3187 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003188 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003189 {
3190 /* When still at the original match use the first entry that matches
3191 * the leader. */
3192 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3193 {
3194 p = NULL;
3195 for (cp = compl_shown_match->cp_next; cp != NULL
3196 && cp != compl_first_match; cp = cp->cp_next)
3197 {
3198 if (ins_compl_equal(cp, compl_leader,
3199 (int)STRLEN(compl_leader)))
3200 {
3201 p = cp->cp_str;
3202 break;
3203 }
3204 }
3205 if (p == NULL || (int)STRLEN(p) <= len)
3206 return;
3207 }
3208 else
3209 return;
3210 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003211 p += len;
3212#ifdef FEAT_MBYTE
3213 c = mb_ptr2char(p);
3214#else
3215 c = *p;
3216#endif
3217 ins_compl_addleader(c);
3218}
3219
3220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003222 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003223 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003225 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226ins_compl_prep(c)
3227 int c;
3228{
3229 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 int temp;
3231 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003232 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234 /* Forget any previous 'special' messages if this is actually
3235 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3236 */
3237 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3238 edit_submode_extra = NULL;
3239
3240 /* Ignore end of Select mode mapping */
3241 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003242 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003244 /* Set "compl_get_longest" when finding the first matches. */
3245 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3246 || (ctrl_x_mode == 0 && !compl_started))
3247 {
3248 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3249 compl_used_match = TRUE;
3250 }
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3253 {
3254 /*
3255 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3256 * it will be yet. Now we decide.
3257 */
3258 switch (c)
3259 {
3260 case Ctrl_E:
3261 case Ctrl_Y:
3262 ctrl_x_mode = CTRL_X_SCROLL;
3263 if (!(State & REPLACE_FLAG))
3264 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3265 else
3266 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3267 edit_submode_pre = NULL;
3268 showmode();
3269 break;
3270 case Ctrl_L:
3271 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3272 break;
3273 case Ctrl_F:
3274 ctrl_x_mode = CTRL_X_FILES;
3275 break;
3276 case Ctrl_K:
3277 ctrl_x_mode = CTRL_X_DICTIONARY;
3278 break;
3279 case Ctrl_R:
3280 /* Simply allow ^R to happen without affecting ^X mode */
3281 break;
3282 case Ctrl_T:
3283 ctrl_x_mode = CTRL_X_THESAURUS;
3284 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003285#ifdef FEAT_COMPL_FUNC
3286 case Ctrl_U:
3287 ctrl_x_mode = CTRL_X_FUNCTION;
3288 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003289 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003290 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003291 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003292#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003293 case 's':
3294 case Ctrl_S:
3295 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003296#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003297 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003298 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003299 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003300#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 case Ctrl_RSB:
3303 ctrl_x_mode = CTRL_X_TAGS;
3304 break;
3305#ifdef FEAT_FIND_ID
3306 case Ctrl_I:
3307 case K_S_TAB:
3308 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3309 break;
3310 case Ctrl_D:
3311 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3312 break;
3313#endif
3314 case Ctrl_V:
3315 case Ctrl_Q:
3316 ctrl_x_mode = CTRL_X_CMDLINE;
3317 break;
3318 case Ctrl_P:
3319 case Ctrl_N:
3320 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3321 * just started ^X mode, or there were enough ^X's to cancel
3322 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3323 * do normal expansion when interrupting a different mode (say
3324 * ^X^F^X^P or ^P^X^X^P, see below)
3325 * nothing changes if interrupting mode 0, (eg, the flag
3326 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003327 if (!(compl_cont_status & CONT_INTRPT))
3328 compl_cont_status |= CONT_LOCAL;
3329 else if (compl_cont_mode != 0)
3330 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 /* FALLTHROUGH */
3332 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003333 /* If we have typed at least 2 ^X's... for modes != 0, we set
3334 * compl_cont_status = 0 (eg, as if we had just started ^X
3335 * mode).
3336 * For mode 0, we set "compl_cont_mode" to an impossible
3337 * value, in both cases ^X^X can be used to restart the same
3338 * mode (avoiding ADDING mode).
3339 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3340 * 'complete' and local ^P expansions respectively.
3341 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3342 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (c == Ctrl_X)
3344 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003345 if (compl_cont_mode != 0)
3346 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003348 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 ctrl_x_mode = 0;
3351 edit_submode = NULL;
3352 showmode();
3353 break;
3354 }
3355 }
3356 else if (ctrl_x_mode != 0)
3357 {
3358 /* We're already in CTRL-X mode, do we stay in it? */
3359 if (!vim_is_ctrl_x_key(c))
3360 {
3361 if (ctrl_x_mode == CTRL_X_SCROLL)
3362 ctrl_x_mode = 0;
3363 else
3364 ctrl_x_mode = CTRL_X_FINISHED;
3365 edit_submode = NULL;
3366 }
3367 showmode();
3368 }
3369
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003370 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 /* Show error message from attempted keyword completion (probably
3373 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003374 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003376 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3377 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 || ctrl_x_mode == CTRL_X_FINISHED)
3379 {
3380 /* Get here when we have finished typing a sequence of ^N and
3381 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003382 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003383 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003385 char_u *p;
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003388 * If any of the original typed text has been changed, eg when
3389 * ignorecase is set, we must add back-spaces to the redo
3390 * buffer. We add as few as necessary to delete just the part
3391 * of the original text that has changed.
3392 * When using the longest match, edited the match or used
3393 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003395 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3396 ptr = compl_curr_match->cp_str;
3397 else if (compl_leader != NULL)
3398 ptr = compl_leader;
3399 else
3400 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003401 if (compl_orig_text != NULL)
3402 {
3403 p = compl_orig_text;
3404 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3405 ++temp)
3406 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003407#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003408 if (temp > 0)
3409 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003410#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003411 for (p += temp; *p != NUL; mb_ptr_adv(p))
3412 AppendCharToRedobuff(K_BS);
3413 }
3414 if (ptr != NULL)
3415 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417
3418#ifdef FEAT_CINDENT
3419 want_cindent = (can_cindent && cindent_on());
3420#endif
3421 /*
3422 * When completing whole lines: fix indent for 'cindent'.
3423 * Otherwise, break line if it's too long.
3424 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003425 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427#ifdef FEAT_CINDENT
3428 /* re-indent the current line */
3429 if (want_cindent)
3430 {
3431 do_c_expr_indent();
3432 want_cindent = FALSE; /* don't do it again */
3433 }
3434#endif
3435 }
3436 else
3437 {
3438 /* put the cursor on the last char, for 'tw' formatting */
3439 curwin->w_cursor.col--;
3440 if (stop_arrow() == OK)
3441 insertchar(NUL, 0, -1);
3442 curwin->w_cursor.col++;
3443 }
3444
3445 auto_format(FALSE, TRUE);
3446
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003447 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003448 * the selection without inserting anything. When
3449 * compl_enter_selects is set the Enter key does the same. */
3450 if ((c == Ctrl_Y || (compl_enter_selects
3451 && (c == CAR || c == K_KENTER || c == NL)))
3452 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003453 retval = TRUE;
3454
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003455 /* CTRL-E means completion is Ended, go back to the typed text. */
3456 if (c == Ctrl_E)
3457 {
3458 ins_compl_delete();
3459 if (compl_leader != NULL)
3460 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3461 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003462 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003463 + curwin->w_cursor.col - compl_col);
3464 retval = TRUE;
3465 }
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003468 compl_started = FALSE;
3469 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 msg_clr_cmdline(); /* necessary for "noshowmode" */
3471 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003472 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (edit_submode != NULL)
3474 {
3475 edit_submode = NULL;
3476 showmode();
3477 }
3478
3479#ifdef FEAT_CINDENT
3480 /*
3481 * Indent now if a key was typed that is in 'cinkeys'.
3482 */
3483 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3484 do_c_expr_indent();
3485#endif
3486 }
3487 }
3488
3489 /* reset continue_* if we left expansion-mode, if we stay they'll be
3490 * (re)set properly in ins_complete() */
3491 if (!vim_is_ctrl_x_key(c))
3492 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 compl_cont_status = 0;
3494 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003496
3497 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498}
3499
3500/*
3501 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3502 * (depending on flag) starting from buf and looking for a non-scanned
3503 * buffer (other than curbuf). curbuf is special, if it is called with
3504 * buf=curbuf then it has to be the first call for a given flag/expansion.
3505 *
3506 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3507 */
3508 static buf_T *
3509ins_compl_next_buf(buf, flag)
3510 buf_T *buf;
3511 int flag;
3512{
3513#ifdef FEAT_WINDOWS
3514 static win_T *wp;
3515#endif
3516
3517 if (flag == 'w') /* just windows */
3518 {
3519#ifdef FEAT_WINDOWS
3520 if (buf == curbuf) /* first call for this flag/expansion */
3521 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003522 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 && wp->w_buffer->b_scanned)
3524 ;
3525 buf = wp->w_buffer;
3526#else
3527 buf = curbuf;
3528#endif
3529 }
3530 else
3531 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3532 * (unlisted buffers)
3533 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003534 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 && ((flag == 'U'
3536 ? buf->b_p_bl
3537 : (!buf->b_p_bl
3538 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 ;
3541 return buf;
3542}
3543
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003544#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003545static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003546
3547/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003548 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003549 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003550 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003551 static void
3552expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003553 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003554 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003555{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003556 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003557 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003558 char_u *funcname;
3559 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003560
Bram Moolenaare344bea2005-09-01 20:46:49 +00003561 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3562 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003563 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003564
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003565 /* Call 'completefunc' to obtain the list of matches. */
3566 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003567 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003568
Bram Moolenaare344bea2005-09-01 20:46:49 +00003569 pos = curwin->w_cursor;
3570 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3571 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003572 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003573 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003574
Bram Moolenaara94bc432006-03-10 21:42:59 +00003575 ins_compl_add_list(matchlist);
3576 list_unref(matchlist);
3577}
3578#endif /* FEAT_COMPL_FUNC */
3579
Bram Moolenaar39f05632006-03-19 22:15:26 +00003580#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003581/*
3582 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003583 */
3584 static void
3585ins_compl_add_list(list)
3586 list_T *list;
3587{
3588 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003589 int dir = compl_direction;
3590
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003591 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003592 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003593 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003594 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3595 /* if dir was BACKWARD then honor it just once */
3596 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003597 else if (did_emsg)
3598 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003599 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003600}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003601
3602/*
3603 * Add a match to the list of matches from a typeval_T.
3604 * If the given string is already in the list of completions, then return
3605 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3606 * maybe because alloc() returns NULL, then FAIL is returned.
3607 */
3608 int
3609ins_compl_add_tv(tv, dir)
3610 typval_T *tv;
3611 int dir;
3612{
3613 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003614 int icase = FALSE;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003615 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003616 char_u *(cptext[CPT_COUNT]);
3617
3618 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3619 {
3620 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3621 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3622 (char_u *)"abbr", FALSE);
3623 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3624 (char_u *)"menu", FALSE);
3625 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3626 (char_u *)"kind", FALSE);
3627 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3628 (char_u *)"info", FALSE);
3629 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3630 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003631 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3632 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003633 }
3634 else
3635 {
3636 word = get_tv_string_chk(tv);
3637 vim_memset(cptext, 0, sizeof(cptext));
3638 }
3639 if (word == NULL || *word == NUL)
3640 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003641 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003642}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003643#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003644
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645/*
3646 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003647 * The search starts at position "ini" in curbuf and in the direction
3648 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003649 * When "compl_started" is FALSE start at that position, otherwise continue
3650 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003651 * This may return before finding all the matches.
3652 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
3654 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003655ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 static pos_T first_match_pos;
3659 static pos_T last_match_pos;
3660 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003661 static int found_all = FALSE; /* Found all matches of a
3662 certain type. */
3663 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar572cb562005-08-05 21:35:02 +00003665 pos_T *pos;
3666 char_u **matches;
3667 int save_p_scs;
3668 int save_p_ws;
3669 int save_p_ic;
3670 int i;
3671 int num_matches;
3672 int len;
3673 int found_new_match;
3674 int type = ctrl_x_mode;
3675 char_u *ptr;
3676 char_u *dict = NULL;
3677 int dict_f = 0;
3678 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003680 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3683 ins_buf->b_scanned = 0;
3684 found_all = FALSE;
3685 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003687 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 last_match_pos = first_match_pos = *ini;
3689 }
3690
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003691 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003692 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3694 for (;;)
3695 {
3696 found_new_match = FAIL;
3697
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 * or if found_all says this entry is done. For ^X^L only use the
3700 * entries from 'complete' that look in loaded buffers. */
3701 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 found_all = FALSE;
3705 while (*e_cpt == ',' || *e_cpt == ' ')
3706 e_cpt++;
3707 if (*e_cpt == '.' && !curbuf->b_scanned)
3708 {
3709 ins_buf = curbuf;
3710 first_match_pos = *ini;
3711 /* So that ^N can match word immediately after cursor */
3712 if (ctrl_x_mode == 0)
3713 dec(&first_match_pos);
3714 last_match_pos = first_match_pos;
3715 type = 0;
3716 }
3717 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3718 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3719 {
3720 /* Scan a buffer, but not the current one. */
3721 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3722 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 first_match_pos.col = last_match_pos.col = 0;
3725 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3726 last_match_pos.lnum = 0;
3727 type = 0;
3728 }
3729 else /* unloaded buffer, scan like dictionary */
3730 {
3731 found_all = TRUE;
3732 if (ins_buf->b_fname == NULL)
3733 continue;
3734 type = CTRL_X_DICTIONARY;
3735 dict = ins_buf->b_fname;
3736 dict_f = DICT_EXACT;
3737 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003738 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 ins_buf->b_fname == NULL
3740 ? buf_spname(ins_buf)
3741 : ins_buf->b_sfname == NULL
3742 ? (char *)ins_buf->b_fname
3743 : (char *)ins_buf->b_sfname);
3744 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3745 }
3746 else if (*e_cpt == NUL)
3747 break;
3748 else
3749 {
3750 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3751 type = -1;
3752 else if (*e_cpt == 'k' || *e_cpt == 's')
3753 {
3754 if (*e_cpt == 'k')
3755 type = CTRL_X_DICTIONARY;
3756 else
3757 type = CTRL_X_THESAURUS;
3758 if (*++e_cpt != ',' && *e_cpt != NUL)
3759 {
3760 dict = e_cpt;
3761 dict_f = DICT_FIRST;
3762 }
3763 }
3764#ifdef FEAT_FIND_ID
3765 else if (*e_cpt == 'i')
3766 type = CTRL_X_PATH_PATTERNS;
3767 else if (*e_cpt == 'd')
3768 type = CTRL_X_PATH_DEFINES;
3769#endif
3770 else if (*e_cpt == ']' || *e_cpt == 't')
3771 {
3772 type = CTRL_X_TAGS;
3773 sprintf((char*)IObuff, _("Scanning tags."));
3774 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3775 }
3776 else
3777 type = -1;
3778
3779 /* in any case e_cpt is advanced to the next entry */
3780 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3781
3782 found_all = TRUE;
3783 if (type == -1)
3784 continue;
3785 }
3786 }
3787
3788 switch (type)
3789 {
3790 case -1:
3791 break;
3792#ifdef FEAT_FIND_ID
3793 case CTRL_X_PATH_PATTERNS:
3794 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003795 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003796 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003798 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3800 (linenr_T)1, (linenr_T)MAXLNUM);
3801 break;
3802#endif
3803
3804 case CTRL_X_DICTIONARY:
3805 case CTRL_X_THESAURUS:
3806 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003807 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 : (type == CTRL_X_THESAURUS
3809 ? (*curbuf->b_p_tsr == NUL
3810 ? p_tsr
3811 : curbuf->b_p_tsr)
3812 : (*curbuf->b_p_dict == NUL
3813 ? p_dict
3814 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003815 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003816 dict != NULL ? dict_f
3817 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 dict = NULL;
3819 break;
3820
3821 case CTRL_X_TAGS:
3822 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3823 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 * of matches is found when compl_pattern is empty */
3828 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3830 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3831 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3832 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00003833 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 p_ic = save_p_ic;
3836 break;
3837
3838 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3841 {
3842
3843 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003845 ins_compl_add_matches(num_matches, matches,
3846#ifdef CASE_INSENSITIVE_FILENAME
3847 TRUE
3848#else
3849 FALSE
3850#endif
3851 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853 break;
3854
3855 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 if (expand_cmdline(&compl_xp, compl_pattern,
3857 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003859 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 break;
3861
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003862#ifdef FEAT_COMPL_FUNC
3863 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003864 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003865 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003866 break;
3867#endif
3868
Bram Moolenaar488c6512005-08-11 20:09:58 +00003869 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003870#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003871 num_matches = expand_spelling(first_match_pos.lnum,
3872 first_match_pos.col, compl_pattern, &matches);
3873 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003874 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003875#endif
3876 break;
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 default: /* normal ^P/^N and ^X^L */
3879 /*
3880 * If 'infercase' is set, don't use 'smartcase' here
3881 */
3882 save_p_scs = p_scs;
3883 if (ins_buf->b_p_inf)
3884 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 /* buffers other than curbuf are scanned from the beginning or the
3887 * end but never from the middle, thus setting nowrapscan in this
3888 * buffers is a good idea, on the other hand, we always set
3889 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3890 save_p_ws = p_ws;
3891 if (ins_buf != curbuf)
3892 p_ws = FALSE;
3893 else if (*e_cpt == '.')
3894 p_ws = TRUE;
3895 for (;;)
3896 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003897 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003899 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3900 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003902 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003904 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003906 found_new_match = searchit(NULL, ins_buf, pos,
3907 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003909 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003912 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003913 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 first_match_pos = *pos;
3915 last_match_pos = *pos;
3916 }
3917 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003918 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 found_new_match = FAIL;
3920 if (found_new_match == FAIL)
3921 {
3922 if (ins_buf == curbuf)
3923 found_all = TRUE;
3924 break;
3925 }
3926
3927 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 && ini->lnum == pos->lnum
3930 && ini->col == pos->col)
3931 continue;
3932 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3933 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3934 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003935 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
3937 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3938 continue;
3939 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3940 if (!p_paste)
3941 ptr = skipwhite(ptr);
3942 }
3943 len = (int)STRLEN(ptr);
3944 }
3945 else
3946 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947 char_u *tmp_ptr = ptr;
3948
3949 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 /* Skip if already inside a word. */
3953 if (vim_iswordp(tmp_ptr))
3954 continue;
3955 /* Find start of next word. */
3956 tmp_ptr = find_word_start(tmp_ptr);
3957 }
3958 /* Find end of this word. */
3959 tmp_ptr = find_word_end(tmp_ptr);
3960 len = (int)(tmp_ptr - ptr);
3961
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 if ((compl_cont_status & CONT_ADDING)
3963 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3966 {
3967 /* Try next line, if any. the new word will be
3968 * "join" as if the normal command "J" was used.
3969 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003970 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 * works -- Acevedo */
3972 STRNCPY(IObuff, ptr, len);
3973 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3974 tmp_ptr = ptr = skipwhite(ptr);
3975 /* Find start of next word. */
3976 tmp_ptr = find_word_start(tmp_ptr);
3977 /* Find end of next word. */
3978 tmp_ptr = find_word_end(tmp_ptr);
3979 if (tmp_ptr > ptr)
3980 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003981 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003983 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 IObuff[len++] = ' ';
3985 /* IObuf =~ "\k.* ", thus len >= 2 */
3986 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003987 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 || (vim_strchr(p_cpo, CPO_JOINSP)
3989 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003990 && (IObuff[len - 2] == '?'
3991 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 IObuff[len++] = ' ';
3993 }
3994 /* copy as much as posible of the new word */
3995 if (tmp_ptr - ptr >= IOSIZE - len)
3996 tmp_ptr = ptr + IOSIZE - len - 1;
3997 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3998 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003999 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 IObuff[len] = NUL;
4002 ptr = IObuff;
4003 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004004 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 continue;
4006 }
4007 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004008 if (ins_compl_add_infercase(ptr, len, FALSE,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004009 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004010 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 found_new_match = OK;
4013 break;
4014 }
4015 }
4016 p_scs = save_p_scs;
4017 p_ws = save_p_ws;
4018 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004019
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004020 /* check if compl_curr_match has changed, (e.g. other type of
4021 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004022 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 found_new_match = OK;
4024
4025 /* break the loop for specialized modes (use 'complete' just for the
4026 * generic ctrl_x_mode == 0) or when we've found a new match */
4027 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004029 {
4030 if (got_int)
4031 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004032 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004033 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004034 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004035
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004036 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4037 || compl_interrupted)
4038 break;
4039 compl_started = TRUE;
4040 }
4041 else
4042 {
4043 /* Mark a buffer scanned when it has been scanned completely */
4044 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4045 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004047 compl_started = FALSE;
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4053 && *e_cpt == NUL) /* Got to end of 'complete' */
4054 found_new_match = FAIL;
4055
4056 i = -1; /* total of matches, unknown */
4057 if (found_new_match == FAIL
4058 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4059 i = ins_compl_make_cyclic();
4060
4061 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 * just been made cyclic then we have to move compl_curr_match to the next
4063 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004064 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4065 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004066 if (compl_curr_match == NULL)
4067 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return i;
4069}
4070
4071/* Delete the old text being completed. */
4072 static void
4073ins_compl_delete()
4074{
4075 int i;
4076
4077 /*
4078 * In insert mode: Delete the typed part.
4079 * In replace mode: Put the old characters back, if any.
4080 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004081 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 backspace_until_column(i);
4083 changed_cline_bef_curs();
4084}
4085
4086/* Insert the new text being completed. */
4087 static void
4088ins_compl_insert()
4089{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004090 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004091 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4092 compl_used_match = FALSE;
4093 else
4094 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095}
4096
4097/*
4098 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004099 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4100 * get more completions. If it is FALSE, then we just do nothing when there
4101 * are no more completions in a given direction. The latter case is used when
4102 * we are still in the middle of finding completions, to allow browsing
4103 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * Return the total number of matches, or -1 if still unknown -- webb.
4105 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004106 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4107 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 *
4109 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004110 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4111 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 */
4113 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004114ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004116 int count; /* repeat completion this many times; should
4117 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004118 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 int num_matches = -1;
4121 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004122 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004123 compl_T *found_compl = NULL;
4124 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004125 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004127 if (compl_leader != NULL
4128 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004130 /* Set "compl_shown_match" to the actually shown match, it may differ
4131 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004132 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004133 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004134 && compl_shown_match->cp_next != NULL
4135 && compl_shown_match->cp_next != compl_first_match)
4136 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004137
4138 /* If we didn't find it searching forward, and compl_shows_dir is
4139 * backward, find the last match. */
4140 if (compl_shows_dir == BACKWARD
4141 && !ins_compl_equal(compl_shown_match,
4142 compl_leader, (int)STRLEN(compl_leader))
4143 && (compl_shown_match->cp_next == NULL
4144 || compl_shown_match->cp_next == compl_first_match))
4145 {
4146 while (!ins_compl_equal(compl_shown_match,
4147 compl_leader, (int)STRLEN(compl_leader))
4148 && compl_shown_match->cp_prev != NULL
4149 && compl_shown_match->cp_prev != compl_first_match)
4150 compl_shown_match = compl_shown_match->cp_prev;
4151 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004152 }
4153
4154 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004155 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 /* Delete old text to be replaced */
4157 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004158
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004159 /* When finding the longest common text we stick at the original text,
4160 * don't let CTRL-N or CTRL-P move to the first match. */
4161 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4162
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004163 /* When restarting the search don't insert the first match either. */
4164 if (compl_restarting)
4165 {
4166 advance = FALSE;
4167 compl_restarting = FALSE;
4168 }
4169
Bram Moolenaare3226be2005-12-18 22:10:00 +00004170 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4171 * around. */
4172 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004174 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004176 if (compl_pending != 0)
4177 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004178 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004179 found_end = (compl_first_match != NULL
4180 && (compl_shown_match->cp_next == compl_first_match
4181 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004182 }
4183 else if (compl_shows_dir == BACKWARD
4184 && compl_shown_match->cp_prev != NULL)
4185 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004186 if (compl_pending != 0)
4187 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004188 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004189 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004190 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004193 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004194 if (advance)
4195 {
4196 if (compl_shows_dir == BACKWARD)
4197 --compl_pending;
4198 else
4199 ++compl_pending;
4200 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004201 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004202 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004203
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004204 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004205 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004206 if (compl_pending != 0 && compl_direction == compl_shows_dir
4207 && advance)
Bram Moolenaara6557602006-02-04 22:43:20 +00004208 compl_shown_match = compl_curr_match;
4209 found_end = FALSE;
4210 }
4211 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4212 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004213 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004214 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004215 ++todo;
4216 else
4217 /* Remember a matching item. */
4218 found_compl = compl_shown_match;
4219
4220 /* Stop at the end of the list when we found a usable match. */
4221 if (found_end)
4222 {
4223 if (found_compl != NULL)
4224 {
4225 compl_shown_match = found_compl;
4226 break;
4227 }
4228 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004232 /* Insert the text of the new completion, or the compl_leader. */
4233 if (insert_match)
4234 {
4235 if (!compl_get_longest || compl_used_match)
4236 ins_compl_insert();
4237 else
4238 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4239 }
4240 else
4241 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 if (!allow_get_expansion)
4244 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004245 /* may undisplay the popup menu first */
4246 ins_compl_upd_pum();
4247
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004248 /* redraw to show the user what was inserted */
4249 update_screen(0);
4250
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004251 /* display the updated popup menu */
4252 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004253#ifdef FEAT_GUI
4254 if (gui.in_use)
4255 {
4256 /* Show the cursor after the match, not after the redrawn text. */
4257 setcursor();
4258 out_flush();
4259 gui_update_cursor(FALSE, FALSE);
4260 }
4261#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 /* Delete old text to be replaced, since we're still searching and
4264 * don't want to match ourselves! */
4265 ins_compl_delete();
4266 }
4267
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004268 /* Enter will select a match when the match wasn't inserted and the popup
4269 * menu is visislbe. */
4270 compl_enter_selects = !insert_match && compl_match_array != NULL;
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 /*
4273 * Show the file name for the match (if any)
4274 * Truncate the file name to avoid a wait for return.
4275 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004276 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
4278 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004279 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 if (i <= 0)
4281 i = 0;
4282 else
4283 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004284 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 msg(IObuff);
4286 redraw_cmdline = FALSE; /* don't overwrite! */
4287 }
4288
4289 return num_matches;
4290}
4291
4292/*
4293 * Call this while finding completions, to check whether the user has hit a key
4294 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004295 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004297 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 */
4299 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004300ins_compl_check_keys(frequency)
4301 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
4303 static int count = 0;
4304
4305 int c;
4306
4307 /* Don't check when reading keys from a script. That would break the test
4308 * scripts */
4309 if (using_script())
4310 return;
4311
4312 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004313 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return;
4315 count = 0;
4316
4317 ++no_mapping;
4318 c = vpeekc_any();
4319 --no_mapping;
4320 if (c != NUL)
4321 {
4322 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4323 {
4324 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004325 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004326 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4327 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004332 if (compl_pending != 0 && !got_int)
4333 (void)ins_compl_next(FALSE, compl_pending > 0
4334 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004335}
4336
4337/*
4338 * Decide the direction of Insert mode complete from the key typed.
4339 * Returns BACKWARD or FORWARD.
4340 */
4341 static int
4342ins_compl_key2dir(c)
4343 int c;
4344{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004345 if (c == Ctrl_P || c == Ctrl_L
4346 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4347 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004348 return BACKWARD;
4349 return FORWARD;
4350}
4351
4352/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004353 * Return TRUE for keys that are used for completion only when the popup menu
4354 * is visible.
4355 */
4356 static int
4357ins_compl_pum_key(c)
4358 int c;
4359{
4360 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004361 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4362 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004363}
4364
4365/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004366 * Decide the number of completions to move forward.
4367 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4368 */
4369 static int
4370ins_compl_key2count(c)
4371 int c;
4372{
4373 int h;
4374
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004375 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004376 {
4377 h = pum_get_height();
4378 if (h > 3)
4379 h -= 2; /* keep some context */
4380 return h;
4381 }
4382 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383}
4384
4385/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004386 * Return TRUE if completion with "c" should insert the match, FALSE if only
4387 * to change the currently selected completion.
4388 */
4389 static int
4390ins_compl_use_match(c)
4391 int c;
4392{
4393 switch (c)
4394 {
4395 case K_UP:
4396 case K_DOWN:
4397 case K_PAGEDOWN:
4398 case K_KPAGEDOWN:
4399 case K_S_DOWN:
4400 case K_PAGEUP:
4401 case K_KPAGEUP:
4402 case K_S_UP:
4403 return FALSE;
4404 }
4405 return TRUE;
4406}
4407
4408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 * Do Insert mode completion.
4410 * Called when character "c" was typed, which has a meaning for completion.
4411 * Returns OK if completion was done, FAIL if something failed (out of mem).
4412 */
4413 static int
4414ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 char_u *line;
4418 int startcol = 0; /* column where searched text starts */
4419 colnr_T curs_col; /* cursor column */
4420 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaare3226be2005-12-18 22:10:00 +00004422 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 /* First time we hit ^N or ^P (in a row, I mean) */
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 did_ai = FALSE;
4428#ifdef FEAT_SMARTINDENT
4429 did_si = FALSE;
4430 can_si = FALSE;
4431 can_si_back = FALSE;
4432#endif
4433 if (stop_arrow() == FAIL)
4434 return FAIL;
4435
4436 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004438 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 * "compl_startpos" to the cursor as a pattern to add a new word
4442 * instead of expand the one before the cursor, in word-wise if
4443 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 * is not in the same line as the cursor then fix it (the line has
4445 * been split because it was longer than 'tw'). if SOL is set then
4446 * skip the previous pattern, a word at the beginning of the line has
4447 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004448 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4449 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 /*
4452 * it is a continued search
4453 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4456 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4457 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 /* line (probably) wrapped, set compl_startpos to the
4461 * first non_blank in the line, if it is not a wordchar
4462 * include it to get a better pattern, but then we don't
4463 * want the "\\<" prefix, check it bellow */
4464 compl_col = (colnr_T)(skipwhite(line) - line);
4465 compl_startpos.col = compl_col;
4466 compl_startpos.lnum = curwin->w_cursor.lnum;
4467 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else
4470 {
4471 /* S_IPOS was set when we inserted a word that was at the
4472 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004473 * mode but first we need to redefine compl_startpos */
4474 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 compl_cont_status |= CONT_SOL;
4477 compl_startpos.col = (colnr_T)(skipwhite(
4478 line + compl_length
4479 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004481 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004483 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004484 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 * have enough space? just being paranoic */
4486#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 compl_cont_status &= ~CONT_SOL;
4490 compl_length = (IOSIZE - MIN_SPACE);
4491 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4494 if (compl_length < 1)
4495 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004509 compl_cont_status = 0;
4510 compl_cont_status |= CONT_N_ADDS;
4511 compl_startpos = curwin->w_cursor;
4512 startcol = (int)curs_col;
4513 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515
4516 /* Work out completion pattern and original text -- webb */
4517 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4518 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4521 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004526 compl_col += ++startcol;
4527 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 compl_pattern = str_foldcase(line + compl_col,
4531 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004533 compl_pattern = vim_strnsave(line + compl_col,
4534 compl_length);
4535 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 return FAIL;
4537 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
4540 char_u *prefix = (char_u *)"\\<";
4541
4542 /* we need 3 extra chars, 1 for the NUL and
4543 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004544 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4545 compl_length) + 3);
4546 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 if (!vim_iswordp(line + compl_col)
4549 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 && (
4551#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#endif
4556 )))
4557 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 STRCPY((char *)compl_pattern, prefix);
4559 (void)quote_meta(compl_pattern + STRLEN(prefix),
4560 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#endif
4568 )
4569 {
4570 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4572 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 compl_col += curs_col;
4575 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577 else
4578 {
4579#ifdef FEAT_MBYTE
4580 /* Search the point of change class of multibyte character
4581 * or not a word single byte character backward. */
4582 if (has_mbyte)
4583 {
4584 int base_class;
4585 int head_off;
4586
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 startcol -= (*mb_head_off)(line, line + startcol);
4588 base_class = mb_get_class(line + startcol);
4589 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 head_off = (*mb_head_off)(line, line + startcol);
4592 if (base_class != mb_get_class(line + startcol
4593 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
4598 else
4599#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004602 compl_col += ++startcol;
4603 compl_length = (int)curs_col - startcol;
4604 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 /* Only match word with at least two chars -- webb
4607 * there's no need to call quote_meta,
4608 * alloc(7) is enough -- Acevedo
4609 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 compl_pattern = alloc(7);
4611 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 STRCPY((char *)compl_pattern, "\\<");
4614 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4615 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617 else
4618 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004619 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4620 compl_length) + 3);
4621 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 STRCPY((char *)compl_pattern, "\\<");
4624 (void)quote_meta(compl_pattern + 2, line + compl_col,
4625 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
4627 }
4628 }
4629 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4630 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004631 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 compl_length = (int)curs_col - (int)compl_col;
4633 if (compl_length < 0) /* cursor in indent: empty pattern */
4634 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004636 compl_pattern = str_foldcase(line + compl_col, compl_length,
4637 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4640 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 return FAIL;
4642 }
4643 else if (ctrl_x_mode == CTRL_X_FILES)
4644 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 compl_col += ++startcol;
4648 compl_length = (int)curs_col - startcol;
4649 compl_pattern = addstar(line + compl_col, compl_length,
4650 EXPAND_FILES);
4651 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 return FAIL;
4653 }
4654 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4655 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 compl_pattern = vim_strnsave(line, curs_col);
4657 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004659 set_cmd_context(&compl_xp, compl_pattern,
4660 (int)STRLEN(compl_pattern), curs_col);
4661 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4662 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004663 {
4664 compl_col = curs_col;
4665 compl_length = 0;
4666 vim_free(compl_pattern);
4667 compl_pattern = NULL;
4668 }
4669 else
4670 {
4671 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4672 compl_col = startcol;
4673 compl_length = curs_col - startcol;
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004676 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004677 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004678#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004679 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004680 * Call user defined function 'completefunc' with "a:findstart"
4681 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004682 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004683 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004684 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004685 char_u *funcname;
4686 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004687
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004688 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004689 * string */
4690 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4691 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4692 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004693 {
4694 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4695 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004696 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004697 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004698
4699 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004700 args[1] = NULL;
4701 pos = curwin->w_cursor;
4702 col = call_func_retnr(funcname, 2, args, FALSE);
4703 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004704
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004705 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004706 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004707 compl_col = col;
4708 if ((colnr_T)compl_col > curs_col)
4709 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004710
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004711 /* Setup variables for completion. Need to obtain "line" again,
4712 * it may have become invalid. */
4713 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004714 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004715 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4716 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004717#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 return FAIL;
4719 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004720 else if (ctrl_x_mode == CTRL_X_SPELL)
4721 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004722#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004723 if (spell_bad_len > 0)
4724 compl_col = curs_col - spell_bad_len;
4725 else
4726 compl_col = spell_word_start(startcol);
4727 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004728 {
4729 compl_length = 0;
4730 compl_col = curs_col;
4731 }
4732 else
4733 {
4734 spell_expand_check_cap(compl_col);
4735 compl_length = (int)curs_col - compl_col;
4736 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004737 /* Need to obtain "line" again, it may have become invalid. */
4738 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004739 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4740 if (compl_pattern == NULL)
4741#endif
4742 return FAIL;
4743 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004744 else
4745 {
4746 EMSG2(_(e_intern2), "ins_complete()");
4747 return FAIL;
4748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004750 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 edit_submode_pre = (char_u *)_(" Adding");
4753 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4754 {
4755 /* Insert a new line, keep indentation but ignore 'comments' */
4756#ifdef FEAT_COMMENTS
4757 char_u *old = curbuf->b_p_com;
4758
4759 curbuf->b_p_com = (char_u *)"";
4760#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 compl_startpos.lnum = curwin->w_cursor.lnum;
4762 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 ins_eol('\r');
4764#ifdef FEAT_COMMENTS
4765 curbuf->b_p_com = old;
4766#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 compl_length = 0;
4768 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 }
4771 else
4772 {
4773 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004774 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004777 if (compl_cont_status & CONT_LOCAL)
4778 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4781
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004782 /* Always add completion for the original text. */
4783 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004784 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4785 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004786 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 vim_free(compl_pattern);
4789 compl_pattern = NULL;
4790 vim_free(compl_orig_text);
4791 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 return FAIL;
4793 }
4794
4795 /* showmode might reset the internal line pointers, so it must
4796 * be called before line = ml_get(), or when this address is no
4797 * longer needed. -- Acevedo.
4798 */
4799 edit_submode_extra = (char_u *)_("-- Searching...");
4800 edit_submode_highl = HLF_COUNT;
4801 showmode();
4802 edit_submode_extra = NULL;
4803 out_flush();
4804 }
4805
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004806 compl_shown_match = compl_curr_match;
4807 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
4809 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004810 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004812 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004814 /* may undisplay the popup menu */
4815 ins_compl_upd_pum();
4816
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 if (n > 1) /* all matches have been found */
4818 compl_matches = n;
4819 compl_curr_match = compl_shown_match;
4820 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
Bram Moolenaard68071d2006-05-02 22:08:30 +00004822 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4823 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (got_int && !global_busy)
4825 {
4826 (void)vgetc();
4827 got_int = FALSE;
4828 }
4829
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004830 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004831 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4834 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4836 edit_submode_highl = HLF_E;
4837 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4838 * because we couldn't expand anything at first place, but if we used
4839 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4840 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004841 if ( compl_length > 1
4842 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 || (ctrl_x_mode != 0
4844 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4845 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004846 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848
Bram Moolenaar572cb562005-08-05 21:35:02 +00004849 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004850 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 if (edit_submode_extra == NULL)
4855 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004856 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
4858 edit_submode_extra = (char_u *)_("Back at original");
4859 edit_submode_highl = HLF_W;
4860 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004861 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
4863 edit_submode_extra = (char_u *)_("Word from other line");
4864 edit_submode_highl = HLF_COUNT;
4865 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004866 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 edit_submode_extra = (char_u *)_("The only match");
4869 edit_submode_highl = HLF_COUNT;
4870 }
4871 else
4872 {
4873 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004874 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004876 int number = 0;
4877 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004879 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 /* search backwards for the first valid (!= -1) number.
4882 * This should normally succeed already at the first loop
4883 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004884 for (match = compl_curr_match->cp_prev; match != NULL
4885 && match != compl_first_match;
4886 match = match->cp_prev)
4887 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004889 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 break;
4891 }
4892 if (match != NULL)
4893 /* go up and assign all numbers which are not assigned
4894 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004895 for (match = match->cp_next;
4896 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004897 match = match->cp_next)
4898 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 else /* BACKWARD */
4901 {
4902 /* search forwards (upwards) for the first valid (!= -1)
4903 * number. This should normally succeed already at the
4904 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004905 for (match = compl_curr_match->cp_next; match != NULL
4906 && match != compl_first_match;
4907 match = match->cp_next)
4908 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004910 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 break;
4912 }
4913 if (match != NULL)
4914 /* go down and assign all numbers which are not
4915 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004916 for (match = match->cp_prev; match
4917 && match->cp_number == -1;
4918 match = match->cp_prev)
4919 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921 }
4922
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004923 /* The match should always have a sequence number now, this is
4924 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004925 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 /* Space for 10 text chars. + 2x10-digit no.s */
4928 static char_u match_ref[31];
4929
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004930 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004932 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004934 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004935 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004936 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 edit_submode_extra = match_ref;
4938 edit_submode_highl = HLF_R;
4939 if (dollar_vcol)
4940 curs_columns(FALSE);
4941 }
4942 }
4943 }
4944
4945 /* Show a message about what (completion) mode we're in. */
4946 showmode();
4947 if (edit_submode_extra != NULL)
4948 {
4949 if (!p_smd)
4950 msg_attr(edit_submode_extra,
4951 edit_submode_highl < HLF_COUNT
4952 ? hl_attr(edit_submode_highl) : 0);
4953 }
4954 else
4955 msg_clr_cmdline(); /* necessary for "noshowmode" */
4956
Bram Moolenaard68071d2006-05-02 22:08:30 +00004957 /* Show the popup menu, unless we got interrupted. */
4958 if (!compl_interrupted)
4959 {
4960 /* RedrawingDisabled may be set when invoked through complete(). */
4961 n = RedrawingDisabled;
4962 RedrawingDisabled = 0;
4963 ins_compl_show_pum();
4964 setcursor();
4965 RedrawingDisabled = n;
4966 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004967 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004968 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 return OK;
4971}
4972
4973/*
4974 * Looks in the first "len" chars. of "src" for search-metachars.
4975 * If dest is not NULL the chars. are copied there quoting (with
4976 * a backslash) the metachars, and dest would be NUL terminated.
4977 * Returns the length (needed) of dest
4978 */
4979 static int
4980quote_meta(dest, src, len)
4981 char_u *dest;
4982 char_u *src;
4983 int len;
4984{
4985 int m;
4986
4987 for (m = len; --len >= 0; src++)
4988 {
4989 switch (*src)
4990 {
4991 case '.':
4992 case '*':
4993 case '[':
4994 if (ctrl_x_mode == CTRL_X_DICTIONARY
4995 || ctrl_x_mode == CTRL_X_THESAURUS)
4996 break;
4997 case '~':
4998 if (!p_magic) /* quote these only if magic is set */
4999 break;
5000 case '\\':
5001 if (ctrl_x_mode == CTRL_X_DICTIONARY
5002 || ctrl_x_mode == CTRL_X_THESAURUS)
5003 break;
5004 case '^': /* currently it's not needed. */
5005 case '$':
5006 m++;
5007 if (dest != NULL)
5008 *dest++ = '\\';
5009 break;
5010 }
5011 if (dest != NULL)
5012 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005013# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 /* Copy remaining bytes of a multibyte character. */
5015 if (has_mbyte)
5016 {
5017 int i, mb_len;
5018
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005019 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (mb_len > 0 && len >= mb_len)
5021 for (i = 0; i < mb_len; ++i)
5022 {
5023 --len;
5024 ++src;
5025 if (dest != NULL)
5026 *dest++ = *src;
5027 }
5028 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 if (dest != NULL)
5032 *dest = NUL;
5033
5034 return m;
5035}
5036#endif /* FEAT_INS_EXPAND */
5037
5038/*
5039 * Next character is interpreted literally.
5040 * A one, two or three digit decimal number is interpreted as its byte value.
5041 * If one or two digits are entered, the next character is given to vungetc().
5042 * For Unicode a character > 255 may be returned.
5043 */
5044 int
5045get_literal()
5046{
5047 int cc;
5048 int nc;
5049 int i;
5050 int hex = FALSE;
5051 int octal = FALSE;
5052#ifdef FEAT_MBYTE
5053 int unicode = 0;
5054#endif
5055
5056 if (got_int)
5057 return Ctrl_C;
5058
5059#ifdef FEAT_GUI
5060 /*
5061 * In GUI there is no point inserting the internal code for a special key.
5062 * It is more useful to insert the string "<KEY>" instead. This would
5063 * probably be useful in a text window too, but it would not be
5064 * vi-compatible (maybe there should be an option for it?) -- webb
5065 */
5066 if (gui.in_use)
5067 ++allow_keys;
5068#endif
5069#ifdef USE_ON_FLY_SCROLL
5070 dont_scroll = TRUE; /* disallow scrolling here */
5071#endif
5072 ++no_mapping; /* don't map the next key hits */
5073 cc = 0;
5074 i = 0;
5075 for (;;)
5076 {
5077 do
5078 nc = safe_vgetc();
5079 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5080 || nc == K_HOR_SCROLLBAR);
5081#ifdef FEAT_CMDL_INFO
5082 if (!(State & CMDLINE)
5083# ifdef FEAT_MBYTE
5084 && MB_BYTE2LEN_CHECK(nc) == 1
5085# endif
5086 )
5087 add_to_showcmd(nc);
5088#endif
5089 if (nc == 'x' || nc == 'X')
5090 hex = TRUE;
5091 else if (nc == 'o' || nc == 'O')
5092 octal = TRUE;
5093#ifdef FEAT_MBYTE
5094 else if (nc == 'u' || nc == 'U')
5095 unicode = nc;
5096#endif
5097 else
5098 {
5099 if (hex
5100#ifdef FEAT_MBYTE
5101 || unicode != 0
5102#endif
5103 )
5104 {
5105 if (!vim_isxdigit(nc))
5106 break;
5107 cc = cc * 16 + hex2nr(nc);
5108 }
5109 else if (octal)
5110 {
5111 if (nc < '0' || nc > '7')
5112 break;
5113 cc = cc * 8 + nc - '0';
5114 }
5115 else
5116 {
5117 if (!VIM_ISDIGIT(nc))
5118 break;
5119 cc = cc * 10 + nc - '0';
5120 }
5121
5122 ++i;
5123 }
5124
5125 if (cc > 255
5126#ifdef FEAT_MBYTE
5127 && unicode == 0
5128#endif
5129 )
5130 cc = 255; /* limit range to 0-255 */
5131 nc = 0;
5132
5133 if (hex) /* hex: up to two chars */
5134 {
5135 if (i >= 2)
5136 break;
5137 }
5138#ifdef FEAT_MBYTE
5139 else if (unicode) /* Unicode: up to four or eight chars */
5140 {
5141 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5142 break;
5143 }
5144#endif
5145 else if (i >= 3) /* decimal or octal: up to three chars */
5146 break;
5147 }
5148 if (i == 0) /* no number entered */
5149 {
5150 if (nc == K_ZERO) /* NUL is stored as NL */
5151 {
5152 cc = '\n';
5153 nc = 0;
5154 }
5155 else
5156 {
5157 cc = nc;
5158 nc = 0;
5159 }
5160 }
5161
5162 if (cc == 0) /* NUL is stored as NL */
5163 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005164#ifdef FEAT_MBYTE
5165 if (enc_dbcs && (cc & 0xff) == 0)
5166 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5167 second byte will cause trouble! */
5168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 --no_mapping;
5171#ifdef FEAT_GUI
5172 if (gui.in_use)
5173 --allow_keys;
5174#endif
5175 if (nc)
5176 vungetc(nc);
5177 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5178 return cc;
5179}
5180
5181/*
5182 * Insert character, taking care of special keys and mod_mask
5183 */
5184 static void
5185insert_special(c, allow_modmask, ctrlv)
5186 int c;
5187 int allow_modmask;
5188 int ctrlv; /* c was typed after CTRL-V */
5189{
5190 char_u *p;
5191 int len;
5192
5193 /*
5194 * Special function key, translate into "<Key>". Up to the last '>' is
5195 * inserted with ins_str(), so as not to replace characters in replace
5196 * mode.
5197 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5198 * unless 'allow_modmask' is TRUE.
5199 */
5200#ifdef MACOS
5201 /* Command-key never produces a normal key */
5202 if (mod_mask & MOD_MASK_CMD)
5203 allow_modmask = TRUE;
5204#endif
5205 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5206 {
5207 p = get_special_key_name(c, mod_mask);
5208 len = (int)STRLEN(p);
5209 c = p[len - 1];
5210 if (len > 2)
5211 {
5212 if (stop_arrow() == FAIL)
5213 return;
5214 p[len - 1] = NUL;
5215 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005216 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 ctrlv = FALSE;
5218 }
5219 }
5220 if (stop_arrow() == OK)
5221 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5222}
5223
5224/*
5225 * Special characters in this context are those that need processing other
5226 * than the simple insertion that can be performed here. This includes ESC
5227 * which terminates the insert, and CR/NL which need special processing to
5228 * open up a new line. This routine tries to optimize insertions performed by
5229 * the "redo", "undo" or "put" commands, so it needs to know when it should
5230 * stop and defer processing to the "normal" mechanism.
5231 * '0' and '^' are special, because they can be followed by CTRL-D.
5232 */
5233#ifdef EBCDIC
5234# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5235#else
5236# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5237#endif
5238
5239#ifdef FEAT_MBYTE
5240# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5241#else
5242# define WHITECHAR(cc) vim_iswhite(cc)
5243#endif
5244
5245 void
5246insertchar(c, flags, second_indent)
5247 int c; /* character to insert or NUL */
5248 int flags; /* INSCHAR_FORMAT, etc. */
5249 int second_indent; /* indent for second line if >= 0 */
5250{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 int textwidth;
5252#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5258 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 /*
5261 * Try to break the line in two or more pieces when:
5262 * - Always do this if we have been called to do formatting only.
5263 * - Always do this when 'formatoptions' has the 'a' flag and the line
5264 * ends in white space.
5265 * - Otherwise:
5266 * - Don't do this if inserting a blank
5267 * - Don't do this if an existing character is being replaced, unless
5268 * we're in VREPLACE mode.
5269 * - Do this if the cursor is not on the line where insert started
5270 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5271 * before the insert.
5272 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5273 * before 'textwidth'
5274 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005275 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 && ((flags & INSCHAR_FORMAT)
5277 || (!vim_iswhite(c)
5278 && !((State & REPLACE_FLAG)
5279#ifdef FEAT_VREPLACE
5280 && !(State & VREPLACE_FLAG)
5281#endif
5282 && *ml_get_cursor() != NUL)
5283 && (curwin->w_cursor.lnum != Insstart.lnum
5284 || ((!has_format_option(FO_INS_LONG)
5285 || Insstart_textlen <= (colnr_T)textwidth)
5286 && (!fo_ins_blank
5287 || Insstart_blank_vcol <= (colnr_T)textwidth
5288 ))))))
5289 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005290 /* Format with 'formatexpr' when it's set. Use internal formatting
5291 * when 'formatexpr' isn't set or it returns non-zero. */
5292#if defined(FEAT_EVAL)
5293 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005294 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005296 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (c == NUL) /* only formatting was wanted */
5300 return;
5301
5302#ifdef FEAT_COMMENTS
5303 /* Check whether this character should end a comment. */
5304 if (did_ai && (int)c == end_comment_pending)
5305 {
5306 char_u *line;
5307 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5308 int middle_len, end_len;
5309 int i;
5310
5311 /*
5312 * Need to remove existing (middle) comment leader and insert end
5313 * comment leader. First, check what comment leader we can find.
5314 */
5315 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5316 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5317 {
5318 /* Skip middle-comment string */
5319 while (*p && p[-1] != ':') /* find end of middle flags */
5320 ++p;
5321 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5322 /* Don't count trailing white space for middle_len */
5323 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5324 --middle_len;
5325
5326 /* Find the end-comment string */
5327 while (*p && p[-1] != ':') /* find end of end flags */
5328 ++p;
5329 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5330
5331 /* Skip white space before the cursor */
5332 i = curwin->w_cursor.col;
5333 while (--i >= 0 && vim_iswhite(line[i]))
5334 ;
5335 i++;
5336
5337 /* Skip to before the middle leader */
5338 i -= middle_len;
5339
5340 /* Check some expected things before we go on */
5341 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5342 {
5343 /* Backspace over all the stuff we want to replace */
5344 backspace_until_column(i);
5345
5346 /*
5347 * Insert the end-comment string, except for the last
5348 * character, which will get inserted as normal later.
5349 */
5350 ins_bytes_len(lead_end, end_len - 1);
5351 }
5352 }
5353 }
5354 end_comment_pending = NUL;
5355#endif
5356
5357 did_ai = FALSE;
5358#ifdef FEAT_SMARTINDENT
5359 did_si = FALSE;
5360 can_si = FALSE;
5361 can_si_back = FALSE;
5362#endif
5363
5364 /*
5365 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5366 * This speeds up normal text input considerably.
5367 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5368 * need to re-indent at a ':', or any other character (but not what
5369 * 'paste' is set)..
5370 */
5371#ifdef USE_ON_FLY_SCROLL
5372 dont_scroll = FALSE; /* allow scrolling here */
5373#endif
5374
5375 if ( !ISSPECIAL(c)
5376#ifdef FEAT_MBYTE
5377 && (!has_mbyte || (*mb_char2len)(c) == 1)
5378#endif
5379 && vpeekc() != NUL
5380 && !(State & REPLACE_FLAG)
5381#ifdef FEAT_CINDENT
5382 && !cindent_on()
5383#endif
5384#ifdef FEAT_RIGHTLEFT
5385 && !p_ri
5386#endif
5387 )
5388 {
5389#define INPUT_BUFLEN 100
5390 char_u buf[INPUT_BUFLEN + 1];
5391 int i;
5392 colnr_T virtcol = 0;
5393
5394 buf[0] = c;
5395 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005396 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 virtcol = get_nolist_virtcol();
5398 /*
5399 * Stop the string when:
5400 * - no more chars available
5401 * - finding a special character (command key)
5402 * - buffer is full
5403 * - running into the 'textwidth' boundary
5404 * - need to check for abbreviation: A non-word char after a word-char
5405 */
5406 while ( (c = vpeekc()) != NUL
5407 && !ISSPECIAL(c)
5408#ifdef FEAT_MBYTE
5409 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5410#endif
5411 && i < INPUT_BUFLEN
5412 && (textwidth == 0
5413 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5414 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5415 {
5416#ifdef FEAT_RIGHTLEFT
5417 c = vgetc();
5418 if (p_hkmap && KeyTyped)
5419 c = hkmap(c); /* Hebrew mode mapping */
5420# ifdef FEAT_FKMAP
5421 if (p_fkmap && KeyTyped)
5422 c = fkmap(c); /* Farsi mode mapping */
5423# endif
5424 buf[i++] = c;
5425#else
5426 buf[i++] = vgetc();
5427#endif
5428 }
5429
5430#ifdef FEAT_DIGRAPHS
5431 do_digraph(-1); /* clear digraphs */
5432 do_digraph(buf[i-1]); /* may be the start of a digraph */
5433#endif
5434 buf[i] = NUL;
5435 ins_str(buf);
5436 if (flags & INSCHAR_CTRLV)
5437 {
5438 redo_literal(*buf);
5439 i = 1;
5440 }
5441 else
5442 i = 0;
5443 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005444 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446 else
5447 {
5448#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005449 int cc;
5450
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5452 {
5453 char_u buf[MB_MAXBYTES + 1];
5454
5455 (*mb_char2bytes)(c, buf);
5456 buf[cc] = NUL;
5457 ins_char_bytes(buf, cc);
5458 AppendCharToRedobuff(c);
5459 }
5460 else
5461#endif
5462 {
5463 ins_char(c);
5464 if (flags & INSCHAR_CTRLV)
5465 redo_literal(c);
5466 else
5467 AppendCharToRedobuff(c);
5468 }
5469 }
5470}
5471
5472/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005473 * Format text at the current insert position.
5474 */
5475 static void
5476internal_format(textwidth, second_indent, flags, format_only)
5477 int textwidth;
5478 int second_indent;
5479 int flags;
5480 int format_only;
5481{
5482 int cc;
5483 int save_char = NUL;
5484 int haveto_redraw = FALSE;
5485 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5486#ifdef FEAT_MBYTE
5487 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5488#endif
5489 int fo_white_par = has_format_option(FO_WHITE_PAR);
5490 int first_line = TRUE;
5491#ifdef FEAT_COMMENTS
5492 colnr_T leader_len;
5493 int no_leader = FALSE;
5494 int do_comments = (flags & INSCHAR_DO_COM);
5495#endif
5496
5497 /*
5498 * When 'ai' is off we don't want a space under the cursor to be
5499 * deleted. Replace it with an 'x' temporarily.
5500 */
5501 if (!curbuf->b_p_ai)
5502 {
5503 cc = gchar_cursor();
5504 if (vim_iswhite(cc))
5505 {
5506 save_char = cc;
5507 pchar_cursor('x');
5508 }
5509 }
5510
5511 /*
5512 * Repeat breaking lines, until the current line is not too long.
5513 */
5514 while (!got_int)
5515 {
5516 int startcol; /* Cursor column at entry */
5517 int wantcol; /* column at textwidth border */
5518 int foundcol; /* column for start of spaces */
5519 int end_foundcol = 0; /* column for start of word */
5520 colnr_T len;
5521 colnr_T virtcol;
5522#ifdef FEAT_VREPLACE
5523 int orig_col = 0;
5524 char_u *saved_text = NULL;
5525#endif
5526 colnr_T col;
5527
5528 virtcol = get_nolist_virtcol();
5529 if (virtcol < (colnr_T)textwidth)
5530 break;
5531
5532#ifdef FEAT_COMMENTS
5533 if (no_leader)
5534 do_comments = FALSE;
5535 else if (!(flags & INSCHAR_FORMAT)
5536 && has_format_option(FO_WRAP_COMS))
5537 do_comments = TRUE;
5538
5539 /* Don't break until after the comment leader */
5540 if (do_comments)
5541 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5542 else
5543 leader_len = 0;
5544
5545 /* If the line doesn't start with a comment leader, then don't
5546 * start one in a following broken line. Avoids that a %word
5547 * moved to the start of the next line causes all following lines
5548 * to start with %. */
5549 if (leader_len == 0)
5550 no_leader = TRUE;
5551#endif
5552 if (!(flags & INSCHAR_FORMAT)
5553#ifdef FEAT_COMMENTS
5554 && leader_len == 0
5555#endif
5556 && !has_format_option(FO_WRAP))
5557
5558 {
5559 textwidth = 0;
5560 break;
5561 }
5562 if ((startcol = curwin->w_cursor.col) == 0)
5563 break;
5564
5565 /* find column of textwidth border */
5566 coladvance((colnr_T)textwidth);
5567 wantcol = curwin->w_cursor.col;
5568
5569 curwin->w_cursor.col = startcol - 1;
5570#ifdef FEAT_MBYTE
5571 /* Correct cursor for multi-byte character. */
5572 if (has_mbyte)
5573 mb_adjust_cursor();
5574#endif
5575 foundcol = 0;
5576
5577 /*
5578 * Find position to break at.
5579 * Stop at first entered white when 'formatoptions' has 'v'
5580 */
5581 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5582 || curwin->w_cursor.lnum != Insstart.lnum
5583 || curwin->w_cursor.col >= Insstart.col)
5584 {
5585 cc = gchar_cursor();
5586 if (WHITECHAR(cc))
5587 {
5588 /* remember position of blank just before text */
5589 end_foundcol = curwin->w_cursor.col;
5590
5591 /* find start of sequence of blanks */
5592 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5593 {
5594 dec_cursor();
5595 cc = gchar_cursor();
5596 }
5597 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5598 break; /* only spaces in front of text */
5599#ifdef FEAT_COMMENTS
5600 /* Don't break until after the comment leader */
5601 if (curwin->w_cursor.col < leader_len)
5602 break;
5603#endif
5604 if (has_format_option(FO_ONE_LETTER))
5605 {
5606 /* do not break after one-letter words */
5607 if (curwin->w_cursor.col == 0)
5608 break; /* one-letter word at begin */
5609
5610 col = curwin->w_cursor.col;
5611 dec_cursor();
5612 cc = gchar_cursor();
5613
5614 if (WHITECHAR(cc))
5615 continue; /* one-letter, continue */
5616 curwin->w_cursor.col = col;
5617 }
5618#ifdef FEAT_MBYTE
5619 if (has_mbyte)
5620 foundcol = curwin->w_cursor.col
5621 + (*mb_ptr2len)(ml_get_cursor());
5622 else
5623#endif
5624 foundcol = curwin->w_cursor.col + 1;
5625 if (curwin->w_cursor.col < (colnr_T)wantcol)
5626 break;
5627 }
5628#ifdef FEAT_MBYTE
5629 else if (cc >= 0x100 && fo_multibyte
5630 && curwin->w_cursor.col <= (colnr_T)wantcol)
5631 {
5632 /* Break after or before a multi-byte character. */
5633 foundcol = curwin->w_cursor.col;
5634 if (curwin->w_cursor.col < (colnr_T)wantcol)
5635 foundcol += (*mb_char2len)(cc);
5636 end_foundcol = foundcol;
5637 break;
5638 }
5639#endif
5640 if (curwin->w_cursor.col == 0)
5641 break;
5642 dec_cursor();
5643 }
5644
5645 if (foundcol == 0) /* no spaces, cannot break line */
5646 {
5647 curwin->w_cursor.col = startcol;
5648 break;
5649 }
5650
5651 /* Going to break the line, remove any "$" now. */
5652 undisplay_dollar();
5653
5654 /*
5655 * Offset between cursor position and line break is used by replace
5656 * stack functions. VREPLACE does not use this, and backspaces
5657 * over the text instead.
5658 */
5659#ifdef FEAT_VREPLACE
5660 if (State & VREPLACE_FLAG)
5661 orig_col = startcol; /* Will start backspacing from here */
5662 else
5663#endif
5664 replace_offset = startcol - end_foundcol - 1;
5665
5666 /*
5667 * adjust startcol for spaces that will be deleted and
5668 * characters that will remain on top line
5669 */
5670 curwin->w_cursor.col = foundcol;
5671 while (cc = gchar_cursor(), WHITECHAR(cc))
5672 inc_cursor();
5673 startcol -= curwin->w_cursor.col;
5674 if (startcol < 0)
5675 startcol = 0;
5676
5677#ifdef FEAT_VREPLACE
5678 if (State & VREPLACE_FLAG)
5679 {
5680 /*
5681 * In VREPLACE mode, we will backspace over the text to be
5682 * wrapped, so save a copy now to put on the next line.
5683 */
5684 saved_text = vim_strsave(ml_get_cursor());
5685 curwin->w_cursor.col = orig_col;
5686 if (saved_text == NULL)
5687 break; /* Can't do it, out of memory */
5688 saved_text[startcol] = NUL;
5689
5690 /* Backspace over characters that will move to the next line */
5691 if (!fo_white_par)
5692 backspace_until_column(foundcol);
5693 }
5694 else
5695#endif
5696 {
5697 /* put cursor after pos. to break line */
5698 if (!fo_white_par)
5699 curwin->w_cursor.col = foundcol;
5700 }
5701
5702 /*
5703 * Split the line just before the margin.
5704 * Only insert/delete lines, but don't really redraw the window.
5705 */
5706 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5707 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5708#ifdef FEAT_COMMENTS
5709 + (do_comments ? OPENLINE_DO_COM : 0)
5710#endif
5711 , old_indent);
5712 old_indent = 0;
5713
5714 replace_offset = 0;
5715 if (first_line)
5716 {
5717 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5718 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5719 if (second_indent >= 0)
5720 {
5721#ifdef FEAT_VREPLACE
5722 if (State & VREPLACE_FLAG)
5723 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5724 else
5725#endif
5726 (void)set_indent(second_indent, SIN_CHANGED);
5727 }
5728 first_line = FALSE;
5729 }
5730
5731#ifdef FEAT_VREPLACE
5732 if (State & VREPLACE_FLAG)
5733 {
5734 /*
5735 * In VREPLACE mode we have backspaced over the text to be
5736 * moved, now we re-insert it into the new line.
5737 */
5738 ins_bytes(saved_text);
5739 vim_free(saved_text);
5740 }
5741 else
5742#endif
5743 {
5744 /*
5745 * Check if cursor is not past the NUL off the line, cindent
5746 * may have added or removed indent.
5747 */
5748 curwin->w_cursor.col += startcol;
5749 len = (colnr_T)STRLEN(ml_get_curline());
5750 if (curwin->w_cursor.col > len)
5751 curwin->w_cursor.col = len;
5752 }
5753
5754 haveto_redraw = TRUE;
5755#ifdef FEAT_CINDENT
5756 can_cindent = TRUE;
5757#endif
5758 /* moved the cursor, don't autoindent or cindent now */
5759 did_ai = FALSE;
5760#ifdef FEAT_SMARTINDENT
5761 did_si = FALSE;
5762 can_si = FALSE;
5763 can_si_back = FALSE;
5764#endif
5765 line_breakcheck();
5766 }
5767
5768 if (save_char != NUL) /* put back space after cursor */
5769 pchar_cursor(save_char);
5770
5771 if (!format_only && haveto_redraw)
5772 {
5773 update_topline();
5774 redraw_curbuf_later(VALID);
5775 }
5776}
5777
5778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 * Called after inserting or deleting text: When 'formatoptions' includes the
5780 * 'a' flag format from the current line until the end of the paragraph.
5781 * Keep the cursor at the same position relative to the text.
5782 * The caller must have saved the cursor line for undo, following ones will be
5783 * saved here.
5784 */
5785 void
5786auto_format(trailblank, prev_line)
5787 int trailblank; /* when TRUE also format with trailing blank */
5788 int prev_line; /* may start in previous line */
5789{
5790 pos_T pos;
5791 colnr_T len;
5792 char_u *old;
5793 char_u *new, *pnew;
5794 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005795 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 if (!has_format_option(FO_AUTO))
5798 return;
5799
5800 pos = curwin->w_cursor;
5801 old = ml_get_curline();
5802
5803 /* may remove added space */
5804 check_auto_format(FALSE);
5805
5806 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5807 * user might insert normal text next. Also skip formatting when "1" is
5808 * in 'formatoptions' and there is a single character before the cursor.
5809 * Otherwise the line would be broken and when typing another non-white
5810 * next they are not joined back together. */
5811 wasatend = (pos.col == STRLEN(old));
5812 if (*old != NUL && !trailblank && wasatend)
5813 {
5814 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005815 cc = gchar_cursor();
5816 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5817 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005819 cc = gchar_cursor();
5820 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
5822 curwin->w_cursor = pos;
5823 return;
5824 }
5825 curwin->w_cursor = pos;
5826 }
5827
5828#ifdef FEAT_COMMENTS
5829 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5830 * comments. */
5831 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5832 && get_leader_len(old, NULL, FALSE) == 0)
5833 return;
5834#endif
5835
5836 /*
5837 * May start formatting in a previous line, so that after "x" a word is
5838 * moved to the previous line if it fits there now. Only when this is not
5839 * the start of a paragraph.
5840 */
5841 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5842 {
5843 --curwin->w_cursor.lnum;
5844 if (u_save_cursor() == FAIL)
5845 return;
5846 }
5847
5848 /*
5849 * Do the formatting and restore the cursor position. "saved_cursor" will
5850 * be adjusted for the text formatting.
5851 */
5852 saved_cursor = pos;
5853 format_lines((linenr_T)-1);
5854 curwin->w_cursor = saved_cursor;
5855 saved_cursor.lnum = 0;
5856
5857 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5858 {
5859 /* "cannot happen" */
5860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5861 coladvance((colnr_T)MAXCOL);
5862 }
5863 else
5864 check_cursor_col();
5865
5866 /* Insert mode: If the cursor is now after the end of the line while it
5867 * previously wasn't, the line was broken. Because of the rule above we
5868 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5869 * formatted. */
5870 if (!wasatend && has_format_option(FO_WHITE_PAR))
5871 {
5872 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005873 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (curwin->w_cursor.col == len)
5875 {
5876 pnew = vim_strnsave(new, len + 2);
5877 pnew[len] = ' ';
5878 pnew[len + 1] = NUL;
5879 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5880 /* remove the space later */
5881 did_add_space = TRUE;
5882 }
5883 else
5884 /* may remove added space */
5885 check_auto_format(FALSE);
5886 }
5887
5888 check_cursor();
5889}
5890
5891/*
5892 * When an extra space was added to continue a paragraph for auto-formatting,
5893 * delete it now. The space must be under the cursor, just after the insert
5894 * position.
5895 */
5896 static void
5897check_auto_format(end_insert)
5898 int end_insert; /* TRUE when ending Insert mode */
5899{
5900 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005901 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 if (did_add_space)
5904 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005905 cc = gchar_cursor();
5906 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 /* Somehow the space was removed already. */
5908 did_add_space = FALSE;
5909 else
5910 {
5911 if (!end_insert)
5912 {
5913 inc_cursor();
5914 c = gchar_cursor();
5915 dec_cursor();
5916 }
5917 if (c != NUL)
5918 {
5919 /* The space is no longer at the end of the line, delete it. */
5920 del_char(FALSE);
5921 did_add_space = FALSE;
5922 }
5923 }
5924 }
5925}
5926
5927/*
5928 * Find out textwidth to be used for formatting:
5929 * if 'textwidth' option is set, use it
5930 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5931 * if invalid value, use 0.
5932 * Set default to window width (maximum 79) for "gq" operator.
5933 */
5934 int
5935comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005936 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 int textwidth;
5939
5940 textwidth = curbuf->b_p_tw;
5941 if (textwidth == 0 && curbuf->b_p_wm)
5942 {
5943 /* The width is the window width minus 'wrapmargin' minus all the
5944 * things that add to the margin. */
5945 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5946#ifdef FEAT_CMDWIN
5947 if (cmdwin_type != 0)
5948 textwidth -= 1;
5949#endif
5950#ifdef FEAT_FOLDING
5951 textwidth -= curwin->w_p_fdc;
5952#endif
5953#ifdef FEAT_SIGNS
5954 if (curwin->w_buffer->b_signlist != NULL
5955# ifdef FEAT_NETBEANS_INTG
5956 || usingNetbeans
5957# endif
5958 )
5959 textwidth -= 1;
5960#endif
5961 if (curwin->w_p_nu)
5962 textwidth -= 8;
5963 }
5964 if (textwidth < 0)
5965 textwidth = 0;
5966 if (ff && textwidth == 0)
5967 {
5968 textwidth = W_WIDTH(curwin) - 1;
5969 if (textwidth > 79)
5970 textwidth = 79;
5971 }
5972 return textwidth;
5973}
5974
5975/*
5976 * Put a character in the redo buffer, for when just after a CTRL-V.
5977 */
5978 static void
5979redo_literal(c)
5980 int c;
5981{
5982 char_u buf[10];
5983
5984 /* Only digits need special treatment. Translate them into a string of
5985 * three digits. */
5986 if (VIM_ISDIGIT(c))
5987 {
5988 sprintf((char *)buf, "%03d", c);
5989 AppendToRedobuff(buf);
5990 }
5991 else
5992 AppendCharToRedobuff(c);
5993}
5994
5995/*
5996 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005997 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 */
5999 static void
6000start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006001 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 if (!arrow_used) /* something has been inserted */
6004 {
6005 AppendToRedobuff(ESC_STR);
6006 stop_insert(end_insert_pos, FALSE);
6007 arrow_used = TRUE; /* this means we stopped the current insert */
6008 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006009#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006010 check_spell_redraw();
6011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012}
6013
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006014#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006015/*
6016 * If we skipped highlighting word at cursor, do it now.
6017 * It may be skipped again, thus reset spell_redraw_lnum first.
6018 */
6019 static void
6020check_spell_redraw()
6021{
6022 if (spell_redraw_lnum != 0)
6023 {
6024 linenr_T lnum = spell_redraw_lnum;
6025
6026 spell_redraw_lnum = 0;
6027 redrawWinline(lnum, FALSE);
6028 }
6029}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006030
6031/*
6032 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6033 * spelled word, if there is one.
6034 */
6035 static void
6036spell_back_to_badword()
6037{
6038 pos_T tpos = curwin->w_cursor;
6039
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006040 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006041 if (curwin->w_cursor.col != tpos.col)
6042 start_arrow(&tpos);
6043}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006044#endif
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046/*
6047 * stop_arrow() is called before a change is made in insert mode.
6048 * If an arrow key has been used, start a new insertion.
6049 * Returns FAIL if undo is impossible, shouldn't insert then.
6050 */
6051 int
6052stop_arrow()
6053{
6054 if (arrow_used)
6055 {
6056 if (u_save_cursor() == OK)
6057 {
6058 arrow_used = FALSE;
6059 ins_need_undo = FALSE;
6060 }
6061 Insstart = curwin->w_cursor; /* new insertion starts here */
6062 Insstart_textlen = linetabsize(ml_get_curline());
6063 ai_col = 0;
6064#ifdef FEAT_VREPLACE
6065 if (State & VREPLACE_FLAG)
6066 {
6067 orig_line_count = curbuf->b_ml.ml_line_count;
6068 vr_lines_changed = 1;
6069 }
6070#endif
6071 ResetRedobuff();
6072 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006073 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 }
6075 else if (ins_need_undo)
6076 {
6077 if (u_save_cursor() == OK)
6078 ins_need_undo = FALSE;
6079 }
6080
6081#ifdef FEAT_FOLDING
6082 /* Always open fold at the cursor line when inserting something. */
6083 foldOpenCursor();
6084#endif
6085
6086 return (arrow_used || ins_need_undo ? FAIL : OK);
6087}
6088
6089/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006090 * Do a few things to stop inserting.
6091 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6092 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 */
6094 static void
6095stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006096 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006097 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006099 int cc;
6100 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 stop_redo_ins();
6103 replace_flush(); /* abandon replace stack */
6104
6105 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006106 * Save the inserted text for later redo with ^@ and CTRL-A.
6107 * Don't do it when "restart_edit" was set and nothing was inserted,
6108 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006110 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006111 if (did_restart_edit == 0 || (ptr != NULL
6112 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006113 {
6114 vim_free(last_insert);
6115 last_insert = ptr;
6116 last_insert_skip = new_insert_skip;
6117 }
6118 else
6119 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006121 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
6123 /* Auto-format now. It may seem strange to do this when stopping an
6124 * insertion (or moving the cursor), but it's required when appending
6125 * a line and having it end in a space. But only do it when something
6126 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006127 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006129 pos_T tpos = curwin->w_cursor;
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 /* When the cursor is at the end of the line after a space the
6132 * formatting will move it to the following word. Avoid that by
6133 * moving the cursor onto the space. */
6134 cc = 'x';
6135 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6136 {
6137 dec_cursor();
6138 cc = gchar_cursor();
6139 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006140 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 }
6142
6143 auto_format(TRUE, FALSE);
6144
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006145 if (vim_iswhite(cc))
6146 {
6147 if (gchar_cursor() != NUL)
6148 inc_cursor();
6149#ifdef FEAT_VIRTUALEDIT
6150 /* If the cursor is still at the same character, also keep
6151 * the "coladd". */
6152 if (gchar_cursor() == NUL
6153 && curwin->w_cursor.lnum == tpos.lnum
6154 && curwin->w_cursor.col == tpos.col)
6155 curwin->w_cursor.coladd = tpos.coladd;
6156#endif
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159
6160 /* If a space was inserted for auto-formatting, remove it now. */
6161 check_auto_format(TRUE);
6162
6163 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006164 * of the line, and put the cursor back.
6165 * Do this when ESC was used or moving the cursor up/down. */
6166 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006167 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006169 pos_T tpos = curwin->w_cursor;
6170
6171 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006172 for (;;)
6173 {
6174 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6175 --curwin->w_cursor.col;
6176 cc = gchar_cursor();
6177 if (!vim_iswhite(cc))
6178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006180 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006181 if (curwin->w_cursor.lnum != tpos.lnum)
6182 curwin->w_cursor = tpos;
6183 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6185
6186#ifdef FEAT_VISUAL
6187 /* <C-S-Right> may have started Visual mode, adjust the position for
6188 * deleted characters. */
6189 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6190 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006191 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (VIsual.col > (colnr_T)cc)
6193 {
6194 VIsual.col = cc;
6195# ifdef FEAT_VIRTUALEDIT
6196 VIsual.coladd = 0;
6197# endif
6198 }
6199 }
6200#endif
6201 }
6202 }
6203 did_ai = FALSE;
6204#ifdef FEAT_SMARTINDENT
6205 did_si = FALSE;
6206 can_si = FALSE;
6207 can_si_back = FALSE;
6208#endif
6209
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006210 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6211 * now in a different buffer. */
6212 if (end_insert_pos != NULL)
6213 {
6214 curbuf->b_op_start = Insstart;
6215 curbuf->b_op_end = *end_insert_pos;
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217}
6218
6219/*
6220 * Set the last inserted text to a single character.
6221 * Used for the replace command.
6222 */
6223 void
6224set_last_insert(c)
6225 int c;
6226{
6227 char_u *s;
6228
6229 vim_free(last_insert);
6230#ifdef FEAT_MBYTE
6231 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6232#else
6233 last_insert = alloc(6);
6234#endif
6235 if (last_insert != NULL)
6236 {
6237 s = last_insert;
6238 /* Use the CTRL-V only when entering a special char */
6239 if (c < ' ' || c == DEL)
6240 *s++ = Ctrl_V;
6241 s = add_char2buf(c, s);
6242 *s++ = ESC;
6243 *s++ = NUL;
6244 last_insert_skip = 0;
6245 }
6246}
6247
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006248#if defined(EXITFREE) || defined(PROTO)
6249 void
6250free_last_insert()
6251{
6252 vim_free(last_insert);
6253 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006254 vim_free(compl_orig_text);
6255 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006256}
6257#endif
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259/*
6260 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6261 * and CSI. Handle multi-byte characters.
6262 * Returns a pointer to after the added bytes.
6263 */
6264 char_u *
6265add_char2buf(c, s)
6266 int c;
6267 char_u *s;
6268{
6269#ifdef FEAT_MBYTE
6270 char_u temp[MB_MAXBYTES];
6271 int i;
6272 int len;
6273
6274 len = (*mb_char2bytes)(c, temp);
6275 for (i = 0; i < len; ++i)
6276 {
6277 c = temp[i];
6278#endif
6279 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6280 if (c == K_SPECIAL)
6281 {
6282 *s++ = K_SPECIAL;
6283 *s++ = KS_SPECIAL;
6284 *s++ = KE_FILLER;
6285 }
6286#ifdef FEAT_GUI
6287 else if (c == CSI)
6288 {
6289 *s++ = CSI;
6290 *s++ = KS_EXTRA;
6291 *s++ = (int)KE_CSI;
6292 }
6293#endif
6294 else
6295 *s++ = c;
6296#ifdef FEAT_MBYTE
6297 }
6298#endif
6299 return s;
6300}
6301
6302/*
6303 * move cursor to start of line
6304 * if flags & BL_WHITE move to first non-white
6305 * if flags & BL_SOL move to first non-white if startofline is set,
6306 * otherwise keep "curswant" column
6307 * if flags & BL_FIX don't leave the cursor on a NUL.
6308 */
6309 void
6310beginline(flags)
6311 int flags;
6312{
6313 if ((flags & BL_SOL) && !p_sol)
6314 coladvance(curwin->w_curswant);
6315 else
6316 {
6317 curwin->w_cursor.col = 0;
6318#ifdef FEAT_VIRTUALEDIT
6319 curwin->w_cursor.coladd = 0;
6320#endif
6321
6322 if (flags & (BL_WHITE | BL_SOL))
6323 {
6324 char_u *ptr;
6325
6326 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6327 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6328 ++curwin->w_cursor.col;
6329 }
6330 curwin->w_set_curswant = TRUE;
6331 }
6332}
6333
6334/*
6335 * oneright oneleft cursor_down cursor_up
6336 *
6337 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006338 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 * Return OK when successful, FAIL when we hit a line of file boundary.
6340 */
6341
6342 int
6343oneright()
6344{
6345 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348#ifdef FEAT_VIRTUALEDIT
6349 if (virtual_active())
6350 {
6351 pos_T prevpos = curwin->w_cursor;
6352
6353 /* Adjust for multi-wide char (excluding TAB) */
6354 ptr = ml_get_cursor();
6355 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006356# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006358# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 ))
6362 ? ptr2cells(ptr) : 1));
6363 curwin->w_set_curswant = TRUE;
6364 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6365 return (prevpos.col != curwin->w_cursor.col
6366 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6367 }
6368#endif
6369
6370 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006371 if (*ptr == NUL)
6372 return FAIL; /* already at the very end */
6373
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006375 if (has_mbyte)
6376 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 else
6378#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006379 l = 1;
6380
6381 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6382 * contains "onemore". */
6383 if (ptr[l] == NUL
6384#ifdef FEAT_VIRTUALEDIT
6385 && (ve_flags & VE_ONEMORE) == 0
6386#endif
6387 )
6388 return FAIL;
6389 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
6391 curwin->w_set_curswant = TRUE;
6392 return OK;
6393}
6394
6395 int
6396oneleft()
6397{
6398#ifdef FEAT_VIRTUALEDIT
6399 if (virtual_active())
6400 {
6401 int width;
6402 int v = getviscol();
6403
6404 if (v == 0)
6405 return FAIL;
6406
6407# ifdef FEAT_LINEBREAK
6408 /* We might get stuck on 'showbreak', skip over it. */
6409 width = 1;
6410 for (;;)
6411 {
6412 coladvance(v - width);
6413 /* getviscol() is slow, skip it when 'showbreak' is empty and
6414 * there are no multi-byte characters */
6415 if ((*p_sbr == NUL
6416# ifdef FEAT_MBYTE
6417 && !has_mbyte
6418# endif
6419 ) || getviscol() < v)
6420 break;
6421 ++width;
6422 }
6423# else
6424 coladvance(v - 1);
6425# endif
6426
6427 if (curwin->w_cursor.coladd == 1)
6428 {
6429 char_u *ptr;
6430
6431 /* Adjust for multi-wide char (not a TAB) */
6432 ptr = ml_get_cursor();
6433 if (*ptr != TAB && vim_isprintc(
6434# ifdef FEAT_MBYTE
6435 (*mb_ptr2char)(ptr)
6436# else
6437 *ptr
6438# endif
6439 ) && ptr2cells(ptr) > 1)
6440 curwin->w_cursor.coladd = 0;
6441 }
6442
6443 curwin->w_set_curswant = TRUE;
6444 return OK;
6445 }
6446#endif
6447
6448 if (curwin->w_cursor.col == 0)
6449 return FAIL;
6450
6451 curwin->w_set_curswant = TRUE;
6452 --curwin->w_cursor.col;
6453
6454#ifdef FEAT_MBYTE
6455 /* if the character on the left of the current cursor is a multi-byte
6456 * character, move to its first byte */
6457 if (has_mbyte)
6458 mb_adjust_cursor();
6459#endif
6460 return OK;
6461}
6462
6463 int
6464cursor_up(n, upd_topline)
6465 long n;
6466 int upd_topline; /* When TRUE: update topline */
6467{
6468 linenr_T lnum;
6469
6470 if (n > 0)
6471 {
6472 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006473 /* This fails if the cursor is already in the first line or the count
6474 * is larger than the line number and '-' is in 'cpoptions' */
6475 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 return FAIL;
6477 if (n >= lnum)
6478 lnum = 1;
6479 else
6480#ifdef FEAT_FOLDING
6481 if (hasAnyFolding(curwin))
6482 {
6483 /*
6484 * Count each sequence of folded lines as one logical line.
6485 */
6486 /* go to the the start of the current fold */
6487 (void)hasFolding(lnum, &lnum, NULL);
6488
6489 while (n--)
6490 {
6491 /* move up one line */
6492 --lnum;
6493 if (lnum <= 1)
6494 break;
6495 /* If we entered a fold, move to the beginning, unless in
6496 * Insert mode or when 'foldopen' contains "all": it will open
6497 * in a moment. */
6498 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6499 (void)hasFolding(lnum, &lnum, NULL);
6500 }
6501 if (lnum < 1)
6502 lnum = 1;
6503 }
6504 else
6505#endif
6506 lnum -= n;
6507 curwin->w_cursor.lnum = lnum;
6508 }
6509
6510 /* try to advance to the column we want to be at */
6511 coladvance(curwin->w_curswant);
6512
6513 if (upd_topline)
6514 update_topline(); /* make sure curwin->w_topline is valid */
6515
6516 return OK;
6517}
6518
6519/*
6520 * Cursor down a number of logical lines.
6521 */
6522 int
6523cursor_down(n, upd_topline)
6524 long n;
6525 int upd_topline; /* When TRUE: update topline */
6526{
6527 linenr_T lnum;
6528
6529 if (n > 0)
6530 {
6531 lnum = curwin->w_cursor.lnum;
6532#ifdef FEAT_FOLDING
6533 /* Move to last line of fold, will fail if it's the end-of-file. */
6534 (void)hasFolding(lnum, NULL, &lnum);
6535#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006536 /* This fails if the cursor is already in the last line or would move
6537 * beyound the last line and '-' is in 'cpoptions' */
6538 if (lnum >= curbuf->b_ml.ml_line_count
6539 || (lnum + n > curbuf->b_ml.ml_line_count
6540 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 return FAIL;
6542 if (lnum + n >= curbuf->b_ml.ml_line_count)
6543 lnum = curbuf->b_ml.ml_line_count;
6544 else
6545#ifdef FEAT_FOLDING
6546 if (hasAnyFolding(curwin))
6547 {
6548 linenr_T last;
6549
6550 /* count each sequence of folded lines as one logical line */
6551 while (n--)
6552 {
6553 if (hasFolding(lnum, NULL, &last))
6554 lnum = last + 1;
6555 else
6556 ++lnum;
6557 if (lnum >= curbuf->b_ml.ml_line_count)
6558 break;
6559 }
6560 if (lnum > curbuf->b_ml.ml_line_count)
6561 lnum = curbuf->b_ml.ml_line_count;
6562 }
6563 else
6564#endif
6565 lnum += n;
6566 curwin->w_cursor.lnum = lnum;
6567 }
6568
6569 /* try to advance to the column we want to be at */
6570 coladvance(curwin->w_curswant);
6571
6572 if (upd_topline)
6573 update_topline(); /* make sure curwin->w_topline is valid */
6574
6575 return OK;
6576}
6577
6578/*
6579 * Stuff the last inserted text in the read buffer.
6580 * Last_insert actually is a copy of the redo buffer, so we
6581 * first have to remove the command.
6582 */
6583 int
6584stuff_inserted(c, count, no_esc)
6585 int c; /* Command character to be inserted */
6586 long count; /* Repeat this many times */
6587 int no_esc; /* Don't add an ESC at the end */
6588{
6589 char_u *esc_ptr;
6590 char_u *ptr;
6591 char_u *last_ptr;
6592 char_u last = NUL;
6593
6594 ptr = get_last_insert();
6595 if (ptr == NULL)
6596 {
6597 EMSG(_(e_noinstext));
6598 return FAIL;
6599 }
6600
6601 /* may want to stuff the command character, to start Insert mode */
6602 if (c != NUL)
6603 stuffcharReadbuff(c);
6604 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6605 *esc_ptr = NUL; /* remove the ESC */
6606
6607 /* when the last char is either "0" or "^" it will be quoted if no ESC
6608 * comes after it OR if it will inserted more than once and "ptr"
6609 * starts with ^D. -- Acevedo
6610 */
6611 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6612 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6613 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6614 {
6615 last = *last_ptr;
6616 *last_ptr = NUL;
6617 }
6618
6619 do
6620 {
6621 stuffReadbuff(ptr);
6622 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6623 if (last)
6624 stuffReadbuff((char_u *)(last == '0'
6625 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6626 : IF_EB("\026^", CTRL_V_STR "^")));
6627 }
6628 while (--count > 0);
6629
6630 if (last)
6631 *last_ptr = last;
6632
6633 if (esc_ptr != NULL)
6634 *esc_ptr = ESC; /* put the ESC back */
6635
6636 /* may want to stuff a trailing ESC, to get out of Insert mode */
6637 if (!no_esc)
6638 stuffcharReadbuff(ESC);
6639
6640 return OK;
6641}
6642
6643 char_u *
6644get_last_insert()
6645{
6646 if (last_insert == NULL)
6647 return NULL;
6648 return last_insert + last_insert_skip;
6649}
6650
6651/*
6652 * Get last inserted string, and remove trailing <Esc>.
6653 * Returns pointer to allocated memory (must be freed) or NULL.
6654 */
6655 char_u *
6656get_last_insert_save()
6657{
6658 char_u *s;
6659 int len;
6660
6661 if (last_insert == NULL)
6662 return NULL;
6663 s = vim_strsave(last_insert + last_insert_skip);
6664 if (s != NULL)
6665 {
6666 len = (int)STRLEN(s);
6667 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6668 s[len - 1] = NUL;
6669 }
6670 return s;
6671}
6672
6673/*
6674 * Check the word in front of the cursor for an abbreviation.
6675 * Called when the non-id character "c" has been entered.
6676 * When an abbreviation is recognized it is removed from the text and
6677 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6678 */
6679 static int
6680echeck_abbr(c)
6681 int c;
6682{
6683 /* Don't check for abbreviation in paste mode, when disabled and just
6684 * after moving around with cursor keys. */
6685 if (p_paste || no_abbr || arrow_used)
6686 return FALSE;
6687
6688 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6689 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6690}
6691
6692/*
6693 * replace-stack functions
6694 *
6695 * When replacing characters, the replaced characters are remembered for each
6696 * new character. This is used to re-insert the old text when backspacing.
6697 *
6698 * There is a NUL headed list of characters for each character that is
6699 * currently in the file after the insertion point. When BS is used, one NUL
6700 * headed list is put back for the deleted character.
6701 *
6702 * For a newline, there are two NUL headed lists. One contains the characters
6703 * that the NL replaced. The extra one stores the characters after the cursor
6704 * that were deleted (always white space).
6705 *
6706 * Replace_offset is normally 0, in which case replace_push will add a new
6707 * character at the end of the stack. If replace_offset is not 0, that many
6708 * characters will be left on the stack above the newly inserted character.
6709 */
6710
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006711static char_u *replace_stack = NULL;
6712static long replace_stack_nr = 0; /* next entry in replace stack */
6713static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 void
6716replace_push(c)
6717 int c; /* character that is replaced (NUL is none) */
6718{
6719 char_u *p;
6720
6721 if (replace_stack_nr < replace_offset) /* nothing to do */
6722 return;
6723 if (replace_stack_len <= replace_stack_nr)
6724 {
6725 replace_stack_len += 50;
6726 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6727 if (p == NULL) /* out of memory */
6728 {
6729 replace_stack_len -= 50;
6730 return;
6731 }
6732 if (replace_stack != NULL)
6733 {
6734 mch_memmove(p, replace_stack,
6735 (size_t)(replace_stack_nr * sizeof(char_u)));
6736 vim_free(replace_stack);
6737 }
6738 replace_stack = p;
6739 }
6740 p = replace_stack + replace_stack_nr - replace_offset;
6741 if (replace_offset)
6742 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6743 *p = c;
6744 ++replace_stack_nr;
6745}
6746
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006747#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748/*
6749 * call replace_push(c) with replace_offset set to the first NUL.
6750 */
6751 static void
6752replace_push_off(c)
6753 int c;
6754{
6755 char_u *p;
6756
6757 p = replace_stack + replace_stack_nr;
6758 for (replace_offset = 1; replace_offset < replace_stack_nr;
6759 ++replace_offset)
6760 if (*--p == NUL)
6761 break;
6762 replace_push(c);
6763 replace_offset = 0;
6764}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767/*
6768 * Pop one item from the replace stack.
6769 * return -1 if stack empty
6770 * return replaced character or NUL otherwise
6771 */
6772 static int
6773replace_pop()
6774{
6775 if (replace_stack_nr == 0)
6776 return -1;
6777 return (int)replace_stack[--replace_stack_nr];
6778}
6779
6780/*
6781 * Join the top two items on the replace stack. This removes to "off"'th NUL
6782 * encountered.
6783 */
6784 static void
6785replace_join(off)
6786 int off; /* offset for which NUL to remove */
6787{
6788 int i;
6789
6790 for (i = replace_stack_nr; --i >= 0; )
6791 if (replace_stack[i] == NUL && off-- <= 0)
6792 {
6793 --replace_stack_nr;
6794 mch_memmove(replace_stack + i, replace_stack + i + 1,
6795 (size_t)(replace_stack_nr - i));
6796 return;
6797 }
6798}
6799
6800/*
6801 * Pop bytes from the replace stack until a NUL is found, and insert them
6802 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6803 */
6804 static void
6805replace_pop_ins()
6806{
6807 int cc;
6808 int oldState = State;
6809
6810 State = NORMAL; /* don't want REPLACE here */
6811 while ((cc = replace_pop()) > 0)
6812 {
6813#ifdef FEAT_MBYTE
6814 mb_replace_pop_ins(cc);
6815#else
6816 ins_char(cc);
6817#endif
6818 dec_cursor();
6819 }
6820 State = oldState;
6821}
6822
6823#ifdef FEAT_MBYTE
6824/*
6825 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6826 * indicates a multi-byte char, pop the other bytes too.
6827 */
6828 static void
6829mb_replace_pop_ins(cc)
6830 int cc;
6831{
6832 int n;
6833 char_u buf[MB_MAXBYTES];
6834 int i;
6835 int c;
6836
6837 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6838 {
6839 buf[0] = cc;
6840 for (i = 1; i < n; ++i)
6841 buf[i] = replace_pop();
6842 ins_bytes_len(buf, n);
6843 }
6844 else
6845 ins_char(cc);
6846
6847 if (enc_utf8)
6848 /* Handle composing chars. */
6849 for (;;)
6850 {
6851 c = replace_pop();
6852 if (c == -1) /* stack empty */
6853 break;
6854 if ((n = MB_BYTE2LEN(c)) == 1)
6855 {
6856 /* Not a multi-byte char, put it back. */
6857 replace_push(c);
6858 break;
6859 }
6860 else
6861 {
6862 buf[0] = c;
6863 for (i = 1; i < n; ++i)
6864 buf[i] = replace_pop();
6865 if (utf_iscomposing(utf_ptr2char(buf)))
6866 ins_bytes_len(buf, n);
6867 else
6868 {
6869 /* Not a composing char, put it back. */
6870 for (i = n - 1; i >= 0; --i)
6871 replace_push(buf[i]);
6872 break;
6873 }
6874 }
6875 }
6876}
6877#endif
6878
6879/*
6880 * make the replace stack empty
6881 * (called when exiting replace mode)
6882 */
6883 static void
6884replace_flush()
6885{
6886 vim_free(replace_stack);
6887 replace_stack = NULL;
6888 replace_stack_len = 0;
6889 replace_stack_nr = 0;
6890}
6891
6892/*
6893 * Handle doing a BS for one character.
6894 * cc < 0: replace stack empty, just move cursor
6895 * cc == 0: character was inserted, delete it
6896 * cc > 0: character was replaced, put cc (first byte of original char) back
6897 * and check for more characters to be put back
6898 */
6899 static void
6900replace_do_bs()
6901{
6902 int cc;
6903#ifdef FEAT_VREPLACE
6904 int orig_len = 0;
6905 int ins_len;
6906 int orig_vcols = 0;
6907 colnr_T start_vcol;
6908 char_u *p;
6909 int i;
6910 int vcol;
6911#endif
6912
6913 cc = replace_pop();
6914 if (cc > 0)
6915 {
6916#ifdef FEAT_VREPLACE
6917 if (State & VREPLACE_FLAG)
6918 {
6919 /* Get the number of screen cells used by the character we are
6920 * going to delete. */
6921 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6922 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6923 }
6924#endif
6925#ifdef FEAT_MBYTE
6926 if (has_mbyte)
6927 {
6928 del_char(FALSE);
6929# ifdef FEAT_VREPLACE
6930 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006931 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932# endif
6933 replace_push(cc);
6934 }
6935 else
6936#endif
6937 {
6938 pchar_cursor(cc);
6939#ifdef FEAT_VREPLACE
6940 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006941 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942#endif
6943 }
6944 replace_pop_ins();
6945
6946#ifdef FEAT_VREPLACE
6947 if (State & VREPLACE_FLAG)
6948 {
6949 /* Get the number of screen cells used by the inserted characters */
6950 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006951 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 vcol = start_vcol;
6953 for (i = 0; i < ins_len; ++i)
6954 {
6955 vcol += chartabsize(p + i, vcol);
6956#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006957 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958#endif
6959 }
6960 vcol -= start_vcol;
6961
6962 /* Delete spaces that were inserted after the cursor to keep the
6963 * text aligned. */
6964 curwin->w_cursor.col += ins_len;
6965 while (vcol > orig_vcols && gchar_cursor() == ' ')
6966 {
6967 del_char(FALSE);
6968 ++orig_vcols;
6969 }
6970 curwin->w_cursor.col -= ins_len;
6971 }
6972#endif
6973
6974 /* mark the buffer as changed and prepare for displaying */
6975 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6976 }
6977 else if (cc == 0)
6978 (void)del_char(FALSE);
6979}
6980
6981#ifdef FEAT_CINDENT
6982/*
6983 * Return TRUE if C-indenting is on.
6984 */
6985 static int
6986cindent_on()
6987{
6988 return (!p_paste && (curbuf->b_p_cin
6989# ifdef FEAT_EVAL
6990 || *curbuf->b_p_inde != NUL
6991# endif
6992 ));
6993}
6994#endif
6995
6996#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6997/*
6998 * Re-indent the current line, based on the current contents of it and the
6999 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7000 * confused what all the part that handles Control-T is doing that I'm not.
7001 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7002 */
7003
7004 void
7005fixthisline(get_the_indent)
7006 int (*get_the_indent) __ARGS((void));
7007{
7008 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7009 if (linewhite(curwin->w_cursor.lnum))
7010 did_ai = TRUE; /* delete the indent if the line stays empty */
7011}
7012
7013 void
7014fix_indent()
7015{
7016 if (p_paste)
7017 return;
7018# ifdef FEAT_LISP
7019 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7020 fixthisline(get_lisp_indent);
7021# endif
7022# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7023 else
7024# endif
7025# ifdef FEAT_CINDENT
7026 if (cindent_on())
7027 do_c_expr_indent();
7028# endif
7029}
7030
7031#endif
7032
7033#ifdef FEAT_CINDENT
7034/*
7035 * return TRUE if 'cinkeys' contains the key "keytyped",
7036 * when == '*': Only if key is preceded with '*' (indent before insert)
7037 * when == '!': Only if key is prededed with '!' (don't insert)
7038 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7039 *
7040 * "keytyped" can have a few special values:
7041 * KEY_OPEN_FORW
7042 * KEY_OPEN_BACK
7043 * KEY_COMPLETE just finished completion.
7044 *
7045 * If line_is_empty is TRUE accept keys with '0' before them.
7046 */
7047 int
7048in_cinkeys(keytyped, when, line_is_empty)
7049 int keytyped;
7050 int when;
7051 int line_is_empty;
7052{
7053 char_u *look;
7054 int try_match;
7055 int try_match_word;
7056 char_u *p;
7057 char_u *line;
7058 int icase;
7059 int i;
7060
7061#ifdef FEAT_EVAL
7062 if (*curbuf->b_p_inde != NUL)
7063 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7064 else
7065#endif
7066 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7067 while (*look)
7068 {
7069 /*
7070 * Find out if we want to try a match with this key, depending on
7071 * 'when' and a '*' or '!' before the key.
7072 */
7073 switch (when)
7074 {
7075 case '*': try_match = (*look == '*'); break;
7076 case '!': try_match = (*look == '!'); break;
7077 default: try_match = (*look != '*'); break;
7078 }
7079 if (*look == '*' || *look == '!')
7080 ++look;
7081
7082 /*
7083 * If there is a '0', only accept a match if the line is empty.
7084 * But may still match when typing last char of a word.
7085 */
7086 if (*look == '0')
7087 {
7088 try_match_word = try_match;
7089 if (!line_is_empty)
7090 try_match = FALSE;
7091 ++look;
7092 }
7093 else
7094 try_match_word = FALSE;
7095
7096 /*
7097 * does it look like a control character?
7098 */
7099 if (*look == '^'
7100#ifdef EBCDIC
7101 && (Ctrl_chr(look[1]) != 0)
7102#else
7103 && look[1] >= '?' && look[1] <= '_'
7104#endif
7105 )
7106 {
7107 if (try_match && keytyped == Ctrl_chr(look[1]))
7108 return TRUE;
7109 look += 2;
7110 }
7111 /*
7112 * 'o' means "o" command, open forward.
7113 * 'O' means "O" command, open backward.
7114 */
7115 else if (*look == 'o')
7116 {
7117 if (try_match && keytyped == KEY_OPEN_FORW)
7118 return TRUE;
7119 ++look;
7120 }
7121 else if (*look == 'O')
7122 {
7123 if (try_match && keytyped == KEY_OPEN_BACK)
7124 return TRUE;
7125 ++look;
7126 }
7127
7128 /*
7129 * 'e' means to check for "else" at start of line and just before the
7130 * cursor.
7131 */
7132 else if (*look == 'e')
7133 {
7134 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7135 {
7136 p = ml_get_curline();
7137 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7138 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7139 return TRUE;
7140 }
7141 ++look;
7142 }
7143
7144 /*
7145 * ':' only causes an indent if it is at the end of a label or case
7146 * statement, or when it was before typing the ':' (to fix
7147 * class::method for C++).
7148 */
7149 else if (*look == ':')
7150 {
7151 if (try_match && keytyped == ':')
7152 {
7153 p = ml_get_curline();
7154 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7155 return TRUE;
7156 if (curwin->w_cursor.col > 2
7157 && p[curwin->w_cursor.col - 1] == ':'
7158 && p[curwin->w_cursor.col - 2] == ':')
7159 {
7160 p[curwin->w_cursor.col - 1] = ' ';
7161 i = (cin_iscase(p) || cin_isscopedecl(p)
7162 || cin_islabel(30));
7163 p = ml_get_curline();
7164 p[curwin->w_cursor.col - 1] = ':';
7165 if (i)
7166 return TRUE;
7167 }
7168 }
7169 ++look;
7170 }
7171
7172
7173 /*
7174 * Is it a key in <>, maybe?
7175 */
7176 else if (*look == '<')
7177 {
7178 if (try_match)
7179 {
7180 /*
7181 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7182 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7183 * >, *, : and ! keys if they really really want to.
7184 */
7185 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7186 && keytyped == look[1])
7187 return TRUE;
7188
7189 if (keytyped == get_special_key_code(look + 1))
7190 return TRUE;
7191 }
7192 while (*look && *look != '>')
7193 look++;
7194 while (*look == '>')
7195 look++;
7196 }
7197
7198 /*
7199 * Is it a word: "=word"?
7200 */
7201 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7202 {
7203 ++look;
7204 if (*look == '~')
7205 {
7206 icase = TRUE;
7207 ++look;
7208 }
7209 else
7210 icase = FALSE;
7211 p = vim_strchr(look, ',');
7212 if (p == NULL)
7213 p = look + STRLEN(look);
7214 if ((try_match || try_match_word)
7215 && curwin->w_cursor.col >= (colnr_T)(p - look))
7216 {
7217 int match = FALSE;
7218
7219#ifdef FEAT_INS_EXPAND
7220 if (keytyped == KEY_COMPLETE)
7221 {
7222 char_u *s;
7223
7224 /* Just completed a word, check if it starts with "look".
7225 * search back for the start of a word. */
7226 line = ml_get_curline();
7227# ifdef FEAT_MBYTE
7228 if (has_mbyte)
7229 {
7230 char_u *n;
7231
7232 for (s = line + curwin->w_cursor.col; s > line; s = n)
7233 {
7234 n = mb_prevptr(line, s);
7235 if (!vim_iswordp(n))
7236 break;
7237 }
7238 }
7239 else
7240# endif
7241 for (s = line + curwin->w_cursor.col; s > line; --s)
7242 if (!vim_iswordc(s[-1]))
7243 break;
7244 if (s + (p - look) <= line + curwin->w_cursor.col
7245 && (icase
7246 ? MB_STRNICMP(s, look, p - look)
7247 : STRNCMP(s, look, p - look)) == 0)
7248 match = TRUE;
7249 }
7250 else
7251#endif
7252 /* TODO: multi-byte */
7253 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7254 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7255 {
7256 line = ml_get_cursor();
7257 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7258 || !vim_iswordc(line[-(p - look) - 1]))
7259 && (icase
7260 ? MB_STRNICMP(line - (p - look), look, p - look)
7261 : STRNCMP(line - (p - look), look, p - look))
7262 == 0)
7263 match = TRUE;
7264 }
7265 if (match && try_match_word && !try_match)
7266 {
7267 /* "0=word": Check if there are only blanks before the
7268 * word. */
7269 line = ml_get_curline();
7270 if ((int)(skipwhite(line) - line) !=
7271 (int)(curwin->w_cursor.col - (p - look)))
7272 match = FALSE;
7273 }
7274 if (match)
7275 return TRUE;
7276 }
7277 look = p;
7278 }
7279
7280 /*
7281 * ok, it's a boring generic character.
7282 */
7283 else
7284 {
7285 if (try_match && *look == keytyped)
7286 return TRUE;
7287 ++look;
7288 }
7289
7290 /*
7291 * Skip over ", ".
7292 */
7293 look = skip_to_option_part(look);
7294 }
7295 return FALSE;
7296}
7297#endif /* FEAT_CINDENT */
7298
7299#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7300/*
7301 * Map Hebrew keyboard when in hkmap mode.
7302 */
7303 int
7304hkmap(c)
7305 int c;
7306{
7307 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7308 {
7309 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7310 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7311 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7312 static char_u map[26] =
7313 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7314 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7315 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7316 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7317 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7318 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7319 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7320 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7321 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7322
7323 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7324 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7325 /* '-1'='sofit' */
7326 else if (c == 'x')
7327 return 'X';
7328 else if (c == 'q')
7329 return '\''; /* {geresh}={'} */
7330 else if (c == 246)
7331 return ' '; /* \"o --> ' ' for a german keyboard */
7332 else if (c == 228)
7333 return ' '; /* \"a --> ' ' -- / -- */
7334 else if (c == 252)
7335 return ' '; /* \"u --> ' ' -- / -- */
7336#ifdef EBCDIC
7337 else if (islower(c))
7338#else
7339 /* NOTE: islower() does not do the right thing for us on Linux so we
7340 * do this the same was as 5.7 and previous, so it works correctly on
7341 * all systems. Specifically, the e.g. Delete and Arrow keys are
7342 * munged and won't work if e.g. searching for Hebrew text.
7343 */
7344 else if (c >= 'a' && c <= 'z')
7345#endif
7346 return (int)(map[CharOrdLow(c)] + p_aleph);
7347 else
7348 return c;
7349 }
7350 else
7351 {
7352 switch (c)
7353 {
7354 case '`': return ';';
7355 case '/': return '.';
7356 case '\'': return ',';
7357 case 'q': return '/';
7358 case 'w': return '\'';
7359
7360 /* Hebrew letters - set offset from 'a' */
7361 case ',': c = '{'; break;
7362 case '.': c = 'v'; break;
7363 case ';': c = 't'; break;
7364 default: {
7365 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7366
7367#ifdef EBCDIC
7368 /* see note about islower() above */
7369 if (!islower(c))
7370#else
7371 if (c < 'a' || c > 'z')
7372#endif
7373 return c;
7374 c = str[CharOrdLow(c)];
7375 break;
7376 }
7377 }
7378
7379 return (int)(CharOrdLow(c) + p_aleph);
7380 }
7381}
7382#endif
7383
7384 static void
7385ins_reg()
7386{
7387 int need_redraw = FALSE;
7388 int regname;
7389 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007390#ifdef FEAT_VISUAL
7391 int vis_active = VIsual_active;
7392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
7394 /*
7395 * If we are going to wait for a character, show a '"'.
7396 */
7397 pc_status = PC_STATUS_UNSET;
7398 if (redrawing() && !char_avail())
7399 {
7400 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007401 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 edit_putchar('"', TRUE);
7404#ifdef FEAT_CMDL_INFO
7405 add_to_showcmd_c(Ctrl_R);
7406#endif
7407 }
7408
7409#ifdef USE_ON_FLY_SCROLL
7410 dont_scroll = TRUE; /* disallow scrolling here */
7411#endif
7412
7413 /*
7414 * Don't map the register name. This also prevents the mode message to be
7415 * deleted when ESC is hit.
7416 */
7417 ++no_mapping;
7418 regname = safe_vgetc();
7419#ifdef FEAT_LANGMAP
7420 LANGMAP_ADJUST(regname, TRUE);
7421#endif
7422 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7423 {
7424 /* Get a third key for literal register insertion */
7425 literally = regname;
7426#ifdef FEAT_CMDL_INFO
7427 add_to_showcmd_c(literally);
7428#endif
7429 regname = safe_vgetc();
7430#ifdef FEAT_LANGMAP
7431 LANGMAP_ADJUST(regname, TRUE);
7432#endif
7433 }
7434 --no_mapping;
7435
7436#ifdef FEAT_EVAL
7437 /*
7438 * Don't call u_sync() while getting the expression,
7439 * evaluating it or giving an error message for it!
7440 */
7441 ++no_u_sync;
7442 if (regname == '=')
7443 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007444# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007448# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 /* Restore the Input Method. */
7450 if (im_on)
7451 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007454 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7455 {
7456 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 else
7460 {
7461#endif
7462 if (literally == Ctrl_O || literally == Ctrl_P)
7463 {
7464 /* Append the command to the redo buffer. */
7465 AppendCharToRedobuff(Ctrl_R);
7466 AppendCharToRedobuff(literally);
7467 AppendCharToRedobuff(regname);
7468
7469 do_put(regname, BACKWARD, 1L,
7470 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7471 }
7472 else if (insert_reg(regname, literally) == FAIL)
7473 {
7474 vim_beep();
7475 need_redraw = TRUE; /* remove the '"' */
7476 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007477 else if (stop_insert_mode)
7478 /* When the '=' register was used and a function was invoked that
7479 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7480 * insert anything, need to remove the '"' */
7481 need_redraw = TRUE;
7482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483#ifdef FEAT_EVAL
7484 }
7485 --no_u_sync;
7486#endif
7487#ifdef FEAT_CMDL_INFO
7488 clear_showcmd();
7489#endif
7490
7491 /* If the inserted register is empty, we need to remove the '"' */
7492 if (need_redraw || stuff_empty())
7493 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007494
7495#ifdef FEAT_VISUAL
7496 /* Disallow starting Visual mode here, would get a weird mode. */
7497 if (!vis_active && VIsual_active)
7498 end_visual_mode();
7499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500}
7501
7502/*
7503 * CTRL-G commands in Insert mode.
7504 */
7505 static void
7506ins_ctrl_g()
7507{
7508 int c;
7509
7510#ifdef FEAT_INS_EXPAND
7511 /* Right after CTRL-X the cursor will be after the ruler. */
7512 setcursor();
7513#endif
7514
7515 /*
7516 * Don't map the second key. This also prevents the mode message to be
7517 * deleted when ESC is hit.
7518 */
7519 ++no_mapping;
7520 c = safe_vgetc();
7521 --no_mapping;
7522 switch (c)
7523 {
7524 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7525 case K_UP:
7526 case Ctrl_K:
7527 case 'k': ins_up(TRUE);
7528 break;
7529
7530 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7531 case K_DOWN:
7532 case Ctrl_J:
7533 case 'j': ins_down(TRUE);
7534 break;
7535
7536 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007537 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007539
7540 /* Need to reset Insstart, esp. because a BS that joins
7541 * aline to the previous one must save for undo. */
7542 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 break;
7544
7545 /* Unknown CTRL-G command, reserved for future expansion. */
7546 default: vim_beep();
7547 }
7548}
7549
7550/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007551 * CTRL-^ in Insert mode.
7552 */
7553 static void
7554ins_ctrl_hat()
7555{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007556 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007557 {
7558 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7559 if (State & LANGMAP)
7560 {
7561 curbuf->b_p_iminsert = B_IMODE_NONE;
7562 State &= ~LANGMAP;
7563 }
7564 else
7565 {
7566 curbuf->b_p_iminsert = B_IMODE_LMAP;
7567 State |= LANGMAP;
7568#ifdef USE_IM_CONTROL
7569 im_set_active(FALSE);
7570#endif
7571 }
7572 }
7573#ifdef USE_IM_CONTROL
7574 else
7575 {
7576 /* There are no ":lmap" mappings, toggle IM */
7577 if (im_get_status())
7578 {
7579 curbuf->b_p_iminsert = B_IMODE_NONE;
7580 im_set_active(FALSE);
7581 }
7582 else
7583 {
7584 curbuf->b_p_iminsert = B_IMODE_IM;
7585 State &= ~LANGMAP;
7586 im_set_active(TRUE);
7587 }
7588 }
7589#endif
7590 set_iminsert_global();
7591 showmode();
7592#ifdef FEAT_GUI
7593 /* may show different cursor shape or color */
7594 if (gui.in_use)
7595 gui_update_cursor(TRUE, FALSE);
7596#endif
7597#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7598 /* Show/unshow value of 'keymap' in status lines. */
7599 status_redraw_curbuf();
7600#endif
7601}
7602
7603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 * Handle ESC in insert mode.
7605 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7606 * insert.
7607 */
7608 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007609ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 long *count;
7611 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007612 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 int temp;
7615 static int disabled_redraw = FALSE;
7616
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007617#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007618 check_spell_redraw();
7619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620#if defined(FEAT_HANGULIN)
7621# if defined(ESC_CHG_TO_ENG_MODE)
7622 hangul_input_state_set(0);
7623# endif
7624 if (composing_hangul)
7625 {
7626 push_raw_key(composing_hangul_buffer, 2);
7627 composing_hangul = 0;
7628 }
7629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630
7631 temp = curwin->w_cursor.col;
7632 if (disabled_redraw)
7633 {
7634 --RedrawingDisabled;
7635 disabled_redraw = FALSE;
7636 }
7637 if (!arrow_used)
7638 {
7639 /*
7640 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007641 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7642 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 */
7644 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007645 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
7647 /*
7648 * Repeating insert may take a long time. Check for
7649 * interrupt now and then.
7650 */
7651 if (*count > 0)
7652 {
7653 line_breakcheck();
7654 if (got_int)
7655 *count = 0;
7656 }
7657
7658 if (--*count > 0) /* repeat what was typed */
7659 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007660 /* Vi repeats the insert without replacing characters. */
7661 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7662 State &= ~REPLACE_FLAG;
7663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 (void)start_redo_ins();
7665 if (cmdchar == 'r' || cmdchar == 'v')
7666 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7667 ++RedrawingDisabled;
7668 disabled_redraw = TRUE;
7669 return FALSE; /* repeat the insert */
7670 }
7671 stop_insert(&curwin->w_cursor, TRUE);
7672 undisplay_dollar();
7673 }
7674
7675 /* When an autoindent was removed, curswant stays after the
7676 * indent */
7677 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7678 curwin->w_set_curswant = TRUE;
7679
7680 /* Remember the last Insert position in the '^ mark. */
7681 if (!cmdmod.keepjumps)
7682 curbuf->b_last_insert = curwin->w_cursor;
7683
7684 /*
7685 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007686 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007688 if (!nomove
7689 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690#ifdef FEAT_VIRTUALEDIT
7691 || curwin->w_cursor.coladd > 0
7692#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007693 )
7694 && (restart_edit == NUL
7695 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007697 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007699 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700#ifdef FEAT_RIGHTLEFT
7701 && !revins_on
7702#endif
7703 )
7704 {
7705#ifdef FEAT_VIRTUALEDIT
7706 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7707 {
7708 oneleft();
7709 if (restart_edit != NUL)
7710 ++curwin->w_cursor.coladd;
7711 }
7712 else
7713#endif
7714 {
7715 --curwin->w_cursor.col;
7716#ifdef FEAT_MBYTE
7717 /* Correct cursor for multi-byte character. */
7718 if (has_mbyte)
7719 mb_adjust_cursor();
7720#endif
7721 }
7722 }
7723
7724#ifdef USE_IM_CONTROL
7725 /* Disable IM to allow typing English directly for Normal mode commands.
7726 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7727 * well). */
7728 if (!(State & LANGMAP))
7729 im_save_status(&curbuf->b_p_iminsert);
7730 im_set_active(FALSE);
7731#endif
7732
7733 State = NORMAL;
7734 /* need to position cursor again (e.g. when on a TAB ) */
7735 changed_cline_bef_curs();
7736
7737#ifdef FEAT_MOUSE
7738 setmouse();
7739#endif
7740#ifdef CURSOR_SHAPE
7741 ui_cursor_shape(); /* may show different cursor shape */
7742#endif
7743
7744 /*
7745 * When recording or for CTRL-O, need to display the new mode.
7746 * Otherwise remove the mode message.
7747 */
7748 if (Recording || restart_edit != NUL)
7749 showmode();
7750 else if (p_smd)
7751 MSG("");
7752
7753 return TRUE; /* exit Insert mode */
7754}
7755
7756#ifdef FEAT_RIGHTLEFT
7757/*
7758 * Toggle language: hkmap and revins_on.
7759 * Move to end of reverse inserted text.
7760 */
7761 static void
7762ins_ctrl_()
7763{
7764 if (revins_on && revins_chars && revins_scol >= 0)
7765 {
7766 while (gchar_cursor() != NUL && revins_chars--)
7767 ++curwin->w_cursor.col;
7768 }
7769 p_ri = !p_ri;
7770 revins_on = (State == INSERT && p_ri);
7771 if (revins_on)
7772 {
7773 revins_scol = curwin->w_cursor.col;
7774 revins_legal++;
7775 revins_chars = 0;
7776 undisplay_dollar();
7777 }
7778 else
7779 revins_scol = -1;
7780#ifdef FEAT_FKMAP
7781 if (p_altkeymap)
7782 {
7783 /*
7784 * to be consistent also for redo command, using '.'
7785 * set arrow_used to true and stop it - causing to redo
7786 * characters entered in one mode (normal/reverse insert).
7787 */
7788 arrow_used = TRUE;
7789 (void)stop_arrow();
7790 p_fkmap = curwin->w_p_rl ^ p_ri;
7791 if (p_fkmap && p_ri)
7792 State = INSERT;
7793 }
7794 else
7795#endif
7796 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7797 showmode();
7798}
7799#endif
7800
7801#ifdef FEAT_VISUAL
7802/*
7803 * If 'keymodel' contains "startsel", may start selection.
7804 * Returns TRUE when a CTRL-O and other keys stuffed.
7805 */
7806 static int
7807ins_start_select(c)
7808 int c;
7809{
7810 if (km_startsel)
7811 switch (c)
7812 {
7813 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 case K_PAGEUP:
7816 case K_KPAGEUP:
7817 case K_PAGEDOWN:
7818 case K_KPAGEDOWN:
7819# ifdef MACOS
7820 case K_LEFT:
7821 case K_RIGHT:
7822 case K_UP:
7823 case K_DOWN:
7824 case K_END:
7825 case K_HOME:
7826# endif
7827 if (!(mod_mask & MOD_MASK_SHIFT))
7828 break;
7829 /* FALLTHROUGH */
7830 case K_S_LEFT:
7831 case K_S_RIGHT:
7832 case K_S_UP:
7833 case K_S_DOWN:
7834 case K_S_END:
7835 case K_S_HOME:
7836 /* Start selection right away, the cursor can move with
7837 * CTRL-O when beyond the end of the line. */
7838 start_selection();
7839
7840 /* Execute the key in (insert) Select mode. */
7841 stuffcharReadbuff(Ctrl_O);
7842 if (mod_mask)
7843 {
7844 char_u buf[4];
7845
7846 buf[0] = K_SPECIAL;
7847 buf[1] = KS_MODIFIER;
7848 buf[2] = mod_mask;
7849 buf[3] = NUL;
7850 stuffReadbuff(buf);
7851 }
7852 stuffcharReadbuff(c);
7853 return TRUE;
7854 }
7855 return FALSE;
7856}
7857#endif
7858
7859/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007860 * <Insert> key in Insert mode: toggle insert/remplace mode.
7861 */
7862 static void
7863ins_insert(replaceState)
7864 int replaceState;
7865{
7866#ifdef FEAT_FKMAP
7867 if (p_fkmap && p_ri)
7868 {
7869 beep_flush();
7870 EMSG(farsi_text_3); /* encoded in Farsi */
7871 return;
7872 }
7873#endif
7874
7875#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007876# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007877 set_vim_var_string(VV_INSERTMODE,
7878 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007879# ifdef FEAT_VREPLACE
7880 replaceState == VREPLACE ? "v" :
7881# endif
7882 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007883# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007884 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7885#endif
7886 if (State & REPLACE_FLAG)
7887 State = INSERT | (State & LANGMAP);
7888 else
7889 State = replaceState | (State & LANGMAP);
7890 AppendCharToRedobuff(K_INS);
7891 showmode();
7892#ifdef CURSOR_SHAPE
7893 ui_cursor_shape(); /* may show different cursor shape */
7894#endif
7895}
7896
7897/*
7898 * Pressed CTRL-O in Insert mode.
7899 */
7900 static void
7901ins_ctrl_o()
7902{
7903#ifdef FEAT_VREPLACE
7904 if (State & VREPLACE_FLAG)
7905 restart_edit = 'V';
7906 else
7907#endif
7908 if (State & REPLACE_FLAG)
7909 restart_edit = 'R';
7910 else
7911 restart_edit = 'I';
7912#ifdef FEAT_VIRTUALEDIT
7913 if (virtual_active())
7914 ins_at_eol = FALSE; /* cursor always keeps its column */
7915 else
7916#endif
7917 ins_at_eol = (gchar_cursor() == NUL);
7918}
7919
7920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 * If the cursor is on an indent, ^T/^D insert/delete one
7922 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7923 * Always round the indent to 'shiftwith', this is compatible
7924 * with vi. But vi only supports ^T and ^D after an
7925 * autoindent, we support it everywhere.
7926 */
7927 static void
7928ins_shift(c, lastc)
7929 int c;
7930 int lastc;
7931{
7932 if (stop_arrow() == FAIL)
7933 return;
7934 AppendCharToRedobuff(c);
7935
7936 /*
7937 * 0^D and ^^D: remove all indent.
7938 */
7939 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7940 {
7941 --curwin->w_cursor.col;
7942 (void)del_char(FALSE); /* delete the '^' or '0' */
7943 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7944 if (State & REPLACE_FLAG)
7945 replace_pop_ins();
7946 if (lastc == '^')
7947 old_indent = get_indent(); /* remember curr. indent */
7948 change_indent(INDENT_SET, 0, TRUE, 0);
7949 }
7950 else
7951 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7952
7953 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7954 did_ai = FALSE;
7955#ifdef FEAT_SMARTINDENT
7956 did_si = FALSE;
7957 can_si = FALSE;
7958 can_si_back = FALSE;
7959#endif
7960#ifdef FEAT_CINDENT
7961 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7962#endif
7963}
7964
7965 static void
7966ins_del()
7967{
7968 int temp;
7969
7970 if (stop_arrow() == FAIL)
7971 return;
7972 if (gchar_cursor() == NUL) /* delete newline */
7973 {
7974 temp = curwin->w_cursor.col;
7975 if (!can_bs(BS_EOL) /* only if "eol" included */
7976 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7977 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7978 || do_join(FALSE) == FAIL)
7979 vim_beep();
7980 else
7981 curwin->w_cursor.col = temp;
7982 }
7983 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7984 vim_beep();
7985 did_ai = FALSE;
7986#ifdef FEAT_SMARTINDENT
7987 did_si = FALSE;
7988 can_si = FALSE;
7989 can_si_back = FALSE;
7990#endif
7991 AppendCharToRedobuff(K_DEL);
7992}
7993
7994/*
7995 * Handle Backspace, delete-word and delete-line in Insert mode.
7996 * Return TRUE when backspace was actually used.
7997 */
7998 static int
7999ins_bs(c, mode, inserted_space_p)
8000 int c;
8001 int mode;
8002 int *inserted_space_p;
8003{
8004 linenr_T lnum;
8005 int cc;
8006 int temp = 0; /* init for GCC */
8007 colnr_T mincol;
8008 int did_backspace = FALSE;
8009 int in_indent;
8010 int oldState;
8011#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008012 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013#endif
8014
8015 /*
8016 * can't delete anything in an empty file
8017 * can't backup past first character in buffer
8018 * can't backup past starting point unless 'backspace' > 1
8019 * can backup to a previous line if 'backspace' == 0
8020 */
8021 if ( bufempty()
8022 || (
8023#ifdef FEAT_RIGHTLEFT
8024 !revins_on &&
8025#endif
8026 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8027 || (!can_bs(BS_START)
8028 && (arrow_used
8029 || (curwin->w_cursor.lnum == Insstart.lnum
8030 && curwin->w_cursor.col <= Insstart.col)))
8031 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8032 && curwin->w_cursor.col <= ai_col)
8033 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8034 {
8035 vim_beep();
8036 return FALSE;
8037 }
8038
8039 if (stop_arrow() == FAIL)
8040 return FALSE;
8041 in_indent = inindent(0);
8042#ifdef FEAT_CINDENT
8043 if (in_indent)
8044 can_cindent = FALSE;
8045#endif
8046#ifdef FEAT_COMMENTS
8047 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8048#endif
8049#ifdef FEAT_RIGHTLEFT
8050 if (revins_on) /* put cursor after last inserted char */
8051 inc_cursor();
8052#endif
8053
8054#ifdef FEAT_VIRTUALEDIT
8055 /* Virtualedit:
8056 * BACKSPACE_CHAR eats a virtual space
8057 * BACKSPACE_WORD eats all coladd
8058 * BACKSPACE_LINE eats all coladd and keeps going
8059 */
8060 if (curwin->w_cursor.coladd > 0)
8061 {
8062 if (mode == BACKSPACE_CHAR)
8063 {
8064 --curwin->w_cursor.coladd;
8065 return TRUE;
8066 }
8067 if (mode == BACKSPACE_WORD)
8068 {
8069 curwin->w_cursor.coladd = 0;
8070 return TRUE;
8071 }
8072 curwin->w_cursor.coladd = 0;
8073 }
8074#endif
8075
8076 /*
8077 * delete newline!
8078 */
8079 if (curwin->w_cursor.col == 0)
8080 {
8081 lnum = Insstart.lnum;
8082 if (curwin->w_cursor.lnum == Insstart.lnum
8083#ifdef FEAT_RIGHTLEFT
8084 || revins_on
8085#endif
8086 )
8087 {
8088 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8089 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8090 return FALSE;
8091 --Insstart.lnum;
8092 Insstart.col = MAXCOL;
8093 }
8094 /*
8095 * In replace mode:
8096 * cc < 0: NL was inserted, delete it
8097 * cc >= 0: NL was replaced, put original characters back
8098 */
8099 cc = -1;
8100 if (State & REPLACE_FLAG)
8101 cc = replace_pop(); /* returns -1 if NL was inserted */
8102 /*
8103 * In replace mode, in the line we started replacing, we only move the
8104 * cursor.
8105 */
8106 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8107 {
8108 dec_cursor();
8109 }
8110 else
8111 {
8112#ifdef FEAT_VREPLACE
8113 if (!(State & VREPLACE_FLAG)
8114 || curwin->w_cursor.lnum > orig_line_count)
8115#endif
8116 {
8117 temp = gchar_cursor(); /* remember current char */
8118 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008119
8120 /* When "aw" is in 'formatoptions' we must delete the space at
8121 * the end of the line, otherwise the line will be broken
8122 * again when auto-formatting. */
8123 if (has_format_option(FO_AUTO)
8124 && has_format_option(FO_WHITE_PAR))
8125 {
8126 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8127 TRUE);
8128 int len;
8129
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008130 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008131 if (len > 0 && ptr[len - 1] == ' ')
8132 ptr[len - 1] = NUL;
8133 }
8134
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 (void)do_join(FALSE);
8136 if (temp == NUL && gchar_cursor() != NUL)
8137 inc_cursor();
8138 }
8139#ifdef FEAT_VREPLACE
8140 else
8141 dec_cursor();
8142#endif
8143
8144 /*
8145 * In REPLACE mode we have to put back the text that was replaced
8146 * by the NL. On the replace stack is first a NUL-terminated
8147 * sequence of characters that were deleted and then the
8148 * characters that NL replaced.
8149 */
8150 if (State & REPLACE_FLAG)
8151 {
8152 /*
8153 * Do the next ins_char() in NORMAL state, to
8154 * prevent ins_char() from replacing characters and
8155 * avoiding showmatch().
8156 */
8157 oldState = State;
8158 State = NORMAL;
8159 /*
8160 * restore characters (blanks) deleted after cursor
8161 */
8162 while (cc > 0)
8163 {
8164 temp = curwin->w_cursor.col;
8165#ifdef FEAT_MBYTE
8166 mb_replace_pop_ins(cc);
8167#else
8168 ins_char(cc);
8169#endif
8170 curwin->w_cursor.col = temp;
8171 cc = replace_pop();
8172 }
8173 /* restore the characters that NL replaced */
8174 replace_pop_ins();
8175 State = oldState;
8176 }
8177 }
8178 did_ai = FALSE;
8179 }
8180 else
8181 {
8182 /*
8183 * Delete character(s) before the cursor.
8184 */
8185#ifdef FEAT_RIGHTLEFT
8186 if (revins_on) /* put cursor on last inserted char */
8187 dec_cursor();
8188#endif
8189 mincol = 0;
8190 /* keep indent */
8191 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8192#ifdef FEAT_RIGHTLEFT
8193 && !revins_on
8194#endif
8195 )
8196 {
8197 temp = curwin->w_cursor.col;
8198 beginline(BL_WHITE);
8199 if (curwin->w_cursor.col < (colnr_T)temp)
8200 mincol = curwin->w_cursor.col;
8201 curwin->w_cursor.col = temp;
8202 }
8203
8204 /*
8205 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8206 */
8207 if ( mode == BACKSPACE_CHAR
8208 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008209 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 && (*(ml_get_cursor() - 1) == TAB
8211 || (*(ml_get_cursor() - 1) == ' '
8212 && (!*inserted_space_p
8213 || arrow_used))))))
8214 {
8215 int ts;
8216 colnr_T vcol;
8217 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008218#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
8222 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008223 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 ts = curbuf->b_p_sw;
8225 else
8226 ts = curbuf->b_p_sts;
8227 /* Compute the virtual column where we want to be. Since
8228 * 'showbreak' may get in the way, need to get the last column of
8229 * the previous character. */
8230 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8231 dec_cursor();
8232 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8233 inc_cursor();
8234 want_vcol = (want_vcol / ts) * ts;
8235
8236 /* delete characters until we are at or before want_vcol */
8237 while (vcol > want_vcol
8238 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8239 {
8240 dec_cursor();
8241 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8242 if (State & REPLACE_FLAG)
8243 {
8244 /* Don't delete characters before the insert point when in
8245 * Replace mode */
8246 if (curwin->w_cursor.lnum != Insstart.lnum
8247 || curwin->w_cursor.col >= Insstart.col)
8248 {
8249#if 0 /* what was this for? It causes problems when sw != ts. */
8250 if (State == REPLACE && (int)vcol < want_vcol)
8251 {
8252 (void)del_char(FALSE);
8253 extra = 2; /* don't pop too much */
8254 }
8255 else
8256#endif
8257 replace_do_bs();
8258 }
8259 }
8260 else
8261 (void)del_char(FALSE);
8262 }
8263
8264 /* insert extra spaces until we are at want_vcol */
8265 while (vcol < want_vcol)
8266 {
8267 /* Remember the first char we inserted */
8268 if (curwin->w_cursor.lnum == Insstart.lnum
8269 && curwin->w_cursor.col < Insstart.col)
8270 Insstart.col = curwin->w_cursor.col;
8271
8272#ifdef FEAT_VREPLACE
8273 if (State & VREPLACE_FLAG)
8274 ins_char(' ');
8275 else
8276#endif
8277 {
8278 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008279 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008281#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 if (extra)
8283 replace_push_off(NUL);
8284 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 replace_push(NUL);
8287 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008288#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 if (extra == 2)
8290 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8294 }
8295 }
8296
8297 /*
8298 * Delete upto starting point, start of line or previous word.
8299 */
8300 else do
8301 {
8302#ifdef FEAT_RIGHTLEFT
8303 if (!revins_on) /* put cursor on char to be deleted */
8304#endif
8305 dec_cursor();
8306
8307 /* start of word? */
8308 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8309 {
8310 mode = BACKSPACE_WORD_NOT_SPACE;
8311 temp = vim_iswordc(gchar_cursor());
8312 }
8313 /* end of word? */
8314 else if (mode == BACKSPACE_WORD_NOT_SPACE
8315 && (vim_isspace(cc = gchar_cursor())
8316 || vim_iswordc(cc) != temp))
8317 {
8318#ifdef FEAT_RIGHTLEFT
8319 if (!revins_on)
8320#endif
8321 inc_cursor();
8322#ifdef FEAT_RIGHTLEFT
8323 else if (State & REPLACE_FLAG)
8324 dec_cursor();
8325#endif
8326 break;
8327 }
8328 if (State & REPLACE_FLAG)
8329 replace_do_bs();
8330 else
8331 {
8332#ifdef FEAT_MBYTE
8333 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008334 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335#endif
8336 (void)del_char(FALSE);
8337#ifdef FEAT_MBYTE
8338 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008339 * If there are combining characters and 'delcombine' is set
8340 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 * character.
8342 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008343 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 inc_cursor();
8345#endif
8346#ifdef FEAT_RIGHTLEFT
8347 if (revins_chars)
8348 {
8349 revins_chars--;
8350 revins_legal++;
8351 }
8352 if (revins_on && gchar_cursor() == NUL)
8353 break;
8354#endif
8355 }
8356 /* Just a single backspace?: */
8357 if (mode == BACKSPACE_CHAR)
8358 break;
8359 } while (
8360#ifdef FEAT_RIGHTLEFT
8361 revins_on ||
8362#endif
8363 (curwin->w_cursor.col > mincol
8364 && (curwin->w_cursor.lnum != Insstart.lnum
8365 || curwin->w_cursor.col != Insstart.col)));
8366 did_backspace = TRUE;
8367 }
8368#ifdef FEAT_SMARTINDENT
8369 did_si = FALSE;
8370 can_si = FALSE;
8371 can_si_back = FALSE;
8372#endif
8373 if (curwin->w_cursor.col <= 1)
8374 did_ai = FALSE;
8375 /*
8376 * It's a little strange to put backspaces into the redo
8377 * buffer, but it makes auto-indent a lot easier to deal
8378 * with.
8379 */
8380 AppendCharToRedobuff(c);
8381
8382 /* If deleted before the insertion point, adjust it */
8383 if (curwin->w_cursor.lnum == Insstart.lnum
8384 && curwin->w_cursor.col < Insstart.col)
8385 Insstart.col = curwin->w_cursor.col;
8386
8387 /* vi behaviour: the cursor moves backward but the character that
8388 * was there remains visible
8389 * Vim behaviour: the cursor moves backward and the character that
8390 * was there is erased from the screen.
8391 * We can emulate the vi behaviour by pretending there is a dollar
8392 * displayed even when there isn't.
8393 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8394 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8395 dollar_vcol = curwin->w_virtcol;
8396
8397 return did_backspace;
8398}
8399
8400#ifdef FEAT_MOUSE
8401 static void
8402ins_mouse(c)
8403 int c;
8404{
8405 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008406 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
8408# ifdef FEAT_GUI
8409 /* When GUI is active, also move/paste when 'mouse' is empty */
8410 if (!gui.in_use)
8411# endif
8412 if (!mouse_has(MOUSE_INSERT))
8413 return;
8414
8415 undisplay_dollar();
8416 tpos = curwin->w_cursor;
8417 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8418 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008419#ifdef FEAT_WINDOWS
8420 win_T *new_curwin = curwin;
8421
8422 if (curwin != old_curwin && win_valid(old_curwin))
8423 {
8424 /* Mouse took us to another window. We need to go back to the
8425 * previous one to stop insert there properly. */
8426 curwin = old_curwin;
8427 curbuf = curwin->w_buffer;
8428 }
8429#endif
8430 start_arrow(curwin == old_curwin ? &tpos : NULL);
8431#ifdef FEAT_WINDOWS
8432 if (curwin != new_curwin && win_valid(new_curwin))
8433 {
8434 curwin = new_curwin;
8435 curbuf = curwin->w_buffer;
8436 }
8437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438# ifdef FEAT_CINDENT
8439 can_cindent = TRUE;
8440# endif
8441 }
8442
8443#ifdef FEAT_WINDOWS
8444 /* redraw status lines (in case another window became active) */
8445 redraw_statuslines();
8446#endif
8447}
8448
8449 static void
8450ins_mousescroll(up)
8451 int up;
8452{
8453 pos_T tpos;
8454# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8455 win_T *old_curwin;
8456# endif
8457
8458 tpos = curwin->w_cursor;
8459
8460# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8461 old_curwin = curwin;
8462
8463 /* Currently the mouse coordinates are only known in the GUI. */
8464 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8465 {
8466 int row, col;
8467
8468 row = mouse_row;
8469 col = mouse_col;
8470
8471 /* find the window at the pointer coordinates */
8472 curwin = mouse_find_win(&row, &col);
8473 curbuf = curwin->w_buffer;
8474 }
8475 if (curwin == old_curwin)
8476# endif
8477 undisplay_dollar();
8478
8479 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8480 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8481 else
8482 scroll_redraw(up, 3L);
8483
8484# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8485 curwin->w_redr_status = TRUE;
8486
8487 curwin = old_curwin;
8488 curbuf = curwin->w_buffer;
8489# endif
8490
8491 if (!equalpos(curwin->w_cursor, tpos))
8492 {
8493 start_arrow(&tpos);
8494# ifdef FEAT_CINDENT
8495 can_cindent = TRUE;
8496# endif
8497 }
8498}
8499#endif
8500
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008501#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008502 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008503ins_tabline(c)
8504 int c;
8505{
8506 /* We will be leaving the current window, unless closing another tab. */
8507 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8508 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8509 {
8510 undisplay_dollar();
8511 start_arrow(&curwin->w_cursor);
8512# ifdef FEAT_CINDENT
8513 can_cindent = TRUE;
8514# endif
8515 }
8516
8517 if (c == K_TABLINE)
8518 goto_tabpage(current_tab);
8519 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008520 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008521 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008522 redraw_statuslines(); /* will redraw the tabline when needed */
8523 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008524}
8525#endif
8526
8527#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 void
8529ins_scroll()
8530{
8531 pos_T tpos;
8532
8533 undisplay_dollar();
8534 tpos = curwin->w_cursor;
8535 if (gui_do_scroll())
8536 {
8537 start_arrow(&tpos);
8538# ifdef FEAT_CINDENT
8539 can_cindent = TRUE;
8540# endif
8541 }
8542}
8543
8544 void
8545ins_horscroll()
8546{
8547 pos_T tpos;
8548
8549 undisplay_dollar();
8550 tpos = curwin->w_cursor;
8551 if (gui_do_horiz_scroll())
8552 {
8553 start_arrow(&tpos);
8554# ifdef FEAT_CINDENT
8555 can_cindent = TRUE;
8556# endif
8557 }
8558}
8559#endif
8560
8561 static void
8562ins_left()
8563{
8564 pos_T tpos;
8565
8566#ifdef FEAT_FOLDING
8567 if ((fdo_flags & FDO_HOR) && KeyTyped)
8568 foldOpenCursor();
8569#endif
8570 undisplay_dollar();
8571 tpos = curwin->w_cursor;
8572 if (oneleft() == OK)
8573 {
8574 start_arrow(&tpos);
8575#ifdef FEAT_RIGHTLEFT
8576 /* If exit reversed string, position is fixed */
8577 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8578 revins_legal++;
8579 revins_chars++;
8580#endif
8581 }
8582
8583 /*
8584 * if 'whichwrap' set for cursor in insert mode may go to
8585 * previous line
8586 */
8587 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8588 {
8589 start_arrow(&tpos);
8590 --(curwin->w_cursor.lnum);
8591 coladvance((colnr_T)MAXCOL);
8592 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8593 }
8594 else
8595 vim_beep();
8596}
8597
8598 static void
8599ins_home(c)
8600 int c;
8601{
8602 pos_T tpos;
8603
8604#ifdef FEAT_FOLDING
8605 if ((fdo_flags & FDO_HOR) && KeyTyped)
8606 foldOpenCursor();
8607#endif
8608 undisplay_dollar();
8609 tpos = curwin->w_cursor;
8610 if (c == K_C_HOME)
8611 curwin->w_cursor.lnum = 1;
8612 curwin->w_cursor.col = 0;
8613#ifdef FEAT_VIRTUALEDIT
8614 curwin->w_cursor.coladd = 0;
8615#endif
8616 curwin->w_curswant = 0;
8617 start_arrow(&tpos);
8618}
8619
8620 static void
8621ins_end(c)
8622 int c;
8623{
8624 pos_T tpos;
8625
8626#ifdef FEAT_FOLDING
8627 if ((fdo_flags & FDO_HOR) && KeyTyped)
8628 foldOpenCursor();
8629#endif
8630 undisplay_dollar();
8631 tpos = curwin->w_cursor;
8632 if (c == K_C_END)
8633 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8634 coladvance((colnr_T)MAXCOL);
8635 curwin->w_curswant = MAXCOL;
8636
8637 start_arrow(&tpos);
8638}
8639
8640 static void
8641ins_s_left()
8642{
8643#ifdef FEAT_FOLDING
8644 if ((fdo_flags & FDO_HOR) && KeyTyped)
8645 foldOpenCursor();
8646#endif
8647 undisplay_dollar();
8648 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8649 {
8650 start_arrow(&curwin->w_cursor);
8651 (void)bck_word(1L, FALSE, FALSE);
8652 curwin->w_set_curswant = TRUE;
8653 }
8654 else
8655 vim_beep();
8656}
8657
8658 static void
8659ins_right()
8660{
8661#ifdef FEAT_FOLDING
8662 if ((fdo_flags & FDO_HOR) && KeyTyped)
8663 foldOpenCursor();
8664#endif
8665 undisplay_dollar();
8666 if (gchar_cursor() != NUL || virtual_active()
8667 )
8668 {
8669 start_arrow(&curwin->w_cursor);
8670 curwin->w_set_curswant = TRUE;
8671#ifdef FEAT_VIRTUALEDIT
8672 if (virtual_active())
8673 oneright();
8674 else
8675#endif
8676 {
8677#ifdef FEAT_MBYTE
8678 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008679 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 else
8681#endif
8682 ++curwin->w_cursor.col;
8683 }
8684
8685#ifdef FEAT_RIGHTLEFT
8686 revins_legal++;
8687 if (revins_chars)
8688 revins_chars--;
8689#endif
8690 }
8691 /* if 'whichwrap' set for cursor in insert mode, may move the
8692 * cursor to the next line */
8693 else if (vim_strchr(p_ww, ']') != NULL
8694 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8695 {
8696 start_arrow(&curwin->w_cursor);
8697 curwin->w_set_curswant = TRUE;
8698 ++curwin->w_cursor.lnum;
8699 curwin->w_cursor.col = 0;
8700 }
8701 else
8702 vim_beep();
8703}
8704
8705 static void
8706ins_s_right()
8707{
8708#ifdef FEAT_FOLDING
8709 if ((fdo_flags & FDO_HOR) && KeyTyped)
8710 foldOpenCursor();
8711#endif
8712 undisplay_dollar();
8713 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8714 || gchar_cursor() != NUL)
8715 {
8716 start_arrow(&curwin->w_cursor);
8717 (void)fwd_word(1L, FALSE, 0);
8718 curwin->w_set_curswant = TRUE;
8719 }
8720 else
8721 vim_beep();
8722}
8723
8724 static void
8725ins_up(startcol)
8726 int startcol; /* when TRUE move to Insstart.col */
8727{
8728 pos_T tpos;
8729 linenr_T old_topline = curwin->w_topline;
8730#ifdef FEAT_DIFF
8731 int old_topfill = curwin->w_topfill;
8732#endif
8733
8734 undisplay_dollar();
8735 tpos = curwin->w_cursor;
8736 if (cursor_up(1L, TRUE) == OK)
8737 {
8738 if (startcol)
8739 coladvance(getvcol_nolist(&Insstart));
8740 if (old_topline != curwin->w_topline
8741#ifdef FEAT_DIFF
8742 || old_topfill != curwin->w_topfill
8743#endif
8744 )
8745 redraw_later(VALID);
8746 start_arrow(&tpos);
8747#ifdef FEAT_CINDENT
8748 can_cindent = TRUE;
8749#endif
8750 }
8751 else
8752 vim_beep();
8753}
8754
8755 static void
8756ins_pageup()
8757{
8758 pos_T tpos;
8759
8760 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008761
8762#ifdef FEAT_WINDOWS
8763 if (mod_mask & MOD_MASK_CTRL)
8764 {
8765 /* <C-PageUp>: tab page back */
8766 goto_tabpage(-1);
8767 return;
8768 }
8769#endif
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 tpos = curwin->w_cursor;
8772 if (onepage(BACKWARD, 1L) == OK)
8773 {
8774 start_arrow(&tpos);
8775#ifdef FEAT_CINDENT
8776 can_cindent = TRUE;
8777#endif
8778 }
8779 else
8780 vim_beep();
8781}
8782
8783 static void
8784ins_down(startcol)
8785 int startcol; /* when TRUE move to Insstart.col */
8786{
8787 pos_T tpos;
8788 linenr_T old_topline = curwin->w_topline;
8789#ifdef FEAT_DIFF
8790 int old_topfill = curwin->w_topfill;
8791#endif
8792
8793 undisplay_dollar();
8794 tpos = curwin->w_cursor;
8795 if (cursor_down(1L, TRUE) == OK)
8796 {
8797 if (startcol)
8798 coladvance(getvcol_nolist(&Insstart));
8799 if (old_topline != curwin->w_topline
8800#ifdef FEAT_DIFF
8801 || old_topfill != curwin->w_topfill
8802#endif
8803 )
8804 redraw_later(VALID);
8805 start_arrow(&tpos);
8806#ifdef FEAT_CINDENT
8807 can_cindent = TRUE;
8808#endif
8809 }
8810 else
8811 vim_beep();
8812}
8813
8814 static void
8815ins_pagedown()
8816{
8817 pos_T tpos;
8818
8819 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008820
8821#ifdef FEAT_WINDOWS
8822 if (mod_mask & MOD_MASK_CTRL)
8823 {
8824 /* <C-PageDown>: tab page forward */
8825 goto_tabpage(0);
8826 return;
8827 }
8828#endif
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 tpos = curwin->w_cursor;
8831 if (onepage(FORWARD, 1L) == OK)
8832 {
8833 start_arrow(&tpos);
8834#ifdef FEAT_CINDENT
8835 can_cindent = TRUE;
8836#endif
8837 }
8838 else
8839 vim_beep();
8840}
8841
8842#ifdef FEAT_DND
8843 static void
8844ins_drop()
8845{
8846 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8847}
8848#endif
8849
8850/*
8851 * Handle TAB in Insert or Replace mode.
8852 * Return TRUE when the TAB needs to be inserted like a normal character.
8853 */
8854 static int
8855ins_tab()
8856{
8857 int ind;
8858 int i;
8859 int temp;
8860
8861 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8862 Insstart_blank_vcol = get_nolist_virtcol();
8863 if (echeck_abbr(TAB + ABBR_OFF))
8864 return FALSE;
8865
8866 ind = inindent(0);
8867#ifdef FEAT_CINDENT
8868 if (ind)
8869 can_cindent = FALSE;
8870#endif
8871
8872 /*
8873 * When nothing special, insert TAB like a normal character
8874 */
8875 if (!curbuf->b_p_et
8876 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8877 && curbuf->b_p_sts == 0)
8878 return TRUE;
8879
8880 if (stop_arrow() == FAIL)
8881 return TRUE;
8882
8883 did_ai = FALSE;
8884#ifdef FEAT_SMARTINDENT
8885 did_si = FALSE;
8886 can_si = FALSE;
8887 can_si_back = FALSE;
8888#endif
8889 AppendToRedobuff((char_u *)"\t");
8890
8891 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8892 temp = (int)curbuf->b_p_sw;
8893 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8894 temp = (int)curbuf->b_p_sts;
8895 else /* otherwise use 'tabstop' */
8896 temp = (int)curbuf->b_p_ts;
8897 temp -= get_nolist_virtcol() % temp;
8898
8899 /*
8900 * Insert the first space with ins_char(). It will delete one char in
8901 * replace mode. Insert the rest with ins_str(); it will not delete any
8902 * chars. For VREPLACE mode, we use ins_char() for all characters.
8903 */
8904 ins_char(' ');
8905 while (--temp > 0)
8906 {
8907#ifdef FEAT_VREPLACE
8908 if (State & VREPLACE_FLAG)
8909 ins_char(' ');
8910 else
8911#endif
8912 {
8913 ins_str((char_u *)" ");
8914 if (State & REPLACE_FLAG) /* no char replaced */
8915 replace_push(NUL);
8916 }
8917 }
8918
8919 /*
8920 * When 'expandtab' not set: Replace spaces by TABs where possible.
8921 */
8922 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8923 {
8924 char_u *ptr;
8925#ifdef FEAT_VREPLACE
8926 char_u *saved_line = NULL; /* init for GCC */
8927 pos_T pos;
8928#endif
8929 pos_T fpos;
8930 pos_T *cursor;
8931 colnr_T want_vcol, vcol;
8932 int change_col = -1;
8933 int save_list = curwin->w_p_list;
8934
8935 /*
8936 * Get the current line. For VREPLACE mode, don't make real changes
8937 * yet, just work on a copy of the line.
8938 */
8939#ifdef FEAT_VREPLACE
8940 if (State & VREPLACE_FLAG)
8941 {
8942 pos = curwin->w_cursor;
8943 cursor = &pos;
8944 saved_line = vim_strsave(ml_get_curline());
8945 if (saved_line == NULL)
8946 return FALSE;
8947 ptr = saved_line + pos.col;
8948 }
8949 else
8950#endif
8951 {
8952 ptr = ml_get_cursor();
8953 cursor = &curwin->w_cursor;
8954 }
8955
8956 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8957 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8958 curwin->w_p_list = FALSE;
8959
8960 /* Find first white before the cursor */
8961 fpos = curwin->w_cursor;
8962 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8963 {
8964 --fpos.col;
8965 --ptr;
8966 }
8967
8968 /* In Replace mode, don't change characters before the insert point. */
8969 if ((State & REPLACE_FLAG)
8970 && fpos.lnum == Insstart.lnum
8971 && fpos.col < Insstart.col)
8972 {
8973 ptr += Insstart.col - fpos.col;
8974 fpos.col = Insstart.col;
8975 }
8976
8977 /* compute virtual column numbers of first white and cursor */
8978 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8979 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8980
8981 /* Use as many TABs as possible. Beware of 'showbreak' and
8982 * 'linebreak' adding extra virtual columns. */
8983 while (vim_iswhite(*ptr))
8984 {
8985 i = lbr_chartabsize((char_u *)"\t", vcol);
8986 if (vcol + i > want_vcol)
8987 break;
8988 if (*ptr != TAB)
8989 {
8990 *ptr = TAB;
8991 if (change_col < 0)
8992 {
8993 change_col = fpos.col; /* Column of first change */
8994 /* May have to adjust Insstart */
8995 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8996 Insstart.col = fpos.col;
8997 }
8998 }
8999 ++fpos.col;
9000 ++ptr;
9001 vcol += i;
9002 }
9003
9004 if (change_col >= 0)
9005 {
9006 int repl_off = 0;
9007
9008 /* Skip over the spaces we need. */
9009 while (vcol < want_vcol && *ptr == ' ')
9010 {
9011 vcol += lbr_chartabsize(ptr, vcol);
9012 ++ptr;
9013 ++repl_off;
9014 }
9015 if (vcol > want_vcol)
9016 {
9017 /* Must have a char with 'showbreak' just before it. */
9018 --ptr;
9019 --repl_off;
9020 }
9021 fpos.col += repl_off;
9022
9023 /* Delete following spaces. */
9024 i = cursor->col - fpos.col;
9025 if (i > 0)
9026 {
9027 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9028 /* correct replace stack. */
9029 if ((State & REPLACE_FLAG)
9030#ifdef FEAT_VREPLACE
9031 && !(State & VREPLACE_FLAG)
9032#endif
9033 )
9034 for (temp = i; --temp >= 0; )
9035 replace_join(repl_off);
9036 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009037#ifdef FEAT_NETBEANS_INTG
9038 if (usingNetbeans)
9039 {
9040 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9041 (long)(i + 1));
9042 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9043 (char_u *)"\t", 1);
9044 }
9045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 cursor->col -= i;
9047
9048#ifdef FEAT_VREPLACE
9049 /*
9050 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9051 * backspacing over the changed spacing and then inserting the new
9052 * spacing.
9053 */
9054 if (State & VREPLACE_FLAG)
9055 {
9056 /* Backspace from real cursor to change_col */
9057 backspace_until_column(change_col);
9058
9059 /* Insert each char in saved_line from changed_col to
9060 * ptr-cursor */
9061 ins_bytes_len(saved_line + change_col,
9062 cursor->col - change_col);
9063 }
9064#endif
9065 }
9066
9067#ifdef FEAT_VREPLACE
9068 if (State & VREPLACE_FLAG)
9069 vim_free(saved_line);
9070#endif
9071 curwin->w_p_list = save_list;
9072 }
9073
9074 return FALSE;
9075}
9076
9077/*
9078 * Handle CR or NL in insert mode.
9079 * Return TRUE when out of memory or can't undo.
9080 */
9081 static int
9082ins_eol(c)
9083 int c;
9084{
9085 int i;
9086
9087 if (echeck_abbr(c + ABBR_OFF))
9088 return FALSE;
9089 if (stop_arrow() == FAIL)
9090 return TRUE;
9091 undisplay_dollar();
9092
9093 /*
9094 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9095 * character under the cursor. Only push a NUL on the replace stack,
9096 * nothing to put back when the NL is deleted.
9097 */
9098 if ((State & REPLACE_FLAG)
9099#ifdef FEAT_VREPLACE
9100 && !(State & VREPLACE_FLAG)
9101#endif
9102 )
9103 replace_push(NUL);
9104
9105 /*
9106 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9107 * replacing the next line, so we push all of the characters left on the
9108 * line onto the replace stack. This is not done here though, it is done
9109 * in open_line().
9110 */
9111
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009112#ifdef FEAT_VIRTUALEDIT
9113 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9114 * CTRL-O). */
9115 if (virtual_active() && curwin->w_cursor.coladd > 0)
9116 coladvance(getviscol());
9117#endif
9118
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119#ifdef FEAT_RIGHTLEFT
9120# ifdef FEAT_FKMAP
9121 if (p_altkeymap && p_fkmap)
9122 fkmap(NL);
9123# endif
9124 /* NL in reverse insert will always start in the end of
9125 * current line. */
9126 if (revins_on)
9127 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9128#endif
9129
9130 AppendToRedobuff(NL_STR);
9131 i = open_line(FORWARD,
9132#ifdef FEAT_COMMENTS
9133 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9134#endif
9135 0, old_indent);
9136 old_indent = 0;
9137#ifdef FEAT_CINDENT
9138 can_cindent = TRUE;
9139#endif
9140
9141 return (!i);
9142}
9143
9144#ifdef FEAT_DIGRAPHS
9145/*
9146 * Handle digraph in insert mode.
9147 * Returns character still to be inserted, or NUL when nothing remaining to be
9148 * done.
9149 */
9150 static int
9151ins_digraph()
9152{
9153 int c;
9154 int cc;
9155
9156 pc_status = PC_STATUS_UNSET;
9157 if (redrawing() && !char_avail())
9158 {
9159 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009160 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162 edit_putchar('?', TRUE);
9163#ifdef FEAT_CMDL_INFO
9164 add_to_showcmd_c(Ctrl_K);
9165#endif
9166 }
9167
9168#ifdef USE_ON_FLY_SCROLL
9169 dont_scroll = TRUE; /* disallow scrolling here */
9170#endif
9171
9172 /* don't map the digraph chars. This also prevents the
9173 * mode message to be deleted when ESC is hit */
9174 ++no_mapping;
9175 ++allow_keys;
9176 c = safe_vgetc();
9177 --no_mapping;
9178 --allow_keys;
9179 if (IS_SPECIAL(c) || mod_mask) /* special key */
9180 {
9181#ifdef FEAT_CMDL_INFO
9182 clear_showcmd();
9183#endif
9184 insert_special(c, TRUE, FALSE);
9185 return NUL;
9186 }
9187 if (c != ESC)
9188 {
9189 if (redrawing() && !char_avail())
9190 {
9191 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009192 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194 if (char2cells(c) == 1)
9195 {
9196 /* first remove the '?', otherwise it's restored when typing
9197 * an ESC next */
9198 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009199 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 edit_putchar(c, TRUE);
9201 }
9202#ifdef FEAT_CMDL_INFO
9203 add_to_showcmd_c(c);
9204#endif
9205 }
9206 ++no_mapping;
9207 ++allow_keys;
9208 cc = safe_vgetc();
9209 --no_mapping;
9210 --allow_keys;
9211 if (cc != ESC)
9212 {
9213 AppendToRedobuff((char_u *)CTRL_V_STR);
9214 c = getdigraph(c, cc, TRUE);
9215#ifdef FEAT_CMDL_INFO
9216 clear_showcmd();
9217#endif
9218 return c;
9219 }
9220 }
9221 edit_unputchar();
9222#ifdef FEAT_CMDL_INFO
9223 clear_showcmd();
9224#endif
9225 return NUL;
9226}
9227#endif
9228
9229/*
9230 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9231 * Returns the char to be inserted, or NUL if none found.
9232 */
9233 static int
9234ins_copychar(lnum)
9235 linenr_T lnum;
9236{
9237 int c;
9238 int temp;
9239 char_u *ptr, *prev_ptr;
9240
9241 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9242 {
9243 vim_beep();
9244 return NUL;
9245 }
9246
9247 /* try to advance to the cursor column */
9248 temp = 0;
9249 ptr = ml_get(lnum);
9250 prev_ptr = ptr;
9251 validate_virtcol();
9252 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9253 {
9254 prev_ptr = ptr;
9255 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9256 }
9257 if ((colnr_T)temp > curwin->w_virtcol)
9258 ptr = prev_ptr;
9259
9260#ifdef FEAT_MBYTE
9261 c = (*mb_ptr2char)(ptr);
9262#else
9263 c = *ptr;
9264#endif
9265 if (c == NUL)
9266 vim_beep();
9267 return c;
9268}
9269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009270/*
9271 * CTRL-Y or CTRL-E typed in Insert mode.
9272 */
9273 static int
9274ins_ctrl_ey(tc)
9275 int tc;
9276{
9277 int c = tc;
9278
9279#ifdef FEAT_INS_EXPAND
9280 if (ctrl_x_mode == CTRL_X_SCROLL)
9281 {
9282 if (c == Ctrl_Y)
9283 scrolldown_clamp();
9284 else
9285 scrollup_clamp();
9286 redraw_later(VALID);
9287 }
9288 else
9289#endif
9290 {
9291 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9292 if (c != NUL)
9293 {
9294 long tw_save;
9295
9296 /* The character must be taken literally, insert like it
9297 * was typed after a CTRL-V, and pretend 'textwidth'
9298 * wasn't set. Digits, 'o' and 'x' are special after a
9299 * CTRL-V, don't use it for these. */
9300 if (c < 256 && !isalnum(c))
9301 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9302 tw_save = curbuf->b_p_tw;
9303 curbuf->b_p_tw = -1;
9304 insert_special(c, TRUE, FALSE);
9305 curbuf->b_p_tw = tw_save;
9306#ifdef FEAT_RIGHTLEFT
9307 revins_chars++;
9308 revins_legal++;
9309#endif
9310 c = Ctrl_V; /* pretend CTRL-V is last character */
9311 auto_format(FALSE, TRUE);
9312 }
9313 }
9314 return c;
9315}
9316
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317#ifdef FEAT_SMARTINDENT
9318/*
9319 * Try to do some very smart auto-indenting.
9320 * Used when inserting a "normal" character.
9321 */
9322 static void
9323ins_try_si(c)
9324 int c;
9325{
9326 pos_T *pos, old_pos;
9327 char_u *ptr;
9328 int i;
9329 int temp;
9330
9331 /*
9332 * do some very smart indenting when entering '{' or '}'
9333 */
9334 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9335 {
9336 /*
9337 * for '}' set indent equal to indent of line containing matching '{'
9338 */
9339 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9340 {
9341 old_pos = curwin->w_cursor;
9342 /*
9343 * If the matching '{' has a ')' immediately before it (ignoring
9344 * white-space), then line up with the start of the line
9345 * containing the matching '(' if there is one. This handles the
9346 * case where an "if (..\n..) {" statement continues over multiple
9347 * lines -- webb
9348 */
9349 ptr = ml_get(pos->lnum);
9350 i = pos->col;
9351 if (i > 0) /* skip blanks before '{' */
9352 while (--i > 0 && vim_iswhite(ptr[i]))
9353 ;
9354 curwin->w_cursor.lnum = pos->lnum;
9355 curwin->w_cursor.col = i;
9356 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9357 curwin->w_cursor = *pos;
9358 i = get_indent();
9359 curwin->w_cursor = old_pos;
9360#ifdef FEAT_VREPLACE
9361 if (State & VREPLACE_FLAG)
9362 change_indent(INDENT_SET, i, FALSE, NUL);
9363 else
9364#endif
9365 (void)set_indent(i, SIN_CHANGED);
9366 }
9367 else if (curwin->w_cursor.col > 0)
9368 {
9369 /*
9370 * when inserting '{' after "O" reduce indent, but not
9371 * more than indent of previous line
9372 */
9373 temp = TRUE;
9374 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9375 {
9376 old_pos = curwin->w_cursor;
9377 i = get_indent();
9378 while (curwin->w_cursor.lnum > 1)
9379 {
9380 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9381
9382 /* ignore empty lines and lines starting with '#'. */
9383 if (*ptr != '#' && *ptr != NUL)
9384 break;
9385 }
9386 if (get_indent() >= i)
9387 temp = FALSE;
9388 curwin->w_cursor = old_pos;
9389 }
9390 if (temp)
9391 shift_line(TRUE, FALSE, 1);
9392 }
9393 }
9394
9395 /*
9396 * set indent of '#' always to 0
9397 */
9398 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9399 {
9400 /* remember current indent for next line */
9401 old_indent = get_indent();
9402 (void)set_indent(0, SIN_CHANGED);
9403 }
9404
9405 /* Adjust ai_col, the char at this position can be deleted. */
9406 if (ai_col > curwin->w_cursor.col)
9407 ai_col = curwin->w_cursor.col;
9408}
9409#endif
9410
9411/*
9412 * Get the value that w_virtcol would have when 'list' is off.
9413 * Unless 'cpo' contains the 'L' flag.
9414 */
9415 static colnr_T
9416get_nolist_virtcol()
9417{
9418 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9419 return getvcol_nolist(&curwin->w_cursor);
9420 validate_virtcol();
9421 return curwin->w_virtcol;
9422}