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