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