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