1 /* vi:set ts=8 sts=4 sw=4 noet: 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 * getchar.c 12 * 13 * functions related with getting a character from the user/mapping/redo/... 14 * 15 * manipulations with redo buffer and stuff buffer 16 * mappings and abbreviations 17 */ 18 19 #include "vim.h" 20 21 /* 22 * These buffers are used for storing: 23 * - stuffed characters: A command that is translated into another command. 24 * - redo characters: will redo the last change. 25 * - recorded characters: for the "q" command. 26 * 27 * The bytes are stored like in the typeahead buffer: 28 * - K_SPECIAL introduces a special key (two more bytes follow). A literal 29 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER. 30 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE, 31 * otherwise switching the GUI on would make mappings invalid). 32 * A literal CSI is stored as CSI KS_EXTRA KE_CSI. 33 * These translations are also done on multi-byte characters! 34 * 35 * Escaping CSI bytes is done by the system-specific input functions, called 36 * by ui_inchar(). 37 * Escaping K_SPECIAL is done by inchar(). 38 * Un-escaping is done by vgetc(). 39 */ 40 41 #define MINIMAL_SIZE 20 // minimal size for b_str 42 43 static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0}; 44 static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; 45 static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0}; 46 47 static int typeahead_char = 0; // typeahead char that's not flushed 48 49 /* 50 * when block_redo is TRUE redo buffer will not be changed 51 * used by edit() to repeat insertions and 'V' command for redoing 52 */ 53 static int block_redo = FALSE; 54 55 static int KeyNoremap = 0; // remapping flags 56 57 /* 58 * Variables used by vgetorpeek() and flush_buffers(). 59 * 60 * typebuf.tb_buf[] contains all characters that are not consumed yet. 61 * typebuf.tb_buf[typebuf.tb_off] is the first valid character. 62 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char. 63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL. 64 * The head of the buffer may contain the result of mappings, abbreviations 65 * and @a commands. The length of this part is typebuf.tb_maplen. 66 * typebuf.tb_silent is the part where <silent> applies. 67 * After the head are characters that come from the terminal. 68 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that 69 * should not be considered for abbreviations. 70 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered 71 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and 72 * contains RM_NONE for the characters that are not to be remapped. 73 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag. 74 * (typebuf has been put in globals.h, because check_termcode() needs it). 75 */ 76 #define RM_YES 0 // tb_noremap: remap 77 #define RM_NONE 1 // tb_noremap: don't remap 78 #define RM_SCRIPT 2 // tb_noremap: remap local script mappings 79 #define RM_ABBR 4 // tb_noremap: don't remap, do abbrev. 80 81 // typebuf.tb_buf has three parts: room in front (for result of mappings), the 82 // middle for typeahead and room for new characters (which needs to be 3 * 83 // MAXMAPLEN) for the Amiga). 84 #define TYPELEN_INIT (5 * (MAXMAPLEN + 3)) 85 static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf 86 static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap 87 88 static int last_recorded_len = 0; // number of last recorded chars 89 90 static int read_readbuf(buffheader_T *buf, int advance); 91 static void init_typebuf(void); 92 static void may_sync_undo(void); 93 static void free_typebuf(void); 94 static void closescript(void); 95 static void updatescript(int c); 96 static int vgetorpeek(int); 97 static int inchar(char_u *buf, int maxlen, long wait_time); 98 99 /* 100 * Free and clear a buffer. 101 */ 102 void 103 free_buff(buffheader_T *buf) 104 { 105 buffblock_T *p, *np; 106 107 for (p = buf->bh_first.b_next; p != NULL; p = np) 108 { 109 np = p->b_next; 110 vim_free(p); 111 } 112 buf->bh_first.b_next = NULL; 113 } 114 115 /* 116 * Return the contents of a buffer as a single string. 117 * K_SPECIAL and CSI in the returned string are escaped. 118 */ 119 static char_u * 120 get_buffcont( 121 buffheader_T *buffer, 122 int dozero) // count == zero is not an error 123 { 124 long_u count = 0; 125 char_u *p = NULL; 126 char_u *p2; 127 char_u *str; 128 buffblock_T *bp; 129 130 // compute the total length of the string 131 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) 132 count += (long_u)STRLEN(bp->b_str); 133 134 if ((count || dozero) && (p = alloc(count + 1)) != NULL) 135 { 136 p2 = p; 137 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) 138 for (str = bp->b_str; *str; ) 139 *p2++ = *str++; 140 *p2 = NUL; 141 } 142 return (p); 143 } 144 145 /* 146 * Return the contents of the record buffer as a single string 147 * and clear the record buffer. 148 * K_SPECIAL and CSI in the returned string are escaped. 149 */ 150 char_u * 151 get_recorded(void) 152 { 153 char_u *p; 154 size_t len; 155 156 p = get_buffcont(&recordbuff, TRUE); 157 free_buff(&recordbuff); 158 159 /* 160 * Remove the characters that were added the last time, these must be the 161 * (possibly mapped) characters that stopped the recording. 162 */ 163 len = STRLEN(p); 164 if ((int)len >= last_recorded_len) 165 { 166 len -= last_recorded_len; 167 p[len] = NUL; 168 } 169 170 /* 171 * When stopping recording from Insert mode with CTRL-O q, also remove the 172 * CTRL-O. 173 */ 174 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O) 175 p[len - 1] = NUL; 176 177 return (p); 178 } 179 180 /* 181 * Return the contents of the redo buffer as a single string. 182 * K_SPECIAL and CSI in the returned string are escaped. 183 */ 184 char_u * 185 get_inserted(void) 186 { 187 return get_buffcont(&redobuff, FALSE); 188 } 189 190 /* 191 * Add string "s" after the current block of buffer "buf". 192 * K_SPECIAL and CSI should have been escaped already. 193 */ 194 static void 195 add_buff( 196 buffheader_T *buf, 197 char_u *s, 198 long slen) // length of "s" or -1 199 { 200 buffblock_T *p; 201 long_u len; 202 203 if (slen < 0) 204 slen = (long)STRLEN(s); 205 if (slen == 0) // don't add empty strings 206 return; 207 208 if (buf->bh_first.b_next == NULL) // first add to list 209 { 210 buf->bh_space = 0; 211 buf->bh_curr = &(buf->bh_first); 212 } 213 else if (buf->bh_curr == NULL) // buffer has already been read 214 { 215 iemsg(_("E222: Add to read buffer")); 216 return; 217 } 218 else if (buf->bh_index != 0) 219 mch_memmove(buf->bh_first.b_next->b_str, 220 buf->bh_first.b_next->b_str + buf->bh_index, 221 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1); 222 buf->bh_index = 0; 223 224 if (buf->bh_space >= (int)slen) 225 { 226 len = (long_u)STRLEN(buf->bh_curr->b_str); 227 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen); 228 buf->bh_space -= slen; 229 } 230 else 231 { 232 if (slen < MINIMAL_SIZE) 233 len = MINIMAL_SIZE; 234 else 235 len = slen; 236 p = alloc(offsetof(buffblock_T, b_str) + len + 1); 237 if (p == NULL) 238 return; // no space, just forget it 239 buf->bh_space = (int)(len - slen); 240 vim_strncpy(p->b_str, s, (size_t)slen); 241 242 p->b_next = buf->bh_curr->b_next; 243 buf->bh_curr->b_next = p; 244 buf->bh_curr = p; 245 } 246 return; 247 } 248 249 /* 250 * Add number "n" to buffer "buf". 251 */ 252 static void 253 add_num_buff(buffheader_T *buf, long n) 254 { 255 char_u number[32]; 256 257 sprintf((char *)number, "%ld", n); 258 add_buff(buf, number, -1L); 259 } 260 261 /* 262 * Add character 'c' to buffer "buf". 263 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. 264 */ 265 static void 266 add_char_buff(buffheader_T *buf, int c) 267 { 268 char_u bytes[MB_MAXBYTES + 1]; 269 int len; 270 int i; 271 char_u temp[4]; 272 273 if (IS_SPECIAL(c)) 274 len = 1; 275 else 276 len = (*mb_char2bytes)(c, bytes); 277 for (i = 0; i < len; ++i) 278 { 279 if (!IS_SPECIAL(c)) 280 c = bytes[i]; 281 282 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL) 283 { 284 // translate special key code into three byte sequence 285 temp[0] = K_SPECIAL; 286 temp[1] = K_SECOND(c); 287 temp[2] = K_THIRD(c); 288 temp[3] = NUL; 289 } 290 #ifdef FEAT_GUI 291 else if (c == CSI) 292 { 293 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence 294 temp[0] = CSI; 295 temp[1] = KS_EXTRA; 296 temp[2] = (int)KE_CSI; 297 temp[3] = NUL; 298 } 299 #endif 300 else 301 { 302 temp[0] = c; 303 temp[1] = NUL; 304 } 305 add_buff(buf, temp, -1L); 306 } 307 } 308 309 // First read ahead buffer. Used for translated commands. 310 static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0}; 311 312 // Second read ahead buffer. Used for redo. 313 static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0}; 314 315 /* 316 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 317 * if that one is empty. 318 * If advance == TRUE go to the next char. 319 * No translation is done K_SPECIAL and CSI are escaped. 320 */ 321 static int 322 read_readbuffers(int advance) 323 { 324 int c; 325 326 c = read_readbuf(&readbuf1, advance); 327 if (c == NUL) 328 c = read_readbuf(&readbuf2, advance); 329 return c; 330 } 331 332 static int 333 read_readbuf(buffheader_T *buf, int advance) 334 { 335 char_u c; 336 buffblock_T *curr; 337 338 if (buf->bh_first.b_next == NULL) // buffer is empty 339 return NUL; 340 341 curr = buf->bh_first.b_next; 342 c = curr->b_str[buf->bh_index]; 343 344 if (advance) 345 { 346 if (curr->b_str[++buf->bh_index] == NUL) 347 { 348 buf->bh_first.b_next = curr->b_next; 349 vim_free(curr); 350 buf->bh_index = 0; 351 } 352 } 353 return c; 354 } 355 356 /* 357 * Prepare the read buffers for reading (if they contain something). 358 */ 359 static void 360 start_stuff(void) 361 { 362 if (readbuf1.bh_first.b_next != NULL) 363 { 364 readbuf1.bh_curr = &(readbuf1.bh_first); 365 readbuf1.bh_space = 0; 366 } 367 if (readbuf2.bh_first.b_next != NULL) 368 { 369 readbuf2.bh_curr = &(readbuf2.bh_first); 370 readbuf2.bh_space = 0; 371 } 372 } 373 374 /* 375 * Return TRUE if the stuff buffer is empty. 376 */ 377 int 378 stuff_empty(void) 379 { 380 return (readbuf1.bh_first.b_next == NULL 381 && readbuf2.bh_first.b_next == NULL); 382 } 383 384 #if defined(FEAT_EVAL) || defined(PROTO) 385 /* 386 * Return TRUE if readbuf1 is empty. There may still be redo characters in 387 * redbuf2. 388 */ 389 int 390 readbuf1_empty(void) 391 { 392 return (readbuf1.bh_first.b_next == NULL); 393 } 394 #endif 395 396 /* 397 * Set a typeahead character that won't be flushed. 398 */ 399 void 400 typeahead_noflush(int c) 401 { 402 typeahead_char = c; 403 } 404 405 /* 406 * Remove the contents of the stuff buffer and the mapped characters in the 407 * typeahead buffer (used in case of an error). If "flush_typeahead" is true, 408 * flush all typeahead characters (used when interrupted by a CTRL-C). 409 */ 410 void 411 flush_buffers(flush_buffers_T flush_typeahead) 412 { 413 init_typebuf(); 414 415 start_stuff(); 416 while (read_readbuffers(TRUE) != NUL) 417 ; 418 419 if (flush_typeahead == FLUSH_MINIMAL) 420 { 421 // remove mapped characters at the start only 422 typebuf.tb_off += typebuf.tb_maplen; 423 typebuf.tb_len -= typebuf.tb_maplen; 424 } 425 else 426 { 427 // remove typeahead 428 if (flush_typeahead == FLUSH_INPUT) 429 // We have to get all characters, because we may delete the first 430 // part of an escape sequence. In an xterm we get one char at a 431 // time and we have to get them all. 432 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0) 433 ; 434 typebuf.tb_off = MAXMAPLEN; 435 typebuf.tb_len = 0; 436 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) 437 // Reset the flag that text received from a client or from feedkeys() 438 // was inserted in the typeahead buffer. 439 typebuf_was_filled = FALSE; 440 #endif 441 } 442 typebuf.tb_maplen = 0; 443 typebuf.tb_silent = 0; 444 cmd_silent = FALSE; 445 typebuf.tb_no_abbr_cnt = 0; 446 } 447 448 /* 449 * The previous contents of the redo buffer is kept in old_redobuffer. 450 * This is used for the CTRL-O <.> command in insert mode. 451 */ 452 void 453 ResetRedobuff(void) 454 { 455 if (!block_redo) 456 { 457 free_buff(&old_redobuff); 458 old_redobuff = redobuff; 459 redobuff.bh_first.b_next = NULL; 460 } 461 } 462 463 /* 464 * Discard the contents of the redo buffer and restore the previous redo 465 * buffer. 466 */ 467 void 468 CancelRedo(void) 469 { 470 if (!block_redo) 471 { 472 free_buff(&redobuff); 473 redobuff = old_redobuff; 474 old_redobuff.bh_first.b_next = NULL; 475 start_stuff(); 476 while (read_readbuffers(TRUE) != NUL) 477 ; 478 } 479 } 480 481 /* 482 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff. 483 * Used before executing autocommands and user functions. 484 */ 485 void 486 saveRedobuff(save_redo_T *save_redo) 487 { 488 char_u *s; 489 490 save_redo->sr_redobuff = redobuff; 491 redobuff.bh_first.b_next = NULL; 492 save_redo->sr_old_redobuff = old_redobuff; 493 old_redobuff.bh_first.b_next = NULL; 494 495 // Make a copy, so that ":normal ." in a function works. 496 s = get_buffcont(&save_redo->sr_redobuff, FALSE); 497 if (s != NULL) 498 { 499 add_buff(&redobuff, s, -1L); 500 vim_free(s); 501 } 502 } 503 504 /* 505 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff. 506 * Used after executing autocommands and user functions. 507 */ 508 void 509 restoreRedobuff(save_redo_T *save_redo) 510 { 511 free_buff(&redobuff); 512 redobuff = save_redo->sr_redobuff; 513 free_buff(&old_redobuff); 514 old_redobuff = save_redo->sr_old_redobuff; 515 } 516 517 /* 518 * Append "s" to the redo buffer. 519 * K_SPECIAL and CSI should already have been escaped. 520 */ 521 void 522 AppendToRedobuff(char_u *s) 523 { 524 if (!block_redo) 525 add_buff(&redobuff, s, -1L); 526 } 527 528 /* 529 * Append to Redo buffer literally, escaping special characters with CTRL-V. 530 * K_SPECIAL and CSI are escaped as well. 531 */ 532 void 533 AppendToRedobuffLit( 534 char_u *str, 535 int len) // length of "str" or -1 for up to the NUL 536 { 537 char_u *s = str; 538 int c; 539 char_u *start; 540 541 if (block_redo) 542 return; 543 544 while (len < 0 ? *s != NUL : s - str < len) 545 { 546 // Put a string of normal characters in the redo buffer (that's 547 // faster). 548 start = s; 549 while (*s >= ' ' 550 #ifndef EBCDIC 551 && *s < DEL // EBCDIC: all chars above space are normal 552 #endif 553 && (len < 0 || s - str < len)) 554 ++s; 555 556 // Don't put '0' or '^' as last character, just in case a CTRL-D is 557 // typed next. 558 if (*s == NUL && (s[-1] == '0' || s[-1] == '^')) 559 --s; 560 if (s > start) 561 add_buff(&redobuff, start, (long)(s - start)); 562 563 if (*s == NUL || (len >= 0 && s - str >= len)) 564 break; 565 566 // Handle a special or multibyte character. 567 if (has_mbyte) 568 // Handle composing chars separately. 569 c = mb_cptr2char_adv(&s); 570 else 571 c = *s++; 572 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^'))) 573 add_char_buff(&redobuff, Ctrl_V); 574 575 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) 576 if (*s == NUL && c == '0') 577 #ifdef EBCDIC 578 add_buff(&redobuff, (char_u *)"xf0", 3L); 579 #else 580 add_buff(&redobuff, (char_u *)"048", 3L); 581 #endif 582 else 583 add_char_buff(&redobuff, c); 584 } 585 } 586 587 /* 588 * Append a character to the redo buffer. 589 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. 590 */ 591 void 592 AppendCharToRedobuff(int c) 593 { 594 if (!block_redo) 595 add_char_buff(&redobuff, c); 596 } 597 598 /* 599 * Append a number to the redo buffer. 600 */ 601 void 602 AppendNumberToRedobuff(long n) 603 { 604 if (!block_redo) 605 add_num_buff(&redobuff, n); 606 } 607 608 /* 609 * Append string "s" to the stuff buffer. 610 * CSI and K_SPECIAL must already have been escaped. 611 */ 612 void 613 stuffReadbuff(char_u *s) 614 { 615 add_buff(&readbuf1, s, -1L); 616 } 617 618 /* 619 * Append string "s" to the redo stuff buffer. 620 * CSI and K_SPECIAL must already have been escaped. 621 */ 622 void 623 stuffRedoReadbuff(char_u *s) 624 { 625 add_buff(&readbuf2, s, -1L); 626 } 627 628 void 629 stuffReadbuffLen(char_u *s, long len) 630 { 631 add_buff(&readbuf1, s, len); 632 } 633 634 #if defined(FEAT_EVAL) || defined(PROTO) 635 /* 636 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and 637 * escaping other K_SPECIAL and CSI bytes. 638 * Change CR, LF and ESC into a space. 639 */ 640 void 641 stuffReadbuffSpec(char_u *s) 642 { 643 int c; 644 645 while (*s != NUL) 646 { 647 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL) 648 { 649 // Insert special key literally. 650 stuffReadbuffLen(s, 3L); 651 s += 3; 652 } 653 else 654 { 655 c = mb_ptr2char_adv(&s); 656 if (c == CAR || c == NL || c == ESC) 657 c = ' '; 658 stuffcharReadbuff(c); 659 } 660 } 661 } 662 #endif 663 664 /* 665 * Append a character to the stuff buffer. 666 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. 667 */ 668 void 669 stuffcharReadbuff(int c) 670 { 671 add_char_buff(&readbuf1, c); 672 } 673 674 /* 675 * Append a number to the stuff buffer. 676 */ 677 void 678 stuffnumReadbuff(long n) 679 { 680 add_num_buff(&readbuf1, n); 681 } 682 683 /* 684 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and 685 * multibyte characters. 686 * The redo buffer is left as it is. 687 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK 688 * otherwise. 689 * If old is TRUE, use old_redobuff instead of redobuff. 690 */ 691 static int 692 read_redo(int init, int old_redo) 693 { 694 static buffblock_T *bp; 695 static char_u *p; 696 int c; 697 int n; 698 char_u buf[MB_MAXBYTES + 1]; 699 int i; 700 701 if (init) 702 { 703 if (old_redo) 704 bp = old_redobuff.bh_first.b_next; 705 else 706 bp = redobuff.bh_first.b_next; 707 if (bp == NULL) 708 return FAIL; 709 p = bp->b_str; 710 return OK; 711 } 712 if ((c = *p) != NUL) 713 { 714 // Reverse the conversion done by add_char_buff() 715 // For a multi-byte character get all the bytes and return the 716 // converted character. 717 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL)) 718 n = MB_BYTE2LEN_CHECK(c); 719 else 720 n = 1; 721 for (i = 0; ; ++i) 722 { 723 if (c == K_SPECIAL) // special key or escaped K_SPECIAL 724 { 725 c = TO_SPECIAL(p[1], p[2]); 726 p += 2; 727 } 728 #ifdef FEAT_GUI 729 if (c == CSI) // escaped CSI 730 p += 2; 731 #endif 732 if (*++p == NUL && bp->b_next != NULL) 733 { 734 bp = bp->b_next; 735 p = bp->b_str; 736 } 737 buf[i] = c; 738 if (i == n - 1) // last byte of a character 739 { 740 if (n != 1) 741 c = (*mb_ptr2char)(buf); 742 break; 743 } 744 c = *p; 745 if (c == NUL) // cannot happen? 746 break; 747 } 748 } 749 750 return c; 751 } 752 753 /* 754 * Copy the rest of the redo buffer into the stuff buffer (in a slow way). 755 * If old_redo is TRUE, use old_redobuff instead of redobuff. 756 * The escaped K_SPECIAL and CSI are copied without translation. 757 */ 758 static void 759 copy_redo(int old_redo) 760 { 761 int c; 762 763 while ((c = read_redo(FALSE, old_redo)) != NUL) 764 add_char_buff(&readbuf2, c); 765 } 766 767 /* 768 * Stuff the redo buffer into readbuf2. 769 * Insert the redo count into the command. 770 * If "old_redo" is TRUE, the last but one command is repeated 771 * instead of the last command (inserting text). This is used for 772 * CTRL-O <.> in insert mode 773 * 774 * return FAIL for failure, OK otherwise 775 */ 776 int 777 start_redo(long count, int old_redo) 778 { 779 int c; 780 781 // init the pointers; return if nothing to redo 782 if (read_redo(TRUE, old_redo) == FAIL) 783 return FAIL; 784 785 c = read_redo(FALSE, old_redo); 786 787 // copy the buffer name, if present 788 if (c == '"') 789 { 790 add_buff(&readbuf2, (char_u *)"\"", 1L); 791 c = read_redo(FALSE, old_redo); 792 793 // if a numbered buffer is used, increment the number 794 if (c >= '1' && c < '9') 795 ++c; 796 add_char_buff(&readbuf2, c); 797 798 // the expression register should be re-evaluated 799 if (c == '=') 800 { 801 add_char_buff(&readbuf2, CAR); 802 cmd_silent = TRUE; 803 } 804 805 c = read_redo(FALSE, old_redo); 806 } 807 808 if (c == 'v') // redo Visual 809 { 810 VIsual = curwin->w_cursor; 811 VIsual_active = TRUE; 812 VIsual_select = FALSE; 813 VIsual_reselect = TRUE; 814 redo_VIsual_busy = TRUE; 815 c = read_redo(FALSE, old_redo); 816 } 817 818 // try to enter the count (in place of a previous count) 819 if (count) 820 { 821 while (VIM_ISDIGIT(c)) // skip "old" count 822 c = read_redo(FALSE, old_redo); 823 add_num_buff(&readbuf2, count); 824 } 825 826 // copy from the redo buffer into the stuff buffer 827 add_char_buff(&readbuf2, c); 828 copy_redo(old_redo); 829 return OK; 830 } 831 832 /* 833 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing 834 * the redo buffer into readbuf2. 835 * return FAIL for failure, OK otherwise 836 */ 837 int 838 start_redo_ins(void) 839 { 840 int c; 841 842 if (read_redo(TRUE, FALSE) == FAIL) 843 return FAIL; 844 start_stuff(); 845 846 // skip the count and the command character 847 while ((c = read_redo(FALSE, FALSE)) != NUL) 848 { 849 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL) 850 { 851 if (c == 'O' || c == 'o') 852 add_buff(&readbuf2, NL_STR, -1L); 853 break; 854 } 855 } 856 857 // copy the typed text from the redo buffer into the stuff buffer 858 copy_redo(FALSE); 859 block_redo = TRUE; 860 return OK; 861 } 862 863 void 864 stop_redo_ins(void) 865 { 866 block_redo = FALSE; 867 } 868 869 /* 870 * Initialize typebuf.tb_buf to point to typebuf_init. 871 * alloc() cannot be used here: In out-of-memory situations it would 872 * be impossible to type anything. 873 */ 874 static void 875 init_typebuf(void) 876 { 877 if (typebuf.tb_buf == NULL) 878 { 879 typebuf.tb_buf = typebuf_init; 880 typebuf.tb_noremap = noremapbuf_init; 881 typebuf.tb_buflen = TYPELEN_INIT; 882 typebuf.tb_len = 0; 883 typebuf.tb_off = MAXMAPLEN + 4; 884 typebuf.tb_change_cnt = 1; 885 } 886 } 887 888 /* 889 * Returns TRUE when keys cannot be remapped. 890 */ 891 int 892 noremap_keys(void) 893 { 894 return KeyNoremap & (RM_NONE|RM_SCRIPT); 895 } 896 897 /* 898 * Insert a string in position 'offset' in the typeahead buffer (for "@r" 899 * and ":normal" command, vgetorpeek() and check_termcode()). 900 * 901 * If noremap is REMAP_YES, new string can be mapped again. 902 * If noremap is REMAP_NONE, new string cannot be mapped again. 903 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again, 904 * but abbreviations are allowed. 905 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for 906 * script-local mappings. 907 * If noremap is > 0, that many characters of the new string cannot be mapped. 908 * 909 * If nottyped is TRUE, the string does not return KeyTyped (don't use when 910 * offset is non-zero!). 911 * 912 * If silent is TRUE, cmd_silent is set when the characters are obtained. 913 * 914 * return FAIL for failure, OK otherwise 915 */ 916 int 917 ins_typebuf( 918 char_u *str, 919 int noremap, 920 int offset, 921 int nottyped, 922 int silent) 923 { 924 char_u *s1, *s2; 925 int newlen; 926 int addlen; 927 int i; 928 int newoff; 929 int val; 930 int nrm; 931 932 init_typebuf(); 933 if (++typebuf.tb_change_cnt == 0) 934 typebuf.tb_change_cnt = 1; 935 state_no_longer_safe("ins_typebuf()"); 936 937 addlen = (int)STRLEN(str); 938 939 if (offset == 0 && addlen <= typebuf.tb_off) 940 { 941 /* 942 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off] 943 */ 944 typebuf.tb_off -= addlen; 945 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); 946 } 947 else if (typebuf.tb_len == 0 && typebuf.tb_buflen 948 >= addlen + 3 * (MAXMAPLEN + 4)) 949 { 950 /* 951 * Buffer is empty and string fits in the existing buffer. 952 * Leave some space before and after, if possible. 953 */ 954 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2; 955 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); 956 } 957 else 958 { 959 /* 960 * Need to allocate a new buffer. 961 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4) 962 * characters. We add some extra room to avoid having to allocate too 963 * often. 964 */ 965 newoff = MAXMAPLEN + 4; 966 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4); 967 if (newlen < 0) // string is getting too long 968 { 969 emsg(_(e_toocompl)); // also calls flush_buffers 970 setcursor(); 971 return FAIL; 972 } 973 s1 = alloc(newlen); 974 if (s1 == NULL) // out of memory 975 return FAIL; 976 s2 = alloc(newlen); 977 if (s2 == NULL) // out of memory 978 { 979 vim_free(s1); 980 return FAIL; 981 } 982 typebuf.tb_buflen = newlen; 983 984 // copy the old chars, before the insertion point 985 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off, 986 (size_t)offset); 987 // copy the new chars 988 mch_memmove(s1 + newoff + offset, str, (size_t)addlen); 989 // copy the old chars, after the insertion point, including the NUL at 990 // the end 991 mch_memmove(s1 + newoff + offset + addlen, 992 typebuf.tb_buf + typebuf.tb_off + offset, 993 (size_t)(typebuf.tb_len - offset + 1)); 994 if (typebuf.tb_buf != typebuf_init) 995 vim_free(typebuf.tb_buf); 996 typebuf.tb_buf = s1; 997 998 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off, 999 (size_t)offset); 1000 mch_memmove(s2 + newoff + offset + addlen, 1001 typebuf.tb_noremap + typebuf.tb_off + offset, 1002 (size_t)(typebuf.tb_len - offset)); 1003 if (typebuf.tb_noremap != noremapbuf_init) 1004 vim_free(typebuf.tb_noremap); 1005 typebuf.tb_noremap = s2; 1006 1007 typebuf.tb_off = newoff; 1008 } 1009 typebuf.tb_len += addlen; 1010 1011 // If noremap == REMAP_SCRIPT: do remap script-local mappings. 1012 if (noremap == REMAP_SCRIPT) 1013 val = RM_SCRIPT; 1014 else if (noremap == REMAP_SKIP) 1015 val = RM_ABBR; 1016 else 1017 val = RM_NONE; 1018 1019 /* 1020 * Adjust typebuf.tb_noremap[] for the new characters: 1021 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are 1022 * (sometimes) not remappable 1023 * If noremap == REMAP_YES: all the new characters are mappable 1024 * If noremap > 0: "noremap" characters are not remappable, the rest 1025 * mappable 1026 */ 1027 if (noremap == REMAP_SKIP) 1028 nrm = 1; 1029 else if (noremap < 0) 1030 nrm = addlen; 1031 else 1032 nrm = noremap; 1033 for (i = 0; i < addlen; ++i) 1034 typebuf.tb_noremap[typebuf.tb_off + i + offset] = 1035 (--nrm >= 0) ? val : RM_YES; 1036 1037 // tb_maplen and tb_silent only remember the length of mapped and/or 1038 // silent mappings at the start of the buffer, assuming that a mapped 1039 // sequence doesn't result in typed characters. 1040 if (nottyped || typebuf.tb_maplen > offset) 1041 typebuf.tb_maplen += addlen; 1042 if (silent || typebuf.tb_silent > offset) 1043 { 1044 typebuf.tb_silent += addlen; 1045 cmd_silent = TRUE; 1046 } 1047 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s 1048 typebuf.tb_no_abbr_cnt += addlen; 1049 1050 return OK; 1051 } 1052 1053 /* 1054 * Put character "c" back into the typeahead buffer. 1055 * Can be used for a character obtained by vgetc() that needs to be put back. 1056 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to 1057 * the char. 1058 */ 1059 void 1060 ins_char_typebuf(int c) 1061 { 1062 char_u buf[MB_MAXBYTES + 1]; 1063 if (IS_SPECIAL(c)) 1064 { 1065 buf[0] = K_SPECIAL; 1066 buf[1] = K_SECOND(c); 1067 buf[2] = K_THIRD(c); 1068 buf[3] = NUL; 1069 } 1070 else 1071 buf[(*mb_char2bytes)(c, buf)] = NUL; 1072 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent); 1073 } 1074 1075 /* 1076 * Return TRUE if the typeahead buffer was changed (while waiting for a 1077 * character to arrive). Happens when a message was received from a client or 1078 * from feedkeys(). 1079 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf" 1080 * changed it was reallocated and the old pointer can no longer be used. 1081 * Or "typebuf.tb_off" may have been changed and we would overwrite characters 1082 * that was just added. 1083 */ 1084 int 1085 typebuf_changed( 1086 int tb_change_cnt) // old value of typebuf.tb_change_cnt 1087 { 1088 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt 1089 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) 1090 || typebuf_was_filled 1091 #endif 1092 )); 1093 } 1094 1095 /* 1096 * Return TRUE if there are no characters in the typeahead buffer that have 1097 * not been typed (result from a mapping or come from ":normal"). 1098 */ 1099 int 1100 typebuf_typed(void) 1101 { 1102 return typebuf.tb_maplen == 0; 1103 } 1104 1105 /* 1106 * Return the number of characters that are mapped (or not typed). 1107 */ 1108 int 1109 typebuf_maplen(void) 1110 { 1111 return typebuf.tb_maplen; 1112 } 1113 1114 /* 1115 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset] 1116 */ 1117 void 1118 del_typebuf(int len, int offset) 1119 { 1120 int i; 1121 1122 if (len == 0) 1123 return; // nothing to do 1124 1125 typebuf.tb_len -= len; 1126 1127 /* 1128 * Easy case: Just increase typebuf.tb_off. 1129 */ 1130 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len) 1131 >= 3 * MAXMAPLEN + 3) 1132 typebuf.tb_off += len; 1133 /* 1134 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[] 1135 */ 1136 else 1137 { 1138 i = typebuf.tb_off + offset; 1139 /* 1140 * Leave some extra room at the end to avoid reallocation. 1141 */ 1142 if (typebuf.tb_off > MAXMAPLEN) 1143 { 1144 mch_memmove(typebuf.tb_buf + MAXMAPLEN, 1145 typebuf.tb_buf + typebuf.tb_off, (size_t)offset); 1146 mch_memmove(typebuf.tb_noremap + MAXMAPLEN, 1147 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset); 1148 typebuf.tb_off = MAXMAPLEN; 1149 } 1150 // adjust typebuf.tb_buf (include the NUL at the end) 1151 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, 1152 typebuf.tb_buf + i + len, 1153 (size_t)(typebuf.tb_len - offset + 1)); 1154 // adjust typebuf.tb_noremap[] 1155 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset, 1156 typebuf.tb_noremap + i + len, 1157 (size_t)(typebuf.tb_len - offset)); 1158 } 1159 1160 if (typebuf.tb_maplen > offset) // adjust tb_maplen 1161 { 1162 if (typebuf.tb_maplen < offset + len) 1163 typebuf.tb_maplen = offset; 1164 else 1165 typebuf.tb_maplen -= len; 1166 } 1167 if (typebuf.tb_silent > offset) // adjust tb_silent 1168 { 1169 if (typebuf.tb_silent < offset + len) 1170 typebuf.tb_silent = offset; 1171 else 1172 typebuf.tb_silent -= len; 1173 } 1174 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt 1175 { 1176 if (typebuf.tb_no_abbr_cnt < offset + len) 1177 typebuf.tb_no_abbr_cnt = offset; 1178 else 1179 typebuf.tb_no_abbr_cnt -= len; 1180 } 1181 1182 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) 1183 // Reset the flag that text received from a client or from feedkeys() 1184 // was inserted in the typeahead buffer. 1185 typebuf_was_filled = FALSE; 1186 #endif 1187 if (++typebuf.tb_change_cnt == 0) 1188 typebuf.tb_change_cnt = 1; 1189 } 1190 1191 /* 1192 * Write typed characters to script file. 1193 * If recording is on put the character in the recordbuffer. 1194 */ 1195 static void 1196 gotchars(char_u *chars, int len) 1197 { 1198 char_u *s = chars; 1199 int i; 1200 static char_u buf[4]; 1201 static int buflen = 0; 1202 int todo = len; 1203 1204 while (todo--) 1205 { 1206 buf[buflen++] = *s++; 1207 1208 // When receiving a special key sequence, store it until we have all 1209 // the bytes and we can decide what to do with it. 1210 if (buflen == 1 && buf[0] == K_SPECIAL) 1211 continue; 1212 if (buflen == 2) 1213 continue; 1214 if (buflen == 3 && buf[1] == KS_EXTRA 1215 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST)) 1216 { 1217 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a 1218 // recording. 1219 buflen = 0; 1220 continue; 1221 } 1222 1223 // Handle one byte at a time; no translation to be done. 1224 for (i = 0; i < buflen; ++i) 1225 updatescript(buf[i]); 1226 1227 if (reg_recording != 0) 1228 { 1229 buf[buflen] = NUL; 1230 add_buff(&recordbuff, buf, (long)buflen); 1231 // remember how many chars were last recorded 1232 last_recorded_len += buflen; 1233 } 1234 buflen = 0; 1235 } 1236 may_sync_undo(); 1237 1238 #ifdef FEAT_EVAL 1239 // output "debug mode" message next time in debug mode 1240 debug_did_msg = FALSE; 1241 #endif 1242 1243 // Since characters have been typed, consider the following to be in 1244 // another mapping. Search string will be kept in history. 1245 ++maptick; 1246 } 1247 1248 /* 1249 * Sync undo. Called when typed characters are obtained from the typeahead 1250 * buffer, or when a menu is used. 1251 * Do not sync: 1252 * - In Insert mode, unless cursor key has been used. 1253 * - While reading a script file. 1254 * - When no_u_sync is non-zero. 1255 */ 1256 static void 1257 may_sync_undo(void) 1258 { 1259 if ((!(State & (INSERT + CMDLINE)) || arrow_used) 1260 && scriptin[curscript] == NULL) 1261 u_sync(FALSE); 1262 } 1263 1264 /* 1265 * Make "typebuf" empty and allocate new buffers. 1266 * Returns FAIL when out of memory. 1267 */ 1268 static int 1269 alloc_typebuf(void) 1270 { 1271 typebuf.tb_buf = alloc(TYPELEN_INIT); 1272 typebuf.tb_noremap = alloc(TYPELEN_INIT); 1273 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL) 1274 { 1275 free_typebuf(); 1276 return FAIL; 1277 } 1278 typebuf.tb_buflen = TYPELEN_INIT; 1279 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc 1280 typebuf.tb_len = 0; 1281 typebuf.tb_maplen = 0; 1282 typebuf.tb_silent = 0; 1283 typebuf.tb_no_abbr_cnt = 0; 1284 if (++typebuf.tb_change_cnt == 0) 1285 typebuf.tb_change_cnt = 1; 1286 return OK; 1287 } 1288 1289 /* 1290 * Free the buffers of "typebuf". 1291 */ 1292 static void 1293 free_typebuf(void) 1294 { 1295 if (typebuf.tb_buf == typebuf_init) 1296 internal_error("Free typebuf 1"); 1297 else 1298 VIM_CLEAR(typebuf.tb_buf); 1299 if (typebuf.tb_noremap == noremapbuf_init) 1300 internal_error("Free typebuf 2"); 1301 else 1302 VIM_CLEAR(typebuf.tb_noremap); 1303 } 1304 1305 /* 1306 * When doing ":so! file", the current typeahead needs to be saved, and 1307 * restored when "file" has been read completely. 1308 */ 1309 static typebuf_T saved_typebuf[NSCRIPT]; 1310 1311 int 1312 save_typebuf(void) 1313 { 1314 init_typebuf(); 1315 saved_typebuf[curscript] = typebuf; 1316 // If out of memory: restore typebuf and close file. 1317 if (alloc_typebuf() == FAIL) 1318 { 1319 closescript(); 1320 return FAIL; 1321 } 1322 return OK; 1323 } 1324 1325 static int old_char = -1; // character put back by vungetc() 1326 static int old_mod_mask; // mod_mask for ungotten character 1327 static int old_mouse_row; // mouse_row related to old_char 1328 static int old_mouse_col; // mouse_col related to old_char 1329 1330 /* 1331 * Save all three kinds of typeahead, so that the user must type at a prompt. 1332 */ 1333 void 1334 save_typeahead(tasave_T *tp) 1335 { 1336 tp->save_typebuf = typebuf; 1337 tp->typebuf_valid = (alloc_typebuf() == OK); 1338 if (!tp->typebuf_valid) 1339 typebuf = tp->save_typebuf; 1340 1341 tp->old_char = old_char; 1342 tp->old_mod_mask = old_mod_mask; 1343 old_char = -1; 1344 1345 tp->save_readbuf1 = readbuf1; 1346 readbuf1.bh_first.b_next = NULL; 1347 tp->save_readbuf2 = readbuf2; 1348 readbuf2.bh_first.b_next = NULL; 1349 # ifdef USE_INPUT_BUF 1350 tp->save_inputbuf = get_input_buf(); 1351 # endif 1352 } 1353 1354 /* 1355 * Restore the typeahead to what it was before calling save_typeahead(). 1356 * The allocated memory is freed, can only be called once! 1357 */ 1358 void 1359 restore_typeahead(tasave_T *tp) 1360 { 1361 if (tp->typebuf_valid) 1362 { 1363 free_typebuf(); 1364 typebuf = tp->save_typebuf; 1365 } 1366 1367 old_char = tp->old_char; 1368 old_mod_mask = tp->old_mod_mask; 1369 1370 free_buff(&readbuf1); 1371 readbuf1 = tp->save_readbuf1; 1372 free_buff(&readbuf2); 1373 readbuf2 = tp->save_readbuf2; 1374 # ifdef USE_INPUT_BUF 1375 set_input_buf(tp->save_inputbuf); 1376 # endif 1377 } 1378 1379 /* 1380 * Open a new script file for the ":source!" command. 1381 */ 1382 void 1383 openscript( 1384 char_u *name, 1385 int directly) // when TRUE execute directly 1386 { 1387 if (curscript + 1 == NSCRIPT) 1388 { 1389 emsg(_(e_nesting)); 1390 return; 1391 } 1392 1393 // Disallow sourcing a file in the sandbox, the commands would be executed 1394 // later, possibly outside of the sandbox. 1395 if (check_secure()) 1396 return; 1397 1398 #ifdef FEAT_EVAL 1399 if (ignore_script) 1400 // Not reading from script, also don't open one. Warning message? 1401 return; 1402 #endif 1403 1404 if (scriptin[curscript] != NULL) // already reading script 1405 ++curscript; 1406 // use NameBuff for expanded name 1407 expand_env(name, NameBuff, MAXPATHL); 1408 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL) 1409 { 1410 semsg(_(e_notopen), name); 1411 if (curscript) 1412 --curscript; 1413 return; 1414 } 1415 if (save_typebuf() == FAIL) 1416 return; 1417 1418 /* 1419 * Execute the commands from the file right now when using ":source!" 1420 * after ":global" or ":argdo" or in a loop. Also when another command 1421 * follows. This means the display won't be updated. Don't do this 1422 * always, "make test" would fail. 1423 */ 1424 if (directly) 1425 { 1426 oparg_T oa; 1427 int oldcurscript; 1428 int save_State = State; 1429 int save_restart_edit = restart_edit; 1430 int save_insertmode = p_im; 1431 int save_finish_op = finish_op; 1432 int save_msg_scroll = msg_scroll; 1433 1434 State = NORMAL; 1435 msg_scroll = FALSE; // no msg scrolling in Normal mode 1436 restart_edit = 0; // don't go to Insert mode 1437 p_im = FALSE; // don't use 'insertmode' 1438 clear_oparg(&oa); 1439 finish_op = FALSE; 1440 1441 oldcurscript = curscript; 1442 do 1443 { 1444 update_topline_cursor(); // update cursor position and topline 1445 normal_cmd(&oa, FALSE); // execute one command 1446 vpeekc(); // check for end of file 1447 } 1448 while (scriptin[oldcurscript] != NULL); 1449 1450 State = save_State; 1451 msg_scroll = save_msg_scroll; 1452 restart_edit = save_restart_edit; 1453 p_im = save_insertmode; 1454 finish_op = save_finish_op; 1455 } 1456 } 1457 1458 /* 1459 * Close the currently active input script. 1460 */ 1461 static void 1462 closescript(void) 1463 { 1464 free_typebuf(); 1465 typebuf = saved_typebuf[curscript]; 1466 1467 fclose(scriptin[curscript]); 1468 scriptin[curscript] = NULL; 1469 if (curscript > 0) 1470 --curscript; 1471 } 1472 1473 #if defined(EXITFREE) || defined(PROTO) 1474 void 1475 close_all_scripts(void) 1476 { 1477 while (scriptin[0] != NULL) 1478 closescript(); 1479 } 1480 #endif 1481 1482 /* 1483 * Return TRUE when reading keys from a script file. 1484 */ 1485 int 1486 using_script(void) 1487 { 1488 return scriptin[curscript] != NULL; 1489 } 1490 1491 /* 1492 * This function is called just before doing a blocking wait. Thus after 1493 * waiting 'updatetime' for a character to arrive. 1494 */ 1495 void 1496 before_blocking(void) 1497 { 1498 updatescript(0); 1499 #ifdef FEAT_EVAL 1500 if (may_garbage_collect) 1501 garbage_collect(FALSE); 1502 #endif 1503 } 1504 1505 /* 1506 * updatescript() is called when a character can be written into the script file 1507 * or when we have waited some time for a character (c == 0) 1508 * 1509 * All the changed memfiles are synced if c == 0 or when the number of typed 1510 * characters reaches 'updatecount' and 'updatecount' is non-zero. 1511 */ 1512 static void 1513 updatescript(int c) 1514 { 1515 static int count = 0; 1516 1517 if (c && scriptout) 1518 putc(c, scriptout); 1519 if (c == 0 || (p_uc > 0 && ++count >= p_uc)) 1520 { 1521 ml_sync_all(c == 0, TRUE); 1522 count = 0; 1523 } 1524 } 1525 1526 /* 1527 * Convert "c" plus "mod_mask" to merge the effect of modifyOtherKeys into the 1528 * character. 1529 */ 1530 int 1531 merge_modifyOtherKeys(int c_arg) 1532 { 1533 int c = c_arg; 1534 1535 if (mod_mask & MOD_MASK_CTRL) 1536 { 1537 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')) 1538 { 1539 c &= 0x1f; 1540 mod_mask &= ~MOD_MASK_CTRL; 1541 } 1542 else if (c == '6') 1543 { 1544 // CTRL-6 is equivalent to CTRL-^ 1545 c = 0x1e; 1546 mod_mask &= ~MOD_MASK_CTRL; 1547 } 1548 } 1549 if ((mod_mask & (MOD_MASK_META | MOD_MASK_ALT)) 1550 && c >= 0 && c <= 127) 1551 { 1552 c += 0x80; 1553 mod_mask &= ~(MOD_MASK_META|MOD_MASK_ALT); 1554 } 1555 return c; 1556 } 1557 1558 /* 1559 * Get the next input character. 1560 * Can return a special key or a multi-byte character. 1561 * Can return NUL when called recursively, use safe_vgetc() if that's not 1562 * wanted. 1563 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte. 1564 * Collects the bytes of a multibyte character into the whole character. 1565 * Returns the modifiers in the global "mod_mask". 1566 */ 1567 int 1568 vgetc(void) 1569 { 1570 int c, c2; 1571 int n; 1572 char_u buf[MB_MAXBYTES + 1]; 1573 int i; 1574 1575 #ifdef FEAT_EVAL 1576 // Do garbage collection when garbagecollect() was called previously and 1577 // we are now at the toplevel. 1578 if (may_garbage_collect && want_garbage_collect) 1579 garbage_collect(FALSE); 1580 #endif 1581 1582 /* 1583 * If a character was put back with vungetc, it was already processed. 1584 * Return it directly. 1585 */ 1586 if (old_char != -1) 1587 { 1588 c = old_char; 1589 old_char = -1; 1590 mod_mask = old_mod_mask; 1591 mouse_row = old_mouse_row; 1592 mouse_col = old_mouse_col; 1593 } 1594 else 1595 { 1596 mod_mask = 0x0; 1597 last_recorded_len = 0; 1598 for (;;) // this is done twice if there are modifiers 1599 { 1600 int did_inc = FALSE; 1601 1602 if (mod_mask 1603 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) 1604 || im_is_preediting() 1605 #endif 1606 #if defined(FEAT_PROP_POPUP) 1607 || popup_no_mapping() 1608 #endif 1609 ) 1610 { 1611 // no mapping after modifier has been read 1612 ++no_mapping; 1613 ++allow_keys; 1614 did_inc = TRUE; // mod_mask may change value 1615 } 1616 c = vgetorpeek(TRUE); 1617 if (did_inc) 1618 { 1619 --no_mapping; 1620 --allow_keys; 1621 } 1622 1623 // Get two extra bytes for special keys 1624 if (c == K_SPECIAL 1625 #ifdef FEAT_GUI 1626 || (gui.in_use && c == CSI) 1627 #endif 1628 ) 1629 { 1630 int save_allow_keys = allow_keys; 1631 1632 ++no_mapping; 1633 allow_keys = 0; // make sure BS is not found 1634 c2 = vgetorpeek(TRUE); // no mapping for these chars 1635 c = vgetorpeek(TRUE); 1636 --no_mapping; 1637 allow_keys = save_allow_keys; 1638 if (c2 == KS_MODIFIER) 1639 { 1640 mod_mask = c; 1641 continue; 1642 } 1643 c = TO_SPECIAL(c2, c); 1644 1645 #if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) 1646 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to 1647 // know that a menu was torn off 1648 if ( 1649 # ifdef VIMDLL 1650 gui.in_use && 1651 # endif 1652 c == K_TEAROFF) 1653 { 1654 char_u name[200]; 1655 int i; 1656 1657 // get menu path, it ends with a <CR> 1658 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; ) 1659 { 1660 name[i] = c; 1661 if (i < 199) 1662 ++i; 1663 } 1664 name[i] = NUL; 1665 gui_make_tearoff(name); 1666 continue; 1667 } 1668 #endif 1669 #if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU) 1670 // GTK: <F10> normally selects the menu, but it's passed until 1671 // here to allow mapping it. Intercept and invoke the GTK 1672 // behavior if it's not mapped. 1673 if (c == K_F10 && gui.menubar != NULL) 1674 { 1675 gtk_menu_shell_select_first( 1676 GTK_MENU_SHELL(gui.menubar), FALSE); 1677 continue; 1678 } 1679 #endif 1680 #ifdef FEAT_GUI 1681 if (gui.in_use) 1682 { 1683 // Handle focus event here, so that the caller doesn't 1684 // need to know about it. Return K_IGNORE so that we loop 1685 // once (needed if 'lazyredraw' is set). 1686 if (c == K_FOCUSGAINED || c == K_FOCUSLOST) 1687 { 1688 ui_focus_change(c == K_FOCUSGAINED); 1689 c = K_IGNORE; 1690 } 1691 1692 // Translate K_CSI to CSI. The special key is only used 1693 // to avoid it being recognized as the start of a special 1694 // key. 1695 if (c == K_CSI) 1696 c = CSI; 1697 } 1698 #endif 1699 } 1700 // a keypad or special function key was not mapped, use it like 1701 // its ASCII equivalent 1702 switch (c) 1703 { 1704 case K_KPLUS: c = '+'; break; 1705 case K_KMINUS: c = '-'; break; 1706 case K_KDIVIDE: c = '/'; break; 1707 case K_KMULTIPLY: c = '*'; break; 1708 case K_KENTER: c = CAR; break; 1709 case K_KPOINT: 1710 #ifdef MSWIN 1711 // Can be either '.' or a ',', 1712 // depending on the type of keypad. 1713 c = MapVirtualKey(VK_DECIMAL, 2); break; 1714 #else 1715 c = '.'; break; 1716 #endif 1717 case K_K0: c = '0'; break; 1718 case K_K1: c = '1'; break; 1719 case K_K2: c = '2'; break; 1720 case K_K3: c = '3'; break; 1721 case K_K4: c = '4'; break; 1722 case K_K5: c = '5'; break; 1723 case K_K6: c = '6'; break; 1724 case K_K7: c = '7'; break; 1725 case K_K8: c = '8'; break; 1726 case K_K9: c = '9'; break; 1727 1728 case K_XHOME: 1729 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT) 1730 { 1731 c = K_S_HOME; 1732 mod_mask = 0; 1733 } 1734 else if (mod_mask == MOD_MASK_CTRL) 1735 { 1736 c = K_C_HOME; 1737 mod_mask = 0; 1738 } 1739 else 1740 c = K_HOME; 1741 break; 1742 case K_XEND: 1743 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT) 1744 { 1745 c = K_S_END; 1746 mod_mask = 0; 1747 } 1748 else if (mod_mask == MOD_MASK_CTRL) 1749 { 1750 c = K_C_END; 1751 mod_mask = 0; 1752 } 1753 else 1754 c = K_END; 1755 break; 1756 1757 case K_XUP: c = K_UP; break; 1758 case K_XDOWN: c = K_DOWN; break; 1759 case K_XLEFT: c = K_LEFT; break; 1760 case K_XRIGHT: c = K_RIGHT; break; 1761 } 1762 1763 // For a multi-byte character get all the bytes and return the 1764 // converted character. 1765 // Note: This will loop until enough bytes are received! 1766 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1) 1767 { 1768 ++no_mapping; 1769 buf[0] = c; 1770 for (i = 1; i < n; ++i) 1771 { 1772 buf[i] = vgetorpeek(TRUE); 1773 if (buf[i] == K_SPECIAL 1774 #ifdef FEAT_GUI 1775 || ( 1776 # ifdef VIMDLL 1777 gui.in_use && 1778 # endif 1779 buf[i] == CSI) 1780 #endif 1781 ) 1782 { 1783 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER 1784 // sequence, which represents a K_SPECIAL (0x80), 1785 // or a CSI - KS_EXTRA - KE_CSI sequence, which 1786 // represents a CSI (0x9B), 1787 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI 1788 // too. 1789 c = vgetorpeek(TRUE); 1790 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA) 1791 buf[i] = CSI; 1792 } 1793 } 1794 --no_mapping; 1795 c = (*mb_ptr2char)(buf); 1796 } 1797 1798 if (!no_reduce_keys) 1799 // A modifier was not used for a mapping, apply it to ASCII 1800 // keys. Shift would already have been applied. 1801 c = merge_modifyOtherKeys(c); 1802 1803 break; 1804 } 1805 } 1806 1807 #ifdef FEAT_EVAL 1808 /* 1809 * In the main loop "may_garbage_collect" can be set to do garbage 1810 * collection in the first next vgetc(). It's disabled after that to 1811 * avoid internally used Lists and Dicts to be freed. 1812 */ 1813 may_garbage_collect = FALSE; 1814 #endif 1815 1816 #ifdef FEAT_BEVAL_TERM 1817 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD) 1818 { 1819 // Don't trigger 'balloonexpr' unless only the mouse was moved. 1820 bevalexpr_due_set = FALSE; 1821 ui_remove_balloon(); 1822 } 1823 #endif 1824 #ifdef FEAT_PROP_POPUP 1825 if (popup_do_filter(c)) 1826 { 1827 if (c == Ctrl_C) 1828 got_int = FALSE; // avoid looping 1829 c = K_IGNORE; 1830 } 1831 #endif 1832 1833 // Need to process the character before we know it's safe to do something 1834 // else. 1835 if (c != K_IGNORE) 1836 state_no_longer_safe("key typed"); 1837 1838 return c; 1839 } 1840 1841 /* 1842 * Like vgetc(), but never return a NUL when called recursively, get a key 1843 * directly from the user (ignoring typeahead). 1844 */ 1845 int 1846 safe_vgetc(void) 1847 { 1848 int c; 1849 1850 c = vgetc(); 1851 if (c == NUL) 1852 c = get_keystroke(); 1853 return c; 1854 } 1855 1856 /* 1857 * Like safe_vgetc(), but loop to handle K_IGNORE. 1858 * Also ignore scrollbar events. 1859 */ 1860 int 1861 plain_vgetc(void) 1862 { 1863 int c; 1864 1865 do 1866 c = safe_vgetc(); 1867 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR); 1868 1869 if (c == K_PS) 1870 // Only handle the first pasted character. Drop the rest, since we 1871 // don't know what to do with it. 1872 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL); 1873 1874 return c; 1875 } 1876 1877 /* 1878 * Check if a character is available, such that vgetc() will not block. 1879 * If the next character is a special character or multi-byte, the returned 1880 * character is not valid!. 1881 * Returns NUL if no character is available. 1882 */ 1883 int 1884 vpeekc(void) 1885 { 1886 if (old_char != -1) 1887 return old_char; 1888 return vgetorpeek(FALSE); 1889 } 1890 1891 #if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO) 1892 /* 1893 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal 1894 * codes. 1895 */ 1896 int 1897 vpeekc_nomap(void) 1898 { 1899 int c; 1900 1901 ++no_mapping; 1902 ++allow_keys; 1903 c = vpeekc(); 1904 --no_mapping; 1905 --allow_keys; 1906 return c; 1907 } 1908 #endif 1909 1910 /* 1911 * Check if any character is available, also half an escape sequence. 1912 * Trick: when no typeahead found, but there is something in the typeahead 1913 * buffer, it must be an ESC that is recognized as the start of a key code. 1914 */ 1915 int 1916 vpeekc_any(void) 1917 { 1918 int c; 1919 1920 c = vpeekc(); 1921 if (c == NUL && typebuf.tb_len > 0) 1922 c = ESC; 1923 return c; 1924 } 1925 1926 /* 1927 * Call vpeekc() without causing anything to be mapped. 1928 * Return TRUE if a character is available, FALSE otherwise. 1929 */ 1930 int 1931 char_avail(void) 1932 { 1933 int retval; 1934 1935 #ifdef FEAT_EVAL 1936 // When test_override("char_avail", 1) was called pretend there is no 1937 // typeahead. 1938 if (disable_char_avail_for_testing) 1939 return FALSE; 1940 #endif 1941 ++no_mapping; 1942 retval = vpeekc(); 1943 --no_mapping; 1944 return (retval != NUL); 1945 } 1946 1947 #if defined(FEAT_EVAL) || defined(PROTO) 1948 /* 1949 * "getchar()" function 1950 */ 1951 void 1952 f_getchar(typval_T *argvars, typval_T *rettv) 1953 { 1954 varnumber_T n; 1955 int error = FALSE; 1956 1957 #ifdef MESSAGE_QUEUE 1958 // vpeekc() used to check for messages, but that caused problems, invoking 1959 // a callback where it was not expected. Some plugins use getchar(1) in a 1960 // loop to await a message, therefore make sure we check for messages here. 1961 parse_queued_messages(); 1962 #endif 1963 1964 // Position the cursor. Needed after a message that ends in a space. 1965 windgoto(msg_row, msg_col); 1966 1967 ++no_mapping; 1968 ++allow_keys; 1969 for (;;) 1970 { 1971 if (argvars[0].v_type == VAR_UNKNOWN) 1972 // getchar(): blocking wait. 1973 n = plain_vgetc(); 1974 else if (tv_get_number_chk(&argvars[0], &error) == 1) 1975 // getchar(1): only check if char avail 1976 n = vpeekc_any(); 1977 else if (error || vpeekc_any() == NUL) 1978 // illegal argument or getchar(0) and no char avail: return zero 1979 n = 0; 1980 else 1981 // getchar(0) and char avail: return char 1982 n = plain_vgetc(); 1983 1984 if (n == K_IGNORE) 1985 continue; 1986 break; 1987 } 1988 --no_mapping; 1989 --allow_keys; 1990 1991 set_vim_var_nr(VV_MOUSE_WIN, 0); 1992 set_vim_var_nr(VV_MOUSE_WINID, 0); 1993 set_vim_var_nr(VV_MOUSE_LNUM, 0); 1994 set_vim_var_nr(VV_MOUSE_COL, 0); 1995 1996 rettv->vval.v_number = n; 1997 if (IS_SPECIAL(n) || mod_mask != 0) 1998 { 1999 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1 2000 int i = 0; 2001 2002 // Turn a special key into three bytes, plus modifier. 2003 if (mod_mask != 0) 2004 { 2005 temp[i++] = K_SPECIAL; 2006 temp[i++] = KS_MODIFIER; 2007 temp[i++] = mod_mask; 2008 } 2009 if (IS_SPECIAL(n)) 2010 { 2011 temp[i++] = K_SPECIAL; 2012 temp[i++] = K_SECOND(n); 2013 temp[i++] = K_THIRD(n); 2014 } 2015 else if (has_mbyte) 2016 i += (*mb_char2bytes)(n, temp + i); 2017 else 2018 temp[i++] = n; 2019 temp[i++] = NUL; 2020 rettv->v_type = VAR_STRING; 2021 rettv->vval.v_string = vim_strsave(temp); 2022 2023 if (is_mouse_key(n)) 2024 { 2025 int row = mouse_row; 2026 int col = mouse_col; 2027 win_T *win; 2028 linenr_T lnum; 2029 win_T *wp; 2030 int winnr = 1; 2031 2032 if (row >= 0 && col >= 0) 2033 { 2034 // Find the window at the mouse coordinates and compute the 2035 // text position. 2036 win = mouse_find_win(&row, &col, FIND_POPUP); 2037 if (win == NULL) 2038 return; 2039 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL); 2040 #ifdef FEAT_PROP_POPUP 2041 if (WIN_IS_POPUP(win)) 2042 winnr = 0; 2043 else 2044 #endif 2045 for (wp = firstwin; wp != win && wp != NULL; 2046 wp = wp->w_next) 2047 ++winnr; 2048 set_vim_var_nr(VV_MOUSE_WIN, winnr); 2049 set_vim_var_nr(VV_MOUSE_WINID, win->w_id); 2050 set_vim_var_nr(VV_MOUSE_LNUM, lnum); 2051 set_vim_var_nr(VV_MOUSE_COL, col + 1); 2052 } 2053 } 2054 } 2055 } 2056 2057 /* 2058 * "getcharmod()" function 2059 */ 2060 void 2061 f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv) 2062 { 2063 rettv->vval.v_number = mod_mask; 2064 } 2065 #endif // FEAT_EVAL 2066 2067 #if defined(MESSAGE_QUEUE) || defined(PROTO) 2068 # define MAX_REPEAT_PARSE 8 2069 2070 /* 2071 * Process messages that have been queued for netbeans or clientserver. 2072 * Also check if any jobs have ended. 2073 * These functions can call arbitrary vimscript and should only be called when 2074 * it is safe to do so. 2075 */ 2076 void 2077 parse_queued_messages(void) 2078 { 2079 int old_curwin_id; 2080 int old_curbuf_fnum; 2081 int i; 2082 int save_may_garbage_collect = may_garbage_collect; 2083 static int entered = 0; 2084 int was_safe = get_was_safe_state(); 2085 2086 // Do not handle messages while redrawing, because it may cause buffers to 2087 // change or be wiped while they are being redrawn. 2088 if (updating_screen) 2089 return; 2090 2091 // If memory allocation fails during startup we'll exit but curbuf or 2092 // curwin could be NULL. 2093 if (curbuf == NULL || curwin == NULL) 2094 return; 2095 2096 old_curbuf_fnum = curbuf->b_fnum; 2097 old_curwin_id = curwin->w_id; 2098 2099 ++entered; 2100 2101 // may_garbage_collect is set in main_loop() to do garbage collection when 2102 // blocking to wait on a character. We don't want that while parsing 2103 // messages, a callback may invoke vgetc() while lists and dicts are in use 2104 // in the call stack. 2105 may_garbage_collect = FALSE; 2106 2107 // Loop when a job ended, but don't keep looping forever. 2108 for (i = 0; i < MAX_REPEAT_PARSE; ++i) 2109 { 2110 // For Win32 mch_breakcheck() does not check for input, do it here. 2111 # if defined(MSWIN) && defined(FEAT_JOB_CHANNEL) 2112 channel_handle_events(FALSE); 2113 # endif 2114 2115 # ifdef FEAT_NETBEANS_INTG 2116 // Process the queued netbeans messages. 2117 netbeans_parse_messages(); 2118 # endif 2119 # ifdef FEAT_JOB_CHANNEL 2120 // Write any buffer lines still to be written. 2121 channel_write_any_lines(); 2122 2123 // Process the messages queued on channels. 2124 channel_parse_messages(); 2125 # endif 2126 # if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) 2127 // Process the queued clientserver messages. 2128 server_parse_messages(); 2129 # endif 2130 # ifdef FEAT_JOB_CHANNEL 2131 // Check if any jobs have ended. If so, repeat the above to handle 2132 // changes, e.g. stdin may have been closed. 2133 if (job_check_ended()) 2134 continue; 2135 # endif 2136 # ifdef FEAT_TERMINAL 2137 free_unused_terminals(); 2138 # endif 2139 # ifdef FEAT_SOUND_CANBERRA 2140 if (has_sound_callback_in_queue()) 2141 invoke_sound_callback(); 2142 # endif 2143 break; 2144 } 2145 2146 // When not nested we'll go back to waiting for a typed character. If it 2147 // was safe before then this triggers a SafeStateAgain autocommand event. 2148 if (entered == 1 && was_safe) 2149 may_trigger_safestateagain(); 2150 2151 may_garbage_collect = save_may_garbage_collect; 2152 2153 // If the current window or buffer changed we need to bail out of the 2154 // waiting loop. E.g. when a job exit callback closes the terminal window. 2155 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum) 2156 ins_char_typebuf(K_IGNORE); 2157 2158 --entered; 2159 } 2160 #endif 2161 2162 2163 typedef enum { 2164 map_result_fail, // failed, break loop 2165 map_result_get, // get a character from typeahead 2166 map_result_retry, // try to map again 2167 map_result_nomatch // no matching mapping, get char 2168 } map_result_T; 2169 2170 /* 2171 * Check if the bytes at the start of the typeahead buffer are a character used 2172 * in CTRL-X mode. This includes the form with a CTRL modifier. 2173 */ 2174 static int 2175 at_ctrl_x_key(void) 2176 { 2177 char_u *p = typebuf.tb_buf + typebuf.tb_off; 2178 int c = *p; 2179 2180 if (typebuf.tb_len > 3 2181 && c == K_SPECIAL 2182 && p[1] == KS_MODIFIER 2183 && (p[2] & MOD_MASK_CTRL)) 2184 c = p[3] & 0x1f; 2185 return vim_is_ctrl_x_key(c); 2186 } 2187 2188 /* 2189 * Handle mappings in the typeahead buffer. 2190 * - When something was mapped, return map_result_retry for recursive mappings. 2191 * - When nothing mapped and typeahead has a character: return map_result_get. 2192 * - When there is no match yet, return map_result_nomatch, need to get more 2193 * typeahead. 2194 */ 2195 static int 2196 handle_mapping( 2197 int *keylenp, 2198 int *timedout, 2199 int *mapdepth) 2200 { 2201 mapblock_T *mp = NULL; 2202 mapblock_T *mp2; 2203 mapblock_T *mp_match; 2204 int mp_match_len = 0; 2205 int max_mlen = 0; 2206 int tb_c1; 2207 int mlen; 2208 #ifdef FEAT_LANGMAP 2209 int nolmaplen; 2210 #endif 2211 int keylen = *keylenp; 2212 int i; 2213 int local_State = get_real_state(); 2214 2215 /* 2216 * Check for a mappable key sequence. 2217 * Walk through one maphash[] list until we find an entry that matches. 2218 * 2219 * Don't look for mappings if: 2220 * - no_mapping set: mapping disabled (e.g. for CTRL-V) 2221 * - maphash_valid not set: no mappings present. 2222 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped 2223 * - in insert or cmdline mode and 'paste' option set 2224 * - waiting for "hit return to continue" and CR or SPACE typed 2225 * - waiting for a char with --more-- 2226 * - in Ctrl-X mode, and we get a valid char for that mode 2227 */ 2228 tb_c1 = typebuf.tb_buf[typebuf.tb_off]; 2229 if (no_mapping == 0 && is_maphash_valid() 2230 && (no_zero_mapping == 0 || tb_c1 != '0') 2231 && (typebuf.tb_maplen == 0 2232 || (p_remap 2233 && (typebuf.tb_noremap[typebuf.tb_off] 2234 & (RM_NONE|RM_ABBR)) == 0)) 2235 && !(p_paste && (State & (INSERT + CMDLINE))) 2236 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' ')) 2237 && State != ASKMORE 2238 && State != CONFIRM 2239 && !((ctrl_x_mode_not_default() && at_ctrl_x_key()) 2240 || ((compl_cont_status & CONT_LOCAL) 2241 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P)))) 2242 { 2243 #ifdef FEAT_LANGMAP 2244 if (tb_c1 == K_SPECIAL) 2245 nolmaplen = 2; 2246 else 2247 { 2248 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0 2249 && get_real_state() != SELECTMODE); 2250 nolmaplen = 0; 2251 } 2252 #endif 2253 // First try buffer-local mappings. 2254 mp = get_buf_maphash_list(local_State, tb_c1); 2255 mp2 = get_maphash_list(local_State, tb_c1); 2256 if (mp == NULL) 2257 { 2258 // There are no buffer-local mappings. 2259 mp = mp2; 2260 mp2 = NULL; 2261 } 2262 2263 /* 2264 * Loop until a partly matching mapping is found or all (local) 2265 * mappings have been checked. 2266 * The longest full match is remembered in "mp_match". 2267 * A full match is only accepted if there is no partly match, so "aa" 2268 * and "aaa" can both be mapped. 2269 */ 2270 mp_match = NULL; 2271 mp_match_len = 0; 2272 for ( ; mp != NULL; 2273 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next)) 2274 { 2275 // Only consider an entry if the first character matches and it is 2276 // for the current state. 2277 // Skip ":lmap" mappings if keys were mapped. 2278 if (mp->m_keys[0] == tb_c1 2279 && (mp->m_mode & local_State) 2280 && !(mp->m_simplified && seenModifyOtherKeys) 2281 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0)) 2282 { 2283 #ifdef FEAT_LANGMAP 2284 int nomap = nolmaplen; 2285 int c2; 2286 #endif 2287 // find the match length of this mapping 2288 for (mlen = 1; mlen < typebuf.tb_len; ++mlen) 2289 { 2290 #ifdef FEAT_LANGMAP 2291 c2 = typebuf.tb_buf[typebuf.tb_off + mlen]; 2292 if (nomap > 0) 2293 --nomap; 2294 else if (c2 == K_SPECIAL) 2295 nomap = 2; 2296 else 2297 LANGMAP_ADJUST(c2, TRUE); 2298 if (mp->m_keys[mlen] != c2) 2299 #else 2300 if (mp->m_keys[mlen] != 2301 typebuf.tb_buf[typebuf.tb_off + mlen]) 2302 #endif 2303 break; 2304 } 2305 2306 // Don't allow mapping the first byte(s) of a multi-byte char. 2307 // Happens when mapping <M-a> and then changing 'encoding'. 2308 // Beware that 0x80 is escaped. 2309 { 2310 char_u *p1 = mp->m_keys; 2311 char_u *p2 = mb_unescape(&p1); 2312 2313 if (has_mbyte && p2 != NULL 2314 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2)) 2315 mlen = 0; 2316 } 2317 2318 // Check an entry whether it matches. 2319 // - Full match: mlen == keylen 2320 // - Partly match: mlen == typebuf.tb_len 2321 keylen = mp->m_keylen; 2322 if (mlen == keylen || (mlen == typebuf.tb_len 2323 && typebuf.tb_len < keylen)) 2324 { 2325 char_u *s; 2326 int n; 2327 2328 // If only script-local mappings are allowed, check if the 2329 // mapping starts with K_SNR. 2330 s = typebuf.tb_noremap + typebuf.tb_off; 2331 if (*s == RM_SCRIPT 2332 && (mp->m_keys[0] != K_SPECIAL 2333 || mp->m_keys[1] != KS_EXTRA 2334 || mp->m_keys[2] != (int)KE_SNR)) 2335 continue; 2336 2337 // If one of the typed keys cannot be remapped, skip the 2338 // entry. 2339 for (n = mlen; --n >= 0; ) 2340 if (*s++ & (RM_NONE|RM_ABBR)) 2341 break; 2342 if (n >= 0) 2343 continue; 2344 2345 if (keylen > typebuf.tb_len) 2346 { 2347 if (!*timedout && !(mp_match != NULL 2348 && mp_match->m_nowait)) 2349 { 2350 // break at a partly match 2351 keylen = KEYLEN_PART_MAP; 2352 break; 2353 } 2354 } 2355 else if (keylen > mp_match_len) 2356 { 2357 // found a longer match 2358 mp_match = mp; 2359 mp_match_len = keylen; 2360 } 2361 } 2362 else 2363 // No match; may have to check for termcode at next 2364 // character. 2365 if (max_mlen < mlen) 2366 max_mlen = mlen; 2367 } 2368 } 2369 2370 // If no partly match found, use the longest full match. 2371 if (keylen != KEYLEN_PART_MAP) 2372 { 2373 mp = mp_match; 2374 keylen = mp_match_len; 2375 } 2376 } 2377 2378 /* 2379 * Check for match with 'pastetoggle' 2380 */ 2381 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL))) 2382 { 2383 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen) 2384 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen]) 2385 break; 2386 if (p_pt[mlen] == NUL) // match 2387 { 2388 // write chars to script file(s) 2389 if (mlen > typebuf.tb_maplen) 2390 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen, 2391 mlen - typebuf.tb_maplen); 2392 2393 del_typebuf(mlen, 0); // remove the chars 2394 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0); 2395 if (!(State & INSERT)) 2396 { 2397 msg_col = 0; 2398 msg_row = Rows - 1; 2399 msg_clr_eos(); // clear ruler 2400 } 2401 status_redraw_all(); 2402 redraw_statuslines(); 2403 showmode(); 2404 setcursor(); 2405 *keylenp = keylen; 2406 return map_result_retry; 2407 } 2408 // Need more chars for partly match. 2409 if (mlen == typebuf.tb_len) 2410 keylen = KEYLEN_PART_KEY; 2411 else if (max_mlen < mlen) 2412 // no match, may have to check for termcode at next character 2413 max_mlen = mlen + 1; 2414 } 2415 2416 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP) 2417 { 2418 int save_keylen = keylen; 2419 2420 /* 2421 * When no matching mapping found or found a non-matching mapping that 2422 * matches at least what the matching mapping matched: 2423 * Check if we have a terminal code, when: 2424 * - mapping is allowed, 2425 * - keys have not been mapped, 2426 * - and not an ESC sequence, not in insert mode or p_ek is on, 2427 * - and when not timed out, 2428 */ 2429 if ((no_mapping == 0 || allow_keys != 0) 2430 && (typebuf.tb_maplen == 0 2431 || (p_remap && typebuf.tb_noremap[ 2432 typebuf.tb_off] == RM_YES)) 2433 && !*timedout) 2434 { 2435 keylen = check_termcode(max_mlen + 1, 2436 NULL, 0, NULL); 2437 2438 // If no termcode matched but 'pastetoggle' matched partially it's 2439 // like an incomplete key sequence. 2440 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY) 2441 keylen = KEYLEN_PART_KEY; 2442 2443 // When getting a partial match, but the last characters were not 2444 // typed, don't wait for a typed character to complete the 2445 // termcode. This helps a lot when a ":normal" command ends in an 2446 // ESC. 2447 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen) 2448 keylen = 0; 2449 } 2450 else 2451 keylen = 0; 2452 if (keylen == 0) // no matching terminal code 2453 { 2454 #ifdef AMIGA 2455 // check for window bounds report 2456 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[ 2457 typebuf.tb_off] & 0xff) == CSI) 2458 { 2459 char_u *s; 2460 2461 for (s = typebuf.tb_buf + typebuf.tb_off + 1; 2462 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len 2463 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' '); 2464 ++s) 2465 ; 2466 if (*s == 'r' || *s == '|') // found one 2467 { 2468 del_typebuf( 2469 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0); 2470 // get size and redraw screen 2471 shell_resized(); 2472 *keylenp = keylen; 2473 return map_result_retry; 2474 } 2475 if (*s == NUL) // need more characters 2476 keylen = KEYLEN_PART_KEY; 2477 } 2478 if (keylen >= 0) 2479 #endif 2480 // When there was a matching mapping and no termcode could be 2481 // replaced after another one, use that mapping (loop around). 2482 // If there was no mapping at all use the character from the 2483 // typeahead buffer right here. 2484 if (mp == NULL) 2485 { 2486 *keylenp = keylen; 2487 return map_result_get; // got character, break for loop 2488 } 2489 } 2490 2491 if (keylen > 0) // full matching terminal code 2492 { 2493 #if defined(FEAT_GUI) && defined(FEAT_MENU) 2494 if (typebuf.tb_len >= 2 2495 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL 2496 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU) 2497 { 2498 int idx; 2499 2500 // Using a menu may cause a break in undo! It's like using 2501 // gotchars(), but without recording or writing to a script 2502 // file. 2503 may_sync_undo(); 2504 del_typebuf(3, 0); 2505 idx = get_menu_index(current_menu, local_State); 2506 if (idx != MENU_INDEX_INVALID) 2507 { 2508 // In Select mode and a Visual mode menu is used: Switch 2509 // to Visual mode temporarily. Append K_SELECT to switch 2510 // back to Select mode. 2511 if (VIsual_active && VIsual_select 2512 && (current_menu->modes & VISUAL)) 2513 { 2514 VIsual_select = FALSE; 2515 (void)ins_typebuf(K_SELECT_STRING, 2516 REMAP_NONE, 0, TRUE, FALSE); 2517 } 2518 ins_typebuf(current_menu->strings[idx], 2519 current_menu->noremap[idx], 2520 0, TRUE, current_menu->silent[idx]); 2521 } 2522 } 2523 #endif // FEAT_GUI && FEAT_MENU 2524 *keylenp = keylen; 2525 return map_result_retry; // try mapping again 2526 } 2527 2528 // Partial match: get some more characters. When a matching mapping 2529 // was found use that one. 2530 if (mp == NULL || keylen < 0) 2531 keylen = KEYLEN_PART_KEY; 2532 else 2533 keylen = mp_match_len; 2534 } 2535 2536 /* 2537 * complete match 2538 */ 2539 if (keylen >= 0 && keylen <= typebuf.tb_len) 2540 { 2541 char_u *map_str; 2542 2543 #ifdef FEAT_EVAL 2544 int save_m_expr; 2545 int save_m_noremap; 2546 int save_m_silent; 2547 char_u *save_m_keys; 2548 char_u *save_m_str; 2549 #else 2550 # define save_m_noremap mp->m_noremap 2551 # define save_m_silent mp->m_silent 2552 #endif 2553 2554 // write chars to script file(s) 2555 if (keylen > typebuf.tb_maplen) 2556 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen, 2557 keylen - typebuf.tb_maplen); 2558 2559 cmd_silent = (typebuf.tb_silent > 0); 2560 del_typebuf(keylen, 0); // remove the mapped keys 2561 2562 /* 2563 * Put the replacement string in front of mapstr. 2564 * The depth check catches ":map x y" and ":map y x". 2565 */ 2566 if (++*mapdepth >= p_mmd) 2567 { 2568 emsg(_("E223: recursive mapping")); 2569 if (State & CMDLINE) 2570 redrawcmdline(); 2571 else 2572 setcursor(); 2573 flush_buffers(FLUSH_MINIMAL); 2574 *mapdepth = 0; // for next one 2575 *keylenp = keylen; 2576 return map_result_fail; 2577 } 2578 2579 /* 2580 * In Select mode and a Visual mode mapping is used: Switch to Visual 2581 * mode temporarily. Append K_SELECT to switch back to Select mode. 2582 */ 2583 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL)) 2584 { 2585 VIsual_select = FALSE; 2586 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE); 2587 } 2588 2589 #ifdef FEAT_EVAL 2590 // Copy the values from *mp that are used, because evaluating the 2591 // expression may invoke a function that redefines the mapping, thereby 2592 // making *mp invalid. 2593 save_m_expr = mp->m_expr; 2594 save_m_noremap = mp->m_noremap; 2595 save_m_silent = mp->m_silent; 2596 save_m_keys = NULL; // only saved when needed 2597 save_m_str = NULL; // only saved when needed 2598 2599 /* 2600 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also 2601 * save and restore the command line for "normal :". 2602 */ 2603 if (mp->m_expr) 2604 { 2605 int save_vgetc_busy = vgetc_busy; 2606 int save_may_garbage_collect = may_garbage_collect; 2607 int was_screen_col = screen_cur_col; 2608 int was_screen_row = screen_cur_row; 2609 2610 vgetc_busy = 0; 2611 may_garbage_collect = FALSE; 2612 2613 save_m_keys = vim_strsave(mp->m_keys); 2614 save_m_str = vim_strsave(mp->m_str); 2615 map_str = eval_map_expr(save_m_str, NUL); 2616 2617 // The mapping may do anything, but we expect it to take care of 2618 // redrawing. Do put the cursor back where it was. 2619 windgoto(was_screen_row, was_screen_col); 2620 out_flush(); 2621 2622 vgetc_busy = save_vgetc_busy; 2623 may_garbage_collect = save_may_garbage_collect; 2624 } 2625 else 2626 #endif 2627 map_str = mp->m_str; 2628 2629 /* 2630 * Insert the 'to' part in the typebuf.tb_buf. 2631 * If 'from' field is the same as the start of the 'to' field, don't 2632 * remap the first character (but do allow abbreviations). 2633 * If m_noremap is set, don't remap the whole 'to' part. 2634 */ 2635 if (map_str == NULL) 2636 i = FAIL; 2637 else 2638 { 2639 int noremap; 2640 2641 if (save_m_noremap != REMAP_YES) 2642 noremap = save_m_noremap; 2643 else if ( 2644 #ifdef FEAT_EVAL 2645 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys, 2646 (size_t)keylen) 2647 #else 2648 STRNCMP(map_str, mp->m_keys, (size_t)keylen) 2649 #endif 2650 != 0) 2651 noremap = REMAP_YES; 2652 else 2653 noremap = REMAP_SKIP; 2654 i = ins_typebuf(map_str, noremap, 2655 0, TRUE, cmd_silent || save_m_silent); 2656 #ifdef FEAT_EVAL 2657 if (save_m_expr) 2658 vim_free(map_str); 2659 #endif 2660 } 2661 #ifdef FEAT_EVAL 2662 vim_free(save_m_keys); 2663 vim_free(save_m_str); 2664 #endif 2665 *keylenp = keylen; 2666 if (i == FAIL) 2667 return map_result_fail; 2668 return map_result_retry; 2669 } 2670 2671 *keylenp = keylen; 2672 return map_result_nomatch; 2673 } 2674 2675 /* 2676 * unget one character (can only be done once!) 2677 */ 2678 void 2679 vungetc(int c) 2680 { 2681 old_char = c; 2682 old_mod_mask = mod_mask; 2683 old_mouse_row = mouse_row; 2684 old_mouse_col = mouse_col; 2685 } 2686 2687 /* 2688 * Get a byte: 2689 * 1. from the stuffbuffer 2690 * This is used for abbreviated commands like "D" -> "d$". 2691 * Also used to redo a command for ".". 2692 * 2. from the typeahead buffer 2693 * Stores text obtained previously but not used yet. 2694 * Also stores the result of mappings. 2695 * Also used for the ":normal" command. 2696 * 3. from the user 2697 * This may do a blocking wait if "advance" is TRUE. 2698 * 2699 * if "advance" is TRUE (vgetc()): 2700 * Really get the character. 2701 * KeyTyped is set to TRUE in the case the user typed the key. 2702 * KeyStuffed is TRUE if the character comes from the stuff buffer. 2703 * if "advance" is FALSE (vpeekc()): 2704 * Just look whether there is a character available. 2705 * Return NUL if not. 2706 * 2707 * When "no_mapping" is zero, checks for mappings in the current mode. 2708 * Only returns one byte (of a multi-byte character). 2709 * K_SPECIAL and CSI may be escaped, need to get two more bytes then. 2710 */ 2711 static int 2712 vgetorpeek(int advance) 2713 { 2714 int c, c1; 2715 int timedout = FALSE; // waited for more than 1 second 2716 // for mapping to complete 2717 int mapdepth = 0; // check for recursive mapping 2718 int mode_deleted = FALSE; // set when mode has been deleted 2719 #ifdef FEAT_CMDL_INFO 2720 int new_wcol, new_wrow; 2721 #endif 2722 #ifdef FEAT_GUI 2723 int shape_changed = FALSE; // adjusted cursor shape 2724 #endif 2725 int n; 2726 int old_wcol, old_wrow; 2727 int wait_tb_len; 2728 2729 /* 2730 * This function doesn't work very well when called recursively. This may 2731 * happen though, because of: 2732 * 1. The call to add_to_showcmd(). char_avail() is then used to check if 2733 * there is a character available, which calls this function. In that 2734 * case we must return NUL, to indicate no character is available. 2735 * 2. A GUI callback function writes to the screen, causing a 2736 * wait_return(). 2737 * Using ":normal" can also do this, but it saves the typeahead buffer, 2738 * thus it should be OK. But don't get a key from the user then. 2739 */ 2740 if (vgetc_busy > 0 && ex_normal_busy == 0) 2741 return NUL; 2742 2743 ++vgetc_busy; 2744 2745 if (advance) 2746 KeyStuffed = FALSE; 2747 2748 init_typebuf(); 2749 start_stuff(); 2750 if (advance && typebuf.tb_maplen == 0) 2751 reg_executing = 0; 2752 do 2753 { 2754 /* 2755 * get a character: 1. from the stuffbuffer 2756 */ 2757 if (typeahead_char != 0) 2758 { 2759 c = typeahead_char; 2760 if (advance) 2761 typeahead_char = 0; 2762 } 2763 else 2764 c = read_readbuffers(advance); 2765 if (c != NUL && !got_int) 2766 { 2767 if (advance) 2768 { 2769 // KeyTyped = FALSE; When the command that stuffed something 2770 // was typed, behave like the stuffed command was typed. 2771 // needed for CTRL-W CTRL-] to open a fold, for example. 2772 KeyStuffed = TRUE; 2773 } 2774 if (typebuf.tb_no_abbr_cnt == 0) 2775 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now 2776 } 2777 else 2778 { 2779 /* 2780 * Loop until we either find a matching mapped key, or we 2781 * are sure that it is not a mapped key. 2782 * If a mapped key sequence is found we go back to the start to 2783 * try re-mapping. 2784 */ 2785 for (;;) 2786 { 2787 long wait_time; 2788 int keylen = 0; 2789 #ifdef FEAT_CMDL_INFO 2790 int showcmd_idx; 2791 #endif 2792 /* 2793 * ui_breakcheck() is slow, don't use it too often when 2794 * inside a mapping. But call it each time for typed 2795 * characters. 2796 */ 2797 if (typebuf.tb_maplen) 2798 line_breakcheck(); 2799 else 2800 ui_breakcheck(); // check for CTRL-C 2801 if (got_int) 2802 { 2803 // flush all input 2804 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L); 2805 2806 /* 2807 * If inchar() returns TRUE (script file was active) or we 2808 * are inside a mapping, get out of Insert mode. 2809 * Otherwise we behave like having gotten a CTRL-C. 2810 * As a result typing CTRL-C in insert mode will 2811 * really insert a CTRL-C. 2812 */ 2813 if ((c || typebuf.tb_maplen) 2814 && (State & (INSERT + CMDLINE))) 2815 c = ESC; 2816 else 2817 c = Ctrl_C; 2818 flush_buffers(FLUSH_INPUT); // flush all typeahead 2819 2820 if (advance) 2821 { 2822 // Also record this character, it might be needed to 2823 // get out of Insert mode. 2824 *typebuf.tb_buf = c; 2825 gotchars(typebuf.tb_buf, 1); 2826 } 2827 cmd_silent = FALSE; 2828 2829 break; 2830 } 2831 else if (typebuf.tb_len > 0) 2832 { 2833 /* 2834 * Check for a mapping in "typebuf". 2835 */ 2836 map_result_T result = handle_mapping( 2837 &keylen, &timedout, &mapdepth); 2838 2839 if (result == map_result_retry) 2840 // try mapping again 2841 continue; 2842 if (result == map_result_fail) 2843 { 2844 // failed, use the outer loop 2845 c = -1; 2846 break; 2847 } 2848 if (result == map_result_get) 2849 { 2850 /* 2851 * get a character: 2. from the typeahead buffer 2852 */ 2853 c = typebuf.tb_buf[typebuf.tb_off]; 2854 if (advance) // remove chars from tb_buf 2855 { 2856 cmd_silent = (typebuf.tb_silent > 0); 2857 if (typebuf.tb_maplen > 0) 2858 KeyTyped = FALSE; 2859 else 2860 { 2861 KeyTyped = TRUE; 2862 // write char to script file(s) 2863 gotchars(typebuf.tb_buf 2864 + typebuf.tb_off, 1); 2865 } 2866 KeyNoremap = typebuf.tb_noremap[ 2867 typebuf.tb_off]; 2868 del_typebuf(1, 0); 2869 } 2870 break; 2871 } 2872 2873 // not enough characters, get more 2874 } 2875 2876 /* 2877 * get a character: 3. from the user - handle <Esc> in Insert mode 2878 */ 2879 /* 2880 * Special case: if we get an <ESC> in insert mode and there 2881 * are no more characters at once, we pretend to go out of 2882 * insert mode. This prevents the one second delay after 2883 * typing an <ESC>. If we get something after all, we may 2884 * have to redisplay the mode. That the cursor is in the wrong 2885 * place does not matter. 2886 */ 2887 c = 0; 2888 #ifdef FEAT_CMDL_INFO 2889 new_wcol = curwin->w_wcol; 2890 new_wrow = curwin->w_wrow; 2891 #endif 2892 if ( advance 2893 && typebuf.tb_len == 1 2894 && typebuf.tb_buf[typebuf.tb_off] == ESC 2895 && !no_mapping 2896 && ex_normal_busy == 0 2897 && typebuf.tb_maplen == 0 2898 && (State & INSERT) 2899 && (p_timeout 2900 || (keylen == KEYLEN_PART_KEY && p_ttimeout)) 2901 && (c = inchar(typebuf.tb_buf + typebuf.tb_off 2902 + typebuf.tb_len, 3, 25L)) == 0) 2903 { 2904 colnr_T col = 0, vcol; 2905 char_u *ptr; 2906 2907 if (mode_displayed) 2908 { 2909 unshowmode(TRUE); 2910 mode_deleted = TRUE; 2911 } 2912 #ifdef FEAT_GUI 2913 // may show a different cursor shape 2914 if (gui.in_use && State != NORMAL && !cmd_silent) 2915 { 2916 int save_State; 2917 2918 save_State = State; 2919 State = NORMAL; 2920 gui_update_cursor(TRUE, FALSE); 2921 State = save_State; 2922 shape_changed = TRUE; 2923 } 2924 #endif 2925 validate_cursor(); 2926 old_wcol = curwin->w_wcol; 2927 old_wrow = curwin->w_wrow; 2928 2929 // move cursor left, if possible 2930 if (curwin->w_cursor.col != 0) 2931 { 2932 if (curwin->w_wcol > 0) 2933 { 2934 if (did_ai) 2935 { 2936 /* 2937 * We are expecting to truncate the trailing 2938 * white-space, so find the last non-white 2939 * character -- webb 2940 */ 2941 col = vcol = curwin->w_wcol = 0; 2942 ptr = ml_get_curline(); 2943 while (col < curwin->w_cursor.col) 2944 { 2945 if (!VIM_ISWHITE(ptr[col])) 2946 curwin->w_wcol = vcol; 2947 vcol += lbr_chartabsize(ptr, ptr + col, 2948 (colnr_T)vcol); 2949 if (has_mbyte) 2950 col += (*mb_ptr2len)(ptr + col); 2951 else 2952 ++col; 2953 } 2954 curwin->w_wrow = curwin->w_cline_row 2955 + curwin->w_wcol / curwin->w_width; 2956 curwin->w_wcol %= curwin->w_width; 2957 curwin->w_wcol += curwin_col_off(); 2958 col = 0; // no correction needed 2959 } 2960 else 2961 { 2962 --curwin->w_wcol; 2963 col = curwin->w_cursor.col - 1; 2964 } 2965 } 2966 else if (curwin->w_p_wrap && curwin->w_wrow) 2967 { 2968 --curwin->w_wrow; 2969 curwin->w_wcol = curwin->w_width - 1; 2970 col = curwin->w_cursor.col - 1; 2971 } 2972 if (has_mbyte && col > 0 && curwin->w_wcol > 0) 2973 { 2974 // Correct when the cursor is on the right halve 2975 // of a double-wide character. 2976 ptr = ml_get_curline(); 2977 col -= (*mb_head_off)(ptr, ptr + col); 2978 if ((*mb_ptr2cells)(ptr + col) > 1) 2979 --curwin->w_wcol; 2980 } 2981 } 2982 setcursor(); 2983 out_flush(); 2984 #ifdef FEAT_CMDL_INFO 2985 new_wcol = curwin->w_wcol; 2986 new_wrow = curwin->w_wrow; 2987 #endif 2988 curwin->w_wcol = old_wcol; 2989 curwin->w_wrow = old_wrow; 2990 } 2991 if (c < 0) 2992 continue; // end of input script reached 2993 2994 // Allow mapping for just typed characters. When we get here c 2995 // is the number of extra bytes and typebuf.tb_len is 1. 2996 for (n = 1; n <= c; ++n) 2997 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES; 2998 typebuf.tb_len += c; 2999 3000 // buffer full, don't map 3001 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN) 3002 { 3003 timedout = TRUE; 3004 continue; 3005 } 3006 3007 if (ex_normal_busy > 0) 3008 { 3009 #ifdef FEAT_CMDWIN 3010 static int tc = 0; 3011 #endif 3012 3013 // No typeahead left and inside ":normal". Must return 3014 // something to avoid getting stuck. When an incomplete 3015 // mapping is present, behave like it timed out. 3016 if (typebuf.tb_len > 0) 3017 { 3018 timedout = TRUE; 3019 continue; 3020 } 3021 // When 'insertmode' is set, ESC just beeps in Insert 3022 // mode. Use CTRL-L to make edit() return. 3023 // For the command line only CTRL-C always breaks it. 3024 // For the cmdline window: Alternate between ESC and 3025 // CTRL-C: ESC for most situations and CTRL-C to close the 3026 // cmdline window. 3027 if (p_im && (State & INSERT)) 3028 c = Ctrl_L; 3029 #ifdef FEAT_TERMINAL 3030 else if (terminal_is_active()) 3031 c = K_CANCEL; 3032 #endif 3033 else if ((State & CMDLINE) 3034 #ifdef FEAT_CMDWIN 3035 || (cmdwin_type > 0 && tc == ESC) 3036 #endif 3037 ) 3038 c = Ctrl_C; 3039 else 3040 c = ESC; 3041 #ifdef FEAT_CMDWIN 3042 tc = c; 3043 #endif 3044 // return from main_loop() 3045 if (pending_exmode_active) 3046 exmode_active = EXMODE_NORMAL; 3047 3048 break; 3049 } 3050 3051 /* 3052 * get a character: 3. from the user - update display 3053 */ 3054 // In insert mode a screen update is skipped when characters 3055 // are still available. But when those available characters 3056 // are part of a mapping, and we are going to do a blocking 3057 // wait here. Need to update the screen to display the 3058 // changed text so far. Also for when 'lazyredraw' is set and 3059 // redrawing was postponed because there was something in the 3060 // input buffer (e.g., termresponse). 3061 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0 3062 && advance && must_redraw != 0 && !need_wait_return) 3063 { 3064 update_screen(0); 3065 setcursor(); // put cursor back where it belongs 3066 } 3067 3068 /* 3069 * If we have a partial match (and are going to wait for more 3070 * input from the user), show the partially matched characters 3071 * to the user with showcmd. 3072 */ 3073 #ifdef FEAT_CMDL_INFO 3074 showcmd_idx = 0; 3075 #endif 3076 c1 = 0; 3077 if (typebuf.tb_len > 0 && advance && !exmode_active) 3078 { 3079 if (((State & (NORMAL | INSERT)) || State == LANGMAP) 3080 && State != HITRETURN) 3081 { 3082 // this looks nice when typing a dead character map 3083 if (State & INSERT 3084 && ptr2cells(typebuf.tb_buf + typebuf.tb_off 3085 + typebuf.tb_len - 1) == 1) 3086 { 3087 edit_putchar(typebuf.tb_buf[typebuf.tb_off 3088 + typebuf.tb_len - 1], FALSE); 3089 setcursor(); // put cursor back where it belongs 3090 c1 = 1; 3091 } 3092 #ifdef FEAT_CMDL_INFO 3093 // need to use the col and row from above here 3094 old_wcol = curwin->w_wcol; 3095 old_wrow = curwin->w_wrow; 3096 curwin->w_wcol = new_wcol; 3097 curwin->w_wrow = new_wrow; 3098 push_showcmd(); 3099 if (typebuf.tb_len > SHOWCMD_COLS) 3100 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS; 3101 while (showcmd_idx < typebuf.tb_len) 3102 (void)add_to_showcmd( 3103 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]); 3104 curwin->w_wcol = old_wcol; 3105 curwin->w_wrow = old_wrow; 3106 #endif 3107 } 3108 3109 // this looks nice when typing a dead character map 3110 if ((State & CMDLINE) 3111 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) 3112 && cmdline_star == 0 3113 #endif 3114 && ptr2cells(typebuf.tb_buf + typebuf.tb_off 3115 + typebuf.tb_len - 1) == 1) 3116 { 3117 putcmdline(typebuf.tb_buf[typebuf.tb_off 3118 + typebuf.tb_len - 1], FALSE); 3119 c1 = 1; 3120 } 3121 } 3122 3123 /* 3124 * get a character: 3. from the user - get it 3125 */ 3126 if (typebuf.tb_len == 0) 3127 // timedout may have been set while waiting for a mapping 3128 // that has a <Nop> RHS. 3129 timedout = FALSE; 3130 3131 if (advance) 3132 { 3133 if (typebuf.tb_len == 0 3134 || !(p_timeout 3135 || (p_ttimeout && keylen == KEYLEN_PART_KEY))) 3136 // blocking wait 3137 wait_time = -1L; 3138 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0) 3139 wait_time = p_ttm; 3140 else 3141 wait_time = p_tm; 3142 } 3143 else 3144 wait_time = 0; 3145 3146 wait_tb_len = typebuf.tb_len; 3147 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len, 3148 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1, 3149 wait_time); 3150 3151 #ifdef FEAT_CMDL_INFO 3152 if (showcmd_idx != 0) 3153 pop_showcmd(); 3154 #endif 3155 if (c1 == 1) 3156 { 3157 if (State & INSERT) 3158 edit_unputchar(); 3159 if (State & CMDLINE) 3160 unputcmdline(); 3161 else 3162 setcursor(); // put cursor back where it belongs 3163 } 3164 3165 if (c < 0) 3166 continue; // end of input script reached 3167 if (c == NUL) // no character available 3168 { 3169 if (!advance) 3170 break; 3171 if (wait_tb_len > 0) // timed out 3172 { 3173 timedout = TRUE; 3174 continue; 3175 } 3176 } 3177 else 3178 { // allow mapping for just typed characters 3179 while (typebuf.tb_buf[typebuf.tb_off 3180 + typebuf.tb_len] != NUL) 3181 typebuf.tb_noremap[typebuf.tb_off 3182 + typebuf.tb_len++] = RM_YES; 3183 #ifdef HAVE_INPUT_METHOD 3184 // Get IM status right after getting keys, not after the 3185 // timeout for a mapping (focus may be lost by then). 3186 vgetc_im_active = im_get_status(); 3187 #endif 3188 } 3189 } // for (;;) 3190 } // if (!character from stuffbuf) 3191 3192 // if advance is FALSE don't loop on NULs 3193 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL)); 3194 3195 /* 3196 * The "INSERT" message is taken care of here: 3197 * if we return an ESC to exit insert mode, the message is deleted 3198 * if we don't return an ESC but deleted the message before, redisplay it 3199 */ 3200 if (advance && p_smd && msg_silent == 0 && (State & INSERT)) 3201 { 3202 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed) 3203 { 3204 if (typebuf.tb_len && !KeyTyped) 3205 redraw_cmdline = TRUE; // delete mode later 3206 else 3207 unshowmode(FALSE); 3208 } 3209 else if (c != ESC && mode_deleted) 3210 { 3211 if (typebuf.tb_len && !KeyTyped) 3212 redraw_cmdline = TRUE; // show mode later 3213 else 3214 showmode(); 3215 } 3216 } 3217 #ifdef FEAT_GUI 3218 // may unshow different cursor shape 3219 if (gui.in_use && shape_changed) 3220 gui_update_cursor(TRUE, FALSE); 3221 #endif 3222 if (timedout && c == ESC) 3223 { 3224 char_u nop_buf[3]; 3225 3226 // When recording there will be no timeout. Add a <Nop> after the ESC 3227 // to avoid that it forms a key code with following characters. 3228 nop_buf[0] = K_SPECIAL; 3229 nop_buf[1] = KS_EXTRA; 3230 nop_buf[2] = KE_NOP; 3231 gotchars(nop_buf, 3); 3232 } 3233 3234 --vgetc_busy; 3235 3236 return c; 3237 } 3238 3239 /* 3240 * inchar() - get one character from 3241 * 1. a scriptfile 3242 * 2. the keyboard 3243 * 3244 * As many characters as we can get (up to 'maxlen') are put in "buf" and 3245 * NUL terminated (buffer length must be 'maxlen' + 1). 3246 * Minimum for "maxlen" is 3!!!! 3247 * 3248 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into 3249 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received 3250 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0 3251 * otherwise. 3252 * 3253 * If we got an interrupt all input is read until none is available. 3254 * 3255 * If wait_time == 0 there is no waiting for the char. 3256 * If wait_time == n we wait for n msec for a character to arrive. 3257 * If wait_time == -1 we wait forever for a character to arrive. 3258 * 3259 * Return the number of obtained characters. 3260 * Return -1 when end of input script reached. 3261 */ 3262 static int 3263 inchar( 3264 char_u *buf, 3265 int maxlen, 3266 long wait_time) // milli seconds 3267 { 3268 int len = 0; // init for GCC 3269 int retesc = FALSE; // return ESC with gotint 3270 int script_char; 3271 int tb_change_cnt = typebuf.tb_change_cnt; 3272 3273 if (wait_time == -1L || wait_time > 100L) // flush output before waiting 3274 { 3275 cursor_on(); 3276 out_flush_cursor(FALSE, FALSE); 3277 #if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE) 3278 if (gui.in_use && postponed_mouseshape) 3279 update_mouseshape(-1); 3280 #endif 3281 } 3282 3283 /* 3284 * Don't reset these when at the hit-return prompt, otherwise a endless 3285 * recursive loop may result (write error in swapfile, hit-return, timeout 3286 * on char wait, flush swapfile, write error....). 3287 */ 3288 if (State != HITRETURN) 3289 { 3290 did_outofmem_msg = FALSE; // display out of memory message (again) 3291 did_swapwrite_msg = FALSE; // display swap file write error again 3292 } 3293 undo_off = FALSE; // restart undo now 3294 3295 /* 3296 * Get a character from a script file if there is one. 3297 * If interrupted: Stop reading script files, close them all. 3298 */ 3299 script_char = -1; 3300 while (scriptin[curscript] != NULL && script_char < 0 3301 #ifdef FEAT_EVAL 3302 && !ignore_script 3303 #endif 3304 ) 3305 { 3306 #ifdef MESSAGE_QUEUE 3307 parse_queued_messages(); 3308 #endif 3309 3310 if (got_int || (script_char = getc(scriptin[curscript])) < 0) 3311 { 3312 // Reached EOF. 3313 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may 3314 // point inside typebuf.tb_buf[]. Don't use buf[] after this! 3315 closescript(); 3316 /* 3317 * When reading script file is interrupted, return an ESC to get 3318 * back to normal mode. 3319 * Otherwise return -1, because typebuf.tb_buf[] has changed. 3320 */ 3321 if (got_int) 3322 retesc = TRUE; 3323 else 3324 return -1; 3325 } 3326 else 3327 { 3328 buf[0] = script_char; 3329 len = 1; 3330 } 3331 } 3332 3333 if (script_char < 0) // did not get a character from script 3334 { 3335 /* 3336 * If we got an interrupt, skip all previously typed characters and 3337 * return TRUE if quit reading script file. 3338 * Stop reading typeahead when a single CTRL-C was read, 3339 * fill_input_buf() returns this when not able to read from stdin. 3340 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[] 3341 * and buf may be pointing inside typebuf.tb_buf[]. 3342 */ 3343 if (got_int) 3344 { 3345 #define DUM_LEN MAXMAPLEN * 3 + 3 3346 char_u dum[DUM_LEN + 1]; 3347 3348 for (;;) 3349 { 3350 len = ui_inchar(dum, DUM_LEN, 0L, 0); 3351 if (len == 0 || (len == 1 && dum[0] == 3)) 3352 break; 3353 } 3354 return retesc; 3355 } 3356 3357 /* 3358 * Always flush the output characters when getting input characters 3359 * from the user and not just peeking. 3360 */ 3361 if (wait_time == -1L || wait_time > 10L) 3362 out_flush(); 3363 3364 /* 3365 * Fill up to a third of the buffer, because each character may be 3366 * tripled below. 3367 */ 3368 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt); 3369 } 3370 3371 // If the typebuf was changed further down, it is like nothing was added by 3372 // this call. 3373 if (typebuf_changed(tb_change_cnt)) 3374 return 0; 3375 3376 // Note the change in the typeahead buffer, this matters for when 3377 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer 3378 // function. 3379 if (len > 0 && ++typebuf.tb_change_cnt == 0) 3380 typebuf.tb_change_cnt = 1; 3381 3382 return fix_input_buffer(buf, len); 3383 } 3384 3385 /* 3386 * Fix typed characters for use by vgetc() and check_termcode(). 3387 * "buf[]" must have room to triple the number of bytes! 3388 * Returns the new length. 3389 */ 3390 int 3391 fix_input_buffer(char_u *buf, int len) 3392 { 3393 int i; 3394 char_u *p = buf; 3395 3396 /* 3397 * Two characters are special: NUL and K_SPECIAL. 3398 * When compiled With the GUI CSI is also special. 3399 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER 3400 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER 3401 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI 3402 */ 3403 for (i = len; --i >= 0; ++p) 3404 { 3405 #ifdef FEAT_GUI 3406 // When the GUI is used any character can come after a CSI, don't 3407 // escape it. 3408 if (gui.in_use && p[0] == CSI && i >= 2) 3409 { 3410 p += 2; 3411 i -= 2; 3412 } 3413 # ifndef MSWIN 3414 // When the GUI is not used CSI needs to be escaped. 3415 else if (!gui.in_use && p[0] == CSI) 3416 { 3417 mch_memmove(p + 3, p + 1, (size_t)i); 3418 *p++ = K_SPECIAL; 3419 *p++ = KS_EXTRA; 3420 *p = (int)KE_CSI; 3421 len += 2; 3422 } 3423 # endif 3424 else 3425 #endif 3426 if (p[0] == NUL || (p[0] == K_SPECIAL 3427 // timeout may generate K_CURSORHOLD 3428 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD) 3429 #if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) 3430 // Win32 console passes modifiers 3431 && ( 3432 # ifdef VIMDLL 3433 gui.in_use || 3434 # endif 3435 (i < 2 || p[1] != KS_MODIFIER)) 3436 #endif 3437 )) 3438 { 3439 mch_memmove(p + 3, p + 1, (size_t)i); 3440 p[2] = K_THIRD(p[0]); 3441 p[1] = K_SECOND(p[0]); 3442 p[0] = K_SPECIAL; 3443 p += 2; 3444 len += 2; 3445 } 3446 } 3447 *p = NUL; // add trailing NUL 3448 return len; 3449 } 3450 3451 #if defined(USE_INPUT_BUF) || defined(PROTO) 3452 /* 3453 * Return TRUE when bytes are in the input buffer or in the typeahead buffer. 3454 * Normally the input buffer would be sufficient, but the server_to_input_buf() 3455 * or feedkeys() may insert characters in the typeahead buffer while we are 3456 * waiting for input to arrive. 3457 */ 3458 int 3459 input_available(void) 3460 { 3461 return (!vim_is_input_buf_empty() 3462 # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) 3463 || typebuf_was_filled 3464 # endif 3465 ); 3466 } 3467 #endif 3468