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 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 static int compl_was_interrupted
= FALSE
; /* didn't finish finding
111 static int compl_restarting
= FALSE
; /* don't insert match */
113 /* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
115 static int compl_started
= FALSE
;
117 static int compl_matches
= 0;
118 static char_u
*compl_pattern
= NULL
;
119 static int compl_direction
= FORWARD
;
120 static int compl_shows_dir
= FORWARD
;
121 static int compl_pending
= 0; /* > 1 for postponed CTRL-N */
122 static pos_T compl_startpos
;
123 static colnr_T compl_col
= 0; /* column where the text starts
124 * that is being completed */
125 static char_u
*compl_orig_text
= NULL
; /* text as it was before
126 * completion started */
127 static int compl_cont_mode
= 0;
128 static expand_T compl_xp
;
130 static void ins_ctrl_x
__ARGS((void));
131 static int has_compl_option
__ARGS((int dict_opt
));
132 static int ins_compl_accept_char
__ARGS((int c
));
133 static int ins_compl_add
__ARGS((char_u
*str
, int len
, int icase
, char_u
*fname
, char_u
**cptext
, int cdir
, int flags
, int adup
));
134 static int ins_compl_equal
__ARGS((compl_T
*match
, char_u
*str
, int len
));
135 static void ins_compl_longest_match
__ARGS((compl_T
*match
));
136 static void ins_compl_add_matches
__ARGS((int num_matches
, char_u
**matches
, int icase
));
137 static int ins_compl_make_cyclic
__ARGS((void));
138 static void ins_compl_upd_pum
__ARGS((void));
139 static void ins_compl_del_pum
__ARGS((void));
140 static int pum_wanted
__ARGS((void));
141 static int pum_enough_matches
__ARGS((void));
142 static void ins_compl_dictionaries
__ARGS((char_u
*dict
, char_u
*pat
, int flags
, int thesaurus
));
143 static void ins_compl_files
__ARGS((int count
, char_u
**files
, int thesaurus
, int flags
, regmatch_T
*regmatch
, char_u
*buf
, int *dir
));
144 static char_u
*find_line_end
__ARGS((char_u
*ptr
));
145 static void ins_compl_free
__ARGS((void));
146 static void ins_compl_clear
__ARGS((void));
147 static int ins_compl_bs
__ARGS((void));
148 static void ins_compl_new_leader
__ARGS((void));
149 static void ins_compl_addleader
__ARGS((int c
));
150 static int ins_compl_len
__ARGS((void));
151 static void ins_compl_restart
__ARGS((void));
152 static void ins_compl_set_original_text
__ARGS((char_u
*str
));
153 static void ins_compl_addfrommatch
__ARGS((void));
154 static int ins_compl_prep
__ARGS((int c
));
155 static buf_T
*ins_compl_next_buf
__ARGS((buf_T
*buf
, int flag
));
156 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
157 static void ins_compl_add_list
__ARGS((list_T
*list
));
159 static int ins_compl_get_exp
__ARGS((pos_T
*ini
));
160 static void ins_compl_delete
__ARGS((void));
161 static void ins_compl_insert
__ARGS((void));
162 static int ins_compl_next
__ARGS((int allow_get_expansion
, int count
, int insert_match
));
163 static int ins_compl_key2dir
__ARGS((int c
));
164 static int ins_compl_pum_key
__ARGS((int c
));
165 static int ins_compl_key2count
__ARGS((int c
));
166 static int ins_compl_use_match
__ARGS((int c
));
167 static int ins_complete
__ARGS((int c
));
168 static unsigned quote_meta
__ARGS((char_u
*dest
, char_u
*str
, int len
));
169 #endif /* FEAT_INS_EXPAND */
171 #define BACKSPACE_CHAR 1
172 #define BACKSPACE_WORD 2
173 #define BACKSPACE_WORD_NOT_SPACE 3
174 #define BACKSPACE_LINE 4
176 static void ins_redraw
__ARGS((int ready
));
177 static void ins_ctrl_v
__ARGS((void));
178 static void undisplay_dollar
__ARGS((void));
179 static void insert_special
__ARGS((int, int, int));
180 static void internal_format
__ARGS((int textwidth
, int second_indent
, int flags
, int format_only
));
181 static void check_auto_format
__ARGS((int));
182 static void redo_literal
__ARGS((int c
));
183 static void start_arrow
__ARGS((pos_T
*end_insert_pos
));
185 static void check_spell_redraw
__ARGS((void));
186 static void spell_back_to_badword
__ARGS((void));
187 static int spell_bad_len
= 0; /* length of located bad word */
189 static void stop_insert
__ARGS((pos_T
*end_insert_pos
, int esc
));
190 static int echeck_abbr
__ARGS((int));
192 static void replace_push_off
__ARGS((int c
));
194 static int replace_pop
__ARGS((void));
195 static void replace_join
__ARGS((int off
));
196 static void replace_pop_ins
__ARGS((void));
198 static void mb_replace_pop_ins
__ARGS((int cc
));
200 static void replace_flush
__ARGS((void));
201 static void replace_do_bs
__ARGS((int limit_col
));
202 static int del_char_after_col
__ARGS((int limit_col
));
204 static int cindent_on
__ARGS((void));
206 static void ins_reg
__ARGS((void));
207 static void ins_ctrl_g
__ARGS((void));
208 static void ins_ctrl_hat
__ARGS((void));
209 static int ins_esc
__ARGS((long *count
, int cmdchar
, int nomove
));
210 #ifdef FEAT_RIGHTLEFT
211 static void ins_ctrl_
__ARGS((void));
214 static int ins_start_select
__ARGS((int c
));
216 static void ins_insert
__ARGS((int replaceState
));
217 static void ins_ctrl_o
__ARGS((void));
218 static void ins_shift
__ARGS((int c
, int lastc
));
219 static void ins_del
__ARGS((void));
220 static int ins_bs
__ARGS((int c
, int mode
, int *inserted_space_p
));
222 static void ins_mouse
__ARGS((int c
));
223 static void ins_mousescroll
__ARGS((int up
));
225 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
226 static void ins_tabline
__ARGS((int c
));
228 static void ins_left
__ARGS((void));
229 static void ins_home
__ARGS((int c
));
230 static void ins_end
__ARGS((int c
));
231 static void ins_s_left
__ARGS((void));
232 static void ins_right
__ARGS((void));
233 static void ins_s_right
__ARGS((void));
234 static void ins_up
__ARGS((int startcol
));
235 static void ins_pageup
__ARGS((void));
236 static void ins_down
__ARGS((int startcol
));
237 static void ins_pagedown
__ARGS((void));
239 static void ins_drop
__ARGS((void));
241 static int ins_tab
__ARGS((void));
242 static int ins_eol
__ARGS((int c
));
244 static int ins_digraph
__ARGS((void));
246 static int ins_copychar
__ARGS((linenr_T lnum
));
247 static int ins_ctrl_ey
__ARGS((int tc
));
248 #ifdef FEAT_SMARTINDENT
249 static void ins_try_si
__ARGS((int c
));
251 static colnr_T get_nolist_virtcol
__ARGS((void));
253 static colnr_T Insstart_textlen
; /* length of line when insert started */
254 static colnr_T Insstart_blank_vcol
; /* vcol for first inserted blank */
256 static char_u
*last_insert
= NULL
; /* the text of the previous insert,
257 K_SPECIAL and CSI are escaped */
258 static int last_insert_skip
; /* nr of chars in front of previous insert */
259 static int new_insert_skip
; /* nr of chars in front of current insert */
260 static int did_restart_edit
; /* "restart_edit" when calling edit() */
263 static int can_cindent
; /* may do cindenting on this line */
266 static int old_indent
= 0; /* for ^^D command in insert mode */
268 #ifdef FEAT_RIGHTLEFT
269 static int revins_on
; /* reverse insert mode on */
270 static int revins_chars
; /* how much to skip after edit */
271 static int revins_legal
; /* was the last char 'legal'? */
272 static int revins_scol
; /* start column of revins session */
275 static int ins_need_undo
; /* call u_save() before inserting a
276 char. Set when edit() is called.
277 after that arrow_used is used. */
279 static int did_add_space
= FALSE
; /* auto_format() added an extra space
283 * edit(): Start inserting text.
286 * 'i' normal insert command
287 * 'a' normal append command
288 * 'R' replace command
289 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
290 * but still only one <CR> is inserted. The <Esc> is not used for redo.
292 * 'V' "gR" command for Virtual Replace mode.
293 * 'v' "gr" command for single character Virtual Replace mode.
295 * This function is not called recursively. For CTRL-O commands, it returns
296 * and lets the caller handle the Normal-mode command.
298 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
301 edit(cmdchar
, startln
, count
)
303 int startln
; /* if set, insert at start of line */
310 static linenr_T o_lnum
= 0;
312 int did_backspace
= TRUE
; /* previous char was backspace */
314 int line_is_white
= FALSE
; /* line is empty before insert */
316 linenr_T old_topline
= 0; /* topline before insertion */
318 int old_topfill
= -1;
320 int inserted_space
= FALSE
; /* just inserted a space */
321 int replaceState
= REPLACE
;
322 int nomove
= FALSE
; /* don't move cursor on return */
324 /* Remember whether editing was restarted after CTRL-O. */
325 did_restart_edit
= restart_edit
;
327 /* sleep before redrawing, needed for "CTRL-O :" that results in an
329 check_for_delay(TRUE
);
332 /* Don't allow inserting in the sandbox. */
339 /* Don't allow changes in the buffer while editing the cmdline. The
340 * caller of getcmdline() may get confused. */
347 #ifdef FEAT_INS_EXPAND
348 /* Don't allow recursive insert mode when busy with completion. */
349 if (compl_started
|| pum_visible())
354 ins_compl_clear(); /* clear stuff for CTRL-X mode */
359 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
361 if (cmdchar
!= 'r' && cmdchar
!= 'v')
366 else if (cmdchar
== 'V')
370 set_vim_var_string(VV_INSERTMODE
, ptr
, 1);
372 apply_autocmds(EVENT_INSERTENTER
, NULL
, NULL
, FALSE
, curbuf
);
378 * When doing a paste with the middle mouse button, Insstart is set to
379 * where the paste started.
381 if (where_paste_started
.lnum
!= 0)
382 Insstart
= where_paste_started
;
386 Insstart
= curwin
->w_cursor
;
390 Insstart_textlen
= (colnr_T
)linetabsize(ml_get_curline());
391 Insstart_blank_vcol
= MAXCOL
;
395 if (cmdchar
!= NUL
&& restart_edit
== 0)
398 AppendNumberToRedobuff(count
);
400 if (cmdchar
== 'V' || cmdchar
== 'v')
402 /* "gR" or "gr" command */
403 AppendCharToRedobuff('g');
404 AppendCharToRedobuff((cmdchar
== 'v') ? 'r' : 'R');
409 AppendCharToRedobuff(cmdchar
);
410 if (cmdchar
== 'g') /* "gI" command */
411 AppendCharToRedobuff('I');
412 else if (cmdchar
== 'r') /* "r<CR>" command */
413 count
= 1; /* insert only one <CR> */
423 EMSG(farsi_text_3
); /* encoded in Farsi */
431 else if (cmdchar
== 'V' || cmdchar
== 'v')
434 replaceState
= VREPLACE
;
435 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
436 vr_lines_changed
= 1;
442 stop_insert_mode
= FALSE
;
445 * Need to recompute the cursor position, it might move when the cursor is
446 * on a TAB or special character.
451 * Enable langmap or IME, indicated by 'iminsert'.
452 * Note that IME may enabled/disabled without us noticing here, thus the
453 * 'iminsert' value may not reflect what is actually used. It is updated
454 * when hitting <Esc>.
456 if (curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
458 #ifdef USE_IM_CONTROL
459 im_set_active(curbuf
->b_p_iminsert
== B_IMODE_IM
);
465 #ifdef FEAT_CMDL_INFO
468 #ifdef FEAT_RIGHTLEFT
469 /* there is no reverse replace mode */
470 revins_on
= (State
== INSERT
&& p_ri
);
479 * Handle restarting Insert mode.
480 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
481 * restart_edit non-zero, and something in the stuff buffer.
483 if (restart_edit
!= 0 && stuff_empty())
487 * After a paste we consider text typed to be part of the insert for
488 * the pasted text. You can backspace over the pasted text too.
490 if (where_paste_started
.lnum
)
498 * If the cursor was after the end-of-line before the CTRL-O and it is
499 * now at the end-of-line, put it after the end-of-line (this is not
500 * correct in very rare cases).
501 * Also do this if curswant is greater than the current virtual
502 * column. Eg after "^O$" or "^O80|".
506 if (((ins_at_eol
&& curwin
->w_cursor
.lnum
== o_lnum
)
507 || curwin
->w_curswant
> curwin
->w_virtcol
)
508 && *(ptr
= ml_get_curline() + curwin
->w_cursor
.col
) != NUL
)
511 ++curwin
->w_cursor
.col
;
515 i
= (*mb_ptr2len
)(ptr
);
517 curwin
->w_cursor
.col
+= i
;
526 /* we are in insert mode now, don't need to start it anymore */
527 need_start_insertmode
= FALSE
;
529 /* Need to save the line for undo before inserting the first char. */
530 ins_need_undo
= TRUE
;
533 where_paste_started
.lnum
= 0;
539 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
541 if (!p_im
&& did_restart_edit
== 0)
546 * If 'showmode' is set, show the current (insert/replace/..) mode.
547 * A warning message for changing a readonly file is given here, before
548 * actually changing anything. It's put after the mode, if any.
551 if (p_smd
&& msg_silent
== 0)
554 if (!p_im
&& did_restart_edit
== 0)
555 change_warning(i
== 0 ? 0 : i
+ 1);
558 ui_cursor_shape(); /* may show different cursor shape */
561 do_digraph(-1); /* clear digraphs */
565 * Get the current length of the redo buffer, those characters have to be
566 * skipped if we want to get to the inserted characters.
568 ptr
= get_inserted();
573 new_insert_skip
= (int)STRLEN(ptr
);
580 * Main loop in Insert mode: repeat until Insert mode is left.
584 #ifdef FEAT_RIGHTLEFT
586 revins_scol
= -1; /* reset on illegal motions */
590 if (arrow_used
) /* don't repeat insert when arrow key used */
593 if (stop_insert_mode
)
595 /* ":stopinsert" used or 'insertmode' reset */
600 /* set curwin->w_curswant for next K_DOWN or K_UP */
602 curwin
->w_set_curswant
= TRUE
;
604 /* If there is no typeahead may check for timestamps (e.g., for when a
605 * menu invoked a shell command). */
608 did_check_timestamps
= FALSE
;
609 if (need_check_timestamps
)
610 check_timestamps(FALSE
);
614 * When emsg() was called msg_scroll will have been set.
619 /* When 'mousefocus' is set a mouse movement may have taken us to
620 * another window. "need_mouse_correct" may then be set because of an
622 if (need_mouse_correct
)
627 /* Open fold at the cursor line, according to 'foldopen'. */
628 if (fdo_flags
& FDO_INSERT
)
630 /* Close folds where the cursor isn't, according to 'foldclose' */
636 * If we inserted a character at the last position of the last line in
637 * the window, scroll the window one line up. This avoids an extra
639 * This is detected when the cursor column is smaller after inserting
641 * Don't do this when the topline changed already, it has
642 * already been adjusted (by insertchar() calling open_line())).
644 if (curbuf
->b_mod_set
647 && curwin
->w_topline
== old_topline
649 && curwin
->w_topfill
== old_topfill
653 mincol
= curwin
->w_wcol
;
654 validate_cursor_col();
656 if ((int)curwin
->w_wcol
< mincol
- curbuf
->b_p_ts
657 && curwin
->w_wrow
== W_WINROW(curwin
)
658 + curwin
->w_height
- 1 - p_so
659 && (curwin
->w_cursor
.lnum
!= curwin
->w_topline
661 || curwin
->w_topfill
> 0
666 if (curwin
->w_topfill
> 0)
671 if (hasFolding(curwin
->w_topline
, NULL
, &old_topline
))
672 set_topline(curwin
, old_topline
+ 1);
675 set_topline(curwin
, curwin
->w_topline
+ 1);
679 /* May need to adjust w_topline to show the cursor. */
682 did_backspace
= FALSE
;
684 validate_cursor(); /* may set must_redraw */
687 * Redraw the display when no characters are waiting.
688 * Also shows mode, ruler and positions cursor.
692 #ifdef FEAT_SCROLLBIND
694 do_check_scrollbind(TRUE
);
698 old_topline
= curwin
->w_topline
;
700 old_topfill
= curwin
->w_topfill
;
703 #ifdef USE_ON_FLY_SCROLL
704 dont_scroll
= FALSE
; /* allow scrolling here */
708 * Get a character for Insert mode. Ignore K_IGNORE.
710 lastc
= c
; /* remember previous char for CTRL-D */
714 } while (c
== K_IGNORE
);
717 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
718 did_cursorhold
= TRUE
;
721 #ifdef FEAT_RIGHTLEFT
722 if (p_hkmap
&& KeyTyped
)
723 c
= hkmap(c
); /* Hebrew mode mapping */
726 if (p_fkmap
&& KeyTyped
)
727 c
= fkmap(c
); /* Farsi mode mapping */
730 #ifdef FEAT_INS_EXPAND
732 * Special handling of keys while the popup menu is visible or wanted
733 * and the cursor is still in the completed word. Only when there is
734 * a match, skip this when no matches were found.
738 && curwin
->w_cursor
.col
>= compl_col
739 && (compl_shown_match
== NULL
740 || compl_shown_match
!= compl_shown_match
->cp_next
))
742 /* BS: Delete one character from "compl_leader". */
743 if ((c
== K_BS
|| c
== Ctrl_H
)
744 && curwin
->w_cursor
.col
> compl_col
745 && (c
= ins_compl_bs()) == NUL
)
748 /* When no match was selected or it was edited. */
749 if (!compl_used_match
)
751 /* CTRL-L: Add one character from the current match to
752 * "compl_leader". Except when at the original match and
753 * there is nothing to add, CTRL-L works like CTRL-P then. */
755 && (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
756 || (int)STRLEN(compl_shown_match
->cp_str
)
757 > curwin
->w_cursor
.col
- compl_col
))
759 ins_compl_addfrommatch();
763 /* A non-white character that fits in with the current
764 * completion: Add to "compl_leader". */
765 if (ins_compl_accept_char(c
))
767 ins_compl_addleader(c
);
771 /* Pressing CTRL-Y selects the current match. When
772 * compl_enter_selects is set the Enter key does the same. */
773 if (c
== Ctrl_Y
|| (compl_enter_selects
774 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
782 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
783 * it does fix up the text when finishing completion. */
784 compl_get_longest
= FALSE
;
785 if (ins_compl_prep(c
))
789 /* CTRL-\ CTRL-N goes to Normal mode,
790 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
791 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
794 /* may need to redraw when no more chars available now */
801 if (c
!= Ctrl_N
&& c
!= Ctrl_G
&& c
!= Ctrl_O
)
803 /* it's something else */
807 else if (c
== Ctrl_G
&& p_im
)
814 ins_at_eol
= FALSE
; /* cursor keeps its column */
826 #ifdef FEAT_INS_EXPAND
827 if ((c
== Ctrl_V
|| c
== Ctrl_Q
) && ctrl_x_mode
== CTRL_X_CMDLINE
)
830 if (c
== Ctrl_V
|| c
== Ctrl_Q
)
833 c
= Ctrl_V
; /* pretend CTRL-V is last typed character */
839 # ifdef FEAT_INS_EXPAND
844 /* A key name preceded by a bang means this key is not to be
845 * inserted. Skip ahead to the re-indenting below.
846 * A key name preceded by a star means that indenting has to be
847 * done before inserting the key. */
848 line_is_white
= inindent(0);
849 if (in_cinkeys(c
, '!', line_is_white
))
851 if (can_cindent
&& in_cinkeys(c
, '*', line_is_white
)
852 && stop_arrow() == OK
)
857 #ifdef FEAT_RIGHTLEFT
861 case K_LEFT
: c
= K_RIGHT
; break;
862 case K_S_LEFT
: c
= K_S_RIGHT
; break;
863 case K_C_LEFT
: c
= K_C_RIGHT
; break;
864 case K_RIGHT
: c
= K_LEFT
; break;
865 case K_S_RIGHT
: c
= K_S_LEFT
; break;
866 case K_C_RIGHT
: c
= K_C_LEFT
; break;
872 * If 'keymodel' contains "startsel", may start selection. If it
873 * does, a CTRL-O and c will be stuffed, we need to get these
876 if (ins_start_select(c
))
881 * The big switch to handle a character in insert mode.
885 case ESC
: /* End input mode */
886 if (echeck_abbr(ESC
+ ABBR_OFF
))
890 case Ctrl_C
: /* End input mode */
892 if (c
== Ctrl_C
&& cmdwin_type
!= 0)
894 /* Close the cmdline window. */
895 cmdwin_result
= K_IGNORE
;
896 got_int
= FALSE
; /* don't stop executing autocommands et al. */
905 /* when 'insertmode' set, and not halfway a mapping, don't leave
911 (void)vgetc(); /* flush all buffers */
920 * This is the ONLY return from edit()!
922 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
923 * still puts the cursor back after the inserted text. */
924 if (ins_at_eol
&& gchar_cursor() == NUL
)
925 o_lnum
= curwin
->w_cursor
.lnum
;
927 if (ins_esc(&count
, cmdchar
, nomove
))
930 if (cmdchar
!= 'r' && cmdchar
!= 'v')
931 apply_autocmds(EVENT_INSERTLEAVE
, NULL
, NULL
,
933 did_cursorhold
= FALSE
;
935 return (c
== Ctrl_O
);
939 case Ctrl_Z
: /* suspend when 'insertmode' set */
941 goto normalchar
; /* insert CTRL-Z as normal char */
942 stuffReadbuff((char_u
*)":st\r");
946 case Ctrl_O
: /* execute one command */
947 #ifdef FEAT_COMPL_FUNC
948 if (ctrl_x_mode
== CTRL_X_OMNI
)
951 if (echeck_abbr(Ctrl_O
+ ABBR_OFF
))
955 #ifdef FEAT_VIRTUALEDIT
956 /* don't move the cursor left when 'virtualedit' has "onemore". */
957 if (ve_flags
& VE_ONEMORE
)
966 case K_INS
: /* toggle insert/replace mode */
968 ins_insert(replaceState
);
971 case K_SELECT
: /* end of Select mode mapping - ignore */
975 case K_SNIFF
: /* Sniff command received */
976 stuffcharReadbuff(K_SNIFF
);
980 case K_HELP
: /* Help key works like <ESC> <Help> */
983 stuffcharReadbuff(K_HELP
);
985 need_start_insertmode
= TRUE
;
988 #ifdef FEAT_NETBEANS_INTG
989 case K_F21
: /* NetBeans command */
990 ++no_mapping
; /* don't map the next key hits */
993 netbeans_keycommand(i
);
997 case K_ZERO
: /* Insert the previously inserted text. */
1000 /* For ^@ the trailing ESC will end the insert, unless there is an
1002 if (stuff_inserted(NUL
, 1L, (c
== Ctrl_A
)) == FAIL
1003 && c
!= Ctrl_A
&& !p_im
)
1004 goto doESCkey
; /* quit insert mode */
1005 inserted_space
= FALSE
;
1008 case Ctrl_R
: /* insert the contents of a register */
1010 auto_format(FALSE
, TRUE
);
1011 inserted_space
= FALSE
;
1014 case Ctrl_G
: /* commands starting with CTRL-G */
1018 case Ctrl_HAT
: /* switch input mode and/or langmap */
1022 #ifdef FEAT_RIGHTLEFT
1023 case Ctrl__
: /* switch between languages */
1030 case Ctrl_D
: /* Make indent one shiftwidth smaller. */
1031 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1032 if (ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
1037 case Ctrl_T
: /* Make indent one shiftwidth greater. */
1038 # ifdef FEAT_INS_EXPAND
1039 if (c
== Ctrl_T
&& ctrl_x_mode
== CTRL_X_THESAURUS
)
1041 if (has_compl_option(FALSE
))
1046 ins_shift(c
, lastc
);
1047 auto_format(FALSE
, TRUE
);
1048 inserted_space
= FALSE
;
1051 case K_DEL
: /* delete character under the cursor */
1054 auto_format(FALSE
, TRUE
);
1057 case K_BS
: /* delete character before the cursor */
1059 did_backspace
= ins_bs(c
, BACKSPACE_CHAR
, &inserted_space
);
1060 auto_format(FALSE
, TRUE
);
1063 case Ctrl_W
: /* delete word before the cursor */
1064 did_backspace
= ins_bs(c
, BACKSPACE_WORD
, &inserted_space
);
1065 auto_format(FALSE
, TRUE
);
1068 case Ctrl_U
: /* delete all inserted text in current line */
1069 # ifdef FEAT_COMPL_FUNC
1070 /* CTRL-X CTRL-U completes with 'completefunc'. */
1071 if (ctrl_x_mode
== CTRL_X_FUNCTION
)
1074 did_backspace
= ins_bs(c
, BACKSPACE_LINE
, &inserted_space
);
1075 auto_format(FALSE
, TRUE
);
1076 inserted_space
= FALSE
;
1080 case K_LEFTMOUSE
: /* mouse keys */
1081 case K_LEFTMOUSE_NM
:
1084 case K_LEFTRELEASE_NM
:
1087 case K_MIDDLERELEASE
:
1090 case K_RIGHTRELEASE
:
1100 case K_MOUSEDOWN
: /* Default action for scroll wheel up: scroll up */
1101 ins_mousescroll(FALSE
);
1104 case K_MOUSEUP
: /* Default action for scroll wheel down: scroll down */
1105 ins_mousescroll(TRUE
);
1108 #ifdef FEAT_GUI_TABLINE
1115 case K_IGNORE
: /* Something mapped to nothing */
1119 case K_CURSORHOLD
: /* Didn't type something for a while. */
1120 apply_autocmds(EVENT_CURSORHOLDI
, NULL
, NULL
, FALSE
, curbuf
);
1121 did_cursorhold
= TRUE
;
1126 /* On Win32 ignore <M-F4>, we get it when closing the window was
1129 if (mod_mask
!= MOD_MASK_ALT
)
1135 case K_VER_SCROLLBAR
:
1139 case K_HOR_SCROLLBAR
:
1144 case K_HOME
: /* <Home> */
1151 case K_END
: /* <End> */
1158 case K_LEFT
: /* <Left> */
1159 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1165 case K_S_LEFT
: /* <S-Left> */
1170 case K_RIGHT
: /* <Right> */
1171 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1177 case K_S_RIGHT
: /* <S-Right> */
1182 case K_UP
: /* <Up> */
1183 #ifdef FEAT_INS_EXPAND
1187 if (mod_mask
& MOD_MASK_SHIFT
)
1193 case K_S_UP
: /* <S-Up> */
1196 #ifdef FEAT_INS_EXPAND
1203 case K_DOWN
: /* <Down> */
1204 #ifdef FEAT_INS_EXPAND
1208 if (mod_mask
& MOD_MASK_SHIFT
)
1214 case K_S_DOWN
: /* <S-Down> */
1217 #ifdef FEAT_INS_EXPAND
1225 case K_DROP
: /* drag-n-drop event */
1230 case K_S_TAB
: /* When not mapped, use like a normal TAB */
1234 case TAB
: /* TAB or Complete patterns along path */
1235 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1236 if (ctrl_x_mode
== CTRL_X_PATH_PATTERNS
)
1239 inserted_space
= FALSE
;
1241 goto normalchar
; /* insert TAB as a normal char */
1242 auto_format(FALSE
, TRUE
);
1245 case K_KENTER
: /* <Enter> */
1250 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1251 /* In a quickfix window a <CR> jumps to the error under the
1253 if (bt_quickfix(curbuf
) && c
== CAR
)
1255 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
1256 do_cmdline_cmd((char_u
*)".cc");
1257 else /* location list window */
1258 do_cmdline_cmd((char_u
*)".ll");
1263 if (cmdwin_type
!= 0)
1265 /* Execute the command in the cmdline window. */
1266 cmdwin_result
= CAR
;
1270 if (ins_eol(c
) && !p_im
)
1271 goto doESCkey
; /* out of memory */
1272 auto_format(FALSE
, FALSE
);
1273 inserted_space
= FALSE
;
1276 #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
1277 case Ctrl_K
: /* digraph or keyword completion */
1278 # ifdef FEAT_INS_EXPAND
1279 if (ctrl_x_mode
== CTRL_X_DICTIONARY
)
1281 if (has_compl_option(TRUE
))
1286 # ifdef FEAT_DIGRAPHS
1294 #ifdef FEAT_INS_EXPAND
1295 case Ctrl_X
: /* Enter CTRL-X mode */
1299 case Ctrl_RSB
: /* Tag name completion after ^X */
1300 if (ctrl_x_mode
!= CTRL_X_TAGS
)
1304 case Ctrl_F
: /* File name completion after ^X */
1305 if (ctrl_x_mode
!= CTRL_X_FILES
)
1309 case 's': /* Spelling completion after ^X */
1311 if (ctrl_x_mode
!= CTRL_X_SPELL
)
1316 case Ctrl_L
: /* Whole line completion after ^X */
1317 #ifdef FEAT_INS_EXPAND
1318 if (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
1321 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1324 if (echeck_abbr(Ctrl_L
+ ABBR_OFF
))
1330 #ifdef FEAT_INS_EXPAND
1333 case Ctrl_P
: /* Do previous/next pattern completion */
1335 /* if 'complete' is empty then plain ^P is no longer special,
1336 * but it is under other ^X modes */
1337 if (*curbuf
->b_p_cpt
== NUL
1339 && !(compl_cont_status
& CONT_LOCAL
))
1343 if (ins_complete(c
) == FAIL
)
1344 compl_cont_status
= 0;
1346 #endif /* FEAT_INS_EXPAND */
1348 case Ctrl_Y
: /* copy from previous line or scroll down */
1349 case Ctrl_E
: /* copy from next line or scroll up */
1355 if (c
== intr_char
) /* special interrupt char */
1360 * Insert a nomal character.
1363 #ifdef FEAT_SMARTINDENT
1364 /* Try to perform smart-indenting. */
1370 inserted_space
= TRUE
;
1373 can_cindent
= FALSE
;
1375 if (Insstart_blank_vcol
== MAXCOL
1376 && curwin
->w_cursor
.lnum
== Insstart
.lnum
)
1377 Insstart_blank_vcol
= get_nolist_virtcol();
1380 if (vim_iswordc(c
) || !echeck_abbr(
1382 /* Add ABBR_OFF for characters above 0x100, this is
1383 * what check_abbr() expects. */
1384 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1388 insert_special(c
, FALSE
, FALSE
);
1389 #ifdef FEAT_RIGHTLEFT
1395 auto_format(FALSE
, TRUE
);
1398 /* When inserting a character the cursor line must never be in a
1403 } /* end of switch (c) */
1406 /* If typed something may trigger CursorHoldI again. */
1407 if (c
!= K_CURSORHOLD
)
1408 did_cursorhold
= FALSE
;
1411 /* If the cursor was moved we didn't just insert a space */
1413 inserted_space
= FALSE
;
1416 if (can_cindent
&& cindent_on()
1417 # ifdef FEAT_INS_EXPAND
1424 * Indent now if a key was typed that is in 'cinkeys'.
1426 if (in_cinkeys(c
, ' ', line_is_white
))
1428 if (stop_arrow() == OK
)
1429 /* re-indent the current line */
1433 #endif /* FEAT_CINDENT */
1440 * Redraw for Insert mode.
1441 * This is postponed until getting the next character to make '$' in the 'cpo'
1442 * option work correctly.
1443 * Only redraw when there are no characters available. This speeds up
1444 * inserting sequences of characters (e.g., for CTRL-R).
1448 int ready UNUSED
; /* not busy with something */
1453 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1454 * visible, the command might delete it. */
1455 if (ready
&& has_cursormovedI()
1456 && !equalpos(last_cursormoved
, curwin
->w_cursor
)
1457 # ifdef FEAT_INS_EXPAND
1463 /* Need to update the screen first, to make sure syntax
1464 * highlighting is correct after making a change (e.g., inserting
1465 * a "(". The autocommand may also require a redraw, so it's done
1466 * again below, unfortunately. */
1467 if (syntax_present(curbuf
) && must_redraw
)
1470 apply_autocmds(EVENT_CURSORMOVEDI
, NULL
, NULL
, FALSE
, curbuf
);
1471 last_cursormoved
= curwin
->w_cursor
;
1476 else if (clear_cmdline
|| redraw_cmdline
)
1477 showmode(); /* clear cmdline and show mode */
1480 emsg_on_display
= FALSE
; /* may remove error message now */
1485 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1492 /* may need to redraw when no more chars available now */
1495 if (redrawing() && !char_avail())
1496 edit_putchar('^', TRUE
);
1497 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
1499 #ifdef FEAT_CMDL_INFO
1500 add_to_showcmd_c(Ctrl_V
);
1504 #ifdef FEAT_CMDL_INFO
1507 insert_special(c
, FALSE
, TRUE
);
1508 #ifdef FEAT_RIGHTLEFT
1515 * Put a character directly onto the screen. It's not stored in a buffer.
1516 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1518 static int pc_status
;
1519 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1520 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1521 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1522 #define PC_STATUS_SET 3 /* pc_bytes was filled */
1524 static char_u pc_bytes
[MB_MAXBYTES
+ 1]; /* saved bytes */
1526 static char_u pc_bytes
[2]; /* saved bytes */
1533 edit_putchar(c
, highlight
)
1539 if (ScreenLines
!= NULL
)
1541 update_topline(); /* just in case w_topline isn't valid */
1544 attr
= hl_attr(HLF_8
);
1547 pc_row
= W_WINROW(curwin
) + curwin
->w_wrow
;
1548 pc_col
= W_WINCOL(curwin
);
1549 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1550 pc_status
= PC_STATUS_UNSET
;
1552 #ifdef FEAT_RIGHTLEFT
1555 pc_col
+= W_WIDTH(curwin
) - 1 - curwin
->w_wcol
;
1559 int fix_col
= mb_fix_col(pc_col
, pc_row
);
1561 if (fix_col
!= pc_col
)
1563 screen_putchar(' ', pc_row
, fix_col
, attr
);
1565 pc_status
= PC_STATUS_RIGHT
;
1573 pc_col
+= curwin
->w_wcol
;
1575 if (mb_lefthalve(pc_row
, pc_col
))
1576 pc_status
= PC_STATUS_LEFT
;
1580 /* save the character to be able to put it back */
1581 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1582 if (pc_status
== PC_STATUS_UNSET
)
1585 screen_getbytes(pc_row
, pc_col
, pc_bytes
, &pc_attr
);
1586 pc_status
= PC_STATUS_SET
;
1588 screen_putchar(c
, pc_row
, pc_col
, attr
);
1593 * Undo the previous edit_putchar().
1598 if (pc_status
!= PC_STATUS_UNSET
&& pc_row
>= msg_scrolled
)
1600 #if defined(FEAT_MBYTE)
1601 if (pc_status
== PC_STATUS_RIGHT
)
1603 if (pc_status
== PC_STATUS_RIGHT
|| pc_status
== PC_STATUS_LEFT
)
1604 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1607 screen_puts(pc_bytes
, pc_row
- msg_scrolled
, pc_col
, pc_attr
);
1612 * Called when p_dollar is set: display a '$' at the end of the changed text
1613 * Only works when cursor is in the line that changes.
1625 save_col
= curwin
->w_cursor
.col
;
1626 curwin
->w_cursor
.col
= col
;
1632 /* If on the last byte of a multi-byte move to the first byte. */
1633 p
= ml_get_curline();
1634 curwin
->w_cursor
.col
-= (*mb_head_off
)(p
, p
+ col
);
1637 curs_columns(FALSE
); /* recompute w_wrow and w_wcol */
1638 if (curwin
->w_wcol
< W_WIDTH(curwin
))
1640 edit_putchar('$', FALSE
);
1641 dollar_vcol
= curwin
->w_virtcol
;
1643 curwin
->w_cursor
.col
= save_col
;
1647 * Call this function before moving the cursor from the normal insert position
1656 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1661 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1662 * Keep the cursor on the same character.
1663 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1664 * type == INDENT_DEC decrease indent (for CTRL-D)
1665 * type == INDENT_SET set indent to "amount"
1666 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1669 change_indent(type
, amount
, round
, replaced
, call_changed_bytes
)
1673 int replaced
; /* replaced character, put on replace stack */
1674 int call_changed_bytes
; /* call changed_bytes() */
1678 int insstart_less
; /* reduction for Insstart.col */
1685 #ifdef FEAT_VREPLACE
1686 colnr_T orig_col
= 0; /* init for GCC */
1687 char_u
*new_line
, *orig_line
= NULL
; /* init for GCC */
1689 /* VREPLACE mode needs to know what the line was like before changing */
1690 if (State
& VREPLACE_FLAG
)
1692 orig_line
= vim_strsave(ml_get_curline()); /* Deal with NULL below */
1693 orig_col
= curwin
->w_cursor
.col
;
1697 /* for the following tricks we don't want list mode */
1698 save_p_list
= curwin
->w_p_list
;
1699 curwin
->w_p_list
= FALSE
;
1700 vc
= getvcol_nolist(&curwin
->w_cursor
);
1704 * For Replace mode we need to fix the replace stack later, which is only
1705 * possible when the cursor is in the indent. Remember the number of
1706 * characters before the cursor if it's possible.
1708 start_col
= curwin
->w_cursor
.col
;
1710 /* determine offset from first non-blank */
1711 new_cursor_col
= curwin
->w_cursor
.col
;
1712 beginline(BL_WHITE
);
1713 new_cursor_col
-= curwin
->w_cursor
.col
;
1715 insstart_less
= curwin
->w_cursor
.col
;
1718 * If the cursor is in the indent, compute how many screen columns the
1719 * cursor is to the left of the first non-blank.
1721 if (new_cursor_col
< 0)
1722 vcol
= get_indent() - vcol
;
1724 if (new_cursor_col
> 0) /* can't fix replace stack */
1728 * Set the new indent. The cursor will be put on the first non-blank.
1730 if (type
== INDENT_SET
)
1731 (void)set_indent(amount
, call_changed_bytes
? SIN_CHANGED
: 0);
1734 #ifdef FEAT_VREPLACE
1735 int save_State
= State
;
1737 /* Avoid being called recursively. */
1738 if (State
& VREPLACE_FLAG
)
1741 shift_line(type
== INDENT_DEC
, round
, 1, call_changed_bytes
);
1742 #ifdef FEAT_VREPLACE
1746 insstart_less
-= curwin
->w_cursor
.col
;
1749 * Try to put cursor on same character.
1750 * If the cursor is at or after the first non-blank in the line,
1751 * compute the cursor column relative to the column of the first
1752 * non-blank character.
1753 * If we are not in insert mode, leave the cursor on the first non-blank.
1754 * If the cursor is before the first non-blank, position it relative
1755 * to the first non-blank, counted in screen columns.
1757 if (new_cursor_col
>= 0)
1760 * When changing the indent while the cursor is touching it, reset
1761 * Insstart_col to 0.
1763 if (new_cursor_col
== 0)
1764 insstart_less
= MAXCOL
;
1765 new_cursor_col
+= curwin
->w_cursor
.col
;
1767 else if (!(State
& INSERT
))
1768 new_cursor_col
= curwin
->w_cursor
.col
;
1772 * Compute the screen column where the cursor should be.
1774 vcol
= get_indent() - vcol
;
1775 curwin
->w_virtcol
= (colnr_T
)((vcol
< 0) ? 0 : vcol
);
1778 * Advance the cursor until we reach the right screen column.
1780 vcol
= last_vcol
= 0;
1781 new_cursor_col
= -1;
1782 ptr
= ml_get_curline();
1783 while (vcol
<= (int)curwin
->w_virtcol
)
1787 if (has_mbyte
&& new_cursor_col
>= 0)
1788 new_cursor_col
+= (*mb_ptr2len
)(ptr
+ new_cursor_col
);
1792 vcol
+= lbr_chartabsize(ptr
+ new_cursor_col
, (colnr_T
)vcol
);
1797 * May need to insert spaces to be able to position the cursor on
1798 * the right screen column.
1800 if (vcol
!= (int)curwin
->w_virtcol
)
1802 curwin
->w_cursor
.col
= (colnr_T
)new_cursor_col
;
1803 i
= (int)curwin
->w_virtcol
- vcol
;
1804 ptr
= alloc((unsigned)(i
+ 1));
1807 new_cursor_col
+= i
;
1817 * When changing the indent while the cursor is in it, reset
1818 * Insstart_col to 0.
1820 insstart_less
= MAXCOL
;
1823 curwin
->w_p_list
= save_p_list
;
1825 if (new_cursor_col
<= 0)
1826 curwin
->w_cursor
.col
= 0;
1828 curwin
->w_cursor
.col
= (colnr_T
)new_cursor_col
;
1829 curwin
->w_set_curswant
= TRUE
;
1830 changed_cline_bef_curs();
1833 * May have to adjust the start of the insert.
1837 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
&& Insstart
.col
!= 0)
1839 if ((int)Insstart
.col
<= insstart_less
)
1842 Insstart
.col
-= insstart_less
;
1844 if ((int)ai_col
<= insstart_less
)
1847 ai_col
-= insstart_less
;
1851 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1852 * If the number of characters before the cursor decreased, need to pop a
1853 * few characters from the replace stack.
1854 * If the number of characters before the cursor increased, need to push a
1855 * few NULs onto the replace stack.
1857 if (REPLACE_NORMAL(State
) && start_col
>= 0)
1859 while (start_col
> (int)curwin
->w_cursor
.col
)
1861 replace_join(0); /* remove a NUL from the replace stack */
1864 while (start_col
< (int)curwin
->w_cursor
.col
|| replaced
)
1869 replace_push(replaced
);
1876 #ifdef FEAT_VREPLACE
1878 * For VREPLACE mode, we also have to fix the replace stack. In this case
1879 * it is always possible because we backspace over the whole line and then
1880 * put it back again the way we wanted it.
1882 if (State
& VREPLACE_FLAG
)
1884 /* If orig_line didn't allocate, just return. At least we did the job,
1885 * even if you can't backspace. */
1886 if (orig_line
== NULL
)
1890 new_line
= vim_strsave(ml_get_curline());
1891 if (new_line
== NULL
)
1894 /* We only put back the new line up to the cursor */
1895 new_line
[curwin
->w_cursor
.col
] = NUL
;
1897 /* Put back original line */
1898 ml_replace(curwin
->w_cursor
.lnum
, orig_line
, FALSE
);
1899 curwin
->w_cursor
.col
= orig_col
;
1901 /* Backspace from cursor to start of line */
1902 backspace_until_column(0);
1904 /* Insert new stuff into line again */
1905 ins_bytes(new_line
);
1913 * Truncate the space at the end of a line. This is to be used only in an
1914 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1918 truncate_spaces(line
)
1923 /* find start of trailing white space */
1924 for (i
= (int)STRLEN(line
) - 1; i
>= 0 && vim_iswhite(line
[i
]); i
--)
1926 if (State
& REPLACE_FLAG
)
1927 replace_join(0); /* remove a NUL from the replace stack */
1932 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1933 || defined(FEAT_COMMENTS) || defined(PROTO)
1935 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1936 * modes correctly. May also be used when not in insert mode at all.
1937 * Will attempt not to go before "col" even when there is a composing
1941 backspace_until_column(col
)
1944 while ((int)curwin
->w_cursor
.col
> col
)
1946 curwin
->w_cursor
.col
--;
1947 if (State
& REPLACE_FLAG
)
1949 else if (!del_char_after_col(col
))
1956 * Like del_char(), but make sure not to go before column "limit_col".
1957 * Only matters when there are composing characters.
1958 * Return TRUE when something was deleted.
1961 del_char_after_col(limit_col
)
1962 int limit_col UNUSED
;
1965 if (enc_utf8
&& limit_col
>= 0)
1967 colnr_T ecol
= curwin
->w_cursor
.col
+ 1;
1969 /* Make sure the cursor is at the start of a character, but
1970 * skip forward again when going too far back because of a
1971 * composing character. */
1973 while (curwin
->w_cursor
.col
< (colnr_T
)limit_col
)
1975 int l
= utf_ptr2len(ml_get_cursor());
1977 if (l
== 0) /* end of line */
1979 curwin
->w_cursor
.col
+= l
;
1981 if (*ml_get_cursor() == NUL
|| curwin
->w_cursor
.col
== ecol
)
1983 del_bytes((long)((int)ecol
- curwin
->w_cursor
.col
), FALSE
, TRUE
);
1987 (void)del_char(FALSE
);
1991 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1993 * CTRL-X pressed in Insert mode.
1998 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1999 * CTRL-V works like CTRL-N */
2000 if (ctrl_x_mode
!= CTRL_X_CMDLINE
)
2002 /* if the next ^X<> won't ADD nothing, then reset
2003 * compl_cont_status */
2004 if (compl_cont_status
& CONT_N_ADDS
)
2005 compl_cont_status
|= CONT_INTRPT
;
2007 compl_cont_status
= 0;
2008 /* We're not sure which CTRL-X mode it will be yet */
2009 ctrl_x_mode
= CTRL_X_NOT_DEFINED_YET
;
2010 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
2011 edit_submode_pre
= NULL
;
2017 * Return TRUE if the 'dict' or 'tsr' option can be used.
2020 has_compl_option(dict_opt
)
2023 if (dict_opt
? (*curbuf
->b_p_dict
== NUL
&& *p_dict
== NUL
2025 && !curwin
->w_p_spell
2028 : (*curbuf
->b_p_tsr
== NUL
&& *p_tsr
== NUL
))
2031 edit_submode
= NULL
;
2032 msg_attr(dict_opt
? (char_u
*)_("'dictionary' option is empty")
2033 : (char_u
*)_("'thesaurus' option is empty"),
2035 if (emsg_silent
== 0)
2040 ui_delay(2000L, FALSE
);
2048 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2049 * This depends on the current mode.
2052 vim_is_ctrl_x_key(c
)
2055 /* Always allow ^R - let it's results then be checked */
2059 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
2060 if (ins_compl_pum_key(c
))
2063 switch (ctrl_x_mode
)
2065 case 0: /* Not in any CTRL-X mode */
2066 return (c
== Ctrl_N
|| c
== Ctrl_P
|| c
== Ctrl_X
);
2067 case CTRL_X_NOT_DEFINED_YET
:
2068 return ( c
== Ctrl_X
|| c
== Ctrl_Y
|| c
== Ctrl_E
2069 || c
== Ctrl_L
|| c
== Ctrl_F
|| c
== Ctrl_RSB
2070 || c
== Ctrl_I
|| c
== Ctrl_D
|| c
== Ctrl_P
2071 || c
== Ctrl_N
|| c
== Ctrl_T
|| c
== Ctrl_V
2072 || c
== Ctrl_Q
|| c
== Ctrl_U
|| c
== Ctrl_O
2073 || c
== Ctrl_S
|| c
== 's');
2075 return (c
== Ctrl_Y
|| c
== Ctrl_E
);
2076 case CTRL_X_WHOLE_LINE
:
2077 return (c
== Ctrl_L
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2079 return (c
== Ctrl_F
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2080 case CTRL_X_DICTIONARY
:
2081 return (c
== Ctrl_K
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2082 case CTRL_X_THESAURUS
:
2083 return (c
== Ctrl_T
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2085 return (c
== Ctrl_RSB
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2087 case CTRL_X_PATH_PATTERNS
:
2088 return (c
== Ctrl_P
|| c
== Ctrl_N
);
2089 case CTRL_X_PATH_DEFINES
:
2090 return (c
== Ctrl_D
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2092 case CTRL_X_CMDLINE
:
2093 return (c
== Ctrl_V
|| c
== Ctrl_Q
|| c
== Ctrl_P
|| c
== Ctrl_N
2095 #ifdef FEAT_COMPL_FUNC
2096 case CTRL_X_FUNCTION
:
2097 return (c
== Ctrl_U
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2099 return (c
== Ctrl_O
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2102 return (c
== Ctrl_S
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2104 EMSG(_(e_internal
));
2109 * Return TRUE when character "c" is part of the item currently being
2110 * completed. Used to decide whether to abandon complete mode when the menu
2114 ins_compl_accept_char(c
)
2117 if (ctrl_x_mode
& CTRL_X_WANT_IDENT
)
2118 /* When expanding an identifier only accept identifier chars. */
2119 return vim_isIDc(c
);
2121 switch (ctrl_x_mode
)
2124 /* When expanding file name only accept file name chars. But not
2125 * path separators, so that "proto/<Tab>" expands files in
2126 * "proto", not "proto/" as a whole */
2127 return vim_isfilec(c
) && !vim_ispathsep(c
);
2129 case CTRL_X_CMDLINE
:
2131 /* Command line and Omni completion can work with just about any
2132 * printable character, but do stop at white space. */
2133 return vim_isprintc(c
) && !vim_iswhite(c
);
2135 case CTRL_X_WHOLE_LINE
:
2136 /* For while line completion a space can be part of the line. */
2137 return vim_isprintc(c
);
2139 return vim_iswordc(c
);
2143 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
2144 * case of the originally typed text is used, and the case of the completed
2145 * text is inferred, ie this tries to work out what case you probably wanted
2146 * the rest of the word to be in -- webb
2149 ins_compl_add_infercase(str
, len
, icase
, fname
, dir
, flags
)
2159 int actual_len
; /* Take multi-byte characters */
2160 int actual_compl_length
; /* into account. */
2161 int *wca
; /* Wide character array. */
2162 int has_lower
= FALSE
;
2163 int was_letter
= FALSE
;
2165 if (p_ic
&& curbuf
->b_p_inf
&& len
> 0)
2167 /* Infer case of completed part. */
2169 /* Find actual length of completion. */
2185 /* Find actual length of original text. */
2189 p
= compl_orig_text
;
2190 actual_compl_length
= 0;
2194 ++actual_compl_length
;
2199 actual_compl_length
= compl_length
;
2201 /* Allocate wide character array for the completion and fill it. */
2202 wca
= (int *)alloc((unsigned)(actual_len
* sizeof(int)));
2206 for (i
= 0; i
< actual_len
; ++i
)
2209 wca
[i
] = mb_ptr2char_adv(&p
);
2214 /* Rule 1: Were any chars converted to lower? */
2215 p
= compl_orig_text
;
2216 for (i
= 0; i
< actual_compl_length
; ++i
)
2220 c
= mb_ptr2char_adv(&p
);
2227 if (MB_ISUPPER(wca
[i
]))
2229 /* Rule 1 is satisfied. */
2230 for (i
= actual_compl_length
; i
< actual_len
; ++i
)
2231 wca
[i
] = MB_TOLOWER(wca
[i
]);
2238 * Rule 2: No lower case, 2nd consecutive letter converted to
2243 p
= compl_orig_text
;
2244 for (i
= 0; i
< actual_compl_length
; ++i
)
2248 c
= mb_ptr2char_adv(&p
);
2252 if (was_letter
&& MB_ISUPPER(c
) && MB_ISLOWER(wca
[i
]))
2254 /* Rule 2 is satisfied. */
2255 for (i
= actual_compl_length
; i
< actual_len
; ++i
)
2256 wca
[i
] = MB_TOUPPER(wca
[i
]);
2259 was_letter
= MB_ISLOWER(c
) || MB_ISUPPER(c
);
2263 /* Copy the original case of the part we typed. */
2264 p
= compl_orig_text
;
2265 for (i
= 0; i
< actual_compl_length
; ++i
)
2269 c
= mb_ptr2char_adv(&p
);
2274 wca
[i
] = MB_TOLOWER(wca
[i
]);
2275 else if (MB_ISUPPER(c
))
2276 wca
[i
] = MB_TOUPPER(wca
[i
]);
2280 * Generate encoding specific output from wide character array.
2281 * Multi-byte characters can occupy up to five bytes more than
2282 * ASCII characters, and we also need one byte for NUL, so stay
2283 * six bytes away from the edge of IObuff.
2287 while (i
< actual_len
&& (p
- IObuff
+ 6) < IOSIZE
)
2290 p
+= (*mb_char2bytes
)(wca
[i
++], p
);
2299 return ins_compl_add(IObuff
, len
, icase
, fname
, NULL
, dir
,
2302 return ins_compl_add(str
, len
, icase
, fname
, NULL
, dir
, flags
, FALSE
);
2306 * Add a match to the list of matches.
2307 * If the given string is already in the list of completions, then return
2308 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2309 * maybe because alloc() returns NULL, then FAIL is returned.
2312 ins_compl_add(str
, len
, icase
, fname
, cptext
, cdir
, flags
, adup
)
2317 char_u
**cptext
; /* extra text for popup menu or NULL */
2320 int adup
; /* accept duplicate match */
2323 int dir
= (cdir
== 0 ? compl_direction
: cdir
);
2329 len
= (int)STRLEN(str
);
2332 * If the same match is already present, don't add it.
2334 if (compl_first_match
!= NULL
&& !adup
)
2336 match
= compl_first_match
;
2339 if ( !(match
->cp_flags
& ORIGINAL_TEXT
)
2340 && STRNCMP(match
->cp_str
, str
, len
) == 0
2341 && match
->cp_str
[len
] == NUL
)
2343 match
= match
->cp_next
;
2344 } while (match
!= NULL
&& match
!= compl_first_match
);
2347 /* Remove any popup menu before changing the list of matches. */
2348 ins_compl_del_pum();
2351 * Allocate a new match structure.
2352 * Copy the values to the new match structure.
2354 match
= (compl_T
*)alloc_clear((unsigned)sizeof(compl_T
));
2357 match
->cp_number
= -1;
2358 if (flags
& ORIGINAL_TEXT
)
2359 match
->cp_number
= 0;
2360 if ((match
->cp_str
= vim_strnsave(str
, len
)) == NULL
)
2365 match
->cp_icase
= icase
;
2368 * - compl_curr_match->cp_fname if it is a string equal to fname.
2369 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2370 * - NULL otherwise. --Acevedo */
2372 && compl_curr_match
!= NULL
2373 && compl_curr_match
->cp_fname
!= NULL
2374 && STRCMP(fname
, compl_curr_match
->cp_fname
) == 0)
2375 match
->cp_fname
= compl_curr_match
->cp_fname
;
2376 else if (fname
!= NULL
)
2378 match
->cp_fname
= vim_strsave(fname
);
2379 flags
|= FREE_FNAME
;
2382 match
->cp_fname
= NULL
;
2383 match
->cp_flags
= flags
;
2389 for (i
= 0; i
< CPT_COUNT
; ++i
)
2390 if (cptext
[i
] != NULL
&& *cptext
[i
] != NUL
)
2391 match
->cp_text
[i
] = vim_strsave(cptext
[i
]);
2395 * Link the new match structure in the list of matches.
2397 if (compl_first_match
== NULL
)
2398 match
->cp_next
= match
->cp_prev
= NULL
;
2399 else if (dir
== FORWARD
)
2401 match
->cp_next
= compl_curr_match
->cp_next
;
2402 match
->cp_prev
= compl_curr_match
;
2406 match
->cp_next
= compl_curr_match
;
2407 match
->cp_prev
= compl_curr_match
->cp_prev
;
2410 match
->cp_next
->cp_prev
= match
;
2412 match
->cp_prev
->cp_next
= match
;
2413 else /* if there's nothing before, it is the first match */
2414 compl_first_match
= match
;
2415 compl_curr_match
= match
;
2418 * Find the longest common string if still doing that.
2420 if (compl_get_longest
&& (flags
& ORIGINAL_TEXT
) == 0)
2421 ins_compl_longest_match(match
);
2427 * Return TRUE if "str[len]" matches with match->cp_str, considering
2431 ins_compl_equal(match
, str
, len
)
2436 if (match
->cp_icase
)
2437 return STRNICMP(match
->cp_str
, str
, (size_t)len
) == 0;
2438 return STRNCMP(match
->cp_str
, str
, (size_t)len
) == 0;
2442 * Reduce the longest common string for match "match".
2445 ins_compl_longest_match(match
)
2452 if (compl_leader
== NULL
)
2454 /* First match, use it as a whole. */
2455 compl_leader
= vim_strsave(match
->cp_str
);
2456 if (compl_leader
!= NULL
)
2458 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2460 ins_bytes(compl_leader
+ ins_compl_len());
2463 /* When the match isn't there (to avoid matching itself) remove it
2464 * again after redrawing. */
2467 compl_used_match
= FALSE
;
2472 /* Reduce the text if this match differs from compl_leader. */
2480 c1
= mb_ptr2char(p
);
2481 c2
= mb_ptr2char(s
);
2489 if (match
->cp_icase
? (MB_TOLOWER(c1
) != MB_TOLOWER(c2
))
2508 /* Leader was shortened, need to change the inserted text. */
2510 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2512 ins_bytes(compl_leader
+ ins_compl_len());
2515 /* When the match isn't there (to avoid matching itself) remove it
2516 * again after redrawing. */
2521 compl_used_match
= FALSE
;
2526 * Add an array of matches to the list of matches.
2530 ins_compl_add_matches(num_matches
, matches
, icase
)
2537 int dir
= compl_direction
;
2539 for (i
= 0; i
< num_matches
&& add_r
!= FAIL
; i
++)
2540 if ((add_r
= ins_compl_add(matches
[i
], -1, icase
,
2541 NULL
, NULL
, dir
, 0, FALSE
)) == OK
)
2542 /* if dir was BACKWARD then honor it just once */
2544 FreeWild(num_matches
, matches
);
2547 /* Make the completion list cyclic.
2548 * Return the number of matches (excluding the original).
2551 ins_compl_make_cyclic()
2556 if (compl_first_match
!= NULL
)
2559 * Find the end of the list.
2561 match
= compl_first_match
;
2562 /* there's always an entry for the compl_orig_text, it doesn't count. */
2563 while (match
->cp_next
!= NULL
&& match
->cp_next
!= compl_first_match
)
2565 match
= match
->cp_next
;
2568 match
->cp_next
= compl_first_match
;
2569 compl_first_match
->cp_prev
= match
;
2575 * Start completion for the complete() function.
2576 * "startcol" is where the matched text starts (1 is first column).
2577 * "list" is the list of matches.
2580 set_completion(startcol
, list
)
2584 /* If already doing completions stop it. */
2585 if (ctrl_x_mode
!= 0)
2586 ins_compl_prep(' ');
2589 if (stop_arrow() == FAIL
)
2592 if (startcol
> curwin
->w_cursor
.col
)
2593 startcol
= curwin
->w_cursor
.col
;
2594 compl_col
= startcol
;
2595 compl_length
= (int)curwin
->w_cursor
.col
- (int)startcol
;
2596 /* compl_pattern doesn't need to be set */
2597 compl_orig_text
= vim_strnsave(ml_get_curline() + compl_col
, compl_length
);
2598 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
2599 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
2602 /* Handle like dictionary completion. */
2603 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
2605 ins_compl_add_list(list
);
2606 compl_matches
= ins_compl_make_cyclic();
2607 compl_started
= TRUE
;
2608 compl_used_match
= TRUE
;
2609 compl_cont_status
= 0;
2611 compl_curr_match
= compl_first_match
;
2612 ins_complete(Ctrl_N
);
2617 /* "compl_match_array" points the currently displayed list of entries in the
2618 * popup menu. It is NULL when there is no popup menu. */
2619 static pumitem_T
*compl_match_array
= NULL
;
2620 static int compl_match_arraysize
;
2623 * Update the screen and when there is any scrolling remove the popup menu.
2630 if (compl_match_array
!= NULL
)
2632 h
= curwin
->w_cline_height
;
2634 if (h
!= curwin
->w_cline_height
)
2635 ins_compl_del_pum();
2640 * Remove any popup menu.
2645 if (compl_match_array
!= NULL
)
2648 vim_free(compl_match_array
);
2649 compl_match_array
= NULL
;
2654 * Return TRUE if the popup menu should be displayed.
2659 /* 'completeopt' must contain "menu" or "menuone" */
2660 if (vim_strchr(p_cot
, 'm') == NULL
)
2663 /* The display looks bad on a B&W display. */
2674 * Return TRUE if there are two or more matches to be shown in the popup menu.
2675 * One if 'completopt' contains "menuone".
2678 pum_enough_matches()
2683 /* Don't display the popup menu if there are no matches or there is only
2684 * one (ignoring the original text). */
2685 compl = compl_first_match
;
2690 || ((compl->cp_flags
& ORIGINAL_TEXT
) == 0 && ++i
== 2))
2692 compl = compl->cp_next
;
2693 } while (compl != compl_first_match
);
2695 if (strstr((char *)p_cot
, "menuone") != NULL
)
2701 * Show the popup menu for the list of matches.
2702 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
2705 ins_compl_show_pum()
2708 compl_T
*shown_compl
= NULL
;
2709 int did_find_shown_match
= FALSE
;
2710 int shown_match_ok
= FALSE
;
2716 if (!pum_wanted() || !pum_enough_matches())
2719 #if defined(FEAT_EVAL)
2720 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2721 do_cmdline_cmd((char_u
*)"if exists('g:loaded_matchparen')|3match none|endif");
2724 /* Update the screen before drawing the popup menu over it. */
2727 if (compl_match_array
== NULL
)
2729 /* Need to build the popup menu list. */
2730 compl_match_arraysize
= 0;
2731 compl = compl_first_match
;
2732 if (compl_leader
!= NULL
)
2733 lead_len
= (int)STRLEN(compl_leader
);
2736 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2737 && (compl_leader
== NULL
2738 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2739 ++compl_match_arraysize
;
2740 compl = compl->cp_next
;
2741 } while (compl != NULL
&& compl != compl_first_match
);
2742 if (compl_match_arraysize
== 0)
2744 compl_match_array
= (pumitem_T
*)alloc_clear(
2745 (unsigned)(sizeof(pumitem_T
)
2746 * compl_match_arraysize
));
2747 if (compl_match_array
!= NULL
)
2749 /* If the current match is the original text don't find the first
2750 * match after it, don't highlight anything. */
2751 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
2752 shown_match_ok
= TRUE
;
2755 compl = compl_first_match
;
2758 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2759 && (compl_leader
== NULL
2760 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2762 if (!shown_match_ok
)
2764 if (compl == compl_shown_match
|| did_find_shown_match
)
2766 /* This item is the shown match or this is the
2767 * first displayed item after the shown match. */
2768 compl_shown_match
= compl;
2769 did_find_shown_match
= TRUE
;
2770 shown_match_ok
= TRUE
;
2773 /* Remember this displayed match for when the
2774 * shown match is just below it. */
2775 shown_compl
= compl;
2779 if (compl->cp_text
[CPT_ABBR
] != NULL
)
2780 compl_match_array
[i
].pum_text
=
2781 compl->cp_text
[CPT_ABBR
];
2783 compl_match_array
[i
].pum_text
= compl->cp_str
;
2784 compl_match_array
[i
].pum_kind
= compl->cp_text
[CPT_KIND
];
2785 compl_match_array
[i
].pum_info
= compl->cp_text
[CPT_INFO
];
2786 if (compl->cp_text
[CPT_MENU
] != NULL
)
2787 compl_match_array
[i
++].pum_extra
=
2788 compl->cp_text
[CPT_MENU
];
2790 compl_match_array
[i
++].pum_extra
= compl->cp_fname
;
2793 if (compl == compl_shown_match
)
2795 did_find_shown_match
= TRUE
;
2797 /* When the original text is the shown match don't set
2798 * compl_shown_match. */
2799 if (compl->cp_flags
& ORIGINAL_TEXT
)
2800 shown_match_ok
= TRUE
;
2802 if (!shown_match_ok
&& shown_compl
!= NULL
)
2804 /* The shown match isn't displayed, set it to the
2805 * previously displayed match. */
2806 compl_shown_match
= shown_compl
;
2807 shown_match_ok
= TRUE
;
2810 compl = compl->cp_next
;
2811 } while (compl != NULL
&& compl != compl_first_match
);
2813 if (!shown_match_ok
) /* no displayed match at all */
2819 /* popup menu already exists, only need to find the current item.*/
2820 for (i
= 0; i
< compl_match_arraysize
; ++i
)
2821 if (compl_match_array
[i
].pum_text
== compl_shown_match
->cp_str
2822 || compl_match_array
[i
].pum_text
2823 == compl_shown_match
->cp_text
[CPT_ABBR
])
2830 if (compl_match_array
!= NULL
)
2832 /* Compute the screen column of the start of the completed text.
2833 * Use the cursor to get all wrapping and other settings right. */
2834 col
= curwin
->w_cursor
.col
;
2835 curwin
->w_cursor
.col
= compl_col
;
2836 pum_display(compl_match_array
, compl_match_arraysize
, cur
);
2837 curwin
->w_cursor
.col
= col
;
2841 #define DICT_FIRST (1) /* use just first element in "dict" */
2842 #define DICT_EXACT (2) /* "dict" is the exact name of a file */
2845 * Add any identifiers that match the given pattern in the list of dictionary
2846 * files "dict_start" to the list of completions.
2849 ins_compl_dictionaries(dict_start
, pat
, flags
, thesaurus
)
2852 int flags
; /* DICT_FIRST and/or DICT_EXACT */
2853 int thesaurus
; /* Thesaurus completion */
2855 char_u
*dict
= dict_start
;
2858 regmatch_T regmatch
;
2862 int dir
= compl_direction
;
2867 /* When 'dictionary' is empty and spell checking is enabled use
2869 if (!thesaurus
&& curwin
->w_p_spell
)
2870 dict
= (char_u
*)"spell";
2879 regmatch
.regprog
= NULL
; /* so that we can goto theend */
2881 /* If 'infercase' is set, don't use 'smartcase' here */
2883 if (curbuf
->b_p_inf
)
2886 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2887 * to only match at the start of a line. Otherwise just match the
2888 * pattern. Also need to double backslashes. */
2889 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2891 char_u
*pat_esc
= vim_strsave_escaped(pat
, (char_u
*)"\\");
2894 if (pat_esc
== NULL
)
2896 len
= STRLEN(pat_esc
) + 10;
2897 ptr
= alloc((unsigned)len
);
2903 vim_snprintf((char *)ptr
, len
, "^\\s*\\zs\\V%s", pat_esc
);
2904 regmatch
.regprog
= vim_regcomp(ptr
, RE_MAGIC
);
2910 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
2911 if (regmatch
.regprog
== NULL
)
2915 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2916 regmatch
.rm_ic
= ignorecase(pat
);
2917 while (*dict
!= NUL
&& !got_int
&& !compl_interrupted
)
2919 /* copy one dictionary file name into buf */
2920 if (flags
== DICT_EXACT
)
2927 /* Expand wildcards in the dictionary name, but do not allow
2928 * backticks (for security, the 'dict' option may have been set in
2930 copy_option_part(&dict
, buf
, LSIZE
, ",");
2932 if (!thesaurus
&& STRCMP(buf
, "spell") == 0)
2936 if (vim_strchr(buf
, '`') != NULL
2937 || expand_wildcards(1, &buf
, &count
, &files
,
2938 EW_FILE
|EW_SILENT
) != OK
)
2945 /* Complete from active spelling. Skip "\<" in the pattern, we
2946 * don't use it as a RE. */
2947 if (pat
[0] == '\\' && pat
[1] == '<')
2951 spell_dump_compl(curbuf
, ptr
, regmatch
.rm_ic
, &dir
, 0);
2955 if (count
> 0) /* avoid warning for using "files" uninit */
2957 ins_compl_files(count
, files
, thesaurus
, flags
,
2958 ®match
, buf
, &dir
);
2959 if (flags
!= DICT_EXACT
)
2960 FreeWild(count
, files
);
2968 vim_free(regmatch
.regprog
);
2973 ins_compl_files(count
, files
, thesaurus
, flags
, regmatch
, buf
, dir
)
2978 regmatch_T
*regmatch
;
2987 for (i
= 0; i
< count
&& !got_int
&& !compl_interrupted
; i
++)
2989 fp
= mch_fopen((char *)files
[i
], "r"); /* open dictionary file */
2990 if (flags
!= DICT_EXACT
)
2992 vim_snprintf((char *)IObuff
, IOSIZE
,
2993 _("Scanning dictionary: %s"), (char *)files
[i
]);
2994 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3000 * Read dictionary file line by line.
3001 * Check each line for a match.
3003 while (!got_int
&& !compl_interrupted
3004 && !vim_fgets(buf
, LSIZE
, fp
))
3007 while (vim_regexec(regmatch
, buf
, (colnr_T
)(ptr
- buf
)))
3009 ptr
= regmatch
->startp
[0];
3010 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3011 ptr
= find_line_end(ptr
);
3013 ptr
= find_word_end(ptr
);
3014 add_r
= ins_compl_add_infercase(regmatch
->startp
[0],
3015 (int)(ptr
- regmatch
->startp
[0]),
3016 p_ic
, files
[i
], *dir
, 0);
3022 * Add the other matches on the line
3027 /* Find start of the next word. Skip white
3028 * space and punctuation. */
3029 ptr
= find_word_start(ptr
);
3030 if (*ptr
== NUL
|| *ptr
== NL
)
3034 /* Find end of the word. */
3037 /* Japanese words may have characters in
3038 * different classes, only separate words
3039 * with single-byte non-word characters. */
3042 int l
= (*mb_ptr2len
)(ptr
);
3044 if (l
< 2 && !vim_iswordc(*ptr
))
3050 ptr
= find_word_end(ptr
);
3052 /* Add the word. Skip the regexp match. */
3053 if (wstart
!= regmatch
->startp
[0])
3054 add_r
= ins_compl_add_infercase(wstart
,
3055 (int)(ptr
- wstart
),
3056 p_ic
, files
[i
], *dir
, 0);
3060 /* if dir was BACKWARD then honor it just once */
3062 else if (add_r
== FAIL
)
3064 /* avoid expensive call to vim_regexec() when at end
3066 if (*ptr
== '\n' || got_int
)
3070 ins_compl_check_keys(50);
3078 * Find the start of the next word.
3079 * Returns a pointer to the first char of the word. Also stops at a NUL.
3082 find_word_start(ptr
)
3087 while (*ptr
!= NUL
&& *ptr
!= '\n' && mb_get_class(ptr
) <= 1)
3088 ptr
+= (*mb_ptr2len
)(ptr
);
3091 while (*ptr
!= NUL
&& *ptr
!= '\n' && !vim_iswordc(*ptr
))
3097 * Find the end of the word. Assumes it starts inside a word.
3098 * Returns a pointer to just after the word.
3109 start_class
= mb_get_class(ptr
);
3110 if (start_class
> 1)
3113 ptr
+= (*mb_ptr2len
)(ptr
);
3114 if (mb_get_class(ptr
) != start_class
)
3120 while (vim_iswordc(*ptr
))
3126 * Find the end of the line, omitting CR and NL at the end.
3127 * Returns a pointer to just after the line.
3135 s
= ptr
+ STRLEN(ptr
);
3136 while (s
> ptr
&& (s
[-1] == CAR
|| s
[-1] == NL
))
3142 * Free the list of completions
3150 vim_free(compl_pattern
);
3151 compl_pattern
= NULL
;
3152 vim_free(compl_leader
);
3153 compl_leader
= NULL
;
3155 if (compl_first_match
== NULL
)
3158 ins_compl_del_pum();
3161 compl_curr_match
= compl_first_match
;
3164 match
= compl_curr_match
;
3165 compl_curr_match
= compl_curr_match
->cp_next
;
3166 vim_free(match
->cp_str
);
3167 /* several entries may use the same fname, free it just once. */
3168 if (match
->cp_flags
& FREE_FNAME
)
3169 vim_free(match
->cp_fname
);
3170 for (i
= 0; i
< CPT_COUNT
; ++i
)
3171 vim_free(match
->cp_text
[i
]);
3173 } while (compl_curr_match
!= NULL
&& compl_curr_match
!= compl_first_match
);
3174 compl_first_match
= compl_curr_match
= NULL
;
3180 compl_cont_status
= 0;
3181 compl_started
= FALSE
;
3183 vim_free(compl_pattern
);
3184 compl_pattern
= NULL
;
3185 vim_free(compl_leader
);
3186 compl_leader
= NULL
;
3187 edit_submode_extra
= NULL
;
3188 vim_free(compl_orig_text
);
3189 compl_orig_text
= NULL
;
3190 compl_enter_selects
= FALSE
;
3194 * Return TRUE when Insert completion is active.
3199 return compl_started
;
3203 * Delete one character before the cursor and show the subset of the matches
3204 * that match the word that is now before the cursor.
3205 * Returns the character to be used, NUL if the work is done and another char
3206 * to be got from the user.
3214 line
= ml_get_curline();
3215 p
= line
+ curwin
->w_cursor
.col
;
3216 mb_ptr_back(line
, p
);
3218 /* Stop completion when the whole word was deleted. For Omni completion
3219 * allow the word to be deleted, we won't match everything. */
3220 if ((int)(p
- line
) - (int)compl_col
< 0
3221 || ((int)(p
- line
) - (int)compl_col
== 0
3222 && (ctrl_x_mode
& CTRL_X_OMNI
) == 0))
3225 /* Deleted more than what was used to find matches or didn't finish
3226 * finding all matches: need to look for matches all over again. */
3227 if (curwin
->w_cursor
.col
<= compl_col
+ compl_length
3228 || compl_was_interrupted
)
3229 ins_compl_restart();
3231 vim_free(compl_leader
);
3232 compl_leader
= vim_strnsave(line
+ compl_col
, (int)(p
- line
) - compl_col
);
3233 if (compl_leader
!= NULL
)
3235 ins_compl_new_leader();
3242 * Called after changing "compl_leader".
3243 * Show the popup menu with a different set of matches.
3244 * May also search for matches again if the previous search was interrupted.
3247 ins_compl_new_leader()
3249 ins_compl_del_pum();
3251 ins_bytes(compl_leader
+ ins_compl_len());
3252 compl_used_match
= FALSE
;
3255 ins_compl_set_original_text(compl_leader
);
3259 spell_bad_len
= 0; /* need to redetect bad word */
3262 * Matches were cleared, need to search for them now. First display
3263 * the changed text before the cursor. Set "compl_restarting" to
3264 * avoid that the first match is inserted.
3270 /* Show the cursor after the match, not after the redrawn text. */
3273 gui_update_cursor(FALSE
, FALSE
);
3276 compl_restarting
= TRUE
;
3277 if (ins_complete(Ctrl_N
) == FAIL
)
3278 compl_cont_status
= 0;
3279 compl_restarting
= FALSE
;
3282 #if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
3283 if (!compl_used_match
)
3285 /* Go to the original text, since none of the matches is inserted. */
3286 if (compl_first_match
->cp_prev
!= NULL
3287 && (compl_first_match
->cp_prev
->cp_flags
& ORIGINAL_TEXT
))
3288 compl_shown_match
= compl_first_match
->cp_prev
;
3290 compl_shown_match
= compl_first_match
;
3291 compl_curr_match
= compl_shown_match
;
3292 compl_shows_dir
= compl_direction
;
3295 compl_enter_selects
= !compl_used_match
;
3297 /* Show the popup menu with a different set of matches. */
3298 ins_compl_show_pum();
3300 /* Don't let Enter select the original text when there is no popup menu. */
3301 if (compl_match_array
== NULL
)
3302 compl_enter_selects
= FALSE
;
3306 * Return the length of the completion, from the completion start column to
3307 * the cursor column. Making sure it never goes below zero.
3312 int off
= (int)curwin
->w_cursor
.col
- (int)compl_col
;
3320 * Append one character to the match leader. May reduce the number of
3324 ins_compl_addleader(c
)
3330 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
3332 char_u buf
[MB_MAXBYTES
+ 1];
3334 (*mb_char2bytes
)(c
, buf
);
3336 ins_char_bytes(buf
, cc
);
3342 /* If we didn't complete finding matches we must search again. */
3343 if (compl_was_interrupted
)
3344 ins_compl_restart();
3346 vim_free(compl_leader
);
3347 compl_leader
= vim_strnsave(ml_get_curline() + compl_col
,
3348 (int)(curwin
->w_cursor
.col
- compl_col
));
3349 if (compl_leader
!= NULL
)
3350 ins_compl_new_leader();
3354 * Setup for finding completions again without leaving CTRL-X mode. Used when
3355 * BS or a key was typed while still searching for matches.
3361 compl_started
= FALSE
;
3363 compl_cont_status
= 0;
3364 compl_cont_mode
= 0;
3368 * Set the first match, the original text.
3371 ins_compl_set_original_text(str
)
3376 /* Replace the original text entry. */
3377 if (compl_first_match
->cp_flags
& ORIGINAL_TEXT
) /* safety check */
3379 p
= vim_strsave(str
);
3382 vim_free(compl_first_match
->cp_str
);
3383 compl_first_match
->cp_str
= p
;
3389 * Append one character to the match leader. May reduce the number of
3393 ins_compl_addfrommatch()
3396 int len
= (int)curwin
->w_cursor
.col
- (int)compl_col
;
3400 p
= compl_shown_match
->cp_str
;
3401 if ((int)STRLEN(p
) <= len
) /* the match is too short */
3403 /* When still at the original match use the first entry that matches
3405 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
3408 for (cp
= compl_shown_match
->cp_next
; cp
!= NULL
3409 && cp
!= compl_first_match
; cp
= cp
->cp_next
)
3411 if (compl_leader
== NULL
3412 || ins_compl_equal(cp
, compl_leader
,
3413 (int)STRLEN(compl_leader
)))
3419 if (p
== NULL
|| (int)STRLEN(p
) <= len
)
3431 ins_compl_addleader(c
);
3435 * Prepare for Insert mode completion, or stop it.
3436 * Called just after typing a character in Insert mode.
3437 * Returns TRUE when the character is not to be inserted;
3447 /* Forget any previous 'special' messages if this is actually
3448 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3450 if (c
!= Ctrl_R
&& vim_is_ctrl_x_key(c
))
3451 edit_submode_extra
= NULL
;
3453 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3454 if (c
== K_SELECT
|| c
== K_MOUSEDOWN
|| c
== K_MOUSEUP
)
3457 /* Set "compl_get_longest" when finding the first matches. */
3458 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
3459 || (ctrl_x_mode
== 0 && !compl_started
))
3461 compl_get_longest
= (vim_strchr(p_cot
, 'l') != NULL
);
3462 compl_used_match
= TRUE
;
3465 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
)
3468 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3469 * it will be yet. Now we decide.
3475 ctrl_x_mode
= CTRL_X_SCROLL
;
3476 if (!(State
& REPLACE_FLAG
))
3477 edit_submode
= (char_u
*)_(" (insert) Scroll (^E/^Y)");
3479 edit_submode
= (char_u
*)_(" (replace) Scroll (^E/^Y)");
3480 edit_submode_pre
= NULL
;
3484 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
3487 ctrl_x_mode
= CTRL_X_FILES
;
3490 ctrl_x_mode
= CTRL_X_DICTIONARY
;
3493 /* Simply allow ^R to happen without affecting ^X mode */
3496 ctrl_x_mode
= CTRL_X_THESAURUS
;
3498 #ifdef FEAT_COMPL_FUNC
3500 ctrl_x_mode
= CTRL_X_FUNCTION
;
3503 ctrl_x_mode
= CTRL_X_OMNI
;
3508 ctrl_x_mode
= CTRL_X_SPELL
;
3510 ++emsg_off
; /* Avoid getting the E756 error twice. */
3511 spell_back_to_badword();
3516 ctrl_x_mode
= CTRL_X_TAGS
;
3521 ctrl_x_mode
= CTRL_X_PATH_PATTERNS
;
3524 ctrl_x_mode
= CTRL_X_PATH_DEFINES
;
3529 ctrl_x_mode
= CTRL_X_CMDLINE
;
3533 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3534 * just started ^X mode, or there were enough ^X's to cancel
3535 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3536 * do normal expansion when interrupting a different mode (say
3537 * ^X^F^X^P or ^P^X^X^P, see below)
3538 * nothing changes if interrupting mode 0, (eg, the flag
3539 * doesn't change when going to ADDING mode -- Acevedo */
3540 if (!(compl_cont_status
& CONT_INTRPT
))
3541 compl_cont_status
|= CONT_LOCAL
;
3542 else if (compl_cont_mode
!= 0)
3543 compl_cont_status
&= ~CONT_LOCAL
;
3546 /* If we have typed at least 2 ^X's... for modes != 0, we set
3547 * compl_cont_status = 0 (eg, as if we had just started ^X
3549 * For mode 0, we set "compl_cont_mode" to an impossible
3550 * value, in both cases ^X^X can be used to restart the same
3551 * mode (avoiding ADDING mode).
3552 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3553 * 'complete' and local ^P expansions respectively.
3554 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3555 * mode -- Acevedo */
3558 if (compl_cont_mode
!= 0)
3559 compl_cont_status
= 0;
3561 compl_cont_mode
= CTRL_X_NOT_DEFINED_YET
;
3564 edit_submode
= NULL
;
3569 else if (ctrl_x_mode
!= 0)
3571 /* We're already in CTRL-X mode, do we stay in it? */
3572 if (!vim_is_ctrl_x_key(c
))
3574 if (ctrl_x_mode
== CTRL_X_SCROLL
)
3577 ctrl_x_mode
= CTRL_X_FINISHED
;
3578 edit_submode
= NULL
;
3583 if (compl_started
|| ctrl_x_mode
== CTRL_X_FINISHED
)
3585 /* Show error message from attempted keyword completion (probably
3586 * 'Pattern not found') until another key is hit, then go back to
3587 * showing what mode we are in. */
3589 if ((ctrl_x_mode
== 0 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_R
3590 && !ins_compl_pum_key(c
))
3591 || ctrl_x_mode
== CTRL_X_FINISHED
)
3593 /* Get here when we have finished typing a sequence of ^N and
3594 * ^P or other completion characters in CTRL-X mode. Free up
3595 * memory that was used, and make sure we can redo the insert. */
3596 if (compl_curr_match
!= NULL
|| compl_leader
!= NULL
|| c
== Ctrl_E
)
3602 * If any of the original typed text has been changed, eg when
3603 * ignorecase is set, we must add back-spaces to the redo
3604 * buffer. We add as few as necessary to delete just the part
3605 * of the original text that has changed.
3606 * When using the longest match, edited the match or used
3607 * CTRL-E then don't use the current match.
3609 if (compl_curr_match
!= NULL
&& compl_used_match
&& c
!= Ctrl_E
)
3610 ptr
= compl_curr_match
->cp_str
;
3611 else if (compl_leader
!= NULL
)
3614 ptr
= compl_orig_text
;
3615 if (compl_orig_text
!= NULL
)
3617 p
= compl_orig_text
;
3618 for (temp
= 0; p
[temp
] != NUL
&& p
[temp
] == ptr
[temp
];
3623 temp
-= (*mb_head_off
)(compl_orig_text
, p
+ temp
);
3625 for (p
+= temp
; *p
!= NUL
; mb_ptr_adv(p
))
3626 AppendCharToRedobuff(K_BS
);
3629 AppendToRedobuffLit(ptr
+ temp
, -1);
3633 want_cindent
= (can_cindent
&& cindent_on());
3636 * When completing whole lines: fix indent for 'cindent'.
3637 * Otherwise, break line if it's too long.
3639 if (compl_cont_mode
== CTRL_X_WHOLE_LINE
)
3642 /* re-indent the current line */
3646 want_cindent
= FALSE
; /* don't do it again */
3652 int prev_col
= curwin
->w_cursor
.col
;
3654 /* put the cursor on the last char, for 'tw' formatting */
3657 if (stop_arrow() == OK
)
3658 insertchar(NUL
, 0, -1);
3660 && ml_get_curline()[curwin
->w_cursor
.col
] != NUL
)
3664 /* If the popup menu is displayed pressing CTRL-Y means accepting
3665 * the selection without inserting anything. When
3666 * compl_enter_selects is set the Enter key does the same. */
3667 if ((c
== Ctrl_Y
|| (compl_enter_selects
3668 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
3672 /* CTRL-E means completion is Ended, go back to the typed text. */
3676 if (compl_leader
!= NULL
)
3677 ins_bytes(compl_leader
+ ins_compl_len());
3678 else if (compl_first_match
!= NULL
)
3679 ins_bytes(compl_orig_text
+ ins_compl_len());
3683 auto_format(FALSE
, TRUE
);
3686 compl_started
= FALSE
;
3688 msg_clr_cmdline(); /* necessary for "noshowmode" */
3690 compl_enter_selects
= FALSE
;
3691 if (edit_submode
!= NULL
)
3693 edit_submode
= NULL
;
3699 * Indent now if a key was typed that is in 'cinkeys'.
3701 if (want_cindent
&& in_cinkeys(KEY_COMPLETE
, ' ', inindent(0)))
3707 /* reset continue_* if we left expansion-mode, if we stay they'll be
3708 * (re)set properly in ins_complete() */
3709 if (!vim_is_ctrl_x_key(c
))
3711 compl_cont_status
= 0;
3712 compl_cont_mode
= 0;
3719 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3720 * (depending on flag) starting from buf and looking for a non-scanned
3721 * buffer (other than curbuf). curbuf is special, if it is called with
3722 * buf=curbuf then it has to be the first call for a given flag/expansion.
3724 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3727 ins_compl_next_buf(buf
, flag
)
3735 if (flag
== 'w') /* just windows */
3738 if (buf
== curbuf
) /* first call for this flag/expansion */
3740 while ((wp
= (wp
->w_next
!= NULL
? wp
->w_next
: firstwin
)) != curwin
3741 && wp
->w_buffer
->b_scanned
)
3749 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3750 * (unlisted buffers)
3751 * When completing whole lines skip unloaded buffers. */
3752 while ((buf
= (buf
->b_next
!= NULL
? buf
->b_next
: firstbuf
)) != curbuf
3756 || (buf
->b_ml
.ml_mfp
== NULL
) != (flag
== 'u')))
3762 #ifdef FEAT_COMPL_FUNC
3763 static void expand_by_function
__ARGS((int type
, char_u
*base
));
3766 * Execute user defined complete function 'completefunc' or 'omnifunc', and
3767 * get matches in "matches".
3770 expand_by_function(type
, base
)
3771 int type
; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
3779 funcname
= (type
== CTRL_X_FUNCTION
) ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
3780 if (*funcname
== NUL
)
3783 /* Call 'completefunc' to obtain the list of matches. */
3784 args
[0] = (char_u
*)"0";
3787 pos
= curwin
->w_cursor
;
3788 matchlist
= call_func_retlist(funcname
, 2, args
, FALSE
);
3789 curwin
->w_cursor
= pos
; /* restore the cursor position */
3790 if (matchlist
== NULL
)
3793 ins_compl_add_list(matchlist
);
3794 list_unref(matchlist
);
3796 #endif /* FEAT_COMPL_FUNC */
3798 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
3800 * Add completions from a list.
3803 ins_compl_add_list(list
)
3807 int dir
= compl_direction
;
3809 /* Go through the List with matches and add each of them. */
3810 for (li
= list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3812 if (ins_compl_add_tv(&li
->li_tv
, dir
) == OK
)
3813 /* if dir was BACKWARD then honor it just once */
3821 * Add a match to the list of matches from a typeval_T.
3822 * If the given string is already in the list of completions, then return
3823 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3824 * maybe because alloc() returns NULL, then FAIL is returned.
3827 ins_compl_add_tv(tv
, dir
)
3834 char_u
*(cptext
[CPT_COUNT
]);
3836 if (tv
->v_type
== VAR_DICT
&& tv
->vval
.v_dict
!= NULL
)
3838 word
= get_dict_string(tv
->vval
.v_dict
, (char_u
*)"word", FALSE
);
3839 cptext
[CPT_ABBR
] = get_dict_string(tv
->vval
.v_dict
,
3840 (char_u
*)"abbr", FALSE
);
3841 cptext
[CPT_MENU
] = get_dict_string(tv
->vval
.v_dict
,
3842 (char_u
*)"menu", FALSE
);
3843 cptext
[CPT_KIND
] = get_dict_string(tv
->vval
.v_dict
,
3844 (char_u
*)"kind", FALSE
);
3845 cptext
[CPT_INFO
] = get_dict_string(tv
->vval
.v_dict
,
3846 (char_u
*)"info", FALSE
);
3847 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"icase", FALSE
) != NULL
)
3848 icase
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"icase");
3849 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"dup", FALSE
) != NULL
)
3850 adup
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"dup");
3854 word
= get_tv_string_chk(tv
);
3855 vim_memset(cptext
, 0, sizeof(cptext
));
3857 if (word
== NULL
|| *word
== NUL
)
3859 return ins_compl_add(word
, -1, icase
, NULL
, cptext
, dir
, 0, adup
);
3864 * Get the next expansion(s), using "compl_pattern".
3865 * The search starts at position "ini" in curbuf and in the direction
3867 * When "compl_started" is FALSE start at that position, otherwise continue
3868 * where we stopped searching before.
3869 * This may return before finding all the matches.
3870 * Return the total number of matches or -1 if still unknown -- Acevedo
3873 ins_compl_get_exp(ini
)
3876 static pos_T first_match_pos
;
3877 static pos_T last_match_pos
;
3878 static char_u
*e_cpt
= (char_u
*)""; /* curr. entry in 'complete' */
3879 static int found_all
= FALSE
; /* Found all matches of a
3881 static buf_T
*ins_buf
= NULL
; /* buffer being scanned */
3891 int found_new_match
;
3892 int type
= ctrl_x_mode
;
3894 char_u
*dict
= NULL
;
3900 for (ins_buf
= firstbuf
; ins_buf
!= NULL
; ins_buf
= ins_buf
->b_next
)
3901 ins_buf
->b_scanned
= 0;
3904 e_cpt
= (compl_cont_status
& CONT_LOCAL
)
3905 ? (char_u
*)"." : curbuf
->b_p_cpt
;
3906 last_match_pos
= first_match_pos
= *ini
;
3909 old_match
= compl_curr_match
; /* remember the last current match */
3910 pos
= (compl_direction
== FORWARD
) ? &last_match_pos
: &first_match_pos
;
3911 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3914 found_new_match
= FAIL
;
3916 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
3917 * or if found_all says this entry is done. For ^X^L only use the
3918 * entries from 'complete' that look in loaded buffers. */
3919 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3920 && (!compl_started
|| found_all
))
3923 while (*e_cpt
== ',' || *e_cpt
== ' ')
3925 if (*e_cpt
== '.' && !curbuf
->b_scanned
)
3928 first_match_pos
= *ini
;
3929 /* So that ^N can match word immediately after cursor */
3930 if (ctrl_x_mode
== 0)
3931 dec(&first_match_pos
);
3932 last_match_pos
= first_match_pos
;
3935 else if (vim_strchr((char_u
*)"buwU", *e_cpt
) != NULL
3936 && (ins_buf
= ins_compl_next_buf(ins_buf
, *e_cpt
)) != curbuf
)
3938 /* Scan a buffer, but not the current one. */
3939 if (ins_buf
->b_ml
.ml_mfp
!= NULL
) /* loaded buffer */
3941 compl_started
= TRUE
;
3942 first_match_pos
.col
= last_match_pos
.col
= 0;
3943 first_match_pos
.lnum
= ins_buf
->b_ml
.ml_line_count
+ 1;
3944 last_match_pos
.lnum
= 0;
3947 else /* unloaded buffer, scan like dictionary */
3950 if (ins_buf
->b_fname
== NULL
)
3952 type
= CTRL_X_DICTIONARY
;
3953 dict
= ins_buf
->b_fname
;
3954 dict_f
= DICT_EXACT
;
3956 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning: %s"),
3957 ins_buf
->b_fname
== NULL
3958 ? buf_spname(ins_buf
)
3959 : ins_buf
->b_sfname
== NULL
3960 ? (char *)ins_buf
->b_fname
3961 : (char *)ins_buf
->b_sfname
);
3962 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3964 else if (*e_cpt
== NUL
)
3968 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3970 else if (*e_cpt
== 'k' || *e_cpt
== 's')
3973 type
= CTRL_X_DICTIONARY
;
3975 type
= CTRL_X_THESAURUS
;
3976 if (*++e_cpt
!= ',' && *e_cpt
!= NUL
)
3979 dict_f
= DICT_FIRST
;
3983 else if (*e_cpt
== 'i')
3984 type
= CTRL_X_PATH_PATTERNS
;
3985 else if (*e_cpt
== 'd')
3986 type
= CTRL_X_PATH_DEFINES
;
3988 else if (*e_cpt
== ']' || *e_cpt
== 't')
3991 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning tags."));
3992 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3997 /* in any case e_cpt is advanced to the next entry */
3998 (void)copy_option_part(&e_cpt
, IObuff
, IOSIZE
, ",");
4011 case CTRL_X_PATH_PATTERNS
:
4012 case CTRL_X_PATH_DEFINES
:
4013 find_pattern_in_path(compl_pattern
, compl_direction
,
4014 (int)STRLEN(compl_pattern
), FALSE
, FALSE
,
4015 (type
== CTRL_X_PATH_DEFINES
4016 && !(compl_cont_status
& CONT_SOL
))
4017 ? FIND_DEFINE
: FIND_ANY
, 1L, ACTION_EXPAND
,
4018 (linenr_T
)1, (linenr_T
)MAXLNUM
);
4022 case CTRL_X_DICTIONARY
:
4023 case CTRL_X_THESAURUS
:
4024 ins_compl_dictionaries(
4026 : (type
== CTRL_X_THESAURUS
4027 ? (*curbuf
->b_p_tsr
== NUL
4030 : (*curbuf
->b_p_dict
== NUL
4032 : curbuf
->b_p_dict
)),
4034 dict
!= NULL
? dict_f
4035 : 0, type
== CTRL_X_THESAURUS
);
4040 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4042 p_ic
= ignorecase(compl_pattern
);
4044 /* Find up to TAG_MANY matches. Avoids that an enourmous number
4045 * of matches is found when compl_pattern is empty */
4046 if (find_tags(compl_pattern
, &num_matches
, &matches
,
4047 TAG_REGEXP
| TAG_NAMES
| TAG_NOIC
|
4048 TAG_INS_COMP
| (ctrl_x_mode
? TAG_VERBOSE
: 0),
4049 TAG_MANY
, curbuf
->b_ffname
) == OK
&& num_matches
> 0)
4051 ins_compl_add_matches(num_matches
, matches
, p_ic
);
4057 if (expand_wildcards(1, &compl_pattern
, &num_matches
, &matches
,
4058 EW_FILE
|EW_DIR
|EW_ADDSLASH
|EW_SILENT
) == OK
)
4061 /* May change home directory back to "~". */
4062 tilde_replace(compl_pattern
, num_matches
, matches
);
4063 ins_compl_add_matches(num_matches
, matches
,
4064 #ifdef CASE_INSENSITIVE_FILENAME
4073 case CTRL_X_CMDLINE
:
4074 if (expand_cmdline(&compl_xp
, compl_pattern
,
4075 (int)STRLEN(compl_pattern
),
4076 &num_matches
, &matches
) == EXPAND_OK
)
4077 ins_compl_add_matches(num_matches
, matches
, FALSE
);
4080 #ifdef FEAT_COMPL_FUNC
4081 case CTRL_X_FUNCTION
:
4083 expand_by_function(type
, compl_pattern
);
4089 num_matches
= expand_spelling(first_match_pos
.lnum
,
4090 compl_pattern
, &matches
);
4091 if (num_matches
> 0)
4092 ins_compl_add_matches(num_matches
, matches
, p_ic
);
4096 default: /* normal ^P/^N and ^X^L */
4098 * If 'infercase' is set, don't use 'smartcase' here
4101 if (ins_buf
->b_p_inf
)
4104 /* buffers other than curbuf are scanned from the beginning or the
4105 * end but never from the middle, thus setting nowrapscan in this
4106 * buffers is a good idea, on the other hand, we always set
4107 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4109 if (ins_buf
!= curbuf
)
4111 else if (*e_cpt
== '.')
4117 ++msg_silent
; /* Don't want messages for wrapscan. */
4119 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4120 * has added a word that was at the beginning of the line */
4121 if ( ctrl_x_mode
== CTRL_X_WHOLE_LINE
4122 || (compl_cont_status
& CONT_SOL
))
4123 found_new_match
= search_for_exact_line(ins_buf
, pos
,
4124 compl_direction
, compl_pattern
);
4126 found_new_match
= searchit(NULL
, ins_buf
, pos
,
4128 compl_pattern
, 1L, SEARCH_KEEP
+ SEARCH_NFMSG
,
4129 RE_LAST
, (linenr_T
)0, NULL
);
4133 /* set "compl_started" even on fail */
4134 compl_started
= TRUE
;
4135 first_match_pos
= *pos
;
4136 last_match_pos
= *pos
;
4138 else if (first_match_pos
.lnum
== last_match_pos
.lnum
4139 && first_match_pos
.col
== last_match_pos
.col
)
4140 found_new_match
= FAIL
;
4141 if (found_new_match
== FAIL
)
4143 if (ins_buf
== curbuf
)
4148 /* when ADDING, the text before the cursor matches, skip it */
4149 if ( (compl_cont_status
& CONT_ADDING
) && ins_buf
== curbuf
4150 && ini
->lnum
== pos
->lnum
4151 && ini
->col
== pos
->col
)
4153 ptr
= ml_get_buf(ins_buf
, pos
->lnum
, FALSE
) + pos
->col
;
4154 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4156 if (compl_cont_status
& CONT_ADDING
)
4158 if (pos
->lnum
>= ins_buf
->b_ml
.ml_line_count
)
4160 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
4162 ptr
= skipwhite(ptr
);
4164 len
= (int)STRLEN(ptr
);
4168 char_u
*tmp_ptr
= ptr
;
4170 if (compl_cont_status
& CONT_ADDING
)
4172 tmp_ptr
+= compl_length
;
4173 /* Skip if already inside a word. */
4174 if (vim_iswordp(tmp_ptr
))
4176 /* Find start of next word. */
4177 tmp_ptr
= find_word_start(tmp_ptr
);
4179 /* Find end of this word. */
4180 tmp_ptr
= find_word_end(tmp_ptr
);
4181 len
= (int)(tmp_ptr
- ptr
);
4183 if ((compl_cont_status
& CONT_ADDING
)
4184 && len
== compl_length
)
4186 if (pos
->lnum
< ins_buf
->b_ml
.ml_line_count
)
4188 /* Try next line, if any. the new word will be
4189 * "join" as if the normal command "J" was used.
4190 * IOSIZE is always greater than
4191 * compl_length, so the next STRNCPY always
4192 * works -- Acevedo */
4193 STRNCPY(IObuff
, ptr
, len
);
4194 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
4195 tmp_ptr
= ptr
= skipwhite(ptr
);
4196 /* Find start of next word. */
4197 tmp_ptr
= find_word_start(tmp_ptr
);
4198 /* Find end of next word. */
4199 tmp_ptr
= find_word_end(tmp_ptr
);
4202 if (*ptr
!= ')' && IObuff
[len
- 1] != TAB
)
4204 if (IObuff
[len
- 1] != ' ')
4205 IObuff
[len
++] = ' ';
4206 /* IObuf =~ "\k.* ", thus len >= 2 */
4208 && (IObuff
[len
- 2] == '.'
4209 || (vim_strchr(p_cpo
, CPO_JOINSP
)
4211 && (IObuff
[len
- 2] == '?'
4212 || IObuff
[len
- 2] == '!'))))
4213 IObuff
[len
++] = ' ';
4215 /* copy as much as posible of the new word */
4216 if (tmp_ptr
- ptr
>= IOSIZE
- len
)
4217 tmp_ptr
= ptr
+ IOSIZE
- len
- 1;
4218 STRNCPY(IObuff
+ len
, ptr
, tmp_ptr
- ptr
);
4219 len
+= (int)(tmp_ptr
- ptr
);
4220 flags
|= CONT_S_IPOS
;
4225 if (len
== compl_length
)
4229 if (ins_compl_add_infercase(ptr
, len
, p_ic
,
4230 ins_buf
== curbuf
? NULL
: ins_buf
->b_sfname
,
4231 0, flags
) != NOTDONE
)
4233 found_new_match
= OK
;
4241 /* check if compl_curr_match has changed, (e.g. other type of
4242 * expansion added something) */
4243 if (type
!= 0 && compl_curr_match
!= old_match
)
4244 found_new_match
= OK
;
4246 /* break the loop for specialized modes (use 'complete' just for the
4247 * generic ctrl_x_mode == 0) or when we've found a new match */
4248 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4249 || found_new_match
!= FAIL
)
4253 /* Fill the popup menu as soon as possible. */
4255 ins_compl_check_keys(0);
4257 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4258 || compl_interrupted
)
4260 compl_started
= TRUE
;
4264 /* Mark a buffer scanned when it has been scanned completely */
4265 if (type
== 0 || type
== CTRL_X_PATH_PATTERNS
)
4266 ins_buf
->b_scanned
= TRUE
;
4268 compl_started
= FALSE
;
4271 compl_started
= TRUE
;
4273 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4274 && *e_cpt
== NUL
) /* Got to end of 'complete' */
4275 found_new_match
= FAIL
;
4277 i
= -1; /* total of matches, unknown */
4278 if (found_new_match
== FAIL
4279 || (ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
))
4280 i
= ins_compl_make_cyclic();
4282 /* If several matches were added (FORWARD) or the search failed and has
4283 * just been made cyclic then we have to move compl_curr_match to the next
4284 * or previous entry (if any) -- Acevedo */
4285 compl_curr_match
= compl_direction
== FORWARD
? old_match
->cp_next
4286 : old_match
->cp_prev
;
4287 if (compl_curr_match
== NULL
)
4288 compl_curr_match
= old_match
;
4292 /* Delete the old text being completed. */
4299 * In insert mode: Delete the typed part.
4300 * In replace mode: Put the old characters back, if any.
4302 i
= compl_col
+ (compl_cont_status
& CONT_ADDING
? compl_length
: 0);
4303 backspace_until_column(i
);
4304 changed_cline_bef_curs();
4307 /* Insert the new text being completed. */
4311 ins_bytes(compl_shown_match
->cp_str
+ ins_compl_len());
4312 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
4313 compl_used_match
= FALSE
;
4315 compl_used_match
= TRUE
;
4319 * Fill in the next completion in the current direction.
4320 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4321 * get more completions. If it is FALSE, then we just do nothing when there
4322 * are no more completions in a given direction. The latter case is used when
4323 * we are still in the middle of finding completions, to allow browsing
4324 * through the ones found so far.
4325 * Return the total number of matches, or -1 if still unknown -- webb.
4327 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4328 * compl_shown_match here.
4330 * Note that this function may be called recursively once only. First with
4331 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4332 * calls this function with "allow_get_expansion" FALSE.
4335 ins_compl_next(allow_get_expansion
, count
, insert_match
)
4336 int allow_get_expansion
;
4337 int count
; /* repeat completion this many times; should
4339 int insert_match
; /* Insert the newly selected match */
4341 int num_matches
= -1;
4344 compl_T
*found_compl
= NULL
;
4345 int found_end
= FALSE
;
4348 if (compl_leader
!= NULL
4349 && (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0)
4351 /* Set "compl_shown_match" to the actually shown match, it may differ
4352 * when "compl_leader" is used to omit some of the matches. */
4353 while (!ins_compl_equal(compl_shown_match
,
4354 compl_leader
, (int)STRLEN(compl_leader
))
4355 && compl_shown_match
->cp_next
!= NULL
4356 && compl_shown_match
->cp_next
!= compl_first_match
)
4357 compl_shown_match
= compl_shown_match
->cp_next
;
4359 /* If we didn't find it searching forward, and compl_shows_dir is
4360 * backward, find the last match. */
4361 if (compl_shows_dir
== BACKWARD
4362 && !ins_compl_equal(compl_shown_match
,
4363 compl_leader
, (int)STRLEN(compl_leader
))
4364 && (compl_shown_match
->cp_next
== NULL
4365 || compl_shown_match
->cp_next
== compl_first_match
))
4367 while (!ins_compl_equal(compl_shown_match
,
4368 compl_leader
, (int)STRLEN(compl_leader
))
4369 && compl_shown_match
->cp_prev
!= NULL
4370 && compl_shown_match
->cp_prev
!= compl_first_match
)
4371 compl_shown_match
= compl_shown_match
->cp_prev
;
4375 if (allow_get_expansion
&& insert_match
4376 && (!(compl_get_longest
|| compl_restarting
) || compl_used_match
))
4377 /* Delete old text to be replaced */
4380 /* When finding the longest common text we stick at the original text,
4381 * don't let CTRL-N or CTRL-P move to the first match. */
4382 advance
= count
!= 1 || !allow_get_expansion
|| !compl_get_longest
;
4384 /* When restarting the search don't insert the first match either. */
4385 if (compl_restarting
)
4388 compl_restarting
= FALSE
;
4391 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4395 if (compl_shows_dir
== FORWARD
&& compl_shown_match
->cp_next
!= NULL
)
4397 compl_shown_match
= compl_shown_match
->cp_next
;
4398 found_end
= (compl_first_match
!= NULL
4399 && (compl_shown_match
->cp_next
== compl_first_match
4400 || compl_shown_match
== compl_first_match
));
4402 else if (compl_shows_dir
== BACKWARD
4403 && compl_shown_match
->cp_prev
!= NULL
)
4405 found_end
= (compl_shown_match
== compl_first_match
);
4406 compl_shown_match
= compl_shown_match
->cp_prev
;
4407 found_end
|= (compl_shown_match
== compl_first_match
);
4411 if (!allow_get_expansion
)
4415 if (compl_shows_dir
== BACKWARD
)
4416 compl_pending
-= todo
+ 1;
4418 compl_pending
+= todo
+ 1;
4425 if (compl_shows_dir
== BACKWARD
)
4432 num_matches
= ins_compl_get_exp(&compl_startpos
);
4434 /* handle any pending completions */
4435 while (compl_pending
!= 0 && compl_direction
== compl_shows_dir
4438 if (compl_pending
> 0 && compl_shown_match
->cp_next
!= NULL
)
4440 compl_shown_match
= compl_shown_match
->cp_next
;
4443 if (compl_pending
< 0 && compl_shown_match
->cp_prev
!= NULL
)
4445 compl_shown_match
= compl_shown_match
->cp_prev
;
4453 if ((compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0
4454 && compl_leader
!= NULL
4455 && !ins_compl_equal(compl_shown_match
,
4456 compl_leader
, (int)STRLEN(compl_leader
)))
4459 /* Remember a matching item. */
4460 found_compl
= compl_shown_match
;
4462 /* Stop at the end of the list when we found a usable match. */
4465 if (found_compl
!= NULL
)
4467 compl_shown_match
= found_compl
;
4470 todo
= 1; /* use first usable match after wrapping around */
4474 /* Insert the text of the new completion, or the compl_leader. */
4477 if (!compl_get_longest
|| compl_used_match
)
4480 ins_bytes(compl_leader
+ ins_compl_len());
4483 compl_used_match
= FALSE
;
4485 if (!allow_get_expansion
)
4487 /* may undisplay the popup menu first */
4488 ins_compl_upd_pum();
4490 /* redraw to show the user what was inserted */
4493 /* display the updated popup menu */
4494 ins_compl_show_pum();
4498 /* Show the cursor after the match, not after the redrawn text. */
4501 gui_update_cursor(FALSE
, FALSE
);
4505 /* Delete old text to be replaced, since we're still searching and
4506 * don't want to match ourselves! */
4510 /* Enter will select a match when the match wasn't inserted and the popup
4511 * menu is visible. */
4512 compl_enter_selects
= !insert_match
&& compl_match_array
!= NULL
;
4515 * Show the file name for the match (if any)
4516 * Truncate the file name to avoid a wait for return.
4518 if (compl_shown_match
->cp_fname
!= NULL
)
4520 STRCPY(IObuff
, "match in file ");
4521 i
= (vim_strsize(compl_shown_match
->cp_fname
) + 16) - sc_col
;
4525 STRCAT(IObuff
, "<");
4526 STRCAT(IObuff
, compl_shown_match
->cp_fname
+ i
);
4528 redraw_cmdline
= FALSE
; /* don't overwrite! */
4535 * Call this while finding completions, to check whether the user has hit a key
4536 * that should change the currently displayed completion, or exit completion
4537 * mode. Also, when compl_pending is not zero, show a completion as soon as
4539 * "frequency" specifies out of how many calls we actually check.
4542 ins_compl_check_keys(frequency
)
4545 static int count
= 0;
4549 /* Don't check when reading keys from a script. That would break the test
4554 /* Only do this at regular intervals */
4555 if (++count
< frequency
)
4559 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4560 * can't do its work correctly. */
4564 if (vim_is_ctrl_x_key(c
) && c
!= Ctrl_X
&& c
!= Ctrl_R
)
4566 c
= safe_vgetc(); /* Eat the character */
4567 compl_shows_dir
= ins_compl_key2dir(c
);
4568 (void)ins_compl_next(FALSE
, ins_compl_key2count(c
),
4569 c
!= K_UP
&& c
!= K_DOWN
);
4573 /* Need to get the character to have KeyTyped set. We'll put it
4574 * back with vungetc() below. But skip K_IGNORE. */
4578 /* Don't interrupt completion when the character wasn't typed,
4579 * e.g., when doing @q to replay keys. */
4580 if (c
!= Ctrl_R
&& KeyTyped
)
4581 compl_interrupted
= TRUE
;
4587 if (compl_pending
!= 0 && !got_int
)
4589 int todo
= compl_pending
> 0 ? compl_pending
: -compl_pending
;
4592 (void)ins_compl_next(FALSE
, todo
, TRUE
);
4597 * Decide the direction of Insert mode complete from the key typed.
4598 * Returns BACKWARD or FORWARD.
4601 ins_compl_key2dir(c
)
4604 if (c
== Ctrl_P
|| c
== Ctrl_L
4605 || (pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
4606 || c
== K_S_UP
|| c
== K_UP
)))
4612 * Return TRUE for keys that are used for completion only when the popup menu
4616 ins_compl_pum_key(c
)
4619 return pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
|| c
== K_S_UP
4620 || c
== K_PAGEDOWN
|| c
== K_KPAGEDOWN
|| c
== K_S_DOWN
4621 || c
== K_UP
|| c
== K_DOWN
);
4625 * Decide the number of completions to move forward.
4626 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4629 ins_compl_key2count(c
)
4634 if (ins_compl_pum_key(c
) && c
!= K_UP
&& c
!= K_DOWN
)
4636 h
= pum_get_height();
4638 h
-= 2; /* keep some context */
4645 * Return TRUE if completion with "c" should insert the match, FALSE if only
4646 * to change the currently selected completion.
4649 ins_compl_use_match(c
)
4668 * Do Insert mode completion.
4669 * Called when character "c" was typed, which has a meaning for completion.
4670 * Returns OK if completion was done, FAIL if something failed (out of mem).
4677 int startcol
= 0; /* column where searched text starts */
4678 colnr_T curs_col
; /* cursor column */
4681 compl_direction
= ins_compl_key2dir(c
);
4684 /* First time we hit ^N or ^P (in a row, I mean) */
4687 #ifdef FEAT_SMARTINDENT
4690 can_si_back
= FALSE
;
4692 if (stop_arrow() == FAIL
)
4695 line
= ml_get(curwin
->w_cursor
.lnum
);
4696 curs_col
= curwin
->w_cursor
.col
;
4699 /* If this same ctrl_x_mode has been interrupted use the text from
4700 * "compl_startpos" to the cursor as a pattern to add a new word
4701 * instead of expand the one before the cursor, in word-wise if
4702 * "compl_startpos" is not in the same line as the cursor then fix it
4703 * (the line has been split because it was longer than 'tw'). if SOL
4704 * is set then skip the previous pattern, a word at the beginning of
4705 * the line has been inserted, we'll look for that -- Acevedo. */
4706 if ((compl_cont_status
& CONT_INTRPT
) == CONT_INTRPT
4707 && compl_cont_mode
== ctrl_x_mode
)
4710 * it is a continued search
4712 compl_cont_status
&= ~CONT_INTRPT
; /* remove INTRPT */
4713 if (ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_PATH_PATTERNS
4714 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4716 if (compl_startpos
.lnum
!= curwin
->w_cursor
.lnum
)
4718 /* line (probably) wrapped, set compl_startpos to the
4719 * first non_blank in the line, if it is not a wordchar
4720 * include it to get a better pattern, but then we don't
4721 * want the "\\<" prefix, check it bellow */
4722 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4723 compl_startpos
.col
= compl_col
;
4724 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4725 compl_cont_status
&= ~CONT_SOL
; /* clear SOL if present */
4729 /* S_IPOS was set when we inserted a word that was at the
4730 * beginning of the line, which means that we'll go to SOL
4731 * mode but first we need to redefine compl_startpos */
4732 if (compl_cont_status
& CONT_S_IPOS
)
4734 compl_cont_status
|= CONT_SOL
;
4735 compl_startpos
.col
= (colnr_T
)(skipwhite(
4737 + compl_startpos
.col
) - line
);
4739 compl_col
= compl_startpos
.col
;
4741 compl_length
= curwin
->w_cursor
.col
- (int)compl_col
;
4742 /* IObuff is used to add a "word from the next line" would we
4743 * have enough space? just being paranoid */
4744 #define MIN_SPACE 75
4745 if (compl_length
> (IOSIZE
- MIN_SPACE
))
4747 compl_cont_status
&= ~CONT_SOL
;
4748 compl_length
= (IOSIZE
- MIN_SPACE
);
4749 compl_col
= curwin
->w_cursor
.col
- compl_length
;
4751 compl_cont_status
|= CONT_ADDING
| CONT_N_ADDS
;
4752 if (compl_length
< 1)
4753 compl_cont_status
&= CONT_LOCAL
;
4755 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4756 compl_cont_status
= CONT_ADDING
| CONT_N_ADDS
;
4758 compl_cont_status
= 0;
4761 compl_cont_status
&= CONT_LOCAL
;
4763 if (!(compl_cont_status
& CONT_ADDING
)) /* normal expansion */
4765 compl_cont_mode
= ctrl_x_mode
;
4766 if (ctrl_x_mode
!= 0) /* Remove LOCAL if ctrl_x_mode != 0 */
4767 compl_cont_status
= 0;
4768 compl_cont_status
|= CONT_N_ADDS
;
4769 compl_startpos
= curwin
->w_cursor
;
4770 startcol
= (int)curs_col
;
4774 /* Work out completion pattern and original text -- webb */
4775 if (ctrl_x_mode
== 0 || (ctrl_x_mode
& CTRL_X_WANT_IDENT
))
4777 if ((compl_cont_status
& CONT_SOL
)
4778 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4780 if (!(compl_cont_status
& CONT_ADDING
))
4782 while (--startcol
>= 0 && vim_isIDc(line
[startcol
]))
4784 compl_col
+= ++startcol
;
4785 compl_length
= curs_col
- startcol
;
4788 compl_pattern
= str_foldcase(line
+ compl_col
,
4789 compl_length
, NULL
, 0);
4791 compl_pattern
= vim_strnsave(line
+ compl_col
,
4793 if (compl_pattern
== NULL
)
4796 else if (compl_cont_status
& CONT_ADDING
)
4798 char_u
*prefix
= (char_u
*)"\\<";
4800 /* we need up to 2 extra chars for the prefix */
4801 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4803 if (compl_pattern
== NULL
)
4805 if (!vim_iswordp(line
+ compl_col
)
4809 vim_iswordp(mb_prevptr(line
, line
+ compl_col
))
4811 vim_iswordc(line
[compl_col
- 1])
4814 prefix
= (char_u
*)"";
4815 STRCPY((char *)compl_pattern
, prefix
);
4816 (void)quote_meta(compl_pattern
+ STRLEN(prefix
),
4817 line
+ compl_col
, compl_length
);
4819 else if (--startcol
< 0 ||
4821 !vim_iswordp(mb_prevptr(line
, line
+ startcol
+ 1))
4823 !vim_iswordc(line
[startcol
])
4827 /* Match any word of at least two chars */
4828 compl_pattern
= vim_strsave((char_u
*)"\\<\\k\\k");
4829 if (compl_pattern
== NULL
)
4831 compl_col
+= curs_col
;
4837 /* Search the point of change class of multibyte character
4838 * or not a word single byte character backward. */
4844 startcol
-= (*mb_head_off
)(line
, line
+ startcol
);
4845 base_class
= mb_get_class(line
+ startcol
);
4846 while (--startcol
>= 0)
4848 head_off
= (*mb_head_off
)(line
, line
+ startcol
);
4849 if (base_class
!= mb_get_class(line
+ startcol
4852 startcol
-= head_off
;
4857 while (--startcol
>= 0 && vim_iswordc(line
[startcol
]))
4859 compl_col
+= ++startcol
;
4860 compl_length
= (int)curs_col
- startcol
;
4861 if (compl_length
== 1)
4863 /* Only match word with at least two chars -- webb
4864 * there's no need to call quote_meta,
4865 * alloc(7) is enough -- Acevedo
4867 compl_pattern
= alloc(7);
4868 if (compl_pattern
== NULL
)
4870 STRCPY((char *)compl_pattern
, "\\<");
4871 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
, 1);
4872 STRCAT((char *)compl_pattern
, "\\k");
4876 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4878 if (compl_pattern
== NULL
)
4880 STRCPY((char *)compl_pattern
, "\\<");
4881 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
,
4886 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4888 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4889 compl_length
= (int)curs_col
- (int)compl_col
;
4890 if (compl_length
< 0) /* cursor in indent: empty pattern */
4893 compl_pattern
= str_foldcase(line
+ compl_col
, compl_length
,
4896 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4897 if (compl_pattern
== NULL
)
4900 else if (ctrl_x_mode
== CTRL_X_FILES
)
4902 while (--startcol
>= 0 && vim_isfilec(line
[startcol
]))
4904 compl_col
+= ++startcol
;
4905 compl_length
= (int)curs_col
- startcol
;
4906 compl_pattern
= addstar(line
+ compl_col
, compl_length
,
4908 if (compl_pattern
== NULL
)
4911 else if (ctrl_x_mode
== CTRL_X_CMDLINE
)
4913 compl_pattern
= vim_strnsave(line
, curs_col
);
4914 if (compl_pattern
== NULL
)
4916 set_cmd_context(&compl_xp
, compl_pattern
,
4917 (int)STRLEN(compl_pattern
), curs_col
);
4918 if (compl_xp
.xp_context
== EXPAND_UNSUCCESSFUL
4919 || compl_xp
.xp_context
== EXPAND_NOTHING
)
4920 /* No completion possible, use an empty pattern to get a
4921 * "pattern not found" message. */
4922 compl_col
= curs_col
;
4924 compl_col
= (int)(compl_xp
.xp_pattern
- compl_pattern
);
4925 compl_length
= curs_col
- compl_col
;
4927 else if (ctrl_x_mode
== CTRL_X_FUNCTION
|| ctrl_x_mode
== CTRL_X_OMNI
)
4929 #ifdef FEAT_COMPL_FUNC
4931 * Call user defined function 'completefunc' with "a:findstart"
4932 * set to 1 to obtain the length of text to use for completion.
4939 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
4941 funcname
= ctrl_x_mode
== CTRL_X_FUNCTION
4942 ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
4943 if (*funcname
== NUL
)
4945 EMSG2(_(e_notset
), ctrl_x_mode
== CTRL_X_FUNCTION
4946 ? "completefunc" : "omnifunc");
4950 args
[0] = (char_u
*)"1";
4952 pos
= curwin
->w_cursor
;
4953 col
= call_func_retnr(funcname
, 2, args
, FALSE
);
4954 curwin
->w_cursor
= pos
; /* restore the cursor position */
4959 if (compl_col
> curs_col
)
4960 compl_col
= curs_col
;
4962 /* Setup variables for completion. Need to obtain "line" again,
4963 * it may have become invalid. */
4964 line
= ml_get(curwin
->w_cursor
.lnum
);
4965 compl_length
= curs_col
- compl_col
;
4966 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4967 if (compl_pattern
== NULL
)
4971 else if (ctrl_x_mode
== CTRL_X_SPELL
)
4974 if (spell_bad_len
> 0)
4975 compl_col
= curs_col
- spell_bad_len
;
4977 compl_col
= spell_word_start(startcol
);
4978 if (compl_col
>= (colnr_T
)startcol
)
4981 compl_col
= curs_col
;
4985 spell_expand_check_cap(compl_col
);
4986 compl_length
= (int)curs_col
- compl_col
;
4988 /* Need to obtain "line" again, it may have become invalid. */
4989 line
= ml_get(curwin
->w_cursor
.lnum
);
4990 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4991 if (compl_pattern
== NULL
)
4997 EMSG2(_(e_intern2
), "ins_complete()");
5001 if (compl_cont_status
& CONT_ADDING
)
5003 edit_submode_pre
= (char_u
*)_(" Adding");
5004 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
5006 /* Insert a new line, keep indentation but ignore 'comments' */
5007 #ifdef FEAT_COMMENTS
5008 char_u
*old
= curbuf
->b_p_com
;
5010 curbuf
->b_p_com
= (char_u
*)"";
5012 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
5013 compl_startpos
.col
= compl_col
;
5015 #ifdef FEAT_COMMENTS
5016 curbuf
->b_p_com
= old
;
5019 compl_col
= curwin
->w_cursor
.col
;
5024 edit_submode_pre
= NULL
;
5025 compl_startpos
.col
= compl_col
;
5028 if (compl_cont_status
& CONT_LOCAL
)
5029 edit_submode
= (char_u
*)_(ctrl_x_msgs
[CTRL_X_LOCAL_MSG
]);
5031 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
5033 /* Always add completion for the original text. */
5034 vim_free(compl_orig_text
);
5035 compl_orig_text
= vim_strnsave(line
+ compl_col
, compl_length
);
5036 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
5037 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
5039 vim_free(compl_pattern
);
5040 compl_pattern
= NULL
;
5041 vim_free(compl_orig_text
);
5042 compl_orig_text
= NULL
;
5046 /* showmode might reset the internal line pointers, so it must
5047 * be called before line = ml_get(), or when this address is no
5048 * longer needed. -- Acevedo.
5050 edit_submode_extra
= (char_u
*)_("-- Searching...");
5051 edit_submode_highl
= HLF_COUNT
;
5053 edit_submode_extra
= NULL
;
5057 compl_shown_match
= compl_curr_match
;
5058 compl_shows_dir
= compl_direction
;
5061 * Find next match (and following matches).
5063 n
= ins_compl_next(TRUE
, ins_compl_key2count(c
), ins_compl_use_match(c
));
5065 /* may undisplay the popup menu */
5066 ins_compl_upd_pum();
5068 if (n
> 1) /* all matches have been found */
5070 compl_curr_match
= compl_shown_match
;
5071 compl_direction
= compl_shows_dir
;
5073 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5075 if (got_int
&& !global_busy
)
5081 /* we found no match if the list has only the "compl_orig_text"-entry */
5082 if (compl_first_match
== compl_first_match
->cp_next
)
5084 edit_submode_extra
= (compl_cont_status
& CONT_ADDING
)
5086 ? (char_u
*)_(e_hitend
) : (char_u
*)_(e_patnotf
);
5087 edit_submode_highl
= HLF_E
;
5088 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5089 * because we couldn't expand anything at first place, but if we used
5090 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5091 * (such as M in M'exico) if not tried already. -- Acevedo */
5092 if ( compl_length
> 1
5093 || (compl_cont_status
& CONT_ADDING
)
5094 || (ctrl_x_mode
!= 0
5095 && ctrl_x_mode
!= CTRL_X_PATH_PATTERNS
5096 && ctrl_x_mode
!= CTRL_X_PATH_DEFINES
))
5097 compl_cont_status
&= ~CONT_N_ADDS
;
5100 if (compl_curr_match
->cp_flags
& CONT_S_IPOS
)
5101 compl_cont_status
|= CONT_S_IPOS
;
5103 compl_cont_status
&= ~CONT_S_IPOS
;
5105 if (edit_submode_extra
== NULL
)
5107 if (compl_curr_match
->cp_flags
& ORIGINAL_TEXT
)
5109 edit_submode_extra
= (char_u
*)_("Back at original");
5110 edit_submode_highl
= HLF_W
;
5112 else if (compl_cont_status
& CONT_S_IPOS
)
5114 edit_submode_extra
= (char_u
*)_("Word from other line");
5115 edit_submode_highl
= HLF_COUNT
;
5117 else if (compl_curr_match
->cp_next
== compl_curr_match
->cp_prev
)
5119 edit_submode_extra
= (char_u
*)_("The only match");
5120 edit_submode_highl
= HLF_COUNT
;
5124 /* Update completion sequence number when needed. */
5125 if (compl_curr_match
->cp_number
== -1)
5130 if (compl_direction
== FORWARD
)
5132 /* search backwards for the first valid (!= -1) number.
5133 * This should normally succeed already at the first loop
5134 * cycle, so it's fast! */
5135 for (match
= compl_curr_match
->cp_prev
; match
!= NULL
5136 && match
!= compl_first_match
;
5137 match
= match
->cp_prev
)
5138 if (match
->cp_number
!= -1)
5140 number
= match
->cp_number
;
5144 /* go up and assign all numbers which are not assigned
5146 for (match
= match
->cp_next
;
5147 match
!= NULL
&& match
->cp_number
== -1;
5148 match
= match
->cp_next
)
5149 match
->cp_number
= ++number
;
5153 /* search forwards (upwards) for the first valid (!= -1)
5154 * number. This should normally succeed already at the
5155 * first loop cycle, so it's fast! */
5156 for (match
= compl_curr_match
->cp_next
; match
!= NULL
5157 && match
!= compl_first_match
;
5158 match
= match
->cp_next
)
5159 if (match
->cp_number
!= -1)
5161 number
= match
->cp_number
;
5165 /* go down and assign all numbers which are not
5167 for (match
= match
->cp_prev
; match
5168 && match
->cp_number
== -1;
5169 match
= match
->cp_prev
)
5170 match
->cp_number
= ++number
;
5174 /* The match should always have a sequence number now, this is
5175 * just a safety check. */
5176 if (compl_curr_match
->cp_number
!= -1)
5178 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5179 * Translations may need more than twice that. */
5180 static char_u match_ref
[81];
5182 if (compl_matches
> 0)
5183 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
5184 _("match %d of %d"),
5185 compl_curr_match
->cp_number
, compl_matches
);
5187 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
5189 compl_curr_match
->cp_number
);
5190 edit_submode_extra
= match_ref
;
5191 edit_submode_highl
= HLF_R
;
5193 curs_columns(FALSE
);
5198 /* Show a message about what (completion) mode we're in. */
5200 if (edit_submode_extra
!= NULL
)
5203 msg_attr(edit_submode_extra
,
5204 edit_submode_highl
< HLF_COUNT
5205 ? hl_attr(edit_submode_highl
) : 0);
5208 msg_clr_cmdline(); /* necessary for "noshowmode" */
5210 /* Show the popup menu, unless we got interrupted. */
5211 if (!compl_interrupted
)
5213 /* RedrawingDisabled may be set when invoked through complete(). */
5214 n
= RedrawingDisabled
;
5215 RedrawingDisabled
= 0;
5216 ins_compl_show_pum();
5218 RedrawingDisabled
= n
;
5220 compl_was_interrupted
= compl_interrupted
;
5221 compl_interrupted
= FALSE
;
5227 * Looks in the first "len" chars. of "src" for search-metachars.
5228 * If dest is not NULL the chars. are copied there quoting (with
5229 * a backslash) the metachars, and dest would be NUL terminated.
5230 * Returns the length (needed) of dest
5233 quote_meta(dest
, src
, len
)
5238 unsigned m
= (unsigned)len
+ 1; /* one extra for the NUL */
5240 for ( ; --len
>= 0; src
++)
5247 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5248 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5251 if (!p_magic
) /* quote these only if magic is set */
5254 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5255 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5257 case '^': /* currently it's not needed. */
5267 /* Copy remaining bytes of a multibyte character. */
5272 mb_len
= (*mb_ptr2len
)(src
) - 1;
5273 if (mb_len
> 0 && len
>= mb_len
)
5274 for (i
= 0; i
< mb_len
; ++i
)
5289 #endif /* FEAT_INS_EXPAND */
5292 * Next character is interpreted literally.
5293 * A one, two or three digit decimal number is interpreted as its byte value.
5294 * If one or two digits are entered, the next character is given to vungetc().
5295 * For Unicode a character > 255 may be returned.
5314 * In GUI there is no point inserting the internal code for a special key.
5315 * It is more useful to insert the string "<KEY>" instead. This would
5316 * probably be useful in a text window too, but it would not be
5317 * vi-compatible (maybe there should be an option for it?) -- webb
5322 #ifdef USE_ON_FLY_SCROLL
5323 dont_scroll
= TRUE
; /* disallow scrolling here */
5325 ++no_mapping
; /* don't map the next key hits */
5331 #ifdef FEAT_CMDL_INFO
5332 if (!(State
& CMDLINE
)
5334 && MB_BYTE2LEN_CHECK(nc
) == 1
5339 if (nc
== 'x' || nc
== 'X')
5341 else if (nc
== 'o' || nc
== 'O')
5344 else if (nc
== 'u' || nc
== 'U')
5355 if (!vim_isxdigit(nc
))
5357 cc
= cc
* 16 + hex2nr(nc
);
5361 if (nc
< '0' || nc
> '7')
5363 cc
= cc
* 8 + nc
- '0';
5367 if (!VIM_ISDIGIT(nc
))
5369 cc
= cc
* 10 + nc
- '0';
5380 cc
= 255; /* limit range to 0-255 */
5383 if (hex
) /* hex: up to two chars */
5389 else if (unicode
) /* Unicode: up to four or eight chars */
5391 if ((unicode
== 'u' && i
>= 4) || (unicode
== 'U' && i
>= 8))
5395 else if (i
>= 3) /* decimal or octal: up to three chars */
5398 if (i
== 0) /* no number entered */
5400 if (nc
== K_ZERO
) /* NUL is stored as NL */
5412 if (cc
== 0) /* NUL is stored as NL */
5415 if (enc_dbcs
&& (cc
& 0xff) == 0)
5416 cc
= '?'; /* don't accept an illegal DBCS char, the NUL in the
5417 second byte will cause trouble! */
5427 got_int
= FALSE
; /* CTRL-C typed after CTRL-V is not an interrupt */
5432 * Insert character, taking care of special keys and mod_mask
5435 insert_special(c
, allow_modmask
, ctrlv
)
5438 int ctrlv
; /* c was typed after CTRL-V */
5444 * Special function key, translate into "<Key>". Up to the last '>' is
5445 * inserted with ins_str(), so as not to replace characters in replace
5447 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5448 * unless 'allow_modmask' is TRUE.
5451 /* Command-key never produces a normal key */
5452 if (mod_mask
& MOD_MASK_CMD
)
5453 allow_modmask
= TRUE
;
5455 if (IS_SPECIAL(c
) || (mod_mask
&& allow_modmask
))
5457 p
= get_special_key_name(c
, mod_mask
);
5458 len
= (int)STRLEN(p
);
5462 if (stop_arrow() == FAIL
)
5466 AppendToRedobuffLit(p
, -1);
5470 if (stop_arrow() == OK
)
5471 insertchar(c
, ctrlv
? INSCHAR_CTRLV
: 0, -1);
5475 * Special characters in this context are those that need processing other
5476 * than the simple insertion that can be performed here. This includes ESC
5477 * which terminates the insert, and CR/NL which need special processing to
5478 * open up a new line. This routine tries to optimize insertions performed by
5479 * the "redo", "undo" or "put" commands, so it needs to know when it should
5480 * stop and defer processing to the "normal" mechanism.
5481 * '0' and '^' are special, because they can be followed by CTRL-D.
5484 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5486 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5490 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5492 # define WHITECHAR(cc) vim_iswhite(cc)
5496 insertchar(c
, flags
, second_indent
)
5497 int c
; /* character to insert or NUL */
5498 int flags
; /* INSCHAR_FORMAT, etc. */
5499 int second_indent
; /* indent for second line if >= 0 */
5502 #ifdef FEAT_COMMENTS
5507 textwidth
= comp_textwidth(flags
& INSCHAR_FORMAT
);
5508 fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5511 * Try to break the line in two or more pieces when:
5512 * - Always do this if we have been called to do formatting only.
5513 * - Always do this when 'formatoptions' has the 'a' flag and the line
5514 * ends in white space.
5516 * - Don't do this if inserting a blank
5517 * - Don't do this if an existing character is being replaced, unless
5518 * we're in VREPLACE mode.
5519 * - Do this if the cursor is not on the line where insert started
5520 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5521 * before the insert.
5522 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5523 * before 'textwidth'
5526 && ((flags
& INSCHAR_FORMAT
)
5528 && !((State
& REPLACE_FLAG
)
5529 #ifdef FEAT_VREPLACE
5530 && !(State
& VREPLACE_FLAG
)
5532 && *ml_get_cursor() != NUL
)
5533 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
5534 || ((!has_format_option(FO_INS_LONG
)
5535 || Insstart_textlen
<= (colnr_T
)textwidth
)
5537 || Insstart_blank_vcol
<= (colnr_T
)textwidth
5540 /* Format with 'formatexpr' when it's set. Use internal formatting
5541 * when 'formatexpr' isn't set or it returns non-zero. */
5542 #if defined(FEAT_EVAL)
5543 int do_internal
= TRUE
;
5545 if (*curbuf
->b_p_fex
!= NUL
&& (flags
& INSCHAR_NO_FEX
) == 0)
5547 do_internal
= (fex_format(curwin
->w_cursor
.lnum
, 1L, c
) != 0);
5548 /* It may be required to save for undo again, e.g. when setline()
5550 ins_need_undo
= TRUE
;
5554 internal_format(textwidth
, second_indent
, flags
, c
== NUL
);
5557 if (c
== NUL
) /* only formatting was wanted */
5560 #ifdef FEAT_COMMENTS
5561 /* Check whether this character should end a comment. */
5562 if (did_ai
&& (int)c
== end_comment_pending
)
5565 char_u lead_end
[COM_MAX_LEN
]; /* end-comment string */
5566 int middle_len
, end_len
;
5570 * Need to remove existing (middle) comment leader and insert end
5571 * comment leader. First, check what comment leader we can find.
5573 i
= get_leader_len(line
= ml_get_curline(), &p
, FALSE
);
5574 if (i
> 0 && vim_strchr(p
, COM_MIDDLE
) != NULL
) /* Just checking */
5576 /* Skip middle-comment string */
5577 while (*p
&& p
[-1] != ':') /* find end of middle flags */
5579 middle_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5580 /* Don't count trailing white space for middle_len */
5581 while (middle_len
> 0 && vim_iswhite(lead_end
[middle_len
- 1]))
5584 /* Find the end-comment string */
5585 while (*p
&& p
[-1] != ':') /* find end of end flags */
5587 end_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5589 /* Skip white space before the cursor */
5590 i
= curwin
->w_cursor
.col
;
5591 while (--i
>= 0 && vim_iswhite(line
[i
]))
5595 /* Skip to before the middle leader */
5598 /* Check some expected things before we go on */
5599 if (i
>= 0 && lead_end
[end_len
- 1] == end_comment_pending
)
5601 /* Backspace over all the stuff we want to replace */
5602 backspace_until_column(i
);
5605 * Insert the end-comment string, except for the last
5606 * character, which will get inserted as normal later.
5608 ins_bytes_len(lead_end
, end_len
- 1);
5612 end_comment_pending
= NUL
;
5616 #ifdef FEAT_SMARTINDENT
5619 can_si_back
= FALSE
;
5623 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5624 * This speeds up normal text input considerably.
5625 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5626 * need to re-indent at a ':', or any other character (but not what
5629 #ifdef USE_ON_FLY_SCROLL
5630 dont_scroll
= FALSE
; /* allow scrolling here */
5635 && (!has_mbyte
|| (*mb_char2len
)(c
) == 1)
5638 && !(State
& REPLACE_FLAG
)
5642 #ifdef FEAT_RIGHTLEFT
5647 #define INPUT_BUFLEN 100
5648 char_u buf
[INPUT_BUFLEN
+ 1];
5650 colnr_T virtcol
= 0;
5655 virtcol
= get_nolist_virtcol();
5657 * Stop the string when:
5658 * - no more chars available
5659 * - finding a special character (command key)
5661 * - running into the 'textwidth' boundary
5662 * - need to check for abbreviation: A non-word char after a word-char
5664 while ( (c
= vpeekc()) != NUL
5667 && (!has_mbyte
|| MB_BYTE2LEN_CHECK(c
) == 1)
5671 || (virtcol
+= byte2cells(buf
[i
- 1])) < (colnr_T
)textwidth
)
5672 && !(!no_abbr
&& !vim_iswordc(c
) && vim_iswordc(buf
[i
- 1])))
5674 #ifdef FEAT_RIGHTLEFT
5676 if (p_hkmap
&& KeyTyped
)
5677 c
= hkmap(c
); /* Hebrew mode mapping */
5679 if (p_fkmap
&& KeyTyped
)
5680 c
= fkmap(c
); /* Farsi mode mapping */
5688 #ifdef FEAT_DIGRAPHS
5689 do_digraph(-1); /* clear digraphs */
5690 do_digraph(buf
[i
-1]); /* may be the start of a digraph */
5694 if (flags
& INSCHAR_CTRLV
)
5702 AppendToRedobuffLit(buf
+ i
, -1);
5709 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
5711 char_u buf
[MB_MAXBYTES
+ 1];
5713 (*mb_char2bytes
)(c
, buf
);
5715 ins_char_bytes(buf
, cc
);
5716 AppendCharToRedobuff(c
);
5722 if (flags
& INSCHAR_CTRLV
)
5725 AppendCharToRedobuff(c
);
5731 * Format text at the current insert position.
5734 internal_format(textwidth
, second_indent
, flags
, format_only
)
5741 int save_char
= NUL
;
5742 int haveto_redraw
= FALSE
;
5743 int fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5745 int fo_multibyte
= has_format_option(FO_MBYTE_BREAK
);
5747 int fo_white_par
= has_format_option(FO_WHITE_PAR
);
5748 int first_line
= TRUE
;
5749 #ifdef FEAT_COMMENTS
5751 int no_leader
= FALSE
;
5752 int do_comments
= (flags
& INSCHAR_DO_COM
);
5756 * When 'ai' is off we don't want a space under the cursor to be
5757 * deleted. Replace it with an 'x' temporarily.
5759 if (!curbuf
->b_p_ai
)
5761 cc
= gchar_cursor();
5762 if (vim_iswhite(cc
))
5770 * Repeat breaking lines, until the current line is not too long.
5774 int startcol
; /* Cursor column at entry */
5775 int wantcol
; /* column at textwidth border */
5776 int foundcol
; /* column for start of spaces */
5777 int end_foundcol
= 0; /* column for start of word */
5780 #ifdef FEAT_VREPLACE
5782 char_u
*saved_text
= NULL
;
5786 virtcol
= get_nolist_virtcol();
5787 if (virtcol
< (colnr_T
)textwidth
)
5790 #ifdef FEAT_COMMENTS
5792 do_comments
= FALSE
;
5793 else if (!(flags
& INSCHAR_FORMAT
)
5794 && has_format_option(FO_WRAP_COMS
))
5797 /* Don't break until after the comment leader */
5799 leader_len
= get_leader_len(ml_get_curline(), NULL
, FALSE
);
5803 /* If the line doesn't start with a comment leader, then don't
5804 * start one in a following broken line. Avoids that a %word
5805 * moved to the start of the next line causes all following lines
5806 * to start with %. */
5807 if (leader_len
== 0)
5810 if (!(flags
& INSCHAR_FORMAT
)
5811 #ifdef FEAT_COMMENTS
5814 && !has_format_option(FO_WRAP
))
5820 if ((startcol
= curwin
->w_cursor
.col
) == 0)
5823 /* find column of textwidth border */
5824 coladvance((colnr_T
)textwidth
);
5825 wantcol
= curwin
->w_cursor
.col
;
5827 curwin
->w_cursor
.col
= startcol
- 1;
5829 /* Correct cursor for multi-byte character. */
5836 * Find position to break at.
5837 * Stop at first entered white when 'formatoptions' has 'v'
5839 while ((!fo_ins_blank
&& !has_format_option(FO_INS_VI
))
5840 || curwin
->w_cursor
.lnum
!= Insstart
.lnum
5841 || curwin
->w_cursor
.col
>= Insstart
.col
)
5843 cc
= gchar_cursor();
5846 /* remember position of blank just before text */
5847 end_foundcol
= curwin
->w_cursor
.col
;
5849 /* find start of sequence of blanks */
5850 while (curwin
->w_cursor
.col
> 0 && WHITECHAR(cc
))
5853 cc
= gchar_cursor();
5855 if (curwin
->w_cursor
.col
== 0 && WHITECHAR(cc
))
5856 break; /* only spaces in front of text */
5857 #ifdef FEAT_COMMENTS
5858 /* Don't break until after the comment leader */
5859 if (curwin
->w_cursor
.col
< leader_len
)
5862 if (has_format_option(FO_ONE_LETTER
))
5864 /* do not break after one-letter words */
5865 if (curwin
->w_cursor
.col
== 0)
5866 break; /* one-letter word at begin */
5868 col
= curwin
->w_cursor
.col
;
5870 cc
= gchar_cursor();
5873 continue; /* one-letter, continue */
5874 curwin
->w_cursor
.col
= col
;
5878 foundcol
= curwin
->w_cursor
.col
5879 + (*mb_ptr2len
)(ml_get_cursor());
5882 foundcol
= curwin
->w_cursor
.col
+ 1;
5883 if (curwin
->w_cursor
.col
< (colnr_T
)wantcol
)
5887 else if (cc
>= 0x100 && fo_multibyte
5888 && curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5890 /* Break after or before a multi-byte character. */
5891 foundcol
= curwin
->w_cursor
.col
;
5892 if (curwin
->w_cursor
.col
< (colnr_T
)wantcol
)
5893 foundcol
+= (*mb_char2len
)(cc
);
5894 end_foundcol
= foundcol
;
5898 if (curwin
->w_cursor
.col
== 0)
5903 if (foundcol
== 0) /* no spaces, cannot break line */
5905 curwin
->w_cursor
.col
= startcol
;
5909 /* Going to break the line, remove any "$" now. */
5913 * Offset between cursor position and line break is used by replace
5914 * stack functions. VREPLACE does not use this, and backspaces
5915 * over the text instead.
5917 #ifdef FEAT_VREPLACE
5918 if (State
& VREPLACE_FLAG
)
5919 orig_col
= startcol
; /* Will start backspacing from here */
5922 replace_offset
= startcol
- end_foundcol
- 1;
5925 * adjust startcol for spaces that will be deleted and
5926 * characters that will remain on top line
5928 curwin
->w_cursor
.col
= foundcol
;
5929 while (cc
= gchar_cursor(), WHITECHAR(cc
))
5931 startcol
-= curwin
->w_cursor
.col
;
5935 #ifdef FEAT_VREPLACE
5936 if (State
& VREPLACE_FLAG
)
5939 * In VREPLACE mode, we will backspace over the text to be
5940 * wrapped, so save a copy now to put on the next line.
5942 saved_text
= vim_strsave(ml_get_cursor());
5943 curwin
->w_cursor
.col
= orig_col
;
5944 if (saved_text
== NULL
)
5945 break; /* Can't do it, out of memory */
5946 saved_text
[startcol
] = NUL
;
5948 /* Backspace over characters that will move to the next line */
5950 backspace_until_column(foundcol
);
5955 /* put cursor after pos. to break line */
5957 curwin
->w_cursor
.col
= foundcol
;
5961 * Split the line just before the margin.
5962 * Only insert/delete lines, but don't really redraw the window.
5964 open_line(FORWARD
, OPENLINE_DELSPACES
+ OPENLINE_MARKFIX
5965 + (fo_white_par
? OPENLINE_KEEPTRAIL
: 0)
5966 #ifdef FEAT_COMMENTS
5967 + (do_comments
? OPENLINE_DO_COM
: 0)
5975 if (second_indent
< 0 && has_format_option(FO_Q_NUMBER
))
5976 second_indent
= get_number_indent(curwin
->w_cursor
.lnum
-1);
5977 if (second_indent
>= 0)
5979 #ifdef FEAT_VREPLACE
5980 if (State
& VREPLACE_FLAG
)
5981 change_indent(INDENT_SET
, second_indent
, FALSE
, NUL
, TRUE
);
5984 (void)set_indent(second_indent
, SIN_CHANGED
);
5989 #ifdef FEAT_VREPLACE
5990 if (State
& VREPLACE_FLAG
)
5993 * In VREPLACE mode we have backspaced over the text to be
5994 * moved, now we re-insert it into the new line.
5996 ins_bytes(saved_text
);
5997 vim_free(saved_text
);
6003 * Check if cursor is not past the NUL off the line, cindent
6004 * may have added or removed indent.
6006 curwin
->w_cursor
.col
+= startcol
;
6007 len
= (colnr_T
)STRLEN(ml_get_curline());
6008 if (curwin
->w_cursor
.col
> len
)
6009 curwin
->w_cursor
.col
= len
;
6012 haveto_redraw
= TRUE
;
6016 /* moved the cursor, don't autoindent or cindent now */
6018 #ifdef FEAT_SMARTINDENT
6021 can_si_back
= FALSE
;
6026 if (save_char
!= NUL
) /* put back space after cursor */
6027 pchar_cursor(save_char
);
6029 if (!format_only
&& haveto_redraw
)
6032 redraw_curbuf_later(VALID
);
6037 * Called after inserting or deleting text: When 'formatoptions' includes the
6038 * 'a' flag format from the current line until the end of the paragraph.
6039 * Keep the cursor at the same position relative to the text.
6040 * The caller must have saved the cursor line for undo, following ones will be
6044 auto_format(trailblank
, prev_line
)
6045 int trailblank
; /* when TRUE also format with trailing blank */
6046 int prev_line
; /* may start in previous line */
6055 if (!has_format_option(FO_AUTO
))
6058 pos
= curwin
->w_cursor
;
6059 old
= ml_get_curline();
6061 /* may remove added space */
6062 check_auto_format(FALSE
);
6064 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6065 * user might insert normal text next. Also skip formatting when "1" is
6066 * in 'formatoptions' and there is a single character before the cursor.
6067 * Otherwise the line would be broken and when typing another non-white
6068 * next they are not joined back together. */
6069 wasatend
= (pos
.col
== (colnr_T
)STRLEN(old
));
6070 if (*old
!= NUL
&& !trailblank
&& wasatend
)
6073 cc
= gchar_cursor();
6074 if (!WHITECHAR(cc
) && curwin
->w_cursor
.col
> 0
6075 && has_format_option(FO_ONE_LETTER
))
6077 cc
= gchar_cursor();
6080 curwin
->w_cursor
= pos
;
6083 curwin
->w_cursor
= pos
;
6086 #ifdef FEAT_COMMENTS
6087 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6089 if (has_format_option(FO_WRAP_COMS
) && !has_format_option(FO_WRAP
)
6090 && get_leader_len(old
, NULL
, FALSE
) == 0)
6095 * May start formatting in a previous line, so that after "x" a word is
6096 * moved to the previous line if it fits there now. Only when this is not
6097 * the start of a paragraph.
6099 if (prev_line
&& !paragraph_start(curwin
->w_cursor
.lnum
))
6101 --curwin
->w_cursor
.lnum
;
6102 if (u_save_cursor() == FAIL
)
6107 * Do the formatting and restore the cursor position. "saved_cursor" will
6108 * be adjusted for the text formatting.
6111 format_lines((linenr_T
)-1, FALSE
);
6112 curwin
->w_cursor
= saved_cursor
;
6113 saved_cursor
.lnum
= 0;
6115 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6117 /* "cannot happen" */
6118 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6119 coladvance((colnr_T
)MAXCOL
);
6124 /* Insert mode: If the cursor is now after the end of the line while it
6125 * previously wasn't, the line was broken. Because of the rule above we
6126 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6128 if (!wasatend
&& has_format_option(FO_WHITE_PAR
))
6130 new = ml_get_curline();
6131 len
= (colnr_T
)STRLEN(new);
6132 if (curwin
->w_cursor
.col
== len
)
6134 pnew
= vim_strnsave(new, len
+ 2);
6136 pnew
[len
+ 1] = NUL
;
6137 ml_replace(curwin
->w_cursor
.lnum
, pnew
, FALSE
);
6138 /* remove the space later */
6139 did_add_space
= TRUE
;
6142 /* may remove added space */
6143 check_auto_format(FALSE
);
6150 * When an extra space was added to continue a paragraph for auto-formatting,
6151 * delete it now. The space must be under the cursor, just after the insert
6155 check_auto_format(end_insert
)
6156 int end_insert
; /* TRUE when ending Insert mode */
6163 cc
= gchar_cursor();
6165 /* Somehow the space was removed already. */
6166 did_add_space
= FALSE
;
6177 /* The space is no longer at the end of the line, delete it. */
6179 did_add_space
= FALSE
;
6186 * Find out textwidth to be used for formatting:
6187 * if 'textwidth' option is set, use it
6188 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6189 * if invalid value, use 0.
6190 * Set default to window width (maximum 79) for "gq" operator.
6194 int ff
; /* force formatting (for "gq" command) */
6198 textwidth
= curbuf
->b_p_tw
;
6199 if (textwidth
== 0 && curbuf
->b_p_wm
)
6201 /* The width is the window width minus 'wrapmargin' minus all the
6202 * things that add to the margin. */
6203 textwidth
= W_WIDTH(curwin
) - curbuf
->b_p_wm
;
6205 if (cmdwin_type
!= 0)
6209 textwidth
-= curwin
->w_p_fdc
;
6212 if (curwin
->w_buffer
->b_signlist
!= NULL
6213 # ifdef FEAT_NETBEANS_INTG
6224 if (ff
&& textwidth
== 0)
6226 textwidth
= W_WIDTH(curwin
) - 1;
6234 * Put a character in the redo buffer, for when just after a CTRL-V.
6242 /* Only digits need special treatment. Translate them into a string of
6246 vim_snprintf((char *)buf
, sizeof(buf
), "%03d", c
);
6247 AppendToRedobuff(buf
);
6250 AppendCharToRedobuff(c
);
6254 * start_arrow() is called when an arrow key is used in insert mode.
6255 * For undo/redo it resembles hitting the <ESC> key.
6258 start_arrow(end_insert_pos
)
6259 pos_T
*end_insert_pos
; /* can be NULL */
6261 if (!arrow_used
) /* something has been inserted */
6263 AppendToRedobuff(ESC_STR
);
6264 stop_insert(end_insert_pos
, FALSE
);
6265 arrow_used
= TRUE
; /* this means we stopped the current insert */
6268 check_spell_redraw();
6274 * If we skipped highlighting word at cursor, do it now.
6275 * It may be skipped again, thus reset spell_redraw_lnum first.
6278 check_spell_redraw()
6280 if (spell_redraw_lnum
!= 0)
6282 linenr_T lnum
= spell_redraw_lnum
;
6284 spell_redraw_lnum
= 0;
6285 redrawWinline(lnum
, FALSE
);
6290 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6291 * spelled word, if there is one.
6294 spell_back_to_badword()
6296 pos_T tpos
= curwin
->w_cursor
;
6298 spell_bad_len
= spell_move_to(curwin
, BACKWARD
, TRUE
, TRUE
, NULL
);
6299 if (curwin
->w_cursor
.col
!= tpos
.col
)
6305 * stop_arrow() is called before a change is made in insert mode.
6306 * If an arrow key has been used, start a new insertion.
6307 * Returns FAIL if undo is impossible, shouldn't insert then.
6314 if (u_save_cursor() == OK
)
6317 ins_need_undo
= FALSE
;
6319 Insstart
= curwin
->w_cursor
; /* new insertion starts here */
6320 Insstart_textlen
= (colnr_T
)linetabsize(ml_get_curline());
6322 #ifdef FEAT_VREPLACE
6323 if (State
& VREPLACE_FLAG
)
6325 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
6326 vr_lines_changed
= 1;
6330 AppendToRedobuff((char_u
*)"1i"); /* pretend we start an insertion */
6331 new_insert_skip
= 2;
6333 else if (ins_need_undo
)
6335 if (u_save_cursor() == OK
)
6336 ins_need_undo
= FALSE
;
6340 /* Always open fold at the cursor line when inserting something. */
6344 return (arrow_used
|| ins_need_undo
? FAIL
: OK
);
6348 * Do a few things to stop inserting.
6349 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6350 * to another window/buffer.
6353 stop_insert(end_insert_pos
, esc
)
6354 pos_T
*end_insert_pos
;
6355 int esc
; /* called by ins_esc() */
6361 replace_flush(); /* abandon replace stack */
6364 * Save the inserted text for later redo with ^@ and CTRL-A.
6365 * Don't do it when "restart_edit" was set and nothing was inserted,
6366 * otherwise CTRL-O w and then <Left> will clear "last_insert".
6368 ptr
= get_inserted();
6369 if (did_restart_edit
== 0 || (ptr
!= NULL
6370 && (int)STRLEN(ptr
) > new_insert_skip
))
6372 vim_free(last_insert
);
6374 last_insert_skip
= new_insert_skip
;
6379 if (!arrow_used
&& end_insert_pos
!= NULL
)
6381 /* Auto-format now. It may seem strange to do this when stopping an
6382 * insertion (or moving the cursor), but it's required when appending
6383 * a line and having it end in a space. But only do it when something
6384 * was actually inserted, otherwise undo won't work. */
6385 if (!ins_need_undo
&& has_format_option(FO_AUTO
))
6387 pos_T tpos
= curwin
->w_cursor
;
6389 /* When the cursor is at the end of the line after a space the
6390 * formatting will move it to the following word. Avoid that by
6391 * moving the cursor onto the space. */
6393 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
)
6396 cc
= gchar_cursor();
6397 if (!vim_iswhite(cc
))
6398 curwin
->w_cursor
= tpos
;
6401 auto_format(TRUE
, FALSE
);
6403 if (vim_iswhite(cc
))
6405 if (gchar_cursor() != NUL
)
6407 #ifdef FEAT_VIRTUALEDIT
6408 /* If the cursor is still at the same character, also keep
6410 if (gchar_cursor() == NUL
6411 && curwin
->w_cursor
.lnum
== tpos
.lnum
6412 && curwin
->w_cursor
.col
== tpos
.col
)
6413 curwin
->w_cursor
.coladd
= tpos
.coladd
;
6418 /* If a space was inserted for auto-formatting, remove it now. */
6419 check_auto_format(TRUE
);
6421 /* If we just did an auto-indent, remove the white space from the end
6422 * of the line, and put the cursor back.
6423 * Do this when ESC was used or moving the cursor up/down.
6424 * Check for the old position still being valid, just in case the text
6425 * got changed unexpectedly. */
6426 if (did_ai
&& (esc
|| (vim_strchr(p_cpo
, CPO_INDENT
) == NULL
6427 && curwin
->w_cursor
.lnum
!= end_insert_pos
->lnum
))
6428 && end_insert_pos
->lnum
<= curbuf
->b_ml
.ml_line_count
)
6430 pos_T tpos
= curwin
->w_cursor
;
6432 curwin
->w_cursor
= *end_insert_pos
;
6433 check_cursor_col(); /* make sure it is not past the line */
6436 if (gchar_cursor() == NUL
&& curwin
->w_cursor
.col
> 0)
6437 --curwin
->w_cursor
.col
;
6438 cc
= gchar_cursor();
6439 if (!vim_iswhite(cc
))
6441 if (del_char(TRUE
) == FAIL
)
6442 break; /* should not happen */
6444 if (curwin
->w_cursor
.lnum
!= tpos
.lnum
)
6445 curwin
->w_cursor
= tpos
;
6447 ++curwin
->w_cursor
.col
; /* put cursor back on the NUL */
6450 /* <C-S-Right> may have started Visual mode, adjust the position for
6451 * deleted characters. */
6452 if (VIsual_active
&& VIsual
.lnum
== curwin
->w_cursor
.lnum
)
6454 int len
= (int)STRLEN(ml_get_curline());
6456 if (VIsual
.col
> len
)
6459 # ifdef FEAT_VIRTUALEDIT
6468 #ifdef FEAT_SMARTINDENT
6471 can_si_back
= FALSE
;
6474 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6475 * now in a different buffer. */
6476 if (end_insert_pos
!= NULL
)
6478 curbuf
->b_op_start
= Insstart
;
6479 curbuf
->b_op_end
= *end_insert_pos
;
6484 * Set the last inserted text to a single character.
6485 * Used for the replace command.
6493 vim_free(last_insert
);
6495 last_insert
= alloc(MB_MAXBYTES
* 3 + 5);
6497 last_insert
= alloc(6);
6499 if (last_insert
!= NULL
)
6502 /* Use the CTRL-V only when entering a special char */
6503 if (c
< ' ' || c
== DEL
)
6505 s
= add_char2buf(c
, s
);
6508 last_insert_skip
= 0;
6512 #if defined(EXITFREE) || defined(PROTO)
6516 vim_free(last_insert
);
6518 # ifdef FEAT_INS_EXPAND
6519 vim_free(compl_orig_text
);
6520 compl_orig_text
= NULL
;
6526 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6527 * and CSI. Handle multi-byte characters.
6528 * Returns a pointer to after the added bytes.
6536 char_u temp
[MB_MAXBYTES
];
6540 len
= (*mb_char2bytes
)(c
, temp
);
6541 for (i
= 0; i
< len
; ++i
)
6545 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6569 * move cursor to start of line
6570 * if flags & BL_WHITE move to first non-white
6571 * if flags & BL_SOL move to first non-white if startofline is set,
6572 * otherwise keep "curswant" column
6573 * if flags & BL_FIX don't leave the cursor on a NUL.
6579 if ((flags
& BL_SOL
) && !p_sol
)
6580 coladvance(curwin
->w_curswant
);
6583 curwin
->w_cursor
.col
= 0;
6584 #ifdef FEAT_VIRTUALEDIT
6585 curwin
->w_cursor
.coladd
= 0;
6588 if (flags
& (BL_WHITE
| BL_SOL
))
6592 for (ptr
= ml_get_curline(); vim_iswhite(*ptr
)
6593 && !((flags
& BL_FIX
) && ptr
[1] == NUL
); ++ptr
)
6594 ++curwin
->w_cursor
.col
;
6596 curwin
->w_set_curswant
= TRUE
;
6601 * oneright oneleft cursor_down cursor_up
6603 * Move one char {right,left,down,up}.
6604 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
6605 * Return OK when successful, FAIL when we hit a line of file boundary.
6614 #ifdef FEAT_VIRTUALEDIT
6615 if (virtual_active())
6617 pos_T prevpos
= curwin
->w_cursor
;
6619 /* Adjust for multi-wide char (excluding TAB) */
6620 ptr
= ml_get_cursor();
6621 coladvance(getviscol() + ((*ptr
!= TAB
&& vim_isprintc(
6628 ? ptr2cells(ptr
) : 1));
6629 curwin
->w_set_curswant
= TRUE
;
6630 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6631 return (prevpos
.col
!= curwin
->w_cursor
.col
6632 || prevpos
.coladd
!= curwin
->w_cursor
.coladd
) ? OK
: FAIL
;
6636 ptr
= ml_get_cursor();
6638 return FAIL
; /* already at the very end */
6642 l
= (*mb_ptr2len
)(ptr
);
6647 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6648 * contains "onemore". */
6650 #ifdef FEAT_VIRTUALEDIT
6651 && (ve_flags
& VE_ONEMORE
) == 0
6655 curwin
->w_cursor
.col
+= l
;
6657 curwin
->w_set_curswant
= TRUE
;
6664 #ifdef FEAT_VIRTUALEDIT
6665 if (virtual_active())
6668 int v
= getviscol();
6673 # ifdef FEAT_LINEBREAK
6674 /* We might get stuck on 'showbreak', skip over it. */
6678 coladvance(v
- width
);
6679 /* getviscol() is slow, skip it when 'showbreak' is empty and
6680 * there are no multi-byte characters */
6685 ) || getviscol() < v
)
6693 if (curwin
->w_cursor
.coladd
== 1)
6697 /* Adjust for multi-wide char (not a TAB) */
6698 ptr
= ml_get_cursor();
6699 if (*ptr
!= TAB
&& vim_isprintc(
6705 ) && ptr2cells(ptr
) > 1)
6706 curwin
->w_cursor
.coladd
= 0;
6709 curwin
->w_set_curswant
= TRUE
;
6714 if (curwin
->w_cursor
.col
== 0)
6717 curwin
->w_set_curswant
= TRUE
;
6718 --curwin
->w_cursor
.col
;
6721 /* if the character on the left of the current cursor is a multi-byte
6722 * character, move to its first byte */
6730 cursor_up(n
, upd_topline
)
6732 int upd_topline
; /* When TRUE: update topline */
6738 lnum
= curwin
->w_cursor
.lnum
;
6739 /* This fails if the cursor is already in the first line or the count
6740 * is larger than the line number and '-' is in 'cpoptions' */
6741 if (lnum
<= 1 || (n
>= lnum
&& vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6747 if (hasAnyFolding(curwin
))
6750 * Count each sequence of folded lines as one logical line.
6752 /* go to the the start of the current fold */
6753 (void)hasFolding(lnum
, &lnum
, NULL
);
6757 /* move up one line */
6761 /* If we entered a fold, move to the beginning, unless in
6762 * Insert mode or when 'foldopen' contains "all": it will open
6764 if (n
> 0 || !((State
& INSERT
) || (fdo_flags
& FDO_ALL
)))
6765 (void)hasFolding(lnum
, &lnum
, NULL
);
6773 curwin
->w_cursor
.lnum
= lnum
;
6776 /* try to advance to the column we want to be at */
6777 coladvance(curwin
->w_curswant
);
6780 update_topline(); /* make sure curwin->w_topline is valid */
6786 * Cursor down a number of logical lines.
6789 cursor_down(n
, upd_topline
)
6791 int upd_topline
; /* When TRUE: update topline */
6797 lnum
= curwin
->w_cursor
.lnum
;
6799 /* Move to last line of fold, will fail if it's the end-of-file. */
6800 (void)hasFolding(lnum
, NULL
, &lnum
);
6802 /* This fails if the cursor is already in the last line or would move
6803 * beyound the last line and '-' is in 'cpoptions' */
6804 if (lnum
>= curbuf
->b_ml
.ml_line_count
6805 || (lnum
+ n
> curbuf
->b_ml
.ml_line_count
6806 && vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6808 if (lnum
+ n
>= curbuf
->b_ml
.ml_line_count
)
6809 lnum
= curbuf
->b_ml
.ml_line_count
;
6812 if (hasAnyFolding(curwin
))
6816 /* count each sequence of folded lines as one logical line */
6819 if (hasFolding(lnum
, NULL
, &last
))
6823 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
6826 if (lnum
> curbuf
->b_ml
.ml_line_count
)
6827 lnum
= curbuf
->b_ml
.ml_line_count
;
6832 curwin
->w_cursor
.lnum
= lnum
;
6835 /* try to advance to the column we want to be at */
6836 coladvance(curwin
->w_curswant
);
6839 update_topline(); /* make sure curwin->w_topline is valid */
6845 * Stuff the last inserted text in the read buffer.
6846 * Last_insert actually is a copy of the redo buffer, so we
6847 * first have to remove the command.
6850 stuff_inserted(c
, count
, no_esc
)
6851 int c
; /* Command character to be inserted */
6852 long count
; /* Repeat this many times */
6853 int no_esc
; /* Don't add an ESC at the end */
6860 ptr
= get_last_insert();
6863 EMSG(_(e_noinstext
));
6867 /* may want to stuff the command character, to start Insert mode */
6869 stuffcharReadbuff(c
);
6870 if ((esc_ptr
= (char_u
*)vim_strrchr(ptr
, ESC
)) != NULL
)
6871 *esc_ptr
= NUL
; /* remove the ESC */
6873 /* when the last char is either "0" or "^" it will be quoted if no ESC
6874 * comes after it OR if it will inserted more than once and "ptr"
6875 * starts with ^D. -- Acevedo
6877 last_ptr
= (esc_ptr
? esc_ptr
: ptr
+ STRLEN(ptr
)) - 1;
6878 if (last_ptr
>= ptr
&& (*last_ptr
== '0' || *last_ptr
== '^')
6879 && (no_esc
|| (*ptr
== Ctrl_D
&& count
> 1)))
6888 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6890 stuffReadbuff((char_u
*)(last
== '0'
6891 ? IF_EB("\026\060\064\070", CTRL_V_STR
"xf0")
6892 : IF_EB("\026^", CTRL_V_STR
"^")));
6894 while (--count
> 0);
6899 if (esc_ptr
!= NULL
)
6900 *esc_ptr
= ESC
; /* put the ESC back */
6902 /* may want to stuff a trailing ESC, to get out of Insert mode */
6904 stuffcharReadbuff(ESC
);
6912 if (last_insert
== NULL
)
6914 return last_insert
+ last_insert_skip
;
6918 * Get last inserted string, and remove trailing <Esc>.
6919 * Returns pointer to allocated memory (must be freed) or NULL.
6922 get_last_insert_save()
6927 if (last_insert
== NULL
)
6929 s
= vim_strsave(last_insert
+ last_insert_skip
);
6932 len
= (int)STRLEN(s
);
6933 if (len
> 0 && s
[len
- 1] == ESC
) /* remove trailing ESC */
6940 * Check the word in front of the cursor for an abbreviation.
6941 * Called when the non-id character "c" has been entered.
6942 * When an abbreviation is recognized it is removed from the text and
6943 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6949 /* Don't check for abbreviation in paste mode, when disabled and just
6950 * after moving around with cursor keys. */
6951 if (p_paste
|| no_abbr
|| arrow_used
)
6954 return check_abbr(c
, ml_get_curline(), curwin
->w_cursor
.col
,
6955 curwin
->w_cursor
.lnum
== Insstart
.lnum
? Insstart
.col
: 0);
6959 * replace-stack functions
6961 * When replacing characters, the replaced characters are remembered for each
6962 * new character. This is used to re-insert the old text when backspacing.
6964 * There is a NUL headed list of characters for each character that is
6965 * currently in the file after the insertion point. When BS is used, one NUL
6966 * headed list is put back for the deleted character.
6968 * For a newline, there are two NUL headed lists. One contains the characters
6969 * that the NL replaced. The extra one stores the characters after the cursor
6970 * that were deleted (always white space).
6972 * Replace_offset is normally 0, in which case replace_push will add a new
6973 * character at the end of the stack. If replace_offset is not 0, that many
6974 * characters will be left on the stack above the newly inserted character.
6977 static char_u
*replace_stack
= NULL
;
6978 static long replace_stack_nr
= 0; /* next entry in replace stack */
6979 static long replace_stack_len
= 0; /* max. number of entries */
6983 int c
; /* character that is replaced (NUL is none) */
6987 if (replace_stack_nr
< replace_offset
) /* nothing to do */
6989 if (replace_stack_len
<= replace_stack_nr
)
6991 replace_stack_len
+= 50;
6992 p
= lalloc(sizeof(char_u
) * replace_stack_len
, TRUE
);
6993 if (p
== NULL
) /* out of memory */
6995 replace_stack_len
-= 50;
6998 if (replace_stack
!= NULL
)
7000 mch_memmove(p
, replace_stack
,
7001 (size_t)(replace_stack_nr
* sizeof(char_u
)));
7002 vim_free(replace_stack
);
7006 p
= replace_stack
+ replace_stack_nr
- replace_offset
;
7008 mch_memmove(p
+ 1, p
, (size_t)(replace_offset
* sizeof(char_u
)));
7013 #if defined(FEAT_MBYTE) || defined(PROTO)
7015 * Push a character onto the replace stack. Handles a multi-byte character in
7016 * reverse byte order, so that the first byte is popped off first.
7017 * Return the number of bytes done (includes composing characters).
7023 int l
= (*mb_ptr2len
)(p
);
7026 for (j
= l
- 1; j
>= 0; --j
)
7034 * call replace_push(c) with replace_offset set to the first NUL.
7042 p
= replace_stack
+ replace_stack_nr
;
7043 for (replace_offset
= 1; replace_offset
< replace_stack_nr
;
7053 * Pop one item from the replace stack.
7054 * return -1 if stack empty
7055 * return replaced character or NUL otherwise
7060 if (replace_stack_nr
== 0)
7062 return (int)replace_stack
[--replace_stack_nr
];
7066 * Join the top two items on the replace stack. This removes to "off"'th NUL
7071 int off
; /* offset for which NUL to remove */
7075 for (i
= replace_stack_nr
; --i
>= 0; )
7076 if (replace_stack
[i
] == NUL
&& off
-- <= 0)
7079 mch_memmove(replace_stack
+ i
, replace_stack
+ i
+ 1,
7080 (size_t)(replace_stack_nr
- i
));
7086 * Pop bytes from the replace stack until a NUL is found, and insert them
7087 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7093 int oldState
= State
;
7095 State
= NORMAL
; /* don't want REPLACE here */
7096 while ((cc
= replace_pop()) > 0)
7099 mb_replace_pop_ins(cc
);
7110 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7111 * indicates a multi-byte char, pop the other bytes too.
7114 mb_replace_pop_ins(cc
)
7118 char_u buf
[MB_MAXBYTES
];
7122 if (has_mbyte
&& (n
= MB_BYTE2LEN(cc
)) > 1)
7125 for (i
= 1; i
< n
; ++i
)
7126 buf
[i
] = replace_pop();
7127 ins_bytes_len(buf
, n
);
7133 /* Handle composing chars. */
7137 if (c
== -1) /* stack empty */
7139 if ((n
= MB_BYTE2LEN(c
)) == 1)
7141 /* Not a multi-byte char, put it back. */
7148 for (i
= 1; i
< n
; ++i
)
7149 buf
[i
] = replace_pop();
7150 if (utf_iscomposing(utf_ptr2char(buf
)))
7151 ins_bytes_len(buf
, n
);
7154 /* Not a composing char, put it back. */
7155 for (i
= n
- 1; i
>= 0; --i
)
7156 replace_push(buf
[i
]);
7165 * make the replace stack empty
7166 * (called when exiting replace mode)
7171 vim_free(replace_stack
);
7172 replace_stack
= NULL
;
7173 replace_stack_len
= 0;
7174 replace_stack_nr
= 0;
7178 * Handle doing a BS for one character.
7179 * cc < 0: replace stack empty, just move cursor
7180 * cc == 0: character was inserted, delete it
7181 * cc > 0: character was replaced, put cc (first byte of original char) back
7182 * and check for more characters to be put back
7183 * When "limit_col" is >= 0, don't delete before this column. Matters when
7184 * using composing characters, use del_char_after_col() instead of del_char().
7187 replace_do_bs(limit_col
)
7191 #ifdef FEAT_VREPLACE
7204 #ifdef FEAT_VREPLACE
7205 if (State
& VREPLACE_FLAG
)
7207 /* Get the number of screen cells used by the character we are
7208 * going to delete. */
7209 getvcol(curwin
, &curwin
->w_cursor
, NULL
, &start_vcol
, NULL
);
7210 orig_vcols
= chartabsize(ml_get_cursor(), start_vcol
);
7216 (void)del_char_after_col(limit_col
);
7217 # ifdef FEAT_VREPLACE
7218 if (State
& VREPLACE_FLAG
)
7219 orig_len
= (int)STRLEN(ml_get_cursor());
7227 #ifdef FEAT_VREPLACE
7228 if (State
& VREPLACE_FLAG
)
7229 orig_len
= (int)STRLEN(ml_get_cursor()) - 1;
7234 #ifdef FEAT_VREPLACE
7235 if (State
& VREPLACE_FLAG
)
7237 /* Get the number of screen cells used by the inserted characters */
7238 p
= ml_get_cursor();
7239 ins_len
= (int)STRLEN(p
) - orig_len
;
7241 for (i
= 0; i
< ins_len
; ++i
)
7243 vcol
+= chartabsize(p
+ i
, vcol
);
7245 i
+= (*mb_ptr2len
)(p
) - 1;
7250 /* Delete spaces that were inserted after the cursor to keep the
7252 curwin
->w_cursor
.col
+= ins_len
;
7253 while (vcol
> orig_vcols
&& gchar_cursor() == ' ')
7258 curwin
->w_cursor
.col
-= ins_len
;
7262 /* mark the buffer as changed and prepare for displaying */
7263 changed_bytes(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
7266 (void)del_char_after_col(limit_col
);
7271 * Return TRUE if C-indenting is on.
7276 return (!p_paste
&& (curbuf
->b_p_cin
7278 || *curbuf
->b_p_inde
!= NUL
7284 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7286 * Re-indent the current line, based on the current contents of it and the
7287 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7288 * confused what all the part that handles Control-T is doing that I'm not.
7289 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7293 fixthisline(get_the_indent
)
7294 int (*get_the_indent
) __ARGS((void));
7296 change_indent(INDENT_SET
, get_the_indent(), FALSE
, 0, TRUE
);
7297 if (linewhite(curwin
->w_cursor
.lnum
))
7298 did_ai
= TRUE
; /* delete the indent if the line stays empty */
7307 if (curbuf
->b_p_lisp
&& curbuf
->b_p_ai
)
7308 fixthisline(get_lisp_indent
);
7310 # if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7313 # ifdef FEAT_CINDENT
7323 * return TRUE if 'cinkeys' contains the key "keytyped",
7324 * when == '*': Only if key is preceded with '*' (indent before insert)
7325 * when == '!': Only if key is prededed with '!' (don't insert)
7326 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7328 * "keytyped" can have a few special values:
7331 * KEY_COMPLETE just finished completion.
7333 * If line_is_empty is TRUE accept keys with '0' before them.
7336 in_cinkeys(keytyped
, when
, line_is_empty
)
7350 if (*curbuf
->b_p_inde
!= NUL
)
7351 look
= curbuf
->b_p_indk
; /* 'indentexpr' set: use 'indentkeys' */
7354 look
= curbuf
->b_p_cink
; /* 'indentexpr' empty: use 'cinkeys' */
7358 * Find out if we want to try a match with this key, depending on
7359 * 'when' and a '*' or '!' before the key.
7363 case '*': try_match
= (*look
== '*'); break;
7364 case '!': try_match
= (*look
== '!'); break;
7365 default: try_match
= (*look
!= '*'); break;
7367 if (*look
== '*' || *look
== '!')
7371 * If there is a '0', only accept a match if the line is empty.
7372 * But may still match when typing last char of a word.
7376 try_match_word
= try_match
;
7382 try_match_word
= FALSE
;
7385 * does it look like a control character?
7389 && (Ctrl_chr(look
[1]) != 0)
7391 && look
[1] >= '?' && look
[1] <= '_'
7395 if (try_match
&& keytyped
== Ctrl_chr(look
[1]))
7400 * 'o' means "o" command, open forward.
7401 * 'O' means "O" command, open backward.
7403 else if (*look
== 'o')
7405 if (try_match
&& keytyped
== KEY_OPEN_FORW
)
7409 else if (*look
== 'O')
7411 if (try_match
&& keytyped
== KEY_OPEN_BACK
)
7417 * 'e' means to check for "else" at start of line and just before the
7420 else if (*look
== 'e')
7422 if (try_match
&& keytyped
== 'e' && curwin
->w_cursor
.col
>= 4)
7424 p
= ml_get_curline();
7425 if (skipwhite(p
) == p
+ curwin
->w_cursor
.col
- 4 &&
7426 STRNCMP(p
+ curwin
->w_cursor
.col
- 4, "else", 4) == 0)
7433 * ':' only causes an indent if it is at the end of a label or case
7434 * statement, or when it was before typing the ':' (to fix
7435 * class::method for C++).
7437 else if (*look
== ':')
7439 if (try_match
&& keytyped
== ':')
7441 p
= ml_get_curline();
7442 if (cin_iscase(p
) || cin_isscopedecl(p
) || cin_islabel(30))
7444 /* Need to get the line again after cin_islabel(). */
7445 p
= ml_get_curline();
7446 if (curwin
->w_cursor
.col
> 2
7447 && p
[curwin
->w_cursor
.col
- 1] == ':'
7448 && p
[curwin
->w_cursor
.col
- 2] == ':')
7450 p
[curwin
->w_cursor
.col
- 1] = ' ';
7451 i
= (cin_iscase(p
) || cin_isscopedecl(p
)
7452 || cin_islabel(30));
7453 p
= ml_get_curline();
7454 p
[curwin
->w_cursor
.col
- 1] = ':';
7464 * Is it a key in <>, maybe?
7466 else if (*look
== '<')
7471 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7472 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7473 * >, *, : and ! keys if they really really want to.
7475 if (vim_strchr((char_u
*)"<>!*oOe0:", look
[1]) != NULL
7476 && keytyped
== look
[1])
7479 if (keytyped
== get_special_key_code(look
+ 1))
7482 while (*look
&& *look
!= '>')
7484 while (*look
== '>')
7489 * Is it a word: "=word"?
7491 else if (*look
== '=' && look
[1] != ',' && look
[1] != NUL
)
7501 p
= vim_strchr(look
, ',');
7503 p
= look
+ STRLEN(look
);
7504 if ((try_match
|| try_match_word
)
7505 && curwin
->w_cursor
.col
>= (colnr_T
)(p
- look
))
7509 #ifdef FEAT_INS_EXPAND
7510 if (keytyped
== KEY_COMPLETE
)
7514 /* Just completed a word, check if it starts with "look".
7515 * search back for the start of a word. */
7516 line
= ml_get_curline();
7522 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; s
= n
)
7524 n
= mb_prevptr(line
, s
);
7525 if (!vim_iswordp(n
))
7531 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; --s
)
7532 if (!vim_iswordc(s
[-1]))
7534 if (s
+ (p
- look
) <= line
+ curwin
->w_cursor
.col
7536 ? MB_STRNICMP(s
, look
, p
- look
)
7537 : STRNCMP(s
, look
, p
- look
)) == 0)
7542 /* TODO: multi-byte */
7543 if (keytyped
== (int)p
[-1] || (icase
&& keytyped
< 256
7544 && TOLOWER_LOC(keytyped
) == TOLOWER_LOC((int)p
[-1])))
7546 line
= ml_get_cursor();
7547 if ((curwin
->w_cursor
.col
== (colnr_T
)(p
- look
)
7548 || !vim_iswordc(line
[-(p
- look
) - 1]))
7550 ? MB_STRNICMP(line
- (p
- look
), look
, p
- look
)
7551 : STRNCMP(line
- (p
- look
), look
, p
- look
))
7555 if (match
&& try_match_word
&& !try_match
)
7557 /* "0=word": Check if there are only blanks before the
7559 line
= ml_get_curline();
7560 if ((int)(skipwhite(line
) - line
) !=
7561 (int)(curwin
->w_cursor
.col
- (p
- look
)))
7571 * ok, it's a boring generic character.
7575 if (try_match
&& *look
== keytyped
)
7583 look
= skip_to_option_part(look
);
7587 #endif /* FEAT_CINDENT */
7589 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7591 * Map Hebrew keyboard when in hkmap mode.
7597 if (p_hkmapp
) /* phonetic mapping, by Ilya Dogolazky */
7599 enum {hALEF
=0, BET
, GIMEL
, DALET
, HEI
, VAV
, ZAIN
, HET
, TET
, IUD
,
7600 KAFsofit
, hKAF
, LAMED
, MEMsofit
, MEM
, NUNsofit
, NUN
, SAMEH
, AIN
,
7601 PEIsofit
, PEI
, ZADIsofit
, ZADI
, KOF
, RESH
, hSHIN
, TAV
};
7602 static char_u map
[26] =
7603 {(char_u
)hALEF
/*a*/, (char_u
)BET
/*b*/, (char_u
)hKAF
/*c*/,
7604 (char_u
)DALET
/*d*/, (char_u
)-1 /*e*/, (char_u
)PEIsofit
/*f*/,
7605 (char_u
)GIMEL
/*g*/, (char_u
)HEI
/*h*/, (char_u
)IUD
/*i*/,
7606 (char_u
)HET
/*j*/, (char_u
)KOF
/*k*/, (char_u
)LAMED
/*l*/,
7607 (char_u
)MEM
/*m*/, (char_u
)NUN
/*n*/, (char_u
)SAMEH
/*o*/,
7608 (char_u
)PEI
/*p*/, (char_u
)-1 /*q*/, (char_u
)RESH
/*r*/,
7609 (char_u
)ZAIN
/*s*/, (char_u
)TAV
/*t*/, (char_u
)TET
/*u*/,
7610 (char_u
)VAV
/*v*/, (char_u
)hSHIN
/*w*/, (char_u
)-1 /*x*/,
7611 (char_u
)AIN
/*y*/, (char_u
)ZADI
/*z*/};
7613 if (c
== 'N' || c
== 'M' || c
== 'P' || c
== 'C' || c
== 'Z')
7614 return (int)(map
[CharOrd(c
)] - 1 + p_aleph
);
7619 return '\''; /* {geresh}={'} */
7621 return ' '; /* \"o --> ' ' for a german keyboard */
7623 return ' '; /* \"a --> ' ' -- / -- */
7625 return ' '; /* \"u --> ' ' -- / -- */
7627 else if (islower(c
))
7629 /* NOTE: islower() does not do the right thing for us on Linux so we
7630 * do this the same was as 5.7 and previous, so it works correctly on
7631 * all systems. Specifically, the e.g. Delete and Arrow keys are
7632 * munged and won't work if e.g. searching for Hebrew text.
7634 else if (c
>= 'a' && c
<= 'z')
7636 return (int)(map
[CharOrdLow(c
)] + p_aleph
);
7644 case '`': return ';';
7645 case '/': return '.';
7646 case '\'': return ',';
7647 case 'q': return '/';
7648 case 'w': return '\'';
7650 /* Hebrew letters - set offset from 'a' */
7651 case ',': c
= '{'; break;
7652 case '.': c
= 'v'; break;
7653 case ';': c
= 't'; break;
7655 static char str
[] = "zqbcxlsjphmkwonu ydafe rig";
7658 /* see note about islower() above */
7661 if (c
< 'a' || c
> 'z')
7664 c
= str
[CharOrdLow(c
)];
7669 return (int)(CharOrdLow(c
) + p_aleph
);
7677 int need_redraw
= FALSE
;
7681 int vis_active
= VIsual_active
;
7685 * If we are going to wait for a character, show a '"'.
7687 pc_status
= PC_STATUS_UNSET
;
7688 if (redrawing() && !char_avail())
7690 /* may need to redraw when no more chars available now */
7693 edit_putchar('"', TRUE
);
7694 #ifdef FEAT_CMDL_INFO
7695 add_to_showcmd_c(Ctrl_R
);
7699 #ifdef USE_ON_FLY_SCROLL
7700 dont_scroll
= TRUE
; /* disallow scrolling here */
7704 * Don't map the register name. This also prevents the mode message to be
7705 * deleted when ESC is hit.
7708 regname
= plain_vgetc();
7709 LANGMAP_ADJUST(regname
, TRUE
);
7710 if (regname
== Ctrl_R
|| regname
== Ctrl_O
|| regname
== Ctrl_P
)
7712 /* Get a third key for literal register insertion */
7713 literally
= regname
;
7714 #ifdef FEAT_CMDL_INFO
7715 add_to_showcmd_c(literally
);
7717 regname
= plain_vgetc();
7718 LANGMAP_ADJUST(regname
, TRUE
);
7724 * Don't call u_sync() while getting the expression,
7725 * evaluating it or giving an error message for it!
7730 # ifdef USE_IM_CONTROL
7731 int im_on
= im_get_status();
7733 regname
= get_expr_register();
7734 # ifdef USE_IM_CONTROL
7735 /* Restore the Input Method. */
7737 im_set_active(TRUE
);
7740 if (regname
== NUL
|| !valid_yank_reg(regname
, FALSE
))
7743 need_redraw
= TRUE
; /* remove the '"' */
7748 if (literally
== Ctrl_O
|| literally
== Ctrl_P
)
7750 /* Append the command to the redo buffer. */
7751 AppendCharToRedobuff(Ctrl_R
);
7752 AppendCharToRedobuff(literally
);
7753 AppendCharToRedobuff(regname
);
7755 do_put(regname
, BACKWARD
, 1L,
7756 (literally
== Ctrl_P
? PUT_FIXINDENT
: 0) | PUT_CURSEND
);
7758 else if (insert_reg(regname
, literally
) == FAIL
)
7761 need_redraw
= TRUE
; /* remove the '"' */
7763 else if (stop_insert_mode
)
7764 /* When the '=' register was used and a function was invoked that
7765 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7766 * insert anything, need to remove the '"' */
7773 #ifdef FEAT_CMDL_INFO
7777 /* If the inserted register is empty, we need to remove the '"' */
7778 if (need_redraw
|| stuff_empty())
7782 /* Disallow starting Visual mode here, would get a weird mode. */
7783 if (!vis_active
&& VIsual_active
)
7789 * CTRL-G commands in Insert mode.
7796 #ifdef FEAT_INS_EXPAND
7797 /* Right after CTRL-X the cursor will be after the ruler. */
7802 * Don't map the second key. This also prevents the mode message to be
7803 * deleted when ESC is hit.
7810 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7813 case 'k': ins_up(TRUE
);
7816 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7819 case 'j': ins_down(TRUE
);
7822 /* CTRL-G u: start new undoable edit */
7823 case 'u': u_sync(TRUE
);
7824 ins_need_undo
= TRUE
;
7826 /* Need to reset Insstart, esp. because a BS that joins
7827 * a line to the previous one must save for undo. */
7828 Insstart
= curwin
->w_cursor
;
7831 /* Unknown CTRL-G command, reserved for future expansion. */
7832 default: vim_beep();
7837 * CTRL-^ in Insert mode.
7842 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
7844 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7845 if (State
& LANGMAP
)
7847 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7852 curbuf
->b_p_iminsert
= B_IMODE_LMAP
;
7854 #ifdef USE_IM_CONTROL
7855 im_set_active(FALSE
);
7859 #ifdef USE_IM_CONTROL
7862 /* There are no ":lmap" mappings, toggle IM */
7863 if (im_get_status())
7865 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7866 im_set_active(FALSE
);
7870 curbuf
->b_p_iminsert
= B_IMODE_IM
;
7872 im_set_active(TRUE
);
7876 set_iminsert_global();
7879 /* may show different cursor shape or color */
7881 gui_update_cursor(TRUE
, FALSE
);
7883 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7884 /* Show/unshow value of 'keymap' in status lines. */
7885 status_redraw_curbuf();
7890 * Handle ESC in insert mode.
7891 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7895 ins_esc(count
, cmdchar
, nomove
)
7898 int nomove
; /* don't move cursor */
7901 static int disabled_redraw
= FALSE
;
7904 check_spell_redraw();
7906 #if defined(FEAT_HANGULIN)
7907 # if defined(ESC_CHG_TO_ENG_MODE)
7908 hangul_input_state_set(0);
7910 if (composing_hangul
)
7912 push_raw_key(composing_hangul_buffer
, 2);
7913 composing_hangul
= 0;
7917 temp
= curwin
->w_cursor
.col
;
7918 if (disabled_redraw
)
7920 --RedrawingDisabled
;
7921 disabled_redraw
= FALSE
;
7926 * Don't append the ESC for "r<CR>" and "grx".
7927 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7928 * when "count" is non-zero.
7930 if (cmdchar
!= 'r' && cmdchar
!= 'v')
7931 AppendToRedobuff(p_im
? (char_u
*)"\014" : ESC_STR
);
7934 * Repeating insert may take a long time. Check for
7935 * interrupt now and then.
7944 if (--*count
> 0) /* repeat what was typed */
7946 /* Vi repeats the insert without replacing characters. */
7947 if (vim_strchr(p_cpo
, CPO_REPLCNT
) != NULL
)
7948 State
&= ~REPLACE_FLAG
;
7950 (void)start_redo_ins();
7951 if (cmdchar
== 'r' || cmdchar
== 'v')
7952 stuffReadbuff(ESC_STR
); /* no ESC in redo buffer */
7953 ++RedrawingDisabled
;
7954 disabled_redraw
= TRUE
;
7955 return FALSE
; /* repeat the insert */
7957 stop_insert(&curwin
->w_cursor
, TRUE
);
7961 /* When an autoindent was removed, curswant stays after the
7963 if (restart_edit
== NUL
&& (colnr_T
)temp
== curwin
->w_cursor
.col
)
7964 curwin
->w_set_curswant
= TRUE
;
7966 /* Remember the last Insert position in the '^ mark. */
7967 if (!cmdmod
.keepjumps
)
7968 curbuf
->b_last_insert
= curwin
->w_cursor
;
7971 * The cursor should end up on the last inserted character.
7972 * Don't do it for CTRL-O, unless past the end of the line.
7975 && (curwin
->w_cursor
.col
!= 0
7976 #ifdef FEAT_VIRTUALEDIT
7977 || curwin
->w_cursor
.coladd
> 0
7980 && (restart_edit
== NUL
7981 || (gchar_cursor() == NUL
7986 #ifdef FEAT_RIGHTLEFT
7991 #ifdef FEAT_VIRTUALEDIT
7992 if (curwin
->w_cursor
.coladd
> 0 || ve_flags
== VE_ALL
)
7995 if (restart_edit
!= NUL
)
7996 ++curwin
->w_cursor
.coladd
;
8001 --curwin
->w_cursor
.col
;
8003 /* Correct cursor for multi-byte character. */
8010 #ifdef USE_IM_CONTROL
8011 /* Disable IM to allow typing English directly for Normal mode commands.
8012 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8014 if (!(State
& LANGMAP
))
8015 im_save_status(&curbuf
->b_p_iminsert
);
8016 im_set_active(FALSE
);
8020 /* need to position cursor again (e.g. when on a TAB ) */
8021 changed_cline_bef_curs();
8027 ui_cursor_shape(); /* may show different cursor shape */
8031 * When recording or for CTRL-O, need to display the new mode.
8032 * Otherwise remove the mode message.
8034 if (Recording
|| restart_edit
!= NUL
)
8039 return TRUE
; /* exit Insert mode */
8042 #ifdef FEAT_RIGHTLEFT
8044 * Toggle language: hkmap and revins_on.
8045 * Move to end of reverse inserted text.
8050 if (revins_on
&& revins_chars
&& revins_scol
>= 0)
8052 while (gchar_cursor() != NUL
&& revins_chars
--)
8053 ++curwin
->w_cursor
.col
;
8056 revins_on
= (State
== INSERT
&& p_ri
);
8059 revins_scol
= curwin
->w_cursor
.col
;
8070 * to be consistent also for redo command, using '.'
8071 * set arrow_used to true and stop it - causing to redo
8072 * characters entered in one mode (normal/reverse insert).
8076 p_fkmap
= curwin
->w_p_rl
^ p_ri
;
8077 if (p_fkmap
&& p_ri
)
8082 p_hkmap
= curwin
->w_p_rl
^ p_ri
; /* be consistent! */
8089 * If 'keymodel' contains "startsel", may start selection.
8090 * Returns TRUE when a CTRL-O and other keys stuffed.
8113 if (!(mod_mask
& MOD_MASK_SHIFT
))
8122 /* Start selection right away, the cursor can move with
8123 * CTRL-O when beyond the end of the line. */
8126 /* Execute the key in (insert) Select mode. */
8127 stuffcharReadbuff(Ctrl_O
);
8133 buf
[1] = KS_MODIFIER
;
8138 stuffcharReadbuff(c
);
8146 * <Insert> key in Insert mode: toggle insert/remplace mode.
8149 ins_insert(replaceState
)
8153 if (p_fkmap
&& p_ri
)
8156 EMSG(farsi_text_3
); /* encoded in Farsi */
8163 set_vim_var_string(VV_INSERTMODE
,
8164 (char_u
*)((State
& REPLACE_FLAG
) ? "i" :
8165 # ifdef FEAT_VREPLACE
8166 replaceState
== VREPLACE
? "v" :
8170 apply_autocmds(EVENT_INSERTCHANGE
, NULL
, NULL
, FALSE
, curbuf
);
8172 if (State
& REPLACE_FLAG
)
8173 State
= INSERT
| (State
& LANGMAP
);
8175 State
= replaceState
| (State
& LANGMAP
);
8176 AppendCharToRedobuff(K_INS
);
8179 ui_cursor_shape(); /* may show different cursor shape */
8184 * Pressed CTRL-O in Insert mode.
8189 #ifdef FEAT_VREPLACE
8190 if (State
& VREPLACE_FLAG
)
8194 if (State
& REPLACE_FLAG
)
8198 #ifdef FEAT_VIRTUALEDIT
8199 if (virtual_active())
8200 ins_at_eol
= FALSE
; /* cursor always keeps its column */
8203 ins_at_eol
= (gchar_cursor() == NUL
);
8207 * If the cursor is on an indent, ^T/^D insert/delete one
8208 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8209 * Always round the indent to 'shiftwidth', this is compatible
8210 * with vi. But vi only supports ^T and ^D after an
8211 * autoindent, we support it everywhere.
8218 if (stop_arrow() == FAIL
)
8220 AppendCharToRedobuff(c
);
8223 * 0^D and ^^D: remove all indent.
8225 if (c
== Ctrl_D
&& (lastc
== '0' || lastc
== '^')
8226 && curwin
->w_cursor
.col
> 0)
8228 --curwin
->w_cursor
.col
;
8229 (void)del_char(FALSE
); /* delete the '^' or '0' */
8230 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8231 if (State
& REPLACE_FLAG
)
8234 old_indent
= get_indent(); /* remember curr. indent */
8235 change_indent(INDENT_SET
, 0, TRUE
, 0, TRUE
);
8238 change_indent(c
== Ctrl_D
? INDENT_DEC
: INDENT_INC
, 0, TRUE
, 0, TRUE
);
8240 if (did_ai
&& *skipwhite(ml_get_curline()) != NUL
)
8242 #ifdef FEAT_SMARTINDENT
8245 can_si_back
= FALSE
;
8248 can_cindent
= FALSE
; /* no cindenting after ^D or ^T */
8257 if (stop_arrow() == FAIL
)
8259 if (gchar_cursor() == NUL
) /* delete newline */
8261 temp
= curwin
->w_cursor
.col
;
8262 if (!can_bs(BS_EOL
) /* only if "eol" included */
8263 || u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
8264 (linenr_T
)(curwin
->w_cursor
.lnum
+ 2)) == FAIL
8265 || do_join(FALSE
) == FAIL
)
8268 curwin
->w_cursor
.col
= temp
;
8270 else if (del_char(FALSE
) == FAIL
) /* delete char under cursor */
8273 #ifdef FEAT_SMARTINDENT
8276 can_si_back
= FALSE
;
8278 AppendCharToRedobuff(K_DEL
);
8281 static void ins_bs_one
__ARGS((colnr_T
*vcolp
));
8284 * Delete one character for ins_bs().
8291 getvcol(curwin
, &curwin
->w_cursor
, vcolp
, NULL
, NULL
);
8292 if (State
& REPLACE_FLAG
)
8294 /* Don't delete characters before the insert point when in
8296 if (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8297 || curwin
->w_cursor
.col
>= Insstart
.col
)
8301 (void)del_char(FALSE
);
8305 * Handle Backspace, delete-word and delete-line in Insert mode.
8306 * Return TRUE when backspace was actually used.
8309 ins_bs(c
, mode
, inserted_space_p
)
8312 int *inserted_space_p
;
8316 int temp
= 0; /* init for GCC */
8319 int did_backspace
= FALSE
;
8323 int cpc
[MAX_MCO
]; /* composing characters */
8327 * can't delete anything in an empty file
8328 * can't backup past first character in buffer
8329 * can't backup past starting point unless 'backspace' > 1
8330 * can backup to a previous line if 'backspace' == 0
8334 #ifdef FEAT_RIGHTLEFT
8337 ((curwin
->w_cursor
.lnum
== 1 && curwin
->w_cursor
.col
== 0)
8338 || (!can_bs(BS_START
)
8340 || (curwin
->w_cursor
.lnum
== Insstart
.lnum
8341 && curwin
->w_cursor
.col
<= Insstart
.col
)))
8342 || (!can_bs(BS_INDENT
) && !arrow_used
&& ai_col
> 0
8343 && curwin
->w_cursor
.col
<= ai_col
)
8344 || (!can_bs(BS_EOL
) && curwin
->w_cursor
.col
== 0))))
8350 if (stop_arrow() == FAIL
)
8352 in_indent
= inindent(0);
8355 can_cindent
= FALSE
;
8357 #ifdef FEAT_COMMENTS
8358 end_comment_pending
= NUL
; /* After BS, don't auto-end comment */
8360 #ifdef FEAT_RIGHTLEFT
8361 if (revins_on
) /* put cursor after last inserted char */
8365 #ifdef FEAT_VIRTUALEDIT
8367 * BACKSPACE_CHAR eats a virtual space
8368 * BACKSPACE_WORD eats all coladd
8369 * BACKSPACE_LINE eats all coladd and keeps going
8371 if (curwin
->w_cursor
.coladd
> 0)
8373 if (mode
== BACKSPACE_CHAR
)
8375 --curwin
->w_cursor
.coladd
;
8378 if (mode
== BACKSPACE_WORD
)
8380 curwin
->w_cursor
.coladd
= 0;
8383 curwin
->w_cursor
.coladd
= 0;
8390 if (curwin
->w_cursor
.col
== 0)
8392 lnum
= Insstart
.lnum
;
8393 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8394 #ifdef FEAT_RIGHTLEFT
8399 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 2),
8400 (linenr_T
)(curwin
->w_cursor
.lnum
+ 1)) == FAIL
)
8403 Insstart
.col
= MAXCOL
;
8407 * cc < 0: NL was inserted, delete it
8408 * cc >= 0: NL was replaced, put original characters back
8411 if (State
& REPLACE_FLAG
)
8412 cc
= replace_pop(); /* returns -1 if NL was inserted */
8414 * In replace mode, in the line we started replacing, we only move the
8417 if ((State
& REPLACE_FLAG
) && curwin
->w_cursor
.lnum
<= lnum
)
8423 #ifdef FEAT_VREPLACE
8424 if (!(State
& VREPLACE_FLAG
)
8425 || curwin
->w_cursor
.lnum
> orig_line_count
)
8428 temp
= gchar_cursor(); /* remember current char */
8429 --curwin
->w_cursor
.lnum
;
8431 /* When "aw" is in 'formatoptions' we must delete the space at
8432 * the end of the line, otherwise the line will be broken
8433 * again when auto-formatting. */
8434 if (has_format_option(FO_AUTO
)
8435 && has_format_option(FO_WHITE_PAR
))
8437 char_u
*ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
,
8441 len
= (int)STRLEN(ptr
);
8442 if (len
> 0 && ptr
[len
- 1] == ' ')
8446 (void)do_join(FALSE
);
8447 if (temp
== NUL
&& gchar_cursor() != NUL
)
8450 #ifdef FEAT_VREPLACE
8456 * In REPLACE mode we have to put back the text that was replaced
8457 * by the NL. On the replace stack is first a NUL-terminated
8458 * sequence of characters that were deleted and then the
8459 * characters that NL replaced.
8461 if (State
& REPLACE_FLAG
)
8464 * Do the next ins_char() in NORMAL state, to
8465 * prevent ins_char() from replacing characters and
8466 * avoiding showmatch().
8471 * restore characters (blanks) deleted after cursor
8475 save_col
= curwin
->w_cursor
.col
;
8477 mb_replace_pop_ins(cc
);
8481 curwin
->w_cursor
.col
= save_col
;
8484 /* restore the characters that NL replaced */
8494 * Delete character(s) before the cursor.
8496 #ifdef FEAT_RIGHTLEFT
8497 if (revins_on
) /* put cursor on last inserted char */
8502 if (mode
== BACKSPACE_LINE
8508 #ifdef FEAT_RIGHTLEFT
8513 save_col
= curwin
->w_cursor
.col
;
8514 beginline(BL_WHITE
);
8515 if (curwin
->w_cursor
.col
< (colnr_T
)temp
)
8516 mincol
= curwin
->w_cursor
.col
;
8517 curwin
->w_cursor
.col
= save_col
;
8521 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8523 if ( mode
== BACKSPACE_CHAR
8524 && ((p_sta
&& in_indent
)
8525 || (curbuf
->b_p_sts
!= 0
8526 && curwin
->w_cursor
.col
> 0
8527 && (*(ml_get_cursor() - 1) == TAB
8528 || (*(ml_get_cursor() - 1) == ' '
8529 && (!*inserted_space_p
8537 *inserted_space_p
= FALSE
;
8538 if (p_sta
&& in_indent
)
8539 ts
= curbuf
->b_p_sw
;
8541 ts
= curbuf
->b_p_sts
;
8542 /* Compute the virtual column where we want to be. Since
8543 * 'showbreak' may get in the way, need to get the last column of
8544 * the previous character. */
8545 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8548 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &want_vcol
);
8550 want_vcol
= (want_vcol
/ ts
) * ts
;
8552 /* delete characters until we are at or before want_vcol */
8553 while (vcol
> want_vcol
8554 && (cc
= *(ml_get_cursor() - 1), vim_iswhite(cc
)))
8557 /* insert extra spaces until we are at want_vcol */
8558 while (vcol
< want_vcol
)
8560 /* Remember the first char we inserted */
8561 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8562 && curwin
->w_cursor
.col
< Insstart
.col
)
8563 Insstart
.col
= curwin
->w_cursor
.col
;
8565 #ifdef FEAT_VREPLACE
8566 if (State
& VREPLACE_FLAG
)
8571 ins_str((char_u
*)" ");
8572 if ((State
& REPLACE_FLAG
))
8575 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8578 /* If we are now back where we started delete one character. Can
8579 * happen when using 'sts' and 'linebreak'. */
8580 if (vcol
>= start_vcol
)
8585 * Delete upto starting point, start of line or previous word.
8589 #ifdef FEAT_RIGHTLEFT
8590 if (!revins_on
) /* put cursor on char to be deleted */
8594 /* start of word? */
8595 if (mode
== BACKSPACE_WORD
&& !vim_isspace(gchar_cursor()))
8597 mode
= BACKSPACE_WORD_NOT_SPACE
;
8598 temp
= vim_iswordc(gchar_cursor());
8601 else if (mode
== BACKSPACE_WORD_NOT_SPACE
8602 && (vim_isspace(cc
= gchar_cursor())
8603 || vim_iswordc(cc
) != temp
))
8605 #ifdef FEAT_RIGHTLEFT
8609 #ifdef FEAT_RIGHTLEFT
8610 else if (State
& REPLACE_FLAG
)
8615 if (State
& REPLACE_FLAG
)
8620 if (enc_utf8
&& p_deco
)
8621 (void)utfc_ptr2char(ml_get_cursor(), cpc
);
8623 (void)del_char(FALSE
);
8626 * If there are combining characters and 'delcombine' is set
8627 * move the cursor back. Don't back up before the base
8630 if (enc_utf8
&& p_deco
&& cpc
[0] != NUL
)
8633 #ifdef FEAT_RIGHTLEFT
8639 if (revins_on
&& gchar_cursor() == NUL
)
8643 /* Just a single backspace?: */
8644 if (mode
== BACKSPACE_CHAR
)
8647 #ifdef FEAT_RIGHTLEFT
8650 (curwin
->w_cursor
.col
> mincol
8651 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8652 || curwin
->w_cursor
.col
!= Insstart
.col
)));
8653 did_backspace
= TRUE
;
8655 #ifdef FEAT_SMARTINDENT
8658 can_si_back
= FALSE
;
8660 if (curwin
->w_cursor
.col
<= 1)
8663 * It's a little strange to put backspaces into the redo
8664 * buffer, but it makes auto-indent a lot easier to deal
8667 AppendCharToRedobuff(c
);
8669 /* If deleted before the insertion point, adjust it */
8670 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8671 && curwin
->w_cursor
.col
< Insstart
.col
)
8672 Insstart
.col
= curwin
->w_cursor
.col
;
8674 /* vi behaviour: the cursor moves backward but the character that
8675 * was there remains visible
8676 * Vim behaviour: the cursor moves backward and the character that
8677 * was there is erased from the screen.
8678 * We can emulate the vi behaviour by pretending there is a dollar
8679 * displayed even when there isn't.
8680 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8681 if (vim_strchr(p_cpo
, CPO_BACKSPACE
) != NULL
&& dollar_vcol
== 0)
8682 dollar_vcol
= curwin
->w_virtcol
;
8685 /* When deleting a char the cursor line must never be in a closed fold.
8686 * E.g., when 'foldmethod' is indent and deleting the first non-white
8687 * char before a Tab. */
8692 return did_backspace
;
8701 win_T
*old_curwin
= curwin
;
8704 /* When GUI is active, also move/paste when 'mouse' is empty */
8707 if (!mouse_has(MOUSE_INSERT
))
8711 tpos
= curwin
->w_cursor
;
8712 if (do_mouse(NULL
, c
, BACKWARD
, 1L, 0))
8715 win_T
*new_curwin
= curwin
;
8717 if (curwin
!= old_curwin
&& win_valid(old_curwin
))
8719 /* Mouse took us to another window. We need to go back to the
8720 * previous one to stop insert there properly. */
8721 curwin
= old_curwin
;
8722 curbuf
= curwin
->w_buffer
;
8725 start_arrow(curwin
== old_curwin
? &tpos
: NULL
);
8727 if (curwin
!= new_curwin
&& win_valid(new_curwin
))
8729 curwin
= new_curwin
;
8730 curbuf
= curwin
->w_buffer
;
8733 # ifdef FEAT_CINDENT
8739 /* redraw status lines (in case another window became active) */
8740 redraw_statuslines();
8749 # if defined(FEAT_WINDOWS)
8750 win_T
*old_curwin
= curwin
;
8752 # ifdef FEAT_INS_EXPAND
8753 int did_scroll
= FALSE
;
8756 tpos
= curwin
->w_cursor
;
8758 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8759 /* Currently the mouse coordinates are only known in the GUI. */
8760 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
8767 /* find the window at the pointer coordinates */
8768 curwin
= mouse_find_win(&row
, &col
);
8769 curbuf
= curwin
->w_buffer
;
8771 if (curwin
== old_curwin
)
8775 # ifdef FEAT_INS_EXPAND
8776 /* Don't scroll the window in which completion is being done. */
8778 # if defined(FEAT_WINDOWS)
8779 || curwin
!= old_curwin
8784 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
8785 scroll_redraw(up
, (long)(curwin
->w_botline
- curwin
->w_topline
));
8787 scroll_redraw(up
, 3L);
8788 # ifdef FEAT_INS_EXPAND
8793 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8794 curwin
->w_redr_status
= TRUE
;
8796 curwin
= old_curwin
;
8797 curbuf
= curwin
->w_buffer
;
8800 # ifdef FEAT_INS_EXPAND
8801 /* The popup menu may overlay the window, need to redraw it.
8802 * TODO: Would be more efficient to only redraw the windows that are
8803 * overlapped by the popup menu. */
8804 if (pum_visible() && did_scroll
)
8806 redraw_all_later(NOT_VALID
);
8807 ins_compl_show_pum();
8811 if (!equalpos(curwin
->w_cursor
, tpos
))
8814 # ifdef FEAT_CINDENT
8821 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8826 /* We will be leaving the current window, unless closing another tab. */
8827 if (c
!= K_TABMENU
|| current_tabmenu
!= TABLINE_MENU_CLOSE
8828 || (current_tab
!= 0 && current_tab
!= tabpage_index(curtab
)))
8831 start_arrow(&curwin
->w_cursor
);
8832 # ifdef FEAT_CINDENT
8838 goto_tabpage(current_tab
);
8842 redraw_statuslines(); /* will redraw the tabline when needed */
8847 #if defined(FEAT_GUI) || defined(PROTO)
8854 tpos
= curwin
->w_cursor
;
8855 if (gui_do_scroll())
8858 # ifdef FEAT_CINDENT
8870 tpos
= curwin
->w_cursor
;
8871 if (gui_do_horiz_scroll())
8874 # ifdef FEAT_CINDENT
8887 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8891 tpos
= curwin
->w_cursor
;
8892 if (oneleft() == OK
)
8894 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8895 /* Only call start_arrow() when not busy with preediting, it will
8896 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8897 if (!im_is_preediting())
8900 #ifdef FEAT_RIGHTLEFT
8901 /* If exit reversed string, position is fixed */
8902 if (revins_scol
!= -1 && (int)curwin
->w_cursor
.col
>= revins_scol
)
8909 * if 'whichwrap' set for cursor in insert mode may go to
8912 else if (vim_strchr(p_ww
, '[') != NULL
&& curwin
->w_cursor
.lnum
> 1)
8915 --(curwin
->w_cursor
.lnum
);
8916 coladvance((colnr_T
)MAXCOL
);
8917 curwin
->w_set_curswant
= TRUE
; /* so we stay at the end */
8930 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8934 tpos
= curwin
->w_cursor
;
8936 curwin
->w_cursor
.lnum
= 1;
8937 curwin
->w_cursor
.col
= 0;
8938 #ifdef FEAT_VIRTUALEDIT
8939 curwin
->w_cursor
.coladd
= 0;
8941 curwin
->w_curswant
= 0;
8952 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8956 tpos
= curwin
->w_cursor
;
8958 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
8959 coladvance((colnr_T
)MAXCOL
);
8960 curwin
->w_curswant
= MAXCOL
;
8969 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8973 if (curwin
->w_cursor
.lnum
> 1 || curwin
->w_cursor
.col
> 0)
8975 start_arrow(&curwin
->w_cursor
);
8976 (void)bck_word(1L, FALSE
, FALSE
);
8977 curwin
->w_set_curswant
= TRUE
;
8987 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8991 if (gchar_cursor() != NUL
8992 #ifdef FEAT_VIRTUALEDIT
8997 start_arrow(&curwin
->w_cursor
);
8998 curwin
->w_set_curswant
= TRUE
;
8999 #ifdef FEAT_VIRTUALEDIT
9000 if (virtual_active())
9007 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
9010 ++curwin
->w_cursor
.col
;
9013 #ifdef FEAT_RIGHTLEFT
9019 /* if 'whichwrap' set for cursor in insert mode, may move the
9020 * cursor to the next line */
9021 else if (vim_strchr(p_ww
, ']') != NULL
9022 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
9024 start_arrow(&curwin
->w_cursor
);
9025 curwin
->w_set_curswant
= TRUE
;
9026 ++curwin
->w_cursor
.lnum
;
9027 curwin
->w_cursor
.col
= 0;
9037 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9041 if (curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
9042 || gchar_cursor() != NUL
)
9044 start_arrow(&curwin
->w_cursor
);
9045 (void)fwd_word(1L, FALSE
, 0);
9046 curwin
->w_set_curswant
= TRUE
;
9054 int startcol
; /* when TRUE move to Insstart.col */
9057 linenr_T old_topline
= curwin
->w_topline
;
9059 int old_topfill
= curwin
->w_topfill
;
9063 tpos
= curwin
->w_cursor
;
9064 if (cursor_up(1L, TRUE
) == OK
)
9067 coladvance(getvcol_nolist(&Insstart
));
9068 if (old_topline
!= curwin
->w_topline
9070 || old_topfill
!= curwin
->w_topfill
9073 redraw_later(VALID
);
9091 if (mod_mask
& MOD_MASK_CTRL
)
9093 /* <C-PageUp>: tab page back */
9094 if (first_tabpage
->tp_next
!= NULL
)
9096 start_arrow(&curwin
->w_cursor
);
9103 tpos
= curwin
->w_cursor
;
9104 if (onepage(BACKWARD
, 1L) == OK
)
9117 int startcol
; /* when TRUE move to Insstart.col */
9120 linenr_T old_topline
= curwin
->w_topline
;
9122 int old_topfill
= curwin
->w_topfill
;
9126 tpos
= curwin
->w_cursor
;
9127 if (cursor_down(1L, TRUE
) == OK
)
9130 coladvance(getvcol_nolist(&Insstart
));
9131 if (old_topline
!= curwin
->w_topline
9133 || old_topfill
!= curwin
->w_topfill
9136 redraw_later(VALID
);
9154 if (mod_mask
& MOD_MASK_CTRL
)
9156 /* <C-PageDown>: tab page forward */
9157 if (first_tabpage
->tp_next
!= NULL
)
9159 start_arrow(&curwin
->w_cursor
);
9166 tpos
= curwin
->w_cursor
;
9167 if (onepage(FORWARD
, 1L) == OK
)
9182 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9187 * Handle TAB in Insert or Replace mode.
9188 * Return TRUE when the TAB needs to be inserted like a normal character.
9197 if (Insstart_blank_vcol
== MAXCOL
&& curwin
->w_cursor
.lnum
== Insstart
.lnum
)
9198 Insstart_blank_vcol
= get_nolist_virtcol();
9199 if (echeck_abbr(TAB
+ ABBR_OFF
))
9205 can_cindent
= FALSE
;
9209 * When nothing special, insert TAB like a normal character
9212 && !(p_sta
&& ind
&& curbuf
->b_p_ts
!= curbuf
->b_p_sw
)
9213 && curbuf
->b_p_sts
== 0)
9216 if (stop_arrow() == FAIL
)
9220 #ifdef FEAT_SMARTINDENT
9223 can_si_back
= FALSE
;
9225 AppendToRedobuff((char_u
*)"\t");
9227 if (p_sta
&& ind
) /* insert tab in indent, use 'shiftwidth' */
9228 temp
= (int)curbuf
->b_p_sw
;
9229 else if (curbuf
->b_p_sts
> 0) /* use 'softtabstop' when set */
9230 temp
= (int)curbuf
->b_p_sts
;
9231 else /* otherwise use 'tabstop' */
9232 temp
= (int)curbuf
->b_p_ts
;
9233 temp
-= get_nolist_virtcol() % temp
;
9236 * Insert the first space with ins_char(). It will delete one char in
9237 * replace mode. Insert the rest with ins_str(); it will not delete any
9238 * chars. For VREPLACE mode, we use ins_char() for all characters.
9243 #ifdef FEAT_VREPLACE
9244 if (State
& VREPLACE_FLAG
)
9249 ins_str((char_u
*)" ");
9250 if (State
& REPLACE_FLAG
) /* no char replaced */
9256 * When 'expandtab' not set: Replace spaces by TABs where possible.
9258 if (!curbuf
->b_p_et
&& (curbuf
->b_p_sts
|| (p_sta
&& ind
)))
9261 #ifdef FEAT_VREPLACE
9262 char_u
*saved_line
= NULL
; /* init for GCC */
9267 colnr_T want_vcol
, vcol
;
9268 int change_col
= -1;
9269 int save_list
= curwin
->w_p_list
;
9272 * Get the current line. For VREPLACE mode, don't make real changes
9273 * yet, just work on a copy of the line.
9275 #ifdef FEAT_VREPLACE
9276 if (State
& VREPLACE_FLAG
)
9278 pos
= curwin
->w_cursor
;
9280 saved_line
= vim_strsave(ml_get_curline());
9281 if (saved_line
== NULL
)
9283 ptr
= saved_line
+ pos
.col
;
9288 ptr
= ml_get_cursor();
9289 cursor
= &curwin
->w_cursor
;
9292 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9293 if (vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9294 curwin
->w_p_list
= FALSE
;
9296 /* Find first white before the cursor */
9297 fpos
= curwin
->w_cursor
;
9298 while (fpos
.col
> 0 && vim_iswhite(ptr
[-1]))
9304 /* In Replace mode, don't change characters before the insert point. */
9305 if ((State
& REPLACE_FLAG
)
9306 && fpos
.lnum
== Insstart
.lnum
9307 && fpos
.col
< Insstart
.col
)
9309 ptr
+= Insstart
.col
- fpos
.col
;
9310 fpos
.col
= Insstart
.col
;
9313 /* compute virtual column numbers of first white and cursor */
9314 getvcol(curwin
, &fpos
, &vcol
, NULL
, NULL
);
9315 getvcol(curwin
, cursor
, &want_vcol
, NULL
, NULL
);
9317 /* Use as many TABs as possible. Beware of 'showbreak' and
9318 * 'linebreak' adding extra virtual columns. */
9319 while (vim_iswhite(*ptr
))
9321 i
= lbr_chartabsize((char_u
*)"\t", vcol
);
9322 if (vcol
+ i
> want_vcol
)
9329 change_col
= fpos
.col
; /* Column of first change */
9330 /* May have to adjust Insstart */
9331 if (fpos
.lnum
== Insstart
.lnum
&& fpos
.col
< Insstart
.col
)
9332 Insstart
.col
= fpos
.col
;
9340 if (change_col
>= 0)
9344 /* Skip over the spaces we need. */
9345 while (vcol
< want_vcol
&& *ptr
== ' ')
9347 vcol
+= lbr_chartabsize(ptr
, vcol
);
9351 if (vcol
> want_vcol
)
9353 /* Must have a char with 'showbreak' just before it. */
9357 fpos
.col
+= repl_off
;
9359 /* Delete following spaces. */
9360 i
= cursor
->col
- fpos
.col
;
9363 STRMOVE(ptr
, ptr
+ i
);
9364 /* correct replace stack. */
9365 if ((State
& REPLACE_FLAG
)
9366 #ifdef FEAT_VREPLACE
9367 && !(State
& VREPLACE_FLAG
)
9370 for (temp
= i
; --temp
>= 0; )
9371 replace_join(repl_off
);
9373 #ifdef FEAT_NETBEANS_INTG
9376 netbeans_removed(curbuf
, fpos
.lnum
, cursor
->col
,
9378 netbeans_inserted(curbuf
, fpos
.lnum
, cursor
->col
,
9384 #ifdef FEAT_VREPLACE
9386 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9387 * backspacing over the changed spacing and then inserting the new
9390 if (State
& VREPLACE_FLAG
)
9392 /* Backspace from real cursor to change_col */
9393 backspace_until_column(change_col
);
9395 /* Insert each char in saved_line from changed_col to
9397 ins_bytes_len(saved_line
+ change_col
,
9398 cursor
->col
- change_col
);
9403 #ifdef FEAT_VREPLACE
9404 if (State
& VREPLACE_FLAG
)
9405 vim_free(saved_line
);
9407 curwin
->w_p_list
= save_list
;
9414 * Handle CR or NL in insert mode.
9415 * Return TRUE when out of memory or can't undo.
9423 if (echeck_abbr(c
+ ABBR_OFF
))
9425 if (stop_arrow() == FAIL
)
9430 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9431 * character under the cursor. Only push a NUL on the replace stack,
9432 * nothing to put back when the NL is deleted.
9434 if ((State
& REPLACE_FLAG
)
9435 #ifdef FEAT_VREPLACE
9436 && !(State
& VREPLACE_FLAG
)
9442 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9443 * replacing the next line, so we push all of the characters left on the
9444 * line onto the replace stack. This is not done here though, it is done
9448 #ifdef FEAT_VIRTUALEDIT
9449 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9451 if (virtual_active() && curwin
->w_cursor
.coladd
> 0)
9452 coladvance(getviscol());
9455 #ifdef FEAT_RIGHTLEFT
9457 if (p_altkeymap
&& p_fkmap
)
9460 /* NL in reverse insert will always start in the end of
9463 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
9466 AppendToRedobuff(NL_STR
);
9467 i
= open_line(FORWARD
,
9468 #ifdef FEAT_COMMENTS
9469 has_format_option(FO_RET_COMS
) ? OPENLINE_DO_COM
:
9477 /* When inserting a line the cursor line must never be in a closed fold. */
9484 #ifdef FEAT_DIGRAPHS
9486 * Handle digraph in insert mode.
9487 * Returns character still to be inserted, or NUL when nothing remaining to be
9496 pc_status
= PC_STATUS_UNSET
;
9497 if (redrawing() && !char_avail())
9499 /* may need to redraw when no more chars available now */
9502 edit_putchar('?', TRUE
);
9503 #ifdef FEAT_CMDL_INFO
9504 add_to_showcmd_c(Ctrl_K
);
9508 #ifdef USE_ON_FLY_SCROLL
9509 dont_scroll
= TRUE
; /* disallow scrolling here */
9512 /* don't map the digraph chars. This also prevents the
9513 * mode message to be deleted when ESC is hit */
9519 if (IS_SPECIAL(c
) || mod_mask
) /* special key */
9521 #ifdef FEAT_CMDL_INFO
9524 insert_special(c
, TRUE
, FALSE
);
9529 if (redrawing() && !char_avail())
9531 /* may need to redraw when no more chars available now */
9534 if (char2cells(c
) == 1)
9536 /* first remove the '?', otherwise it's restored when typing
9540 edit_putchar(c
, TRUE
);
9542 #ifdef FEAT_CMDL_INFO
9543 add_to_showcmd_c(c
);
9553 AppendToRedobuff((char_u
*)CTRL_V_STR
);
9554 c
= getdigraph(c
, cc
, TRUE
);
9555 #ifdef FEAT_CMDL_INFO
9562 #ifdef FEAT_CMDL_INFO
9570 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9571 * Returns the char to be inserted, or NUL if none found.
9579 char_u
*ptr
, *prev_ptr
;
9581 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
9587 /* try to advance to the cursor column */
9592 while ((colnr_T
)temp
< curwin
->w_virtcol
&& *ptr
!= NUL
)
9595 temp
+= lbr_chartabsize_adv(&ptr
, (colnr_T
)temp
);
9597 if ((colnr_T
)temp
> curwin
->w_virtcol
)
9601 c
= (*mb_ptr2char
)(ptr
);
9611 * CTRL-Y or CTRL-E typed in Insert mode.
9619 #ifdef FEAT_INS_EXPAND
9620 if (ctrl_x_mode
== CTRL_X_SCROLL
)
9626 redraw_later(VALID
);
9631 c
= ins_copychar(curwin
->w_cursor
.lnum
+ (c
== Ctrl_Y
? -1 : 1));
9636 /* The character must be taken literally, insert like it
9637 * was typed after a CTRL-V, and pretend 'textwidth'
9638 * wasn't set. Digits, 'o' and 'x' are special after a
9639 * CTRL-V, don't use it for these. */
9640 if (c
< 256 && !isalnum(c
))
9641 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
9642 tw_save
= curbuf
->b_p_tw
;
9643 curbuf
->b_p_tw
= -1;
9644 insert_special(c
, TRUE
, FALSE
);
9645 curbuf
->b_p_tw
= tw_save
;
9646 #ifdef FEAT_RIGHTLEFT
9650 c
= Ctrl_V
; /* pretend CTRL-V is last character */
9651 auto_format(FALSE
, TRUE
);
9657 #ifdef FEAT_SMARTINDENT
9659 * Try to do some very smart auto-indenting.
9660 * Used when inserting a "normal" character.
9666 pos_T
*pos
, old_pos
;
9672 * do some very smart indenting when entering '{' or '}'
9674 if (((did_si
|| can_si_back
) && c
== '{') || (can_si
&& c
== '}'))
9677 * for '}' set indent equal to indent of line containing matching '{'
9679 if (c
== '}' && (pos
= findmatch(NULL
, '{')) != NULL
)
9681 old_pos
= curwin
->w_cursor
;
9683 * If the matching '{' has a ')' immediately before it (ignoring
9684 * white-space), then line up with the start of the line
9685 * containing the matching '(' if there is one. This handles the
9686 * case where an "if (..\n..) {" statement continues over multiple
9689 ptr
= ml_get(pos
->lnum
);
9691 if (i
> 0) /* skip blanks before '{' */
9692 while (--i
> 0 && vim_iswhite(ptr
[i
]))
9694 curwin
->w_cursor
.lnum
= pos
->lnum
;
9695 curwin
->w_cursor
.col
= i
;
9696 if (ptr
[i
] == ')' && (pos
= findmatch(NULL
, '(')) != NULL
)
9697 curwin
->w_cursor
= *pos
;
9699 curwin
->w_cursor
= old_pos
;
9700 #ifdef FEAT_VREPLACE
9701 if (State
& VREPLACE_FLAG
)
9702 change_indent(INDENT_SET
, i
, FALSE
, NUL
, TRUE
);
9705 (void)set_indent(i
, SIN_CHANGED
);
9707 else if (curwin
->w_cursor
.col
> 0)
9710 * when inserting '{' after "O" reduce indent, but not
9711 * more than indent of previous line
9714 if (c
== '{' && can_si_back
&& curwin
->w_cursor
.lnum
> 1)
9716 old_pos
= curwin
->w_cursor
;
9718 while (curwin
->w_cursor
.lnum
> 1)
9720 ptr
= skipwhite(ml_get(--(curwin
->w_cursor
.lnum
)));
9722 /* ignore empty lines and lines starting with '#'. */
9723 if (*ptr
!= '#' && *ptr
!= NUL
)
9726 if (get_indent() >= i
)
9728 curwin
->w_cursor
= old_pos
;
9731 shift_line(TRUE
, FALSE
, 1, TRUE
);
9736 * set indent of '#' always to 0
9738 if (curwin
->w_cursor
.col
> 0 && can_si
&& c
== '#')
9740 /* remember current indent for next line */
9741 old_indent
= get_indent();
9742 (void)set_indent(0, SIN_CHANGED
);
9745 /* Adjust ai_col, the char at this position can be deleted. */
9746 if (ai_col
> curwin
->w_cursor
.col
)
9747 ai_col
= curwin
->w_cursor
.col
;
9752 * Get the value that w_virtcol would have when 'list' is off.
9753 * Unless 'cpo' contains the 'L' flag.
9756 get_nolist_virtcol()
9758 if (curwin
->w_p_list
&& vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9759 return getvcol_nolist(&curwin
->w_cursor
);
9761 return curwin
->w_virtcol
;