1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
11 * edit.c: functions for Insert mode
16 #ifdef FEAT_INS_EXPAND
18 * definitions used for CTRL-X submode
20 #define CTRL_X_WANT_IDENT 0x100
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
33 #define CTRL_X_FUNCTION 12
34 #define CTRL_X_OMNI 13
35 #define CTRL_X_SPELL 14
36 #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38 #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
40 static char *ctrl_x_msgs
[] =
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
43 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
45 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
53 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
55 N_(" Omni completion (^O^N^P)"),
56 N_(" Spelling suggestion (s^N^P)"),
57 N_(" Keyword Local completion (^N^P)"),
60 static char_u e_hitend
[] = N_("Hit end of paragraph");
63 * Structure used to store one match for insert completion.
65 typedef struct compl_S compl_T
;
70 char_u
*cp_str
; /* matched text */
71 char cp_icase
; /* TRUE or FALSE: ignore case */
72 char_u
*(cp_text
[CPT_COUNT
]); /* text for the menu */
73 char_u
*cp_fname
; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
75 int cp_flags
; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number
; /* sequence number */
79 #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
80 #define FREE_FNAME (2)
83 * All the current matches are stored in a list.
84 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
89 static compl_T
*compl_first_match
= NULL
;
90 static compl_T
*compl_curr_match
= NULL
;
91 static compl_T
*compl_shown_match
= NULL
;
93 /* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95 static int compl_enter_selects
= FALSE
;
97 /* When "compl_leader" is not NULL only matches that start with this string
99 static char_u
*compl_leader
= NULL
;
101 static int compl_get_longest
= FALSE
; /* put longest common string
104 static int compl_used_match
; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
108 /* When the first completion is done "compl_started" is set. When it's
109 * FALSE the word to be completed must be located. */
110 static int compl_started
= FALSE
;
112 static int compl_matches
= 0;
113 static char_u
*compl_pattern
= NULL
;
114 static int compl_direction
= FORWARD
;
115 static int compl_shows_dir
= FORWARD
;
116 static int compl_pending
= 0; /* > 1 for postponed CTRL-N */
117 static pos_T compl_startpos
;
118 static colnr_T compl_col
= 0; /* column where the text starts
119 * that is being completed */
120 static char_u
*compl_orig_text
= NULL
; /* text as it was before
121 * completion started */
122 static int compl_cont_mode
= 0;
123 static expand_T compl_xp
;
125 static void ins_ctrl_x
__ARGS((void));
126 static int has_compl_option
__ARGS((int dict_opt
));
127 static int ins_compl_add
__ARGS((char_u
*str
, int len
, int icase
, char_u
*fname
, char_u
**cptext
, int cdir
, int flags
, int dup
));
128 static int ins_compl_equal
__ARGS((compl_T
*match
, char_u
*str
, int len
));
129 static void ins_compl_longest_match
__ARGS((compl_T
*match
));
130 static void ins_compl_add_matches
__ARGS((int num_matches
, char_u
**matches
, int icase
));
131 static int ins_compl_make_cyclic
__ARGS((void));
132 static void ins_compl_upd_pum
__ARGS((void));
133 static void ins_compl_del_pum
__ARGS((void));
134 static int pum_wanted
__ARGS((void));
135 static int pum_enough_matches
__ARGS((void));
136 static void ins_compl_dictionaries
__ARGS((char_u
*dict
, char_u
*pat
, int flags
, int thesaurus
));
137 static void ins_compl_files
__ARGS((int count
, char_u
**files
, int thesaurus
, int flags
, regmatch_T
*regmatch
, char_u
*buf
, int *dir
));
138 static char_u
*find_line_end
__ARGS((char_u
*ptr
));
139 static void ins_compl_free
__ARGS((void));
140 static void ins_compl_clear
__ARGS((void));
141 static int ins_compl_bs
__ARGS((void));
142 static void ins_compl_addleader
__ARGS((int c
));
143 static void ins_compl_set_original_text
__ARGS((char_u
*str
));
144 static void ins_compl_addfrommatch
__ARGS((void));
145 static int ins_compl_prep
__ARGS((int c
));
146 static buf_T
*ins_compl_next_buf
__ARGS((buf_T
*buf
, int flag
));
147 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
148 static void ins_compl_add_list
__ARGS((list_T
*list
));
150 static int ins_compl_get_exp
__ARGS((pos_T
*ini
));
151 static void ins_compl_delete
__ARGS((void));
152 static void ins_compl_insert
__ARGS((void));
153 static int ins_compl_next
__ARGS((int allow_get_expansion
, int count
, int insert_match
));
154 static int ins_compl_key2dir
__ARGS((int c
));
155 static int ins_compl_pum_key
__ARGS((int c
));
156 static int ins_compl_key2count
__ARGS((int c
));
157 static int ins_compl_use_match
__ARGS((int c
));
158 static int ins_complete
__ARGS((int c
));
159 static int quote_meta
__ARGS((char_u
*dest
, char_u
*str
, int len
));
160 #endif /* FEAT_INS_EXPAND */
162 #define BACKSPACE_CHAR 1
163 #define BACKSPACE_WORD 2
164 #define BACKSPACE_WORD_NOT_SPACE 3
165 #define BACKSPACE_LINE 4
167 static void ins_redraw
__ARGS((int ready
));
168 static void ins_ctrl_v
__ARGS((void));
169 static void undisplay_dollar
__ARGS((void));
170 static void insert_special
__ARGS((int, int, int));
171 static void internal_format
__ARGS((int textwidth
, int second_indent
, int flags
, int format_only
));
172 static void check_auto_format
__ARGS((int));
173 static void redo_literal
__ARGS((int c
));
174 static void start_arrow
__ARGS((pos_T
*end_insert_pos
));
176 static void check_spell_redraw
__ARGS((void));
177 static void spell_back_to_badword
__ARGS((void));
178 static int spell_bad_len
= 0; /* length of located bad word */
180 static void stop_insert
__ARGS((pos_T
*end_insert_pos
, int esc
));
181 static int echeck_abbr
__ARGS((int));
183 static void replace_push_off
__ARGS((int c
));
185 static int replace_pop
__ARGS((void));
186 static void replace_join
__ARGS((int off
));
187 static void replace_pop_ins
__ARGS((void));
189 static void mb_replace_pop_ins
__ARGS((int cc
));
191 static void replace_flush
__ARGS((void));
192 static void replace_do_bs
__ARGS((void));
194 static int cindent_on
__ARGS((void));
196 static void ins_reg
__ARGS((void));
197 static void ins_ctrl_g
__ARGS((void));
198 static void ins_ctrl_hat
__ARGS((void));
199 static int ins_esc
__ARGS((long *count
, int cmdchar
, int nomove
));
200 #ifdef FEAT_RIGHTLEFT
201 static void ins_ctrl_
__ARGS((void));
204 static int ins_start_select
__ARGS((int c
));
206 static void ins_insert
__ARGS((int replaceState
));
207 static void ins_ctrl_o
__ARGS((void));
208 static void ins_shift
__ARGS((int c
, int lastc
));
209 static void ins_del
__ARGS((void));
210 static int ins_bs
__ARGS((int c
, int mode
, int *inserted_space_p
));
212 static void ins_mouse
__ARGS((int c
));
213 static void ins_mousescroll
__ARGS((int up
));
215 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
216 static void ins_tabline
__ARGS((int c
));
218 static void ins_left
__ARGS((void));
219 static void ins_home
__ARGS((int c
));
220 static void ins_end
__ARGS((int c
));
221 static void ins_s_left
__ARGS((void));
222 static void ins_right
__ARGS((void));
223 static void ins_s_right
__ARGS((void));
224 static void ins_up
__ARGS((int startcol
));
225 static void ins_pageup
__ARGS((void));
226 static void ins_down
__ARGS((int startcol
));
227 static void ins_pagedown
__ARGS((void));
229 static void ins_drop
__ARGS((void));
231 static int ins_tab
__ARGS((void));
232 static int ins_eol
__ARGS((int c
));
234 static int ins_digraph
__ARGS((void));
236 static int ins_copychar
__ARGS((linenr_T lnum
));
237 static int ins_ctrl_ey
__ARGS((int tc
));
238 #ifdef FEAT_SMARTINDENT
239 static void ins_try_si
__ARGS((int c
));
241 static colnr_T get_nolist_virtcol
__ARGS((void));
243 static colnr_T Insstart_textlen
; /* length of line when insert started */
244 static colnr_T Insstart_blank_vcol
; /* vcol for first inserted blank */
246 static char_u
*last_insert
= NULL
; /* the text of the previous insert,
247 K_SPECIAL and CSI are escaped */
248 static int last_insert_skip
; /* nr of chars in front of previous insert */
249 static int new_insert_skip
; /* nr of chars in front of current insert */
250 static int did_restart_edit
; /* "restart_edit" when calling edit() */
253 static int can_cindent
; /* may do cindenting on this line */
256 static int old_indent
= 0; /* for ^^D command in insert mode */
258 #ifdef FEAT_RIGHTLEFT
259 static int revins_on
; /* reverse insert mode on */
260 static int revins_chars
; /* how much to skip after edit */
261 static int revins_legal
; /* was the last char 'legal'? */
262 static int revins_scol
; /* start column of revins session */
265 static int ins_need_undo
; /* call u_save() before inserting a
266 char. Set when edit() is called.
267 after that arrow_used is used. */
269 static int did_add_space
= FALSE
; /* auto_format() added an extra space
273 * edit(): Start inserting text.
276 * 'i' normal insert command
277 * 'a' normal append command
278 * 'R' replace command
279 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
280 * but still only one <CR> is inserted. The <Esc> is not used for redo.
282 * 'V' "gR" command for Virtual Replace mode.
283 * 'v' "gr" command for single character Virtual Replace mode.
285 * This function is not called recursively. For CTRL-O commands, it returns
286 * and lets the caller handle the Normal-mode command.
288 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
291 edit(cmdchar
, startln
, count
)
293 int startln
; /* if set, insert at start of line */
300 static linenr_T o_lnum
= 0;
302 int did_backspace
= TRUE
; /* previous char was backspace */
304 int line_is_white
= FALSE
; /* line is empty before insert */
306 linenr_T old_topline
= 0; /* topline before insertion */
308 int old_topfill
= -1;
310 int inserted_space
= FALSE
; /* just inserted a space */
311 int replaceState
= REPLACE
;
312 int nomove
= FALSE
; /* don't move cursor on return */
314 /* Remember whether editing was restarted after CTRL-O. */
315 did_restart_edit
= restart_edit
;
317 /* sleep before redrawing, needed for "CTRL-O :" that results in an
319 check_for_delay(TRUE
);
322 /* Don't allow inserting in the sandbox. */
329 /* Don't allow changes in the buffer while editing the cmdline. The
330 * caller of getcmdline() may get confused. */
337 #ifdef FEAT_INS_EXPAND
338 ins_compl_clear(); /* clear stuff for CTRL-X mode */
343 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
345 if (cmdchar
!= 'r' && cmdchar
!= 'v')
350 else if (cmdchar
== 'V')
354 set_vim_var_string(VV_INSERTMODE
, ptr
, 1);
356 apply_autocmds(EVENT_INSERTENTER
, NULL
, NULL
, FALSE
, curbuf
);
362 * When doing a paste with the middle mouse button, Insstart is set to
363 * where the paste started.
365 if (where_paste_started
.lnum
!= 0)
366 Insstart
= where_paste_started
;
370 Insstart
= curwin
->w_cursor
;
374 Insstart_textlen
= linetabsize(ml_get_curline());
375 Insstart_blank_vcol
= MAXCOL
;
379 if (cmdchar
!= NUL
&& restart_edit
== 0)
382 AppendNumberToRedobuff(count
);
384 if (cmdchar
== 'V' || cmdchar
== 'v')
386 /* "gR" or "gr" command */
387 AppendCharToRedobuff('g');
388 AppendCharToRedobuff((cmdchar
== 'v') ? 'r' : 'R');
393 AppendCharToRedobuff(cmdchar
);
394 if (cmdchar
== 'g') /* "gI" command */
395 AppendCharToRedobuff('I');
396 else if (cmdchar
== 'r') /* "r<CR>" command */
397 count
= 1; /* insert only one <CR> */
407 EMSG(farsi_text_3
); /* encoded in Farsi */
415 else if (cmdchar
== 'V' || cmdchar
== 'v')
418 replaceState
= VREPLACE
;
419 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
420 vr_lines_changed
= 1;
426 stop_insert_mode
= FALSE
;
429 * Need to recompute the cursor position, it might move when the cursor is
430 * on a TAB or special character.
435 * Enable langmap or IME, indicated by 'iminsert'.
436 * Note that IME may enabled/disabled without us noticing here, thus the
437 * 'iminsert' value may not reflect what is actually used. It is updated
438 * when hitting <Esc>.
440 if (curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
442 #ifdef USE_IM_CONTROL
443 im_set_active(curbuf
->b_p_iminsert
== B_IMODE_IM
);
449 #ifdef FEAT_CMDL_INFO
452 #ifdef FEAT_RIGHTLEFT
453 /* there is no reverse replace mode */
454 revins_on
= (State
== INSERT
&& p_ri
);
463 * Handle restarting Insert mode.
464 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
465 * restart_edit non-zero, and something in the stuff buffer.
467 if (restart_edit
!= 0 && stuff_empty())
471 * After a paste we consider text typed to be part of the insert for
472 * the pasted text. You can backspace over the pasted text too.
474 if (where_paste_started
.lnum
)
482 * If the cursor was after the end-of-line before the CTRL-O and it is
483 * now at the end-of-line, put it after the end-of-line (this is not
484 * correct in very rare cases).
485 * Also do this if curswant is greater than the current virtual
486 * column. Eg after "^O$" or "^O80|".
490 if (((ins_at_eol
&& curwin
->w_cursor
.lnum
== o_lnum
)
491 || curwin
->w_curswant
> curwin
->w_virtcol
)
492 && *(ptr
= ml_get_curline() + curwin
->w_cursor
.col
) != NUL
)
495 ++curwin
->w_cursor
.col
;
499 i
= (*mb_ptr2len
)(ptr
);
501 curwin
->w_cursor
.col
+= i
;
510 /* we are in insert mode now, don't need to start it anymore */
511 need_start_insertmode
= FALSE
;
513 /* Need to save the line for undo before inserting the first char. */
514 ins_need_undo
= TRUE
;
517 where_paste_started
.lnum
= 0;
523 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
525 if (!p_im
&& did_restart_edit
== 0)
530 * If 'showmode' is set, show the current (insert/replace/..) mode.
531 * A warning message for changing a readonly file is given here, before
532 * actually changing anything. It's put after the mode, if any.
535 if (p_smd
&& msg_silent
== 0)
538 if (!p_im
&& did_restart_edit
== 0)
539 change_warning(i
+ 1);
542 ui_cursor_shape(); /* may show different cursor shape */
545 do_digraph(-1); /* clear digraphs */
549 * Get the current length of the redo buffer, those characters have to be
550 * skipped if we want to get to the inserted characters.
552 ptr
= get_inserted();
557 new_insert_skip
= (int)STRLEN(ptr
);
564 * Main loop in Insert mode: repeat until Insert mode is left.
568 #ifdef FEAT_RIGHTLEFT
570 revins_scol
= -1; /* reset on illegal motions */
574 if (arrow_used
) /* don't repeat insert when arrow key used */
577 if (stop_insert_mode
)
579 /* ":stopinsert" used or 'insertmode' reset */
584 /* set curwin->w_curswant for next K_DOWN or K_UP */
586 curwin
->w_set_curswant
= TRUE
;
588 /* If there is no typeahead may check for timestamps (e.g., for when a
589 * menu invoked a shell command). */
592 did_check_timestamps
= FALSE
;
593 if (need_check_timestamps
)
594 check_timestamps(FALSE
);
598 * When emsg() was called msg_scroll will have been set.
603 /* When 'mousefocus' is set a mouse movement may have taken us to
604 * another window. "need_mouse_correct" may then be set because of an
606 if (need_mouse_correct
)
611 /* Open fold at the cursor line, according to 'foldopen'. */
612 if (fdo_flags
& FDO_INSERT
)
614 /* Close folds where the cursor isn't, according to 'foldclose' */
620 * If we inserted a character at the last position of the last line in
621 * the window, scroll the window one line up. This avoids an extra
623 * This is detected when the cursor column is smaller after inserting
625 * Don't do this when the topline changed already, it has
626 * already been adjusted (by insertchar() calling open_line())).
628 if (curbuf
->b_mod_set
631 && curwin
->w_topline
== old_topline
633 && curwin
->w_topfill
== old_topfill
637 mincol
= curwin
->w_wcol
;
638 validate_cursor_col();
640 if ((int)curwin
->w_wcol
< (int)mincol
- curbuf
->b_p_ts
641 && curwin
->w_wrow
== W_WINROW(curwin
)
642 + curwin
->w_height
- 1 - p_so
643 && (curwin
->w_cursor
.lnum
!= curwin
->w_topline
645 || curwin
->w_topfill
> 0
650 if (curwin
->w_topfill
> 0)
655 if (hasFolding(curwin
->w_topline
, NULL
, &old_topline
))
656 set_topline(curwin
, old_topline
+ 1);
659 set_topline(curwin
, curwin
->w_topline
+ 1);
663 /* May need to adjust w_topline to show the cursor. */
666 did_backspace
= FALSE
;
668 validate_cursor(); /* may set must_redraw */
671 * Redraw the display when no characters are waiting.
672 * Also shows mode, ruler and positions cursor.
676 #ifdef FEAT_SCROLLBIND
678 do_check_scrollbind(TRUE
);
682 old_topline
= curwin
->w_topline
;
684 old_topfill
= curwin
->w_topfill
;
687 #ifdef USE_ON_FLY_SCROLL
688 dont_scroll
= FALSE
; /* allow scrolling here */
692 * Get a character for Insert mode.
694 lastc
= c
; /* remember previous char for CTRL-D */
697 #ifdef FEAT_RIGHTLEFT
698 if (p_hkmap
&& KeyTyped
)
699 c
= hkmap(c
); /* Hebrew mode mapping */
702 if (p_fkmap
&& KeyTyped
)
703 c
= fkmap(c
); /* Farsi mode mapping */
706 #ifdef FEAT_INS_EXPAND
708 * Special handling of keys while the popup menu is visible or wanted
709 * and the cursor is still in the completed word.
711 if (compl_started
&& pum_wanted() && curwin
->w_cursor
.col
>= compl_col
)
713 /* BS: Delete one character from "compl_leader". */
714 if ((c
== K_BS
|| c
== Ctrl_H
)
715 && curwin
->w_cursor
.col
> compl_col
716 && (c
= ins_compl_bs()) == NUL
)
719 /* When no match was selected or it was edited. */
720 if (!compl_used_match
)
722 /* CTRL-L: Add one character from the current match to
723 * "compl_leader". Except when at the original match and
724 * there is nothing to add, CTRL-L works like CTRL-P then. */
726 && (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
727 || STRLEN(compl_shown_match
->cp_str
)
728 > curwin
->w_cursor
.col
- compl_col
))
730 ins_compl_addfrommatch();
734 /* A printable, non-white character: Add to "compl_leader". */
735 if (vim_isprintc(c
) && !vim_iswhite(c
))
737 ins_compl_addleader(c
);
741 /* Pressing CTRL-Y selects the current match. Shen
742 * compl_enter_selects is set the Enter key does the same. */
743 if (c
== Ctrl_Y
|| (compl_enter_selects
744 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
752 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
753 * it does fix up the text when finishing completion. */
754 compl_get_longest
= FALSE
;
755 if (c
!= K_IGNORE
&& ins_compl_prep(c
))
759 /* CTRL-\ CTRL-N goes to Normal mode,
760 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
761 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
764 /* may need to redraw when no more chars available now */
771 if (c
!= Ctrl_N
&& c
!= Ctrl_G
&& c
!= Ctrl_O
)
773 /* it's something else */
777 else if (c
== Ctrl_G
&& p_im
)
784 ins_at_eol
= FALSE
; /* cursor keeps its column */
796 #ifdef FEAT_INS_EXPAND
797 if ((c
== Ctrl_V
|| c
== Ctrl_Q
) && ctrl_x_mode
== CTRL_X_CMDLINE
)
800 if (c
== Ctrl_V
|| c
== Ctrl_Q
)
803 c
= Ctrl_V
; /* pretend CTRL-V is last typed character */
809 # ifdef FEAT_INS_EXPAND
814 /* A key name preceded by a bang means this key is not to be
815 * inserted. Skip ahead to the re-indenting below.
816 * A key name preceded by a star means that indenting has to be
817 * done before inserting the key. */
818 line_is_white
= inindent(0);
819 if (in_cinkeys(c
, '!', line_is_white
))
821 if (can_cindent
&& in_cinkeys(c
, '*', line_is_white
)
822 && stop_arrow() == OK
)
827 #ifdef FEAT_RIGHTLEFT
831 case K_LEFT
: c
= K_RIGHT
; break;
832 case K_S_LEFT
: c
= K_S_RIGHT
; break;
833 case K_C_LEFT
: c
= K_C_RIGHT
; break;
834 case K_RIGHT
: c
= K_LEFT
; break;
835 case K_S_RIGHT
: c
= K_S_LEFT
; break;
836 case K_C_RIGHT
: c
= K_C_LEFT
; break;
842 * If 'keymodel' contains "startsel", may start selection. If it
843 * does, a CTRL-O and c will be stuffed, we need to get these
846 if (ins_start_select(c
))
851 * The big switch to handle a character in insert mode.
855 case ESC
: /* End input mode */
856 if (echeck_abbr(ESC
+ ABBR_OFF
))
860 case Ctrl_C
: /* End input mode */
862 if (c
== Ctrl_C
&& cmdwin_type
!= 0)
864 /* Close the cmdline window. */
865 cmdwin_result
= K_IGNORE
;
866 got_int
= FALSE
; /* don't stop executing autocommands et al. */
874 /* when 'insertmode' set, and not halfway a mapping, don't leave
880 (void)vgetc(); /* flush all buffers */
889 * This is the ONLY return from edit()!
891 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
892 * still puts the cursor back after the inserted text. */
893 if (ins_at_eol
&& gchar_cursor() == NUL
)
894 o_lnum
= curwin
->w_cursor
.lnum
;
896 if (ins_esc(&count
, cmdchar
, nomove
))
899 if (cmdchar
!= 'r' && cmdchar
!= 'v')
900 apply_autocmds(EVENT_INSERTLEAVE
, NULL
, NULL
,
903 return (c
== Ctrl_O
);
907 case Ctrl_Z
: /* suspend when 'insertmode' set */
909 goto normalchar
; /* insert CTRL-Z as normal char */
910 stuffReadbuff((char_u
*)":st\r");
914 case Ctrl_O
: /* execute one command */
915 #ifdef FEAT_COMPL_FUNC
916 if (ctrl_x_mode
== CTRL_X_OMNI
)
919 if (echeck_abbr(Ctrl_O
+ ABBR_OFF
))
925 case K_INS
: /* toggle insert/replace mode */
927 ins_insert(replaceState
);
930 case K_SELECT
: /* end of Select mode mapping - ignore */
934 case K_SNIFF
: /* Sniff command received */
935 stuffcharReadbuff(K_SNIFF
);
939 case K_HELP
: /* Help key works like <ESC> <Help> */
942 stuffcharReadbuff(K_HELP
);
944 need_start_insertmode
= TRUE
;
947 #ifdef FEAT_NETBEANS_INTG
948 case K_F21
: /* NetBeans command */
949 ++no_mapping
; /* don't map the next key hits */
952 netbeans_keycommand(i
);
956 case K_ZERO
: /* Insert the previously inserted text. */
959 /* For ^@ the trailing ESC will end the insert, unless there is an
961 if (stuff_inserted(NUL
, 1L, (c
== Ctrl_A
)) == FAIL
962 && c
!= Ctrl_A
&& !p_im
)
963 goto doESCkey
; /* quit insert mode */
964 inserted_space
= FALSE
;
967 case Ctrl_R
: /* insert the contents of a register */
969 auto_format(FALSE
, TRUE
);
970 inserted_space
= FALSE
;
973 case Ctrl_G
: /* commands starting with CTRL-G */
977 case Ctrl_HAT
: /* switch input mode and/or langmap */
981 #ifdef FEAT_RIGHTLEFT
982 case Ctrl__
: /* switch between languages */
989 case Ctrl_D
: /* Make indent one shiftwidth smaller. */
990 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
991 if (ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
996 case Ctrl_T
: /* Make indent one shiftwidth greater. */
997 # ifdef FEAT_INS_EXPAND
998 if (c
== Ctrl_T
&& ctrl_x_mode
== CTRL_X_THESAURUS
)
1000 if (has_compl_option(FALSE
))
1005 ins_shift(c
, lastc
);
1006 auto_format(FALSE
, TRUE
);
1007 inserted_space
= FALSE
;
1010 case K_DEL
: /* delete character under the cursor */
1013 auto_format(FALSE
, TRUE
);
1016 case K_BS
: /* delete character before the cursor */
1018 did_backspace
= ins_bs(c
, BACKSPACE_CHAR
, &inserted_space
);
1019 auto_format(FALSE
, TRUE
);
1022 case Ctrl_W
: /* delete word before the cursor */
1023 did_backspace
= ins_bs(c
, BACKSPACE_WORD
, &inserted_space
);
1024 auto_format(FALSE
, TRUE
);
1027 case Ctrl_U
: /* delete all inserted text in current line */
1028 # ifdef FEAT_COMPL_FUNC
1029 /* CTRL-X CTRL-U completes with 'completefunc'. */
1030 if (ctrl_x_mode
== CTRL_X_FUNCTION
)
1033 did_backspace
= ins_bs(c
, BACKSPACE_LINE
, &inserted_space
);
1034 auto_format(FALSE
, TRUE
);
1035 inserted_space
= FALSE
;
1039 case K_LEFTMOUSE
: /* mouse keys */
1040 case K_LEFTMOUSE_NM
:
1043 case K_LEFTRELEASE_NM
:
1046 case K_MIDDLERELEASE
:
1049 case K_RIGHTRELEASE
:
1059 case K_MOUSEDOWN
: /* Default action for scroll wheel up: scroll up */
1060 ins_mousescroll(FALSE
);
1063 case K_MOUSEUP
: /* Default action for scroll wheel down: scroll down */
1064 ins_mousescroll(TRUE
);
1067 #ifdef FEAT_GUI_TABLINE
1074 case K_IGNORE
: /* Something mapped to nothing */
1078 case K_CURSORHOLD
: /* Didn't type something for a while. */
1079 apply_autocmds(EVENT_CURSORHOLDI
, NULL
, NULL
, FALSE
, curbuf
);
1080 did_cursorhold
= TRUE
;
1085 /* On Win32 ignore <M-F4>, we get it when closing the window was
1088 if (mod_mask
!= MOD_MASK_ALT
)
1094 case K_VER_SCROLLBAR
:
1098 case K_HOR_SCROLLBAR
:
1103 case K_HOME
: /* <Home> */
1110 case K_END
: /* <End> */
1117 case K_LEFT
: /* <Left> */
1118 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1124 case K_S_LEFT
: /* <S-Left> */
1129 case K_RIGHT
: /* <Right> */
1130 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1136 case K_S_RIGHT
: /* <S-Right> */
1141 case K_UP
: /* <Up> */
1142 #ifdef FEAT_INS_EXPAND
1146 if (mod_mask
& MOD_MASK_SHIFT
)
1152 case K_S_UP
: /* <S-Up> */
1155 #ifdef FEAT_INS_EXPAND
1162 case K_DOWN
: /* <Down> */
1163 #ifdef FEAT_INS_EXPAND
1167 if (mod_mask
& MOD_MASK_SHIFT
)
1173 case K_S_DOWN
: /* <S-Down> */
1176 #ifdef FEAT_INS_EXPAND
1184 case K_DROP
: /* drag-n-drop event */
1189 case K_S_TAB
: /* When not mapped, use like a normal TAB */
1193 case TAB
: /* TAB or Complete patterns along path */
1194 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1195 if (ctrl_x_mode
== CTRL_X_PATH_PATTERNS
)
1198 inserted_space
= FALSE
;
1200 goto normalchar
; /* insert TAB as a normal char */
1201 auto_format(FALSE
, TRUE
);
1204 case K_KENTER
: /* <Enter> */
1209 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1210 /* In a quickfix window a <CR> jumps to the error under the
1212 if (bt_quickfix(curbuf
) && c
== CAR
)
1214 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
1215 do_cmdline_cmd((char_u
*)".cc");
1216 else /* location list window */
1217 do_cmdline_cmd((char_u
*)".ll");
1222 if (cmdwin_type
!= 0)
1224 /* Execute the command in the cmdline window. */
1225 cmdwin_result
= CAR
;
1229 if (ins_eol(c
) && !p_im
)
1230 goto doESCkey
; /* out of memory */
1231 auto_format(FALSE
, FALSE
);
1232 inserted_space
= FALSE
;
1235 #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
1236 case Ctrl_K
: /* digraph or keyword completion */
1237 # ifdef FEAT_INS_EXPAND
1238 if (ctrl_x_mode
== CTRL_X_DICTIONARY
)
1240 if (has_compl_option(TRUE
))
1245 # ifdef FEAT_DIGRAPHS
1253 #ifdef FEAT_INS_EXPAND
1254 case Ctrl_X
: /* Enter CTRL-X mode */
1258 case Ctrl_RSB
: /* Tag name completion after ^X */
1259 if (ctrl_x_mode
!= CTRL_X_TAGS
)
1263 case Ctrl_F
: /* File name completion after ^X */
1264 if (ctrl_x_mode
!= CTRL_X_FILES
)
1268 case 's': /* Spelling completion after ^X */
1270 if (ctrl_x_mode
!= CTRL_X_SPELL
)
1275 case Ctrl_L
: /* Whole line completion after ^X */
1276 #ifdef FEAT_INS_EXPAND
1277 if (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
1280 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1283 if (echeck_abbr(Ctrl_L
+ ABBR_OFF
))
1289 #ifdef FEAT_INS_EXPAND
1292 case Ctrl_P
: /* Do previous/next pattern completion */
1294 /* if 'complete' is empty then plain ^P is no longer special,
1295 * but it is under other ^X modes */
1296 if (*curbuf
->b_p_cpt
== NUL
1298 && !(compl_cont_status
& CONT_LOCAL
))
1302 if (ins_complete(c
) == FAIL
)
1303 compl_cont_status
= 0;
1305 #endif /* FEAT_INS_EXPAND */
1307 case Ctrl_Y
: /* copy from previous line or scroll down */
1308 case Ctrl_E
: /* copy from next line or scroll up */
1314 if (c
== intr_char
) /* special interrupt char */
1319 * Insert a nomal character.
1322 #ifdef FEAT_SMARTINDENT
1323 /* Try to perform smart-indenting. */
1329 inserted_space
= TRUE
;
1332 can_cindent
= FALSE
;
1334 if (Insstart_blank_vcol
== MAXCOL
1335 && curwin
->w_cursor
.lnum
== Insstart
.lnum
)
1336 Insstart_blank_vcol
= get_nolist_virtcol();
1339 if (vim_iswordc(c
) || !echeck_abbr(
1341 /* Add ABBR_OFF for characters above 0x100, this is
1342 * what check_abbr() expects. */
1343 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1347 insert_special(c
, FALSE
, FALSE
);
1348 #ifdef FEAT_RIGHTLEFT
1354 auto_format(FALSE
, TRUE
);
1357 /* When inserting a character the cursor line must never be in a
1362 } /* end of switch (c) */
1364 /* If the cursor was moved we didn't just insert a space */
1366 inserted_space
= FALSE
;
1369 if (can_cindent
&& cindent_on()
1370 # ifdef FEAT_INS_EXPAND
1377 * Indent now if a key was typed that is in 'cinkeys'.
1379 if (in_cinkeys(c
, ' ', line_is_white
))
1381 if (stop_arrow() == OK
)
1382 /* re-indent the current line */
1386 #endif /* FEAT_CINDENT */
1393 * Redraw for Insert mode.
1394 * This is postponed until getting the next character to make '$' in the 'cpo'
1395 * option work correctly.
1396 * Only redraw when there are no characters available. This speeds up
1397 * inserting sequences of characters (e.g., for CTRL-R).
1402 int ready
; /* not busy with something */
1407 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1408 * visible, the command might delete it. */
1409 if (ready
&& has_cursormovedI()
1410 && !equalpos(last_cursormoved
, curwin
->w_cursor
)
1411 # ifdef FEAT_INS_EXPAND
1416 apply_autocmds(EVENT_CURSORMOVEDI
, NULL
, NULL
, FALSE
, curbuf
);
1417 last_cursormoved
= curwin
->w_cursor
;
1422 else if (clear_cmdline
|| redraw_cmdline
)
1423 showmode(); /* clear cmdline and show mode */
1426 emsg_on_display
= FALSE
; /* may remove error message now */
1431 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1438 /* may need to redraw when no more chars available now */
1441 if (redrawing() && !char_avail())
1442 edit_putchar('^', TRUE
);
1443 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
1445 #ifdef FEAT_CMDL_INFO
1446 add_to_showcmd_c(Ctrl_V
);
1450 #ifdef FEAT_CMDL_INFO
1453 insert_special(c
, FALSE
, TRUE
);
1454 #ifdef FEAT_RIGHTLEFT
1461 * Put a character directly onto the screen. It's not stored in a buffer.
1462 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1464 static int pc_status
;
1465 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1466 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1467 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1468 #define PC_STATUS_SET 3 /* pc_bytes was filled */
1470 static char_u pc_bytes
[MB_MAXBYTES
+ 1]; /* saved bytes */
1472 static char_u pc_bytes
[2]; /* saved bytes */
1479 edit_putchar(c
, highlight
)
1485 if (ScreenLines
!= NULL
)
1487 update_topline(); /* just in case w_topline isn't valid */
1490 attr
= hl_attr(HLF_8
);
1493 pc_row
= W_WINROW(curwin
) + curwin
->w_wrow
;
1494 pc_col
= W_WINCOL(curwin
);
1495 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1496 pc_status
= PC_STATUS_UNSET
;
1498 #ifdef FEAT_RIGHTLEFT
1501 pc_col
+= W_WIDTH(curwin
) - 1 - curwin
->w_wcol
;
1505 int fix_col
= mb_fix_col(pc_col
, pc_row
);
1507 if (fix_col
!= pc_col
)
1509 screen_putchar(' ', pc_row
, fix_col
, attr
);
1511 pc_status
= PC_STATUS_RIGHT
;
1519 pc_col
+= curwin
->w_wcol
;
1521 if (mb_lefthalve(pc_row
, pc_col
))
1522 pc_status
= PC_STATUS_LEFT
;
1526 /* save the character to be able to put it back */
1527 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1528 if (pc_status
== PC_STATUS_UNSET
)
1531 screen_getbytes(pc_row
, pc_col
, pc_bytes
, &pc_attr
);
1532 pc_status
= PC_STATUS_SET
;
1534 screen_putchar(c
, pc_row
, pc_col
, attr
);
1539 * Undo the previous edit_putchar().
1544 if (pc_status
!= PC_STATUS_UNSET
&& pc_row
>= msg_scrolled
)
1546 #if defined(FEAT_MBYTE)
1547 if (pc_status
== PC_STATUS_RIGHT
)
1549 if (pc_status
== PC_STATUS_RIGHT
|| pc_status
== PC_STATUS_LEFT
)
1550 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1553 screen_puts(pc_bytes
, pc_row
- msg_scrolled
, pc_col
, pc_attr
);
1558 * Called when p_dollar is set: display a '$' at the end of the changed text
1559 * Only works when cursor is in the line that changes.
1571 save_col
= curwin
->w_cursor
.col
;
1572 curwin
->w_cursor
.col
= col
;
1578 /* If on the last byte of a multi-byte move to the first byte. */
1579 p
= ml_get_curline();
1580 curwin
->w_cursor
.col
-= (*mb_head_off
)(p
, p
+ col
);
1583 curs_columns(FALSE
); /* recompute w_wrow and w_wcol */
1584 if (curwin
->w_wcol
< W_WIDTH(curwin
))
1586 edit_putchar('$', FALSE
);
1587 dollar_vcol
= curwin
->w_virtcol
;
1589 curwin
->w_cursor
.col
= save_col
;
1593 * Call this function before moving the cursor from the normal insert position
1602 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1607 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1608 * Keep the cursor on the same character.
1609 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1610 * type == INDENT_DEC decrease indent (for CTRL-D)
1611 * type == INDENT_SET set indent to "amount"
1612 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1615 change_indent(type
, amount
, round
, replaced
)
1619 int replaced
; /* replaced character, put on replace stack */
1623 int insstart_less
; /* reduction for Insstart.col */
1630 #ifdef FEAT_VREPLACE
1631 colnr_T orig_col
= 0; /* init for GCC */
1632 char_u
*new_line
, *orig_line
= NULL
; /* init for GCC */
1634 /* VREPLACE mode needs to know what the line was like before changing */
1635 if (State
& VREPLACE_FLAG
)
1637 orig_line
= vim_strsave(ml_get_curline()); /* Deal with NULL below */
1638 orig_col
= curwin
->w_cursor
.col
;
1642 /* for the following tricks we don't want list mode */
1643 save_p_list
= curwin
->w_p_list
;
1644 curwin
->w_p_list
= FALSE
;
1645 vc
= getvcol_nolist(&curwin
->w_cursor
);
1649 * For Replace mode we need to fix the replace stack later, which is only
1650 * possible when the cursor is in the indent. Remember the number of
1651 * characters before the cursor if it's possible.
1653 start_col
= curwin
->w_cursor
.col
;
1655 /* determine offset from first non-blank */
1656 new_cursor_col
= curwin
->w_cursor
.col
;
1657 beginline(BL_WHITE
);
1658 new_cursor_col
-= curwin
->w_cursor
.col
;
1660 insstart_less
= curwin
->w_cursor
.col
;
1663 * If the cursor is in the indent, compute how many screen columns the
1664 * cursor is to the left of the first non-blank.
1666 if (new_cursor_col
< 0)
1667 vcol
= get_indent() - vcol
;
1669 if (new_cursor_col
> 0) /* can't fix replace stack */
1673 * Set the new indent. The cursor will be put on the first non-blank.
1675 if (type
== INDENT_SET
)
1676 (void)set_indent(amount
, SIN_CHANGED
);
1679 #ifdef FEAT_VREPLACE
1680 int save_State
= State
;
1682 /* Avoid being called recursively. */
1683 if (State
& VREPLACE_FLAG
)
1686 shift_line(type
== INDENT_DEC
, round
, 1);
1687 #ifdef FEAT_VREPLACE
1691 insstart_less
-= curwin
->w_cursor
.col
;
1694 * Try to put cursor on same character.
1695 * If the cursor is at or after the first non-blank in the line,
1696 * compute the cursor column relative to the column of the first
1697 * non-blank character.
1698 * If we are not in insert mode, leave the cursor on the first non-blank.
1699 * If the cursor is before the first non-blank, position it relative
1700 * to the first non-blank, counted in screen columns.
1702 if (new_cursor_col
>= 0)
1705 * When changing the indent while the cursor is touching it, reset
1706 * Insstart_col to 0.
1708 if (new_cursor_col
== 0)
1709 insstart_less
= MAXCOL
;
1710 new_cursor_col
+= curwin
->w_cursor
.col
;
1712 else if (!(State
& INSERT
))
1713 new_cursor_col
= curwin
->w_cursor
.col
;
1717 * Compute the screen column where the cursor should be.
1719 vcol
= get_indent() - vcol
;
1720 curwin
->w_virtcol
= (vcol
< 0) ? 0 : vcol
;
1723 * Advance the cursor until we reach the right screen column.
1725 vcol
= last_vcol
= 0;
1726 new_cursor_col
= -1;
1727 ptr
= ml_get_curline();
1728 while (vcol
<= (int)curwin
->w_virtcol
)
1732 if (has_mbyte
&& new_cursor_col
>= 0)
1733 new_cursor_col
+= (*mb_ptr2len
)(ptr
+ new_cursor_col
);
1737 vcol
+= lbr_chartabsize(ptr
+ new_cursor_col
, (colnr_T
)vcol
);
1742 * May need to insert spaces to be able to position the cursor on
1743 * the right screen column.
1745 if (vcol
!= (int)curwin
->w_virtcol
)
1747 curwin
->w_cursor
.col
= new_cursor_col
;
1748 i
= (int)curwin
->w_virtcol
- vcol
;
1752 new_cursor_col
+= i
;
1762 * When changing the indent while the cursor is in it, reset
1763 * Insstart_col to 0.
1765 insstart_less
= MAXCOL
;
1768 curwin
->w_p_list
= save_p_list
;
1770 if (new_cursor_col
<= 0)
1771 curwin
->w_cursor
.col
= 0;
1773 curwin
->w_cursor
.col
= new_cursor_col
;
1774 curwin
->w_set_curswant
= TRUE
;
1775 changed_cline_bef_curs();
1778 * May have to adjust the start of the insert.
1782 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
&& Insstart
.col
!= 0)
1784 if ((int)Insstart
.col
<= insstart_less
)
1787 Insstart
.col
-= insstart_less
;
1789 if ((int)ai_col
<= insstart_less
)
1792 ai_col
-= insstart_less
;
1796 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1797 * If the number of characters before the cursor decreased, need to pop a
1798 * few characters from the replace stack.
1799 * If the number of characters before the cursor increased, need to push a
1800 * few NULs onto the replace stack.
1802 if (REPLACE_NORMAL(State
) && start_col
>= 0)
1804 while (start_col
> (int)curwin
->w_cursor
.col
)
1806 replace_join(0); /* remove a NUL from the replace stack */
1809 while (start_col
< (int)curwin
->w_cursor
.col
|| replaced
)
1814 replace_push(replaced
);
1821 #ifdef FEAT_VREPLACE
1823 * For VREPLACE mode, we also have to fix the replace stack. In this case
1824 * it is always possible because we backspace over the whole line and then
1825 * put it back again the way we wanted it.
1827 if (State
& VREPLACE_FLAG
)
1829 /* If orig_line didn't allocate, just return. At least we did the job,
1830 * even if you can't backspace. */
1831 if (orig_line
== NULL
)
1835 new_line
= vim_strsave(ml_get_curline());
1836 if (new_line
== NULL
)
1839 /* We only put back the new line up to the cursor */
1840 new_line
[curwin
->w_cursor
.col
] = NUL
;
1842 /* Put back original line */
1843 ml_replace(curwin
->w_cursor
.lnum
, orig_line
, FALSE
);
1844 curwin
->w_cursor
.col
= orig_col
;
1846 /* Backspace from cursor to start of line */
1847 backspace_until_column(0);
1849 /* Insert new stuff into line again */
1850 ins_bytes(new_line
);
1858 * Truncate the space at the end of a line. This is to be used only in an
1859 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1863 truncate_spaces(line
)
1868 /* find start of trailing white space */
1869 for (i
= (int)STRLEN(line
) - 1; i
>= 0 && vim_iswhite(line
[i
]); i
--)
1871 if (State
& REPLACE_FLAG
)
1872 replace_join(0); /* remove a NUL from the replace stack */
1877 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1878 || defined(FEAT_COMMENTS) || defined(PROTO)
1880 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1881 * modes correctly. May also be used when not in insert mode at all.
1884 backspace_until_column(col
)
1887 while ((int)curwin
->w_cursor
.col
> col
)
1889 curwin
->w_cursor
.col
--;
1890 if (State
& REPLACE_FLAG
)
1893 (void)del_char(FALSE
);
1898 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1900 * CTRL-X pressed in Insert mode.
1905 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1906 * CTRL-V works like CTRL-N */
1907 if (ctrl_x_mode
!= CTRL_X_CMDLINE
)
1909 /* if the next ^X<> won't ADD nothing, then reset
1910 * compl_cont_status */
1911 if (compl_cont_status
& CONT_N_ADDS
)
1912 compl_cont_status
|= CONT_INTRPT
;
1914 compl_cont_status
= 0;
1915 /* We're not sure which CTRL-X mode it will be yet */
1916 ctrl_x_mode
= CTRL_X_NOT_DEFINED_YET
;
1917 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
1918 edit_submode_pre
= NULL
;
1924 * Return TRUE if the 'dict' or 'tsr' option can be used.
1927 has_compl_option(dict_opt
)
1930 if (dict_opt
? (*curbuf
->b_p_dict
== NUL
&& *p_dict
== NUL
1932 && !curwin
->w_p_spell
1935 : (*curbuf
->b_p_tsr
== NUL
&& *p_tsr
== NUL
))
1938 edit_submode
= NULL
;
1939 msg_attr(dict_opt
? (char_u
*)_("'dictionary' option is empty")
1940 : (char_u
*)_("'thesaurus' option is empty"),
1942 if (emsg_silent
== 0)
1947 ui_delay(2000L, FALSE
);
1955 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1956 * This depends on the current mode.
1959 vim_is_ctrl_x_key(c
)
1962 /* Always allow ^R - let it's results then be checked */
1966 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
1967 if (ins_compl_pum_key(c
))
1970 switch (ctrl_x_mode
)
1972 case 0: /* Not in any CTRL-X mode */
1973 return (c
== Ctrl_N
|| c
== Ctrl_P
|| c
== Ctrl_X
);
1974 case CTRL_X_NOT_DEFINED_YET
:
1975 return ( c
== Ctrl_X
|| c
== Ctrl_Y
|| c
== Ctrl_E
1976 || c
== Ctrl_L
|| c
== Ctrl_F
|| c
== Ctrl_RSB
1977 || c
== Ctrl_I
|| c
== Ctrl_D
|| c
== Ctrl_P
1978 || c
== Ctrl_N
|| c
== Ctrl_T
|| c
== Ctrl_V
1979 || c
== Ctrl_Q
|| c
== Ctrl_U
|| c
== Ctrl_O
1980 || c
== Ctrl_S
|| c
== 's');
1982 return (c
== Ctrl_Y
|| c
== Ctrl_E
);
1983 case CTRL_X_WHOLE_LINE
:
1984 return (c
== Ctrl_L
|| c
== Ctrl_P
|| c
== Ctrl_N
);
1986 return (c
== Ctrl_F
|| c
== Ctrl_P
|| c
== Ctrl_N
);
1987 case CTRL_X_DICTIONARY
:
1988 return (c
== Ctrl_K
|| c
== Ctrl_P
|| c
== Ctrl_N
);
1989 case CTRL_X_THESAURUS
:
1990 return (c
== Ctrl_T
|| c
== Ctrl_P
|| c
== Ctrl_N
);
1992 return (c
== Ctrl_RSB
|| c
== Ctrl_P
|| c
== Ctrl_N
);
1994 case CTRL_X_PATH_PATTERNS
:
1995 return (c
== Ctrl_P
|| c
== Ctrl_N
);
1996 case CTRL_X_PATH_DEFINES
:
1997 return (c
== Ctrl_D
|| c
== Ctrl_P
|| c
== Ctrl_N
);
1999 case CTRL_X_CMDLINE
:
2000 return (c
== Ctrl_V
|| c
== Ctrl_Q
|| c
== Ctrl_P
|| c
== Ctrl_N
2002 #ifdef FEAT_COMPL_FUNC
2003 case CTRL_X_FUNCTION
:
2004 return (c
== Ctrl_U
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2006 return (c
== Ctrl_O
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2009 return (c
== Ctrl_S
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2011 EMSG(_(e_internal
));
2016 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
2017 * case of the originally typed text is used, and the case of the completed
2018 * text is infered, ie this tries to work out what case you probably wanted
2019 * the rest of the word to be in -- webb
2020 * TODO: make this work for multi-byte characters.
2023 ins_compl_add_infercase(str
, len
, icase
, fname
, dir
, flags
)
2031 int has_lower
= FALSE
;
2032 int was_letter
= FALSE
;
2035 if (p_ic
&& curbuf
->b_p_inf
&& len
< IOSIZE
)
2037 /* Infer case of completed part -- webb */
2038 /* Use IObuff, str would change text in buffer! */
2039 vim_strncpy(IObuff
, str
, len
);
2041 /* Rule 1: Were any chars converted to lower? */
2042 for (idx
= 0; idx
< compl_length
; ++idx
)
2044 if (islower(compl_orig_text
[idx
]))
2047 if (isupper(IObuff
[idx
]))
2049 /* Rule 1 is satisfied */
2050 for (idx
= compl_length
; idx
< len
; ++idx
)
2051 IObuff
[idx
] = TOLOWER_LOC(IObuff
[idx
]);
2058 * Rule 2: No lower case, 2nd consecutive letter converted to
2063 for (idx
= 0; idx
< compl_length
; ++idx
)
2065 if (was_letter
&& isupper(compl_orig_text
[idx
])
2066 && islower(IObuff
[idx
]))
2068 /* Rule 2 is satisfied */
2069 for (idx
= compl_length
; idx
< len
; ++idx
)
2070 IObuff
[idx
] = TOUPPER_LOC(IObuff
[idx
]);
2073 was_letter
= isalpha(compl_orig_text
[idx
]);
2077 /* Copy the original case of the part we typed */
2078 STRNCPY(IObuff
, compl_orig_text
, compl_length
);
2080 return ins_compl_add(IObuff
, len
, icase
, fname
, NULL
, dir
,
2083 return ins_compl_add(str
, len
, icase
, fname
, NULL
, dir
, flags
, FALSE
);
2087 * Add a match to the list of matches.
2088 * If the given string is already in the list of completions, then return
2089 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2090 * maybe because alloc() returns NULL, then FAIL is returned.
2093 ins_compl_add(str
, len
, icase
, fname
, cptext
, cdir
, flags
, dup
)
2098 char_u
**cptext
; /* extra text for popup menu or NULL */
2101 int dup
; /* accept duplicate match */
2104 int dir
= (cdir
== 0 ? compl_direction
: cdir
);
2110 len
= (int)STRLEN(str
);
2113 * If the same match is already present, don't add it.
2115 if (compl_first_match
!= NULL
&& !dup
)
2117 match
= compl_first_match
;
2120 if ( !(match
->cp_flags
& ORIGINAL_TEXT
)
2121 && ins_compl_equal(match
, str
, len
)
2122 && match
->cp_str
[len
] == NUL
)
2124 match
= match
->cp_next
;
2125 } while (match
!= NULL
&& match
!= compl_first_match
);
2128 /* Remove any popup menu before changing the list of matches. */
2129 ins_compl_del_pum();
2132 * Allocate a new match structure.
2133 * Copy the values to the new match structure.
2135 match
= (compl_T
*)alloc_clear((unsigned)sizeof(compl_T
));
2138 match
->cp_number
= -1;
2139 if (flags
& ORIGINAL_TEXT
)
2140 match
->cp_number
= 0;
2141 if ((match
->cp_str
= vim_strnsave(str
, len
)) == NULL
)
2146 match
->cp_icase
= icase
;
2149 * - compl_curr_match->cp_fname if it is a string equal to fname.
2150 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2151 * - NULL otherwise. --Acevedo */
2153 && compl_curr_match
!= NULL
2154 && compl_curr_match
->cp_fname
!= NULL
2155 && STRCMP(fname
, compl_curr_match
->cp_fname
) == 0)
2156 match
->cp_fname
= compl_curr_match
->cp_fname
;
2157 else if (fname
!= NULL
)
2159 match
->cp_fname
= vim_strsave(fname
);
2160 flags
|= FREE_FNAME
;
2163 match
->cp_fname
= NULL
;
2164 match
->cp_flags
= flags
;
2170 for (i
= 0; i
< CPT_COUNT
; ++i
)
2171 if (cptext
[i
] != NULL
&& *cptext
[i
] != NUL
)
2172 match
->cp_text
[i
] = vim_strsave(cptext
[i
]);
2176 * Link the new match structure in the list of matches.
2178 if (compl_first_match
== NULL
)
2179 match
->cp_next
= match
->cp_prev
= NULL
;
2180 else if (dir
== FORWARD
)
2182 match
->cp_next
= compl_curr_match
->cp_next
;
2183 match
->cp_prev
= compl_curr_match
;
2187 match
->cp_next
= compl_curr_match
;
2188 match
->cp_prev
= compl_curr_match
->cp_prev
;
2191 match
->cp_next
->cp_prev
= match
;
2193 match
->cp_prev
->cp_next
= match
;
2194 else /* if there's nothing before, it is the first match */
2195 compl_first_match
= match
;
2196 compl_curr_match
= match
;
2199 * Find the longest common string if still doing that.
2201 if (compl_get_longest
&& (flags
& ORIGINAL_TEXT
) == 0)
2202 ins_compl_longest_match(match
);
2208 * Return TRUE if "str[len]" matches with match->cp_str, considering
2212 ins_compl_equal(match
, str
, len
)
2217 if (match
->cp_icase
)
2218 return STRNICMP(match
->cp_str
, str
, (size_t)len
) == 0;
2219 return STRNCMP(match
->cp_str
, str
, (size_t)len
) == 0;
2223 * Reduce the longest common string for match "match".
2226 ins_compl_longest_match(match
)
2233 if (compl_leader
== NULL
)
2235 /* First match, use it as a whole. */
2236 compl_leader
= vim_strsave(match
->cp_str
);
2237 if (compl_leader
!= NULL
)
2239 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2241 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
2244 /* When the match isn't there (to avoid matching itself) remove it
2245 * again after redrawing. */
2248 compl_used_match
= FALSE
;
2253 /* Reduce the text if this match differs from compl_leader. */
2261 c1
= mb_ptr2char(p
);
2262 c2
= mb_ptr2char(s
);
2270 if (match
->cp_icase
? (MB_TOLOWER(c1
) != MB_TOLOWER(c2
))
2289 /* Leader was shortened, need to change the inserted text. */
2291 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2293 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
2296 /* When the match isn't there (to avoid matching itself) remove it
2297 * again after redrawing. */
2302 compl_used_match
= FALSE
;
2307 * Add an array of matches to the list of matches.
2311 ins_compl_add_matches(num_matches
, matches
, icase
)
2318 int dir
= compl_direction
;
2320 for (i
= 0; i
< num_matches
&& add_r
!= FAIL
; i
++)
2321 if ((add_r
= ins_compl_add(matches
[i
], -1, icase
,
2322 NULL
, NULL
, dir
, 0, FALSE
)) == OK
)
2323 /* if dir was BACKWARD then honor it just once */
2325 FreeWild(num_matches
, matches
);
2328 /* Make the completion list cyclic.
2329 * Return the number of matches (excluding the original).
2332 ins_compl_make_cyclic()
2337 if (compl_first_match
!= NULL
)
2340 * Find the end of the list.
2342 match
= compl_first_match
;
2343 /* there's always an entry for the compl_orig_text, it doesn't count. */
2344 while (match
->cp_next
!= NULL
&& match
->cp_next
!= compl_first_match
)
2346 match
= match
->cp_next
;
2349 match
->cp_next
= compl_first_match
;
2350 compl_first_match
->cp_prev
= match
;
2356 * Start completion for the complete() function.
2357 * "startcol" is where the matched text starts (1 is first column).
2358 * "list" is the list of matches.
2361 set_completion(startcol
, list
)
2365 /* If already doing completions stop it. */
2366 if (ctrl_x_mode
!= 0)
2367 ins_compl_prep(' ');
2370 if (stop_arrow() == FAIL
)
2373 if (startcol
> (int)curwin
->w_cursor
.col
)
2374 startcol
= curwin
->w_cursor
.col
;
2375 compl_col
= startcol
;
2376 compl_length
= curwin
->w_cursor
.col
- startcol
;
2377 /* compl_pattern doesn't need to be set */
2378 compl_orig_text
= vim_strnsave(ml_get_curline() + compl_col
, compl_length
);
2379 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
2380 -1, FALSE
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
2383 /* Handle like dictionary completion. */
2384 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
2386 ins_compl_add_list(list
);
2387 compl_matches
= ins_compl_make_cyclic();
2388 compl_started
= TRUE
;
2389 compl_used_match
= TRUE
;
2391 compl_curr_match
= compl_first_match
;
2392 ins_complete(Ctrl_N
);
2397 /* "compl_match_array" points the currently displayed list of entries in the
2398 * popup menu. It is NULL when there is no popup menu. */
2399 static pumitem_T
*compl_match_array
= NULL
;
2400 static int compl_match_arraysize
;
2403 * Update the screen and when there is any scrolling remove the popup menu.
2410 if (compl_match_array
!= NULL
)
2412 h
= curwin
->w_cline_height
;
2414 if (h
!= curwin
->w_cline_height
)
2415 ins_compl_del_pum();
2420 * Remove any popup menu.
2425 if (compl_match_array
!= NULL
)
2428 vim_free(compl_match_array
);
2429 compl_match_array
= NULL
;
2434 * Return TRUE if the popup menu should be displayed.
2439 /* 'completeopt' must contain "menu" or "menuone" */
2440 if (vim_strchr(p_cot
, 'm') == NULL
)
2443 /* The display looks bad on a B&W display. */
2454 * Return TRUE if there are two or more matches to be shown in the popup menu.
2455 * One if 'completopt' contains "menuone".
2458 pum_enough_matches()
2463 /* Don't display the popup menu if there are no matches or there is only
2464 * one (ignoring the original text). */
2465 compl = compl_first_match
;
2470 || ((compl->cp_flags
& ORIGINAL_TEXT
) == 0 && ++i
== 2))
2472 compl = compl->cp_next
;
2473 } while (compl != compl_first_match
);
2475 if (strstr((char *)p_cot
, "menuone") != NULL
)
2481 * Show the popup menu for the list of matches.
2482 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
2485 ins_compl_show_pum()
2488 compl_T
*shown_compl
= NULL
;
2489 int did_find_shown_match
= FALSE
;
2490 int shown_match_ok
= FALSE
;
2496 if (!pum_wanted() || !pum_enough_matches())
2499 #if defined(FEAT_EVAL)
2500 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2501 do_cmdline_cmd((char_u
*)"if exists('g:loaded_matchparen')|3match none|endif");
2504 /* Update the screen before drawing the popup menu over it. */
2507 if (compl_match_array
== NULL
)
2509 /* Need to build the popup menu list. */
2510 compl_match_arraysize
= 0;
2511 compl = compl_first_match
;
2512 if (compl_leader
!= NULL
)
2513 lead_len
= (int)STRLEN(compl_leader
);
2516 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2517 && (compl_leader
== NULL
2518 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2519 ++compl_match_arraysize
;
2520 compl = compl->cp_next
;
2521 } while (compl != NULL
&& compl != compl_first_match
);
2522 if (compl_match_arraysize
== 0)
2524 compl_match_array
= (pumitem_T
*)alloc_clear(
2525 (unsigned)(sizeof(pumitem_T
)
2526 * compl_match_arraysize
));
2527 if (compl_match_array
!= NULL
)
2529 /* If the current match is the original text don't find the first
2530 * match after it, don't highlight anything. */
2531 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
2532 shown_match_ok
= TRUE
;
2535 compl = compl_first_match
;
2538 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2539 && (compl_leader
== NULL
2540 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2542 if (!shown_match_ok
)
2544 if (compl == compl_shown_match
|| did_find_shown_match
)
2546 /* This item is the shown match or this is the
2547 * first displayed item after the shown match. */
2548 compl_shown_match
= compl;
2549 did_find_shown_match
= TRUE
;
2550 shown_match_ok
= TRUE
;
2553 /* Remember this displayed match for when the
2554 * shown match is just below it. */
2555 shown_compl
= compl;
2559 if (compl->cp_text
[CPT_ABBR
] != NULL
)
2560 compl_match_array
[i
].pum_text
=
2561 compl->cp_text
[CPT_ABBR
];
2563 compl_match_array
[i
].pum_text
= compl->cp_str
;
2564 compl_match_array
[i
].pum_kind
= compl->cp_text
[CPT_KIND
];
2565 compl_match_array
[i
].pum_info
= compl->cp_text
[CPT_INFO
];
2566 if (compl->cp_text
[CPT_MENU
] != NULL
)
2567 compl_match_array
[i
++].pum_extra
=
2568 compl->cp_text
[CPT_MENU
];
2570 compl_match_array
[i
++].pum_extra
= compl->cp_fname
;
2573 if (compl == compl_shown_match
)
2575 did_find_shown_match
= TRUE
;
2577 /* When the original text is the shown match don't set
2578 * compl_shown_match. */
2579 if (compl->cp_flags
& ORIGINAL_TEXT
)
2580 shown_match_ok
= TRUE
;
2582 if (!shown_match_ok
&& shown_compl
!= NULL
)
2584 /* The shown match isn't displayed, set it to the
2585 * previously displayed match. */
2586 compl_shown_match
= shown_compl
;
2587 shown_match_ok
= TRUE
;
2590 compl = compl->cp_next
;
2591 } while (compl != NULL
&& compl != compl_first_match
);
2593 if (!shown_match_ok
) /* no displayed match at all */
2599 /* popup menu already exists, only need to find the current item.*/
2600 for (i
= 0; i
< compl_match_arraysize
; ++i
)
2601 if (compl_match_array
[i
].pum_text
== compl_shown_match
->cp_str
2602 || compl_match_array
[i
].pum_text
2603 == compl_shown_match
->cp_text
[CPT_ABBR
])
2610 if (compl_match_array
!= NULL
)
2612 /* Compute the screen column of the start of the completed text.
2613 * Use the cursor to get all wrapping and other settings right. */
2614 col
= curwin
->w_cursor
.col
;
2615 curwin
->w_cursor
.col
= compl_col
;
2616 pum_display(compl_match_array
, compl_match_arraysize
, cur
);
2617 curwin
->w_cursor
.col
= col
;
2621 #define DICT_FIRST (1) /* use just first element in "dict" */
2622 #define DICT_EXACT (2) /* "dict" is the exact name of a file */
2625 * Add any identifiers that match the given pattern in the list of dictionary
2626 * files "dict_start" to the list of completions.
2629 ins_compl_dictionaries(dict_start
, pat
, flags
, thesaurus
)
2632 int flags
; /* DICT_FIRST and/or DICT_EXACT */
2633 int thesaurus
; /* Thesaurus completion */
2635 char_u
*dict
= dict_start
;
2638 regmatch_T regmatch
;
2643 int dir
= compl_direction
;
2648 /* When 'dictionary' is empty and spell checking is enabled use
2650 if (!thesaurus
&& curwin
->w_p_spell
)
2651 dict
= (char_u
*)"spell";
2661 /* If 'infercase' is set, don't use 'smartcase' here */
2663 if (curbuf
->b_p_inf
)
2666 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2667 * to only match at the start of a line. Otherwise just match the
2668 * pattern. Also need to double backslashes. */
2669 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2671 char_u
*pat_esc
= vim_strsave_escaped(pat
, (char_u
*)"\\");
2673 if (pat_esc
== NULL
)
2675 i
= (int)STRLEN(pat_esc
) + 10;
2682 vim_snprintf((char *)ptr
, i
, "^\\s*\\zs\\V%s", pat_esc
);
2683 regmatch
.regprog
= vim_regcomp(ptr
, RE_MAGIC
);
2689 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
2690 if (regmatch
.regprog
== NULL
)
2694 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2695 regmatch
.rm_ic
= ignorecase(pat
);
2696 while (*dict
!= NUL
&& !got_int
&& !compl_interrupted
)
2698 /* copy one dictionary file name into buf */
2699 if (flags
== DICT_EXACT
)
2706 /* Expand wildcards in the dictionary name, but do not allow
2707 * backticks (for security, the 'dict' option may have been set in
2709 copy_option_part(&dict
, buf
, LSIZE
, ",");
2711 if (!thesaurus
&& STRCMP(buf
, "spell") == 0)
2715 if (vim_strchr(buf
, '`') != NULL
2716 || expand_wildcards(1, &buf
, &count
, &files
,
2717 EW_FILE
|EW_SILENT
) != OK
)
2724 /* Complete from active spelling. Skip "\<" in the pattern, we
2725 * don't use it as a RE. */
2726 if (pat
[0] == '\\' && pat
[1] == '<')
2730 spell_dump_compl(curbuf
, ptr
, regmatch
.rm_ic
, &dir
, 0);
2735 ins_compl_files(count
, files
, thesaurus
, flags
,
2736 ®match
, buf
, &dir
);
2737 if (flags
!= DICT_EXACT
)
2738 FreeWild(count
, files
);
2746 vim_free(regmatch
.regprog
);
2751 ins_compl_files(count
, files
, thesaurus
, flags
, regmatch
, buf
, dir
)
2756 regmatch_T
*regmatch
;
2765 for (i
= 0; i
< count
&& !got_int
&& !compl_interrupted
; i
++)
2767 fp
= mch_fopen((char *)files
[i
], "r"); /* open dictionary file */
2768 if (flags
!= DICT_EXACT
)
2770 vim_snprintf((char *)IObuff
, IOSIZE
,
2771 _("Scanning dictionary: %s"), (char *)files
[i
]);
2772 msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
2778 * Read dictionary file line by line.
2779 * Check each line for a match.
2781 while (!got_int
&& !compl_interrupted
2782 && !vim_fgets(buf
, LSIZE
, fp
))
2785 while (vim_regexec(regmatch
, buf
, (colnr_T
)(ptr
- buf
)))
2787 ptr
= regmatch
->startp
[0];
2788 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2789 ptr
= find_line_end(ptr
);
2791 ptr
= find_word_end(ptr
);
2792 add_r
= ins_compl_add_infercase(regmatch
->startp
[0],
2793 (int)(ptr
- regmatch
->startp
[0]),
2794 p_ic
, files
[i
], *dir
, 0);
2800 * Add the other matches on the line
2804 /* Find start of the next word. Skip white
2805 * space and punctuation. */
2806 ptr
= find_word_start(ptr
);
2807 if (*ptr
== NUL
|| *ptr
== NL
)
2811 /* Find end of the word and add it. */
2814 /* Japanese words may have characters in
2815 * different classes, only separate words
2816 * with single-byte non-word characters. */
2819 int l
= (*mb_ptr2len
)(ptr
);
2821 if (l
< 2 && !vim_iswordc(*ptr
))
2827 ptr
= find_word_end(ptr
);
2828 add_r
= ins_compl_add_infercase(wstart
,
2829 (int)(ptr
- wstart
),
2830 p_ic
, files
[i
], *dir
, 0);
2834 /* if dir was BACKWARD then honor it just once */
2836 else if (add_r
== FAIL
)
2838 /* avoid expensive call to vim_regexec() when at end
2840 if (*ptr
== '\n' || got_int
)
2844 ins_compl_check_keys(50);
2852 * Find the start of the next word.
2853 * Returns a pointer to the first char of the word. Also stops at a NUL.
2856 find_word_start(ptr
)
2861 while (*ptr
!= NUL
&& *ptr
!= '\n' && mb_get_class(ptr
) <= 1)
2862 ptr
+= (*mb_ptr2len
)(ptr
);
2865 while (*ptr
!= NUL
&& *ptr
!= '\n' && !vim_iswordc(*ptr
))
2871 * Find the end of the word. Assumes it starts inside a word.
2872 * Returns a pointer to just after the word.
2883 start_class
= mb_get_class(ptr
);
2884 if (start_class
> 1)
2887 ptr
+= (*mb_ptr2len
)(ptr
);
2888 if (mb_get_class(ptr
) != start_class
)
2894 while (vim_iswordc(*ptr
))
2900 * Find the end of the line, omitting CR and NL at the end.
2901 * Returns a pointer to just after the line.
2909 s
= ptr
+ STRLEN(ptr
);
2910 while (s
> ptr
&& (s
[-1] == CAR
|| s
[-1] == NL
))
2916 * Free the list of completions
2924 vim_free(compl_pattern
);
2925 compl_pattern
= NULL
;
2926 vim_free(compl_leader
);
2927 compl_leader
= NULL
;
2929 if (compl_first_match
== NULL
)
2932 ins_compl_del_pum();
2935 compl_curr_match
= compl_first_match
;
2938 match
= compl_curr_match
;
2939 compl_curr_match
= compl_curr_match
->cp_next
;
2940 vim_free(match
->cp_str
);
2941 /* several entries may use the same fname, free it just once. */
2942 if (match
->cp_flags
& FREE_FNAME
)
2943 vim_free(match
->cp_fname
);
2944 for (i
= 0; i
< CPT_COUNT
; ++i
)
2945 vim_free(match
->cp_text
[i
]);
2947 } while (compl_curr_match
!= NULL
&& compl_curr_match
!= compl_first_match
);
2948 compl_first_match
= compl_curr_match
= NULL
;
2954 compl_cont_status
= 0;
2955 compl_started
= FALSE
;
2957 vim_free(compl_pattern
);
2958 compl_pattern
= NULL
;
2959 vim_free(compl_leader
);
2960 compl_leader
= NULL
;
2961 edit_submode_extra
= NULL
;
2962 vim_free(compl_orig_text
);
2963 compl_orig_text
= NULL
;
2964 compl_enter_selects
= FALSE
;
2968 * Return TRUE when Insert completion is active.
2973 return compl_started
;
2977 * Delete one character before the cursor and show the subset of the matches
2978 * that match the word that is now before the cursor.
2979 * Returns the character to be used, NUL if the work is done and another char
2980 * to be got from the user.
2988 line
= ml_get_curline();
2989 p
= line
+ curwin
->w_cursor
.col
;
2990 mb_ptr_back(line
, p
);
2992 /* Stop completion when the whole word was deleted. */
2993 if ((int)(p
- line
) - (int)compl_col
<= 0)
2996 if (curwin
->w_cursor
.col
<= compl_col
+ compl_length
)
2998 /* Deleted more than what was used to find matches, need to look for
2999 * matches all over again. */
3001 compl_started
= FALSE
;
3003 compl_cont_status
= 0;
3004 compl_cont_mode
= 0;
3007 vim_free(compl_leader
);
3008 compl_leader
= vim_strnsave(line
+ compl_col
, (int)(p
- line
) - compl_col
);
3009 if (compl_leader
!= NULL
)
3011 ins_compl_del_pum();
3013 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
3016 ins_compl_set_original_text(compl_leader
);
3020 spell_bad_len
= 0; /* need to redetect bad word */
3022 /* Matches were cleared, need to search for them now. */
3023 if (ins_complete(Ctrl_N
) == FAIL
)
3024 compl_cont_status
= 0;
3027 /* Remove the completed word again. */
3029 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
3033 /* Go to the original text, since none of the matches is inserted. */
3034 if (compl_first_match
->cp_prev
!= NULL
3035 && (compl_first_match
->cp_prev
->cp_flags
& ORIGINAL_TEXT
))
3036 compl_shown_match
= compl_first_match
->cp_prev
;
3038 compl_shown_match
= compl_first_match
;
3039 compl_curr_match
= compl_shown_match
;
3040 compl_shows_dir
= compl_direction
;
3042 /* Show the popup menu with a different set of matches. */
3043 ins_compl_show_pum();
3044 compl_used_match
= FALSE
;
3045 compl_enter_selects
= FALSE
;
3053 * Append one character to the match leader. May reduce the number of
3057 ins_compl_addleader(c
)
3063 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
3065 char_u buf
[MB_MAXBYTES
+ 1];
3067 (*mb_char2bytes
)(c
, buf
);
3069 ins_char_bytes(buf
, cc
);
3075 vim_free(compl_leader
);
3076 compl_leader
= vim_strnsave(ml_get_curline() + compl_col
,
3077 curwin
->w_cursor
.col
- compl_col
);
3078 if (compl_leader
!= NULL
)
3080 /* Show the popup menu with a different set of matches. */
3081 ins_compl_del_pum();
3082 ins_compl_show_pum();
3083 compl_used_match
= FALSE
;
3084 compl_enter_selects
= FALSE
;
3085 ins_compl_set_original_text(compl_leader
);
3090 * Set the first match, the original text.
3093 ins_compl_set_original_text(str
)
3098 /* Replace the original text entry. */
3099 if (compl_first_match
->cp_flags
& ORIGINAL_TEXT
) /* safety check */
3101 p
= vim_strsave(str
);
3104 vim_free(compl_first_match
->cp_str
);
3105 compl_first_match
->cp_str
= p
;
3111 * Append one character to the match leader. May reduce the number of
3115 ins_compl_addfrommatch()
3118 int len
= curwin
->w_cursor
.col
- compl_col
;
3121 p
= compl_shown_match
->cp_str
;
3122 if ((int)STRLEN(p
) <= len
) /* the match is too short */
3130 ins_compl_addleader(c
);
3134 * Prepare for Insert mode completion, or stop it.
3135 * Called just after typing a character in Insert mode.
3136 * Returns TRUE when the character is not to be inserted;
3147 /* Forget any previous 'special' messages if this is actually
3148 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3150 if (c
!= Ctrl_R
&& vim_is_ctrl_x_key(c
))
3151 edit_submode_extra
= NULL
;
3153 /* Ignore end of Select mode mapping */
3157 /* Set "compl_get_longest" when finding the first matches. */
3158 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
3159 || (ctrl_x_mode
== 0 && !compl_started
))
3161 compl_get_longest
= (vim_strchr(p_cot
, 'l') != NULL
);
3162 compl_used_match
= TRUE
;
3165 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
)
3168 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3169 * it will be yet. Now we decide.
3175 ctrl_x_mode
= CTRL_X_SCROLL
;
3176 if (!(State
& REPLACE_FLAG
))
3177 edit_submode
= (char_u
*)_(" (insert) Scroll (^E/^Y)");
3179 edit_submode
= (char_u
*)_(" (replace) Scroll (^E/^Y)");
3180 edit_submode_pre
= NULL
;
3184 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
3187 ctrl_x_mode
= CTRL_X_FILES
;
3190 ctrl_x_mode
= CTRL_X_DICTIONARY
;
3193 /* Simply allow ^R to happen without affecting ^X mode */
3196 ctrl_x_mode
= CTRL_X_THESAURUS
;
3198 #ifdef FEAT_COMPL_FUNC
3200 ctrl_x_mode
= CTRL_X_FUNCTION
;
3203 ctrl_x_mode
= CTRL_X_OMNI
;
3208 ctrl_x_mode
= CTRL_X_SPELL
;
3210 ++emsg_off
; /* Avoid getting the E756 error twice. */
3211 spell_back_to_badword();
3216 ctrl_x_mode
= CTRL_X_TAGS
;
3221 ctrl_x_mode
= CTRL_X_PATH_PATTERNS
;
3224 ctrl_x_mode
= CTRL_X_PATH_DEFINES
;
3229 ctrl_x_mode
= CTRL_X_CMDLINE
;
3233 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3234 * just started ^X mode, or there were enough ^X's to cancel
3235 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3236 * do normal expansion when interrupting a different mode (say
3237 * ^X^F^X^P or ^P^X^X^P, see below)
3238 * nothing changes if interrupting mode 0, (eg, the flag
3239 * doesn't change when going to ADDING mode -- Acevedo */
3240 if (!(compl_cont_status
& CONT_INTRPT
))
3241 compl_cont_status
|= CONT_LOCAL
;
3242 else if (compl_cont_mode
!= 0)
3243 compl_cont_status
&= ~CONT_LOCAL
;
3246 /* If we have typed at least 2 ^X's... for modes != 0, we set
3247 * compl_cont_status = 0 (eg, as if we had just started ^X
3249 * For mode 0, we set "compl_cont_mode" to an impossible
3250 * value, in both cases ^X^X can be used to restart the same
3251 * mode (avoiding ADDING mode).
3252 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3253 * 'complete' and local ^P expansions respectively.
3254 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3255 * mode -- Acevedo */
3258 if (compl_cont_mode
!= 0)
3259 compl_cont_status
= 0;
3261 compl_cont_mode
= CTRL_X_NOT_DEFINED_YET
;
3264 edit_submode
= NULL
;
3269 else if (ctrl_x_mode
!= 0)
3271 /* We're already in CTRL-X mode, do we stay in it? */
3272 if (!vim_is_ctrl_x_key(c
))
3274 if (ctrl_x_mode
== CTRL_X_SCROLL
)
3277 ctrl_x_mode
= CTRL_X_FINISHED
;
3278 edit_submode
= NULL
;
3283 if (compl_started
|| ctrl_x_mode
== CTRL_X_FINISHED
)
3285 /* Show error message from attempted keyword completion (probably
3286 * 'Pattern not found') until another key is hit, then go back to
3287 * showing what mode we are in. */
3289 if ((ctrl_x_mode
== 0 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_R
3290 && !ins_compl_pum_key(c
))
3291 || ctrl_x_mode
== CTRL_X_FINISHED
)
3293 /* Get here when we have finished typing a sequence of ^N and
3294 * ^P or other completion characters in CTRL-X mode. Free up
3295 * memory that was used, and make sure we can redo the insert. */
3296 if (compl_curr_match
!= NULL
|| compl_leader
!= NULL
|| c
== Ctrl_E
)
3301 * If any of the original typed text has been changed, eg when
3302 * ignorecase is set, we must add back-spaces to the redo
3303 * buffer. We add as few as necessary to delete just the part
3304 * of the original text that has changed.
3305 * When using the longest match, edited the match or used
3306 * CTRL-E then don't use the current match.
3308 if (compl_curr_match
!= NULL
&& compl_used_match
&& c
!= Ctrl_E
)
3309 ptr
= compl_curr_match
->cp_str
;
3310 else if (compl_leader
!= NULL
)
3313 ptr
= compl_orig_text
;
3314 p
= compl_orig_text
;
3315 for (temp
= 0; p
[temp
] != NUL
&& p
[temp
] == ptr
[temp
]; ++temp
)
3319 temp
-= (*mb_head_off
)(compl_orig_text
, p
+ temp
);
3321 for (p
+= temp
; *p
!= NUL
; mb_ptr_adv(p
))
3322 AppendCharToRedobuff(K_BS
);
3323 AppendToRedobuffLit(ptr
+ temp
, -1);
3327 want_cindent
= (can_cindent
&& cindent_on());
3330 * When completing whole lines: fix indent for 'cindent'.
3331 * Otherwise, break line if it's too long.
3333 if (compl_cont_mode
== CTRL_X_WHOLE_LINE
)
3336 /* re-indent the current line */
3340 want_cindent
= FALSE
; /* don't do it again */
3346 /* put the cursor on the last char, for 'tw' formatting */
3347 curwin
->w_cursor
.col
--;
3348 if (stop_arrow() == OK
)
3349 insertchar(NUL
, 0, -1);
3350 curwin
->w_cursor
.col
++;
3353 auto_format(FALSE
, TRUE
);
3355 /* If the popup menu is displayed pressing CTRL-Y means accepting
3356 * the selection without inserting anything. When
3357 * compl_enter_selects is set the Enter key does the same. */
3358 if ((c
== Ctrl_Y
|| (compl_enter_selects
3359 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
3363 /* CTRL-E means completion is Ended, go back to the typed text. */
3367 if (compl_leader
!= NULL
)
3368 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
3369 else if (compl_first_match
!= NULL
)
3370 ins_bytes(compl_orig_text
3371 + curwin
->w_cursor
.col
- compl_col
);
3376 compl_started
= FALSE
;
3378 msg_clr_cmdline(); /* necessary for "noshowmode" */
3380 compl_enter_selects
= FALSE
;
3381 if (edit_submode
!= NULL
)
3383 edit_submode
= NULL
;
3389 * Indent now if a key was typed that is in 'cinkeys'.
3391 if (want_cindent
&& in_cinkeys(KEY_COMPLETE
, ' ', inindent(0)))
3397 /* reset continue_* if we left expansion-mode, if we stay they'll be
3398 * (re)set properly in ins_complete() */
3399 if (!vim_is_ctrl_x_key(c
))
3401 compl_cont_status
= 0;
3402 compl_cont_mode
= 0;
3409 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3410 * (depending on flag) starting from buf and looking for a non-scanned
3411 * buffer (other than curbuf). curbuf is special, if it is called with
3412 * buf=curbuf then it has to be the first call for a given flag/expansion.
3414 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3417 ins_compl_next_buf(buf
, flag
)
3425 if (flag
== 'w') /* just windows */
3428 if (buf
== curbuf
) /* first call for this flag/expansion */
3430 while ((wp
= (wp
->w_next
!= NULL
? wp
->w_next
: firstwin
)) != curwin
3431 && wp
->w_buffer
->b_scanned
)
3439 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3440 * (unlisted buffers)
3441 * When completing whole lines skip unloaded buffers. */
3442 while ((buf
= (buf
->b_next
!= NULL
? buf
->b_next
: firstbuf
)) != curbuf
3446 || (buf
->b_ml
.ml_mfp
== NULL
) != (flag
== 'u')))
3452 #ifdef FEAT_COMPL_FUNC
3453 static void expand_by_function
__ARGS((int type
, char_u
*base
));
3456 * Execute user defined complete function 'completefunc' or 'omnifunc', and
3457 * get matches in "matches".
3460 expand_by_function(type
, base
)
3461 int type
; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
3469 funcname
= (type
== CTRL_X_FUNCTION
) ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
3470 if (*funcname
== NUL
)
3473 /* Call 'completefunc' to obtain the list of matches. */
3474 args
[0] = (char_u
*)"0";
3477 pos
= curwin
->w_cursor
;
3478 matchlist
= call_func_retlist(funcname
, 2, args
, FALSE
);
3479 curwin
->w_cursor
= pos
; /* restore the cursor position */
3480 if (matchlist
== NULL
)
3483 ins_compl_add_list(matchlist
);
3484 list_unref(matchlist
);
3486 #endif /* FEAT_COMPL_FUNC */
3488 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
3490 * Add completions from a list.
3493 ins_compl_add_list(list
)
3497 int dir
= compl_direction
;
3499 /* Go through the List with matches and add each of them. */
3500 for (li
= list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3502 if (ins_compl_add_tv(&li
->li_tv
, dir
) == OK
)
3503 /* if dir was BACKWARD then honor it just once */
3511 * Add a match to the list of matches from a typeval_T.
3512 * If the given string is already in the list of completions, then return
3513 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3514 * maybe because alloc() returns NULL, then FAIL is returned.
3517 ins_compl_add_tv(tv
, dir
)
3524 char_u
*(cptext
[CPT_COUNT
]);
3526 if (tv
->v_type
== VAR_DICT
&& tv
->vval
.v_dict
!= NULL
)
3528 word
= get_dict_string(tv
->vval
.v_dict
, (char_u
*)"word", FALSE
);
3529 cptext
[CPT_ABBR
] = get_dict_string(tv
->vval
.v_dict
,
3530 (char_u
*)"abbr", FALSE
);
3531 cptext
[CPT_MENU
] = get_dict_string(tv
->vval
.v_dict
,
3532 (char_u
*)"menu", FALSE
);
3533 cptext
[CPT_KIND
] = get_dict_string(tv
->vval
.v_dict
,
3534 (char_u
*)"kind", FALSE
);
3535 cptext
[CPT_INFO
] = get_dict_string(tv
->vval
.v_dict
,
3536 (char_u
*)"info", FALSE
);
3537 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"icase", FALSE
) != NULL
)
3538 icase
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"icase");
3539 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"dup", FALSE
) != NULL
)
3540 dup
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"dup");
3544 word
= get_tv_string_chk(tv
);
3545 vim_memset(cptext
, 0, sizeof(cptext
));
3547 if (word
== NULL
|| *word
== NUL
)
3549 return ins_compl_add(word
, -1, icase
, NULL
, cptext
, dir
, 0, dup
);
3554 * Get the next expansion(s), using "compl_pattern".
3555 * The search starts at position "ini" in curbuf and in the direction
3557 * When "compl_started" is FALSE start at that position, otherwise continue
3558 * where we stopped searching before.
3559 * This may return before finding all the matches.
3560 * Return the total number of matches or -1 if still unknown -- Acevedo
3563 ins_compl_get_exp(ini
)
3566 static pos_T first_match_pos
;
3567 static pos_T last_match_pos
;
3568 static char_u
*e_cpt
= (char_u
*)""; /* curr. entry in 'complete' */
3569 static int found_all
= FALSE
; /* Found all matches of a
3571 static buf_T
*ins_buf
= NULL
; /* buffer being scanned */
3581 int found_new_match
;
3582 int type
= ctrl_x_mode
;
3584 char_u
*dict
= NULL
;
3590 for (ins_buf
= firstbuf
; ins_buf
!= NULL
; ins_buf
= ins_buf
->b_next
)
3591 ins_buf
->b_scanned
= 0;
3594 e_cpt
= (compl_cont_status
& CONT_LOCAL
)
3595 ? (char_u
*)"." : curbuf
->b_p_cpt
;
3596 last_match_pos
= first_match_pos
= *ini
;
3599 old_match
= compl_curr_match
; /* remember the last current match */
3600 pos
= (compl_direction
== FORWARD
) ? &last_match_pos
: &first_match_pos
;
3601 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3604 found_new_match
= FAIL
;
3606 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
3607 * or if found_all says this entry is done. For ^X^L only use the
3608 * entries from 'complete' that look in loaded buffers. */
3609 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3610 && (!compl_started
|| found_all
))
3613 while (*e_cpt
== ',' || *e_cpt
== ' ')
3615 if (*e_cpt
== '.' && !curbuf
->b_scanned
)
3618 first_match_pos
= *ini
;
3619 /* So that ^N can match word immediately after cursor */
3620 if (ctrl_x_mode
== 0)
3621 dec(&first_match_pos
);
3622 last_match_pos
= first_match_pos
;
3625 else if (vim_strchr((char_u
*)"buwU", *e_cpt
) != NULL
3626 && (ins_buf
= ins_compl_next_buf(ins_buf
, *e_cpt
)) != curbuf
)
3628 /* Scan a buffer, but not the current one. */
3629 if (ins_buf
->b_ml
.ml_mfp
!= NULL
) /* loaded buffer */
3631 compl_started
= TRUE
;
3632 first_match_pos
.col
= last_match_pos
.col
= 0;
3633 first_match_pos
.lnum
= ins_buf
->b_ml
.ml_line_count
+ 1;
3634 last_match_pos
.lnum
= 0;
3637 else /* unloaded buffer, scan like dictionary */
3640 if (ins_buf
->b_fname
== NULL
)
3642 type
= CTRL_X_DICTIONARY
;
3643 dict
= ins_buf
->b_fname
;
3644 dict_f
= DICT_EXACT
;
3646 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning: %s"),
3647 ins_buf
->b_fname
== NULL
3648 ? buf_spname(ins_buf
)
3649 : ins_buf
->b_sfname
== NULL
3650 ? (char *)ins_buf
->b_fname
3651 : (char *)ins_buf
->b_sfname
);
3652 msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3654 else if (*e_cpt
== NUL
)
3658 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3660 else if (*e_cpt
== 'k' || *e_cpt
== 's')
3663 type
= CTRL_X_DICTIONARY
;
3665 type
= CTRL_X_THESAURUS
;
3666 if (*++e_cpt
!= ',' && *e_cpt
!= NUL
)
3669 dict_f
= DICT_FIRST
;
3673 else if (*e_cpt
== 'i')
3674 type
= CTRL_X_PATH_PATTERNS
;
3675 else if (*e_cpt
== 'd')
3676 type
= CTRL_X_PATH_DEFINES
;
3678 else if (*e_cpt
== ']' || *e_cpt
== 't')
3681 sprintf((char*)IObuff
, _("Scanning tags."));
3682 msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3687 /* in any case e_cpt is advanced to the next entry */
3688 (void)copy_option_part(&e_cpt
, IObuff
, IOSIZE
, ",");
3701 case CTRL_X_PATH_PATTERNS
:
3702 case CTRL_X_PATH_DEFINES
:
3703 find_pattern_in_path(compl_pattern
, compl_direction
,
3704 (int)STRLEN(compl_pattern
), FALSE
, FALSE
,
3705 (type
== CTRL_X_PATH_DEFINES
3706 && !(compl_cont_status
& CONT_SOL
))
3707 ? FIND_DEFINE
: FIND_ANY
, 1L, ACTION_EXPAND
,
3708 (linenr_T
)1, (linenr_T
)MAXLNUM
);
3712 case CTRL_X_DICTIONARY
:
3713 case CTRL_X_THESAURUS
:
3714 ins_compl_dictionaries(
3716 : (type
== CTRL_X_THESAURUS
3717 ? (*curbuf
->b_p_tsr
== NUL
3720 : (*curbuf
->b_p_dict
== NUL
3722 : curbuf
->b_p_dict
)),
3724 dict
!= NULL
? dict_f
3725 : 0, type
== CTRL_X_THESAURUS
);
3730 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3732 p_ic
= ignorecase(compl_pattern
);
3734 /* Find up to TAG_MANY matches. Avoids that an enourmous number
3735 * of matches is found when compl_pattern is empty */
3736 if (find_tags(compl_pattern
, &num_matches
, &matches
,
3737 TAG_REGEXP
| TAG_NAMES
| TAG_NOIC
|
3738 TAG_INS_COMP
| (ctrl_x_mode
? TAG_VERBOSE
: 0),
3739 TAG_MANY
, curbuf
->b_ffname
) == OK
&& num_matches
> 0)
3741 ins_compl_add_matches(num_matches
, matches
, p_ic
);
3747 if (expand_wildcards(1, &compl_pattern
, &num_matches
, &matches
,
3748 EW_FILE
|EW_DIR
|EW_ADDSLASH
|EW_SILENT
) == OK
)
3751 /* May change home directory back to "~". */
3752 tilde_replace(compl_pattern
, num_matches
, matches
);
3753 ins_compl_add_matches(num_matches
, matches
,
3754 #ifdef CASE_INSENSITIVE_FILENAME
3763 case CTRL_X_CMDLINE
:
3764 if (expand_cmdline(&compl_xp
, compl_pattern
,
3765 (int)STRLEN(compl_pattern
),
3766 &num_matches
, &matches
) == EXPAND_OK
)
3767 ins_compl_add_matches(num_matches
, matches
, FALSE
);
3770 #ifdef FEAT_COMPL_FUNC
3771 case CTRL_X_FUNCTION
:
3773 expand_by_function(type
, compl_pattern
);
3779 num_matches
= expand_spelling(first_match_pos
.lnum
,
3780 first_match_pos
.col
, compl_pattern
, &matches
);
3781 if (num_matches
> 0)
3782 ins_compl_add_matches(num_matches
, matches
, FALSE
);
3786 default: /* normal ^P/^N and ^X^L */
3788 * If 'infercase' is set, don't use 'smartcase' here
3791 if (ins_buf
->b_p_inf
)
3794 /* buffers other than curbuf are scanned from the beginning or the
3795 * end but never from the middle, thus setting nowrapscan in this
3796 * buffers is a good idea, on the other hand, we always set
3797 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3799 if (ins_buf
!= curbuf
)
3801 else if (*e_cpt
== '.')
3807 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3808 * has added a word that was at the beginning of the line */
3809 if ( ctrl_x_mode
== CTRL_X_WHOLE_LINE
3810 || (compl_cont_status
& CONT_SOL
))
3811 found_new_match
= search_for_exact_line(ins_buf
, pos
,
3812 compl_direction
, compl_pattern
);
3814 found_new_match
= searchit(NULL
, ins_buf
, pos
,
3816 compl_pattern
, 1L, SEARCH_KEEP
+ SEARCH_NFMSG
,
3817 RE_LAST
, (linenr_T
)0);
3820 /* set "compl_started" even on fail */
3821 compl_started
= TRUE
;
3822 first_match_pos
= *pos
;
3823 last_match_pos
= *pos
;
3825 else if (first_match_pos
.lnum
== last_match_pos
.lnum
3826 && first_match_pos
.col
== last_match_pos
.col
)
3827 found_new_match
= FAIL
;
3828 if (found_new_match
== FAIL
)
3830 if (ins_buf
== curbuf
)
3835 /* when ADDING, the text before the cursor matches, skip it */
3836 if ( (compl_cont_status
& CONT_ADDING
) && ins_buf
== curbuf
3837 && ini
->lnum
== pos
->lnum
3838 && ini
->col
== pos
->col
)
3840 ptr
= ml_get_buf(ins_buf
, pos
->lnum
, FALSE
) + pos
->col
;
3841 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3843 if (compl_cont_status
& CONT_ADDING
)
3845 if (pos
->lnum
>= ins_buf
->b_ml
.ml_line_count
)
3847 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
3849 ptr
= skipwhite(ptr
);
3851 len
= (int)STRLEN(ptr
);
3855 char_u
*tmp_ptr
= ptr
;
3857 if (compl_cont_status
& CONT_ADDING
)
3859 tmp_ptr
+= compl_length
;
3860 /* Skip if already inside a word. */
3861 if (vim_iswordp(tmp_ptr
))
3863 /* Find start of next word. */
3864 tmp_ptr
= find_word_start(tmp_ptr
);
3866 /* Find end of this word. */
3867 tmp_ptr
= find_word_end(tmp_ptr
);
3868 len
= (int)(tmp_ptr
- ptr
);
3870 if ((compl_cont_status
& CONT_ADDING
)
3871 && len
== compl_length
)
3873 if (pos
->lnum
< ins_buf
->b_ml
.ml_line_count
)
3875 /* Try next line, if any. the new word will be
3876 * "join" as if the normal command "J" was used.
3877 * IOSIZE is always greater than
3878 * compl_length, so the next STRNCPY always
3879 * works -- Acevedo */
3880 STRNCPY(IObuff
, ptr
, len
);
3881 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
3882 tmp_ptr
= ptr
= skipwhite(ptr
);
3883 /* Find start of next word. */
3884 tmp_ptr
= find_word_start(tmp_ptr
);
3885 /* Find end of next word. */
3886 tmp_ptr
= find_word_end(tmp_ptr
);
3889 if (*ptr
!= ')' && IObuff
[len
- 1] != TAB
)
3891 if (IObuff
[len
- 1] != ' ')
3892 IObuff
[len
++] = ' ';
3893 /* IObuf =~ "\k.* ", thus len >= 2 */
3895 && (IObuff
[len
- 2] == '.'
3896 || (vim_strchr(p_cpo
, CPO_JOINSP
)
3898 && (IObuff
[len
- 2] == '?'
3899 || IObuff
[len
- 2] == '!'))))
3900 IObuff
[len
++] = ' ';
3902 /* copy as much as posible of the new word */
3903 if (tmp_ptr
- ptr
>= IOSIZE
- len
)
3904 tmp_ptr
= ptr
+ IOSIZE
- len
- 1;
3905 STRNCPY(IObuff
+ len
, ptr
, tmp_ptr
- ptr
);
3906 len
+= (int)(tmp_ptr
- ptr
);
3907 flags
|= CONT_S_IPOS
;
3912 if (len
== compl_length
)
3916 if (ins_compl_add_infercase(ptr
, len
, p_ic
,
3917 ins_buf
== curbuf
? NULL
: ins_buf
->b_sfname
,
3918 0, flags
) != NOTDONE
)
3920 found_new_match
= OK
;
3928 /* check if compl_curr_match has changed, (e.g. other type of
3929 * expansion added somenthing) */
3930 if (type
!= 0 && compl_curr_match
!= old_match
)
3931 found_new_match
= OK
;
3933 /* break the loop for specialized modes (use 'complete' just for the
3934 * generic ctrl_x_mode == 0) or when we've found a new match */
3935 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
3936 || found_new_match
!= FAIL
)
3940 /* Fill the popup menu as soon as possible. */
3941 if (pum_wanted() && type
!= -1)
3942 ins_compl_check_keys(0);
3944 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
3945 || compl_interrupted
)
3947 compl_started
= TRUE
;
3951 /* Mark a buffer scanned when it has been scanned completely */
3952 if (type
== 0 || type
== CTRL_X_PATH_PATTERNS
)
3953 ins_buf
->b_scanned
= TRUE
;
3955 compl_started
= FALSE
;
3958 compl_started
= TRUE
;
3960 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3961 && *e_cpt
== NUL
) /* Got to end of 'complete' */
3962 found_new_match
= FAIL
;
3964 i
= -1; /* total of matches, unknown */
3965 if (found_new_match
== FAIL
3966 || (ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
))
3967 i
= ins_compl_make_cyclic();
3969 /* If several matches were added (FORWARD) or the search failed and has
3970 * just been made cyclic then we have to move compl_curr_match to the next
3971 * or previous entry (if any) -- Acevedo */
3972 compl_curr_match
= compl_direction
== FORWARD
? old_match
->cp_next
3973 : old_match
->cp_prev
;
3974 if (compl_curr_match
== NULL
)
3975 compl_curr_match
= old_match
;
3979 /* Delete the old text being completed. */
3986 * In insert mode: Delete the typed part.
3987 * In replace mode: Put the old characters back, if any.
3989 i
= compl_col
+ (compl_cont_status
& CONT_ADDING
? compl_length
: 0);
3990 backspace_until_column(i
);
3991 changed_cline_bef_curs();
3994 /* Insert the new text being completed. */
3998 ins_bytes(compl_shown_match
->cp_str
+ curwin
->w_cursor
.col
- compl_col
);
3999 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
4000 compl_used_match
= FALSE
;
4002 compl_used_match
= TRUE
;
4006 * Fill in the next completion in the current direction.
4007 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4008 * get more completions. If it is FALSE, then we just do nothing when there
4009 * are no more completions in a given direction. The latter case is used when
4010 * we are still in the middle of finding completions, to allow browsing
4011 * through the ones found so far.
4012 * Return the total number of matches, or -1 if still unknown -- webb.
4014 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4015 * compl_shown_match here.
4017 * Note that this function may be called recursively once only. First with
4018 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4019 * calls this function with "allow_get_expansion" FALSE.
4022 ins_compl_next(allow_get_expansion
, count
, insert_match
)
4023 int allow_get_expansion
;
4024 int count
; /* repeat completion this many times; should
4026 int insert_match
; /* Insert the newly selected match */
4028 int num_matches
= -1;
4031 compl_T
*found_compl
= NULL
;
4032 int found_end
= FALSE
;
4035 if (compl_leader
!= NULL
4036 && (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0)
4038 /* Set "compl_shown_match" to the actually shown match, it may differ
4039 * when "compl_leader" is used to omit some of the matches. */
4040 while (!ins_compl_equal(compl_shown_match
,
4041 compl_leader
, (int)STRLEN(compl_leader
))
4042 && compl_shown_match
->cp_next
!= NULL
4043 && compl_shown_match
->cp_next
!= compl_first_match
)
4044 compl_shown_match
= compl_shown_match
->cp_next
;
4047 if (allow_get_expansion
&& insert_match
4048 && (!compl_get_longest
|| compl_used_match
))
4049 /* Delete old text to be replaced */
4052 /* When finding the longest common text we stick at the original text,
4053 * don't let CTRL-N or CTRL-P move to the first match. */
4054 advance
= count
!= 1 || !allow_get_expansion
|| !compl_get_longest
;
4056 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4060 if (compl_shows_dir
== FORWARD
&& compl_shown_match
->cp_next
!= NULL
)
4062 if (compl_pending
!= 0)
4064 compl_shown_match
= compl_shown_match
->cp_next
;
4065 found_end
= (compl_first_match
!= NULL
4066 && (compl_shown_match
->cp_next
== compl_first_match
4067 || compl_shown_match
== compl_first_match
));
4069 else if (compl_shows_dir
== BACKWARD
4070 && compl_shown_match
->cp_prev
!= NULL
)
4072 if (compl_pending
!= 0)
4074 found_end
= (compl_shown_match
== compl_first_match
);
4075 compl_shown_match
= compl_shown_match
->cp_prev
;
4076 found_end
|= (compl_shown_match
== compl_first_match
);
4082 if (compl_shows_dir
== BACKWARD
)
4087 if (!allow_get_expansion
)
4090 num_matches
= ins_compl_get_exp(&compl_startpos
);
4091 if (compl_pending
!= 0 && compl_direction
== compl_shows_dir
4093 compl_shown_match
= compl_curr_match
;
4096 if ((compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0
4097 && compl_leader
!= NULL
4098 && !ins_compl_equal(compl_shown_match
,
4099 compl_leader
, (int)STRLEN(compl_leader
)))
4102 /* Remember a matching item. */
4103 found_compl
= compl_shown_match
;
4105 /* Stop at the end of the list when we found a usable match. */
4108 if (found_compl
!= NULL
)
4110 compl_shown_match
= found_compl
;
4113 todo
= 1; /* use first usable match after wrapping around */
4117 /* Insert the text of the new completion, or the compl_leader. */
4120 if (!compl_get_longest
|| compl_used_match
)
4123 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
4126 compl_used_match
= FALSE
;
4128 if (!allow_get_expansion
)
4130 /* may undisplay the popup menu first */
4131 ins_compl_upd_pum();
4133 /* redraw to show the user what was inserted */
4136 /* display the updated popup menu */
4137 ins_compl_show_pum();
4139 /* Delete old text to be replaced, since we're still searching and
4140 * don't want to match ourselves! */
4144 /* Enter will select a match when the match wasn't inserted and the popup
4145 * menu is visislbe. */
4146 compl_enter_selects
= !insert_match
&& compl_match_array
!= NULL
;
4149 * Show the file name for the match (if any)
4150 * Truncate the file name to avoid a wait for return.
4152 if (compl_shown_match
->cp_fname
!= NULL
)
4154 STRCPY(IObuff
, "match in file ");
4155 i
= (vim_strsize(compl_shown_match
->cp_fname
) + 16) - sc_col
;
4159 STRCAT(IObuff
, "<");
4160 STRCAT(IObuff
, compl_shown_match
->cp_fname
+ i
);
4162 redraw_cmdline
= FALSE
; /* don't overwrite! */
4169 * Call this while finding completions, to check whether the user has hit a key
4170 * that should change the currently displayed completion, or exit completion
4171 * mode. Also, when compl_pending is not zero, show a completion as soon as
4173 * "frequency" specifies out of how many calls we actually check.
4176 ins_compl_check_keys(frequency
)
4179 static int count
= 0;
4183 /* Don't check when reading keys from a script. That would break the test
4188 /* Only do this at regular intervals */
4189 if (++count
< frequency
)
4198 if (vim_is_ctrl_x_key(c
) && c
!= Ctrl_X
&& c
!= Ctrl_R
)
4200 c
= safe_vgetc(); /* Eat the character */
4201 compl_shows_dir
= ins_compl_key2dir(c
);
4202 (void)ins_compl_next(FALSE
, ins_compl_key2count(c
),
4203 c
!= K_UP
&& c
!= K_DOWN
);
4205 else if (c
!= Ctrl_R
)
4206 compl_interrupted
= TRUE
;
4208 if (compl_pending
!= 0 && !got_int
)
4209 (void)ins_compl_next(FALSE
, compl_pending
> 0
4210 ? compl_pending
: -compl_pending
, TRUE
);
4214 * Decide the direction of Insert mode complete from the key typed.
4215 * Returns BACKWARD or FORWARD.
4218 ins_compl_key2dir(c
)
4221 if (c
== Ctrl_P
|| c
== Ctrl_L
4222 || (pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
4223 || c
== K_S_UP
|| c
== K_UP
)))
4229 * Return TRUE for keys that are used for completion only when the popup menu
4233 ins_compl_pum_key(c
)
4236 return pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
|| c
== K_S_UP
4237 || c
== K_PAGEDOWN
|| c
== K_KPAGEDOWN
|| c
== K_S_DOWN
4238 || c
== K_UP
|| c
== K_DOWN
);
4242 * Decide the number of completions to move forward.
4243 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4246 ins_compl_key2count(c
)
4251 if (ins_compl_pum_key(c
) && c
!= K_UP
&& c
!= K_DOWN
)
4253 h
= pum_get_height();
4255 h
-= 2; /* keep some context */
4262 * Return TRUE if completion with "c" should insert the match, FALSE if only
4263 * to change the currently selected completion.
4266 ins_compl_use_match(c
)
4285 * Do Insert mode completion.
4286 * Called when character "c" was typed, which has a meaning for completion.
4287 * Returns OK if completion was done, FAIL if something failed (out of mem).
4294 int startcol
= 0; /* column where searched text starts */
4295 colnr_T curs_col
; /* cursor column */
4298 compl_direction
= ins_compl_key2dir(c
);
4301 /* First time we hit ^N or ^P (in a row, I mean) */
4304 #ifdef FEAT_SMARTINDENT
4307 can_si_back
= FALSE
;
4309 if (stop_arrow() == FAIL
)
4312 line
= ml_get(curwin
->w_cursor
.lnum
);
4313 curs_col
= curwin
->w_cursor
.col
;
4316 /* if this same ctrl_x_mode has been interrupted use the text from
4317 * "compl_startpos" to the cursor as a pattern to add a new word
4318 * instead of expand the one before the cursor, in word-wise if
4320 * is not in the same line as the cursor then fix it (the line has
4321 * been split because it was longer than 'tw'). if SOL is set then
4322 * skip the previous pattern, a word at the beginning of the line has
4323 * been inserted, we'll look for that -- Acevedo. */
4324 if ((compl_cont_status
& CONT_INTRPT
) == CONT_INTRPT
4325 && compl_cont_mode
== ctrl_x_mode
)
4328 * it is a continued search
4330 compl_cont_status
&= ~CONT_INTRPT
; /* remove INTRPT */
4331 if (ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_PATH_PATTERNS
4332 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4334 if (compl_startpos
.lnum
!= curwin
->w_cursor
.lnum
)
4336 /* line (probably) wrapped, set compl_startpos to the
4337 * first non_blank in the line, if it is not a wordchar
4338 * include it to get a better pattern, but then we don't
4339 * want the "\\<" prefix, check it bellow */
4340 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4341 compl_startpos
.col
= compl_col
;
4342 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4343 compl_cont_status
&= ~CONT_SOL
; /* clear SOL if present */
4347 /* S_IPOS was set when we inserted a word that was at the
4348 * beginning of the line, which means that we'll go to SOL
4349 * mode but first we need to redefine compl_startpos */
4350 if (compl_cont_status
& CONT_S_IPOS
)
4352 compl_cont_status
|= CONT_SOL
;
4353 compl_startpos
.col
= (colnr_T
)(skipwhite(
4355 + compl_startpos
.col
) - line
);
4357 compl_col
= compl_startpos
.col
;
4359 compl_length
= curwin
->w_cursor
.col
- (int)compl_col
;
4360 /* IObuff is used to add a "word from the next line" would we
4361 * have enough space? just being paranoic */
4362 #define MIN_SPACE 75
4363 if (compl_length
> (IOSIZE
- MIN_SPACE
))
4365 compl_cont_status
&= ~CONT_SOL
;
4366 compl_length
= (IOSIZE
- MIN_SPACE
);
4367 compl_col
= curwin
->w_cursor
.col
- compl_length
;
4369 compl_cont_status
|= CONT_ADDING
| CONT_N_ADDS
;
4370 if (compl_length
< 1)
4371 compl_cont_status
&= CONT_LOCAL
;
4373 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4374 compl_cont_status
= CONT_ADDING
| CONT_N_ADDS
;
4376 compl_cont_status
= 0;
4379 compl_cont_status
&= CONT_LOCAL
;
4381 if (!(compl_cont_status
& CONT_ADDING
)) /* normal expansion */
4383 compl_cont_mode
= ctrl_x_mode
;
4384 if (ctrl_x_mode
!= 0) /* Remove LOCAL if ctrl_x_mode != 0 */
4385 compl_cont_status
= 0;
4386 compl_cont_status
|= CONT_N_ADDS
;
4387 compl_startpos
= curwin
->w_cursor
;
4388 startcol
= (int)curs_col
;
4392 /* Work out completion pattern and original text -- webb */
4393 if (ctrl_x_mode
== 0 || (ctrl_x_mode
& CTRL_X_WANT_IDENT
))
4395 if ((compl_cont_status
& CONT_SOL
)
4396 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4398 if (!(compl_cont_status
& CONT_ADDING
))
4400 while (--startcol
>= 0 && vim_isIDc(line
[startcol
]))
4402 compl_col
+= ++startcol
;
4403 compl_length
= curs_col
- startcol
;
4406 compl_pattern
= str_foldcase(line
+ compl_col
,
4407 compl_length
, NULL
, 0);
4409 compl_pattern
= vim_strnsave(line
+ compl_col
,
4411 if (compl_pattern
== NULL
)
4414 else if (compl_cont_status
& CONT_ADDING
)
4416 char_u
*prefix
= (char_u
*)"\\<";
4418 /* we need 3 extra chars, 1 for the NUL and
4419 * 2 >= strlen(prefix) -- Acevedo */
4420 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4422 if (compl_pattern
== NULL
)
4424 if (!vim_iswordp(line
+ compl_col
)
4428 vim_iswordp(mb_prevptr(line
, line
+ compl_col
))
4430 vim_iswordc(line
[compl_col
- 1])
4433 prefix
= (char_u
*)"";
4434 STRCPY((char *)compl_pattern
, prefix
);
4435 (void)quote_meta(compl_pattern
+ STRLEN(prefix
),
4436 line
+ compl_col
, compl_length
);
4438 else if (--startcol
< 0 ||
4440 !vim_iswordp(mb_prevptr(line
, line
+ startcol
+ 1))
4442 !vim_iswordc(line
[startcol
])
4446 /* Match any word of at least two chars */
4447 compl_pattern
= vim_strsave((char_u
*)"\\<\\k\\k");
4448 if (compl_pattern
== NULL
)
4450 compl_col
+= curs_col
;
4456 /* Search the point of change class of multibyte character
4457 * or not a word single byte character backward. */
4463 startcol
-= (*mb_head_off
)(line
, line
+ startcol
);
4464 base_class
= mb_get_class(line
+ startcol
);
4465 while (--startcol
>= 0)
4467 head_off
= (*mb_head_off
)(line
, line
+ startcol
);
4468 if (base_class
!= mb_get_class(line
+ startcol
4471 startcol
-= head_off
;
4476 while (--startcol
>= 0 && vim_iswordc(line
[startcol
]))
4478 compl_col
+= ++startcol
;
4479 compl_length
= (int)curs_col
- startcol
;
4480 if (compl_length
== 1)
4482 /* Only match word with at least two chars -- webb
4483 * there's no need to call quote_meta,
4484 * alloc(7) is enough -- Acevedo
4486 compl_pattern
= alloc(7);
4487 if (compl_pattern
== NULL
)
4489 STRCPY((char *)compl_pattern
, "\\<");
4490 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
, 1);
4491 STRCAT((char *)compl_pattern
, "\\k");
4495 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4497 if (compl_pattern
== NULL
)
4499 STRCPY((char *)compl_pattern
, "\\<");
4500 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
,
4505 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4507 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4508 compl_length
= (int)curs_col
- (int)compl_col
;
4509 if (compl_length
< 0) /* cursor in indent: empty pattern */
4512 compl_pattern
= str_foldcase(line
+ compl_col
, compl_length
,
4515 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4516 if (compl_pattern
== NULL
)
4519 else if (ctrl_x_mode
== CTRL_X_FILES
)
4521 while (--startcol
>= 0 && vim_isfilec(line
[startcol
]))
4523 compl_col
+= ++startcol
;
4524 compl_length
= (int)curs_col
- startcol
;
4525 compl_pattern
= addstar(line
+ compl_col
, compl_length
,
4527 if (compl_pattern
== NULL
)
4530 else if (ctrl_x_mode
== CTRL_X_CMDLINE
)
4532 compl_pattern
= vim_strnsave(line
, curs_col
);
4533 if (compl_pattern
== NULL
)
4535 set_cmd_context(&compl_xp
, compl_pattern
,
4536 (int)STRLEN(compl_pattern
), curs_col
);
4537 if (compl_xp
.xp_context
== EXPAND_UNSUCCESSFUL
4538 || compl_xp
.xp_context
== EXPAND_NOTHING
)
4540 startcol
= (int)(compl_xp
.xp_pattern
- compl_pattern
);
4541 compl_col
= startcol
;
4542 compl_length
= curs_col
- startcol
;
4544 else if (ctrl_x_mode
== CTRL_X_FUNCTION
|| ctrl_x_mode
== CTRL_X_OMNI
)
4546 #ifdef FEAT_COMPL_FUNC
4548 * Call user defined function 'completefunc' with "a:findstart"
4549 * set to 1 to obtain the length of text to use for completion.
4556 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
4558 funcname
= ctrl_x_mode
== CTRL_X_FUNCTION
4559 ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
4560 if (*funcname
== NUL
)
4562 EMSG2(_(e_notset
), ctrl_x_mode
== CTRL_X_FUNCTION
4563 ? "completefunc" : "omnifunc");
4567 args
[0] = (char_u
*)"1";
4569 pos
= curwin
->w_cursor
;
4570 col
= call_func_retnr(funcname
, 2, args
, FALSE
);
4571 curwin
->w_cursor
= pos
; /* restore the cursor position */
4576 if ((colnr_T
)compl_col
> curs_col
)
4577 compl_col
= curs_col
;
4579 /* Setup variables for completion. Need to obtain "line" again,
4580 * it may have become invalid. */
4581 line
= ml_get(curwin
->w_cursor
.lnum
);
4582 compl_length
= curs_col
- compl_col
;
4583 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4584 if (compl_pattern
== NULL
)
4588 else if (ctrl_x_mode
== CTRL_X_SPELL
)
4591 if (spell_bad_len
> 0)
4592 compl_col
= curs_col
- spell_bad_len
;
4594 compl_col
= spell_word_start(startcol
);
4595 if (compl_col
>= (colnr_T
)startcol
)
4597 spell_expand_check_cap(compl_col
);
4598 /* Need to obtain "line" again, it may have become invalid. */
4599 line
= ml_get(curwin
->w_cursor
.lnum
);
4600 compl_length
= (int)curs_col
- compl_col
;
4601 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4602 if (compl_pattern
== NULL
)
4608 EMSG2(_(e_intern2
), "ins_complete()");
4612 if (compl_cont_status
& CONT_ADDING
)
4614 edit_submode_pre
= (char_u
*)_(" Adding");
4615 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4617 /* Insert a new line, keep indentation but ignore 'comments' */
4618 #ifdef FEAT_COMMENTS
4619 char_u
*old
= curbuf
->b_p_com
;
4621 curbuf
->b_p_com
= (char_u
*)"";
4623 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4624 compl_startpos
.col
= compl_col
;
4626 #ifdef FEAT_COMMENTS
4627 curbuf
->b_p_com
= old
;
4630 compl_col
= curwin
->w_cursor
.col
;
4635 edit_submode_pre
= NULL
;
4636 compl_startpos
.col
= compl_col
;
4639 if (compl_cont_status
& CONT_LOCAL
)
4640 edit_submode
= (char_u
*)_(ctrl_x_msgs
[CTRL_X_LOCAL_MSG
]);
4642 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
4644 /* Always add completion for the original text. */
4645 vim_free(compl_orig_text
);
4646 compl_orig_text
= vim_strnsave(line
+ compl_col
, compl_length
);
4647 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
4648 -1, FALSE
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
4650 vim_free(compl_pattern
);
4651 compl_pattern
= NULL
;
4652 vim_free(compl_orig_text
);
4653 compl_orig_text
= NULL
;
4657 /* showmode might reset the internal line pointers, so it must
4658 * be called before line = ml_get(), or when this address is no
4659 * longer needed. -- Acevedo.
4661 edit_submode_extra
= (char_u
*)_("-- Searching...");
4662 edit_submode_highl
= HLF_COUNT
;
4664 edit_submode_extra
= NULL
;
4668 compl_shown_match
= compl_curr_match
;
4669 compl_shows_dir
= compl_direction
;
4672 * Find next match (and following matches).
4674 n
= ins_compl_next(TRUE
, ins_compl_key2count(c
), ins_compl_use_match(c
));
4676 /* may undisplay the popup menu */
4677 ins_compl_upd_pum();
4679 if (n
> 1) /* all matches have been found */
4681 compl_curr_match
= compl_shown_match
;
4682 compl_direction
= compl_shows_dir
;
4683 compl_interrupted
= FALSE
;
4685 /* eat the ESC to avoid leaving insert mode */
4686 if (got_int
&& !global_busy
)
4692 /* we found no match if the list has only the "compl_orig_text"-entry */
4693 if (compl_first_match
== compl_first_match
->cp_next
)
4695 edit_submode_extra
= (compl_cont_status
& CONT_ADDING
)
4697 ? (char_u
*)_(e_hitend
) : (char_u
*)_(e_patnotf
);
4698 edit_submode_highl
= HLF_E
;
4699 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4700 * because we couldn't expand anything at first place, but if we used
4701 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4702 * (such as M in M'exico) if not tried already. -- Acevedo */
4703 if ( compl_length
> 1
4704 || (compl_cont_status
& CONT_ADDING
)
4705 || (ctrl_x_mode
!= 0
4706 && ctrl_x_mode
!= CTRL_X_PATH_PATTERNS
4707 && ctrl_x_mode
!= CTRL_X_PATH_DEFINES
))
4708 compl_cont_status
&= ~CONT_N_ADDS
;
4711 if (compl_curr_match
->cp_flags
& CONT_S_IPOS
)
4712 compl_cont_status
|= CONT_S_IPOS
;
4714 compl_cont_status
&= ~CONT_S_IPOS
;
4716 if (edit_submode_extra
== NULL
)
4718 if (compl_curr_match
->cp_flags
& ORIGINAL_TEXT
)
4720 edit_submode_extra
= (char_u
*)_("Back at original");
4721 edit_submode_highl
= HLF_W
;
4723 else if (compl_cont_status
& CONT_S_IPOS
)
4725 edit_submode_extra
= (char_u
*)_("Word from other line");
4726 edit_submode_highl
= HLF_COUNT
;
4728 else if (compl_curr_match
->cp_next
== compl_curr_match
->cp_prev
)
4730 edit_submode_extra
= (char_u
*)_("The only match");
4731 edit_submode_highl
= HLF_COUNT
;
4735 /* Update completion sequence number when needed. */
4736 if (compl_curr_match
->cp_number
== -1)
4741 if (compl_direction
== FORWARD
)
4743 /* search backwards for the first valid (!= -1) number.
4744 * This should normally succeed already at the first loop
4745 * cycle, so it's fast! */
4746 for (match
= compl_curr_match
->cp_prev
; match
!= NULL
4747 && match
!= compl_first_match
;
4748 match
= match
->cp_prev
)
4749 if (match
->cp_number
!= -1)
4751 number
= match
->cp_number
;
4755 /* go up and assign all numbers which are not assigned
4757 for (match
= match
->cp_next
;
4758 match
!= NULL
&& match
->cp_number
== -1;
4759 match
= match
->cp_next
)
4760 match
->cp_number
= ++number
;
4764 /* search forwards (upwards) for the first valid (!= -1)
4765 * number. This should normally succeed already at the
4766 * first loop cycle, so it's fast! */
4767 for (match
= compl_curr_match
->cp_next
; match
!= NULL
4768 && match
!= compl_first_match
;
4769 match
= match
->cp_next
)
4770 if (match
->cp_number
!= -1)
4772 number
= match
->cp_number
;
4776 /* go down and assign all numbers which are not
4778 for (match
= match
->cp_prev
; match
4779 && match
->cp_number
== -1;
4780 match
= match
->cp_prev
)
4781 match
->cp_number
= ++number
;
4785 /* The match should always have a sequence number now, this is
4786 * just a safety check. */
4787 if (compl_curr_match
->cp_number
!= -1)
4789 /* Space for 10 text chars. + 2x10-digit no.s */
4790 static char_u match_ref
[31];
4792 if (compl_matches
> 0)
4793 sprintf((char *)IObuff
, _("match %d of %d"),
4794 compl_curr_match
->cp_number
, compl_matches
);
4796 sprintf((char *)IObuff
, _("match %d"),
4797 compl_curr_match
->cp_number
);
4798 vim_strncpy(match_ref
, IObuff
, 30);
4799 edit_submode_extra
= match_ref
;
4800 edit_submode_highl
= HLF_R
;
4802 curs_columns(FALSE
);
4807 /* Show a message about what (completion) mode we're in. */
4809 if (edit_submode_extra
!= NULL
)
4812 msg_attr(edit_submode_extra
,
4813 edit_submode_highl
< HLF_COUNT
4814 ? hl_attr(edit_submode_highl
) : 0);
4817 msg_clr_cmdline(); /* necessary for "noshowmode" */
4819 /* RedrawingDisabled may be set when invoked through complete(). */
4820 n
= RedrawingDisabled
;
4821 RedrawingDisabled
= 0;
4822 ins_compl_show_pum();
4824 RedrawingDisabled
= n
;
4830 * Looks in the first "len" chars. of "src" for search-metachars.
4831 * If dest is not NULL the chars. are copied there quoting (with
4832 * a backslash) the metachars, and dest would be NUL terminated.
4833 * Returns the length (needed) of dest
4836 quote_meta(dest
, src
, len
)
4843 for (m
= len
; --len
>= 0; src
++)
4850 if (ctrl_x_mode
== CTRL_X_DICTIONARY
4851 || ctrl_x_mode
== CTRL_X_THESAURUS
)
4854 if (!p_magic
) /* quote these only if magic is set */
4857 if (ctrl_x_mode
== CTRL_X_DICTIONARY
4858 || ctrl_x_mode
== CTRL_X_THESAURUS
)
4860 case '^': /* currently it's not needed. */
4870 /* Copy remaining bytes of a multibyte character. */
4875 mb_len
= (*mb_ptr2len
)(src
) - 1;
4876 if (mb_len
> 0 && len
>= mb_len
)
4877 for (i
= 0; i
< mb_len
; ++i
)
4892 #endif /* FEAT_INS_EXPAND */
4895 * Next character is interpreted literally.
4896 * A one, two or three digit decimal number is interpreted as its byte value.
4897 * If one or two digits are entered, the next character is given to vungetc().
4898 * For Unicode a character > 255 may be returned.
4917 * In GUI there is no point inserting the internal code for a special key.
4918 * It is more useful to insert the string "<KEY>" instead. This would
4919 * probably be useful in a text window too, but it would not be
4920 * vi-compatible (maybe there should be an option for it?) -- webb
4925 #ifdef USE_ON_FLY_SCROLL
4926 dont_scroll
= TRUE
; /* disallow scrolling here */
4928 ++no_mapping
; /* don't map the next key hits */
4935 while (nc
== K_IGNORE
|| nc
== K_VER_SCROLLBAR
4936 || nc
== K_HOR_SCROLLBAR
);
4937 #ifdef FEAT_CMDL_INFO
4938 if (!(State
& CMDLINE
)
4940 && MB_BYTE2LEN_CHECK(nc
) == 1
4945 if (nc
== 'x' || nc
== 'X')
4947 else if (nc
== 'o' || nc
== 'O')
4950 else if (nc
== 'u' || nc
== 'U')
4961 if (!vim_isxdigit(nc
))
4963 cc
= cc
* 16 + hex2nr(nc
);
4967 if (nc
< '0' || nc
> '7')
4969 cc
= cc
* 8 + nc
- '0';
4973 if (!VIM_ISDIGIT(nc
))
4975 cc
= cc
* 10 + nc
- '0';
4986 cc
= 255; /* limit range to 0-255 */
4989 if (hex
) /* hex: up to two chars */
4995 else if (unicode
) /* Unicode: up to four or eight chars */
4997 if ((unicode
== 'u' && i
>= 4) || (unicode
== 'U' && i
>= 8))
5001 else if (i
>= 3) /* decimal or octal: up to three chars */
5004 if (i
== 0) /* no number entered */
5006 if (nc
== K_ZERO
) /* NUL is stored as NL */
5018 if (cc
== 0) /* NUL is stored as NL */
5021 if (enc_dbcs
&& (cc
& 0xff) == 0)
5022 cc
= '?'; /* don't accept an illegal DBCS char, the NUL in the
5023 second byte will cause trouble! */
5033 got_int
= FALSE
; /* CTRL-C typed after CTRL-V is not an interrupt */
5038 * Insert character, taking care of special keys and mod_mask
5041 insert_special(c
, allow_modmask
, ctrlv
)
5044 int ctrlv
; /* c was typed after CTRL-V */
5050 * Special function key, translate into "<Key>". Up to the last '>' is
5051 * inserted with ins_str(), so as not to replace characters in replace
5053 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5054 * unless 'allow_modmask' is TRUE.
5057 /* Command-key never produces a normal key */
5058 if (mod_mask
& MOD_MASK_CMD
)
5059 allow_modmask
= TRUE
;
5061 if (IS_SPECIAL(c
) || (mod_mask
&& allow_modmask
))
5063 p
= get_special_key_name(c
, mod_mask
);
5064 len
= (int)STRLEN(p
);
5068 if (stop_arrow() == FAIL
)
5072 AppendToRedobuffLit(p
, -1);
5076 if (stop_arrow() == OK
)
5077 insertchar(c
, ctrlv
? INSCHAR_CTRLV
: 0, -1);
5081 * Special characters in this context are those that need processing other
5082 * than the simple insertion that can be performed here. This includes ESC
5083 * which terminates the insert, and CR/NL which need special processing to
5084 * open up a new line. This routine tries to optimize insertions performed by
5085 * the "redo", "undo" or "put" commands, so it needs to know when it should
5086 * stop and defer processing to the "normal" mechanism.
5087 * '0' and '^' are special, because they can be followed by CTRL-D.
5090 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5092 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5096 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5098 # define WHITECHAR(cc) vim_iswhite(cc)
5102 insertchar(c
, flags
, second_indent
)
5103 int c
; /* character to insert or NUL */
5104 int flags
; /* INSCHAR_FORMAT, etc. */
5105 int second_indent
; /* indent for second line if >= 0 */
5108 #ifdef FEAT_COMMENTS
5113 textwidth
= comp_textwidth(flags
& INSCHAR_FORMAT
);
5114 fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5117 * Try to break the line in two or more pieces when:
5118 * - Always do this if we have been called to do formatting only.
5119 * - Always do this when 'formatoptions' has the 'a' flag and the line
5120 * ends in white space.
5122 * - Don't do this if inserting a blank
5123 * - Don't do this if an existing character is being replaced, unless
5124 * we're in VREPLACE mode.
5125 * - Do this if the cursor is not on the line where insert started
5126 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5127 * before the insert.
5128 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5129 * before 'textwidth'
5132 && ((flags
& INSCHAR_FORMAT
)
5134 && !((State
& REPLACE_FLAG
)
5135 #ifdef FEAT_VREPLACE
5136 && !(State
& VREPLACE_FLAG
)
5138 && *ml_get_cursor() != NUL
)
5139 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
5140 || ((!has_format_option(FO_INS_LONG
)
5141 || Insstart_textlen
<= (colnr_T
)textwidth
)
5143 || Insstart_blank_vcol
<= (colnr_T
)textwidth
5146 /* Format with 'formatexpr' when it's set. Use internal formatting
5147 * when 'formatexpr' isn't set or it returns non-zero. */
5148 #if defined(FEAT_EVAL)
5149 if (*curbuf
->b_p_fex
== NUL
5150 || fex_format(curwin
->w_cursor
.lnum
, 1L) != 0)
5152 internal_format(textwidth
, second_indent
, flags
, c
== NUL
);
5155 if (c
== NUL
) /* only formatting was wanted */
5158 #ifdef FEAT_COMMENTS
5159 /* Check whether this character should end a comment. */
5160 if (did_ai
&& (int)c
== end_comment_pending
)
5163 char_u lead_end
[COM_MAX_LEN
]; /* end-comment string */
5164 int middle_len
, end_len
;
5168 * Need to remove existing (middle) comment leader and insert end
5169 * comment leader. First, check what comment leader we can find.
5171 i
= get_leader_len(line
= ml_get_curline(), &p
, FALSE
);
5172 if (i
> 0 && vim_strchr(p
, COM_MIDDLE
) != NULL
) /* Just checking */
5174 /* Skip middle-comment string */
5175 while (*p
&& p
[-1] != ':') /* find end of middle flags */
5177 middle_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5178 /* Don't count trailing white space for middle_len */
5179 while (middle_len
> 0 && vim_iswhite(lead_end
[middle_len
- 1]))
5182 /* Find the end-comment string */
5183 while (*p
&& p
[-1] != ':') /* find end of end flags */
5185 end_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5187 /* Skip white space before the cursor */
5188 i
= curwin
->w_cursor
.col
;
5189 while (--i
>= 0 && vim_iswhite(line
[i
]))
5193 /* Skip to before the middle leader */
5196 /* Check some expected things before we go on */
5197 if (i
>= 0 && lead_end
[end_len
- 1] == end_comment_pending
)
5199 /* Backspace over all the stuff we want to replace */
5200 backspace_until_column(i
);
5203 * Insert the end-comment string, except for the last
5204 * character, which will get inserted as normal later.
5206 ins_bytes_len(lead_end
, end_len
- 1);
5210 end_comment_pending
= NUL
;
5214 #ifdef FEAT_SMARTINDENT
5217 can_si_back
= FALSE
;
5221 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5222 * This speeds up normal text input considerably.
5223 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5224 * need to re-indent at a ':', or any other character (but not what
5227 #ifdef USE_ON_FLY_SCROLL
5228 dont_scroll
= FALSE
; /* allow scrolling here */
5233 && (!has_mbyte
|| (*mb_char2len
)(c
) == 1)
5236 && !(State
& REPLACE_FLAG
)
5240 #ifdef FEAT_RIGHTLEFT
5245 #define INPUT_BUFLEN 100
5246 char_u buf
[INPUT_BUFLEN
+ 1];
5248 colnr_T virtcol
= 0;
5253 virtcol
= get_nolist_virtcol();
5255 * Stop the string when:
5256 * - no more chars available
5257 * - finding a special character (command key)
5259 * - running into the 'textwidth' boundary
5260 * - need to check for abbreviation: A non-word char after a word-char
5262 while ( (c
= vpeekc()) != NUL
5265 && (!has_mbyte
|| MB_BYTE2LEN_CHECK(c
) == 1)
5269 || (virtcol
+= byte2cells(buf
[i
- 1])) < (colnr_T
)textwidth
)
5270 && !(!no_abbr
&& !vim_iswordc(c
) && vim_iswordc(buf
[i
- 1])))
5272 #ifdef FEAT_RIGHTLEFT
5274 if (p_hkmap
&& KeyTyped
)
5275 c
= hkmap(c
); /* Hebrew mode mapping */
5277 if (p_fkmap
&& KeyTyped
)
5278 c
= fkmap(c
); /* Farsi mode mapping */
5286 #ifdef FEAT_DIGRAPHS
5287 do_digraph(-1); /* clear digraphs */
5288 do_digraph(buf
[i
-1]); /* may be the start of a digraph */
5292 if (flags
& INSCHAR_CTRLV
)
5300 AppendToRedobuffLit(buf
+ i
, -1);
5307 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
5309 char_u buf
[MB_MAXBYTES
+ 1];
5311 (*mb_char2bytes
)(c
, buf
);
5313 ins_char_bytes(buf
, cc
);
5314 AppendCharToRedobuff(c
);
5320 if (flags
& INSCHAR_CTRLV
)
5323 AppendCharToRedobuff(c
);
5329 * Format text at the current insert position.
5332 internal_format(textwidth
, second_indent
, flags
, format_only
)
5339 int save_char
= NUL
;
5340 int haveto_redraw
= FALSE
;
5341 int fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5343 int fo_multibyte
= has_format_option(FO_MBYTE_BREAK
);
5345 int fo_white_par
= has_format_option(FO_WHITE_PAR
);
5346 int first_line
= TRUE
;
5347 #ifdef FEAT_COMMENTS
5349 int no_leader
= FALSE
;
5350 int do_comments
= (flags
& INSCHAR_DO_COM
);
5354 * When 'ai' is off we don't want a space under the cursor to be
5355 * deleted. Replace it with an 'x' temporarily.
5357 if (!curbuf
->b_p_ai
)
5359 cc
= gchar_cursor();
5360 if (vim_iswhite(cc
))
5368 * Repeat breaking lines, until the current line is not too long.
5372 int startcol
; /* Cursor column at entry */
5373 int wantcol
; /* column at textwidth border */
5374 int foundcol
; /* column for start of spaces */
5375 int end_foundcol
= 0; /* column for start of word */
5378 #ifdef FEAT_VREPLACE
5380 char_u
*saved_text
= NULL
;
5384 virtcol
= get_nolist_virtcol();
5385 if (virtcol
< (colnr_T
)textwidth
)
5388 #ifdef FEAT_COMMENTS
5390 do_comments
= FALSE
;
5391 else if (!(flags
& INSCHAR_FORMAT
)
5392 && has_format_option(FO_WRAP_COMS
))
5395 /* Don't break until after the comment leader */
5397 leader_len
= get_leader_len(ml_get_curline(), NULL
, FALSE
);
5401 /* If the line doesn't start with a comment leader, then don't
5402 * start one in a following broken line. Avoids that a %word
5403 * moved to the start of the next line causes all following lines
5404 * to start with %. */
5405 if (leader_len
== 0)
5408 if (!(flags
& INSCHAR_FORMAT
)
5409 #ifdef FEAT_COMMENTS
5412 && !has_format_option(FO_WRAP
))
5418 if ((startcol
= curwin
->w_cursor
.col
) == 0)
5421 /* find column of textwidth border */
5422 coladvance((colnr_T
)textwidth
);
5423 wantcol
= curwin
->w_cursor
.col
;
5425 curwin
->w_cursor
.col
= startcol
- 1;
5427 /* Correct cursor for multi-byte character. */
5434 * Find position to break at.
5435 * Stop at first entered white when 'formatoptions' has 'v'
5437 while ((!fo_ins_blank
&& !has_format_option(FO_INS_VI
))
5438 || curwin
->w_cursor
.lnum
!= Insstart
.lnum
5439 || curwin
->w_cursor
.col
>= Insstart
.col
)
5441 cc
= gchar_cursor();
5444 /* remember position of blank just before text */
5445 end_foundcol
= curwin
->w_cursor
.col
;
5447 /* find start of sequence of blanks */
5448 while (curwin
->w_cursor
.col
> 0 && WHITECHAR(cc
))
5451 cc
= gchar_cursor();
5453 if (curwin
->w_cursor
.col
== 0 && WHITECHAR(cc
))
5454 break; /* only spaces in front of text */
5455 #ifdef FEAT_COMMENTS
5456 /* Don't break until after the comment leader */
5457 if (curwin
->w_cursor
.col
< leader_len
)
5460 if (has_format_option(FO_ONE_LETTER
))
5462 /* do not break after one-letter words */
5463 if (curwin
->w_cursor
.col
== 0)
5464 break; /* one-letter word at begin */
5466 col
= curwin
->w_cursor
.col
;
5468 cc
= gchar_cursor();
5471 continue; /* one-letter, continue */
5472 curwin
->w_cursor
.col
= col
;
5476 foundcol
= curwin
->w_cursor
.col
5477 + (*mb_ptr2len
)(ml_get_cursor());
5480 foundcol
= curwin
->w_cursor
.col
+ 1;
5481 if (curwin
->w_cursor
.col
< (colnr_T
)wantcol
)
5485 else if (cc
>= 0x100 && fo_multibyte
5486 && curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5488 /* Break after or before a multi-byte character. */
5489 foundcol
= curwin
->w_cursor
.col
;
5490 if (curwin
->w_cursor
.col
< (colnr_T
)wantcol
)
5491 foundcol
+= (*mb_char2len
)(cc
);
5492 end_foundcol
= foundcol
;
5496 if (curwin
->w_cursor
.col
== 0)
5501 if (foundcol
== 0) /* no spaces, cannot break line */
5503 curwin
->w_cursor
.col
= startcol
;
5507 /* Going to break the line, remove any "$" now. */
5511 * Offset between cursor position and line break is used by replace
5512 * stack functions. VREPLACE does not use this, and backspaces
5513 * over the text instead.
5515 #ifdef FEAT_VREPLACE
5516 if (State
& VREPLACE_FLAG
)
5517 orig_col
= startcol
; /* Will start backspacing from here */
5520 replace_offset
= startcol
- end_foundcol
- 1;
5523 * adjust startcol for spaces that will be deleted and
5524 * characters that will remain on top line
5526 curwin
->w_cursor
.col
= foundcol
;
5527 while (cc
= gchar_cursor(), WHITECHAR(cc
))
5529 startcol
-= curwin
->w_cursor
.col
;
5533 #ifdef FEAT_VREPLACE
5534 if (State
& VREPLACE_FLAG
)
5537 * In VREPLACE mode, we will backspace over the text to be
5538 * wrapped, so save a copy now to put on the next line.
5540 saved_text
= vim_strsave(ml_get_cursor());
5541 curwin
->w_cursor
.col
= orig_col
;
5542 if (saved_text
== NULL
)
5543 break; /* Can't do it, out of memory */
5544 saved_text
[startcol
] = NUL
;
5546 /* Backspace over characters that will move to the next line */
5548 backspace_until_column(foundcol
);
5553 /* put cursor after pos. to break line */
5555 curwin
->w_cursor
.col
= foundcol
;
5559 * Split the line just before the margin.
5560 * Only insert/delete lines, but don't really redraw the window.
5562 open_line(FORWARD
, OPENLINE_DELSPACES
+ OPENLINE_MARKFIX
5563 + (fo_white_par
? OPENLINE_KEEPTRAIL
: 0)
5564 #ifdef FEAT_COMMENTS
5565 + (do_comments
? OPENLINE_DO_COM
: 0)
5573 if (second_indent
< 0 && has_format_option(FO_Q_NUMBER
))
5574 second_indent
= get_number_indent(curwin
->w_cursor
.lnum
-1);
5575 if (second_indent
>= 0)
5577 #ifdef FEAT_VREPLACE
5578 if (State
& VREPLACE_FLAG
)
5579 change_indent(INDENT_SET
, second_indent
, FALSE
, NUL
);
5582 (void)set_indent(second_indent
, SIN_CHANGED
);
5587 #ifdef FEAT_VREPLACE
5588 if (State
& VREPLACE_FLAG
)
5591 * In VREPLACE mode we have backspaced over the text to be
5592 * moved, now we re-insert it into the new line.
5594 ins_bytes(saved_text
);
5595 vim_free(saved_text
);
5601 * Check if cursor is not past the NUL off the line, cindent
5602 * may have added or removed indent.
5604 curwin
->w_cursor
.col
+= startcol
;
5605 len
= (colnr_T
)STRLEN(ml_get_curline());
5606 if (curwin
->w_cursor
.col
> len
)
5607 curwin
->w_cursor
.col
= len
;
5610 haveto_redraw
= TRUE
;
5614 /* moved the cursor, don't autoindent or cindent now */
5616 #ifdef FEAT_SMARTINDENT
5619 can_si_back
= FALSE
;
5624 if (save_char
!= NUL
) /* put back space after cursor */
5625 pchar_cursor(save_char
);
5627 if (!format_only
&& haveto_redraw
)
5630 redraw_curbuf_later(VALID
);
5635 * Called after inserting or deleting text: When 'formatoptions' includes the
5636 * 'a' flag format from the current line until the end of the paragraph.
5637 * Keep the cursor at the same position relative to the text.
5638 * The caller must have saved the cursor line for undo, following ones will be
5642 auto_format(trailblank
, prev_line
)
5643 int trailblank
; /* when TRUE also format with trailing blank */
5644 int prev_line
; /* may start in previous line */
5653 if (!has_format_option(FO_AUTO
))
5656 pos
= curwin
->w_cursor
;
5657 old
= ml_get_curline();
5659 /* may remove added space */
5660 check_auto_format(FALSE
);
5662 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5663 * user might insert normal text next. Also skip formatting when "1" is
5664 * in 'formatoptions' and there is a single character before the cursor.
5665 * Otherwise the line would be broken and when typing another non-white
5666 * next they are not joined back together. */
5667 wasatend
= (pos
.col
== STRLEN(old
));
5668 if (*old
!= NUL
&& !trailblank
&& wasatend
)
5671 cc
= gchar_cursor();
5672 if (!WHITECHAR(cc
) && curwin
->w_cursor
.col
> 0
5673 && has_format_option(FO_ONE_LETTER
))
5675 cc
= gchar_cursor();
5678 curwin
->w_cursor
= pos
;
5681 curwin
->w_cursor
= pos
;
5684 #ifdef FEAT_COMMENTS
5685 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5687 if (has_format_option(FO_WRAP_COMS
) && !has_format_option(FO_WRAP
)
5688 && get_leader_len(old
, NULL
, FALSE
) == 0)
5693 * May start formatting in a previous line, so that after "x" a word is
5694 * moved to the previous line if it fits there now. Only when this is not
5695 * the start of a paragraph.
5697 if (prev_line
&& !paragraph_start(curwin
->w_cursor
.lnum
))
5699 --curwin
->w_cursor
.lnum
;
5700 if (u_save_cursor() == FAIL
)
5705 * Do the formatting and restore the cursor position. "saved_cursor" will
5706 * be adjusted for the text formatting.
5709 format_lines((linenr_T
)-1);
5710 curwin
->w_cursor
= saved_cursor
;
5711 saved_cursor
.lnum
= 0;
5713 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
5715 /* "cannot happen" */
5716 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
5717 coladvance((colnr_T
)MAXCOL
);
5722 /* Insert mode: If the cursor is now after the end of the line while it
5723 * previously wasn't, the line was broken. Because of the rule above we
5724 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5726 if (!wasatend
&& has_format_option(FO_WHITE_PAR
))
5728 new = ml_get_curline();
5729 len
= (colnr_T
)STRLEN(new);
5730 if (curwin
->w_cursor
.col
== len
)
5732 pnew
= vim_strnsave(new, len
+ 2);
5734 pnew
[len
+ 1] = NUL
;
5735 ml_replace(curwin
->w_cursor
.lnum
, pnew
, FALSE
);
5736 /* remove the space later */
5737 did_add_space
= TRUE
;
5740 /* may remove added space */
5741 check_auto_format(FALSE
);
5748 * When an extra space was added to continue a paragraph for auto-formatting,
5749 * delete it now. The space must be under the cursor, just after the insert
5753 check_auto_format(end_insert
)
5754 int end_insert
; /* TRUE when ending Insert mode */
5761 cc
= gchar_cursor();
5763 /* Somehow the space was removed already. */
5764 did_add_space
= FALSE
;
5775 /* The space is no longer at the end of the line, delete it. */
5777 did_add_space
= FALSE
;
5784 * Find out textwidth to be used for formatting:
5785 * if 'textwidth' option is set, use it
5786 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5787 * if invalid value, use 0.
5788 * Set default to window width (maximum 79) for "gq" operator.
5792 int ff
; /* force formatting (for "Q" command) */
5796 textwidth
= curbuf
->b_p_tw
;
5797 if (textwidth
== 0 && curbuf
->b_p_wm
)
5799 /* The width is the window width minus 'wrapmargin' minus all the
5800 * things that add to the margin. */
5801 textwidth
= W_WIDTH(curwin
) - curbuf
->b_p_wm
;
5803 if (cmdwin_type
!= 0)
5807 textwidth
-= curwin
->w_p_fdc
;
5810 if (curwin
->w_buffer
->b_signlist
!= NULL
5811 # ifdef FEAT_NETBEANS_INTG
5822 if (ff
&& textwidth
== 0)
5824 textwidth
= W_WIDTH(curwin
) - 1;
5832 * Put a character in the redo buffer, for when just after a CTRL-V.
5840 /* Only digits need special treatment. Translate them into a string of
5844 sprintf((char *)buf
, "%03d", c
);
5845 AppendToRedobuff(buf
);
5848 AppendCharToRedobuff(c
);
5852 * start_arrow() is called when an arrow key is used in insert mode.
5853 * For undo/redo it resembles hitting the <ESC> key.
5856 start_arrow(end_insert_pos
)
5857 pos_T
*end_insert_pos
; /* can be NULL */
5859 if (!arrow_used
) /* something has been inserted */
5861 AppendToRedobuff(ESC_STR
);
5862 stop_insert(end_insert_pos
, FALSE
);
5863 arrow_used
= TRUE
; /* this means we stopped the current insert */
5866 check_spell_redraw();
5872 * If we skipped highlighting word at cursor, do it now.
5873 * It may be skipped again, thus reset spell_redraw_lnum first.
5876 check_spell_redraw()
5878 if (spell_redraw_lnum
!= 0)
5880 linenr_T lnum
= spell_redraw_lnum
;
5882 spell_redraw_lnum
= 0;
5883 redrawWinline(lnum
, FALSE
);
5888 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5889 * spelled word, if there is one.
5892 spell_back_to_badword()
5894 pos_T tpos
= curwin
->w_cursor
;
5896 spell_bad_len
= spell_move_to(curwin
, BACKWARD
, TRUE
, TRUE
, NULL
);
5897 if (curwin
->w_cursor
.col
!= tpos
.col
)
5903 * stop_arrow() is called before a change is made in insert mode.
5904 * If an arrow key has been used, start a new insertion.
5905 * Returns FAIL if undo is impossible, shouldn't insert then.
5912 if (u_save_cursor() == OK
)
5915 ins_need_undo
= FALSE
;
5917 Insstart
= curwin
->w_cursor
; /* new insertion starts here */
5918 Insstart_textlen
= linetabsize(ml_get_curline());
5920 #ifdef FEAT_VREPLACE
5921 if (State
& VREPLACE_FLAG
)
5923 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
5924 vr_lines_changed
= 1;
5928 AppendToRedobuff((char_u
*)"1i"); /* pretend we start an insertion */
5929 new_insert_skip
= 2;
5931 else if (ins_need_undo
)
5933 if (u_save_cursor() == OK
)
5934 ins_need_undo
= FALSE
;
5938 /* Always open fold at the cursor line when inserting something. */
5942 return (arrow_used
|| ins_need_undo
? FAIL
: OK
);
5946 * Do a few things to stop inserting.
5947 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
5948 * to another window/buffer.
5951 stop_insert(end_insert_pos
, esc
)
5952 pos_T
*end_insert_pos
;
5953 int esc
; /* called by ins_esc() */
5959 replace_flush(); /* abandon replace stack */
5962 * Save the inserted text for later redo with ^@ and CTRL-A.
5963 * Don't do it when "restart_edit" was set and nothing was inserted,
5964 * otherwise CTRL-O w and then <Left> will clear "last_insert".
5966 ptr
= get_inserted();
5967 if (did_restart_edit
== 0 || (ptr
!= NULL
5968 && (int)STRLEN(ptr
) > new_insert_skip
))
5970 vim_free(last_insert
);
5972 last_insert_skip
= new_insert_skip
;
5977 if (!arrow_used
&& end_insert_pos
!= NULL
)
5979 /* Auto-format now. It may seem strange to do this when stopping an
5980 * insertion (or moving the cursor), but it's required when appending
5981 * a line and having it end in a space. But only do it when something
5982 * was actually inserted, otherwise undo won't work. */
5983 if (!ins_need_undo
&& has_format_option(FO_AUTO
))
5985 pos_T tpos
= curwin
->w_cursor
;
5987 /* When the cursor is at the end of the line after a space the
5988 * formatting will move it to the following word. Avoid that by
5989 * moving the cursor onto the space. */
5991 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
)
5994 cc
= gchar_cursor();
5995 if (!vim_iswhite(cc
))
5996 curwin
->w_cursor
= tpos
;
5999 auto_format(TRUE
, FALSE
);
6001 if (vim_iswhite(cc
))
6003 if (gchar_cursor() != NUL
)
6005 #ifdef FEAT_VIRTUALEDIT
6006 /* If the cursor is still at the same character, also keep
6008 if (gchar_cursor() == NUL
6009 && curwin
->w_cursor
.lnum
== tpos
.lnum
6010 && curwin
->w_cursor
.col
== tpos
.col
)
6011 curwin
->w_cursor
.coladd
= tpos
.coladd
;
6016 /* If a space was inserted for auto-formatting, remove it now. */
6017 check_auto_format(TRUE
);
6019 /* If we just did an auto-indent, remove the white space from the end
6020 * of the line, and put the cursor back.
6021 * Do this when ESC was used or moving the cursor up/down. */
6022 if (did_ai
&& (esc
|| (vim_strchr(p_cpo
, CPO_INDENT
) == NULL
6023 && curwin
->w_cursor
.lnum
!= end_insert_pos
->lnum
)))
6025 pos_T tpos
= curwin
->w_cursor
;
6027 curwin
->w_cursor
= *end_insert_pos
;
6030 if (gchar_cursor() == NUL
&& curwin
->w_cursor
.col
> 0)
6031 --curwin
->w_cursor
.col
;
6032 cc
= gchar_cursor();
6033 if (!vim_iswhite(cc
))
6035 (void)del_char(TRUE
);
6037 if (curwin
->w_cursor
.lnum
!= tpos
.lnum
)
6038 curwin
->w_cursor
= tpos
;
6040 ++curwin
->w_cursor
.col
; /* put cursor back on the NUL */
6043 /* <C-S-Right> may have started Visual mode, adjust the position for
6044 * deleted characters. */
6045 if (VIsual_active
&& VIsual
.lnum
== curwin
->w_cursor
.lnum
)
6047 cc
= (int)STRLEN(ml_get_curline());
6048 if (VIsual
.col
> (colnr_T
)cc
)
6051 # ifdef FEAT_VIRTUALEDIT
6060 #ifdef FEAT_SMARTINDENT
6063 can_si_back
= FALSE
;
6066 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6067 * now in a different buffer. */
6068 if (end_insert_pos
!= NULL
)
6070 curbuf
->b_op_start
= Insstart
;
6071 curbuf
->b_op_end
= *end_insert_pos
;
6076 * Set the last inserted text to a single character.
6077 * Used for the replace command.
6085 vim_free(last_insert
);
6087 last_insert
= alloc(MB_MAXBYTES
* 3 + 5);
6089 last_insert
= alloc(6);
6091 if (last_insert
!= NULL
)
6094 /* Use the CTRL-V only when entering a special char */
6095 if (c
< ' ' || c
== DEL
)
6097 s
= add_char2buf(c
, s
);
6100 last_insert_skip
= 0;
6104 #if defined(EXITFREE) || defined(PROTO)
6108 vim_free(last_insert
);
6110 vim_free(compl_orig_text
);
6111 compl_orig_text
= NULL
;
6116 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6117 * and CSI. Handle multi-byte characters.
6118 * Returns a pointer to after the added bytes.
6126 char_u temp
[MB_MAXBYTES
];
6130 len
= (*mb_char2bytes
)(c
, temp
);
6131 for (i
= 0; i
< len
; ++i
)
6135 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6159 * move cursor to start of line
6160 * if flags & BL_WHITE move to first non-white
6161 * if flags & BL_SOL move to first non-white if startofline is set,
6162 * otherwise keep "curswant" column
6163 * if flags & BL_FIX don't leave the cursor on a NUL.
6169 if ((flags
& BL_SOL
) && !p_sol
)
6170 coladvance(curwin
->w_curswant
);
6173 curwin
->w_cursor
.col
= 0;
6174 #ifdef FEAT_VIRTUALEDIT
6175 curwin
->w_cursor
.coladd
= 0;
6178 if (flags
& (BL_WHITE
| BL_SOL
))
6182 for (ptr
= ml_get_curline(); vim_iswhite(*ptr
)
6183 && !((flags
& BL_FIX
) && ptr
[1] == NUL
); ++ptr
)
6184 ++curwin
->w_cursor
.col
;
6186 curwin
->w_set_curswant
= TRUE
;
6191 * oneright oneleft cursor_down cursor_up
6193 * Move one char {right,left,down,up}.
6194 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
6195 * Return OK when successful, FAIL when we hit a line of file boundary.
6204 #ifdef FEAT_VIRTUALEDIT
6205 if (virtual_active())
6207 pos_T prevpos
= curwin
->w_cursor
;
6209 /* Adjust for multi-wide char (excluding TAB) */
6210 ptr
= ml_get_cursor();
6211 coladvance(getviscol() + ((*ptr
!= TAB
&& vim_isprintc(
6218 ? ptr2cells(ptr
) : 1));
6219 curwin
->w_set_curswant
= TRUE
;
6220 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6221 return (prevpos
.col
!= curwin
->w_cursor
.col
6222 || prevpos
.coladd
!= curwin
->w_cursor
.coladd
) ? OK
: FAIL
;
6226 ptr
= ml_get_cursor();
6228 return FAIL
; /* already at the very end */
6232 l
= (*mb_ptr2len
)(ptr
);
6237 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6238 * contains "onemore". */
6240 #ifdef FEAT_VIRTUALEDIT
6241 && (ve_flags
& VE_ONEMORE
) == 0
6245 curwin
->w_cursor
.col
+= l
;
6247 curwin
->w_set_curswant
= TRUE
;
6254 #ifdef FEAT_VIRTUALEDIT
6255 if (virtual_active())
6258 int v
= getviscol();
6263 # ifdef FEAT_LINEBREAK
6264 /* We might get stuck on 'showbreak', skip over it. */
6268 coladvance(v
- width
);
6269 /* getviscol() is slow, skip it when 'showbreak' is empty and
6270 * there are no multi-byte characters */
6275 ) || getviscol() < v
)
6283 if (curwin
->w_cursor
.coladd
== 1)
6287 /* Adjust for multi-wide char (not a TAB) */
6288 ptr
= ml_get_cursor();
6289 if (*ptr
!= TAB
&& vim_isprintc(
6295 ) && ptr2cells(ptr
) > 1)
6296 curwin
->w_cursor
.coladd
= 0;
6299 curwin
->w_set_curswant
= TRUE
;
6304 if (curwin
->w_cursor
.col
== 0)
6307 curwin
->w_set_curswant
= TRUE
;
6308 --curwin
->w_cursor
.col
;
6311 /* if the character on the left of the current cursor is a multi-byte
6312 * character, move to its first byte */
6320 cursor_up(n
, upd_topline
)
6322 int upd_topline
; /* When TRUE: update topline */
6328 lnum
= curwin
->w_cursor
.lnum
;
6329 /* This fails if the cursor is already in the first line or the count
6330 * is larger than the line number and '-' is in 'cpoptions' */
6331 if (lnum
<= 1 || (n
>= lnum
&& vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6337 if (hasAnyFolding(curwin
))
6340 * Count each sequence of folded lines as one logical line.
6342 /* go to the the start of the current fold */
6343 (void)hasFolding(lnum
, &lnum
, NULL
);
6347 /* move up one line */
6351 /* If we entered a fold, move to the beginning, unless in
6352 * Insert mode or when 'foldopen' contains "all": it will open
6354 if (n
> 0 || !((State
& INSERT
) || (fdo_flags
& FDO_ALL
)))
6355 (void)hasFolding(lnum
, &lnum
, NULL
);
6363 curwin
->w_cursor
.lnum
= lnum
;
6366 /* try to advance to the column we want to be at */
6367 coladvance(curwin
->w_curswant
);
6370 update_topline(); /* make sure curwin->w_topline is valid */
6376 * Cursor down a number of logical lines.
6379 cursor_down(n
, upd_topline
)
6381 int upd_topline
; /* When TRUE: update topline */
6387 lnum
= curwin
->w_cursor
.lnum
;
6389 /* Move to last line of fold, will fail if it's the end-of-file. */
6390 (void)hasFolding(lnum
, NULL
, &lnum
);
6392 /* This fails if the cursor is already in the last line or would move
6393 * beyound the last line and '-' is in 'cpoptions' */
6394 if (lnum
>= curbuf
->b_ml
.ml_line_count
6395 || (lnum
+ n
> curbuf
->b_ml
.ml_line_count
6396 && vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6398 if (lnum
+ n
>= curbuf
->b_ml
.ml_line_count
)
6399 lnum
= curbuf
->b_ml
.ml_line_count
;
6402 if (hasAnyFolding(curwin
))
6406 /* count each sequence of folded lines as one logical line */
6409 if (hasFolding(lnum
, NULL
, &last
))
6413 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
6416 if (lnum
> curbuf
->b_ml
.ml_line_count
)
6417 lnum
= curbuf
->b_ml
.ml_line_count
;
6422 curwin
->w_cursor
.lnum
= lnum
;
6425 /* try to advance to the column we want to be at */
6426 coladvance(curwin
->w_curswant
);
6429 update_topline(); /* make sure curwin->w_topline is valid */
6435 * Stuff the last inserted text in the read buffer.
6436 * Last_insert actually is a copy of the redo buffer, so we
6437 * first have to remove the command.
6440 stuff_inserted(c
, count
, no_esc
)
6441 int c
; /* Command character to be inserted */
6442 long count
; /* Repeat this many times */
6443 int no_esc
; /* Don't add an ESC at the end */
6450 ptr
= get_last_insert();
6453 EMSG(_(e_noinstext
));
6457 /* may want to stuff the command character, to start Insert mode */
6459 stuffcharReadbuff(c
);
6460 if ((esc_ptr
= (char_u
*)vim_strrchr(ptr
, ESC
)) != NULL
)
6461 *esc_ptr
= NUL
; /* remove the ESC */
6463 /* when the last char is either "0" or "^" it will be quoted if no ESC
6464 * comes after it OR if it will inserted more than once and "ptr"
6465 * starts with ^D. -- Acevedo
6467 last_ptr
= (esc_ptr
? esc_ptr
: ptr
+ STRLEN(ptr
)) - 1;
6468 if (last_ptr
>= ptr
&& (*last_ptr
== '0' || *last_ptr
== '^')
6469 && (no_esc
|| (*ptr
== Ctrl_D
&& count
> 1)))
6478 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6480 stuffReadbuff((char_u
*)(last
== '0'
6481 ? IF_EB("\026\060\064\070", CTRL_V_STR
"xf0")
6482 : IF_EB("\026^", CTRL_V_STR
"^")));
6484 while (--count
> 0);
6489 if (esc_ptr
!= NULL
)
6490 *esc_ptr
= ESC
; /* put the ESC back */
6492 /* may want to stuff a trailing ESC, to get out of Insert mode */
6494 stuffcharReadbuff(ESC
);
6502 if (last_insert
== NULL
)
6504 return last_insert
+ last_insert_skip
;
6508 * Get last inserted string, and remove trailing <Esc>.
6509 * Returns pointer to allocated memory (must be freed) or NULL.
6512 get_last_insert_save()
6517 if (last_insert
== NULL
)
6519 s
= vim_strsave(last_insert
+ last_insert_skip
);
6522 len
= (int)STRLEN(s
);
6523 if (len
> 0 && s
[len
- 1] == ESC
) /* remove trailing ESC */
6530 * Check the word in front of the cursor for an abbreviation.
6531 * Called when the non-id character "c" has been entered.
6532 * When an abbreviation is recognized it is removed from the text and
6533 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6539 /* Don't check for abbreviation in paste mode, when disabled and just
6540 * after moving around with cursor keys. */
6541 if (p_paste
|| no_abbr
|| arrow_used
)
6544 return check_abbr(c
, ml_get_curline(), curwin
->w_cursor
.col
,
6545 curwin
->w_cursor
.lnum
== Insstart
.lnum
? Insstart
.col
: 0);
6549 * replace-stack functions
6551 * When replacing characters, the replaced characters are remembered for each
6552 * new character. This is used to re-insert the old text when backspacing.
6554 * There is a NUL headed list of characters for each character that is
6555 * currently in the file after the insertion point. When BS is used, one NUL
6556 * headed list is put back for the deleted character.
6558 * For a newline, there are two NUL headed lists. One contains the characters
6559 * that the NL replaced. The extra one stores the characters after the cursor
6560 * that were deleted (always white space).
6562 * Replace_offset is normally 0, in which case replace_push will add a new
6563 * character at the end of the stack. If replace_offset is not 0, that many
6564 * characters will be left on the stack above the newly inserted character.
6567 static char_u
*replace_stack
= NULL
;
6568 static long replace_stack_nr
= 0; /* next entry in replace stack */
6569 static long replace_stack_len
= 0; /* max. number of entries */
6573 int c
; /* character that is replaced (NUL is none) */
6577 if (replace_stack_nr
< replace_offset
) /* nothing to do */
6579 if (replace_stack_len
<= replace_stack_nr
)
6581 replace_stack_len
+= 50;
6582 p
= lalloc(sizeof(char_u
) * replace_stack_len
, TRUE
);
6583 if (p
== NULL
) /* out of memory */
6585 replace_stack_len
-= 50;
6588 if (replace_stack
!= NULL
)
6590 mch_memmove(p
, replace_stack
,
6591 (size_t)(replace_stack_nr
* sizeof(char_u
)));
6592 vim_free(replace_stack
);
6596 p
= replace_stack
+ replace_stack_nr
- replace_offset
;
6598 mch_memmove(p
+ 1, p
, (size_t)(replace_offset
* sizeof(char_u
)));
6605 * call replace_push(c) with replace_offset set to the first NUL.
6613 p
= replace_stack
+ replace_stack_nr
;
6614 for (replace_offset
= 1; replace_offset
< replace_stack_nr
;
6624 * Pop one item from the replace stack.
6625 * return -1 if stack empty
6626 * return replaced character or NUL otherwise
6631 if (replace_stack_nr
== 0)
6633 return (int)replace_stack
[--replace_stack_nr
];
6637 * Join the top two items on the replace stack. This removes to "off"'th NUL
6642 int off
; /* offset for which NUL to remove */
6646 for (i
= replace_stack_nr
; --i
>= 0; )
6647 if (replace_stack
[i
] == NUL
&& off
-- <= 0)
6650 mch_memmove(replace_stack
+ i
, replace_stack
+ i
+ 1,
6651 (size_t)(replace_stack_nr
- i
));
6657 * Pop bytes from the replace stack until a NUL is found, and insert them
6658 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6664 int oldState
= State
;
6666 State
= NORMAL
; /* don't want REPLACE here */
6667 while ((cc
= replace_pop()) > 0)
6670 mb_replace_pop_ins(cc
);
6681 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6682 * indicates a multi-byte char, pop the other bytes too.
6685 mb_replace_pop_ins(cc
)
6689 char_u buf
[MB_MAXBYTES
];
6693 if (has_mbyte
&& (n
= MB_BYTE2LEN(cc
)) > 1)
6696 for (i
= 1; i
< n
; ++i
)
6697 buf
[i
] = replace_pop();
6698 ins_bytes_len(buf
, n
);
6704 /* Handle composing chars. */
6708 if (c
== -1) /* stack empty */
6710 if ((n
= MB_BYTE2LEN(c
)) == 1)
6712 /* Not a multi-byte char, put it back. */
6719 for (i
= 1; i
< n
; ++i
)
6720 buf
[i
] = replace_pop();
6721 if (utf_iscomposing(utf_ptr2char(buf
)))
6722 ins_bytes_len(buf
, n
);
6725 /* Not a composing char, put it back. */
6726 for (i
= n
- 1; i
>= 0; --i
)
6727 replace_push(buf
[i
]);
6736 * make the replace stack empty
6737 * (called when exiting replace mode)
6742 vim_free(replace_stack
);
6743 replace_stack
= NULL
;
6744 replace_stack_len
= 0;
6745 replace_stack_nr
= 0;
6749 * Handle doing a BS for one character.
6750 * cc < 0: replace stack empty, just move cursor
6751 * cc == 0: character was inserted, delete it
6752 * cc > 0: character was replaced, put cc (first byte of original char) back
6753 * and check for more characters to be put back
6759 #ifdef FEAT_VREPLACE
6772 #ifdef FEAT_VREPLACE
6773 if (State
& VREPLACE_FLAG
)
6775 /* Get the number of screen cells used by the character we are
6776 * going to delete. */
6777 getvcol(curwin
, &curwin
->w_cursor
, NULL
, &start_vcol
, NULL
);
6778 orig_vcols
= chartabsize(ml_get_cursor(), start_vcol
);
6785 # ifdef FEAT_VREPLACE
6786 if (State
& VREPLACE_FLAG
)
6787 orig_len
= (int)STRLEN(ml_get_cursor());
6795 #ifdef FEAT_VREPLACE
6796 if (State
& VREPLACE_FLAG
)
6797 orig_len
= (int)STRLEN(ml_get_cursor()) - 1;
6802 #ifdef FEAT_VREPLACE
6803 if (State
& VREPLACE_FLAG
)
6805 /* Get the number of screen cells used by the inserted characters */
6806 p
= ml_get_cursor();
6807 ins_len
= (int)STRLEN(p
) - orig_len
;
6809 for (i
= 0; i
< ins_len
; ++i
)
6811 vcol
+= chartabsize(p
+ i
, vcol
);
6813 i
+= (*mb_ptr2len
)(p
) - 1;
6818 /* Delete spaces that were inserted after the cursor to keep the
6820 curwin
->w_cursor
.col
+= ins_len
;
6821 while (vcol
> orig_vcols
&& gchar_cursor() == ' ')
6826 curwin
->w_cursor
.col
-= ins_len
;
6830 /* mark the buffer as changed and prepare for displaying */
6831 changed_bytes(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
6834 (void)del_char(FALSE
);
6839 * Return TRUE if C-indenting is on.
6844 return (!p_paste
&& (curbuf
->b_p_cin
6846 || *curbuf
->b_p_inde
!= NUL
6852 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6854 * Re-indent the current line, based on the current contents of it and the
6855 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6856 * confused what all the part that handles Control-T is doing that I'm not.
6857 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6861 fixthisline(get_the_indent
)
6862 int (*get_the_indent
) __ARGS((void));
6864 change_indent(INDENT_SET
, get_the_indent(), FALSE
, 0);
6865 if (linewhite(curwin
->w_cursor
.lnum
))
6866 did_ai
= TRUE
; /* delete the indent if the line stays empty */
6875 if (curbuf
->b_p_lisp
&& curbuf
->b_p_ai
)
6876 fixthisline(get_lisp_indent
);
6878 # if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6881 # ifdef FEAT_CINDENT
6891 * return TRUE if 'cinkeys' contains the key "keytyped",
6892 * when == '*': Only if key is preceded with '*' (indent before insert)
6893 * when == '!': Only if key is prededed with '!' (don't insert)
6894 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6896 * "keytyped" can have a few special values:
6899 * KEY_COMPLETE just finished completion.
6901 * If line_is_empty is TRUE accept keys with '0' before them.
6904 in_cinkeys(keytyped
, when
, line_is_empty
)
6918 if (*curbuf
->b_p_inde
!= NUL
)
6919 look
= curbuf
->b_p_indk
; /* 'indentexpr' set: use 'indentkeys' */
6922 look
= curbuf
->b_p_cink
; /* 'indentexpr' empty: use 'cinkeys' */
6926 * Find out if we want to try a match with this key, depending on
6927 * 'when' and a '*' or '!' before the key.
6931 case '*': try_match
= (*look
== '*'); break;
6932 case '!': try_match
= (*look
== '!'); break;
6933 default: try_match
= (*look
!= '*'); break;
6935 if (*look
== '*' || *look
== '!')
6939 * If there is a '0', only accept a match if the line is empty.
6940 * But may still match when typing last char of a word.
6944 try_match_word
= try_match
;
6950 try_match_word
= FALSE
;
6953 * does it look like a control character?
6957 && (Ctrl_chr(look
[1]) != 0)
6959 && look
[1] >= '?' && look
[1] <= '_'
6963 if (try_match
&& keytyped
== Ctrl_chr(look
[1]))
6968 * 'o' means "o" command, open forward.
6969 * 'O' means "O" command, open backward.
6971 else if (*look
== 'o')
6973 if (try_match
&& keytyped
== KEY_OPEN_FORW
)
6977 else if (*look
== 'O')
6979 if (try_match
&& keytyped
== KEY_OPEN_BACK
)
6985 * 'e' means to check for "else" at start of line and just before the
6988 else if (*look
== 'e')
6990 if (try_match
&& keytyped
== 'e' && curwin
->w_cursor
.col
>= 4)
6992 p
= ml_get_curline();
6993 if (skipwhite(p
) == p
+ curwin
->w_cursor
.col
- 4 &&
6994 STRNCMP(p
+ curwin
->w_cursor
.col
- 4, "else", 4) == 0)
7001 * ':' only causes an indent if it is at the end of a label or case
7002 * statement, or when it was before typing the ':' (to fix
7003 * class::method for C++).
7005 else if (*look
== ':')
7007 if (try_match
&& keytyped
== ':')
7009 p
= ml_get_curline();
7010 if (cin_iscase(p
) || cin_isscopedecl(p
) || cin_islabel(30))
7012 if (curwin
->w_cursor
.col
> 2
7013 && p
[curwin
->w_cursor
.col
- 1] == ':'
7014 && p
[curwin
->w_cursor
.col
- 2] == ':')
7016 p
[curwin
->w_cursor
.col
- 1] = ' ';
7017 i
= (cin_iscase(p
) || cin_isscopedecl(p
)
7018 || cin_islabel(30));
7019 p
= ml_get_curline();
7020 p
[curwin
->w_cursor
.col
- 1] = ':';
7030 * Is it a key in <>, maybe?
7032 else if (*look
== '<')
7037 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7038 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7039 * >, *, : and ! keys if they really really want to.
7041 if (vim_strchr((char_u
*)"<>!*oOe0:", look
[1]) != NULL
7042 && keytyped
== look
[1])
7045 if (keytyped
== get_special_key_code(look
+ 1))
7048 while (*look
&& *look
!= '>')
7050 while (*look
== '>')
7055 * Is it a word: "=word"?
7057 else if (*look
== '=' && look
[1] != ',' && look
[1] != NUL
)
7067 p
= vim_strchr(look
, ',');
7069 p
= look
+ STRLEN(look
);
7070 if ((try_match
|| try_match_word
)
7071 && curwin
->w_cursor
.col
>= (colnr_T
)(p
- look
))
7075 #ifdef FEAT_INS_EXPAND
7076 if (keytyped
== KEY_COMPLETE
)
7080 /* Just completed a word, check if it starts with "look".
7081 * search back for the start of a word. */
7082 line
= ml_get_curline();
7088 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; s
= n
)
7090 n
= mb_prevptr(line
, s
);
7091 if (!vim_iswordp(n
))
7097 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; --s
)
7098 if (!vim_iswordc(s
[-1]))
7100 if (s
+ (p
- look
) <= line
+ curwin
->w_cursor
.col
7102 ? MB_STRNICMP(s
, look
, p
- look
)
7103 : STRNCMP(s
, look
, p
- look
)) == 0)
7108 /* TODO: multi-byte */
7109 if (keytyped
== (int)p
[-1] || (icase
&& keytyped
< 256
7110 && TOLOWER_LOC(keytyped
) == TOLOWER_LOC((int)p
[-1])))
7112 line
= ml_get_cursor();
7113 if ((curwin
->w_cursor
.col
== (colnr_T
)(p
- look
)
7114 || !vim_iswordc(line
[-(p
- look
) - 1]))
7116 ? MB_STRNICMP(line
- (p
- look
), look
, p
- look
)
7117 : STRNCMP(line
- (p
- look
), look
, p
- look
))
7121 if (match
&& try_match_word
&& !try_match
)
7123 /* "0=word": Check if there are only blanks before the
7125 line
= ml_get_curline();
7126 if ((int)(skipwhite(line
) - line
) !=
7127 (int)(curwin
->w_cursor
.col
- (p
- look
)))
7137 * ok, it's a boring generic character.
7141 if (try_match
&& *look
== keytyped
)
7149 look
= skip_to_option_part(look
);
7153 #endif /* FEAT_CINDENT */
7155 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7157 * Map Hebrew keyboard when in hkmap mode.
7163 if (p_hkmapp
) /* phonetic mapping, by Ilya Dogolazky */
7165 enum {hALEF
=0, BET
, GIMEL
, DALET
, HEI
, VAV
, ZAIN
, HET
, TET
, IUD
,
7166 KAFsofit
, hKAF
, LAMED
, MEMsofit
, MEM
, NUNsofit
, NUN
, SAMEH
, AIN
,
7167 PEIsofit
, PEI
, ZADIsofit
, ZADI
, KOF
, RESH
, hSHIN
, TAV
};
7168 static char_u map
[26] =
7169 {(char_u
)hALEF
/*a*/, (char_u
)BET
/*b*/, (char_u
)hKAF
/*c*/,
7170 (char_u
)DALET
/*d*/, (char_u
)-1 /*e*/, (char_u
)PEIsofit
/*f*/,
7171 (char_u
)GIMEL
/*g*/, (char_u
)HEI
/*h*/, (char_u
)IUD
/*i*/,
7172 (char_u
)HET
/*j*/, (char_u
)KOF
/*k*/, (char_u
)LAMED
/*l*/,
7173 (char_u
)MEM
/*m*/, (char_u
)NUN
/*n*/, (char_u
)SAMEH
/*o*/,
7174 (char_u
)PEI
/*p*/, (char_u
)-1 /*q*/, (char_u
)RESH
/*r*/,
7175 (char_u
)ZAIN
/*s*/, (char_u
)TAV
/*t*/, (char_u
)TET
/*u*/,
7176 (char_u
)VAV
/*v*/, (char_u
)hSHIN
/*w*/, (char_u
)-1 /*x*/,
7177 (char_u
)AIN
/*y*/, (char_u
)ZADI
/*z*/};
7179 if (c
== 'N' || c
== 'M' || c
== 'P' || c
== 'C' || c
== 'Z')
7180 return (int)(map
[CharOrd(c
)] - 1 + p_aleph
);
7185 return '\''; /* {geresh}={'} */
7187 return ' '; /* \"o --> ' ' for a german keyboard */
7189 return ' '; /* \"a --> ' ' -- / -- */
7191 return ' '; /* \"u --> ' ' -- / -- */
7193 else if (islower(c
))
7195 /* NOTE: islower() does not do the right thing for us on Linux so we
7196 * do this the same was as 5.7 and previous, so it works correctly on
7197 * all systems. Specifically, the e.g. Delete and Arrow keys are
7198 * munged and won't work if e.g. searching for Hebrew text.
7200 else if (c
>= 'a' && c
<= 'z')
7202 return (int)(map
[CharOrdLow(c
)] + p_aleph
);
7210 case '`': return ';';
7211 case '/': return '.';
7212 case '\'': return ',';
7213 case 'q': return '/';
7214 case 'w': return '\'';
7216 /* Hebrew letters - set offset from 'a' */
7217 case ',': c
= '{'; break;
7218 case '.': c
= 'v'; break;
7219 case ';': c
= 't'; break;
7221 static char str
[] = "zqbcxlsjphmkwonu ydafe rig";
7224 /* see note about islower() above */
7227 if (c
< 'a' || c
> 'z')
7230 c
= str
[CharOrdLow(c
)];
7235 return (int)(CharOrdLow(c
) + p_aleph
);
7243 int need_redraw
= FALSE
;
7248 * If we are going to wait for a character, show a '"'.
7250 pc_status
= PC_STATUS_UNSET
;
7251 if (redrawing() && !char_avail())
7253 /* may need to redraw when no more chars available now */
7256 edit_putchar('"', TRUE
);
7257 #ifdef FEAT_CMDL_INFO
7258 add_to_showcmd_c(Ctrl_R
);
7262 #ifdef USE_ON_FLY_SCROLL
7263 dont_scroll
= TRUE
; /* disallow scrolling here */
7267 * Don't map the register name. This also prevents the mode message to be
7268 * deleted when ESC is hit.
7271 regname
= safe_vgetc();
7273 LANGMAP_ADJUST(regname
, TRUE
);
7275 if (regname
== Ctrl_R
|| regname
== Ctrl_O
|| regname
== Ctrl_P
)
7277 /* Get a third key for literal register insertion */
7278 literally
= regname
;
7279 #ifdef FEAT_CMDL_INFO
7280 add_to_showcmd_c(literally
);
7282 regname
= safe_vgetc();
7284 LANGMAP_ADJUST(regname
, TRUE
);
7291 * Don't call u_sync() while getting the expression,
7292 * evaluating it or giving an error message for it!
7297 # ifdef USE_IM_CONTROL
7298 int im_on
= im_get_status();
7300 regname
= get_expr_register();
7301 # ifdef USE_IM_CONTROL
7302 /* Restore the Input Method. */
7304 im_set_active(TRUE
);
7307 if (regname
== NUL
|| !valid_yank_reg(regname
, FALSE
))
7310 need_redraw
= TRUE
; /* remove the '"' */
7315 if (literally
== Ctrl_O
|| literally
== Ctrl_P
)
7317 /* Append the command to the redo buffer. */
7318 AppendCharToRedobuff(Ctrl_R
);
7319 AppendCharToRedobuff(literally
);
7320 AppendCharToRedobuff(regname
);
7322 do_put(regname
, BACKWARD
, 1L,
7323 (literally
== Ctrl_P
? PUT_FIXINDENT
: 0) | PUT_CURSEND
);
7325 else if (insert_reg(regname
, literally
) == FAIL
)
7328 need_redraw
= TRUE
; /* remove the '"' */
7330 else if (stop_insert_mode
)
7331 /* When the '=' register was used and a function was invoked that
7332 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7333 * insert anything, need to remove the '"' */
7340 #ifdef FEAT_CMDL_INFO
7344 /* If the inserted register is empty, we need to remove the '"' */
7345 if (need_redraw
|| stuff_empty())
7350 * CTRL-G commands in Insert mode.
7357 #ifdef FEAT_INS_EXPAND
7358 /* Right after CTRL-X the cursor will be after the ruler. */
7363 * Don't map the second key. This also prevents the mode message to be
7364 * deleted when ESC is hit.
7371 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7374 case 'k': ins_up(TRUE
);
7377 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7380 case 'j': ins_down(TRUE
);
7383 /* CTRL-G u: start new undoable edit */
7384 case 'u': u_sync(TRUE
);
7385 ins_need_undo
= TRUE
;
7387 /* Need to reset Insstart, esp. because a BS that joins
7388 * aline to the previous one must save for undo. */
7389 Insstart
= curwin
->w_cursor
;
7392 /* Unknown CTRL-G command, reserved for future expansion. */
7393 default: vim_beep();
7398 * CTRL-^ in Insert mode.
7403 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
7405 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7406 if (State
& LANGMAP
)
7408 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7413 curbuf
->b_p_iminsert
= B_IMODE_LMAP
;
7415 #ifdef USE_IM_CONTROL
7416 im_set_active(FALSE
);
7420 #ifdef USE_IM_CONTROL
7423 /* There are no ":lmap" mappings, toggle IM */
7424 if (im_get_status())
7426 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7427 im_set_active(FALSE
);
7431 curbuf
->b_p_iminsert
= B_IMODE_IM
;
7433 im_set_active(TRUE
);
7437 set_iminsert_global();
7440 /* may show different cursor shape or color */
7442 gui_update_cursor(TRUE
, FALSE
);
7444 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7445 /* Show/unshow value of 'keymap' in status lines. */
7446 status_redraw_curbuf();
7451 * Handle ESC in insert mode.
7452 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7456 ins_esc(count
, cmdchar
, nomove
)
7459 int nomove
; /* don't move cursor */
7462 static int disabled_redraw
= FALSE
;
7465 check_spell_redraw();
7467 #if defined(FEAT_HANGULIN)
7468 # if defined(ESC_CHG_TO_ENG_MODE)
7469 hangul_input_state_set(0);
7471 if (composing_hangul
)
7473 push_raw_key(composing_hangul_buffer
, 2);
7474 composing_hangul
= 0;
7478 temp
= curwin
->w_cursor
.col
;
7479 if (disabled_redraw
)
7481 --RedrawingDisabled
;
7482 disabled_redraw
= FALSE
;
7487 * Don't append the ESC for "r<CR>" and "grx".
7488 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7489 * when "count" is non-zero.
7491 if (cmdchar
!= 'r' && cmdchar
!= 'v')
7492 AppendToRedobuff(p_im
? (char_u
*)"\014" : ESC_STR
);
7495 * Repeating insert may take a long time. Check for
7496 * interrupt now and then.
7505 if (--*count
> 0) /* repeat what was typed */
7507 /* Vi repeats the insert without replacing characters. */
7508 if (vim_strchr(p_cpo
, CPO_REPLCNT
) != NULL
)
7509 State
&= ~REPLACE_FLAG
;
7511 (void)start_redo_ins();
7512 if (cmdchar
== 'r' || cmdchar
== 'v')
7513 stuffReadbuff(ESC_STR
); /* no ESC in redo buffer */
7514 ++RedrawingDisabled
;
7515 disabled_redraw
= TRUE
;
7516 return FALSE
; /* repeat the insert */
7518 stop_insert(&curwin
->w_cursor
, TRUE
);
7522 /* When an autoindent was removed, curswant stays after the
7524 if (restart_edit
== NUL
&& (colnr_T
)temp
== curwin
->w_cursor
.col
)
7525 curwin
->w_set_curswant
= TRUE
;
7527 /* Remember the last Insert position in the '^ mark. */
7528 if (!cmdmod
.keepjumps
)
7529 curbuf
->b_last_insert
= curwin
->w_cursor
;
7532 * The cursor should end up on the last inserted character.
7533 * Don't do it for CTRL-O, unless past the end of the line.
7536 && (curwin
->w_cursor
.col
!= 0
7537 #ifdef FEAT_VIRTUALEDIT
7538 || curwin
->w_cursor
.coladd
> 0
7541 && (restart_edit
== NUL
7542 || (gchar_cursor() == NUL
7547 #ifdef FEAT_RIGHTLEFT
7552 #ifdef FEAT_VIRTUALEDIT
7553 if (curwin
->w_cursor
.coladd
> 0 || ve_flags
== VE_ALL
)
7556 if (restart_edit
!= NUL
)
7557 ++curwin
->w_cursor
.coladd
;
7562 --curwin
->w_cursor
.col
;
7564 /* Correct cursor for multi-byte character. */
7571 #ifdef USE_IM_CONTROL
7572 /* Disable IM to allow typing English directly for Normal mode commands.
7573 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7575 if (!(State
& LANGMAP
))
7576 im_save_status(&curbuf
->b_p_iminsert
);
7577 im_set_active(FALSE
);
7581 /* need to position cursor again (e.g. when on a TAB ) */
7582 changed_cline_bef_curs();
7588 ui_cursor_shape(); /* may show different cursor shape */
7592 * When recording or for CTRL-O, need to display the new mode.
7593 * Otherwise remove the mode message.
7595 if (Recording
|| restart_edit
!= NUL
)
7600 return TRUE
; /* exit Insert mode */
7603 #ifdef FEAT_RIGHTLEFT
7605 * Toggle language: hkmap and revins_on.
7606 * Move to end of reverse inserted text.
7611 if (revins_on
&& revins_chars
&& revins_scol
>= 0)
7613 while (gchar_cursor() != NUL
&& revins_chars
--)
7614 ++curwin
->w_cursor
.col
;
7617 revins_on
= (State
== INSERT
&& p_ri
);
7620 revins_scol
= curwin
->w_cursor
.col
;
7631 * to be consistent also for redo command, using '.'
7632 * set arrow_used to true and stop it - causing to redo
7633 * characters entered in one mode (normal/reverse insert).
7637 p_fkmap
= curwin
->w_p_rl
^ p_ri
;
7638 if (p_fkmap
&& p_ri
)
7643 p_hkmap
= curwin
->w_p_rl
^ p_ri
; /* be consistent! */
7650 * If 'keymodel' contains "startsel", may start selection.
7651 * Returns TRUE when a CTRL-O and other keys stuffed.
7674 if (!(mod_mask
& MOD_MASK_SHIFT
))
7683 /* Start selection right away, the cursor can move with
7684 * CTRL-O when beyond the end of the line. */
7687 /* Execute the key in (insert) Select mode. */
7688 stuffcharReadbuff(Ctrl_O
);
7694 buf
[1] = KS_MODIFIER
;
7699 stuffcharReadbuff(c
);
7707 * <Insert> key in Insert mode: toggle insert/remplace mode.
7710 ins_insert(replaceState
)
7714 if (p_fkmap
&& p_ri
)
7717 EMSG(farsi_text_3
); /* encoded in Farsi */
7724 set_vim_var_string(VV_INSERTMODE
,
7725 (char_u
*)((State
& REPLACE_FLAG
) ? "i" :
7726 # ifdef FEAT_VREPLACE
7727 replaceState
== VREPLACE
? "v" :
7731 apply_autocmds(EVENT_INSERTCHANGE
, NULL
, NULL
, FALSE
, curbuf
);
7733 if (State
& REPLACE_FLAG
)
7734 State
= INSERT
| (State
& LANGMAP
);
7736 State
= replaceState
| (State
& LANGMAP
);
7737 AppendCharToRedobuff(K_INS
);
7740 ui_cursor_shape(); /* may show different cursor shape */
7745 * Pressed CTRL-O in Insert mode.
7750 #ifdef FEAT_VREPLACE
7751 if (State
& VREPLACE_FLAG
)
7755 if (State
& REPLACE_FLAG
)
7759 #ifdef FEAT_VIRTUALEDIT
7760 if (virtual_active())
7761 ins_at_eol
= FALSE
; /* cursor always keeps its column */
7764 ins_at_eol
= (gchar_cursor() == NUL
);
7768 * If the cursor is on an indent, ^T/^D insert/delete one
7769 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7770 * Always round the indent to 'shiftwith', this is compatible
7771 * with vi. But vi only supports ^T and ^D after an
7772 * autoindent, we support it everywhere.
7779 if (stop_arrow() == FAIL
)
7781 AppendCharToRedobuff(c
);
7784 * 0^D and ^^D: remove all indent.
7786 if ((lastc
== '0' || lastc
== '^') && curwin
->w_cursor
.col
)
7788 --curwin
->w_cursor
.col
;
7789 (void)del_char(FALSE
); /* delete the '^' or '0' */
7790 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7791 if (State
& REPLACE_FLAG
)
7794 old_indent
= get_indent(); /* remember curr. indent */
7795 change_indent(INDENT_SET
, 0, TRUE
, 0);
7798 change_indent(c
== Ctrl_D
? INDENT_DEC
: INDENT_INC
, 0, TRUE
, 0);
7800 if (did_ai
&& *skipwhite(ml_get_curline()) != NUL
)
7802 #ifdef FEAT_SMARTINDENT
7805 can_si_back
= FALSE
;
7808 can_cindent
= FALSE
; /* no cindenting after ^D or ^T */
7817 if (stop_arrow() == FAIL
)
7819 if (gchar_cursor() == NUL
) /* delete newline */
7821 temp
= curwin
->w_cursor
.col
;
7822 if (!can_bs(BS_EOL
) /* only if "eol" included */
7823 || u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
7824 (linenr_T
)(curwin
->w_cursor
.lnum
+ 2)) == FAIL
7825 || do_join(FALSE
) == FAIL
)
7828 curwin
->w_cursor
.col
= temp
;
7830 else if (del_char(FALSE
) == FAIL
) /* delete char under cursor */
7833 #ifdef FEAT_SMARTINDENT
7836 can_si_back
= FALSE
;
7838 AppendCharToRedobuff(K_DEL
);
7842 * Handle Backspace, delete-word and delete-line in Insert mode.
7843 * Return TRUE when backspace was actually used.
7846 ins_bs(c
, mode
, inserted_space_p
)
7849 int *inserted_space_p
;
7853 int temp
= 0; /* init for GCC */
7855 int did_backspace
= FALSE
;
7859 int cpc
[MAX_MCO
]; /* composing characters */
7863 * can't delete anything in an empty file
7864 * can't backup past first character in buffer
7865 * can't backup past starting point unless 'backspace' > 1
7866 * can backup to a previous line if 'backspace' == 0
7870 #ifdef FEAT_RIGHTLEFT
7873 ((curwin
->w_cursor
.lnum
== 1 && curwin
->w_cursor
.col
== 0)
7874 || (!can_bs(BS_START
)
7876 || (curwin
->w_cursor
.lnum
== Insstart
.lnum
7877 && curwin
->w_cursor
.col
<= Insstart
.col
)))
7878 || (!can_bs(BS_INDENT
) && !arrow_used
&& ai_col
> 0
7879 && curwin
->w_cursor
.col
<= ai_col
)
7880 || (!can_bs(BS_EOL
) && curwin
->w_cursor
.col
== 0))))
7886 if (stop_arrow() == FAIL
)
7888 in_indent
= inindent(0);
7891 can_cindent
= FALSE
;
7893 #ifdef FEAT_COMMENTS
7894 end_comment_pending
= NUL
; /* After BS, don't auto-end comment */
7896 #ifdef FEAT_RIGHTLEFT
7897 if (revins_on
) /* put cursor after last inserted char */
7901 #ifdef FEAT_VIRTUALEDIT
7903 * BACKSPACE_CHAR eats a virtual space
7904 * BACKSPACE_WORD eats all coladd
7905 * BACKSPACE_LINE eats all coladd and keeps going
7907 if (curwin
->w_cursor
.coladd
> 0)
7909 if (mode
== BACKSPACE_CHAR
)
7911 --curwin
->w_cursor
.coladd
;
7914 if (mode
== BACKSPACE_WORD
)
7916 curwin
->w_cursor
.coladd
= 0;
7919 curwin
->w_cursor
.coladd
= 0;
7926 if (curwin
->w_cursor
.col
== 0)
7928 lnum
= Insstart
.lnum
;
7929 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
7930 #ifdef FEAT_RIGHTLEFT
7935 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 2),
7936 (linenr_T
)(curwin
->w_cursor
.lnum
+ 1)) == FAIL
)
7939 Insstart
.col
= MAXCOL
;
7943 * cc < 0: NL was inserted, delete it
7944 * cc >= 0: NL was replaced, put original characters back
7947 if (State
& REPLACE_FLAG
)
7948 cc
= replace_pop(); /* returns -1 if NL was inserted */
7950 * In replace mode, in the line we started replacing, we only move the
7953 if ((State
& REPLACE_FLAG
) && curwin
->w_cursor
.lnum
<= lnum
)
7959 #ifdef FEAT_VREPLACE
7960 if (!(State
& VREPLACE_FLAG
)
7961 || curwin
->w_cursor
.lnum
> orig_line_count
)
7964 temp
= gchar_cursor(); /* remember current char */
7965 --curwin
->w_cursor
.lnum
;
7967 /* When "aw" is in 'formatoptions' we must delete the space at
7968 * the end of the line, otherwise the line will be broken
7969 * again when auto-formatting. */
7970 if (has_format_option(FO_AUTO
)
7971 && has_format_option(FO_WHITE_PAR
))
7973 char_u
*ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
,
7977 len
= (int)STRLEN(ptr
);
7978 if (len
> 0 && ptr
[len
- 1] == ' ')
7982 (void)do_join(FALSE
);
7983 if (temp
== NUL
&& gchar_cursor() != NUL
)
7986 #ifdef FEAT_VREPLACE
7992 * In REPLACE mode we have to put back the text that was replaced
7993 * by the NL. On the replace stack is first a NUL-terminated
7994 * sequence of characters that were deleted and then the
7995 * characters that NL replaced.
7997 if (State
& REPLACE_FLAG
)
8000 * Do the next ins_char() in NORMAL state, to
8001 * prevent ins_char() from replacing characters and
8002 * avoiding showmatch().
8007 * restore characters (blanks) deleted after cursor
8011 temp
= curwin
->w_cursor
.col
;
8013 mb_replace_pop_ins(cc
);
8017 curwin
->w_cursor
.col
= temp
;
8020 /* restore the characters that NL replaced */
8030 * Delete character(s) before the cursor.
8032 #ifdef FEAT_RIGHTLEFT
8033 if (revins_on
) /* put cursor on last inserted char */
8038 if (mode
== BACKSPACE_LINE
&& curbuf
->b_p_ai
8039 #ifdef FEAT_RIGHTLEFT
8044 temp
= curwin
->w_cursor
.col
;
8045 beginline(BL_WHITE
);
8046 if (curwin
->w_cursor
.col
< (colnr_T
)temp
)
8047 mincol
= curwin
->w_cursor
.col
;
8048 curwin
->w_cursor
.col
= temp
;
8052 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8054 if ( mode
== BACKSPACE_CHAR
8055 && ((p_sta
&& in_indent
)
8056 || (curbuf
->b_p_sts
!= 0
8057 && (*(ml_get_cursor() - 1) == TAB
8058 || (*(ml_get_cursor() - 1) == ' '
8059 && (!*inserted_space_p
8069 *inserted_space_p
= FALSE
;
8070 if (p_sta
&& in_indent
)
8071 ts
= curbuf
->b_p_sw
;
8073 ts
= curbuf
->b_p_sts
;
8074 /* Compute the virtual column where we want to be. Since
8075 * 'showbreak' may get in the way, need to get the last column of
8076 * the previous character. */
8077 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8079 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &want_vcol
);
8081 want_vcol
= (want_vcol
/ ts
) * ts
;
8083 /* delete characters until we are at or before want_vcol */
8084 while (vcol
> want_vcol
8085 && (cc
= *(ml_get_cursor() - 1), vim_iswhite(cc
)))
8088 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8089 if (State
& REPLACE_FLAG
)
8091 /* Don't delete characters before the insert point when in
8093 if (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8094 || curwin
->w_cursor
.col
>= Insstart
.col
)
8096 #if 0 /* what was this for? It causes problems when sw != ts. */
8097 if (State
== REPLACE
&& (int)vcol
< want_vcol
)
8099 (void)del_char(FALSE
);
8100 extra
= 2; /* don't pop too much */
8108 (void)del_char(FALSE
);
8111 /* insert extra spaces until we are at want_vcol */
8112 while (vcol
< want_vcol
)
8114 /* Remember the first char we inserted */
8115 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8116 && curwin
->w_cursor
.col
< Insstart
.col
)
8117 Insstart
.col
= curwin
->w_cursor
.col
;
8119 #ifdef FEAT_VREPLACE
8120 if (State
& VREPLACE_FLAG
)
8125 ins_str((char_u
*)" ");
8126 if ((State
& REPLACE_FLAG
) /* && extra <= 1 */)
8130 replace_push_off(NUL
);
8140 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8145 * Delete upto starting point, start of line or previous word.
8149 #ifdef FEAT_RIGHTLEFT
8150 if (!revins_on
) /* put cursor on char to be deleted */
8154 /* start of word? */
8155 if (mode
== BACKSPACE_WORD
&& !vim_isspace(gchar_cursor()))
8157 mode
= BACKSPACE_WORD_NOT_SPACE
;
8158 temp
= vim_iswordc(gchar_cursor());
8161 else if (mode
== BACKSPACE_WORD_NOT_SPACE
8162 && (vim_isspace(cc
= gchar_cursor())
8163 || vim_iswordc(cc
) != temp
))
8165 #ifdef FEAT_RIGHTLEFT
8169 #ifdef FEAT_RIGHTLEFT
8170 else if (State
& REPLACE_FLAG
)
8175 if (State
& REPLACE_FLAG
)
8180 if (enc_utf8
&& p_deco
)
8181 (void)utfc_ptr2char(ml_get_cursor(), cpc
);
8183 (void)del_char(FALSE
);
8186 * If there are combining characters and 'delcombine' is set
8187 * move the cursor back. Don't back up before the base
8190 if (enc_utf8
&& p_deco
&& cpc
[0] != NUL
)
8193 #ifdef FEAT_RIGHTLEFT
8199 if (revins_on
&& gchar_cursor() == NUL
)
8203 /* Just a single backspace?: */
8204 if (mode
== BACKSPACE_CHAR
)
8207 #ifdef FEAT_RIGHTLEFT
8210 (curwin
->w_cursor
.col
> mincol
8211 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8212 || curwin
->w_cursor
.col
!= Insstart
.col
)));
8213 did_backspace
= TRUE
;
8215 #ifdef FEAT_SMARTINDENT
8218 can_si_back
= FALSE
;
8220 if (curwin
->w_cursor
.col
<= 1)
8223 * It's a little strange to put backspaces into the redo
8224 * buffer, but it makes auto-indent a lot easier to deal
8227 AppendCharToRedobuff(c
);
8229 /* If deleted before the insertion point, adjust it */
8230 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8231 && curwin
->w_cursor
.col
< Insstart
.col
)
8232 Insstart
.col
= curwin
->w_cursor
.col
;
8234 /* vi behaviour: the cursor moves backward but the character that
8235 * was there remains visible
8236 * Vim behaviour: the cursor moves backward and the character that
8237 * was there is erased from the screen.
8238 * We can emulate the vi behaviour by pretending there is a dollar
8239 * displayed even when there isn't.
8240 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8241 if (vim_strchr(p_cpo
, CPO_BACKSPACE
) != NULL
&& dollar_vcol
== 0)
8242 dollar_vcol
= curwin
->w_virtcol
;
8244 return did_backspace
;
8253 win_T
*old_curwin
= curwin
;
8256 /* When GUI is active, also move/paste when 'mouse' is empty */
8259 if (!mouse_has(MOUSE_INSERT
))
8263 tpos
= curwin
->w_cursor
;
8264 if (do_mouse(NULL
, c
, BACKWARD
, 1L, 0))
8267 win_T
*new_curwin
= curwin
;
8269 if (curwin
!= old_curwin
&& win_valid(old_curwin
))
8271 /* Mouse took us to another window. We need to go back to the
8272 * previous one to stop insert there properly. */
8273 curwin
= old_curwin
;
8274 curbuf
= curwin
->w_buffer
;
8277 start_arrow(curwin
== old_curwin
? &tpos
: NULL
);
8279 if (curwin
!= new_curwin
&& win_valid(new_curwin
))
8281 curwin
= new_curwin
;
8282 curbuf
= curwin
->w_buffer
;
8285 # ifdef FEAT_CINDENT
8291 /* redraw status lines (in case another window became active) */
8292 redraw_statuslines();
8301 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8305 tpos
= curwin
->w_cursor
;
8307 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8308 old_curwin
= curwin
;
8310 /* Currently the mouse coordinates are only known in the GUI. */
8311 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
8318 /* find the window at the pointer coordinates */
8319 curwin
= mouse_find_win(&row
, &col
);
8320 curbuf
= curwin
->w_buffer
;
8322 if (curwin
== old_curwin
)
8326 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
8327 scroll_redraw(up
, (long)(curwin
->w_botline
- curwin
->w_topline
));
8329 scroll_redraw(up
, 3L);
8331 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8332 curwin
->w_redr_status
= TRUE
;
8334 curwin
= old_curwin
;
8335 curbuf
= curwin
->w_buffer
;
8338 if (!equalpos(curwin
->w_cursor
, tpos
))
8341 # ifdef FEAT_CINDENT
8348 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8353 /* We will be leaving the current window, unless closing another tab. */
8354 if (c
!= K_TABMENU
|| current_tabmenu
!= TABLINE_MENU_CLOSE
8355 || (current_tab
!= 0 && current_tab
!= tabpage_index(curtab
)))
8358 start_arrow(&curwin
->w_cursor
);
8359 # ifdef FEAT_CINDENT
8365 goto_tabpage(current_tab
);
8372 #if defined(FEAT_GUI) || defined(PROTO)
8379 tpos
= curwin
->w_cursor
;
8380 if (gui_do_scroll())
8383 # ifdef FEAT_CINDENT
8395 tpos
= curwin
->w_cursor
;
8396 if (gui_do_horiz_scroll())
8399 # ifdef FEAT_CINDENT
8412 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8416 tpos
= curwin
->w_cursor
;
8417 if (oneleft() == OK
)
8420 #ifdef FEAT_RIGHTLEFT
8421 /* If exit reversed string, position is fixed */
8422 if (revins_scol
!= -1 && (int)curwin
->w_cursor
.col
>= revins_scol
)
8429 * if 'whichwrap' set for cursor in insert mode may go to
8432 else if (vim_strchr(p_ww
, '[') != NULL
&& curwin
->w_cursor
.lnum
> 1)
8435 --(curwin
->w_cursor
.lnum
);
8436 coladvance((colnr_T
)MAXCOL
);
8437 curwin
->w_set_curswant
= TRUE
; /* so we stay at the end */
8450 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8454 tpos
= curwin
->w_cursor
;
8456 curwin
->w_cursor
.lnum
= 1;
8457 curwin
->w_cursor
.col
= 0;
8458 #ifdef FEAT_VIRTUALEDIT
8459 curwin
->w_cursor
.coladd
= 0;
8461 curwin
->w_curswant
= 0;
8472 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8476 tpos
= curwin
->w_cursor
;
8478 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
8479 coladvance((colnr_T
)MAXCOL
);
8480 curwin
->w_curswant
= MAXCOL
;
8489 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8493 if (curwin
->w_cursor
.lnum
> 1 || curwin
->w_cursor
.col
> 0)
8495 start_arrow(&curwin
->w_cursor
);
8496 (void)bck_word(1L, FALSE
, FALSE
);
8497 curwin
->w_set_curswant
= TRUE
;
8507 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8511 if (gchar_cursor() != NUL
|| virtual_active()
8514 start_arrow(&curwin
->w_cursor
);
8515 curwin
->w_set_curswant
= TRUE
;
8516 #ifdef FEAT_VIRTUALEDIT
8517 if (virtual_active())
8524 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
8527 ++curwin
->w_cursor
.col
;
8530 #ifdef FEAT_RIGHTLEFT
8536 /* if 'whichwrap' set for cursor in insert mode, may move the
8537 * cursor to the next line */
8538 else if (vim_strchr(p_ww
, ']') != NULL
8539 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
8541 start_arrow(&curwin
->w_cursor
);
8542 curwin
->w_set_curswant
= TRUE
;
8543 ++curwin
->w_cursor
.lnum
;
8544 curwin
->w_cursor
.col
= 0;
8554 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8558 if (curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
8559 || gchar_cursor() != NUL
)
8561 start_arrow(&curwin
->w_cursor
);
8562 (void)fwd_word(1L, FALSE
, 0);
8563 curwin
->w_set_curswant
= TRUE
;
8571 int startcol
; /* when TRUE move to Insstart.col */
8574 linenr_T old_topline
= curwin
->w_topline
;
8576 int old_topfill
= curwin
->w_topfill
;
8580 tpos
= curwin
->w_cursor
;
8581 if (cursor_up(1L, TRUE
) == OK
)
8584 coladvance(getvcol_nolist(&Insstart
));
8585 if (old_topline
!= curwin
->w_topline
8587 || old_topfill
!= curwin
->w_topfill
8590 redraw_later(VALID
);
8608 if (mod_mask
& MOD_MASK_CTRL
)
8610 /* <C-PageUp>: tab page back */
8616 tpos
= curwin
->w_cursor
;
8617 if (onepage(BACKWARD
, 1L) == OK
)
8630 int startcol
; /* when TRUE move to Insstart.col */
8633 linenr_T old_topline
= curwin
->w_topline
;
8635 int old_topfill
= curwin
->w_topfill
;
8639 tpos
= curwin
->w_cursor
;
8640 if (cursor_down(1L, TRUE
) == OK
)
8643 coladvance(getvcol_nolist(&Insstart
));
8644 if (old_topline
!= curwin
->w_topline
8646 || old_topfill
!= curwin
->w_topfill
8649 redraw_later(VALID
);
8667 if (mod_mask
& MOD_MASK_CTRL
)
8669 /* <C-PageDown>: tab page forward */
8675 tpos
= curwin
->w_cursor
;
8676 if (onepage(FORWARD
, 1L) == OK
)
8691 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
8696 * Handle TAB in Insert or Replace mode.
8697 * Return TRUE when the TAB needs to be inserted like a normal character.
8706 if (Insstart_blank_vcol
== MAXCOL
&& curwin
->w_cursor
.lnum
== Insstart
.lnum
)
8707 Insstart_blank_vcol
= get_nolist_virtcol();
8708 if (echeck_abbr(TAB
+ ABBR_OFF
))
8714 can_cindent
= FALSE
;
8718 * When nothing special, insert TAB like a normal character
8721 && !(p_sta
&& ind
&& curbuf
->b_p_ts
!= curbuf
->b_p_sw
)
8722 && curbuf
->b_p_sts
== 0)
8725 if (stop_arrow() == FAIL
)
8729 #ifdef FEAT_SMARTINDENT
8732 can_si_back
= FALSE
;
8734 AppendToRedobuff((char_u
*)"\t");
8736 if (p_sta
&& ind
) /* insert tab in indent, use 'shiftwidth' */
8737 temp
= (int)curbuf
->b_p_sw
;
8738 else if (curbuf
->b_p_sts
> 0) /* use 'softtabstop' when set */
8739 temp
= (int)curbuf
->b_p_sts
;
8740 else /* otherwise use 'tabstop' */
8741 temp
= (int)curbuf
->b_p_ts
;
8742 temp
-= get_nolist_virtcol() % temp
;
8745 * Insert the first space with ins_char(). It will delete one char in
8746 * replace mode. Insert the rest with ins_str(); it will not delete any
8747 * chars. For VREPLACE mode, we use ins_char() for all characters.
8752 #ifdef FEAT_VREPLACE
8753 if (State
& VREPLACE_FLAG
)
8758 ins_str((char_u
*)" ");
8759 if (State
& REPLACE_FLAG
) /* no char replaced */
8765 * When 'expandtab' not set: Replace spaces by TABs where possible.
8767 if (!curbuf
->b_p_et
&& (curbuf
->b_p_sts
|| (p_sta
&& ind
)))
8770 #ifdef FEAT_VREPLACE
8771 char_u
*saved_line
= NULL
; /* init for GCC */
8776 colnr_T want_vcol
, vcol
;
8777 int change_col
= -1;
8778 int save_list
= curwin
->w_p_list
;
8781 * Get the current line. For VREPLACE mode, don't make real changes
8782 * yet, just work on a copy of the line.
8784 #ifdef FEAT_VREPLACE
8785 if (State
& VREPLACE_FLAG
)
8787 pos
= curwin
->w_cursor
;
8789 saved_line
= vim_strsave(ml_get_curline());
8790 if (saved_line
== NULL
)
8792 ptr
= saved_line
+ pos
.col
;
8797 ptr
= ml_get_cursor();
8798 cursor
= &curwin
->w_cursor
;
8801 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8802 if (vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
8803 curwin
->w_p_list
= FALSE
;
8805 /* Find first white before the cursor */
8806 fpos
= curwin
->w_cursor
;
8807 while (fpos
.col
> 0 && vim_iswhite(ptr
[-1]))
8813 /* In Replace mode, don't change characters before the insert point. */
8814 if ((State
& REPLACE_FLAG
)
8815 && fpos
.lnum
== Insstart
.lnum
8816 && fpos
.col
< Insstart
.col
)
8818 ptr
+= Insstart
.col
- fpos
.col
;
8819 fpos
.col
= Insstart
.col
;
8822 /* compute virtual column numbers of first white and cursor */
8823 getvcol(curwin
, &fpos
, &vcol
, NULL
, NULL
);
8824 getvcol(curwin
, cursor
, &want_vcol
, NULL
, NULL
);
8826 /* Use as many TABs as possible. Beware of 'showbreak' and
8827 * 'linebreak' adding extra virtual columns. */
8828 while (vim_iswhite(*ptr
))
8830 i
= lbr_chartabsize((char_u
*)"\t", vcol
);
8831 if (vcol
+ i
> want_vcol
)
8838 change_col
= fpos
.col
; /* Column of first change */
8839 /* May have to adjust Insstart */
8840 if (fpos
.lnum
== Insstart
.lnum
&& fpos
.col
< Insstart
.col
)
8841 Insstart
.col
= fpos
.col
;
8849 if (change_col
>= 0)
8853 /* Skip over the spaces we need. */
8854 while (vcol
< want_vcol
&& *ptr
== ' ')
8856 vcol
+= lbr_chartabsize(ptr
, vcol
);
8860 if (vcol
> want_vcol
)
8862 /* Must have a char with 'showbreak' just before it. */
8866 fpos
.col
+= repl_off
;
8868 /* Delete following spaces. */
8869 i
= cursor
->col
- fpos
.col
;
8872 mch_memmove(ptr
, ptr
+ i
, STRLEN(ptr
+ i
) + 1);
8873 /* correct replace stack. */
8874 if ((State
& REPLACE_FLAG
)
8875 #ifdef FEAT_VREPLACE
8876 && !(State
& VREPLACE_FLAG
)
8879 for (temp
= i
; --temp
>= 0; )
8880 replace_join(repl_off
);
8882 #ifdef FEAT_NETBEANS_INTG
8885 netbeans_removed(curbuf
, fpos
.lnum
, cursor
->col
,
8887 netbeans_inserted(curbuf
, fpos
.lnum
, cursor
->col
,
8893 #ifdef FEAT_VREPLACE
8895 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8896 * backspacing over the changed spacing and then inserting the new
8899 if (State
& VREPLACE_FLAG
)
8901 /* Backspace from real cursor to change_col */
8902 backspace_until_column(change_col
);
8904 /* Insert each char in saved_line from changed_col to
8906 ins_bytes_len(saved_line
+ change_col
,
8907 cursor
->col
- change_col
);
8912 #ifdef FEAT_VREPLACE
8913 if (State
& VREPLACE_FLAG
)
8914 vim_free(saved_line
);
8916 curwin
->w_p_list
= save_list
;
8923 * Handle CR or NL in insert mode.
8924 * Return TRUE when out of memory or can't undo.
8932 if (echeck_abbr(c
+ ABBR_OFF
))
8934 if (stop_arrow() == FAIL
)
8939 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8940 * character under the cursor. Only push a NUL on the replace stack,
8941 * nothing to put back when the NL is deleted.
8943 if ((State
& REPLACE_FLAG
)
8944 #ifdef FEAT_VREPLACE
8945 && !(State
& VREPLACE_FLAG
)
8951 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8952 * replacing the next line, so we push all of the characters left on the
8953 * line onto the replace stack. This is not done here though, it is done
8957 #ifdef FEAT_RIGHTLEFT
8959 if (p_altkeymap
&& p_fkmap
)
8962 /* NL in reverse insert will always start in the end of
8965 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
8968 AppendToRedobuff(NL_STR
);
8969 i
= open_line(FORWARD
,
8970 #ifdef FEAT_COMMENTS
8971 has_format_option(FO_RET_COMS
) ? OPENLINE_DO_COM
:
8982 #ifdef FEAT_DIGRAPHS
8984 * Handle digraph in insert mode.
8985 * Returns character still to be inserted, or NUL when nothing remaining to be
8994 pc_status
= PC_STATUS_UNSET
;
8995 if (redrawing() && !char_avail())
8997 /* may need to redraw when no more chars available now */
9000 edit_putchar('?', TRUE
);
9001 #ifdef FEAT_CMDL_INFO
9002 add_to_showcmd_c(Ctrl_K
);
9006 #ifdef USE_ON_FLY_SCROLL
9007 dont_scroll
= TRUE
; /* disallow scrolling here */
9010 /* don't map the digraph chars. This also prevents the
9011 * mode message to be deleted when ESC is hit */
9017 if (IS_SPECIAL(c
) || mod_mask
) /* special key */
9019 #ifdef FEAT_CMDL_INFO
9022 insert_special(c
, TRUE
, FALSE
);
9027 if (redrawing() && !char_avail())
9029 /* may need to redraw when no more chars available now */
9032 if (char2cells(c
) == 1)
9034 /* first remove the '?', otherwise it's restored when typing
9038 edit_putchar(c
, TRUE
);
9040 #ifdef FEAT_CMDL_INFO
9041 add_to_showcmd_c(c
);
9051 AppendToRedobuff((char_u
*)CTRL_V_STR
);
9052 c
= getdigraph(c
, cc
, TRUE
);
9053 #ifdef FEAT_CMDL_INFO
9060 #ifdef FEAT_CMDL_INFO
9068 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9069 * Returns the char to be inserted, or NUL if none found.
9077 char_u
*ptr
, *prev_ptr
;
9079 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
9085 /* try to advance to the cursor column */
9090 while ((colnr_T
)temp
< curwin
->w_virtcol
&& *ptr
!= NUL
)
9093 temp
+= lbr_chartabsize_adv(&ptr
, (colnr_T
)temp
);
9095 if ((colnr_T
)temp
> curwin
->w_virtcol
)
9099 c
= (*mb_ptr2char
)(ptr
);
9109 * CTRL-Y or CTRL-E typed in Insert mode.
9117 #ifdef FEAT_INS_EXPAND
9118 if (ctrl_x_mode
== CTRL_X_SCROLL
)
9124 redraw_later(VALID
);
9129 c
= ins_copychar(curwin
->w_cursor
.lnum
+ (c
== Ctrl_Y
? -1 : 1));
9134 /* The character must be taken literally, insert like it
9135 * was typed after a CTRL-V, and pretend 'textwidth'
9136 * wasn't set. Digits, 'o' and 'x' are special after a
9137 * CTRL-V, don't use it for these. */
9138 if (c
< 256 && !isalnum(c
))
9139 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
9140 tw_save
= curbuf
->b_p_tw
;
9141 curbuf
->b_p_tw
= -1;
9142 insert_special(c
, TRUE
, FALSE
);
9143 curbuf
->b_p_tw
= tw_save
;
9144 #ifdef FEAT_RIGHTLEFT
9148 c
= Ctrl_V
; /* pretend CTRL-V is last character */
9149 auto_format(FALSE
, TRUE
);
9155 #ifdef FEAT_SMARTINDENT
9157 * Try to do some very smart auto-indenting.
9158 * Used when inserting a "normal" character.
9164 pos_T
*pos
, old_pos
;
9170 * do some very smart indenting when entering '{' or '}'
9172 if (((did_si
|| can_si_back
) && c
== '{') || (can_si
&& c
== '}'))
9175 * for '}' set indent equal to indent of line containing matching '{'
9177 if (c
== '}' && (pos
= findmatch(NULL
, '{')) != NULL
)
9179 old_pos
= curwin
->w_cursor
;
9181 * If the matching '{' has a ')' immediately before it (ignoring
9182 * white-space), then line up with the start of the line
9183 * containing the matching '(' if there is one. This handles the
9184 * case where an "if (..\n..) {" statement continues over multiple
9187 ptr
= ml_get(pos
->lnum
);
9189 if (i
> 0) /* skip blanks before '{' */
9190 while (--i
> 0 && vim_iswhite(ptr
[i
]))
9192 curwin
->w_cursor
.lnum
= pos
->lnum
;
9193 curwin
->w_cursor
.col
= i
;
9194 if (ptr
[i
] == ')' && (pos
= findmatch(NULL
, '(')) != NULL
)
9195 curwin
->w_cursor
= *pos
;
9197 curwin
->w_cursor
= old_pos
;
9198 #ifdef FEAT_VREPLACE
9199 if (State
& VREPLACE_FLAG
)
9200 change_indent(INDENT_SET
, i
, FALSE
, NUL
);
9203 (void)set_indent(i
, SIN_CHANGED
);
9205 else if (curwin
->w_cursor
.col
> 0)
9208 * when inserting '{' after "O" reduce indent, but not
9209 * more than indent of previous line
9212 if (c
== '{' && can_si_back
&& curwin
->w_cursor
.lnum
> 1)
9214 old_pos
= curwin
->w_cursor
;
9216 while (curwin
->w_cursor
.lnum
> 1)
9218 ptr
= skipwhite(ml_get(--(curwin
->w_cursor
.lnum
)));
9220 /* ignore empty lines and lines starting with '#'. */
9221 if (*ptr
!= '#' && *ptr
!= NUL
)
9224 if (get_indent() >= i
)
9226 curwin
->w_cursor
= old_pos
;
9229 shift_line(TRUE
, FALSE
, 1);
9234 * set indent of '#' always to 0
9236 if (curwin
->w_cursor
.col
> 0 && can_si
&& c
== '#')
9238 /* remember current indent for next line */
9239 old_indent
= get_indent();
9240 (void)set_indent(0, SIN_CHANGED
);
9243 /* Adjust ai_col, the char at this position can be deleted. */
9244 if (ai_col
> curwin
->w_cursor
.col
)
9245 ai_col
= curwin
->w_cursor
.col
;
9250 * Get the value that w_virtcol would have when 'list' is off.
9251 * Unless 'cpo' contains the 'L' flag.
9254 get_nolist_virtcol()
9256 if (curwin
->w_p_list
&& vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9257 return getvcol_nolist(&curwin
->w_cursor
);
9259 return curwin
->w_virtcol
;