blob: bfd27cec241c471dcc75fc711b1654b176289f38 [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 Moolenaar488c6512005-08-11 20:09:58 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^S^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 Moolenaar488c6512005-08-11 20:09:58 +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 Moolenaar8b6144b2006-02-08 09:20:24 +000072 char_u *cp_extra; /* extra menu text (allocated, can be NULL) */
73 char_u *cp_info; /* verbose info (can be NULL) */
74 char_u cp_kind; /* kind of match, single letter, or NUL */
75 char_u *cp_fname; /* file containing the match, allocated when
76 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
78 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079};
80
Bram Moolenaar572cb562005-08-05 21:35:02 +000081#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#define FREE_FNAME (2)
83
84/*
85 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000086 * "compl_first_match" points to the start of the list.
87 * "compl_curr_match" points to the currently selected entry.
88 * "compl_shown_match" is different from compl_curr_match during
89 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000091static compl_T *compl_first_match = NULL;
92static compl_T *compl_curr_match = NULL;
93static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaara6557602006-02-04 22:43:20 +000095/* When "compl_leader" is not NULL only matches that start with this string
96 * are used. */
97static char_u *compl_leader = NULL;
98
Bram Moolenaarc7453f52006-02-10 23:20:28 +000099static int compl_get_longest = FALSE; /* put longest common string
100 in compl_leader */
101
Bram Moolenaara6557602006-02-04 22:43:20 +0000102static int compl_used_match; /* Selected one of the matches. When
103 FALSE the match was edited or using
104 the longest common string. */
105
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000106/* When the first completion is done "compl_started" is set. When it's
107 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000109
Bram Moolenaar572cb562005-08-05 21:35:02 +0000110static int compl_matches = 0;
111static char_u *compl_pattern = NULL;
112static int compl_direction = FORWARD;
113static int compl_shows_dir = FORWARD;
114static int compl_pending = FALSE;
115static pos_T compl_startpos;
116static colnr_T compl_col = 0; /* column where the text starts
117 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000118static char_u *compl_orig_text = NULL; /* text as it was before
119 * completion started */
120static int compl_cont_mode = 0;
121static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000123static void ins_ctrl_x __ARGS((void));
124static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000125static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000126static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000127static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000129static void ins_compl_upd_pum __ARGS((void));
130static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000131static int pum_wanted __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000132static int pum_two_or_more __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000133static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000134static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static void ins_compl_free __ARGS((void));
136static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000137static int ins_compl_bs __ARGS((void));
138static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000139static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000140static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000142static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void ins_compl_delete __ARGS((void));
144static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000145static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000146static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000147static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000148static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000149static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static int ins_complete __ARGS((int c));
151static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
152#endif /* FEAT_INS_EXPAND */
153
154#define BACKSPACE_CHAR 1
155#define BACKSPACE_WORD 2
156#define BACKSPACE_WORD_NOT_SPACE 3
157#define BACKSPACE_LINE 4
158
Bram Moolenaar754b5602006-02-09 23:53:20 +0000159static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void ins_ctrl_v __ARGS((void));
161static void undisplay_dollar __ARGS((void));
162static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void check_auto_format __ARGS((int));
165static void redo_literal __ARGS((int c));
166static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000167#ifdef FEAT_SYN_HL
168static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000169static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000170static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
173static int echeck_abbr __ARGS((int));
174static void replace_push_off __ARGS((int c));
175static int replace_pop __ARGS((void));
176static void replace_join __ARGS((int off));
177static void replace_pop_ins __ARGS((void));
178#ifdef FEAT_MBYTE
179static void mb_replace_pop_ins __ARGS((int cc));
180#endif
181static void replace_flush __ARGS((void));
182static void replace_do_bs __ARGS((void));
183#ifdef FEAT_CINDENT
184static int cindent_on __ARGS((void));
185#endif
186static void ins_reg __ARGS((void));
187static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000188static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000189static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#ifdef FEAT_RIGHTLEFT
191static void ins_ctrl_ __ARGS((void));
192#endif
193#ifdef FEAT_VISUAL
194static int ins_start_select __ARGS((int c));
195#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000196static void ins_insert __ARGS((int replaceState));
197static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static void ins_shift __ARGS((int c, int lastc));
199static void ins_del __ARGS((void));
200static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
201#ifdef FEAT_MOUSE
202static void ins_mouse __ARGS((int c));
203static void ins_mousescroll __ARGS((int up));
204#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000205#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
206static void ins_tabline __ARGS((int c));
207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ins_left __ARGS((void));
209static void ins_home __ARGS((int c));
210static void ins_end __ARGS((int c));
211static void ins_s_left __ARGS((void));
212static void ins_right __ARGS((void));
213static void ins_s_right __ARGS((void));
214static void ins_up __ARGS((int startcol));
215static void ins_pageup __ARGS((void));
216static void ins_down __ARGS((int startcol));
217static void ins_pagedown __ARGS((void));
218#ifdef FEAT_DND
219static void ins_drop __ARGS((void));
220#endif
221static int ins_tab __ARGS((void));
222static int ins_eol __ARGS((int c));
223#ifdef FEAT_DIGRAPHS
224static int ins_digraph __ARGS((void));
225#endif
226static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000227static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifdef FEAT_SMARTINDENT
229static void ins_try_si __ARGS((int c));
230#endif
231static colnr_T get_nolist_virtcol __ARGS((void));
232
233static colnr_T Insstart_textlen; /* length of line when insert started */
234static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
235
236static char_u *last_insert = NULL; /* the text of the previous insert,
237 K_SPECIAL and CSI are escaped */
238static int last_insert_skip; /* nr of chars in front of previous insert */
239static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000240static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
242#ifdef FEAT_CINDENT
243static int can_cindent; /* may do cindenting on this line */
244#endif
245
246static int old_indent = 0; /* for ^^D command in insert mode */
247
248#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000249static int revins_on; /* reverse insert mode on */
250static int revins_chars; /* how much to skip after edit */
251static int revins_legal; /* was the last char 'legal'? */
252static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#endif
254
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255static int ins_need_undo; /* call u_save() before inserting a
256 char. Set when edit() is called.
257 after that arrow_used is used. */
258
259static int did_add_space = FALSE; /* auto_format() added an extra space
260 under the cursor */
261
262/*
263 * edit(): Start inserting text.
264 *
265 * "cmdchar" can be:
266 * 'i' normal insert command
267 * 'a' normal append command
268 * 'R' replace command
269 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
270 * but still only one <CR> is inserted. The <Esc> is not used for redo.
271 * 'g' "gI" command.
272 * 'V' "gR" command for Virtual Replace mode.
273 * 'v' "gr" command for single character Virtual Replace mode.
274 *
275 * This function is not called recursively. For CTRL-O commands, it returns
276 * and lets the caller handle the Normal-mode command.
277 *
278 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
279 */
280 int
281edit(cmdchar, startln, count)
282 int cmdchar;
283 int startln; /* if set, insert at start of line */
284 long count;
285{
286 int c = 0;
287 char_u *ptr;
288 int lastc;
289 colnr_T mincol;
290 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 int i;
292 int did_backspace = TRUE; /* previous char was backspace */
293#ifdef FEAT_CINDENT
294 int line_is_white = FALSE; /* line is empty before insert */
295#endif
296 linenr_T old_topline = 0; /* topline before insertion */
297#ifdef FEAT_DIFF
298 int old_topfill = -1;
299#endif
300 int inserted_space = FALSE; /* just inserted a space */
301 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000302 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000304 /* Remember whether editing was restarted after CTRL-O. */
305 did_restart_edit = restart_edit;
306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 /* sleep before redrawing, needed for "CTRL-O :" that results in an
308 * error message */
309 check_for_delay(TRUE);
310
311#ifdef HAVE_SANDBOX
312 /* Don't allow inserting in the sandbox. */
313 if (sandbox != 0)
314 {
315 EMSG(_(e_sandbox));
316 return FALSE;
317 }
318#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000319 /* Don't allow changes in the buffer while editing the cmdline. The
320 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000321 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000322 {
323 EMSG(_(e_secure));
324 return FALSE;
325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
327#ifdef FEAT_INS_EXPAND
328 ins_compl_clear(); /* clear stuff for CTRL-X mode */
329#endif
330
Bram Moolenaar843ee412004-06-30 16:16:41 +0000331#ifdef FEAT_AUTOCMD
332 /*
333 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
334 */
335 if (cmdchar != 'r' && cmdchar != 'v')
336 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000337# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000338 if (cmdchar == 'R')
339 ptr = (char_u *)"r";
340 else if (cmdchar == 'V')
341 ptr = (char_u *)"v";
342 else
343 ptr = (char_u *)"i";
344 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000345# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000346 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
347 }
348#endif
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#ifdef FEAT_MOUSE
351 /*
352 * When doing a paste with the middle mouse button, Insstart is set to
353 * where the paste started.
354 */
355 if (where_paste_started.lnum != 0)
356 Insstart = where_paste_started;
357 else
358#endif
359 {
360 Insstart = curwin->w_cursor;
361 if (startln)
362 Insstart.col = 0;
363 }
364 Insstart_textlen = linetabsize(ml_get_curline());
365 Insstart_blank_vcol = MAXCOL;
366 if (!did_ai)
367 ai_col = 0;
368
369 if (cmdchar != NUL && restart_edit == 0)
370 {
371 ResetRedobuff();
372 AppendNumberToRedobuff(count);
373#ifdef FEAT_VREPLACE
374 if (cmdchar == 'V' || cmdchar == 'v')
375 {
376 /* "gR" or "gr" command */
377 AppendCharToRedobuff('g');
378 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
379 }
380 else
381#endif
382 {
383 AppendCharToRedobuff(cmdchar);
384 if (cmdchar == 'g') /* "gI" command */
385 AppendCharToRedobuff('I');
386 else if (cmdchar == 'r') /* "r<CR>" command */
387 count = 1; /* insert only one <CR> */
388 }
389 }
390
391 if (cmdchar == 'R')
392 {
393#ifdef FEAT_FKMAP
394 if (p_fkmap && p_ri)
395 {
396 beep_flush();
397 EMSG(farsi_text_3); /* encoded in Farsi */
398 State = INSERT;
399 }
400 else
401#endif
402 State = REPLACE;
403 }
404#ifdef FEAT_VREPLACE
405 else if (cmdchar == 'V' || cmdchar == 'v')
406 {
407 State = VREPLACE;
408 replaceState = VREPLACE;
409 orig_line_count = curbuf->b_ml.ml_line_count;
410 vr_lines_changed = 1;
411 }
412#endif
413 else
414 State = INSERT;
415
416 stop_insert_mode = FALSE;
417
418 /*
419 * Need to recompute the cursor position, it might move when the cursor is
420 * on a TAB or special character.
421 */
422 curs_columns(TRUE);
423
424 /*
425 * Enable langmap or IME, indicated by 'iminsert'.
426 * Note that IME may enabled/disabled without us noticing here, thus the
427 * 'iminsert' value may not reflect what is actually used. It is updated
428 * when hitting <Esc>.
429 */
430 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
431 State |= LANGMAP;
432#ifdef USE_IM_CONTROL
433 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
434#endif
435
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#ifdef FEAT_MOUSE
437 setmouse();
438#endif
439#ifdef FEAT_CMDL_INFO
440 clear_showcmd();
441#endif
442#ifdef FEAT_RIGHTLEFT
443 /* there is no reverse replace mode */
444 revins_on = (State == INSERT && p_ri);
445 if (revins_on)
446 undisplay_dollar();
447 revins_chars = 0;
448 revins_legal = 0;
449 revins_scol = -1;
450#endif
451
452 /*
453 * Handle restarting Insert mode.
454 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
455 * restart_edit non-zero, and something in the stuff buffer.
456 */
457 if (restart_edit != 0 && stuff_empty())
458 {
459#ifdef FEAT_MOUSE
460 /*
461 * After a paste we consider text typed to be part of the insert for
462 * the pasted text. You can backspace over the pasted text too.
463 */
464 if (where_paste_started.lnum)
465 arrow_used = FALSE;
466 else
467#endif
468 arrow_used = TRUE;
469 restart_edit = 0;
470
471 /*
472 * If the cursor was after the end-of-line before the CTRL-O and it is
473 * now at the end-of-line, put it after the end-of-line (this is not
474 * correct in very rare cases).
475 * Also do this if curswant is greater than the current virtual
476 * column. Eg after "^O$" or "^O80|".
477 */
478 validate_virtcol();
479 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000480 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 || curwin->w_curswant > curwin->w_virtcol)
482 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
483 {
484 if (ptr[1] == NUL)
485 ++curwin->w_cursor.col;
486#ifdef FEAT_MBYTE
487 else if (has_mbyte)
488 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000489 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (ptr[i] == NUL)
491 curwin->w_cursor.col += i;
492 }
493#endif
494 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000495 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 }
497 else
498 arrow_used = FALSE;
499
500 /* we are in insert mode now, don't need to start it anymore */
501 need_start_insertmode = FALSE;
502
503 /* Need to save the line for undo before inserting the first char. */
504 ins_need_undo = TRUE;
505
506#ifdef FEAT_MOUSE
507 where_paste_started.lnum = 0;
508#endif
509#ifdef FEAT_CINDENT
510 can_cindent = TRUE;
511#endif
512#ifdef FEAT_FOLDING
513 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
514 * restarting. */
515 if (!p_im && did_restart_edit == 0)
516 foldOpenCursor();
517#endif
518
519 /*
520 * If 'showmode' is set, show the current (insert/replace/..) mode.
521 * A warning message for changing a readonly file is given here, before
522 * actually changing anything. It's put after the mode, if any.
523 */
524 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000525 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 i = showmode();
527
528 if (!p_im && did_restart_edit == 0)
529 change_warning(i + 1);
530
531#ifdef CURSOR_SHAPE
532 ui_cursor_shape(); /* may show different cursor shape */
533#endif
534#ifdef FEAT_DIGRAPHS
535 do_digraph(-1); /* clear digraphs */
536#endif
537
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000538 /*
539 * Get the current length of the redo buffer, those characters have to be
540 * skipped if we want to get to the inserted characters.
541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 ptr = get_inserted();
543 if (ptr == NULL)
544 new_insert_skip = 0;
545 else
546 {
547 new_insert_skip = (int)STRLEN(ptr);
548 vim_free(ptr);
549 }
550
551 old_indent = 0;
552
553 /*
554 * Main loop in Insert mode: repeat until Insert mode is left.
555 */
556 for (;;)
557 {
558#ifdef FEAT_RIGHTLEFT
559 if (!revins_legal)
560 revins_scol = -1; /* reset on illegal motions */
561 else
562 revins_legal = 0;
563#endif
564 if (arrow_used) /* don't repeat insert when arrow key used */
565 count = 0;
566
567 if (stop_insert_mode)
568 {
569 /* ":stopinsert" used or 'insertmode' reset */
570 count = 0;
571 goto doESCkey;
572 }
573
574 /* set curwin->w_curswant for next K_DOWN or K_UP */
575 if (!arrow_used)
576 curwin->w_set_curswant = TRUE;
577
578 /* If there is no typeahead may check for timestamps (e.g., for when a
579 * menu invoked a shell command). */
580 if (stuff_empty())
581 {
582 did_check_timestamps = FALSE;
583 if (need_check_timestamps)
584 check_timestamps(FALSE);
585 }
586
587 /*
588 * When emsg() was called msg_scroll will have been set.
589 */
590 msg_scroll = FALSE;
591
592#ifdef FEAT_GUI
593 /* When 'mousefocus' is set a mouse movement may have taken us to
594 * another window. "need_mouse_correct" may then be set because of an
595 * autocommand. */
596 if (need_mouse_correct)
597 gui_mouse_correct();
598#endif
599
600#ifdef FEAT_FOLDING
601 /* Open fold at the cursor line, according to 'foldopen'. */
602 if (fdo_flags & FDO_INSERT)
603 foldOpenCursor();
604 /* Close folds where the cursor isn't, according to 'foldclose' */
605 if (!char_avail())
606 foldCheckClose();
607#endif
608
609 /*
610 * If we inserted a character at the last position of the last line in
611 * the window, scroll the window one line up. This avoids an extra
612 * redraw.
613 * This is detected when the cursor column is smaller after inserting
614 * something.
615 * Don't do this when the topline changed already, it has
616 * already been adjusted (by insertchar() calling open_line())).
617 */
618 if (curbuf->b_mod_set
619 && curwin->w_p_wrap
620 && !did_backspace
621 && curwin->w_topline == old_topline
622#ifdef FEAT_DIFF
623 && curwin->w_topfill == old_topfill
624#endif
625 )
626 {
627 mincol = curwin->w_wcol;
628 validate_cursor_col();
629
630 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
631 && curwin->w_wrow == W_WINROW(curwin)
632 + curwin->w_height - 1 - p_so
633 && (curwin->w_cursor.lnum != curwin->w_topline
634#ifdef FEAT_DIFF
635 || curwin->w_topfill > 0
636#endif
637 ))
638 {
639#ifdef FEAT_DIFF
640 if (curwin->w_topfill > 0)
641 --curwin->w_topfill;
642 else
643#endif
644#ifdef FEAT_FOLDING
645 if (hasFolding(curwin->w_topline, NULL, &old_topline))
646 set_topline(curwin, old_topline + 1);
647 else
648#endif
649 set_topline(curwin, curwin->w_topline + 1);
650 }
651 }
652
653 /* May need to adjust w_topline to show the cursor. */
654 update_topline();
655
656 did_backspace = FALSE;
657
658 validate_cursor(); /* may set must_redraw */
659
660 /*
661 * Redraw the display when no characters are waiting.
662 * Also shows mode, ruler and positions cursor.
663 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000664 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666#ifdef FEAT_SCROLLBIND
667 if (curwin->w_p_scb)
668 do_check_scrollbind(TRUE);
669#endif
670
671 update_curswant();
672 old_topline = curwin->w_topline;
673#ifdef FEAT_DIFF
674 old_topfill = curwin->w_topfill;
675#endif
676
677#ifdef USE_ON_FLY_SCROLL
678 dont_scroll = FALSE; /* allow scrolling here */
679#endif
680
681 /*
682 * Get a character for Insert mode.
683 */
684 lastc = c; /* remember previous char for CTRL-D */
685 c = safe_vgetc();
686
687#ifdef FEAT_RIGHTLEFT
688 if (p_hkmap && KeyTyped)
689 c = hkmap(c); /* Hebrew mode mapping */
690#endif
691#ifdef FEAT_FKMAP
692 if (p_fkmap && KeyTyped)
693 c = fkmap(c); /* Farsi mode mapping */
694#endif
695
696#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000697 /*
698 * Special handling of keys while the popup menu is visible or wanted
699 * and the cursor is still in the completed word.
700 */
701 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000702 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000703 /* BS: Delete one character from "compl_leader". */
704 if ((c == K_BS || c == Ctrl_H)
705 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000706 continue;
707
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000708 /* When no match was selected or it was edited. */
709 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000710 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000711 /* CTRL-L: Add one character from the current match to
712 * "compl_leader". */
713 if (c == Ctrl_L)
714 {
715 ins_compl_addfrommatch();
716 continue;
717 }
718
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000719 /* A printable, non-white character: Add to "compl_leader". */
720 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000721 {
722 ins_compl_addleader(c);
723 continue;
724 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000725
726 /* Pressing Enter selects the current match. */
727 if (c == CAR || c == K_KENTER || c == NL)
728 {
729 ins_compl_delete();
730 ins_compl_insert();
731 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000732 }
733 }
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
736 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000737 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000738 if (c != K_IGNORE && ins_compl_prep(c))
739 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#endif
741
Bram Moolenaar488c6512005-08-11 20:09:58 +0000742 /* CTRL-\ CTRL-N goes to Normal mode,
743 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
744 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 if (c == Ctrl_BSL)
746 {
747 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000748 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 ++no_mapping;
750 ++allow_keys;
751 c = safe_vgetc();
752 --no_mapping;
753 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000754 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000756 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 vungetc(c);
758 c = Ctrl_BSL;
759 }
760 else if (c == Ctrl_G && p_im)
761 continue;
762 else
763 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000764 if (c == Ctrl_O)
765 {
766 ins_ctrl_o();
767 ins_at_eol = FALSE; /* cursor keeps its column */
768 nomove = TRUE;
769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 count = 0;
771 goto doESCkey;
772 }
773 }
774
775#ifdef FEAT_DIGRAPHS
776 c = do_digraph(c);
777#endif
778
779#ifdef FEAT_INS_EXPAND
780 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
781 goto docomplete;
782#endif
783 if (c == Ctrl_V || c == Ctrl_Q)
784 {
785 ins_ctrl_v();
786 c = Ctrl_V; /* pretend CTRL-V is last typed character */
787 continue;
788 }
789
790#ifdef FEAT_CINDENT
791 if (cindent_on()
792# ifdef FEAT_INS_EXPAND
793 && ctrl_x_mode == 0
794# endif
795 )
796 {
797 /* A key name preceded by a bang means this key is not to be
798 * inserted. Skip ahead to the re-indenting below.
799 * A key name preceded by a star means that indenting has to be
800 * done before inserting the key. */
801 line_is_white = inindent(0);
802 if (in_cinkeys(c, '!', line_is_white))
803 goto force_cindent;
804 if (can_cindent && in_cinkeys(c, '*', line_is_white)
805 && stop_arrow() == OK)
806 do_c_expr_indent();
807 }
808#endif
809
810#ifdef FEAT_RIGHTLEFT
811 if (curwin->w_p_rl)
812 switch (c)
813 {
814 case K_LEFT: c = K_RIGHT; break;
815 case K_S_LEFT: c = K_S_RIGHT; break;
816 case K_C_LEFT: c = K_C_RIGHT; break;
817 case K_RIGHT: c = K_LEFT; break;
818 case K_S_RIGHT: c = K_S_LEFT; break;
819 case K_C_RIGHT: c = K_C_LEFT; break;
820 }
821#endif
822
823#ifdef FEAT_VISUAL
824 /*
825 * If 'keymodel' contains "startsel", may start selection. If it
826 * does, a CTRL-O and c will be stuffed, we need to get these
827 * characters.
828 */
829 if (ins_start_select(c))
830 continue;
831#endif
832
833 /*
834 * The big switch to handle a character in insert mode.
835 */
836 switch (c)
837 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000838 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (echeck_abbr(ESC + ABBR_OFF))
840 break;
841 /*FALLTHROUGH*/
842
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000843 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844#ifdef FEAT_CMDWIN
845 if (c == Ctrl_C && cmdwin_type != 0)
846 {
847 /* Close the cmdline window. */
848 cmdwin_result = K_IGNORE;
849 got_int = FALSE; /* don't stop executing autocommands et al. */
850 goto doESCkey;
851 }
852#endif
853
854#ifdef UNIX
855do_intr:
856#endif
857 /* when 'insertmode' set, and not halfway a mapping, don't leave
858 * Insert mode */
859 if (goto_im())
860 {
861 if (got_int)
862 {
863 (void)vgetc(); /* flush all buffers */
864 got_int = FALSE;
865 }
866 else
867 vim_beep();
868 break;
869 }
870doESCkey:
871 /*
872 * This is the ONLY return from edit()!
873 */
874 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
875 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000876 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 o_lnum = curwin->w_cursor.lnum;
878
Bram Moolenaar488c6512005-08-11 20:09:58 +0000879 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000880 {
881#ifdef FEAT_AUTOCMD
882 if (cmdchar != 'r' && cmdchar != 'v')
883 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
884 FALSE, curbuf);
885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 continue;
889
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000890 case Ctrl_Z: /* suspend when 'insertmode' set */
891 if (!p_im)
892 goto normalchar; /* insert CTRL-Z as normal char */
893 stuffReadbuff((char_u *)":st\r");
894 c = Ctrl_O;
895 /*FALLTHROUGH*/
896
897 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000898#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000899 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000900 goto docomplete;
901#endif
902 if (echeck_abbr(Ctrl_O + ABBR_OFF))
903 break;
904 ins_ctrl_o();
905 count = 0;
906 goto doESCkey;
907
Bram Moolenaar572cb562005-08-05 21:35:02 +0000908 case K_INS: /* toggle insert/replace mode */
909 case K_KINS:
910 ins_insert(replaceState);
911 break;
912
913 case K_SELECT: /* end of Select mode mapping - ignore */
914 break;
915
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000916#ifdef FEAT_SNIFF
917 case K_SNIFF: /* Sniff command received */
918 stuffcharReadbuff(K_SNIFF);
919 goto doESCkey;
920#endif
921
922 case K_HELP: /* Help key works like <ESC> <Help> */
923 case K_F1:
924 case K_XF1:
925 stuffcharReadbuff(K_HELP);
926 if (p_im)
927 need_start_insertmode = TRUE;
928 goto doESCkey;
929
930#ifdef FEAT_NETBEANS_INTG
931 case K_F21: /* NetBeans command */
932 ++no_mapping; /* don't map the next key hits */
933 i = safe_vgetc();
934 --no_mapping;
935 netbeans_keycommand(i);
936 break;
937#endif
938
939 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 case NUL:
941 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000942 /* For ^@ the trailing ESC will end the insert, unless there is an
943 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
945 && c != Ctrl_A && !p_im)
946 goto doESCkey; /* quit insert mode */
947 inserted_space = FALSE;
948 break;
949
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000950 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 ins_reg();
952 auto_format(FALSE, TRUE);
953 inserted_space = FALSE;
954 break;
955
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000956 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 ins_ctrl_g();
958 break;
959
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000960 case Ctrl_HAT: /* switch input mode and/or langmap */
961 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 break;
963
964#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (!p_ari)
967 goto normalchar;
968 ins_ctrl_();
969 break;
970#endif
971
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000972 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
974 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
975 goto docomplete;
976#endif
977 /* FALLTHROUGH */
978
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000979 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980# ifdef FEAT_INS_EXPAND
981 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 if (has_compl_option(FALSE))
984 goto docomplete;
985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987# endif
988 ins_shift(c, lastc);
989 auto_format(FALSE, TRUE);
990 inserted_space = FALSE;
991 break;
992
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000993 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 case K_KDEL:
995 ins_del();
996 auto_format(FALSE, TRUE);
997 break;
998
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 case Ctrl_H:
1001 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1002 auto_format(FALSE, TRUE);
1003 break;
1004
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001005 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1007 auto_format(FALSE, TRUE);
1008 break;
1009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001011# ifdef FEAT_COMPL_FUNC
1012 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001013 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001014 goto docomplete;
1015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1017 auto_format(FALSE, TRUE);
1018 inserted_space = FALSE;
1019 break;
1020
1021#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 case K_LEFTMOUSE_NM:
1024 case K_LEFTDRAG:
1025 case K_LEFTRELEASE:
1026 case K_LEFTRELEASE_NM:
1027 case K_MIDDLEMOUSE:
1028 case K_MIDDLEDRAG:
1029 case K_MIDDLERELEASE:
1030 case K_RIGHTMOUSE:
1031 case K_RIGHTDRAG:
1032 case K_RIGHTRELEASE:
1033 case K_X1MOUSE:
1034 case K_X1DRAG:
1035 case K_X1RELEASE:
1036 case K_X2MOUSE:
1037 case K_X2DRAG:
1038 case K_X2RELEASE:
1039 ins_mouse(c);
1040 break;
1041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 ins_mousescroll(FALSE);
1044 break;
1045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 ins_mousescroll(TRUE);
1048 break;
1049#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001050#ifdef FEAT_GUI_TABLINE
1051 case K_TABLINE:
1052 case K_TABMENU:
1053 ins_tabline(c);
1054 break;
1055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 break;
1059
Bram Moolenaar754b5602006-02-09 23:53:20 +00001060#ifdef FEAT_AUTOCMD
1061 case K_CURSORHOLD: /* Didn't type something for a while. */
1062 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1063 did_cursorhold = TRUE;
1064 break;
1065#endif
1066
Bram Moolenaar4770d092006-01-12 23:22:24 +00001067#ifdef FEAT_GUI_W32
1068 /* On Win32 ignore <M-F4>, we get it when closing the window was
1069 * cancelled. */
1070 case K_F4:
1071 if (mod_mask != MOD_MASK_ALT)
1072 goto normalchar;
1073 break;
1074#endif
1075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#ifdef FEAT_GUI
1077 case K_VER_SCROLLBAR:
1078 ins_scroll();
1079 break;
1080
1081 case K_HOR_SCROLLBAR:
1082 ins_horscroll();
1083 break;
1084#endif
1085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 case K_S_HOME:
1089 case K_C_HOME:
1090 ins_home(c);
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 case K_S_END:
1096 case K_C_END:
1097 ins_end(c);
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001101 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1102 ins_s_left();
1103 else
1104 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 break;
1106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001107 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 case K_C_LEFT:
1109 ins_s_left();
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001113 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1114 ins_s_right();
1115 else
1116 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 break;
1118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 case K_C_RIGHT:
1121 ins_s_right();
1122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001125#ifdef FEAT_INS_EXPAND
1126 if (pum_visible())
1127 goto docomplete;
1128#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129 if (mod_mask & MOD_MASK_SHIFT)
1130 ins_pageup();
1131 else
1132 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 break;
1134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 case K_PAGEUP:
1137 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001138#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001139 if (pum_visible())
1140 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 ins_pageup();
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001146#ifdef FEAT_INS_EXPAND
1147 if (pum_visible())
1148 goto docomplete;
1149#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001150 if (mod_mask & MOD_MASK_SHIFT)
1151 ins_pagedown();
1152 else
1153 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 break;
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 case K_PAGEDOWN:
1158 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001159#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001160 if (pum_visible())
1161 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 ins_pagedown();
1164 break;
1165
1166#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001167 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 ins_drop();
1169 break;
1170#endif
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 c = TAB;
1174 /* FALLTHROUGH */
1175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001176 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1178 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1179 goto docomplete;
1180#endif
1181 inserted_space = FALSE;
1182 if (ins_tab())
1183 goto normalchar; /* insert TAB as a normal char */
1184 auto_format(FALSE, TRUE);
1185 break;
1186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001187 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 c = CAR;
1189 /* FALLTHROUGH */
1190 case CAR:
1191 case NL:
1192#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1193 /* In a quickfix window a <CR> jumps to the error under the
1194 * cursor. */
1195 if (bt_quickfix(curbuf) && c == CAR)
1196 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001197 if (curwin->w_llist_ref == NULL) /* quickfix window */
1198 do_cmdline_cmd((char_u *)".cc");
1199 else /* location list window */
1200 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 break;
1202 }
1203#endif
1204#ifdef FEAT_CMDWIN
1205 if (cmdwin_type != 0)
1206 {
1207 /* Execute the command in the cmdline window. */
1208 cmdwin_result = CAR;
1209 goto doESCkey;
1210 }
1211#endif
1212 if (ins_eol(c) && !p_im)
1213 goto doESCkey; /* out of memory */
1214 auto_format(FALSE, FALSE);
1215 inserted_space = FALSE;
1216 break;
1217
1218#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001219 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220# ifdef FEAT_INS_EXPAND
1221 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1222 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001223 if (has_compl_option(TRUE))
1224 goto docomplete;
1225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227# endif
1228# ifdef FEAT_DIGRAPHS
1229 c = ins_digraph();
1230 if (c == NUL)
1231 break;
1232# endif
1233 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
1236#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001237 case Ctrl_X: /* Enter CTRL-X mode */
1238 ins_ctrl_x();
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (ctrl_x_mode != CTRL_X_TAGS)
1243 goto normalchar;
1244 goto docomplete;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (ctrl_x_mode != CTRL_X_FILES)
1248 goto normalchar;
1249 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001250
1251 case 's': /* Spelling completion after ^X */
1252 case Ctrl_S:
1253 if (ctrl_x_mode != CTRL_X_SPELL)
1254 goto normalchar;
1255 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259#ifdef FEAT_INS_EXPAND
1260 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1261#endif
1262 {
1263 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1264 if (p_im)
1265 {
1266 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1267 break;
1268 goto doESCkey;
1269 }
1270 goto normalchar;
1271 }
1272#ifdef FEAT_INS_EXPAND
1273 /* FALLTHROUGH */
1274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001275 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 case Ctrl_N:
1277 /* if 'complete' is empty then plain ^P is no longer special,
1278 * but it is under other ^X modes */
1279 if (*curbuf->b_p_cpt == NUL
1280 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 goto normalchar;
1283
1284docomplete:
1285 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 break;
1288#endif /* FEAT_INS_EXPAND */
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case Ctrl_Y: /* copy from previous line or scroll down */
1291 case Ctrl_E: /* copy from next line or scroll up */
1292 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 break;
1294
1295 default:
1296#ifdef UNIX
1297 if (c == intr_char) /* special interrupt char */
1298 goto do_intr;
1299#endif
1300
1301 /*
1302 * Insert a nomal character.
1303 */
1304normalchar:
1305#ifdef FEAT_SMARTINDENT
1306 /* Try to perform smart-indenting. */
1307 ins_try_si(c);
1308#endif
1309
1310 if (c == ' ')
1311 {
1312 inserted_space = TRUE;
1313#ifdef FEAT_CINDENT
1314 if (inindent(0))
1315 can_cindent = FALSE;
1316#endif
1317 if (Insstart_blank_vcol == MAXCOL
1318 && curwin->w_cursor.lnum == Insstart.lnum)
1319 Insstart_blank_vcol = get_nolist_virtcol();
1320 }
1321
1322 if (vim_iswordc(c) || !echeck_abbr(
1323#ifdef FEAT_MBYTE
1324 /* Add ABBR_OFF for characters above 0x100, this is
1325 * what check_abbr() expects. */
1326 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1327#endif
1328 c))
1329 {
1330 insert_special(c, FALSE, FALSE);
1331#ifdef FEAT_RIGHTLEFT
1332 revins_legal++;
1333 revins_chars++;
1334#endif
1335 }
1336
1337 auto_format(FALSE, TRUE);
1338
1339#ifdef FEAT_FOLDING
1340 /* When inserting a character the cursor line must never be in a
1341 * closed fold. */
1342 foldOpenCursor();
1343#endif
1344 break;
1345 } /* end of switch (c) */
1346
1347 /* If the cursor was moved we didn't just insert a space */
1348 if (arrow_used)
1349 inserted_space = FALSE;
1350
1351#ifdef FEAT_CINDENT
1352 if (can_cindent && cindent_on()
1353# ifdef FEAT_INS_EXPAND
1354 && ctrl_x_mode == 0
1355# endif
1356 )
1357 {
1358force_cindent:
1359 /*
1360 * Indent now if a key was typed that is in 'cinkeys'.
1361 */
1362 if (in_cinkeys(c, ' ', line_is_white))
1363 {
1364 if (stop_arrow() == OK)
1365 /* re-indent the current line */
1366 do_c_expr_indent();
1367 }
1368 }
1369#endif /* FEAT_CINDENT */
1370
1371 } /* for (;;) */
1372 /* NOTREACHED */
1373}
1374
1375/*
1376 * Redraw for Insert mode.
1377 * This is postponed until getting the next character to make '$' in the 'cpo'
1378 * option work correctly.
1379 * Only redraw when there are no characters available. This speeds up
1380 * inserting sequences of characters (e.g., for CTRL-R).
1381 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001382/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001384ins_redraw(ready)
1385 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 if (!char_avail())
1388 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001389#ifdef FEAT_AUTOCMD
1390 /* Trigger CursorMoved if the cursor moved. */
1391 if (ready && has_cursormovedI()
1392 && !equalpos(last_cursormoved, curwin->w_cursor))
1393 {
1394 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1395 last_cursormoved = curwin->w_cursor;
1396 }
1397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (must_redraw)
1399 update_screen(0);
1400 else if (clear_cmdline || redraw_cmdline)
1401 showmode(); /* clear cmdline and show mode */
1402 showruler(FALSE);
1403 setcursor();
1404 emsg_on_display = FALSE; /* may remove error message now */
1405 }
1406}
1407
1408/*
1409 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1410 */
1411 static void
1412ins_ctrl_v()
1413{
1414 int c;
1415
1416 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001417 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 if (redrawing() && !char_avail())
1420 edit_putchar('^', TRUE);
1421 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1422
1423#ifdef FEAT_CMDL_INFO
1424 add_to_showcmd_c(Ctrl_V);
1425#endif
1426
1427 c = get_literal();
1428#ifdef FEAT_CMDL_INFO
1429 clear_showcmd();
1430#endif
1431 insert_special(c, FALSE, TRUE);
1432#ifdef FEAT_RIGHTLEFT
1433 revins_chars++;
1434 revins_legal++;
1435#endif
1436}
1437
1438/*
1439 * Put a character directly onto the screen. It's not stored in a buffer.
1440 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1441 */
1442static int pc_status;
1443#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1444#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1445#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1446#define PC_STATUS_SET 3 /* pc_bytes was filled */
1447#ifdef FEAT_MBYTE
1448static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1449#else
1450static char_u pc_bytes[2]; /* saved bytes */
1451#endif
1452static int pc_attr;
1453static int pc_row;
1454static int pc_col;
1455
1456 void
1457edit_putchar(c, highlight)
1458 int c;
1459 int highlight;
1460{
1461 int attr;
1462
1463 if (ScreenLines != NULL)
1464 {
1465 update_topline(); /* just in case w_topline isn't valid */
1466 validate_cursor();
1467 if (highlight)
1468 attr = hl_attr(HLF_8);
1469 else
1470 attr = 0;
1471 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1472 pc_col = W_WINCOL(curwin);
1473#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1474 pc_status = PC_STATUS_UNSET;
1475#endif
1476#ifdef FEAT_RIGHTLEFT
1477 if (curwin->w_p_rl)
1478 {
1479 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1480# ifdef FEAT_MBYTE
1481 if (has_mbyte)
1482 {
1483 int fix_col = mb_fix_col(pc_col, pc_row);
1484
1485 if (fix_col != pc_col)
1486 {
1487 screen_putchar(' ', pc_row, fix_col, attr);
1488 --curwin->w_wcol;
1489 pc_status = PC_STATUS_RIGHT;
1490 }
1491 }
1492# endif
1493 }
1494 else
1495#endif
1496 {
1497 pc_col += curwin->w_wcol;
1498#ifdef FEAT_MBYTE
1499 if (mb_lefthalve(pc_row, pc_col))
1500 pc_status = PC_STATUS_LEFT;
1501#endif
1502 }
1503
1504 /* save the character to be able to put it back */
1505#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1506 if (pc_status == PC_STATUS_UNSET)
1507#endif
1508 {
1509 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1510 pc_status = PC_STATUS_SET;
1511 }
1512 screen_putchar(c, pc_row, pc_col, attr);
1513 }
1514}
1515
1516/*
1517 * Undo the previous edit_putchar().
1518 */
1519 void
1520edit_unputchar()
1521{
1522 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1523 {
1524#if defined(FEAT_MBYTE)
1525 if (pc_status == PC_STATUS_RIGHT)
1526 ++curwin->w_wcol;
1527 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1528 redrawWinline(curwin->w_cursor.lnum, FALSE);
1529 else
1530#endif
1531 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1532 }
1533}
1534
1535/*
1536 * Called when p_dollar is set: display a '$' at the end of the changed text
1537 * Only works when cursor is in the line that changes.
1538 */
1539 void
1540display_dollar(col)
1541 colnr_T col;
1542{
1543 colnr_T save_col;
1544
1545 if (!redrawing())
1546 return;
1547
1548 cursor_off();
1549 save_col = curwin->w_cursor.col;
1550 curwin->w_cursor.col = col;
1551#ifdef FEAT_MBYTE
1552 if (has_mbyte)
1553 {
1554 char_u *p;
1555
1556 /* If on the last byte of a multi-byte move to the first byte. */
1557 p = ml_get_curline();
1558 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1559 }
1560#endif
1561 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1562 if (curwin->w_wcol < W_WIDTH(curwin))
1563 {
1564 edit_putchar('$', FALSE);
1565 dollar_vcol = curwin->w_virtcol;
1566 }
1567 curwin->w_cursor.col = save_col;
1568}
1569
1570/*
1571 * Call this function before moving the cursor from the normal insert position
1572 * in insert mode.
1573 */
1574 static void
1575undisplay_dollar()
1576{
1577 if (dollar_vcol)
1578 {
1579 dollar_vcol = 0;
1580 redrawWinline(curwin->w_cursor.lnum, FALSE);
1581 }
1582}
1583
1584/*
1585 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1586 * Keep the cursor on the same character.
1587 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1588 * type == INDENT_DEC decrease indent (for CTRL-D)
1589 * type == INDENT_SET set indent to "amount"
1590 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1591 */
1592 void
1593change_indent(type, amount, round, replaced)
1594 int type;
1595 int amount;
1596 int round;
1597 int replaced; /* replaced character, put on replace stack */
1598{
1599 int vcol;
1600 int last_vcol;
1601 int insstart_less; /* reduction for Insstart.col */
1602 int new_cursor_col;
1603 int i;
1604 char_u *ptr;
1605 int save_p_list;
1606 int start_col;
1607 colnr_T vc;
1608#ifdef FEAT_VREPLACE
1609 colnr_T orig_col = 0; /* init for GCC */
1610 char_u *new_line, *orig_line = NULL; /* init for GCC */
1611
1612 /* VREPLACE mode needs to know what the line was like before changing */
1613 if (State & VREPLACE_FLAG)
1614 {
1615 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1616 orig_col = curwin->w_cursor.col;
1617 }
1618#endif
1619
1620 /* for the following tricks we don't want list mode */
1621 save_p_list = curwin->w_p_list;
1622 curwin->w_p_list = FALSE;
1623 vc = getvcol_nolist(&curwin->w_cursor);
1624 vcol = vc;
1625
1626 /*
1627 * For Replace mode we need to fix the replace stack later, which is only
1628 * possible when the cursor is in the indent. Remember the number of
1629 * characters before the cursor if it's possible.
1630 */
1631 start_col = curwin->w_cursor.col;
1632
1633 /* determine offset from first non-blank */
1634 new_cursor_col = curwin->w_cursor.col;
1635 beginline(BL_WHITE);
1636 new_cursor_col -= curwin->w_cursor.col;
1637
1638 insstart_less = curwin->w_cursor.col;
1639
1640 /*
1641 * If the cursor is in the indent, compute how many screen columns the
1642 * cursor is to the left of the first non-blank.
1643 */
1644 if (new_cursor_col < 0)
1645 vcol = get_indent() - vcol;
1646
1647 if (new_cursor_col > 0) /* can't fix replace stack */
1648 start_col = -1;
1649
1650 /*
1651 * Set the new indent. The cursor will be put on the first non-blank.
1652 */
1653 if (type == INDENT_SET)
1654 (void)set_indent(amount, SIN_CHANGED);
1655 else
1656 {
1657#ifdef FEAT_VREPLACE
1658 int save_State = State;
1659
1660 /* Avoid being called recursively. */
1661 if (State & VREPLACE_FLAG)
1662 State = INSERT;
1663#endif
1664 shift_line(type == INDENT_DEC, round, 1);
1665#ifdef FEAT_VREPLACE
1666 State = save_State;
1667#endif
1668 }
1669 insstart_less -= curwin->w_cursor.col;
1670
1671 /*
1672 * Try to put cursor on same character.
1673 * If the cursor is at or after the first non-blank in the line,
1674 * compute the cursor column relative to the column of the first
1675 * non-blank character.
1676 * If we are not in insert mode, leave the cursor on the first non-blank.
1677 * If the cursor is before the first non-blank, position it relative
1678 * to the first non-blank, counted in screen columns.
1679 */
1680 if (new_cursor_col >= 0)
1681 {
1682 /*
1683 * When changing the indent while the cursor is touching it, reset
1684 * Insstart_col to 0.
1685 */
1686 if (new_cursor_col == 0)
1687 insstart_less = MAXCOL;
1688 new_cursor_col += curwin->w_cursor.col;
1689 }
1690 else if (!(State & INSERT))
1691 new_cursor_col = curwin->w_cursor.col;
1692 else
1693 {
1694 /*
1695 * Compute the screen column where the cursor should be.
1696 */
1697 vcol = get_indent() - vcol;
1698 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1699
1700 /*
1701 * Advance the cursor until we reach the right screen column.
1702 */
1703 vcol = last_vcol = 0;
1704 new_cursor_col = -1;
1705 ptr = ml_get_curline();
1706 while (vcol <= (int)curwin->w_virtcol)
1707 {
1708 last_vcol = vcol;
1709#ifdef FEAT_MBYTE
1710 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001711 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 else
1713#endif
1714 ++new_cursor_col;
1715 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1716 }
1717 vcol = last_vcol;
1718
1719 /*
1720 * May need to insert spaces to be able to position the cursor on
1721 * the right screen column.
1722 */
1723 if (vcol != (int)curwin->w_virtcol)
1724 {
1725 curwin->w_cursor.col = new_cursor_col;
1726 i = (int)curwin->w_virtcol - vcol;
1727 ptr = alloc(i + 1);
1728 if (ptr != NULL)
1729 {
1730 new_cursor_col += i;
1731 ptr[i] = NUL;
1732 while (--i >= 0)
1733 ptr[i] = ' ';
1734 ins_str(ptr);
1735 vim_free(ptr);
1736 }
1737 }
1738
1739 /*
1740 * When changing the indent while the cursor is in it, reset
1741 * Insstart_col to 0.
1742 */
1743 insstart_less = MAXCOL;
1744 }
1745
1746 curwin->w_p_list = save_p_list;
1747
1748 if (new_cursor_col <= 0)
1749 curwin->w_cursor.col = 0;
1750 else
1751 curwin->w_cursor.col = new_cursor_col;
1752 curwin->w_set_curswant = TRUE;
1753 changed_cline_bef_curs();
1754
1755 /*
1756 * May have to adjust the start of the insert.
1757 */
1758 if (State & INSERT)
1759 {
1760 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1761 {
1762 if ((int)Insstart.col <= insstart_less)
1763 Insstart.col = 0;
1764 else
1765 Insstart.col -= insstart_less;
1766 }
1767 if ((int)ai_col <= insstart_less)
1768 ai_col = 0;
1769 else
1770 ai_col -= insstart_less;
1771 }
1772
1773 /*
1774 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1775 * If the number of characters before the cursor decreased, need to pop a
1776 * few characters from the replace stack.
1777 * If the number of characters before the cursor increased, need to push a
1778 * few NULs onto the replace stack.
1779 */
1780 if (REPLACE_NORMAL(State) && start_col >= 0)
1781 {
1782 while (start_col > (int)curwin->w_cursor.col)
1783 {
1784 replace_join(0); /* remove a NUL from the replace stack */
1785 --start_col;
1786 }
1787 while (start_col < (int)curwin->w_cursor.col || replaced)
1788 {
1789 replace_push(NUL);
1790 if (replaced)
1791 {
1792 replace_push(replaced);
1793 replaced = NUL;
1794 }
1795 ++start_col;
1796 }
1797 }
1798
1799#ifdef FEAT_VREPLACE
1800 /*
1801 * For VREPLACE mode, we also have to fix the replace stack. In this case
1802 * it is always possible because we backspace over the whole line and then
1803 * put it back again the way we wanted it.
1804 */
1805 if (State & VREPLACE_FLAG)
1806 {
1807 /* If orig_line didn't allocate, just return. At least we did the job,
1808 * even if you can't backspace. */
1809 if (orig_line == NULL)
1810 return;
1811
1812 /* Save new line */
1813 new_line = vim_strsave(ml_get_curline());
1814 if (new_line == NULL)
1815 return;
1816
1817 /* We only put back the new line up to the cursor */
1818 new_line[curwin->w_cursor.col] = NUL;
1819
1820 /* Put back original line */
1821 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1822 curwin->w_cursor.col = orig_col;
1823
1824 /* Backspace from cursor to start of line */
1825 backspace_until_column(0);
1826
1827 /* Insert new stuff into line again */
1828 ins_bytes(new_line);
1829
1830 vim_free(new_line);
1831 }
1832#endif
1833}
1834
1835/*
1836 * Truncate the space at the end of a line. This is to be used only in an
1837 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1838 * modes.
1839 */
1840 void
1841truncate_spaces(line)
1842 char_u *line;
1843{
1844 int i;
1845
1846 /* find start of trailing white space */
1847 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1848 {
1849 if (State & REPLACE_FLAG)
1850 replace_join(0); /* remove a NUL from the replace stack */
1851 }
1852 line[i + 1] = NUL;
1853}
1854
1855#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1856 || defined(FEAT_COMMENTS) || defined(PROTO)
1857/*
1858 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1859 * modes correctly. May also be used when not in insert mode at all.
1860 */
1861 void
1862backspace_until_column(col)
1863 int col;
1864{
1865 while ((int)curwin->w_cursor.col > col)
1866 {
1867 curwin->w_cursor.col--;
1868 if (State & REPLACE_FLAG)
1869 replace_do_bs();
1870 else
1871 (void)del_char(FALSE);
1872 }
1873}
1874#endif
1875
1876#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1877/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001878 * CTRL-X pressed in Insert mode.
1879 */
1880 static void
1881ins_ctrl_x()
1882{
1883 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1884 * CTRL-V works like CTRL-N */
1885 if (ctrl_x_mode != CTRL_X_CMDLINE)
1886 {
1887 /* if the next ^X<> won't ADD nothing, then reset
1888 * compl_cont_status */
1889 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001890 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001891 else
1892 compl_cont_status = 0;
1893 /* We're not sure which CTRL-X mode it will be yet */
1894 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1895 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1896 edit_submode_pre = NULL;
1897 showmode();
1898 }
1899}
1900
1901/*
1902 * Return TRUE if the 'dict' or 'tsr' option can be used.
1903 */
1904 static int
1905has_compl_option(dict_opt)
1906 int dict_opt;
1907{
1908 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1909 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1910 {
1911 ctrl_x_mode = 0;
1912 edit_submode = NULL;
1913 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1914 : (char_u *)_("'thesaurus' option is empty"),
1915 hl_attr(HLF_E));
1916 if (emsg_silent == 0)
1917 {
1918 vim_beep();
1919 setcursor();
1920 out_flush();
1921 ui_delay(2000L, FALSE);
1922 }
1923 return FALSE;
1924 }
1925 return TRUE;
1926}
1927
1928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1930 * This depends on the current mode.
1931 */
1932 int
1933vim_is_ctrl_x_key(c)
1934 int c;
1935{
1936 /* Always allow ^R - let it's results then be checked */
1937 if (c == Ctrl_R)
1938 return TRUE;
1939
Bram Moolenaare3226be2005-12-18 22:10:00 +00001940 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001941 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001942 return TRUE;
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 switch (ctrl_x_mode)
1945 {
1946 case 0: /* Not in any CTRL-X mode */
1947 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1948 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001949 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1951 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1952 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001953 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1954 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 case CTRL_X_SCROLL:
1956 return (c == Ctrl_Y || c == Ctrl_E);
1957 case CTRL_X_WHOLE_LINE:
1958 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1959 case CTRL_X_FILES:
1960 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1961 case CTRL_X_DICTIONARY:
1962 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1963 case CTRL_X_THESAURUS:
1964 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1965 case CTRL_X_TAGS:
1966 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1967#ifdef FEAT_FIND_ID
1968 case CTRL_X_PATH_PATTERNS:
1969 return (c == Ctrl_P || c == Ctrl_N);
1970 case CTRL_X_PATH_DEFINES:
1971 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1972#endif
1973 case CTRL_X_CMDLINE:
1974 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1975 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001976#ifdef FEAT_COMPL_FUNC
1977 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001978 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001979 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001980 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001981#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001982 case CTRL_X_SPELL:
1983 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 EMSG(_(e_internal));
1986 return FALSE;
1987}
1988
1989/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001990 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 * case of the originally typed text is used, and the case of the completed
1992 * text is infered, ie this tries to work out what case you probably wanted
1993 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001994 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 */
1996 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001997ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 char_u *str;
1999 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002000 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 char_u *fname;
2002 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002003 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 int has_lower = FALSE;
2006 int was_letter = FALSE;
2007 int idx;
2008
2009 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2010 {
2011 /* Infer case of completed part -- webb */
2012 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002013 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
2015 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002016 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002018 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 has_lower = TRUE;
2021 if (isupper(IObuff[idx]))
2022 {
2023 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002024 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2026 break;
2027 }
2028 }
2029 }
2030
2031 /*
2032 * Rule 2: No lower case, 2nd consecutive letter converted to
2033 * upper case.
2034 */
2035 if (!has_lower)
2036 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002037 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002039 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 && islower(IObuff[idx]))
2041 {
2042 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002043 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2045 break;
2046 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002047 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 }
2050
2051 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002052 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002054 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002056 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057}
2058
2059/*
2060 * Add a match to the list of matches.
2061 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002062 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002063 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002065 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002066ins_compl_add(str, len, icase, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 char_u *str;
2068 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002069 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002071 char_u *extra; /* extra text for popup menu or NULL */
2072 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002073 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002075 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002076 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 ui_breakcheck();
2079 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002080 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (len < 0)
2082 len = (int)STRLEN(str);
2083
2084 /*
2085 * If the same match is already present, don't add it.
2086 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002087 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002089 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 do
2091 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002092 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002093 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002094 && match->cp_str[len] == NUL)
2095 return NOTDONE;
2096 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002097 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
2099
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002100 /* Remove any popup menu before changing the list of matches. */
2101 ins_compl_del_pum();
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /*
2104 * Allocate a new match structure.
2105 * Copy the values to the new match structure.
2106 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002107 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002109 return FAIL;
2110 match->cp_number = -1;
2111 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002113 match->cp_number = 0;
2114 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002116 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002121 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002124 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2126 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002127 if (fname != NULL
2128 && compl_curr_match
2129 && compl_curr_match->cp_fname != NULL
2130 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002131 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002132 else if (fname != NULL)
2133 {
2134 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002138 match->cp_fname = NULL;
2139 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 if (extra != NULL)
2141 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 /*
2144 * Link the new match structure in the list of matches.
2145 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002146 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 else if (dir == FORWARD)
2149 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002150 match->cp_next = compl_curr_match->cp_next;
2151 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 else /* BACKWARD */
2154 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002155 match->cp_next = compl_curr_match;
2156 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002158 if (match->cp_next)
2159 match->cp_next->cp_prev = match;
2160 if (match->cp_prev)
2161 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002163 compl_first_match = match;
2164 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002166 /*
2167 * Find the longest common string if still doing that.
2168 */
2169 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2170 ins_compl_longest_match(match);
2171
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 return OK;
2173}
2174
2175/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002176 * Return TRUE if "str[len]" matches with match->cp_str, considering
2177 * match->cp_icase.
2178 */
2179 static int
2180ins_compl_equal(match, str, len)
2181 compl_T *match;
2182 char_u *str;
2183 int len;
2184{
2185 if (match->cp_icase)
2186 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2187 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2188}
2189
2190/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002191 * Reduce the longest common string for match "match".
2192 */
2193 static void
2194ins_compl_longest_match(match)
2195 compl_T *match;
2196{
2197 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002198 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002199 int had_match;
2200
2201 if (compl_leader == NULL)
2202 /* First match, use it as a whole. */
2203 compl_leader = vim_strsave(match->cp_str);
2204 else
2205 {
2206 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002207 p = compl_leader;
2208 s = match->cp_str;
2209 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002210 {
2211#ifdef FEAT_MBYTE
2212 if (has_mbyte)
2213 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002214 c1 = mb_ptr2char(p);
2215 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002216 }
2217 else
2218#endif
2219 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002220 c1 = *p;
2221 c2 = *s;
2222 }
2223 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2224 : (c1 != c2))
2225 break;
2226#ifdef FEAT_MBYTE
2227 if (has_mbyte)
2228 {
2229 mb_ptr_adv(p);
2230 mb_ptr_adv(s);
2231 }
2232 else
2233#endif
2234 {
2235 ++p;
2236 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002237 }
2238 }
2239
2240 if (*p != NUL)
2241 {
2242 /* Leader was shortened, need to change the inserted text. */
2243 *p = NUL;
2244 had_match = (curwin->w_cursor.col > compl_col);
2245 ins_compl_delete();
2246 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2247 ins_redraw(FALSE);
2248
2249 /* When the match isn't there (to avoid matching itself) remove it
2250 * again after redrawing. */
2251 if (!had_match)
2252 ins_compl_delete();
2253 }
2254
2255 compl_used_match = FALSE;
2256 }
2257}
2258
2259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 * Add an array of matches to the list of matches.
2261 * Frees matches[].
2262 */
2263 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002264ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 int num_matches;
2266 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002267 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 int i;
2270 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002271 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaar572cb562005-08-05 21:35:02 +00002273 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002274 if ((add_r = ins_compl_add(matches[i], -1, icase,
2275 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002277 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 FreeWild(num_matches, matches);
2279}
2280
2281/* Make the completion list cyclic.
2282 * Return the number of matches (excluding the original).
2283 */
2284 static int
2285ins_compl_make_cyclic()
2286{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002287 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 int count = 0;
2289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002290 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 /*
2293 * Find the end of the list.
2294 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 match = compl_first_match;
2296 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002297 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002299 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ++count;
2301 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002302 match->cp_next = compl_first_match;
2303 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 return count;
2306}
2307
Bram Moolenaar9372a112005-12-06 19:59:18 +00002308/* "compl_match_array" points the currently displayed list of entries in the
2309 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002310static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002311static int compl_match_arraysize;
2312
2313/*
2314 * Update the screen and when there is any scrolling remove the popup menu.
2315 */
2316 static void
2317ins_compl_upd_pum()
2318{
2319 int h;
2320
2321 if (compl_match_array != NULL)
2322 {
2323 h = curwin->w_cline_height;
2324 update_screen(0);
2325 if (h != curwin->w_cline_height)
2326 ins_compl_del_pum();
2327 }
2328}
2329
2330/*
2331 * Remove any popup menu.
2332 */
2333 static void
2334ins_compl_del_pum()
2335{
2336 if (compl_match_array != NULL)
2337 {
2338 pum_undisplay();
2339 vim_free(compl_match_array);
2340 compl_match_array = NULL;
2341 }
2342}
2343
2344/*
2345 * Return TRUE if the popup menu should be displayed.
2346 */
2347 static int
2348pum_wanted()
2349{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002350 /* 'completeopt' must contain "menu" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002351 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002352 return FALSE;
2353
2354 /* The display looks bad on a B&W display. */
2355 if (t_colors < 8
2356#ifdef FEAT_GUI
2357 && !gui.in_use
2358#endif
2359 )
2360 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002361 return TRUE;
2362}
2363
2364/*
2365 * Return TRUE if there are two or more matches to be shown in the popup menu.
2366 */
2367 static int
2368pum_two_or_more()
2369{
2370 compl_T *compl;
2371 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002372
2373 /* Don't display the popup menu if there are no matches or there is only
2374 * one (ignoring the original text). */
2375 compl = compl_first_match;
2376 i = 0;
2377 do
2378 {
2379 if (compl == NULL
2380 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2381 break;
2382 compl = compl->cp_next;
2383 } while (compl != compl_first_match);
2384
2385 return (i >= 2);
2386}
2387
2388/*
2389 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002390 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002391 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002392 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002393ins_compl_show_pum()
2394{
2395 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002396 compl_T *shown_compl = NULL;
2397 int did_find_shown_match = FALSE;
2398 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002399 int i;
2400 int cur = -1;
2401 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002402 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002403
Bram Moolenaara6557602006-02-04 22:43:20 +00002404 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002405 return;
2406
2407 /* Update the screen before drawing the popup menu over it. */
2408 update_screen(0);
2409
2410 if (compl_match_array == NULL)
2411 {
2412 /* Need to build the popup menu list. */
2413 compl_match_arraysize = 0;
2414 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002415 if (compl_leader != NULL)
2416 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002417 do
2418 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002419 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2420 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002421 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002422 ++compl_match_arraysize;
2423 compl = compl->cp_next;
2424 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002425 if (compl_match_arraysize == 0)
2426 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002427 compl_match_array = (pumitem_T *)alloc_clear(
2428 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002429 * compl_match_arraysize));
2430 if (compl_match_array != NULL)
2431 {
2432 i = 0;
2433 compl = compl_first_match;
2434 do
2435 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002436 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2437 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002438 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002439 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002440 if (!shown_match_ok)
2441 {
2442 if (compl == compl_shown_match || did_find_shown_match)
2443 {
2444 /* This item is the shown match or this is the
2445 * first displayed item after the shown match. */
2446 compl_shown_match = compl;
2447 did_find_shown_match = TRUE;
2448 shown_match_ok = TRUE;
2449 }
2450 else
2451 /* Remember this displayed match for when the
2452 * shown match is just below it. */
2453 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002454 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002455 }
2456 compl_match_array[i].pum_text = compl->cp_str;
2457 if (compl->cp_extra != NULL)
2458 compl_match_array[i++].pum_extra = compl->cp_extra;
2459 else
2460 compl_match_array[i++].pum_extra = compl->cp_fname;
2461 }
2462
2463 if (compl == compl_shown_match)
2464 {
2465 did_find_shown_match = TRUE;
2466 if (!shown_match_ok && shown_compl != NULL)
2467 {
2468 /* The shown match isn't displayed, set it to the
2469 * previously displayed match. */
2470 compl_shown_match = shown_compl;
2471 shown_match_ok = TRUE;
2472 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002473 }
2474 compl = compl->cp_next;
2475 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002476
2477 if (!shown_match_ok) /* no displayed match at all */
2478 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002479 }
2480 }
2481 else
2482 {
2483 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002484 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002485 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002486 break;
2487 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002488 }
2489
2490 if (compl_match_array != NULL)
2491 {
2492 /* Compute the screen column of the start of the completed text.
2493 * Use the cursor to get all wrapping and other settings right. */
2494 col = curwin->w_cursor.col;
2495 curwin->w_cursor.col = compl_col;
2496 validate_cursor_col();
2497 pum_display(compl_match_array, compl_match_arraysize, cur,
2498 curwin->w_cline_row + W_WINROW(curwin),
2499 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002500 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002501 curwin->w_cursor.col = col;
2502 }
2503}
2504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#define DICT_FIRST (1) /* use just first element in "dict" */
2506#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508/*
2509 * Add any identifiers that match the given pattern to the list of
2510 * completions.
2511 */
2512 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002513ins_compl_dictionaries(dict, pat, flags, thesaurus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 char_u *dict;
2515 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002516 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 int thesaurus;
2518{
2519 char_u *ptr;
2520 char_u *buf;
2521 FILE *fp;
2522 regmatch_T regmatch;
2523 int add_r;
2524 char_u **files;
2525 int count;
2526 int i;
2527 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002528 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 buf = alloc(LSIZE);
2531 /* If 'infercase' is set, don't use 'smartcase' here */
2532 save_p_scs = p_scs;
2533 if (curbuf->b_p_inf)
2534 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002535
2536 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2537 * to only match at the start of a line. Otherwise just match the
2538 * pattern. */
2539 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2540 {
2541 i = STRLEN(pat) + 8;
2542 ptr = alloc(i);
2543 if (ptr == NULL)
2544 return;
2545 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2546 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2547 vim_free(ptr);
2548 }
2549 else
2550 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2553 regmatch.rm_ic = ignorecase(pat);
2554 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002555 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 /* copy one dictionary file name into buf */
2558 if (flags == DICT_EXACT)
2559 {
2560 count = 1;
2561 files = &dict;
2562 }
2563 else
2564 {
2565 /* Expand wildcards in the dictionary name, but do not allow
2566 * backticks (for security, the 'dict' option may have been set in
2567 * a modeline). */
2568 copy_option_part(&dict, buf, LSIZE, ",");
2569 if (vim_strchr(buf, '`') != NULL
2570 || expand_wildcards(1, &buf, &count, &files,
2571 EW_FILE|EW_SILENT) != OK)
2572 count = 0;
2573 }
2574
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002575 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2578 if (flags != DICT_EXACT)
2579 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002580 vim_snprintf((char *)IObuff, IOSIZE,
2581 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2583 }
2584
2585 if (fp != NULL)
2586 {
2587 /*
2588 * Read dictionary file line by line.
2589 * Check each line for a match.
2590 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002591 while (!got_int && !compl_interrupted
2592 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 ptr = buf;
2595 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2596 {
2597 ptr = regmatch.startp[0];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002598 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2599 ptr = find_line_end(ptr);
2600 else
2601 ptr = find_word_end(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 add_r = ins_compl_add_infercase(regmatch.startp[0],
2603 (int)(ptr - regmatch.startp[0]),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002604 p_ic, files[i], dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (thesaurus)
2606 {
2607 char_u *wstart;
2608
2609 /*
2610 * Add the other matches on the line
2611 */
2612 while (!got_int)
2613 {
2614 /* Find start of the next word. Skip white
2615 * space and punctuation. */
2616 ptr = find_word_start(ptr);
2617 if (*ptr == NUL || *ptr == NL)
2618 break;
2619 wstart = ptr;
2620
2621 /* Find end of the word and add it. */
2622#ifdef FEAT_MBYTE
2623 if (has_mbyte)
2624 /* Japanese words may have characters in
2625 * different classes, only separate words
2626 * with single-byte non-word characters. */
2627 while (*ptr != NUL)
2628 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002629 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631 if (l < 2 && !vim_iswordc(*ptr))
2632 break;
2633 ptr += l;
2634 }
2635 else
2636#endif
2637 ptr = find_word_end(ptr);
2638 add_r = ins_compl_add_infercase(wstart,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002639 (int)(ptr - wstart),
2640 p_ic, files[i], dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642 }
2643 if (add_r == OK)
2644 /* if dir was BACKWARD then honor it just once */
2645 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002646 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648 /* avoid expensive call to vim_regexec() when at end
2649 * of line */
2650 if (*ptr == '\n' || got_int)
2651 break;
2652 }
2653 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002654 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656 fclose(fp);
2657 }
2658 }
2659 if (flags != DICT_EXACT)
2660 FreeWild(count, files);
2661 if (flags)
2662 break;
2663 }
2664 p_scs = save_p_scs;
2665 vim_free(regmatch.regprog);
2666 vim_free(buf);
2667}
2668
2669/*
2670 * Find the start of the next word.
2671 * Returns a pointer to the first char of the word. Also stops at a NUL.
2672 */
2673 char_u *
2674find_word_start(ptr)
2675 char_u *ptr;
2676{
2677#ifdef FEAT_MBYTE
2678 if (has_mbyte)
2679 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002680 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 else
2682#endif
2683 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2684 ++ptr;
2685 return ptr;
2686}
2687
2688/*
2689 * Find the end of the word. Assumes it starts inside a word.
2690 * Returns a pointer to just after the word.
2691 */
2692 char_u *
2693find_word_end(ptr)
2694 char_u *ptr;
2695{
2696#ifdef FEAT_MBYTE
2697 int start_class;
2698
2699 if (has_mbyte)
2700 {
2701 start_class = mb_get_class(ptr);
2702 if (start_class > 1)
2703 while (*ptr != NUL)
2704 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002705 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (mb_get_class(ptr) != start_class)
2707 break;
2708 }
2709 }
2710 else
2711#endif
2712 while (vim_iswordc(*ptr))
2713 ++ptr;
2714 return ptr;
2715}
2716
2717/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002718 * Find the end of the line, omitting CR and NL at the end.
2719 * Returns a pointer to just after the line.
2720 */
2721 static char_u *
2722find_line_end(ptr)
2723 char_u *ptr;
2724{
2725 char_u *s;
2726
2727 s = ptr + STRLEN(ptr);
2728 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2729 --s;
2730 return s;
2731}
2732
2733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 * Free the list of completions
2735 */
2736 static void
2737ins_compl_free()
2738{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002739 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002741 vim_free(compl_pattern);
2742 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002743 vim_free(compl_leader);
2744 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002746 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002748
2749 ins_compl_del_pum();
2750 pum_clear();
2751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002752 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 do
2754 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002755 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002756 compl_curr_match = compl_curr_match->cp_next;
2757 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002759 if (match->cp_flags & FREE_FNAME)
2760 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002761 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002763 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2764 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767 static void
2768ins_compl_clear()
2769{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002770 compl_cont_status = 0;
2771 compl_started = FALSE;
2772 compl_matches = 0;
2773 vim_free(compl_pattern);
2774 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002775 vim_free(compl_leader);
2776 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 edit_submode_extra = NULL;
2778}
2779
2780/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002781 * Return TRUE when Insert completion is active.
2782 */
2783 int
2784ins_compl_active()
2785{
2786 return compl_started;
2787}
2788
2789/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002790 * Delete one character before the cursor and show the subset of the matches
2791 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002792 * Returns TRUE if the work is done and another char to be got from the user.
2793 */
2794 static int
2795ins_compl_bs()
2796{
2797 char_u *line;
2798 char_u *p;
2799
2800 if (curwin->w_cursor.col <= compl_col + compl_length)
2801 {
2802 /* Deleted more than what was used to find matches, need to look for
2803 * matches all over again. */
2804 ins_compl_free();
2805 compl_started = FALSE;
2806 compl_matches = 0;
2807 }
2808
2809 line = ml_get_curline();
2810 p = line + curwin->w_cursor.col;
2811 mb_ptr_back(line, p);
2812
2813 vim_free(compl_leader);
2814 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2815 if (compl_leader != NULL)
2816 {
2817 ins_compl_del_pum();
2818 ins_compl_delete();
2819 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2820
2821 if (!compl_started)
2822 {
2823 /* Matches were cleared, need to search for them now. */
2824 if (ins_complete(Ctrl_N) == FAIL)
2825 compl_cont_status = 0;
2826 else
2827 {
2828 /* Remove the completed word again. */
2829 ins_compl_delete();
2830 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2831 }
2832 }
2833
2834 /* Show the popup menu with a different set of matches. */
2835 ins_compl_show_pum();
2836 compl_used_match = FALSE;
2837
2838 return TRUE;
2839 }
2840 return FALSE;
2841}
2842
2843/*
2844 * Append one character to the match leader. May reduce the number of
2845 * matches.
2846 */
2847 static void
2848ins_compl_addleader(c)
2849 int c;
2850{
2851#ifdef FEAT_MBYTE
2852 int cc;
2853
2854 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2855 {
2856 char_u buf[MB_MAXBYTES + 1];
2857
2858 (*mb_char2bytes)(c, buf);
2859 buf[cc] = NUL;
2860 ins_char_bytes(buf, cc);
2861 }
2862 else
2863#endif
2864 ins_char(c);
2865
2866 vim_free(compl_leader);
2867 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2868 curwin->w_cursor.col - compl_col);
2869 if (compl_leader != NULL)
2870 {
2871 /* Show the popup menu with a different set of matches. */
2872 ins_compl_del_pum();
2873 ins_compl_show_pum();
2874 compl_used_match = FALSE;
2875 }
2876}
2877
2878/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002879 * Append one character to the match leader. May reduce the number of
2880 * matches.
2881 */
2882 static void
2883ins_compl_addfrommatch()
2884{
2885 char_u *p;
2886 int len = curwin->w_cursor.col - compl_col;
2887 int c;
2888
2889 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002890 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002891 return;
2892 p += len;
2893#ifdef FEAT_MBYTE
2894 c = mb_ptr2char(p);
2895#else
2896 c = *p;
2897#endif
2898 ins_compl_addleader(c);
2899}
2900
2901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002903 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002904 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002906 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907ins_compl_prep(c)
2908 int c;
2909{
2910 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 int temp;
2912 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002913 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 /* Forget any previous 'special' messages if this is actually
2916 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2917 */
2918 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2919 edit_submode_extra = NULL;
2920
2921 /* Ignore end of Select mode mapping */
2922 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002923 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002925 /* Set "compl_get_longest" when finding the first matches. */
2926 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
2927 || (ctrl_x_mode == 0 && !compl_started))
2928 {
2929 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
2930 compl_used_match = TRUE;
2931 }
2932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2934 {
2935 /*
2936 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2937 * it will be yet. Now we decide.
2938 */
2939 switch (c)
2940 {
2941 case Ctrl_E:
2942 case Ctrl_Y:
2943 ctrl_x_mode = CTRL_X_SCROLL;
2944 if (!(State & REPLACE_FLAG))
2945 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2946 else
2947 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2948 edit_submode_pre = NULL;
2949 showmode();
2950 break;
2951 case Ctrl_L:
2952 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2953 break;
2954 case Ctrl_F:
2955 ctrl_x_mode = CTRL_X_FILES;
2956 break;
2957 case Ctrl_K:
2958 ctrl_x_mode = CTRL_X_DICTIONARY;
2959 break;
2960 case Ctrl_R:
2961 /* Simply allow ^R to happen without affecting ^X mode */
2962 break;
2963 case Ctrl_T:
2964 ctrl_x_mode = CTRL_X_THESAURUS;
2965 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002966#ifdef FEAT_COMPL_FUNC
2967 case Ctrl_U:
2968 ctrl_x_mode = CTRL_X_FUNCTION;
2969 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002970 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002971 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002972 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002973#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002974 case 's':
2975 case Ctrl_S:
2976 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002977#ifdef FEAT_SYN_HL
2978 spell_back_to_badword();
2979#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 case Ctrl_RSB:
2982 ctrl_x_mode = CTRL_X_TAGS;
2983 break;
2984#ifdef FEAT_FIND_ID
2985 case Ctrl_I:
2986 case K_S_TAB:
2987 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2988 break;
2989 case Ctrl_D:
2990 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2991 break;
2992#endif
2993 case Ctrl_V:
2994 case Ctrl_Q:
2995 ctrl_x_mode = CTRL_X_CMDLINE;
2996 break;
2997 case Ctrl_P:
2998 case Ctrl_N:
2999 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3000 * just started ^X mode, or there were enough ^X's to cancel
3001 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3002 * do normal expansion when interrupting a different mode (say
3003 * ^X^F^X^P or ^P^X^X^P, see below)
3004 * nothing changes if interrupting mode 0, (eg, the flag
3005 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003006 if (!(compl_cont_status & CONT_INTRPT))
3007 compl_cont_status |= CONT_LOCAL;
3008 else if (compl_cont_mode != 0)
3009 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 /* FALLTHROUGH */
3011 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003012 /* If we have typed at least 2 ^X's... for modes != 0, we set
3013 * compl_cont_status = 0 (eg, as if we had just started ^X
3014 * mode).
3015 * For mode 0, we set "compl_cont_mode" to an impossible
3016 * value, in both cases ^X^X can be used to restart the same
3017 * mode (avoiding ADDING mode).
3018 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3019 * 'complete' and local ^P expansions respectively.
3020 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3021 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (c == Ctrl_X)
3023 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003024 if (compl_cont_mode != 0)
3025 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003027 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
3029 ctrl_x_mode = 0;
3030 edit_submode = NULL;
3031 showmode();
3032 break;
3033 }
3034 }
3035 else if (ctrl_x_mode != 0)
3036 {
3037 /* We're already in CTRL-X mode, do we stay in it? */
3038 if (!vim_is_ctrl_x_key(c))
3039 {
3040 if (ctrl_x_mode == CTRL_X_SCROLL)
3041 ctrl_x_mode = 0;
3042 else
3043 ctrl_x_mode = CTRL_X_FINISHED;
3044 edit_submode = NULL;
3045 }
3046 showmode();
3047 }
3048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003049 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
3051 /* Show error message from attempted keyword completion (probably
3052 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003053 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003055 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3056 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 || ctrl_x_mode == CTRL_X_FINISHED)
3058 {
3059 /* Get here when we have finished typing a sequence of ^N and
3060 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003061 * memory that was used, and make sure we can redo the insert. */
3062 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003064 char_u *p;
3065
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 /*
3067 * If any of the original typed text has been changed,
3068 * eg when ignorecase is set, we must add back-spaces to
3069 * the redo buffer. We add as few as necessary to delete
3070 * just the part of the original text that has changed.
3071 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003072 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003073 p = compl_orig_text;
3074 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003076 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 ++ptr;
3078 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003079 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003081 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083
3084#ifdef FEAT_CINDENT
3085 want_cindent = (can_cindent && cindent_on());
3086#endif
3087 /*
3088 * When completing whole lines: fix indent for 'cindent'.
3089 * Otherwise, break line if it's too long.
3090 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003091 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093#ifdef FEAT_CINDENT
3094 /* re-indent the current line */
3095 if (want_cindent)
3096 {
3097 do_c_expr_indent();
3098 want_cindent = FALSE; /* don't do it again */
3099 }
3100#endif
3101 }
3102 else
3103 {
3104 /* put the cursor on the last char, for 'tw' formatting */
3105 curwin->w_cursor.col--;
3106 if (stop_arrow() == OK)
3107 insertchar(NUL, 0, -1);
3108 curwin->w_cursor.col++;
3109 }
3110
3111 auto_format(FALSE, TRUE);
3112
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003113 /* if the popup menu is displayed hitting Enter means accepting
3114 * the selection without inserting anything. */
3115 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3116 retval = TRUE;
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003119 compl_started = FALSE;
3120 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 msg_clr_cmdline(); /* necessary for "noshowmode" */
3122 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (edit_submode != NULL)
3124 {
3125 edit_submode = NULL;
3126 showmode();
3127 }
3128
3129#ifdef FEAT_CINDENT
3130 /*
3131 * Indent now if a key was typed that is in 'cinkeys'.
3132 */
3133 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3134 do_c_expr_indent();
3135#endif
3136 }
3137 }
3138
3139 /* reset continue_* if we left expansion-mode, if we stay they'll be
3140 * (re)set properly in ins_complete() */
3141 if (!vim_is_ctrl_x_key(c))
3142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003143 compl_cont_status = 0;
3144 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003146
3147 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149
3150/*
3151 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3152 * (depending on flag) starting from buf and looking for a non-scanned
3153 * buffer (other than curbuf). curbuf is special, if it is called with
3154 * buf=curbuf then it has to be the first call for a given flag/expansion.
3155 *
3156 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3157 */
3158 static buf_T *
3159ins_compl_next_buf(buf, flag)
3160 buf_T *buf;
3161 int flag;
3162{
3163#ifdef FEAT_WINDOWS
3164 static win_T *wp;
3165#endif
3166
3167 if (flag == 'w') /* just windows */
3168 {
3169#ifdef FEAT_WINDOWS
3170 if (buf == curbuf) /* first call for this flag/expansion */
3171 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003172 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 && wp->w_buffer->b_scanned)
3174 ;
3175 buf = wp->w_buffer;
3176#else
3177 buf = curbuf;
3178#endif
3179 }
3180 else
3181 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3182 * (unlisted buffers)
3183 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003184 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 && ((flag == 'U'
3186 ? buf->b_p_bl
3187 : (!buf->b_p_bl
3188 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003189 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 ;
3191 return buf;
3192}
3193
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003194#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003195static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003196
3197/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003198 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003199 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003200 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003201 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003202 static void
3203expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003204 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003205 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003206{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003207 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003208 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003209 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003210 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003211 char_u *funcname;
3212 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003213 int dir = compl_direction;
3214 char_u *x;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003215 int icase;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003216
Bram Moolenaare344bea2005-09-01 20:46:49 +00003217 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3218 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003219 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003220
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003221 /* Call 'completefunc' to obtain the list of matches. */
3222 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003223 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003224
Bram Moolenaare344bea2005-09-01 20:46:49 +00003225 pos = curwin->w_cursor;
3226 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3227 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003228 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003229 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003230
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003231 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003232 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003233 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003234 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3235 {
3236 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3237 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003238 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
3239 FALSE) == NULL)
3240 icase = p_ic;
3241 else
3242 icase = get_dict_number(li->li_tv.vval.v_dict,
3243 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003244 }
3245 else
3246 {
3247 p = get_tv_string_chk(&li->li_tv);
3248 x = NULL;
3249 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003250 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003251 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003252 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003253 /* if dir was BACKWARD then honor it just once */
3254 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003255 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003256 else if (did_emsg)
3257 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003258 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003259
3260 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003261}
3262#endif /* FEAT_COMPL_FUNC */
3263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003264/*
3265 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003266 * The search starts at position "ini" in curbuf and in the direction
3267 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 * When "compl_started" is FALSE start at that position, otherwise continue
3269 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003270 * This may return before finding all the matches.
3271 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
3273 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003274ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 static pos_T first_match_pos;
3278 static pos_T last_match_pos;
3279 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003280 static int found_all = FALSE; /* Found all matches of a
3281 certain type. */
3282 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaar572cb562005-08-05 21:35:02 +00003284 pos_T *pos;
3285 char_u **matches;
3286 int save_p_scs;
3287 int save_p_ws;
3288 int save_p_ic;
3289 int i;
3290 int num_matches;
3291 int len;
3292 int found_new_match;
3293 int type = ctrl_x_mode;
3294 char_u *ptr;
3295 char_u *dict = NULL;
3296 int dict_f = 0;
3297 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003299 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
3301 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3302 ins_buf->b_scanned = 0;
3303 found_all = FALSE;
3304 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003305 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003306 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 last_match_pos = first_match_pos = *ini;
3308 }
3309
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003310 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003311 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3313 for (;;)
3314 {
3315 found_new_match = FAIL;
3316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003317 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 * or if found_all says this entry is done. For ^X^L only use the
3319 * entries from 'complete' that look in loaded buffers. */
3320 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003321 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
3323 found_all = FALSE;
3324 while (*e_cpt == ',' || *e_cpt == ' ')
3325 e_cpt++;
3326 if (*e_cpt == '.' && !curbuf->b_scanned)
3327 {
3328 ins_buf = curbuf;
3329 first_match_pos = *ini;
3330 /* So that ^N can match word immediately after cursor */
3331 if (ctrl_x_mode == 0)
3332 dec(&first_match_pos);
3333 last_match_pos = first_match_pos;
3334 type = 0;
3335 }
3336 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3337 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3338 {
3339 /* Scan a buffer, but not the current one. */
3340 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003342 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 first_match_pos.col = last_match_pos.col = 0;
3344 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3345 last_match_pos.lnum = 0;
3346 type = 0;
3347 }
3348 else /* unloaded buffer, scan like dictionary */
3349 {
3350 found_all = TRUE;
3351 if (ins_buf->b_fname == NULL)
3352 continue;
3353 type = CTRL_X_DICTIONARY;
3354 dict = ins_buf->b_fname;
3355 dict_f = DICT_EXACT;
3356 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003357 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 ins_buf->b_fname == NULL
3359 ? buf_spname(ins_buf)
3360 : ins_buf->b_sfname == NULL
3361 ? (char *)ins_buf->b_fname
3362 : (char *)ins_buf->b_sfname);
3363 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3364 }
3365 else if (*e_cpt == NUL)
3366 break;
3367 else
3368 {
3369 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3370 type = -1;
3371 else if (*e_cpt == 'k' || *e_cpt == 's')
3372 {
3373 if (*e_cpt == 'k')
3374 type = CTRL_X_DICTIONARY;
3375 else
3376 type = CTRL_X_THESAURUS;
3377 if (*++e_cpt != ',' && *e_cpt != NUL)
3378 {
3379 dict = e_cpt;
3380 dict_f = DICT_FIRST;
3381 }
3382 }
3383#ifdef FEAT_FIND_ID
3384 else if (*e_cpt == 'i')
3385 type = CTRL_X_PATH_PATTERNS;
3386 else if (*e_cpt == 'd')
3387 type = CTRL_X_PATH_DEFINES;
3388#endif
3389 else if (*e_cpt == ']' || *e_cpt == 't')
3390 {
3391 type = CTRL_X_TAGS;
3392 sprintf((char*)IObuff, _("Scanning tags."));
3393 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3394 }
3395 else
3396 type = -1;
3397
3398 /* in any case e_cpt is advanced to the next entry */
3399 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3400
3401 found_all = TRUE;
3402 if (type == -1)
3403 continue;
3404 }
3405 }
3406
3407 switch (type)
3408 {
3409 case -1:
3410 break;
3411#ifdef FEAT_FIND_ID
3412 case CTRL_X_PATH_PATTERNS:
3413 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003414 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003415 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003417 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3419 (linenr_T)1, (linenr_T)MAXLNUM);
3420 break;
3421#endif
3422
3423 case CTRL_X_DICTIONARY:
3424 case CTRL_X_THESAURUS:
3425 ins_compl_dictionaries(
3426 dict ? dict
3427 : (type == CTRL_X_THESAURUS
3428 ? (*curbuf->b_p_tsr == NUL
3429 ? p_tsr
3430 : curbuf->b_p_tsr)
3431 : (*curbuf->b_p_dict == NUL
3432 ? p_dict
3433 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003434 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3436 dict = NULL;
3437 break;
3438
3439 case CTRL_X_TAGS:
3440 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3441 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003442 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003445 * of matches is found when compl_pattern is empty */
3446 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3448 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3449 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3450 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003451 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 p_ic = save_p_ic;
3454 break;
3455
3456 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003457 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3459 {
3460
3461 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003462 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003463 ins_compl_add_matches(num_matches, matches,
3464#ifdef CASE_INSENSITIVE_FILENAME
3465 TRUE
3466#else
3467 FALSE
3468#endif
3469 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471 break;
3472
3473 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003474 if (expand_cmdline(&compl_xp, compl_pattern,
3475 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003477 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 break;
3479
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003480#ifdef FEAT_COMPL_FUNC
3481 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003482 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003483 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003484 break;
3485#endif
3486
Bram Moolenaar488c6512005-08-11 20:09:58 +00003487 case CTRL_X_SPELL:
3488#ifdef FEAT_SYN_HL
3489 num_matches = expand_spelling(first_match_pos.lnum,
3490 first_match_pos.col, compl_pattern, &matches);
3491 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003492 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003493#endif
3494 break;
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 default: /* normal ^P/^N and ^X^L */
3497 /*
3498 * If 'infercase' is set, don't use 'smartcase' here
3499 */
3500 save_p_scs = p_scs;
3501 if (ins_buf->b_p_inf)
3502 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 /* buffers other than curbuf are scanned from the beginning or the
3505 * end but never from the middle, thus setting nowrapscan in this
3506 * buffers is a good idea, on the other hand, we always set
3507 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3508 save_p_ws = p_ws;
3509 if (ins_buf != curbuf)
3510 p_ws = FALSE;
3511 else if (*e_cpt == '.')
3512 p_ws = TRUE;
3513 for (;;)
3514 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003515 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003517 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3518 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003522 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003524 found_new_match = searchit(NULL, ins_buf, pos,
3525 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003526 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003527 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003530 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003531 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 first_match_pos = *pos;
3533 last_match_pos = *pos;
3534 }
3535 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003536 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 found_new_match = FAIL;
3538 if (found_new_match == FAIL)
3539 {
3540 if (ins_buf == curbuf)
3541 found_all = TRUE;
3542 break;
3543 }
3544
3545 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003546 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 && ini->lnum == pos->lnum
3548 && ini->col == pos->col)
3549 continue;
3550 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3551 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3556 continue;
3557 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3558 if (!p_paste)
3559 ptr = skipwhite(ptr);
3560 }
3561 len = (int)STRLEN(ptr);
3562 }
3563 else
3564 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 char_u *tmp_ptr = ptr;
3566
3567 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 /* Skip if already inside a word. */
3571 if (vim_iswordp(tmp_ptr))
3572 continue;
3573 /* Find start of next word. */
3574 tmp_ptr = find_word_start(tmp_ptr);
3575 }
3576 /* Find end of this word. */
3577 tmp_ptr = find_word_end(tmp_ptr);
3578 len = (int)(tmp_ptr - ptr);
3579
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003580 if ((compl_cont_status & CONT_ADDING)
3581 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3584 {
3585 /* Try next line, if any. the new word will be
3586 * "join" as if the normal command "J" was used.
3587 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * works -- Acevedo */
3590 STRNCPY(IObuff, ptr, len);
3591 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3592 tmp_ptr = ptr = skipwhite(ptr);
3593 /* Find start of next word. */
3594 tmp_ptr = find_word_start(tmp_ptr);
3595 /* Find end of next word. */
3596 tmp_ptr = find_word_end(tmp_ptr);
3597 if (tmp_ptr > ptr)
3598 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003599 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003601 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 IObuff[len++] = ' ';
3603 /* IObuf =~ "\k.* ", thus len >= 2 */
3604 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003605 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 || (vim_strchr(p_cpo, CPO_JOINSP)
3607 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003608 && (IObuff[len - 2] == '?'
3609 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 IObuff[len++] = ' ';
3611 }
3612 /* copy as much as posible of the new word */
3613 if (tmp_ptr - ptr >= IOSIZE - len)
3614 tmp_ptr = ptr + IOSIZE - len - 1;
3615 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3616 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003617 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 IObuff[len] = NUL;
3620 ptr = IObuff;
3621 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 continue;
3624 }
3625 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003626 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003627 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003628 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 found_new_match = OK;
3631 break;
3632 }
3633 }
3634 p_scs = save_p_scs;
3635 p_ws = save_p_ws;
3636 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003637
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638 /* check if compl_curr_match has changed, (e.g. other type of
3639 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003640 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 found_new_match = OK;
3642
3643 /* break the loop for specialized modes (use 'complete' just for the
3644 * generic ctrl_x_mode == 0) or when we've found a new match */
3645 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003646 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003647 {
3648 if (got_int)
3649 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003650 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003651 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003652 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003653
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003654 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3655 || compl_interrupted)
3656 break;
3657 compl_started = TRUE;
3658 }
3659 else
3660 {
3661 /* Mark a buffer scanned when it has been scanned completely */
3662 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3663 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003665 compl_started = FALSE;
3666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003668 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3671 && *e_cpt == NUL) /* Got to end of 'complete' */
3672 found_new_match = FAIL;
3673
3674 i = -1; /* total of matches, unknown */
3675 if (found_new_match == FAIL
3676 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3677 i = ins_compl_make_cyclic();
3678
3679 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003680 * just been made cyclic then we have to move compl_curr_match to the next
3681 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003682 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003683 if (compl_curr_match == NULL)
3684 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 return i;
3686}
3687
3688/* Delete the old text being completed. */
3689 static void
3690ins_compl_delete()
3691{
3692 int i;
3693
3694 /*
3695 * In insert mode: Delete the typed part.
3696 * In replace mode: Put the old characters back, if any.
3697 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 backspace_until_column(i);
3700 changed_cline_bef_curs();
3701}
3702
3703/* Insert the new text being completed. */
3704 static void
3705ins_compl_insert()
3706{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003707 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003708 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3709 compl_used_match = FALSE;
3710 else
3711 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712}
3713
3714/*
3715 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003716 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3717 * get more completions. If it is FALSE, then we just do nothing when there
3718 * are no more completions in a given direction. The latter case is used when
3719 * we are still in the middle of finding completions, to allow browsing
3720 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 * Return the total number of matches, or -1 if still unknown -- webb.
3722 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3724 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 *
3726 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003727 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3728 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 */
3730 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003731ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003733 int count; /* repeat completion this many times; should
3734 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003735 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 int num_matches = -1;
3738 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003739 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003740 compl_T *found_compl = NULL;
3741 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003743 if (compl_leader != NULL
3744 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003746 /* Set "compl_shown_match" to the actually shown match, it may differ
3747 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003748 while (!ins_compl_equal(compl_shown_match,
3749 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003750 && compl_shown_match->cp_next != NULL
3751 && compl_shown_match->cp_next != compl_first_match)
3752 compl_shown_match = compl_shown_match->cp_next;
3753 }
3754
3755 if (allow_get_expansion && insert_match
3756 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 /* Delete old text to be replaced */
3758 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003759
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003760 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003761
3762 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3763 * around. */
3764 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003766 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003768 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003769 found_end = (compl_first_match != NULL
3770 && (compl_shown_match->cp_next == compl_first_match
3771 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003772 }
3773 else if (compl_shows_dir == BACKWARD
3774 && compl_shown_match->cp_prev != NULL)
3775 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003776 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003777 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003778 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003781 {
3782 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003783 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003784 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003785
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003786 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003787 if (compl_pending && compl_direction == compl_shows_dir)
3788 compl_shown_match = compl_curr_match;
3789 found_end = FALSE;
3790 }
3791 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3792 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003793 && !ins_compl_equal(compl_shown_match,
3794 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003795 ++todo;
3796 else
3797 /* Remember a matching item. */
3798 found_compl = compl_shown_match;
3799
3800 /* Stop at the end of the list when we found a usable match. */
3801 if (found_end)
3802 {
3803 if (found_compl != NULL)
3804 {
3805 compl_shown_match = found_compl;
3806 break;
3807 }
3808 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003812 /* Insert the text of the new completion, or the compl_leader. */
3813 if (insert_match)
3814 {
3815 if (!compl_get_longest || compl_used_match)
3816 ins_compl_insert();
3817 else
3818 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3819 }
3820 else
3821 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 if (!allow_get_expansion)
3824 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003825 /* may undisplay the popup menu first */
3826 ins_compl_upd_pum();
3827
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003828 /* redraw to show the user what was inserted */
3829 update_screen(0);
3830
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003831 /* display the updated popup menu */
3832 ins_compl_show_pum();
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 /* Delete old text to be replaced, since we're still searching and
3835 * don't want to match ourselves! */
3836 ins_compl_delete();
3837 }
3838
3839 /*
3840 * Show the file name for the match (if any)
3841 * Truncate the file name to avoid a wait for return.
3842 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003843 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
3845 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003846 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (i <= 0)
3848 i = 0;
3849 else
3850 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003851 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 msg(IObuff);
3853 redraw_cmdline = FALSE; /* don't overwrite! */
3854 }
3855
3856 return num_matches;
3857}
3858
3859/*
3860 * Call this while finding completions, to check whether the user has hit a key
3861 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003862 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003864 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
3866 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003867ins_compl_check_keys(frequency)
3868 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
3870 static int count = 0;
3871
3872 int c;
3873
3874 /* Don't check when reading keys from a script. That would break the test
3875 * scripts */
3876 if (using_script())
3877 return;
3878
3879 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003880 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return;
3882 count = 0;
3883
3884 ++no_mapping;
3885 c = vpeekc_any();
3886 --no_mapping;
3887 if (c != NUL)
3888 {
3889 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3890 {
3891 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003892 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003893 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3894 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003900 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003901}
3902
3903/*
3904 * Decide the direction of Insert mode complete from the key typed.
3905 * Returns BACKWARD or FORWARD.
3906 */
3907 static int
3908ins_compl_key2dir(c)
3909 int c;
3910{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003911 if (c == Ctrl_P || c == Ctrl_L
3912 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3913 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003914 return BACKWARD;
3915 return FORWARD;
3916}
3917
3918/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003919 * Return TRUE for keys that are used for completion only when the popup menu
3920 * is visible.
3921 */
3922 static int
3923ins_compl_pum_key(c)
3924 int c;
3925{
3926 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003927 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3928 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929}
3930
3931/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003932 * Decide the number of completions to move forward.
3933 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3934 */
3935 static int
3936ins_compl_key2count(c)
3937 int c;
3938{
3939 int h;
3940
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003941 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003942 {
3943 h = pum_get_height();
3944 if (h > 3)
3945 h -= 2; /* keep some context */
3946 return h;
3947 }
3948 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950
3951/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003952 * Return TRUE if completion with "c" should insert the match, FALSE if only
3953 * to change the currently selected completion.
3954 */
3955 static int
3956ins_compl_use_match(c)
3957 int c;
3958{
3959 switch (c)
3960 {
3961 case K_UP:
3962 case K_DOWN:
3963 case K_PAGEDOWN:
3964 case K_KPAGEDOWN:
3965 case K_S_DOWN:
3966 case K_PAGEUP:
3967 case K_KPAGEUP:
3968 case K_S_UP:
3969 return FALSE;
3970 }
3971 return TRUE;
3972}
3973
3974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * Do Insert mode completion.
3976 * Called when character "c" was typed, which has a meaning for completion.
3977 * Returns OK if completion was done, FAIL if something failed (out of mem).
3978 */
3979 static int
3980ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003983 char_u *line;
3984 int startcol = 0; /* column where searched text starts */
3985 colnr_T curs_col; /* cursor column */
3986 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Bram Moolenaare3226be2005-12-18 22:10:00 +00003988 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003989 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
3991 /* First time we hit ^N or ^P (in a row, I mean) */
3992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 did_ai = FALSE;
3994#ifdef FEAT_SMARTINDENT
3995 did_si = FALSE;
3996 can_si = FALSE;
3997 can_si_back = FALSE;
3998#endif
3999 if (stop_arrow() == FAIL)
4000 return FAIL;
4001
4002 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004003 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004006 * "compl_startpos" to the cursor as a pattern to add a new word
4007 * instead of expand the one before the cursor, in word-wise if
4008 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 * is not in the same line as the cursor then fix it (the line has
4010 * been split because it was longer than 'tw'). if SOL is set then
4011 * skip the previous pattern, a word at the beginning of the line has
4012 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004013 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4014 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 /*
4017 * it is a continued search
4018 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004019 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4021 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4022 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004025 /* line (probably) wrapped, set compl_startpos to the
4026 * first non_blank in the line, if it is not a wordchar
4027 * include it to get a better pattern, but then we don't
4028 * want the "\\<" prefix, check it bellow */
4029 compl_col = (colnr_T)(skipwhite(line) - line);
4030 compl_startpos.col = compl_col;
4031 compl_startpos.lnum = curwin->w_cursor.lnum;
4032 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 else
4035 {
4036 /* S_IPOS was set when we inserted a word that was at the
4037 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004038 * mode but first we need to redefine compl_startpos */
4039 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041 compl_cont_status |= CONT_SOL;
4042 compl_startpos.col = (colnr_T)(skipwhite(
4043 line + compl_length
4044 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004048 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004049 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 * have enough space? just being paranoic */
4051#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004052 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 compl_cont_status &= ~CONT_SOL;
4055 compl_length = (IOSIZE - MIN_SPACE);
4056 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4059 if (compl_length < 1)
4060 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004063 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 compl_cont_status = 0;
4075 compl_cont_status |= CONT_N_ADDS;
4076 compl_startpos = curwin->w_cursor;
4077 startcol = (int)curs_col;
4078 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080
4081 /* Work out completion pattern and original text -- webb */
4082 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4083 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004084 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4086 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004089 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004091 compl_col += ++startcol;
4092 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 compl_pattern = str_foldcase(line + compl_col,
4096 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 compl_pattern = vim_strnsave(line + compl_col,
4099 compl_length);
4100 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return FAIL;
4102 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004103 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
4105 char_u *prefix = (char_u *)"\\<";
4106
4107 /* we need 3 extra chars, 1 for the NUL and
4108 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004109 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4110 compl_length) + 3);
4111 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113 if (!vim_iswordp(line + compl_col)
4114 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 && (
4116#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120#endif
4121 )))
4122 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004123 STRCPY((char *)compl_pattern, prefix);
4124 (void)quote_meta(compl_pattern + STRLEN(prefix),
4125 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#endif
4133 )
4134 {
4135 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4137 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 compl_col += curs_col;
4140 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 else
4143 {
4144#ifdef FEAT_MBYTE
4145 /* Search the point of change class of multibyte character
4146 * or not a word single byte character backward. */
4147 if (has_mbyte)
4148 {
4149 int base_class;
4150 int head_off;
4151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004152 startcol -= (*mb_head_off)(line, line + startcol);
4153 base_class = mb_get_class(line + startcol);
4154 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 head_off = (*mb_head_off)(line, line + startcol);
4157 if (base_class != mb_get_class(line + startcol
4158 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 }
4163 else
4164#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004167 compl_col += ++startcol;
4168 compl_length = (int)curs_col - startcol;
4169 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
4171 /* Only match word with at least two chars -- webb
4172 * there's no need to call quote_meta,
4173 * alloc(7) is enough -- Acevedo
4174 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004175 compl_pattern = alloc(7);
4176 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 STRCPY((char *)compl_pattern, "\\<");
4179 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4180 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 else
4183 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4185 compl_length) + 3);
4186 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 STRCPY((char *)compl_pattern, "\\<");
4189 (void)quote_meta(compl_pattern + 2, line + compl_col,
4190 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 }
4193 }
4194 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 compl_col = skipwhite(line) - line;
4197 compl_length = (int)curs_col - (int)compl_col;
4198 if (compl_length < 0) /* cursor in indent: empty pattern */
4199 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 compl_pattern = str_foldcase(line + compl_col, compl_length,
4202 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4205 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207 }
4208 else if (ctrl_x_mode == CTRL_X_FILES)
4209 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212 compl_col += ++startcol;
4213 compl_length = (int)curs_col - startcol;
4214 compl_pattern = addstar(line + compl_col, compl_length,
4215 EXPAND_FILES);
4216 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4220 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 compl_pattern = vim_strnsave(line, curs_col);
4222 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004224 set_cmd_context(&compl_xp, compl_pattern,
4225 (int)STRLEN(compl_pattern), curs_col);
4226 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4227 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4230 compl_col = startcol;
4231 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004233 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004234 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004235#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004236 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004237 * Call user defined function 'completefunc' with "a:findstart"
4238 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004239 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004240 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004241 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004242 char_u *funcname;
4243 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004244
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004245 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004246 * string */
4247 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4248 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4249 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004250 {
4251 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4252 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004253 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004254 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004255
4256 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004257 args[1] = NULL;
4258 pos = curwin->w_cursor;
4259 col = call_func_retnr(funcname, 2, args, FALSE);
4260 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004261
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004262 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004263 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004264 compl_col = col;
4265 if ((colnr_T)compl_col > curs_col)
4266 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 /* Setup variables for completion. Need to obtain "line" again,
4269 * it may have become invalid. */
4270 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004271 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4273 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004274#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 return FAIL;
4276 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004277 else if (ctrl_x_mode == CTRL_X_SPELL)
4278 {
4279#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004280 if (spell_bad_len > 0)
4281 compl_col = curs_col - spell_bad_len;
4282 else
4283 compl_col = spell_word_start(startcol);
4284 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004285 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004286 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004287 compl_length = (int)curs_col - compl_col;
4288 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4289 if (compl_pattern == NULL)
4290#endif
4291 return FAIL;
4292 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 else
4294 {
4295 EMSG2(_(e_intern2), "ins_complete()");
4296 return FAIL;
4297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301 edit_submode_pre = (char_u *)_(" Adding");
4302 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4303 {
4304 /* Insert a new line, keep indentation but ignore 'comments' */
4305#ifdef FEAT_COMMENTS
4306 char_u *old = curbuf->b_p_com;
4307
4308 curbuf->b_p_com = (char_u *)"";
4309#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 compl_startpos.lnum = curwin->w_cursor.lnum;
4311 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 ins_eol('\r');
4313#ifdef FEAT_COMMENTS
4314 curbuf->b_p_com = old;
4315#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 compl_length = 0;
4317 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 }
4320 else
4321 {
4322 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326 if (compl_cont_status & CONT_LOCAL)
4327 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 else
4329 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 * "compl_orig_text" itself (not a copy) is added, it will be freed
4333 * when the list of matches is freed. */
4334 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4335 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004336 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 vim_free(compl_pattern);
4339 compl_pattern = NULL;
4340 vim_free(compl_orig_text);
4341 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 return FAIL;
4343 }
4344
4345 /* showmode might reset the internal line pointers, so it must
4346 * be called before line = ml_get(), or when this address is no
4347 * longer needed. -- Acevedo.
4348 */
4349 edit_submode_extra = (char_u *)_("-- Searching...");
4350 edit_submode_highl = HLF_COUNT;
4351 showmode();
4352 edit_submode_extra = NULL;
4353 out_flush();
4354 }
4355
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 compl_shown_match = compl_curr_match;
4357 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004360 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004362 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004364 /* may undisplay the popup menu */
4365 ins_compl_upd_pum();
4366
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 if (n > 1) /* all matches have been found */
4368 compl_matches = n;
4369 compl_curr_match = compl_shown_match;
4370 compl_direction = compl_shows_dir;
4371 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 /* eat the ESC to avoid leaving insert mode */
4374 if (got_int && !global_busy)
4375 {
4376 (void)vgetc();
4377 got_int = FALSE;
4378 }
4379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004381 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4384 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4386 edit_submode_highl = HLF_E;
4387 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4388 * because we couldn't expand anything at first place, but if we used
4389 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4390 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 if ( compl_length > 1
4392 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 || (ctrl_x_mode != 0
4394 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4395 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004396 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398
Bram Moolenaar572cb562005-08-05 21:35:02 +00004399 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 if (edit_submode_extra == NULL)
4405 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004406 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
4408 edit_submode_extra = (char_u *)_("Back at original");
4409 edit_submode_highl = HLF_W;
4410 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 edit_submode_extra = (char_u *)_("Word from other line");
4414 edit_submode_highl = HLF_COUNT;
4415 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004416 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 edit_submode_extra = (char_u *)_("The only match");
4419 edit_submode_highl = HLF_COUNT;
4420 }
4421 else
4422 {
4423 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004424 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004426 int number = 0;
4427 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 /* search backwards for the first valid (!= -1) number.
4432 * This should normally succeed already at the first loop
4433 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004434 for (match = compl_curr_match->cp_prev; match != NULL
4435 && match != compl_first_match;
4436 match = match->cp_prev)
4437 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004439 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441 }
4442 if (match != NULL)
4443 /* go up and assign all numbers which are not assigned
4444 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004445 for (match = match->cp_next;
4446 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004447 match = match->cp_next)
4448 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 else /* BACKWARD */
4451 {
4452 /* search forwards (upwards) for the first valid (!= -1)
4453 * number. This should normally succeed already at the
4454 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004455 for (match = compl_curr_match->cp_next; match != NULL
4456 && match != compl_first_match;
4457 match = match->cp_next)
4458 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004460 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 break;
4462 }
4463 if (match != NULL)
4464 /* go down and assign all numbers which are not
4465 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004466 for (match = match->cp_prev; match
4467 && match->cp_number == -1;
4468 match = match->cp_prev)
4469 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 }
4472
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004473 /* The match should always have a sequence number now, this is
4474 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004475 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 /* Space for 10 text chars. + 2x10-digit no.s */
4478 static char_u match_ref[31];
4479
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004482 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004485 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004486 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 edit_submode_extra = match_ref;
4488 edit_submode_highl = HLF_R;
4489 if (dollar_vcol)
4490 curs_columns(FALSE);
4491 }
4492 }
4493 }
4494
4495 /* Show a message about what (completion) mode we're in. */
4496 showmode();
4497 if (edit_submode_extra != NULL)
4498 {
4499 if (!p_smd)
4500 msg_attr(edit_submode_extra,
4501 edit_submode_highl < HLF_COUNT
4502 ? hl_attr(edit_submode_highl) : 0);
4503 }
4504 else
4505 msg_clr_cmdline(); /* necessary for "noshowmode" */
4506
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004507 ins_compl_show_pum();
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 return OK;
4510}
4511
4512/*
4513 * Looks in the first "len" chars. of "src" for search-metachars.
4514 * If dest is not NULL the chars. are copied there quoting (with
4515 * a backslash) the metachars, and dest would be NUL terminated.
4516 * Returns the length (needed) of dest
4517 */
4518 static int
4519quote_meta(dest, src, len)
4520 char_u *dest;
4521 char_u *src;
4522 int len;
4523{
4524 int m;
4525
4526 for (m = len; --len >= 0; src++)
4527 {
4528 switch (*src)
4529 {
4530 case '.':
4531 case '*':
4532 case '[':
4533 if (ctrl_x_mode == CTRL_X_DICTIONARY
4534 || ctrl_x_mode == CTRL_X_THESAURUS)
4535 break;
4536 case '~':
4537 if (!p_magic) /* quote these only if magic is set */
4538 break;
4539 case '\\':
4540 if (ctrl_x_mode == CTRL_X_DICTIONARY
4541 || ctrl_x_mode == CTRL_X_THESAURUS)
4542 break;
4543 case '^': /* currently it's not needed. */
4544 case '$':
4545 m++;
4546 if (dest != NULL)
4547 *dest++ = '\\';
4548 break;
4549 }
4550 if (dest != NULL)
4551 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004552# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 /* Copy remaining bytes of a multibyte character. */
4554 if (has_mbyte)
4555 {
4556 int i, mb_len;
4557
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004558 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (mb_len > 0 && len >= mb_len)
4560 for (i = 0; i < mb_len; ++i)
4561 {
4562 --len;
4563 ++src;
4564 if (dest != NULL)
4565 *dest++ = *src;
4566 }
4567 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 if (dest != NULL)
4571 *dest = NUL;
4572
4573 return m;
4574}
4575#endif /* FEAT_INS_EXPAND */
4576
4577/*
4578 * Next character is interpreted literally.
4579 * A one, two or three digit decimal number is interpreted as its byte value.
4580 * If one or two digits are entered, the next character is given to vungetc().
4581 * For Unicode a character > 255 may be returned.
4582 */
4583 int
4584get_literal()
4585{
4586 int cc;
4587 int nc;
4588 int i;
4589 int hex = FALSE;
4590 int octal = FALSE;
4591#ifdef FEAT_MBYTE
4592 int unicode = 0;
4593#endif
4594
4595 if (got_int)
4596 return Ctrl_C;
4597
4598#ifdef FEAT_GUI
4599 /*
4600 * In GUI there is no point inserting the internal code for a special key.
4601 * It is more useful to insert the string "<KEY>" instead. This would
4602 * probably be useful in a text window too, but it would not be
4603 * vi-compatible (maybe there should be an option for it?) -- webb
4604 */
4605 if (gui.in_use)
4606 ++allow_keys;
4607#endif
4608#ifdef USE_ON_FLY_SCROLL
4609 dont_scroll = TRUE; /* disallow scrolling here */
4610#endif
4611 ++no_mapping; /* don't map the next key hits */
4612 cc = 0;
4613 i = 0;
4614 for (;;)
4615 {
4616 do
4617 nc = safe_vgetc();
4618 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4619 || nc == K_HOR_SCROLLBAR);
4620#ifdef FEAT_CMDL_INFO
4621 if (!(State & CMDLINE)
4622# ifdef FEAT_MBYTE
4623 && MB_BYTE2LEN_CHECK(nc) == 1
4624# endif
4625 )
4626 add_to_showcmd(nc);
4627#endif
4628 if (nc == 'x' || nc == 'X')
4629 hex = TRUE;
4630 else if (nc == 'o' || nc == 'O')
4631 octal = TRUE;
4632#ifdef FEAT_MBYTE
4633 else if (nc == 'u' || nc == 'U')
4634 unicode = nc;
4635#endif
4636 else
4637 {
4638 if (hex
4639#ifdef FEAT_MBYTE
4640 || unicode != 0
4641#endif
4642 )
4643 {
4644 if (!vim_isxdigit(nc))
4645 break;
4646 cc = cc * 16 + hex2nr(nc);
4647 }
4648 else if (octal)
4649 {
4650 if (nc < '0' || nc > '7')
4651 break;
4652 cc = cc * 8 + nc - '0';
4653 }
4654 else
4655 {
4656 if (!VIM_ISDIGIT(nc))
4657 break;
4658 cc = cc * 10 + nc - '0';
4659 }
4660
4661 ++i;
4662 }
4663
4664 if (cc > 255
4665#ifdef FEAT_MBYTE
4666 && unicode == 0
4667#endif
4668 )
4669 cc = 255; /* limit range to 0-255 */
4670 nc = 0;
4671
4672 if (hex) /* hex: up to two chars */
4673 {
4674 if (i >= 2)
4675 break;
4676 }
4677#ifdef FEAT_MBYTE
4678 else if (unicode) /* Unicode: up to four or eight chars */
4679 {
4680 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4681 break;
4682 }
4683#endif
4684 else if (i >= 3) /* decimal or octal: up to three chars */
4685 break;
4686 }
4687 if (i == 0) /* no number entered */
4688 {
4689 if (nc == K_ZERO) /* NUL is stored as NL */
4690 {
4691 cc = '\n';
4692 nc = 0;
4693 }
4694 else
4695 {
4696 cc = nc;
4697 nc = 0;
4698 }
4699 }
4700
4701 if (cc == 0) /* NUL is stored as NL */
4702 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004703#ifdef FEAT_MBYTE
4704 if (enc_dbcs && (cc & 0xff) == 0)
4705 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4706 second byte will cause trouble! */
4707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 --no_mapping;
4710#ifdef FEAT_GUI
4711 if (gui.in_use)
4712 --allow_keys;
4713#endif
4714 if (nc)
4715 vungetc(nc);
4716 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4717 return cc;
4718}
4719
4720/*
4721 * Insert character, taking care of special keys and mod_mask
4722 */
4723 static void
4724insert_special(c, allow_modmask, ctrlv)
4725 int c;
4726 int allow_modmask;
4727 int ctrlv; /* c was typed after CTRL-V */
4728{
4729 char_u *p;
4730 int len;
4731
4732 /*
4733 * Special function key, translate into "<Key>". Up to the last '>' is
4734 * inserted with ins_str(), so as not to replace characters in replace
4735 * mode.
4736 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4737 * unless 'allow_modmask' is TRUE.
4738 */
4739#ifdef MACOS
4740 /* Command-key never produces a normal key */
4741 if (mod_mask & MOD_MASK_CMD)
4742 allow_modmask = TRUE;
4743#endif
4744 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4745 {
4746 p = get_special_key_name(c, mod_mask);
4747 len = (int)STRLEN(p);
4748 c = p[len - 1];
4749 if (len > 2)
4750 {
4751 if (stop_arrow() == FAIL)
4752 return;
4753 p[len - 1] = NUL;
4754 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004755 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 ctrlv = FALSE;
4757 }
4758 }
4759 if (stop_arrow() == OK)
4760 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4761}
4762
4763/*
4764 * Special characters in this context are those that need processing other
4765 * than the simple insertion that can be performed here. This includes ESC
4766 * which terminates the insert, and CR/NL which need special processing to
4767 * open up a new line. This routine tries to optimize insertions performed by
4768 * the "redo", "undo" or "put" commands, so it needs to know when it should
4769 * stop and defer processing to the "normal" mechanism.
4770 * '0' and '^' are special, because they can be followed by CTRL-D.
4771 */
4772#ifdef EBCDIC
4773# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4774#else
4775# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4776#endif
4777
4778#ifdef FEAT_MBYTE
4779# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4780#else
4781# define WHITECHAR(cc) vim_iswhite(cc)
4782#endif
4783
4784 void
4785insertchar(c, flags, second_indent)
4786 int c; /* character to insert or NUL */
4787 int flags; /* INSCHAR_FORMAT, etc. */
4788 int second_indent; /* indent for second line if >= 0 */
4789{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int textwidth;
4791#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4797 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 /*
4800 * Try to break the line in two or more pieces when:
4801 * - Always do this if we have been called to do formatting only.
4802 * - Always do this when 'formatoptions' has the 'a' flag and the line
4803 * ends in white space.
4804 * - Otherwise:
4805 * - Don't do this if inserting a blank
4806 * - Don't do this if an existing character is being replaced, unless
4807 * we're in VREPLACE mode.
4808 * - Do this if the cursor is not on the line where insert started
4809 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4810 * before the insert.
4811 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4812 * before 'textwidth'
4813 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004814 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 && ((flags & INSCHAR_FORMAT)
4816 || (!vim_iswhite(c)
4817 && !((State & REPLACE_FLAG)
4818#ifdef FEAT_VREPLACE
4819 && !(State & VREPLACE_FLAG)
4820#endif
4821 && *ml_get_cursor() != NUL)
4822 && (curwin->w_cursor.lnum != Insstart.lnum
4823 || ((!has_format_option(FO_INS_LONG)
4824 || Insstart_textlen <= (colnr_T)textwidth)
4825 && (!fo_ins_blank
4826 || Insstart_blank_vcol <= (colnr_T)textwidth
4827 ))))))
4828 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004829 /* Format with 'formatexpr' when it's set. Use internal formatting
4830 * when 'formatexpr' isn't set or it returns non-zero. */
4831#if defined(FEAT_EVAL)
4832 if (*curbuf->b_p_fex == NUL
4833 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004835 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (c == NUL) /* only formatting was wanted */
4839 return;
4840
4841#ifdef FEAT_COMMENTS
4842 /* Check whether this character should end a comment. */
4843 if (did_ai && (int)c == end_comment_pending)
4844 {
4845 char_u *line;
4846 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4847 int middle_len, end_len;
4848 int i;
4849
4850 /*
4851 * Need to remove existing (middle) comment leader and insert end
4852 * comment leader. First, check what comment leader we can find.
4853 */
4854 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4855 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4856 {
4857 /* Skip middle-comment string */
4858 while (*p && p[-1] != ':') /* find end of middle flags */
4859 ++p;
4860 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4861 /* Don't count trailing white space for middle_len */
4862 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4863 --middle_len;
4864
4865 /* Find the end-comment string */
4866 while (*p && p[-1] != ':') /* find end of end flags */
4867 ++p;
4868 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4869
4870 /* Skip white space before the cursor */
4871 i = curwin->w_cursor.col;
4872 while (--i >= 0 && vim_iswhite(line[i]))
4873 ;
4874 i++;
4875
4876 /* Skip to before the middle leader */
4877 i -= middle_len;
4878
4879 /* Check some expected things before we go on */
4880 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4881 {
4882 /* Backspace over all the stuff we want to replace */
4883 backspace_until_column(i);
4884
4885 /*
4886 * Insert the end-comment string, except for the last
4887 * character, which will get inserted as normal later.
4888 */
4889 ins_bytes_len(lead_end, end_len - 1);
4890 }
4891 }
4892 }
4893 end_comment_pending = NUL;
4894#endif
4895
4896 did_ai = FALSE;
4897#ifdef FEAT_SMARTINDENT
4898 did_si = FALSE;
4899 can_si = FALSE;
4900 can_si_back = FALSE;
4901#endif
4902
4903 /*
4904 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4905 * This speeds up normal text input considerably.
4906 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4907 * need to re-indent at a ':', or any other character (but not what
4908 * 'paste' is set)..
4909 */
4910#ifdef USE_ON_FLY_SCROLL
4911 dont_scroll = FALSE; /* allow scrolling here */
4912#endif
4913
4914 if ( !ISSPECIAL(c)
4915#ifdef FEAT_MBYTE
4916 && (!has_mbyte || (*mb_char2len)(c) == 1)
4917#endif
4918 && vpeekc() != NUL
4919 && !(State & REPLACE_FLAG)
4920#ifdef FEAT_CINDENT
4921 && !cindent_on()
4922#endif
4923#ifdef FEAT_RIGHTLEFT
4924 && !p_ri
4925#endif
4926 )
4927 {
4928#define INPUT_BUFLEN 100
4929 char_u buf[INPUT_BUFLEN + 1];
4930 int i;
4931 colnr_T virtcol = 0;
4932
4933 buf[0] = c;
4934 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004935 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 virtcol = get_nolist_virtcol();
4937 /*
4938 * Stop the string when:
4939 * - no more chars available
4940 * - finding a special character (command key)
4941 * - buffer is full
4942 * - running into the 'textwidth' boundary
4943 * - need to check for abbreviation: A non-word char after a word-char
4944 */
4945 while ( (c = vpeekc()) != NUL
4946 && !ISSPECIAL(c)
4947#ifdef FEAT_MBYTE
4948 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4949#endif
4950 && i < INPUT_BUFLEN
4951 && (textwidth == 0
4952 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4953 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4954 {
4955#ifdef FEAT_RIGHTLEFT
4956 c = vgetc();
4957 if (p_hkmap && KeyTyped)
4958 c = hkmap(c); /* Hebrew mode mapping */
4959# ifdef FEAT_FKMAP
4960 if (p_fkmap && KeyTyped)
4961 c = fkmap(c); /* Farsi mode mapping */
4962# endif
4963 buf[i++] = c;
4964#else
4965 buf[i++] = vgetc();
4966#endif
4967 }
4968
4969#ifdef FEAT_DIGRAPHS
4970 do_digraph(-1); /* clear digraphs */
4971 do_digraph(buf[i-1]); /* may be the start of a digraph */
4972#endif
4973 buf[i] = NUL;
4974 ins_str(buf);
4975 if (flags & INSCHAR_CTRLV)
4976 {
4977 redo_literal(*buf);
4978 i = 1;
4979 }
4980 else
4981 i = 0;
4982 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004983 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 else
4986 {
4987#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004988 int cc;
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4991 {
4992 char_u buf[MB_MAXBYTES + 1];
4993
4994 (*mb_char2bytes)(c, buf);
4995 buf[cc] = NUL;
4996 ins_char_bytes(buf, cc);
4997 AppendCharToRedobuff(c);
4998 }
4999 else
5000#endif
5001 {
5002 ins_char(c);
5003 if (flags & INSCHAR_CTRLV)
5004 redo_literal(c);
5005 else
5006 AppendCharToRedobuff(c);
5007 }
5008 }
5009}
5010
5011/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005012 * Format text at the current insert position.
5013 */
5014 static void
5015internal_format(textwidth, second_indent, flags, format_only)
5016 int textwidth;
5017 int second_indent;
5018 int flags;
5019 int format_only;
5020{
5021 int cc;
5022 int save_char = NUL;
5023 int haveto_redraw = FALSE;
5024 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5025#ifdef FEAT_MBYTE
5026 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5027#endif
5028 int fo_white_par = has_format_option(FO_WHITE_PAR);
5029 int first_line = TRUE;
5030#ifdef FEAT_COMMENTS
5031 colnr_T leader_len;
5032 int no_leader = FALSE;
5033 int do_comments = (flags & INSCHAR_DO_COM);
5034#endif
5035
5036 /*
5037 * When 'ai' is off we don't want a space under the cursor to be
5038 * deleted. Replace it with an 'x' temporarily.
5039 */
5040 if (!curbuf->b_p_ai)
5041 {
5042 cc = gchar_cursor();
5043 if (vim_iswhite(cc))
5044 {
5045 save_char = cc;
5046 pchar_cursor('x');
5047 }
5048 }
5049
5050 /*
5051 * Repeat breaking lines, until the current line is not too long.
5052 */
5053 while (!got_int)
5054 {
5055 int startcol; /* Cursor column at entry */
5056 int wantcol; /* column at textwidth border */
5057 int foundcol; /* column for start of spaces */
5058 int end_foundcol = 0; /* column for start of word */
5059 colnr_T len;
5060 colnr_T virtcol;
5061#ifdef FEAT_VREPLACE
5062 int orig_col = 0;
5063 char_u *saved_text = NULL;
5064#endif
5065 colnr_T col;
5066
5067 virtcol = get_nolist_virtcol();
5068 if (virtcol < (colnr_T)textwidth)
5069 break;
5070
5071#ifdef FEAT_COMMENTS
5072 if (no_leader)
5073 do_comments = FALSE;
5074 else if (!(flags & INSCHAR_FORMAT)
5075 && has_format_option(FO_WRAP_COMS))
5076 do_comments = TRUE;
5077
5078 /* Don't break until after the comment leader */
5079 if (do_comments)
5080 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5081 else
5082 leader_len = 0;
5083
5084 /* If the line doesn't start with a comment leader, then don't
5085 * start one in a following broken line. Avoids that a %word
5086 * moved to the start of the next line causes all following lines
5087 * to start with %. */
5088 if (leader_len == 0)
5089 no_leader = TRUE;
5090#endif
5091 if (!(flags & INSCHAR_FORMAT)
5092#ifdef FEAT_COMMENTS
5093 && leader_len == 0
5094#endif
5095 && !has_format_option(FO_WRAP))
5096
5097 {
5098 textwidth = 0;
5099 break;
5100 }
5101 if ((startcol = curwin->w_cursor.col) == 0)
5102 break;
5103
5104 /* find column of textwidth border */
5105 coladvance((colnr_T)textwidth);
5106 wantcol = curwin->w_cursor.col;
5107
5108 curwin->w_cursor.col = startcol - 1;
5109#ifdef FEAT_MBYTE
5110 /* Correct cursor for multi-byte character. */
5111 if (has_mbyte)
5112 mb_adjust_cursor();
5113#endif
5114 foundcol = 0;
5115
5116 /*
5117 * Find position to break at.
5118 * Stop at first entered white when 'formatoptions' has 'v'
5119 */
5120 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5121 || curwin->w_cursor.lnum != Insstart.lnum
5122 || curwin->w_cursor.col >= Insstart.col)
5123 {
5124 cc = gchar_cursor();
5125 if (WHITECHAR(cc))
5126 {
5127 /* remember position of blank just before text */
5128 end_foundcol = curwin->w_cursor.col;
5129
5130 /* find start of sequence of blanks */
5131 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5132 {
5133 dec_cursor();
5134 cc = gchar_cursor();
5135 }
5136 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5137 break; /* only spaces in front of text */
5138#ifdef FEAT_COMMENTS
5139 /* Don't break until after the comment leader */
5140 if (curwin->w_cursor.col < leader_len)
5141 break;
5142#endif
5143 if (has_format_option(FO_ONE_LETTER))
5144 {
5145 /* do not break after one-letter words */
5146 if (curwin->w_cursor.col == 0)
5147 break; /* one-letter word at begin */
5148
5149 col = curwin->w_cursor.col;
5150 dec_cursor();
5151 cc = gchar_cursor();
5152
5153 if (WHITECHAR(cc))
5154 continue; /* one-letter, continue */
5155 curwin->w_cursor.col = col;
5156 }
5157#ifdef FEAT_MBYTE
5158 if (has_mbyte)
5159 foundcol = curwin->w_cursor.col
5160 + (*mb_ptr2len)(ml_get_cursor());
5161 else
5162#endif
5163 foundcol = curwin->w_cursor.col + 1;
5164 if (curwin->w_cursor.col < (colnr_T)wantcol)
5165 break;
5166 }
5167#ifdef FEAT_MBYTE
5168 else if (cc >= 0x100 && fo_multibyte
5169 && curwin->w_cursor.col <= (colnr_T)wantcol)
5170 {
5171 /* Break after or before a multi-byte character. */
5172 foundcol = curwin->w_cursor.col;
5173 if (curwin->w_cursor.col < (colnr_T)wantcol)
5174 foundcol += (*mb_char2len)(cc);
5175 end_foundcol = foundcol;
5176 break;
5177 }
5178#endif
5179 if (curwin->w_cursor.col == 0)
5180 break;
5181 dec_cursor();
5182 }
5183
5184 if (foundcol == 0) /* no spaces, cannot break line */
5185 {
5186 curwin->w_cursor.col = startcol;
5187 break;
5188 }
5189
5190 /* Going to break the line, remove any "$" now. */
5191 undisplay_dollar();
5192
5193 /*
5194 * Offset between cursor position and line break is used by replace
5195 * stack functions. VREPLACE does not use this, and backspaces
5196 * over the text instead.
5197 */
5198#ifdef FEAT_VREPLACE
5199 if (State & VREPLACE_FLAG)
5200 orig_col = startcol; /* Will start backspacing from here */
5201 else
5202#endif
5203 replace_offset = startcol - end_foundcol - 1;
5204
5205 /*
5206 * adjust startcol for spaces that will be deleted and
5207 * characters that will remain on top line
5208 */
5209 curwin->w_cursor.col = foundcol;
5210 while (cc = gchar_cursor(), WHITECHAR(cc))
5211 inc_cursor();
5212 startcol -= curwin->w_cursor.col;
5213 if (startcol < 0)
5214 startcol = 0;
5215
5216#ifdef FEAT_VREPLACE
5217 if (State & VREPLACE_FLAG)
5218 {
5219 /*
5220 * In VREPLACE mode, we will backspace over the text to be
5221 * wrapped, so save a copy now to put on the next line.
5222 */
5223 saved_text = vim_strsave(ml_get_cursor());
5224 curwin->w_cursor.col = orig_col;
5225 if (saved_text == NULL)
5226 break; /* Can't do it, out of memory */
5227 saved_text[startcol] = NUL;
5228
5229 /* Backspace over characters that will move to the next line */
5230 if (!fo_white_par)
5231 backspace_until_column(foundcol);
5232 }
5233 else
5234#endif
5235 {
5236 /* put cursor after pos. to break line */
5237 if (!fo_white_par)
5238 curwin->w_cursor.col = foundcol;
5239 }
5240
5241 /*
5242 * Split the line just before the margin.
5243 * Only insert/delete lines, but don't really redraw the window.
5244 */
5245 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5246 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5247#ifdef FEAT_COMMENTS
5248 + (do_comments ? OPENLINE_DO_COM : 0)
5249#endif
5250 , old_indent);
5251 old_indent = 0;
5252
5253 replace_offset = 0;
5254 if (first_line)
5255 {
5256 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5257 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5258 if (second_indent >= 0)
5259 {
5260#ifdef FEAT_VREPLACE
5261 if (State & VREPLACE_FLAG)
5262 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5263 else
5264#endif
5265 (void)set_indent(second_indent, SIN_CHANGED);
5266 }
5267 first_line = FALSE;
5268 }
5269
5270#ifdef FEAT_VREPLACE
5271 if (State & VREPLACE_FLAG)
5272 {
5273 /*
5274 * In VREPLACE mode we have backspaced over the text to be
5275 * moved, now we re-insert it into the new line.
5276 */
5277 ins_bytes(saved_text);
5278 vim_free(saved_text);
5279 }
5280 else
5281#endif
5282 {
5283 /*
5284 * Check if cursor is not past the NUL off the line, cindent
5285 * may have added or removed indent.
5286 */
5287 curwin->w_cursor.col += startcol;
5288 len = (colnr_T)STRLEN(ml_get_curline());
5289 if (curwin->w_cursor.col > len)
5290 curwin->w_cursor.col = len;
5291 }
5292
5293 haveto_redraw = TRUE;
5294#ifdef FEAT_CINDENT
5295 can_cindent = TRUE;
5296#endif
5297 /* moved the cursor, don't autoindent or cindent now */
5298 did_ai = FALSE;
5299#ifdef FEAT_SMARTINDENT
5300 did_si = FALSE;
5301 can_si = FALSE;
5302 can_si_back = FALSE;
5303#endif
5304 line_breakcheck();
5305 }
5306
5307 if (save_char != NUL) /* put back space after cursor */
5308 pchar_cursor(save_char);
5309
5310 if (!format_only && haveto_redraw)
5311 {
5312 update_topline();
5313 redraw_curbuf_later(VALID);
5314 }
5315}
5316
5317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 * Called after inserting or deleting text: When 'formatoptions' includes the
5319 * 'a' flag format from the current line until the end of the paragraph.
5320 * Keep the cursor at the same position relative to the text.
5321 * The caller must have saved the cursor line for undo, following ones will be
5322 * saved here.
5323 */
5324 void
5325auto_format(trailblank, prev_line)
5326 int trailblank; /* when TRUE also format with trailing blank */
5327 int prev_line; /* may start in previous line */
5328{
5329 pos_T pos;
5330 colnr_T len;
5331 char_u *old;
5332 char_u *new, *pnew;
5333 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005334 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
5336 if (!has_format_option(FO_AUTO))
5337 return;
5338
5339 pos = curwin->w_cursor;
5340 old = ml_get_curline();
5341
5342 /* may remove added space */
5343 check_auto_format(FALSE);
5344
5345 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5346 * user might insert normal text next. Also skip formatting when "1" is
5347 * in 'formatoptions' and there is a single character before the cursor.
5348 * Otherwise the line would be broken and when typing another non-white
5349 * next they are not joined back together. */
5350 wasatend = (pos.col == STRLEN(old));
5351 if (*old != NUL && !trailblank && wasatend)
5352 {
5353 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005354 cc = gchar_cursor();
5355 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5356 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005358 cc = gchar_cursor();
5359 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
5361 curwin->w_cursor = pos;
5362 return;
5363 }
5364 curwin->w_cursor = pos;
5365 }
5366
5367#ifdef FEAT_COMMENTS
5368 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5369 * comments. */
5370 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5371 && get_leader_len(old, NULL, FALSE) == 0)
5372 return;
5373#endif
5374
5375 /*
5376 * May start formatting in a previous line, so that after "x" a word is
5377 * moved to the previous line if it fits there now. Only when this is not
5378 * the start of a paragraph.
5379 */
5380 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5381 {
5382 --curwin->w_cursor.lnum;
5383 if (u_save_cursor() == FAIL)
5384 return;
5385 }
5386
5387 /*
5388 * Do the formatting and restore the cursor position. "saved_cursor" will
5389 * be adjusted for the text formatting.
5390 */
5391 saved_cursor = pos;
5392 format_lines((linenr_T)-1);
5393 curwin->w_cursor = saved_cursor;
5394 saved_cursor.lnum = 0;
5395
5396 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5397 {
5398 /* "cannot happen" */
5399 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5400 coladvance((colnr_T)MAXCOL);
5401 }
5402 else
5403 check_cursor_col();
5404
5405 /* Insert mode: If the cursor is now after the end of the line while it
5406 * previously wasn't, the line was broken. Because of the rule above we
5407 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5408 * formatted. */
5409 if (!wasatend && has_format_option(FO_WHITE_PAR))
5410 {
5411 new = ml_get_curline();
5412 len = STRLEN(new);
5413 if (curwin->w_cursor.col == len)
5414 {
5415 pnew = vim_strnsave(new, len + 2);
5416 pnew[len] = ' ';
5417 pnew[len + 1] = NUL;
5418 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5419 /* remove the space later */
5420 did_add_space = TRUE;
5421 }
5422 else
5423 /* may remove added space */
5424 check_auto_format(FALSE);
5425 }
5426
5427 check_cursor();
5428}
5429
5430/*
5431 * When an extra space was added to continue a paragraph for auto-formatting,
5432 * delete it now. The space must be under the cursor, just after the insert
5433 * position.
5434 */
5435 static void
5436check_auto_format(end_insert)
5437 int end_insert; /* TRUE when ending Insert mode */
5438{
5439 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005440 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442 if (did_add_space)
5443 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005444 cc = gchar_cursor();
5445 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 /* Somehow the space was removed already. */
5447 did_add_space = FALSE;
5448 else
5449 {
5450 if (!end_insert)
5451 {
5452 inc_cursor();
5453 c = gchar_cursor();
5454 dec_cursor();
5455 }
5456 if (c != NUL)
5457 {
5458 /* The space is no longer at the end of the line, delete it. */
5459 del_char(FALSE);
5460 did_add_space = FALSE;
5461 }
5462 }
5463 }
5464}
5465
5466/*
5467 * Find out textwidth to be used for formatting:
5468 * if 'textwidth' option is set, use it
5469 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5470 * if invalid value, use 0.
5471 * Set default to window width (maximum 79) for "gq" operator.
5472 */
5473 int
5474comp_textwidth(ff)
5475 int ff; /* force formatting (for "Q" command) */
5476{
5477 int textwidth;
5478
5479 textwidth = curbuf->b_p_tw;
5480 if (textwidth == 0 && curbuf->b_p_wm)
5481 {
5482 /* The width is the window width minus 'wrapmargin' minus all the
5483 * things that add to the margin. */
5484 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5485#ifdef FEAT_CMDWIN
5486 if (cmdwin_type != 0)
5487 textwidth -= 1;
5488#endif
5489#ifdef FEAT_FOLDING
5490 textwidth -= curwin->w_p_fdc;
5491#endif
5492#ifdef FEAT_SIGNS
5493 if (curwin->w_buffer->b_signlist != NULL
5494# ifdef FEAT_NETBEANS_INTG
5495 || usingNetbeans
5496# endif
5497 )
5498 textwidth -= 1;
5499#endif
5500 if (curwin->w_p_nu)
5501 textwidth -= 8;
5502 }
5503 if (textwidth < 0)
5504 textwidth = 0;
5505 if (ff && textwidth == 0)
5506 {
5507 textwidth = W_WIDTH(curwin) - 1;
5508 if (textwidth > 79)
5509 textwidth = 79;
5510 }
5511 return textwidth;
5512}
5513
5514/*
5515 * Put a character in the redo buffer, for when just after a CTRL-V.
5516 */
5517 static void
5518redo_literal(c)
5519 int c;
5520{
5521 char_u buf[10];
5522
5523 /* Only digits need special treatment. Translate them into a string of
5524 * three digits. */
5525 if (VIM_ISDIGIT(c))
5526 {
5527 sprintf((char *)buf, "%03d", c);
5528 AppendToRedobuff(buf);
5529 }
5530 else
5531 AppendCharToRedobuff(c);
5532}
5533
5534/*
5535 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005536 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 */
5538 static void
5539start_arrow(end_insert_pos)
5540 pos_T *end_insert_pos;
5541{
5542 if (!arrow_used) /* something has been inserted */
5543 {
5544 AppendToRedobuff(ESC_STR);
5545 stop_insert(end_insert_pos, FALSE);
5546 arrow_used = TRUE; /* this means we stopped the current insert */
5547 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005548#ifdef FEAT_SYN_HL
5549 check_spell_redraw();
5550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
5552
Bram Moolenaar217ad922005-03-20 22:37:15 +00005553#ifdef FEAT_SYN_HL
5554/*
5555 * If we skipped highlighting word at cursor, do it now.
5556 * It may be skipped again, thus reset spell_redraw_lnum first.
5557 */
5558 static void
5559check_spell_redraw()
5560{
5561 if (spell_redraw_lnum != 0)
5562 {
5563 linenr_T lnum = spell_redraw_lnum;
5564
5565 spell_redraw_lnum = 0;
5566 redrawWinline(lnum, FALSE);
5567 }
5568}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005569
5570/*
5571 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5572 * spelled word, if there is one.
5573 */
5574 static void
5575spell_back_to_badword()
5576{
5577 pos_T tpos = curwin->w_cursor;
5578
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005579 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005580 if (curwin->w_cursor.col != tpos.col)
5581 start_arrow(&tpos);
5582}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005583#endif
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585/*
5586 * stop_arrow() is called before a change is made in insert mode.
5587 * If an arrow key has been used, start a new insertion.
5588 * Returns FAIL if undo is impossible, shouldn't insert then.
5589 */
5590 int
5591stop_arrow()
5592{
5593 if (arrow_used)
5594 {
5595 if (u_save_cursor() == OK)
5596 {
5597 arrow_used = FALSE;
5598 ins_need_undo = FALSE;
5599 }
5600 Insstart = curwin->w_cursor; /* new insertion starts here */
5601 Insstart_textlen = linetabsize(ml_get_curline());
5602 ai_col = 0;
5603#ifdef FEAT_VREPLACE
5604 if (State & VREPLACE_FLAG)
5605 {
5606 orig_line_count = curbuf->b_ml.ml_line_count;
5607 vr_lines_changed = 1;
5608 }
5609#endif
5610 ResetRedobuff();
5611 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005612 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 else if (ins_need_undo)
5615 {
5616 if (u_save_cursor() == OK)
5617 ins_need_undo = FALSE;
5618 }
5619
5620#ifdef FEAT_FOLDING
5621 /* Always open fold at the cursor line when inserting something. */
5622 foldOpenCursor();
5623#endif
5624
5625 return (arrow_used || ins_need_undo ? FAIL : OK);
5626}
5627
5628/*
5629 * do a few things to stop inserting
5630 */
5631 static void
5632stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005633 pos_T *end_insert_pos; /* where insert ended */
5634 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005636 int cc;
5637 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
5639 stop_redo_ins();
5640 replace_flush(); /* abandon replace stack */
5641
5642 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005643 * Save the inserted text for later redo with ^@ and CTRL-A.
5644 * Don't do it when "restart_edit" was set and nothing was inserted,
5645 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005647 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005648 if (did_restart_edit == 0 || (ptr != NULL
5649 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005650 {
5651 vim_free(last_insert);
5652 last_insert = ptr;
5653 last_insert_skip = new_insert_skip;
5654 }
5655 else
5656 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 if (!arrow_used)
5659 {
5660 /* Auto-format now. It may seem strange to do this when stopping an
5661 * insertion (or moving the cursor), but it's required when appending
5662 * a line and having it end in a space. But only do it when something
5663 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005664 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005666 pos_T tpos = curwin->w_cursor;
5667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 /* When the cursor is at the end of the line after a space the
5669 * formatting will move it to the following word. Avoid that by
5670 * moving the cursor onto the space. */
5671 cc = 'x';
5672 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5673 {
5674 dec_cursor();
5675 cc = gchar_cursor();
5676 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005677 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679
5680 auto_format(TRUE, FALSE);
5681
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005682 if (vim_iswhite(cc))
5683 {
5684 if (gchar_cursor() != NUL)
5685 inc_cursor();
5686#ifdef FEAT_VIRTUALEDIT
5687 /* If the cursor is still at the same character, also keep
5688 * the "coladd". */
5689 if (gchar_cursor() == NUL
5690 && curwin->w_cursor.lnum == tpos.lnum
5691 && curwin->w_cursor.col == tpos.col)
5692 curwin->w_cursor.coladd = tpos.coladd;
5693#endif
5694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696
5697 /* If a space was inserted for auto-formatting, remove it now. */
5698 check_auto_format(TRUE);
5699
5700 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005701 * of the line, and put the cursor back.
5702 * Do this when ESC was used or moving the cursor up/down. */
5703 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5704 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005706 pos_T tpos = curwin->w_cursor;
5707
5708 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5710 --curwin->w_cursor.col;
5711 while (cc = gchar_cursor(), vim_iswhite(cc))
5712 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005713 if (curwin->w_cursor.lnum != tpos.lnum)
5714 curwin->w_cursor = tpos;
5715 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5717
5718#ifdef FEAT_VISUAL
5719 /* <C-S-Right> may have started Visual mode, adjust the position for
5720 * deleted characters. */
5721 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5722 {
5723 cc = STRLEN(ml_get_curline());
5724 if (VIsual.col > (colnr_T)cc)
5725 {
5726 VIsual.col = cc;
5727# ifdef FEAT_VIRTUALEDIT
5728 VIsual.coladd = 0;
5729# endif
5730 }
5731 }
5732#endif
5733 }
5734 }
5735 did_ai = FALSE;
5736#ifdef FEAT_SMARTINDENT
5737 did_si = FALSE;
5738 can_si = FALSE;
5739 can_si_back = FALSE;
5740#endif
5741
5742 /* set '[ and '] to the inserted text */
5743 curbuf->b_op_start = Insstart;
5744 curbuf->b_op_end = *end_insert_pos;
5745}
5746
5747/*
5748 * Set the last inserted text to a single character.
5749 * Used for the replace command.
5750 */
5751 void
5752set_last_insert(c)
5753 int c;
5754{
5755 char_u *s;
5756
5757 vim_free(last_insert);
5758#ifdef FEAT_MBYTE
5759 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5760#else
5761 last_insert = alloc(6);
5762#endif
5763 if (last_insert != NULL)
5764 {
5765 s = last_insert;
5766 /* Use the CTRL-V only when entering a special char */
5767 if (c < ' ' || c == DEL)
5768 *s++ = Ctrl_V;
5769 s = add_char2buf(c, s);
5770 *s++ = ESC;
5771 *s++ = NUL;
5772 last_insert_skip = 0;
5773 }
5774}
5775
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005776#if defined(EXITFREE) || defined(PROTO)
5777 void
5778free_last_insert()
5779{
5780 vim_free(last_insert);
5781 last_insert = NULL;
5782}
5783#endif
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785/*
5786 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5787 * and CSI. Handle multi-byte characters.
5788 * Returns a pointer to after the added bytes.
5789 */
5790 char_u *
5791add_char2buf(c, s)
5792 int c;
5793 char_u *s;
5794{
5795#ifdef FEAT_MBYTE
5796 char_u temp[MB_MAXBYTES];
5797 int i;
5798 int len;
5799
5800 len = (*mb_char2bytes)(c, temp);
5801 for (i = 0; i < len; ++i)
5802 {
5803 c = temp[i];
5804#endif
5805 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5806 if (c == K_SPECIAL)
5807 {
5808 *s++ = K_SPECIAL;
5809 *s++ = KS_SPECIAL;
5810 *s++ = KE_FILLER;
5811 }
5812#ifdef FEAT_GUI
5813 else if (c == CSI)
5814 {
5815 *s++ = CSI;
5816 *s++ = KS_EXTRA;
5817 *s++ = (int)KE_CSI;
5818 }
5819#endif
5820 else
5821 *s++ = c;
5822#ifdef FEAT_MBYTE
5823 }
5824#endif
5825 return s;
5826}
5827
5828/*
5829 * move cursor to start of line
5830 * if flags & BL_WHITE move to first non-white
5831 * if flags & BL_SOL move to first non-white if startofline is set,
5832 * otherwise keep "curswant" column
5833 * if flags & BL_FIX don't leave the cursor on a NUL.
5834 */
5835 void
5836beginline(flags)
5837 int flags;
5838{
5839 if ((flags & BL_SOL) && !p_sol)
5840 coladvance(curwin->w_curswant);
5841 else
5842 {
5843 curwin->w_cursor.col = 0;
5844#ifdef FEAT_VIRTUALEDIT
5845 curwin->w_cursor.coladd = 0;
5846#endif
5847
5848 if (flags & (BL_WHITE | BL_SOL))
5849 {
5850 char_u *ptr;
5851
5852 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5853 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5854 ++curwin->w_cursor.col;
5855 }
5856 curwin->w_set_curswant = TRUE;
5857 }
5858}
5859
5860/*
5861 * oneright oneleft cursor_down cursor_up
5862 *
5863 * Move one char {right,left,down,up}.
5864 * Doesn't move onto the NUL past the end of the line.
5865 * Return OK when successful, FAIL when we hit a line of file boundary.
5866 */
5867
5868 int
5869oneright()
5870{
5871 char_u *ptr;
5872#ifdef FEAT_MBYTE
5873 int l;
5874#endif
5875
5876#ifdef FEAT_VIRTUALEDIT
5877 if (virtual_active())
5878 {
5879 pos_T prevpos = curwin->w_cursor;
5880
5881 /* Adjust for multi-wide char (excluding TAB) */
5882 ptr = ml_get_cursor();
5883 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5884#ifdef FEAT_MBYTE
5885 (*mb_ptr2char)(ptr)
5886#else
5887 *ptr
5888#endif
5889 ))
5890 ? ptr2cells(ptr) : 1));
5891 curwin->w_set_curswant = TRUE;
5892 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5893 return (prevpos.col != curwin->w_cursor.col
5894 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5895 }
5896#endif
5897
5898 ptr = ml_get_cursor();
5899#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005900 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
5902 /* The character under the cursor is a multi-byte character, move
5903 * several bytes right, but don't end up on the NUL. */
5904 if (ptr[l] == NUL)
5905 return FAIL;
5906 curwin->w_cursor.col += l;
5907 }
5908 else
5909#endif
5910 {
5911 if (*ptr++ == NUL || *ptr == NUL)
5912 return FAIL;
5913 ++curwin->w_cursor.col;
5914 }
5915
5916 curwin->w_set_curswant = TRUE;
5917 return OK;
5918}
5919
5920 int
5921oneleft()
5922{
5923#ifdef FEAT_VIRTUALEDIT
5924 if (virtual_active())
5925 {
5926 int width;
5927 int v = getviscol();
5928
5929 if (v == 0)
5930 return FAIL;
5931
5932# ifdef FEAT_LINEBREAK
5933 /* We might get stuck on 'showbreak', skip over it. */
5934 width = 1;
5935 for (;;)
5936 {
5937 coladvance(v - width);
5938 /* getviscol() is slow, skip it when 'showbreak' is empty and
5939 * there are no multi-byte characters */
5940 if ((*p_sbr == NUL
5941# ifdef FEAT_MBYTE
5942 && !has_mbyte
5943# endif
5944 ) || getviscol() < v)
5945 break;
5946 ++width;
5947 }
5948# else
5949 coladvance(v - 1);
5950# endif
5951
5952 if (curwin->w_cursor.coladd == 1)
5953 {
5954 char_u *ptr;
5955
5956 /* Adjust for multi-wide char (not a TAB) */
5957 ptr = ml_get_cursor();
5958 if (*ptr != TAB && vim_isprintc(
5959# ifdef FEAT_MBYTE
5960 (*mb_ptr2char)(ptr)
5961# else
5962 *ptr
5963# endif
5964 ) && ptr2cells(ptr) > 1)
5965 curwin->w_cursor.coladd = 0;
5966 }
5967
5968 curwin->w_set_curswant = TRUE;
5969 return OK;
5970 }
5971#endif
5972
5973 if (curwin->w_cursor.col == 0)
5974 return FAIL;
5975
5976 curwin->w_set_curswant = TRUE;
5977 --curwin->w_cursor.col;
5978
5979#ifdef FEAT_MBYTE
5980 /* if the character on the left of the current cursor is a multi-byte
5981 * character, move to its first byte */
5982 if (has_mbyte)
5983 mb_adjust_cursor();
5984#endif
5985 return OK;
5986}
5987
5988 int
5989cursor_up(n, upd_topline)
5990 long n;
5991 int upd_topline; /* When TRUE: update topline */
5992{
5993 linenr_T lnum;
5994
5995 if (n > 0)
5996 {
5997 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005998 /* This fails if the cursor is already in the first line or the count
5999 * is larger than the line number and '-' is in 'cpoptions' */
6000 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 return FAIL;
6002 if (n >= lnum)
6003 lnum = 1;
6004 else
6005#ifdef FEAT_FOLDING
6006 if (hasAnyFolding(curwin))
6007 {
6008 /*
6009 * Count each sequence of folded lines as one logical line.
6010 */
6011 /* go to the the start of the current fold */
6012 (void)hasFolding(lnum, &lnum, NULL);
6013
6014 while (n--)
6015 {
6016 /* move up one line */
6017 --lnum;
6018 if (lnum <= 1)
6019 break;
6020 /* If we entered a fold, move to the beginning, unless in
6021 * Insert mode or when 'foldopen' contains "all": it will open
6022 * in a moment. */
6023 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6024 (void)hasFolding(lnum, &lnum, NULL);
6025 }
6026 if (lnum < 1)
6027 lnum = 1;
6028 }
6029 else
6030#endif
6031 lnum -= n;
6032 curwin->w_cursor.lnum = lnum;
6033 }
6034
6035 /* try to advance to the column we want to be at */
6036 coladvance(curwin->w_curswant);
6037
6038 if (upd_topline)
6039 update_topline(); /* make sure curwin->w_topline is valid */
6040
6041 return OK;
6042}
6043
6044/*
6045 * Cursor down a number of logical lines.
6046 */
6047 int
6048cursor_down(n, upd_topline)
6049 long n;
6050 int upd_topline; /* When TRUE: update topline */
6051{
6052 linenr_T lnum;
6053
6054 if (n > 0)
6055 {
6056 lnum = curwin->w_cursor.lnum;
6057#ifdef FEAT_FOLDING
6058 /* Move to last line of fold, will fail if it's the end-of-file. */
6059 (void)hasFolding(lnum, NULL, &lnum);
6060#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006061 /* This fails if the cursor is already in the last line or would move
6062 * beyound the last line and '-' is in 'cpoptions' */
6063 if (lnum >= curbuf->b_ml.ml_line_count
6064 || (lnum + n > curbuf->b_ml.ml_line_count
6065 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 return FAIL;
6067 if (lnum + n >= curbuf->b_ml.ml_line_count)
6068 lnum = curbuf->b_ml.ml_line_count;
6069 else
6070#ifdef FEAT_FOLDING
6071 if (hasAnyFolding(curwin))
6072 {
6073 linenr_T last;
6074
6075 /* count each sequence of folded lines as one logical line */
6076 while (n--)
6077 {
6078 if (hasFolding(lnum, NULL, &last))
6079 lnum = last + 1;
6080 else
6081 ++lnum;
6082 if (lnum >= curbuf->b_ml.ml_line_count)
6083 break;
6084 }
6085 if (lnum > curbuf->b_ml.ml_line_count)
6086 lnum = curbuf->b_ml.ml_line_count;
6087 }
6088 else
6089#endif
6090 lnum += n;
6091 curwin->w_cursor.lnum = lnum;
6092 }
6093
6094 /* try to advance to the column we want to be at */
6095 coladvance(curwin->w_curswant);
6096
6097 if (upd_topline)
6098 update_topline(); /* make sure curwin->w_topline is valid */
6099
6100 return OK;
6101}
6102
6103/*
6104 * Stuff the last inserted text in the read buffer.
6105 * Last_insert actually is a copy of the redo buffer, so we
6106 * first have to remove the command.
6107 */
6108 int
6109stuff_inserted(c, count, no_esc)
6110 int c; /* Command character to be inserted */
6111 long count; /* Repeat this many times */
6112 int no_esc; /* Don't add an ESC at the end */
6113{
6114 char_u *esc_ptr;
6115 char_u *ptr;
6116 char_u *last_ptr;
6117 char_u last = NUL;
6118
6119 ptr = get_last_insert();
6120 if (ptr == NULL)
6121 {
6122 EMSG(_(e_noinstext));
6123 return FAIL;
6124 }
6125
6126 /* may want to stuff the command character, to start Insert mode */
6127 if (c != NUL)
6128 stuffcharReadbuff(c);
6129 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6130 *esc_ptr = NUL; /* remove the ESC */
6131
6132 /* when the last char is either "0" or "^" it will be quoted if no ESC
6133 * comes after it OR if it will inserted more than once and "ptr"
6134 * starts with ^D. -- Acevedo
6135 */
6136 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6137 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6138 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6139 {
6140 last = *last_ptr;
6141 *last_ptr = NUL;
6142 }
6143
6144 do
6145 {
6146 stuffReadbuff(ptr);
6147 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6148 if (last)
6149 stuffReadbuff((char_u *)(last == '0'
6150 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6151 : IF_EB("\026^", CTRL_V_STR "^")));
6152 }
6153 while (--count > 0);
6154
6155 if (last)
6156 *last_ptr = last;
6157
6158 if (esc_ptr != NULL)
6159 *esc_ptr = ESC; /* put the ESC back */
6160
6161 /* may want to stuff a trailing ESC, to get out of Insert mode */
6162 if (!no_esc)
6163 stuffcharReadbuff(ESC);
6164
6165 return OK;
6166}
6167
6168 char_u *
6169get_last_insert()
6170{
6171 if (last_insert == NULL)
6172 return NULL;
6173 return last_insert + last_insert_skip;
6174}
6175
6176/*
6177 * Get last inserted string, and remove trailing <Esc>.
6178 * Returns pointer to allocated memory (must be freed) or NULL.
6179 */
6180 char_u *
6181get_last_insert_save()
6182{
6183 char_u *s;
6184 int len;
6185
6186 if (last_insert == NULL)
6187 return NULL;
6188 s = vim_strsave(last_insert + last_insert_skip);
6189 if (s != NULL)
6190 {
6191 len = (int)STRLEN(s);
6192 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6193 s[len - 1] = NUL;
6194 }
6195 return s;
6196}
6197
6198/*
6199 * Check the word in front of the cursor for an abbreviation.
6200 * Called when the non-id character "c" has been entered.
6201 * When an abbreviation is recognized it is removed from the text and
6202 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6203 */
6204 static int
6205echeck_abbr(c)
6206 int c;
6207{
6208 /* Don't check for abbreviation in paste mode, when disabled and just
6209 * after moving around with cursor keys. */
6210 if (p_paste || no_abbr || arrow_used)
6211 return FALSE;
6212
6213 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6214 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6215}
6216
6217/*
6218 * replace-stack functions
6219 *
6220 * When replacing characters, the replaced characters are remembered for each
6221 * new character. This is used to re-insert the old text when backspacing.
6222 *
6223 * There is a NUL headed list of characters for each character that is
6224 * currently in the file after the insertion point. When BS is used, one NUL
6225 * headed list is put back for the deleted character.
6226 *
6227 * For a newline, there are two NUL headed lists. One contains the characters
6228 * that the NL replaced. The extra one stores the characters after the cursor
6229 * that were deleted (always white space).
6230 *
6231 * Replace_offset is normally 0, in which case replace_push will add a new
6232 * character at the end of the stack. If replace_offset is not 0, that many
6233 * characters will be left on the stack above the newly inserted character.
6234 */
6235
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006236static char_u *replace_stack = NULL;
6237static long replace_stack_nr = 0; /* next entry in replace stack */
6238static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
6240 void
6241replace_push(c)
6242 int c; /* character that is replaced (NUL is none) */
6243{
6244 char_u *p;
6245
6246 if (replace_stack_nr < replace_offset) /* nothing to do */
6247 return;
6248 if (replace_stack_len <= replace_stack_nr)
6249 {
6250 replace_stack_len += 50;
6251 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6252 if (p == NULL) /* out of memory */
6253 {
6254 replace_stack_len -= 50;
6255 return;
6256 }
6257 if (replace_stack != NULL)
6258 {
6259 mch_memmove(p, replace_stack,
6260 (size_t)(replace_stack_nr * sizeof(char_u)));
6261 vim_free(replace_stack);
6262 }
6263 replace_stack = p;
6264 }
6265 p = replace_stack + replace_stack_nr - replace_offset;
6266 if (replace_offset)
6267 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6268 *p = c;
6269 ++replace_stack_nr;
6270}
6271
6272/*
6273 * call replace_push(c) with replace_offset set to the first NUL.
6274 */
6275 static void
6276replace_push_off(c)
6277 int c;
6278{
6279 char_u *p;
6280
6281 p = replace_stack + replace_stack_nr;
6282 for (replace_offset = 1; replace_offset < replace_stack_nr;
6283 ++replace_offset)
6284 if (*--p == NUL)
6285 break;
6286 replace_push(c);
6287 replace_offset = 0;
6288}
6289
6290/*
6291 * Pop one item from the replace stack.
6292 * return -1 if stack empty
6293 * return replaced character or NUL otherwise
6294 */
6295 static int
6296replace_pop()
6297{
6298 if (replace_stack_nr == 0)
6299 return -1;
6300 return (int)replace_stack[--replace_stack_nr];
6301}
6302
6303/*
6304 * Join the top two items on the replace stack. This removes to "off"'th NUL
6305 * encountered.
6306 */
6307 static void
6308replace_join(off)
6309 int off; /* offset for which NUL to remove */
6310{
6311 int i;
6312
6313 for (i = replace_stack_nr; --i >= 0; )
6314 if (replace_stack[i] == NUL && off-- <= 0)
6315 {
6316 --replace_stack_nr;
6317 mch_memmove(replace_stack + i, replace_stack + i + 1,
6318 (size_t)(replace_stack_nr - i));
6319 return;
6320 }
6321}
6322
6323/*
6324 * Pop bytes from the replace stack until a NUL is found, and insert them
6325 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6326 */
6327 static void
6328replace_pop_ins()
6329{
6330 int cc;
6331 int oldState = State;
6332
6333 State = NORMAL; /* don't want REPLACE here */
6334 while ((cc = replace_pop()) > 0)
6335 {
6336#ifdef FEAT_MBYTE
6337 mb_replace_pop_ins(cc);
6338#else
6339 ins_char(cc);
6340#endif
6341 dec_cursor();
6342 }
6343 State = oldState;
6344}
6345
6346#ifdef FEAT_MBYTE
6347/*
6348 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6349 * indicates a multi-byte char, pop the other bytes too.
6350 */
6351 static void
6352mb_replace_pop_ins(cc)
6353 int cc;
6354{
6355 int n;
6356 char_u buf[MB_MAXBYTES];
6357 int i;
6358 int c;
6359
6360 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6361 {
6362 buf[0] = cc;
6363 for (i = 1; i < n; ++i)
6364 buf[i] = replace_pop();
6365 ins_bytes_len(buf, n);
6366 }
6367 else
6368 ins_char(cc);
6369
6370 if (enc_utf8)
6371 /* Handle composing chars. */
6372 for (;;)
6373 {
6374 c = replace_pop();
6375 if (c == -1) /* stack empty */
6376 break;
6377 if ((n = MB_BYTE2LEN(c)) == 1)
6378 {
6379 /* Not a multi-byte char, put it back. */
6380 replace_push(c);
6381 break;
6382 }
6383 else
6384 {
6385 buf[0] = c;
6386 for (i = 1; i < n; ++i)
6387 buf[i] = replace_pop();
6388 if (utf_iscomposing(utf_ptr2char(buf)))
6389 ins_bytes_len(buf, n);
6390 else
6391 {
6392 /* Not a composing char, put it back. */
6393 for (i = n - 1; i >= 0; --i)
6394 replace_push(buf[i]);
6395 break;
6396 }
6397 }
6398 }
6399}
6400#endif
6401
6402/*
6403 * make the replace stack empty
6404 * (called when exiting replace mode)
6405 */
6406 static void
6407replace_flush()
6408{
6409 vim_free(replace_stack);
6410 replace_stack = NULL;
6411 replace_stack_len = 0;
6412 replace_stack_nr = 0;
6413}
6414
6415/*
6416 * Handle doing a BS for one character.
6417 * cc < 0: replace stack empty, just move cursor
6418 * cc == 0: character was inserted, delete it
6419 * cc > 0: character was replaced, put cc (first byte of original char) back
6420 * and check for more characters to be put back
6421 */
6422 static void
6423replace_do_bs()
6424{
6425 int cc;
6426#ifdef FEAT_VREPLACE
6427 int orig_len = 0;
6428 int ins_len;
6429 int orig_vcols = 0;
6430 colnr_T start_vcol;
6431 char_u *p;
6432 int i;
6433 int vcol;
6434#endif
6435
6436 cc = replace_pop();
6437 if (cc > 0)
6438 {
6439#ifdef FEAT_VREPLACE
6440 if (State & VREPLACE_FLAG)
6441 {
6442 /* Get the number of screen cells used by the character we are
6443 * going to delete. */
6444 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6445 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6446 }
6447#endif
6448#ifdef FEAT_MBYTE
6449 if (has_mbyte)
6450 {
6451 del_char(FALSE);
6452# ifdef FEAT_VREPLACE
6453 if (State & VREPLACE_FLAG)
6454 orig_len = STRLEN(ml_get_cursor());
6455# endif
6456 replace_push(cc);
6457 }
6458 else
6459#endif
6460 {
6461 pchar_cursor(cc);
6462#ifdef FEAT_VREPLACE
6463 if (State & VREPLACE_FLAG)
6464 orig_len = STRLEN(ml_get_cursor()) - 1;
6465#endif
6466 }
6467 replace_pop_ins();
6468
6469#ifdef FEAT_VREPLACE
6470 if (State & VREPLACE_FLAG)
6471 {
6472 /* Get the number of screen cells used by the inserted characters */
6473 p = ml_get_cursor();
6474 ins_len = STRLEN(p) - orig_len;
6475 vcol = start_vcol;
6476 for (i = 0; i < ins_len; ++i)
6477 {
6478 vcol += chartabsize(p + i, vcol);
6479#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006480 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481#endif
6482 }
6483 vcol -= start_vcol;
6484
6485 /* Delete spaces that were inserted after the cursor to keep the
6486 * text aligned. */
6487 curwin->w_cursor.col += ins_len;
6488 while (vcol > orig_vcols && gchar_cursor() == ' ')
6489 {
6490 del_char(FALSE);
6491 ++orig_vcols;
6492 }
6493 curwin->w_cursor.col -= ins_len;
6494 }
6495#endif
6496
6497 /* mark the buffer as changed and prepare for displaying */
6498 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6499 }
6500 else if (cc == 0)
6501 (void)del_char(FALSE);
6502}
6503
6504#ifdef FEAT_CINDENT
6505/*
6506 * Return TRUE if C-indenting is on.
6507 */
6508 static int
6509cindent_on()
6510{
6511 return (!p_paste && (curbuf->b_p_cin
6512# ifdef FEAT_EVAL
6513 || *curbuf->b_p_inde != NUL
6514# endif
6515 ));
6516}
6517#endif
6518
6519#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6520/*
6521 * Re-indent the current line, based on the current contents of it and the
6522 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6523 * confused what all the part that handles Control-T is doing that I'm not.
6524 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6525 */
6526
6527 void
6528fixthisline(get_the_indent)
6529 int (*get_the_indent) __ARGS((void));
6530{
6531 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6532 if (linewhite(curwin->w_cursor.lnum))
6533 did_ai = TRUE; /* delete the indent if the line stays empty */
6534}
6535
6536 void
6537fix_indent()
6538{
6539 if (p_paste)
6540 return;
6541# ifdef FEAT_LISP
6542 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6543 fixthisline(get_lisp_indent);
6544# endif
6545# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6546 else
6547# endif
6548# ifdef FEAT_CINDENT
6549 if (cindent_on())
6550 do_c_expr_indent();
6551# endif
6552}
6553
6554#endif
6555
6556#ifdef FEAT_CINDENT
6557/*
6558 * return TRUE if 'cinkeys' contains the key "keytyped",
6559 * when == '*': Only if key is preceded with '*' (indent before insert)
6560 * when == '!': Only if key is prededed with '!' (don't insert)
6561 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6562 *
6563 * "keytyped" can have a few special values:
6564 * KEY_OPEN_FORW
6565 * KEY_OPEN_BACK
6566 * KEY_COMPLETE just finished completion.
6567 *
6568 * If line_is_empty is TRUE accept keys with '0' before them.
6569 */
6570 int
6571in_cinkeys(keytyped, when, line_is_empty)
6572 int keytyped;
6573 int when;
6574 int line_is_empty;
6575{
6576 char_u *look;
6577 int try_match;
6578 int try_match_word;
6579 char_u *p;
6580 char_u *line;
6581 int icase;
6582 int i;
6583
6584#ifdef FEAT_EVAL
6585 if (*curbuf->b_p_inde != NUL)
6586 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6587 else
6588#endif
6589 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6590 while (*look)
6591 {
6592 /*
6593 * Find out if we want to try a match with this key, depending on
6594 * 'when' and a '*' or '!' before the key.
6595 */
6596 switch (when)
6597 {
6598 case '*': try_match = (*look == '*'); break;
6599 case '!': try_match = (*look == '!'); break;
6600 default: try_match = (*look != '*'); break;
6601 }
6602 if (*look == '*' || *look == '!')
6603 ++look;
6604
6605 /*
6606 * If there is a '0', only accept a match if the line is empty.
6607 * But may still match when typing last char of a word.
6608 */
6609 if (*look == '0')
6610 {
6611 try_match_word = try_match;
6612 if (!line_is_empty)
6613 try_match = FALSE;
6614 ++look;
6615 }
6616 else
6617 try_match_word = FALSE;
6618
6619 /*
6620 * does it look like a control character?
6621 */
6622 if (*look == '^'
6623#ifdef EBCDIC
6624 && (Ctrl_chr(look[1]) != 0)
6625#else
6626 && look[1] >= '?' && look[1] <= '_'
6627#endif
6628 )
6629 {
6630 if (try_match && keytyped == Ctrl_chr(look[1]))
6631 return TRUE;
6632 look += 2;
6633 }
6634 /*
6635 * 'o' means "o" command, open forward.
6636 * 'O' means "O" command, open backward.
6637 */
6638 else if (*look == 'o')
6639 {
6640 if (try_match && keytyped == KEY_OPEN_FORW)
6641 return TRUE;
6642 ++look;
6643 }
6644 else if (*look == 'O')
6645 {
6646 if (try_match && keytyped == KEY_OPEN_BACK)
6647 return TRUE;
6648 ++look;
6649 }
6650
6651 /*
6652 * 'e' means to check for "else" at start of line and just before the
6653 * cursor.
6654 */
6655 else if (*look == 'e')
6656 {
6657 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6658 {
6659 p = ml_get_curline();
6660 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6661 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6662 return TRUE;
6663 }
6664 ++look;
6665 }
6666
6667 /*
6668 * ':' only causes an indent if it is at the end of a label or case
6669 * statement, or when it was before typing the ':' (to fix
6670 * class::method for C++).
6671 */
6672 else if (*look == ':')
6673 {
6674 if (try_match && keytyped == ':')
6675 {
6676 p = ml_get_curline();
6677 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6678 return TRUE;
6679 if (curwin->w_cursor.col > 2
6680 && p[curwin->w_cursor.col - 1] == ':'
6681 && p[curwin->w_cursor.col - 2] == ':')
6682 {
6683 p[curwin->w_cursor.col - 1] = ' ';
6684 i = (cin_iscase(p) || cin_isscopedecl(p)
6685 || cin_islabel(30));
6686 p = ml_get_curline();
6687 p[curwin->w_cursor.col - 1] = ':';
6688 if (i)
6689 return TRUE;
6690 }
6691 }
6692 ++look;
6693 }
6694
6695
6696 /*
6697 * Is it a key in <>, maybe?
6698 */
6699 else if (*look == '<')
6700 {
6701 if (try_match)
6702 {
6703 /*
6704 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6705 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6706 * >, *, : and ! keys if they really really want to.
6707 */
6708 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6709 && keytyped == look[1])
6710 return TRUE;
6711
6712 if (keytyped == get_special_key_code(look + 1))
6713 return TRUE;
6714 }
6715 while (*look && *look != '>')
6716 look++;
6717 while (*look == '>')
6718 look++;
6719 }
6720
6721 /*
6722 * Is it a word: "=word"?
6723 */
6724 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6725 {
6726 ++look;
6727 if (*look == '~')
6728 {
6729 icase = TRUE;
6730 ++look;
6731 }
6732 else
6733 icase = FALSE;
6734 p = vim_strchr(look, ',');
6735 if (p == NULL)
6736 p = look + STRLEN(look);
6737 if ((try_match || try_match_word)
6738 && curwin->w_cursor.col >= (colnr_T)(p - look))
6739 {
6740 int match = FALSE;
6741
6742#ifdef FEAT_INS_EXPAND
6743 if (keytyped == KEY_COMPLETE)
6744 {
6745 char_u *s;
6746
6747 /* Just completed a word, check if it starts with "look".
6748 * search back for the start of a word. */
6749 line = ml_get_curline();
6750# ifdef FEAT_MBYTE
6751 if (has_mbyte)
6752 {
6753 char_u *n;
6754
6755 for (s = line + curwin->w_cursor.col; s > line; s = n)
6756 {
6757 n = mb_prevptr(line, s);
6758 if (!vim_iswordp(n))
6759 break;
6760 }
6761 }
6762 else
6763# endif
6764 for (s = line + curwin->w_cursor.col; s > line; --s)
6765 if (!vim_iswordc(s[-1]))
6766 break;
6767 if (s + (p - look) <= line + curwin->w_cursor.col
6768 && (icase
6769 ? MB_STRNICMP(s, look, p - look)
6770 : STRNCMP(s, look, p - look)) == 0)
6771 match = TRUE;
6772 }
6773 else
6774#endif
6775 /* TODO: multi-byte */
6776 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6777 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6778 {
6779 line = ml_get_cursor();
6780 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6781 || !vim_iswordc(line[-(p - look) - 1]))
6782 && (icase
6783 ? MB_STRNICMP(line - (p - look), look, p - look)
6784 : STRNCMP(line - (p - look), look, p - look))
6785 == 0)
6786 match = TRUE;
6787 }
6788 if (match && try_match_word && !try_match)
6789 {
6790 /* "0=word": Check if there are only blanks before the
6791 * word. */
6792 line = ml_get_curline();
6793 if ((int)(skipwhite(line) - line) !=
6794 (int)(curwin->w_cursor.col - (p - look)))
6795 match = FALSE;
6796 }
6797 if (match)
6798 return TRUE;
6799 }
6800 look = p;
6801 }
6802
6803 /*
6804 * ok, it's a boring generic character.
6805 */
6806 else
6807 {
6808 if (try_match && *look == keytyped)
6809 return TRUE;
6810 ++look;
6811 }
6812
6813 /*
6814 * Skip over ", ".
6815 */
6816 look = skip_to_option_part(look);
6817 }
6818 return FALSE;
6819}
6820#endif /* FEAT_CINDENT */
6821
6822#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6823/*
6824 * Map Hebrew keyboard when in hkmap mode.
6825 */
6826 int
6827hkmap(c)
6828 int c;
6829{
6830 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6831 {
6832 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6833 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6834 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6835 static char_u map[26] =
6836 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6837 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6838 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6839 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6840 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6841 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6842 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6843 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6844 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6845
6846 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6847 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6848 /* '-1'='sofit' */
6849 else if (c == 'x')
6850 return 'X';
6851 else if (c == 'q')
6852 return '\''; /* {geresh}={'} */
6853 else if (c == 246)
6854 return ' '; /* \"o --> ' ' for a german keyboard */
6855 else if (c == 228)
6856 return ' '; /* \"a --> ' ' -- / -- */
6857 else if (c == 252)
6858 return ' '; /* \"u --> ' ' -- / -- */
6859#ifdef EBCDIC
6860 else if (islower(c))
6861#else
6862 /* NOTE: islower() does not do the right thing for us on Linux so we
6863 * do this the same was as 5.7 and previous, so it works correctly on
6864 * all systems. Specifically, the e.g. Delete and Arrow keys are
6865 * munged and won't work if e.g. searching for Hebrew text.
6866 */
6867 else if (c >= 'a' && c <= 'z')
6868#endif
6869 return (int)(map[CharOrdLow(c)] + p_aleph);
6870 else
6871 return c;
6872 }
6873 else
6874 {
6875 switch (c)
6876 {
6877 case '`': return ';';
6878 case '/': return '.';
6879 case '\'': return ',';
6880 case 'q': return '/';
6881 case 'w': return '\'';
6882
6883 /* Hebrew letters - set offset from 'a' */
6884 case ',': c = '{'; break;
6885 case '.': c = 'v'; break;
6886 case ';': c = 't'; break;
6887 default: {
6888 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6889
6890#ifdef EBCDIC
6891 /* see note about islower() above */
6892 if (!islower(c))
6893#else
6894 if (c < 'a' || c > 'z')
6895#endif
6896 return c;
6897 c = str[CharOrdLow(c)];
6898 break;
6899 }
6900 }
6901
6902 return (int)(CharOrdLow(c) + p_aleph);
6903 }
6904}
6905#endif
6906
6907 static void
6908ins_reg()
6909{
6910 int need_redraw = FALSE;
6911 int regname;
6912 int literally = 0;
6913
6914 /*
6915 * If we are going to wait for a character, show a '"'.
6916 */
6917 pc_status = PC_STATUS_UNSET;
6918 if (redrawing() && !char_avail())
6919 {
6920 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006921 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922
6923 edit_putchar('"', TRUE);
6924#ifdef FEAT_CMDL_INFO
6925 add_to_showcmd_c(Ctrl_R);
6926#endif
6927 }
6928
6929#ifdef USE_ON_FLY_SCROLL
6930 dont_scroll = TRUE; /* disallow scrolling here */
6931#endif
6932
6933 /*
6934 * Don't map the register name. This also prevents the mode message to be
6935 * deleted when ESC is hit.
6936 */
6937 ++no_mapping;
6938 regname = safe_vgetc();
6939#ifdef FEAT_LANGMAP
6940 LANGMAP_ADJUST(regname, TRUE);
6941#endif
6942 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6943 {
6944 /* Get a third key for literal register insertion */
6945 literally = regname;
6946#ifdef FEAT_CMDL_INFO
6947 add_to_showcmd_c(literally);
6948#endif
6949 regname = safe_vgetc();
6950#ifdef FEAT_LANGMAP
6951 LANGMAP_ADJUST(regname, TRUE);
6952#endif
6953 }
6954 --no_mapping;
6955
6956#ifdef FEAT_EVAL
6957 /*
6958 * Don't call u_sync() while getting the expression,
6959 * evaluating it or giving an error message for it!
6960 */
6961 ++no_u_sync;
6962 if (regname == '=')
6963 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006964# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006968# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 /* Restore the Input Method. */
6970 if (im_on)
6971 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006974 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6975 {
6976 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 else
6980 {
6981#endif
6982 if (literally == Ctrl_O || literally == Ctrl_P)
6983 {
6984 /* Append the command to the redo buffer. */
6985 AppendCharToRedobuff(Ctrl_R);
6986 AppendCharToRedobuff(literally);
6987 AppendCharToRedobuff(regname);
6988
6989 do_put(regname, BACKWARD, 1L,
6990 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6991 }
6992 else if (insert_reg(regname, literally) == FAIL)
6993 {
6994 vim_beep();
6995 need_redraw = TRUE; /* remove the '"' */
6996 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006997 else if (stop_insert_mode)
6998 /* When the '=' register was used and a function was invoked that
6999 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7000 * insert anything, need to remove the '"' */
7001 need_redraw = TRUE;
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#ifdef FEAT_EVAL
7004 }
7005 --no_u_sync;
7006#endif
7007#ifdef FEAT_CMDL_INFO
7008 clear_showcmd();
7009#endif
7010
7011 /* If the inserted register is empty, we need to remove the '"' */
7012 if (need_redraw || stuff_empty())
7013 edit_unputchar();
7014}
7015
7016/*
7017 * CTRL-G commands in Insert mode.
7018 */
7019 static void
7020ins_ctrl_g()
7021{
7022 int c;
7023
7024#ifdef FEAT_INS_EXPAND
7025 /* Right after CTRL-X the cursor will be after the ruler. */
7026 setcursor();
7027#endif
7028
7029 /*
7030 * Don't map the second key. This also prevents the mode message to be
7031 * deleted when ESC is hit.
7032 */
7033 ++no_mapping;
7034 c = safe_vgetc();
7035 --no_mapping;
7036 switch (c)
7037 {
7038 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7039 case K_UP:
7040 case Ctrl_K:
7041 case 'k': ins_up(TRUE);
7042 break;
7043
7044 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7045 case K_DOWN:
7046 case Ctrl_J:
7047 case 'j': ins_down(TRUE);
7048 break;
7049
7050 /* CTRL-G u: start new undoable edit */
7051 case 'u': u_sync();
7052 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007053
7054 /* Need to reset Insstart, esp. because a BS that joins
7055 * aline to the previous one must save for undo. */
7056 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 break;
7058
7059 /* Unknown CTRL-G command, reserved for future expansion. */
7060 default: vim_beep();
7061 }
7062}
7063
7064/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007065 * CTRL-^ in Insert mode.
7066 */
7067 static void
7068ins_ctrl_hat()
7069{
7070 if (map_to_exists_mode((char_u *)"", LANGMAP))
7071 {
7072 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7073 if (State & LANGMAP)
7074 {
7075 curbuf->b_p_iminsert = B_IMODE_NONE;
7076 State &= ~LANGMAP;
7077 }
7078 else
7079 {
7080 curbuf->b_p_iminsert = B_IMODE_LMAP;
7081 State |= LANGMAP;
7082#ifdef USE_IM_CONTROL
7083 im_set_active(FALSE);
7084#endif
7085 }
7086 }
7087#ifdef USE_IM_CONTROL
7088 else
7089 {
7090 /* There are no ":lmap" mappings, toggle IM */
7091 if (im_get_status())
7092 {
7093 curbuf->b_p_iminsert = B_IMODE_NONE;
7094 im_set_active(FALSE);
7095 }
7096 else
7097 {
7098 curbuf->b_p_iminsert = B_IMODE_IM;
7099 State &= ~LANGMAP;
7100 im_set_active(TRUE);
7101 }
7102 }
7103#endif
7104 set_iminsert_global();
7105 showmode();
7106#ifdef FEAT_GUI
7107 /* may show different cursor shape or color */
7108 if (gui.in_use)
7109 gui_update_cursor(TRUE, FALSE);
7110#endif
7111#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7112 /* Show/unshow value of 'keymap' in status lines. */
7113 status_redraw_curbuf();
7114#endif
7115}
7116
7117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 * Handle ESC in insert mode.
7119 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7120 * insert.
7121 */
7122 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007123ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 long *count;
7125 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007126 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128 int temp;
7129 static int disabled_redraw = FALSE;
7130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007131#ifdef FEAT_SYN_HL
7132 check_spell_redraw();
7133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134#if defined(FEAT_HANGULIN)
7135# if defined(ESC_CHG_TO_ENG_MODE)
7136 hangul_input_state_set(0);
7137# endif
7138 if (composing_hangul)
7139 {
7140 push_raw_key(composing_hangul_buffer, 2);
7141 composing_hangul = 0;
7142 }
7143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 temp = curwin->w_cursor.col;
7146 if (disabled_redraw)
7147 {
7148 --RedrawingDisabled;
7149 disabled_redraw = FALSE;
7150 }
7151 if (!arrow_used)
7152 {
7153 /*
7154 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007155 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7156 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
7158 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007159 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161 /*
7162 * Repeating insert may take a long time. Check for
7163 * interrupt now and then.
7164 */
7165 if (*count > 0)
7166 {
7167 line_breakcheck();
7168 if (got_int)
7169 *count = 0;
7170 }
7171
7172 if (--*count > 0) /* repeat what was typed */
7173 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007174 /* Vi repeats the insert without replacing characters. */
7175 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7176 State &= ~REPLACE_FLAG;
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 (void)start_redo_ins();
7179 if (cmdchar == 'r' || cmdchar == 'v')
7180 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7181 ++RedrawingDisabled;
7182 disabled_redraw = TRUE;
7183 return FALSE; /* repeat the insert */
7184 }
7185 stop_insert(&curwin->w_cursor, TRUE);
7186 undisplay_dollar();
7187 }
7188
7189 /* When an autoindent was removed, curswant stays after the
7190 * indent */
7191 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7192 curwin->w_set_curswant = TRUE;
7193
7194 /* Remember the last Insert position in the '^ mark. */
7195 if (!cmdmod.keepjumps)
7196 curbuf->b_last_insert = curwin->w_cursor;
7197
7198 /*
7199 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007200 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007202 if (!nomove
7203 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#ifdef FEAT_VIRTUALEDIT
7205 || curwin->w_cursor.coladd > 0
7206#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007207 )
7208 && (restart_edit == NUL
7209 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007211 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007213 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214#ifdef FEAT_RIGHTLEFT
7215 && !revins_on
7216#endif
7217 )
7218 {
7219#ifdef FEAT_VIRTUALEDIT
7220 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7221 {
7222 oneleft();
7223 if (restart_edit != NUL)
7224 ++curwin->w_cursor.coladd;
7225 }
7226 else
7227#endif
7228 {
7229 --curwin->w_cursor.col;
7230#ifdef FEAT_MBYTE
7231 /* Correct cursor for multi-byte character. */
7232 if (has_mbyte)
7233 mb_adjust_cursor();
7234#endif
7235 }
7236 }
7237
7238#ifdef USE_IM_CONTROL
7239 /* Disable IM to allow typing English directly for Normal mode commands.
7240 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7241 * well). */
7242 if (!(State & LANGMAP))
7243 im_save_status(&curbuf->b_p_iminsert);
7244 im_set_active(FALSE);
7245#endif
7246
7247 State = NORMAL;
7248 /* need to position cursor again (e.g. when on a TAB ) */
7249 changed_cline_bef_curs();
7250
7251#ifdef FEAT_MOUSE
7252 setmouse();
7253#endif
7254#ifdef CURSOR_SHAPE
7255 ui_cursor_shape(); /* may show different cursor shape */
7256#endif
7257
7258 /*
7259 * When recording or for CTRL-O, need to display the new mode.
7260 * Otherwise remove the mode message.
7261 */
7262 if (Recording || restart_edit != NUL)
7263 showmode();
7264 else if (p_smd)
7265 MSG("");
7266
7267 return TRUE; /* exit Insert mode */
7268}
7269
7270#ifdef FEAT_RIGHTLEFT
7271/*
7272 * Toggle language: hkmap and revins_on.
7273 * Move to end of reverse inserted text.
7274 */
7275 static void
7276ins_ctrl_()
7277{
7278 if (revins_on && revins_chars && revins_scol >= 0)
7279 {
7280 while (gchar_cursor() != NUL && revins_chars--)
7281 ++curwin->w_cursor.col;
7282 }
7283 p_ri = !p_ri;
7284 revins_on = (State == INSERT && p_ri);
7285 if (revins_on)
7286 {
7287 revins_scol = curwin->w_cursor.col;
7288 revins_legal++;
7289 revins_chars = 0;
7290 undisplay_dollar();
7291 }
7292 else
7293 revins_scol = -1;
7294#ifdef FEAT_FKMAP
7295 if (p_altkeymap)
7296 {
7297 /*
7298 * to be consistent also for redo command, using '.'
7299 * set arrow_used to true and stop it - causing to redo
7300 * characters entered in one mode (normal/reverse insert).
7301 */
7302 arrow_used = TRUE;
7303 (void)stop_arrow();
7304 p_fkmap = curwin->w_p_rl ^ p_ri;
7305 if (p_fkmap && p_ri)
7306 State = INSERT;
7307 }
7308 else
7309#endif
7310 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7311 showmode();
7312}
7313#endif
7314
7315#ifdef FEAT_VISUAL
7316/*
7317 * If 'keymodel' contains "startsel", may start selection.
7318 * Returns TRUE when a CTRL-O and other keys stuffed.
7319 */
7320 static int
7321ins_start_select(c)
7322 int c;
7323{
7324 if (km_startsel)
7325 switch (c)
7326 {
7327 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 case K_PAGEUP:
7330 case K_KPAGEUP:
7331 case K_PAGEDOWN:
7332 case K_KPAGEDOWN:
7333# ifdef MACOS
7334 case K_LEFT:
7335 case K_RIGHT:
7336 case K_UP:
7337 case K_DOWN:
7338 case K_END:
7339 case K_HOME:
7340# endif
7341 if (!(mod_mask & MOD_MASK_SHIFT))
7342 break;
7343 /* FALLTHROUGH */
7344 case K_S_LEFT:
7345 case K_S_RIGHT:
7346 case K_S_UP:
7347 case K_S_DOWN:
7348 case K_S_END:
7349 case K_S_HOME:
7350 /* Start selection right away, the cursor can move with
7351 * CTRL-O when beyond the end of the line. */
7352 start_selection();
7353
7354 /* Execute the key in (insert) Select mode. */
7355 stuffcharReadbuff(Ctrl_O);
7356 if (mod_mask)
7357 {
7358 char_u buf[4];
7359
7360 buf[0] = K_SPECIAL;
7361 buf[1] = KS_MODIFIER;
7362 buf[2] = mod_mask;
7363 buf[3] = NUL;
7364 stuffReadbuff(buf);
7365 }
7366 stuffcharReadbuff(c);
7367 return TRUE;
7368 }
7369 return FALSE;
7370}
7371#endif
7372
7373/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007374 * <Insert> key in Insert mode: toggle insert/remplace mode.
7375 */
7376 static void
7377ins_insert(replaceState)
7378 int replaceState;
7379{
7380#ifdef FEAT_FKMAP
7381 if (p_fkmap && p_ri)
7382 {
7383 beep_flush();
7384 EMSG(farsi_text_3); /* encoded in Farsi */
7385 return;
7386 }
7387#endif
7388
7389#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007390# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007391 set_vim_var_string(VV_INSERTMODE,
7392 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007393# ifdef FEAT_VREPLACE
7394 replaceState == VREPLACE ? "v" :
7395# endif
7396 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007397# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007398 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7399#endif
7400 if (State & REPLACE_FLAG)
7401 State = INSERT | (State & LANGMAP);
7402 else
7403 State = replaceState | (State & LANGMAP);
7404 AppendCharToRedobuff(K_INS);
7405 showmode();
7406#ifdef CURSOR_SHAPE
7407 ui_cursor_shape(); /* may show different cursor shape */
7408#endif
7409}
7410
7411/*
7412 * Pressed CTRL-O in Insert mode.
7413 */
7414 static void
7415ins_ctrl_o()
7416{
7417#ifdef FEAT_VREPLACE
7418 if (State & VREPLACE_FLAG)
7419 restart_edit = 'V';
7420 else
7421#endif
7422 if (State & REPLACE_FLAG)
7423 restart_edit = 'R';
7424 else
7425 restart_edit = 'I';
7426#ifdef FEAT_VIRTUALEDIT
7427 if (virtual_active())
7428 ins_at_eol = FALSE; /* cursor always keeps its column */
7429 else
7430#endif
7431 ins_at_eol = (gchar_cursor() == NUL);
7432}
7433
7434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 * If the cursor is on an indent, ^T/^D insert/delete one
7436 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7437 * Always round the indent to 'shiftwith', this is compatible
7438 * with vi. But vi only supports ^T and ^D after an
7439 * autoindent, we support it everywhere.
7440 */
7441 static void
7442ins_shift(c, lastc)
7443 int c;
7444 int lastc;
7445{
7446 if (stop_arrow() == FAIL)
7447 return;
7448 AppendCharToRedobuff(c);
7449
7450 /*
7451 * 0^D and ^^D: remove all indent.
7452 */
7453 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7454 {
7455 --curwin->w_cursor.col;
7456 (void)del_char(FALSE); /* delete the '^' or '0' */
7457 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7458 if (State & REPLACE_FLAG)
7459 replace_pop_ins();
7460 if (lastc == '^')
7461 old_indent = get_indent(); /* remember curr. indent */
7462 change_indent(INDENT_SET, 0, TRUE, 0);
7463 }
7464 else
7465 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7466
7467 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7468 did_ai = FALSE;
7469#ifdef FEAT_SMARTINDENT
7470 did_si = FALSE;
7471 can_si = FALSE;
7472 can_si_back = FALSE;
7473#endif
7474#ifdef FEAT_CINDENT
7475 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7476#endif
7477}
7478
7479 static void
7480ins_del()
7481{
7482 int temp;
7483
7484 if (stop_arrow() == FAIL)
7485 return;
7486 if (gchar_cursor() == NUL) /* delete newline */
7487 {
7488 temp = curwin->w_cursor.col;
7489 if (!can_bs(BS_EOL) /* only if "eol" included */
7490 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7491 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7492 || do_join(FALSE) == FAIL)
7493 vim_beep();
7494 else
7495 curwin->w_cursor.col = temp;
7496 }
7497 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7498 vim_beep();
7499 did_ai = FALSE;
7500#ifdef FEAT_SMARTINDENT
7501 did_si = FALSE;
7502 can_si = FALSE;
7503 can_si_back = FALSE;
7504#endif
7505 AppendCharToRedobuff(K_DEL);
7506}
7507
7508/*
7509 * Handle Backspace, delete-word and delete-line in Insert mode.
7510 * Return TRUE when backspace was actually used.
7511 */
7512 static int
7513ins_bs(c, mode, inserted_space_p)
7514 int c;
7515 int mode;
7516 int *inserted_space_p;
7517{
7518 linenr_T lnum;
7519 int cc;
7520 int temp = 0; /* init for GCC */
7521 colnr_T mincol;
7522 int did_backspace = FALSE;
7523 int in_indent;
7524 int oldState;
7525#ifdef FEAT_MBYTE
7526 int p1, p2;
7527#endif
7528
7529 /*
7530 * can't delete anything in an empty file
7531 * can't backup past first character in buffer
7532 * can't backup past starting point unless 'backspace' > 1
7533 * can backup to a previous line if 'backspace' == 0
7534 */
7535 if ( bufempty()
7536 || (
7537#ifdef FEAT_RIGHTLEFT
7538 !revins_on &&
7539#endif
7540 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7541 || (!can_bs(BS_START)
7542 && (arrow_used
7543 || (curwin->w_cursor.lnum == Insstart.lnum
7544 && curwin->w_cursor.col <= Insstart.col)))
7545 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7546 && curwin->w_cursor.col <= ai_col)
7547 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7548 {
7549 vim_beep();
7550 return FALSE;
7551 }
7552
7553 if (stop_arrow() == FAIL)
7554 return FALSE;
7555 in_indent = inindent(0);
7556#ifdef FEAT_CINDENT
7557 if (in_indent)
7558 can_cindent = FALSE;
7559#endif
7560#ifdef FEAT_COMMENTS
7561 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7562#endif
7563#ifdef FEAT_RIGHTLEFT
7564 if (revins_on) /* put cursor after last inserted char */
7565 inc_cursor();
7566#endif
7567
7568#ifdef FEAT_VIRTUALEDIT
7569 /* Virtualedit:
7570 * BACKSPACE_CHAR eats a virtual space
7571 * BACKSPACE_WORD eats all coladd
7572 * BACKSPACE_LINE eats all coladd and keeps going
7573 */
7574 if (curwin->w_cursor.coladd > 0)
7575 {
7576 if (mode == BACKSPACE_CHAR)
7577 {
7578 --curwin->w_cursor.coladd;
7579 return TRUE;
7580 }
7581 if (mode == BACKSPACE_WORD)
7582 {
7583 curwin->w_cursor.coladd = 0;
7584 return TRUE;
7585 }
7586 curwin->w_cursor.coladd = 0;
7587 }
7588#endif
7589
7590 /*
7591 * delete newline!
7592 */
7593 if (curwin->w_cursor.col == 0)
7594 {
7595 lnum = Insstart.lnum;
7596 if (curwin->w_cursor.lnum == Insstart.lnum
7597#ifdef FEAT_RIGHTLEFT
7598 || revins_on
7599#endif
7600 )
7601 {
7602 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7603 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7604 return FALSE;
7605 --Insstart.lnum;
7606 Insstart.col = MAXCOL;
7607 }
7608 /*
7609 * In replace mode:
7610 * cc < 0: NL was inserted, delete it
7611 * cc >= 0: NL was replaced, put original characters back
7612 */
7613 cc = -1;
7614 if (State & REPLACE_FLAG)
7615 cc = replace_pop(); /* returns -1 if NL was inserted */
7616 /*
7617 * In replace mode, in the line we started replacing, we only move the
7618 * cursor.
7619 */
7620 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7621 {
7622 dec_cursor();
7623 }
7624 else
7625 {
7626#ifdef FEAT_VREPLACE
7627 if (!(State & VREPLACE_FLAG)
7628 || curwin->w_cursor.lnum > orig_line_count)
7629#endif
7630 {
7631 temp = gchar_cursor(); /* remember current char */
7632 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007633
7634 /* When "aw" is in 'formatoptions' we must delete the space at
7635 * the end of the line, otherwise the line will be broken
7636 * again when auto-formatting. */
7637 if (has_format_option(FO_AUTO)
7638 && has_format_option(FO_WHITE_PAR))
7639 {
7640 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7641 TRUE);
7642 int len;
7643
7644 len = STRLEN(ptr);
7645 if (len > 0 && ptr[len - 1] == ' ')
7646 ptr[len - 1] = NUL;
7647 }
7648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 (void)do_join(FALSE);
7650 if (temp == NUL && gchar_cursor() != NUL)
7651 inc_cursor();
7652 }
7653#ifdef FEAT_VREPLACE
7654 else
7655 dec_cursor();
7656#endif
7657
7658 /*
7659 * In REPLACE mode we have to put back the text that was replaced
7660 * by the NL. On the replace stack is first a NUL-terminated
7661 * sequence of characters that were deleted and then the
7662 * characters that NL replaced.
7663 */
7664 if (State & REPLACE_FLAG)
7665 {
7666 /*
7667 * Do the next ins_char() in NORMAL state, to
7668 * prevent ins_char() from replacing characters and
7669 * avoiding showmatch().
7670 */
7671 oldState = State;
7672 State = NORMAL;
7673 /*
7674 * restore characters (blanks) deleted after cursor
7675 */
7676 while (cc > 0)
7677 {
7678 temp = curwin->w_cursor.col;
7679#ifdef FEAT_MBYTE
7680 mb_replace_pop_ins(cc);
7681#else
7682 ins_char(cc);
7683#endif
7684 curwin->w_cursor.col = temp;
7685 cc = replace_pop();
7686 }
7687 /* restore the characters that NL replaced */
7688 replace_pop_ins();
7689 State = oldState;
7690 }
7691 }
7692 did_ai = FALSE;
7693 }
7694 else
7695 {
7696 /*
7697 * Delete character(s) before the cursor.
7698 */
7699#ifdef FEAT_RIGHTLEFT
7700 if (revins_on) /* put cursor on last inserted char */
7701 dec_cursor();
7702#endif
7703 mincol = 0;
7704 /* keep indent */
7705 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7706#ifdef FEAT_RIGHTLEFT
7707 && !revins_on
7708#endif
7709 )
7710 {
7711 temp = curwin->w_cursor.col;
7712 beginline(BL_WHITE);
7713 if (curwin->w_cursor.col < (colnr_T)temp)
7714 mincol = curwin->w_cursor.col;
7715 curwin->w_cursor.col = temp;
7716 }
7717
7718 /*
7719 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7720 */
7721 if ( mode == BACKSPACE_CHAR
7722 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007723 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 && (*(ml_get_cursor() - 1) == TAB
7725 || (*(ml_get_cursor() - 1) == ' '
7726 && (!*inserted_space_p
7727 || arrow_used))))))
7728 {
7729 int ts;
7730 colnr_T vcol;
7731 colnr_T want_vcol;
7732 int extra = 0;
7733
7734 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007735 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 ts = curbuf->b_p_sw;
7737 else
7738 ts = curbuf->b_p_sts;
7739 /* Compute the virtual column where we want to be. Since
7740 * 'showbreak' may get in the way, need to get the last column of
7741 * the previous character. */
7742 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7743 dec_cursor();
7744 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7745 inc_cursor();
7746 want_vcol = (want_vcol / ts) * ts;
7747
7748 /* delete characters until we are at or before want_vcol */
7749 while (vcol > want_vcol
7750 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7751 {
7752 dec_cursor();
7753 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7754 if (State & REPLACE_FLAG)
7755 {
7756 /* Don't delete characters before the insert point when in
7757 * Replace mode */
7758 if (curwin->w_cursor.lnum != Insstart.lnum
7759 || curwin->w_cursor.col >= Insstart.col)
7760 {
7761#if 0 /* what was this for? It causes problems when sw != ts. */
7762 if (State == REPLACE && (int)vcol < want_vcol)
7763 {
7764 (void)del_char(FALSE);
7765 extra = 2; /* don't pop too much */
7766 }
7767 else
7768#endif
7769 replace_do_bs();
7770 }
7771 }
7772 else
7773 (void)del_char(FALSE);
7774 }
7775
7776 /* insert extra spaces until we are at want_vcol */
7777 while (vcol < want_vcol)
7778 {
7779 /* Remember the first char we inserted */
7780 if (curwin->w_cursor.lnum == Insstart.lnum
7781 && curwin->w_cursor.col < Insstart.col)
7782 Insstart.col = curwin->w_cursor.col;
7783
7784#ifdef FEAT_VREPLACE
7785 if (State & VREPLACE_FLAG)
7786 ins_char(' ');
7787 else
7788#endif
7789 {
7790 ins_str((char_u *)" ");
7791 if ((State & REPLACE_FLAG) && extra <= 1)
7792 {
7793 if (extra)
7794 replace_push_off(NUL);
7795 else
7796 replace_push(NUL);
7797 }
7798 if (extra == 2)
7799 extra = 1;
7800 }
7801 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7802 }
7803 }
7804
7805 /*
7806 * Delete upto starting point, start of line or previous word.
7807 */
7808 else do
7809 {
7810#ifdef FEAT_RIGHTLEFT
7811 if (!revins_on) /* put cursor on char to be deleted */
7812#endif
7813 dec_cursor();
7814
7815 /* start of word? */
7816 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7817 {
7818 mode = BACKSPACE_WORD_NOT_SPACE;
7819 temp = vim_iswordc(gchar_cursor());
7820 }
7821 /* end of word? */
7822 else if (mode == BACKSPACE_WORD_NOT_SPACE
7823 && (vim_isspace(cc = gchar_cursor())
7824 || vim_iswordc(cc) != temp))
7825 {
7826#ifdef FEAT_RIGHTLEFT
7827 if (!revins_on)
7828#endif
7829 inc_cursor();
7830#ifdef FEAT_RIGHTLEFT
7831 else if (State & REPLACE_FLAG)
7832 dec_cursor();
7833#endif
7834 break;
7835 }
7836 if (State & REPLACE_FLAG)
7837 replace_do_bs();
7838 else
7839 {
7840#ifdef FEAT_MBYTE
7841 if (enc_utf8 && p_deco)
7842 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7843#endif
7844 (void)del_char(FALSE);
7845#ifdef FEAT_MBYTE
7846 /*
7847 * If p1 or p2 is non-zero, there are combining characters we
7848 * need to take account of. Don't back up before the base
7849 * character.
7850 */
7851 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7852 inc_cursor();
7853#endif
7854#ifdef FEAT_RIGHTLEFT
7855 if (revins_chars)
7856 {
7857 revins_chars--;
7858 revins_legal++;
7859 }
7860 if (revins_on && gchar_cursor() == NUL)
7861 break;
7862#endif
7863 }
7864 /* Just a single backspace?: */
7865 if (mode == BACKSPACE_CHAR)
7866 break;
7867 } while (
7868#ifdef FEAT_RIGHTLEFT
7869 revins_on ||
7870#endif
7871 (curwin->w_cursor.col > mincol
7872 && (curwin->w_cursor.lnum != Insstart.lnum
7873 || curwin->w_cursor.col != Insstart.col)));
7874 did_backspace = TRUE;
7875 }
7876#ifdef FEAT_SMARTINDENT
7877 did_si = FALSE;
7878 can_si = FALSE;
7879 can_si_back = FALSE;
7880#endif
7881 if (curwin->w_cursor.col <= 1)
7882 did_ai = FALSE;
7883 /*
7884 * It's a little strange to put backspaces into the redo
7885 * buffer, but it makes auto-indent a lot easier to deal
7886 * with.
7887 */
7888 AppendCharToRedobuff(c);
7889
7890 /* If deleted before the insertion point, adjust it */
7891 if (curwin->w_cursor.lnum == Insstart.lnum
7892 && curwin->w_cursor.col < Insstart.col)
7893 Insstart.col = curwin->w_cursor.col;
7894
7895 /* vi behaviour: the cursor moves backward but the character that
7896 * was there remains visible
7897 * Vim behaviour: the cursor moves backward and the character that
7898 * was there is erased from the screen.
7899 * We can emulate the vi behaviour by pretending there is a dollar
7900 * displayed even when there isn't.
7901 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7902 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7903 dollar_vcol = curwin->w_virtcol;
7904
7905 return did_backspace;
7906}
7907
7908#ifdef FEAT_MOUSE
7909 static void
7910ins_mouse(c)
7911 int c;
7912{
7913 pos_T tpos;
7914
7915# ifdef FEAT_GUI
7916 /* When GUI is active, also move/paste when 'mouse' is empty */
7917 if (!gui.in_use)
7918# endif
7919 if (!mouse_has(MOUSE_INSERT))
7920 return;
7921
7922 undisplay_dollar();
7923 tpos = curwin->w_cursor;
7924 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7925 {
7926 start_arrow(&tpos);
7927# ifdef FEAT_CINDENT
7928 can_cindent = TRUE;
7929# endif
7930 }
7931
7932#ifdef FEAT_WINDOWS
7933 /* redraw status lines (in case another window became active) */
7934 redraw_statuslines();
7935#endif
7936}
7937
7938 static void
7939ins_mousescroll(up)
7940 int up;
7941{
7942 pos_T tpos;
7943# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7944 win_T *old_curwin;
7945# endif
7946
7947 tpos = curwin->w_cursor;
7948
7949# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7950 old_curwin = curwin;
7951
7952 /* Currently the mouse coordinates are only known in the GUI. */
7953 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7954 {
7955 int row, col;
7956
7957 row = mouse_row;
7958 col = mouse_col;
7959
7960 /* find the window at the pointer coordinates */
7961 curwin = mouse_find_win(&row, &col);
7962 curbuf = curwin->w_buffer;
7963 }
7964 if (curwin == old_curwin)
7965# endif
7966 undisplay_dollar();
7967
7968 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7969 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7970 else
7971 scroll_redraw(up, 3L);
7972
7973# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7974 curwin->w_redr_status = TRUE;
7975
7976 curwin = old_curwin;
7977 curbuf = curwin->w_buffer;
7978# endif
7979
7980 if (!equalpos(curwin->w_cursor, tpos))
7981 {
7982 start_arrow(&tpos);
7983# ifdef FEAT_CINDENT
7984 can_cindent = TRUE;
7985# endif
7986 }
7987}
7988#endif
7989
Bram Moolenaara23ccb82006-02-27 00:08:02 +00007990#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7991 void
7992ins_tabline(c)
7993 int c;
7994{
7995 /* We will be leaving the current window, unless closing another tab. */
7996 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
7997 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
7998 {
7999 undisplay_dollar();
8000 start_arrow(&curwin->w_cursor);
8001# ifdef FEAT_CINDENT
8002 can_cindent = TRUE;
8003# endif
8004 }
8005
8006 if (c == K_TABLINE)
8007 goto_tabpage(current_tab);
8008 else
8009 handle_tabmenu();
8010
8011}
8012#endif
8013
8014#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 void
8016ins_scroll()
8017{
8018 pos_T tpos;
8019
8020 undisplay_dollar();
8021 tpos = curwin->w_cursor;
8022 if (gui_do_scroll())
8023 {
8024 start_arrow(&tpos);
8025# ifdef FEAT_CINDENT
8026 can_cindent = TRUE;
8027# endif
8028 }
8029}
8030
8031 void
8032ins_horscroll()
8033{
8034 pos_T tpos;
8035
8036 undisplay_dollar();
8037 tpos = curwin->w_cursor;
8038 if (gui_do_horiz_scroll())
8039 {
8040 start_arrow(&tpos);
8041# ifdef FEAT_CINDENT
8042 can_cindent = TRUE;
8043# endif
8044 }
8045}
8046#endif
8047
8048 static void
8049ins_left()
8050{
8051 pos_T tpos;
8052
8053#ifdef FEAT_FOLDING
8054 if ((fdo_flags & FDO_HOR) && KeyTyped)
8055 foldOpenCursor();
8056#endif
8057 undisplay_dollar();
8058 tpos = curwin->w_cursor;
8059 if (oneleft() == OK)
8060 {
8061 start_arrow(&tpos);
8062#ifdef FEAT_RIGHTLEFT
8063 /* If exit reversed string, position is fixed */
8064 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8065 revins_legal++;
8066 revins_chars++;
8067#endif
8068 }
8069
8070 /*
8071 * if 'whichwrap' set for cursor in insert mode may go to
8072 * previous line
8073 */
8074 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8075 {
8076 start_arrow(&tpos);
8077 --(curwin->w_cursor.lnum);
8078 coladvance((colnr_T)MAXCOL);
8079 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8080 }
8081 else
8082 vim_beep();
8083}
8084
8085 static void
8086ins_home(c)
8087 int c;
8088{
8089 pos_T tpos;
8090
8091#ifdef FEAT_FOLDING
8092 if ((fdo_flags & FDO_HOR) && KeyTyped)
8093 foldOpenCursor();
8094#endif
8095 undisplay_dollar();
8096 tpos = curwin->w_cursor;
8097 if (c == K_C_HOME)
8098 curwin->w_cursor.lnum = 1;
8099 curwin->w_cursor.col = 0;
8100#ifdef FEAT_VIRTUALEDIT
8101 curwin->w_cursor.coladd = 0;
8102#endif
8103 curwin->w_curswant = 0;
8104 start_arrow(&tpos);
8105}
8106
8107 static void
8108ins_end(c)
8109 int c;
8110{
8111 pos_T tpos;
8112
8113#ifdef FEAT_FOLDING
8114 if ((fdo_flags & FDO_HOR) && KeyTyped)
8115 foldOpenCursor();
8116#endif
8117 undisplay_dollar();
8118 tpos = curwin->w_cursor;
8119 if (c == K_C_END)
8120 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8121 coladvance((colnr_T)MAXCOL);
8122 curwin->w_curswant = MAXCOL;
8123
8124 start_arrow(&tpos);
8125}
8126
8127 static void
8128ins_s_left()
8129{
8130#ifdef FEAT_FOLDING
8131 if ((fdo_flags & FDO_HOR) && KeyTyped)
8132 foldOpenCursor();
8133#endif
8134 undisplay_dollar();
8135 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8136 {
8137 start_arrow(&curwin->w_cursor);
8138 (void)bck_word(1L, FALSE, FALSE);
8139 curwin->w_set_curswant = TRUE;
8140 }
8141 else
8142 vim_beep();
8143}
8144
8145 static void
8146ins_right()
8147{
8148#ifdef FEAT_FOLDING
8149 if ((fdo_flags & FDO_HOR) && KeyTyped)
8150 foldOpenCursor();
8151#endif
8152 undisplay_dollar();
8153 if (gchar_cursor() != NUL || virtual_active()
8154 )
8155 {
8156 start_arrow(&curwin->w_cursor);
8157 curwin->w_set_curswant = TRUE;
8158#ifdef FEAT_VIRTUALEDIT
8159 if (virtual_active())
8160 oneright();
8161 else
8162#endif
8163 {
8164#ifdef FEAT_MBYTE
8165 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008166 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 else
8168#endif
8169 ++curwin->w_cursor.col;
8170 }
8171
8172#ifdef FEAT_RIGHTLEFT
8173 revins_legal++;
8174 if (revins_chars)
8175 revins_chars--;
8176#endif
8177 }
8178 /* if 'whichwrap' set for cursor in insert mode, may move the
8179 * cursor to the next line */
8180 else if (vim_strchr(p_ww, ']') != NULL
8181 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8182 {
8183 start_arrow(&curwin->w_cursor);
8184 curwin->w_set_curswant = TRUE;
8185 ++curwin->w_cursor.lnum;
8186 curwin->w_cursor.col = 0;
8187 }
8188 else
8189 vim_beep();
8190}
8191
8192 static void
8193ins_s_right()
8194{
8195#ifdef FEAT_FOLDING
8196 if ((fdo_flags & FDO_HOR) && KeyTyped)
8197 foldOpenCursor();
8198#endif
8199 undisplay_dollar();
8200 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8201 || gchar_cursor() != NUL)
8202 {
8203 start_arrow(&curwin->w_cursor);
8204 (void)fwd_word(1L, FALSE, 0);
8205 curwin->w_set_curswant = TRUE;
8206 }
8207 else
8208 vim_beep();
8209}
8210
8211 static void
8212ins_up(startcol)
8213 int startcol; /* when TRUE move to Insstart.col */
8214{
8215 pos_T tpos;
8216 linenr_T old_topline = curwin->w_topline;
8217#ifdef FEAT_DIFF
8218 int old_topfill = curwin->w_topfill;
8219#endif
8220
8221 undisplay_dollar();
8222 tpos = curwin->w_cursor;
8223 if (cursor_up(1L, TRUE) == OK)
8224 {
8225 if (startcol)
8226 coladvance(getvcol_nolist(&Insstart));
8227 if (old_topline != curwin->w_topline
8228#ifdef FEAT_DIFF
8229 || old_topfill != curwin->w_topfill
8230#endif
8231 )
8232 redraw_later(VALID);
8233 start_arrow(&tpos);
8234#ifdef FEAT_CINDENT
8235 can_cindent = TRUE;
8236#endif
8237 }
8238 else
8239 vim_beep();
8240}
8241
8242 static void
8243ins_pageup()
8244{
8245 pos_T tpos;
8246
8247 undisplay_dollar();
8248 tpos = curwin->w_cursor;
8249 if (onepage(BACKWARD, 1L) == OK)
8250 {
8251 start_arrow(&tpos);
8252#ifdef FEAT_CINDENT
8253 can_cindent = TRUE;
8254#endif
8255 }
8256 else
8257 vim_beep();
8258}
8259
8260 static void
8261ins_down(startcol)
8262 int startcol; /* when TRUE move to Insstart.col */
8263{
8264 pos_T tpos;
8265 linenr_T old_topline = curwin->w_topline;
8266#ifdef FEAT_DIFF
8267 int old_topfill = curwin->w_topfill;
8268#endif
8269
8270 undisplay_dollar();
8271 tpos = curwin->w_cursor;
8272 if (cursor_down(1L, TRUE) == OK)
8273 {
8274 if (startcol)
8275 coladvance(getvcol_nolist(&Insstart));
8276 if (old_topline != curwin->w_topline
8277#ifdef FEAT_DIFF
8278 || old_topfill != curwin->w_topfill
8279#endif
8280 )
8281 redraw_later(VALID);
8282 start_arrow(&tpos);
8283#ifdef FEAT_CINDENT
8284 can_cindent = TRUE;
8285#endif
8286 }
8287 else
8288 vim_beep();
8289}
8290
8291 static void
8292ins_pagedown()
8293{
8294 pos_T tpos;
8295
8296 undisplay_dollar();
8297 tpos = curwin->w_cursor;
8298 if (onepage(FORWARD, 1L) == OK)
8299 {
8300 start_arrow(&tpos);
8301#ifdef FEAT_CINDENT
8302 can_cindent = TRUE;
8303#endif
8304 }
8305 else
8306 vim_beep();
8307}
8308
8309#ifdef FEAT_DND
8310 static void
8311ins_drop()
8312{
8313 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8314}
8315#endif
8316
8317/*
8318 * Handle TAB in Insert or Replace mode.
8319 * Return TRUE when the TAB needs to be inserted like a normal character.
8320 */
8321 static int
8322ins_tab()
8323{
8324 int ind;
8325 int i;
8326 int temp;
8327
8328 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8329 Insstart_blank_vcol = get_nolist_virtcol();
8330 if (echeck_abbr(TAB + ABBR_OFF))
8331 return FALSE;
8332
8333 ind = inindent(0);
8334#ifdef FEAT_CINDENT
8335 if (ind)
8336 can_cindent = FALSE;
8337#endif
8338
8339 /*
8340 * When nothing special, insert TAB like a normal character
8341 */
8342 if (!curbuf->b_p_et
8343 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8344 && curbuf->b_p_sts == 0)
8345 return TRUE;
8346
8347 if (stop_arrow() == FAIL)
8348 return TRUE;
8349
8350 did_ai = FALSE;
8351#ifdef FEAT_SMARTINDENT
8352 did_si = FALSE;
8353 can_si = FALSE;
8354 can_si_back = FALSE;
8355#endif
8356 AppendToRedobuff((char_u *)"\t");
8357
8358 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8359 temp = (int)curbuf->b_p_sw;
8360 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8361 temp = (int)curbuf->b_p_sts;
8362 else /* otherwise use 'tabstop' */
8363 temp = (int)curbuf->b_p_ts;
8364 temp -= get_nolist_virtcol() % temp;
8365
8366 /*
8367 * Insert the first space with ins_char(). It will delete one char in
8368 * replace mode. Insert the rest with ins_str(); it will not delete any
8369 * chars. For VREPLACE mode, we use ins_char() for all characters.
8370 */
8371 ins_char(' ');
8372 while (--temp > 0)
8373 {
8374#ifdef FEAT_VREPLACE
8375 if (State & VREPLACE_FLAG)
8376 ins_char(' ');
8377 else
8378#endif
8379 {
8380 ins_str((char_u *)" ");
8381 if (State & REPLACE_FLAG) /* no char replaced */
8382 replace_push(NUL);
8383 }
8384 }
8385
8386 /*
8387 * When 'expandtab' not set: Replace spaces by TABs where possible.
8388 */
8389 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8390 {
8391 char_u *ptr;
8392#ifdef FEAT_VREPLACE
8393 char_u *saved_line = NULL; /* init for GCC */
8394 pos_T pos;
8395#endif
8396 pos_T fpos;
8397 pos_T *cursor;
8398 colnr_T want_vcol, vcol;
8399 int change_col = -1;
8400 int save_list = curwin->w_p_list;
8401
8402 /*
8403 * Get the current line. For VREPLACE mode, don't make real changes
8404 * yet, just work on a copy of the line.
8405 */
8406#ifdef FEAT_VREPLACE
8407 if (State & VREPLACE_FLAG)
8408 {
8409 pos = curwin->w_cursor;
8410 cursor = &pos;
8411 saved_line = vim_strsave(ml_get_curline());
8412 if (saved_line == NULL)
8413 return FALSE;
8414 ptr = saved_line + pos.col;
8415 }
8416 else
8417#endif
8418 {
8419 ptr = ml_get_cursor();
8420 cursor = &curwin->w_cursor;
8421 }
8422
8423 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8424 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8425 curwin->w_p_list = FALSE;
8426
8427 /* Find first white before the cursor */
8428 fpos = curwin->w_cursor;
8429 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8430 {
8431 --fpos.col;
8432 --ptr;
8433 }
8434
8435 /* In Replace mode, don't change characters before the insert point. */
8436 if ((State & REPLACE_FLAG)
8437 && fpos.lnum == Insstart.lnum
8438 && fpos.col < Insstart.col)
8439 {
8440 ptr += Insstart.col - fpos.col;
8441 fpos.col = Insstart.col;
8442 }
8443
8444 /* compute virtual column numbers of first white and cursor */
8445 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8446 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8447
8448 /* Use as many TABs as possible. Beware of 'showbreak' and
8449 * 'linebreak' adding extra virtual columns. */
8450 while (vim_iswhite(*ptr))
8451 {
8452 i = lbr_chartabsize((char_u *)"\t", vcol);
8453 if (vcol + i > want_vcol)
8454 break;
8455 if (*ptr != TAB)
8456 {
8457 *ptr = TAB;
8458 if (change_col < 0)
8459 {
8460 change_col = fpos.col; /* Column of first change */
8461 /* May have to adjust Insstart */
8462 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8463 Insstart.col = fpos.col;
8464 }
8465 }
8466 ++fpos.col;
8467 ++ptr;
8468 vcol += i;
8469 }
8470
8471 if (change_col >= 0)
8472 {
8473 int repl_off = 0;
8474
8475 /* Skip over the spaces we need. */
8476 while (vcol < want_vcol && *ptr == ' ')
8477 {
8478 vcol += lbr_chartabsize(ptr, vcol);
8479 ++ptr;
8480 ++repl_off;
8481 }
8482 if (vcol > want_vcol)
8483 {
8484 /* Must have a char with 'showbreak' just before it. */
8485 --ptr;
8486 --repl_off;
8487 }
8488 fpos.col += repl_off;
8489
8490 /* Delete following spaces. */
8491 i = cursor->col - fpos.col;
8492 if (i > 0)
8493 {
8494 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8495 /* correct replace stack. */
8496 if ((State & REPLACE_FLAG)
8497#ifdef FEAT_VREPLACE
8498 && !(State & VREPLACE_FLAG)
8499#endif
8500 )
8501 for (temp = i; --temp >= 0; )
8502 replace_join(repl_off);
8503 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008504#ifdef FEAT_NETBEANS_INTG
8505 if (usingNetbeans)
8506 {
8507 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8508 (long)(i + 1));
8509 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8510 (char_u *)"\t", 1);
8511 }
8512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 cursor->col -= i;
8514
8515#ifdef FEAT_VREPLACE
8516 /*
8517 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8518 * backspacing over the changed spacing and then inserting the new
8519 * spacing.
8520 */
8521 if (State & VREPLACE_FLAG)
8522 {
8523 /* Backspace from real cursor to change_col */
8524 backspace_until_column(change_col);
8525
8526 /* Insert each char in saved_line from changed_col to
8527 * ptr-cursor */
8528 ins_bytes_len(saved_line + change_col,
8529 cursor->col - change_col);
8530 }
8531#endif
8532 }
8533
8534#ifdef FEAT_VREPLACE
8535 if (State & VREPLACE_FLAG)
8536 vim_free(saved_line);
8537#endif
8538 curwin->w_p_list = save_list;
8539 }
8540
8541 return FALSE;
8542}
8543
8544/*
8545 * Handle CR or NL in insert mode.
8546 * Return TRUE when out of memory or can't undo.
8547 */
8548 static int
8549ins_eol(c)
8550 int c;
8551{
8552 int i;
8553
8554 if (echeck_abbr(c + ABBR_OFF))
8555 return FALSE;
8556 if (stop_arrow() == FAIL)
8557 return TRUE;
8558 undisplay_dollar();
8559
8560 /*
8561 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8562 * character under the cursor. Only push a NUL on the replace stack,
8563 * nothing to put back when the NL is deleted.
8564 */
8565 if ((State & REPLACE_FLAG)
8566#ifdef FEAT_VREPLACE
8567 && !(State & VREPLACE_FLAG)
8568#endif
8569 )
8570 replace_push(NUL);
8571
8572 /*
8573 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8574 * replacing the next line, so we push all of the characters left on the
8575 * line onto the replace stack. This is not done here though, it is done
8576 * in open_line().
8577 */
8578
8579#ifdef FEAT_RIGHTLEFT
8580# ifdef FEAT_FKMAP
8581 if (p_altkeymap && p_fkmap)
8582 fkmap(NL);
8583# endif
8584 /* NL in reverse insert will always start in the end of
8585 * current line. */
8586 if (revins_on)
8587 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8588#endif
8589
8590 AppendToRedobuff(NL_STR);
8591 i = open_line(FORWARD,
8592#ifdef FEAT_COMMENTS
8593 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8594#endif
8595 0, old_indent);
8596 old_indent = 0;
8597#ifdef FEAT_CINDENT
8598 can_cindent = TRUE;
8599#endif
8600
8601 return (!i);
8602}
8603
8604#ifdef FEAT_DIGRAPHS
8605/*
8606 * Handle digraph in insert mode.
8607 * Returns character still to be inserted, or NUL when nothing remaining to be
8608 * done.
8609 */
8610 static int
8611ins_digraph()
8612{
8613 int c;
8614 int cc;
8615
8616 pc_status = PC_STATUS_UNSET;
8617 if (redrawing() && !char_avail())
8618 {
8619 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008620 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
8622 edit_putchar('?', TRUE);
8623#ifdef FEAT_CMDL_INFO
8624 add_to_showcmd_c(Ctrl_K);
8625#endif
8626 }
8627
8628#ifdef USE_ON_FLY_SCROLL
8629 dont_scroll = TRUE; /* disallow scrolling here */
8630#endif
8631
8632 /* don't map the digraph chars. This also prevents the
8633 * mode message to be deleted when ESC is hit */
8634 ++no_mapping;
8635 ++allow_keys;
8636 c = safe_vgetc();
8637 --no_mapping;
8638 --allow_keys;
8639 if (IS_SPECIAL(c) || mod_mask) /* special key */
8640 {
8641#ifdef FEAT_CMDL_INFO
8642 clear_showcmd();
8643#endif
8644 insert_special(c, TRUE, FALSE);
8645 return NUL;
8646 }
8647 if (c != ESC)
8648 {
8649 if (redrawing() && !char_avail())
8650 {
8651 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008652 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 if (char2cells(c) == 1)
8655 {
8656 /* first remove the '?', otherwise it's restored when typing
8657 * an ESC next */
8658 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008659 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 edit_putchar(c, TRUE);
8661 }
8662#ifdef FEAT_CMDL_INFO
8663 add_to_showcmd_c(c);
8664#endif
8665 }
8666 ++no_mapping;
8667 ++allow_keys;
8668 cc = safe_vgetc();
8669 --no_mapping;
8670 --allow_keys;
8671 if (cc != ESC)
8672 {
8673 AppendToRedobuff((char_u *)CTRL_V_STR);
8674 c = getdigraph(c, cc, TRUE);
8675#ifdef FEAT_CMDL_INFO
8676 clear_showcmd();
8677#endif
8678 return c;
8679 }
8680 }
8681 edit_unputchar();
8682#ifdef FEAT_CMDL_INFO
8683 clear_showcmd();
8684#endif
8685 return NUL;
8686}
8687#endif
8688
8689/*
8690 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8691 * Returns the char to be inserted, or NUL if none found.
8692 */
8693 static int
8694ins_copychar(lnum)
8695 linenr_T lnum;
8696{
8697 int c;
8698 int temp;
8699 char_u *ptr, *prev_ptr;
8700
8701 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8702 {
8703 vim_beep();
8704 return NUL;
8705 }
8706
8707 /* try to advance to the cursor column */
8708 temp = 0;
8709 ptr = ml_get(lnum);
8710 prev_ptr = ptr;
8711 validate_virtcol();
8712 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8713 {
8714 prev_ptr = ptr;
8715 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8716 }
8717 if ((colnr_T)temp > curwin->w_virtcol)
8718 ptr = prev_ptr;
8719
8720#ifdef FEAT_MBYTE
8721 c = (*mb_ptr2char)(ptr);
8722#else
8723 c = *ptr;
8724#endif
8725 if (c == NUL)
8726 vim_beep();
8727 return c;
8728}
8729
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008730/*
8731 * CTRL-Y or CTRL-E typed in Insert mode.
8732 */
8733 static int
8734ins_ctrl_ey(tc)
8735 int tc;
8736{
8737 int c = tc;
8738
8739#ifdef FEAT_INS_EXPAND
8740 if (ctrl_x_mode == CTRL_X_SCROLL)
8741 {
8742 if (c == Ctrl_Y)
8743 scrolldown_clamp();
8744 else
8745 scrollup_clamp();
8746 redraw_later(VALID);
8747 }
8748 else
8749#endif
8750 {
8751 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8752 if (c != NUL)
8753 {
8754 long tw_save;
8755
8756 /* The character must be taken literally, insert like it
8757 * was typed after a CTRL-V, and pretend 'textwidth'
8758 * wasn't set. Digits, 'o' and 'x' are special after a
8759 * CTRL-V, don't use it for these. */
8760 if (c < 256 && !isalnum(c))
8761 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8762 tw_save = curbuf->b_p_tw;
8763 curbuf->b_p_tw = -1;
8764 insert_special(c, TRUE, FALSE);
8765 curbuf->b_p_tw = tw_save;
8766#ifdef FEAT_RIGHTLEFT
8767 revins_chars++;
8768 revins_legal++;
8769#endif
8770 c = Ctrl_V; /* pretend CTRL-V is last character */
8771 auto_format(FALSE, TRUE);
8772 }
8773 }
8774 return c;
8775}
8776
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777#ifdef FEAT_SMARTINDENT
8778/*
8779 * Try to do some very smart auto-indenting.
8780 * Used when inserting a "normal" character.
8781 */
8782 static void
8783ins_try_si(c)
8784 int c;
8785{
8786 pos_T *pos, old_pos;
8787 char_u *ptr;
8788 int i;
8789 int temp;
8790
8791 /*
8792 * do some very smart indenting when entering '{' or '}'
8793 */
8794 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8795 {
8796 /*
8797 * for '}' set indent equal to indent of line containing matching '{'
8798 */
8799 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8800 {
8801 old_pos = curwin->w_cursor;
8802 /*
8803 * If the matching '{' has a ')' immediately before it (ignoring
8804 * white-space), then line up with the start of the line
8805 * containing the matching '(' if there is one. This handles the
8806 * case where an "if (..\n..) {" statement continues over multiple
8807 * lines -- webb
8808 */
8809 ptr = ml_get(pos->lnum);
8810 i = pos->col;
8811 if (i > 0) /* skip blanks before '{' */
8812 while (--i > 0 && vim_iswhite(ptr[i]))
8813 ;
8814 curwin->w_cursor.lnum = pos->lnum;
8815 curwin->w_cursor.col = i;
8816 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8817 curwin->w_cursor = *pos;
8818 i = get_indent();
8819 curwin->w_cursor = old_pos;
8820#ifdef FEAT_VREPLACE
8821 if (State & VREPLACE_FLAG)
8822 change_indent(INDENT_SET, i, FALSE, NUL);
8823 else
8824#endif
8825 (void)set_indent(i, SIN_CHANGED);
8826 }
8827 else if (curwin->w_cursor.col > 0)
8828 {
8829 /*
8830 * when inserting '{' after "O" reduce indent, but not
8831 * more than indent of previous line
8832 */
8833 temp = TRUE;
8834 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8835 {
8836 old_pos = curwin->w_cursor;
8837 i = get_indent();
8838 while (curwin->w_cursor.lnum > 1)
8839 {
8840 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8841
8842 /* ignore empty lines and lines starting with '#'. */
8843 if (*ptr != '#' && *ptr != NUL)
8844 break;
8845 }
8846 if (get_indent() >= i)
8847 temp = FALSE;
8848 curwin->w_cursor = old_pos;
8849 }
8850 if (temp)
8851 shift_line(TRUE, FALSE, 1);
8852 }
8853 }
8854
8855 /*
8856 * set indent of '#' always to 0
8857 */
8858 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8859 {
8860 /* remember current indent for next line */
8861 old_indent = get_indent();
8862 (void)set_indent(0, SIN_CHANGED);
8863 }
8864
8865 /* Adjust ai_col, the char at this position can be deleted. */
8866 if (ai_col > curwin->w_cursor.col)
8867 ai_col = curwin->w_cursor.col;
8868}
8869#endif
8870
8871/*
8872 * Get the value that w_virtcol would have when 'list' is off.
8873 * Unless 'cpo' contains the 'L' flag.
8874 */
8875 static colnr_T
8876get_nolist_virtcol()
8877{
8878 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8879 return getvcol_nolist(&curwin->w_cursor);
8880 validate_virtcol();
8881 return curwin->w_virtcol;
8882}