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