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