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