blob: b31177c50f419a46d180085c8584dceaec7dcb25 [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 Moolenaar4be06f92005-07-29 22:36:03 +000034#define CTRL_X_OCCULT 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 Moolenaar4be06f92005-07-29 22:36:03 +000055 N_(" Occult 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));
112static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
113static void ins_compl_free __ARGS((void));
114static void ins_compl_clear __ARGS((void));
115static void ins_compl_prep __ARGS((int c));
116static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
117static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
118static void ins_compl_delete __ARGS((void));
119static void ins_compl_insert __ARGS((void));
120static int ins_compl_next __ARGS((int allow_get_expansion));
121static int ins_complete __ARGS((int c));
122static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
123#endif /* FEAT_INS_EXPAND */
124
125#define BACKSPACE_CHAR 1
126#define BACKSPACE_WORD 2
127#define BACKSPACE_WORD_NOT_SPACE 3
128#define BACKSPACE_LINE 4
129
130static void ins_redraw __ARGS((void));
131static void ins_ctrl_v __ARGS((void));
132static void undisplay_dollar __ARGS((void));
133static void insert_special __ARGS((int, int, int));
134static void check_auto_format __ARGS((int));
135static void redo_literal __ARGS((int c));
136static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000137#ifdef FEAT_SYN_HL
138static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000139static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000140static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
143static int echeck_abbr __ARGS((int));
144static void replace_push_off __ARGS((int c));
145static int replace_pop __ARGS((void));
146static void replace_join __ARGS((int off));
147static void replace_pop_ins __ARGS((void));
148#ifdef FEAT_MBYTE
149static void mb_replace_pop_ins __ARGS((int cc));
150#endif
151static void replace_flush __ARGS((void));
152static void replace_do_bs __ARGS((void));
153#ifdef FEAT_CINDENT
154static int cindent_on __ARGS((void));
155#endif
156static void ins_reg __ARGS((void));
157static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000158static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000159static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_RIGHTLEFT
161static void ins_ctrl_ __ARGS((void));
162#endif
163#ifdef FEAT_VISUAL
164static int ins_start_select __ARGS((int c));
165#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000166static void ins_insert __ARGS((int replaceState));
167static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168static void ins_shift __ARGS((int c, int lastc));
169static void ins_del __ARGS((void));
170static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
171#ifdef FEAT_MOUSE
172static void ins_mouse __ARGS((int c));
173static void ins_mousescroll __ARGS((int up));
174#endif
175static void ins_left __ARGS((void));
176static void ins_home __ARGS((int c));
177static void ins_end __ARGS((int c));
178static void ins_s_left __ARGS((void));
179static void ins_right __ARGS((void));
180static void ins_s_right __ARGS((void));
181static void ins_up __ARGS((int startcol));
182static void ins_pageup __ARGS((void));
183static void ins_down __ARGS((int startcol));
184static void ins_pagedown __ARGS((void));
185#ifdef FEAT_DND
186static void ins_drop __ARGS((void));
187#endif
188static int ins_tab __ARGS((void));
189static int ins_eol __ARGS((int c));
190#ifdef FEAT_DIGRAPHS
191static int ins_digraph __ARGS((void));
192#endif
193static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000194static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifdef FEAT_SMARTINDENT
196static void ins_try_si __ARGS((int c));
197#endif
198static colnr_T get_nolist_virtcol __ARGS((void));
199
200static colnr_T Insstart_textlen; /* length of line when insert started */
201static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
202
203static char_u *last_insert = NULL; /* the text of the previous insert,
204 K_SPECIAL and CSI are escaped */
205static int last_insert_skip; /* nr of chars in front of previous insert */
206static int new_insert_skip; /* nr of chars in front of current insert */
207
208#ifdef FEAT_CINDENT
209static int can_cindent; /* may do cindenting on this line */
210#endif
211
212static int old_indent = 0; /* for ^^D command in insert mode */
213
214#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000215static int revins_on; /* reverse insert mode on */
216static int revins_chars; /* how much to skip after edit */
217static int revins_legal; /* was the last char 'legal'? */
218static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#endif
220
221#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
222static short previous_script = smRoman;
223#endif
224
225static int ins_need_undo; /* call u_save() before inserting a
226 char. Set when edit() is called.
227 after that arrow_used is used. */
228
229static int did_add_space = FALSE; /* auto_format() added an extra space
230 under the cursor */
231
232/*
233 * edit(): Start inserting text.
234 *
235 * "cmdchar" can be:
236 * 'i' normal insert command
237 * 'a' normal append command
238 * 'R' replace command
239 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
240 * but still only one <CR> is inserted. The <Esc> is not used for redo.
241 * 'g' "gI" command.
242 * 'V' "gR" command for Virtual Replace mode.
243 * 'v' "gr" command for single character Virtual Replace mode.
244 *
245 * This function is not called recursively. For CTRL-O commands, it returns
246 * and lets the caller handle the Normal-mode command.
247 *
248 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
249 */
250 int
251edit(cmdchar, startln, count)
252 int cmdchar;
253 int startln; /* if set, insert at start of line */
254 long count;
255{
256 int c = 0;
257 char_u *ptr;
258 int lastc;
259 colnr_T mincol;
260 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 int i;
262 int did_backspace = TRUE; /* previous char was backspace */
263#ifdef FEAT_CINDENT
264 int line_is_white = FALSE; /* line is empty before insert */
265#endif
266 linenr_T old_topline = 0; /* topline before insertion */
267#ifdef FEAT_DIFF
268 int old_topfill = -1;
269#endif
270 int inserted_space = FALSE; /* just inserted a space */
271 int replaceState = REPLACE;
272 int did_restart_edit = restart_edit;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000273 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 /* sleep before redrawing, needed for "CTRL-O :" that results in an
276 * error message */
277 check_for_delay(TRUE);
278
279#ifdef HAVE_SANDBOX
280 /* Don't allow inserting in the sandbox. */
281 if (sandbox != 0)
282 {
283 EMSG(_(e_sandbox));
284 return FALSE;
285 }
286#endif
287
288#ifdef FEAT_INS_EXPAND
289 ins_compl_clear(); /* clear stuff for CTRL-X mode */
290#endif
291
Bram Moolenaar843ee412004-06-30 16:16:41 +0000292#ifdef FEAT_AUTOCMD
293 /*
294 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
295 */
296 if (cmdchar != 'r' && cmdchar != 'v')
297 {
298 if (cmdchar == 'R')
299 ptr = (char_u *)"r";
300 else if (cmdchar == 'V')
301 ptr = (char_u *)"v";
302 else
303 ptr = (char_u *)"i";
304 set_vim_var_string(VV_INSERTMODE, ptr, 1);
305 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
306 }
307#endif
308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_MOUSE
310 /*
311 * When doing a paste with the middle mouse button, Insstart is set to
312 * where the paste started.
313 */
314 if (where_paste_started.lnum != 0)
315 Insstart = where_paste_started;
316 else
317#endif
318 {
319 Insstart = curwin->w_cursor;
320 if (startln)
321 Insstart.col = 0;
322 }
323 Insstart_textlen = linetabsize(ml_get_curline());
324 Insstart_blank_vcol = MAXCOL;
325 if (!did_ai)
326 ai_col = 0;
327
328 if (cmdchar != NUL && restart_edit == 0)
329 {
330 ResetRedobuff();
331 AppendNumberToRedobuff(count);
332#ifdef FEAT_VREPLACE
333 if (cmdchar == 'V' || cmdchar == 'v')
334 {
335 /* "gR" or "gr" command */
336 AppendCharToRedobuff('g');
337 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
338 }
339 else
340#endif
341 {
342 AppendCharToRedobuff(cmdchar);
343 if (cmdchar == 'g') /* "gI" command */
344 AppendCharToRedobuff('I');
345 else if (cmdchar == 'r') /* "r<CR>" command */
346 count = 1; /* insert only one <CR> */
347 }
348 }
349
350 if (cmdchar == 'R')
351 {
352#ifdef FEAT_FKMAP
353 if (p_fkmap && p_ri)
354 {
355 beep_flush();
356 EMSG(farsi_text_3); /* encoded in Farsi */
357 State = INSERT;
358 }
359 else
360#endif
361 State = REPLACE;
362 }
363#ifdef FEAT_VREPLACE
364 else if (cmdchar == 'V' || cmdchar == 'v')
365 {
366 State = VREPLACE;
367 replaceState = VREPLACE;
368 orig_line_count = curbuf->b_ml.ml_line_count;
369 vr_lines_changed = 1;
370 }
371#endif
372 else
373 State = INSERT;
374
375 stop_insert_mode = FALSE;
376
377 /*
378 * Need to recompute the cursor position, it might move when the cursor is
379 * on a TAB or special character.
380 */
381 curs_columns(TRUE);
382
383 /*
384 * Enable langmap or IME, indicated by 'iminsert'.
385 * Note that IME may enabled/disabled without us noticing here, thus the
386 * 'iminsert' value may not reflect what is actually used. It is updated
387 * when hitting <Esc>.
388 */
389 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
390 State |= LANGMAP;
391#ifdef USE_IM_CONTROL
392 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
393#endif
394
395#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
396 KeyScript(previous_script);
397#endif
398
399#ifdef FEAT_MOUSE
400 setmouse();
401#endif
402#ifdef FEAT_CMDL_INFO
403 clear_showcmd();
404#endif
405#ifdef FEAT_RIGHTLEFT
406 /* there is no reverse replace mode */
407 revins_on = (State == INSERT && p_ri);
408 if (revins_on)
409 undisplay_dollar();
410 revins_chars = 0;
411 revins_legal = 0;
412 revins_scol = -1;
413#endif
414
415 /*
416 * Handle restarting Insert mode.
417 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
418 * restart_edit non-zero, and something in the stuff buffer.
419 */
420 if (restart_edit != 0 && stuff_empty())
421 {
422#ifdef FEAT_MOUSE
423 /*
424 * After a paste we consider text typed to be part of the insert for
425 * the pasted text. You can backspace over the pasted text too.
426 */
427 if (where_paste_started.lnum)
428 arrow_used = FALSE;
429 else
430#endif
431 arrow_used = TRUE;
432 restart_edit = 0;
433
434 /*
435 * If the cursor was after the end-of-line before the CTRL-O and it is
436 * now at the end-of-line, put it after the end-of-line (this is not
437 * correct in very rare cases).
438 * Also do this if curswant is greater than the current virtual
439 * column. Eg after "^O$" or "^O80|".
440 */
441 validate_virtcol();
442 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000443 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 || curwin->w_curswant > curwin->w_virtcol)
445 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
446 {
447 if (ptr[1] == NUL)
448 ++curwin->w_cursor.col;
449#ifdef FEAT_MBYTE
450 else if (has_mbyte)
451 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000452 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (ptr[i] == NUL)
454 curwin->w_cursor.col += i;
455 }
456#endif
457 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000458 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 }
460 else
461 arrow_used = FALSE;
462
463 /* we are in insert mode now, don't need to start it anymore */
464 need_start_insertmode = FALSE;
465
466 /* Need to save the line for undo before inserting the first char. */
467 ins_need_undo = TRUE;
468
469#ifdef FEAT_MOUSE
470 where_paste_started.lnum = 0;
471#endif
472#ifdef FEAT_CINDENT
473 can_cindent = TRUE;
474#endif
475#ifdef FEAT_FOLDING
476 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
477 * restarting. */
478 if (!p_im && did_restart_edit == 0)
479 foldOpenCursor();
480#endif
481
482 /*
483 * If 'showmode' is set, show the current (insert/replace/..) mode.
484 * A warning message for changing a readonly file is given here, before
485 * actually changing anything. It's put after the mode, if any.
486 */
487 i = 0;
488 if (p_smd)
489 i = showmode();
490
491 if (!p_im && did_restart_edit == 0)
492 change_warning(i + 1);
493
494#ifdef CURSOR_SHAPE
495 ui_cursor_shape(); /* may show different cursor shape */
496#endif
497#ifdef FEAT_DIGRAPHS
498 do_digraph(-1); /* clear digraphs */
499#endif
500
501/*
502 * Get the current length of the redo buffer, those characters have to be
503 * skipped if we want to get to the inserted characters.
504 */
505 ptr = get_inserted();
506 if (ptr == NULL)
507 new_insert_skip = 0;
508 else
509 {
510 new_insert_skip = (int)STRLEN(ptr);
511 vim_free(ptr);
512 }
513
514 old_indent = 0;
515
516 /*
517 * Main loop in Insert mode: repeat until Insert mode is left.
518 */
519 for (;;)
520 {
521#ifdef FEAT_RIGHTLEFT
522 if (!revins_legal)
523 revins_scol = -1; /* reset on illegal motions */
524 else
525 revins_legal = 0;
526#endif
527 if (arrow_used) /* don't repeat insert when arrow key used */
528 count = 0;
529
530 if (stop_insert_mode)
531 {
532 /* ":stopinsert" used or 'insertmode' reset */
533 count = 0;
534 goto doESCkey;
535 }
536
537 /* set curwin->w_curswant for next K_DOWN or K_UP */
538 if (!arrow_used)
539 curwin->w_set_curswant = TRUE;
540
541 /* If there is no typeahead may check for timestamps (e.g., for when a
542 * menu invoked a shell command). */
543 if (stuff_empty())
544 {
545 did_check_timestamps = FALSE;
546 if (need_check_timestamps)
547 check_timestamps(FALSE);
548 }
549
550 /*
551 * When emsg() was called msg_scroll will have been set.
552 */
553 msg_scroll = FALSE;
554
555#ifdef FEAT_GUI
556 /* When 'mousefocus' is set a mouse movement may have taken us to
557 * another window. "need_mouse_correct" may then be set because of an
558 * autocommand. */
559 if (need_mouse_correct)
560 gui_mouse_correct();
561#endif
562
563#ifdef FEAT_FOLDING
564 /* Open fold at the cursor line, according to 'foldopen'. */
565 if (fdo_flags & FDO_INSERT)
566 foldOpenCursor();
567 /* Close folds where the cursor isn't, according to 'foldclose' */
568 if (!char_avail())
569 foldCheckClose();
570#endif
571
572 /*
573 * If we inserted a character at the last position of the last line in
574 * the window, scroll the window one line up. This avoids an extra
575 * redraw.
576 * This is detected when the cursor column is smaller after inserting
577 * something.
578 * Don't do this when the topline changed already, it has
579 * already been adjusted (by insertchar() calling open_line())).
580 */
581 if (curbuf->b_mod_set
582 && curwin->w_p_wrap
583 && !did_backspace
584 && curwin->w_topline == old_topline
585#ifdef FEAT_DIFF
586 && curwin->w_topfill == old_topfill
587#endif
588 )
589 {
590 mincol = curwin->w_wcol;
591 validate_cursor_col();
592
593 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
594 && curwin->w_wrow == W_WINROW(curwin)
595 + curwin->w_height - 1 - p_so
596 && (curwin->w_cursor.lnum != curwin->w_topline
597#ifdef FEAT_DIFF
598 || curwin->w_topfill > 0
599#endif
600 ))
601 {
602#ifdef FEAT_DIFF
603 if (curwin->w_topfill > 0)
604 --curwin->w_topfill;
605 else
606#endif
607#ifdef FEAT_FOLDING
608 if (hasFolding(curwin->w_topline, NULL, &old_topline))
609 set_topline(curwin, old_topline + 1);
610 else
611#endif
612 set_topline(curwin, curwin->w_topline + 1);
613 }
614 }
615
616 /* May need to adjust w_topline to show the cursor. */
617 update_topline();
618
619 did_backspace = FALSE;
620
621 validate_cursor(); /* may set must_redraw */
622
623 /*
624 * Redraw the display when no characters are waiting.
625 * Also shows mode, ruler and positions cursor.
626 */
627 ins_redraw();
628
629#ifdef FEAT_SCROLLBIND
630 if (curwin->w_p_scb)
631 do_check_scrollbind(TRUE);
632#endif
633
634 update_curswant();
635 old_topline = curwin->w_topline;
636#ifdef FEAT_DIFF
637 old_topfill = curwin->w_topfill;
638#endif
639
640#ifdef USE_ON_FLY_SCROLL
641 dont_scroll = FALSE; /* allow scrolling here */
642#endif
643
644 /*
645 * Get a character for Insert mode.
646 */
647 lastc = c; /* remember previous char for CTRL-D */
648 c = safe_vgetc();
649
650#ifdef FEAT_RIGHTLEFT
651 if (p_hkmap && KeyTyped)
652 c = hkmap(c); /* Hebrew mode mapping */
653#endif
654#ifdef FEAT_FKMAP
655 if (p_fkmap && KeyTyped)
656 c = fkmap(c); /* Farsi mode mapping */
657#endif
658
659#ifdef FEAT_INS_EXPAND
660 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
661 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000662 if (c != K_IGNORE)
663 ins_compl_prep(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664#endif
665
Bram Moolenaar488c6512005-08-11 20:09:58 +0000666 /* CTRL-\ CTRL-N goes to Normal mode,
667 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
668 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (c == Ctrl_BSL)
670 {
671 /* may need to redraw when no more chars available now */
672 ins_redraw();
673 ++no_mapping;
674 ++allow_keys;
675 c = safe_vgetc();
676 --no_mapping;
677 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000678 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000680 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 vungetc(c);
682 c = Ctrl_BSL;
683 }
684 else if (c == Ctrl_G && p_im)
685 continue;
686 else
687 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000688 if (c == Ctrl_O)
689 {
690 ins_ctrl_o();
691 ins_at_eol = FALSE; /* cursor keeps its column */
692 nomove = TRUE;
693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 count = 0;
695 goto doESCkey;
696 }
697 }
698
699#ifdef FEAT_DIGRAPHS
700 c = do_digraph(c);
701#endif
702
703#ifdef FEAT_INS_EXPAND
704 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
705 goto docomplete;
706#endif
707 if (c == Ctrl_V || c == Ctrl_Q)
708 {
709 ins_ctrl_v();
710 c = Ctrl_V; /* pretend CTRL-V is last typed character */
711 continue;
712 }
713
714#ifdef FEAT_CINDENT
715 if (cindent_on()
716# ifdef FEAT_INS_EXPAND
717 && ctrl_x_mode == 0
718# endif
719 )
720 {
721 /* A key name preceded by a bang means this key is not to be
722 * inserted. Skip ahead to the re-indenting below.
723 * A key name preceded by a star means that indenting has to be
724 * done before inserting the key. */
725 line_is_white = inindent(0);
726 if (in_cinkeys(c, '!', line_is_white))
727 goto force_cindent;
728 if (can_cindent && in_cinkeys(c, '*', line_is_white)
729 && stop_arrow() == OK)
730 do_c_expr_indent();
731 }
732#endif
733
734#ifdef FEAT_RIGHTLEFT
735 if (curwin->w_p_rl)
736 switch (c)
737 {
738 case K_LEFT: c = K_RIGHT; break;
739 case K_S_LEFT: c = K_S_RIGHT; break;
740 case K_C_LEFT: c = K_C_RIGHT; break;
741 case K_RIGHT: c = K_LEFT; break;
742 case K_S_RIGHT: c = K_S_LEFT; break;
743 case K_C_RIGHT: c = K_C_LEFT; break;
744 }
745#endif
746
747#ifdef FEAT_VISUAL
748 /*
749 * If 'keymodel' contains "startsel", may start selection. If it
750 * does, a CTRL-O and c will be stuffed, we need to get these
751 * characters.
752 */
753 if (ins_start_select(c))
754 continue;
755#endif
756
757 /*
758 * The big switch to handle a character in insert mode.
759 */
760 switch (c)
761 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000762 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (echeck_abbr(ESC + ABBR_OFF))
764 break;
765 /*FALLTHROUGH*/
766
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000767 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#ifdef FEAT_CMDWIN
769 if (c == Ctrl_C && cmdwin_type != 0)
770 {
771 /* Close the cmdline window. */
772 cmdwin_result = K_IGNORE;
773 got_int = FALSE; /* don't stop executing autocommands et al. */
774 goto doESCkey;
775 }
776#endif
777
778#ifdef UNIX
779do_intr:
780#endif
781 /* when 'insertmode' set, and not halfway a mapping, don't leave
782 * Insert mode */
783 if (goto_im())
784 {
785 if (got_int)
786 {
787 (void)vgetc(); /* flush all buffers */
788 got_int = FALSE;
789 }
790 else
791 vim_beep();
792 break;
793 }
794doESCkey:
795 /*
796 * This is the ONLY return from edit()!
797 */
798 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
799 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000800 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 o_lnum = curwin->w_cursor.lnum;
802
Bram Moolenaar488c6512005-08-11 20:09:58 +0000803 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000804 {
805#ifdef FEAT_AUTOCMD
806 if (cmdchar != 'r' && cmdchar != 'v')
807 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
808 FALSE, curbuf);
809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 continue;
813
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000814 case Ctrl_Z: /* suspend when 'insertmode' set */
815 if (!p_im)
816 goto normalchar; /* insert CTRL-Z as normal char */
817 stuffReadbuff((char_u *)":st\r");
818 c = Ctrl_O;
819 /*FALLTHROUGH*/
820
821 case Ctrl_O: /* execute one command */
822#ifdef FEAT_INS_EXPAND
823 if (ctrl_x_mode == CTRL_X_OCCULT)
824 goto docomplete;
825#endif
826 if (echeck_abbr(Ctrl_O + ABBR_OFF))
827 break;
828 ins_ctrl_o();
829 count = 0;
830 goto doESCkey;
831
Bram Moolenaar572cb562005-08-05 21:35:02 +0000832 case K_INS: /* toggle insert/replace mode */
833 case K_KINS:
834 ins_insert(replaceState);
835 break;
836
837 case K_SELECT: /* end of Select mode mapping - ignore */
838 break;
839
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000840#ifdef FEAT_SNIFF
841 case K_SNIFF: /* Sniff command received */
842 stuffcharReadbuff(K_SNIFF);
843 goto doESCkey;
844#endif
845
846 case K_HELP: /* Help key works like <ESC> <Help> */
847 case K_F1:
848 case K_XF1:
849 stuffcharReadbuff(K_HELP);
850 if (p_im)
851 need_start_insertmode = TRUE;
852 goto doESCkey;
853
854#ifdef FEAT_NETBEANS_INTG
855 case K_F21: /* NetBeans command */
856 ++no_mapping; /* don't map the next key hits */
857 i = safe_vgetc();
858 --no_mapping;
859 netbeans_keycommand(i);
860 break;
861#endif
862
863 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 case NUL:
865 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000866 /* For ^@ the trailing ESC will end the insert, unless there is an
867 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
869 && c != Ctrl_A && !p_im)
870 goto doESCkey; /* quit insert mode */
871 inserted_space = FALSE;
872 break;
873
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000874 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 ins_reg();
876 auto_format(FALSE, TRUE);
877 inserted_space = FALSE;
878 break;
879
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000880 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 ins_ctrl_g();
882 break;
883
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000884 case Ctrl_HAT: /* switch input mode and/or langmap */
885 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 break;
887
888#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000889 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (!p_ari)
891 goto normalchar;
892 ins_ctrl_();
893 break;
894#endif
895
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000896 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
898 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
899 goto docomplete;
900#endif
901 /* FALLTHROUGH */
902
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904# ifdef FEAT_INS_EXPAND
905 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
906 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000907 if (has_compl_option(FALSE))
908 goto docomplete;
909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911# endif
912 ins_shift(c, lastc);
913 auto_format(FALSE, TRUE);
914 inserted_space = FALSE;
915 break;
916
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000917 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 case K_KDEL:
919 ins_del();
920 auto_format(FALSE, TRUE);
921 break;
922
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000923 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 case Ctrl_H:
925 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
926 auto_format(FALSE, TRUE);
927 break;
928
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
931 auto_format(FALSE, TRUE);
932 break;
933
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000934 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000935# ifdef FEAT_COMPL_FUNC
936 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000938 goto docomplete;
939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
941 auto_format(FALSE, TRUE);
942 inserted_space = FALSE;
943 break;
944
945#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000946 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 case K_LEFTMOUSE_NM:
948 case K_LEFTDRAG:
949 case K_LEFTRELEASE:
950 case K_LEFTRELEASE_NM:
951 case K_MIDDLEMOUSE:
952 case K_MIDDLEDRAG:
953 case K_MIDDLERELEASE:
954 case K_RIGHTMOUSE:
955 case K_RIGHTDRAG:
956 case K_RIGHTRELEASE:
957 case K_X1MOUSE:
958 case K_X1DRAG:
959 case K_X1RELEASE:
960 case K_X2MOUSE:
961 case K_X2DRAG:
962 case K_X2RELEASE:
963 ins_mouse(c);
964 break;
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ins_mousescroll(FALSE);
968 break;
969
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 ins_mousescroll(TRUE);
972 break;
973#endif
974
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000975 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 break;
977
978#ifdef FEAT_GUI
979 case K_VER_SCROLLBAR:
980 ins_scroll();
981 break;
982
983 case K_HOR_SCROLLBAR:
984 ins_horscroll();
985 break;
986#endif
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 case K_S_HOME:
991 case K_C_HOME:
992 ins_home(c);
993 break;
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 case K_S_END:
998 case K_C_END:
999 ins_end(c);
1000 break;
1001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001002 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001003 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1004 ins_s_left();
1005 else
1006 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 case K_C_LEFT:
1011 ins_s_left();
1012 break;
1013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001015 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1016 ins_s_right();
1017 else
1018 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 break;
1020
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 case K_C_RIGHT:
1023 ins_s_right();
1024 break;
1025
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001026 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001027 if (mod_mask & MOD_MASK_SHIFT)
1028 ins_pageup();
1029 else
1030 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 case K_PAGEUP:
1035 case K_KPAGEUP:
1036 ins_pageup();
1037 break;
1038
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001039 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001040 if (mod_mask & MOD_MASK_SHIFT)
1041 ins_pagedown();
1042 else
1043 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 break;
1045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 case K_PAGEDOWN:
1048 case K_KPAGEDOWN:
1049 ins_pagedown();
1050 break;
1051
1052#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001053 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 ins_drop();
1055 break;
1056#endif
1057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 c = TAB;
1060 /* FALLTHROUGH */
1061
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1064 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1065 goto docomplete;
1066#endif
1067 inserted_space = FALSE;
1068 if (ins_tab())
1069 goto normalchar; /* insert TAB as a normal char */
1070 auto_format(FALSE, TRUE);
1071 break;
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 c = CAR;
1075 /* FALLTHROUGH */
1076 case CAR:
1077 case NL:
1078#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1079 /* In a quickfix window a <CR> jumps to the error under the
1080 * cursor. */
1081 if (bt_quickfix(curbuf) && c == CAR)
1082 {
1083 do_cmdline_cmd((char_u *)".cc");
1084 break;
1085 }
1086#endif
1087#ifdef FEAT_CMDWIN
1088 if (cmdwin_type != 0)
1089 {
1090 /* Execute the command in the cmdline window. */
1091 cmdwin_result = CAR;
1092 goto doESCkey;
1093 }
1094#endif
1095 if (ins_eol(c) && !p_im)
1096 goto doESCkey; /* out of memory */
1097 auto_format(FALSE, FALSE);
1098 inserted_space = FALSE;
1099 break;
1100
1101#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103# ifdef FEAT_INS_EXPAND
1104 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1105 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 if (has_compl_option(TRUE))
1107 goto docomplete;
1108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110# endif
1111# ifdef FEAT_DIGRAPHS
1112 c = ins_digraph();
1113 if (c == NUL)
1114 break;
1115# endif
1116 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001120 case Ctrl_X: /* Enter CTRL-X mode */
1121 ins_ctrl_x();
1122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (ctrl_x_mode != CTRL_X_TAGS)
1126 goto normalchar;
1127 goto docomplete;
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (ctrl_x_mode != CTRL_X_FILES)
1131 goto normalchar;
1132 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001133
1134 case 's': /* Spelling completion after ^X */
1135 case Ctrl_S:
1136 if (ctrl_x_mode != CTRL_X_SPELL)
1137 goto normalchar;
1138 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#ifdef FEAT_INS_EXPAND
1143 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1144#endif
1145 {
1146 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1147 if (p_im)
1148 {
1149 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1150 break;
1151 goto doESCkey;
1152 }
1153 goto normalchar;
1154 }
1155#ifdef FEAT_INS_EXPAND
1156 /* FALLTHROUGH */
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 case Ctrl_N:
1160 /* if 'complete' is empty then plain ^P is no longer special,
1161 * but it is under other ^X modes */
1162 if (*curbuf->b_p_cpt == NUL
1163 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001164 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 goto normalchar;
1166
1167docomplete:
1168 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 break;
1171#endif /* FEAT_INS_EXPAND */
1172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case Ctrl_Y: /* copy from previous line or scroll down */
1174 case Ctrl_E: /* copy from next line or scroll up */
1175 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 break;
1177
1178 default:
1179#ifdef UNIX
1180 if (c == intr_char) /* special interrupt char */
1181 goto do_intr;
1182#endif
1183
1184 /*
1185 * Insert a nomal character.
1186 */
1187normalchar:
1188#ifdef FEAT_SMARTINDENT
1189 /* Try to perform smart-indenting. */
1190 ins_try_si(c);
1191#endif
1192
1193 if (c == ' ')
1194 {
1195 inserted_space = TRUE;
1196#ifdef FEAT_CINDENT
1197 if (inindent(0))
1198 can_cindent = FALSE;
1199#endif
1200 if (Insstart_blank_vcol == MAXCOL
1201 && curwin->w_cursor.lnum == Insstart.lnum)
1202 Insstart_blank_vcol = get_nolist_virtcol();
1203 }
1204
1205 if (vim_iswordc(c) || !echeck_abbr(
1206#ifdef FEAT_MBYTE
1207 /* Add ABBR_OFF for characters above 0x100, this is
1208 * what check_abbr() expects. */
1209 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1210#endif
1211 c))
1212 {
1213 insert_special(c, FALSE, FALSE);
1214#ifdef FEAT_RIGHTLEFT
1215 revins_legal++;
1216 revins_chars++;
1217#endif
1218 }
1219
1220 auto_format(FALSE, TRUE);
1221
1222#ifdef FEAT_FOLDING
1223 /* When inserting a character the cursor line must never be in a
1224 * closed fold. */
1225 foldOpenCursor();
1226#endif
1227 break;
1228 } /* end of switch (c) */
1229
1230 /* If the cursor was moved we didn't just insert a space */
1231 if (arrow_used)
1232 inserted_space = FALSE;
1233
1234#ifdef FEAT_CINDENT
1235 if (can_cindent && cindent_on()
1236# ifdef FEAT_INS_EXPAND
1237 && ctrl_x_mode == 0
1238# endif
1239 )
1240 {
1241force_cindent:
1242 /*
1243 * Indent now if a key was typed that is in 'cinkeys'.
1244 */
1245 if (in_cinkeys(c, ' ', line_is_white))
1246 {
1247 if (stop_arrow() == OK)
1248 /* re-indent the current line */
1249 do_c_expr_indent();
1250 }
1251 }
1252#endif /* FEAT_CINDENT */
1253
1254 } /* for (;;) */
1255 /* NOTREACHED */
1256}
1257
1258/*
1259 * Redraw for Insert mode.
1260 * This is postponed until getting the next character to make '$' in the 'cpo'
1261 * option work correctly.
1262 * Only redraw when there are no characters available. This speeds up
1263 * inserting sequences of characters (e.g., for CTRL-R).
1264 */
1265 static void
1266ins_redraw()
1267{
1268 if (!char_avail())
1269 {
1270 if (must_redraw)
1271 update_screen(0);
1272 else if (clear_cmdline || redraw_cmdline)
1273 showmode(); /* clear cmdline and show mode */
1274 showruler(FALSE);
1275 setcursor();
1276 emsg_on_display = FALSE; /* may remove error message now */
1277 }
1278}
1279
1280/*
1281 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1282 */
1283 static void
1284ins_ctrl_v()
1285{
1286 int c;
1287
1288 /* may need to redraw when no more chars available now */
1289 ins_redraw();
1290
1291 if (redrawing() && !char_avail())
1292 edit_putchar('^', TRUE);
1293 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1294
1295#ifdef FEAT_CMDL_INFO
1296 add_to_showcmd_c(Ctrl_V);
1297#endif
1298
1299 c = get_literal();
1300#ifdef FEAT_CMDL_INFO
1301 clear_showcmd();
1302#endif
1303 insert_special(c, FALSE, TRUE);
1304#ifdef FEAT_RIGHTLEFT
1305 revins_chars++;
1306 revins_legal++;
1307#endif
1308}
1309
1310/*
1311 * Put a character directly onto the screen. It's not stored in a buffer.
1312 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1313 */
1314static int pc_status;
1315#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1316#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1317#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1318#define PC_STATUS_SET 3 /* pc_bytes was filled */
1319#ifdef FEAT_MBYTE
1320static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1321#else
1322static char_u pc_bytes[2]; /* saved bytes */
1323#endif
1324static int pc_attr;
1325static int pc_row;
1326static int pc_col;
1327
1328 void
1329edit_putchar(c, highlight)
1330 int c;
1331 int highlight;
1332{
1333 int attr;
1334
1335 if (ScreenLines != NULL)
1336 {
1337 update_topline(); /* just in case w_topline isn't valid */
1338 validate_cursor();
1339 if (highlight)
1340 attr = hl_attr(HLF_8);
1341 else
1342 attr = 0;
1343 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1344 pc_col = W_WINCOL(curwin);
1345#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1346 pc_status = PC_STATUS_UNSET;
1347#endif
1348#ifdef FEAT_RIGHTLEFT
1349 if (curwin->w_p_rl)
1350 {
1351 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1352# ifdef FEAT_MBYTE
1353 if (has_mbyte)
1354 {
1355 int fix_col = mb_fix_col(pc_col, pc_row);
1356
1357 if (fix_col != pc_col)
1358 {
1359 screen_putchar(' ', pc_row, fix_col, attr);
1360 --curwin->w_wcol;
1361 pc_status = PC_STATUS_RIGHT;
1362 }
1363 }
1364# endif
1365 }
1366 else
1367#endif
1368 {
1369 pc_col += curwin->w_wcol;
1370#ifdef FEAT_MBYTE
1371 if (mb_lefthalve(pc_row, pc_col))
1372 pc_status = PC_STATUS_LEFT;
1373#endif
1374 }
1375
1376 /* save the character to be able to put it back */
1377#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1378 if (pc_status == PC_STATUS_UNSET)
1379#endif
1380 {
1381 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1382 pc_status = PC_STATUS_SET;
1383 }
1384 screen_putchar(c, pc_row, pc_col, attr);
1385 }
1386}
1387
1388/*
1389 * Undo the previous edit_putchar().
1390 */
1391 void
1392edit_unputchar()
1393{
1394 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1395 {
1396#if defined(FEAT_MBYTE)
1397 if (pc_status == PC_STATUS_RIGHT)
1398 ++curwin->w_wcol;
1399 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1400 redrawWinline(curwin->w_cursor.lnum, FALSE);
1401 else
1402#endif
1403 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1404 }
1405}
1406
1407/*
1408 * Called when p_dollar is set: display a '$' at the end of the changed text
1409 * Only works when cursor is in the line that changes.
1410 */
1411 void
1412display_dollar(col)
1413 colnr_T col;
1414{
1415 colnr_T save_col;
1416
1417 if (!redrawing())
1418 return;
1419
1420 cursor_off();
1421 save_col = curwin->w_cursor.col;
1422 curwin->w_cursor.col = col;
1423#ifdef FEAT_MBYTE
1424 if (has_mbyte)
1425 {
1426 char_u *p;
1427
1428 /* If on the last byte of a multi-byte move to the first byte. */
1429 p = ml_get_curline();
1430 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1431 }
1432#endif
1433 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1434 if (curwin->w_wcol < W_WIDTH(curwin))
1435 {
1436 edit_putchar('$', FALSE);
1437 dollar_vcol = curwin->w_virtcol;
1438 }
1439 curwin->w_cursor.col = save_col;
1440}
1441
1442/*
1443 * Call this function before moving the cursor from the normal insert position
1444 * in insert mode.
1445 */
1446 static void
1447undisplay_dollar()
1448{
1449 if (dollar_vcol)
1450 {
1451 dollar_vcol = 0;
1452 redrawWinline(curwin->w_cursor.lnum, FALSE);
1453 }
1454}
1455
1456/*
1457 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1458 * Keep the cursor on the same character.
1459 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1460 * type == INDENT_DEC decrease indent (for CTRL-D)
1461 * type == INDENT_SET set indent to "amount"
1462 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1463 */
1464 void
1465change_indent(type, amount, round, replaced)
1466 int type;
1467 int amount;
1468 int round;
1469 int replaced; /* replaced character, put on replace stack */
1470{
1471 int vcol;
1472 int last_vcol;
1473 int insstart_less; /* reduction for Insstart.col */
1474 int new_cursor_col;
1475 int i;
1476 char_u *ptr;
1477 int save_p_list;
1478 int start_col;
1479 colnr_T vc;
1480#ifdef FEAT_VREPLACE
1481 colnr_T orig_col = 0; /* init for GCC */
1482 char_u *new_line, *orig_line = NULL; /* init for GCC */
1483
1484 /* VREPLACE mode needs to know what the line was like before changing */
1485 if (State & VREPLACE_FLAG)
1486 {
1487 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1488 orig_col = curwin->w_cursor.col;
1489 }
1490#endif
1491
1492 /* for the following tricks we don't want list mode */
1493 save_p_list = curwin->w_p_list;
1494 curwin->w_p_list = FALSE;
1495 vc = getvcol_nolist(&curwin->w_cursor);
1496 vcol = vc;
1497
1498 /*
1499 * For Replace mode we need to fix the replace stack later, which is only
1500 * possible when the cursor is in the indent. Remember the number of
1501 * characters before the cursor if it's possible.
1502 */
1503 start_col = curwin->w_cursor.col;
1504
1505 /* determine offset from first non-blank */
1506 new_cursor_col = curwin->w_cursor.col;
1507 beginline(BL_WHITE);
1508 new_cursor_col -= curwin->w_cursor.col;
1509
1510 insstart_less = curwin->w_cursor.col;
1511
1512 /*
1513 * If the cursor is in the indent, compute how many screen columns the
1514 * cursor is to the left of the first non-blank.
1515 */
1516 if (new_cursor_col < 0)
1517 vcol = get_indent() - vcol;
1518
1519 if (new_cursor_col > 0) /* can't fix replace stack */
1520 start_col = -1;
1521
1522 /*
1523 * Set the new indent. The cursor will be put on the first non-blank.
1524 */
1525 if (type == INDENT_SET)
1526 (void)set_indent(amount, SIN_CHANGED);
1527 else
1528 {
1529#ifdef FEAT_VREPLACE
1530 int save_State = State;
1531
1532 /* Avoid being called recursively. */
1533 if (State & VREPLACE_FLAG)
1534 State = INSERT;
1535#endif
1536 shift_line(type == INDENT_DEC, round, 1);
1537#ifdef FEAT_VREPLACE
1538 State = save_State;
1539#endif
1540 }
1541 insstart_less -= curwin->w_cursor.col;
1542
1543 /*
1544 * Try to put cursor on same character.
1545 * If the cursor is at or after the first non-blank in the line,
1546 * compute the cursor column relative to the column of the first
1547 * non-blank character.
1548 * If we are not in insert mode, leave the cursor on the first non-blank.
1549 * If the cursor is before the first non-blank, position it relative
1550 * to the first non-blank, counted in screen columns.
1551 */
1552 if (new_cursor_col >= 0)
1553 {
1554 /*
1555 * When changing the indent while the cursor is touching it, reset
1556 * Insstart_col to 0.
1557 */
1558 if (new_cursor_col == 0)
1559 insstart_less = MAXCOL;
1560 new_cursor_col += curwin->w_cursor.col;
1561 }
1562 else if (!(State & INSERT))
1563 new_cursor_col = curwin->w_cursor.col;
1564 else
1565 {
1566 /*
1567 * Compute the screen column where the cursor should be.
1568 */
1569 vcol = get_indent() - vcol;
1570 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1571
1572 /*
1573 * Advance the cursor until we reach the right screen column.
1574 */
1575 vcol = last_vcol = 0;
1576 new_cursor_col = -1;
1577 ptr = ml_get_curline();
1578 while (vcol <= (int)curwin->w_virtcol)
1579 {
1580 last_vcol = vcol;
1581#ifdef FEAT_MBYTE
1582 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001583 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 else
1585#endif
1586 ++new_cursor_col;
1587 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1588 }
1589 vcol = last_vcol;
1590
1591 /*
1592 * May need to insert spaces to be able to position the cursor on
1593 * the right screen column.
1594 */
1595 if (vcol != (int)curwin->w_virtcol)
1596 {
1597 curwin->w_cursor.col = new_cursor_col;
1598 i = (int)curwin->w_virtcol - vcol;
1599 ptr = alloc(i + 1);
1600 if (ptr != NULL)
1601 {
1602 new_cursor_col += i;
1603 ptr[i] = NUL;
1604 while (--i >= 0)
1605 ptr[i] = ' ';
1606 ins_str(ptr);
1607 vim_free(ptr);
1608 }
1609 }
1610
1611 /*
1612 * When changing the indent while the cursor is in it, reset
1613 * Insstart_col to 0.
1614 */
1615 insstart_less = MAXCOL;
1616 }
1617
1618 curwin->w_p_list = save_p_list;
1619
1620 if (new_cursor_col <= 0)
1621 curwin->w_cursor.col = 0;
1622 else
1623 curwin->w_cursor.col = new_cursor_col;
1624 curwin->w_set_curswant = TRUE;
1625 changed_cline_bef_curs();
1626
1627 /*
1628 * May have to adjust the start of the insert.
1629 */
1630 if (State & INSERT)
1631 {
1632 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1633 {
1634 if ((int)Insstart.col <= insstart_less)
1635 Insstart.col = 0;
1636 else
1637 Insstart.col -= insstart_less;
1638 }
1639 if ((int)ai_col <= insstart_less)
1640 ai_col = 0;
1641 else
1642 ai_col -= insstart_less;
1643 }
1644
1645 /*
1646 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1647 * If the number of characters before the cursor decreased, need to pop a
1648 * few characters from the replace stack.
1649 * If the number of characters before the cursor increased, need to push a
1650 * few NULs onto the replace stack.
1651 */
1652 if (REPLACE_NORMAL(State) && start_col >= 0)
1653 {
1654 while (start_col > (int)curwin->w_cursor.col)
1655 {
1656 replace_join(0); /* remove a NUL from the replace stack */
1657 --start_col;
1658 }
1659 while (start_col < (int)curwin->w_cursor.col || replaced)
1660 {
1661 replace_push(NUL);
1662 if (replaced)
1663 {
1664 replace_push(replaced);
1665 replaced = NUL;
1666 }
1667 ++start_col;
1668 }
1669 }
1670
1671#ifdef FEAT_VREPLACE
1672 /*
1673 * For VREPLACE mode, we also have to fix the replace stack. In this case
1674 * it is always possible because we backspace over the whole line and then
1675 * put it back again the way we wanted it.
1676 */
1677 if (State & VREPLACE_FLAG)
1678 {
1679 /* If orig_line didn't allocate, just return. At least we did the job,
1680 * even if you can't backspace. */
1681 if (orig_line == NULL)
1682 return;
1683
1684 /* Save new line */
1685 new_line = vim_strsave(ml_get_curline());
1686 if (new_line == NULL)
1687 return;
1688
1689 /* We only put back the new line up to the cursor */
1690 new_line[curwin->w_cursor.col] = NUL;
1691
1692 /* Put back original line */
1693 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1694 curwin->w_cursor.col = orig_col;
1695
1696 /* Backspace from cursor to start of line */
1697 backspace_until_column(0);
1698
1699 /* Insert new stuff into line again */
1700 ins_bytes(new_line);
1701
1702 vim_free(new_line);
1703 }
1704#endif
1705}
1706
1707/*
1708 * Truncate the space at the end of a line. This is to be used only in an
1709 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1710 * modes.
1711 */
1712 void
1713truncate_spaces(line)
1714 char_u *line;
1715{
1716 int i;
1717
1718 /* find start of trailing white space */
1719 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1720 {
1721 if (State & REPLACE_FLAG)
1722 replace_join(0); /* remove a NUL from the replace stack */
1723 }
1724 line[i + 1] = NUL;
1725}
1726
1727#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1728 || defined(FEAT_COMMENTS) || defined(PROTO)
1729/*
1730 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1731 * modes correctly. May also be used when not in insert mode at all.
1732 */
1733 void
1734backspace_until_column(col)
1735 int col;
1736{
1737 while ((int)curwin->w_cursor.col > col)
1738 {
1739 curwin->w_cursor.col--;
1740 if (State & REPLACE_FLAG)
1741 replace_do_bs();
1742 else
1743 (void)del_char(FALSE);
1744 }
1745}
1746#endif
1747
1748#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1749/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001750 * CTRL-X pressed in Insert mode.
1751 */
1752 static void
1753ins_ctrl_x()
1754{
1755 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1756 * CTRL-V works like CTRL-N */
1757 if (ctrl_x_mode != CTRL_X_CMDLINE)
1758 {
1759 /* if the next ^X<> won't ADD nothing, then reset
1760 * compl_cont_status */
1761 if (compl_cont_status & CONT_N_ADDS)
1762 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1763 else
1764 compl_cont_status = 0;
1765 /* We're not sure which CTRL-X mode it will be yet */
1766 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1767 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1768 edit_submode_pre = NULL;
1769 showmode();
1770 }
1771}
1772
1773/*
1774 * Return TRUE if the 'dict' or 'tsr' option can be used.
1775 */
1776 static int
1777has_compl_option(dict_opt)
1778 int dict_opt;
1779{
1780 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1781 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1782 {
1783 ctrl_x_mode = 0;
1784 edit_submode = NULL;
1785 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1786 : (char_u *)_("'thesaurus' option is empty"),
1787 hl_attr(HLF_E));
1788 if (emsg_silent == 0)
1789 {
1790 vim_beep();
1791 setcursor();
1792 out_flush();
1793 ui_delay(2000L, FALSE);
1794 }
1795 return FALSE;
1796 }
1797 return TRUE;
1798}
1799
1800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1802 * This depends on the current mode.
1803 */
1804 int
1805vim_is_ctrl_x_key(c)
1806 int c;
1807{
1808 /* Always allow ^R - let it's results then be checked */
1809 if (c == Ctrl_R)
1810 return TRUE;
1811
1812 switch (ctrl_x_mode)
1813 {
1814 case 0: /* Not in any CTRL-X mode */
1815 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1816 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001817 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1819 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1820 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001821 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1822 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 case CTRL_X_SCROLL:
1824 return (c == Ctrl_Y || c == Ctrl_E);
1825 case CTRL_X_WHOLE_LINE:
1826 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1827 case CTRL_X_FILES:
1828 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1829 case CTRL_X_DICTIONARY:
1830 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1831 case CTRL_X_THESAURUS:
1832 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1833 case CTRL_X_TAGS:
1834 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1835#ifdef FEAT_FIND_ID
1836 case CTRL_X_PATH_PATTERNS:
1837 return (c == Ctrl_P || c == Ctrl_N);
1838 case CTRL_X_PATH_DEFINES:
1839 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1840#endif
1841 case CTRL_X_CMDLINE:
1842 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1843 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001844#ifdef FEAT_COMPL_FUNC
1845 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001846 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001847#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001848 case CTRL_X_OCCULT:
1849 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar488c6512005-08-11 20:09:58 +00001850 case CTRL_X_SPELL:
1851 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853 EMSG(_(e_internal));
1854 return FALSE;
1855}
1856
1857/*
1858 * This is like ins_compl_add(), but if ic and inf are set, then the
1859 * case of the originally typed text is used, and the case of the completed
1860 * text is infered, ie this tries to work out what case you probably wanted
1861 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001862 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001865ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 char_u *str;
1867 int len;
1868 char_u *fname;
1869 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001870 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
1872 int has_lower = FALSE;
1873 int was_letter = FALSE;
1874 int idx;
1875
1876 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1877 {
1878 /* Infer case of completed part -- webb */
1879 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001880 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
1882 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001883 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001885 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 has_lower = TRUE;
1888 if (isupper(IObuff[idx]))
1889 {
1890 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001891 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1893 break;
1894 }
1895 }
1896 }
1897
1898 /*
1899 * Rule 2: No lower case, 2nd consecutive letter converted to
1900 * upper case.
1901 */
1902 if (!has_lower)
1903 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001904 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001906 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 && islower(IObuff[idx]))
1908 {
1909 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001910 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1912 break;
1913 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001914 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917
1918 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001919 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaar572cb562005-08-05 21:35:02 +00001921 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001923 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924}
1925
1926/*
1927 * Add a match to the list of matches.
1928 * If the given string is already in the list of completions, then return
1929 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001930 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1931 *
1932 * New:
1933 * If the given string is already in the list of completions, then return
1934 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1935 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001937 int
1938ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 char_u *str;
1940 int len;
1941 char_u *fname;
1942 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001943 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001945 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 ui_breakcheck();
1948 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (len < 0)
1951 len = (int)STRLEN(str);
1952
1953 /*
1954 * If the same match is already present, don't add it.
1955 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001956 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001958 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 do
1960 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001961 if ( !(match->cp_flags & ORIGINAL_TEXT)
1962 && STRNCMP(match->cp_str, str, (size_t)len) == 0
1963 && match->cp_str[len] == NUL)
1964 return NOTDONE;
1965 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001966 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968
1969 /*
1970 * Allocate a new match structure.
1971 * Copy the values to the new match structure.
1972 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001973 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001975 return FAIL;
1976 match->cp_number = -1;
1977 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001979 match->cp_number = 0;
1980 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001982 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00001985 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00001988 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
1990 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001991 if (fname && compl_curr_match && compl_curr_match->cp_fname
1992 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
1993 match->cp_fname = compl_curr_match->cp_fname;
1994 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
1995 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00001997 match->cp_fname = NULL;
1998 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 /*
2001 * Link the new match structure in the list of matches.
2002 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002003 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002004 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 else if (dir == FORWARD)
2006 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002007 match->cp_next = compl_curr_match->cp_next;
2008 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 else /* BACKWARD */
2011 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002012 match->cp_next = compl_curr_match;
2013 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002015 if (match->cp_next)
2016 match->cp_next->cp_prev = match;
2017 if (match->cp_prev)
2018 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002020 compl_first_match = match;
2021 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
2023 return OK;
2024}
2025
2026/*
2027 * Add an array of matches to the list of matches.
2028 * Frees matches[].
2029 */
2030 static void
2031ins_compl_add_matches(num_matches, matches, dir)
2032 int num_matches;
2033 char_u **matches;
2034 int dir;
2035{
2036 int i;
2037 int add_r = OK;
2038 int ldir = dir;
2039
Bram Moolenaar572cb562005-08-05 21:35:02 +00002040 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2042 /* if dir was BACKWARD then honor it just once */
2043 ldir = FORWARD;
2044 FreeWild(num_matches, matches);
2045}
2046
2047/* Make the completion list cyclic.
2048 * Return the number of matches (excluding the original).
2049 */
2050 static int
2051ins_compl_make_cyclic()
2052{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002053 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 int count = 0;
2055
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002056 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 /*
2059 * Find the end of the list.
2060 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002061 match = compl_first_match;
2062 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002063 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002065 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 ++count;
2067 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 match->cp_next = compl_first_match;
2069 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 return count;
2072}
2073
2074#define DICT_FIRST (1) /* use just first element in "dict" */
2075#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2076/*
2077 * Add any identifiers that match the given pattern to the list of
2078 * completions.
2079 */
2080 static void
2081ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2082 char_u *dict;
2083 char_u *pat;
2084 int dir;
2085 int flags;
2086 int thesaurus;
2087{
2088 char_u *ptr;
2089 char_u *buf;
2090 FILE *fp;
2091 regmatch_T regmatch;
2092 int add_r;
2093 char_u **files;
2094 int count;
2095 int i;
2096 int save_p_scs;
2097
2098 buf = alloc(LSIZE);
2099 /* If 'infercase' is set, don't use 'smartcase' here */
2100 save_p_scs = p_scs;
2101 if (curbuf->b_p_inf)
2102 p_scs = FALSE;
2103 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2104 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2105 regmatch.rm_ic = ignorecase(pat);
2106 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002107 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 /* copy one dictionary file name into buf */
2110 if (flags == DICT_EXACT)
2111 {
2112 count = 1;
2113 files = &dict;
2114 }
2115 else
2116 {
2117 /* Expand wildcards in the dictionary name, but do not allow
2118 * backticks (for security, the 'dict' option may have been set in
2119 * a modeline). */
2120 copy_option_part(&dict, buf, LSIZE, ",");
2121 if (vim_strchr(buf, '`') != NULL
2122 || expand_wildcards(1, &buf, &count, &files,
2123 EW_FILE|EW_SILENT) != OK)
2124 count = 0;
2125 }
2126
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002127 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2130 if (flags != DICT_EXACT)
2131 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002132 vim_snprintf((char *)IObuff, IOSIZE,
2133 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2135 }
2136
2137 if (fp != NULL)
2138 {
2139 /*
2140 * Read dictionary file line by line.
2141 * Check each line for a match.
2142 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002143 while (!got_int && !compl_interrupted
2144 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 ptr = buf;
2147 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2148 {
2149 ptr = regmatch.startp[0];
2150 ptr = find_word_end(ptr);
2151 add_r = ins_compl_add_infercase(regmatch.startp[0],
2152 (int)(ptr - regmatch.startp[0]),
2153 files[i], dir, 0);
2154 if (thesaurus)
2155 {
2156 char_u *wstart;
2157
2158 /*
2159 * Add the other matches on the line
2160 */
2161 while (!got_int)
2162 {
2163 /* Find start of the next word. Skip white
2164 * space and punctuation. */
2165 ptr = find_word_start(ptr);
2166 if (*ptr == NUL || *ptr == NL)
2167 break;
2168 wstart = ptr;
2169
2170 /* Find end of the word and add it. */
2171#ifdef FEAT_MBYTE
2172 if (has_mbyte)
2173 /* Japanese words may have characters in
2174 * different classes, only separate words
2175 * with single-byte non-word characters. */
2176 while (*ptr != NUL)
2177 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002178 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 if (l < 2 && !vim_iswordc(*ptr))
2181 break;
2182 ptr += l;
2183 }
2184 else
2185#endif
2186 ptr = find_word_end(ptr);
2187 add_r = ins_compl_add_infercase(wstart,
2188 (int)(ptr - wstart), files[i], dir, 0);
2189 }
2190 }
2191 if (add_r == OK)
2192 /* if dir was BACKWARD then honor it just once */
2193 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002194 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 break;
2196 /* avoid expensive call to vim_regexec() when at end
2197 * of line */
2198 if (*ptr == '\n' || got_int)
2199 break;
2200 }
2201 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002202 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 fclose(fp);
2205 }
2206 }
2207 if (flags != DICT_EXACT)
2208 FreeWild(count, files);
2209 if (flags)
2210 break;
2211 }
2212 p_scs = save_p_scs;
2213 vim_free(regmatch.regprog);
2214 vim_free(buf);
2215}
2216
2217/*
2218 * Find the start of the next word.
2219 * Returns a pointer to the first char of the word. Also stops at a NUL.
2220 */
2221 char_u *
2222find_word_start(ptr)
2223 char_u *ptr;
2224{
2225#ifdef FEAT_MBYTE
2226 if (has_mbyte)
2227 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002228 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 else
2230#endif
2231 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2232 ++ptr;
2233 return ptr;
2234}
2235
2236/*
2237 * Find the end of the word. Assumes it starts inside a word.
2238 * Returns a pointer to just after the word.
2239 */
2240 char_u *
2241find_word_end(ptr)
2242 char_u *ptr;
2243{
2244#ifdef FEAT_MBYTE
2245 int start_class;
2246
2247 if (has_mbyte)
2248 {
2249 start_class = mb_get_class(ptr);
2250 if (start_class > 1)
2251 while (*ptr != NUL)
2252 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002253 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (mb_get_class(ptr) != start_class)
2255 break;
2256 }
2257 }
2258 else
2259#endif
2260 while (vim_iswordc(*ptr))
2261 ++ptr;
2262 return ptr;
2263}
2264
2265/*
2266 * Free the list of completions
2267 */
2268 static void
2269ins_compl_free()
2270{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002271 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002273 vim_free(compl_pattern);
2274 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002276 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002278 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 do
2280 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002281 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002282 compl_curr_match = compl_curr_match->cp_next;
2283 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002285 if (match->cp_flags & FREE_FNAME)
2286 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2289 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290}
2291
2292 static void
2293ins_compl_clear()
2294{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 compl_cont_status = 0;
2296 compl_started = FALSE;
2297 compl_matches = 0;
2298 vim_free(compl_pattern);
2299 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 save_sm = -1;
2301 edit_submode_extra = NULL;
2302}
2303
2304/*
2305 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002306 * Called just after typing a character in Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 static void
2309ins_compl_prep(c)
2310 int c;
2311{
2312 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 int temp;
2314 int want_cindent;
2315
2316 /* Forget any previous 'special' messages if this is actually
2317 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2318 */
2319 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2320 edit_submode_extra = NULL;
2321
2322 /* Ignore end of Select mode mapping */
2323 if (c == K_SELECT)
2324 return;
2325
2326 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2327 {
2328 /*
2329 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2330 * it will be yet. Now we decide.
2331 */
2332 switch (c)
2333 {
2334 case Ctrl_E:
2335 case Ctrl_Y:
2336 ctrl_x_mode = CTRL_X_SCROLL;
2337 if (!(State & REPLACE_FLAG))
2338 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2339 else
2340 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2341 edit_submode_pre = NULL;
2342 showmode();
2343 break;
2344 case Ctrl_L:
2345 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2346 break;
2347 case Ctrl_F:
2348 ctrl_x_mode = CTRL_X_FILES;
2349 break;
2350 case Ctrl_K:
2351 ctrl_x_mode = CTRL_X_DICTIONARY;
2352 break;
2353 case Ctrl_R:
2354 /* Simply allow ^R to happen without affecting ^X mode */
2355 break;
2356 case Ctrl_T:
2357 ctrl_x_mode = CTRL_X_THESAURUS;
2358 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002359#ifdef FEAT_COMPL_FUNC
2360 case Ctrl_U:
2361 ctrl_x_mode = CTRL_X_FUNCTION;
2362 break;
2363#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002364 case Ctrl_O:
2365 ctrl_x_mode = CTRL_X_OCCULT;
2366 break;
Bram Moolenaar488c6512005-08-11 20:09:58 +00002367 case 's':
2368 case Ctrl_S:
2369 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002370#ifdef FEAT_SYN_HL
2371 spell_back_to_badword();
2372#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 case Ctrl_RSB:
2375 ctrl_x_mode = CTRL_X_TAGS;
2376 break;
2377#ifdef FEAT_FIND_ID
2378 case Ctrl_I:
2379 case K_S_TAB:
2380 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2381 break;
2382 case Ctrl_D:
2383 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2384 break;
2385#endif
2386 case Ctrl_V:
2387 case Ctrl_Q:
2388 ctrl_x_mode = CTRL_X_CMDLINE;
2389 break;
2390 case Ctrl_P:
2391 case Ctrl_N:
2392 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2393 * just started ^X mode, or there were enough ^X's to cancel
2394 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2395 * do normal expansion when interrupting a different mode (say
2396 * ^X^F^X^P or ^P^X^X^P, see below)
2397 * nothing changes if interrupting mode 0, (eg, the flag
2398 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002399 if (!(compl_cont_status & CONT_INTRPT))
2400 compl_cont_status |= CONT_LOCAL;
2401 else if (compl_cont_mode != 0)
2402 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 /* FALLTHROUGH */
2404 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002405 /* If we have typed at least 2 ^X's... for modes != 0, we set
2406 * compl_cont_status = 0 (eg, as if we had just started ^X
2407 * mode).
2408 * For mode 0, we set "compl_cont_mode" to an impossible
2409 * value, in both cases ^X^X can be used to restart the same
2410 * mode (avoiding ADDING mode).
2411 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2412 * 'complete' and local ^P expansions respectively.
2413 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2414 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (c == Ctrl_X)
2416 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002417 if (compl_cont_mode != 0)
2418 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002420 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 ctrl_x_mode = 0;
2423 edit_submode = NULL;
2424 showmode();
2425 break;
2426 }
2427 }
2428 else if (ctrl_x_mode != 0)
2429 {
2430 /* We're already in CTRL-X mode, do we stay in it? */
2431 if (!vim_is_ctrl_x_key(c))
2432 {
2433 if (ctrl_x_mode == CTRL_X_SCROLL)
2434 ctrl_x_mode = 0;
2435 else
2436 ctrl_x_mode = CTRL_X_FINISHED;
2437 edit_submode = NULL;
2438 }
2439 showmode();
2440 }
2441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002442 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 /* Show error message from attempted keyword completion (probably
2445 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002446 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 showmode();
2448 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2449 || ctrl_x_mode == CTRL_X_FINISHED)
2450 {
2451 /* Get here when we have finished typing a sequence of ^N and
2452 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002453 * memory that was used, and make sure we can redo the insert. */
2454 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002456 char_u *p;
2457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 /*
2459 * If any of the original typed text has been changed,
2460 * eg when ignorecase is set, we must add back-spaces to
2461 * the redo buffer. We add as few as necessary to delete
2462 * just the part of the original text that has changed.
2463 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002464 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002465 p = compl_orig_text;
2466 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002468 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 ++ptr;
2470 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002471 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 AppendCharToRedobuff(K_BS);
2473 AppendToRedobuffLit(ptr);
2474 }
2475
2476#ifdef FEAT_CINDENT
2477 want_cindent = (can_cindent && cindent_on());
2478#endif
2479 /*
2480 * When completing whole lines: fix indent for 'cindent'.
2481 * Otherwise, break line if it's too long.
2482 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002483 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485#ifdef FEAT_CINDENT
2486 /* re-indent the current line */
2487 if (want_cindent)
2488 {
2489 do_c_expr_indent();
2490 want_cindent = FALSE; /* don't do it again */
2491 }
2492#endif
2493 }
2494 else
2495 {
2496 /* put the cursor on the last char, for 'tw' formatting */
2497 curwin->w_cursor.col--;
2498 if (stop_arrow() == OK)
2499 insertchar(NUL, 0, -1);
2500 curwin->w_cursor.col++;
2501 }
2502
2503 auto_format(FALSE, TRUE);
2504
2505 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002506 compl_started = FALSE;
2507 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 msg_clr_cmdline(); /* necessary for "noshowmode" */
2509 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002510 if (save_sm >= 0)
2511 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (edit_submode != NULL)
2513 {
2514 edit_submode = NULL;
2515 showmode();
2516 }
2517
2518#ifdef FEAT_CINDENT
2519 /*
2520 * Indent now if a key was typed that is in 'cinkeys'.
2521 */
2522 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2523 do_c_expr_indent();
2524#endif
2525 }
2526 }
2527
2528 /* reset continue_* if we left expansion-mode, if we stay they'll be
2529 * (re)set properly in ins_complete() */
2530 if (!vim_is_ctrl_x_key(c))
2531 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002532 compl_cont_status = 0;
2533 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535}
2536
2537/*
2538 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2539 * (depending on flag) starting from buf and looking for a non-scanned
2540 * buffer (other than curbuf). curbuf is special, if it is called with
2541 * buf=curbuf then it has to be the first call for a given flag/expansion.
2542 *
2543 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2544 */
2545 static buf_T *
2546ins_compl_next_buf(buf, flag)
2547 buf_T *buf;
2548 int flag;
2549{
2550#ifdef FEAT_WINDOWS
2551 static win_T *wp;
2552#endif
2553
2554 if (flag == 'w') /* just windows */
2555 {
2556#ifdef FEAT_WINDOWS
2557 if (buf == curbuf) /* first call for this flag/expansion */
2558 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002559 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 && wp->w_buffer->b_scanned)
2561 ;
2562 buf = wp->w_buffer;
2563#else
2564 buf = curbuf;
2565#endif
2566 }
2567 else
2568 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2569 * (unlisted buffers)
2570 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002571 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 && ((flag == 'U'
2573 ? buf->b_p_bl
2574 : (!buf->b_p_bl
2575 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2576 || buf->b_scanned
2577 || (buf->b_ml.ml_mfp == NULL
2578 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2579 ;
2580 return buf;
2581}
2582
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002583#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002584static int expand_by_function __ARGS((int col, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002585
2586/*
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002587 * Execute user defined complete function 'completefunc', and get matches in
2588 * "matches".
2589 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002590 */
2591 static int
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002592expand_by_function(col, base, matches)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002593 int col;
2594 char_u *base;
2595 char_u ***matches;
2596{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002597 list_T *matchlist;
2598 char_u colbuf[30];
2599 char_u *args[3];
2600 listitem_T *li;
2601 garray_T ga;
2602 char_u *p;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002603
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002604 if (*curbuf->b_p_cfu == NUL)
2605 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002606
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002607 /* Call 'completefunc' to obtain the list of matches. */
2608 args[0] = (char_u *)"0";
2609 sprintf((char *)colbuf, "%d", col + (int)STRLEN(base));
2610 args[1] = colbuf;
2611 args[2] = base;
2612
2613 matchlist = call_func_retlist(curbuf->b_p_cfu, 3, args, FALSE);
2614 if (matchlist == NULL)
2615 return 0;
2616
2617 /* Go through the List with matches and put them in an array. */
2618 ga_init2(&ga, (int)sizeof(char_u *), 8);
2619 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002620 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002621 p = get_tv_string_chk(&li->li_tv);
2622 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002623 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002624 if (ga_grow(&ga, 1) == FAIL)
2625 break;
2626 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2627 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002628 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002629 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002630
2631 list_unref(matchlist);
2632 *matches = (char_u **)ga.ga_data;
2633 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002634}
2635#endif /* FEAT_COMPL_FUNC */
2636
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002637static int expand_occult __ARGS((linenr_T lnum, int col, char_u *base, char_u ***matches));
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002640 * Perform occult completion'
2641 * Return value is number of candidates and array of candidates as "matchp".
2642 */
2643 static int
2644expand_occult(lnum, col, pat, matchp)
2645 linenr_T lnum;
2646 int col;
2647 char_u *pat;
2648 char_u ***matchp;
2649{
2650 int num_matches;
2651
2652 /* Use tag completion for now. */
2653 if (find_tags(pat, &num_matches, matchp,
2654 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
2655 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
2656 TAG_MANY, curbuf->b_ffname) == FAIL)
2657 return 0;
2658 return num_matches;
2659}
2660
2661/*
2662 * Get the next expansion(s), using "compl_pattern".
2663 * The search starts at position "ini" in curbuf and in the direction dir.
2664 * When "compl_started" is FALSE start at that position, otherwise
2665 * continue where we stopped searching before.
2666 * This may return before finding all the matches.
2667 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 */
2669 static int
2670ins_compl_get_exp(ini, dir)
2671 pos_T *ini;
2672 int dir;
2673{
2674 static pos_T first_match_pos;
2675 static pos_T last_match_pos;
2676 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002677 static int found_all = FALSE; /* Found all matches of a
2678 certain type. */
2679 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
Bram Moolenaar572cb562005-08-05 21:35:02 +00002681 pos_T *pos;
2682 char_u **matches;
2683 int save_p_scs;
2684 int save_p_ws;
2685 int save_p_ic;
2686 int i;
2687 int num_matches;
2688 int len;
2689 int found_new_match;
2690 int type = ctrl_x_mode;
2691 char_u *ptr;
2692 char_u *dict = NULL;
2693 int dict_f = 0;
2694 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002696 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2699 ins_buf->b_scanned = 0;
2700 found_all = FALSE;
2701 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002702 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002703 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 last_match_pos = first_match_pos = *ini;
2705 }
2706
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002707 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2709 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2710 for (;;)
2711 {
2712 found_new_match = FAIL;
2713
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002714 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 * or if found_all says this entry is done. For ^X^L only use the
2716 * entries from 'complete' that look in loaded buffers. */
2717 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002718 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 found_all = FALSE;
2721 while (*e_cpt == ',' || *e_cpt == ' ')
2722 e_cpt++;
2723 if (*e_cpt == '.' && !curbuf->b_scanned)
2724 {
2725 ins_buf = curbuf;
2726 first_match_pos = *ini;
2727 /* So that ^N can match word immediately after cursor */
2728 if (ctrl_x_mode == 0)
2729 dec(&first_match_pos);
2730 last_match_pos = first_match_pos;
2731 type = 0;
2732 }
2733 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2734 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2735 {
2736 /* Scan a buffer, but not the current one. */
2737 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2738 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002739 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 first_match_pos.col = last_match_pos.col = 0;
2741 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2742 last_match_pos.lnum = 0;
2743 type = 0;
2744 }
2745 else /* unloaded buffer, scan like dictionary */
2746 {
2747 found_all = TRUE;
2748 if (ins_buf->b_fname == NULL)
2749 continue;
2750 type = CTRL_X_DICTIONARY;
2751 dict = ins_buf->b_fname;
2752 dict_f = DICT_EXACT;
2753 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002754 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 ins_buf->b_fname == NULL
2756 ? buf_spname(ins_buf)
2757 : ins_buf->b_sfname == NULL
2758 ? (char *)ins_buf->b_fname
2759 : (char *)ins_buf->b_sfname);
2760 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2761 }
2762 else if (*e_cpt == NUL)
2763 break;
2764 else
2765 {
2766 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2767 type = -1;
2768 else if (*e_cpt == 'k' || *e_cpt == 's')
2769 {
2770 if (*e_cpt == 'k')
2771 type = CTRL_X_DICTIONARY;
2772 else
2773 type = CTRL_X_THESAURUS;
2774 if (*++e_cpt != ',' && *e_cpt != NUL)
2775 {
2776 dict = e_cpt;
2777 dict_f = DICT_FIRST;
2778 }
2779 }
2780#ifdef FEAT_FIND_ID
2781 else if (*e_cpt == 'i')
2782 type = CTRL_X_PATH_PATTERNS;
2783 else if (*e_cpt == 'd')
2784 type = CTRL_X_PATH_DEFINES;
2785#endif
2786 else if (*e_cpt == ']' || *e_cpt == 't')
2787 {
2788 type = CTRL_X_TAGS;
2789 sprintf((char*)IObuff, _("Scanning tags."));
2790 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2791 }
2792 else
2793 type = -1;
2794
2795 /* in any case e_cpt is advanced to the next entry */
2796 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2797
2798 found_all = TRUE;
2799 if (type == -1)
2800 continue;
2801 }
2802 }
2803
2804 switch (type)
2805 {
2806 case -1:
2807 break;
2808#ifdef FEAT_FIND_ID
2809 case CTRL_X_PATH_PATTERNS:
2810 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002811 find_pattern_in_path(compl_pattern, dir,
2812 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002814 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2816 (linenr_T)1, (linenr_T)MAXLNUM);
2817 break;
2818#endif
2819
2820 case CTRL_X_DICTIONARY:
2821 case CTRL_X_THESAURUS:
2822 ins_compl_dictionaries(
2823 dict ? dict
2824 : (type == CTRL_X_THESAURUS
2825 ? (*curbuf->b_p_tsr == NUL
2826 ? p_tsr
2827 : curbuf->b_p_tsr)
2828 : (*curbuf->b_p_dict == NUL
2829 ? p_dict
2830 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002831 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
2833 dict = NULL;
2834 break;
2835
2836 case CTRL_X_TAGS:
2837 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
2838 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002839 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002842 * of matches is found when compl_pattern is empty */
2843 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
2845 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
2846 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
2847 {
2848 ins_compl_add_matches(num_matches, matches, dir);
2849 }
2850 p_ic = save_p_ic;
2851 break;
2852
2853 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002854 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2856 {
2857
2858 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002859 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 ins_compl_add_matches(num_matches, matches, dir);
2861 }
2862 break;
2863
2864 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002865 if (expand_cmdline(&compl_xp, compl_pattern,
2866 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 &num_matches, &matches) == EXPAND_OK)
2868 ins_compl_add_matches(num_matches, matches, dir);
2869 break;
2870
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002871#ifdef FEAT_COMPL_FUNC
2872 case CTRL_X_FUNCTION:
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002873 num_matches = expand_by_function(first_match_pos.col,
2874 compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002875 if (num_matches > 0)
2876 ins_compl_add_matches(num_matches, matches, dir);
2877 break;
2878#endif
2879
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002880 case CTRL_X_OCCULT:
2881 num_matches = expand_occult(first_match_pos.lnum,
2882 first_match_pos.col, compl_pattern, &matches);
2883 if (num_matches > 0)
2884 ins_compl_add_matches(num_matches, matches, dir);
2885 break;
2886
Bram Moolenaar488c6512005-08-11 20:09:58 +00002887 case CTRL_X_SPELL:
2888#ifdef FEAT_SYN_HL
2889 num_matches = expand_spelling(first_match_pos.lnum,
2890 first_match_pos.col, compl_pattern, &matches);
2891 if (num_matches > 0)
2892 ins_compl_add_matches(num_matches, matches, dir);
2893#endif
2894 break;
2895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 default: /* normal ^P/^N and ^X^L */
2897 /*
2898 * If 'infercase' is set, don't use 'smartcase' here
2899 */
2900 save_p_scs = p_scs;
2901 if (ins_buf->b_p_inf)
2902 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 /* buffers other than curbuf are scanned from the beginning or the
2905 * end but never from the middle, thus setting nowrapscan in this
2906 * buffers is a good idea, on the other hand, we always set
2907 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
2908 save_p_ws = p_ws;
2909 if (ins_buf != curbuf)
2910 p_ws = FALSE;
2911 else if (*e_cpt == '.')
2912 p_ws = TRUE;
2913 for (;;)
2914 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002915 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that has
2918 * added a word that was at the beginning of the line */
2919 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002920 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002922 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 else
2924 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002925 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002927 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002929 /* set compl_started even on fail */
2930 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 first_match_pos = *pos;
2932 last_match_pos = *pos;
2933 }
2934 else if (first_match_pos.lnum == last_match_pos.lnum
2935 && first_match_pos.col == last_match_pos.col)
2936 found_new_match = FAIL;
2937 if (found_new_match == FAIL)
2938 {
2939 if (ins_buf == curbuf)
2940 found_all = TRUE;
2941 break;
2942 }
2943
2944 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002945 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 && ini->lnum == pos->lnum
2947 && ini->col == pos->col)
2948 continue;
2949 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
2950 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2951 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002952 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
2954 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
2955 continue;
2956 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2957 if (!p_paste)
2958 ptr = skipwhite(ptr);
2959 }
2960 len = (int)STRLEN(ptr);
2961 }
2962 else
2963 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002964 char_u *tmp_ptr = ptr;
2965
2966 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002968 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /* Skip if already inside a word. */
2970 if (vim_iswordp(tmp_ptr))
2971 continue;
2972 /* Find start of next word. */
2973 tmp_ptr = find_word_start(tmp_ptr);
2974 }
2975 /* Find end of this word. */
2976 tmp_ptr = find_word_end(tmp_ptr);
2977 len = (int)(tmp_ptr - ptr);
2978
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002979 if ((compl_cont_status & CONT_ADDING)
2980 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
2982 if (pos->lnum < ins_buf->b_ml.ml_line_count)
2983 {
2984 /* Try next line, if any. the new word will be
2985 * "join" as if the normal command "J" was used.
2986 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002987 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 * works -- Acevedo */
2989 STRNCPY(IObuff, ptr, len);
2990 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2991 tmp_ptr = ptr = skipwhite(ptr);
2992 /* Find start of next word. */
2993 tmp_ptr = find_word_start(tmp_ptr);
2994 /* Find end of next word. */
2995 tmp_ptr = find_word_end(tmp_ptr);
2996 if (tmp_ptr > ptr)
2997 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002998 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003000 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 IObuff[len++] = ' ';
3002 /* IObuf =~ "\k.* ", thus len >= 2 */
3003 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003004 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 || (vim_strchr(p_cpo, CPO_JOINSP)
3006 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003007 && (IObuff[len - 2] == '?'
3008 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 IObuff[len++] = ' ';
3010 }
3011 /* copy as much as posible of the new word */
3012 if (tmp_ptr - ptr >= IOSIZE - len)
3013 tmp_ptr = ptr + IOSIZE - len - 1;
3014 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3015 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003016 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 IObuff[len] = NUL;
3019 ptr = IObuff;
3020 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003021 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 continue;
3023 }
3024 }
3025 if (ins_compl_add_infercase(ptr, len,
3026 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003027 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
3029 found_new_match = OK;
3030 break;
3031 }
3032 }
3033 p_scs = save_p_scs;
3034 p_ws = save_p_ws;
3035 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003036 /* check if compl_curr_match has changed, (e.g. other type of
3037 * expansion added somenthing) */
3038 if (compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 found_new_match = OK;
3040
3041 /* break the loop for specialized modes (use 'complete' just for the
3042 * generic ctrl_x_mode == 0) or when we've found a new match */
3043 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003044 || found_new_match != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 break;
3046
3047 /* Mark a buffer scanned when it has been scanned completely */
3048 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3049 ins_buf->b_scanned = TRUE;
3050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003051 compl_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003053 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3056 && *e_cpt == NUL) /* Got to end of 'complete' */
3057 found_new_match = FAIL;
3058
3059 i = -1; /* total of matches, unknown */
3060 if (found_new_match == FAIL
3061 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3062 i = ins_compl_make_cyclic();
3063
3064 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003065 * just been made cyclic then we have to move compl_curr_match to the next
3066 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003067 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003068 if (compl_curr_match == NULL)
3069 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 return i;
3071}
3072
3073/* Delete the old text being completed. */
3074 static void
3075ins_compl_delete()
3076{
3077 int i;
3078
3079 /*
3080 * In insert mode: Delete the typed part.
3081 * In replace mode: Put the old characters back, if any.
3082 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003083 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 backspace_until_column(i);
3085 changed_cline_bef_curs();
3086}
3087
3088/* Insert the new text being completed. */
3089 static void
3090ins_compl_insert()
3091{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003092 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093}
3094
3095/*
3096 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003097 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3098 * get more completions. If it is FALSE, then we just do nothing when there
3099 * are no more completions in a given direction. The latter case is used when
3100 * we are still in the middle of finding completions, to allow browsing
3101 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * Return the total number of matches, or -1 if still unknown -- webb.
3103 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003104 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3105 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 *
3107 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003108 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3109 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
3111 static int
3112ins_compl_next(allow_get_expansion)
3113 int allow_get_expansion;
3114{
3115 int num_matches = -1;
3116 int i;
3117
3118 if (allow_get_expansion)
3119 {
3120 /* Delete old text to be replaced */
3121 ins_compl_delete();
3122 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003123 compl_pending = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003124 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3125 compl_shown_match = compl_shown_match->cp_next;
3126 else if (compl_shows_dir == BACKWARD && compl_shown_match->cp_prev != NULL)
3127 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 else
3129 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003130 compl_pending = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (allow_get_expansion)
3132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003133 num_matches = ins_compl_get_exp(&compl_startpos,
3134 compl_direction);
3135 if (compl_pending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003137 if (compl_direction == compl_shows_dir)
3138 compl_shown_match = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
3140 }
3141 else
3142 return -1;
3143 }
3144
3145 /* Insert the text of the new completion */
3146 ins_compl_insert();
3147
3148 if (!allow_get_expansion)
3149 {
3150 /* Display the current match. */
3151 update_screen(0);
3152
3153 /* Delete old text to be replaced, since we're still searching and
3154 * don't want to match ourselves! */
3155 ins_compl_delete();
3156 }
3157
3158 /*
3159 * Show the file name for the match (if any)
3160 * Truncate the file name to avoid a wait for return.
3161 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003162 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003165 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (i <= 0)
3167 i = 0;
3168 else
3169 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003170 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 msg(IObuff);
3172 redraw_cmdline = FALSE; /* don't overwrite! */
3173 }
3174
3175 return num_matches;
3176}
3177
3178/*
3179 * Call this while finding completions, to check whether the user has hit a key
3180 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003181 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003183 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 */
3185 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003186ins_compl_check_keys(frequency)
3187 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 static int count = 0;
3190
3191 int c;
3192
3193 /* Don't check when reading keys from a script. That would break the test
3194 * scripts */
3195 if (using_script())
3196 return;
3197
3198 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003199 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return;
3201 count = 0;
3202
3203 ++no_mapping;
3204 c = vpeekc_any();
3205 --no_mapping;
3206 if (c != NUL)
3207 {
3208 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3209 {
3210 c = safe_vgetc(); /* Eat the character */
3211 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003212 compl_shows_dir = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003214 compl_shows_dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 (void)ins_compl_next(FALSE);
3216 }
3217 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003218 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003220 if (compl_pending && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 (void)ins_compl_next(FALSE);
3222}
3223
3224/*
3225 * Do Insert mode completion.
3226 * Called when character "c" was typed, which has a meaning for completion.
3227 * Returns OK if completion was done, FAIL if something failed (out of mem).
3228 */
3229 static int
3230ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003231 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003233 char_u *line;
3234 int startcol = 0; /* column where searched text starts */
3235 colnr_T curs_col; /* cursor column */
3236 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003239 compl_direction = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003241 compl_direction = FORWARD;
3242 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
3244 /* First time we hit ^N or ^P (in a row, I mean) */
3245
3246 /* Turn off 'sm' so we don't show matches with ^X^L */
3247 save_sm = p_sm;
3248 p_sm = FALSE;
3249
3250 did_ai = FALSE;
3251#ifdef FEAT_SMARTINDENT
3252 did_si = FALSE;
3253 can_si = FALSE;
3254 can_si_back = FALSE;
3255#endif
3256 if (stop_arrow() == FAIL)
3257 return FAIL;
3258
3259 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003260 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003263 * "compl_startpos" to the cursor as a pattern to add a new word
3264 * instead of expand the one before the cursor, in word-wise if
3265 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * is not in the same line as the cursor then fix it (the line has
3267 * been split because it was longer than 'tw'). if SOL is set then
3268 * skip the previous pattern, a word at the beginning of the line has
3269 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003270 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 /*
3273 * it is a continued search
3274 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003275 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3277 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3278 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003279 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003281 /* line (probably) wrapped, set compl_startpos to the
3282 * first non_blank in the line, if it is not a wordchar
3283 * include it to get a better pattern, but then we don't
3284 * want the "\\<" prefix, check it bellow */
3285 compl_col = (colnr_T)(skipwhite(line) - line);
3286 compl_startpos.col = compl_col;
3287 compl_startpos.lnum = curwin->w_cursor.lnum;
3288 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290 else
3291 {
3292 /* S_IPOS was set when we inserted a word that was at the
3293 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003294 * mode but first we need to redefine compl_startpos */
3295 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003297 compl_cont_status |= CONT_SOL;
3298 compl_startpos.col = (colnr_T)(skipwhite(
3299 line + compl_length
3300 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003302 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003304 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 /* IObuf is used to add a "word from the next line" would we
3306 * have enough space? just being paranoic */
3307#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003308 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003310 compl_cont_status &= ~CONT_SOL;
3311 compl_length = (IOSIZE - MIN_SPACE);
3312 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003314 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3315 if (compl_length < 1)
3316 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003319 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003321 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003324 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003328 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003330 compl_cont_status = 0;
3331 compl_cont_status |= CONT_N_ADDS;
3332 compl_startpos = curwin->w_cursor;
3333 startcol = (int)curs_col;
3334 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336
3337 /* Work out completion pattern and original text -- webb */
3338 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3339 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003340 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3342 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003345 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003347 compl_col += ++startcol;
3348 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003351 compl_pattern = str_foldcase(line + compl_col,
3352 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003354 compl_pattern = vim_strnsave(line + compl_col,
3355 compl_length);
3356 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 return FAIL;
3358 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003359 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 char_u *prefix = (char_u *)"\\<";
3362
3363 /* we need 3 extra chars, 1 for the NUL and
3364 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003365 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3366 compl_length) + 3);
3367 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003369 if (!vim_iswordp(line + compl_col)
3370 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 && (
3372#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003373 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003375 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
3377 )))
3378 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003379 STRCPY((char *)compl_pattern, prefix);
3380 (void)quote_meta(compl_pattern + STRLEN(prefix),
3381 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003383 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003385 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003387 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#endif
3389 )
3390 {
3391 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003392 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3393 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 compl_col += curs_col;
3396 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 else
3399 {
3400#ifdef FEAT_MBYTE
3401 /* Search the point of change class of multibyte character
3402 * or not a word single byte character backward. */
3403 if (has_mbyte)
3404 {
3405 int base_class;
3406 int head_off;
3407
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408 startcol -= (*mb_head_off)(line, line + startcol);
3409 base_class = mb_get_class(line + startcol);
3410 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003412 head_off = (*mb_head_off)(line, line + startcol);
3413 if (base_class != mb_get_class(line + startcol
3414 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003416 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418 }
3419 else
3420#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003423 compl_col += ++startcol;
3424 compl_length = (int)curs_col - startcol;
3425 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 /* Only match word with at least two chars -- webb
3428 * there's no need to call quote_meta,
3429 * alloc(7) is enough -- Acevedo
3430 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003431 compl_pattern = alloc(7);
3432 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003434 STRCPY((char *)compl_pattern, "\\<");
3435 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3436 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 else
3439 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003440 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3441 compl_length) + 3);
3442 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003444 STRCPY((char *)compl_pattern, "\\<");
3445 (void)quote_meta(compl_pattern + 2, line + compl_col,
3446 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 }
3449 }
3450 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3451 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003452 compl_col = skipwhite(line) - line;
3453 compl_length = (int)curs_col - (int)compl_col;
3454 if (compl_length < 0) /* cursor in indent: empty pattern */
3455 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003457 compl_pattern = str_foldcase(line + compl_col, compl_length,
3458 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003460 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3461 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 return FAIL;
3463 }
3464 else if (ctrl_x_mode == CTRL_X_FILES)
3465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003466 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003468 compl_col += ++startcol;
3469 compl_length = (int)curs_col - startcol;
3470 compl_pattern = addstar(line + compl_col, compl_length,
3471 EXPAND_FILES);
3472 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return FAIL;
3474 }
3475 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3476 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003477 compl_pattern = vim_strnsave(line, curs_col);
3478 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003480 set_cmd_context(&compl_xp, compl_pattern,
3481 (int)STRLEN(compl_pattern), curs_col);
3482 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3483 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003485 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3486 compl_col = startcol;
3487 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003489#ifdef FEAT_COMPL_FUNC
3490 else if (ctrl_x_mode == CTRL_X_FUNCTION)
3491 {
3492 /*
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003493 * Call user defined function 'completefunc' with "a:findstart" is
3494 * 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003495 */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003496 char_u colbuf[30];
3497 char_u *args[3];
3498 int col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003499
3500 /* Call 'completefunc' and get pattern length as a string */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003501 if (*curbuf->b_p_cfu == NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003502 return FAIL;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003503
3504 args[0] = (char_u *)"1";
3505 sprintf((char *)colbuf, "%d", (int)curs_col);
3506 args[1] = colbuf;
3507 args[2] = NULL;
3508
3509 col = call_func_retnr(curbuf->b_p_cfu, 3, args, FALSE);
3510 if (col < 0)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003511 return FAIL;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003512 compl_col = col;
3513 if ((colnr_T)compl_col > curs_col)
3514 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003515
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003516 /* Setup variables for completion. Need to obtain "line" again,
3517 * it may have become invalid. */
3518 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003519 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3521 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003522 return FAIL;
3523 }
3524#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003525 else if (ctrl_x_mode == CTRL_X_OCCULT)
3526 {
3527 /* TODO: let language-specific function handle locating the text
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003528 * to be completed. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003529 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3530 ;
3531 compl_col += ++startcol;
3532 compl_length = (int)curs_col - startcol;
3533 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3534 if (compl_pattern == NULL)
3535 return FAIL;
3536 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003537 else if (ctrl_x_mode == CTRL_X_SPELL)
3538 {
3539#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003540 if (spell_bad_len > 0)
3541 compl_col = curs_col - spell_bad_len;
3542 else
3543 compl_col = spell_word_start(startcol);
3544 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003545 return FAIL;
3546 compl_length = (int)curs_col - compl_col;
3547 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3548 if (compl_pattern == NULL)
3549#endif
3550 return FAIL;
3551 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 else
3553 {
3554 EMSG2(_(e_intern2), "ins_complete()");
3555 return FAIL;
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003558 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
3560 edit_submode_pre = (char_u *)_(" Adding");
3561 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3562 {
3563 /* Insert a new line, keep indentation but ignore 'comments' */
3564#ifdef FEAT_COMMENTS
3565 char_u *old = curbuf->b_p_com;
3566
3567 curbuf->b_p_com = (char_u *)"";
3568#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569 compl_startpos.lnum = curwin->w_cursor.lnum;
3570 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 ins_eol('\r');
3572#ifdef FEAT_COMMENTS
3573 curbuf->b_p_com = old;
3574#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003575 compl_length = 0;
3576 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 }
3579 else
3580 {
3581 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003585 if (compl_cont_status & CONT_LOCAL)
3586 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else
3588 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 * "compl_orig_text" itself (not a copy) is added, it will be freed
3592 * when the list of matches is freed. */
3593 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3594 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3595 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003597 vim_free(compl_pattern);
3598 compl_pattern = NULL;
3599 vim_free(compl_orig_text);
3600 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return FAIL;
3602 }
3603
3604 /* showmode might reset the internal line pointers, so it must
3605 * be called before line = ml_get(), or when this address is no
3606 * longer needed. -- Acevedo.
3607 */
3608 edit_submode_extra = (char_u *)_("-- Searching...");
3609 edit_submode_highl = HLF_COUNT;
3610 showmode();
3611 edit_submode_extra = NULL;
3612 out_flush();
3613 }
3614
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003615 compl_shown_match = compl_curr_match;
3616 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 /*
3619 * Find next match.
3620 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 n = ins_compl_next(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003623 if (n > 1) /* all matches have been found */
3624 compl_matches = n;
3625 compl_curr_match = compl_shown_match;
3626 compl_direction = compl_shows_dir;
3627 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629 /* eat the ESC to avoid leaving insert mode */
3630 if (got_int && !global_busy)
3631 {
3632 (void)vgetc();
3633 got_int = FALSE;
3634 }
3635
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003637 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003639 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3640 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3642 edit_submode_highl = HLF_E;
3643 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3644 * because we couldn't expand anything at first place, but if we used
3645 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3646 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003647 if ( compl_length > 1
3648 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 || (ctrl_x_mode != 0
3650 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3651 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003652 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654
Bram Moolenaar572cb562005-08-05 21:35:02 +00003655 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003658 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 if (edit_submode_extra == NULL)
3661 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003662 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 edit_submode_extra = (char_u *)_("Back at original");
3665 edit_submode_highl = HLF_W;
3666 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003667 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 edit_submode_extra = (char_u *)_("Word from other line");
3670 edit_submode_highl = HLF_COUNT;
3671 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003672 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 edit_submode_extra = (char_u *)_("The only match");
3675 edit_submode_highl = HLF_COUNT;
3676 }
3677 else
3678 {
3679 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003680 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003682 int number = 0;
3683 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003685 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 /* search backwards for the first valid (!= -1) number.
3688 * This should normally succeed already at the first loop
3689 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003690 for (match = compl_curr_match->cp_prev; match != NULL
3691 && match != compl_first_match;
3692 match = match->cp_prev)
3693 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003695 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 break;
3697 }
3698 if (match != NULL)
3699 /* go up and assign all numbers which are not assigned
3700 * yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003701 for (match = match->cp_next; match
3702 && match->cp_number == -1;
3703 match = match->cp_next)
3704 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 else /* BACKWARD */
3707 {
3708 /* search forwards (upwards) for the first valid (!= -1)
3709 * number. This should normally succeed already at the
3710 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003711 for (match = compl_curr_match->cp_next; match != NULL
3712 && match != compl_first_match;
3713 match = match->cp_next)
3714 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003716 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 break;
3718 }
3719 if (match != NULL)
3720 /* go down and assign all numbers which are not
3721 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003722 for (match = match->cp_prev; match
3723 && match->cp_number == -1;
3724 match = match->cp_prev)
3725 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 }
3728
3729 /* The match should always have a sequnce number now, this is just
3730 * a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003731 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
3733 /* Space for 10 text chars. + 2x10-digit no.s */
3734 static char_u match_ref[31];
3735
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003736 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003738 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003741 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003742 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 edit_submode_extra = match_ref;
3744 edit_submode_highl = HLF_R;
3745 if (dollar_vcol)
3746 curs_columns(FALSE);
3747 }
3748 }
3749 }
3750
3751 /* Show a message about what (completion) mode we're in. */
3752 showmode();
3753 if (edit_submode_extra != NULL)
3754 {
3755 if (!p_smd)
3756 msg_attr(edit_submode_extra,
3757 edit_submode_highl < HLF_COUNT
3758 ? hl_attr(edit_submode_highl) : 0);
3759 }
3760 else
3761 msg_clr_cmdline(); /* necessary for "noshowmode" */
3762
3763 return OK;
3764}
3765
3766/*
3767 * Looks in the first "len" chars. of "src" for search-metachars.
3768 * If dest is not NULL the chars. are copied there quoting (with
3769 * a backslash) the metachars, and dest would be NUL terminated.
3770 * Returns the length (needed) of dest
3771 */
3772 static int
3773quote_meta(dest, src, len)
3774 char_u *dest;
3775 char_u *src;
3776 int len;
3777{
3778 int m;
3779
3780 for (m = len; --len >= 0; src++)
3781 {
3782 switch (*src)
3783 {
3784 case '.':
3785 case '*':
3786 case '[':
3787 if (ctrl_x_mode == CTRL_X_DICTIONARY
3788 || ctrl_x_mode == CTRL_X_THESAURUS)
3789 break;
3790 case '~':
3791 if (!p_magic) /* quote these only if magic is set */
3792 break;
3793 case '\\':
3794 if (ctrl_x_mode == CTRL_X_DICTIONARY
3795 || ctrl_x_mode == CTRL_X_THESAURUS)
3796 break;
3797 case '^': /* currently it's not needed. */
3798 case '$':
3799 m++;
3800 if (dest != NULL)
3801 *dest++ = '\\';
3802 break;
3803 }
3804 if (dest != NULL)
3805 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003806# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /* Copy remaining bytes of a multibyte character. */
3808 if (has_mbyte)
3809 {
3810 int i, mb_len;
3811
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003812 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (mb_len > 0 && len >= mb_len)
3814 for (i = 0; i < mb_len; ++i)
3815 {
3816 --len;
3817 ++src;
3818 if (dest != NULL)
3819 *dest++ = *src;
3820 }
3821 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824 if (dest != NULL)
3825 *dest = NUL;
3826
3827 return m;
3828}
3829#endif /* FEAT_INS_EXPAND */
3830
3831/*
3832 * Next character is interpreted literally.
3833 * A one, two or three digit decimal number is interpreted as its byte value.
3834 * If one or two digits are entered, the next character is given to vungetc().
3835 * For Unicode a character > 255 may be returned.
3836 */
3837 int
3838get_literal()
3839{
3840 int cc;
3841 int nc;
3842 int i;
3843 int hex = FALSE;
3844 int octal = FALSE;
3845#ifdef FEAT_MBYTE
3846 int unicode = 0;
3847#endif
3848
3849 if (got_int)
3850 return Ctrl_C;
3851
3852#ifdef FEAT_GUI
3853 /*
3854 * In GUI there is no point inserting the internal code for a special key.
3855 * It is more useful to insert the string "<KEY>" instead. This would
3856 * probably be useful in a text window too, but it would not be
3857 * vi-compatible (maybe there should be an option for it?) -- webb
3858 */
3859 if (gui.in_use)
3860 ++allow_keys;
3861#endif
3862#ifdef USE_ON_FLY_SCROLL
3863 dont_scroll = TRUE; /* disallow scrolling here */
3864#endif
3865 ++no_mapping; /* don't map the next key hits */
3866 cc = 0;
3867 i = 0;
3868 for (;;)
3869 {
3870 do
3871 nc = safe_vgetc();
3872 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
3873 || nc == K_HOR_SCROLLBAR);
3874#ifdef FEAT_CMDL_INFO
3875 if (!(State & CMDLINE)
3876# ifdef FEAT_MBYTE
3877 && MB_BYTE2LEN_CHECK(nc) == 1
3878# endif
3879 )
3880 add_to_showcmd(nc);
3881#endif
3882 if (nc == 'x' || nc == 'X')
3883 hex = TRUE;
3884 else if (nc == 'o' || nc == 'O')
3885 octal = TRUE;
3886#ifdef FEAT_MBYTE
3887 else if (nc == 'u' || nc == 'U')
3888 unicode = nc;
3889#endif
3890 else
3891 {
3892 if (hex
3893#ifdef FEAT_MBYTE
3894 || unicode != 0
3895#endif
3896 )
3897 {
3898 if (!vim_isxdigit(nc))
3899 break;
3900 cc = cc * 16 + hex2nr(nc);
3901 }
3902 else if (octal)
3903 {
3904 if (nc < '0' || nc > '7')
3905 break;
3906 cc = cc * 8 + nc - '0';
3907 }
3908 else
3909 {
3910 if (!VIM_ISDIGIT(nc))
3911 break;
3912 cc = cc * 10 + nc - '0';
3913 }
3914
3915 ++i;
3916 }
3917
3918 if (cc > 255
3919#ifdef FEAT_MBYTE
3920 && unicode == 0
3921#endif
3922 )
3923 cc = 255; /* limit range to 0-255 */
3924 nc = 0;
3925
3926 if (hex) /* hex: up to two chars */
3927 {
3928 if (i >= 2)
3929 break;
3930 }
3931#ifdef FEAT_MBYTE
3932 else if (unicode) /* Unicode: up to four or eight chars */
3933 {
3934 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
3935 break;
3936 }
3937#endif
3938 else if (i >= 3) /* decimal or octal: up to three chars */
3939 break;
3940 }
3941 if (i == 0) /* no number entered */
3942 {
3943 if (nc == K_ZERO) /* NUL is stored as NL */
3944 {
3945 cc = '\n';
3946 nc = 0;
3947 }
3948 else
3949 {
3950 cc = nc;
3951 nc = 0;
3952 }
3953 }
3954
3955 if (cc == 0) /* NUL is stored as NL */
3956 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00003957#ifdef FEAT_MBYTE
3958 if (enc_dbcs && (cc & 0xff) == 0)
3959 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
3960 second byte will cause trouble! */
3961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 --no_mapping;
3964#ifdef FEAT_GUI
3965 if (gui.in_use)
3966 --allow_keys;
3967#endif
3968 if (nc)
3969 vungetc(nc);
3970 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
3971 return cc;
3972}
3973
3974/*
3975 * Insert character, taking care of special keys and mod_mask
3976 */
3977 static void
3978insert_special(c, allow_modmask, ctrlv)
3979 int c;
3980 int allow_modmask;
3981 int ctrlv; /* c was typed after CTRL-V */
3982{
3983 char_u *p;
3984 int len;
3985
3986 /*
3987 * Special function key, translate into "<Key>". Up to the last '>' is
3988 * inserted with ins_str(), so as not to replace characters in replace
3989 * mode.
3990 * Only use mod_mask for special keys, to avoid things like <S-Space>,
3991 * unless 'allow_modmask' is TRUE.
3992 */
3993#ifdef MACOS
3994 /* Command-key never produces a normal key */
3995 if (mod_mask & MOD_MASK_CMD)
3996 allow_modmask = TRUE;
3997#endif
3998 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
3999 {
4000 p = get_special_key_name(c, mod_mask);
4001 len = (int)STRLEN(p);
4002 c = p[len - 1];
4003 if (len > 2)
4004 {
4005 if (stop_arrow() == FAIL)
4006 return;
4007 p[len - 1] = NUL;
4008 ins_str(p);
4009 AppendToRedobuffLit(p);
4010 ctrlv = FALSE;
4011 }
4012 }
4013 if (stop_arrow() == OK)
4014 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4015}
4016
4017/*
4018 * Special characters in this context are those that need processing other
4019 * than the simple insertion that can be performed here. This includes ESC
4020 * which terminates the insert, and CR/NL which need special processing to
4021 * open up a new line. This routine tries to optimize insertions performed by
4022 * the "redo", "undo" or "put" commands, so it needs to know when it should
4023 * stop and defer processing to the "normal" mechanism.
4024 * '0' and '^' are special, because they can be followed by CTRL-D.
4025 */
4026#ifdef EBCDIC
4027# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4028#else
4029# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4030#endif
4031
4032#ifdef FEAT_MBYTE
4033# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4034#else
4035# define WHITECHAR(cc) vim_iswhite(cc)
4036#endif
4037
4038 void
4039insertchar(c, flags, second_indent)
4040 int c; /* character to insert or NUL */
4041 int flags; /* INSCHAR_FORMAT, etc. */
4042 int second_indent; /* indent for second line if >= 0 */
4043{
4044 int haveto_redraw = FALSE;
4045 int textwidth;
4046#ifdef FEAT_COMMENTS
4047 colnr_T leader_len;
4048 char_u *p;
4049 int no_leader = FALSE;
4050 int do_comments = (flags & INSCHAR_DO_COM);
4051#endif
4052 int fo_white_par;
4053 int first_line = TRUE;
4054 int fo_ins_blank;
4055#ifdef FEAT_MBYTE
4056 int fo_multibyte;
4057#endif
4058 int save_char = NUL;
4059 int cc;
4060
4061 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4062 fo_ins_blank = has_format_option(FO_INS_BLANK);
4063#ifdef FEAT_MBYTE
4064 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4065#endif
4066 fo_white_par = has_format_option(FO_WHITE_PAR);
4067
4068 /*
4069 * Try to break the line in two or more pieces when:
4070 * - Always do this if we have been called to do formatting only.
4071 * - Always do this when 'formatoptions' has the 'a' flag and the line
4072 * ends in white space.
4073 * - Otherwise:
4074 * - Don't do this if inserting a blank
4075 * - Don't do this if an existing character is being replaced, unless
4076 * we're in VREPLACE mode.
4077 * - Do this if the cursor is not on the line where insert started
4078 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4079 * before the insert.
4080 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4081 * before 'textwidth'
4082 */
4083 if (textwidth
4084 && ((flags & INSCHAR_FORMAT)
4085 || (!vim_iswhite(c)
4086 && !((State & REPLACE_FLAG)
4087#ifdef FEAT_VREPLACE
4088 && !(State & VREPLACE_FLAG)
4089#endif
4090 && *ml_get_cursor() != NUL)
4091 && (curwin->w_cursor.lnum != Insstart.lnum
4092 || ((!has_format_option(FO_INS_LONG)
4093 || Insstart_textlen <= (colnr_T)textwidth)
4094 && (!fo_ins_blank
4095 || Insstart_blank_vcol <= (colnr_T)textwidth
4096 ))))))
4097 {
4098 /*
4099 * When 'ai' is off we don't want a space under the cursor to be
4100 * deleted. Replace it with an 'x' temporarily.
4101 */
4102 if (!curbuf->b_p_ai)
4103 {
4104 cc = gchar_cursor();
4105 if (vim_iswhite(cc))
4106 {
4107 save_char = cc;
4108 pchar_cursor('x');
4109 }
4110 }
4111
4112 /*
4113 * Repeat breaking lines, until the current line is not too long.
4114 */
4115 while (!got_int)
4116 {
4117 int startcol; /* Cursor column at entry */
4118 int wantcol; /* column at textwidth border */
4119 int foundcol; /* column for start of spaces */
4120 int end_foundcol = 0; /* column for start of word */
4121 colnr_T len;
4122 colnr_T virtcol;
4123#ifdef FEAT_VREPLACE
4124 int orig_col = 0;
4125 char_u *saved_text = NULL;
4126#endif
4127 colnr_T col;
4128
4129 virtcol = get_nolist_virtcol();
4130 if (virtcol < (colnr_T)textwidth)
4131 break;
4132
4133#ifdef FEAT_COMMENTS
4134 if (no_leader)
4135 do_comments = FALSE;
4136 else if (!(flags & INSCHAR_FORMAT)
4137 && has_format_option(FO_WRAP_COMS))
4138 do_comments = TRUE;
4139
4140 /* Don't break until after the comment leader */
4141 if (do_comments)
4142 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4143 else
4144 leader_len = 0;
4145
4146 /* If the line doesn't start with a comment leader, then don't
4147 * start one in a following broken line. Avoids that a %word
4148 * moved to the start of the next line causes all following lines
4149 * to start with %. */
4150 if (leader_len == 0)
4151 no_leader = TRUE;
4152#endif
4153 if (!(flags & INSCHAR_FORMAT)
4154#ifdef FEAT_COMMENTS
4155 && leader_len == 0
4156#endif
4157 && !has_format_option(FO_WRAP))
4158
4159 {
4160 textwidth = 0;
4161 break;
4162 }
4163 if ((startcol = curwin->w_cursor.col) == 0)
4164 break;
4165
4166 /* find column of textwidth border */
4167 coladvance((colnr_T)textwidth);
4168 wantcol = curwin->w_cursor.col;
4169
4170 curwin->w_cursor.col = startcol - 1;
4171#ifdef FEAT_MBYTE
4172 /* Correct cursor for multi-byte character. */
4173 if (has_mbyte)
4174 mb_adjust_cursor();
4175#endif
4176 foundcol = 0;
4177
4178 /*
4179 * Find position to break at.
4180 * Stop at first entered white when 'formatoptions' has 'v'
4181 */
4182 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4183 || curwin->w_cursor.lnum != Insstart.lnum
4184 || curwin->w_cursor.col >= Insstart.col)
4185 {
4186 cc = gchar_cursor();
4187 if (WHITECHAR(cc))
4188 {
4189 /* remember position of blank just before text */
4190 end_foundcol = curwin->w_cursor.col;
4191
4192 /* find start of sequence of blanks */
4193 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4194 {
4195 dec_cursor();
4196 cc = gchar_cursor();
4197 }
4198 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4199 break; /* only spaces in front of text */
4200#ifdef FEAT_COMMENTS
4201 /* Don't break until after the comment leader */
4202 if (curwin->w_cursor.col < leader_len)
4203 break;
4204#endif
4205 if (has_format_option(FO_ONE_LETTER))
4206 {
4207 /* do not break after one-letter words */
4208 if (curwin->w_cursor.col == 0)
4209 break; /* one-letter word at begin */
4210
4211 col = curwin->w_cursor.col;
4212 dec_cursor();
4213 cc = gchar_cursor();
4214
4215 if (WHITECHAR(cc))
4216 continue; /* one-letter, continue */
4217 curwin->w_cursor.col = col;
4218 }
4219#ifdef FEAT_MBYTE
4220 if (has_mbyte)
4221 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004222 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 else
4224#endif
4225 foundcol = curwin->w_cursor.col + 1;
4226 if (curwin->w_cursor.col < (colnr_T)wantcol)
4227 break;
4228 }
4229#ifdef FEAT_MBYTE
4230 else if (cc >= 0x100 && fo_multibyte
4231 && curwin->w_cursor.col <= (colnr_T)wantcol)
4232 {
4233 /* Break after or before a multi-byte character. */
4234 foundcol = curwin->w_cursor.col;
4235 if (curwin->w_cursor.col < (colnr_T)wantcol)
4236 foundcol += (*mb_char2len)(cc);
4237 end_foundcol = foundcol;
4238 break;
4239 }
4240#endif
4241 if (curwin->w_cursor.col == 0)
4242 break;
4243 dec_cursor();
4244 }
4245
4246 if (foundcol == 0) /* no spaces, cannot break line */
4247 {
4248 curwin->w_cursor.col = startcol;
4249 break;
4250 }
4251
4252 /* Going to break the line, remove any "$" now. */
4253 undisplay_dollar();
4254
4255 /*
4256 * Offset between cursor position and line break is used by replace
4257 * stack functions. VREPLACE does not use this, and backspaces
4258 * over the text instead.
4259 */
4260#ifdef FEAT_VREPLACE
4261 if (State & VREPLACE_FLAG)
4262 orig_col = startcol; /* Will start backspacing from here */
4263 else
4264#endif
4265 replace_offset = startcol - end_foundcol - 1;
4266
4267 /*
4268 * adjust startcol for spaces that will be deleted and
4269 * characters that will remain on top line
4270 */
4271 curwin->w_cursor.col = foundcol;
4272 while (cc = gchar_cursor(), WHITECHAR(cc))
4273 inc_cursor();
4274 startcol -= curwin->w_cursor.col;
4275 if (startcol < 0)
4276 startcol = 0;
4277
4278#ifdef FEAT_VREPLACE
4279 if (State & VREPLACE_FLAG)
4280 {
4281 /*
4282 * In VREPLACE mode, we will backspace over the text to be
4283 * wrapped, so save a copy now to put on the next line.
4284 */
4285 saved_text = vim_strsave(ml_get_cursor());
4286 curwin->w_cursor.col = orig_col;
4287 if (saved_text == NULL)
4288 break; /* Can't do it, out of memory */
4289 saved_text[startcol] = NUL;
4290
4291 /* Backspace over characters that will move to the next line */
4292 if (!fo_white_par)
4293 backspace_until_column(foundcol);
4294 }
4295 else
4296#endif
4297 {
4298 /* put cursor after pos. to break line */
4299 if (!fo_white_par)
4300 curwin->w_cursor.col = foundcol;
4301 }
4302
4303 /*
4304 * Split the line just before the margin.
4305 * Only insert/delete lines, but don't really redraw the window.
4306 */
4307 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4308 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4309#ifdef FEAT_COMMENTS
4310 + (do_comments ? OPENLINE_DO_COM : 0)
4311#endif
4312 , old_indent);
4313 old_indent = 0;
4314
4315 replace_offset = 0;
4316 if (first_line)
4317 {
4318 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4319 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4320 if (second_indent >= 0)
4321 {
4322#ifdef FEAT_VREPLACE
4323 if (State & VREPLACE_FLAG)
4324 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4325 else
4326#endif
4327 (void)set_indent(second_indent, SIN_CHANGED);
4328 }
4329 first_line = FALSE;
4330 }
4331
4332#ifdef FEAT_VREPLACE
4333 if (State & VREPLACE_FLAG)
4334 {
4335 /*
4336 * In VREPLACE mode we have backspaced over the text to be
4337 * moved, now we re-insert it into the new line.
4338 */
4339 ins_bytes(saved_text);
4340 vim_free(saved_text);
4341 }
4342 else
4343#endif
4344 {
4345 /*
4346 * Check if cursor is not past the NUL off the line, cindent
4347 * may have added or removed indent.
4348 */
4349 curwin->w_cursor.col += startcol;
4350 len = (colnr_T)STRLEN(ml_get_curline());
4351 if (curwin->w_cursor.col > len)
4352 curwin->w_cursor.col = len;
4353 }
4354
4355 haveto_redraw = TRUE;
4356#ifdef FEAT_CINDENT
4357 can_cindent = TRUE;
4358#endif
4359 /* moved the cursor, don't autoindent or cindent now */
4360 did_ai = FALSE;
4361#ifdef FEAT_SMARTINDENT
4362 did_si = FALSE;
4363 can_si = FALSE;
4364 can_si_back = FALSE;
4365#endif
4366 line_breakcheck();
4367 }
4368
4369 if (save_char) /* put back space after cursor */
4370 pchar_cursor(save_char);
4371
4372 if (c == NUL) /* formatting only */
4373 return;
4374 if (haveto_redraw)
4375 {
4376 update_topline();
4377 redraw_curbuf_later(VALID);
4378 }
4379 }
4380 if (c == NUL) /* only formatting was wanted */
4381 return;
4382
4383#ifdef FEAT_COMMENTS
4384 /* Check whether this character should end a comment. */
4385 if (did_ai && (int)c == end_comment_pending)
4386 {
4387 char_u *line;
4388 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4389 int middle_len, end_len;
4390 int i;
4391
4392 /*
4393 * Need to remove existing (middle) comment leader and insert end
4394 * comment leader. First, check what comment leader we can find.
4395 */
4396 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4397 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4398 {
4399 /* Skip middle-comment string */
4400 while (*p && p[-1] != ':') /* find end of middle flags */
4401 ++p;
4402 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4403 /* Don't count trailing white space for middle_len */
4404 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4405 --middle_len;
4406
4407 /* Find the end-comment string */
4408 while (*p && p[-1] != ':') /* find end of end flags */
4409 ++p;
4410 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4411
4412 /* Skip white space before the cursor */
4413 i = curwin->w_cursor.col;
4414 while (--i >= 0 && vim_iswhite(line[i]))
4415 ;
4416 i++;
4417
4418 /* Skip to before the middle leader */
4419 i -= middle_len;
4420
4421 /* Check some expected things before we go on */
4422 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4423 {
4424 /* Backspace over all the stuff we want to replace */
4425 backspace_until_column(i);
4426
4427 /*
4428 * Insert the end-comment string, except for the last
4429 * character, which will get inserted as normal later.
4430 */
4431 ins_bytes_len(lead_end, end_len - 1);
4432 }
4433 }
4434 }
4435 end_comment_pending = NUL;
4436#endif
4437
4438 did_ai = FALSE;
4439#ifdef FEAT_SMARTINDENT
4440 did_si = FALSE;
4441 can_si = FALSE;
4442 can_si_back = FALSE;
4443#endif
4444
4445 /*
4446 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4447 * This speeds up normal text input considerably.
4448 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4449 * need to re-indent at a ':', or any other character (but not what
4450 * 'paste' is set)..
4451 */
4452#ifdef USE_ON_FLY_SCROLL
4453 dont_scroll = FALSE; /* allow scrolling here */
4454#endif
4455
4456 if ( !ISSPECIAL(c)
4457#ifdef FEAT_MBYTE
4458 && (!has_mbyte || (*mb_char2len)(c) == 1)
4459#endif
4460 && vpeekc() != NUL
4461 && !(State & REPLACE_FLAG)
4462#ifdef FEAT_CINDENT
4463 && !cindent_on()
4464#endif
4465#ifdef FEAT_RIGHTLEFT
4466 && !p_ri
4467#endif
4468 )
4469 {
4470#define INPUT_BUFLEN 100
4471 char_u buf[INPUT_BUFLEN + 1];
4472 int i;
4473 colnr_T virtcol = 0;
4474
4475 buf[0] = c;
4476 i = 1;
4477 if (textwidth)
4478 virtcol = get_nolist_virtcol();
4479 /*
4480 * Stop the string when:
4481 * - no more chars available
4482 * - finding a special character (command key)
4483 * - buffer is full
4484 * - running into the 'textwidth' boundary
4485 * - need to check for abbreviation: A non-word char after a word-char
4486 */
4487 while ( (c = vpeekc()) != NUL
4488 && !ISSPECIAL(c)
4489#ifdef FEAT_MBYTE
4490 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4491#endif
4492 && i < INPUT_BUFLEN
4493 && (textwidth == 0
4494 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4495 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4496 {
4497#ifdef FEAT_RIGHTLEFT
4498 c = vgetc();
4499 if (p_hkmap && KeyTyped)
4500 c = hkmap(c); /* Hebrew mode mapping */
4501# ifdef FEAT_FKMAP
4502 if (p_fkmap && KeyTyped)
4503 c = fkmap(c); /* Farsi mode mapping */
4504# endif
4505 buf[i++] = c;
4506#else
4507 buf[i++] = vgetc();
4508#endif
4509 }
4510
4511#ifdef FEAT_DIGRAPHS
4512 do_digraph(-1); /* clear digraphs */
4513 do_digraph(buf[i-1]); /* may be the start of a digraph */
4514#endif
4515 buf[i] = NUL;
4516 ins_str(buf);
4517 if (flags & INSCHAR_CTRLV)
4518 {
4519 redo_literal(*buf);
4520 i = 1;
4521 }
4522 else
4523 i = 0;
4524 if (buf[i] != NUL)
4525 AppendToRedobuffLit(buf + i);
4526 }
4527 else
4528 {
4529#ifdef FEAT_MBYTE
4530 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4531 {
4532 char_u buf[MB_MAXBYTES + 1];
4533
4534 (*mb_char2bytes)(c, buf);
4535 buf[cc] = NUL;
4536 ins_char_bytes(buf, cc);
4537 AppendCharToRedobuff(c);
4538 }
4539 else
4540#endif
4541 {
4542 ins_char(c);
4543 if (flags & INSCHAR_CTRLV)
4544 redo_literal(c);
4545 else
4546 AppendCharToRedobuff(c);
4547 }
4548 }
4549}
4550
4551/*
4552 * Called after inserting or deleting text: When 'formatoptions' includes the
4553 * 'a' flag format from the current line until the end of the paragraph.
4554 * Keep the cursor at the same position relative to the text.
4555 * The caller must have saved the cursor line for undo, following ones will be
4556 * saved here.
4557 */
4558 void
4559auto_format(trailblank, prev_line)
4560 int trailblank; /* when TRUE also format with trailing blank */
4561 int prev_line; /* may start in previous line */
4562{
4563 pos_T pos;
4564 colnr_T len;
4565 char_u *old;
4566 char_u *new, *pnew;
4567 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004568 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 if (!has_format_option(FO_AUTO))
4571 return;
4572
4573 pos = curwin->w_cursor;
4574 old = ml_get_curline();
4575
4576 /* may remove added space */
4577 check_auto_format(FALSE);
4578
4579 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4580 * user might insert normal text next. Also skip formatting when "1" is
4581 * in 'formatoptions' and there is a single character before the cursor.
4582 * Otherwise the line would be broken and when typing another non-white
4583 * next they are not joined back together. */
4584 wasatend = (pos.col == STRLEN(old));
4585 if (*old != NUL && !trailblank && wasatend)
4586 {
4587 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004588 cc = gchar_cursor();
4589 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4590 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004592 cc = gchar_cursor();
4593 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
4595 curwin->w_cursor = pos;
4596 return;
4597 }
4598 curwin->w_cursor = pos;
4599 }
4600
4601#ifdef FEAT_COMMENTS
4602 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4603 * comments. */
4604 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4605 && get_leader_len(old, NULL, FALSE) == 0)
4606 return;
4607#endif
4608
4609 /*
4610 * May start formatting in a previous line, so that after "x" a word is
4611 * moved to the previous line if it fits there now. Only when this is not
4612 * the start of a paragraph.
4613 */
4614 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4615 {
4616 --curwin->w_cursor.lnum;
4617 if (u_save_cursor() == FAIL)
4618 return;
4619 }
4620
4621 /*
4622 * Do the formatting and restore the cursor position. "saved_cursor" will
4623 * be adjusted for the text formatting.
4624 */
4625 saved_cursor = pos;
4626 format_lines((linenr_T)-1);
4627 curwin->w_cursor = saved_cursor;
4628 saved_cursor.lnum = 0;
4629
4630 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4631 {
4632 /* "cannot happen" */
4633 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4634 coladvance((colnr_T)MAXCOL);
4635 }
4636 else
4637 check_cursor_col();
4638
4639 /* Insert mode: If the cursor is now after the end of the line while it
4640 * previously wasn't, the line was broken. Because of the rule above we
4641 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4642 * formatted. */
4643 if (!wasatend && has_format_option(FO_WHITE_PAR))
4644 {
4645 new = ml_get_curline();
4646 len = STRLEN(new);
4647 if (curwin->w_cursor.col == len)
4648 {
4649 pnew = vim_strnsave(new, len + 2);
4650 pnew[len] = ' ';
4651 pnew[len + 1] = NUL;
4652 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4653 /* remove the space later */
4654 did_add_space = TRUE;
4655 }
4656 else
4657 /* may remove added space */
4658 check_auto_format(FALSE);
4659 }
4660
4661 check_cursor();
4662}
4663
4664/*
4665 * When an extra space was added to continue a paragraph for auto-formatting,
4666 * delete it now. The space must be under the cursor, just after the insert
4667 * position.
4668 */
4669 static void
4670check_auto_format(end_insert)
4671 int end_insert; /* TRUE when ending Insert mode */
4672{
4673 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004674 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 if (did_add_space)
4677 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004678 cc = gchar_cursor();
4679 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 /* Somehow the space was removed already. */
4681 did_add_space = FALSE;
4682 else
4683 {
4684 if (!end_insert)
4685 {
4686 inc_cursor();
4687 c = gchar_cursor();
4688 dec_cursor();
4689 }
4690 if (c != NUL)
4691 {
4692 /* The space is no longer at the end of the line, delete it. */
4693 del_char(FALSE);
4694 did_add_space = FALSE;
4695 }
4696 }
4697 }
4698}
4699
4700/*
4701 * Find out textwidth to be used for formatting:
4702 * if 'textwidth' option is set, use it
4703 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4704 * if invalid value, use 0.
4705 * Set default to window width (maximum 79) for "gq" operator.
4706 */
4707 int
4708comp_textwidth(ff)
4709 int ff; /* force formatting (for "Q" command) */
4710{
4711 int textwidth;
4712
4713 textwidth = curbuf->b_p_tw;
4714 if (textwidth == 0 && curbuf->b_p_wm)
4715 {
4716 /* The width is the window width minus 'wrapmargin' minus all the
4717 * things that add to the margin. */
4718 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4719#ifdef FEAT_CMDWIN
4720 if (cmdwin_type != 0)
4721 textwidth -= 1;
4722#endif
4723#ifdef FEAT_FOLDING
4724 textwidth -= curwin->w_p_fdc;
4725#endif
4726#ifdef FEAT_SIGNS
4727 if (curwin->w_buffer->b_signlist != NULL
4728# ifdef FEAT_NETBEANS_INTG
4729 || usingNetbeans
4730# endif
4731 )
4732 textwidth -= 1;
4733#endif
4734 if (curwin->w_p_nu)
4735 textwidth -= 8;
4736 }
4737 if (textwidth < 0)
4738 textwidth = 0;
4739 if (ff && textwidth == 0)
4740 {
4741 textwidth = W_WIDTH(curwin) - 1;
4742 if (textwidth > 79)
4743 textwidth = 79;
4744 }
4745 return textwidth;
4746}
4747
4748/*
4749 * Put a character in the redo buffer, for when just after a CTRL-V.
4750 */
4751 static void
4752redo_literal(c)
4753 int c;
4754{
4755 char_u buf[10];
4756
4757 /* Only digits need special treatment. Translate them into a string of
4758 * three digits. */
4759 if (VIM_ISDIGIT(c))
4760 {
4761 sprintf((char *)buf, "%03d", c);
4762 AppendToRedobuff(buf);
4763 }
4764 else
4765 AppendCharToRedobuff(c);
4766}
4767
4768/*
4769 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004770 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
4772 static void
4773start_arrow(end_insert_pos)
4774 pos_T *end_insert_pos;
4775{
4776 if (!arrow_used) /* something has been inserted */
4777 {
4778 AppendToRedobuff(ESC_STR);
4779 stop_insert(end_insert_pos, FALSE);
4780 arrow_used = TRUE; /* this means we stopped the current insert */
4781 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004782#ifdef FEAT_SYN_HL
4783 check_spell_redraw();
4784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785}
4786
Bram Moolenaar217ad922005-03-20 22:37:15 +00004787#ifdef FEAT_SYN_HL
4788/*
4789 * If we skipped highlighting word at cursor, do it now.
4790 * It may be skipped again, thus reset spell_redraw_lnum first.
4791 */
4792 static void
4793check_spell_redraw()
4794{
4795 if (spell_redraw_lnum != 0)
4796 {
4797 linenr_T lnum = spell_redraw_lnum;
4798
4799 spell_redraw_lnum = 0;
4800 redrawWinline(lnum, FALSE);
4801 }
4802}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004803
4804/*
4805 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4806 * spelled word, if there is one.
4807 */
4808 static void
4809spell_back_to_badword()
4810{
4811 pos_T tpos = curwin->w_cursor;
4812
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004813 spell_bad_len = spell_move_to(BACKWARD, TRUE, TRUE);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004814 if (curwin->w_cursor.col != tpos.col)
4815 start_arrow(&tpos);
4816}
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817#endif
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819/*
4820 * stop_arrow() is called before a change is made in insert mode.
4821 * If an arrow key has been used, start a new insertion.
4822 * Returns FAIL if undo is impossible, shouldn't insert then.
4823 */
4824 int
4825stop_arrow()
4826{
4827 if (arrow_used)
4828 {
4829 if (u_save_cursor() == OK)
4830 {
4831 arrow_used = FALSE;
4832 ins_need_undo = FALSE;
4833 }
4834 Insstart = curwin->w_cursor; /* new insertion starts here */
4835 Insstart_textlen = linetabsize(ml_get_curline());
4836 ai_col = 0;
4837#ifdef FEAT_VREPLACE
4838 if (State & VREPLACE_FLAG)
4839 {
4840 orig_line_count = curbuf->b_ml.ml_line_count;
4841 vr_lines_changed = 1;
4842 }
4843#endif
4844 ResetRedobuff();
4845 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
4846 }
4847 else if (ins_need_undo)
4848 {
4849 if (u_save_cursor() == OK)
4850 ins_need_undo = FALSE;
4851 }
4852
4853#ifdef FEAT_FOLDING
4854 /* Always open fold at the cursor line when inserting something. */
4855 foldOpenCursor();
4856#endif
4857
4858 return (arrow_used || ins_need_undo ? FAIL : OK);
4859}
4860
4861/*
4862 * do a few things to stop inserting
4863 */
4864 static void
4865stop_insert(end_insert_pos, esc)
4866 pos_T *end_insert_pos; /* where insert ended */
4867 int esc; /* called by ins_esc() */
4868{
4869 int cc;
4870
4871 stop_redo_ins();
4872 replace_flush(); /* abandon replace stack */
4873
4874 /*
4875 * save the inserted text for later redo with ^@
4876 */
4877 vim_free(last_insert);
4878 last_insert = get_inserted();
4879 last_insert_skip = new_insert_skip;
4880
4881 if (!arrow_used)
4882 {
4883 /* Auto-format now. It may seem strange to do this when stopping an
4884 * insertion (or moving the cursor), but it's required when appending
4885 * a line and having it end in a space. But only do it when something
4886 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004887 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004889 pos_T tpos = curwin->w_cursor;
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /* When the cursor is at the end of the line after a space the
4892 * formatting will move it to the following word. Avoid that by
4893 * moving the cursor onto the space. */
4894 cc = 'x';
4895 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
4896 {
4897 dec_cursor();
4898 cc = gchar_cursor();
4899 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004900 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902
4903 auto_format(TRUE, FALSE);
4904
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004905 if (vim_iswhite(cc))
4906 {
4907 if (gchar_cursor() != NUL)
4908 inc_cursor();
4909#ifdef FEAT_VIRTUALEDIT
4910 /* If the cursor is still at the same character, also keep
4911 * the "coladd". */
4912 if (gchar_cursor() == NUL
4913 && curwin->w_cursor.lnum == tpos.lnum
4914 && curwin->w_cursor.col == tpos.col)
4915 curwin->w_cursor.coladd = tpos.coladd;
4916#endif
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919
4920 /* If a space was inserted for auto-formatting, remove it now. */
4921 check_auto_format(TRUE);
4922
4923 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004924 * of the line, and put the cursor back.
4925 * Do this when ESC was used or moving the cursor up/down. */
4926 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
4927 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004929 pos_T tpos = curwin->w_cursor;
4930
4931 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
4933 --curwin->w_cursor.col;
4934 while (cc = gchar_cursor(), vim_iswhite(cc))
4935 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004936 if (curwin->w_cursor.lnum != tpos.lnum)
4937 curwin->w_cursor = tpos;
4938 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 ++curwin->w_cursor.col; /* put cursor back on the NUL */
4940
4941#ifdef FEAT_VISUAL
4942 /* <C-S-Right> may have started Visual mode, adjust the position for
4943 * deleted characters. */
4944 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
4945 {
4946 cc = STRLEN(ml_get_curline());
4947 if (VIsual.col > (colnr_T)cc)
4948 {
4949 VIsual.col = cc;
4950# ifdef FEAT_VIRTUALEDIT
4951 VIsual.coladd = 0;
4952# endif
4953 }
4954 }
4955#endif
4956 }
4957 }
4958 did_ai = FALSE;
4959#ifdef FEAT_SMARTINDENT
4960 did_si = FALSE;
4961 can_si = FALSE;
4962 can_si_back = FALSE;
4963#endif
4964
4965 /* set '[ and '] to the inserted text */
4966 curbuf->b_op_start = Insstart;
4967 curbuf->b_op_end = *end_insert_pos;
4968}
4969
4970/*
4971 * Set the last inserted text to a single character.
4972 * Used for the replace command.
4973 */
4974 void
4975set_last_insert(c)
4976 int c;
4977{
4978 char_u *s;
4979
4980 vim_free(last_insert);
4981#ifdef FEAT_MBYTE
4982 last_insert = alloc(MB_MAXBYTES * 3 + 5);
4983#else
4984 last_insert = alloc(6);
4985#endif
4986 if (last_insert != NULL)
4987 {
4988 s = last_insert;
4989 /* Use the CTRL-V only when entering a special char */
4990 if (c < ' ' || c == DEL)
4991 *s++ = Ctrl_V;
4992 s = add_char2buf(c, s);
4993 *s++ = ESC;
4994 *s++ = NUL;
4995 last_insert_skip = 0;
4996 }
4997}
4998
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004999#if defined(EXITFREE) || defined(PROTO)
5000 void
5001free_last_insert()
5002{
5003 vim_free(last_insert);
5004 last_insert = NULL;
5005}
5006#endif
5007
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008/*
5009 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5010 * and CSI. Handle multi-byte characters.
5011 * Returns a pointer to after the added bytes.
5012 */
5013 char_u *
5014add_char2buf(c, s)
5015 int c;
5016 char_u *s;
5017{
5018#ifdef FEAT_MBYTE
5019 char_u temp[MB_MAXBYTES];
5020 int i;
5021 int len;
5022
5023 len = (*mb_char2bytes)(c, temp);
5024 for (i = 0; i < len; ++i)
5025 {
5026 c = temp[i];
5027#endif
5028 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5029 if (c == K_SPECIAL)
5030 {
5031 *s++ = K_SPECIAL;
5032 *s++ = KS_SPECIAL;
5033 *s++ = KE_FILLER;
5034 }
5035#ifdef FEAT_GUI
5036 else if (c == CSI)
5037 {
5038 *s++ = CSI;
5039 *s++ = KS_EXTRA;
5040 *s++ = (int)KE_CSI;
5041 }
5042#endif
5043 else
5044 *s++ = c;
5045#ifdef FEAT_MBYTE
5046 }
5047#endif
5048 return s;
5049}
5050
5051/*
5052 * move cursor to start of line
5053 * if flags & BL_WHITE move to first non-white
5054 * if flags & BL_SOL move to first non-white if startofline is set,
5055 * otherwise keep "curswant" column
5056 * if flags & BL_FIX don't leave the cursor on a NUL.
5057 */
5058 void
5059beginline(flags)
5060 int flags;
5061{
5062 if ((flags & BL_SOL) && !p_sol)
5063 coladvance(curwin->w_curswant);
5064 else
5065 {
5066 curwin->w_cursor.col = 0;
5067#ifdef FEAT_VIRTUALEDIT
5068 curwin->w_cursor.coladd = 0;
5069#endif
5070
5071 if (flags & (BL_WHITE | BL_SOL))
5072 {
5073 char_u *ptr;
5074
5075 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5076 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5077 ++curwin->w_cursor.col;
5078 }
5079 curwin->w_set_curswant = TRUE;
5080 }
5081}
5082
5083/*
5084 * oneright oneleft cursor_down cursor_up
5085 *
5086 * Move one char {right,left,down,up}.
5087 * Doesn't move onto the NUL past the end of the line.
5088 * Return OK when successful, FAIL when we hit a line of file boundary.
5089 */
5090
5091 int
5092oneright()
5093{
5094 char_u *ptr;
5095#ifdef FEAT_MBYTE
5096 int l;
5097#endif
5098
5099#ifdef FEAT_VIRTUALEDIT
5100 if (virtual_active())
5101 {
5102 pos_T prevpos = curwin->w_cursor;
5103
5104 /* Adjust for multi-wide char (excluding TAB) */
5105 ptr = ml_get_cursor();
5106 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5107#ifdef FEAT_MBYTE
5108 (*mb_ptr2char)(ptr)
5109#else
5110 *ptr
5111#endif
5112 ))
5113 ? ptr2cells(ptr) : 1));
5114 curwin->w_set_curswant = TRUE;
5115 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5116 return (prevpos.col != curwin->w_cursor.col
5117 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5118 }
5119#endif
5120
5121 ptr = ml_get_cursor();
5122#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005123 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 /* The character under the cursor is a multi-byte character, move
5126 * several bytes right, but don't end up on the NUL. */
5127 if (ptr[l] == NUL)
5128 return FAIL;
5129 curwin->w_cursor.col += l;
5130 }
5131 else
5132#endif
5133 {
5134 if (*ptr++ == NUL || *ptr == NUL)
5135 return FAIL;
5136 ++curwin->w_cursor.col;
5137 }
5138
5139 curwin->w_set_curswant = TRUE;
5140 return OK;
5141}
5142
5143 int
5144oneleft()
5145{
5146#ifdef FEAT_VIRTUALEDIT
5147 if (virtual_active())
5148 {
5149 int width;
5150 int v = getviscol();
5151
5152 if (v == 0)
5153 return FAIL;
5154
5155# ifdef FEAT_LINEBREAK
5156 /* We might get stuck on 'showbreak', skip over it. */
5157 width = 1;
5158 for (;;)
5159 {
5160 coladvance(v - width);
5161 /* getviscol() is slow, skip it when 'showbreak' is empty and
5162 * there are no multi-byte characters */
5163 if ((*p_sbr == NUL
5164# ifdef FEAT_MBYTE
5165 && !has_mbyte
5166# endif
5167 ) || getviscol() < v)
5168 break;
5169 ++width;
5170 }
5171# else
5172 coladvance(v - 1);
5173# endif
5174
5175 if (curwin->w_cursor.coladd == 1)
5176 {
5177 char_u *ptr;
5178
5179 /* Adjust for multi-wide char (not a TAB) */
5180 ptr = ml_get_cursor();
5181 if (*ptr != TAB && vim_isprintc(
5182# ifdef FEAT_MBYTE
5183 (*mb_ptr2char)(ptr)
5184# else
5185 *ptr
5186# endif
5187 ) && ptr2cells(ptr) > 1)
5188 curwin->w_cursor.coladd = 0;
5189 }
5190
5191 curwin->w_set_curswant = TRUE;
5192 return OK;
5193 }
5194#endif
5195
5196 if (curwin->w_cursor.col == 0)
5197 return FAIL;
5198
5199 curwin->w_set_curswant = TRUE;
5200 --curwin->w_cursor.col;
5201
5202#ifdef FEAT_MBYTE
5203 /* if the character on the left of the current cursor is a multi-byte
5204 * character, move to its first byte */
5205 if (has_mbyte)
5206 mb_adjust_cursor();
5207#endif
5208 return OK;
5209}
5210
5211 int
5212cursor_up(n, upd_topline)
5213 long n;
5214 int upd_topline; /* When TRUE: update topline */
5215{
5216 linenr_T lnum;
5217
5218 if (n > 0)
5219 {
5220 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005221 /* This fails if the cursor is already in the first line or the count
5222 * is larger than the line number and '-' is in 'cpoptions' */
5223 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 return FAIL;
5225 if (n >= lnum)
5226 lnum = 1;
5227 else
5228#ifdef FEAT_FOLDING
5229 if (hasAnyFolding(curwin))
5230 {
5231 /*
5232 * Count each sequence of folded lines as one logical line.
5233 */
5234 /* go to the the start of the current fold */
5235 (void)hasFolding(lnum, &lnum, NULL);
5236
5237 while (n--)
5238 {
5239 /* move up one line */
5240 --lnum;
5241 if (lnum <= 1)
5242 break;
5243 /* If we entered a fold, move to the beginning, unless in
5244 * Insert mode or when 'foldopen' contains "all": it will open
5245 * in a moment. */
5246 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5247 (void)hasFolding(lnum, &lnum, NULL);
5248 }
5249 if (lnum < 1)
5250 lnum = 1;
5251 }
5252 else
5253#endif
5254 lnum -= n;
5255 curwin->w_cursor.lnum = lnum;
5256 }
5257
5258 /* try to advance to the column we want to be at */
5259 coladvance(curwin->w_curswant);
5260
5261 if (upd_topline)
5262 update_topline(); /* make sure curwin->w_topline is valid */
5263
5264 return OK;
5265}
5266
5267/*
5268 * Cursor down a number of logical lines.
5269 */
5270 int
5271cursor_down(n, upd_topline)
5272 long n;
5273 int upd_topline; /* When TRUE: update topline */
5274{
5275 linenr_T lnum;
5276
5277 if (n > 0)
5278 {
5279 lnum = curwin->w_cursor.lnum;
5280#ifdef FEAT_FOLDING
5281 /* Move to last line of fold, will fail if it's the end-of-file. */
5282 (void)hasFolding(lnum, NULL, &lnum);
5283#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005284 /* This fails if the cursor is already in the last line or would move
5285 * beyound the last line and '-' is in 'cpoptions' */
5286 if (lnum >= curbuf->b_ml.ml_line_count
5287 || (lnum + n > curbuf->b_ml.ml_line_count
5288 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return FAIL;
5290 if (lnum + n >= curbuf->b_ml.ml_line_count)
5291 lnum = curbuf->b_ml.ml_line_count;
5292 else
5293#ifdef FEAT_FOLDING
5294 if (hasAnyFolding(curwin))
5295 {
5296 linenr_T last;
5297
5298 /* count each sequence of folded lines as one logical line */
5299 while (n--)
5300 {
5301 if (hasFolding(lnum, NULL, &last))
5302 lnum = last + 1;
5303 else
5304 ++lnum;
5305 if (lnum >= curbuf->b_ml.ml_line_count)
5306 break;
5307 }
5308 if (lnum > curbuf->b_ml.ml_line_count)
5309 lnum = curbuf->b_ml.ml_line_count;
5310 }
5311 else
5312#endif
5313 lnum += n;
5314 curwin->w_cursor.lnum = lnum;
5315 }
5316
5317 /* try to advance to the column we want to be at */
5318 coladvance(curwin->w_curswant);
5319
5320 if (upd_topline)
5321 update_topline(); /* make sure curwin->w_topline is valid */
5322
5323 return OK;
5324}
5325
5326/*
5327 * Stuff the last inserted text in the read buffer.
5328 * Last_insert actually is a copy of the redo buffer, so we
5329 * first have to remove the command.
5330 */
5331 int
5332stuff_inserted(c, count, no_esc)
5333 int c; /* Command character to be inserted */
5334 long count; /* Repeat this many times */
5335 int no_esc; /* Don't add an ESC at the end */
5336{
5337 char_u *esc_ptr;
5338 char_u *ptr;
5339 char_u *last_ptr;
5340 char_u last = NUL;
5341
5342 ptr = get_last_insert();
5343 if (ptr == NULL)
5344 {
5345 EMSG(_(e_noinstext));
5346 return FAIL;
5347 }
5348
5349 /* may want to stuff the command character, to start Insert mode */
5350 if (c != NUL)
5351 stuffcharReadbuff(c);
5352 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5353 *esc_ptr = NUL; /* remove the ESC */
5354
5355 /* when the last char is either "0" or "^" it will be quoted if no ESC
5356 * comes after it OR if it will inserted more than once and "ptr"
5357 * starts with ^D. -- Acevedo
5358 */
5359 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5360 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5361 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5362 {
5363 last = *last_ptr;
5364 *last_ptr = NUL;
5365 }
5366
5367 do
5368 {
5369 stuffReadbuff(ptr);
5370 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5371 if (last)
5372 stuffReadbuff((char_u *)(last == '0'
5373 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5374 : IF_EB("\026^", CTRL_V_STR "^")));
5375 }
5376 while (--count > 0);
5377
5378 if (last)
5379 *last_ptr = last;
5380
5381 if (esc_ptr != NULL)
5382 *esc_ptr = ESC; /* put the ESC back */
5383
5384 /* may want to stuff a trailing ESC, to get out of Insert mode */
5385 if (!no_esc)
5386 stuffcharReadbuff(ESC);
5387
5388 return OK;
5389}
5390
5391 char_u *
5392get_last_insert()
5393{
5394 if (last_insert == NULL)
5395 return NULL;
5396 return last_insert + last_insert_skip;
5397}
5398
5399/*
5400 * Get last inserted string, and remove trailing <Esc>.
5401 * Returns pointer to allocated memory (must be freed) or NULL.
5402 */
5403 char_u *
5404get_last_insert_save()
5405{
5406 char_u *s;
5407 int len;
5408
5409 if (last_insert == NULL)
5410 return NULL;
5411 s = vim_strsave(last_insert + last_insert_skip);
5412 if (s != NULL)
5413 {
5414 len = (int)STRLEN(s);
5415 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5416 s[len - 1] = NUL;
5417 }
5418 return s;
5419}
5420
5421/*
5422 * Check the word in front of the cursor for an abbreviation.
5423 * Called when the non-id character "c" has been entered.
5424 * When an abbreviation is recognized it is removed from the text and
5425 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5426 */
5427 static int
5428echeck_abbr(c)
5429 int c;
5430{
5431 /* Don't check for abbreviation in paste mode, when disabled and just
5432 * after moving around with cursor keys. */
5433 if (p_paste || no_abbr || arrow_used)
5434 return FALSE;
5435
5436 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5437 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5438}
5439
5440/*
5441 * replace-stack functions
5442 *
5443 * When replacing characters, the replaced characters are remembered for each
5444 * new character. This is used to re-insert the old text when backspacing.
5445 *
5446 * There is a NUL headed list of characters for each character that is
5447 * currently in the file after the insertion point. When BS is used, one NUL
5448 * headed list is put back for the deleted character.
5449 *
5450 * For a newline, there are two NUL headed lists. One contains the characters
5451 * that the NL replaced. The extra one stores the characters after the cursor
5452 * that were deleted (always white space).
5453 *
5454 * Replace_offset is normally 0, in which case replace_push will add a new
5455 * character at the end of the stack. If replace_offset is not 0, that many
5456 * characters will be left on the stack above the newly inserted character.
5457 */
5458
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005459static char_u *replace_stack = NULL;
5460static long replace_stack_nr = 0; /* next entry in replace stack */
5461static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 void
5464replace_push(c)
5465 int c; /* character that is replaced (NUL is none) */
5466{
5467 char_u *p;
5468
5469 if (replace_stack_nr < replace_offset) /* nothing to do */
5470 return;
5471 if (replace_stack_len <= replace_stack_nr)
5472 {
5473 replace_stack_len += 50;
5474 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5475 if (p == NULL) /* out of memory */
5476 {
5477 replace_stack_len -= 50;
5478 return;
5479 }
5480 if (replace_stack != NULL)
5481 {
5482 mch_memmove(p, replace_stack,
5483 (size_t)(replace_stack_nr * sizeof(char_u)));
5484 vim_free(replace_stack);
5485 }
5486 replace_stack = p;
5487 }
5488 p = replace_stack + replace_stack_nr - replace_offset;
5489 if (replace_offset)
5490 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5491 *p = c;
5492 ++replace_stack_nr;
5493}
5494
5495/*
5496 * call replace_push(c) with replace_offset set to the first NUL.
5497 */
5498 static void
5499replace_push_off(c)
5500 int c;
5501{
5502 char_u *p;
5503
5504 p = replace_stack + replace_stack_nr;
5505 for (replace_offset = 1; replace_offset < replace_stack_nr;
5506 ++replace_offset)
5507 if (*--p == NUL)
5508 break;
5509 replace_push(c);
5510 replace_offset = 0;
5511}
5512
5513/*
5514 * Pop one item from the replace stack.
5515 * return -1 if stack empty
5516 * return replaced character or NUL otherwise
5517 */
5518 static int
5519replace_pop()
5520{
5521 if (replace_stack_nr == 0)
5522 return -1;
5523 return (int)replace_stack[--replace_stack_nr];
5524}
5525
5526/*
5527 * Join the top two items on the replace stack. This removes to "off"'th NUL
5528 * encountered.
5529 */
5530 static void
5531replace_join(off)
5532 int off; /* offset for which NUL to remove */
5533{
5534 int i;
5535
5536 for (i = replace_stack_nr; --i >= 0; )
5537 if (replace_stack[i] == NUL && off-- <= 0)
5538 {
5539 --replace_stack_nr;
5540 mch_memmove(replace_stack + i, replace_stack + i + 1,
5541 (size_t)(replace_stack_nr - i));
5542 return;
5543 }
5544}
5545
5546/*
5547 * Pop bytes from the replace stack until a NUL is found, and insert them
5548 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5549 */
5550 static void
5551replace_pop_ins()
5552{
5553 int cc;
5554 int oldState = State;
5555
5556 State = NORMAL; /* don't want REPLACE here */
5557 while ((cc = replace_pop()) > 0)
5558 {
5559#ifdef FEAT_MBYTE
5560 mb_replace_pop_ins(cc);
5561#else
5562 ins_char(cc);
5563#endif
5564 dec_cursor();
5565 }
5566 State = oldState;
5567}
5568
5569#ifdef FEAT_MBYTE
5570/*
5571 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5572 * indicates a multi-byte char, pop the other bytes too.
5573 */
5574 static void
5575mb_replace_pop_ins(cc)
5576 int cc;
5577{
5578 int n;
5579 char_u buf[MB_MAXBYTES];
5580 int i;
5581 int c;
5582
5583 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5584 {
5585 buf[0] = cc;
5586 for (i = 1; i < n; ++i)
5587 buf[i] = replace_pop();
5588 ins_bytes_len(buf, n);
5589 }
5590 else
5591 ins_char(cc);
5592
5593 if (enc_utf8)
5594 /* Handle composing chars. */
5595 for (;;)
5596 {
5597 c = replace_pop();
5598 if (c == -1) /* stack empty */
5599 break;
5600 if ((n = MB_BYTE2LEN(c)) == 1)
5601 {
5602 /* Not a multi-byte char, put it back. */
5603 replace_push(c);
5604 break;
5605 }
5606 else
5607 {
5608 buf[0] = c;
5609 for (i = 1; i < n; ++i)
5610 buf[i] = replace_pop();
5611 if (utf_iscomposing(utf_ptr2char(buf)))
5612 ins_bytes_len(buf, n);
5613 else
5614 {
5615 /* Not a composing char, put it back. */
5616 for (i = n - 1; i >= 0; --i)
5617 replace_push(buf[i]);
5618 break;
5619 }
5620 }
5621 }
5622}
5623#endif
5624
5625/*
5626 * make the replace stack empty
5627 * (called when exiting replace mode)
5628 */
5629 static void
5630replace_flush()
5631{
5632 vim_free(replace_stack);
5633 replace_stack = NULL;
5634 replace_stack_len = 0;
5635 replace_stack_nr = 0;
5636}
5637
5638/*
5639 * Handle doing a BS for one character.
5640 * cc < 0: replace stack empty, just move cursor
5641 * cc == 0: character was inserted, delete it
5642 * cc > 0: character was replaced, put cc (first byte of original char) back
5643 * and check for more characters to be put back
5644 */
5645 static void
5646replace_do_bs()
5647{
5648 int cc;
5649#ifdef FEAT_VREPLACE
5650 int orig_len = 0;
5651 int ins_len;
5652 int orig_vcols = 0;
5653 colnr_T start_vcol;
5654 char_u *p;
5655 int i;
5656 int vcol;
5657#endif
5658
5659 cc = replace_pop();
5660 if (cc > 0)
5661 {
5662#ifdef FEAT_VREPLACE
5663 if (State & VREPLACE_FLAG)
5664 {
5665 /* Get the number of screen cells used by the character we are
5666 * going to delete. */
5667 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5668 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5669 }
5670#endif
5671#ifdef FEAT_MBYTE
5672 if (has_mbyte)
5673 {
5674 del_char(FALSE);
5675# ifdef FEAT_VREPLACE
5676 if (State & VREPLACE_FLAG)
5677 orig_len = STRLEN(ml_get_cursor());
5678# endif
5679 replace_push(cc);
5680 }
5681 else
5682#endif
5683 {
5684 pchar_cursor(cc);
5685#ifdef FEAT_VREPLACE
5686 if (State & VREPLACE_FLAG)
5687 orig_len = STRLEN(ml_get_cursor()) - 1;
5688#endif
5689 }
5690 replace_pop_ins();
5691
5692#ifdef FEAT_VREPLACE
5693 if (State & VREPLACE_FLAG)
5694 {
5695 /* Get the number of screen cells used by the inserted characters */
5696 p = ml_get_cursor();
5697 ins_len = STRLEN(p) - orig_len;
5698 vcol = start_vcol;
5699 for (i = 0; i < ins_len; ++i)
5700 {
5701 vcol += chartabsize(p + i, vcol);
5702#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005703 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704#endif
5705 }
5706 vcol -= start_vcol;
5707
5708 /* Delete spaces that were inserted after the cursor to keep the
5709 * text aligned. */
5710 curwin->w_cursor.col += ins_len;
5711 while (vcol > orig_vcols && gchar_cursor() == ' ')
5712 {
5713 del_char(FALSE);
5714 ++orig_vcols;
5715 }
5716 curwin->w_cursor.col -= ins_len;
5717 }
5718#endif
5719
5720 /* mark the buffer as changed and prepare for displaying */
5721 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5722 }
5723 else if (cc == 0)
5724 (void)del_char(FALSE);
5725}
5726
5727#ifdef FEAT_CINDENT
5728/*
5729 * Return TRUE if C-indenting is on.
5730 */
5731 static int
5732cindent_on()
5733{
5734 return (!p_paste && (curbuf->b_p_cin
5735# ifdef FEAT_EVAL
5736 || *curbuf->b_p_inde != NUL
5737# endif
5738 ));
5739}
5740#endif
5741
5742#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5743/*
5744 * Re-indent the current line, based on the current contents of it and the
5745 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
5746 * confused what all the part that handles Control-T is doing that I'm not.
5747 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
5748 */
5749
5750 void
5751fixthisline(get_the_indent)
5752 int (*get_the_indent) __ARGS((void));
5753{
5754 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
5755 if (linewhite(curwin->w_cursor.lnum))
5756 did_ai = TRUE; /* delete the indent if the line stays empty */
5757}
5758
5759 void
5760fix_indent()
5761{
5762 if (p_paste)
5763 return;
5764# ifdef FEAT_LISP
5765 if (curbuf->b_p_lisp && curbuf->b_p_ai)
5766 fixthisline(get_lisp_indent);
5767# endif
5768# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
5769 else
5770# endif
5771# ifdef FEAT_CINDENT
5772 if (cindent_on())
5773 do_c_expr_indent();
5774# endif
5775}
5776
5777#endif
5778
5779#ifdef FEAT_CINDENT
5780/*
5781 * return TRUE if 'cinkeys' contains the key "keytyped",
5782 * when == '*': Only if key is preceded with '*' (indent before insert)
5783 * when == '!': Only if key is prededed with '!' (don't insert)
5784 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
5785 *
5786 * "keytyped" can have a few special values:
5787 * KEY_OPEN_FORW
5788 * KEY_OPEN_BACK
5789 * KEY_COMPLETE just finished completion.
5790 *
5791 * If line_is_empty is TRUE accept keys with '0' before them.
5792 */
5793 int
5794in_cinkeys(keytyped, when, line_is_empty)
5795 int keytyped;
5796 int when;
5797 int line_is_empty;
5798{
5799 char_u *look;
5800 int try_match;
5801 int try_match_word;
5802 char_u *p;
5803 char_u *line;
5804 int icase;
5805 int i;
5806
5807#ifdef FEAT_EVAL
5808 if (*curbuf->b_p_inde != NUL)
5809 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
5810 else
5811#endif
5812 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
5813 while (*look)
5814 {
5815 /*
5816 * Find out if we want to try a match with this key, depending on
5817 * 'when' and a '*' or '!' before the key.
5818 */
5819 switch (when)
5820 {
5821 case '*': try_match = (*look == '*'); break;
5822 case '!': try_match = (*look == '!'); break;
5823 default: try_match = (*look != '*'); break;
5824 }
5825 if (*look == '*' || *look == '!')
5826 ++look;
5827
5828 /*
5829 * If there is a '0', only accept a match if the line is empty.
5830 * But may still match when typing last char of a word.
5831 */
5832 if (*look == '0')
5833 {
5834 try_match_word = try_match;
5835 if (!line_is_empty)
5836 try_match = FALSE;
5837 ++look;
5838 }
5839 else
5840 try_match_word = FALSE;
5841
5842 /*
5843 * does it look like a control character?
5844 */
5845 if (*look == '^'
5846#ifdef EBCDIC
5847 && (Ctrl_chr(look[1]) != 0)
5848#else
5849 && look[1] >= '?' && look[1] <= '_'
5850#endif
5851 )
5852 {
5853 if (try_match && keytyped == Ctrl_chr(look[1]))
5854 return TRUE;
5855 look += 2;
5856 }
5857 /*
5858 * 'o' means "o" command, open forward.
5859 * 'O' means "O" command, open backward.
5860 */
5861 else if (*look == 'o')
5862 {
5863 if (try_match && keytyped == KEY_OPEN_FORW)
5864 return TRUE;
5865 ++look;
5866 }
5867 else if (*look == 'O')
5868 {
5869 if (try_match && keytyped == KEY_OPEN_BACK)
5870 return TRUE;
5871 ++look;
5872 }
5873
5874 /*
5875 * 'e' means to check for "else" at start of line and just before the
5876 * cursor.
5877 */
5878 else if (*look == 'e')
5879 {
5880 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
5881 {
5882 p = ml_get_curline();
5883 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
5884 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
5885 return TRUE;
5886 }
5887 ++look;
5888 }
5889
5890 /*
5891 * ':' only causes an indent if it is at the end of a label or case
5892 * statement, or when it was before typing the ':' (to fix
5893 * class::method for C++).
5894 */
5895 else if (*look == ':')
5896 {
5897 if (try_match && keytyped == ':')
5898 {
5899 p = ml_get_curline();
5900 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
5901 return TRUE;
5902 if (curwin->w_cursor.col > 2
5903 && p[curwin->w_cursor.col - 1] == ':'
5904 && p[curwin->w_cursor.col - 2] == ':')
5905 {
5906 p[curwin->w_cursor.col - 1] = ' ';
5907 i = (cin_iscase(p) || cin_isscopedecl(p)
5908 || cin_islabel(30));
5909 p = ml_get_curline();
5910 p[curwin->w_cursor.col - 1] = ':';
5911 if (i)
5912 return TRUE;
5913 }
5914 }
5915 ++look;
5916 }
5917
5918
5919 /*
5920 * Is it a key in <>, maybe?
5921 */
5922 else if (*look == '<')
5923 {
5924 if (try_match)
5925 {
5926 /*
5927 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
5928 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
5929 * >, *, : and ! keys if they really really want to.
5930 */
5931 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
5932 && keytyped == look[1])
5933 return TRUE;
5934
5935 if (keytyped == get_special_key_code(look + 1))
5936 return TRUE;
5937 }
5938 while (*look && *look != '>')
5939 look++;
5940 while (*look == '>')
5941 look++;
5942 }
5943
5944 /*
5945 * Is it a word: "=word"?
5946 */
5947 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
5948 {
5949 ++look;
5950 if (*look == '~')
5951 {
5952 icase = TRUE;
5953 ++look;
5954 }
5955 else
5956 icase = FALSE;
5957 p = vim_strchr(look, ',');
5958 if (p == NULL)
5959 p = look + STRLEN(look);
5960 if ((try_match || try_match_word)
5961 && curwin->w_cursor.col >= (colnr_T)(p - look))
5962 {
5963 int match = FALSE;
5964
5965#ifdef FEAT_INS_EXPAND
5966 if (keytyped == KEY_COMPLETE)
5967 {
5968 char_u *s;
5969
5970 /* Just completed a word, check if it starts with "look".
5971 * search back for the start of a word. */
5972 line = ml_get_curline();
5973# ifdef FEAT_MBYTE
5974 if (has_mbyte)
5975 {
5976 char_u *n;
5977
5978 for (s = line + curwin->w_cursor.col; s > line; s = n)
5979 {
5980 n = mb_prevptr(line, s);
5981 if (!vim_iswordp(n))
5982 break;
5983 }
5984 }
5985 else
5986# endif
5987 for (s = line + curwin->w_cursor.col; s > line; --s)
5988 if (!vim_iswordc(s[-1]))
5989 break;
5990 if (s + (p - look) <= line + curwin->w_cursor.col
5991 && (icase
5992 ? MB_STRNICMP(s, look, p - look)
5993 : STRNCMP(s, look, p - look)) == 0)
5994 match = TRUE;
5995 }
5996 else
5997#endif
5998 /* TODO: multi-byte */
5999 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6000 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6001 {
6002 line = ml_get_cursor();
6003 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6004 || !vim_iswordc(line[-(p - look) - 1]))
6005 && (icase
6006 ? MB_STRNICMP(line - (p - look), look, p - look)
6007 : STRNCMP(line - (p - look), look, p - look))
6008 == 0)
6009 match = TRUE;
6010 }
6011 if (match && try_match_word && !try_match)
6012 {
6013 /* "0=word": Check if there are only blanks before the
6014 * word. */
6015 line = ml_get_curline();
6016 if ((int)(skipwhite(line) - line) !=
6017 (int)(curwin->w_cursor.col - (p - look)))
6018 match = FALSE;
6019 }
6020 if (match)
6021 return TRUE;
6022 }
6023 look = p;
6024 }
6025
6026 /*
6027 * ok, it's a boring generic character.
6028 */
6029 else
6030 {
6031 if (try_match && *look == keytyped)
6032 return TRUE;
6033 ++look;
6034 }
6035
6036 /*
6037 * Skip over ", ".
6038 */
6039 look = skip_to_option_part(look);
6040 }
6041 return FALSE;
6042}
6043#endif /* FEAT_CINDENT */
6044
6045#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6046/*
6047 * Map Hebrew keyboard when in hkmap mode.
6048 */
6049 int
6050hkmap(c)
6051 int c;
6052{
6053 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6054 {
6055 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6056 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6057 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6058 static char_u map[26] =
6059 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6060 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6061 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6062 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6063 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6064 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6065 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6066 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6067 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6068
6069 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6070 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6071 /* '-1'='sofit' */
6072 else if (c == 'x')
6073 return 'X';
6074 else if (c == 'q')
6075 return '\''; /* {geresh}={'} */
6076 else if (c == 246)
6077 return ' '; /* \"o --> ' ' for a german keyboard */
6078 else if (c == 228)
6079 return ' '; /* \"a --> ' ' -- / -- */
6080 else if (c == 252)
6081 return ' '; /* \"u --> ' ' -- / -- */
6082#ifdef EBCDIC
6083 else if (islower(c))
6084#else
6085 /* NOTE: islower() does not do the right thing for us on Linux so we
6086 * do this the same was as 5.7 and previous, so it works correctly on
6087 * all systems. Specifically, the e.g. Delete and Arrow keys are
6088 * munged and won't work if e.g. searching for Hebrew text.
6089 */
6090 else if (c >= 'a' && c <= 'z')
6091#endif
6092 return (int)(map[CharOrdLow(c)] + p_aleph);
6093 else
6094 return c;
6095 }
6096 else
6097 {
6098 switch (c)
6099 {
6100 case '`': return ';';
6101 case '/': return '.';
6102 case '\'': return ',';
6103 case 'q': return '/';
6104 case 'w': return '\'';
6105
6106 /* Hebrew letters - set offset from 'a' */
6107 case ',': c = '{'; break;
6108 case '.': c = 'v'; break;
6109 case ';': c = 't'; break;
6110 default: {
6111 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6112
6113#ifdef EBCDIC
6114 /* see note about islower() above */
6115 if (!islower(c))
6116#else
6117 if (c < 'a' || c > 'z')
6118#endif
6119 return c;
6120 c = str[CharOrdLow(c)];
6121 break;
6122 }
6123 }
6124
6125 return (int)(CharOrdLow(c) + p_aleph);
6126 }
6127}
6128#endif
6129
6130 static void
6131ins_reg()
6132{
6133 int need_redraw = FALSE;
6134 int regname;
6135 int literally = 0;
6136
6137 /*
6138 * If we are going to wait for a character, show a '"'.
6139 */
6140 pc_status = PC_STATUS_UNSET;
6141 if (redrawing() && !char_avail())
6142 {
6143 /* may need to redraw when no more chars available now */
6144 ins_redraw();
6145
6146 edit_putchar('"', TRUE);
6147#ifdef FEAT_CMDL_INFO
6148 add_to_showcmd_c(Ctrl_R);
6149#endif
6150 }
6151
6152#ifdef USE_ON_FLY_SCROLL
6153 dont_scroll = TRUE; /* disallow scrolling here */
6154#endif
6155
6156 /*
6157 * Don't map the register name. This also prevents the mode message to be
6158 * deleted when ESC is hit.
6159 */
6160 ++no_mapping;
6161 regname = safe_vgetc();
6162#ifdef FEAT_LANGMAP
6163 LANGMAP_ADJUST(regname, TRUE);
6164#endif
6165 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6166 {
6167 /* Get a third key for literal register insertion */
6168 literally = regname;
6169#ifdef FEAT_CMDL_INFO
6170 add_to_showcmd_c(literally);
6171#endif
6172 regname = safe_vgetc();
6173#ifdef FEAT_LANGMAP
6174 LANGMAP_ADJUST(regname, TRUE);
6175#endif
6176 }
6177 --no_mapping;
6178
6179#ifdef FEAT_EVAL
6180 /*
6181 * Don't call u_sync() while getting the expression,
6182 * evaluating it or giving an error message for it!
6183 */
6184 ++no_u_sync;
6185 if (regname == '=')
6186 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006187# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006191# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 /* Restore the Input Method. */
6193 if (im_on)
6194 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006197 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6198 {
6199 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 else
6203 {
6204#endif
6205 if (literally == Ctrl_O || literally == Ctrl_P)
6206 {
6207 /* Append the command to the redo buffer. */
6208 AppendCharToRedobuff(Ctrl_R);
6209 AppendCharToRedobuff(literally);
6210 AppendCharToRedobuff(regname);
6211
6212 do_put(regname, BACKWARD, 1L,
6213 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6214 }
6215 else if (insert_reg(regname, literally) == FAIL)
6216 {
6217 vim_beep();
6218 need_redraw = TRUE; /* remove the '"' */
6219 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006220 else if (stop_insert_mode)
6221 /* When the '=' register was used and a function was invoked that
6222 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6223 * insert anything, need to remove the '"' */
6224 need_redraw = TRUE;
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226#ifdef FEAT_EVAL
6227 }
6228 --no_u_sync;
6229#endif
6230#ifdef FEAT_CMDL_INFO
6231 clear_showcmd();
6232#endif
6233
6234 /* If the inserted register is empty, we need to remove the '"' */
6235 if (need_redraw || stuff_empty())
6236 edit_unputchar();
6237}
6238
6239/*
6240 * CTRL-G commands in Insert mode.
6241 */
6242 static void
6243ins_ctrl_g()
6244{
6245 int c;
6246
6247#ifdef FEAT_INS_EXPAND
6248 /* Right after CTRL-X the cursor will be after the ruler. */
6249 setcursor();
6250#endif
6251
6252 /*
6253 * Don't map the second key. This also prevents the mode message to be
6254 * deleted when ESC is hit.
6255 */
6256 ++no_mapping;
6257 c = safe_vgetc();
6258 --no_mapping;
6259 switch (c)
6260 {
6261 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6262 case K_UP:
6263 case Ctrl_K:
6264 case 'k': ins_up(TRUE);
6265 break;
6266
6267 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6268 case K_DOWN:
6269 case Ctrl_J:
6270 case 'j': ins_down(TRUE);
6271 break;
6272
6273 /* CTRL-G u: start new undoable edit */
6274 case 'u': u_sync();
6275 ins_need_undo = TRUE;
6276 break;
6277
6278 /* Unknown CTRL-G command, reserved for future expansion. */
6279 default: vim_beep();
6280 }
6281}
6282
6283/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006284 * CTRL-^ in Insert mode.
6285 */
6286 static void
6287ins_ctrl_hat()
6288{
6289 if (map_to_exists_mode((char_u *)"", LANGMAP))
6290 {
6291 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6292 if (State & LANGMAP)
6293 {
6294 curbuf->b_p_iminsert = B_IMODE_NONE;
6295 State &= ~LANGMAP;
6296 }
6297 else
6298 {
6299 curbuf->b_p_iminsert = B_IMODE_LMAP;
6300 State |= LANGMAP;
6301#ifdef USE_IM_CONTROL
6302 im_set_active(FALSE);
6303#endif
6304 }
6305 }
6306#ifdef USE_IM_CONTROL
6307 else
6308 {
6309 /* There are no ":lmap" mappings, toggle IM */
6310 if (im_get_status())
6311 {
6312 curbuf->b_p_iminsert = B_IMODE_NONE;
6313 im_set_active(FALSE);
6314 }
6315 else
6316 {
6317 curbuf->b_p_iminsert = B_IMODE_IM;
6318 State &= ~LANGMAP;
6319 im_set_active(TRUE);
6320 }
6321 }
6322#endif
6323 set_iminsert_global();
6324 showmode();
6325#ifdef FEAT_GUI
6326 /* may show different cursor shape or color */
6327 if (gui.in_use)
6328 gui_update_cursor(TRUE, FALSE);
6329#endif
6330#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6331 /* Show/unshow value of 'keymap' in status lines. */
6332 status_redraw_curbuf();
6333#endif
6334}
6335
6336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 * Handle ESC in insert mode.
6338 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6339 * insert.
6340 */
6341 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006342ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 long *count;
6344 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006345 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 int temp;
6348 static int disabled_redraw = FALSE;
6349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006350#ifdef FEAT_SYN_HL
6351 check_spell_redraw();
6352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353#if defined(FEAT_HANGULIN)
6354# if defined(ESC_CHG_TO_ENG_MODE)
6355 hangul_input_state_set(0);
6356# endif
6357 if (composing_hangul)
6358 {
6359 push_raw_key(composing_hangul_buffer, 2);
6360 composing_hangul = 0;
6361 }
6362#endif
6363#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
6364 previous_script = GetScriptManagerVariable(smKeyScript);
6365 KeyScript(smKeyRoman); /* or smKeySysScript */
6366#endif
6367
6368 temp = curwin->w_cursor.col;
6369 if (disabled_redraw)
6370 {
6371 --RedrawingDisabled;
6372 disabled_redraw = FALSE;
6373 }
6374 if (!arrow_used)
6375 {
6376 /*
6377 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006378 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6379 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 */
6381 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006382 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384 /*
6385 * Repeating insert may take a long time. Check for
6386 * interrupt now and then.
6387 */
6388 if (*count > 0)
6389 {
6390 line_breakcheck();
6391 if (got_int)
6392 *count = 0;
6393 }
6394
6395 if (--*count > 0) /* repeat what was typed */
6396 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006397 /* Vi repeats the insert without replacing characters. */
6398 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6399 State &= ~REPLACE_FLAG;
6400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 (void)start_redo_ins();
6402 if (cmdchar == 'r' || cmdchar == 'v')
6403 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6404 ++RedrawingDisabled;
6405 disabled_redraw = TRUE;
6406 return FALSE; /* repeat the insert */
6407 }
6408 stop_insert(&curwin->w_cursor, TRUE);
6409 undisplay_dollar();
6410 }
6411
6412 /* When an autoindent was removed, curswant stays after the
6413 * indent */
6414 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6415 curwin->w_set_curswant = TRUE;
6416
6417 /* Remember the last Insert position in the '^ mark. */
6418 if (!cmdmod.keepjumps)
6419 curbuf->b_last_insert = curwin->w_cursor;
6420
6421 /*
6422 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006423 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006425 if (!nomove
6426 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427#ifdef FEAT_VIRTUALEDIT
6428 || curwin->w_cursor.coladd > 0
6429#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006430 )
6431 && (restart_edit == NUL
6432 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006434 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006436 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437#ifdef FEAT_RIGHTLEFT
6438 && !revins_on
6439#endif
6440 )
6441 {
6442#ifdef FEAT_VIRTUALEDIT
6443 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6444 {
6445 oneleft();
6446 if (restart_edit != NUL)
6447 ++curwin->w_cursor.coladd;
6448 }
6449 else
6450#endif
6451 {
6452 --curwin->w_cursor.col;
6453#ifdef FEAT_MBYTE
6454 /* Correct cursor for multi-byte character. */
6455 if (has_mbyte)
6456 mb_adjust_cursor();
6457#endif
6458 }
6459 }
6460
6461#ifdef USE_IM_CONTROL
6462 /* Disable IM to allow typing English directly for Normal mode commands.
6463 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6464 * well). */
6465 if (!(State & LANGMAP))
6466 im_save_status(&curbuf->b_p_iminsert);
6467 im_set_active(FALSE);
6468#endif
6469
6470 State = NORMAL;
6471 /* need to position cursor again (e.g. when on a TAB ) */
6472 changed_cline_bef_curs();
6473
6474#ifdef FEAT_MOUSE
6475 setmouse();
6476#endif
6477#ifdef CURSOR_SHAPE
6478 ui_cursor_shape(); /* may show different cursor shape */
6479#endif
6480
6481 /*
6482 * When recording or for CTRL-O, need to display the new mode.
6483 * Otherwise remove the mode message.
6484 */
6485 if (Recording || restart_edit != NUL)
6486 showmode();
6487 else if (p_smd)
6488 MSG("");
6489
6490 return TRUE; /* exit Insert mode */
6491}
6492
6493#ifdef FEAT_RIGHTLEFT
6494/*
6495 * Toggle language: hkmap and revins_on.
6496 * Move to end of reverse inserted text.
6497 */
6498 static void
6499ins_ctrl_()
6500{
6501 if (revins_on && revins_chars && revins_scol >= 0)
6502 {
6503 while (gchar_cursor() != NUL && revins_chars--)
6504 ++curwin->w_cursor.col;
6505 }
6506 p_ri = !p_ri;
6507 revins_on = (State == INSERT && p_ri);
6508 if (revins_on)
6509 {
6510 revins_scol = curwin->w_cursor.col;
6511 revins_legal++;
6512 revins_chars = 0;
6513 undisplay_dollar();
6514 }
6515 else
6516 revins_scol = -1;
6517#ifdef FEAT_FKMAP
6518 if (p_altkeymap)
6519 {
6520 /*
6521 * to be consistent also for redo command, using '.'
6522 * set arrow_used to true and stop it - causing to redo
6523 * characters entered in one mode (normal/reverse insert).
6524 */
6525 arrow_used = TRUE;
6526 (void)stop_arrow();
6527 p_fkmap = curwin->w_p_rl ^ p_ri;
6528 if (p_fkmap && p_ri)
6529 State = INSERT;
6530 }
6531 else
6532#endif
6533 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6534 showmode();
6535}
6536#endif
6537
6538#ifdef FEAT_VISUAL
6539/*
6540 * If 'keymodel' contains "startsel", may start selection.
6541 * Returns TRUE when a CTRL-O and other keys stuffed.
6542 */
6543 static int
6544ins_start_select(c)
6545 int c;
6546{
6547 if (km_startsel)
6548 switch (c)
6549 {
6550 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 case K_PAGEUP:
6553 case K_KPAGEUP:
6554 case K_PAGEDOWN:
6555 case K_KPAGEDOWN:
6556# ifdef MACOS
6557 case K_LEFT:
6558 case K_RIGHT:
6559 case K_UP:
6560 case K_DOWN:
6561 case K_END:
6562 case K_HOME:
6563# endif
6564 if (!(mod_mask & MOD_MASK_SHIFT))
6565 break;
6566 /* FALLTHROUGH */
6567 case K_S_LEFT:
6568 case K_S_RIGHT:
6569 case K_S_UP:
6570 case K_S_DOWN:
6571 case K_S_END:
6572 case K_S_HOME:
6573 /* Start selection right away, the cursor can move with
6574 * CTRL-O when beyond the end of the line. */
6575 start_selection();
6576
6577 /* Execute the key in (insert) Select mode. */
6578 stuffcharReadbuff(Ctrl_O);
6579 if (mod_mask)
6580 {
6581 char_u buf[4];
6582
6583 buf[0] = K_SPECIAL;
6584 buf[1] = KS_MODIFIER;
6585 buf[2] = mod_mask;
6586 buf[3] = NUL;
6587 stuffReadbuff(buf);
6588 }
6589 stuffcharReadbuff(c);
6590 return TRUE;
6591 }
6592 return FALSE;
6593}
6594#endif
6595
6596/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006597 * <Insert> key in Insert mode: toggle insert/remplace mode.
6598 */
6599 static void
6600ins_insert(replaceState)
6601 int replaceState;
6602{
6603#ifdef FEAT_FKMAP
6604 if (p_fkmap && p_ri)
6605 {
6606 beep_flush();
6607 EMSG(farsi_text_3); /* encoded in Farsi */
6608 return;
6609 }
6610#endif
6611
6612#ifdef FEAT_AUTOCMD
6613 set_vim_var_string(VV_INSERTMODE,
6614 (char_u *)((State & REPLACE_FLAG) ? "i" :
6615 replaceState == VREPLACE ? "v" : "r"), 1);
6616 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6617#endif
6618 if (State & REPLACE_FLAG)
6619 State = INSERT | (State & LANGMAP);
6620 else
6621 State = replaceState | (State & LANGMAP);
6622 AppendCharToRedobuff(K_INS);
6623 showmode();
6624#ifdef CURSOR_SHAPE
6625 ui_cursor_shape(); /* may show different cursor shape */
6626#endif
6627}
6628
6629/*
6630 * Pressed CTRL-O in Insert mode.
6631 */
6632 static void
6633ins_ctrl_o()
6634{
6635#ifdef FEAT_VREPLACE
6636 if (State & VREPLACE_FLAG)
6637 restart_edit = 'V';
6638 else
6639#endif
6640 if (State & REPLACE_FLAG)
6641 restart_edit = 'R';
6642 else
6643 restart_edit = 'I';
6644#ifdef FEAT_VIRTUALEDIT
6645 if (virtual_active())
6646 ins_at_eol = FALSE; /* cursor always keeps its column */
6647 else
6648#endif
6649 ins_at_eol = (gchar_cursor() == NUL);
6650}
6651
6652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 * If the cursor is on an indent, ^T/^D insert/delete one
6654 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6655 * Always round the indent to 'shiftwith', this is compatible
6656 * with vi. But vi only supports ^T and ^D after an
6657 * autoindent, we support it everywhere.
6658 */
6659 static void
6660ins_shift(c, lastc)
6661 int c;
6662 int lastc;
6663{
6664 if (stop_arrow() == FAIL)
6665 return;
6666 AppendCharToRedobuff(c);
6667
6668 /*
6669 * 0^D and ^^D: remove all indent.
6670 */
6671 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6672 {
6673 --curwin->w_cursor.col;
6674 (void)del_char(FALSE); /* delete the '^' or '0' */
6675 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6676 if (State & REPLACE_FLAG)
6677 replace_pop_ins();
6678 if (lastc == '^')
6679 old_indent = get_indent(); /* remember curr. indent */
6680 change_indent(INDENT_SET, 0, TRUE, 0);
6681 }
6682 else
6683 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6684
6685 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6686 did_ai = FALSE;
6687#ifdef FEAT_SMARTINDENT
6688 did_si = FALSE;
6689 can_si = FALSE;
6690 can_si_back = FALSE;
6691#endif
6692#ifdef FEAT_CINDENT
6693 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6694#endif
6695}
6696
6697 static void
6698ins_del()
6699{
6700 int temp;
6701
6702 if (stop_arrow() == FAIL)
6703 return;
6704 if (gchar_cursor() == NUL) /* delete newline */
6705 {
6706 temp = curwin->w_cursor.col;
6707 if (!can_bs(BS_EOL) /* only if "eol" included */
6708 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6709 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6710 || do_join(FALSE) == FAIL)
6711 vim_beep();
6712 else
6713 curwin->w_cursor.col = temp;
6714 }
6715 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6716 vim_beep();
6717 did_ai = FALSE;
6718#ifdef FEAT_SMARTINDENT
6719 did_si = FALSE;
6720 can_si = FALSE;
6721 can_si_back = FALSE;
6722#endif
6723 AppendCharToRedobuff(K_DEL);
6724}
6725
6726/*
6727 * Handle Backspace, delete-word and delete-line in Insert mode.
6728 * Return TRUE when backspace was actually used.
6729 */
6730 static int
6731ins_bs(c, mode, inserted_space_p)
6732 int c;
6733 int mode;
6734 int *inserted_space_p;
6735{
6736 linenr_T lnum;
6737 int cc;
6738 int temp = 0; /* init for GCC */
6739 colnr_T mincol;
6740 int did_backspace = FALSE;
6741 int in_indent;
6742 int oldState;
6743#ifdef FEAT_MBYTE
6744 int p1, p2;
6745#endif
6746
6747 /*
6748 * can't delete anything in an empty file
6749 * can't backup past first character in buffer
6750 * can't backup past starting point unless 'backspace' > 1
6751 * can backup to a previous line if 'backspace' == 0
6752 */
6753 if ( bufempty()
6754 || (
6755#ifdef FEAT_RIGHTLEFT
6756 !revins_on &&
6757#endif
6758 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
6759 || (!can_bs(BS_START)
6760 && (arrow_used
6761 || (curwin->w_cursor.lnum == Insstart.lnum
6762 && curwin->w_cursor.col <= Insstart.col)))
6763 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
6764 && curwin->w_cursor.col <= ai_col)
6765 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
6766 {
6767 vim_beep();
6768 return FALSE;
6769 }
6770
6771 if (stop_arrow() == FAIL)
6772 return FALSE;
6773 in_indent = inindent(0);
6774#ifdef FEAT_CINDENT
6775 if (in_indent)
6776 can_cindent = FALSE;
6777#endif
6778#ifdef FEAT_COMMENTS
6779 end_comment_pending = NUL; /* After BS, don't auto-end comment */
6780#endif
6781#ifdef FEAT_RIGHTLEFT
6782 if (revins_on) /* put cursor after last inserted char */
6783 inc_cursor();
6784#endif
6785
6786#ifdef FEAT_VIRTUALEDIT
6787 /* Virtualedit:
6788 * BACKSPACE_CHAR eats a virtual space
6789 * BACKSPACE_WORD eats all coladd
6790 * BACKSPACE_LINE eats all coladd and keeps going
6791 */
6792 if (curwin->w_cursor.coladd > 0)
6793 {
6794 if (mode == BACKSPACE_CHAR)
6795 {
6796 --curwin->w_cursor.coladd;
6797 return TRUE;
6798 }
6799 if (mode == BACKSPACE_WORD)
6800 {
6801 curwin->w_cursor.coladd = 0;
6802 return TRUE;
6803 }
6804 curwin->w_cursor.coladd = 0;
6805 }
6806#endif
6807
6808 /*
6809 * delete newline!
6810 */
6811 if (curwin->w_cursor.col == 0)
6812 {
6813 lnum = Insstart.lnum;
6814 if (curwin->w_cursor.lnum == Insstart.lnum
6815#ifdef FEAT_RIGHTLEFT
6816 || revins_on
6817#endif
6818 )
6819 {
6820 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
6821 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
6822 return FALSE;
6823 --Insstart.lnum;
6824 Insstart.col = MAXCOL;
6825 }
6826 /*
6827 * In replace mode:
6828 * cc < 0: NL was inserted, delete it
6829 * cc >= 0: NL was replaced, put original characters back
6830 */
6831 cc = -1;
6832 if (State & REPLACE_FLAG)
6833 cc = replace_pop(); /* returns -1 if NL was inserted */
6834 /*
6835 * In replace mode, in the line we started replacing, we only move the
6836 * cursor.
6837 */
6838 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
6839 {
6840 dec_cursor();
6841 }
6842 else
6843 {
6844#ifdef FEAT_VREPLACE
6845 if (!(State & VREPLACE_FLAG)
6846 || curwin->w_cursor.lnum > orig_line_count)
6847#endif
6848 {
6849 temp = gchar_cursor(); /* remember current char */
6850 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00006851
6852 /* When "aw" is in 'formatoptions' we must delete the space at
6853 * the end of the line, otherwise the line will be broken
6854 * again when auto-formatting. */
6855 if (has_format_option(FO_AUTO)
6856 && has_format_option(FO_WHITE_PAR))
6857 {
6858 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
6859 TRUE);
6860 int len;
6861
6862 len = STRLEN(ptr);
6863 if (len > 0 && ptr[len - 1] == ' ')
6864 ptr[len - 1] = NUL;
6865 }
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 (void)do_join(FALSE);
6868 if (temp == NUL && gchar_cursor() != NUL)
6869 inc_cursor();
6870 }
6871#ifdef FEAT_VREPLACE
6872 else
6873 dec_cursor();
6874#endif
6875
6876 /*
6877 * In REPLACE mode we have to put back the text that was replaced
6878 * by the NL. On the replace stack is first a NUL-terminated
6879 * sequence of characters that were deleted and then the
6880 * characters that NL replaced.
6881 */
6882 if (State & REPLACE_FLAG)
6883 {
6884 /*
6885 * Do the next ins_char() in NORMAL state, to
6886 * prevent ins_char() from replacing characters and
6887 * avoiding showmatch().
6888 */
6889 oldState = State;
6890 State = NORMAL;
6891 /*
6892 * restore characters (blanks) deleted after cursor
6893 */
6894 while (cc > 0)
6895 {
6896 temp = curwin->w_cursor.col;
6897#ifdef FEAT_MBYTE
6898 mb_replace_pop_ins(cc);
6899#else
6900 ins_char(cc);
6901#endif
6902 curwin->w_cursor.col = temp;
6903 cc = replace_pop();
6904 }
6905 /* restore the characters that NL replaced */
6906 replace_pop_ins();
6907 State = oldState;
6908 }
6909 }
6910 did_ai = FALSE;
6911 }
6912 else
6913 {
6914 /*
6915 * Delete character(s) before the cursor.
6916 */
6917#ifdef FEAT_RIGHTLEFT
6918 if (revins_on) /* put cursor on last inserted char */
6919 dec_cursor();
6920#endif
6921 mincol = 0;
6922 /* keep indent */
6923 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
6924#ifdef FEAT_RIGHTLEFT
6925 && !revins_on
6926#endif
6927 )
6928 {
6929 temp = curwin->w_cursor.col;
6930 beginline(BL_WHITE);
6931 if (curwin->w_cursor.col < (colnr_T)temp)
6932 mincol = curwin->w_cursor.col;
6933 curwin->w_cursor.col = temp;
6934 }
6935
6936 /*
6937 * Handle deleting one 'shiftwidth' or 'softtabstop'.
6938 */
6939 if ( mode == BACKSPACE_CHAR
6940 && ((p_sta && in_indent)
6941 || (curbuf->b_p_sts
6942 && (*(ml_get_cursor() - 1) == TAB
6943 || (*(ml_get_cursor() - 1) == ' '
6944 && (!*inserted_space_p
6945 || arrow_used))))))
6946 {
6947 int ts;
6948 colnr_T vcol;
6949 colnr_T want_vcol;
6950 int extra = 0;
6951
6952 *inserted_space_p = FALSE;
6953 if (p_sta)
6954 ts = curbuf->b_p_sw;
6955 else
6956 ts = curbuf->b_p_sts;
6957 /* Compute the virtual column where we want to be. Since
6958 * 'showbreak' may get in the way, need to get the last column of
6959 * the previous character. */
6960 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6961 dec_cursor();
6962 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
6963 inc_cursor();
6964 want_vcol = (want_vcol / ts) * ts;
6965
6966 /* delete characters until we are at or before want_vcol */
6967 while (vcol > want_vcol
6968 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
6969 {
6970 dec_cursor();
6971 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6972 if (State & REPLACE_FLAG)
6973 {
6974 /* Don't delete characters before the insert point when in
6975 * Replace mode */
6976 if (curwin->w_cursor.lnum != Insstart.lnum
6977 || curwin->w_cursor.col >= Insstart.col)
6978 {
6979#if 0 /* what was this for? It causes problems when sw != ts. */
6980 if (State == REPLACE && (int)vcol < want_vcol)
6981 {
6982 (void)del_char(FALSE);
6983 extra = 2; /* don't pop too much */
6984 }
6985 else
6986#endif
6987 replace_do_bs();
6988 }
6989 }
6990 else
6991 (void)del_char(FALSE);
6992 }
6993
6994 /* insert extra spaces until we are at want_vcol */
6995 while (vcol < want_vcol)
6996 {
6997 /* Remember the first char we inserted */
6998 if (curwin->w_cursor.lnum == Insstart.lnum
6999 && curwin->w_cursor.col < Insstart.col)
7000 Insstart.col = curwin->w_cursor.col;
7001
7002#ifdef FEAT_VREPLACE
7003 if (State & VREPLACE_FLAG)
7004 ins_char(' ');
7005 else
7006#endif
7007 {
7008 ins_str((char_u *)" ");
7009 if ((State & REPLACE_FLAG) && extra <= 1)
7010 {
7011 if (extra)
7012 replace_push_off(NUL);
7013 else
7014 replace_push(NUL);
7015 }
7016 if (extra == 2)
7017 extra = 1;
7018 }
7019 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7020 }
7021 }
7022
7023 /*
7024 * Delete upto starting point, start of line or previous word.
7025 */
7026 else do
7027 {
7028#ifdef FEAT_RIGHTLEFT
7029 if (!revins_on) /* put cursor on char to be deleted */
7030#endif
7031 dec_cursor();
7032
7033 /* start of word? */
7034 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7035 {
7036 mode = BACKSPACE_WORD_NOT_SPACE;
7037 temp = vim_iswordc(gchar_cursor());
7038 }
7039 /* end of word? */
7040 else if (mode == BACKSPACE_WORD_NOT_SPACE
7041 && (vim_isspace(cc = gchar_cursor())
7042 || vim_iswordc(cc) != temp))
7043 {
7044#ifdef FEAT_RIGHTLEFT
7045 if (!revins_on)
7046#endif
7047 inc_cursor();
7048#ifdef FEAT_RIGHTLEFT
7049 else if (State & REPLACE_FLAG)
7050 dec_cursor();
7051#endif
7052 break;
7053 }
7054 if (State & REPLACE_FLAG)
7055 replace_do_bs();
7056 else
7057 {
7058#ifdef FEAT_MBYTE
7059 if (enc_utf8 && p_deco)
7060 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7061#endif
7062 (void)del_char(FALSE);
7063#ifdef FEAT_MBYTE
7064 /*
7065 * If p1 or p2 is non-zero, there are combining characters we
7066 * need to take account of. Don't back up before the base
7067 * character.
7068 */
7069 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7070 inc_cursor();
7071#endif
7072#ifdef FEAT_RIGHTLEFT
7073 if (revins_chars)
7074 {
7075 revins_chars--;
7076 revins_legal++;
7077 }
7078 if (revins_on && gchar_cursor() == NUL)
7079 break;
7080#endif
7081 }
7082 /* Just a single backspace?: */
7083 if (mode == BACKSPACE_CHAR)
7084 break;
7085 } while (
7086#ifdef FEAT_RIGHTLEFT
7087 revins_on ||
7088#endif
7089 (curwin->w_cursor.col > mincol
7090 && (curwin->w_cursor.lnum != Insstart.lnum
7091 || curwin->w_cursor.col != Insstart.col)));
7092 did_backspace = TRUE;
7093 }
7094#ifdef FEAT_SMARTINDENT
7095 did_si = FALSE;
7096 can_si = FALSE;
7097 can_si_back = FALSE;
7098#endif
7099 if (curwin->w_cursor.col <= 1)
7100 did_ai = FALSE;
7101 /*
7102 * It's a little strange to put backspaces into the redo
7103 * buffer, but it makes auto-indent a lot easier to deal
7104 * with.
7105 */
7106 AppendCharToRedobuff(c);
7107
7108 /* If deleted before the insertion point, adjust it */
7109 if (curwin->w_cursor.lnum == Insstart.lnum
7110 && curwin->w_cursor.col < Insstart.col)
7111 Insstart.col = curwin->w_cursor.col;
7112
7113 /* vi behaviour: the cursor moves backward but the character that
7114 * was there remains visible
7115 * Vim behaviour: the cursor moves backward and the character that
7116 * was there is erased from the screen.
7117 * We can emulate the vi behaviour by pretending there is a dollar
7118 * displayed even when there isn't.
7119 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7120 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7121 dollar_vcol = curwin->w_virtcol;
7122
7123 return did_backspace;
7124}
7125
7126#ifdef FEAT_MOUSE
7127 static void
7128ins_mouse(c)
7129 int c;
7130{
7131 pos_T tpos;
7132
7133# ifdef FEAT_GUI
7134 /* When GUI is active, also move/paste when 'mouse' is empty */
7135 if (!gui.in_use)
7136# endif
7137 if (!mouse_has(MOUSE_INSERT))
7138 return;
7139
7140 undisplay_dollar();
7141 tpos = curwin->w_cursor;
7142 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7143 {
7144 start_arrow(&tpos);
7145# ifdef FEAT_CINDENT
7146 can_cindent = TRUE;
7147# endif
7148 }
7149
7150#ifdef FEAT_WINDOWS
7151 /* redraw status lines (in case another window became active) */
7152 redraw_statuslines();
7153#endif
7154}
7155
7156 static void
7157ins_mousescroll(up)
7158 int up;
7159{
7160 pos_T tpos;
7161# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7162 win_T *old_curwin;
7163# endif
7164
7165 tpos = curwin->w_cursor;
7166
7167# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7168 old_curwin = curwin;
7169
7170 /* Currently the mouse coordinates are only known in the GUI. */
7171 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7172 {
7173 int row, col;
7174
7175 row = mouse_row;
7176 col = mouse_col;
7177
7178 /* find the window at the pointer coordinates */
7179 curwin = mouse_find_win(&row, &col);
7180 curbuf = curwin->w_buffer;
7181 }
7182 if (curwin == old_curwin)
7183# endif
7184 undisplay_dollar();
7185
7186 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7187 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7188 else
7189 scroll_redraw(up, 3L);
7190
7191# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7192 curwin->w_redr_status = TRUE;
7193
7194 curwin = old_curwin;
7195 curbuf = curwin->w_buffer;
7196# endif
7197
7198 if (!equalpos(curwin->w_cursor, tpos))
7199 {
7200 start_arrow(&tpos);
7201# ifdef FEAT_CINDENT
7202 can_cindent = TRUE;
7203# endif
7204 }
7205}
7206#endif
7207
7208#ifdef FEAT_GUI
7209 void
7210ins_scroll()
7211{
7212 pos_T tpos;
7213
7214 undisplay_dollar();
7215 tpos = curwin->w_cursor;
7216 if (gui_do_scroll())
7217 {
7218 start_arrow(&tpos);
7219# ifdef FEAT_CINDENT
7220 can_cindent = TRUE;
7221# endif
7222 }
7223}
7224
7225 void
7226ins_horscroll()
7227{
7228 pos_T tpos;
7229
7230 undisplay_dollar();
7231 tpos = curwin->w_cursor;
7232 if (gui_do_horiz_scroll())
7233 {
7234 start_arrow(&tpos);
7235# ifdef FEAT_CINDENT
7236 can_cindent = TRUE;
7237# endif
7238 }
7239}
7240#endif
7241
7242 static void
7243ins_left()
7244{
7245 pos_T tpos;
7246
7247#ifdef FEAT_FOLDING
7248 if ((fdo_flags & FDO_HOR) && KeyTyped)
7249 foldOpenCursor();
7250#endif
7251 undisplay_dollar();
7252 tpos = curwin->w_cursor;
7253 if (oneleft() == OK)
7254 {
7255 start_arrow(&tpos);
7256#ifdef FEAT_RIGHTLEFT
7257 /* If exit reversed string, position is fixed */
7258 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7259 revins_legal++;
7260 revins_chars++;
7261#endif
7262 }
7263
7264 /*
7265 * if 'whichwrap' set for cursor in insert mode may go to
7266 * previous line
7267 */
7268 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7269 {
7270 start_arrow(&tpos);
7271 --(curwin->w_cursor.lnum);
7272 coladvance((colnr_T)MAXCOL);
7273 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7274 }
7275 else
7276 vim_beep();
7277}
7278
7279 static void
7280ins_home(c)
7281 int c;
7282{
7283 pos_T tpos;
7284
7285#ifdef FEAT_FOLDING
7286 if ((fdo_flags & FDO_HOR) && KeyTyped)
7287 foldOpenCursor();
7288#endif
7289 undisplay_dollar();
7290 tpos = curwin->w_cursor;
7291 if (c == K_C_HOME)
7292 curwin->w_cursor.lnum = 1;
7293 curwin->w_cursor.col = 0;
7294#ifdef FEAT_VIRTUALEDIT
7295 curwin->w_cursor.coladd = 0;
7296#endif
7297 curwin->w_curswant = 0;
7298 start_arrow(&tpos);
7299}
7300
7301 static void
7302ins_end(c)
7303 int c;
7304{
7305 pos_T tpos;
7306
7307#ifdef FEAT_FOLDING
7308 if ((fdo_flags & FDO_HOR) && KeyTyped)
7309 foldOpenCursor();
7310#endif
7311 undisplay_dollar();
7312 tpos = curwin->w_cursor;
7313 if (c == K_C_END)
7314 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7315 coladvance((colnr_T)MAXCOL);
7316 curwin->w_curswant = MAXCOL;
7317
7318 start_arrow(&tpos);
7319}
7320
7321 static void
7322ins_s_left()
7323{
7324#ifdef FEAT_FOLDING
7325 if ((fdo_flags & FDO_HOR) && KeyTyped)
7326 foldOpenCursor();
7327#endif
7328 undisplay_dollar();
7329 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7330 {
7331 start_arrow(&curwin->w_cursor);
7332 (void)bck_word(1L, FALSE, FALSE);
7333 curwin->w_set_curswant = TRUE;
7334 }
7335 else
7336 vim_beep();
7337}
7338
7339 static void
7340ins_right()
7341{
7342#ifdef FEAT_FOLDING
7343 if ((fdo_flags & FDO_HOR) && KeyTyped)
7344 foldOpenCursor();
7345#endif
7346 undisplay_dollar();
7347 if (gchar_cursor() != NUL || virtual_active()
7348 )
7349 {
7350 start_arrow(&curwin->w_cursor);
7351 curwin->w_set_curswant = TRUE;
7352#ifdef FEAT_VIRTUALEDIT
7353 if (virtual_active())
7354 oneright();
7355 else
7356#endif
7357 {
7358#ifdef FEAT_MBYTE
7359 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007360 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else
7362#endif
7363 ++curwin->w_cursor.col;
7364 }
7365
7366#ifdef FEAT_RIGHTLEFT
7367 revins_legal++;
7368 if (revins_chars)
7369 revins_chars--;
7370#endif
7371 }
7372 /* if 'whichwrap' set for cursor in insert mode, may move the
7373 * cursor to the next line */
7374 else if (vim_strchr(p_ww, ']') != NULL
7375 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7376 {
7377 start_arrow(&curwin->w_cursor);
7378 curwin->w_set_curswant = TRUE;
7379 ++curwin->w_cursor.lnum;
7380 curwin->w_cursor.col = 0;
7381 }
7382 else
7383 vim_beep();
7384}
7385
7386 static void
7387ins_s_right()
7388{
7389#ifdef FEAT_FOLDING
7390 if ((fdo_flags & FDO_HOR) && KeyTyped)
7391 foldOpenCursor();
7392#endif
7393 undisplay_dollar();
7394 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7395 || gchar_cursor() != NUL)
7396 {
7397 start_arrow(&curwin->w_cursor);
7398 (void)fwd_word(1L, FALSE, 0);
7399 curwin->w_set_curswant = TRUE;
7400 }
7401 else
7402 vim_beep();
7403}
7404
7405 static void
7406ins_up(startcol)
7407 int startcol; /* when TRUE move to Insstart.col */
7408{
7409 pos_T tpos;
7410 linenr_T old_topline = curwin->w_topline;
7411#ifdef FEAT_DIFF
7412 int old_topfill = curwin->w_topfill;
7413#endif
7414
7415 undisplay_dollar();
7416 tpos = curwin->w_cursor;
7417 if (cursor_up(1L, TRUE) == OK)
7418 {
7419 if (startcol)
7420 coladvance(getvcol_nolist(&Insstart));
7421 if (old_topline != curwin->w_topline
7422#ifdef FEAT_DIFF
7423 || old_topfill != curwin->w_topfill
7424#endif
7425 )
7426 redraw_later(VALID);
7427 start_arrow(&tpos);
7428#ifdef FEAT_CINDENT
7429 can_cindent = TRUE;
7430#endif
7431 }
7432 else
7433 vim_beep();
7434}
7435
7436 static void
7437ins_pageup()
7438{
7439 pos_T tpos;
7440
7441 undisplay_dollar();
7442 tpos = curwin->w_cursor;
7443 if (onepage(BACKWARD, 1L) == OK)
7444 {
7445 start_arrow(&tpos);
7446#ifdef FEAT_CINDENT
7447 can_cindent = TRUE;
7448#endif
7449 }
7450 else
7451 vim_beep();
7452}
7453
7454 static void
7455ins_down(startcol)
7456 int startcol; /* when TRUE move to Insstart.col */
7457{
7458 pos_T tpos;
7459 linenr_T old_topline = curwin->w_topline;
7460#ifdef FEAT_DIFF
7461 int old_topfill = curwin->w_topfill;
7462#endif
7463
7464 undisplay_dollar();
7465 tpos = curwin->w_cursor;
7466 if (cursor_down(1L, TRUE) == OK)
7467 {
7468 if (startcol)
7469 coladvance(getvcol_nolist(&Insstart));
7470 if (old_topline != curwin->w_topline
7471#ifdef FEAT_DIFF
7472 || old_topfill != curwin->w_topfill
7473#endif
7474 )
7475 redraw_later(VALID);
7476 start_arrow(&tpos);
7477#ifdef FEAT_CINDENT
7478 can_cindent = TRUE;
7479#endif
7480 }
7481 else
7482 vim_beep();
7483}
7484
7485 static void
7486ins_pagedown()
7487{
7488 pos_T tpos;
7489
7490 undisplay_dollar();
7491 tpos = curwin->w_cursor;
7492 if (onepage(FORWARD, 1L) == OK)
7493 {
7494 start_arrow(&tpos);
7495#ifdef FEAT_CINDENT
7496 can_cindent = TRUE;
7497#endif
7498 }
7499 else
7500 vim_beep();
7501}
7502
7503#ifdef FEAT_DND
7504 static void
7505ins_drop()
7506{
7507 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7508}
7509#endif
7510
7511/*
7512 * Handle TAB in Insert or Replace mode.
7513 * Return TRUE when the TAB needs to be inserted like a normal character.
7514 */
7515 static int
7516ins_tab()
7517{
7518 int ind;
7519 int i;
7520 int temp;
7521
7522 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7523 Insstart_blank_vcol = get_nolist_virtcol();
7524 if (echeck_abbr(TAB + ABBR_OFF))
7525 return FALSE;
7526
7527 ind = inindent(0);
7528#ifdef FEAT_CINDENT
7529 if (ind)
7530 can_cindent = FALSE;
7531#endif
7532
7533 /*
7534 * When nothing special, insert TAB like a normal character
7535 */
7536 if (!curbuf->b_p_et
7537 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7538 && curbuf->b_p_sts == 0)
7539 return TRUE;
7540
7541 if (stop_arrow() == FAIL)
7542 return TRUE;
7543
7544 did_ai = FALSE;
7545#ifdef FEAT_SMARTINDENT
7546 did_si = FALSE;
7547 can_si = FALSE;
7548 can_si_back = FALSE;
7549#endif
7550 AppendToRedobuff((char_u *)"\t");
7551
7552 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7553 temp = (int)curbuf->b_p_sw;
7554 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7555 temp = (int)curbuf->b_p_sts;
7556 else /* otherwise use 'tabstop' */
7557 temp = (int)curbuf->b_p_ts;
7558 temp -= get_nolist_virtcol() % temp;
7559
7560 /*
7561 * Insert the first space with ins_char(). It will delete one char in
7562 * replace mode. Insert the rest with ins_str(); it will not delete any
7563 * chars. For VREPLACE mode, we use ins_char() for all characters.
7564 */
7565 ins_char(' ');
7566 while (--temp > 0)
7567 {
7568#ifdef FEAT_VREPLACE
7569 if (State & VREPLACE_FLAG)
7570 ins_char(' ');
7571 else
7572#endif
7573 {
7574 ins_str((char_u *)" ");
7575 if (State & REPLACE_FLAG) /* no char replaced */
7576 replace_push(NUL);
7577 }
7578 }
7579
7580 /*
7581 * When 'expandtab' not set: Replace spaces by TABs where possible.
7582 */
7583 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7584 {
7585 char_u *ptr;
7586#ifdef FEAT_VREPLACE
7587 char_u *saved_line = NULL; /* init for GCC */
7588 pos_T pos;
7589#endif
7590 pos_T fpos;
7591 pos_T *cursor;
7592 colnr_T want_vcol, vcol;
7593 int change_col = -1;
7594 int save_list = curwin->w_p_list;
7595
7596 /*
7597 * Get the current line. For VREPLACE mode, don't make real changes
7598 * yet, just work on a copy of the line.
7599 */
7600#ifdef FEAT_VREPLACE
7601 if (State & VREPLACE_FLAG)
7602 {
7603 pos = curwin->w_cursor;
7604 cursor = &pos;
7605 saved_line = vim_strsave(ml_get_curline());
7606 if (saved_line == NULL)
7607 return FALSE;
7608 ptr = saved_line + pos.col;
7609 }
7610 else
7611#endif
7612 {
7613 ptr = ml_get_cursor();
7614 cursor = &curwin->w_cursor;
7615 }
7616
7617 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7618 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7619 curwin->w_p_list = FALSE;
7620
7621 /* Find first white before the cursor */
7622 fpos = curwin->w_cursor;
7623 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7624 {
7625 --fpos.col;
7626 --ptr;
7627 }
7628
7629 /* In Replace mode, don't change characters before the insert point. */
7630 if ((State & REPLACE_FLAG)
7631 && fpos.lnum == Insstart.lnum
7632 && fpos.col < Insstart.col)
7633 {
7634 ptr += Insstart.col - fpos.col;
7635 fpos.col = Insstart.col;
7636 }
7637
7638 /* compute virtual column numbers of first white and cursor */
7639 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7640 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7641
7642 /* Use as many TABs as possible. Beware of 'showbreak' and
7643 * 'linebreak' adding extra virtual columns. */
7644 while (vim_iswhite(*ptr))
7645 {
7646 i = lbr_chartabsize((char_u *)"\t", vcol);
7647 if (vcol + i > want_vcol)
7648 break;
7649 if (*ptr != TAB)
7650 {
7651 *ptr = TAB;
7652 if (change_col < 0)
7653 {
7654 change_col = fpos.col; /* Column of first change */
7655 /* May have to adjust Insstart */
7656 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7657 Insstart.col = fpos.col;
7658 }
7659 }
7660 ++fpos.col;
7661 ++ptr;
7662 vcol += i;
7663 }
7664
7665 if (change_col >= 0)
7666 {
7667 int repl_off = 0;
7668
7669 /* Skip over the spaces we need. */
7670 while (vcol < want_vcol && *ptr == ' ')
7671 {
7672 vcol += lbr_chartabsize(ptr, vcol);
7673 ++ptr;
7674 ++repl_off;
7675 }
7676 if (vcol > want_vcol)
7677 {
7678 /* Must have a char with 'showbreak' just before it. */
7679 --ptr;
7680 --repl_off;
7681 }
7682 fpos.col += repl_off;
7683
7684 /* Delete following spaces. */
7685 i = cursor->col - fpos.col;
7686 if (i > 0)
7687 {
7688 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7689 /* correct replace stack. */
7690 if ((State & REPLACE_FLAG)
7691#ifdef FEAT_VREPLACE
7692 && !(State & VREPLACE_FLAG)
7693#endif
7694 )
7695 for (temp = i; --temp >= 0; )
7696 replace_join(repl_off);
7697 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007698#ifdef FEAT_NETBEANS_INTG
7699 if (usingNetbeans)
7700 {
7701 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7702 (long)(i + 1));
7703 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7704 (char_u *)"\t", 1);
7705 }
7706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 cursor->col -= i;
7708
7709#ifdef FEAT_VREPLACE
7710 /*
7711 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7712 * backspacing over the changed spacing and then inserting the new
7713 * spacing.
7714 */
7715 if (State & VREPLACE_FLAG)
7716 {
7717 /* Backspace from real cursor to change_col */
7718 backspace_until_column(change_col);
7719
7720 /* Insert each char in saved_line from changed_col to
7721 * ptr-cursor */
7722 ins_bytes_len(saved_line + change_col,
7723 cursor->col - change_col);
7724 }
7725#endif
7726 }
7727
7728#ifdef FEAT_VREPLACE
7729 if (State & VREPLACE_FLAG)
7730 vim_free(saved_line);
7731#endif
7732 curwin->w_p_list = save_list;
7733 }
7734
7735 return FALSE;
7736}
7737
7738/*
7739 * Handle CR or NL in insert mode.
7740 * Return TRUE when out of memory or can't undo.
7741 */
7742 static int
7743ins_eol(c)
7744 int c;
7745{
7746 int i;
7747
7748 if (echeck_abbr(c + ABBR_OFF))
7749 return FALSE;
7750 if (stop_arrow() == FAIL)
7751 return TRUE;
7752 undisplay_dollar();
7753
7754 /*
7755 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
7756 * character under the cursor. Only push a NUL on the replace stack,
7757 * nothing to put back when the NL is deleted.
7758 */
7759 if ((State & REPLACE_FLAG)
7760#ifdef FEAT_VREPLACE
7761 && !(State & VREPLACE_FLAG)
7762#endif
7763 )
7764 replace_push(NUL);
7765
7766 /*
7767 * In VREPLACE mode, a NL replaces the rest of the line, and starts
7768 * replacing the next line, so we push all of the characters left on the
7769 * line onto the replace stack. This is not done here though, it is done
7770 * in open_line().
7771 */
7772
7773#ifdef FEAT_RIGHTLEFT
7774# ifdef FEAT_FKMAP
7775 if (p_altkeymap && p_fkmap)
7776 fkmap(NL);
7777# endif
7778 /* NL in reverse insert will always start in the end of
7779 * current line. */
7780 if (revins_on)
7781 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7782#endif
7783
7784 AppendToRedobuff(NL_STR);
7785 i = open_line(FORWARD,
7786#ifdef FEAT_COMMENTS
7787 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
7788#endif
7789 0, old_indent);
7790 old_indent = 0;
7791#ifdef FEAT_CINDENT
7792 can_cindent = TRUE;
7793#endif
7794
7795 return (!i);
7796}
7797
7798#ifdef FEAT_DIGRAPHS
7799/*
7800 * Handle digraph in insert mode.
7801 * Returns character still to be inserted, or NUL when nothing remaining to be
7802 * done.
7803 */
7804 static int
7805ins_digraph()
7806{
7807 int c;
7808 int cc;
7809
7810 pc_status = PC_STATUS_UNSET;
7811 if (redrawing() && !char_avail())
7812 {
7813 /* may need to redraw when no more chars available now */
7814 ins_redraw();
7815
7816 edit_putchar('?', TRUE);
7817#ifdef FEAT_CMDL_INFO
7818 add_to_showcmd_c(Ctrl_K);
7819#endif
7820 }
7821
7822#ifdef USE_ON_FLY_SCROLL
7823 dont_scroll = TRUE; /* disallow scrolling here */
7824#endif
7825
7826 /* don't map the digraph chars. This also prevents the
7827 * mode message to be deleted when ESC is hit */
7828 ++no_mapping;
7829 ++allow_keys;
7830 c = safe_vgetc();
7831 --no_mapping;
7832 --allow_keys;
7833 if (IS_SPECIAL(c) || mod_mask) /* special key */
7834 {
7835#ifdef FEAT_CMDL_INFO
7836 clear_showcmd();
7837#endif
7838 insert_special(c, TRUE, FALSE);
7839 return NUL;
7840 }
7841 if (c != ESC)
7842 {
7843 if (redrawing() && !char_avail())
7844 {
7845 /* may need to redraw when no more chars available now */
7846 ins_redraw();
7847
7848 if (char2cells(c) == 1)
7849 {
7850 /* first remove the '?', otherwise it's restored when typing
7851 * an ESC next */
7852 edit_unputchar();
7853 ins_redraw();
7854 edit_putchar(c, TRUE);
7855 }
7856#ifdef FEAT_CMDL_INFO
7857 add_to_showcmd_c(c);
7858#endif
7859 }
7860 ++no_mapping;
7861 ++allow_keys;
7862 cc = safe_vgetc();
7863 --no_mapping;
7864 --allow_keys;
7865 if (cc != ESC)
7866 {
7867 AppendToRedobuff((char_u *)CTRL_V_STR);
7868 c = getdigraph(c, cc, TRUE);
7869#ifdef FEAT_CMDL_INFO
7870 clear_showcmd();
7871#endif
7872 return c;
7873 }
7874 }
7875 edit_unputchar();
7876#ifdef FEAT_CMDL_INFO
7877 clear_showcmd();
7878#endif
7879 return NUL;
7880}
7881#endif
7882
7883/*
7884 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
7885 * Returns the char to be inserted, or NUL if none found.
7886 */
7887 static int
7888ins_copychar(lnum)
7889 linenr_T lnum;
7890{
7891 int c;
7892 int temp;
7893 char_u *ptr, *prev_ptr;
7894
7895 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7896 {
7897 vim_beep();
7898 return NUL;
7899 }
7900
7901 /* try to advance to the cursor column */
7902 temp = 0;
7903 ptr = ml_get(lnum);
7904 prev_ptr = ptr;
7905 validate_virtcol();
7906 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
7907 {
7908 prev_ptr = ptr;
7909 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
7910 }
7911 if ((colnr_T)temp > curwin->w_virtcol)
7912 ptr = prev_ptr;
7913
7914#ifdef FEAT_MBYTE
7915 c = (*mb_ptr2char)(ptr);
7916#else
7917 c = *ptr;
7918#endif
7919 if (c == NUL)
7920 vim_beep();
7921 return c;
7922}
7923
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007924/*
7925 * CTRL-Y or CTRL-E typed in Insert mode.
7926 */
7927 static int
7928ins_ctrl_ey(tc)
7929 int tc;
7930{
7931 int c = tc;
7932
7933#ifdef FEAT_INS_EXPAND
7934 if (ctrl_x_mode == CTRL_X_SCROLL)
7935 {
7936 if (c == Ctrl_Y)
7937 scrolldown_clamp();
7938 else
7939 scrollup_clamp();
7940 redraw_later(VALID);
7941 }
7942 else
7943#endif
7944 {
7945 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
7946 if (c != NUL)
7947 {
7948 long tw_save;
7949
7950 /* The character must be taken literally, insert like it
7951 * was typed after a CTRL-V, and pretend 'textwidth'
7952 * wasn't set. Digits, 'o' and 'x' are special after a
7953 * CTRL-V, don't use it for these. */
7954 if (c < 256 && !isalnum(c))
7955 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
7956 tw_save = curbuf->b_p_tw;
7957 curbuf->b_p_tw = -1;
7958 insert_special(c, TRUE, FALSE);
7959 curbuf->b_p_tw = tw_save;
7960#ifdef FEAT_RIGHTLEFT
7961 revins_chars++;
7962 revins_legal++;
7963#endif
7964 c = Ctrl_V; /* pretend CTRL-V is last character */
7965 auto_format(FALSE, TRUE);
7966 }
7967 }
7968 return c;
7969}
7970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#ifdef FEAT_SMARTINDENT
7972/*
7973 * Try to do some very smart auto-indenting.
7974 * Used when inserting a "normal" character.
7975 */
7976 static void
7977ins_try_si(c)
7978 int c;
7979{
7980 pos_T *pos, old_pos;
7981 char_u *ptr;
7982 int i;
7983 int temp;
7984
7985 /*
7986 * do some very smart indenting when entering '{' or '}'
7987 */
7988 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
7989 {
7990 /*
7991 * for '}' set indent equal to indent of line containing matching '{'
7992 */
7993 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
7994 {
7995 old_pos = curwin->w_cursor;
7996 /*
7997 * If the matching '{' has a ')' immediately before it (ignoring
7998 * white-space), then line up with the start of the line
7999 * containing the matching '(' if there is one. This handles the
8000 * case where an "if (..\n..) {" statement continues over multiple
8001 * lines -- webb
8002 */
8003 ptr = ml_get(pos->lnum);
8004 i = pos->col;
8005 if (i > 0) /* skip blanks before '{' */
8006 while (--i > 0 && vim_iswhite(ptr[i]))
8007 ;
8008 curwin->w_cursor.lnum = pos->lnum;
8009 curwin->w_cursor.col = i;
8010 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8011 curwin->w_cursor = *pos;
8012 i = get_indent();
8013 curwin->w_cursor = old_pos;
8014#ifdef FEAT_VREPLACE
8015 if (State & VREPLACE_FLAG)
8016 change_indent(INDENT_SET, i, FALSE, NUL);
8017 else
8018#endif
8019 (void)set_indent(i, SIN_CHANGED);
8020 }
8021 else if (curwin->w_cursor.col > 0)
8022 {
8023 /*
8024 * when inserting '{' after "O" reduce indent, but not
8025 * more than indent of previous line
8026 */
8027 temp = TRUE;
8028 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8029 {
8030 old_pos = curwin->w_cursor;
8031 i = get_indent();
8032 while (curwin->w_cursor.lnum > 1)
8033 {
8034 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8035
8036 /* ignore empty lines and lines starting with '#'. */
8037 if (*ptr != '#' && *ptr != NUL)
8038 break;
8039 }
8040 if (get_indent() >= i)
8041 temp = FALSE;
8042 curwin->w_cursor = old_pos;
8043 }
8044 if (temp)
8045 shift_line(TRUE, FALSE, 1);
8046 }
8047 }
8048
8049 /*
8050 * set indent of '#' always to 0
8051 */
8052 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8053 {
8054 /* remember current indent for next line */
8055 old_indent = get_indent();
8056 (void)set_indent(0, SIN_CHANGED);
8057 }
8058
8059 /* Adjust ai_col, the char at this position can be deleted. */
8060 if (ai_col > curwin->w_cursor.col)
8061 ai_col = curwin->w_cursor.col;
8062}
8063#endif
8064
8065/*
8066 * Get the value that w_virtcol would have when 'list' is off.
8067 * Unless 'cpo' contains the 'L' flag.
8068 */
8069 static colnr_T
8070get_nolist_virtcol()
8071{
8072 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8073 return getvcol_nolist(&curwin->w_cursor);
8074 validate_virtcol();
8075 return curwin->w_virtcol;
8076}