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