blob: ad2e8313e1e96ca5f1bdeb87e55e4bb5124d0983 [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
192 {
193 // Remove escaping of CSI, because "m_str" is in a format to be used
194 // as typeahead.
195 char_u *s = vim_strsave(mp->m_str);
196 if (s != NULL)
197 {
198 vim_unescape_csi(s);
199 msg_outtrans_special(s, FALSE, 0);
200 vim_free(s);
201 }
202 }
203#ifdef FEAT_EVAL
204 if (p_verbose > 0)
205 last_set_msg(mp->m_script_ctx);
206#endif
Bram Moolenaard288eaa2022-02-16 18:27:55 +0000207 msg_clr_eos();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200208 out_flush(); // show one line at a time
209}
210
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200211 static int
212map_add(
213 mapblock_T **map_table,
214 mapblock_T **abbr_table,
215 char_u *keys,
216 char_u *rhs,
217 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200218 int noremap,
219 int nowait,
220 int silent,
221 int mode,
222 int is_abbr,
223#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200224 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200225 scid_T sid, // -1 to use current_sctx
Bram Moolenaara9528b32022-01-18 20:51:35 +0000226 int scriptversion,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200227 linenr_T lnum,
228#endif
229 int simplified)
230{
Bram Moolenaar94075b22022-01-18 20:30:39 +0000231 mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200232
233 if (mp == NULL)
234 return FAIL;
235
236 // If CTRL-C has been mapped, don't always use it for Interrupting.
237 if (*keys == Ctrl_C)
238 {
239 if (map_table == curbuf->b_maphash)
240 curbuf->b_mapped_ctrl_c |= mode;
241 else
242 mapped_ctrl_c |= mode;
243 }
244
245 mp->m_keys = vim_strsave(keys);
246 mp->m_str = vim_strsave(rhs);
247 mp->m_orig_str = vim_strsave(orig_rhs);
248 if (mp->m_keys == NULL || mp->m_str == NULL)
249 {
250 vim_free(mp->m_keys);
251 vim_free(mp->m_str);
252 vim_free(mp->m_orig_str);
253 vim_free(mp);
254 return FAIL;
255 }
256 mp->m_keylen = (int)STRLEN(mp->m_keys);
257 mp->m_noremap = noremap;
258 mp->m_nowait = nowait;
259 mp->m_silent = silent;
260 mp->m_mode = mode;
261 mp->m_simplified = simplified;
262#ifdef FEAT_EVAL
263 mp->m_expr = expr;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000264 if (sid > 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200265 {
266 mp->m_script_ctx.sc_sid = sid;
267 mp->m_script_ctx.sc_lnum = lnum;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000268 mp->m_script_ctx.sc_version = scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200269 }
270 else
271 {
272 mp->m_script_ctx = current_sctx;
273 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
274 }
275#endif
276
277 // add the new entry in front of the abbrlist or maphash[] list
278 if (is_abbr)
279 {
280 mp->m_next = *abbr_table;
281 *abbr_table = mp;
282 }
283 else
284 {
285 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
286
287 mp->m_next = map_table[n];
288 map_table[n] = mp;
289 }
290 return OK;
291}
292
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200293/*
294 * map[!] : show all key mappings
295 * map[!] {lhs} : show key mapping for {lhs}
296 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
297 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
298 * unmap[!] {lhs} : remove key mapping for {lhs}
299 * abbr : show all abbreviations
300 * abbr {lhs} : show abbreviations for {lhs}
301 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
302 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
303 * unabbr {lhs} : remove abbreviation for {lhs}
304 *
305 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
306 *
307 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
308 * it will be modified.
309 *
310 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
311 * for :map! mode is INSERT + CMDLINE
312 * for :cmap mode is CMDLINE
313 * for :imap mode is INSERT
314 * for :lmap mode is LANGMAP
315 * for :nmap mode is NORMAL
316 * for :vmap mode is VISUAL + SELECTMODE
317 * for :xmap mode is VISUAL
318 * for :smap mode is SELECTMODE
319 * for :omap mode is OP_PENDING
320 * for :tmap mode is TERMINAL
321 *
322 * for :abbr mode is INSERT + CMDLINE
323 * for :iabbr mode is INSERT
324 * for :cabbr mode is CMDLINE
325 *
326 * Return 0 for success
327 * 1 for invalid arguments
328 * 2 for no match
329 * 4 for out of mem
330 * 5 for entry not unique
331 */
332 int
333do_map(
334 int maptype,
335 char_u *arg,
336 int mode,
337 int abbrev) // not a mapping but an abbreviation
338{
339 char_u *keys;
340 mapblock_T *mp, **mpp;
341 char_u *rhs;
342 char_u *p;
343 int n;
344 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200345 int hasarg;
346 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200347 int do_print;
348 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200349 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200350 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200351 char_u *arg_buf = NULL;
352 int retval = 0;
353 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200354 mapblock_T **abbr_table;
355 mapblock_T **map_table;
356 int unique = FALSE;
357 int nowait = FALSE;
358 int silent = FALSE;
359 int special = FALSE;
360#ifdef FEAT_EVAL
361 int expr = FALSE;
362#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200363 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200364 int noremap;
365 char_u *orig_rhs;
366
367 keys = arg;
368 map_table = maphash;
369 abbr_table = &first_abbr;
370
371 // For ":noremap" don't remap, otherwise do remap.
372 if (maptype == 2)
373 noremap = REMAP_NONE;
374 else
375 noremap = REMAP_YES;
376
377 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
378 // any order.
379 for (;;)
380 {
381 // Check for "<buffer>": mapping local to buffer.
382 if (STRNCMP(keys, "<buffer>", 8) == 0)
383 {
384 keys = skipwhite(keys + 8);
385 map_table = curbuf->b_maphash;
386 abbr_table = &curbuf->b_first_abbr;
387 continue;
388 }
389
390 // Check for "<nowait>": don't wait for more characters.
391 if (STRNCMP(keys, "<nowait>", 8) == 0)
392 {
393 keys = skipwhite(keys + 8);
394 nowait = TRUE;
395 continue;
396 }
397
398 // Check for "<silent>": don't echo commands.
399 if (STRNCMP(keys, "<silent>", 8) == 0)
400 {
401 keys = skipwhite(keys + 8);
402 silent = TRUE;
403 continue;
404 }
405
406 // Check for "<special>": accept special keys in <>
407 if (STRNCMP(keys, "<special>", 9) == 0)
408 {
409 keys = skipwhite(keys + 9);
410 special = TRUE;
411 continue;
412 }
413
414#ifdef FEAT_EVAL
415 // Check for "<script>": remap script-local mappings only
416 if (STRNCMP(keys, "<script>", 8) == 0)
417 {
418 keys = skipwhite(keys + 8);
419 noremap = REMAP_SCRIPT;
420 continue;
421 }
422
423 // Check for "<expr>": {rhs} is an expression.
424 if (STRNCMP(keys, "<expr>", 6) == 0)
425 {
426 keys = skipwhite(keys + 6);
427 expr = TRUE;
428 continue;
429 }
430#endif
431 // Check for "<unique>": don't overwrite an existing mapping.
432 if (STRNCMP(keys, "<unique>", 8) == 0)
433 {
434 keys = skipwhite(keys + 8);
435 unique = TRUE;
436 continue;
437 }
438 break;
439 }
440
441 validate_maphash();
442
443 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
444 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
445 // with :unmap white space is included in the keys, no argument possible.
446 p = keys;
447 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
448 while (*p && (maptype == 1 || !VIM_ISWHITE(*p)))
449 {
450 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
451 p[1] != NUL)
452 ++p; // skip CTRL-V or backslash
453 ++p;
454 }
455 if (*p != NUL)
456 *p++ = NUL;
457
458 p = skipwhite(p);
459 rhs = p;
460 hasarg = (*rhs != NUL);
461 haskey = (*keys != NUL);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200462 do_print = !haskey || (maptype != 1 && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200463
464 // check for :unmap without argument
465 if (maptype == 1 && !haskey)
466 {
467 retval = 1;
468 goto theend;
469 }
470
471 // If mapping has been given as ^V<C_UP> say, then replace the term codes
472 // with the appropriate two bytes. If it is a shifted special key, unshift
473 // it too, giving another two bytes.
474 // replace_termcodes() may move the result to allocated memory, which
475 // needs to be freed later (*keys_buf and *arg_buf).
476 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200477 // If something like <C-H> is simplified to 0x08 then mark it as simplified
478 // and also add a n entry with a modifier, which will work when
479 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200480 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200481 {
482 char_u *new_keys;
483 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
484
485 if (special)
486 flags |= REPTERM_SPECIAL;
487 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
488 if (did_simplify)
489 (void)replace_termcodes(keys, &alt_keys_buf,
490 flags | REPTERM_NO_SIMPLIFY, NULL);
491 keys = new_keys;
492 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200493 orig_rhs = rhs;
494 if (hasarg)
495 {
496 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
497 rhs = (char_u *)"";
498 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200499 rhs = replace_termcodes(rhs, &arg_buf,
500 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200501 }
502
Bram Moolenaar459fd782019-10-13 16:43:39 +0200503 /*
504 * The following is done twice if we have two versions of keys:
505 * "alt_keys_buf" is not NULL.
506 */
507 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200508 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200509 int did_it = FALSE;
510 int did_local = FALSE;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100511 int keyround1_simplified = keyround == 1 && did_simplify;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200512 int round;
513 int hash;
514 int new_hash;
515
516 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200517 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200518 if (alt_keys_buf == NULL)
519 break;
520 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200521 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200522 else if (alt_keys_buf != NULL && do_print)
523 // when printing always use the not-simplified map
524 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200525
Bram Moolenaar459fd782019-10-13 16:43:39 +0200526 // check arguments and translate function keys
527 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200528 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200529 len = (int)STRLEN(keys);
530 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200531 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200532 retval = 1;
533 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200534 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200535
536 if (abbrev && maptype != 1)
537 {
538 // If an abbreviation ends in a keyword character, the
539 // rest must be all keyword-char or all non-keyword-char.
540 // Otherwise we won't be able to find the start of it in a
541 // vi-compatible way.
542 if (has_mbyte)
543 {
544 int first, last;
545 int same = -1;
546
547 first = vim_iswordp(keys);
548 last = first;
549 p = keys + (*mb_ptr2len)(keys);
550 n = 1;
551 while (p < keys + len)
552 {
553 ++n; // nr of (multi-byte) chars
554 last = vim_iswordp(p); // type of last char
555 if (same == -1 && last != first)
556 same = n - 1; // count of same char type
557 p += (*mb_ptr2len)(p);
558 }
559 if (last && n > 2 && same >= 0 && same < n - 1)
560 {
561 retval = 1;
562 goto theend;
563 }
564 }
565 else if (vim_iswordc(keys[len - 1]))
566 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200567 for (n = 0; n < len - 2; ++n)
568 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
569 {
570 retval = 1;
571 goto theend;
572 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200573 // An abbreviation cannot contain white space.
574 for (n = 0; n < len; ++n)
575 if (VIM_ISWHITE(keys[n]))
576 {
577 retval = 1;
578 goto theend;
579 }
580 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200581 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200582
Bram Moolenaar459fd782019-10-13 16:43:39 +0200583 if (haskey && hasarg && abbrev) // if we will add an abbreviation
584 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200585 // no abbreviations
586
Bram Moolenaar459fd782019-10-13 16:43:39 +0200587 if (do_print)
588 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200589
Bram Moolenaar459fd782019-10-13 16:43:39 +0200590 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200591 if (unique && map_table == curbuf->b_maphash
592 && haskey && hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200593 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200594 // need to loop over all global hash lists
595 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200596 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200597 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200598 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200599 if (hash != 0) // there is only one abbreviation list
600 break;
601 mp = first_abbr;
602 }
603 else
604 mp = maphash[hash];
605 for ( ; mp != NULL && !got_int; mp = mp->m_next)
606 {
607 // check entries with the same mode
608 if ((mp->m_mode & mode) != 0
609 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200610 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
611 {
612 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000613 semsg(
614 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200615 mp->m_keys);
616 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000617 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200618 mp->m_keys);
619 retval = 5;
620 goto theend;
621 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200622 }
623 }
624 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200625
Bram Moolenaar459fd782019-10-13 16:43:39 +0200626 // When listing global mappings, also list buffer-local ones here.
627 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200628 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200629 // need to loop over all global hash lists
630 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200631 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200632 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200633 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200634 if (hash != 0) // there is only one abbreviation list
635 break;
636 mp = curbuf->b_first_abbr;
637 }
638 else
639 mp = curbuf->b_maphash[hash];
640 for ( ; mp != NULL && !got_int; mp = mp->m_next)
641 {
642 // check entries with the same mode
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200643 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200644 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200645 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200646 {
647 showmap(mp, TRUE);
648 did_local = TRUE;
649 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200650 else
651 {
652 n = mp->m_keylen;
653 if (STRNCMP(mp->m_keys, keys,
654 (size_t)(n < len ? n : len)) == 0)
655 {
656 showmap(mp, TRUE);
657 did_local = TRUE;
658 }
659 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200660 }
661 }
662 }
663 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200664
Bram Moolenaar459fd782019-10-13 16:43:39 +0200665 // Find an entry in the maphash[] list that matches.
666 // For :unmap we may loop two times: once to try to unmap an entry with
667 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000668 // an entry with a matching 'to' part. This was done to allow
669 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
670 // be replaced by "bar" because of the abbreviation.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200671 for (round = 0; (round == 0 || maptype == 1) && round <= 1
672 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200673 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200674 // need to loop over all hash lists
675 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200676 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200677 if (abbrev)
678 {
679 if (hash > 0) // there is only one abbreviation list
680 break;
681 mpp = abbr_table;
682 }
683 else
684 mpp = &(map_table[hash]);
685 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
686 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200687
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200688 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200689 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200690 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200691 mpp = &(mp->m_next);
692 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200693 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200694 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200695 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200696 if (!mp->m_simplified)
697 {
698 showmap(mp, map_table != maphash);
699 did_it = TRUE;
700 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200701 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200702 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200703 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200704 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200705 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200706 n = (int)STRLEN(mp->m_str);
707 p = mp->m_str;
708 }
709 else
710 {
711 n = mp->m_keylen;
712 p = mp->m_keys;
713 }
714 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
715 {
716 if (maptype == 1)
717 {
718 // Delete entry.
719 // Only accept a full match. For abbreviations
720 // we ignore trailing space when matching with
721 // the "lhs", since an abbreviation can't have
722 // trailing space.
723 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200724 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200725 {
726 mpp = &(mp->m_next);
727 continue;
728 }
zeertzjqabeb09b2022-04-26 12:29:43 +0100729 // In keyround for simplified keys, don't unmap
730 // a mapping without m_simplified flag.
Bram Moolenaar87f74102022-04-25 18:59:25 +0100731 if (keyround1_simplified && !mp->m_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100732 break;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200733 // We reset the indicated mode bits. If nothing
734 // is left the entry is deleted below.
735 mp->m_mode &= ~mode;
736 did_it = TRUE; // remember we did something
737 }
738 else if (!hasarg) // show matching entry
739 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200740 if (!mp->m_simplified)
741 {
742 showmap(mp, map_table != maphash);
743 did_it = TRUE;
744 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200745 }
746 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200747 {
748 mpp = &(mp->m_next);
749 continue;
750 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200751 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200752 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200753 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000754 semsg(
755 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200756 p);
757 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000758 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200759 p);
760 retval = 5;
761 goto theend;
762 }
763 else
764 {
765 // new rhs for existing entry
766 mp->m_mode &= ~mode; // remove mode bits
767 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200768 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200769 char_u *newstr = vim_strsave(rhs);
770
771 if (newstr == NULL)
772 {
773 retval = 4; // no mem
774 goto theend;
775 }
776 vim_free(mp->m_str);
777 mp->m_str = newstr;
778 vim_free(mp->m_orig_str);
779 mp->m_orig_str = vim_strsave(orig_rhs);
780 mp->m_noremap = noremap;
781 mp->m_nowait = nowait;
782 mp->m_silent = silent;
783 mp->m_mode = mode;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100784 mp->m_simplified = keyround1_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200785#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200786 mp->m_expr = expr;
787 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100788 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200789#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200790 did_it = TRUE;
791 }
792 }
793 if (mp->m_mode == 0) // entry can be deleted
794 {
795 map_free(mpp);
796 continue; // continue with *mpp
797 }
798
799 // May need to put this entry into another hash
800 // list.
801 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
802 if (!abbrev && new_hash != hash)
803 {
804 *mpp = mp->m_next;
805 mp->m_next = map_table[new_hash];
806 map_table[new_hash] = mp;
807
808 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200809 }
810 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200811 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200812 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200813 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200814 }
815 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200816
Bram Moolenaar459fd782019-10-13 16:43:39 +0200817 if (maptype == 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200818 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200819 // delete entry
820 if (!did_it)
zeertzjqa4e33322022-04-24 17:07:53 +0100821 {
Bram Moolenaar87f74102022-04-25 18:59:25 +0100822 if (!keyround1_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100823 retval = 2; // no match
824 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200825 else if (*keys == Ctrl_C)
826 {
827 // If CTRL-C has been unmapped, reuse it for Interrupting.
828 if (map_table == curbuf->b_maphash)
829 curbuf->b_mapped_ctrl_c &= ~mode;
830 else
831 mapped_ctrl_c &= ~mode;
832 }
833 continue;
834 }
835
836 if (!haskey || !hasarg)
837 {
838 // print entries
839 if (!did_it && !did_local)
840 {
841 if (abbrev)
842 msg(_("No abbreviation found"));
843 else
844 msg(_("No mapping found"));
845 }
846 goto theend; // listing finished
847 }
848
849 if (did_it)
850 continue; // have added the new entry already
851
852 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200853 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
854 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200855#ifdef FEAT_EVAL
Bram Moolenaara9528b32022-01-18 20:51:35 +0000856 expr, /* sid */ -1, /* scriptversion */ 0, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200857#endif
Bram Moolenaar87f74102022-04-25 18:59:25 +0100858 keyround1_simplified) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200859 {
860 retval = 4; // no mem
861 goto theend;
862 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200863 }
864
865theend:
866 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200867 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200868 vim_free(arg_buf);
869 return retval;
870}
871
872/*
873 * Get the mapping mode from the command name.
874 */
875 static int
876get_map_mode(char_u **cmdp, int forceit)
877{
878 char_u *p;
879 int modec;
880 int mode;
881
882 p = *cmdp;
883 modec = *p++;
884 if (modec == 'i')
885 mode = INSERT; // :imap
886 else if (modec == 'l')
887 mode = LANGMAP; // :lmap
888 else if (modec == 'c')
889 mode = CMDLINE; // :cmap
890 else if (modec == 'n' && *p != 'o') // avoid :noremap
891 mode = NORMAL; // :nmap
892 else if (modec == 'v')
893 mode = VISUAL + SELECTMODE; // :vmap
894 else if (modec == 'x')
895 mode = VISUAL; // :xmap
896 else if (modec == 's')
897 mode = SELECTMODE; // :smap
898 else if (modec == 'o')
899 mode = OP_PENDING; // :omap
900 else if (modec == 't')
901 mode = TERMINAL; // :tmap
902 else
903 {
904 --p;
905 if (forceit)
906 mode = INSERT + CMDLINE; // :map !
907 else
908 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;// :map
909 }
910
911 *cmdp = p;
912 return mode;
913}
914
915/*
916 * Clear all mappings or abbreviations.
917 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
918 */
919 static void
920map_clear(
921 char_u *cmdp,
922 char_u *arg UNUSED,
923 int forceit,
924 int abbr)
925{
926 int mode;
927 int local;
928
929 local = (STRCMP(arg, "<buffer>") == 0);
930 if (!local && *arg != NUL)
931 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000932 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200933 return;
934 }
935
936 mode = get_map_mode(&cmdp, forceit);
937 map_clear_int(curbuf, mode, local, abbr);
938}
939
940/*
941 * Clear all mappings in "mode".
942 */
943 void
944map_clear_int(
945 buf_T *buf, // buffer for local mappings
946 int mode, // mode in which to delete
947 int local, // TRUE for buffer-local mappings
948 int abbr) // TRUE for abbreviations
949{
950 mapblock_T *mp, **mpp;
951 int hash;
952 int new_hash;
953
954 validate_maphash();
955
956 for (hash = 0; hash < 256; ++hash)
957 {
958 if (abbr)
959 {
960 if (hash > 0) // there is only one abbrlist
961 break;
962 if (local)
963 mpp = &buf->b_first_abbr;
964 else
965 mpp = &first_abbr;
966 }
967 else
968 {
969 if (local)
970 mpp = &buf->b_maphash[hash];
971 else
972 mpp = &maphash[hash];
973 }
974 while (*mpp != NULL)
975 {
976 mp = *mpp;
977 if (mp->m_mode & mode)
978 {
979 mp->m_mode &= ~mode;
980 if (mp->m_mode == 0) // entry can be deleted
981 {
982 map_free(mpp);
983 continue;
984 }
985 // May need to put this entry into another hash list.
986 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
987 if (!abbr && new_hash != hash)
988 {
989 *mpp = mp->m_next;
990 if (local)
991 {
992 mp->m_next = buf->b_maphash[new_hash];
993 buf->b_maphash[new_hash] = mp;
994 }
995 else
996 {
997 mp->m_next = maphash[new_hash];
998 maphash[new_hash] = mp;
999 }
1000 continue; // continue with *mpp
1001 }
1002 }
1003 mpp = &(mp->m_next);
1004 }
1005 }
1006}
1007
1008#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001009 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001010mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001011{
1012 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001013
1014 if (vim_strchr(modechars, 'n') != NULL)
1015 mode |= NORMAL;
1016 if (vim_strchr(modechars, 'v') != NULL)
1017 mode |= VISUAL + SELECTMODE;
1018 if (vim_strchr(modechars, 'x') != NULL)
1019 mode |= VISUAL;
1020 if (vim_strchr(modechars, 's') != NULL)
1021 mode |= SELECTMODE;
1022 if (vim_strchr(modechars, 'o') != NULL)
1023 mode |= OP_PENDING;
1024 if (vim_strchr(modechars, 'i') != NULL)
1025 mode |= INSERT;
1026 if (vim_strchr(modechars, 'l') != NULL)
1027 mode |= LANGMAP;
1028 if (vim_strchr(modechars, 'c') != NULL)
1029 mode |= CMDLINE;
1030
Bram Moolenaar581ba392019-09-03 22:08:33 +02001031 return mode;
1032}
1033
1034/*
1035 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1036 * Recognize termcap codes in "str".
1037 * Also checks mappings local to the current buffer.
1038 */
1039 int
1040map_to_exists(char_u *str, char_u *modechars, int abbr)
1041{
1042 char_u *rhs;
1043 char_u *buf;
1044 int retval;
1045
Bram Moolenaar459fd782019-10-13 16:43:39 +02001046 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001047
1048 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001049 vim_free(buf);
1050
1051 return retval;
1052}
1053#endif
1054
1055/*
1056 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1057 * Also checks mappings local to the current buffer.
1058 */
1059 int
1060map_to_exists_mode(char_u *rhs, int mode, int abbr)
1061{
1062 mapblock_T *mp;
1063 int hash;
1064 int exp_buffer = FALSE;
1065
1066 validate_maphash();
1067
1068 // Do it twice: once for global maps and once for local maps.
1069 for (;;)
1070 {
1071 for (hash = 0; hash < 256; ++hash)
1072 {
1073 if (abbr)
1074 {
1075 if (hash > 0) // there is only one abbr list
1076 break;
1077 if (exp_buffer)
1078 mp = curbuf->b_first_abbr;
1079 else
1080 mp = first_abbr;
1081 }
1082 else if (exp_buffer)
1083 mp = curbuf->b_maphash[hash];
1084 else
1085 mp = maphash[hash];
1086 for (; mp; mp = mp->m_next)
1087 {
1088 if ((mp->m_mode & mode)
1089 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1090 return TRUE;
1091 }
1092 }
1093 if (exp_buffer)
1094 break;
1095 exp_buffer = TRUE;
1096 }
1097
1098 return FALSE;
1099}
1100
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001101/*
1102 * Used below when expanding mapping/abbreviation names.
1103 */
1104static int expand_mapmodes = 0;
1105static int expand_isabbrev = 0;
1106static int expand_buffer = FALSE;
1107
1108/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001109 * Translate an internal mapping/abbreviation representation into the
1110 * corresponding external one recognized by :map/:abbrev commands.
1111 * Respects the current B/k/< settings of 'cpoption'.
1112 *
1113 * This function is called when expanding mappings/abbreviations on the
1114 * command-line.
1115 *
1116 * It uses a growarray to build the translation string since the latter can be
1117 * wider than the original description. The caller has to free the string
1118 * afterwards.
1119 *
1120 * Returns NULL when there is a problem.
1121 */
1122 static char_u *
1123translate_mapping(char_u *str)
1124{
1125 garray_T ga;
1126 int c;
1127 int modifiers;
1128 int cpo_bslash;
1129 int cpo_special;
1130
1131 ga_init(&ga);
1132 ga.ga_itemsize = 1;
1133 ga.ga_growsize = 40;
1134
1135 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1136 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1137
1138 for (; *str; ++str)
1139 {
1140 c = *str;
1141 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1142 {
1143 modifiers = 0;
1144 if (str[1] == KS_MODIFIER)
1145 {
1146 str++;
1147 modifiers = *++str;
1148 c = *++str;
1149 }
1150 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1151 {
1152 if (cpo_special)
1153 {
1154 ga_clear(&ga);
1155 return NULL;
1156 }
1157 c = TO_SPECIAL(str[1], str[2]);
1158 if (c == K_ZERO) // display <Nul> as ^@
1159 c = NUL;
1160 str += 2;
1161 }
1162 if (IS_SPECIAL(c) || modifiers) // special key
1163 {
1164 if (cpo_special)
1165 {
1166 ga_clear(&ga);
1167 return NULL;
1168 }
1169 ga_concat(&ga, get_special_key_name(c, modifiers));
1170 continue; // for (str)
1171 }
1172 }
1173 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1174 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1175 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1176 if (c)
1177 ga_append(&ga, c);
1178 }
1179 ga_append(&ga, NUL);
1180 return (char_u *)(ga.ga_data);
1181}
1182
1183/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001184 * Work out what to complete when doing command line completion of mapping
1185 * or abbreviation names.
1186 */
1187 char_u *
1188set_context_in_map_cmd(
1189 expand_T *xp,
1190 char_u *cmd,
1191 char_u *arg,
1192 int forceit, // TRUE if '!' given
1193 int isabbrev, // TRUE if abbreviation
1194 int isunmap, // TRUE if unmap/unabbrev command
1195 cmdidx_T cmdidx)
1196{
1197 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1198 xp->xp_context = EXPAND_NOTHING;
1199 else
1200 {
1201 if (isunmap)
1202 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1203 else
1204 {
1205 expand_mapmodes = INSERT + CMDLINE;
1206 if (!isabbrev)
1207 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
1208 }
1209 expand_isabbrev = isabbrev;
1210 xp->xp_context = EXPAND_MAPPINGS;
1211 expand_buffer = FALSE;
1212 for (;;)
1213 {
1214 if (STRNCMP(arg, "<buffer>", 8) == 0)
1215 {
1216 expand_buffer = TRUE;
1217 arg = skipwhite(arg + 8);
1218 continue;
1219 }
1220 if (STRNCMP(arg, "<unique>", 8) == 0)
1221 {
1222 arg = skipwhite(arg + 8);
1223 continue;
1224 }
1225 if (STRNCMP(arg, "<nowait>", 8) == 0)
1226 {
1227 arg = skipwhite(arg + 8);
1228 continue;
1229 }
1230 if (STRNCMP(arg, "<silent>", 8) == 0)
1231 {
1232 arg = skipwhite(arg + 8);
1233 continue;
1234 }
1235 if (STRNCMP(arg, "<special>", 9) == 0)
1236 {
1237 arg = skipwhite(arg + 9);
1238 continue;
1239 }
1240#ifdef FEAT_EVAL
1241 if (STRNCMP(arg, "<script>", 8) == 0)
1242 {
1243 arg = skipwhite(arg + 8);
1244 continue;
1245 }
1246 if (STRNCMP(arg, "<expr>", 6) == 0)
1247 {
1248 arg = skipwhite(arg + 6);
1249 continue;
1250 }
1251#endif
1252 break;
1253 }
1254 xp->xp_pattern = arg;
1255 }
1256
1257 return NULL;
1258}
1259
1260/*
1261 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1262 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1263 * Return OK if matches found, FAIL otherwise.
1264 */
1265 int
1266ExpandMappings(
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001267 char_u *pat,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001268 regmatch_T *regmatch,
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001269 int *numMatches,
1270 char_u ***matches)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001271{
1272 mapblock_T *mp;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001273 garray_T ga;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001274 int hash;
1275 int count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001276 char_u *p;
1277 int i;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001278 int fuzzy;
1279 int match;
1280 int score;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001281 fuzmatch_str_T *fuzmatch;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001282
1283 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001284
1285 validate_maphash();
1286
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001287 *numMatches = 0; // return values in case of FAIL
1288 *matches = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001289
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001290 if (!fuzzy)
1291 ga_init2(&ga, sizeof(char *), 3);
1292 else
1293 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001294
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001295 // First search in map modifier arguments
1296 for (i = 0; i < 7; ++i)
1297 {
1298 if (i == 0)
1299 p = (char_u *)"<silent>";
1300 else if (i == 1)
1301 p = (char_u *)"<unique>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001302#ifdef FEAT_EVAL
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001303 else if (i == 2)
1304 p = (char_u *)"<script>";
1305 else if (i == 3)
1306 p = (char_u *)"<expr>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001307#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001308 else if (i == 4 && !expand_buffer)
1309 p = (char_u *)"<buffer>";
1310 else if (i == 5)
1311 p = (char_u *)"<nowait>";
1312 else if (i == 6)
1313 p = (char_u *)"<special>";
1314 else
1315 continue;
1316
1317 if (!fuzzy)
1318 match = vim_regexec(regmatch, p, (colnr_T)0);
1319 else
1320 {
1321 score = fuzzy_match_str(p, pat);
1322 match = (score != 0);
1323 }
1324
1325 if (!match)
1326 continue;
1327
1328 if (ga_grow(&ga, 1) == FAIL)
1329 break;
1330
1331 if (fuzzy)
1332 {
1333 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1334 fuzmatch->idx = ga.ga_len;
1335 fuzmatch->str = vim_strsave(p);
1336 fuzmatch->score = score;
1337 }
1338 else
1339 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
1340 ++ga.ga_len;
1341 }
1342
1343 for (hash = 0; hash < 256; ++hash)
1344 {
1345 if (expand_isabbrev)
1346 {
1347 if (hash > 0) // only one abbrev list
1348 break; // for (hash)
1349 mp = first_abbr;
1350 }
1351 else if (expand_buffer)
1352 mp = curbuf->b_maphash[hash];
1353 else
1354 mp = maphash[hash];
1355 for (; mp; mp = mp->m_next)
1356 {
1357 if (!(mp->m_mode & expand_mapmodes))
1358 continue;
1359
1360 p = translate_mapping(mp->m_keys);
1361 if (p == NULL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001362 continue;
1363
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001364 if (!fuzzy)
1365 match = vim_regexec(regmatch, p, (colnr_T)0);
1366 else
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001367 {
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001368 score = fuzzy_match_str(p, pat);
1369 match = (score != 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001370 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001371
1372 if (!match)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001373 {
1374 vim_free(p);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001375 continue;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001376 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001377
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001378 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001379 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001380 vim_free(p);
1381 break;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001382 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001383
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001384 if (fuzzy)
1385 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001386 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1387 fuzmatch->idx = ga.ga_len;
1388 fuzmatch->str = p;
1389 fuzmatch->score = score;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001390 }
1391 else
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001392 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001393
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001394 ++ga.ga_len;
1395 } // for (mp)
1396 } // for (hash)
1397
1398 if (ga.ga_len == 0)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001399 return FAIL;
1400
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001401 if (!fuzzy)
1402 {
1403 *matches = ga.ga_data;
1404 *numMatches = ga.ga_len;
1405 }
1406 else
1407 {
1408 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
1409 FALSE) == FAIL)
1410 return FAIL;
1411 *numMatches = ga.ga_len;
1412 }
1413
1414 count = *numMatches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001415 if (count > 1)
1416 {
1417 char_u **ptr1;
1418 char_u **ptr2;
1419 char_u **ptr3;
1420
1421 // Sort the matches
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001422 // Fuzzy matching already sorts the matches
1423 if (!fuzzy)
1424 sort_strings(*matches, count);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001425
1426 // Remove multiple entries
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001427 ptr1 = *matches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001428 ptr2 = ptr1 + 1;
1429 ptr3 = ptr1 + count;
1430
1431 while (ptr2 < ptr3)
1432 {
1433 if (STRCMP(*ptr1, *ptr2))
1434 *++ptr1 = *ptr2++;
1435 else
1436 {
1437 vim_free(*ptr2++);
1438 count--;
1439 }
1440 }
1441 }
1442
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001443 *numMatches = count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001444 return (count == 0 ? FAIL : OK);
1445}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001446
1447/*
1448 * Check for an abbreviation.
1449 * Cursor is at ptr[col].
1450 * When inserting, mincol is where insert started.
1451 * For the command line, mincol is what is to be skipped over.
1452 * "c" is the character typed before check_abbr was called. It may have
1453 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1454 *
1455 * Historic vi practice: The last character of an abbreviation must be an id
1456 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1457 * characters or all non-id characters. This allows for abbr. "#i" to
1458 * "#include".
1459 *
1460 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1461 * Then there must be white space before the abbr.
1462 *
1463 * return TRUE if there is an abbreviation, FALSE if not
1464 */
1465 int
1466check_abbr(
1467 int c,
1468 char_u *ptr,
1469 int col,
1470 int mincol)
1471{
1472 int len;
1473 int scol; // starting column of the abbr.
1474 int j;
1475 char_u *s;
1476 char_u tb[MB_MAXBYTES + 4];
1477 mapblock_T *mp;
1478 mapblock_T *mp2;
1479 int clen = 0; // length in characters
1480 int is_id = TRUE;
1481 int vim_abbr;
1482
1483 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1484 return FALSE;
1485
1486 // no remapping implies no abbreviation, except for CTRL-]
1487 if (noremap_keys() && c != Ctrl_RSB)
1488 return FALSE;
1489
1490 // Check for word before the cursor: If it ends in a keyword char all
1491 // chars before it must be keyword chars or non-keyword chars, but not
1492 // white space. If it ends in a non-keyword char we accept any characters
1493 // before it except white space.
1494 if (col == 0) // cannot be an abbr.
1495 return FALSE;
1496
1497 if (has_mbyte)
1498 {
1499 char_u *p;
1500
1501 p = mb_prevptr(ptr, ptr + col);
1502 if (!vim_iswordp(p))
1503 vim_abbr = TRUE; // Vim added abbr.
1504 else
1505 {
1506 vim_abbr = FALSE; // vi compatible abbr.
1507 if (p > ptr)
1508 is_id = vim_iswordp(mb_prevptr(ptr, p));
1509 }
1510 clen = 1;
1511 while (p > ptr + mincol)
1512 {
1513 p = mb_prevptr(ptr, p);
1514 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1515 {
1516 p += (*mb_ptr2len)(p);
1517 break;
1518 }
1519 ++clen;
1520 }
1521 scol = (int)(p - ptr);
1522 }
1523 else
1524 {
1525 if (!vim_iswordc(ptr[col - 1]))
1526 vim_abbr = TRUE; // Vim added abbr.
1527 else
1528 {
1529 vim_abbr = FALSE; // vi compatible abbr.
1530 if (col > 1)
1531 is_id = vim_iswordc(ptr[col - 2]);
1532 }
1533 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1534 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1535 ;
1536 }
1537
1538 if (scol < mincol)
1539 scol = mincol;
1540 if (scol < col) // there is a word in front of the cursor
1541 {
1542 ptr += scol;
1543 len = col - scol;
1544 mp = curbuf->b_first_abbr;
1545 mp2 = first_abbr;
1546 if (mp == NULL)
1547 {
1548 mp = mp2;
1549 mp2 = NULL;
1550 }
1551 for ( ; mp; mp->m_next == NULL
1552 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1553 {
1554 int qlen = mp->m_keylen;
1555 char_u *q = mp->m_keys;
1556 int match;
1557
1558 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1559 {
1560 char_u *qe = vim_strsave(mp->m_keys);
1561
1562 // might have CSI escaped mp->m_keys
1563 if (qe != NULL)
1564 {
1565 q = qe;
1566 vim_unescape_csi(q);
1567 qlen = (int)STRLEN(q);
1568 }
1569 }
1570
1571 // find entries with right mode and keys
1572 match = (mp->m_mode & State)
1573 && qlen == len
1574 && !STRNCMP(q, ptr, (size_t)len);
1575 if (q != mp->m_keys)
1576 vim_free(q);
1577 if (match)
1578 break;
1579 }
1580 if (mp != NULL)
1581 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001582 int noremap;
1583 int silent;
1584#ifdef FEAT_EVAL
1585 int expr;
1586#endif
1587
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001588 // Found a match:
1589 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1590 // This goes from end to start.
1591 //
1592 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1593 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1594 // Characters where IS_SPECIAL() == TRUE: key codes, need
1595 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1596 //
1597 // Character CTRL-] is treated specially - it completes the
1598 // abbreviation, but is not inserted into the input stream.
1599 j = 0;
1600 if (c != Ctrl_RSB)
1601 {
1602 // special key code, split up
1603 if (IS_SPECIAL(c) || c == K_SPECIAL)
1604 {
1605 tb[j++] = K_SPECIAL;
1606 tb[j++] = K_SECOND(c);
1607 tb[j++] = K_THIRD(c);
1608 }
1609 else
1610 {
1611 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1612 tb[j++] = Ctrl_V; // special char needs CTRL-V
1613 if (has_mbyte)
1614 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001615 int newlen;
1616 char_u *escaped;
1617
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001618 // if ABBR_OFF has been added, remove it here
1619 if (c >= ABBR_OFF)
1620 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001621 newlen = (*mb_char2bytes)(c, tb + j);
1622 tb[j + newlen] = NUL;
1623 // Need to escape K_SPECIAL.
1624 escaped = vim_strsave_escape_csi(tb + j);
1625 if (escaped != NULL)
1626 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001627 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001628 mch_memmove(tb + j, escaped, newlen);
1629 j += newlen;
1630 vim_free(escaped);
1631 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001632 }
1633 else
1634 tb[j++] = c;
1635 }
1636 tb[j] = NUL;
1637 // insert the last typed char
1638 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1639 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001640
1641 // copy values here, calling eval_map_expr() may make "mp" invalid!
1642 noremap = mp->m_noremap;
1643 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001644#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001645 expr = mp->m_expr;
1646
1647 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001648 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001649 else
1650#endif
1651 s = mp->m_str;
1652 if (s != NULL)
1653 {
1654 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001655 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001656 // no abbrev. for these chars
1657 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1658#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001659 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001660 vim_free(s);
1661#endif
1662 }
1663
1664 tb[0] = Ctrl_H;
1665 tb[1] = NUL;
1666 if (has_mbyte)
1667 len = clen; // Delete characters instead of bytes
1668 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001669 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001670 return TRUE;
1671 }
1672 }
1673 return FALSE;
1674}
1675
1676#ifdef FEAT_EVAL
1677/*
1678 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1679 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001680 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001681 */
1682 char_u *
1683eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001684 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001685 int c) // NUL or typed character for abbreviation
1686{
1687 char_u *res;
1688 char_u *p;
1689 char_u *expr;
1690 pos_T save_cursor;
1691 int save_msg_col;
1692 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001693 scid_T save_sctx_sid = current_sctx.sc_sid;
1694 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001695
1696 // Remove escaping of CSI, because "str" is in a format to be used as
1697 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001698 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001699 if (expr == NULL)
1700 return NULL;
1701 vim_unescape_csi(expr);
1702
1703 // Forbid changing text or using ":normal" to avoid most of the bad side
1704 // effects. Also restore the cursor position.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001705 ++textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001706 ++ex_normal_lock;
1707 set_vim_var_char(c); // set v:char to the typed character
1708 save_cursor = curwin->w_cursor;
1709 save_msg_col = msg_col;
1710 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001711 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1712 {
1713 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1714 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1715 }
1716
1717 // Note: the evaluation may make "mp" invalid.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001718 p = eval_to_string(expr, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001719
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001720 --textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001721 --ex_normal_lock;
1722 curwin->w_cursor = save_cursor;
1723 msg_col = save_msg_col;
1724 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001725 current_sctx.sc_sid = save_sctx_sid;
1726 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001727
1728 vim_free(expr);
1729
1730 if (p == NULL)
1731 return NULL;
1732 // Escape CSI in the result to be able to use the string as typeahead.
1733 res = vim_strsave_escape_csi(p);
1734 vim_free(p);
1735
1736 return res;
1737}
1738#endif
1739
1740/*
1741 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1742 * can be put in the typeahead buffer.
1743 * Returns NULL when out of memory.
1744 */
1745 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001746vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001747{
1748 char_u *res;
1749 char_u *s, *d;
1750
1751 // Need a buffer to hold up to three times as much. Four in case of an
1752 // illegal utf-8 byte:
1753 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1754 res = alloc(STRLEN(p) * 4 + 1);
1755 if (res != NULL)
1756 {
1757 d = res;
1758 for (s = p; *s != NUL; )
1759 {
1760 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1761 {
1762 // Copy special key unmodified.
1763 *d++ = *s++;
1764 *d++ = *s++;
1765 *d++ = *s++;
1766 }
1767 else
1768 {
1769 // Add character, possibly multi-byte to destination, escaping
1770 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1771 d = add_char2buf(PTR2CHAR(s), d);
1772 s += MB_CPTR2LEN(s);
1773 }
1774 }
1775 *d = NUL;
1776 }
1777 return res;
1778}
1779
1780/*
1781 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1782 * vim_strsave_escape_csi(). Works in-place.
1783 */
1784 void
1785vim_unescape_csi(char_u *p)
1786{
1787 char_u *s = p, *d = p;
1788
1789 while (*s != NUL)
1790 {
1791 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1792 {
1793 *d++ = K_SPECIAL;
1794 s += 3;
1795 }
1796 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1797 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1798 {
1799 *d++ = CSI;
1800 s += 3;
1801 }
1802 else
1803 *d++ = *s++;
1804 }
1805 *d = NUL;
1806}
1807
1808/*
1809 * Write map commands for the current mappings to an .exrc file.
1810 * Return FAIL on error, OK otherwise.
1811 */
1812 int
1813makemap(
1814 FILE *fd,
1815 buf_T *buf) // buffer for local mappings or NULL
1816{
1817 mapblock_T *mp;
1818 char_u c1, c2, c3;
1819 char_u *p;
1820 char *cmd;
1821 int abbr;
1822 int hash;
1823 int did_cpo = FALSE;
1824 int i;
1825
1826 validate_maphash();
1827
1828 // Do the loop twice: Once for mappings, once for abbreviations.
1829 // Then loop over all map hash lists.
1830 for (abbr = 0; abbr < 2; ++abbr)
1831 for (hash = 0; hash < 256; ++hash)
1832 {
1833 if (abbr)
1834 {
1835 if (hash > 0) // there is only one abbr list
1836 break;
1837 if (buf != NULL)
1838 mp = buf->b_first_abbr;
1839 else
1840 mp = first_abbr;
1841 }
1842 else
1843 {
1844 if (buf != NULL)
1845 mp = buf->b_maphash[hash];
1846 else
1847 mp = maphash[hash];
1848 }
1849
1850 for ( ; mp; mp = mp->m_next)
1851 {
1852 // skip script-local mappings
1853 if (mp->m_noremap == REMAP_SCRIPT)
1854 continue;
1855
1856 // skip mappings that contain a <SNR> (script-local thing),
1857 // they probably don't work when loaded again
1858 for (p = mp->m_str; *p != NUL; ++p)
1859 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1860 && p[2] == (int)KE_SNR)
1861 break;
1862 if (*p != NUL)
1863 continue;
1864
1865 // It's possible to create a mapping and then ":unmap" certain
1866 // modes. We recreate this here by mapping the individual
1867 // modes, which requires up to three of them.
1868 c1 = NUL;
1869 c2 = NUL;
1870 c3 = NUL;
1871 if (abbr)
1872 cmd = "abbr";
1873 else
1874 cmd = "map";
1875 switch (mp->m_mode)
1876 {
1877 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
1878 break;
1879 case NORMAL:
1880 c1 = 'n';
1881 break;
1882 case VISUAL:
1883 c1 = 'x';
1884 break;
1885 case SELECTMODE:
1886 c1 = 's';
1887 break;
1888 case OP_PENDING:
1889 c1 = 'o';
1890 break;
1891 case NORMAL + VISUAL:
1892 c1 = 'n';
1893 c2 = 'x';
1894 break;
1895 case NORMAL + SELECTMODE:
1896 c1 = 'n';
1897 c2 = 's';
1898 break;
1899 case NORMAL + OP_PENDING:
1900 c1 = 'n';
1901 c2 = 'o';
1902 break;
1903 case VISUAL + SELECTMODE:
1904 c1 = 'v';
1905 break;
1906 case VISUAL + OP_PENDING:
1907 c1 = 'x';
1908 c2 = 'o';
1909 break;
1910 case SELECTMODE + OP_PENDING:
1911 c1 = 's';
1912 c2 = 'o';
1913 break;
1914 case NORMAL + VISUAL + SELECTMODE:
1915 c1 = 'n';
1916 c2 = 'v';
1917 break;
1918 case NORMAL + VISUAL + OP_PENDING:
1919 c1 = 'n';
1920 c2 = 'x';
1921 c3 = 'o';
1922 break;
1923 case NORMAL + SELECTMODE + OP_PENDING:
1924 c1 = 'n';
1925 c2 = 's';
1926 c3 = 'o';
1927 break;
1928 case VISUAL + SELECTMODE + OP_PENDING:
1929 c1 = 'v';
1930 c2 = 'o';
1931 break;
1932 case CMDLINE + INSERT:
1933 if (!abbr)
1934 cmd = "map!";
1935 break;
1936 case CMDLINE:
1937 c1 = 'c';
1938 break;
1939 case INSERT:
1940 c1 = 'i';
1941 break;
1942 case LANGMAP:
1943 c1 = 'l';
1944 break;
1945 case TERMINAL:
1946 c1 = 't';
1947 break;
1948 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001949 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001950 return FAIL;
1951 }
1952 do // do this twice if c2 is set, 3 times with c3
1953 {
1954 // When outputting <> form, need to make sure that 'cpo'
1955 // is set to the Vim default.
1956 if (!did_cpo)
1957 {
1958 if (*mp->m_str == NUL) // will use <Nop>
1959 did_cpo = TRUE;
1960 else
1961 for (i = 0; i < 2; ++i)
1962 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1963 if (*p == K_SPECIAL || *p == NL)
1964 did_cpo = TRUE;
1965 if (did_cpo)
1966 {
1967 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1968 || put_eol(fd) < 0
1969 || fprintf(fd, "set cpo&vim") < 0
1970 || put_eol(fd) < 0)
1971 return FAIL;
1972 }
1973 }
1974 if (c1 && putc(c1, fd) < 0)
1975 return FAIL;
1976 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1977 return FAIL;
1978 if (fputs(cmd, fd) < 0)
1979 return FAIL;
1980 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1981 return FAIL;
1982 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1983 return FAIL;
1984 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1985 return FAIL;
1986#ifdef FEAT_EVAL
1987 if (mp->m_noremap == REMAP_SCRIPT
1988 && fputs("<script>", fd) < 0)
1989 return FAIL;
1990 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1991 return FAIL;
1992#endif
1993
1994 if ( putc(' ', fd) < 0
1995 || put_escstr(fd, mp->m_keys, 0) == FAIL
1996 || putc(' ', fd) < 0
1997 || put_escstr(fd, mp->m_str, 1) == FAIL
1998 || put_eol(fd) < 0)
1999 return FAIL;
2000 c1 = c2;
2001 c2 = c3;
2002 c3 = NUL;
2003 } while (c1 != NUL);
2004 }
2005 }
2006
2007 if (did_cpo)
2008 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
2009 || put_eol(fd) < 0
2010 || fprintf(fd, "unlet s:cpo_save") < 0
2011 || put_eol(fd) < 0)
2012 return FAIL;
2013 return OK;
2014}
2015
2016/*
2017 * write escape string to file
2018 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2019 *
2020 * return FAIL for failure, OK otherwise
2021 */
2022 int
2023put_escstr(FILE *fd, char_u *strstart, int what)
2024{
2025 char_u *str = strstart;
2026 int c;
2027 int modifiers;
2028
2029 // :map xx <Nop>
2030 if (*str == NUL && what == 1)
2031 {
2032 if (fprintf(fd, "<Nop>") < 0)
2033 return FAIL;
2034 return OK;
2035 }
2036
2037 for ( ; *str != NUL; ++str)
2038 {
2039 char_u *p;
2040
2041 // Check for a multi-byte character, which may contain escaped
2042 // K_SPECIAL and CSI bytes
2043 p = mb_unescape(&str);
2044 if (p != NULL)
2045 {
2046 while (*p != NUL)
2047 if (fputc(*p++, fd) < 0)
2048 return FAIL;
2049 --str;
2050 continue;
2051 }
2052
2053 c = *str;
2054 // Special key codes have to be translated to be able to make sense
2055 // when they are read back.
2056 if (c == K_SPECIAL && what != 2)
2057 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002058 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002059 if (str[1] == KS_MODIFIER)
2060 {
2061 modifiers = str[2];
2062 str += 3;
2063 c = *str;
2064 }
2065 if (c == K_SPECIAL)
2066 {
2067 c = TO_SPECIAL(str[1], str[2]);
2068 str += 2;
2069 }
2070 if (IS_SPECIAL(c) || modifiers) // special key
2071 {
2072 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2073 return FAIL;
2074 continue;
2075 }
2076 }
2077
2078 // A '\n' in a map command should be written as <NL>.
2079 // A '\n' in a set command should be written as \^V^J.
2080 if (c == NL)
2081 {
2082 if (what == 2)
2083 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002084 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002085 return FAIL;
2086 }
2087 else
2088 {
2089 if (fprintf(fd, "<NL>") < 0)
2090 return FAIL;
2091 }
2092 continue;
2093 }
2094
2095 // Some characters have to be escaped with CTRL-V to
2096 // prevent them from misinterpreted in DoOneCmd().
2097 // A space, Tab and '"' has to be escaped with a backslash to
2098 // prevent it to be misinterpreted in do_set().
2099 // A space has to be escaped with a CTRL-V when it's at the start of a
2100 // ":map" rhs.
2101 // A '<' has to be escaped with a CTRL-V to prevent it being
2102 // interpreted as the start of a special key name.
2103 // A space in the lhs of a :map needs a CTRL-V.
2104 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2105 {
2106 if (putc('\\', fd) < 0)
2107 return FAIL;
2108 }
2109 else if (c < ' ' || c > '~' || c == '|'
2110 || (what == 0 && c == ' ')
2111 || (what == 1 && str == strstart && c == ' ')
2112 || (what != 2 && c == '<'))
2113 {
2114 if (putc(Ctrl_V, fd) < 0)
2115 return FAIL;
2116 }
2117 if (putc(c, fd) < 0)
2118 return FAIL;
2119 }
2120 return OK;
2121}
2122
2123/*
2124 * Check all mappings for the presence of special key codes.
2125 * Used after ":set term=xxx".
2126 */
2127 void
2128check_map_keycodes(void)
2129{
2130 mapblock_T *mp;
2131 char_u *p;
2132 int i;
2133 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002134 int abbr;
2135 int hash;
2136 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002137 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002138
2139 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002140 // avoids giving error messages
2141 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002142 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002143
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002144 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002145 // mappings/abbreviations with bp == NULL
2146 for (bp = firstbuf; ; bp = bp->b_next)
2147 {
2148 // Do the loop twice: Once for mappings, once for abbreviations.
2149 // Then loop over all map hash lists.
2150 for (abbr = 0; abbr <= 1; ++abbr)
2151 for (hash = 0; hash < 256; ++hash)
2152 {
2153 if (abbr)
2154 {
2155 if (hash) // there is only one abbr list
2156 break;
2157 if (bp != NULL)
2158 mp = bp->b_first_abbr;
2159 else
2160 mp = first_abbr;
2161 }
2162 else
2163 {
2164 if (bp != NULL)
2165 mp = bp->b_maphash[hash];
2166 else
2167 mp = maphash[hash];
2168 }
2169 for ( ; mp != NULL; mp = mp->m_next)
2170 {
2171 for (i = 0; i <= 1; ++i) // do this twice
2172 {
2173 if (i == 0)
2174 p = mp->m_keys; // once for the "from" part
2175 else
2176 p = mp->m_str; // and once for the "to" part
2177 while (*p)
2178 {
2179 if (*p == K_SPECIAL)
2180 {
2181 ++p;
2182 if (*p < 128) // for "normal" tcap entries
2183 {
2184 buf[0] = p[0];
2185 buf[1] = p[1];
2186 buf[2] = NUL;
2187 (void)add_termcap_entry(buf, FALSE);
2188 }
2189 ++p;
2190 }
2191 ++p;
2192 }
2193 }
2194 }
2195 }
2196 if (bp == NULL)
2197 break;
2198 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002199 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002200 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002201}
2202
2203#if defined(FEAT_EVAL) || defined(PROTO)
2204/*
2205 * Check the string "keys" against the lhs of all mappings.
2206 * Return pointer to rhs of mapping (mapblock->m_str).
2207 * NULL when no mapping found.
2208 */
2209 char_u *
2210check_map(
2211 char_u *keys,
2212 int mode,
2213 int exact, // require exact match
2214 int ign_mod, // ignore preceding modifier
2215 int abbr, // do abbreviations
2216 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2217 int *local_ptr) // return: buffer-local mapping or NULL
2218{
2219 int hash;
2220 int len, minlen;
2221 mapblock_T *mp;
2222 char_u *s;
2223 int local;
2224
2225 validate_maphash();
2226
2227 len = (int)STRLEN(keys);
2228 for (local = 1; local >= 0; --local)
2229 // loop over all hash lists
2230 for (hash = 0; hash < 256; ++hash)
2231 {
2232 if (abbr)
2233 {
2234 if (hash > 0) // there is only one list.
2235 break;
2236 if (local)
2237 mp = curbuf->b_first_abbr;
2238 else
2239 mp = first_abbr;
2240 }
2241 else if (local)
2242 mp = curbuf->b_maphash[hash];
2243 else
2244 mp = maphash[hash];
2245 for ( ; mp != NULL; mp = mp->m_next)
2246 {
2247 // skip entries with wrong mode, wrong length and not matching
2248 // ones
2249 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2250 {
2251 if (len > mp->m_keylen)
2252 minlen = mp->m_keylen;
2253 else
2254 minlen = len;
2255 s = mp->m_keys;
2256 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2257 && s[2] != NUL)
2258 {
2259 s += 3;
2260 if (len > mp->m_keylen - 3)
2261 minlen = mp->m_keylen - 3;
2262 }
2263 if (STRNCMP(s, keys, minlen) == 0)
2264 {
2265 if (mp_ptr != NULL)
2266 *mp_ptr = mp;
2267 if (local_ptr != NULL)
2268 *local_ptr = local;
2269 return mp->m_str;
2270 }
2271 }
2272 }
2273 }
2274
2275 return NULL;
2276}
2277
Ernie Rael659c2402022-04-24 18:40:28 +01002278/*
2279 * Fill in the empty dictionary with items as defined by maparg builtin.
2280 */
2281 static void
2282mapblock2dict(
2283 mapblock_T *mp,
2284 dict_T *dict,
2285 char_u *lhsrawalt, // may be NULL
2286 int buffer_local) // false if not buffer local mapping
2287{
2288 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2289 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2290
2291 dict_add_string(dict, "lhs", lhs);
2292 vim_free(lhs);
2293 dict_add_string(dict, "lhsraw", mp->m_keys);
2294 if (lhsrawalt)
2295 // Also add the value for the simplified entry.
2296 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2297 dict_add_string(dict, "rhs", mp->m_orig_str);
2298 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2299 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2300 ? 1L : 0L);
2301 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2302 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2303 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2304 dict_add_number(dict, "scriptversion",
2305 (long)mp->m_script_ctx.sc_version);
2306 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2307 dict_add_number(dict, "buffer", (long)buffer_local);
2308 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2309 dict_add_string(dict, "mode", mapmode);
2310
2311 vim_free(mapmode);
2312}
2313
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002314 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002315get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2316{
2317 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002318 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002319 char_u *which;
2320 char_u buf[NUMBUFLEN];
2321 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002322 char_u *alt_keys_buf = NULL;
2323 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002324 char_u *rhs;
2325 int mode;
2326 int abbr = FALSE;
2327 int get_dict = FALSE;
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002328 mapblock_T *mp = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002329 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002330 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002331
2332 // return empty string for failure
2333 rettv->v_type = VAR_STRING;
2334 rettv->vval.v_string = NULL;
2335
2336 keys = tv_get_string(&argvars[0]);
2337 if (*keys == NUL)
2338 return;
2339
2340 if (argvars[1].v_type != VAR_UNKNOWN)
2341 {
2342 which = tv_get_string_buf_chk(&argvars[1], buf);
2343 if (argvars[2].v_type != VAR_UNKNOWN)
2344 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002345 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002346 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002347 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002348 }
2349 }
2350 else
2351 which = (char_u *)"";
2352 if (which == NULL)
2353 return;
2354
2355 mode = get_map_mode(&which, 0);
2356
Bram Moolenaar9c652532020-05-24 13:10:18 +02002357 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2358 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2359 &mp, &buffer_local);
2360 if (did_simplify)
2361 {
2362 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002363 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002364 (void)replace_termcodes(keys, &alt_keys_buf,
2365 flags | REPTERM_NO_SIMPLIFY, NULL);
2366 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2367 &buffer_local);
2368 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002369
2370 if (!get_dict)
2371 {
2372 // Return a string.
2373 if (rhs != NULL)
2374 {
2375 if (*rhs == NUL)
2376 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2377 else
2378 rettv->vval.v_string = str2special_save(rhs, FALSE);
2379 }
2380
2381 }
2382 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002383 mapblock2dict(mp, rettv->vval.v_dict,
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002384 did_simplify ? keys_simplified : NULL, buffer_local);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002385
2386 vim_free(keys_buf);
2387 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002388}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002389
2390/*
Ernie Rael09661202022-04-25 14:40:44 +01002391 * "maplist()" function
Ernie Rael659c2402022-04-24 18:40:28 +01002392 */
2393 void
Ernie Rael09661202022-04-25 14:40:44 +01002394f_maplist(typval_T *argvars UNUSED, typval_T *rettv)
Ernie Rael659c2402022-04-24 18:40:28 +01002395{
2396 dict_T *d;
2397 mapblock_T *mp;
2398 int buffer_local;
2399 char_u *keys_buf;
2400 int did_simplify;
2401 int hash;
2402 char_u *lhs;
2403 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Ernie Rael09661202022-04-25 14:40:44 +01002404 int abbr = FALSE;
2405
2406 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2407 return;
2408 if (argvars[0].v_type != VAR_UNKNOWN)
2409 abbr = tv_get_bool(&argvars[0]);
Ernie Rael659c2402022-04-24 18:40:28 +01002410
2411 if (rettv_list_alloc(rettv) != OK)
2412 return;
2413
2414 validate_maphash();
2415
2416 // Do it twice: once for global maps and once for local maps.
2417 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2418 {
2419 for (hash = 0; hash < 256; ++hash)
2420 {
Ernie Rael09661202022-04-25 14:40:44 +01002421 if (abbr)
2422 {
2423 if (hash > 0) // there is only one abbr list
2424 break;
2425 if (buffer_local)
2426 mp = curbuf->b_first_abbr;
2427 else
2428 mp = first_abbr;
2429 }
2430 else if (buffer_local)
Ernie Rael659c2402022-04-24 18:40:28 +01002431 mp = curbuf->b_maphash[hash];
2432 else
2433 mp = maphash[hash];
2434 for (; mp; mp = mp->m_next)
2435 {
2436 if (mp->m_simplified)
2437 continue;
2438 if ((d = dict_alloc()) == NULL)
2439 return;
2440 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2441 return;
2442
2443 keys_buf = NULL;
2444 did_simplify = FALSE;
2445
2446 lhs = str2special_save(mp->m_keys, TRUE);
2447 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2448 vim_free(lhs);
2449
2450 mapblock2dict(mp, d,
2451 did_simplify ? keys_buf : NULL, buffer_local);
2452 vim_free(keys_buf);
2453 }
2454 }
2455 }
2456}
2457
2458/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002459 * "maparg()" function
2460 */
2461 void
2462f_maparg(typval_T *argvars, typval_T *rettv)
2463{
2464 if (in_vim9script()
2465 && (check_for_string_arg(argvars, 0) == FAIL
2466 || check_for_opt_string_arg(argvars, 1) == FAIL
2467 || (argvars[1].v_type != VAR_UNKNOWN
2468 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2469 || (argvars[2].v_type != VAR_UNKNOWN
2470 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2471 return;
2472
2473 get_maparg(argvars, rettv, TRUE);
2474}
2475
2476/*
2477 * "mapcheck()" function
2478 */
2479 void
2480f_mapcheck(typval_T *argvars, typval_T *rettv)
2481{
2482 if (in_vim9script()
2483 && (check_for_string_arg(argvars, 0) == FAIL
2484 || check_for_opt_string_arg(argvars, 1) == FAIL
2485 || (argvars[1].v_type != VAR_UNKNOWN
2486 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2487 return;
2488
2489 get_maparg(argvars, rettv, FALSE);
2490}
2491
2492/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002493 * "mapset()" function
2494 */
2495 void
2496f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2497{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002498 char_u *keys_buf = NULL;
2499 char_u *which;
2500 int mode;
2501 char_u buf[NUMBUFLEN];
2502 int is_abbr;
2503 dict_T *d;
2504 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002505 char_u *lhsraw;
2506 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002507 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002508 char_u *orig_rhs;
2509 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002510 int noremap;
2511 int expr;
2512 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002513 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002514 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002515 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002516 linenr_T lnum;
2517 mapblock_T **map_table = maphash;
2518 mapblock_T **abbr_table = &first_abbr;
2519 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002520 char_u *arg;
2521
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002522 if (in_vim9script()
2523 && (check_for_string_arg(argvars, 0) == FAIL
2524 || check_for_bool_arg(argvars, 1) == FAIL
2525 || check_for_dict_arg(argvars, 2) == FAIL))
2526 return;
2527
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002528 which = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar1b912982020-09-29 21:45:41 +02002529 if (which == NULL)
2530 return;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002531 mode = get_map_mode(&which, 0);
Bram Moolenaar74273e62020-10-01 21:37:21 +02002532 is_abbr = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002533
2534 if (argvars[2].v_type != VAR_DICT)
2535 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002536 emsg(_(e_key_not_present_in_dictionary));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002537 return;
2538 }
2539 d = argvars[2].vval.v_dict;
2540
2541 // Get the values in the same order as above in get_maparg().
2542 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002543 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2544 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002545 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002546 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002547 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002548 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002549 return;
2550 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002551 orig_rhs = rhs;
2552 rhs = replace_termcodes(rhs, &arg_buf,
2553 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002554
2555 noremap = dict_get_number(d, (char_u *)"noremap") ? REMAP_NONE: 0;
2556 if (dict_get_number(d, (char_u *)"script") != 0)
2557 noremap = REMAP_SCRIPT;
2558 expr = dict_get_number(d, (char_u *)"expr") != 0;
2559 silent = dict_get_number(d, (char_u *)"silent") != 0;
2560 sid = dict_get_number(d, (char_u *)"sid");
Bram Moolenaara9528b32022-01-18 20:51:35 +00002561 scriptversion = dict_get_number(d, (char_u *)"scriptversion");
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002562 lnum = dict_get_number(d, (char_u *)"lnum");
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002563 buffer = dict_get_number(d, (char_u *)"buffer");
2564 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2565 // mode from the dict is not used
2566
2567 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002568 {
2569 map_table = curbuf->b_maphash;
2570 abbr_table = &curbuf->b_first_abbr;
2571 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002572
2573 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002574 if (buffer)
2575 {
2576 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2577 if (arg == NULL)
2578 return;
2579 STRCPY(arg, "<buffer>");
2580 STRCPY(arg + 8, lhs);
2581 }
2582 else
2583 {
2584 arg = vim_strsave(lhs);
2585 if (arg == NULL)
2586 return;
2587 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002588 do_map(1, arg, mode, is_abbr);
2589 vim_free(arg);
2590
Bram Moolenaar9c652532020-05-24 13:10:18 +02002591 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002592 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002593 if (lhsrawalt != NULL)
2594 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002595 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2596 lnum, 1);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002597 vim_free(keys_buf);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002598 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002599}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002600#endif
2601
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002602
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002603#if defined(MSWIN) || defined(MACOS_X)
2604
2605# define VIS_SEL (VISUAL+SELECTMODE) // abbreviation
2606
2607/*
2608 * Default mappings for some often used keys.
2609 */
2610struct initmap
2611{
2612 char_u *arg;
2613 int mode;
2614};
2615
2616# ifdef FEAT_GUI_MSWIN
2617// Use the Windows (CUA) keybindings. (GUI)
2618static struct initmap initmappings[] =
2619{
2620 // paste, copy and cut
2621 {(char_u *)"<S-Insert> \"*P", NORMAL},
2622 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
2623 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
2624 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2625 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2626 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2627 {(char_u *)"<C-X> \"*d", VIS_SEL},
2628 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2629};
2630# endif
2631
2632# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2633// Use the Windows (CUA) keybindings. (Console)
2634static struct initmap cinitmappings[] =
2635{
2636 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
2637 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
2638 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
2639 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
2640
2641 // paste, copy and cut
2642# ifdef FEAT_CLIPBOARD
2643 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P
2644 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
2645 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O*
2646 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2647 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2648 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2649 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2650# else
2651 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P
2652 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
2653 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O"
2654 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2655 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2656 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2657# endif
2658};
2659# endif
2660
2661# if defined(MACOS_X)
2662static struct initmap initmappings[] =
2663{
2664 // Use the Standard MacOS binding.
2665 // paste, copy and cut
2666 {(char_u *)"<D-v> \"*P", NORMAL},
2667 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
2668 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
2669 {(char_u *)"<D-c> \"*y", VIS_SEL},
2670 {(char_u *)"<D-x> \"*d", VIS_SEL},
2671 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2672};
2673# endif
2674
2675# undef VIS_SEL
2676#endif
2677
2678/*
2679 * Set up default mappings.
2680 */
2681 void
2682init_mappings(void)
2683{
2684#if defined(MSWIN) || defined(MACOS_X)
2685 int i;
2686
2687# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2688# ifdef VIMDLL
2689 if (!gui.starting)
2690# endif
2691 {
K.Takataeeec2542021-06-02 13:28:16 +02002692 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002693 add_map(cinitmappings[i].arg, cinitmappings[i].mode);
2694 }
2695# endif
2696# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002697 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002698 add_map(initmappings[i].arg, initmappings[i].mode);
2699# endif
2700#endif
2701}
2702
2703#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2704 || defined(PROTO)
2705/*
2706 * Add a mapping "map" for mode "mode".
2707 * Need to put string in allocated memory, because do_map() will modify it.
2708 */
2709 void
2710add_map(char_u *map, int mode)
2711{
2712 char_u *s;
2713 char_u *cpo_save = p_cpo;
2714
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002715 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002716 s = vim_strsave(map);
2717 if (s != NULL)
2718 {
2719 (void)do_map(0, s, mode, FALSE);
2720 vim_free(s);
2721 }
2722 p_cpo = cpo_save;
2723}
2724#endif
2725
Bram Moolenaare677df82019-09-02 22:31:11 +02002726#if defined(FEAT_LANGMAP) || defined(PROTO)
2727/*
2728 * Any character has an equivalent 'langmap' character. This is used for
2729 * keyboards that have a special language mode that sends characters above
2730 * 128 (although other characters can be translated too). The "to" field is a
2731 * Vim command character. This avoids having to switch the keyboard back to
2732 * ASCII mode when leaving Insert mode.
2733 *
2734 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2735 * commands.
2736 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2737 * same as langmap_mapchar[] for characters >= 256.
2738 *
2739 * Use growarray for 'langmap' chars >= 256
2740 */
2741typedef struct
2742{
2743 int from;
2744 int to;
2745} langmap_entry_T;
2746
2747static garray_T langmap_mapga;
2748
2749/*
2750 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2751 * field. If not found insert a new entry at the appropriate location.
2752 */
2753 static void
2754langmap_set_entry(int from, int to)
2755{
2756 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2757 int a = 0;
2758 int b = langmap_mapga.ga_len;
2759
2760 // Do a binary search for an existing entry.
2761 while (a != b)
2762 {
2763 int i = (a + b) / 2;
2764 int d = entries[i].from - from;
2765
2766 if (d == 0)
2767 {
2768 entries[i].to = to;
2769 return;
2770 }
2771 if (d < 0)
2772 a = i + 1;
2773 else
2774 b = i;
2775 }
2776
2777 if (ga_grow(&langmap_mapga, 1) != OK)
2778 return; // out of memory
2779
2780 // insert new entry at position "a"
2781 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2782 mch_memmove(entries + 1, entries,
2783 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2784 ++langmap_mapga.ga_len;
2785 entries[0].from = from;
2786 entries[0].to = to;
2787}
2788
2789/*
2790 * Apply 'langmap' to multi-byte character "c" and return the result.
2791 */
2792 int
2793langmap_adjust_mb(int c)
2794{
2795 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2796 int a = 0;
2797 int b = langmap_mapga.ga_len;
2798
2799 while (a != b)
2800 {
2801 int i = (a + b) / 2;
2802 int d = entries[i].from - c;
2803
2804 if (d == 0)
2805 return entries[i].to; // found matching entry
2806 if (d < 0)
2807 a = i + 1;
2808 else
2809 b = i;
2810 }
2811 return c; // no entry found, return "c" unmodified
2812}
2813
2814 void
2815langmap_init(void)
2816{
2817 int i;
2818
2819 for (i = 0; i < 256; i++)
2820 langmap_mapchar[i] = i; // we init with a one-to-one map
2821 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2822}
2823
2824/*
2825 * Called when langmap option is set; the language map can be
2826 * changed at any time!
2827 */
2828 void
2829langmap_set(void)
2830{
2831 char_u *p;
2832 char_u *p2;
2833 int from, to;
2834
2835 ga_clear(&langmap_mapga); // clear the previous map first
2836 langmap_init(); // back to one-to-one map
2837
2838 for (p = p_langmap; p[0] != NUL; )
2839 {
2840 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2841 MB_PTR_ADV(p2))
2842 {
2843 if (p2[0] == '\\' && p2[1] != NUL)
2844 ++p2;
2845 }
2846 if (p2[0] == ';')
2847 ++p2; // abcd;ABCD form, p2 points to A
2848 else
2849 p2 = NULL; // aAbBcCdD form, p2 is NULL
2850 while (p[0])
2851 {
2852 if (p[0] == ',')
2853 {
2854 ++p;
2855 break;
2856 }
2857 if (p[0] == '\\' && p[1] != NUL)
2858 ++p;
2859 from = (*mb_ptr2char)(p);
2860 to = NUL;
2861 if (p2 == NULL)
2862 {
2863 MB_PTR_ADV(p);
2864 if (p[0] != ',')
2865 {
2866 if (p[0] == '\\')
2867 ++p;
2868 to = (*mb_ptr2char)(p);
2869 }
2870 }
2871 else
2872 {
2873 if (p2[0] != ',')
2874 {
2875 if (p2[0] == '\\')
2876 ++p2;
2877 to = (*mb_ptr2char)(p2);
2878 }
2879 }
2880 if (to == NUL)
2881 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002882 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002883 transchar(from));
2884 return;
2885 }
2886
2887 if (from >= 256)
2888 langmap_set_entry(from, to);
2889 else
2890 langmap_mapchar[from & 255] = to;
2891
2892 // Advance to next pair
2893 MB_PTR_ADV(p);
2894 if (p2 != NULL)
2895 {
2896 MB_PTR_ADV(p2);
2897 if (*p == ';')
2898 {
2899 p = p2;
2900 if (p[0] != NUL)
2901 {
2902 if (p[0] != ',')
2903 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002904 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02002905 return;
2906 }
2907 ++p;
2908 }
2909 break;
2910 }
2911 }
2912 }
2913 }
2914}
2915#endif
2916
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002917 static void
2918do_exmap(exarg_T *eap, int isabbrev)
2919{
2920 int mode;
2921 char_u *cmdp;
2922
2923 cmdp = eap->cmd;
2924 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
2925
2926 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
2927 eap->arg, mode, isabbrev))
2928 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002929 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002930 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002931 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
2932 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002933 break;
2934 }
2935}
2936
2937/*
2938 * ":abbreviate" and friends.
2939 */
2940 void
2941ex_abbreviate(exarg_T *eap)
2942{
2943 do_exmap(eap, TRUE); // almost the same as mapping
2944}
2945
2946/*
2947 * ":map" and friends.
2948 */
2949 void
2950ex_map(exarg_T *eap)
2951{
2952 // If we are sourcing .exrc or .vimrc in current directory we
2953 // print the mappings for security reasons.
2954 if (secure)
2955 {
2956 secure = 2;
2957 msg_outtrans(eap->cmd);
2958 msg_putchar('\n');
2959 }
2960 do_exmap(eap, FALSE);
2961}
2962
2963/*
2964 * ":unmap" and friends.
2965 */
2966 void
2967ex_unmap(exarg_T *eap)
2968{
2969 do_exmap(eap, FALSE);
2970}
2971
2972/*
2973 * ":mapclear" and friends.
2974 */
2975 void
2976ex_mapclear(exarg_T *eap)
2977{
2978 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
2979}
2980
2981/*
2982 * ":abclear" and friends.
2983 */
2984 void
2985ex_abclear(exarg_T *eap)
2986{
2987 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
2988}