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();
660 if ((int)curwin
->w_wcol
< mincol
- curbuf
->b_p_ts
661 && curwin
->w_wrow
== W_WINROW(curwin
)
662 + curwin
->w_height
- 1 - p_so
663 && (curwin
->w_cursor
.lnum
!= curwin
->w_topline
665 || curwin
->w_topfill
> 0
670 if (curwin
->w_topfill
> 0)
675 if (hasFolding(curwin
->w_topline
, NULL
, &old_topline
))
676 set_topline(curwin
, old_topline
+ 1);
679 set_topline(curwin
, curwin
->w_topline
+ 1);
683 /* May need to adjust w_topline to show the cursor. */
686 did_backspace
= FALSE
;
688 validate_cursor(); /* may set must_redraw */
691 * Redraw the display when no characters are waiting.
692 * Also shows mode, ruler and positions cursor.
696 #ifdef FEAT_SCROLLBIND
698 do_check_scrollbind(TRUE
);
702 old_topline
= curwin
->w_topline
;
704 old_topfill
= curwin
->w_topfill
;
707 #ifdef USE_ON_FLY_SCROLL
708 dont_scroll
= FALSE
; /* allow scrolling here */
712 * Get a character for Insert mode. Ignore K_IGNORE.
714 lastc
= c
; /* remember previous char for CTRL-D */
718 } while (c
== K_IGNORE
);
721 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
722 did_cursorhold
= TRUE
;
725 #ifdef FEAT_RIGHTLEFT
726 if (p_hkmap
&& KeyTyped
)
727 c
= hkmap(c
); /* Hebrew mode mapping */
730 if (p_fkmap
&& KeyTyped
)
731 c
= fkmap(c
); /* Farsi mode mapping */
734 #ifdef FEAT_INS_EXPAND
736 * Special handling of keys while the popup menu is visible or wanted
737 * and the cursor is still in the completed word. Only when there is
738 * a match, skip this when no matches were found.
742 && curwin
->w_cursor
.col
>= compl_col
743 && (compl_shown_match
== NULL
744 || compl_shown_match
!= compl_shown_match
->cp_next
))
746 /* BS: Delete one character from "compl_leader". */
747 if ((c
== K_BS
|| c
== Ctrl_H
)
748 && curwin
->w_cursor
.col
> compl_col
749 && (c
= ins_compl_bs()) == NUL
)
752 /* When no match was selected or it was edited. */
753 if (!compl_used_match
)
755 /* CTRL-L: Add one character from the current match to
756 * "compl_leader". Except when at the original match and
757 * there is nothing to add, CTRL-L works like CTRL-P then. */
759 && (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
760 || (int)STRLEN(compl_shown_match
->cp_str
)
761 > curwin
->w_cursor
.col
- compl_col
))
763 ins_compl_addfrommatch();
767 /* A non-white character that fits in with the current
768 * completion: Add to "compl_leader". */
769 if (ins_compl_accept_char(c
))
771 ins_compl_addleader(c
);
775 /* Pressing CTRL-Y selects the current match. When
776 * compl_enter_selects is set the Enter key does the same. */
777 if (c
== Ctrl_Y
|| (compl_enter_selects
778 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
786 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
787 * it does fix up the text when finishing completion. */
788 compl_get_longest
= FALSE
;
789 if (ins_compl_prep(c
))
793 /* CTRL-\ CTRL-N goes to Normal mode,
794 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
795 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
798 /* may need to redraw when no more chars available now */
805 if (c
!= Ctrl_N
&& c
!= Ctrl_G
&& c
!= Ctrl_O
)
807 /* it's something else */
811 else if (c
== Ctrl_G
&& p_im
)
818 ins_at_eol
= FALSE
; /* cursor keeps its column */
830 #ifdef FEAT_INS_EXPAND
831 if ((c
== Ctrl_V
|| c
== Ctrl_Q
) && ctrl_x_mode
== CTRL_X_CMDLINE
)
834 if (c
== Ctrl_V
|| c
== Ctrl_Q
)
837 c
= Ctrl_V
; /* pretend CTRL-V is last typed character */
843 # ifdef FEAT_INS_EXPAND
848 /* A key name preceded by a bang means this key is not to be
849 * inserted. Skip ahead to the re-indenting below.
850 * A key name preceded by a star means that indenting has to be
851 * done before inserting the key. */
852 line_is_white
= inindent(0);
853 if (in_cinkeys(c
, '!', line_is_white
))
855 if (can_cindent
&& in_cinkeys(c
, '*', line_is_white
)
856 && stop_arrow() == OK
)
861 #ifdef FEAT_RIGHTLEFT
865 case K_LEFT
: c
= K_RIGHT
; break;
866 case K_S_LEFT
: c
= K_S_RIGHT
; break;
867 case K_C_LEFT
: c
= K_C_RIGHT
; break;
868 case K_RIGHT
: c
= K_LEFT
; break;
869 case K_S_RIGHT
: c
= K_S_LEFT
; break;
870 case K_C_RIGHT
: c
= K_C_LEFT
; break;
876 * If 'keymodel' contains "startsel", may start selection. If it
877 * does, a CTRL-O and c will be stuffed, we need to get these
880 if (ins_start_select(c
))
885 * The big switch to handle a character in insert mode.
889 case ESC
: /* End input mode */
890 if (echeck_abbr(ESC
+ ABBR_OFF
))
894 case Ctrl_C
: /* End input mode */
896 if (c
== Ctrl_C
&& cmdwin_type
!= 0)
898 /* Close the cmdline window. */
899 cmdwin_result
= K_IGNORE
;
900 got_int
= FALSE
; /* don't stop executing autocommands et al. */
909 /* when 'insertmode' set, and not halfway a mapping, don't leave
915 (void)vgetc(); /* flush all buffers */
924 * This is the ONLY return from edit()!
926 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
927 * still puts the cursor back after the inserted text. */
928 if (ins_at_eol
&& gchar_cursor() == NUL
)
929 o_lnum
= curwin
->w_cursor
.lnum
;
931 if (ins_esc(&count
, cmdchar
, nomove
))
934 if (cmdchar
!= 'r' && cmdchar
!= 'v')
935 apply_autocmds(EVENT_INSERTLEAVE
, NULL
, NULL
,
937 did_cursorhold
= FALSE
;
939 return (c
== Ctrl_O
);
943 case Ctrl_Z
: /* suspend when 'insertmode' set */
945 goto normalchar
; /* insert CTRL-Z as normal char */
946 stuffReadbuff((char_u
*)":st\r");
950 case Ctrl_O
: /* execute one command */
951 #ifdef FEAT_COMPL_FUNC
952 if (ctrl_x_mode
== CTRL_X_OMNI
)
955 if (echeck_abbr(Ctrl_O
+ ABBR_OFF
))
959 #ifdef FEAT_VIRTUALEDIT
960 /* don't move the cursor left when 'virtualedit' has "onemore". */
961 if (ve_flags
& VE_ONEMORE
)
970 case K_INS
: /* toggle insert/replace mode */
972 ins_insert(replaceState
);
975 case K_SELECT
: /* end of Select mode mapping - ignore */
979 case K_SNIFF
: /* Sniff command received */
980 stuffcharReadbuff(K_SNIFF
);
984 case K_HELP
: /* Help key works like <ESC> <Help> */
987 stuffcharReadbuff(K_HELP
);
989 need_start_insertmode
= TRUE
;
992 #ifdef FEAT_NETBEANS_INTG
993 case K_F21
: /* NetBeans command */
994 ++no_mapping
; /* don't map the next key hits */
997 netbeans_keycommand(i
);
1001 case K_ZERO
: /* Insert the previously inserted text. */
1004 /* For ^@ the trailing ESC will end the insert, unless there is an
1006 if (stuff_inserted(NUL
, 1L, (c
== Ctrl_A
)) == FAIL
1007 && c
!= Ctrl_A
&& !p_im
)
1008 goto doESCkey
; /* quit insert mode */
1009 inserted_space
= FALSE
;
1012 case Ctrl_R
: /* insert the contents of a register */
1014 auto_format(FALSE
, TRUE
);
1015 inserted_space
= FALSE
;
1018 case Ctrl_G
: /* commands starting with CTRL-G */
1022 case Ctrl_HAT
: /* switch input mode and/or langmap */
1026 #ifdef FEAT_RIGHTLEFT
1027 case Ctrl__
: /* switch between languages */
1034 case Ctrl_D
: /* Make indent one shiftwidth smaller. */
1035 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1036 if (ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
1041 case Ctrl_T
: /* Make indent one shiftwidth greater. */
1042 # ifdef FEAT_INS_EXPAND
1043 if (c
== Ctrl_T
&& ctrl_x_mode
== CTRL_X_THESAURUS
)
1045 if (has_compl_option(FALSE
))
1050 ins_shift(c
, lastc
);
1051 auto_format(FALSE
, TRUE
);
1052 inserted_space
= FALSE
;
1055 case K_DEL
: /* delete character under the cursor */
1058 auto_format(FALSE
, TRUE
);
1061 case K_BS
: /* delete character before the cursor */
1063 did_backspace
= ins_bs(c
, BACKSPACE_CHAR
, &inserted_space
);
1064 auto_format(FALSE
, TRUE
);
1067 case Ctrl_W
: /* delete word before the cursor */
1068 did_backspace
= ins_bs(c
, BACKSPACE_WORD
, &inserted_space
);
1069 auto_format(FALSE
, TRUE
);
1072 case Ctrl_U
: /* delete all inserted text in current line */
1073 # ifdef FEAT_COMPL_FUNC
1074 /* CTRL-X CTRL-U completes with 'completefunc'. */
1075 if (ctrl_x_mode
== CTRL_X_FUNCTION
)
1078 did_backspace
= ins_bs(c
, BACKSPACE_LINE
, &inserted_space
);
1079 auto_format(FALSE
, TRUE
);
1080 inserted_space
= FALSE
;
1084 case K_LEFTMOUSE
: /* mouse keys */
1085 case K_LEFTMOUSE_NM
:
1088 case K_LEFTRELEASE_NM
:
1091 case K_MIDDLERELEASE
:
1094 case K_RIGHTRELEASE
:
1104 case K_MOUSEDOWN
: /* Default action for scroll wheel up: scroll up */
1105 ins_mousescroll(FALSE
);
1108 case K_MOUSEUP
: /* Default action for scroll wheel down: scroll down */
1109 ins_mousescroll(TRUE
);
1112 #ifdef FEAT_GUI_TABLINE
1119 case K_IGNORE
: /* Something mapped to nothing */
1123 case K_CURSORHOLD
: /* Didn't type something for a while. */
1124 apply_autocmds(EVENT_CURSORHOLDI
, NULL
, NULL
, FALSE
, curbuf
);
1125 did_cursorhold
= TRUE
;
1130 /* On Win32 ignore <M-F4>, we get it when closing the window was
1133 if (mod_mask
!= MOD_MASK_ALT
)
1139 case K_VER_SCROLLBAR
:
1143 case K_HOR_SCROLLBAR
:
1148 case K_HOME
: /* <Home> */
1155 case K_END
: /* <End> */
1162 case K_LEFT
: /* <Left> */
1163 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1169 case K_S_LEFT
: /* <S-Left> */
1174 case K_RIGHT
: /* <Right> */
1175 if (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
))
1181 case K_S_RIGHT
: /* <S-Right> */
1186 case K_UP
: /* <Up> */
1187 #ifdef FEAT_INS_EXPAND
1191 if (mod_mask
& MOD_MASK_SHIFT
)
1197 case K_S_UP
: /* <S-Up> */
1200 #ifdef FEAT_INS_EXPAND
1207 case K_DOWN
: /* <Down> */
1208 #ifdef FEAT_INS_EXPAND
1212 if (mod_mask
& MOD_MASK_SHIFT
)
1218 case K_S_DOWN
: /* <S-Down> */
1221 #ifdef FEAT_INS_EXPAND
1229 case K_DROP
: /* drag-n-drop event */
1234 case K_S_TAB
: /* When not mapped, use like a normal TAB */
1238 case TAB
: /* TAB or Complete patterns along path */
1239 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1240 if (ctrl_x_mode
== CTRL_X_PATH_PATTERNS
)
1243 inserted_space
= FALSE
;
1245 goto normalchar
; /* insert TAB as a normal char */
1246 auto_format(FALSE
, TRUE
);
1249 case K_KENTER
: /* <Enter> */
1254 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1255 /* In a quickfix window a <CR> jumps to the error under the
1257 if (bt_quickfix(curbuf
) && c
== CAR
)
1259 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
1260 do_cmdline_cmd((char_u
*)".cc");
1261 else /* location list window */
1262 do_cmdline_cmd((char_u
*)".ll");
1267 if (cmdwin_type
!= 0)
1269 /* Execute the command in the cmdline window. */
1270 cmdwin_result
= CAR
;
1274 if (ins_eol(c
) && !p_im
)
1275 goto doESCkey
; /* out of memory */
1276 auto_format(FALSE
, FALSE
);
1277 inserted_space
= FALSE
;
1280 #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
1281 case Ctrl_K
: /* digraph or keyword completion */
1282 # ifdef FEAT_INS_EXPAND
1283 if (ctrl_x_mode
== CTRL_X_DICTIONARY
)
1285 if (has_compl_option(TRUE
))
1290 # ifdef FEAT_DIGRAPHS
1298 #ifdef FEAT_INS_EXPAND
1299 case Ctrl_X
: /* Enter CTRL-X mode */
1303 case Ctrl_RSB
: /* Tag name completion after ^X */
1304 if (ctrl_x_mode
!= CTRL_X_TAGS
)
1308 case Ctrl_F
: /* File name completion after ^X */
1309 if (ctrl_x_mode
!= CTRL_X_FILES
)
1313 case 's': /* Spelling completion after ^X */
1315 if (ctrl_x_mode
!= CTRL_X_SPELL
)
1320 case Ctrl_L
: /* Whole line completion after ^X */
1321 #ifdef FEAT_INS_EXPAND
1322 if (ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
1325 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1328 if (echeck_abbr(Ctrl_L
+ ABBR_OFF
))
1334 #ifdef FEAT_INS_EXPAND
1337 case Ctrl_P
: /* Do previous/next pattern completion */
1339 /* if 'complete' is empty then plain ^P is no longer special,
1340 * but it is under other ^X modes */
1341 if (*curbuf
->b_p_cpt
== NUL
1343 && !(compl_cont_status
& CONT_LOCAL
))
1348 if (ins_complete(c
) == FAIL
)
1349 compl_cont_status
= 0;
1352 #endif /* FEAT_INS_EXPAND */
1354 case Ctrl_Y
: /* copy from previous line or scroll down */
1355 case Ctrl_E
: /* copy from next line or scroll up */
1361 if (c
== intr_char
) /* special interrupt char */
1366 * Insert a nomal character.
1369 #ifdef FEAT_SMARTINDENT
1370 /* Try to perform smart-indenting. */
1376 inserted_space
= TRUE
;
1379 can_cindent
= FALSE
;
1381 if (Insstart_blank_vcol
== MAXCOL
1382 && curwin
->w_cursor
.lnum
== Insstart
.lnum
)
1383 Insstart_blank_vcol
= get_nolist_virtcol();
1386 if (vim_iswordc(c
) || !echeck_abbr(
1388 /* Add ABBR_OFF for characters above 0x100, this is
1389 * what check_abbr() expects. */
1390 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1394 insert_special(c
, FALSE
, FALSE
);
1395 #ifdef FEAT_RIGHTLEFT
1401 auto_format(FALSE
, TRUE
);
1404 /* When inserting a character the cursor line must never be in a
1409 } /* end of switch (c) */
1412 /* If typed something may trigger CursorHoldI again. */
1413 if (c
!= K_CURSORHOLD
)
1414 did_cursorhold
= FALSE
;
1417 /* If the cursor was moved we didn't just insert a space */
1419 inserted_space
= FALSE
;
1422 if (can_cindent
&& cindent_on()
1423 # ifdef FEAT_INS_EXPAND
1430 * Indent now if a key was typed that is in 'cinkeys'.
1432 if (in_cinkeys(c
, ' ', line_is_white
))
1434 if (stop_arrow() == OK
)
1435 /* re-indent the current line */
1439 #endif /* FEAT_CINDENT */
1446 * Redraw for Insert mode.
1447 * This is postponed until getting the next character to make '$' in the 'cpo'
1448 * option work correctly.
1449 * Only redraw when there are no characters available. This speeds up
1450 * inserting sequences of characters (e.g., for CTRL-R).
1454 int ready UNUSED
; /* not busy with something */
1459 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1460 * visible, the command might delete it. */
1461 if (ready
&& has_cursormovedI()
1462 && !equalpos(last_cursormoved
, curwin
->w_cursor
)
1463 # ifdef FEAT_INS_EXPAND
1469 /* Need to update the screen first, to make sure syntax
1470 * highlighting is correct after making a change (e.g., inserting
1471 * a "(". The autocommand may also require a redraw, so it's done
1472 * again below, unfortunately. */
1473 if (syntax_present(curbuf
) && must_redraw
)
1476 apply_autocmds(EVENT_CURSORMOVEDI
, NULL
, NULL
, FALSE
, curbuf
);
1477 last_cursormoved
= curwin
->w_cursor
;
1482 else if (clear_cmdline
|| redraw_cmdline
)
1483 showmode(); /* clear cmdline and show mode */
1486 emsg_on_display
= FALSE
; /* may remove error message now */
1491 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1498 /* may need to redraw when no more chars available now */
1501 if (redrawing() && !char_avail())
1502 edit_putchar('^', TRUE
);
1503 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
1505 #ifdef FEAT_CMDL_INFO
1506 add_to_showcmd_c(Ctrl_V
);
1510 #ifdef FEAT_CMDL_INFO
1513 insert_special(c
, FALSE
, TRUE
);
1514 #ifdef FEAT_RIGHTLEFT
1521 * Put a character directly onto the screen. It's not stored in a buffer.
1522 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1524 static int pc_status
;
1525 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1526 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1527 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1528 #define PC_STATUS_SET 3 /* pc_bytes was filled */
1530 static char_u pc_bytes
[MB_MAXBYTES
+ 1]; /* saved bytes */
1532 static char_u pc_bytes
[2]; /* saved bytes */
1539 edit_putchar(c
, highlight
)
1545 if (ScreenLines
!= NULL
)
1547 update_topline(); /* just in case w_topline isn't valid */
1550 attr
= hl_attr(HLF_8
);
1553 pc_row
= W_WINROW(curwin
) + curwin
->w_wrow
;
1554 pc_col
= W_WINCOL(curwin
);
1555 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1556 pc_status
= PC_STATUS_UNSET
;
1558 #ifdef FEAT_RIGHTLEFT
1561 pc_col
+= W_WIDTH(curwin
) - 1 - curwin
->w_wcol
;
1565 int fix_col
= mb_fix_col(pc_col
, pc_row
);
1567 if (fix_col
!= pc_col
)
1569 screen_putchar(' ', pc_row
, fix_col
, attr
);
1571 pc_status
= PC_STATUS_RIGHT
;
1579 pc_col
+= curwin
->w_wcol
;
1581 if (mb_lefthalve(pc_row
, pc_col
))
1582 pc_status
= PC_STATUS_LEFT
;
1586 /* save the character to be able to put it back */
1587 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1588 if (pc_status
== PC_STATUS_UNSET
)
1591 screen_getbytes(pc_row
, pc_col
, pc_bytes
, &pc_attr
);
1592 pc_status
= PC_STATUS_SET
;
1594 screen_putchar(c
, pc_row
, pc_col
, attr
);
1599 * Undo the previous edit_putchar().
1604 if (pc_status
!= PC_STATUS_UNSET
&& pc_row
>= msg_scrolled
)
1606 #if defined(FEAT_MBYTE)
1607 if (pc_status
== PC_STATUS_RIGHT
)
1609 if (pc_status
== PC_STATUS_RIGHT
|| pc_status
== PC_STATUS_LEFT
)
1610 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1613 screen_puts(pc_bytes
, pc_row
- msg_scrolled
, pc_col
, pc_attr
);
1618 * Called when p_dollar is set: display a '$' at the end of the changed text
1619 * Only works when cursor is in the line that changes.
1631 save_col
= curwin
->w_cursor
.col
;
1632 curwin
->w_cursor
.col
= col
;
1638 /* If on the last byte of a multi-byte move to the first byte. */
1639 p
= ml_get_curline();
1640 curwin
->w_cursor
.col
-= (*mb_head_off
)(p
, p
+ col
);
1643 curs_columns(FALSE
); /* recompute w_wrow and w_wcol */
1644 if (curwin
->w_wcol
< W_WIDTH(curwin
))
1646 edit_putchar('$', FALSE
);
1647 dollar_vcol
= curwin
->w_virtcol
;
1649 curwin
->w_cursor
.col
= save_col
;
1653 * Call this function before moving the cursor from the normal insert position
1662 redrawWinline(curwin
->w_cursor
.lnum
, FALSE
);
1667 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1668 * Keep the cursor on the same character.
1669 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1670 * type == INDENT_DEC decrease indent (for CTRL-D)
1671 * type == INDENT_SET set indent to "amount"
1672 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1675 change_indent(type
, amount
, round
, replaced
, call_changed_bytes
)
1679 int replaced
; /* replaced character, put on replace stack */
1680 int call_changed_bytes
; /* call changed_bytes() */
1684 int insstart_less
; /* reduction for Insstart.col */
1691 #ifdef FEAT_VREPLACE
1692 colnr_T orig_col
= 0; /* init for GCC */
1693 char_u
*new_line
, *orig_line
= NULL
; /* init for GCC */
1695 /* VREPLACE mode needs to know what the line was like before changing */
1696 if (State
& VREPLACE_FLAG
)
1698 orig_line
= vim_strsave(ml_get_curline()); /* Deal with NULL below */
1699 orig_col
= curwin
->w_cursor
.col
;
1703 /* for the following tricks we don't want list mode */
1704 save_p_list
= curwin
->w_p_list
;
1705 curwin
->w_p_list
= FALSE
;
1706 vc
= getvcol_nolist(&curwin
->w_cursor
);
1710 * For Replace mode we need to fix the replace stack later, which is only
1711 * possible when the cursor is in the indent. Remember the number of
1712 * characters before the cursor if it's possible.
1714 start_col
= curwin
->w_cursor
.col
;
1716 /* determine offset from first non-blank */
1717 new_cursor_col
= curwin
->w_cursor
.col
;
1718 beginline(BL_WHITE
);
1719 new_cursor_col
-= curwin
->w_cursor
.col
;
1721 insstart_less
= curwin
->w_cursor
.col
;
1724 * If the cursor is in the indent, compute how many screen columns the
1725 * cursor is to the left of the first non-blank.
1727 if (new_cursor_col
< 0)
1728 vcol
= get_indent() - vcol
;
1730 if (new_cursor_col
> 0) /* can't fix replace stack */
1734 * Set the new indent. The cursor will be put on the first non-blank.
1736 if (type
== INDENT_SET
)
1737 (void)set_indent(amount
, call_changed_bytes
? SIN_CHANGED
: 0);
1740 #ifdef FEAT_VREPLACE
1741 int save_State
= State
;
1743 /* Avoid being called recursively. */
1744 if (State
& VREPLACE_FLAG
)
1747 shift_line(type
== INDENT_DEC
, round
, 1, call_changed_bytes
);
1748 #ifdef FEAT_VREPLACE
1752 insstart_less
-= curwin
->w_cursor
.col
;
1755 * Try to put cursor on same character.
1756 * If the cursor is at or after the first non-blank in the line,
1757 * compute the cursor column relative to the column of the first
1758 * non-blank character.
1759 * If we are not in insert mode, leave the cursor on the first non-blank.
1760 * If the cursor is before the first non-blank, position it relative
1761 * to the first non-blank, counted in screen columns.
1763 if (new_cursor_col
>= 0)
1766 * When changing the indent while the cursor is touching it, reset
1767 * Insstart_col to 0.
1769 if (new_cursor_col
== 0)
1770 insstart_less
= MAXCOL
;
1771 new_cursor_col
+= curwin
->w_cursor
.col
;
1773 else if (!(State
& INSERT
))
1774 new_cursor_col
= curwin
->w_cursor
.col
;
1778 * Compute the screen column where the cursor should be.
1780 vcol
= get_indent() - vcol
;
1781 curwin
->w_virtcol
= (colnr_T
)((vcol
< 0) ? 0 : vcol
);
1784 * Advance the cursor until we reach the right screen column.
1786 vcol
= last_vcol
= 0;
1787 new_cursor_col
= -1;
1788 ptr
= ml_get_curline();
1789 while (vcol
<= (int)curwin
->w_virtcol
)
1793 if (has_mbyte
&& new_cursor_col
>= 0)
1794 new_cursor_col
+= (*mb_ptr2len
)(ptr
+ new_cursor_col
);
1798 vcol
+= lbr_chartabsize(ptr
+ new_cursor_col
, (colnr_T
)vcol
);
1803 * May need to insert spaces to be able to position the cursor on
1804 * the right screen column.
1806 if (vcol
!= (int)curwin
->w_virtcol
)
1808 curwin
->w_cursor
.col
= (colnr_T
)new_cursor_col
;
1809 i
= (int)curwin
->w_virtcol
- vcol
;
1810 ptr
= alloc((unsigned)(i
+ 1));
1813 new_cursor_col
+= i
;
1823 * When changing the indent while the cursor is in it, reset
1824 * Insstart_col to 0.
1826 insstart_less
= MAXCOL
;
1829 curwin
->w_p_list
= save_p_list
;
1831 if (new_cursor_col
<= 0)
1832 curwin
->w_cursor
.col
= 0;
1834 curwin
->w_cursor
.col
= (colnr_T
)new_cursor_col
;
1835 curwin
->w_set_curswant
= TRUE
;
1836 changed_cline_bef_curs();
1839 * May have to adjust the start of the insert.
1843 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
&& Insstart
.col
!= 0)
1845 if ((int)Insstart
.col
<= insstart_less
)
1848 Insstart
.col
-= insstart_less
;
1850 if ((int)ai_col
<= insstart_less
)
1853 ai_col
-= insstart_less
;
1857 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1858 * If the number of characters before the cursor decreased, need to pop a
1859 * few characters from the replace stack.
1860 * If the number of characters before the cursor increased, need to push a
1861 * few NULs onto the replace stack.
1863 if (REPLACE_NORMAL(State
) && start_col
>= 0)
1865 while (start_col
> (int)curwin
->w_cursor
.col
)
1867 replace_join(0); /* remove a NUL from the replace stack */
1870 while (start_col
< (int)curwin
->w_cursor
.col
|| replaced
)
1875 replace_push(replaced
);
1882 #ifdef FEAT_VREPLACE
1884 * For VREPLACE mode, we also have to fix the replace stack. In this case
1885 * it is always possible because we backspace over the whole line and then
1886 * put it back again the way we wanted it.
1888 if (State
& VREPLACE_FLAG
)
1890 /* If orig_line didn't allocate, just return. At least we did the job,
1891 * even if you can't backspace. */
1892 if (orig_line
== NULL
)
1896 new_line
= vim_strsave(ml_get_curline());
1897 if (new_line
== NULL
)
1900 /* We only put back the new line up to the cursor */
1901 new_line
[curwin
->w_cursor
.col
] = NUL
;
1903 /* Put back original line */
1904 ml_replace(curwin
->w_cursor
.lnum
, orig_line
, FALSE
);
1905 curwin
->w_cursor
.col
= orig_col
;
1907 /* Backspace from cursor to start of line */
1908 backspace_until_column(0);
1910 /* Insert new stuff into line again */
1911 ins_bytes(new_line
);
1919 * Truncate the space at the end of a line. This is to be used only in an
1920 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1924 truncate_spaces(line
)
1929 /* find start of trailing white space */
1930 for (i
= (int)STRLEN(line
) - 1; i
>= 0 && vim_iswhite(line
[i
]); i
--)
1932 if (State
& REPLACE_FLAG
)
1933 replace_join(0); /* remove a NUL from the replace stack */
1938 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1939 || defined(FEAT_COMMENTS) || defined(PROTO)
1941 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1942 * modes correctly. May also be used when not in insert mode at all.
1943 * Will attempt not to go before "col" even when there is a composing
1947 backspace_until_column(col
)
1950 while ((int)curwin
->w_cursor
.col
> col
)
1952 curwin
->w_cursor
.col
--;
1953 if (State
& REPLACE_FLAG
)
1955 else if (!del_char_after_col(col
))
1962 * Like del_char(), but make sure not to go before column "limit_col".
1963 * Only matters when there are composing characters.
1964 * Return TRUE when something was deleted.
1967 del_char_after_col(limit_col
)
1968 int limit_col UNUSED
;
1971 if (enc_utf8
&& limit_col
>= 0)
1973 colnr_T ecol
= curwin
->w_cursor
.col
+ 1;
1975 /* Make sure the cursor is at the start of a character, but
1976 * skip forward again when going too far back because of a
1977 * composing character. */
1979 while (curwin
->w_cursor
.col
< (colnr_T
)limit_col
)
1981 int l
= utf_ptr2len(ml_get_cursor());
1983 if (l
== 0) /* end of line */
1985 curwin
->w_cursor
.col
+= l
;
1987 if (*ml_get_cursor() == NUL
|| curwin
->w_cursor
.col
== ecol
)
1989 del_bytes((long)((int)ecol
- curwin
->w_cursor
.col
), FALSE
, TRUE
);
1993 (void)del_char(FALSE
);
1997 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1999 * CTRL-X pressed in Insert mode.
2004 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2005 * CTRL-V works like CTRL-N */
2006 if (ctrl_x_mode
!= CTRL_X_CMDLINE
)
2008 /* if the next ^X<> won't ADD nothing, then reset
2009 * compl_cont_status */
2010 if (compl_cont_status
& CONT_N_ADDS
)
2011 compl_cont_status
|= CONT_INTRPT
;
2013 compl_cont_status
= 0;
2014 /* We're not sure which CTRL-X mode it will be yet */
2015 ctrl_x_mode
= CTRL_X_NOT_DEFINED_YET
;
2016 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
2017 edit_submode_pre
= NULL
;
2023 * Return TRUE if the 'dict' or 'tsr' option can be used.
2026 has_compl_option(dict_opt
)
2029 if (dict_opt
? (*curbuf
->b_p_dict
== NUL
&& *p_dict
== NUL
2031 && !curwin
->w_p_spell
2034 : (*curbuf
->b_p_tsr
== NUL
&& *p_tsr
== NUL
))
2037 edit_submode
= NULL
;
2038 msg_attr(dict_opt
? (char_u
*)_("'dictionary' option is empty")
2039 : (char_u
*)_("'thesaurus' option is empty"),
2041 if (emsg_silent
== 0)
2046 ui_delay(2000L, FALSE
);
2054 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2055 * This depends on the current mode.
2058 vim_is_ctrl_x_key(c
)
2061 /* Always allow ^R - let it's results then be checked */
2065 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
2066 if (ins_compl_pum_key(c
))
2069 switch (ctrl_x_mode
)
2071 case 0: /* Not in any CTRL-X mode */
2072 return (c
== Ctrl_N
|| c
== Ctrl_P
|| c
== Ctrl_X
);
2073 case CTRL_X_NOT_DEFINED_YET
:
2074 return ( c
== Ctrl_X
|| c
== Ctrl_Y
|| c
== Ctrl_E
2075 || c
== Ctrl_L
|| c
== Ctrl_F
|| c
== Ctrl_RSB
2076 || c
== Ctrl_I
|| c
== Ctrl_D
|| c
== Ctrl_P
2077 || c
== Ctrl_N
|| c
== Ctrl_T
|| c
== Ctrl_V
2078 || c
== Ctrl_Q
|| c
== Ctrl_U
|| c
== Ctrl_O
2079 || c
== Ctrl_S
|| c
== 's');
2081 return (c
== Ctrl_Y
|| c
== Ctrl_E
);
2082 case CTRL_X_WHOLE_LINE
:
2083 return (c
== Ctrl_L
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2085 return (c
== Ctrl_F
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2086 case CTRL_X_DICTIONARY
:
2087 return (c
== Ctrl_K
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2088 case CTRL_X_THESAURUS
:
2089 return (c
== Ctrl_T
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2091 return (c
== Ctrl_RSB
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2093 case CTRL_X_PATH_PATTERNS
:
2094 return (c
== Ctrl_P
|| c
== Ctrl_N
);
2095 case CTRL_X_PATH_DEFINES
:
2096 return (c
== Ctrl_D
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2098 case CTRL_X_CMDLINE
:
2099 return (c
== Ctrl_V
|| c
== Ctrl_Q
|| c
== Ctrl_P
|| c
== Ctrl_N
2101 #ifdef FEAT_COMPL_FUNC
2102 case CTRL_X_FUNCTION
:
2103 return (c
== Ctrl_U
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2105 return (c
== Ctrl_O
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2108 return (c
== Ctrl_S
|| c
== Ctrl_P
|| c
== Ctrl_N
);
2110 EMSG(_(e_internal
));
2115 * Return TRUE when character "c" is part of the item currently being
2116 * completed. Used to decide whether to abandon complete mode when the menu
2120 ins_compl_accept_char(c
)
2123 if (ctrl_x_mode
& CTRL_X_WANT_IDENT
)
2124 /* When expanding an identifier only accept identifier chars. */
2125 return vim_isIDc(c
);
2127 switch (ctrl_x_mode
)
2130 /* When expanding file name only accept file name chars. But not
2131 * path separators, so that "proto/<Tab>" expands files in
2132 * "proto", not "proto/" as a whole */
2133 return vim_isfilec(c
) && !vim_ispathsep(c
);
2135 case CTRL_X_CMDLINE
:
2137 /* Command line and Omni completion can work with just about any
2138 * printable character, but do stop at white space. */
2139 return vim_isprintc(c
) && !vim_iswhite(c
);
2141 case CTRL_X_WHOLE_LINE
:
2142 /* For while line completion a space can be part of the line. */
2143 return vim_isprintc(c
);
2145 return vim_iswordc(c
);
2149 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
2150 * case of the originally typed text is used, and the case of the completed
2151 * text is inferred, ie this tries to work out what case you probably wanted
2152 * the rest of the word to be in -- webb
2155 ins_compl_add_infercase(str
, len
, icase
, fname
, dir
, flags
)
2165 int actual_len
; /* Take multi-byte characters */
2166 int actual_compl_length
; /* into account. */
2167 int *wca
; /* Wide character array. */
2168 int has_lower
= FALSE
;
2169 int was_letter
= FALSE
;
2171 if (p_ic
&& curbuf
->b_p_inf
&& len
> 0)
2173 /* Infer case of completed part. */
2175 /* Find actual length of completion. */
2191 /* Find actual length of original text. */
2195 p
= compl_orig_text
;
2196 actual_compl_length
= 0;
2200 ++actual_compl_length
;
2205 actual_compl_length
= compl_length
;
2207 /* Allocate wide character array for the completion and fill it. */
2208 wca
= (int *)alloc((unsigned)(actual_len
* sizeof(int)));
2212 for (i
= 0; i
< actual_len
; ++i
)
2215 wca
[i
] = mb_ptr2char_adv(&p
);
2220 /* Rule 1: Were any chars converted to lower? */
2221 p
= compl_orig_text
;
2222 for (i
= 0; i
< actual_compl_length
; ++i
)
2226 c
= mb_ptr2char_adv(&p
);
2233 if (MB_ISUPPER(wca
[i
]))
2235 /* Rule 1 is satisfied. */
2236 for (i
= actual_compl_length
; i
< actual_len
; ++i
)
2237 wca
[i
] = MB_TOLOWER(wca
[i
]);
2244 * Rule 2: No lower case, 2nd consecutive letter converted to
2249 p
= compl_orig_text
;
2250 for (i
= 0; i
< actual_compl_length
; ++i
)
2254 c
= mb_ptr2char_adv(&p
);
2258 if (was_letter
&& MB_ISUPPER(c
) && MB_ISLOWER(wca
[i
]))
2260 /* Rule 2 is satisfied. */
2261 for (i
= actual_compl_length
; i
< actual_len
; ++i
)
2262 wca
[i
] = MB_TOUPPER(wca
[i
]);
2265 was_letter
= MB_ISLOWER(c
) || MB_ISUPPER(c
);
2269 /* Copy the original case of the part we typed. */
2270 p
= compl_orig_text
;
2271 for (i
= 0; i
< actual_compl_length
; ++i
)
2275 c
= mb_ptr2char_adv(&p
);
2280 wca
[i
] = MB_TOLOWER(wca
[i
]);
2281 else if (MB_ISUPPER(c
))
2282 wca
[i
] = MB_TOUPPER(wca
[i
]);
2286 * Generate encoding specific output from wide character array.
2287 * Multi-byte characters can occupy up to five bytes more than
2288 * ASCII characters, and we also need one byte for NUL, so stay
2289 * six bytes away from the edge of IObuff.
2293 while (i
< actual_len
&& (p
- IObuff
+ 6) < IOSIZE
)
2296 p
+= (*mb_char2bytes
)(wca
[i
++], p
);
2305 return ins_compl_add(IObuff
, len
, icase
, fname
, NULL
, dir
,
2308 return ins_compl_add(str
, len
, icase
, fname
, NULL
, dir
, flags
, FALSE
);
2312 * Add a match to the list of matches.
2313 * If the given string is already in the list of completions, then return
2314 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2315 * maybe because alloc() returns NULL, then FAIL is returned.
2318 ins_compl_add(str
, len
, icase
, fname
, cptext
, cdir
, flags
, adup
)
2323 char_u
**cptext
; /* extra text for popup menu or NULL */
2326 int adup
; /* accept duplicate match */
2329 int dir
= (cdir
== 0 ? compl_direction
: cdir
);
2335 len
= (int)STRLEN(str
);
2338 * If the same match is already present, don't add it.
2340 if (compl_first_match
!= NULL
&& !adup
)
2342 match
= compl_first_match
;
2345 if ( !(match
->cp_flags
& ORIGINAL_TEXT
)
2346 && STRNCMP(match
->cp_str
, str
, len
) == 0
2347 && match
->cp_str
[len
] == NUL
)
2349 match
= match
->cp_next
;
2350 } while (match
!= NULL
&& match
!= compl_first_match
);
2353 /* Remove any popup menu before changing the list of matches. */
2354 ins_compl_del_pum();
2357 * Allocate a new match structure.
2358 * Copy the values to the new match structure.
2360 match
= (compl_T
*)alloc_clear((unsigned)sizeof(compl_T
));
2363 match
->cp_number
= -1;
2364 if (flags
& ORIGINAL_TEXT
)
2365 match
->cp_number
= 0;
2366 if ((match
->cp_str
= vim_strnsave(str
, len
)) == NULL
)
2371 match
->cp_icase
= icase
;
2374 * - compl_curr_match->cp_fname if it is a string equal to fname.
2375 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2376 * - NULL otherwise. --Acevedo */
2378 && compl_curr_match
!= NULL
2379 && compl_curr_match
->cp_fname
!= NULL
2380 && STRCMP(fname
, compl_curr_match
->cp_fname
) == 0)
2381 match
->cp_fname
= compl_curr_match
->cp_fname
;
2382 else if (fname
!= NULL
)
2384 match
->cp_fname
= vim_strsave(fname
);
2385 flags
|= FREE_FNAME
;
2388 match
->cp_fname
= NULL
;
2389 match
->cp_flags
= flags
;
2395 for (i
= 0; i
< CPT_COUNT
; ++i
)
2396 if (cptext
[i
] != NULL
&& *cptext
[i
] != NUL
)
2397 match
->cp_text
[i
] = vim_strsave(cptext
[i
]);
2401 * Link the new match structure in the list of matches.
2403 if (compl_first_match
== NULL
)
2404 match
->cp_next
= match
->cp_prev
= NULL
;
2405 else if (dir
== FORWARD
)
2407 match
->cp_next
= compl_curr_match
->cp_next
;
2408 match
->cp_prev
= compl_curr_match
;
2412 match
->cp_next
= compl_curr_match
;
2413 match
->cp_prev
= compl_curr_match
->cp_prev
;
2416 match
->cp_next
->cp_prev
= match
;
2418 match
->cp_prev
->cp_next
= match
;
2419 else /* if there's nothing before, it is the first match */
2420 compl_first_match
= match
;
2421 compl_curr_match
= match
;
2424 * Find the longest common string if still doing that.
2426 if (compl_get_longest
&& (flags
& ORIGINAL_TEXT
) == 0)
2427 ins_compl_longest_match(match
);
2433 * Return TRUE if "str[len]" matches with match->cp_str, considering
2437 ins_compl_equal(match
, str
, len
)
2442 if (match
->cp_icase
)
2443 return STRNICMP(match
->cp_str
, str
, (size_t)len
) == 0;
2444 return STRNCMP(match
->cp_str
, str
, (size_t)len
) == 0;
2448 * Reduce the longest common string for match "match".
2451 ins_compl_longest_match(match
)
2458 if (compl_leader
== NULL
)
2460 /* First match, use it as a whole. */
2461 compl_leader
= vim_strsave(match
->cp_str
);
2462 if (compl_leader
!= NULL
)
2464 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2466 ins_bytes(compl_leader
+ ins_compl_len());
2469 /* When the match isn't there (to avoid matching itself) remove it
2470 * again after redrawing. */
2473 compl_used_match
= FALSE
;
2478 /* Reduce the text if this match differs from compl_leader. */
2486 c1
= mb_ptr2char(p
);
2487 c2
= mb_ptr2char(s
);
2495 if (match
->cp_icase
? (MB_TOLOWER(c1
) != MB_TOLOWER(c2
))
2514 /* Leader was shortened, need to change the inserted text. */
2516 had_match
= (curwin
->w_cursor
.col
> compl_col
);
2518 ins_bytes(compl_leader
+ ins_compl_len());
2521 /* When the match isn't there (to avoid matching itself) remove it
2522 * again after redrawing. */
2527 compl_used_match
= FALSE
;
2532 * Add an array of matches to the list of matches.
2536 ins_compl_add_matches(num_matches
, matches
, icase
)
2543 int dir
= compl_direction
;
2545 for (i
= 0; i
< num_matches
&& add_r
!= FAIL
; i
++)
2546 if ((add_r
= ins_compl_add(matches
[i
], -1, icase
,
2547 NULL
, NULL
, dir
, 0, FALSE
)) == OK
)
2548 /* if dir was BACKWARD then honor it just once */
2550 FreeWild(num_matches
, matches
);
2553 /* Make the completion list cyclic.
2554 * Return the number of matches (excluding the original).
2557 ins_compl_make_cyclic()
2562 if (compl_first_match
!= NULL
)
2565 * Find the end of the list.
2567 match
= compl_first_match
;
2568 /* there's always an entry for the compl_orig_text, it doesn't count. */
2569 while (match
->cp_next
!= NULL
&& match
->cp_next
!= compl_first_match
)
2571 match
= match
->cp_next
;
2574 match
->cp_next
= compl_first_match
;
2575 compl_first_match
->cp_prev
= match
;
2581 * Start completion for the complete() function.
2582 * "startcol" is where the matched text starts (1 is first column).
2583 * "list" is the list of matches.
2586 set_completion(startcol
, list
)
2590 /* If already doing completions stop it. */
2591 if (ctrl_x_mode
!= 0)
2592 ins_compl_prep(' ');
2595 if (stop_arrow() == FAIL
)
2598 if (startcol
> curwin
->w_cursor
.col
)
2599 startcol
= curwin
->w_cursor
.col
;
2600 compl_col
= startcol
;
2601 compl_length
= (int)curwin
->w_cursor
.col
- (int)startcol
;
2602 /* compl_pattern doesn't need to be set */
2603 compl_orig_text
= vim_strnsave(ml_get_curline() + compl_col
, compl_length
);
2604 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
2605 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
2608 /* Handle like dictionary completion. */
2609 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
2611 ins_compl_add_list(list
);
2612 compl_matches
= ins_compl_make_cyclic();
2613 compl_started
= TRUE
;
2614 compl_used_match
= TRUE
;
2615 compl_cont_status
= 0;
2617 compl_curr_match
= compl_first_match
;
2618 ins_complete(Ctrl_N
);
2623 /* "compl_match_array" points the currently displayed list of entries in the
2624 * popup menu. It is NULL when there is no popup menu. */
2625 static pumitem_T
*compl_match_array
= NULL
;
2626 static int compl_match_arraysize
;
2629 * Update the screen and when there is any scrolling remove the popup menu.
2636 if (compl_match_array
!= NULL
)
2638 h
= curwin
->w_cline_height
;
2640 if (h
!= curwin
->w_cline_height
)
2641 ins_compl_del_pum();
2646 * Remove any popup menu.
2651 if (compl_match_array
!= NULL
)
2654 vim_free(compl_match_array
);
2655 compl_match_array
= NULL
;
2660 * Return TRUE if the popup menu should be displayed.
2665 /* 'completeopt' must contain "menu" or "menuone" */
2666 if (vim_strchr(p_cot
, 'm') == NULL
)
2669 /* The display looks bad on a B&W display. */
2680 * Return TRUE if there are two or more matches to be shown in the popup menu.
2681 * One if 'completopt' contains "menuone".
2684 pum_enough_matches()
2689 /* Don't display the popup menu if there are no matches or there is only
2690 * one (ignoring the original text). */
2691 compl = compl_first_match
;
2696 || ((compl->cp_flags
& ORIGINAL_TEXT
) == 0 && ++i
== 2))
2698 compl = compl->cp_next
;
2699 } while (compl != compl_first_match
);
2701 if (strstr((char *)p_cot
, "menuone") != NULL
)
2707 * Show the popup menu for the list of matches.
2708 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
2711 ins_compl_show_pum()
2714 compl_T
*shown_compl
= NULL
;
2715 int did_find_shown_match
= FALSE
;
2716 int shown_match_ok
= FALSE
;
2722 if (!pum_wanted() || !pum_enough_matches())
2725 #if defined(FEAT_EVAL)
2726 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2727 do_cmdline_cmd((char_u
*)"if exists('g:loaded_matchparen')|3match none|endif");
2730 /* Update the screen before drawing the popup menu over it. */
2733 if (compl_match_array
== NULL
)
2735 /* Need to build the popup menu list. */
2736 compl_match_arraysize
= 0;
2737 compl = compl_first_match
;
2738 if (compl_leader
!= NULL
)
2739 lead_len
= (int)STRLEN(compl_leader
);
2742 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2743 && (compl_leader
== NULL
2744 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2745 ++compl_match_arraysize
;
2746 compl = compl->cp_next
;
2747 } while (compl != NULL
&& compl != compl_first_match
);
2748 if (compl_match_arraysize
== 0)
2750 compl_match_array
= (pumitem_T
*)alloc_clear(
2751 (unsigned)(sizeof(pumitem_T
)
2752 * compl_match_arraysize
));
2753 if (compl_match_array
!= NULL
)
2755 /* If the current match is the original text don't find the first
2756 * match after it, don't highlight anything. */
2757 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
2758 shown_match_ok
= TRUE
;
2761 compl = compl_first_match
;
2764 if ((compl->cp_flags
& ORIGINAL_TEXT
) == 0
2765 && (compl_leader
== NULL
2766 || ins_compl_equal(compl, compl_leader
, lead_len
)))
2768 if (!shown_match_ok
)
2770 if (compl == compl_shown_match
|| did_find_shown_match
)
2772 /* This item is the shown match or this is the
2773 * first displayed item after the shown match. */
2774 compl_shown_match
= compl;
2775 did_find_shown_match
= TRUE
;
2776 shown_match_ok
= TRUE
;
2779 /* Remember this displayed match for when the
2780 * shown match is just below it. */
2781 shown_compl
= compl;
2785 if (compl->cp_text
[CPT_ABBR
] != NULL
)
2786 compl_match_array
[i
].pum_text
=
2787 compl->cp_text
[CPT_ABBR
];
2789 compl_match_array
[i
].pum_text
= compl->cp_str
;
2790 compl_match_array
[i
].pum_kind
= compl->cp_text
[CPT_KIND
];
2791 compl_match_array
[i
].pum_info
= compl->cp_text
[CPT_INFO
];
2792 if (compl->cp_text
[CPT_MENU
] != NULL
)
2793 compl_match_array
[i
++].pum_extra
=
2794 compl->cp_text
[CPT_MENU
];
2796 compl_match_array
[i
++].pum_extra
= compl->cp_fname
;
2799 if (compl == compl_shown_match
)
2801 did_find_shown_match
= TRUE
;
2803 /* When the original text is the shown match don't set
2804 * compl_shown_match. */
2805 if (compl->cp_flags
& ORIGINAL_TEXT
)
2806 shown_match_ok
= TRUE
;
2808 if (!shown_match_ok
&& shown_compl
!= NULL
)
2810 /* The shown match isn't displayed, set it to the
2811 * previously displayed match. */
2812 compl_shown_match
= shown_compl
;
2813 shown_match_ok
= TRUE
;
2816 compl = compl->cp_next
;
2817 } while (compl != NULL
&& compl != compl_first_match
);
2819 if (!shown_match_ok
) /* no displayed match at all */
2825 /* popup menu already exists, only need to find the current item.*/
2826 for (i
= 0; i
< compl_match_arraysize
; ++i
)
2827 if (compl_match_array
[i
].pum_text
== compl_shown_match
->cp_str
2828 || compl_match_array
[i
].pum_text
2829 == compl_shown_match
->cp_text
[CPT_ABBR
])
2836 if (compl_match_array
!= NULL
)
2838 /* Compute the screen column of the start of the completed text.
2839 * Use the cursor to get all wrapping and other settings right. */
2840 col
= curwin
->w_cursor
.col
;
2841 curwin
->w_cursor
.col
= compl_col
;
2842 pum_display(compl_match_array
, compl_match_arraysize
, cur
);
2843 curwin
->w_cursor
.col
= col
;
2847 #define DICT_FIRST (1) /* use just first element in "dict" */
2848 #define DICT_EXACT (2) /* "dict" is the exact name of a file */
2851 * Add any identifiers that match the given pattern in the list of dictionary
2852 * files "dict_start" to the list of completions.
2855 ins_compl_dictionaries(dict_start
, pat
, flags
, thesaurus
)
2858 int flags
; /* DICT_FIRST and/or DICT_EXACT */
2859 int thesaurus
; /* Thesaurus completion */
2861 char_u
*dict
= dict_start
;
2864 regmatch_T regmatch
;
2868 int dir
= compl_direction
;
2873 /* When 'dictionary' is empty and spell checking is enabled use
2875 if (!thesaurus
&& curwin
->w_p_spell
)
2876 dict
= (char_u
*)"spell";
2885 regmatch
.regprog
= NULL
; /* so that we can goto theend */
2887 /* If 'infercase' is set, don't use 'smartcase' here */
2889 if (curbuf
->b_p_inf
)
2892 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2893 * to only match at the start of a line. Otherwise just match the
2894 * pattern. Also need to double backslashes. */
2895 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
2897 char_u
*pat_esc
= vim_strsave_escaped(pat
, (char_u
*)"\\");
2900 if (pat_esc
== NULL
)
2902 len
= STRLEN(pat_esc
) + 10;
2903 ptr
= alloc((unsigned)len
);
2909 vim_snprintf((char *)ptr
, len
, "^\\s*\\zs\\V%s", pat_esc
);
2910 regmatch
.regprog
= vim_regcomp(ptr
, RE_MAGIC
);
2916 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
2917 if (regmatch
.regprog
== NULL
)
2921 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2922 regmatch
.rm_ic
= ignorecase(pat
);
2923 while (*dict
!= NUL
&& !got_int
&& !compl_interrupted
)
2925 /* copy one dictionary file name into buf */
2926 if (flags
== DICT_EXACT
)
2933 /* Expand wildcards in the dictionary name, but do not allow
2934 * backticks (for security, the 'dict' option may have been set in
2936 copy_option_part(&dict
, buf
, LSIZE
, ",");
2938 if (!thesaurus
&& STRCMP(buf
, "spell") == 0)
2942 if (vim_strchr(buf
, '`') != NULL
2943 || expand_wildcards(1, &buf
, &count
, &files
,
2944 EW_FILE
|EW_SILENT
) != OK
)
2951 /* Complete from active spelling. Skip "\<" in the pattern, we
2952 * don't use it as a RE. */
2953 if (pat
[0] == '\\' && pat
[1] == '<')
2957 spell_dump_compl(curbuf
, ptr
, regmatch
.rm_ic
, &dir
, 0);
2961 if (count
> 0) /* avoid warning for using "files" uninit */
2963 ins_compl_files(count
, files
, thesaurus
, flags
,
2964 ®match
, buf
, &dir
);
2965 if (flags
!= DICT_EXACT
)
2966 FreeWild(count
, files
);
2974 vim_free(regmatch
.regprog
);
2979 ins_compl_files(count
, files
, thesaurus
, flags
, regmatch
, buf
, dir
)
2984 regmatch_T
*regmatch
;
2993 for (i
= 0; i
< count
&& !got_int
&& !compl_interrupted
; i
++)
2995 fp
= mch_fopen((char *)files
[i
], "r"); /* open dictionary file */
2996 if (flags
!= DICT_EXACT
)
2998 vim_snprintf((char *)IObuff
, IOSIZE
,
2999 _("Scanning dictionary: %s"), (char *)files
[i
]);
3000 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3006 * Read dictionary file line by line.
3007 * Check each line for a match.
3009 while (!got_int
&& !compl_interrupted
3010 && !vim_fgets(buf
, LSIZE
, fp
))
3013 while (vim_regexec(regmatch
, buf
, (colnr_T
)(ptr
- buf
)))
3015 ptr
= regmatch
->startp
[0];
3016 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3017 ptr
= find_line_end(ptr
);
3019 ptr
= find_word_end(ptr
);
3020 add_r
= ins_compl_add_infercase(regmatch
->startp
[0],
3021 (int)(ptr
- regmatch
->startp
[0]),
3022 p_ic
, files
[i
], *dir
, 0);
3028 * Add the other matches on the line
3033 /* Find start of the next word. Skip white
3034 * space and punctuation. */
3035 ptr
= find_word_start(ptr
);
3036 if (*ptr
== NUL
|| *ptr
== NL
)
3040 /* Find end of the word. */
3043 /* Japanese words may have characters in
3044 * different classes, only separate words
3045 * with single-byte non-word characters. */
3048 int l
= (*mb_ptr2len
)(ptr
);
3050 if (l
< 2 && !vim_iswordc(*ptr
))
3056 ptr
= find_word_end(ptr
);
3058 /* Add the word. Skip the regexp match. */
3059 if (wstart
!= regmatch
->startp
[0])
3060 add_r
= ins_compl_add_infercase(wstart
,
3061 (int)(ptr
- wstart
),
3062 p_ic
, files
[i
], *dir
, 0);
3066 /* if dir was BACKWARD then honor it just once */
3068 else if (add_r
== FAIL
)
3070 /* avoid expensive call to vim_regexec() when at end
3072 if (*ptr
== '\n' || got_int
)
3076 ins_compl_check_keys(50);
3084 * Find the start of the next word.
3085 * Returns a pointer to the first char of the word. Also stops at a NUL.
3088 find_word_start(ptr
)
3093 while (*ptr
!= NUL
&& *ptr
!= '\n' && mb_get_class(ptr
) <= 1)
3094 ptr
+= (*mb_ptr2len
)(ptr
);
3097 while (*ptr
!= NUL
&& *ptr
!= '\n' && !vim_iswordc(*ptr
))
3103 * Find the end of the word. Assumes it starts inside a word.
3104 * Returns a pointer to just after the word.
3115 start_class
= mb_get_class(ptr
);
3116 if (start_class
> 1)
3119 ptr
+= (*mb_ptr2len
)(ptr
);
3120 if (mb_get_class(ptr
) != start_class
)
3126 while (vim_iswordc(*ptr
))
3132 * Find the end of the line, omitting CR and NL at the end.
3133 * Returns a pointer to just after the line.
3141 s
= ptr
+ STRLEN(ptr
);
3142 while (s
> ptr
&& (s
[-1] == CAR
|| s
[-1] == NL
))
3148 * Free the list of completions
3156 vim_free(compl_pattern
);
3157 compl_pattern
= NULL
;
3158 vim_free(compl_leader
);
3159 compl_leader
= NULL
;
3161 if (compl_first_match
== NULL
)
3164 ins_compl_del_pum();
3167 compl_curr_match
= compl_first_match
;
3170 match
= compl_curr_match
;
3171 compl_curr_match
= compl_curr_match
->cp_next
;
3172 vim_free(match
->cp_str
);
3173 /* several entries may use the same fname, free it just once. */
3174 if (match
->cp_flags
& FREE_FNAME
)
3175 vim_free(match
->cp_fname
);
3176 for (i
= 0; i
< CPT_COUNT
; ++i
)
3177 vim_free(match
->cp_text
[i
]);
3179 } while (compl_curr_match
!= NULL
&& compl_curr_match
!= compl_first_match
);
3180 compl_first_match
= compl_curr_match
= NULL
;
3181 compl_shown_match
= NULL
;
3187 compl_cont_status
= 0;
3188 compl_started
= FALSE
;
3190 vim_free(compl_pattern
);
3191 compl_pattern
= NULL
;
3192 vim_free(compl_leader
);
3193 compl_leader
= NULL
;
3194 edit_submode_extra
= NULL
;
3195 vim_free(compl_orig_text
);
3196 compl_orig_text
= NULL
;
3197 compl_enter_selects
= FALSE
;
3201 * Return TRUE when Insert completion is active.
3206 return compl_started
;
3210 * Delete one character before the cursor and show the subset of the matches
3211 * that match the word that is now before the cursor.
3212 * Returns the character to be used, NUL if the work is done and another char
3213 * to be got from the user.
3221 line
= ml_get_curline();
3222 p
= line
+ curwin
->w_cursor
.col
;
3223 mb_ptr_back(line
, p
);
3225 /* Stop completion when the whole word was deleted. For Omni completion
3226 * allow the word to be deleted, we won't match everything. */
3227 if ((int)(p
- line
) - (int)compl_col
< 0
3228 || ((int)(p
- line
) - (int)compl_col
== 0
3229 && (ctrl_x_mode
& CTRL_X_OMNI
) == 0))
3232 /* Deleted more than what was used to find matches or didn't finish
3233 * finding all matches: need to look for matches all over again. */
3234 if (curwin
->w_cursor
.col
<= compl_col
+ compl_length
3235 || compl_was_interrupted
)
3236 ins_compl_restart();
3238 vim_free(compl_leader
);
3239 compl_leader
= vim_strnsave(line
+ compl_col
, (int)(p
- line
) - compl_col
);
3240 if (compl_leader
!= NULL
)
3242 ins_compl_new_leader();
3249 * Called after changing "compl_leader".
3250 * Show the popup menu with a different set of matches.
3251 * May also search for matches again if the previous search was interrupted.
3254 ins_compl_new_leader()
3256 ins_compl_del_pum();
3258 ins_bytes(compl_leader
+ ins_compl_len());
3259 compl_used_match
= FALSE
;
3262 ins_compl_set_original_text(compl_leader
);
3266 spell_bad_len
= 0; /* need to redetect bad word */
3269 * Matches were cleared, need to search for them now. First display
3270 * the changed text before the cursor. Set "compl_restarting" to
3271 * avoid that the first match is inserted.
3277 /* Show the cursor after the match, not after the redrawn text. */
3280 gui_update_cursor(FALSE
, FALSE
);
3283 compl_restarting
= TRUE
;
3284 if (ins_complete(Ctrl_N
) == FAIL
)
3285 compl_cont_status
= 0;
3286 compl_restarting
= FALSE
;
3289 #if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
3290 if (!compl_used_match
)
3292 /* Go to the original text, since none of the matches is inserted. */
3293 if (compl_first_match
->cp_prev
!= NULL
3294 && (compl_first_match
->cp_prev
->cp_flags
& ORIGINAL_TEXT
))
3295 compl_shown_match
= compl_first_match
->cp_prev
;
3297 compl_shown_match
= compl_first_match
;
3298 compl_curr_match
= compl_shown_match
;
3299 compl_shows_dir
= compl_direction
;
3302 compl_enter_selects
= !compl_used_match
;
3304 /* Show the popup menu with a different set of matches. */
3305 ins_compl_show_pum();
3307 /* Don't let Enter select the original text when there is no popup menu. */
3308 if (compl_match_array
== NULL
)
3309 compl_enter_selects
= FALSE
;
3313 * Return the length of the completion, from the completion start column to
3314 * the cursor column. Making sure it never goes below zero.
3319 int off
= (int)curwin
->w_cursor
.col
- (int)compl_col
;
3327 * Append one character to the match leader. May reduce the number of
3331 ins_compl_addleader(c
)
3337 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
3339 char_u buf
[MB_MAXBYTES
+ 1];
3341 (*mb_char2bytes
)(c
, buf
);
3343 ins_char_bytes(buf
, cc
);
3349 /* If we didn't complete finding matches we must search again. */
3350 if (compl_was_interrupted
)
3351 ins_compl_restart();
3353 vim_free(compl_leader
);
3354 compl_leader
= vim_strnsave(ml_get_curline() + compl_col
,
3355 (int)(curwin
->w_cursor
.col
- compl_col
));
3356 if (compl_leader
!= NULL
)
3357 ins_compl_new_leader();
3361 * Setup for finding completions again without leaving CTRL-X mode. Used when
3362 * BS or a key was typed while still searching for matches.
3368 compl_started
= FALSE
;
3370 compl_cont_status
= 0;
3371 compl_cont_mode
= 0;
3375 * Set the first match, the original text.
3378 ins_compl_set_original_text(str
)
3383 /* Replace the original text entry. */
3384 if (compl_first_match
->cp_flags
& ORIGINAL_TEXT
) /* safety check */
3386 p
= vim_strsave(str
);
3389 vim_free(compl_first_match
->cp_str
);
3390 compl_first_match
->cp_str
= p
;
3396 * Append one character to the match leader. May reduce the number of
3400 ins_compl_addfrommatch()
3403 int len
= (int)curwin
->w_cursor
.col
- (int)compl_col
;
3407 p
= compl_shown_match
->cp_str
;
3408 if ((int)STRLEN(p
) <= len
) /* the match is too short */
3410 /* When still at the original match use the first entry that matches
3412 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
3415 for (cp
= compl_shown_match
->cp_next
; cp
!= NULL
3416 && cp
!= compl_first_match
; cp
= cp
->cp_next
)
3418 if (compl_leader
== NULL
3419 || ins_compl_equal(cp
, compl_leader
,
3420 (int)STRLEN(compl_leader
)))
3426 if (p
== NULL
|| (int)STRLEN(p
) <= len
)
3438 ins_compl_addleader(c
);
3442 * Prepare for Insert mode completion, or stop it.
3443 * Called just after typing a character in Insert mode.
3444 * Returns TRUE when the character is not to be inserted;
3454 /* Forget any previous 'special' messages if this is actually
3455 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3457 if (c
!= Ctrl_R
&& vim_is_ctrl_x_key(c
))
3458 edit_submode_extra
= NULL
;
3460 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3461 if (c
== K_SELECT
|| c
== K_MOUSEDOWN
|| c
== K_MOUSEUP
)
3464 /* Set "compl_get_longest" when finding the first matches. */
3465 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
3466 || (ctrl_x_mode
== 0 && !compl_started
))
3468 compl_get_longest
= (vim_strchr(p_cot
, 'l') != NULL
);
3469 compl_used_match
= TRUE
;
3472 if (ctrl_x_mode
== CTRL_X_NOT_DEFINED_YET
)
3475 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3476 * it will be yet. Now we decide.
3482 ctrl_x_mode
= CTRL_X_SCROLL
;
3483 if (!(State
& REPLACE_FLAG
))
3484 edit_submode
= (char_u
*)_(" (insert) Scroll (^E/^Y)");
3486 edit_submode
= (char_u
*)_(" (replace) Scroll (^E/^Y)");
3487 edit_submode_pre
= NULL
;
3491 ctrl_x_mode
= CTRL_X_WHOLE_LINE
;
3494 ctrl_x_mode
= CTRL_X_FILES
;
3497 ctrl_x_mode
= CTRL_X_DICTIONARY
;
3500 /* Simply allow ^R to happen without affecting ^X mode */
3503 ctrl_x_mode
= CTRL_X_THESAURUS
;
3505 #ifdef FEAT_COMPL_FUNC
3507 ctrl_x_mode
= CTRL_X_FUNCTION
;
3510 ctrl_x_mode
= CTRL_X_OMNI
;
3515 ctrl_x_mode
= CTRL_X_SPELL
;
3517 ++emsg_off
; /* Avoid getting the E756 error twice. */
3518 spell_back_to_badword();
3523 ctrl_x_mode
= CTRL_X_TAGS
;
3528 ctrl_x_mode
= CTRL_X_PATH_PATTERNS
;
3531 ctrl_x_mode
= CTRL_X_PATH_DEFINES
;
3536 ctrl_x_mode
= CTRL_X_CMDLINE
;
3540 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3541 * just started ^X mode, or there were enough ^X's to cancel
3542 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3543 * do normal expansion when interrupting a different mode (say
3544 * ^X^F^X^P or ^P^X^X^P, see below)
3545 * nothing changes if interrupting mode 0, (eg, the flag
3546 * doesn't change when going to ADDING mode -- Acevedo */
3547 if (!(compl_cont_status
& CONT_INTRPT
))
3548 compl_cont_status
|= CONT_LOCAL
;
3549 else if (compl_cont_mode
!= 0)
3550 compl_cont_status
&= ~CONT_LOCAL
;
3553 /* If we have typed at least 2 ^X's... for modes != 0, we set
3554 * compl_cont_status = 0 (eg, as if we had just started ^X
3556 * For mode 0, we set "compl_cont_mode" to an impossible
3557 * value, in both cases ^X^X can be used to restart the same
3558 * mode (avoiding ADDING mode).
3559 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3560 * 'complete' and local ^P expansions respectively.
3561 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3562 * mode -- Acevedo */
3565 if (compl_cont_mode
!= 0)
3566 compl_cont_status
= 0;
3568 compl_cont_mode
= CTRL_X_NOT_DEFINED_YET
;
3571 edit_submode
= NULL
;
3576 else if (ctrl_x_mode
!= 0)
3578 /* We're already in CTRL-X mode, do we stay in it? */
3579 if (!vim_is_ctrl_x_key(c
))
3581 if (ctrl_x_mode
== CTRL_X_SCROLL
)
3584 ctrl_x_mode
= CTRL_X_FINISHED
;
3585 edit_submode
= NULL
;
3590 if (compl_started
|| ctrl_x_mode
== CTRL_X_FINISHED
)
3592 /* Show error message from attempted keyword completion (probably
3593 * 'Pattern not found') until another key is hit, then go back to
3594 * showing what mode we are in. */
3596 if ((ctrl_x_mode
== 0 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_R
3597 && !ins_compl_pum_key(c
))
3598 || ctrl_x_mode
== CTRL_X_FINISHED
)
3600 /* Get here when we have finished typing a sequence of ^N and
3601 * ^P or other completion characters in CTRL-X mode. Free up
3602 * memory that was used, and make sure we can redo the insert. */
3603 if (compl_curr_match
!= NULL
|| compl_leader
!= NULL
|| c
== Ctrl_E
)
3609 * If any of the original typed text has been changed, eg when
3610 * ignorecase is set, we must add back-spaces to the redo
3611 * buffer. We add as few as necessary to delete just the part
3612 * of the original text that has changed.
3613 * When using the longest match, edited the match or used
3614 * CTRL-E then don't use the current match.
3616 if (compl_curr_match
!= NULL
&& compl_used_match
&& c
!= Ctrl_E
)
3617 ptr
= compl_curr_match
->cp_str
;
3618 else if (compl_leader
!= NULL
)
3621 ptr
= compl_orig_text
;
3622 if (compl_orig_text
!= NULL
)
3624 p
= compl_orig_text
;
3625 for (temp
= 0; p
[temp
] != NUL
&& p
[temp
] == ptr
[temp
];
3630 temp
-= (*mb_head_off
)(compl_orig_text
, p
+ temp
);
3632 for (p
+= temp
; *p
!= NUL
; mb_ptr_adv(p
))
3633 AppendCharToRedobuff(K_BS
);
3636 AppendToRedobuffLit(ptr
+ temp
, -1);
3640 want_cindent
= (can_cindent
&& cindent_on());
3643 * When completing whole lines: fix indent for 'cindent'.
3644 * Otherwise, break line if it's too long.
3646 if (compl_cont_mode
== CTRL_X_WHOLE_LINE
)
3649 /* re-indent the current line */
3653 want_cindent
= FALSE
; /* don't do it again */
3659 int prev_col
= curwin
->w_cursor
.col
;
3661 /* put the cursor on the last char, for 'tw' formatting */
3664 if (stop_arrow() == OK
)
3665 insertchar(NUL
, 0, -1);
3667 && ml_get_curline()[curwin
->w_cursor
.col
] != NUL
)
3671 /* If the popup menu is displayed pressing CTRL-Y means accepting
3672 * the selection without inserting anything. When
3673 * compl_enter_selects is set the Enter key does the same. */
3674 if ((c
== Ctrl_Y
|| (compl_enter_selects
3675 && (c
== CAR
|| c
== K_KENTER
|| c
== NL
)))
3679 /* CTRL-E means completion is Ended, go back to the typed text. */
3683 if (compl_leader
!= NULL
)
3684 ins_bytes(compl_leader
+ ins_compl_len());
3685 else if (compl_first_match
!= NULL
)
3686 ins_bytes(compl_orig_text
+ ins_compl_len());
3690 auto_format(FALSE
, TRUE
);
3693 compl_started
= FALSE
;
3695 msg_clr_cmdline(); /* necessary for "noshowmode" */
3697 compl_enter_selects
= FALSE
;
3698 if (edit_submode
!= NULL
)
3700 edit_submode
= NULL
;
3706 * Indent now if a key was typed that is in 'cinkeys'.
3708 if (want_cindent
&& in_cinkeys(KEY_COMPLETE
, ' ', inindent(0)))
3714 /* reset continue_* if we left expansion-mode, if we stay they'll be
3715 * (re)set properly in ins_complete() */
3716 if (!vim_is_ctrl_x_key(c
))
3718 compl_cont_status
= 0;
3719 compl_cont_mode
= 0;
3726 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3727 * (depending on flag) starting from buf and looking for a non-scanned
3728 * buffer (other than curbuf). curbuf is special, if it is called with
3729 * buf=curbuf then it has to be the first call for a given flag/expansion.
3731 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3734 ins_compl_next_buf(buf
, flag
)
3742 if (flag
== 'w') /* just windows */
3745 if (buf
== curbuf
) /* first call for this flag/expansion */
3747 while ((wp
= (wp
->w_next
!= NULL
? wp
->w_next
: firstwin
)) != curwin
3748 && wp
->w_buffer
->b_scanned
)
3756 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3757 * (unlisted buffers)
3758 * When completing whole lines skip unloaded buffers. */
3759 while ((buf
= (buf
->b_next
!= NULL
? buf
->b_next
: firstbuf
)) != curbuf
3763 || (buf
->b_ml
.ml_mfp
== NULL
) != (flag
== 'u')))
3769 #ifdef FEAT_COMPL_FUNC
3770 static void expand_by_function
__ARGS((int type
, char_u
*base
));
3773 * Execute user defined complete function 'completefunc' or 'omnifunc', and
3774 * get matches in "matches".
3777 expand_by_function(type
, base
)
3778 int type
; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
3786 funcname
= (type
== CTRL_X_FUNCTION
) ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
3787 if (*funcname
== NUL
)
3790 /* Call 'completefunc' to obtain the list of matches. */
3791 args
[0] = (char_u
*)"0";
3794 pos
= curwin
->w_cursor
;
3795 matchlist
= call_func_retlist(funcname
, 2, args
, FALSE
);
3796 curwin
->w_cursor
= pos
; /* restore the cursor position */
3797 if (matchlist
== NULL
)
3800 ins_compl_add_list(matchlist
);
3801 list_unref(matchlist
);
3803 #endif /* FEAT_COMPL_FUNC */
3805 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
3807 * Add completions from a list.
3810 ins_compl_add_list(list
)
3814 int dir
= compl_direction
;
3816 /* Go through the List with matches and add each of them. */
3817 for (li
= list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3819 if (ins_compl_add_tv(&li
->li_tv
, dir
) == OK
)
3820 /* if dir was BACKWARD then honor it just once */
3828 * Add a match to the list of matches from a typeval_T.
3829 * If the given string is already in the list of completions, then return
3830 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3831 * maybe because alloc() returns NULL, then FAIL is returned.
3834 ins_compl_add_tv(tv
, dir
)
3841 char_u
*(cptext
[CPT_COUNT
]);
3843 if (tv
->v_type
== VAR_DICT
&& tv
->vval
.v_dict
!= NULL
)
3845 word
= get_dict_string(tv
->vval
.v_dict
, (char_u
*)"word", FALSE
);
3846 cptext
[CPT_ABBR
] = get_dict_string(tv
->vval
.v_dict
,
3847 (char_u
*)"abbr", FALSE
);
3848 cptext
[CPT_MENU
] = get_dict_string(tv
->vval
.v_dict
,
3849 (char_u
*)"menu", FALSE
);
3850 cptext
[CPT_KIND
] = get_dict_string(tv
->vval
.v_dict
,
3851 (char_u
*)"kind", FALSE
);
3852 cptext
[CPT_INFO
] = get_dict_string(tv
->vval
.v_dict
,
3853 (char_u
*)"info", FALSE
);
3854 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"icase", FALSE
) != NULL
)
3855 icase
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"icase");
3856 if (get_dict_string(tv
->vval
.v_dict
, (char_u
*)"dup", FALSE
) != NULL
)
3857 adup
= get_dict_number(tv
->vval
.v_dict
, (char_u
*)"dup");
3861 word
= get_tv_string_chk(tv
);
3862 vim_memset(cptext
, 0, sizeof(cptext
));
3864 if (word
== NULL
|| *word
== NUL
)
3866 return ins_compl_add(word
, -1, icase
, NULL
, cptext
, dir
, 0, adup
);
3871 * Get the next expansion(s), using "compl_pattern".
3872 * The search starts at position "ini" in curbuf and in the direction
3874 * When "compl_started" is FALSE start at that position, otherwise continue
3875 * where we stopped searching before.
3876 * This may return before finding all the matches.
3877 * Return the total number of matches or -1 if still unknown -- Acevedo
3880 ins_compl_get_exp(ini
)
3883 static pos_T first_match_pos
;
3884 static pos_T last_match_pos
;
3885 static char_u
*e_cpt
= (char_u
*)""; /* curr. entry in 'complete' */
3886 static int found_all
= FALSE
; /* Found all matches of a
3888 static buf_T
*ins_buf
= NULL
; /* buffer being scanned */
3898 int found_new_match
;
3899 int type
= ctrl_x_mode
;
3901 char_u
*dict
= NULL
;
3907 for (ins_buf
= firstbuf
; ins_buf
!= NULL
; ins_buf
= ins_buf
->b_next
)
3908 ins_buf
->b_scanned
= 0;
3911 e_cpt
= (compl_cont_status
& CONT_LOCAL
)
3912 ? (char_u
*)"." : curbuf
->b_p_cpt
;
3913 last_match_pos
= first_match_pos
= *ini
;
3916 old_match
= compl_curr_match
; /* remember the last current match */
3917 pos
= (compl_direction
== FORWARD
) ? &last_match_pos
: &first_match_pos
;
3918 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3921 found_new_match
= FAIL
;
3923 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
3924 * or if found_all says this entry is done. For ^X^L only use the
3925 * entries from 'complete' that look in loaded buffers. */
3926 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3927 && (!compl_started
|| found_all
))
3930 while (*e_cpt
== ',' || *e_cpt
== ' ')
3932 if (*e_cpt
== '.' && !curbuf
->b_scanned
)
3935 first_match_pos
= *ini
;
3936 /* So that ^N can match word immediately after cursor */
3937 if (ctrl_x_mode
== 0)
3938 dec(&first_match_pos
);
3939 last_match_pos
= first_match_pos
;
3942 else if (vim_strchr((char_u
*)"buwU", *e_cpt
) != NULL
3943 && (ins_buf
= ins_compl_next_buf(ins_buf
, *e_cpt
)) != curbuf
)
3945 /* Scan a buffer, but not the current one. */
3946 if (ins_buf
->b_ml
.ml_mfp
!= NULL
) /* loaded buffer */
3948 compl_started
= TRUE
;
3949 first_match_pos
.col
= last_match_pos
.col
= 0;
3950 first_match_pos
.lnum
= ins_buf
->b_ml
.ml_line_count
+ 1;
3951 last_match_pos
.lnum
= 0;
3954 else /* unloaded buffer, scan like dictionary */
3957 if (ins_buf
->b_fname
== NULL
)
3959 type
= CTRL_X_DICTIONARY
;
3960 dict
= ins_buf
->b_fname
;
3961 dict_f
= DICT_EXACT
;
3963 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning: %s"),
3964 ins_buf
->b_fname
== NULL
3965 ? buf_spname(ins_buf
)
3966 : ins_buf
->b_sfname
== NULL
3967 ? (char *)ins_buf
->b_fname
3968 : (char *)ins_buf
->b_sfname
);
3969 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
3971 else if (*e_cpt
== NUL
)
3975 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
3977 else if (*e_cpt
== 'k' || *e_cpt
== 's')
3980 type
= CTRL_X_DICTIONARY
;
3982 type
= CTRL_X_THESAURUS
;
3983 if (*++e_cpt
!= ',' && *e_cpt
!= NUL
)
3986 dict_f
= DICT_FIRST
;
3990 else if (*e_cpt
== 'i')
3991 type
= CTRL_X_PATH_PATTERNS
;
3992 else if (*e_cpt
== 'd')
3993 type
= CTRL_X_PATH_DEFINES
;
3995 else if (*e_cpt
== ']' || *e_cpt
== 't')
3998 vim_snprintf((char *)IObuff
, IOSIZE
, _("Scanning tags."));
3999 (void)msg_trunc_attr(IObuff
, TRUE
, hl_attr(HLF_R
));
4004 /* in any case e_cpt is advanced to the next entry */
4005 (void)copy_option_part(&e_cpt
, IObuff
, IOSIZE
, ",");
4018 case CTRL_X_PATH_PATTERNS
:
4019 case CTRL_X_PATH_DEFINES
:
4020 find_pattern_in_path(compl_pattern
, compl_direction
,
4021 (int)STRLEN(compl_pattern
), FALSE
, FALSE
,
4022 (type
== CTRL_X_PATH_DEFINES
4023 && !(compl_cont_status
& CONT_SOL
))
4024 ? FIND_DEFINE
: FIND_ANY
, 1L, ACTION_EXPAND
,
4025 (linenr_T
)1, (linenr_T
)MAXLNUM
);
4029 case CTRL_X_DICTIONARY
:
4030 case CTRL_X_THESAURUS
:
4031 ins_compl_dictionaries(
4033 : (type
== CTRL_X_THESAURUS
4034 ? (*curbuf
->b_p_tsr
== NUL
4037 : (*curbuf
->b_p_dict
== NUL
4039 : curbuf
->b_p_dict
)),
4041 dict
!= NULL
? dict_f
4042 : 0, type
== CTRL_X_THESAURUS
);
4047 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4049 p_ic
= ignorecase(compl_pattern
);
4051 /* Find up to TAG_MANY matches. Avoids that an enormous number
4052 * of matches is found when compl_pattern is empty */
4053 if (find_tags(compl_pattern
, &num_matches
, &matches
,
4054 TAG_REGEXP
| TAG_NAMES
| TAG_NOIC
|
4055 TAG_INS_COMP
| (ctrl_x_mode
? TAG_VERBOSE
: 0),
4056 TAG_MANY
, curbuf
->b_ffname
) == OK
&& num_matches
> 0)
4058 ins_compl_add_matches(num_matches
, matches
, p_ic
);
4064 if (expand_wildcards(1, &compl_pattern
, &num_matches
, &matches
,
4065 EW_FILE
|EW_DIR
|EW_ADDSLASH
|EW_SILENT
) == OK
)
4068 /* May change home directory back to "~". */
4069 tilde_replace(compl_pattern
, num_matches
, matches
);
4070 ins_compl_add_matches(num_matches
, matches
,
4071 #ifdef CASE_INSENSITIVE_FILENAME
4080 case CTRL_X_CMDLINE
:
4081 if (expand_cmdline(&compl_xp
, compl_pattern
,
4082 (int)STRLEN(compl_pattern
),
4083 &num_matches
, &matches
) == EXPAND_OK
)
4084 ins_compl_add_matches(num_matches
, matches
, FALSE
);
4087 #ifdef FEAT_COMPL_FUNC
4088 case CTRL_X_FUNCTION
:
4090 expand_by_function(type
, compl_pattern
);
4096 num_matches
= expand_spelling(first_match_pos
.lnum
,
4097 compl_pattern
, &matches
);
4098 if (num_matches
> 0)
4099 ins_compl_add_matches(num_matches
, matches
, p_ic
);
4103 default: /* normal ^P/^N and ^X^L */
4105 * If 'infercase' is set, don't use 'smartcase' here
4108 if (ins_buf
->b_p_inf
)
4111 /* buffers other than curbuf are scanned from the beginning or the
4112 * end but never from the middle, thus setting nowrapscan in this
4113 * buffers is a good idea, on the other hand, we always set
4114 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4116 if (ins_buf
!= curbuf
)
4118 else if (*e_cpt
== '.')
4124 ++msg_silent
; /* Don't want messages for wrapscan. */
4126 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4127 * has added a word that was at the beginning of the line */
4128 if ( ctrl_x_mode
== CTRL_X_WHOLE_LINE
4129 || (compl_cont_status
& CONT_SOL
))
4130 found_new_match
= search_for_exact_line(ins_buf
, pos
,
4131 compl_direction
, compl_pattern
);
4133 found_new_match
= searchit(NULL
, ins_buf
, pos
,
4135 compl_pattern
, 1L, SEARCH_KEEP
+ SEARCH_NFMSG
,
4136 RE_LAST
, (linenr_T
)0, NULL
);
4140 /* set "compl_started" even on fail */
4141 compl_started
= TRUE
;
4142 first_match_pos
= *pos
;
4143 last_match_pos
= *pos
;
4145 else if (first_match_pos
.lnum
== last_match_pos
.lnum
4146 && first_match_pos
.col
== last_match_pos
.col
)
4147 found_new_match
= FAIL
;
4148 if (found_new_match
== FAIL
)
4150 if (ins_buf
== curbuf
)
4155 /* when ADDING, the text before the cursor matches, skip it */
4156 if ( (compl_cont_status
& CONT_ADDING
) && ins_buf
== curbuf
4157 && ini
->lnum
== pos
->lnum
4158 && ini
->col
== pos
->col
)
4160 ptr
= ml_get_buf(ins_buf
, pos
->lnum
, FALSE
) + pos
->col
;
4161 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4163 if (compl_cont_status
& CONT_ADDING
)
4165 if (pos
->lnum
>= ins_buf
->b_ml
.ml_line_count
)
4167 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
4169 ptr
= skipwhite(ptr
);
4171 len
= (int)STRLEN(ptr
);
4175 char_u
*tmp_ptr
= ptr
;
4177 if (compl_cont_status
& CONT_ADDING
)
4179 tmp_ptr
+= compl_length
;
4180 /* Skip if already inside a word. */
4181 if (vim_iswordp(tmp_ptr
))
4183 /* Find start of next word. */
4184 tmp_ptr
= find_word_start(tmp_ptr
);
4186 /* Find end of this word. */
4187 tmp_ptr
= find_word_end(tmp_ptr
);
4188 len
= (int)(tmp_ptr
- ptr
);
4190 if ((compl_cont_status
& CONT_ADDING
)
4191 && len
== compl_length
)
4193 if (pos
->lnum
< ins_buf
->b_ml
.ml_line_count
)
4195 /* Try next line, if any. the new word will be
4196 * "join" as if the normal command "J" was used.
4197 * IOSIZE is always greater than
4198 * compl_length, so the next STRNCPY always
4199 * works -- Acevedo */
4200 STRNCPY(IObuff
, ptr
, len
);
4201 ptr
= ml_get_buf(ins_buf
, pos
->lnum
+ 1, FALSE
);
4202 tmp_ptr
= ptr
= skipwhite(ptr
);
4203 /* Find start of next word. */
4204 tmp_ptr
= find_word_start(tmp_ptr
);
4205 /* Find end of next word. */
4206 tmp_ptr
= find_word_end(tmp_ptr
);
4209 if (*ptr
!= ')' && IObuff
[len
- 1] != TAB
)
4211 if (IObuff
[len
- 1] != ' ')
4212 IObuff
[len
++] = ' ';
4213 /* IObuf =~ "\k.* ", thus len >= 2 */
4215 && (IObuff
[len
- 2] == '.'
4216 || (vim_strchr(p_cpo
, CPO_JOINSP
)
4218 && (IObuff
[len
- 2] == '?'
4219 || IObuff
[len
- 2] == '!'))))
4220 IObuff
[len
++] = ' ';
4222 /* copy as much as possible of the new word */
4223 if (tmp_ptr
- ptr
>= IOSIZE
- len
)
4224 tmp_ptr
= ptr
+ IOSIZE
- len
- 1;
4225 STRNCPY(IObuff
+ len
, ptr
, tmp_ptr
- ptr
);
4226 len
+= (int)(tmp_ptr
- ptr
);
4227 flags
|= CONT_S_IPOS
;
4232 if (len
== compl_length
)
4236 if (ins_compl_add_infercase(ptr
, len
, p_ic
,
4237 ins_buf
== curbuf
? NULL
: ins_buf
->b_sfname
,
4238 0, flags
) != NOTDONE
)
4240 found_new_match
= OK
;
4248 /* check if compl_curr_match has changed, (e.g. other type of
4249 * expansion added something) */
4250 if (type
!= 0 && compl_curr_match
!= old_match
)
4251 found_new_match
= OK
;
4253 /* break the loop for specialized modes (use 'complete' just for the
4254 * generic ctrl_x_mode == 0) or when we've found a new match */
4255 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4256 || found_new_match
!= FAIL
)
4260 /* Fill the popup menu as soon as possible. */
4262 ins_compl_check_keys(0);
4264 if ((ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
)
4265 || compl_interrupted
)
4267 compl_started
= TRUE
;
4271 /* Mark a buffer scanned when it has been scanned completely */
4272 if (type
== 0 || type
== CTRL_X_PATH_PATTERNS
)
4273 ins_buf
->b_scanned
= TRUE
;
4275 compl_started
= FALSE
;
4278 compl_started
= TRUE
;
4280 if ((ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4281 && *e_cpt
== NUL
) /* Got to end of 'complete' */
4282 found_new_match
= FAIL
;
4284 i
= -1; /* total of matches, unknown */
4285 if (found_new_match
== FAIL
4286 || (ctrl_x_mode
!= 0 && ctrl_x_mode
!= CTRL_X_WHOLE_LINE
))
4287 i
= ins_compl_make_cyclic();
4289 /* If several matches were added (FORWARD) or the search failed and has
4290 * just been made cyclic then we have to move compl_curr_match to the next
4291 * or previous entry (if any) -- Acevedo */
4292 compl_curr_match
= compl_direction
== FORWARD
? old_match
->cp_next
4293 : old_match
->cp_prev
;
4294 if (compl_curr_match
== NULL
)
4295 compl_curr_match
= old_match
;
4299 /* Delete the old text being completed. */
4306 * In insert mode: Delete the typed part.
4307 * In replace mode: Put the old characters back, if any.
4309 i
= compl_col
+ (compl_cont_status
& CONT_ADDING
? compl_length
: 0);
4310 backspace_until_column(i
);
4311 changed_cline_bef_curs();
4314 /* Insert the new text being completed. */
4318 ins_bytes(compl_shown_match
->cp_str
+ ins_compl_len());
4319 if (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
)
4320 compl_used_match
= FALSE
;
4322 compl_used_match
= TRUE
;
4326 * Fill in the next completion in the current direction.
4327 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4328 * get more completions. If it is FALSE, then we just do nothing when there
4329 * are no more completions in a given direction. The latter case is used when
4330 * we are still in the middle of finding completions, to allow browsing
4331 * through the ones found so far.
4332 * Return the total number of matches, or -1 if still unknown -- webb.
4334 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4335 * compl_shown_match here.
4337 * Note that this function may be called recursively once only. First with
4338 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4339 * calls this function with "allow_get_expansion" FALSE.
4342 ins_compl_next(allow_get_expansion
, count
, insert_match
)
4343 int allow_get_expansion
;
4344 int count
; /* repeat completion this many times; should
4346 int insert_match
; /* Insert the newly selected match */
4348 int num_matches
= -1;
4351 compl_T
*found_compl
= NULL
;
4352 int found_end
= FALSE
;
4355 if (compl_leader
!= NULL
4356 && (compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0)
4358 /* Set "compl_shown_match" to the actually shown match, it may differ
4359 * when "compl_leader" is used to omit some of the matches. */
4360 while (!ins_compl_equal(compl_shown_match
,
4361 compl_leader
, (int)STRLEN(compl_leader
))
4362 && compl_shown_match
->cp_next
!= NULL
4363 && compl_shown_match
->cp_next
!= compl_first_match
)
4364 compl_shown_match
= compl_shown_match
->cp_next
;
4366 /* If we didn't find it searching forward, and compl_shows_dir is
4367 * backward, find the last match. */
4368 if (compl_shows_dir
== BACKWARD
4369 && !ins_compl_equal(compl_shown_match
,
4370 compl_leader
, (int)STRLEN(compl_leader
))
4371 && (compl_shown_match
->cp_next
== NULL
4372 || compl_shown_match
->cp_next
== compl_first_match
))
4374 while (!ins_compl_equal(compl_shown_match
,
4375 compl_leader
, (int)STRLEN(compl_leader
))
4376 && compl_shown_match
->cp_prev
!= NULL
4377 && compl_shown_match
->cp_prev
!= compl_first_match
)
4378 compl_shown_match
= compl_shown_match
->cp_prev
;
4382 if (allow_get_expansion
&& insert_match
4383 && (!(compl_get_longest
|| compl_restarting
) || compl_used_match
))
4384 /* Delete old text to be replaced */
4387 /* When finding the longest common text we stick at the original text,
4388 * don't let CTRL-N or CTRL-P move to the first match. */
4389 advance
= count
!= 1 || !allow_get_expansion
|| !compl_get_longest
;
4391 /* When restarting the search don't insert the first match either. */
4392 if (compl_restarting
)
4395 compl_restarting
= FALSE
;
4398 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4402 if (compl_shows_dir
== FORWARD
&& compl_shown_match
->cp_next
!= NULL
)
4404 compl_shown_match
= compl_shown_match
->cp_next
;
4405 found_end
= (compl_first_match
!= NULL
4406 && (compl_shown_match
->cp_next
== compl_first_match
4407 || compl_shown_match
== compl_first_match
));
4409 else if (compl_shows_dir
== BACKWARD
4410 && compl_shown_match
->cp_prev
!= NULL
)
4412 found_end
= (compl_shown_match
== compl_first_match
);
4413 compl_shown_match
= compl_shown_match
->cp_prev
;
4414 found_end
|= (compl_shown_match
== compl_first_match
);
4418 if (!allow_get_expansion
)
4422 if (compl_shows_dir
== BACKWARD
)
4423 compl_pending
-= todo
+ 1;
4425 compl_pending
+= todo
+ 1;
4432 if (compl_shows_dir
== BACKWARD
)
4439 num_matches
= ins_compl_get_exp(&compl_startpos
);
4441 /* handle any pending completions */
4442 while (compl_pending
!= 0 && compl_direction
== compl_shows_dir
4445 if (compl_pending
> 0 && compl_shown_match
->cp_next
!= NULL
)
4447 compl_shown_match
= compl_shown_match
->cp_next
;
4450 if (compl_pending
< 0 && compl_shown_match
->cp_prev
!= NULL
)
4452 compl_shown_match
= compl_shown_match
->cp_prev
;
4460 if ((compl_shown_match
->cp_flags
& ORIGINAL_TEXT
) == 0
4461 && compl_leader
!= NULL
4462 && !ins_compl_equal(compl_shown_match
,
4463 compl_leader
, (int)STRLEN(compl_leader
)))
4466 /* Remember a matching item. */
4467 found_compl
= compl_shown_match
;
4469 /* Stop at the end of the list when we found a usable match. */
4472 if (found_compl
!= NULL
)
4474 compl_shown_match
= found_compl
;
4477 todo
= 1; /* use first usable match after wrapping around */
4481 /* Insert the text of the new completion, or the compl_leader. */
4484 if (!compl_get_longest
|| compl_used_match
)
4487 ins_bytes(compl_leader
+ ins_compl_len());
4490 compl_used_match
= FALSE
;
4492 if (!allow_get_expansion
)
4494 /* may undisplay the popup menu first */
4495 ins_compl_upd_pum();
4497 /* redraw to show the user what was inserted */
4500 /* display the updated popup menu */
4501 ins_compl_show_pum();
4505 /* Show the cursor after the match, not after the redrawn text. */
4508 gui_update_cursor(FALSE
, FALSE
);
4512 /* Delete old text to be replaced, since we're still searching and
4513 * don't want to match ourselves! */
4517 /* Enter will select a match when the match wasn't inserted and the popup
4518 * menu is visible. */
4519 compl_enter_selects
= !insert_match
&& compl_match_array
!= NULL
;
4522 * Show the file name for the match (if any)
4523 * Truncate the file name to avoid a wait for return.
4525 if (compl_shown_match
->cp_fname
!= NULL
)
4527 STRCPY(IObuff
, "match in file ");
4528 i
= (vim_strsize(compl_shown_match
->cp_fname
) + 16) - sc_col
;
4532 STRCAT(IObuff
, "<");
4533 STRCAT(IObuff
, compl_shown_match
->cp_fname
+ i
);
4535 redraw_cmdline
= FALSE
; /* don't overwrite! */
4542 * Call this while finding completions, to check whether the user has hit a key
4543 * that should change the currently displayed completion, or exit completion
4544 * mode. Also, when compl_pending is not zero, show a completion as soon as
4546 * "frequency" specifies out of how many calls we actually check.
4549 ins_compl_check_keys(frequency
)
4552 static int count
= 0;
4556 /* Don't check when reading keys from a script. That would break the test
4561 /* Only do this at regular intervals */
4562 if (++count
< frequency
)
4566 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4567 * can't do its work correctly. */
4571 if (vim_is_ctrl_x_key(c
) && c
!= Ctrl_X
&& c
!= Ctrl_R
)
4573 c
= safe_vgetc(); /* Eat the character */
4574 compl_shows_dir
= ins_compl_key2dir(c
);
4575 (void)ins_compl_next(FALSE
, ins_compl_key2count(c
),
4576 c
!= K_UP
&& c
!= K_DOWN
);
4580 /* Need to get the character to have KeyTyped set. We'll put it
4581 * back with vungetc() below. But skip K_IGNORE. */
4585 /* Don't interrupt completion when the character wasn't typed,
4586 * e.g., when doing @q to replay keys. */
4587 if (c
!= Ctrl_R
&& KeyTyped
)
4588 compl_interrupted
= TRUE
;
4594 if (compl_pending
!= 0 && !got_int
)
4596 int todo
= compl_pending
> 0 ? compl_pending
: -compl_pending
;
4599 (void)ins_compl_next(FALSE
, todo
, TRUE
);
4604 * Decide the direction of Insert mode complete from the key typed.
4605 * Returns BACKWARD or FORWARD.
4608 ins_compl_key2dir(c
)
4611 if (c
== Ctrl_P
|| c
== Ctrl_L
4612 || (pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
4613 || c
== K_S_UP
|| c
== K_UP
)))
4619 * Return TRUE for keys that are used for completion only when the popup menu
4623 ins_compl_pum_key(c
)
4626 return pum_visible() && (c
== K_PAGEUP
|| c
== K_KPAGEUP
|| c
== K_S_UP
4627 || c
== K_PAGEDOWN
|| c
== K_KPAGEDOWN
|| c
== K_S_DOWN
4628 || c
== K_UP
|| c
== K_DOWN
);
4632 * Decide the number of completions to move forward.
4633 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4636 ins_compl_key2count(c
)
4641 if (ins_compl_pum_key(c
) && c
!= K_UP
&& c
!= K_DOWN
)
4643 h
= pum_get_height();
4645 h
-= 2; /* keep some context */
4652 * Return TRUE if completion with "c" should insert the match, FALSE if only
4653 * to change the currently selected completion.
4656 ins_compl_use_match(c
)
4675 * Do Insert mode completion.
4676 * Called when character "c" was typed, which has a meaning for completion.
4677 * Returns OK if completion was done, FAIL if something failed (out of mem).
4684 int startcol
= 0; /* column where searched text starts */
4685 colnr_T curs_col
; /* cursor column */
4689 compl_direction
= ins_compl_key2dir(c
);
4692 /* First time we hit ^N or ^P (in a row, I mean) */
4695 #ifdef FEAT_SMARTINDENT
4698 can_si_back
= FALSE
;
4700 if (stop_arrow() == FAIL
)
4703 line
= ml_get(curwin
->w_cursor
.lnum
);
4704 curs_col
= curwin
->w_cursor
.col
;
4707 /* If this same ctrl_x_mode has been interrupted use the text from
4708 * "compl_startpos" to the cursor as a pattern to add a new word
4709 * instead of expand the one before the cursor, in word-wise if
4710 * "compl_startpos" is not in the same line as the cursor then fix it
4711 * (the line has been split because it was longer than 'tw'). if SOL
4712 * is set then skip the previous pattern, a word at the beginning of
4713 * the line has been inserted, we'll look for that -- Acevedo. */
4714 if ((compl_cont_status
& CONT_INTRPT
) == CONT_INTRPT
4715 && compl_cont_mode
== ctrl_x_mode
)
4718 * it is a continued search
4720 compl_cont_status
&= ~CONT_INTRPT
; /* remove INTRPT */
4721 if (ctrl_x_mode
== 0 || ctrl_x_mode
== CTRL_X_PATH_PATTERNS
4722 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4724 if (compl_startpos
.lnum
!= curwin
->w_cursor
.lnum
)
4726 /* line (probably) wrapped, set compl_startpos to the
4727 * first non_blank in the line, if it is not a wordchar
4728 * include it to get a better pattern, but then we don't
4729 * want the "\\<" prefix, check it bellow */
4730 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4731 compl_startpos
.col
= compl_col
;
4732 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
4733 compl_cont_status
&= ~CONT_SOL
; /* clear SOL if present */
4737 /* S_IPOS was set when we inserted a word that was at the
4738 * beginning of the line, which means that we'll go to SOL
4739 * mode but first we need to redefine compl_startpos */
4740 if (compl_cont_status
& CONT_S_IPOS
)
4742 compl_cont_status
|= CONT_SOL
;
4743 compl_startpos
.col
= (colnr_T
)(skipwhite(
4745 + compl_startpos
.col
) - line
);
4747 compl_col
= compl_startpos
.col
;
4749 compl_length
= curwin
->w_cursor
.col
- (int)compl_col
;
4750 /* IObuff is used to add a "word from the next line" would we
4751 * have enough space? just being paranoid */
4752 #define MIN_SPACE 75
4753 if (compl_length
> (IOSIZE
- MIN_SPACE
))
4755 compl_cont_status
&= ~CONT_SOL
;
4756 compl_length
= (IOSIZE
- MIN_SPACE
);
4757 compl_col
= curwin
->w_cursor
.col
- compl_length
;
4759 compl_cont_status
|= CONT_ADDING
| CONT_N_ADDS
;
4760 if (compl_length
< 1)
4761 compl_cont_status
&= CONT_LOCAL
;
4763 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4764 compl_cont_status
= CONT_ADDING
| CONT_N_ADDS
;
4766 compl_cont_status
= 0;
4769 compl_cont_status
&= CONT_LOCAL
;
4771 if (!(compl_cont_status
& CONT_ADDING
)) /* normal expansion */
4773 compl_cont_mode
= ctrl_x_mode
;
4774 if (ctrl_x_mode
!= 0) /* Remove LOCAL if ctrl_x_mode != 0 */
4775 compl_cont_status
= 0;
4776 compl_cont_status
|= CONT_N_ADDS
;
4777 compl_startpos
= curwin
->w_cursor
;
4778 startcol
= (int)curs_col
;
4782 /* Work out completion pattern and original text -- webb */
4783 if (ctrl_x_mode
== 0 || (ctrl_x_mode
& CTRL_X_WANT_IDENT
))
4785 if ((compl_cont_status
& CONT_SOL
)
4786 || ctrl_x_mode
== CTRL_X_PATH_DEFINES
)
4788 if (!(compl_cont_status
& CONT_ADDING
))
4790 while (--startcol
>= 0 && vim_isIDc(line
[startcol
]))
4792 compl_col
+= ++startcol
;
4793 compl_length
= curs_col
- startcol
;
4796 compl_pattern
= str_foldcase(line
+ compl_col
,
4797 compl_length
, NULL
, 0);
4799 compl_pattern
= vim_strnsave(line
+ compl_col
,
4801 if (compl_pattern
== NULL
)
4804 else if (compl_cont_status
& CONT_ADDING
)
4806 char_u
*prefix
= (char_u
*)"\\<";
4808 /* we need up to 2 extra chars for the prefix */
4809 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4811 if (compl_pattern
== NULL
)
4813 if (!vim_iswordp(line
+ compl_col
)
4817 vim_iswordp(mb_prevptr(line
, line
+ compl_col
))
4819 vim_iswordc(line
[compl_col
- 1])
4822 prefix
= (char_u
*)"";
4823 STRCPY((char *)compl_pattern
, prefix
);
4824 (void)quote_meta(compl_pattern
+ STRLEN(prefix
),
4825 line
+ compl_col
, compl_length
);
4827 else if (--startcol
< 0 ||
4829 !vim_iswordp(mb_prevptr(line
, line
+ startcol
+ 1))
4831 !vim_iswordc(line
[startcol
])
4835 /* Match any word of at least two chars */
4836 compl_pattern
= vim_strsave((char_u
*)"\\<\\k\\k");
4837 if (compl_pattern
== NULL
)
4839 compl_col
+= curs_col
;
4845 /* Search the point of change class of multibyte character
4846 * or not a word single byte character backward. */
4852 startcol
-= (*mb_head_off
)(line
, line
+ startcol
);
4853 base_class
= mb_get_class(line
+ startcol
);
4854 while (--startcol
>= 0)
4856 head_off
= (*mb_head_off
)(line
, line
+ startcol
);
4857 if (base_class
!= mb_get_class(line
+ startcol
4860 startcol
-= head_off
;
4865 while (--startcol
>= 0 && vim_iswordc(line
[startcol
]))
4867 compl_col
+= ++startcol
;
4868 compl_length
= (int)curs_col
- startcol
;
4869 if (compl_length
== 1)
4871 /* Only match word with at least two chars -- webb
4872 * there's no need to call quote_meta,
4873 * alloc(7) is enough -- Acevedo
4875 compl_pattern
= alloc(7);
4876 if (compl_pattern
== NULL
)
4878 STRCPY((char *)compl_pattern
, "\\<");
4879 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
, 1);
4880 STRCAT((char *)compl_pattern
, "\\k");
4884 compl_pattern
= alloc(quote_meta(NULL
, line
+ compl_col
,
4886 if (compl_pattern
== NULL
)
4888 STRCPY((char *)compl_pattern
, "\\<");
4889 (void)quote_meta(compl_pattern
+ 2, line
+ compl_col
,
4894 else if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
4896 compl_col
= (colnr_T
)(skipwhite(line
) - line
);
4897 compl_length
= (int)curs_col
- (int)compl_col
;
4898 if (compl_length
< 0) /* cursor in indent: empty pattern */
4901 compl_pattern
= str_foldcase(line
+ compl_col
, compl_length
,
4904 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4905 if (compl_pattern
== NULL
)
4908 else if (ctrl_x_mode
== CTRL_X_FILES
)
4910 while (--startcol
>= 0 && vim_isfilec(line
[startcol
]))
4912 compl_col
+= ++startcol
;
4913 compl_length
= (int)curs_col
- startcol
;
4914 compl_pattern
= addstar(line
+ compl_col
, compl_length
,
4916 if (compl_pattern
== NULL
)
4919 else if (ctrl_x_mode
== CTRL_X_CMDLINE
)
4921 compl_pattern
= vim_strnsave(line
, curs_col
);
4922 if (compl_pattern
== NULL
)
4924 set_cmd_context(&compl_xp
, compl_pattern
,
4925 (int)STRLEN(compl_pattern
), curs_col
);
4926 if (compl_xp
.xp_context
== EXPAND_UNSUCCESSFUL
4927 || compl_xp
.xp_context
== EXPAND_NOTHING
)
4928 /* No completion possible, use an empty pattern to get a
4929 * "pattern not found" message. */
4930 compl_col
= curs_col
;
4932 compl_col
= (int)(compl_xp
.xp_pattern
- compl_pattern
);
4933 compl_length
= curs_col
- compl_col
;
4935 else if (ctrl_x_mode
== CTRL_X_FUNCTION
|| ctrl_x_mode
== CTRL_X_OMNI
)
4937 #ifdef FEAT_COMPL_FUNC
4939 * Call user defined function 'completefunc' with "a:findstart"
4940 * set to 1 to obtain the length of text to use for completion.
4947 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
4949 funcname
= ctrl_x_mode
== CTRL_X_FUNCTION
4950 ? curbuf
->b_p_cfu
: curbuf
->b_p_ofu
;
4951 if (*funcname
== NUL
)
4953 EMSG2(_(e_notset
), ctrl_x_mode
== CTRL_X_FUNCTION
4954 ? "completefunc" : "omnifunc");
4958 args
[0] = (char_u
*)"1";
4960 pos
= curwin
->w_cursor
;
4961 col
= call_func_retnr(funcname
, 2, args
, FALSE
);
4962 curwin
->w_cursor
= pos
; /* restore the cursor position */
4967 if (compl_col
> curs_col
)
4968 compl_col
= curs_col
;
4970 /* Setup variables for completion. Need to obtain "line" again,
4971 * it may have become invalid. */
4972 line
= ml_get(curwin
->w_cursor
.lnum
);
4973 compl_length
= curs_col
- compl_col
;
4974 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4975 if (compl_pattern
== NULL
)
4979 else if (ctrl_x_mode
== CTRL_X_SPELL
)
4982 if (spell_bad_len
> 0)
4983 compl_col
= curs_col
- spell_bad_len
;
4985 compl_col
= spell_word_start(startcol
);
4986 if (compl_col
>= (colnr_T
)startcol
)
4989 compl_col
= curs_col
;
4993 spell_expand_check_cap(compl_col
);
4994 compl_length
= (int)curs_col
- compl_col
;
4996 /* Need to obtain "line" again, it may have become invalid. */
4997 line
= ml_get(curwin
->w_cursor
.lnum
);
4998 compl_pattern
= vim_strnsave(line
+ compl_col
, compl_length
);
4999 if (compl_pattern
== NULL
)
5005 EMSG2(_(e_intern2
), "ins_complete()");
5009 if (compl_cont_status
& CONT_ADDING
)
5011 edit_submode_pre
= (char_u
*)_(" Adding");
5012 if (ctrl_x_mode
== CTRL_X_WHOLE_LINE
)
5014 /* Insert a new line, keep indentation but ignore 'comments' */
5015 #ifdef FEAT_COMMENTS
5016 char_u
*old
= curbuf
->b_p_com
;
5018 curbuf
->b_p_com
= (char_u
*)"";
5020 compl_startpos
.lnum
= curwin
->w_cursor
.lnum
;
5021 compl_startpos
.col
= compl_col
;
5023 #ifdef FEAT_COMMENTS
5024 curbuf
->b_p_com
= old
;
5027 compl_col
= curwin
->w_cursor
.col
;
5032 edit_submode_pre
= NULL
;
5033 compl_startpos
.col
= compl_col
;
5036 if (compl_cont_status
& CONT_LOCAL
)
5037 edit_submode
= (char_u
*)_(ctrl_x_msgs
[CTRL_X_LOCAL_MSG
]);
5039 edit_submode
= (char_u
*)_(CTRL_X_MSG(ctrl_x_mode
));
5041 /* Always add completion for the original text. */
5042 vim_free(compl_orig_text
);
5043 compl_orig_text
= vim_strnsave(line
+ compl_col
, compl_length
);
5044 if (compl_orig_text
== NULL
|| ins_compl_add(compl_orig_text
,
5045 -1, p_ic
, NULL
, NULL
, 0, ORIGINAL_TEXT
, FALSE
) != OK
)
5047 vim_free(compl_pattern
);
5048 compl_pattern
= NULL
;
5049 vim_free(compl_orig_text
);
5050 compl_orig_text
= NULL
;
5054 /* showmode might reset the internal line pointers, so it must
5055 * be called before line = ml_get(), or when this address is no
5056 * longer needed. -- Acevedo.
5058 edit_submode_extra
= (char_u
*)_("-- Searching...");
5059 edit_submode_highl
= HLF_COUNT
;
5061 edit_submode_extra
= NULL
;
5065 compl_shown_match
= compl_curr_match
;
5066 compl_shows_dir
= compl_direction
;
5069 * Find next match (and following matches).
5071 save_w_wrow
= curwin
->w_wrow
;
5072 n
= ins_compl_next(TRUE
, ins_compl_key2count(c
), ins_compl_use_match(c
));
5074 /* may undisplay the popup menu */
5075 ins_compl_upd_pum();
5077 if (n
> 1) /* all matches have been found */
5079 compl_curr_match
= compl_shown_match
;
5080 compl_direction
= compl_shows_dir
;
5082 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5084 if (got_int
&& !global_busy
)
5090 /* we found no match if the list has only the "compl_orig_text"-entry */
5091 if (compl_first_match
== compl_first_match
->cp_next
)
5093 edit_submode_extra
= (compl_cont_status
& CONT_ADDING
)
5095 ? (char_u
*)_(e_hitend
) : (char_u
*)_(e_patnotf
);
5096 edit_submode_highl
= HLF_E
;
5097 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5098 * because we couldn't expand anything at first place, but if we used
5099 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5100 * (such as M in M'exico) if not tried already. -- Acevedo */
5101 if ( compl_length
> 1
5102 || (compl_cont_status
& CONT_ADDING
)
5103 || (ctrl_x_mode
!= 0
5104 && ctrl_x_mode
!= CTRL_X_PATH_PATTERNS
5105 && ctrl_x_mode
!= CTRL_X_PATH_DEFINES
))
5106 compl_cont_status
&= ~CONT_N_ADDS
;
5109 if (compl_curr_match
->cp_flags
& CONT_S_IPOS
)
5110 compl_cont_status
|= CONT_S_IPOS
;
5112 compl_cont_status
&= ~CONT_S_IPOS
;
5114 if (edit_submode_extra
== NULL
)
5116 if (compl_curr_match
->cp_flags
& ORIGINAL_TEXT
)
5118 edit_submode_extra
= (char_u
*)_("Back at original");
5119 edit_submode_highl
= HLF_W
;
5121 else if (compl_cont_status
& CONT_S_IPOS
)
5123 edit_submode_extra
= (char_u
*)_("Word from other line");
5124 edit_submode_highl
= HLF_COUNT
;
5126 else if (compl_curr_match
->cp_next
== compl_curr_match
->cp_prev
)
5128 edit_submode_extra
= (char_u
*)_("The only match");
5129 edit_submode_highl
= HLF_COUNT
;
5133 /* Update completion sequence number when needed. */
5134 if (compl_curr_match
->cp_number
== -1)
5139 if (compl_direction
== FORWARD
)
5141 /* search backwards for the first valid (!= -1) number.
5142 * This should normally succeed already at the first loop
5143 * cycle, so it's fast! */
5144 for (match
= compl_curr_match
->cp_prev
; match
!= NULL
5145 && match
!= compl_first_match
;
5146 match
= match
->cp_prev
)
5147 if (match
->cp_number
!= -1)
5149 number
= match
->cp_number
;
5153 /* go up and assign all numbers which are not assigned
5155 for (match
= match
->cp_next
;
5156 match
!= NULL
&& match
->cp_number
== -1;
5157 match
= match
->cp_next
)
5158 match
->cp_number
= ++number
;
5162 /* search forwards (upwards) for the first valid (!= -1)
5163 * number. This should normally succeed already at the
5164 * first loop cycle, so it's fast! */
5165 for (match
= compl_curr_match
->cp_next
; match
!= NULL
5166 && match
!= compl_first_match
;
5167 match
= match
->cp_next
)
5168 if (match
->cp_number
!= -1)
5170 number
= match
->cp_number
;
5174 /* go down and assign all numbers which are not
5176 for (match
= match
->cp_prev
; match
5177 && match
->cp_number
== -1;
5178 match
= match
->cp_prev
)
5179 match
->cp_number
= ++number
;
5183 /* The match should always have a sequence number now, this is
5184 * just a safety check. */
5185 if (compl_curr_match
->cp_number
!= -1)
5187 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5188 * Translations may need more than twice that. */
5189 static char_u match_ref
[81];
5191 if (compl_matches
> 0)
5192 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
5193 _("match %d of %d"),
5194 compl_curr_match
->cp_number
, compl_matches
);
5196 vim_snprintf((char *)match_ref
, sizeof(match_ref
),
5198 compl_curr_match
->cp_number
);
5199 edit_submode_extra
= match_ref
;
5200 edit_submode_highl
= HLF_R
;
5202 curs_columns(FALSE
);
5207 /* Show a message about what (completion) mode we're in. */
5209 if (edit_submode_extra
!= NULL
)
5212 msg_attr(edit_submode_extra
,
5213 edit_submode_highl
< HLF_COUNT
5214 ? hl_attr(edit_submode_highl
) : 0);
5217 msg_clr_cmdline(); /* necessary for "noshowmode" */
5219 /* Show the popup menu, unless we got interrupted. */
5220 if (!compl_interrupted
)
5222 /* RedrawingDisabled may be set when invoked through complete(). */
5223 n
= RedrawingDisabled
;
5224 RedrawingDisabled
= 0;
5226 /* If the cursor moved we need to remove the pum first. */
5228 if (save_w_wrow
!= curwin
->w_wrow
)
5229 ins_compl_del_pum();
5231 ins_compl_show_pum();
5233 RedrawingDisabled
= n
;
5235 compl_was_interrupted
= compl_interrupted
;
5236 compl_interrupted
= FALSE
;
5242 * Looks in the first "len" chars. of "src" for search-metachars.
5243 * If dest is not NULL the chars. are copied there quoting (with
5244 * a backslash) the metachars, and dest would be NUL terminated.
5245 * Returns the length (needed) of dest
5248 quote_meta(dest
, src
, len
)
5253 unsigned m
= (unsigned)len
+ 1; /* one extra for the NUL */
5255 for ( ; --len
>= 0; src
++)
5262 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5263 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5266 if (!p_magic
) /* quote these only if magic is set */
5269 if (ctrl_x_mode
== CTRL_X_DICTIONARY
5270 || ctrl_x_mode
== CTRL_X_THESAURUS
)
5272 case '^': /* currently it's not needed. */
5282 /* Copy remaining bytes of a multibyte character. */
5287 mb_len
= (*mb_ptr2len
)(src
) - 1;
5288 if (mb_len
> 0 && len
>= mb_len
)
5289 for (i
= 0; i
< mb_len
; ++i
)
5304 #endif /* FEAT_INS_EXPAND */
5307 * Next character is interpreted literally.
5308 * A one, two or three digit decimal number is interpreted as its byte value.
5309 * If one or two digits are entered, the next character is given to vungetc().
5310 * For Unicode a character > 255 may be returned.
5329 * In GUI there is no point inserting the internal code for a special key.
5330 * It is more useful to insert the string "<KEY>" instead. This would
5331 * probably be useful in a text window too, but it would not be
5332 * vi-compatible (maybe there should be an option for it?) -- webb
5337 #ifdef USE_ON_FLY_SCROLL
5338 dont_scroll
= TRUE
; /* disallow scrolling here */
5340 ++no_mapping
; /* don't map the next key hits */
5346 #ifdef FEAT_CMDL_INFO
5347 if (!(State
& CMDLINE
)
5349 && MB_BYTE2LEN_CHECK(nc
) == 1
5354 if (nc
== 'x' || nc
== 'X')
5356 else if (nc
== 'o' || nc
== 'O')
5359 else if (nc
== 'u' || nc
== 'U')
5370 if (!vim_isxdigit(nc
))
5372 cc
= cc
* 16 + hex2nr(nc
);
5376 if (nc
< '0' || nc
> '7')
5378 cc
= cc
* 8 + nc
- '0';
5382 if (!VIM_ISDIGIT(nc
))
5384 cc
= cc
* 10 + nc
- '0';
5395 cc
= 255; /* limit range to 0-255 */
5398 if (hex
) /* hex: up to two chars */
5404 else if (unicode
) /* Unicode: up to four or eight chars */
5406 if ((unicode
== 'u' && i
>= 4) || (unicode
== 'U' && i
>= 8))
5410 else if (i
>= 3) /* decimal or octal: up to three chars */
5413 if (i
== 0) /* no number entered */
5415 if (nc
== K_ZERO
) /* NUL is stored as NL */
5427 if (cc
== 0) /* NUL is stored as NL */
5430 if (enc_dbcs
&& (cc
& 0xff) == 0)
5431 cc
= '?'; /* don't accept an illegal DBCS char, the NUL in the
5432 second byte will cause trouble! */
5442 got_int
= FALSE
; /* CTRL-C typed after CTRL-V is not an interrupt */
5447 * Insert character, taking care of special keys and mod_mask
5450 insert_special(c
, allow_modmask
, ctrlv
)
5453 int ctrlv
; /* c was typed after CTRL-V */
5459 * Special function key, translate into "<Key>". Up to the last '>' is
5460 * inserted with ins_str(), so as not to replace characters in replace
5462 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5463 * unless 'allow_modmask' is TRUE.
5466 /* Command-key never produces a normal key */
5467 if (mod_mask
& MOD_MASK_CMD
)
5468 allow_modmask
= TRUE
;
5470 if (IS_SPECIAL(c
) || (mod_mask
&& allow_modmask
))
5472 p
= get_special_key_name(c
, mod_mask
);
5473 len
= (int)STRLEN(p
);
5477 if (stop_arrow() == FAIL
)
5481 AppendToRedobuffLit(p
, -1);
5485 if (stop_arrow() == OK
)
5486 insertchar(c
, ctrlv
? INSCHAR_CTRLV
: 0, -1);
5490 * Special characters in this context are those that need processing other
5491 * than the simple insertion that can be performed here. This includes ESC
5492 * which terminates the insert, and CR/NL which need special processing to
5493 * open up a new line. This routine tries to optimize insertions performed by
5494 * the "redo", "undo" or "put" commands, so it needs to know when it should
5495 * stop and defer processing to the "normal" mechanism.
5496 * '0' and '^' are special, because they can be followed by CTRL-D.
5499 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5501 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5505 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5507 # define WHITECHAR(cc) vim_iswhite(cc)
5511 insertchar(c
, flags
, second_indent
)
5512 int c
; /* character to insert or NUL */
5513 int flags
; /* INSCHAR_FORMAT, etc. */
5514 int second_indent
; /* indent for second line if >= 0 */
5517 #ifdef FEAT_COMMENTS
5522 textwidth
= comp_textwidth(flags
& INSCHAR_FORMAT
);
5523 fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5526 * Try to break the line in two or more pieces when:
5527 * - Always do this if we have been called to do formatting only.
5528 * - Always do this when 'formatoptions' has the 'a' flag and the line
5529 * ends in white space.
5531 * - Don't do this if inserting a blank
5532 * - Don't do this if an existing character is being replaced, unless
5533 * we're in VREPLACE mode.
5534 * - Do this if the cursor is not on the line where insert started
5535 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5536 * before the insert.
5537 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5538 * before 'textwidth'
5541 && ((flags
& INSCHAR_FORMAT
)
5543 && !((State
& REPLACE_FLAG
)
5544 #ifdef FEAT_VREPLACE
5545 && !(State
& VREPLACE_FLAG
)
5547 && *ml_get_cursor() != NUL
)
5548 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
5549 || ((!has_format_option(FO_INS_LONG
)
5550 || Insstart_textlen
<= (colnr_T
)textwidth
)
5552 || Insstart_blank_vcol
<= (colnr_T
)textwidth
5555 /* Format with 'formatexpr' when it's set. Use internal formatting
5556 * when 'formatexpr' isn't set or it returns non-zero. */
5557 #if defined(FEAT_EVAL)
5558 int do_internal
= TRUE
;
5560 if (*curbuf
->b_p_fex
!= NUL
&& (flags
& INSCHAR_NO_FEX
) == 0)
5562 do_internal
= (fex_format(curwin
->w_cursor
.lnum
, 1L, c
) != 0);
5563 /* It may be required to save for undo again, e.g. when setline()
5565 ins_need_undo
= TRUE
;
5569 internal_format(textwidth
, second_indent
, flags
, c
== NUL
, c
);
5572 if (c
== NUL
) /* only formatting was wanted */
5575 #ifdef FEAT_COMMENTS
5576 /* Check whether this character should end a comment. */
5577 if (did_ai
&& (int)c
== end_comment_pending
)
5580 char_u lead_end
[COM_MAX_LEN
]; /* end-comment string */
5581 int middle_len
, end_len
;
5585 * Need to remove existing (middle) comment leader and insert end
5586 * comment leader. First, check what comment leader we can find.
5588 i
= get_leader_len(line
= ml_get_curline(), &p
, FALSE
);
5589 if (i
> 0 && vim_strchr(p
, COM_MIDDLE
) != NULL
) /* Just checking */
5591 /* Skip middle-comment string */
5592 while (*p
&& p
[-1] != ':') /* find end of middle flags */
5594 middle_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5595 /* Don't count trailing white space for middle_len */
5596 while (middle_len
> 0 && vim_iswhite(lead_end
[middle_len
- 1]))
5599 /* Find the end-comment string */
5600 while (*p
&& p
[-1] != ':') /* find end of end flags */
5602 end_len
= copy_option_part(&p
, lead_end
, COM_MAX_LEN
, ",");
5604 /* Skip white space before the cursor */
5605 i
= curwin
->w_cursor
.col
;
5606 while (--i
>= 0 && vim_iswhite(line
[i
]))
5610 /* Skip to before the middle leader */
5613 /* Check some expected things before we go on */
5614 if (i
>= 0 && lead_end
[end_len
- 1] == end_comment_pending
)
5616 /* Backspace over all the stuff we want to replace */
5617 backspace_until_column(i
);
5620 * Insert the end-comment string, except for the last
5621 * character, which will get inserted as normal later.
5623 ins_bytes_len(lead_end
, end_len
- 1);
5627 end_comment_pending
= NUL
;
5631 #ifdef FEAT_SMARTINDENT
5634 can_si_back
= FALSE
;
5638 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5639 * This speeds up normal text input considerably.
5640 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5641 * need to re-indent at a ':', or any other character (but not what
5644 #ifdef USE_ON_FLY_SCROLL
5645 dont_scroll
= FALSE
; /* allow scrolling here */
5650 && (!has_mbyte
|| (*mb_char2len
)(c
) == 1)
5653 && !(State
& REPLACE_FLAG
)
5657 #ifdef FEAT_RIGHTLEFT
5662 #define INPUT_BUFLEN 100
5663 char_u buf
[INPUT_BUFLEN
+ 1];
5665 colnr_T virtcol
= 0;
5670 virtcol
= get_nolist_virtcol();
5672 * Stop the string when:
5673 * - no more chars available
5674 * - finding a special character (command key)
5676 * - running into the 'textwidth' boundary
5677 * - need to check for abbreviation: A non-word char after a word-char
5679 while ( (c
= vpeekc()) != NUL
5682 && (!has_mbyte
|| MB_BYTE2LEN_CHECK(c
) == 1)
5686 || (virtcol
+= byte2cells(buf
[i
- 1])) < (colnr_T
)textwidth
)
5687 && !(!no_abbr
&& !vim_iswordc(c
) && vim_iswordc(buf
[i
- 1])))
5689 #ifdef FEAT_RIGHTLEFT
5691 if (p_hkmap
&& KeyTyped
)
5692 c
= hkmap(c
); /* Hebrew mode mapping */
5694 if (p_fkmap
&& KeyTyped
)
5695 c
= fkmap(c
); /* Farsi mode mapping */
5703 #ifdef FEAT_DIGRAPHS
5704 do_digraph(-1); /* clear digraphs */
5705 do_digraph(buf
[i
-1]); /* may be the start of a digraph */
5709 if (flags
& INSCHAR_CTRLV
)
5717 AppendToRedobuffLit(buf
+ i
, -1);
5724 if (has_mbyte
&& (cc
= (*mb_char2len
)(c
)) > 1)
5726 char_u buf
[MB_MAXBYTES
+ 1];
5728 (*mb_char2bytes
)(c
, buf
);
5730 ins_char_bytes(buf
, cc
);
5731 AppendCharToRedobuff(c
);
5737 if (flags
& INSCHAR_CTRLV
)
5740 AppendCharToRedobuff(c
);
5746 * Format text at the current insert position.
5749 internal_format(textwidth
, second_indent
, flags
, format_only
, c
)
5754 int c
; /* character to be inserted (can be NUL) */
5757 int save_char
= NUL
;
5758 int haveto_redraw
= FALSE
;
5759 int fo_ins_blank
= has_format_option(FO_INS_BLANK
);
5761 int fo_multibyte
= has_format_option(FO_MBYTE_BREAK
);
5763 int fo_white_par
= has_format_option(FO_WHITE_PAR
);
5764 int first_line
= TRUE
;
5765 #ifdef FEAT_COMMENTS
5767 int no_leader
= FALSE
;
5768 int do_comments
= (flags
& INSCHAR_DO_COM
);
5772 * When 'ai' is off we don't want a space under the cursor to be
5773 * deleted. Replace it with an 'x' temporarily.
5776 #ifdef FEAT_VREPLACE
5777 && !(State
& VREPLACE_FLAG
)
5781 cc
= gchar_cursor();
5782 if (vim_iswhite(cc
))
5790 * Repeat breaking lines, until the current line is not too long.
5794 int startcol
; /* Cursor column at entry */
5795 int wantcol
; /* column at textwidth border */
5796 int foundcol
; /* column for start of spaces */
5797 int end_foundcol
= 0; /* column for start of word */
5800 #ifdef FEAT_VREPLACE
5802 char_u
*saved_text
= NULL
;
5807 virtcol
= get_nolist_virtcol()
5808 + char2cells(c
!= NUL
? c
: gchar_cursor());
5809 if (virtcol
<= (colnr_T
)textwidth
)
5812 #ifdef FEAT_COMMENTS
5814 do_comments
= FALSE
;
5815 else if (!(flags
& INSCHAR_FORMAT
)
5816 && has_format_option(FO_WRAP_COMS
))
5819 /* Don't break until after the comment leader */
5821 leader_len
= get_leader_len(ml_get_curline(), NULL
, FALSE
);
5825 /* If the line doesn't start with a comment leader, then don't
5826 * start one in a following broken line. Avoids that a %word
5827 * moved to the start of the next line causes all following lines
5828 * to start with %. */
5829 if (leader_len
== 0)
5832 if (!(flags
& INSCHAR_FORMAT
)
5833 #ifdef FEAT_COMMENTS
5836 && !has_format_option(FO_WRAP
))
5839 if ((startcol
= curwin
->w_cursor
.col
) == 0)
5842 /* find column of textwidth border */
5843 coladvance((colnr_T
)textwidth
);
5844 wantcol
= curwin
->w_cursor
.col
;
5846 curwin
->w_cursor
.col
= startcol
;
5850 * Find position to break at.
5851 * Stop at first entered white when 'formatoptions' has 'v'
5853 while ((!fo_ins_blank
&& !has_format_option(FO_INS_VI
))
5854 || curwin
->w_cursor
.lnum
!= Insstart
.lnum
5855 || curwin
->w_cursor
.col
>= Insstart
.col
)
5857 if (curwin
->w_cursor
.col
== startcol
&& c
!= NUL
)
5860 cc
= gchar_cursor();
5863 /* remember position of blank just before text */
5864 end_col
= curwin
->w_cursor
.col
;
5866 /* find start of sequence of blanks */
5867 while (curwin
->w_cursor
.col
> 0 && WHITECHAR(cc
))
5870 cc
= gchar_cursor();
5872 if (curwin
->w_cursor
.col
== 0 && WHITECHAR(cc
))
5873 break; /* only spaces in front of text */
5874 #ifdef FEAT_COMMENTS
5875 /* Don't break until after the comment leader */
5876 if (curwin
->w_cursor
.col
< leader_len
)
5879 if (has_format_option(FO_ONE_LETTER
))
5881 /* do not break after one-letter words */
5882 if (curwin
->w_cursor
.col
== 0)
5883 break; /* one-letter word at begin */
5884 #ifdef FEAT_COMMENTS
5885 /* do not break "#a b" when 'tw' is 2 */
5886 if (curwin
->w_cursor
.col
<= leader_len
)
5889 col
= curwin
->w_cursor
.col
;
5891 cc
= gchar_cursor();
5894 continue; /* one-letter, continue */
5895 curwin
->w_cursor
.col
= col
;
5900 end_foundcol
= end_col
+ 1;
5901 foundcol
= curwin
->w_cursor
.col
;
5902 if (curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5906 else if (cc
>= 0x100 && fo_multibyte
)
5908 /* Break after or before a multi-byte character. */
5909 if (curwin
->w_cursor
.col
!= startcol
)
5911 #ifdef FEAT_COMMENTS
5912 /* Don't break until after the comment leader */
5913 if (curwin
->w_cursor
.col
< leader_len
)
5916 col
= curwin
->w_cursor
.col
;
5918 /* Don't change end_foundcol if already set. */
5919 if (foundcol
!= curwin
->w_cursor
.col
)
5921 foundcol
= curwin
->w_cursor
.col
;
5922 end_foundcol
= foundcol
;
5923 if (curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5926 curwin
->w_cursor
.col
= col
;
5929 if (curwin
->w_cursor
.col
== 0)
5932 col
= curwin
->w_cursor
.col
;
5935 cc
= gchar_cursor();
5938 continue; /* break with space */
5939 #ifdef FEAT_COMMENTS
5940 /* Don't break until after the comment leader */
5941 if (curwin
->w_cursor
.col
< leader_len
)
5945 curwin
->w_cursor
.col
= col
;
5947 foundcol
= curwin
->w_cursor
.col
;
5948 end_foundcol
= foundcol
;
5949 if (curwin
->w_cursor
.col
<= (colnr_T
)wantcol
)
5953 if (curwin
->w_cursor
.col
== 0)
5958 if (foundcol
== 0) /* no spaces, cannot break line */
5960 curwin
->w_cursor
.col
= startcol
;
5964 /* Going to break the line, remove any "$" now. */
5968 * Offset between cursor position and line break is used by replace
5969 * stack functions. VREPLACE does not use this, and backspaces
5970 * over the text instead.
5972 #ifdef FEAT_VREPLACE
5973 if (State
& VREPLACE_FLAG
)
5974 orig_col
= startcol
; /* Will start backspacing from here */
5977 replace_offset
= startcol
- end_foundcol
;
5980 * adjust startcol for spaces that will be deleted and
5981 * characters that will remain on top line
5983 curwin
->w_cursor
.col
= foundcol
;
5984 while ((cc
= gchar_cursor(), WHITECHAR(cc
))
5985 && (!fo_white_par
|| curwin
->w_cursor
.col
< startcol
))
5987 startcol
-= curwin
->w_cursor
.col
;
5991 #ifdef FEAT_VREPLACE
5992 if (State
& VREPLACE_FLAG
)
5995 * In VREPLACE mode, we will backspace over the text to be
5996 * wrapped, so save a copy now to put on the next line.
5998 saved_text
= vim_strsave(ml_get_cursor());
5999 curwin
->w_cursor
.col
= orig_col
;
6000 if (saved_text
== NULL
)
6001 break; /* Can't do it, out of memory */
6002 saved_text
[startcol
] = NUL
;
6004 /* Backspace over characters that will move to the next line */
6006 backspace_until_column(foundcol
);
6011 /* put cursor after pos. to break line */
6013 curwin
->w_cursor
.col
= foundcol
;
6017 * Split the line just before the margin.
6018 * Only insert/delete lines, but don't really redraw the window.
6020 open_line(FORWARD
, OPENLINE_DELSPACES
+ OPENLINE_MARKFIX
6021 + (fo_white_par
? OPENLINE_KEEPTRAIL
: 0)
6022 #ifdef FEAT_COMMENTS
6023 + (do_comments
? OPENLINE_DO_COM
: 0)
6031 if (second_indent
< 0 && has_format_option(FO_Q_NUMBER
))
6032 second_indent
= get_number_indent(curwin
->w_cursor
.lnum
-1);
6033 if (second_indent
>= 0)
6035 #ifdef FEAT_VREPLACE
6036 if (State
& VREPLACE_FLAG
)
6037 change_indent(INDENT_SET
, second_indent
, FALSE
, NUL
, TRUE
);
6040 (void)set_indent(second_indent
, SIN_CHANGED
);
6045 #ifdef FEAT_VREPLACE
6046 if (State
& VREPLACE_FLAG
)
6049 * In VREPLACE mode we have backspaced over the text to be
6050 * moved, now we re-insert it into the new line.
6052 ins_bytes(saved_text
);
6053 vim_free(saved_text
);
6059 * Check if cursor is not past the NUL off the line, cindent
6060 * may have added or removed indent.
6062 curwin
->w_cursor
.col
+= startcol
;
6063 len
= (colnr_T
)STRLEN(ml_get_curline());
6064 if (curwin
->w_cursor
.col
> len
)
6065 curwin
->w_cursor
.col
= len
;
6068 haveto_redraw
= TRUE
;
6072 /* moved the cursor, don't autoindent or cindent now */
6074 #ifdef FEAT_SMARTINDENT
6077 can_si_back
= FALSE
;
6082 if (save_char
!= NUL
) /* put back space after cursor */
6083 pchar_cursor(save_char
);
6085 if (!format_only
&& haveto_redraw
)
6088 redraw_curbuf_later(VALID
);
6093 * Called after inserting or deleting text: When 'formatoptions' includes the
6094 * 'a' flag format from the current line until the end of the paragraph.
6095 * Keep the cursor at the same position relative to the text.
6096 * The caller must have saved the cursor line for undo, following ones will be
6100 auto_format(trailblank
, prev_line
)
6101 int trailblank
; /* when TRUE also format with trailing blank */
6102 int prev_line
; /* may start in previous line */
6111 if (!has_format_option(FO_AUTO
))
6114 pos
= curwin
->w_cursor
;
6115 old
= ml_get_curline();
6117 /* may remove added space */
6118 check_auto_format(FALSE
);
6120 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6121 * user might insert normal text next. Also skip formatting when "1" is
6122 * in 'formatoptions' and there is a single character before the cursor.
6123 * Otherwise the line would be broken and when typing another non-white
6124 * next they are not joined back together. */
6125 wasatend
= (pos
.col
== (colnr_T
)STRLEN(old
));
6126 if (*old
!= NUL
&& !trailblank
&& wasatend
)
6129 cc
= gchar_cursor();
6130 if (!WHITECHAR(cc
) && curwin
->w_cursor
.col
> 0
6131 && has_format_option(FO_ONE_LETTER
))
6133 cc
= gchar_cursor();
6136 curwin
->w_cursor
= pos
;
6139 curwin
->w_cursor
= pos
;
6142 #ifdef FEAT_COMMENTS
6143 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6145 if (has_format_option(FO_WRAP_COMS
) && !has_format_option(FO_WRAP
)
6146 && get_leader_len(old
, NULL
, FALSE
) == 0)
6151 * May start formatting in a previous line, so that after "x" a word is
6152 * moved to the previous line if it fits there now. Only when this is not
6153 * the start of a paragraph.
6155 if (prev_line
&& !paragraph_start(curwin
->w_cursor
.lnum
))
6157 --curwin
->w_cursor
.lnum
;
6158 if (u_save_cursor() == FAIL
)
6163 * Do the formatting and restore the cursor position. "saved_cursor" will
6164 * be adjusted for the text formatting.
6167 format_lines((linenr_T
)-1, FALSE
);
6168 curwin
->w_cursor
= saved_cursor
;
6169 saved_cursor
.lnum
= 0;
6171 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6173 /* "cannot happen" */
6174 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6175 coladvance((colnr_T
)MAXCOL
);
6180 /* Insert mode: If the cursor is now after the end of the line while it
6181 * previously wasn't, the line was broken. Because of the rule above we
6182 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6184 if (!wasatend
&& has_format_option(FO_WHITE_PAR
))
6186 new = ml_get_curline();
6187 len
= (colnr_T
)STRLEN(new);
6188 if (curwin
->w_cursor
.col
== len
)
6190 pnew
= vim_strnsave(new, len
+ 2);
6192 pnew
[len
+ 1] = NUL
;
6193 ml_replace(curwin
->w_cursor
.lnum
, pnew
, FALSE
);
6194 /* remove the space later */
6195 did_add_space
= TRUE
;
6198 /* may remove added space */
6199 check_auto_format(FALSE
);
6206 * When an extra space was added to continue a paragraph for auto-formatting,
6207 * delete it now. The space must be under the cursor, just after the insert
6211 check_auto_format(end_insert
)
6212 int end_insert
; /* TRUE when ending Insert mode */
6219 cc
= gchar_cursor();
6221 /* Somehow the space was removed already. */
6222 did_add_space
= FALSE
;
6233 /* The space is no longer at the end of the line, delete it. */
6235 did_add_space
= FALSE
;
6242 * Find out textwidth to be used for formatting:
6243 * if 'textwidth' option is set, use it
6244 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6245 * if invalid value, use 0.
6246 * Set default to window width (maximum 79) for "gq" operator.
6250 int ff
; /* force formatting (for "gq" command) */
6254 textwidth
= curbuf
->b_p_tw
;
6255 if (textwidth
== 0 && curbuf
->b_p_wm
)
6257 /* The width is the window width minus 'wrapmargin' minus all the
6258 * things that add to the margin. */
6259 textwidth
= W_WIDTH(curwin
) - curbuf
->b_p_wm
;
6261 if (cmdwin_type
!= 0)
6265 textwidth
-= curwin
->w_p_fdc
;
6268 if (curwin
->w_buffer
->b_signlist
!= NULL
6269 # ifdef FEAT_NETBEANS_INTG
6275 if (curwin
->w_p_nu
|| curwin
->w_p_rnu
)
6280 if (ff
&& textwidth
== 0)
6282 textwidth
= W_WIDTH(curwin
) - 1;
6290 * Put a character in the redo buffer, for when just after a CTRL-V.
6298 /* Only digits need special treatment. Translate them into a string of
6302 vim_snprintf((char *)buf
, sizeof(buf
), "%03d", c
);
6303 AppendToRedobuff(buf
);
6306 AppendCharToRedobuff(c
);
6310 * start_arrow() is called when an arrow key is used in insert mode.
6311 * For undo/redo it resembles hitting the <ESC> key.
6314 start_arrow(end_insert_pos
)
6315 pos_T
*end_insert_pos
; /* can be NULL */
6317 if (!arrow_used
) /* something has been inserted */
6319 AppendToRedobuff(ESC_STR
);
6320 stop_insert(end_insert_pos
, FALSE
);
6321 arrow_used
= TRUE
; /* this means we stopped the current insert */
6324 check_spell_redraw();
6330 * If we skipped highlighting word at cursor, do it now.
6331 * It may be skipped again, thus reset spell_redraw_lnum first.
6334 check_spell_redraw()
6336 if (spell_redraw_lnum
!= 0)
6338 linenr_T lnum
= spell_redraw_lnum
;
6340 spell_redraw_lnum
= 0;
6341 redrawWinline(lnum
, FALSE
);
6346 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6347 * spelled word, if there is one.
6350 spell_back_to_badword()
6352 pos_T tpos
= curwin
->w_cursor
;
6354 spell_bad_len
= spell_move_to(curwin
, BACKWARD
, TRUE
, TRUE
, NULL
);
6355 if (curwin
->w_cursor
.col
!= tpos
.col
)
6361 * stop_arrow() is called before a change is made in insert mode.
6362 * If an arrow key has been used, start a new insertion.
6363 * Returns FAIL if undo is impossible, shouldn't insert then.
6370 if (u_save_cursor() == OK
)
6373 ins_need_undo
= FALSE
;
6375 Insstart
= curwin
->w_cursor
; /* new insertion starts here */
6376 Insstart_textlen
= (colnr_T
)linetabsize(ml_get_curline());
6378 #ifdef FEAT_VREPLACE
6379 if (State
& VREPLACE_FLAG
)
6381 orig_line_count
= curbuf
->b_ml
.ml_line_count
;
6382 vr_lines_changed
= 1;
6386 AppendToRedobuff((char_u
*)"1i"); /* pretend we start an insertion */
6387 new_insert_skip
= 2;
6389 else if (ins_need_undo
)
6391 if (u_save_cursor() == OK
)
6392 ins_need_undo
= FALSE
;
6396 /* Always open fold at the cursor line when inserting something. */
6400 return (arrow_used
|| ins_need_undo
? FAIL
: OK
);
6404 * Do a few things to stop inserting.
6405 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6406 * to another window/buffer.
6409 stop_insert(end_insert_pos
, esc
)
6410 pos_T
*end_insert_pos
;
6411 int esc
; /* called by ins_esc() */
6417 replace_flush(); /* abandon replace stack */
6420 * Save the inserted text for later redo with ^@ and CTRL-A.
6421 * Don't do it when "restart_edit" was set and nothing was inserted,
6422 * otherwise CTRL-O w and then <Left> will clear "last_insert".
6424 ptr
= get_inserted();
6425 if (did_restart_edit
== 0 || (ptr
!= NULL
6426 && (int)STRLEN(ptr
) > new_insert_skip
))
6428 vim_free(last_insert
);
6430 last_insert_skip
= new_insert_skip
;
6435 if (!arrow_used
&& end_insert_pos
!= NULL
)
6437 /* Auto-format now. It may seem strange to do this when stopping an
6438 * insertion (or moving the cursor), but it's required when appending
6439 * a line and having it end in a space. But only do it when something
6440 * was actually inserted, otherwise undo won't work. */
6441 if (!ins_need_undo
&& has_format_option(FO_AUTO
))
6443 pos_T tpos
= curwin
->w_cursor
;
6445 /* When the cursor is at the end of the line after a space the
6446 * formatting will move it to the following word. Avoid that by
6447 * moving the cursor onto the space. */
6449 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
)
6452 cc
= gchar_cursor();
6453 if (!vim_iswhite(cc
))
6454 curwin
->w_cursor
= tpos
;
6457 auto_format(TRUE
, FALSE
);
6459 if (vim_iswhite(cc
))
6461 if (gchar_cursor() != NUL
)
6463 #ifdef FEAT_VIRTUALEDIT
6464 /* If the cursor is still at the same character, also keep
6466 if (gchar_cursor() == NUL
6467 && curwin
->w_cursor
.lnum
== tpos
.lnum
6468 && curwin
->w_cursor
.col
== tpos
.col
)
6469 curwin
->w_cursor
.coladd
= tpos
.coladd
;
6474 /* If a space was inserted for auto-formatting, remove it now. */
6475 check_auto_format(TRUE
);
6477 /* If we just did an auto-indent, remove the white space from the end
6478 * of the line, and put the cursor back.
6479 * Do this when ESC was used or moving the cursor up/down.
6480 * Check for the old position still being valid, just in case the text
6481 * got changed unexpectedly. */
6482 if (did_ai
&& (esc
|| (vim_strchr(p_cpo
, CPO_INDENT
) == NULL
6483 && curwin
->w_cursor
.lnum
!= end_insert_pos
->lnum
))
6484 && end_insert_pos
->lnum
<= curbuf
->b_ml
.ml_line_count
)
6486 pos_T tpos
= curwin
->w_cursor
;
6488 curwin
->w_cursor
= *end_insert_pos
;
6489 check_cursor_col(); /* make sure it is not past the line */
6492 if (gchar_cursor() == NUL
&& curwin
->w_cursor
.col
> 0)
6493 --curwin
->w_cursor
.col
;
6494 cc
= gchar_cursor();
6495 if (!vim_iswhite(cc
))
6497 if (del_char(TRUE
) == FAIL
)
6498 break; /* should not happen */
6500 if (curwin
->w_cursor
.lnum
!= tpos
.lnum
)
6501 curwin
->w_cursor
= tpos
;
6503 ++curwin
->w_cursor
.col
; /* put cursor back on the NUL */
6506 /* <C-S-Right> may have started Visual mode, adjust the position for
6507 * deleted characters. */
6508 if (VIsual_active
&& VIsual
.lnum
== curwin
->w_cursor
.lnum
)
6510 int len
= (int)STRLEN(ml_get_curline());
6512 if (VIsual
.col
> len
)
6515 # ifdef FEAT_VIRTUALEDIT
6524 #ifdef FEAT_SMARTINDENT
6527 can_si_back
= FALSE
;
6530 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6531 * now in a different buffer. */
6532 if (end_insert_pos
!= NULL
)
6534 curbuf
->b_op_start
= Insstart
;
6535 curbuf
->b_op_end
= *end_insert_pos
;
6540 * Set the last inserted text to a single character.
6541 * Used for the replace command.
6549 vim_free(last_insert
);
6551 last_insert
= alloc(MB_MAXBYTES
* 3 + 5);
6553 last_insert
= alloc(6);
6555 if (last_insert
!= NULL
)
6558 /* Use the CTRL-V only when entering a special char */
6559 if (c
< ' ' || c
== DEL
)
6561 s
= add_char2buf(c
, s
);
6564 last_insert_skip
= 0;
6568 #if defined(EXITFREE) || defined(PROTO)
6572 vim_free(last_insert
);
6574 # ifdef FEAT_INS_EXPAND
6575 vim_free(compl_orig_text
);
6576 compl_orig_text
= NULL
;
6582 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6583 * and CSI. Handle multi-byte characters.
6584 * Returns a pointer to after the added bytes.
6592 char_u temp
[MB_MAXBYTES
];
6596 len
= (*mb_char2bytes
)(c
, temp
);
6597 for (i
= 0; i
< len
; ++i
)
6601 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6625 * move cursor to start of line
6626 * if flags & BL_WHITE move to first non-white
6627 * if flags & BL_SOL move to first non-white if startofline is set,
6628 * otherwise keep "curswant" column
6629 * if flags & BL_FIX don't leave the cursor on a NUL.
6635 if ((flags
& BL_SOL
) && !p_sol
)
6636 coladvance(curwin
->w_curswant
);
6639 curwin
->w_cursor
.col
= 0;
6640 #ifdef FEAT_VIRTUALEDIT
6641 curwin
->w_cursor
.coladd
= 0;
6644 if (flags
& (BL_WHITE
| BL_SOL
))
6648 for (ptr
= ml_get_curline(); vim_iswhite(*ptr
)
6649 && !((flags
& BL_FIX
) && ptr
[1] == NUL
); ++ptr
)
6650 ++curwin
->w_cursor
.col
;
6652 curwin
->w_set_curswant
= TRUE
;
6657 * oneright oneleft cursor_down cursor_up
6659 * Move one char {right,left,down,up}.
6660 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
6661 * Return OK when successful, FAIL when we hit a line of file boundary.
6670 #ifdef FEAT_VIRTUALEDIT
6671 if (virtual_active())
6673 pos_T prevpos
= curwin
->w_cursor
;
6675 /* Adjust for multi-wide char (excluding TAB) */
6676 ptr
= ml_get_cursor();
6677 coladvance(getviscol() + ((*ptr
!= TAB
&& vim_isprintc(
6684 ? ptr2cells(ptr
) : 1));
6685 curwin
->w_set_curswant
= TRUE
;
6686 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6687 return (prevpos
.col
!= curwin
->w_cursor
.col
6688 || prevpos
.coladd
!= curwin
->w_cursor
.coladd
) ? OK
: FAIL
;
6692 ptr
= ml_get_cursor();
6694 return FAIL
; /* already at the very end */
6698 l
= (*mb_ptr2len
)(ptr
);
6703 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6704 * contains "onemore". */
6706 #ifdef FEAT_VIRTUALEDIT
6707 && (ve_flags
& VE_ONEMORE
) == 0
6711 curwin
->w_cursor
.col
+= l
;
6713 curwin
->w_set_curswant
= TRUE
;
6720 #ifdef FEAT_VIRTUALEDIT
6721 if (virtual_active())
6724 int v
= getviscol();
6729 # ifdef FEAT_LINEBREAK
6730 /* We might get stuck on 'showbreak', skip over it. */
6734 coladvance(v
- width
);
6735 /* getviscol() is slow, skip it when 'showbreak' is empty and
6736 * there are no multi-byte characters */
6741 ) || getviscol() < v
)
6749 if (curwin
->w_cursor
.coladd
== 1)
6753 /* Adjust for multi-wide char (not a TAB) */
6754 ptr
= ml_get_cursor();
6755 if (*ptr
!= TAB
&& vim_isprintc(
6761 ) && ptr2cells(ptr
) > 1)
6762 curwin
->w_cursor
.coladd
= 0;
6765 curwin
->w_set_curswant
= TRUE
;
6770 if (curwin
->w_cursor
.col
== 0)
6773 curwin
->w_set_curswant
= TRUE
;
6774 --curwin
->w_cursor
.col
;
6777 /* if the character on the left of the current cursor is a multi-byte
6778 * character, move to its first byte */
6786 cursor_up(n
, upd_topline
)
6788 int upd_topline
; /* When TRUE: update topline */
6794 lnum
= curwin
->w_cursor
.lnum
;
6795 /* This fails if the cursor is already in the first line or the count
6796 * is larger than the line number and '-' is in 'cpoptions' */
6797 if (lnum
<= 1 || (n
>= lnum
&& vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6803 if (hasAnyFolding(curwin
))
6806 * Count each sequence of folded lines as one logical line.
6808 /* go to the the start of the current fold */
6809 (void)hasFolding(lnum
, &lnum
, NULL
);
6813 /* move up one line */
6817 /* If we entered a fold, move to the beginning, unless in
6818 * Insert mode or when 'foldopen' contains "all": it will open
6820 if (n
> 0 || !((State
& INSERT
) || (fdo_flags
& FDO_ALL
)))
6821 (void)hasFolding(lnum
, &lnum
, NULL
);
6829 curwin
->w_cursor
.lnum
= lnum
;
6832 /* try to advance to the column we want to be at */
6833 coladvance(curwin
->w_curswant
);
6836 update_topline(); /* make sure curwin->w_topline is valid */
6842 * Cursor down a number of logical lines.
6845 cursor_down(n
, upd_topline
)
6847 int upd_topline
; /* When TRUE: update topline */
6853 lnum
= curwin
->w_cursor
.lnum
;
6855 /* Move to last line of fold, will fail if it's the end-of-file. */
6856 (void)hasFolding(lnum
, NULL
, &lnum
);
6858 /* This fails if the cursor is already in the last line or would move
6859 * beyound the last line and '-' is in 'cpoptions' */
6860 if (lnum
>= curbuf
->b_ml
.ml_line_count
6861 || (lnum
+ n
> curbuf
->b_ml
.ml_line_count
6862 && vim_strchr(p_cpo
, CPO_MINUS
) != NULL
))
6864 if (lnum
+ n
>= curbuf
->b_ml
.ml_line_count
)
6865 lnum
= curbuf
->b_ml
.ml_line_count
;
6868 if (hasAnyFolding(curwin
))
6872 /* count each sequence of folded lines as one logical line */
6875 if (hasFolding(lnum
, NULL
, &last
))
6879 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
6882 if (lnum
> curbuf
->b_ml
.ml_line_count
)
6883 lnum
= curbuf
->b_ml
.ml_line_count
;
6888 curwin
->w_cursor
.lnum
= lnum
;
6891 /* try to advance to the column we want to be at */
6892 coladvance(curwin
->w_curswant
);
6895 update_topline(); /* make sure curwin->w_topline is valid */
6901 * Stuff the last inserted text in the read buffer.
6902 * Last_insert actually is a copy of the redo buffer, so we
6903 * first have to remove the command.
6906 stuff_inserted(c
, count
, no_esc
)
6907 int c
; /* Command character to be inserted */
6908 long count
; /* Repeat this many times */
6909 int no_esc
; /* Don't add an ESC at the end */
6916 ptr
= get_last_insert();
6919 EMSG(_(e_noinstext
));
6923 /* may want to stuff the command character, to start Insert mode */
6925 stuffcharReadbuff(c
);
6926 if ((esc_ptr
= (char_u
*)vim_strrchr(ptr
, ESC
)) != NULL
)
6927 *esc_ptr
= NUL
; /* remove the ESC */
6929 /* when the last char is either "0" or "^" it will be quoted if no ESC
6930 * comes after it OR if it will inserted more than once and "ptr"
6931 * starts with ^D. -- Acevedo
6933 last_ptr
= (esc_ptr
? esc_ptr
: ptr
+ STRLEN(ptr
)) - 1;
6934 if (last_ptr
>= ptr
&& (*last_ptr
== '0' || *last_ptr
== '^')
6935 && (no_esc
|| (*ptr
== Ctrl_D
&& count
> 1)))
6944 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6946 stuffReadbuff((char_u
*)(last
== '0'
6947 ? IF_EB("\026\060\064\070", CTRL_V_STR
"xf0")
6948 : IF_EB("\026^", CTRL_V_STR
"^")));
6950 while (--count
> 0);
6955 if (esc_ptr
!= NULL
)
6956 *esc_ptr
= ESC
; /* put the ESC back */
6958 /* may want to stuff a trailing ESC, to get out of Insert mode */
6960 stuffcharReadbuff(ESC
);
6968 if (last_insert
== NULL
)
6970 return last_insert
+ last_insert_skip
;
6974 * Get last inserted string, and remove trailing <Esc>.
6975 * Returns pointer to allocated memory (must be freed) or NULL.
6978 get_last_insert_save()
6983 if (last_insert
== NULL
)
6985 s
= vim_strsave(last_insert
+ last_insert_skip
);
6988 len
= (int)STRLEN(s
);
6989 if (len
> 0 && s
[len
- 1] == ESC
) /* remove trailing ESC */
6996 * Check the word in front of the cursor for an abbreviation.
6997 * Called when the non-id character "c" has been entered.
6998 * When an abbreviation is recognized it is removed from the text and
6999 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7005 /* Don't check for abbreviation in paste mode, when disabled and just
7006 * after moving around with cursor keys. */
7007 if (p_paste
|| no_abbr
|| arrow_used
)
7010 return check_abbr(c
, ml_get_curline(), curwin
->w_cursor
.col
,
7011 curwin
->w_cursor
.lnum
== Insstart
.lnum
? Insstart
.col
: 0);
7015 * replace-stack functions
7017 * When replacing characters, the replaced characters are remembered for each
7018 * new character. This is used to re-insert the old text when backspacing.
7020 * There is a NUL headed list of characters for each character that is
7021 * currently in the file after the insertion point. When BS is used, one NUL
7022 * headed list is put back for the deleted character.
7024 * For a newline, there are two NUL headed lists. One contains the characters
7025 * that the NL replaced. The extra one stores the characters after the cursor
7026 * that were deleted (always white space).
7028 * Replace_offset is normally 0, in which case replace_push will add a new
7029 * character at the end of the stack. If replace_offset is not 0, that many
7030 * characters will be left on the stack above the newly inserted character.
7033 static char_u
*replace_stack
= NULL
;
7034 static long replace_stack_nr
= 0; /* next entry in replace stack */
7035 static long replace_stack_len
= 0; /* max. number of entries */
7039 int c
; /* character that is replaced (NUL is none) */
7043 if (replace_stack_nr
< replace_offset
) /* nothing to do */
7045 if (replace_stack_len
<= replace_stack_nr
)
7047 replace_stack_len
+= 50;
7048 p
= lalloc(sizeof(char_u
) * replace_stack_len
, TRUE
);
7049 if (p
== NULL
) /* out of memory */
7051 replace_stack_len
-= 50;
7054 if (replace_stack
!= NULL
)
7056 mch_memmove(p
, replace_stack
,
7057 (size_t)(replace_stack_nr
* sizeof(char_u
)));
7058 vim_free(replace_stack
);
7062 p
= replace_stack
+ replace_stack_nr
- replace_offset
;
7064 mch_memmove(p
+ 1, p
, (size_t)(replace_offset
* sizeof(char_u
)));
7069 #if defined(FEAT_MBYTE) || defined(PROTO)
7071 * Push a character onto the replace stack. Handles a multi-byte character in
7072 * reverse byte order, so that the first byte is popped off first.
7073 * Return the number of bytes done (includes composing characters).
7079 int l
= (*mb_ptr2len
)(p
);
7082 for (j
= l
- 1; j
>= 0; --j
)
7090 * call replace_push(c) with replace_offset set to the first NUL.
7098 p
= replace_stack
+ replace_stack_nr
;
7099 for (replace_offset
= 1; replace_offset
< replace_stack_nr
;
7109 * Pop one item from the replace stack.
7110 * return -1 if stack empty
7111 * return replaced character or NUL otherwise
7116 if (replace_stack_nr
== 0)
7118 return (int)replace_stack
[--replace_stack_nr
];
7122 * Join the top two items on the replace stack. This removes to "off"'th NUL
7127 int off
; /* offset for which NUL to remove */
7131 for (i
= replace_stack_nr
; --i
>= 0; )
7132 if (replace_stack
[i
] == NUL
&& off
-- <= 0)
7135 mch_memmove(replace_stack
+ i
, replace_stack
+ i
+ 1,
7136 (size_t)(replace_stack_nr
- i
));
7142 * Pop bytes from the replace stack until a NUL is found, and insert them
7143 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7149 int oldState
= State
;
7151 State
= NORMAL
; /* don't want REPLACE here */
7152 while ((cc
= replace_pop()) > 0)
7155 mb_replace_pop_ins(cc
);
7166 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7167 * indicates a multi-byte char, pop the other bytes too.
7170 mb_replace_pop_ins(cc
)
7174 char_u buf
[MB_MAXBYTES
];
7178 if (has_mbyte
&& (n
= MB_BYTE2LEN(cc
)) > 1)
7181 for (i
= 1; i
< n
; ++i
)
7182 buf
[i
] = replace_pop();
7183 ins_bytes_len(buf
, n
);
7189 /* Handle composing chars. */
7193 if (c
== -1) /* stack empty */
7195 if ((n
= MB_BYTE2LEN(c
)) == 1)
7197 /* Not a multi-byte char, put it back. */
7204 for (i
= 1; i
< n
; ++i
)
7205 buf
[i
] = replace_pop();
7206 if (utf_iscomposing(utf_ptr2char(buf
)))
7207 ins_bytes_len(buf
, n
);
7210 /* Not a composing char, put it back. */
7211 for (i
= n
- 1; i
>= 0; --i
)
7212 replace_push(buf
[i
]);
7221 * make the replace stack empty
7222 * (called when exiting replace mode)
7227 vim_free(replace_stack
);
7228 replace_stack
= NULL
;
7229 replace_stack_len
= 0;
7230 replace_stack_nr
= 0;
7234 * Handle doing a BS for one character.
7235 * cc < 0: replace stack empty, just move cursor
7236 * cc == 0: character was inserted, delete it
7237 * cc > 0: character was replaced, put cc (first byte of original char) back
7238 * and check for more characters to be put back
7239 * When "limit_col" is >= 0, don't delete before this column. Matters when
7240 * using composing characters, use del_char_after_col() instead of del_char().
7243 replace_do_bs(limit_col
)
7247 #ifdef FEAT_VREPLACE
7260 #ifdef FEAT_VREPLACE
7261 if (State
& VREPLACE_FLAG
)
7263 /* Get the number of screen cells used by the character we are
7264 * going to delete. */
7265 getvcol(curwin
, &curwin
->w_cursor
, NULL
, &start_vcol
, NULL
);
7266 orig_vcols
= chartabsize(ml_get_cursor(), start_vcol
);
7272 (void)del_char_after_col(limit_col
);
7273 # ifdef FEAT_VREPLACE
7274 if (State
& VREPLACE_FLAG
)
7275 orig_len
= (int)STRLEN(ml_get_cursor());
7283 #ifdef FEAT_VREPLACE
7284 if (State
& VREPLACE_FLAG
)
7285 orig_len
= (int)STRLEN(ml_get_cursor()) - 1;
7290 #ifdef FEAT_VREPLACE
7291 if (State
& VREPLACE_FLAG
)
7293 /* Get the number of screen cells used by the inserted characters */
7294 p
= ml_get_cursor();
7295 ins_len
= (int)STRLEN(p
) - orig_len
;
7297 for (i
= 0; i
< ins_len
; ++i
)
7299 vcol
+= chartabsize(p
+ i
, vcol
);
7301 i
+= (*mb_ptr2len
)(p
) - 1;
7306 /* Delete spaces that were inserted after the cursor to keep the
7308 curwin
->w_cursor
.col
+= ins_len
;
7309 while (vcol
> orig_vcols
&& gchar_cursor() == ' ')
7314 curwin
->w_cursor
.col
-= ins_len
;
7318 /* mark the buffer as changed and prepare for displaying */
7319 changed_bytes(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
7322 (void)del_char_after_col(limit_col
);
7327 * Return TRUE if C-indenting is on.
7332 return (!p_paste
&& (curbuf
->b_p_cin
7334 || *curbuf
->b_p_inde
!= NUL
7340 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7342 * Re-indent the current line, based on the current contents of it and the
7343 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7344 * confused what all the part that handles Control-T is doing that I'm not.
7345 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7349 fixthisline(get_the_indent
)
7350 int (*get_the_indent
) __ARGS((void));
7352 change_indent(INDENT_SET
, get_the_indent(), FALSE
, 0, TRUE
);
7353 if (linewhite(curwin
->w_cursor
.lnum
))
7354 did_ai
= TRUE
; /* delete the indent if the line stays empty */
7363 if (curbuf
->b_p_lisp
&& curbuf
->b_p_ai
)
7364 fixthisline(get_lisp_indent
);
7366 # if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7369 # ifdef FEAT_CINDENT
7379 * return TRUE if 'cinkeys' contains the key "keytyped",
7380 * when == '*': Only if key is preceded with '*' (indent before insert)
7381 * when == '!': Only if key is prededed with '!' (don't insert)
7382 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7384 * "keytyped" can have a few special values:
7387 * KEY_COMPLETE just finished completion.
7389 * If line_is_empty is TRUE accept keys with '0' before them.
7392 in_cinkeys(keytyped
, when
, line_is_empty
)
7405 if (keytyped
== NUL
)
7406 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7410 if (*curbuf
->b_p_inde
!= NUL
)
7411 look
= curbuf
->b_p_indk
; /* 'indentexpr' set: use 'indentkeys' */
7414 look
= curbuf
->b_p_cink
; /* 'indentexpr' empty: use 'cinkeys' */
7418 * Find out if we want to try a match with this key, depending on
7419 * 'when' and a '*' or '!' before the key.
7423 case '*': try_match
= (*look
== '*'); break;
7424 case '!': try_match
= (*look
== '!'); break;
7425 default: try_match
= (*look
!= '*'); break;
7427 if (*look
== '*' || *look
== '!')
7431 * If there is a '0', only accept a match if the line is empty.
7432 * But may still match when typing last char of a word.
7436 try_match_word
= try_match
;
7442 try_match_word
= FALSE
;
7445 * does it look like a control character?
7449 && (Ctrl_chr(look
[1]) != 0)
7451 && look
[1] >= '?' && look
[1] <= '_'
7455 if (try_match
&& keytyped
== Ctrl_chr(look
[1]))
7460 * 'o' means "o" command, open forward.
7461 * 'O' means "O" command, open backward.
7463 else if (*look
== 'o')
7465 if (try_match
&& keytyped
== KEY_OPEN_FORW
)
7469 else if (*look
== 'O')
7471 if (try_match
&& keytyped
== KEY_OPEN_BACK
)
7477 * 'e' means to check for "else" at start of line and just before the
7480 else if (*look
== 'e')
7482 if (try_match
&& keytyped
== 'e' && curwin
->w_cursor
.col
>= 4)
7484 p
= ml_get_curline();
7485 if (skipwhite(p
) == p
+ curwin
->w_cursor
.col
- 4 &&
7486 STRNCMP(p
+ curwin
->w_cursor
.col
- 4, "else", 4) == 0)
7493 * ':' only causes an indent if it is at the end of a label or case
7494 * statement, or when it was before typing the ':' (to fix
7495 * class::method for C++).
7497 else if (*look
== ':')
7499 if (try_match
&& keytyped
== ':')
7501 p
= ml_get_curline();
7502 if (cin_iscase(p
) || cin_isscopedecl(p
) || cin_islabel(30))
7504 /* Need to get the line again after cin_islabel(). */
7505 p
= ml_get_curline();
7506 if (curwin
->w_cursor
.col
> 2
7507 && p
[curwin
->w_cursor
.col
- 1] == ':'
7508 && p
[curwin
->w_cursor
.col
- 2] == ':')
7510 p
[curwin
->w_cursor
.col
- 1] = ' ';
7511 i
= (cin_iscase(p
) || cin_isscopedecl(p
)
7512 || cin_islabel(30));
7513 p
= ml_get_curline();
7514 p
[curwin
->w_cursor
.col
- 1] = ':';
7524 * Is it a key in <>, maybe?
7526 else if (*look
== '<')
7531 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7532 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7533 * >, *, : and ! keys if they really really want to.
7535 if (vim_strchr((char_u
*)"<>!*oOe0:", look
[1]) != NULL
7536 && keytyped
== look
[1])
7539 if (keytyped
== get_special_key_code(look
+ 1))
7542 while (*look
&& *look
!= '>')
7544 while (*look
== '>')
7549 * Is it a word: "=word"?
7551 else if (*look
== '=' && look
[1] != ',' && look
[1] != NUL
)
7561 p
= vim_strchr(look
, ',');
7563 p
= look
+ STRLEN(look
);
7564 if ((try_match
|| try_match_word
)
7565 && curwin
->w_cursor
.col
>= (colnr_T
)(p
- look
))
7569 #ifdef FEAT_INS_EXPAND
7570 if (keytyped
== KEY_COMPLETE
)
7574 /* Just completed a word, check if it starts with "look".
7575 * search back for the start of a word. */
7576 line
= ml_get_curline();
7582 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; s
= n
)
7584 n
= mb_prevptr(line
, s
);
7585 if (!vim_iswordp(n
))
7591 for (s
= line
+ curwin
->w_cursor
.col
; s
> line
; --s
)
7592 if (!vim_iswordc(s
[-1]))
7594 if (s
+ (p
- look
) <= line
+ curwin
->w_cursor
.col
7596 ? MB_STRNICMP(s
, look
, p
- look
)
7597 : STRNCMP(s
, look
, p
- look
)) == 0)
7602 /* TODO: multi-byte */
7603 if (keytyped
== (int)p
[-1] || (icase
&& keytyped
< 256
7604 && TOLOWER_LOC(keytyped
) == TOLOWER_LOC((int)p
[-1])))
7606 line
= ml_get_cursor();
7607 if ((curwin
->w_cursor
.col
== (colnr_T
)(p
- look
)
7608 || !vim_iswordc(line
[-(p
- look
) - 1]))
7610 ? MB_STRNICMP(line
- (p
- look
), look
, p
- look
)
7611 : STRNCMP(line
- (p
- look
), look
, p
- look
))
7615 if (match
&& try_match_word
&& !try_match
)
7617 /* "0=word": Check if there are only blanks before the
7619 line
= ml_get_curline();
7620 if ((int)(skipwhite(line
) - line
) !=
7621 (int)(curwin
->w_cursor
.col
- (p
- look
)))
7631 * ok, it's a boring generic character.
7635 if (try_match
&& *look
== keytyped
)
7643 look
= skip_to_option_part(look
);
7647 #endif /* FEAT_CINDENT */
7649 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7651 * Map Hebrew keyboard when in hkmap mode.
7657 if (p_hkmapp
) /* phonetic mapping, by Ilya Dogolazky */
7659 enum {hALEF
=0, BET
, GIMEL
, DALET
, HEI
, VAV
, ZAIN
, HET
, TET
, IUD
,
7660 KAFsofit
, hKAF
, LAMED
, MEMsofit
, MEM
, NUNsofit
, NUN
, SAMEH
, AIN
,
7661 PEIsofit
, PEI
, ZADIsofit
, ZADI
, KOF
, RESH
, hSHIN
, TAV
};
7662 static char_u map
[26] =
7663 {(char_u
)hALEF
/*a*/, (char_u
)BET
/*b*/, (char_u
)hKAF
/*c*/,
7664 (char_u
)DALET
/*d*/, (char_u
)-1 /*e*/, (char_u
)PEIsofit
/*f*/,
7665 (char_u
)GIMEL
/*g*/, (char_u
)HEI
/*h*/, (char_u
)IUD
/*i*/,
7666 (char_u
)HET
/*j*/, (char_u
)KOF
/*k*/, (char_u
)LAMED
/*l*/,
7667 (char_u
)MEM
/*m*/, (char_u
)NUN
/*n*/, (char_u
)SAMEH
/*o*/,
7668 (char_u
)PEI
/*p*/, (char_u
)-1 /*q*/, (char_u
)RESH
/*r*/,
7669 (char_u
)ZAIN
/*s*/, (char_u
)TAV
/*t*/, (char_u
)TET
/*u*/,
7670 (char_u
)VAV
/*v*/, (char_u
)hSHIN
/*w*/, (char_u
)-1 /*x*/,
7671 (char_u
)AIN
/*y*/, (char_u
)ZADI
/*z*/};
7673 if (c
== 'N' || c
== 'M' || c
== 'P' || c
== 'C' || c
== 'Z')
7674 return (int)(map
[CharOrd(c
)] - 1 + p_aleph
);
7679 return '\''; /* {geresh}={'} */
7681 return ' '; /* \"o --> ' ' for a german keyboard */
7683 return ' '; /* \"a --> ' ' -- / -- */
7685 return ' '; /* \"u --> ' ' -- / -- */
7687 else if (islower(c
))
7689 /* NOTE: islower() does not do the right thing for us on Linux so we
7690 * do this the same was as 5.7 and previous, so it works correctly on
7691 * all systems. Specifically, the e.g. Delete and Arrow keys are
7692 * munged and won't work if e.g. searching for Hebrew text.
7694 else if (c
>= 'a' && c
<= 'z')
7696 return (int)(map
[CharOrdLow(c
)] + p_aleph
);
7704 case '`': return ';';
7705 case '/': return '.';
7706 case '\'': return ',';
7707 case 'q': return '/';
7708 case 'w': return '\'';
7710 /* Hebrew letters - set offset from 'a' */
7711 case ',': c
= '{'; break;
7712 case '.': c
= 'v'; break;
7713 case ';': c
= 't'; break;
7715 static char str
[] = "zqbcxlsjphmkwonu ydafe rig";
7718 /* see note about islower() above */
7721 if (c
< 'a' || c
> 'z')
7724 c
= str
[CharOrdLow(c
)];
7729 return (int)(CharOrdLow(c
) + p_aleph
);
7737 int need_redraw
= FALSE
;
7741 int vis_active
= VIsual_active
;
7745 * If we are going to wait for a character, show a '"'.
7747 pc_status
= PC_STATUS_UNSET
;
7748 if (redrawing() && !char_avail())
7750 /* may need to redraw when no more chars available now */
7753 edit_putchar('"', TRUE
);
7754 #ifdef FEAT_CMDL_INFO
7755 add_to_showcmd_c(Ctrl_R
);
7759 #ifdef USE_ON_FLY_SCROLL
7760 dont_scroll
= TRUE
; /* disallow scrolling here */
7764 * Don't map the register name. This also prevents the mode message to be
7765 * deleted when ESC is hit.
7768 regname
= plain_vgetc();
7769 LANGMAP_ADJUST(regname
, TRUE
);
7770 if (regname
== Ctrl_R
|| regname
== Ctrl_O
|| regname
== Ctrl_P
)
7772 /* Get a third key for literal register insertion */
7773 literally
= regname
;
7774 #ifdef FEAT_CMDL_INFO
7775 add_to_showcmd_c(literally
);
7777 regname
= plain_vgetc();
7778 LANGMAP_ADJUST(regname
, TRUE
);
7784 * Don't call u_sync() while getting the expression,
7785 * evaluating it or giving an error message for it!
7790 # ifdef USE_IM_CONTROL
7791 int im_on
= im_get_status();
7793 regname
= get_expr_register();
7794 # ifdef USE_IM_CONTROL
7795 /* Restore the Input Method. */
7797 im_set_active(TRUE
);
7800 if (regname
== NUL
|| !valid_yank_reg(regname
, FALSE
))
7803 need_redraw
= TRUE
; /* remove the '"' */
7808 if (literally
== Ctrl_O
|| literally
== Ctrl_P
)
7810 /* Append the command to the redo buffer. */
7811 AppendCharToRedobuff(Ctrl_R
);
7812 AppendCharToRedobuff(literally
);
7813 AppendCharToRedobuff(regname
);
7815 do_put(regname
, BACKWARD
, 1L,
7816 (literally
== Ctrl_P
? PUT_FIXINDENT
: 0) | PUT_CURSEND
);
7818 else if (insert_reg(regname
, literally
) == FAIL
)
7821 need_redraw
= TRUE
; /* remove the '"' */
7823 else if (stop_insert_mode
)
7824 /* When the '=' register was used and a function was invoked that
7825 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7826 * insert anything, need to remove the '"' */
7833 #ifdef FEAT_CMDL_INFO
7837 /* If the inserted register is empty, we need to remove the '"' */
7838 if (need_redraw
|| stuff_empty())
7842 /* Disallow starting Visual mode here, would get a weird mode. */
7843 if (!vis_active
&& VIsual_active
)
7849 * CTRL-G commands in Insert mode.
7856 #ifdef FEAT_INS_EXPAND
7857 /* Right after CTRL-X the cursor will be after the ruler. */
7862 * Don't map the second key. This also prevents the mode message to be
7863 * deleted when ESC is hit.
7870 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7873 case 'k': ins_up(TRUE
);
7876 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7879 case 'j': ins_down(TRUE
);
7882 /* CTRL-G u: start new undoable edit */
7883 case 'u': u_sync(TRUE
);
7884 ins_need_undo
= TRUE
;
7886 /* Need to reset Insstart, esp. because a BS that joins
7887 * a line to the previous one must save for undo. */
7888 Insstart
= curwin
->w_cursor
;
7891 /* Unknown CTRL-G command, reserved for future expansion. */
7892 default: vim_beep();
7897 * CTRL-^ in Insert mode.
7902 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
7904 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7905 if (State
& LANGMAP
)
7907 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7912 curbuf
->b_p_iminsert
= B_IMODE_LMAP
;
7914 #ifdef USE_IM_CONTROL
7915 im_set_active(FALSE
);
7919 #ifdef USE_IM_CONTROL
7922 /* There are no ":lmap" mappings, toggle IM */
7923 if (im_get_status())
7925 curbuf
->b_p_iminsert
= B_IMODE_NONE
;
7926 im_set_active(FALSE
);
7930 curbuf
->b_p_iminsert
= B_IMODE_IM
;
7932 im_set_active(TRUE
);
7936 set_iminsert_global();
7939 /* may show different cursor shape or color */
7941 gui_update_cursor(TRUE
, FALSE
);
7943 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7944 /* Show/unshow value of 'keymap' in status lines. */
7945 status_redraw_curbuf();
7950 * Handle ESC in insert mode.
7951 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7955 ins_esc(count
, cmdchar
, nomove
)
7958 int nomove
; /* don't move cursor */
7961 static int disabled_redraw
= FALSE
;
7964 check_spell_redraw();
7966 #if defined(FEAT_HANGULIN)
7967 # if defined(ESC_CHG_TO_ENG_MODE)
7968 hangul_input_state_set(0);
7970 if (composing_hangul
)
7972 push_raw_key(composing_hangul_buffer
, 2);
7973 composing_hangul
= 0;
7977 temp
= curwin
->w_cursor
.col
;
7978 if (disabled_redraw
)
7980 --RedrawingDisabled
;
7981 disabled_redraw
= FALSE
;
7986 * Don't append the ESC for "r<CR>" and "grx".
7987 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7988 * when "count" is non-zero.
7990 if (cmdchar
!= 'r' && cmdchar
!= 'v')
7991 AppendToRedobuff(p_im
? (char_u
*)"\014" : ESC_STR
);
7994 * Repeating insert may take a long time. Check for
7995 * interrupt now and then.
8004 if (--*count
> 0) /* repeat what was typed */
8006 /* Vi repeats the insert without replacing characters. */
8007 if (vim_strchr(p_cpo
, CPO_REPLCNT
) != NULL
)
8008 State
&= ~REPLACE_FLAG
;
8010 (void)start_redo_ins();
8011 if (cmdchar
== 'r' || cmdchar
== 'v')
8012 stuffReadbuff(ESC_STR
); /* no ESC in redo buffer */
8013 ++RedrawingDisabled
;
8014 disabled_redraw
= TRUE
;
8015 return FALSE
; /* repeat the insert */
8017 stop_insert(&curwin
->w_cursor
, TRUE
);
8021 /* When an autoindent was removed, curswant stays after the
8023 if (restart_edit
== NUL
&& (colnr_T
)temp
== curwin
->w_cursor
.col
)
8024 curwin
->w_set_curswant
= TRUE
;
8026 /* Remember the last Insert position in the '^ mark. */
8027 if (!cmdmod
.keepjumps
)
8028 curbuf
->b_last_insert
= curwin
->w_cursor
;
8031 * The cursor should end up on the last inserted character.
8032 * Don't do it for CTRL-O, unless past the end of the line.
8035 && (curwin
->w_cursor
.col
!= 0
8036 #ifdef FEAT_VIRTUALEDIT
8037 || curwin
->w_cursor
.coladd
> 0
8040 && (restart_edit
== NUL
8041 || (gchar_cursor() == NUL
8046 #ifdef FEAT_RIGHTLEFT
8051 #ifdef FEAT_VIRTUALEDIT
8052 if (curwin
->w_cursor
.coladd
> 0 || ve_flags
== VE_ALL
)
8055 if (restart_edit
!= NUL
)
8056 ++curwin
->w_cursor
.coladd
;
8061 --curwin
->w_cursor
.col
;
8063 /* Correct cursor for multi-byte character. */
8070 #ifdef USE_IM_CONTROL
8071 /* Disable IM to allow typing English directly for Normal mode commands.
8072 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8074 if (!(State
& LANGMAP
))
8075 im_save_status(&curbuf
->b_p_iminsert
);
8076 im_set_active(FALSE
);
8080 /* need to position cursor again (e.g. when on a TAB ) */
8081 changed_cline_bef_curs();
8087 ui_cursor_shape(); /* may show different cursor shape */
8091 * When recording or for CTRL-O, need to display the new mode.
8092 * Otherwise remove the mode message.
8094 if (Recording
|| restart_edit
!= NUL
)
8099 return TRUE
; /* exit Insert mode */
8102 #ifdef FEAT_RIGHTLEFT
8104 * Toggle language: hkmap and revins_on.
8105 * Move to end of reverse inserted text.
8110 if (revins_on
&& revins_chars
&& revins_scol
>= 0)
8112 while (gchar_cursor() != NUL
&& revins_chars
--)
8113 ++curwin
->w_cursor
.col
;
8116 revins_on
= (State
== INSERT
&& p_ri
);
8119 revins_scol
= curwin
->w_cursor
.col
;
8130 * to be consistent also for redo command, using '.'
8131 * set arrow_used to true and stop it - causing to redo
8132 * characters entered in one mode (normal/reverse insert).
8136 p_fkmap
= curwin
->w_p_rl
^ p_ri
;
8137 if (p_fkmap
&& p_ri
)
8142 p_hkmap
= curwin
->w_p_rl
^ p_ri
; /* be consistent! */
8149 * If 'keymodel' contains "startsel", may start selection.
8150 * Returns TRUE when a CTRL-O and other keys stuffed.
8173 if (!(mod_mask
& MOD_MASK_SHIFT
))
8182 /* Start selection right away, the cursor can move with
8183 * CTRL-O when beyond the end of the line. */
8186 /* Execute the key in (insert) Select mode. */
8187 stuffcharReadbuff(Ctrl_O
);
8193 buf
[1] = KS_MODIFIER
;
8198 stuffcharReadbuff(c
);
8206 * <Insert> key in Insert mode: toggle insert/remplace mode.
8209 ins_insert(replaceState
)
8213 if (p_fkmap
&& p_ri
)
8216 EMSG(farsi_text_3
); /* encoded in Farsi */
8223 set_vim_var_string(VV_INSERTMODE
,
8224 (char_u
*)((State
& REPLACE_FLAG
) ? "i" :
8225 # ifdef FEAT_VREPLACE
8226 replaceState
== VREPLACE
? "v" :
8230 apply_autocmds(EVENT_INSERTCHANGE
, NULL
, NULL
, FALSE
, curbuf
);
8232 if (State
& REPLACE_FLAG
)
8233 State
= INSERT
| (State
& LANGMAP
);
8235 State
= replaceState
| (State
& LANGMAP
);
8236 AppendCharToRedobuff(K_INS
);
8239 ui_cursor_shape(); /* may show different cursor shape */
8244 * Pressed CTRL-O in Insert mode.
8249 #ifdef FEAT_VREPLACE
8250 if (State
& VREPLACE_FLAG
)
8254 if (State
& REPLACE_FLAG
)
8258 #ifdef FEAT_VIRTUALEDIT
8259 if (virtual_active())
8260 ins_at_eol
= FALSE
; /* cursor always keeps its column */
8263 ins_at_eol
= (gchar_cursor() == NUL
);
8267 * If the cursor is on an indent, ^T/^D insert/delete one
8268 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8269 * Always round the indent to 'shiftwidth', this is compatible
8270 * with vi. But vi only supports ^T and ^D after an
8271 * autoindent, we support it everywhere.
8278 if (stop_arrow() == FAIL
)
8280 AppendCharToRedobuff(c
);
8283 * 0^D and ^^D: remove all indent.
8285 if (c
== Ctrl_D
&& (lastc
== '0' || lastc
== '^')
8286 && curwin
->w_cursor
.col
> 0)
8288 --curwin
->w_cursor
.col
;
8289 (void)del_char(FALSE
); /* delete the '^' or '0' */
8290 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8291 if (State
& REPLACE_FLAG
)
8294 old_indent
= get_indent(); /* remember curr. indent */
8295 change_indent(INDENT_SET
, 0, TRUE
, 0, TRUE
);
8298 change_indent(c
== Ctrl_D
? INDENT_DEC
: INDENT_INC
, 0, TRUE
, 0, TRUE
);
8300 if (did_ai
&& *skipwhite(ml_get_curline()) != NUL
)
8302 #ifdef FEAT_SMARTINDENT
8305 can_si_back
= FALSE
;
8308 can_cindent
= FALSE
; /* no cindenting after ^D or ^T */
8317 if (stop_arrow() == FAIL
)
8319 if (gchar_cursor() == NUL
) /* delete newline */
8321 temp
= curwin
->w_cursor
.col
;
8322 if (!can_bs(BS_EOL
) /* only if "eol" included */
8323 || u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
8324 (linenr_T
)(curwin
->w_cursor
.lnum
+ 2)) == FAIL
8325 || do_join(FALSE
) == FAIL
)
8328 curwin
->w_cursor
.col
= temp
;
8330 else if (del_char(FALSE
) == FAIL
) /* delete char under cursor */
8333 #ifdef FEAT_SMARTINDENT
8336 can_si_back
= FALSE
;
8338 AppendCharToRedobuff(K_DEL
);
8341 static void ins_bs_one
__ARGS((colnr_T
*vcolp
));
8344 * Delete one character for ins_bs().
8351 getvcol(curwin
, &curwin
->w_cursor
, vcolp
, NULL
, NULL
);
8352 if (State
& REPLACE_FLAG
)
8354 /* Don't delete characters before the insert point when in
8356 if (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8357 || curwin
->w_cursor
.col
>= Insstart
.col
)
8361 (void)del_char(FALSE
);
8365 * Handle Backspace, delete-word and delete-line in Insert mode.
8366 * Return TRUE when backspace was actually used.
8369 ins_bs(c
, mode
, inserted_space_p
)
8372 int *inserted_space_p
;
8376 int temp
= 0; /* init for GCC */
8379 int did_backspace
= FALSE
;
8383 int cpc
[MAX_MCO
]; /* composing characters */
8387 * can't delete anything in an empty file
8388 * can't backup past first character in buffer
8389 * can't backup past starting point unless 'backspace' > 1
8390 * can backup to a previous line if 'backspace' == 0
8394 #ifdef FEAT_RIGHTLEFT
8397 ((curwin
->w_cursor
.lnum
== 1 && curwin
->w_cursor
.col
== 0)
8398 || (!can_bs(BS_START
)
8400 || (curwin
->w_cursor
.lnum
== Insstart
.lnum
8401 && curwin
->w_cursor
.col
<= Insstart
.col
)))
8402 || (!can_bs(BS_INDENT
) && !arrow_used
&& ai_col
> 0
8403 && curwin
->w_cursor
.col
<= ai_col
)
8404 || (!can_bs(BS_EOL
) && curwin
->w_cursor
.col
== 0))))
8410 if (stop_arrow() == FAIL
)
8412 in_indent
= inindent(0);
8415 can_cindent
= FALSE
;
8417 #ifdef FEAT_COMMENTS
8418 end_comment_pending
= NUL
; /* After BS, don't auto-end comment */
8420 #ifdef FEAT_RIGHTLEFT
8421 if (revins_on
) /* put cursor after last inserted char */
8425 #ifdef FEAT_VIRTUALEDIT
8427 * BACKSPACE_CHAR eats a virtual space
8428 * BACKSPACE_WORD eats all coladd
8429 * BACKSPACE_LINE eats all coladd and keeps going
8431 if (curwin
->w_cursor
.coladd
> 0)
8433 if (mode
== BACKSPACE_CHAR
)
8435 --curwin
->w_cursor
.coladd
;
8438 if (mode
== BACKSPACE_WORD
)
8440 curwin
->w_cursor
.coladd
= 0;
8443 curwin
->w_cursor
.coladd
= 0;
8450 if (curwin
->w_cursor
.col
== 0)
8452 lnum
= Insstart
.lnum
;
8453 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8454 #ifdef FEAT_RIGHTLEFT
8459 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 2),
8460 (linenr_T
)(curwin
->w_cursor
.lnum
+ 1)) == FAIL
)
8463 Insstart
.col
= MAXCOL
;
8467 * cc < 0: NL was inserted, delete it
8468 * cc >= 0: NL was replaced, put original characters back
8471 if (State
& REPLACE_FLAG
)
8472 cc
= replace_pop(); /* returns -1 if NL was inserted */
8474 * In replace mode, in the line we started replacing, we only move the
8477 if ((State
& REPLACE_FLAG
) && curwin
->w_cursor
.lnum
<= lnum
)
8483 #ifdef FEAT_VREPLACE
8484 if (!(State
& VREPLACE_FLAG
)
8485 || curwin
->w_cursor
.lnum
> orig_line_count
)
8488 temp
= gchar_cursor(); /* remember current char */
8489 --curwin
->w_cursor
.lnum
;
8491 /* When "aw" is in 'formatoptions' we must delete the space at
8492 * the end of the line, otherwise the line will be broken
8493 * again when auto-formatting. */
8494 if (has_format_option(FO_AUTO
)
8495 && has_format_option(FO_WHITE_PAR
))
8497 char_u
*ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
,
8501 len
= (int)STRLEN(ptr
);
8502 if (len
> 0 && ptr
[len
- 1] == ' ')
8506 (void)do_join(FALSE
);
8507 if (temp
== NUL
&& gchar_cursor() != NUL
)
8510 #ifdef FEAT_VREPLACE
8516 * In REPLACE mode we have to put back the text that was replaced
8517 * by the NL. On the replace stack is first a NUL-terminated
8518 * sequence of characters that were deleted and then the
8519 * characters that NL replaced.
8521 if (State
& REPLACE_FLAG
)
8524 * Do the next ins_char() in NORMAL state, to
8525 * prevent ins_char() from replacing characters and
8526 * avoiding showmatch().
8531 * restore characters (blanks) deleted after cursor
8535 save_col
= curwin
->w_cursor
.col
;
8537 mb_replace_pop_ins(cc
);
8541 curwin
->w_cursor
.col
= save_col
;
8544 /* restore the characters that NL replaced */
8554 * Delete character(s) before the cursor.
8556 #ifdef FEAT_RIGHTLEFT
8557 if (revins_on
) /* put cursor on last inserted char */
8562 if (mode
== BACKSPACE_LINE
8568 #ifdef FEAT_RIGHTLEFT
8573 save_col
= curwin
->w_cursor
.col
;
8574 beginline(BL_WHITE
);
8575 if (curwin
->w_cursor
.col
< save_col
)
8576 mincol
= curwin
->w_cursor
.col
;
8577 curwin
->w_cursor
.col
= save_col
;
8581 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8583 if ( mode
== BACKSPACE_CHAR
8584 && ((p_sta
&& in_indent
)
8585 || (curbuf
->b_p_sts
!= 0
8586 && curwin
->w_cursor
.col
> 0
8587 && (*(ml_get_cursor() - 1) == TAB
8588 || (*(ml_get_cursor() - 1) == ' '
8589 && (!*inserted_space_p
8597 *inserted_space_p
= FALSE
;
8598 if (p_sta
&& in_indent
)
8599 ts
= curbuf
->b_p_sw
;
8601 ts
= curbuf
->b_p_sts
;
8602 /* Compute the virtual column where we want to be. Since
8603 * 'showbreak' may get in the way, need to get the last column of
8604 * the previous character. */
8605 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8608 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &want_vcol
);
8610 want_vcol
= (want_vcol
/ ts
) * ts
;
8612 /* delete characters until we are at or before want_vcol */
8613 while (vcol
> want_vcol
8614 && (cc
= *(ml_get_cursor() - 1), vim_iswhite(cc
)))
8617 /* insert extra spaces until we are at want_vcol */
8618 while (vcol
< want_vcol
)
8620 /* Remember the first char we inserted */
8621 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8622 && curwin
->w_cursor
.col
< Insstart
.col
)
8623 Insstart
.col
= curwin
->w_cursor
.col
;
8625 #ifdef FEAT_VREPLACE
8626 if (State
& VREPLACE_FLAG
)
8631 ins_str((char_u
*)" ");
8632 if ((State
& REPLACE_FLAG
))
8635 getvcol(curwin
, &curwin
->w_cursor
, &vcol
, NULL
, NULL
);
8638 /* If we are now back where we started delete one character. Can
8639 * happen when using 'sts' and 'linebreak'. */
8640 if (vcol
>= start_vcol
)
8645 * Delete upto starting point, start of line or previous word.
8649 #ifdef FEAT_RIGHTLEFT
8650 if (!revins_on
) /* put cursor on char to be deleted */
8654 /* start of word? */
8655 if (mode
== BACKSPACE_WORD
&& !vim_isspace(gchar_cursor()))
8657 mode
= BACKSPACE_WORD_NOT_SPACE
;
8658 temp
= vim_iswordc(gchar_cursor());
8661 else if (mode
== BACKSPACE_WORD_NOT_SPACE
8662 && (vim_isspace(cc
= gchar_cursor())
8663 || vim_iswordc(cc
) != temp
))
8665 #ifdef FEAT_RIGHTLEFT
8669 #ifdef FEAT_RIGHTLEFT
8670 else if (State
& REPLACE_FLAG
)
8675 if (State
& REPLACE_FLAG
)
8680 if (enc_utf8
&& p_deco
)
8681 (void)utfc_ptr2char(ml_get_cursor(), cpc
);
8683 (void)del_char(FALSE
);
8686 * If there are combining characters and 'delcombine' is set
8687 * move the cursor back. Don't back up before the base
8690 if (enc_utf8
&& p_deco
&& cpc
[0] != NUL
)
8693 #ifdef FEAT_RIGHTLEFT
8699 if (revins_on
&& gchar_cursor() == NUL
)
8703 /* Just a single backspace?: */
8704 if (mode
== BACKSPACE_CHAR
)
8707 #ifdef FEAT_RIGHTLEFT
8710 (curwin
->w_cursor
.col
> mincol
8711 && (curwin
->w_cursor
.lnum
!= Insstart
.lnum
8712 || curwin
->w_cursor
.col
!= Insstart
.col
)));
8713 did_backspace
= TRUE
;
8715 #ifdef FEAT_SMARTINDENT
8718 can_si_back
= FALSE
;
8720 if (curwin
->w_cursor
.col
<= 1)
8723 * It's a little strange to put backspaces into the redo
8724 * buffer, but it makes auto-indent a lot easier to deal
8727 AppendCharToRedobuff(c
);
8729 /* If deleted before the insertion point, adjust it */
8730 if (curwin
->w_cursor
.lnum
== Insstart
.lnum
8731 && curwin
->w_cursor
.col
< Insstart
.col
)
8732 Insstart
.col
= curwin
->w_cursor
.col
;
8734 /* vi behaviour: the cursor moves backward but the character that
8735 * was there remains visible
8736 * Vim behaviour: the cursor moves backward and the character that
8737 * was there is erased from the screen.
8738 * We can emulate the vi behaviour by pretending there is a dollar
8739 * displayed even when there isn't.
8740 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8741 if (vim_strchr(p_cpo
, CPO_BACKSPACE
) != NULL
&& dollar_vcol
== 0)
8742 dollar_vcol
= curwin
->w_virtcol
;
8745 /* When deleting a char the cursor line must never be in a closed fold.
8746 * E.g., when 'foldmethod' is indent and deleting the first non-white
8747 * char before a Tab. */
8752 return did_backspace
;
8761 win_T
*old_curwin
= curwin
;
8764 /* When GUI is active, also move/paste when 'mouse' is empty */
8767 if (!mouse_has(MOUSE_INSERT
))
8771 tpos
= curwin
->w_cursor
;
8772 if (do_mouse(NULL
, c
, BACKWARD
, 1L, 0))
8775 win_T
*new_curwin
= curwin
;
8777 if (curwin
!= old_curwin
&& win_valid(old_curwin
))
8779 /* Mouse took us to another window. We need to go back to the
8780 * previous one to stop insert there properly. */
8781 curwin
= old_curwin
;
8782 curbuf
= curwin
->w_buffer
;
8785 start_arrow(curwin
== old_curwin
? &tpos
: NULL
);
8787 if (curwin
!= new_curwin
&& win_valid(new_curwin
))
8789 curwin
= new_curwin
;
8790 curbuf
= curwin
->w_buffer
;
8793 # ifdef FEAT_CINDENT
8799 /* redraw status lines (in case another window became active) */
8800 redraw_statuslines();
8809 # if defined(FEAT_WINDOWS)
8810 win_T
*old_curwin
= curwin
;
8812 # ifdef FEAT_INS_EXPAND
8813 int did_scroll
= FALSE
;
8816 tpos
= curwin
->w_cursor
;
8818 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8819 /* Currently the mouse coordinates are only known in the GUI. */
8820 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
8827 /* find the window at the pointer coordinates */
8828 curwin
= mouse_find_win(&row
, &col
);
8829 curbuf
= curwin
->w_buffer
;
8831 if (curwin
== old_curwin
)
8835 # ifdef FEAT_INS_EXPAND
8836 /* Don't scroll the window in which completion is being done. */
8838 # if defined(FEAT_WINDOWS)
8839 || curwin
!= old_curwin
8844 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
8845 scroll_redraw(up
, (long)(curwin
->w_botline
- curwin
->w_topline
));
8847 scroll_redraw(up
, 3L);
8848 # ifdef FEAT_INS_EXPAND
8853 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8854 curwin
->w_redr_status
= TRUE
;
8856 curwin
= old_curwin
;
8857 curbuf
= curwin
->w_buffer
;
8860 # ifdef FEAT_INS_EXPAND
8861 /* The popup menu may overlay the window, need to redraw it.
8862 * TODO: Would be more efficient to only redraw the windows that are
8863 * overlapped by the popup menu. */
8864 if (pum_visible() && did_scroll
)
8866 redraw_all_later(NOT_VALID
);
8867 ins_compl_show_pum();
8871 if (!equalpos(curwin
->w_cursor
, tpos
))
8874 # ifdef FEAT_CINDENT
8881 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8886 /* We will be leaving the current window, unless closing another tab. */
8887 if (c
!= K_TABMENU
|| current_tabmenu
!= TABLINE_MENU_CLOSE
8888 || (current_tab
!= 0 && current_tab
!= tabpage_index(curtab
)))
8891 start_arrow(&curwin
->w_cursor
);
8892 # ifdef FEAT_CINDENT
8898 goto_tabpage(current_tab
);
8902 redraw_statuslines(); /* will redraw the tabline when needed */
8907 #if defined(FEAT_GUI) || defined(PROTO)
8914 tpos
= curwin
->w_cursor
;
8915 if (gui_do_scroll())
8918 # ifdef FEAT_CINDENT
8930 tpos
= curwin
->w_cursor
;
8931 if (gui_do_horiz_scroll())
8934 # ifdef FEAT_CINDENT
8947 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8951 tpos
= curwin
->w_cursor
;
8952 if (oneleft() == OK
)
8954 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8955 /* Only call start_arrow() when not busy with preediting, it will
8956 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8957 if (!im_is_preediting())
8960 #ifdef FEAT_RIGHTLEFT
8961 /* If exit reversed string, position is fixed */
8962 if (revins_scol
!= -1 && (int)curwin
->w_cursor
.col
>= revins_scol
)
8969 * if 'whichwrap' set for cursor in insert mode may go to
8972 else if (vim_strchr(p_ww
, '[') != NULL
&& curwin
->w_cursor
.lnum
> 1)
8975 --(curwin
->w_cursor
.lnum
);
8976 coladvance((colnr_T
)MAXCOL
);
8977 curwin
->w_set_curswant
= TRUE
; /* so we stay at the end */
8990 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
8994 tpos
= curwin
->w_cursor
;
8996 curwin
->w_cursor
.lnum
= 1;
8997 curwin
->w_cursor
.col
= 0;
8998 #ifdef FEAT_VIRTUALEDIT
8999 curwin
->w_cursor
.coladd
= 0;
9001 curwin
->w_curswant
= 0;
9012 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9016 tpos
= curwin
->w_cursor
;
9018 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9019 coladvance((colnr_T
)MAXCOL
);
9020 curwin
->w_curswant
= MAXCOL
;
9029 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9033 if (curwin
->w_cursor
.lnum
> 1 || curwin
->w_cursor
.col
> 0)
9035 start_arrow(&curwin
->w_cursor
);
9036 (void)bck_word(1L, FALSE
, FALSE
);
9037 curwin
->w_set_curswant
= TRUE
;
9047 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9051 if (gchar_cursor() != NUL
9052 #ifdef FEAT_VIRTUALEDIT
9057 start_arrow(&curwin
->w_cursor
);
9058 curwin
->w_set_curswant
= TRUE
;
9059 #ifdef FEAT_VIRTUALEDIT
9060 if (virtual_active())
9067 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
9070 ++curwin
->w_cursor
.col
;
9073 #ifdef FEAT_RIGHTLEFT
9079 /* if 'whichwrap' set for cursor in insert mode, may move the
9080 * cursor to the next line */
9081 else if (vim_strchr(p_ww
, ']') != NULL
9082 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
9084 start_arrow(&curwin
->w_cursor
);
9085 curwin
->w_set_curswant
= TRUE
;
9086 ++curwin
->w_cursor
.lnum
;
9087 curwin
->w_cursor
.col
= 0;
9097 if ((fdo_flags
& FDO_HOR
) && KeyTyped
)
9101 if (curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
9102 || gchar_cursor() != NUL
)
9104 start_arrow(&curwin
->w_cursor
);
9105 (void)fwd_word(1L, FALSE
, 0);
9106 curwin
->w_set_curswant
= TRUE
;
9114 int startcol
; /* when TRUE move to Insstart.col */
9117 linenr_T old_topline
= curwin
->w_topline
;
9119 int old_topfill
= curwin
->w_topfill
;
9123 tpos
= curwin
->w_cursor
;
9124 if (cursor_up(1L, TRUE
) == OK
)
9127 coladvance(getvcol_nolist(&Insstart
));
9128 if (old_topline
!= curwin
->w_topline
9130 || old_topfill
!= curwin
->w_topfill
9133 redraw_later(VALID
);
9151 if (mod_mask
& MOD_MASK_CTRL
)
9153 /* <C-PageUp>: tab page back */
9154 if (first_tabpage
->tp_next
!= NULL
)
9156 start_arrow(&curwin
->w_cursor
);
9163 tpos
= curwin
->w_cursor
;
9164 if (onepage(BACKWARD
, 1L) == OK
)
9177 int startcol
; /* when TRUE move to Insstart.col */
9180 linenr_T old_topline
= curwin
->w_topline
;
9182 int old_topfill
= curwin
->w_topfill
;
9186 tpos
= curwin
->w_cursor
;
9187 if (cursor_down(1L, TRUE
) == OK
)
9190 coladvance(getvcol_nolist(&Insstart
));
9191 if (old_topline
!= curwin
->w_topline
9193 || old_topfill
!= curwin
->w_topfill
9196 redraw_later(VALID
);
9214 if (mod_mask
& MOD_MASK_CTRL
)
9216 /* <C-PageDown>: tab page forward */
9217 if (first_tabpage
->tp_next
!= NULL
)
9219 start_arrow(&curwin
->w_cursor
);
9226 tpos
= curwin
->w_cursor
;
9227 if (onepage(FORWARD
, 1L) == OK
)
9242 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9247 * Handle TAB in Insert or Replace mode.
9248 * Return TRUE when the TAB needs to be inserted like a normal character.
9257 if (Insstart_blank_vcol
== MAXCOL
&& curwin
->w_cursor
.lnum
== Insstart
.lnum
)
9258 Insstart_blank_vcol
= get_nolist_virtcol();
9259 if (echeck_abbr(TAB
+ ABBR_OFF
))
9265 can_cindent
= FALSE
;
9269 * When nothing special, insert TAB like a normal character
9272 && !(p_sta
&& ind
&& curbuf
->b_p_ts
!= curbuf
->b_p_sw
)
9273 && curbuf
->b_p_sts
== 0)
9276 if (stop_arrow() == FAIL
)
9280 #ifdef FEAT_SMARTINDENT
9283 can_si_back
= FALSE
;
9285 AppendToRedobuff((char_u
*)"\t");
9287 if (p_sta
&& ind
) /* insert tab in indent, use 'shiftwidth' */
9288 temp
= (int)curbuf
->b_p_sw
;
9289 else if (curbuf
->b_p_sts
> 0) /* use 'softtabstop' when set */
9290 temp
= (int)curbuf
->b_p_sts
;
9291 else /* otherwise use 'tabstop' */
9292 temp
= (int)curbuf
->b_p_ts
;
9293 temp
-= get_nolist_virtcol() % temp
;
9296 * Insert the first space with ins_char(). It will delete one char in
9297 * replace mode. Insert the rest with ins_str(); it will not delete any
9298 * chars. For VREPLACE mode, we use ins_char() for all characters.
9303 #ifdef FEAT_VREPLACE
9304 if (State
& VREPLACE_FLAG
)
9309 ins_str((char_u
*)" ");
9310 if (State
& REPLACE_FLAG
) /* no char replaced */
9316 * When 'expandtab' not set: Replace spaces by TABs where possible.
9318 if (!curbuf
->b_p_et
&& (curbuf
->b_p_sts
|| (p_sta
&& ind
)))
9321 #ifdef FEAT_VREPLACE
9322 char_u
*saved_line
= NULL
; /* init for GCC */
9327 colnr_T want_vcol
, vcol
;
9328 int change_col
= -1;
9329 int save_list
= curwin
->w_p_list
;
9332 * Get the current line. For VREPLACE mode, don't make real changes
9333 * yet, just work on a copy of the line.
9335 #ifdef FEAT_VREPLACE
9336 if (State
& VREPLACE_FLAG
)
9338 pos
= curwin
->w_cursor
;
9340 saved_line
= vim_strsave(ml_get_curline());
9341 if (saved_line
== NULL
)
9343 ptr
= saved_line
+ pos
.col
;
9348 ptr
= ml_get_cursor();
9349 cursor
= &curwin
->w_cursor
;
9352 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9353 if (vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9354 curwin
->w_p_list
= FALSE
;
9356 /* Find first white before the cursor */
9357 fpos
= curwin
->w_cursor
;
9358 while (fpos
.col
> 0 && vim_iswhite(ptr
[-1]))
9364 /* In Replace mode, don't change characters before the insert point. */
9365 if ((State
& REPLACE_FLAG
)
9366 && fpos
.lnum
== Insstart
.lnum
9367 && fpos
.col
< Insstart
.col
)
9369 ptr
+= Insstart
.col
- fpos
.col
;
9370 fpos
.col
= Insstart
.col
;
9373 /* compute virtual column numbers of first white and cursor */
9374 getvcol(curwin
, &fpos
, &vcol
, NULL
, NULL
);
9375 getvcol(curwin
, cursor
, &want_vcol
, NULL
, NULL
);
9377 /* Use as many TABs as possible. Beware of 'showbreak' and
9378 * 'linebreak' adding extra virtual columns. */
9379 while (vim_iswhite(*ptr
))
9381 i
= lbr_chartabsize((char_u
*)"\t", vcol
);
9382 if (vcol
+ i
> want_vcol
)
9389 change_col
= fpos
.col
; /* Column of first change */
9390 /* May have to adjust Insstart */
9391 if (fpos
.lnum
== Insstart
.lnum
&& fpos
.col
< Insstart
.col
)
9392 Insstart
.col
= fpos
.col
;
9400 if (change_col
>= 0)
9404 /* Skip over the spaces we need. */
9405 while (vcol
< want_vcol
&& *ptr
== ' ')
9407 vcol
+= lbr_chartabsize(ptr
, vcol
);
9411 if (vcol
> want_vcol
)
9413 /* Must have a char with 'showbreak' just before it. */
9417 fpos
.col
+= repl_off
;
9419 /* Delete following spaces. */
9420 i
= cursor
->col
- fpos
.col
;
9423 STRMOVE(ptr
, ptr
+ i
);
9424 /* correct replace stack. */
9425 if ((State
& REPLACE_FLAG
)
9426 #ifdef FEAT_VREPLACE
9427 && !(State
& VREPLACE_FLAG
)
9430 for (temp
= i
; --temp
>= 0; )
9431 replace_join(repl_off
);
9433 #ifdef FEAT_NETBEANS_INTG
9436 netbeans_removed(curbuf
, fpos
.lnum
, cursor
->col
,
9438 netbeans_inserted(curbuf
, fpos
.lnum
, cursor
->col
,
9444 #ifdef FEAT_VREPLACE
9446 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9447 * backspacing over the changed spacing and then inserting the new
9450 if (State
& VREPLACE_FLAG
)
9452 /* Backspace from real cursor to change_col */
9453 backspace_until_column(change_col
);
9455 /* Insert each char in saved_line from changed_col to
9457 ins_bytes_len(saved_line
+ change_col
,
9458 cursor
->col
- change_col
);
9463 #ifdef FEAT_VREPLACE
9464 if (State
& VREPLACE_FLAG
)
9465 vim_free(saved_line
);
9467 curwin
->w_p_list
= save_list
;
9474 * Handle CR or NL in insert mode.
9475 * Return TRUE when out of memory or can't undo.
9483 if (echeck_abbr(c
+ ABBR_OFF
))
9485 if (stop_arrow() == FAIL
)
9490 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9491 * character under the cursor. Only push a NUL on the replace stack,
9492 * nothing to put back when the NL is deleted.
9494 if ((State
& REPLACE_FLAG
)
9495 #ifdef FEAT_VREPLACE
9496 && !(State
& VREPLACE_FLAG
)
9502 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9503 * replacing the next line, so we push all of the characters left on the
9504 * line onto the replace stack. This is not done here though, it is done
9508 #ifdef FEAT_VIRTUALEDIT
9509 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9511 if (virtual_active() && curwin
->w_cursor
.coladd
> 0)
9512 coladvance(getviscol());
9515 #ifdef FEAT_RIGHTLEFT
9517 if (p_altkeymap
&& p_fkmap
)
9520 /* NL in reverse insert will always start in the end of
9523 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
9526 AppendToRedobuff(NL_STR
);
9527 i
= open_line(FORWARD
,
9528 #ifdef FEAT_COMMENTS
9529 has_format_option(FO_RET_COMS
) ? OPENLINE_DO_COM
:
9537 /* When inserting a line the cursor line must never be in a closed fold. */
9544 #ifdef FEAT_DIGRAPHS
9546 * Handle digraph in insert mode.
9547 * Returns character still to be inserted, or NUL when nothing remaining to be
9556 pc_status
= PC_STATUS_UNSET
;
9557 if (redrawing() && !char_avail())
9559 /* may need to redraw when no more chars available now */
9562 edit_putchar('?', TRUE
);
9563 #ifdef FEAT_CMDL_INFO
9564 add_to_showcmd_c(Ctrl_K
);
9568 #ifdef USE_ON_FLY_SCROLL
9569 dont_scroll
= TRUE
; /* disallow scrolling here */
9572 /* don't map the digraph chars. This also prevents the
9573 * mode message to be deleted when ESC is hit */
9579 if (IS_SPECIAL(c
) || mod_mask
) /* special key */
9581 #ifdef FEAT_CMDL_INFO
9584 insert_special(c
, TRUE
, FALSE
);
9589 if (redrawing() && !char_avail())
9591 /* may need to redraw when no more chars available now */
9594 if (char2cells(c
) == 1)
9596 /* first remove the '?', otherwise it's restored when typing
9600 edit_putchar(c
, TRUE
);
9602 #ifdef FEAT_CMDL_INFO
9603 add_to_showcmd_c(c
);
9613 AppendToRedobuff((char_u
*)CTRL_V_STR
);
9614 c
= getdigraph(c
, cc
, TRUE
);
9615 #ifdef FEAT_CMDL_INFO
9622 #ifdef FEAT_CMDL_INFO
9630 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9631 * Returns the char to be inserted, or NUL if none found.
9639 char_u
*ptr
, *prev_ptr
;
9641 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
9647 /* try to advance to the cursor column */
9652 while ((colnr_T
)temp
< curwin
->w_virtcol
&& *ptr
!= NUL
)
9655 temp
+= lbr_chartabsize_adv(&ptr
, (colnr_T
)temp
);
9657 if ((colnr_T
)temp
> curwin
->w_virtcol
)
9661 c
= (*mb_ptr2char
)(ptr
);
9671 * CTRL-Y or CTRL-E typed in Insert mode.
9679 #ifdef FEAT_INS_EXPAND
9680 if (ctrl_x_mode
== CTRL_X_SCROLL
)
9686 redraw_later(VALID
);
9691 c
= ins_copychar(curwin
->w_cursor
.lnum
+ (c
== Ctrl_Y
? -1 : 1));
9696 /* The character must be taken literally, insert like it
9697 * was typed after a CTRL-V, and pretend 'textwidth'
9698 * wasn't set. Digits, 'o' and 'x' are special after a
9699 * CTRL-V, don't use it for these. */
9700 if (c
< 256 && !isalnum(c
))
9701 AppendToRedobuff((char_u
*)CTRL_V_STR
); /* CTRL-V */
9702 tw_save
= curbuf
->b_p_tw
;
9703 curbuf
->b_p_tw
= -1;
9704 insert_special(c
, TRUE
, FALSE
);
9705 curbuf
->b_p_tw
= tw_save
;
9706 #ifdef FEAT_RIGHTLEFT
9710 c
= Ctrl_V
; /* pretend CTRL-V is last character */
9711 auto_format(FALSE
, TRUE
);
9717 #ifdef FEAT_SMARTINDENT
9719 * Try to do some very smart auto-indenting.
9720 * Used when inserting a "normal" character.
9726 pos_T
*pos
, old_pos
;
9732 * do some very smart indenting when entering '{' or '}'
9734 if (((did_si
|| can_si_back
) && c
== '{') || (can_si
&& c
== '}'))
9737 * for '}' set indent equal to indent of line containing matching '{'
9739 if (c
== '}' && (pos
= findmatch(NULL
, '{')) != NULL
)
9741 old_pos
= curwin
->w_cursor
;
9743 * If the matching '{' has a ')' immediately before it (ignoring
9744 * white-space), then line up with the start of the line
9745 * containing the matching '(' if there is one. This handles the
9746 * case where an "if (..\n..) {" statement continues over multiple
9749 ptr
= ml_get(pos
->lnum
);
9751 if (i
> 0) /* skip blanks before '{' */
9752 while (--i
> 0 && vim_iswhite(ptr
[i
]))
9754 curwin
->w_cursor
.lnum
= pos
->lnum
;
9755 curwin
->w_cursor
.col
= i
;
9756 if (ptr
[i
] == ')' && (pos
= findmatch(NULL
, '(')) != NULL
)
9757 curwin
->w_cursor
= *pos
;
9759 curwin
->w_cursor
= old_pos
;
9760 #ifdef FEAT_VREPLACE
9761 if (State
& VREPLACE_FLAG
)
9762 change_indent(INDENT_SET
, i
, FALSE
, NUL
, TRUE
);
9765 (void)set_indent(i
, SIN_CHANGED
);
9767 else if (curwin
->w_cursor
.col
> 0)
9770 * when inserting '{' after "O" reduce indent, but not
9771 * more than indent of previous line
9774 if (c
== '{' && can_si_back
&& curwin
->w_cursor
.lnum
> 1)
9776 old_pos
= curwin
->w_cursor
;
9778 while (curwin
->w_cursor
.lnum
> 1)
9780 ptr
= skipwhite(ml_get(--(curwin
->w_cursor
.lnum
)));
9782 /* ignore empty lines and lines starting with '#'. */
9783 if (*ptr
!= '#' && *ptr
!= NUL
)
9786 if (get_indent() >= i
)
9788 curwin
->w_cursor
= old_pos
;
9791 shift_line(TRUE
, FALSE
, 1, TRUE
);
9796 * set indent of '#' always to 0
9798 if (curwin
->w_cursor
.col
> 0 && can_si
&& c
== '#')
9800 /* remember current indent for next line */
9801 old_indent
= get_indent();
9802 (void)set_indent(0, SIN_CHANGED
);
9805 /* Adjust ai_col, the char at this position can be deleted. */
9806 if (ai_col
> curwin
->w_cursor
.col
)
9807 ai_col
= curwin
->w_cursor
.col
;
9812 * Get the value that w_virtcol would have when 'list' is off.
9813 * Unless 'cpo' contains the 'L' flag.
9816 get_nolist_virtcol()
9818 if (curwin
->w_p_list
&& vim_strchr(p_cpo
, CPO_LISTWM
) == NULL
)
9819 return getvcol_nolist(&curwin
->w_cursor
);
9821 return curwin
->w_virtcol
;