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