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 * map.c: functions for maps and abbreviations 12 */ 13 14 #include "vim.h" 15 16 /* 17 * List used for abbreviations. 18 */ 19 static mapblock_T *first_abbr = NULL; // first entry in abbrlist 20 21 /* 22 * Each mapping is put in one of the 256 hash lists, to speed up finding it. 23 */ 24 static mapblock_T *(maphash[256]); 25 static int maphash_valid = FALSE; 26 27 /* 28 * Make a hash value for a mapping. 29 * "mode" is the lower 4 bits of the State for the mapping. 30 * "c1" is the first character of the "lhs". 31 * Returns a value between 0 and 255, index in maphash. 32 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode. 33 */ 34 #define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING + TERMINAL)) ? (c1) : ((c1) ^ 0x80)) 35 36 /* 37 * Get the start of the hashed map list for "state" and first character "c". 38 */ 39 mapblock_T * 40 get_maphash_list(int state, int c) 41 { 42 return maphash[MAP_HASH(state, c)]; 43 } 44 45 /* 46 * Get the buffer-local hashed map list for "state" and first character "c". 47 */ 48 mapblock_T * 49 get_buf_maphash_list(int state, int c) 50 { 51 return curbuf->b_maphash[MAP_HASH(state, c)]; 52 } 53 54 int 55 is_maphash_valid(void) 56 { 57 return maphash_valid; 58 } 59 60 /* 61 * Initialize maphash[] for first use. 62 */ 63 static void 64 validate_maphash(void) 65 { 66 if (!maphash_valid) 67 { 68 vim_memset(maphash, 0, sizeof(maphash)); 69 maphash_valid = TRUE; 70 } 71 } 72 73 /* 74 * Delete one entry from the abbrlist or maphash[]. 75 * "mpp" is a pointer to the m_next field of the PREVIOUS entry! 76 */ 77 static void 78 map_free(mapblock_T **mpp) 79 { 80 mapblock_T *mp; 81 82 mp = *mpp; 83 vim_free(mp->m_keys); 84 vim_free(mp->m_str); 85 vim_free(mp->m_orig_str); 86 *mpp = mp->m_next; 87 vim_free(mp); 88 } 89 90 /* 91 * Return characters to represent the map mode in an allocated string. 92 * Returns NULL when out of memory. 93 */ 94 static char_u * 95 map_mode_to_chars(int mode) 96 { 97 garray_T mapmode; 98 99 ga_init2(&mapmode, 1, 7); 100 101 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE) 102 ga_append(&mapmode, '!'); // :map! 103 else if (mode & INSERT) 104 ga_append(&mapmode, 'i'); // :imap 105 else if (mode & LANGMAP) 106 ga_append(&mapmode, 'l'); // :lmap 107 else if (mode & CMDLINE) 108 ga_append(&mapmode, 'c'); // :cmap 109 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) 110 == NORMAL + VISUAL + SELECTMODE + OP_PENDING) 111 ga_append(&mapmode, ' '); // :map 112 else 113 { 114 if (mode & NORMAL) 115 ga_append(&mapmode, 'n'); // :nmap 116 if (mode & OP_PENDING) 117 ga_append(&mapmode, 'o'); // :omap 118 if (mode & TERMINAL) 119 ga_append(&mapmode, 't'); // :tmap 120 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE) 121 ga_append(&mapmode, 'v'); // :vmap 122 else 123 { 124 if (mode & VISUAL) 125 ga_append(&mapmode, 'x'); // :xmap 126 if (mode & SELECTMODE) 127 ga_append(&mapmode, 's'); // :smap 128 } 129 } 130 131 ga_append(&mapmode, NUL); 132 return (char_u *)mapmode.ga_data; 133 } 134 135 static void 136 showmap( 137 mapblock_T *mp, 138 int local) // TRUE for buffer-local map 139 { 140 int len = 1; 141 char_u *mapchars; 142 143 if (message_filtered(mp->m_keys) && message_filtered(mp->m_str)) 144 return; 145 146 if (msg_didout || msg_silent != 0) 147 { 148 msg_putchar('\n'); 149 if (got_int) // 'q' typed at MORE prompt 150 return; 151 } 152 153 mapchars = map_mode_to_chars(mp->m_mode); 154 if (mapchars != NULL) 155 { 156 msg_puts((char *)mapchars); 157 len = (int)STRLEN(mapchars); 158 vim_free(mapchars); 159 } 160 161 while (++len <= 3) 162 msg_putchar(' '); 163 164 // Display the LHS. Get length of what we write. 165 len = msg_outtrans_special(mp->m_keys, TRUE, 0); 166 do 167 { 168 msg_putchar(' '); // padd with blanks 169 ++len; 170 } while (len < 12); 171 172 if (mp->m_noremap == REMAP_NONE) 173 msg_puts_attr("*", HL_ATTR(HLF_8)); 174 else if (mp->m_noremap == REMAP_SCRIPT) 175 msg_puts_attr("&", HL_ATTR(HLF_8)); 176 else 177 msg_putchar(' '); 178 179 if (local) 180 msg_putchar('@'); 181 else 182 msg_putchar(' '); 183 184 // Use FALSE below if we only want things like <Up> to show up as such on 185 // the rhs, and not M-x etc, TRUE gets both -- webb 186 if (*mp->m_str == NUL) 187 msg_puts_attr("<Nop>", HL_ATTR(HLF_8)); 188 else 189 { 190 // Remove escaping of CSI, because "m_str" is in a format to be used 191 // as typeahead. 192 char_u *s = vim_strsave(mp->m_str); 193 if (s != NULL) 194 { 195 vim_unescape_csi(s); 196 msg_outtrans_special(s, FALSE, 0); 197 vim_free(s); 198 } 199 } 200 #ifdef FEAT_EVAL 201 if (p_verbose > 0) 202 last_set_msg(mp->m_script_ctx); 203 #endif 204 out_flush(); // show one line at a time 205 } 206 207 /* 208 * map[!] : show all key mappings 209 * map[!] {lhs} : show key mapping for {lhs} 210 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs} 211 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs} 212 * unmap[!] {lhs} : remove key mapping for {lhs} 213 * abbr : show all abbreviations 214 * abbr {lhs} : show abbreviations for {lhs} 215 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs} 216 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs} 217 * unabbr {lhs} : remove abbreviation for {lhs} 218 * 219 * maptype: 0 for :map, 1 for :unmap, 2 for noremap. 220 * 221 * arg is pointer to any arguments. Note: arg cannot be a read-only string, 222 * it will be modified. 223 * 224 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING 225 * for :map! mode is INSERT + CMDLINE 226 * for :cmap mode is CMDLINE 227 * for :imap mode is INSERT 228 * for :lmap mode is LANGMAP 229 * for :nmap mode is NORMAL 230 * for :vmap mode is VISUAL + SELECTMODE 231 * for :xmap mode is VISUAL 232 * for :smap mode is SELECTMODE 233 * for :omap mode is OP_PENDING 234 * for :tmap mode is TERMINAL 235 * 236 * for :abbr mode is INSERT + CMDLINE 237 * for :iabbr mode is INSERT 238 * for :cabbr mode is CMDLINE 239 * 240 * Return 0 for success 241 * 1 for invalid arguments 242 * 2 for no match 243 * 4 for out of mem 244 * 5 for entry not unique 245 */ 246 int 247 do_map( 248 int maptype, 249 char_u *arg, 250 int mode, 251 int abbrev) // not a mapping but an abbreviation 252 { 253 char_u *keys; 254 mapblock_T *mp, **mpp; 255 char_u *rhs; 256 char_u *p; 257 int n; 258 int len = 0; // init for GCC 259 char_u *newstr; 260 int hasarg; 261 int haskey; 262 int did_it = FALSE; 263 int did_local = FALSE; 264 int round; 265 char_u *keys_buf = NULL; 266 char_u *arg_buf = NULL; 267 int retval = 0; 268 int do_backslash; 269 int hash; 270 int new_hash; 271 mapblock_T **abbr_table; 272 mapblock_T **map_table; 273 int unique = FALSE; 274 int nowait = FALSE; 275 int silent = FALSE; 276 int special = FALSE; 277 #ifdef FEAT_EVAL 278 int expr = FALSE; 279 #endif 280 int noremap; 281 char_u *orig_rhs; 282 283 keys = arg; 284 map_table = maphash; 285 abbr_table = &first_abbr; 286 287 // For ":noremap" don't remap, otherwise do remap. 288 if (maptype == 2) 289 noremap = REMAP_NONE; 290 else 291 noremap = REMAP_YES; 292 293 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in 294 // any order. 295 for (;;) 296 { 297 // Check for "<buffer>": mapping local to buffer. 298 if (STRNCMP(keys, "<buffer>", 8) == 0) 299 { 300 keys = skipwhite(keys + 8); 301 map_table = curbuf->b_maphash; 302 abbr_table = &curbuf->b_first_abbr; 303 continue; 304 } 305 306 // Check for "<nowait>": don't wait for more characters. 307 if (STRNCMP(keys, "<nowait>", 8) == 0) 308 { 309 keys = skipwhite(keys + 8); 310 nowait = TRUE; 311 continue; 312 } 313 314 // Check for "<silent>": don't echo commands. 315 if (STRNCMP(keys, "<silent>", 8) == 0) 316 { 317 keys = skipwhite(keys + 8); 318 silent = TRUE; 319 continue; 320 } 321 322 // Check for "<special>": accept special keys in <> 323 if (STRNCMP(keys, "<special>", 9) == 0) 324 { 325 keys = skipwhite(keys + 9); 326 special = TRUE; 327 continue; 328 } 329 330 #ifdef FEAT_EVAL 331 // Check for "<script>": remap script-local mappings only 332 if (STRNCMP(keys, "<script>", 8) == 0) 333 { 334 keys = skipwhite(keys + 8); 335 noremap = REMAP_SCRIPT; 336 continue; 337 } 338 339 // Check for "<expr>": {rhs} is an expression. 340 if (STRNCMP(keys, "<expr>", 6) == 0) 341 { 342 keys = skipwhite(keys + 6); 343 expr = TRUE; 344 continue; 345 } 346 #endif 347 // Check for "<unique>": don't overwrite an existing mapping. 348 if (STRNCMP(keys, "<unique>", 8) == 0) 349 { 350 keys = skipwhite(keys + 8); 351 unique = TRUE; 352 continue; 353 } 354 break; 355 } 356 357 validate_maphash(); 358 359 // Find end of keys and skip CTRL-Vs (and backslashes) in it. 360 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'. 361 // with :unmap white space is included in the keys, no argument possible. 362 p = keys; 363 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL); 364 while (*p && (maptype == 1 || !VIM_ISWHITE(*p))) 365 { 366 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) && 367 p[1] != NUL) 368 ++p; // skip CTRL-V or backslash 369 ++p; 370 } 371 if (*p != NUL) 372 *p++ = NUL; 373 374 p = skipwhite(p); 375 rhs = p; 376 hasarg = (*rhs != NUL); 377 haskey = (*keys != NUL); 378 379 // check for :unmap without argument 380 if (maptype == 1 && !haskey) 381 { 382 retval = 1; 383 goto theend; 384 } 385 386 // If mapping has been given as ^V<C_UP> say, then replace the term codes 387 // with the appropriate two bytes. If it is a shifted special key, unshift 388 // it too, giving another two bytes. 389 // replace_termcodes() may move the result to allocated memory, which 390 // needs to be freed later (*keys_buf and *arg_buf). 391 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes. 392 if (haskey) 393 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special); 394 orig_rhs = rhs; 395 if (hasarg) 396 { 397 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing 398 rhs = (char_u *)""; 399 else 400 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special); 401 } 402 403 // check arguments and translate function keys 404 if (haskey) 405 { 406 len = (int)STRLEN(keys); 407 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars 408 { 409 retval = 1; 410 goto theend; 411 } 412 413 if (abbrev && maptype != 1) 414 { 415 // If an abbreviation ends in a keyword character, the 416 // rest must be all keyword-char or all non-keyword-char. 417 // Otherwise we won't be able to find the start of it in a 418 // vi-compatible way. 419 if (has_mbyte) 420 { 421 int first, last; 422 int same = -1; 423 424 first = vim_iswordp(keys); 425 last = first; 426 p = keys + (*mb_ptr2len)(keys); 427 n = 1; 428 while (p < keys + len) 429 { 430 ++n; // nr of (multi-byte) chars 431 last = vim_iswordp(p); // type of last char 432 if (same == -1 && last != first) 433 same = n - 1; // count of same char type 434 p += (*mb_ptr2len)(p); 435 } 436 if (last && n > 2 && same >= 0 && same < n - 1) 437 { 438 retval = 1; 439 goto theend; 440 } 441 } 442 else if (vim_iswordc(keys[len - 1])) // ends in keyword char 443 for (n = 0; n < len - 2; ++n) 444 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2])) 445 { 446 retval = 1; 447 goto theend; 448 } 449 // An abbreviation cannot contain white space. 450 for (n = 0; n < len; ++n) 451 if (VIM_ISWHITE(keys[n])) 452 { 453 retval = 1; 454 goto theend; 455 } 456 } 457 } 458 459 if (haskey && hasarg && abbrev) // if we will add an abbreviation 460 no_abbr = FALSE; // reset flag that indicates there are 461 // no abbreviations 462 463 if (!haskey || (maptype != 1 && !hasarg)) 464 msg_start(); 465 466 // Check if a new local mapping wasn't already defined globally. 467 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1) 468 { 469 // need to loop over all global hash lists 470 for (hash = 0; hash < 256 && !got_int; ++hash) 471 { 472 if (abbrev) 473 { 474 if (hash != 0) // there is only one abbreviation list 475 break; 476 mp = first_abbr; 477 } 478 else 479 mp = maphash[hash]; 480 for ( ; mp != NULL && !got_int; mp = mp->m_next) 481 { 482 // check entries with the same mode 483 if ((mp->m_mode & mode) != 0 484 && mp->m_keylen == len 485 && unique 486 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0) 487 { 488 if (abbrev) 489 semsg(_("E224: global abbreviation already exists for %s"), 490 mp->m_keys); 491 else 492 semsg(_("E225: global mapping already exists for %s"), 493 mp->m_keys); 494 retval = 5; 495 goto theend; 496 } 497 } 498 } 499 } 500 501 // When listing global mappings, also list buffer-local ones here. 502 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1) 503 { 504 // need to loop over all global hash lists 505 for (hash = 0; hash < 256 && !got_int; ++hash) 506 { 507 if (abbrev) 508 { 509 if (hash != 0) // there is only one abbreviation list 510 break; 511 mp = curbuf->b_first_abbr; 512 } 513 else 514 mp = curbuf->b_maphash[hash]; 515 for ( ; mp != NULL && !got_int; mp = mp->m_next) 516 { 517 // check entries with the same mode 518 if ((mp->m_mode & mode) != 0) 519 { 520 if (!haskey) // show all entries 521 { 522 showmap(mp, TRUE); 523 did_local = TRUE; 524 } 525 else 526 { 527 n = mp->m_keylen; 528 if (STRNCMP(mp->m_keys, keys, 529 (size_t)(n < len ? n : len)) == 0) 530 { 531 showmap(mp, TRUE); 532 did_local = TRUE; 533 } 534 } 535 } 536 } 537 } 538 } 539 540 // Find an entry in the maphash[] list that matches. 541 // For :unmap we may loop two times: once to try to unmap an entry with a 542 // matching 'from' part, a second time, if the first fails, to unmap an 543 // entry with a matching 'to' part. This was done to allow ":ab foo bar" 544 // to be unmapped by typing ":unab foo", where "foo" will be replaced by 545 // "bar" because of the abbreviation. 546 for (round = 0; (round == 0 || maptype == 1) && round <= 1 547 && !did_it && !got_int; ++round) 548 { 549 // need to loop over all hash lists 550 for (hash = 0; hash < 256 && !got_int; ++hash) 551 { 552 if (abbrev) 553 { 554 if (hash > 0) // there is only one abbreviation list 555 break; 556 mpp = abbr_table; 557 } 558 else 559 mpp = &(map_table[hash]); 560 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp) 561 { 562 563 if (!(mp->m_mode & mode)) // skip entries with wrong mode 564 { 565 mpp = &(mp->m_next); 566 continue; 567 } 568 if (!haskey) // show all entries 569 { 570 showmap(mp, map_table != maphash); 571 did_it = TRUE; 572 } 573 else // do we have a match? 574 { 575 if (round) // second round: Try unmap "rhs" string 576 { 577 n = (int)STRLEN(mp->m_str); 578 p = mp->m_str; 579 } 580 else 581 { 582 n = mp->m_keylen; 583 p = mp->m_keys; 584 } 585 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0) 586 { 587 if (maptype == 1) // delete entry 588 { 589 // Only accept a full match. For abbreviations we 590 // ignore trailing space when matching with the 591 // "lhs", since an abbreviation can't have 592 // trailing space. 593 if (n != len && (!abbrev || round || n > len 594 || *skipwhite(keys + n) != NUL)) 595 { 596 mpp = &(mp->m_next); 597 continue; 598 } 599 // We reset the indicated mode bits. If nothing is 600 // left the entry is deleted below. 601 mp->m_mode &= ~mode; 602 did_it = TRUE; // remember we did something 603 } 604 else if (!hasarg) // show matching entry 605 { 606 showmap(mp, map_table != maphash); 607 did_it = TRUE; 608 } 609 else if (n != len) // new entry is ambiguous 610 { 611 mpp = &(mp->m_next); 612 continue; 613 } 614 else if (unique) 615 { 616 if (abbrev) 617 semsg(_("E226: abbreviation already exists for %s"), 618 p); 619 else 620 semsg(_("E227: mapping already exists for %s"), p); 621 retval = 5; 622 goto theend; 623 } 624 else // new rhs for existing entry 625 { 626 mp->m_mode &= ~mode; // remove mode bits 627 if (mp->m_mode == 0 && !did_it) // reuse entry 628 { 629 newstr = vim_strsave(rhs); 630 if (newstr == NULL) 631 { 632 retval = 4; // no mem 633 goto theend; 634 } 635 vim_free(mp->m_str); 636 mp->m_str = newstr; 637 vim_free(mp->m_orig_str); 638 mp->m_orig_str = vim_strsave(orig_rhs); 639 mp->m_noremap = noremap; 640 mp->m_nowait = nowait; 641 mp->m_silent = silent; 642 mp->m_mode = mode; 643 #ifdef FEAT_EVAL 644 mp->m_expr = expr; 645 mp->m_script_ctx = current_sctx; 646 mp->m_script_ctx.sc_lnum += sourcing_lnum; 647 #endif 648 did_it = TRUE; 649 } 650 } 651 if (mp->m_mode == 0) // entry can be deleted 652 { 653 map_free(mpp); 654 continue; // continue with *mpp 655 } 656 657 // May need to put this entry into another hash list. 658 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]); 659 if (!abbrev && new_hash != hash) 660 { 661 *mpp = mp->m_next; 662 mp->m_next = map_table[new_hash]; 663 map_table[new_hash] = mp; 664 665 continue; // continue with *mpp 666 } 667 } 668 } 669 mpp = &(mp->m_next); 670 } 671 } 672 } 673 674 if (maptype == 1) // delete entry 675 { 676 if (!did_it) 677 retval = 2; // no match 678 else if (*keys == Ctrl_C) 679 { 680 // If CTRL-C has been unmapped, reuse it for Interrupting. 681 if (map_table == curbuf->b_maphash) 682 curbuf->b_mapped_ctrl_c &= ~mode; 683 else 684 mapped_ctrl_c &= ~mode; 685 } 686 goto theend; 687 } 688 689 if (!haskey || !hasarg) // print entries 690 { 691 if (!did_it && !did_local) 692 { 693 if (abbrev) 694 msg(_("No abbreviation found")); 695 else 696 msg(_("No mapping found")); 697 } 698 goto theend; // listing finished 699 } 700 701 if (did_it) // have added the new entry already 702 goto theend; 703 704 // Get here when adding a new entry to the maphash[] list or abbrlist. 705 mp = ALLOC_ONE(mapblock_T); 706 if (mp == NULL) 707 { 708 retval = 4; // no mem 709 goto theend; 710 } 711 712 // If CTRL-C has been mapped, don't always use it for Interrupting. 713 if (*keys == Ctrl_C) 714 { 715 if (map_table == curbuf->b_maphash) 716 curbuf->b_mapped_ctrl_c |= mode; 717 else 718 mapped_ctrl_c |= mode; 719 } 720 721 mp->m_keys = vim_strsave(keys); 722 mp->m_str = vim_strsave(rhs); 723 mp->m_orig_str = vim_strsave(orig_rhs); 724 if (mp->m_keys == NULL || mp->m_str == NULL) 725 { 726 vim_free(mp->m_keys); 727 vim_free(mp->m_str); 728 vim_free(mp->m_orig_str); 729 vim_free(mp); 730 retval = 4; // no mem 731 goto theend; 732 } 733 mp->m_keylen = (int)STRLEN(mp->m_keys); 734 mp->m_noremap = noremap; 735 mp->m_nowait = nowait; 736 mp->m_silent = silent; 737 mp->m_mode = mode; 738 #ifdef FEAT_EVAL 739 mp->m_expr = expr; 740 mp->m_script_ctx = current_sctx; 741 mp->m_script_ctx.sc_lnum += sourcing_lnum; 742 #endif 743 744 // add the new entry in front of the abbrlist or maphash[] list 745 if (abbrev) 746 { 747 mp->m_next = *abbr_table; 748 *abbr_table = mp; 749 } 750 else 751 { 752 n = MAP_HASH(mp->m_mode, mp->m_keys[0]); 753 mp->m_next = map_table[n]; 754 map_table[n] = mp; 755 } 756 757 theend: 758 vim_free(keys_buf); 759 vim_free(arg_buf); 760 return retval; 761 } 762 763 /* 764 * Get the mapping mode from the command name. 765 */ 766 static int 767 get_map_mode(char_u **cmdp, int forceit) 768 { 769 char_u *p; 770 int modec; 771 int mode; 772 773 p = *cmdp; 774 modec = *p++; 775 if (modec == 'i') 776 mode = INSERT; // :imap 777 else if (modec == 'l') 778 mode = LANGMAP; // :lmap 779 else if (modec == 'c') 780 mode = CMDLINE; // :cmap 781 else if (modec == 'n' && *p != 'o') // avoid :noremap 782 mode = NORMAL; // :nmap 783 else if (modec == 'v') 784 mode = VISUAL + SELECTMODE; // :vmap 785 else if (modec == 'x') 786 mode = VISUAL; // :xmap 787 else if (modec == 's') 788 mode = SELECTMODE; // :smap 789 else if (modec == 'o') 790 mode = OP_PENDING; // :omap 791 else if (modec == 't') 792 mode = TERMINAL; // :tmap 793 else 794 { 795 --p; 796 if (forceit) 797 mode = INSERT + CMDLINE; // :map ! 798 else 799 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;// :map 800 } 801 802 *cmdp = p; 803 return mode; 804 } 805 806 /* 807 * Clear all mappings or abbreviations. 808 * 'abbr' should be FALSE for mappings, TRUE for abbreviations. 809 */ 810 static void 811 map_clear( 812 char_u *cmdp, 813 char_u *arg UNUSED, 814 int forceit, 815 int abbr) 816 { 817 int mode; 818 int local; 819 820 local = (STRCMP(arg, "<buffer>") == 0); 821 if (!local && *arg != NUL) 822 { 823 emsg(_(e_invarg)); 824 return; 825 } 826 827 mode = get_map_mode(&cmdp, forceit); 828 map_clear_int(curbuf, mode, local, abbr); 829 } 830 831 /* 832 * Clear all mappings in "mode". 833 */ 834 void 835 map_clear_int( 836 buf_T *buf, // buffer for local mappings 837 int mode, // mode in which to delete 838 int local, // TRUE for buffer-local mappings 839 int abbr) // TRUE for abbreviations 840 { 841 mapblock_T *mp, **mpp; 842 int hash; 843 int new_hash; 844 845 validate_maphash(); 846 847 for (hash = 0; hash < 256; ++hash) 848 { 849 if (abbr) 850 { 851 if (hash > 0) // there is only one abbrlist 852 break; 853 if (local) 854 mpp = &buf->b_first_abbr; 855 else 856 mpp = &first_abbr; 857 } 858 else 859 { 860 if (local) 861 mpp = &buf->b_maphash[hash]; 862 else 863 mpp = &maphash[hash]; 864 } 865 while (*mpp != NULL) 866 { 867 mp = *mpp; 868 if (mp->m_mode & mode) 869 { 870 mp->m_mode &= ~mode; 871 if (mp->m_mode == 0) // entry can be deleted 872 { 873 map_free(mpp); 874 continue; 875 } 876 // May need to put this entry into another hash list. 877 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]); 878 if (!abbr && new_hash != hash) 879 { 880 *mpp = mp->m_next; 881 if (local) 882 { 883 mp->m_next = buf->b_maphash[new_hash]; 884 buf->b_maphash[new_hash] = mp; 885 } 886 else 887 { 888 mp->m_next = maphash[new_hash]; 889 maphash[new_hash] = mp; 890 } 891 continue; // continue with *mpp 892 } 893 } 894 mpp = &(mp->m_next); 895 } 896 } 897 } 898 899 #if defined(FEAT_EVAL) || defined(PROTO) 900 /* 901 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars". 902 * Recognize termcap codes in "str". 903 * Also checks mappings local to the current buffer. 904 */ 905 int 906 map_to_exists(char_u *str, char_u *modechars, int abbr) 907 { 908 int mode = 0; 909 char_u *rhs; 910 char_u *buf; 911 int retval; 912 913 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE); 914 915 if (vim_strchr(modechars, 'n') != NULL) 916 mode |= NORMAL; 917 if (vim_strchr(modechars, 'v') != NULL) 918 mode |= VISUAL + SELECTMODE; 919 if (vim_strchr(modechars, 'x') != NULL) 920 mode |= VISUAL; 921 if (vim_strchr(modechars, 's') != NULL) 922 mode |= SELECTMODE; 923 if (vim_strchr(modechars, 'o') != NULL) 924 mode |= OP_PENDING; 925 if (vim_strchr(modechars, 'i') != NULL) 926 mode |= INSERT; 927 if (vim_strchr(modechars, 'l') != NULL) 928 mode |= LANGMAP; 929 if (vim_strchr(modechars, 'c') != NULL) 930 mode |= CMDLINE; 931 932 retval = map_to_exists_mode(rhs, mode, abbr); 933 vim_free(buf); 934 935 return retval; 936 } 937 #endif 938 939 /* 940 * Return TRUE if a map exists that has "str" in the rhs for mode "mode". 941 * Also checks mappings local to the current buffer. 942 */ 943 int 944 map_to_exists_mode(char_u *rhs, int mode, int abbr) 945 { 946 mapblock_T *mp; 947 int hash; 948 int exp_buffer = FALSE; 949 950 validate_maphash(); 951 952 // Do it twice: once for global maps and once for local maps. 953 for (;;) 954 { 955 for (hash = 0; hash < 256; ++hash) 956 { 957 if (abbr) 958 { 959 if (hash > 0) // there is only one abbr list 960 break; 961 if (exp_buffer) 962 mp = curbuf->b_first_abbr; 963 else 964 mp = first_abbr; 965 } 966 else if (exp_buffer) 967 mp = curbuf->b_maphash[hash]; 968 else 969 mp = maphash[hash]; 970 for (; mp; mp = mp->m_next) 971 { 972 if ((mp->m_mode & mode) 973 && strstr((char *)mp->m_str, (char *)rhs) != NULL) 974 return TRUE; 975 } 976 } 977 if (exp_buffer) 978 break; 979 exp_buffer = TRUE; 980 } 981 982 return FALSE; 983 } 984 985 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 986 /* 987 * Used below when expanding mapping/abbreviation names. 988 */ 989 static int expand_mapmodes = 0; 990 static int expand_isabbrev = 0; 991 static int expand_buffer = FALSE; 992 993 /* 994 * Work out what to complete when doing command line completion of mapping 995 * or abbreviation names. 996 */ 997 char_u * 998 set_context_in_map_cmd( 999 expand_T *xp, 1000 char_u *cmd, 1001 char_u *arg, 1002 int forceit, // TRUE if '!' given 1003 int isabbrev, // TRUE if abbreviation 1004 int isunmap, // TRUE if unmap/unabbrev command 1005 cmdidx_T cmdidx) 1006 { 1007 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap) 1008 xp->xp_context = EXPAND_NOTHING; 1009 else 1010 { 1011 if (isunmap) 1012 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev); 1013 else 1014 { 1015 expand_mapmodes = INSERT + CMDLINE; 1016 if (!isabbrev) 1017 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING; 1018 } 1019 expand_isabbrev = isabbrev; 1020 xp->xp_context = EXPAND_MAPPINGS; 1021 expand_buffer = FALSE; 1022 for (;;) 1023 { 1024 if (STRNCMP(arg, "<buffer>", 8) == 0) 1025 { 1026 expand_buffer = TRUE; 1027 arg = skipwhite(arg + 8); 1028 continue; 1029 } 1030 if (STRNCMP(arg, "<unique>", 8) == 0) 1031 { 1032 arg = skipwhite(arg + 8); 1033 continue; 1034 } 1035 if (STRNCMP(arg, "<nowait>", 8) == 0) 1036 { 1037 arg = skipwhite(arg + 8); 1038 continue; 1039 } 1040 if (STRNCMP(arg, "<silent>", 8) == 0) 1041 { 1042 arg = skipwhite(arg + 8); 1043 continue; 1044 } 1045 if (STRNCMP(arg, "<special>", 9) == 0) 1046 { 1047 arg = skipwhite(arg + 9); 1048 continue; 1049 } 1050 #ifdef FEAT_EVAL 1051 if (STRNCMP(arg, "<script>", 8) == 0) 1052 { 1053 arg = skipwhite(arg + 8); 1054 continue; 1055 } 1056 if (STRNCMP(arg, "<expr>", 6) == 0) 1057 { 1058 arg = skipwhite(arg + 6); 1059 continue; 1060 } 1061 #endif 1062 break; 1063 } 1064 xp->xp_pattern = arg; 1065 } 1066 1067 return NULL; 1068 } 1069 1070 /* 1071 * Find all mapping/abbreviation names that match regexp "regmatch"'. 1072 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes. 1073 * Return OK if matches found, FAIL otherwise. 1074 */ 1075 int 1076 ExpandMappings( 1077 regmatch_T *regmatch, 1078 int *num_file, 1079 char_u ***file) 1080 { 1081 mapblock_T *mp; 1082 int hash; 1083 int count; 1084 int round; 1085 char_u *p; 1086 int i; 1087 1088 validate_maphash(); 1089 1090 *num_file = 0; // return values in case of FAIL 1091 *file = NULL; 1092 1093 // round == 1: Count the matches. 1094 // round == 2: Build the array to keep the matches. 1095 for (round = 1; round <= 2; ++round) 1096 { 1097 count = 0; 1098 1099 for (i = 0; i < 7; ++i) 1100 { 1101 if (i == 0) 1102 p = (char_u *)"<silent>"; 1103 else if (i == 1) 1104 p = (char_u *)"<unique>"; 1105 #ifdef FEAT_EVAL 1106 else if (i == 2) 1107 p = (char_u *)"<script>"; 1108 else if (i == 3) 1109 p = (char_u *)"<expr>"; 1110 #endif 1111 else if (i == 4 && !expand_buffer) 1112 p = (char_u *)"<buffer>"; 1113 else if (i == 5) 1114 p = (char_u *)"<nowait>"; 1115 else if (i == 6) 1116 p = (char_u *)"<special>"; 1117 else 1118 continue; 1119 1120 if (vim_regexec(regmatch, p, (colnr_T)0)) 1121 { 1122 if (round == 1) 1123 ++count; 1124 else 1125 (*file)[count++] = vim_strsave(p); 1126 } 1127 } 1128 1129 for (hash = 0; hash < 256; ++hash) 1130 { 1131 if (expand_isabbrev) 1132 { 1133 if (hash > 0) // only one abbrev list 1134 break; // for (hash) 1135 mp = first_abbr; 1136 } 1137 else if (expand_buffer) 1138 mp = curbuf->b_maphash[hash]; 1139 else 1140 mp = maphash[hash]; 1141 for (; mp; mp = mp->m_next) 1142 { 1143 if (mp->m_mode & expand_mapmodes) 1144 { 1145 p = translate_mapping(mp->m_keys); 1146 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0)) 1147 { 1148 if (round == 1) 1149 ++count; 1150 else 1151 { 1152 (*file)[count++] = p; 1153 p = NULL; 1154 } 1155 } 1156 vim_free(p); 1157 } 1158 } // for (mp) 1159 } // for (hash) 1160 1161 if (count == 0) // no match found 1162 break; // for (round) 1163 1164 if (round == 1) 1165 { 1166 *file = ALLOC_MULT(char_u *, count); 1167 if (*file == NULL) 1168 return FAIL; 1169 } 1170 } // for (round) 1171 1172 if (count > 1) 1173 { 1174 char_u **ptr1; 1175 char_u **ptr2; 1176 char_u **ptr3; 1177 1178 // Sort the matches 1179 sort_strings(*file, count); 1180 1181 // Remove multiple entries 1182 ptr1 = *file; 1183 ptr2 = ptr1 + 1; 1184 ptr3 = ptr1 + count; 1185 1186 while (ptr2 < ptr3) 1187 { 1188 if (STRCMP(*ptr1, *ptr2)) 1189 *++ptr1 = *ptr2++; 1190 else 1191 { 1192 vim_free(*ptr2++); 1193 count--; 1194 } 1195 } 1196 } 1197 1198 *num_file = count; 1199 return (count == 0 ? FAIL : OK); 1200 } 1201 #endif // FEAT_CMDL_COMPL 1202 1203 /* 1204 * Check for an abbreviation. 1205 * Cursor is at ptr[col]. 1206 * When inserting, mincol is where insert started. 1207 * For the command line, mincol is what is to be skipped over. 1208 * "c" is the character typed before check_abbr was called. It may have 1209 * ABBR_OFF added to avoid prepending a CTRL-V to it. 1210 * 1211 * Historic vi practice: The last character of an abbreviation must be an id 1212 * character ([a-zA-Z0-9_]). The characters in front of it must be all id 1213 * characters or all non-id characters. This allows for abbr. "#i" to 1214 * "#include". 1215 * 1216 * Vim addition: Allow for abbreviations that end in a non-keyword character. 1217 * Then there must be white space before the abbr. 1218 * 1219 * return TRUE if there is an abbreviation, FALSE if not 1220 */ 1221 int 1222 check_abbr( 1223 int c, 1224 char_u *ptr, 1225 int col, 1226 int mincol) 1227 { 1228 int len; 1229 int scol; // starting column of the abbr. 1230 int j; 1231 char_u *s; 1232 char_u tb[MB_MAXBYTES + 4]; 1233 mapblock_T *mp; 1234 mapblock_T *mp2; 1235 int clen = 0; // length in characters 1236 int is_id = TRUE; 1237 int vim_abbr; 1238 1239 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive 1240 return FALSE; 1241 1242 // no remapping implies no abbreviation, except for CTRL-] 1243 if (noremap_keys() && c != Ctrl_RSB) 1244 return FALSE; 1245 1246 // Check for word before the cursor: If it ends in a keyword char all 1247 // chars before it must be keyword chars or non-keyword chars, but not 1248 // white space. If it ends in a non-keyword char we accept any characters 1249 // before it except white space. 1250 if (col == 0) // cannot be an abbr. 1251 return FALSE; 1252 1253 if (has_mbyte) 1254 { 1255 char_u *p; 1256 1257 p = mb_prevptr(ptr, ptr + col); 1258 if (!vim_iswordp(p)) 1259 vim_abbr = TRUE; // Vim added abbr. 1260 else 1261 { 1262 vim_abbr = FALSE; // vi compatible abbr. 1263 if (p > ptr) 1264 is_id = vim_iswordp(mb_prevptr(ptr, p)); 1265 } 1266 clen = 1; 1267 while (p > ptr + mincol) 1268 { 1269 p = mb_prevptr(ptr, p); 1270 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p))) 1271 { 1272 p += (*mb_ptr2len)(p); 1273 break; 1274 } 1275 ++clen; 1276 } 1277 scol = (int)(p - ptr); 1278 } 1279 else 1280 { 1281 if (!vim_iswordc(ptr[col - 1])) 1282 vim_abbr = TRUE; // Vim added abbr. 1283 else 1284 { 1285 vim_abbr = FALSE; // vi compatible abbr. 1286 if (col > 1) 1287 is_id = vim_iswordc(ptr[col - 2]); 1288 } 1289 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1]) 1290 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol) 1291 ; 1292 } 1293 1294 if (scol < mincol) 1295 scol = mincol; 1296 if (scol < col) // there is a word in front of the cursor 1297 { 1298 ptr += scol; 1299 len = col - scol; 1300 mp = curbuf->b_first_abbr; 1301 mp2 = first_abbr; 1302 if (mp == NULL) 1303 { 1304 mp = mp2; 1305 mp2 = NULL; 1306 } 1307 for ( ; mp; mp->m_next == NULL 1308 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next)) 1309 { 1310 int qlen = mp->m_keylen; 1311 char_u *q = mp->m_keys; 1312 int match; 1313 1314 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL) 1315 { 1316 char_u *qe = vim_strsave(mp->m_keys); 1317 1318 // might have CSI escaped mp->m_keys 1319 if (qe != NULL) 1320 { 1321 q = qe; 1322 vim_unescape_csi(q); 1323 qlen = (int)STRLEN(q); 1324 } 1325 } 1326 1327 // find entries with right mode and keys 1328 match = (mp->m_mode & State) 1329 && qlen == len 1330 && !STRNCMP(q, ptr, (size_t)len); 1331 if (q != mp->m_keys) 1332 vim_free(q); 1333 if (match) 1334 break; 1335 } 1336 if (mp != NULL) 1337 { 1338 // Found a match: 1339 // Insert the rest of the abbreviation in typebuf.tb_buf[]. 1340 // This goes from end to start. 1341 // 1342 // Characters 0x000 - 0x100: normal chars, may need CTRL-V, 1343 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER 1344 // Characters where IS_SPECIAL() == TRUE: key codes, need 1345 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V. 1346 // 1347 // Character CTRL-] is treated specially - it completes the 1348 // abbreviation, but is not inserted into the input stream. 1349 j = 0; 1350 if (c != Ctrl_RSB) 1351 { 1352 // special key code, split up 1353 if (IS_SPECIAL(c) || c == K_SPECIAL) 1354 { 1355 tb[j++] = K_SPECIAL; 1356 tb[j++] = K_SECOND(c); 1357 tb[j++] = K_THIRD(c); 1358 } 1359 else 1360 { 1361 if (c < ABBR_OFF && (c < ' ' || c > '~')) 1362 tb[j++] = Ctrl_V; // special char needs CTRL-V 1363 if (has_mbyte) 1364 { 1365 // if ABBR_OFF has been added, remove it here 1366 if (c >= ABBR_OFF) 1367 c -= ABBR_OFF; 1368 j += (*mb_char2bytes)(c, tb + j); 1369 } 1370 else 1371 tb[j++] = c; 1372 } 1373 tb[j] = NUL; 1374 // insert the last typed char 1375 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent); 1376 } 1377 #ifdef FEAT_EVAL 1378 if (mp->m_expr) 1379 s = eval_map_expr(mp->m_str, c); 1380 else 1381 #endif 1382 s = mp->m_str; 1383 if (s != NULL) 1384 { 1385 // insert the to string 1386 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent); 1387 // no abbrev. for these chars 1388 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1; 1389 #ifdef FEAT_EVAL 1390 if (mp->m_expr) 1391 vim_free(s); 1392 #endif 1393 } 1394 1395 tb[0] = Ctrl_H; 1396 tb[1] = NUL; 1397 if (has_mbyte) 1398 len = clen; // Delete characters instead of bytes 1399 while (len-- > 0) // delete the from string 1400 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent); 1401 return TRUE; 1402 } 1403 } 1404 return FALSE; 1405 } 1406 1407 #ifdef FEAT_EVAL 1408 /* 1409 * Evaluate the RHS of a mapping or abbreviations and take care of escaping 1410 * special characters. 1411 */ 1412 char_u * 1413 eval_map_expr( 1414 char_u *str, 1415 int c) // NUL or typed character for abbreviation 1416 { 1417 char_u *res; 1418 char_u *p; 1419 char_u *expr; 1420 pos_T save_cursor; 1421 int save_msg_col; 1422 int save_msg_row; 1423 1424 // Remove escaping of CSI, because "str" is in a format to be used as 1425 // typeahead. 1426 expr = vim_strsave(str); 1427 if (expr == NULL) 1428 return NULL; 1429 vim_unescape_csi(expr); 1430 1431 // Forbid changing text or using ":normal" to avoid most of the bad side 1432 // effects. Also restore the cursor position. 1433 ++textlock; 1434 ++ex_normal_lock; 1435 set_vim_var_char(c); // set v:char to the typed character 1436 save_cursor = curwin->w_cursor; 1437 save_msg_col = msg_col; 1438 save_msg_row = msg_row; 1439 p = eval_to_string(expr, NULL, FALSE); 1440 --textlock; 1441 --ex_normal_lock; 1442 curwin->w_cursor = save_cursor; 1443 msg_col = save_msg_col; 1444 msg_row = save_msg_row; 1445 1446 vim_free(expr); 1447 1448 if (p == NULL) 1449 return NULL; 1450 // Escape CSI in the result to be able to use the string as typeahead. 1451 res = vim_strsave_escape_csi(p); 1452 vim_free(p); 1453 1454 return res; 1455 } 1456 #endif 1457 1458 /* 1459 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result 1460 * can be put in the typeahead buffer. 1461 * Returns NULL when out of memory. 1462 */ 1463 char_u * 1464 vim_strsave_escape_csi( 1465 char_u *p) 1466 { 1467 char_u *res; 1468 char_u *s, *d; 1469 1470 // Need a buffer to hold up to three times as much. Four in case of an 1471 // illegal utf-8 byte: 1472 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER 1473 res = alloc(STRLEN(p) * 4 + 1); 1474 if (res != NULL) 1475 { 1476 d = res; 1477 for (s = p; *s != NUL; ) 1478 { 1479 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL) 1480 { 1481 // Copy special key unmodified. 1482 *d++ = *s++; 1483 *d++ = *s++; 1484 *d++ = *s++; 1485 } 1486 else 1487 { 1488 // Add character, possibly multi-byte to destination, escaping 1489 // CSI and K_SPECIAL. Be careful, it can be an illegal byte! 1490 d = add_char2buf(PTR2CHAR(s), d); 1491 s += MB_CPTR2LEN(s); 1492 } 1493 } 1494 *d = NUL; 1495 } 1496 return res; 1497 } 1498 1499 /* 1500 * Remove escaping from CSI and K_SPECIAL characters. Reverse of 1501 * vim_strsave_escape_csi(). Works in-place. 1502 */ 1503 void 1504 vim_unescape_csi(char_u *p) 1505 { 1506 char_u *s = p, *d = p; 1507 1508 while (*s != NUL) 1509 { 1510 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER) 1511 { 1512 *d++ = K_SPECIAL; 1513 s += 3; 1514 } 1515 else if ((s[0] == K_SPECIAL || s[0] == CSI) 1516 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI) 1517 { 1518 *d++ = CSI; 1519 s += 3; 1520 } 1521 else 1522 *d++ = *s++; 1523 } 1524 *d = NUL; 1525 } 1526 1527 /* 1528 * Write map commands for the current mappings to an .exrc file. 1529 * Return FAIL on error, OK otherwise. 1530 */ 1531 int 1532 makemap( 1533 FILE *fd, 1534 buf_T *buf) // buffer for local mappings or NULL 1535 { 1536 mapblock_T *mp; 1537 char_u c1, c2, c3; 1538 char_u *p; 1539 char *cmd; 1540 int abbr; 1541 int hash; 1542 int did_cpo = FALSE; 1543 int i; 1544 1545 validate_maphash(); 1546 1547 // Do the loop twice: Once for mappings, once for abbreviations. 1548 // Then loop over all map hash lists. 1549 for (abbr = 0; abbr < 2; ++abbr) 1550 for (hash = 0; hash < 256; ++hash) 1551 { 1552 if (abbr) 1553 { 1554 if (hash > 0) // there is only one abbr list 1555 break; 1556 if (buf != NULL) 1557 mp = buf->b_first_abbr; 1558 else 1559 mp = first_abbr; 1560 } 1561 else 1562 { 1563 if (buf != NULL) 1564 mp = buf->b_maphash[hash]; 1565 else 1566 mp = maphash[hash]; 1567 } 1568 1569 for ( ; mp; mp = mp->m_next) 1570 { 1571 // skip script-local mappings 1572 if (mp->m_noremap == REMAP_SCRIPT) 1573 continue; 1574 1575 // skip mappings that contain a <SNR> (script-local thing), 1576 // they probably don't work when loaded again 1577 for (p = mp->m_str; *p != NUL; ++p) 1578 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA 1579 && p[2] == (int)KE_SNR) 1580 break; 1581 if (*p != NUL) 1582 continue; 1583 1584 // It's possible to create a mapping and then ":unmap" certain 1585 // modes. We recreate this here by mapping the individual 1586 // modes, which requires up to three of them. 1587 c1 = NUL; 1588 c2 = NUL; 1589 c3 = NUL; 1590 if (abbr) 1591 cmd = "abbr"; 1592 else 1593 cmd = "map"; 1594 switch (mp->m_mode) 1595 { 1596 case NORMAL + VISUAL + SELECTMODE + OP_PENDING: 1597 break; 1598 case NORMAL: 1599 c1 = 'n'; 1600 break; 1601 case VISUAL: 1602 c1 = 'x'; 1603 break; 1604 case SELECTMODE: 1605 c1 = 's'; 1606 break; 1607 case OP_PENDING: 1608 c1 = 'o'; 1609 break; 1610 case NORMAL + VISUAL: 1611 c1 = 'n'; 1612 c2 = 'x'; 1613 break; 1614 case NORMAL + SELECTMODE: 1615 c1 = 'n'; 1616 c2 = 's'; 1617 break; 1618 case NORMAL + OP_PENDING: 1619 c1 = 'n'; 1620 c2 = 'o'; 1621 break; 1622 case VISUAL + SELECTMODE: 1623 c1 = 'v'; 1624 break; 1625 case VISUAL + OP_PENDING: 1626 c1 = 'x'; 1627 c2 = 'o'; 1628 break; 1629 case SELECTMODE + OP_PENDING: 1630 c1 = 's'; 1631 c2 = 'o'; 1632 break; 1633 case NORMAL + VISUAL + SELECTMODE: 1634 c1 = 'n'; 1635 c2 = 'v'; 1636 break; 1637 case NORMAL + VISUAL + OP_PENDING: 1638 c1 = 'n'; 1639 c2 = 'x'; 1640 c3 = 'o'; 1641 break; 1642 case NORMAL + SELECTMODE + OP_PENDING: 1643 c1 = 'n'; 1644 c2 = 's'; 1645 c3 = 'o'; 1646 break; 1647 case VISUAL + SELECTMODE + OP_PENDING: 1648 c1 = 'v'; 1649 c2 = 'o'; 1650 break; 1651 case CMDLINE + INSERT: 1652 if (!abbr) 1653 cmd = "map!"; 1654 break; 1655 case CMDLINE: 1656 c1 = 'c'; 1657 break; 1658 case INSERT: 1659 c1 = 'i'; 1660 break; 1661 case LANGMAP: 1662 c1 = 'l'; 1663 break; 1664 case TERMINAL: 1665 c1 = 't'; 1666 break; 1667 default: 1668 iemsg(_("E228: makemap: Illegal mode")); 1669 return FAIL; 1670 } 1671 do // do this twice if c2 is set, 3 times with c3 1672 { 1673 // When outputting <> form, need to make sure that 'cpo' 1674 // is set to the Vim default. 1675 if (!did_cpo) 1676 { 1677 if (*mp->m_str == NUL) // will use <Nop> 1678 did_cpo = TRUE; 1679 else 1680 for (i = 0; i < 2; ++i) 1681 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p) 1682 if (*p == K_SPECIAL || *p == NL) 1683 did_cpo = TRUE; 1684 if (did_cpo) 1685 { 1686 if (fprintf(fd, "let s:cpo_save=&cpo") < 0 1687 || put_eol(fd) < 0 1688 || fprintf(fd, "set cpo&vim") < 0 1689 || put_eol(fd) < 0) 1690 return FAIL; 1691 } 1692 } 1693 if (c1 && putc(c1, fd) < 0) 1694 return FAIL; 1695 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0) 1696 return FAIL; 1697 if (fputs(cmd, fd) < 0) 1698 return FAIL; 1699 if (buf != NULL && fputs(" <buffer>", fd) < 0) 1700 return FAIL; 1701 if (mp->m_nowait && fputs(" <nowait>", fd) < 0) 1702 return FAIL; 1703 if (mp->m_silent && fputs(" <silent>", fd) < 0) 1704 return FAIL; 1705 #ifdef FEAT_EVAL 1706 if (mp->m_noremap == REMAP_SCRIPT 1707 && fputs("<script>", fd) < 0) 1708 return FAIL; 1709 if (mp->m_expr && fputs(" <expr>", fd) < 0) 1710 return FAIL; 1711 #endif 1712 1713 if ( putc(' ', fd) < 0 1714 || put_escstr(fd, mp->m_keys, 0) == FAIL 1715 || putc(' ', fd) < 0 1716 || put_escstr(fd, mp->m_str, 1) == FAIL 1717 || put_eol(fd) < 0) 1718 return FAIL; 1719 c1 = c2; 1720 c2 = c3; 1721 c3 = NUL; 1722 } while (c1 != NUL); 1723 } 1724 } 1725 1726 if (did_cpo) 1727 if (fprintf(fd, "let &cpo=s:cpo_save") < 0 1728 || put_eol(fd) < 0 1729 || fprintf(fd, "unlet s:cpo_save") < 0 1730 || put_eol(fd) < 0) 1731 return FAIL; 1732 return OK; 1733 } 1734 1735 /* 1736 * write escape string to file 1737 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set 1738 * 1739 * return FAIL for failure, OK otherwise 1740 */ 1741 int 1742 put_escstr(FILE *fd, char_u *strstart, int what) 1743 { 1744 char_u *str = strstart; 1745 int c; 1746 int modifiers; 1747 1748 // :map xx <Nop> 1749 if (*str == NUL && what == 1) 1750 { 1751 if (fprintf(fd, "<Nop>") < 0) 1752 return FAIL; 1753 return OK; 1754 } 1755 1756 for ( ; *str != NUL; ++str) 1757 { 1758 char_u *p; 1759 1760 // Check for a multi-byte character, which may contain escaped 1761 // K_SPECIAL and CSI bytes 1762 p = mb_unescape(&str); 1763 if (p != NULL) 1764 { 1765 while (*p != NUL) 1766 if (fputc(*p++, fd) < 0) 1767 return FAIL; 1768 --str; 1769 continue; 1770 } 1771 1772 c = *str; 1773 // Special key codes have to be translated to be able to make sense 1774 // when they are read back. 1775 if (c == K_SPECIAL && what != 2) 1776 { 1777 modifiers = 0x0; 1778 if (str[1] == KS_MODIFIER) 1779 { 1780 modifiers = str[2]; 1781 str += 3; 1782 c = *str; 1783 } 1784 if (c == K_SPECIAL) 1785 { 1786 c = TO_SPECIAL(str[1], str[2]); 1787 str += 2; 1788 } 1789 if (IS_SPECIAL(c) || modifiers) // special key 1790 { 1791 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0) 1792 return FAIL; 1793 continue; 1794 } 1795 } 1796 1797 // A '\n' in a map command should be written as <NL>. 1798 // A '\n' in a set command should be written as \^V^J. 1799 if (c == NL) 1800 { 1801 if (what == 2) 1802 { 1803 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0) 1804 return FAIL; 1805 } 1806 else 1807 { 1808 if (fprintf(fd, "<NL>") < 0) 1809 return FAIL; 1810 } 1811 continue; 1812 } 1813 1814 // Some characters have to be escaped with CTRL-V to 1815 // prevent them from misinterpreted in DoOneCmd(). 1816 // A space, Tab and '"' has to be escaped with a backslash to 1817 // prevent it to be misinterpreted in do_set(). 1818 // A space has to be escaped with a CTRL-V when it's at the start of a 1819 // ":map" rhs. 1820 // A '<' has to be escaped with a CTRL-V to prevent it being 1821 // interpreted as the start of a special key name. 1822 // A space in the lhs of a :map needs a CTRL-V. 1823 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\')) 1824 { 1825 if (putc('\\', fd) < 0) 1826 return FAIL; 1827 } 1828 else if (c < ' ' || c > '~' || c == '|' 1829 || (what == 0 && c == ' ') 1830 || (what == 1 && str == strstart && c == ' ') 1831 || (what != 2 && c == '<')) 1832 { 1833 if (putc(Ctrl_V, fd) < 0) 1834 return FAIL; 1835 } 1836 if (putc(c, fd) < 0) 1837 return FAIL; 1838 } 1839 return OK; 1840 } 1841 1842 /* 1843 * Check all mappings for the presence of special key codes. 1844 * Used after ":set term=xxx". 1845 */ 1846 void 1847 check_map_keycodes(void) 1848 { 1849 mapblock_T *mp; 1850 char_u *p; 1851 int i; 1852 char_u buf[3]; 1853 char_u *save_name; 1854 int abbr; 1855 int hash; 1856 buf_T *bp; 1857 1858 validate_maphash(); 1859 save_name = sourcing_name; 1860 sourcing_name = (char_u *)"mappings"; // avoids giving error messages 1861 1862 // This this once for each buffer, and then once for global 1863 // mappings/abbreviations with bp == NULL 1864 for (bp = firstbuf; ; bp = bp->b_next) 1865 { 1866 // Do the loop twice: Once for mappings, once for abbreviations. 1867 // Then loop over all map hash lists. 1868 for (abbr = 0; abbr <= 1; ++abbr) 1869 for (hash = 0; hash < 256; ++hash) 1870 { 1871 if (abbr) 1872 { 1873 if (hash) // there is only one abbr list 1874 break; 1875 if (bp != NULL) 1876 mp = bp->b_first_abbr; 1877 else 1878 mp = first_abbr; 1879 } 1880 else 1881 { 1882 if (bp != NULL) 1883 mp = bp->b_maphash[hash]; 1884 else 1885 mp = maphash[hash]; 1886 } 1887 for ( ; mp != NULL; mp = mp->m_next) 1888 { 1889 for (i = 0; i <= 1; ++i) // do this twice 1890 { 1891 if (i == 0) 1892 p = mp->m_keys; // once for the "from" part 1893 else 1894 p = mp->m_str; // and once for the "to" part 1895 while (*p) 1896 { 1897 if (*p == K_SPECIAL) 1898 { 1899 ++p; 1900 if (*p < 128) // for "normal" tcap entries 1901 { 1902 buf[0] = p[0]; 1903 buf[1] = p[1]; 1904 buf[2] = NUL; 1905 (void)add_termcap_entry(buf, FALSE); 1906 } 1907 ++p; 1908 } 1909 ++p; 1910 } 1911 } 1912 } 1913 } 1914 if (bp == NULL) 1915 break; 1916 } 1917 sourcing_name = save_name; 1918 } 1919 1920 #if defined(FEAT_EVAL) || defined(PROTO) 1921 /* 1922 * Check the string "keys" against the lhs of all mappings. 1923 * Return pointer to rhs of mapping (mapblock->m_str). 1924 * NULL when no mapping found. 1925 */ 1926 char_u * 1927 check_map( 1928 char_u *keys, 1929 int mode, 1930 int exact, // require exact match 1931 int ign_mod, // ignore preceding modifier 1932 int abbr, // do abbreviations 1933 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL 1934 int *local_ptr) // return: buffer-local mapping or NULL 1935 { 1936 int hash; 1937 int len, minlen; 1938 mapblock_T *mp; 1939 char_u *s; 1940 int local; 1941 1942 validate_maphash(); 1943 1944 len = (int)STRLEN(keys); 1945 for (local = 1; local >= 0; --local) 1946 // loop over all hash lists 1947 for (hash = 0; hash < 256; ++hash) 1948 { 1949 if (abbr) 1950 { 1951 if (hash > 0) // there is only one list. 1952 break; 1953 if (local) 1954 mp = curbuf->b_first_abbr; 1955 else 1956 mp = first_abbr; 1957 } 1958 else if (local) 1959 mp = curbuf->b_maphash[hash]; 1960 else 1961 mp = maphash[hash]; 1962 for ( ; mp != NULL; mp = mp->m_next) 1963 { 1964 // skip entries with wrong mode, wrong length and not matching 1965 // ones 1966 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len)) 1967 { 1968 if (len > mp->m_keylen) 1969 minlen = mp->m_keylen; 1970 else 1971 minlen = len; 1972 s = mp->m_keys; 1973 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER 1974 && s[2] != NUL) 1975 { 1976 s += 3; 1977 if (len > mp->m_keylen - 3) 1978 minlen = mp->m_keylen - 3; 1979 } 1980 if (STRNCMP(s, keys, minlen) == 0) 1981 { 1982 if (mp_ptr != NULL) 1983 *mp_ptr = mp; 1984 if (local_ptr != NULL) 1985 *local_ptr = local; 1986 return mp->m_str; 1987 } 1988 } 1989 } 1990 } 1991 1992 return NULL; 1993 } 1994 1995 void 1996 get_maparg(typval_T *argvars, typval_T *rettv, int exact) 1997 { 1998 char_u *keys; 1999 char_u *which; 2000 char_u buf[NUMBUFLEN]; 2001 char_u *keys_buf = NULL; 2002 char_u *rhs; 2003 int mode; 2004 int abbr = FALSE; 2005 int get_dict = FALSE; 2006 mapblock_T *mp; 2007 int buffer_local; 2008 2009 // return empty string for failure 2010 rettv->v_type = VAR_STRING; 2011 rettv->vval.v_string = NULL; 2012 2013 keys = tv_get_string(&argvars[0]); 2014 if (*keys == NUL) 2015 return; 2016 2017 if (argvars[1].v_type != VAR_UNKNOWN) 2018 { 2019 which = tv_get_string_buf_chk(&argvars[1], buf); 2020 if (argvars[2].v_type != VAR_UNKNOWN) 2021 { 2022 abbr = (int)tv_get_number(&argvars[2]); 2023 if (argvars[3].v_type != VAR_UNKNOWN) 2024 get_dict = (int)tv_get_number(&argvars[3]); 2025 } 2026 } 2027 else 2028 which = (char_u *)""; 2029 if (which == NULL) 2030 return; 2031 2032 mode = get_map_mode(&which, 0); 2033 2034 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE); 2035 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local); 2036 vim_free(keys_buf); 2037 2038 if (!get_dict) 2039 { 2040 // Return a string. 2041 if (rhs != NULL) 2042 { 2043 if (*rhs == NUL) 2044 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>"); 2045 else 2046 rettv->vval.v_string = str2special_save(rhs, FALSE); 2047 } 2048 2049 } 2050 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL) 2051 { 2052 // Return a dictionary. 2053 char_u *lhs = str2special_save(mp->m_keys, TRUE); 2054 char_u *mapmode = map_mode_to_chars(mp->m_mode); 2055 dict_T *dict = rettv->vval.v_dict; 2056 2057 dict_add_string(dict, "lhs", lhs); 2058 dict_add_string(dict, "rhs", mp->m_orig_str); 2059 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L); 2060 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L); 2061 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L); 2062 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid); 2063 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum); 2064 dict_add_number(dict, "buffer", (long)buffer_local); 2065 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L); 2066 dict_add_string(dict, "mode", mapmode); 2067 2068 vim_free(lhs); 2069 vim_free(mapmode); 2070 } 2071 } 2072 #endif 2073 2074 #if defined(MSWIN) || defined(MACOS_X) 2075 2076 # define VIS_SEL (VISUAL+SELECTMODE) // abbreviation 2077 2078 /* 2079 * Default mappings for some often used keys. 2080 */ 2081 struct initmap 2082 { 2083 char_u *arg; 2084 int mode; 2085 }; 2086 2087 # ifdef FEAT_GUI_MSWIN 2088 // Use the Windows (CUA) keybindings. (GUI) 2089 static struct initmap initmappings[] = 2090 { 2091 // paste, copy and cut 2092 {(char_u *)"<S-Insert> \"*P", NORMAL}, 2093 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL}, 2094 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE}, 2095 {(char_u *)"<C-Insert> \"*y", VIS_SEL}, 2096 {(char_u *)"<S-Del> \"*d", VIS_SEL}, 2097 {(char_u *)"<C-Del> \"*d", VIS_SEL}, 2098 {(char_u *)"<C-X> \"*d", VIS_SEL}, 2099 // Missing: CTRL-C (cancel) and CTRL-V (block selection) 2100 }; 2101 # endif 2102 2103 # if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) 2104 // Use the Windows (CUA) keybindings. (Console) 2105 static struct initmap cinitmappings[] = 2106 { 2107 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL}, 2108 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE}, 2109 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL}, 2110 {(char_u *)"\316u <C-End>", INSERT+CMDLINE}, 2111 2112 // paste, copy and cut 2113 # ifdef FEAT_CLIPBOARD 2114 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P 2115 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P 2116 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O* 2117 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y 2118 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d 2119 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d 2120 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d 2121 # else 2122 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P 2123 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP 2124 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O" 2125 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y 2126 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d 2127 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d 2128 # endif 2129 }; 2130 # endif 2131 2132 # if defined(MACOS_X) 2133 static struct initmap initmappings[] = 2134 { 2135 // Use the Standard MacOS binding. 2136 // paste, copy and cut 2137 {(char_u *)"<D-v> \"*P", NORMAL}, 2138 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL}, 2139 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE}, 2140 {(char_u *)"<D-c> \"*y", VIS_SEL}, 2141 {(char_u *)"<D-x> \"*d", VIS_SEL}, 2142 {(char_u *)"<Backspace> \"-d", VIS_SEL}, 2143 }; 2144 # endif 2145 2146 # undef VIS_SEL 2147 #endif 2148 2149 /* 2150 * Set up default mappings. 2151 */ 2152 void 2153 init_mappings(void) 2154 { 2155 #if defined(MSWIN) || defined(MACOS_X) 2156 int i; 2157 2158 # if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 2159 # ifdef VIMDLL 2160 if (!gui.starting) 2161 # endif 2162 { 2163 for (i = 0; 2164 i < (int)(sizeof(cinitmappings) / sizeof(struct initmap)); ++i) 2165 add_map(cinitmappings[i].arg, cinitmappings[i].mode); 2166 } 2167 # endif 2168 # if defined(FEAT_GUI_MSWIN) || defined(MACOS_X) 2169 for (i = 0; i < (int)(sizeof(initmappings) / sizeof(struct initmap)); ++i) 2170 add_map(initmappings[i].arg, initmappings[i].mode); 2171 # endif 2172 #endif 2173 } 2174 2175 #if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \ 2176 || defined(PROTO) 2177 /* 2178 * Add a mapping "map" for mode "mode". 2179 * Need to put string in allocated memory, because do_map() will modify it. 2180 */ 2181 void 2182 add_map(char_u *map, int mode) 2183 { 2184 char_u *s; 2185 char_u *cpo_save = p_cpo; 2186 2187 p_cpo = (char_u *)""; // Allow <> notation 2188 s = vim_strsave(map); 2189 if (s != NULL) 2190 { 2191 (void)do_map(0, s, mode, FALSE); 2192 vim_free(s); 2193 } 2194 p_cpo = cpo_save; 2195 } 2196 #endif 2197 2198 static void 2199 do_exmap(exarg_T *eap, int isabbrev) 2200 { 2201 int mode; 2202 char_u *cmdp; 2203 2204 cmdp = eap->cmd; 2205 mode = get_map_mode(&cmdp, eap->forceit || isabbrev); 2206 2207 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'), 2208 eap->arg, mode, isabbrev)) 2209 { 2210 case 1: emsg(_(e_invarg)); 2211 break; 2212 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap))); 2213 break; 2214 } 2215 } 2216 2217 /* 2218 * ":abbreviate" and friends. 2219 */ 2220 void 2221 ex_abbreviate(exarg_T *eap) 2222 { 2223 do_exmap(eap, TRUE); // almost the same as mapping 2224 } 2225 2226 /* 2227 * ":map" and friends. 2228 */ 2229 void 2230 ex_map(exarg_T *eap) 2231 { 2232 // If we are sourcing .exrc or .vimrc in current directory we 2233 // print the mappings for security reasons. 2234 if (secure) 2235 { 2236 secure = 2; 2237 msg_outtrans(eap->cmd); 2238 msg_putchar('\n'); 2239 } 2240 do_exmap(eap, FALSE); 2241 } 2242 2243 /* 2244 * ":unmap" and friends. 2245 */ 2246 void 2247 ex_unmap(exarg_T *eap) 2248 { 2249 do_exmap(eap, FALSE); 2250 } 2251 2252 /* 2253 * ":mapclear" and friends. 2254 */ 2255 void 2256 ex_mapclear(exarg_T *eap) 2257 { 2258 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE); 2259 } 2260 2261 /* 2262 * ":abclear" and friends. 2263 */ 2264 void 2265 ex_abclear(exarg_T *eap) 2266 { 2267 map_clear(eap->cmd, eap->arg, TRUE, TRUE); 2268 } 2269