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