7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
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.
|
|
8 */
|
|
9
|
|
10 /*
|
|
11 * edit.c: functions for Insert mode
|
|
12 */
|
|
13
|
|
14 #include "vim.h"
|
|
15
|
|
16 #ifdef FEAT_INS_EXPAND
|
|
17 /*
|
|
18 * definitions used for CTRL-X submode
|
|
19 */
|
|
20 #define CTRL_X_WANT_IDENT 0x100
|
|
21
|
|
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
|
12
|
33 #define CTRL_X_FUNCTION 12
|
523
|
34 #define CTRL_X_OMNI 13
|
477
|
35 #define CTRL_X_SPELL 14
|
|
36 #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
|
7
|
37
|
|
38 #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
|
|
39
|
|
40 static char *ctrl_x_msgs[] =
|
|
41 {
|
|
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
|
819
|
43 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
|
449
|
44 NULL,
|
7
|
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)"),
|
|
50 NULL,
|
|
51 N_(" Dictionary completion (^K^N^P)"),
|
|
52 N_(" Thesaurus completion (^T^N^P)"),
|
12
|
53 N_(" Command-line completion (^V^N^P)"),
|
|
54 N_(" User defined completion (^U^N^P)"),
|
523
|
55 N_(" Omni completion (^O^N^P)"),
|
819
|
56 N_(" Spelling suggestion (s^N^P)"),
|
449
|
57 N_(" Keyword Local completion (^N^P)"),
|
7
|
58 };
|
|
59
|
1869
|
60 static char e_hitend[] = N_("Hit end of paragraph");
|
7
|
61
|
|
62 /*
|
|
63 * Structure used to store one match for insert completion.
|
|
64 */
|
681
|
65 typedef struct compl_S compl_T;
|
|
66 struct compl_S
|
7
|
67 {
|
464
|
68 compl_T *cp_next;
|
|
69 compl_T *cp_prev;
|
|
70 char_u *cp_str; /* matched text */
|
681
|
71 char cp_icase; /* TRUE or FALSE: ignore case */
|
1869
|
72 #ifdef S_SPLINT_S /* splint can't handle array of pointers */
|
|
73 char_u **cp_text; /* text for the menu */
|
|
74 #else
|
786
|
75 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
|
1869
|
76 #endif
|
659
|
77 char_u *cp_fname; /* file containing the match, allocated when
|
|
78 * cp_flags has FREE_FNAME */
|
464
|
79 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
|
|
80 int cp_number; /* sequence number */
|
7
|
81 };
|
|
82
|
464
|
83 #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
|
7
|
84 #define FREE_FNAME (2)
|
|
85
|
|
86 /*
|
|
87 * All the current matches are stored in a list.
|
449
|
88 * "compl_first_match" points to the start of the list.
|
|
89 * "compl_curr_match" points to the currently selected entry.
|
|
90 * "compl_shown_match" is different from compl_curr_match during
|
|
91 * ins_compl_get_exp().
|
7
|
92 */
|
464
|
93 static compl_T *compl_first_match = NULL;
|
|
94 static compl_T *compl_curr_match = NULL;
|
|
95 static compl_T *compl_shown_match = NULL;
|
449
|
96
|
825
|
97 /* After using a cursor key <Enter> selects a match in the popup menu,
|
|
98 * otherwise it inserts a line break. */
|
|
99 static int compl_enter_selects = FALSE;
|
|
100
|
657
|
101 /* When "compl_leader" is not NULL only matches that start with this string
|
|
102 * are used. */
|
|
103 static char_u *compl_leader = NULL;
|
|
104
|
665
|
105 static int compl_get_longest = FALSE; /* put longest common string
|
|
106 in compl_leader */
|
|
107
|
657
|
108 static int compl_used_match; /* Selected one of the matches. When
|
|
109 FALSE the match was edited or using
|
|
110 the longest common string. */
|
|
111
|
874
|
112 static int compl_was_interrupted = FALSE; /* didn't finish finding
|
|
113 completions. */
|
|
114
|
|
115 static int compl_restarting = FALSE; /* don't insert match */
|
|
116
|
449
|
117 /* When the first completion is done "compl_started" is set. When it's
|
|
118 * FALSE the word to be completed must be located. */
|
644
|
119 static int compl_started = FALSE;
|
449
|
120
|
464
|
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;
|
716
|
125 static int compl_pending = 0; /* > 1 for postponed CTRL-N */
|
464
|
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;
|
449
|
133
|
|
134 static void ins_ctrl_x __ARGS((void));
|
|
135 static int has_compl_option __ARGS((int dict_opt));
|
1430
|
136 static int ins_compl_accept_char __ARGS((int c));
|
944
|
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));
|
681
|
138 static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
|
665
|
139 static void ins_compl_longest_match __ARGS((compl_T *match));
|
681
|
140 static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
|
7
|
141 static int ins_compl_make_cyclic __ARGS((void));
|
540
|
142 static void ins_compl_upd_pum __ARGS((void));
|
|
143 static void ins_compl_del_pum __ARGS((void));
|
648
|
144 static int pum_wanted __ARGS((void));
|
707
|
145 static int pum_enough_matches __ARGS((void));
|
659
|
146 static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
|
703
|
147 static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
|
667
|
148 static char_u *find_line_end __ARGS((char_u *ptr));
|
7
|
149 static void ins_compl_free __ARGS((void));
|
|
150 static void ins_compl_clear __ARGS((void));
|
657
|
151 static int ins_compl_bs __ARGS((void));
|
874
|
152 static void ins_compl_new_leader __ARGS((void));
|
657
|
153 static void ins_compl_addleader __ARGS((int c));
|
1782
|
154 static int ins_compl_len __ARGS((void));
|
874
|
155 static void ins_compl_restart __ARGS((void));
|
694
|
156 static void ins_compl_set_original_text __ARGS((char_u *str));
|
659
|
157 static void ins_compl_addfrommatch __ARGS((void));
|
540
|
158 static int ins_compl_prep __ARGS((int c));
|
7
|
159 static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
|
724
|
160 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
|
|
161 static void ins_compl_add_list __ARGS((list_T *list));
|
|
162 #endif
|
659
|
163 static int ins_compl_get_exp __ARGS((pos_T *ini));
|
7
|
164 static void ins_compl_delete __ARGS((void));
|
|
165 static void ins_compl_insert __ARGS((void));
|
665
|
166 static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
|
610
|
167 static int ins_compl_key2dir __ARGS((int c));
|
644
|
168 static int ins_compl_pum_key __ARGS((int c));
|
610
|
169 static int ins_compl_key2count __ARGS((int c));
|
681
|
170 static int ins_compl_use_match __ARGS((int c));
|
7
|
171 static int ins_complete __ARGS((int c));
|
|
172 static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
|
|
173 #endif /* FEAT_INS_EXPAND */
|
|
174
|
|
175 #define BACKSPACE_CHAR 1
|
|
176 #define BACKSPACE_WORD 2
|
|
177 #define BACKSPACE_WORD_NOT_SPACE 3
|
|
178 #define BACKSPACE_LINE 4
|
|
179
|
661
|
180 static void ins_redraw __ARGS((int ready));
|
7
|
181 static void ins_ctrl_v __ARGS((void));
|
|
182 static void undisplay_dollar __ARGS((void));
|
|
183 static void insert_special __ARGS((int, int, int));
|
667
|
184 static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
|
7
|
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));
|
744
|
188 #ifdef FEAT_SPELL
|
221
|
189 static void check_spell_redraw __ARGS((void));
|
484
|
190 static void spell_back_to_badword __ARGS((void));
|
497
|
191 static int spell_bad_len = 0; /* length of located bad word */
|
221
|
192 #endif
|
7
|
193 static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
|
|
194 static int echeck_abbr __ARGS((int));
|
840
|
195 #if 0
|
7
|
196 static void replace_push_off __ARGS((int c));
|
840
|
197 #endif
|
7
|
198 static int replace_pop __ARGS((void));
|
|
199 static void replace_join __ARGS((int off));
|
|
200 static void replace_pop_ins __ARGS((void));
|
|
201 #ifdef FEAT_MBYTE
|
|
202 static void mb_replace_pop_ins __ARGS((int cc));
|
|
203 #endif
|
|
204 static void replace_flush __ARGS((void));
|
1782
|
205 static void replace_do_bs __ARGS((int limit_col));
|
|
206 static int del_char_after_col __ARGS((int limit_col));
|
7
|
207 #ifdef FEAT_CINDENT
|
|
208 static int cindent_on __ARGS((void));
|
|
209 #endif
|
|
210 static void ins_reg __ARGS((void));
|
|
211 static void ins_ctrl_g __ARGS((void));
|
449
|
212 static void ins_ctrl_hat __ARGS((void));
|
477
|
213 static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
|
7
|
214 #ifdef FEAT_RIGHTLEFT
|
|
215 static void ins_ctrl_ __ARGS((void));
|
|
216 #endif
|
|
217 #ifdef FEAT_VISUAL
|
|
218 static int ins_start_select __ARGS((int c));
|
|
219 #endif
|
449
|
220 static void ins_insert __ARGS((int replaceState));
|
|
221 static void ins_ctrl_o __ARGS((void));
|
7
|
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));
|
|
225 #ifdef FEAT_MOUSE
|
|
226 static void ins_mouse __ARGS((int c));
|
|
227 static void ins_mousescroll __ARGS((int up));
|
|
228 #endif
|
692
|
229 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
|
|
230 static void ins_tabline __ARGS((int c));
|
|
231 #endif
|
7
|
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));
|
|
242 #ifdef FEAT_DND
|
|
243 static void ins_drop __ARGS((void));
|
|
244 #endif
|
|
245 static int ins_tab __ARGS((void));
|
|
246 static int ins_eol __ARGS((int c));
|
|
247 #ifdef FEAT_DIGRAPHS
|
|
248 static int ins_digraph __ARGS((void));
|
|
249 #endif
|
|
250 static int ins_copychar __ARGS((linenr_T lnum));
|
449
|
251 static int ins_ctrl_ey __ARGS((int tc));
|
7
|
252 #ifdef FEAT_SMARTINDENT
|
|
253 static void ins_try_si __ARGS((int c));
|
|
254 #endif
|
|
255 static colnr_T get_nolist_virtcol __ARGS((void));
|
|
256
|
|
257 static colnr_T Insstart_textlen; /* length of line when insert started */
|
|
258 static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
|
|
259
|
|
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 */
|
603
|
264 static int did_restart_edit; /* "restart_edit" when calling edit() */
|
7
|
265
|
|
266 #ifdef FEAT_CINDENT
|
|
267 static int can_cindent; /* may do cindenting on this line */
|
|
268 #endif
|
|
269
|
|
270 static int old_indent = 0; /* for ^^D command in insert mode */
|
|
271
|
|
272 #ifdef FEAT_RIGHTLEFT
|
298
|
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 */
|
7
|
277 #endif
|
|
278
|
|
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. */
|
|
282
|
|
283 static int did_add_space = FALSE; /* auto_format() added an extra space
|
|
284 under the cursor */
|
|
285
|
|
286 /*
|
|
287 * edit(): Start inserting text.
|
|
288 *
|
|
289 * "cmdchar" can be:
|
|
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.
|
|
295 * 'g' "gI" command.
|
|
296 * 'V' "gR" command for Virtual Replace mode.
|
|
297 * 'v' "gr" command for single character Virtual Replace mode.
|
|
298 *
|
|
299 * This function is not called recursively. For CTRL-O commands, it returns
|
|
300 * and lets the caller handle the Normal-mode command.
|
|
301 *
|
|
302 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
|
|
303 */
|
|
304 int
|
|
305 edit(cmdchar, startln, count)
|
|
306 int cmdchar;
|
|
307 int startln; /* if set, insert at start of line */
|
|
308 long count;
|
|
309 {
|
|
310 int c = 0;
|
|
311 char_u *ptr;
|
|
312 int lastc;
|
1869
|
313 int mincol;
|
7
|
314 static linenr_T o_lnum = 0;
|
|
315 int i;
|
|
316 int did_backspace = TRUE; /* previous char was backspace */
|
|
317 #ifdef FEAT_CINDENT
|
|
318 int line_is_white = FALSE; /* line is empty before insert */
|
|
319 #endif
|
|
320 linenr_T old_topline = 0; /* topline before insertion */
|
|
321 #ifdef FEAT_DIFF
|
|
322 int old_topfill = -1;
|
|
323 #endif
|
|
324 int inserted_space = FALSE; /* just inserted a space */
|
|
325 int replaceState = REPLACE;
|
477
|
326 int nomove = FALSE; /* don't move cursor on return */
|
7
|
327
|
603
|
328 /* Remember whether editing was restarted after CTRL-O. */
|
|
329 did_restart_edit = restart_edit;
|
|
330
|
7
|
331 /* sleep before redrawing, needed for "CTRL-O :" that results in an
|
|
332 * error message */
|
|
333 check_for_delay(TRUE);
|
|
334
|
|
335 #ifdef HAVE_SANDBOX
|
|
336 /* Don't allow inserting in the sandbox. */
|
|
337 if (sandbox != 0)
|
|
338 {
|
|
339 EMSG(_(e_sandbox));
|
|
340 return FALSE;
|
|
341 }
|
|
342 #endif
|
632
|
343 /* Don't allow changes in the buffer while editing the cmdline. The
|
|
344 * caller of getcmdline() may get confused. */
|
634
|
345 if (textlock != 0)
|
632
|
346 {
|
|
347 EMSG(_(e_secure));
|
|
348 return FALSE;
|
|
349 }
|
7
|
350
|
|
351 #ifdef FEAT_INS_EXPAND
|
844
|
352 /* Don't allow recursive insert mode when busy with completion. */
|
|
353 if (compl_started || pum_visible())
|
|
354 {
|
|
355 EMSG(_(e_secure));
|
|
356 return FALSE;
|
|
357 }
|
7
|
358 ins_compl_clear(); /* clear stuff for CTRL-X mode */
|
|
359 #endif
|
|
360
|
11
|
361 #ifdef FEAT_AUTOCMD
|
|
362 /*
|
|
363 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
|
|
364 */
|
|
365 if (cmdchar != 'r' && cmdchar != 'v')
|
|
366 {
|
532
|
367 # ifdef FEAT_EVAL
|
11
|
368 if (cmdchar == 'R')
|
|
369 ptr = (char_u *)"r";
|
|
370 else if (cmdchar == 'V')
|
|
371 ptr = (char_u *)"v";
|
|
372 else
|
|
373 ptr = (char_u *)"i";
|
|
374 set_vim_var_string(VV_INSERTMODE, ptr, 1);
|
532
|
375 # endif
|
11
|
376 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
|
|
377 }
|
|
378 #endif
|
|
379
|
7
|
380 #ifdef FEAT_MOUSE
|
|
381 /*
|
|
382 * When doing a paste with the middle mouse button, Insstart is set to
|
|
383 * where the paste started.
|
|
384 */
|
|
385 if (where_paste_started.lnum != 0)
|
|
386 Insstart = where_paste_started;
|
|
387 else
|
|
388 #endif
|
|
389 {
|
|
390 Insstart = curwin->w_cursor;
|
|
391 if (startln)
|
|
392 Insstart.col = 0;
|
|
393 }
|
1869
|
394 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
|
7
|
395 Insstart_blank_vcol = MAXCOL;
|
|
396 if (!did_ai)
|
|
397 ai_col = 0;
|
|
398
|
|
399 if (cmdchar != NUL && restart_edit == 0)
|
|
400 {
|
|
401 ResetRedobuff();
|
|
402 AppendNumberToRedobuff(count);
|
|
403 #ifdef FEAT_VREPLACE
|
|
404 if (cmdchar == 'V' || cmdchar == 'v')
|
|
405 {
|
|
406 /* "gR" or "gr" command */
|
|
407 AppendCharToRedobuff('g');
|
|
408 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
|
|
409 }
|
|
410 else
|
|
411 #endif
|
|
412 {
|
|
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> */
|
|
418 }
|
|
419 }
|
|
420
|
|
421 if (cmdchar == 'R')
|
|
422 {
|
|
423 #ifdef FEAT_FKMAP
|
|
424 if (p_fkmap && p_ri)
|
|
425 {
|
|
426 beep_flush();
|
|
427 EMSG(farsi_text_3); /* encoded in Farsi */
|
|
428 State = INSERT;
|
|
429 }
|
|
430 else
|
|
431 #endif
|
|
432 State = REPLACE;
|
|
433 }
|
|
434 #ifdef FEAT_VREPLACE
|
|
435 else if (cmdchar == 'V' || cmdchar == 'v')
|
|
436 {
|
|
437 State = VREPLACE;
|
|
438 replaceState = VREPLACE;
|
|
439 orig_line_count = curbuf->b_ml.ml_line_count;
|
|
440 vr_lines_changed = 1;
|
|
441 }
|
|
442 #endif
|
|
443 else
|
|
444 State = INSERT;
|
|
445
|
|
446 stop_insert_mode = FALSE;
|
|
447
|
|
448 /*
|
|
449 * Need to recompute the cursor position, it might move when the cursor is
|
|
450 * on a TAB or special character.
|
|
451 */
|
|
452 curs_columns(TRUE);
|
|
453
|
|
454 /*
|
|
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>.
|
|
459 */
|
|
460 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
|
|
461 State |= LANGMAP;
|
|
462 #ifdef USE_IM_CONTROL
|
|
463 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
|
|
464 #endif
|
|
465
|
|
466 #ifdef FEAT_MOUSE
|
|
467 setmouse();
|
|
468 #endif
|
|
469 #ifdef FEAT_CMDL_INFO
|
|
470 clear_showcmd();
|
|
471 #endif
|
|
472 #ifdef FEAT_RIGHTLEFT
|
|
473 /* there is no reverse replace mode */
|
|
474 revins_on = (State == INSERT && p_ri);
|
|
475 if (revins_on)
|
|
476 undisplay_dollar();
|
|
477 revins_chars = 0;
|
|
478 revins_legal = 0;
|
|
479 revins_scol = -1;
|
|
480 #endif
|
|
481
|
|
482 /*
|
|
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.
|
|
486 */
|
|
487 if (restart_edit != 0 && stuff_empty())
|
|
488 {
|
|
489 #ifdef FEAT_MOUSE
|
|
490 /*
|
|
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.
|
|
493 */
|
|
494 if (where_paste_started.lnum)
|
|
495 arrow_used = FALSE;
|
|
496 else
|
|
497 #endif
|
|
498 arrow_used = TRUE;
|
|
499 restart_edit = 0;
|
|
500
|
|
501 /*
|
|
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|".
|
|
507 */
|
|
508 validate_virtcol();
|
|
509 update_curswant();
|
230
|
510 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
|
7
|
511 || curwin->w_curswant > curwin->w_virtcol)
|
|
512 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
|
|
513 {
|
|
514 if (ptr[1] == NUL)
|
|
515 ++curwin->w_cursor.col;
|
|
516 #ifdef FEAT_MBYTE
|
|
517 else if (has_mbyte)
|
|
518 {
|
474
|
519 i = (*mb_ptr2len)(ptr);
|
7
|
520 if (ptr[i] == NUL)
|
|
521 curwin->w_cursor.col += i;
|
|
522 }
|
|
523 #endif
|
|
524 }
|
230
|
525 ins_at_eol = FALSE;
|
7
|
526 }
|
|
527 else
|
|
528 arrow_used = FALSE;
|
|
529
|
|
530 /* we are in insert mode now, don't need to start it anymore */
|
|
531 need_start_insertmode = FALSE;
|
|
532
|
|
533 /* Need to save the line for undo before inserting the first char. */
|
|
534 ins_need_undo = TRUE;
|
|
535
|
|
536 #ifdef FEAT_MOUSE
|
|
537 where_paste_started.lnum = 0;
|
|
538 #endif
|
|
539 #ifdef FEAT_CINDENT
|
|
540 can_cindent = TRUE;
|
|
541 #endif
|
|
542 #ifdef FEAT_FOLDING
|
|
543 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
|
|
544 * restarting. */
|
|
545 if (!p_im && did_restart_edit == 0)
|
|
546 foldOpenCursor();
|
|
547 #endif
|
|
548
|
|
549 /*
|
|
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.
|
|
553 */
|
|
554 i = 0;
|
644
|
555 if (p_smd && msg_silent == 0)
|
7
|
556 i = showmode();
|
|
557
|
|
558 if (!p_im && did_restart_edit == 0)
|
1473
|
559 change_warning(i == 0 ? 0 : i + 1);
|
7
|
560
|
|
561 #ifdef CURSOR_SHAPE
|
|
562 ui_cursor_shape(); /* may show different cursor shape */
|
|
563 #endif
|
|
564 #ifdef FEAT_DIGRAPHS
|
|
565 do_digraph(-1); /* clear digraphs */
|
|
566 #endif
|
|
567
|
603
|
568 /*
|
|
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.
|
|
571 */
|
7
|
572 ptr = get_inserted();
|
|
573 if (ptr == NULL)
|
|
574 new_insert_skip = 0;
|
|
575 else
|
|
576 {
|
|
577 new_insert_skip = (int)STRLEN(ptr);
|
|
578 vim_free(ptr);
|
|
579 }
|
|
580
|
|
581 old_indent = 0;
|
|
582
|
|
583 /*
|
|
584 * Main loop in Insert mode: repeat until Insert mode is left.
|
|
585 */
|
|
586 for (;;)
|
|
587 {
|
|
588 #ifdef FEAT_RIGHTLEFT
|
|
589 if (!revins_legal)
|
|
590 revins_scol = -1; /* reset on illegal motions */
|
|
591 else
|
|
592 revins_legal = 0;
|
|
593 #endif
|
|
594 if (arrow_used) /* don't repeat insert when arrow key used */
|
|
595 count = 0;
|
|
596
|
|
597 if (stop_insert_mode)
|
|
598 {
|
|
599 /* ":stopinsert" used or 'insertmode' reset */
|
|
600 count = 0;
|
|
601 goto doESCkey;
|
|
602 }
|
|
603
|
|
604 /* set curwin->w_curswant for next K_DOWN or K_UP */
|
|
605 if (!arrow_used)
|
|
606 curwin->w_set_curswant = TRUE;
|
|
607
|
|
608 /* If there is no typeahead may check for timestamps (e.g., for when a
|
|
609 * menu invoked a shell command). */
|
|
610 if (stuff_empty())
|
|
611 {
|
|
612 did_check_timestamps = FALSE;
|
|
613 if (need_check_timestamps)
|
|
614 check_timestamps(FALSE);
|
|
615 }
|
|
616
|
|
617 /*
|
|
618 * When emsg() was called msg_scroll will have been set.
|
|
619 */
|
|
620 msg_scroll = FALSE;
|
|
621
|
|
622 #ifdef FEAT_GUI
|
|
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
|
|
625 * autocommand. */
|
|
626 if (need_mouse_correct)
|
|
627 gui_mouse_correct();
|
|
628 #endif
|
|
629
|
|
630 #ifdef FEAT_FOLDING
|
|
631 /* Open fold at the cursor line, according to 'foldopen'. */
|
|
632 if (fdo_flags & FDO_INSERT)
|
|
633 foldOpenCursor();
|
|
634 /* Close folds where the cursor isn't, according to 'foldclose' */
|
|
635 if (!char_avail())
|
|
636 foldCheckClose();
|
|
637 #endif
|
|
638
|
|
639 /*
|
|
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
|
|
642 * redraw.
|
|
643 * This is detected when the cursor column is smaller after inserting
|
|
644 * something.
|
|
645 * Don't do this when the topline changed already, it has
|
|
646 * already been adjusted (by insertchar() calling open_line())).
|
|
647 */
|
|
648 if (curbuf->b_mod_set
|
|
649 && curwin->w_p_wrap
|
|
650 && !did_backspace
|
|
651 && curwin->w_topline == old_topline
|
|
652 #ifdef FEAT_DIFF
|
|
653 && curwin->w_topfill == old_topfill
|
|
654 #endif
|
|
655 )
|
|
656 {
|
|
657 mincol = curwin->w_wcol;
|
|
658 validate_cursor_col();
|
|
659
|
1869
|
660 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
|
7
|
661 && curwin->w_wrow == W_WINROW(curwin)
|
|
662 + curwin->w_height - 1 - p_so
|
|
663 && (curwin->w_cursor.lnum != curwin->w_topline
|
|
664 #ifdef FEAT_DIFF
|
|
665 || curwin->w_topfill > 0
|
|
666 #endif
|
|
667 ))
|
|
668 {
|
|
669 #ifdef FEAT_DIFF
|
|
670 if (curwin->w_topfill > 0)
|
|
671 --curwin->w_topfill;
|
|
672 else
|
|
673 #endif
|
|
674 #ifdef FEAT_FOLDING
|
|
675 if (hasFolding(curwin->w_topline, NULL, &old_topline))
|
|
676 set_topline(curwin, old_topline + 1);
|
|
677 else
|
|
678 #endif
|
|
679 set_topline(curwin, curwin->w_topline + 1);
|
|
680 }
|
|
681 }
|
|
682
|
|
683 /* May need to adjust w_topline to show the cursor. */
|
|
684 update_topline();
|
|
685
|
|
686 did_backspace = FALSE;
|
|
687
|
|
688 validate_cursor(); /* may set must_redraw */
|
|
689
|
|
690 /*
|
|
691 * Redraw the display when no characters are waiting.
|
|
692 * Also shows mode, ruler and positions cursor.
|
|
693 */
|
661
|
694 ins_redraw(TRUE);
|
7
|
695
|
|
696 #ifdef FEAT_SCROLLBIND
|
|
697 if (curwin->w_p_scb)
|
|
698 do_check_scrollbind(TRUE);
|
|
699 #endif
|
|
700
|
|
701 update_curswant();
|
|
702 old_topline = curwin->w_topline;
|
|
703 #ifdef FEAT_DIFF
|
|
704 old_topfill = curwin->w_topfill;
|
|
705 #endif
|
|
706
|
|
707 #ifdef USE_ON_FLY_SCROLL
|
|
708 dont_scroll = FALSE; /* allow scrolling here */
|
|
709 #endif
|
|
710
|
|
711 /*
|
1526
|
712 * Get a character for Insert mode. Ignore K_IGNORE.
|
7
|
713 */
|
|
714 lastc = c; /* remember previous char for CTRL-D */
|
1526
|
715 do
|
|
716 {
|
|
717 c = safe_vgetc();
|
|
718 } while (c == K_IGNORE);
|
7
|
719
|
978
|
720 #ifdef FEAT_AUTOCMD
|
|
721 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
|
|
722 did_cursorhold = TRUE;
|
|
723 #endif
|
|
724
|
7
|
725 #ifdef FEAT_RIGHTLEFT
|
|
726 if (p_hkmap && KeyTyped)
|
|
727 c = hkmap(c); /* Hebrew mode mapping */
|
|
728 #endif
|
|
729 #ifdef FEAT_FKMAP
|
|
730 if (p_fkmap && KeyTyped)
|
|
731 c = fkmap(c); /* Farsi mode mapping */
|
|
732 #endif
|
|
733
|
|
734 #ifdef FEAT_INS_EXPAND
|
659
|
735 /*
|
|
736 * Special handling of keys while the popup menu is visible or wanted
|
897
|
737 * and the cursor is still in the completed word. Only when there is
|
|
738 * a match, skip this when no matches were found.
|
659
|
739 */
|
897
|
740 if (compl_started
|
|
741 && pum_wanted()
|
|
742 && curwin->w_cursor.col >= compl_col
|
|
743 && (compl_shown_match == NULL
|
|
744 || compl_shown_match != compl_shown_match->cp_next))
|
659
|
745 {
|
|
746 /* BS: Delete one character from "compl_leader". */
|
|
747 if ((c == K_BS || c == Ctrl_H)
|
836
|
748 && curwin->w_cursor.col > compl_col
|
|
749 && (c = ins_compl_bs()) == NUL)
|
657
|
750 continue;
|
|
751
|
659
|
752 /* When no match was selected or it was edited. */
|
|
753 if (!compl_used_match)
|
657
|
754 {
|
659
|
755 /* CTRL-L: Add one character from the current match to
|
836
|
756 * "compl_leader". Except when at the original match and
|
|
757 * there is nothing to add, CTRL-L works like CTRL-P then. */
|
|
758 if (c == Ctrl_L
|
|
759 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
|
|
760 || STRLEN(compl_shown_match->cp_str)
|
|
761 > curwin->w_cursor.col - compl_col))
|
659
|
762 {
|
|
763 ins_compl_addfrommatch();
|
|
764 continue;
|
|
765 }
|
|
766
|
1430
|
767 /* A non-white character that fits in with the current
|
|
768 * completion: Add to "compl_leader". */
|
|
769 if (ins_compl_accept_char(c))
|
659
|
770 {
|
|
771 ins_compl_addleader(c);
|
|
772 continue;
|
|
773 }
|
665
|
774
|
887
|
775 /* Pressing CTRL-Y selects the current match. When
|
825
|
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)))
|
665
|
779 {
|
|
780 ins_compl_delete();
|
|
781 ins_compl_insert();
|
|
782 }
|
657
|
783 }
|
|
784 }
|
|
785
|
7
|
786 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
|
|
787 * it does fix up the text when finishing completion. */
|
665
|
788 compl_get_longest = FALSE;
|
1526
|
789 if (ins_compl_prep(c))
|
657
|
790 continue;
|
7
|
791 #endif
|
|
792
|
477
|
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. */
|
7
|
796 if (c == Ctrl_BSL)
|
|
797 {
|
|
798 /* may need to redraw when no more chars available now */
|
661
|
799 ins_redraw(FALSE);
|
7
|
800 ++no_mapping;
|
|
801 ++allow_keys;
|
1389
|
802 c = plain_vgetc();
|
7
|
803 --no_mapping;
|
|
804 --allow_keys;
|
477
|
805 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
|
7
|
806 {
|
477
|
807 /* it's something else */
|
7
|
808 vungetc(c);
|
|
809 c = Ctrl_BSL;
|
|
810 }
|
|
811 else if (c == Ctrl_G && p_im)
|
|
812 continue;
|
|
813 else
|
|
814 {
|
477
|
815 if (c == Ctrl_O)
|
|
816 {
|
|
817 ins_ctrl_o();
|
|
818 ins_at_eol = FALSE; /* cursor keeps its column */
|
|
819 nomove = TRUE;
|
|
820 }
|
7
|
821 count = 0;
|
|
822 goto doESCkey;
|
|
823 }
|
|
824 }
|
|
825
|
|
826 #ifdef FEAT_DIGRAPHS
|
|
827 c = do_digraph(c);
|
|
828 #endif
|
|
829
|
|
830 #ifdef FEAT_INS_EXPAND
|
|
831 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
|
|
832 goto docomplete;
|
|
833 #endif
|
|
834 if (c == Ctrl_V || c == Ctrl_Q)
|
|
835 {
|
|
836 ins_ctrl_v();
|
|
837 c = Ctrl_V; /* pretend CTRL-V is last typed character */
|
|
838 continue;
|
|
839 }
|
|
840
|
|
841 #ifdef FEAT_CINDENT
|
|
842 if (cindent_on()
|
|
843 # ifdef FEAT_INS_EXPAND
|
|
844 && ctrl_x_mode == 0
|
|
845 # endif
|
|
846 )
|
|
847 {
|
|
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))
|
|
854 goto force_cindent;
|
|
855 if (can_cindent && in_cinkeys(c, '*', line_is_white)
|
|
856 && stop_arrow() == OK)
|
|
857 do_c_expr_indent();
|
|
858 }
|
|
859 #endif
|
|
860
|
|
861 #ifdef FEAT_RIGHTLEFT
|
|
862 if (curwin->w_p_rl)
|
|
863 switch (c)
|
|
864 {
|
|
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;
|
|
871 }
|
|
872 #endif
|
|
873
|
|
874 #ifdef FEAT_VISUAL
|
|
875 /*
|
|
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
|
|
878 * characters.
|
|
879 */
|
|
880 if (ins_start_select(c))
|
|
881 continue;
|
|
882 #endif
|
|
883
|
|
884 /*
|
|
885 * The big switch to handle a character in insert mode.
|
|
886 */
|
|
887 switch (c)
|
|
888 {
|
449
|
889 case ESC: /* End input mode */
|
7
|
890 if (echeck_abbr(ESC + ABBR_OFF))
|
|
891 break;
|
|
892 /*FALLTHROUGH*/
|
|
893
|
449
|
894 case Ctrl_C: /* End input mode */
|
7
|
895 #ifdef FEAT_CMDWIN
|
|
896 if (c == Ctrl_C && cmdwin_type != 0)
|
|
897 {
|
|
898 /* Close the cmdline window. */
|
|
899 cmdwin_result = K_IGNORE;
|
|
900 got_int = FALSE; /* don't stop executing autocommands et al. */
|
929
|
901 nomove = TRUE;
|
7
|
902 goto doESCkey;
|
|
903 }
|
|
904 #endif
|
|
905
|
|
906 #ifdef UNIX
|
|
907 do_intr:
|
|
908 #endif
|
|
909 /* when 'insertmode' set, and not halfway a mapping, don't leave
|
|
910 * Insert mode */
|
|
911 if (goto_im())
|
|
912 {
|
|
913 if (got_int)
|
|
914 {
|
|
915 (void)vgetc(); /* flush all buffers */
|
|
916 got_int = FALSE;
|
|
917 }
|
|
918 else
|
|
919 vim_beep();
|
|
920 break;
|
|
921 }
|
|
922 doESCkey:
|
|
923 /*
|
|
924 * This is the ONLY return from edit()!
|
|
925 */
|
|
926 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
|
|
927 * still puts the cursor back after the inserted text. */
|
230
|
928 if (ins_at_eol && gchar_cursor() == NUL)
|
7
|
929 o_lnum = curwin->w_cursor.lnum;
|
|
930
|
477
|
931 if (ins_esc(&count, cmdchar, nomove))
|
11
|
932 {
|
|
933 #ifdef FEAT_AUTOCMD
|
|
934 if (cmdchar != 'r' && cmdchar != 'v')
|
|
935 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
|
|
936 FALSE, curbuf);
|
993
|
937 did_cursorhold = FALSE;
|
11
|
938 #endif
|
7
|
939 return (c == Ctrl_O);
|
11
|
940 }
|
7
|
941 continue;
|
|
942
|
449
|
943 case Ctrl_Z: /* suspend when 'insertmode' set */
|
|
944 if (!p_im)
|
|
945 goto normalchar; /* insert CTRL-Z as normal char */
|
|
946 stuffReadbuff((char_u *)":st\r");
|
|
947 c = Ctrl_O;
|
|
948 /*FALLTHROUGH*/
|
|
949
|
|
950 case Ctrl_O: /* execute one command */
|
502
|
951 #ifdef FEAT_COMPL_FUNC
|
523
|
952 if (ctrl_x_mode == CTRL_X_OMNI)
|
449
|
953 goto docomplete;
|
|
954 #endif
|
|
955 if (echeck_abbr(Ctrl_O + ABBR_OFF))
|
|
956 break;
|
|
957 ins_ctrl_o();
|
853
|
958
|
|
959 #ifdef FEAT_VIRTUALEDIT
|
|
960 /* don't move the cursor left when 'virtualedit' has "onemore". */
|
|
961 if (ve_flags & VE_ONEMORE)
|
|
962 {
|
|
963 ins_at_eol = FALSE;
|
|
964 nomove = TRUE;
|
|
965 }
|
|
966 #endif
|
449
|
967 count = 0;
|
|
968 goto doESCkey;
|
|
969
|
464
|
970 case K_INS: /* toggle insert/replace mode */
|
|
971 case K_KINS:
|
|
972 ins_insert(replaceState);
|
|
973 break;
|
|
974
|
|
975 case K_SELECT: /* end of Select mode mapping - ignore */
|
|
976 break;
|
|
977
|
449
|
978 #ifdef FEAT_SNIFF
|
|
979 case K_SNIFF: /* Sniff command received */
|
|
980 stuffcharReadbuff(K_SNIFF);
|
|
981 goto doESCkey;
|
|
982 #endif
|
|
983
|
|
984 case K_HELP: /* Help key works like <ESC> <Help> */
|
|
985 case K_F1:
|
|
986 case K_XF1:
|
|
987 stuffcharReadbuff(K_HELP);
|
|
988 if (p_im)
|
|
989 need_start_insertmode = TRUE;
|
|
990 goto doESCkey;
|
|
991
|
|
992 #ifdef FEAT_NETBEANS_INTG
|
|
993 case K_F21: /* NetBeans command */
|
|
994 ++no_mapping; /* don't map the next key hits */
|
1389
|
995 i = plain_vgetc();
|
449
|
996 --no_mapping;
|
|
997 netbeans_keycommand(i);
|
|
998 break;
|
|
999 #endif
|
|
1000
|
|
1001 case K_ZERO: /* Insert the previously inserted text. */
|
7
|
1002 case NUL:
|
|
1003 case Ctrl_A:
|
449
|
1004 /* For ^@ the trailing ESC will end the insert, unless there is an
|
|
1005 * error. */
|
7
|
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;
|
|
1010 break;
|
|
1011
|
449
|
1012 case Ctrl_R: /* insert the contents of a register */
|
7
|
1013 ins_reg();
|
|
1014 auto_format(FALSE, TRUE);
|
|
1015 inserted_space = FALSE;
|
|
1016 break;
|
|
1017
|
449
|
1018 case Ctrl_G: /* commands starting with CTRL-G */
|
7
|
1019 ins_ctrl_g();
|
|
1020 break;
|
|
1021
|
449
|
1022 case Ctrl_HAT: /* switch input mode and/or langmap */
|
|
1023 ins_ctrl_hat();
|
7
|
1024 break;
|
|
1025
|
|
1026 #ifdef FEAT_RIGHTLEFT
|
449
|
1027 case Ctrl__: /* switch between languages */
|
7
|
1028 if (!p_ari)
|
|
1029 goto normalchar;
|
|
1030 ins_ctrl_();
|
|
1031 break;
|
|
1032 #endif
|
|
1033
|
449
|
1034 case Ctrl_D: /* Make indent one shiftwidth smaller. */
|
7
|
1035 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
|
|
1036 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
|
|
1037 goto docomplete;
|
|
1038 #endif
|
|
1039 /* FALLTHROUGH */
|
|
1040
|
449
|
1041 case Ctrl_T: /* Make indent one shiftwidth greater. */
|
7
|
1042 # ifdef FEAT_INS_EXPAND
|
|
1043 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
|
|
1044 {
|
449
|
1045 if (has_compl_option(FALSE))
|
|
1046 goto docomplete;
|
|
1047 break;
|
7
|
1048 }
|
|
1049 # endif
|
|
1050 ins_shift(c, lastc);
|
|
1051 auto_format(FALSE, TRUE);
|
|
1052 inserted_space = FALSE;
|
|
1053 break;
|
|
1054
|
449
|
1055 case K_DEL: /* delete character under the cursor */
|
7
|
1056 case K_KDEL:
|
|
1057 ins_del();
|
|
1058 auto_format(FALSE, TRUE);
|
|
1059 break;
|
|
1060
|
449
|
1061 case K_BS: /* delete character before the cursor */
|
7
|
1062 case Ctrl_H:
|
|
1063 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
|
|
1064 auto_format(FALSE, TRUE);
|
|
1065 break;
|
|
1066
|
449
|
1067 case Ctrl_W: /* delete word before the cursor */
|
7
|
1068 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
|
|
1069 auto_format(FALSE, TRUE);
|
|
1070 break;
|
|
1071
|
449
|
1072 case Ctrl_U: /* delete all inserted text in current line */
|
12
|
1073 # ifdef FEAT_COMPL_FUNC
|
|
1074 /* CTRL-X CTRL-U completes with 'completefunc'. */
|
449
|
1075 if (ctrl_x_mode == CTRL_X_FUNCTION)
|
12
|
1076 goto docomplete;
|
|
1077 # endif
|
7
|
1078 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
|
|
1079 auto_format(FALSE, TRUE);
|
|
1080 inserted_space = FALSE;
|
|
1081 break;
|
|
1082
|
|
1083 #ifdef FEAT_MOUSE
|
449
|
1084 case K_LEFTMOUSE: /* mouse keys */
|
7
|
1085 case K_LEFTMOUSE_NM:
|
|
1086 case K_LEFTDRAG:
|
|
1087 case K_LEFTRELEASE:
|
|
1088 case K_LEFTRELEASE_NM:
|
|
1089 case K_MIDDLEMOUSE:
|
|
1090 case K_MIDDLEDRAG:
|
|
1091 case K_MIDDLERELEASE:
|
|
1092 case K_RIGHTMOUSE:
|
|
1093 case K_RIGHTDRAG:
|
|
1094 case K_RIGHTRELEASE:
|
|
1095 case K_X1MOUSE:
|
|
1096 case K_X1DRAG:
|
|
1097 case K_X1RELEASE:
|
|
1098 case K_X2MOUSE:
|
|
1099 case K_X2DRAG:
|
|
1100 case K_X2RELEASE:
|
|
1101 ins_mouse(c);
|
|
1102 break;
|
|
1103
|
449
|
1104 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
|
7
|
1105 ins_mousescroll(FALSE);
|
|
1106 break;
|
|
1107
|
449
|
1108 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
|
7
|
1109 ins_mousescroll(TRUE);
|
|
1110 break;
|
|
1111 #endif
|
692
|
1112 #ifdef FEAT_GUI_TABLINE
|
|
1113 case K_TABLINE:
|
|
1114 case K_TABMENU:
|
|
1115 ins_tabline(c);
|
|
1116 break;
|
|
1117 #endif
|
7
|
1118
|
449
|
1119 case K_IGNORE: /* Something mapped to nothing */
|
7
|
1120 break;
|
|
1121
|
661
|
1122 #ifdef FEAT_AUTOCMD
|
|
1123 case K_CURSORHOLD: /* Didn't type something for a while. */
|
|
1124 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
|
|
1125 did_cursorhold = TRUE;
|
|
1126 break;
|
|
1127 #endif
|
|
1128
|
625
|
1129 #ifdef FEAT_GUI_W32
|
|
1130 /* On Win32 ignore <M-F4>, we get it when closing the window was
|
|
1131 * cancelled. */
|
|
1132 case K_F4:
|
|
1133 if (mod_mask != MOD_MASK_ALT)
|
|
1134 goto normalchar;
|
|
1135 break;
|
|
1136 #endif
|
|
1137
|
7
|
1138 #ifdef FEAT_GUI
|
|
1139 case K_VER_SCROLLBAR:
|
|
1140 ins_scroll();
|
|
1141 break;
|
|
1142
|
|
1143 case K_HOR_SCROLLBAR:
|
|
1144 ins_horscroll();
|
|
1145 break;
|
|
1146 #endif
|
|
1147
|
449
|
1148 case K_HOME: /* <Home> */
|
7
|
1149 case K_KHOME:
|
|
1150 case K_S_HOME:
|
|
1151 case K_C_HOME:
|
|
1152 ins_home(c);
|
|
1153 break;
|
|
1154
|
449
|
1155 case K_END: /* <End> */
|
7
|
1156 case K_KEND:
|
|
1157 case K_S_END:
|
|
1158 case K_C_END:
|
|
1159 ins_end(c);
|
|
1160 break;
|
|
1161
|
449
|
1162 case K_LEFT: /* <Left> */
|
180
|
1163 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
|
|
1164 ins_s_left();
|
|
1165 else
|
|
1166 ins_left();
|
7
|
1167 break;
|
|
1168
|
449
|
1169 case K_S_LEFT: /* <S-Left> */
|
7
|
1170 case K_C_LEFT:
|
|
1171 ins_s_left();
|
|
1172 break;
|
|
1173
|
449
|
1174 case K_RIGHT: /* <Right> */
|
180
|
1175 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
|
|
1176 ins_s_right();
|
|
1177 else
|
|
1178 ins_right();
|
7
|
1179 break;
|
|
1180
|
449
|
1181 case K_S_RIGHT: /* <S-Right> */
|
7
|
1182 case K_C_RIGHT:
|
|
1183 ins_s_right();
|
|
1184 break;
|
|
1185
|
449
|
1186 case K_UP: /* <Up> */
|
665
|
1187 #ifdef FEAT_INS_EXPAND
|
|
1188 if (pum_visible())
|
|
1189 goto docomplete;
|
|
1190 #endif
|
180
|
1191 if (mod_mask & MOD_MASK_SHIFT)
|
|
1192 ins_pageup();
|
|
1193 else
|
|
1194 ins_up(FALSE);
|
7
|
1195 break;
|
|
1196
|
449
|
1197 case K_S_UP: /* <S-Up> */
|
7
|
1198 case K_PAGEUP:
|
|
1199 case K_KPAGEUP:
|
613
|
1200 #ifdef FEAT_INS_EXPAND
|
610
|
1201 if (pum_visible())
|
|
1202 goto docomplete;
|
613
|
1203 #endif
|
7
|
1204 ins_pageup();
|
|
1205 break;
|
|
1206
|
449
|
1207 case K_DOWN: /* <Down> */
|
665
|
1208 #ifdef FEAT_INS_EXPAND
|
|
1209 if (pum_visible())
|
|
1210 goto docomplete;
|
|
1211 #endif
|
180
|
1212 if (mod_mask & MOD_MASK_SHIFT)
|
|
1213 ins_pagedown();
|
|
1214 else
|
|
1215 ins_down(FALSE);
|
7
|
1216 break;
|
|
1217
|
449
|
1218 case K_S_DOWN: /* <S-Down> */
|
7
|
1219 case K_PAGEDOWN:
|
|
1220 case K_KPAGEDOWN:
|
613
|
1221 #ifdef FEAT_INS_EXPAND
|
610
|
1222 if (pum_visible())
|
|
1223 goto docomplete;
|
613
|
1224 #endif
|
7
|
1225 ins_pagedown();
|
|
1226 break;
|
|
1227
|
|
1228 #ifdef FEAT_DND
|
449
|
1229 case K_DROP: /* drag-n-drop event */
|
7
|
1230 ins_drop();
|
|
1231 break;
|
|
1232 #endif
|
|
1233
|
449
|
1234 case K_S_TAB: /* When not mapped, use like a normal TAB */
|
7
|
1235 c = TAB;
|
|
1236 /* FALLTHROUGH */
|
|
1237
|
449
|
1238 case TAB: /* TAB or Complete patterns along path */
|
7
|
1239 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
|
|
1240 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
|
|
1241 goto docomplete;
|
|
1242 #endif
|
|
1243 inserted_space = FALSE;
|
|
1244 if (ins_tab())
|
|
1245 goto normalchar; /* insert TAB as a normal char */
|
|
1246 auto_format(FALSE, TRUE);
|
|
1247 break;
|
|
1248
|
449
|
1249 case K_KENTER: /* <Enter> */
|
7
|
1250 c = CAR;
|
|
1251 /* FALLTHROUGH */
|
|
1252 case CAR:
|
|
1253 case NL:
|
|
1254 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
|
|
1255 /* In a quickfix window a <CR> jumps to the error under the
|
|
1256 * cursor. */
|
|
1257 if (bt_quickfix(curbuf) && c == CAR)
|
|
1258 {
|
644
|
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");
|
7
|
1263 break;
|
|
1264 }
|
|
1265 #endif
|
|
1266 #ifdef FEAT_CMDWIN
|
|
1267 if (cmdwin_type != 0)
|
|
1268 {
|
|
1269 /* Execute the command in the cmdline window. */
|
|
1270 cmdwin_result = CAR;
|
|
1271 goto doESCkey;
|
|
1272 }
|
|
1273 #endif
|
|
1274 if (ins_eol(c) && !p_im)
|
|
1275 goto doESCkey; /* out of memory */
|
|
1276 auto_format(FALSE, FALSE);
|
|
1277 inserted_space = FALSE;
|
|
1278 break;
|
|
1279
|
|
1280 #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
|
449
|
1281 case Ctrl_K: /* digraph or keyword completion */
|
7
|
1282 # ifdef FEAT_INS_EXPAND
|
|
1283 if (ctrl_x_mode == CTRL_X_DICTIONARY)
|
|
1284 {
|
449
|
1285 if (has_compl_option(TRUE))
|
|
1286 goto docomplete;
|
|
1287 break;
|
7
|
1288 }
|
|
1289 # endif
|
|
1290 # ifdef FEAT_DIGRAPHS
|
|
1291 c = ins_digraph();
|
|
1292 if (c == NUL)
|
|
1293 break;
|
|
1294 # endif
|
|
1295 goto normalchar;
|
449
|
1296 #endif
|
7
|
1297
|
|
1298 #ifdef FEAT_INS_EXPAND
|
464
|
1299 case Ctrl_X: /* Enter CTRL-X mode */
|
|
1300 ins_ctrl_x();
|
|
1301 break;
|
|
1302
|
449
|
1303 case Ctrl_RSB: /* Tag name completion after ^X */
|
7
|
1304 if (ctrl_x_mode != CTRL_X_TAGS)
|
|
1305 goto normalchar;
|
|
1306 goto docomplete;
|
|
1307
|
449
|
1308 case Ctrl_F: /* File name completion after ^X */
|
7
|
1309 if (ctrl_x_mode != CTRL_X_FILES)
|
|
1310 goto normalchar;
|
|
1311 goto docomplete;
|
477
|
1312
|
|
1313 case 's': /* Spelling completion after ^X */
|
|
1314 case Ctrl_S:
|
|
1315 if (ctrl_x_mode != CTRL_X_SPELL)
|
|
1316 goto normalchar;
|
|
1317 goto docomplete;
|
7
|
1318 #endif
|
|
1319
|
449
|
1320 case Ctrl_L: /* Whole line completion after ^X */
|
7
|
1321 #ifdef FEAT_INS_EXPAND
|
|
1322 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
|
|
1323 #endif
|
|
1324 {
|
|
1325 /* CTRL-L with 'insertmode' set: Leave Insert mode */
|
|
1326 if (p_im)
|
|
1327 {
|
|
1328 if (echeck_abbr(Ctrl_L + ABBR_OFF))
|
|
1329 break;
|
|
1330 goto doESCkey;
|
|
1331 }
|
|
1332 goto normalchar;
|
|
1333 }
|
|
1334 #ifdef FEAT_INS_EXPAND
|
|
1335 /* FALLTHROUGH */
|
|
1336
|
449
|
1337 case Ctrl_P: /* Do previous/next pattern completion */
|
7
|
1338 case Ctrl_N:
|
|
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
|
|
1342 && ctrl_x_mode != 0
|
449
|
1343 && !(compl_cont_status & CONT_LOCAL))
|
7
|
1344 goto normalchar;
|
|
1345
|
|
1346 docomplete:
|
|
1347 if (ins_complete(c) == FAIL)
|
449
|
1348 compl_cont_status = 0;
|
7
|
1349 break;
|
|
1350 #endif /* FEAT_INS_EXPAND */
|
|
1351
|
449
|
1352 case Ctrl_Y: /* copy from previous line or scroll down */
|
|
1353 case Ctrl_E: /* copy from next line or scroll up */
|
|
1354 c = ins_ctrl_ey(c);
|
7
|
1355 break;
|
|
1356
|
|
1357 default:
|
|
1358 #ifdef UNIX
|
|
1359 if (c == intr_char) /* special interrupt char */
|
|
1360 goto do_intr;
|
|
1361 #endif
|
|
1362
|
|
1363 /*
|
|
1364 * Insert a nomal character.
|
|
1365 */
|
|
1366 normalchar:
|
|
1367 #ifdef FEAT_SMARTINDENT
|
|
1368 /* Try to perform smart-indenting. */
|
|
1369 ins_try_si(c);
|
|
1370 #endif
|
|
1371
|
|
1372 if (c == ' ')
|
|
1373 {
|
|
1374 inserted_space = TRUE;
|
|
1375 #ifdef FEAT_CINDENT
|
|
1376 if (inindent(0))
|
|
1377 can_cindent = FALSE;
|
|
1378 #endif
|
|
1379 if (Insstart_blank_vcol == MAXCOL
|
|
1380 && curwin->w_cursor.lnum == Insstart.lnum)
|
|
1381 Insstart_blank_vcol = get_nolist_virtcol();
|
|
1382 }
|
|
1383
|
|
1384 if (vim_iswordc(c) || !echeck_abbr(
|
|
1385 #ifdef FEAT_MBYTE
|
|
1386 /* Add ABBR_OFF for characters above 0x100, this is
|
|
1387 * what check_abbr() expects. */
|
|
1388 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
|
|
1389 #endif
|
|
1390 c))
|
|
1391 {
|
|
1392 insert_special(c, FALSE, FALSE);
|
|
1393 #ifdef FEAT_RIGHTLEFT
|
|
1394 revins_legal++;
|
|
1395 revins_chars++;
|
|
1396 #endif
|
|
1397 }
|
|
1398
|
|
1399 auto_format(FALSE, TRUE);
|
|
1400
|
|
1401 #ifdef FEAT_FOLDING
|
|
1402 /* When inserting a character the cursor line must never be in a
|
|
1403 * closed fold. */
|
|
1404 foldOpenCursor();
|
|
1405 #endif
|
|
1406 break;
|
|
1407 } /* end of switch (c) */
|
|
1408
|
978
|
1409 #ifdef FEAT_AUTOCMD
|
|
1410 /* If typed something may trigger CursorHoldI again. */
|
|
1411 if (c != K_CURSORHOLD)
|
|
1412 did_cursorhold = FALSE;
|
|
1413 #endif
|
|
1414
|
7
|
1415 /* If the cursor was moved we didn't just insert a space */
|
|
1416 if (arrow_used)
|
|
1417 inserted_space = FALSE;
|
|
1418
|
|
1419 #ifdef FEAT_CINDENT
|
|
1420 if (can_cindent && cindent_on()
|
|
1421 # ifdef FEAT_INS_EXPAND
|
|
1422 && ctrl_x_mode == 0
|
|
1423 # endif
|
|
1424 )
|
|
1425 {
|
|
1426 force_cindent:
|
|
1427 /*
|
|
1428 * Indent now if a key was typed that is in 'cinkeys'.
|
|
1429 */
|
|
1430 if (in_cinkeys(c, ' ', line_is_white))
|
|
1431 {
|
|
1432 if (stop_arrow() == OK)
|
|
1433 /* re-indent the current line */
|
|
1434 do_c_expr_indent();
|
|
1435 }
|
|
1436 }
|
|
1437 #endif /* FEAT_CINDENT */
|
|
1438
|
|
1439 } /* for (;;) */
|
|
1440 /* NOTREACHED */
|
|
1441 }
|
|
1442
|
|
1443 /*
|
|
1444 * Redraw for Insert mode.
|
|
1445 * This is postponed until getting the next character to make '$' in the 'cpo'
|
|
1446 * option work correctly.
|
|
1447 * Only redraw when there are no characters available. This speeds up
|
|
1448 * inserting sequences of characters (e.g., for CTRL-R).
|
|
1449 */
|
661
|
1450 /*ARGSUSED*/
|
7
|
1451 static void
|
661
|
1452 ins_redraw(ready)
|
|
1453 int ready; /* not busy with something */
|
7
|
1454 {
|
|
1455 if (!char_avail())
|
|
1456 {
|
661
|
1457 #ifdef FEAT_AUTOCMD
|
794
|
1458 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
|
|
1459 * visible, the command might delete it. */
|
661
|
1460 if (ready && has_cursormovedI()
|
794
|
1461 && !equalpos(last_cursormoved, curwin->w_cursor)
|
|
1462 # ifdef FEAT_INS_EXPAND
|
|
1463 && !pum_visible()
|
|
1464 # endif
|
|
1465 )
|
661
|
1466 {
|
1506
|
1467 # ifdef FEAT_SYN_HL
|
|
1468 /* Need to update the screen first, to make sure syntax
|
|
1469 * highlighting is correct after making a change (e.g., inserting
|
|
1470 * a "(". The autocommand may also require a redraw, so it's done
|
|
1471 * again below, unfortunately. */
|
|
1472 if (syntax_present(curbuf) && must_redraw)
|
|
1473 update_screen(0);
|
|
1474 # endif
|
661
|
1475 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
|
|
1476 last_cursormoved = curwin->w_cursor;
|
|
1477 }
|
|
1478 #endif
|
7
|
1479 if (must_redraw)
|
|
1480 update_screen(0);
|
|
1481 else if (clear_cmdline || redraw_cmdline)
|
|
1482 showmode(); /* clear cmdline and show mode */
|
|
1483 showruler(FALSE);
|
|
1484 setcursor();
|
|
1485 emsg_on_display = FALSE; /* may remove error message now */
|
|
1486 }
|
|
1487 }
|
|
1488
|
|
1489 /*
|
|
1490 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
|
|
1491 */
|
|
1492 static void
|
|
1493 ins_ctrl_v()
|
|
1494 {
|
|
1495 int c;
|
|
1496
|
|
1497 /* may need to redraw when no more chars available now */
|
661
|
1498 ins_redraw(FALSE);
|
7
|
1499
|
|
1500 if (redrawing() && !char_avail())
|
|
1501 edit_putchar('^', TRUE);
|
|
1502 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
|
|
1503
|
|
1504 #ifdef FEAT_CMDL_INFO
|
|
1505 add_to_showcmd_c(Ctrl_V);
|
|
1506 #endif
|
|
1507
|
|
1508 c = get_literal();
|
|
1509 #ifdef FEAT_CMDL_INFO
|
|
1510 clear_showcmd();
|
|
1511 #endif
|
|
1512 insert_special(c, FALSE, TRUE);
|
|
1513 #ifdef FEAT_RIGHTLEFT
|
|
1514 revins_chars++;
|
|
1515 revins_legal++;
|
|
1516 #endif
|
|
1517 }
|
|
1518
|
|
1519 /*
|
|
1520 * Put a character directly onto the screen. It's not stored in a buffer.
|
|
1521 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
|
|
1522 */
|
|
1523 static int pc_status;
|
|
1524 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */
|
|
1525 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
|
|
1526 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */
|
|
1527 #define PC_STATUS_SET 3 /* pc_bytes was filled */
|
|
1528 #ifdef FEAT_MBYTE
|
|
1529 static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
|
|
1530 #else
|
|
1531 static char_u pc_bytes[2]; /* saved bytes */
|
|
1532 #endif
|
|
1533 static int pc_attr;
|
|
1534 static int pc_row;
|
|
1535 static int pc_col;
|
|
1536
|
|
1537 void
|
|
1538 edit_putchar(c, highlight)
|
|
1539 int c;
|
|
1540 int highlight;
|
|
1541 {
|
|
1542 int attr;
|
|
1543
|
|
1544 if (ScreenLines != NULL)
|
|
1545 {
|
|
1546 update_topline(); /* just in case w_topline isn't valid */
|
|
1547 validate_cursor();
|
|
1548 if (highlight)
|
|
1549 attr = hl_attr(HLF_8);
|
|
1550 else
|
|
1551 attr = 0;
|
|
1552 pc_row = W_WINROW(curwin) + curwin->w_wrow;
|
|
1553 pc_col = W_WINCOL(curwin);
|
|
1554 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
|
|
1555 pc_status = PC_STATUS_UNSET;
|
|
1556 #endif
|
|
1557 #ifdef FEAT_RIGHTLEFT
|
|
1558 if (curwin->w_p_rl)
|
|
1559 {
|
|
1560 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
|
|
1561 # ifdef FEAT_MBYTE
|
|
1562 if (has_mbyte)
|
|
1563 {
|
|
1564 int fix_col = mb_fix_col(pc_col, pc_row);
|
|
1565
|
|
1566 if (fix_col != pc_col)
|
|
1567 {
|
|
1568 screen_putchar(' ', pc_row, fix_col, attr);
|
|
1569 --curwin->w_wcol;
|
|
1570 pc_status = PC_STATUS_RIGHT;
|
|
1571 }
|
|
1572 }
|
|
1573 # endif
|
|
1574 }
|
|
1575 else
|
|
1576 #endif
|
|
1577 {
|
|
1578 pc_col += curwin->w_wcol;
|
|
1579 #ifdef FEAT_MBYTE
|
|
1580 if (mb_lefthalve(pc_row, pc_col))
|
|
1581 pc_status = PC_STATUS_LEFT;
|
|
1582 #endif
|
|
1583 }
|
|
1584
|
|
1585 /* save the character to be able to put it back */
|
|
1586 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
|
|
1587 if (pc_status == PC_STATUS_UNSET)
|
|
1588 #endif
|
|
1589 {
|
|
1590 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
|
|
1591 pc_status = PC_STATUS_SET;
|
|
1592 }
|
|
1593 screen_putchar(c, pc_row, pc_col, attr);
|
|
1594 }
|
|
1595 }
|
|
1596
|
|
1597 /*
|
|
1598 * Undo the previous edit_putchar().
|
|
1599 */
|
|
1600 void
|
|
1601 edit_unputchar()
|
|
1602 {
|
|
1603 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
|
|
1604 {
|
|
1605 #if defined(FEAT_MBYTE)
|
|
1606 if (pc_status == PC_STATUS_RIGHT)
|
|
1607 ++curwin->w_wcol;
|
|
1608 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
|
|
1609 redrawWinline(curwin->w_cursor.lnum, FALSE);
|
|
1610 else
|
|
1611 #endif
|
|
1612 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
|
|
1613 }
|
|
1614 }
|
|
1615
|
|
1616 /*
|
|
1617 * Called when p_dollar is set: display a '$' at the end of the changed text
|
|
1618 * Only works when cursor is in the line that changes.
|
|
1619 */
|
|
1620 void
|
|
1621 display_dollar(col)
|
|
1622 colnr_T col;
|
|
1623 {
|
|
1624 colnr_T save_col;
|
|
1625
|
|
1626 if (!redrawing())
|
|
1627 return;
|
|
1628
|
|
1629 cursor_off();
|
|
1630 save_col = curwin->w_cursor.col;
|
|
1631 curwin->w_cursor.col = col;
|
|
1632 #ifdef FEAT_MBYTE
|
|
1633 if (has_mbyte)
|
|
1634 {
|
|
1635 char_u *p;
|
|
1636
|
|
1637 /* If on the last byte of a multi-byte move to the first byte. */
|
|
1638 p = ml_get_curline();
|
|
1639 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
|
|
1640 }
|
|
1641 #endif
|
|
1642 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
|
|
1643 if (curwin->w_wcol < W_WIDTH(curwin))
|
|
1644 {
|
|
1645 edit_putchar('$', FALSE);
|
|
1646 dollar_vcol = curwin->w_virtcol;
|
|
1647 }
|
|
1648 curwin->w_cursor.col = save_col;
|
|
1649 }
|
|
1650
|
|
1651 /*
|
|
1652 * Call this function before moving the cursor from the normal insert position
|
|
1653 * in insert mode.
|
|
1654 */
|
|
1655 static void
|
|
1656 undisplay_dollar()
|
|
1657 {
|
|
1658 if (dollar_vcol)
|
|
1659 {
|
|
1660 dollar_vcol = 0;
|
|
1661 redrawWinline(curwin->w_cursor.lnum, FALSE);
|
|
1662 }
|
|
1663 }
|
|
1664
|
|
1665 /*
|
|
1666 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
|
|
1667 * Keep the cursor on the same character.
|
|
1668 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
|
|
1669 * type == INDENT_DEC decrease indent (for CTRL-D)
|
|
1670 * type == INDENT_SET set indent to "amount"
|
|
1671 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
|
|
1672 */
|
|
1673 void
|
1516
|
1674 change_indent(type, amount, round, replaced, call_changed_bytes)
|
7
|
1675 int type;
|
|
1676 int amount;
|
|
1677 int round;
|
|
1678 int replaced; /* replaced character, put on replace stack */
|
1516
|
1679 int call_changed_bytes; /* call changed_bytes() */
|
7
|
1680 {
|
|
1681 int vcol;
|
|
1682 int last_vcol;
|
|
1683 int insstart_less; /* reduction for Insstart.col */
|
|
1684 int new_cursor_col;
|
|
1685 int i;
|
|
1686 char_u *ptr;
|
|
1687 int save_p_list;
|
|
1688 int start_col;
|
|
1689 colnr_T vc;
|
|
1690 #ifdef FEAT_VREPLACE
|
|
1691 colnr_T orig_col = 0; /* init for GCC */
|
|
1692 char_u *new_line, *orig_line = NULL; /* init for GCC */
|
|
1693
|
|
1694 /* VREPLACE mode needs to know what the line was like before changing */
|
|
1695 if (State & VREPLACE_FLAG)
|
|
1696 {
|
|
1697 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
|
|
1698 orig_col = curwin->w_cursor.col;
|
|
1699 }
|
|
1700 #endif
|
|
1701
|
|
1702 /* for the following tricks we don't want list mode */
|
|
1703 save_p_list = curwin->w_p_list;
|
|
1704 curwin->w_p_list = FALSE;
|
|
1705 vc = getvcol_nolist(&curwin->w_cursor);
|
|
1706 vcol = vc;
|
|
1707
|
|
1708 /*
|
|
1709 * For Replace mode we need to fix the replace stack later, which is only
|
|
1710 * possible when the cursor is in the indent. Remember the number of
|
|
1711 * characters before the cursor if it's possible.
|
|
1712 */
|
|
1713 start_col = curwin->w_cursor.col;
|
|
1714
|
|
1715 /* determine offset from first non-blank */
|
|
1716 new_cursor_col = curwin->w_cursor.col;
|
|
1717 beginline(BL_WHITE);
|
|
1718 new_cursor_col -= curwin->w_cursor.col;
|
|
1719
|
|
1720 insstart_less = curwin->w_cursor.col;
|
|
1721
|
|
1722 /*
|
|
1723 * If the cursor is in the indent, compute how many screen columns the
|
|
1724 * cursor is to the left of the first non-blank.
|
|
1725 */
|
|
1726 if (new_cursor_col < 0)
|
|
1727 vcol = get_indent() - vcol;
|
|
1728
|
|
1729 if (new_cursor_col > 0) /* can't fix replace stack */
|
|
1730 start_col = -1;
|
|
1731
|
|
1732 /*
|
|
1733 * Set the new indent. The cursor will be put on the first non-blank.
|
|
1734 */
|
|
1735 if (type == INDENT_SET)
|
1516
|
1736 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
|
7
|
1737 else
|
|
1738 {
|
|
1739 #ifdef FEAT_VREPLACE
|
|
1740 int save_State = State;
|
|
1741
|
|
1742 /* Avoid being called recursively. */
|
|
1743 if (State & VREPLACE_FLAG)
|
|
1744 State = INSERT;
|
|
1745 #endif
|
1516
|
1746 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
|
7
|
1747 #ifdef FEAT_VREPLACE
|
|
1748 State = save_State;
|
|
1749 #endif
|
|
1750 }
|
|
1751 insstart_less -= curwin->w_cursor.col;
|
|
1752
|
|
1753 /*
|
|
1754 * Try to put cursor on same character.
|
|
1755 * If the cursor is at or after the first non-blank in the line,
|
|
1756 * compute the cursor column relative to the column of the first
|
|
1757 * non-blank character.
|
|
1758 * If we are not in insert mode, leave the cursor on the first non-blank.
|
|
1759 * If the cursor is before the first non-blank, position it relative
|
|
1760 * to the first non-blank, counted in screen columns.
|
|
1761 */
|
|
1762 if (new_cursor_col >= 0)
|
|
1763 {
|
|
1764 /*
|
|
1765 * When changing the indent while the cursor is touching it, reset
|
|
1766 * Insstart_col to 0.
|
|
1767 */
|
|
1768 if (new_cursor_col == 0)
|
|
1769 insstart_less = MAXCOL;
|
|
1770 new_cursor_col += curwin->w_cursor.col;
|
|
1771 }
|
|
1772 else if (!(State & INSERT))
|
|
1773 new_cursor_col = curwin->w_cursor.col;
|
|
1774 else
|
|
1775 {
|
|
1776 /*
|
|
1777 * Compute the screen column where the cursor should be.
|
|
1778 */
|
|
1779 vcol = get_indent() - vcol;
|
1869
|
1780 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
|
7
|
1781
|
|
1782 /*
|
|
1783 * Advance the cursor until we reach the right screen column.
|
|
1784 */
|
|
1785 vcol = last_vcol = 0;
|
|
1786 new_cursor_col = -1;
|
|
1787 ptr = ml_get_curline();
|
|
1788 while (vcol <= (int)curwin->w_virtcol)
|
|
1789 {
|
|
1790 last_vcol = vcol;
|
|
1791 #ifdef FEAT_MBYTE
|
|
1792 if (has_mbyte && new_cursor_col >= 0)
|
474
|
1793 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
|
7
|
1794 else
|
|
1795 #endif
|
|
1796 ++new_cursor_col;
|
|
1797 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
|
|
1798 }
|
|
1799 vcol = last_vcol;
|
|
1800
|
|
1801 /*
|
|
1802 * May need to insert spaces to be able to position the cursor on
|
|
1803 * the right screen column.
|
|
1804 */
|
|
1805 if (vcol != (int)curwin->w_virtcol)
|
|
1806 {
|
1869
|
1807 curwin->w_cursor.col = (colnr_T)new_cursor_col;
|
7
|
1808 i = (int)curwin->w_virtcol - vcol;
|
1869
|
1809 ptr = alloc((unsigned)(i + 1));
|
7
|
1810 if (ptr != NULL)
|
|
1811 {
|
|
1812 new_cursor_col += i;
|
|
1813 ptr[i] = NUL;
|
|
1814 while (--i >= 0)
|
|
1815 ptr[i] = ' ';
|
|
1816 ins_str(ptr);
|
|
1817 vim_free(ptr);
|
|
1818 }
|
|
1819 }
|
|
1820
|
|
1821 /*
|
|
1822 * When changing the indent while the cursor is in it, reset
|
|
1823 * Insstart_col to 0.
|
|
1824 */
|
|
1825 insstart_less = MAXCOL;
|
|
1826 }
|
|
1827
|
|
1828 curwin->w_p_list = save_p_list;
|
|
1829
|
|
1830 if (new_cursor_col <= 0)
|
|
1831 curwin->w_cursor.col = 0;
|
|
1832 else
|
1869
|
1833 curwin->w_cursor.col = (colnr_T)new_cursor_col;
|
7
|
1834 curwin->w_set_curswant = TRUE;
|
|
1835 changed_cline_bef_curs();
|
|
1836
|
|
1837 /*
|
|
1838 * May have to adjust the start of the insert.
|
|
1839 */
|
|
1840 if (State & INSERT)
|
|
1841 {
|
|
1842 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
|
|
1843 {
|
|
1844 if ((int)Insstart.col <= insstart_less)
|
|
1845 Insstart.col = 0;
|
|
1846 else
|
|
1847 Insstart.col -= insstart_less;
|
|
1848 }
|
|
1849 if ((int)ai_col <= insstart_less)
|
|
1850 ai_col = 0;
|
|
1851 else
|
|
1852 ai_col -= insstart_less;
|
|
1853 }
|
|
1854
|
|
1855 /*
|
|
1856 * For REPLACE mode, may have to fix the replace stack, if it's possible.
|
|
1857 * If the number of characters before the cursor decreased, need to pop a
|
|
1858 * few characters from the replace stack.
|
|
1859 * If the number of characters before the cursor increased, need to push a
|
|
1860 * few NULs onto the replace stack.
|
|
1861 */
|
|
1862 if (REPLACE_NORMAL(State) && start_col >= 0)
|
|
1863 {
|
|
1864 while (start_col > (int)curwin->w_cursor.col)
|
|
1865 {
|
|
1866 replace_join(0); /* remove a NUL from the replace stack */
|
|
1867 --start_col;
|
|
1868 }
|
|
1869 while (start_col < (int)curwin->w_cursor.col || replaced)
|
|
1870 {
|
|
1871 replace_push(NUL);
|
|
1872 if (replaced)
|
|
1873 {
|
|
1874 replace_push(replaced);
|
|
1875 replaced = NUL;
|
|
1876 }
|
|
1877 ++start_col;
|
|
1878 }
|
|
1879 }
|
|
1880
|
|
1881 #ifdef FEAT_VREPLACE
|
|
1882 /*
|
|
1883 * For VREPLACE mode, we also have to fix the replace stack. In this case
|
|
1884 * it is always possible because we backspace over the whole line and then
|
|
1885 * put it back again the way we wanted it.
|
|
1886 */
|
|
1887 if (State & VREPLACE_FLAG)
|
|
1888 {
|
|
1889 /* If orig_line didn't allocate, just return. At least we did the job,
|
|
1890 * even if you can't backspace. */
|
|
1891 if (orig_line == NULL)
|
|
1892 return;
|
|
1893
|
|
1894 /* Save new line */
|
|
1895 new_line = vim_strsave(ml_get_curline());
|
|
1896 if (new_line == NULL)
|
|
1897 return;
|
|
1898
|
|
1899 /* We only put back the new line up to the cursor */
|
|
1900 new_line[curwin->w_cursor.col] = NUL;
|
|
1901
|
|
1902 /* Put back original line */
|
|
1903 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
|
|
1904 curwin->w_cursor.col = orig_col;
|
|
1905
|
|
1906 /* Backspace from cursor to start of line */
|
|
1907 backspace_until_column(0);
|
|
1908
|
|
1909 /* Insert new stuff into line again */
|
|
1910 ins_bytes(new_line);
|
|
1911
|
|
1912 vim_free(new_line);
|
|
1913 }
|
|
1914 #endif
|
|
1915 }
|
|
1916
|
|
1917 /*
|
|
1918 * Truncate the space at the end of a line. This is to be used only in an
|
|
1919 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
|
|
1920 * modes.
|
|
1921 */
|
|
1922 void
|
|
1923 truncate_spaces(line)
|
|
1924 char_u *line;
|
|
1925 {
|
|
1926 int i;
|
|
1927
|
|
1928 /* find start of trailing white space */
|
|
1929 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
|
|
1930 {
|
|
1931 if (State & REPLACE_FLAG)
|
|
1932 replace_join(0); /* remove a NUL from the replace stack */
|
|
1933 }
|
|
1934 line[i + 1] = NUL;
|
|
1935 }
|
|
1936
|
|
1937 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
|
|
1938 || defined(FEAT_COMMENTS) || defined(PROTO)
|
|
1939 /*
|
|
1940 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
|
|
1941 * modes correctly. May also be used when not in insert mode at all.
|
1782
|
1942 * Will attempt not to go before "col" even when there is a composing
|
|
1943 * character.
|
7
|
1944 */
|
|
1945 void
|
|
1946 backspace_until_column(col)
|
|
1947 int col;
|
|
1948 {
|
|
1949 while ((int)curwin->w_cursor.col > col)
|
|
1950 {
|
|
1951 curwin->w_cursor.col--;
|
|
1952 if (State & REPLACE_FLAG)
|
1782
|
1953 replace_do_bs(col);
|
|
1954 else if (!del_char_after_col(col))
|
|
1955 break;
|
|
1956 }
|
|
1957 }
|
|
1958 #endif
|
|
1959
|
|
1960 /*
|
|
1961 * Like del_char(), but make sure not to go before column "limit_col".
|
|
1962 * Only matters when there are composing characters.
|
|
1963 * Return TRUE when something was deleted.
|
|
1964 */
|
1796
|
1965 /*ARGSUSED*/
|
1782
|
1966 static int
|
|
1967 del_char_after_col(limit_col)
|
|
1968 int limit_col;
|
|
1969 {
|
|
1970 #ifdef FEAT_MBYTE
|
|
1971 if (enc_utf8 && limit_col >= 0)
|
|
1972 {
|
1869
|
1973 colnr_T ecol = curwin->w_cursor.col + 1;
|
1782
|
1974
|
|
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. */
|
|
1978 mb_adjust_cursor();
|
1796
|
1979 while (curwin->w_cursor.col < (colnr_T)limit_col)
|
1782
|
1980 {
|
|
1981 int l = utf_ptr2len(ml_get_cursor());
|
|
1982
|
|
1983 if (l == 0) /* end of line */
|
|
1984 break;
|
|
1985 curwin->w_cursor.col += l;
|
|
1986 }
|
|
1987 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
|
|
1988 return FALSE;
|
1869
|
1989 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
|
1782
|
1990 }
|
|
1991 else
|
|
1992 #endif
|
|
1993 (void)del_char(FALSE);
|
|
1994 return TRUE;
|
|
1995 }
|
7
|
1996
|
|
1997 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
|
|
1998 /*
|
449
|
1999 * CTRL-X pressed in Insert mode.
|
|
2000 */
|
|
2001 static void
|
|
2002 ins_ctrl_x()
|
|
2003 {
|
|
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)
|
|
2007 {
|
|
2008 /* if the next ^X<> won't ADD nothing, then reset
|
|
2009 * compl_cont_status */
|
|
2010 if (compl_cont_status & CONT_N_ADDS)
|
665
|
2011 compl_cont_status |= CONT_INTRPT;
|
449
|
2012 else
|
|
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;
|
|
2018 showmode();
|
|
2019 }
|
|
2020 }
|
|
2021
|
|
2022 /*
|
|
2023 * Return TRUE if the 'dict' or 'tsr' option can be used.
|
|
2024 */
|
|
2025 static int
|
|
2026 has_compl_option(dict_opt)
|
|
2027 int dict_opt;
|
|
2028 {
|
703
|
2029 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
|
744
|
2030 # ifdef FEAT_SPELL
|
|
2031 && !curwin->w_p_spell
|
|
2032 # endif
|
|
2033 )
|
449
|
2034 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
|
|
2035 {
|
|
2036 ctrl_x_mode = 0;
|
|
2037 edit_submode = NULL;
|
|
2038 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
|
|
2039 : (char_u *)_("'thesaurus' option is empty"),
|
|
2040 hl_attr(HLF_E));
|
|
2041 if (emsg_silent == 0)
|
|
2042 {
|
|
2043 vim_beep();
|
|
2044 setcursor();
|
|
2045 out_flush();
|
|
2046 ui_delay(2000L, FALSE);
|
|
2047 }
|
|
2048 return FALSE;
|
|
2049 }
|
|
2050 return TRUE;
|
|
2051 }
|
|
2052
|
|
2053 /*
|
7
|
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.
|
|
2056 */
|
|
2057 int
|
|
2058 vim_is_ctrl_x_key(c)
|
|
2059 int c;
|
|
2060 {
|
|
2061 /* Always allow ^R - let it's results then be checked */
|
|
2062 if (c == Ctrl_R)
|
|
2063 return TRUE;
|
|
2064
|
610
|
2065 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
|
644
|
2066 if (ins_compl_pum_key(c))
|
610
|
2067 return TRUE;
|
|
2068
|
7
|
2069 switch (ctrl_x_mode)
|
|
2070 {
|
|
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:
|
449
|
2074 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
|
7
|
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
|
477
|
2078 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
|
|
2079 || c == Ctrl_S || c == 's');
|
7
|
2080 case CTRL_X_SCROLL:
|
|
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);
|
|
2084 case CTRL_X_FILES:
|
|
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);
|
|
2090 case CTRL_X_TAGS:
|
|
2091 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
|
|
2092 #ifdef FEAT_FIND_ID
|
|
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);
|
|
2097 #endif
|
|
2098 case CTRL_X_CMDLINE:
|
|
2099 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
|
|
2100 || c == Ctrl_X);
|
12
|
2101 #ifdef FEAT_COMPL_FUNC
|
|
2102 case CTRL_X_FUNCTION:
|
449
|
2103 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
|
523
|
2104 case CTRL_X_OMNI:
|
449
|
2105 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
|
502
|
2106 #endif
|
477
|
2107 case CTRL_X_SPELL:
|
|
2108 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
|
7
|
2109 }
|
|
2110 EMSG(_(e_internal));
|
|
2111 return FALSE;
|
|
2112 }
|
|
2113
|
|
2114 /*
|
1430
|
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
|
|
2117 * is visible.
|
|
2118 */
|
|
2119 static int
|
|
2120 ins_compl_accept_char(c)
|
|
2121 int c;
|
|
2122 {
|
|
2123 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
|
|
2124 /* When expanding an identifier only accept identifier chars. */
|
|
2125 return vim_isIDc(c);
|
|
2126
|
|
2127 switch (ctrl_x_mode)
|
|
2128 {
|
|
2129 case CTRL_X_FILES:
|
|
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);
|
|
2134
|
|
2135 case CTRL_X_CMDLINE:
|
|
2136 case CTRL_X_OMNI:
|
|
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);
|
|
2140
|
|
2141 case CTRL_X_WHOLE_LINE:
|
|
2142 /* For while line completion a space can be part of the line. */
|
|
2143 return vim_isprintc(c);
|
|
2144 }
|
|
2145 return vim_iswordc(c);
|
|
2146 }
|
|
2147
|
|
2148 /*
|
659
|
2149 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
|
7
|
2150 * case of the originally typed text is used, and the case of the completed
|
1219
|
2151 * text is inferred, ie this tries to work out what case you probably wanted
|
7
|
2152 * the rest of the word to be in -- webb
|
|
2153 */
|
|
2154 int
|
681
|
2155 ins_compl_add_infercase(str, len, icase, fname, dir, flags)
|
7
|
2156 char_u *str;
|
|
2157 int len;
|
681
|
2158 int icase;
|
7
|
2159 char_u *fname;
|
|
2160 int dir;
|
464
|
2161 int flags;
|
7
|
2162 {
|
1353
|
2163 char_u *p;
|
|
2164 int i, c;
|
|
2165 int actual_len; /* Take multi-byte characters */
|
|
2166 int actual_compl_length; /* into account. */
|
|
2167 int *wca; /* Wide character array. */
|
7
|
2168 int has_lower = FALSE;
|
|
2169 int was_letter = FALSE;
|
1353
|
2170
|
1436
|
2171 if (p_ic && curbuf->b_p_inf && len > 0)
|
1353
|
2172 {
|
|
2173 /* Infer case of completed part. */
|
|
2174
|
|
2175 /* Find actual length of completion. */
|
|
2176 #ifdef FEAT_MBYTE
|
|
2177 if (has_mbyte)
|
|
2178 {
|
|
2179 p = str;
|
|
2180 actual_len = 0;
|
|
2181 while (*p != NUL)
|
|
2182 {
|
|
2183 mb_ptr_adv(p);
|
|
2184 ++actual_len;
|
|
2185 }
|
|
2186 }
|
|
2187 else
|
|
2188 #endif
|
|
2189 actual_len = len;
|
|
2190
|
|
2191 /* Find actual length of original text. */
|
|
2192 #ifdef FEAT_MBYTE
|
|
2193 if (has_mbyte)
|
|
2194 {
|
|
2195 p = compl_orig_text;
|
|
2196 actual_compl_length = 0;
|
|
2197 while (*p != NUL)
|
7
|
2198 {
|
1353
|
2199 mb_ptr_adv(p);
|
|
2200 ++actual_compl_length;
|
|
2201 }
|
|
2202 }
|
|
2203 else
|
|
2204 #endif
|
|
2205 actual_compl_length = compl_length;
|
|
2206
|
|
2207 /* Allocate wide character array for the completion and fill it. */
|
1869
|
2208 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
|
1353
|
2209 if (wca != NULL)
|
|
2210 {
|
|
2211 p = str;
|
|
2212 for (i = 0; i < actual_len; ++i)
|
|
2213 #ifdef FEAT_MBYTE
|
|
2214 if (has_mbyte)
|
|
2215 wca[i] = mb_ptr2char_adv(&p);
|
|
2216 else
|
|
2217 #endif
|
|
2218 wca[i] = *(p++);
|
|
2219
|
|
2220 /* Rule 1: Were any chars converted to lower? */
|
|
2221 p = compl_orig_text;
|
|
2222 for (i = 0; i < actual_compl_length; ++i)
|
|
2223 {
|
|
2224 #ifdef FEAT_MBYTE
|
|
2225 if (has_mbyte)
|
|
2226 c = mb_ptr2char_adv(&p);
|
|
2227 else
|
|
2228 #endif
|
|
2229 c = *(p++);
|
|
2230 if (MB_ISLOWER(c))
|
7
|
2231 {
|
1353
|
2232 has_lower = TRUE;
|
|
2233 if (MB_ISUPPER(wca[i]))
|
|
2234 {
|
|
2235 /* Rule 1 is satisfied. */
|
|
2236 for (i = actual_compl_length; i < actual_len; ++i)
|
|
2237 wca[i] = MB_TOLOWER(wca[i]);
|
|
2238 break;
|
|
2239 }
|
7
|
2240 }
|
|
2241 }
|
1353
|
2242
|
|
2243 /*
|
|
2244 * Rule 2: No lower case, 2nd consecutive letter converted to
|
|
2245 * upper case.
|
|
2246 */
|
|
2247 if (!has_lower)
|
|
2248 {
|
|
2249 p = compl_orig_text;
|
|
2250 for (i = 0; i < actual_compl_length; ++i)
|
|
2251 {
|
|
2252 #ifdef FEAT_MBYTE
|
|
2253 if (has_mbyte)
|
|
2254 c = mb_ptr2char_adv(&p);
|
|
2255 else
|
|
2256 #endif
|
|
2257 c = *(p++);
|
|
2258 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
|
|
2259 {
|
|
2260 /* Rule 2 is satisfied. */
|
|
2261 for (i = actual_compl_length; i < actual_len; ++i)
|
|
2262 wca[i] = MB_TOUPPER(wca[i]);
|
|
2263 break;
|
|
2264 }
|
|
2265 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
|
|
2266 }
|
|
2267 }
|
|
2268
|
|
2269 /* Copy the original case of the part we typed. */
|
|
2270 p = compl_orig_text;
|
|
2271 for (i = 0; i < actual_compl_length; ++i)
|
7
|
2272 {
|
1353
|
2273 #ifdef FEAT_MBYTE
|
|
2274 if (has_mbyte)
|
|
2275 c = mb_ptr2char_adv(&p);
|
|
2276 else
|
|
2277 #endif
|
|
2278 c = *(p++);
|
|
2279 if (MB_ISLOWER(c))
|
|
2280 wca[i] = MB_TOLOWER(wca[i]);
|
|
2281 else if (MB_ISUPPER(c))
|
|
2282 wca[i] = MB_TOUPPER(wca[i]);
|
7
|
2283 }
|
1353
|
2284
|
1436
|
2285 /*
|
1353
|
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.
|
|
2290 */
|
|
2291 p = IObuff;
|
|
2292 i = 0;
|
|
2293 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
|
|
2294 #ifdef FEAT_MBYTE
|
|
2295 if (has_mbyte)
|
1446
|
2296 p += (*mb_char2bytes)(wca[i++], p);
|
1353
|
2297 else
|
|
2298 #endif
|
|
2299 *(p++) = wca[i++];
|
|
2300 *p = NUL;
|
|
2301
|
|
2302 vim_free(wca);
|
|
2303 }
|
7
|
2304
|
841
|
2305 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
|
|
2306 flags, FALSE);
|
|
2307 }
|
|
2308 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
|
7
|
2309 }
|
|
2310
|
|
2311 /*
|
|
2312 * Add a match to the list of matches.
|
|
2313 * If the given string is already in the list of completions, then return
|
464
|
2314 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
|
681
|
2315 * maybe because alloc() returns NULL, then FAIL is returned.
|
7
|
2316 */
|
841
|
2317 static int
|
944
|
2318 ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
|
7
|
2319 char_u *str;
|
|
2320 int len;
|
681
|
2321 int icase;
|
7
|
2322 char_u *fname;
|
841
|
2323 char_u **cptext; /* extra text for popup menu or NULL */
|
659
|
2324 int cdir;
|
464
|
2325 int flags;
|
944
|
2326 int adup; /* accept duplicate match */
|
7
|
2327 {
|
464
|
2328 compl_T *match;
|
659
|
2329 int dir = (cdir == 0 ? compl_direction : cdir);
|
7
|
2330
|
|
2331 ui_breakcheck();
|
|
2332 if (got_int)
|
464
|
2333 return FAIL;
|
7
|
2334 if (len < 0)
|
|
2335 len = (int)STRLEN(str);
|
|
2336
|
|
2337 /*
|
|
2338 * If the same match is already present, don't add it.
|
|
2339 */
|
944
|
2340 if (compl_first_match != NULL && !adup)
|
449
|
2341 {
|
|
2342 match = compl_first_match;
|
7
|
2343 do
|
|
2344 {
|
464
|
2345 if ( !(match->cp_flags & ORIGINAL_TEXT)
|
989
|
2346 && STRNCMP(match->cp_str, str, len) == 0
|
464
|
2347 && match->cp_str[len] == NUL)
|
|
2348 return NOTDONE;
|
|
2349 match = match->cp_next;
|
449
|
2350 } while (match != NULL && match != compl_first_match);
|
7
|
2351 }
|
|
2352
|
540
|
2353 /* Remove any popup menu before changing the list of matches. */
|
|
2354 ins_compl_del_pum();
|
|
2355
|
7
|
2356 /*
|
|
2357 * Allocate a new match structure.
|
|
2358 * Copy the values to the new match structure.
|
|
2359 */
|
659
|
2360 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
|
7
|
2361 if (match == NULL)
|
464
|
2362 return FAIL;
|
|
2363 match->cp_number = -1;
|
|
2364 if (flags & ORIGINAL_TEXT)
|
|
2365 match->cp_number = 0;
|
694
|
2366 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
|
7
|
2367 {
|
|
2368 vim_free(match);
|
464
|
2369 return FAIL;
|
7
|
2370 }
|
681
|
2371 match->cp_icase = icase;
|
659
|
2372
|
7
|
2373 /* match-fname is:
|
464
|
2374 * - compl_curr_match->cp_fname if it is a string equal to fname.
|
7
|
2375 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
|
|
2376 * - NULL otherwise. --Acevedo */
|
659
|
2377 if (fname != NULL
|
829
|
2378 && compl_curr_match != NULL
|
659
|
2379 && compl_curr_match->cp_fname != NULL
|
|
2380 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
|
464
|
2381 match->cp_fname = compl_curr_match->cp_fname;
|
659
|
2382 else if (fname != NULL)
|
|
2383 {
|
|
2384 match->cp_fname = vim_strsave(fname);
|
464
|
2385 flags |= FREE_FNAME;
|
659
|
2386 }
|
7
|
2387 else
|
464
|
2388 match->cp_fname = NULL;
|
|
2389 match->cp_flags = flags;
|
786
|
2390
|
|
2391 if (cptext != NULL)
|
|
2392 {
|
|
2393 int i;
|
|
2394
|
|
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]);
|
|
2398 }
|
7
|
2399
|
|
2400 /*
|
|
2401 * Link the new match structure in the list of matches.
|
|
2402 */
|
449
|
2403 if (compl_first_match == NULL)
|
464
|
2404 match->cp_next = match->cp_prev = NULL;
|
7
|
2405 else if (dir == FORWARD)
|
|
2406 {
|
464
|
2407 match->cp_next = compl_curr_match->cp_next;
|
|
2408 match->cp_prev = compl_curr_match;
|
7
|
2409 }
|
|
2410 else /* BACKWARD */
|
|
2411 {
|
464
|
2412 match->cp_next = compl_curr_match;
|
|
2413 match->cp_prev = compl_curr_match->cp_prev;
|
|
2414 }
|
|
2415 if (match->cp_next)
|
|
2416 match->cp_next->cp_prev = match;
|
|
2417 if (match->cp_prev)
|
|
2418 match->cp_prev->cp_next = match;
|
7
|
2419 else /* if there's nothing before, it is the first match */
|
449
|
2420 compl_first_match = match;
|
|
2421 compl_curr_match = match;
|
7
|
2422
|
665
|
2423 /*
|
|
2424 * Find the longest common string if still doing that.
|
|
2425 */
|
|
2426 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
|
|
2427 ins_compl_longest_match(match);
|
|
2428
|
7
|
2429 return OK;
|
|
2430 }
|
|
2431
|
|
2432 /*
|
681
|
2433 * Return TRUE if "str[len]" matches with match->cp_str, considering
|
|
2434 * match->cp_icase.
|
|
2435 */
|
|
2436 static int
|
|
2437 ins_compl_equal(match, str, len)
|
|
2438 compl_T *match;
|
|
2439 char_u *str;
|
|
2440 int len;
|
|
2441 {
|
|
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;
|
|
2445 }
|
|
2446
|
|
2447 /*
|
665
|
2448 * Reduce the longest common string for match "match".
|
|
2449 */
|
|
2450 static void
|
|
2451 ins_compl_longest_match(match)
|
|
2452 compl_T *match;
|
|
2453 {
|
|
2454 char_u *p, *s;
|
681
|
2455 int c1, c2;
|
665
|
2456 int had_match;
|
|
2457
|
|
2458 if (compl_leader == NULL)
|
842
|
2459 {
|
665
|
2460 /* First match, use it as a whole. */
|
|
2461 compl_leader = vim_strsave(match->cp_str);
|
842
|
2462 if (compl_leader != NULL)
|
|
2463 {
|
|
2464 had_match = (curwin->w_cursor.col > compl_col);
|
|
2465 ins_compl_delete();
|
1782
|
2466 ins_bytes(compl_leader + ins_compl_len());
|
842
|
2467 ins_redraw(FALSE);
|
|
2468
|
|
2469 /* When the match isn't there (to avoid matching itself) remove it
|
|
2470 * again after redrawing. */
|
|
2471 if (!had_match)
|
|
2472 ins_compl_delete();
|
|
2473 compl_used_match = FALSE;
|
|
2474 }
|
|
2475 }
|
665
|
2476 else
|
|
2477 {
|
|
2478 /* Reduce the text if this match differs from compl_leader. */
|
681
|
2479 p = compl_leader;
|
|
2480 s = match->cp_str;
|
|
2481 while (*p != NUL)
|
665
|
2482 {
|
|
2483 #ifdef FEAT_MBYTE
|
|
2484 if (has_mbyte)
|
|
2485 {
|
681
|
2486 c1 = mb_ptr2char(p);
|
|
2487 c2 = mb_ptr2char(s);
|
665
|
2488 }
|
|
2489 else
|
|
2490 #endif
|
|
2491 {
|
681
|
2492 c1 = *p;
|
|
2493 c2 = *s;
|
|
2494 }
|
|
2495 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
|
|
2496 : (c1 != c2))
|
|
2497 break;
|
|
2498 #ifdef FEAT_MBYTE
|
|
2499 if (has_mbyte)
|
|
2500 {
|
|
2501 mb_ptr_adv(p);
|
|
2502 mb_ptr_adv(s);
|
|
2503 }
|
|
2504 else
|
|
2505 #endif
|
|
2506 {
|
|
2507 ++p;
|
|
2508 ++s;
|
665
|
2509 }
|
|
2510 }
|
|
2511
|
|
2512 if (*p != NUL)
|
|
2513 {
|
|
2514 /* Leader was shortened, need to change the inserted text. */
|
|
2515 *p = NUL;
|
|
2516 had_match = (curwin->w_cursor.col > compl_col);
|
|
2517 ins_compl_delete();
|
1782
|
2518 ins_bytes(compl_leader + ins_compl_len());
|
665
|
2519 ins_redraw(FALSE);
|
|
2520
|
|
2521 /* When the match isn't there (to avoid matching itself) remove it
|
|
2522 * again after redrawing. */
|
|
2523 if (!had_match)
|
|
2524 ins_compl_delete();
|
|
2525 }
|
|
2526
|
|
2527 compl_used_match = FALSE;
|
|
2528 }
|
|
2529 }
|
|
2530
|
|
2531 /*
|
7
|
2532 * Add an array of matches to the list of matches.
|
|
2533 * Frees matches[].
|
|
2534 */
|
|
2535 static void
|
681
|
2536 ins_compl_add_matches(num_matches, matches, icase)
|
7
|
2537 int num_matches;
|
|
2538 char_u **matches;
|
681
|
2539 int icase;
|
7
|
2540 {
|
|
2541 int i;
|
|
2542 int add_r = OK;
|
659
|
2543 int dir = compl_direction;
|
7
|
2544
|
464
|
2545 for (i = 0; i < num_matches && add_r != FAIL; i++)
|
681
|
2546 if ((add_r = ins_compl_add(matches[i], -1, icase,
|
841
|
2547 NULL, NULL, dir, 0, FALSE)) == OK)
|
7
|
2548 /* if dir was BACKWARD then honor it just once */
|
659
|
2549 dir = FORWARD;
|
7
|
2550 FreeWild(num_matches, matches);
|
|
2551 }
|
|
2552
|
|
2553 /* Make the completion list cyclic.
|
|
2554 * Return the number of matches (excluding the original).
|
|
2555 */
|
|
2556 static int
|
|
2557 ins_compl_make_cyclic()
|
|
2558 {
|
464
|
2559 compl_T *match;
|
7
|
2560 int count = 0;
|
|
2561
|
449
|
2562 if (compl_first_match != NULL)
|
7
|
2563 {
|
|
2564 /*
|
|
2565 * Find the end of the list.
|
|
2566 */
|
449
|
2567 match = compl_first_match;
|
|
2568 /* there's always an entry for the compl_orig_text, it doesn't count. */
|
464
|
2569 while (match->cp_next != NULL && match->cp_next != compl_first_match)
|
|
2570 {
|
|
2571 match = match->cp_next;
|
7
|
2572 ++count;
|
|
2573 }
|
464
|
2574 match->cp_next = compl_first_match;
|
|
2575 compl_first_match->cp_prev = match;
|
7
|
2576 }
|
|
2577 return count;
|
|
2578 }
|
|
2579
|
724
|
2580 /*
|
|
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.
|
|
2584 */
|
|
2585 void
|
|
2586 set_completion(startcol, list)
|
1869
|
2587 colnr_T startcol;
|
724
|
2588 list_T *list;
|
|
2589 {
|
|
2590 /* If already doing completions stop it. */
|
|
2591 if (ctrl_x_mode != 0)
|
|
2592 ins_compl_prep(' ');
|
|
2593 ins_compl_clear();
|
|
2594
|
|
2595 if (stop_arrow() == FAIL)
|
|
2596 return;
|
|
2597
|
1869
|
2598 if (startcol > curwin->w_cursor.col)
|
724
|
2599 startcol = curwin->w_cursor.col;
|
|
2600 compl_col = startcol;
|
1869
|
2601 compl_length = (int)curwin->w_cursor.col - (int)startcol;
|
724
|
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,
|
942
|
2605 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
|
724
|
2606 return;
|
|
2607
|
|
2608 /* Handle like dictionary completion. */
|
|
2609 ctrl_x_mode = CTRL_X_WHOLE_LINE;
|
|
2610
|
|
2611 ins_compl_add_list(list);
|
|
2612 compl_matches = ins_compl_make_cyclic();
|
|
2613 compl_started = TRUE;
|
|
2614 compl_used_match = TRUE;
|
929
|
2615 compl_cont_status = 0;
|
724
|
2616
|
|
2617 compl_curr_match = compl_first_match;
|
|
2618 ins_complete(Ctrl_N);
|
|
2619 out_flush();
|
|
2620 }
|
|
2621
|
|
2622
|
574
|
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. */
|
659
|
2625 static pumitem_T *compl_match_array = NULL;
|
540
|
2626 static int compl_match_arraysize;
|
|
2627
|
|
2628 /*
|
|
2629 * Update the screen and when there is any scrolling remove the popup menu.
|
|
2630 */
|
|
2631 static void
|
|
2632 ins_compl_upd_pum()
|
|
2633 {
|
|
2634 int h;
|
|
2635
|
|
2636 if (compl_match_array != NULL)
|
|
2637 {
|
|
2638 h = curwin->w_cline_height;
|
|
2639 update_screen(0);
|
|
2640 if (h != curwin->w_cline_height)
|
|
2641 ins_compl_del_pum();
|
|
2642 }
|
|
2643 }
|
|
2644
|
|
2645 /*
|
|
2646 * Remove any popup menu.
|
|
2647 */
|
|
2648 static void
|
|
2649 ins_compl_del_pum()
|
|
2650 {
|
|
2651 if (compl_match_array != NULL)
|
|
2652 {
|
|
2653 pum_undisplay();
|
|
2654 vim_free(compl_match_array);
|
|
2655 compl_match_array = NULL;
|
|
2656 }
|
|
2657 }
|
|
2658
|
|
2659 /*
|
|
2660 * Return TRUE if the popup menu should be displayed.
|
|
2661 */
|
|
2662 static int
|
|
2663 pum_wanted()
|
|
2664 {
|
707
|
2665 /* 'completeopt' must contain "menu" or "menuone" */
|
665
|
2666 if (vim_strchr(p_cot, 'm') == NULL)
|
540
|
2667 return FALSE;
|
|
2668
|
|
2669 /* The display looks bad on a B&W display. */
|
|
2670 if (t_colors < 8
|
|
2671 #ifdef FEAT_GUI
|
|
2672 && !gui.in_use
|
|
2673 #endif
|
|
2674 )
|
|
2675 return FALSE;
|
657
|
2676 return TRUE;
|
|
2677 }
|
|
2678
|
|
2679 /*
|
|
2680 * Return TRUE if there are two or more matches to be shown in the popup menu.
|
707
|
2681 * One if 'completopt' contains "menuone".
|
657
|
2682 */
|
|
2683 static int
|
707
|
2684 pum_enough_matches()
|
657
|
2685 {
|
|
2686 compl_T *compl;
|
|
2687 int i;
|
540
|
2688
|
|
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;
|
|
2692 i = 0;
|
|
2693 do
|
|
2694 {
|
|
2695 if (compl == NULL
|
|
2696 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
|
|
2697 break;
|
|
2698 compl = compl->cp_next;
|
|
2699 } while (compl != compl_first_match);
|
|
2700
|
707
|
2701 if (strstr((char *)p_cot, "menuone") != NULL)
|
|
2702 return (i >= 1);
|
540
|
2703 return (i >= 2);
|
|
2704 }
|
|
2705
|
|
2706 /*
|
|
2707 * Show the popup menu for the list of matches.
|
659
|
2708 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
|
540
|
2709 */
|
648
|
2710 void
|
540
|
2711 ins_compl_show_pum()
|
|
2712 {
|
|
2713 compl_T *compl;
|
659
|
2714 compl_T *shown_compl = NULL;
|
|
2715 int did_find_shown_match = FALSE;
|
|
2716 int shown_match_ok = FALSE;
|
540
|
2717 int i;
|
|
2718 int cur = -1;
|
|
2719 colnr_T col;
|
657
|
2720 int lead_len = 0;
|
|
2721
|
707
|
2722 if (!pum_wanted() || !pum_enough_matches())
|
540
|
2723 return;
|
|
2724
|
794
|
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");
|
|
2728 #endif
|
|
2729
|
540
|
2730 /* Update the screen before drawing the popup menu over it. */
|
|
2731 update_screen(0);
|
|
2732
|
|
2733 if (compl_match_array == NULL)
|
|
2734 {
|
|
2735 /* Need to build the popup menu list. */
|
|
2736 compl_match_arraysize = 0;
|
|
2737 compl = compl_first_match;
|
657
|
2738 if (compl_leader != NULL)
|
835
|
2739 lead_len = (int)STRLEN(compl_leader);
|
540
|
2740 do
|
|
2741 {
|
657
|
2742 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
|
|
2743 && (compl_leader == NULL
|
681
|
2744 || ins_compl_equal(compl, compl_leader, lead_len)))
|
540
|
2745 ++compl_match_arraysize;
|
|
2746 compl = compl->cp_next;
|
|
2747 } while (compl != NULL && compl != compl_first_match);
|
657
|
2748 if (compl_match_arraysize == 0)
|
|
2749 return;
|
659
|
2750 compl_match_array = (pumitem_T *)alloc_clear(
|
|
2751 (unsigned)(sizeof(pumitem_T)
|
540
|
2752 * compl_match_arraysize));
|
|
2753 if (compl_match_array != NULL)
|
|
2754 {
|
829
|
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;
|
|
2759
|
540
|
2760 i = 0;
|
|
2761 compl = compl_first_match;
|
|
2762 do
|
|
2763 {
|
657
|
2764 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
|
|
2765 && (compl_leader == NULL
|
681
|
2766 || ins_compl_equal(compl, compl_leader, lead_len)))
|
540
|
2767 {
|
659
|
2768 if (!shown_match_ok)
|
|
2769 {
|
|
2770 if (compl == compl_shown_match || did_find_shown_match)
|
|
2771 {
|
|
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;
|
|
2777 }
|
|
2778 else
|
|
2779 /* Remember this displayed match for when the
|
|
2780 * shown match is just below it. */
|
|
2781 shown_compl = compl;
|
540
|
2782 cur = i;
|
659
|
2783 }
|
786
|
2784
|
|
2785 if (compl->cp_text[CPT_ABBR] != NULL)
|
|
2786 compl_match_array[i].pum_text =
|
|
2787 compl->cp_text[CPT_ABBR];
|
|
2788 else
|
|
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];
|
659
|
2795 else
|
|
2796 compl_match_array[i++].pum_extra = compl->cp_fname;
|
|
2797 }
|
|
2798
|
|
2799 if (compl == compl_shown_match)
|
|
2800 {
|
|
2801 did_find_shown_match = TRUE;
|
716
|
2802
|
|
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;
|
|
2807
|
659
|
2808 if (!shown_match_ok && shown_compl != NULL)
|
|
2809 {
|
|
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;
|
|
2814 }
|
540
|
2815 }
|
|
2816 compl = compl->cp_next;
|
|
2817 } while (compl != NULL && compl != compl_first_match);
|
659
|
2818
|
|
2819 if (!shown_match_ok) /* no displayed match at all */
|
|
2820 cur = -1;
|
540
|
2821 }
|
|
2822 }
|
|
2823 else
|
|
2824 {
|
|
2825 /* popup menu already exists, only need to find the current item.*/
|
657
|
2826 for (i = 0; i < compl_match_arraysize; ++i)
|
786
|
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])
|
829
|
2830 {
|
|
2831 cur = i;
|
657
|
2832 break;
|
829
|
2833 }
|
540
|
2834 }
|
|
2835
|
|
2836 if (compl_match_array != NULL)
|
|
2837 {
|
|
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;
|
732
|
2842 pum_display(compl_match_array, compl_match_arraysize, cur);
|
540
|
2843 curwin->w_cursor.col = col;
|
|
2844 }
|
|
2845 }
|
|
2846
|
7
|
2847 #define DICT_FIRST (1) /* use just first element in "dict" */
|
|
2848 #define DICT_EXACT (2) /* "dict" is the exact name of a file */
|
648
|
2849
|
7
|
2850 /*
|
703
|
2851 * Add any identifiers that match the given pattern in the list of dictionary
|
|
2852 * files "dict_start" to the list of completions.
|
7
|
2853 */
|
|
2854 static void
|
703
|
2855 ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
|
|
2856 char_u *dict_start;
|
7
|
2857 char_u *pat;
|
667
|
2858 int flags; /* DICT_FIRST and/or DICT_EXACT */
|
703
|
2859 int thesaurus; /* Thesaurus completion */
|
|
2860 {
|
|
2861 char_u *dict = dict_start;
|
7
|
2862 char_u *ptr;
|
|
2863 char_u *buf;
|
|
2864 regmatch_T regmatch;
|
|
2865 char_u **files;
|
|
2866 int count;
|
|
2867 int save_p_scs;
|
659
|
2868 int dir = compl_direction;
|
7
|
2869
|
703
|
2870 if (*dict == NUL)
|
|
2871 {
|
744
|
2872 #ifdef FEAT_SPELL
|
703
|
2873 /* When 'dictionary' is empty and spell checking is enabled use
|
|
2874 * "spell". */
|
|
2875 if (!thesaurus && curwin->w_p_spell)
|
|
2876 dict = (char_u *)"spell";
|
|
2877 else
|
|
2878 #endif
|
|
2879 return;
|
|
2880 }
|
|
2881
|
7
|
2882 buf = alloc(LSIZE);
|
703
|
2883 if (buf == NULL)
|
|
2884 return;
|
1074
|
2885 regmatch.regprog = NULL; /* so that we can goto theend */
|
703
|
2886
|
7
|
2887 /* If 'infercase' is set, don't use 'smartcase' here */
|
|
2888 save_p_scs = p_scs;
|
|
2889 if (curbuf->b_p_inf)
|
|
2890 p_scs = FALSE;
|
667
|
2891
|
|
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
|
842
|
2894 * pattern. Also need to double backslashes. */
|
667
|
2895 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
2896 {
|
842
|
2897 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
|
1869
|
2898 size_t len;
|
842
|
2899
|
|
2900 if (pat_esc == NULL)
|
1159
|
2901 goto theend;
|
1869
|
2902 len = STRLEN(pat_esc) + 10;
|
|
2903 ptr = alloc((unsigned)len);
|
667
|
2904 if (ptr == NULL)
|
842
|
2905 {
|
|
2906 vim_free(pat_esc);
|
1074
|
2907 goto theend;
|
842
|
2908 }
|
1869
|
2909 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
|
842
|
2910 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
|
|
2911 vim_free(pat_esc);
|
667
|
2912 vim_free(ptr);
|
|
2913 }
|
|
2914 else
|
703
|
2915 {
|
667
|
2916 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
|
703
|
2917 if (regmatch.regprog == NULL)
|
|
2918 goto theend;
|
|
2919 }
|
667
|
2920
|
7
|
2921 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
|
|
2922 regmatch.rm_ic = ignorecase(pat);
|
703
|
2923 while (*dict != NUL && !got_int && !compl_interrupted)
|
7
|
2924 {
|
|
2925 /* copy one dictionary file name into buf */
|
|
2926 if (flags == DICT_EXACT)
|
|
2927 {
|
|
2928 count = 1;
|
|
2929 files = &dict;
|
|
2930 }
|
|
2931 else
|
|
2932 {
|
|
2933 /* Expand wildcards in the dictionary name, but do not allow
|
|
2934 * backticks (for security, the 'dict' option may have been set in
|
|
2935 * a modeline). */
|
|
2936 copy_option_part(&dict, buf, LSIZE, ",");
|
744
|
2937 # ifdef FEAT_SPELL
|
703
|
2938 if (!thesaurus && STRCMP(buf, "spell") == 0)
|
|
2939 count = -1;
|
744
|
2940 else
|
|
2941 # endif
|
|
2942 if (vim_strchr(buf, '`') != NULL
|
7
|
2943 || expand_wildcards(1, &buf, &count, &files,
|
|
2944 EW_FILE|EW_SILENT) != OK)
|
|
2945 count = 0;
|
|
2946 }
|
|
2947
|
744
|
2948 # ifdef FEAT_SPELL
|
703
|
2949 if (count == -1)
|
|
2950 {
|
712
|
2951 /* Complete from active spelling. Skip "\<" in the pattern, we
|
|
2952 * don't use it as a RE. */
|
703
|
2953 if (pat[0] == '\\' && pat[1] == '<')
|
|
2954 ptr = pat + 2;
|
|
2955 else
|
|
2956 ptr = pat;
|
|
2957 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
|
|
2958 }
|
|
2959 else
|
744
|
2960 # endif
|
938
|
2961 if (count > 0) /* avoid warning for using "files" uninit */
|
703
|
2962 {
|
|
2963 ins_compl_files(count, files, thesaurus, flags,
|
|
2964 ®match, buf, &dir);
|
7
|
2965 if (flags != DICT_EXACT)
|
703
|
2966 FreeWild(count, files);
|
|
2967 }
|
|
2968 if (flags != 0)
|
7
|
2969 break;
|
|
2970 }
|
703
|
2971
|
|
2972 theend:
|
7
|
2973 p_scs = save_p_scs;
|
|
2974 vim_free(regmatch.regprog);
|
|
2975 vim_free(buf);
|
|
2976 }
|
|
2977
|
703
|
2978 static void
|
|
2979 ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
|
|
2980 int count;
|
|
2981 char_u **files;
|
|
2982 int thesaurus;
|
|
2983 int flags;
|
|
2984 regmatch_T *regmatch;
|
|
2985 char_u *buf;
|
|
2986 int *dir;
|
|
2987 {
|
|
2988 char_u *ptr;
|
|
2989 int i;
|
|
2990 FILE *fp;
|
|
2991 int add_r;
|
|
2992
|
|
2993 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
|
|
2994 {
|
|
2995 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
|
|
2996 if (flags != DICT_EXACT)
|
|
2997 {
|
|
2998 vim_snprintf((char *)IObuff, IOSIZE,
|
|
2999 _("Scanning dictionary: %s"), (char *)files[i]);
|
1869
|
3000 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
|
703
|
3001 }
|
|
3002
|
|
3003 if (fp != NULL)
|
|
3004 {
|
|
3005 /*
|
|
3006 * Read dictionary file line by line.
|
|
3007 * Check each line for a match.
|
|
3008 */
|
|
3009 while (!got_int && !compl_interrupted
|
|
3010 && !vim_fgets(buf, LSIZE, fp))
|
|
3011 {
|
|
3012 ptr = buf;
|
|
3013 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
|
|
3014 {
|
|
3015 ptr = regmatch->startp[0];
|
|
3016 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
3017 ptr = find_line_end(ptr);
|
|
3018 else
|
|
3019 ptr = find_word_end(ptr);
|
|
3020 add_r = ins_compl_add_infercase(regmatch->startp[0],
|
|
3021 (int)(ptr - regmatch->startp[0]),
|
942
|
3022 p_ic, files[i], *dir, 0);
|
703
|
3023 if (thesaurus)
|
|
3024 {
|
|
3025 char_u *wstart;
|
|
3026
|
|
3027 /*
|
|
3028 * Add the other matches on the line
|
|
3029 */
|
1353
|
3030 ptr = buf;
|
703
|
3031 while (!got_int)
|
|
3032 {
|
|
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)
|
|
3037 break;
|
|
3038 wstart = ptr;
|
|
3039
|
1353
|
3040 /* Find end of the word. */
|
703
|
3041 #ifdef FEAT_MBYTE
|
|
3042 if (has_mbyte)
|
|
3043 /* Japanese words may have characters in
|
|
3044 * different classes, only separate words
|
|
3045 * with single-byte non-word characters. */
|
|
3046 while (*ptr != NUL)
|
|
3047 {
|
|
3048 int l = (*mb_ptr2len)(ptr);
|
|
3049
|
|
3050 if (l < 2 && !vim_iswordc(*ptr))
|
|
3051 break;
|
|
3052 ptr += l;
|
|
3053 }
|
|
3054 else
|
|
3055 #endif
|
|
3056 ptr = find_word_end(ptr);
|
1353
|
3057
|
|
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);
|
703
|
3063 }
|
|
3064 }
|
|
3065 if (add_r == OK)
|
|
3066 /* if dir was BACKWARD then honor it just once */
|
|
3067 *dir = FORWARD;
|
|
3068 else if (add_r == FAIL)
|
|
3069 break;
|
|
3070 /* avoid expensive call to vim_regexec() when at end
|
|
3071 * of line */
|
|
3072 if (*ptr == '\n' || got_int)
|
|
3073 break;
|
|
3074 }
|
|
3075 line_breakcheck();
|
|
3076 ins_compl_check_keys(50);
|
|
3077 }
|
|
3078 fclose(fp);
|
|
3079 }
|
|
3080 }
|
|
3081 }
|
|
3082
|
7
|
3083 /*
|
|
3084 * Find the start of the next word.
|
|
3085 * Returns a pointer to the first char of the word. Also stops at a NUL.
|
|
3086 */
|
|
3087 char_u *
|
|
3088 find_word_start(ptr)
|
|
3089 char_u *ptr;
|
|
3090 {
|
|
3091 #ifdef FEAT_MBYTE
|
|
3092 if (has_mbyte)
|
|
3093 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
|
474
|
3094 ptr += (*mb_ptr2len)(ptr);
|
7
|
3095 else
|
|
3096 #endif
|
|
3097 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
|
|
3098 ++ptr;
|
|
3099 return ptr;
|
|
3100 }
|
|
3101
|
|
3102 /*
|
|
3103 * Find the end of the word. Assumes it starts inside a word.
|
|
3104 * Returns a pointer to just after the word.
|
|
3105 */
|
|
3106 char_u *
|
|
3107 find_word_end(ptr)
|
|
3108 char_u *ptr;
|
|
3109 {
|
|
3110 #ifdef FEAT_MBYTE
|
|
3111 int start_class;
|
|
3112
|
|
3113 if (has_mbyte)
|
|
3114 {
|
|
3115 start_class = mb_get_class(ptr);
|
|
3116 if (start_class > 1)
|
|
3117 while (*ptr != NUL)
|
|
3118 {
|
474
|
3119 ptr += (*mb_ptr2len)(ptr);
|
7
|
3120 if (mb_get_class(ptr) != start_class)
|
|
3121 break;
|
|
3122 }
|
|
3123 }
|
|
3124 else
|
|
3125 #endif
|
|
3126 while (vim_iswordc(*ptr))
|
|
3127 ++ptr;
|
|
3128 return ptr;
|
|
3129 }
|
|
3130
|
|
3131 /*
|
667
|
3132 * Find the end of the line, omitting CR and NL at the end.
|
|
3133 * Returns a pointer to just after the line.
|
|
3134 */
|
|
3135 static char_u *
|
|
3136 find_line_end(ptr)
|
|
3137 char_u *ptr;
|
|
3138 {
|
|
3139 char_u *s;
|
|
3140
|
|
3141 s = ptr + STRLEN(ptr);
|
|
3142 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
|
|
3143 --s;
|
|
3144 return s;
|
|
3145 }
|
|
3146
|
|
3147 /*
|
7
|
3148 * Free the list of completions
|
|
3149 */
|
|
3150 static void
|
|
3151 ins_compl_free()
|
|
3152 {
|
464
|
3153 compl_T *match;
|
786
|
3154 int i;
|
7
|
3155
|
449
|
3156 vim_free(compl_pattern);
|
|
3157 compl_pattern = NULL;
|
657
|
3158 vim_free(compl_leader);
|
|
3159 compl_leader = NULL;
|
449
|
3160
|
|
3161 if (compl_first_match == NULL)
|
7
|
3162 return;
|
540
|
3163
|
|
3164 ins_compl_del_pum();
|
|
3165 pum_clear();
|
|
3166
|
449
|
3167 compl_curr_match = compl_first_match;
|
7
|
3168 do
|
|
3169 {
|
449
|
3170 match = compl_curr_match;
|
464
|
3171 compl_curr_match = compl_curr_match->cp_next;
|
|
3172 vim_free(match->cp_str);
|
7
|
3173 /* several entries may use the same fname, free it just once. */
|
464
|
3174 if (match->cp_flags & FREE_FNAME)
|
|
3175 vim_free(match->cp_fname);
|
786
|
3176 for (i = 0; i < CPT_COUNT; ++i)
|
|
3177 vim_free(match->cp_text[i]);
|
7
|
3178 vim_free(match);
|
449
|
3179 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
|
|
3180 compl_first_match = compl_curr_match = NULL;
|
7
|
3181 }
|
|
3182
|
|
3183 static void
|
|
3184 ins_compl_clear()
|
|
3185 {
|
449
|
3186 compl_cont_status = 0;
|
|
3187 compl_started = FALSE;
|
|
3188 compl_matches = 0;
|
|
3189 vim_free(compl_pattern);
|
|
3190 compl_pattern = NULL;
|
657
|
3191 vim_free(compl_leader);
|
|
3192 compl_leader = NULL;
|
7
|
3193 edit_submode_extra = NULL;
|
724
|
3194 vim_free(compl_orig_text);
|
|
3195 compl_orig_text = NULL;
|
825
|
3196 compl_enter_selects = FALSE;
|
7
|
3197 }
|
|
3198
|
|
3199 /*
|
674
|
3200 * Return TRUE when Insert completion is active.
|
|
3201 */
|
|
3202 int
|
|
3203 ins_compl_active()
|
|
3204 {
|
|
3205 return compl_started;
|
|
3206 }
|
|
3207
|
|
3208 /*
|
659
|
3209 * Delete one character before the cursor and show the subset of the matches
|
|
3210 * that match the word that is now before the cursor.
|
836
|
3211 * Returns the character to be used, NUL if the work is done and another char
|
|
3212 * to be got from the user.
|
657
|
3213 */
|
|
3214 static int
|
|
3215 ins_compl_bs()
|
|
3216 {
|
|
3217 char_u *line;
|
|
3218 char_u *p;
|
|
3219
|
836
|
3220 line = ml_get_curline();
|
|
3221 p = line + curwin->w_cursor.col;
|
|
3222 mb_ptr_back(line, p);
|
|
3223
|
1430
|
3224 /* Stop completion when the whole word was deleted. For Omni completion
|
|
3225 * allow the word to be deleted, we won't match everything. */
|
|
3226 if ((int)(p - line) - (int)compl_col < 0
|
|
3227 || ((int)(p - line) - (int)compl_col == 0
|
|
3228 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
|
836
|
3229 return K_BS;
|
|
3230
|
874
|
3231 /* Deleted more than what was used to find matches or didn't finish
|
|
3232 * finding all matches: need to look for matches all over again. */
|
|
3233 if (curwin->w_cursor.col <= compl_col + compl_length
|
|
3234 || compl_was_interrupted)
|
|
3235 ins_compl_restart();
|
657
|
3236
|
|
3237 vim_free(compl_leader);
|
835
|
3238 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
|
657
|
3239 if (compl_leader != NULL)
|
|
3240 {
|
874
|
3241 ins_compl_new_leader();
|
|
3242 return NUL;
|
|
3243 }
|
|
3244 return K_BS;
|
|
3245 }
|
|
3246
|
|
3247 /*
|
|
3248 * Called after changing "compl_leader".
|
|
3249 * Show the popup menu with a different set of matches.
|
|
3250 * May also search for matches again if the previous search was interrupted.
|
|
3251 */
|
|
3252 static void
|
|
3253 ins_compl_new_leader()
|
|
3254 {
|
|
3255 ins_compl_del_pum();
|
|
3256 ins_compl_delete();
|
1782
|
3257 ins_bytes(compl_leader + ins_compl_len());
|
874
|
3258 compl_used_match = FALSE;
|
|
3259
|
|
3260 if (compl_started)
|
|
3261 ins_compl_set_original_text(compl_leader);
|
|
3262 else
|
|
3263 {
|
826
|
3264 #ifdef FEAT_SPELL
|
874
|
3265 spell_bad_len = 0; /* need to redetect bad word */
|
|
3266 #endif
|
|
3267 /*
|
|
3268 * Matches were cleared, need to search for them now. First display
|
|
3269 * the changed text before the cursor. Set "compl_restarting" to
|
|
3270 * avoid that the first match is inserted.
|
|
3271 */
|
|
3272 update_screen(0);
|
|
3273 #ifdef FEAT_GUI
|
|
3274 if (gui.in_use)
|
|
3275 {
|
|
3276 /* Show the cursor after the match, not after the redrawn text. */
|
|
3277 setcursor();
|
|
3278 out_flush();
|
|
3279 gui_update_cursor(FALSE, FALSE);
|
|
3280 }
|
|
3281 #endif
|
|
3282 compl_restarting = TRUE;
|
|
3283 if (ins_complete(Ctrl_N) == FAIL)
|
|
3284 compl_cont_status = 0;
|
|
3285 compl_restarting = FALSE;
|
|
3286 }
|
|
3287
|
887
|
3288 #if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
|
874
|
3289 if (!compl_used_match)
|
|
3290 {
|
829
|
3291 /* Go to the original text, since none of the matches is inserted. */
|
|
3292 if (compl_first_match->cp_prev != NULL
|
|
3293 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
|
|
3294 compl_shown_match = compl_first_match->cp_prev;
|
|
3295 else
|
|
3296 compl_shown_match = compl_first_match;
|
|
3297 compl_curr_match = compl_shown_match;
|
|
3298 compl_shows_dir = compl_direction;
|
874
|
3299 }
|
887
|
3300 #endif
|
|
3301 compl_enter_selects = !compl_used_match;
|
874
|
3302
|
|
3303 /* Show the popup menu with a different set of matches. */
|
|
3304 ins_compl_show_pum();
|
947
|
3305
|
|
3306 /* Don't let Enter select the original text when there is no popup menu. */
|
|
3307 if (compl_match_array == NULL)
|
|
3308 compl_enter_selects = FALSE;
|
657
|
3309 }
|
|
3310
|
|
3311 /*
|
1782
|
3312 * Return the length of the completion, from the completion start column to
|
|
3313 * the cursor column. Making sure it never goes below zero.
|
|
3314 */
|
|
3315 static int
|
|
3316 ins_compl_len()
|
|
3317 {
|
1869
|
3318 int off = (int)curwin->w_cursor.col - (int)compl_col;
|
1782
|
3319
|
|
3320 if (off < 0)
|
|
3321 return 0;
|
|
3322 return off;
|
|
3323 }
|
|
3324
|
|
3325 /*
|
657
|
3326 * Append one character to the match leader. May reduce the number of
|
|
3327 * matches.
|
|
3328 */
|
|
3329 static void
|
|
3330 ins_compl_addleader(c)
|
|
3331 int c;
|
|
3332 {
|
|
3333 #ifdef FEAT_MBYTE
|
|
3334 int cc;
|
|
3335
|
|
3336 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
|
|
3337 {
|
|
3338 char_u buf[MB_MAXBYTES + 1];
|
|
3339
|
|
3340 (*mb_char2bytes)(c, buf);
|
|
3341 buf[cc] = NUL;
|
|
3342 ins_char_bytes(buf, cc);
|
|
3343 }
|
|
3344 else
|
|
3345 #endif
|
|
3346 ins_char(c);
|
|
3347
|
874
|
3348 /* If we didn't complete finding matches we must search again. */
|
|
3349 if (compl_was_interrupted)
|
|
3350 ins_compl_restart();
|
|
3351
|
657
|
3352 vim_free(compl_leader);
|
|
3353 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
|
1869
|
3354 (int)(curwin->w_cursor.col - compl_col));
|
657
|
3355 if (compl_leader != NULL)
|
874
|
3356 ins_compl_new_leader();
|
|
3357 }
|
|
3358
|
|
3359 /*
|
|
3360 * Setup for finding completions again without leaving CTRL-X mode. Used when
|
|
3361 * BS or a key was typed while still searching for matches.
|
|
3362 */
|
|
3363 static void
|
|
3364 ins_compl_restart()
|
|
3365 {
|
|
3366 ins_compl_free();
|
|
3367 compl_started = FALSE;
|
|
3368 compl_matches = 0;
|
|
3369 compl_cont_status = 0;
|
|
3370 compl_cont_mode = 0;
|
694
|
3371 }
|
|
3372
|
|
3373 /*
|
|
3374 * Set the first match, the original text.
|
|
3375 */
|
|
3376 static void
|
|
3377 ins_compl_set_original_text(str)
|
|
3378 char_u *str;
|
|
3379 {
|
|
3380 char_u *p;
|
|
3381
|
|
3382 /* Replace the original text entry. */
|
|
3383 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
|
|
3384 {
|
|
3385 p = vim_strsave(str);
|
|
3386 if (p != NULL)
|
|
3387 {
|
|
3388 vim_free(compl_first_match->cp_str);
|
|
3389 compl_first_match->cp_str = p;
|
|
3390 }
|
657
|
3391 }
|
|
3392 }
|
|
3393
|
|
3394 /*
|
659
|
3395 * Append one character to the match leader. May reduce the number of
|
|
3396 * matches.
|
|
3397 */
|
|
3398 static void
|
|
3399 ins_compl_addfrommatch()
|
|
3400 {
|
|
3401 char_u *p;
|
1869
|
3402 int len = (int)curwin->w_cursor.col - (int)compl_col;
|
659
|
3403 int c;
|
887
|
3404 compl_T *cp;
|
659
|
3405
|
|
3406 p = compl_shown_match->cp_str;
|
677
|
3407 if ((int)STRLEN(p) <= len) /* the match is too short */
|
887
|
3408 {
|
|
3409 /* When still at the original match use the first entry that matches
|
|
3410 * the leader. */
|
|
3411 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
|
|
3412 {
|
|
3413 p = NULL;
|
|
3414 for (cp = compl_shown_match->cp_next; cp != NULL
|
|
3415 && cp != compl_first_match; cp = cp->cp_next)
|
|
3416 {
|
987
|
3417 if (compl_leader == NULL
|
|
3418 || ins_compl_equal(cp, compl_leader,
|
887
|
3419 (int)STRLEN(compl_leader)))
|
|
3420 {
|
|
3421 p = cp->cp_str;
|
|
3422 break;
|
|
3423 }
|
|
3424 }
|
|
3425 if (p == NULL || (int)STRLEN(p) <= len)
|
|
3426 return;
|
|
3427 }
|
|
3428 else
|
|
3429 return;
|
|
3430 }
|
659
|
3431 p += len;
|
|
3432 #ifdef FEAT_MBYTE
|
|
3433 c = mb_ptr2char(p);
|
|
3434 #else
|
|
3435 c = *p;
|
|
3436 #endif
|
|
3437 ins_compl_addleader(c);
|
|
3438 }
|
|
3439
|
|
3440 /*
|
7
|
3441 * Prepare for Insert mode completion, or stop it.
|
464
|
3442 * Called just after typing a character in Insert mode.
|
540
|
3443 * Returns TRUE when the character is not to be inserted;
|
7
|
3444 */
|
540
|
3445 static int
|
7
|
3446 ins_compl_prep(c)
|
|
3447 int c;
|
|
3448 {
|
|
3449 char_u *ptr;
|
|
3450 int want_cindent;
|
540
|
3451 int retval = FALSE;
|
7
|
3452
|
|
3453 /* Forget any previous 'special' messages if this is actually
|
|
3454 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
|
|
3455 */
|
|
3456 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
|
|
3457 edit_submode_extra = NULL;
|
|
3458
|
1434
|
3459 /* Ignore end of Select mode mapping and mouse scroll buttons. */
|
|
3460 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
|
540
|
3461 return retval;
|
7
|
3462
|
665
|
3463 /* Set "compl_get_longest" when finding the first matches. */
|
|
3464 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
|
|
3465 || (ctrl_x_mode == 0 && !compl_started))
|
|
3466 {
|
|
3467 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
|
|
3468 compl_used_match = TRUE;
|
|
3469 }
|
|
3470
|
7
|
3471 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
|
|
3472 {
|
|
3473 /*
|
|
3474 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
|
|
3475 * it will be yet. Now we decide.
|
|
3476 */
|
|
3477 switch (c)
|
|
3478 {
|
|
3479 case Ctrl_E:
|
|
3480 case Ctrl_Y:
|
|
3481 ctrl_x_mode = CTRL_X_SCROLL;
|
|
3482 if (!(State & REPLACE_FLAG))
|
|
3483 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
|
|
3484 else
|
|
3485 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
|
|
3486 edit_submode_pre = NULL;
|
|
3487 showmode();
|
|
3488 break;
|
|
3489 case Ctrl_L:
|
|
3490 ctrl_x_mode = CTRL_X_WHOLE_LINE;
|
|
3491 break;
|
|
3492 case Ctrl_F:
|
|
3493 ctrl_x_mode = CTRL_X_FILES;
|
|
3494 break;
|
|
3495 case Ctrl_K:
|
|
3496 ctrl_x_mode = CTRL_X_DICTIONARY;
|
|
3497 break;
|
|
3498 case Ctrl_R:
|
|
3499 /* Simply allow ^R to happen without affecting ^X mode */
|
|
3500 break;
|
|
3501 case Ctrl_T:
|
|
3502 ctrl_x_mode = CTRL_X_THESAURUS;
|
|
3503 break;
|
12
|
3504 #ifdef FEAT_COMPL_FUNC
|
|
3505 case Ctrl_U:
|
|
3506 ctrl_x_mode = CTRL_X_FUNCTION;
|
|
3507 break;
|
449
|
3508 case Ctrl_O:
|
523
|
3509 ctrl_x_mode = CTRL_X_OMNI;
|
449
|
3510 break;
|
502
|
3511 #endif
|
477
|
3512 case 's':
|
|
3513 case Ctrl_S:
|
|
3514 ctrl_x_mode = CTRL_X_SPELL;
|
744
|
3515 #ifdef FEAT_SPELL
|
819
|
3516 ++emsg_off; /* Avoid getting the E756 error twice. */
|
484
|
3517 spell_back_to_badword();
|
819
|
3518 --emsg_off;
|
484
|
3519 #endif
|
477
|
3520 break;
|
7
|
3521 case Ctrl_RSB:
|
|
3522 ctrl_x_mode = CTRL_X_TAGS;
|
|
3523 break;
|
|
3524 #ifdef FEAT_FIND_ID
|
|
3525 case Ctrl_I:
|
|
3526 case K_S_TAB:
|
|
3527 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
|
|
3528 break;
|
|
3529 case Ctrl_D:
|
|
3530 ctrl_x_mode = CTRL_X_PATH_DEFINES;
|
|
3531 break;
|
|
3532 #endif
|
|
3533 case Ctrl_V:
|
|
3534 case Ctrl_Q:
|
|
3535 ctrl_x_mode = CTRL_X_CMDLINE;
|
|
3536 break;
|
|
3537 case Ctrl_P:
|
|
3538 case Ctrl_N:
|
|
3539 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
|
|
3540 * just started ^X mode, or there were enough ^X's to cancel
|
|
3541 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
|
|
3542 * do normal expansion when interrupting a different mode (say
|
|
3543 * ^X^F^X^P or ^P^X^X^P, see below)
|
|
3544 * nothing changes if interrupting mode 0, (eg, the flag
|
|
3545 * doesn't change when going to ADDING mode -- Acevedo */
|
449
|
3546 if (!(compl_cont_status & CONT_INTRPT))
|
|
3547 compl_cont_status |= CONT_LOCAL;
|
|
3548 else if (compl_cont_mode != 0)
|
|
3549 compl_cont_status &= ~CONT_LOCAL;
|
7
|
3550 /* FALLTHROUGH */
|
|
3551 default:
|
449
|
3552 /* If we have typed at least 2 ^X's... for modes != 0, we set
|
|
3553 * compl_cont_status = 0 (eg, as if we had just started ^X
|
|
3554 * mode).
|
|
3555 * For mode 0, we set "compl_cont_mode" to an impossible
|
|
3556 * value, in both cases ^X^X can be used to restart the same
|
|
3557 * mode (avoiding ADDING mode).
|
|
3558 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
|
|
3559 * 'complete' and local ^P expansions respectively.
|
|
3560 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
|
|
3561 * mode -- Acevedo */
|
7
|
3562 if (c == Ctrl_X)
|
|
3563 {
|
449
|
3564 if (compl_cont_mode != 0)
|
|
3565 compl_cont_status = 0;
|
7
|
3566 else
|
449
|
3567 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
|
7
|
3568 }
|
|
3569 ctrl_x_mode = 0;
|
|
3570 edit_submode = NULL;
|
|
3571 showmode();
|
|
3572 break;
|
|
3573 }
|
|
3574 }
|
|
3575 else if (ctrl_x_mode != 0)
|
|
3576 {
|
|
3577 /* We're already in CTRL-X mode, do we stay in it? */
|
|
3578 if (!vim_is_ctrl_x_key(c))
|
|
3579 {
|
|
3580 if (ctrl_x_mode == CTRL_X_SCROLL)
|
|
3581 ctrl_x_mode = 0;
|
|
3582 else
|
|
3583 ctrl_x_mode = CTRL_X_FINISHED;
|
|
3584 edit_submode = NULL;
|
|
3585 }
|
|
3586 showmode();
|
|
3587 }
|
|
3588
|
449
|
3589 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
|
7
|
3590 {
|
|
3591 /* Show error message from attempted keyword completion (probably
|
|
3592 * 'Pattern not found') until another key is hit, then go back to
|
449
|
3593 * showing what mode we are in. */
|
7
|
3594 showmode();
|
644
|
3595 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
|
|
3596 && !ins_compl_pum_key(c))
|
7
|
3597 || ctrl_x_mode == CTRL_X_FINISHED)
|
|
3598 {
|
|
3599 /* Get here when we have finished typing a sequence of ^N and
|
|
3600 * ^P or other completion characters in CTRL-X mode. Free up
|
449
|
3601 * memory that was used, and make sure we can redo the insert. */
|
836
|
3602 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
|
7
|
3603 {
|
449
|
3604 char_u *p;
|
938
|
3605 int temp = 0;
|
449
|
3606
|
7
|
3607 /*
|
836
|
3608 * If any of the original typed text has been changed, eg when
|
|
3609 * ignorecase is set, we must add back-spaces to the redo
|
|
3610 * buffer. We add as few as necessary to delete just the part
|
|
3611 * of the original text that has changed.
|
|
3612 * When using the longest match, edited the match or used
|
|
3613 * CTRL-E then don't use the current match.
|
7
|
3614 */
|
836
|
3615 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
|
|
3616 ptr = compl_curr_match->cp_str;
|
|
3617 else if (compl_leader != NULL)
|
|
3618 ptr = compl_leader;
|
|
3619 else
|
|
3620 ptr = compl_orig_text;
|
897
|
3621 if (compl_orig_text != NULL)
|
|
3622 {
|
|
3623 p = compl_orig_text;
|
|
3624 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
|
|
3625 ++temp)
|
|
3626 ;
|
836
|
3627 #ifdef FEAT_MBYTE
|
897
|
3628 if (temp > 0)
|
|
3629 temp -= (*mb_head_off)(compl_orig_text, p + temp);
|
|
3630 #endif
|
|
3631 for (p += temp; *p != NUL; mb_ptr_adv(p))
|
|
3632 AppendCharToRedobuff(K_BS);
|
|
3633 }
|
|
3634 if (ptr != NULL)
|
|
3635 AppendToRedobuffLit(ptr + temp, -1);
|
7
|
3636 }
|
|
3637
|
|
3638 #ifdef FEAT_CINDENT
|
|
3639 want_cindent = (can_cindent && cindent_on());
|
|
3640 #endif
|
|
3641 /*
|
|
3642 * When completing whole lines: fix indent for 'cindent'.
|
|
3643 * Otherwise, break line if it's too long.
|
|
3644 */
|
449
|
3645 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
|
7
|
3646 {
|
|
3647 #ifdef FEAT_CINDENT
|
|
3648 /* re-indent the current line */
|
|
3649 if (want_cindent)
|
|
3650 {
|
|
3651 do_c_expr_indent();
|
|
3652 want_cindent = FALSE; /* don't do it again */
|
|
3653 }
|
|
3654 #endif
|
|
3655 }
|
|
3656 else
|
|
3657 {
|
1073
|
3658 int prev_col = curwin->w_cursor.col;
|
|
3659
|
7
|
3660 /* put the cursor on the last char, for 'tw' formatting */
|
1073
|
3661 if (prev_col > 0)
|
|
3662 dec_cursor();
|
7
|
3663 if (stop_arrow() == OK)
|
|
3664 insertchar(NUL, 0, -1);
|
1073
|
3665 if (prev_col > 0
|
|
3666 && ml_get_curline()[curwin->w_cursor.col] != NUL)
|
|
3667 inc_cursor();
|
7
|
3668 }
|
|
3669
|
816
|
3670 /* If the popup menu is displayed pressing CTRL-Y means accepting
|
825
|
3671 * the selection without inserting anything. When
|
|
3672 * compl_enter_selects is set the Enter key does the same. */
|
|
3673 if ((c == Ctrl_Y || (compl_enter_selects
|
|
3674 && (c == CAR || c == K_KENTER || c == NL)))
|
|
3675 && pum_visible())
|
540
|
3676 retval = TRUE;
|
|
3677
|
816
|
3678 /* CTRL-E means completion is Ended, go back to the typed text. */
|
|
3679 if (c == Ctrl_E)
|
|
3680 {
|
|
3681 ins_compl_delete();
|
|
3682 if (compl_leader != NULL)
|
1782
|
3683 ins_bytes(compl_leader + ins_compl_len());
|
816
|
3684 else if (compl_first_match != NULL)
|
1782
|
3685 ins_bytes(compl_orig_text + ins_compl_len());
|
816
|
3686 retval = TRUE;
|
|
3687 }
|
|
3688
|
1698
|
3689 auto_format(FALSE, TRUE);
|
|
3690
|
7
|
3691 ins_compl_free();
|
449
|
3692 compl_started = FALSE;
|
|
3693 compl_matches = 0;
|
7
|
3694 msg_clr_cmdline(); /* necessary for "noshowmode" */
|
|
3695 ctrl_x_mode = 0;
|
825
|
3696 compl_enter_selects = FALSE;
|
7
|
3697 if (edit_submode != NULL)
|
|
3698 {
|
|
3699 edit_submode = NULL;
|
|
3700 showmode();
|
|
3701 }
|
|
3702
|
|
3703 #ifdef FEAT_CINDENT
|
|
3704 /*
|
|
3705 * Indent now if a key was typed that is in 'cinkeys'.
|
|
3706 */
|
|
3707 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
|
|
3708 do_c_expr_indent();
|
|
3709 #endif
|
|
3710 }
|
|
3711 }
|
|
3712
|
|
3713 /* reset continue_* if we left expansion-mode, if we stay they'll be
|
|
3714 * (re)set properly in ins_complete() */
|
|
3715 if (!vim_is_ctrl_x_key(c))
|
|
3716 {
|
449
|
3717 compl_cont_status = 0;
|
|
3718 compl_cont_mode = 0;
|
7
|
3719 }
|
540
|
3720
|
|
3721 return retval;
|
7
|
3722 }
|
|
3723
|
|
3724 /*
|
|
3725 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
|
|
3726 * (depending on flag) starting from buf and looking for a non-scanned
|
|
3727 * buffer (other than curbuf). curbuf is special, if it is called with
|
|
3728 * buf=curbuf then it has to be the first call for a given flag/expansion.
|
|
3729 *
|
|
3730 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
|
|
3731 */
|
|
3732 static buf_T *
|
|
3733 ins_compl_next_buf(buf, flag)
|
|
3734 buf_T *buf;
|
|
3735 int flag;
|
|
3736 {
|
|
3737 #ifdef FEAT_WINDOWS
|
|
3738 static win_T *wp;
|
|
3739 #endif
|
|
3740
|
|
3741 if (flag == 'w') /* just windows */
|
|
3742 {
|
|
3743 #ifdef FEAT_WINDOWS
|
|
3744 if (buf == curbuf) /* first call for this flag/expansion */
|
|
3745 wp = curwin;
|
383
|
3746 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
|
7
|
3747 && wp->w_buffer->b_scanned)
|
|
3748 ;
|
|
3749 buf = wp->w_buffer;
|
|
3750 #else
|
|
3751 buf = curbuf;
|
|
3752 #endif
|
|
3753 }
|
|
3754 else
|
|
3755 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
|
|
3756 * (unlisted buffers)
|
|
3757 * When completing whole lines skip unloaded buffers. */
|
383
|
3758 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
|
7
|
3759 && ((flag == 'U'
|
|
3760 ? buf->b_p_bl
|
|
3761 : (!buf->b_p_bl
|
|
3762 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
|
667
|
3763 || buf->b_scanned))
|
7
|
3764 ;
|
|
3765 return buf;
|
|
3766 }
|
|
3767
|
12
|
3768 #ifdef FEAT_COMPL_FUNC
|
659
|
3769 static void expand_by_function __ARGS((int type, char_u *base));
|
12
|
3770
|
|
3771 /*
|
523
|
3772 * Execute user defined complete function 'completefunc' or 'omnifunc', and
|
502
|
3773 * get matches in "matches".
|
12
|
3774 */
|
659
|
3775 static void
|
|
3776 expand_by_function(type, base)
|
523
|
3777 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
|
12
|
3778 char_u *base;
|
|
3779 {
|
452
|
3780 list_T *matchlist;
|
502
|
3781 char_u *args[2];
|
|
3782 char_u *funcname;
|
|
3783 pos_T pos;
|
|
3784
|
|
3785 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
|
|
3786 if (*funcname == NUL)
|
659
|
3787 return;
|
452
|
3788
|
|
3789 /* Call 'completefunc' to obtain the list of matches. */
|
|
3790 args[0] = (char_u *)"0";
|
502
|
3791 args[1] = base;
|
|
3792
|
|
3793 pos = curwin->w_cursor;
|
|
3794 matchlist = call_func_retlist(funcname, 2, args, FALSE);
|
|
3795 curwin->w_cursor = pos; /* restore the cursor position */
|
452
|
3796 if (matchlist == NULL)
|
659
|
3797 return;
|
|
3798
|
724
|
3799 ins_compl_add_list(matchlist);
|
|
3800 list_unref(matchlist);
|
|
3801 }
|
|
3802 #endif /* FEAT_COMPL_FUNC */
|
|
3803
|
786
|
3804 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
|
724
|
3805 /*
|
|
3806 * Add completions from a list.
|
|
3807 */
|
|
3808 static void
|
|
3809 ins_compl_add_list(list)
|
|
3810 list_T *list;
|
|
3811 {
|
|
3812 listitem_T *li;
|
|
3813 int dir = compl_direction;
|
|
3814
|
659
|
3815 /* Go through the List with matches and add each of them. */
|
724
|
3816 for (li = list->lv_first; li != NULL; li = li->li_next)
|
452
|
3817 {
|
786
|
3818 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
|
|
3819 /* if dir was BACKWARD then honor it just once */
|
|
3820 dir = FORWARD;
|
648
|
3821 else if (did_emsg)
|
|
3822 break;
|
452
|
3823 }
|
724
|
3824 }
|
786
|
3825
|
|
3826 /*
|
|
3827 * Add a match to the list of matches from a typeval_T.
|
|
3828 * If the given string is already in the list of completions, then return
|
|
3829 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
|
|
3830 * maybe because alloc() returns NULL, then FAIL is returned.
|
|
3831 */
|
|
3832 int
|
|
3833 ins_compl_add_tv(tv, dir)
|
|
3834 typval_T *tv;
|
|
3835 int dir;
|
|
3836 {
|
|
3837 char_u *word;
|
867
|
3838 int icase = FALSE;
|
944
|
3839 int adup = FALSE;
|
786
|
3840 char_u *(cptext[CPT_COUNT]);
|
|
3841
|
|
3842 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
|
|
3843 {
|
|
3844 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
|
|
3845 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
|
|
3846 (char_u *)"abbr", FALSE);
|
|
3847 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
|
|
3848 (char_u *)"menu", FALSE);
|
|
3849 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
|
|
3850 (char_u *)"kind", FALSE);
|
|
3851 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
|
|
3852 (char_u *)"info", FALSE);
|
|
3853 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
|
|
3854 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
|
841
|
3855 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
|
944
|
3856 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
|
786
|
3857 }
|
|
3858 else
|
|
3859 {
|
|
3860 word = get_tv_string_chk(tv);
|
|
3861 vim_memset(cptext, 0, sizeof(cptext));
|
|
3862 }
|
|
3863 if (word == NULL || *word == NUL)
|
|
3864 return FAIL;
|
944
|
3865 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
|
786
|
3866 }
|
724
|
3867 #endif
|
12
|
3868
|
449
|
3869 /*
|
|
3870 * Get the next expansion(s), using "compl_pattern".
|
659
|
3871 * The search starts at position "ini" in curbuf and in the direction
|
|
3872 * compl_direction.
|
667
|
3873 * When "compl_started" is FALSE start at that position, otherwise continue
|
|
3874 * where we stopped searching before.
|
449
|
3875 * This may return before finding all the matches.
|
|
3876 * Return the total number of matches or -1 if still unknown -- Acevedo
|
7
|
3877 */
|
|
3878 static int
|
659
|
3879 ins_compl_get_exp(ini)
|
7
|
3880 pos_T *ini;
|
|
3881 {
|
|
3882 static pos_T first_match_pos;
|
|
3883 static pos_T last_match_pos;
|
|
3884 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
|
449
|
3885 static int found_all = FALSE; /* Found all matches of a
|
|
3886 certain type. */
|
|
3887 static buf_T *ins_buf = NULL; /* buffer being scanned */
|
7
|
3888
|
464
|
3889 pos_T *pos;
|
|
3890 char_u **matches;
|
|
3891 int save_p_scs;
|
|
3892 int save_p_ws;
|
|
3893 int save_p_ic;
|
|
3894 int i;
|
|
3895 int num_matches;
|
|
3896 int len;
|
|
3897 int found_new_match;
|
|
3898 int type = ctrl_x_mode;
|
|
3899 char_u *ptr;
|
|
3900 char_u *dict = NULL;
|
|
3901 int dict_f = 0;
|
|
3902 compl_T *old_match;
|
7
|
3903
|
449
|
3904 if (!compl_started)
|
7
|
3905 {
|
|
3906 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
|
|
3907 ins_buf->b_scanned = 0;
|
|
3908 found_all = FALSE;
|
|
3909 ins_buf = curbuf;
|
449
|
3910 e_cpt = (compl_cont_status & CONT_LOCAL)
|
12
|
3911 ? (char_u *)"." : curbuf->b_p_cpt;
|
7
|
3912 last_match_pos = first_match_pos = *ini;
|
|
3913 }
|
|
3914
|
449
|
3915 old_match = compl_curr_match; /* remember the last current match */
|
659
|
3916 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
|
7
|
3917 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
|
|
3918 for (;;)
|
|
3919 {
|
|
3920 found_new_match = FAIL;
|
|
3921
|
449
|
3922 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
|
7
|
3923 * or if found_all says this entry is done. For ^X^L only use the
|
|
3924 * entries from 'complete' that look in loaded buffers. */
|
|
3925 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
449
|
3926 && (!compl_started || found_all))
|
7
|
3927 {
|
|
3928 found_all = FALSE;
|
|
3929 while (*e_cpt == ',' || *e_cpt == ' ')
|
|
3930 e_cpt++;
|
|
3931 if (*e_cpt == '.' && !curbuf->b_scanned)
|
|
3932 {
|
|
3933 ins_buf = curbuf;
|
|
3934 first_match_pos = *ini;
|
|
3935 /* So that ^N can match word immediately after cursor */
|
|
3936 if (ctrl_x_mode == 0)
|
|
3937 dec(&first_match_pos);
|
|
3938 last_match_pos = first_match_pos;
|
|
3939 type = 0;
|
|
3940 }
|
|
3941 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
|
|
3942 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
|
|
3943 {
|
|
3944 /* Scan a buffer, but not the current one. */
|
|
3945 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
|
|
3946 {
|
449
|
3947 compl_started = TRUE;
|
7
|
3948 first_match_pos.col = last_match_pos.col = 0;
|
|
3949 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
|
|
3950 last_match_pos.lnum = 0;
|
|
3951 type = 0;
|
|
3952 }
|
|
3953 else /* unloaded buffer, scan like dictionary */
|
|
3954 {
|
|
3955 found_all = TRUE;
|
|
3956 if (ins_buf->b_fname == NULL)
|
|
3957 continue;
|
|
3958 type = CTRL_X_DICTIONARY;
|
|
3959 dict = ins_buf->b_fname;
|
|
3960 dict_f = DICT_EXACT;
|
|
3961 }
|
274
|
3962 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
|
7
|
3963 ins_buf->b_fname == NULL
|
|
3964 ? buf_spname(ins_buf)
|
|
3965 : ins_buf->b_sfname == NULL
|
|
3966 ? (char *)ins_buf->b_fname
|
|
3967 : (char *)ins_buf->b_sfname);
|
1869
|
3968 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
|
7
|
3969 }
|
|
3970 else if (*e_cpt == NUL)
|
|
3971 break;
|
|
3972 else
|
|
3973 {
|
|
3974 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
3975 type = -1;
|
|
3976 else if (*e_cpt == 'k' || *e_cpt == 's')
|
|
3977 {
|
|
3978 if (*e_cpt == 'k')
|
|
3979 type = CTRL_X_DICTIONARY;
|
|
3980 else
|
|
3981 type = CTRL_X_THESAURUS;
|
|
3982 if (*++e_cpt != ',' && *e_cpt != NUL)
|
|
3983 {
|
|
3984 dict = e_cpt;
|
|
3985 dict_f = DICT_FIRST;
|
|
3986 }
|
|
3987 }
|
|
3988 #ifdef FEAT_FIND_ID
|
|
3989 else if (*e_cpt == 'i')
|
|
3990 type = CTRL_X_PATH_PATTERNS;
|
|
3991 else if (*e_cpt == 'd')
|
|
3992 type = CTRL_X_PATH_DEFINES;
|
|
3993 #endif
|
|
3994 else if (*e_cpt == ']' || *e_cpt == 't')
|
|
3995 {
|
|
3996 type = CTRL_X_TAGS;
|
|
3997 sprintf((char*)IObuff, _("Scanning tags."));
|
1869
|
3998 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
|
7
|
3999 }
|
|
4000 else
|
|
4001 type = -1;
|
|
4002
|
|
4003 /* in any case e_cpt is advanced to the next entry */
|
|
4004 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
|
|
4005
|
|
4006 found_all = TRUE;
|
|
4007 if (type == -1)
|
|
4008 continue;
|
|
4009 }
|
|
4010 }
|
|
4011
|
|
4012 switch (type)
|
|
4013 {
|
|
4014 case -1:
|
|
4015 break;
|
|
4016 #ifdef FEAT_FIND_ID
|
|
4017 case CTRL_X_PATH_PATTERNS:
|
|
4018 case CTRL_X_PATH_DEFINES:
|
659
|
4019 find_pattern_in_path(compl_pattern, compl_direction,
|
449
|
4020 (int)STRLEN(compl_pattern), FALSE, FALSE,
|
7
|
4021 (type == CTRL_X_PATH_DEFINES
|
449
|
4022 && !(compl_cont_status & CONT_SOL))
|
7
|
4023 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
|
|
4024 (linenr_T)1, (linenr_T)MAXLNUM);
|
|
4025 break;
|
|
4026 #endif
|
|
4027
|
|
4028 case CTRL_X_DICTIONARY:
|
|
4029 case CTRL_X_THESAURUS:
|
|
4030 ins_compl_dictionaries(
|
703
|
4031 dict != NULL ? dict
|
7
|
4032 : (type == CTRL_X_THESAURUS
|
|
4033 ? (*curbuf->b_p_tsr == NUL
|
|
4034 ? p_tsr
|
|
4035 : curbuf->b_p_tsr)
|
|
4036 : (*curbuf->b_p_dict == NUL
|
|
4037 ? p_dict
|
|
4038 : curbuf->b_p_dict)),
|
659
|
4039 compl_pattern,
|
703
|
4040 dict != NULL ? dict_f
|
|
4041 : 0, type == CTRL_X_THESAURUS);
|
7
|
4042 dict = NULL;
|
|
4043 break;
|
|
4044
|
|
4045 case CTRL_X_TAGS:
|
|
4046 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
|
|
4047 save_p_ic = p_ic;
|
449
|
4048 p_ic = ignorecase(compl_pattern);
|
7
|
4049
|
|
4050 /* Find up to TAG_MANY matches. Avoids that an enourmous number
|
449
|
4051 * of matches is found when compl_pattern is empty */
|
|
4052 if (find_tags(compl_pattern, &num_matches, &matches,
|
7
|
4053 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
|
|
4054 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
|
|
4055 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
|
|
4056 {
|
942
|
4057 ins_compl_add_matches(num_matches, matches, p_ic);
|
7
|
4058 }
|
|
4059 p_ic = save_p_ic;
|
|
4060 break;
|
|
4061
|
|
4062 case CTRL_X_FILES:
|
449
|
4063 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
|
7
|
4064 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
|
|
4065 {
|
|
4066
|
|
4067 /* May change home directory back to "~". */
|
449
|
4068 tilde_replace(compl_pattern, num_matches, matches);
|
681
|
4069 ins_compl_add_matches(num_matches, matches,
|
|
4070 #ifdef CASE_INSENSITIVE_FILENAME
|
|
4071 TRUE
|
|
4072 #else
|
|
4073 FALSE
|
|
4074 #endif
|
|
4075 );
|
7
|
4076 }
|
|
4077 break;
|
|
4078
|
|
4079 case CTRL_X_CMDLINE:
|
449
|
4080 if (expand_cmdline(&compl_xp, compl_pattern,
|
|
4081 (int)STRLEN(compl_pattern),
|
7
|
4082 &num_matches, &matches) == EXPAND_OK)
|
681
|
4083 ins_compl_add_matches(num_matches, matches, FALSE);
|
7
|
4084 break;
|
|
4085
|
12
|
4086 #ifdef FEAT_COMPL_FUNC
|
|
4087 case CTRL_X_FUNCTION:
|
523
|
4088 case CTRL_X_OMNI:
|
659
|
4089 expand_by_function(type, compl_pattern);
|
12
|
4090 break;
|
|
4091 #endif
|
|
4092
|
477
|
4093 case CTRL_X_SPELL:
|
744
|
4094 #ifdef FEAT_SPELL
|
477
|
4095 num_matches = expand_spelling(first_match_pos.lnum,
|
|
4096 first_match_pos.col, compl_pattern, &matches);
|
|
4097 if (num_matches > 0)
|
942
|
4098 ins_compl_add_matches(num_matches, matches, p_ic);
|
477
|
4099 #endif
|
|
4100 break;
|
|
4101
|
7
|
4102 default: /* normal ^P/^N and ^X^L */
|
|
4103 /*
|
|
4104 * If 'infercase' is set, don't use 'smartcase' here
|
|
4105 */
|
|
4106 save_p_scs = p_scs;
|
|
4107 if (ins_buf->b_p_inf)
|
|
4108 p_scs = FALSE;
|
449
|
4109
|
7
|
4110 /* buffers other than curbuf are scanned from the beginning or the
|
|
4111 * end but never from the middle, thus setting nowrapscan in this
|
|
4112 * buffers is a good idea, on the other hand, we always set
|
|
4113 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
|
|
4114 save_p_ws = p_ws;
|
|
4115 if (ins_buf != curbuf)
|
|
4116 p_ws = FALSE;
|
|
4117 else if (*e_cpt == '.')
|
|
4118 p_ws = TRUE;
|
|
4119 for (;;)
|
|
4120 {
|
464
|
4121 int flags = 0;
|
7
|
4122
|
1007
|
4123 ++msg_silent; /* Don't want messages for wrapscan. */
|
|
4124
|
540
|
4125 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
|
|
4126 * has added a word that was at the beginning of the line */
|
7
|
4127 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
|
449
|
4128 || (compl_cont_status & CONT_SOL))
|
7
|
4129 found_new_match = search_for_exact_line(ins_buf, pos,
|
659
|
4130 compl_direction, compl_pattern);
|
7
|
4131 else
|
659
|
4132 found_new_match = searchit(NULL, ins_buf, pos,
|
|
4133 compl_direction,
|
449
|
4134 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
|
1496
|
4135 RE_LAST, (linenr_T)0, NULL);
|
1007
|
4136 --msg_silent;
|
449
|
4137 if (!compl_started)
|
7
|
4138 {
|
667
|
4139 /* set "compl_started" even on fail */
|
449
|
4140 compl_started = TRUE;
|
7
|
4141 first_match_pos = *pos;
|
|
4142 last_match_pos = *pos;
|
|
4143 }
|
|
4144 else if (first_match_pos.lnum == last_match_pos.lnum
|
665
|
4145 && first_match_pos.col == last_match_pos.col)
|
7
|
4146 found_new_match = FAIL;
|
|
4147 if (found_new_match == FAIL)
|
|
4148 {
|
|
4149 if (ins_buf == curbuf)
|
|
4150 found_all = TRUE;
|
|
4151 break;
|
|
4152 }
|
|
4153
|
|
4154 /* when ADDING, the text before the cursor matches, skip it */
|
449
|
4155 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
|
7
|
4156 && ini->lnum == pos->lnum
|
|
4157 && ini->col == pos->col)
|
|
4158 continue;
|
|
4159 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
|
|
4160 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
4161 {
|
449
|
4162 if (compl_cont_status & CONT_ADDING)
|
7
|
4163 {
|
|
4164 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
|
|
4165 continue;
|
|
4166 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
|
|
4167 if (!p_paste)
|
|
4168 ptr = skipwhite(ptr);
|
|
4169 }
|
|
4170 len = (int)STRLEN(ptr);
|
|
4171 }
|
|
4172 else
|
|
4173 {
|
449
|
4174 char_u *tmp_ptr = ptr;
|
|
4175
|
|
4176 if (compl_cont_status & CONT_ADDING)
|
7
|
4177 {
|
449
|
4178 tmp_ptr += compl_length;
|
7
|
4179 /* Skip if already inside a word. */
|
|
4180 if (vim_iswordp(tmp_ptr))
|
|
4181 continue;
|
|
4182 /* Find start of next word. */
|
|
4183 tmp_ptr = find_word_start(tmp_ptr);
|
|
4184 }
|
|
4185 /* Find end of this word. */
|
|
4186 tmp_ptr = find_word_end(tmp_ptr);
|
|
4187 len = (int)(tmp_ptr - ptr);
|
|
4188
|
449
|
4189 if ((compl_cont_status & CONT_ADDING)
|
|
4190 && len == compl_length)
|
7
|
4191 {
|
|
4192 if (pos->lnum < ins_buf->b_ml.ml_line_count)
|
|
4193 {
|
|
4194 /* Try next line, if any. the new word will be
|
|
4195 * "join" as if the normal command "J" was used.
|
|
4196 * IOSIZE is always greater than
|
449
|
4197 * compl_length, so the next STRNCPY always
|
7
|
4198 * works -- Acevedo */
|
|
4199 STRNCPY(IObuff, ptr, len);
|
|
4200 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
|
|
4201 tmp_ptr = ptr = skipwhite(ptr);
|
|
4202 /* Find start of next word. */
|
|
4203 tmp_ptr = find_word_start(tmp_ptr);
|
|
4204 /* Find end of next word. */
|
|
4205 tmp_ptr = find_word_end(tmp_ptr);
|
|
4206 if (tmp_ptr > ptr)
|
|
4207 {
|
419
|
4208 if (*ptr != ')' && IObuff[len - 1] != TAB)
|
7
|
4209 {
|
419
|
4210 if (IObuff[len - 1] != ' ')
|
7
|
4211 IObuff[len++] = ' ';
|
|
4212 /* IObuf =~ "\k.* ", thus len >= 2 */
|
|
4213 if (p_js
|
419
|
4214 && (IObuff[len - 2] == '.'
|
7
|
4215 || (vim_strchr(p_cpo, CPO_JOINSP)
|
|
4216 == NULL
|
419
|
4217 && (IObuff[len - 2] == '?'
|
|
4218 || IObuff[len - 2] == '!'))))
|
7
|
4219 IObuff[len++] = ' ';
|
|
4220 }
|
|
4221 /* copy as much as posible of the new word */
|
|
4222 if (tmp_ptr - ptr >= IOSIZE - len)
|
|
4223 tmp_ptr = ptr + IOSIZE - len - 1;
|
|
4224 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
|
|
4225 len += (int)(tmp_ptr - ptr);
|
464
|
4226 flags |= CONT_S_IPOS;
|
7
|
4227 }
|
|
4228 IObuff[len] = NUL;
|
|
4229 ptr = IObuff;
|
|
4230 }
|
449
|
4231 if (len == compl_length)
|
7
|
4232 continue;
|
|
4233 }
|
|
4234 }
|
942
|
4235 if (ins_compl_add_infercase(ptr, len, p_ic,
|
540
|
4236 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
|
659
|
4237 0, flags) != NOTDONE)
|
7
|
4238 {
|
|
4239 found_new_match = OK;
|
|
4240 break;
|
|
4241 }
|
|
4242 }
|
|
4243 p_scs = save_p_scs;
|
|
4244 p_ws = save_p_ws;
|
|
4245 }
|
540
|
4246
|
449
|
4247 /* check if compl_curr_match has changed, (e.g. other type of
|
1796
|
4248 * expansion added something) */
|
540
|
4249 if (type != 0 && compl_curr_match != old_match)
|
7
|
4250 found_new_match = OK;
|
|
4251
|
|
4252 /* break the loop for specialized modes (use 'complete' just for the
|
|
4253 * generic ctrl_x_mode == 0) or when we've found a new match */
|
|
4254 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
|
449
|
4255 || found_new_match != FAIL)
|
540
|
4256 {
|
|
4257 if (got_int)
|
|
4258 break;
|
665
|
4259 /* Fill the popup menu as soon as possible. */
|
989
|
4260 if (type != -1)
|
540
|
4261 ins_compl_check_keys(0);
|
665
|
4262
|
540
|
4263 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
|
|
4264 || compl_interrupted)
|
|
4265 break;
|
|
4266 compl_started = TRUE;
|
|
4267 }
|
|
4268 else
|
|
4269 {
|
|
4270 /* Mark a buffer scanned when it has been scanned completely */
|
|
4271 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
|
|
4272 ins_buf->b_scanned = TRUE;
|
|
4273
|
|
4274 compl_started = FALSE;
|
|
4275 }
|
449
|
4276 }
|
|
4277 compl_started = TRUE;
|
7
|
4278
|
|
4279 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
4280 && *e_cpt == NUL) /* Got to end of 'complete' */
|
|
4281 found_new_match = FAIL;
|
|
4282
|
|
4283 i = -1; /* total of matches, unknown */
|
|
4284 if (found_new_match == FAIL
|
|
4285 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
|
|
4286 i = ins_compl_make_cyclic();
|
|
4287
|
|
4288 /* If several matches were added (FORWARD) or the search failed and has
|
449
|
4289 * just been made cyclic then we have to move compl_curr_match to the next
|
|
4290 * or previous entry (if any) -- Acevedo */
|
724
|
4291 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
|
|
4292 : old_match->cp_prev;
|
449
|
4293 if (compl_curr_match == NULL)
|
|
4294 compl_curr_match = old_match;
|
7
|
4295 return i;
|
|
4296 }
|
|
4297
|
|
4298 /* Delete the old text being completed. */
|
|
4299 static void
|
|
4300 ins_compl_delete()
|
|
4301 {
|
|
4302 int i;
|
|
4303
|
|
4304 /*
|
|
4305 * In insert mode: Delete the typed part.
|
|
4306 * In replace mode: Put the old characters back, if any.
|
|
4307 */
|
449
|
4308 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
|
7
|
4309 backspace_until_column(i);
|
|
4310 changed_cline_bef_curs();
|
|
4311 }
|
|
4312
|
|
4313 /* Insert the new text being completed. */
|
|
4314 static void
|
|
4315 ins_compl_insert()
|
|
4316 {
|
1782
|
4317 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
|
683
|
4318 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
|
|
4319 compl_used_match = FALSE;
|
|
4320 else
|
|
4321 compl_used_match = TRUE;
|
7
|
4322 }
|
|
4323
|
|
4324 /*
|
|
4325 * Fill in the next completion in the current direction.
|
464
|
4326 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
|
|
4327 * get more completions. If it is FALSE, then we just do nothing when there
|
|
4328 * are no more completions in a given direction. The latter case is used when
|
|
4329 * we are still in the middle of finding completions, to allow browsing
|
|
4330 * through the ones found so far.
|
7
|
4331 * Return the total number of matches, or -1 if still unknown -- webb.
|
|
4332 *
|
449
|
4333 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
|
|
4334 * compl_shown_match here.
|
7
|
4335 *
|
|
4336 * Note that this function may be called recursively once only. First with
|
464
|
4337 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
|
|
4338 * calls this function with "allow_get_expansion" FALSE.
|
7
|
4339 */
|
|
4340 static int
|
665
|
4341 ins_compl_next(allow_get_expansion, count, insert_match)
|
7
|
4342 int allow_get_expansion;
|
610
|
4343 int count; /* repeat completion this many times; should
|
|
4344 be at least 1 */
|
665
|
4345 int insert_match; /* Insert the newly selected match */
|
7
|
4346 {
|
|
4347 int num_matches = -1;
|
|
4348 int i;
|
610
|
4349 int todo = count;
|
657
|
4350 compl_T *found_compl = NULL;
|
|
4351 int found_end = FALSE;
|
836
|
4352 int advance;
|
7
|
4353
|
665
|
4354 if (compl_leader != NULL
|
|
4355 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
|
|
4356 {
|
|
4357 /* Set "compl_shown_match" to the actually shown match, it may differ
|
|
4358 * when "compl_leader" is used to omit some of the matches. */
|
681
|
4359 while (!ins_compl_equal(compl_shown_match,
|
835
|
4360 compl_leader, (int)STRLEN(compl_leader))
|
665
|
4361 && compl_shown_match->cp_next != NULL
|
|
4362 && compl_shown_match->cp_next != compl_first_match)
|
|
4363 compl_shown_match = compl_shown_match->cp_next;
|
887
|
4364
|
|
4365 /* If we didn't find it searching forward, and compl_shows_dir is
|
|
4366 * backward, find the last match. */
|
|
4367 if (compl_shows_dir == BACKWARD
|
|
4368 && !ins_compl_equal(compl_shown_match,
|
|
4369 compl_leader, (int)STRLEN(compl_leader))
|
|
4370 && (compl_shown_match->cp_next == NULL
|
|
4371 || compl_shown_match->cp_next == compl_first_match))
|
|
4372 {
|
|
4373 while (!ins_compl_equal(compl_shown_match,
|
|
4374 compl_leader, (int)STRLEN(compl_leader))
|
|
4375 && compl_shown_match->cp_prev != NULL
|
|
4376 && compl_shown_match->cp_prev != compl_first_match)
|
|
4377 compl_shown_match = compl_shown_match->cp_prev;
|
|
4378 }
|
665
|
4379 }
|
|
4380
|
|
4381 if (allow_get_expansion && insert_match
|
874
|
4382 && (!(compl_get_longest || compl_restarting) || compl_used_match))
|
7
|
4383 /* Delete old text to be replaced */
|
|
4384 ins_compl_delete();
|
665
|
4385
|
836
|
4386 /* When finding the longest common text we stick at the original text,
|
|
4387 * don't let CTRL-N or CTRL-P move to the first match. */
|
|
4388 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
|
|
4389
|
874
|
4390 /* When restarting the search don't insert the first match either. */
|
|
4391 if (compl_restarting)
|
|
4392 {
|
|
4393 advance = FALSE;
|
|
4394 compl_restarting = FALSE;
|
|
4395 }
|
|
4396
|
610
|
4397 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
|
|
4398 * around. */
|
|
4399 while (--todo >= 0)
|
|
4400 {
|
|
4401 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
|
|
4402 {
|
|
4403 compl_shown_match = compl_shown_match->cp_next;
|
657
|
4404 found_end = (compl_first_match != NULL
|
|
4405 && (compl_shown_match->cp_next == compl_first_match
|
|
4406 || compl_shown_match == compl_first_match));
|
610
|
4407 }
|
|
4408 else if (compl_shows_dir == BACKWARD
|
|
4409 && compl_shown_match->cp_prev != NULL)
|
|
4410 {
|
657
|
4411 found_end = (compl_shown_match == compl_first_match);
|
610
|
4412 compl_shown_match = compl_shown_match->cp_prev;
|
657
|
4413 found_end |= (compl_shown_match == compl_first_match);
|
7
|
4414 }
|
|
4415 else
|
610
|
4416 {
|
909
|
4417 if (!allow_get_expansion)
|
|
4418 {
|
|
4419 if (advance)
|
|
4420 {
|
|
4421 if (compl_shows_dir == BACKWARD)
|
|
4422 compl_pending -= todo + 1;
|
|
4423 else
|
|
4424 compl_pending += todo + 1;
|
|
4425 }
|
|
4426 return -1;
|
|
4427 }
|
|
4428
|
836
|
4429 if (advance)
|
|
4430 {
|
|
4431 if (compl_shows_dir == BACKWARD)
|
|
4432 --compl_pending;
|
|
4433 else
|
|
4434 ++compl_pending;
|
|
4435 }
|
657
|
4436
|
874
|
4437 /* Find matches. */
|
659
|
4438 num_matches = ins_compl_get_exp(&compl_startpos);
|
909
|
4439
|
|
4440 /* handle any pending completions */
|
|
4441 while (compl_pending != 0 && compl_direction == compl_shows_dir
|
836
|
4442 && advance)
|
909
|
4443 {
|
|
4444 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
|
|
4445 {
|
|
4446 compl_shown_match = compl_shown_match->cp_next;
|
|
4447 --compl_pending;
|
|
4448 }
|
|
4449 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
|
|
4450 {
|
|
4451 compl_shown_match = compl_shown_match->cp_prev;
|
|
4452 ++compl_pending;
|
|
4453 }
|
|
4454 else
|
|
4455 break;
|
|
4456 }
|
657
|
4457 found_end = FALSE;
|
|
4458 }
|
|
4459 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
|
|
4460 && compl_leader != NULL
|
681
|
4461 && !ins_compl_equal(compl_shown_match,
|
835
|
4462 compl_leader, (int)STRLEN(compl_leader)))
|
657
|
4463 ++todo;
|
|
4464 else
|
|
4465 /* Remember a matching item. */
|
|
4466 found_compl = compl_shown_match;
|
|
4467
|
|
4468 /* Stop at the end of the list when we found a usable match. */
|
|
4469 if (found_end)
|
|
4470 {
|
|
4471 if (found_compl != NULL)
|
610
|
4472 {
|
657
|
4473 compl_shown_match = found_compl;
|
|
4474 break;
|
610
|
4475 }
|
657
|
4476 todo = 1; /* use first usable match after wrapping around */
|
610
|
4477 }
|
7
|
4478 }
|
|
4479
|
665
|
4480 /* Insert the text of the new completion, or the compl_leader. */
|
|
4481 if (insert_match)
|
|
4482 {
|
|
4483 if (!compl_get_longest || compl_used_match)
|
|
4484 ins_compl_insert();
|
|
4485 else
|
1782
|
4486 ins_bytes(compl_leader + ins_compl_len());
|
665
|
4487 }
|
|
4488 else
|
|
4489 compl_used_match = FALSE;
|
7
|
4490
|
|
4491 if (!allow_get_expansion)
|
|
4492 {
|
540
|
4493 /* may undisplay the popup menu first */
|
|
4494 ins_compl_upd_pum();
|
|
4495
|
665
|
4496 /* redraw to show the user what was inserted */
|
|
4497 update_screen(0);
|
|
4498
|
540
|
4499 /* display the updated popup menu */
|
|
4500 ins_compl_show_pum();
|
864
|
4501 #ifdef FEAT_GUI
|
|
4502 if (gui.in_use)
|
|
4503 {
|
|
4504 /* Show the cursor after the match, not after the redrawn text. */
|
|
4505 setcursor();
|
|
4506 out_flush();
|
|
4507 gui_update_cursor(FALSE, FALSE);
|
|
4508 }
|
|
4509 #endif
|
540
|
4510
|
7
|
4511 /* Delete old text to be replaced, since we're still searching and
|
|
4512 * don't want to match ourselves! */
|
|
4513 ins_compl_delete();
|
|
4514 }
|
|
4515
|
825
|
4516 /* Enter will select a match when the match wasn't inserted and the popup
|
1219
|
4517 * menu is visible. */
|
825
|
4518 compl_enter_selects = !insert_match && compl_match_array != NULL;
|
|
4519
|
7
|
4520 /*
|
|
4521 * Show the file name for the match (if any)
|
|
4522 * Truncate the file name to avoid a wait for return.
|
|
4523 */
|
464
|
4524 if (compl_shown_match->cp_fname != NULL)
|
7
|
4525 {
|
|
4526 STRCPY(IObuff, "match in file ");
|
464
|
4527 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
|
7
|
4528 if (i <= 0)
|
|
4529 i = 0;
|
|
4530 else
|
|
4531 STRCAT(IObuff, "<");
|
464
|
4532 STRCAT(IObuff, compl_shown_match->cp_fname + i);
|
7
|
4533 msg(IObuff);
|
|
4534 redraw_cmdline = FALSE; /* don't overwrite! */
|
|
4535 }
|
|
4536
|
|
4537 return num_matches;
|
|
4538 }
|
|
4539
|
|
4540 /*
|
|
4541 * Call this while finding completions, to check whether the user has hit a key
|
|
4542 * that should change the currently displayed completion, or exit completion
|
716
|
4543 * mode. Also, when compl_pending is not zero, show a completion as soon as
|
7
|
4544 * possible. -- webb
|
464
|
4545 * "frequency" specifies out of how many calls we actually check.
|
7
|
4546 */
|
|
4547 void
|
464
|
4548 ins_compl_check_keys(frequency)
|
|
4549 int frequency;
|
7
|
4550 {
|
|
4551 static int count = 0;
|
|
4552
|
|
4553 int c;
|
|
4554
|
|
4555 /* Don't check when reading keys from a script. That would break the test
|
|
4556 * scripts */
|
|
4557 if (using_script())
|
|
4558 return;
|
|
4559
|
|
4560 /* Only do this at regular intervals */
|
464
|
4561 if (++count < frequency)
|
7
|
4562 return;
|
|
4563 count = 0;
|
|
4564
|
909
|
4565 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
|
|
4566 * can't do its work correctly. */
|
7
|
4567 c = vpeekc_any();
|
|
4568 if (c != NUL)
|
|
4569 {
|
|
4570 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
|
|
4571 {
|
|
4572 c = safe_vgetc(); /* Eat the character */
|
610
|
4573 compl_shows_dir = ins_compl_key2dir(c);
|
665
|
4574 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
|
|
4575 c != K_UP && c != K_DOWN);
|
7
|
4576 }
|
909
|
4577 else
|
|
4578 {
|
|
4579 /* Need to get the character to have KeyTyped set. We'll put it
|
1526
|
4580 * back with vungetc() below. But skip K_IGNORE. */
|
909
|
4581 c = safe_vgetc();
|
1526
|
4582 if (c != K_IGNORE)
|
|
4583 {
|
|
4584 /* Don't interrupt completion when the character wasn't typed,
|
|
4585 * e.g., when doing @q to replay keys. */
|
|
4586 if (c != Ctrl_R && KeyTyped)
|
|
4587 compl_interrupted = TRUE;
|
|
4588
|
|
4589 vungetc(c);
|
|
4590 }
|
909
|
4591 }
|
449
|
4592 }
|
716
|
4593 if (compl_pending != 0 && !got_int)
|
909
|
4594 {
|
|
4595 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
|
|
4596
|
|
4597 compl_pending = 0;
|
|
4598 (void)ins_compl_next(FALSE, todo, TRUE);
|
|
4599 }
|
610
|
4600 }
|
|
4601
|
|
4602 /*
|
|
4603 * Decide the direction of Insert mode complete from the key typed.
|
|
4604 * Returns BACKWARD or FORWARD.
|
|
4605 */
|
|
4606 static int
|
|
4607 ins_compl_key2dir(c)
|
|
4608 int c;
|
|
4609 {
|
665
|
4610 if (c == Ctrl_P || c == Ctrl_L
|
|
4611 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
|
|
4612 || c == K_S_UP || c == K_UP)))
|
610
|
4613 return BACKWARD;
|
|
4614 return FORWARD;
|
|
4615 }
|
|
4616
|
|
4617 /*
|
644
|
4618 * Return TRUE for keys that are used for completion only when the popup menu
|
|
4619 * is visible.
|
|
4620 */
|
|
4621 static int
|
|
4622 ins_compl_pum_key(c)
|
|
4623 int c;
|
|
4624 {
|
|
4625 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
|
665
|
4626 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
|
|
4627 || c == K_UP || c == K_DOWN);
|
644
|
4628 }
|
|
4629
|
|
4630 /*
|
610
|
4631 * Decide the number of completions to move forward.
|
|
4632 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
|
|
4633 */
|
|
4634 static int
|
|
4635 ins_compl_key2count(c)
|
|
4636 int c;
|
|
4637 {
|
|
4638 int h;
|
|
4639
|
665
|
4640 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
|
610
|
4641 {
|
|
4642 h = pum_get_height();
|
|
4643 if (h > 3)
|
|
4644 h -= 2; /* keep some context */
|
|
4645 return h;
|
|
4646 }
|
|
4647 return 1;
|
7
|
4648 }
|
|
4649
|
|
4650 /*
|
681
|
4651 * Return TRUE if completion with "c" should insert the match, FALSE if only
|
|
4652 * to change the currently selected completion.
|
|
4653 */
|
|
4654 static int
|
|
4655 ins_compl_use_match(c)
|
|
4656 int c;
|
|
4657 {
|
|
4658 switch (c)
|
|
4659 {
|
|
4660 case K_UP:
|
|
4661 case K_DOWN:
|
|
4662 case K_PAGEDOWN:
|
|
4663 case K_KPAGEDOWN:
|
|
4664 case K_S_DOWN:
|
|
4665 case K_PAGEUP:
|
|
4666 case K_KPAGEUP:
|
|
4667 case K_S_UP:
|
|
4668 return FALSE;
|
|
4669 }
|
|
4670 return TRUE;
|
|
4671 }
|
|
4672
|
|
4673 /*
|
7
|
4674 * Do Insert mode completion.
|
|
4675 * Called when character "c" was typed, which has a meaning for completion.
|
|
4676 * Returns OK if completion was done, FAIL if something failed (out of mem).
|
|
4677 */
|
|
4678 static int
|
|
4679 ins_complete(c)
|
449
|
4680 int c;
|
7
|
4681 {
|
449
|
4682 char_u *line;
|
|
4683 int startcol = 0; /* column where searched text starts */
|
|
4684 colnr_T curs_col; /* cursor column */
|
|
4685 int n;
|
7
|
4686
|
610
|
4687 compl_direction = ins_compl_key2dir(c);
|
449
|
4688 if (!compl_started)
|
7
|
4689 {
|
|
4690 /* First time we hit ^N or ^P (in a row, I mean) */
|
|
4691
|
|
4692 did_ai = FALSE;
|
|
4693 #ifdef FEAT_SMARTINDENT
|
|
4694 did_si = FALSE;
|
|
4695 can_si = FALSE;
|
|
4696 can_si_back = FALSE;
|
|
4697 #endif
|
|
4698 if (stop_arrow() == FAIL)
|
|
4699 return FAIL;
|
|
4700
|
|
4701 line = ml_get(curwin->w_cursor.lnum);
|
449
|
4702 curs_col = curwin->w_cursor.col;
|
716
|
4703 compl_pending = 0;
|
7
|
4704
|
1430
|
4705 /* If this same ctrl_x_mode has been interrupted use the text from
|
449
|
4706 * "compl_startpos" to the cursor as a pattern to add a new word
|
|
4707 * instead of expand the one before the cursor, in word-wise if
|
1430
|
4708 * "compl_startpos" is not in the same line as the cursor then fix it
|
|
4709 * (the line has been split because it was longer than 'tw'). if SOL
|
|
4710 * is set then skip the previous pattern, a word at the beginning of
|
|
4711 * the line has been inserted, we'll look for that -- Acevedo. */
|
665
|
4712 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
|
|
4713 && compl_cont_mode == ctrl_x_mode)
|
7
|
4714 {
|
|
4715 /*
|
|
4716 * it is a continued search
|
|
4717 */
|
449
|
4718 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
|
7
|
4719 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
|
|
4720 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
|
|
4721 {
|
449
|
4722 if (compl_startpos.lnum != curwin->w_cursor.lnum)
|
7
|
4723 {
|
449
|
4724 /* line (probably) wrapped, set compl_startpos to the
|
|
4725 * first non_blank in the line, if it is not a wordchar
|
|
4726 * include it to get a better pattern, but then we don't
|
|
4727 * want the "\\<" prefix, check it bellow */
|
|
4728 compl_col = (colnr_T)(skipwhite(line) - line);
|
|
4729 compl_startpos.col = compl_col;
|
|
4730 compl_startpos.lnum = curwin->w_cursor.lnum;
|
|
4731 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
|
7
|
4732 }
|
|
4733 else
|
|
4734 {
|
|
4735 /* S_IPOS was set when we inserted a word that was at the
|
|
4736 * beginning of the line, which means that we'll go to SOL
|
449
|
4737 * mode but first we need to redefine compl_startpos */
|
|
4738 if (compl_cont_status & CONT_S_IPOS)
|
7
|
4739 {
|
449
|
4740 compl_cont_status |= CONT_SOL;
|
|
4741 compl_startpos.col = (colnr_T)(skipwhite(
|
|
4742 line + compl_length
|
|
4743 + compl_startpos.col) - line);
|
7
|
4744 }
|
449
|
4745 compl_col = compl_startpos.col;
|
7
|
4746 }
|
449
|
4747 compl_length = curwin->w_cursor.col - (int)compl_col;
|
502
|
4748 /* IObuff is used to add a "word from the next line" would we
|
1796
|
4749 * have enough space? just being paranoid */
|
7
|
4750 #define MIN_SPACE 75
|
449
|
4751 if (compl_length > (IOSIZE - MIN_SPACE))
|
7
|
4752 {
|
449
|
4753 compl_cont_status &= ~CONT_SOL;
|
|
4754 compl_length = (IOSIZE - MIN_SPACE);
|
|
4755 compl_col = curwin->w_cursor.col - compl_length;
|
7
|
4756 }
|
449
|
4757 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
|
|
4758 if (compl_length < 1)
|
|
4759 compl_cont_status &= CONT_LOCAL;
|
7
|
4760 }
|
|
4761 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
449
|
4762 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
|
7
|
4763 else
|
449
|
4764 compl_cont_status = 0;
|
7
|
4765 }
|
|
4766 else
|
449
|
4767 compl_cont_status &= CONT_LOCAL;
|
|
4768
|
|
4769 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
|
|
4770 {
|
|
4771 compl_cont_mode = ctrl_x_mode;
|
7
|
4772 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
|
449
|
4773 compl_cont_status = 0;
|
|
4774 compl_cont_status |= CONT_N_ADDS;
|
|
4775 compl_startpos = curwin->w_cursor;
|
|
4776 startcol = (int)curs_col;
|
|
4777 compl_col = 0;
|
7
|
4778 }
|
|
4779
|
|
4780 /* Work out completion pattern and original text -- webb */
|
|
4781 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
|
|
4782 {
|
449
|
4783 if ((compl_cont_status & CONT_SOL)
|
7
|
4784 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
|
|
4785 {
|
449
|
4786 if (!(compl_cont_status & CONT_ADDING))
|
7
|
4787 {
|
449
|
4788 while (--startcol >= 0 && vim_isIDc(line[startcol]))
|
7
|
4789 ;
|
449
|
4790 compl_col += ++startcol;
|
|
4791 compl_length = curs_col - startcol;
|
7
|
4792 }
|
|
4793 if (p_ic)
|
449
|
4794 compl_pattern = str_foldcase(line + compl_col,
|
|
4795 compl_length, NULL, 0);
|
7
|
4796 else
|
449
|
4797 compl_pattern = vim_strnsave(line + compl_col,
|
|
4798 compl_length);
|
|
4799 if (compl_pattern == NULL)
|
7
|
4800 return FAIL;
|
|
4801 }
|
449
|
4802 else if (compl_cont_status & CONT_ADDING)
|
7
|
4803 {
|
|
4804 char_u *prefix = (char_u *)"\\<";
|
|
4805
|
|
4806 /* we need 3 extra chars, 1 for the NUL and
|
|
4807 * 2 >= strlen(prefix) -- Acevedo */
|
449
|
4808 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
|
|
4809 compl_length) + 3);
|
|
4810 if (compl_pattern == NULL)
|
7
|
4811 return FAIL;
|
449
|
4812 if (!vim_iswordp(line + compl_col)
|
|
4813 || (compl_col > 0
|
7
|
4814 && (
|
|
4815 #ifdef FEAT_MBYTE
|
449
|
4816 vim_iswordp(mb_prevptr(line, line + compl_col))
|
7
|
4817 #else
|
449
|
4818 vim_iswordc(line[compl_col - 1])
|
7
|
4819 #endif
|
|
4820 )))
|
|
4821 prefix = (char_u *)"";
|
449
|
4822 STRCPY((char *)compl_pattern, prefix);
|
|
4823 (void)quote_meta(compl_pattern + STRLEN(prefix),
|
|
4824 line + compl_col, compl_length);
|
7
|
4825 }
|
449
|
4826 else if (--startcol < 0 ||
|
7
|
4827 #ifdef FEAT_MBYTE
|
449
|
4828 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
|
7
|
4829 #else
|
449
|
4830 !vim_iswordc(line[startcol])
|
7
|
4831 #endif
|
|
4832 )
|
|
4833 {
|
|
4834 /* Match any word of at least two chars */
|
449
|
4835 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
|
|
4836 if (compl_pattern == NULL)
|
7
|
4837 return FAIL;
|
449
|
4838 compl_col += curs_col;
|
|
4839 compl_length = 0;
|
7
|
4840 }
|
|
4841 else
|
|
4842 {
|
|
4843 #ifdef FEAT_MBYTE
|
|
4844 /* Search the point of change class of multibyte character
|
|
4845 * or not a word single byte character backward. */
|
|
4846 if (has_mbyte)
|
|
4847 {
|
|
4848 int base_class;
|
|
4849 int head_off;
|
|
4850
|
449
|
4851 startcol -= (*mb_head_off)(line, line + startcol);
|
|
4852 base_class = mb_get_class(line + startcol);
|
|
4853 while (--startcol >= 0)
|
7
|
4854 {
|
449
|
4855 head_off = (*mb_head_off)(line, line + startcol);
|
|
4856 if (base_class != mb_get_class(line + startcol
|
|
4857 - head_off))
|
7
|
4858 break;
|
449
|
4859 startcol -= head_off;
|
7
|
4860 }
|
|
4861 }
|
|
4862 else
|
|
4863 #endif
|
449
|
4864 while (--startcol >= 0 && vim_iswordc(line[startcol]))
|
7
|
4865 ;
|
449
|
4866 compl_col += ++startcol;
|
|
4867 compl_length = (int)curs_col - startcol;
|
|
4868 if (compl_length == 1)
|
7
|
4869 {
|
|
4870 /* Only match word with at least two chars -- webb
|
|
4871 * there's no need to call quote_meta,
|
|
4872 * alloc(7) is enough -- Acevedo
|
|
4873 */
|
449
|
4874 compl_pattern = alloc(7);
|
|
4875 if (compl_pattern == NULL)
|
7
|
4876 return FAIL;
|
449
|
4877 STRCPY((char *)compl_pattern, "\\<");
|
|
4878 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
|
|
4879 STRCAT((char *)compl_pattern, "\\k");
|
7
|
4880 }
|
|
4881 else
|
|
4882 {
|
449
|
4883 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
|
|
4884 compl_length) + 3);
|
|
4885 if (compl_pattern == NULL)
|
7
|
4886 return FAIL;
|
449
|
4887 STRCPY((char *)compl_pattern, "\\<");
|
|
4888 (void)quote_meta(compl_pattern + 2, line + compl_col,
|
|
4889 compl_length);
|
7
|
4890 }
|
|
4891 }
|
|
4892 }
|
|
4893 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
4894 {
|
835
|
4895 compl_col = (colnr_T)(skipwhite(line) - line);
|
449
|
4896 compl_length = (int)curs_col - (int)compl_col;
|
|
4897 if (compl_length < 0) /* cursor in indent: empty pattern */
|
|
4898 compl_length = 0;
|
7
|
4899 if (p_ic)
|
449
|
4900 compl_pattern = str_foldcase(line + compl_col, compl_length,
|
|
4901 NULL, 0);
|
7
|
4902 else
|
449
|
4903 compl_pattern = vim_strnsave(line + compl_col, compl_length);
|
|
4904 if (compl_pattern == NULL)
|
7
|
4905 return FAIL;
|
|
4906 }
|
|
4907 else if (ctrl_x_mode == CTRL_X_FILES)
|
|
4908 {
|
449
|
4909 while (--startcol >= 0 && vim_isfilec(line[startcol]))
|
7
|
4910 ;
|
449
|
4911 compl_col += ++startcol;
|
|
4912 compl_length = (int)curs_col - startcol;
|
|
4913 compl_pattern = addstar(line + compl_col, compl_length,
|
|
4914 EXPAND_FILES);
|
|
4915 if (compl_pattern == NULL)
|
7
|
4916 return FAIL;
|
|
4917 }
|
|
4918 else if (ctrl_x_mode == CTRL_X_CMDLINE)
|
|
4919 {
|
449
|
4920 compl_pattern = vim_strnsave(line, curs_col);
|
|
4921 if (compl_pattern == NULL)
|
7
|
4922 return FAIL;
|
449
|
4923 set_cmd_context(&compl_xp, compl_pattern,
|
|
4924 (int)STRLEN(compl_pattern), curs_col);
|
|
4925 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
|
|
4926 || compl_xp.xp_context == EXPAND_NOTHING)
|
935
|
4927 /* No completion possible, use an empty pattern to get a
|
|
4928 * "pattern not found" message. */
|
897
|
4929 compl_col = curs_col;
|
|
4930 else
|
935
|
4931 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
|
|
4932 compl_length = curs_col - compl_col;
|
7
|
4933 }
|
523
|
4934 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
|
502
|
4935 {
|
12
|
4936 #ifdef FEAT_COMPL_FUNC
|
|
4937 /*
|
502
|
4938 * Call user defined function 'completefunc' with "a:findstart"
|
|
4939 * set to 1 to obtain the length of text to use for completion.
|
12
|
4940 */
|
502
|
4941 char_u *args[2];
|
452
|
4942 int col;
|
502
|
4943 char_u *funcname;
|
|
4944 pos_T pos;
|
|
4945
|
523
|
4946 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
|
502
|
4947 * string */
|
|
4948 funcname = ctrl_x_mode == CTRL_X_FUNCTION
|
|
4949 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
|
|
4950 if (*funcname == NUL)
|
523
|
4951 {
|
|
4952 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
|
|
4953 ? "completefunc" : "omnifunc");
|
12
|
4954 return FAIL;
|
523
|
4955 }
|
452
|
4956
|
|
4957 args[0] = (char_u *)"1";
|
502
|
4958 args[1] = NULL;
|
|
4959 pos = curwin->w_cursor;
|
|
4960 col = call_func_retnr(funcname, 2, args, FALSE);
|
|
4961 curwin->w_cursor = pos; /* restore the cursor position */
|
|
4962
|
452
|
4963 if (col < 0)
|
523
|
4964 col = curs_col;
|
452
|
4965 compl_col = col;
|
|
4966 if ((colnr_T)compl_col > curs_col)
|
|
4967 compl_col = curs_col;
|
449
|
4968
|
|
4969 /* Setup variables for completion. Need to obtain "line" again,
|
|
4970 * it may have become invalid. */
|
|
4971 line = ml_get(curwin->w_cursor.lnum);
|
452
|
4972 compl_length = curs_col - compl_col;
|
449
|
4973 compl_pattern = vim_strnsave(line + compl_col, compl_length);
|
|
4974 if (compl_pattern == NULL)
|
502
|
4975 #endif
|
449
|
4976 return FAIL;
|
|
4977 }
|
477
|
4978 else if (ctrl_x_mode == CTRL_X_SPELL)
|
|
4979 {
|
744
|
4980 #ifdef FEAT_SPELL
|
497
|
4981 if (spell_bad_len > 0)
|
|
4982 compl_col = curs_col - spell_bad_len;
|
|
4983 else
|
|
4984 compl_col = spell_word_start(startcol);
|
|
4985 if (compl_col >= (colnr_T)startcol)
|
897
|
4986 {
|
|
4987 compl_length = 0;
|
|
4988 compl_col = curs_col;
|
|
4989 }
|
|
4990 else
|
|
4991 {
|
|
4992 spell_expand_check_cap(compl_col);
|
|
4993 compl_length = (int)curs_col - compl_col;
|
|
4994 }
|
818
|
4995 /* Need to obtain "line" again, it may have become invalid. */
|
|
4996 line = ml_get(curwin->w_cursor.lnum);
|
477
|
4997 compl_pattern = vim_strnsave(line + compl_col, compl_length);
|
|
4998 if (compl_pattern == NULL)
|
|
4999 #endif
|
|
5000 return FAIL;
|
|
5001 }
|
449
|
5002 else
|
|
5003 {
|
|
5004 EMSG2(_(e_intern2), "ins_complete()");
|
|
5005 return FAIL;
|
|
5006 }
|
|
5007
|
|
5008 if (compl_cont_status & CONT_ADDING)
|
7
|
5009 {
|
|
5010 edit_submode_pre = (char_u *)_(" Adding");
|
|
5011 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
|
|
5012 {
|
|
5013 /* Insert a new line, keep indentation but ignore 'comments' */
|
|
5014 #ifdef FEAT_COMMENTS
|
|
5015 char_u *old = curbuf->b_p_com;
|
|
5016
|
|
5017 curbuf->b_p_com = (char_u *)"";
|
|
5018 #endif
|
449
|
5019 compl_startpos.lnum = curwin->w_cursor.lnum;
|
|
5020 compl_startpos.col = compl_col;
|
7
|
5021 ins_eol('\r');
|
|
5022 #ifdef FEAT_COMMENTS
|
|
5023 curbuf->b_p_com = old;
|
|
5024 #endif
|
449
|
5025 compl_length = 0;
|
|
5026 compl_col = curwin->w_cursor.col;
|
7
|
5027 }
|
|
5028 }
|
|
5029 else
|
|
5030 {
|
|
5031 edit_submode_pre = NULL;
|
449
|
5032 compl_startpos.col = compl_col;
|
|
5033 }
|
|
5034
|
|
5035 if (compl_cont_status & CONT_LOCAL)
|
|
5036 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
|
7
|
5037 else
|
|
5038 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
|
|
5039
|
694
|
5040 /* Always add completion for the original text. */
|
|
5041 vim_free(compl_orig_text);
|
449
|
5042 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
|
|
5043 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
|
942
|
5044 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
|
449
|
5045 {
|
|
5046 vim_free(compl_pattern);
|
|
5047 compl_pattern = NULL;
|
|
5048 vim_free(compl_orig_text);
|
|
5049 compl_orig_text = NULL;
|
7
|
5050 return FAIL;
|
|
5051 }
|
|
5052
|
|
5053 /* showmode might reset the internal line pointers, so it must
|
|
5054 * be called before line = ml_get(), or when this address is no
|
|
5055 * longer needed. -- Acevedo.
|
|
5056 */
|
|
5057 edit_submode_extra = (char_u *)_("-- Searching...");
|
|
5058 edit_submode_highl = HLF_COUNT;
|
|
5059 showmode();
|
|
5060 edit_submode_extra = NULL;
|
|
5061 out_flush();
|
|
5062 }
|
|
5063
|
449
|
5064 compl_shown_match = compl_curr_match;
|
|
5065 compl_shows_dir = compl_direction;
|
7
|
5066
|
|
5067 /*
|
665
|
5068 * Find next match (and following matches).
|
7
|
5069 */
|
681
|
5070 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
|
449
|
5071
|
540
|
5072 /* may undisplay the popup menu */
|
|
5073 ins_compl_upd_pum();
|
|
5074
|
449
|
5075 if (n > 1) /* all matches have been found */
|
|
5076 compl_matches = n;
|
|
5077 compl_curr_match = compl_shown_match;
|
|
5078 compl_direction = compl_shows_dir;
|
857
|
5079
|
|
5080 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
|
|
5081 * mode. */
|
7
|
5082 if (got_int && !global_busy)
|
|
5083 {
|
|
5084 (void)vgetc();
|
|
5085 got_int = FALSE;
|
|
5086 }
|
|
5087
|
449
|
5088 /* we found no match if the list has only the "compl_orig_text"-entry */
|
464
|
5089 if (compl_first_match == compl_first_match->cp_next)
|
449
|
5090 {
|
|
5091 edit_submode_extra = (compl_cont_status & CONT_ADDING)
|
|
5092 && compl_length > 1
|
7
|
5093 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
|
|
5094 edit_submode_highl = HLF_E;
|
|
5095 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
|
|
5096 * because we couldn't expand anything at first place, but if we used
|
|
5097 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
|
|
5098 * (such as M in M'exico) if not tried already. -- Acevedo */
|
449
|
5099 if ( compl_length > 1
|
|
5100 || (compl_cont_status & CONT_ADDING)
|
7
|
5101 || (ctrl_x_mode != 0
|
|
5102 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
|
|
5103 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
|
449
|
5104 compl_cont_status &= ~CONT_N_ADDS;
|
|
5105 }
|
|
5106
|
464
|
5107 if (compl_curr_match->cp_flags & CONT_S_IPOS)
|
449
|
5108 compl_cont_status |= CONT_S_IPOS;
|
7
|
5109 else
|
449
|
5110 compl_cont_status &= ~CONT_S_IPOS;
|
7
|
5111
|
|
5112 if (edit_submode_extra == NULL)
|
|
5113 {
|
464
|
5114 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
|
7
|
5115 {
|
|
5116 edit_submode_extra = (char_u *)_("Back at original");
|
|
5117 edit_submode_highl = HLF_W;
|
|
5118 }
|
449
|
5119 else if (compl_cont_status & CONT_S_IPOS)
|
7
|
5120 {
|
|
5121 edit_submode_extra = (char_u *)_("Word from other line");
|
|
5122 edit_submode_highl = HLF_COUNT;
|
|
5123 }
|
464
|
5124 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
|
7
|
5125 {
|
|
5126 edit_submode_extra = (char_u *)_("The only match");
|
|
5127 edit_submode_highl = HLF_COUNT;
|
|
5128 }
|
|
5129 else
|
|
5130 {
|
|
5131 /* Update completion sequence number when needed. */
|
464
|
5132 if (compl_curr_match->cp_number == -1)
|
7
|
5133 {
|
464
|
5134 int number = 0;
|
|
5135 compl_T *match;
|
7
|
5136
|
449
|
5137 if (compl_direction == FORWARD)
|
7
|
5138 {
|
|
5139 /* search backwards for the first valid (!= -1) number.
|
|
5140 * This should normally succeed already at the first loop
|
|
5141 * cycle, so it's fast! */
|
464
|
5142 for (match = compl_curr_match->cp_prev; match != NULL
|
|
5143 && match != compl_first_match;
|
|
5144 match = match->cp_prev)
|
|
5145 if (match->cp_number != -1)
|
7
|
5146 {
|
464
|
5147 number = match->cp_number;
|
7
|
5148 break;
|
|
5149 }
|
|
5150 if (match != NULL)
|
|
5151 /* go up and assign all numbers which are not assigned
|
|
5152 * yet */
|
540
|
5153 for (match = match->cp_next;
|
|
5154 match != NULL && match->cp_number == -1;
|
464
|
5155 match = match->cp_next)
|
|
5156 match->cp_number = ++number;
|
7
|
5157 }
|
|
5158 else /* BACKWARD */
|
|
5159 {
|
|
5160 /* search forwards (upwards) for the first valid (!= -1)
|
|
5161 * number. This should normally succeed already at the
|
|
5162 * first loop cycle, so it's fast! */
|
464
|
5163 for (match = compl_curr_match->cp_next; match != NULL
|
|
5164 && match != compl_first_match;
|
|
5165 match = match->cp_next)
|
|
5166 if (match->cp_number != -1)
|
7
|
5167 {
|
464
|
5168 number = match->cp_number;
|
7
|
5169 break;
|
|
5170 }
|
|
5171 if (match != NULL)
|
|
5172 /* go down and assign all numbers which are not
|
|
5173 * assigned yet */
|
464
|
5174 for (match = match->cp_prev; match
|
|
5175 && match->cp_number == -1;
|
|
5176 match = match->cp_prev)
|
|
5177 match->cp_number = ++number;
|
7
|
5178 }
|
|
5179 }
|
|
5180
|
540
|
5181 /* The match should always have a sequence number now, this is
|
|
5182 * just a safety check. */
|
464
|
5183 if (compl_curr_match->cp_number != -1)
|
7
|
5184 {
|
1063
|
5185 /* Space for 10 text chars. + 2x10-digit no.s = 31.
|
|
5186 * Translations may need more than twice that. */
|
|
5187 static char_u match_ref[81];
|
7
|
5188
|
449
|
5189 if (compl_matches > 0)
|
1063
|
5190 vim_snprintf((char *)match_ref, sizeof(match_ref),
|
|
5191 _("match %d of %d"),
|
464
|
5192 compl_curr_match->cp_number, compl_matches);
|
7
|
5193 else
|
1063
|
5194 vim_snprintf((char *)match_ref, sizeof(match_ref),
|
|
5195 _("match %d"),
|
|
5196 compl_curr_match->cp_number);
|
7
|
5197 edit_submode_extra = match_ref;
|
|
5198 edit_submode_highl = HLF_R;
|
|
5199 if (dollar_vcol)
|
|
5200 curs_columns(FALSE);
|
|
5201 }
|
|
5202 }
|
|
5203 }
|
|
5204
|
|
5205 /* Show a message about what (completion) mode we're in. */
|
|
5206 showmode();
|
|
5207 if (edit_submode_extra != NULL)
|
|
5208 {
|
|
5209 if (!p_smd)
|
|
5210 msg_attr(edit_submode_extra,
|
|
5211 edit_submode_highl < HLF_COUNT
|
|
5212 ? hl_attr(edit_submode_highl) : 0);
|
|
5213 }
|
|
5214 else
|
|
5215 msg_clr_cmdline(); /* necessary for "noshowmode" */
|
|
5216
|
857
|
5217 /* Show the popup menu, unless we got interrupted. */
|
|
5218 if (!compl_interrupted)
|
|
5219 {
|
|
5220 /* RedrawingDisabled may be set when invoked through complete(). */
|
|
5221 n = RedrawingDisabled;
|
|
5222 RedrawingDisabled = 0;
|
|
5223 ins_compl_show_pum();
|
|
5224 setcursor();
|
|
5225 RedrawingDisabled = n;
|
|
5226 }
|
874
|
5227 compl_was_interrupted = compl_interrupted;
|
857
|
5228 compl_interrupted = FALSE;
|
540
|
5229
|
7
|
5230 return OK;
|
|
5231 }
|
|
5232
|
|
5233 /*
|
|
5234 * Looks in the first "len" chars. of "src" for search-metachars.
|
|
5235 * If dest is not NULL the chars. are copied there quoting (with
|
|
5236 * a backslash) the metachars, and dest would be NUL terminated.
|
|
5237 * Returns the length (needed) of dest
|
|
5238 */
|
|
5239 static int
|
|
5240 quote_meta(dest, src, len)
|
|
5241 char_u *dest;
|
|
5242 char_u *src;
|
|
5243 int len;
|
|
5244 {
|
|
5245 int m;
|
|
5246
|
|
5247 for (m = len; --len >= 0; src++)
|
|
5248 {
|
|
5249 switch (*src)
|
|
5250 {
|
|
5251 case '.':
|
|
5252 case '*':
|
|
5253 case '[':
|
|
5254 if (ctrl_x_mode == CTRL_X_DICTIONARY
|
|
5255 || ctrl_x_mode == CTRL_X_THESAURUS)
|
|
5256 break;
|
|
5257 case '~':
|
|
5258 if (!p_magic) /* quote these only if magic is set */
|
|
5259 break;
|
|
5260 case '\\':
|
|
5261 if (ctrl_x_mode == CTRL_X_DICTIONARY
|
|
5262 || ctrl_x_mode == CTRL_X_THESAURUS)
|
|
5263 break;
|
|
5264 case '^': /* currently it's not needed. */
|
|
5265 case '$':
|
|
5266 m++;
|
|
5267 if (dest != NULL)
|
|
5268 *dest++ = '\\';
|
|
5269 break;
|
|
5270 }
|
|
5271 if (dest != NULL)
|
|
5272 *dest++ = *src;
|
464
|
5273 # ifdef FEAT_MBYTE
|
7
|
5274 /* Copy remaining bytes of a multibyte character. */
|
|
5275 if (has_mbyte)
|
|
5276 {
|
|
5277 int i, mb_len;
|
|
5278
|
474
|
5279 mb_len = (*mb_ptr2len)(src) - 1;
|
7
|
5280 if (mb_len > 0 && len >= mb_len)
|
|
5281 for (i = 0; i < mb_len; ++i)
|
|
5282 {
|
|
5283 --len;
|
|
5284 ++src;
|
|
5285 if (dest != NULL)
|
|
5286 *dest++ = *src;
|
|
5287 }
|
|
5288 }
|
464
|
5289 # endif
|
7
|
5290 }
|
|
5291 if (dest != NULL)
|
|
5292 *dest = NUL;
|
|
5293
|
|
5294 return m;
|
|
5295 }
|
|
5296 #endif /* FEAT_INS_EXPAND */
|
|
5297
|
|
5298 /*
|
|
5299 * Next character is interpreted literally.
|
|
5300 * A one, two or three digit decimal number is interpreted as its byte value.
|
|
5301 * If one or two digits are entered, the next character is given to vungetc().
|
|
5302 * For Unicode a character > 255 may be returned.
|
|
5303 */
|
|
5304 int
|
|
5305 get_literal()
|
|
5306 {
|
|
5307 int cc;
|
|
5308 int nc;
|
|
5309 int i;
|
|
5310 int hex = FALSE;
|
|
5311 int octal = FALSE;
|
|
5312 #ifdef FEAT_MBYTE
|
|
5313 int unicode = 0;
|
|
5314 #endif
|
|
5315
|
|
5316 if (got_int)
|
|
5317 return Ctrl_C;
|
|
5318
|
|
5319 #ifdef FEAT_GUI
|
|
5320 /*
|
|
5321 * In GUI there is no point inserting the internal code for a special key.
|
|
5322 * It is more useful to insert the string "<KEY>" instead. This would
|
|
5323 * probably be useful in a text window too, but it would not be
|
|
5324 * vi-compatible (maybe there should be an option for it?) -- webb
|
|
5325 */
|
|
5326 if (gui.in_use)
|
|
5327 ++allow_keys;
|
|
5328 #endif
|
|
5329 #ifdef USE_ON_FLY_SCROLL
|
|
5330 dont_scroll = TRUE; /* disallow scrolling here */
|
|
5331 #endif
|
|
5332 ++no_mapping; /* don't map the next key hits */
|
|
5333 cc = 0;
|
|
5334 i = 0;
|
|
5335 for (;;)
|
|
5336 {
|
1389
|
5337 nc = plain_vgetc();
|
7
|
5338 #ifdef FEAT_CMDL_INFO
|
|
5339 if (!(State & CMDLINE)
|
|
5340 # ifdef FEAT_MBYTE
|
|
5341 && MB_BYTE2LEN_CHECK(nc) == 1
|
|
5342 # endif
|
|
5343 )
|
|
5344 add_to_showcmd(nc);
|
|
5345 #endif
|
|
5346 if (nc == 'x' || nc == 'X')
|
|
5347 hex = TRUE;
|
|
5348 else if (nc == 'o' || nc == 'O')
|
|
5349 octal = TRUE;
|
|
5350 #ifdef FEAT_MBYTE
|
|
5351 else if (nc == 'u' || nc == 'U')
|
|
5352 unicode = nc;
|
|
5353 #endif
|
|
5354 else
|
|
5355 {
|
|
5356 if (hex
|
|
5357 #ifdef FEAT_MBYTE
|
|
5358 || unicode != 0
|
|
5359 #endif
|
|
5360 )
|
|
5361 {
|
|
5362 if (!vim_isxdigit(nc))
|
|
5363 break;
|
|
5364 cc = cc * 16 + hex2nr(nc);
|
|
5365 }
|
|
5366 else if (octal)
|
|
5367 {
|
|
5368 if (nc < '0' || nc > '7')
|
|
5369 break;
|
|
5370 cc = cc * 8 + nc - '0';
|
|
5371 }
|
|
5372 else
|
|
5373 {
|
|
5374 if (!VIM_ISDIGIT(nc))
|
|
5375 break;
|
|
5376 cc = cc * 10 + nc - '0';
|
|
5377 }
|
|
5378
|
|
5379 ++i;
|
|
5380 }
|
|
5381
|
|
5382 if (cc > 255
|
|
5383 #ifdef FEAT_MBYTE
|
|
5384 && unicode == 0
|
|
5385 #endif
|
|
5386 )
|
|
5387 cc = 255; /* limit range to 0-255 */
|
|
5388 nc = 0;
|
|
5389
|
|
5390 if (hex) /* hex: up to two chars */
|
|
5391 {
|
|
5392 if (i >= 2)
|
|
5393 break;
|
|
5394 }
|
|
5395 #ifdef FEAT_MBYTE
|
|
5396 else if (unicode) /* Unicode: up to four or eight chars */
|
|
5397 {
|
|
5398 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
|
|
5399 break;
|
|
5400 }
|
|
5401 #endif
|
|
5402 else if (i >= 3) /* decimal or octal: up to three chars */
|
|
5403 break;
|
|
5404 }
|
|
5405 if (i == 0) /* no number entered */
|
|
5406 {
|
|
5407 if (nc == K_ZERO) /* NUL is stored as NL */
|
|
5408 {
|
|
5409 cc = '\n';
|
|
5410 nc = 0;
|
|
5411 }
|
|
5412 else
|
|
5413 {
|
|
5414 cc = nc;
|
|
5415 nc = 0;
|
|
5416 }
|
|
5417 }
|
|
5418
|
|
5419 if (cc == 0) /* NUL is stored as NL */
|
|
5420 cc = '\n';
|
221
|
5421 #ifdef FEAT_MBYTE
|
|
5422 if (enc_dbcs && (cc & 0xff) == 0)
|
|
5423 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
|
|
5424 second byte will cause trouble! */
|
|
5425 #endif
|
7
|
5426
|
|
5427 --no_mapping;
|
|
5428 #ifdef FEAT_GUI
|
|
5429 if (gui.in_use)
|
|
5430 --allow_keys;
|
|
5431 #endif
|
|
5432 if (nc)
|
|
5433 vungetc(nc);
|
|
5434 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
|
|
5435 return cc;
|
|
5436 }
|
|
5437
|
|
5438 /*
|
|
5439 * Insert character, taking care of special keys and mod_mask
|
|
5440 */
|
|
5441 static void
|
|
5442 insert_special(c, allow_modmask, ctrlv)
|
|
5443 int c;
|
|
5444 int allow_modmask;
|
|
5445 int ctrlv; /* c was typed after CTRL-V */
|
|
5446 {
|
|
5447 char_u *p;
|
|
5448 int len;
|
|
5449
|
|
5450 /*
|
|
5451 * Special function key, translate into "<Key>". Up to the last '>' is
|
|
5452 * inserted with ins_str(), so as not to replace characters in replace
|
|
5453 * mode.
|
|
5454 * Only use mod_mask for special keys, to avoid things like <S-Space>,
|
|
5455 * unless 'allow_modmask' is TRUE.
|
|
5456 */
|
|
5457 #ifdef MACOS
|
|
5458 /* Command-key never produces a normal key */
|
|
5459 if (mod_mask & MOD_MASK_CMD)
|
|
5460 allow_modmask = TRUE;
|
|
5461 #endif
|
|
5462 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
|
|
5463 {
|
|
5464 p = get_special_key_name(c, mod_mask);
|
|
5465 len = (int)STRLEN(p);
|
|
5466 c = p[len - 1];
|
|
5467 if (len > 2)
|
|
5468 {
|
|
5469 if (stop_arrow() == FAIL)
|
|
5470 return;
|
|
5471 p[len - 1] = NUL;
|
|
5472 ins_str(p);
|
620
|
5473 AppendToRedobuffLit(p, -1);
|
7
|
5474 ctrlv = FALSE;
|
|
5475 }
|
|
5476 }
|
|
5477 if (stop_arrow() == OK)
|
|
5478 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
|
|
5479 }
|
|
5480
|
|
5481 /*
|
|
5482 * Special characters in this context are those that need processing other
|
|
5483 * than the simple insertion that can be performed here. This includes ESC
|
|
5484 * which terminates the insert, and CR/NL which need special processing to
|
|
5485 * open up a new line. This routine tries to optimize insertions performed by
|
|
5486 * the "redo", "undo" or "put" commands, so it needs to know when it should
|
|
5487 * stop and defer processing to the "normal" mechanism.
|
|
5488 * '0' and '^' are special, because they can be followed by CTRL-D.
|
|
5489 */
|
|
5490 #ifdef EBCDIC
|
|
5491 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
|
|
5492 #else
|
|
5493 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
|
|
5494 #endif
|
|
5495
|
|
5496 #ifdef FEAT_MBYTE
|
|
5497 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
|
|
5498 #else
|
|
5499 # define WHITECHAR(cc) vim_iswhite(cc)
|
|
5500 #endif
|
|
5501
|
|
5502 void
|
|
5503 insertchar(c, flags, second_indent)
|
|
5504 int c; /* character to insert or NUL */
|
|
5505 int flags; /* INSCHAR_FORMAT, etc. */
|
|
5506 int second_indent; /* indent for second line if >= 0 */
|
|
5507 {
|
|
5508 int textwidth;
|
|
5509 #ifdef FEAT_COMMENTS
|
|
5510 char_u *p;
|
667
|
5511 #endif
|
7
|
5512 int fo_ins_blank;
|
|
5513
|
|
5514 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
|
|
5515 fo_ins_blank = has_format_option(FO_INS_BLANK);
|
|
5516
|
|
5517 /*
|
|
5518 * Try to break the line in two or more pieces when:
|
|
5519 * - Always do this if we have been called to do formatting only.
|
|
5520 * - Always do this when 'formatoptions' has the 'a' flag and the line
|
|
5521 * ends in white space.
|
|
5522 * - Otherwise:
|
|
5523 * - Don't do this if inserting a blank
|
|
5524 * - Don't do this if an existing character is being replaced, unless
|
|
5525 * we're in VREPLACE mode.
|
|
5526 * - Do this if the cursor is not on the line where insert started
|
|
5527 * or - 'formatoptions' doesn't have 'l' or the line was not too long
|
|
5528 * before the insert.
|
|
5529 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
|
|
5530 * before 'textwidth'
|
|
5531 */
|
667
|
5532 if (textwidth > 0
|
7
|
5533 && ((flags & INSCHAR_FORMAT)
|
|
5534 || (!vim_iswhite(c)
|
|
5535 && !((State & REPLACE_FLAG)
|
|
5536 #ifdef FEAT_VREPLACE
|
|
5537 && !(State & VREPLACE_FLAG)
|
|
5538 #endif
|
|
5539 && *ml_get_cursor() != NUL)
|
|
5540 && (curwin->w_cursor.lnum != Insstart.lnum
|
|
5541 || ((!has_format_option(FO_INS_LONG)
|
|
5542 || Insstart_textlen <= (colnr_T)textwidth)
|
|
5543 && (!fo_ins_blank
|
|
5544 || Insstart_blank_vcol <= (colnr_T)textwidth
|
|
5545 ))))))
|
|
5546 {
|
667
|
5547 /* Format with 'formatexpr' when it's set. Use internal formatting
|
|
5548 * when 'formatexpr' isn't set or it returns non-zero. */
|
|
5549 #if defined(FEAT_EVAL)
|
1000
|
5550 int do_internal = TRUE;
|
|
5551
|
1563
|
5552 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
|
1000
|
5553 {
|
|
5554 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
|
|
5555 /* It may be required to save for undo again, e.g. when setline()
|
|
5556 * was called. */
|
|
5557 ins_need_undo = TRUE;
|
|
5558 }
|
|
5559 if (do_internal)
|
667
|
5560 #endif
|
|
5561 internal_format(textwidth, second_indent, flags, c == NUL);
|
|
5562 }
|
|
5563
|
7
|
5564 if (c == NUL) /* only formatting was wanted */
|
|
5565 return;
|
|
5566
|
|
5567 #ifdef FEAT_COMMENTS
|
|
5568 /* Check whether this character should end a comment. */
|
|
5569 if (did_ai && (int)c == end_comment_pending)
|
|
5570 {
|
|
5571 char_u *line;
|
|
5572 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
|
|
5573 int middle_len, end_len;
|
|
5574 int i;
|
|
5575
|
|
5576 /*
|
|
5577 * Need to remove existing (middle) comment leader and insert end
|
|
5578 * comment leader. First, check what comment leader we can find.
|
|
5579 */
|
|
5580 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
|
|
5581 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
|
|
5582 {
|
|
5583 /* Skip middle-comment string */
|
|
5584 while (*p && p[-1] != ':') /* find end of middle flags */
|
|
5585 ++p;
|
|
5586 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
|
|
5587 /* Don't count trailing white space for middle_len */
|
|
5588 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
|
|
5589 --middle_len;
|
|
5590
|
|
5591 /* Find the end-comment string */
|
|
5592 while (*p && p[-1] != ':') /* find end of end flags */
|
|
5593 ++p;
|
|
5594 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
|
|
5595
|
|
5596 /* Skip white space before the cursor */
|
|
5597 i = curwin->w_cursor.col;
|
|
5598 while (--i >= 0 && vim_iswhite(line[i]))
|
|
5599 ;
|
|
5600 i++;
|
|
5601
|
|
5602 /* Skip to before the middle leader */
|
|
5603 i -= middle_len;
|
|
5604
|
|
5605 /* Check some expected things before we go on */
|
|
5606 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
|
|
5607 {
|
|
5608 /* Backspace over all the stuff we want to replace */
|
|
5609 backspace_until_column(i);
|
|
5610
|
|
5611 /*
|
|
5612 * Insert the end-comment string, except for the last
|
|
5613 * character, which will get inserted as normal later.
|
|
5614 */
|
|
5615 ins_bytes_len(lead_end, end_len - 1);
|
|
5616 }
|
|
5617 }
|
|
5618 }
|
|
5619 end_comment_pending = NUL;
|
|
5620 #endif
|
|
5621
|
|
5622 did_ai = FALSE;
|
|
5623 #ifdef FEAT_SMARTINDENT
|
|
5624 did_si = FALSE;
|
|
5625 can_si = FALSE;
|
|
5626 can_si_back = FALSE;
|
|
5627 #endif
|
|
5628
|
|
5629 /*
|
|
5630 * If there's any pending input, grab up to INPUT_BUFLEN at once.
|
|
5631 * This speeds up normal text input considerably.
|
|
5632 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
|
|
5633 * need to re-indent at a ':', or any other character (but not what
|
|
5634 * 'paste' is set)..
|
|
5635 */
|
|
5636 #ifdef USE_ON_FLY_SCROLL
|
|
5637 dont_scroll = FALSE; /* allow scrolling here */
|
|
5638 #endif
|
|
5639
|
|
5640 if ( !ISSPECIAL(c)
|
|
5641 #ifdef FEAT_MBYTE
|
|
5642 && (!has_mbyte || (*mb_char2len)(c) == 1)
|
|
5643 #endif
|
|
5644 && vpeekc() != NUL
|
|
5645 && !(State & REPLACE_FLAG)
|
|
5646 #ifdef FEAT_CINDENT
|
|
5647 && !cindent_on()
|
|
5648 #endif
|
|
5649 #ifdef FEAT_RIGHTLEFT
|
|
5650 && !p_ri
|
|
5651 #endif
|
|
5652 )
|
|
5653 {
|
|
5654 #define INPUT_BUFLEN 100
|
|
5655 char_u buf[INPUT_BUFLEN + 1];
|
|
5656 int i;
|
|
5657 colnr_T virtcol = 0;
|
|
5658
|
|
5659 buf[0] = c;
|
|
5660 i = 1;
|
667
|
5661 if (textwidth > 0)
|
7
|
5662 virtcol = get_nolist_virtcol();
|
|
5663 /*
|
|
5664 * Stop the string when:
|
|
5665 * - no more chars available
|
|
5666 * - finding a special character (command key)
|
|
5667 * - buffer is full
|
|
5668 * - running into the 'textwidth' boundary
|
|
5669 * - need to check for abbreviation: A non-word char after a word-char
|
|
5670 */
|
|
5671 while ( (c = vpeekc()) != NUL
|
|
5672 && !ISSPECIAL(c)
|
|
5673 #ifdef FEAT_MBYTE
|
|
5674 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
|
|
5675 #endif
|
|
5676 && i < INPUT_BUFLEN
|
|
5677 && (textwidth == 0
|
|
5678 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
|
|
5679 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
|
|
5680 {
|
|
5681 #ifdef FEAT_RIGHTLEFT
|
|
5682 c = vgetc();
|
|
5683 if (p_hkmap && KeyTyped)
|
|
5684 c = hkmap(c); /* Hebrew mode mapping */
|
|
5685 # ifdef FEAT_FKMAP
|
|
5686 if (p_fkmap && KeyTyped)
|
|
5687 c = fkmap(c); /* Farsi mode mapping */
|
|
5688 # endif
|
|
5689 buf[i++] = c;
|
|
5690 #else
|
|
5691 buf[i++] = vgetc();
|
|
5692 #endif
|
|
5693 }
|
|
5694
|
|
5695 #ifdef FEAT_DIGRAPHS
|
|
5696 do_digraph(-1); /* clear digraphs */
|
|
5697 do_digraph(buf[i-1]); /* may be the start of a digraph */
|
|
5698 #endif
|
|
5699 buf[i] = NUL;
|
|
5700 ins_str(buf);
|
|
5701 if (flags & INSCHAR_CTRLV)
|
|
5702 {
|
|
5703 redo_literal(*buf);
|
|
5704 i = 1;
|
|
5705 }
|
|
5706 else
|
|
5707 i = 0;
|
|
5708 if (buf[i] != NUL)
|
620
|
5709 AppendToRedobuffLit(buf + i, -1);
|
7
|
5710 }
|
|
5711 else
|
|
5712 {
|
|
5713 #ifdef FEAT_MBYTE
|
667
|
5714 int cc;
|
|
5715
|
7
|
5716 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
|
|
5717 {
|
|
5718 char_u buf[MB_MAXBYTES + 1];
|
|
5719
|
|
5720 (*mb_char2bytes)(c, buf);
|
|
5721 buf[cc] = NUL;
|
|
5722 ins_char_bytes(buf, cc);
|
|
5723 AppendCharToRedobuff(c);
|
|
5724 }
|
|
5725 else
|
|
5726 #endif
|
|
5727 {
|
|
5728 ins_char(c);
|
|
5729 if (flags & INSCHAR_CTRLV)
|
|
5730 redo_literal(c);
|
|
5731 else
|
|
5732 AppendCharToRedobuff(c);
|
|
5733 }
|
|
5734 }
|
|
5735 }
|
|
5736
|
|
5737 /*
|
667
|
5738 * Format text at the current insert position.
|
|
5739 */
|
|
5740 static void
|
|
5741 internal_format(textwidth, second_indent, flags, format_only)
|
|
5742 int textwidth;
|
|
5743 int second_indent;
|
|
5744 int flags;
|
|
5745 int format_only;
|
|
5746 {
|
|
5747 int cc;
|
|
5748 int save_char = NUL;
|
|
5749 int haveto_redraw = FALSE;
|
|
5750 int fo_ins_blank = has_format_option(FO_INS_BLANK);
|
|
5751 #ifdef FEAT_MBYTE
|
|
5752 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
|
|
5753 #endif
|
|
5754 int fo_white_par = has_format_option(FO_WHITE_PAR);
|
|
5755 int first_line = TRUE;
|
|
5756 #ifdef FEAT_COMMENTS
|
|
5757 colnr_T leader_len;
|
|
5758 int no_leader = FALSE;
|
|
5759 int do_comments = (flags & INSCHAR_DO_COM);
|
|
5760 #endif
|
|
5761
|
|
5762 /*
|
|
5763 * When 'ai' is off we don't want a space under the cursor to be
|
|
5764 * deleted. Replace it with an 'x' temporarily.
|
|
5765 */
|
|
5766 if (!curbuf->b_p_ai)
|
|
5767 {
|
|
5768 cc = gchar_cursor();
|
|
5769 if (vim_iswhite(cc))
|
|
5770 {
|
|
5771 save_char = cc;
|
|
5772 pchar_cursor('x');
|
|
5773 }
|
|
5774 }
|
|
5775
|
|
5776 /*
|
|
5777 * Repeat breaking lines, until the current line is not too long.
|
|
5778 */
|
|
5779 while (!got_int)
|
|
5780 {
|
|
5781 int startcol; /* Cursor column at entry */
|
|
5782 int wantcol; /* column at textwidth border */
|
|
5783 int foundcol; /* column for start of spaces */
|
|
5784 int end_foundcol = 0; /* column for start of word */
|
|
5785 colnr_T len;
|
|
5786 colnr_T virtcol;
|
|
5787 #ifdef FEAT_VREPLACE
|
|
5788 int orig_col = 0;
|
|
5789 char_u *saved_text = NULL;
|
|
5790 #endif
|
|
5791 colnr_T col;
|
|
5792
|
|
5793 virtcol = get_nolist_virtcol();
|
|
5794 if (virtcol < (colnr_T)textwidth)
|
|
5795 break;
|
|
5796
|
|
5797 #ifdef FEAT_COMMENTS
|
|
5798 if (no_leader)
|
|
5799 do_comments = FALSE;
|
|
5800 else if (!(flags & INSCHAR_FORMAT)
|
|
5801 && has_format_option(FO_WRAP_COMS))
|
|
5802 do_comments = TRUE;
|
|
5803
|
|
5804 /* Don't break until after the comment leader */
|
|
5805 if (do_comments)
|
|
5806 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
|
|
5807 else
|
|
5808 leader_len = 0;
|
|
5809
|
|
5810 /* If the line doesn't start with a comment leader, then don't
|
|
5811 * start one in a following broken line. Avoids that a %word
|
|
5812 * moved to the start of the next line causes all following lines
|
|
5813 * to start with %. */
|
|
5814 if (leader_len == 0)
|
|
5815 no_leader = TRUE;
|
|
5816 #endif
|
|
5817 if (!(flags & INSCHAR_FORMAT)
|
|
5818 #ifdef FEAT_COMMENTS
|
|
5819 && leader_len == 0
|
|
5820 #endif
|
|
5821 && !has_format_option(FO_WRAP))
|
|
5822
|
|
5823 {
|
|
5824 textwidth = 0;
|
|
5825 break;
|
|
5826 }
|
|
5827 if ((startcol = curwin->w_cursor.col) == 0)
|
|
5828 break;
|
|
5829
|
|
5830 /* find column of textwidth border */
|
|
5831 coladvance((colnr_T)textwidth);
|
|
5832 wantcol = curwin->w_cursor.col;
|
|
5833
|
|
5834 curwin->w_cursor.col = startcol - 1;
|
|
5835 #ifdef FEAT_MBYTE
|
|
5836 /* Correct cursor for multi-byte character. */
|
|
5837 if (has_mbyte)
|
|
5838 mb_adjust_cursor();
|
|
5839 #endif
|
|
5840 foundcol = 0;
|
|
5841
|
|
5842 /*
|
|
5843 * Find position to break at.
|
|
5844 * Stop at first entered white when 'formatoptions' has 'v'
|
|
5845 */
|
|
5846 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
|
|
5847 || curwin->w_cursor.lnum != Insstart.lnum
|
|
5848 || curwin->w_cursor.col >= Insstart.col)
|
|
5849 {
|
|
5850 cc = gchar_cursor();
|
|
5851 if (WHITECHAR(cc))
|
|
5852 {
|
|
5853 /* remember position of blank just before text */
|
|
5854 end_foundcol = curwin->w_cursor.col;
|
|
5855
|
|
5856 /* find start of sequence of blanks */
|
|
5857 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
|
|
5858 {
|
|
5859 dec_cursor();
|
|
5860 cc = gchar_cursor();
|
|
5861 }
|
|
5862 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
|
|
5863 break; /* only spaces in front of text */
|
|
5864 #ifdef FEAT_COMMENTS
|
|
5865 /* Don't break until after the comment leader */
|
|
5866 if (curwin->w_cursor.col < leader_len)
|
|
5867 break;
|
|
5868 #endif
|
|
5869 if (has_format_option(FO_ONE_LETTER))
|
|
5870 {
|
|
5871 /* do not break after one-letter words */
|
|
5872 if (curwin->w_cursor.col == 0)
|
|
5873 break; /* one-letter word at begin */
|
|
5874
|
|
5875 col = curwin->w_cursor.col;
|
|
5876 dec_cursor();
|
|
5877 cc = gchar_cursor();
|
|
5878
|
|
5879 if (WHITECHAR(cc))
|
|
5880 continue; /* one-letter, continue */
|
|
5881 curwin->w_cursor.col = col;
|
|
5882 }
|
|
5883 #ifdef FEAT_MBYTE
|
|
5884 if (has_mbyte)
|
|
5885 foundcol = curwin->w_cursor.col
|
|
5886 + (*mb_ptr2len)(ml_get_cursor());
|
|
5887 else
|
|
5888 #endif
|
|
5889 foundcol = curwin->w_cursor.col + 1;
|
|
5890 if (curwin->w_cursor.col < (colnr_T)wantcol)
|
|
5891 break;
|
|
5892 }
|
|
5893 #ifdef FEAT_MBYTE
|
|
5894 else if (cc >= 0x100 && fo_multibyte
|
|
5895 && curwin->w_cursor.col <= (colnr_T)wantcol)
|
|
5896 {
|
|
5897 /* Break after or before a multi-byte character. */
|
|
5898 foundcol = curwin->w_cursor.col;
|
|
5899 if (curwin->w_cursor.col < (colnr_T)wantcol)
|
|
5900 foundcol += (*mb_char2len)(cc);
|
|
5901 end_foundcol = foundcol;
|
|
5902 break;
|
|
5903 }
|
|
5904 #endif
|
|
5905 if (curwin->w_cursor.col == 0)
|
|
5906 break;
|
|
5907 dec_cursor();
|
|
5908 }
|
|
5909
|
|
5910 if (foundcol == 0) /* no spaces, cannot break line */
|
|
5911 {
|
|
5912 curwin->w_cursor.col = startcol;
|
|
5913 break;
|
|
5914 }
|
|
5915
|
|
5916 /* Going to break the line, remove any "$" now. */
|
|
5917 undisplay_dollar();
|
|
5918
|
|
5919 /*
|
|
5920 * Offset between cursor position and line break is used by replace
|
|
5921 * stack functions. VREPLACE does not use this, and backspaces
|
|
5922 * over the text instead.
|
|
5923 */
|
|
5924 #ifdef FEAT_VREPLACE
|
|
5925 if (State & VREPLACE_FLAG)
|
|
5926 orig_col = startcol; /* Will start backspacing from here */
|
|
5927 else
|
|
5928 #endif
|
|
5929 replace_offset = startcol - end_foundcol - 1;
|
|
5930
|
|
5931 /*
|
|
5932 * adjust startcol for spaces that will be deleted and
|
|
5933 * characters that will remain on top line
|
|
5934 */
|
|
5935 curwin->w_cursor.col = foundcol;
|
|
5936 while (cc = gchar_cursor(), WHITECHAR(cc))
|
|
5937 inc_cursor();
|
|
5938 startcol -= curwin->w_cursor.col;
|
|
5939 if (startcol < 0)
|
|
5940 startcol = 0;
|
|
5941
|
|
5942 #ifdef FEAT_VREPLACE
|
|
5943 if (State & VREPLACE_FLAG)
|
|
5944 {
|
|
5945 /*
|
|
5946 * In VREPLACE mode, we will backspace over the text to be
|
|
5947 * wrapped, so save a copy now to put on the next line.
|
|
5948 */
|
|
5949 saved_text = vim_strsave(ml_get_cursor());
|
|
5950 curwin->w_cursor.col = orig_col;
|
|
5951 if (saved_text == NULL)
|
|
5952 break; /* Can't do it, out of memory */
|
|
5953 saved_text[startcol] = NUL;
|
|
5954
|
|
5955 /* Backspace over characters that will move to the next line */
|
|
5956 if (!fo_white_par)
|
|
5957 backspace_until_column(foundcol);
|
|
5958 }
|
|
5959 else
|
|
5960 #endif
|
|
5961 {
|
|
5962 /* put cursor after pos. to break line */
|
|
5963 if (!fo_white_par)
|
|
5964 curwin->w_cursor.col = foundcol;
|
|
5965 }
|
|
5966
|
|
5967 /*
|
|
5968 * Split the line just before the margin.
|
|
5969 * Only insert/delete lines, but don't really redraw the window.
|
|
5970 */
|
|
5971 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
|
|
5972 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
|
|
5973 #ifdef FEAT_COMMENTS
|
|
5974 + (do_comments ? OPENLINE_DO_COM : 0)
|
|
5975 #endif
|
|
5976 , old_indent);
|
|
5977 old_indent = 0;
|
|
5978
|
|
5979 replace_offset = 0;
|
|
5980 if (first_line)
|
|
5981 {
|
|
5982 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
|
|
5983 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
|
|
5984 if (second_indent >= 0)
|
|
5985 {
|
|
5986 #ifdef FEAT_VREPLACE
|
|
5987 if (State & VREPLACE_FLAG)
|
1516
|
5988 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
|
667
|
5989 else
|
|
5990 #endif
|
|
5991 (void)set_indent(second_indent, SIN_CHANGED);
|
|
5992 }
|
|
5993 first_line = FALSE;
|
|
5994 }
|
|
5995
|
|
5996 #ifdef FEAT_VREPLACE
|
|
5997 if (State & VREPLACE_FLAG)
|
|
5998 {
|
|
5999 /*
|
|
6000 * In VREPLACE mode we have backspaced over the text to be
|
|
6001 * moved, now we re-insert it into the new line.
|
|
6002 */
|
|
6003 ins_bytes(saved_text);
|
|
6004 vim_free(saved_text);
|
|
6005 }
|
|
6006 else
|
|
6007 #endif
|
|
6008 {
|
|
6009 /*
|
|
6010 * Check if cursor is not past the NUL off the line, cindent
|
|
6011 * may have added or removed indent.
|
|
6012 */
|
|
6013 curwin->w_cursor.col += startcol;
|
|
6014 len = (colnr_T)STRLEN(ml_get_curline());
|
|
6015 if (curwin->w_cursor.col > len)
|
|
6016 curwin->w_cursor.col = len;
|
|
6017 }
|
|
6018
|
|
6019 haveto_redraw = TRUE;
|
|
6020 #ifdef FEAT_CINDENT
|
|
6021 can_cindent = TRUE;
|
|
6022 #endif
|
|
6023 /* moved the cursor, don't autoindent or cindent now */
|
|
6024 did_ai = FALSE;
|
|
6025 #ifdef FEAT_SMARTINDENT
|
|
6026 did_si = FALSE;
|
|
6027 can_si = FALSE;
|
|
6028 can_si_back = FALSE;
|
|
6029 #endif
|
|
6030 line_breakcheck();
|
|
6031 }
|
|
6032
|
|
6033 if (save_char != NUL) /* put back space after cursor */
|
|
6034 pchar_cursor(save_char);
|
|
6035
|
|
6036 if (!format_only && haveto_redraw)
|
|
6037 {
|
|
6038 update_topline();
|
|
6039 redraw_curbuf_later(VALID);
|
|
6040 }
|
|
6041 }
|
|
6042
|
|
6043 /*
|
7
|
6044 * Called after inserting or deleting text: When 'formatoptions' includes the
|
|
6045 * 'a' flag format from the current line until the end of the paragraph.
|
|
6046 * Keep the cursor at the same position relative to the text.
|
|
6047 * The caller must have saved the cursor line for undo, following ones will be
|
|
6048 * saved here.
|
|
6049 */
|
|
6050 void
|
|
6051 auto_format(trailblank, prev_line)
|
|
6052 int trailblank; /* when TRUE also format with trailing blank */
|
|
6053 int prev_line; /* may start in previous line */
|
|
6054 {
|
|
6055 pos_T pos;
|
|
6056 colnr_T len;
|
|
6057 char_u *old;
|
|
6058 char_u *new, *pnew;
|
|
6059 int wasatend;
|
301
|
6060 int cc;
|
7
|
6061
|
|
6062 if (!has_format_option(FO_AUTO))
|
|
6063 return;
|
|
6064
|
|
6065 pos = curwin->w_cursor;
|
|
6066 old = ml_get_curline();
|
|
6067
|
|
6068 /* may remove added space */
|
|
6069 check_auto_format(FALSE);
|
|
6070
|
|
6071 /* Don't format in Insert mode when the cursor is on a trailing blank, the
|
|
6072 * user might insert normal text next. Also skip formatting when "1" is
|
|
6073 * in 'formatoptions' and there is a single character before the cursor.
|
|
6074 * Otherwise the line would be broken and when typing another non-white
|
|
6075 * next they are not joined back together. */
|
|
6076 wasatend = (pos.col == STRLEN(old));
|
|
6077 if (*old != NUL && !trailblank && wasatend)
|
|
6078 {
|
|
6079 dec_cursor();
|
301
|
6080 cc = gchar_cursor();
|
|
6081 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
|
|
6082 && has_format_option(FO_ONE_LETTER))
|
7
|
6083 dec_cursor();
|
301
|
6084 cc = gchar_cursor();
|
|
6085 if (WHITECHAR(cc))
|
7
|
6086 {
|
|
6087 curwin->w_cursor = pos;
|
|
6088 return;
|
|
6089 }
|
|
6090 curwin->w_cursor = pos;
|
|
6091 }
|
|
6092
|
|
6093 #ifdef FEAT_COMMENTS
|
|
6094 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
|
|
6095 * comments. */
|
|
6096 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
|
|
6097 && get_leader_len(old, NULL, FALSE) == 0)
|
|
6098 return;
|
|
6099 #endif
|
|
6100
|
|
6101 /*
|
|
6102 * May start formatting in a previous line, so that after "x" a word is
|
|
6103 * moved to the previous line if it fits there now. Only when this is not
|
|
6104 * the start of a paragraph.
|
|
6105 */
|
|
6106 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
|
|
6107 {
|
|
6108 --curwin->w_cursor.lnum;
|
|
6109 if (u_save_cursor() == FAIL)
|
|
6110 return;
|
|
6111 }
|
|
6112
|
|
6113 /*
|
|
6114 * Do the formatting and restore the cursor position. "saved_cursor" will
|
|
6115 * be adjusted for the text formatting.
|
|
6116 */
|
|
6117 saved_cursor = pos;
|
1563
|
6118 format_lines((linenr_T)-1, FALSE);
|
7
|
6119 curwin->w_cursor = saved_cursor;
|
|
6120 saved_cursor.lnum = 0;
|
|
6121
|
|
6122 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
|
|
6123 {
|
|
6124 /* "cannot happen" */
|
|
6125 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
|
|
6126 coladvance((colnr_T)MAXCOL);
|
|
6127 }
|
|
6128 else
|
|
6129 check_cursor_col();
|
|
6130
|
|
6131 /* Insert mode: If the cursor is now after the end of the line while it
|
|
6132 * previously wasn't, the line was broken. Because of the rule above we
|
|
6133 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
|
|
6134 * formatted. */
|
|
6135 if (!wasatend && has_format_option(FO_WHITE_PAR))
|
|
6136 {
|
|
6137 new = ml_get_curline();
|
835
|
6138 len = (colnr_T)STRLEN(new);
|
7
|
6139 if (curwin->w_cursor.col == len)
|
|
6140 {
|
|
6141 pnew = vim_strnsave(new, len + 2);
|
|
6142 pnew[len] = ' ';
|
|
6143 pnew[len + 1] = NUL;
|
|
6144 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
|
|
6145 /* remove the space later */
|
|
6146 did_add_space = TRUE;
|
|
6147 }
|
|
6148 else
|
|
6149 /* may remove added space */
|
|
6150 check_auto_format(FALSE);
|
|
6151 }
|
|
6152
|
|
6153 check_cursor();
|
|
6154 }
|
|
6155
|
|
6156 /*
|
|
6157 * When an extra space was added to continue a paragraph for auto-formatting,
|
|
6158 * delete it now. The space must be under the cursor, just after the insert
|
|
6159 * position.
|
|
6160 */
|
|
6161 static void
|
|
6162 check_auto_format(end_insert)
|
|
6163 int end_insert; /* TRUE when ending Insert mode */
|
|
6164 {
|
|
6165 int c = ' ';
|
301
|
6166 int cc;
|
7
|
6167
|
|
6168 if (did_add_space)
|
|
6169 {
|
301
|
6170 cc = gchar_cursor();
|
|
6171 if (!WHITECHAR(cc))
|
7
|
6172 /* Somehow the space was removed already. */
|
|
6173 did_add_space = FALSE;
|
|
6174 else
|
|
6175 {
|
|
6176 if (!end_insert)
|
|
6177 {
|
|
6178 inc_cursor();
|
|
6179 c = gchar_cursor();
|
|
6180 dec_cursor();
|
|
6181 }
|
|
6182 if (c != NUL)
|
|
6183 {
|
|
6184 /* The space is no longer at the end of the line, delete it. */
|
|
6185 del_char(FALSE);
|
|
6186 did_add_space = FALSE;
|
|
6187 }
|
|
6188 }
|
|
6189 }
|
|
6190 }
|
|
6191
|
|
6192 /*
|
|
6193 * Find out textwidth to be used for formatting:
|
|
6194 * if 'textwidth' option is set, use it
|
|
6195 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
|
|
6196 * if invalid value, use 0.
|
|
6197 * Set default to window width (maximum 79) for "gq" operator.
|
|
6198 */
|
|
6199 int
|
|
6200 comp_textwidth(ff)
|
867
|
6201 int ff; /* force formatting (for "gq" command) */
|
7
|
6202 {
|
|
6203 int textwidth;
|
|
6204
|
|
6205 textwidth = curbuf->b_p_tw;
|
|
6206 if (textwidth == 0 && curbuf->b_p_wm)
|
|
6207 {
|
|
6208 /* The width is the window width minus 'wrapmargin' minus all the
|
|
6209 * things that add to the margin. */
|
|
6210 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
|
|
6211 #ifdef FEAT_CMDWIN
|
|
6212 if (cmdwin_type != 0)
|
|
6213 textwidth -= 1;
|
|
6214 #endif
|
|
6215 #ifdef FEAT_FOLDING
|
|
6216 textwidth -= curwin->w_p_fdc;
|
|
6217 #endif
|
|
6218 #ifdef FEAT_SIGNS
|
|
6219 if (curwin->w_buffer->b_signlist != NULL
|
|
6220 # ifdef FEAT_NETBEANS_INTG
|
|
6221 || usingNetbeans
|
|
6222 # endif
|
|
6223 )
|
|
6224 textwidth -= 1;
|
|
6225 #endif
|
|
6226 if (curwin->w_p_nu)
|
|
6227 textwidth -= 8;
|
|
6228 }
|
|
6229 if (textwidth < 0)
|
|
6230 textwidth = 0;
|
|
6231 if (ff && textwidth == 0)
|
|
6232 {
|
|
6233 textwidth = W_WIDTH(curwin) - 1;
|
|
6234 if (textwidth > 79)
|
|
6235 textwidth = 79;
|
|
6236 }
|
|
6237 return textwidth;
|
|
6238 }
|
|
6239
|
|
6240 /*
|
|
6241 * Put a character in the redo buffer, for when just after a CTRL-V.
|
|
6242 */
|
|
6243 static void
|
|
6244 redo_literal(c)
|
|
6245 int c;
|
|
6246 {
|
|
6247 char_u buf[10];
|
|
6248
|
|
6249 /* Only digits need special treatment. Translate them into a string of
|
|
6250 * three digits. */
|
|
6251 if (VIM_ISDIGIT(c))
|
|
6252 {
|
|
6253 sprintf((char *)buf, "%03d", c);
|
|
6254 AppendToRedobuff(buf);
|
|
6255 }
|
|
6256 else
|
|
6257 AppendCharToRedobuff(c);
|
|
6258 }
|
|
6259
|
|
6260 /*
|
|
6261 * start_arrow() is called when an arrow key is used in insert mode.
|
484
|
6262 * For undo/redo it resembles hitting the <ESC> key.
|
7
|
6263 */
|
|
6264 static void
|
|
6265 start_arrow(end_insert_pos)
|
840
|
6266 pos_T *end_insert_pos; /* can be NULL */
|
7
|
6267 {
|
|
6268 if (!arrow_used) /* something has been inserted */
|
|
6269 {
|
|
6270 AppendToRedobuff(ESC_STR);
|
|
6271 stop_insert(end_insert_pos, FALSE);
|
|
6272 arrow_used = TRUE; /* this means we stopped the current insert */
|
|
6273 }
|
744
|
6274 #ifdef FEAT_SPELL
|
221
|
6275 check_spell_redraw();
|
|
6276 #endif
|
7
|
6277 }
|
|
6278
|
744
|
6279 #ifdef FEAT_SPELL
|
221
|
6280 /*
|
|
6281 * If we skipped highlighting word at cursor, do it now.
|
|
6282 * It may be skipped again, thus reset spell_redraw_lnum first.
|
|
6283 */
|
|
6284 static void
|
|
6285 check_spell_redraw()
|
|
6286 {
|
|
6287 if (spell_redraw_lnum != 0)
|
|
6288 {
|
|
6289 linenr_T lnum = spell_redraw_lnum;
|
|
6290
|
|
6291 spell_redraw_lnum = 0;
|
|
6292 redrawWinline(lnum, FALSE);
|
|
6293 }
|
|
6294 }
|
484
|
6295
|
|
6296 /*
|
|
6297 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
|
|
6298 * spelled word, if there is one.
|
|
6299 */
|
|
6300 static void
|
|
6301 spell_back_to_badword()
|
|
6302 {
|
|
6303 pos_T tpos = curwin->w_cursor;
|
|
6304
|
499
|
6305 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
|
484
|
6306 if (curwin->w_cursor.col != tpos.col)
|
|
6307 start_arrow(&tpos);
|
|
6308 }
|
221
|
6309 #endif
|
|
6310
|
7
|
6311 /*
|
|
6312 * stop_arrow() is called before a change is made in insert mode.
|
|
6313 * If an arrow key has been used, start a new insertion.
|
|
6314 * Returns FAIL if undo is impossible, shouldn't insert then.
|
|
6315 */
|
|
6316 int
|
|
6317 stop_arrow()
|
|
6318 {
|
|
6319 if (arrow_used)
|
|
6320 {
|
|
6321 if (u_save_cursor() == OK)
|
|
6322 {
|
|
6323 arrow_used = FALSE;
|
|
6324 ins_need_undo = FALSE;
|
|
6325 }
|
|
6326 Insstart = curwin->w_cursor; /* new insertion starts here */
|
1869
|
6327 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
|
7
|
6328 ai_col = 0;
|
|
6329 #ifdef FEAT_VREPLACE
|
|
6330 if (State & VREPLACE_FLAG)
|
|
6331 {
|
|
6332 orig_line_count = curbuf->b_ml.ml_line_count;
|
|
6333 vr_lines_changed = 1;
|
|
6334 }
|
|
6335 #endif
|
|
6336 ResetRedobuff();
|
|
6337 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
|
613
|
6338 new_insert_skip = 2;
|
7
|
6339 }
|
|
6340 else if (ins_need_undo)
|
|
6341 {
|
|
6342 if (u_save_cursor() == OK)
|
|
6343 ins_need_undo = FALSE;
|
|
6344 }
|
|
6345
|
|
6346 #ifdef FEAT_FOLDING
|
|
6347 /* Always open fold at the cursor line when inserting something. */
|
|
6348 foldOpenCursor();
|
|
6349 #endif
|
|
6350
|
|
6351 return (arrow_used || ins_need_undo ? FAIL : OK);
|
|
6352 }
|
|
6353
|
|
6354 /*
|
840
|
6355 * Do a few things to stop inserting.
|
|
6356 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
|
|
6357 * to another window/buffer.
|
7
|
6358 */
|
|
6359 static void
|
|
6360 stop_insert(end_insert_pos, esc)
|
840
|
6361 pos_T *end_insert_pos;
|
603
|
6362 int esc; /* called by ins_esc() */
|
7
|
6363 {
|
603
|
6364 int cc;
|
|
6365 char_u *ptr;
|
7
|
6366
|
|
6367 stop_redo_ins();
|
|
6368 replace_flush(); /* abandon replace stack */
|
|
6369
|
|
6370 /*
|
603
|
6371 * Save the inserted text for later redo with ^@ and CTRL-A.
|
|
6372 * Don't do it when "restart_edit" was set and nothing was inserted,
|
|
6373 * otherwise CTRL-O w and then <Left> will clear "last_insert".
|
7
|
6374 */
|
603
|
6375 ptr = get_inserted();
|
615
|
6376 if (did_restart_edit == 0 || (ptr != NULL
|
|
6377 && (int)STRLEN(ptr) > new_insert_skip))
|
603
|
6378 {
|
|
6379 vim_free(last_insert);
|
|
6380 last_insert = ptr;
|
|
6381 last_insert_skip = new_insert_skip;
|
|
6382 }
|
|
6383 else
|
|
6384 vim_free(ptr);
|
7
|
6385
|
840
|
6386 if (!arrow_used && end_insert_pos != NULL)
|
7
|
6387 {
|
|
6388 /* Auto-format now. It may seem strange to do this when stopping an
|
|
6389 * insertion (or moving the cursor), but it's required when appending
|
|
6390 * a line and having it end in a space. But only do it when something
|
|
6391 * was actually inserted, otherwise undo won't work. */
|
10
|
6392 if (!ins_need_undo && has_format_option(FO_AUTO))
|
7
|
6393 {
|
10
|
6394 pos_T tpos = curwin->w_cursor;
|
|
6395
|
7
|
6396 /* When the cursor is at the end of the line after a space the
|
|
6397 * formatting will move it to the following word. Avoid that by
|
|
6398 * moving the cursor onto the space. */
|
|
6399 cc = 'x';
|
|
6400 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
|
|
6401 {
|
|
6402 dec_cursor();
|
|
6403 cc = gchar_cursor();
|
|
6404 if (!vim_iswhite(cc))
|
10
|
6405 curwin->w_cursor = tpos;
|
7
|
6406 }
|
|
6407
|
|
6408 auto_format(TRUE, FALSE);
|
|
6409
|
10
|
6410 if (vim_iswhite(cc))
|
|
6411 {
|
|
6412 if (gchar_cursor() != NUL)
|
|
6413 inc_cursor();
|
|
6414 #ifdef FEAT_VIRTUALEDIT
|
|
6415 /* If the cursor is still at the same character, also keep
|
|
6416 * the "coladd". */
|
|
6417 if (gchar_cursor() == NUL
|
|
6418 && curwin->w_cursor.lnum == tpos.lnum
|
|
6419 && curwin->w_cursor.col == tpos.col)
|
|
6420 curwin->w_cursor.coladd = tpos.coladd;
|
|
6421 #endif
|
|
6422 }
|
7
|
6423 }
|
|
6424
|
|
6425 /* If a space was inserted for auto-formatting, remove it now. */
|
|
6426 check_auto_format(TRUE);
|
|
6427
|
|
6428 /* If we just did an auto-indent, remove the white space from the end
|
10
|
6429 * of the line, and put the cursor back.
|
|
6430 * Do this when ESC was used or moving the cursor up/down. */
|
|
6431 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
|
840
|
6432 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
|
7
|
6433 {
|
10
|
6434 pos_T tpos = curwin->w_cursor;
|
|
6435
|
|
6436 curwin->w_cursor = *end_insert_pos;
|
786
|
6437 for (;;)
|
|
6438 {
|
|
6439 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
|
|
6440 --curwin->w_cursor.col;
|
|
6441 cc = gchar_cursor();
|
|
6442 if (!vim_iswhite(cc))
|
|
6443 break;
|
7
|
6444 (void)del_char(TRUE);
|
786
|
6445 }
|
10
|
6446 if (curwin->w_cursor.lnum != tpos.lnum)
|
|
6447 curwin->w_cursor = tpos;
|
|
6448 else if (cc != NUL)
|
7
|
6449 ++curwin->w_cursor.col; /* put cursor back on the NUL */
|
|
6450
|
|
6451 #ifdef FEAT_VISUAL
|
|
6452 /* <C-S-Right> may have started Visual mode, adjust the position for
|
|
6453 * deleted characters. */
|
|
6454 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
|
|
6455 {
|
835
|
6456 cc = (int)STRLEN(ml_get_curline());
|
7
|
6457 if (VIsual.col > (colnr_T)cc)
|
|
6458 {
|
|
6459 VIsual.col = cc;
|
|
6460 # ifdef FEAT_VIRTUALEDIT
|
|
6461 VIsual.coladd = 0;
|
|
6462 # endif
|
|
6463 }
|
|
6464 }
|
|
6465 #endif
|
|
6466 }
|
|
6467 }
|
|
6468 did_ai = FALSE;
|
|
6469 #ifdef FEAT_SMARTINDENT
|
|
6470 did_si = FALSE;
|
|
6471 can_si = FALSE;
|
|
6472 can_si_back = FALSE;
|
|
6473 #endif
|
|
6474
|
840
|
6475 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
|
|
6476 * now in a different buffer. */
|
|
6477 if (end_insert_pos != NULL)
|
|
6478 {
|
|
6479 curbuf->b_op_start = Insstart;
|
|
6480 curbuf->b_op_end = *end_insert_pos;
|
|
6481 }
|
7
|
6482 }
|
|
6483
|
|
6484 /*
|
|
6485 * Set the last inserted text to a single character.
|
|
6486 * Used for the replace command.
|
|
6487 */
|
|
6488 void
|
|
6489 set_last_insert(c)
|
|
6490 int c;
|
|
6491 {
|
|
6492 char_u *s;
|
|
6493
|
|
6494 vim_free(last_insert);
|
|
6495 #ifdef FEAT_MBYTE
|
|
6496 last_insert = alloc(MB_MAXBYTES * 3 + 5);
|
|
6497 #else
|
|
6498 last_insert = alloc(6);
|
|
6499 #endif
|
|
6500 if (last_insert != NULL)
|
|
6501 {
|
|
6502 s = last_insert;
|
|
6503 /* Use the CTRL-V only when entering a special char */
|
|
6504 if (c < ' ' || c == DEL)
|
|
6505 *s++ = Ctrl_V;
|
|
6506 s = add_char2buf(c, s);
|
|
6507 *s++ = ESC;
|
|
6508 *s++ = NUL;
|
|
6509 last_insert_skip = 0;
|
|
6510 }
|
|
6511 }
|
|
6512
|
359
|
6513 #if defined(EXITFREE) || defined(PROTO)
|
|
6514 void
|
|
6515 free_last_insert()
|
|
6516 {
|
|
6517 vim_free(last_insert);
|
|
6518 last_insert = NULL;
|
1446
|
6519 # ifdef FEAT_INS_EXPAND
|
694
|
6520 vim_free(compl_orig_text);
|
|
6521 compl_orig_text = NULL;
|
1446
|
6522 # endif
|
359
|
6523 }
|
|
6524 #endif
|
|
6525
|
7
|
6526 /*
|
|
6527 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
|
|
6528 * and CSI. Handle multi-byte characters.
|
|
6529 * Returns a pointer to after the added bytes.
|
|
6530 */
|
|
6531 char_u *
|
|
6532 add_char2buf(c, s)
|
|
6533 int c;
|
|
6534 char_u *s;
|
|
6535 {
|
|
6536 #ifdef FEAT_MBYTE
|
|
6537 char_u temp[MB_MAXBYTES];
|
|
6538 int i;
|
|
6539 int len;
|
|
6540
|
|
6541 len = (*mb_char2bytes)(c, temp);
|
|
6542 for (i = 0; i < len; ++i)
|
|
6543 {
|
|
6544 c = temp[i];
|
|
6545 #endif
|
|
6546 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
|
|
6547 if (c == K_SPECIAL)
|
|
6548 {
|
|
6549 *s++ = K_SPECIAL;
|
|
6550 *s++ = KS_SPECIAL;
|
|
6551 *s++ = KE_FILLER;
|
|
6552 }
|
|
6553 #ifdef FEAT_GUI
|
|
6554 else if (c == CSI)
|
|
6555 {
|
|
6556 *s++ = CSI;
|
|
6557 *s++ = KS_EXTRA;
|
|
6558 *s++ = (int)KE_CSI;
|
|
6559 }
|
|
6560 #endif
|
|
6561 else
|
|
6562 *s++ = c;
|
|
6563 #ifdef FEAT_MBYTE
|
|
6564 }
|
|
6565 #endif
|
|
6566 return s;
|
|
6567 }
|
|
6568
|
|
6569 /*
|
|
6570 * move cursor to start of line
|
|
6571 * if flags & BL_WHITE move to first non-white
|
|
6572 * if flags & BL_SOL move to first non-white if startofline is set,
|
|
6573 * otherwise keep "curswant" column
|
|
6574 * if flags & BL_FIX don't leave the cursor on a NUL.
|
|
6575 */
|
|
6576 void
|
|
6577 beginline(flags)
|
|
6578 int flags;
|
|
6579 {
|
|
6580 if ((flags & BL_SOL) && !p_sol)
|
|
6581 coladvance(curwin->w_curswant);
|
|
6582 else
|
|
6583 {
|
|
6584 curwin->w_cursor.col = 0;
|
|
6585 #ifdef FEAT_VIRTUALEDIT
|
|
6586 curwin->w_cursor.coladd = 0;
|
|
6587 #endif
|
|
6588
|
|
6589 if (flags & (BL_WHITE | BL_SOL))
|
|
6590 {
|
|
6591 char_u *ptr;
|
|
6592
|
|
6593 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
|
|
6594 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
|
|
6595 ++curwin->w_cursor.col;
|
|
6596 }
|
|
6597 curwin->w_set_curswant = TRUE;
|
|
6598 }
|
|
6599 }
|
|
6600
|
|
6601 /*
|
|
6602 * oneright oneleft cursor_down cursor_up
|
|
6603 *
|
|
6604 * Move one char {right,left,down,up}.
|
773
|
6605 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
|
7
|
6606 * Return OK when successful, FAIL when we hit a line of file boundary.
|
|
6607 */
|
|
6608
|
|
6609 int
|
|
6610 oneright()
|
|
6611 {
|
|
6612 char_u *ptr;
|
|
6613 int l;
|
|
6614
|
|
6615 #ifdef FEAT_VIRTUALEDIT
|
|
6616 if (virtual_active())
|
|
6617 {
|
|
6618 pos_T prevpos = curwin->w_cursor;
|
|
6619
|
|
6620 /* Adjust for multi-wide char (excluding TAB) */
|
|
6621 ptr = ml_get_cursor();
|
|
6622 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
|
773
|
6623 # ifdef FEAT_MBYTE
|
7
|
6624 (*mb_ptr2char)(ptr)
|
773
|
6625 # else
|
7
|
6626 *ptr
|
773
|
6627 # endif
|
7
|
6628 ))
|
|
6629 ? ptr2cells(ptr) : 1));
|
|
6630 curwin->w_set_curswant = TRUE;
|
|
6631 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
|
|
6632 return (prevpos.col != curwin->w_cursor.col
|
|
6633 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
|
|
6634 }
|
|
6635 #endif
|
|
6636
|
|
6637 ptr = ml_get_cursor();
|
773
|
6638 if (*ptr == NUL)
|
|
6639 return FAIL; /* already at the very end */
|
|
6640
|
7
|
6641 #ifdef FEAT_MBYTE
|
773
|
6642 if (has_mbyte)
|
|
6643 l = (*mb_ptr2len)(ptr);
|
7
|
6644 else
|
|
6645 #endif
|
773
|
6646 l = 1;
|
|
6647
|
|
6648 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
|
|
6649 * contains "onemore". */
|
|
6650 if (ptr[l] == NUL
|
|
6651 #ifdef FEAT_VIRTUALEDIT
|
|
6652 && (ve_flags & VE_ONEMORE) == 0
|
|
6653 #endif
|
|
6654 )
|
|
6655 return FAIL;
|
|
6656 curwin->w_cursor.col += l;
|
7
|
6657
|
|
6658 curwin->w_set_curswant = TRUE;
|
|
6659 return OK;
|
|
6660 }
|
|
6661
|
|
6662 int
|
|
6663 oneleft()
|
|
6664 {
|
|
6665 #ifdef FEAT_VIRTUALEDIT
|
|
6666 if (virtual_active())
|
|
6667 {
|
|
6668 int width;
|
|
6669 int v = getviscol();
|
|
6670
|
|
6671 if (v == 0)
|
|
6672 return FAIL;
|
|
6673
|
|
6674 # ifdef FEAT_LINEBREAK
|
|
6675 /* We might get stuck on 'showbreak', skip over it. */
|
|
6676 width = 1;
|
|
6677 for (;;)
|
|
6678 {
|
|
6679 coladvance(v - width);
|
|
6680 /* getviscol() is slow, skip it when 'showbreak' is empty and
|
|
6681 * there are no multi-byte characters */
|
|
6682 if ((*p_sbr == NUL
|
|
6683 # ifdef FEAT_MBYTE
|
|
6684 && !has_mbyte
|
|
6685 # endif
|
|
6686 ) || getviscol() < v)
|
|
6687 break;
|
|
6688 ++width;
|
|
6689 }
|
|
6690 # else
|
|
6691 coladvance(v - 1);
|
|
6692 # endif
|
|
6693
|
|
6694 if (curwin->w_cursor.coladd == 1)
|
|
6695 {
|
|
6696 char_u *ptr;
|
|
6697
|
|
6698 /* Adjust for multi-wide char (not a TAB) */
|
|
6699 ptr = ml_get_cursor();
|
|
6700 if (*ptr != TAB && vim_isprintc(
|
|
6701 # ifdef FEAT_MBYTE
|
|
6702 (*mb_ptr2char)(ptr)
|
|
6703 # else
|
|
6704 *ptr
|
|
6705 # endif
|
|
6706 ) && ptr2cells(ptr) > 1)
|
|
6707 curwin->w_cursor.coladd = 0;
|
|
6708 }
|
|
6709
|
|
6710 curwin->w_set_curswant = TRUE;
|
|
6711 return OK;
|
|
6712 }
|
|
6713 #endif
|
|
6714
|
|
6715 if (curwin->w_cursor.col == 0)
|
|
6716 return FAIL;
|
|
6717
|
|
6718 curwin->w_set_curswant = TRUE;
|
|
6719 --curwin->w_cursor.col;
|
|
6720
|
|
6721 #ifdef FEAT_MBYTE
|
|
6722 /* if the character on the left of the current cursor is a multi-byte
|
|
6723 * character, move to its first byte */
|
|
6724 if (has_mbyte)
|
|
6725 mb_adjust_cursor();
|
|
6726 #endif
|
|
6727 return OK;
|
|
6728 }
|
|
6729
|
|
6730 int
|
|
6731 cursor_up(n, upd_topline)
|
|
6732 long n;
|
|
6733 int upd_topline; /* When TRUE: update topline */
|
|
6734 {
|
|
6735 linenr_T lnum;
|
|
6736
|
|
6737 if (n > 0)
|
|
6738 {
|
|
6739 lnum = curwin->w_cursor.lnum;
|
161
|
6740 /* This fails if the cursor is already in the first line or the count
|
|
6741 * is larger than the line number and '-' is in 'cpoptions' */
|
|
6742 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
|
7
|
6743 return FAIL;
|
|
6744 if (n >= lnum)
|
|
6745 lnum = 1;
|
|
6746 else
|
|
6747 #ifdef FEAT_FOLDING
|
|
6748 if (hasAnyFolding(curwin))
|
|
6749 {
|
|
6750 /*
|
|
6751 * Count each sequence of folded lines as one logical line.
|
|
6752 */
|
|
6753 /* go to the the start of the current fold */
|
|
6754 (void)hasFolding(lnum, &lnum, NULL);
|
|
6755
|
|
6756 while (n--)
|
|
6757 {
|
|
6758 /* move up one line */
|
|
6759 --lnum;
|
|
6760 if (lnum <= 1)
|
|
6761 break;
|
|
6762 /* If we entered a fold, move to the beginning, unless in
|
|
6763 * Insert mode or when 'foldopen' contains "all": it will open
|
|
6764 * in a moment. */
|
|
6765 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
|
|
6766 (void)hasFolding(lnum, &lnum, NULL);
|
|
6767 }
|
|
6768 if (lnum < 1)
|
|
6769 lnum = 1;
|
|
6770 }
|
|
6771 else
|
|
6772 #endif
|
|
6773 lnum -= n;
|
|
6774 curwin->w_cursor.lnum = lnum;
|
|
6775 }
|
|
6776
|
|
6777 /* try to advance to the column we want to be at */
|
|
6778 coladvance(curwin->w_curswant);
|
|
6779
|
|
6780 if (upd_topline)
|
|
6781 update_topline(); /* make sure curwin->w_topline is valid */
|
|
6782
|
|
6783 return OK;
|
|
6784 }
|
|
6785
|
|
6786 /*
|
|
6787 * Cursor down a number of logical lines.
|
|
6788 */
|
|
6789 int
|
|
6790 cursor_down(n, upd_topline)
|
|
6791 long n;
|
|
6792 int upd_topline; /* When TRUE: update topline */
|
|
6793 {
|
|
6794 linenr_T lnum;
|
|
6795
|
|
6796 if (n > 0)
|
|
6797 {
|
|
6798 lnum = curwin->w_cursor.lnum;
|
|
6799 #ifdef FEAT_FOLDING
|
|
6800 /* Move to last line of fold, will fail if it's the end-of-file. */
|
|
6801 (void)hasFolding(lnum, NULL, &lnum);
|
|
6802 #endif
|
161
|
6803 /* This fails if the cursor is already in the last line or would move
|
|
6804 * beyound the last line and '-' is in 'cpoptions' */
|
|
6805 if (lnum >= curbuf->b_ml.ml_line_count
|
|
6806 || (lnum + n > curbuf->b_ml.ml_line_count
|
|
6807 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
|
7
|
6808 return FAIL;
|
|
6809 if (lnum + n >= curbuf->b_ml.ml_line_count)
|
|
6810 lnum = curbuf->b_ml.ml_line_count;
|
|
6811 else
|
|
6812 #ifdef FEAT_FOLDING
|
|
6813 if (hasAnyFolding(curwin))
|
|
6814 {
|
|
6815 linenr_T last;
|
|
6816
|
|
6817 /* count each sequence of folded lines as one logical line */
|
|
6818 while (n--)
|
|
6819 {
|
|
6820 if (hasFolding(lnum, NULL, &last))
|
|
6821 lnum = last + 1;
|
|
6822 else
|
|
6823 ++lnum;
|
|
6824 if (lnum >= curbuf->b_ml.ml_line_count)
|
|
6825 break;
|
|
6826 }
|
|
6827 if (lnum > curbuf->b_ml.ml_line_count)
|
|
6828 lnum = curbuf->b_ml.ml_line_count;
|
|
6829 }
|
|
6830 else
|
|
6831 #endif
|
|
6832 lnum += n;
|
|
6833 curwin->w_cursor.lnum = lnum;
|
|
6834 }
|
|
6835
|
|
6836 /* try to advance to the column we want to be at */
|
|
6837 coladvance(curwin->w_curswant);
|
|
6838
|
|
6839 if (upd_topline)
|
|
6840 update_topline(); /* make sure curwin->w_topline is valid */
|
|
6841
|
|
6842 return OK;
|
|
6843 }
|
|
6844
|
|
6845 /*
|
|
6846 * Stuff the last inserted text in the read buffer.
|
|
6847 * Last_insert actually is a copy of the redo buffer, so we
|
|
6848 * first have to remove the command.
|
|
6849 */
|
|
6850 int
|
|
6851 stuff_inserted(c, count, no_esc)
|
|
6852 int c; /* Command character to be inserted */
|
|
6853 long count; /* Repeat this many times */
|
|
6854 int no_esc; /* Don't add an ESC at the end */
|
|
6855 {
|
|
6856 char_u *esc_ptr;
|
|
6857 char_u *ptr;
|
|
6858 char_u *last_ptr;
|
|
6859 char_u last = NUL;
|
|
6860
|
|
6861 ptr = get_last_insert();
|
|
6862 if (ptr == NULL)
|
|
6863 {
|
|
6864 EMSG(_(e_noinstext));
|
|
6865 return FAIL;
|
|
6866 }
|
|
6867
|
|
6868 /* may want to stuff the command character, to start Insert mode */
|
|
6869 if (c != NUL)
|
|
6870 stuffcharReadbuff(c);
|
|
6871 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
|
|
6872 *esc_ptr = NUL; /* remove the ESC */
|
|
6873
|
|
6874 /* when the last char is either "0" or "^" it will be quoted if no ESC
|
|
6875 * comes after it OR if it will inserted more than once and "ptr"
|
|
6876 * starts with ^D. -- Acevedo
|
|
6877 */
|
|
6878 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
|
|
6879 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
|
|
6880 && (no_esc || (*ptr == Ctrl_D && count > 1)))
|
|
6881 {
|
|
6882 last = *last_ptr;
|
|
6883 *last_ptr = NUL;
|
|
6884 }
|
|
6885
|
|
6886 do
|
|
6887 {
|
|
6888 stuffReadbuff(ptr);
|
|
6889 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
|
|
6890 if (last)
|
|
6891 stuffReadbuff((char_u *)(last == '0'
|
|
6892 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
|
|
6893 : IF_EB("\026^", CTRL_V_STR "^")));
|
|
6894 }
|
|
6895 while (--count > 0);
|
|
6896
|
|
6897 if (last)
|
|
6898 *last_ptr = last;
|
|
6899
|
|
6900 if (esc_ptr != NULL)
|
|
6901 *esc_ptr = ESC; /* put the ESC back */
|
|
6902
|
|
6903 /* may want to stuff a trailing ESC, to get out of Insert mode */
|
|
6904 if (!no_esc)
|
|
6905 stuffcharReadbuff(ESC);
|
|
6906
|
|
6907 return OK;
|
|
6908 }
|
|
6909
|
|
6910 char_u *
|
|
6911 get_last_insert()
|
|
6912 {
|
|
6913 if (last_insert == NULL)
|
|
6914 return NULL;
|
|
6915 return last_insert + last_insert_skip;
|
|
6916 }
|
|
6917
|
|
6918 /*
|
|
6919 * Get last inserted string, and remove trailing <Esc>.
|
|
6920 * Returns pointer to allocated memory (must be freed) or NULL.
|
|
6921 */
|
|
6922 char_u *
|
|
6923 get_last_insert_save()
|
|
6924 {
|
|
6925 char_u *s;
|
|
6926 int len;
|
|
6927
|
|
6928 if (last_insert == NULL)
|
|
6929 return NULL;
|
|
6930 s = vim_strsave(last_insert + last_insert_skip);
|
|
6931 if (s != NULL)
|
|
6932 {
|
|
6933 len = (int)STRLEN(s);
|
|
6934 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
|
|
6935 s[len - 1] = NUL;
|
|
6936 }
|
|
6937 return s;
|
|
6938 }
|
|
6939
|
|
6940 /*
|
|
6941 * Check the word in front of the cursor for an abbreviation.
|
|
6942 * Called when the non-id character "c" has been entered.
|
|
6943 * When an abbreviation is recognized it is removed from the text and
|
|
6944 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
|
|
6945 */
|
|
6946 static int
|
|
6947 echeck_abbr(c)
|
|
6948 int c;
|
|
6949 {
|
|
6950 /* Don't check for abbreviation in paste mode, when disabled and just
|
|
6951 * after moving around with cursor keys. */
|
|
6952 if (p_paste || no_abbr || arrow_used)
|
|
6953 return FALSE;
|
|
6954
|
|
6955 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
|
|
6956 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
|
|
6957 }
|
|
6958
|
|
6959 /*
|
|
6960 * replace-stack functions
|
|
6961 *
|
|
6962 * When replacing characters, the replaced characters are remembered for each
|
|
6963 * new character. This is used to re-insert the old text when backspacing.
|
|
6964 *
|
|
6965 * There is a NUL headed list of characters for each character that is
|
|
6966 * currently in the file after the insertion point. When BS is used, one NUL
|
|
6967 * headed list is put back for the deleted character.
|
|
6968 *
|
|
6969 * For a newline, there are two NUL headed lists. One contains the characters
|
|
6970 * that the NL replaced. The extra one stores the characters after the cursor
|
|
6971 * that were deleted (always white space).
|
|
6972 *
|
|
6973 * Replace_offset is normally 0, in which case replace_push will add a new
|
|
6974 * character at the end of the stack. If replace_offset is not 0, that many
|
|
6975 * characters will be left on the stack above the newly inserted character.
|
|
6976 */
|
|
6977
|
298
|
6978 static char_u *replace_stack = NULL;
|
|
6979 static long replace_stack_nr = 0; /* next entry in replace stack */
|
|
6980 static long replace_stack_len = 0; /* max. number of entries */
|
7
|
6981
|
|
6982 void
|
|
6983 replace_push(c)
|
|
6984 int c; /* character that is replaced (NUL is none) */
|
|
6985 {
|
|
6986 char_u *p;
|
|
6987
|
|
6988 if (replace_stack_nr < replace_offset) /* nothing to do */
|
|
6989 return;
|
|
6990 if (replace_stack_len <= replace_stack_nr)
|
|
6991 {
|
|
6992 replace_stack_len += 50;
|
|
6993 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
|
|
6994 if (p == NULL) /* out of memory */
|
|
6995 {
|
|
6996 replace_stack_len -= 50;
|
|
6997 return;
|
|
6998 }
|
|
6999 if (replace_stack != NULL)
|
|
7000 {
|
|
7001 mch_memmove(p, replace_stack,
|
|
7002 (size_t)(replace_stack_nr * sizeof(char_u)));
|
|
7003 vim_free(replace_stack);
|
|
7004 }
|
|
7005 replace_stack = p;
|
|
7006 }
|
|
7007 p = replace_stack + replace_stack_nr - replace_offset;
|
|
7008 if (replace_offset)
|
|
7009 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
|
|
7010 *p = c;
|
|
7011 ++replace_stack_nr;
|
|
7012 }
|
|
7013
|
1470
|
7014 #if defined(FEAT_MBYTE) || defined(PROTO)
|
|
7015 /*
|
|
7016 * Push a character onto the replace stack. Handles a multi-byte character in
|
|
7017 * reverse byte order, so that the first byte is popped off first.
|
|
7018 * Return the number of bytes done (includes composing characters).
|
|
7019 */
|
|
7020 int
|
|
7021 replace_push_mb(p)
|
|
7022 char_u *p;
|
|
7023 {
|
|
7024 int l = (*mb_ptr2len)(p);
|
|
7025 int j;
|
|
7026
|
|
7027 for (j = l - 1; j >= 0; --j)
|
|
7028 replace_push(p[j]);
|
|
7029 return l;
|
|
7030 }
|
|
7031 #endif
|
|
7032
|
840
|
7033 #if 0
|
7
|
7034 /*
|
|
7035 * call replace_push(c) with replace_offset set to the first NUL.
|
|
7036 */
|
|
7037 static void
|
|
7038 replace_push_off(c)
|
|
7039 int c;
|
|
7040 {
|
|
7041 char_u *p;
|
|
7042
|
|
7043 p = replace_stack + replace_stack_nr;
|
|
7044 for (replace_offset = 1; replace_offset < replace_stack_nr;
|
|
7045 ++replace_offset)
|
|
7046 if (*--p == NUL)
|
|
7047 break;
|
|
7048 replace_push(c);
|
|
7049 replace_offset = 0;
|
|
7050 }
|
840
|
7051 #endif
|
7
|
7052
|
|
7053 /*
|
|
7054 * Pop one item from the replace stack.
|
|
7055 * return -1 if stack empty
|
|
7056 * return replaced character or NUL otherwise
|
|
7057 */
|
|
7058 static int
|
|
7059 replace_pop()
|
|
7060 {
|
|
7061 if (replace_stack_nr == 0)
|
|
7062 return -1;
|
|
7063 return (int)replace_stack[--replace_stack_nr];
|
|
7064 }
|
|
7065
|
|
7066 /*
|
|
7067 * Join the top two items on the replace stack. This removes to "off"'th NUL
|
|
7068 * encountered.
|
|
7069 */
|
|
7070 static void
|
|
7071 replace_join(off)
|
|
7072 int off; /* offset for which NUL to remove */
|
|
7073 {
|
|
7074 int i;
|
|
7075
|
|
7076 for (i = replace_stack_nr; --i >= 0; )
|
|
7077 if (replace_stack[i] == NUL && off-- <= 0)
|
|
7078 {
|
|
7079 --replace_stack_nr;
|
|
7080 mch_memmove(replace_stack + i, replace_stack + i + 1,
|
|
7081 (size_t)(replace_stack_nr - i));
|
|
7082 return;
|
|
7083 }
|
|
7084 }
|
|
7085
|
|
7086 /*
|
|
7087 * Pop bytes from the replace stack until a NUL is found, and insert them
|
|
7088 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
|
|
7089 */
|
|
7090 static void
|
|
7091 replace_pop_ins()
|
|
7092 {
|
|
7093 int cc;
|
|
7094 int oldState = State;
|
|
7095
|
|
7096 State = NORMAL; /* don't want REPLACE here */
|
|
7097 while ((cc = replace_pop()) > 0)
|
|
7098 {
|
|
7099 #ifdef FEAT_MBYTE
|
|
7100 mb_replace_pop_ins(cc);
|
|
7101 #else
|
|
7102 ins_char(cc);
|
|
7103 #endif
|
|
7104 dec_cursor();
|
|
7105 }
|
|
7106 State = oldState;
|
|
7107 }
|
|
7108
|
|
7109 #ifdef FEAT_MBYTE
|
|
7110 /*
|
|
7111 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
|
|
7112 * indicates a multi-byte char, pop the other bytes too.
|
|
7113 */
|
|
7114 static void
|
|
7115 mb_replace_pop_ins(cc)
|
|
7116 int cc;
|
|
7117 {
|
|
7118 int n;
|
|
7119 char_u buf[MB_MAXBYTES];
|
|
7120 int i;
|
|
7121 int c;
|
|
7122
|
|
7123 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
|
|
7124 {
|
|
7125 buf[0] = cc;
|
|
7126 for (i = 1; i < n; ++i)
|
|
7127 buf[i] = replace_pop();
|
|
7128 ins_bytes_len(buf, n);
|
|
7129 }
|
|
7130 else
|
|
7131 ins_char(cc);
|
|
7132
|
|
7133 if (enc_utf8)
|
|
7134 /* Handle composing chars. */
|
|
7135 for (;;)
|
|
7136 {
|
|
7137 c = replace_pop();
|
|
7138 if (c == -1) /* stack empty */
|
|
7139 break;
|
|
7140 if ((n = MB_BYTE2LEN(c)) == 1)
|
|
7141 {
|
|
7142 /* Not a multi-byte char, put it back. */
|
|
7143 replace_push(c);
|
|
7144 break;
|
|
7145 }
|
|
7146 else
|
|
7147 {
|
|
7148 buf[0] = c;
|
|
7149 for (i = 1; i < n; ++i)
|
|
7150 buf[i] = replace_pop();
|
|
7151 if (utf_iscomposing(utf_ptr2char(buf)))
|
|
7152 ins_bytes_len(buf, n);
|
|
7153 else
|
|
7154 {
|
|
7155 /* Not a composing char, put it back. */
|
|
7156 for (i = n - 1; i >= 0; --i)
|
|
7157 replace_push(buf[i]);
|
|
7158 break;
|
|
7159 }
|
|
7160 }
|
|
7161 }
|
|
7162 }
|
|
7163 #endif
|
|
7164
|
|
7165 /*
|
|
7166 * make the replace stack empty
|
|
7167 * (called when exiting replace mode)
|
|
7168 */
|
|
7169 static void
|
|
7170 replace_flush()
|
|
7171 {
|
|
7172 vim_free(replace_stack);
|
|
7173 replace_stack = NULL;
|
|
7174 replace_stack_len = 0;
|
|
7175 replace_stack_nr = 0;
|
|
7176 }
|
|
7177
|
|
7178 /*
|
|
7179 * Handle doing a BS for one character.
|
|
7180 * cc < 0: replace stack empty, just move cursor
|
|
7181 * cc == 0: character was inserted, delete it
|
|
7182 * cc > 0: character was replaced, put cc (first byte of original char) back
|
|
7183 * and check for more characters to be put back
|
1782
|
7184 * When "limit_col" is >= 0, don't delete before this column. Matters when
|
|
7185 * using composing characters, use del_char_after_col() instead of del_char().
|
7
|
7186 */
|
|
7187 static void
|
1782
|
7188 replace_do_bs(limit_col)
|
|
7189 int limit_col;
|
7
|
7190 {
|
|
7191 int cc;
|
|
7192 #ifdef FEAT_VREPLACE
|
|
7193 int orig_len = 0;
|
|
7194 int ins_len;
|
|
7195 int orig_vcols = 0;
|
|
7196 colnr_T start_vcol;
|
|
7197 char_u *p;
|
|
7198 int i;
|
|
7199 int vcol;
|
|
7200 #endif
|
|
7201
|
|
7202 cc = replace_pop();
|
|
7203 if (cc > 0)
|
|
7204 {
|
|
7205 #ifdef FEAT_VREPLACE
|
|
7206 if (State & VREPLACE_FLAG)
|
|
7207 {
|
|
7208 /* Get the number of screen cells used by the character we are
|
|
7209 * going to delete. */
|
|
7210 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
|
|
7211 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
|
|
7212 }
|
|
7213 #endif
|
|
7214 #ifdef FEAT_MBYTE
|
|
7215 if (has_mbyte)
|
|
7216 {
|
1782
|
7217 (void)del_char_after_col(limit_col);
|
7
|
7218 # ifdef FEAT_VREPLACE
|
|
7219 if (State & VREPLACE_FLAG)
|
835
|
7220 orig_len = (int)STRLEN(ml_get_cursor());
|
7
|
7221 # endif
|
|
7222 replace_push(cc);
|
|
7223 }
|
|
7224 else
|
|
7225 #endif
|
|
7226 {
|
|
7227 pchar_cursor(cc);
|
|
7228 #ifdef FEAT_VREPLACE
|
|
7229 if (State & VREPLACE_FLAG)
|
835
|
7230 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
|
7
|
7231 #endif
|
|
7232 }
|
|
7233 replace_pop_ins();
|
|
7234
|
|
7235 #ifdef FEAT_VREPLACE
|
|
7236 if (State & VREPLACE_FLAG)
|
|
7237 {
|
|
7238 /* Get the number of screen cells used by the inserted characters */
|
|
7239 p = ml_get_cursor();
|
835
|
7240 ins_len = (int)STRLEN(p) - orig_len;
|
7
|
7241 vcol = start_vcol;
|
|
7242 for (i = 0; i < ins_len; ++i)
|
|
7243 {
|
|
7244 vcol += chartabsize(p + i, vcol);
|
|
7245 #ifdef FEAT_MBYTE
|
474
|
7246 i += (*mb_ptr2len)(p) - 1;
|
7
|
7247 #endif
|
|
7248 }
|
|
7249 vcol -= start_vcol;
|
|
7250
|
|
7251 /* Delete spaces that were inserted after the cursor to keep the
|
|
7252 * text aligned. */
|
|
7253 curwin->w_cursor.col += ins_len;
|
|
7254 while (vcol > orig_vcols && gchar_cursor() == ' ')
|
|
7255 {
|
|
7256 del_char(FALSE);
|
|
7257 ++orig_vcols;
|
|
7258 }
|
|
7259 curwin->w_cursor.col -= ins_len;
|
|
7260 }
|
|
7261 #endif
|
|
7262
|
|
7263 /* mark the buffer as changed and prepare for displaying */
|
|
7264 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
|
|
7265 }
|
|
7266 else if (cc == 0)
|
1782
|
7267 (void)del_char_after_col(limit_col);
|
7
|
7268 }
|
|
7269
|
|
7270 #ifdef FEAT_CINDENT
|
|
7271 /*
|
|
7272 * Return TRUE if C-indenting is on.
|
|
7273 */
|
|
7274 static int
|
|
7275 cindent_on()
|
|
7276 {
|
|
7277 return (!p_paste && (curbuf->b_p_cin
|
|
7278 # ifdef FEAT_EVAL
|
|
7279 || *curbuf->b_p_inde != NUL
|
|
7280 # endif
|
|
7281 ));
|
|
7282 }
|
|
7283 #endif
|
|
7284
|
|
7285 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
|
|
7286 /*
|
|
7287 * Re-indent the current line, based on the current contents of it and the
|
|
7288 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
|
|
7289 * confused what all the part that handles Control-T is doing that I'm not.
|
|
7290 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
|
|
7291 */
|
|
7292
|
|
7293 void
|
|
7294 fixthisline(get_the_indent)
|
|
7295 int (*get_the_indent) __ARGS((void));
|
|
7296 {
|
1516
|
7297 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
|
7
|
7298 if (linewhite(curwin->w_cursor.lnum))
|
|
7299 did_ai = TRUE; /* delete the indent if the line stays empty */
|
|
7300 }
|
|
7301
|
|
7302 void
|
|
7303 fix_indent()
|
|
7304 {
|
|
7305 if (p_paste)
|
|
7306 return;
|
|
7307 # ifdef FEAT_LISP
|
|
7308 if (curbuf->b_p_lisp && curbuf->b_p_ai)
|
|
7309 fixthisline(get_lisp_indent);
|
|
7310 # endif
|
|
7311 # if defined(FEAT_LISP) && defined(FEAT_CINDENT)
|
|
7312 else
|
|
7313 # endif
|
|
7314 # ifdef FEAT_CINDENT
|
|
7315 if (cindent_on())
|
|
7316 do_c_expr_indent();
|
|
7317 # endif
|
|
7318 }
|
|
7319
|
|
7320 #endif
|
|
7321
|
|
7322 #ifdef FEAT_CINDENT
|
|
7323 /*
|
|
7324 * return TRUE if 'cinkeys' contains the key "keytyped",
|
|
7325 * when == '*': Only if key is preceded with '*' (indent before insert)
|
|
7326 * when == '!': Only if key is prededed with '!' (don't insert)
|
|
7327 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
|
|
7328 *
|
|
7329 * "keytyped" can have a few special values:
|
|
7330 * KEY_OPEN_FORW
|
|
7331 * KEY_OPEN_BACK
|
|
7332 * KEY_COMPLETE just finished completion.
|
|
7333 *
|
|
7334 * If line_is_empty is TRUE accept keys with '0' before them.
|
|
7335 */
|
|
7336 int
|
|
7337 in_cinkeys(keytyped, when, line_is_empty)
|
|
7338 int keytyped;
|
|
7339 int when;
|
|
7340 int line_is_empty;
|
|
7341 {
|
|
7342 char_u *look;
|
|
7343 int try_match;
|
|
7344 int try_match_word;
|
|
7345 char_u *p;
|
|
7346 char_u *line;
|
|
7347 int icase;
|
|
7348 int i;
|
|
7349
|
|
7350 #ifdef FEAT_EVAL
|
|
7351 if (*curbuf->b_p_inde != NUL)
|
|
7352 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
|
|
7353 else
|
|
7354 #endif
|
|
7355 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
|
|
7356 while (*look)
|
|
7357 {
|
|
7358 /*
|
|
7359 * Find out if we want to try a match with this key, depending on
|
|
7360 * 'when' and a '*' or '!' before the key.
|
|
7361 */
|
|
7362 switch (when)
|
|
7363 {
|
|
7364 case '*': try_match = (*look == '*'); break;
|
|
7365 case '!': try_match = (*look == '!'); break;
|
|
7366 default: try_match = (*look != '*'); break;
|
|
7367 }
|
|
7368 if (*look == '*' || *look == '!')
|
|
7369 ++look;
|
|
7370
|
|
7371 /*
|
|
7372 * If there is a '0', only accept a match if the line is empty.
|
|
7373 * But may still match when typing last char of a word.
|
|
7374 */
|
|
7375 if (*look == '0')
|
|
7376 {
|
|
7377 try_match_word = try_match;
|
|
7378 if (!line_is_empty)
|
|
7379 try_match = FALSE;
|
|
7380 ++look;
|
|
7381 }
|
|
7382 else
|
|
7383 try_match_word = FALSE;
|
|
7384
|
|
7385 /*
|
|
7386 * does it look like a control character?
|
|
7387 */
|
|
7388 if (*look == '^'
|
|
7389 #ifdef EBCDIC
|
|
7390 && (Ctrl_chr(look[1]) != 0)
|
|
7391 #else
|
|
7392 && look[1] >= '?' && look[1] <= '_'
|
|
7393 #endif
|
|
7394 )
|
|
7395 {
|
|
7396 if (try_match && keytyped == Ctrl_chr(look[1]))
|
|
7397 return TRUE;
|
|
7398 look += 2;
|
|
7399 }
|
|
7400 /*
|
|
7401 * 'o' means "o" command, open forward.
|
|
7402 * 'O' means "O" command, open backward.
|
|
7403 */
|
|
7404 else if (*look == 'o')
|
|
7405 {
|
|
7406 if (try_match && keytyped == KEY_OPEN_FORW)
|
|
7407 return TRUE;
|
|
7408 ++look;
|
|
7409 }
|
|
7410 else if (*look == 'O')
|
|
7411 {
|
|
7412 if (try_match && keytyped == KEY_OPEN_BACK)
|
|
7413 return TRUE;
|
|
7414 ++look;
|
|
7415 }
|
|
7416
|
|
7417 /*
|
|
7418 * 'e' means to check for "else" at start of line and just before the
|
|
7419 * cursor.
|
|
7420 */
|
|
7421 else if (*look == 'e')
|
|
7422 {
|
|
7423 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
|
|
7424 {
|
|
7425 p = ml_get_curline();
|
|
7426 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
|
|
7427 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
|
|
7428 return TRUE;
|
|
7429 }
|
|
7430 ++look;
|
|
7431 }
|
|
7432
|
|
7433 /*
|
|
7434 * ':' only causes an indent if it is at the end of a label or case
|
|
7435 * statement, or when it was before typing the ':' (to fix
|
|
7436 * class::method for C++).
|
|
7437 */
|
|
7438 else if (*look == ':')
|
|
7439 {
|
|
7440 if (try_match && keytyped == ':')
|
|
7441 {
|
|
7442 p = ml_get_curline();
|
|
7443 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
|
|
7444 return TRUE;
|
1300
|
7445 /* Need to get the line again after cin_islabel(). */
|
|
7446 p = ml_get_curline();
|
7
|
7447 if (curwin->w_cursor.col > 2
|
|
7448 && p[curwin->w_cursor.col - 1] == ':'
|
|
7449 && p[curwin->w_cursor.col - 2] == ':')
|
|
7450 {
|
|
7451 p[curwin->w_cursor.col - 1] = ' ';
|
|
7452 i = (cin_iscase(p) || cin_isscopedecl(p)
|
|
7453 || cin_islabel(30));
|
|
7454 p = ml_get_curline();
|
|
7455 p[curwin->w_cursor.col - 1] = ':';
|
|
7456 if (i)
|
|
7457 return TRUE;
|
|
7458 }
|
|
7459 }
|
|
7460 ++look;
|
|
7461 }
|
|
7462
|
|
7463
|
|
7464 /*
|
|
7465 * Is it a key in <>, maybe?
|
|
7466 */
|
|
7467 else if (*look == '<')
|
|
7468 {
|
|
7469 if (try_match)
|
|
7470 {
|
|
7471 /*
|
|
7472 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
|
|
7473 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
|
|
7474 * >, *, : and ! keys if they really really want to.
|
|
7475 */
|
|
7476 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
|
|
7477 && keytyped == look[1])
|
|
7478 return TRUE;
|
|
7479
|
|
7480 if (keytyped == get_special_key_code(look + 1))
|
|
7481 return TRUE;
|
|
7482 }
|
|
7483 while (*look && *look != '>')
|
|
7484 look++;
|
|
7485 while (*look == '>')
|
|
7486 look++;
|
|
7487 }
|
|
7488
|
|
7489 /*
|
|
7490 * Is it a word: "=word"?
|
|
7491 */
|
|
7492 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
|
|
7493 {
|
|
7494 ++look;
|
|
7495 if (*look == '~')
|
|
7496 {
|
|
7497 icase = TRUE;
|
|
7498 ++look;
|
|
7499 }
|
|
7500 else
|
|
7501 icase = FALSE;
|
|
7502 p = vim_strchr(look, ',');
|
|
7503 if (p == NULL)
|
|
7504 p = look + STRLEN(look);
|
|
7505 if ((try_match || try_match_word)
|
|
7506 && curwin->w_cursor.col >= (colnr_T)(p - look))
|
|
7507 {
|
|
7508 int match = FALSE;
|
|
7509
|
|
7510 #ifdef FEAT_INS_EXPAND
|
|
7511 if (keytyped == KEY_COMPLETE)
|
|
7512 {
|
|
7513 char_u *s;
|
|
7514
|
|
7515 /* Just completed a word, check if it starts with "look".
|
|
7516 * search back for the start of a word. */
|
|
7517 line = ml_get_curline();
|
|
7518 # ifdef FEAT_MBYTE
|
|
7519 if (has_mbyte)
|
|
7520 {
|
|
7521 char_u *n;
|
|
7522
|
|
7523 for (s = line + curwin->w_cursor.col; s > line; s = n)
|
|
7524 {
|
|
7525 n = mb_prevptr(line, s);
|
|
7526 if (!vim_iswordp(n))
|
|
7527 break;
|
|
7528 }
|
|
7529 }
|
|
7530 else
|
|
7531 # endif
|
|
7532 for (s = line + curwin->w_cursor.col; s > line; --s)
|
|
7533 if (!vim_iswordc(s[-1]))
|
|
7534 break;
|
|
7535 if (s + (p - look) <= line + curwin->w_cursor.col
|
|
7536 && (icase
|
|
7537 ? MB_STRNICMP(s, look, p - look)
|
|
7538 : STRNCMP(s, look, p - look)) == 0)
|
|
7539 match = TRUE;
|
|
7540 }
|
|
7541 else
|
|
7542 #endif
|
|
7543 /* TODO: multi-byte */
|
|
7544 if (keytyped == (int)p[-1] || (icase && keytyped < 256
|
|
7545 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
|
|
7546 {
|
|
7547 line = ml_get_cursor();
|
|
7548 if ((curwin->w_cursor.col == (colnr_T)(p - look)
|
|
7549 || !vim_iswordc(line[-(p - look) - 1]))
|
|
7550 && (icase
|
|
7551 ? MB_STRNICMP(line - (p - look), look, p - look)
|
|
7552 : STRNCMP(line - (p - look), look, p - look))
|
|
7553 == 0)
|
|
7554 match = TRUE;
|
|
7555 }
|
|
7556 if (match && try_match_word && !try_match)
|
|
7557 {
|
|
7558 /* "0=word": Check if there are only blanks before the
|
|
7559 * word. */
|
|
7560 line = ml_get_curline();
|
|
7561 if ((int)(skipwhite(line) - line) !=
|
|
7562 (int)(curwin->w_cursor.col - (p - look)))
|
|
7563 match = FALSE;
|
|
7564 }
|
|
7565 if (match)
|
|
7566 return TRUE;
|
|
7567 }
|
|
7568 look = p;
|
|
7569 }
|
|
7570
|
|
7571 /*
|
|
7572 * ok, it's a boring generic character.
|
|
7573 */
|
|
7574 else
|
|
7575 {
|
|
7576 if (try_match && *look == keytyped)
|
|
7577 return TRUE;
|
|
7578 ++look;
|
|
7579 }
|
|
7580
|
|
7581 /*
|
|
7582 * Skip over ", ".
|
|
7583 */
|
|
7584 look = skip_to_option_part(look);
|
|
7585 }
|
|
7586 return FALSE;
|
|
7587 }
|
|
7588 #endif /* FEAT_CINDENT */
|
|
7589
|
|
7590 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
|
|
7591 /*
|
|
7592 * Map Hebrew keyboard when in hkmap mode.
|
|
7593 */
|
|
7594 int
|
|
7595 hkmap(c)
|
|
7596 int c;
|
|
7597 {
|
|
7598 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
|
|
7599 {
|
|
7600 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
|
|
7601 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
|
|
7602 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
|
|
7603 static char_u map[26] =
|
|
7604 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
|
|
7605 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
|
|
7606 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
|
|
7607 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
|
|
7608 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
|
|
7609 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
|
|
7610 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
|
|
7611 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
|
|
7612 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
|
|
7613
|
|
7614 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
|
|
7615 return (int)(map[CharOrd(c)] - 1 + p_aleph);
|
|
7616 /* '-1'='sofit' */
|
|
7617 else if (c == 'x')
|
|
7618 return 'X';
|
|
7619 else if (c == 'q')
|
|
7620 return '\''; /* {geresh}={'} */
|
|
7621 else if (c == 246)
|
|
7622 return ' '; /* \"o --> ' ' for a german keyboard */
|
|
7623 else if (c == 228)
|
|
7624 return ' '; /* \"a --> ' ' -- / -- */
|
|
7625 else if (c == 252)
|
|
7626 return ' '; /* \"u --> ' ' -- / -- */
|
|
7627 #ifdef EBCDIC
|
|
7628 else if (islower(c))
|
|
7629 #else
|
|
7630 /* NOTE: islower() does not do the right thing for us on Linux so we
|
|
7631 * do this the same was as 5.7 and previous, so it works correctly on
|
|
7632 * all systems. Specifically, the e.g. Delete and Arrow keys are
|
|
7633 * munged and won't work if e.g. searching for Hebrew text.
|
|
7634 */
|
|
7635 else if (c >= 'a' && c <= 'z')
|
|
7636 #endif
|
|
7637 return (int)(map[CharOrdLow(c)] + p_aleph);
|
|
7638 else
|
|
7639 return c;
|
|
7640 }
|
|
7641 else
|
|
7642 {
|
|
7643 switch (c)
|
|
7644 {
|
|
7645 case '`': return ';';
|
|
7646 case '/': return '.';
|
|
7647 case '\'': return ',';
|
|
7648 case 'q': return '/';
|
|
7649 case 'w': return '\'';
|
|
7650
|
|
7651 /* Hebrew letters - set offset from 'a' */
|
|
7652 case ',': c = '{'; break;
|
|
7653 case '.': c = 'v'; break;
|
|
7654 case ';': c = 't'; break;
|
|
7655 default: {
|
|
7656 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
|
|
7657
|
|
7658 #ifdef EBCDIC
|
|
7659 /* see note about islower() above */
|
|
7660 if (!islower(c))
|
|
7661 #else
|
|
7662 if (c < 'a' || c > 'z')
|
|
7663 #endif
|
|
7664 return c;
|
|
7665 c = str[CharOrdLow(c)];
|
|
7666 break;
|
|
7667 }
|
|
7668 }
|
|
7669
|
|
7670 return (int)(CharOrdLow(c) + p_aleph);
|
|
7671 }
|
|
7672 }
|
|
7673 #endif
|
|
7674
|
|
7675 static void
|
|
7676 ins_reg()
|
|
7677 {
|
|
7678 int need_redraw = FALSE;
|
|
7679 int regname;
|
|
7680 int literally = 0;
|
844
|
7681 #ifdef FEAT_VISUAL
|
|
7682 int vis_active = VIsual_active;
|
|
7683 #endif
|
7
|
7684
|
|
7685 /*
|
|
7686 * If we are going to wait for a character, show a '"'.
|
|
7687 */
|
|
7688 pc_status = PC_STATUS_UNSET;
|
|
7689 if (redrawing() && !char_avail())
|
|
7690 {
|
|
7691 /* may need to redraw when no more chars available now */
|
661
|
7692 ins_redraw(FALSE);
|
7
|
7693
|
|
7694 edit_putchar('"', TRUE);
|
|
7695 #ifdef FEAT_CMDL_INFO
|
|
7696 add_to_showcmd_c(Ctrl_R);
|
|
7697 #endif
|
|
7698 }
|
|
7699
|
|
7700 #ifdef USE_ON_FLY_SCROLL
|
|
7701 dont_scroll = TRUE; /* disallow scrolling here */
|
|
7702 #endif
|
|
7703
|
|
7704 /*
|
|
7705 * Don't map the register name. This also prevents the mode message to be
|
|
7706 * deleted when ESC is hit.
|
|
7707 */
|
|
7708 ++no_mapping;
|
1389
|
7709 regname = plain_vgetc();
|
7
|
7710 LANGMAP_ADJUST(regname, TRUE);
|
|
7711 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
|
|
7712 {
|
|
7713 /* Get a third key for literal register insertion */
|
|
7714 literally = regname;
|
|
7715 #ifdef FEAT_CMDL_INFO
|
|
7716 add_to_showcmd_c(literally);
|
|
7717 #endif
|
1389
|
7718 regname = plain_vgetc();
|
7
|
7719 LANGMAP_ADJUST(regname, TRUE);
|
|
7720 }
|
|
7721 --no_mapping;
|
|
7722
|
|
7723 #ifdef FEAT_EVAL
|
|
7724 /*
|
|
7725 * Don't call u_sync() while getting the expression,
|
|
7726 * evaluating it or giving an error message for it!
|
|
7727 */
|
|
7728 ++no_u_sync;
|
|
7729 if (regname == '=')
|
|
7730 {
|
133
|
7731 # ifdef USE_IM_CONTROL
|
7
|
7732 int im_on = im_get_status();
|
133
|
7733 # endif
|
7
|
7734 regname = get_expr_register();
|
133
|
7735 # ifdef USE_IM_CONTROL
|
7
|
7736 /* Restore the Input Method. */
|
|
7737 if (im_on)
|
|
7738 im_set_active(TRUE);
|
133
|
7739 # endif
|
7
|
7740 }
|
140
|
7741 if (regname == NUL || !valid_yank_reg(regname, FALSE))
|
|
7742 {
|
|
7743 vim_beep();
|
7
|
7744 need_redraw = TRUE; /* remove the '"' */
|
140
|
7745 }
|
7
|
7746 else
|
|
7747 {
|
|
7748 #endif
|
|
7749 if (literally == Ctrl_O || literally == Ctrl_P)
|
|
7750 {
|
|
7751 /* Append the command to the redo buffer. */
|
|
7752 AppendCharToRedobuff(Ctrl_R);
|
|
7753 AppendCharToRedobuff(literally);
|
|
7754 AppendCharToRedobuff(regname);
|
|
7755
|
|
7756 do_put(regname, BACKWARD, 1L,
|
|
7757 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
|
|
7758 }
|
|
7759 else if (insert_reg(regname, literally) == FAIL)
|
|
7760 {
|
|
7761 vim_beep();
|
|
7762 need_redraw = TRUE; /* remove the '"' */
|
|
7763 }
|
133
|
7764 else if (stop_insert_mode)
|
|
7765 /* When the '=' register was used and a function was invoked that
|
|
7766 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
|
|
7767 * insert anything, need to remove the '"' */
|
|
7768 need_redraw = TRUE;
|
|
7769
|
7
|
7770 #ifdef FEAT_EVAL
|
|
7771 }
|
|
7772 --no_u_sync;
|
|
7773 #endif
|
|
7774 #ifdef FEAT_CMDL_INFO
|
|
7775 clear_showcmd();
|
|
7776 #endif
|
|
7777
|
|
7778 /* If the inserted register is empty, we need to remove the '"' */
|
|
7779 if (need_redraw || stuff_empty())
|
|
7780 edit_unputchar();
|
844
|
7781
|
|
7782 #ifdef FEAT_VISUAL
|
|
7783 /* Disallow starting Visual mode here, would get a weird mode. */
|
|
7784 if (!vis_active && VIsual_active)
|
|
7785 end_visual_mode();
|
|
7786 #endif
|
7
|
7787 }
|
|
7788
|
|
7789 /*
|
|
7790 * CTRL-G commands in Insert mode.
|
|
7791 */
|
|
7792 static void
|
|
7793 ins_ctrl_g()
|
|
7794 {
|
|
7795 int c;
|
|
7796
|
|
7797 #ifdef FEAT_INS_EXPAND
|
|
7798 /* Right after CTRL-X the cursor will be after the ruler. */
|
|
7799 setcursor();
|
|
7800 #endif
|
|
7801
|
|
7802 /*
|
|
7803 * Don't map the second key. This also prevents the mode message to be
|
|
7804 * deleted when ESC is hit.
|
|
7805 */
|
|
7806 ++no_mapping;
|
1389
|
7807 c = plain_vgetc();
|
7
|
7808 --no_mapping;
|
|
7809 switch (c)
|
|
7810 {
|
|
7811 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
|
|
7812 case K_UP:
|
|
7813 case Ctrl_K:
|
|
7814 case 'k': ins_up(TRUE);
|
|
7815 break;
|
|
7816
|
|
7817 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
|
|
7818 case K_DOWN:
|
|
7819 case Ctrl_J:
|
|
7820 case 'j': ins_down(TRUE);
|
|
7821 break;
|
|
7822
|
|
7823 /* CTRL-G u: start new undoable edit */
|
825
|
7824 case 'u': u_sync(TRUE);
|
7
|
7825 ins_need_undo = TRUE;
|
626
|
7826
|
|
7827 /* Need to reset Insstart, esp. because a BS that joins
|
1219
|
7828 * a line to the previous one must save for undo. */
|
626
|
7829 Insstart = curwin->w_cursor;
|
7
|
7830 break;
|
|
7831
|
|
7832 /* Unknown CTRL-G command, reserved for future expansion. */
|
|
7833 default: vim_beep();
|
|
7834 }
|
|
7835 }
|
|
7836
|
|
7837 /*
|
449
|
7838 * CTRL-^ in Insert mode.
|
|
7839 */
|
|
7840 static void
|
|
7841 ins_ctrl_hat()
|
|
7842 {
|
782
|
7843 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
|
449
|
7844 {
|
|
7845 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
|
|
7846 if (State & LANGMAP)
|
|
7847 {
|
|
7848 curbuf->b_p_iminsert = B_IMODE_NONE;
|
|
7849 State &= ~LANGMAP;
|
|
7850 }
|
|
7851 else
|
|
7852 {
|
|
7853 curbuf->b_p_iminsert = B_IMODE_LMAP;
|
|
7854 State |= LANGMAP;
|
|
7855 #ifdef USE_IM_CONTROL
|
|
7856 im_set_active(FALSE);
|
|
7857 #endif
|
|
7858 }
|
|
7859 }
|
|
7860 #ifdef USE_IM_CONTROL
|
|
7861 else
|
|
7862 {
|
|
7863 /* There are no ":lmap" mappings, toggle IM */
|
|
7864 if (im_get_status())
|
|
7865 {
|
|
7866 curbuf->b_p_iminsert = B_IMODE_NONE;
|
|
7867 im_set_active(FALSE);
|
|
7868 }
|
|
7869 else
|
|
7870 {
|
|
7871 curbuf->b_p_iminsert = B_IMODE_IM;
|
|
7872 State &= ~LANGMAP;
|
|
7873 im_set_active(TRUE);
|
|
7874 }
|
|
7875 }
|
|
7876 #endif
|
|
7877 set_iminsert_global();
|
|
7878 showmode();
|
|
7879 #ifdef FEAT_GUI
|
|
7880 /* may show different cursor shape or color */
|
|
7881 if (gui.in_use)
|
|
7882 gui_update_cursor(TRUE, FALSE);
|
|
7883 #endif
|
|
7884 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
|
|
7885 /* Show/unshow value of 'keymap' in status lines. */
|
|
7886 status_redraw_curbuf();
|
|
7887 #endif
|
|
7888 }
|
|
7889
|
|
7890 /*
|
7
|
7891 * Handle ESC in insert mode.
|
|
7892 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
|
|
7893 * insert.
|
|
7894 */
|
|
7895 static int
|
477
|
7896 ins_esc(count, cmdchar, nomove)
|
7
|
7897 long *count;
|
|
7898 int cmdchar;
|
477
|
7899 int nomove; /* don't move cursor */
|
7
|
7900 {
|
|
7901 int temp;
|
|
7902 static int disabled_redraw = FALSE;
|
|
7903
|
744
|
7904 #ifdef FEAT_SPELL
|
449
|
7905 check_spell_redraw();
|
|
7906 #endif
|
7
|
7907 #if defined(FEAT_HANGULIN)
|
|
7908 # if defined(ESC_CHG_TO_ENG_MODE)
|
|
7909 hangul_input_state_set(0);
|
|
7910 # endif
|
|
7911 if (composing_hangul)
|
|
7912 {
|
|
7913 push_raw_key(composing_hangul_buffer, 2);
|
|
7914 composing_hangul = 0;
|
|
7915 }
|
|
7916 #endif
|
|
7917
|
|
7918 temp = curwin->w_cursor.col;
|
|
7919 if (disabled_redraw)
|
|
7920 {
|
|
7921 --RedrawingDisabled;
|
|
7922 disabled_redraw = FALSE;
|
|
7923 }
|
|
7924 if (!arrow_used)
|
|
7925 {
|
|
7926 /*
|
|
7927 * Don't append the ESC for "r<CR>" and "grx".
|
75
|
7928 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
|
|
7929 * when "count" is non-zero.
|
7
|
7930 */
|
|
7931 if (cmdchar != 'r' && cmdchar != 'v')
|
75
|
7932 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
|
7
|
7933
|
|
7934 /*
|
|
7935 * Repeating insert may take a long time. Check for
|
|
7936 * interrupt now and then.
|
|
7937 */
|
|
7938 if (*count > 0)
|
|
7939 {
|
|
7940 line_breakcheck();
|
|
7941 if (got_int)
|
|
7942 *count = 0;
|
|
7943 }
|
|
7944
|
|
7945 if (--*count > 0) /* repeat what was typed */
|
|
7946 {
|
164
|
7947 /* Vi repeats the insert without replacing characters. */
|
|
7948 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
|
|
7949 State &= ~REPLACE_FLAG;
|
|
7950
|
7
|
7951 (void)start_redo_ins();
|
|
7952 if (cmdchar == 'r' || cmdchar == 'v')
|
|
7953 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
|
|
7954 ++RedrawingDisabled;
|
|
7955 disabled_redraw = TRUE;
|
|
7956 return FALSE; /* repeat the insert */
|
|
7957 }
|
|
7958 stop_insert(&curwin->w_cursor, TRUE);
|
|
7959 undisplay_dollar();
|
|
7960 }
|
|
7961
|
|
7962 /* When an autoindent was removed, curswant stays after the
|
|
7963 * indent */
|
|
7964 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
|
|
7965 curwin->w_set_curswant = TRUE;
|
|
7966
|
|
7967 /* Remember the last Insert position in the '^ mark. */
|
|
7968 if (!cmdmod.keepjumps)
|
|
7969 curbuf->b_last_insert = curwin->w_cursor;
|
|
7970
|
|
7971 /*
|
|
7972 * The cursor should end up on the last inserted character.
|
477
|
7973 * Don't do it for CTRL-O, unless past the end of the line.
|
7
|
7974 */
|
477
|
7975 if (!nomove
|
|
7976 && (curwin->w_cursor.col != 0
|
7
|
7977 #ifdef FEAT_VIRTUALEDIT
|
|
7978 || curwin->w_cursor.coladd > 0
|
|
7979 #endif
|
477
|
7980 )
|
|
7981 && (restart_edit == NUL
|
|
7982 || (gchar_cursor() == NUL
|
7
|
7983 #ifdef FEAT_VISUAL
|
477
|
7984 && !VIsual_active
|
|
7985 #endif
|
|
7986 ))
|
7
|
7987 #ifdef FEAT_RIGHTLEFT
|
|
7988 && !revins_on
|
|
7989 #endif
|
|
7990 )
|
|
7991 {
|
|
7992 #ifdef FEAT_VIRTUALEDIT
|
|
7993 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
|
|
7994 {
|
|
7995 oneleft();
|
|
7996 if (restart_edit != NUL)
|
|
7997 ++curwin->w_cursor.coladd;
|
|
7998 }
|
|
7999 else
|
|
8000 #endif
|
|
8001 {
|
|
8002 --curwin->w_cursor.col;
|
|
8003 #ifdef FEAT_MBYTE
|
|
8004 /* Correct cursor for multi-byte character. */
|
|
8005 if (has_mbyte)
|
|
8006 mb_adjust_cursor();
|
|
8007 #endif
|
|
8008 }
|
|
8009 }
|
|
8010
|
|
8011 #ifdef USE_IM_CONTROL
|
|
8012 /* Disable IM to allow typing English directly for Normal mode commands.
|
|
8013 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
|
|
8014 * well). */
|
|
8015 if (!(State & LANGMAP))
|
|
8016 im_save_status(&curbuf->b_p_iminsert);
|
|
8017 im_set_active(FALSE);
|
|
8018 #endif
|
|
8019
|
|
8020 State = NORMAL;
|
|
8021 /* need to position cursor again (e.g. when on a TAB ) */
|
|
8022 changed_cline_bef_curs();
|
|
8023
|
|
8024 #ifdef FEAT_MOUSE
|
|
8025 setmouse();
|
|
8026 #endif
|
|
8027 #ifdef CURSOR_SHAPE
|
|
8028 ui_cursor_shape(); /* may show different cursor shape */
|
|
8029 #endif
|
|
8030
|
|
8031 /*
|
|
8032 * When recording or for CTRL-O, need to display the new mode.
|
|
8033 * Otherwise remove the mode message.
|
|
8034 */
|
|
8035 if (Recording || restart_edit != NUL)
|
|
8036 showmode();
|
|
8037 else if (p_smd)
|
|
8038 MSG("");
|
|
8039
|
|
8040 return TRUE; /* exit Insert mode */
|
|
8041 }
|
|
8042
|
|
8043 #ifdef FEAT_RIGHTLEFT
|
|
8044 /*
|
|
8045 * Toggle language: hkmap and revins_on.
|
|
8046 * Move to end of reverse inserted text.
|
|
8047 */
|
|
8048 static void
|
|
8049 ins_ctrl_()
|
|
8050 {
|
|
8051 if (revins_on && revins_chars && revins_scol >= 0)
|
|
8052 {
|
|
8053 while (gchar_cursor() != NUL && revins_chars--)
|
|
8054 ++curwin->w_cursor.col;
|
|
8055 }
|
|
8056 p_ri = !p_ri;
|
|
8057 revins_on = (State == INSERT && p_ri);
|
|
8058 if (revins_on)
|
|
8059 {
|
|
8060 revins_scol = curwin->w_cursor.col;
|
|
8061 revins_legal++;
|
|
8062 revins_chars = 0;
|
|
8063 undisplay_dollar();
|
|
8064 }
|
|
8065 else
|
|
8066 revins_scol = -1;
|
|
8067 #ifdef FEAT_FKMAP
|
|
8068 if (p_altkeymap)
|
|
8069 {
|
|
8070 /*
|
|
8071 * to be consistent also for redo command, using '.'
|
|
8072 * set arrow_used to true and stop it - causing to redo
|
|
8073 * characters entered in one mode (normal/reverse insert).
|
|
8074 */
|
|
8075 arrow_used = TRUE;
|
|
8076 (void)stop_arrow();
|
|
8077 p_fkmap = curwin->w_p_rl ^ p_ri;
|
|
8078 if (p_fkmap && p_ri)
|
|
8079 State = INSERT;
|
|
8080 }
|
|
8081 else
|
|
8082 #endif
|
|
8083 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
|
|
8084 showmode();
|
|
8085 }
|
|
8086 #endif
|
|
8087
|
|
8088 #ifdef FEAT_VISUAL
|
|
8089 /*
|
|
8090 * If 'keymodel' contains "startsel", may start selection.
|
|
8091 * Returns TRUE when a CTRL-O and other keys stuffed.
|
|
8092 */
|
|
8093 static int
|
|
8094 ins_start_select(c)
|
|
8095 int c;
|
|
8096 {
|
|
8097 if (km_startsel)
|
|
8098 switch (c)
|
|
8099 {
|
|
8100 case K_KHOME:
|
|
8101 case K_KEND:
|
|
8102 case K_PAGEUP:
|
|
8103 case K_KPAGEUP:
|
|
8104 case K_PAGEDOWN:
|
|
8105 case K_KPAGEDOWN:
|
|
8106 # ifdef MACOS
|
|
8107 case K_LEFT:
|
|
8108 case K_RIGHT:
|
|
8109 case K_UP:
|
|
8110 case K_DOWN:
|
|
8111 case K_END:
|
|
8112 case K_HOME:
|
|
8113 # endif
|
|
8114 if (!(mod_mask & MOD_MASK_SHIFT))
|
|
8115 break;
|
|
8116 /* FALLTHROUGH */
|
|
8117 case K_S_LEFT:
|
|
8118 case K_S_RIGHT:
|
|
8119 case K_S_UP:
|
|
8120 case K_S_DOWN:
|
|
8121 case K_S_END:
|
|
8122 case K_S_HOME:
|
|
8123 /* Start selection right away, the cursor can move with
|
|
8124 * CTRL-O when beyond the end of the line. */
|
|
8125 start_selection();
|
|
8126
|
|
8127 /* Execute the key in (insert) Select mode. */
|
|
8128 stuffcharReadbuff(Ctrl_O);
|
|
8129 if (mod_mask)
|
|
8130 {
|
|
8131 char_u buf[4];
|
|
8132
|
|
8133 buf[0] = K_SPECIAL;
|
|
8134 buf[1] = KS_MODIFIER;
|
|
8135 buf[2] = mod_mask;
|
|
8136 buf[3] = NUL;
|
|
8137 stuffReadbuff(buf);
|
|
8138 }
|
|
8139 stuffcharReadbuff(c);
|
|
8140 return TRUE;
|
|
8141 }
|
|
8142 return FALSE;
|
|
8143 }
|
|
8144 #endif
|
|
8145
|
|
8146 /*
|
449
|
8147 * <Insert> key in Insert mode: toggle insert/remplace mode.
|
|
8148 */
|
|
8149 static void
|
|
8150 ins_insert(replaceState)
|
|
8151 int replaceState;
|
|
8152 {
|
|
8153 #ifdef FEAT_FKMAP
|
|
8154 if (p_fkmap && p_ri)
|
|
8155 {
|
|
8156 beep_flush();
|
|
8157 EMSG(farsi_text_3); /* encoded in Farsi */
|
|
8158 return;
|
|
8159 }
|
|
8160 #endif
|
|
8161
|
|
8162 #ifdef FEAT_AUTOCMD
|
532
|
8163 # ifdef FEAT_EVAL
|
449
|
8164 set_vim_var_string(VV_INSERTMODE,
|
|
8165 (char_u *)((State & REPLACE_FLAG) ? "i" :
|
667
|
8166 # ifdef FEAT_VREPLACE
|
|
8167 replaceState == VREPLACE ? "v" :
|
|
8168 # endif
|
|
8169 "r"), 1);
|
532
|
8170 # endif
|
449
|
8171 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
|
|
8172 #endif
|
|
8173 if (State & REPLACE_FLAG)
|
|
8174 State = INSERT | (State & LANGMAP);
|
|
8175 else
|
|
8176 State = replaceState | (State & LANGMAP);
|
|
8177 AppendCharToRedobuff(K_INS);
|
|
8178 showmode();
|
|
8179 #ifdef CURSOR_SHAPE
|
|
8180 ui_cursor_shape(); /* may show different cursor shape */
|
|
8181 #endif
|
|
8182 }
|
|
8183
|
|
8184 /*
|
|
8185 * Pressed CTRL-O in Insert mode.
|
|
8186 */
|
|
8187 static void
|
|
8188 ins_ctrl_o()
|
|
8189 {
|
|
8190 #ifdef FEAT_VREPLACE
|
|
8191 if (State & VREPLACE_FLAG)
|
|
8192 restart_edit = 'V';
|
|
8193 else
|
|
8194 #endif
|
|
8195 if (State & REPLACE_FLAG)
|
|
8196 restart_edit = 'R';
|
|
8197 else
|
|
8198 restart_edit = 'I';
|
|
8199 #ifdef FEAT_VIRTUALEDIT
|
|
8200 if (virtual_active())
|
|
8201 ins_at_eol = FALSE; /* cursor always keeps its column */
|
|
8202 else
|
|
8203 #endif
|
|
8204 ins_at_eol = (gchar_cursor() == NUL);
|
|
8205 }
|
|
8206
|
|
8207 /*
|
7
|
8208 * If the cursor is on an indent, ^T/^D insert/delete one
|
|
8209 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
|
1796
|
8210 * Always round the indent to 'shiftwidth', this is compatible
|
7
|
8211 * with vi. But vi only supports ^T and ^D after an
|
|
8212 * autoindent, we support it everywhere.
|
|
8213 */
|
|
8214 static void
|
|
8215 ins_shift(c, lastc)
|
|
8216 int c;
|
|
8217 int lastc;
|
|
8218 {
|
|
8219 if (stop_arrow() == FAIL)
|
|
8220 return;
|
|
8221 AppendCharToRedobuff(c);
|
|
8222
|
|
8223 /*
|
|
8224 * 0^D and ^^D: remove all indent.
|
|
8225 */
|
1330
|
8226 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
|
|
8227 && curwin->w_cursor.col > 0)
|
7
|
8228 {
|
|
8229 --curwin->w_cursor.col;
|
|
8230 (void)del_char(FALSE); /* delete the '^' or '0' */
|
|
8231 /* In Replace mode, restore the characters that '^' or '0' replaced. */
|
|
8232 if (State & REPLACE_FLAG)
|
|
8233 replace_pop_ins();
|
|
8234 if (lastc == '^')
|
|
8235 old_indent = get_indent(); /* remember curr. indent */
|
1516
|
8236 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
|
7
|
8237 }
|
|
8238 else
|
1516
|
8239 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
|
7
|
8240
|
|
8241 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
|
|
8242 did_ai = FALSE;
|
|
8243 #ifdef FEAT_SMARTINDENT
|
|
8244 did_si = FALSE;
|
|
8245 can_si = FALSE;
|
|
8246 can_si_back = FALSE;
|
|
8247 #endif
|
|
8248 #ifdef FEAT_CINDENT
|
|
8249 can_cindent = FALSE; /* no cindenting after ^D or ^T */
|
|
8250 #endif
|
|
8251 }
|
|
8252
|
|
8253 static void
|
|
8254 ins_del()
|
|
8255 {
|
|
8256 int temp;
|
|
8257
|
|
8258 if (stop_arrow() == FAIL)
|
|
8259 return;
|
|
8260 if (gchar_cursor() == NUL) /* delete newline */
|
|
8261 {
|
|
8262 temp = curwin->w_cursor.col;
|
|
8263 if (!can_bs(BS_EOL) /* only if "eol" included */
|
|
8264 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
|
|
8265 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
|
|
8266 || do_join(FALSE) == FAIL)
|
|
8267 vim_beep();
|
|
8268 else
|
|
8269 curwin->w_cursor.col = temp;
|
|
8270 }
|
|
8271 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
|
|
8272 vim_beep();
|
|
8273 did_ai = FALSE;
|
|
8274 #ifdef FEAT_SMARTINDENT
|
|
8275 did_si = FALSE;
|
|
8276 can_si = FALSE;
|
|
8277 can_si_back = FALSE;
|
|
8278 #endif
|
|
8279 AppendCharToRedobuff(K_DEL);
|
|
8280 }
|
|
8281
|
1460
|
8282 static void ins_bs_one __ARGS((colnr_T *vcolp));
|
|
8283
|
|
8284 /*
|
|
8285 * Delete one character for ins_bs().
|
|
8286 */
|
|
8287 static void
|
|
8288 ins_bs_one(vcolp)
|
|
8289 colnr_T *vcolp;
|
|
8290 {
|
|
8291 dec_cursor();
|
|
8292 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
|
|
8293 if (State & REPLACE_FLAG)
|
|
8294 {
|
|
8295 /* Don't delete characters before the insert point when in
|
|
8296 * Replace mode */
|
|
8297 if (curwin->w_cursor.lnum != Insstart.lnum
|
|
8298 || curwin->w_cursor.col >= Insstart.col)
|
1782
|
8299 replace_do_bs(-1);
|
1460
|
8300 }
|
|
8301 else
|
|
8302 (void)del_char(FALSE);
|
|
8303 }
|
|
8304
|
7
|
8305 /*
|
|
8306 * Handle Backspace, delete-word and delete-line in Insert mode.
|
|
8307 * Return TRUE when backspace was actually used.
|
|
8308 */
|
|
8309 static int
|
|
8310 ins_bs(c, mode, inserted_space_p)
|
|
8311 int c;
|
|
8312 int mode;
|
|
8313 int *inserted_space_p;
|
|
8314 {
|
|
8315 linenr_T lnum;
|
|
8316 int cc;
|
|
8317 int temp = 0; /* init for GCC */
|
|
8318 colnr_T mincol;
|
|
8319 int did_backspace = FALSE;
|
|
8320 int in_indent;
|
|
8321 int oldState;
|
|
8322 #ifdef FEAT_MBYTE
|
714
|
8323 int cpc[MAX_MCO]; /* composing characters */
|
7
|
8324 #endif
|
|
8325
|
|
8326 /*
|
|
8327 * can't delete anything in an empty file
|
|
8328 * can't backup past first character in buffer
|
|
8329 * can't backup past starting point unless 'backspace' > 1
|
|
8330 * can backup to a previous line if 'backspace' == 0
|
|
8331 */
|
|
8332 if ( bufempty()
|
|
8333 || (
|
|
8334 #ifdef FEAT_RIGHTLEFT
|
|
8335 !revins_on &&
|
|
8336 #endif
|
|
8337 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
|
|
8338 || (!can_bs(BS_START)
|
|
8339 && (arrow_used
|
|
8340 || (curwin->w_cursor.lnum == Insstart.lnum
|
|
8341 && curwin->w_cursor.col <= Insstart.col)))
|
|
8342 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
|
|
8343 && curwin->w_cursor.col <= ai_col)
|
|
8344 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
|
|
8345 {
|
|
8346 vim_beep();
|
|
8347 return FALSE;
|
|
8348 }
|
|
8349
|
|
8350 if (stop_arrow() == FAIL)
|
|
8351 return FALSE;
|
|
8352 in_indent = inindent(0);
|
|
8353 #ifdef FEAT_CINDENT
|
|
8354 if (in_indent)
|
|
8355 can_cindent = FALSE;
|
|
8356 #endif
|
|
8357 #ifdef FEAT_COMMENTS
|
|
8358 end_comment_pending = NUL; /* After BS, don't auto-end comment */
|
|
8359 #endif
|
|
8360 #ifdef FEAT_RIGHTLEFT
|
|
8361 if (revins_on) /* put cursor after last inserted char */
|
|
8362 inc_cursor();
|
|
8363 #endif
|
|
8364
|
|
8365 #ifdef FEAT_VIRTUALEDIT
|
|
8366 /* Virtualedit:
|
|
8367 * BACKSPACE_CHAR eats a virtual space
|
|
8368 * BACKSPACE_WORD eats all coladd
|
|
8369 * BACKSPACE_LINE eats all coladd and keeps going
|
|
8370 */
|
|
8371 if (curwin->w_cursor.coladd > 0)
|
|
8372 {
|
|
8373 if (mode == BACKSPACE_CHAR)
|
|
8374 {
|
|
8375 --curwin->w_cursor.coladd;
|
|
8376 return TRUE;
|
|
8377 }
|
|
8378 if (mode == BACKSPACE_WORD)
|
|
8379 {
|
|
8380 curwin->w_cursor.coladd = 0;
|
|
8381 return TRUE;
|
|
8382 }
|
|
8383 curwin->w_cursor.coladd = 0;
|
|
8384 }
|
|
8385 #endif
|
|
8386
|
|
8387 /*
|
|
8388 * delete newline!
|
|
8389 */
|
|
8390 if (curwin->w_cursor.col == 0)
|
|
8391 {
|
|
8392 lnum = Insstart.lnum;
|
|
8393 if (curwin->w_cursor.lnum == Insstart.lnum
|
|
8394 #ifdef FEAT_RIGHTLEFT
|
|
8395 || revins_on
|
|
8396 #endif
|
|
8397 )
|
|
8398 {
|
|
8399 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
|
|
8400 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
|
|
8401 return FALSE;
|
|
8402 --Insstart.lnum;
|
|
8403 Insstart.col = MAXCOL;
|
|
8404 }
|
|
8405 /*
|
|
8406 * In replace mode:
|
|
8407 * cc < 0: NL was inserted, delete it
|
|
8408 * cc >= 0: NL was replaced, put original characters back
|
|
8409 */
|
|
8410 cc = -1;
|
|
8411 if (State & REPLACE_FLAG)
|
|
8412 cc = replace_pop(); /* returns -1 if NL was inserted */
|
|
8413 /*
|
|
8414 * In replace mode, in the line we started replacing, we only move the
|
|
8415 * cursor.
|
|
8416 */
|
|
8417 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
|
|
8418 {
|
|
8419 dec_cursor();
|
|
8420 }
|
|
8421 else
|
|
8422 {
|
|
8423 #ifdef FEAT_VREPLACE
|
|
8424 if (!(State & VREPLACE_FLAG)
|
|
8425 || curwin->w_cursor.lnum > orig_line_count)
|
|
8426 #endif
|
|
8427 {
|
|
8428 temp = gchar_cursor(); /* remember current char */
|
|
8429 --curwin->w_cursor.lnum;
|
278
|
8430
|
|
8431 /* When "aw" is in 'formatoptions' we must delete the space at
|
|
8432 * the end of the line, otherwise the line will be broken
|
|
8433 * again when auto-formatting. */
|
|
8434 if (has_format_option(FO_AUTO)
|
|
8435 && has_format_option(FO_WHITE_PAR))
|
|
8436 {
|
|
8437 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
|
|
8438 TRUE);
|
|
8439 int len;
|
|
8440
|
835
|
8441 len = (int)STRLEN(ptr);
|
278
|
8442 if (len > 0 && ptr[len - 1] == ' ')
|
|
8443 ptr[len - 1] = NUL;
|
|
8444 }
|
|
8445
|
7
|
8446 (void)do_join(FALSE);
|
|
8447 if (temp == NUL && gchar_cursor() != NUL)
|
|
8448 inc_cursor();
|
|
8449 }
|
|
8450 #ifdef FEAT_VREPLACE
|
|
8451 else
|
|
8452 dec_cursor();
|
|
8453 #endif
|
|
8454
|
|
8455 /*
|
|
8456 * In REPLACE mode we have to put back the text that was replaced
|
|
8457 * by the NL. On the replace stack is first a NUL-terminated
|
|
8458 * sequence of characters that were deleted and then the
|
|
8459 * characters that NL replaced.
|
|
8460 */
|
|
8461 if (State & REPLACE_FLAG)
|
|
8462 {
|
|
8463 /*
|
|
8464 * Do the next ins_char() in NORMAL state, to
|
|
8465 * prevent ins_char() from replacing characters and
|
|
8466 * avoiding showmatch().
|
|
8467 */
|
|
8468 oldState = State;
|
|
8469 State = NORMAL;
|
|
8470 /*
|
|
8471 * restore characters (blanks) deleted after cursor
|
|
8472 */
|
|
8473 while (cc > 0)
|
|
8474 {
|
|
8475 temp = curwin->w_cursor.col;
|
|
8476 #ifdef FEAT_MBYTE
|
|
8477 mb_replace_pop_ins(cc);
|
|
8478 #else
|
|
8479 ins_char(cc);
|
|
8480 #endif
|
|
8481 curwin->w_cursor.col = temp;
|
|
8482 cc = replace_pop();
|
|
8483 }
|
|
8484 /* restore the characters that NL replaced */
|
|
8485 replace_pop_ins();
|
|
8486 State = oldState;
|
|
8487 }
|
|
8488 }
|
|
8489 did_ai = FALSE;
|
|
8490 }
|
|
8491 else
|
|
8492 {
|
|
8493 /*
|
|
8494 * Delete character(s) before the cursor.
|
|
8495 */
|
|
8496 #ifdef FEAT_RIGHTLEFT
|
|
8497 if (revins_on) /* put cursor on last inserted char */
|
|
8498 dec_cursor();
|
|
8499 #endif
|
|
8500 mincol = 0;
|
|
8501 /* keep indent */
|
1085
|
8502 if (mode == BACKSPACE_LINE
|
|
8503 && (curbuf->b_p_ai
|
|
8504 #ifdef FEAT_CINDENT
|
|
8505 || cindent_on()
|
|
8506 #endif
|
|
8507 )
|
7
|
8508 #ifdef FEAT_RIGHTLEFT
|
|
8509 && !revins_on
|
|
8510 #endif
|
|
8511 )
|
|
8512 {
|
|
8513 temp = curwin->w_cursor.col;
|
|
8514 beginline(BL_WHITE);
|
|
8515 if (curwin->w_cursor.col < (colnr_T)temp)
|
|
8516 mincol = curwin->w_cursor.col;
|
|
8517 curwin->w_cursor.col = temp;
|
|
8518 }
|
|
8519
|
|
8520 /*
|
|
8521 * Handle deleting one 'shiftwidth' or 'softtabstop'.
|
|
8522 */
|
|
8523 if ( mode == BACKSPACE_CHAR
|
|
8524 && ((p_sta && in_indent)
|
648
|
8525 || (curbuf->b_p_sts != 0
|
1497
|
8526 && curwin->w_cursor.col > 0
|
7
|
8527 && (*(ml_get_cursor() - 1) == TAB
|
|
8528 || (*(ml_get_cursor() - 1) == ' '
|
|
8529 && (!*inserted_space_p
|
|
8530 || arrow_used))))))
|
|
8531 {
|
|
8532 int ts;
|
|
8533 colnr_T vcol;
|
|
8534 colnr_T want_vcol;
|
1460
|
8535 colnr_T start_vcol;
|
7
|
8536
|
|
8537 *inserted_space_p = FALSE;
|
648
|
8538 if (p_sta && in_indent)
|
7
|
8539 ts = curbuf->b_p_sw;
|
|
8540 else
|
|
8541 ts = curbuf->b_p_sts;
|
|
8542 /* Compute the virtual column where we want to be. Since
|
|
8543 * 'showbreak' may get in the way, need to get the last column of
|
|
8544 * the previous character. */
|
|
8545 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
|
1460
|
8546 start_vcol = vcol;
|
7
|
8547 dec_cursor();
|
|
8548 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
|
|
8549 inc_cursor();
|
|
8550 want_vcol = (want_vcol / ts) * ts;
|
|
8551
|
|
8552 /* delete characters until we are at or before want_vcol */
|
|
8553 while (vcol > want_vcol
|
|
8554 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
|
1460
|
8555 ins_bs_one(&vcol);
|
7
|
8556
|
|
8557 /* insert extra spaces until we are at want_vcol */
|
|
8558 while (vcol < want_vcol)
|
|
8559 {
|
|
8560 /* Remember the first char we inserted */
|
|
8561 if (curwin->w_cursor.lnum == Insstart.lnum
|
|
8562 && curwin->w_cursor.col < Insstart.col)
|
|
8563 Insstart.col = curwin->w_cursor.col;
|
|
8564
|
|
8565 #ifdef FEAT_VREPLACE
|
|
8566 if (State & VREPLACE_FLAG)
|
|
8567 ins_char(' ');
|
|
8568 else
|
|
8569 #endif
|
|
8570 {
|
|
8571 ins_str((char_u *)" ");
|
1460
|
8572 if ((State & REPLACE_FLAG))
|
|
8573 replace_push(NUL);
|
7
|
8574 }
|
|
8575 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
|
|
8576 }
|
1460
|
8577
|
|
8578 /* If we are now back where we started delete one character. Can
|
|
8579 * happen when using 'sts' and 'linebreak'. */
|
|
8580 if (vcol >= start_vcol)
|
|
8581 ins_bs_one(&vcol);
|
7
|
8582 }
|
|
8583
|
|
8584 /*
|
|
8585 * Delete upto starting point, start of line or previous word.
|
|
8586 */
|
|
8587 else do
|
|
8588 {
|
|
8589 #ifdef FEAT_RIGHTLEFT
|
|
8590 if (!revins_on) /* put cursor on char to be deleted */
|
|
8591 #endif
|
|
8592 dec_cursor();
|
|
8593
|
|
8594 /* start of word? */
|
|
8595 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
|
|
8596 {
|
|
8597 mode = BACKSPACE_WORD_NOT_SPACE;
|
|
8598 temp = vim_iswordc(gchar_cursor());
|
|
8599 }
|
|
8600 /* end of word? */
|
|
8601 else if (mode == BACKSPACE_WORD_NOT_SPACE
|
|
8602 && (vim_isspace(cc = gchar_cursor())
|
|
8603 || vim_iswordc(cc) != temp))
|
|
8604 {
|
|
8605 #ifdef FEAT_RIGHTLEFT
|
|
8606 if (!revins_on)
|
|
8607 #endif
|
|
8608 inc_cursor();
|
|
8609 #ifdef FEAT_RIGHTLEFT
|
|
8610 else if (State & REPLACE_FLAG)
|
|
8611 dec_cursor();
|
|
8612 #endif
|
|
8613 break;
|
|
8614 }
|
|
8615 if (State & REPLACE_FLAG)
|
1782
|
8616 replace_do_bs(-1);
|
7
|
8617 else
|
|
8618 {
|
|
8619 #ifdef FEAT_MBYTE
|
|
8620 if (enc_utf8 && p_deco)
|
714
|
8621 (void)utfc_ptr2char(ml_get_cursor(), cpc);
|
7
|
8622 #endif
|
|
8623 (void)del_char(FALSE);
|
|
8624 #ifdef FEAT_MBYTE
|
|
8625 /*
|
714
|
8626 * If there are combining characters and 'delcombine' is set
|
|
8627 * move the cursor back. Don't back up before the base
|
7
|
8628 * character.
|
|
8629 */
|
714
|
8630 if (enc_utf8 && p_deco && cpc[0] != NUL)
|
7
|
8631 inc_cursor();
|
|
8632 #endif
|
|
8633 #ifdef FEAT_RIGHTLEFT
|
|
8634 if (revins_chars)
|
|
8635 {
|
|
8636 revins_chars--;
|
|
8637 revins_legal++;
|
|
8638 }
|
|
8639 if (revins_on && gchar_cursor() == NUL)
|
|
8640 break;
|
|
8641 #endif
|
|
8642 }
|
|
8643 /* Just a single backspace?: */
|
|
8644 if (mode == BACKSPACE_CHAR)
|
|
8645 break;
|
|
8646 } while (
|
|
8647 #ifdef FEAT_RIGHTLEFT
|
|
8648 revins_on ||
|
|
8649 #endif
|
|
8650 (curwin->w_cursor.col > mincol
|
|
8651 && (curwin->w_cursor.lnum != Insstart.lnum
|
|
8652 || curwin->w_cursor.col != Insstart.col)));
|
|
8653 did_backspace = TRUE;
|
|
8654 }
|
|
8655 #ifdef FEAT_SMARTINDENT
|
|
8656 did_si = FALSE;
|
|
8657 can_si = FALSE;
|
|
8658 can_si_back = FALSE;
|
|
8659 #endif
|
|
8660 if (curwin->w_cursor.col <= 1)
|
|
8661 did_ai = FALSE;
|
|
8662 /*
|
|
8663 * It's a little strange to put backspaces into the redo
|
|
8664 * buffer, but it makes auto-indent a lot easier to deal
|
|
8665 * with.
|
|
8666 */
|
|
8667 AppendCharToRedobuff(c);
|
|
8668
|
|
8669 /* If deleted before the insertion point, adjust it */
|
|
8670 if (curwin->w_cursor.lnum == Insstart.lnum
|
|
8671 && curwin->w_cursor.col < Insstart.col)
|
|
8672 Insstart.col = curwin->w_cursor.col;
|
|
8673
|
|
8674 /* vi behaviour: the cursor moves backward but the character that
|
|
8675 * was there remains visible
|
|
8676 * Vim behaviour: the cursor moves backward and the character that
|
|
8677 * was there is erased from the screen.
|
|
8678 * We can emulate the vi behaviour by pretending there is a dollar
|
|
8679 * displayed even when there isn't.
|
|
8680 * --pkv Sun Jan 19 01:56:40 EST 2003 */
|
|
8681 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
|
|
8682 dollar_vcol = curwin->w_virtcol;
|
|
8683
|
1514
|
8684 #ifdef FEAT_FOLDING
|
|
8685 /* When deleting a char the cursor line must never be in a closed fold.
|
|
8686 * E.g., when 'foldmethod' is indent and deleting the first non-white
|
|
8687 * char before a Tab. */
|
|
8688 if (did_backspace)
|
|
8689 foldOpenCursor();
|
|
8690 #endif
|
|
8691
|
7
|
8692 return did_backspace;
|
|
8693 }
|
|
8694
|
|
8695 #ifdef FEAT_MOUSE
|
|
8696 static void
|
|
8697 ins_mouse(c)
|
|
8698 int c;
|
|
8699 {
|
|
8700 pos_T tpos;
|
840
|
8701 win_T *old_curwin = curwin;
|
7
|
8702
|
|
8703 # ifdef FEAT_GUI
|
|
8704 /* When GUI is active, also move/paste when 'mouse' is empty */
|
|
8705 if (!gui.in_use)
|
|
8706 # endif
|
|
8707 if (!mouse_has(MOUSE_INSERT))
|
|
8708 return;
|
|
8709
|
|
8710 undisplay_dollar();
|
|
8711 tpos = curwin->w_cursor;
|
|
8712 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
|
|
8713 {
|
840
|
8714 #ifdef FEAT_WINDOWS
|
|
8715 win_T *new_curwin = curwin;
|
|
8716
|
|
8717 if (curwin != old_curwin && win_valid(old_curwin))
|
|
8718 {
|
|
8719 /* Mouse took us to another window. We need to go back to the
|
|
8720 * previous one to stop insert there properly. */
|
|
8721 curwin = old_curwin;
|
|
8722 curbuf = curwin->w_buffer;
|
|
8723 }
|
|
8724 #endif
|
|
8725 start_arrow(curwin == old_curwin ? &tpos : NULL);
|
|
8726 #ifdef FEAT_WINDOWS
|
|
8727 if (curwin != new_curwin && win_valid(new_curwin))
|
|
8728 {
|
|
8729 curwin = new_curwin;
|
|
8730 curbuf = curwin->w_buffer;
|
|
8731 }
|
|
8732 #endif
|
7
|
8733 # ifdef FEAT_CINDENT
|
|
8734 can_cindent = TRUE;
|
|
8735 # endif
|
|
8736 }
|
|
8737
|
|
8738 #ifdef FEAT_WINDOWS
|
|
8739 /* redraw status lines (in case another window became active) */
|
|
8740 redraw_statuslines();
|
|
8741 #endif
|
|
8742 }
|
|
8743
|
|
8744 static void
|
|
8745 ins_mousescroll(up)
|
|
8746 int up;
|
|
8747 {
|
|
8748 pos_T tpos;
|
1434
|
8749 # if defined(FEAT_WINDOWS)
|
|
8750 win_T *old_curwin = curwin;
|
|
8751 # endif
|
|
8752 # ifdef FEAT_INS_EXPAND
|
|
8753 int did_scroll = FALSE;
|
7
|
8754 # endif
|
|
8755
|
|
8756 tpos = curwin->w_cursor;
|
|
8757
|
|
8758 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
|
|
8759 /* Currently the mouse coordinates are only known in the GUI. */
|
|
8760 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
|
|
8761 {
|
|
8762 int row, col;
|
|
8763
|
|
8764 row = mouse_row;
|
|
8765 col = mouse_col;
|
|
8766
|
|
8767 /* find the window at the pointer coordinates */
|
|
8768 curwin = mouse_find_win(&row, &col);
|
|
8769 curbuf = curwin->w_buffer;
|
|
8770 }
|
|
8771 if (curwin == old_curwin)
|
|
8772 # endif
|
|
8773 undisplay_dollar();
|
|
8774
|
1434
|
8775 # ifdef FEAT_INS_EXPAND
|
|
8776 /* Don't scroll the window in which completion is being done. */
|
|
8777 if (!pum_visible()
|
|
8778 # if defined(FEAT_WINDOWS)
|
|
8779 || curwin != old_curwin
|
|
8780 # endif
|
|
8781 )
|
|
8782 # endif
|
|
8783 {
|
|
8784 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
|
|
8785 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
|
|
8786 else
|
|
8787 scroll_redraw(up, 3L);
|
|
8788 # ifdef FEAT_INS_EXPAND
|
|
8789 did_scroll = TRUE;
|
|
8790 # endif
|
|
8791 }
|
7
|
8792
|
|
8793 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
|
|
8794 curwin->w_redr_status = TRUE;
|
|
8795
|
|
8796 curwin = old_curwin;
|
|
8797 curbuf = curwin->w_buffer;
|
|
8798 # endif
|
|
8799
|
1434
|
8800 # ifdef FEAT_INS_EXPAND
|
|
8801 /* The popup menu may overlay the window, need to redraw it.
|
|
8802 * TODO: Would be more efficient to only redraw the windows that are
|
|
8803 * overlapped by the popup menu. */
|
|
8804 if (pum_visible() && did_scroll)
|
|
8805 {
|
|
8806 redraw_all_later(NOT_VALID);
|
|
8807 ins_compl_show_pum();
|
|
8808 }
|
|
8809 # endif
|
|
8810
|
7
|
8811 if (!equalpos(curwin->w_cursor, tpos))
|
|
8812 {
|
|
8813 start_arrow(&tpos);
|
|
8814 # ifdef FEAT_CINDENT
|
|
8815 can_cindent = TRUE;
|
|
8816 # endif
|
|
8817 }
|
|
8818 }
|
|
8819 #endif
|
|
8820
|
692
|
8821 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
|
724
|
8822 static void
|
692
|
8823 ins_tabline(c)
|
|
8824 int c;
|
|
8825 {
|
|
8826 /* We will be leaving the current window, unless closing another tab. */
|
|
8827 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
|
|
8828 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
|
|
8829 {
|
|
8830 undisplay_dollar();
|
|
8831 start_arrow(&curwin->w_cursor);
|
|
8832 # ifdef FEAT_CINDENT
|
|
8833 can_cindent = TRUE;
|
|
8834 # endif
|
|
8835 }
|
|
8836
|
|
8837 if (c == K_TABLINE)
|
|
8838 goto_tabpage(current_tab);
|
|
8839 else
|
846
|
8840 {
|
692
|
8841 handle_tabmenu();
|
846
|
8842 redraw_statuslines(); /* will redraw the tabline when needed */
|
|
8843 }
|
692
|
8844 }
|
|
8845 #endif
|
|
8846
|
|
8847 #if defined(FEAT_GUI) || defined(PROTO)
|
7
|
8848 void
|
|
8849 ins_scroll()
|
|
8850 {
|
|
8851 pos_T tpos;
|
|
8852
|
|
8853 undisplay_dollar();
|
|
8854 tpos = curwin->w_cursor;
|
|
8855 if (gui_do_scroll())
|
|
8856 {
|
|
8857 start_arrow(&tpos);
|
|
8858 # ifdef FEAT_CINDENT
|
|
8859 can_cindent = TRUE;
|
|
8860 # endif
|
|
8861 }
|
|
8862 }
|
|
8863
|
|
8864 void
|
|
8865 ins_horscroll()
|
|
8866 {
|
|
8867 pos_T tpos;
|
|
8868
|
|
8869 undisplay_dollar();
|
|
8870 tpos = curwin->w_cursor;
|
|
8871 if (gui_do_horiz_scroll())
|
|
8872 {
|
|
8873 start_arrow(&tpos);
|
|
8874 # ifdef FEAT_CINDENT
|
|
8875 can_cindent = TRUE;
|
|
8876 # endif
|
|
8877 }
|
|
8878 }
|
|
8879 #endif
|
|
8880
|
|
8881 static void
|
|
8882 ins_left()
|
|
8883 {
|
|
8884 pos_T tpos;
|
|
8885
|
|
8886 #ifdef FEAT_FOLDING
|
|
8887 if ((fdo_flags & FDO_HOR) && KeyTyped)
|
|
8888 foldOpenCursor();
|
|
8889 #endif
|
|
8890 undisplay_dollar();
|
|
8891 tpos = curwin->w_cursor;
|
|
8892 if (oneleft() == OK)
|
|
8893 {
|
941
|
8894 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
|
|
8895 /* Only call start_arrow() when not busy with preediting, it will
|
|
8896 * break undo. K_LEFT is inserted in im_correct_cursor(). */
|
|
8897 if (!im_is_preediting())
|
|
8898 #endif
|
|
8899 start_arrow(&tpos);
|
7
|
8900 #ifdef FEAT_RIGHTLEFT
|
|
8901 /* If exit reversed string, position is fixed */
|
|
8902 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
|
|
8903 revins_legal++;
|
|
8904 revins_chars++;
|
|
8905 #endif
|
|
8906 }
|
|
8907
|
|
8908 /*
|
|
8909 * if 'whichwrap' set for cursor in insert mode may go to
|
|
8910 * previous line
|
|
8911 */
|
|
8912 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
|
|
8913 {
|
|
8914 start_arrow(&tpos);
|
|
8915 --(curwin->w_cursor.lnum);
|
|
8916 coladvance((colnr_T)MAXCOL);
|
|
8917 curwin->w_set_curswant = TRUE; /* so we stay at the end */
|
|
8918 }
|
|
8919 else
|
|
8920 vim_beep();
|
|
8921 }
|
|
8922
|
|
8923 static void
|
|
8924 ins_home(c)
|
|
8925 int c;
|
|
8926 {
|
|
8927 pos_T tpos;
|
|
8928
|
|
8929 #ifdef FEAT_FOLDING
|
|
8930 if ((fdo_flags & FDO_HOR) && KeyTyped)
|
|
8931 foldOpenCursor();
|
|
8932 #endif
|
|
8933 undisplay_dollar();
|
|
8934 tpos = curwin->w_cursor;
|
|
8935 if (c == K_C_HOME)
|
|
8936 curwin->w_cursor.lnum = 1;
|
|
8937 curwin->w_cursor.col = 0;
|
|
8938 #ifdef FEAT_VIRTUALEDIT
|
|
8939 curwin->w_cursor.coladd = 0;
|
|
8940 #endif
|
|
8941 curwin->w_curswant = 0;
|
|
8942 start_arrow(&tpos);
|
|
8943 }
|
|
8944
|
|
8945 static void
|
|
8946 ins_end(c)
|
|
8947 int c;
|
|
8948 {
|
|
8949 pos_T tpos;
|
|
8950
|
|
8951 #ifdef FEAT_FOLDING
|
|
8952 if ((fdo_flags & FDO_HOR) && KeyTyped)
|
|
8953 foldOpenCursor();
|
|
8954 #endif
|
|
8955 undisplay_dollar();
|
|
8956 tpos = curwin->w_cursor;
|
|
8957 if (c == K_C_END)
|
|
8958 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
|
|
8959 coladvance((colnr_T)MAXCOL);
|
|
8960 curwin->w_curswant = MAXCOL;
|
|
8961
|
|
8962 start_arrow(&tpos);
|
|
8963 }
|
|
8964
|
|
8965 static void
|
|
8966 ins_s_left()
|
|
8967 {
|
|
8968 #ifdef FEAT_FOLDING
|
|
8969 if ((fdo_flags & FDO_HOR) && KeyTyped)
|
|
8970 foldOpenCursor();
|
|
8971 #endif
|
|
8972 undisplay_dollar();
|
|
8973 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
|
|
8974 {
|
|
8975 start_arrow(&curwin->w_cursor);
|
|
8976 (void)bck_word(1L, FALSE, FALSE);
|
|
8977 curwin->w_set_curswant = TRUE;
|
|
8978 }
|
|
8979 else
|
|
8980 vim_beep();
|
|
8981 }
|
|
8982
|
|
8983 static void
|
|
8984 ins_right()
|
|
8985 {
|
|
8986 #ifdef FEAT_FOLDING
|
|
8987 if ((fdo_flags & FDO_HOR) && KeyTyped)
|
|
8988 foldOpenCursor();
|
|
8989 #endif
|
|
8990 undisplay_dollar();
|
|
8991 if (gchar_cursor() != NUL || virtual_active()
|
|
8992 )
|
|
8993 {
|
|
8994 start_arrow(&curwin->w_cursor);
|
|
8995 curwin->w_set_curswant = TRUE;
|
|
8996 #ifdef FEAT_VIRTUALEDIT
|
|
8997 if (virtual_active())
|
|
8998 oneright();
|
|
8999 else
|
|
9000 #endif
|
|
9001 {
|
|
9002 #ifdef FEAT_MBYTE
|
|
9003 if (has_mbyte)
|
474
|
9004 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
|
7
|
9005 else
|
|
9006 #endif
|
|
9007 ++curwin->w_cursor.col;
|
|
9008 }
|
|
9009
|
|
9010 #ifdef FEAT_RIGHTLEFT
|
|
9011 revins_legal++;
|
|
9012 if (revins_chars)
|
|
9013 revins_chars--;
|
|
9014 #endif
|
|
9015 }
|
|
9016 /* if 'whichwrap' set for cursor in insert mode, may move the
|
|
9017 * cursor to the next line */
|
|
9018 else if (vim_strchr(p_ww, ']') != NULL
|
|
9019 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
|
|
9020 {
|
|
9021 start_arrow(&curwin->w_cursor);
|
|
9022 curwin->w_set_curswant = TRUE;
|
|
9023 ++curwin->w_cursor.lnum;
|
|
9024 curwin->w_cursor.col = 0;
|
|
9025 }
|
|
9026 else
|
|
9027 vim_beep();
|
|
9028 }
|
|
9029
|
|
9030 static void
|
|
9031 ins_s_right()
|
|
9032 {
|
|
9033 #ifdef FEAT_FOLDING
|
|
9034 if ((fdo_flags & FDO_HOR) && KeyTyped)
|
|
9035 foldOpenCursor();
|
|
9036 #endif
|
|
9037 undisplay_dollar();
|
|
9038 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
|
|
9039 || gchar_cursor() != NUL)
|
|
9040 {
|
|
9041 start_arrow(&curwin->w_cursor);
|
|
9042 (void)fwd_word(1L, FALSE, 0);
|
|
9043 curwin->w_set_curswant = TRUE;
|
|
9044 }
|
|
9045 else
|
|
9046 vim_beep();
|
|
9047 }
|
|
9048
|
|
9049 static void
|
|
9050 ins_up(startcol)
|
|
9051 int startcol; /* when TRUE move to Insstart.col */
|
|
9052 {
|
|
9053 pos_T tpos;
|
|
9054 linenr_T old_topline = curwin->w_topline;
|
|
9055 #ifdef FEAT_DIFF
|
|
9056 int old_topfill = curwin->w_topfill;
|
|
9057 #endif
|
|
9058
|
|
9059 undisplay_dollar();
|
|
9060 tpos = curwin->w_cursor;
|
|
9061 if (cursor_up(1L, TRUE) == OK)
|
|
9062 {
|
|
9063 if (startcol)
|
|
9064 coladvance(getvcol_nolist(&Insstart));
|
|
9065 if (old_topline != curwin->w_topline
|
|
9066 #ifdef FEAT_DIFF
|
|
9067 || old_topfill != curwin->w_topfill
|
|
9068 #endif
|
|
9069 )
|
|
9070 redraw_later(VALID);
|
|
9071 start_arrow(&tpos);
|
|
9072 #ifdef FEAT_CINDENT
|
|
9073 can_cindent = TRUE;
|
|
9074 #endif
|
|
9075 }
|
|
9076 else
|
|
9077 vim_beep();
|
|
9078 }
|
|
9079
|
|
9080 static void
|
|
9081 ins_pageup()
|
|
9082 {
|
|
9083 pos_T tpos;
|
|
9084
|
|
9085 undisplay_dollar();
|
828
|
9086
|
|
9087 #ifdef FEAT_WINDOWS
|
|
9088 if (mod_mask & MOD_MASK_CTRL)
|
|
9089 {
|
|
9090 /* <C-PageUp>: tab page back */
|
1013
|
9091 if (first_tabpage->tp_next != NULL)
|
|
9092 {
|
|
9093 start_arrow(&curwin->w_cursor);
|
|
9094 goto_tabpage(-1);
|
|
9095 }
|
828
|
9096 return;
|
|
9097 }
|
|
9098 #endif
|
|
9099
|
7
|
9100 tpos = curwin->w_cursor;
|
|
9101 if (onepage(BACKWARD, 1L) == OK)
|
|
9102 {
|
|
9103 start_arrow(&tpos);
|
|
9104 #ifdef FEAT_CINDENT
|
|
9105 can_cindent = TRUE;
|
|
9106 #endif
|
|
9107 }
|
|
9108 else
|
|
9109 vim_beep();
|
|
9110 }
|
|
9111
|
|
9112 static void
|
|
9113 ins_down(startcol)
|
|
9114 int startcol; /* when TRUE move to Insstart.col */
|
|
9115 {
|
|
9116 pos_T tpos;
|
|
9117 linenr_T old_topline = curwin->w_topline;
|
|
9118 #ifdef FEAT_DIFF
|
|
9119 int old_topfill = curwin->w_topfill;
|
|
9120 #endif
|
|
9121
|
|
9122 undisplay_dollar();
|
|
9123 tpos = curwin->w_cursor;
|
|
9124 if (cursor_down(1L, TRUE) == OK)
|
|
9125 {
|
|
9126 if (startcol)
|
|
9127 coladvance(getvcol_nolist(&Insstart));
|
|
9128 if (old_topline != curwin->w_topline
|
|
9129 #ifdef FEAT_DIFF
|
|
9130 || old_topfill != curwin->w_topfill
|
|
9131 #endif
|
|
9132 )
|
|
9133 redraw_later(VALID);
|
|
9134 start_arrow(&tpos);
|
|
9135 #ifdef FEAT_CINDENT
|
|
9136 can_cindent = TRUE;
|
|
9137 #endif
|
|
9138 }
|
|
9139 else
|
|
9140 vim_beep();
|
|
9141 }
|
|
9142
|
|
9143 static void
|
|
9144 ins_pagedown()
|
|
9145 {
|
|
9146 pos_T tpos;
|
|
9147
|
|
9148 undisplay_dollar();
|
828
|
9149
|
|
9150 #ifdef FEAT_WINDOWS
|
|
9151 if (mod_mask & MOD_MASK_CTRL)
|
|
9152 {
|
|
9153 /* <C-PageDown>: tab page forward */
|
1013
|
9154 if (first_tabpage->tp_next != NULL)
|
|
9155 {
|
|
9156 start_arrow(&curwin->w_cursor);
|
|
9157 goto_tabpage(0);
|
|
9158 }
|
828
|
9159 return;
|
|
9160 }
|
|
9161 #endif
|
|
9162
|
7
|
9163 tpos = curwin->w_cursor;
|
|
9164 if (onepage(FORWARD, 1L) == OK)
|
|
9165 {
|
|
9166 start_arrow(&tpos);
|
|
9167 #ifdef FEAT_CINDENT
|
|
9168 can_cindent = TRUE;
|
|
9169 #endif
|
|
9170 }
|
|
9171 else
|
|
9172 vim_beep();
|
|
9173 }
|
|
9174
|
|
9175 #ifdef FEAT_DND
|
|
9176 static void
|
|
9177 ins_drop()
|
|
9178 {
|
|
9179 do_put('~', BACKWARD, 1L, PUT_CURSEND);
|
|
9180 }
|
|
9181 #endif
|
|
9182
|
|
9183 /*
|
|
9184 * Handle TAB in Insert or Replace mode.
|
|
9185 * Return TRUE when the TAB needs to be inserted like a normal character.
|
|
9186 */
|
|
9187 static int
|
|
9188 ins_tab()
|
|
9189 {
|
|
9190 int ind;
|
|
9191 int i;
|
|
9192 int temp;
|
|
9193
|
|
9194 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
|
|
9195 Insstart_blank_vcol = get_nolist_virtcol();
|
|
9196 if (echeck_abbr(TAB + ABBR_OFF))
|
|
9197 return FALSE;
|
|
9198
|
|
9199 ind = inindent(0);
|
|
9200 #ifdef FEAT_CINDENT
|
|
9201 if (ind)
|
|
9202 can_cindent = FALSE;
|
|
9203 #endif
|
|
9204
|
|
9205 /*
|
|
9206 * When nothing special, insert TAB like a normal character
|
|
9207 */
|
|
9208 if (!curbuf->b_p_et
|
|
9209 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
|
|
9210 && curbuf->b_p_sts == 0)
|
|
9211 return TRUE;
|
|
9212
|
|
9213 if (stop_arrow() == FAIL)
|
|
9214 return TRUE;
|
|
9215
|
|
9216 did_ai = FALSE;
|
|
9217 #ifdef FEAT_SMARTINDENT
|
|
9218 did_si = FALSE;
|
|
9219 can_si = FALSE;
|
|
9220 can_si_back = FALSE;
|
|
9221 #endif
|
|
9222 AppendToRedobuff((char_u *)"\t");
|
|
9223
|
|
9224 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
|
|
9225 temp = (int)curbuf->b_p_sw;
|
|
9226 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
|
|
9227 temp = (int)curbuf->b_p_sts;
|
|
9228 else /* otherwise use 'tabstop' */
|
|
9229 temp = (int)curbuf->b_p_ts;
|
|
9230 temp -= get_nolist_virtcol() % temp;
|
|
9231
|
|
9232 /*
|
|
9233 * Insert the first space with ins_char(). It will delete one char in
|
|
9234 * replace mode. Insert the rest with ins_str(); it will not delete any
|
|
9235 * chars. For VREPLACE mode, we use ins_char() for all characters.
|
|
9236 */
|
|
9237 ins_char(' ');
|
|
9238 while (--temp > 0)
|
|
9239 {
|
|
9240 #ifdef FEAT_VREPLACE
|
|
9241 if (State & VREPLACE_FLAG)
|
|
9242 ins_char(' ');
|
|
9243 else
|
|
9244 #endif
|
|
9245 {
|
|
9246 ins_str((char_u *)" ");
|
|
9247 if (State & REPLACE_FLAG) /* no char replaced */
|
|
9248 replace_push(NUL);
|
|
9249 }
|
|
9250 }
|
|
9251
|
|
9252 /*
|
|
9253 * When 'expandtab' not set: Replace spaces by TABs where possible.
|
|
9254 */
|
|
9255 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
|
|
9256 {
|
|
9257 char_u *ptr;
|
|
9258 #ifdef FEAT_VREPLACE
|
|
9259 char_u *saved_line = NULL; /* init for GCC */
|
|
9260 pos_T pos;
|
|
9261 #endif
|
|
9262 pos_T fpos;
|
|
9263 pos_T *cursor;
|
|
9264 colnr_T want_vcol, vcol;
|
|
9265 int change_col = -1;
|
|
9266 int save_list = curwin->w_p_list;
|
|
9267
|
|
9268 /*
|
|
9269 * Get the current line. For VREPLACE mode, don't make real changes
|
|
9270 * yet, just work on a copy of the line.
|
|
9271 */
|
|
9272 #ifdef FEAT_VREPLACE
|
|
9273 if (State & VREPLACE_FLAG)
|
|
9274 {
|
|
9275 pos = curwin->w_cursor;
|
|
9276 cursor = &pos;
|
|
9277 saved_line = vim_strsave(ml_get_curline());
|
|
9278 if (saved_line == NULL)
|
|
9279 return FALSE;
|
|
9280 ptr = saved_line + pos.col;
|
|
9281 }
|
|
9282 else
|
|
9283 #endif
|
|
9284 {
|
|
9285 ptr = ml_get_cursor();
|
|
9286 cursor = &curwin->w_cursor;
|
|
9287 }
|
|
9288
|
|
9289 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
|
|
9290 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
|
|
9291 curwin->w_p_list = FALSE;
|
|
9292
|
|
9293 /* Find first white before the cursor */
|
|
9294 fpos = curwin->w_cursor;
|
|
9295 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
|
|
9296 {
|
|
9297 --fpos.col;
|
|
9298 --ptr;
|
|
9299 }
|
|
9300
|
|
9301 /* In Replace mode, don't change characters before the insert point. */
|
|
9302 if ((State & REPLACE_FLAG)
|
|
9303 && fpos.lnum == Insstart.lnum
|
|
9304 && fpos.col < Insstart.col)
|
|
9305 {
|
|
9306 ptr += Insstart.col - fpos.col;
|
|
9307 fpos.col = Insstart.col;
|
|
9308 }
|
|
9309
|
|
9310 /* compute virtual column numbers of first white and cursor */
|
|
9311 getvcol(curwin, &fpos, &vcol, NULL, NULL);
|
|
9312 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
|
|
9313
|
|
9314 /* Use as many TABs as possible. Beware of 'showbreak' and
|
|
9315 * 'linebreak' adding extra virtual columns. */
|
|
9316 while (vim_iswhite(*ptr))
|
|
9317 {
|
|
9318 i = lbr_chartabsize((char_u *)"\t", vcol);
|
|
9319 if (vcol + i > want_vcol)
|
|
9320 break;
|
|
9321 if (*ptr != TAB)
|
|
9322 {
|
|
9323 *ptr = TAB;
|
|
9324 if (change_col < 0)
|
|
9325 {
|
|
9326 change_col = fpos.col; /* Column of first change */
|
|
9327 /* May have to adjust Insstart */
|
|
9328 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
|
|
9329 Insstart.col = fpos.col;
|
|
9330 }
|
|
9331 }
|
|
9332 ++fpos.col;
|
|
9333 ++ptr;
|
|
9334 vcol += i;
|
|
9335 }
|
|
9336
|
|
9337 if (change_col >= 0)
|
|
9338 {
|
|
9339 int repl_off = 0;
|
|
9340
|
|
9341 /* Skip over the spaces we need. */
|
|
9342 while (vcol < want_vcol && *ptr == ' ')
|
|
9343 {
|
|
9344 vcol += lbr_chartabsize(ptr, vcol);
|
|
9345 ++ptr;
|
|
9346 ++repl_off;
|
|
9347 }
|
|
9348 if (vcol > want_vcol)
|
|
9349 {
|
|
9350 /* Must have a char with 'showbreak' just before it. */
|
|
9351 --ptr;
|
|
9352 --repl_off;
|
|
9353 }
|
|
9354 fpos.col += repl_off;
|
|
9355
|
|
9356 /* Delete following spaces. */
|
|
9357 i = cursor->col - fpos.col;
|
|
9358 if (i > 0)
|
|
9359 {
|
1622
|
9360 STRMOVE(ptr, ptr + i);
|
7
|
9361 /* correct replace stack. */
|
|
9362 if ((State & REPLACE_FLAG)
|
|
9363 #ifdef FEAT_VREPLACE
|
|
9364 && !(State & VREPLACE_FLAG)
|
|
9365 #endif
|
|
9366 )
|
|
9367 for (temp = i; --temp >= 0; )
|
|
9368 replace_join(repl_off);
|
|
9369 }
|
33
|
9370 #ifdef FEAT_NETBEANS_INTG
|
|
9371 if (usingNetbeans)
|
|
9372 {
|
|
9373 netbeans_removed(curbuf, fpos.lnum, cursor->col,
|
|
9374 (long)(i + 1));
|
|
9375 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
|
|
9376 (char_u *)"\t", 1);
|
|
9377 }
|
|
9378 #endif
|
7
|
9379 cursor->col -= i;
|
|
9380
|
|
9381 #ifdef FEAT_VREPLACE
|
|
9382 /*
|
|
9383 * In VREPLACE mode, we haven't changed anything yet. Do it now by
|
|
9384 * backspacing over the changed spacing and then inserting the new
|
|
9385 * spacing.
|
|
9386 */
|
|
9387 if (State & VREPLACE_FLAG)
|
|
9388 {
|
|
9389 /* Backspace from real cursor to change_col */
|
|
9390 backspace_until_column(change_col);
|
|
9391
|
|
9392 /* Insert each char in saved_line from changed_col to
|
|
9393 * ptr-cursor */
|
|
9394 ins_bytes_len(saved_line + change_col,
|
|
9395 cursor->col - change_col);
|
|
9396 }
|
|
9397 #endif
|
|
9398 }
|
|
9399
|
|
9400 #ifdef FEAT_VREPLACE
|
|
9401 if (State & VREPLACE_FLAG)
|
|
9402 vim_free(saved_line);
|
|
9403 #endif
|
|
9404 curwin->w_p_list = save_list;
|
|
9405 }
|
|
9406
|
|
9407 return FALSE;
|
|
9408 }
|
|
9409
|
|
9410 /*
|
|
9411 * Handle CR or NL in insert mode.
|
|
9412 * Return TRUE when out of memory or can't undo.
|
|
9413 */
|
|
9414 static int
|
|
9415 ins_eol(c)
|
|
9416 int c;
|
|
9417 {
|
|
9418 int i;
|
|
9419
|
|
9420 if (echeck_abbr(c + ABBR_OFF))
|
|
9421 return FALSE;
|
|
9422 if (stop_arrow() == FAIL)
|
|
9423 return TRUE;
|
|
9424 undisplay_dollar();
|
|
9425
|
|
9426 /*
|
|
9427 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
|
|
9428 * character under the cursor. Only push a NUL on the replace stack,
|
|
9429 * nothing to put back when the NL is deleted.
|
|
9430 */
|
|
9431 if ((State & REPLACE_FLAG)
|
|
9432 #ifdef FEAT_VREPLACE
|
|
9433 && !(State & VREPLACE_FLAG)
|
|
9434 #endif
|
|
9435 )
|
|
9436 replace_push(NUL);
|
|
9437
|
|
9438 /*
|
|
9439 * In VREPLACE mode, a NL replaces the rest of the line, and starts
|
|
9440 * replacing the next line, so we push all of the characters left on the
|
|
9441 * line onto the replace stack. This is not done here though, it is done
|
|
9442 * in open_line().
|
|
9443 */
|
|
9444
|
844
|
9445 #ifdef FEAT_VIRTUALEDIT
|
|
9446 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
|
|
9447 * CTRL-O). */
|
|
9448 if (virtual_active() && curwin->w_cursor.coladd > 0)
|
|
9449 coladvance(getviscol());
|
|
9450 #endif
|
|
9451
|
7
|
9452 #ifdef FEAT_RIGHTLEFT
|
|
9453 # ifdef FEAT_FKMAP
|
|
9454 if (p_altkeymap && p_fkmap)
|
|
9455 fkmap(NL);
|
|
9456 # endif
|
|
9457 /* NL in reverse insert will always start in the end of
|
|
9458 * current line. */
|
|
9459 if (revins_on)
|
|
9460 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
|
|
9461 #endif
|
|
9462
|
|
9463 AppendToRedobuff(NL_STR);
|
|
9464 i = open_line(FORWARD,
|
|
9465 #ifdef FEAT_COMMENTS
|
|
9466 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
|
|
9467 #endif
|
|
9468 0, old_indent);
|
|
9469 old_indent = 0;
|
|
9470 #ifdef FEAT_CINDENT
|
|
9471 can_cindent = TRUE;
|
|
9472 #endif
|
1032
|
9473 #ifdef FEAT_FOLDING
|
|
9474 /* When inserting a line the cursor line must never be in a closed fold. */
|
|
9475 foldOpenCursor();
|
|
9476 #endif
|
7
|
9477
|
|
9478 return (!i);
|
|
9479 }
|
|
9480
|
|
9481 #ifdef FEAT_DIGRAPHS
|
|
9482 /*
|
|
9483 * Handle digraph in insert mode.
|
|
9484 * Returns character still to be inserted, or NUL when nothing remaining to be
|
|
9485 * done.
|
|
9486 */
|
|
9487 static int
|
|
9488 ins_digraph()
|
|
9489 {
|
|
9490 int c;
|
|
9491 int cc;
|
|
9492
|
|
9493 pc_status = PC_STATUS_UNSET;
|
|
9494 if (redrawing() && !char_avail())
|
|
9495 {
|
|
9496 /* may need to redraw when no more chars available now */
|
661
|
9497 ins_redraw(FALSE);
|
7
|
9498
|
|
9499 edit_putchar('?', TRUE);
|
|
9500 #ifdef FEAT_CMDL_INFO
|
|
9501 add_to_showcmd_c(Ctrl_K);
|
|
9502 #endif
|
|
9503 }
|
|
9504
|
|
9505 #ifdef USE_ON_FLY_SCROLL
|
|
9506 dont_scroll = TRUE; /* disallow scrolling here */
|
|
9507 #endif
|
|
9508
|
|
9509 /* don't map the digraph chars. This also prevents the
|
|
9510 * mode message to be deleted when ESC is hit */
|
|
9511 ++no_mapping;
|
|
9512 ++allow_keys;
|
1389
|
9513 c = plain_vgetc();
|
7
|
9514 --no_mapping;
|
|
9515 --allow_keys;
|
|
9516 if (IS_SPECIAL(c) || mod_mask) /* special key */
|
|
9517 {
|
|
9518 #ifdef FEAT_CMDL_INFO
|
|
9519 clear_showcmd();
|
|
9520 #endif
|
|
9521 insert_special(c, TRUE, FALSE);
|
|
9522 return NUL;
|
|
9523 }
|
|
9524 if (c != ESC)
|
|
9525 {
|
|
9526 if (redrawing() && !char_avail())
|
|
9527 {
|
|
9528 /* may need to redraw when no more chars available now */
|
661
|
9529 ins_redraw(FALSE);
|
7
|
9530
|
|
9531 if (char2cells(c) == 1)
|
|
9532 {
|
|
9533 /* first remove the '?', otherwise it's restored when typing
|
|
9534 * an ESC next */
|
|
9535 edit_unputchar();
|
661
|
9536 ins_redraw(FALSE);
|
7
|
9537 edit_putchar(c, TRUE);
|
|
9538 }
|
|
9539 #ifdef FEAT_CMDL_INFO
|
|
9540 add_to_showcmd_c(c);
|
|
9541 #endif
|
|
9542 }
|
|
9543 ++no_mapping;
|
|
9544 ++allow_keys;
|
1389
|
9545 cc = plain_vgetc();
|
7
|
9546 --no_mapping;
|
|
9547 --allow_keys;
|
|
9548 if (cc != ESC)
|
|
9549 {
|
|
9550 AppendToRedobuff((char_u *)CTRL_V_STR);
|
|
9551 c = getdigraph(c, cc, TRUE);
|
|
9552 #ifdef FEAT_CMDL_INFO
|
|
9553 clear_showcmd();
|
|
9554 #endif
|
|
9555 return c;
|
|
9556 }
|
|
9557 }
|
|
9558 edit_unputchar();
|
|
9559 #ifdef FEAT_CMDL_INFO
|
|
9560 clear_showcmd();
|
|
9561 #endif
|
|
9562 return NUL;
|
|
9563 }
|
|
9564 #endif
|
|
9565
|
|
9566 /*
|
|
9567 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
|
|
9568 * Returns the char to be inserted, or NUL if none found.
|
|
9569 */
|
|
9570 static int
|
|
9571 ins_copychar(lnum)
|
|
9572 linenr_T lnum;
|
|
9573 {
|
|
9574 int c;
|
|
9575 int temp;
|
|
9576 char_u *ptr, *prev_ptr;
|
|
9577
|
|
9578 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
|
|
9579 {
|
|
9580 vim_beep();
|
|
9581 return NUL;
|
|
9582 }
|
|
9583
|
|
9584 /* try to advance to the cursor column */
|
|
9585 temp = 0;
|
|
9586 ptr = ml_get(lnum);
|
|
9587 prev_ptr = ptr;
|
|
9588 validate_virtcol();
|
|
9589 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
|
|
9590 {
|
|
9591 prev_ptr = ptr;
|
|
9592 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
|
|
9593 }
|
|
9594 if ((colnr_T)temp > curwin->w_virtcol)
|
|
9595 ptr = prev_ptr;
|
|
9596
|
|
9597 #ifdef FEAT_MBYTE
|
|
9598 c = (*mb_ptr2char)(ptr);
|
|
9599 #else
|
|
9600 c = *ptr;
|
|
9601 #endif
|
|
9602 if (c == NUL)
|
|
9603 vim_beep();
|
|
9604 return c;
|
|
9605 }
|
|
9606
|
449
|
9607 /*
|
|
9608 * CTRL-Y or CTRL-E typed in Insert mode.
|
|
9609 */
|
|
9610 static int
|
|
9611 ins_ctrl_ey(tc)
|
|
9612 int tc;
|
|
9613 {
|
|
9614 int c = tc;
|
|
9615
|
|
9616 #ifdef FEAT_INS_EXPAND
|
|
9617 if (ctrl_x_mode == CTRL_X_SCROLL)
|
|
9618 {
|
|
9619 if (c == Ctrl_Y)
|
|
9620 scrolldown_clamp();
|
|
9621 else
|
|
9622 scrollup_clamp();
|
|
9623 redraw_later(VALID);
|
|
9624 }
|
|
9625 else
|
|
9626 #endif
|
|
9627 {
|
|
9628 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
|
|
9629 if (c != NUL)
|
|
9630 {
|
|
9631 long tw_save;
|
|
9632
|
|
9633 /* The character must be taken literally, insert like it
|
|
9634 * was typed after a CTRL-V, and pretend 'textwidth'
|
|
9635 * wasn't set. Digits, 'o' and 'x' are special after a
|
|
9636 * CTRL-V, don't use it for these. */
|
|
9637 if (c < 256 && !isalnum(c))
|
|
9638 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
|
|
9639 tw_save = curbuf->b_p_tw;
|
|
9640 curbuf->b_p_tw = -1;
|
|
9641 insert_special(c, TRUE, FALSE);
|
|
9642 curbuf->b_p_tw = tw_save;
|
|
9643 #ifdef FEAT_RIGHTLEFT
|
|
9644 revins_chars++;
|
|
9645 revins_legal++;
|
|
9646 #endif
|
|
9647 c = Ctrl_V; /* pretend CTRL-V is last character */
|
|
9648 auto_format(FALSE, TRUE);
|
|
9649 }
|
|
9650 }
|
|
9651 return c;
|
|
9652 }
|
|
9653
|
7
|
9654 #ifdef FEAT_SMARTINDENT
|
|
9655 /*
|
|
9656 * Try to do some very smart auto-indenting.
|
|
9657 * Used when inserting a "normal" character.
|
|
9658 */
|
|
9659 static void
|
|
9660 ins_try_si(c)
|
|
9661 int c;
|
|
9662 {
|
|
9663 pos_T *pos, old_pos;
|
|
9664 char_u *ptr;
|
|
9665 int i;
|
|
9666 int temp;
|
|
9667
|
|
9668 /*
|
|
9669 * do some very smart indenting when entering '{' or '}'
|
|
9670 */
|
|
9671 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
|
|
9672 {
|
|
9673 /*
|
|
9674 * for '}' set indent equal to indent of line containing matching '{'
|
|
9675 */
|
|
9676 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
|
|
9677 {
|
|
9678 old_pos = curwin->w_cursor;
|
|
9679 /*
|
|
9680 * If the matching '{' has a ')' immediately before it (ignoring
|
|
9681 * white-space), then line up with the start of the line
|
|
9682 * containing the matching '(' if there is one. This handles the
|
|
9683 * case where an "if (..\n..) {" statement continues over multiple
|
|
9684 * lines -- webb
|
|
9685 */
|
|
9686 ptr = ml_get(pos->lnum);
|
|
9687 i = pos->col;
|
|
9688 if (i > 0) /* skip blanks before '{' */
|
|
9689 while (--i > 0 && vim_iswhite(ptr[i]))
|
|
9690 ;
|
|
9691 curwin->w_cursor.lnum = pos->lnum;
|
|
9692 curwin->w_cursor.col = i;
|
|
9693 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
|
|
9694 curwin->w_cursor = *pos;
|
|
9695 i = get_indent();
|
|
9696 curwin->w_cursor = old_pos;
|
|
9697 #ifdef FEAT_VREPLACE
|
|
9698 if (State & VREPLACE_FLAG)
|
1516
|
9699 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
|
7
|
9700 else
|
|
9701 #endif
|
|
9702 (void)set_indent(i, SIN_CHANGED);
|
|
9703 }
|
|
9704 else if (curwin->w_cursor.col > 0)
|
|
9705 {
|
|
9706 /*
|
|
9707 * when inserting '{' after "O" reduce indent, but not
|
|
9708 * more than indent of previous line
|
|
9709 */
|
|
9710 temp = TRUE;
|
|
9711 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
|
|
9712 {
|
|
9713 old_pos = curwin->w_cursor;
|
|
9714 i = get_indent();
|
|
9715 while (curwin->w_cursor.lnum > 1)
|
|
9716 {
|
|
9717 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
|
|
9718
|
|
9719 /* ignore empty lines and lines starting with '#'. */
|
|
9720 if (*ptr != '#' && *ptr != NUL)
|
|
9721 break;
|
|
9722 }
|
|
9723 if (get_indent() >= i)
|
|
9724 temp = FALSE;
|
|
9725 curwin->w_cursor = old_pos;
|
|
9726 }
|
|
9727 if (temp)
|
1516
|
9728 shift_line(TRUE, FALSE, 1, TRUE);
|
7
|
9729 }
|
|
9730 }
|
|
9731
|
|
9732 /*
|
|
9733 * set indent of '#' always to 0
|
|
9734 */
|
|
9735 if (curwin->w_cursor.col > 0 && can_si && c == '#')
|
|
9736 {
|
|
9737 /* remember current indent for next line */
|
|
9738 old_indent = get_indent();
|
|
9739 (void)set_indent(0, SIN_CHANGED);
|
|
9740 }
|
|
9741
|
|
9742 /* Adjust ai_col, the char at this position can be deleted. */
|
|
9743 if (ai_col > curwin->w_cursor.col)
|
|
9744 ai_col = curwin->w_cursor.col;
|
|
9745 }
|
|
9746 #endif
|
|
9747
|
|
9748 /*
|
|
9749 * Get the value that w_virtcol would have when 'list' is off.
|
|
9750 * Unless 'cpo' contains the 'L' flag.
|
|
9751 */
|
|
9752 static colnr_T
|
|
9753 get_nolist_virtcol()
|
|
9754 {
|
|
9755 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
|
|
9756 return getvcol_nolist(&curwin->w_cursor);
|
|
9757 validate_virtcol();
|
|
9758 return curwin->w_virtcol;
|
|
9759 }
|