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