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