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