blob: ee249c77ae9fce311ffdd55e778e8a947d934c1c [file] [log] [blame]
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001/* 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/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * map.c: Code for mappings and abbreviations.
Bram Moolenaarb66bab32019-08-01 14:28:24 +020012 */
13
14#include "vim.h"
15
16/*
17 * List used for abbreviations.
18 */
19static 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 */
24static mapblock_T *(maphash[256]);
25static int maphash_valid = FALSE;
26
Bram Moolenaarbf533e42022-11-13 20:43:19 +000027// When non-zero then no mappings can be added or removed. Prevents mappings
28// to change while listing them.
29static int map_locked = 0;
30
Bram Moolenaarb66bab32019-08-01 14:28:24 +020031/*
32 * Make a hash value for a mapping.
33 * "mode" is the lower 4 bits of the State for the mapping.
34 * "c1" is the first character of the "lhs".
35 * Returns a value between 0 and 255, index in maphash.
36 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode.
37 */
Bram Moolenaar24959102022-05-07 20:01:16 +010038#define MAP_HASH(mode, c1) (((mode) & (MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING | MODE_TERMINAL)) ? (c1) : ((c1) ^ 0x80))
Bram Moolenaarb66bab32019-08-01 14:28:24 +020039
40/*
41 * Get the start of the hashed map list for "state" and first character "c".
42 */
43 mapblock_T *
44get_maphash_list(int state, int c)
45{
46 return maphash[MAP_HASH(state, c)];
47}
48
49/*
50 * Get the buffer-local hashed map list for "state" and first character "c".
51 */
52 mapblock_T *
53get_buf_maphash_list(int state, int c)
54{
55 return curbuf->b_maphash[MAP_HASH(state, c)];
56}
57
58 int
59is_maphash_valid(void)
60{
61 return maphash_valid;
62}
63
64/*
65 * Initialize maphash[] for first use.
66 */
67 static void
68validate_maphash(void)
69{
70 if (!maphash_valid)
71 {
Bram Moolenaara80faa82020-04-12 19:37:17 +020072 CLEAR_FIELD(maphash);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020073 maphash_valid = TRUE;
74 }
75}
76
77/*
78 * Delete one entry from the abbrlist or maphash[].
79 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
80 */
81 static void
82map_free(mapblock_T **mpp)
83{
84 mapblock_T *mp;
85
86 mp = *mpp;
87 vim_free(mp->m_keys);
88 vim_free(mp->m_str);
89 vim_free(mp->m_orig_str);
90 *mpp = mp->m_next;
Bram Moolenaard648c012022-01-16 14:58:34 +000091#ifdef FEAT_EVAL
Bram Moolenaarf61c89d2022-01-19 22:51:48 +000092 reset_last_used_map(mp);
Bram Moolenaard648c012022-01-16 14:58:34 +000093#endif
Bram Moolenaar8aa0e6c2022-01-20 11:27:58 +000094 vim_free(mp);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020095}
96
97/*
98 * Return characters to represent the map mode in an allocated string.
99 * Returns NULL when out of memory.
100 */
101 static char_u *
102map_mode_to_chars(int mode)
103{
104 garray_T mapmode;
105
106 ga_init2(&mapmode, 1, 7);
107
Bram Moolenaar24959102022-05-07 20:01:16 +0100108 if ((mode & (MODE_INSERT | MODE_CMDLINE)) == (MODE_INSERT | MODE_CMDLINE))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200109 ga_append(&mapmode, '!'); // :map!
Bram Moolenaar24959102022-05-07 20:01:16 +0100110 else if (mode & MODE_INSERT)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200111 ga_append(&mapmode, 'i'); // :imap
Bram Moolenaar24959102022-05-07 20:01:16 +0100112 else if (mode & MODE_LANGMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200113 ga_append(&mapmode, 'l'); // :lmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100114 else if (mode & MODE_CMDLINE)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200115 ga_append(&mapmode, 'c'); // :cmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100116 else if ((mode
117 & (MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING))
118 == (MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200119 ga_append(&mapmode, ' '); // :map
120 else
121 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100122 if (mode & MODE_NORMAL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200123 ga_append(&mapmode, 'n'); // :nmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100124 if (mode & MODE_OP_PENDING)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200125 ga_append(&mapmode, 'o'); // :omap
Bram Moolenaar24959102022-05-07 20:01:16 +0100126 if (mode & MODE_TERMINAL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200127 ga_append(&mapmode, 't'); // :tmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100128 if ((mode & (MODE_VISUAL | MODE_SELECT)) == (MODE_VISUAL | MODE_SELECT))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200129 ga_append(&mapmode, 'v'); // :vmap
130 else
131 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100132 if (mode & MODE_VISUAL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200133 ga_append(&mapmode, 'x'); // :xmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100134 if (mode & MODE_SELECT)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200135 ga_append(&mapmode, 's'); // :smap
136 }
137 }
138
139 ga_append(&mapmode, NUL);
140 return (char_u *)mapmode.ga_data;
141}
142
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100143/*
144 * Output a line for one mapping.
145 */
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200146 static void
147showmap(
148 mapblock_T *mp,
149 int local) // TRUE for buffer-local map
150{
151 int len = 1;
152 char_u *mapchars;
153
154 if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
155 return;
156
Bram Moolenaarbf533e42022-11-13 20:43:19 +0000157 // Prevent mappings to be cleared while at the more prompt.
158 // Must jump to "theend" instead of returning.
159 ++map_locked;
160
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200161 if (msg_didout || msg_silent != 0)
162 {
163 msg_putchar('\n');
164 if (got_int) // 'q' typed at MORE prompt
Bram Moolenaarbf533e42022-11-13 20:43:19 +0000165 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200166 }
167
168 mapchars = map_mode_to_chars(mp->m_mode);
169 if (mapchars != NULL)
170 {
171 msg_puts((char *)mapchars);
172 len = (int)STRLEN(mapchars);
173 vim_free(mapchars);
174 }
175
176 while (++len <= 3)
177 msg_putchar(' ');
178
179 // Display the LHS. Get length of what we write.
180 len = msg_outtrans_special(mp->m_keys, TRUE, 0);
181 do
182 {
183 msg_putchar(' '); // padd with blanks
184 ++len;
185 } while (len < 12);
186
187 if (mp->m_noremap == REMAP_NONE)
188 msg_puts_attr("*", HL_ATTR(HLF_8));
189 else if (mp->m_noremap == REMAP_SCRIPT)
190 msg_puts_attr("&", HL_ATTR(HLF_8));
191 else
192 msg_putchar(' ');
193
194 if (local)
195 msg_putchar('@');
196 else
197 msg_putchar(' ');
198
199 // Use FALSE below if we only want things like <Up> to show up as such on
200 // the rhs, and not M-x etc, TRUE gets both -- webb
201 if (*mp->m_str == NUL)
202 msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
203 else
zeertzjqac402f42022-05-04 18:51:43 +0100204 msg_outtrans_special(mp->m_str, FALSE, 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200205#ifdef FEAT_EVAL
206 if (p_verbose > 0)
207 last_set_msg(mp->m_script_ctx);
208#endif
Bram Moolenaard288eaa2022-02-16 18:27:55 +0000209 msg_clr_eos();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200210 out_flush(); // show one line at a time
Bram Moolenaarbf533e42022-11-13 20:43:19 +0000211
212theend:
213 --map_locked;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200214}
215
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200216 static int
217map_add(
218 mapblock_T **map_table,
219 mapblock_T **abbr_table,
220 char_u *keys,
221 char_u *rhs,
222 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200223 int noremap,
224 int nowait,
225 int silent,
226 int mode,
227 int is_abbr,
228#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200229 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200230 scid_T sid, // -1 to use current_sctx
Bram Moolenaara9528b32022-01-18 20:51:35 +0000231 int scriptversion,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200232 linenr_T lnum,
233#endif
234 int simplified)
235{
Bram Moolenaar94075b22022-01-18 20:30:39 +0000236 mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200237
238 if (mp == NULL)
239 return FAIL;
240
241 // If CTRL-C has been mapped, don't always use it for Interrupting.
242 if (*keys == Ctrl_C)
243 {
244 if (map_table == curbuf->b_maphash)
245 curbuf->b_mapped_ctrl_c |= mode;
246 else
247 mapped_ctrl_c |= mode;
248 }
249
250 mp->m_keys = vim_strsave(keys);
251 mp->m_str = vim_strsave(rhs);
252 mp->m_orig_str = vim_strsave(orig_rhs);
253 if (mp->m_keys == NULL || mp->m_str == NULL)
254 {
255 vim_free(mp->m_keys);
256 vim_free(mp->m_str);
257 vim_free(mp->m_orig_str);
258 vim_free(mp);
259 return FAIL;
260 }
261 mp->m_keylen = (int)STRLEN(mp->m_keys);
262 mp->m_noremap = noremap;
263 mp->m_nowait = nowait;
264 mp->m_silent = silent;
265 mp->m_mode = mode;
266 mp->m_simplified = simplified;
267#ifdef FEAT_EVAL
268 mp->m_expr = expr;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000269 if (sid > 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200270 {
271 mp->m_script_ctx.sc_sid = sid;
272 mp->m_script_ctx.sc_lnum = lnum;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000273 mp->m_script_ctx.sc_version = scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200274 }
275 else
276 {
277 mp->m_script_ctx = current_sctx;
278 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
279 }
280#endif
281
282 // add the new entry in front of the abbrlist or maphash[] list
283 if (is_abbr)
284 {
285 mp->m_next = *abbr_table;
286 *abbr_table = mp;
287 }
288 else
289 {
290 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
291
292 mp->m_next = map_table[n];
293 map_table[n] = mp;
294 }
295 return OK;
296}
297
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200298/*
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100299 * List mappings. When "haskey" is FALSE all mappings, otherwise mappings that
300 * match "keys[keys_len]".
301 */
302 static void
303list_mappings(
304 int keyround,
305 int abbrev,
306 int haskey,
307 char_u *keys,
308 int keys_len,
309 int mode,
310 int *did_local)
311{
Bram Moolenaarbf533e42022-11-13 20:43:19 +0000312 // Prevent mappings to be cleared while at the more prompt.
313 ++map_locked;
314
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000315 if (p_verbose > 0 && keyround == 1)
316 {
317 if (seenModifyOtherKeys)
318 msg_puts(_("Seen modifyOtherKeys: true"));
319 if (kitty_protocol_state != KKPS_INITIAL)
320 {
321 char *name = _("Unknown");
322 switch (kitty_protocol_state)
323 {
324 case KKPS_INITIAL: break;
325 case KKPS_OFF: name = _("Off"); break;
326 case KKPS_ENABLED: name = _("On"); break;
327 case KKPS_DISABLED: name = _("Disabled"); break;
328 case KKPS_AFTER_T_KE: name = _("Cleared"); break;
329 }
330
331 char buf[200];
332 vim_snprintf(buf, sizeof(buf), _("Kitty keyboard protocol: %s"), name);
333 msg_puts(buf);
334 }
335 }
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100336
337 // need to loop over all global hash lists
338 for (int hash = 0; hash < 256 && !got_int; ++hash)
339 {
340 mapblock_T *mp;
341
342 if (abbrev)
343 {
344 if (hash != 0) // there is only one abbreviation list
345 break;
346 mp = curbuf->b_first_abbr;
347 }
348 else
349 mp = curbuf->b_maphash[hash];
350 for ( ; mp != NULL && !got_int; mp = mp->m_next)
351 {
352 // check entries with the same mode
353 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
354 {
355 if (!haskey) // show all entries
356 {
357 showmap(mp, TRUE);
358 *did_local = TRUE;
359 }
360 else
361 {
362 int n = mp->m_keylen;
363 if (STRNCMP(mp->m_keys, keys,
364 (size_t)(n < keys_len ? n : keys_len)) == 0)
365 {
366 showmap(mp, TRUE);
367 *did_local = TRUE;
368 }
369 }
370 }
371 }
372 }
Bram Moolenaarbf533e42022-11-13 20:43:19 +0000373
374 --map_locked;
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100375}
376
377/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200378 * map[!] : show all key mappings
379 * map[!] {lhs} : show key mapping for {lhs}
380 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
381 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
382 * unmap[!] {lhs} : remove key mapping for {lhs}
383 * abbr : show all abbreviations
384 * abbr {lhs} : show abbreviations for {lhs}
385 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
386 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
387 * unabbr {lhs} : remove abbreviation for {lhs}
388 *
zeertzjq44068e92022-06-16 11:14:55 +0100389 * maptype: MAPTYPE_MAP for :map
390 * MAPTYPE_UNMAP for :unmap
391 * MAPTYPE_NOREMAP for noremap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200392 *
393 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
394 * it will be modified.
395 *
Bram Moolenaar24959102022-05-07 20:01:16 +0100396 * for :map mode is MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING
397 * for :map! mode is MODE_INSERT | MODE_CMDLINE
398 * for :cmap mode is MODE_CMDLINE
399 * for :imap mode is MODE_INSERT
400 * for :lmap mode is MODE_LANGMAP
401 * for :nmap mode is MODE_NORMAL
402 * for :vmap mode is MODE_VISUAL | MODE_SELECT
403 * for :xmap mode is MODE_VISUAL
404 * for :smap mode is MODE_SELECT
405 * for :omap mode is MODE_OP_PENDING
406 * for :tmap mode is MODE_TERMINAL
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200407 *
Bram Moolenaar24959102022-05-07 20:01:16 +0100408 * for :abbr mode is MODE_INSERT | MODE_CMDLINE
409 * for :iabbr mode is MODE_INSERT
410 * for :cabbr mode is MODE_CMDLINE
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200411 *
412 * Return 0 for success
413 * 1 for invalid arguments
414 * 2 for no match
415 * 4 for out of mem
416 * 5 for entry not unique
417 */
418 int
419do_map(
420 int maptype,
421 char_u *arg,
422 int mode,
423 int abbrev) // not a mapping but an abbreviation
424{
425 char_u *keys;
426 mapblock_T *mp, **mpp;
427 char_u *rhs;
428 char_u *p;
429 int n;
430 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200431 int hasarg;
432 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200433 int do_print;
434 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200435 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200436 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200437 char_u *arg_buf = NULL;
438 int retval = 0;
439 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200440 mapblock_T **abbr_table;
441 mapblock_T **map_table;
442 int unique = FALSE;
443 int nowait = FALSE;
444 int silent = FALSE;
445 int special = FALSE;
446#ifdef FEAT_EVAL
447 int expr = FALSE;
448#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200449 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200450 int noremap;
451 char_u *orig_rhs;
452
453 keys = arg;
454 map_table = maphash;
455 abbr_table = &first_abbr;
456
457 // For ":noremap" don't remap, otherwise do remap.
zeertzjq44068e92022-06-16 11:14:55 +0100458 if (maptype == MAPTYPE_NOREMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200459 noremap = REMAP_NONE;
460 else
461 noremap = REMAP_YES;
462
463 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
464 // any order.
465 for (;;)
466 {
467 // Check for "<buffer>": mapping local to buffer.
468 if (STRNCMP(keys, "<buffer>", 8) == 0)
469 {
470 keys = skipwhite(keys + 8);
471 map_table = curbuf->b_maphash;
472 abbr_table = &curbuf->b_first_abbr;
473 continue;
474 }
475
476 // Check for "<nowait>": don't wait for more characters.
477 if (STRNCMP(keys, "<nowait>", 8) == 0)
478 {
479 keys = skipwhite(keys + 8);
480 nowait = TRUE;
481 continue;
482 }
483
484 // Check for "<silent>": don't echo commands.
485 if (STRNCMP(keys, "<silent>", 8) == 0)
486 {
487 keys = skipwhite(keys + 8);
488 silent = TRUE;
489 continue;
490 }
491
492 // Check for "<special>": accept special keys in <>
493 if (STRNCMP(keys, "<special>", 9) == 0)
494 {
495 keys = skipwhite(keys + 9);
496 special = TRUE;
497 continue;
498 }
499
500#ifdef FEAT_EVAL
501 // Check for "<script>": remap script-local mappings only
502 if (STRNCMP(keys, "<script>", 8) == 0)
503 {
504 keys = skipwhite(keys + 8);
505 noremap = REMAP_SCRIPT;
506 continue;
507 }
508
509 // Check for "<expr>": {rhs} is an expression.
510 if (STRNCMP(keys, "<expr>", 6) == 0)
511 {
512 keys = skipwhite(keys + 6);
513 expr = TRUE;
514 continue;
515 }
516#endif
517 // Check for "<unique>": don't overwrite an existing mapping.
518 if (STRNCMP(keys, "<unique>", 8) == 0)
519 {
520 keys = skipwhite(keys + 8);
521 unique = TRUE;
522 continue;
523 }
524 break;
525 }
526
527 validate_maphash();
528
529 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
530 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
531 // with :unmap white space is included in the keys, no argument possible.
532 p = keys;
533 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
zeertzjq44068e92022-06-16 11:14:55 +0100534 while (*p && (maptype == MAPTYPE_UNMAP || !VIM_ISWHITE(*p)))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200535 {
536 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
537 p[1] != NUL)
538 ++p; // skip CTRL-V or backslash
539 ++p;
540 }
541 if (*p != NUL)
542 *p++ = NUL;
543
544 p = skipwhite(p);
545 rhs = p;
546 hasarg = (*rhs != NUL);
547 haskey = (*keys != NUL);
zeertzjq44068e92022-06-16 11:14:55 +0100548 do_print = !haskey || (maptype != MAPTYPE_UNMAP && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200549
550 // check for :unmap without argument
zeertzjq44068e92022-06-16 11:14:55 +0100551 if (maptype == MAPTYPE_UNMAP && !haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200552 {
553 retval = 1;
554 goto theend;
555 }
556
557 // If mapping has been given as ^V<C_UP> say, then replace the term codes
558 // with the appropriate two bytes. If it is a shifted special key, unshift
559 // it too, giving another two bytes.
560 // replace_termcodes() may move the result to allocated memory, which
561 // needs to be freed later (*keys_buf and *arg_buf).
562 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200563 // If something like <C-H> is simplified to 0x08 then mark it as simplified
564 // and also add a n entry with a modifier, which will work when
565 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200566 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200567 {
568 char_u *new_keys;
569 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
570
571 if (special)
572 flags |= REPTERM_SPECIAL;
573 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
574 if (did_simplify)
575 (void)replace_termcodes(keys, &alt_keys_buf,
576 flags | REPTERM_NO_SIMPLIFY, NULL);
577 keys = new_keys;
578 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200579 orig_rhs = rhs;
580 if (hasarg)
581 {
582 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
583 rhs = (char_u *)"";
584 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200585 rhs = replace_termcodes(rhs, &arg_buf,
586 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200587 }
588
Bram Moolenaar459fd782019-10-13 16:43:39 +0200589 /*
590 * The following is done twice if we have two versions of keys:
591 * "alt_keys_buf" is not NULL.
592 */
593 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200594 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200595 int did_it = FALSE;
596 int did_local = FALSE;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100597 int keyround1_simplified = keyround == 1 && did_simplify;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200598 int round;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200599
600 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200601 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200602 if (alt_keys_buf == NULL)
603 break;
604 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200605 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200606 else if (alt_keys_buf != NULL && do_print)
607 // when printing always use the not-simplified map
608 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200609
Bram Moolenaar459fd782019-10-13 16:43:39 +0200610 // check arguments and translate function keys
611 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200612 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200613 len = (int)STRLEN(keys);
614 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200615 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200616 retval = 1;
617 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200618 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200619
zeertzjq44068e92022-06-16 11:14:55 +0100620 if (abbrev && maptype != MAPTYPE_UNMAP)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200621 {
622 // If an abbreviation ends in a keyword character, the
623 // rest must be all keyword-char or all non-keyword-char.
624 // Otherwise we won't be able to find the start of it in a
625 // vi-compatible way.
626 if (has_mbyte)
627 {
628 int first, last;
629 int same = -1;
630
631 first = vim_iswordp(keys);
632 last = first;
633 p = keys + (*mb_ptr2len)(keys);
634 n = 1;
635 while (p < keys + len)
636 {
637 ++n; // nr of (multi-byte) chars
638 last = vim_iswordp(p); // type of last char
639 if (same == -1 && last != first)
640 same = n - 1; // count of same char type
641 p += (*mb_ptr2len)(p);
642 }
643 if (last && n > 2 && same >= 0 && same < n - 1)
644 {
645 retval = 1;
646 goto theend;
647 }
648 }
649 else if (vim_iswordc(keys[len - 1]))
650 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200651 for (n = 0; n < len - 2; ++n)
652 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
653 {
654 retval = 1;
655 goto theend;
656 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200657 // An abbreviation cannot contain white space.
658 for (n = 0; n < len; ++n)
659 if (VIM_ISWHITE(keys[n]))
660 {
661 retval = 1;
662 goto theend;
663 }
664 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200665 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200666
Bram Moolenaar459fd782019-10-13 16:43:39 +0200667 if (haskey && hasarg && abbrev) // if we will add an abbreviation
668 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200669 // no abbreviations
670
Bram Moolenaar459fd782019-10-13 16:43:39 +0200671 if (do_print)
672 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200673
Bram Moolenaar459fd782019-10-13 16:43:39 +0200674 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200675 if (unique && map_table == curbuf->b_maphash
zeertzjq44068e92022-06-16 11:14:55 +0100676 && haskey && hasarg && maptype != MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200677 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200678 // need to loop over all global hash lists
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100679 for (int hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200680 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200681 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200682 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200683 if (hash != 0) // there is only one abbreviation list
684 break;
685 mp = first_abbr;
686 }
687 else
688 mp = maphash[hash];
689 for ( ; mp != NULL && !got_int; mp = mp->m_next)
690 {
691 // check entries with the same mode
692 if ((mp->m_mode & mode) != 0
693 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200694 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
695 {
696 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000697 semsg(
698 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200699 mp->m_keys);
700 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000701 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200702 mp->m_keys);
703 retval = 5;
704 goto theend;
705 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200706 }
707 }
708 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200709
Bram Moolenaar459fd782019-10-13 16:43:39 +0200710 // When listing global mappings, also list buffer-local ones here.
zeertzjq44068e92022-06-16 11:14:55 +0100711 if (map_table != curbuf->b_maphash && !hasarg
712 && maptype != MAPTYPE_UNMAP)
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100713 list_mappings(keyround, abbrev, haskey, keys, len,
714 mode, &did_local);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200715
Bram Moolenaar459fd782019-10-13 16:43:39 +0200716 // Find an entry in the maphash[] list that matches.
717 // For :unmap we may loop two times: once to try to unmap an entry with
718 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000719 // an entry with a matching 'to' part. This was done to allow
720 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
721 // be replaced by "bar" because of the abbreviation.
zeertzjq44068e92022-06-16 11:14:55 +0100722 for (round = 0; (round == 0 || maptype == MAPTYPE_UNMAP) && round <= 1
Bram Moolenaar459fd782019-10-13 16:43:39 +0200723 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200724 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200725 // need to loop over all hash lists
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100726 for (int hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200727 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200728 if (abbrev)
729 {
730 if (hash > 0) // there is only one abbreviation list
731 break;
732 mpp = abbr_table;
733 }
734 else
735 mpp = &(map_table[hash]);
736 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
737 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200738
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200739 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200740 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200741 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200742 mpp = &(mp->m_next);
743 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200744 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200745 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200746 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200747 if (!mp->m_simplified)
748 {
749 showmap(mp, map_table != maphash);
750 did_it = TRUE;
751 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200752 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200753 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200754 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200755 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200756 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200757 n = (int)STRLEN(mp->m_str);
758 p = mp->m_str;
759 }
760 else
761 {
762 n = mp->m_keylen;
763 p = mp->m_keys;
764 }
765 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
766 {
zeertzjq44068e92022-06-16 11:14:55 +0100767 if (maptype == MAPTYPE_UNMAP)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200768 {
769 // Delete entry.
770 // Only accept a full match. For abbreviations
771 // we ignore trailing space when matching with
772 // the "lhs", since an abbreviation can't have
773 // trailing space.
774 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200775 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200776 {
777 mpp = &(mp->m_next);
778 continue;
779 }
zeertzjqabeb09b2022-04-26 12:29:43 +0100780 // In keyround for simplified keys, don't unmap
781 // a mapping without m_simplified flag.
Bram Moolenaar87f74102022-04-25 18:59:25 +0100782 if (keyround1_simplified && !mp->m_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100783 break;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200784 // We reset the indicated mode bits. If nothing
785 // is left the entry is deleted below.
786 mp->m_mode &= ~mode;
787 did_it = TRUE; // remember we did something
788 }
789 else if (!hasarg) // show matching entry
790 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200791 if (!mp->m_simplified)
792 {
793 showmap(mp, map_table != maphash);
794 did_it = TRUE;
795 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200796 }
797 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200798 {
799 mpp = &(mp->m_next);
800 continue;
801 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200802 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200803 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200804 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000805 semsg(
806 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200807 p);
808 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000809 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200810 p);
811 retval = 5;
812 goto theend;
813 }
814 else
815 {
816 // new rhs for existing entry
817 mp->m_mode &= ~mode; // remove mode bits
818 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200819 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200820 char_u *newstr = vim_strsave(rhs);
821
822 if (newstr == NULL)
823 {
824 retval = 4; // no mem
825 goto theend;
826 }
827 vim_free(mp->m_str);
828 mp->m_str = newstr;
829 vim_free(mp->m_orig_str);
830 mp->m_orig_str = vim_strsave(orig_rhs);
831 mp->m_noremap = noremap;
832 mp->m_nowait = nowait;
833 mp->m_silent = silent;
834 mp->m_mode = mode;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100835 mp->m_simplified = keyround1_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200836#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200837 mp->m_expr = expr;
838 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200840#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200841 did_it = TRUE;
842 }
843 }
844 if (mp->m_mode == 0) // entry can be deleted
845 {
846 map_free(mpp);
847 continue; // continue with *mpp
848 }
849
850 // May need to put this entry into another hash
851 // list.
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100852 int new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200853 if (!abbrev && new_hash != hash)
854 {
855 *mpp = mp->m_next;
856 mp->m_next = map_table[new_hash];
857 map_table[new_hash] = mp;
858
859 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200860 }
861 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200862 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200863 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200864 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200865 }
866 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200867
zeertzjq44068e92022-06-16 11:14:55 +0100868 if (maptype == MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200869 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200870 // delete entry
871 if (!did_it)
zeertzjqa4e33322022-04-24 17:07:53 +0100872 {
Bram Moolenaar87f74102022-04-25 18:59:25 +0100873 if (!keyround1_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100874 retval = 2; // no match
875 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200876 else if (*keys == Ctrl_C)
877 {
878 // If CTRL-C has been unmapped, reuse it for Interrupting.
879 if (map_table == curbuf->b_maphash)
880 curbuf->b_mapped_ctrl_c &= ~mode;
881 else
882 mapped_ctrl_c &= ~mode;
883 }
884 continue;
885 }
886
887 if (!haskey || !hasarg)
888 {
889 // print entries
890 if (!did_it && !did_local)
891 {
892 if (abbrev)
893 msg(_("No abbreviation found"));
894 else
895 msg(_("No mapping found"));
896 }
897 goto theend; // listing finished
898 }
899
900 if (did_it)
901 continue; // have added the new entry already
902
903 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200904 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
905 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200906#ifdef FEAT_EVAL
Bram Moolenaara9528b32022-01-18 20:51:35 +0000907 expr, /* sid */ -1, /* scriptversion */ 0, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200908#endif
Bram Moolenaar87f74102022-04-25 18:59:25 +0100909 keyround1_simplified) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200910 {
911 retval = 4; // no mem
912 goto theend;
913 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200914 }
915
916theend:
917 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200918 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200919 vim_free(arg_buf);
920 return retval;
921}
922
923/*
924 * Get the mapping mode from the command name.
925 */
926 static int
927get_map_mode(char_u **cmdp, int forceit)
928{
929 char_u *p;
930 int modec;
931 int mode;
932
933 p = *cmdp;
934 modec = *p++;
935 if (modec == 'i')
Bram Moolenaar24959102022-05-07 20:01:16 +0100936 mode = MODE_INSERT; // :imap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200937 else if (modec == 'l')
Bram Moolenaar24959102022-05-07 20:01:16 +0100938 mode = MODE_LANGMAP; // :lmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200939 else if (modec == 'c')
Bram Moolenaar24959102022-05-07 20:01:16 +0100940 mode = MODE_CMDLINE; // :cmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200941 else if (modec == 'n' && *p != 'o') // avoid :noremap
Bram Moolenaar24959102022-05-07 20:01:16 +0100942 mode = MODE_NORMAL; // :nmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200943 else if (modec == 'v')
Bram Moolenaar24959102022-05-07 20:01:16 +0100944 mode = MODE_VISUAL | MODE_SELECT; // :vmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200945 else if (modec == 'x')
Bram Moolenaar24959102022-05-07 20:01:16 +0100946 mode = MODE_VISUAL; // :xmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200947 else if (modec == 's')
Bram Moolenaar24959102022-05-07 20:01:16 +0100948 mode = MODE_SELECT; // :smap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200949 else if (modec == 'o')
Bram Moolenaar24959102022-05-07 20:01:16 +0100950 mode = MODE_OP_PENDING; // :omap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200951 else if (modec == 't')
Bram Moolenaar24959102022-05-07 20:01:16 +0100952 mode = MODE_TERMINAL; // :tmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200953 else
954 {
955 --p;
956 if (forceit)
Bram Moolenaar24959102022-05-07 20:01:16 +0100957 mode = MODE_INSERT | MODE_CMDLINE; // :map !
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200958 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100959 mode = MODE_VISUAL | MODE_SELECT | MODE_NORMAL | MODE_OP_PENDING;
960 // :map
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200961 }
962
963 *cmdp = p;
964 return mode;
965}
966
967/*
zeertzjqc207fd22022-06-29 10:37:40 +0100968 * Clear all mappings (":mapclear") or abbreviations (":abclear").
969 * "abbr" should be FALSE for mappings, TRUE for abbreviations.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200970 */
971 static void
972map_clear(
973 char_u *cmdp,
zeertzjqc207fd22022-06-29 10:37:40 +0100974 char_u *arg,
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200975 int forceit,
976 int abbr)
977{
978 int mode;
979 int local;
980
981 local = (STRCMP(arg, "<buffer>") == 0);
982 if (!local && *arg != NUL)
983 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000984 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200985 return;
986 }
987
988 mode = get_map_mode(&cmdp, forceit);
zeertzjqc207fd22022-06-29 10:37:40 +0100989 map_clear_mode(curbuf, mode, local, abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200990}
991
992/*
Bram Moolenaarbf533e42022-11-13 20:43:19 +0000993 * If "map_locked" is set then give an error and return TRUE.
994 * Otherwise return FALSE.
995 */
996 static int
997is_map_locked(void)
998{
999 if (map_locked > 0)
1000 {
1001 emsg(_(e_cannot_change_mappings_while_listing));
1002 return TRUE;
1003 }
1004 return FALSE;
1005}
1006
1007/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001008 * Clear all mappings in "mode".
1009 */
1010 void
zeertzjqc207fd22022-06-29 10:37:40 +01001011map_clear_mode(
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001012 buf_T *buf, // buffer for local mappings
1013 int mode, // mode in which to delete
1014 int local, // TRUE for buffer-local mappings
1015 int abbr) // TRUE for abbreviations
1016{
1017 mapblock_T *mp, **mpp;
1018 int hash;
1019 int new_hash;
1020
Bram Moolenaarbf533e42022-11-13 20:43:19 +00001021 if (is_map_locked())
1022 return;
1023
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001024 validate_maphash();
1025
1026 for (hash = 0; hash < 256; ++hash)
1027 {
1028 if (abbr)
1029 {
1030 if (hash > 0) // there is only one abbrlist
1031 break;
1032 if (local)
1033 mpp = &buf->b_first_abbr;
1034 else
1035 mpp = &first_abbr;
1036 }
1037 else
1038 {
1039 if (local)
1040 mpp = &buf->b_maphash[hash];
1041 else
1042 mpp = &maphash[hash];
1043 }
1044 while (*mpp != NULL)
1045 {
1046 mp = *mpp;
1047 if (mp->m_mode & mode)
1048 {
1049 mp->m_mode &= ~mode;
1050 if (mp->m_mode == 0) // entry can be deleted
1051 {
1052 map_free(mpp);
1053 continue;
1054 }
1055 // May need to put this entry into another hash list.
1056 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
1057 if (!abbr && new_hash != hash)
1058 {
1059 *mpp = mp->m_next;
1060 if (local)
1061 {
1062 mp->m_next = buf->b_maphash[new_hash];
1063 buf->b_maphash[new_hash] = mp;
1064 }
1065 else
1066 {
1067 mp->m_next = maphash[new_hash];
1068 maphash[new_hash] = mp;
1069 }
1070 continue; // continue with *mpp
1071 }
1072 }
1073 mpp = &(mp->m_next);
1074 }
1075 }
1076}
1077
1078#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001079 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001080mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001081{
1082 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001083
1084 if (vim_strchr(modechars, 'n') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001085 mode |= MODE_NORMAL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001086 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001087 mode |= MODE_VISUAL | MODE_SELECT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001088 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001089 mode |= MODE_VISUAL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001090 if (vim_strchr(modechars, 's') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001091 mode |= MODE_SELECT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001092 if (vim_strchr(modechars, 'o') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001093 mode |= MODE_OP_PENDING;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001094 if (vim_strchr(modechars, 'i') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001095 mode |= MODE_INSERT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001096 if (vim_strchr(modechars, 'l') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001097 mode |= MODE_LANGMAP;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001098 if (vim_strchr(modechars, 'c') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001099 mode |= MODE_CMDLINE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001100
Bram Moolenaar581ba392019-09-03 22:08:33 +02001101 return mode;
1102}
1103
1104/*
1105 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1106 * Recognize termcap codes in "str".
1107 * Also checks mappings local to the current buffer.
1108 */
1109 int
1110map_to_exists(char_u *str, char_u *modechars, int abbr)
1111{
1112 char_u *rhs;
1113 char_u *buf;
1114 int retval;
1115
Bram Moolenaar459fd782019-10-13 16:43:39 +02001116 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001117
1118 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001119 vim_free(buf);
1120
1121 return retval;
1122}
1123#endif
1124
1125/*
1126 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1127 * Also checks mappings local to the current buffer.
1128 */
1129 int
1130map_to_exists_mode(char_u *rhs, int mode, int abbr)
1131{
1132 mapblock_T *mp;
1133 int hash;
1134 int exp_buffer = FALSE;
1135
1136 validate_maphash();
1137
1138 // Do it twice: once for global maps and once for local maps.
1139 for (;;)
1140 {
1141 for (hash = 0; hash < 256; ++hash)
1142 {
1143 if (abbr)
1144 {
1145 if (hash > 0) // there is only one abbr list
1146 break;
1147 if (exp_buffer)
1148 mp = curbuf->b_first_abbr;
1149 else
1150 mp = first_abbr;
1151 }
1152 else if (exp_buffer)
1153 mp = curbuf->b_maphash[hash];
1154 else
1155 mp = maphash[hash];
1156 for (; mp; mp = mp->m_next)
1157 {
1158 if ((mp->m_mode & mode)
1159 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1160 return TRUE;
1161 }
1162 }
1163 if (exp_buffer)
1164 break;
1165 exp_buffer = TRUE;
1166 }
1167
1168 return FALSE;
1169}
1170
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001171/*
1172 * Used below when expanding mapping/abbreviation names.
1173 */
1174static int expand_mapmodes = 0;
1175static int expand_isabbrev = 0;
1176static int expand_buffer = FALSE;
1177
1178/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001179 * Translate an internal mapping/abbreviation representation into the
1180 * corresponding external one recognized by :map/:abbrev commands.
1181 * Respects the current B/k/< settings of 'cpoption'.
1182 *
1183 * This function is called when expanding mappings/abbreviations on the
1184 * command-line.
1185 *
1186 * It uses a growarray to build the translation string since the latter can be
1187 * wider than the original description. The caller has to free the string
1188 * afterwards.
1189 *
1190 * Returns NULL when there is a problem.
1191 */
1192 static char_u *
1193translate_mapping(char_u *str)
1194{
1195 garray_T ga;
1196 int c;
1197 int modifiers;
1198 int cpo_bslash;
1199 int cpo_special;
1200
1201 ga_init(&ga);
1202 ga.ga_itemsize = 1;
1203 ga.ga_growsize = 40;
1204
1205 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1206 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1207
1208 for (; *str; ++str)
1209 {
1210 c = *str;
1211 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1212 {
1213 modifiers = 0;
1214 if (str[1] == KS_MODIFIER)
1215 {
1216 str++;
1217 modifiers = *++str;
1218 c = *++str;
1219 }
1220 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1221 {
1222 if (cpo_special)
1223 {
1224 ga_clear(&ga);
1225 return NULL;
1226 }
1227 c = TO_SPECIAL(str[1], str[2]);
1228 if (c == K_ZERO) // display <Nul> as ^@
1229 c = NUL;
1230 str += 2;
1231 }
1232 if (IS_SPECIAL(c) || modifiers) // special key
1233 {
1234 if (cpo_special)
1235 {
1236 ga_clear(&ga);
1237 return NULL;
1238 }
1239 ga_concat(&ga, get_special_key_name(c, modifiers));
1240 continue; // for (str)
1241 }
1242 }
1243 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1244 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1245 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1246 if (c)
1247 ga_append(&ga, c);
1248 }
1249 ga_append(&ga, NUL);
1250 return (char_u *)(ga.ga_data);
1251}
1252
1253/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001254 * Work out what to complete when doing command line completion of mapping
1255 * or abbreviation names.
1256 */
1257 char_u *
1258set_context_in_map_cmd(
1259 expand_T *xp,
1260 char_u *cmd,
1261 char_u *arg,
1262 int forceit, // TRUE if '!' given
1263 int isabbrev, // TRUE if abbreviation
1264 int isunmap, // TRUE if unmap/unabbrev command
1265 cmdidx_T cmdidx)
1266{
1267 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1268 xp->xp_context = EXPAND_NOTHING;
1269 else
1270 {
1271 if (isunmap)
1272 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1273 else
1274 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001275 expand_mapmodes = MODE_INSERT | MODE_CMDLINE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001276 if (!isabbrev)
Bram Moolenaar24959102022-05-07 20:01:16 +01001277 expand_mapmodes += MODE_VISUAL | MODE_SELECT | MODE_NORMAL
1278 | MODE_OP_PENDING;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001279 }
1280 expand_isabbrev = isabbrev;
1281 xp->xp_context = EXPAND_MAPPINGS;
1282 expand_buffer = FALSE;
1283 for (;;)
1284 {
1285 if (STRNCMP(arg, "<buffer>", 8) == 0)
1286 {
1287 expand_buffer = TRUE;
1288 arg = skipwhite(arg + 8);
1289 continue;
1290 }
1291 if (STRNCMP(arg, "<unique>", 8) == 0)
1292 {
1293 arg = skipwhite(arg + 8);
1294 continue;
1295 }
1296 if (STRNCMP(arg, "<nowait>", 8) == 0)
1297 {
1298 arg = skipwhite(arg + 8);
1299 continue;
1300 }
1301 if (STRNCMP(arg, "<silent>", 8) == 0)
1302 {
1303 arg = skipwhite(arg + 8);
1304 continue;
1305 }
1306 if (STRNCMP(arg, "<special>", 9) == 0)
1307 {
1308 arg = skipwhite(arg + 9);
1309 continue;
1310 }
1311#ifdef FEAT_EVAL
1312 if (STRNCMP(arg, "<script>", 8) == 0)
1313 {
1314 arg = skipwhite(arg + 8);
1315 continue;
1316 }
1317 if (STRNCMP(arg, "<expr>", 6) == 0)
1318 {
1319 arg = skipwhite(arg + 6);
1320 continue;
1321 }
1322#endif
1323 break;
1324 }
1325 xp->xp_pattern = arg;
1326 }
1327
1328 return NULL;
1329}
1330
1331/*
1332 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1333 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1334 * Return OK if matches found, FAIL otherwise.
1335 */
1336 int
1337ExpandMappings(
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001338 char_u *pat,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001339 regmatch_T *regmatch,
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001340 int *numMatches,
1341 char_u ***matches)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001342{
1343 mapblock_T *mp;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001344 garray_T ga;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001345 int hash;
1346 int count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001347 char_u *p;
1348 int i;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001349 int fuzzy;
1350 int match;
Yasuhiro Matsumoto09f68a52022-06-18 16:48:36 +01001351 int score = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001352 fuzmatch_str_T *fuzmatch;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001353
1354 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001355
1356 validate_maphash();
1357
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001358 *numMatches = 0; // return values in case of FAIL
1359 *matches = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001360
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001361 if (!fuzzy)
1362 ga_init2(&ga, sizeof(char *), 3);
1363 else
1364 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001365
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001366 // First search in map modifier arguments
1367 for (i = 0; i < 7; ++i)
1368 {
1369 if (i == 0)
1370 p = (char_u *)"<silent>";
1371 else if (i == 1)
1372 p = (char_u *)"<unique>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001373#ifdef FEAT_EVAL
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001374 else if (i == 2)
1375 p = (char_u *)"<script>";
1376 else if (i == 3)
1377 p = (char_u *)"<expr>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001378#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001379 else if (i == 4 && !expand_buffer)
1380 p = (char_u *)"<buffer>";
1381 else if (i == 5)
1382 p = (char_u *)"<nowait>";
1383 else if (i == 6)
1384 p = (char_u *)"<special>";
1385 else
1386 continue;
1387
1388 if (!fuzzy)
1389 match = vim_regexec(regmatch, p, (colnr_T)0);
1390 else
1391 {
1392 score = fuzzy_match_str(p, pat);
1393 match = (score != 0);
1394 }
1395
1396 if (!match)
1397 continue;
1398
1399 if (ga_grow(&ga, 1) == FAIL)
1400 break;
1401
1402 if (fuzzy)
1403 {
1404 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1405 fuzmatch->idx = ga.ga_len;
1406 fuzmatch->str = vim_strsave(p);
1407 fuzmatch->score = score;
1408 }
1409 else
1410 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
1411 ++ga.ga_len;
1412 }
1413
1414 for (hash = 0; hash < 256; ++hash)
1415 {
1416 if (expand_isabbrev)
1417 {
1418 if (hash > 0) // only one abbrev list
1419 break; // for (hash)
1420 mp = first_abbr;
1421 }
1422 else if (expand_buffer)
1423 mp = curbuf->b_maphash[hash];
1424 else
1425 mp = maphash[hash];
1426 for (; mp; mp = mp->m_next)
1427 {
1428 if (!(mp->m_mode & expand_mapmodes))
1429 continue;
1430
1431 p = translate_mapping(mp->m_keys);
1432 if (p == NULL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001433 continue;
1434
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001435 if (!fuzzy)
1436 match = vim_regexec(regmatch, p, (colnr_T)0);
1437 else
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001438 {
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001439 score = fuzzy_match_str(p, pat);
1440 match = (score != 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001441 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001442
1443 if (!match)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001444 {
1445 vim_free(p);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001446 continue;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001447 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001448
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001449 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001450 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001451 vim_free(p);
1452 break;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001453 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001454
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001455 if (fuzzy)
1456 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001457 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1458 fuzmatch->idx = ga.ga_len;
1459 fuzmatch->str = p;
1460 fuzmatch->score = score;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001461 }
1462 else
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001463 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001464
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001465 ++ga.ga_len;
1466 } // for (mp)
1467 } // for (hash)
1468
1469 if (ga.ga_len == 0)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001470 return FAIL;
1471
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001472 if (!fuzzy)
1473 {
1474 *matches = ga.ga_data;
1475 *numMatches = ga.ga_len;
1476 }
1477 else
1478 {
1479 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
1480 FALSE) == FAIL)
1481 return FAIL;
1482 *numMatches = ga.ga_len;
1483 }
1484
1485 count = *numMatches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001486 if (count > 1)
1487 {
1488 char_u **ptr1;
1489 char_u **ptr2;
1490 char_u **ptr3;
1491
1492 // Sort the matches
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001493 // Fuzzy matching already sorts the matches
1494 if (!fuzzy)
1495 sort_strings(*matches, count);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001496
1497 // Remove multiple entries
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001498 ptr1 = *matches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001499 ptr2 = ptr1 + 1;
1500 ptr3 = ptr1 + count;
1501
1502 while (ptr2 < ptr3)
1503 {
1504 if (STRCMP(*ptr1, *ptr2))
1505 *++ptr1 = *ptr2++;
1506 else
1507 {
1508 vim_free(*ptr2++);
1509 count--;
1510 }
1511 }
1512 }
1513
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001514 *numMatches = count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001515 return (count == 0 ? FAIL : OK);
1516}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001517
1518/*
1519 * Check for an abbreviation.
1520 * Cursor is at ptr[col].
1521 * When inserting, mincol is where insert started.
1522 * For the command line, mincol is what is to be skipped over.
1523 * "c" is the character typed before check_abbr was called. It may have
1524 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1525 *
1526 * Historic vi practice: The last character of an abbreviation must be an id
1527 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1528 * characters or all non-id characters. This allows for abbr. "#i" to
1529 * "#include".
1530 *
1531 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1532 * Then there must be white space before the abbr.
1533 *
1534 * return TRUE if there is an abbreviation, FALSE if not
1535 */
1536 int
1537check_abbr(
1538 int c,
1539 char_u *ptr,
1540 int col,
1541 int mincol)
1542{
1543 int len;
1544 int scol; // starting column of the abbr.
1545 int j;
1546 char_u *s;
1547 char_u tb[MB_MAXBYTES + 4];
1548 mapblock_T *mp;
1549 mapblock_T *mp2;
1550 int clen = 0; // length in characters
1551 int is_id = TRUE;
1552 int vim_abbr;
1553
1554 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1555 return FALSE;
1556
1557 // no remapping implies no abbreviation, except for CTRL-]
1558 if (noremap_keys() && c != Ctrl_RSB)
1559 return FALSE;
1560
1561 // Check for word before the cursor: If it ends in a keyword char all
1562 // chars before it must be keyword chars or non-keyword chars, but not
1563 // white space. If it ends in a non-keyword char we accept any characters
1564 // before it except white space.
1565 if (col == 0) // cannot be an abbr.
1566 return FALSE;
1567
1568 if (has_mbyte)
1569 {
1570 char_u *p;
1571
1572 p = mb_prevptr(ptr, ptr + col);
1573 if (!vim_iswordp(p))
1574 vim_abbr = TRUE; // Vim added abbr.
1575 else
1576 {
1577 vim_abbr = FALSE; // vi compatible abbr.
1578 if (p > ptr)
1579 is_id = vim_iswordp(mb_prevptr(ptr, p));
1580 }
1581 clen = 1;
1582 while (p > ptr + mincol)
1583 {
1584 p = mb_prevptr(ptr, p);
1585 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1586 {
1587 p += (*mb_ptr2len)(p);
1588 break;
1589 }
1590 ++clen;
1591 }
1592 scol = (int)(p - ptr);
1593 }
1594 else
1595 {
1596 if (!vim_iswordc(ptr[col - 1]))
1597 vim_abbr = TRUE; // Vim added abbr.
1598 else
1599 {
1600 vim_abbr = FALSE; // vi compatible abbr.
1601 if (col > 1)
1602 is_id = vim_iswordc(ptr[col - 2]);
1603 }
1604 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1605 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1606 ;
1607 }
1608
1609 if (scol < mincol)
1610 scol = mincol;
1611 if (scol < col) // there is a word in front of the cursor
1612 {
1613 ptr += scol;
1614 len = col - scol;
1615 mp = curbuf->b_first_abbr;
1616 mp2 = first_abbr;
1617 if (mp == NULL)
1618 {
1619 mp = mp2;
1620 mp2 = NULL;
1621 }
1622 for ( ; mp; mp->m_next == NULL
1623 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1624 {
1625 int qlen = mp->m_keylen;
1626 char_u *q = mp->m_keys;
1627 int match;
1628
1629 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1630 {
1631 char_u *qe = vim_strsave(mp->m_keys);
1632
1633 // might have CSI escaped mp->m_keys
1634 if (qe != NULL)
1635 {
1636 q = qe;
1637 vim_unescape_csi(q);
1638 qlen = (int)STRLEN(q);
1639 }
1640 }
1641
1642 // find entries with right mode and keys
1643 match = (mp->m_mode & State)
1644 && qlen == len
1645 && !STRNCMP(q, ptr, (size_t)len);
1646 if (q != mp->m_keys)
1647 vim_free(q);
1648 if (match)
1649 break;
1650 }
1651 if (mp != NULL)
1652 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001653 int noremap;
1654 int silent;
1655#ifdef FEAT_EVAL
1656 int expr;
1657#endif
1658
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001659 // Found a match:
1660 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1661 // This goes from end to start.
1662 //
1663 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1664 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1665 // Characters where IS_SPECIAL() == TRUE: key codes, need
1666 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1667 //
1668 // Character CTRL-] is treated specially - it completes the
1669 // abbreviation, but is not inserted into the input stream.
1670 j = 0;
1671 if (c != Ctrl_RSB)
1672 {
1673 // special key code, split up
1674 if (IS_SPECIAL(c) || c == K_SPECIAL)
1675 {
1676 tb[j++] = K_SPECIAL;
1677 tb[j++] = K_SECOND(c);
1678 tb[j++] = K_THIRD(c);
1679 }
1680 else
1681 {
1682 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1683 tb[j++] = Ctrl_V; // special char needs CTRL-V
1684 if (has_mbyte)
1685 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001686 int newlen;
1687 char_u *escaped;
1688
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001689 // if ABBR_OFF has been added, remove it here
1690 if (c >= ABBR_OFF)
1691 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001692 newlen = (*mb_char2bytes)(c, tb + j);
1693 tb[j + newlen] = NUL;
1694 // Need to escape K_SPECIAL.
1695 escaped = vim_strsave_escape_csi(tb + j);
1696 if (escaped != NULL)
1697 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001698 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001699 mch_memmove(tb + j, escaped, newlen);
1700 j += newlen;
1701 vim_free(escaped);
1702 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001703 }
1704 else
1705 tb[j++] = c;
1706 }
1707 tb[j] = NUL;
1708 // insert the last typed char
1709 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1710 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001711
1712 // copy values here, calling eval_map_expr() may make "mp" invalid!
1713 noremap = mp->m_noremap;
1714 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001715#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001716 expr = mp->m_expr;
1717
1718 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001719 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001720 else
1721#endif
1722 s = mp->m_str;
1723 if (s != NULL)
1724 {
1725 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001726 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001727 // no abbrev. for these chars
1728 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1729#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001730 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001731 vim_free(s);
1732#endif
1733 }
1734
1735 tb[0] = Ctrl_H;
1736 tb[1] = NUL;
1737 if (has_mbyte)
1738 len = clen; // Delete characters instead of bytes
1739 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001740 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001741 return TRUE;
1742 }
1743 }
1744 return FALSE;
1745}
1746
1747#ifdef FEAT_EVAL
1748/*
1749 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1750 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001751 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001752 */
1753 char_u *
1754eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001755 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001756 int c) // NUL or typed character for abbreviation
1757{
1758 char_u *res;
1759 char_u *p;
1760 char_u *expr;
1761 pos_T save_cursor;
1762 int save_msg_col;
1763 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001764 scid_T save_sctx_sid = current_sctx.sc_sid;
1765 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001766
1767 // Remove escaping of CSI, because "str" is in a format to be used as
1768 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001769 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001770 if (expr == NULL)
1771 return NULL;
1772 vim_unescape_csi(expr);
1773
1774 // Forbid changing text or using ":normal" to avoid most of the bad side
1775 // effects. Also restore the cursor position.
zeertzjqcfe45652022-05-27 17:26:55 +01001776 ++textlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001777 ++ex_normal_lock;
1778 set_vim_var_char(c); // set v:char to the typed character
1779 save_cursor = curwin->w_cursor;
1780 save_msg_col = msg_col;
1781 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001782 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1783 {
1784 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1785 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1786 }
1787
1788 // Note: the evaluation may make "mp" invalid.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001789 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001790
zeertzjqcfe45652022-05-27 17:26:55 +01001791 --textlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001792 --ex_normal_lock;
1793 curwin->w_cursor = save_cursor;
1794 msg_col = save_msg_col;
1795 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001796 current_sctx.sc_sid = save_sctx_sid;
1797 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001798
1799 vim_free(expr);
1800
1801 if (p == NULL)
1802 return NULL;
1803 // Escape CSI in the result to be able to use the string as typeahead.
1804 res = vim_strsave_escape_csi(p);
1805 vim_free(p);
1806
1807 return res;
1808}
1809#endif
1810
1811/*
1812 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1813 * can be put in the typeahead buffer.
1814 * Returns NULL when out of memory.
1815 */
1816 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001817vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001818{
1819 char_u *res;
1820 char_u *s, *d;
1821
1822 // Need a buffer to hold up to three times as much. Four in case of an
1823 // illegal utf-8 byte:
1824 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1825 res = alloc(STRLEN(p) * 4 + 1);
1826 if (res != NULL)
1827 {
1828 d = res;
1829 for (s = p; *s != NUL; )
1830 {
zeertzjq2cd0f272022-10-04 20:14:28 +01001831 if ((s[0] == K_SPECIAL
1832#ifdef FEAT_GUI
1833 || (gui.in_use && s[0] == CSI)
1834#endif
1835 ) && s[1] != NUL && s[2] != NUL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001836 {
1837 // Copy special key unmodified.
1838 *d++ = *s++;
1839 *d++ = *s++;
1840 *d++ = *s++;
1841 }
1842 else
1843 {
1844 // Add character, possibly multi-byte to destination, escaping
1845 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1846 d = add_char2buf(PTR2CHAR(s), d);
1847 s += MB_CPTR2LEN(s);
1848 }
1849 }
1850 *d = NUL;
1851 }
1852 return res;
1853}
1854
1855/*
1856 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1857 * vim_strsave_escape_csi(). Works in-place.
1858 */
1859 void
1860vim_unescape_csi(char_u *p)
1861{
1862 char_u *s = p, *d = p;
1863
1864 while (*s != NUL)
1865 {
1866 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1867 {
1868 *d++ = K_SPECIAL;
1869 s += 3;
1870 }
1871 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1872 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1873 {
1874 *d++ = CSI;
1875 s += 3;
1876 }
1877 else
1878 *d++ = *s++;
1879 }
1880 *d = NUL;
1881}
1882
1883/*
1884 * Write map commands for the current mappings to an .exrc file.
1885 * Return FAIL on error, OK otherwise.
1886 */
1887 int
1888makemap(
1889 FILE *fd,
1890 buf_T *buf) // buffer for local mappings or NULL
1891{
1892 mapblock_T *mp;
1893 char_u c1, c2, c3;
1894 char_u *p;
1895 char *cmd;
1896 int abbr;
1897 int hash;
1898 int did_cpo = FALSE;
1899 int i;
1900
1901 validate_maphash();
1902
1903 // Do the loop twice: Once for mappings, once for abbreviations.
1904 // Then loop over all map hash lists.
1905 for (abbr = 0; abbr < 2; ++abbr)
1906 for (hash = 0; hash < 256; ++hash)
1907 {
1908 if (abbr)
1909 {
1910 if (hash > 0) // there is only one abbr list
1911 break;
1912 if (buf != NULL)
1913 mp = buf->b_first_abbr;
1914 else
1915 mp = first_abbr;
1916 }
1917 else
1918 {
1919 if (buf != NULL)
1920 mp = buf->b_maphash[hash];
1921 else
1922 mp = maphash[hash];
1923 }
1924
1925 for ( ; mp; mp = mp->m_next)
1926 {
1927 // skip script-local mappings
1928 if (mp->m_noremap == REMAP_SCRIPT)
1929 continue;
1930
1931 // skip mappings that contain a <SNR> (script-local thing),
1932 // they probably don't work when loaded again
1933 for (p = mp->m_str; *p != NUL; ++p)
1934 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1935 && p[2] == (int)KE_SNR)
1936 break;
1937 if (*p != NUL)
1938 continue;
1939
1940 // It's possible to create a mapping and then ":unmap" certain
1941 // modes. We recreate this here by mapping the individual
1942 // modes, which requires up to three of them.
1943 c1 = NUL;
1944 c2 = NUL;
1945 c3 = NUL;
1946 if (abbr)
1947 cmd = "abbr";
1948 else
1949 cmd = "map";
1950 switch (mp->m_mode)
1951 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001952 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT
1953 | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001954 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001955 case MODE_NORMAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001956 c1 = 'n';
1957 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001958 case MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001959 c1 = 'x';
1960 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001961 case MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001962 c1 = 's';
1963 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001964 case MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001965 c1 = 'o';
1966 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001967 case MODE_NORMAL | MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001968 c1 = 'n';
1969 c2 = 'x';
1970 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001971 case MODE_NORMAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001972 c1 = 'n';
1973 c2 = 's';
1974 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001975 case MODE_NORMAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001976 c1 = 'n';
1977 c2 = 'o';
1978 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001979 case MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001980 c1 = 'v';
1981 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001982 case MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001983 c1 = 'x';
1984 c2 = 'o';
1985 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001986 case MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001987 c1 = 's';
1988 c2 = 'o';
1989 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001990 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001991 c1 = 'n';
1992 c2 = 'v';
1993 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001994 case MODE_NORMAL | MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001995 c1 = 'n';
1996 c2 = 'x';
1997 c3 = 'o';
1998 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001999 case MODE_NORMAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002000 c1 = 'n';
2001 c2 = 's';
2002 c3 = 'o';
2003 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01002004 case MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002005 c1 = 'v';
2006 c2 = 'o';
2007 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01002008 case MODE_CMDLINE | MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002009 if (!abbr)
2010 cmd = "map!";
2011 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01002012 case MODE_CMDLINE:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002013 c1 = 'c';
2014 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01002015 case MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002016 c1 = 'i';
2017 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01002018 case MODE_LANGMAP:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002019 c1 = 'l';
2020 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01002021 case MODE_TERMINAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002022 c1 = 't';
2023 break;
2024 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00002025 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002026 return FAIL;
2027 }
2028 do // do this twice if c2 is set, 3 times with c3
2029 {
2030 // When outputting <> form, need to make sure that 'cpo'
2031 // is set to the Vim default.
2032 if (!did_cpo)
2033 {
2034 if (*mp->m_str == NUL) // will use <Nop>
2035 did_cpo = TRUE;
2036 else
2037 for (i = 0; i < 2; ++i)
2038 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
2039 if (*p == K_SPECIAL || *p == NL)
2040 did_cpo = TRUE;
2041 if (did_cpo)
2042 {
2043 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
2044 || put_eol(fd) < 0
2045 || fprintf(fd, "set cpo&vim") < 0
2046 || put_eol(fd) < 0)
2047 return FAIL;
2048 }
2049 }
2050 if (c1 && putc(c1, fd) < 0)
2051 return FAIL;
2052 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
2053 return FAIL;
2054 if (fputs(cmd, fd) < 0)
2055 return FAIL;
2056 if (buf != NULL && fputs(" <buffer>", fd) < 0)
2057 return FAIL;
2058 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
2059 return FAIL;
2060 if (mp->m_silent && fputs(" <silent>", fd) < 0)
2061 return FAIL;
2062#ifdef FEAT_EVAL
2063 if (mp->m_noremap == REMAP_SCRIPT
2064 && fputs("<script>", fd) < 0)
2065 return FAIL;
2066 if (mp->m_expr && fputs(" <expr>", fd) < 0)
2067 return FAIL;
2068#endif
2069
2070 if ( putc(' ', fd) < 0
2071 || put_escstr(fd, mp->m_keys, 0) == FAIL
2072 || putc(' ', fd) < 0
2073 || put_escstr(fd, mp->m_str, 1) == FAIL
2074 || put_eol(fd) < 0)
2075 return FAIL;
2076 c1 = c2;
2077 c2 = c3;
2078 c3 = NUL;
2079 } while (c1 != NUL);
2080 }
2081 }
2082
2083 if (did_cpo)
2084 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
2085 || put_eol(fd) < 0
2086 || fprintf(fd, "unlet s:cpo_save") < 0
2087 || put_eol(fd) < 0)
2088 return FAIL;
2089 return OK;
2090}
2091
2092/*
2093 * write escape string to file
2094 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2095 *
2096 * return FAIL for failure, OK otherwise
2097 */
2098 int
2099put_escstr(FILE *fd, char_u *strstart, int what)
2100{
2101 char_u *str = strstart;
2102 int c;
2103 int modifiers;
2104
2105 // :map xx <Nop>
2106 if (*str == NUL && what == 1)
2107 {
2108 if (fprintf(fd, "<Nop>") < 0)
2109 return FAIL;
2110 return OK;
2111 }
2112
2113 for ( ; *str != NUL; ++str)
2114 {
2115 char_u *p;
2116
2117 // Check for a multi-byte character, which may contain escaped
2118 // K_SPECIAL and CSI bytes
2119 p = mb_unescape(&str);
2120 if (p != NULL)
2121 {
2122 while (*p != NUL)
2123 if (fputc(*p++, fd) < 0)
2124 return FAIL;
2125 --str;
2126 continue;
2127 }
2128
2129 c = *str;
2130 // Special key codes have to be translated to be able to make sense
2131 // when they are read back.
2132 if (c == K_SPECIAL && what != 2)
2133 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002134 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002135 if (str[1] == KS_MODIFIER)
2136 {
2137 modifiers = str[2];
2138 str += 3;
2139 c = *str;
2140 }
2141 if (c == K_SPECIAL)
2142 {
2143 c = TO_SPECIAL(str[1], str[2]);
2144 str += 2;
2145 }
2146 if (IS_SPECIAL(c) || modifiers) // special key
2147 {
2148 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2149 return FAIL;
2150 continue;
2151 }
2152 }
2153
2154 // A '\n' in a map command should be written as <NL>.
2155 // A '\n' in a set command should be written as \^V^J.
2156 if (c == NL)
2157 {
2158 if (what == 2)
2159 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002160 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002161 return FAIL;
2162 }
2163 else
2164 {
2165 if (fprintf(fd, "<NL>") < 0)
2166 return FAIL;
2167 }
2168 continue;
2169 }
2170
2171 // Some characters have to be escaped with CTRL-V to
2172 // prevent them from misinterpreted in DoOneCmd().
2173 // A space, Tab and '"' has to be escaped with a backslash to
2174 // prevent it to be misinterpreted in do_set().
2175 // A space has to be escaped with a CTRL-V when it's at the start of a
2176 // ":map" rhs.
2177 // A '<' has to be escaped with a CTRL-V to prevent it being
2178 // interpreted as the start of a special key name.
2179 // A space in the lhs of a :map needs a CTRL-V.
2180 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2181 {
2182 if (putc('\\', fd) < 0)
2183 return FAIL;
2184 }
2185 else if (c < ' ' || c > '~' || c == '|'
2186 || (what == 0 && c == ' ')
2187 || (what == 1 && str == strstart && c == ' ')
2188 || (what != 2 && c == '<'))
2189 {
2190 if (putc(Ctrl_V, fd) < 0)
2191 return FAIL;
2192 }
2193 if (putc(c, fd) < 0)
2194 return FAIL;
2195 }
2196 return OK;
2197}
2198
2199/*
2200 * Check all mappings for the presence of special key codes.
2201 * Used after ":set term=xxx".
2202 */
2203 void
2204check_map_keycodes(void)
2205{
2206 mapblock_T *mp;
2207 char_u *p;
2208 int i;
2209 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002210 int abbr;
2211 int hash;
2212 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002213 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002214
2215 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002216 // avoids giving error messages
2217 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002218 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002219
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002220 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002221 // mappings/abbreviations with bp == NULL
2222 for (bp = firstbuf; ; bp = bp->b_next)
2223 {
2224 // Do the loop twice: Once for mappings, once for abbreviations.
2225 // Then loop over all map hash lists.
2226 for (abbr = 0; abbr <= 1; ++abbr)
2227 for (hash = 0; hash < 256; ++hash)
2228 {
2229 if (abbr)
2230 {
2231 if (hash) // there is only one abbr list
2232 break;
2233 if (bp != NULL)
2234 mp = bp->b_first_abbr;
2235 else
2236 mp = first_abbr;
2237 }
2238 else
2239 {
2240 if (bp != NULL)
2241 mp = bp->b_maphash[hash];
2242 else
2243 mp = maphash[hash];
2244 }
2245 for ( ; mp != NULL; mp = mp->m_next)
2246 {
2247 for (i = 0; i <= 1; ++i) // do this twice
2248 {
2249 if (i == 0)
2250 p = mp->m_keys; // once for the "from" part
2251 else
2252 p = mp->m_str; // and once for the "to" part
2253 while (*p)
2254 {
2255 if (*p == K_SPECIAL)
2256 {
2257 ++p;
2258 if (*p < 128) // for "normal" tcap entries
2259 {
2260 buf[0] = p[0];
2261 buf[1] = p[1];
2262 buf[2] = NUL;
2263 (void)add_termcap_entry(buf, FALSE);
2264 }
2265 ++p;
2266 }
2267 ++p;
2268 }
2269 }
2270 }
2271 }
2272 if (bp == NULL)
2273 break;
2274 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002275 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002276 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002277}
2278
2279#if defined(FEAT_EVAL) || defined(PROTO)
2280/*
2281 * Check the string "keys" against the lhs of all mappings.
2282 * Return pointer to rhs of mapping (mapblock->m_str).
2283 * NULL when no mapping found.
2284 */
2285 char_u *
2286check_map(
2287 char_u *keys,
2288 int mode,
2289 int exact, // require exact match
2290 int ign_mod, // ignore preceding modifier
2291 int abbr, // do abbreviations
2292 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2293 int *local_ptr) // return: buffer-local mapping or NULL
2294{
2295 int hash;
2296 int len, minlen;
2297 mapblock_T *mp;
2298 char_u *s;
2299 int local;
2300
2301 validate_maphash();
2302
2303 len = (int)STRLEN(keys);
2304 for (local = 1; local >= 0; --local)
2305 // loop over all hash lists
2306 for (hash = 0; hash < 256; ++hash)
2307 {
2308 if (abbr)
2309 {
2310 if (hash > 0) // there is only one list.
2311 break;
2312 if (local)
2313 mp = curbuf->b_first_abbr;
2314 else
2315 mp = first_abbr;
2316 }
2317 else if (local)
2318 mp = curbuf->b_maphash[hash];
2319 else
2320 mp = maphash[hash];
2321 for ( ; mp != NULL; mp = mp->m_next)
2322 {
2323 // skip entries with wrong mode, wrong length and not matching
2324 // ones
2325 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2326 {
2327 if (len > mp->m_keylen)
2328 minlen = mp->m_keylen;
2329 else
2330 minlen = len;
2331 s = mp->m_keys;
2332 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2333 && s[2] != NUL)
2334 {
2335 s += 3;
2336 if (len > mp->m_keylen - 3)
2337 minlen = mp->m_keylen - 3;
2338 }
2339 if (STRNCMP(s, keys, minlen) == 0)
2340 {
2341 if (mp_ptr != NULL)
2342 *mp_ptr = mp;
2343 if (local_ptr != NULL)
2344 *local_ptr = local;
2345 return mp->m_str;
2346 }
2347 }
2348 }
2349 }
2350
2351 return NULL;
2352}
2353
Ernie Rael659c2402022-04-24 18:40:28 +01002354/*
zeertzjqc207fd22022-06-29 10:37:40 +01002355 * "hasmapto()" function
2356 */
2357 void
2358f_hasmapto(typval_T *argvars, typval_T *rettv)
2359{
2360 char_u *name;
2361 char_u *mode;
2362 char_u buf[NUMBUFLEN];
2363 int abbr = FALSE;
2364
2365 if (in_vim9script()
2366 && (check_for_string_arg(argvars, 0) == FAIL
2367 || check_for_opt_string_arg(argvars, 1) == FAIL
2368 || (argvars[1].v_type != VAR_UNKNOWN
2369 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2370 return;
2371
2372 name = tv_get_string(&argvars[0]);
2373 if (argvars[1].v_type == VAR_UNKNOWN)
2374 mode = (char_u *)"nvo";
2375 else
2376 {
2377 mode = tv_get_string_buf(&argvars[1], buf);
2378 if (argvars[2].v_type != VAR_UNKNOWN)
2379 abbr = (int)tv_get_bool(&argvars[2]);
2380 }
2381
2382 if (map_to_exists(name, mode, abbr))
2383 rettv->vval.v_number = TRUE;
2384 else
2385 rettv->vval.v_number = FALSE;
2386}
2387
2388/*
Ernie Rael659c2402022-04-24 18:40:28 +01002389 * Fill in the empty dictionary with items as defined by maparg builtin.
2390 */
2391 static void
2392mapblock2dict(
2393 mapblock_T *mp,
2394 dict_T *dict,
2395 char_u *lhsrawalt, // may be NULL
Ernie Rael51d04d12022-05-04 15:40:22 +01002396 int buffer_local, // false if not buffer local mapping
2397 int abbr) // true if abbreviation
Ernie Rael659c2402022-04-24 18:40:28 +01002398{
zeertzjqcdc83932022-09-12 13:38:41 +01002399 char_u *lhs = str2special_save(mp->m_keys, TRUE, FALSE);
Ernie Rael659c2402022-04-24 18:40:28 +01002400 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2401
2402 dict_add_string(dict, "lhs", lhs);
2403 vim_free(lhs);
2404 dict_add_string(dict, "lhsraw", mp->m_keys);
2405 if (lhsrawalt)
2406 // Also add the value for the simplified entry.
2407 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2408 dict_add_string(dict, "rhs", mp->m_orig_str);
2409 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2410 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2411 ? 1L : 0L);
2412 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2413 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2414 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2415 dict_add_number(dict, "scriptversion",
2416 (long)mp->m_script_ctx.sc_version);
2417 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2418 dict_add_number(dict, "buffer", (long)buffer_local);
2419 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2420 dict_add_string(dict, "mode", mapmode);
Ernie Rael51d04d12022-05-04 15:40:22 +01002421 dict_add_number(dict, "abbr", abbr ? 1L : 0L);
Ernie Raeld8f5f762022-05-10 17:50:39 +01002422 dict_add_number(dict, "mode_bits", mp->m_mode);
Ernie Rael659c2402022-04-24 18:40:28 +01002423
2424 vim_free(mapmode);
2425}
2426
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002427 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002428get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2429{
2430 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002431 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002432 char_u *which;
2433 char_u buf[NUMBUFLEN];
2434 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002435 char_u *alt_keys_buf = NULL;
2436 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002437 char_u *rhs;
2438 int mode;
2439 int abbr = FALSE;
2440 int get_dict = FALSE;
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002441 mapblock_T *mp = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002442 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002443 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002444
2445 // return empty string for failure
2446 rettv->v_type = VAR_STRING;
2447 rettv->vval.v_string = NULL;
2448
2449 keys = tv_get_string(&argvars[0]);
2450 if (*keys == NUL)
2451 return;
2452
2453 if (argvars[1].v_type != VAR_UNKNOWN)
2454 {
2455 which = tv_get_string_buf_chk(&argvars[1], buf);
2456 if (argvars[2].v_type != VAR_UNKNOWN)
2457 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002458 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002459 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002460 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002461 }
2462 }
2463 else
2464 which = (char_u *)"";
2465 if (which == NULL)
2466 return;
2467
2468 mode = get_map_mode(&which, 0);
2469
Bram Moolenaar9c652532020-05-24 13:10:18 +02002470 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2471 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2472 &mp, &buffer_local);
2473 if (did_simplify)
2474 {
2475 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002476 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002477 (void)replace_termcodes(keys, &alt_keys_buf,
2478 flags | REPTERM_NO_SIMPLIFY, NULL);
2479 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2480 &buffer_local);
2481 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002482
2483 if (!get_dict)
2484 {
2485 // Return a string.
2486 if (rhs != NULL)
2487 {
2488 if (*rhs == NUL)
2489 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2490 else
zeertzjqcdc83932022-09-12 13:38:41 +01002491 rettv->vval.v_string = str2special_save(rhs, FALSE, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002492 }
2493
2494 }
Bram Moolenaar93a10962022-06-16 11:42:09 +01002495 else if (rettv_dict_alloc(rettv) == OK && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002496 mapblock2dict(mp, rettv->vval.v_dict,
Ernie Rael51d04d12022-05-04 15:40:22 +01002497 did_simplify ? keys_simplified : NULL,
2498 buffer_local, abbr);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002499
2500 vim_free(keys_buf);
2501 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002502}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002503
2504/*
Ernie Rael09661202022-04-25 14:40:44 +01002505 * "maplist()" function
Ernie Rael659c2402022-04-24 18:40:28 +01002506 */
2507 void
Ernie Rael09661202022-04-25 14:40:44 +01002508f_maplist(typval_T *argvars UNUSED, typval_T *rettv)
Ernie Rael659c2402022-04-24 18:40:28 +01002509{
2510 dict_T *d;
2511 mapblock_T *mp;
2512 int buffer_local;
2513 char_u *keys_buf;
2514 int did_simplify;
2515 int hash;
2516 char_u *lhs;
2517 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Ernie Rael09661202022-04-25 14:40:44 +01002518 int abbr = FALSE;
2519
2520 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2521 return;
2522 if (argvars[0].v_type != VAR_UNKNOWN)
2523 abbr = tv_get_bool(&argvars[0]);
Ernie Rael659c2402022-04-24 18:40:28 +01002524
Bram Moolenaar93a10962022-06-16 11:42:09 +01002525 if (rettv_list_alloc(rettv) == FAIL)
Ernie Rael659c2402022-04-24 18:40:28 +01002526 return;
2527
2528 validate_maphash();
2529
2530 // Do it twice: once for global maps and once for local maps.
2531 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2532 {
2533 for (hash = 0; hash < 256; ++hash)
2534 {
Ernie Rael09661202022-04-25 14:40:44 +01002535 if (abbr)
2536 {
2537 if (hash > 0) // there is only one abbr list
2538 break;
2539 if (buffer_local)
2540 mp = curbuf->b_first_abbr;
2541 else
2542 mp = first_abbr;
2543 }
2544 else if (buffer_local)
Ernie Rael659c2402022-04-24 18:40:28 +01002545 mp = curbuf->b_maphash[hash];
2546 else
2547 mp = maphash[hash];
2548 for (; mp; mp = mp->m_next)
2549 {
2550 if (mp->m_simplified)
2551 continue;
2552 if ((d = dict_alloc()) == NULL)
2553 return;
2554 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2555 return;
2556
2557 keys_buf = NULL;
2558 did_simplify = FALSE;
2559
zeertzjqcdc83932022-09-12 13:38:41 +01002560 lhs = str2special_save(mp->m_keys, TRUE, FALSE);
Ernie Rael659c2402022-04-24 18:40:28 +01002561 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2562 vim_free(lhs);
2563
2564 mapblock2dict(mp, d,
Ernie Rael51d04d12022-05-04 15:40:22 +01002565 did_simplify ? keys_buf : NULL,
2566 buffer_local, abbr);
Ernie Rael659c2402022-04-24 18:40:28 +01002567 vim_free(keys_buf);
2568 }
2569 }
2570 }
2571}
2572
2573/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002574 * "maparg()" function
2575 */
2576 void
2577f_maparg(typval_T *argvars, typval_T *rettv)
2578{
2579 if (in_vim9script()
2580 && (check_for_string_arg(argvars, 0) == FAIL
2581 || check_for_opt_string_arg(argvars, 1) == FAIL
2582 || (argvars[1].v_type != VAR_UNKNOWN
2583 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2584 || (argvars[2].v_type != VAR_UNKNOWN
2585 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2586 return;
2587
2588 get_maparg(argvars, rettv, TRUE);
2589}
2590
2591/*
2592 * "mapcheck()" function
2593 */
2594 void
2595f_mapcheck(typval_T *argvars, typval_T *rettv)
2596{
2597 if (in_vim9script()
2598 && (check_for_string_arg(argvars, 0) == FAIL
2599 || check_for_opt_string_arg(argvars, 1) == FAIL
2600 || (argvars[1].v_type != VAR_UNKNOWN
2601 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2602 return;
2603
2604 get_maparg(argvars, rettv, FALSE);
2605}
2606
2607/*
Ernie Rael51d04d12022-05-04 15:40:22 +01002608 * Get the mapping mode from the mode string.
2609 * It may contain multiple characters, eg "nox", or "!", or ' '
2610 * Return 0 if there is an error.
2611 */
2612 static int
2613get_map_mode_string(char_u *mode_string, int abbr)
2614{
2615 char_u *p = mode_string;
2616 int mode = 0;
2617 int tmode;
2618 int modec;
Bram Moolenaar24959102022-05-07 20:01:16 +01002619 const int MASK_V = MODE_VISUAL | MODE_SELECT;
2620 const int MASK_MAP = MODE_VISUAL | MODE_SELECT | MODE_NORMAL
2621 | MODE_OP_PENDING;
2622 const int MASK_BANG = MODE_INSERT | MODE_CMDLINE;
Ernie Rael51d04d12022-05-04 15:40:22 +01002623
2624 if (*p == NUL)
2625 p = (char_u *)" "; // compatibility
2626 while ((modec = *p++))
2627 {
2628 switch (modec)
2629 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002630 case 'i': tmode = MODE_INSERT; break;
2631 case 'l': tmode = MODE_LANGMAP; break;
2632 case 'c': tmode = MODE_CMDLINE; break;
2633 case 'n': tmode = MODE_NORMAL; break;
2634 case 'x': tmode = MODE_VISUAL; break;
2635 case 's': tmode = MODE_SELECT; break;
2636 case 'o': tmode = MODE_OP_PENDING; break;
2637 case 't': tmode = MODE_TERMINAL; break;
Ernie Rael51d04d12022-05-04 15:40:22 +01002638 case 'v': tmode = MASK_V; break;
2639 case '!': tmode = MASK_BANG; break;
2640 case ' ': tmode = MASK_MAP; break;
2641 default:
2642 return 0; // error, unknown mode character
2643 }
2644 mode |= tmode;
2645 }
2646 if ((abbr && (mode & ~MASK_BANG) != 0)
2647 || (!abbr && (mode & (mode-1)) != 0 // more than one bit set
2648 && (
2649 // false if multiple bits set in mode and mode is fully
2650 // contained in one mask
2651 !(((mode & MASK_BANG) != 0 && (mode & ~MASK_BANG) == 0)
2652 || ((mode & MASK_MAP) != 0 && (mode & ~MASK_MAP) == 0)))))
2653 return 0;
2654
2655 return mode;
2656}
2657
2658/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002659 * "mapset()" function
2660 */
2661 void
2662f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2663{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002664 char_u *which;
2665 int mode;
2666 char_u buf[NUMBUFLEN];
2667 int is_abbr;
2668 dict_T *d;
2669 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002670 char_u *lhsraw;
2671 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002672 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002673 char_u *orig_rhs;
2674 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002675 int noremap;
2676 int expr;
2677 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002678 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002679 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002680 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002681 linenr_T lnum;
2682 mapblock_T **map_table = maphash;
2683 mapblock_T **abbr_table = &first_abbr;
2684 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002685 char_u *arg;
Ernie Rael51d04d12022-05-04 15:40:22 +01002686 int dict_only;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002687
Ernie Rael51d04d12022-05-04 15:40:22 +01002688 // If first arg is a dict, then that's the only arg permitted.
2689 dict_only = argvars[0].v_type == VAR_DICT;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002690 if (in_vim9script()
Ernie Rael51d04d12022-05-04 15:40:22 +01002691 && (check_for_string_or_dict_arg(argvars, 0) == FAIL
2692 || (dict_only && check_for_unknown_arg(argvars, 1) == FAIL)
2693 || (!dict_only
2694 && (check_for_string_arg(argvars, 0) == FAIL
2695 || check_for_bool_arg(argvars, 1) == FAIL
2696 || check_for_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002697 return;
2698
Ernie Rael51d04d12022-05-04 15:40:22 +01002699 if (dict_only)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002700 {
Ernie Rael51d04d12022-05-04 15:40:22 +01002701 d = argvars[0].vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002702 which = dict_get_string(d, "mode", FALSE);
2703 is_abbr = dict_get_bool(d, "abbr", -1);
Ernie Rael51d04d12022-05-04 15:40:22 +01002704 if (which == NULL || is_abbr < 0)
2705 {
2706 emsg(_(e_entries_missing_in_mapset_dict_argument));
2707 return;
2708 }
2709 }
2710 else
2711 {
2712 which = tv_get_string_buf_chk(&argvars[0], buf);
2713 if (which == NULL)
2714 return;
2715 is_abbr = (int)tv_get_bool(&argvars[1]);
2716
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002717 if (check_for_dict_arg(argvars, 2) == FAIL)
Ernie Rael51d04d12022-05-04 15:40:22 +01002718 return;
Ernie Rael51d04d12022-05-04 15:40:22 +01002719 d = argvars[2].vval.v_dict;
2720 }
2721 mode = get_map_mode_string(which, is_abbr);
2722 if (mode == 0)
2723 {
2724 semsg(_(e_illegal_map_mode_string_str), which);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002725 return;
2726 }
Ernie Rael51d04d12022-05-04 15:40:22 +01002727
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002728
2729 // Get the values in the same order as above in get_maparg().
Bram Moolenaard61efa52022-07-23 09:52:04 +01002730 lhs = dict_get_string(d, "lhs", FALSE);
2731 lhsraw = dict_get_string(d, "lhsraw", FALSE);
2732 lhsrawalt = dict_get_string(d, "lhsrawalt", FALSE);
2733 rhs = dict_get_string(d, "rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002734 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002735 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002736 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002737 return;
2738 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002739 orig_rhs = rhs;
zeertzjq92a3d202022-08-31 16:40:17 +01002740 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
2741 rhs = (char_u *)"";
2742 else
2743 rhs = replace_termcodes(rhs, &arg_buf,
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002744 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002745
Bram Moolenaard61efa52022-07-23 09:52:04 +01002746 noremap = dict_get_number(d, "noremap") ? REMAP_NONE: 0;
2747 if (dict_get_number(d, "script") != 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002748 noremap = REMAP_SCRIPT;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002749 expr = dict_get_number(d, "expr") != 0;
2750 silent = dict_get_number(d, "silent") != 0;
2751 sid = dict_get_number(d, "sid");
2752 scriptversion = dict_get_number(d, "scriptversion");
2753 lnum = dict_get_number(d, "lnum");
2754 buffer = dict_get_number(d, "buffer");
2755 nowait = dict_get_number(d, "nowait") != 0;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002756 // mode from the dict is not used
2757
2758 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002759 {
2760 map_table = curbuf->b_maphash;
2761 abbr_table = &curbuf->b_first_abbr;
2762 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002763
2764 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002765 if (buffer)
2766 {
2767 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2768 if (arg == NULL)
2769 return;
2770 STRCPY(arg, "<buffer>");
2771 STRCPY(arg + 8, lhs);
2772 }
2773 else
2774 {
2775 arg = vim_strsave(lhs);
2776 if (arg == NULL)
2777 return;
2778 }
zeertzjq44068e92022-06-16 11:14:55 +01002779 do_map(MAPTYPE_UNMAP, arg, mode, is_abbr);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002780 vim_free(arg);
2781
Bram Moolenaar9c652532020-05-24 13:10:18 +02002782 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002783 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002784 if (lhsrawalt != NULL)
2785 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002786 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2787 lnum, 1);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002788 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002789}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002790#endif
2791
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002792
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002793#if defined(MSWIN) || defined(MACOS_X)
2794
Bram Moolenaar24959102022-05-07 20:01:16 +01002795# define VIS_SEL (MODE_VISUAL | MODE_SELECT) // abbreviation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002796
2797/*
2798 * Default mappings for some often used keys.
2799 */
2800struct initmap
2801{
2802 char_u *arg;
2803 int mode;
2804};
2805
2806# ifdef FEAT_GUI_MSWIN
2807// Use the Windows (CUA) keybindings. (GUI)
2808static struct initmap initmappings[] =
2809{
2810 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002811 {(char_u *)"<S-Insert> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002812 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002813 {(char_u *)"<S-Insert> <C-R><C-O>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002814 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2815 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2816 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2817 {(char_u *)"<C-X> \"*d", VIS_SEL},
2818 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2819};
2820# endif
2821
2822# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2823// Use the Windows (CUA) keybindings. (Console)
2824static struct initmap cinitmappings[] =
2825{
Bram Moolenaar24959102022-05-07 20:01:16 +01002826 {(char_u *)"\316w <C-Home>", MODE_NORMAL | VIS_SEL},
2827 {(char_u *)"\316w <C-Home>", MODE_INSERT | MODE_CMDLINE},
2828 {(char_u *)"\316u <C-End>", MODE_NORMAL | VIS_SEL},
2829 {(char_u *)"\316u <C-End>", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002830
2831 // paste, copy and cut
2832# ifdef FEAT_CLIPBOARD
Bram Moolenaar24959102022-05-07 20:01:16 +01002833 {(char_u *)"\316\324 \"*P", MODE_NORMAL}, // SHIFT-Insert is "*P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002834 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
Bram Moolenaar24959102022-05-07 20:01:16 +01002835 {(char_u *)"\316\324 \022\017*", MODE_INSERT}, // SHIFT-Insert is ^R^O*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002836 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2837 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2838 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2839 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2840# else
Bram Moolenaar24959102022-05-07 20:01:16 +01002841 {(char_u *)"\316\324 P", MODE_NORMAL}, // SHIFT-Insert is P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002842 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
Bram Moolenaar24959102022-05-07 20:01:16 +01002843 {(char_u *)"\316\324 \022\017\"", MODE_INSERT}, // SHIFT-Insert is ^R^O"
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002844 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2845 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2846 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2847# endif
2848};
2849# endif
2850
2851# if defined(MACOS_X)
2852static struct initmap initmappings[] =
2853{
2854 // Use the Standard MacOS binding.
2855 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002856 {(char_u *)"<D-v> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002857 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002858 {(char_u *)"<D-v> <C-R>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002859 {(char_u *)"<D-c> \"*y", VIS_SEL},
2860 {(char_u *)"<D-x> \"*d", VIS_SEL},
2861 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2862};
2863# endif
2864
2865# undef VIS_SEL
2866#endif
2867
2868/*
2869 * Set up default mappings.
2870 */
2871 void
2872init_mappings(void)
2873{
2874#if defined(MSWIN) || defined(MACOS_X)
2875 int i;
2876
2877# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2878# ifdef VIMDLL
2879 if (!gui.starting)
2880# endif
2881 {
K.Takataeeec2542021-06-02 13:28:16 +02002882 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002883 add_map(cinitmappings[i].arg, cinitmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002884 }
2885# endif
2886# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002887 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002888 add_map(initmappings[i].arg, initmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002889# endif
2890#endif
2891}
2892
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002893/*
2894 * Add a mapping "map" for mode "mode".
zeertzjq44068e92022-06-16 11:14:55 +01002895 * When "nore" is TRUE use MAPTYPE_NOREMAP.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002896 * Need to put string in allocated memory, because do_map() will modify it.
2897 */
2898 void
zeertzjq44068e92022-06-16 11:14:55 +01002899add_map(char_u *map, int mode, int nore)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002900{
2901 char_u *s;
2902 char_u *cpo_save = p_cpo;
2903
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002904 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002905 s = vim_strsave(map);
2906 if (s != NULL)
2907 {
zeertzjq44068e92022-06-16 11:14:55 +01002908 (void)do_map(nore ? MAPTYPE_NOREMAP : MAPTYPE_MAP, s, mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002909 vim_free(s);
2910 }
2911 p_cpo = cpo_save;
2912}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002913
Bram Moolenaare677df82019-09-02 22:31:11 +02002914#if defined(FEAT_LANGMAP) || defined(PROTO)
2915/*
2916 * Any character has an equivalent 'langmap' character. This is used for
2917 * keyboards that have a special language mode that sends characters above
2918 * 128 (although other characters can be translated too). The "to" field is a
2919 * Vim command character. This avoids having to switch the keyboard back to
2920 * ASCII mode when leaving Insert mode.
2921 *
2922 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2923 * commands.
2924 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2925 * same as langmap_mapchar[] for characters >= 256.
2926 *
2927 * Use growarray for 'langmap' chars >= 256
2928 */
2929typedef struct
2930{
2931 int from;
2932 int to;
2933} langmap_entry_T;
2934
2935static garray_T langmap_mapga;
2936
2937/*
2938 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2939 * field. If not found insert a new entry at the appropriate location.
2940 */
2941 static void
2942langmap_set_entry(int from, int to)
2943{
2944 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2945 int a = 0;
2946 int b = langmap_mapga.ga_len;
2947
2948 // Do a binary search for an existing entry.
2949 while (a != b)
2950 {
2951 int i = (a + b) / 2;
2952 int d = entries[i].from - from;
2953
2954 if (d == 0)
2955 {
2956 entries[i].to = to;
2957 return;
2958 }
2959 if (d < 0)
2960 a = i + 1;
2961 else
2962 b = i;
2963 }
2964
2965 if (ga_grow(&langmap_mapga, 1) != OK)
2966 return; // out of memory
2967
2968 // insert new entry at position "a"
2969 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2970 mch_memmove(entries + 1, entries,
2971 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2972 ++langmap_mapga.ga_len;
2973 entries[0].from = from;
2974 entries[0].to = to;
2975}
2976
2977/*
2978 * Apply 'langmap' to multi-byte character "c" and return the result.
2979 */
2980 int
2981langmap_adjust_mb(int c)
2982{
2983 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2984 int a = 0;
2985 int b = langmap_mapga.ga_len;
2986
2987 while (a != b)
2988 {
2989 int i = (a + b) / 2;
2990 int d = entries[i].from - c;
2991
2992 if (d == 0)
2993 return entries[i].to; // found matching entry
2994 if (d < 0)
2995 a = i + 1;
2996 else
2997 b = i;
2998 }
2999 return c; // no entry found, return "c" unmodified
3000}
3001
3002 void
3003langmap_init(void)
3004{
3005 int i;
3006
3007 for (i = 0; i < 256; i++)
3008 langmap_mapchar[i] = i; // we init with a one-to-one map
3009 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
3010}
3011
3012/*
3013 * Called when langmap option is set; the language map can be
3014 * changed at any time!
3015 */
3016 void
3017langmap_set(void)
3018{
3019 char_u *p;
3020 char_u *p2;
3021 int from, to;
3022
3023 ga_clear(&langmap_mapga); // clear the previous map first
3024 langmap_init(); // back to one-to-one map
3025
3026 for (p = p_langmap; p[0] != NUL; )
3027 {
3028 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
3029 MB_PTR_ADV(p2))
3030 {
3031 if (p2[0] == '\\' && p2[1] != NUL)
3032 ++p2;
3033 }
3034 if (p2[0] == ';')
3035 ++p2; // abcd;ABCD form, p2 points to A
3036 else
3037 p2 = NULL; // aAbBcCdD form, p2 is NULL
3038 while (p[0])
3039 {
3040 if (p[0] == ',')
3041 {
3042 ++p;
3043 break;
3044 }
3045 if (p[0] == '\\' && p[1] != NUL)
3046 ++p;
3047 from = (*mb_ptr2char)(p);
3048 to = NUL;
3049 if (p2 == NULL)
3050 {
3051 MB_PTR_ADV(p);
3052 if (p[0] != ',')
3053 {
3054 if (p[0] == '\\')
3055 ++p;
3056 to = (*mb_ptr2char)(p);
3057 }
3058 }
3059 else
3060 {
3061 if (p2[0] != ',')
3062 {
3063 if (p2[0] == '\\')
3064 ++p2;
3065 to = (*mb_ptr2char)(p2);
3066 }
3067 }
3068 if (to == NUL)
3069 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003070 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02003071 transchar(from));
3072 return;
3073 }
3074
3075 if (from >= 256)
3076 langmap_set_entry(from, to);
3077 else
3078 langmap_mapchar[from & 255] = to;
3079
3080 // Advance to next pair
3081 MB_PTR_ADV(p);
3082 if (p2 != NULL)
3083 {
3084 MB_PTR_ADV(p2);
3085 if (*p == ';')
3086 {
3087 p = p2;
3088 if (p[0] != NUL)
3089 {
3090 if (p[0] != ',')
3091 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003092 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02003093 return;
3094 }
3095 ++p;
3096 }
3097 break;
3098 }
3099 }
3100 }
3101 }
3102}
3103#endif
3104
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003105 static void
3106do_exmap(exarg_T *eap, int isabbrev)
3107{
3108 int mode;
3109 char_u *cmdp;
3110
3111 cmdp = eap->cmd;
3112 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
3113
zeertzjq44068e92022-06-16 11:14:55 +01003114 switch (do_map(*cmdp == 'n' ? MAPTYPE_NOREMAP
3115 : *cmdp == 'u' ? MAPTYPE_UNMAP : MAPTYPE_MAP,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003116 eap->arg, mode, isabbrev))
3117 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003118 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003119 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003120 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
3121 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003122 break;
3123 }
3124}
3125
3126/*
3127 * ":abbreviate" and friends.
3128 */
3129 void
3130ex_abbreviate(exarg_T *eap)
3131{
3132 do_exmap(eap, TRUE); // almost the same as mapping
3133}
3134
3135/*
3136 * ":map" and friends.
3137 */
3138 void
3139ex_map(exarg_T *eap)
3140{
3141 // If we are sourcing .exrc or .vimrc in current directory we
3142 // print the mappings for security reasons.
3143 if (secure)
3144 {
3145 secure = 2;
3146 msg_outtrans(eap->cmd);
3147 msg_putchar('\n');
3148 }
3149 do_exmap(eap, FALSE);
3150}
3151
3152/*
3153 * ":unmap" and friends.
3154 */
3155 void
3156ex_unmap(exarg_T *eap)
3157{
3158 do_exmap(eap, FALSE);
3159}
3160
3161/*
3162 * ":mapclear" and friends.
3163 */
3164 void
3165ex_mapclear(exarg_T *eap)
3166{
3167 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
3168}
3169
3170/*
3171 * ":abclear" and friends.
3172 */
3173 void
3174ex_abclear(exarg_T *eap)
3175{
3176 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
3177}