1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * edit.c: functions for Insert mode
16 #ifdef FEAT_INS_EXPAND
18 * definitions used for CTRL-X submode
20 #define CTRL_X_WANT_IDENT 0x100
22 #define CTRL_X_NOT_DEFINED_YET 1
23 #define CTRL_X_SCROLL 2
24 #define CTRL_X_WHOLE_LINE 3
25 #define CTRL_X_FILES 4
26 #define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27 #define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28 #define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29 #define CTRL_X_FINISHED 8
30 #define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31 #define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32 #define CTRL_X_CMDLINE 11
33 #define CTRL_X_FUNCTION 12
34 #define CTRL_X_OMNI 13
35 #define CTRL_X_SPELL 14
36 #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38 #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
40 static char *ctrl_x_msgs
[] =
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
43 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
45 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
53 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
55 N_(" Omni completion (^O^N^P)"),
56 N_(" Spelling suggestion (s^N^P)"),
57 N_(" Keyword Local completion (^N^P)"),
60 static char e_hitend
[] = N_("Hit end of paragraph");
63 * Structure used to store one match for insert completion.
65 typedef struct compl_S compl_T
;
70 char_u
*cp_str
; /* matched text */
71 char cp_icase
; /* TRUE or FALSE: ignore case */
72 char_u
*(cp_text
[CPT_COUNT
]); /* text for the menu */
73 char_u
*cp_fname
; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
75 int cp_flags
; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number
; /* sequence number */
79 #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
80 #define FREE_FNAME (2)
83 * All the current matches are stored in a list.
84 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
89 static compl_T
*compl_first_match
= NULL
;
90 static compl_T
*compl_curr_match
= NULL
;
91 static compl_T
*compl_shown_match
= NULL
;
93 /* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95 static int compl_enter_selects
= FALSE
;
97 /* When "compl_leader" is not NULL only matches that start with this string
99 static char_u
*compl_leader
= NULL
;
101 static int compl_get_longest
= FALSE
; /* put longest common string
104 static int compl_used_match
; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
108 static int compl_was_interrupted
= FALSE
; /* didn't finish finding
111 static int compl_restarting
= FALSE
; /* don't insert match */
113 /* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
115 static int compl_started
= FALSE
;
117 /* Set when doing something for completion that may call edit() recursively,
118 * which is not allowed. */
119 static int compl_busy
= FALSE
;
121 static int compl_matches
= 0;
122 static char_u
*compl_pattern
= NULL
;
123 static int compl_direction
= FORWARD
;
124 static int compl_shows_dir
= FORWARD
;
125 static int compl_pending
= 0; /* > 1 for postponed CTRL-N */
126 static pos_T compl_startpos
;
127 static colnr_T compl_col
= 0; /* column where the text starts
128 * that is being completed */
129 static char_u
*compl_orig_text
= NULL
; /* text as it was before
130 * completion started */
131 static int compl_cont_mode
= 0;
132 static expand_T compl_xp
;
134 static void ins_ctrl_x
__ARGS((void));
135 static int has_compl_option
__ARGS((int dict_opt
));
136 static int ins_compl_accept_char
__ARGS((int c
));
137 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
));
138 static int ins_compl_equal
__ARGS((compl_T
*match
, char_u
*str
, int len
));
139 static void ins_compl_longest_match
__ARGS((compl_T
*match
));
140 static void ins_compl_add_matches
__ARGS((int num_matches
, char_u
**matches
, int icase
));
141 static int ins_compl_make_cyclic
__ARGS((void));
142 static void ins_compl_upd_pum
__ARGS((void));
143 static void ins_compl_del_pum
__ARGS((void));
144 static int pum_wanted
__ARGS((void));
145 static int pum_enough_matches
__ARGS((void));
146 static void ins_compl_dictionaries
__ARGS((char_u
*dict
, char_u
*pat
, int flags
, int thesaurus
));
147 static void ins_compl_files
__ARGS((int count
, char_u
**files
, int thesaurus
, int flags
, regmatch_T
*regmatch
, char_u
*buf
, int *dir
));
148 static char_u
*find_line_end
__ARGS((char_u
*ptr
));
149 static void ins_compl_free
__ARGS((void));
150 static void ins_compl_clear
__ARGS((void));
151 static int ins_compl_bs
__ARGS((void));
152 static void ins_compl_new_leader
__ARGS((void));
153 static void ins_compl_addleader
__ARGS((int c
));
154 static int ins_compl_len
__ARGS((void));
155 static void ins_compl_restart
__ARGS((void));
156 static void ins_compl_set_original_text
__ARGS((char_u
*str
));
157 static void ins_compl_addfrommatch
__ARGS((void));
158 static int ins_compl_prep
__ARGS((int c
));
159 static buf_T
*ins_compl_next_buf
__ARGS((buf_T
*buf
, int flag
));
160 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
161 static void ins_compl_add_list
__ARGS((list_T
*list
));
163 static int ins_compl_get_exp
__ARGS((pos_T
*ini
));
164 static void ins_compl_delete
__ARGS((void));
165 static void ins_compl_insert
__ARGS((void));
166 static int ins_compl_next
__ARGS((int allow_get_expansion
, int count
, int insert_match
));
167 static int ins_compl_key2dir
__ARGS((int c
));
168 static int ins_compl_pum_key
__ARGS((int c
));
169 static int ins_compl_key2count
__ARGS((int c
));
170 static int ins_compl_use_match
__ARGS((int c
));
171 static int ins_complete
__ARGS((int c
));
172 static unsigned quote_meta
__ARGS((char_u
*dest
, char_u
*str
, int len
));
173 #endif /* FEAT_INS_EXPAND */
175 #define BACKSPACE_CHAR 1
176 #define BACKSPACE_WORD 2
177 #define BACKSPACE_WORD_NOT_SPACE 3
178 #define BACKSPACE_LINE 4
180 static void ins_redraw
__ARGS((int ready
));
181 static void ins_ctrl_v
__ARGS((void));
182 static void undisplay_dollar
__ARGS((void));
183 static void insert_special
__ARGS((int, int, int));
184 static void internal_format
__ARGS((int textwidth
, int second_indent
, int flags
, int format_only
, int c
));
185 static void check_auto_format
__ARGS((int));
186 static void redo_literal
__ARGS((int c
));
187 static void start_arrow
__ARGS((pos_T
*end_insert_pos
));
189 static void check_spell_redraw
__ARGS((void));
190 static void spell_back_to_badword
__ARGS((void));
191 static int spell_bad_len
= 0; /* length of located bad word */
193 static void stop_insert
__ARGS((pos_T
*end_insert_pos
, int esc
));
194 static int echeck_abbr
__ARGS((int));
196 static void replace_push_off
__ARGS((int c
));
198 static int replace_pop
__ARGS((void));
199 static void replace_join
__ARGS((int off
));
200 static void replace_pop_ins
__ARGS((void));
202 static void mb_replace_pop_ins
__ARGS((int cc
));
204 static void replace_flush
__ARGS((void));
205 static void replace_do_bs
__ARGS((int limit_col
));
206 static int del_char_after_col
__ARGS((int limit_col
));
208 static int cindent_on
__ARGS((void));
210 static void ins_reg
__ARGS((void));
211 static void ins_ctrl_g
__ARGS((void));
212 static void ins_ctrl_hat
__ARGS((void));
213 static int ins_esc
__ARGS((long *count
, int cmdchar
, int nomove
));
214 #ifdef FEAT_RIGHTLEFT
215 static void ins_ctrl_
__ARGS((void));
218 static int ins_start_select
__ARGS((int c
));
220 static void ins_insert
__ARGS((int replaceState
));
221 static void ins_ctrl_o
__ARGS((void));
222 static void ins_shift
__ARGS((int c
, int lastc
));
223 static void ins_del
__ARGS((void));
224 static int ins_bs
__ARGS((int c
, int mode
, int *inserted_space_p
));
226 static void ins_mouse
__ARGS((int c
));
227 static void ins_mousescroll
__ARGS((int up
));
229 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
230 static void ins_tabline
__ARGS((int c
));
232 static void ins_left
__ARGS((void));
233 static void ins_home
__ARGS((int c
));
234 static void ins_end
__ARGS((int c
));
235 static void ins_s_left
__ARGS((void));
236 static void ins_right
__ARGS((void));
237 static void ins_s_right
__ARGS((void));
238 static void ins_up
__ARGS((int startcol
));
239 static void ins_pageup
__ARGS((void));
240 static void ins_down
__ARGS((int startcol
));
241 static void ins_pagedown
__ARGS((void));
243 static void ins_drop
__ARGS((void));
245 static int ins_tab
__ARGS((void));
246 static int ins_eol
__ARGS((int c
));
248 static int ins_digraph
__ARGS((void));
250 static int ins_copychar
__ARGS((linenr_T lnum
));
251 static int ins_ctrl_ey
__ARGS((int tc
));
252 #ifdef FEAT_SMARTINDENT
253 static void ins_try_si
__ARGS((int c
));
255 static colnr_T get_nolist_virtcol
__ARGS((void));
257 static colnr_T Insstart_textlen
; /* length of line when insert started */
258 static colnr_T Insstart_blank_vcol
; /* vcol for first inserted blank */
260 static char_u
*last_insert
= NULL
; /* the text of the previous insert,
261 K_SPECIAL and CSI are escaped */
262 static int last_insert_skip
; /* nr of chars in front of previous insert */
263 static int new_insert_skip
; /* nr of chars in front of current insert */
264 static int did_restart_edit
; /* "restart_edit" when calling edit() */
267 static int can_cindent
; /* may do cindenting on this line */
270 static int old_indent
= 0; /* for ^^D command in insert mode */
272 #ifdef FEAT_RIGHTLEFT
273 static int revins_on
; /* reverse insert mode on */
274 static int revins_chars
; /* how much to skip after edit */
275 static int revins_legal
; /* was the last char 'legal'? */
276 static int revins_scol
; /* start column of revins session */
279 static int ins_need_undo
; /* call u_save() before inserting a
280 char. Set when edit() is called.
281 after that arrow_used is used. */
283 static int did_add_space
= FALSE
; /* auto_format() added an extra space
287 * edit(): Start inserting text.
290 * 'i' normal insert command
291 * 'a' normal append command
292 * 'R' replace command
293 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
294 * but still only one <CR> is inserted. The <Esc> is not used for redo.
296 * 'V' "gR" command for Virtual Replace mode.
297 * 'v' "gr" command for single character Virtual Replace mode.
299 * This function is not called recursively. For CTRL-O commands, it returns
300 * and lets the caller handle the Normal-mode command.
302 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
305 edit(cmdchar
, startln
, count
)
307 int startln
; /* if set, insert at start of line */
314 static linenr_T o_lnum
= 0;
316 int did_backspace
= TRUE
; /* previous char was backspace */
318 int line_is_white
= FALSE
; /* line is empty before insert */
320 linenr_T old_topline
= 0; /* topline before insertion */
322 int old_topfill
= -1;
324 int inserted_space
= FALSE
; /* just inserted a space */
325 int replaceState
= REPLACE
;
326 int nomove
= FALSE
; /* don't move cursor on return */
328 /* Remember whether editing was restarted after CTRL-O. */
329 did_restart_edit
= restart_edit
;
331 /* sleep before redrawing, needed for "CTRL-O :" that results in an
333 check_for_delay(TRUE
);
336 /* Don't allow inserting in the sandbox. */
343 /* Don't allow changes in the buffer while editing the cmdline. The
344 * caller of getcmdline() may get confused. */
351 #ifdef FEAT_INS_EXPAND
352 /* Don't allow recursive insert mode when busy with completion. */
353 if (compl_started
|| compl_busy
|| pum_visible())
358 ins_compl_clear(); /* clear stuff for CTRL-X mode */
363 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
365 if (cmdchar
!= 'r' && cmdchar
!= 'v')
370 else if (cmdchar
== 'V')
374 set_vim_var_string(VV_INSERTMODE
, ptr
, 1);
376 apply_autocmds(EVENT_INSERTENTER
, NULL
, NULL
, FALSE
, curbuf
);
382 * When doing a paste with the middle mouse button, Insstart is set to
383 * where the paste started.
385 if (where_paste_started
.lnum
!= 0)
386 Insstart
= where_paste_started
;
390 Insstart
= curwin
->w_cursor
;
394 Insstart_textlen
= (colnr_T
)linetabsize(ml_get_curline());
395 Insstart_blank_vcol
= MAXCOL
;
399 if (cmdchar
!= NUL
&& restart_edit
== 0)
402 AppendNumberToRedobuff(count
);
404 if (cmdchar
== 'V' || cmdchar
== 'v')
406 /* "gR" or "gr" command */
407 AppendCharToRedobuff('g');
408 AppendCharToRedobuff((cmdchar
== 'v') ? 'r' : 'R');
413 AppendCharToRedobuff(cmdchar
);
414 if (cmdchar
== 'g') /* "gI" command */
415 AppendCharToRedobuff('I');
416 else if (cmdchar
== 'r') /* "r<CR>" command */
417 count
= 1; /* insert only one <CR> */
427 EMSG(farsi_text_3
); /* encoded in Farsi */
435 else if (cmdchar
== 'V' || cmdchar
== 'v')
438 replaceState
= VREPLACE
;
439 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
440 vr_lines_changed
= 1;
446 stop_insert_mode
= FALSE
;
449 * Need to recompute the cursor position, it might move when the cursor is
450 * on a TAB or special character.
455 * Enable langmap or IME, indicated by 'iminsert'.
456 * Note that IME may enabled/disabled without us noticing here, thus the
457 * 'iminsert' value may not reflect what is actually used. It is updated
458 * when hitting <Esc>.
460 if (curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
462 #ifdef USE_IM_CONTROL
463 im_set_active(curbuf
->b_p_iminsert
== B_IMODE_IM
);
469 #ifdef FEAT_CMDL_INFO
472 #ifdef FEAT_RIGHTLEFT
473 /* there is no reverse replace mode */
474 revins_on
= (State
== INSERT
&& p_ri
);
483 * Handle restarting Insert mode.
484 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
485 * restart_edit non-zero, and something in the stuff buffer.
487 if (restart_edit
!= 0 && stuff_empty())
491 * After a paste we consider text typed to be part of the insert for
492 * the pasted text. You can backspace over the pasted text too.
494 if (where_paste_started
.lnum
)
502 * If the cursor was after the end-of-line before the CTRL-O and it is
503 * now at the end-of-line, put it after the end-of-line (this is not
504 * correct in very rare cases).
505 * Also do this if curswant is greater than the current virtual
506 * column. Eg after "^O$" or "^O80|".
510 if (((ins_at_eol
&& curwin
->w_cursor
.lnum
== o_lnum
)
511 || curwin
->w_curswant
> curwin
->w_virtcol
)
512 && *(ptr
= ml_get_curline() + curwin
->w_cursor
.col
) != NUL
)
515 ++curwin
->w_cursor
.col
;
519 i
= (*mb_ptr2len
)(ptr
);
521 curwin
->w_cursor
.col
+= i
;
530 /* we are in insert mode now, don't need to start it anymore */
531 need_start_insertmode
= FALSE
;
533 /* Need to save the line for undo before inserting the first char. */
534 ins_need_undo
= TRUE
;
537 where_paste_started
.lnum
= 0;
543 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
545 if (!p_im
&& did_restart_edit
== 0)
550 * If 'showmode' is set, show the current (insert/replace/..) mode.
551 * A warning message for changing a readonly file is given here, before
552 * actually changing anything. It's put after the mode, if any.
555 if (p_smd
&& msg_silent
== 0)
558 if (!p_im
&& did_restart_edit
== 0)
559 change_warning(i
== 0 ? 0 : i
+ 1);
562 ui_cursor_shape(); /* may show different cursor shape */
565 do_digraph(-1); /* clear digraphs */
569 * Get the current length of the redo buffer, those characters have to be
570 * skipped if we want to get to the inserted characters.
572 ptr
= get_inserted();
577 new_insert_skip
= (int)STRLEN(ptr
);
584 * Main loop in Insert mode: repeat until Insert mode is left.
588 #ifdef FEAT_RIGHTLEFT
590 revins_scol
= -1; /* reset on illegal motions */
594 if (arrow_used
) /* don't repeat insert when arrow key used */
597 if (stop_insert_mode
)
599 /* ":stopinsert" used or 'insertmode' reset */
604 /* set curwin->w_curswant for next K_DOWN or K_UP */
606 curwin
->w_set_curswant
= TRUE
;
608 /* If there is no typeahead may check for timestamps (e.g., for when a
609 * menu invoked a shell command). */
612 did_check_timestamps
= FALSE
;
613 if (need_check_timestamps
)
614 check_timestamps(FALSE
);
618 * When emsg() was called msg_scroll will have been set.
623 /* When 'mousefocus' is set a mouse movement may have taken us to
624 * another window. "need_mouse_correct" may then be set because of an
626 if (need_mouse_correct
)
631 /* Open fold at the cursor line, according to 'foldopen'. */
632 if (fdo_flags
& FDO_INSERT
)
634 /* Close folds where the cursor isn't, according to 'foldclose' */
640 * If we inserted a character at the last position of the last line in
641 * the window, scroll the window one line up. This avoids an extra
643 * This is detected when the cursor column is smaller after inserting
645 * Don't do this when the topline changed already, it has
646 * already been adjusted (by insertchar() calling open_line())).
648 if (curbuf
->b_mod_set
651 && curwin
->w_topline
== old_topline
653 && curwin
->w_topfill
== old_topfill
657 mincol
= curwin
->w_wcol
;
658 validate_cursor_col();
662 (int)curwin
->w_wcol
< mincol
663 - tabstop_at(get_nolist_virtcol(), curbuf
->b_p_ts
, curbuf
->b_p_vts_ary
)
665 (int)curwin
->w_wcol
< mincol
- curbuf
->b_p_ts
667 && curwin
->w_wrow
== W_WINROW(curwin
)
668 + curwin
->w_height
- 1 - p_so
669 && (curwin
->w_cursor
.lnum
!= curwin
->w_topline
671 || curwin
->w_topfill
> 0
676 if (curwin
->w_topfill
> 0)
681 if (hasFolding(curwin
->w_topline
, NULL
, &old_topline
))
682 set_topline(curwin
, old_topline
+ 1);
685 set_topline(curwin
, curwin
->w_topline
+ 1);
689 /* May need to adjust w_topline to show the cursor. */
692 did_backspace
= FALSE
;
694 validate_cursor(); /* may set must_redraw */
697 * Redraw the display when no characters are waiting.
698 * Also shows mode, ruler and positions cursor.
702 #ifdef FEAT_SCROLLBIND
704 do_check_scrollbind(TRUE
);
708 old_topline
= curwin
->w_topline
;
710 old_topfill
= curwin
->w_topfill
;
713 #ifdef USE_ON_FLY_SCROLL
714 dont_scroll
= FALSE
; /* allow scrolling here */
718 * Get a character for Insert mode. Ignore K_IGNORE.
720 lastc
= c
; /* remember previous char for CTRL-D */
724 } while (c
== K_IGNORE
);
727 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
728 did_cursorhold
= TRUE
;
731 #ifdef FEAT_RIGHTLEFT
732 if (p_hkmap
&& KeyTyped
)
733 c
= hkmap(c
); /* Hebrew mode mapping */
736 if (p_fkmap
&& KeyTyped
)
737 c
= fkmap(c
); /* Farsi mode mapping */
740 #ifdef FEAT_INS_EXPAND
742 * Special handling of keys while the popup menu is visible or wanted
743 * and the cursor is still in the completed word. Only when there is
744 * a match, skip this when no matches were found.
748 && curwin
->w_cursor
.col
>= compl_col
749 && (compl_shown_match
== NULL
750 || compl_shown_match
!= compl_shown_match
->cp_next
))
752 /* BS: Delete one character from "compl_leader". */
753 if ((c
== K_BS
|| c
== Ctrl_H
)
754 && curwin
->w_cursor
.col
> compl_col
755 && (c
= ins_compl_bs()) == NUL
)
758 /* When no match was selected or it was edited. */
759 if (!compl_used_match
)
761 /* CTRL-L: Add one character from the current match to
762 * "compl_leader". Except when at the original match and
763 * there is nothing to add, CTRL-L works like CTRL-P then. */
765 && (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
766 || (int)STRLEN(compl_shown_match
->cp_str
)
767 > curwin
->w_cursor
.col
- compl_col
))
769 ins_compl_addfrommatch();
773 /* A non-white character that fits in with the current
774 * completion: Add to "compl_leader". */
775 if (ins_compl_accept_char(c
))
777 ins_compl_addleader(c
);
781 /* Pressing CTRL-Y selects the current match. When
782 * compl_enter_selects is set the Enter key does the same. */
783 if (c
== Ctrl_Y
|| (compl_enter_selects
784 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
792 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
793 * it does fix up the text when finishing completion. */
794 compl_get_longest
= FALSE
;
795 if (ins_compl_prep(c
))
799 /* CTRL-\ CTRL-N goes to Normal mode,
800 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
801 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
804 /* may need to redraw when no more chars available now */
811 if (c
!= Ctrl_N
&& c
!= Ctrl_G
&& c
!= Ctrl_O
)
813 /* it's something else */
817 else if (c
== Ctrl_G
&& p_im
)
824 ins_at_eol
= FALSE
; /* cursor keeps its column */
836 #ifdef FEAT_INS_EXPAND
837 if ((c
== Ctrl_V
|| c
== Ctrl_Q
) && ctrl_x_mode
== CTRL_X_CMDLINE
)
840 if (c
== Ctrl_V
|| c
== Ctrl_Q
)
843 c
= Ctrl_V
; /* pretend CTRL-V is last typed character */
849 # ifdef FEAT_INS_EXPAND
854 /* A key name preceded by a bang means this key is not to be
855 * inserted. Skip ahead to the re-indenting below.
856 * A key name preceded by a star means that indenting has to be
857 * done before inserting the key. */
858 line_is_white
= inindent(0);
859 if (in_cinkeys(c
, '!', line_is_white
))
861 if (can_cindent
&& in_cinkeys(c
, '*', line_is_white
)
862 && stop_arrow() == OK
)
867 #ifdef FEAT_RIGHTLEFT
871 case K_LEFT
: c
= K_RIGHT
; break;
872 case K_S_LEFT
: c
= K_S_RIGHT
; break;
873 case K_C_LEFT
: c
= K_C_RIGHT
; break;
874 case K_RIGHT
: c
= K_LEFT
; break;
875 case K_S_RIGHT
: c
= K_S_LEFT
; break;
876 case K_C_RIGHT
: c
= K_C_LEFT
; break;
882 * If 'keymodel' contains "startsel", may start selection. If it
883 * does, a CTRL-O and c will be stuffed, we need to get these
886 if (ins_start_select(c
))
891 * The big switch to handle a character in insert mode.
895 case ESC
: /* End input mode */
896 if (echeck_abbr(ESC
+ ABBR_OFF
))
900 case Ctrl_C
: /* End input mode */
902 if (c
== Ctrl_C
&& cmdwin_type
!= 0)
904 /* Close the cmdline window. */
905 cmdwin_result
= K_IGNORE
;
906 got_int
= FALSE
; /* don't stop executing autocommands et al. */
915 /* when 'insertmode' set, and not halfway a mapping, don't leave
921 (void)vgetc(); /* flush all buffers */
930 * This is the ONLY return from edit()!
932 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
933 * still puts the cursor back after the inserted text. */
934 if (ins_at_eol
&& gchar_cursor() == NUL
)
935 o_lnum
= curwin
->w_cursor
.lnum
;
937 if (ins_esc(&count
, cmdchar
, nomove
))
940 if (cmdchar
!= 'r' && cmdchar
!= 'v')
941 apply_autocmds(EVENT_INSERTLEAVE
, NULL
, NULL
,
943 did_cursorhold
= FALSE
;
945 return (c
== Ctrl_O
);
949 case Ctrl_Z
: /* suspend when 'insertmode' set */
951 goto normalchar
; /* insert CTRL-Z as normal char */
952 stuffReadbuff((char_u
*)":st\r");
956 case Ctrl_O
: /* execute one command */
957 #ifdef FEAT_COMPL_FUNC
958 if (ctrl_x_mode
== CTRL_X_OMNI
)
961 if (echeck_abbr(Ctrl_O
+ ABBR_OFF
))
965 #ifdef FEAT_VIRTUALEDIT
966 /* don't move the cursor left when 'virtualedit' has "onemore". */
967 if (ve_flags
& VE_ONEMORE
)
976 case K_INS
: /* toggle insert/replace mode */
978 ins_insert(replaceState
);
981 case K_SELECT
: /* end of Select mode mapping - ignore */
985 case K_SNIFF
: /* Sniff command received */
986 stuffcharReadbuff(K_SNIFF
);
990 case K_HELP
: /* Help key works like <ESC> <Help> */
993 stuffcharReadbuff(K_HELP
);
995 need_start_insertmode
= TRUE
;
998 #ifdef FEAT_NETBEANS_INTG
999 case K_F21
: /* NetBeans command */
1000 ++no_mapping
; /* don't map the next key hits */
1003 netbeans_keycommand(i
);
1007 case K_ZERO
: /* Insert the previously inserted text. */
1010 /* For ^@ the trailing ESC will end the insert, unless there is an
1012 if (stuff_inserted(NUL
, 1L, (c
== Ctrl_A
)) == FAIL
1013 && c
!= Ctrl_A
&& !p_im
)
1014 goto doESCkey
; /* quit insert mode */
1015 inserted_space
= FALSE
;
1018 case Ctrl_R
: /* insert the contents of a register */
1020 auto_format(FALSE
, TRUE
);
1021 inserted_space
= FALSE
;
1024 case Ctrl_G
: /* commands starting with CTRL-G */
1028 case Ctrl_HAT
: /* switch input mode and/or langmap */
1032 #ifdef FEAT_RIGHTLEFT
1033 case Ctrl__
: /* switch between languages */
1040 case Ctrl_D
: /* Make indent one shiftwidth smaller. */
1041 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1042 if (ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
1047 case Ctrl_T
: /* Make indent one shiftwidth greater. */
1048 # ifdef FEAT_INS_EXPAND
1049 if (c
== Ctrl_T
&& ctrl_x_mode
== CTRL_X_THESAURUS
)
1051 if (has_compl_option(FALSE
))
1056 ins_shift(c
, lastc
);
1057 auto_format(FALSE
, TRUE
);
1058 inserted_space
= FALSE
;
1061 case K_DEL
: /* delete character under the cursor */
1064 auto_format(FALSE
, TRUE
);
1067 case K_BS
: /* delete character before the cursor */
1069 did_backspace
= ins_bs(c
, BACKSPACE_CHAR
, &inserted_space
);
1070 auto_format(FALSE
, TRUE
);
1073 case Ctrl_W
: /* delete word before the cursor */
1074 did_backspace
= ins_bs(c
, BACKSPACE_WORD
, &inserted_space
);
1075 auto_format(FALSE
, TRUE
);
1078 case Ctrl_U
: /* delete all inserted text in current line */
1079 # ifdef FEAT_COMPL_FUNC
1080 /* CTRL-X CTRL-U completes with 'completefunc'. */
1081 if (ctrl_x_mode
== CTRL_X_FUNCTION
)
1084 did_backspace
= ins_bs(c
, BACKSPACE_LINE
, &inserted_space
);
1085 auto_format(FALSE
, TRUE
);
1086 inserted_space
= FALSE
;
1090 case K_LEFTMOUSE
: /* mouse keys */
1091 case K_LEFTMOUSE_NM
:
1094 case K_LEFTRELEASE_NM
:
1097 case K_MIDDLERELEASE
:
1100 case K_RIGHTRELEASE
:
1110 case K_MOUSEDOWN
: /* Default action for scroll wheel up: scroll up */
1111 ins_mousescroll(FALSE
);
1114 case K_MOUSEUP
: /* Default action for scroll wheel down: scroll down */
1115 ins_mousescroll(TRUE
);
1118 #ifdef FEAT_GUI_TABLINE
1125 case K_IGNORE
: /* Something mapped to nothing */
1129 case K_CURSORHOLD
: /* Didn't type something for a while. */
1130 apply_autocmds(EVENT_CURSORHOLDI
, NULL
, NULL
, FALSE
, curbuf
);
1131 did_cursorhold
= TRUE
;
1136 /* On Win32 ignore <M-F4>, we get it when closing the window was
1139 if (mod_mask
!= MOD_MASK_ALT
)
1145 case K_VER_SCROLLBAR
:
1149 case K_HOR_SCROLLBAR
:
1154 case K_HOME
: /* <Home> */
1161 case K_END
: /* <End> */
1168 case K_LEFT
: /* <Left> */
1169 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1175 case K_S_LEFT
: /* <S-Left> */
1180 case K_RIGHT
: /* <Right> */
1181 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1187 case K_S_RIGHT
: /* <S-Right> */
1192 case K_UP
: /* <Up> */
1193 #ifdef FEAT_INS_EXPAND
1197 if (mod_mask
& MOD_MASK_SHIFT
)
1203 case K_S_UP
: /* <S-Up> */
1206 #ifdef FEAT_INS_EXPAND
1213 case K_DOWN
: /* <Down> */
1214 #ifdef FEAT_INS_EXPAND
1218 if (mod_mask
& MOD_MASK_SHIFT
)
1224 case K_S_DOWN
: /* <S-Down> */
1227 #ifdef FEAT_INS_EXPAND
1235 case K_DROP
: /* drag-n-drop event */
1240 case K_S_TAB
: /* When not mapped, use like a normal TAB */
1244 case TAB
: /* TAB or Complete patterns along path */
1245 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1246 if (ctrl_x_mode
== CTRL_X_PATH_PATTERNS
)
1249 inserted_space
= FALSE
;
1251 goto normalchar
; /* insert TAB as a normal char */
1252 auto_format(FALSE
, TRUE
);
1255 case K_KENTER
: /* <Enter> */
1260 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1261 /* In a quickfix window a <CR> jumps to the error under the
1263 if (bt_quickfix(curbuf
) && c
== CAR
)
1265 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
1266 do_cmdline_cmd((char_u
*)".cc");
1267 else /* location list window */
1268 do_cmdline_cmd((char_u
*)".ll");
1273 if (cmdwin_type
!= 0)
1275 /* Execute the command in the cmdline window. */
1276 cmdwin_result
= CAR
;
1280 if (ins_eol(c
) && !p_im
)
1281 goto doESCkey
; /* out of memory */
1282 auto_format(FALSE
, FALSE
);
1283 inserted_space
= FALSE
;
1286 #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
1287 case Ctrl_K
: /* digraph or keyword completion */
1288 # ifdef FEAT_INS_EXPAND
1289 if (ctrl_x_mode
== CTRL_X_DICTIONARY
)
1291 if (has_compl_option(TRUE
))
1296 # ifdef FEAT_DIGRAPHS
1304 #ifdef FEAT_INS_EXPAND
1305 case Ctrl_X
: /* Enter CTRL-X mode */
1309 case Ctrl_RSB
: /* Tag name completion after ^X */
1310 if (ctrl_x_mode
!= CTRL_X_TAGS
)
1314 case Ctrl_F
: /* File name completion after ^X */
1315 if (ctrl_x_mode
!= CTRL_X_FILES
)
1319 case 's': /* Spelling completion after ^X */
1321 if (ctrl_x_mode
!= CTRL_X_SPELL
)
1326 case Ctrl_L
: /* Whole line completion after ^X */
1327 #ifdef FEAT_INS_EXPAND
1328 if (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
1331 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1334 if (echeck_abbr(Ctrl_L
+ ABBR_OFF
))
1340 #ifdef FEAT_INS_EXPAND
1343 case Ctrl_P
: /* Do previous/next pattern completion */
1345 /* if 'complete' is empty then plain ^P is no longer special,
1346 * but it is under other ^X modes */
1347 if (*curbuf
->b_p_cpt
== NUL
1349 && !(compl_cont_status
& CONT_LOCAL
))
1354 if (ins_complete(c
) == FAIL
)
1355 compl_cont_status
= 0;
1358 #endif /* FEAT_INS_EXPAND */
1360 case Ctrl_Y
: /* copy from previous line or scroll down */
1361 case Ctrl_E
: /* copy from next line or scroll up */
1367 if (c
== intr_char
) /* special interrupt char */
1372 * Insert a nomal character.
1375 #ifdef FEAT_SMARTINDENT
1376 /* Try to perform smart-indenting. */
1382 inserted_space
= TRUE
;
1385 can_cindent
= FALSE
;
1387 if (Insstart_blank_vcol
== MAXCOL
1388 && curwin
->w_cursor
.lnum
== Insstart
.lnum
)
1389 Insstart_blank_vcol
= get_nolist_virtcol();
1392 if (vim_iswordc(c
) || !echeck_abbr(
1394 /* Add ABBR_OFF for characters above 0x100, this is
1395 * what check_abbr() expects. */
1396 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1400 insert_special(c
, FALSE
, FALSE
);
1401 #ifdef FEAT_RIGHTLEFT
1407 auto_format(FALSE
, TRUE
);
1410 /* When inserting a character the cursor line must never be in a
1415 } /* end of switch (c) */
1418 /* If typed something may trigger CursorHoldI again. */
1419 if (c
!= K_CURSORHOLD
)
1420 did_cursorhold
= FALSE
;
1423 /* If the cursor was moved we didn't just insert a space */
1425 inserted_space
= FALSE
;
1428 if (can_cindent
&& cindent_on()
1429 # ifdef FEAT_INS_EXPAND
1436 * Indent now if a key was typed that is in 'cinkeys'.
1438 if (in_cinkeys(c
, ' ', line_is_white
))
1440 if (stop_arrow() == OK
)
1441 /* re-indent the current line */
1445 #endif /* FEAT_CINDENT */
1452 * Redraw for Insert mode.
1453 * This is postponed until getting the next character to make '$' in the 'cpo'
1454 * option work correctly.
1455 * Only redraw when there are no characters available. This speeds up
1456 * inserting sequences of characters (e.g., for CTRL-R).
1460 int ready UNUSED
; /* not busy with something */
1465 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1466 * visible, the command might delete it. */
1467 if (ready
&& has_cursormovedI()
1468 && !equalpos(last_cursormoved
, curwin
->w_cursor
)
1469 # ifdef FEAT_INS_EXPAND
1475 /* Need to update the screen first, to make sure syntax
1476 * highlighting is correct after making a change (e.g., inserting
1477 * a "(". The autocommand may also require a redraw, so it's done
1478 * again below, unfortunately. */
1479 if (syntax_present(curbuf
) && must_redraw
)
1482 apply_autocmds(EVENT_CURSORMOVEDI
, NULL
, NULL
, FALSE
, curbuf
);
1483 last_cursormoved
= curwin
->w_cursor
;
1488 else if (clear_cmdline
|| redraw_cmdline
)
1489 showmode(); /* clear cmdline and show mode */
1492 emsg_on_display
= FALSE
; /* may remove error message now */
1497 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1504 /* may need to redraw when no more chars available now */
1507 if (redrawing() && !char_avail())
1508 edit_putchar('^', TRUE
);
1509 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
1511 #ifdef FEAT_CMDL_INFO
1512 add_to_showcmd_c(Ctrl_V
);
1516 #ifdef FEAT_CMDL_INFO
1519 insert_special(c
, FALSE
, TRUE
);
1520 #ifdef FEAT_RIGHTLEFT
1527 * Put a character directly onto the screen. It's not stored in a buffer.
1528 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1530 static int pc_status
;
1531 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1532 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1533 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1534 #define PC_STATUS_SET 3 /* pc_bytes was filled */
1536 static char_u pc_bytes
[MB_MAXBYTES
+ 1]; /* saved bytes */
1538 static char_u pc_bytes
[2]; /* saved bytes */
1545 edit_putchar(c
, highlight
)
1551 if (ScreenLines
!= NULL
)
1553 update_topline(); /* just in case w_topline isn't valid */
1556 attr
= hl_attr(HLF_8
);
1559 pc_row
= W_WINROW(curwin
) + curwin
->w_wrow
;
1560 pc_col
= W_WINCOL(curwin
);
1561 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1562 pc_status
= PC_STATUS_UNSET
;
1564 #ifdef FEAT_RIGHTLEFT
1567 pc_col
+= W_WIDTH(curwin
) - 1 - curwin
->w_wcol
;
1571 int fix_col
= mb_fix_col(pc_col
, pc_row
);
1573 if (fix_col
!= pc_col
)
1575 screen_putchar(' ', pc_row
, fix_col
, attr
);
1577 pc_status
= PC_STATUS_RIGHT
;
1585 pc_col
+= curwin
->w_wcol
;
1587 if (mb_lefthalve(pc_row
, pc_col
))
1588 pc_status
= PC_STATUS_LEFT
;
1592 /* save the character to be able to put it back */
1593 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1594 if (pc_status
== PC_STATUS_UNSET
)
1597 screen_getbytes(pc_row
, pc_col
, pc_bytes
, &pc_attr
);
1598 pc_status
= PC_STATUS_SET
;
1600 screen_putchar(c
, pc_row
, pc_col
, attr
);
1605 * Undo the previous edit_putchar().
1610 if (pc_status
!= PC_STATUS_UNSET
&& pc_row
>= msg_scrolled
)
1612 #if defined(FEAT_MBYTE)
1613 if (pc_status
== PC_STATUS_RIGHT
)
1615 if (pc_status
== PC_STATUS_RIGHT
|| pc_status
== PC_STATUS_LEFT
)
1616 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1619 screen_puts(pc_bytes
, pc_row
- msg_scrolled
, pc_col
, pc_attr
);
1624 * Called when p_dollar is set: display a '$' at the end of the changed text
1625 * Only works when cursor is in the line that changes.
1637 save_col
= curwin
->w_cursor
.col
;
1638 curwin
->w_cursor
.col
= col
;
1644 /* If on the last byte of a multi-byte move to the first byte. */
1645 p
= ml_get_curline();
1646 curwin
->w_cursor
.col
-= (*mb_head_off
)(p
, p
+ col
);
1649 curs_columns(FALSE
); /* recompute w_wrow and w_wcol */
1650 if (curwin
->w_wcol
< W_WIDTH(curwin
))
1652 edit_putchar('$', FALSE
);
1653 dollar_vcol
= curwin
->w_virtcol
;
1655 curwin
->w_cursor
.col
= save_col
;
1659 * Call this function before moving the cursor from the normal insert position
1668 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1673 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1674 * Keep the cursor on the same character.
1675 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1676 * type == INDENT_DEC decrease indent (for CTRL-D)
1677 * type == INDENT_SET set indent to "amount"
1678 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1681 change_indent(type
, amount
, round
, replaced
, call_changed_bytes
)
1685 int replaced
; /* replaced character, put on replace stack */
1686 int call_changed_bytes
; /* call changed_bytes() */
1690 int insstart_less
; /* reduction for Insstart.col */
1697 #ifdef FEAT_VREPLACE
1698 colnr_T orig_col
= 0; /* init for GCC */
1699 char_u
*new_line
, *orig_line
= NULL
; /* init for GCC */
1701 /* VREPLACE mode needs to know what the line was like before changing */
1702 if (State
& VREPLACE_FLAG
)
1704 orig_line
= vim_strsave(ml_get_curline()); /* Deal with NULL below */
1705 orig_col
= curwin
->w_cursor
.col
;
1709 /* for the following tricks we don't want list mode */
1710 save_p_list
= curwin
->w_p_list
;
1711 curwin
->w_p_list
= FALSE
;
1712 vc
= getvcol_nolist(&curwin
->w_cursor
);
1716 * For Replace mode we need to fix the replace stack later, which is only
1717 * possible when the cursor is in the indent. Remember the number of
1718 * characters before the cursor if it's possible.
1720 start_col
= curwin
->w_cursor
.col
;
1722 /* determine offset from first non-blank */
1723 new_cursor_col
= curwin
->w_cursor
.col
;
1724 beginline(BL_WHITE
);
1725 new_cursor_col
-= curwin
->w_cursor
.col
;
1727 insstart_less
= curwin
->w_cursor
.col
;
1730 * If the cursor is in the indent, compute how many screen columns the
1731 * cursor is to the left of the first non-blank.
1733 if (new_cursor_col
< 0)
1734 vcol
= get_indent() - vcol
;
1736 if (new_cursor_col
> 0) /* can't fix replace stack */
1740 * Set the new indent. The cursor will be put on the first non-blank.
1742 if (type
== INDENT_SET
)
1743 (void)set_indent(amount
, call_changed_bytes
? SIN_CHANGED
: 0);
1746 #ifdef FEAT_VREPLACE
1747 int save_State
= State
;
1749 /* Avoid being called recursively. */
1750 if (State
& VREPLACE_FLAG
)
1753 shift_line(type
== INDENT_DEC
, round
, 1, call_changed_bytes
);
1754 #ifdef FEAT_VREPLACE
1758 insstart_less
-= curwin
->w_cursor
.col
;
1761 * Try to put cursor on same character.
1762 * If the cursor is at or after the first non-blank in the line,
1763 * compute the cursor column relative to the column of the first
1764 * non-blank character.
1765 * If we are not in insert mode, leave the cursor on the first non-blank.
1766 * If the cursor is before the first non-blank, position it relative
1767 * to the first non-blank, counted in screen columns.
1769 if (new_cursor_col
>= 0)
1772 * When changing the indent while the cursor is touching it, reset
1773 * Insstart_col to 0.
1775 if (new_cursor_col
== 0)
1776 insstart_less
= MAXCOL
;
1777 new_cursor_col
+= curwin
->w_cursor
.col
;
1779 else if (!(State
& INSERT
))
1780 new_cursor_col
= curwin
->w_cursor
.col
;
1784 * Compute the screen column where the cursor should be.
1786 vcol
= get_indent() - vcol
;
1787 curwin
->w_virtcol
= (colnr_T
)((vcol
< 0) ? 0 : vcol
);
1790 * Advance the cursor until we reach the right screen column.
1792 vcol
= last_vcol
= 0;
1793 new_cursor_col
= -1;
1794 ptr
= ml_get_curline();
1795 while (vcol
<= (int)curwin
->w_virtcol
)
1799 if (has_mbyte
&& new_cursor_col
>= 0)
1800 new_cursor_col
+= (*mb_ptr2len
)(ptr
+ new_cursor_col
);
1804 vcol
+= lbr_chartabsize(ptr
+ new_cursor_col
, (colnr_T
)vcol
);
1809 * May need to insert spaces to be able to position the cursor on
1810 * the right screen column.
1812 if (vcol
!= (int)curwin
->w_virtcol
)
1814 curwin
->w_cursor
.col
= (colnr_T
)new_cursor_col
;
1815 i
= (int)curwin
->w_virtcol
- vcol
;
1816 ptr
= alloc((unsigned)(i
+ 1));
1819 new_cursor_col
+= i
;
1829 * When changing the indent while the cursor is in it, reset
1830 * Insstart_col to 0.
1832 insstart_less
= MAXCOL
;
1835 curwin
->w_p_list
= save_p_list
;
1837 if (new_cursor_col
<= 0)
1838 curwin
->w_cursor
.col
= 0;
1840 curwin
->w_cursor
.col
= (colnr_T
)new_cursor_col
;
1841 curwin
->w_set_curswant
= TRUE
;
1842 changed_cline_bef_curs();
1845 * May have to adjust the start of the insert.
1849 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
&& Insstart
.col
!= 0)
1851 if ((int)Insstart
.col
<= insstart_less
)
1854 Insstart
.col
-= insstart_less
;
1856 if ((int)ai_col
<= insstart_less
)
1859 ai_col
-= insstart_less
;
1863 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1864 * If the number of characters before the cursor decreased, need to pop a
1865 * few characters from the replace stack.
1866 * If the number of characters before the cursor increased, need to push a
1867 * few NULs onto the replace stack.
1869 if (REPLACE_NORMAL(State
) && start_col
>= 0)
1871 while (start_col
> (int)curwin
->w_cursor
.col
)
1873 replace_join(0); /* remove a NUL from the replace stack */
1876 while (start_col
< (int)curwin
->w_cursor
.col
|| replaced
)
1881 replace_push(replaced
);
1888 #ifdef FEAT_VREPLACE
1890 * For VREPLACE mode, we also have to fix the replace stack. In this case
1891 * it is always possible because we backspace over the whole line and then
1892 * put it back again the way we wanted it.
1894 if (State
& VREPLACE_FLAG
)
1896 /* If orig_line didn't allocate, just return. At least we did the job,
1897 * even if you can't backspace. */
1898 if (orig_line
== NULL
)
1902 new_line
= vim_strsave(ml_get_curline());
1903 if (new_line
== NULL
)
1906 /* We only put back the new line up to the cursor */
1907 new_line
[curwin
->w_cursor
.col
] = NUL
;
1909 /* Put back original line */
1910 ml_replace(curwin
->w_cursor
.lnum
, orig_line
, FALSE
);
1911 curwin
->w_cursor
.col
= orig_col
;
1913 /* Backspace from cursor to start of line */
1914 backspace_until_column(0);
1916 /* Insert new stuff into line again */
1917 ins_bytes(new_line
);
1925 * Truncate the space at the end of a line. This is to be used only in an
1926 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1930 truncate_spaces(line
)
1935 /* find start of trailing white space */
1936 for (i
= (int)STRLEN(line
) - 1; i
>= 0 && vim_iswhite(line
[i
]); i
--)
1938 if (State
& REPLACE_FLAG
)
1939 replace_join(0); /* remove a NUL from the replace stack */
1944 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1945 || defined(FEAT_COMMENTS) || defined(PROTO)
1947 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1948 * modes correctly. May also be used when not in insert mode at all.
1949 * Will attempt not to go before "col" even when there is a composing
1953 backspace_until_column(col
)
1956 while ((int)curwin
->w_cursor
.col
> col
)
1958 curwin
->w_cursor
.col
--;
1959 if (State
& REPLACE_FLAG
)
1961 else if (!del_char_after_col(col
))
1968 * Like del_char(), but make sure not to go before column "limit_col".
1969 * Only matters when there are composing characters.
1970 * Return TRUE when something was deleted.
1973 del_char_after_col(limit_col
)
1974 int limit_col UNUSED
;
1977 if (enc_utf8
&& limit_col
>= 0)
1979 colnr_T ecol
= curwin
->w_cursor
.col
+ 1;
1981 /* Make sure the cursor is at the start of a character, but
1982 * skip forward again when going too far back because of a
1983 * composing character. */
1985 while (curwin
->w_cursor
.col
< (colnr_T
)limit_col
)
1987 int l
= utf_ptr2len(ml_get_cursor());
1989 if (l
== 0) /* end of line */
1991 curwin
->w_cursor
.col
+= l
;
1993 if (*ml_get_cursor() == NUL
|| curwin
->w_cursor
.col
== ecol
)
1995 del_bytes((long)((int)ecol
- curwin
->w_cursor
.col
), FALSE
, TRUE
);
1999 (void)del_char(FALSE
);
2003 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
2005 * CTRL-X pressed in Insert mode.
2010 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2011 * CTRL-V works like CTRL-N */
2012 if (ctrl_x_mode
!= CTRL_X_CMDLINE
)
2014 /* if the next ^X<> won't ADD nothing, then reset
2015 * compl_cont_status */
2016 if (compl_cont_status
& CONT_N_ADDS
)
2017 compl_cont_status
|= CONT_INTRPT
;
2019 compl_cont_status
= 0;
2020 /* We're not sure which CTRL-X mode it will be yet */
2021 ctrl_x_mode
= CTRL_X_NOT_DEFINED_YET
;
2022 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
2023 edit_submode_pre
= NULL
;
2029 * Return TRUE if the 'dict' or 'tsr' option can be used.
2032 has_compl_option(dict_opt
)
2035 if (dict_opt
? (*curbuf
->b_p_dict
== NUL
&& *p_dict
== NUL
2037 && !curwin
->w_p_spell
2040 : (*curbuf
->b_p_tsr
== NUL
&& *p_tsr
== NUL
))
2043 edit_submode
= NULL
;
2044 msg_attr(dict_opt
? (char_u
*)_("'dictionary' option is empty")
2045 : (char_u
*)_("'thesaurus' option is empty"),
2047 if (emsg_silent
== 0)
2052 ui_delay(2000L, FALSE
);
2060 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2061 * This depends on the current mode.
2064 vim_is_ctrl_x_key(c
)
2067 /* Always allow ^R - let it's results then be checked */
2071 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
2072 if (ins_compl_pum_key(c
))
2075 switch (ctrl_x_mode
)
2077 case 0: /* Not in any CTRL-X mode */
2078 return (c
== Ctrl_N
|| c
== Ctrl_P
|| c
== Ctrl_X
);
2079 case CTRL_X_NOT_DEFINED_YET
:
2080 return ( c
== Ctrl_X
|| c
== Ctrl_Y
|| c
== Ctrl_E
2081 || c
== Ctrl_L
|| c
== Ctrl_F
|| c
== Ctrl_RSB
2082 || c
== Ctrl_I
|| c
== Ctrl_D
|| c
== Ctrl_P
2083 || c
== Ctrl_N
|| c
== Ctrl_T
|| c
== Ctrl_V
2084 || c
== Ctrl_Q
|| c
== Ctrl_U
|| c
== Ctrl_O
2085 || c
== Ctrl_S
|| c
== 's');
2087 return (c
== Ctrl_Y
|| c
== Ctrl_E
);
2088 case CTRL_X_WHOLE_LINE
:
2089 return (c
== Ctrl_L
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2091 return (c
== Ctrl_F
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2092 case CTRL_X_DICTIONARY
:
2093 return (c
== Ctrl_K
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2094 case CTRL_X_THESAURUS
:
2095 return (c
== Ctrl_T
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2097 return (c
== Ctrl_RSB
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2099 case CTRL_X_PATH_PATTERNS
:
2100 return (c
== Ctrl_P
|| c
== Ctrl_N
);
2101 case CTRL_X_PATH_DEFINES
:
2102 return (c
== Ctrl_D
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2104 case CTRL_X_CMDLINE
:
2105 return (c
== Ctrl_V
|| c
== Ctrl_Q
|| c
== Ctrl_P
|| c
== Ctrl_N
2107 #ifdef FEAT_COMPL_FUNC
2108 case CTRL_X_FUNCTION
:
2109 return (c
== Ctrl_U
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2111 return (c
== Ctrl_O
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2114 return (c
== Ctrl_S
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2116 EMSG(_(e_internal
));
2121 * Return TRUE when character "c" is part of the item currently being
2122 * completed. Used to decide whether to abandon complete mode when the menu
2126 ins_compl_accept_char(c
)
2129 if (ctrl_x_mode
& CTRL_X_WANT_IDENT
)
2130 /* When expanding an identifier only accept identifier chars. */
2131 return vim_isIDc(c
);
2133 switch (ctrl_x_mode
)
2136 /* When expanding file name only accept file name chars. But not
2137 * path separators, so that "proto/<Tab>" expands files in
2138 * "proto", not "proto/" as a whole */
2139 return vim_isfilec(c
) && !vim_ispathsep(c
);
2141 case CTRL_X_CMDLINE
:
2143 /* Command line and Omni completion can work with just about any
2144 * printable character, but do stop at white space. */
2145 return vim_isprintc(c
) && !vim_iswhite(c
);
2147 case CTRL_X_WHOLE_LINE
:
2148 /* For while line completion a space can be part of the line. */
2149 return vim_isprintc(c
);
2151 return vim_iswordc(c
);
2155 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
2156 * case of the originally typed text is used, and the case of the completed
2157 * text is inferred, ie this tries to work out what case you probably wanted
2158 * the rest of the word to be in -- webb
2161 ins_compl_add_infercase(str
, len
, icase
, fname
, dir
, flags
)
2171 int actual_len
; /* Take multi-byte characters */
2172 int actual_compl_length
; /* into account. */
2173 int *wca
; /* Wide character array. */
2174 int has_lower
= FALSE
;
2175 int was_letter
= FALSE
;
2177 if (p_ic
&& curbuf
->b_p_inf
&& len
> 0)
2179 /* Infer case of completed part. */
2181 /* Find actual length of completion. */
2197 /* Find actual length of original text. */
2201 p
= compl_orig_text
;
2202 actual_compl_length
= 0;
2206 ++actual_compl_length
;
2211 actual_compl_length
= compl_length
;
2213 /* Allocate wide character array for the completion and fill it. */
2214 wca
= (int *)alloc((unsigned)(actual_len
* sizeof(int)));
2218 for (i
= 0; i
< actual_len
; ++i
)
2221 wca
[i
] = mb_ptr2char_adv(&p
);
2226 /* Rule 1: Were any chars converted to lower? */
2227 p
= compl_orig_text
;
2228 for (i
= 0; i
< actual_compl_length
; ++i
)
2232 c
= mb_ptr2char_adv(&p
);
2239 if (MB_ISUPPER(wca
[i
]))
2241 /* Rule 1 is satisfied. */
2242 for (i
= actual_compl_length
; i
< actual_len
; ++i
)
2243 wca
[i
] = MB_TOLOWER(wca
[i
]);
2250 * Rule 2: No lower case, 2nd consecutive letter converted to
2255 p
= compl_orig_text
;
2256 for (i
= 0; i
< actual_compl_length
; ++i
)
2260 c
= mb_ptr2char_adv(&p
);
2264 if (was_letter
&& MB_ISUPPER(c
) && MB_ISLOWER(wca
[i
]))
2266 /* Rule 2 is satisfied. */
2267 for (i
= actual_compl_length
; i
< actual_len
; ++i
)
2268 wca
[i
] = MB_TOUPPER(wca
[i
]);
2271 was_letter
= MB_ISLOWER(c
) || MB_ISUPPER(c
);
2275 /* Copy the original case of the part we typed. */
2276 p
= compl_orig_text
;
2277 for (i
= 0; i
< actual_compl_length
; ++i
)
2281 c
= mb_ptr2char_adv(&p
);
2286 wca
[i
] = MB_TOLOWER(wca
[i
]);
2287 else if (MB_ISUPPER(c
))
2288 wca
[i
] = MB_TOUPPER(wca
[i
]);
2292 * Generate encoding specific output from wide character array.
2293 * Multi-byte characters can occupy up to five bytes more than
2294 * ASCII characters, and we also need one byte for NUL, so stay
2295 * six bytes away from the edge of IObuff.
2299 while (i
< actual_len
&& (p
- IObuff
+ 6) < IOSIZE
)
2302 p
+= (*mb_char2bytes
)(wca
[i
++], p
);
2311 return ins_compl_add(IObuff
, len
, icase
, fname
, NULL
, dir
,
2314 return ins_compl_add(str
, len
, icase
, fname
, NULL
, dir
, flags
, FALSE
);
2318 * Add a match to the list of matches.
2319 * If the given string is already in the list of completions, then return
2320 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2321 * maybe because alloc() returns NULL, then FAIL is returned.
2324 ins_compl_add(str
, len
, icase
, fname
, cptext
, cdir
, flags
, adup
)
2329 char_u
**cptext
; /* extra text for popup menu or NULL */
2332 int adup
; /* accept duplicate match */
2335 int dir
= (cdir
== 0 ? compl_direction
: cdir
);
2341 len
= (int)STRLEN(str
);
2344 * If the same match is already present, don't add it.
2346 if (compl_first_match
!= NULL
&& !adup
)
2348 match
= compl_first_match
;
2351 if ( !(match
->cp_flags
& ORIGINAL_TEXT
)
2352 && STRNCMP(match
->cp_str
, str
, len
) == 0
2353 && match
->cp_str
[len
] == NUL
)
2355 match
= match
->cp_next
;
2356 } while (match
!= NULL
&& match
!= compl_first_match
);
2359 /* Remove any popup menu before changing the list of matches. */
2360 ins_compl_del_pum();
2363 * Allocate a new match structure.
2364 * Copy the values to the new match structure.
2366 match
= (compl_T
*)alloc_clear((unsigned)sizeof(compl_T
));
2369 match
->cp_number
= -1;
2370 if (flags
& ORIGINAL_TEXT
)
2371 match
->cp_number
= 0;
2372 if ((match
->cp_str
= vim_strnsave(str
, len
)) == NULL
)
2377 match
->cp_icase
= icase
;
2380 * - compl_curr_match->cp_fname if it is a string equal to fname.
2381 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2382 * - NULL otherwise. --Acevedo */
2384 && compl_curr_match
!= NULL
2385 && compl_curr_match
->cp_fname
!= NULL
2386 && STRCMP(fname
, compl_curr_match
->cp_fname
) == 0)
2387 match
->cp_fname
= compl_curr_match
->cp_fname
;
2388 else if (fname
!= NULL
)
2390 match
->cp_fname
= vim_strsave(fname
);
2391 flags
|= FREE_FNAME
;
2394 match
->cp_fname
= NULL
;
2395 match
->cp_flags
= flags
;
2401 for (i
= 0; i
< CPT_COUNT
; ++i
)
2402 if (cptext
[i
] != NULL
&& *cptext
[i
] != NUL
)
2403 match
->cp_text
[i
] = vim_strsave(cptext
[i
]);
2407 * Link the new match structure in the list of matches.
2409 if (compl_first_match
== NULL
)
2410 match
->cp_next
= match
->cp_prev
= NULL
;
2411 else if (dir
== FORWARD
)
2413 match
->cp_next
= compl_curr_match
->cp_next
;
2414 match
->cp_prev
= compl_curr_match
;
2418 match
->cp_next
= compl_curr_match
;
2419 match
->cp_prev
= compl_curr_match
->cp_prev
;
2422 match
->cp_next
->cp_prev
= match
;
2424 match
->cp_prev
->cp_next
= match
;
2425 else /* if there's nothing before, it is the first match */
2426 compl_first_match
= match
;
2427 compl_curr_match
= match
;
2430 * Find the longest common string if still doing that.
2432 if (compl_get_longest
&& (flags
& ORIGINAL_TEXT
) == 0)
2433 ins_compl_longest_match(match
);
2439 * Return TRUE if "str[len]" matches with match->cp_str, considering
2443 ins_compl_equal(match
, str
, len
)
2448 if (match
->cp_icase
)
2449 return STRNICMP(match
->cp_str
, str
, (size_t)len
) == 0;
2450 return STRNCMP(match
->cp_str
, str
, (size_t)len
) == 0;
2454 * Reduce the longest common string for match "match".
2457 ins_compl_longest_match(match
)
2464 if (compl_leader
== NULL
)
2466 /* First match, use it as a whole. */
2467 compl_leader
= vim_strsave(match
->cp_str
);
2468 if (compl_leader
!= NULL
)
2470 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2472 ins_bytes(compl_leader
+ ins_compl_len());
2475 /* When the match isn't there (to avoid matching itself) remove it
2476 * again after redrawing. */
2479 compl_used_match
= FALSE
;
2484 /* Reduce the text if this match differs from compl_leader. */
2492 c1
= mb_ptr2char(p
);
2493 c2
= mb_ptr2char(s
);
2501 if (match
->cp_icase
? (MB_TOLOWER(c1
) != MB_TOLOWER(c2
))
2520 /* Leader was shortened, need to change the inserted text. */
2522 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2524 ins_bytes(compl_leader
+ ins_compl_len());
2527 /* When the match isn't there (to avoid matching itself) remove it
2528 * again after redrawing. */
2533 compl_used_match
= FALSE
;
2538 * Add an array of matches to the list of matches.
2542 ins_compl_add_matches(num_matches
, matches
, icase
)
2549 int dir
= compl_direction
;
2551 for (i
= 0; i
< num_matches
&& add_r
!= FAIL
; i
++)
2552 if ((add_r
= ins_compl_add(matches
[i
], -1, icase
,
2553 NULL
, NULL
, dir
, 0, FALSE
)) == OK
)
2554 /* if dir was BACKWARD then honor it just once */
2556 FreeWild(num_matches
, matches
);
2559 /* Make the completion list cyclic.
2560 * Return the number of matches (excluding the original).
2563 ins_compl_make_cyclic()
2568 if (compl_first_match
!= NULL
)
2571 * Find the end of the list.
2573 match
= compl_first_match
;
2574 /* there's always an entry for the compl_orig_text, it doesn't count. */
2575 while (match
->cp_next
!= NULL
&& match
->cp_next
!= compl_first_match
)
2577 match
= match
->cp_next
;
2580 match
->cp_next
= compl_first_match
;
2581 compl_first_match
->cp_prev
= match
;
2587 * Start completion for the complete() function.
2588 * "startcol" is where the matched text starts (1 is first column).
2589 * "list" is the list of matches.
2592 set_completion(startcol
, list
)
2596 /* If already doing completions stop it. */
2597 if (ctrl_x_mode
!= 0)
2598 ins_compl_prep(' ');
2601 if (stop_arrow() == FAIL
)
2604 if (startcol
> curwin
->w_cursor
.col
)
2605 startcol
= curwin
->w_cursor
.col
;
2606 compl_col
= startcol
;
2607 compl_length
= (int)curwin
->w_cursor
.col
- (int)startcol
;
2608 /* compl_pattern doesn't need to be set */
2609 compl_orig_text
= vim_strnsave(ml_get_curline() + compl_col
, compl_length
);
2610 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
2611 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
2614 /* Handle like dictionary completion. */
2615 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
2617 ins_compl_add_list(list
);
2618 compl_matches
= ins_compl_make_cyclic();
2619 compl_started
= TRUE
;
2620 compl_used_match
= TRUE
;
2621 compl_cont_status
= 0;
2623 compl_curr_match
= compl_first_match
;
2624 ins_complete(Ctrl_N
);
2629 /* "compl_match_array" points the currently displayed list of entries in the
2630 * popup menu. It is NULL when there is no popup menu. */
2631 static pumitem_T
*compl_match_array
= NULL
;
2632 static int compl_match_arraysize
;
2635 * Update the screen and when there is any scrolling remove the popup menu.
2642 if (compl_match_array
!= NULL
)
2644 h
= curwin
->w_cline_height
;
2646 if (h
!= curwin
->w_cline_height
)
2647 ins_compl_del_pum();
2652 * Remove any popup menu.
2657 if (compl_match_array
!= NULL
)
2660 vim_free(compl_match_array
);
2661 compl_match_array
= NULL
;
2666 * Return TRUE if the popup menu should be displayed.
2671 /* 'completeopt' must contain "menu" or "menuone" */
2672 if (vim_strchr(p_cot
, 'm') == NULL
)
2675 /* The display looks bad on a B&W display. */
2686 * Return TRUE if there are two or more matches to be shown in the popup menu.
2687 * One if 'completopt' contains "menuone".
2690 pum_enough_matches()
2695 /* Don't display the popup menu if there are no matches or there is only
2696 * one (ignoring the original text). */
2697 compl = compl_first_match
;
2702 || ((compl->cp_flags
& ORIGINAL_TEXT
) == 0 && ++i
== 2))
2704 compl = compl->cp_next
;
2705 } while (compl != compl_first_match
);
2707 if (strstr((char *)p_cot
, "menuone") != NULL
)
2713 * Show the popup menu for the list of matches.
2714 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
2717 ins_compl_show_pum()
2720 compl_T
*shown_compl
= NULL
;
2721 int did_find_shown_match
= FALSE
;
2722 int shown_match_ok
= FALSE
;
2728 if (!pum_wanted() || !pum_enough_matches())
2731 #if defined(FEAT_EVAL)
2732 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2733 do_cmdline_cmd((char_u
*)"if exists('g:loaded_matchparen')|3match none|endif");
2736 /* Update the screen before drawing the popup menu over it. */
2739 if (compl_match_array
== NULL
)
2741 /* Need to build the popup menu list. */
2742 compl_match_arraysize
= 0;
2743 compl = compl_first_match
;
2744 if (compl_leader
!= NULL
)
2745 lead_len
= (int)STRLEN(compl_leader
);
2748 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2749 && (compl_leader
== NULL
2750 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2751 ++compl_match_arraysize
;
2752 compl = compl->cp_next
;
2753 } while (compl != NULL
&& compl != compl_first_match
);
2754 if (compl_match_arraysize
== 0)
2756 compl_match_array
= (pumitem_T
*)alloc_clear(
2757 (unsigned)(sizeof(pumitem_T
)
2758 * compl_match_arraysize
));
2759 if (compl_match_array
!= NULL
)
2761 /* If the current match is the original text don't find the first
2762 * match after it, don't highlight anything. */
2763 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
2764 shown_match_ok
= TRUE
;
2767 compl = compl_first_match
;
2770 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2771 && (compl_leader
== NULL
2772 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2774 if (!shown_match_ok
)
2776 if (compl == compl_shown_match
|| did_find_shown_match
)
2778 /* This item is the shown match or this is the
2779 * first displayed item after the shown match. */
2780 compl_shown_match
= compl;
2781 did_find_shown_match
= TRUE
;
2782 shown_match_ok
= TRUE
;
2785 /* Remember this displayed match for when the
2786 * shown match is just below it. */
2787 shown_compl
= compl;
2791 if (compl->cp_text
[CPT_ABBR
] != NULL
)
2792 compl_match_array
[i
].pum_text
=
2793 compl->cp_text
[CPT_ABBR
];
2795 compl_match_array
[i
].pum_text
= compl->cp_str
;
2796 compl_match_array
[i
].pum_kind
= compl->cp_text
[CPT_KIND
];
2797 compl_match_array
[i
].pum_info
= compl->cp_text
[CPT_INFO
];
2798 if (compl->cp_text
[CPT_MENU
] != NULL
)
2799 compl_match_array
[i
++].pum_extra
=
2800 compl->cp_text
[CPT_MENU
];
2802 compl_match_array
[i
++].pum_extra
= compl->cp_fname
;
2805 if (compl == compl_shown_match
)
2807 did_find_shown_match
= TRUE
;
2809 /* When the original text is the shown match don't set
2810 * compl_shown_match. */
2811 if (compl->cp_flags
& ORIGINAL_TEXT
)
2812 shown_match_ok
= TRUE
;
2814 if (!shown_match_ok
&& shown_compl
!= NULL
)
2816 /* The shown match isn't displayed, set it to the
2817 * previously displayed match. */
2818 compl_shown_match
= shown_compl
;
2819 shown_match_ok
= TRUE
;
2822 compl = compl->cp_next
;
2823 } while (compl != NULL
&& compl != compl_first_match
);
2825 if (!shown_match_ok
) /* no displayed match at all */
2831 /* popup menu already exists, only need to find the current item.*/
2832 for (i
= 0; i
< compl_match_arraysize
; ++i
)
2833 if (compl_match_array
[i
].pum_text
== compl_shown_match
->cp_str
2834 || compl_match_array
[i
].pum_text
2835 == compl_shown_match
->cp_text
[CPT_ABBR
])
2842 if (compl_match_array
!= NULL
)
2844 /* Compute the screen column of the start of the completed text.
2845 * Use the cursor to get all wrapping and other settings right. */
2846 col
= curwin
->w_cursor
.col
;
2847 curwin
->w_cursor
.col
= compl_col
;
2848 pum_display(compl_match_array
, compl_match_arraysize
, cur
);
2849 curwin
->w_cursor
.col
= col
;
2853 #define DICT_FIRST (1) /* use just first element in "dict" */
2854 #define DICT_EXACT (2) /* "dict" is the exact name of a file */
2857 * Add any identifiers that match the given pattern in the list of dictionary
2858 * files "dict_start" to the list of completions.
2861 ins_compl_dictionaries(dict_start
, pat
, flags
, thesaurus
)
2864 int flags
; /* DICT_FIRST and/or DICT_EXACT */
2865 int thesaurus
; /* Thesaurus completion */
2867 char_u
*dict
= dict_start
;
2870 regmatch_T regmatch
;
2874 int dir
= compl_direction
;
2879 /* When 'dictionary' is empty and spell checking is enabled use
2881 if (!thesaurus
&& curwin
->w_p_spell
)
2882 dict
= (char_u
*)"spell";
2891 regmatch
.regprog
= NULL
; /* so that we can goto theend */
2893 /* If 'infercase' is set, don't use 'smartcase' here */
2895 if (curbuf
->b_p_inf
)
2898 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2899 * to only match at the start of a line. Otherwise just match the
2900 * pattern. Also need to double backslashes. */
2901 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2903 char_u
*pat_esc
= vim_strsave_escaped(pat
, (char_u
*)"\\");
2906 if (pat_esc
== NULL
)
2908 len
= STRLEN(pat_esc
) + 10;
2909 ptr
= alloc((unsigned)len
);
2915 vim_snprintf((char *)ptr
, len
, "^\\s*\\zs\\V%s", pat_esc
);
2916 regmatch
.regprog
= vim_regcomp(ptr
, RE_MAGIC
);
2922 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
2923 if (regmatch
.regprog
== NULL
)
2927 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2928 regmatch
.rm_ic
= ignorecase(pat
);
2929 while (*dict
!= NUL
&& !got_int
&& !compl_interrupted
)
2931 /* copy one dictionary file name into buf */
2932 if (flags
== DICT_EXACT
)
2939 /* Expand wildcards in the dictionary name, but do not allow
2940 * backticks (for security, the 'dict' option may have been set in
2942 copy_option_part(&dict
, buf
, LSIZE
, ",");
2944 if (!thesaurus
&& STRCMP(buf
, "spell") == 0)
2948 if (vim_strchr(buf
, '`') != NULL
2949 || expand_wildcards(1, &buf
, &count
, &files
,
2950 EW_FILE
|EW_SILENT
) != OK
)
2957 /* Complete from active spelling. Skip "\<" in the pattern, we
2958 * don't use it as a RE. */
2959 if (pat
[0] == '\\' && pat
[1] == '<')
2963 spell_dump_compl(curbuf
, ptr
, regmatch
.rm_ic
, &dir
, 0);
2967 if (count
> 0) /* avoid warning for using "files" uninit */
2969 ins_compl_files(count
, files
, thesaurus
, flags
,
2970 ®match
, buf
, &dir
);
2971 if (flags
!= DICT_EXACT
)
2972 FreeWild(count
, files
);
2980 vim_free(regmatch
.regprog
);
2985 ins_compl_files(count
, files
, thesaurus
, flags
, regmatch
, buf
, dir
)
2990 regmatch_T
*regmatch
;
2999 for (i
= 0; i
< count
&& !got_int
&& !compl_interrupted
; i
++)
3001 fp
= mch_fopen((char *)files
[i
], "r"); /* open dictionary file */
3002 if (flags
!= DICT_EXACT
)
3004 vim_snprintf((char *)IObuff
, IOSIZE
,
3005 _("Scanning dictionary: %s"), (char *)files
[i
]);
3006 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3012 * Read dictionary file line by line.
3013 * Check each line for a match.
3015 while (!got_int
&& !compl_interrupted
3016 && !vim_fgets(buf
, LSIZE
, fp
))
3019 while (vim_regexec(regmatch
, buf
, (colnr_T
)(ptr
- buf
)))
3021 ptr
= regmatch
->startp
[0];
3022 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3023 ptr
= find_line_end(ptr
);
3025 ptr
= find_word_end(ptr
);
3026 add_r
= ins_compl_add_infercase(regmatch
->startp
[0],
3027 (int)(ptr
- regmatch
->startp
[0]),
3028 p_ic
, files
[i
], *dir
, 0);
3034 * Add the other matches on the line
3039 /* Find start of the next word. Skip white
3040 * space and punctuation. */
3041 ptr
= find_word_start(ptr
);
3042 if (*ptr
== NUL
|| *ptr
== NL
)
3046 /* Find end of the word. */
3049 /* Japanese words may have characters in
3050 * different classes, only separate words
3051 * with single-byte non-word characters. */
3054 int l
= (*mb_ptr2len
)(ptr
);
3056 if (l
< 2 && !vim_iswordc(*ptr
))
3062 ptr
= find_word_end(ptr
);
3064 /* Add the word. Skip the regexp match. */
3065 if (wstart
!= regmatch
->startp
[0])
3066 add_r
= ins_compl_add_infercase(wstart
,
3067 (int)(ptr
- wstart
),
3068 p_ic
, files
[i
], *dir
, 0);
3072 /* if dir was BACKWARD then honor it just once */
3074 else if (add_r
== FAIL
)
3076 /* avoid expensive call to vim_regexec() when at end
3078 if (*ptr
== '\n' || got_int
)
3082 ins_compl_check_keys(50);
3090 * Find the start of the next word.
3091 * Returns a pointer to the first char of the word. Also stops at a NUL.
3094 find_word_start(ptr
)
3099 while (*ptr
!= NUL
&& *ptr
!= '\n' && mb_get_class(ptr
) <= 1)
3100 ptr
+= (*mb_ptr2len
)(ptr
);
3103 while (*ptr
!= NUL
&& *ptr
!= '\n' && !vim_iswordc(*ptr
))
3109 * Find the end of the word. Assumes it starts inside a word.
3110 * Returns a pointer to just after the word.
3121 start_class
= mb_get_class(ptr
);
3122 if (start_class
> 1)
3125 ptr
+= (*mb_ptr2len
)(ptr
);
3126 if (mb_get_class(ptr
) != start_class
)
3132 while (vim_iswordc(*ptr
))
3138 * Find the end of the line, omitting CR and NL at the end.
3139 * Returns a pointer to just after the line.
3147 s
= ptr
+ STRLEN(ptr
);
3148 while (s
> ptr
&& (s
[-1] == CAR
|| s
[-1] == NL
))
3154 * Free the list of completions
3162 vim_free(compl_pattern
);
3163 compl_pattern
= NULL
;
3164 vim_free(compl_leader
);
3165 compl_leader
= NULL
;
3167 if (compl_first_match
== NULL
)
3170 ins_compl_del_pum();
3173 compl_curr_match
= compl_first_match
;
3176 match
= compl_curr_match
;
3177 compl_curr_match
= compl_curr_match
->cp_next
;
3178 vim_free(match
->cp_str
);
3179 /* several entries may use the same fname, free it just once. */
3180 if (match
->cp_flags
& FREE_FNAME
)
3181 vim_free(match
->cp_fname
);
3182 for (i
= 0; i
< CPT_COUNT
; ++i
)
3183 vim_free(match
->cp_text
[i
]);
3185 } while (compl_curr_match
!= NULL
&& compl_curr_match
!= compl_first_match
);
3186 compl_first_match
= compl_curr_match
= NULL
;
3187 compl_shown_match
= NULL
;
3193 compl_cont_status
= 0;
3194 compl_started
= FALSE
;
3196 vim_free(compl_pattern
);
3197 compl_pattern
= NULL
;
3198 vim_free(compl_leader
);
3199 compl_leader
= NULL
;
3200 edit_submode_extra
= NULL
;
3201 vim_free(compl_orig_text
);
3202 compl_orig_text
= NULL
;
3203 compl_enter_selects
= FALSE
;
3207 * Return TRUE when Insert completion is active.
3212 return compl_started
;
3216 * Delete one character before the cursor and show the subset of the matches
3217 * that match the word that is now before the cursor.
3218 * Returns the character to be used, NUL if the work is done and another char
3219 * to be got from the user.
3227 line
= ml_get_curline();
3228 p
= line
+ curwin
->w_cursor
.col
;
3229 mb_ptr_back(line
, p
);
3231 /* Stop completion when the whole word was deleted. For Omni completion
3232 * allow the word to be deleted, we won't match everything. */
3233 if ((int)(p
- line
) - (int)compl_col
< 0
3234 || ((int)(p
- line
) - (int)compl_col
== 0
3235 && (ctrl_x_mode
& CTRL_X_OMNI
) == 0))
3238 /* Deleted more than what was used to find matches or didn't finish
3239 * finding all matches: need to look for matches all over again. */
3240 if (curwin
->w_cursor
.col
<= compl_col
+ compl_length
3241 || compl_was_interrupted
)
3242 ins_compl_restart();
3244 vim_free(compl_leader
);
3245 compl_leader
= vim_strnsave(line
+ compl_col
, (int)(p
- line
) - compl_col
);
3246 if (compl_leader
!= NULL
)
3248 ins_compl_new_leader();
3255 * Called after changing "compl_leader".
3256 * Show the popup menu with a different set of matches.
3257 * May also search for matches again if the previous search was interrupted.
3260 ins_compl_new_leader()
3262 ins_compl_del_pum();
3264 ins_bytes(compl_leader
+ ins_compl_len());
3265 compl_used_match
= FALSE
;
3268 ins_compl_set_original_text(compl_leader
);
3272 spell_bad_len
= 0; /* need to redetect bad word */
3275 * Matches were cleared, need to search for them now. First display
3276 * the changed text before the cursor. Set "compl_restarting" to
3277 * avoid that the first match is inserted.
3283 /* Show the cursor after the match, not after the redrawn text. */
3286 gui_update_cursor(FALSE
, FALSE
);
3289 compl_restarting
= TRUE
;
3290 if (ins_complete(Ctrl_N
) == FAIL
)
3291 compl_cont_status
= 0;
3292 compl_restarting
= FALSE
;
3295 #if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
3296 if (!compl_used_match
)
3298 /* Go to the original text, since none of the matches is inserted. */
3299 if (compl_first_match
->cp_prev
!= NULL
3300 && (compl_first_match
->cp_prev
->cp_flags
& ORIGINAL_TEXT
))
3301 compl_shown_match
= compl_first_match
->cp_prev
;
3303 compl_shown_match
= compl_first_match
;
3304 compl_curr_match
= compl_shown_match
;
3305 compl_shows_dir
= compl_direction
;
3308 compl_enter_selects
= !compl_used_match
;
3310 /* Show the popup menu with a different set of matches. */
3311 ins_compl_show_pum();
3313 /* Don't let Enter select the original text when there is no popup menu. */
3314 if (compl_match_array
== NULL
)
3315 compl_enter_selects
= FALSE
;
3319 * Return the length of the completion, from the completion start column to
3320 * the cursor column. Making sure it never goes below zero.
3325 int off
= (int)curwin
->w_cursor
.col
- (int)compl_col
;
3333 * Append one character to the match leader. May reduce the number of
3337 ins_compl_addleader(c
)
3343 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
3345 char_u buf
[MB_MAXBYTES
+ 1];
3347 (*mb_char2bytes
)(c
, buf
);
3349 ins_char_bytes(buf
, cc
);
3355 /* If we didn't complete finding matches we must search again. */
3356 if (compl_was_interrupted
)
3357 ins_compl_restart();
3359 vim_free(compl_leader
);
3360 compl_leader
= vim_strnsave(ml_get_curline() + compl_col
,
3361 (int)(curwin
->w_cursor
.col
- compl_col
));
3362 if (compl_leader
!= NULL
)
3363 ins_compl_new_leader();
3367 * Setup for finding completions again without leaving CTRL-X mode. Used when
3368 * BS or a key was typed while still searching for matches.
3374 compl_started
= FALSE
;
3376 compl_cont_status
= 0;
3377 compl_cont_mode
= 0;
3381 * Set the first match, the original text.
3384 ins_compl_set_original_text(str
)
3389 /* Replace the original text entry. */
3390 if (compl_first_match
->cp_flags
& ORIGINAL_TEXT
) /* safety check */
3392 p
= vim_strsave(str
);
3395 vim_free(compl_first_match
->cp_str
);
3396 compl_first_match
->cp_str
= p
;
3402 * Append one character to the match leader. May reduce the number of
3406 ins_compl_addfrommatch()
3409 int len
= (int)curwin
->w_cursor
.col
- (int)compl_col
;
3413 p
= compl_shown_match
->cp_str
;
3414 if ((int)STRLEN(p
) <= len
) /* the match is too short */
3416 /* When still at the original match use the first entry that matches
3418 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
3421 for (cp
= compl_shown_match
->cp_next
; cp
!= NULL
3422 && cp
!= compl_first_match
; cp
= cp
->cp_next
)
3424 if (compl_leader
== NULL
3425 || ins_compl_equal(cp
, compl_leader
,
3426 (int)STRLEN(compl_leader
)))
3432 if (p
== NULL
|| (int)STRLEN(p
) <= len
)
3444 ins_compl_addleader(c
);
3448 * Prepare for Insert mode completion, or stop it.
3449 * Called just after typing a character in Insert mode.
3450 * Returns TRUE when the character is not to be inserted;
3460 /* Forget any previous 'special' messages if this is actually
3461 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3463 if (c
!= Ctrl_R
&& vim_is_ctrl_x_key(c
))
3464 edit_submode_extra
= NULL
;
3466 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3467 if (c
== K_SELECT
|| c
== K_MOUSEDOWN
|| c
== K_MOUSEUP
)
3470 /* Set "compl_get_longest" when finding the first matches. */
3471 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
3472 || (ctrl_x_mode
== 0 && !compl_started
))
3474 compl_get_longest
= (vim_strchr(p_cot
, 'l') != NULL
);
3475 compl_used_match
= TRUE
;
3478 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
)
3481 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3482 * it will be yet. Now we decide.
3488 ctrl_x_mode
= CTRL_X_SCROLL
;
3489 if (!(State
& REPLACE_FLAG
))
3490 edit_submode
= (char_u
*)_(" (insert) Scroll (^E/^Y)");
3492 edit_submode
= (char_u
*)_(" (replace) Scroll (^E/^Y)");
3493 edit_submode_pre
= NULL
;
3497 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
3500 ctrl_x_mode
= CTRL_X_FILES
;
3503 ctrl_x_mode
= CTRL_X_DICTIONARY
;
3506 /* Simply allow ^R to happen without affecting ^X mode */
3509 ctrl_x_mode
= CTRL_X_THESAURUS
;
3511 #ifdef FEAT_COMPL_FUNC
3513 ctrl_x_mode
= CTRL_X_FUNCTION
;
3516 ctrl_x_mode
= CTRL_X_OMNI
;
3521 ctrl_x_mode
= CTRL_X_SPELL
;
3523 ++emsg_off
; /* Avoid getting the E756 error twice. */
3524 spell_back_to_badword();
3529 ctrl_x_mode
= CTRL_X_TAGS
;
3534 ctrl_x_mode
= CTRL_X_PATH_PATTERNS
;
3537 ctrl_x_mode
= CTRL_X_PATH_DEFINES
;
3542 ctrl_x_mode
= CTRL_X_CMDLINE
;
3546 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3547 * just started ^X mode, or there were enough ^X's to cancel
3548 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3549 * do normal expansion when interrupting a different mode (say
3550 * ^X^F^X^P or ^P^X^X^P, see below)
3551 * nothing changes if interrupting mode 0, (eg, the flag
3552 * doesn't change when going to ADDING mode -- Acevedo */
3553 if (!(compl_cont_status
& CONT_INTRPT
))
3554 compl_cont_status
|= CONT_LOCAL
;
3555 else if (compl_cont_mode
!= 0)
3556 compl_cont_status
&= ~CONT_LOCAL
;
3559 /* If we have typed at least 2 ^X's... for modes != 0, we set
3560 * compl_cont_status = 0 (eg, as if we had just started ^X
3562 * For mode 0, we set "compl_cont_mode" to an impossible
3563 * value, in both cases ^X^X can be used to restart the same
3564 * mode (avoiding ADDING mode).
3565 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3566 * 'complete' and local ^P expansions respectively.
3567 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3568 * mode -- Acevedo */
3571 if (compl_cont_mode
!= 0)
3572 compl_cont_status
= 0;
3574 compl_cont_mode
= CTRL_X_NOT_DEFINED_YET
;
3577 edit_submode
= NULL
;
3582 else if (ctrl_x_mode
!= 0)
3584 /* We're already in CTRL-X mode, do we stay in it? */
3585 if (!vim_is_ctrl_x_key(c
))
3587 if (ctrl_x_mode
== CTRL_X_SCROLL
)
3590 ctrl_x_mode
= CTRL_X_FINISHED
;
3591 edit_submode
= NULL
;
3596 if (compl_started
|| ctrl_x_mode
== CTRL_X_FINISHED
)
3598 /* Show error message from attempted keyword completion (probably
3599 * 'Pattern not found') until another key is hit, then go back to
3600 * showing what mode we are in. */
3602 if ((ctrl_x_mode
== 0 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_R
3603 && !ins_compl_pum_key(c
))
3604 || ctrl_x_mode
== CTRL_X_FINISHED
)
3606 /* Get here when we have finished typing a sequence of ^N and
3607 * ^P or other completion characters in CTRL-X mode. Free up
3608 * memory that was used, and make sure we can redo the insert. */
3609 if (compl_curr_match
!= NULL
|| compl_leader
!= NULL
|| c
== Ctrl_E
)
3615 * If any of the original typed text has been changed, eg when
3616 * ignorecase is set, we must add back-spaces to the redo
3617 * buffer. We add as few as necessary to delete just the part
3618 * of the original text that has changed.
3619 * When using the longest match, edited the match or used
3620 * CTRL-E then don't use the current match.
3622 if (compl_curr_match
!= NULL
&& compl_used_match
&& c
!= Ctrl_E
)
3623 ptr
= compl_curr_match
->cp_str
;
3624 else if (compl_leader
!= NULL
)
3627 ptr
= compl_orig_text
;
3628 if (compl_orig_text
!= NULL
)
3630 p
= compl_orig_text
;
3631 for (temp
= 0; p
[temp
] != NUL
&& p
[temp
] == ptr
[temp
];
3636 temp
-= (*mb_head_off
)(compl_orig_text
, p
+ temp
);
3638 for (p
+= temp
; *p
!= NUL
; mb_ptr_adv(p
))
3639 AppendCharToRedobuff(K_BS
);
3642 AppendToRedobuffLit(ptr
+ temp
, -1);
3646 want_cindent
= (can_cindent
&& cindent_on());
3649 * When completing whole lines: fix indent for 'cindent'.
3650 * Otherwise, break line if it's too long.
3652 if (compl_cont_mode
== CTRL_X_WHOLE_LINE
)
3655 /* re-indent the current line */
3659 want_cindent
= FALSE
; /* don't do it again */
3665 int prev_col
= curwin
->w_cursor
.col
;
3667 /* put the cursor on the last char, for 'tw' formatting */
3670 if (stop_arrow() == OK
)
3671 insertchar(NUL
, 0, -1);
3673 && ml_get_curline()[curwin
->w_cursor
.col
] != NUL
)
3677 /* If the popup menu is displayed pressing CTRL-Y means accepting
3678 * the selection without inserting anything. When
3679 * compl_enter_selects is set the Enter key does the same. */
3680 if ((c
== Ctrl_Y
|| (compl_enter_selects
3681 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
3685 /* CTRL-E means completion is Ended, go back to the typed text. */
3689 if (compl_leader
!= NULL
)
3690 ins_bytes(compl_leader
+ ins_compl_len());
3691 else if (compl_first_match
!= NULL
)
3692 ins_bytes(compl_orig_text
+ ins_compl_len());
3696 auto_format(FALSE
, TRUE
);
3699 compl_started
= FALSE
;
3701 msg_clr_cmdline(); /* necessary for "noshowmode" */
3703 compl_enter_selects
= FALSE
;
3704 if (edit_submode
!= NULL
)
3706 edit_submode
= NULL
;
3712 * Indent now if a key was typed that is in 'cinkeys'.
3714 if (want_cindent
&& in_cinkeys(KEY_COMPLETE
, ' ', inindent(0)))
3720 /* reset continue_* if we left expansion-mode, if we stay they'll be
3721 * (re)set properly in ins_complete() */
3722 if (!vim_is_ctrl_x_key(c
))
3724 compl_cont_status
= 0;
3725 compl_cont_mode
= 0;
3732 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3733 * (depending on flag) starting from buf and looking for a non-scanned
3734 * buffer (other than curbuf). curbuf is special, if it is called with
3735 * buf=curbuf then it has to be the first call for a given flag/expansion.
3737 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3740 ins_compl_next_buf(buf
, flag
)
3748 if (flag
== 'w') /* just windows */
3751 if (buf
== curbuf
) /* first call for this flag/expansion */
3753 while ((wp
= (wp
->w_next
!= NULL
? wp
->w_next
: firstwin
)) != curwin
3754 && wp
->w_buffer
->b_scanned
)
3762 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3763 * (unlisted buffers)
3764 * When completing whole lines skip unloaded buffers. */
3765 while ((buf
= (buf
->b_next
!= NULL
? buf
->b_next
: firstbuf
)) != curbuf
3769 || (buf
->b_ml
.ml_mfp
== NULL
) != (flag
== 'u')))
3775 #ifdef FEAT_COMPL_FUNC
3776 static void expand_by_function
__ARGS((int type
, char_u
*base
));
3779 * Execute user defined complete function 'completefunc' or 'omnifunc', and
3780 * get matches in "matches".
3783 expand_by_function(type
, base
)
3784 int type
; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
3792 funcname
= (type
== CTRL_X_FUNCTION
) ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
3793 if (*funcname
== NUL
)
3796 /* Call 'completefunc' to obtain the list of matches. */
3797 args
[0] = (char_u
*)"0";
3800 pos
= curwin
->w_cursor
;
3801 matchlist
= call_func_retlist(funcname
, 2, args
, FALSE
);
3802 curwin
->w_cursor
= pos
; /* restore the cursor position */
3803 if (matchlist
== NULL
)
3806 ins_compl_add_list(matchlist
);
3807 list_unref(matchlist
);
3809 #endif /* FEAT_COMPL_FUNC */
3811 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
3813 * Add completions from a list.
3816 ins_compl_add_list(list
)
3820 int dir
= compl_direction
;
3822 /* Go through the List with matches and add each of them. */
3823 for (li
= list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3825 if (ins_compl_add_tv(&li
->li_tv
, dir
) == OK
)
3826 /* if dir was BACKWARD then honor it just once */
3834 * Add a match to the list of matches from a typeval_T.
3835 * If the given string is already in the list of completions, then return
3836 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3837 * maybe because alloc() returns NULL, then FAIL is returned.
3840 ins_compl_add_tv(tv
, dir
)
3847 char_u
*(cptext
[CPT_COUNT
]);
3849 if (tv
->v_type
== VAR_DICT
&& tv
->vval
.v_dict
!= NULL
)
3851 word
= get_dict_string(tv
->vval
.v_dict
, (char_u
*)"word", FALSE
);
3852 cptext
[CPT_ABBR
] = get_dict_string(tv
->vval
.v_dict
,
3853 (char_u
*)"abbr", FALSE
);
3854 cptext
[CPT_MENU
] = get_dict_string(tv
->vval
.v_dict
,
3855 (char_u
*)"menu", FALSE
);
3856 cptext
[CPT_KIND
] = get_dict_string(tv
->vval
.v_dict
,
3857 (char_u
*)"kind", FALSE
);
3858 cptext
[CPT_INFO
] = get_dict_string(tv
->vval
.v_dict
,
3859 (char_u
*)"info", FALSE
);
3860 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"icase", FALSE
) != NULL
)
3861 icase
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"icase");
3862 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"dup", FALSE
) != NULL
)
3863 adup
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"dup");
3867 word
= get_tv_string_chk(tv
);
3868 vim_memset(cptext
, 0, sizeof(cptext
));
3870 if (word
== NULL
|| *word
== NUL
)
3872 return ins_compl_add(word
, -1, icase
, NULL
, cptext
, dir
, 0, adup
);
3877 * Get the next expansion(s), using "compl_pattern".
3878 * The search starts at position "ini" in curbuf and in the direction
3880 * When "compl_started" is FALSE start at that position, otherwise continue
3881 * where we stopped searching before.
3882 * This may return before finding all the matches.
3883 * Return the total number of matches or -1 if still unknown -- Acevedo
3886 ins_compl_get_exp(ini
)
3889 static pos_T first_match_pos
;
3890 static pos_T last_match_pos
;
3891 static char_u
*e_cpt
= (char_u
*)""; /* curr. entry in 'complete' */
3892 static int found_all
= FALSE
; /* Found all matches of a
3894 static buf_T
*ins_buf
= NULL
; /* buffer being scanned */
3904 int found_new_match
;
3905 int type
= ctrl_x_mode
;
3907 char_u
*dict
= NULL
;
3913 for (ins_buf
= firstbuf
; ins_buf
!= NULL
; ins_buf
= ins_buf
->b_next
)
3914 ins_buf
->b_scanned
= 0;
3917 e_cpt
= (compl_cont_status
& CONT_LOCAL
)
3918 ? (char_u
*)"." : curbuf
->b_p_cpt
;
3919 last_match_pos
= first_match_pos
= *ini
;
3922 old_match
= compl_curr_match
; /* remember the last current match */
3923 pos
= (compl_direction
== FORWARD
) ? &last_match_pos
: &first_match_pos
;
3924 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3927 found_new_match
= FAIL
;
3929 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
3930 * or if found_all says this entry is done. For ^X^L only use the
3931 * entries from 'complete' that look in loaded buffers. */
3932 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3933 && (!compl_started
|| found_all
))
3936 while (*e_cpt
== ',' || *e_cpt
== ' ')
3938 if (*e_cpt
== '.' && !curbuf
->b_scanned
)
3941 first_match_pos
= *ini
;
3942 /* So that ^N can match word immediately after cursor */
3943 if (ctrl_x_mode
== 0)
3944 dec(&first_match_pos
);
3945 last_match_pos
= first_match_pos
;
3948 else if (vim_strchr((char_u
*)"buwU", *e_cpt
) != NULL
3949 && (ins_buf
= ins_compl_next_buf(ins_buf
, *e_cpt
)) != curbuf
)
3951 /* Scan a buffer, but not the current one. */
3952 if (ins_buf
->b_ml
.ml_mfp
!= NULL
) /* loaded buffer */
3954 compl_started
= TRUE
;
3955 first_match_pos
.col
= last_match_pos
.col
= 0;
3956 first_match_pos
.lnum
= ins_buf
->b_ml
.ml_line_count
+ 1;
3957 last_match_pos
.lnum
= 0;
3960 else /* unloaded buffer, scan like dictionary */
3963 if (ins_buf
->b_fname
== NULL
)
3965 type
= CTRL_X_DICTIONARY
;
3966 dict
= ins_buf
->b_fname
;
3967 dict_f
= DICT_EXACT
;
3969 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning: %s"),
3970 ins_buf
->b_fname
== NULL
3971 ? buf_spname(ins_buf
)
3972 : ins_buf
->b_sfname
== NULL
3973 ? (char *)ins_buf
->b_fname
3974 : (char *)ins_buf
->b_sfname
);
3975 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3977 else if (*e_cpt
== NUL
)
3981 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3983 else if (*e_cpt
== 'k' || *e_cpt
== 's')
3986 type
= CTRL_X_DICTIONARY
;
3988 type
= CTRL_X_THESAURUS
;
3989 if (*++e_cpt
!= ',' && *e_cpt
!= NUL
)
3992 dict_f
= DICT_FIRST
;
3996 else if (*e_cpt
== 'i')
3997 type
= CTRL_X_PATH_PATTERNS
;
3998 else if (*e_cpt
== 'd')
3999 type
= CTRL_X_PATH_DEFINES
;
4001 else if (*e_cpt
== ']' || *e_cpt
== 't')
4004 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning tags."));
4005 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
4010 /* in any case e_cpt is advanced to the next entry */
4011 (void)copy_option_part(&e_cpt
, IObuff
, IOSIZE
, ",");
4024 case CTRL_X_PATH_PATTERNS
:
4025 case CTRL_X_PATH_DEFINES
:
4026 find_pattern_in_path(compl_pattern
, compl_direction
,
4027 (int)STRLEN(compl_pattern
), FALSE
, FALSE
,
4028 (type
== CTRL_X_PATH_DEFINES
4029 && !(compl_cont_status
& CONT_SOL
))
4030 ? FIND_DEFINE
: FIND_ANY
, 1L, ACTION_EXPAND
,
4031 (linenr_T
)1, (linenr_T
)MAXLNUM
);
4035 case CTRL_X_DICTIONARY
:
4036 case CTRL_X_THESAURUS
:
4037 ins_compl_dictionaries(
4039 : (type
== CTRL_X_THESAURUS
4040 ? (*curbuf
->b_p_tsr
== NUL
4043 : (*curbuf
->b_p_dict
== NUL
4045 : curbuf
->b_p_dict
)),
4047 dict
!= NULL
? dict_f
4048 : 0, type
== CTRL_X_THESAURUS
);
4053 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4055 p_ic
= ignorecase(compl_pattern
);
4057 /* Find up to TAG_MANY matches. Avoids that an enormous number
4058 * of matches is found when compl_pattern is empty */
4059 if (find_tags(compl_pattern
, &num_matches
, &matches
,
4060 TAG_REGEXP
| TAG_NAMES
| TAG_NOIC
|
4061 TAG_INS_COMP
| (ctrl_x_mode
? TAG_VERBOSE
: 0),
4062 TAG_MANY
, curbuf
->b_ffname
) == OK
&& num_matches
> 0)
4064 ins_compl_add_matches(num_matches
, matches
, p_ic
);
4070 if (expand_wildcards(1, &compl_pattern
, &num_matches
, &matches
,
4071 EW_FILE
|EW_DIR
|EW_ADDSLASH
|EW_SILENT
) == OK
)
4074 /* May change home directory back to "~". */
4075 tilde_replace(compl_pattern
, num_matches
, matches
);
4076 ins_compl_add_matches(num_matches
, matches
,
4077 #ifdef CASE_INSENSITIVE_FILENAME
4086 case CTRL_X_CMDLINE
:
4087 if (expand_cmdline(&compl_xp
, compl_pattern
,
4088 (int)STRLEN(compl_pattern
),
4089 &num_matches
, &matches
) == EXPAND_OK
)
4090 ins_compl_add_matches(num_matches
, matches
, FALSE
);
4093 #ifdef FEAT_COMPL_FUNC
4094 case CTRL_X_FUNCTION
:
4096 expand_by_function(type
, compl_pattern
);
4102 num_matches
= expand_spelling(first_match_pos
.lnum
,
4103 compl_pattern
, &matches
);
4104 if (num_matches
> 0)
4105 ins_compl_add_matches(num_matches
, matches
, p_ic
);
4109 default: /* normal ^P/^N and ^X^L */
4111 * If 'infercase' is set, don't use 'smartcase' here
4114 if (ins_buf
->b_p_inf
)
4117 /* buffers other than curbuf are scanned from the beginning or the
4118 * end but never from the middle, thus setting nowrapscan in this
4119 * buffers is a good idea, on the other hand, we always set
4120 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4122 if (ins_buf
!= curbuf
)
4124 else if (*e_cpt
== '.')
4130 ++msg_silent
; /* Don't want messages for wrapscan. */
4132 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4133 * has added a word that was at the beginning of the line */
4134 if ( ctrl_x_mode
== CTRL_X_WHOLE_LINE
4135 || (compl_cont_status
& CONT_SOL
))
4136 found_new_match
= search_for_exact_line(ins_buf
, pos
,
4137 compl_direction
, compl_pattern
);
4139 found_new_match
= searchit(NULL
, ins_buf
, pos
,
4141 compl_pattern
, 1L, SEARCH_KEEP
+ SEARCH_NFMSG
,
4142 RE_LAST
, (linenr_T
)0, NULL
);
4146 /* set "compl_started" even on fail */
4147 compl_started
= TRUE
;
4148 first_match_pos
= *pos
;
4149 last_match_pos
= *pos
;
4151 else if (first_match_pos
.lnum
== last_match_pos
.lnum
4152 && first_match_pos
.col
== last_match_pos
.col
)
4153 found_new_match
= FAIL
;
4154 if (found_new_match
== FAIL
)
4156 if (ins_buf
== curbuf
)
4161 /* when ADDING, the text before the cursor matches, skip it */
4162 if ( (compl_cont_status
& CONT_ADDING
) && ins_buf
== curbuf
4163 && ini
->lnum
== pos
->lnum
4164 && ini
->col
== pos
->col
)
4166 ptr
= ml_get_buf(ins_buf
, pos
->lnum
, FALSE
) + pos
->col
;
4167 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4169 if (compl_cont_status
& CONT_ADDING
)
4171 if (pos
->lnum
>= ins_buf
->b_ml
.ml_line_count
)
4173 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
4175 ptr
= skipwhite(ptr
);
4177 len
= (int)STRLEN(ptr
);
4181 char_u
*tmp_ptr
= ptr
;
4183 if (compl_cont_status
& CONT_ADDING
)
4185 tmp_ptr
+= compl_length
;
4186 /* Skip if already inside a word. */
4187 if (vim_iswordp(tmp_ptr
))
4189 /* Find start of next word. */
4190 tmp_ptr
= find_word_start(tmp_ptr
);
4192 /* Find end of this word. */
4193 tmp_ptr
= find_word_end(tmp_ptr
);
4194 len
= (int)(tmp_ptr
- ptr
);
4196 if ((compl_cont_status
& CONT_ADDING
)
4197 && len
== compl_length
)
4199 if (pos
->lnum
< ins_buf
->b_ml
.ml_line_count
)
4201 /* Try next line, if any. the new word will be
4202 * "join" as if the normal command "J" was used.
4203 * IOSIZE is always greater than
4204 * compl_length, so the next STRNCPY always
4205 * works -- Acevedo */
4206 STRNCPY(IObuff
, ptr
, len
);
4207 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
4208 tmp_ptr
= ptr
= skipwhite(ptr
);
4209 /* Find start of next word. */
4210 tmp_ptr
= find_word_start(tmp_ptr
);
4211 /* Find end of next word. */
4212 tmp_ptr
= find_word_end(tmp_ptr
);
4215 if (*ptr
!= ')' && IObuff
[len
- 1] != TAB
)
4217 if (IObuff
[len
- 1] != ' ')
4218 IObuff
[len
++] = ' ';
4219 /* IObuf =~ "\k.* ", thus len >= 2 */
4221 && (IObuff
[len
- 2] == '.'
4222 || (vim_strchr(p_cpo
, CPO_JOINSP
)
4224 && (IObuff
[len
- 2] == '?'
4225 || IObuff
[len
- 2] == '!'))))
4226 IObuff
[len
++] = ' ';
4228 /* copy as much as possible of the new word */
4229 if (tmp_ptr
- ptr
>= IOSIZE
- len
)
4230 tmp_ptr
= ptr
+ IOSIZE
- len
- 1;
4231 STRNCPY(IObuff
+ len
, ptr
, tmp_ptr
- ptr
);
4232 len
+= (int)(tmp_ptr
- ptr
);
4233 flags
|= CONT_S_IPOS
;
4238 if (len
== compl_length
)
4242 if (ins_compl_add_infercase(ptr
, len
, p_ic
,
4243 ins_buf
== curbuf
? NULL
: ins_buf
->b_sfname
,
4244 0, flags
) != NOTDONE
)
4246 found_new_match
= OK
;
4254 /* check if compl_curr_match has changed, (e.g. other type of
4255 * expansion added something) */
4256 if (type
!= 0 && compl_curr_match
!= old_match
)
4257 found_new_match
= OK
;
4259 /* break the loop for specialized modes (use 'complete' just for the
4260 * generic ctrl_x_mode == 0) or when we've found a new match */
4261 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4262 || found_new_match
!= FAIL
)
4266 /* Fill the popup menu as soon as possible. */
4268 ins_compl_check_keys(0);
4270 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4271 || compl_interrupted
)
4273 compl_started
= TRUE
;
4277 /* Mark a buffer scanned when it has been scanned completely */
4278 if (type
== 0 || type
== CTRL_X_PATH_PATTERNS
)
4279 ins_buf
->b_scanned
= TRUE
;
4281 compl_started
= FALSE
;
4284 compl_started
= TRUE
;
4286 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4287 && *e_cpt
== NUL
) /* Got to end of 'complete' */
4288 found_new_match
= FAIL
;
4290 i
= -1; /* total of matches, unknown */
4291 if (found_new_match
== FAIL
4292 || (ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
))
4293 i
= ins_compl_make_cyclic();
4295 /* If several matches were added (FORWARD) or the search failed and has
4296 * just been made cyclic then we have to move compl_curr_match to the next
4297 * or previous entry (if any) -- Acevedo */
4298 compl_curr_match
= compl_direction
== FORWARD
? old_match
->cp_next
4299 : old_match
->cp_prev
;
4300 if (compl_curr_match
== NULL
)
4301 compl_curr_match
= old_match
;
4305 /* Delete the old text being completed. */
4312 * In insert mode: Delete the typed part.
4313 * In replace mode: Put the old characters back, if any.
4315 i
= compl_col
+ (compl_cont_status
& CONT_ADDING
? compl_length
: 0);
4316 backspace_until_column(i
);
4317 changed_cline_bef_curs();
4320 /* Insert the new text being completed. */
4324 ins_bytes(compl_shown_match
->cp_str
+ ins_compl_len());
4325 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
4326 compl_used_match
= FALSE
;
4328 compl_used_match
= TRUE
;
4332 * Fill in the next completion in the current direction.
4333 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4334 * get more completions. If it is FALSE, then we just do nothing when there
4335 * are no more completions in a given direction. The latter case is used when
4336 * we are still in the middle of finding completions, to allow browsing
4337 * through the ones found so far.
4338 * Return the total number of matches, or -1 if still unknown -- webb.
4340 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4341 * compl_shown_match here.
4343 * Note that this function may be called recursively once only. First with
4344 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4345 * calls this function with "allow_get_expansion" FALSE.
4348 ins_compl_next(allow_get_expansion
, count
, insert_match
)
4349 int allow_get_expansion
;
4350 int count
; /* repeat completion this many times; should
4352 int insert_match
; /* Insert the newly selected match */
4354 int num_matches
= -1;
4357 compl_T
*found_compl
= NULL
;
4358 int found_end
= FALSE
;
4361 if (compl_leader
!= NULL
4362 && (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0)
4364 /* Set "compl_shown_match" to the actually shown match, it may differ
4365 * when "compl_leader" is used to omit some of the matches. */
4366 while (!ins_compl_equal(compl_shown_match
,
4367 compl_leader
, (int)STRLEN(compl_leader
))
4368 && compl_shown_match
->cp_next
!= NULL
4369 && compl_shown_match
->cp_next
!= compl_first_match
)
4370 compl_shown_match
= compl_shown_match
->cp_next
;
4372 /* If we didn't find it searching forward, and compl_shows_dir is
4373 * backward, find the last match. */
4374 if (compl_shows_dir
== BACKWARD
4375 && !ins_compl_equal(compl_shown_match
,
4376 compl_leader
, (int)STRLEN(compl_leader
))
4377 && (compl_shown_match
->cp_next
== NULL
4378 || compl_shown_match
->cp_next
== compl_first_match
))
4380 while (!ins_compl_equal(compl_shown_match
,
4381 compl_leader
, (int)STRLEN(compl_leader
))
4382 && compl_shown_match
->cp_prev
!= NULL
4383 && compl_shown_match
->cp_prev
!= compl_first_match
)
4384 compl_shown_match
= compl_shown_match
->cp_prev
;
4388 if (allow_get_expansion
&& insert_match
4389 && (!(compl_get_longest
|| compl_restarting
) || compl_used_match
))
4390 /* Delete old text to be replaced */
4393 /* When finding the longest common text we stick at the original text,
4394 * don't let CTRL-N or CTRL-P move to the first match. */
4395 advance
= count
!= 1 || !allow_get_expansion
|| !compl_get_longest
;
4397 /* When restarting the search don't insert the first match either. */
4398 if (compl_restarting
)
4401 compl_restarting
= FALSE
;
4404 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4408 if (compl_shows_dir
== FORWARD
&& compl_shown_match
->cp_next
!= NULL
)
4410 compl_shown_match
= compl_shown_match
->cp_next
;
4411 found_end
= (compl_first_match
!= NULL
4412 && (compl_shown_match
->cp_next
== compl_first_match
4413 || compl_shown_match
== compl_first_match
));
4415 else if (compl_shows_dir
== BACKWARD
4416 && compl_shown_match
->cp_prev
!= NULL
)
4418 found_end
= (compl_shown_match
== compl_first_match
);
4419 compl_shown_match
= compl_shown_match
->cp_prev
;
4420 found_end
|= (compl_shown_match
== compl_first_match
);
4424 if (!allow_get_expansion
)
4428 if (compl_shows_dir
== BACKWARD
)
4429 compl_pending
-= todo
+ 1;
4431 compl_pending
+= todo
+ 1;
4438 if (compl_shows_dir
== BACKWARD
)
4445 num_matches
= ins_compl_get_exp(&compl_startpos
);
4447 /* handle any pending completions */
4448 while (compl_pending
!= 0 && compl_direction
== compl_shows_dir
4451 if (compl_pending
> 0 && compl_shown_match
->cp_next
!= NULL
)
4453 compl_shown_match
= compl_shown_match
->cp_next
;
4456 if (compl_pending
< 0 && compl_shown_match
->cp_prev
!= NULL
)
4458 compl_shown_match
= compl_shown_match
->cp_prev
;
4466 if ((compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0
4467 && compl_leader
!= NULL
4468 && !ins_compl_equal(compl_shown_match
,
4469 compl_leader
, (int)STRLEN(compl_leader
)))
4472 /* Remember a matching item. */
4473 found_compl
= compl_shown_match
;
4475 /* Stop at the end of the list when we found a usable match. */
4478 if (found_compl
!= NULL
)
4480 compl_shown_match
= found_compl
;
4483 todo
= 1; /* use first usable match after wrapping around */
4487 /* Insert the text of the new completion, or the compl_leader. */
4490 if (!compl_get_longest
|| compl_used_match
)
4493 ins_bytes(compl_leader
+ ins_compl_len());
4496 compl_used_match
= FALSE
;
4498 if (!allow_get_expansion
)
4500 /* may undisplay the popup menu first */
4501 ins_compl_upd_pum();
4503 /* redraw to show the user what was inserted */
4506 /* display the updated popup menu */
4507 ins_compl_show_pum();
4511 /* Show the cursor after the match, not after the redrawn text. */
4514 gui_update_cursor(FALSE
, FALSE
);
4518 /* Delete old text to be replaced, since we're still searching and
4519 * don't want to match ourselves! */
4523 /* Enter will select a match when the match wasn't inserted and the popup
4524 * menu is visible. */
4525 compl_enter_selects
= !insert_match
&& compl_match_array
!= NULL
;
4528 * Show the file name for the match (if any)
4529 * Truncate the file name to avoid a wait for return.
4531 if (compl_shown_match
->cp_fname
!= NULL
)
4533 STRCPY(IObuff
, "match in file ");
4534 i
= (vim_strsize(compl_shown_match
->cp_fname
) + 16) - sc_col
;
4538 STRCAT(IObuff
, "<");
4539 STRCAT(IObuff
, compl_shown_match
->cp_fname
+ i
);
4541 redraw_cmdline
= FALSE
; /* don't overwrite! */
4548 * Call this while finding completions, to check whether the user has hit a key
4549 * that should change the currently displayed completion, or exit completion
4550 * mode. Also, when compl_pending is not zero, show a completion as soon as
4552 * "frequency" specifies out of how many calls we actually check.
4555 ins_compl_check_keys(frequency
)
4558 static int count
= 0;
4562 /* Don't check when reading keys from a script. That would break the test
4567 /* Only do this at regular intervals */
4568 if (++count
< frequency
)
4572 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4573 * can't do its work correctly. */
4577 if (vim_is_ctrl_x_key(c
) && c
!= Ctrl_X
&& c
!= Ctrl_R
)
4579 c
= safe_vgetc(); /* Eat the character */
4580 compl_shows_dir
= ins_compl_key2dir(c
);
4581 (void)ins_compl_next(FALSE
, ins_compl_key2count(c
),
4582 c
!= K_UP
&& c
!= K_DOWN
);
4586 /* Need to get the character to have KeyTyped set. We'll put it
4587 * back with vungetc() below. But skip K_IGNORE. */
4591 /* Don't interrupt completion when the character wasn't typed,
4592 * e.g., when doing @q to replay keys. */
4593 if (c
!= Ctrl_R
&& KeyTyped
)
4594 compl_interrupted
= TRUE
;
4600 if (compl_pending
!= 0 && !got_int
)
4602 int todo
= compl_pending
> 0 ? compl_pending
: -compl_pending
;
4605 (void)ins_compl_next(FALSE
, todo
, TRUE
);
4610 * Decide the direction of Insert mode complete from the key typed.
4611 * Returns BACKWARD or FORWARD.
4614 ins_compl_key2dir(c
)
4617 if (c
== Ctrl_P
|| c
== Ctrl_L
4618 || (pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
4619 || c
== K_S_UP
|| c
== K_UP
)))
4625 * Return TRUE for keys that are used for completion only when the popup menu
4629 ins_compl_pum_key(c
)
4632 return pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
|| c
== K_S_UP
4633 || c
== K_PAGEDOWN
|| c
== K_KPAGEDOWN
|| c
== K_S_DOWN
4634 || c
== K_UP
|| c
== K_DOWN
);
4638 * Decide the number of completions to move forward.
4639 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4642 ins_compl_key2count(c
)
4647 if (ins_compl_pum_key(c
) && c
!= K_UP
&& c
!= K_DOWN
)
4649 h
= pum_get_height();
4651 h
-= 2; /* keep some context */
4658 * Return TRUE if completion with "c" should insert the match, FALSE if only
4659 * to change the currently selected completion.
4662 ins_compl_use_match(c
)
4681 * Do Insert mode completion.
4682 * Called when character "c" was typed, which has a meaning for completion.
4683 * Returns OK if completion was done, FAIL if something failed (out of mem).
4690 int startcol
= 0; /* column where searched text starts */
4691 colnr_T curs_col
; /* cursor column */
4695 compl_direction
= ins_compl_key2dir(c
);
4698 /* First time we hit ^N or ^P (in a row, I mean) */
4701 #ifdef FEAT_SMARTINDENT
4704 can_si_back
= FALSE
;
4706 if (stop_arrow() == FAIL
)
4709 line
= ml_get(curwin
->w_cursor
.lnum
);
4710 curs_col
= curwin
->w_cursor
.col
;
4713 /* If this same ctrl_x_mode has been interrupted use the text from
4714 * "compl_startpos" to the cursor as a pattern to add a new word
4715 * instead of expand the one before the cursor, in word-wise if
4716 * "compl_startpos" is not in the same line as the cursor then fix it
4717 * (the line has been split because it was longer than 'tw'). if SOL
4718 * is set then skip the previous pattern, a word at the beginning of
4719 * the line has been inserted, we'll look for that -- Acevedo. */
4720 if ((compl_cont_status
& CONT_INTRPT
) == CONT_INTRPT
4721 && compl_cont_mode
== ctrl_x_mode
)
4724 * it is a continued search
4726 compl_cont_status
&= ~CONT_INTRPT
; /* remove INTRPT */
4727 if (ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_PATH_PATTERNS
4728 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4730 if (compl_startpos
.lnum
!= curwin
->w_cursor
.lnum
)
4732 /* line (probably) wrapped, set compl_startpos to the
4733 * first non_blank in the line, if it is not a wordchar
4734 * include it to get a better pattern, but then we don't
4735 * want the "\\<" prefix, check it bellow */
4736 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4737 compl_startpos
.col
= compl_col
;
4738 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4739 compl_cont_status
&= ~CONT_SOL
; /* clear SOL if present */
4743 /* S_IPOS was set when we inserted a word that was at the
4744 * beginning of the line, which means that we'll go to SOL
4745 * mode but first we need to redefine compl_startpos */
4746 if (compl_cont_status
& CONT_S_IPOS
)
4748 compl_cont_status
|= CONT_SOL
;
4749 compl_startpos
.col
= (colnr_T
)(skipwhite(
4751 + compl_startpos
.col
) - line
);
4753 compl_col
= compl_startpos
.col
;
4755 compl_length
= curwin
->w_cursor
.col
- (int)compl_col
;
4756 /* IObuff is used to add a "word from the next line" would we
4757 * have enough space? just being paranoid */
4758 #define MIN_SPACE 75
4759 if (compl_length
> (IOSIZE
- MIN_SPACE
))
4761 compl_cont_status
&= ~CONT_SOL
;
4762 compl_length
= (IOSIZE
- MIN_SPACE
);
4763 compl_col
= curwin
->w_cursor
.col
- compl_length
;
4765 compl_cont_status
|= CONT_ADDING
| CONT_N_ADDS
;
4766 if (compl_length
< 1)
4767 compl_cont_status
&= CONT_LOCAL
;
4769 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4770 compl_cont_status
= CONT_ADDING
| CONT_N_ADDS
;
4772 compl_cont_status
= 0;
4775 compl_cont_status
&= CONT_LOCAL
;
4777 if (!(compl_cont_status
& CONT_ADDING
)) /* normal expansion */
4779 compl_cont_mode
= ctrl_x_mode
;
4780 if (ctrl_x_mode
!= 0) /* Remove LOCAL if ctrl_x_mode != 0 */
4781 compl_cont_status
= 0;
4782 compl_cont_status
|= CONT_N_ADDS
;
4783 compl_startpos
= curwin
->w_cursor
;
4784 startcol
= (int)curs_col
;
4788 /* Work out completion pattern and original text -- webb */
4789 if (ctrl_x_mode
== 0 || (ctrl_x_mode
& CTRL_X_WANT_IDENT
))
4791 if ((compl_cont_status
& CONT_SOL
)
4792 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4794 if (!(compl_cont_status
& CONT_ADDING
))
4796 while (--startcol
>= 0 && vim_isIDc(line
[startcol
]))
4798 compl_col
+= ++startcol
;
4799 compl_length
= curs_col
- startcol
;
4802 compl_pattern
= str_foldcase(line
+ compl_col
,
4803 compl_length
, NULL
, 0);
4805 compl_pattern
= vim_strnsave(line
+ compl_col
,
4807 if (compl_pattern
== NULL
)
4810 else if (compl_cont_status
& CONT_ADDING
)
4812 char_u
*prefix
= (char_u
*)"\\<";
4814 /* we need up to 2 extra chars for the prefix */
4815 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4817 if (compl_pattern
== NULL
)
4819 if (!vim_iswordp(line
+ compl_col
)
4823 vim_iswordp(mb_prevptr(line
, line
+ compl_col
))
4825 vim_iswordc(line
[compl_col
- 1])
4828 prefix
= (char_u
*)"";
4829 STRCPY((char *)compl_pattern
, prefix
);
4830 (void)quote_meta(compl_pattern
+ STRLEN(prefix
),
4831 line
+ compl_col
, compl_length
);
4833 else if (--startcol
< 0 ||
4835 !vim_iswordp(mb_prevptr(line
, line
+ startcol
+ 1))
4837 !vim_iswordc(line
[startcol
])
4841 /* Match any word of at least two chars */
4842 compl_pattern
= vim_strsave((char_u
*)"\\<\\k\\k");
4843 if (compl_pattern
== NULL
)
4845 compl_col
+= curs_col
;
4851 /* Search the point of change class of multibyte character
4852 * or not a word single byte character backward. */
4858 startcol
-= (*mb_head_off
)(line
, line
+ startcol
);
4859 base_class
= mb_get_class(line
+ startcol
);
4860 while (--startcol
>= 0)
4862 head_off
= (*mb_head_off
)(line
, line
+ startcol
);
4863 if (base_class
!= mb_get_class(line
+ startcol
4866 startcol
-= head_off
;
4871 while (--startcol
>= 0 && vim_iswordc(line
[startcol
]))
4873 compl_col
+= ++startcol
;
4874 compl_length
= (int)curs_col
- startcol
;
4875 if (compl_length
== 1)
4877 /* Only match word with at least two chars -- webb
4878 * there's no need to call quote_meta,
4879 * alloc(7) is enough -- Acevedo
4881 compl_pattern
= alloc(7);
4882 if (compl_pattern
== NULL
)
4884 STRCPY((char *)compl_pattern
, "\\<");
4885 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
, 1);
4886 STRCAT((char *)compl_pattern
, "\\k");
4890 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4892 if (compl_pattern
== NULL
)
4894 STRCPY((char *)compl_pattern
, "\\<");
4895 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
,
4900 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4902 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4903 compl_length
= (int)curs_col
- (int)compl_col
;
4904 if (compl_length
< 0) /* cursor in indent: empty pattern */
4907 compl_pattern
= str_foldcase(line
+ compl_col
, compl_length
,
4910 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4911 if (compl_pattern
== NULL
)
4914 else if (ctrl_x_mode
== CTRL_X_FILES
)
4916 while (--startcol
>= 0 && vim_isfilec(line
[startcol
]))
4918 compl_col
+= ++startcol
;
4919 compl_length
= (int)curs_col
- startcol
;
4920 compl_pattern
= addstar(line
+ compl_col
, compl_length
,
4922 if (compl_pattern
== NULL
)
4925 else if (ctrl_x_mode
== CTRL_X_CMDLINE
)
4927 compl_pattern
= vim_strnsave(line
, curs_col
);
4928 if (compl_pattern
== NULL
)
4930 set_cmd_context(&compl_xp
, compl_pattern
,
4931 (int)STRLEN(compl_pattern
), curs_col
);
4932 if (compl_xp
.xp_context
== EXPAND_UNSUCCESSFUL
4933 || compl_xp
.xp_context
== EXPAND_NOTHING
)
4934 /* No completion possible, use an empty pattern to get a
4935 * "pattern not found" message. */
4936 compl_col
= curs_col
;
4938 compl_col
= (int)(compl_xp
.xp_pattern
- compl_pattern
);
4939 compl_length
= curs_col
- compl_col
;
4941 else if (ctrl_x_mode
== CTRL_X_FUNCTION
|| ctrl_x_mode
== CTRL_X_OMNI
)
4943 #ifdef FEAT_COMPL_FUNC
4945 * Call user defined function 'completefunc' with "a:findstart"
4946 * set to 1 to obtain the length of text to use for completion.
4953 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
4955 funcname
= ctrl_x_mode
== CTRL_X_FUNCTION
4956 ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
4957 if (*funcname
== NUL
)
4959 EMSG2(_(e_notset
), ctrl_x_mode
== CTRL_X_FUNCTION
4960 ? "completefunc" : "omnifunc");
4964 args
[0] = (char_u
*)"1";
4966 pos
= curwin
->w_cursor
;
4967 col
= call_func_retnr(funcname
, 2, args
, FALSE
);
4968 curwin
->w_cursor
= pos
; /* restore the cursor position */
4973 if (compl_col
> curs_col
)
4974 compl_col
= curs_col
;
4976 /* Setup variables for completion. Need to obtain "line" again,
4977 * it may have become invalid. */
4978 line
= ml_get(curwin
->w_cursor
.lnum
);
4979 compl_length
= curs_col
- compl_col
;
4980 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4981 if (compl_pattern
== NULL
)
4985 else if (ctrl_x_mode
== CTRL_X_SPELL
)
4988 if (spell_bad_len
> 0)
4989 compl_col
= curs_col
- spell_bad_len
;
4991 compl_col
= spell_word_start(startcol
);
4992 if (compl_col
>= (colnr_T
)startcol
)
4995 compl_col
= curs_col
;
4999 spell_expand_check_cap(compl_col
);
5000 compl_length
= (int)curs_col
- compl_col
;
5002 /* Need to obtain "line" again, it may have become invalid. */
5003 line
= ml_get(curwin
->w_cursor
.lnum
);
5004 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
5005 if (compl_pattern
== NULL
)
5011 EMSG2(_(e_intern2
), "ins_complete()");
5015 if (compl_cont_status
& CONT_ADDING
)
5017 edit_submode_pre
= (char_u
*)_(" Adding");
5018 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
5020 /* Insert a new line, keep indentation but ignore 'comments' */
5021 #ifdef FEAT_COMMENTS
5022 char_u
*old
= curbuf
->b_p_com
;
5024 curbuf
->b_p_com
= (char_u
*)"";
5026 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
5027 compl_startpos
.col
= compl_col
;
5029 #ifdef FEAT_COMMENTS
5030 curbuf
->b_p_com
= old
;
5033 compl_col
= curwin
->w_cursor
.col
;
5038 edit_submode_pre
= NULL
;
5039 compl_startpos
.col
= compl_col
;
5042 if (compl_cont_status
& CONT_LOCAL
)
5043 edit_submode
= (char_u
*)_(ctrl_x_msgs
[CTRL_X_LOCAL_MSG
]);
5045 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
5047 /* Always add completion for the original text. */
5048 vim_free(compl_orig_text
);
5049 compl_orig_text
= vim_strnsave(line
+ compl_col
, compl_length
);
5050 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
5051 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
5053 vim_free(compl_pattern
);
5054 compl_pattern
= NULL
;
5055 vim_free(compl_orig_text
);
5056 compl_orig_text
= NULL
;
5060 /* showmode might reset the internal line pointers, so it must
5061 * be called before line = ml_get(), or when this address is no
5062 * longer needed. -- Acevedo.
5064 edit_submode_extra
= (char_u
*)_("-- Searching...");
5065 edit_submode_highl
= HLF_COUNT
;
5067 edit_submode_extra
= NULL
;
5071 compl_shown_match
= compl_curr_match
;
5072 compl_shows_dir
= compl_direction
;
5075 * Find next match (and following matches).
5077 save_w_wrow
= curwin
->w_wrow
;
5078 n
= ins_compl_next(TRUE
, ins_compl_key2count(c
), ins_compl_use_match(c
));
5080 /* may undisplay the popup menu */
5081 ins_compl_upd_pum();
5083 if (n
> 1) /* all matches have been found */
5085 compl_curr_match
= compl_shown_match
;
5086 compl_direction
= compl_shows_dir
;
5088 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5090 if (got_int
&& !global_busy
)
5096 /* we found no match if the list has only the "compl_orig_text"-entry */
5097 if (compl_first_match
== compl_first_match
->cp_next
)
5099 edit_submode_extra
= (compl_cont_status
& CONT_ADDING
)
5101 ? (char_u
*)_(e_hitend
) : (char_u
*)_(e_patnotf
);
5102 edit_submode_highl
= HLF_E
;
5103 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5104 * because we couldn't expand anything at first place, but if we used
5105 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5106 * (such as M in M'exico) if not tried already. -- Acevedo */
5107 if ( compl_length
> 1
5108 || (compl_cont_status
& CONT_ADDING
)
5109 || (ctrl_x_mode
!= 0
5110 && ctrl_x_mode
!= CTRL_X_PATH_PATTERNS
5111 && ctrl_x_mode
!= CTRL_X_PATH_DEFINES
))
5112 compl_cont_status
&= ~CONT_N_ADDS
;
5115 if (compl_curr_match
->cp_flags
& CONT_S_IPOS
)
5116 compl_cont_status
|= CONT_S_IPOS
;
5118 compl_cont_status
&= ~CONT_S_IPOS
;
5120 if (edit_submode_extra
== NULL
)
5122 if (compl_curr_match
->cp_flags
& ORIGINAL_TEXT
)
5124 edit_submode_extra
= (char_u
*)_("Back at original");
5125 edit_submode_highl
= HLF_W
;
5127 else if (compl_cont_status
& CONT_S_IPOS
)
5129 edit_submode_extra
= (char_u
*)_("Word from other line");
5130 edit_submode_highl
= HLF_COUNT
;
5132 else if (compl_curr_match
->cp_next
== compl_curr_match
->cp_prev
)
5134 edit_submode_extra
= (char_u
*)_("The only match");
5135 edit_submode_highl
= HLF_COUNT
;
5139 /* Update completion sequence number when needed. */
5140 if (compl_curr_match
->cp_number
== -1)
5145 if (compl_direction
== FORWARD
)
5147 /* search backwards for the first valid (!= -1) number.
5148 * This should normally succeed already at the first loop
5149 * cycle, so it's fast! */
5150 for (match
= compl_curr_match
->cp_prev
; match
!= NULL
5151 && match
!= compl_first_match
;
5152 match
= match
->cp_prev
)
5153 if (match
->cp_number
!= -1)
5155 number
= match
->cp_number
;
5159 /* go up and assign all numbers which are not assigned
5161 for (match
= match
->cp_next
;
5162 match
!= NULL
&& match
->cp_number
== -1;
5163 match
= match
->cp_next
)
5164 match
->cp_number
= ++number
;
5168 /* search forwards (upwards) for the first valid (!= -1)
5169 * number. This should normally succeed already at the
5170 * first loop cycle, so it's fast! */
5171 for (match
= compl_curr_match
->cp_next
; match
!= NULL
5172 && match
!= compl_first_match
;
5173 match
= match
->cp_next
)
5174 if (match
->cp_number
!= -1)
5176 number
= match
->cp_number
;
5180 /* go down and assign all numbers which are not
5182 for (match
= match
->cp_prev
; match
5183 && match
->cp_number
== -1;
5184 match
= match
->cp_prev
)
5185 match
->cp_number
= ++number
;
5189 /* The match should always have a sequence number now, this is
5190 * just a safety check. */
5191 if (compl_curr_match
->cp_number
!= -1)
5193 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5194 * Translations may need more than twice that. */
5195 static char_u match_ref
[81];
5197 if (compl_matches
> 0)
5198 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
5199 _("match %d of %d"),
5200 compl_curr_match
->cp_number
, compl_matches
);
5202 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
5204 compl_curr_match
->cp_number
);
5205 edit_submode_extra
= match_ref
;
5206 edit_submode_highl
= HLF_R
;
5208 curs_columns(FALSE
);
5213 /* Show a message about what (completion) mode we're in. */
5215 if (edit_submode_extra
!= NULL
)
5218 msg_attr(edit_submode_extra
,
5219 edit_submode_highl
< HLF_COUNT
5220 ? hl_attr(edit_submode_highl
) : 0);
5223 msg_clr_cmdline(); /* necessary for "noshowmode" */
5225 /* Show the popup menu, unless we got interrupted. */
5226 if (!compl_interrupted
)
5228 /* RedrawingDisabled may be set when invoked through complete(). */
5229 n
= RedrawingDisabled
;
5230 RedrawingDisabled
= 0;
5232 /* If the cursor moved we need to remove the pum first. */
5234 if (save_w_wrow
!= curwin
->w_wrow
)
5235 ins_compl_del_pum();
5237 ins_compl_show_pum();
5239 RedrawingDisabled
= n
;
5241 compl_was_interrupted
= compl_interrupted
;
5242 compl_interrupted
= FALSE
;
5248 * Looks in the first "len" chars. of "src" for search-metachars.
5249 * If dest is not NULL the chars. are copied there quoting (with
5250 * a backslash) the metachars, and dest would be NUL terminated.
5251 * Returns the length (needed) of dest
5254 quote_meta(dest
, src
, len
)
5259 unsigned m
= (unsigned)len
+ 1; /* one extra for the NUL */
5261 for ( ; --len
>= 0; src
++)
5268 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5269 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5272 if (!p_magic
) /* quote these only if magic is set */
5275 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5276 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5278 case '^': /* currently it's not needed. */
5288 /* Copy remaining bytes of a multibyte character. */
5293 mb_len
= (*mb_ptr2len
)(src
) - 1;
5294 if (mb_len
> 0 && len
>= mb_len
)
5295 for (i
= 0; i
< mb_len
; ++i
)
5310 #endif /* FEAT_INS_EXPAND */
5313 * Next character is interpreted literally.
5314 * A one, two or three digit decimal number is interpreted as its byte value.
5315 * If one or two digits are entered, the next character is given to vungetc().
5316 * For Unicode a character > 255 may be returned.
5335 * In GUI there is no point inserting the internal code for a special key.
5336 * It is more useful to insert the string "<KEY>" instead. This would
5337 * probably be useful in a text window too, but it would not be
5338 * vi-compatible (maybe there should be an option for it?) -- webb
5343 #ifdef USE_ON_FLY_SCROLL
5344 dont_scroll
= TRUE
; /* disallow scrolling here */
5346 ++no_mapping
; /* don't map the next key hits */
5352 #ifdef FEAT_CMDL_INFO
5353 if (!(State
& CMDLINE
)
5355 && MB_BYTE2LEN_CHECK(nc
) == 1
5360 if (nc
== 'x' || nc
== 'X')
5362 else if (nc
== 'o' || nc
== 'O')
5365 else if (nc
== 'u' || nc
== 'U')
5376 if (!vim_isxdigit(nc
))
5378 cc
= cc
* 16 + hex2nr(nc
);
5382 if (nc
< '0' || nc
> '7')
5384 cc
= cc
* 8 + nc
- '0';
5388 if (!VIM_ISDIGIT(nc
))
5390 cc
= cc
* 10 + nc
- '0';
5401 cc
= 255; /* limit range to 0-255 */
5404 if (hex
) /* hex: up to two chars */
5410 else if (unicode
) /* Unicode: up to four or eight chars */
5412 if ((unicode
== 'u' && i
>= 4) || (unicode
== 'U' && i
>= 8))
5416 else if (i
>= 3) /* decimal or octal: up to three chars */
5419 if (i
== 0) /* no number entered */
5421 if (nc
== K_ZERO
) /* NUL is stored as NL */
5433 if (cc
== 0) /* NUL is stored as NL */
5436 if (enc_dbcs
&& (cc
& 0xff) == 0)
5437 cc
= '?'; /* don't accept an illegal DBCS char, the NUL in the
5438 second byte will cause trouble! */
5448 got_int
= FALSE
; /* CTRL-C typed after CTRL-V is not an interrupt */
5453 * Insert character, taking care of special keys and mod_mask
5456 insert_special(c
, allow_modmask
, ctrlv
)
5459 int ctrlv
; /* c was typed after CTRL-V */
5465 * Special function key, translate into "<Key>". Up to the last '>' is
5466 * inserted with ins_str(), so as not to replace characters in replace
5468 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5469 * unless 'allow_modmask' is TRUE.
5472 /* Command-key never produces a normal key */
5473 if (mod_mask
& MOD_MASK_CMD
)
5474 allow_modmask
= TRUE
;
5476 if (IS_SPECIAL(c
) || (mod_mask
&& allow_modmask
))
5478 p
= get_special_key_name(c
, mod_mask
);
5479 len
= (int)STRLEN(p
);
5483 if (stop_arrow() == FAIL
)
5487 AppendToRedobuffLit(p
, -1);
5491 if (stop_arrow() == OK
)
5492 insertchar(c
, ctrlv
? INSCHAR_CTRLV
: 0, -1);
5496 * Special characters in this context are those that need processing other
5497 * than the simple insertion that can be performed here. This includes ESC
5498 * which terminates the insert, and CR/NL which need special processing to
5499 * open up a new line. This routine tries to optimize insertions performed by
5500 * the "redo", "undo" or "put" commands, so it needs to know when it should
5501 * stop and defer processing to the "normal" mechanism.
5502 * '0' and '^' are special, because they can be followed by CTRL-D.
5505 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5507 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5511 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5513 # define WHITECHAR(cc) vim_iswhite(cc)
5517 insertchar(c
, flags
, second_indent
)
5518 int c
; /* character to insert or NUL */
5519 int flags
; /* INSCHAR_FORMAT, etc. */
5520 int second_indent
; /* indent for second line if >= 0 */
5523 #ifdef FEAT_COMMENTS
5528 textwidth
= comp_textwidth(flags
& INSCHAR_FORMAT
);
5529 fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5532 * Try to break the line in two or more pieces when:
5533 * - Always do this if we have been called to do formatting only.
5534 * - Always do this when 'formatoptions' has the 'a' flag and the line
5535 * ends in white space.
5537 * - Don't do this if inserting a blank
5538 * - Don't do this if an existing character is being replaced, unless
5539 * we're in VREPLACE mode.
5540 * - Do this if the cursor is not on the line where insert started
5541 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5542 * before the insert.
5543 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5544 * before 'textwidth'
5547 && ((flags
& INSCHAR_FORMAT
)
5549 && !((State
& REPLACE_FLAG
)
5550 #ifdef FEAT_VREPLACE
5551 && !(State
& VREPLACE_FLAG
)
5553 && *ml_get_cursor() != NUL
)
5554 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
5555 || ((!has_format_option(FO_INS_LONG
)
5556 || Insstart_textlen
<= (colnr_T
)textwidth
)
5558 || Insstart_blank_vcol
<= (colnr_T
)textwidth
5561 /* Format with 'formatexpr' when it's set. Use internal formatting
5562 * when 'formatexpr' isn't set or it returns non-zero. */
5563 #if defined(FEAT_EVAL)
5564 int do_internal
= TRUE
;
5566 if (*curbuf
->b_p_fex
!= NUL
&& (flags
& INSCHAR_NO_FEX
) == 0)
5568 do_internal
= (fex_format(curwin
->w_cursor
.lnum
, 1L, c
) != 0);
5569 /* It may be required to save for undo again, e.g. when setline()
5571 ins_need_undo
= TRUE
;
5575 internal_format(textwidth
, second_indent
, flags
, c
== NUL
, c
);
5578 if (c
== NUL
) /* only formatting was wanted */
5581 #ifdef FEAT_COMMENTS
5582 /* Check whether this character should end a comment. */
5583 if (did_ai
&& (int)c
== end_comment_pending
)
5586 char_u lead_end
[COM_MAX_LEN
]; /* end-comment string */
5587 int middle_len
, end_len
;
5591 * Need to remove existing (middle) comment leader and insert end
5592 * comment leader. First, check what comment leader we can find.
5594 i
= get_leader_len(line
= ml_get_curline(), &p
, FALSE
);
5595 if (i
> 0 && vim_strchr(p
, COM_MIDDLE
) != NULL
) /* Just checking */
5597 /* Skip middle-comment string */
5598 while (*p
&& p
[-1] != ':') /* find end of middle flags */
5600 middle_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5601 /* Don't count trailing white space for middle_len */
5602 while (middle_len
> 0 && vim_iswhite(lead_end
[middle_len
- 1]))
5605 /* Find the end-comment string */
5606 while (*p
&& p
[-1] != ':') /* find end of end flags */
5608 end_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5610 /* Skip white space before the cursor */
5611 i
= curwin
->w_cursor
.col
;
5612 while (--i
>= 0 && vim_iswhite(line
[i
]))
5616 /* Skip to before the middle leader */
5619 /* Check some expected things before we go on */
5620 if (i
>= 0 && lead_end
[end_len
- 1] == end_comment_pending
)
5622 /* Backspace over all the stuff we want to replace */
5623 backspace_until_column(i
);
5626 * Insert the end-comment string, except for the last
5627 * character, which will get inserted as normal later.
5629 ins_bytes_len(lead_end
, end_len
- 1);
5633 end_comment_pending
= NUL
;
5637 #ifdef FEAT_SMARTINDENT
5640 can_si_back
= FALSE
;
5644 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5645 * This speeds up normal text input considerably.
5646 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5647 * need to re-indent at a ':', or any other character (but not what
5650 #ifdef USE_ON_FLY_SCROLL
5651 dont_scroll
= FALSE
; /* allow scrolling here */
5656 && (!has_mbyte
|| (*mb_char2len
)(c
) == 1)
5659 && !(State
& REPLACE_FLAG
)
5663 #ifdef FEAT_RIGHTLEFT
5668 #define INPUT_BUFLEN 100
5669 char_u buf
[INPUT_BUFLEN
+ 1];
5671 colnr_T virtcol
= 0;
5676 virtcol
= get_nolist_virtcol();
5678 * Stop the string when:
5679 * - no more chars available
5680 * - finding a special character (command key)
5682 * - running into the 'textwidth' boundary
5683 * - need to check for abbreviation: A non-word char after a word-char
5685 while ( (c
= vpeekc()) != NUL
5688 && (!has_mbyte
|| MB_BYTE2LEN_CHECK(c
) == 1)
5692 || (virtcol
+= byte2cells(buf
[i
- 1])) < (colnr_T
)textwidth
)
5693 && !(!no_abbr
&& !vim_iswordc(c
) && vim_iswordc(buf
[i
- 1])))
5695 #ifdef FEAT_RIGHTLEFT
5697 if (p_hkmap
&& KeyTyped
)
5698 c
= hkmap(c
); /* Hebrew mode mapping */
5700 if (p_fkmap
&& KeyTyped
)
5701 c
= fkmap(c
); /* Farsi mode mapping */
5709 #ifdef FEAT_DIGRAPHS
5710 do_digraph(-1); /* clear digraphs */
5711 do_digraph(buf
[i
-1]); /* may be the start of a digraph */
5715 if (flags
& INSCHAR_CTRLV
)
5723 AppendToRedobuffLit(buf
+ i
, -1);
5730 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
5732 char_u buf
[MB_MAXBYTES
+ 1];
5734 (*mb_char2bytes
)(c
, buf
);
5736 ins_char_bytes(buf
, cc
);
5737 AppendCharToRedobuff(c
);
5743 if (flags
& INSCHAR_CTRLV
)
5746 AppendCharToRedobuff(c
);
5752 * Format text at the current insert position.
5755 internal_format(textwidth
, second_indent
, flags
, format_only
, c
)
5760 int c
; /* character to be inserted (can be NUL) */
5763 int save_char
= NUL
;
5764 int haveto_redraw
= FALSE
;
5765 int fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5767 int fo_multibyte
= has_format_option(FO_MBYTE_BREAK
);
5769 int fo_white_par
= has_format_option(FO_WHITE_PAR
);
5770 int first_line
= TRUE
;
5771 #ifdef FEAT_COMMENTS
5773 int no_leader
= FALSE
;
5774 int do_comments
= (flags
& INSCHAR_DO_COM
);
5778 * When 'ai' is off we don't want a space under the cursor to be
5779 * deleted. Replace it with an 'x' temporarily.
5782 #ifdef FEAT_VREPLACE
5783 && !(State
& VREPLACE_FLAG
)
5787 cc
= gchar_cursor();
5788 if (vim_iswhite(cc
))
5796 * Repeat breaking lines, until the current line is not too long.
5800 int startcol
; /* Cursor column at entry */
5801 int wantcol
; /* column at textwidth border */
5802 int foundcol
; /* column for start of spaces */
5803 int end_foundcol
= 0; /* column for start of word */
5806 #ifdef FEAT_VREPLACE
5808 char_u
*saved_text
= NULL
;
5813 virtcol
= get_nolist_virtcol()
5814 + char2cells(c
!= NUL
? c
: gchar_cursor());
5815 if (virtcol
<= (colnr_T
)textwidth
)
5818 #ifdef FEAT_COMMENTS
5820 do_comments
= FALSE
;
5821 else if (!(flags
& INSCHAR_FORMAT
)
5822 && has_format_option(FO_WRAP_COMS
))
5825 /* Don't break until after the comment leader */
5827 leader_len
= get_leader_len(ml_get_curline(), NULL
, FALSE
);
5831 /* If the line doesn't start with a comment leader, then don't
5832 * start one in a following broken line. Avoids that a %word
5833 * moved to the start of the next line causes all following lines
5834 * to start with %. */
5835 if (leader_len
== 0)
5838 if (!(flags
& INSCHAR_FORMAT
)
5839 #ifdef FEAT_COMMENTS
5842 && !has_format_option(FO_WRAP
))
5845 if ((startcol
= curwin
->w_cursor
.col
) == 0)
5848 /* find column of textwidth border */
5849 coladvance((colnr_T
)textwidth
);
5850 wantcol
= curwin
->w_cursor
.col
;
5852 curwin
->w_cursor
.col
= startcol
;
5856 * Find position to break at.
5857 * Stop at first entered white when 'formatoptions' has 'v'
5859 while ((!fo_ins_blank
&& !has_format_option(FO_INS_VI
))
5860 || curwin
->w_cursor
.lnum
!= Insstart
.lnum
5861 || curwin
->w_cursor
.col
>= Insstart
.col
)
5863 if (curwin
->w_cursor
.col
== startcol
&& c
!= NUL
)
5866 cc
= gchar_cursor();
5869 /* remember position of blank just before text */
5870 end_col
= curwin
->w_cursor
.col
;
5872 /* find start of sequence of blanks */
5873 while (curwin
->w_cursor
.col
> 0 && WHITECHAR(cc
))
5876 cc
= gchar_cursor();
5878 if (curwin
->w_cursor
.col
== 0 && WHITECHAR(cc
))
5879 break; /* only spaces in front of text */
5880 #ifdef FEAT_COMMENTS
5881 /* Don't break until after the comment leader */
5882 if (curwin
->w_cursor
.col
< leader_len
)
5885 if (has_format_option(FO_ONE_LETTER
))
5887 /* do not break after one-letter words */
5888 if (curwin
->w_cursor
.col
== 0)
5889 break; /* one-letter word at begin */
5890 #ifdef FEAT_COMMENTS
5891 /* do not break "#a b" when 'tw' is 2 */
5892 if (curwin
->w_cursor
.col
<= leader_len
)
5895 col
= curwin
->w_cursor
.col
;
5897 cc
= gchar_cursor();
5900 continue; /* one-letter, continue */
5901 curwin
->w_cursor
.col
= col
;
5906 end_foundcol
= end_col
+ 1;
5907 foundcol
= curwin
->w_cursor
.col
;
5908 if (curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5912 else if (cc
>= 0x100 && fo_multibyte
)
5914 /* Break after or before a multi-byte character. */
5915 if (curwin
->w_cursor
.col
!= startcol
)
5917 #ifdef FEAT_COMMENTS
5918 /* Don't break until after the comment leader */
5919 if (curwin
->w_cursor
.col
< leader_len
)
5922 col
= curwin
->w_cursor
.col
;
5924 /* Don't change end_foundcol if already set. */
5925 if (foundcol
!= curwin
->w_cursor
.col
)
5927 foundcol
= curwin
->w_cursor
.col
;
5928 end_foundcol
= foundcol
;
5929 if (curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5932 curwin
->w_cursor
.col
= col
;
5935 if (curwin
->w_cursor
.col
== 0)
5938 col
= curwin
->w_cursor
.col
;
5941 cc
= gchar_cursor();
5944 continue; /* break with space */
5945 #ifdef FEAT_COMMENTS
5946 /* Don't break until after the comment leader */
5947 if (curwin
->w_cursor
.col
< leader_len
)
5951 curwin
->w_cursor
.col
= col
;
5953 foundcol
= curwin
->w_cursor
.col
;
5954 end_foundcol
= foundcol
;
5955 if (curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5959 if (curwin
->w_cursor
.col
== 0)
5964 if (foundcol
== 0) /* no spaces, cannot break line */
5966 curwin
->w_cursor
.col
= startcol
;
5970 /* Going to break the line, remove any "$" now. */
5974 * Offset between cursor position and line break is used by replace
5975 * stack functions. VREPLACE does not use this, and backspaces
5976 * over the text instead.
5978 #ifdef FEAT_VREPLACE
5979 if (State
& VREPLACE_FLAG
)
5980 orig_col
= startcol
; /* Will start backspacing from here */
5983 replace_offset
= startcol
- end_foundcol
;
5986 * adjust startcol for spaces that will be deleted and
5987 * characters that will remain on top line
5989 curwin
->w_cursor
.col
= foundcol
;
5990 while ((cc
= gchar_cursor(), WHITECHAR(cc
))
5991 && (!fo_white_par
|| curwin
->w_cursor
.col
< startcol
))
5993 startcol
-= curwin
->w_cursor
.col
;
5997 #ifdef FEAT_VREPLACE
5998 if (State
& VREPLACE_FLAG
)
6001 * In VREPLACE mode, we will backspace over the text to be
6002 * wrapped, so save a copy now to put on the next line.
6004 saved_text
= vim_strsave(ml_get_cursor());
6005 curwin
->w_cursor
.col
= orig_col
;
6006 if (saved_text
== NULL
)
6007 break; /* Can't do it, out of memory */
6008 saved_text
[startcol
] = NUL
;
6010 /* Backspace over characters that will move to the next line */
6012 backspace_until_column(foundcol
);
6017 /* put cursor after pos. to break line */
6019 curwin
->w_cursor
.col
= foundcol
;
6023 * Split the line just before the margin.
6024 * Only insert/delete lines, but don't really redraw the window.
6026 open_line(FORWARD
, OPENLINE_DELSPACES
+ OPENLINE_MARKFIX
6027 + (fo_white_par
? OPENLINE_KEEPTRAIL
: 0)
6028 #ifdef FEAT_COMMENTS
6029 + (do_comments
? OPENLINE_DO_COM
: 0)
6037 if (second_indent
< 0 && has_format_option(FO_Q_NUMBER
))
6038 second_indent
= get_number_indent(curwin
->w_cursor
.lnum
-1);
6039 if (second_indent
>= 0)
6041 #ifdef FEAT_VREPLACE
6042 if (State
& VREPLACE_FLAG
)
6043 change_indent(INDENT_SET
, second_indent
, FALSE
, NUL
, TRUE
);
6046 (void)set_indent(second_indent
, SIN_CHANGED
);
6051 #ifdef FEAT_VREPLACE
6052 if (State
& VREPLACE_FLAG
)
6055 * In VREPLACE mode we have backspaced over the text to be
6056 * moved, now we re-insert it into the new line.
6058 ins_bytes(saved_text
);
6059 vim_free(saved_text
);
6065 * Check if cursor is not past the NUL off the line, cindent
6066 * may have added or removed indent.
6068 curwin
->w_cursor
.col
+= startcol
;
6069 len
= (colnr_T
)STRLEN(ml_get_curline());
6070 if (curwin
->w_cursor
.col
> len
)
6071 curwin
->w_cursor
.col
= len
;
6074 haveto_redraw
= TRUE
;
6078 /* moved the cursor, don't autoindent or cindent now */
6080 #ifdef FEAT_SMARTINDENT
6083 can_si_back
= FALSE
;
6088 if (save_char
!= NUL
) /* put back space after cursor */
6089 pchar_cursor(save_char
);
6091 if (!format_only
&& haveto_redraw
)
6094 redraw_curbuf_later(VALID
);
6099 * Called after inserting or deleting text: When 'formatoptions' includes the
6100 * 'a' flag format from the current line until the end of the paragraph.
6101 * Keep the cursor at the same position relative to the text.
6102 * The caller must have saved the cursor line for undo, following ones will be
6106 auto_format(trailblank
, prev_line
)
6107 int trailblank
; /* when TRUE also format with trailing blank */
6108 int prev_line
; /* may start in previous line */
6117 if (!has_format_option(FO_AUTO
))
6120 pos
= curwin
->w_cursor
;
6121 old
= ml_get_curline();
6123 /* may remove added space */
6124 check_auto_format(FALSE
);
6126 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6127 * user might insert normal text next. Also skip formatting when "1" is
6128 * in 'formatoptions' and there is a single character before the cursor.
6129 * Otherwise the line would be broken and when typing another non-white
6130 * next they are not joined back together. */
6131 wasatend
= (pos
.col
== (colnr_T
)STRLEN(old
));
6132 if (*old
!= NUL
&& !trailblank
&& wasatend
)
6135 cc
= gchar_cursor();
6136 if (!WHITECHAR(cc
) && curwin
->w_cursor
.col
> 0
6137 && has_format_option(FO_ONE_LETTER
))
6139 cc
= gchar_cursor();
6142 curwin
->w_cursor
= pos
;
6145 curwin
->w_cursor
= pos
;
6148 #ifdef FEAT_COMMENTS
6149 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6151 if (has_format_option(FO_WRAP_COMS
) && !has_format_option(FO_WRAP
)
6152 && get_leader_len(old
, NULL
, FALSE
) == 0)
6157 * May start formatting in a previous line, so that after "x" a word is
6158 * moved to the previous line if it fits there now. Only when this is not
6159 * the start of a paragraph.
6161 if (prev_line
&& !paragraph_start(curwin
->w_cursor
.lnum
))
6163 --curwin
->w_cursor
.lnum
;
6164 if (u_save_cursor() == FAIL
)
6169 * Do the formatting and restore the cursor position. "saved_cursor" will
6170 * be adjusted for the text formatting.
6173 format_lines((linenr_T
)-1, FALSE
);
6174 curwin
->w_cursor
= saved_cursor
;
6175 saved_cursor
.lnum
= 0;
6177 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6179 /* "cannot happen" */
6180 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6181 coladvance((colnr_T
)MAXCOL
);
6186 /* Insert mode: If the cursor is now after the end of the line while it
6187 * previously wasn't, the line was broken. Because of the rule above we
6188 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6190 if (!wasatend
&& has_format_option(FO_WHITE_PAR
))
6192 new = ml_get_curline();
6193 len
= (colnr_T
)STRLEN(new);
6194 if (curwin
->w_cursor
.col
== len
)
6196 pnew
= vim_strnsave(new, len
+ 2);
6198 pnew
[len
+ 1] = NUL
;
6199 ml_replace(curwin
->w_cursor
.lnum
, pnew
, FALSE
);
6200 /* remove the space later */
6201 did_add_space
= TRUE
;
6204 /* may remove added space */
6205 check_auto_format(FALSE
);
6212 * When an extra space was added to continue a paragraph for auto-formatting,
6213 * delete it now. The space must be under the cursor, just after the insert
6217 check_auto_format(end_insert
)
6218 int end_insert
; /* TRUE when ending Insert mode */
6225 cc
= gchar_cursor();
6227 /* Somehow the space was removed already. */
6228 did_add_space
= FALSE
;
6239 /* The space is no longer at the end of the line, delete it. */
6241 did_add_space
= FALSE
;
6248 * Find out textwidth to be used for formatting:
6249 * if 'textwidth' option is set, use it
6250 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6251 * if invalid value, use 0.
6252 * Set default to window width (maximum 79) for "gq" operator.
6256 int ff
; /* force formatting (for "gq" command) */
6260 textwidth
= curbuf
->b_p_tw
;
6261 if (textwidth
== 0 && curbuf
->b_p_wm
)
6263 /* The width is the window width minus 'wrapmargin' minus all the
6264 * things that add to the margin. */
6265 textwidth
= W_WIDTH(curwin
) - curbuf
->b_p_wm
;
6267 if (cmdwin_type
!= 0)
6271 textwidth
-= curwin
->w_p_fdc
;
6274 if (curwin
->w_buffer
->b_signlist
!= NULL
6275 # ifdef FEAT_NETBEANS_INTG
6286 if (ff
&& textwidth
== 0)
6288 textwidth
= W_WIDTH(curwin
) - 1;
6296 * Put a character in the redo buffer, for when just after a CTRL-V.
6304 /* Only digits need special treatment. Translate them into a string of
6308 vim_snprintf((char *)buf
, sizeof(buf
), "%03d", c
);
6309 AppendToRedobuff(buf
);
6312 AppendCharToRedobuff(c
);
6316 * start_arrow() is called when an arrow key is used in insert mode.
6317 * For undo/redo it resembles hitting the <ESC> key.
6320 start_arrow(end_insert_pos
)
6321 pos_T
*end_insert_pos
; /* can be NULL */
6323 if (!arrow_used
) /* something has been inserted */
6325 AppendToRedobuff(ESC_STR
);
6326 stop_insert(end_insert_pos
, FALSE
);
6327 arrow_used
= TRUE
; /* this means we stopped the current insert */
6330 check_spell_redraw();
6336 * If we skipped highlighting word at cursor, do it now.
6337 * It may be skipped again, thus reset spell_redraw_lnum first.
6340 check_spell_redraw()
6342 if (spell_redraw_lnum
!= 0)
6344 linenr_T lnum
= spell_redraw_lnum
;
6346 spell_redraw_lnum
= 0;
6347 redrawWinline(lnum
, FALSE
);
6352 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6353 * spelled word, if there is one.
6356 spell_back_to_badword()
6358 pos_T tpos
= curwin
->w_cursor
;
6360 spell_bad_len
= spell_move_to(curwin
, BACKWARD
, TRUE
, TRUE
, NULL
);
6361 if (curwin
->w_cursor
.col
!= tpos
.col
)
6367 * stop_arrow() is called before a change is made in insert mode.
6368 * If an arrow key has been used, start a new insertion.
6369 * Returns FAIL if undo is impossible, shouldn't insert then.
6376 if (u_save_cursor() == OK
)
6379 ins_need_undo
= FALSE
;
6381 Insstart
= curwin
->w_cursor
; /* new insertion starts here */
6382 Insstart_textlen
= (colnr_T
)linetabsize(ml_get_curline());
6384 #ifdef FEAT_VREPLACE
6385 if (State
& VREPLACE_FLAG
)
6387 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
6388 vr_lines_changed
= 1;
6392 AppendToRedobuff((char_u
*)"1i"); /* pretend we start an insertion */
6393 new_insert_skip
= 2;
6395 else if (ins_need_undo
)
6397 if (u_save_cursor() == OK
)
6398 ins_need_undo
= FALSE
;
6402 /* Always open fold at the cursor line when inserting something. */
6406 return (arrow_used
|| ins_need_undo
? FAIL
: OK
);
6410 * Do a few things to stop inserting.
6411 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6412 * to another window/buffer.
6415 stop_insert(end_insert_pos
, esc
)
6416 pos_T
*end_insert_pos
;
6417 int esc
; /* called by ins_esc() */
6423 replace_flush(); /* abandon replace stack */
6426 * Save the inserted text for later redo with ^@ and CTRL-A.
6427 * Don't do it when "restart_edit" was set and nothing was inserted,
6428 * otherwise CTRL-O w and then <Left> will clear "last_insert".
6430 ptr
= get_inserted();
6431 if (did_restart_edit
== 0 || (ptr
!= NULL
6432 && (int)STRLEN(ptr
) > new_insert_skip
))
6434 vim_free(last_insert
);
6436 last_insert_skip
= new_insert_skip
;
6441 if (!arrow_used
&& end_insert_pos
!= NULL
)
6443 /* Auto-format now. It may seem strange to do this when stopping an
6444 * insertion (or moving the cursor), but it's required when appending
6445 * a line and having it end in a space. But only do it when something
6446 * was actually inserted, otherwise undo won't work. */
6447 if (!ins_need_undo
&& has_format_option(FO_AUTO
))
6449 pos_T tpos
= curwin
->w_cursor
;
6451 /* When the cursor is at the end of the line after a space the
6452 * formatting will move it to the following word. Avoid that by
6453 * moving the cursor onto the space. */
6455 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
)
6458 cc
= gchar_cursor();
6459 if (!vim_iswhite(cc
))
6460 curwin
->w_cursor
= tpos
;
6463 auto_format(TRUE
, FALSE
);
6465 if (vim_iswhite(cc
))
6467 if (gchar_cursor() != NUL
)
6469 #ifdef FEAT_VIRTUALEDIT
6470 /* If the cursor is still at the same character, also keep
6472 if (gchar_cursor() == NUL
6473 && curwin
->w_cursor
.lnum
== tpos
.lnum
6474 && curwin
->w_cursor
.col
== tpos
.col
)
6475 curwin
->w_cursor
.coladd
= tpos
.coladd
;
6480 /* If a space was inserted for auto-formatting, remove it now. */
6481 check_auto_format(TRUE
);
6483 /* If we just did an auto-indent, remove the white space from the end
6484 * of the line, and put the cursor back.
6485 * Do this when ESC was used or moving the cursor up/down.
6486 * Check for the old position still being valid, just in case the text
6487 * got changed unexpectedly. */
6488 if (did_ai
&& (esc
|| (vim_strchr(p_cpo
, CPO_INDENT
) == NULL
6489 && curwin
->w_cursor
.lnum
!= end_insert_pos
->lnum
))
6490 && end_insert_pos
->lnum
<= curbuf
->b_ml
.ml_line_count
)
6492 pos_T tpos
= curwin
->w_cursor
;
6494 curwin
->w_cursor
= *end_insert_pos
;
6495 check_cursor_col(); /* make sure it is not past the line */
6498 if (gchar_cursor() == NUL
&& curwin
->w_cursor
.col
> 0)
6499 --curwin
->w_cursor
.col
;
6500 cc
= gchar_cursor();
6501 if (!vim_iswhite(cc
))
6503 if (del_char(TRUE
) == FAIL
)
6504 break; /* should not happen */
6506 if (curwin
->w_cursor
.lnum
!= tpos
.lnum
)
6507 curwin
->w_cursor
= tpos
;
6509 ++curwin
->w_cursor
.col
; /* put cursor back on the NUL */
6512 /* <C-S-Right> may have started Visual mode, adjust the position for
6513 * deleted characters. */
6514 if (VIsual_active
&& VIsual
.lnum
== curwin
->w_cursor
.lnum
)
6516 int len
= (int)STRLEN(ml_get_curline());
6518 if (VIsual
.col
> len
)
6521 # ifdef FEAT_VIRTUALEDIT
6530 #ifdef FEAT_SMARTINDENT
6533 can_si_back
= FALSE
;
6536 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6537 * now in a different buffer. */
6538 if (end_insert_pos
!= NULL
)
6540 curbuf
->b_op_start
= Insstart
;
6541 curbuf
->b_op_end
= *end_insert_pos
;
6546 * Set the last inserted text to a single character.
6547 * Used for the replace command.
6555 vim_free(last_insert
);
6557 last_insert
= alloc(MB_MAXBYTES
* 3 + 5);
6559 last_insert
= alloc(6);
6561 if (last_insert
!= NULL
)
6564 /* Use the CTRL-V only when entering a special char */
6565 if (c
< ' ' || c
== DEL
)
6567 s
= add_char2buf(c
, s
);
6570 last_insert_skip
= 0;
6574 #if defined(EXITFREE) || defined(PROTO)
6578 vim_free(last_insert
);
6580 # ifdef FEAT_INS_EXPAND
6581 vim_free(compl_orig_text
);
6582 compl_orig_text
= NULL
;
6588 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6589 * and CSI. Handle multi-byte characters.
6590 * Returns a pointer to after the added bytes.
6598 char_u temp
[MB_MAXBYTES
];
6602 len
= (*mb_char2bytes
)(c
, temp
);
6603 for (i
= 0; i
< len
; ++i
)
6607 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6631 * move cursor to start of line
6632 * if flags & BL_WHITE move to first non-white
6633 * if flags & BL_SOL move to first non-white if startofline is set,
6634 * otherwise keep "curswant" column
6635 * if flags & BL_FIX don't leave the cursor on a NUL.
6641 if ((flags
& BL_SOL
) && !p_sol
)
6642 coladvance(curwin
->w_curswant
);
6645 curwin
->w_cursor
.col
= 0;
6646 #ifdef FEAT_VIRTUALEDIT
6647 curwin
->w_cursor
.coladd
= 0;
6650 if (flags
& (BL_WHITE
| BL_SOL
))
6654 for (ptr
= ml_get_curline(); vim_iswhite(*ptr
)
6655 && !((flags
& BL_FIX
) && ptr
[1] == NUL
); ++ptr
)
6656 ++curwin
->w_cursor
.col
;
6658 curwin
->w_set_curswant
= TRUE
;
6663 * oneright oneleft cursor_down cursor_up
6665 * Move one char {right,left,down,up}.
6666 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
6667 * Return OK when successful, FAIL when we hit a line of file boundary.
6676 #ifdef FEAT_VIRTUALEDIT
6677 if (virtual_active())
6679 pos_T prevpos
= curwin
->w_cursor
;
6681 /* Adjust for multi-wide char (excluding TAB) */
6682 ptr
= ml_get_cursor();
6683 coladvance(getviscol() + ((*ptr
!= TAB
&& vim_isprintc(
6690 ? ptr2cells(ptr
) : 1));
6691 curwin
->w_set_curswant
= TRUE
;
6692 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6693 return (prevpos
.col
!= curwin
->w_cursor
.col
6694 || prevpos
.coladd
!= curwin
->w_cursor
.coladd
) ? OK
: FAIL
;
6698 ptr
= ml_get_cursor();
6700 return FAIL
; /* already at the very end */
6704 l
= (*mb_ptr2len
)(ptr
);
6709 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6710 * contains "onemore". */
6712 #ifdef FEAT_VIRTUALEDIT
6713 && (ve_flags
& VE_ONEMORE
) == 0
6717 curwin
->w_cursor
.col
+= l
;
6719 curwin
->w_set_curswant
= TRUE
;
6726 #ifdef FEAT_VIRTUALEDIT
6727 if (virtual_active())
6730 int v
= getviscol();
6735 # ifdef FEAT_LINEBREAK
6736 /* We might get stuck on 'showbreak', skip over it. */
6740 coladvance(v
- width
);
6741 /* getviscol() is slow, skip it when 'showbreak' is empty and
6742 * there are no multi-byte characters */
6747 ) || getviscol() < v
)
6755 if (curwin
->w_cursor
.coladd
== 1)
6759 /* Adjust for multi-wide char (not a TAB) */
6760 ptr
= ml_get_cursor();
6761 if (*ptr
!= TAB
&& vim_isprintc(
6767 ) && ptr2cells(ptr
) > 1)
6768 curwin
->w_cursor
.coladd
= 0;
6771 curwin
->w_set_curswant
= TRUE
;
6776 if (curwin
->w_cursor
.col
== 0)
6779 curwin
->w_set_curswant
= TRUE
;
6780 --curwin
->w_cursor
.col
;
6783 /* if the character on the left of the current cursor is a multi-byte
6784 * character, move to its first byte */
6792 cursor_up(n
, upd_topline
)
6794 int upd_topline
; /* When TRUE: update topline */
6800 lnum
= curwin
->w_cursor
.lnum
;
6801 /* This fails if the cursor is already in the first line or the count
6802 * is larger than the line number and '-' is in 'cpoptions' */
6803 if (lnum
<= 1 || (n
>= lnum
&& vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6809 if (hasAnyFolding(curwin
))
6812 * Count each sequence of folded lines as one logical line.
6814 /* go to the the start of the current fold */
6815 (void)hasFolding(lnum
, &lnum
, NULL
);
6819 /* move up one line */
6823 /* If we entered a fold, move to the beginning, unless in
6824 * Insert mode or when 'foldopen' contains "all": it will open
6826 if (n
> 0 || !((State
& INSERT
) || (fdo_flags
& FDO_ALL
)))
6827 (void)hasFolding(lnum
, &lnum
, NULL
);
6835 curwin
->w_cursor
.lnum
= lnum
;
6838 /* try to advance to the column we want to be at */
6839 coladvance(curwin
->w_curswant
);
6842 update_topline(); /* make sure curwin->w_topline is valid */
6848 * Cursor down a number of logical lines.
6851 cursor_down(n
, upd_topline
)
6853 int upd_topline
; /* When TRUE: update topline */
6859 lnum
= curwin
->w_cursor
.lnum
;
6861 /* Move to last line of fold, will fail if it's the end-of-file. */
6862 (void)hasFolding(lnum
, NULL
, &lnum
);
6864 /* This fails if the cursor is already in the last line or would move
6865 * beyound the last line and '-' is in 'cpoptions' */
6866 if (lnum
>= curbuf
->b_ml
.ml_line_count
6867 || (lnum
+ n
> curbuf
->b_ml
.ml_line_count
6868 && vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6870 if (lnum
+ n
>= curbuf
->b_ml
.ml_line_count
)
6871 lnum
= curbuf
->b_ml
.ml_line_count
;
6874 if (hasAnyFolding(curwin
))
6878 /* count each sequence of folded lines as one logical line */
6881 if (hasFolding(lnum
, NULL
, &last
))
6885 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
6888 if (lnum
> curbuf
->b_ml
.ml_line_count
)
6889 lnum
= curbuf
->b_ml
.ml_line_count
;
6894 curwin
->w_cursor
.lnum
= lnum
;
6897 /* try to advance to the column we want to be at */
6898 coladvance(curwin
->w_curswant
);
6901 update_topline(); /* make sure curwin->w_topline is valid */
6907 * Stuff the last inserted text in the read buffer.
6908 * Last_insert actually is a copy of the redo buffer, so we
6909 * first have to remove the command.
6912 stuff_inserted(c
, count
, no_esc
)
6913 int c
; /* Command character to be inserted */
6914 long count
; /* Repeat this many times */
6915 int no_esc
; /* Don't add an ESC at the end */
6922 ptr
= get_last_insert();
6925 EMSG(_(e_noinstext
));
6929 /* may want to stuff the command character, to start Insert mode */
6931 stuffcharReadbuff(c
);
6932 if ((esc_ptr
= (char_u
*)vim_strrchr(ptr
, ESC
)) != NULL
)
6933 *esc_ptr
= NUL
; /* remove the ESC */
6935 /* when the last char is either "0" or "^" it will be quoted if no ESC
6936 * comes after it OR if it will inserted more than once and "ptr"
6937 * starts with ^D. -- Acevedo
6939 last_ptr
= (esc_ptr
? esc_ptr
: ptr
+ STRLEN(ptr
)) - 1;
6940 if (last_ptr
>= ptr
&& (*last_ptr
== '0' || *last_ptr
== '^')
6941 && (no_esc
|| (*ptr
== Ctrl_D
&& count
> 1)))
6950 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6952 stuffReadbuff((char_u
*)(last
== '0'
6953 ? IF_EB("\026\060\064\070", CTRL_V_STR
"xf0")
6954 : IF_EB("\026^", CTRL_V_STR
"^")));
6956 while (--count
> 0);
6961 if (esc_ptr
!= NULL
)
6962 *esc_ptr
= ESC
; /* put the ESC back */
6964 /* may want to stuff a trailing ESC, to get out of Insert mode */
6966 stuffcharReadbuff(ESC
);
6974 if (last_insert
== NULL
)
6976 return last_insert
+ last_insert_skip
;
6980 * Get last inserted string, and remove trailing <Esc>.
6981 * Returns pointer to allocated memory (must be freed) or NULL.
6984 get_last_insert_save()
6989 if (last_insert
== NULL
)
6991 s
= vim_strsave(last_insert
+ last_insert_skip
);
6994 len
= (int)STRLEN(s
);
6995 if (len
> 0 && s
[len
- 1] == ESC
) /* remove trailing ESC */
7002 * Check the word in front of the cursor for an abbreviation.
7003 * Called when the non-id character "c" has been entered.
7004 * When an abbreviation is recognized it is removed from the text and
7005 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7011 /* Don't check for abbreviation in paste mode, when disabled and just
7012 * after moving around with cursor keys. */
7013 if (p_paste
|| no_abbr
|| arrow_used
)
7016 return check_abbr(c
, ml_get_curline(), curwin
->w_cursor
.col
,
7017 curwin
->w_cursor
.lnum
== Insstart
.lnum
? Insstart
.col
: 0);
7021 * replace-stack functions
7023 * When replacing characters, the replaced characters are remembered for each
7024 * new character. This is used to re-insert the old text when backspacing.
7026 * There is a NUL headed list of characters for each character that is
7027 * currently in the file after the insertion point. When BS is used, one NUL
7028 * headed list is put back for the deleted character.
7030 * For a newline, there are two NUL headed lists. One contains the characters
7031 * that the NL replaced. The extra one stores the characters after the cursor
7032 * that were deleted (always white space).
7034 * Replace_offset is normally 0, in which case replace_push will add a new
7035 * character at the end of the stack. If replace_offset is not 0, that many
7036 * characters will be left on the stack above the newly inserted character.
7039 static char_u
*replace_stack
= NULL
;
7040 static long replace_stack_nr
= 0; /* next entry in replace stack */
7041 static long replace_stack_len
= 0; /* max. number of entries */
7045 int c
; /* character that is replaced (NUL is none) */
7049 if (replace_stack_nr
< replace_offset
) /* nothing to do */
7051 if (replace_stack_len
<= replace_stack_nr
)
7053 replace_stack_len
+= 50;
7054 p
= lalloc(sizeof(char_u
) * replace_stack_len
, TRUE
);
7055 if (p
== NULL
) /* out of memory */
7057 replace_stack_len
-= 50;
7060 if (replace_stack
!= NULL
)
7062 mch_memmove(p
, replace_stack
,
7063 (size_t)(replace_stack_nr
* sizeof(char_u
)));
7064 vim_free(replace_stack
);
7068 p
= replace_stack
+ replace_stack_nr
- replace_offset
;
7070 mch_memmove(p
+ 1, p
, (size_t)(replace_offset
* sizeof(char_u
)));
7075 #if defined(FEAT_MBYTE) || defined(PROTO)
7077 * Push a character onto the replace stack. Handles a multi-byte character in
7078 * reverse byte order, so that the first byte is popped off first.
7079 * Return the number of bytes done (includes composing characters).
7085 int l
= (*mb_ptr2len
)(p
);
7088 for (j
= l
- 1; j
>= 0; --j
)
7096 * call replace_push(c) with replace_offset set to the first NUL.
7104 p
= replace_stack
+ replace_stack_nr
;
7105 for (replace_offset
= 1; replace_offset
< replace_stack_nr
;
7115 * Pop one item from the replace stack.
7116 * return -1 if stack empty
7117 * return replaced character or NUL otherwise
7122 if (replace_stack_nr
== 0)
7124 return (int)replace_stack
[--replace_stack_nr
];
7128 * Join the top two items on the replace stack. This removes to "off"'th NUL
7133 int off
; /* offset for which NUL to remove */
7137 for (i
= replace_stack_nr
; --i
>= 0; )
7138 if (replace_stack
[i
] == NUL
&& off
-- <= 0)
7141 mch_memmove(replace_stack
+ i
, replace_stack
+ i
+ 1,
7142 (size_t)(replace_stack_nr
- i
));
7148 * Pop bytes from the replace stack until a NUL is found, and insert them
7149 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7155 int oldState
= State
;
7157 State
= NORMAL
; /* don't want REPLACE here */
7158 while ((cc
= replace_pop()) > 0)
7161 mb_replace_pop_ins(cc
);
7172 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7173 * indicates a multi-byte char, pop the other bytes too.
7176 mb_replace_pop_ins(cc
)
7180 char_u buf
[MB_MAXBYTES
];
7184 if (has_mbyte
&& (n
= MB_BYTE2LEN(cc
)) > 1)
7187 for (i
= 1; i
< n
; ++i
)
7188 buf
[i
] = replace_pop();
7189 ins_bytes_len(buf
, n
);
7195 /* Handle composing chars. */
7199 if (c
== -1) /* stack empty */
7201 if ((n
= MB_BYTE2LEN(c
)) == 1)
7203 /* Not a multi-byte char, put it back. */
7210 for (i
= 1; i
< n
; ++i
)
7211 buf
[i
] = replace_pop();
7212 if (utf_iscomposing(utf_ptr2char(buf
)))
7213 ins_bytes_len(buf
, n
);
7216 /* Not a composing char, put it back. */
7217 for (i
= n
- 1; i
>= 0; --i
)
7218 replace_push(buf
[i
]);
7227 * make the replace stack empty
7228 * (called when exiting replace mode)
7233 vim_free(replace_stack
);
7234 replace_stack
= NULL
;
7235 replace_stack_len
= 0;
7236 replace_stack_nr
= 0;
7240 * Handle doing a BS for one character.
7241 * cc < 0: replace stack empty, just move cursor
7242 * cc == 0: character was inserted, delete it
7243 * cc > 0: character was replaced, put cc (first byte of original char) back
7244 * and check for more characters to be put back
7245 * When "limit_col" is >= 0, don't delete before this column. Matters when
7246 * using composing characters, use del_char_after_col() instead of del_char().
7249 replace_do_bs(limit_col
)
7253 #ifdef FEAT_VREPLACE
7266 #ifdef FEAT_VREPLACE
7267 if (State
& VREPLACE_FLAG
)
7269 /* Get the number of screen cells used by the character we are
7270 * going to delete. */
7271 getvcol(curwin
, &curwin
->w_cursor
, NULL
, &start_vcol
, NULL
);
7272 orig_vcols
= chartabsize(ml_get_cursor(), start_vcol
);
7278 (void)del_char_after_col(limit_col
);
7279 # ifdef FEAT_VREPLACE
7280 if (State
& VREPLACE_FLAG
)
7281 orig_len
= (int)STRLEN(ml_get_cursor());
7289 #ifdef FEAT_VREPLACE
7290 if (State
& VREPLACE_FLAG
)
7291 orig_len
= (int)STRLEN(ml_get_cursor()) - 1;
7296 #ifdef FEAT_VREPLACE
7297 if (State
& VREPLACE_FLAG
)
7299 /* Get the number of screen cells used by the inserted characters */
7300 p
= ml_get_cursor();
7301 ins_len
= (int)STRLEN(p
) - orig_len
;
7303 for (i
= 0; i
< ins_len
; ++i
)
7305 vcol
+= chartabsize(p
+ i
, vcol
);
7307 i
+= (*mb_ptr2len
)(p
) - 1;
7312 /* Delete spaces that were inserted after the cursor to keep the
7314 curwin
->w_cursor
.col
+= ins_len
;
7315 while (vcol
> orig_vcols
&& gchar_cursor() == ' ')
7320 curwin
->w_cursor
.col
-= ins_len
;
7324 /* mark the buffer as changed and prepare for displaying */
7325 changed_bytes(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
7328 (void)del_char_after_col(limit_col
);
7333 * Return TRUE if C-indenting is on.
7338 return (!p_paste
&& (curbuf
->b_p_cin
7340 || *curbuf
->b_p_inde
!= NUL
7346 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7348 * Re-indent the current line, based on the current contents of it and the
7349 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7350 * confused what all the part that handles Control-T is doing that I'm not.
7351 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7355 fixthisline(get_the_indent
)
7356 int (*get_the_indent
) __ARGS((void));
7358 change_indent(INDENT_SET
, get_the_indent(), FALSE
, 0, TRUE
);
7359 if (linewhite(curwin
->w_cursor
.lnum
))
7360 did_ai
= TRUE
; /* delete the indent if the line stays empty */
7369 if (curbuf
->b_p_lisp
&& curbuf
->b_p_ai
)
7370 fixthisline(get_lisp_indent
);
7372 # if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7375 # ifdef FEAT_CINDENT
7385 * return TRUE if 'cinkeys' contains the key "keytyped",
7386 * when == '*': Only if key is preceded with '*' (indent before insert)
7387 * when == '!': Only if key is prededed with '!' (don't insert)
7388 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7390 * "keytyped" can have a few special values:
7393 * KEY_COMPLETE just finished completion.
7395 * If line_is_empty is TRUE accept keys with '0' before them.
7398 in_cinkeys(keytyped
, when
, line_is_empty
)
7411 if (keytyped
== NUL
)
7412 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7416 if (*curbuf
->b_p_inde
!= NUL
)
7417 look
= curbuf
->b_p_indk
; /* 'indentexpr' set: use 'indentkeys' */
7420 look
= curbuf
->b_p_cink
; /* 'indentexpr' empty: use 'cinkeys' */
7424 * Find out if we want to try a match with this key, depending on
7425 * 'when' and a '*' or '!' before the key.
7429 case '*': try_match
= (*look
== '*'); break;
7430 case '!': try_match
= (*look
== '!'); break;
7431 default: try_match
= (*look
!= '*'); break;
7433 if (*look
== '*' || *look
== '!')
7437 * If there is a '0', only accept a match if the line is empty.
7438 * But may still match when typing last char of a word.
7442 try_match_word
= try_match
;
7448 try_match_word
= FALSE
;
7451 * does it look like a control character?
7455 && (Ctrl_chr(look
[1]) != 0)
7457 && look
[1] >= '?' && look
[1] <= '_'
7461 if (try_match
&& keytyped
== Ctrl_chr(look
[1]))
7466 * 'o' means "o" command, open forward.
7467 * 'O' means "O" command, open backward.
7469 else if (*look
== 'o')
7471 if (try_match
&& keytyped
== KEY_OPEN_FORW
)
7475 else if (*look
== 'O')
7477 if (try_match
&& keytyped
== KEY_OPEN_BACK
)
7483 * 'e' means to check for "else" at start of line and just before the
7486 else if (*look
== 'e')
7488 if (try_match
&& keytyped
== 'e' && curwin
->w_cursor
.col
>= 4)
7490 p
= ml_get_curline();
7491 if (skipwhite(p
) == p
+ curwin
->w_cursor
.col
- 4 &&
7492 STRNCMP(p
+ curwin
->w_cursor
.col
- 4, "else", 4) == 0)
7499 * ':' only causes an indent if it is at the end of a label or case
7500 * statement, or when it was before typing the ':' (to fix
7501 * class::method for C++).
7503 else if (*look
== ':')
7505 if (try_match
&& keytyped
== ':')
7507 p
= ml_get_curline();
7508 if (cin_iscase(p
) || cin_isscopedecl(p
) || cin_islabel(30))
7510 /* Need to get the line again after cin_islabel(). */
7511 p
= ml_get_curline();
7512 if (curwin
->w_cursor
.col
> 2
7513 && p
[curwin
->w_cursor
.col
- 1] == ':'
7514 && p
[curwin
->w_cursor
.col
- 2] == ':')
7516 p
[curwin
->w_cursor
.col
- 1] = ' ';
7517 i
= (cin_iscase(p
) || cin_isscopedecl(p
)
7518 || cin_islabel(30));
7519 p
= ml_get_curline();
7520 p
[curwin
->w_cursor
.col
- 1] = ':';
7530 * Is it a key in <>, maybe?
7532 else if (*look
== '<')
7537 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7538 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7539 * >, *, : and ! keys if they really really want to.
7541 if (vim_strchr((char_u
*)"<>!*oOe0:", look
[1]) != NULL
7542 && keytyped
== look
[1])
7545 if (keytyped
== get_special_key_code(look
+ 1))
7548 while (*look
&& *look
!= '>')
7550 while (*look
== '>')
7555 * Is it a word: "=word"?
7557 else if (*look
== '=' && look
[1] != ',' && look
[1] != NUL
)
7567 p
= vim_strchr(look
, ',');
7569 p
= look
+ STRLEN(look
);
7570 if ((try_match
|| try_match_word
)
7571 && curwin
->w_cursor
.col
>= (colnr_T
)(p
- look
))
7575 #ifdef FEAT_INS_EXPAND
7576 if (keytyped
== KEY_COMPLETE
)
7580 /* Just completed a word, check if it starts with "look".
7581 * search back for the start of a word. */
7582 line
= ml_get_curline();
7588 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; s
= n
)
7590 n
= mb_prevptr(line
, s
);
7591 if (!vim_iswordp(n
))
7597 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; --s
)
7598 if (!vim_iswordc(s
[-1]))
7600 if (s
+ (p
- look
) <= line
+ curwin
->w_cursor
.col
7602 ? MB_STRNICMP(s
, look
, p
- look
)
7603 : STRNCMP(s
, look
, p
- look
)) == 0)
7608 /* TODO: multi-byte */
7609 if (keytyped
== (int)p
[-1] || (icase
&& keytyped
< 256
7610 && TOLOWER_LOC(keytyped
) == TOLOWER_LOC((int)p
[-1])))
7612 line
= ml_get_cursor();
7613 if ((curwin
->w_cursor
.col
== (colnr_T
)(p
- look
)
7614 || !vim_iswordc(line
[-(p
- look
) - 1]))
7616 ? MB_STRNICMP(line
- (p
- look
), look
, p
- look
)
7617 : STRNCMP(line
- (p
- look
), look
, p
- look
))
7621 if (match
&& try_match_word
&& !try_match
)
7623 /* "0=word": Check if there are only blanks before the
7625 line
= ml_get_curline();
7626 if ((int)(skipwhite(line
) - line
) !=
7627 (int)(curwin
->w_cursor
.col
- (p
- look
)))
7637 * ok, it's a boring generic character.
7641 if (try_match
&& *look
== keytyped
)
7649 look
= skip_to_option_part(look
);
7653 #endif /* FEAT_CINDENT */
7655 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7657 * Map Hebrew keyboard when in hkmap mode.
7663 if (p_hkmapp
) /* phonetic mapping, by Ilya Dogolazky */
7665 enum {hALEF
=0, BET
, GIMEL
, DALET
, HEI
, VAV
, ZAIN
, HET
, TET
, IUD
,
7666 KAFsofit
, hKAF
, LAMED
, MEMsofit
, MEM
, NUNsofit
, NUN
, SAMEH
, AIN
,
7667 PEIsofit
, PEI
, ZADIsofit
, ZADI
, KOF
, RESH
, hSHIN
, TAV
};
7668 static char_u map
[26] =
7669 {(char_u
)hALEF
/*a*/, (char_u
)BET
/*b*/, (char_u
)hKAF
/*c*/,
7670 (char_u
)DALET
/*d*/, (char_u
)-1 /*e*/, (char_u
)PEIsofit
/*f*/,
7671 (char_u
)GIMEL
/*g*/, (char_u
)HEI
/*h*/, (char_u
)IUD
/*i*/,
7672 (char_u
)HET
/*j*/, (char_u
)KOF
/*k*/, (char_u
)LAMED
/*l*/,
7673 (char_u
)MEM
/*m*/, (char_u
)NUN
/*n*/, (char_u
)SAMEH
/*o*/,
7674 (char_u
)PEI
/*p*/, (char_u
)-1 /*q*/, (char_u
)RESH
/*r*/,
7675 (char_u
)ZAIN
/*s*/, (char_u
)TAV
/*t*/, (char_u
)TET
/*u*/,
7676 (char_u
)VAV
/*v*/, (char_u
)hSHIN
/*w*/, (char_u
)-1 /*x*/,
7677 (char_u
)AIN
/*y*/, (char_u
)ZADI
/*z*/};
7679 if (c
== 'N' || c
== 'M' || c
== 'P' || c
== 'C' || c
== 'Z')
7680 return (int)(map
[CharOrd(c
)] - 1 + p_aleph
);
7685 return '\''; /* {geresh}={'} */
7687 return ' '; /* \"o --> ' ' for a german keyboard */
7689 return ' '; /* \"a --> ' ' -- / -- */
7691 return ' '; /* \"u --> ' ' -- / -- */
7693 else if (islower(c
))
7695 /* NOTE: islower() does not do the right thing for us on Linux so we
7696 * do this the same was as 5.7 and previous, so it works correctly on
7697 * all systems. Specifically, the e.g. Delete and Arrow keys are
7698 * munged and won't work if e.g. searching for Hebrew text.
7700 else if (c
>= 'a' && c
<= 'z')
7702 return (int)(map
[CharOrdLow(c
)] + p_aleph
);
7710 case '`': return ';';
7711 case '/': return '.';
7712 case '\'': return ',';
7713 case 'q': return '/';
7714 case 'w': return '\'';
7716 /* Hebrew letters - set offset from 'a' */
7717 case ',': c
= '{'; break;
7718 case '.': c
= 'v'; break;
7719 case ';': c
= 't'; break;
7721 static char str
[] = "zqbcxlsjphmkwonu ydafe rig";
7724 /* see note about islower() above */
7727 if (c
< 'a' || c
> 'z')
7730 c
= str
[CharOrdLow(c
)];
7735 return (int)(CharOrdLow(c
) + p_aleph
);
7743 int need_redraw
= FALSE
;
7747 int vis_active
= VIsual_active
;
7751 * If we are going to wait for a character, show a '"'.
7753 pc_status
= PC_STATUS_UNSET
;
7754 if (redrawing() && !char_avail())
7756 /* may need to redraw when no more chars available now */
7759 edit_putchar('"', TRUE
);
7760 #ifdef FEAT_CMDL_INFO
7761 add_to_showcmd_c(Ctrl_R
);
7765 #ifdef USE_ON_FLY_SCROLL
7766 dont_scroll
= TRUE
; /* disallow scrolling here */
7770 * Don't map the register name. This also prevents the mode message to be
7771 * deleted when ESC is hit.
7774 regname
= plain_vgetc();
7775 LANGMAP_ADJUST(regname
, TRUE
);
7776 if (regname
== Ctrl_R
|| regname
== Ctrl_O
|| regname
== Ctrl_P
)
7778 /* Get a third key for literal register insertion */
7779 literally
= regname
;
7780 #ifdef FEAT_CMDL_INFO
7781 add_to_showcmd_c(literally
);
7783 regname
= plain_vgetc();
7784 LANGMAP_ADJUST(regname
, TRUE
);
7790 * Don't call u_sync() while getting the expression,
7791 * evaluating it or giving an error message for it!
7796 # ifdef USE_IM_CONTROL
7797 int im_on
= im_get_status();
7799 regname
= get_expr_register();
7800 # ifdef USE_IM_CONTROL
7801 /* Restore the Input Method. */
7803 im_set_active(TRUE
);
7806 if (regname
== NUL
|| !valid_yank_reg(regname
, FALSE
))
7809 need_redraw
= TRUE
; /* remove the '"' */
7814 if (literally
== Ctrl_O
|| literally
== Ctrl_P
)
7816 /* Append the command to the redo buffer. */
7817 AppendCharToRedobuff(Ctrl_R
);
7818 AppendCharToRedobuff(literally
);
7819 AppendCharToRedobuff(regname
);
7821 do_put(regname
, BACKWARD
, 1L,
7822 (literally
== Ctrl_P
? PUT_FIXINDENT
: 0) | PUT_CURSEND
);
7824 else if (insert_reg(regname
, literally
) == FAIL
)
7827 need_redraw
= TRUE
; /* remove the '"' */
7829 else if (stop_insert_mode
)
7830 /* When the '=' register was used and a function was invoked that
7831 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7832 * insert anything, need to remove the '"' */
7839 #ifdef FEAT_CMDL_INFO
7843 /* If the inserted register is empty, we need to remove the '"' */
7844 if (need_redraw
|| stuff_empty())
7848 /* Disallow starting Visual mode here, would get a weird mode. */
7849 if (!vis_active
&& VIsual_active
)
7855 * CTRL-G commands in Insert mode.
7862 #ifdef FEAT_INS_EXPAND
7863 /* Right after CTRL-X the cursor will be after the ruler. */
7868 * Don't map the second key. This also prevents the mode message to be
7869 * deleted when ESC is hit.
7876 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7879 case 'k': ins_up(TRUE
);
7882 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7885 case 'j': ins_down(TRUE
);
7888 /* CTRL-G u: start new undoable edit */
7889 case 'u': u_sync(TRUE
);
7890 ins_need_undo
= TRUE
;
7892 /* Need to reset Insstart, esp. because a BS that joins
7893 * a line to the previous one must save for undo. */
7894 Insstart
= curwin
->w_cursor
;
7897 /* Unknown CTRL-G command, reserved for future expansion. */
7898 default: vim_beep();
7903 * CTRL-^ in Insert mode.
7908 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
7910 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7911 if (State
& LANGMAP
)
7913 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7918 curbuf
->b_p_iminsert
= B_IMODE_LMAP
;
7920 #ifdef USE_IM_CONTROL
7921 im_set_active(FALSE
);
7925 #ifdef USE_IM_CONTROL
7928 /* There are no ":lmap" mappings, toggle IM */
7929 if (im_get_status())
7931 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7932 im_set_active(FALSE
);
7936 curbuf
->b_p_iminsert
= B_IMODE_IM
;
7938 im_set_active(TRUE
);
7942 set_iminsert_global();
7945 /* may show different cursor shape or color */
7947 gui_update_cursor(TRUE
, FALSE
);
7949 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7950 /* Show/unshow value of 'keymap' in status lines. */
7951 status_redraw_curbuf();
7956 * Handle ESC in insert mode.
7957 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7961 ins_esc(count
, cmdchar
, nomove
)
7964 int nomove
; /* don't move cursor */
7967 static int disabled_redraw
= FALSE
;
7970 check_spell_redraw();
7972 #if defined(FEAT_HANGULIN)
7973 # if defined(ESC_CHG_TO_ENG_MODE)
7974 hangul_input_state_set(0);
7976 if (composing_hangul
)
7978 push_raw_key(composing_hangul_buffer
, 2);
7979 composing_hangul
= 0;
7983 temp
= curwin
->w_cursor
.col
;
7984 if (disabled_redraw
)
7986 --RedrawingDisabled
;
7987 disabled_redraw
= FALSE
;
7992 * Don't append the ESC for "r<CR>" and "grx".
7993 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7994 * when "count" is non-zero.
7996 if (cmdchar
!= 'r' && cmdchar
!= 'v')
7997 AppendToRedobuff(p_im
? (char_u
*)"\014" : ESC_STR
);
8000 * Repeating insert may take a long time. Check for
8001 * interrupt now and then.
8010 if (--*count
> 0) /* repeat what was typed */
8012 /* Vi repeats the insert without replacing characters. */
8013 if (vim_strchr(p_cpo
, CPO_REPLCNT
) != NULL
)
8014 State
&= ~REPLACE_FLAG
;
8016 (void)start_redo_ins();
8017 if (cmdchar
== 'r' || cmdchar
== 'v')
8018 stuffReadbuff(ESC_STR
); /* no ESC in redo buffer */
8019 ++RedrawingDisabled
;
8020 disabled_redraw
= TRUE
;
8021 return FALSE
; /* repeat the insert */
8023 stop_insert(&curwin
->w_cursor
, TRUE
);
8027 /* When an autoindent was removed, curswant stays after the
8029 if (restart_edit
== NUL
&& (colnr_T
)temp
== curwin
->w_cursor
.col
)
8030 curwin
->w_set_curswant
= TRUE
;
8032 /* Remember the last Insert position in the '^ mark. */
8033 if (!cmdmod
.keepjumps
)
8034 curbuf
->b_last_insert
= curwin
->w_cursor
;
8037 * The cursor should end up on the last inserted character.
8038 * Don't do it for CTRL-O, unless past the end of the line.
8041 && (curwin
->w_cursor
.col
!= 0
8042 #ifdef FEAT_VIRTUALEDIT
8043 || curwin
->w_cursor
.coladd
> 0
8046 && (restart_edit
== NUL
8047 || (gchar_cursor() == NUL
8052 #ifdef FEAT_RIGHTLEFT
8057 #ifdef FEAT_VIRTUALEDIT
8058 if (curwin
->w_cursor
.coladd
> 0 || ve_flags
== VE_ALL
)
8061 if (restart_edit
!= NUL
)
8062 ++curwin
->w_cursor
.coladd
;
8067 --curwin
->w_cursor
.col
;
8069 /* Correct cursor for multi-byte character. */
8076 #ifdef USE_IM_CONTROL
8077 /* Disable IM to allow typing English directly for Normal mode commands.
8078 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8080 if (!(State
& LANGMAP
))
8081 im_save_status(&curbuf
->b_p_iminsert
);
8082 im_set_active(FALSE
);
8086 /* need to position cursor again (e.g. when on a TAB ) */
8087 changed_cline_bef_curs();
8093 ui_cursor_shape(); /* may show different cursor shape */
8097 * When recording or for CTRL-O, need to display the new mode.
8098 * Otherwise remove the mode message.
8100 if (Recording
|| restart_edit
!= NUL
)
8105 return TRUE
; /* exit Insert mode */
8108 #ifdef FEAT_RIGHTLEFT
8110 * Toggle language: hkmap and revins_on.
8111 * Move to end of reverse inserted text.
8116 if (revins_on
&& revins_chars
&& revins_scol
>= 0)
8118 while (gchar_cursor() != NUL
&& revins_chars
--)
8119 ++curwin
->w_cursor
.col
;
8122 revins_on
= (State
== INSERT
&& p_ri
);
8125 revins_scol
= curwin
->w_cursor
.col
;
8136 * to be consistent also for redo command, using '.'
8137 * set arrow_used to true and stop it - causing to redo
8138 * characters entered in one mode (normal/reverse insert).
8142 p_fkmap
= curwin
->w_p_rl
^ p_ri
;
8143 if (p_fkmap
&& p_ri
)
8148 p_hkmap
= curwin
->w_p_rl
^ p_ri
; /* be consistent! */
8155 * If 'keymodel' contains "startsel", may start selection.
8156 * Returns TRUE when a CTRL-O and other keys stuffed.
8179 if (!(mod_mask
& MOD_MASK_SHIFT
))
8188 /* Start selection right away, the cursor can move with
8189 * CTRL-O when beyond the end of the line. */
8192 /* Execute the key in (insert) Select mode. */
8193 stuffcharReadbuff(Ctrl_O
);
8199 buf
[1] = KS_MODIFIER
;
8204 stuffcharReadbuff(c
);
8212 * <Insert> key in Insert mode: toggle insert/remplace mode.
8215 ins_insert(replaceState
)
8219 if (p_fkmap
&& p_ri
)
8222 EMSG(farsi_text_3
); /* encoded in Farsi */
8229 set_vim_var_string(VV_INSERTMODE
,
8230 (char_u
*)((State
& REPLACE_FLAG
) ? "i" :
8231 # ifdef FEAT_VREPLACE
8232 replaceState
== VREPLACE
? "v" :
8236 apply_autocmds(EVENT_INSERTCHANGE
, NULL
, NULL
, FALSE
, curbuf
);
8238 if (State
& REPLACE_FLAG
)
8239 State
= INSERT
| (State
& LANGMAP
);
8241 State
= replaceState
| (State
& LANGMAP
);
8242 AppendCharToRedobuff(K_INS
);
8245 ui_cursor_shape(); /* may show different cursor shape */
8250 * Pressed CTRL-O in Insert mode.
8255 #ifdef FEAT_VREPLACE
8256 if (State
& VREPLACE_FLAG
)
8260 if (State
& REPLACE_FLAG
)
8264 #ifdef FEAT_VIRTUALEDIT
8265 if (virtual_active())
8266 ins_at_eol
= FALSE
; /* cursor always keeps its column */
8269 ins_at_eol
= (gchar_cursor() == NUL
);
8273 * If the cursor is on an indent, ^T/^D insert/delete one
8274 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8275 * Always round the indent to 'shiftwidth', this is compatible
8276 * with vi. But vi only supports ^T and ^D after an
8277 * autoindent, we support it everywhere.
8284 if (stop_arrow() == FAIL
)
8286 AppendCharToRedobuff(c
);
8289 * 0^D and ^^D: remove all indent.
8291 if (c
== Ctrl_D
&& (lastc
== '0' || lastc
== '^')
8292 && curwin
->w_cursor
.col
> 0)
8294 --curwin
->w_cursor
.col
;
8295 (void)del_char(FALSE
); /* delete the '^' or '0' */
8296 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8297 if (State
& REPLACE_FLAG
)
8300 old_indent
= get_indent(); /* remember curr. indent */
8301 change_indent(INDENT_SET
, 0, TRUE
, 0, TRUE
);
8304 change_indent(c
== Ctrl_D
? INDENT_DEC
: INDENT_INC
, 0, TRUE
, 0, TRUE
);
8306 if (did_ai
&& *skipwhite(ml_get_curline()) != NUL
)
8308 #ifdef FEAT_SMARTINDENT
8311 can_si_back
= FALSE
;
8314 can_cindent
= FALSE
; /* no cindenting after ^D or ^T */
8323 if (stop_arrow() == FAIL
)
8325 if (gchar_cursor() == NUL
) /* delete newline */
8327 temp
= curwin
->w_cursor
.col
;
8328 if (!can_bs(BS_EOL
) /* only if "eol" included */
8329 || u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
8330 (linenr_T
)(curwin
->w_cursor
.lnum
+ 2)) == FAIL
8331 || do_join(FALSE
) == FAIL
)
8334 curwin
->w_cursor
.col
= temp
;
8336 else if (del_char(FALSE
) == FAIL
) /* delete char under cursor */
8339 #ifdef FEAT_SMARTINDENT
8342 can_si_back
= FALSE
;
8344 AppendCharToRedobuff(K_DEL
);
8347 static void ins_bs_one
__ARGS((colnr_T
*vcolp
));
8350 * Delete one character for ins_bs().
8357 getvcol(curwin
, &curwin
->w_cursor
, vcolp
, NULL
, NULL
);
8358 if (State
& REPLACE_FLAG
)
8360 /* Don't delete characters before the insert point when in
8362 if (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8363 || curwin
->w_cursor
.col
>= Insstart
.col
)
8367 (void)del_char(FALSE
);
8371 * Handle Backspace, delete-word and delete-line in Insert mode.
8372 * Return TRUE when backspace was actually used.
8375 ins_bs(c
, mode
, inserted_space_p
)
8378 int *inserted_space_p
;
8382 int temp
= 0; /* init for GCC */
8385 int did_backspace
= FALSE
;
8389 int cpc
[MAX_MCO
]; /* composing characters */
8393 * can't delete anything in an empty file
8394 * can't backup past first character in buffer
8395 * can't backup past starting point unless 'backspace' > 1
8396 * can backup to a previous line if 'backspace' == 0
8400 #ifdef FEAT_RIGHTLEFT
8403 ((curwin
->w_cursor
.lnum
== 1 && curwin
->w_cursor
.col
== 0)
8404 || (!can_bs(BS_START
)
8406 || (curwin
->w_cursor
.lnum
== Insstart
.lnum
8407 && curwin
->w_cursor
.col
<= Insstart
.col
)))
8408 || (!can_bs(BS_INDENT
) && !arrow_used
&& ai_col
> 0
8409 && curwin
->w_cursor
.col
<= ai_col
)
8410 || (!can_bs(BS_EOL
) && curwin
->w_cursor
.col
== 0))))
8416 if (stop_arrow() == FAIL
)
8418 in_indent
= inindent(0);
8421 can_cindent
= FALSE
;
8423 #ifdef FEAT_COMMENTS
8424 end_comment_pending
= NUL
; /* After BS, don't auto-end comment */
8426 #ifdef FEAT_RIGHTLEFT
8427 if (revins_on
) /* put cursor after last inserted char */
8431 #ifdef FEAT_VIRTUALEDIT
8433 * BACKSPACE_CHAR eats a virtual space
8434 * BACKSPACE_WORD eats all coladd
8435 * BACKSPACE_LINE eats all coladd and keeps going
8437 if (curwin
->w_cursor
.coladd
> 0)
8439 if (mode
== BACKSPACE_CHAR
)
8441 --curwin
->w_cursor
.coladd
;
8444 if (mode
== BACKSPACE_WORD
)
8446 curwin
->w_cursor
.coladd
= 0;
8449 curwin
->w_cursor
.coladd
= 0;
8456 if (curwin
->w_cursor
.col
== 0)
8458 lnum
= Insstart
.lnum
;
8459 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8460 #ifdef FEAT_RIGHTLEFT
8465 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 2),
8466 (linenr_T
)(curwin
->w_cursor
.lnum
+ 1)) == FAIL
)
8469 Insstart
.col
= MAXCOL
;
8473 * cc < 0: NL was inserted, delete it
8474 * cc >= 0: NL was replaced, put original characters back
8477 if (State
& REPLACE_FLAG
)
8478 cc
= replace_pop(); /* returns -1 if NL was inserted */
8480 * In replace mode, in the line we started replacing, we only move the
8483 if ((State
& REPLACE_FLAG
) && curwin
->w_cursor
.lnum
<= lnum
)
8489 #ifdef FEAT_VREPLACE
8490 if (!(State
& VREPLACE_FLAG
)
8491 || curwin
->w_cursor
.lnum
> orig_line_count
)
8494 temp
= gchar_cursor(); /* remember current char */
8495 --curwin
->w_cursor
.lnum
;
8497 /* When "aw" is in 'formatoptions' we must delete the space at
8498 * the end of the line, otherwise the line will be broken
8499 * again when auto-formatting. */
8500 if (has_format_option(FO_AUTO
)
8501 && has_format_option(FO_WHITE_PAR
))
8503 char_u
*ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
,
8507 len
= (int)STRLEN(ptr
);
8508 if (len
> 0 && ptr
[len
- 1] == ' ')
8512 (void)do_join(FALSE
);
8513 if (temp
== NUL
&& gchar_cursor() != NUL
)
8516 #ifdef FEAT_VREPLACE
8522 * In REPLACE mode we have to put back the text that was replaced
8523 * by the NL. On the replace stack is first a NUL-terminated
8524 * sequence of characters that were deleted and then the
8525 * characters that NL replaced.
8527 if (State
& REPLACE_FLAG
)
8530 * Do the next ins_char() in NORMAL state, to
8531 * prevent ins_char() from replacing characters and
8532 * avoiding showmatch().
8537 * restore characters (blanks) deleted after cursor
8541 save_col
= curwin
->w_cursor
.col
;
8543 mb_replace_pop_ins(cc
);
8547 curwin
->w_cursor
.col
= save_col
;
8550 /* restore the characters that NL replaced */
8560 * Delete character(s) before the cursor.
8562 #ifdef FEAT_RIGHTLEFT
8563 if (revins_on
) /* put cursor on last inserted char */
8568 if (mode
== BACKSPACE_LINE
8574 #ifdef FEAT_RIGHTLEFT
8579 save_col
= curwin
->w_cursor
.col
;
8580 beginline(BL_WHITE
);
8581 if (curwin
->w_cursor
.col
< save_col
)
8582 mincol
= curwin
->w_cursor
.col
;
8583 curwin
->w_cursor
.col
= save_col
;
8587 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8589 if ( mode
== BACKSPACE_CHAR
8590 && ((p_sta
&& in_indent
)
8593 (tabstop_count(curbuf
->b_p_vsts_ary
) != 0
8594 || curbuf
->b_p_sts
!= 0)
8596 curbuf
->b_p_sts
!= 0
8598 && curwin
->w_cursor
.col
> 0
8599 && (*(ml_get_cursor() - 1) == TAB
8600 || (*(ml_get_cursor() - 1) == ' '
8601 && (!*inserted_space_p
8604 #ifndef FEAT_VARTABS
8611 *inserted_space_p
= FALSE
;
8612 #ifndef FEAT_VARTABS
8613 if (p_sta
&& in_indent
)
8614 ts
= curbuf
->b_p_sw
;
8616 ts
= curbuf
->b_p_sts
;
8618 /* Compute the virtual column where we want to be. Since
8619 * 'showbreak' may get in the way, need to get the last column of
8620 * the previous character. */
8621 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8624 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &want_vcol
);
8627 if (p_sta
&& in_indent
)
8628 want_vcol
= (want_vcol
/ curbuf
->b_p_sw
) * curbuf
->b_p_sw
;
8630 want_vcol
= tabstop_start(want_vcol
, curbuf
->b_p_sts
,
8631 curbuf
->b_p_vsts_ary
);
8633 want_vcol
= (want_vcol
/ ts
) * ts
;
8636 /* delete characters until we are at or before want_vcol */
8637 while (vcol
> want_vcol
8638 && (cc
= *(ml_get_cursor() - 1), vim_iswhite(cc
)))
8641 /* insert extra spaces until we are at want_vcol */
8642 while (vcol
< want_vcol
)
8644 /* Remember the first char we inserted */
8645 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8646 && curwin
->w_cursor
.col
< Insstart
.col
)
8647 Insstart
.col
= curwin
->w_cursor
.col
;
8649 #ifdef FEAT_VREPLACE
8650 if (State
& VREPLACE_FLAG
)
8655 ins_str((char_u
*)" ");
8656 if ((State
& REPLACE_FLAG
))
8659 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8662 /* If we are now back where we started delete one character. Can
8663 * happen when using 'sts' and 'linebreak'. */
8664 if (vcol
>= start_vcol
)
8669 * Delete upto starting point, start of line or previous word.
8673 #ifdef FEAT_RIGHTLEFT
8674 if (!revins_on
) /* put cursor on char to be deleted */
8678 /* start of word? */
8679 if (mode
== BACKSPACE_WORD
&& !vim_isspace(gchar_cursor()))
8681 mode
= BACKSPACE_WORD_NOT_SPACE
;
8682 temp
= vim_iswordc(gchar_cursor());
8685 else if (mode
== BACKSPACE_WORD_NOT_SPACE
8686 && (vim_isspace(cc
= gchar_cursor())
8687 || vim_iswordc(cc
) != temp
))
8689 #ifdef FEAT_RIGHTLEFT
8693 #ifdef FEAT_RIGHTLEFT
8694 else if (State
& REPLACE_FLAG
)
8699 if (State
& REPLACE_FLAG
)
8704 if (enc_utf8
&& p_deco
)
8705 (void)utfc_ptr2char(ml_get_cursor(), cpc
);
8707 (void)del_char(FALSE
);
8710 * If there are combining characters and 'delcombine' is set
8711 * move the cursor back. Don't back up before the base
8714 if (enc_utf8
&& p_deco
&& cpc
[0] != NUL
)
8717 #ifdef FEAT_RIGHTLEFT
8723 if (revins_on
&& gchar_cursor() == NUL
)
8727 /* Just a single backspace?: */
8728 if (mode
== BACKSPACE_CHAR
)
8731 #ifdef FEAT_RIGHTLEFT
8734 (curwin
->w_cursor
.col
> mincol
8735 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8736 || curwin
->w_cursor
.col
!= Insstart
.col
)));
8737 did_backspace
= TRUE
;
8739 #ifdef FEAT_SMARTINDENT
8742 can_si_back
= FALSE
;
8744 if (curwin
->w_cursor
.col
<= 1)
8747 * It's a little strange to put backspaces into the redo
8748 * buffer, but it makes auto-indent a lot easier to deal
8751 AppendCharToRedobuff(c
);
8753 /* If deleted before the insertion point, adjust it */
8754 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8755 && curwin
->w_cursor
.col
< Insstart
.col
)
8756 Insstart
.col
= curwin
->w_cursor
.col
;
8758 /* vi behaviour: the cursor moves backward but the character that
8759 * was there remains visible
8760 * Vim behaviour: the cursor moves backward and the character that
8761 * was there is erased from the screen.
8762 * We can emulate the vi behaviour by pretending there is a dollar
8763 * displayed even when there isn't.
8764 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8765 if (vim_strchr(p_cpo
, CPO_BACKSPACE
) != NULL
&& dollar_vcol
== 0)
8766 dollar_vcol
= curwin
->w_virtcol
;
8769 /* When deleting a char the cursor line must never be in a closed fold.
8770 * E.g., when 'foldmethod' is indent and deleting the first non-white
8771 * char before a Tab. */
8776 return did_backspace
;
8785 win_T
*old_curwin
= curwin
;
8788 /* When GUI is active, also move/paste when 'mouse' is empty */
8791 if (!mouse_has(MOUSE_INSERT
))
8795 tpos
= curwin
->w_cursor
;
8796 if (do_mouse(NULL
, c
, BACKWARD
, 1L, 0))
8799 win_T
*new_curwin
= curwin
;
8801 if (curwin
!= old_curwin
&& win_valid(old_curwin
))
8803 /* Mouse took us to another window. We need to go back to the
8804 * previous one to stop insert there properly. */
8805 curwin
= old_curwin
;
8806 curbuf
= curwin
->w_buffer
;
8809 start_arrow(curwin
== old_curwin
? &tpos
: NULL
);
8811 if (curwin
!= new_curwin
&& win_valid(new_curwin
))
8813 curwin
= new_curwin
;
8814 curbuf
= curwin
->w_buffer
;
8817 # ifdef FEAT_CINDENT
8823 /* redraw status lines (in case another window became active) */
8824 redraw_statuslines();
8833 # if defined(FEAT_WINDOWS)
8834 win_T
*old_curwin
= curwin
;
8836 # ifdef FEAT_INS_EXPAND
8837 int did_scroll
= FALSE
;
8840 tpos
= curwin
->w_cursor
;
8842 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8843 /* Currently the mouse coordinates are only known in the GUI. */
8844 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
8851 /* find the window at the pointer coordinates */
8852 curwin
= mouse_find_win(&row
, &col
);
8853 curbuf
= curwin
->w_buffer
;
8855 if (curwin
== old_curwin
)
8859 # ifdef FEAT_INS_EXPAND
8860 /* Don't scroll the window in which completion is being done. */
8862 # if defined(FEAT_WINDOWS)
8863 || curwin
!= old_curwin
8868 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
8869 scroll_redraw(up
, (long)(curwin
->w_botline
- curwin
->w_topline
));
8871 scroll_redraw(up
, 3L);
8872 # ifdef FEAT_INS_EXPAND
8877 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8878 curwin
->w_redr_status
= TRUE
;
8880 curwin
= old_curwin
;
8881 curbuf
= curwin
->w_buffer
;
8884 # ifdef FEAT_INS_EXPAND
8885 /* The popup menu may overlay the window, need to redraw it.
8886 * TODO: Would be more efficient to only redraw the windows that are
8887 * overlapped by the popup menu. */
8888 if (pum_visible() && did_scroll
)
8890 redraw_all_later(NOT_VALID
);
8891 ins_compl_show_pum();
8895 if (!equalpos(curwin
->w_cursor
, tpos
))
8898 # ifdef FEAT_CINDENT
8905 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8910 /* We will be leaving the current window, unless closing another tab. */
8911 if (c
!= K_TABMENU
|| current_tabmenu
!= TABLINE_MENU_CLOSE
8912 || (current_tab
!= 0 && current_tab
!= tabpage_index(curtab
)))
8915 start_arrow(&curwin
->w_cursor
);
8916 # ifdef FEAT_CINDENT
8922 goto_tabpage(current_tab
);
8926 redraw_statuslines(); /* will redraw the tabline when needed */
8931 #if defined(FEAT_GUI) || defined(PROTO)
8938 tpos
= curwin
->w_cursor
;
8939 if (gui_do_scroll())
8942 # ifdef FEAT_CINDENT
8954 tpos
= curwin
->w_cursor
;
8955 if (gui_do_horiz_scroll())
8958 # ifdef FEAT_CINDENT
8971 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8975 tpos
= curwin
->w_cursor
;
8976 if (oneleft() == OK
)
8978 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8979 /* Only call start_arrow() when not busy with preediting, it will
8980 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8981 if (!im_is_preediting())
8984 #ifdef FEAT_RIGHTLEFT
8985 /* If exit reversed string, position is fixed */
8986 if (revins_scol
!= -1 && (int)curwin
->w_cursor
.col
>= revins_scol
)
8993 * if 'whichwrap' set for cursor in insert mode may go to
8996 else if (vim_strchr(p_ww
, '[') != NULL
&& curwin
->w_cursor
.lnum
> 1)
8999 --(curwin
->w_cursor
.lnum
);
9000 coladvance((colnr_T
)MAXCOL
);
9001 curwin
->w_set_curswant
= TRUE
; /* so we stay at the end */
9014 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9018 tpos
= curwin
->w_cursor
;
9020 curwin
->w_cursor
.lnum
= 1;
9021 curwin
->w_cursor
.col
= 0;
9022 #ifdef FEAT_VIRTUALEDIT
9023 curwin
->w_cursor
.coladd
= 0;
9025 curwin
->w_curswant
= 0;
9036 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9040 tpos
= curwin
->w_cursor
;
9042 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9043 coladvance((colnr_T
)MAXCOL
);
9044 curwin
->w_curswant
= MAXCOL
;
9053 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9057 if (curwin
->w_cursor
.lnum
> 1 || curwin
->w_cursor
.col
> 0)
9059 start_arrow(&curwin
->w_cursor
);
9060 (void)bck_word(1L, FALSE
, FALSE
);
9061 curwin
->w_set_curswant
= TRUE
;
9071 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9075 if (gchar_cursor() != NUL
9076 #ifdef FEAT_VIRTUALEDIT
9081 start_arrow(&curwin
->w_cursor
);
9082 curwin
->w_set_curswant
= TRUE
;
9083 #ifdef FEAT_VIRTUALEDIT
9084 if (virtual_active())
9091 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
9094 ++curwin
->w_cursor
.col
;
9097 #ifdef FEAT_RIGHTLEFT
9103 /* if 'whichwrap' set for cursor in insert mode, may move the
9104 * cursor to the next line */
9105 else if (vim_strchr(p_ww
, ']') != NULL
9106 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
9108 start_arrow(&curwin
->w_cursor
);
9109 curwin
->w_set_curswant
= TRUE
;
9110 ++curwin
->w_cursor
.lnum
;
9111 curwin
->w_cursor
.col
= 0;
9121 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9125 if (curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
9126 || gchar_cursor() != NUL
)
9128 start_arrow(&curwin
->w_cursor
);
9129 (void)fwd_word(1L, FALSE
, 0);
9130 curwin
->w_set_curswant
= TRUE
;
9138 int startcol
; /* when TRUE move to Insstart.col */
9141 linenr_T old_topline
= curwin
->w_topline
;
9143 int old_topfill
= curwin
->w_topfill
;
9147 tpos
= curwin
->w_cursor
;
9148 if (cursor_up(1L, TRUE
) == OK
)
9151 coladvance(getvcol_nolist(&Insstart
));
9152 if (old_topline
!= curwin
->w_topline
9154 || old_topfill
!= curwin
->w_topfill
9157 redraw_later(VALID
);
9175 if (mod_mask
& MOD_MASK_CTRL
)
9177 /* <C-PageUp>: tab page back */
9178 if (first_tabpage
->tp_next
!= NULL
)
9180 start_arrow(&curwin
->w_cursor
);
9187 tpos
= curwin
->w_cursor
;
9188 if (onepage(BACKWARD
, 1L) == OK
)
9201 int startcol
; /* when TRUE move to Insstart.col */
9204 linenr_T old_topline
= curwin
->w_topline
;
9206 int old_topfill
= curwin
->w_topfill
;
9210 tpos
= curwin
->w_cursor
;
9211 if (cursor_down(1L, TRUE
) == OK
)
9214 coladvance(getvcol_nolist(&Insstart
));
9215 if (old_topline
!= curwin
->w_topline
9217 || old_topfill
!= curwin
->w_topfill
9220 redraw_later(VALID
);
9238 if (mod_mask
& MOD_MASK_CTRL
)
9240 /* <C-PageDown>: tab page forward */
9241 if (first_tabpage
->tp_next
!= NULL
)
9243 start_arrow(&curwin
->w_cursor
);
9250 tpos
= curwin
->w_cursor
;
9251 if (onepage(FORWARD
, 1L) == OK
)
9266 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9271 * Handle TAB in Insert or Replace mode.
9272 * Return TRUE when the TAB needs to be inserted like a normal character.
9281 if (Insstart_blank_vcol
== MAXCOL
&& curwin
->w_cursor
.lnum
== Insstart
.lnum
)
9282 Insstart_blank_vcol
= get_nolist_virtcol();
9283 if (echeck_abbr(TAB
+ ABBR_OFF
))
9289 can_cindent
= FALSE
;
9293 * When nothing special, insert TAB like a normal character
9298 /* These five lines mean 'tabstop' != 'shiftwidth' */
9299 && ((tabstop_count(curbuf
->b_p_vts_ary
) > 1)
9300 || (tabstop_count(curbuf
->b_p_vts_ary
) == 1
9301 && tabstop_first(curbuf
->b_p_vts_ary
) != curbuf
->b_p_sw
)
9302 || (tabstop_count(curbuf
->b_p_vts_ary
) == 0
9303 && curbuf
->b_p_ts
!= curbuf
->b_p_sw
)))
9304 && tabstop_count(curbuf
->b_p_vsts_ary
) == 0
9305 && curbuf
->b_p_sts
== 0
9307 && !(p_sta
&& ind
&& curbuf
->b_p_ts
!= curbuf
->b_p_sw
)
9308 && curbuf
->b_p_sts
== 0
9313 if (stop_arrow() == FAIL
)
9317 #ifdef FEAT_SMARTINDENT
9320 can_si_back
= FALSE
;
9322 AppendToRedobuff((char_u
*)"\t");
9325 if (p_sta
&& ind
) /* insert tab in indent, use 'shiftwidth' */
9327 temp
= (int)curbuf
->b_p_sw
;
9328 temp
-= get_nolist_virtcol() % temp
;
9330 else if (tabstop_count(curbuf
->b_p_vsts_ary
) > 0 || curbuf
->b_p_sts
> 0)
9331 /* use 'softtabstop' when set */
9332 temp
= tabstop_padding(get_nolist_virtcol(), curbuf
->b_p_sts
,
9333 curbuf
->b_p_vsts_ary
);
9334 else /* otherwise use 'tabstop' */
9335 temp
= tabstop_padding(get_nolist_virtcol(), curbuf
->b_p_ts
,
9336 curbuf
->b_p_vts_ary
);
9338 if (p_sta
&& ind
) /* insert tab in indent, use 'shiftwidth' */
9339 temp
= (int)curbuf
->b_p_sw
;
9340 else if (curbuf
->b_p_sts
> 0) /* use 'softtabstop' when set */
9341 temp
= (int)curbuf
->b_p_sts
;
9342 else /* otherwise use 'tabstop' */
9343 temp
= (int)curbuf
->b_p_ts
;
9344 temp
-= get_nolist_virtcol() % temp
;
9348 * Insert the first space with ins_char(). It will delete one char in
9349 * replace mode. Insert the rest with ins_str(); it will not delete any
9350 * chars. For VREPLACE mode, we use ins_char() for all characters.
9355 #ifdef FEAT_VREPLACE
9356 if (State
& VREPLACE_FLAG
)
9361 ins_str((char_u
*)" ");
9362 if (State
& REPLACE_FLAG
) /* no char replaced */
9368 * When 'expandtab' not set: Replace spaces by TABs where possible.
9371 if (!curbuf
->b_p_et
&& (tabstop_count(curbuf
->b_p_vsts_ary
) > 0
9372 || curbuf
->b_p_sts
> 0
9375 if (!curbuf
->b_p_et
&& (curbuf
->b_p_sts
|| (p_sta
&& ind
)))
9379 #ifdef FEAT_VREPLACE
9380 char_u
*saved_line
= NULL
; /* init for GCC */
9385 colnr_T want_vcol
, vcol
;
9386 int change_col
= -1;
9387 int save_list
= curwin
->w_p_list
;
9390 * Get the current line. For VREPLACE mode, don't make real changes
9391 * yet, just work on a copy of the line.
9393 #ifdef FEAT_VREPLACE
9394 if (State
& VREPLACE_FLAG
)
9396 pos
= curwin
->w_cursor
;
9398 saved_line
= vim_strsave(ml_get_curline());
9399 if (saved_line
== NULL
)
9401 ptr
= saved_line
+ pos
.col
;
9406 ptr
= ml_get_cursor();
9407 cursor
= &curwin
->w_cursor
;
9410 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9411 if (vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9412 curwin
->w_p_list
= FALSE
;
9414 /* Find first white before the cursor */
9415 fpos
= curwin
->w_cursor
;
9416 while (fpos
.col
> 0 && vim_iswhite(ptr
[-1]))
9422 /* In Replace mode, don't change characters before the insert point. */
9423 if ((State
& REPLACE_FLAG
)
9424 && fpos
.lnum
== Insstart
.lnum
9425 && fpos
.col
< Insstart
.col
)
9427 ptr
+= Insstart
.col
- fpos
.col
;
9428 fpos
.col
= Insstart
.col
;
9431 /* compute virtual column numbers of first white and cursor */
9432 getvcol(curwin
, &fpos
, &vcol
, NULL
, NULL
);
9433 getvcol(curwin
, cursor
, &want_vcol
, NULL
, NULL
);
9435 /* Use as many TABs as possible. Beware of 'showbreak' and
9436 * 'linebreak' adding extra virtual columns. */
9437 while (vim_iswhite(*ptr
))
9439 i
= lbr_chartabsize((char_u
*)"\t", vcol
);
9440 if (vcol
+ i
> want_vcol
)
9447 change_col
= fpos
.col
; /* Column of first change */
9448 /* May have to adjust Insstart */
9449 if (fpos
.lnum
== Insstart
.lnum
&& fpos
.col
< Insstart
.col
)
9450 Insstart
.col
= fpos
.col
;
9458 if (change_col
>= 0)
9462 /* Skip over the spaces we need. */
9463 while (vcol
< want_vcol
&& *ptr
== ' ')
9465 vcol
+= lbr_chartabsize(ptr
, vcol
);
9469 if (vcol
> want_vcol
)
9471 /* Must have a char with 'showbreak' just before it. */
9475 fpos
.col
+= repl_off
;
9477 /* Delete following spaces. */
9478 i
= cursor
->col
- fpos
.col
;
9481 STRMOVE(ptr
, ptr
+ i
);
9482 /* correct replace stack. */
9483 if ((State
& REPLACE_FLAG
)
9484 #ifdef FEAT_VREPLACE
9485 && !(State
& VREPLACE_FLAG
)
9488 for (temp
= i
; --temp
>= 0; )
9489 replace_join(repl_off
);
9491 #ifdef FEAT_NETBEANS_INTG
9494 netbeans_removed(curbuf
, fpos
.lnum
, cursor
->col
,
9496 netbeans_inserted(curbuf
, fpos
.lnum
, cursor
->col
,
9502 #ifdef FEAT_VREPLACE
9504 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9505 * backspacing over the changed spacing and then inserting the new
9508 if (State
& VREPLACE_FLAG
)
9510 /* Backspace from real cursor to change_col */
9511 backspace_until_column(change_col
);
9513 /* Insert each char in saved_line from changed_col to
9515 ins_bytes_len(saved_line
+ change_col
,
9516 cursor
->col
- change_col
);
9521 #ifdef FEAT_VREPLACE
9522 if (State
& VREPLACE_FLAG
)
9523 vim_free(saved_line
);
9525 curwin
->w_p_list
= save_list
;
9532 * Handle CR or NL in insert mode.
9533 * Return TRUE when out of memory or can't undo.
9541 if (echeck_abbr(c
+ ABBR_OFF
))
9543 if (stop_arrow() == FAIL
)
9548 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9549 * character under the cursor. Only push a NUL on the replace stack,
9550 * nothing to put back when the NL is deleted.
9552 if ((State
& REPLACE_FLAG
)
9553 #ifdef FEAT_VREPLACE
9554 && !(State
& VREPLACE_FLAG
)
9560 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9561 * replacing the next line, so we push all of the characters left on the
9562 * line onto the replace stack. This is not done here though, it is done
9566 #ifdef FEAT_VIRTUALEDIT
9567 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9569 if (virtual_active() && curwin
->w_cursor
.coladd
> 0)
9570 coladvance(getviscol());
9573 #ifdef FEAT_RIGHTLEFT
9575 if (p_altkeymap
&& p_fkmap
)
9578 /* NL in reverse insert will always start in the end of
9581 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
9584 AppendToRedobuff(NL_STR
);
9585 i
= open_line(FORWARD
,
9586 #ifdef FEAT_COMMENTS
9587 has_format_option(FO_RET_COMS
) ? OPENLINE_DO_COM
:
9595 /* When inserting a line the cursor line must never be in a closed fold. */
9602 #ifdef FEAT_DIGRAPHS
9604 * Handle digraph in insert mode.
9605 * Returns character still to be inserted, or NUL when nothing remaining to be
9614 pc_status
= PC_STATUS_UNSET
;
9615 if (redrawing() && !char_avail())
9617 /* may need to redraw when no more chars available now */
9620 edit_putchar('?', TRUE
);
9621 #ifdef FEAT_CMDL_INFO
9622 add_to_showcmd_c(Ctrl_K
);
9626 #ifdef USE_ON_FLY_SCROLL
9627 dont_scroll
= TRUE
; /* disallow scrolling here */
9630 /* don't map the digraph chars. This also prevents the
9631 * mode message to be deleted when ESC is hit */
9637 if (IS_SPECIAL(c
) || mod_mask
) /* special key */
9639 #ifdef FEAT_CMDL_INFO
9642 insert_special(c
, TRUE
, FALSE
);
9647 if (redrawing() && !char_avail())
9649 /* may need to redraw when no more chars available now */
9652 if (char2cells(c
) == 1)
9654 /* first remove the '?', otherwise it's restored when typing
9658 edit_putchar(c
, TRUE
);
9660 #ifdef FEAT_CMDL_INFO
9661 add_to_showcmd_c(c
);
9671 AppendToRedobuff((char_u
*)CTRL_V_STR
);
9672 c
= getdigraph(c
, cc
, TRUE
);
9673 #ifdef FEAT_CMDL_INFO
9680 #ifdef FEAT_CMDL_INFO
9688 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9689 * Returns the char to be inserted, or NUL if none found.
9697 char_u
*ptr
, *prev_ptr
;
9699 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
9705 /* try to advance to the cursor column */
9710 while ((colnr_T
)temp
< curwin
->w_virtcol
&& *ptr
!= NUL
)
9713 temp
+= lbr_chartabsize_adv(&ptr
, (colnr_T
)temp
);
9715 if ((colnr_T
)temp
> curwin
->w_virtcol
)
9719 c
= (*mb_ptr2char
)(ptr
);
9729 * CTRL-Y or CTRL-E typed in Insert mode.
9737 #ifdef FEAT_INS_EXPAND
9738 if (ctrl_x_mode
== CTRL_X_SCROLL
)
9744 redraw_later(VALID
);
9749 c
= ins_copychar(curwin
->w_cursor
.lnum
+ (c
== Ctrl_Y
? -1 : 1));
9754 /* The character must be taken literally, insert like it
9755 * was typed after a CTRL-V, and pretend 'textwidth'
9756 * wasn't set. Digits, 'o' and 'x' are special after a
9757 * CTRL-V, don't use it for these. */
9758 if (c
< 256 && !isalnum(c
))
9759 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
9760 tw_save
= curbuf
->b_p_tw
;
9761 curbuf
->b_p_tw
= -1;
9762 insert_special(c
, TRUE
, FALSE
);
9763 curbuf
->b_p_tw
= tw_save
;
9764 #ifdef FEAT_RIGHTLEFT
9768 c
= Ctrl_V
; /* pretend CTRL-V is last character */
9769 auto_format(FALSE
, TRUE
);
9775 #ifdef FEAT_SMARTINDENT
9777 * Try to do some very smart auto-indenting.
9778 * Used when inserting a "normal" character.
9784 pos_T
*pos
, old_pos
;
9790 * do some very smart indenting when entering '{' or '}'
9792 if (((did_si
|| can_si_back
) && c
== '{') || (can_si
&& c
== '}'))
9795 * for '}' set indent equal to indent of line containing matching '{'
9797 if (c
== '}' && (pos
= findmatch(NULL
, '{')) != NULL
)
9799 old_pos
= curwin
->w_cursor
;
9801 * If the matching '{' has a ')' immediately before it (ignoring
9802 * white-space), then line up with the start of the line
9803 * containing the matching '(' if there is one. This handles the
9804 * case where an "if (..\n..) {" statement continues over multiple
9807 ptr
= ml_get(pos
->lnum
);
9809 if (i
> 0) /* skip blanks before '{' */
9810 while (--i
> 0 && vim_iswhite(ptr
[i
]))
9812 curwin
->w_cursor
.lnum
= pos
->lnum
;
9813 curwin
->w_cursor
.col
= i
;
9814 if (ptr
[i
] == ')' && (pos
= findmatch(NULL
, '(')) != NULL
)
9815 curwin
->w_cursor
= *pos
;
9817 curwin
->w_cursor
= old_pos
;
9818 #ifdef FEAT_VREPLACE
9819 if (State
& VREPLACE_FLAG
)
9820 change_indent(INDENT_SET
, i
, FALSE
, NUL
, TRUE
);
9823 (void)set_indent(i
, SIN_CHANGED
);
9825 else if (curwin
->w_cursor
.col
> 0)
9828 * when inserting '{' after "O" reduce indent, but not
9829 * more than indent of previous line
9832 if (c
== '{' && can_si_back
&& curwin
->w_cursor
.lnum
> 1)
9834 old_pos
= curwin
->w_cursor
;
9836 while (curwin
->w_cursor
.lnum
> 1)
9838 ptr
= skipwhite(ml_get(--(curwin
->w_cursor
.lnum
)));
9840 /* ignore empty lines and lines starting with '#'. */
9841 if (*ptr
!= '#' && *ptr
!= NUL
)
9844 if (get_indent() >= i
)
9846 curwin
->w_cursor
= old_pos
;
9849 shift_line(TRUE
, FALSE
, 1, TRUE
);
9854 * set indent of '#' always to 0
9856 if (curwin
->w_cursor
.col
> 0 && can_si
&& c
== '#')
9858 /* remember current indent for next line */
9859 old_indent
= get_indent();
9860 (void)set_indent(0, SIN_CHANGED
);
9863 /* Adjust ai_col, the char at this position can be deleted. */
9864 if (ai_col
> curwin
->w_cursor
.col
)
9865 ai_col
= curwin
->w_cursor
.col
;
9870 * Get the value that w_virtcol would have when 'list' is off.
9871 * Unless 'cpo' contains the 'L' flag.
9874 get_nolist_virtcol()
9876 if (curwin
->w_p_list
&& vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9877 return getvcol_nolist(&curwin
->w_cursor
);
9879 return curwin
->w_virtcol
;