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