blob: 762cce2d5fc9e7aa2e7cff08df80e124fe7ff0d2 [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
27/*
28 * Make a hash value for a mapping.
29 * "mode" is the lower 4 bits of the State for the mapping.
30 * "c1" is the first character of the "lhs".
31 * Returns a value between 0 and 255, index in maphash.
32 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode.
33 */
34#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING + TERMINAL)) ? (c1) : ((c1) ^ 0x80))
35
36/*
37 * Get the start of the hashed map list for "state" and first character "c".
38 */
39 mapblock_T *
40get_maphash_list(int state, int c)
41{
42 return maphash[MAP_HASH(state, c)];
43}
44
45/*
46 * Get the buffer-local hashed map list for "state" and first character "c".
47 */
48 mapblock_T *
49get_buf_maphash_list(int state, int c)
50{
51 return curbuf->b_maphash[MAP_HASH(state, c)];
52}
53
54 int
55is_maphash_valid(void)
56{
57 return maphash_valid;
58}
59
60/*
61 * Initialize maphash[] for first use.
62 */
63 static void
64validate_maphash(void)
65{
66 if (!maphash_valid)
67 {
Bram Moolenaara80faa82020-04-12 19:37:17 +020068 CLEAR_FIELD(maphash);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020069 maphash_valid = TRUE;
70 }
71}
72
73/*
74 * Delete one entry from the abbrlist or maphash[].
75 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
76 */
77 static void
78map_free(mapblock_T **mpp)
79{
80 mapblock_T *mp;
81
82 mp = *mpp;
83 vim_free(mp->m_keys);
84 vim_free(mp->m_str);
85 vim_free(mp->m_orig_str);
86 *mpp = mp->m_next;
Bram Moolenaard648c012022-01-16 14:58:34 +000087#ifdef FEAT_EVAL
Bram Moolenaarf61c89d2022-01-19 22:51:48 +000088 reset_last_used_map(mp);
Bram Moolenaard648c012022-01-16 14:58:34 +000089#endif
Bram Moolenaar8aa0e6c2022-01-20 11:27:58 +000090 vim_free(mp);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020091}
92
93/*
94 * Return characters to represent the map mode in an allocated string.
95 * Returns NULL when out of memory.
96 */
97 static char_u *
98map_mode_to_chars(int mode)
99{
100 garray_T mapmode;
101
102 ga_init2(&mapmode, 1, 7);
103
104 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
105 ga_append(&mapmode, '!'); // :map!
106 else if (mode & INSERT)
107 ga_append(&mapmode, 'i'); // :imap
108 else if (mode & LANGMAP)
109 ga_append(&mapmode, 'l'); // :lmap
110 else if (mode & CMDLINE)
111 ga_append(&mapmode, 'c'); // :cmap
112 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
113 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
114 ga_append(&mapmode, ' '); // :map
115 else
116 {
117 if (mode & NORMAL)
118 ga_append(&mapmode, 'n'); // :nmap
119 if (mode & OP_PENDING)
120 ga_append(&mapmode, 'o'); // :omap
121 if (mode & TERMINAL)
122 ga_append(&mapmode, 't'); // :tmap
123 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
124 ga_append(&mapmode, 'v'); // :vmap
125 else
126 {
127 if (mode & VISUAL)
128 ga_append(&mapmode, 'x'); // :xmap
129 if (mode & SELECTMODE)
130 ga_append(&mapmode, 's'); // :smap
131 }
132 }
133
134 ga_append(&mapmode, NUL);
135 return (char_u *)mapmode.ga_data;
136}
137
138 static void
139showmap(
140 mapblock_T *mp,
141 int local) // TRUE for buffer-local map
142{
143 int len = 1;
144 char_u *mapchars;
145
146 if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
147 return;
148
149 if (msg_didout || msg_silent != 0)
150 {
151 msg_putchar('\n');
152 if (got_int) // 'q' typed at MORE prompt
153 return;
154 }
155
156 mapchars = map_mode_to_chars(mp->m_mode);
157 if (mapchars != NULL)
158 {
159 msg_puts((char *)mapchars);
160 len = (int)STRLEN(mapchars);
161 vim_free(mapchars);
162 }
163
164 while (++len <= 3)
165 msg_putchar(' ');
166
167 // Display the LHS. Get length of what we write.
168 len = msg_outtrans_special(mp->m_keys, TRUE, 0);
169 do
170 {
171 msg_putchar(' '); // padd with blanks
172 ++len;
173 } while (len < 12);
174
175 if (mp->m_noremap == REMAP_NONE)
176 msg_puts_attr("*", HL_ATTR(HLF_8));
177 else if (mp->m_noremap == REMAP_SCRIPT)
178 msg_puts_attr("&", HL_ATTR(HLF_8));
179 else
180 msg_putchar(' ');
181
182 if (local)
183 msg_putchar('@');
184 else
185 msg_putchar(' ');
186
187 // Use FALSE below if we only want things like <Up> to show up as such on
188 // the rhs, and not M-x etc, TRUE gets both -- webb
189 if (*mp->m_str == NUL)
190 msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
191 else
zeertzjqac402f42022-05-04 18:51:43 +0100192 msg_outtrans_special(mp->m_str, FALSE, 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200193#ifdef FEAT_EVAL
194 if (p_verbose > 0)
195 last_set_msg(mp->m_script_ctx);
196#endif
Bram Moolenaard288eaa2022-02-16 18:27:55 +0000197 msg_clr_eos();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200198 out_flush(); // show one line at a time
199}
200
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200201 static int
202map_add(
203 mapblock_T **map_table,
204 mapblock_T **abbr_table,
205 char_u *keys,
206 char_u *rhs,
207 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200208 int noremap,
209 int nowait,
210 int silent,
211 int mode,
212 int is_abbr,
213#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200214 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200215 scid_T sid, // -1 to use current_sctx
Bram Moolenaara9528b32022-01-18 20:51:35 +0000216 int scriptversion,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200217 linenr_T lnum,
218#endif
219 int simplified)
220{
Bram Moolenaar94075b22022-01-18 20:30:39 +0000221 mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200222
223 if (mp == NULL)
224 return FAIL;
225
226 // If CTRL-C has been mapped, don't always use it for Interrupting.
227 if (*keys == Ctrl_C)
228 {
229 if (map_table == curbuf->b_maphash)
230 curbuf->b_mapped_ctrl_c |= mode;
231 else
232 mapped_ctrl_c |= mode;
233 }
234
235 mp->m_keys = vim_strsave(keys);
236 mp->m_str = vim_strsave(rhs);
237 mp->m_orig_str = vim_strsave(orig_rhs);
238 if (mp->m_keys == NULL || mp->m_str == NULL)
239 {
240 vim_free(mp->m_keys);
241 vim_free(mp->m_str);
242 vim_free(mp->m_orig_str);
243 vim_free(mp);
244 return FAIL;
245 }
246 mp->m_keylen = (int)STRLEN(mp->m_keys);
247 mp->m_noremap = noremap;
248 mp->m_nowait = nowait;
249 mp->m_silent = silent;
250 mp->m_mode = mode;
251 mp->m_simplified = simplified;
252#ifdef FEAT_EVAL
253 mp->m_expr = expr;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000254 if (sid > 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200255 {
256 mp->m_script_ctx.sc_sid = sid;
257 mp->m_script_ctx.sc_lnum = lnum;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000258 mp->m_script_ctx.sc_version = scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200259 }
260 else
261 {
262 mp->m_script_ctx = current_sctx;
263 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
264 }
265#endif
266
267 // add the new entry in front of the abbrlist or maphash[] list
268 if (is_abbr)
269 {
270 mp->m_next = *abbr_table;
271 *abbr_table = mp;
272 }
273 else
274 {
275 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
276
277 mp->m_next = map_table[n];
278 map_table[n] = mp;
279 }
280 return OK;
281}
282
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200283/*
284 * map[!] : show all key mappings
285 * map[!] {lhs} : show key mapping for {lhs}
286 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
287 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
288 * unmap[!] {lhs} : remove key mapping for {lhs}
289 * abbr : show all abbreviations
290 * abbr {lhs} : show abbreviations for {lhs}
291 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
292 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
293 * unabbr {lhs} : remove abbreviation for {lhs}
294 *
295 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
296 *
297 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
298 * it will be modified.
299 *
300 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
301 * for :map! mode is INSERT + CMDLINE
302 * for :cmap mode is CMDLINE
303 * for :imap mode is INSERT
304 * for :lmap mode is LANGMAP
305 * for :nmap mode is NORMAL
306 * for :vmap mode is VISUAL + SELECTMODE
307 * for :xmap mode is VISUAL
308 * for :smap mode is SELECTMODE
309 * for :omap mode is OP_PENDING
310 * for :tmap mode is TERMINAL
311 *
312 * for :abbr mode is INSERT + CMDLINE
313 * for :iabbr mode is INSERT
314 * for :cabbr mode is CMDLINE
315 *
316 * Return 0 for success
317 * 1 for invalid arguments
318 * 2 for no match
319 * 4 for out of mem
320 * 5 for entry not unique
321 */
322 int
323do_map(
324 int maptype,
325 char_u *arg,
326 int mode,
327 int abbrev) // not a mapping but an abbreviation
328{
329 char_u *keys;
330 mapblock_T *mp, **mpp;
331 char_u *rhs;
332 char_u *p;
333 int n;
334 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200335 int hasarg;
336 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200337 int do_print;
338 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200339 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200340 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200341 char_u *arg_buf = NULL;
342 int retval = 0;
343 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200344 mapblock_T **abbr_table;
345 mapblock_T **map_table;
346 int unique = FALSE;
347 int nowait = FALSE;
348 int silent = FALSE;
349 int special = FALSE;
350#ifdef FEAT_EVAL
351 int expr = FALSE;
352#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200353 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200354 int noremap;
355 char_u *orig_rhs;
356
357 keys = arg;
358 map_table = maphash;
359 abbr_table = &first_abbr;
360
361 // For ":noremap" don't remap, otherwise do remap.
362 if (maptype == 2)
363 noremap = REMAP_NONE;
364 else
365 noremap = REMAP_YES;
366
367 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
368 // any order.
369 for (;;)
370 {
371 // Check for "<buffer>": mapping local to buffer.
372 if (STRNCMP(keys, "<buffer>", 8) == 0)
373 {
374 keys = skipwhite(keys + 8);
375 map_table = curbuf->b_maphash;
376 abbr_table = &curbuf->b_first_abbr;
377 continue;
378 }
379
380 // Check for "<nowait>": don't wait for more characters.
381 if (STRNCMP(keys, "<nowait>", 8) == 0)
382 {
383 keys = skipwhite(keys + 8);
384 nowait = TRUE;
385 continue;
386 }
387
388 // Check for "<silent>": don't echo commands.
389 if (STRNCMP(keys, "<silent>", 8) == 0)
390 {
391 keys = skipwhite(keys + 8);
392 silent = TRUE;
393 continue;
394 }
395
396 // Check for "<special>": accept special keys in <>
397 if (STRNCMP(keys, "<special>", 9) == 0)
398 {
399 keys = skipwhite(keys + 9);
400 special = TRUE;
401 continue;
402 }
403
404#ifdef FEAT_EVAL
405 // Check for "<script>": remap script-local mappings only
406 if (STRNCMP(keys, "<script>", 8) == 0)
407 {
408 keys = skipwhite(keys + 8);
409 noremap = REMAP_SCRIPT;
410 continue;
411 }
412
413 // Check for "<expr>": {rhs} is an expression.
414 if (STRNCMP(keys, "<expr>", 6) == 0)
415 {
416 keys = skipwhite(keys + 6);
417 expr = TRUE;
418 continue;
419 }
420#endif
421 // Check for "<unique>": don't overwrite an existing mapping.
422 if (STRNCMP(keys, "<unique>", 8) == 0)
423 {
424 keys = skipwhite(keys + 8);
425 unique = TRUE;
426 continue;
427 }
428 break;
429 }
430
431 validate_maphash();
432
433 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
434 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
435 // with :unmap white space is included in the keys, no argument possible.
436 p = keys;
437 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
438 while (*p && (maptype == 1 || !VIM_ISWHITE(*p)))
439 {
440 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
441 p[1] != NUL)
442 ++p; // skip CTRL-V or backslash
443 ++p;
444 }
445 if (*p != NUL)
446 *p++ = NUL;
447
448 p = skipwhite(p);
449 rhs = p;
450 hasarg = (*rhs != NUL);
451 haskey = (*keys != NUL);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200452 do_print = !haskey || (maptype != 1 && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200453
454 // check for :unmap without argument
455 if (maptype == 1 && !haskey)
456 {
457 retval = 1;
458 goto theend;
459 }
460
461 // If mapping has been given as ^V<C_UP> say, then replace the term codes
462 // with the appropriate two bytes. If it is a shifted special key, unshift
463 // it too, giving another two bytes.
464 // replace_termcodes() may move the result to allocated memory, which
465 // needs to be freed later (*keys_buf and *arg_buf).
466 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200467 // If something like <C-H> is simplified to 0x08 then mark it as simplified
468 // and also add a n entry with a modifier, which will work when
469 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200470 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200471 {
472 char_u *new_keys;
473 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
474
475 if (special)
476 flags |= REPTERM_SPECIAL;
477 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
478 if (did_simplify)
479 (void)replace_termcodes(keys, &alt_keys_buf,
480 flags | REPTERM_NO_SIMPLIFY, NULL);
481 keys = new_keys;
482 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200483 orig_rhs = rhs;
484 if (hasarg)
485 {
486 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
487 rhs = (char_u *)"";
488 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200489 rhs = replace_termcodes(rhs, &arg_buf,
490 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200491 }
492
Bram Moolenaar459fd782019-10-13 16:43:39 +0200493 /*
494 * The following is done twice if we have two versions of keys:
495 * "alt_keys_buf" is not NULL.
496 */
497 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200498 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200499 int did_it = FALSE;
500 int did_local = FALSE;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100501 int keyround1_simplified = keyround == 1 && did_simplify;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200502 int round;
503 int hash;
504 int new_hash;
505
506 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200507 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200508 if (alt_keys_buf == NULL)
509 break;
510 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200511 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200512 else if (alt_keys_buf != NULL && do_print)
513 // when printing always use the not-simplified map
514 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200515
Bram Moolenaar459fd782019-10-13 16:43:39 +0200516 // check arguments and translate function keys
517 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200518 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200519 len = (int)STRLEN(keys);
520 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200521 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200522 retval = 1;
523 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200524 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200525
526 if (abbrev && maptype != 1)
527 {
528 // If an abbreviation ends in a keyword character, the
529 // rest must be all keyword-char or all non-keyword-char.
530 // Otherwise we won't be able to find the start of it in a
531 // vi-compatible way.
532 if (has_mbyte)
533 {
534 int first, last;
535 int same = -1;
536
537 first = vim_iswordp(keys);
538 last = first;
539 p = keys + (*mb_ptr2len)(keys);
540 n = 1;
541 while (p < keys + len)
542 {
543 ++n; // nr of (multi-byte) chars
544 last = vim_iswordp(p); // type of last char
545 if (same == -1 && last != first)
546 same = n - 1; // count of same char type
547 p += (*mb_ptr2len)(p);
548 }
549 if (last && n > 2 && same >= 0 && same < n - 1)
550 {
551 retval = 1;
552 goto theend;
553 }
554 }
555 else if (vim_iswordc(keys[len - 1]))
556 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200557 for (n = 0; n < len - 2; ++n)
558 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
559 {
560 retval = 1;
561 goto theend;
562 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200563 // An abbreviation cannot contain white space.
564 for (n = 0; n < len; ++n)
565 if (VIM_ISWHITE(keys[n]))
566 {
567 retval = 1;
568 goto theend;
569 }
570 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200571 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200572
Bram Moolenaar459fd782019-10-13 16:43:39 +0200573 if (haskey && hasarg && abbrev) // if we will add an abbreviation
574 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200575 // no abbreviations
576
Bram Moolenaar459fd782019-10-13 16:43:39 +0200577 if (do_print)
578 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200579
Bram Moolenaar459fd782019-10-13 16:43:39 +0200580 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200581 if (unique && map_table == curbuf->b_maphash
582 && haskey && hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200583 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200584 // need to loop over all global hash lists
585 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200586 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200587 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200588 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200589 if (hash != 0) // there is only one abbreviation list
590 break;
591 mp = first_abbr;
592 }
593 else
594 mp = maphash[hash];
595 for ( ; mp != NULL && !got_int; mp = mp->m_next)
596 {
597 // check entries with the same mode
598 if ((mp->m_mode & mode) != 0
599 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200600 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
601 {
602 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000603 semsg(
604 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200605 mp->m_keys);
606 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000607 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200608 mp->m_keys);
609 retval = 5;
610 goto theend;
611 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200612 }
613 }
614 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200615
Bram Moolenaar459fd782019-10-13 16:43:39 +0200616 // When listing global mappings, also list buffer-local ones here.
617 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200618 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200619 // need to loop over all global hash lists
620 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200621 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200622 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200623 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200624 if (hash != 0) // there is only one abbreviation list
625 break;
626 mp = curbuf->b_first_abbr;
627 }
628 else
629 mp = curbuf->b_maphash[hash];
630 for ( ; mp != NULL && !got_int; mp = mp->m_next)
631 {
632 // check entries with the same mode
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200633 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200634 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200635 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200636 {
637 showmap(mp, TRUE);
638 did_local = TRUE;
639 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200640 else
641 {
642 n = mp->m_keylen;
643 if (STRNCMP(mp->m_keys, keys,
644 (size_t)(n < len ? n : len)) == 0)
645 {
646 showmap(mp, TRUE);
647 did_local = TRUE;
648 }
649 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200650 }
651 }
652 }
653 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200654
Bram Moolenaar459fd782019-10-13 16:43:39 +0200655 // Find an entry in the maphash[] list that matches.
656 // For :unmap we may loop two times: once to try to unmap an entry with
657 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000658 // an entry with a matching 'to' part. This was done to allow
659 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
660 // be replaced by "bar" because of the abbreviation.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200661 for (round = 0; (round == 0 || maptype == 1) && round <= 1
662 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200663 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200664 // need to loop over all hash lists
665 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200666 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200667 if (abbrev)
668 {
669 if (hash > 0) // there is only one abbreviation list
670 break;
671 mpp = abbr_table;
672 }
673 else
674 mpp = &(map_table[hash]);
675 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
676 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200677
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200678 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200679 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200680 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200681 mpp = &(mp->m_next);
682 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200683 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200684 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200685 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200686 if (!mp->m_simplified)
687 {
688 showmap(mp, map_table != maphash);
689 did_it = TRUE;
690 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200691 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200692 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200693 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200694 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200695 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200696 n = (int)STRLEN(mp->m_str);
697 p = mp->m_str;
698 }
699 else
700 {
701 n = mp->m_keylen;
702 p = mp->m_keys;
703 }
704 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
705 {
706 if (maptype == 1)
707 {
708 // Delete entry.
709 // Only accept a full match. For abbreviations
710 // we ignore trailing space when matching with
711 // the "lhs", since an abbreviation can't have
712 // trailing space.
713 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200714 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200715 {
716 mpp = &(mp->m_next);
717 continue;
718 }
zeertzjqabeb09b2022-04-26 12:29:43 +0100719 // In keyround for simplified keys, don't unmap
720 // a mapping without m_simplified flag.
Bram Moolenaar87f74102022-04-25 18:59:25 +0100721 if (keyround1_simplified && !mp->m_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100722 break;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200723 // We reset the indicated mode bits. If nothing
724 // is left the entry is deleted below.
725 mp->m_mode &= ~mode;
726 did_it = TRUE; // remember we did something
727 }
728 else if (!hasarg) // show matching entry
729 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200730 if (!mp->m_simplified)
731 {
732 showmap(mp, map_table != maphash);
733 did_it = TRUE;
734 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200735 }
736 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200737 {
738 mpp = &(mp->m_next);
739 continue;
740 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200741 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200742 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200743 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000744 semsg(
745 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200746 p);
747 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000748 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200749 p);
750 retval = 5;
751 goto theend;
752 }
753 else
754 {
755 // new rhs for existing entry
756 mp->m_mode &= ~mode; // remove mode bits
757 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200758 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200759 char_u *newstr = vim_strsave(rhs);
760
761 if (newstr == NULL)
762 {
763 retval = 4; // no mem
764 goto theend;
765 }
766 vim_free(mp->m_str);
767 mp->m_str = newstr;
768 vim_free(mp->m_orig_str);
769 mp->m_orig_str = vim_strsave(orig_rhs);
770 mp->m_noremap = noremap;
771 mp->m_nowait = nowait;
772 mp->m_silent = silent;
773 mp->m_mode = mode;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100774 mp->m_simplified = keyround1_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200775#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200776 mp->m_expr = expr;
777 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100778 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200779#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200780 did_it = TRUE;
781 }
782 }
783 if (mp->m_mode == 0) // entry can be deleted
784 {
785 map_free(mpp);
786 continue; // continue with *mpp
787 }
788
789 // May need to put this entry into another hash
790 // list.
791 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
792 if (!abbrev && new_hash != hash)
793 {
794 *mpp = mp->m_next;
795 mp->m_next = map_table[new_hash];
796 map_table[new_hash] = mp;
797
798 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200799 }
800 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200801 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200802 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200803 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200804 }
805 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200806
Bram Moolenaar459fd782019-10-13 16:43:39 +0200807 if (maptype == 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200808 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200809 // delete entry
810 if (!did_it)
zeertzjqa4e33322022-04-24 17:07:53 +0100811 {
Bram Moolenaar87f74102022-04-25 18:59:25 +0100812 if (!keyround1_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100813 retval = 2; // no match
814 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200815 else if (*keys == Ctrl_C)
816 {
817 // If CTRL-C has been unmapped, reuse it for Interrupting.
818 if (map_table == curbuf->b_maphash)
819 curbuf->b_mapped_ctrl_c &= ~mode;
820 else
821 mapped_ctrl_c &= ~mode;
822 }
823 continue;
824 }
825
826 if (!haskey || !hasarg)
827 {
828 // print entries
829 if (!did_it && !did_local)
830 {
831 if (abbrev)
832 msg(_("No abbreviation found"));
833 else
834 msg(_("No mapping found"));
835 }
836 goto theend; // listing finished
837 }
838
839 if (did_it)
840 continue; // have added the new entry already
841
842 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200843 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
844 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200845#ifdef FEAT_EVAL
Bram Moolenaara9528b32022-01-18 20:51:35 +0000846 expr, /* sid */ -1, /* scriptversion */ 0, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200847#endif
Bram Moolenaar87f74102022-04-25 18:59:25 +0100848 keyround1_simplified) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200849 {
850 retval = 4; // no mem
851 goto theend;
852 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200853 }
854
855theend:
856 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200857 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200858 vim_free(arg_buf);
859 return retval;
860}
861
862/*
863 * Get the mapping mode from the command name.
864 */
865 static int
866get_map_mode(char_u **cmdp, int forceit)
867{
868 char_u *p;
869 int modec;
870 int mode;
871
872 p = *cmdp;
873 modec = *p++;
874 if (modec == 'i')
875 mode = INSERT; // :imap
876 else if (modec == 'l')
877 mode = LANGMAP; // :lmap
878 else if (modec == 'c')
879 mode = CMDLINE; // :cmap
880 else if (modec == 'n' && *p != 'o') // avoid :noremap
881 mode = NORMAL; // :nmap
882 else if (modec == 'v')
883 mode = VISUAL + SELECTMODE; // :vmap
884 else if (modec == 'x')
885 mode = VISUAL; // :xmap
886 else if (modec == 's')
887 mode = SELECTMODE; // :smap
888 else if (modec == 'o')
889 mode = OP_PENDING; // :omap
890 else if (modec == 't')
891 mode = TERMINAL; // :tmap
892 else
893 {
894 --p;
895 if (forceit)
896 mode = INSERT + CMDLINE; // :map !
897 else
898 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;// :map
899 }
900
901 *cmdp = p;
902 return mode;
903}
904
905/*
906 * Clear all mappings or abbreviations.
907 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
908 */
909 static void
910map_clear(
911 char_u *cmdp,
912 char_u *arg UNUSED,
913 int forceit,
914 int abbr)
915{
916 int mode;
917 int local;
918
919 local = (STRCMP(arg, "<buffer>") == 0);
920 if (!local && *arg != NUL)
921 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000922 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200923 return;
924 }
925
926 mode = get_map_mode(&cmdp, forceit);
927 map_clear_int(curbuf, mode, local, abbr);
928}
929
930/*
931 * Clear all mappings in "mode".
932 */
933 void
934map_clear_int(
935 buf_T *buf, // buffer for local mappings
936 int mode, // mode in which to delete
937 int local, // TRUE for buffer-local mappings
938 int abbr) // TRUE for abbreviations
939{
940 mapblock_T *mp, **mpp;
941 int hash;
942 int new_hash;
943
944 validate_maphash();
945
946 for (hash = 0; hash < 256; ++hash)
947 {
948 if (abbr)
949 {
950 if (hash > 0) // there is only one abbrlist
951 break;
952 if (local)
953 mpp = &buf->b_first_abbr;
954 else
955 mpp = &first_abbr;
956 }
957 else
958 {
959 if (local)
960 mpp = &buf->b_maphash[hash];
961 else
962 mpp = &maphash[hash];
963 }
964 while (*mpp != NULL)
965 {
966 mp = *mpp;
967 if (mp->m_mode & mode)
968 {
969 mp->m_mode &= ~mode;
970 if (mp->m_mode == 0) // entry can be deleted
971 {
972 map_free(mpp);
973 continue;
974 }
975 // May need to put this entry into another hash list.
976 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
977 if (!abbr && new_hash != hash)
978 {
979 *mpp = mp->m_next;
980 if (local)
981 {
982 mp->m_next = buf->b_maphash[new_hash];
983 buf->b_maphash[new_hash] = mp;
984 }
985 else
986 {
987 mp->m_next = maphash[new_hash];
988 maphash[new_hash] = mp;
989 }
990 continue; // continue with *mpp
991 }
992 }
993 mpp = &(mp->m_next);
994 }
995 }
996}
997
998#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200999 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001000mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001001{
1002 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001003
1004 if (vim_strchr(modechars, 'n') != NULL)
1005 mode |= NORMAL;
1006 if (vim_strchr(modechars, 'v') != NULL)
1007 mode |= VISUAL + SELECTMODE;
1008 if (vim_strchr(modechars, 'x') != NULL)
1009 mode |= VISUAL;
1010 if (vim_strchr(modechars, 's') != NULL)
1011 mode |= SELECTMODE;
1012 if (vim_strchr(modechars, 'o') != NULL)
1013 mode |= OP_PENDING;
1014 if (vim_strchr(modechars, 'i') != NULL)
1015 mode |= INSERT;
1016 if (vim_strchr(modechars, 'l') != NULL)
1017 mode |= LANGMAP;
1018 if (vim_strchr(modechars, 'c') != NULL)
1019 mode |= CMDLINE;
1020
Bram Moolenaar581ba392019-09-03 22:08:33 +02001021 return mode;
1022}
1023
1024/*
1025 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1026 * Recognize termcap codes in "str".
1027 * Also checks mappings local to the current buffer.
1028 */
1029 int
1030map_to_exists(char_u *str, char_u *modechars, int abbr)
1031{
1032 char_u *rhs;
1033 char_u *buf;
1034 int retval;
1035
Bram Moolenaar459fd782019-10-13 16:43:39 +02001036 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001037
1038 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001039 vim_free(buf);
1040
1041 return retval;
1042}
1043#endif
1044
1045/*
1046 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1047 * Also checks mappings local to the current buffer.
1048 */
1049 int
1050map_to_exists_mode(char_u *rhs, int mode, int abbr)
1051{
1052 mapblock_T *mp;
1053 int hash;
1054 int exp_buffer = FALSE;
1055
1056 validate_maphash();
1057
1058 // Do it twice: once for global maps and once for local maps.
1059 for (;;)
1060 {
1061 for (hash = 0; hash < 256; ++hash)
1062 {
1063 if (abbr)
1064 {
1065 if (hash > 0) // there is only one abbr list
1066 break;
1067 if (exp_buffer)
1068 mp = curbuf->b_first_abbr;
1069 else
1070 mp = first_abbr;
1071 }
1072 else if (exp_buffer)
1073 mp = curbuf->b_maphash[hash];
1074 else
1075 mp = maphash[hash];
1076 for (; mp; mp = mp->m_next)
1077 {
1078 if ((mp->m_mode & mode)
1079 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1080 return TRUE;
1081 }
1082 }
1083 if (exp_buffer)
1084 break;
1085 exp_buffer = TRUE;
1086 }
1087
1088 return FALSE;
1089}
1090
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001091/*
1092 * Used below when expanding mapping/abbreviation names.
1093 */
1094static int expand_mapmodes = 0;
1095static int expand_isabbrev = 0;
1096static int expand_buffer = FALSE;
1097
1098/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001099 * Translate an internal mapping/abbreviation representation into the
1100 * corresponding external one recognized by :map/:abbrev commands.
1101 * Respects the current B/k/< settings of 'cpoption'.
1102 *
1103 * This function is called when expanding mappings/abbreviations on the
1104 * command-line.
1105 *
1106 * It uses a growarray to build the translation string since the latter can be
1107 * wider than the original description. The caller has to free the string
1108 * afterwards.
1109 *
1110 * Returns NULL when there is a problem.
1111 */
1112 static char_u *
1113translate_mapping(char_u *str)
1114{
1115 garray_T ga;
1116 int c;
1117 int modifiers;
1118 int cpo_bslash;
1119 int cpo_special;
1120
1121 ga_init(&ga);
1122 ga.ga_itemsize = 1;
1123 ga.ga_growsize = 40;
1124
1125 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1126 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1127
1128 for (; *str; ++str)
1129 {
1130 c = *str;
1131 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1132 {
1133 modifiers = 0;
1134 if (str[1] == KS_MODIFIER)
1135 {
1136 str++;
1137 modifiers = *++str;
1138 c = *++str;
1139 }
1140 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1141 {
1142 if (cpo_special)
1143 {
1144 ga_clear(&ga);
1145 return NULL;
1146 }
1147 c = TO_SPECIAL(str[1], str[2]);
1148 if (c == K_ZERO) // display <Nul> as ^@
1149 c = NUL;
1150 str += 2;
1151 }
1152 if (IS_SPECIAL(c) || modifiers) // special key
1153 {
1154 if (cpo_special)
1155 {
1156 ga_clear(&ga);
1157 return NULL;
1158 }
1159 ga_concat(&ga, get_special_key_name(c, modifiers));
1160 continue; // for (str)
1161 }
1162 }
1163 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1164 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1165 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1166 if (c)
1167 ga_append(&ga, c);
1168 }
1169 ga_append(&ga, NUL);
1170 return (char_u *)(ga.ga_data);
1171}
1172
1173/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001174 * Work out what to complete when doing command line completion of mapping
1175 * or abbreviation names.
1176 */
1177 char_u *
1178set_context_in_map_cmd(
1179 expand_T *xp,
1180 char_u *cmd,
1181 char_u *arg,
1182 int forceit, // TRUE if '!' given
1183 int isabbrev, // TRUE if abbreviation
1184 int isunmap, // TRUE if unmap/unabbrev command
1185 cmdidx_T cmdidx)
1186{
1187 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1188 xp->xp_context = EXPAND_NOTHING;
1189 else
1190 {
1191 if (isunmap)
1192 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1193 else
1194 {
1195 expand_mapmodes = INSERT + CMDLINE;
1196 if (!isabbrev)
1197 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
1198 }
1199 expand_isabbrev = isabbrev;
1200 xp->xp_context = EXPAND_MAPPINGS;
1201 expand_buffer = FALSE;
1202 for (;;)
1203 {
1204 if (STRNCMP(arg, "<buffer>", 8) == 0)
1205 {
1206 expand_buffer = TRUE;
1207 arg = skipwhite(arg + 8);
1208 continue;
1209 }
1210 if (STRNCMP(arg, "<unique>", 8) == 0)
1211 {
1212 arg = skipwhite(arg + 8);
1213 continue;
1214 }
1215 if (STRNCMP(arg, "<nowait>", 8) == 0)
1216 {
1217 arg = skipwhite(arg + 8);
1218 continue;
1219 }
1220 if (STRNCMP(arg, "<silent>", 8) == 0)
1221 {
1222 arg = skipwhite(arg + 8);
1223 continue;
1224 }
1225 if (STRNCMP(arg, "<special>", 9) == 0)
1226 {
1227 arg = skipwhite(arg + 9);
1228 continue;
1229 }
1230#ifdef FEAT_EVAL
1231 if (STRNCMP(arg, "<script>", 8) == 0)
1232 {
1233 arg = skipwhite(arg + 8);
1234 continue;
1235 }
1236 if (STRNCMP(arg, "<expr>", 6) == 0)
1237 {
1238 arg = skipwhite(arg + 6);
1239 continue;
1240 }
1241#endif
1242 break;
1243 }
1244 xp->xp_pattern = arg;
1245 }
1246
1247 return NULL;
1248}
1249
1250/*
1251 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1252 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1253 * Return OK if matches found, FAIL otherwise.
1254 */
1255 int
1256ExpandMappings(
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001257 char_u *pat,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001258 regmatch_T *regmatch,
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001259 int *numMatches,
1260 char_u ***matches)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001261{
1262 mapblock_T *mp;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001263 garray_T ga;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001264 int hash;
1265 int count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001266 char_u *p;
1267 int i;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001268 int fuzzy;
1269 int match;
1270 int score;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001271 fuzmatch_str_T *fuzmatch;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001272
1273 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001274
1275 validate_maphash();
1276
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001277 *numMatches = 0; // return values in case of FAIL
1278 *matches = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001279
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001280 if (!fuzzy)
1281 ga_init2(&ga, sizeof(char *), 3);
1282 else
1283 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001284
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001285 // First search in map modifier arguments
1286 for (i = 0; i < 7; ++i)
1287 {
1288 if (i == 0)
1289 p = (char_u *)"<silent>";
1290 else if (i == 1)
1291 p = (char_u *)"<unique>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001292#ifdef FEAT_EVAL
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001293 else if (i == 2)
1294 p = (char_u *)"<script>";
1295 else if (i == 3)
1296 p = (char_u *)"<expr>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001297#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001298 else if (i == 4 && !expand_buffer)
1299 p = (char_u *)"<buffer>";
1300 else if (i == 5)
1301 p = (char_u *)"<nowait>";
1302 else if (i == 6)
1303 p = (char_u *)"<special>";
1304 else
1305 continue;
1306
1307 if (!fuzzy)
1308 match = vim_regexec(regmatch, p, (colnr_T)0);
1309 else
1310 {
1311 score = fuzzy_match_str(p, pat);
1312 match = (score != 0);
1313 }
1314
1315 if (!match)
1316 continue;
1317
1318 if (ga_grow(&ga, 1) == FAIL)
1319 break;
1320
1321 if (fuzzy)
1322 {
1323 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1324 fuzmatch->idx = ga.ga_len;
1325 fuzmatch->str = vim_strsave(p);
1326 fuzmatch->score = score;
1327 }
1328 else
1329 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
1330 ++ga.ga_len;
1331 }
1332
1333 for (hash = 0; hash < 256; ++hash)
1334 {
1335 if (expand_isabbrev)
1336 {
1337 if (hash > 0) // only one abbrev list
1338 break; // for (hash)
1339 mp = first_abbr;
1340 }
1341 else if (expand_buffer)
1342 mp = curbuf->b_maphash[hash];
1343 else
1344 mp = maphash[hash];
1345 for (; mp; mp = mp->m_next)
1346 {
1347 if (!(mp->m_mode & expand_mapmodes))
1348 continue;
1349
1350 p = translate_mapping(mp->m_keys);
1351 if (p == NULL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001352 continue;
1353
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001354 if (!fuzzy)
1355 match = vim_regexec(regmatch, p, (colnr_T)0);
1356 else
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001357 {
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001358 score = fuzzy_match_str(p, pat);
1359 match = (score != 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001360 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001361
1362 if (!match)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001363 {
1364 vim_free(p);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001365 continue;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001366 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001367
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001368 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001369 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001370 vim_free(p);
1371 break;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001372 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001373
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001374 if (fuzzy)
1375 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001376 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1377 fuzmatch->idx = ga.ga_len;
1378 fuzmatch->str = p;
1379 fuzmatch->score = score;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001380 }
1381 else
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001382 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001383
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001384 ++ga.ga_len;
1385 } // for (mp)
1386 } // for (hash)
1387
1388 if (ga.ga_len == 0)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001389 return FAIL;
1390
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001391 if (!fuzzy)
1392 {
1393 *matches = ga.ga_data;
1394 *numMatches = ga.ga_len;
1395 }
1396 else
1397 {
1398 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
1399 FALSE) == FAIL)
1400 return FAIL;
1401 *numMatches = ga.ga_len;
1402 }
1403
1404 count = *numMatches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001405 if (count > 1)
1406 {
1407 char_u **ptr1;
1408 char_u **ptr2;
1409 char_u **ptr3;
1410
1411 // Sort the matches
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001412 // Fuzzy matching already sorts the matches
1413 if (!fuzzy)
1414 sort_strings(*matches, count);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001415
1416 // Remove multiple entries
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001417 ptr1 = *matches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001418 ptr2 = ptr1 + 1;
1419 ptr3 = ptr1 + count;
1420
1421 while (ptr2 < ptr3)
1422 {
1423 if (STRCMP(*ptr1, *ptr2))
1424 *++ptr1 = *ptr2++;
1425 else
1426 {
1427 vim_free(*ptr2++);
1428 count--;
1429 }
1430 }
1431 }
1432
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001433 *numMatches = count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001434 return (count == 0 ? FAIL : OK);
1435}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001436
1437/*
1438 * Check for an abbreviation.
1439 * Cursor is at ptr[col].
1440 * When inserting, mincol is where insert started.
1441 * For the command line, mincol is what is to be skipped over.
1442 * "c" is the character typed before check_abbr was called. It may have
1443 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1444 *
1445 * Historic vi practice: The last character of an abbreviation must be an id
1446 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1447 * characters or all non-id characters. This allows for abbr. "#i" to
1448 * "#include".
1449 *
1450 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1451 * Then there must be white space before the abbr.
1452 *
1453 * return TRUE if there is an abbreviation, FALSE if not
1454 */
1455 int
1456check_abbr(
1457 int c,
1458 char_u *ptr,
1459 int col,
1460 int mincol)
1461{
1462 int len;
1463 int scol; // starting column of the abbr.
1464 int j;
1465 char_u *s;
1466 char_u tb[MB_MAXBYTES + 4];
1467 mapblock_T *mp;
1468 mapblock_T *mp2;
1469 int clen = 0; // length in characters
1470 int is_id = TRUE;
1471 int vim_abbr;
1472
1473 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1474 return FALSE;
1475
1476 // no remapping implies no abbreviation, except for CTRL-]
1477 if (noremap_keys() && c != Ctrl_RSB)
1478 return FALSE;
1479
1480 // Check for word before the cursor: If it ends in a keyword char all
1481 // chars before it must be keyword chars or non-keyword chars, but not
1482 // white space. If it ends in a non-keyword char we accept any characters
1483 // before it except white space.
1484 if (col == 0) // cannot be an abbr.
1485 return FALSE;
1486
1487 if (has_mbyte)
1488 {
1489 char_u *p;
1490
1491 p = mb_prevptr(ptr, ptr + col);
1492 if (!vim_iswordp(p))
1493 vim_abbr = TRUE; // Vim added abbr.
1494 else
1495 {
1496 vim_abbr = FALSE; // vi compatible abbr.
1497 if (p > ptr)
1498 is_id = vim_iswordp(mb_prevptr(ptr, p));
1499 }
1500 clen = 1;
1501 while (p > ptr + mincol)
1502 {
1503 p = mb_prevptr(ptr, p);
1504 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1505 {
1506 p += (*mb_ptr2len)(p);
1507 break;
1508 }
1509 ++clen;
1510 }
1511 scol = (int)(p - ptr);
1512 }
1513 else
1514 {
1515 if (!vim_iswordc(ptr[col - 1]))
1516 vim_abbr = TRUE; // Vim added abbr.
1517 else
1518 {
1519 vim_abbr = FALSE; // vi compatible abbr.
1520 if (col > 1)
1521 is_id = vim_iswordc(ptr[col - 2]);
1522 }
1523 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1524 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1525 ;
1526 }
1527
1528 if (scol < mincol)
1529 scol = mincol;
1530 if (scol < col) // there is a word in front of the cursor
1531 {
1532 ptr += scol;
1533 len = col - scol;
1534 mp = curbuf->b_first_abbr;
1535 mp2 = first_abbr;
1536 if (mp == NULL)
1537 {
1538 mp = mp2;
1539 mp2 = NULL;
1540 }
1541 for ( ; mp; mp->m_next == NULL
1542 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1543 {
1544 int qlen = mp->m_keylen;
1545 char_u *q = mp->m_keys;
1546 int match;
1547
1548 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1549 {
1550 char_u *qe = vim_strsave(mp->m_keys);
1551
1552 // might have CSI escaped mp->m_keys
1553 if (qe != NULL)
1554 {
1555 q = qe;
1556 vim_unescape_csi(q);
1557 qlen = (int)STRLEN(q);
1558 }
1559 }
1560
1561 // find entries with right mode and keys
1562 match = (mp->m_mode & State)
1563 && qlen == len
1564 && !STRNCMP(q, ptr, (size_t)len);
1565 if (q != mp->m_keys)
1566 vim_free(q);
1567 if (match)
1568 break;
1569 }
1570 if (mp != NULL)
1571 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001572 int noremap;
1573 int silent;
1574#ifdef FEAT_EVAL
1575 int expr;
1576#endif
1577
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001578 // Found a match:
1579 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1580 // This goes from end to start.
1581 //
1582 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1583 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1584 // Characters where IS_SPECIAL() == TRUE: key codes, need
1585 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1586 //
1587 // Character CTRL-] is treated specially - it completes the
1588 // abbreviation, but is not inserted into the input stream.
1589 j = 0;
1590 if (c != Ctrl_RSB)
1591 {
1592 // special key code, split up
1593 if (IS_SPECIAL(c) || c == K_SPECIAL)
1594 {
1595 tb[j++] = K_SPECIAL;
1596 tb[j++] = K_SECOND(c);
1597 tb[j++] = K_THIRD(c);
1598 }
1599 else
1600 {
1601 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1602 tb[j++] = Ctrl_V; // special char needs CTRL-V
1603 if (has_mbyte)
1604 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001605 int newlen;
1606 char_u *escaped;
1607
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001608 // if ABBR_OFF has been added, remove it here
1609 if (c >= ABBR_OFF)
1610 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001611 newlen = (*mb_char2bytes)(c, tb + j);
1612 tb[j + newlen] = NUL;
1613 // Need to escape K_SPECIAL.
1614 escaped = vim_strsave_escape_csi(tb + j);
1615 if (escaped != NULL)
1616 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001617 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001618 mch_memmove(tb + j, escaped, newlen);
1619 j += newlen;
1620 vim_free(escaped);
1621 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001622 }
1623 else
1624 tb[j++] = c;
1625 }
1626 tb[j] = NUL;
1627 // insert the last typed char
1628 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1629 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001630
1631 // copy values here, calling eval_map_expr() may make "mp" invalid!
1632 noremap = mp->m_noremap;
1633 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001634#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001635 expr = mp->m_expr;
1636
1637 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001638 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001639 else
1640#endif
1641 s = mp->m_str;
1642 if (s != NULL)
1643 {
1644 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001645 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001646 // no abbrev. for these chars
1647 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1648#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001649 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001650 vim_free(s);
1651#endif
1652 }
1653
1654 tb[0] = Ctrl_H;
1655 tb[1] = NUL;
1656 if (has_mbyte)
1657 len = clen; // Delete characters instead of bytes
1658 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001659 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001660 return TRUE;
1661 }
1662 }
1663 return FALSE;
1664}
1665
1666#ifdef FEAT_EVAL
1667/*
1668 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1669 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001670 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001671 */
1672 char_u *
1673eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001674 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001675 int c) // NUL or typed character for abbreviation
1676{
1677 char_u *res;
1678 char_u *p;
1679 char_u *expr;
1680 pos_T save_cursor;
1681 int save_msg_col;
1682 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001683 scid_T save_sctx_sid = current_sctx.sc_sid;
1684 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001685
1686 // Remove escaping of CSI, because "str" is in a format to be used as
1687 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001688 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001689 if (expr == NULL)
1690 return NULL;
1691 vim_unescape_csi(expr);
1692
1693 // Forbid changing text or using ":normal" to avoid most of the bad side
1694 // effects. Also restore the cursor position.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001695 ++textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001696 ++ex_normal_lock;
1697 set_vim_var_char(c); // set v:char to the typed character
1698 save_cursor = curwin->w_cursor;
1699 save_msg_col = msg_col;
1700 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001701 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1702 {
1703 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1704 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1705 }
1706
1707 // Note: the evaluation may make "mp" invalid.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001708 p = eval_to_string(expr, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001709
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001710 --textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001711 --ex_normal_lock;
1712 curwin->w_cursor = save_cursor;
1713 msg_col = save_msg_col;
1714 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001715 current_sctx.sc_sid = save_sctx_sid;
1716 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001717
1718 vim_free(expr);
1719
1720 if (p == NULL)
1721 return NULL;
1722 // Escape CSI in the result to be able to use the string as typeahead.
1723 res = vim_strsave_escape_csi(p);
1724 vim_free(p);
1725
1726 return res;
1727}
1728#endif
1729
1730/*
1731 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1732 * can be put in the typeahead buffer.
1733 * Returns NULL when out of memory.
1734 */
1735 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001736vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001737{
1738 char_u *res;
1739 char_u *s, *d;
1740
1741 // Need a buffer to hold up to three times as much. Four in case of an
1742 // illegal utf-8 byte:
1743 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1744 res = alloc(STRLEN(p) * 4 + 1);
1745 if (res != NULL)
1746 {
1747 d = res;
1748 for (s = p; *s != NUL; )
1749 {
1750 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1751 {
1752 // Copy special key unmodified.
1753 *d++ = *s++;
1754 *d++ = *s++;
1755 *d++ = *s++;
1756 }
1757 else
1758 {
1759 // Add character, possibly multi-byte to destination, escaping
1760 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1761 d = add_char2buf(PTR2CHAR(s), d);
1762 s += MB_CPTR2LEN(s);
1763 }
1764 }
1765 *d = NUL;
1766 }
1767 return res;
1768}
1769
1770/*
1771 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1772 * vim_strsave_escape_csi(). Works in-place.
1773 */
1774 void
1775vim_unescape_csi(char_u *p)
1776{
1777 char_u *s = p, *d = p;
1778
1779 while (*s != NUL)
1780 {
1781 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1782 {
1783 *d++ = K_SPECIAL;
1784 s += 3;
1785 }
1786 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1787 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1788 {
1789 *d++ = CSI;
1790 s += 3;
1791 }
1792 else
1793 *d++ = *s++;
1794 }
1795 *d = NUL;
1796}
1797
1798/*
1799 * Write map commands for the current mappings to an .exrc file.
1800 * Return FAIL on error, OK otherwise.
1801 */
1802 int
1803makemap(
1804 FILE *fd,
1805 buf_T *buf) // buffer for local mappings or NULL
1806{
1807 mapblock_T *mp;
1808 char_u c1, c2, c3;
1809 char_u *p;
1810 char *cmd;
1811 int abbr;
1812 int hash;
1813 int did_cpo = FALSE;
1814 int i;
1815
1816 validate_maphash();
1817
1818 // Do the loop twice: Once for mappings, once for abbreviations.
1819 // Then loop over all map hash lists.
1820 for (abbr = 0; abbr < 2; ++abbr)
1821 for (hash = 0; hash < 256; ++hash)
1822 {
1823 if (abbr)
1824 {
1825 if (hash > 0) // there is only one abbr list
1826 break;
1827 if (buf != NULL)
1828 mp = buf->b_first_abbr;
1829 else
1830 mp = first_abbr;
1831 }
1832 else
1833 {
1834 if (buf != NULL)
1835 mp = buf->b_maphash[hash];
1836 else
1837 mp = maphash[hash];
1838 }
1839
1840 for ( ; mp; mp = mp->m_next)
1841 {
1842 // skip script-local mappings
1843 if (mp->m_noremap == REMAP_SCRIPT)
1844 continue;
1845
1846 // skip mappings that contain a <SNR> (script-local thing),
1847 // they probably don't work when loaded again
1848 for (p = mp->m_str; *p != NUL; ++p)
1849 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1850 && p[2] == (int)KE_SNR)
1851 break;
1852 if (*p != NUL)
1853 continue;
1854
1855 // It's possible to create a mapping and then ":unmap" certain
1856 // modes. We recreate this here by mapping the individual
1857 // modes, which requires up to three of them.
1858 c1 = NUL;
1859 c2 = NUL;
1860 c3 = NUL;
1861 if (abbr)
1862 cmd = "abbr";
1863 else
1864 cmd = "map";
1865 switch (mp->m_mode)
1866 {
1867 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
1868 break;
1869 case NORMAL:
1870 c1 = 'n';
1871 break;
1872 case VISUAL:
1873 c1 = 'x';
1874 break;
1875 case SELECTMODE:
1876 c1 = 's';
1877 break;
1878 case OP_PENDING:
1879 c1 = 'o';
1880 break;
1881 case NORMAL + VISUAL:
1882 c1 = 'n';
1883 c2 = 'x';
1884 break;
1885 case NORMAL + SELECTMODE:
1886 c1 = 'n';
1887 c2 = 's';
1888 break;
1889 case NORMAL + OP_PENDING:
1890 c1 = 'n';
1891 c2 = 'o';
1892 break;
1893 case VISUAL + SELECTMODE:
1894 c1 = 'v';
1895 break;
1896 case VISUAL + OP_PENDING:
1897 c1 = 'x';
1898 c2 = 'o';
1899 break;
1900 case SELECTMODE + OP_PENDING:
1901 c1 = 's';
1902 c2 = 'o';
1903 break;
1904 case NORMAL + VISUAL + SELECTMODE:
1905 c1 = 'n';
1906 c2 = 'v';
1907 break;
1908 case NORMAL + VISUAL + OP_PENDING:
1909 c1 = 'n';
1910 c2 = 'x';
1911 c3 = 'o';
1912 break;
1913 case NORMAL + SELECTMODE + OP_PENDING:
1914 c1 = 'n';
1915 c2 = 's';
1916 c3 = 'o';
1917 break;
1918 case VISUAL + SELECTMODE + OP_PENDING:
1919 c1 = 'v';
1920 c2 = 'o';
1921 break;
1922 case CMDLINE + INSERT:
1923 if (!abbr)
1924 cmd = "map!";
1925 break;
1926 case CMDLINE:
1927 c1 = 'c';
1928 break;
1929 case INSERT:
1930 c1 = 'i';
1931 break;
1932 case LANGMAP:
1933 c1 = 'l';
1934 break;
1935 case TERMINAL:
1936 c1 = 't';
1937 break;
1938 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001939 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001940 return FAIL;
1941 }
1942 do // do this twice if c2 is set, 3 times with c3
1943 {
1944 // When outputting <> form, need to make sure that 'cpo'
1945 // is set to the Vim default.
1946 if (!did_cpo)
1947 {
1948 if (*mp->m_str == NUL) // will use <Nop>
1949 did_cpo = TRUE;
1950 else
1951 for (i = 0; i < 2; ++i)
1952 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1953 if (*p == K_SPECIAL || *p == NL)
1954 did_cpo = TRUE;
1955 if (did_cpo)
1956 {
1957 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1958 || put_eol(fd) < 0
1959 || fprintf(fd, "set cpo&vim") < 0
1960 || put_eol(fd) < 0)
1961 return FAIL;
1962 }
1963 }
1964 if (c1 && putc(c1, fd) < 0)
1965 return FAIL;
1966 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1967 return FAIL;
1968 if (fputs(cmd, fd) < 0)
1969 return FAIL;
1970 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1971 return FAIL;
1972 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1973 return FAIL;
1974 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1975 return FAIL;
1976#ifdef FEAT_EVAL
1977 if (mp->m_noremap == REMAP_SCRIPT
1978 && fputs("<script>", fd) < 0)
1979 return FAIL;
1980 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1981 return FAIL;
1982#endif
1983
1984 if ( putc(' ', fd) < 0
1985 || put_escstr(fd, mp->m_keys, 0) == FAIL
1986 || putc(' ', fd) < 0
1987 || put_escstr(fd, mp->m_str, 1) == FAIL
1988 || put_eol(fd) < 0)
1989 return FAIL;
1990 c1 = c2;
1991 c2 = c3;
1992 c3 = NUL;
1993 } while (c1 != NUL);
1994 }
1995 }
1996
1997 if (did_cpo)
1998 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
1999 || put_eol(fd) < 0
2000 || fprintf(fd, "unlet s:cpo_save") < 0
2001 || put_eol(fd) < 0)
2002 return FAIL;
2003 return OK;
2004}
2005
2006/*
2007 * write escape string to file
2008 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2009 *
2010 * return FAIL for failure, OK otherwise
2011 */
2012 int
2013put_escstr(FILE *fd, char_u *strstart, int what)
2014{
2015 char_u *str = strstart;
2016 int c;
2017 int modifiers;
2018
2019 // :map xx <Nop>
2020 if (*str == NUL && what == 1)
2021 {
2022 if (fprintf(fd, "<Nop>") < 0)
2023 return FAIL;
2024 return OK;
2025 }
2026
2027 for ( ; *str != NUL; ++str)
2028 {
2029 char_u *p;
2030
2031 // Check for a multi-byte character, which may contain escaped
2032 // K_SPECIAL and CSI bytes
2033 p = mb_unescape(&str);
2034 if (p != NULL)
2035 {
2036 while (*p != NUL)
2037 if (fputc(*p++, fd) < 0)
2038 return FAIL;
2039 --str;
2040 continue;
2041 }
2042
2043 c = *str;
2044 // Special key codes have to be translated to be able to make sense
2045 // when they are read back.
2046 if (c == K_SPECIAL && what != 2)
2047 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002048 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002049 if (str[1] == KS_MODIFIER)
2050 {
2051 modifiers = str[2];
2052 str += 3;
2053 c = *str;
2054 }
2055 if (c == K_SPECIAL)
2056 {
2057 c = TO_SPECIAL(str[1], str[2]);
2058 str += 2;
2059 }
2060 if (IS_SPECIAL(c) || modifiers) // special key
2061 {
2062 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2063 return FAIL;
2064 continue;
2065 }
2066 }
2067
2068 // A '\n' in a map command should be written as <NL>.
2069 // A '\n' in a set command should be written as \^V^J.
2070 if (c == NL)
2071 {
2072 if (what == 2)
2073 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002074 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002075 return FAIL;
2076 }
2077 else
2078 {
2079 if (fprintf(fd, "<NL>") < 0)
2080 return FAIL;
2081 }
2082 continue;
2083 }
2084
2085 // Some characters have to be escaped with CTRL-V to
2086 // prevent them from misinterpreted in DoOneCmd().
2087 // A space, Tab and '"' has to be escaped with a backslash to
2088 // prevent it to be misinterpreted in do_set().
2089 // A space has to be escaped with a CTRL-V when it's at the start of a
2090 // ":map" rhs.
2091 // A '<' has to be escaped with a CTRL-V to prevent it being
2092 // interpreted as the start of a special key name.
2093 // A space in the lhs of a :map needs a CTRL-V.
2094 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2095 {
2096 if (putc('\\', fd) < 0)
2097 return FAIL;
2098 }
2099 else if (c < ' ' || c > '~' || c == '|'
2100 || (what == 0 && c == ' ')
2101 || (what == 1 && str == strstart && c == ' ')
2102 || (what != 2 && c == '<'))
2103 {
2104 if (putc(Ctrl_V, fd) < 0)
2105 return FAIL;
2106 }
2107 if (putc(c, fd) < 0)
2108 return FAIL;
2109 }
2110 return OK;
2111}
2112
2113/*
2114 * Check all mappings for the presence of special key codes.
2115 * Used after ":set term=xxx".
2116 */
2117 void
2118check_map_keycodes(void)
2119{
2120 mapblock_T *mp;
2121 char_u *p;
2122 int i;
2123 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002124 int abbr;
2125 int hash;
2126 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002127 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002128
2129 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002130 // avoids giving error messages
2131 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002132 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002133
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002134 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002135 // mappings/abbreviations with bp == NULL
2136 for (bp = firstbuf; ; bp = bp->b_next)
2137 {
2138 // Do the loop twice: Once for mappings, once for abbreviations.
2139 // Then loop over all map hash lists.
2140 for (abbr = 0; abbr <= 1; ++abbr)
2141 for (hash = 0; hash < 256; ++hash)
2142 {
2143 if (abbr)
2144 {
2145 if (hash) // there is only one abbr list
2146 break;
2147 if (bp != NULL)
2148 mp = bp->b_first_abbr;
2149 else
2150 mp = first_abbr;
2151 }
2152 else
2153 {
2154 if (bp != NULL)
2155 mp = bp->b_maphash[hash];
2156 else
2157 mp = maphash[hash];
2158 }
2159 for ( ; mp != NULL; mp = mp->m_next)
2160 {
2161 for (i = 0; i <= 1; ++i) // do this twice
2162 {
2163 if (i == 0)
2164 p = mp->m_keys; // once for the "from" part
2165 else
2166 p = mp->m_str; // and once for the "to" part
2167 while (*p)
2168 {
2169 if (*p == K_SPECIAL)
2170 {
2171 ++p;
2172 if (*p < 128) // for "normal" tcap entries
2173 {
2174 buf[0] = p[0];
2175 buf[1] = p[1];
2176 buf[2] = NUL;
2177 (void)add_termcap_entry(buf, FALSE);
2178 }
2179 ++p;
2180 }
2181 ++p;
2182 }
2183 }
2184 }
2185 }
2186 if (bp == NULL)
2187 break;
2188 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002189 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002190 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002191}
2192
2193#if defined(FEAT_EVAL) || defined(PROTO)
2194/*
2195 * Check the string "keys" against the lhs of all mappings.
2196 * Return pointer to rhs of mapping (mapblock->m_str).
2197 * NULL when no mapping found.
2198 */
2199 char_u *
2200check_map(
2201 char_u *keys,
2202 int mode,
2203 int exact, // require exact match
2204 int ign_mod, // ignore preceding modifier
2205 int abbr, // do abbreviations
2206 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2207 int *local_ptr) // return: buffer-local mapping or NULL
2208{
2209 int hash;
2210 int len, minlen;
2211 mapblock_T *mp;
2212 char_u *s;
2213 int local;
2214
2215 validate_maphash();
2216
2217 len = (int)STRLEN(keys);
2218 for (local = 1; local >= 0; --local)
2219 // loop over all hash lists
2220 for (hash = 0; hash < 256; ++hash)
2221 {
2222 if (abbr)
2223 {
2224 if (hash > 0) // there is only one list.
2225 break;
2226 if (local)
2227 mp = curbuf->b_first_abbr;
2228 else
2229 mp = first_abbr;
2230 }
2231 else if (local)
2232 mp = curbuf->b_maphash[hash];
2233 else
2234 mp = maphash[hash];
2235 for ( ; mp != NULL; mp = mp->m_next)
2236 {
2237 // skip entries with wrong mode, wrong length and not matching
2238 // ones
2239 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2240 {
2241 if (len > mp->m_keylen)
2242 minlen = mp->m_keylen;
2243 else
2244 minlen = len;
2245 s = mp->m_keys;
2246 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2247 && s[2] != NUL)
2248 {
2249 s += 3;
2250 if (len > mp->m_keylen - 3)
2251 minlen = mp->m_keylen - 3;
2252 }
2253 if (STRNCMP(s, keys, minlen) == 0)
2254 {
2255 if (mp_ptr != NULL)
2256 *mp_ptr = mp;
2257 if (local_ptr != NULL)
2258 *local_ptr = local;
2259 return mp->m_str;
2260 }
2261 }
2262 }
2263 }
2264
2265 return NULL;
2266}
2267
Ernie Rael659c2402022-04-24 18:40:28 +01002268/*
2269 * Fill in the empty dictionary with items as defined by maparg builtin.
2270 */
2271 static void
2272mapblock2dict(
2273 mapblock_T *mp,
2274 dict_T *dict,
2275 char_u *lhsrawalt, // may be NULL
Ernie Rael51d04d12022-05-04 15:40:22 +01002276 int buffer_local, // false if not buffer local mapping
2277 int abbr) // true if abbreviation
Ernie Rael659c2402022-04-24 18:40:28 +01002278{
2279 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2280 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2281
2282 dict_add_string(dict, "lhs", lhs);
2283 vim_free(lhs);
2284 dict_add_string(dict, "lhsraw", mp->m_keys);
2285 if (lhsrawalt)
2286 // Also add the value for the simplified entry.
2287 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2288 dict_add_string(dict, "rhs", mp->m_orig_str);
2289 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2290 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2291 ? 1L : 0L);
2292 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2293 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2294 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2295 dict_add_number(dict, "scriptversion",
2296 (long)mp->m_script_ctx.sc_version);
2297 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2298 dict_add_number(dict, "buffer", (long)buffer_local);
2299 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2300 dict_add_string(dict, "mode", mapmode);
Ernie Rael51d04d12022-05-04 15:40:22 +01002301 dict_add_number(dict, "abbr", abbr ? 1L : 0L);
Ernie Rael659c2402022-04-24 18:40:28 +01002302
2303 vim_free(mapmode);
2304}
2305
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002306 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002307get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2308{
2309 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002310 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002311 char_u *which;
2312 char_u buf[NUMBUFLEN];
2313 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002314 char_u *alt_keys_buf = NULL;
2315 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002316 char_u *rhs;
2317 int mode;
2318 int abbr = FALSE;
2319 int get_dict = FALSE;
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002320 mapblock_T *mp = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002321 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002322 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002323
2324 // return empty string for failure
2325 rettv->v_type = VAR_STRING;
2326 rettv->vval.v_string = NULL;
2327
2328 keys = tv_get_string(&argvars[0]);
2329 if (*keys == NUL)
2330 return;
2331
2332 if (argvars[1].v_type != VAR_UNKNOWN)
2333 {
2334 which = tv_get_string_buf_chk(&argvars[1], buf);
2335 if (argvars[2].v_type != VAR_UNKNOWN)
2336 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002337 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002338 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002339 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002340 }
2341 }
2342 else
2343 which = (char_u *)"";
2344 if (which == NULL)
2345 return;
2346
2347 mode = get_map_mode(&which, 0);
2348
Bram Moolenaar9c652532020-05-24 13:10:18 +02002349 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2350 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2351 &mp, &buffer_local);
2352 if (did_simplify)
2353 {
2354 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002355 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002356 (void)replace_termcodes(keys, &alt_keys_buf,
2357 flags | REPTERM_NO_SIMPLIFY, NULL);
2358 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2359 &buffer_local);
2360 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002361
2362 if (!get_dict)
2363 {
2364 // Return a string.
2365 if (rhs != NULL)
2366 {
2367 if (*rhs == NUL)
2368 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2369 else
2370 rettv->vval.v_string = str2special_save(rhs, FALSE);
2371 }
2372
2373 }
2374 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002375 mapblock2dict(mp, rettv->vval.v_dict,
Ernie Rael51d04d12022-05-04 15:40:22 +01002376 did_simplify ? keys_simplified : NULL,
2377 buffer_local, abbr);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002378
2379 vim_free(keys_buf);
2380 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002381}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002382
2383/*
Ernie Rael09661202022-04-25 14:40:44 +01002384 * "maplist()" function
Ernie Rael659c2402022-04-24 18:40:28 +01002385 */
2386 void
Ernie Rael09661202022-04-25 14:40:44 +01002387f_maplist(typval_T *argvars UNUSED, typval_T *rettv)
Ernie Rael659c2402022-04-24 18:40:28 +01002388{
2389 dict_T *d;
2390 mapblock_T *mp;
2391 int buffer_local;
2392 char_u *keys_buf;
2393 int did_simplify;
2394 int hash;
2395 char_u *lhs;
2396 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Ernie Rael09661202022-04-25 14:40:44 +01002397 int abbr = FALSE;
2398
2399 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2400 return;
2401 if (argvars[0].v_type != VAR_UNKNOWN)
2402 abbr = tv_get_bool(&argvars[0]);
Ernie Rael659c2402022-04-24 18:40:28 +01002403
2404 if (rettv_list_alloc(rettv) != OK)
2405 return;
2406
2407 validate_maphash();
2408
2409 // Do it twice: once for global maps and once for local maps.
2410 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2411 {
2412 for (hash = 0; hash < 256; ++hash)
2413 {
Ernie Rael09661202022-04-25 14:40:44 +01002414 if (abbr)
2415 {
2416 if (hash > 0) // there is only one abbr list
2417 break;
2418 if (buffer_local)
2419 mp = curbuf->b_first_abbr;
2420 else
2421 mp = first_abbr;
2422 }
2423 else if (buffer_local)
Ernie Rael659c2402022-04-24 18:40:28 +01002424 mp = curbuf->b_maphash[hash];
2425 else
2426 mp = maphash[hash];
2427 for (; mp; mp = mp->m_next)
2428 {
2429 if (mp->m_simplified)
2430 continue;
2431 if ((d = dict_alloc()) == NULL)
2432 return;
2433 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2434 return;
2435
2436 keys_buf = NULL;
2437 did_simplify = FALSE;
2438
2439 lhs = str2special_save(mp->m_keys, TRUE);
2440 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2441 vim_free(lhs);
2442
2443 mapblock2dict(mp, d,
Ernie Rael51d04d12022-05-04 15:40:22 +01002444 did_simplify ? keys_buf : NULL,
2445 buffer_local, abbr);
Ernie Rael659c2402022-04-24 18:40:28 +01002446 vim_free(keys_buf);
2447 }
2448 }
2449 }
2450}
2451
2452/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002453 * "maparg()" function
2454 */
2455 void
2456f_maparg(typval_T *argvars, typval_T *rettv)
2457{
2458 if (in_vim9script()
2459 && (check_for_string_arg(argvars, 0) == FAIL
2460 || check_for_opt_string_arg(argvars, 1) == FAIL
2461 || (argvars[1].v_type != VAR_UNKNOWN
2462 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2463 || (argvars[2].v_type != VAR_UNKNOWN
2464 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2465 return;
2466
2467 get_maparg(argvars, rettv, TRUE);
2468}
2469
2470/*
2471 * "mapcheck()" function
2472 */
2473 void
2474f_mapcheck(typval_T *argvars, typval_T *rettv)
2475{
2476 if (in_vim9script()
2477 && (check_for_string_arg(argvars, 0) == FAIL
2478 || check_for_opt_string_arg(argvars, 1) == FAIL
2479 || (argvars[1].v_type != VAR_UNKNOWN
2480 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2481 return;
2482
2483 get_maparg(argvars, rettv, FALSE);
2484}
2485
2486/*
Ernie Rael51d04d12022-05-04 15:40:22 +01002487 * Get the mapping mode from the mode string.
2488 * It may contain multiple characters, eg "nox", or "!", or ' '
2489 * Return 0 if there is an error.
2490 */
2491 static int
2492get_map_mode_string(char_u *mode_string, int abbr)
2493{
2494 char_u *p = mode_string;
2495 int mode = 0;
2496 int tmode;
2497 int modec;
2498 const int MASK_V = VISUAL + SELECTMODE;
2499 const int MASK_MAP = VISUAL + SELECTMODE + NORMAL + OP_PENDING;
2500 const int MASK_BANG = INSERT + CMDLINE;
2501
2502 if (*p == NUL)
2503 p = (char_u *)" "; // compatibility
2504 while ((modec = *p++))
2505 {
2506 switch (modec)
2507 {
2508 case 'i': tmode = INSERT; break;
2509 case 'l': tmode = LANGMAP; break;
2510 case 'c': tmode = CMDLINE; break;
2511 case 'n': tmode = NORMAL; break;
2512 case 'x': tmode = VISUAL; break;
2513 case 's': tmode = SELECTMODE; break;
2514 case 'o': tmode = OP_PENDING; break;
2515 case 't': tmode = TERMINAL; break;
2516 case 'v': tmode = MASK_V; break;
2517 case '!': tmode = MASK_BANG; break;
2518 case ' ': tmode = MASK_MAP; break;
2519 default:
2520 return 0; // error, unknown mode character
2521 }
2522 mode |= tmode;
2523 }
2524 if ((abbr && (mode & ~MASK_BANG) != 0)
2525 || (!abbr && (mode & (mode-1)) != 0 // more than one bit set
2526 && (
2527 // false if multiple bits set in mode and mode is fully
2528 // contained in one mask
2529 !(((mode & MASK_BANG) != 0 && (mode & ~MASK_BANG) == 0)
2530 || ((mode & MASK_MAP) != 0 && (mode & ~MASK_MAP) == 0)))))
2531 return 0;
2532
2533 return mode;
2534}
2535
2536/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002537 * "mapset()" function
2538 */
2539 void
2540f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2541{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002542 char_u *keys_buf = NULL;
2543 char_u *which;
2544 int mode;
2545 char_u buf[NUMBUFLEN];
2546 int is_abbr;
2547 dict_T *d;
2548 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002549 char_u *lhsraw;
2550 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002551 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002552 char_u *orig_rhs;
2553 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002554 int noremap;
2555 int expr;
2556 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002557 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002558 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002559 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002560 linenr_T lnum;
2561 mapblock_T **map_table = maphash;
2562 mapblock_T **abbr_table = &first_abbr;
2563 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002564 char_u *arg;
Ernie Rael51d04d12022-05-04 15:40:22 +01002565 int dict_only;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002566
Ernie Rael51d04d12022-05-04 15:40:22 +01002567 // If first arg is a dict, then that's the only arg permitted.
2568 dict_only = argvars[0].v_type == VAR_DICT;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002569 if (in_vim9script()
Ernie Rael51d04d12022-05-04 15:40:22 +01002570 && (check_for_string_or_dict_arg(argvars, 0) == FAIL
2571 || (dict_only && check_for_unknown_arg(argvars, 1) == FAIL)
2572 || (!dict_only
2573 && (check_for_string_arg(argvars, 0) == FAIL
2574 || check_for_bool_arg(argvars, 1) == FAIL
2575 || check_for_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002576 return;
2577
Ernie Rael51d04d12022-05-04 15:40:22 +01002578 if (dict_only)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002579 {
Ernie Rael51d04d12022-05-04 15:40:22 +01002580 d = argvars[0].vval.v_dict;
2581 which = dict_get_string(d, (char_u *)"mode", FALSE);
2582 is_abbr = dict_get_bool(d, (char_u *)"abbr", -1);
2583 if (which == NULL || is_abbr < 0)
2584 {
2585 emsg(_(e_entries_missing_in_mapset_dict_argument));
2586 return;
2587 }
2588 }
2589 else
2590 {
2591 which = tv_get_string_buf_chk(&argvars[0], buf);
2592 if (which == NULL)
2593 return;
2594 is_abbr = (int)tv_get_bool(&argvars[1]);
2595
2596 if (argvars[2].v_type != VAR_DICT)
2597 {
2598 emsg(_(e_dictionary_required));
2599 return;
2600 }
2601 d = argvars[2].vval.v_dict;
2602 }
2603 mode = get_map_mode_string(which, is_abbr);
2604 if (mode == 0)
2605 {
2606 semsg(_(e_illegal_map_mode_string_str), which);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002607 return;
2608 }
Ernie Rael51d04d12022-05-04 15:40:22 +01002609
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002610
2611 // Get the values in the same order as above in get_maparg().
2612 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002613 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2614 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002615 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002616 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002617 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002618 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002619 return;
2620 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002621 orig_rhs = rhs;
2622 rhs = replace_termcodes(rhs, &arg_buf,
2623 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002624
2625 noremap = dict_get_number(d, (char_u *)"noremap") ? REMAP_NONE: 0;
2626 if (dict_get_number(d, (char_u *)"script") != 0)
2627 noremap = REMAP_SCRIPT;
2628 expr = dict_get_number(d, (char_u *)"expr") != 0;
2629 silent = dict_get_number(d, (char_u *)"silent") != 0;
2630 sid = dict_get_number(d, (char_u *)"sid");
Bram Moolenaara9528b32022-01-18 20:51:35 +00002631 scriptversion = dict_get_number(d, (char_u *)"scriptversion");
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002632 lnum = dict_get_number(d, (char_u *)"lnum");
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002633 buffer = dict_get_number(d, (char_u *)"buffer");
2634 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2635 // mode from the dict is not used
2636
2637 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002638 {
2639 map_table = curbuf->b_maphash;
2640 abbr_table = &curbuf->b_first_abbr;
2641 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002642
2643 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002644 if (buffer)
2645 {
2646 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2647 if (arg == NULL)
2648 return;
2649 STRCPY(arg, "<buffer>");
2650 STRCPY(arg + 8, lhs);
2651 }
2652 else
2653 {
2654 arg = vim_strsave(lhs);
2655 if (arg == NULL)
2656 return;
2657 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002658 do_map(1, arg, mode, is_abbr);
2659 vim_free(arg);
2660
Bram Moolenaar9c652532020-05-24 13:10:18 +02002661 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002662 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002663 if (lhsrawalt != NULL)
2664 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002665 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2666 lnum, 1);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002667 vim_free(keys_buf);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002668 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002669}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002670#endif
2671
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002672
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002673#if defined(MSWIN) || defined(MACOS_X)
2674
2675# define VIS_SEL (VISUAL+SELECTMODE) // abbreviation
2676
2677/*
2678 * Default mappings for some often used keys.
2679 */
2680struct initmap
2681{
2682 char_u *arg;
2683 int mode;
2684};
2685
2686# ifdef FEAT_GUI_MSWIN
2687// Use the Windows (CUA) keybindings. (GUI)
2688static struct initmap initmappings[] =
2689{
2690 // paste, copy and cut
2691 {(char_u *)"<S-Insert> \"*P", NORMAL},
2692 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
2693 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
2694 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2695 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2696 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2697 {(char_u *)"<C-X> \"*d", VIS_SEL},
2698 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2699};
2700# endif
2701
2702# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2703// Use the Windows (CUA) keybindings. (Console)
2704static struct initmap cinitmappings[] =
2705{
2706 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
2707 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
2708 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
2709 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
2710
2711 // paste, copy and cut
2712# ifdef FEAT_CLIPBOARD
2713 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P
2714 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
2715 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O*
2716 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2717 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2718 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2719 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2720# else
2721 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P
2722 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
2723 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O"
2724 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2725 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2726 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2727# endif
2728};
2729# endif
2730
2731# if defined(MACOS_X)
2732static struct initmap initmappings[] =
2733{
2734 // Use the Standard MacOS binding.
2735 // paste, copy and cut
2736 {(char_u *)"<D-v> \"*P", NORMAL},
2737 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
2738 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
2739 {(char_u *)"<D-c> \"*y", VIS_SEL},
2740 {(char_u *)"<D-x> \"*d", VIS_SEL},
2741 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2742};
2743# endif
2744
2745# undef VIS_SEL
2746#endif
2747
2748/*
2749 * Set up default mappings.
2750 */
2751 void
2752init_mappings(void)
2753{
2754#if defined(MSWIN) || defined(MACOS_X)
2755 int i;
2756
2757# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2758# ifdef VIMDLL
2759 if (!gui.starting)
2760# endif
2761 {
K.Takataeeec2542021-06-02 13:28:16 +02002762 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002763 add_map(cinitmappings[i].arg, cinitmappings[i].mode);
2764 }
2765# endif
2766# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002767 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002768 add_map(initmappings[i].arg, initmappings[i].mode);
2769# endif
2770#endif
2771}
2772
2773#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2774 || defined(PROTO)
2775/*
2776 * Add a mapping "map" for mode "mode".
2777 * Need to put string in allocated memory, because do_map() will modify it.
2778 */
2779 void
2780add_map(char_u *map, int mode)
2781{
2782 char_u *s;
2783 char_u *cpo_save = p_cpo;
2784
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002785 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002786 s = vim_strsave(map);
2787 if (s != NULL)
2788 {
2789 (void)do_map(0, s, mode, FALSE);
2790 vim_free(s);
2791 }
2792 p_cpo = cpo_save;
2793}
2794#endif
2795
Bram Moolenaare677df82019-09-02 22:31:11 +02002796#if defined(FEAT_LANGMAP) || defined(PROTO)
2797/*
2798 * Any character has an equivalent 'langmap' character. This is used for
2799 * keyboards that have a special language mode that sends characters above
2800 * 128 (although other characters can be translated too). The "to" field is a
2801 * Vim command character. This avoids having to switch the keyboard back to
2802 * ASCII mode when leaving Insert mode.
2803 *
2804 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2805 * commands.
2806 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2807 * same as langmap_mapchar[] for characters >= 256.
2808 *
2809 * Use growarray for 'langmap' chars >= 256
2810 */
2811typedef struct
2812{
2813 int from;
2814 int to;
2815} langmap_entry_T;
2816
2817static garray_T langmap_mapga;
2818
2819/*
2820 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2821 * field. If not found insert a new entry at the appropriate location.
2822 */
2823 static void
2824langmap_set_entry(int from, int to)
2825{
2826 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2827 int a = 0;
2828 int b = langmap_mapga.ga_len;
2829
2830 // Do a binary search for an existing entry.
2831 while (a != b)
2832 {
2833 int i = (a + b) / 2;
2834 int d = entries[i].from - from;
2835
2836 if (d == 0)
2837 {
2838 entries[i].to = to;
2839 return;
2840 }
2841 if (d < 0)
2842 a = i + 1;
2843 else
2844 b = i;
2845 }
2846
2847 if (ga_grow(&langmap_mapga, 1) != OK)
2848 return; // out of memory
2849
2850 // insert new entry at position "a"
2851 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2852 mch_memmove(entries + 1, entries,
2853 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2854 ++langmap_mapga.ga_len;
2855 entries[0].from = from;
2856 entries[0].to = to;
2857}
2858
2859/*
2860 * Apply 'langmap' to multi-byte character "c" and return the result.
2861 */
2862 int
2863langmap_adjust_mb(int c)
2864{
2865 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2866 int a = 0;
2867 int b = langmap_mapga.ga_len;
2868
2869 while (a != b)
2870 {
2871 int i = (a + b) / 2;
2872 int d = entries[i].from - c;
2873
2874 if (d == 0)
2875 return entries[i].to; // found matching entry
2876 if (d < 0)
2877 a = i + 1;
2878 else
2879 b = i;
2880 }
2881 return c; // no entry found, return "c" unmodified
2882}
2883
2884 void
2885langmap_init(void)
2886{
2887 int i;
2888
2889 for (i = 0; i < 256; i++)
2890 langmap_mapchar[i] = i; // we init with a one-to-one map
2891 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2892}
2893
2894/*
2895 * Called when langmap option is set; the language map can be
2896 * changed at any time!
2897 */
2898 void
2899langmap_set(void)
2900{
2901 char_u *p;
2902 char_u *p2;
2903 int from, to;
2904
2905 ga_clear(&langmap_mapga); // clear the previous map first
2906 langmap_init(); // back to one-to-one map
2907
2908 for (p = p_langmap; p[0] != NUL; )
2909 {
2910 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2911 MB_PTR_ADV(p2))
2912 {
2913 if (p2[0] == '\\' && p2[1] != NUL)
2914 ++p2;
2915 }
2916 if (p2[0] == ';')
2917 ++p2; // abcd;ABCD form, p2 points to A
2918 else
2919 p2 = NULL; // aAbBcCdD form, p2 is NULL
2920 while (p[0])
2921 {
2922 if (p[0] == ',')
2923 {
2924 ++p;
2925 break;
2926 }
2927 if (p[0] == '\\' && p[1] != NUL)
2928 ++p;
2929 from = (*mb_ptr2char)(p);
2930 to = NUL;
2931 if (p2 == NULL)
2932 {
2933 MB_PTR_ADV(p);
2934 if (p[0] != ',')
2935 {
2936 if (p[0] == '\\')
2937 ++p;
2938 to = (*mb_ptr2char)(p);
2939 }
2940 }
2941 else
2942 {
2943 if (p2[0] != ',')
2944 {
2945 if (p2[0] == '\\')
2946 ++p2;
2947 to = (*mb_ptr2char)(p2);
2948 }
2949 }
2950 if (to == NUL)
2951 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002952 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002953 transchar(from));
2954 return;
2955 }
2956
2957 if (from >= 256)
2958 langmap_set_entry(from, to);
2959 else
2960 langmap_mapchar[from & 255] = to;
2961
2962 // Advance to next pair
2963 MB_PTR_ADV(p);
2964 if (p2 != NULL)
2965 {
2966 MB_PTR_ADV(p2);
2967 if (*p == ';')
2968 {
2969 p = p2;
2970 if (p[0] != NUL)
2971 {
2972 if (p[0] != ',')
2973 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002974 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02002975 return;
2976 }
2977 ++p;
2978 }
2979 break;
2980 }
2981 }
2982 }
2983 }
2984}
2985#endif
2986
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002987 static void
2988do_exmap(exarg_T *eap, int isabbrev)
2989{
2990 int mode;
2991 char_u *cmdp;
2992
2993 cmdp = eap->cmd;
2994 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
2995
2996 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
2997 eap->arg, mode, isabbrev))
2998 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002999 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003000 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003001 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
3002 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003003 break;
3004 }
3005}
3006
3007/*
3008 * ":abbreviate" and friends.
3009 */
3010 void
3011ex_abbreviate(exarg_T *eap)
3012{
3013 do_exmap(eap, TRUE); // almost the same as mapping
3014}
3015
3016/*
3017 * ":map" and friends.
3018 */
3019 void
3020ex_map(exarg_T *eap)
3021{
3022 // If we are sourcing .exrc or .vimrc in current directory we
3023 // print the mappings for security reasons.
3024 if (secure)
3025 {
3026 secure = 2;
3027 msg_outtrans(eap->cmd);
3028 msg_putchar('\n');
3029 }
3030 do_exmap(eap, FALSE);
3031}
3032
3033/*
3034 * ":unmap" and friends.
3035 */
3036 void
3037ex_unmap(exarg_T *eap)
3038{
3039 do_exmap(eap, FALSE);
3040}
3041
3042/*
3043 * ":mapclear" and friends.
3044 */
3045 void
3046ex_mapclear(exarg_T *eap)
3047{
3048 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
3049}
3050
3051/*
3052 * ":abclear" and friends.
3053 */
3054 void
3055ex_abclear(exarg_T *eap)
3056{
3057 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
3058}