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