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