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