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