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