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