1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * edit.c: functions for Insert mode
16 #ifdef FEAT_INS_EXPAND
18 * definitions used for CTRL-X submode
20 #define CTRL_X_WANT_IDENT 0x100
22 #define CTRL_X_NOT_DEFINED_YET 1
23 #define CTRL_X_SCROLL 2
24 #define CTRL_X_WHOLE_LINE 3
25 #define CTRL_X_FILES 4
26 #define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27 #define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28 #define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29 #define CTRL_X_FINISHED 8
30 #define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31 #define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32 #define CTRL_X_CMDLINE 11
33 #define CTRL_X_FUNCTION 12
34 #define CTRL_X_OMNI 13
35 #define CTRL_X_SPELL 14
36 #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38 #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
40 static char *ctrl_x_msgs
[] =
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
43 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
45 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
53 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
55 N_(" Omni completion (^O^N^P)"),
56 N_(" Spelling suggestion (s^N^P)"),
57 N_(" Keyword Local completion (^N^P)"),
60 static char_u e_hitend
[] = N_("Hit end of paragraph");
63 * Structure used to store one match for insert completion.
65 typedef struct compl_S compl_T
;
70 char_u
*cp_str
; /* matched text */
71 char cp_icase
; /* TRUE or FALSE: ignore case */
72 char_u
*(cp_text
[CPT_COUNT
]); /* text for the menu */
73 char_u
*cp_fname
; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
75 int cp_flags
; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number
; /* sequence number */
79 #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
80 #define FREE_FNAME (2)
83 * All the current matches are stored in a list.
84 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
89 static compl_T
*compl_first_match
= NULL
;
90 static compl_T
*compl_curr_match
= NULL
;
91 static compl_T
*compl_shown_match
= NULL
;
93 /* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95 static int compl_enter_selects
= FALSE
;
97 /* When "compl_leader" is not NULL only matches that start with this string
99 static char_u
*compl_leader
= NULL
;
101 static int compl_get_longest
= FALSE
; /* put longest common string
104 static int compl_used_match
; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
108 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_add
__ARGS((char_u
*str
, int len
, int icase
, char_u
*fname
, char_u
**cptext
, int cdir
, int flags
, int adup
));
133 static int ins_compl_equal
__ARGS((compl_T
*match
, char_u
*str
, int len
));
134 static void ins_compl_longest_match
__ARGS((compl_T
*match
));
135 static void ins_compl_add_matches
__ARGS((int num_matches
, char_u
**matches
, int icase
));
136 static int ins_compl_make_cyclic
__ARGS((void));
137 static void ins_compl_upd_pum
__ARGS((void));
138 static void ins_compl_del_pum
__ARGS((void));
139 static int pum_wanted
__ARGS((void));
140 static int pum_enough_matches
__ARGS((void));
141 static void ins_compl_dictionaries
__ARGS((char_u
*dict
, char_u
*pat
, int flags
, int thesaurus
));
142 static void ins_compl_files
__ARGS((int count
, char_u
**files
, int thesaurus
, int flags
, regmatch_T
*regmatch
, char_u
*buf
, int *dir
));
143 static char_u
*find_line_end
__ARGS((char_u
*ptr
));
144 static void ins_compl_free
__ARGS((void));
145 static void ins_compl_clear
__ARGS((void));
146 static int ins_compl_bs
__ARGS((void));
147 static void ins_compl_new_leader
__ARGS((void));
148 static void ins_compl_addleader
__ARGS((int c
));
149 static void ins_compl_restart
__ARGS((void));
150 static void ins_compl_set_original_text
__ARGS((char_u
*str
));
151 static void ins_compl_addfrommatch
__ARGS((void));
152 static int ins_compl_prep
__ARGS((int c
));
153 static buf_T
*ins_compl_next_buf
__ARGS((buf_T
*buf
, int flag
));
154 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
155 static void ins_compl_add_list
__ARGS((list_T
*list
));
157 static int ins_compl_get_exp
__ARGS((pos_T
*ini
));
158 static void ins_compl_delete
__ARGS((void));
159 static void ins_compl_insert
__ARGS((void));
160 static int ins_compl_next
__ARGS((int allow_get_expansion
, int count
, int insert_match
));
161 static int ins_compl_key2dir
__ARGS((int c
));
162 static int ins_compl_pum_key
__ARGS((int c
));
163 static int ins_compl_key2count
__ARGS((int c
));
164 static int ins_compl_use_match
__ARGS((int c
));
165 static int ins_complete
__ARGS((int c
));
166 static int quote_meta
__ARGS((char_u
*dest
, char_u
*str
, int len
));
167 #endif /* FEAT_INS_EXPAND */
169 #define BACKSPACE_CHAR 1
170 #define BACKSPACE_WORD 2
171 #define BACKSPACE_WORD_NOT_SPACE 3
172 #define BACKSPACE_LINE 4
174 static void ins_redraw
__ARGS((int ready
));
175 static void ins_ctrl_v
__ARGS((void));
176 static void undisplay_dollar
__ARGS((void));
177 static void insert_special
__ARGS((int, int, int));
178 static void internal_format
__ARGS((int textwidth
, int second_indent
, int flags
, int format_only
));
179 static void check_auto_format
__ARGS((int));
180 static void redo_literal
__ARGS((int c
));
181 static void start_arrow
__ARGS((pos_T
*end_insert_pos
));
183 static void check_spell_redraw
__ARGS((void));
184 static void spell_back_to_badword
__ARGS((void));
185 static int spell_bad_len
= 0; /* length of located bad word */
187 static void stop_insert
__ARGS((pos_T
*end_insert_pos
, int esc
));
188 static int echeck_abbr
__ARGS((int));
190 static void replace_push_off
__ARGS((int c
));
192 static int replace_pop
__ARGS((void));
193 static void replace_join
__ARGS((int off
));
194 static void replace_pop_ins
__ARGS((void));
196 static void mb_replace_pop_ins
__ARGS((int cc
));
198 static void replace_flush
__ARGS((void));
199 static void replace_do_bs
__ARGS((void));
201 static int cindent_on
__ARGS((void));
203 static void ins_reg
__ARGS((void));
204 static void ins_ctrl_g
__ARGS((void));
205 static void ins_ctrl_hat
__ARGS((void));
206 static int ins_esc
__ARGS((long *count
, int cmdchar
, int nomove
));
207 #ifdef FEAT_RIGHTLEFT
208 static void ins_ctrl_
__ARGS((void));
211 static int ins_start_select
__ARGS((int c
));
213 static void ins_insert
__ARGS((int replaceState
));
214 static void ins_ctrl_o
__ARGS((void));
215 static void ins_shift
__ARGS((int c
, int lastc
));
216 static void ins_del
__ARGS((void));
217 static int ins_bs
__ARGS((int c
, int mode
, int *inserted_space_p
));
219 static void ins_mouse
__ARGS((int c
));
220 static void ins_mousescroll
__ARGS((int up
));
222 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
223 static void ins_tabline
__ARGS((int c
));
225 static void ins_left
__ARGS((void));
226 static void ins_home
__ARGS((int c
));
227 static void ins_end
__ARGS((int c
));
228 static void ins_s_left
__ARGS((void));
229 static void ins_right
__ARGS((void));
230 static void ins_s_right
__ARGS((void));
231 static void ins_up
__ARGS((int startcol
));
232 static void ins_pageup
__ARGS((void));
233 static void ins_down
__ARGS((int startcol
));
234 static void ins_pagedown
__ARGS((void));
236 static void ins_drop
__ARGS((void));
238 static int ins_tab
__ARGS((void));
239 static int ins_eol
__ARGS((int c
));
241 static int ins_digraph
__ARGS((void));
243 static int ins_copychar
__ARGS((linenr_T lnum
));
244 static int ins_ctrl_ey
__ARGS((int tc
));
245 #ifdef FEAT_SMARTINDENT
246 static void ins_try_si
__ARGS((int c
));
248 static colnr_T get_nolist_virtcol
__ARGS((void));
250 static colnr_T Insstart_textlen
; /* length of line when insert started */
251 static colnr_T Insstart_blank_vcol
; /* vcol for first inserted blank */
253 static char_u
*last_insert
= NULL
; /* the text of the previous insert,
254 K_SPECIAL and CSI are escaped */
255 static int last_insert_skip
; /* nr of chars in front of previous insert */
256 static int new_insert_skip
; /* nr of chars in front of current insert */
257 static int did_restart_edit
; /* "restart_edit" when calling edit() */
260 static int can_cindent
; /* may do cindenting on this line */
263 static int old_indent
= 0; /* for ^^D command in insert mode */
265 #ifdef FEAT_RIGHTLEFT
266 static int revins_on
; /* reverse insert mode on */
267 static int revins_chars
; /* how much to skip after edit */
268 static int revins_legal
; /* was the last char 'legal'? */
269 static int revins_scol
; /* start column of revins session */
272 static int ins_need_undo
; /* call u_save() before inserting a
273 char. Set when edit() is called.
274 after that arrow_used is used. */
276 static int did_add_space
= FALSE
; /* auto_format() added an extra space
280 * edit(): Start inserting text.
283 * 'i' normal insert command
284 * 'a' normal append command
285 * 'R' replace command
286 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
287 * but still only one <CR> is inserted. The <Esc> is not used for redo.
289 * 'V' "gR" command for Virtual Replace mode.
290 * 'v' "gr" command for single character Virtual Replace mode.
292 * This function is not called recursively. For CTRL-O commands, it returns
293 * and lets the caller handle the Normal-mode command.
295 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
298 edit(cmdchar
, startln
, count
)
300 int startln
; /* if set, insert at start of line */
307 static linenr_T o_lnum
= 0;
309 int did_backspace
= TRUE
; /* previous char was backspace */
311 int line_is_white
= FALSE
; /* line is empty before insert */
313 linenr_T old_topline
= 0; /* topline before insertion */
315 int old_topfill
= -1;
317 int inserted_space
= FALSE
; /* just inserted a space */
318 int replaceState
= REPLACE
;
319 int nomove
= FALSE
; /* don't move cursor on return */
321 /* Remember whether editing was restarted after CTRL-O. */
322 did_restart_edit
= restart_edit
;
324 /* sleep before redrawing, needed for "CTRL-O :" that results in an
326 check_for_delay(TRUE
);
329 /* Don't allow inserting in the sandbox. */
336 /* Don't allow changes in the buffer while editing the cmdline. The
337 * caller of getcmdline() may get confused. */
344 #ifdef FEAT_INS_EXPAND
345 /* Don't allow recursive insert mode when busy with completion. */
346 if (compl_started
|| pum_visible())
351 ins_compl_clear(); /* clear stuff for CTRL-X mode */
356 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
358 if (cmdchar
!= 'r' && cmdchar
!= 'v')
363 else if (cmdchar
== 'V')
367 set_vim_var_string(VV_INSERTMODE
, ptr
, 1);
369 apply_autocmds(EVENT_INSERTENTER
, NULL
, NULL
, FALSE
, curbuf
);
375 * When doing a paste with the middle mouse button, Insstart is set to
376 * where the paste started.
378 if (where_paste_started
.lnum
!= 0)
379 Insstart
= where_paste_started
;
383 Insstart
= curwin
->w_cursor
;
387 Insstart_textlen
= linetabsize(ml_get_curline());
388 Insstart_blank_vcol
= MAXCOL
;
392 if (cmdchar
!= NUL
&& restart_edit
== 0)
395 AppendNumberToRedobuff(count
);
397 if (cmdchar
== 'V' || cmdchar
== 'v')
399 /* "gR" or "gr" command */
400 AppendCharToRedobuff('g');
401 AppendCharToRedobuff((cmdchar
== 'v') ? 'r' : 'R');
406 AppendCharToRedobuff(cmdchar
);
407 if (cmdchar
== 'g') /* "gI" command */
408 AppendCharToRedobuff('I');
409 else if (cmdchar
== 'r') /* "r<CR>" command */
410 count
= 1; /* insert only one <CR> */
420 EMSG(farsi_text_3
); /* encoded in Farsi */
428 else if (cmdchar
== 'V' || cmdchar
== 'v')
431 replaceState
= VREPLACE
;
432 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
433 vr_lines_changed
= 1;
439 stop_insert_mode
= FALSE
;
442 * Need to recompute the cursor position, it might move when the cursor is
443 * on a TAB or special character.
448 * Enable langmap or IME, indicated by 'iminsert'.
449 * Note that IME may enabled/disabled without us noticing here, thus the
450 * 'iminsert' value may not reflect what is actually used. It is updated
451 * when hitting <Esc>.
453 if (curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
455 #ifdef USE_IM_CONTROL
456 im_set_active(curbuf
->b_p_iminsert
== B_IMODE_IM
);
462 #ifdef FEAT_CMDL_INFO
465 #ifdef FEAT_RIGHTLEFT
466 /* there is no reverse replace mode */
467 revins_on
= (State
== INSERT
&& p_ri
);
476 * Handle restarting Insert mode.
477 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
478 * restart_edit non-zero, and something in the stuff buffer.
480 if (restart_edit
!= 0 && stuff_empty())
484 * After a paste we consider text typed to be part of the insert for
485 * the pasted text. You can backspace over the pasted text too.
487 if (where_paste_started
.lnum
)
495 * If the cursor was after the end-of-line before the CTRL-O and it is
496 * now at the end-of-line, put it after the end-of-line (this is not
497 * correct in very rare cases).
498 * Also do this if curswant is greater than the current virtual
499 * column. Eg after "^O$" or "^O80|".
503 if (((ins_at_eol
&& curwin
->w_cursor
.lnum
== o_lnum
)
504 || curwin
->w_curswant
> curwin
->w_virtcol
)
505 && *(ptr
= ml_get_curline() + curwin
->w_cursor
.col
) != NUL
)
508 ++curwin
->w_cursor
.col
;
512 i
= (*mb_ptr2len
)(ptr
);
514 curwin
->w_cursor
.col
+= i
;
523 /* we are in insert mode now, don't need to start it anymore */
524 need_start_insertmode
= FALSE
;
526 /* Need to save the line for undo before inserting the first char. */
527 ins_need_undo
= TRUE
;
530 where_paste_started
.lnum
= 0;
536 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
538 if (!p_im
&& did_restart_edit
== 0)
543 * If 'showmode' is set, show the current (insert/replace/..) mode.
544 * A warning message for changing a readonly file is given here, before
545 * actually changing anything. It's put after the mode, if any.
548 if (p_smd
&& msg_silent
== 0)
551 if (!p_im
&& did_restart_edit
== 0)
552 change_warning(i
+ 1);
555 ui_cursor_shape(); /* may show different cursor shape */
558 do_digraph(-1); /* clear digraphs */
562 * Get the current length of the redo buffer, those characters have to be
563 * skipped if we want to get to the inserted characters.
565 ptr
= get_inserted();
570 new_insert_skip
= (int)STRLEN(ptr
);
577 * Main loop in Insert mode: repeat until Insert mode is left.
581 #ifdef FEAT_RIGHTLEFT
583 revins_scol
= -1; /* reset on illegal motions */
587 if (arrow_used
) /* don't repeat insert when arrow key used */
590 if (stop_insert_mode
)
592 /* ":stopinsert" used or 'insertmode' reset */
597 /* set curwin->w_curswant for next K_DOWN or K_UP */
599 curwin
->w_set_curswant
= TRUE
;
601 /* If there is no typeahead may check for timestamps (e.g., for when a
602 * menu invoked a shell command). */
605 did_check_timestamps
= FALSE
;
606 if (need_check_timestamps
)
607 check_timestamps(FALSE
);
611 * When emsg() was called msg_scroll will have been set.
616 /* When 'mousefocus' is set a mouse movement may have taken us to
617 * another window. "need_mouse_correct" may then be set because of an
619 if (need_mouse_correct
)
624 /* Open fold at the cursor line, according to 'foldopen'. */
625 if (fdo_flags
& FDO_INSERT
)
627 /* Close folds where the cursor isn't, according to 'foldclose' */
633 * If we inserted a character at the last position of the last line in
634 * the window, scroll the window one line up. This avoids an extra
636 * This is detected when the cursor column is smaller after inserting
638 * Don't do this when the topline changed already, it has
639 * already been adjusted (by insertchar() calling open_line())).
641 if (curbuf
->b_mod_set
644 && curwin
->w_topline
== old_topline
646 && curwin
->w_topfill
== old_topfill
650 mincol
= curwin
->w_wcol
;
651 validate_cursor_col();
653 if ((int)curwin
->w_wcol
< (int)mincol
- curbuf
->b_p_ts
654 && curwin
->w_wrow
== W_WINROW(curwin
)
655 + curwin
->w_height
- 1 - p_so
656 && (curwin
->w_cursor
.lnum
!= curwin
->w_topline
658 || curwin
->w_topfill
> 0
663 if (curwin
->w_topfill
> 0)
668 if (hasFolding(curwin
->w_topline
, NULL
, &old_topline
))
669 set_topline(curwin
, old_topline
+ 1);
672 set_topline(curwin
, curwin
->w_topline
+ 1);
676 /* May need to adjust w_topline to show the cursor. */
679 did_backspace
= FALSE
;
681 validate_cursor(); /* may set must_redraw */
684 * Redraw the display when no characters are waiting.
685 * Also shows mode, ruler and positions cursor.
689 #ifdef FEAT_SCROLLBIND
691 do_check_scrollbind(TRUE
);
695 old_topline
= curwin
->w_topline
;
697 old_topfill
= curwin
->w_topfill
;
700 #ifdef USE_ON_FLY_SCROLL
701 dont_scroll
= FALSE
; /* allow scrolling here */
705 * Get a character for Insert mode.
707 lastc
= c
; /* remember previous char for CTRL-D */
711 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
712 did_cursorhold
= TRUE
;
715 #ifdef FEAT_RIGHTLEFT
716 if (p_hkmap
&& KeyTyped
)
717 c
= hkmap(c
); /* Hebrew mode mapping */
720 if (p_fkmap
&& KeyTyped
)
721 c
= fkmap(c
); /* Farsi mode mapping */
724 #ifdef FEAT_INS_EXPAND
726 * Special handling of keys while the popup menu is visible or wanted
727 * and the cursor is still in the completed word. Only when there is
728 * a match, skip this when no matches were found.
732 && curwin
->w_cursor
.col
>= compl_col
733 && (compl_shown_match
== NULL
734 || compl_shown_match
!= compl_shown_match
->cp_next
))
736 /* BS: Delete one character from "compl_leader". */
737 if ((c
== K_BS
|| c
== Ctrl_H
)
738 && curwin
->w_cursor
.col
> compl_col
739 && (c
= ins_compl_bs()) == NUL
)
742 /* When no match was selected or it was edited. */
743 if (!compl_used_match
)
745 /* CTRL-L: Add one character from the current match to
746 * "compl_leader". Except when at the original match and
747 * there is nothing to add, CTRL-L works like CTRL-P then. */
749 && (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
750 || STRLEN(compl_shown_match
->cp_str
)
751 > curwin
->w_cursor
.col
- compl_col
))
753 ins_compl_addfrommatch();
757 /* A printable, non-white character: Add to "compl_leader". */
758 if (vim_isprintc(c
) && !vim_iswhite(c
))
760 ins_compl_addleader(c
);
764 /* Pressing CTRL-Y selects the current match. When
765 * compl_enter_selects is set the Enter key does the same. */
766 if (c
== Ctrl_Y
|| (compl_enter_selects
767 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
775 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
776 * it does fix up the text when finishing completion. */
777 compl_get_longest
= FALSE
;
778 if (c
!= K_IGNORE
&& ins_compl_prep(c
))
782 /* CTRL-\ CTRL-N goes to Normal mode,
783 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
784 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
787 /* may need to redraw when no more chars available now */
794 if (c
!= Ctrl_N
&& c
!= Ctrl_G
&& c
!= Ctrl_O
)
796 /* it's something else */
800 else if (c
== Ctrl_G
&& p_im
)
807 ins_at_eol
= FALSE
; /* cursor keeps its column */
819 #ifdef FEAT_INS_EXPAND
820 if ((c
== Ctrl_V
|| c
== Ctrl_Q
) && ctrl_x_mode
== CTRL_X_CMDLINE
)
823 if (c
== Ctrl_V
|| c
== Ctrl_Q
)
826 c
= Ctrl_V
; /* pretend CTRL-V is last typed character */
832 # ifdef FEAT_INS_EXPAND
837 /* A key name preceded by a bang means this key is not to be
838 * inserted. Skip ahead to the re-indenting below.
839 * A key name preceded by a star means that indenting has to be
840 * done before inserting the key. */
841 line_is_white
= inindent(0);
842 if (in_cinkeys(c
, '!', line_is_white
))
844 if (can_cindent
&& in_cinkeys(c
, '*', line_is_white
)
845 && stop_arrow() == OK
)
850 #ifdef FEAT_RIGHTLEFT
854 case K_LEFT
: c
= K_RIGHT
; break;
855 case K_S_LEFT
: c
= K_S_RIGHT
; break;
856 case K_C_LEFT
: c
= K_C_RIGHT
; break;
857 case K_RIGHT
: c
= K_LEFT
; break;
858 case K_S_RIGHT
: c
= K_S_LEFT
; break;
859 case K_C_RIGHT
: c
= K_C_LEFT
; break;
865 * If 'keymodel' contains "startsel", may start selection. If it
866 * does, a CTRL-O and c will be stuffed, we need to get these
869 if (ins_start_select(c
))
874 * The big switch to handle a character in insert mode.
878 case ESC
: /* End input mode */
879 if (echeck_abbr(ESC
+ ABBR_OFF
))
883 case Ctrl_C
: /* End input mode */
885 if (c
== Ctrl_C
&& cmdwin_type
!= 0)
887 /* Close the cmdline window. */
888 cmdwin_result
= K_IGNORE
;
889 got_int
= FALSE
; /* don't stop executing autocommands et al. */
898 /* when 'insertmode' set, and not halfway a mapping, don't leave
904 (void)vgetc(); /* flush all buffers */
913 * This is the ONLY return from edit()!
915 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
916 * still puts the cursor back after the inserted text. */
917 if (ins_at_eol
&& gchar_cursor() == NUL
)
918 o_lnum
= curwin
->w_cursor
.lnum
;
920 if (ins_esc(&count
, cmdchar
, nomove
))
923 if (cmdchar
!= 'r' && cmdchar
!= 'v')
924 apply_autocmds(EVENT_INSERTLEAVE
, NULL
, NULL
,
926 did_cursorhold
= FALSE
;
928 return (c
== Ctrl_O
);
932 case Ctrl_Z
: /* suspend when 'insertmode' set */
934 goto normalchar
; /* insert CTRL-Z as normal char */
935 stuffReadbuff((char_u
*)":st\r");
939 case Ctrl_O
: /* execute one command */
940 #ifdef FEAT_COMPL_FUNC
941 if (ctrl_x_mode
== CTRL_X_OMNI
)
944 if (echeck_abbr(Ctrl_O
+ ABBR_OFF
))
948 #ifdef FEAT_VIRTUALEDIT
949 /* don't move the cursor left when 'virtualedit' has "onemore". */
950 if (ve_flags
& VE_ONEMORE
)
959 case K_INS
: /* toggle insert/replace mode */
961 ins_insert(replaceState
);
964 case K_SELECT
: /* end of Select mode mapping - ignore */
968 case K_SNIFF
: /* Sniff command received */
969 stuffcharReadbuff(K_SNIFF
);
973 case K_HELP
: /* Help key works like <ESC> <Help> */
976 stuffcharReadbuff(K_HELP
);
978 need_start_insertmode
= TRUE
;
981 #ifdef FEAT_NETBEANS_INTG
982 case K_F21
: /* NetBeans command */
983 ++no_mapping
; /* don't map the next key hits */
986 netbeans_keycommand(i
);
990 case K_ZERO
: /* Insert the previously inserted text. */
993 /* For ^@ the trailing ESC will end the insert, unless there is an
995 if (stuff_inserted(NUL
, 1L, (c
== Ctrl_A
)) == FAIL
996 && c
!= Ctrl_A
&& !p_im
)
997 goto doESCkey
; /* quit insert mode */
998 inserted_space
= FALSE
;
1001 case Ctrl_R
: /* insert the contents of a register */
1003 auto_format(FALSE
, TRUE
);
1004 inserted_space
= FALSE
;
1007 case Ctrl_G
: /* commands starting with CTRL-G */
1011 case Ctrl_HAT
: /* switch input mode and/or langmap */
1015 #ifdef FEAT_RIGHTLEFT
1016 case Ctrl__
: /* switch between languages */
1023 case Ctrl_D
: /* Make indent one shiftwidth smaller. */
1024 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1025 if (ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
1030 case Ctrl_T
: /* Make indent one shiftwidth greater. */
1031 # ifdef FEAT_INS_EXPAND
1032 if (c
== Ctrl_T
&& ctrl_x_mode
== CTRL_X_THESAURUS
)
1034 if (has_compl_option(FALSE
))
1039 ins_shift(c
, lastc
);
1040 auto_format(FALSE
, TRUE
);
1041 inserted_space
= FALSE
;
1044 case K_DEL
: /* delete character under the cursor */
1047 auto_format(FALSE
, TRUE
);
1050 case K_BS
: /* delete character before the cursor */
1052 did_backspace
= ins_bs(c
, BACKSPACE_CHAR
, &inserted_space
);
1053 auto_format(FALSE
, TRUE
);
1056 case Ctrl_W
: /* delete word before the cursor */
1057 did_backspace
= ins_bs(c
, BACKSPACE_WORD
, &inserted_space
);
1058 auto_format(FALSE
, TRUE
);
1061 case Ctrl_U
: /* delete all inserted text in current line */
1062 # ifdef FEAT_COMPL_FUNC
1063 /* CTRL-X CTRL-U completes with 'completefunc'. */
1064 if (ctrl_x_mode
== CTRL_X_FUNCTION
)
1067 did_backspace
= ins_bs(c
, BACKSPACE_LINE
, &inserted_space
);
1068 auto_format(FALSE
, TRUE
);
1069 inserted_space
= FALSE
;
1073 case K_LEFTMOUSE
: /* mouse keys */
1074 case K_LEFTMOUSE_NM
:
1077 case K_LEFTRELEASE_NM
:
1080 case K_MIDDLERELEASE
:
1083 case K_RIGHTRELEASE
:
1093 case K_MOUSEDOWN
: /* Default action for scroll wheel up: scroll up */
1094 ins_mousescroll(FALSE
);
1097 case K_MOUSEUP
: /* Default action for scroll wheel down: scroll down */
1098 ins_mousescroll(TRUE
);
1101 #ifdef FEAT_GUI_TABLINE
1108 case K_IGNORE
: /* Something mapped to nothing */
1112 case K_CURSORHOLD
: /* Didn't type something for a while. */
1113 apply_autocmds(EVENT_CURSORHOLDI
, NULL
, NULL
, FALSE
, curbuf
);
1114 did_cursorhold
= TRUE
;
1119 /* On Win32 ignore <M-F4>, we get it when closing the window was
1122 if (mod_mask
!= MOD_MASK_ALT
)
1128 case K_VER_SCROLLBAR
:
1132 case K_HOR_SCROLLBAR
:
1137 case K_HOME
: /* <Home> */
1144 case K_END
: /* <End> */
1151 case K_LEFT
: /* <Left> */
1152 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1158 case K_S_LEFT
: /* <S-Left> */
1163 case K_RIGHT
: /* <Right> */
1164 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1170 case K_S_RIGHT
: /* <S-Right> */
1175 case K_UP
: /* <Up> */
1176 #ifdef FEAT_INS_EXPAND
1180 if (mod_mask
& MOD_MASK_SHIFT
)
1186 case K_S_UP
: /* <S-Up> */
1189 #ifdef FEAT_INS_EXPAND
1196 case K_DOWN
: /* <Down> */
1197 #ifdef FEAT_INS_EXPAND
1201 if (mod_mask
& MOD_MASK_SHIFT
)
1207 case K_S_DOWN
: /* <S-Down> */
1210 #ifdef FEAT_INS_EXPAND
1218 case K_DROP
: /* drag-n-drop event */
1223 case K_S_TAB
: /* When not mapped, use like a normal TAB */
1227 case TAB
: /* TAB or Complete patterns along path */
1228 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1229 if (ctrl_x_mode
== CTRL_X_PATH_PATTERNS
)
1232 inserted_space
= FALSE
;
1234 goto normalchar
; /* insert TAB as a normal char */
1235 auto_format(FALSE
, TRUE
);
1238 case K_KENTER
: /* <Enter> */
1243 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1244 /* In a quickfix window a <CR> jumps to the error under the
1246 if (bt_quickfix(curbuf
) && c
== CAR
)
1248 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
1249 do_cmdline_cmd((char_u
*)".cc");
1250 else /* location list window */
1251 do_cmdline_cmd((char_u
*)".ll");
1256 if (cmdwin_type
!= 0)
1258 /* Execute the command in the cmdline window. */
1259 cmdwin_result
= CAR
;
1263 if (ins_eol(c
) && !p_im
)
1264 goto doESCkey
; /* out of memory */
1265 auto_format(FALSE
, FALSE
);
1266 inserted_space
= FALSE
;
1269 #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
1270 case Ctrl_K
: /* digraph or keyword completion */
1271 # ifdef FEAT_INS_EXPAND
1272 if (ctrl_x_mode
== CTRL_X_DICTIONARY
)
1274 if (has_compl_option(TRUE
))
1279 # ifdef FEAT_DIGRAPHS
1287 #ifdef FEAT_INS_EXPAND
1288 case Ctrl_X
: /* Enter CTRL-X mode */
1292 case Ctrl_RSB
: /* Tag name completion after ^X */
1293 if (ctrl_x_mode
!= CTRL_X_TAGS
)
1297 case Ctrl_F
: /* File name completion after ^X */
1298 if (ctrl_x_mode
!= CTRL_X_FILES
)
1302 case 's': /* Spelling completion after ^X */
1304 if (ctrl_x_mode
!= CTRL_X_SPELL
)
1309 case Ctrl_L
: /* Whole line completion after ^X */
1310 #ifdef FEAT_INS_EXPAND
1311 if (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
1314 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1317 if (echeck_abbr(Ctrl_L
+ ABBR_OFF
))
1323 #ifdef FEAT_INS_EXPAND
1326 case Ctrl_P
: /* Do previous/next pattern completion */
1328 /* if 'complete' is empty then plain ^P is no longer special,
1329 * but it is under other ^X modes */
1330 if (*curbuf
->b_p_cpt
== NUL
1332 && !(compl_cont_status
& CONT_LOCAL
))
1336 if (ins_complete(c
) == FAIL
)
1337 compl_cont_status
= 0;
1339 #endif /* FEAT_INS_EXPAND */
1341 case Ctrl_Y
: /* copy from previous line or scroll down */
1342 case Ctrl_E
: /* copy from next line or scroll up */
1348 if (c
== intr_char
) /* special interrupt char */
1353 * Insert a nomal character.
1356 #ifdef FEAT_SMARTINDENT
1357 /* Try to perform smart-indenting. */
1363 inserted_space
= TRUE
;
1366 can_cindent
= FALSE
;
1368 if (Insstart_blank_vcol
== MAXCOL
1369 && curwin
->w_cursor
.lnum
== Insstart
.lnum
)
1370 Insstart_blank_vcol
= get_nolist_virtcol();
1373 if (vim_iswordc(c
) || !echeck_abbr(
1375 /* Add ABBR_OFF for characters above 0x100, this is
1376 * what check_abbr() expects. */
1377 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1381 insert_special(c
, FALSE
, FALSE
);
1382 #ifdef FEAT_RIGHTLEFT
1388 auto_format(FALSE
, TRUE
);
1391 /* When inserting a character the cursor line must never be in a
1396 } /* end of switch (c) */
1399 /* If typed something may trigger CursorHoldI again. */
1400 if (c
!= K_CURSORHOLD
)
1401 did_cursorhold
= FALSE
;
1404 /* If the cursor was moved we didn't just insert a space */
1406 inserted_space
= FALSE
;
1409 if (can_cindent
&& cindent_on()
1410 # ifdef FEAT_INS_EXPAND
1417 * Indent now if a key was typed that is in 'cinkeys'.
1419 if (in_cinkeys(c
, ' ', line_is_white
))
1421 if (stop_arrow() == OK
)
1422 /* re-indent the current line */
1426 #endif /* FEAT_CINDENT */
1433 * Redraw for Insert mode.
1434 * This is postponed until getting the next character to make '$' in the 'cpo'
1435 * option work correctly.
1436 * Only redraw when there are no characters available. This speeds up
1437 * inserting sequences of characters (e.g., for CTRL-R).
1442 int ready
; /* not busy with something */
1447 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1448 * visible, the command might delete it. */
1449 if (ready
&& has_cursormovedI()
1450 && !equalpos(last_cursormoved
, curwin
->w_cursor
)
1451 # ifdef FEAT_INS_EXPAND
1456 apply_autocmds(EVENT_CURSORMOVEDI
, NULL
, NULL
, FALSE
, curbuf
);
1457 last_cursormoved
= curwin
->w_cursor
;
1462 else if (clear_cmdline
|| redraw_cmdline
)
1463 showmode(); /* clear cmdline and show mode */
1466 emsg_on_display
= FALSE
; /* may remove error message now */
1471 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1478 /* may need to redraw when no more chars available now */
1481 if (redrawing() && !char_avail())
1482 edit_putchar('^', TRUE
);
1483 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
1485 #ifdef FEAT_CMDL_INFO
1486 add_to_showcmd_c(Ctrl_V
);
1490 #ifdef FEAT_CMDL_INFO
1493 insert_special(c
, FALSE
, TRUE
);
1494 #ifdef FEAT_RIGHTLEFT
1501 * Put a character directly onto the screen. It's not stored in a buffer.
1502 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1504 static int pc_status
;
1505 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1506 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1507 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1508 #define PC_STATUS_SET 3 /* pc_bytes was filled */
1510 static char_u pc_bytes
[MB_MAXBYTES
+ 1]; /* saved bytes */
1512 static char_u pc_bytes
[2]; /* saved bytes */
1519 edit_putchar(c
, highlight
)
1525 if (ScreenLines
!= NULL
)
1527 update_topline(); /* just in case w_topline isn't valid */
1530 attr
= hl_attr(HLF_8
);
1533 pc_row
= W_WINROW(curwin
) + curwin
->w_wrow
;
1534 pc_col
= W_WINCOL(curwin
);
1535 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1536 pc_status
= PC_STATUS_UNSET
;
1538 #ifdef FEAT_RIGHTLEFT
1541 pc_col
+= W_WIDTH(curwin
) - 1 - curwin
->w_wcol
;
1545 int fix_col
= mb_fix_col(pc_col
, pc_row
);
1547 if (fix_col
!= pc_col
)
1549 screen_putchar(' ', pc_row
, fix_col
, attr
);
1551 pc_status
= PC_STATUS_RIGHT
;
1559 pc_col
+= curwin
->w_wcol
;
1561 if (mb_lefthalve(pc_row
, pc_col
))
1562 pc_status
= PC_STATUS_LEFT
;
1566 /* save the character to be able to put it back */
1567 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1568 if (pc_status
== PC_STATUS_UNSET
)
1571 screen_getbytes(pc_row
, pc_col
, pc_bytes
, &pc_attr
);
1572 pc_status
= PC_STATUS_SET
;
1574 screen_putchar(c
, pc_row
, pc_col
, attr
);
1579 * Undo the previous edit_putchar().
1584 if (pc_status
!= PC_STATUS_UNSET
&& pc_row
>= msg_scrolled
)
1586 #if defined(FEAT_MBYTE)
1587 if (pc_status
== PC_STATUS_RIGHT
)
1589 if (pc_status
== PC_STATUS_RIGHT
|| pc_status
== PC_STATUS_LEFT
)
1590 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1593 screen_puts(pc_bytes
, pc_row
- msg_scrolled
, pc_col
, pc_attr
);
1598 * Called when p_dollar is set: display a '$' at the end of the changed text
1599 * Only works when cursor is in the line that changes.
1611 save_col
= curwin
->w_cursor
.col
;
1612 curwin
->w_cursor
.col
= col
;
1618 /* If on the last byte of a multi-byte move to the first byte. */
1619 p
= ml_get_curline();
1620 curwin
->w_cursor
.col
-= (*mb_head_off
)(p
, p
+ col
);
1623 curs_columns(FALSE
); /* recompute w_wrow and w_wcol */
1624 if (curwin
->w_wcol
< W_WIDTH(curwin
))
1626 edit_putchar('$', FALSE
);
1627 dollar_vcol
= curwin
->w_virtcol
;
1629 curwin
->w_cursor
.col
= save_col
;
1633 * Call this function before moving the cursor from the normal insert position
1642 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1647 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1648 * Keep the cursor on the same character.
1649 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1650 * type == INDENT_DEC decrease indent (for CTRL-D)
1651 * type == INDENT_SET set indent to "amount"
1652 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1655 change_indent(type
, amount
, round
, replaced
)
1659 int replaced
; /* replaced character, put on replace stack */
1663 int insstart_less
; /* reduction for Insstart.col */
1670 #ifdef FEAT_VREPLACE
1671 colnr_T orig_col
= 0; /* init for GCC */
1672 char_u
*new_line
, *orig_line
= NULL
; /* init for GCC */
1674 /* VREPLACE mode needs to know what the line was like before changing */
1675 if (State
& VREPLACE_FLAG
)
1677 orig_line
= vim_strsave(ml_get_curline()); /* Deal with NULL below */
1678 orig_col
= curwin
->w_cursor
.col
;
1682 /* for the following tricks we don't want list mode */
1683 save_p_list
= curwin
->w_p_list
;
1684 curwin
->w_p_list
= FALSE
;
1685 vc
= getvcol_nolist(&curwin
->w_cursor
);
1689 * For Replace mode we need to fix the replace stack later, which is only
1690 * possible when the cursor is in the indent. Remember the number of
1691 * characters before the cursor if it's possible.
1693 start_col
= curwin
->w_cursor
.col
;
1695 /* determine offset from first non-blank */
1696 new_cursor_col
= curwin
->w_cursor
.col
;
1697 beginline(BL_WHITE
);
1698 new_cursor_col
-= curwin
->w_cursor
.col
;
1700 insstart_less
= curwin
->w_cursor
.col
;
1703 * If the cursor is in the indent, compute how many screen columns the
1704 * cursor is to the left of the first non-blank.
1706 if (new_cursor_col
< 0)
1707 vcol
= get_indent() - vcol
;
1709 if (new_cursor_col
> 0) /* can't fix replace stack */
1713 * Set the new indent. The cursor will be put on the first non-blank.
1715 if (type
== INDENT_SET
)
1716 (void)set_indent(amount
, SIN_CHANGED
);
1719 #ifdef FEAT_VREPLACE
1720 int save_State
= State
;
1722 /* Avoid being called recursively. */
1723 if (State
& VREPLACE_FLAG
)
1726 shift_line(type
== INDENT_DEC
, round
, 1);
1727 #ifdef FEAT_VREPLACE
1731 insstart_less
-= curwin
->w_cursor
.col
;
1734 * Try to put cursor on same character.
1735 * If the cursor is at or after the first non-blank in the line,
1736 * compute the cursor column relative to the column of the first
1737 * non-blank character.
1738 * If we are not in insert mode, leave the cursor on the first non-blank.
1739 * If the cursor is before the first non-blank, position it relative
1740 * to the first non-blank, counted in screen columns.
1742 if (new_cursor_col
>= 0)
1745 * When changing the indent while the cursor is touching it, reset
1746 * Insstart_col to 0.
1748 if (new_cursor_col
== 0)
1749 insstart_less
= MAXCOL
;
1750 new_cursor_col
+= curwin
->w_cursor
.col
;
1752 else if (!(State
& INSERT
))
1753 new_cursor_col
= curwin
->w_cursor
.col
;
1757 * Compute the screen column where the cursor should be.
1759 vcol
= get_indent() - vcol
;
1760 curwin
->w_virtcol
= (vcol
< 0) ? 0 : vcol
;
1763 * Advance the cursor until we reach the right screen column.
1765 vcol
= last_vcol
= 0;
1766 new_cursor_col
= -1;
1767 ptr
= ml_get_curline();
1768 while (vcol
<= (int)curwin
->w_virtcol
)
1772 if (has_mbyte
&& new_cursor_col
>= 0)
1773 new_cursor_col
+= (*mb_ptr2len
)(ptr
+ new_cursor_col
);
1777 vcol
+= lbr_chartabsize(ptr
+ new_cursor_col
, (colnr_T
)vcol
);
1782 * May need to insert spaces to be able to position the cursor on
1783 * the right screen column.
1785 if (vcol
!= (int)curwin
->w_virtcol
)
1787 curwin
->w_cursor
.col
= new_cursor_col
;
1788 i
= (int)curwin
->w_virtcol
- vcol
;
1792 new_cursor_col
+= i
;
1802 * When changing the indent while the cursor is in it, reset
1803 * Insstart_col to 0.
1805 insstart_less
= MAXCOL
;
1808 curwin
->w_p_list
= save_p_list
;
1810 if (new_cursor_col
<= 0)
1811 curwin
->w_cursor
.col
= 0;
1813 curwin
->w_cursor
.col
= new_cursor_col
;
1814 curwin
->w_set_curswant
= TRUE
;
1815 changed_cline_bef_curs();
1818 * May have to adjust the start of the insert.
1822 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
&& Insstart
.col
!= 0)
1824 if ((int)Insstart
.col
<= insstart_less
)
1827 Insstart
.col
-= insstart_less
;
1829 if ((int)ai_col
<= insstart_less
)
1832 ai_col
-= insstart_less
;
1836 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1837 * If the number of characters before the cursor decreased, need to pop a
1838 * few characters from the replace stack.
1839 * If the number of characters before the cursor increased, need to push a
1840 * few NULs onto the replace stack.
1842 if (REPLACE_NORMAL(State
) && start_col
>= 0)
1844 while (start_col
> (int)curwin
->w_cursor
.col
)
1846 replace_join(0); /* remove a NUL from the replace stack */
1849 while (start_col
< (int)curwin
->w_cursor
.col
|| replaced
)
1854 replace_push(replaced
);
1861 #ifdef FEAT_VREPLACE
1863 * For VREPLACE mode, we also have to fix the replace stack. In this case
1864 * it is always possible because we backspace over the whole line and then
1865 * put it back again the way we wanted it.
1867 if (State
& VREPLACE_FLAG
)
1869 /* If orig_line didn't allocate, just return. At least we did the job,
1870 * even if you can't backspace. */
1871 if (orig_line
== NULL
)
1875 new_line
= vim_strsave(ml_get_curline());
1876 if (new_line
== NULL
)
1879 /* We only put back the new line up to the cursor */
1880 new_line
[curwin
->w_cursor
.col
] = NUL
;
1882 /* Put back original line */
1883 ml_replace(curwin
->w_cursor
.lnum
, orig_line
, FALSE
);
1884 curwin
->w_cursor
.col
= orig_col
;
1886 /* Backspace from cursor to start of line */
1887 backspace_until_column(0);
1889 /* Insert new stuff into line again */
1890 ins_bytes(new_line
);
1898 * Truncate the space at the end of a line. This is to be used only in an
1899 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1903 truncate_spaces(line
)
1908 /* find start of trailing white space */
1909 for (i
= (int)STRLEN(line
) - 1; i
>= 0 && vim_iswhite(line
[i
]); i
--)
1911 if (State
& REPLACE_FLAG
)
1912 replace_join(0); /* remove a NUL from the replace stack */
1917 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1918 || defined(FEAT_COMMENTS) || defined(PROTO)
1920 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1921 * modes correctly. May also be used when not in insert mode at all.
1924 backspace_until_column(col
)
1927 while ((int)curwin
->w_cursor
.col
> col
)
1929 curwin
->w_cursor
.col
--;
1930 if (State
& REPLACE_FLAG
)
1933 (void)del_char(FALSE
);
1938 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1940 * CTRL-X pressed in Insert mode.
1945 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1946 * CTRL-V works like CTRL-N */
1947 if (ctrl_x_mode
!= CTRL_X_CMDLINE
)
1949 /* if the next ^X<> won't ADD nothing, then reset
1950 * compl_cont_status */
1951 if (compl_cont_status
& CONT_N_ADDS
)
1952 compl_cont_status
|= CONT_INTRPT
;
1954 compl_cont_status
= 0;
1955 /* We're not sure which CTRL-X mode it will be yet */
1956 ctrl_x_mode
= CTRL_X_NOT_DEFINED_YET
;
1957 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
1958 edit_submode_pre
= NULL
;
1964 * Return TRUE if the 'dict' or 'tsr' option can be used.
1967 has_compl_option(dict_opt
)
1970 if (dict_opt
? (*curbuf
->b_p_dict
== NUL
&& *p_dict
== NUL
1972 && !curwin
->w_p_spell
1975 : (*curbuf
->b_p_tsr
== NUL
&& *p_tsr
== NUL
))
1978 edit_submode
= NULL
;
1979 msg_attr(dict_opt
? (char_u
*)_("'dictionary' option is empty")
1980 : (char_u
*)_("'thesaurus' option is empty"),
1982 if (emsg_silent
== 0)
1987 ui_delay(2000L, FALSE
);
1995 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1996 * This depends on the current mode.
1999 vim_is_ctrl_x_key(c
)
2002 /* Always allow ^R - let it's results then be checked */
2006 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
2007 if (ins_compl_pum_key(c
))
2010 switch (ctrl_x_mode
)
2012 case 0: /* Not in any CTRL-X mode */
2013 return (c
== Ctrl_N
|| c
== Ctrl_P
|| c
== Ctrl_X
);
2014 case CTRL_X_NOT_DEFINED_YET
:
2015 return ( c
== Ctrl_X
|| c
== Ctrl_Y
|| c
== Ctrl_E
2016 || c
== Ctrl_L
|| c
== Ctrl_F
|| c
== Ctrl_RSB
2017 || c
== Ctrl_I
|| c
== Ctrl_D
|| c
== Ctrl_P
2018 || c
== Ctrl_N
|| c
== Ctrl_T
|| c
== Ctrl_V
2019 || c
== Ctrl_Q
|| c
== Ctrl_U
|| c
== Ctrl_O
2020 || c
== Ctrl_S
|| c
== 's');
2022 return (c
== Ctrl_Y
|| c
== Ctrl_E
);
2023 case CTRL_X_WHOLE_LINE
:
2024 return (c
== Ctrl_L
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2026 return (c
== Ctrl_F
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2027 case CTRL_X_DICTIONARY
:
2028 return (c
== Ctrl_K
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2029 case CTRL_X_THESAURUS
:
2030 return (c
== Ctrl_T
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2032 return (c
== Ctrl_RSB
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2034 case CTRL_X_PATH_PATTERNS
:
2035 return (c
== Ctrl_P
|| c
== Ctrl_N
);
2036 case CTRL_X_PATH_DEFINES
:
2037 return (c
== Ctrl_D
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2039 case CTRL_X_CMDLINE
:
2040 return (c
== Ctrl_V
|| c
== Ctrl_Q
|| c
== Ctrl_P
|| c
== Ctrl_N
2042 #ifdef FEAT_COMPL_FUNC
2043 case CTRL_X_FUNCTION
:
2044 return (c
== Ctrl_U
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2046 return (c
== Ctrl_O
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2049 return (c
== Ctrl_S
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2051 EMSG(_(e_internal
));
2056 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
2057 * case of the originally typed text is used, and the case of the completed
2058 * text is inferred, ie this tries to work out what case you probably wanted
2059 * the rest of the word to be in -- webb
2060 * TODO: make this work for multi-byte characters.
2063 ins_compl_add_infercase(str
, len
, icase
, fname
, dir
, flags
)
2071 int has_lower
= FALSE
;
2072 int was_letter
= FALSE
;
2075 if (p_ic
&& curbuf
->b_p_inf
&& len
< IOSIZE
)
2077 /* Infer case of completed part -- webb */
2078 /* Use IObuff, str would change text in buffer! */
2079 vim_strncpy(IObuff
, str
, len
);
2081 /* Rule 1: Were any chars converted to lower? */
2082 for (idx
= 0; idx
< compl_length
; ++idx
)
2084 if (islower(compl_orig_text
[idx
]))
2087 if (isupper(IObuff
[idx
]))
2089 /* Rule 1 is satisfied */
2090 for (idx
= compl_length
; idx
< len
; ++idx
)
2091 IObuff
[idx
] = TOLOWER_LOC(IObuff
[idx
]);
2098 * Rule 2: No lower case, 2nd consecutive letter converted to
2103 for (idx
= 0; idx
< compl_length
; ++idx
)
2105 if (was_letter
&& isupper(compl_orig_text
[idx
])
2106 && islower(IObuff
[idx
]))
2108 /* Rule 2 is satisfied */
2109 for (idx
= compl_length
; idx
< len
; ++idx
)
2110 IObuff
[idx
] = TOUPPER_LOC(IObuff
[idx
]);
2113 was_letter
= isalpha(compl_orig_text
[idx
]);
2117 /* Copy the original case of the part we typed */
2118 STRNCPY(IObuff
, compl_orig_text
, compl_length
);
2120 return ins_compl_add(IObuff
, len
, icase
, fname
, NULL
, dir
,
2123 return ins_compl_add(str
, len
, icase
, fname
, NULL
, dir
, flags
, FALSE
);
2127 * Add a match to the list of matches.
2128 * If the given string is already in the list of completions, then return
2129 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2130 * maybe because alloc() returns NULL, then FAIL is returned.
2133 ins_compl_add(str
, len
, icase
, fname
, cptext
, cdir
, flags
, adup
)
2138 char_u
**cptext
; /* extra text for popup menu or NULL */
2141 int adup
; /* accept duplicate match */
2144 int dir
= (cdir
== 0 ? compl_direction
: cdir
);
2150 len
= (int)STRLEN(str
);
2153 * If the same match is already present, don't add it.
2155 if (compl_first_match
!= NULL
&& !adup
)
2157 match
= compl_first_match
;
2160 if ( !(match
->cp_flags
& ORIGINAL_TEXT
)
2161 && STRNCMP(match
->cp_str
, str
, len
) == 0
2162 && match
->cp_str
[len
] == NUL
)
2164 match
= match
->cp_next
;
2165 } while (match
!= NULL
&& match
!= compl_first_match
);
2168 /* Remove any popup menu before changing the list of matches. */
2169 ins_compl_del_pum();
2172 * Allocate a new match structure.
2173 * Copy the values to the new match structure.
2175 match
= (compl_T
*)alloc_clear((unsigned)sizeof(compl_T
));
2178 match
->cp_number
= -1;
2179 if (flags
& ORIGINAL_TEXT
)
2180 match
->cp_number
= 0;
2181 if ((match
->cp_str
= vim_strnsave(str
, len
)) == NULL
)
2186 match
->cp_icase
= icase
;
2189 * - compl_curr_match->cp_fname if it is a string equal to fname.
2190 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2191 * - NULL otherwise. --Acevedo */
2193 && compl_curr_match
!= NULL
2194 && compl_curr_match
->cp_fname
!= NULL
2195 && STRCMP(fname
, compl_curr_match
->cp_fname
) == 0)
2196 match
->cp_fname
= compl_curr_match
->cp_fname
;
2197 else if (fname
!= NULL
)
2199 match
->cp_fname
= vim_strsave(fname
);
2200 flags
|= FREE_FNAME
;
2203 match
->cp_fname
= NULL
;
2204 match
->cp_flags
= flags
;
2210 for (i
= 0; i
< CPT_COUNT
; ++i
)
2211 if (cptext
[i
] != NULL
&& *cptext
[i
] != NUL
)
2212 match
->cp_text
[i
] = vim_strsave(cptext
[i
]);
2216 * Link the new match structure in the list of matches.
2218 if (compl_first_match
== NULL
)
2219 match
->cp_next
= match
->cp_prev
= NULL
;
2220 else if (dir
== FORWARD
)
2222 match
->cp_next
= compl_curr_match
->cp_next
;
2223 match
->cp_prev
= compl_curr_match
;
2227 match
->cp_next
= compl_curr_match
;
2228 match
->cp_prev
= compl_curr_match
->cp_prev
;
2231 match
->cp_next
->cp_prev
= match
;
2233 match
->cp_prev
->cp_next
= match
;
2234 else /* if there's nothing before, it is the first match */
2235 compl_first_match
= match
;
2236 compl_curr_match
= match
;
2239 * Find the longest common string if still doing that.
2241 if (compl_get_longest
&& (flags
& ORIGINAL_TEXT
) == 0)
2242 ins_compl_longest_match(match
);
2248 * Return TRUE if "str[len]" matches with match->cp_str, considering
2252 ins_compl_equal(match
, str
, len
)
2257 if (match
->cp_icase
)
2258 return STRNICMP(match
->cp_str
, str
, (size_t)len
) == 0;
2259 return STRNCMP(match
->cp_str
, str
, (size_t)len
) == 0;
2263 * Reduce the longest common string for match "match".
2266 ins_compl_longest_match(match
)
2273 if (compl_leader
== NULL
)
2275 /* First match, use it as a whole. */
2276 compl_leader
= vim_strsave(match
->cp_str
);
2277 if (compl_leader
!= NULL
)
2279 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2281 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
2284 /* When the match isn't there (to avoid matching itself) remove it
2285 * again after redrawing. */
2288 compl_used_match
= FALSE
;
2293 /* Reduce the text if this match differs from compl_leader. */
2301 c1
= mb_ptr2char(p
);
2302 c2
= mb_ptr2char(s
);
2310 if (match
->cp_icase
? (MB_TOLOWER(c1
) != MB_TOLOWER(c2
))
2329 /* Leader was shortened, need to change the inserted text. */
2331 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2333 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
2336 /* When the match isn't there (to avoid matching itself) remove it
2337 * again after redrawing. */
2342 compl_used_match
= FALSE
;
2347 * Add an array of matches to the list of matches.
2351 ins_compl_add_matches(num_matches
, matches
, icase
)
2358 int dir
= compl_direction
;
2360 for (i
= 0; i
< num_matches
&& add_r
!= FAIL
; i
++)
2361 if ((add_r
= ins_compl_add(matches
[i
], -1, icase
,
2362 NULL
, NULL
, dir
, 0, FALSE
)) == OK
)
2363 /* if dir was BACKWARD then honor it just once */
2365 FreeWild(num_matches
, matches
);
2368 /* Make the completion list cyclic.
2369 * Return the number of matches (excluding the original).
2372 ins_compl_make_cyclic()
2377 if (compl_first_match
!= NULL
)
2380 * Find the end of the list.
2382 match
= compl_first_match
;
2383 /* there's always an entry for the compl_orig_text, it doesn't count. */
2384 while (match
->cp_next
!= NULL
&& match
->cp_next
!= compl_first_match
)
2386 match
= match
->cp_next
;
2389 match
->cp_next
= compl_first_match
;
2390 compl_first_match
->cp_prev
= match
;
2396 * Start completion for the complete() function.
2397 * "startcol" is where the matched text starts (1 is first column).
2398 * "list" is the list of matches.
2401 set_completion(startcol
, list
)
2405 /* If already doing completions stop it. */
2406 if (ctrl_x_mode
!= 0)
2407 ins_compl_prep(' ');
2410 if (stop_arrow() == FAIL
)
2413 if (startcol
> (int)curwin
->w_cursor
.col
)
2414 startcol
= curwin
->w_cursor
.col
;
2415 compl_col
= startcol
;
2416 compl_length
= curwin
->w_cursor
.col
- startcol
;
2417 /* compl_pattern doesn't need to be set */
2418 compl_orig_text
= vim_strnsave(ml_get_curline() + compl_col
, compl_length
);
2419 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
2420 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
2423 /* Handle like dictionary completion. */
2424 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
2426 ins_compl_add_list(list
);
2427 compl_matches
= ins_compl_make_cyclic();
2428 compl_started
= TRUE
;
2429 compl_used_match
= TRUE
;
2430 compl_cont_status
= 0;
2432 compl_curr_match
= compl_first_match
;
2433 ins_complete(Ctrl_N
);
2438 /* "compl_match_array" points the currently displayed list of entries in the
2439 * popup menu. It is NULL when there is no popup menu. */
2440 static pumitem_T
*compl_match_array
= NULL
;
2441 static int compl_match_arraysize
;
2444 * Update the screen and when there is any scrolling remove the popup menu.
2451 if (compl_match_array
!= NULL
)
2453 h
= curwin
->w_cline_height
;
2455 if (h
!= curwin
->w_cline_height
)
2456 ins_compl_del_pum();
2461 * Remove any popup menu.
2466 if (compl_match_array
!= NULL
)
2469 vim_free(compl_match_array
);
2470 compl_match_array
= NULL
;
2475 * Return TRUE if the popup menu should be displayed.
2480 /* 'completeopt' must contain "menu" or "menuone" */
2481 if (vim_strchr(p_cot
, 'm') == NULL
)
2484 /* The display looks bad on a B&W display. */
2495 * Return TRUE if there are two or more matches to be shown in the popup menu.
2496 * One if 'completopt' contains "menuone".
2499 pum_enough_matches()
2504 /* Don't display the popup menu if there are no matches or there is only
2505 * one (ignoring the original text). */
2506 compl = compl_first_match
;
2511 || ((compl->cp_flags
& ORIGINAL_TEXT
) == 0 && ++i
== 2))
2513 compl = compl->cp_next
;
2514 } while (compl != compl_first_match
);
2516 if (strstr((char *)p_cot
, "menuone") != NULL
)
2522 * Show the popup menu for the list of matches.
2523 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
2526 ins_compl_show_pum()
2529 compl_T
*shown_compl
= NULL
;
2530 int did_find_shown_match
= FALSE
;
2531 int shown_match_ok
= FALSE
;
2537 if (!pum_wanted() || !pum_enough_matches())
2540 #if defined(FEAT_EVAL)
2541 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2542 do_cmdline_cmd((char_u
*)"if exists('g:loaded_matchparen')|3match none|endif");
2545 /* Update the screen before drawing the popup menu over it. */
2548 if (compl_match_array
== NULL
)
2550 /* Need to build the popup menu list. */
2551 compl_match_arraysize
= 0;
2552 compl = compl_first_match
;
2553 if (compl_leader
!= NULL
)
2554 lead_len
= (int)STRLEN(compl_leader
);
2557 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2558 && (compl_leader
== NULL
2559 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2560 ++compl_match_arraysize
;
2561 compl = compl->cp_next
;
2562 } while (compl != NULL
&& compl != compl_first_match
);
2563 if (compl_match_arraysize
== 0)
2565 compl_match_array
= (pumitem_T
*)alloc_clear(
2566 (unsigned)(sizeof(pumitem_T
)
2567 * compl_match_arraysize
));
2568 if (compl_match_array
!= NULL
)
2570 /* If the current match is the original text don't find the first
2571 * match after it, don't highlight anything. */
2572 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
2573 shown_match_ok
= TRUE
;
2576 compl = compl_first_match
;
2579 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2580 && (compl_leader
== NULL
2581 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2583 if (!shown_match_ok
)
2585 if (compl == compl_shown_match
|| did_find_shown_match
)
2587 /* This item is the shown match or this is the
2588 * first displayed item after the shown match. */
2589 compl_shown_match
= compl;
2590 did_find_shown_match
= TRUE
;
2591 shown_match_ok
= TRUE
;
2594 /* Remember this displayed match for when the
2595 * shown match is just below it. */
2596 shown_compl
= compl;
2600 if (compl->cp_text
[CPT_ABBR
] != NULL
)
2601 compl_match_array
[i
].pum_text
=
2602 compl->cp_text
[CPT_ABBR
];
2604 compl_match_array
[i
].pum_text
= compl->cp_str
;
2605 compl_match_array
[i
].pum_kind
= compl->cp_text
[CPT_KIND
];
2606 compl_match_array
[i
].pum_info
= compl->cp_text
[CPT_INFO
];
2607 if (compl->cp_text
[CPT_MENU
] != NULL
)
2608 compl_match_array
[i
++].pum_extra
=
2609 compl->cp_text
[CPT_MENU
];
2611 compl_match_array
[i
++].pum_extra
= compl->cp_fname
;
2614 if (compl == compl_shown_match
)
2616 did_find_shown_match
= TRUE
;
2618 /* When the original text is the shown match don't set
2619 * compl_shown_match. */
2620 if (compl->cp_flags
& ORIGINAL_TEXT
)
2621 shown_match_ok
= TRUE
;
2623 if (!shown_match_ok
&& shown_compl
!= NULL
)
2625 /* The shown match isn't displayed, set it to the
2626 * previously displayed match. */
2627 compl_shown_match
= shown_compl
;
2628 shown_match_ok
= TRUE
;
2631 compl = compl->cp_next
;
2632 } while (compl != NULL
&& compl != compl_first_match
);
2634 if (!shown_match_ok
) /* no displayed match at all */
2640 /* popup menu already exists, only need to find the current item.*/
2641 for (i
= 0; i
< compl_match_arraysize
; ++i
)
2642 if (compl_match_array
[i
].pum_text
== compl_shown_match
->cp_str
2643 || compl_match_array
[i
].pum_text
2644 == compl_shown_match
->cp_text
[CPT_ABBR
])
2651 if (compl_match_array
!= NULL
)
2653 /* Compute the screen column of the start of the completed text.
2654 * Use the cursor to get all wrapping and other settings right. */
2655 col
= curwin
->w_cursor
.col
;
2656 curwin
->w_cursor
.col
= compl_col
;
2657 pum_display(compl_match_array
, compl_match_arraysize
, cur
);
2658 curwin
->w_cursor
.col
= col
;
2662 #define DICT_FIRST (1) /* use just first element in "dict" */
2663 #define DICT_EXACT (2) /* "dict" is the exact name of a file */
2666 * Add any identifiers that match the given pattern in the list of dictionary
2667 * files "dict_start" to the list of completions.
2670 ins_compl_dictionaries(dict_start
, pat
, flags
, thesaurus
)
2673 int flags
; /* DICT_FIRST and/or DICT_EXACT */
2674 int thesaurus
; /* Thesaurus completion */
2676 char_u
*dict
= dict_start
;
2679 regmatch_T regmatch
;
2684 int dir
= compl_direction
;
2689 /* When 'dictionary' is empty and spell checking is enabled use
2691 if (!thesaurus
&& curwin
->w_p_spell
)
2692 dict
= (char_u
*)"spell";
2701 regmatch
.regprog
= NULL
; /* so that we can goto theend */
2703 /* If 'infercase' is set, don't use 'smartcase' here */
2705 if (curbuf
->b_p_inf
)
2708 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2709 * to only match at the start of a line. Otherwise just match the
2710 * pattern. Also need to double backslashes. */
2711 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2713 char_u
*pat_esc
= vim_strsave_escaped(pat
, (char_u
*)"\\");
2715 if (pat_esc
== NULL
)
2717 i
= (int)STRLEN(pat_esc
) + 10;
2724 vim_snprintf((char *)ptr
, i
, "^\\s*\\zs\\V%s", pat_esc
);
2725 regmatch
.regprog
= vim_regcomp(ptr
, RE_MAGIC
);
2731 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
2732 if (regmatch
.regprog
== NULL
)
2736 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2737 regmatch
.rm_ic
= ignorecase(pat
);
2738 while (*dict
!= NUL
&& !got_int
&& !compl_interrupted
)
2740 /* copy one dictionary file name into buf */
2741 if (flags
== DICT_EXACT
)
2748 /* Expand wildcards in the dictionary name, but do not allow
2749 * backticks (for security, the 'dict' option may have been set in
2751 copy_option_part(&dict
, buf
, LSIZE
, ",");
2753 if (!thesaurus
&& STRCMP(buf
, "spell") == 0)
2757 if (vim_strchr(buf
, '`') != NULL
2758 || expand_wildcards(1, &buf
, &count
, &files
,
2759 EW_FILE
|EW_SILENT
) != OK
)
2766 /* Complete from active spelling. Skip "\<" in the pattern, we
2767 * don't use it as a RE. */
2768 if (pat
[0] == '\\' && pat
[1] == '<')
2772 spell_dump_compl(curbuf
, ptr
, regmatch
.rm_ic
, &dir
, 0);
2776 if (count
> 0) /* avoid warning for using "files" uninit */
2778 ins_compl_files(count
, files
, thesaurus
, flags
,
2779 ®match
, buf
, &dir
);
2780 if (flags
!= DICT_EXACT
)
2781 FreeWild(count
, files
);
2789 vim_free(regmatch
.regprog
);
2794 ins_compl_files(count
, files
, thesaurus
, flags
, regmatch
, buf
, dir
)
2799 regmatch_T
*regmatch
;
2808 for (i
= 0; i
< count
&& !got_int
&& !compl_interrupted
; i
++)
2810 fp
= mch_fopen((char *)files
[i
], "r"); /* open dictionary file */
2811 if (flags
!= DICT_EXACT
)
2813 vim_snprintf((char *)IObuff
, IOSIZE
,
2814 _("Scanning dictionary: %s"), (char *)files
[i
]);
2815 msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
2821 * Read dictionary file line by line.
2822 * Check each line for a match.
2824 while (!got_int
&& !compl_interrupted
2825 && !vim_fgets(buf
, LSIZE
, fp
))
2828 while (vim_regexec(regmatch
, buf
, (colnr_T
)(ptr
- buf
)))
2830 ptr
= regmatch
->startp
[0];
2831 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2832 ptr
= find_line_end(ptr
);
2834 ptr
= find_word_end(ptr
);
2835 add_r
= ins_compl_add_infercase(regmatch
->startp
[0],
2836 (int)(ptr
- regmatch
->startp
[0]),
2837 p_ic
, files
[i
], *dir
, 0);
2843 * Add the other matches on the line
2847 /* Find start of the next word. Skip white
2848 * space and punctuation. */
2849 ptr
= find_word_start(ptr
);
2850 if (*ptr
== NUL
|| *ptr
== NL
)
2854 /* Find end of the word and add it. */
2857 /* Japanese words may have characters in
2858 * different classes, only separate words
2859 * with single-byte non-word characters. */
2862 int l
= (*mb_ptr2len
)(ptr
);
2864 if (l
< 2 && !vim_iswordc(*ptr
))
2870 ptr
= find_word_end(ptr
);
2871 add_r
= ins_compl_add_infercase(wstart
,
2872 (int)(ptr
- wstart
),
2873 p_ic
, files
[i
], *dir
, 0);
2877 /* if dir was BACKWARD then honor it just once */
2879 else if (add_r
== FAIL
)
2881 /* avoid expensive call to vim_regexec() when at end
2883 if (*ptr
== '\n' || got_int
)
2887 ins_compl_check_keys(50);
2895 * Find the start of the next word.
2896 * Returns a pointer to the first char of the word. Also stops at a NUL.
2899 find_word_start(ptr
)
2904 while (*ptr
!= NUL
&& *ptr
!= '\n' && mb_get_class(ptr
) <= 1)
2905 ptr
+= (*mb_ptr2len
)(ptr
);
2908 while (*ptr
!= NUL
&& *ptr
!= '\n' && !vim_iswordc(*ptr
))
2914 * Find the end of the word. Assumes it starts inside a word.
2915 * Returns a pointer to just after the word.
2926 start_class
= mb_get_class(ptr
);
2927 if (start_class
> 1)
2930 ptr
+= (*mb_ptr2len
)(ptr
);
2931 if (mb_get_class(ptr
) != start_class
)
2937 while (vim_iswordc(*ptr
))
2943 * Find the end of the line, omitting CR and NL at the end.
2944 * Returns a pointer to just after the line.
2952 s
= ptr
+ STRLEN(ptr
);
2953 while (s
> ptr
&& (s
[-1] == CAR
|| s
[-1] == NL
))
2959 * Free the list of completions
2967 vim_free(compl_pattern
);
2968 compl_pattern
= NULL
;
2969 vim_free(compl_leader
);
2970 compl_leader
= NULL
;
2972 if (compl_first_match
== NULL
)
2975 ins_compl_del_pum();
2978 compl_curr_match
= compl_first_match
;
2981 match
= compl_curr_match
;
2982 compl_curr_match
= compl_curr_match
->cp_next
;
2983 vim_free(match
->cp_str
);
2984 /* several entries may use the same fname, free it just once. */
2985 if (match
->cp_flags
& FREE_FNAME
)
2986 vim_free(match
->cp_fname
);
2987 for (i
= 0; i
< CPT_COUNT
; ++i
)
2988 vim_free(match
->cp_text
[i
]);
2990 } while (compl_curr_match
!= NULL
&& compl_curr_match
!= compl_first_match
);
2991 compl_first_match
= compl_curr_match
= NULL
;
2997 compl_cont_status
= 0;
2998 compl_started
= FALSE
;
3000 vim_free(compl_pattern
);
3001 compl_pattern
= NULL
;
3002 vim_free(compl_leader
);
3003 compl_leader
= NULL
;
3004 edit_submode_extra
= NULL
;
3005 vim_free(compl_orig_text
);
3006 compl_orig_text
= NULL
;
3007 compl_enter_selects
= FALSE
;
3011 * Return TRUE when Insert completion is active.
3016 return compl_started
;
3020 * Delete one character before the cursor and show the subset of the matches
3021 * that match the word that is now before the cursor.
3022 * Returns the character to be used, NUL if the work is done and another char
3023 * to be got from the user.
3031 line
= ml_get_curline();
3032 p
= line
+ curwin
->w_cursor
.col
;
3033 mb_ptr_back(line
, p
);
3035 /* Stop completion when the whole word was deleted. */
3036 if ((int)(p
- line
) - (int)compl_col
<= 0)
3039 /* Deleted more than what was used to find matches or didn't finish
3040 * finding all matches: need to look for matches all over again. */
3041 if (curwin
->w_cursor
.col
<= compl_col
+ compl_length
3042 || compl_was_interrupted
)
3043 ins_compl_restart();
3045 vim_free(compl_leader
);
3046 compl_leader
= vim_strnsave(line
+ compl_col
, (int)(p
- line
) - compl_col
);
3047 if (compl_leader
!= NULL
)
3049 ins_compl_new_leader();
3056 * Called after changing "compl_leader".
3057 * Show the popup menu with a different set of matches.
3058 * May also search for matches again if the previous search was interrupted.
3061 ins_compl_new_leader()
3063 ins_compl_del_pum();
3065 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
3066 compl_used_match
= FALSE
;
3069 ins_compl_set_original_text(compl_leader
);
3073 spell_bad_len
= 0; /* need to redetect bad word */
3076 * Matches were cleared, need to search for them now. First display
3077 * the changed text before the cursor. Set "compl_restarting" to
3078 * avoid that the first match is inserted.
3084 /* Show the cursor after the match, not after the redrawn text. */
3087 gui_update_cursor(FALSE
, FALSE
);
3090 compl_restarting
= TRUE
;
3091 if (ins_complete(Ctrl_N
) == FAIL
)
3092 compl_cont_status
= 0;
3093 compl_restarting
= FALSE
;
3096 #if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
3097 if (!compl_used_match
)
3099 /* Go to the original text, since none of the matches is inserted. */
3100 if (compl_first_match
->cp_prev
!= NULL
3101 && (compl_first_match
->cp_prev
->cp_flags
& ORIGINAL_TEXT
))
3102 compl_shown_match
= compl_first_match
->cp_prev
;
3104 compl_shown_match
= compl_first_match
;
3105 compl_curr_match
= compl_shown_match
;
3106 compl_shows_dir
= compl_direction
;
3109 compl_enter_selects
= !compl_used_match
;
3111 /* Show the popup menu with a different set of matches. */
3112 ins_compl_show_pum();
3114 /* Don't let Enter select the original text when there is no popup menu. */
3115 if (compl_match_array
== NULL
)
3116 compl_enter_selects
= FALSE
;
3120 * Append one character to the match leader. May reduce the number of
3124 ins_compl_addleader(c
)
3130 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
3132 char_u buf
[MB_MAXBYTES
+ 1];
3134 (*mb_char2bytes
)(c
, buf
);
3136 ins_char_bytes(buf
, cc
);
3142 /* If we didn't complete finding matches we must search again. */
3143 if (compl_was_interrupted
)
3144 ins_compl_restart();
3146 vim_free(compl_leader
);
3147 compl_leader
= vim_strnsave(ml_get_curline() + compl_col
,
3148 curwin
->w_cursor
.col
- compl_col
);
3149 if (compl_leader
!= NULL
)
3150 ins_compl_new_leader();
3154 * Setup for finding completions again without leaving CTRL-X mode. Used when
3155 * BS or a key was typed while still searching for matches.
3161 compl_started
= FALSE
;
3163 compl_cont_status
= 0;
3164 compl_cont_mode
= 0;
3168 * Set the first match, the original text.
3171 ins_compl_set_original_text(str
)
3176 /* Replace the original text entry. */
3177 if (compl_first_match
->cp_flags
& ORIGINAL_TEXT
) /* safety check */
3179 p
= vim_strsave(str
);
3182 vim_free(compl_first_match
->cp_str
);
3183 compl_first_match
->cp_str
= p
;
3189 * Append one character to the match leader. May reduce the number of
3193 ins_compl_addfrommatch()
3196 int len
= curwin
->w_cursor
.col
- compl_col
;
3200 p
= compl_shown_match
->cp_str
;
3201 if ((int)STRLEN(p
) <= len
) /* the match is too short */
3203 /* When still at the original match use the first entry that matches
3205 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
3208 for (cp
= compl_shown_match
->cp_next
; cp
!= NULL
3209 && cp
!= compl_first_match
; cp
= cp
->cp_next
)
3211 if (compl_leader
== NULL
3212 || ins_compl_equal(cp
, compl_leader
,
3213 (int)STRLEN(compl_leader
)))
3219 if (p
== NULL
|| (int)STRLEN(p
) <= len
)
3231 ins_compl_addleader(c
);
3235 * Prepare for Insert mode completion, or stop it.
3236 * Called just after typing a character in Insert mode.
3237 * Returns TRUE when the character is not to be inserted;
3247 /* Forget any previous 'special' messages if this is actually
3248 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3250 if (c
!= Ctrl_R
&& vim_is_ctrl_x_key(c
))
3251 edit_submode_extra
= NULL
;
3253 /* Ignore end of Select mode mapping */
3257 /* Set "compl_get_longest" when finding the first matches. */
3258 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
3259 || (ctrl_x_mode
== 0 && !compl_started
))
3261 compl_get_longest
= (vim_strchr(p_cot
, 'l') != NULL
);
3262 compl_used_match
= TRUE
;
3265 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
)
3268 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3269 * it will be yet. Now we decide.
3275 ctrl_x_mode
= CTRL_X_SCROLL
;
3276 if (!(State
& REPLACE_FLAG
))
3277 edit_submode
= (char_u
*)_(" (insert) Scroll (^E/^Y)");
3279 edit_submode
= (char_u
*)_(" (replace) Scroll (^E/^Y)");
3280 edit_submode_pre
= NULL
;
3284 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
3287 ctrl_x_mode
= CTRL_X_FILES
;
3290 ctrl_x_mode
= CTRL_X_DICTIONARY
;
3293 /* Simply allow ^R to happen without affecting ^X mode */
3296 ctrl_x_mode
= CTRL_X_THESAURUS
;
3298 #ifdef FEAT_COMPL_FUNC
3300 ctrl_x_mode
= CTRL_X_FUNCTION
;
3303 ctrl_x_mode
= CTRL_X_OMNI
;
3308 ctrl_x_mode
= CTRL_X_SPELL
;
3310 ++emsg_off
; /* Avoid getting the E756 error twice. */
3311 spell_back_to_badword();
3316 ctrl_x_mode
= CTRL_X_TAGS
;
3321 ctrl_x_mode
= CTRL_X_PATH_PATTERNS
;
3324 ctrl_x_mode
= CTRL_X_PATH_DEFINES
;
3329 ctrl_x_mode
= CTRL_X_CMDLINE
;
3333 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3334 * just started ^X mode, or there were enough ^X's to cancel
3335 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3336 * do normal expansion when interrupting a different mode (say
3337 * ^X^F^X^P or ^P^X^X^P, see below)
3338 * nothing changes if interrupting mode 0, (eg, the flag
3339 * doesn't change when going to ADDING mode -- Acevedo */
3340 if (!(compl_cont_status
& CONT_INTRPT
))
3341 compl_cont_status
|= CONT_LOCAL
;
3342 else if (compl_cont_mode
!= 0)
3343 compl_cont_status
&= ~CONT_LOCAL
;
3346 /* If we have typed at least 2 ^X's... for modes != 0, we set
3347 * compl_cont_status = 0 (eg, as if we had just started ^X
3349 * For mode 0, we set "compl_cont_mode" to an impossible
3350 * value, in both cases ^X^X can be used to restart the same
3351 * mode (avoiding ADDING mode).
3352 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3353 * 'complete' and local ^P expansions respectively.
3354 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3355 * mode -- Acevedo */
3358 if (compl_cont_mode
!= 0)
3359 compl_cont_status
= 0;
3361 compl_cont_mode
= CTRL_X_NOT_DEFINED_YET
;
3364 edit_submode
= NULL
;
3369 else if (ctrl_x_mode
!= 0)
3371 /* We're already in CTRL-X mode, do we stay in it? */
3372 if (!vim_is_ctrl_x_key(c
))
3374 if (ctrl_x_mode
== CTRL_X_SCROLL
)
3377 ctrl_x_mode
= CTRL_X_FINISHED
;
3378 edit_submode
= NULL
;
3383 if (compl_started
|| ctrl_x_mode
== CTRL_X_FINISHED
)
3385 /* Show error message from attempted keyword completion (probably
3386 * 'Pattern not found') until another key is hit, then go back to
3387 * showing what mode we are in. */
3389 if ((ctrl_x_mode
== 0 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_R
3390 && !ins_compl_pum_key(c
))
3391 || ctrl_x_mode
== CTRL_X_FINISHED
)
3393 /* Get here when we have finished typing a sequence of ^N and
3394 * ^P or other completion characters in CTRL-X mode. Free up
3395 * memory that was used, and make sure we can redo the insert. */
3396 if (compl_curr_match
!= NULL
|| compl_leader
!= NULL
|| c
== Ctrl_E
)
3402 * If any of the original typed text has been changed, eg when
3403 * ignorecase is set, we must add back-spaces to the redo
3404 * buffer. We add as few as necessary to delete just the part
3405 * of the original text that has changed.
3406 * When using the longest match, edited the match or used
3407 * CTRL-E then don't use the current match.
3409 if (compl_curr_match
!= NULL
&& compl_used_match
&& c
!= Ctrl_E
)
3410 ptr
= compl_curr_match
->cp_str
;
3411 else if (compl_leader
!= NULL
)
3414 ptr
= compl_orig_text
;
3415 if (compl_orig_text
!= NULL
)
3417 p
= compl_orig_text
;
3418 for (temp
= 0; p
[temp
] != NUL
&& p
[temp
] == ptr
[temp
];
3423 temp
-= (*mb_head_off
)(compl_orig_text
, p
+ temp
);
3425 for (p
+= temp
; *p
!= NUL
; mb_ptr_adv(p
))
3426 AppendCharToRedobuff(K_BS
);
3429 AppendToRedobuffLit(ptr
+ temp
, -1);
3433 want_cindent
= (can_cindent
&& cindent_on());
3436 * When completing whole lines: fix indent for 'cindent'.
3437 * Otherwise, break line if it's too long.
3439 if (compl_cont_mode
== CTRL_X_WHOLE_LINE
)
3442 /* re-indent the current line */
3446 want_cindent
= FALSE
; /* don't do it again */
3452 int prev_col
= curwin
->w_cursor
.col
;
3454 /* put the cursor on the last char, for 'tw' formatting */
3457 if (stop_arrow() == OK
)
3458 insertchar(NUL
, 0, -1);
3460 && ml_get_curline()[curwin
->w_cursor
.col
] != NUL
)
3464 auto_format(FALSE
, TRUE
);
3466 /* If the popup menu is displayed pressing CTRL-Y means accepting
3467 * the selection without inserting anything. When
3468 * compl_enter_selects is set the Enter key does the same. */
3469 if ((c
== Ctrl_Y
|| (compl_enter_selects
3470 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
3474 /* CTRL-E means completion is Ended, go back to the typed text. */
3478 if (compl_leader
!= NULL
)
3479 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
3480 else if (compl_first_match
!= NULL
)
3481 ins_bytes(compl_orig_text
3482 + curwin
->w_cursor
.col
- compl_col
);
3487 compl_started
= FALSE
;
3489 msg_clr_cmdline(); /* necessary for "noshowmode" */
3491 compl_enter_selects
= FALSE
;
3492 if (edit_submode
!= NULL
)
3494 edit_submode
= NULL
;
3500 * Indent now if a key was typed that is in 'cinkeys'.
3502 if (want_cindent
&& in_cinkeys(KEY_COMPLETE
, ' ', inindent(0)))
3508 /* reset continue_* if we left expansion-mode, if we stay they'll be
3509 * (re)set properly in ins_complete() */
3510 if (!vim_is_ctrl_x_key(c
))
3512 compl_cont_status
= 0;
3513 compl_cont_mode
= 0;
3520 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3521 * (depending on flag) starting from buf and looking for a non-scanned
3522 * buffer (other than curbuf). curbuf is special, if it is called with
3523 * buf=curbuf then it has to be the first call for a given flag/expansion.
3525 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3528 ins_compl_next_buf(buf
, flag
)
3536 if (flag
== 'w') /* just windows */
3539 if (buf
== curbuf
) /* first call for this flag/expansion */
3541 while ((wp
= (wp
->w_next
!= NULL
? wp
->w_next
: firstwin
)) != curwin
3542 && wp
->w_buffer
->b_scanned
)
3550 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3551 * (unlisted buffers)
3552 * When completing whole lines skip unloaded buffers. */
3553 while ((buf
= (buf
->b_next
!= NULL
? buf
->b_next
: firstbuf
)) != curbuf
3557 || (buf
->b_ml
.ml_mfp
== NULL
) != (flag
== 'u')))
3563 #ifdef FEAT_COMPL_FUNC
3564 static void expand_by_function
__ARGS((int type
, char_u
*base
));
3567 * Execute user defined complete function 'completefunc' or 'omnifunc', and
3568 * get matches in "matches".
3571 expand_by_function(type
, base
)
3572 int type
; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
3580 funcname
= (type
== CTRL_X_FUNCTION
) ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
3581 if (*funcname
== NUL
)
3584 /* Call 'completefunc' to obtain the list of matches. */
3585 args
[0] = (char_u
*)"0";
3588 pos
= curwin
->w_cursor
;
3589 matchlist
= call_func_retlist(funcname
, 2, args
, FALSE
);
3590 curwin
->w_cursor
= pos
; /* restore the cursor position */
3591 if (matchlist
== NULL
)
3594 ins_compl_add_list(matchlist
);
3595 list_unref(matchlist
);
3597 #endif /* FEAT_COMPL_FUNC */
3599 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
3601 * Add completions from a list.
3604 ins_compl_add_list(list
)
3608 int dir
= compl_direction
;
3610 /* Go through the List with matches and add each of them. */
3611 for (li
= list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3613 if (ins_compl_add_tv(&li
->li_tv
, dir
) == OK
)
3614 /* if dir was BACKWARD then honor it just once */
3622 * Add a match to the list of matches from a typeval_T.
3623 * If the given string is already in the list of completions, then return
3624 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3625 * maybe because alloc() returns NULL, then FAIL is returned.
3628 ins_compl_add_tv(tv
, dir
)
3635 char_u
*(cptext
[CPT_COUNT
]);
3637 if (tv
->v_type
== VAR_DICT
&& tv
->vval
.v_dict
!= NULL
)
3639 word
= get_dict_string(tv
->vval
.v_dict
, (char_u
*)"word", FALSE
);
3640 cptext
[CPT_ABBR
] = get_dict_string(tv
->vval
.v_dict
,
3641 (char_u
*)"abbr", FALSE
);
3642 cptext
[CPT_MENU
] = get_dict_string(tv
->vval
.v_dict
,
3643 (char_u
*)"menu", FALSE
);
3644 cptext
[CPT_KIND
] = get_dict_string(tv
->vval
.v_dict
,
3645 (char_u
*)"kind", FALSE
);
3646 cptext
[CPT_INFO
] = get_dict_string(tv
->vval
.v_dict
,
3647 (char_u
*)"info", FALSE
);
3648 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"icase", FALSE
) != NULL
)
3649 icase
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"icase");
3650 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"dup", FALSE
) != NULL
)
3651 adup
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"dup");
3655 word
= get_tv_string_chk(tv
);
3656 vim_memset(cptext
, 0, sizeof(cptext
));
3658 if (word
== NULL
|| *word
== NUL
)
3660 return ins_compl_add(word
, -1, icase
, NULL
, cptext
, dir
, 0, adup
);
3665 * Get the next expansion(s), using "compl_pattern".
3666 * The search starts at position "ini" in curbuf and in the direction
3668 * When "compl_started" is FALSE start at that position, otherwise continue
3669 * where we stopped searching before.
3670 * This may return before finding all the matches.
3671 * Return the total number of matches or -1 if still unknown -- Acevedo
3674 ins_compl_get_exp(ini
)
3677 static pos_T first_match_pos
;
3678 static pos_T last_match_pos
;
3679 static char_u
*e_cpt
= (char_u
*)""; /* curr. entry in 'complete' */
3680 static int found_all
= FALSE
; /* Found all matches of a
3682 static buf_T
*ins_buf
= NULL
; /* buffer being scanned */
3692 int found_new_match
;
3693 int type
= ctrl_x_mode
;
3695 char_u
*dict
= NULL
;
3701 for (ins_buf
= firstbuf
; ins_buf
!= NULL
; ins_buf
= ins_buf
->b_next
)
3702 ins_buf
->b_scanned
= 0;
3705 e_cpt
= (compl_cont_status
& CONT_LOCAL
)
3706 ? (char_u
*)"." : curbuf
->b_p_cpt
;
3707 last_match_pos
= first_match_pos
= *ini
;
3710 old_match
= compl_curr_match
; /* remember the last current match */
3711 pos
= (compl_direction
== FORWARD
) ? &last_match_pos
: &first_match_pos
;
3712 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3715 found_new_match
= FAIL
;
3717 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
3718 * or if found_all says this entry is done. For ^X^L only use the
3719 * entries from 'complete' that look in loaded buffers. */
3720 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3721 && (!compl_started
|| found_all
))
3724 while (*e_cpt
== ',' || *e_cpt
== ' ')
3726 if (*e_cpt
== '.' && !curbuf
->b_scanned
)
3729 first_match_pos
= *ini
;
3730 /* So that ^N can match word immediately after cursor */
3731 if (ctrl_x_mode
== 0)
3732 dec(&first_match_pos
);
3733 last_match_pos
= first_match_pos
;
3736 else if (vim_strchr((char_u
*)"buwU", *e_cpt
) != NULL
3737 && (ins_buf
= ins_compl_next_buf(ins_buf
, *e_cpt
)) != curbuf
)
3739 /* Scan a buffer, but not the current one. */
3740 if (ins_buf
->b_ml
.ml_mfp
!= NULL
) /* loaded buffer */
3742 compl_started
= TRUE
;
3743 first_match_pos
.col
= last_match_pos
.col
= 0;
3744 first_match_pos
.lnum
= ins_buf
->b_ml
.ml_line_count
+ 1;
3745 last_match_pos
.lnum
= 0;
3748 else /* unloaded buffer, scan like dictionary */
3751 if (ins_buf
->b_fname
== NULL
)
3753 type
= CTRL_X_DICTIONARY
;
3754 dict
= ins_buf
->b_fname
;
3755 dict_f
= DICT_EXACT
;
3757 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning: %s"),
3758 ins_buf
->b_fname
== NULL
3759 ? buf_spname(ins_buf
)
3760 : ins_buf
->b_sfname
== NULL
3761 ? (char *)ins_buf
->b_fname
3762 : (char *)ins_buf
->b_sfname
);
3763 msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3765 else if (*e_cpt
== NUL
)
3769 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3771 else if (*e_cpt
== 'k' || *e_cpt
== 's')
3774 type
= CTRL_X_DICTIONARY
;
3776 type
= CTRL_X_THESAURUS
;
3777 if (*++e_cpt
!= ',' && *e_cpt
!= NUL
)
3780 dict_f
= DICT_FIRST
;
3784 else if (*e_cpt
== 'i')
3785 type
= CTRL_X_PATH_PATTERNS
;
3786 else if (*e_cpt
== 'd')
3787 type
= CTRL_X_PATH_DEFINES
;
3789 else if (*e_cpt
== ']' || *e_cpt
== 't')
3792 sprintf((char*)IObuff
, _("Scanning tags."));
3793 msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3798 /* in any case e_cpt is advanced to the next entry */
3799 (void)copy_option_part(&e_cpt
, IObuff
, IOSIZE
, ",");
3812 case CTRL_X_PATH_PATTERNS
:
3813 case CTRL_X_PATH_DEFINES
:
3814 find_pattern_in_path(compl_pattern
, compl_direction
,
3815 (int)STRLEN(compl_pattern
), FALSE
, FALSE
,
3816 (type
== CTRL_X_PATH_DEFINES
3817 && !(compl_cont_status
& CONT_SOL
))
3818 ? FIND_DEFINE
: FIND_ANY
, 1L, ACTION_EXPAND
,
3819 (linenr_T
)1, (linenr_T
)MAXLNUM
);
3823 case CTRL_X_DICTIONARY
:
3824 case CTRL_X_THESAURUS
:
3825 ins_compl_dictionaries(
3827 : (type
== CTRL_X_THESAURUS
3828 ? (*curbuf
->b_p_tsr
== NUL
3831 : (*curbuf
->b_p_dict
== NUL
3833 : curbuf
->b_p_dict
)),
3835 dict
!= NULL
? dict_f
3836 : 0, type
== CTRL_X_THESAURUS
);
3841 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3843 p_ic
= ignorecase(compl_pattern
);
3845 /* Find up to TAG_MANY matches. Avoids that an enourmous number
3846 * of matches is found when compl_pattern is empty */
3847 if (find_tags(compl_pattern
, &num_matches
, &matches
,
3848 TAG_REGEXP
| TAG_NAMES
| TAG_NOIC
|
3849 TAG_INS_COMP
| (ctrl_x_mode
? TAG_VERBOSE
: 0),
3850 TAG_MANY
, curbuf
->b_ffname
) == OK
&& num_matches
> 0)
3852 ins_compl_add_matches(num_matches
, matches
, p_ic
);
3858 if (expand_wildcards(1, &compl_pattern
, &num_matches
, &matches
,
3859 EW_FILE
|EW_DIR
|EW_ADDSLASH
|EW_SILENT
) == OK
)
3862 /* May change home directory back to "~". */
3863 tilde_replace(compl_pattern
, num_matches
, matches
);
3864 ins_compl_add_matches(num_matches
, matches
,
3865 #ifdef CASE_INSENSITIVE_FILENAME
3874 case CTRL_X_CMDLINE
:
3875 if (expand_cmdline(&compl_xp
, compl_pattern
,
3876 (int)STRLEN(compl_pattern
),
3877 &num_matches
, &matches
) == EXPAND_OK
)
3878 ins_compl_add_matches(num_matches
, matches
, FALSE
);
3881 #ifdef FEAT_COMPL_FUNC
3882 case CTRL_X_FUNCTION
:
3884 expand_by_function(type
, compl_pattern
);
3890 num_matches
= expand_spelling(first_match_pos
.lnum
,
3891 first_match_pos
.col
, compl_pattern
, &matches
);
3892 if (num_matches
> 0)
3893 ins_compl_add_matches(num_matches
, matches
, p_ic
);
3897 default: /* normal ^P/^N and ^X^L */
3899 * If 'infercase' is set, don't use 'smartcase' here
3902 if (ins_buf
->b_p_inf
)
3905 /* buffers other than curbuf are scanned from the beginning or the
3906 * end but never from the middle, thus setting nowrapscan in this
3907 * buffers is a good idea, on the other hand, we always set
3908 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3910 if (ins_buf
!= curbuf
)
3912 else if (*e_cpt
== '.')
3918 ++msg_silent
; /* Don't want messages for wrapscan. */
3920 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3921 * has added a word that was at the beginning of the line */
3922 if ( ctrl_x_mode
== CTRL_X_WHOLE_LINE
3923 || (compl_cont_status
& CONT_SOL
))
3924 found_new_match
= search_for_exact_line(ins_buf
, pos
,
3925 compl_direction
, compl_pattern
);
3927 found_new_match
= searchit(NULL
, ins_buf
, pos
,
3929 compl_pattern
, 1L, SEARCH_KEEP
+ SEARCH_NFMSG
,
3930 RE_LAST
, (linenr_T
)0);
3934 /* set "compl_started" even on fail */
3935 compl_started
= TRUE
;
3936 first_match_pos
= *pos
;
3937 last_match_pos
= *pos
;
3939 else if (first_match_pos
.lnum
== last_match_pos
.lnum
3940 && first_match_pos
.col
== last_match_pos
.col
)
3941 found_new_match
= FAIL
;
3942 if (found_new_match
== FAIL
)
3944 if (ins_buf
== curbuf
)
3949 /* when ADDING, the text before the cursor matches, skip it */
3950 if ( (compl_cont_status
& CONT_ADDING
) && ins_buf
== curbuf
3951 && ini
->lnum
== pos
->lnum
3952 && ini
->col
== pos
->col
)
3954 ptr
= ml_get_buf(ins_buf
, pos
->lnum
, FALSE
) + pos
->col
;
3955 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3957 if (compl_cont_status
& CONT_ADDING
)
3959 if (pos
->lnum
>= ins_buf
->b_ml
.ml_line_count
)
3961 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
3963 ptr
= skipwhite(ptr
);
3965 len
= (int)STRLEN(ptr
);
3969 char_u
*tmp_ptr
= ptr
;
3971 if (compl_cont_status
& CONT_ADDING
)
3973 tmp_ptr
+= compl_length
;
3974 /* Skip if already inside a word. */
3975 if (vim_iswordp(tmp_ptr
))
3977 /* Find start of next word. */
3978 tmp_ptr
= find_word_start(tmp_ptr
);
3980 /* Find end of this word. */
3981 tmp_ptr
= find_word_end(tmp_ptr
);
3982 len
= (int)(tmp_ptr
- ptr
);
3984 if ((compl_cont_status
& CONT_ADDING
)
3985 && len
== compl_length
)
3987 if (pos
->lnum
< ins_buf
->b_ml
.ml_line_count
)
3989 /* Try next line, if any. the new word will be
3990 * "join" as if the normal command "J" was used.
3991 * IOSIZE is always greater than
3992 * compl_length, so the next STRNCPY always
3993 * works -- Acevedo */
3994 STRNCPY(IObuff
, ptr
, len
);
3995 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
3996 tmp_ptr
= ptr
= skipwhite(ptr
);
3997 /* Find start of next word. */
3998 tmp_ptr
= find_word_start(tmp_ptr
);
3999 /* Find end of next word. */
4000 tmp_ptr
= find_word_end(tmp_ptr
);
4003 if (*ptr
!= ')' && IObuff
[len
- 1] != TAB
)
4005 if (IObuff
[len
- 1] != ' ')
4006 IObuff
[len
++] = ' ';
4007 /* IObuf =~ "\k.* ", thus len >= 2 */
4009 && (IObuff
[len
- 2] == '.'
4010 || (vim_strchr(p_cpo
, CPO_JOINSP
)
4012 && (IObuff
[len
- 2] == '?'
4013 || IObuff
[len
- 2] == '!'))))
4014 IObuff
[len
++] = ' ';
4016 /* copy as much as posible of the new word */
4017 if (tmp_ptr
- ptr
>= IOSIZE
- len
)
4018 tmp_ptr
= ptr
+ IOSIZE
- len
- 1;
4019 STRNCPY(IObuff
+ len
, ptr
, tmp_ptr
- ptr
);
4020 len
+= (int)(tmp_ptr
- ptr
);
4021 flags
|= CONT_S_IPOS
;
4026 if (len
== compl_length
)
4030 if (ins_compl_add_infercase(ptr
, len
, p_ic
,
4031 ins_buf
== curbuf
? NULL
: ins_buf
->b_sfname
,
4032 0, flags
) != NOTDONE
)
4034 found_new_match
= OK
;
4042 /* check if compl_curr_match has changed, (e.g. other type of
4043 * expansion added somenthing) */
4044 if (type
!= 0 && compl_curr_match
!= old_match
)
4045 found_new_match
= OK
;
4047 /* break the loop for specialized modes (use 'complete' just for the
4048 * generic ctrl_x_mode == 0) or when we've found a new match */
4049 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4050 || found_new_match
!= FAIL
)
4054 /* Fill the popup menu as soon as possible. */
4056 ins_compl_check_keys(0);
4058 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4059 || compl_interrupted
)
4061 compl_started
= TRUE
;
4065 /* Mark a buffer scanned when it has been scanned completely */
4066 if (type
== 0 || type
== CTRL_X_PATH_PATTERNS
)
4067 ins_buf
->b_scanned
= TRUE
;
4069 compl_started
= FALSE
;
4072 compl_started
= TRUE
;
4074 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4075 && *e_cpt
== NUL
) /* Got to end of 'complete' */
4076 found_new_match
= FAIL
;
4078 i
= -1; /* total of matches, unknown */
4079 if (found_new_match
== FAIL
4080 || (ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
))
4081 i
= ins_compl_make_cyclic();
4083 /* If several matches were added (FORWARD) or the search failed and has
4084 * just been made cyclic then we have to move compl_curr_match to the next
4085 * or previous entry (if any) -- Acevedo */
4086 compl_curr_match
= compl_direction
== FORWARD
? old_match
->cp_next
4087 : old_match
->cp_prev
;
4088 if (compl_curr_match
== NULL
)
4089 compl_curr_match
= old_match
;
4093 /* Delete the old text being completed. */
4100 * In insert mode: Delete the typed part.
4101 * In replace mode: Put the old characters back, if any.
4103 i
= compl_col
+ (compl_cont_status
& CONT_ADDING
? compl_length
: 0);
4104 backspace_until_column(i
);
4105 changed_cline_bef_curs();
4108 /* Insert the new text being completed. */
4112 ins_bytes(compl_shown_match
->cp_str
+ curwin
->w_cursor
.col
- compl_col
);
4113 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
4114 compl_used_match
= FALSE
;
4116 compl_used_match
= TRUE
;
4120 * Fill in the next completion in the current direction.
4121 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4122 * get more completions. If it is FALSE, then we just do nothing when there
4123 * are no more completions in a given direction. The latter case is used when
4124 * we are still in the middle of finding completions, to allow browsing
4125 * through the ones found so far.
4126 * Return the total number of matches, or -1 if still unknown -- webb.
4128 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4129 * compl_shown_match here.
4131 * Note that this function may be called recursively once only. First with
4132 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4133 * calls this function with "allow_get_expansion" FALSE.
4136 ins_compl_next(allow_get_expansion
, count
, insert_match
)
4137 int allow_get_expansion
;
4138 int count
; /* repeat completion this many times; should
4140 int insert_match
; /* Insert the newly selected match */
4142 int num_matches
= -1;
4145 compl_T
*found_compl
= NULL
;
4146 int found_end
= FALSE
;
4149 if (compl_leader
!= NULL
4150 && (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0)
4152 /* Set "compl_shown_match" to the actually shown match, it may differ
4153 * when "compl_leader" is used to omit some of the matches. */
4154 while (!ins_compl_equal(compl_shown_match
,
4155 compl_leader
, (int)STRLEN(compl_leader
))
4156 && compl_shown_match
->cp_next
!= NULL
4157 && compl_shown_match
->cp_next
!= compl_first_match
)
4158 compl_shown_match
= compl_shown_match
->cp_next
;
4160 /* If we didn't find it searching forward, and compl_shows_dir is
4161 * backward, find the last match. */
4162 if (compl_shows_dir
== BACKWARD
4163 && !ins_compl_equal(compl_shown_match
,
4164 compl_leader
, (int)STRLEN(compl_leader
))
4165 && (compl_shown_match
->cp_next
== NULL
4166 || compl_shown_match
->cp_next
== compl_first_match
))
4168 while (!ins_compl_equal(compl_shown_match
,
4169 compl_leader
, (int)STRLEN(compl_leader
))
4170 && compl_shown_match
->cp_prev
!= NULL
4171 && compl_shown_match
->cp_prev
!= compl_first_match
)
4172 compl_shown_match
= compl_shown_match
->cp_prev
;
4176 if (allow_get_expansion
&& insert_match
4177 && (!(compl_get_longest
|| compl_restarting
) || compl_used_match
))
4178 /* Delete old text to be replaced */
4181 /* When finding the longest common text we stick at the original text,
4182 * don't let CTRL-N or CTRL-P move to the first match. */
4183 advance
= count
!= 1 || !allow_get_expansion
|| !compl_get_longest
;
4185 /* When restarting the search don't insert the first match either. */
4186 if (compl_restarting
)
4189 compl_restarting
= FALSE
;
4192 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4196 if (compl_shows_dir
== FORWARD
&& compl_shown_match
->cp_next
!= NULL
)
4198 compl_shown_match
= compl_shown_match
->cp_next
;
4199 found_end
= (compl_first_match
!= NULL
4200 && (compl_shown_match
->cp_next
== compl_first_match
4201 || compl_shown_match
== compl_first_match
));
4203 else if (compl_shows_dir
== BACKWARD
4204 && compl_shown_match
->cp_prev
!= NULL
)
4206 found_end
= (compl_shown_match
== compl_first_match
);
4207 compl_shown_match
= compl_shown_match
->cp_prev
;
4208 found_end
|= (compl_shown_match
== compl_first_match
);
4212 if (!allow_get_expansion
)
4216 if (compl_shows_dir
== BACKWARD
)
4217 compl_pending
-= todo
+ 1;
4219 compl_pending
+= todo
+ 1;
4226 if (compl_shows_dir
== BACKWARD
)
4233 num_matches
= ins_compl_get_exp(&compl_startpos
);
4235 /* handle any pending completions */
4236 while (compl_pending
!= 0 && compl_direction
== compl_shows_dir
4239 if (compl_pending
> 0 && compl_shown_match
->cp_next
!= NULL
)
4241 compl_shown_match
= compl_shown_match
->cp_next
;
4244 if (compl_pending
< 0 && compl_shown_match
->cp_prev
!= NULL
)
4246 compl_shown_match
= compl_shown_match
->cp_prev
;
4254 if ((compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0
4255 && compl_leader
!= NULL
4256 && !ins_compl_equal(compl_shown_match
,
4257 compl_leader
, (int)STRLEN(compl_leader
)))
4260 /* Remember a matching item. */
4261 found_compl
= compl_shown_match
;
4263 /* Stop at the end of the list when we found a usable match. */
4266 if (found_compl
!= NULL
)
4268 compl_shown_match
= found_compl
;
4271 todo
= 1; /* use first usable match after wrapping around */
4275 /* Insert the text of the new completion, or the compl_leader. */
4278 if (!compl_get_longest
|| compl_used_match
)
4281 ins_bytes(compl_leader
+ curwin
->w_cursor
.col
- compl_col
);
4284 compl_used_match
= FALSE
;
4286 if (!allow_get_expansion
)
4288 /* may undisplay the popup menu first */
4289 ins_compl_upd_pum();
4291 /* redraw to show the user what was inserted */
4294 /* display the updated popup menu */
4295 ins_compl_show_pum();
4299 /* Show the cursor after the match, not after the redrawn text. */
4302 gui_update_cursor(FALSE
, FALSE
);
4306 /* Delete old text to be replaced, since we're still searching and
4307 * don't want to match ourselves! */
4311 /* Enter will select a match when the match wasn't inserted and the popup
4312 * menu is visible. */
4313 compl_enter_selects
= !insert_match
&& compl_match_array
!= NULL
;
4316 * Show the file name for the match (if any)
4317 * Truncate the file name to avoid a wait for return.
4319 if (compl_shown_match
->cp_fname
!= NULL
)
4321 STRCPY(IObuff
, "match in file ");
4322 i
= (vim_strsize(compl_shown_match
->cp_fname
) + 16) - sc_col
;
4326 STRCAT(IObuff
, "<");
4327 STRCAT(IObuff
, compl_shown_match
->cp_fname
+ i
);
4329 redraw_cmdline
= FALSE
; /* don't overwrite! */
4336 * Call this while finding completions, to check whether the user has hit a key
4337 * that should change the currently displayed completion, or exit completion
4338 * mode. Also, when compl_pending is not zero, show a completion as soon as
4340 * "frequency" specifies out of how many calls we actually check.
4343 ins_compl_check_keys(frequency
)
4346 static int count
= 0;
4350 /* Don't check when reading keys from a script. That would break the test
4355 /* Only do this at regular intervals */
4356 if (++count
< frequency
)
4360 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4361 * can't do its work correctly. */
4365 if (vim_is_ctrl_x_key(c
) && c
!= Ctrl_X
&& c
!= Ctrl_R
)
4367 c
= safe_vgetc(); /* Eat the character */
4368 compl_shows_dir
= ins_compl_key2dir(c
);
4369 (void)ins_compl_next(FALSE
, ins_compl_key2count(c
),
4370 c
!= K_UP
&& c
!= K_DOWN
);
4374 /* Need to get the character to have KeyTyped set. We'll put it
4375 * back with vungetc() below. */
4378 /* Don't interrupt completion when the character wasn't typed,
4379 * e.g., when doing @q to replay keys. */
4380 if (c
!= Ctrl_R
&& KeyTyped
)
4381 compl_interrupted
= TRUE
;
4386 if (compl_pending
!= 0 && !got_int
)
4388 int todo
= compl_pending
> 0 ? compl_pending
: -compl_pending
;
4391 (void)ins_compl_next(FALSE
, todo
, TRUE
);
4396 * Decide the direction of Insert mode complete from the key typed.
4397 * Returns BACKWARD or FORWARD.
4400 ins_compl_key2dir(c
)
4403 if (c
== Ctrl_P
|| c
== Ctrl_L
4404 || (pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
4405 || c
== K_S_UP
|| c
== K_UP
)))
4411 * Return TRUE for keys that are used for completion only when the popup menu
4415 ins_compl_pum_key(c
)
4418 return pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
|| c
== K_S_UP
4419 || c
== K_PAGEDOWN
|| c
== K_KPAGEDOWN
|| c
== K_S_DOWN
4420 || c
== K_UP
|| c
== K_DOWN
);
4424 * Decide the number of completions to move forward.
4425 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4428 ins_compl_key2count(c
)
4433 if (ins_compl_pum_key(c
) && c
!= K_UP
&& c
!= K_DOWN
)
4435 h
= pum_get_height();
4437 h
-= 2; /* keep some context */
4444 * Return TRUE if completion with "c" should insert the match, FALSE if only
4445 * to change the currently selected completion.
4448 ins_compl_use_match(c
)
4467 * Do Insert mode completion.
4468 * Called when character "c" was typed, which has a meaning for completion.
4469 * Returns OK if completion was done, FAIL if something failed (out of mem).
4476 int startcol
= 0; /* column where searched text starts */
4477 colnr_T curs_col
; /* cursor column */
4480 compl_direction
= ins_compl_key2dir(c
);
4483 /* First time we hit ^N or ^P (in a row, I mean) */
4486 #ifdef FEAT_SMARTINDENT
4489 can_si_back
= FALSE
;
4491 if (stop_arrow() == FAIL
)
4494 line
= ml_get(curwin
->w_cursor
.lnum
);
4495 curs_col
= curwin
->w_cursor
.col
;
4498 /* if this same ctrl_x_mode has been interrupted use the text from
4499 * "compl_startpos" to the cursor as a pattern to add a new word
4500 * instead of expand the one before the cursor, in word-wise if
4502 * is not in the same line as the cursor then fix it (the line has
4503 * been split because it was longer than 'tw'). if SOL is set then
4504 * skip the previous pattern, a word at the beginning of the line has
4505 * been inserted, we'll look for that -- Acevedo. */
4506 if ((compl_cont_status
& CONT_INTRPT
) == CONT_INTRPT
4507 && compl_cont_mode
== ctrl_x_mode
)
4510 * it is a continued search
4512 compl_cont_status
&= ~CONT_INTRPT
; /* remove INTRPT */
4513 if (ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_PATH_PATTERNS
4514 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4516 if (compl_startpos
.lnum
!= curwin
->w_cursor
.lnum
)
4518 /* line (probably) wrapped, set compl_startpos to the
4519 * first non_blank in the line, if it is not a wordchar
4520 * include it to get a better pattern, but then we don't
4521 * want the "\\<" prefix, check it bellow */
4522 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4523 compl_startpos
.col
= compl_col
;
4524 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4525 compl_cont_status
&= ~CONT_SOL
; /* clear SOL if present */
4529 /* S_IPOS was set when we inserted a word that was at the
4530 * beginning of the line, which means that we'll go to SOL
4531 * mode but first we need to redefine compl_startpos */
4532 if (compl_cont_status
& CONT_S_IPOS
)
4534 compl_cont_status
|= CONT_SOL
;
4535 compl_startpos
.col
= (colnr_T
)(skipwhite(
4537 + compl_startpos
.col
) - line
);
4539 compl_col
= compl_startpos
.col
;
4541 compl_length
= curwin
->w_cursor
.col
- (int)compl_col
;
4542 /* IObuff is used to add a "word from the next line" would we
4543 * have enough space? just being paranoic */
4544 #define MIN_SPACE 75
4545 if (compl_length
> (IOSIZE
- MIN_SPACE
))
4547 compl_cont_status
&= ~CONT_SOL
;
4548 compl_length
= (IOSIZE
- MIN_SPACE
);
4549 compl_col
= curwin
->w_cursor
.col
- compl_length
;
4551 compl_cont_status
|= CONT_ADDING
| CONT_N_ADDS
;
4552 if (compl_length
< 1)
4553 compl_cont_status
&= CONT_LOCAL
;
4555 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4556 compl_cont_status
= CONT_ADDING
| CONT_N_ADDS
;
4558 compl_cont_status
= 0;
4561 compl_cont_status
&= CONT_LOCAL
;
4563 if (!(compl_cont_status
& CONT_ADDING
)) /* normal expansion */
4565 compl_cont_mode
= ctrl_x_mode
;
4566 if (ctrl_x_mode
!= 0) /* Remove LOCAL if ctrl_x_mode != 0 */
4567 compl_cont_status
= 0;
4568 compl_cont_status
|= CONT_N_ADDS
;
4569 compl_startpos
= curwin
->w_cursor
;
4570 startcol
= (int)curs_col
;
4574 /* Work out completion pattern and original text -- webb */
4575 if (ctrl_x_mode
== 0 || (ctrl_x_mode
& CTRL_X_WANT_IDENT
))
4577 if ((compl_cont_status
& CONT_SOL
)
4578 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4580 if (!(compl_cont_status
& CONT_ADDING
))
4582 while (--startcol
>= 0 && vim_isIDc(line
[startcol
]))
4584 compl_col
+= ++startcol
;
4585 compl_length
= curs_col
- startcol
;
4588 compl_pattern
= str_foldcase(line
+ compl_col
,
4589 compl_length
, NULL
, 0);
4591 compl_pattern
= vim_strnsave(line
+ compl_col
,
4593 if (compl_pattern
== NULL
)
4596 else if (compl_cont_status
& CONT_ADDING
)
4598 char_u
*prefix
= (char_u
*)"\\<";
4600 /* we need 3 extra chars, 1 for the NUL and
4601 * 2 >= strlen(prefix) -- Acevedo */
4602 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4604 if (compl_pattern
== NULL
)
4606 if (!vim_iswordp(line
+ compl_col
)
4610 vim_iswordp(mb_prevptr(line
, line
+ compl_col
))
4612 vim_iswordc(line
[compl_col
- 1])
4615 prefix
= (char_u
*)"";
4616 STRCPY((char *)compl_pattern
, prefix
);
4617 (void)quote_meta(compl_pattern
+ STRLEN(prefix
),
4618 line
+ compl_col
, compl_length
);
4620 else if (--startcol
< 0 ||
4622 !vim_iswordp(mb_prevptr(line
, line
+ startcol
+ 1))
4624 !vim_iswordc(line
[startcol
])
4628 /* Match any word of at least two chars */
4629 compl_pattern
= vim_strsave((char_u
*)"\\<\\k\\k");
4630 if (compl_pattern
== NULL
)
4632 compl_col
+= curs_col
;
4638 /* Search the point of change class of multibyte character
4639 * or not a word single byte character backward. */
4645 startcol
-= (*mb_head_off
)(line
, line
+ startcol
);
4646 base_class
= mb_get_class(line
+ startcol
);
4647 while (--startcol
>= 0)
4649 head_off
= (*mb_head_off
)(line
, line
+ startcol
);
4650 if (base_class
!= mb_get_class(line
+ startcol
4653 startcol
-= head_off
;
4658 while (--startcol
>= 0 && vim_iswordc(line
[startcol
]))
4660 compl_col
+= ++startcol
;
4661 compl_length
= (int)curs_col
- startcol
;
4662 if (compl_length
== 1)
4664 /* Only match word with at least two chars -- webb
4665 * there's no need to call quote_meta,
4666 * alloc(7) is enough -- Acevedo
4668 compl_pattern
= alloc(7);
4669 if (compl_pattern
== NULL
)
4671 STRCPY((char *)compl_pattern
, "\\<");
4672 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
, 1);
4673 STRCAT((char *)compl_pattern
, "\\k");
4677 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4679 if (compl_pattern
== NULL
)
4681 STRCPY((char *)compl_pattern
, "\\<");
4682 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
,
4687 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4689 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4690 compl_length
= (int)curs_col
- (int)compl_col
;
4691 if (compl_length
< 0) /* cursor in indent: empty pattern */
4694 compl_pattern
= str_foldcase(line
+ compl_col
, compl_length
,
4697 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4698 if (compl_pattern
== NULL
)
4701 else if (ctrl_x_mode
== CTRL_X_FILES
)
4703 while (--startcol
>= 0 && vim_isfilec(line
[startcol
]))
4705 compl_col
+= ++startcol
;
4706 compl_length
= (int)curs_col
- startcol
;
4707 compl_pattern
= addstar(line
+ compl_col
, compl_length
,
4709 if (compl_pattern
== NULL
)
4712 else if (ctrl_x_mode
== CTRL_X_CMDLINE
)
4714 compl_pattern
= vim_strnsave(line
, curs_col
);
4715 if (compl_pattern
== NULL
)
4717 set_cmd_context(&compl_xp
, compl_pattern
,
4718 (int)STRLEN(compl_pattern
), curs_col
);
4719 if (compl_xp
.xp_context
== EXPAND_UNSUCCESSFUL
4720 || compl_xp
.xp_context
== EXPAND_NOTHING
)
4721 /* No completion possible, use an empty pattern to get a
4722 * "pattern not found" message. */
4723 compl_col
= curs_col
;
4725 compl_col
= (int)(compl_xp
.xp_pattern
- compl_pattern
);
4726 compl_length
= curs_col
- compl_col
;
4728 else if (ctrl_x_mode
== CTRL_X_FUNCTION
|| ctrl_x_mode
== CTRL_X_OMNI
)
4730 #ifdef FEAT_COMPL_FUNC
4732 * Call user defined function 'completefunc' with "a:findstart"
4733 * set to 1 to obtain the length of text to use for completion.
4740 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
4742 funcname
= ctrl_x_mode
== CTRL_X_FUNCTION
4743 ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
4744 if (*funcname
== NUL
)
4746 EMSG2(_(e_notset
), ctrl_x_mode
== CTRL_X_FUNCTION
4747 ? "completefunc" : "omnifunc");
4751 args
[0] = (char_u
*)"1";
4753 pos
= curwin
->w_cursor
;
4754 col
= call_func_retnr(funcname
, 2, args
, FALSE
);
4755 curwin
->w_cursor
= pos
; /* restore the cursor position */
4760 if ((colnr_T
)compl_col
> curs_col
)
4761 compl_col
= curs_col
;
4763 /* Setup variables for completion. Need to obtain "line" again,
4764 * it may have become invalid. */
4765 line
= ml_get(curwin
->w_cursor
.lnum
);
4766 compl_length
= curs_col
- compl_col
;
4767 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4768 if (compl_pattern
== NULL
)
4772 else if (ctrl_x_mode
== CTRL_X_SPELL
)
4775 if (spell_bad_len
> 0)
4776 compl_col
= curs_col
- spell_bad_len
;
4778 compl_col
= spell_word_start(startcol
);
4779 if (compl_col
>= (colnr_T
)startcol
)
4782 compl_col
= curs_col
;
4786 spell_expand_check_cap(compl_col
);
4787 compl_length
= (int)curs_col
- compl_col
;
4789 /* Need to obtain "line" again, it may have become invalid. */
4790 line
= ml_get(curwin
->w_cursor
.lnum
);
4791 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4792 if (compl_pattern
== NULL
)
4798 EMSG2(_(e_intern2
), "ins_complete()");
4802 if (compl_cont_status
& CONT_ADDING
)
4804 edit_submode_pre
= (char_u
*)_(" Adding");
4805 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4807 /* Insert a new line, keep indentation but ignore 'comments' */
4808 #ifdef FEAT_COMMENTS
4809 char_u
*old
= curbuf
->b_p_com
;
4811 curbuf
->b_p_com
= (char_u
*)"";
4813 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4814 compl_startpos
.col
= compl_col
;
4816 #ifdef FEAT_COMMENTS
4817 curbuf
->b_p_com
= old
;
4820 compl_col
= curwin
->w_cursor
.col
;
4825 edit_submode_pre
= NULL
;
4826 compl_startpos
.col
= compl_col
;
4829 if (compl_cont_status
& CONT_LOCAL
)
4830 edit_submode
= (char_u
*)_(ctrl_x_msgs
[CTRL_X_LOCAL_MSG
]);
4832 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
4834 /* Always add completion for the original text. */
4835 vim_free(compl_orig_text
);
4836 compl_orig_text
= vim_strnsave(line
+ compl_col
, compl_length
);
4837 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
4838 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
4840 vim_free(compl_pattern
);
4841 compl_pattern
= NULL
;
4842 vim_free(compl_orig_text
);
4843 compl_orig_text
= NULL
;
4847 /* showmode might reset the internal line pointers, so it must
4848 * be called before line = ml_get(), or when this address is no
4849 * longer needed. -- Acevedo.
4851 edit_submode_extra
= (char_u
*)_("-- Searching...");
4852 edit_submode_highl
= HLF_COUNT
;
4854 edit_submode_extra
= NULL
;
4858 compl_shown_match
= compl_curr_match
;
4859 compl_shows_dir
= compl_direction
;
4862 * Find next match (and following matches).
4864 n
= ins_compl_next(TRUE
, ins_compl_key2count(c
), ins_compl_use_match(c
));
4866 /* may undisplay the popup menu */
4867 ins_compl_upd_pum();
4869 if (n
> 1) /* all matches have been found */
4871 compl_curr_match
= compl_shown_match
;
4872 compl_direction
= compl_shows_dir
;
4874 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4876 if (got_int
&& !global_busy
)
4882 /* we found no match if the list has only the "compl_orig_text"-entry */
4883 if (compl_first_match
== compl_first_match
->cp_next
)
4885 edit_submode_extra
= (compl_cont_status
& CONT_ADDING
)
4887 ? (char_u
*)_(e_hitend
) : (char_u
*)_(e_patnotf
);
4888 edit_submode_highl
= HLF_E
;
4889 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4890 * because we couldn't expand anything at first place, but if we used
4891 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4892 * (such as M in M'exico) if not tried already. -- Acevedo */
4893 if ( compl_length
> 1
4894 || (compl_cont_status
& CONT_ADDING
)
4895 || (ctrl_x_mode
!= 0
4896 && ctrl_x_mode
!= CTRL_X_PATH_PATTERNS
4897 && ctrl_x_mode
!= CTRL_X_PATH_DEFINES
))
4898 compl_cont_status
&= ~CONT_N_ADDS
;
4901 if (compl_curr_match
->cp_flags
& CONT_S_IPOS
)
4902 compl_cont_status
|= CONT_S_IPOS
;
4904 compl_cont_status
&= ~CONT_S_IPOS
;
4906 if (edit_submode_extra
== NULL
)
4908 if (compl_curr_match
->cp_flags
& ORIGINAL_TEXT
)
4910 edit_submode_extra
= (char_u
*)_("Back at original");
4911 edit_submode_highl
= HLF_W
;
4913 else if (compl_cont_status
& CONT_S_IPOS
)
4915 edit_submode_extra
= (char_u
*)_("Word from other line");
4916 edit_submode_highl
= HLF_COUNT
;
4918 else if (compl_curr_match
->cp_next
== compl_curr_match
->cp_prev
)
4920 edit_submode_extra
= (char_u
*)_("The only match");
4921 edit_submode_highl
= HLF_COUNT
;
4925 /* Update completion sequence number when needed. */
4926 if (compl_curr_match
->cp_number
== -1)
4931 if (compl_direction
== FORWARD
)
4933 /* search backwards for the first valid (!= -1) number.
4934 * This should normally succeed already at the first loop
4935 * cycle, so it's fast! */
4936 for (match
= compl_curr_match
->cp_prev
; match
!= NULL
4937 && match
!= compl_first_match
;
4938 match
= match
->cp_prev
)
4939 if (match
->cp_number
!= -1)
4941 number
= match
->cp_number
;
4945 /* go up and assign all numbers which are not assigned
4947 for (match
= match
->cp_next
;
4948 match
!= NULL
&& match
->cp_number
== -1;
4949 match
= match
->cp_next
)
4950 match
->cp_number
= ++number
;
4954 /* search forwards (upwards) for the first valid (!= -1)
4955 * number. This should normally succeed already at the
4956 * first loop cycle, so it's fast! */
4957 for (match
= compl_curr_match
->cp_next
; match
!= NULL
4958 && match
!= compl_first_match
;
4959 match
= match
->cp_next
)
4960 if (match
->cp_number
!= -1)
4962 number
= match
->cp_number
;
4966 /* go down and assign all numbers which are not
4968 for (match
= match
->cp_prev
; match
4969 && match
->cp_number
== -1;
4970 match
= match
->cp_prev
)
4971 match
->cp_number
= ++number
;
4975 /* The match should always have a sequence number now, this is
4976 * just a safety check. */
4977 if (compl_curr_match
->cp_number
!= -1)
4979 /* Space for 10 text chars. + 2x10-digit no.s = 31.
4980 * Translations may need more than twice that. */
4981 static char_u match_ref
[81];
4983 if (compl_matches
> 0)
4984 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
4985 _("match %d of %d"),
4986 compl_curr_match
->cp_number
, compl_matches
);
4988 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
4990 compl_curr_match
->cp_number
);
4991 edit_submode_extra
= match_ref
;
4992 edit_submode_highl
= HLF_R
;
4994 curs_columns(FALSE
);
4999 /* Show a message about what (completion) mode we're in. */
5001 if (edit_submode_extra
!= NULL
)
5004 msg_attr(edit_submode_extra
,
5005 edit_submode_highl
< HLF_COUNT
5006 ? hl_attr(edit_submode_highl
) : 0);
5009 msg_clr_cmdline(); /* necessary for "noshowmode" */
5011 /* Show the popup menu, unless we got interrupted. */
5012 if (!compl_interrupted
)
5014 /* RedrawingDisabled may be set when invoked through complete(). */
5015 n
= RedrawingDisabled
;
5016 RedrawingDisabled
= 0;
5017 ins_compl_show_pum();
5019 RedrawingDisabled
= n
;
5021 compl_was_interrupted
= compl_interrupted
;
5022 compl_interrupted
= FALSE
;
5028 * Looks in the first "len" chars. of "src" for search-metachars.
5029 * If dest is not NULL the chars. are copied there quoting (with
5030 * a backslash) the metachars, and dest would be NUL terminated.
5031 * Returns the length (needed) of dest
5034 quote_meta(dest
, src
, len
)
5041 for (m
= len
; --len
>= 0; src
++)
5048 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5049 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5052 if (!p_magic
) /* quote these only if magic is set */
5055 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5056 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5058 case '^': /* currently it's not needed. */
5068 /* Copy remaining bytes of a multibyte character. */
5073 mb_len
= (*mb_ptr2len
)(src
) - 1;
5074 if (mb_len
> 0 && len
>= mb_len
)
5075 for (i
= 0; i
< mb_len
; ++i
)
5090 #endif /* FEAT_INS_EXPAND */
5093 * Next character is interpreted literally.
5094 * A one, two or three digit decimal number is interpreted as its byte value.
5095 * If one or two digits are entered, the next character is given to vungetc().
5096 * For Unicode a character > 255 may be returned.
5115 * In GUI there is no point inserting the internal code for a special key.
5116 * It is more useful to insert the string "<KEY>" instead. This would
5117 * probably be useful in a text window too, but it would not be
5118 * vi-compatible (maybe there should be an option for it?) -- webb
5123 #ifdef USE_ON_FLY_SCROLL
5124 dont_scroll
= TRUE
; /* disallow scrolling here */
5126 ++no_mapping
; /* don't map the next key hits */
5133 while (nc
== K_IGNORE
|| nc
== K_VER_SCROLLBAR
5134 || nc
== K_HOR_SCROLLBAR
);
5135 #ifdef FEAT_CMDL_INFO
5136 if (!(State
& CMDLINE
)
5138 && MB_BYTE2LEN_CHECK(nc
) == 1
5143 if (nc
== 'x' || nc
== 'X')
5145 else if (nc
== 'o' || nc
== 'O')
5148 else if (nc
== 'u' || nc
== 'U')
5159 if (!vim_isxdigit(nc
))
5161 cc
= cc
* 16 + hex2nr(nc
);
5165 if (nc
< '0' || nc
> '7')
5167 cc
= cc
* 8 + nc
- '0';
5171 if (!VIM_ISDIGIT(nc
))
5173 cc
= cc
* 10 + nc
- '0';
5184 cc
= 255; /* limit range to 0-255 */
5187 if (hex
) /* hex: up to two chars */
5193 else if (unicode
) /* Unicode: up to four or eight chars */
5195 if ((unicode
== 'u' && i
>= 4) || (unicode
== 'U' && i
>= 8))
5199 else if (i
>= 3) /* decimal or octal: up to three chars */
5202 if (i
== 0) /* no number entered */
5204 if (nc
== K_ZERO
) /* NUL is stored as NL */
5216 if (cc
== 0) /* NUL is stored as NL */
5219 if (enc_dbcs
&& (cc
& 0xff) == 0)
5220 cc
= '?'; /* don't accept an illegal DBCS char, the NUL in the
5221 second byte will cause trouble! */
5231 got_int
= FALSE
; /* CTRL-C typed after CTRL-V is not an interrupt */
5236 * Insert character, taking care of special keys and mod_mask
5239 insert_special(c
, allow_modmask
, ctrlv
)
5242 int ctrlv
; /* c was typed after CTRL-V */
5248 * Special function key, translate into "<Key>". Up to the last '>' is
5249 * inserted with ins_str(), so as not to replace characters in replace
5251 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5252 * unless 'allow_modmask' is TRUE.
5255 /* Command-key never produces a normal key */
5256 if (mod_mask
& MOD_MASK_CMD
)
5257 allow_modmask
= TRUE
;
5259 if (IS_SPECIAL(c
) || (mod_mask
&& allow_modmask
))
5261 p
= get_special_key_name(c
, mod_mask
);
5262 len
= (int)STRLEN(p
);
5266 if (stop_arrow() == FAIL
)
5270 AppendToRedobuffLit(p
, -1);
5274 if (stop_arrow() == OK
)
5275 insertchar(c
, ctrlv
? INSCHAR_CTRLV
: 0, -1);
5279 * Special characters in this context are those that need processing other
5280 * than the simple insertion that can be performed here. This includes ESC
5281 * which terminates the insert, and CR/NL which need special processing to
5282 * open up a new line. This routine tries to optimize insertions performed by
5283 * the "redo", "undo" or "put" commands, so it needs to know when it should
5284 * stop and defer processing to the "normal" mechanism.
5285 * '0' and '^' are special, because they can be followed by CTRL-D.
5288 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5290 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5294 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5296 # define WHITECHAR(cc) vim_iswhite(cc)
5300 insertchar(c
, flags
, second_indent
)
5301 int c
; /* character to insert or NUL */
5302 int flags
; /* INSCHAR_FORMAT, etc. */
5303 int second_indent
; /* indent for second line if >= 0 */
5306 #ifdef FEAT_COMMENTS
5311 textwidth
= comp_textwidth(flags
& INSCHAR_FORMAT
);
5312 fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5315 * Try to break the line in two or more pieces when:
5316 * - Always do this if we have been called to do formatting only.
5317 * - Always do this when 'formatoptions' has the 'a' flag and the line
5318 * ends in white space.
5320 * - Don't do this if inserting a blank
5321 * - Don't do this if an existing character is being replaced, unless
5322 * we're in VREPLACE mode.
5323 * - Do this if the cursor is not on the line where insert started
5324 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5325 * before the insert.
5326 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5327 * before 'textwidth'
5330 && ((flags
& INSCHAR_FORMAT
)
5332 && !((State
& REPLACE_FLAG
)
5333 #ifdef FEAT_VREPLACE
5334 && !(State
& VREPLACE_FLAG
)
5336 && *ml_get_cursor() != NUL
)
5337 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
5338 || ((!has_format_option(FO_INS_LONG
)
5339 || Insstart_textlen
<= (colnr_T
)textwidth
)
5341 || Insstart_blank_vcol
<= (colnr_T
)textwidth
5344 /* Format with 'formatexpr' when it's set. Use internal formatting
5345 * when 'formatexpr' isn't set or it returns non-zero. */
5346 #if defined(FEAT_EVAL)
5347 int do_internal
= TRUE
;
5349 if (*curbuf
->b_p_fex
!= NUL
)
5351 do_internal
= (fex_format(curwin
->w_cursor
.lnum
, 1L, c
) != 0);
5352 /* It may be required to save for undo again, e.g. when setline()
5354 ins_need_undo
= TRUE
;
5358 internal_format(textwidth
, second_indent
, flags
, c
== NUL
);
5361 if (c
== NUL
) /* only formatting was wanted */
5364 #ifdef FEAT_COMMENTS
5365 /* Check whether this character should end a comment. */
5366 if (did_ai
&& (int)c
== end_comment_pending
)
5369 char_u lead_end
[COM_MAX_LEN
]; /* end-comment string */
5370 int middle_len
, end_len
;
5374 * Need to remove existing (middle) comment leader and insert end
5375 * comment leader. First, check what comment leader we can find.
5377 i
= get_leader_len(line
= ml_get_curline(), &p
, FALSE
);
5378 if (i
> 0 && vim_strchr(p
, COM_MIDDLE
) != NULL
) /* Just checking */
5380 /* Skip middle-comment string */
5381 while (*p
&& p
[-1] != ':') /* find end of middle flags */
5383 middle_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5384 /* Don't count trailing white space for middle_len */
5385 while (middle_len
> 0 && vim_iswhite(lead_end
[middle_len
- 1]))
5388 /* Find the end-comment string */
5389 while (*p
&& p
[-1] != ':') /* find end of end flags */
5391 end_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5393 /* Skip white space before the cursor */
5394 i
= curwin
->w_cursor
.col
;
5395 while (--i
>= 0 && vim_iswhite(line
[i
]))
5399 /* Skip to before the middle leader */
5402 /* Check some expected things before we go on */
5403 if (i
>= 0 && lead_end
[end_len
- 1] == end_comment_pending
)
5405 /* Backspace over all the stuff we want to replace */
5406 backspace_until_column(i
);
5409 * Insert the end-comment string, except for the last
5410 * character, which will get inserted as normal later.
5412 ins_bytes_len(lead_end
, end_len
- 1);
5416 end_comment_pending
= NUL
;
5420 #ifdef FEAT_SMARTINDENT
5423 can_si_back
= FALSE
;
5427 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5428 * This speeds up normal text input considerably.
5429 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5430 * need to re-indent at a ':', or any other character (but not what
5433 #ifdef USE_ON_FLY_SCROLL
5434 dont_scroll
= FALSE
; /* allow scrolling here */
5439 && (!has_mbyte
|| (*mb_char2len
)(c
) == 1)
5442 && !(State
& REPLACE_FLAG
)
5446 #ifdef FEAT_RIGHTLEFT
5451 #define INPUT_BUFLEN 100
5452 char_u buf
[INPUT_BUFLEN
+ 1];
5454 colnr_T virtcol
= 0;
5459 virtcol
= get_nolist_virtcol();
5461 * Stop the string when:
5462 * - no more chars available
5463 * - finding a special character (command key)
5465 * - running into the 'textwidth' boundary
5466 * - need to check for abbreviation: A non-word char after a word-char
5468 while ( (c
= vpeekc()) != NUL
5471 && (!has_mbyte
|| MB_BYTE2LEN_CHECK(c
) == 1)
5475 || (virtcol
+= byte2cells(buf
[i
- 1])) < (colnr_T
)textwidth
)
5476 && !(!no_abbr
&& !vim_iswordc(c
) && vim_iswordc(buf
[i
- 1])))
5478 #ifdef FEAT_RIGHTLEFT
5480 if (p_hkmap
&& KeyTyped
)
5481 c
= hkmap(c
); /* Hebrew mode mapping */
5483 if (p_fkmap
&& KeyTyped
)
5484 c
= fkmap(c
); /* Farsi mode mapping */
5492 #ifdef FEAT_DIGRAPHS
5493 do_digraph(-1); /* clear digraphs */
5494 do_digraph(buf
[i
-1]); /* may be the start of a digraph */
5498 if (flags
& INSCHAR_CTRLV
)
5506 AppendToRedobuffLit(buf
+ i
, -1);
5513 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
5515 char_u buf
[MB_MAXBYTES
+ 1];
5517 (*mb_char2bytes
)(c
, buf
);
5519 ins_char_bytes(buf
, cc
);
5520 AppendCharToRedobuff(c
);
5526 if (flags
& INSCHAR_CTRLV
)
5529 AppendCharToRedobuff(c
);
5535 * Format text at the current insert position.
5538 internal_format(textwidth
, second_indent
, flags
, format_only
)
5545 int save_char
= NUL
;
5546 int haveto_redraw
= FALSE
;
5547 int fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5549 int fo_multibyte
= has_format_option(FO_MBYTE_BREAK
);
5551 int fo_white_par
= has_format_option(FO_WHITE_PAR
);
5552 int first_line
= TRUE
;
5553 #ifdef FEAT_COMMENTS
5555 int no_leader
= FALSE
;
5556 int do_comments
= (flags
& INSCHAR_DO_COM
);
5560 * When 'ai' is off we don't want a space under the cursor to be
5561 * deleted. Replace it with an 'x' temporarily.
5563 if (!curbuf
->b_p_ai
)
5565 cc
= gchar_cursor();
5566 if (vim_iswhite(cc
))
5574 * Repeat breaking lines, until the current line is not too long.
5578 int startcol
; /* Cursor column at entry */
5579 int wantcol
; /* column at textwidth border */
5580 int foundcol
; /* column for start of spaces */
5581 int end_foundcol
= 0; /* column for start of word */
5584 #ifdef FEAT_VREPLACE
5586 char_u
*saved_text
= NULL
;
5590 virtcol
= get_nolist_virtcol();
5591 if (virtcol
< (colnr_T
)textwidth
)
5594 #ifdef FEAT_COMMENTS
5596 do_comments
= FALSE
;
5597 else if (!(flags
& INSCHAR_FORMAT
)
5598 && has_format_option(FO_WRAP_COMS
))
5601 /* Don't break until after the comment leader */
5603 leader_len
= get_leader_len(ml_get_curline(), NULL
, FALSE
);
5607 /* If the line doesn't start with a comment leader, then don't
5608 * start one in a following broken line. Avoids that a %word
5609 * moved to the start of the next line causes all following lines
5610 * to start with %. */
5611 if (leader_len
== 0)
5614 if (!(flags
& INSCHAR_FORMAT
)
5615 #ifdef FEAT_COMMENTS
5618 && !has_format_option(FO_WRAP
))
5624 if ((startcol
= curwin
->w_cursor
.col
) == 0)
5627 /* find column of textwidth border */
5628 coladvance((colnr_T
)textwidth
);
5629 wantcol
= curwin
->w_cursor
.col
;
5631 curwin
->w_cursor
.col
= startcol
- 1;
5633 /* Correct cursor for multi-byte character. */
5640 * Find position to break at.
5641 * Stop at first entered white when 'formatoptions' has 'v'
5643 while ((!fo_ins_blank
&& !has_format_option(FO_INS_VI
))
5644 || curwin
->w_cursor
.lnum
!= Insstart
.lnum
5645 || curwin
->w_cursor
.col
>= Insstart
.col
)
5647 cc
= gchar_cursor();
5650 /* remember position of blank just before text */
5651 end_foundcol
= curwin
->w_cursor
.col
;
5653 /* find start of sequence of blanks */
5654 while (curwin
->w_cursor
.col
> 0 && WHITECHAR(cc
))
5657 cc
= gchar_cursor();
5659 if (curwin
->w_cursor
.col
== 0 && WHITECHAR(cc
))
5660 break; /* only spaces in front of text */
5661 #ifdef FEAT_COMMENTS
5662 /* Don't break until after the comment leader */
5663 if (curwin
->w_cursor
.col
< leader_len
)
5666 if (has_format_option(FO_ONE_LETTER
))
5668 /* do not break after one-letter words */
5669 if (curwin
->w_cursor
.col
== 0)
5670 break; /* one-letter word at begin */
5672 col
= curwin
->w_cursor
.col
;
5674 cc
= gchar_cursor();
5677 continue; /* one-letter, continue */
5678 curwin
->w_cursor
.col
= col
;
5682 foundcol
= curwin
->w_cursor
.col
5683 + (*mb_ptr2len
)(ml_get_cursor());
5686 foundcol
= curwin
->w_cursor
.col
+ 1;
5687 if (curwin
->w_cursor
.col
< (colnr_T
)wantcol
)
5691 else if (cc
>= 0x100 && fo_multibyte
5692 && curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5694 /* Break after or before a multi-byte character. */
5695 foundcol
= curwin
->w_cursor
.col
;
5696 if (curwin
->w_cursor
.col
< (colnr_T
)wantcol
)
5697 foundcol
+= (*mb_char2len
)(cc
);
5698 end_foundcol
= foundcol
;
5702 if (curwin
->w_cursor
.col
== 0)
5707 if (foundcol
== 0) /* no spaces, cannot break line */
5709 curwin
->w_cursor
.col
= startcol
;
5713 /* Going to break the line, remove any "$" now. */
5717 * Offset between cursor position and line break is used by replace
5718 * stack functions. VREPLACE does not use this, and backspaces
5719 * over the text instead.
5721 #ifdef FEAT_VREPLACE
5722 if (State
& VREPLACE_FLAG
)
5723 orig_col
= startcol
; /* Will start backspacing from here */
5726 replace_offset
= startcol
- end_foundcol
- 1;
5729 * adjust startcol for spaces that will be deleted and
5730 * characters that will remain on top line
5732 curwin
->w_cursor
.col
= foundcol
;
5733 while (cc
= gchar_cursor(), WHITECHAR(cc
))
5735 startcol
-= curwin
->w_cursor
.col
;
5739 #ifdef FEAT_VREPLACE
5740 if (State
& VREPLACE_FLAG
)
5743 * In VREPLACE mode, we will backspace over the text to be
5744 * wrapped, so save a copy now to put on the next line.
5746 saved_text
= vim_strsave(ml_get_cursor());
5747 curwin
->w_cursor
.col
= orig_col
;
5748 if (saved_text
== NULL
)
5749 break; /* Can't do it, out of memory */
5750 saved_text
[startcol
] = NUL
;
5752 /* Backspace over characters that will move to the next line */
5754 backspace_until_column(foundcol
);
5759 /* put cursor after pos. to break line */
5761 curwin
->w_cursor
.col
= foundcol
;
5765 * Split the line just before the margin.
5766 * Only insert/delete lines, but don't really redraw the window.
5768 open_line(FORWARD
, OPENLINE_DELSPACES
+ OPENLINE_MARKFIX
5769 + (fo_white_par
? OPENLINE_KEEPTRAIL
: 0)
5770 #ifdef FEAT_COMMENTS
5771 + (do_comments
? OPENLINE_DO_COM
: 0)
5779 if (second_indent
< 0 && has_format_option(FO_Q_NUMBER
))
5780 second_indent
= get_number_indent(curwin
->w_cursor
.lnum
-1);
5781 if (second_indent
>= 0)
5783 #ifdef FEAT_VREPLACE
5784 if (State
& VREPLACE_FLAG
)
5785 change_indent(INDENT_SET
, second_indent
, FALSE
, NUL
);
5788 (void)set_indent(second_indent
, SIN_CHANGED
);
5793 #ifdef FEAT_VREPLACE
5794 if (State
& VREPLACE_FLAG
)
5797 * In VREPLACE mode we have backspaced over the text to be
5798 * moved, now we re-insert it into the new line.
5800 ins_bytes(saved_text
);
5801 vim_free(saved_text
);
5807 * Check if cursor is not past the NUL off the line, cindent
5808 * may have added or removed indent.
5810 curwin
->w_cursor
.col
+= startcol
;
5811 len
= (colnr_T
)STRLEN(ml_get_curline());
5812 if (curwin
->w_cursor
.col
> len
)
5813 curwin
->w_cursor
.col
= len
;
5816 haveto_redraw
= TRUE
;
5820 /* moved the cursor, don't autoindent or cindent now */
5822 #ifdef FEAT_SMARTINDENT
5825 can_si_back
= FALSE
;
5830 if (save_char
!= NUL
) /* put back space after cursor */
5831 pchar_cursor(save_char
);
5833 if (!format_only
&& haveto_redraw
)
5836 redraw_curbuf_later(VALID
);
5841 * Called after inserting or deleting text: When 'formatoptions' includes the
5842 * 'a' flag format from the current line until the end of the paragraph.
5843 * Keep the cursor at the same position relative to the text.
5844 * The caller must have saved the cursor line for undo, following ones will be
5848 auto_format(trailblank
, prev_line
)
5849 int trailblank
; /* when TRUE also format with trailing blank */
5850 int prev_line
; /* may start in previous line */
5859 if (!has_format_option(FO_AUTO
))
5862 pos
= curwin
->w_cursor
;
5863 old
= ml_get_curline();
5865 /* may remove added space */
5866 check_auto_format(FALSE
);
5868 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5869 * user might insert normal text next. Also skip formatting when "1" is
5870 * in 'formatoptions' and there is a single character before the cursor.
5871 * Otherwise the line would be broken and when typing another non-white
5872 * next they are not joined back together. */
5873 wasatend
= (pos
.col
== STRLEN(old
));
5874 if (*old
!= NUL
&& !trailblank
&& wasatend
)
5877 cc
= gchar_cursor();
5878 if (!WHITECHAR(cc
) && curwin
->w_cursor
.col
> 0
5879 && has_format_option(FO_ONE_LETTER
))
5881 cc
= gchar_cursor();
5884 curwin
->w_cursor
= pos
;
5887 curwin
->w_cursor
= pos
;
5890 #ifdef FEAT_COMMENTS
5891 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5893 if (has_format_option(FO_WRAP_COMS
) && !has_format_option(FO_WRAP
)
5894 && get_leader_len(old
, NULL
, FALSE
) == 0)
5899 * May start formatting in a previous line, so that after "x" a word is
5900 * moved to the previous line if it fits there now. Only when this is not
5901 * the start of a paragraph.
5903 if (prev_line
&& !paragraph_start(curwin
->w_cursor
.lnum
))
5905 --curwin
->w_cursor
.lnum
;
5906 if (u_save_cursor() == FAIL
)
5911 * Do the formatting and restore the cursor position. "saved_cursor" will
5912 * be adjusted for the text formatting.
5915 format_lines((linenr_T
)-1);
5916 curwin
->w_cursor
= saved_cursor
;
5917 saved_cursor
.lnum
= 0;
5919 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
5921 /* "cannot happen" */
5922 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
5923 coladvance((colnr_T
)MAXCOL
);
5928 /* Insert mode: If the cursor is now after the end of the line while it
5929 * previously wasn't, the line was broken. Because of the rule above we
5930 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5932 if (!wasatend
&& has_format_option(FO_WHITE_PAR
))
5934 new = ml_get_curline();
5935 len
= (colnr_T
)STRLEN(new);
5936 if (curwin
->w_cursor
.col
== len
)
5938 pnew
= vim_strnsave(new, len
+ 2);
5940 pnew
[len
+ 1] = NUL
;
5941 ml_replace(curwin
->w_cursor
.lnum
, pnew
, FALSE
);
5942 /* remove the space later */
5943 did_add_space
= TRUE
;
5946 /* may remove added space */
5947 check_auto_format(FALSE
);
5954 * When an extra space was added to continue a paragraph for auto-formatting,
5955 * delete it now. The space must be under the cursor, just after the insert
5959 check_auto_format(end_insert
)
5960 int end_insert
; /* TRUE when ending Insert mode */
5967 cc
= gchar_cursor();
5969 /* Somehow the space was removed already. */
5970 did_add_space
= FALSE
;
5981 /* The space is no longer at the end of the line, delete it. */
5983 did_add_space
= FALSE
;
5990 * Find out textwidth to be used for formatting:
5991 * if 'textwidth' option is set, use it
5992 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5993 * if invalid value, use 0.
5994 * Set default to window width (maximum 79) for "gq" operator.
5998 int ff
; /* force formatting (for "gq" command) */
6002 textwidth
= curbuf
->b_p_tw
;
6003 if (textwidth
== 0 && curbuf
->b_p_wm
)
6005 /* The width is the window width minus 'wrapmargin' minus all the
6006 * things that add to the margin. */
6007 textwidth
= W_WIDTH(curwin
) - curbuf
->b_p_wm
;
6009 if (cmdwin_type
!= 0)
6013 textwidth
-= curwin
->w_p_fdc
;
6016 if (curwin
->w_buffer
->b_signlist
!= NULL
6017 # ifdef FEAT_NETBEANS_INTG
6028 if (ff
&& textwidth
== 0)
6030 textwidth
= W_WIDTH(curwin
) - 1;
6038 * Put a character in the redo buffer, for when just after a CTRL-V.
6046 /* Only digits need special treatment. Translate them into a string of
6050 sprintf((char *)buf
, "%03d", c
);
6051 AppendToRedobuff(buf
);
6054 AppendCharToRedobuff(c
);
6058 * start_arrow() is called when an arrow key is used in insert mode.
6059 * For undo/redo it resembles hitting the <ESC> key.
6062 start_arrow(end_insert_pos
)
6063 pos_T
*end_insert_pos
; /* can be NULL */
6065 if (!arrow_used
) /* something has been inserted */
6067 AppendToRedobuff(ESC_STR
);
6068 stop_insert(end_insert_pos
, FALSE
);
6069 arrow_used
= TRUE
; /* this means we stopped the current insert */
6072 check_spell_redraw();
6078 * If we skipped highlighting word at cursor, do it now.
6079 * It may be skipped again, thus reset spell_redraw_lnum first.
6082 check_spell_redraw()
6084 if (spell_redraw_lnum
!= 0)
6086 linenr_T lnum
= spell_redraw_lnum
;
6088 spell_redraw_lnum
= 0;
6089 redrawWinline(lnum
, FALSE
);
6094 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6095 * spelled word, if there is one.
6098 spell_back_to_badword()
6100 pos_T tpos
= curwin
->w_cursor
;
6102 spell_bad_len
= spell_move_to(curwin
, BACKWARD
, TRUE
, TRUE
, NULL
);
6103 if (curwin
->w_cursor
.col
!= tpos
.col
)
6109 * stop_arrow() is called before a change is made in insert mode.
6110 * If an arrow key has been used, start a new insertion.
6111 * Returns FAIL if undo is impossible, shouldn't insert then.
6118 if (u_save_cursor() == OK
)
6121 ins_need_undo
= FALSE
;
6123 Insstart
= curwin
->w_cursor
; /* new insertion starts here */
6124 Insstart_textlen
= linetabsize(ml_get_curline());
6126 #ifdef FEAT_VREPLACE
6127 if (State
& VREPLACE_FLAG
)
6129 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
6130 vr_lines_changed
= 1;
6134 AppendToRedobuff((char_u
*)"1i"); /* pretend we start an insertion */
6135 new_insert_skip
= 2;
6137 else if (ins_need_undo
)
6139 if (u_save_cursor() == OK
)
6140 ins_need_undo
= FALSE
;
6144 /* Always open fold at the cursor line when inserting something. */
6148 return (arrow_used
|| ins_need_undo
? FAIL
: OK
);
6152 * Do a few things to stop inserting.
6153 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6154 * to another window/buffer.
6157 stop_insert(end_insert_pos
, esc
)
6158 pos_T
*end_insert_pos
;
6159 int esc
; /* called by ins_esc() */
6165 replace_flush(); /* abandon replace stack */
6168 * Save the inserted text for later redo with ^@ and CTRL-A.
6169 * Don't do it when "restart_edit" was set and nothing was inserted,
6170 * otherwise CTRL-O w and then <Left> will clear "last_insert".
6172 ptr
= get_inserted();
6173 if (did_restart_edit
== 0 || (ptr
!= NULL
6174 && (int)STRLEN(ptr
) > new_insert_skip
))
6176 vim_free(last_insert
);
6178 last_insert_skip
= new_insert_skip
;
6183 if (!arrow_used
&& end_insert_pos
!= NULL
)
6185 /* Auto-format now. It may seem strange to do this when stopping an
6186 * insertion (or moving the cursor), but it's required when appending
6187 * a line and having it end in a space. But only do it when something
6188 * was actually inserted, otherwise undo won't work. */
6189 if (!ins_need_undo
&& has_format_option(FO_AUTO
))
6191 pos_T tpos
= curwin
->w_cursor
;
6193 /* When the cursor is at the end of the line after a space the
6194 * formatting will move it to the following word. Avoid that by
6195 * moving the cursor onto the space. */
6197 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
)
6200 cc
= gchar_cursor();
6201 if (!vim_iswhite(cc
))
6202 curwin
->w_cursor
= tpos
;
6205 auto_format(TRUE
, FALSE
);
6207 if (vim_iswhite(cc
))
6209 if (gchar_cursor() != NUL
)
6211 #ifdef FEAT_VIRTUALEDIT
6212 /* If the cursor is still at the same character, also keep
6214 if (gchar_cursor() == NUL
6215 && curwin
->w_cursor
.lnum
== tpos
.lnum
6216 && curwin
->w_cursor
.col
== tpos
.col
)
6217 curwin
->w_cursor
.coladd
= tpos
.coladd
;
6222 /* If a space was inserted for auto-formatting, remove it now. */
6223 check_auto_format(TRUE
);
6225 /* If we just did an auto-indent, remove the white space from the end
6226 * of the line, and put the cursor back.
6227 * Do this when ESC was used or moving the cursor up/down. */
6228 if (did_ai
&& (esc
|| (vim_strchr(p_cpo
, CPO_INDENT
) == NULL
6229 && curwin
->w_cursor
.lnum
!= end_insert_pos
->lnum
)))
6231 pos_T tpos
= curwin
->w_cursor
;
6233 curwin
->w_cursor
= *end_insert_pos
;
6236 if (gchar_cursor() == NUL
&& curwin
->w_cursor
.col
> 0)
6237 --curwin
->w_cursor
.col
;
6238 cc
= gchar_cursor();
6239 if (!vim_iswhite(cc
))
6241 (void)del_char(TRUE
);
6243 if (curwin
->w_cursor
.lnum
!= tpos
.lnum
)
6244 curwin
->w_cursor
= tpos
;
6246 ++curwin
->w_cursor
.col
; /* put cursor back on the NUL */
6249 /* <C-S-Right> may have started Visual mode, adjust the position for
6250 * deleted characters. */
6251 if (VIsual_active
&& VIsual
.lnum
== curwin
->w_cursor
.lnum
)
6253 cc
= (int)STRLEN(ml_get_curline());
6254 if (VIsual
.col
> (colnr_T
)cc
)
6257 # ifdef FEAT_VIRTUALEDIT
6266 #ifdef FEAT_SMARTINDENT
6269 can_si_back
= FALSE
;
6272 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6273 * now in a different buffer. */
6274 if (end_insert_pos
!= NULL
)
6276 curbuf
->b_op_start
= Insstart
;
6277 curbuf
->b_op_end
= *end_insert_pos
;
6282 * Set the last inserted text to a single character.
6283 * Used for the replace command.
6291 vim_free(last_insert
);
6293 last_insert
= alloc(MB_MAXBYTES
* 3 + 5);
6295 last_insert
= alloc(6);
6297 if (last_insert
!= NULL
)
6300 /* Use the CTRL-V only when entering a special char */
6301 if (c
< ' ' || c
== DEL
)
6303 s
= add_char2buf(c
, s
);
6306 last_insert_skip
= 0;
6310 #if defined(EXITFREE) || defined(PROTO)
6314 vim_free(last_insert
);
6316 vim_free(compl_orig_text
);
6317 compl_orig_text
= NULL
;
6322 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6323 * and CSI. Handle multi-byte characters.
6324 * Returns a pointer to after the added bytes.
6332 char_u temp
[MB_MAXBYTES
];
6336 len
= (*mb_char2bytes
)(c
, temp
);
6337 for (i
= 0; i
< len
; ++i
)
6341 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6365 * move cursor to start of line
6366 * if flags & BL_WHITE move to first non-white
6367 * if flags & BL_SOL move to first non-white if startofline is set,
6368 * otherwise keep "curswant" column
6369 * if flags & BL_FIX don't leave the cursor on a NUL.
6375 if ((flags
& BL_SOL
) && !p_sol
)
6376 coladvance(curwin
->w_curswant
);
6379 curwin
->w_cursor
.col
= 0;
6380 #ifdef FEAT_VIRTUALEDIT
6381 curwin
->w_cursor
.coladd
= 0;
6384 if (flags
& (BL_WHITE
| BL_SOL
))
6388 for (ptr
= ml_get_curline(); vim_iswhite(*ptr
)
6389 && !((flags
& BL_FIX
) && ptr
[1] == NUL
); ++ptr
)
6390 ++curwin
->w_cursor
.col
;
6392 curwin
->w_set_curswant
= TRUE
;
6397 * oneright oneleft cursor_down cursor_up
6399 * Move one char {right,left,down,up}.
6400 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
6401 * Return OK when successful, FAIL when we hit a line of file boundary.
6410 #ifdef FEAT_VIRTUALEDIT
6411 if (virtual_active())
6413 pos_T prevpos
= curwin
->w_cursor
;
6415 /* Adjust for multi-wide char (excluding TAB) */
6416 ptr
= ml_get_cursor();
6417 coladvance(getviscol() + ((*ptr
!= TAB
&& vim_isprintc(
6424 ? ptr2cells(ptr
) : 1));
6425 curwin
->w_set_curswant
= TRUE
;
6426 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6427 return (prevpos
.col
!= curwin
->w_cursor
.col
6428 || prevpos
.coladd
!= curwin
->w_cursor
.coladd
) ? OK
: FAIL
;
6432 ptr
= ml_get_cursor();
6434 return FAIL
; /* already at the very end */
6438 l
= (*mb_ptr2len
)(ptr
);
6443 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6444 * contains "onemore". */
6446 #ifdef FEAT_VIRTUALEDIT
6447 && (ve_flags
& VE_ONEMORE
) == 0
6451 curwin
->w_cursor
.col
+= l
;
6453 curwin
->w_set_curswant
= TRUE
;
6460 #ifdef FEAT_VIRTUALEDIT
6461 if (virtual_active())
6464 int v
= getviscol();
6469 # ifdef FEAT_LINEBREAK
6470 /* We might get stuck on 'showbreak', skip over it. */
6474 coladvance(v
- width
);
6475 /* getviscol() is slow, skip it when 'showbreak' is empty and
6476 * there are no multi-byte characters */
6481 ) || getviscol() < v
)
6489 if (curwin
->w_cursor
.coladd
== 1)
6493 /* Adjust for multi-wide char (not a TAB) */
6494 ptr
= ml_get_cursor();
6495 if (*ptr
!= TAB
&& vim_isprintc(
6501 ) && ptr2cells(ptr
) > 1)
6502 curwin
->w_cursor
.coladd
= 0;
6505 curwin
->w_set_curswant
= TRUE
;
6510 if (curwin
->w_cursor
.col
== 0)
6513 curwin
->w_set_curswant
= TRUE
;
6514 --curwin
->w_cursor
.col
;
6517 /* if the character on the left of the current cursor is a multi-byte
6518 * character, move to its first byte */
6526 cursor_up(n
, upd_topline
)
6528 int upd_topline
; /* When TRUE: update topline */
6534 lnum
= curwin
->w_cursor
.lnum
;
6535 /* This fails if the cursor is already in the first line or the count
6536 * is larger than the line number and '-' is in 'cpoptions' */
6537 if (lnum
<= 1 || (n
>= lnum
&& vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6543 if (hasAnyFolding(curwin
))
6546 * Count each sequence of folded lines as one logical line.
6548 /* go to the the start of the current fold */
6549 (void)hasFolding(lnum
, &lnum
, NULL
);
6553 /* move up one line */
6557 /* If we entered a fold, move to the beginning, unless in
6558 * Insert mode or when 'foldopen' contains "all": it will open
6560 if (n
> 0 || !((State
& INSERT
) || (fdo_flags
& FDO_ALL
)))
6561 (void)hasFolding(lnum
, &lnum
, NULL
);
6569 curwin
->w_cursor
.lnum
= lnum
;
6572 /* try to advance to the column we want to be at */
6573 coladvance(curwin
->w_curswant
);
6576 update_topline(); /* make sure curwin->w_topline is valid */
6582 * Cursor down a number of logical lines.
6585 cursor_down(n
, upd_topline
)
6587 int upd_topline
; /* When TRUE: update topline */
6593 lnum
= curwin
->w_cursor
.lnum
;
6595 /* Move to last line of fold, will fail if it's the end-of-file. */
6596 (void)hasFolding(lnum
, NULL
, &lnum
);
6598 /* This fails if the cursor is already in the last line or would move
6599 * beyound the last line and '-' is in 'cpoptions' */
6600 if (lnum
>= curbuf
->b_ml
.ml_line_count
6601 || (lnum
+ n
> curbuf
->b_ml
.ml_line_count
6602 && vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6604 if (lnum
+ n
>= curbuf
->b_ml
.ml_line_count
)
6605 lnum
= curbuf
->b_ml
.ml_line_count
;
6608 if (hasAnyFolding(curwin
))
6612 /* count each sequence of folded lines as one logical line */
6615 if (hasFolding(lnum
, NULL
, &last
))
6619 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
6622 if (lnum
> curbuf
->b_ml
.ml_line_count
)
6623 lnum
= curbuf
->b_ml
.ml_line_count
;
6628 curwin
->w_cursor
.lnum
= lnum
;
6631 /* try to advance to the column we want to be at */
6632 coladvance(curwin
->w_curswant
);
6635 update_topline(); /* make sure curwin->w_topline is valid */
6641 * Stuff the last inserted text in the read buffer.
6642 * Last_insert actually is a copy of the redo buffer, so we
6643 * first have to remove the command.
6646 stuff_inserted(c
, count
, no_esc
)
6647 int c
; /* Command character to be inserted */
6648 long count
; /* Repeat this many times */
6649 int no_esc
; /* Don't add an ESC at the end */
6656 ptr
= get_last_insert();
6659 EMSG(_(e_noinstext
));
6663 /* may want to stuff the command character, to start Insert mode */
6665 stuffcharReadbuff(c
);
6666 if ((esc_ptr
= (char_u
*)vim_strrchr(ptr
, ESC
)) != NULL
)
6667 *esc_ptr
= NUL
; /* remove the ESC */
6669 /* when the last char is either "0" or "^" it will be quoted if no ESC
6670 * comes after it OR if it will inserted more than once and "ptr"
6671 * starts with ^D. -- Acevedo
6673 last_ptr
= (esc_ptr
? esc_ptr
: ptr
+ STRLEN(ptr
)) - 1;
6674 if (last_ptr
>= ptr
&& (*last_ptr
== '0' || *last_ptr
== '^')
6675 && (no_esc
|| (*ptr
== Ctrl_D
&& count
> 1)))
6684 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6686 stuffReadbuff((char_u
*)(last
== '0'
6687 ? IF_EB("\026\060\064\070", CTRL_V_STR
"xf0")
6688 : IF_EB("\026^", CTRL_V_STR
"^")));
6690 while (--count
> 0);
6695 if (esc_ptr
!= NULL
)
6696 *esc_ptr
= ESC
; /* put the ESC back */
6698 /* may want to stuff a trailing ESC, to get out of Insert mode */
6700 stuffcharReadbuff(ESC
);
6708 if (last_insert
== NULL
)
6710 return last_insert
+ last_insert_skip
;
6714 * Get last inserted string, and remove trailing <Esc>.
6715 * Returns pointer to allocated memory (must be freed) or NULL.
6718 get_last_insert_save()
6723 if (last_insert
== NULL
)
6725 s
= vim_strsave(last_insert
+ last_insert_skip
);
6728 len
= (int)STRLEN(s
);
6729 if (len
> 0 && s
[len
- 1] == ESC
) /* remove trailing ESC */
6736 * Check the word in front of the cursor for an abbreviation.
6737 * Called when the non-id character "c" has been entered.
6738 * When an abbreviation is recognized it is removed from the text and
6739 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6745 /* Don't check for abbreviation in paste mode, when disabled and just
6746 * after moving around with cursor keys. */
6747 if (p_paste
|| no_abbr
|| arrow_used
)
6750 return check_abbr(c
, ml_get_curline(), curwin
->w_cursor
.col
,
6751 curwin
->w_cursor
.lnum
== Insstart
.lnum
? Insstart
.col
: 0);
6755 * replace-stack functions
6757 * When replacing characters, the replaced characters are remembered for each
6758 * new character. This is used to re-insert the old text when backspacing.
6760 * There is a NUL headed list of characters for each character that is
6761 * currently in the file after the insertion point. When BS is used, one NUL
6762 * headed list is put back for the deleted character.
6764 * For a newline, there are two NUL headed lists. One contains the characters
6765 * that the NL replaced. The extra one stores the characters after the cursor
6766 * that were deleted (always white space).
6768 * Replace_offset is normally 0, in which case replace_push will add a new
6769 * character at the end of the stack. If replace_offset is not 0, that many
6770 * characters will be left on the stack above the newly inserted character.
6773 static char_u
*replace_stack
= NULL
;
6774 static long replace_stack_nr
= 0; /* next entry in replace stack */
6775 static long replace_stack_len
= 0; /* max. number of entries */
6779 int c
; /* character that is replaced (NUL is none) */
6783 if (replace_stack_nr
< replace_offset
) /* nothing to do */
6785 if (replace_stack_len
<= replace_stack_nr
)
6787 replace_stack_len
+= 50;
6788 p
= lalloc(sizeof(char_u
) * replace_stack_len
, TRUE
);
6789 if (p
== NULL
) /* out of memory */
6791 replace_stack_len
-= 50;
6794 if (replace_stack
!= NULL
)
6796 mch_memmove(p
, replace_stack
,
6797 (size_t)(replace_stack_nr
* sizeof(char_u
)));
6798 vim_free(replace_stack
);
6802 p
= replace_stack
+ replace_stack_nr
- replace_offset
;
6804 mch_memmove(p
+ 1, p
, (size_t)(replace_offset
* sizeof(char_u
)));
6811 * call replace_push(c) with replace_offset set to the first NUL.
6819 p
= replace_stack
+ replace_stack_nr
;
6820 for (replace_offset
= 1; replace_offset
< replace_stack_nr
;
6830 * Pop one item from the replace stack.
6831 * return -1 if stack empty
6832 * return replaced character or NUL otherwise
6837 if (replace_stack_nr
== 0)
6839 return (int)replace_stack
[--replace_stack_nr
];
6843 * Join the top two items on the replace stack. This removes to "off"'th NUL
6848 int off
; /* offset for which NUL to remove */
6852 for (i
= replace_stack_nr
; --i
>= 0; )
6853 if (replace_stack
[i
] == NUL
&& off
-- <= 0)
6856 mch_memmove(replace_stack
+ i
, replace_stack
+ i
+ 1,
6857 (size_t)(replace_stack_nr
- i
));
6863 * Pop bytes from the replace stack until a NUL is found, and insert them
6864 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6870 int oldState
= State
;
6872 State
= NORMAL
; /* don't want REPLACE here */
6873 while ((cc
= replace_pop()) > 0)
6876 mb_replace_pop_ins(cc
);
6887 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6888 * indicates a multi-byte char, pop the other bytes too.
6891 mb_replace_pop_ins(cc
)
6895 char_u buf
[MB_MAXBYTES
];
6899 if (has_mbyte
&& (n
= MB_BYTE2LEN(cc
)) > 1)
6902 for (i
= 1; i
< n
; ++i
)
6903 buf
[i
] = replace_pop();
6904 ins_bytes_len(buf
, n
);
6910 /* Handle composing chars. */
6914 if (c
== -1) /* stack empty */
6916 if ((n
= MB_BYTE2LEN(c
)) == 1)
6918 /* Not a multi-byte char, put it back. */
6925 for (i
= 1; i
< n
; ++i
)
6926 buf
[i
] = replace_pop();
6927 if (utf_iscomposing(utf_ptr2char(buf
)))
6928 ins_bytes_len(buf
, n
);
6931 /* Not a composing char, put it back. */
6932 for (i
= n
- 1; i
>= 0; --i
)
6933 replace_push(buf
[i
]);
6942 * make the replace stack empty
6943 * (called when exiting replace mode)
6948 vim_free(replace_stack
);
6949 replace_stack
= NULL
;
6950 replace_stack_len
= 0;
6951 replace_stack_nr
= 0;
6955 * Handle doing a BS for one character.
6956 * cc < 0: replace stack empty, just move cursor
6957 * cc == 0: character was inserted, delete it
6958 * cc > 0: character was replaced, put cc (first byte of original char) back
6959 * and check for more characters to be put back
6965 #ifdef FEAT_VREPLACE
6978 #ifdef FEAT_VREPLACE
6979 if (State
& VREPLACE_FLAG
)
6981 /* Get the number of screen cells used by the character we are
6982 * going to delete. */
6983 getvcol(curwin
, &curwin
->w_cursor
, NULL
, &start_vcol
, NULL
);
6984 orig_vcols
= chartabsize(ml_get_cursor(), start_vcol
);
6991 # ifdef FEAT_VREPLACE
6992 if (State
& VREPLACE_FLAG
)
6993 orig_len
= (int)STRLEN(ml_get_cursor());
7001 #ifdef FEAT_VREPLACE
7002 if (State
& VREPLACE_FLAG
)
7003 orig_len
= (int)STRLEN(ml_get_cursor()) - 1;
7008 #ifdef FEAT_VREPLACE
7009 if (State
& VREPLACE_FLAG
)
7011 /* Get the number of screen cells used by the inserted characters */
7012 p
= ml_get_cursor();
7013 ins_len
= (int)STRLEN(p
) - orig_len
;
7015 for (i
= 0; i
< ins_len
; ++i
)
7017 vcol
+= chartabsize(p
+ i
, vcol
);
7019 i
+= (*mb_ptr2len
)(p
) - 1;
7024 /* Delete spaces that were inserted after the cursor to keep the
7026 curwin
->w_cursor
.col
+= ins_len
;
7027 while (vcol
> orig_vcols
&& gchar_cursor() == ' ')
7032 curwin
->w_cursor
.col
-= ins_len
;
7036 /* mark the buffer as changed and prepare for displaying */
7037 changed_bytes(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
7040 (void)del_char(FALSE
);
7045 * Return TRUE if C-indenting is on.
7050 return (!p_paste
&& (curbuf
->b_p_cin
7052 || *curbuf
->b_p_inde
!= NUL
7058 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7060 * Re-indent the current line, based on the current contents of it and the
7061 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7062 * confused what all the part that handles Control-T is doing that I'm not.
7063 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7067 fixthisline(get_the_indent
)
7068 int (*get_the_indent
) __ARGS((void));
7070 change_indent(INDENT_SET
, get_the_indent(), FALSE
, 0);
7071 if (linewhite(curwin
->w_cursor
.lnum
))
7072 did_ai
= TRUE
; /* delete the indent if the line stays empty */
7081 if (curbuf
->b_p_lisp
&& curbuf
->b_p_ai
)
7082 fixthisline(get_lisp_indent
);
7084 # if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7087 # ifdef FEAT_CINDENT
7097 * return TRUE if 'cinkeys' contains the key "keytyped",
7098 * when == '*': Only if key is preceded with '*' (indent before insert)
7099 * when == '!': Only if key is prededed with '!' (don't insert)
7100 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7102 * "keytyped" can have a few special values:
7105 * KEY_COMPLETE just finished completion.
7107 * If line_is_empty is TRUE accept keys with '0' before them.
7110 in_cinkeys(keytyped
, when
, line_is_empty
)
7124 if (*curbuf
->b_p_inde
!= NUL
)
7125 look
= curbuf
->b_p_indk
; /* 'indentexpr' set: use 'indentkeys' */
7128 look
= curbuf
->b_p_cink
; /* 'indentexpr' empty: use 'cinkeys' */
7132 * Find out if we want to try a match with this key, depending on
7133 * 'when' and a '*' or '!' before the key.
7137 case '*': try_match
= (*look
== '*'); break;
7138 case '!': try_match
= (*look
== '!'); break;
7139 default: try_match
= (*look
!= '*'); break;
7141 if (*look
== '*' || *look
== '!')
7145 * If there is a '0', only accept a match if the line is empty.
7146 * But may still match when typing last char of a word.
7150 try_match_word
= try_match
;
7156 try_match_word
= FALSE
;
7159 * does it look like a control character?
7163 && (Ctrl_chr(look
[1]) != 0)
7165 && look
[1] >= '?' && look
[1] <= '_'
7169 if (try_match
&& keytyped
== Ctrl_chr(look
[1]))
7174 * 'o' means "o" command, open forward.
7175 * 'O' means "O" command, open backward.
7177 else if (*look
== 'o')
7179 if (try_match
&& keytyped
== KEY_OPEN_FORW
)
7183 else if (*look
== 'O')
7185 if (try_match
&& keytyped
== KEY_OPEN_BACK
)
7191 * 'e' means to check for "else" at start of line and just before the
7194 else if (*look
== 'e')
7196 if (try_match
&& keytyped
== 'e' && curwin
->w_cursor
.col
>= 4)
7198 p
= ml_get_curline();
7199 if (skipwhite(p
) == p
+ curwin
->w_cursor
.col
- 4 &&
7200 STRNCMP(p
+ curwin
->w_cursor
.col
- 4, "else", 4) == 0)
7207 * ':' only causes an indent if it is at the end of a label or case
7208 * statement, or when it was before typing the ':' (to fix
7209 * class::method for C++).
7211 else if (*look
== ':')
7213 if (try_match
&& keytyped
== ':')
7215 p
= ml_get_curline();
7216 if (cin_iscase(p
) || cin_isscopedecl(p
) || cin_islabel(30))
7218 /* Need to get the line again after cin_islabel(). */
7219 p
= ml_get_curline();
7220 if (curwin
->w_cursor
.col
> 2
7221 && p
[curwin
->w_cursor
.col
- 1] == ':'
7222 && p
[curwin
->w_cursor
.col
- 2] == ':')
7224 p
[curwin
->w_cursor
.col
- 1] = ' ';
7225 i
= (cin_iscase(p
) || cin_isscopedecl(p
)
7226 || cin_islabel(30));
7227 p
= ml_get_curline();
7228 p
[curwin
->w_cursor
.col
- 1] = ':';
7238 * Is it a key in <>, maybe?
7240 else if (*look
== '<')
7245 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7246 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7247 * >, *, : and ! keys if they really really want to.
7249 if (vim_strchr((char_u
*)"<>!*oOe0:", look
[1]) != NULL
7250 && keytyped
== look
[1])
7253 if (keytyped
== get_special_key_code(look
+ 1))
7256 while (*look
&& *look
!= '>')
7258 while (*look
== '>')
7263 * Is it a word: "=word"?
7265 else if (*look
== '=' && look
[1] != ',' && look
[1] != NUL
)
7275 p
= vim_strchr(look
, ',');
7277 p
= look
+ STRLEN(look
);
7278 if ((try_match
|| try_match_word
)
7279 && curwin
->w_cursor
.col
>= (colnr_T
)(p
- look
))
7283 #ifdef FEAT_INS_EXPAND
7284 if (keytyped
== KEY_COMPLETE
)
7288 /* Just completed a word, check if it starts with "look".
7289 * search back for the start of a word. */
7290 line
= ml_get_curline();
7296 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; s
= n
)
7298 n
= mb_prevptr(line
, s
);
7299 if (!vim_iswordp(n
))
7305 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; --s
)
7306 if (!vim_iswordc(s
[-1]))
7308 if (s
+ (p
- look
) <= line
+ curwin
->w_cursor
.col
7310 ? MB_STRNICMP(s
, look
, p
- look
)
7311 : STRNCMP(s
, look
, p
- look
)) == 0)
7316 /* TODO: multi-byte */
7317 if (keytyped
== (int)p
[-1] || (icase
&& keytyped
< 256
7318 && TOLOWER_LOC(keytyped
) == TOLOWER_LOC((int)p
[-1])))
7320 line
= ml_get_cursor();
7321 if ((curwin
->w_cursor
.col
== (colnr_T
)(p
- look
)
7322 || !vim_iswordc(line
[-(p
- look
) - 1]))
7324 ? MB_STRNICMP(line
- (p
- look
), look
, p
- look
)
7325 : STRNCMP(line
- (p
- look
), look
, p
- look
))
7329 if (match
&& try_match_word
&& !try_match
)
7331 /* "0=word": Check if there are only blanks before the
7333 line
= ml_get_curline();
7334 if ((int)(skipwhite(line
) - line
) !=
7335 (int)(curwin
->w_cursor
.col
- (p
- look
)))
7345 * ok, it's a boring generic character.
7349 if (try_match
&& *look
== keytyped
)
7357 look
= skip_to_option_part(look
);
7361 #endif /* FEAT_CINDENT */
7363 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7365 * Map Hebrew keyboard when in hkmap mode.
7371 if (p_hkmapp
) /* phonetic mapping, by Ilya Dogolazky */
7373 enum {hALEF
=0, BET
, GIMEL
, DALET
, HEI
, VAV
, ZAIN
, HET
, TET
, IUD
,
7374 KAFsofit
, hKAF
, LAMED
, MEMsofit
, MEM
, NUNsofit
, NUN
, SAMEH
, AIN
,
7375 PEIsofit
, PEI
, ZADIsofit
, ZADI
, KOF
, RESH
, hSHIN
, TAV
};
7376 static char_u map
[26] =
7377 {(char_u
)hALEF
/*a*/, (char_u
)BET
/*b*/, (char_u
)hKAF
/*c*/,
7378 (char_u
)DALET
/*d*/, (char_u
)-1 /*e*/, (char_u
)PEIsofit
/*f*/,
7379 (char_u
)GIMEL
/*g*/, (char_u
)HEI
/*h*/, (char_u
)IUD
/*i*/,
7380 (char_u
)HET
/*j*/, (char_u
)KOF
/*k*/, (char_u
)LAMED
/*l*/,
7381 (char_u
)MEM
/*m*/, (char_u
)NUN
/*n*/, (char_u
)SAMEH
/*o*/,
7382 (char_u
)PEI
/*p*/, (char_u
)-1 /*q*/, (char_u
)RESH
/*r*/,
7383 (char_u
)ZAIN
/*s*/, (char_u
)TAV
/*t*/, (char_u
)TET
/*u*/,
7384 (char_u
)VAV
/*v*/, (char_u
)hSHIN
/*w*/, (char_u
)-1 /*x*/,
7385 (char_u
)AIN
/*y*/, (char_u
)ZADI
/*z*/};
7387 if (c
== 'N' || c
== 'M' || c
== 'P' || c
== 'C' || c
== 'Z')
7388 return (int)(map
[CharOrd(c
)] - 1 + p_aleph
);
7393 return '\''; /* {geresh}={'} */
7395 return ' '; /* \"o --> ' ' for a german keyboard */
7397 return ' '; /* \"a --> ' ' -- / -- */
7399 return ' '; /* \"u --> ' ' -- / -- */
7401 else if (islower(c
))
7403 /* NOTE: islower() does not do the right thing for us on Linux so we
7404 * do this the same was as 5.7 and previous, so it works correctly on
7405 * all systems. Specifically, the e.g. Delete and Arrow keys are
7406 * munged and won't work if e.g. searching for Hebrew text.
7408 else if (c
>= 'a' && c
<= 'z')
7410 return (int)(map
[CharOrdLow(c
)] + p_aleph
);
7418 case '`': return ';';
7419 case '/': return '.';
7420 case '\'': return ',';
7421 case 'q': return '/';
7422 case 'w': return '\'';
7424 /* Hebrew letters - set offset from 'a' */
7425 case ',': c
= '{'; break;
7426 case '.': c
= 'v'; break;
7427 case ';': c
= 't'; break;
7429 static char str
[] = "zqbcxlsjphmkwonu ydafe rig";
7432 /* see note about islower() above */
7435 if (c
< 'a' || c
> 'z')
7438 c
= str
[CharOrdLow(c
)];
7443 return (int)(CharOrdLow(c
) + p_aleph
);
7451 int need_redraw
= FALSE
;
7455 int vis_active
= VIsual_active
;
7459 * If we are going to wait for a character, show a '"'.
7461 pc_status
= PC_STATUS_UNSET
;
7462 if (redrawing() && !char_avail())
7464 /* may need to redraw when no more chars available now */
7467 edit_putchar('"', TRUE
);
7468 #ifdef FEAT_CMDL_INFO
7469 add_to_showcmd_c(Ctrl_R
);
7473 #ifdef USE_ON_FLY_SCROLL
7474 dont_scroll
= TRUE
; /* disallow scrolling here */
7478 * Don't map the register name. This also prevents the mode message to be
7479 * deleted when ESC is hit.
7482 regname
= safe_vgetc();
7484 LANGMAP_ADJUST(regname
, TRUE
);
7486 if (regname
== Ctrl_R
|| regname
== Ctrl_O
|| regname
== Ctrl_P
)
7488 /* Get a third key for literal register insertion */
7489 literally
= regname
;
7490 #ifdef FEAT_CMDL_INFO
7491 add_to_showcmd_c(literally
);
7493 regname
= safe_vgetc();
7495 LANGMAP_ADJUST(regname
, TRUE
);
7502 * Don't call u_sync() while getting the expression,
7503 * evaluating it or giving an error message for it!
7508 # ifdef USE_IM_CONTROL
7509 int im_on
= im_get_status();
7511 regname
= get_expr_register();
7512 # ifdef USE_IM_CONTROL
7513 /* Restore the Input Method. */
7515 im_set_active(TRUE
);
7518 if (regname
== NUL
|| !valid_yank_reg(regname
, FALSE
))
7521 need_redraw
= TRUE
; /* remove the '"' */
7526 if (literally
== Ctrl_O
|| literally
== Ctrl_P
)
7528 /* Append the command to the redo buffer. */
7529 AppendCharToRedobuff(Ctrl_R
);
7530 AppendCharToRedobuff(literally
);
7531 AppendCharToRedobuff(regname
);
7533 do_put(regname
, BACKWARD
, 1L,
7534 (literally
== Ctrl_P
? PUT_FIXINDENT
: 0) | PUT_CURSEND
);
7536 else if (insert_reg(regname
, literally
) == FAIL
)
7539 need_redraw
= TRUE
; /* remove the '"' */
7541 else if (stop_insert_mode
)
7542 /* When the '=' register was used and a function was invoked that
7543 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7544 * insert anything, need to remove the '"' */
7551 #ifdef FEAT_CMDL_INFO
7555 /* If the inserted register is empty, we need to remove the '"' */
7556 if (need_redraw
|| stuff_empty())
7560 /* Disallow starting Visual mode here, would get a weird mode. */
7561 if (!vis_active
&& VIsual_active
)
7567 * CTRL-G commands in Insert mode.
7574 #ifdef FEAT_INS_EXPAND
7575 /* Right after CTRL-X the cursor will be after the ruler. */
7580 * Don't map the second key. This also prevents the mode message to be
7581 * deleted when ESC is hit.
7588 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7591 case 'k': ins_up(TRUE
);
7594 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7597 case 'j': ins_down(TRUE
);
7600 /* CTRL-G u: start new undoable edit */
7601 case 'u': u_sync(TRUE
);
7602 ins_need_undo
= TRUE
;
7604 /* Need to reset Insstart, esp. because a BS that joins
7605 * a line to the previous one must save for undo. */
7606 Insstart
= curwin
->w_cursor
;
7609 /* Unknown CTRL-G command, reserved for future expansion. */
7610 default: vim_beep();
7615 * CTRL-^ in Insert mode.
7620 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
7622 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7623 if (State
& LANGMAP
)
7625 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7630 curbuf
->b_p_iminsert
= B_IMODE_LMAP
;
7632 #ifdef USE_IM_CONTROL
7633 im_set_active(FALSE
);
7637 #ifdef USE_IM_CONTROL
7640 /* There are no ":lmap" mappings, toggle IM */
7641 if (im_get_status())
7643 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7644 im_set_active(FALSE
);
7648 curbuf
->b_p_iminsert
= B_IMODE_IM
;
7650 im_set_active(TRUE
);
7654 set_iminsert_global();
7657 /* may show different cursor shape or color */
7659 gui_update_cursor(TRUE
, FALSE
);
7661 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7662 /* Show/unshow value of 'keymap' in status lines. */
7663 status_redraw_curbuf();
7668 * Handle ESC in insert mode.
7669 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7673 ins_esc(count
, cmdchar
, nomove
)
7676 int nomove
; /* don't move cursor */
7679 static int disabled_redraw
= FALSE
;
7682 check_spell_redraw();
7684 #if defined(FEAT_HANGULIN)
7685 # if defined(ESC_CHG_TO_ENG_MODE)
7686 hangul_input_state_set(0);
7688 if (composing_hangul
)
7690 push_raw_key(composing_hangul_buffer
, 2);
7691 composing_hangul
= 0;
7695 temp
= curwin
->w_cursor
.col
;
7696 if (disabled_redraw
)
7698 --RedrawingDisabled
;
7699 disabled_redraw
= FALSE
;
7704 * Don't append the ESC for "r<CR>" and "grx".
7705 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7706 * when "count" is non-zero.
7708 if (cmdchar
!= 'r' && cmdchar
!= 'v')
7709 AppendToRedobuff(p_im
? (char_u
*)"\014" : ESC_STR
);
7712 * Repeating insert may take a long time. Check for
7713 * interrupt now and then.
7722 if (--*count
> 0) /* repeat what was typed */
7724 /* Vi repeats the insert without replacing characters. */
7725 if (vim_strchr(p_cpo
, CPO_REPLCNT
) != NULL
)
7726 State
&= ~REPLACE_FLAG
;
7728 (void)start_redo_ins();
7729 if (cmdchar
== 'r' || cmdchar
== 'v')
7730 stuffReadbuff(ESC_STR
); /* no ESC in redo buffer */
7731 ++RedrawingDisabled
;
7732 disabled_redraw
= TRUE
;
7733 return FALSE
; /* repeat the insert */
7735 stop_insert(&curwin
->w_cursor
, TRUE
);
7739 /* When an autoindent was removed, curswant stays after the
7741 if (restart_edit
== NUL
&& (colnr_T
)temp
== curwin
->w_cursor
.col
)
7742 curwin
->w_set_curswant
= TRUE
;
7744 /* Remember the last Insert position in the '^ mark. */
7745 if (!cmdmod
.keepjumps
)
7746 curbuf
->b_last_insert
= curwin
->w_cursor
;
7749 * The cursor should end up on the last inserted character.
7750 * Don't do it for CTRL-O, unless past the end of the line.
7753 && (curwin
->w_cursor
.col
!= 0
7754 #ifdef FEAT_VIRTUALEDIT
7755 || curwin
->w_cursor
.coladd
> 0
7758 && (restart_edit
== NUL
7759 || (gchar_cursor() == NUL
7764 #ifdef FEAT_RIGHTLEFT
7769 #ifdef FEAT_VIRTUALEDIT
7770 if (curwin
->w_cursor
.coladd
> 0 || ve_flags
== VE_ALL
)
7773 if (restart_edit
!= NUL
)
7774 ++curwin
->w_cursor
.coladd
;
7779 --curwin
->w_cursor
.col
;
7781 /* Correct cursor for multi-byte character. */
7788 #ifdef USE_IM_CONTROL
7789 /* Disable IM to allow typing English directly for Normal mode commands.
7790 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7792 if (!(State
& LANGMAP
))
7793 im_save_status(&curbuf
->b_p_iminsert
);
7794 im_set_active(FALSE
);
7798 /* need to position cursor again (e.g. when on a TAB ) */
7799 changed_cline_bef_curs();
7805 ui_cursor_shape(); /* may show different cursor shape */
7809 * When recording or for CTRL-O, need to display the new mode.
7810 * Otherwise remove the mode message.
7812 if (Recording
|| restart_edit
!= NUL
)
7817 return TRUE
; /* exit Insert mode */
7820 #ifdef FEAT_RIGHTLEFT
7822 * Toggle language: hkmap and revins_on.
7823 * Move to end of reverse inserted text.
7828 if (revins_on
&& revins_chars
&& revins_scol
>= 0)
7830 while (gchar_cursor() != NUL
&& revins_chars
--)
7831 ++curwin
->w_cursor
.col
;
7834 revins_on
= (State
== INSERT
&& p_ri
);
7837 revins_scol
= curwin
->w_cursor
.col
;
7848 * to be consistent also for redo command, using '.'
7849 * set arrow_used to true and stop it - causing to redo
7850 * characters entered in one mode (normal/reverse insert).
7854 p_fkmap
= curwin
->w_p_rl
^ p_ri
;
7855 if (p_fkmap
&& p_ri
)
7860 p_hkmap
= curwin
->w_p_rl
^ p_ri
; /* be consistent! */
7867 * If 'keymodel' contains "startsel", may start selection.
7868 * Returns TRUE when a CTRL-O and other keys stuffed.
7891 if (!(mod_mask
& MOD_MASK_SHIFT
))
7900 /* Start selection right away, the cursor can move with
7901 * CTRL-O when beyond the end of the line. */
7904 /* Execute the key in (insert) Select mode. */
7905 stuffcharReadbuff(Ctrl_O
);
7911 buf
[1] = KS_MODIFIER
;
7916 stuffcharReadbuff(c
);
7924 * <Insert> key in Insert mode: toggle insert/remplace mode.
7927 ins_insert(replaceState
)
7931 if (p_fkmap
&& p_ri
)
7934 EMSG(farsi_text_3
); /* encoded in Farsi */
7941 set_vim_var_string(VV_INSERTMODE
,
7942 (char_u
*)((State
& REPLACE_FLAG
) ? "i" :
7943 # ifdef FEAT_VREPLACE
7944 replaceState
== VREPLACE
? "v" :
7948 apply_autocmds(EVENT_INSERTCHANGE
, NULL
, NULL
, FALSE
, curbuf
);
7950 if (State
& REPLACE_FLAG
)
7951 State
= INSERT
| (State
& LANGMAP
);
7953 State
= replaceState
| (State
& LANGMAP
);
7954 AppendCharToRedobuff(K_INS
);
7957 ui_cursor_shape(); /* may show different cursor shape */
7962 * Pressed CTRL-O in Insert mode.
7967 #ifdef FEAT_VREPLACE
7968 if (State
& VREPLACE_FLAG
)
7972 if (State
& REPLACE_FLAG
)
7976 #ifdef FEAT_VIRTUALEDIT
7977 if (virtual_active())
7978 ins_at_eol
= FALSE
; /* cursor always keeps its column */
7981 ins_at_eol
= (gchar_cursor() == NUL
);
7985 * If the cursor is on an indent, ^T/^D insert/delete one
7986 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7987 * Always round the indent to 'shiftwith', this is compatible
7988 * with vi. But vi only supports ^T and ^D after an
7989 * autoindent, we support it everywhere.
7996 if (stop_arrow() == FAIL
)
7998 AppendCharToRedobuff(c
);
8001 * 0^D and ^^D: remove all indent.
8003 if ((lastc
== '0' || lastc
== '^') && curwin
->w_cursor
.col
)
8005 --curwin
->w_cursor
.col
;
8006 (void)del_char(FALSE
); /* delete the '^' or '0' */
8007 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8008 if (State
& REPLACE_FLAG
)
8011 old_indent
= get_indent(); /* remember curr. indent */
8012 change_indent(INDENT_SET
, 0, TRUE
, 0);
8015 change_indent(c
== Ctrl_D
? INDENT_DEC
: INDENT_INC
, 0, TRUE
, 0);
8017 if (did_ai
&& *skipwhite(ml_get_curline()) != NUL
)
8019 #ifdef FEAT_SMARTINDENT
8022 can_si_back
= FALSE
;
8025 can_cindent
= FALSE
; /* no cindenting after ^D or ^T */
8034 if (stop_arrow() == FAIL
)
8036 if (gchar_cursor() == NUL
) /* delete newline */
8038 temp
= curwin
->w_cursor
.col
;
8039 if (!can_bs(BS_EOL
) /* only if "eol" included */
8040 || u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
8041 (linenr_T
)(curwin
->w_cursor
.lnum
+ 2)) == FAIL
8042 || do_join(FALSE
) == FAIL
)
8045 curwin
->w_cursor
.col
= temp
;
8047 else if (del_char(FALSE
) == FAIL
) /* delete char under cursor */
8050 #ifdef FEAT_SMARTINDENT
8053 can_si_back
= FALSE
;
8055 AppendCharToRedobuff(K_DEL
);
8059 * Handle Backspace, delete-word and delete-line in Insert mode.
8060 * Return TRUE when backspace was actually used.
8063 ins_bs(c
, mode
, inserted_space_p
)
8066 int *inserted_space_p
;
8070 int temp
= 0; /* init for GCC */
8072 int did_backspace
= FALSE
;
8076 int cpc
[MAX_MCO
]; /* composing characters */
8080 * can't delete anything in an empty file
8081 * can't backup past first character in buffer
8082 * can't backup past starting point unless 'backspace' > 1
8083 * can backup to a previous line if 'backspace' == 0
8087 #ifdef FEAT_RIGHTLEFT
8090 ((curwin
->w_cursor
.lnum
== 1 && curwin
->w_cursor
.col
== 0)
8091 || (!can_bs(BS_START
)
8093 || (curwin
->w_cursor
.lnum
== Insstart
.lnum
8094 && curwin
->w_cursor
.col
<= Insstart
.col
)))
8095 || (!can_bs(BS_INDENT
) && !arrow_used
&& ai_col
> 0
8096 && curwin
->w_cursor
.col
<= ai_col
)
8097 || (!can_bs(BS_EOL
) && curwin
->w_cursor
.col
== 0))))
8103 if (stop_arrow() == FAIL
)
8105 in_indent
= inindent(0);
8108 can_cindent
= FALSE
;
8110 #ifdef FEAT_COMMENTS
8111 end_comment_pending
= NUL
; /* After BS, don't auto-end comment */
8113 #ifdef FEAT_RIGHTLEFT
8114 if (revins_on
) /* put cursor after last inserted char */
8118 #ifdef FEAT_VIRTUALEDIT
8120 * BACKSPACE_CHAR eats a virtual space
8121 * BACKSPACE_WORD eats all coladd
8122 * BACKSPACE_LINE eats all coladd and keeps going
8124 if (curwin
->w_cursor
.coladd
> 0)
8126 if (mode
== BACKSPACE_CHAR
)
8128 --curwin
->w_cursor
.coladd
;
8131 if (mode
== BACKSPACE_WORD
)
8133 curwin
->w_cursor
.coladd
= 0;
8136 curwin
->w_cursor
.coladd
= 0;
8143 if (curwin
->w_cursor
.col
== 0)
8145 lnum
= Insstart
.lnum
;
8146 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8147 #ifdef FEAT_RIGHTLEFT
8152 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 2),
8153 (linenr_T
)(curwin
->w_cursor
.lnum
+ 1)) == FAIL
)
8156 Insstart
.col
= MAXCOL
;
8160 * cc < 0: NL was inserted, delete it
8161 * cc >= 0: NL was replaced, put original characters back
8164 if (State
& REPLACE_FLAG
)
8165 cc
= replace_pop(); /* returns -1 if NL was inserted */
8167 * In replace mode, in the line we started replacing, we only move the
8170 if ((State
& REPLACE_FLAG
) && curwin
->w_cursor
.lnum
<= lnum
)
8176 #ifdef FEAT_VREPLACE
8177 if (!(State
& VREPLACE_FLAG
)
8178 || curwin
->w_cursor
.lnum
> orig_line_count
)
8181 temp
= gchar_cursor(); /* remember current char */
8182 --curwin
->w_cursor
.lnum
;
8184 /* When "aw" is in 'formatoptions' we must delete the space at
8185 * the end of the line, otherwise the line will be broken
8186 * again when auto-formatting. */
8187 if (has_format_option(FO_AUTO
)
8188 && has_format_option(FO_WHITE_PAR
))
8190 char_u
*ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
,
8194 len
= (int)STRLEN(ptr
);
8195 if (len
> 0 && ptr
[len
- 1] == ' ')
8199 (void)do_join(FALSE
);
8200 if (temp
== NUL
&& gchar_cursor() != NUL
)
8203 #ifdef FEAT_VREPLACE
8209 * In REPLACE mode we have to put back the text that was replaced
8210 * by the NL. On the replace stack is first a NUL-terminated
8211 * sequence of characters that were deleted and then the
8212 * characters that NL replaced.
8214 if (State
& REPLACE_FLAG
)
8217 * Do the next ins_char() in NORMAL state, to
8218 * prevent ins_char() from replacing characters and
8219 * avoiding showmatch().
8224 * restore characters (blanks) deleted after cursor
8228 temp
= curwin
->w_cursor
.col
;
8230 mb_replace_pop_ins(cc
);
8234 curwin
->w_cursor
.col
= temp
;
8237 /* restore the characters that NL replaced */
8247 * Delete character(s) before the cursor.
8249 #ifdef FEAT_RIGHTLEFT
8250 if (revins_on
) /* put cursor on last inserted char */
8255 if (mode
== BACKSPACE_LINE
8261 #ifdef FEAT_RIGHTLEFT
8266 temp
= curwin
->w_cursor
.col
;
8267 beginline(BL_WHITE
);
8268 if (curwin
->w_cursor
.col
< (colnr_T
)temp
)
8269 mincol
= curwin
->w_cursor
.col
;
8270 curwin
->w_cursor
.col
= temp
;
8274 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8276 if ( mode
== BACKSPACE_CHAR
8277 && ((p_sta
&& in_indent
)
8278 || (curbuf
->b_p_sts
!= 0
8279 && (*(ml_get_cursor() - 1) == TAB
8280 || (*(ml_get_cursor() - 1) == ' '
8281 && (!*inserted_space_p
8291 *inserted_space_p
= FALSE
;
8292 if (p_sta
&& in_indent
)
8293 ts
= curbuf
->b_p_sw
;
8295 ts
= curbuf
->b_p_sts
;
8296 /* Compute the virtual column where we want to be. Since
8297 * 'showbreak' may get in the way, need to get the last column of
8298 * the previous character. */
8299 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8301 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &want_vcol
);
8303 want_vcol
= (want_vcol
/ ts
) * ts
;
8305 /* delete characters until we are at or before want_vcol */
8306 while (vcol
> want_vcol
8307 && (cc
= *(ml_get_cursor() - 1), vim_iswhite(cc
)))
8310 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8311 if (State
& REPLACE_FLAG
)
8313 /* Don't delete characters before the insert point when in
8315 if (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8316 || curwin
->w_cursor
.col
>= Insstart
.col
)
8318 #if 0 /* what was this for? It causes problems when sw != ts. */
8319 if (State
== REPLACE
&& (int)vcol
< want_vcol
)
8321 (void)del_char(FALSE
);
8322 extra
= 2; /* don't pop too much */
8330 (void)del_char(FALSE
);
8333 /* insert extra spaces until we are at want_vcol */
8334 while (vcol
< want_vcol
)
8336 /* Remember the first char we inserted */
8337 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8338 && curwin
->w_cursor
.col
< Insstart
.col
)
8339 Insstart
.col
= curwin
->w_cursor
.col
;
8341 #ifdef FEAT_VREPLACE
8342 if (State
& VREPLACE_FLAG
)
8347 ins_str((char_u
*)" ");
8348 if ((State
& REPLACE_FLAG
) /* && extra <= 1 */)
8352 replace_push_off(NUL
);
8362 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8367 * Delete upto starting point, start of line or previous word.
8371 #ifdef FEAT_RIGHTLEFT
8372 if (!revins_on
) /* put cursor on char to be deleted */
8376 /* start of word? */
8377 if (mode
== BACKSPACE_WORD
&& !vim_isspace(gchar_cursor()))
8379 mode
= BACKSPACE_WORD_NOT_SPACE
;
8380 temp
= vim_iswordc(gchar_cursor());
8383 else if (mode
== BACKSPACE_WORD_NOT_SPACE
8384 && (vim_isspace(cc
= gchar_cursor())
8385 || vim_iswordc(cc
) != temp
))
8387 #ifdef FEAT_RIGHTLEFT
8391 #ifdef FEAT_RIGHTLEFT
8392 else if (State
& REPLACE_FLAG
)
8397 if (State
& REPLACE_FLAG
)
8402 if (enc_utf8
&& p_deco
)
8403 (void)utfc_ptr2char(ml_get_cursor(), cpc
);
8405 (void)del_char(FALSE
);
8408 * If there are combining characters and 'delcombine' is set
8409 * move the cursor back. Don't back up before the base
8412 if (enc_utf8
&& p_deco
&& cpc
[0] != NUL
)
8415 #ifdef FEAT_RIGHTLEFT
8421 if (revins_on
&& gchar_cursor() == NUL
)
8425 /* Just a single backspace?: */
8426 if (mode
== BACKSPACE_CHAR
)
8429 #ifdef FEAT_RIGHTLEFT
8432 (curwin
->w_cursor
.col
> mincol
8433 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8434 || curwin
->w_cursor
.col
!= Insstart
.col
)));
8435 did_backspace
= TRUE
;
8437 #ifdef FEAT_SMARTINDENT
8440 can_si_back
= FALSE
;
8442 if (curwin
->w_cursor
.col
<= 1)
8445 * It's a little strange to put backspaces into the redo
8446 * buffer, but it makes auto-indent a lot easier to deal
8449 AppendCharToRedobuff(c
);
8451 /* If deleted before the insertion point, adjust it */
8452 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8453 && curwin
->w_cursor
.col
< Insstart
.col
)
8454 Insstart
.col
= curwin
->w_cursor
.col
;
8456 /* vi behaviour: the cursor moves backward but the character that
8457 * was there remains visible
8458 * Vim behaviour: the cursor moves backward and the character that
8459 * was there is erased from the screen.
8460 * We can emulate the vi behaviour by pretending there is a dollar
8461 * displayed even when there isn't.
8462 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8463 if (vim_strchr(p_cpo
, CPO_BACKSPACE
) != NULL
&& dollar_vcol
== 0)
8464 dollar_vcol
= curwin
->w_virtcol
;
8466 return did_backspace
;
8475 win_T
*old_curwin
= curwin
;
8478 /* When GUI is active, also move/paste when 'mouse' is empty */
8481 if (!mouse_has(MOUSE_INSERT
))
8485 tpos
= curwin
->w_cursor
;
8486 if (do_mouse(NULL
, c
, BACKWARD
, 1L, 0))
8489 win_T
*new_curwin
= curwin
;
8491 if (curwin
!= old_curwin
&& win_valid(old_curwin
))
8493 /* Mouse took us to another window. We need to go back to the
8494 * previous one to stop insert there properly. */
8495 curwin
= old_curwin
;
8496 curbuf
= curwin
->w_buffer
;
8499 start_arrow(curwin
== old_curwin
? &tpos
: NULL
);
8501 if (curwin
!= new_curwin
&& win_valid(new_curwin
))
8503 curwin
= new_curwin
;
8504 curbuf
= curwin
->w_buffer
;
8507 # ifdef FEAT_CINDENT
8513 /* redraw status lines (in case another window became active) */
8514 redraw_statuslines();
8523 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8527 tpos
= curwin
->w_cursor
;
8529 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8530 old_curwin
= curwin
;
8532 /* Currently the mouse coordinates are only known in the GUI. */
8533 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
8540 /* find the window at the pointer coordinates */
8541 curwin
= mouse_find_win(&row
, &col
);
8542 curbuf
= curwin
->w_buffer
;
8544 if (curwin
== old_curwin
)
8548 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
8549 scroll_redraw(up
, (long)(curwin
->w_botline
- curwin
->w_topline
));
8551 scroll_redraw(up
, 3L);
8553 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8554 curwin
->w_redr_status
= TRUE
;
8556 curwin
= old_curwin
;
8557 curbuf
= curwin
->w_buffer
;
8560 if (!equalpos(curwin
->w_cursor
, tpos
))
8563 # ifdef FEAT_CINDENT
8570 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8575 /* We will be leaving the current window, unless closing another tab. */
8576 if (c
!= K_TABMENU
|| current_tabmenu
!= TABLINE_MENU_CLOSE
8577 || (current_tab
!= 0 && current_tab
!= tabpage_index(curtab
)))
8580 start_arrow(&curwin
->w_cursor
);
8581 # ifdef FEAT_CINDENT
8587 goto_tabpage(current_tab
);
8591 redraw_statuslines(); /* will redraw the tabline when needed */
8596 #if defined(FEAT_GUI) || defined(PROTO)
8603 tpos
= curwin
->w_cursor
;
8604 if (gui_do_scroll())
8607 # ifdef FEAT_CINDENT
8619 tpos
= curwin
->w_cursor
;
8620 if (gui_do_horiz_scroll())
8623 # ifdef FEAT_CINDENT
8636 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8640 tpos
= curwin
->w_cursor
;
8641 if (oneleft() == OK
)
8643 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8644 /* Only call start_arrow() when not busy with preediting, it will
8645 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8646 if (!im_is_preediting())
8649 #ifdef FEAT_RIGHTLEFT
8650 /* If exit reversed string, position is fixed */
8651 if (revins_scol
!= -1 && (int)curwin
->w_cursor
.col
>= revins_scol
)
8658 * if 'whichwrap' set for cursor in insert mode may go to
8661 else if (vim_strchr(p_ww
, '[') != NULL
&& curwin
->w_cursor
.lnum
> 1)
8664 --(curwin
->w_cursor
.lnum
);
8665 coladvance((colnr_T
)MAXCOL
);
8666 curwin
->w_set_curswant
= TRUE
; /* so we stay at the end */
8679 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8683 tpos
= curwin
->w_cursor
;
8685 curwin
->w_cursor
.lnum
= 1;
8686 curwin
->w_cursor
.col
= 0;
8687 #ifdef FEAT_VIRTUALEDIT
8688 curwin
->w_cursor
.coladd
= 0;
8690 curwin
->w_curswant
= 0;
8701 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8705 tpos
= curwin
->w_cursor
;
8707 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
8708 coladvance((colnr_T
)MAXCOL
);
8709 curwin
->w_curswant
= MAXCOL
;
8718 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8722 if (curwin
->w_cursor
.lnum
> 1 || curwin
->w_cursor
.col
> 0)
8724 start_arrow(&curwin
->w_cursor
);
8725 (void)bck_word(1L, FALSE
, FALSE
);
8726 curwin
->w_set_curswant
= TRUE
;
8736 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8740 if (gchar_cursor() != NUL
|| virtual_active()
8743 start_arrow(&curwin
->w_cursor
);
8744 curwin
->w_set_curswant
= TRUE
;
8745 #ifdef FEAT_VIRTUALEDIT
8746 if (virtual_active())
8753 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
8756 ++curwin
->w_cursor
.col
;
8759 #ifdef FEAT_RIGHTLEFT
8765 /* if 'whichwrap' set for cursor in insert mode, may move the
8766 * cursor to the next line */
8767 else if (vim_strchr(p_ww
, ']') != NULL
8768 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
8770 start_arrow(&curwin
->w_cursor
);
8771 curwin
->w_set_curswant
= TRUE
;
8772 ++curwin
->w_cursor
.lnum
;
8773 curwin
->w_cursor
.col
= 0;
8783 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8787 if (curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
8788 || gchar_cursor() != NUL
)
8790 start_arrow(&curwin
->w_cursor
);
8791 (void)fwd_word(1L, FALSE
, 0);
8792 curwin
->w_set_curswant
= TRUE
;
8800 int startcol
; /* when TRUE move to Insstart.col */
8803 linenr_T old_topline
= curwin
->w_topline
;
8805 int old_topfill
= curwin
->w_topfill
;
8809 tpos
= curwin
->w_cursor
;
8810 if (cursor_up(1L, TRUE
) == OK
)
8813 coladvance(getvcol_nolist(&Insstart
));
8814 if (old_topline
!= curwin
->w_topline
8816 || old_topfill
!= curwin
->w_topfill
8819 redraw_later(VALID
);
8837 if (mod_mask
& MOD_MASK_CTRL
)
8839 /* <C-PageUp>: tab page back */
8840 if (first_tabpage
->tp_next
!= NULL
)
8842 start_arrow(&curwin
->w_cursor
);
8849 tpos
= curwin
->w_cursor
;
8850 if (onepage(BACKWARD
, 1L) == OK
)
8863 int startcol
; /* when TRUE move to Insstart.col */
8866 linenr_T old_topline
= curwin
->w_topline
;
8868 int old_topfill
= curwin
->w_topfill
;
8872 tpos
= curwin
->w_cursor
;
8873 if (cursor_down(1L, TRUE
) == OK
)
8876 coladvance(getvcol_nolist(&Insstart
));
8877 if (old_topline
!= curwin
->w_topline
8879 || old_topfill
!= curwin
->w_topfill
8882 redraw_later(VALID
);
8900 if (mod_mask
& MOD_MASK_CTRL
)
8902 /* <C-PageDown>: tab page forward */
8903 if (first_tabpage
->tp_next
!= NULL
)
8905 start_arrow(&curwin
->w_cursor
);
8912 tpos
= curwin
->w_cursor
;
8913 if (onepage(FORWARD
, 1L) == OK
)
8928 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
8933 * Handle TAB in Insert or Replace mode.
8934 * Return TRUE when the TAB needs to be inserted like a normal character.
8943 if (Insstart_blank_vcol
== MAXCOL
&& curwin
->w_cursor
.lnum
== Insstart
.lnum
)
8944 Insstart_blank_vcol
= get_nolist_virtcol();
8945 if (echeck_abbr(TAB
+ ABBR_OFF
))
8951 can_cindent
= FALSE
;
8955 * When nothing special, insert TAB like a normal character
8958 && !(p_sta
&& ind
&& curbuf
->b_p_ts
!= curbuf
->b_p_sw
)
8959 && curbuf
->b_p_sts
== 0)
8962 if (stop_arrow() == FAIL
)
8966 #ifdef FEAT_SMARTINDENT
8969 can_si_back
= FALSE
;
8971 AppendToRedobuff((char_u
*)"\t");
8973 if (p_sta
&& ind
) /* insert tab in indent, use 'shiftwidth' */
8974 temp
= (int)curbuf
->b_p_sw
;
8975 else if (curbuf
->b_p_sts
> 0) /* use 'softtabstop' when set */
8976 temp
= (int)curbuf
->b_p_sts
;
8977 else /* otherwise use 'tabstop' */
8978 temp
= (int)curbuf
->b_p_ts
;
8979 temp
-= get_nolist_virtcol() % temp
;
8982 * Insert the first space with ins_char(). It will delete one char in
8983 * replace mode. Insert the rest with ins_str(); it will not delete any
8984 * chars. For VREPLACE mode, we use ins_char() for all characters.
8989 #ifdef FEAT_VREPLACE
8990 if (State
& VREPLACE_FLAG
)
8995 ins_str((char_u
*)" ");
8996 if (State
& REPLACE_FLAG
) /* no char replaced */
9002 * When 'expandtab' not set: Replace spaces by TABs where possible.
9004 if (!curbuf
->b_p_et
&& (curbuf
->b_p_sts
|| (p_sta
&& ind
)))
9007 #ifdef FEAT_VREPLACE
9008 char_u
*saved_line
= NULL
; /* init for GCC */
9013 colnr_T want_vcol
, vcol
;
9014 int change_col
= -1;
9015 int save_list
= curwin
->w_p_list
;
9018 * Get the current line. For VREPLACE mode, don't make real changes
9019 * yet, just work on a copy of the line.
9021 #ifdef FEAT_VREPLACE
9022 if (State
& VREPLACE_FLAG
)
9024 pos
= curwin
->w_cursor
;
9026 saved_line
= vim_strsave(ml_get_curline());
9027 if (saved_line
== NULL
)
9029 ptr
= saved_line
+ pos
.col
;
9034 ptr
= ml_get_cursor();
9035 cursor
= &curwin
->w_cursor
;
9038 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9039 if (vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9040 curwin
->w_p_list
= FALSE
;
9042 /* Find first white before the cursor */
9043 fpos
= curwin
->w_cursor
;
9044 while (fpos
.col
> 0 && vim_iswhite(ptr
[-1]))
9050 /* In Replace mode, don't change characters before the insert point. */
9051 if ((State
& REPLACE_FLAG
)
9052 && fpos
.lnum
== Insstart
.lnum
9053 && fpos
.col
< Insstart
.col
)
9055 ptr
+= Insstart
.col
- fpos
.col
;
9056 fpos
.col
= Insstart
.col
;
9059 /* compute virtual column numbers of first white and cursor */
9060 getvcol(curwin
, &fpos
, &vcol
, NULL
, NULL
);
9061 getvcol(curwin
, cursor
, &want_vcol
, NULL
, NULL
);
9063 /* Use as many TABs as possible. Beware of 'showbreak' and
9064 * 'linebreak' adding extra virtual columns. */
9065 while (vim_iswhite(*ptr
))
9067 i
= lbr_chartabsize((char_u
*)"\t", vcol
);
9068 if (vcol
+ i
> want_vcol
)
9075 change_col
= fpos
.col
; /* Column of first change */
9076 /* May have to adjust Insstart */
9077 if (fpos
.lnum
== Insstart
.lnum
&& fpos
.col
< Insstart
.col
)
9078 Insstart
.col
= fpos
.col
;
9086 if (change_col
>= 0)
9090 /* Skip over the spaces we need. */
9091 while (vcol
< want_vcol
&& *ptr
== ' ')
9093 vcol
+= lbr_chartabsize(ptr
, vcol
);
9097 if (vcol
> want_vcol
)
9099 /* Must have a char with 'showbreak' just before it. */
9103 fpos
.col
+= repl_off
;
9105 /* Delete following spaces. */
9106 i
= cursor
->col
- fpos
.col
;
9109 mch_memmove(ptr
, ptr
+ i
, STRLEN(ptr
+ i
) + 1);
9110 /* correct replace stack. */
9111 if ((State
& REPLACE_FLAG
)
9112 #ifdef FEAT_VREPLACE
9113 && !(State
& VREPLACE_FLAG
)
9116 for (temp
= i
; --temp
>= 0; )
9117 replace_join(repl_off
);
9119 #ifdef FEAT_NETBEANS_INTG
9122 netbeans_removed(curbuf
, fpos
.lnum
, cursor
->col
,
9124 netbeans_inserted(curbuf
, fpos
.lnum
, cursor
->col
,
9130 #ifdef FEAT_VREPLACE
9132 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9133 * backspacing over the changed spacing and then inserting the new
9136 if (State
& VREPLACE_FLAG
)
9138 /* Backspace from real cursor to change_col */
9139 backspace_until_column(change_col
);
9141 /* Insert each char in saved_line from changed_col to
9143 ins_bytes_len(saved_line
+ change_col
,
9144 cursor
->col
- change_col
);
9149 #ifdef FEAT_VREPLACE
9150 if (State
& VREPLACE_FLAG
)
9151 vim_free(saved_line
);
9153 curwin
->w_p_list
= save_list
;
9160 * Handle CR or NL in insert mode.
9161 * Return TRUE when out of memory or can't undo.
9169 if (echeck_abbr(c
+ ABBR_OFF
))
9171 if (stop_arrow() == FAIL
)
9176 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9177 * character under the cursor. Only push a NUL on the replace stack,
9178 * nothing to put back when the NL is deleted.
9180 if ((State
& REPLACE_FLAG
)
9181 #ifdef FEAT_VREPLACE
9182 && !(State
& VREPLACE_FLAG
)
9188 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9189 * replacing the next line, so we push all of the characters left on the
9190 * line onto the replace stack. This is not done here though, it is done
9194 #ifdef FEAT_VIRTUALEDIT
9195 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9197 if (virtual_active() && curwin
->w_cursor
.coladd
> 0)
9198 coladvance(getviscol());
9201 #ifdef FEAT_RIGHTLEFT
9203 if (p_altkeymap
&& p_fkmap
)
9206 /* NL in reverse insert will always start in the end of
9209 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
9212 AppendToRedobuff(NL_STR
);
9213 i
= open_line(FORWARD
,
9214 #ifdef FEAT_COMMENTS
9215 has_format_option(FO_RET_COMS
) ? OPENLINE_DO_COM
:
9223 /* When inserting a line the cursor line must never be in a closed fold. */
9230 #ifdef FEAT_DIGRAPHS
9232 * Handle digraph in insert mode.
9233 * Returns character still to be inserted, or NUL when nothing remaining to be
9242 pc_status
= PC_STATUS_UNSET
;
9243 if (redrawing() && !char_avail())
9245 /* may need to redraw when no more chars available now */
9248 edit_putchar('?', TRUE
);
9249 #ifdef FEAT_CMDL_INFO
9250 add_to_showcmd_c(Ctrl_K
);
9254 #ifdef USE_ON_FLY_SCROLL
9255 dont_scroll
= TRUE
; /* disallow scrolling here */
9258 /* don't map the digraph chars. This also prevents the
9259 * mode message to be deleted when ESC is hit */
9265 if (IS_SPECIAL(c
) || mod_mask
) /* special key */
9267 #ifdef FEAT_CMDL_INFO
9270 insert_special(c
, TRUE
, FALSE
);
9275 if (redrawing() && !char_avail())
9277 /* may need to redraw when no more chars available now */
9280 if (char2cells(c
) == 1)
9282 /* first remove the '?', otherwise it's restored when typing
9286 edit_putchar(c
, TRUE
);
9288 #ifdef FEAT_CMDL_INFO
9289 add_to_showcmd_c(c
);
9299 AppendToRedobuff((char_u
*)CTRL_V_STR
);
9300 c
= getdigraph(c
, cc
, TRUE
);
9301 #ifdef FEAT_CMDL_INFO
9308 #ifdef FEAT_CMDL_INFO
9316 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9317 * Returns the char to be inserted, or NUL if none found.
9325 char_u
*ptr
, *prev_ptr
;
9327 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
9333 /* try to advance to the cursor column */
9338 while ((colnr_T
)temp
< curwin
->w_virtcol
&& *ptr
!= NUL
)
9341 temp
+= lbr_chartabsize_adv(&ptr
, (colnr_T
)temp
);
9343 if ((colnr_T
)temp
> curwin
->w_virtcol
)
9347 c
= (*mb_ptr2char
)(ptr
);
9357 * CTRL-Y or CTRL-E typed in Insert mode.
9365 #ifdef FEAT_INS_EXPAND
9366 if (ctrl_x_mode
== CTRL_X_SCROLL
)
9372 redraw_later(VALID
);
9377 c
= ins_copychar(curwin
->w_cursor
.lnum
+ (c
== Ctrl_Y
? -1 : 1));
9382 /* The character must be taken literally, insert like it
9383 * was typed after a CTRL-V, and pretend 'textwidth'
9384 * wasn't set. Digits, 'o' and 'x' are special after a
9385 * CTRL-V, don't use it for these. */
9386 if (c
< 256 && !isalnum(c
))
9387 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
9388 tw_save
= curbuf
->b_p_tw
;
9389 curbuf
->b_p_tw
= -1;
9390 insert_special(c
, TRUE
, FALSE
);
9391 curbuf
->b_p_tw
= tw_save
;
9392 #ifdef FEAT_RIGHTLEFT
9396 c
= Ctrl_V
; /* pretend CTRL-V is last character */
9397 auto_format(FALSE
, TRUE
);
9403 #ifdef FEAT_SMARTINDENT
9405 * Try to do some very smart auto-indenting.
9406 * Used when inserting a "normal" character.
9412 pos_T
*pos
, old_pos
;
9418 * do some very smart indenting when entering '{' or '}'
9420 if (((did_si
|| can_si_back
) && c
== '{') || (can_si
&& c
== '}'))
9423 * for '}' set indent equal to indent of line containing matching '{'
9425 if (c
== '}' && (pos
= findmatch(NULL
, '{')) != NULL
)
9427 old_pos
= curwin
->w_cursor
;
9429 * If the matching '{' has a ')' immediately before it (ignoring
9430 * white-space), then line up with the start of the line
9431 * containing the matching '(' if there is one. This handles the
9432 * case where an "if (..\n..) {" statement continues over multiple
9435 ptr
= ml_get(pos
->lnum
);
9437 if (i
> 0) /* skip blanks before '{' */
9438 while (--i
> 0 && vim_iswhite(ptr
[i
]))
9440 curwin
->w_cursor
.lnum
= pos
->lnum
;
9441 curwin
->w_cursor
.col
= i
;
9442 if (ptr
[i
] == ')' && (pos
= findmatch(NULL
, '(')) != NULL
)
9443 curwin
->w_cursor
= *pos
;
9445 curwin
->w_cursor
= old_pos
;
9446 #ifdef FEAT_VREPLACE
9447 if (State
& VREPLACE_FLAG
)
9448 change_indent(INDENT_SET
, i
, FALSE
, NUL
);
9451 (void)set_indent(i
, SIN_CHANGED
);
9453 else if (curwin
->w_cursor
.col
> 0)
9456 * when inserting '{' after "O" reduce indent, but not
9457 * more than indent of previous line
9460 if (c
== '{' && can_si_back
&& curwin
->w_cursor
.lnum
> 1)
9462 old_pos
= curwin
->w_cursor
;
9464 while (curwin
->w_cursor
.lnum
> 1)
9466 ptr
= skipwhite(ml_get(--(curwin
->w_cursor
.lnum
)));
9468 /* ignore empty lines and lines starting with '#'. */
9469 if (*ptr
!= '#' && *ptr
!= NUL
)
9472 if (get_indent() >= i
)
9474 curwin
->w_cursor
= old_pos
;
9477 shift_line(TRUE
, FALSE
, 1);
9482 * set indent of '#' always to 0
9484 if (curwin
->w_cursor
.col
> 0 && can_si
&& c
== '#')
9486 /* remember current indent for next line */
9487 old_indent
= get_indent();
9488 (void)set_indent(0, SIN_CHANGED
);
9491 /* Adjust ai_col, the char at this position can be deleted. */
9492 if (ai_col
> curwin
->w_cursor
.col
)
9493 ai_col
= curwin
->w_cursor
.col
;
9498 * Get the value that w_virtcol would have when 'list' is off.
9499 * Unless 'cpo' contains the 'L' flag.
9502 get_nolist_virtcol()
9504 if (curwin
->w_p_list
&& vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9505 return getvcol_nolist(&curwin
->w_cursor
);
9507 return curwin
->w_virtcol
;