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