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