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