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