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