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