blob: 8dc07fdf97e6b44d8e88fc8239b49f8970492787 [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 Moolenaar572cb562005-08-05 21:35:02 +000065typedef struct Completion compl_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066struct Completion
67{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
71 char_u *cp_fname; /* file containing the match */
72 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
73 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000074};
75
Bram Moolenaar572cb562005-08-05 21:35:02 +000076#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#define FREE_FNAME (2)
78
79/*
80 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000081 * "compl_first_match" points to the start of the list.
82 * "compl_curr_match" points to the currently selected entry.
83 * "compl_shown_match" is different from compl_curr_match during
84 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000086static compl_T *compl_first_match = NULL;
87static compl_T *compl_curr_match = NULL;
88static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
Bram Moolenaar4be06f92005-07-29 22:36:03 +000090/* When the first completion is done "compl_started" is set. When it's
91 * FALSE the word to be completed must be located. */
92static int compl_started = FALSE;
93
Bram Moolenaar572cb562005-08-05 21:35:02 +000094static int compl_matches = 0;
95static char_u *compl_pattern = NULL;
96static int compl_direction = FORWARD;
97static int compl_shows_dir = FORWARD;
98static int compl_pending = FALSE;
99static pos_T compl_startpos;
100static colnr_T compl_col = 0; /* column where the text starts
101 * that is being completed */
102static int save_sm = -1;
103static char_u *compl_orig_text = NULL; /* text as it was before
104 * completion started */
105static int compl_cont_mode = 0;
106static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000108static void ins_ctrl_x __ARGS((void));
109static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir));
111static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000112static void ins_compl_upd_pum __ARGS((void));
113static void ins_compl_del_pum __ARGS((void));
114static int pum_wanted __ARGS((void));
115static void ins_compl_show_pum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
117static void ins_compl_free __ARGS((void));
118static void ins_compl_clear __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000119static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
121static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
122static void ins_compl_delete __ARGS((void));
123static void ins_compl_insert __ARGS((void));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000124static int ins_compl_next __ARGS((int allow_get_expansion, int count));
125static int ins_compl_key2dir __ARGS((int c));
126static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int ins_complete __ARGS((int c));
128static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
129#endif /* FEAT_INS_EXPAND */
130
131#define BACKSPACE_CHAR 1
132#define BACKSPACE_WORD 2
133#define BACKSPACE_WORD_NOT_SPACE 3
134#define BACKSPACE_LINE 4
135
136static void ins_redraw __ARGS((void));
137static void ins_ctrl_v __ARGS((void));
138static void undisplay_dollar __ARGS((void));
139static void insert_special __ARGS((int, int, int));
140static void check_auto_format __ARGS((int));
141static void redo_literal __ARGS((int c));
142static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000143#ifdef FEAT_SYN_HL
144static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000145static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000146static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
149static int echeck_abbr __ARGS((int));
150static void replace_push_off __ARGS((int c));
151static int replace_pop __ARGS((void));
152static void replace_join __ARGS((int off));
153static void replace_pop_ins __ARGS((void));
154#ifdef FEAT_MBYTE
155static void mb_replace_pop_ins __ARGS((int cc));
156#endif
157static void replace_flush __ARGS((void));
158static void replace_do_bs __ARGS((void));
159#ifdef FEAT_CINDENT
160static int cindent_on __ARGS((void));
161#endif
162static void ins_reg __ARGS((void));
163static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000164static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000165static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_RIGHTLEFT
167static void ins_ctrl_ __ARGS((void));
168#endif
169#ifdef FEAT_VISUAL
170static int ins_start_select __ARGS((int c));
171#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000172static void ins_insert __ARGS((int replaceState));
173static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void ins_shift __ARGS((int c, int lastc));
175static void ins_del __ARGS((void));
176static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
177#ifdef FEAT_MOUSE
178static void ins_mouse __ARGS((int c));
179static void ins_mousescroll __ARGS((int up));
180#endif
181static void ins_left __ARGS((void));
182static void ins_home __ARGS((int c));
183static void ins_end __ARGS((int c));
184static void ins_s_left __ARGS((void));
185static void ins_right __ARGS((void));
186static void ins_s_right __ARGS((void));
187static void ins_up __ARGS((int startcol));
188static void ins_pageup __ARGS((void));
189static void ins_down __ARGS((int startcol));
190static void ins_pagedown __ARGS((void));
191#ifdef FEAT_DND
192static void ins_drop __ARGS((void));
193#endif
194static int ins_tab __ARGS((void));
195static int ins_eol __ARGS((int c));
196#ifdef FEAT_DIGRAPHS
197static int ins_digraph __ARGS((void));
198#endif
199static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000200static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef FEAT_SMARTINDENT
202static void ins_try_si __ARGS((int c));
203#endif
204static colnr_T get_nolist_virtcol __ARGS((void));
205
206static colnr_T Insstart_textlen; /* length of line when insert started */
207static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
208
209static char_u *last_insert = NULL; /* the text of the previous insert,
210 K_SPECIAL and CSI are escaped */
211static int last_insert_skip; /* nr of chars in front of previous insert */
212static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000213static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifdef FEAT_CINDENT
216static int can_cindent; /* may do cindenting on this line */
217#endif
218
219static int old_indent = 0; /* for ^^D command in insert mode */
220
221#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000222static int revins_on; /* reverse insert mode on */
223static int revins_chars; /* how much to skip after edit */
224static int revins_legal; /* was the last char 'legal'? */
225static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static int ins_need_undo; /* call u_save() before inserting a
229 char. Set when edit() is called.
230 after that arrow_used is used. */
231
232static int did_add_space = FALSE; /* auto_format() added an extra space
233 under the cursor */
234
235/*
236 * edit(): Start inserting text.
237 *
238 * "cmdchar" can be:
239 * 'i' normal insert command
240 * 'a' normal append command
241 * 'R' replace command
242 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
243 * but still only one <CR> is inserted. The <Esc> is not used for redo.
244 * 'g' "gI" command.
245 * 'V' "gR" command for Virtual Replace mode.
246 * 'v' "gr" command for single character Virtual Replace mode.
247 *
248 * This function is not called recursively. For CTRL-O commands, it returns
249 * and lets the caller handle the Normal-mode command.
250 *
251 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
252 */
253 int
254edit(cmdchar, startln, count)
255 int cmdchar;
256 int startln; /* if set, insert at start of line */
257 long count;
258{
259 int c = 0;
260 char_u *ptr;
261 int lastc;
262 colnr_T mincol;
263 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 int i;
265 int did_backspace = TRUE; /* previous char was backspace */
266#ifdef FEAT_CINDENT
267 int line_is_white = FALSE; /* line is empty before insert */
268#endif
269 linenr_T old_topline = 0; /* topline before insertion */
270#ifdef FEAT_DIFF
271 int old_topfill = -1;
272#endif
273 int inserted_space = FALSE; /* just inserted a space */
274 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000275 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000277 /* Remember whether editing was restarted after CTRL-O. */
278 did_restart_edit = restart_edit;
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 /* sleep before redrawing, needed for "CTRL-O :" that results in an
281 * error message */
282 check_for_delay(TRUE);
283
284#ifdef HAVE_SANDBOX
285 /* Don't allow inserting in the sandbox. */
286 if (sandbox != 0)
287 {
288 EMSG(_(e_sandbox));
289 return FALSE;
290 }
291#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000292 /* Don't allow changes in the buffer while editing the cmdline. The
293 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000294 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000295 {
296 EMSG(_(e_secure));
297 return FALSE;
298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300#ifdef FEAT_INS_EXPAND
301 ins_compl_clear(); /* clear stuff for CTRL-X mode */
302#endif
303
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304#ifdef FEAT_AUTOCMD
305 /*
306 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
307 */
308 if (cmdchar != 'r' && cmdchar != 'v')
309 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000310# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000311 if (cmdchar == 'R')
312 ptr = (char_u *)"r";
313 else if (cmdchar == 'V')
314 ptr = (char_u *)"v";
315 else
316 ptr = (char_u *)"i";
317 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000318# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000319 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
320 }
321#endif
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_MOUSE
324 /*
325 * When doing a paste with the middle mouse button, Insstart is set to
326 * where the paste started.
327 */
328 if (where_paste_started.lnum != 0)
329 Insstart = where_paste_started;
330 else
331#endif
332 {
333 Insstart = curwin->w_cursor;
334 if (startln)
335 Insstart.col = 0;
336 }
337 Insstart_textlen = linetabsize(ml_get_curline());
338 Insstart_blank_vcol = MAXCOL;
339 if (!did_ai)
340 ai_col = 0;
341
342 if (cmdchar != NUL && restart_edit == 0)
343 {
344 ResetRedobuff();
345 AppendNumberToRedobuff(count);
346#ifdef FEAT_VREPLACE
347 if (cmdchar == 'V' || cmdchar == 'v')
348 {
349 /* "gR" or "gr" command */
350 AppendCharToRedobuff('g');
351 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
352 }
353 else
354#endif
355 {
356 AppendCharToRedobuff(cmdchar);
357 if (cmdchar == 'g') /* "gI" command */
358 AppendCharToRedobuff('I');
359 else if (cmdchar == 'r') /* "r<CR>" command */
360 count = 1; /* insert only one <CR> */
361 }
362 }
363
364 if (cmdchar == 'R')
365 {
366#ifdef FEAT_FKMAP
367 if (p_fkmap && p_ri)
368 {
369 beep_flush();
370 EMSG(farsi_text_3); /* encoded in Farsi */
371 State = INSERT;
372 }
373 else
374#endif
375 State = REPLACE;
376 }
377#ifdef FEAT_VREPLACE
378 else if (cmdchar == 'V' || cmdchar == 'v')
379 {
380 State = VREPLACE;
381 replaceState = VREPLACE;
382 orig_line_count = curbuf->b_ml.ml_line_count;
383 vr_lines_changed = 1;
384 }
385#endif
386 else
387 State = INSERT;
388
389 stop_insert_mode = FALSE;
390
391 /*
392 * Need to recompute the cursor position, it might move when the cursor is
393 * on a TAB or special character.
394 */
395 curs_columns(TRUE);
396
397 /*
398 * Enable langmap or IME, indicated by 'iminsert'.
399 * Note that IME may enabled/disabled without us noticing here, thus the
400 * 'iminsert' value may not reflect what is actually used. It is updated
401 * when hitting <Esc>.
402 */
403 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
404 State |= LANGMAP;
405#ifdef USE_IM_CONTROL
406 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
407#endif
408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_MOUSE
410 setmouse();
411#endif
412#ifdef FEAT_CMDL_INFO
413 clear_showcmd();
414#endif
415#ifdef FEAT_RIGHTLEFT
416 /* there is no reverse replace mode */
417 revins_on = (State == INSERT && p_ri);
418 if (revins_on)
419 undisplay_dollar();
420 revins_chars = 0;
421 revins_legal = 0;
422 revins_scol = -1;
423#endif
424
425 /*
426 * Handle restarting Insert mode.
427 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
428 * restart_edit non-zero, and something in the stuff buffer.
429 */
430 if (restart_edit != 0 && stuff_empty())
431 {
432#ifdef FEAT_MOUSE
433 /*
434 * After a paste we consider text typed to be part of the insert for
435 * the pasted text. You can backspace over the pasted text too.
436 */
437 if (where_paste_started.lnum)
438 arrow_used = FALSE;
439 else
440#endif
441 arrow_used = TRUE;
442 restart_edit = 0;
443
444 /*
445 * If the cursor was after the end-of-line before the CTRL-O and it is
446 * now at the end-of-line, put it after the end-of-line (this is not
447 * correct in very rare cases).
448 * Also do this if curswant is greater than the current virtual
449 * column. Eg after "^O$" or "^O80|".
450 */
451 validate_virtcol();
452 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000453 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 || curwin->w_curswant > curwin->w_virtcol)
455 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
456 {
457 if (ptr[1] == NUL)
458 ++curwin->w_cursor.col;
459#ifdef FEAT_MBYTE
460 else if (has_mbyte)
461 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000462 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (ptr[i] == NUL)
464 curwin->w_cursor.col += i;
465 }
466#endif
467 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000468 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 }
470 else
471 arrow_used = FALSE;
472
473 /* we are in insert mode now, don't need to start it anymore */
474 need_start_insertmode = FALSE;
475
476 /* Need to save the line for undo before inserting the first char. */
477 ins_need_undo = TRUE;
478
479#ifdef FEAT_MOUSE
480 where_paste_started.lnum = 0;
481#endif
482#ifdef FEAT_CINDENT
483 can_cindent = TRUE;
484#endif
485#ifdef FEAT_FOLDING
486 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
487 * restarting. */
488 if (!p_im && did_restart_edit == 0)
489 foldOpenCursor();
490#endif
491
492 /*
493 * If 'showmode' is set, show the current (insert/replace/..) mode.
494 * A warning message for changing a readonly file is given here, before
495 * actually changing anything. It's put after the mode, if any.
496 */
497 i = 0;
498 if (p_smd)
499 i = showmode();
500
501 if (!p_im && did_restart_edit == 0)
502 change_warning(i + 1);
503
504#ifdef CURSOR_SHAPE
505 ui_cursor_shape(); /* may show different cursor shape */
506#endif
507#ifdef FEAT_DIGRAPHS
508 do_digraph(-1); /* clear digraphs */
509#endif
510
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000511 /*
512 * Get the current length of the redo buffer, those characters have to be
513 * skipped if we want to get to the inserted characters.
514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 ptr = get_inserted();
516 if (ptr == NULL)
517 new_insert_skip = 0;
518 else
519 {
520 new_insert_skip = (int)STRLEN(ptr);
521 vim_free(ptr);
522 }
523
524 old_indent = 0;
525
526 /*
527 * Main loop in Insert mode: repeat until Insert mode is left.
528 */
529 for (;;)
530 {
531#ifdef FEAT_RIGHTLEFT
532 if (!revins_legal)
533 revins_scol = -1; /* reset on illegal motions */
534 else
535 revins_legal = 0;
536#endif
537 if (arrow_used) /* don't repeat insert when arrow key used */
538 count = 0;
539
540 if (stop_insert_mode)
541 {
542 /* ":stopinsert" used or 'insertmode' reset */
543 count = 0;
544 goto doESCkey;
545 }
546
547 /* set curwin->w_curswant for next K_DOWN or K_UP */
548 if (!arrow_used)
549 curwin->w_set_curswant = TRUE;
550
551 /* If there is no typeahead may check for timestamps (e.g., for when a
552 * menu invoked a shell command). */
553 if (stuff_empty())
554 {
555 did_check_timestamps = FALSE;
556 if (need_check_timestamps)
557 check_timestamps(FALSE);
558 }
559
560 /*
561 * When emsg() was called msg_scroll will have been set.
562 */
563 msg_scroll = FALSE;
564
565#ifdef FEAT_GUI
566 /* When 'mousefocus' is set a mouse movement may have taken us to
567 * another window. "need_mouse_correct" may then be set because of an
568 * autocommand. */
569 if (need_mouse_correct)
570 gui_mouse_correct();
571#endif
572
573#ifdef FEAT_FOLDING
574 /* Open fold at the cursor line, according to 'foldopen'. */
575 if (fdo_flags & FDO_INSERT)
576 foldOpenCursor();
577 /* Close folds where the cursor isn't, according to 'foldclose' */
578 if (!char_avail())
579 foldCheckClose();
580#endif
581
582 /*
583 * If we inserted a character at the last position of the last line in
584 * the window, scroll the window one line up. This avoids an extra
585 * redraw.
586 * This is detected when the cursor column is smaller after inserting
587 * something.
588 * Don't do this when the topline changed already, it has
589 * already been adjusted (by insertchar() calling open_line())).
590 */
591 if (curbuf->b_mod_set
592 && curwin->w_p_wrap
593 && !did_backspace
594 && curwin->w_topline == old_topline
595#ifdef FEAT_DIFF
596 && curwin->w_topfill == old_topfill
597#endif
598 )
599 {
600 mincol = curwin->w_wcol;
601 validate_cursor_col();
602
603 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
604 && curwin->w_wrow == W_WINROW(curwin)
605 + curwin->w_height - 1 - p_so
606 && (curwin->w_cursor.lnum != curwin->w_topline
607#ifdef FEAT_DIFF
608 || curwin->w_topfill > 0
609#endif
610 ))
611 {
612#ifdef FEAT_DIFF
613 if (curwin->w_topfill > 0)
614 --curwin->w_topfill;
615 else
616#endif
617#ifdef FEAT_FOLDING
618 if (hasFolding(curwin->w_topline, NULL, &old_topline))
619 set_topline(curwin, old_topline + 1);
620 else
621#endif
622 set_topline(curwin, curwin->w_topline + 1);
623 }
624 }
625
626 /* May need to adjust w_topline to show the cursor. */
627 update_topline();
628
629 did_backspace = FALSE;
630
631 validate_cursor(); /* may set must_redraw */
632
633 /*
634 * Redraw the display when no characters are waiting.
635 * Also shows mode, ruler and positions cursor.
636 */
637 ins_redraw();
638
639#ifdef FEAT_SCROLLBIND
640 if (curwin->w_p_scb)
641 do_check_scrollbind(TRUE);
642#endif
643
644 update_curswant();
645 old_topline = curwin->w_topline;
646#ifdef FEAT_DIFF
647 old_topfill = curwin->w_topfill;
648#endif
649
650#ifdef USE_ON_FLY_SCROLL
651 dont_scroll = FALSE; /* allow scrolling here */
652#endif
653
654 /*
655 * Get a character for Insert mode.
656 */
657 lastc = c; /* remember previous char for CTRL-D */
658 c = safe_vgetc();
659
660#ifdef FEAT_RIGHTLEFT
661 if (p_hkmap && KeyTyped)
662 c = hkmap(c); /* Hebrew mode mapping */
663#endif
664#ifdef FEAT_FKMAP
665 if (p_fkmap && KeyTyped)
666 c = fkmap(c); /* Farsi mode mapping */
667#endif
668
669#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000670 /* When the popup menu is visible cursor keys change the selection. */
671 if (c == K_UP && pum_visible())
672 c = Ctrl_P;
673 if (c == K_DOWN && pum_visible())
674 c = Ctrl_N;
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
677 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000678 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000679 {
680 if (ins_compl_prep(c))
681 continue;
682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683#endif
684
Bram Moolenaar488c6512005-08-11 20:09:58 +0000685 /* CTRL-\ CTRL-N goes to Normal mode,
686 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
687 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 if (c == Ctrl_BSL)
689 {
690 /* may need to redraw when no more chars available now */
691 ins_redraw();
692 ++no_mapping;
693 ++allow_keys;
694 c = safe_vgetc();
695 --no_mapping;
696 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000697 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000699 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 vungetc(c);
701 c = Ctrl_BSL;
702 }
703 else if (c == Ctrl_G && p_im)
704 continue;
705 else
706 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000707 if (c == Ctrl_O)
708 {
709 ins_ctrl_o();
710 ins_at_eol = FALSE; /* cursor keeps its column */
711 nomove = TRUE;
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 count = 0;
714 goto doESCkey;
715 }
716 }
717
718#ifdef FEAT_DIGRAPHS
719 c = do_digraph(c);
720#endif
721
722#ifdef FEAT_INS_EXPAND
723 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
724 goto docomplete;
725#endif
726 if (c == Ctrl_V || c == Ctrl_Q)
727 {
728 ins_ctrl_v();
729 c = Ctrl_V; /* pretend CTRL-V is last typed character */
730 continue;
731 }
732
733#ifdef FEAT_CINDENT
734 if (cindent_on()
735# ifdef FEAT_INS_EXPAND
736 && ctrl_x_mode == 0
737# endif
738 )
739 {
740 /* A key name preceded by a bang means this key is not to be
741 * inserted. Skip ahead to the re-indenting below.
742 * A key name preceded by a star means that indenting has to be
743 * done before inserting the key. */
744 line_is_white = inindent(0);
745 if (in_cinkeys(c, '!', line_is_white))
746 goto force_cindent;
747 if (can_cindent && in_cinkeys(c, '*', line_is_white)
748 && stop_arrow() == OK)
749 do_c_expr_indent();
750 }
751#endif
752
753#ifdef FEAT_RIGHTLEFT
754 if (curwin->w_p_rl)
755 switch (c)
756 {
757 case K_LEFT: c = K_RIGHT; break;
758 case K_S_LEFT: c = K_S_RIGHT; break;
759 case K_C_LEFT: c = K_C_RIGHT; break;
760 case K_RIGHT: c = K_LEFT; break;
761 case K_S_RIGHT: c = K_S_LEFT; break;
762 case K_C_RIGHT: c = K_C_LEFT; break;
763 }
764#endif
765
766#ifdef FEAT_VISUAL
767 /*
768 * If 'keymodel' contains "startsel", may start selection. If it
769 * does, a CTRL-O and c will be stuffed, we need to get these
770 * characters.
771 */
772 if (ins_start_select(c))
773 continue;
774#endif
775
776 /*
777 * The big switch to handle a character in insert mode.
778 */
779 switch (c)
780 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000781 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (echeck_abbr(ESC + ABBR_OFF))
783 break;
784 /*FALLTHROUGH*/
785
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000786 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#ifdef FEAT_CMDWIN
788 if (c == Ctrl_C && cmdwin_type != 0)
789 {
790 /* Close the cmdline window. */
791 cmdwin_result = K_IGNORE;
792 got_int = FALSE; /* don't stop executing autocommands et al. */
793 goto doESCkey;
794 }
795#endif
796
797#ifdef UNIX
798do_intr:
799#endif
800 /* when 'insertmode' set, and not halfway a mapping, don't leave
801 * Insert mode */
802 if (goto_im())
803 {
804 if (got_int)
805 {
806 (void)vgetc(); /* flush all buffers */
807 got_int = FALSE;
808 }
809 else
810 vim_beep();
811 break;
812 }
813doESCkey:
814 /*
815 * This is the ONLY return from edit()!
816 */
817 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
818 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000819 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 o_lnum = curwin->w_cursor.lnum;
821
Bram Moolenaar488c6512005-08-11 20:09:58 +0000822 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000823 {
824#ifdef FEAT_AUTOCMD
825 if (cmdchar != 'r' && cmdchar != 'v')
826 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
827 FALSE, curbuf);
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 continue;
832
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000833 case Ctrl_Z: /* suspend when 'insertmode' set */
834 if (!p_im)
835 goto normalchar; /* insert CTRL-Z as normal char */
836 stuffReadbuff((char_u *)":st\r");
837 c = Ctrl_O;
838 /*FALLTHROUGH*/
839
840 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000841#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000842 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000843 goto docomplete;
844#endif
845 if (echeck_abbr(Ctrl_O + ABBR_OFF))
846 break;
847 ins_ctrl_o();
848 count = 0;
849 goto doESCkey;
850
Bram Moolenaar572cb562005-08-05 21:35:02 +0000851 case K_INS: /* toggle insert/replace mode */
852 case K_KINS:
853 ins_insert(replaceState);
854 break;
855
856 case K_SELECT: /* end of Select mode mapping - ignore */
857 break;
858
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000859#ifdef FEAT_SNIFF
860 case K_SNIFF: /* Sniff command received */
861 stuffcharReadbuff(K_SNIFF);
862 goto doESCkey;
863#endif
864
865 case K_HELP: /* Help key works like <ESC> <Help> */
866 case K_F1:
867 case K_XF1:
868 stuffcharReadbuff(K_HELP);
869 if (p_im)
870 need_start_insertmode = TRUE;
871 goto doESCkey;
872
873#ifdef FEAT_NETBEANS_INTG
874 case K_F21: /* NetBeans command */
875 ++no_mapping; /* don't map the next key hits */
876 i = safe_vgetc();
877 --no_mapping;
878 netbeans_keycommand(i);
879 break;
880#endif
881
882 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 case NUL:
884 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 /* For ^@ the trailing ESC will end the insert, unless there is an
886 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
888 && c != Ctrl_A && !p_im)
889 goto doESCkey; /* quit insert mode */
890 inserted_space = FALSE;
891 break;
892
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 ins_reg();
895 auto_format(FALSE, TRUE);
896 inserted_space = FALSE;
897 break;
898
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 ins_ctrl_g();
901 break;
902
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903 case Ctrl_HAT: /* switch input mode and/or langmap */
904 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 break;
906
907#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000908 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (!p_ari)
910 goto normalchar;
911 ins_ctrl_();
912 break;
913#endif
914
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000915 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
917 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
918 goto docomplete;
919#endif
920 /* FALLTHROUGH */
921
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000922 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923# ifdef FEAT_INS_EXPAND
924 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
925 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000926 if (has_compl_option(FALSE))
927 goto docomplete;
928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 }
930# endif
931 ins_shift(c, lastc);
932 auto_format(FALSE, TRUE);
933 inserted_space = FALSE;
934 break;
935
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000936 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 case K_KDEL:
938 ins_del();
939 auto_format(FALSE, TRUE);
940 break;
941
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000942 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 case Ctrl_H:
944 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
945 auto_format(FALSE, TRUE);
946 break;
947
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000948 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
950 auto_format(FALSE, TRUE);
951 break;
952
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000954# ifdef FEAT_COMPL_FUNC
955 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000956 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000957 goto docomplete;
958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
960 auto_format(FALSE, TRUE);
961 inserted_space = FALSE;
962 break;
963
964#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 case K_LEFTMOUSE_NM:
967 case K_LEFTDRAG:
968 case K_LEFTRELEASE:
969 case K_LEFTRELEASE_NM:
970 case K_MIDDLEMOUSE:
971 case K_MIDDLEDRAG:
972 case K_MIDDLERELEASE:
973 case K_RIGHTMOUSE:
974 case K_RIGHTDRAG:
975 case K_RIGHTRELEASE:
976 case K_X1MOUSE:
977 case K_X1DRAG:
978 case K_X1RELEASE:
979 case K_X2MOUSE:
980 case K_X2DRAG:
981 case K_X2RELEASE:
982 ins_mouse(c);
983 break;
984
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ins_mousescroll(FALSE);
987 break;
988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 ins_mousescroll(TRUE);
991 break;
992#endif
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 break;
996
Bram Moolenaar4770d092006-01-12 23:22:24 +0000997#ifdef FEAT_GUI_W32
998 /* On Win32 ignore <M-F4>, we get it when closing the window was
999 * cancelled. */
1000 case K_F4:
1001 if (mod_mask != MOD_MASK_ALT)
1002 goto normalchar;
1003 break;
1004#endif
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#ifdef FEAT_GUI
1007 case K_VER_SCROLLBAR:
1008 ins_scroll();
1009 break;
1010
1011 case K_HOR_SCROLLBAR:
1012 ins_horscroll();
1013 break;
1014#endif
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 case K_S_HOME:
1019 case K_C_HOME:
1020 ins_home(c);
1021 break;
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 case K_S_END:
1026 case K_C_END:
1027 ins_end(c);
1028 break;
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001031 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1032 ins_s_left();
1033 else
1034 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 case K_C_LEFT:
1039 ins_s_left();
1040 break;
1041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001043 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1044 ins_s_right();
1045 else
1046 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 case K_C_RIGHT:
1051 ins_s_right();
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001055 if (mod_mask & MOD_MASK_SHIFT)
1056 ins_pageup();
1057 else
1058 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case K_PAGEUP:
1063 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001064#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001065 if (pum_visible())
1066 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 ins_pageup();
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001072 if (mod_mask & MOD_MASK_SHIFT)
1073 ins_pagedown();
1074 else
1075 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 case K_PAGEDOWN:
1080 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001081#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001082 if (pum_visible())
1083 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ins_pagedown();
1086 break;
1087
1088#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ins_drop();
1091 break;
1092#endif
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 c = TAB;
1096 /* FALLTHROUGH */
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1100 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1101 goto docomplete;
1102#endif
1103 inserted_space = FALSE;
1104 if (ins_tab())
1105 goto normalchar; /* insert TAB as a normal char */
1106 auto_format(FALSE, TRUE);
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 c = CAR;
1111 /* FALLTHROUGH */
1112 case CAR:
1113 case NL:
1114#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1115 /* In a quickfix window a <CR> jumps to the error under the
1116 * cursor. */
1117 if (bt_quickfix(curbuf) && c == CAR)
1118 {
1119 do_cmdline_cmd((char_u *)".cc");
1120 break;
1121 }
1122#endif
1123#ifdef FEAT_CMDWIN
1124 if (cmdwin_type != 0)
1125 {
1126 /* Execute the command in the cmdline window. */
1127 cmdwin_result = CAR;
1128 goto doESCkey;
1129 }
1130#endif
1131 if (ins_eol(c) && !p_im)
1132 goto doESCkey; /* out of memory */
1133 auto_format(FALSE, FALSE);
1134 inserted_space = FALSE;
1135 break;
1136
1137#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139# ifdef FEAT_INS_EXPAND
1140 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1141 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 if (has_compl_option(TRUE))
1143 goto docomplete;
1144 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146# endif
1147# ifdef FEAT_DIGRAPHS
1148 c = ins_digraph();
1149 if (c == NUL)
1150 break;
1151# endif
1152 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001156 case Ctrl_X: /* Enter CTRL-X mode */
1157 ins_ctrl_x();
1158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (ctrl_x_mode != CTRL_X_TAGS)
1162 goto normalchar;
1163 goto docomplete;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (ctrl_x_mode != CTRL_X_FILES)
1167 goto normalchar;
1168 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001169
1170 case 's': /* Spelling completion after ^X */
1171 case Ctrl_S:
1172 if (ctrl_x_mode != CTRL_X_SPELL)
1173 goto normalchar;
1174 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#ifdef FEAT_INS_EXPAND
1179 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1180#endif
1181 {
1182 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1183 if (p_im)
1184 {
1185 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1186 break;
1187 goto doESCkey;
1188 }
1189 goto normalchar;
1190 }
1191#ifdef FEAT_INS_EXPAND
1192 /* FALLTHROUGH */
1193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001194 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 case Ctrl_N:
1196 /* if 'complete' is empty then plain ^P is no longer special,
1197 * but it is under other ^X modes */
1198 if (*curbuf->b_p_cpt == NUL
1199 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 goto normalchar;
1202
1203docomplete:
1204 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207#endif /* FEAT_INS_EXPAND */
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case Ctrl_Y: /* copy from previous line or scroll down */
1210 case Ctrl_E: /* copy from next line or scroll up */
1211 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 break;
1213
1214 default:
1215#ifdef UNIX
1216 if (c == intr_char) /* special interrupt char */
1217 goto do_intr;
1218#endif
1219
1220 /*
1221 * Insert a nomal character.
1222 */
1223normalchar:
1224#ifdef FEAT_SMARTINDENT
1225 /* Try to perform smart-indenting. */
1226 ins_try_si(c);
1227#endif
1228
1229 if (c == ' ')
1230 {
1231 inserted_space = TRUE;
1232#ifdef FEAT_CINDENT
1233 if (inindent(0))
1234 can_cindent = FALSE;
1235#endif
1236 if (Insstart_blank_vcol == MAXCOL
1237 && curwin->w_cursor.lnum == Insstart.lnum)
1238 Insstart_blank_vcol = get_nolist_virtcol();
1239 }
1240
1241 if (vim_iswordc(c) || !echeck_abbr(
1242#ifdef FEAT_MBYTE
1243 /* Add ABBR_OFF for characters above 0x100, this is
1244 * what check_abbr() expects. */
1245 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1246#endif
1247 c))
1248 {
1249 insert_special(c, FALSE, FALSE);
1250#ifdef FEAT_RIGHTLEFT
1251 revins_legal++;
1252 revins_chars++;
1253#endif
1254 }
1255
1256 auto_format(FALSE, TRUE);
1257
1258#ifdef FEAT_FOLDING
1259 /* When inserting a character the cursor line must never be in a
1260 * closed fold. */
1261 foldOpenCursor();
1262#endif
1263 break;
1264 } /* end of switch (c) */
1265
1266 /* If the cursor was moved we didn't just insert a space */
1267 if (arrow_used)
1268 inserted_space = FALSE;
1269
1270#ifdef FEAT_CINDENT
1271 if (can_cindent && cindent_on()
1272# ifdef FEAT_INS_EXPAND
1273 && ctrl_x_mode == 0
1274# endif
1275 )
1276 {
1277force_cindent:
1278 /*
1279 * Indent now if a key was typed that is in 'cinkeys'.
1280 */
1281 if (in_cinkeys(c, ' ', line_is_white))
1282 {
1283 if (stop_arrow() == OK)
1284 /* re-indent the current line */
1285 do_c_expr_indent();
1286 }
1287 }
1288#endif /* FEAT_CINDENT */
1289
1290 } /* for (;;) */
1291 /* NOTREACHED */
1292}
1293
1294/*
1295 * Redraw for Insert mode.
1296 * This is postponed until getting the next character to make '$' in the 'cpo'
1297 * option work correctly.
1298 * Only redraw when there are no characters available. This speeds up
1299 * inserting sequences of characters (e.g., for CTRL-R).
1300 */
1301 static void
1302ins_redraw()
1303{
1304 if (!char_avail())
1305 {
1306 if (must_redraw)
1307 update_screen(0);
1308 else if (clear_cmdline || redraw_cmdline)
1309 showmode(); /* clear cmdline and show mode */
1310 showruler(FALSE);
1311 setcursor();
1312 emsg_on_display = FALSE; /* may remove error message now */
1313 }
1314}
1315
1316/*
1317 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1318 */
1319 static void
1320ins_ctrl_v()
1321{
1322 int c;
1323
1324 /* may need to redraw when no more chars available now */
1325 ins_redraw();
1326
1327 if (redrawing() && !char_avail())
1328 edit_putchar('^', TRUE);
1329 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1330
1331#ifdef FEAT_CMDL_INFO
1332 add_to_showcmd_c(Ctrl_V);
1333#endif
1334
1335 c = get_literal();
1336#ifdef FEAT_CMDL_INFO
1337 clear_showcmd();
1338#endif
1339 insert_special(c, FALSE, TRUE);
1340#ifdef FEAT_RIGHTLEFT
1341 revins_chars++;
1342 revins_legal++;
1343#endif
1344}
1345
1346/*
1347 * Put a character directly onto the screen. It's not stored in a buffer.
1348 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1349 */
1350static int pc_status;
1351#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1352#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1353#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1354#define PC_STATUS_SET 3 /* pc_bytes was filled */
1355#ifdef FEAT_MBYTE
1356static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1357#else
1358static char_u pc_bytes[2]; /* saved bytes */
1359#endif
1360static int pc_attr;
1361static int pc_row;
1362static int pc_col;
1363
1364 void
1365edit_putchar(c, highlight)
1366 int c;
1367 int highlight;
1368{
1369 int attr;
1370
1371 if (ScreenLines != NULL)
1372 {
1373 update_topline(); /* just in case w_topline isn't valid */
1374 validate_cursor();
1375 if (highlight)
1376 attr = hl_attr(HLF_8);
1377 else
1378 attr = 0;
1379 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1380 pc_col = W_WINCOL(curwin);
1381#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1382 pc_status = PC_STATUS_UNSET;
1383#endif
1384#ifdef FEAT_RIGHTLEFT
1385 if (curwin->w_p_rl)
1386 {
1387 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1388# ifdef FEAT_MBYTE
1389 if (has_mbyte)
1390 {
1391 int fix_col = mb_fix_col(pc_col, pc_row);
1392
1393 if (fix_col != pc_col)
1394 {
1395 screen_putchar(' ', pc_row, fix_col, attr);
1396 --curwin->w_wcol;
1397 pc_status = PC_STATUS_RIGHT;
1398 }
1399 }
1400# endif
1401 }
1402 else
1403#endif
1404 {
1405 pc_col += curwin->w_wcol;
1406#ifdef FEAT_MBYTE
1407 if (mb_lefthalve(pc_row, pc_col))
1408 pc_status = PC_STATUS_LEFT;
1409#endif
1410 }
1411
1412 /* save the character to be able to put it back */
1413#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1414 if (pc_status == PC_STATUS_UNSET)
1415#endif
1416 {
1417 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1418 pc_status = PC_STATUS_SET;
1419 }
1420 screen_putchar(c, pc_row, pc_col, attr);
1421 }
1422}
1423
1424/*
1425 * Undo the previous edit_putchar().
1426 */
1427 void
1428edit_unputchar()
1429{
1430 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1431 {
1432#if defined(FEAT_MBYTE)
1433 if (pc_status == PC_STATUS_RIGHT)
1434 ++curwin->w_wcol;
1435 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1436 redrawWinline(curwin->w_cursor.lnum, FALSE);
1437 else
1438#endif
1439 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1440 }
1441}
1442
1443/*
1444 * Called when p_dollar is set: display a '$' at the end of the changed text
1445 * Only works when cursor is in the line that changes.
1446 */
1447 void
1448display_dollar(col)
1449 colnr_T col;
1450{
1451 colnr_T save_col;
1452
1453 if (!redrawing())
1454 return;
1455
1456 cursor_off();
1457 save_col = curwin->w_cursor.col;
1458 curwin->w_cursor.col = col;
1459#ifdef FEAT_MBYTE
1460 if (has_mbyte)
1461 {
1462 char_u *p;
1463
1464 /* If on the last byte of a multi-byte move to the first byte. */
1465 p = ml_get_curline();
1466 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1467 }
1468#endif
1469 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1470 if (curwin->w_wcol < W_WIDTH(curwin))
1471 {
1472 edit_putchar('$', FALSE);
1473 dollar_vcol = curwin->w_virtcol;
1474 }
1475 curwin->w_cursor.col = save_col;
1476}
1477
1478/*
1479 * Call this function before moving the cursor from the normal insert position
1480 * in insert mode.
1481 */
1482 static void
1483undisplay_dollar()
1484{
1485 if (dollar_vcol)
1486 {
1487 dollar_vcol = 0;
1488 redrawWinline(curwin->w_cursor.lnum, FALSE);
1489 }
1490}
1491
1492/*
1493 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1494 * Keep the cursor on the same character.
1495 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1496 * type == INDENT_DEC decrease indent (for CTRL-D)
1497 * type == INDENT_SET set indent to "amount"
1498 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1499 */
1500 void
1501change_indent(type, amount, round, replaced)
1502 int type;
1503 int amount;
1504 int round;
1505 int replaced; /* replaced character, put on replace stack */
1506{
1507 int vcol;
1508 int last_vcol;
1509 int insstart_less; /* reduction for Insstart.col */
1510 int new_cursor_col;
1511 int i;
1512 char_u *ptr;
1513 int save_p_list;
1514 int start_col;
1515 colnr_T vc;
1516#ifdef FEAT_VREPLACE
1517 colnr_T orig_col = 0; /* init for GCC */
1518 char_u *new_line, *orig_line = NULL; /* init for GCC */
1519
1520 /* VREPLACE mode needs to know what the line was like before changing */
1521 if (State & VREPLACE_FLAG)
1522 {
1523 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1524 orig_col = curwin->w_cursor.col;
1525 }
1526#endif
1527
1528 /* for the following tricks we don't want list mode */
1529 save_p_list = curwin->w_p_list;
1530 curwin->w_p_list = FALSE;
1531 vc = getvcol_nolist(&curwin->w_cursor);
1532 vcol = vc;
1533
1534 /*
1535 * For Replace mode we need to fix the replace stack later, which is only
1536 * possible when the cursor is in the indent. Remember the number of
1537 * characters before the cursor if it's possible.
1538 */
1539 start_col = curwin->w_cursor.col;
1540
1541 /* determine offset from first non-blank */
1542 new_cursor_col = curwin->w_cursor.col;
1543 beginline(BL_WHITE);
1544 new_cursor_col -= curwin->w_cursor.col;
1545
1546 insstart_less = curwin->w_cursor.col;
1547
1548 /*
1549 * If the cursor is in the indent, compute how many screen columns the
1550 * cursor is to the left of the first non-blank.
1551 */
1552 if (new_cursor_col < 0)
1553 vcol = get_indent() - vcol;
1554
1555 if (new_cursor_col > 0) /* can't fix replace stack */
1556 start_col = -1;
1557
1558 /*
1559 * Set the new indent. The cursor will be put on the first non-blank.
1560 */
1561 if (type == INDENT_SET)
1562 (void)set_indent(amount, SIN_CHANGED);
1563 else
1564 {
1565#ifdef FEAT_VREPLACE
1566 int save_State = State;
1567
1568 /* Avoid being called recursively. */
1569 if (State & VREPLACE_FLAG)
1570 State = INSERT;
1571#endif
1572 shift_line(type == INDENT_DEC, round, 1);
1573#ifdef FEAT_VREPLACE
1574 State = save_State;
1575#endif
1576 }
1577 insstart_less -= curwin->w_cursor.col;
1578
1579 /*
1580 * Try to put cursor on same character.
1581 * If the cursor is at or after the first non-blank in the line,
1582 * compute the cursor column relative to the column of the first
1583 * non-blank character.
1584 * If we are not in insert mode, leave the cursor on the first non-blank.
1585 * If the cursor is before the first non-blank, position it relative
1586 * to the first non-blank, counted in screen columns.
1587 */
1588 if (new_cursor_col >= 0)
1589 {
1590 /*
1591 * When changing the indent while the cursor is touching it, reset
1592 * Insstart_col to 0.
1593 */
1594 if (new_cursor_col == 0)
1595 insstart_less = MAXCOL;
1596 new_cursor_col += curwin->w_cursor.col;
1597 }
1598 else if (!(State & INSERT))
1599 new_cursor_col = curwin->w_cursor.col;
1600 else
1601 {
1602 /*
1603 * Compute the screen column where the cursor should be.
1604 */
1605 vcol = get_indent() - vcol;
1606 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1607
1608 /*
1609 * Advance the cursor until we reach the right screen column.
1610 */
1611 vcol = last_vcol = 0;
1612 new_cursor_col = -1;
1613 ptr = ml_get_curline();
1614 while (vcol <= (int)curwin->w_virtcol)
1615 {
1616 last_vcol = vcol;
1617#ifdef FEAT_MBYTE
1618 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001619 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 else
1621#endif
1622 ++new_cursor_col;
1623 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1624 }
1625 vcol = last_vcol;
1626
1627 /*
1628 * May need to insert spaces to be able to position the cursor on
1629 * the right screen column.
1630 */
1631 if (vcol != (int)curwin->w_virtcol)
1632 {
1633 curwin->w_cursor.col = new_cursor_col;
1634 i = (int)curwin->w_virtcol - vcol;
1635 ptr = alloc(i + 1);
1636 if (ptr != NULL)
1637 {
1638 new_cursor_col += i;
1639 ptr[i] = NUL;
1640 while (--i >= 0)
1641 ptr[i] = ' ';
1642 ins_str(ptr);
1643 vim_free(ptr);
1644 }
1645 }
1646
1647 /*
1648 * When changing the indent while the cursor is in it, reset
1649 * Insstart_col to 0.
1650 */
1651 insstart_less = MAXCOL;
1652 }
1653
1654 curwin->w_p_list = save_p_list;
1655
1656 if (new_cursor_col <= 0)
1657 curwin->w_cursor.col = 0;
1658 else
1659 curwin->w_cursor.col = new_cursor_col;
1660 curwin->w_set_curswant = TRUE;
1661 changed_cline_bef_curs();
1662
1663 /*
1664 * May have to adjust the start of the insert.
1665 */
1666 if (State & INSERT)
1667 {
1668 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1669 {
1670 if ((int)Insstart.col <= insstart_less)
1671 Insstart.col = 0;
1672 else
1673 Insstart.col -= insstart_less;
1674 }
1675 if ((int)ai_col <= insstart_less)
1676 ai_col = 0;
1677 else
1678 ai_col -= insstart_less;
1679 }
1680
1681 /*
1682 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1683 * If the number of characters before the cursor decreased, need to pop a
1684 * few characters from the replace stack.
1685 * If the number of characters before the cursor increased, need to push a
1686 * few NULs onto the replace stack.
1687 */
1688 if (REPLACE_NORMAL(State) && start_col >= 0)
1689 {
1690 while (start_col > (int)curwin->w_cursor.col)
1691 {
1692 replace_join(0); /* remove a NUL from the replace stack */
1693 --start_col;
1694 }
1695 while (start_col < (int)curwin->w_cursor.col || replaced)
1696 {
1697 replace_push(NUL);
1698 if (replaced)
1699 {
1700 replace_push(replaced);
1701 replaced = NUL;
1702 }
1703 ++start_col;
1704 }
1705 }
1706
1707#ifdef FEAT_VREPLACE
1708 /*
1709 * For VREPLACE mode, we also have to fix the replace stack. In this case
1710 * it is always possible because we backspace over the whole line and then
1711 * put it back again the way we wanted it.
1712 */
1713 if (State & VREPLACE_FLAG)
1714 {
1715 /* If orig_line didn't allocate, just return. At least we did the job,
1716 * even if you can't backspace. */
1717 if (orig_line == NULL)
1718 return;
1719
1720 /* Save new line */
1721 new_line = vim_strsave(ml_get_curline());
1722 if (new_line == NULL)
1723 return;
1724
1725 /* We only put back the new line up to the cursor */
1726 new_line[curwin->w_cursor.col] = NUL;
1727
1728 /* Put back original line */
1729 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1730 curwin->w_cursor.col = orig_col;
1731
1732 /* Backspace from cursor to start of line */
1733 backspace_until_column(0);
1734
1735 /* Insert new stuff into line again */
1736 ins_bytes(new_line);
1737
1738 vim_free(new_line);
1739 }
1740#endif
1741}
1742
1743/*
1744 * Truncate the space at the end of a line. This is to be used only in an
1745 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1746 * modes.
1747 */
1748 void
1749truncate_spaces(line)
1750 char_u *line;
1751{
1752 int i;
1753
1754 /* find start of trailing white space */
1755 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1756 {
1757 if (State & REPLACE_FLAG)
1758 replace_join(0); /* remove a NUL from the replace stack */
1759 }
1760 line[i + 1] = NUL;
1761}
1762
1763#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1764 || defined(FEAT_COMMENTS) || defined(PROTO)
1765/*
1766 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1767 * modes correctly. May also be used when not in insert mode at all.
1768 */
1769 void
1770backspace_until_column(col)
1771 int col;
1772{
1773 while ((int)curwin->w_cursor.col > col)
1774 {
1775 curwin->w_cursor.col--;
1776 if (State & REPLACE_FLAG)
1777 replace_do_bs();
1778 else
1779 (void)del_char(FALSE);
1780 }
1781}
1782#endif
1783
1784#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1785/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001786 * CTRL-X pressed in Insert mode.
1787 */
1788 static void
1789ins_ctrl_x()
1790{
1791 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1792 * CTRL-V works like CTRL-N */
1793 if (ctrl_x_mode != CTRL_X_CMDLINE)
1794 {
1795 /* if the next ^X<> won't ADD nothing, then reset
1796 * compl_cont_status */
1797 if (compl_cont_status & CONT_N_ADDS)
1798 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1799 else
1800 compl_cont_status = 0;
1801 /* We're not sure which CTRL-X mode it will be yet */
1802 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1803 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1804 edit_submode_pre = NULL;
1805 showmode();
1806 }
1807}
1808
1809/*
1810 * Return TRUE if the 'dict' or 'tsr' option can be used.
1811 */
1812 static int
1813has_compl_option(dict_opt)
1814 int dict_opt;
1815{
1816 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1817 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1818 {
1819 ctrl_x_mode = 0;
1820 edit_submode = NULL;
1821 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1822 : (char_u *)_("'thesaurus' option is empty"),
1823 hl_attr(HLF_E));
1824 if (emsg_silent == 0)
1825 {
1826 vim_beep();
1827 setcursor();
1828 out_flush();
1829 ui_delay(2000L, FALSE);
1830 }
1831 return FALSE;
1832 }
1833 return TRUE;
1834}
1835
1836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1838 * This depends on the current mode.
1839 */
1840 int
1841vim_is_ctrl_x_key(c)
1842 int c;
1843{
1844 /* Always allow ^R - let it's results then be checked */
1845 if (c == Ctrl_R)
1846 return TRUE;
1847
Bram Moolenaare3226be2005-12-18 22:10:00 +00001848 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
1849 if (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
1850 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN))
1851 return TRUE;
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 switch (ctrl_x_mode)
1854 {
1855 case 0: /* Not in any CTRL-X mode */
1856 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1857 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001858 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1860 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1861 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001862 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1863 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 case CTRL_X_SCROLL:
1865 return (c == Ctrl_Y || c == Ctrl_E);
1866 case CTRL_X_WHOLE_LINE:
1867 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1868 case CTRL_X_FILES:
1869 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1870 case CTRL_X_DICTIONARY:
1871 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1872 case CTRL_X_THESAURUS:
1873 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1874 case CTRL_X_TAGS:
1875 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1876#ifdef FEAT_FIND_ID
1877 case CTRL_X_PATH_PATTERNS:
1878 return (c == Ctrl_P || c == Ctrl_N);
1879 case CTRL_X_PATH_DEFINES:
1880 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1881#endif
1882 case CTRL_X_CMDLINE:
1883 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1884 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001885#ifdef FEAT_COMPL_FUNC
1886 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001887 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001888 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001889 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001890#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001891 case CTRL_X_SPELL:
1892 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894 EMSG(_(e_internal));
1895 return FALSE;
1896}
1897
1898/*
1899 * This is like ins_compl_add(), but if ic and inf are set, then the
1900 * case of the originally typed text is used, and the case of the completed
1901 * text is infered, ie this tries to work out what case you probably wanted
1902 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001903 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 */
1905 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001906ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 char_u *str;
1908 int len;
1909 char_u *fname;
1910 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001911 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913 int has_lower = FALSE;
1914 int was_letter = FALSE;
1915 int idx;
1916
1917 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1918 {
1919 /* Infer case of completed part -- webb */
1920 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001921 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
1923 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001924 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001926 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
1928 has_lower = TRUE;
1929 if (isupper(IObuff[idx]))
1930 {
1931 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001932 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1934 break;
1935 }
1936 }
1937 }
1938
1939 /*
1940 * Rule 2: No lower case, 2nd consecutive letter converted to
1941 * upper case.
1942 */
1943 if (!has_lower)
1944 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001945 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001947 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 && islower(IObuff[idx]))
1949 {
1950 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001951 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1953 break;
1954 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001955 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958
1959 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001960 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
Bram Moolenaar572cb562005-08-05 21:35:02 +00001962 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001964 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965}
1966
1967/*
1968 * Add a match to the list of matches.
1969 * If the given string is already in the list of completions, then return
1970 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001971 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1972 *
1973 * New:
1974 * If the given string is already in the list of completions, then return
1975 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1976 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001978 int
1979ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 char_u *str;
1981 int len;
1982 char_u *fname;
1983 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001984 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001986 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 ui_breakcheck();
1989 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001990 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (len < 0)
1992 len = (int)STRLEN(str);
1993
1994 /*
1995 * If the same match is already present, don't add it.
1996 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001997 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001999 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 do
2001 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002002 if ( !(match->cp_flags & ORIGINAL_TEXT)
2003 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2004 && match->cp_str[len] == NUL)
2005 return NOTDONE;
2006 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002007 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002010 /* Remove any popup menu before changing the list of matches. */
2011 ins_compl_del_pum();
2012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 /*
2014 * Allocate a new match structure.
2015 * Copy the values to the new match structure.
2016 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002017 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002019 return FAIL;
2020 match->cp_number = -1;
2021 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002023 match->cp_number = 0;
2024 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002026 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
2028 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002032 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2034 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002035 if (fname && compl_curr_match && compl_curr_match->cp_fname
2036 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2037 match->cp_fname = compl_curr_match->cp_fname;
2038 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2039 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002041 match->cp_fname = NULL;
2042 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 /*
2045 * Link the new match structure in the list of matches.
2046 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002047 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002048 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 else if (dir == FORWARD)
2050 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002051 match->cp_next = compl_curr_match->cp_next;
2052 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054 else /* BACKWARD */
2055 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002056 match->cp_next = compl_curr_match;
2057 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002059 if (match->cp_next)
2060 match->cp_next->cp_prev = match;
2061 if (match->cp_prev)
2062 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002064 compl_first_match = match;
2065 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 return OK;
2068}
2069
2070/*
2071 * Add an array of matches to the list of matches.
2072 * Frees matches[].
2073 */
2074 static void
2075ins_compl_add_matches(num_matches, matches, dir)
2076 int num_matches;
2077 char_u **matches;
2078 int dir;
2079{
2080 int i;
2081 int add_r = OK;
2082 int ldir = dir;
2083
Bram Moolenaar572cb562005-08-05 21:35:02 +00002084 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2086 /* if dir was BACKWARD then honor it just once */
2087 ldir = FORWARD;
2088 FreeWild(num_matches, matches);
2089}
2090
2091/* Make the completion list cyclic.
2092 * Return the number of matches (excluding the original).
2093 */
2094 static int
2095ins_compl_make_cyclic()
2096{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002097 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 int count = 0;
2099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002100 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 /*
2103 * Find the end of the list.
2104 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 match = compl_first_match;
2106 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002107 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002109 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 ++count;
2111 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002112 match->cp_next = compl_first_match;
2113 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115 return count;
2116}
2117
Bram Moolenaar9372a112005-12-06 19:59:18 +00002118/* "compl_match_array" points the currently displayed list of entries in the
2119 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002120static char_u **compl_match_array = NULL;
2121static int compl_match_arraysize;
2122
2123/*
2124 * Update the screen and when there is any scrolling remove the popup menu.
2125 */
2126 static void
2127ins_compl_upd_pum()
2128{
2129 int h;
2130
2131 if (compl_match_array != NULL)
2132 {
2133 h = curwin->w_cline_height;
2134 update_screen(0);
2135 if (h != curwin->w_cline_height)
2136 ins_compl_del_pum();
2137 }
2138}
2139
2140/*
2141 * Remove any popup menu.
2142 */
2143 static void
2144ins_compl_del_pum()
2145{
2146 if (compl_match_array != NULL)
2147 {
2148 pum_undisplay();
2149 vim_free(compl_match_array);
2150 compl_match_array = NULL;
2151 }
2152}
2153
2154/*
2155 * Return TRUE if the popup menu should be displayed.
2156 */
2157 static int
2158pum_wanted()
2159{
2160 compl_T *compl;
2161 int i;
2162
2163 /* 'completeopt' must contain "menu" */
2164 if (*p_cot == NUL)
2165 return FALSE;
2166
2167 /* The display looks bad on a B&W display. */
2168 if (t_colors < 8
2169#ifdef FEAT_GUI
2170 && !gui.in_use
2171#endif
2172 )
2173 return FALSE;
2174
2175 /* Don't display the popup menu if there are no matches or there is only
2176 * one (ignoring the original text). */
2177 compl = compl_first_match;
2178 i = 0;
2179 do
2180 {
2181 if (compl == NULL
2182 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2183 break;
2184 compl = compl->cp_next;
2185 } while (compl != compl_first_match);
2186
2187 return (i >= 2);
2188}
2189
2190/*
2191 * Show the popup menu for the list of matches.
2192 */
2193 static void
2194ins_compl_show_pum()
2195{
2196 compl_T *compl;
2197 int i;
2198 int cur = -1;
2199 colnr_T col;
2200
2201 if (!pum_wanted())
2202 return;
2203
2204 /* Update the screen before drawing the popup menu over it. */
2205 update_screen(0);
2206
2207 if (compl_match_array == NULL)
2208 {
2209 /* Need to build the popup menu list. */
2210 compl_match_arraysize = 0;
2211 compl = compl_first_match;
2212 do
2213 {
2214 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2215 ++compl_match_arraysize;
2216 compl = compl->cp_next;
2217 } while (compl != NULL && compl != compl_first_match);
2218 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2219 * compl_match_arraysize));
2220 if (compl_match_array != NULL)
2221 {
2222 i = 0;
2223 compl = compl_first_match;
2224 do
2225 {
2226 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2227 {
2228 if (compl == compl_shown_match)
2229 cur = i;
2230 compl_match_array[i++] = compl->cp_str;
2231 }
2232 compl = compl->cp_next;
2233 } while (compl != NULL && compl != compl_first_match);
2234 }
2235 }
2236 else
2237 {
2238 /* popup menu already exists, only need to find the current item.*/
2239 i = 0;
2240 compl = compl_first_match;
2241 do
2242 {
2243 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2244 {
2245 if (compl == compl_shown_match)
2246 {
2247 cur = i;
2248 break;
2249 }
2250 ++i;
2251 }
2252 compl = compl->cp_next;
2253 } while (compl != NULL && compl != compl_first_match);
2254 }
2255
2256 if (compl_match_array != NULL)
2257 {
2258 /* Compute the screen column of the start of the completed text.
2259 * Use the cursor to get all wrapping and other settings right. */
2260 col = curwin->w_cursor.col;
2261 curwin->w_cursor.col = compl_col;
2262 validate_cursor_col();
2263 pum_display(compl_match_array, compl_match_arraysize, cur,
2264 curwin->w_cline_row + W_WINROW(curwin),
2265 curwin->w_cline_height,
2266 curwin->w_wcol + W_WINCOL(curwin));
2267 curwin->w_cursor.col = col;
2268 }
2269}
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#define DICT_FIRST (1) /* use just first element in "dict" */
2272#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2273/*
2274 * Add any identifiers that match the given pattern to the list of
2275 * completions.
2276 */
2277 static void
2278ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2279 char_u *dict;
2280 char_u *pat;
2281 int dir;
2282 int flags;
2283 int thesaurus;
2284{
2285 char_u *ptr;
2286 char_u *buf;
2287 FILE *fp;
2288 regmatch_T regmatch;
2289 int add_r;
2290 char_u **files;
2291 int count;
2292 int i;
2293 int save_p_scs;
2294
2295 buf = alloc(LSIZE);
2296 /* If 'infercase' is set, don't use 'smartcase' here */
2297 save_p_scs = p_scs;
2298 if (curbuf->b_p_inf)
2299 p_scs = FALSE;
2300 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2301 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2302 regmatch.rm_ic = ignorecase(pat);
2303 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002304 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 /* copy one dictionary file name into buf */
2307 if (flags == DICT_EXACT)
2308 {
2309 count = 1;
2310 files = &dict;
2311 }
2312 else
2313 {
2314 /* Expand wildcards in the dictionary name, but do not allow
2315 * backticks (for security, the 'dict' option may have been set in
2316 * a modeline). */
2317 copy_option_part(&dict, buf, LSIZE, ",");
2318 if (vim_strchr(buf, '`') != NULL
2319 || expand_wildcards(1, &buf, &count, &files,
2320 EW_FILE|EW_SILENT) != OK)
2321 count = 0;
2322 }
2323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002324 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
2326 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2327 if (flags != DICT_EXACT)
2328 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002329 vim_snprintf((char *)IObuff, IOSIZE,
2330 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2332 }
2333
2334 if (fp != NULL)
2335 {
2336 /*
2337 * Read dictionary file line by line.
2338 * Check each line for a match.
2339 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002340 while (!got_int && !compl_interrupted
2341 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 ptr = buf;
2344 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2345 {
2346 ptr = regmatch.startp[0];
2347 ptr = find_word_end(ptr);
2348 add_r = ins_compl_add_infercase(regmatch.startp[0],
2349 (int)(ptr - regmatch.startp[0]),
2350 files[i], dir, 0);
2351 if (thesaurus)
2352 {
2353 char_u *wstart;
2354
2355 /*
2356 * Add the other matches on the line
2357 */
2358 while (!got_int)
2359 {
2360 /* Find start of the next word. Skip white
2361 * space and punctuation. */
2362 ptr = find_word_start(ptr);
2363 if (*ptr == NUL || *ptr == NL)
2364 break;
2365 wstart = ptr;
2366
2367 /* Find end of the word and add it. */
2368#ifdef FEAT_MBYTE
2369 if (has_mbyte)
2370 /* Japanese words may have characters in
2371 * different classes, only separate words
2372 * with single-byte non-word characters. */
2373 while (*ptr != NUL)
2374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002375 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377 if (l < 2 && !vim_iswordc(*ptr))
2378 break;
2379 ptr += l;
2380 }
2381 else
2382#endif
2383 ptr = find_word_end(ptr);
2384 add_r = ins_compl_add_infercase(wstart,
2385 (int)(ptr - wstart), files[i], dir, 0);
2386 }
2387 }
2388 if (add_r == OK)
2389 /* if dir was BACKWARD then honor it just once */
2390 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002391 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 break;
2393 /* avoid expensive call to vim_regexec() when at end
2394 * of line */
2395 if (*ptr == '\n' || got_int)
2396 break;
2397 }
2398 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002399 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 fclose(fp);
2402 }
2403 }
2404 if (flags != DICT_EXACT)
2405 FreeWild(count, files);
2406 if (flags)
2407 break;
2408 }
2409 p_scs = save_p_scs;
2410 vim_free(regmatch.regprog);
2411 vim_free(buf);
2412}
2413
2414/*
2415 * Find the start of the next word.
2416 * Returns a pointer to the first char of the word. Also stops at a NUL.
2417 */
2418 char_u *
2419find_word_start(ptr)
2420 char_u *ptr;
2421{
2422#ifdef FEAT_MBYTE
2423 if (has_mbyte)
2424 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002425 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 else
2427#endif
2428 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2429 ++ptr;
2430 return ptr;
2431}
2432
2433/*
2434 * Find the end of the word. Assumes it starts inside a word.
2435 * Returns a pointer to just after the word.
2436 */
2437 char_u *
2438find_word_end(ptr)
2439 char_u *ptr;
2440{
2441#ifdef FEAT_MBYTE
2442 int start_class;
2443
2444 if (has_mbyte)
2445 {
2446 start_class = mb_get_class(ptr);
2447 if (start_class > 1)
2448 while (*ptr != NUL)
2449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002450 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (mb_get_class(ptr) != start_class)
2452 break;
2453 }
2454 }
2455 else
2456#endif
2457 while (vim_iswordc(*ptr))
2458 ++ptr;
2459 return ptr;
2460}
2461
2462/*
2463 * Free the list of completions
2464 */
2465 static void
2466ins_compl_free()
2467{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002468 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002470 vim_free(compl_pattern);
2471 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002473 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002475
2476 ins_compl_del_pum();
2477 pum_clear();
2478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002479 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 do
2481 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002482 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002483 compl_curr_match = compl_curr_match->cp_next;
2484 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002486 if (match->cp_flags & FREE_FNAME)
2487 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002489 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2490 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
2492
2493 static void
2494ins_compl_clear()
2495{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002496 compl_cont_status = 0;
2497 compl_started = FALSE;
2498 compl_matches = 0;
2499 vim_free(compl_pattern);
2500 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 save_sm = -1;
2502 edit_submode_extra = NULL;
2503}
2504
2505/*
2506 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002507 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002508 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511ins_compl_prep(c)
2512 int c;
2513{
2514 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int temp;
2516 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002517 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 /* Forget any previous 'special' messages if this is actually
2520 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2521 */
2522 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2523 edit_submode_extra = NULL;
2524
2525 /* Ignore end of Select mode mapping */
2526 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002527 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2530 {
2531 /*
2532 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2533 * it will be yet. Now we decide.
2534 */
2535 switch (c)
2536 {
2537 case Ctrl_E:
2538 case Ctrl_Y:
2539 ctrl_x_mode = CTRL_X_SCROLL;
2540 if (!(State & REPLACE_FLAG))
2541 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2542 else
2543 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2544 edit_submode_pre = NULL;
2545 showmode();
2546 break;
2547 case Ctrl_L:
2548 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2549 break;
2550 case Ctrl_F:
2551 ctrl_x_mode = CTRL_X_FILES;
2552 break;
2553 case Ctrl_K:
2554 ctrl_x_mode = CTRL_X_DICTIONARY;
2555 break;
2556 case Ctrl_R:
2557 /* Simply allow ^R to happen without affecting ^X mode */
2558 break;
2559 case Ctrl_T:
2560 ctrl_x_mode = CTRL_X_THESAURUS;
2561 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002562#ifdef FEAT_COMPL_FUNC
2563 case Ctrl_U:
2564 ctrl_x_mode = CTRL_X_FUNCTION;
2565 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002566 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002567 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002568 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002569#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002570 case 's':
2571 case Ctrl_S:
2572 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002573#ifdef FEAT_SYN_HL
2574 spell_back_to_badword();
2575#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 case Ctrl_RSB:
2578 ctrl_x_mode = CTRL_X_TAGS;
2579 break;
2580#ifdef FEAT_FIND_ID
2581 case Ctrl_I:
2582 case K_S_TAB:
2583 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2584 break;
2585 case Ctrl_D:
2586 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2587 break;
2588#endif
2589 case Ctrl_V:
2590 case Ctrl_Q:
2591 ctrl_x_mode = CTRL_X_CMDLINE;
2592 break;
2593 case Ctrl_P:
2594 case Ctrl_N:
2595 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2596 * just started ^X mode, or there were enough ^X's to cancel
2597 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2598 * do normal expansion when interrupting a different mode (say
2599 * ^X^F^X^P or ^P^X^X^P, see below)
2600 * nothing changes if interrupting mode 0, (eg, the flag
2601 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002602 if (!(compl_cont_status & CONT_INTRPT))
2603 compl_cont_status |= CONT_LOCAL;
2604 else if (compl_cont_mode != 0)
2605 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 /* FALLTHROUGH */
2607 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002608 /* If we have typed at least 2 ^X's... for modes != 0, we set
2609 * compl_cont_status = 0 (eg, as if we had just started ^X
2610 * mode).
2611 * For mode 0, we set "compl_cont_mode" to an impossible
2612 * value, in both cases ^X^X can be used to restart the same
2613 * mode (avoiding ADDING mode).
2614 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2615 * 'complete' and local ^P expansions respectively.
2616 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2617 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (c == Ctrl_X)
2619 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002620 if (compl_cont_mode != 0)
2621 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002623 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625 ctrl_x_mode = 0;
2626 edit_submode = NULL;
2627 showmode();
2628 break;
2629 }
2630 }
2631 else if (ctrl_x_mode != 0)
2632 {
2633 /* We're already in CTRL-X mode, do we stay in it? */
2634 if (!vim_is_ctrl_x_key(c))
2635 {
2636 if (ctrl_x_mode == CTRL_X_SCROLL)
2637 ctrl_x_mode = 0;
2638 else
2639 ctrl_x_mode = CTRL_X_FINISHED;
2640 edit_submode = NULL;
2641 }
2642 showmode();
2643 }
2644
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002645 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 /* Show error message from attempted keyword completion (probably
2648 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002649 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 showmode();
2651 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2652 || ctrl_x_mode == CTRL_X_FINISHED)
2653 {
2654 /* Get here when we have finished typing a sequence of ^N and
2655 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002656 * memory that was used, and make sure we can redo the insert. */
2657 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002659 char_u *p;
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 /*
2662 * If any of the original typed text has been changed,
2663 * eg when ignorecase is set, we must add back-spaces to
2664 * the redo buffer. We add as few as necessary to delete
2665 * just the part of the original text that has changed.
2666 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002667 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002668 p = compl_orig_text;
2669 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002671 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 ++ptr;
2673 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002674 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002676 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679#ifdef FEAT_CINDENT
2680 want_cindent = (can_cindent && cindent_on());
2681#endif
2682 /*
2683 * When completing whole lines: fix indent for 'cindent'.
2684 * Otherwise, break line if it's too long.
2685 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002686 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688#ifdef FEAT_CINDENT
2689 /* re-indent the current line */
2690 if (want_cindent)
2691 {
2692 do_c_expr_indent();
2693 want_cindent = FALSE; /* don't do it again */
2694 }
2695#endif
2696 }
2697 else
2698 {
2699 /* put the cursor on the last char, for 'tw' formatting */
2700 curwin->w_cursor.col--;
2701 if (stop_arrow() == OK)
2702 insertchar(NUL, 0, -1);
2703 curwin->w_cursor.col++;
2704 }
2705
2706 auto_format(FALSE, TRUE);
2707
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002708 /* if the popup menu is displayed hitting Enter means accepting
2709 * the selection without inserting anything. */
2710 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2711 retval = TRUE;
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002714 compl_started = FALSE;
2715 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 msg_clr_cmdline(); /* necessary for "noshowmode" */
2717 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002718 if (save_sm >= 0)
2719 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if (edit_submode != NULL)
2721 {
2722 edit_submode = NULL;
2723 showmode();
2724 }
2725
2726#ifdef FEAT_CINDENT
2727 /*
2728 * Indent now if a key was typed that is in 'cinkeys'.
2729 */
2730 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2731 do_c_expr_indent();
2732#endif
2733 }
2734 }
2735
2736 /* reset continue_* if we left expansion-mode, if we stay they'll be
2737 * (re)set properly in ins_complete() */
2738 if (!vim_is_ctrl_x_key(c))
2739 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002740 compl_cont_status = 0;
2741 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002743
2744 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745}
2746
2747/*
2748 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2749 * (depending on flag) starting from buf and looking for a non-scanned
2750 * buffer (other than curbuf). curbuf is special, if it is called with
2751 * buf=curbuf then it has to be the first call for a given flag/expansion.
2752 *
2753 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2754 */
2755 static buf_T *
2756ins_compl_next_buf(buf, flag)
2757 buf_T *buf;
2758 int flag;
2759{
2760#ifdef FEAT_WINDOWS
2761 static win_T *wp;
2762#endif
2763
2764 if (flag == 'w') /* just windows */
2765 {
2766#ifdef FEAT_WINDOWS
2767 if (buf == curbuf) /* first call for this flag/expansion */
2768 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002769 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 && wp->w_buffer->b_scanned)
2771 ;
2772 buf = wp->w_buffer;
2773#else
2774 buf = curbuf;
2775#endif
2776 }
2777 else
2778 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2779 * (unlisted buffers)
2780 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002781 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 && ((flag == 'U'
2783 ? buf->b_p_bl
2784 : (!buf->b_p_bl
2785 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2786 || buf->b_scanned
2787 || (buf->b_ml.ml_mfp == NULL
2788 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2789 ;
2790 return buf;
2791}
2792
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002793#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002794static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002795
2796/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002797 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002798 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002799 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002800 */
2801 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002802expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002803 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002804 char_u *base;
2805 char_u ***matches;
2806{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002807 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002808 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002809 listitem_T *li;
2810 garray_T ga;
2811 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002812 char_u *funcname;
2813 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002814
Bram Moolenaare344bea2005-09-01 20:46:49 +00002815 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2816 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002817 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002818
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002819 /* Call 'completefunc' to obtain the list of matches. */
2820 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002821 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002822
Bram Moolenaare344bea2005-09-01 20:46:49 +00002823 pos = curwin->w_cursor;
2824 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2825 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002826 if (matchlist == NULL)
2827 return 0;
2828
2829 /* Go through the List with matches and put them in an array. */
2830 ga_init2(&ga, (int)sizeof(char_u *), 8);
2831 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002832 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002833 p = get_tv_string_chk(&li->li_tv);
2834 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002835 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002836 if (ga_grow(&ga, 1) == FAIL)
2837 break;
2838 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2839 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002840 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002841 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002842
2843 list_unref(matchlist);
2844 *matches = (char_u **)ga.ga_data;
2845 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002846}
2847#endif /* FEAT_COMPL_FUNC */
2848
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002849/*
2850 * Get the next expansion(s), using "compl_pattern".
2851 * The search starts at position "ini" in curbuf and in the direction dir.
2852 * When "compl_started" is FALSE start at that position, otherwise
2853 * continue where we stopped searching before.
2854 * This may return before finding all the matches.
2855 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 static int
2858ins_compl_get_exp(ini, dir)
2859 pos_T *ini;
2860 int dir;
2861{
2862 static pos_T first_match_pos;
2863 static pos_T last_match_pos;
2864 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002865 static int found_all = FALSE; /* Found all matches of a
2866 certain type. */
2867 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar572cb562005-08-05 21:35:02 +00002869 pos_T *pos;
2870 char_u **matches;
2871 int save_p_scs;
2872 int save_p_ws;
2873 int save_p_ic;
2874 int i;
2875 int num_matches;
2876 int len;
2877 int found_new_match;
2878 int type = ctrl_x_mode;
2879 char_u *ptr;
2880 char_u *dict = NULL;
2881 int dict_f = 0;
2882 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002884 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2887 ins_buf->b_scanned = 0;
2888 found_all = FALSE;
2889 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002890 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002891 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 last_match_pos = first_match_pos = *ini;
2893 }
2894
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002895 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2897 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2898 for (;;)
2899 {
2900 found_new_match = FAIL;
2901
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002902 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 * or if found_all says this entry is done. For ^X^L only use the
2904 * entries from 'complete' that look in loaded buffers. */
2905 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002906 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 found_all = FALSE;
2909 while (*e_cpt == ',' || *e_cpt == ' ')
2910 e_cpt++;
2911 if (*e_cpt == '.' && !curbuf->b_scanned)
2912 {
2913 ins_buf = curbuf;
2914 first_match_pos = *ini;
2915 /* So that ^N can match word immediately after cursor */
2916 if (ctrl_x_mode == 0)
2917 dec(&first_match_pos);
2918 last_match_pos = first_match_pos;
2919 type = 0;
2920 }
2921 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2922 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2923 {
2924 /* Scan a buffer, but not the current one. */
2925 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002927 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 first_match_pos.col = last_match_pos.col = 0;
2929 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2930 last_match_pos.lnum = 0;
2931 type = 0;
2932 }
2933 else /* unloaded buffer, scan like dictionary */
2934 {
2935 found_all = TRUE;
2936 if (ins_buf->b_fname == NULL)
2937 continue;
2938 type = CTRL_X_DICTIONARY;
2939 dict = ins_buf->b_fname;
2940 dict_f = DICT_EXACT;
2941 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002942 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 ins_buf->b_fname == NULL
2944 ? buf_spname(ins_buf)
2945 : ins_buf->b_sfname == NULL
2946 ? (char *)ins_buf->b_fname
2947 : (char *)ins_buf->b_sfname);
2948 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2949 }
2950 else if (*e_cpt == NUL)
2951 break;
2952 else
2953 {
2954 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2955 type = -1;
2956 else if (*e_cpt == 'k' || *e_cpt == 's')
2957 {
2958 if (*e_cpt == 'k')
2959 type = CTRL_X_DICTIONARY;
2960 else
2961 type = CTRL_X_THESAURUS;
2962 if (*++e_cpt != ',' && *e_cpt != NUL)
2963 {
2964 dict = e_cpt;
2965 dict_f = DICT_FIRST;
2966 }
2967 }
2968#ifdef FEAT_FIND_ID
2969 else if (*e_cpt == 'i')
2970 type = CTRL_X_PATH_PATTERNS;
2971 else if (*e_cpt == 'd')
2972 type = CTRL_X_PATH_DEFINES;
2973#endif
2974 else if (*e_cpt == ']' || *e_cpt == 't')
2975 {
2976 type = CTRL_X_TAGS;
2977 sprintf((char*)IObuff, _("Scanning tags."));
2978 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2979 }
2980 else
2981 type = -1;
2982
2983 /* in any case e_cpt is advanced to the next entry */
2984 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2985
2986 found_all = TRUE;
2987 if (type == -1)
2988 continue;
2989 }
2990 }
2991
2992 switch (type)
2993 {
2994 case -1:
2995 break;
2996#ifdef FEAT_FIND_ID
2997 case CTRL_X_PATH_PATTERNS:
2998 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002999 find_pattern_in_path(compl_pattern, dir,
3000 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003002 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3004 (linenr_T)1, (linenr_T)MAXLNUM);
3005 break;
3006#endif
3007
3008 case CTRL_X_DICTIONARY:
3009 case CTRL_X_THESAURUS:
3010 ins_compl_dictionaries(
3011 dict ? dict
3012 : (type == CTRL_X_THESAURUS
3013 ? (*curbuf->b_p_tsr == NUL
3014 ? p_tsr
3015 : curbuf->b_p_tsr)
3016 : (*curbuf->b_p_dict == NUL
3017 ? p_dict
3018 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003019 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3021 dict = NULL;
3022 break;
3023
3024 case CTRL_X_TAGS:
3025 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3026 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003027 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003030 * of matches is found when compl_pattern is empty */
3031 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3033 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3034 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3035 {
3036 ins_compl_add_matches(num_matches, matches, dir);
3037 }
3038 p_ic = save_p_ic;
3039 break;
3040
3041 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003042 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3044 {
3045
3046 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003047 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 ins_compl_add_matches(num_matches, matches, dir);
3049 }
3050 break;
3051
3052 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003053 if (expand_cmdline(&compl_xp, compl_pattern,
3054 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 &num_matches, &matches) == EXPAND_OK)
3056 ins_compl_add_matches(num_matches, matches, dir);
3057 break;
3058
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003059#ifdef FEAT_COMPL_FUNC
3060 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003061 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003062 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003063 if (num_matches > 0)
3064 ins_compl_add_matches(num_matches, matches, dir);
3065 break;
3066#endif
3067
Bram Moolenaar488c6512005-08-11 20:09:58 +00003068 case CTRL_X_SPELL:
3069#ifdef FEAT_SYN_HL
3070 num_matches = expand_spelling(first_match_pos.lnum,
3071 first_match_pos.col, compl_pattern, &matches);
3072 if (num_matches > 0)
3073 ins_compl_add_matches(num_matches, matches, dir);
3074#endif
3075 break;
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 default: /* normal ^P/^N and ^X^L */
3078 /*
3079 * If 'infercase' is set, don't use 'smartcase' here
3080 */
3081 save_p_scs = p_scs;
3082 if (ins_buf->b_p_inf)
3083 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 /* buffers other than curbuf are scanned from the beginning or the
3086 * end but never from the middle, thus setting nowrapscan in this
3087 * buffers is a good idea, on the other hand, we always set
3088 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3089 save_p_ws = p_ws;
3090 if (ins_buf != curbuf)
3091 p_ws = FALSE;
3092 else if (*e_cpt == '.')
3093 p_ws = TRUE;
3094 for (;;)
3095 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003096 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003098 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3099 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003101 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003103 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 else
3105 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003106 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003108 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003110 /* set compl_started even on fail */
3111 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 first_match_pos = *pos;
3113 last_match_pos = *pos;
3114 }
3115 else if (first_match_pos.lnum == last_match_pos.lnum
3116 && first_match_pos.col == last_match_pos.col)
3117 found_new_match = FAIL;
3118 if (found_new_match == FAIL)
3119 {
3120 if (ins_buf == curbuf)
3121 found_all = TRUE;
3122 break;
3123 }
3124
3125 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003126 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 && ini->lnum == pos->lnum
3128 && ini->col == pos->col)
3129 continue;
3130 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3131 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003133 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3136 continue;
3137 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3138 if (!p_paste)
3139 ptr = skipwhite(ptr);
3140 }
3141 len = (int)STRLEN(ptr);
3142 }
3143 else
3144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003145 char_u *tmp_ptr = ptr;
3146
3147 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003149 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 /* Skip if already inside a word. */
3151 if (vim_iswordp(tmp_ptr))
3152 continue;
3153 /* Find start of next word. */
3154 tmp_ptr = find_word_start(tmp_ptr);
3155 }
3156 /* Find end of this word. */
3157 tmp_ptr = find_word_end(tmp_ptr);
3158 len = (int)(tmp_ptr - ptr);
3159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003160 if ((compl_cont_status & CONT_ADDING)
3161 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3164 {
3165 /* Try next line, if any. the new word will be
3166 * "join" as if the normal command "J" was used.
3167 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003168 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * works -- Acevedo */
3170 STRNCPY(IObuff, ptr, len);
3171 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3172 tmp_ptr = ptr = skipwhite(ptr);
3173 /* Find start of next word. */
3174 tmp_ptr = find_word_start(tmp_ptr);
3175 /* Find end of next word. */
3176 tmp_ptr = find_word_end(tmp_ptr);
3177 if (tmp_ptr > ptr)
3178 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003179 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003181 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 IObuff[len++] = ' ';
3183 /* IObuf =~ "\k.* ", thus len >= 2 */
3184 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003185 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 || (vim_strchr(p_cpo, CPO_JOINSP)
3187 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003188 && (IObuff[len - 2] == '?'
3189 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 IObuff[len++] = ' ';
3191 }
3192 /* copy as much as posible of the new word */
3193 if (tmp_ptr - ptr >= IOSIZE - len)
3194 tmp_ptr = ptr + IOSIZE - len - 1;
3195 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3196 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003197 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 IObuff[len] = NUL;
3200 ptr = IObuff;
3201 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003202 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 continue;
3204 }
3205 }
3206 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003207 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003208 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
3210 found_new_match = OK;
3211 break;
3212 }
3213 }
3214 p_scs = save_p_scs;
3215 p_ws = save_p_ws;
3216 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003218 /* check if compl_curr_match has changed, (e.g. other type of
3219 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003220 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 found_new_match = OK;
3222
3223 /* break the loop for specialized modes (use 'complete' just for the
3224 * generic ctrl_x_mode == 0) or when we've found a new match */
3225 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003226 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003227 {
3228 if (got_int)
3229 break;
3230 if (pum_wanted() && type != -1)
3231 /* Fill the popup menu as soon as possible. */
3232 ins_compl_check_keys(0);
3233 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3234 || compl_interrupted)
3235 break;
3236 compl_started = TRUE;
3237 }
3238 else
3239 {
3240 /* Mark a buffer scanned when it has been scanned completely */
3241 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3242 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003244 compl_started = FALSE;
3245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003247 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3250 && *e_cpt == NUL) /* Got to end of 'complete' */
3251 found_new_match = FAIL;
3252
3253 i = -1; /* total of matches, unknown */
3254 if (found_new_match == FAIL
3255 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3256 i = ins_compl_make_cyclic();
3257
3258 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003259 * just been made cyclic then we have to move compl_curr_match to the next
3260 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003261 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003262 if (compl_curr_match == NULL)
3263 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 return i;
3265}
3266
3267/* Delete the old text being completed. */
3268 static void
3269ins_compl_delete()
3270{
3271 int i;
3272
3273 /*
3274 * In insert mode: Delete the typed part.
3275 * In replace mode: Put the old characters back, if any.
3276 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003277 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 backspace_until_column(i);
3279 changed_cline_bef_curs();
3280}
3281
3282/* Insert the new text being completed. */
3283 static void
3284ins_compl_insert()
3285{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003286 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287}
3288
3289/*
3290 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003291 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3292 * get more completions. If it is FALSE, then we just do nothing when there
3293 * are no more completions in a given direction. The latter case is used when
3294 * we are still in the middle of finding completions, to allow browsing
3295 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 * Return the total number of matches, or -1 if still unknown -- webb.
3297 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003298 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3299 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 *
3301 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003302 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3303 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 */
3305 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003306ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003308 int count; /* repeat completion this many times; should
3309 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 int num_matches = -1;
3312 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003313 int todo = count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
3315 if (allow_get_expansion)
3316 {
3317 /* Delete old text to be replaced */
3318 ins_compl_delete();
3319 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003320 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003321
3322 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3323 * around. */
3324 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003326 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003328 compl_shown_match = compl_shown_match->cp_next;
3329 if (compl_shown_match->cp_next != NULL
3330 && compl_shown_match->cp_next == compl_first_match)
3331 break;
3332 }
3333 else if (compl_shows_dir == BACKWARD
3334 && compl_shown_match->cp_prev != NULL)
3335 {
3336 compl_shown_match = compl_shown_match->cp_prev;
3337 if (compl_shown_match == compl_first_match)
3338 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003341 {
3342 compl_pending = TRUE;
3343 if (allow_get_expansion)
3344 {
3345 num_matches = ins_compl_get_exp(&compl_startpos,
3346 compl_direction);
3347 if (compl_pending)
3348 {
3349 if (compl_direction == compl_shows_dir)
3350 compl_shown_match = compl_curr_match;
3351 }
3352 }
3353 else
3354 return -1;
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357
3358 /* Insert the text of the new completion */
3359 ins_compl_insert();
3360
3361 if (!allow_get_expansion)
3362 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003363 /* may undisplay the popup menu first */
3364 ins_compl_upd_pum();
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 /* Display the current match. */
3367 update_screen(0);
3368
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003369 /* display the updated popup menu */
3370 ins_compl_show_pum();
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 /* Delete old text to be replaced, since we're still searching and
3373 * don't want to match ourselves! */
3374 ins_compl_delete();
3375 }
3376
3377 /*
3378 * Show the file name for the match (if any)
3379 * Truncate the file name to avoid a wait for return.
3380 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003381 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003384 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (i <= 0)
3386 i = 0;
3387 else
3388 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003389 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 msg(IObuff);
3391 redraw_cmdline = FALSE; /* don't overwrite! */
3392 }
3393
3394 return num_matches;
3395}
3396
3397/*
3398 * Call this while finding completions, to check whether the user has hit a key
3399 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003400 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003402 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 */
3404 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003405ins_compl_check_keys(frequency)
3406 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 static int count = 0;
3409
3410 int c;
3411
3412 /* Don't check when reading keys from a script. That would break the test
3413 * scripts */
3414 if (using_script())
3415 return;
3416
3417 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003418 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 return;
3420 count = 0;
3421
3422 ++no_mapping;
3423 c = vpeekc_any();
3424 --no_mapping;
3425 if (c != NUL)
3426 {
3427 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3428 {
3429 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003430 compl_shows_dir = ins_compl_key2dir(c);
3431 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003434 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003437 (void)ins_compl_next(FALSE, 1);
3438}
3439
3440/*
3441 * Decide the direction of Insert mode complete from the key typed.
3442 * Returns BACKWARD or FORWARD.
3443 */
3444 static int
3445ins_compl_key2dir(c)
3446 int c;
3447{
3448 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3449 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3450 return BACKWARD;
3451 return FORWARD;
3452}
3453
3454/*
3455 * Decide the number of completions to move forward.
3456 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3457 */
3458 static int
3459ins_compl_key2count(c)
3460 int c;
3461{
3462 int h;
3463
3464 if (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3465 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN))
3466 {
3467 h = pum_get_height();
3468 if (h > 3)
3469 h -= 2; /* keep some context */
3470 return h;
3471 }
3472 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473}
3474
3475/*
3476 * Do Insert mode completion.
3477 * Called when character "c" was typed, which has a meaning for completion.
3478 * Returns OK if completion was done, FAIL if something failed (out of mem).
3479 */
3480 static int
3481ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003482 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003484 char_u *line;
3485 int startcol = 0; /* column where searched text starts */
3486 colnr_T curs_col; /* cursor column */
3487 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaare3226be2005-12-18 22:10:00 +00003489 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003490 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 /* First time we hit ^N or ^P (in a row, I mean) */
3493
3494 /* Turn off 'sm' so we don't show matches with ^X^L */
3495 save_sm = p_sm;
3496 p_sm = FALSE;
3497
3498 did_ai = FALSE;
3499#ifdef FEAT_SMARTINDENT
3500 did_si = FALSE;
3501 can_si = FALSE;
3502 can_si_back = FALSE;
3503#endif
3504 if (stop_arrow() == FAIL)
3505 return FAIL;
3506
3507 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003508 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003511 * "compl_startpos" to the cursor as a pattern to add a new word
3512 * instead of expand the one before the cursor, in word-wise if
3513 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 * is not in the same line as the cursor then fix it (the line has
3515 * been split because it was longer than 'tw'). if SOL is set then
3516 * skip the previous pattern, a word at the beginning of the line has
3517 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003518 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 /*
3521 * it is a continued search
3522 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003523 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3525 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3526 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003527 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003529 /* line (probably) wrapped, set compl_startpos to the
3530 * first non_blank in the line, if it is not a wordchar
3531 * include it to get a better pattern, but then we don't
3532 * want the "\\<" prefix, check it bellow */
3533 compl_col = (colnr_T)(skipwhite(line) - line);
3534 compl_startpos.col = compl_col;
3535 compl_startpos.lnum = curwin->w_cursor.lnum;
3536 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 else
3539 {
3540 /* S_IPOS was set when we inserted a word that was at the
3541 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 * mode but first we need to redefine compl_startpos */
3543 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003545 compl_cont_status |= CONT_SOL;
3546 compl_startpos.col = (colnr_T)(skipwhite(
3547 line + compl_length
3548 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003553 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 * have enough space? just being paranoic */
3555#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003556 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003558 compl_cont_status &= ~CONT_SOL;
3559 compl_length = (IOSIZE - MIN_SPACE);
3560 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3563 if (compl_length < 1)
3564 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003572 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003574 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003576 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003578 compl_cont_status = 0;
3579 compl_cont_status |= CONT_N_ADDS;
3580 compl_startpos = curwin->w_cursor;
3581 startcol = (int)curs_col;
3582 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584
3585 /* Work out completion pattern and original text -- webb */
3586 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3587 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3590 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003593 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003595 compl_col += ++startcol;
3596 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 compl_pattern = str_foldcase(line + compl_col,
3600 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 compl_pattern = vim_strnsave(line + compl_col,
3603 compl_length);
3604 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 return FAIL;
3606 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 char_u *prefix = (char_u *)"\\<";
3610
3611 /* we need 3 extra chars, 1 for the NUL and
3612 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003613 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3614 compl_length) + 3);
3615 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 if (!vim_iswordp(line + compl_col)
3618 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 && (
3620#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003623 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624#endif
3625 )))
3626 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003627 STRCPY((char *)compl_pattern, prefix);
3628 (void)quote_meta(compl_pattern + STRLEN(prefix),
3629 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003631 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003633 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003635 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636#endif
3637 )
3638 {
3639 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3641 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003643 compl_col += curs_col;
3644 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 else
3647 {
3648#ifdef FEAT_MBYTE
3649 /* Search the point of change class of multibyte character
3650 * or not a word single byte character backward. */
3651 if (has_mbyte)
3652 {
3653 int base_class;
3654 int head_off;
3655
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656 startcol -= (*mb_head_off)(line, line + startcol);
3657 base_class = mb_get_class(line + startcol);
3658 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003660 head_off = (*mb_head_off)(line, line + startcol);
3661 if (base_class != mb_get_class(line + startcol
3662 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 }
3667 else
3668#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003669 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003671 compl_col += ++startcol;
3672 compl_length = (int)curs_col - startcol;
3673 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 /* Only match word with at least two chars -- webb
3676 * there's no need to call quote_meta,
3677 * alloc(7) is enough -- Acevedo
3678 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 compl_pattern = alloc(7);
3680 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003682 STRCPY((char *)compl_pattern, "\\<");
3683 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3684 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 else
3687 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003688 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3689 compl_length) + 3);
3690 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 STRCPY((char *)compl_pattern, "\\<");
3693 (void)quote_meta(compl_pattern + 2, line + compl_col,
3694 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 }
3697 }
3698 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3699 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003700 compl_col = skipwhite(line) - line;
3701 compl_length = (int)curs_col - (int)compl_col;
3702 if (compl_length < 0) /* cursor in indent: empty pattern */
3703 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003705 compl_pattern = str_foldcase(line + compl_col, compl_length,
3706 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003708 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3709 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 return FAIL;
3711 }
3712 else if (ctrl_x_mode == CTRL_X_FILES)
3713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003716 compl_col += ++startcol;
3717 compl_length = (int)curs_col - startcol;
3718 compl_pattern = addstar(line + compl_col, compl_length,
3719 EXPAND_FILES);
3720 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 return FAIL;
3722 }
3723 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3724 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003725 compl_pattern = vim_strnsave(line, curs_col);
3726 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003728 set_cmd_context(&compl_xp, compl_pattern,
3729 (int)STRLEN(compl_pattern), curs_col);
3730 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3731 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3734 compl_col = startcol;
3735 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003737 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003738 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003739#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003740 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003741 * Call user defined function 'completefunc' with "a:findstart"
3742 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003743 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003744 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003745 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003746 char_u *funcname;
3747 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003748
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003749 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003750 * string */
3751 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3752 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3753 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003754 {
3755 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3756 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003757 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003758 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003759
3760 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003761 args[1] = NULL;
3762 pos = curwin->w_cursor;
3763 col = call_func_retnr(funcname, 2, args, FALSE);
3764 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003765
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003766 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003767 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003768 compl_col = col;
3769 if ((colnr_T)compl_col > curs_col)
3770 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003771
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003772 /* Setup variables for completion. Need to obtain "line" again,
3773 * it may have become invalid. */
3774 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003775 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3777 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003778#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 return FAIL;
3780 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003781 else if (ctrl_x_mode == CTRL_X_SPELL)
3782 {
3783#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003784 if (spell_bad_len > 0)
3785 compl_col = curs_col - spell_bad_len;
3786 else
3787 compl_col = spell_word_start(startcol);
3788 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003789 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003790 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003791 compl_length = (int)curs_col - compl_col;
3792 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3793 if (compl_pattern == NULL)
3794#endif
3795 return FAIL;
3796 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003797 else
3798 {
3799 EMSG2(_(e_intern2), "ins_complete()");
3800 return FAIL;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003803 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 edit_submode_pre = (char_u *)_(" Adding");
3806 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3807 {
3808 /* Insert a new line, keep indentation but ignore 'comments' */
3809#ifdef FEAT_COMMENTS
3810 char_u *old = curbuf->b_p_com;
3811
3812 curbuf->b_p_com = (char_u *)"";
3813#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003814 compl_startpos.lnum = curwin->w_cursor.lnum;
3815 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 ins_eol('\r');
3817#ifdef FEAT_COMMENTS
3818 curbuf->b_p_com = old;
3819#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 compl_length = 0;
3821 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 }
3824 else
3825 {
3826 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 if (compl_cont_status & CONT_LOCAL)
3831 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
3833 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003836 * "compl_orig_text" itself (not a copy) is added, it will be freed
3837 * when the list of matches is freed. */
3838 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3839 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3840 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 vim_free(compl_pattern);
3843 compl_pattern = NULL;
3844 vim_free(compl_orig_text);
3845 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return FAIL;
3847 }
3848
3849 /* showmode might reset the internal line pointers, so it must
3850 * be called before line = ml_get(), or when this address is no
3851 * longer needed. -- Acevedo.
3852 */
3853 edit_submode_extra = (char_u *)_("-- Searching...");
3854 edit_submode_highl = HLF_COUNT;
3855 showmode();
3856 edit_submode_extra = NULL;
3857 out_flush();
3858 }
3859
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 compl_shown_match = compl_curr_match;
3861 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 /*
3864 * Find next match.
3865 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003866 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003868 /* may undisplay the popup menu */
3869 ins_compl_upd_pum();
3870
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 if (n > 1) /* all matches have been found */
3872 compl_matches = n;
3873 compl_curr_match = compl_shown_match;
3874 compl_direction = compl_shows_dir;
3875 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877 /* eat the ESC to avoid leaving insert mode */
3878 if (got_int && !global_busy)
3879 {
3880 (void)vgetc();
3881 got_int = FALSE;
3882 }
3883
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003884 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003885 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3888 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3890 edit_submode_highl = HLF_E;
3891 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3892 * because we couldn't expand anything at first place, but if we used
3893 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3894 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 if ( compl_length > 1
3896 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 || (ctrl_x_mode != 0
3898 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3899 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003900 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
Bram Moolenaar572cb562005-08-05 21:35:02 +00003903 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 if (edit_submode_extra == NULL)
3909 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003910 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
3912 edit_submode_extra = (char_u *)_("Back at original");
3913 edit_submode_highl = HLF_W;
3914 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003915 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
3917 edit_submode_extra = (char_u *)_("Word from other line");
3918 edit_submode_highl = HLF_COUNT;
3919 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003920 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
3922 edit_submode_extra = (char_u *)_("The only match");
3923 edit_submode_highl = HLF_COUNT;
3924 }
3925 else
3926 {
3927 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003928 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003930 int number = 0;
3931 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003933 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
3935 /* search backwards for the first valid (!= -1) number.
3936 * This should normally succeed already at the first loop
3937 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003938 for (match = compl_curr_match->cp_prev; match != NULL
3939 && match != compl_first_match;
3940 match = match->cp_prev)
3941 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003943 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 break;
3945 }
3946 if (match != NULL)
3947 /* go up and assign all numbers which are not assigned
3948 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003949 for (match = match->cp_next;
3950 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003951 match = match->cp_next)
3952 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 else /* BACKWARD */
3955 {
3956 /* search forwards (upwards) for the first valid (!= -1)
3957 * number. This should normally succeed already at the
3958 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003959 for (match = compl_curr_match->cp_next; match != NULL
3960 && match != compl_first_match;
3961 match = match->cp_next)
3962 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003964 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 break;
3966 }
3967 if (match != NULL)
3968 /* go down and assign all numbers which are not
3969 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003970 for (match = match->cp_prev; match
3971 && match->cp_number == -1;
3972 match = match->cp_prev)
3973 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003977 /* The match should always have a sequence number now, this is
3978 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003979 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
3981 /* Space for 10 text chars. + 2x10-digit no.s */
3982 static char_u match_ref[31];
3983
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003984 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003986 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003988 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003989 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003990 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 edit_submode_extra = match_ref;
3992 edit_submode_highl = HLF_R;
3993 if (dollar_vcol)
3994 curs_columns(FALSE);
3995 }
3996 }
3997 }
3998
3999 /* Show a message about what (completion) mode we're in. */
4000 showmode();
4001 if (edit_submode_extra != NULL)
4002 {
4003 if (!p_smd)
4004 msg_attr(edit_submode_extra,
4005 edit_submode_highl < HLF_COUNT
4006 ? hl_attr(edit_submode_highl) : 0);
4007 }
4008 else
4009 msg_clr_cmdline(); /* necessary for "noshowmode" */
4010
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004011 ins_compl_show_pum();
4012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return OK;
4014}
4015
4016/*
4017 * Looks in the first "len" chars. of "src" for search-metachars.
4018 * If dest is not NULL the chars. are copied there quoting (with
4019 * a backslash) the metachars, and dest would be NUL terminated.
4020 * Returns the length (needed) of dest
4021 */
4022 static int
4023quote_meta(dest, src, len)
4024 char_u *dest;
4025 char_u *src;
4026 int len;
4027{
4028 int m;
4029
4030 for (m = len; --len >= 0; src++)
4031 {
4032 switch (*src)
4033 {
4034 case '.':
4035 case '*':
4036 case '[':
4037 if (ctrl_x_mode == CTRL_X_DICTIONARY
4038 || ctrl_x_mode == CTRL_X_THESAURUS)
4039 break;
4040 case '~':
4041 if (!p_magic) /* quote these only if magic is set */
4042 break;
4043 case '\\':
4044 if (ctrl_x_mode == CTRL_X_DICTIONARY
4045 || ctrl_x_mode == CTRL_X_THESAURUS)
4046 break;
4047 case '^': /* currently it's not needed. */
4048 case '$':
4049 m++;
4050 if (dest != NULL)
4051 *dest++ = '\\';
4052 break;
4053 }
4054 if (dest != NULL)
4055 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004056# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 /* Copy remaining bytes of a multibyte character. */
4058 if (has_mbyte)
4059 {
4060 int i, mb_len;
4061
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004062 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 if (mb_len > 0 && len >= mb_len)
4064 for (i = 0; i < mb_len; ++i)
4065 {
4066 --len;
4067 ++src;
4068 if (dest != NULL)
4069 *dest++ = *src;
4070 }
4071 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 if (dest != NULL)
4075 *dest = NUL;
4076
4077 return m;
4078}
4079#endif /* FEAT_INS_EXPAND */
4080
4081/*
4082 * Next character is interpreted literally.
4083 * A one, two or three digit decimal number is interpreted as its byte value.
4084 * If one or two digits are entered, the next character is given to vungetc().
4085 * For Unicode a character > 255 may be returned.
4086 */
4087 int
4088get_literal()
4089{
4090 int cc;
4091 int nc;
4092 int i;
4093 int hex = FALSE;
4094 int octal = FALSE;
4095#ifdef FEAT_MBYTE
4096 int unicode = 0;
4097#endif
4098
4099 if (got_int)
4100 return Ctrl_C;
4101
4102#ifdef FEAT_GUI
4103 /*
4104 * In GUI there is no point inserting the internal code for a special key.
4105 * It is more useful to insert the string "<KEY>" instead. This would
4106 * probably be useful in a text window too, but it would not be
4107 * vi-compatible (maybe there should be an option for it?) -- webb
4108 */
4109 if (gui.in_use)
4110 ++allow_keys;
4111#endif
4112#ifdef USE_ON_FLY_SCROLL
4113 dont_scroll = TRUE; /* disallow scrolling here */
4114#endif
4115 ++no_mapping; /* don't map the next key hits */
4116 cc = 0;
4117 i = 0;
4118 for (;;)
4119 {
4120 do
4121 nc = safe_vgetc();
4122 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4123 || nc == K_HOR_SCROLLBAR);
4124#ifdef FEAT_CMDL_INFO
4125 if (!(State & CMDLINE)
4126# ifdef FEAT_MBYTE
4127 && MB_BYTE2LEN_CHECK(nc) == 1
4128# endif
4129 )
4130 add_to_showcmd(nc);
4131#endif
4132 if (nc == 'x' || nc == 'X')
4133 hex = TRUE;
4134 else if (nc == 'o' || nc == 'O')
4135 octal = TRUE;
4136#ifdef FEAT_MBYTE
4137 else if (nc == 'u' || nc == 'U')
4138 unicode = nc;
4139#endif
4140 else
4141 {
4142 if (hex
4143#ifdef FEAT_MBYTE
4144 || unicode != 0
4145#endif
4146 )
4147 {
4148 if (!vim_isxdigit(nc))
4149 break;
4150 cc = cc * 16 + hex2nr(nc);
4151 }
4152 else if (octal)
4153 {
4154 if (nc < '0' || nc > '7')
4155 break;
4156 cc = cc * 8 + nc - '0';
4157 }
4158 else
4159 {
4160 if (!VIM_ISDIGIT(nc))
4161 break;
4162 cc = cc * 10 + nc - '0';
4163 }
4164
4165 ++i;
4166 }
4167
4168 if (cc > 255
4169#ifdef FEAT_MBYTE
4170 && unicode == 0
4171#endif
4172 )
4173 cc = 255; /* limit range to 0-255 */
4174 nc = 0;
4175
4176 if (hex) /* hex: up to two chars */
4177 {
4178 if (i >= 2)
4179 break;
4180 }
4181#ifdef FEAT_MBYTE
4182 else if (unicode) /* Unicode: up to four or eight chars */
4183 {
4184 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4185 break;
4186 }
4187#endif
4188 else if (i >= 3) /* decimal or octal: up to three chars */
4189 break;
4190 }
4191 if (i == 0) /* no number entered */
4192 {
4193 if (nc == K_ZERO) /* NUL is stored as NL */
4194 {
4195 cc = '\n';
4196 nc = 0;
4197 }
4198 else
4199 {
4200 cc = nc;
4201 nc = 0;
4202 }
4203 }
4204
4205 if (cc == 0) /* NUL is stored as NL */
4206 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004207#ifdef FEAT_MBYTE
4208 if (enc_dbcs && (cc & 0xff) == 0)
4209 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4210 second byte will cause trouble! */
4211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 --no_mapping;
4214#ifdef FEAT_GUI
4215 if (gui.in_use)
4216 --allow_keys;
4217#endif
4218 if (nc)
4219 vungetc(nc);
4220 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4221 return cc;
4222}
4223
4224/*
4225 * Insert character, taking care of special keys and mod_mask
4226 */
4227 static void
4228insert_special(c, allow_modmask, ctrlv)
4229 int c;
4230 int allow_modmask;
4231 int ctrlv; /* c was typed after CTRL-V */
4232{
4233 char_u *p;
4234 int len;
4235
4236 /*
4237 * Special function key, translate into "<Key>". Up to the last '>' is
4238 * inserted with ins_str(), so as not to replace characters in replace
4239 * mode.
4240 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4241 * unless 'allow_modmask' is TRUE.
4242 */
4243#ifdef MACOS
4244 /* Command-key never produces a normal key */
4245 if (mod_mask & MOD_MASK_CMD)
4246 allow_modmask = TRUE;
4247#endif
4248 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4249 {
4250 p = get_special_key_name(c, mod_mask);
4251 len = (int)STRLEN(p);
4252 c = p[len - 1];
4253 if (len > 2)
4254 {
4255 if (stop_arrow() == FAIL)
4256 return;
4257 p[len - 1] = NUL;
4258 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004259 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 ctrlv = FALSE;
4261 }
4262 }
4263 if (stop_arrow() == OK)
4264 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4265}
4266
4267/*
4268 * Special characters in this context are those that need processing other
4269 * than the simple insertion that can be performed here. This includes ESC
4270 * which terminates the insert, and CR/NL which need special processing to
4271 * open up a new line. This routine tries to optimize insertions performed by
4272 * the "redo", "undo" or "put" commands, so it needs to know when it should
4273 * stop and defer processing to the "normal" mechanism.
4274 * '0' and '^' are special, because they can be followed by CTRL-D.
4275 */
4276#ifdef EBCDIC
4277# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4278#else
4279# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4280#endif
4281
4282#ifdef FEAT_MBYTE
4283# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4284#else
4285# define WHITECHAR(cc) vim_iswhite(cc)
4286#endif
4287
4288 void
4289insertchar(c, flags, second_indent)
4290 int c; /* character to insert or NUL */
4291 int flags; /* INSCHAR_FORMAT, etc. */
4292 int second_indent; /* indent for second line if >= 0 */
4293{
4294 int haveto_redraw = FALSE;
4295 int textwidth;
4296#ifdef FEAT_COMMENTS
4297 colnr_T leader_len;
4298 char_u *p;
4299 int no_leader = FALSE;
4300 int do_comments = (flags & INSCHAR_DO_COM);
4301#endif
4302 int fo_white_par;
4303 int first_line = TRUE;
4304 int fo_ins_blank;
4305#ifdef FEAT_MBYTE
4306 int fo_multibyte;
4307#endif
4308 int save_char = NUL;
4309 int cc;
4310
4311 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4312 fo_ins_blank = has_format_option(FO_INS_BLANK);
4313#ifdef FEAT_MBYTE
4314 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4315#endif
4316 fo_white_par = has_format_option(FO_WHITE_PAR);
4317
4318 /*
4319 * Try to break the line in two or more pieces when:
4320 * - Always do this if we have been called to do formatting only.
4321 * - Always do this when 'formatoptions' has the 'a' flag and the line
4322 * ends in white space.
4323 * - Otherwise:
4324 * - Don't do this if inserting a blank
4325 * - Don't do this if an existing character is being replaced, unless
4326 * we're in VREPLACE mode.
4327 * - Do this if the cursor is not on the line where insert started
4328 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4329 * before the insert.
4330 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4331 * before 'textwidth'
4332 */
4333 if (textwidth
4334 && ((flags & INSCHAR_FORMAT)
4335 || (!vim_iswhite(c)
4336 && !((State & REPLACE_FLAG)
4337#ifdef FEAT_VREPLACE
4338 && !(State & VREPLACE_FLAG)
4339#endif
4340 && *ml_get_cursor() != NUL)
4341 && (curwin->w_cursor.lnum != Insstart.lnum
4342 || ((!has_format_option(FO_INS_LONG)
4343 || Insstart_textlen <= (colnr_T)textwidth)
4344 && (!fo_ins_blank
4345 || Insstart_blank_vcol <= (colnr_T)textwidth
4346 ))))))
4347 {
4348 /*
4349 * When 'ai' is off we don't want a space under the cursor to be
4350 * deleted. Replace it with an 'x' temporarily.
4351 */
4352 if (!curbuf->b_p_ai)
4353 {
4354 cc = gchar_cursor();
4355 if (vim_iswhite(cc))
4356 {
4357 save_char = cc;
4358 pchar_cursor('x');
4359 }
4360 }
4361
4362 /*
4363 * Repeat breaking lines, until the current line is not too long.
4364 */
4365 while (!got_int)
4366 {
4367 int startcol; /* Cursor column at entry */
4368 int wantcol; /* column at textwidth border */
4369 int foundcol; /* column for start of spaces */
4370 int end_foundcol = 0; /* column for start of word */
4371 colnr_T len;
4372 colnr_T virtcol;
4373#ifdef FEAT_VREPLACE
4374 int orig_col = 0;
4375 char_u *saved_text = NULL;
4376#endif
4377 colnr_T col;
4378
4379 virtcol = get_nolist_virtcol();
4380 if (virtcol < (colnr_T)textwidth)
4381 break;
4382
4383#ifdef FEAT_COMMENTS
4384 if (no_leader)
4385 do_comments = FALSE;
4386 else if (!(flags & INSCHAR_FORMAT)
4387 && has_format_option(FO_WRAP_COMS))
4388 do_comments = TRUE;
4389
4390 /* Don't break until after the comment leader */
4391 if (do_comments)
4392 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4393 else
4394 leader_len = 0;
4395
4396 /* If the line doesn't start with a comment leader, then don't
4397 * start one in a following broken line. Avoids that a %word
4398 * moved to the start of the next line causes all following lines
4399 * to start with %. */
4400 if (leader_len == 0)
4401 no_leader = TRUE;
4402#endif
4403 if (!(flags & INSCHAR_FORMAT)
4404#ifdef FEAT_COMMENTS
4405 && leader_len == 0
4406#endif
4407 && !has_format_option(FO_WRAP))
4408
4409 {
4410 textwidth = 0;
4411 break;
4412 }
4413 if ((startcol = curwin->w_cursor.col) == 0)
4414 break;
4415
4416 /* find column of textwidth border */
4417 coladvance((colnr_T)textwidth);
4418 wantcol = curwin->w_cursor.col;
4419
4420 curwin->w_cursor.col = startcol - 1;
4421#ifdef FEAT_MBYTE
4422 /* Correct cursor for multi-byte character. */
4423 if (has_mbyte)
4424 mb_adjust_cursor();
4425#endif
4426 foundcol = 0;
4427
4428 /*
4429 * Find position to break at.
4430 * Stop at first entered white when 'formatoptions' has 'v'
4431 */
4432 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4433 || curwin->w_cursor.lnum != Insstart.lnum
4434 || curwin->w_cursor.col >= Insstart.col)
4435 {
4436 cc = gchar_cursor();
4437 if (WHITECHAR(cc))
4438 {
4439 /* remember position of blank just before text */
4440 end_foundcol = curwin->w_cursor.col;
4441
4442 /* find start of sequence of blanks */
4443 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4444 {
4445 dec_cursor();
4446 cc = gchar_cursor();
4447 }
4448 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4449 break; /* only spaces in front of text */
4450#ifdef FEAT_COMMENTS
4451 /* Don't break until after the comment leader */
4452 if (curwin->w_cursor.col < leader_len)
4453 break;
4454#endif
4455 if (has_format_option(FO_ONE_LETTER))
4456 {
4457 /* do not break after one-letter words */
4458 if (curwin->w_cursor.col == 0)
4459 break; /* one-letter word at begin */
4460
4461 col = curwin->w_cursor.col;
4462 dec_cursor();
4463 cc = gchar_cursor();
4464
4465 if (WHITECHAR(cc))
4466 continue; /* one-letter, continue */
4467 curwin->w_cursor.col = col;
4468 }
4469#ifdef FEAT_MBYTE
4470 if (has_mbyte)
4471 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004472 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 else
4474#endif
4475 foundcol = curwin->w_cursor.col + 1;
4476 if (curwin->w_cursor.col < (colnr_T)wantcol)
4477 break;
4478 }
4479#ifdef FEAT_MBYTE
4480 else if (cc >= 0x100 && fo_multibyte
4481 && curwin->w_cursor.col <= (colnr_T)wantcol)
4482 {
4483 /* Break after or before a multi-byte character. */
4484 foundcol = curwin->w_cursor.col;
4485 if (curwin->w_cursor.col < (colnr_T)wantcol)
4486 foundcol += (*mb_char2len)(cc);
4487 end_foundcol = foundcol;
4488 break;
4489 }
4490#endif
4491 if (curwin->w_cursor.col == 0)
4492 break;
4493 dec_cursor();
4494 }
4495
4496 if (foundcol == 0) /* no spaces, cannot break line */
4497 {
4498 curwin->w_cursor.col = startcol;
4499 break;
4500 }
4501
4502 /* Going to break the line, remove any "$" now. */
4503 undisplay_dollar();
4504
4505 /*
4506 * Offset between cursor position and line break is used by replace
4507 * stack functions. VREPLACE does not use this, and backspaces
4508 * over the text instead.
4509 */
4510#ifdef FEAT_VREPLACE
4511 if (State & VREPLACE_FLAG)
4512 orig_col = startcol; /* Will start backspacing from here */
4513 else
4514#endif
4515 replace_offset = startcol - end_foundcol - 1;
4516
4517 /*
4518 * adjust startcol for spaces that will be deleted and
4519 * characters that will remain on top line
4520 */
4521 curwin->w_cursor.col = foundcol;
4522 while (cc = gchar_cursor(), WHITECHAR(cc))
4523 inc_cursor();
4524 startcol -= curwin->w_cursor.col;
4525 if (startcol < 0)
4526 startcol = 0;
4527
4528#ifdef FEAT_VREPLACE
4529 if (State & VREPLACE_FLAG)
4530 {
4531 /*
4532 * In VREPLACE mode, we will backspace over the text to be
4533 * wrapped, so save a copy now to put on the next line.
4534 */
4535 saved_text = vim_strsave(ml_get_cursor());
4536 curwin->w_cursor.col = orig_col;
4537 if (saved_text == NULL)
4538 break; /* Can't do it, out of memory */
4539 saved_text[startcol] = NUL;
4540
4541 /* Backspace over characters that will move to the next line */
4542 if (!fo_white_par)
4543 backspace_until_column(foundcol);
4544 }
4545 else
4546#endif
4547 {
4548 /* put cursor after pos. to break line */
4549 if (!fo_white_par)
4550 curwin->w_cursor.col = foundcol;
4551 }
4552
4553 /*
4554 * Split the line just before the margin.
4555 * Only insert/delete lines, but don't really redraw the window.
4556 */
4557 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4558 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4559#ifdef FEAT_COMMENTS
4560 + (do_comments ? OPENLINE_DO_COM : 0)
4561#endif
4562 , old_indent);
4563 old_indent = 0;
4564
4565 replace_offset = 0;
4566 if (first_line)
4567 {
4568 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4569 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4570 if (second_indent >= 0)
4571 {
4572#ifdef FEAT_VREPLACE
4573 if (State & VREPLACE_FLAG)
4574 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4575 else
4576#endif
4577 (void)set_indent(second_indent, SIN_CHANGED);
4578 }
4579 first_line = FALSE;
4580 }
4581
4582#ifdef FEAT_VREPLACE
4583 if (State & VREPLACE_FLAG)
4584 {
4585 /*
4586 * In VREPLACE mode we have backspaced over the text to be
4587 * moved, now we re-insert it into the new line.
4588 */
4589 ins_bytes(saved_text);
4590 vim_free(saved_text);
4591 }
4592 else
4593#endif
4594 {
4595 /*
4596 * Check if cursor is not past the NUL off the line, cindent
4597 * may have added or removed indent.
4598 */
4599 curwin->w_cursor.col += startcol;
4600 len = (colnr_T)STRLEN(ml_get_curline());
4601 if (curwin->w_cursor.col > len)
4602 curwin->w_cursor.col = len;
4603 }
4604
4605 haveto_redraw = TRUE;
4606#ifdef FEAT_CINDENT
4607 can_cindent = TRUE;
4608#endif
4609 /* moved the cursor, don't autoindent or cindent now */
4610 did_ai = FALSE;
4611#ifdef FEAT_SMARTINDENT
4612 did_si = FALSE;
4613 can_si = FALSE;
4614 can_si_back = FALSE;
4615#endif
4616 line_breakcheck();
4617 }
4618
4619 if (save_char) /* put back space after cursor */
4620 pchar_cursor(save_char);
4621
4622 if (c == NUL) /* formatting only */
4623 return;
4624 if (haveto_redraw)
4625 {
4626 update_topline();
4627 redraw_curbuf_later(VALID);
4628 }
4629 }
4630 if (c == NUL) /* only formatting was wanted */
4631 return;
4632
4633#ifdef FEAT_COMMENTS
4634 /* Check whether this character should end a comment. */
4635 if (did_ai && (int)c == end_comment_pending)
4636 {
4637 char_u *line;
4638 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4639 int middle_len, end_len;
4640 int i;
4641
4642 /*
4643 * Need to remove existing (middle) comment leader and insert end
4644 * comment leader. First, check what comment leader we can find.
4645 */
4646 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4647 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4648 {
4649 /* Skip middle-comment string */
4650 while (*p && p[-1] != ':') /* find end of middle flags */
4651 ++p;
4652 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4653 /* Don't count trailing white space for middle_len */
4654 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4655 --middle_len;
4656
4657 /* Find the end-comment string */
4658 while (*p && p[-1] != ':') /* find end of end flags */
4659 ++p;
4660 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4661
4662 /* Skip white space before the cursor */
4663 i = curwin->w_cursor.col;
4664 while (--i >= 0 && vim_iswhite(line[i]))
4665 ;
4666 i++;
4667
4668 /* Skip to before the middle leader */
4669 i -= middle_len;
4670
4671 /* Check some expected things before we go on */
4672 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4673 {
4674 /* Backspace over all the stuff we want to replace */
4675 backspace_until_column(i);
4676
4677 /*
4678 * Insert the end-comment string, except for the last
4679 * character, which will get inserted as normal later.
4680 */
4681 ins_bytes_len(lead_end, end_len - 1);
4682 }
4683 }
4684 }
4685 end_comment_pending = NUL;
4686#endif
4687
4688 did_ai = FALSE;
4689#ifdef FEAT_SMARTINDENT
4690 did_si = FALSE;
4691 can_si = FALSE;
4692 can_si_back = FALSE;
4693#endif
4694
4695 /*
4696 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4697 * This speeds up normal text input considerably.
4698 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4699 * need to re-indent at a ':', or any other character (but not what
4700 * 'paste' is set)..
4701 */
4702#ifdef USE_ON_FLY_SCROLL
4703 dont_scroll = FALSE; /* allow scrolling here */
4704#endif
4705
4706 if ( !ISSPECIAL(c)
4707#ifdef FEAT_MBYTE
4708 && (!has_mbyte || (*mb_char2len)(c) == 1)
4709#endif
4710 && vpeekc() != NUL
4711 && !(State & REPLACE_FLAG)
4712#ifdef FEAT_CINDENT
4713 && !cindent_on()
4714#endif
4715#ifdef FEAT_RIGHTLEFT
4716 && !p_ri
4717#endif
4718 )
4719 {
4720#define INPUT_BUFLEN 100
4721 char_u buf[INPUT_BUFLEN + 1];
4722 int i;
4723 colnr_T virtcol = 0;
4724
4725 buf[0] = c;
4726 i = 1;
4727 if (textwidth)
4728 virtcol = get_nolist_virtcol();
4729 /*
4730 * Stop the string when:
4731 * - no more chars available
4732 * - finding a special character (command key)
4733 * - buffer is full
4734 * - running into the 'textwidth' boundary
4735 * - need to check for abbreviation: A non-word char after a word-char
4736 */
4737 while ( (c = vpeekc()) != NUL
4738 && !ISSPECIAL(c)
4739#ifdef FEAT_MBYTE
4740 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4741#endif
4742 && i < INPUT_BUFLEN
4743 && (textwidth == 0
4744 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4745 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4746 {
4747#ifdef FEAT_RIGHTLEFT
4748 c = vgetc();
4749 if (p_hkmap && KeyTyped)
4750 c = hkmap(c); /* Hebrew mode mapping */
4751# ifdef FEAT_FKMAP
4752 if (p_fkmap && KeyTyped)
4753 c = fkmap(c); /* Farsi mode mapping */
4754# endif
4755 buf[i++] = c;
4756#else
4757 buf[i++] = vgetc();
4758#endif
4759 }
4760
4761#ifdef FEAT_DIGRAPHS
4762 do_digraph(-1); /* clear digraphs */
4763 do_digraph(buf[i-1]); /* may be the start of a digraph */
4764#endif
4765 buf[i] = NUL;
4766 ins_str(buf);
4767 if (flags & INSCHAR_CTRLV)
4768 {
4769 redo_literal(*buf);
4770 i = 1;
4771 }
4772 else
4773 i = 0;
4774 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004775 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777 else
4778 {
4779#ifdef FEAT_MBYTE
4780 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4781 {
4782 char_u buf[MB_MAXBYTES + 1];
4783
4784 (*mb_char2bytes)(c, buf);
4785 buf[cc] = NUL;
4786 ins_char_bytes(buf, cc);
4787 AppendCharToRedobuff(c);
4788 }
4789 else
4790#endif
4791 {
4792 ins_char(c);
4793 if (flags & INSCHAR_CTRLV)
4794 redo_literal(c);
4795 else
4796 AppendCharToRedobuff(c);
4797 }
4798 }
4799}
4800
4801/*
4802 * Called after inserting or deleting text: When 'formatoptions' includes the
4803 * 'a' flag format from the current line until the end of the paragraph.
4804 * Keep the cursor at the same position relative to the text.
4805 * The caller must have saved the cursor line for undo, following ones will be
4806 * saved here.
4807 */
4808 void
4809auto_format(trailblank, prev_line)
4810 int trailblank; /* when TRUE also format with trailing blank */
4811 int prev_line; /* may start in previous line */
4812{
4813 pos_T pos;
4814 colnr_T len;
4815 char_u *old;
4816 char_u *new, *pnew;
4817 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004818 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 if (!has_format_option(FO_AUTO))
4821 return;
4822
4823 pos = curwin->w_cursor;
4824 old = ml_get_curline();
4825
4826 /* may remove added space */
4827 check_auto_format(FALSE);
4828
4829 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4830 * user might insert normal text next. Also skip formatting when "1" is
4831 * in 'formatoptions' and there is a single character before the cursor.
4832 * Otherwise the line would be broken and when typing another non-white
4833 * next they are not joined back together. */
4834 wasatend = (pos.col == STRLEN(old));
4835 if (*old != NUL && !trailblank && wasatend)
4836 {
4837 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004838 cc = gchar_cursor();
4839 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4840 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004842 cc = gchar_cursor();
4843 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 curwin->w_cursor = pos;
4846 return;
4847 }
4848 curwin->w_cursor = pos;
4849 }
4850
4851#ifdef FEAT_COMMENTS
4852 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4853 * comments. */
4854 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4855 && get_leader_len(old, NULL, FALSE) == 0)
4856 return;
4857#endif
4858
4859 /*
4860 * May start formatting in a previous line, so that after "x" a word is
4861 * moved to the previous line if it fits there now. Only when this is not
4862 * the start of a paragraph.
4863 */
4864 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4865 {
4866 --curwin->w_cursor.lnum;
4867 if (u_save_cursor() == FAIL)
4868 return;
4869 }
4870
4871 /*
4872 * Do the formatting and restore the cursor position. "saved_cursor" will
4873 * be adjusted for the text formatting.
4874 */
4875 saved_cursor = pos;
4876 format_lines((linenr_T)-1);
4877 curwin->w_cursor = saved_cursor;
4878 saved_cursor.lnum = 0;
4879
4880 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4881 {
4882 /* "cannot happen" */
4883 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4884 coladvance((colnr_T)MAXCOL);
4885 }
4886 else
4887 check_cursor_col();
4888
4889 /* Insert mode: If the cursor is now after the end of the line while it
4890 * previously wasn't, the line was broken. Because of the rule above we
4891 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4892 * formatted. */
4893 if (!wasatend && has_format_option(FO_WHITE_PAR))
4894 {
4895 new = ml_get_curline();
4896 len = STRLEN(new);
4897 if (curwin->w_cursor.col == len)
4898 {
4899 pnew = vim_strnsave(new, len + 2);
4900 pnew[len] = ' ';
4901 pnew[len + 1] = NUL;
4902 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4903 /* remove the space later */
4904 did_add_space = TRUE;
4905 }
4906 else
4907 /* may remove added space */
4908 check_auto_format(FALSE);
4909 }
4910
4911 check_cursor();
4912}
4913
4914/*
4915 * When an extra space was added to continue a paragraph for auto-formatting,
4916 * delete it now. The space must be under the cursor, just after the insert
4917 * position.
4918 */
4919 static void
4920check_auto_format(end_insert)
4921 int end_insert; /* TRUE when ending Insert mode */
4922{
4923 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004924 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 if (did_add_space)
4927 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004928 cc = gchar_cursor();
4929 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 /* Somehow the space was removed already. */
4931 did_add_space = FALSE;
4932 else
4933 {
4934 if (!end_insert)
4935 {
4936 inc_cursor();
4937 c = gchar_cursor();
4938 dec_cursor();
4939 }
4940 if (c != NUL)
4941 {
4942 /* The space is no longer at the end of the line, delete it. */
4943 del_char(FALSE);
4944 did_add_space = FALSE;
4945 }
4946 }
4947 }
4948}
4949
4950/*
4951 * Find out textwidth to be used for formatting:
4952 * if 'textwidth' option is set, use it
4953 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4954 * if invalid value, use 0.
4955 * Set default to window width (maximum 79) for "gq" operator.
4956 */
4957 int
4958comp_textwidth(ff)
4959 int ff; /* force formatting (for "Q" command) */
4960{
4961 int textwidth;
4962
4963 textwidth = curbuf->b_p_tw;
4964 if (textwidth == 0 && curbuf->b_p_wm)
4965 {
4966 /* The width is the window width minus 'wrapmargin' minus all the
4967 * things that add to the margin. */
4968 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4969#ifdef FEAT_CMDWIN
4970 if (cmdwin_type != 0)
4971 textwidth -= 1;
4972#endif
4973#ifdef FEAT_FOLDING
4974 textwidth -= curwin->w_p_fdc;
4975#endif
4976#ifdef FEAT_SIGNS
4977 if (curwin->w_buffer->b_signlist != NULL
4978# ifdef FEAT_NETBEANS_INTG
4979 || usingNetbeans
4980# endif
4981 )
4982 textwidth -= 1;
4983#endif
4984 if (curwin->w_p_nu)
4985 textwidth -= 8;
4986 }
4987 if (textwidth < 0)
4988 textwidth = 0;
4989 if (ff && textwidth == 0)
4990 {
4991 textwidth = W_WIDTH(curwin) - 1;
4992 if (textwidth > 79)
4993 textwidth = 79;
4994 }
4995 return textwidth;
4996}
4997
4998/*
4999 * Put a character in the redo buffer, for when just after a CTRL-V.
5000 */
5001 static void
5002redo_literal(c)
5003 int c;
5004{
5005 char_u buf[10];
5006
5007 /* Only digits need special treatment. Translate them into a string of
5008 * three digits. */
5009 if (VIM_ISDIGIT(c))
5010 {
5011 sprintf((char *)buf, "%03d", c);
5012 AppendToRedobuff(buf);
5013 }
5014 else
5015 AppendCharToRedobuff(c);
5016}
5017
5018/*
5019 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005020 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 */
5022 static void
5023start_arrow(end_insert_pos)
5024 pos_T *end_insert_pos;
5025{
5026 if (!arrow_used) /* something has been inserted */
5027 {
5028 AppendToRedobuff(ESC_STR);
5029 stop_insert(end_insert_pos, FALSE);
5030 arrow_used = TRUE; /* this means we stopped the current insert */
5031 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005032#ifdef FEAT_SYN_HL
5033 check_spell_redraw();
5034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035}
5036
Bram Moolenaar217ad922005-03-20 22:37:15 +00005037#ifdef FEAT_SYN_HL
5038/*
5039 * If we skipped highlighting word at cursor, do it now.
5040 * It may be skipped again, thus reset spell_redraw_lnum first.
5041 */
5042 static void
5043check_spell_redraw()
5044{
5045 if (spell_redraw_lnum != 0)
5046 {
5047 linenr_T lnum = spell_redraw_lnum;
5048
5049 spell_redraw_lnum = 0;
5050 redrawWinline(lnum, FALSE);
5051 }
5052}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005053
5054/*
5055 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5056 * spelled word, if there is one.
5057 */
5058 static void
5059spell_back_to_badword()
5060{
5061 pos_T tpos = curwin->w_cursor;
5062
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005063 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005064 if (curwin->w_cursor.col != tpos.col)
5065 start_arrow(&tpos);
5066}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005067#endif
5068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069/*
5070 * stop_arrow() is called before a change is made in insert mode.
5071 * If an arrow key has been used, start a new insertion.
5072 * Returns FAIL if undo is impossible, shouldn't insert then.
5073 */
5074 int
5075stop_arrow()
5076{
5077 if (arrow_used)
5078 {
5079 if (u_save_cursor() == OK)
5080 {
5081 arrow_used = FALSE;
5082 ins_need_undo = FALSE;
5083 }
5084 Insstart = curwin->w_cursor; /* new insertion starts here */
5085 Insstart_textlen = linetabsize(ml_get_curline());
5086 ai_col = 0;
5087#ifdef FEAT_VREPLACE
5088 if (State & VREPLACE_FLAG)
5089 {
5090 orig_line_count = curbuf->b_ml.ml_line_count;
5091 vr_lines_changed = 1;
5092 }
5093#endif
5094 ResetRedobuff();
5095 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005096 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else if (ins_need_undo)
5099 {
5100 if (u_save_cursor() == OK)
5101 ins_need_undo = FALSE;
5102 }
5103
5104#ifdef FEAT_FOLDING
5105 /* Always open fold at the cursor line when inserting something. */
5106 foldOpenCursor();
5107#endif
5108
5109 return (arrow_used || ins_need_undo ? FAIL : OK);
5110}
5111
5112/*
5113 * do a few things to stop inserting
5114 */
5115 static void
5116stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005117 pos_T *end_insert_pos; /* where insert ended */
5118 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005120 int cc;
5121 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 stop_redo_ins();
5124 replace_flush(); /* abandon replace stack */
5125
5126 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005127 * Save the inserted text for later redo with ^@ and CTRL-A.
5128 * Don't do it when "restart_edit" was set and nothing was inserted,
5129 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005131 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005132 if (did_restart_edit == 0 || (ptr != NULL
5133 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005134 {
5135 vim_free(last_insert);
5136 last_insert = ptr;
5137 last_insert_skip = new_insert_skip;
5138 }
5139 else
5140 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 if (!arrow_used)
5143 {
5144 /* Auto-format now. It may seem strange to do this when stopping an
5145 * insertion (or moving the cursor), but it's required when appending
5146 * a line and having it end in a space. But only do it when something
5147 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005148 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005150 pos_T tpos = curwin->w_cursor;
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 /* When the cursor is at the end of the line after a space the
5153 * formatting will move it to the following word. Avoid that by
5154 * moving the cursor onto the space. */
5155 cc = 'x';
5156 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5157 {
5158 dec_cursor();
5159 cc = gchar_cursor();
5160 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005161 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163
5164 auto_format(TRUE, FALSE);
5165
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005166 if (vim_iswhite(cc))
5167 {
5168 if (gchar_cursor() != NUL)
5169 inc_cursor();
5170#ifdef FEAT_VIRTUALEDIT
5171 /* If the cursor is still at the same character, also keep
5172 * the "coladd". */
5173 if (gchar_cursor() == NUL
5174 && curwin->w_cursor.lnum == tpos.lnum
5175 && curwin->w_cursor.col == tpos.col)
5176 curwin->w_cursor.coladd = tpos.coladd;
5177#endif
5178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
5181 /* If a space was inserted for auto-formatting, remove it now. */
5182 check_auto_format(TRUE);
5183
5184 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005185 * of the line, and put the cursor back.
5186 * Do this when ESC was used or moving the cursor up/down. */
5187 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5188 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005190 pos_T tpos = curwin->w_cursor;
5191
5192 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5194 --curwin->w_cursor.col;
5195 while (cc = gchar_cursor(), vim_iswhite(cc))
5196 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005197 if (curwin->w_cursor.lnum != tpos.lnum)
5198 curwin->w_cursor = tpos;
5199 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5201
5202#ifdef FEAT_VISUAL
5203 /* <C-S-Right> may have started Visual mode, adjust the position for
5204 * deleted characters. */
5205 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5206 {
5207 cc = STRLEN(ml_get_curline());
5208 if (VIsual.col > (colnr_T)cc)
5209 {
5210 VIsual.col = cc;
5211# ifdef FEAT_VIRTUALEDIT
5212 VIsual.coladd = 0;
5213# endif
5214 }
5215 }
5216#endif
5217 }
5218 }
5219 did_ai = FALSE;
5220#ifdef FEAT_SMARTINDENT
5221 did_si = FALSE;
5222 can_si = FALSE;
5223 can_si_back = FALSE;
5224#endif
5225
5226 /* set '[ and '] to the inserted text */
5227 curbuf->b_op_start = Insstart;
5228 curbuf->b_op_end = *end_insert_pos;
5229}
5230
5231/*
5232 * Set the last inserted text to a single character.
5233 * Used for the replace command.
5234 */
5235 void
5236set_last_insert(c)
5237 int c;
5238{
5239 char_u *s;
5240
5241 vim_free(last_insert);
5242#ifdef FEAT_MBYTE
5243 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5244#else
5245 last_insert = alloc(6);
5246#endif
5247 if (last_insert != NULL)
5248 {
5249 s = last_insert;
5250 /* Use the CTRL-V only when entering a special char */
5251 if (c < ' ' || c == DEL)
5252 *s++ = Ctrl_V;
5253 s = add_char2buf(c, s);
5254 *s++ = ESC;
5255 *s++ = NUL;
5256 last_insert_skip = 0;
5257 }
5258}
5259
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005260#if defined(EXITFREE) || defined(PROTO)
5261 void
5262free_last_insert()
5263{
5264 vim_free(last_insert);
5265 last_insert = NULL;
5266}
5267#endif
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
5270 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5271 * and CSI. Handle multi-byte characters.
5272 * Returns a pointer to after the added bytes.
5273 */
5274 char_u *
5275add_char2buf(c, s)
5276 int c;
5277 char_u *s;
5278{
5279#ifdef FEAT_MBYTE
5280 char_u temp[MB_MAXBYTES];
5281 int i;
5282 int len;
5283
5284 len = (*mb_char2bytes)(c, temp);
5285 for (i = 0; i < len; ++i)
5286 {
5287 c = temp[i];
5288#endif
5289 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5290 if (c == K_SPECIAL)
5291 {
5292 *s++ = K_SPECIAL;
5293 *s++ = KS_SPECIAL;
5294 *s++ = KE_FILLER;
5295 }
5296#ifdef FEAT_GUI
5297 else if (c == CSI)
5298 {
5299 *s++ = CSI;
5300 *s++ = KS_EXTRA;
5301 *s++ = (int)KE_CSI;
5302 }
5303#endif
5304 else
5305 *s++ = c;
5306#ifdef FEAT_MBYTE
5307 }
5308#endif
5309 return s;
5310}
5311
5312/*
5313 * move cursor to start of line
5314 * if flags & BL_WHITE move to first non-white
5315 * if flags & BL_SOL move to first non-white if startofline is set,
5316 * otherwise keep "curswant" column
5317 * if flags & BL_FIX don't leave the cursor on a NUL.
5318 */
5319 void
5320beginline(flags)
5321 int flags;
5322{
5323 if ((flags & BL_SOL) && !p_sol)
5324 coladvance(curwin->w_curswant);
5325 else
5326 {
5327 curwin->w_cursor.col = 0;
5328#ifdef FEAT_VIRTUALEDIT
5329 curwin->w_cursor.coladd = 0;
5330#endif
5331
5332 if (flags & (BL_WHITE | BL_SOL))
5333 {
5334 char_u *ptr;
5335
5336 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5337 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5338 ++curwin->w_cursor.col;
5339 }
5340 curwin->w_set_curswant = TRUE;
5341 }
5342}
5343
5344/*
5345 * oneright oneleft cursor_down cursor_up
5346 *
5347 * Move one char {right,left,down,up}.
5348 * Doesn't move onto the NUL past the end of the line.
5349 * Return OK when successful, FAIL when we hit a line of file boundary.
5350 */
5351
5352 int
5353oneright()
5354{
5355 char_u *ptr;
5356#ifdef FEAT_MBYTE
5357 int l;
5358#endif
5359
5360#ifdef FEAT_VIRTUALEDIT
5361 if (virtual_active())
5362 {
5363 pos_T prevpos = curwin->w_cursor;
5364
5365 /* Adjust for multi-wide char (excluding TAB) */
5366 ptr = ml_get_cursor();
5367 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5368#ifdef FEAT_MBYTE
5369 (*mb_ptr2char)(ptr)
5370#else
5371 *ptr
5372#endif
5373 ))
5374 ? ptr2cells(ptr) : 1));
5375 curwin->w_set_curswant = TRUE;
5376 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5377 return (prevpos.col != curwin->w_cursor.col
5378 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5379 }
5380#endif
5381
5382 ptr = ml_get_cursor();
5383#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005384 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
5386 /* The character under the cursor is a multi-byte character, move
5387 * several bytes right, but don't end up on the NUL. */
5388 if (ptr[l] == NUL)
5389 return FAIL;
5390 curwin->w_cursor.col += l;
5391 }
5392 else
5393#endif
5394 {
5395 if (*ptr++ == NUL || *ptr == NUL)
5396 return FAIL;
5397 ++curwin->w_cursor.col;
5398 }
5399
5400 curwin->w_set_curswant = TRUE;
5401 return OK;
5402}
5403
5404 int
5405oneleft()
5406{
5407#ifdef FEAT_VIRTUALEDIT
5408 if (virtual_active())
5409 {
5410 int width;
5411 int v = getviscol();
5412
5413 if (v == 0)
5414 return FAIL;
5415
5416# ifdef FEAT_LINEBREAK
5417 /* We might get stuck on 'showbreak', skip over it. */
5418 width = 1;
5419 for (;;)
5420 {
5421 coladvance(v - width);
5422 /* getviscol() is slow, skip it when 'showbreak' is empty and
5423 * there are no multi-byte characters */
5424 if ((*p_sbr == NUL
5425# ifdef FEAT_MBYTE
5426 && !has_mbyte
5427# endif
5428 ) || getviscol() < v)
5429 break;
5430 ++width;
5431 }
5432# else
5433 coladvance(v - 1);
5434# endif
5435
5436 if (curwin->w_cursor.coladd == 1)
5437 {
5438 char_u *ptr;
5439
5440 /* Adjust for multi-wide char (not a TAB) */
5441 ptr = ml_get_cursor();
5442 if (*ptr != TAB && vim_isprintc(
5443# ifdef FEAT_MBYTE
5444 (*mb_ptr2char)(ptr)
5445# else
5446 *ptr
5447# endif
5448 ) && ptr2cells(ptr) > 1)
5449 curwin->w_cursor.coladd = 0;
5450 }
5451
5452 curwin->w_set_curswant = TRUE;
5453 return OK;
5454 }
5455#endif
5456
5457 if (curwin->w_cursor.col == 0)
5458 return FAIL;
5459
5460 curwin->w_set_curswant = TRUE;
5461 --curwin->w_cursor.col;
5462
5463#ifdef FEAT_MBYTE
5464 /* if the character on the left of the current cursor is a multi-byte
5465 * character, move to its first byte */
5466 if (has_mbyte)
5467 mb_adjust_cursor();
5468#endif
5469 return OK;
5470}
5471
5472 int
5473cursor_up(n, upd_topline)
5474 long n;
5475 int upd_topline; /* When TRUE: update topline */
5476{
5477 linenr_T lnum;
5478
5479 if (n > 0)
5480 {
5481 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005482 /* This fails if the cursor is already in the first line or the count
5483 * is larger than the line number and '-' is in 'cpoptions' */
5484 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 return FAIL;
5486 if (n >= lnum)
5487 lnum = 1;
5488 else
5489#ifdef FEAT_FOLDING
5490 if (hasAnyFolding(curwin))
5491 {
5492 /*
5493 * Count each sequence of folded lines as one logical line.
5494 */
5495 /* go to the the start of the current fold */
5496 (void)hasFolding(lnum, &lnum, NULL);
5497
5498 while (n--)
5499 {
5500 /* move up one line */
5501 --lnum;
5502 if (lnum <= 1)
5503 break;
5504 /* If we entered a fold, move to the beginning, unless in
5505 * Insert mode or when 'foldopen' contains "all": it will open
5506 * in a moment. */
5507 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5508 (void)hasFolding(lnum, &lnum, NULL);
5509 }
5510 if (lnum < 1)
5511 lnum = 1;
5512 }
5513 else
5514#endif
5515 lnum -= n;
5516 curwin->w_cursor.lnum = lnum;
5517 }
5518
5519 /* try to advance to the column we want to be at */
5520 coladvance(curwin->w_curswant);
5521
5522 if (upd_topline)
5523 update_topline(); /* make sure curwin->w_topline is valid */
5524
5525 return OK;
5526}
5527
5528/*
5529 * Cursor down a number of logical lines.
5530 */
5531 int
5532cursor_down(n, upd_topline)
5533 long n;
5534 int upd_topline; /* When TRUE: update topline */
5535{
5536 linenr_T lnum;
5537
5538 if (n > 0)
5539 {
5540 lnum = curwin->w_cursor.lnum;
5541#ifdef FEAT_FOLDING
5542 /* Move to last line of fold, will fail if it's the end-of-file. */
5543 (void)hasFolding(lnum, NULL, &lnum);
5544#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005545 /* This fails if the cursor is already in the last line or would move
5546 * beyound the last line and '-' is in 'cpoptions' */
5547 if (lnum >= curbuf->b_ml.ml_line_count
5548 || (lnum + n > curbuf->b_ml.ml_line_count
5549 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return FAIL;
5551 if (lnum + n >= curbuf->b_ml.ml_line_count)
5552 lnum = curbuf->b_ml.ml_line_count;
5553 else
5554#ifdef FEAT_FOLDING
5555 if (hasAnyFolding(curwin))
5556 {
5557 linenr_T last;
5558
5559 /* count each sequence of folded lines as one logical line */
5560 while (n--)
5561 {
5562 if (hasFolding(lnum, NULL, &last))
5563 lnum = last + 1;
5564 else
5565 ++lnum;
5566 if (lnum >= curbuf->b_ml.ml_line_count)
5567 break;
5568 }
5569 if (lnum > curbuf->b_ml.ml_line_count)
5570 lnum = curbuf->b_ml.ml_line_count;
5571 }
5572 else
5573#endif
5574 lnum += n;
5575 curwin->w_cursor.lnum = lnum;
5576 }
5577
5578 /* try to advance to the column we want to be at */
5579 coladvance(curwin->w_curswant);
5580
5581 if (upd_topline)
5582 update_topline(); /* make sure curwin->w_topline is valid */
5583
5584 return OK;
5585}
5586
5587/*
5588 * Stuff the last inserted text in the read buffer.
5589 * Last_insert actually is a copy of the redo buffer, so we
5590 * first have to remove the command.
5591 */
5592 int
5593stuff_inserted(c, count, no_esc)
5594 int c; /* Command character to be inserted */
5595 long count; /* Repeat this many times */
5596 int no_esc; /* Don't add an ESC at the end */
5597{
5598 char_u *esc_ptr;
5599 char_u *ptr;
5600 char_u *last_ptr;
5601 char_u last = NUL;
5602
5603 ptr = get_last_insert();
5604 if (ptr == NULL)
5605 {
5606 EMSG(_(e_noinstext));
5607 return FAIL;
5608 }
5609
5610 /* may want to stuff the command character, to start Insert mode */
5611 if (c != NUL)
5612 stuffcharReadbuff(c);
5613 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5614 *esc_ptr = NUL; /* remove the ESC */
5615
5616 /* when the last char is either "0" or "^" it will be quoted if no ESC
5617 * comes after it OR if it will inserted more than once and "ptr"
5618 * starts with ^D. -- Acevedo
5619 */
5620 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5621 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5622 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5623 {
5624 last = *last_ptr;
5625 *last_ptr = NUL;
5626 }
5627
5628 do
5629 {
5630 stuffReadbuff(ptr);
5631 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5632 if (last)
5633 stuffReadbuff((char_u *)(last == '0'
5634 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5635 : IF_EB("\026^", CTRL_V_STR "^")));
5636 }
5637 while (--count > 0);
5638
5639 if (last)
5640 *last_ptr = last;
5641
5642 if (esc_ptr != NULL)
5643 *esc_ptr = ESC; /* put the ESC back */
5644
5645 /* may want to stuff a trailing ESC, to get out of Insert mode */
5646 if (!no_esc)
5647 stuffcharReadbuff(ESC);
5648
5649 return OK;
5650}
5651
5652 char_u *
5653get_last_insert()
5654{
5655 if (last_insert == NULL)
5656 return NULL;
5657 return last_insert + last_insert_skip;
5658}
5659
5660/*
5661 * Get last inserted string, and remove trailing <Esc>.
5662 * Returns pointer to allocated memory (must be freed) or NULL.
5663 */
5664 char_u *
5665get_last_insert_save()
5666{
5667 char_u *s;
5668 int len;
5669
5670 if (last_insert == NULL)
5671 return NULL;
5672 s = vim_strsave(last_insert + last_insert_skip);
5673 if (s != NULL)
5674 {
5675 len = (int)STRLEN(s);
5676 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5677 s[len - 1] = NUL;
5678 }
5679 return s;
5680}
5681
5682/*
5683 * Check the word in front of the cursor for an abbreviation.
5684 * Called when the non-id character "c" has been entered.
5685 * When an abbreviation is recognized it is removed from the text and
5686 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5687 */
5688 static int
5689echeck_abbr(c)
5690 int c;
5691{
5692 /* Don't check for abbreviation in paste mode, when disabled and just
5693 * after moving around with cursor keys. */
5694 if (p_paste || no_abbr || arrow_used)
5695 return FALSE;
5696
5697 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5698 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5699}
5700
5701/*
5702 * replace-stack functions
5703 *
5704 * When replacing characters, the replaced characters are remembered for each
5705 * new character. This is used to re-insert the old text when backspacing.
5706 *
5707 * There is a NUL headed list of characters for each character that is
5708 * currently in the file after the insertion point. When BS is used, one NUL
5709 * headed list is put back for the deleted character.
5710 *
5711 * For a newline, there are two NUL headed lists. One contains the characters
5712 * that the NL replaced. The extra one stores the characters after the cursor
5713 * that were deleted (always white space).
5714 *
5715 * Replace_offset is normally 0, in which case replace_push will add a new
5716 * character at the end of the stack. If replace_offset is not 0, that many
5717 * characters will be left on the stack above the newly inserted character.
5718 */
5719
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005720static char_u *replace_stack = NULL;
5721static long replace_stack_nr = 0; /* next entry in replace stack */
5722static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724 void
5725replace_push(c)
5726 int c; /* character that is replaced (NUL is none) */
5727{
5728 char_u *p;
5729
5730 if (replace_stack_nr < replace_offset) /* nothing to do */
5731 return;
5732 if (replace_stack_len <= replace_stack_nr)
5733 {
5734 replace_stack_len += 50;
5735 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5736 if (p == NULL) /* out of memory */
5737 {
5738 replace_stack_len -= 50;
5739 return;
5740 }
5741 if (replace_stack != NULL)
5742 {
5743 mch_memmove(p, replace_stack,
5744 (size_t)(replace_stack_nr * sizeof(char_u)));
5745 vim_free(replace_stack);
5746 }
5747 replace_stack = p;
5748 }
5749 p = replace_stack + replace_stack_nr - replace_offset;
5750 if (replace_offset)
5751 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5752 *p = c;
5753 ++replace_stack_nr;
5754}
5755
5756/*
5757 * call replace_push(c) with replace_offset set to the first NUL.
5758 */
5759 static void
5760replace_push_off(c)
5761 int c;
5762{
5763 char_u *p;
5764
5765 p = replace_stack + replace_stack_nr;
5766 for (replace_offset = 1; replace_offset < replace_stack_nr;
5767 ++replace_offset)
5768 if (*--p == NUL)
5769 break;
5770 replace_push(c);
5771 replace_offset = 0;
5772}
5773
5774/*
5775 * Pop one item from the replace stack.
5776 * return -1 if stack empty
5777 * return replaced character or NUL otherwise
5778 */
5779 static int
5780replace_pop()
5781{
5782 if (replace_stack_nr == 0)
5783 return -1;
5784 return (int)replace_stack[--replace_stack_nr];
5785}
5786
5787/*
5788 * Join the top two items on the replace stack. This removes to "off"'th NUL
5789 * encountered.
5790 */
5791 static void
5792replace_join(off)
5793 int off; /* offset for which NUL to remove */
5794{
5795 int i;
5796
5797 for (i = replace_stack_nr; --i >= 0; )
5798 if (replace_stack[i] == NUL && off-- <= 0)
5799 {
5800 --replace_stack_nr;
5801 mch_memmove(replace_stack + i, replace_stack + i + 1,
5802 (size_t)(replace_stack_nr - i));
5803 return;
5804 }
5805}
5806
5807/*
5808 * Pop bytes from the replace stack until a NUL is found, and insert them
5809 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5810 */
5811 static void
5812replace_pop_ins()
5813{
5814 int cc;
5815 int oldState = State;
5816
5817 State = NORMAL; /* don't want REPLACE here */
5818 while ((cc = replace_pop()) > 0)
5819 {
5820#ifdef FEAT_MBYTE
5821 mb_replace_pop_ins(cc);
5822#else
5823 ins_char(cc);
5824#endif
5825 dec_cursor();
5826 }
5827 State = oldState;
5828}
5829
5830#ifdef FEAT_MBYTE
5831/*
5832 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5833 * indicates a multi-byte char, pop the other bytes too.
5834 */
5835 static void
5836mb_replace_pop_ins(cc)
5837 int cc;
5838{
5839 int n;
5840 char_u buf[MB_MAXBYTES];
5841 int i;
5842 int c;
5843
5844 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5845 {
5846 buf[0] = cc;
5847 for (i = 1; i < n; ++i)
5848 buf[i] = replace_pop();
5849 ins_bytes_len(buf, n);
5850 }
5851 else
5852 ins_char(cc);
5853
5854 if (enc_utf8)
5855 /* Handle composing chars. */
5856 for (;;)
5857 {
5858 c = replace_pop();
5859 if (c == -1) /* stack empty */
5860 break;
5861 if ((n = MB_BYTE2LEN(c)) == 1)
5862 {
5863 /* Not a multi-byte char, put it back. */
5864 replace_push(c);
5865 break;
5866 }
5867 else
5868 {
5869 buf[0] = c;
5870 for (i = 1; i < n; ++i)
5871 buf[i] = replace_pop();
5872 if (utf_iscomposing(utf_ptr2char(buf)))
5873 ins_bytes_len(buf, n);
5874 else
5875 {
5876 /* Not a composing char, put it back. */
5877 for (i = n - 1; i >= 0; --i)
5878 replace_push(buf[i]);
5879 break;
5880 }
5881 }
5882 }
5883}
5884#endif
5885
5886/*
5887 * make the replace stack empty
5888 * (called when exiting replace mode)
5889 */
5890 static void
5891replace_flush()
5892{
5893 vim_free(replace_stack);
5894 replace_stack = NULL;
5895 replace_stack_len = 0;
5896 replace_stack_nr = 0;
5897}
5898
5899/*
5900 * Handle doing a BS for one character.
5901 * cc < 0: replace stack empty, just move cursor
5902 * cc == 0: character was inserted, delete it
5903 * cc > 0: character was replaced, put cc (first byte of original char) back
5904 * and check for more characters to be put back
5905 */
5906 static void
5907replace_do_bs()
5908{
5909 int cc;
5910#ifdef FEAT_VREPLACE
5911 int orig_len = 0;
5912 int ins_len;
5913 int orig_vcols = 0;
5914 colnr_T start_vcol;
5915 char_u *p;
5916 int i;
5917 int vcol;
5918#endif
5919
5920 cc = replace_pop();
5921 if (cc > 0)
5922 {
5923#ifdef FEAT_VREPLACE
5924 if (State & VREPLACE_FLAG)
5925 {
5926 /* Get the number of screen cells used by the character we are
5927 * going to delete. */
5928 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5929 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5930 }
5931#endif
5932#ifdef FEAT_MBYTE
5933 if (has_mbyte)
5934 {
5935 del_char(FALSE);
5936# ifdef FEAT_VREPLACE
5937 if (State & VREPLACE_FLAG)
5938 orig_len = STRLEN(ml_get_cursor());
5939# endif
5940 replace_push(cc);
5941 }
5942 else
5943#endif
5944 {
5945 pchar_cursor(cc);
5946#ifdef FEAT_VREPLACE
5947 if (State & VREPLACE_FLAG)
5948 orig_len = STRLEN(ml_get_cursor()) - 1;
5949#endif
5950 }
5951 replace_pop_ins();
5952
5953#ifdef FEAT_VREPLACE
5954 if (State & VREPLACE_FLAG)
5955 {
5956 /* Get the number of screen cells used by the inserted characters */
5957 p = ml_get_cursor();
5958 ins_len = STRLEN(p) - orig_len;
5959 vcol = start_vcol;
5960 for (i = 0; i < ins_len; ++i)
5961 {
5962 vcol += chartabsize(p + i, vcol);
5963#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005964 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965#endif
5966 }
5967 vcol -= start_vcol;
5968
5969 /* Delete spaces that were inserted after the cursor to keep the
5970 * text aligned. */
5971 curwin->w_cursor.col += ins_len;
5972 while (vcol > orig_vcols && gchar_cursor() == ' ')
5973 {
5974 del_char(FALSE);
5975 ++orig_vcols;
5976 }
5977 curwin->w_cursor.col -= ins_len;
5978 }
5979#endif
5980
5981 /* mark the buffer as changed and prepare for displaying */
5982 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5983 }
5984 else if (cc == 0)
5985 (void)del_char(FALSE);
5986}
5987
5988#ifdef FEAT_CINDENT
5989/*
5990 * Return TRUE if C-indenting is on.
5991 */
5992 static int
5993cindent_on()
5994{
5995 return (!p_paste && (curbuf->b_p_cin
5996# ifdef FEAT_EVAL
5997 || *curbuf->b_p_inde != NUL
5998# endif
5999 ));
6000}
6001#endif
6002
6003#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6004/*
6005 * Re-indent the current line, based on the current contents of it and the
6006 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6007 * confused what all the part that handles Control-T is doing that I'm not.
6008 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6009 */
6010
6011 void
6012fixthisline(get_the_indent)
6013 int (*get_the_indent) __ARGS((void));
6014{
6015 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6016 if (linewhite(curwin->w_cursor.lnum))
6017 did_ai = TRUE; /* delete the indent if the line stays empty */
6018}
6019
6020 void
6021fix_indent()
6022{
6023 if (p_paste)
6024 return;
6025# ifdef FEAT_LISP
6026 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6027 fixthisline(get_lisp_indent);
6028# endif
6029# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6030 else
6031# endif
6032# ifdef FEAT_CINDENT
6033 if (cindent_on())
6034 do_c_expr_indent();
6035# endif
6036}
6037
6038#endif
6039
6040#ifdef FEAT_CINDENT
6041/*
6042 * return TRUE if 'cinkeys' contains the key "keytyped",
6043 * when == '*': Only if key is preceded with '*' (indent before insert)
6044 * when == '!': Only if key is prededed with '!' (don't insert)
6045 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6046 *
6047 * "keytyped" can have a few special values:
6048 * KEY_OPEN_FORW
6049 * KEY_OPEN_BACK
6050 * KEY_COMPLETE just finished completion.
6051 *
6052 * If line_is_empty is TRUE accept keys with '0' before them.
6053 */
6054 int
6055in_cinkeys(keytyped, when, line_is_empty)
6056 int keytyped;
6057 int when;
6058 int line_is_empty;
6059{
6060 char_u *look;
6061 int try_match;
6062 int try_match_word;
6063 char_u *p;
6064 char_u *line;
6065 int icase;
6066 int i;
6067
6068#ifdef FEAT_EVAL
6069 if (*curbuf->b_p_inde != NUL)
6070 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6071 else
6072#endif
6073 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6074 while (*look)
6075 {
6076 /*
6077 * Find out if we want to try a match with this key, depending on
6078 * 'when' and a '*' or '!' before the key.
6079 */
6080 switch (when)
6081 {
6082 case '*': try_match = (*look == '*'); break;
6083 case '!': try_match = (*look == '!'); break;
6084 default: try_match = (*look != '*'); break;
6085 }
6086 if (*look == '*' || *look == '!')
6087 ++look;
6088
6089 /*
6090 * If there is a '0', only accept a match if the line is empty.
6091 * But may still match when typing last char of a word.
6092 */
6093 if (*look == '0')
6094 {
6095 try_match_word = try_match;
6096 if (!line_is_empty)
6097 try_match = FALSE;
6098 ++look;
6099 }
6100 else
6101 try_match_word = FALSE;
6102
6103 /*
6104 * does it look like a control character?
6105 */
6106 if (*look == '^'
6107#ifdef EBCDIC
6108 && (Ctrl_chr(look[1]) != 0)
6109#else
6110 && look[1] >= '?' && look[1] <= '_'
6111#endif
6112 )
6113 {
6114 if (try_match && keytyped == Ctrl_chr(look[1]))
6115 return TRUE;
6116 look += 2;
6117 }
6118 /*
6119 * 'o' means "o" command, open forward.
6120 * 'O' means "O" command, open backward.
6121 */
6122 else if (*look == 'o')
6123 {
6124 if (try_match && keytyped == KEY_OPEN_FORW)
6125 return TRUE;
6126 ++look;
6127 }
6128 else if (*look == 'O')
6129 {
6130 if (try_match && keytyped == KEY_OPEN_BACK)
6131 return TRUE;
6132 ++look;
6133 }
6134
6135 /*
6136 * 'e' means to check for "else" at start of line and just before the
6137 * cursor.
6138 */
6139 else if (*look == 'e')
6140 {
6141 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6142 {
6143 p = ml_get_curline();
6144 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6145 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6146 return TRUE;
6147 }
6148 ++look;
6149 }
6150
6151 /*
6152 * ':' only causes an indent if it is at the end of a label or case
6153 * statement, or when it was before typing the ':' (to fix
6154 * class::method for C++).
6155 */
6156 else if (*look == ':')
6157 {
6158 if (try_match && keytyped == ':')
6159 {
6160 p = ml_get_curline();
6161 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6162 return TRUE;
6163 if (curwin->w_cursor.col > 2
6164 && p[curwin->w_cursor.col - 1] == ':'
6165 && p[curwin->w_cursor.col - 2] == ':')
6166 {
6167 p[curwin->w_cursor.col - 1] = ' ';
6168 i = (cin_iscase(p) || cin_isscopedecl(p)
6169 || cin_islabel(30));
6170 p = ml_get_curline();
6171 p[curwin->w_cursor.col - 1] = ':';
6172 if (i)
6173 return TRUE;
6174 }
6175 }
6176 ++look;
6177 }
6178
6179
6180 /*
6181 * Is it a key in <>, maybe?
6182 */
6183 else if (*look == '<')
6184 {
6185 if (try_match)
6186 {
6187 /*
6188 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6189 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6190 * >, *, : and ! keys if they really really want to.
6191 */
6192 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6193 && keytyped == look[1])
6194 return TRUE;
6195
6196 if (keytyped == get_special_key_code(look + 1))
6197 return TRUE;
6198 }
6199 while (*look && *look != '>')
6200 look++;
6201 while (*look == '>')
6202 look++;
6203 }
6204
6205 /*
6206 * Is it a word: "=word"?
6207 */
6208 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6209 {
6210 ++look;
6211 if (*look == '~')
6212 {
6213 icase = TRUE;
6214 ++look;
6215 }
6216 else
6217 icase = FALSE;
6218 p = vim_strchr(look, ',');
6219 if (p == NULL)
6220 p = look + STRLEN(look);
6221 if ((try_match || try_match_word)
6222 && curwin->w_cursor.col >= (colnr_T)(p - look))
6223 {
6224 int match = FALSE;
6225
6226#ifdef FEAT_INS_EXPAND
6227 if (keytyped == KEY_COMPLETE)
6228 {
6229 char_u *s;
6230
6231 /* Just completed a word, check if it starts with "look".
6232 * search back for the start of a word. */
6233 line = ml_get_curline();
6234# ifdef FEAT_MBYTE
6235 if (has_mbyte)
6236 {
6237 char_u *n;
6238
6239 for (s = line + curwin->w_cursor.col; s > line; s = n)
6240 {
6241 n = mb_prevptr(line, s);
6242 if (!vim_iswordp(n))
6243 break;
6244 }
6245 }
6246 else
6247# endif
6248 for (s = line + curwin->w_cursor.col; s > line; --s)
6249 if (!vim_iswordc(s[-1]))
6250 break;
6251 if (s + (p - look) <= line + curwin->w_cursor.col
6252 && (icase
6253 ? MB_STRNICMP(s, look, p - look)
6254 : STRNCMP(s, look, p - look)) == 0)
6255 match = TRUE;
6256 }
6257 else
6258#endif
6259 /* TODO: multi-byte */
6260 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6261 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6262 {
6263 line = ml_get_cursor();
6264 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6265 || !vim_iswordc(line[-(p - look) - 1]))
6266 && (icase
6267 ? MB_STRNICMP(line - (p - look), look, p - look)
6268 : STRNCMP(line - (p - look), look, p - look))
6269 == 0)
6270 match = TRUE;
6271 }
6272 if (match && try_match_word && !try_match)
6273 {
6274 /* "0=word": Check if there are only blanks before the
6275 * word. */
6276 line = ml_get_curline();
6277 if ((int)(skipwhite(line) - line) !=
6278 (int)(curwin->w_cursor.col - (p - look)))
6279 match = FALSE;
6280 }
6281 if (match)
6282 return TRUE;
6283 }
6284 look = p;
6285 }
6286
6287 /*
6288 * ok, it's a boring generic character.
6289 */
6290 else
6291 {
6292 if (try_match && *look == keytyped)
6293 return TRUE;
6294 ++look;
6295 }
6296
6297 /*
6298 * Skip over ", ".
6299 */
6300 look = skip_to_option_part(look);
6301 }
6302 return FALSE;
6303}
6304#endif /* FEAT_CINDENT */
6305
6306#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6307/*
6308 * Map Hebrew keyboard when in hkmap mode.
6309 */
6310 int
6311hkmap(c)
6312 int c;
6313{
6314 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6315 {
6316 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6317 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6318 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6319 static char_u map[26] =
6320 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6321 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6322 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6323 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6324 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6325 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6326 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6327 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6328 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6329
6330 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6331 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6332 /* '-1'='sofit' */
6333 else if (c == 'x')
6334 return 'X';
6335 else if (c == 'q')
6336 return '\''; /* {geresh}={'} */
6337 else if (c == 246)
6338 return ' '; /* \"o --> ' ' for a german keyboard */
6339 else if (c == 228)
6340 return ' '; /* \"a --> ' ' -- / -- */
6341 else if (c == 252)
6342 return ' '; /* \"u --> ' ' -- / -- */
6343#ifdef EBCDIC
6344 else if (islower(c))
6345#else
6346 /* NOTE: islower() does not do the right thing for us on Linux so we
6347 * do this the same was as 5.7 and previous, so it works correctly on
6348 * all systems. Specifically, the e.g. Delete and Arrow keys are
6349 * munged and won't work if e.g. searching for Hebrew text.
6350 */
6351 else if (c >= 'a' && c <= 'z')
6352#endif
6353 return (int)(map[CharOrdLow(c)] + p_aleph);
6354 else
6355 return c;
6356 }
6357 else
6358 {
6359 switch (c)
6360 {
6361 case '`': return ';';
6362 case '/': return '.';
6363 case '\'': return ',';
6364 case 'q': return '/';
6365 case 'w': return '\'';
6366
6367 /* Hebrew letters - set offset from 'a' */
6368 case ',': c = '{'; break;
6369 case '.': c = 'v'; break;
6370 case ';': c = 't'; break;
6371 default: {
6372 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6373
6374#ifdef EBCDIC
6375 /* see note about islower() above */
6376 if (!islower(c))
6377#else
6378 if (c < 'a' || c > 'z')
6379#endif
6380 return c;
6381 c = str[CharOrdLow(c)];
6382 break;
6383 }
6384 }
6385
6386 return (int)(CharOrdLow(c) + p_aleph);
6387 }
6388}
6389#endif
6390
6391 static void
6392ins_reg()
6393{
6394 int need_redraw = FALSE;
6395 int regname;
6396 int literally = 0;
6397
6398 /*
6399 * If we are going to wait for a character, show a '"'.
6400 */
6401 pc_status = PC_STATUS_UNSET;
6402 if (redrawing() && !char_avail())
6403 {
6404 /* may need to redraw when no more chars available now */
6405 ins_redraw();
6406
6407 edit_putchar('"', TRUE);
6408#ifdef FEAT_CMDL_INFO
6409 add_to_showcmd_c(Ctrl_R);
6410#endif
6411 }
6412
6413#ifdef USE_ON_FLY_SCROLL
6414 dont_scroll = TRUE; /* disallow scrolling here */
6415#endif
6416
6417 /*
6418 * Don't map the register name. This also prevents the mode message to be
6419 * deleted when ESC is hit.
6420 */
6421 ++no_mapping;
6422 regname = safe_vgetc();
6423#ifdef FEAT_LANGMAP
6424 LANGMAP_ADJUST(regname, TRUE);
6425#endif
6426 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6427 {
6428 /* Get a third key for literal register insertion */
6429 literally = regname;
6430#ifdef FEAT_CMDL_INFO
6431 add_to_showcmd_c(literally);
6432#endif
6433 regname = safe_vgetc();
6434#ifdef FEAT_LANGMAP
6435 LANGMAP_ADJUST(regname, TRUE);
6436#endif
6437 }
6438 --no_mapping;
6439
6440#ifdef FEAT_EVAL
6441 /*
6442 * Don't call u_sync() while getting the expression,
6443 * evaluating it or giving an error message for it!
6444 */
6445 ++no_u_sync;
6446 if (regname == '=')
6447 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006448# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006452# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 /* Restore the Input Method. */
6454 if (im_on)
6455 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006458 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6459 {
6460 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 else
6464 {
6465#endif
6466 if (literally == Ctrl_O || literally == Ctrl_P)
6467 {
6468 /* Append the command to the redo buffer. */
6469 AppendCharToRedobuff(Ctrl_R);
6470 AppendCharToRedobuff(literally);
6471 AppendCharToRedobuff(regname);
6472
6473 do_put(regname, BACKWARD, 1L,
6474 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6475 }
6476 else if (insert_reg(regname, literally) == FAIL)
6477 {
6478 vim_beep();
6479 need_redraw = TRUE; /* remove the '"' */
6480 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006481 else if (stop_insert_mode)
6482 /* When the '=' register was used and a function was invoked that
6483 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6484 * insert anything, need to remove the '"' */
6485 need_redraw = TRUE;
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#ifdef FEAT_EVAL
6488 }
6489 --no_u_sync;
6490#endif
6491#ifdef FEAT_CMDL_INFO
6492 clear_showcmd();
6493#endif
6494
6495 /* If the inserted register is empty, we need to remove the '"' */
6496 if (need_redraw || stuff_empty())
6497 edit_unputchar();
6498}
6499
6500/*
6501 * CTRL-G commands in Insert mode.
6502 */
6503 static void
6504ins_ctrl_g()
6505{
6506 int c;
6507
6508#ifdef FEAT_INS_EXPAND
6509 /* Right after CTRL-X the cursor will be after the ruler. */
6510 setcursor();
6511#endif
6512
6513 /*
6514 * Don't map the second key. This also prevents the mode message to be
6515 * deleted when ESC is hit.
6516 */
6517 ++no_mapping;
6518 c = safe_vgetc();
6519 --no_mapping;
6520 switch (c)
6521 {
6522 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6523 case K_UP:
6524 case Ctrl_K:
6525 case 'k': ins_up(TRUE);
6526 break;
6527
6528 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6529 case K_DOWN:
6530 case Ctrl_J:
6531 case 'j': ins_down(TRUE);
6532 break;
6533
6534 /* CTRL-G u: start new undoable edit */
6535 case 'u': u_sync();
6536 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006537
6538 /* Need to reset Insstart, esp. because a BS that joins
6539 * aline to the previous one must save for undo. */
6540 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 break;
6542
6543 /* Unknown CTRL-G command, reserved for future expansion. */
6544 default: vim_beep();
6545 }
6546}
6547
6548/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006549 * CTRL-^ in Insert mode.
6550 */
6551 static void
6552ins_ctrl_hat()
6553{
6554 if (map_to_exists_mode((char_u *)"", LANGMAP))
6555 {
6556 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6557 if (State & LANGMAP)
6558 {
6559 curbuf->b_p_iminsert = B_IMODE_NONE;
6560 State &= ~LANGMAP;
6561 }
6562 else
6563 {
6564 curbuf->b_p_iminsert = B_IMODE_LMAP;
6565 State |= LANGMAP;
6566#ifdef USE_IM_CONTROL
6567 im_set_active(FALSE);
6568#endif
6569 }
6570 }
6571#ifdef USE_IM_CONTROL
6572 else
6573 {
6574 /* There are no ":lmap" mappings, toggle IM */
6575 if (im_get_status())
6576 {
6577 curbuf->b_p_iminsert = B_IMODE_NONE;
6578 im_set_active(FALSE);
6579 }
6580 else
6581 {
6582 curbuf->b_p_iminsert = B_IMODE_IM;
6583 State &= ~LANGMAP;
6584 im_set_active(TRUE);
6585 }
6586 }
6587#endif
6588 set_iminsert_global();
6589 showmode();
6590#ifdef FEAT_GUI
6591 /* may show different cursor shape or color */
6592 if (gui.in_use)
6593 gui_update_cursor(TRUE, FALSE);
6594#endif
6595#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6596 /* Show/unshow value of 'keymap' in status lines. */
6597 status_redraw_curbuf();
6598#endif
6599}
6600
6601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * Handle ESC in insert mode.
6603 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6604 * insert.
6605 */
6606 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006607ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 long *count;
6609 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006610 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 int temp;
6613 static int disabled_redraw = FALSE;
6614
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006615#ifdef FEAT_SYN_HL
6616 check_spell_redraw();
6617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618#if defined(FEAT_HANGULIN)
6619# if defined(ESC_CHG_TO_ENG_MODE)
6620 hangul_input_state_set(0);
6621# endif
6622 if (composing_hangul)
6623 {
6624 push_raw_key(composing_hangul_buffer, 2);
6625 composing_hangul = 0;
6626 }
6627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 temp = curwin->w_cursor.col;
6630 if (disabled_redraw)
6631 {
6632 --RedrawingDisabled;
6633 disabled_redraw = FALSE;
6634 }
6635 if (!arrow_used)
6636 {
6637 /*
6638 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006639 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6640 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 */
6642 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006643 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 /*
6646 * Repeating insert may take a long time. Check for
6647 * interrupt now and then.
6648 */
6649 if (*count > 0)
6650 {
6651 line_breakcheck();
6652 if (got_int)
6653 *count = 0;
6654 }
6655
6656 if (--*count > 0) /* repeat what was typed */
6657 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006658 /* Vi repeats the insert without replacing characters. */
6659 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6660 State &= ~REPLACE_FLAG;
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 (void)start_redo_ins();
6663 if (cmdchar == 'r' || cmdchar == 'v')
6664 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6665 ++RedrawingDisabled;
6666 disabled_redraw = TRUE;
6667 return FALSE; /* repeat the insert */
6668 }
6669 stop_insert(&curwin->w_cursor, TRUE);
6670 undisplay_dollar();
6671 }
6672
6673 /* When an autoindent was removed, curswant stays after the
6674 * indent */
6675 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6676 curwin->w_set_curswant = TRUE;
6677
6678 /* Remember the last Insert position in the '^ mark. */
6679 if (!cmdmod.keepjumps)
6680 curbuf->b_last_insert = curwin->w_cursor;
6681
6682 /*
6683 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006684 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006686 if (!nomove
6687 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688#ifdef FEAT_VIRTUALEDIT
6689 || curwin->w_cursor.coladd > 0
6690#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006691 )
6692 && (restart_edit == NUL
6693 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006695 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006697 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698#ifdef FEAT_RIGHTLEFT
6699 && !revins_on
6700#endif
6701 )
6702 {
6703#ifdef FEAT_VIRTUALEDIT
6704 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6705 {
6706 oneleft();
6707 if (restart_edit != NUL)
6708 ++curwin->w_cursor.coladd;
6709 }
6710 else
6711#endif
6712 {
6713 --curwin->w_cursor.col;
6714#ifdef FEAT_MBYTE
6715 /* Correct cursor for multi-byte character. */
6716 if (has_mbyte)
6717 mb_adjust_cursor();
6718#endif
6719 }
6720 }
6721
6722#ifdef USE_IM_CONTROL
6723 /* Disable IM to allow typing English directly for Normal mode commands.
6724 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6725 * well). */
6726 if (!(State & LANGMAP))
6727 im_save_status(&curbuf->b_p_iminsert);
6728 im_set_active(FALSE);
6729#endif
6730
6731 State = NORMAL;
6732 /* need to position cursor again (e.g. when on a TAB ) */
6733 changed_cline_bef_curs();
6734
6735#ifdef FEAT_MOUSE
6736 setmouse();
6737#endif
6738#ifdef CURSOR_SHAPE
6739 ui_cursor_shape(); /* may show different cursor shape */
6740#endif
6741
6742 /*
6743 * When recording or for CTRL-O, need to display the new mode.
6744 * Otherwise remove the mode message.
6745 */
6746 if (Recording || restart_edit != NUL)
6747 showmode();
6748 else if (p_smd)
6749 MSG("");
6750
6751 return TRUE; /* exit Insert mode */
6752}
6753
6754#ifdef FEAT_RIGHTLEFT
6755/*
6756 * Toggle language: hkmap and revins_on.
6757 * Move to end of reverse inserted text.
6758 */
6759 static void
6760ins_ctrl_()
6761{
6762 if (revins_on && revins_chars && revins_scol >= 0)
6763 {
6764 while (gchar_cursor() != NUL && revins_chars--)
6765 ++curwin->w_cursor.col;
6766 }
6767 p_ri = !p_ri;
6768 revins_on = (State == INSERT && p_ri);
6769 if (revins_on)
6770 {
6771 revins_scol = curwin->w_cursor.col;
6772 revins_legal++;
6773 revins_chars = 0;
6774 undisplay_dollar();
6775 }
6776 else
6777 revins_scol = -1;
6778#ifdef FEAT_FKMAP
6779 if (p_altkeymap)
6780 {
6781 /*
6782 * to be consistent also for redo command, using '.'
6783 * set arrow_used to true and stop it - causing to redo
6784 * characters entered in one mode (normal/reverse insert).
6785 */
6786 arrow_used = TRUE;
6787 (void)stop_arrow();
6788 p_fkmap = curwin->w_p_rl ^ p_ri;
6789 if (p_fkmap && p_ri)
6790 State = INSERT;
6791 }
6792 else
6793#endif
6794 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6795 showmode();
6796}
6797#endif
6798
6799#ifdef FEAT_VISUAL
6800/*
6801 * If 'keymodel' contains "startsel", may start selection.
6802 * Returns TRUE when a CTRL-O and other keys stuffed.
6803 */
6804 static int
6805ins_start_select(c)
6806 int c;
6807{
6808 if (km_startsel)
6809 switch (c)
6810 {
6811 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 case K_PAGEUP:
6814 case K_KPAGEUP:
6815 case K_PAGEDOWN:
6816 case K_KPAGEDOWN:
6817# ifdef MACOS
6818 case K_LEFT:
6819 case K_RIGHT:
6820 case K_UP:
6821 case K_DOWN:
6822 case K_END:
6823 case K_HOME:
6824# endif
6825 if (!(mod_mask & MOD_MASK_SHIFT))
6826 break;
6827 /* FALLTHROUGH */
6828 case K_S_LEFT:
6829 case K_S_RIGHT:
6830 case K_S_UP:
6831 case K_S_DOWN:
6832 case K_S_END:
6833 case K_S_HOME:
6834 /* Start selection right away, the cursor can move with
6835 * CTRL-O when beyond the end of the line. */
6836 start_selection();
6837
6838 /* Execute the key in (insert) Select mode. */
6839 stuffcharReadbuff(Ctrl_O);
6840 if (mod_mask)
6841 {
6842 char_u buf[4];
6843
6844 buf[0] = K_SPECIAL;
6845 buf[1] = KS_MODIFIER;
6846 buf[2] = mod_mask;
6847 buf[3] = NUL;
6848 stuffReadbuff(buf);
6849 }
6850 stuffcharReadbuff(c);
6851 return TRUE;
6852 }
6853 return FALSE;
6854}
6855#endif
6856
6857/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006858 * <Insert> key in Insert mode: toggle insert/remplace mode.
6859 */
6860 static void
6861ins_insert(replaceState)
6862 int replaceState;
6863{
6864#ifdef FEAT_FKMAP
6865 if (p_fkmap && p_ri)
6866 {
6867 beep_flush();
6868 EMSG(farsi_text_3); /* encoded in Farsi */
6869 return;
6870 }
6871#endif
6872
6873#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006874# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006875 set_vim_var_string(VV_INSERTMODE,
6876 (char_u *)((State & REPLACE_FLAG) ? "i" :
6877 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006878# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006879 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6880#endif
6881 if (State & REPLACE_FLAG)
6882 State = INSERT | (State & LANGMAP);
6883 else
6884 State = replaceState | (State & LANGMAP);
6885 AppendCharToRedobuff(K_INS);
6886 showmode();
6887#ifdef CURSOR_SHAPE
6888 ui_cursor_shape(); /* may show different cursor shape */
6889#endif
6890}
6891
6892/*
6893 * Pressed CTRL-O in Insert mode.
6894 */
6895 static void
6896ins_ctrl_o()
6897{
6898#ifdef FEAT_VREPLACE
6899 if (State & VREPLACE_FLAG)
6900 restart_edit = 'V';
6901 else
6902#endif
6903 if (State & REPLACE_FLAG)
6904 restart_edit = 'R';
6905 else
6906 restart_edit = 'I';
6907#ifdef FEAT_VIRTUALEDIT
6908 if (virtual_active())
6909 ins_at_eol = FALSE; /* cursor always keeps its column */
6910 else
6911#endif
6912 ins_at_eol = (gchar_cursor() == NUL);
6913}
6914
6915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 * If the cursor is on an indent, ^T/^D insert/delete one
6917 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6918 * Always round the indent to 'shiftwith', this is compatible
6919 * with vi. But vi only supports ^T and ^D after an
6920 * autoindent, we support it everywhere.
6921 */
6922 static void
6923ins_shift(c, lastc)
6924 int c;
6925 int lastc;
6926{
6927 if (stop_arrow() == FAIL)
6928 return;
6929 AppendCharToRedobuff(c);
6930
6931 /*
6932 * 0^D and ^^D: remove all indent.
6933 */
6934 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6935 {
6936 --curwin->w_cursor.col;
6937 (void)del_char(FALSE); /* delete the '^' or '0' */
6938 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6939 if (State & REPLACE_FLAG)
6940 replace_pop_ins();
6941 if (lastc == '^')
6942 old_indent = get_indent(); /* remember curr. indent */
6943 change_indent(INDENT_SET, 0, TRUE, 0);
6944 }
6945 else
6946 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6947
6948 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6949 did_ai = FALSE;
6950#ifdef FEAT_SMARTINDENT
6951 did_si = FALSE;
6952 can_si = FALSE;
6953 can_si_back = FALSE;
6954#endif
6955#ifdef FEAT_CINDENT
6956 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6957#endif
6958}
6959
6960 static void
6961ins_del()
6962{
6963 int temp;
6964
6965 if (stop_arrow() == FAIL)
6966 return;
6967 if (gchar_cursor() == NUL) /* delete newline */
6968 {
6969 temp = curwin->w_cursor.col;
6970 if (!can_bs(BS_EOL) /* only if "eol" included */
6971 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6972 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6973 || do_join(FALSE) == FAIL)
6974 vim_beep();
6975 else
6976 curwin->w_cursor.col = temp;
6977 }
6978 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6979 vim_beep();
6980 did_ai = FALSE;
6981#ifdef FEAT_SMARTINDENT
6982 did_si = FALSE;
6983 can_si = FALSE;
6984 can_si_back = FALSE;
6985#endif
6986 AppendCharToRedobuff(K_DEL);
6987}
6988
6989/*
6990 * Handle Backspace, delete-word and delete-line in Insert mode.
6991 * Return TRUE when backspace was actually used.
6992 */
6993 static int
6994ins_bs(c, mode, inserted_space_p)
6995 int c;
6996 int mode;
6997 int *inserted_space_p;
6998{
6999 linenr_T lnum;
7000 int cc;
7001 int temp = 0; /* init for GCC */
7002 colnr_T mincol;
7003 int did_backspace = FALSE;
7004 int in_indent;
7005 int oldState;
7006#ifdef FEAT_MBYTE
7007 int p1, p2;
7008#endif
7009
7010 /*
7011 * can't delete anything in an empty file
7012 * can't backup past first character in buffer
7013 * can't backup past starting point unless 'backspace' > 1
7014 * can backup to a previous line if 'backspace' == 0
7015 */
7016 if ( bufempty()
7017 || (
7018#ifdef FEAT_RIGHTLEFT
7019 !revins_on &&
7020#endif
7021 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7022 || (!can_bs(BS_START)
7023 && (arrow_used
7024 || (curwin->w_cursor.lnum == Insstart.lnum
7025 && curwin->w_cursor.col <= Insstart.col)))
7026 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7027 && curwin->w_cursor.col <= ai_col)
7028 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7029 {
7030 vim_beep();
7031 return FALSE;
7032 }
7033
7034 if (stop_arrow() == FAIL)
7035 return FALSE;
7036 in_indent = inindent(0);
7037#ifdef FEAT_CINDENT
7038 if (in_indent)
7039 can_cindent = FALSE;
7040#endif
7041#ifdef FEAT_COMMENTS
7042 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7043#endif
7044#ifdef FEAT_RIGHTLEFT
7045 if (revins_on) /* put cursor after last inserted char */
7046 inc_cursor();
7047#endif
7048
7049#ifdef FEAT_VIRTUALEDIT
7050 /* Virtualedit:
7051 * BACKSPACE_CHAR eats a virtual space
7052 * BACKSPACE_WORD eats all coladd
7053 * BACKSPACE_LINE eats all coladd and keeps going
7054 */
7055 if (curwin->w_cursor.coladd > 0)
7056 {
7057 if (mode == BACKSPACE_CHAR)
7058 {
7059 --curwin->w_cursor.coladd;
7060 return TRUE;
7061 }
7062 if (mode == BACKSPACE_WORD)
7063 {
7064 curwin->w_cursor.coladd = 0;
7065 return TRUE;
7066 }
7067 curwin->w_cursor.coladd = 0;
7068 }
7069#endif
7070
7071 /*
7072 * delete newline!
7073 */
7074 if (curwin->w_cursor.col == 0)
7075 {
7076 lnum = Insstart.lnum;
7077 if (curwin->w_cursor.lnum == Insstart.lnum
7078#ifdef FEAT_RIGHTLEFT
7079 || revins_on
7080#endif
7081 )
7082 {
7083 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7084 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7085 return FALSE;
7086 --Insstart.lnum;
7087 Insstart.col = MAXCOL;
7088 }
7089 /*
7090 * In replace mode:
7091 * cc < 0: NL was inserted, delete it
7092 * cc >= 0: NL was replaced, put original characters back
7093 */
7094 cc = -1;
7095 if (State & REPLACE_FLAG)
7096 cc = replace_pop(); /* returns -1 if NL was inserted */
7097 /*
7098 * In replace mode, in the line we started replacing, we only move the
7099 * cursor.
7100 */
7101 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7102 {
7103 dec_cursor();
7104 }
7105 else
7106 {
7107#ifdef FEAT_VREPLACE
7108 if (!(State & VREPLACE_FLAG)
7109 || curwin->w_cursor.lnum > orig_line_count)
7110#endif
7111 {
7112 temp = gchar_cursor(); /* remember current char */
7113 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007114
7115 /* When "aw" is in 'formatoptions' we must delete the space at
7116 * the end of the line, otherwise the line will be broken
7117 * again when auto-formatting. */
7118 if (has_format_option(FO_AUTO)
7119 && has_format_option(FO_WHITE_PAR))
7120 {
7121 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7122 TRUE);
7123 int len;
7124
7125 len = STRLEN(ptr);
7126 if (len > 0 && ptr[len - 1] == ' ')
7127 ptr[len - 1] = NUL;
7128 }
7129
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 (void)do_join(FALSE);
7131 if (temp == NUL && gchar_cursor() != NUL)
7132 inc_cursor();
7133 }
7134#ifdef FEAT_VREPLACE
7135 else
7136 dec_cursor();
7137#endif
7138
7139 /*
7140 * In REPLACE mode we have to put back the text that was replaced
7141 * by the NL. On the replace stack is first a NUL-terminated
7142 * sequence of characters that were deleted and then the
7143 * characters that NL replaced.
7144 */
7145 if (State & REPLACE_FLAG)
7146 {
7147 /*
7148 * Do the next ins_char() in NORMAL state, to
7149 * prevent ins_char() from replacing characters and
7150 * avoiding showmatch().
7151 */
7152 oldState = State;
7153 State = NORMAL;
7154 /*
7155 * restore characters (blanks) deleted after cursor
7156 */
7157 while (cc > 0)
7158 {
7159 temp = curwin->w_cursor.col;
7160#ifdef FEAT_MBYTE
7161 mb_replace_pop_ins(cc);
7162#else
7163 ins_char(cc);
7164#endif
7165 curwin->w_cursor.col = temp;
7166 cc = replace_pop();
7167 }
7168 /* restore the characters that NL replaced */
7169 replace_pop_ins();
7170 State = oldState;
7171 }
7172 }
7173 did_ai = FALSE;
7174 }
7175 else
7176 {
7177 /*
7178 * Delete character(s) before the cursor.
7179 */
7180#ifdef FEAT_RIGHTLEFT
7181 if (revins_on) /* put cursor on last inserted char */
7182 dec_cursor();
7183#endif
7184 mincol = 0;
7185 /* keep indent */
7186 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7187#ifdef FEAT_RIGHTLEFT
7188 && !revins_on
7189#endif
7190 )
7191 {
7192 temp = curwin->w_cursor.col;
7193 beginline(BL_WHITE);
7194 if (curwin->w_cursor.col < (colnr_T)temp)
7195 mincol = curwin->w_cursor.col;
7196 curwin->w_cursor.col = temp;
7197 }
7198
7199 /*
7200 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7201 */
7202 if ( mode == BACKSPACE_CHAR
7203 && ((p_sta && in_indent)
7204 || (curbuf->b_p_sts
7205 && (*(ml_get_cursor() - 1) == TAB
7206 || (*(ml_get_cursor() - 1) == ' '
7207 && (!*inserted_space_p
7208 || arrow_used))))))
7209 {
7210 int ts;
7211 colnr_T vcol;
7212 colnr_T want_vcol;
7213 int extra = 0;
7214
7215 *inserted_space_p = FALSE;
7216 if (p_sta)
7217 ts = curbuf->b_p_sw;
7218 else
7219 ts = curbuf->b_p_sts;
7220 /* Compute the virtual column where we want to be. Since
7221 * 'showbreak' may get in the way, need to get the last column of
7222 * the previous character. */
7223 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7224 dec_cursor();
7225 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7226 inc_cursor();
7227 want_vcol = (want_vcol / ts) * ts;
7228
7229 /* delete characters until we are at or before want_vcol */
7230 while (vcol > want_vcol
7231 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7232 {
7233 dec_cursor();
7234 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7235 if (State & REPLACE_FLAG)
7236 {
7237 /* Don't delete characters before the insert point when in
7238 * Replace mode */
7239 if (curwin->w_cursor.lnum != Insstart.lnum
7240 || curwin->w_cursor.col >= Insstart.col)
7241 {
7242#if 0 /* what was this for? It causes problems when sw != ts. */
7243 if (State == REPLACE && (int)vcol < want_vcol)
7244 {
7245 (void)del_char(FALSE);
7246 extra = 2; /* don't pop too much */
7247 }
7248 else
7249#endif
7250 replace_do_bs();
7251 }
7252 }
7253 else
7254 (void)del_char(FALSE);
7255 }
7256
7257 /* insert extra spaces until we are at want_vcol */
7258 while (vcol < want_vcol)
7259 {
7260 /* Remember the first char we inserted */
7261 if (curwin->w_cursor.lnum == Insstart.lnum
7262 && curwin->w_cursor.col < Insstart.col)
7263 Insstart.col = curwin->w_cursor.col;
7264
7265#ifdef FEAT_VREPLACE
7266 if (State & VREPLACE_FLAG)
7267 ins_char(' ');
7268 else
7269#endif
7270 {
7271 ins_str((char_u *)" ");
7272 if ((State & REPLACE_FLAG) && extra <= 1)
7273 {
7274 if (extra)
7275 replace_push_off(NUL);
7276 else
7277 replace_push(NUL);
7278 }
7279 if (extra == 2)
7280 extra = 1;
7281 }
7282 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7283 }
7284 }
7285
7286 /*
7287 * Delete upto starting point, start of line or previous word.
7288 */
7289 else do
7290 {
7291#ifdef FEAT_RIGHTLEFT
7292 if (!revins_on) /* put cursor on char to be deleted */
7293#endif
7294 dec_cursor();
7295
7296 /* start of word? */
7297 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7298 {
7299 mode = BACKSPACE_WORD_NOT_SPACE;
7300 temp = vim_iswordc(gchar_cursor());
7301 }
7302 /* end of word? */
7303 else if (mode == BACKSPACE_WORD_NOT_SPACE
7304 && (vim_isspace(cc = gchar_cursor())
7305 || vim_iswordc(cc) != temp))
7306 {
7307#ifdef FEAT_RIGHTLEFT
7308 if (!revins_on)
7309#endif
7310 inc_cursor();
7311#ifdef FEAT_RIGHTLEFT
7312 else if (State & REPLACE_FLAG)
7313 dec_cursor();
7314#endif
7315 break;
7316 }
7317 if (State & REPLACE_FLAG)
7318 replace_do_bs();
7319 else
7320 {
7321#ifdef FEAT_MBYTE
7322 if (enc_utf8 && p_deco)
7323 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7324#endif
7325 (void)del_char(FALSE);
7326#ifdef FEAT_MBYTE
7327 /*
7328 * If p1 or p2 is non-zero, there are combining characters we
7329 * need to take account of. Don't back up before the base
7330 * character.
7331 */
7332 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7333 inc_cursor();
7334#endif
7335#ifdef FEAT_RIGHTLEFT
7336 if (revins_chars)
7337 {
7338 revins_chars--;
7339 revins_legal++;
7340 }
7341 if (revins_on && gchar_cursor() == NUL)
7342 break;
7343#endif
7344 }
7345 /* Just a single backspace?: */
7346 if (mode == BACKSPACE_CHAR)
7347 break;
7348 } while (
7349#ifdef FEAT_RIGHTLEFT
7350 revins_on ||
7351#endif
7352 (curwin->w_cursor.col > mincol
7353 && (curwin->w_cursor.lnum != Insstart.lnum
7354 || curwin->w_cursor.col != Insstart.col)));
7355 did_backspace = TRUE;
7356 }
7357#ifdef FEAT_SMARTINDENT
7358 did_si = FALSE;
7359 can_si = FALSE;
7360 can_si_back = FALSE;
7361#endif
7362 if (curwin->w_cursor.col <= 1)
7363 did_ai = FALSE;
7364 /*
7365 * It's a little strange to put backspaces into the redo
7366 * buffer, but it makes auto-indent a lot easier to deal
7367 * with.
7368 */
7369 AppendCharToRedobuff(c);
7370
7371 /* If deleted before the insertion point, adjust it */
7372 if (curwin->w_cursor.lnum == Insstart.lnum
7373 && curwin->w_cursor.col < Insstart.col)
7374 Insstart.col = curwin->w_cursor.col;
7375
7376 /* vi behaviour: the cursor moves backward but the character that
7377 * was there remains visible
7378 * Vim behaviour: the cursor moves backward and the character that
7379 * was there is erased from the screen.
7380 * We can emulate the vi behaviour by pretending there is a dollar
7381 * displayed even when there isn't.
7382 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7383 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7384 dollar_vcol = curwin->w_virtcol;
7385
7386 return did_backspace;
7387}
7388
7389#ifdef FEAT_MOUSE
7390 static void
7391ins_mouse(c)
7392 int c;
7393{
7394 pos_T tpos;
7395
7396# ifdef FEAT_GUI
7397 /* When GUI is active, also move/paste when 'mouse' is empty */
7398 if (!gui.in_use)
7399# endif
7400 if (!mouse_has(MOUSE_INSERT))
7401 return;
7402
7403 undisplay_dollar();
7404 tpos = curwin->w_cursor;
7405 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7406 {
7407 start_arrow(&tpos);
7408# ifdef FEAT_CINDENT
7409 can_cindent = TRUE;
7410# endif
7411 }
7412
7413#ifdef FEAT_WINDOWS
7414 /* redraw status lines (in case another window became active) */
7415 redraw_statuslines();
7416#endif
7417}
7418
7419 static void
7420ins_mousescroll(up)
7421 int up;
7422{
7423 pos_T tpos;
7424# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7425 win_T *old_curwin;
7426# endif
7427
7428 tpos = curwin->w_cursor;
7429
7430# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7431 old_curwin = curwin;
7432
7433 /* Currently the mouse coordinates are only known in the GUI. */
7434 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7435 {
7436 int row, col;
7437
7438 row = mouse_row;
7439 col = mouse_col;
7440
7441 /* find the window at the pointer coordinates */
7442 curwin = mouse_find_win(&row, &col);
7443 curbuf = curwin->w_buffer;
7444 }
7445 if (curwin == old_curwin)
7446# endif
7447 undisplay_dollar();
7448
7449 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7450 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7451 else
7452 scroll_redraw(up, 3L);
7453
7454# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7455 curwin->w_redr_status = TRUE;
7456
7457 curwin = old_curwin;
7458 curbuf = curwin->w_buffer;
7459# endif
7460
7461 if (!equalpos(curwin->w_cursor, tpos))
7462 {
7463 start_arrow(&tpos);
7464# ifdef FEAT_CINDENT
7465 can_cindent = TRUE;
7466# endif
7467 }
7468}
7469#endif
7470
7471#ifdef FEAT_GUI
7472 void
7473ins_scroll()
7474{
7475 pos_T tpos;
7476
7477 undisplay_dollar();
7478 tpos = curwin->w_cursor;
7479 if (gui_do_scroll())
7480 {
7481 start_arrow(&tpos);
7482# ifdef FEAT_CINDENT
7483 can_cindent = TRUE;
7484# endif
7485 }
7486}
7487
7488 void
7489ins_horscroll()
7490{
7491 pos_T tpos;
7492
7493 undisplay_dollar();
7494 tpos = curwin->w_cursor;
7495 if (gui_do_horiz_scroll())
7496 {
7497 start_arrow(&tpos);
7498# ifdef FEAT_CINDENT
7499 can_cindent = TRUE;
7500# endif
7501 }
7502}
7503#endif
7504
7505 static void
7506ins_left()
7507{
7508 pos_T tpos;
7509
7510#ifdef FEAT_FOLDING
7511 if ((fdo_flags & FDO_HOR) && KeyTyped)
7512 foldOpenCursor();
7513#endif
7514 undisplay_dollar();
7515 tpos = curwin->w_cursor;
7516 if (oneleft() == OK)
7517 {
7518 start_arrow(&tpos);
7519#ifdef FEAT_RIGHTLEFT
7520 /* If exit reversed string, position is fixed */
7521 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7522 revins_legal++;
7523 revins_chars++;
7524#endif
7525 }
7526
7527 /*
7528 * if 'whichwrap' set for cursor in insert mode may go to
7529 * previous line
7530 */
7531 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7532 {
7533 start_arrow(&tpos);
7534 --(curwin->w_cursor.lnum);
7535 coladvance((colnr_T)MAXCOL);
7536 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7537 }
7538 else
7539 vim_beep();
7540}
7541
7542 static void
7543ins_home(c)
7544 int c;
7545{
7546 pos_T tpos;
7547
7548#ifdef FEAT_FOLDING
7549 if ((fdo_flags & FDO_HOR) && KeyTyped)
7550 foldOpenCursor();
7551#endif
7552 undisplay_dollar();
7553 tpos = curwin->w_cursor;
7554 if (c == K_C_HOME)
7555 curwin->w_cursor.lnum = 1;
7556 curwin->w_cursor.col = 0;
7557#ifdef FEAT_VIRTUALEDIT
7558 curwin->w_cursor.coladd = 0;
7559#endif
7560 curwin->w_curswant = 0;
7561 start_arrow(&tpos);
7562}
7563
7564 static void
7565ins_end(c)
7566 int c;
7567{
7568 pos_T tpos;
7569
7570#ifdef FEAT_FOLDING
7571 if ((fdo_flags & FDO_HOR) && KeyTyped)
7572 foldOpenCursor();
7573#endif
7574 undisplay_dollar();
7575 tpos = curwin->w_cursor;
7576 if (c == K_C_END)
7577 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7578 coladvance((colnr_T)MAXCOL);
7579 curwin->w_curswant = MAXCOL;
7580
7581 start_arrow(&tpos);
7582}
7583
7584 static void
7585ins_s_left()
7586{
7587#ifdef FEAT_FOLDING
7588 if ((fdo_flags & FDO_HOR) && KeyTyped)
7589 foldOpenCursor();
7590#endif
7591 undisplay_dollar();
7592 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7593 {
7594 start_arrow(&curwin->w_cursor);
7595 (void)bck_word(1L, FALSE, FALSE);
7596 curwin->w_set_curswant = TRUE;
7597 }
7598 else
7599 vim_beep();
7600}
7601
7602 static void
7603ins_right()
7604{
7605#ifdef FEAT_FOLDING
7606 if ((fdo_flags & FDO_HOR) && KeyTyped)
7607 foldOpenCursor();
7608#endif
7609 undisplay_dollar();
7610 if (gchar_cursor() != NUL || virtual_active()
7611 )
7612 {
7613 start_arrow(&curwin->w_cursor);
7614 curwin->w_set_curswant = TRUE;
7615#ifdef FEAT_VIRTUALEDIT
7616 if (virtual_active())
7617 oneright();
7618 else
7619#endif
7620 {
7621#ifdef FEAT_MBYTE
7622 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
7625#endif
7626 ++curwin->w_cursor.col;
7627 }
7628
7629#ifdef FEAT_RIGHTLEFT
7630 revins_legal++;
7631 if (revins_chars)
7632 revins_chars--;
7633#endif
7634 }
7635 /* if 'whichwrap' set for cursor in insert mode, may move the
7636 * cursor to the next line */
7637 else if (vim_strchr(p_ww, ']') != NULL
7638 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7639 {
7640 start_arrow(&curwin->w_cursor);
7641 curwin->w_set_curswant = TRUE;
7642 ++curwin->w_cursor.lnum;
7643 curwin->w_cursor.col = 0;
7644 }
7645 else
7646 vim_beep();
7647}
7648
7649 static void
7650ins_s_right()
7651{
7652#ifdef FEAT_FOLDING
7653 if ((fdo_flags & FDO_HOR) && KeyTyped)
7654 foldOpenCursor();
7655#endif
7656 undisplay_dollar();
7657 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7658 || gchar_cursor() != NUL)
7659 {
7660 start_arrow(&curwin->w_cursor);
7661 (void)fwd_word(1L, FALSE, 0);
7662 curwin->w_set_curswant = TRUE;
7663 }
7664 else
7665 vim_beep();
7666}
7667
7668 static void
7669ins_up(startcol)
7670 int startcol; /* when TRUE move to Insstart.col */
7671{
7672 pos_T tpos;
7673 linenr_T old_topline = curwin->w_topline;
7674#ifdef FEAT_DIFF
7675 int old_topfill = curwin->w_topfill;
7676#endif
7677
7678 undisplay_dollar();
7679 tpos = curwin->w_cursor;
7680 if (cursor_up(1L, TRUE) == OK)
7681 {
7682 if (startcol)
7683 coladvance(getvcol_nolist(&Insstart));
7684 if (old_topline != curwin->w_topline
7685#ifdef FEAT_DIFF
7686 || old_topfill != curwin->w_topfill
7687#endif
7688 )
7689 redraw_later(VALID);
7690 start_arrow(&tpos);
7691#ifdef FEAT_CINDENT
7692 can_cindent = TRUE;
7693#endif
7694 }
7695 else
7696 vim_beep();
7697}
7698
7699 static void
7700ins_pageup()
7701{
7702 pos_T tpos;
7703
7704 undisplay_dollar();
7705 tpos = curwin->w_cursor;
7706 if (onepage(BACKWARD, 1L) == OK)
7707 {
7708 start_arrow(&tpos);
7709#ifdef FEAT_CINDENT
7710 can_cindent = TRUE;
7711#endif
7712 }
7713 else
7714 vim_beep();
7715}
7716
7717 static void
7718ins_down(startcol)
7719 int startcol; /* when TRUE move to Insstart.col */
7720{
7721 pos_T tpos;
7722 linenr_T old_topline = curwin->w_topline;
7723#ifdef FEAT_DIFF
7724 int old_topfill = curwin->w_topfill;
7725#endif
7726
7727 undisplay_dollar();
7728 tpos = curwin->w_cursor;
7729 if (cursor_down(1L, TRUE) == OK)
7730 {
7731 if (startcol)
7732 coladvance(getvcol_nolist(&Insstart));
7733 if (old_topline != curwin->w_topline
7734#ifdef FEAT_DIFF
7735 || old_topfill != curwin->w_topfill
7736#endif
7737 )
7738 redraw_later(VALID);
7739 start_arrow(&tpos);
7740#ifdef FEAT_CINDENT
7741 can_cindent = TRUE;
7742#endif
7743 }
7744 else
7745 vim_beep();
7746}
7747
7748 static void
7749ins_pagedown()
7750{
7751 pos_T tpos;
7752
7753 undisplay_dollar();
7754 tpos = curwin->w_cursor;
7755 if (onepage(FORWARD, 1L) == OK)
7756 {
7757 start_arrow(&tpos);
7758#ifdef FEAT_CINDENT
7759 can_cindent = TRUE;
7760#endif
7761 }
7762 else
7763 vim_beep();
7764}
7765
7766#ifdef FEAT_DND
7767 static void
7768ins_drop()
7769{
7770 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7771}
7772#endif
7773
7774/*
7775 * Handle TAB in Insert or Replace mode.
7776 * Return TRUE when the TAB needs to be inserted like a normal character.
7777 */
7778 static int
7779ins_tab()
7780{
7781 int ind;
7782 int i;
7783 int temp;
7784
7785 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7786 Insstart_blank_vcol = get_nolist_virtcol();
7787 if (echeck_abbr(TAB + ABBR_OFF))
7788 return FALSE;
7789
7790 ind = inindent(0);
7791#ifdef FEAT_CINDENT
7792 if (ind)
7793 can_cindent = FALSE;
7794#endif
7795
7796 /*
7797 * When nothing special, insert TAB like a normal character
7798 */
7799 if (!curbuf->b_p_et
7800 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7801 && curbuf->b_p_sts == 0)
7802 return TRUE;
7803
7804 if (stop_arrow() == FAIL)
7805 return TRUE;
7806
7807 did_ai = FALSE;
7808#ifdef FEAT_SMARTINDENT
7809 did_si = FALSE;
7810 can_si = FALSE;
7811 can_si_back = FALSE;
7812#endif
7813 AppendToRedobuff((char_u *)"\t");
7814
7815 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7816 temp = (int)curbuf->b_p_sw;
7817 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7818 temp = (int)curbuf->b_p_sts;
7819 else /* otherwise use 'tabstop' */
7820 temp = (int)curbuf->b_p_ts;
7821 temp -= get_nolist_virtcol() % temp;
7822
7823 /*
7824 * Insert the first space with ins_char(). It will delete one char in
7825 * replace mode. Insert the rest with ins_str(); it will not delete any
7826 * chars. For VREPLACE mode, we use ins_char() for all characters.
7827 */
7828 ins_char(' ');
7829 while (--temp > 0)
7830 {
7831#ifdef FEAT_VREPLACE
7832 if (State & VREPLACE_FLAG)
7833 ins_char(' ');
7834 else
7835#endif
7836 {
7837 ins_str((char_u *)" ");
7838 if (State & REPLACE_FLAG) /* no char replaced */
7839 replace_push(NUL);
7840 }
7841 }
7842
7843 /*
7844 * When 'expandtab' not set: Replace spaces by TABs where possible.
7845 */
7846 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7847 {
7848 char_u *ptr;
7849#ifdef FEAT_VREPLACE
7850 char_u *saved_line = NULL; /* init for GCC */
7851 pos_T pos;
7852#endif
7853 pos_T fpos;
7854 pos_T *cursor;
7855 colnr_T want_vcol, vcol;
7856 int change_col = -1;
7857 int save_list = curwin->w_p_list;
7858
7859 /*
7860 * Get the current line. For VREPLACE mode, don't make real changes
7861 * yet, just work on a copy of the line.
7862 */
7863#ifdef FEAT_VREPLACE
7864 if (State & VREPLACE_FLAG)
7865 {
7866 pos = curwin->w_cursor;
7867 cursor = &pos;
7868 saved_line = vim_strsave(ml_get_curline());
7869 if (saved_line == NULL)
7870 return FALSE;
7871 ptr = saved_line + pos.col;
7872 }
7873 else
7874#endif
7875 {
7876 ptr = ml_get_cursor();
7877 cursor = &curwin->w_cursor;
7878 }
7879
7880 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7881 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7882 curwin->w_p_list = FALSE;
7883
7884 /* Find first white before the cursor */
7885 fpos = curwin->w_cursor;
7886 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7887 {
7888 --fpos.col;
7889 --ptr;
7890 }
7891
7892 /* In Replace mode, don't change characters before the insert point. */
7893 if ((State & REPLACE_FLAG)
7894 && fpos.lnum == Insstart.lnum
7895 && fpos.col < Insstart.col)
7896 {
7897 ptr += Insstart.col - fpos.col;
7898 fpos.col = Insstart.col;
7899 }
7900
7901 /* compute virtual column numbers of first white and cursor */
7902 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7903 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7904
7905 /* Use as many TABs as possible. Beware of 'showbreak' and
7906 * 'linebreak' adding extra virtual columns. */
7907 while (vim_iswhite(*ptr))
7908 {
7909 i = lbr_chartabsize((char_u *)"\t", vcol);
7910 if (vcol + i > want_vcol)
7911 break;
7912 if (*ptr != TAB)
7913 {
7914 *ptr = TAB;
7915 if (change_col < 0)
7916 {
7917 change_col = fpos.col; /* Column of first change */
7918 /* May have to adjust Insstart */
7919 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7920 Insstart.col = fpos.col;
7921 }
7922 }
7923 ++fpos.col;
7924 ++ptr;
7925 vcol += i;
7926 }
7927
7928 if (change_col >= 0)
7929 {
7930 int repl_off = 0;
7931
7932 /* Skip over the spaces we need. */
7933 while (vcol < want_vcol && *ptr == ' ')
7934 {
7935 vcol += lbr_chartabsize(ptr, vcol);
7936 ++ptr;
7937 ++repl_off;
7938 }
7939 if (vcol > want_vcol)
7940 {
7941 /* Must have a char with 'showbreak' just before it. */
7942 --ptr;
7943 --repl_off;
7944 }
7945 fpos.col += repl_off;
7946
7947 /* Delete following spaces. */
7948 i = cursor->col - fpos.col;
7949 if (i > 0)
7950 {
7951 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7952 /* correct replace stack. */
7953 if ((State & REPLACE_FLAG)
7954#ifdef FEAT_VREPLACE
7955 && !(State & VREPLACE_FLAG)
7956#endif
7957 )
7958 for (temp = i; --temp >= 0; )
7959 replace_join(repl_off);
7960 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007961#ifdef FEAT_NETBEANS_INTG
7962 if (usingNetbeans)
7963 {
7964 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7965 (long)(i + 1));
7966 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7967 (char_u *)"\t", 1);
7968 }
7969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 cursor->col -= i;
7971
7972#ifdef FEAT_VREPLACE
7973 /*
7974 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7975 * backspacing over the changed spacing and then inserting the new
7976 * spacing.
7977 */
7978 if (State & VREPLACE_FLAG)
7979 {
7980 /* Backspace from real cursor to change_col */
7981 backspace_until_column(change_col);
7982
7983 /* Insert each char in saved_line from changed_col to
7984 * ptr-cursor */
7985 ins_bytes_len(saved_line + change_col,
7986 cursor->col - change_col);
7987 }
7988#endif
7989 }
7990
7991#ifdef FEAT_VREPLACE
7992 if (State & VREPLACE_FLAG)
7993 vim_free(saved_line);
7994#endif
7995 curwin->w_p_list = save_list;
7996 }
7997
7998 return FALSE;
7999}
8000
8001/*
8002 * Handle CR or NL in insert mode.
8003 * Return TRUE when out of memory or can't undo.
8004 */
8005 static int
8006ins_eol(c)
8007 int c;
8008{
8009 int i;
8010
8011 if (echeck_abbr(c + ABBR_OFF))
8012 return FALSE;
8013 if (stop_arrow() == FAIL)
8014 return TRUE;
8015 undisplay_dollar();
8016
8017 /*
8018 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8019 * character under the cursor. Only push a NUL on the replace stack,
8020 * nothing to put back when the NL is deleted.
8021 */
8022 if ((State & REPLACE_FLAG)
8023#ifdef FEAT_VREPLACE
8024 && !(State & VREPLACE_FLAG)
8025#endif
8026 )
8027 replace_push(NUL);
8028
8029 /*
8030 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8031 * replacing the next line, so we push all of the characters left on the
8032 * line onto the replace stack. This is not done here though, it is done
8033 * in open_line().
8034 */
8035
8036#ifdef FEAT_RIGHTLEFT
8037# ifdef FEAT_FKMAP
8038 if (p_altkeymap && p_fkmap)
8039 fkmap(NL);
8040# endif
8041 /* NL in reverse insert will always start in the end of
8042 * current line. */
8043 if (revins_on)
8044 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8045#endif
8046
8047 AppendToRedobuff(NL_STR);
8048 i = open_line(FORWARD,
8049#ifdef FEAT_COMMENTS
8050 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8051#endif
8052 0, old_indent);
8053 old_indent = 0;
8054#ifdef FEAT_CINDENT
8055 can_cindent = TRUE;
8056#endif
8057
8058 return (!i);
8059}
8060
8061#ifdef FEAT_DIGRAPHS
8062/*
8063 * Handle digraph in insert mode.
8064 * Returns character still to be inserted, or NUL when nothing remaining to be
8065 * done.
8066 */
8067 static int
8068ins_digraph()
8069{
8070 int c;
8071 int cc;
8072
8073 pc_status = PC_STATUS_UNSET;
8074 if (redrawing() && !char_avail())
8075 {
8076 /* may need to redraw when no more chars available now */
8077 ins_redraw();
8078
8079 edit_putchar('?', TRUE);
8080#ifdef FEAT_CMDL_INFO
8081 add_to_showcmd_c(Ctrl_K);
8082#endif
8083 }
8084
8085#ifdef USE_ON_FLY_SCROLL
8086 dont_scroll = TRUE; /* disallow scrolling here */
8087#endif
8088
8089 /* don't map the digraph chars. This also prevents the
8090 * mode message to be deleted when ESC is hit */
8091 ++no_mapping;
8092 ++allow_keys;
8093 c = safe_vgetc();
8094 --no_mapping;
8095 --allow_keys;
8096 if (IS_SPECIAL(c) || mod_mask) /* special key */
8097 {
8098#ifdef FEAT_CMDL_INFO
8099 clear_showcmd();
8100#endif
8101 insert_special(c, TRUE, FALSE);
8102 return NUL;
8103 }
8104 if (c != ESC)
8105 {
8106 if (redrawing() && !char_avail())
8107 {
8108 /* may need to redraw when no more chars available now */
8109 ins_redraw();
8110
8111 if (char2cells(c) == 1)
8112 {
8113 /* first remove the '?', otherwise it's restored when typing
8114 * an ESC next */
8115 edit_unputchar();
8116 ins_redraw();
8117 edit_putchar(c, TRUE);
8118 }
8119#ifdef FEAT_CMDL_INFO
8120 add_to_showcmd_c(c);
8121#endif
8122 }
8123 ++no_mapping;
8124 ++allow_keys;
8125 cc = safe_vgetc();
8126 --no_mapping;
8127 --allow_keys;
8128 if (cc != ESC)
8129 {
8130 AppendToRedobuff((char_u *)CTRL_V_STR);
8131 c = getdigraph(c, cc, TRUE);
8132#ifdef FEAT_CMDL_INFO
8133 clear_showcmd();
8134#endif
8135 return c;
8136 }
8137 }
8138 edit_unputchar();
8139#ifdef FEAT_CMDL_INFO
8140 clear_showcmd();
8141#endif
8142 return NUL;
8143}
8144#endif
8145
8146/*
8147 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8148 * Returns the char to be inserted, or NUL if none found.
8149 */
8150 static int
8151ins_copychar(lnum)
8152 linenr_T lnum;
8153{
8154 int c;
8155 int temp;
8156 char_u *ptr, *prev_ptr;
8157
8158 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8159 {
8160 vim_beep();
8161 return NUL;
8162 }
8163
8164 /* try to advance to the cursor column */
8165 temp = 0;
8166 ptr = ml_get(lnum);
8167 prev_ptr = ptr;
8168 validate_virtcol();
8169 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8170 {
8171 prev_ptr = ptr;
8172 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8173 }
8174 if ((colnr_T)temp > curwin->w_virtcol)
8175 ptr = prev_ptr;
8176
8177#ifdef FEAT_MBYTE
8178 c = (*mb_ptr2char)(ptr);
8179#else
8180 c = *ptr;
8181#endif
8182 if (c == NUL)
8183 vim_beep();
8184 return c;
8185}
8186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008187/*
8188 * CTRL-Y or CTRL-E typed in Insert mode.
8189 */
8190 static int
8191ins_ctrl_ey(tc)
8192 int tc;
8193{
8194 int c = tc;
8195
8196#ifdef FEAT_INS_EXPAND
8197 if (ctrl_x_mode == CTRL_X_SCROLL)
8198 {
8199 if (c == Ctrl_Y)
8200 scrolldown_clamp();
8201 else
8202 scrollup_clamp();
8203 redraw_later(VALID);
8204 }
8205 else
8206#endif
8207 {
8208 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8209 if (c != NUL)
8210 {
8211 long tw_save;
8212
8213 /* The character must be taken literally, insert like it
8214 * was typed after a CTRL-V, and pretend 'textwidth'
8215 * wasn't set. Digits, 'o' and 'x' are special after a
8216 * CTRL-V, don't use it for these. */
8217 if (c < 256 && !isalnum(c))
8218 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8219 tw_save = curbuf->b_p_tw;
8220 curbuf->b_p_tw = -1;
8221 insert_special(c, TRUE, FALSE);
8222 curbuf->b_p_tw = tw_save;
8223#ifdef FEAT_RIGHTLEFT
8224 revins_chars++;
8225 revins_legal++;
8226#endif
8227 c = Ctrl_V; /* pretend CTRL-V is last character */
8228 auto_format(FALSE, TRUE);
8229 }
8230 }
8231 return c;
8232}
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234#ifdef FEAT_SMARTINDENT
8235/*
8236 * Try to do some very smart auto-indenting.
8237 * Used when inserting a "normal" character.
8238 */
8239 static void
8240ins_try_si(c)
8241 int c;
8242{
8243 pos_T *pos, old_pos;
8244 char_u *ptr;
8245 int i;
8246 int temp;
8247
8248 /*
8249 * do some very smart indenting when entering '{' or '}'
8250 */
8251 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8252 {
8253 /*
8254 * for '}' set indent equal to indent of line containing matching '{'
8255 */
8256 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8257 {
8258 old_pos = curwin->w_cursor;
8259 /*
8260 * If the matching '{' has a ')' immediately before it (ignoring
8261 * white-space), then line up with the start of the line
8262 * containing the matching '(' if there is one. This handles the
8263 * case where an "if (..\n..) {" statement continues over multiple
8264 * lines -- webb
8265 */
8266 ptr = ml_get(pos->lnum);
8267 i = pos->col;
8268 if (i > 0) /* skip blanks before '{' */
8269 while (--i > 0 && vim_iswhite(ptr[i]))
8270 ;
8271 curwin->w_cursor.lnum = pos->lnum;
8272 curwin->w_cursor.col = i;
8273 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8274 curwin->w_cursor = *pos;
8275 i = get_indent();
8276 curwin->w_cursor = old_pos;
8277#ifdef FEAT_VREPLACE
8278 if (State & VREPLACE_FLAG)
8279 change_indent(INDENT_SET, i, FALSE, NUL);
8280 else
8281#endif
8282 (void)set_indent(i, SIN_CHANGED);
8283 }
8284 else if (curwin->w_cursor.col > 0)
8285 {
8286 /*
8287 * when inserting '{' after "O" reduce indent, but not
8288 * more than indent of previous line
8289 */
8290 temp = TRUE;
8291 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8292 {
8293 old_pos = curwin->w_cursor;
8294 i = get_indent();
8295 while (curwin->w_cursor.lnum > 1)
8296 {
8297 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8298
8299 /* ignore empty lines and lines starting with '#'. */
8300 if (*ptr != '#' && *ptr != NUL)
8301 break;
8302 }
8303 if (get_indent() >= i)
8304 temp = FALSE;
8305 curwin->w_cursor = old_pos;
8306 }
8307 if (temp)
8308 shift_line(TRUE, FALSE, 1);
8309 }
8310 }
8311
8312 /*
8313 * set indent of '#' always to 0
8314 */
8315 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8316 {
8317 /* remember current indent for next line */
8318 old_indent = get_indent();
8319 (void)set_indent(0, SIN_CHANGED);
8320 }
8321
8322 /* Adjust ai_col, the char at this position can be deleted. */
8323 if (ai_col > curwin->w_cursor.col)
8324 ai_col = curwin->w_cursor.col;
8325}
8326#endif
8327
8328/*
8329 * Get the value that w_virtcol would have when 'list' is off.
8330 * Unless 'cpo' contains the 'L' flag.
8331 */
8332 static colnr_T
8333get_nolist_virtcol()
8334{
8335 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8336 return getvcol_nolist(&curwin->w_cursor);
8337 validate_virtcol();
8338 return curwin->w_virtcol;
8339}