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