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