blob: 38f5282899efdbab3f7553759dbca60ea80fd306 [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;
511 int round;
512 int hash;
513 int new_hash;
514
515 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200516 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200517 if (alt_keys_buf == NULL)
518 break;
519 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200520 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200521 else if (alt_keys_buf != NULL && do_print)
522 // when printing always use the not-simplified map
523 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200524
Bram Moolenaar459fd782019-10-13 16:43:39 +0200525 // check arguments and translate function keys
526 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200527 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200528 len = (int)STRLEN(keys);
529 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200530 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200531 retval = 1;
532 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200533 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200534
535 if (abbrev && maptype != 1)
536 {
537 // If an abbreviation ends in a keyword character, the
538 // rest must be all keyword-char or all non-keyword-char.
539 // Otherwise we won't be able to find the start of it in a
540 // vi-compatible way.
541 if (has_mbyte)
542 {
543 int first, last;
544 int same = -1;
545
546 first = vim_iswordp(keys);
547 last = first;
548 p = keys + (*mb_ptr2len)(keys);
549 n = 1;
550 while (p < keys + len)
551 {
552 ++n; // nr of (multi-byte) chars
553 last = vim_iswordp(p); // type of last char
554 if (same == -1 && last != first)
555 same = n - 1; // count of same char type
556 p += (*mb_ptr2len)(p);
557 }
558 if (last && n > 2 && same >= 0 && same < n - 1)
559 {
560 retval = 1;
561 goto theend;
562 }
563 }
564 else if (vim_iswordc(keys[len - 1]))
565 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200566 for (n = 0; n < len - 2; ++n)
567 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
568 {
569 retval = 1;
570 goto theend;
571 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200572 // An abbreviation cannot contain white space.
573 for (n = 0; n < len; ++n)
574 if (VIM_ISWHITE(keys[n]))
575 {
576 retval = 1;
577 goto theend;
578 }
579 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200580 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200581
Bram Moolenaar459fd782019-10-13 16:43:39 +0200582 if (haskey && hasarg && abbrev) // if we will add an abbreviation
583 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200584 // no abbreviations
585
Bram Moolenaar459fd782019-10-13 16:43:39 +0200586 if (do_print)
587 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200588
Bram Moolenaar459fd782019-10-13 16:43:39 +0200589 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200590 if (unique && map_table == curbuf->b_maphash
591 && haskey && hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200592 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200593 // need to loop over all global hash lists
594 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200595 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200596 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200597 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200598 if (hash != 0) // there is only one abbreviation list
599 break;
600 mp = first_abbr;
601 }
602 else
603 mp = maphash[hash];
604 for ( ; mp != NULL && !got_int; mp = mp->m_next)
605 {
606 // check entries with the same mode
607 if ((mp->m_mode & mode) != 0
608 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200609 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
610 {
611 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000612 semsg(
613 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200614 mp->m_keys);
615 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000616 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200617 mp->m_keys);
618 retval = 5;
619 goto theend;
620 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200621 }
622 }
623 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200624
Bram Moolenaar459fd782019-10-13 16:43:39 +0200625 // When listing global mappings, also list buffer-local ones here.
626 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200627 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200628 // need to loop over all global hash lists
629 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200630 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200631 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200632 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200633 if (hash != 0) // there is only one abbreviation list
634 break;
635 mp = curbuf->b_first_abbr;
636 }
637 else
638 mp = curbuf->b_maphash[hash];
639 for ( ; mp != NULL && !got_int; mp = mp->m_next)
640 {
641 // check entries with the same mode
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200642 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200643 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200644 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200645 {
646 showmap(mp, TRUE);
647 did_local = TRUE;
648 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200649 else
650 {
651 n = mp->m_keylen;
652 if (STRNCMP(mp->m_keys, keys,
653 (size_t)(n < len ? n : len)) == 0)
654 {
655 showmap(mp, TRUE);
656 did_local = TRUE;
657 }
658 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200659 }
660 }
661 }
662 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200663
Bram Moolenaar459fd782019-10-13 16:43:39 +0200664 // Find an entry in the maphash[] list that matches.
665 // For :unmap we may loop two times: once to try to unmap an entry with
666 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000667 // an entry with a matching 'to' part. This was done to allow
668 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
669 // be replaced by "bar" because of the abbreviation.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200670 for (round = 0; (round == 0 || maptype == 1) && round <= 1
671 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200672 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200673 // need to loop over all hash lists
674 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200675 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200676 if (abbrev)
677 {
678 if (hash > 0) // there is only one abbreviation list
679 break;
680 mpp = abbr_table;
681 }
682 else
683 mpp = &(map_table[hash]);
684 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
685 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200686
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200687 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200688 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200689 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200690 mpp = &(mp->m_next);
691 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200692 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200693 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200694 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200695 if (!mp->m_simplified)
696 {
697 showmap(mp, map_table != maphash);
698 did_it = TRUE;
699 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200700 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200701 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200702 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200703 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200704 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200705 n = (int)STRLEN(mp->m_str);
706 p = mp->m_str;
707 }
708 else
709 {
710 n = mp->m_keylen;
711 p = mp->m_keys;
712 }
713 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
714 {
715 if (maptype == 1)
716 {
717 // Delete entry.
718 // Only accept a full match. For abbreviations
719 // we ignore trailing space when matching with
720 // the "lhs", since an abbreviation can't have
721 // trailing space.
722 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200723 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200724 {
725 mpp = &(mp->m_next);
726 continue;
727 }
zeertzjqa4e33322022-04-24 17:07:53 +0100728 if (did_simplify && keyround == 1
729 && !mp->m_simplified)
730 break;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200731 // We reset the indicated mode bits. If nothing
732 // is left the entry is deleted below.
733 mp->m_mode &= ~mode;
734 did_it = TRUE; // remember we did something
735 }
736 else if (!hasarg) // show matching entry
737 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200738 if (!mp->m_simplified)
739 {
740 showmap(mp, map_table != maphash);
741 did_it = TRUE;
742 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200743 }
744 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200745 {
746 mpp = &(mp->m_next);
747 continue;
748 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200749 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200750 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200751 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000752 semsg(
753 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200754 p);
755 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000756 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200757 p);
758 retval = 5;
759 goto theend;
760 }
761 else
762 {
763 // new rhs for existing entry
764 mp->m_mode &= ~mode; // remove mode bits
765 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200766 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200767 char_u *newstr = vim_strsave(rhs);
768
769 if (newstr == NULL)
770 {
771 retval = 4; // no mem
772 goto theend;
773 }
774 vim_free(mp->m_str);
775 mp->m_str = newstr;
776 vim_free(mp->m_orig_str);
777 mp->m_orig_str = vim_strsave(orig_rhs);
778 mp->m_noremap = noremap;
779 mp->m_nowait = nowait;
780 mp->m_silent = silent;
781 mp->m_mode = mode;
782 mp->m_simplified =
783 did_simplify && keyround == 1;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200784#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200785 mp->m_expr = expr;
786 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100787 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200788#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200789 did_it = TRUE;
790 }
791 }
792 if (mp->m_mode == 0) // entry can be deleted
793 {
794 map_free(mpp);
795 continue; // continue with *mpp
796 }
797
798 // May need to put this entry into another hash
799 // list.
800 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
801 if (!abbrev && new_hash != hash)
802 {
803 *mpp = mp->m_next;
804 mp->m_next = map_table[new_hash];
805 map_table[new_hash] = mp;
806
807 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200808 }
809 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200810 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200811 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200812 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200813 }
814 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200815
Bram Moolenaar459fd782019-10-13 16:43:39 +0200816 if (maptype == 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200817 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200818 // delete entry
819 if (!did_it)
zeertzjqa4e33322022-04-24 17:07:53 +0100820 {
821 if (!did_simplify || keyround == 2)
822 retval = 2; // no match
823 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200824 else if (*keys == Ctrl_C)
825 {
826 // If CTRL-C has been unmapped, reuse it for Interrupting.
827 if (map_table == curbuf->b_maphash)
828 curbuf->b_mapped_ctrl_c &= ~mode;
829 else
830 mapped_ctrl_c &= ~mode;
831 }
832 continue;
833 }
834
835 if (!haskey || !hasarg)
836 {
837 // print entries
838 if (!did_it && !did_local)
839 {
840 if (abbrev)
841 msg(_("No abbreviation found"));
842 else
843 msg(_("No mapping found"));
844 }
845 goto theend; // listing finished
846 }
847
848 if (did_it)
849 continue; // have added the new entry already
850
851 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200852 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
853 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200854#ifdef FEAT_EVAL
Bram Moolenaara9528b32022-01-18 20:51:35 +0000855 expr, /* sid */ -1, /* scriptversion */ 0, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200856#endif
857 did_simplify && keyround == 1) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200858 {
859 retval = 4; // no mem
860 goto theend;
861 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200862 }
863
864theend:
865 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200866 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200867 vim_free(arg_buf);
868 return retval;
869}
870
871/*
872 * Get the mapping mode from the command name.
873 */
874 static int
875get_map_mode(char_u **cmdp, int forceit)
876{
877 char_u *p;
878 int modec;
879 int mode;
880
881 p = *cmdp;
882 modec = *p++;
883 if (modec == 'i')
884 mode = INSERT; // :imap
885 else if (modec == 'l')
886 mode = LANGMAP; // :lmap
887 else if (modec == 'c')
888 mode = CMDLINE; // :cmap
889 else if (modec == 'n' && *p != 'o') // avoid :noremap
890 mode = NORMAL; // :nmap
891 else if (modec == 'v')
892 mode = VISUAL + SELECTMODE; // :vmap
893 else if (modec == 'x')
894 mode = VISUAL; // :xmap
895 else if (modec == 's')
896 mode = SELECTMODE; // :smap
897 else if (modec == 'o')
898 mode = OP_PENDING; // :omap
899 else if (modec == 't')
900 mode = TERMINAL; // :tmap
901 else
902 {
903 --p;
904 if (forceit)
905 mode = INSERT + CMDLINE; // :map !
906 else
907 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;// :map
908 }
909
910 *cmdp = p;
911 return mode;
912}
913
914/*
915 * Clear all mappings or abbreviations.
916 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
917 */
918 static void
919map_clear(
920 char_u *cmdp,
921 char_u *arg UNUSED,
922 int forceit,
923 int abbr)
924{
925 int mode;
926 int local;
927
928 local = (STRCMP(arg, "<buffer>") == 0);
929 if (!local && *arg != NUL)
930 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000931 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200932 return;
933 }
934
935 mode = get_map_mode(&cmdp, forceit);
936 map_clear_int(curbuf, mode, local, abbr);
937}
938
939/*
940 * Clear all mappings in "mode".
941 */
942 void
943map_clear_int(
944 buf_T *buf, // buffer for local mappings
945 int mode, // mode in which to delete
946 int local, // TRUE for buffer-local mappings
947 int abbr) // TRUE for abbreviations
948{
949 mapblock_T *mp, **mpp;
950 int hash;
951 int new_hash;
952
953 validate_maphash();
954
955 for (hash = 0; hash < 256; ++hash)
956 {
957 if (abbr)
958 {
959 if (hash > 0) // there is only one abbrlist
960 break;
961 if (local)
962 mpp = &buf->b_first_abbr;
963 else
964 mpp = &first_abbr;
965 }
966 else
967 {
968 if (local)
969 mpp = &buf->b_maphash[hash];
970 else
971 mpp = &maphash[hash];
972 }
973 while (*mpp != NULL)
974 {
975 mp = *mpp;
976 if (mp->m_mode & mode)
977 {
978 mp->m_mode &= ~mode;
979 if (mp->m_mode == 0) // entry can be deleted
980 {
981 map_free(mpp);
982 continue;
983 }
984 // May need to put this entry into another hash list.
985 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
986 if (!abbr && new_hash != hash)
987 {
988 *mpp = mp->m_next;
989 if (local)
990 {
991 mp->m_next = buf->b_maphash[new_hash];
992 buf->b_maphash[new_hash] = mp;
993 }
994 else
995 {
996 mp->m_next = maphash[new_hash];
997 maphash[new_hash] = mp;
998 }
999 continue; // continue with *mpp
1000 }
1001 }
1002 mpp = &(mp->m_next);
1003 }
1004 }
1005}
1006
1007#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001008 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001009mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001010{
1011 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001012
1013 if (vim_strchr(modechars, 'n') != NULL)
1014 mode |= NORMAL;
1015 if (vim_strchr(modechars, 'v') != NULL)
1016 mode |= VISUAL + SELECTMODE;
1017 if (vim_strchr(modechars, 'x') != NULL)
1018 mode |= VISUAL;
1019 if (vim_strchr(modechars, 's') != NULL)
1020 mode |= SELECTMODE;
1021 if (vim_strchr(modechars, 'o') != NULL)
1022 mode |= OP_PENDING;
1023 if (vim_strchr(modechars, 'i') != NULL)
1024 mode |= INSERT;
1025 if (vim_strchr(modechars, 'l') != NULL)
1026 mode |= LANGMAP;
1027 if (vim_strchr(modechars, 'c') != NULL)
1028 mode |= CMDLINE;
1029
Bram Moolenaar581ba392019-09-03 22:08:33 +02001030 return mode;
1031}
1032
1033/*
1034 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1035 * Recognize termcap codes in "str".
1036 * Also checks mappings local to the current buffer.
1037 */
1038 int
1039map_to_exists(char_u *str, char_u *modechars, int abbr)
1040{
1041 char_u *rhs;
1042 char_u *buf;
1043 int retval;
1044
Bram Moolenaar459fd782019-10-13 16:43:39 +02001045 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001046
1047 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001048 vim_free(buf);
1049
1050 return retval;
1051}
1052#endif
1053
1054/*
1055 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1056 * Also checks mappings local to the current buffer.
1057 */
1058 int
1059map_to_exists_mode(char_u *rhs, int mode, int abbr)
1060{
1061 mapblock_T *mp;
1062 int hash;
1063 int exp_buffer = FALSE;
1064
1065 validate_maphash();
1066
1067 // Do it twice: once for global maps and once for local maps.
1068 for (;;)
1069 {
1070 for (hash = 0; hash < 256; ++hash)
1071 {
1072 if (abbr)
1073 {
1074 if (hash > 0) // there is only one abbr list
1075 break;
1076 if (exp_buffer)
1077 mp = curbuf->b_first_abbr;
1078 else
1079 mp = first_abbr;
1080 }
1081 else if (exp_buffer)
1082 mp = curbuf->b_maphash[hash];
1083 else
1084 mp = maphash[hash];
1085 for (; mp; mp = mp->m_next)
1086 {
1087 if ((mp->m_mode & mode)
1088 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1089 return TRUE;
1090 }
1091 }
1092 if (exp_buffer)
1093 break;
1094 exp_buffer = TRUE;
1095 }
1096
1097 return FALSE;
1098}
1099
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001100/*
1101 * Used below when expanding mapping/abbreviation names.
1102 */
1103static int expand_mapmodes = 0;
1104static int expand_isabbrev = 0;
1105static int expand_buffer = FALSE;
1106
1107/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001108 * Translate an internal mapping/abbreviation representation into the
1109 * corresponding external one recognized by :map/:abbrev commands.
1110 * Respects the current B/k/< settings of 'cpoption'.
1111 *
1112 * This function is called when expanding mappings/abbreviations on the
1113 * command-line.
1114 *
1115 * It uses a growarray to build the translation string since the latter can be
1116 * wider than the original description. The caller has to free the string
1117 * afterwards.
1118 *
1119 * Returns NULL when there is a problem.
1120 */
1121 static char_u *
1122translate_mapping(char_u *str)
1123{
1124 garray_T ga;
1125 int c;
1126 int modifiers;
1127 int cpo_bslash;
1128 int cpo_special;
1129
1130 ga_init(&ga);
1131 ga.ga_itemsize = 1;
1132 ga.ga_growsize = 40;
1133
1134 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1135 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1136
1137 for (; *str; ++str)
1138 {
1139 c = *str;
1140 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1141 {
1142 modifiers = 0;
1143 if (str[1] == KS_MODIFIER)
1144 {
1145 str++;
1146 modifiers = *++str;
1147 c = *++str;
1148 }
1149 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1150 {
1151 if (cpo_special)
1152 {
1153 ga_clear(&ga);
1154 return NULL;
1155 }
1156 c = TO_SPECIAL(str[1], str[2]);
1157 if (c == K_ZERO) // display <Nul> as ^@
1158 c = NUL;
1159 str += 2;
1160 }
1161 if (IS_SPECIAL(c) || modifiers) // special key
1162 {
1163 if (cpo_special)
1164 {
1165 ga_clear(&ga);
1166 return NULL;
1167 }
1168 ga_concat(&ga, get_special_key_name(c, modifiers));
1169 continue; // for (str)
1170 }
1171 }
1172 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1173 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1174 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1175 if (c)
1176 ga_append(&ga, c);
1177 }
1178 ga_append(&ga, NUL);
1179 return (char_u *)(ga.ga_data);
1180}
1181
1182/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001183 * Work out what to complete when doing command line completion of mapping
1184 * or abbreviation names.
1185 */
1186 char_u *
1187set_context_in_map_cmd(
1188 expand_T *xp,
1189 char_u *cmd,
1190 char_u *arg,
1191 int forceit, // TRUE if '!' given
1192 int isabbrev, // TRUE if abbreviation
1193 int isunmap, // TRUE if unmap/unabbrev command
1194 cmdidx_T cmdidx)
1195{
1196 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1197 xp->xp_context = EXPAND_NOTHING;
1198 else
1199 {
1200 if (isunmap)
1201 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1202 else
1203 {
1204 expand_mapmodes = INSERT + CMDLINE;
1205 if (!isabbrev)
1206 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
1207 }
1208 expand_isabbrev = isabbrev;
1209 xp->xp_context = EXPAND_MAPPINGS;
1210 expand_buffer = FALSE;
1211 for (;;)
1212 {
1213 if (STRNCMP(arg, "<buffer>", 8) == 0)
1214 {
1215 expand_buffer = TRUE;
1216 arg = skipwhite(arg + 8);
1217 continue;
1218 }
1219 if (STRNCMP(arg, "<unique>", 8) == 0)
1220 {
1221 arg = skipwhite(arg + 8);
1222 continue;
1223 }
1224 if (STRNCMP(arg, "<nowait>", 8) == 0)
1225 {
1226 arg = skipwhite(arg + 8);
1227 continue;
1228 }
1229 if (STRNCMP(arg, "<silent>", 8) == 0)
1230 {
1231 arg = skipwhite(arg + 8);
1232 continue;
1233 }
1234 if (STRNCMP(arg, "<special>", 9) == 0)
1235 {
1236 arg = skipwhite(arg + 9);
1237 continue;
1238 }
1239#ifdef FEAT_EVAL
1240 if (STRNCMP(arg, "<script>", 8) == 0)
1241 {
1242 arg = skipwhite(arg + 8);
1243 continue;
1244 }
1245 if (STRNCMP(arg, "<expr>", 6) == 0)
1246 {
1247 arg = skipwhite(arg + 6);
1248 continue;
1249 }
1250#endif
1251 break;
1252 }
1253 xp->xp_pattern = arg;
1254 }
1255
1256 return NULL;
1257}
1258
1259/*
1260 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1261 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1262 * Return OK if matches found, FAIL otherwise.
1263 */
1264 int
1265ExpandMappings(
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001266 char_u *pat,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001267 regmatch_T *regmatch,
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001268 int *numMatches,
1269 char_u ***matches)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001270{
1271 mapblock_T *mp;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001272 garray_T ga;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001273 int hash;
1274 int count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001275 char_u *p;
1276 int i;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001277 int fuzzy;
1278 int match;
1279 int score;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001280 fuzmatch_str_T *fuzmatch;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001281
1282 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001283
1284 validate_maphash();
1285
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001286 *numMatches = 0; // return values in case of FAIL
1287 *matches = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001288
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001289 if (!fuzzy)
1290 ga_init2(&ga, sizeof(char *), 3);
1291 else
1292 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001293
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001294 // First search in map modifier arguments
1295 for (i = 0; i < 7; ++i)
1296 {
1297 if (i == 0)
1298 p = (char_u *)"<silent>";
1299 else if (i == 1)
1300 p = (char_u *)"<unique>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001301#ifdef FEAT_EVAL
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001302 else if (i == 2)
1303 p = (char_u *)"<script>";
1304 else if (i == 3)
1305 p = (char_u *)"<expr>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001306#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001307 else if (i == 4 && !expand_buffer)
1308 p = (char_u *)"<buffer>";
1309 else if (i == 5)
1310 p = (char_u *)"<nowait>";
1311 else if (i == 6)
1312 p = (char_u *)"<special>";
1313 else
1314 continue;
1315
1316 if (!fuzzy)
1317 match = vim_regexec(regmatch, p, (colnr_T)0);
1318 else
1319 {
1320 score = fuzzy_match_str(p, pat);
1321 match = (score != 0);
1322 }
1323
1324 if (!match)
1325 continue;
1326
1327 if (ga_grow(&ga, 1) == FAIL)
1328 break;
1329
1330 if (fuzzy)
1331 {
1332 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1333 fuzmatch->idx = ga.ga_len;
1334 fuzmatch->str = vim_strsave(p);
1335 fuzmatch->score = score;
1336 }
1337 else
1338 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
1339 ++ga.ga_len;
1340 }
1341
1342 for (hash = 0; hash < 256; ++hash)
1343 {
1344 if (expand_isabbrev)
1345 {
1346 if (hash > 0) // only one abbrev list
1347 break; // for (hash)
1348 mp = first_abbr;
1349 }
1350 else if (expand_buffer)
1351 mp = curbuf->b_maphash[hash];
1352 else
1353 mp = maphash[hash];
1354 for (; mp; mp = mp->m_next)
1355 {
1356 if (!(mp->m_mode & expand_mapmodes))
1357 continue;
1358
1359 p = translate_mapping(mp->m_keys);
1360 if (p == NULL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001361 continue;
1362
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001363 if (!fuzzy)
1364 match = vim_regexec(regmatch, p, (colnr_T)0);
1365 else
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001366 {
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001367 score = fuzzy_match_str(p, pat);
1368 match = (score != 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001369 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001370
1371 if (!match)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001372 {
1373 vim_free(p);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001374 continue;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001375 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001376
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001377 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001378 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001379 vim_free(p);
1380 break;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001381 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001382
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001383 if (fuzzy)
1384 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001385 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1386 fuzmatch->idx = ga.ga_len;
1387 fuzmatch->str = p;
1388 fuzmatch->score = score;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001389 }
1390 else
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001391 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001392
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001393 ++ga.ga_len;
1394 } // for (mp)
1395 } // for (hash)
1396
1397 if (ga.ga_len == 0)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001398 return FAIL;
1399
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001400 if (!fuzzy)
1401 {
1402 *matches = ga.ga_data;
1403 *numMatches = ga.ga_len;
1404 }
1405 else
1406 {
1407 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
1408 FALSE) == FAIL)
1409 return FAIL;
1410 *numMatches = ga.ga_len;
1411 }
1412
1413 count = *numMatches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001414 if (count > 1)
1415 {
1416 char_u **ptr1;
1417 char_u **ptr2;
1418 char_u **ptr3;
1419
1420 // Sort the matches
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001421 // Fuzzy matching already sorts the matches
1422 if (!fuzzy)
1423 sort_strings(*matches, count);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001424
1425 // Remove multiple entries
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001426 ptr1 = *matches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001427 ptr2 = ptr1 + 1;
1428 ptr3 = ptr1 + count;
1429
1430 while (ptr2 < ptr3)
1431 {
1432 if (STRCMP(*ptr1, *ptr2))
1433 *++ptr1 = *ptr2++;
1434 else
1435 {
1436 vim_free(*ptr2++);
1437 count--;
1438 }
1439 }
1440 }
1441
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001442 *numMatches = count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001443 return (count == 0 ? FAIL : OK);
1444}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001445
1446/*
1447 * Check for an abbreviation.
1448 * Cursor is at ptr[col].
1449 * When inserting, mincol is where insert started.
1450 * For the command line, mincol is what is to be skipped over.
1451 * "c" is the character typed before check_abbr was called. It may have
1452 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1453 *
1454 * Historic vi practice: The last character of an abbreviation must be an id
1455 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1456 * characters or all non-id characters. This allows for abbr. "#i" to
1457 * "#include".
1458 *
1459 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1460 * Then there must be white space before the abbr.
1461 *
1462 * return TRUE if there is an abbreviation, FALSE if not
1463 */
1464 int
1465check_abbr(
1466 int c,
1467 char_u *ptr,
1468 int col,
1469 int mincol)
1470{
1471 int len;
1472 int scol; // starting column of the abbr.
1473 int j;
1474 char_u *s;
1475 char_u tb[MB_MAXBYTES + 4];
1476 mapblock_T *mp;
1477 mapblock_T *mp2;
1478 int clen = 0; // length in characters
1479 int is_id = TRUE;
1480 int vim_abbr;
1481
1482 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1483 return FALSE;
1484
1485 // no remapping implies no abbreviation, except for CTRL-]
1486 if (noremap_keys() && c != Ctrl_RSB)
1487 return FALSE;
1488
1489 // Check for word before the cursor: If it ends in a keyword char all
1490 // chars before it must be keyword chars or non-keyword chars, but not
1491 // white space. If it ends in a non-keyword char we accept any characters
1492 // before it except white space.
1493 if (col == 0) // cannot be an abbr.
1494 return FALSE;
1495
1496 if (has_mbyte)
1497 {
1498 char_u *p;
1499
1500 p = mb_prevptr(ptr, ptr + col);
1501 if (!vim_iswordp(p))
1502 vim_abbr = TRUE; // Vim added abbr.
1503 else
1504 {
1505 vim_abbr = FALSE; // vi compatible abbr.
1506 if (p > ptr)
1507 is_id = vim_iswordp(mb_prevptr(ptr, p));
1508 }
1509 clen = 1;
1510 while (p > ptr + mincol)
1511 {
1512 p = mb_prevptr(ptr, p);
1513 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1514 {
1515 p += (*mb_ptr2len)(p);
1516 break;
1517 }
1518 ++clen;
1519 }
1520 scol = (int)(p - ptr);
1521 }
1522 else
1523 {
1524 if (!vim_iswordc(ptr[col - 1]))
1525 vim_abbr = TRUE; // Vim added abbr.
1526 else
1527 {
1528 vim_abbr = FALSE; // vi compatible abbr.
1529 if (col > 1)
1530 is_id = vim_iswordc(ptr[col - 2]);
1531 }
1532 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1533 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1534 ;
1535 }
1536
1537 if (scol < mincol)
1538 scol = mincol;
1539 if (scol < col) // there is a word in front of the cursor
1540 {
1541 ptr += scol;
1542 len = col - scol;
1543 mp = curbuf->b_first_abbr;
1544 mp2 = first_abbr;
1545 if (mp == NULL)
1546 {
1547 mp = mp2;
1548 mp2 = NULL;
1549 }
1550 for ( ; mp; mp->m_next == NULL
1551 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1552 {
1553 int qlen = mp->m_keylen;
1554 char_u *q = mp->m_keys;
1555 int match;
1556
1557 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1558 {
1559 char_u *qe = vim_strsave(mp->m_keys);
1560
1561 // might have CSI escaped mp->m_keys
1562 if (qe != NULL)
1563 {
1564 q = qe;
1565 vim_unescape_csi(q);
1566 qlen = (int)STRLEN(q);
1567 }
1568 }
1569
1570 // find entries with right mode and keys
1571 match = (mp->m_mode & State)
1572 && qlen == len
1573 && !STRNCMP(q, ptr, (size_t)len);
1574 if (q != mp->m_keys)
1575 vim_free(q);
1576 if (match)
1577 break;
1578 }
1579 if (mp != NULL)
1580 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001581 int noremap;
1582 int silent;
1583#ifdef FEAT_EVAL
1584 int expr;
1585#endif
1586
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001587 // Found a match:
1588 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1589 // This goes from end to start.
1590 //
1591 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1592 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1593 // Characters where IS_SPECIAL() == TRUE: key codes, need
1594 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1595 //
1596 // Character CTRL-] is treated specially - it completes the
1597 // abbreviation, but is not inserted into the input stream.
1598 j = 0;
1599 if (c != Ctrl_RSB)
1600 {
1601 // special key code, split up
1602 if (IS_SPECIAL(c) || c == K_SPECIAL)
1603 {
1604 tb[j++] = K_SPECIAL;
1605 tb[j++] = K_SECOND(c);
1606 tb[j++] = K_THIRD(c);
1607 }
1608 else
1609 {
1610 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1611 tb[j++] = Ctrl_V; // special char needs CTRL-V
1612 if (has_mbyte)
1613 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001614 int newlen;
1615 char_u *escaped;
1616
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001617 // if ABBR_OFF has been added, remove it here
1618 if (c >= ABBR_OFF)
1619 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001620 newlen = (*mb_char2bytes)(c, tb + j);
1621 tb[j + newlen] = NUL;
1622 // Need to escape K_SPECIAL.
1623 escaped = vim_strsave_escape_csi(tb + j);
1624 if (escaped != NULL)
1625 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001626 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001627 mch_memmove(tb + j, escaped, newlen);
1628 j += newlen;
1629 vim_free(escaped);
1630 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001631 }
1632 else
1633 tb[j++] = c;
1634 }
1635 tb[j] = NUL;
1636 // insert the last typed char
1637 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1638 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001639
1640 // copy values here, calling eval_map_expr() may make "mp" invalid!
1641 noremap = mp->m_noremap;
1642 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001643#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001644 expr = mp->m_expr;
1645
1646 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001647 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001648 else
1649#endif
1650 s = mp->m_str;
1651 if (s != NULL)
1652 {
1653 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001654 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001655 // no abbrev. for these chars
1656 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1657#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001658 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001659 vim_free(s);
1660#endif
1661 }
1662
1663 tb[0] = Ctrl_H;
1664 tb[1] = NUL;
1665 if (has_mbyte)
1666 len = clen; // Delete characters instead of bytes
1667 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001668 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001669 return TRUE;
1670 }
1671 }
1672 return FALSE;
1673}
1674
1675#ifdef FEAT_EVAL
1676/*
1677 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1678 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001679 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001680 */
1681 char_u *
1682eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001683 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001684 int c) // NUL or typed character for abbreviation
1685{
1686 char_u *res;
1687 char_u *p;
1688 char_u *expr;
1689 pos_T save_cursor;
1690 int save_msg_col;
1691 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001692 scid_T save_sctx_sid = current_sctx.sc_sid;
1693 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001694
1695 // Remove escaping of CSI, because "str" is in a format to be used as
1696 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001697 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001698 if (expr == NULL)
1699 return NULL;
1700 vim_unescape_csi(expr);
1701
1702 // Forbid changing text or using ":normal" to avoid most of the bad side
1703 // effects. Also restore the cursor position.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001704 ++textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001705 ++ex_normal_lock;
1706 set_vim_var_char(c); // set v:char to the typed character
1707 save_cursor = curwin->w_cursor;
1708 save_msg_col = msg_col;
1709 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001710 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1711 {
1712 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1713 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1714 }
1715
1716 // Note: the evaluation may make "mp" invalid.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001717 p = eval_to_string(expr, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001718
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001719 --textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001720 --ex_normal_lock;
1721 curwin->w_cursor = save_cursor;
1722 msg_col = save_msg_col;
1723 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001724 current_sctx.sc_sid = save_sctx_sid;
1725 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001726
1727 vim_free(expr);
1728
1729 if (p == NULL)
1730 return NULL;
1731 // Escape CSI in the result to be able to use the string as typeahead.
1732 res = vim_strsave_escape_csi(p);
1733 vim_free(p);
1734
1735 return res;
1736}
1737#endif
1738
1739/*
1740 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1741 * can be put in the typeahead buffer.
1742 * Returns NULL when out of memory.
1743 */
1744 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001745vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001746{
1747 char_u *res;
1748 char_u *s, *d;
1749
1750 // Need a buffer to hold up to three times as much. Four in case of an
1751 // illegal utf-8 byte:
1752 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1753 res = alloc(STRLEN(p) * 4 + 1);
1754 if (res != NULL)
1755 {
1756 d = res;
1757 for (s = p; *s != NUL; )
1758 {
1759 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1760 {
1761 // Copy special key unmodified.
1762 *d++ = *s++;
1763 *d++ = *s++;
1764 *d++ = *s++;
1765 }
1766 else
1767 {
1768 // Add character, possibly multi-byte to destination, escaping
1769 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1770 d = add_char2buf(PTR2CHAR(s), d);
1771 s += MB_CPTR2LEN(s);
1772 }
1773 }
1774 *d = NUL;
1775 }
1776 return res;
1777}
1778
1779/*
1780 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1781 * vim_strsave_escape_csi(). Works in-place.
1782 */
1783 void
1784vim_unescape_csi(char_u *p)
1785{
1786 char_u *s = p, *d = p;
1787
1788 while (*s != NUL)
1789 {
1790 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1791 {
1792 *d++ = K_SPECIAL;
1793 s += 3;
1794 }
1795 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1796 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1797 {
1798 *d++ = CSI;
1799 s += 3;
1800 }
1801 else
1802 *d++ = *s++;
1803 }
1804 *d = NUL;
1805}
1806
1807/*
1808 * Write map commands for the current mappings to an .exrc file.
1809 * Return FAIL on error, OK otherwise.
1810 */
1811 int
1812makemap(
1813 FILE *fd,
1814 buf_T *buf) // buffer for local mappings or NULL
1815{
1816 mapblock_T *mp;
1817 char_u c1, c2, c3;
1818 char_u *p;
1819 char *cmd;
1820 int abbr;
1821 int hash;
1822 int did_cpo = FALSE;
1823 int i;
1824
1825 validate_maphash();
1826
1827 // Do the loop twice: Once for mappings, once for abbreviations.
1828 // Then loop over all map hash lists.
1829 for (abbr = 0; abbr < 2; ++abbr)
1830 for (hash = 0; hash < 256; ++hash)
1831 {
1832 if (abbr)
1833 {
1834 if (hash > 0) // there is only one abbr list
1835 break;
1836 if (buf != NULL)
1837 mp = buf->b_first_abbr;
1838 else
1839 mp = first_abbr;
1840 }
1841 else
1842 {
1843 if (buf != NULL)
1844 mp = buf->b_maphash[hash];
1845 else
1846 mp = maphash[hash];
1847 }
1848
1849 for ( ; mp; mp = mp->m_next)
1850 {
1851 // skip script-local mappings
1852 if (mp->m_noremap == REMAP_SCRIPT)
1853 continue;
1854
1855 // skip mappings that contain a <SNR> (script-local thing),
1856 // they probably don't work when loaded again
1857 for (p = mp->m_str; *p != NUL; ++p)
1858 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1859 && p[2] == (int)KE_SNR)
1860 break;
1861 if (*p != NUL)
1862 continue;
1863
1864 // It's possible to create a mapping and then ":unmap" certain
1865 // modes. We recreate this here by mapping the individual
1866 // modes, which requires up to three of them.
1867 c1 = NUL;
1868 c2 = NUL;
1869 c3 = NUL;
1870 if (abbr)
1871 cmd = "abbr";
1872 else
1873 cmd = "map";
1874 switch (mp->m_mode)
1875 {
1876 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
1877 break;
1878 case NORMAL:
1879 c1 = 'n';
1880 break;
1881 case VISUAL:
1882 c1 = 'x';
1883 break;
1884 case SELECTMODE:
1885 c1 = 's';
1886 break;
1887 case OP_PENDING:
1888 c1 = 'o';
1889 break;
1890 case NORMAL + VISUAL:
1891 c1 = 'n';
1892 c2 = 'x';
1893 break;
1894 case NORMAL + SELECTMODE:
1895 c1 = 'n';
1896 c2 = 's';
1897 break;
1898 case NORMAL + OP_PENDING:
1899 c1 = 'n';
1900 c2 = 'o';
1901 break;
1902 case VISUAL + SELECTMODE:
1903 c1 = 'v';
1904 break;
1905 case VISUAL + OP_PENDING:
1906 c1 = 'x';
1907 c2 = 'o';
1908 break;
1909 case SELECTMODE + OP_PENDING:
1910 c1 = 's';
1911 c2 = 'o';
1912 break;
1913 case NORMAL + VISUAL + SELECTMODE:
1914 c1 = 'n';
1915 c2 = 'v';
1916 break;
1917 case NORMAL + VISUAL + OP_PENDING:
1918 c1 = 'n';
1919 c2 = 'x';
1920 c3 = 'o';
1921 break;
1922 case NORMAL + SELECTMODE + OP_PENDING:
1923 c1 = 'n';
1924 c2 = 's';
1925 c3 = 'o';
1926 break;
1927 case VISUAL + SELECTMODE + OP_PENDING:
1928 c1 = 'v';
1929 c2 = 'o';
1930 break;
1931 case CMDLINE + INSERT:
1932 if (!abbr)
1933 cmd = "map!";
1934 break;
1935 case CMDLINE:
1936 c1 = 'c';
1937 break;
1938 case INSERT:
1939 c1 = 'i';
1940 break;
1941 case LANGMAP:
1942 c1 = 'l';
1943 break;
1944 case TERMINAL:
1945 c1 = 't';
1946 break;
1947 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001948 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001949 return FAIL;
1950 }
1951 do // do this twice if c2 is set, 3 times with c3
1952 {
1953 // When outputting <> form, need to make sure that 'cpo'
1954 // is set to the Vim default.
1955 if (!did_cpo)
1956 {
1957 if (*mp->m_str == NUL) // will use <Nop>
1958 did_cpo = TRUE;
1959 else
1960 for (i = 0; i < 2; ++i)
1961 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1962 if (*p == K_SPECIAL || *p == NL)
1963 did_cpo = TRUE;
1964 if (did_cpo)
1965 {
1966 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1967 || put_eol(fd) < 0
1968 || fprintf(fd, "set cpo&vim") < 0
1969 || put_eol(fd) < 0)
1970 return FAIL;
1971 }
1972 }
1973 if (c1 && putc(c1, fd) < 0)
1974 return FAIL;
1975 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1976 return FAIL;
1977 if (fputs(cmd, fd) < 0)
1978 return FAIL;
1979 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1980 return FAIL;
1981 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1982 return FAIL;
1983 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1984 return FAIL;
1985#ifdef FEAT_EVAL
1986 if (mp->m_noremap == REMAP_SCRIPT
1987 && fputs("<script>", fd) < 0)
1988 return FAIL;
1989 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1990 return FAIL;
1991#endif
1992
1993 if ( putc(' ', fd) < 0
1994 || put_escstr(fd, mp->m_keys, 0) == FAIL
1995 || putc(' ', fd) < 0
1996 || put_escstr(fd, mp->m_str, 1) == FAIL
1997 || put_eol(fd) < 0)
1998 return FAIL;
1999 c1 = c2;
2000 c2 = c3;
2001 c3 = NUL;
2002 } while (c1 != NUL);
2003 }
2004 }
2005
2006 if (did_cpo)
2007 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
2008 || put_eol(fd) < 0
2009 || fprintf(fd, "unlet s:cpo_save") < 0
2010 || put_eol(fd) < 0)
2011 return FAIL;
2012 return OK;
2013}
2014
2015/*
2016 * write escape string to file
2017 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2018 *
2019 * return FAIL for failure, OK otherwise
2020 */
2021 int
2022put_escstr(FILE *fd, char_u *strstart, int what)
2023{
2024 char_u *str = strstart;
2025 int c;
2026 int modifiers;
2027
2028 // :map xx <Nop>
2029 if (*str == NUL && what == 1)
2030 {
2031 if (fprintf(fd, "<Nop>") < 0)
2032 return FAIL;
2033 return OK;
2034 }
2035
2036 for ( ; *str != NUL; ++str)
2037 {
2038 char_u *p;
2039
2040 // Check for a multi-byte character, which may contain escaped
2041 // K_SPECIAL and CSI bytes
2042 p = mb_unescape(&str);
2043 if (p != NULL)
2044 {
2045 while (*p != NUL)
2046 if (fputc(*p++, fd) < 0)
2047 return FAIL;
2048 --str;
2049 continue;
2050 }
2051
2052 c = *str;
2053 // Special key codes have to be translated to be able to make sense
2054 // when they are read back.
2055 if (c == K_SPECIAL && what != 2)
2056 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002057 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002058 if (str[1] == KS_MODIFIER)
2059 {
2060 modifiers = str[2];
2061 str += 3;
2062 c = *str;
2063 }
2064 if (c == K_SPECIAL)
2065 {
2066 c = TO_SPECIAL(str[1], str[2]);
2067 str += 2;
2068 }
2069 if (IS_SPECIAL(c) || modifiers) // special key
2070 {
2071 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2072 return FAIL;
2073 continue;
2074 }
2075 }
2076
2077 // A '\n' in a map command should be written as <NL>.
2078 // A '\n' in a set command should be written as \^V^J.
2079 if (c == NL)
2080 {
2081 if (what == 2)
2082 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002083 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002084 return FAIL;
2085 }
2086 else
2087 {
2088 if (fprintf(fd, "<NL>") < 0)
2089 return FAIL;
2090 }
2091 continue;
2092 }
2093
2094 // Some characters have to be escaped with CTRL-V to
2095 // prevent them from misinterpreted in DoOneCmd().
2096 // A space, Tab and '"' has to be escaped with a backslash to
2097 // prevent it to be misinterpreted in do_set().
2098 // A space has to be escaped with a CTRL-V when it's at the start of a
2099 // ":map" rhs.
2100 // A '<' has to be escaped with a CTRL-V to prevent it being
2101 // interpreted as the start of a special key name.
2102 // A space in the lhs of a :map needs a CTRL-V.
2103 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2104 {
2105 if (putc('\\', fd) < 0)
2106 return FAIL;
2107 }
2108 else if (c < ' ' || c > '~' || c == '|'
2109 || (what == 0 && c == ' ')
2110 || (what == 1 && str == strstart && c == ' ')
2111 || (what != 2 && c == '<'))
2112 {
2113 if (putc(Ctrl_V, fd) < 0)
2114 return FAIL;
2115 }
2116 if (putc(c, fd) < 0)
2117 return FAIL;
2118 }
2119 return OK;
2120}
2121
2122/*
2123 * Check all mappings for the presence of special key codes.
2124 * Used after ":set term=xxx".
2125 */
2126 void
2127check_map_keycodes(void)
2128{
2129 mapblock_T *mp;
2130 char_u *p;
2131 int i;
2132 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002133 int abbr;
2134 int hash;
2135 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002136 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002137
2138 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002139 // avoids giving error messages
2140 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002141 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002142
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002143 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002144 // mappings/abbreviations with bp == NULL
2145 for (bp = firstbuf; ; bp = bp->b_next)
2146 {
2147 // Do the loop twice: Once for mappings, once for abbreviations.
2148 // Then loop over all map hash lists.
2149 for (abbr = 0; abbr <= 1; ++abbr)
2150 for (hash = 0; hash < 256; ++hash)
2151 {
2152 if (abbr)
2153 {
2154 if (hash) // there is only one abbr list
2155 break;
2156 if (bp != NULL)
2157 mp = bp->b_first_abbr;
2158 else
2159 mp = first_abbr;
2160 }
2161 else
2162 {
2163 if (bp != NULL)
2164 mp = bp->b_maphash[hash];
2165 else
2166 mp = maphash[hash];
2167 }
2168 for ( ; mp != NULL; mp = mp->m_next)
2169 {
2170 for (i = 0; i <= 1; ++i) // do this twice
2171 {
2172 if (i == 0)
2173 p = mp->m_keys; // once for the "from" part
2174 else
2175 p = mp->m_str; // and once for the "to" part
2176 while (*p)
2177 {
2178 if (*p == K_SPECIAL)
2179 {
2180 ++p;
2181 if (*p < 128) // for "normal" tcap entries
2182 {
2183 buf[0] = p[0];
2184 buf[1] = p[1];
2185 buf[2] = NUL;
2186 (void)add_termcap_entry(buf, FALSE);
2187 }
2188 ++p;
2189 }
2190 ++p;
2191 }
2192 }
2193 }
2194 }
2195 if (bp == NULL)
2196 break;
2197 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002198 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002199 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002200}
2201
2202#if defined(FEAT_EVAL) || defined(PROTO)
2203/*
2204 * Check the string "keys" against the lhs of all mappings.
2205 * Return pointer to rhs of mapping (mapblock->m_str).
2206 * NULL when no mapping found.
2207 */
2208 char_u *
2209check_map(
2210 char_u *keys,
2211 int mode,
2212 int exact, // require exact match
2213 int ign_mod, // ignore preceding modifier
2214 int abbr, // do abbreviations
2215 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2216 int *local_ptr) // return: buffer-local mapping or NULL
2217{
2218 int hash;
2219 int len, minlen;
2220 mapblock_T *mp;
2221 char_u *s;
2222 int local;
2223
2224 validate_maphash();
2225
2226 len = (int)STRLEN(keys);
2227 for (local = 1; local >= 0; --local)
2228 // loop over all hash lists
2229 for (hash = 0; hash < 256; ++hash)
2230 {
2231 if (abbr)
2232 {
2233 if (hash > 0) // there is only one list.
2234 break;
2235 if (local)
2236 mp = curbuf->b_first_abbr;
2237 else
2238 mp = first_abbr;
2239 }
2240 else if (local)
2241 mp = curbuf->b_maphash[hash];
2242 else
2243 mp = maphash[hash];
2244 for ( ; mp != NULL; mp = mp->m_next)
2245 {
2246 // skip entries with wrong mode, wrong length and not matching
2247 // ones
2248 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2249 {
2250 if (len > mp->m_keylen)
2251 minlen = mp->m_keylen;
2252 else
2253 minlen = len;
2254 s = mp->m_keys;
2255 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2256 && s[2] != NUL)
2257 {
2258 s += 3;
2259 if (len > mp->m_keylen - 3)
2260 minlen = mp->m_keylen - 3;
2261 }
2262 if (STRNCMP(s, keys, minlen) == 0)
2263 {
2264 if (mp_ptr != NULL)
2265 *mp_ptr = mp;
2266 if (local_ptr != NULL)
2267 *local_ptr = local;
2268 return mp->m_str;
2269 }
2270 }
2271 }
2272 }
2273
2274 return NULL;
2275}
2276
Ernie Rael659c2402022-04-24 18:40:28 +01002277/*
2278 * Fill in the empty dictionary with items as defined by maparg builtin.
2279 */
2280 static void
2281mapblock2dict(
2282 mapblock_T *mp,
2283 dict_T *dict,
2284 char_u *lhsrawalt, // may be NULL
2285 int buffer_local) // false if not buffer local mapping
2286{
2287 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2288 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2289
2290 dict_add_string(dict, "lhs", lhs);
2291 vim_free(lhs);
2292 dict_add_string(dict, "lhsraw", mp->m_keys);
2293 if (lhsrawalt)
2294 // Also add the value for the simplified entry.
2295 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2296 dict_add_string(dict, "rhs", mp->m_orig_str);
2297 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2298 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2299 ? 1L : 0L);
2300 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2301 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2302 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2303 dict_add_number(dict, "scriptversion",
2304 (long)mp->m_script_ctx.sc_version);
2305 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2306 dict_add_number(dict, "buffer", (long)buffer_local);
2307 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2308 dict_add_string(dict, "mode", mapmode);
2309
2310 vim_free(mapmode);
2311}
2312
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002313 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002314get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2315{
2316 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002317 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002318 char_u *which;
2319 char_u buf[NUMBUFLEN];
2320 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002321 char_u *alt_keys_buf = NULL;
2322 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002323 char_u *rhs;
2324 int mode;
2325 int abbr = FALSE;
2326 int get_dict = FALSE;
2327 mapblock_T *mp;
Bram Moolenaara55ba062020-05-27 21:29:04 +02002328 mapblock_T *mp_simplified = 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 // The "rhs" and "buffer_local" values are not expected to change.
2365 mp_simplified = mp;
2366 (void)replace_termcodes(keys, &alt_keys_buf,
2367 flags | REPTERM_NO_SIMPLIFY, NULL);
2368 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2369 &buffer_local);
2370 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002371
2372 if (!get_dict)
2373 {
2374 // Return a string.
2375 if (rhs != NULL)
2376 {
2377 if (*rhs == NUL)
2378 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2379 else
2380 rettv->vval.v_string = str2special_save(rhs, FALSE);
2381 }
2382
2383 }
2384 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002385 mapblock2dict(mp, rettv->vval.v_dict,
2386 did_simplify ? mp_simplified->m_keys : NULL, buffer_local);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002387
2388 vim_free(keys_buf);
2389 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002390}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002391
2392/*
Ernie Rael659c2402022-04-24 18:40:28 +01002393 * "getmappings()" function
2394 */
2395 void
2396f_getmappings(typval_T *argvars UNUSED, typval_T *rettv)
2397{
2398 dict_T *d;
2399 mapblock_T *mp;
2400 int buffer_local;
2401 char_u *keys_buf;
2402 int did_simplify;
2403 int hash;
2404 char_u *lhs;
2405 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
2406
2407 if (rettv_list_alloc(rettv) != OK)
2408 return;
2409
2410 validate_maphash();
2411
2412 // Do it twice: once for global maps and once for local maps.
2413 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2414 {
2415 for (hash = 0; hash < 256; ++hash)
2416 {
2417 if (buffer_local)
2418 mp = curbuf->b_maphash[hash];
2419 else
2420 mp = maphash[hash];
2421 for (; mp; mp = mp->m_next)
2422 {
2423 if (mp->m_simplified)
2424 continue;
2425 if ((d = dict_alloc()) == NULL)
2426 return;
2427 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2428 return;
2429
2430 keys_buf = NULL;
2431 did_simplify = FALSE;
2432
2433 lhs = str2special_save(mp->m_keys, TRUE);
2434 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2435 vim_free(lhs);
2436
2437 mapblock2dict(mp, d,
2438 did_simplify ? keys_buf : NULL, buffer_local);
2439 vim_free(keys_buf);
2440 }
2441 }
2442 }
2443}
2444
2445/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002446 * "maparg()" function
2447 */
2448 void
2449f_maparg(typval_T *argvars, typval_T *rettv)
2450{
2451 if (in_vim9script()
2452 && (check_for_string_arg(argvars, 0) == FAIL
2453 || check_for_opt_string_arg(argvars, 1) == FAIL
2454 || (argvars[1].v_type != VAR_UNKNOWN
2455 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2456 || (argvars[2].v_type != VAR_UNKNOWN
2457 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2458 return;
2459
2460 get_maparg(argvars, rettv, TRUE);
2461}
2462
2463/*
2464 * "mapcheck()" function
2465 */
2466 void
2467f_mapcheck(typval_T *argvars, typval_T *rettv)
2468{
2469 if (in_vim9script()
2470 && (check_for_string_arg(argvars, 0) == FAIL
2471 || check_for_opt_string_arg(argvars, 1) == FAIL
2472 || (argvars[1].v_type != VAR_UNKNOWN
2473 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2474 return;
2475
2476 get_maparg(argvars, rettv, FALSE);
2477}
2478
2479/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002480 * "mapset()" function
2481 */
2482 void
2483f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2484{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002485 char_u *keys_buf = NULL;
2486 char_u *which;
2487 int mode;
2488 char_u buf[NUMBUFLEN];
2489 int is_abbr;
2490 dict_T *d;
2491 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002492 char_u *lhsraw;
2493 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002494 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002495 char_u *orig_rhs;
2496 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002497 int noremap;
2498 int expr;
2499 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002500 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002501 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002502 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002503 linenr_T lnum;
2504 mapblock_T **map_table = maphash;
2505 mapblock_T **abbr_table = &first_abbr;
2506 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002507 char_u *arg;
2508
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002509 if (in_vim9script()
2510 && (check_for_string_arg(argvars, 0) == FAIL
2511 || check_for_bool_arg(argvars, 1) == FAIL
2512 || check_for_dict_arg(argvars, 2) == FAIL))
2513 return;
2514
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002515 which = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar1b912982020-09-29 21:45:41 +02002516 if (which == NULL)
2517 return;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002518 mode = get_map_mode(&which, 0);
Bram Moolenaar74273e62020-10-01 21:37:21 +02002519 is_abbr = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002520
2521 if (argvars[2].v_type != VAR_DICT)
2522 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002523 emsg(_(e_key_not_present_in_dictionary));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002524 return;
2525 }
2526 d = argvars[2].vval.v_dict;
2527
2528 // Get the values in the same order as above in get_maparg().
2529 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002530 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2531 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002532 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002533 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002534 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002535 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002536 return;
2537 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002538 orig_rhs = rhs;
2539 rhs = replace_termcodes(rhs, &arg_buf,
2540 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002541
2542 noremap = dict_get_number(d, (char_u *)"noremap") ? REMAP_NONE: 0;
2543 if (dict_get_number(d, (char_u *)"script") != 0)
2544 noremap = REMAP_SCRIPT;
2545 expr = dict_get_number(d, (char_u *)"expr") != 0;
2546 silent = dict_get_number(d, (char_u *)"silent") != 0;
2547 sid = dict_get_number(d, (char_u *)"sid");
Bram Moolenaara9528b32022-01-18 20:51:35 +00002548 scriptversion = dict_get_number(d, (char_u *)"scriptversion");
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002549 lnum = dict_get_number(d, (char_u *)"lnum");
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002550 buffer = dict_get_number(d, (char_u *)"buffer");
2551 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2552 // mode from the dict is not used
2553
2554 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002555 {
2556 map_table = curbuf->b_maphash;
2557 abbr_table = &curbuf->b_first_abbr;
2558 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002559
2560 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002561 if (buffer)
2562 {
2563 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2564 if (arg == NULL)
2565 return;
2566 STRCPY(arg, "<buffer>");
2567 STRCPY(arg + 8, lhs);
2568 }
2569 else
2570 {
2571 arg = vim_strsave(lhs);
2572 if (arg == NULL)
2573 return;
2574 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002575 do_map(1, arg, mode, is_abbr);
2576 vim_free(arg);
2577
Bram Moolenaar9c652532020-05-24 13:10:18 +02002578 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002579 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002580 if (lhsrawalt != NULL)
2581 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002582 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2583 lnum, 1);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002584 vim_free(keys_buf);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002585 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002586}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002587#endif
2588
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002589
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002590#if defined(MSWIN) || defined(MACOS_X)
2591
2592# define VIS_SEL (VISUAL+SELECTMODE) // abbreviation
2593
2594/*
2595 * Default mappings for some often used keys.
2596 */
2597struct initmap
2598{
2599 char_u *arg;
2600 int mode;
2601};
2602
2603# ifdef FEAT_GUI_MSWIN
2604// Use the Windows (CUA) keybindings. (GUI)
2605static struct initmap initmappings[] =
2606{
2607 // paste, copy and cut
2608 {(char_u *)"<S-Insert> \"*P", NORMAL},
2609 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
2610 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
2611 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2612 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2613 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2614 {(char_u *)"<C-X> \"*d", VIS_SEL},
2615 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2616};
2617# endif
2618
2619# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2620// Use the Windows (CUA) keybindings. (Console)
2621static struct initmap cinitmappings[] =
2622{
2623 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
2624 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
2625 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
2626 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
2627
2628 // paste, copy and cut
2629# ifdef FEAT_CLIPBOARD
2630 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P
2631 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
2632 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O*
2633 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2634 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2635 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2636 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2637# else
2638 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P
2639 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
2640 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O"
2641 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2642 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2643 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2644# endif
2645};
2646# endif
2647
2648# if defined(MACOS_X)
2649static struct initmap initmappings[] =
2650{
2651 // Use the Standard MacOS binding.
2652 // paste, copy and cut
2653 {(char_u *)"<D-v> \"*P", NORMAL},
2654 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
2655 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
2656 {(char_u *)"<D-c> \"*y", VIS_SEL},
2657 {(char_u *)"<D-x> \"*d", VIS_SEL},
2658 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2659};
2660# endif
2661
2662# undef VIS_SEL
2663#endif
2664
2665/*
2666 * Set up default mappings.
2667 */
2668 void
2669init_mappings(void)
2670{
2671#if defined(MSWIN) || defined(MACOS_X)
2672 int i;
2673
2674# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2675# ifdef VIMDLL
2676 if (!gui.starting)
2677# endif
2678 {
K.Takataeeec2542021-06-02 13:28:16 +02002679 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002680 add_map(cinitmappings[i].arg, cinitmappings[i].mode);
2681 }
2682# endif
2683# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002684 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002685 add_map(initmappings[i].arg, initmappings[i].mode);
2686# endif
2687#endif
2688}
2689
2690#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2691 || defined(PROTO)
2692/*
2693 * Add a mapping "map" for mode "mode".
2694 * Need to put string in allocated memory, because do_map() will modify it.
2695 */
2696 void
2697add_map(char_u *map, int mode)
2698{
2699 char_u *s;
2700 char_u *cpo_save = p_cpo;
2701
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002702 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002703 s = vim_strsave(map);
2704 if (s != NULL)
2705 {
2706 (void)do_map(0, s, mode, FALSE);
2707 vim_free(s);
2708 }
2709 p_cpo = cpo_save;
2710}
2711#endif
2712
Bram Moolenaare677df82019-09-02 22:31:11 +02002713#if defined(FEAT_LANGMAP) || defined(PROTO)
2714/*
2715 * Any character has an equivalent 'langmap' character. This is used for
2716 * keyboards that have a special language mode that sends characters above
2717 * 128 (although other characters can be translated too). The "to" field is a
2718 * Vim command character. This avoids having to switch the keyboard back to
2719 * ASCII mode when leaving Insert mode.
2720 *
2721 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2722 * commands.
2723 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2724 * same as langmap_mapchar[] for characters >= 256.
2725 *
2726 * Use growarray for 'langmap' chars >= 256
2727 */
2728typedef struct
2729{
2730 int from;
2731 int to;
2732} langmap_entry_T;
2733
2734static garray_T langmap_mapga;
2735
2736/*
2737 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2738 * field. If not found insert a new entry at the appropriate location.
2739 */
2740 static void
2741langmap_set_entry(int from, int to)
2742{
2743 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2744 int a = 0;
2745 int b = langmap_mapga.ga_len;
2746
2747 // Do a binary search for an existing entry.
2748 while (a != b)
2749 {
2750 int i = (a + b) / 2;
2751 int d = entries[i].from - from;
2752
2753 if (d == 0)
2754 {
2755 entries[i].to = to;
2756 return;
2757 }
2758 if (d < 0)
2759 a = i + 1;
2760 else
2761 b = i;
2762 }
2763
2764 if (ga_grow(&langmap_mapga, 1) != OK)
2765 return; // out of memory
2766
2767 // insert new entry at position "a"
2768 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2769 mch_memmove(entries + 1, entries,
2770 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2771 ++langmap_mapga.ga_len;
2772 entries[0].from = from;
2773 entries[0].to = to;
2774}
2775
2776/*
2777 * Apply 'langmap' to multi-byte character "c" and return the result.
2778 */
2779 int
2780langmap_adjust_mb(int c)
2781{
2782 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2783 int a = 0;
2784 int b = langmap_mapga.ga_len;
2785
2786 while (a != b)
2787 {
2788 int i = (a + b) / 2;
2789 int d = entries[i].from - c;
2790
2791 if (d == 0)
2792 return entries[i].to; // found matching entry
2793 if (d < 0)
2794 a = i + 1;
2795 else
2796 b = i;
2797 }
2798 return c; // no entry found, return "c" unmodified
2799}
2800
2801 void
2802langmap_init(void)
2803{
2804 int i;
2805
2806 for (i = 0; i < 256; i++)
2807 langmap_mapchar[i] = i; // we init with a one-to-one map
2808 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2809}
2810
2811/*
2812 * Called when langmap option is set; the language map can be
2813 * changed at any time!
2814 */
2815 void
2816langmap_set(void)
2817{
2818 char_u *p;
2819 char_u *p2;
2820 int from, to;
2821
2822 ga_clear(&langmap_mapga); // clear the previous map first
2823 langmap_init(); // back to one-to-one map
2824
2825 for (p = p_langmap; p[0] != NUL; )
2826 {
2827 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2828 MB_PTR_ADV(p2))
2829 {
2830 if (p2[0] == '\\' && p2[1] != NUL)
2831 ++p2;
2832 }
2833 if (p2[0] == ';')
2834 ++p2; // abcd;ABCD form, p2 points to A
2835 else
2836 p2 = NULL; // aAbBcCdD form, p2 is NULL
2837 while (p[0])
2838 {
2839 if (p[0] == ',')
2840 {
2841 ++p;
2842 break;
2843 }
2844 if (p[0] == '\\' && p[1] != NUL)
2845 ++p;
2846 from = (*mb_ptr2char)(p);
2847 to = NUL;
2848 if (p2 == NULL)
2849 {
2850 MB_PTR_ADV(p);
2851 if (p[0] != ',')
2852 {
2853 if (p[0] == '\\')
2854 ++p;
2855 to = (*mb_ptr2char)(p);
2856 }
2857 }
2858 else
2859 {
2860 if (p2[0] != ',')
2861 {
2862 if (p2[0] == '\\')
2863 ++p2;
2864 to = (*mb_ptr2char)(p2);
2865 }
2866 }
2867 if (to == NUL)
2868 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002869 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002870 transchar(from));
2871 return;
2872 }
2873
2874 if (from >= 256)
2875 langmap_set_entry(from, to);
2876 else
2877 langmap_mapchar[from & 255] = to;
2878
2879 // Advance to next pair
2880 MB_PTR_ADV(p);
2881 if (p2 != NULL)
2882 {
2883 MB_PTR_ADV(p2);
2884 if (*p == ';')
2885 {
2886 p = p2;
2887 if (p[0] != NUL)
2888 {
2889 if (p[0] != ',')
2890 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002891 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02002892 return;
2893 }
2894 ++p;
2895 }
2896 break;
2897 }
2898 }
2899 }
2900 }
2901}
2902#endif
2903
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002904 static void
2905do_exmap(exarg_T *eap, int isabbrev)
2906{
2907 int mode;
2908 char_u *cmdp;
2909
2910 cmdp = eap->cmd;
2911 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
2912
2913 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
2914 eap->arg, mode, isabbrev))
2915 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002916 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002917 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002918 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
2919 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002920 break;
2921 }
2922}
2923
2924/*
2925 * ":abbreviate" and friends.
2926 */
2927 void
2928ex_abbreviate(exarg_T *eap)
2929{
2930 do_exmap(eap, TRUE); // almost the same as mapping
2931}
2932
2933/*
2934 * ":map" and friends.
2935 */
2936 void
2937ex_map(exarg_T *eap)
2938{
2939 // If we are sourcing .exrc or .vimrc in current directory we
2940 // print the mappings for security reasons.
2941 if (secure)
2942 {
2943 secure = 2;
2944 msg_outtrans(eap->cmd);
2945 msg_putchar('\n');
2946 }
2947 do_exmap(eap, FALSE);
2948}
2949
2950/*
2951 * ":unmap" and friends.
2952 */
2953 void
2954ex_unmap(exarg_T *eap)
2955{
2956 do_exmap(eap, FALSE);
2957}
2958
2959/*
2960 * ":mapclear" and friends.
2961 */
2962 void
2963ex_mapclear(exarg_T *eap)
2964{
2965 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
2966}
2967
2968/*
2969 * ":abclear" and friends.
2970 */
2971 void
2972ex_abclear(exarg_T *eap)
2973{
2974 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
2975}