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