blob: b188e4375dcc7301306d37277d8d22f9ea10e3cc [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;
87 vim_free(mp);
Bram Moolenaard648c012022-01-16 14:58:34 +000088#ifdef FEAT_EVAL
89 reset_last_used_map();
90#endif
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
207 out_flush(); // show one line at a time
208}
209
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200210 static int
211map_add(
212 mapblock_T **map_table,
213 mapblock_T **abbr_table,
214 char_u *keys,
215 char_u *rhs,
216 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200217 int noremap,
218 int nowait,
219 int silent,
220 int mode,
221 int is_abbr,
222#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200223 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200224 scid_T sid, // -1 to use current_sctx
225 linenr_T lnum,
226#endif
227 int simplified)
228{
Bram Moolenaar94075b22022-01-18 20:30:39 +0000229 mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200230
231 if (mp == NULL)
232 return FAIL;
233
234 // If CTRL-C has been mapped, don't always use it for Interrupting.
235 if (*keys == Ctrl_C)
236 {
237 if (map_table == curbuf->b_maphash)
238 curbuf->b_mapped_ctrl_c |= mode;
239 else
240 mapped_ctrl_c |= mode;
241 }
242
243 mp->m_keys = vim_strsave(keys);
244 mp->m_str = vim_strsave(rhs);
245 mp->m_orig_str = vim_strsave(orig_rhs);
246 if (mp->m_keys == NULL || mp->m_str == NULL)
247 {
248 vim_free(mp->m_keys);
249 vim_free(mp->m_str);
250 vim_free(mp->m_orig_str);
251 vim_free(mp);
252 return FAIL;
253 }
254 mp->m_keylen = (int)STRLEN(mp->m_keys);
255 mp->m_noremap = noremap;
256 mp->m_nowait = nowait;
257 mp->m_silent = silent;
258 mp->m_mode = mode;
259 mp->m_simplified = simplified;
260#ifdef FEAT_EVAL
261 mp->m_expr = expr;
262 if (sid >= 0)
263 {
264 mp->m_script_ctx.sc_sid = sid;
265 mp->m_script_ctx.sc_lnum = lnum;
Bram Moolenaar19db9e62022-01-11 11:58:19 +0000266 mp->m_script_ctx.sc_version = in_vim9script() ? SCRIPT_VERSION_VIM9 : 0;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200267 }
268 else
269 {
270 mp->m_script_ctx = current_sctx;
271 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
272 }
273#endif
274
275 // add the new entry in front of the abbrlist or maphash[] list
276 if (is_abbr)
277 {
278 mp->m_next = *abbr_table;
279 *abbr_table = mp;
280 }
281 else
282 {
283 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
284
285 mp->m_next = map_table[n];
286 map_table[n] = mp;
287 }
288 return OK;
289}
290
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200291/*
292 * map[!] : show all key mappings
293 * map[!] {lhs} : show key mapping for {lhs}
294 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
295 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
296 * unmap[!] {lhs} : remove key mapping for {lhs}
297 * abbr : show all abbreviations
298 * abbr {lhs} : show abbreviations for {lhs}
299 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
300 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
301 * unabbr {lhs} : remove abbreviation for {lhs}
302 *
303 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
304 *
305 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
306 * it will be modified.
307 *
308 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
309 * for :map! mode is INSERT + CMDLINE
310 * for :cmap mode is CMDLINE
311 * for :imap mode is INSERT
312 * for :lmap mode is LANGMAP
313 * for :nmap mode is NORMAL
314 * for :vmap mode is VISUAL + SELECTMODE
315 * for :xmap mode is VISUAL
316 * for :smap mode is SELECTMODE
317 * for :omap mode is OP_PENDING
318 * for :tmap mode is TERMINAL
319 *
320 * for :abbr mode is INSERT + CMDLINE
321 * for :iabbr mode is INSERT
322 * for :cabbr mode is CMDLINE
323 *
324 * Return 0 for success
325 * 1 for invalid arguments
326 * 2 for no match
327 * 4 for out of mem
328 * 5 for entry not unique
329 */
330 int
331do_map(
332 int maptype,
333 char_u *arg,
334 int mode,
335 int abbrev) // not a mapping but an abbreviation
336{
337 char_u *keys;
338 mapblock_T *mp, **mpp;
339 char_u *rhs;
340 char_u *p;
341 int n;
342 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200343 int hasarg;
344 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200345 int do_print;
346 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200347 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200348 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200349 char_u *arg_buf = NULL;
350 int retval = 0;
351 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200352 mapblock_T **abbr_table;
353 mapblock_T **map_table;
354 int unique = FALSE;
355 int nowait = FALSE;
356 int silent = FALSE;
357 int special = FALSE;
358#ifdef FEAT_EVAL
359 int expr = FALSE;
360#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200361 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200362 int noremap;
363 char_u *orig_rhs;
364
365 keys = arg;
366 map_table = maphash;
367 abbr_table = &first_abbr;
368
369 // For ":noremap" don't remap, otherwise do remap.
370 if (maptype == 2)
371 noremap = REMAP_NONE;
372 else
373 noremap = REMAP_YES;
374
375 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
376 // any order.
377 for (;;)
378 {
379 // Check for "<buffer>": mapping local to buffer.
380 if (STRNCMP(keys, "<buffer>", 8) == 0)
381 {
382 keys = skipwhite(keys + 8);
383 map_table = curbuf->b_maphash;
384 abbr_table = &curbuf->b_first_abbr;
385 continue;
386 }
387
388 // Check for "<nowait>": don't wait for more characters.
389 if (STRNCMP(keys, "<nowait>", 8) == 0)
390 {
391 keys = skipwhite(keys + 8);
392 nowait = TRUE;
393 continue;
394 }
395
396 // Check for "<silent>": don't echo commands.
397 if (STRNCMP(keys, "<silent>", 8) == 0)
398 {
399 keys = skipwhite(keys + 8);
400 silent = TRUE;
401 continue;
402 }
403
404 // Check for "<special>": accept special keys in <>
405 if (STRNCMP(keys, "<special>", 9) == 0)
406 {
407 keys = skipwhite(keys + 9);
408 special = TRUE;
409 continue;
410 }
411
412#ifdef FEAT_EVAL
413 // Check for "<script>": remap script-local mappings only
414 if (STRNCMP(keys, "<script>", 8) == 0)
415 {
416 keys = skipwhite(keys + 8);
417 noremap = REMAP_SCRIPT;
418 continue;
419 }
420
421 // Check for "<expr>": {rhs} is an expression.
422 if (STRNCMP(keys, "<expr>", 6) == 0)
423 {
424 keys = skipwhite(keys + 6);
425 expr = TRUE;
426 continue;
427 }
428#endif
429 // Check for "<unique>": don't overwrite an existing mapping.
430 if (STRNCMP(keys, "<unique>", 8) == 0)
431 {
432 keys = skipwhite(keys + 8);
433 unique = TRUE;
434 continue;
435 }
436 break;
437 }
438
439 validate_maphash();
440
441 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
442 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
443 // with :unmap white space is included in the keys, no argument possible.
444 p = keys;
445 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
446 while (*p && (maptype == 1 || !VIM_ISWHITE(*p)))
447 {
448 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
449 p[1] != NUL)
450 ++p; // skip CTRL-V or backslash
451 ++p;
452 }
453 if (*p != NUL)
454 *p++ = NUL;
455
456 p = skipwhite(p);
457 rhs = p;
458 hasarg = (*rhs != NUL);
459 haskey = (*keys != NUL);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200460 do_print = !haskey || (maptype != 1 && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200461
462 // check for :unmap without argument
463 if (maptype == 1 && !haskey)
464 {
465 retval = 1;
466 goto theend;
467 }
468
469 // If mapping has been given as ^V<C_UP> say, then replace the term codes
470 // with the appropriate two bytes. If it is a shifted special key, unshift
471 // it too, giving another two bytes.
472 // replace_termcodes() may move the result to allocated memory, which
473 // needs to be freed later (*keys_buf and *arg_buf).
474 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200475 // If something like <C-H> is simplified to 0x08 then mark it as simplified
476 // and also add a n entry with a modifier, which will work when
477 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200478 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200479 {
480 char_u *new_keys;
481 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
482
483 if (special)
484 flags |= REPTERM_SPECIAL;
485 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
486 if (did_simplify)
487 (void)replace_termcodes(keys, &alt_keys_buf,
488 flags | REPTERM_NO_SIMPLIFY, NULL);
489 keys = new_keys;
490 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200491 orig_rhs = rhs;
492 if (hasarg)
493 {
494 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
495 rhs = (char_u *)"";
496 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200497 rhs = replace_termcodes(rhs, &arg_buf,
498 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200499 }
500
Bram Moolenaar459fd782019-10-13 16:43:39 +0200501 /*
502 * The following is done twice if we have two versions of keys:
503 * "alt_keys_buf" is not NULL.
504 */
505 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200506 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200507 int did_it = FALSE;
508 int did_local = FALSE;
509 int round;
510 int hash;
511 int new_hash;
512
513 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200514 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200515 if (alt_keys_buf == NULL)
516 break;
517 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200518 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200519 else if (alt_keys_buf != NULL && do_print)
520 // when printing always use the not-simplified map
521 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200522
Bram Moolenaar459fd782019-10-13 16:43:39 +0200523 // check arguments and translate function keys
524 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200525 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200526 len = (int)STRLEN(keys);
527 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200528 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200529 retval = 1;
530 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200531 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200532
533 if (abbrev && maptype != 1)
534 {
535 // If an abbreviation ends in a keyword character, the
536 // rest must be all keyword-char or all non-keyword-char.
537 // Otherwise we won't be able to find the start of it in a
538 // vi-compatible way.
539 if (has_mbyte)
540 {
541 int first, last;
542 int same = -1;
543
544 first = vim_iswordp(keys);
545 last = first;
546 p = keys + (*mb_ptr2len)(keys);
547 n = 1;
548 while (p < keys + len)
549 {
550 ++n; // nr of (multi-byte) chars
551 last = vim_iswordp(p); // type of last char
552 if (same == -1 && last != first)
553 same = n - 1; // count of same char type
554 p += (*mb_ptr2len)(p);
555 }
556 if (last && n > 2 && same >= 0 && same < n - 1)
557 {
558 retval = 1;
559 goto theend;
560 }
561 }
562 else if (vim_iswordc(keys[len - 1]))
563 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200564 for (n = 0; n < len - 2; ++n)
565 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
566 {
567 retval = 1;
568 goto theend;
569 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200570 // An abbreviation cannot contain white space.
571 for (n = 0; n < len; ++n)
572 if (VIM_ISWHITE(keys[n]))
573 {
574 retval = 1;
575 goto theend;
576 }
577 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200578 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200579
Bram Moolenaar459fd782019-10-13 16:43:39 +0200580 if (haskey && hasarg && abbrev) // if we will add an abbreviation
581 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200582 // no abbreviations
583
Bram Moolenaar459fd782019-10-13 16:43:39 +0200584 if (do_print)
585 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200586
Bram Moolenaar459fd782019-10-13 16:43:39 +0200587 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200588 if (unique && map_table == curbuf->b_maphash
589 && haskey && hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200590 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200591 // need to loop over all global hash lists
592 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200593 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200594 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200595 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200596 if (hash != 0) // there is only one abbreviation list
597 break;
598 mp = first_abbr;
599 }
600 else
601 mp = maphash[hash];
602 for ( ; mp != NULL && !got_int; mp = mp->m_next)
603 {
604 // check entries with the same mode
605 if ((mp->m_mode & mode) != 0
606 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200607 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
608 {
609 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000610 semsg(
611 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200612 mp->m_keys);
613 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000614 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200615 mp->m_keys);
616 retval = 5;
617 goto theend;
618 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200619 }
620 }
621 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200622
Bram Moolenaar459fd782019-10-13 16:43:39 +0200623 // When listing global mappings, also list buffer-local ones here.
624 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200625 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200626 // need to loop over all global hash lists
627 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200628 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200629 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200630 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200631 if (hash != 0) // there is only one abbreviation list
632 break;
633 mp = curbuf->b_first_abbr;
634 }
635 else
636 mp = curbuf->b_maphash[hash];
637 for ( ; mp != NULL && !got_int; mp = mp->m_next)
638 {
639 // check entries with the same mode
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200640 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200641 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200642 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200643 {
644 showmap(mp, TRUE);
645 did_local = TRUE;
646 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200647 else
648 {
649 n = mp->m_keylen;
650 if (STRNCMP(mp->m_keys, keys,
651 (size_t)(n < len ? n : len)) == 0)
652 {
653 showmap(mp, TRUE);
654 did_local = TRUE;
655 }
656 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200657 }
658 }
659 }
660 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200661
Bram Moolenaar459fd782019-10-13 16:43:39 +0200662 // Find an entry in the maphash[] list that matches.
663 // For :unmap we may loop two times: once to try to unmap an entry with
664 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000665 // an entry with a matching 'to' part. This was done to allow
666 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
667 // be replaced by "bar" because of the abbreviation.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200668 for (round = 0; (round == 0 || maptype == 1) && round <= 1
669 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200670 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200671 // need to loop over all hash lists
672 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200673 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200674 if (abbrev)
675 {
676 if (hash > 0) // there is only one abbreviation list
677 break;
678 mpp = abbr_table;
679 }
680 else
681 mpp = &(map_table[hash]);
682 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
683 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200684
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200685 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200686 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200687 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200688 mpp = &(mp->m_next);
689 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200690 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200691 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200692 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200693 if (!mp->m_simplified)
694 {
695 showmap(mp, map_table != maphash);
696 did_it = TRUE;
697 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200698 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200699 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200700 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200701 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200702 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200703 n = (int)STRLEN(mp->m_str);
704 p = mp->m_str;
705 }
706 else
707 {
708 n = mp->m_keylen;
709 p = mp->m_keys;
710 }
711 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
712 {
713 if (maptype == 1)
714 {
715 // Delete entry.
716 // Only accept a full match. For abbreviations
717 // we ignore trailing space when matching with
718 // the "lhs", since an abbreviation can't have
719 // trailing space.
720 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200721 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200722 {
723 mpp = &(mp->m_next);
724 continue;
725 }
726 // We reset the indicated mode bits. If nothing
727 // is left the entry is deleted below.
728 mp->m_mode &= ~mode;
729 did_it = TRUE; // remember we did something
730 }
731 else if (!hasarg) // show matching entry
732 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200733 if (!mp->m_simplified)
734 {
735 showmap(mp, map_table != maphash);
736 did_it = TRUE;
737 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200738 }
739 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200740 {
741 mpp = &(mp->m_next);
742 continue;
743 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200744 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200745 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200746 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000747 semsg(
748 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200749 p);
750 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000751 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200752 p);
753 retval = 5;
754 goto theend;
755 }
756 else
757 {
758 // new rhs for existing entry
759 mp->m_mode &= ~mode; // remove mode bits
760 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200761 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200762 char_u *newstr = vim_strsave(rhs);
763
764 if (newstr == NULL)
765 {
766 retval = 4; // no mem
767 goto theend;
768 }
769 vim_free(mp->m_str);
770 mp->m_str = newstr;
771 vim_free(mp->m_orig_str);
772 mp->m_orig_str = vim_strsave(orig_rhs);
773 mp->m_noremap = noremap;
774 mp->m_nowait = nowait;
775 mp->m_silent = silent;
776 mp->m_mode = mode;
777 mp->m_simplified =
778 did_simplify && keyround == 1;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200779#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200780 mp->m_expr = expr;
781 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100782 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200783#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200784 did_it = TRUE;
785 }
786 }
787 if (mp->m_mode == 0) // entry can be deleted
788 {
789 map_free(mpp);
790 continue; // continue with *mpp
791 }
792
793 // May need to put this entry into another hash
794 // list.
795 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
796 if (!abbrev && new_hash != hash)
797 {
798 *mpp = mp->m_next;
799 mp->m_next = map_table[new_hash];
800 map_table[new_hash] = mp;
801
802 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200803 }
804 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200805 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200806 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200807 }
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 if (maptype == 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200812 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200813 // delete entry
814 if (!did_it)
815 retval = 2; // no match
816 else if (*keys == Ctrl_C)
817 {
818 // If CTRL-C has been unmapped, reuse it for Interrupting.
819 if (map_table == curbuf->b_maphash)
820 curbuf->b_mapped_ctrl_c &= ~mode;
821 else
822 mapped_ctrl_c &= ~mode;
823 }
824 continue;
825 }
826
827 if (!haskey || !hasarg)
828 {
829 // print entries
830 if (!did_it && !did_local)
831 {
832 if (abbrev)
833 msg(_("No abbreviation found"));
834 else
835 msg(_("No mapping found"));
836 }
837 goto theend; // listing finished
838 }
839
840 if (did_it)
841 continue; // have added the new entry already
842
843 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200844 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
845 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200846#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200847 expr, /* sid */ -1, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200848#endif
849 did_simplify && keyround == 1) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200850 {
851 retval = 4; // no mem
852 goto theend;
853 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200854 }
855
856theend:
857 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200858 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200859 vim_free(arg_buf);
860 return retval;
861}
862
863/*
864 * Get the mapping mode from the command name.
865 */
866 static int
867get_map_mode(char_u **cmdp, int forceit)
868{
869 char_u *p;
870 int modec;
871 int mode;
872
873 p = *cmdp;
874 modec = *p++;
875 if (modec == 'i')
876 mode = INSERT; // :imap
877 else if (modec == 'l')
878 mode = LANGMAP; // :lmap
879 else if (modec == 'c')
880 mode = CMDLINE; // :cmap
881 else if (modec == 'n' && *p != 'o') // avoid :noremap
882 mode = NORMAL; // :nmap
883 else if (modec == 'v')
884 mode = VISUAL + SELECTMODE; // :vmap
885 else if (modec == 'x')
886 mode = VISUAL; // :xmap
887 else if (modec == 's')
888 mode = SELECTMODE; // :smap
889 else if (modec == 'o')
890 mode = OP_PENDING; // :omap
891 else if (modec == 't')
892 mode = TERMINAL; // :tmap
893 else
894 {
895 --p;
896 if (forceit)
897 mode = INSERT + CMDLINE; // :map !
898 else
899 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;// :map
900 }
901
902 *cmdp = p;
903 return mode;
904}
905
906/*
907 * Clear all mappings or abbreviations.
908 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
909 */
910 static void
911map_clear(
912 char_u *cmdp,
913 char_u *arg UNUSED,
914 int forceit,
915 int abbr)
916{
917 int mode;
918 int local;
919
920 local = (STRCMP(arg, "<buffer>") == 0);
921 if (!local && *arg != NUL)
922 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000923 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200924 return;
925 }
926
927 mode = get_map_mode(&cmdp, forceit);
928 map_clear_int(curbuf, mode, local, abbr);
929}
930
931/*
932 * Clear all mappings in "mode".
933 */
934 void
935map_clear_int(
936 buf_T *buf, // buffer for local mappings
937 int mode, // mode in which to delete
938 int local, // TRUE for buffer-local mappings
939 int abbr) // TRUE for abbreviations
940{
941 mapblock_T *mp, **mpp;
942 int hash;
943 int new_hash;
944
945 validate_maphash();
946
947 for (hash = 0; hash < 256; ++hash)
948 {
949 if (abbr)
950 {
951 if (hash > 0) // there is only one abbrlist
952 break;
953 if (local)
954 mpp = &buf->b_first_abbr;
955 else
956 mpp = &first_abbr;
957 }
958 else
959 {
960 if (local)
961 mpp = &buf->b_maphash[hash];
962 else
963 mpp = &maphash[hash];
964 }
965 while (*mpp != NULL)
966 {
967 mp = *mpp;
968 if (mp->m_mode & mode)
969 {
970 mp->m_mode &= ~mode;
971 if (mp->m_mode == 0) // entry can be deleted
972 {
973 map_free(mpp);
974 continue;
975 }
976 // May need to put this entry into another hash list.
977 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
978 if (!abbr && new_hash != hash)
979 {
980 *mpp = mp->m_next;
981 if (local)
982 {
983 mp->m_next = buf->b_maphash[new_hash];
984 buf->b_maphash[new_hash] = mp;
985 }
986 else
987 {
988 mp->m_next = maphash[new_hash];
989 maphash[new_hash] = mp;
990 }
991 continue; // continue with *mpp
992 }
993 }
994 mpp = &(mp->m_next);
995 }
996 }
997}
998
999#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001000 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001001mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001002{
1003 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001004
1005 if (vim_strchr(modechars, 'n') != NULL)
1006 mode |= NORMAL;
1007 if (vim_strchr(modechars, 'v') != NULL)
1008 mode |= VISUAL + SELECTMODE;
1009 if (vim_strchr(modechars, 'x') != NULL)
1010 mode |= VISUAL;
1011 if (vim_strchr(modechars, 's') != NULL)
1012 mode |= SELECTMODE;
1013 if (vim_strchr(modechars, 'o') != NULL)
1014 mode |= OP_PENDING;
1015 if (vim_strchr(modechars, 'i') != NULL)
1016 mode |= INSERT;
1017 if (vim_strchr(modechars, 'l') != NULL)
1018 mode |= LANGMAP;
1019 if (vim_strchr(modechars, 'c') != NULL)
1020 mode |= CMDLINE;
1021
Bram Moolenaar581ba392019-09-03 22:08:33 +02001022 return mode;
1023}
1024
1025/*
1026 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1027 * Recognize termcap codes in "str".
1028 * Also checks mappings local to the current buffer.
1029 */
1030 int
1031map_to_exists(char_u *str, char_u *modechars, int abbr)
1032{
1033 char_u *rhs;
1034 char_u *buf;
1035 int retval;
1036
Bram Moolenaar459fd782019-10-13 16:43:39 +02001037 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001038
1039 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001040 vim_free(buf);
1041
1042 return retval;
1043}
1044#endif
1045
1046/*
1047 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1048 * Also checks mappings local to the current buffer.
1049 */
1050 int
1051map_to_exists_mode(char_u *rhs, int mode, int abbr)
1052{
1053 mapblock_T *mp;
1054 int hash;
1055 int exp_buffer = FALSE;
1056
1057 validate_maphash();
1058
1059 // Do it twice: once for global maps and once for local maps.
1060 for (;;)
1061 {
1062 for (hash = 0; hash < 256; ++hash)
1063 {
1064 if (abbr)
1065 {
1066 if (hash > 0) // there is only one abbr list
1067 break;
1068 if (exp_buffer)
1069 mp = curbuf->b_first_abbr;
1070 else
1071 mp = first_abbr;
1072 }
1073 else if (exp_buffer)
1074 mp = curbuf->b_maphash[hash];
1075 else
1076 mp = maphash[hash];
1077 for (; mp; mp = mp->m_next)
1078 {
1079 if ((mp->m_mode & mode)
1080 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1081 return TRUE;
1082 }
1083 }
1084 if (exp_buffer)
1085 break;
1086 exp_buffer = TRUE;
1087 }
1088
1089 return FALSE;
1090}
1091
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001092/*
1093 * Used below when expanding mapping/abbreviation names.
1094 */
1095static int expand_mapmodes = 0;
1096static int expand_isabbrev = 0;
1097static int expand_buffer = FALSE;
1098
1099/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001100 * Translate an internal mapping/abbreviation representation into the
1101 * corresponding external one recognized by :map/:abbrev commands.
1102 * Respects the current B/k/< settings of 'cpoption'.
1103 *
1104 * This function is called when expanding mappings/abbreviations on the
1105 * command-line.
1106 *
1107 * It uses a growarray to build the translation string since the latter can be
1108 * wider than the original description. The caller has to free the string
1109 * afterwards.
1110 *
1111 * Returns NULL when there is a problem.
1112 */
1113 static char_u *
1114translate_mapping(char_u *str)
1115{
1116 garray_T ga;
1117 int c;
1118 int modifiers;
1119 int cpo_bslash;
1120 int cpo_special;
1121
1122 ga_init(&ga);
1123 ga.ga_itemsize = 1;
1124 ga.ga_growsize = 40;
1125
1126 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1127 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1128
1129 for (; *str; ++str)
1130 {
1131 c = *str;
1132 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1133 {
1134 modifiers = 0;
1135 if (str[1] == KS_MODIFIER)
1136 {
1137 str++;
1138 modifiers = *++str;
1139 c = *++str;
1140 }
1141 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1142 {
1143 if (cpo_special)
1144 {
1145 ga_clear(&ga);
1146 return NULL;
1147 }
1148 c = TO_SPECIAL(str[1], str[2]);
1149 if (c == K_ZERO) // display <Nul> as ^@
1150 c = NUL;
1151 str += 2;
1152 }
1153 if (IS_SPECIAL(c) || modifiers) // special key
1154 {
1155 if (cpo_special)
1156 {
1157 ga_clear(&ga);
1158 return NULL;
1159 }
1160 ga_concat(&ga, get_special_key_name(c, modifiers));
1161 continue; // for (str)
1162 }
1163 }
1164 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1165 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1166 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1167 if (c)
1168 ga_append(&ga, c);
1169 }
1170 ga_append(&ga, NUL);
1171 return (char_u *)(ga.ga_data);
1172}
1173
1174/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001175 * Work out what to complete when doing command line completion of mapping
1176 * or abbreviation names.
1177 */
1178 char_u *
1179set_context_in_map_cmd(
1180 expand_T *xp,
1181 char_u *cmd,
1182 char_u *arg,
1183 int forceit, // TRUE if '!' given
1184 int isabbrev, // TRUE if abbreviation
1185 int isunmap, // TRUE if unmap/unabbrev command
1186 cmdidx_T cmdidx)
1187{
1188 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1189 xp->xp_context = EXPAND_NOTHING;
1190 else
1191 {
1192 if (isunmap)
1193 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1194 else
1195 {
1196 expand_mapmodes = INSERT + CMDLINE;
1197 if (!isabbrev)
1198 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
1199 }
1200 expand_isabbrev = isabbrev;
1201 xp->xp_context = EXPAND_MAPPINGS;
1202 expand_buffer = FALSE;
1203 for (;;)
1204 {
1205 if (STRNCMP(arg, "<buffer>", 8) == 0)
1206 {
1207 expand_buffer = TRUE;
1208 arg = skipwhite(arg + 8);
1209 continue;
1210 }
1211 if (STRNCMP(arg, "<unique>", 8) == 0)
1212 {
1213 arg = skipwhite(arg + 8);
1214 continue;
1215 }
1216 if (STRNCMP(arg, "<nowait>", 8) == 0)
1217 {
1218 arg = skipwhite(arg + 8);
1219 continue;
1220 }
1221 if (STRNCMP(arg, "<silent>", 8) == 0)
1222 {
1223 arg = skipwhite(arg + 8);
1224 continue;
1225 }
1226 if (STRNCMP(arg, "<special>", 9) == 0)
1227 {
1228 arg = skipwhite(arg + 9);
1229 continue;
1230 }
1231#ifdef FEAT_EVAL
1232 if (STRNCMP(arg, "<script>", 8) == 0)
1233 {
1234 arg = skipwhite(arg + 8);
1235 continue;
1236 }
1237 if (STRNCMP(arg, "<expr>", 6) == 0)
1238 {
1239 arg = skipwhite(arg + 6);
1240 continue;
1241 }
1242#endif
1243 break;
1244 }
1245 xp->xp_pattern = arg;
1246 }
1247
1248 return NULL;
1249}
1250
1251/*
1252 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1253 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1254 * Return OK if matches found, FAIL otherwise.
1255 */
1256 int
1257ExpandMappings(
1258 regmatch_T *regmatch,
1259 int *num_file,
1260 char_u ***file)
1261{
1262 mapblock_T *mp;
1263 int hash;
1264 int count;
1265 int round;
1266 char_u *p;
1267 int i;
1268
1269 validate_maphash();
1270
1271 *num_file = 0; // return values in case of FAIL
1272 *file = NULL;
1273
1274 // round == 1: Count the matches.
1275 // round == 2: Build the array to keep the matches.
1276 for (round = 1; round <= 2; ++round)
1277 {
1278 count = 0;
1279
1280 for (i = 0; i < 7; ++i)
1281 {
1282 if (i == 0)
1283 p = (char_u *)"<silent>";
1284 else if (i == 1)
1285 p = (char_u *)"<unique>";
1286#ifdef FEAT_EVAL
1287 else if (i == 2)
1288 p = (char_u *)"<script>";
1289 else if (i == 3)
1290 p = (char_u *)"<expr>";
1291#endif
1292 else if (i == 4 && !expand_buffer)
1293 p = (char_u *)"<buffer>";
1294 else if (i == 5)
1295 p = (char_u *)"<nowait>";
1296 else if (i == 6)
1297 p = (char_u *)"<special>";
1298 else
1299 continue;
1300
1301 if (vim_regexec(regmatch, p, (colnr_T)0))
1302 {
1303 if (round == 1)
1304 ++count;
1305 else
1306 (*file)[count++] = vim_strsave(p);
1307 }
1308 }
1309
1310 for (hash = 0; hash < 256; ++hash)
1311 {
1312 if (expand_isabbrev)
1313 {
1314 if (hash > 0) // only one abbrev list
1315 break; // for (hash)
1316 mp = first_abbr;
1317 }
1318 else if (expand_buffer)
1319 mp = curbuf->b_maphash[hash];
1320 else
1321 mp = maphash[hash];
1322 for (; mp; mp = mp->m_next)
1323 {
1324 if (mp->m_mode & expand_mapmodes)
1325 {
1326 p = translate_mapping(mp->m_keys);
1327 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
1328 {
1329 if (round == 1)
1330 ++count;
1331 else
1332 {
1333 (*file)[count++] = p;
1334 p = NULL;
1335 }
1336 }
1337 vim_free(p);
1338 }
1339 } // for (mp)
1340 } // for (hash)
1341
1342 if (count == 0) // no match found
1343 break; // for (round)
1344
1345 if (round == 1)
1346 {
1347 *file = ALLOC_MULT(char_u *, count);
1348 if (*file == NULL)
1349 return FAIL;
1350 }
1351 } // for (round)
1352
1353 if (count > 1)
1354 {
1355 char_u **ptr1;
1356 char_u **ptr2;
1357 char_u **ptr3;
1358
1359 // Sort the matches
1360 sort_strings(*file, count);
1361
1362 // Remove multiple entries
1363 ptr1 = *file;
1364 ptr2 = ptr1 + 1;
1365 ptr3 = ptr1 + count;
1366
1367 while (ptr2 < ptr3)
1368 {
1369 if (STRCMP(*ptr1, *ptr2))
1370 *++ptr1 = *ptr2++;
1371 else
1372 {
1373 vim_free(*ptr2++);
1374 count--;
1375 }
1376 }
1377 }
1378
1379 *num_file = count;
1380 return (count == 0 ? FAIL : OK);
1381}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001382
1383/*
1384 * Check for an abbreviation.
1385 * Cursor is at ptr[col].
1386 * When inserting, mincol is where insert started.
1387 * For the command line, mincol is what is to be skipped over.
1388 * "c" is the character typed before check_abbr was called. It may have
1389 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1390 *
1391 * Historic vi practice: The last character of an abbreviation must be an id
1392 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1393 * characters or all non-id characters. This allows for abbr. "#i" to
1394 * "#include".
1395 *
1396 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1397 * Then there must be white space before the abbr.
1398 *
1399 * return TRUE if there is an abbreviation, FALSE if not
1400 */
1401 int
1402check_abbr(
1403 int c,
1404 char_u *ptr,
1405 int col,
1406 int mincol)
1407{
1408 int len;
1409 int scol; // starting column of the abbr.
1410 int j;
1411 char_u *s;
1412 char_u tb[MB_MAXBYTES + 4];
1413 mapblock_T *mp;
1414 mapblock_T *mp2;
1415 int clen = 0; // length in characters
1416 int is_id = TRUE;
1417 int vim_abbr;
1418
1419 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1420 return FALSE;
1421
1422 // no remapping implies no abbreviation, except for CTRL-]
1423 if (noremap_keys() && c != Ctrl_RSB)
1424 return FALSE;
1425
1426 // Check for word before the cursor: If it ends in a keyword char all
1427 // chars before it must be keyword chars or non-keyword chars, but not
1428 // white space. If it ends in a non-keyword char we accept any characters
1429 // before it except white space.
1430 if (col == 0) // cannot be an abbr.
1431 return FALSE;
1432
1433 if (has_mbyte)
1434 {
1435 char_u *p;
1436
1437 p = mb_prevptr(ptr, ptr + col);
1438 if (!vim_iswordp(p))
1439 vim_abbr = TRUE; // Vim added abbr.
1440 else
1441 {
1442 vim_abbr = FALSE; // vi compatible abbr.
1443 if (p > ptr)
1444 is_id = vim_iswordp(mb_prevptr(ptr, p));
1445 }
1446 clen = 1;
1447 while (p > ptr + mincol)
1448 {
1449 p = mb_prevptr(ptr, p);
1450 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1451 {
1452 p += (*mb_ptr2len)(p);
1453 break;
1454 }
1455 ++clen;
1456 }
1457 scol = (int)(p - ptr);
1458 }
1459 else
1460 {
1461 if (!vim_iswordc(ptr[col - 1]))
1462 vim_abbr = TRUE; // Vim added abbr.
1463 else
1464 {
1465 vim_abbr = FALSE; // vi compatible abbr.
1466 if (col > 1)
1467 is_id = vim_iswordc(ptr[col - 2]);
1468 }
1469 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1470 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1471 ;
1472 }
1473
1474 if (scol < mincol)
1475 scol = mincol;
1476 if (scol < col) // there is a word in front of the cursor
1477 {
1478 ptr += scol;
1479 len = col - scol;
1480 mp = curbuf->b_first_abbr;
1481 mp2 = first_abbr;
1482 if (mp == NULL)
1483 {
1484 mp = mp2;
1485 mp2 = NULL;
1486 }
1487 for ( ; mp; mp->m_next == NULL
1488 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1489 {
1490 int qlen = mp->m_keylen;
1491 char_u *q = mp->m_keys;
1492 int match;
1493
1494 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1495 {
1496 char_u *qe = vim_strsave(mp->m_keys);
1497
1498 // might have CSI escaped mp->m_keys
1499 if (qe != NULL)
1500 {
1501 q = qe;
1502 vim_unescape_csi(q);
1503 qlen = (int)STRLEN(q);
1504 }
1505 }
1506
1507 // find entries with right mode and keys
1508 match = (mp->m_mode & State)
1509 && qlen == len
1510 && !STRNCMP(q, ptr, (size_t)len);
1511 if (q != mp->m_keys)
1512 vim_free(q);
1513 if (match)
1514 break;
1515 }
1516 if (mp != NULL)
1517 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001518 int noremap;
1519 int silent;
1520#ifdef FEAT_EVAL
1521 int expr;
1522#endif
1523
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001524 // Found a match:
1525 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1526 // This goes from end to start.
1527 //
1528 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1529 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1530 // Characters where IS_SPECIAL() == TRUE: key codes, need
1531 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1532 //
1533 // Character CTRL-] is treated specially - it completes the
1534 // abbreviation, but is not inserted into the input stream.
1535 j = 0;
1536 if (c != Ctrl_RSB)
1537 {
1538 // special key code, split up
1539 if (IS_SPECIAL(c) || c == K_SPECIAL)
1540 {
1541 tb[j++] = K_SPECIAL;
1542 tb[j++] = K_SECOND(c);
1543 tb[j++] = K_THIRD(c);
1544 }
1545 else
1546 {
1547 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1548 tb[j++] = Ctrl_V; // special char needs CTRL-V
1549 if (has_mbyte)
1550 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001551 int newlen;
1552 char_u *escaped;
1553
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001554 // if ABBR_OFF has been added, remove it here
1555 if (c >= ABBR_OFF)
1556 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001557 newlen = (*mb_char2bytes)(c, tb + j);
1558 tb[j + newlen] = NUL;
1559 // Need to escape K_SPECIAL.
1560 escaped = vim_strsave_escape_csi(tb + j);
1561 if (escaped != NULL)
1562 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001563 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001564 mch_memmove(tb + j, escaped, newlen);
1565 j += newlen;
1566 vim_free(escaped);
1567 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001568 }
1569 else
1570 tb[j++] = c;
1571 }
1572 tb[j] = NUL;
1573 // insert the last typed char
1574 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1575 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001576
1577 // copy values here, calling eval_map_expr() may make "mp" invalid!
1578 noremap = mp->m_noremap;
1579 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001580#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001581 expr = mp->m_expr;
1582
1583 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001584 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001585 else
1586#endif
1587 s = mp->m_str;
1588 if (s != NULL)
1589 {
1590 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001591 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001592 // no abbrev. for these chars
1593 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1594#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001595 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001596 vim_free(s);
1597#endif
1598 }
1599
1600 tb[0] = Ctrl_H;
1601 tb[1] = NUL;
1602 if (has_mbyte)
1603 len = clen; // Delete characters instead of bytes
1604 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001605 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001606 return TRUE;
1607 }
1608 }
1609 return FALSE;
1610}
1611
1612#ifdef FEAT_EVAL
1613/*
1614 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1615 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001616 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001617 */
1618 char_u *
1619eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001620 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001621 int c) // NUL or typed character for abbreviation
1622{
1623 char_u *res;
1624 char_u *p;
1625 char_u *expr;
1626 pos_T save_cursor;
1627 int save_msg_col;
1628 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001629 scid_T save_sctx_sid = current_sctx.sc_sid;
1630 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001631
1632 // Remove escaping of CSI, because "str" is in a format to be used as
1633 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001634 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001635 if (expr == NULL)
1636 return NULL;
1637 vim_unescape_csi(expr);
1638
1639 // Forbid changing text or using ":normal" to avoid most of the bad side
1640 // effects. Also restore the cursor position.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001641 ++textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001642 ++ex_normal_lock;
1643 set_vim_var_char(c); // set v:char to the typed character
1644 save_cursor = curwin->w_cursor;
1645 save_msg_col = msg_col;
1646 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001647 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1648 {
1649 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1650 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1651 }
1652
1653 // Note: the evaluation may make "mp" invalid.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001654 p = eval_to_string(expr, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001655
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001656 --textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001657 --ex_normal_lock;
1658 curwin->w_cursor = save_cursor;
1659 msg_col = save_msg_col;
1660 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001661 current_sctx.sc_sid = save_sctx_sid;
1662 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001663
1664 vim_free(expr);
1665
1666 if (p == NULL)
1667 return NULL;
1668 // Escape CSI in the result to be able to use the string as typeahead.
1669 res = vim_strsave_escape_csi(p);
1670 vim_free(p);
1671
1672 return res;
1673}
1674#endif
1675
1676/*
1677 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1678 * can be put in the typeahead buffer.
1679 * Returns NULL when out of memory.
1680 */
1681 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001682vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001683{
1684 char_u *res;
1685 char_u *s, *d;
1686
1687 // Need a buffer to hold up to three times as much. Four in case of an
1688 // illegal utf-8 byte:
1689 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1690 res = alloc(STRLEN(p) * 4 + 1);
1691 if (res != NULL)
1692 {
1693 d = res;
1694 for (s = p; *s != NUL; )
1695 {
1696 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1697 {
1698 // Copy special key unmodified.
1699 *d++ = *s++;
1700 *d++ = *s++;
1701 *d++ = *s++;
1702 }
1703 else
1704 {
1705 // Add character, possibly multi-byte to destination, escaping
1706 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1707 d = add_char2buf(PTR2CHAR(s), d);
1708 s += MB_CPTR2LEN(s);
1709 }
1710 }
1711 *d = NUL;
1712 }
1713 return res;
1714}
1715
1716/*
1717 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1718 * vim_strsave_escape_csi(). Works in-place.
1719 */
1720 void
1721vim_unescape_csi(char_u *p)
1722{
1723 char_u *s = p, *d = p;
1724
1725 while (*s != NUL)
1726 {
1727 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1728 {
1729 *d++ = K_SPECIAL;
1730 s += 3;
1731 }
1732 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1733 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1734 {
1735 *d++ = CSI;
1736 s += 3;
1737 }
1738 else
1739 *d++ = *s++;
1740 }
1741 *d = NUL;
1742}
1743
1744/*
1745 * Write map commands for the current mappings to an .exrc file.
1746 * Return FAIL on error, OK otherwise.
1747 */
1748 int
1749makemap(
1750 FILE *fd,
1751 buf_T *buf) // buffer for local mappings or NULL
1752{
1753 mapblock_T *mp;
1754 char_u c1, c2, c3;
1755 char_u *p;
1756 char *cmd;
1757 int abbr;
1758 int hash;
1759 int did_cpo = FALSE;
1760 int i;
1761
1762 validate_maphash();
1763
1764 // Do the loop twice: Once for mappings, once for abbreviations.
1765 // Then loop over all map hash lists.
1766 for (abbr = 0; abbr < 2; ++abbr)
1767 for (hash = 0; hash < 256; ++hash)
1768 {
1769 if (abbr)
1770 {
1771 if (hash > 0) // there is only one abbr list
1772 break;
1773 if (buf != NULL)
1774 mp = buf->b_first_abbr;
1775 else
1776 mp = first_abbr;
1777 }
1778 else
1779 {
1780 if (buf != NULL)
1781 mp = buf->b_maphash[hash];
1782 else
1783 mp = maphash[hash];
1784 }
1785
1786 for ( ; mp; mp = mp->m_next)
1787 {
1788 // skip script-local mappings
1789 if (mp->m_noremap == REMAP_SCRIPT)
1790 continue;
1791
1792 // skip mappings that contain a <SNR> (script-local thing),
1793 // they probably don't work when loaded again
1794 for (p = mp->m_str; *p != NUL; ++p)
1795 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1796 && p[2] == (int)KE_SNR)
1797 break;
1798 if (*p != NUL)
1799 continue;
1800
1801 // It's possible to create a mapping and then ":unmap" certain
1802 // modes. We recreate this here by mapping the individual
1803 // modes, which requires up to three of them.
1804 c1 = NUL;
1805 c2 = NUL;
1806 c3 = NUL;
1807 if (abbr)
1808 cmd = "abbr";
1809 else
1810 cmd = "map";
1811 switch (mp->m_mode)
1812 {
1813 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
1814 break;
1815 case NORMAL:
1816 c1 = 'n';
1817 break;
1818 case VISUAL:
1819 c1 = 'x';
1820 break;
1821 case SELECTMODE:
1822 c1 = 's';
1823 break;
1824 case OP_PENDING:
1825 c1 = 'o';
1826 break;
1827 case NORMAL + VISUAL:
1828 c1 = 'n';
1829 c2 = 'x';
1830 break;
1831 case NORMAL + SELECTMODE:
1832 c1 = 'n';
1833 c2 = 's';
1834 break;
1835 case NORMAL + OP_PENDING:
1836 c1 = 'n';
1837 c2 = 'o';
1838 break;
1839 case VISUAL + SELECTMODE:
1840 c1 = 'v';
1841 break;
1842 case VISUAL + OP_PENDING:
1843 c1 = 'x';
1844 c2 = 'o';
1845 break;
1846 case SELECTMODE + OP_PENDING:
1847 c1 = 's';
1848 c2 = 'o';
1849 break;
1850 case NORMAL + VISUAL + SELECTMODE:
1851 c1 = 'n';
1852 c2 = 'v';
1853 break;
1854 case NORMAL + VISUAL + OP_PENDING:
1855 c1 = 'n';
1856 c2 = 'x';
1857 c3 = 'o';
1858 break;
1859 case NORMAL + SELECTMODE + OP_PENDING:
1860 c1 = 'n';
1861 c2 = 's';
1862 c3 = 'o';
1863 break;
1864 case VISUAL + SELECTMODE + OP_PENDING:
1865 c1 = 'v';
1866 c2 = 'o';
1867 break;
1868 case CMDLINE + INSERT:
1869 if (!abbr)
1870 cmd = "map!";
1871 break;
1872 case CMDLINE:
1873 c1 = 'c';
1874 break;
1875 case INSERT:
1876 c1 = 'i';
1877 break;
1878 case LANGMAP:
1879 c1 = 'l';
1880 break;
1881 case TERMINAL:
1882 c1 = 't';
1883 break;
1884 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001885 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001886 return FAIL;
1887 }
1888 do // do this twice if c2 is set, 3 times with c3
1889 {
1890 // When outputting <> form, need to make sure that 'cpo'
1891 // is set to the Vim default.
1892 if (!did_cpo)
1893 {
1894 if (*mp->m_str == NUL) // will use <Nop>
1895 did_cpo = TRUE;
1896 else
1897 for (i = 0; i < 2; ++i)
1898 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1899 if (*p == K_SPECIAL || *p == NL)
1900 did_cpo = TRUE;
1901 if (did_cpo)
1902 {
1903 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1904 || put_eol(fd) < 0
1905 || fprintf(fd, "set cpo&vim") < 0
1906 || put_eol(fd) < 0)
1907 return FAIL;
1908 }
1909 }
1910 if (c1 && putc(c1, fd) < 0)
1911 return FAIL;
1912 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1913 return FAIL;
1914 if (fputs(cmd, fd) < 0)
1915 return FAIL;
1916 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1917 return FAIL;
1918 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1919 return FAIL;
1920 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1921 return FAIL;
1922#ifdef FEAT_EVAL
1923 if (mp->m_noremap == REMAP_SCRIPT
1924 && fputs("<script>", fd) < 0)
1925 return FAIL;
1926 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1927 return FAIL;
1928#endif
1929
1930 if ( putc(' ', fd) < 0
1931 || put_escstr(fd, mp->m_keys, 0) == FAIL
1932 || putc(' ', fd) < 0
1933 || put_escstr(fd, mp->m_str, 1) == FAIL
1934 || put_eol(fd) < 0)
1935 return FAIL;
1936 c1 = c2;
1937 c2 = c3;
1938 c3 = NUL;
1939 } while (c1 != NUL);
1940 }
1941 }
1942
1943 if (did_cpo)
1944 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
1945 || put_eol(fd) < 0
1946 || fprintf(fd, "unlet s:cpo_save") < 0
1947 || put_eol(fd) < 0)
1948 return FAIL;
1949 return OK;
1950}
1951
1952/*
1953 * write escape string to file
1954 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
1955 *
1956 * return FAIL for failure, OK otherwise
1957 */
1958 int
1959put_escstr(FILE *fd, char_u *strstart, int what)
1960{
1961 char_u *str = strstart;
1962 int c;
1963 int modifiers;
1964
1965 // :map xx <Nop>
1966 if (*str == NUL && what == 1)
1967 {
1968 if (fprintf(fd, "<Nop>") < 0)
1969 return FAIL;
1970 return OK;
1971 }
1972
1973 for ( ; *str != NUL; ++str)
1974 {
1975 char_u *p;
1976
1977 // Check for a multi-byte character, which may contain escaped
1978 // K_SPECIAL and CSI bytes
1979 p = mb_unescape(&str);
1980 if (p != NULL)
1981 {
1982 while (*p != NUL)
1983 if (fputc(*p++, fd) < 0)
1984 return FAIL;
1985 --str;
1986 continue;
1987 }
1988
1989 c = *str;
1990 // Special key codes have to be translated to be able to make sense
1991 // when they are read back.
1992 if (c == K_SPECIAL && what != 2)
1993 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02001994 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001995 if (str[1] == KS_MODIFIER)
1996 {
1997 modifiers = str[2];
1998 str += 3;
1999 c = *str;
2000 }
2001 if (c == K_SPECIAL)
2002 {
2003 c = TO_SPECIAL(str[1], str[2]);
2004 str += 2;
2005 }
2006 if (IS_SPECIAL(c) || modifiers) // special key
2007 {
2008 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2009 return FAIL;
2010 continue;
2011 }
2012 }
2013
2014 // A '\n' in a map command should be written as <NL>.
2015 // A '\n' in a set command should be written as \^V^J.
2016 if (c == NL)
2017 {
2018 if (what == 2)
2019 {
2020 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
2021 return FAIL;
2022 }
2023 else
2024 {
2025 if (fprintf(fd, "<NL>") < 0)
2026 return FAIL;
2027 }
2028 continue;
2029 }
2030
2031 // Some characters have to be escaped with CTRL-V to
2032 // prevent them from misinterpreted in DoOneCmd().
2033 // A space, Tab and '"' has to be escaped with a backslash to
2034 // prevent it to be misinterpreted in do_set().
2035 // A space has to be escaped with a CTRL-V when it's at the start of a
2036 // ":map" rhs.
2037 // A '<' has to be escaped with a CTRL-V to prevent it being
2038 // interpreted as the start of a special key name.
2039 // A space in the lhs of a :map needs a CTRL-V.
2040 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2041 {
2042 if (putc('\\', fd) < 0)
2043 return FAIL;
2044 }
2045 else if (c < ' ' || c > '~' || c == '|'
2046 || (what == 0 && c == ' ')
2047 || (what == 1 && str == strstart && c == ' ')
2048 || (what != 2 && c == '<'))
2049 {
2050 if (putc(Ctrl_V, fd) < 0)
2051 return FAIL;
2052 }
2053 if (putc(c, fd) < 0)
2054 return FAIL;
2055 }
2056 return OK;
2057}
2058
2059/*
2060 * Check all mappings for the presence of special key codes.
2061 * Used after ":set term=xxx".
2062 */
2063 void
2064check_map_keycodes(void)
2065{
2066 mapblock_T *mp;
2067 char_u *p;
2068 int i;
2069 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002070 int abbr;
2071 int hash;
2072 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002073 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002074
2075 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002076 // avoids giving error messages
2077 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002078 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002079
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002080 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002081 // mappings/abbreviations with bp == NULL
2082 for (bp = firstbuf; ; bp = bp->b_next)
2083 {
2084 // Do the loop twice: Once for mappings, once for abbreviations.
2085 // Then loop over all map hash lists.
2086 for (abbr = 0; abbr <= 1; ++abbr)
2087 for (hash = 0; hash < 256; ++hash)
2088 {
2089 if (abbr)
2090 {
2091 if (hash) // there is only one abbr list
2092 break;
2093 if (bp != NULL)
2094 mp = bp->b_first_abbr;
2095 else
2096 mp = first_abbr;
2097 }
2098 else
2099 {
2100 if (bp != NULL)
2101 mp = bp->b_maphash[hash];
2102 else
2103 mp = maphash[hash];
2104 }
2105 for ( ; mp != NULL; mp = mp->m_next)
2106 {
2107 for (i = 0; i <= 1; ++i) // do this twice
2108 {
2109 if (i == 0)
2110 p = mp->m_keys; // once for the "from" part
2111 else
2112 p = mp->m_str; // and once for the "to" part
2113 while (*p)
2114 {
2115 if (*p == K_SPECIAL)
2116 {
2117 ++p;
2118 if (*p < 128) // for "normal" tcap entries
2119 {
2120 buf[0] = p[0];
2121 buf[1] = p[1];
2122 buf[2] = NUL;
2123 (void)add_termcap_entry(buf, FALSE);
2124 }
2125 ++p;
2126 }
2127 ++p;
2128 }
2129 }
2130 }
2131 }
2132 if (bp == NULL)
2133 break;
2134 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002135 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002136 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002137}
2138
2139#if defined(FEAT_EVAL) || defined(PROTO)
2140/*
2141 * Check the string "keys" against the lhs of all mappings.
2142 * Return pointer to rhs of mapping (mapblock->m_str).
2143 * NULL when no mapping found.
2144 */
2145 char_u *
2146check_map(
2147 char_u *keys,
2148 int mode,
2149 int exact, // require exact match
2150 int ign_mod, // ignore preceding modifier
2151 int abbr, // do abbreviations
2152 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2153 int *local_ptr) // return: buffer-local mapping or NULL
2154{
2155 int hash;
2156 int len, minlen;
2157 mapblock_T *mp;
2158 char_u *s;
2159 int local;
2160
2161 validate_maphash();
2162
2163 len = (int)STRLEN(keys);
2164 for (local = 1; local >= 0; --local)
2165 // loop over all hash lists
2166 for (hash = 0; hash < 256; ++hash)
2167 {
2168 if (abbr)
2169 {
2170 if (hash > 0) // there is only one list.
2171 break;
2172 if (local)
2173 mp = curbuf->b_first_abbr;
2174 else
2175 mp = first_abbr;
2176 }
2177 else if (local)
2178 mp = curbuf->b_maphash[hash];
2179 else
2180 mp = maphash[hash];
2181 for ( ; mp != NULL; mp = mp->m_next)
2182 {
2183 // skip entries with wrong mode, wrong length and not matching
2184 // ones
2185 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2186 {
2187 if (len > mp->m_keylen)
2188 minlen = mp->m_keylen;
2189 else
2190 minlen = len;
2191 s = mp->m_keys;
2192 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2193 && s[2] != NUL)
2194 {
2195 s += 3;
2196 if (len > mp->m_keylen - 3)
2197 minlen = mp->m_keylen - 3;
2198 }
2199 if (STRNCMP(s, keys, minlen) == 0)
2200 {
2201 if (mp_ptr != NULL)
2202 *mp_ptr = mp;
2203 if (local_ptr != NULL)
2204 *local_ptr = local;
2205 return mp->m_str;
2206 }
2207 }
2208 }
2209 }
2210
2211 return NULL;
2212}
2213
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002214 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002215get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2216{
2217 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002218 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002219 char_u *which;
2220 char_u buf[NUMBUFLEN];
2221 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002222 char_u *alt_keys_buf = NULL;
2223 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002224 char_u *rhs;
2225 int mode;
2226 int abbr = FALSE;
2227 int get_dict = FALSE;
2228 mapblock_T *mp;
Bram Moolenaara55ba062020-05-27 21:29:04 +02002229 mapblock_T *mp_simplified = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002230 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002231 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002232
2233 // return empty string for failure
2234 rettv->v_type = VAR_STRING;
2235 rettv->vval.v_string = NULL;
2236
2237 keys = tv_get_string(&argvars[0]);
2238 if (*keys == NUL)
2239 return;
2240
2241 if (argvars[1].v_type != VAR_UNKNOWN)
2242 {
2243 which = tv_get_string_buf_chk(&argvars[1], buf);
2244 if (argvars[2].v_type != VAR_UNKNOWN)
2245 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002246 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002247 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002248 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002249 }
2250 }
2251 else
2252 which = (char_u *)"";
2253 if (which == NULL)
2254 return;
2255
2256 mode = get_map_mode(&which, 0);
2257
Bram Moolenaar9c652532020-05-24 13:10:18 +02002258 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2259 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2260 &mp, &buffer_local);
2261 if (did_simplify)
2262 {
2263 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002264 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002265 // The "rhs" and "buffer_local" values are not expected to change.
2266 mp_simplified = mp;
2267 (void)replace_termcodes(keys, &alt_keys_buf,
2268 flags | REPTERM_NO_SIMPLIFY, NULL);
2269 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2270 &buffer_local);
2271 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002272
2273 if (!get_dict)
2274 {
2275 // Return a string.
2276 if (rhs != NULL)
2277 {
2278 if (*rhs == NUL)
2279 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2280 else
2281 rettv->vval.v_string = str2special_save(rhs, FALSE);
2282 }
2283
2284 }
2285 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
2286 {
2287 // Return a dictionary.
2288 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2289 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2290 dict_T *dict = rettv->vval.v_dict;
2291
2292 dict_add_string(dict, "lhs", lhs);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002293 vim_free(lhs);
2294 dict_add_string(dict, "lhsraw", mp->m_keys);
2295 if (did_simplify)
2296 // Also add the value for the simplified entry.
2297 dict_add_string(dict, "lhsrawalt", mp_simplified->m_keys);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002298 dict_add_string(dict, "rhs", mp->m_orig_str);
2299 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +02002300 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2301 ? 1L : 0L);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002302 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2303 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2304 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
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
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002310 vim_free(mapmode);
2311 }
Bram Moolenaar9c652532020-05-24 13:10:18 +02002312
2313 vim_free(keys_buf);
2314 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002315}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002316
2317/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002318 * "maparg()" function
2319 */
2320 void
2321f_maparg(typval_T *argvars, typval_T *rettv)
2322{
2323 if (in_vim9script()
2324 && (check_for_string_arg(argvars, 0) == FAIL
2325 || check_for_opt_string_arg(argvars, 1) == FAIL
2326 || (argvars[1].v_type != VAR_UNKNOWN
2327 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2328 || (argvars[2].v_type != VAR_UNKNOWN
2329 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2330 return;
2331
2332 get_maparg(argvars, rettv, TRUE);
2333}
2334
2335/*
2336 * "mapcheck()" function
2337 */
2338 void
2339f_mapcheck(typval_T *argvars, typval_T *rettv)
2340{
2341 if (in_vim9script()
2342 && (check_for_string_arg(argvars, 0) == FAIL
2343 || check_for_opt_string_arg(argvars, 1) == FAIL
2344 || (argvars[1].v_type != VAR_UNKNOWN
2345 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2346 return;
2347
2348 get_maparg(argvars, rettv, FALSE);
2349}
2350
2351/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002352 * "mapset()" function
2353 */
2354 void
2355f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2356{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002357 char_u *keys_buf = NULL;
2358 char_u *which;
2359 int mode;
2360 char_u buf[NUMBUFLEN];
2361 int is_abbr;
2362 dict_T *d;
2363 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002364 char_u *lhsraw;
2365 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002366 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002367 char_u *orig_rhs;
2368 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002369 int noremap;
2370 int expr;
2371 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002372 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002373 scid_T sid;
2374 linenr_T lnum;
2375 mapblock_T **map_table = maphash;
2376 mapblock_T **abbr_table = &first_abbr;
2377 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002378 char_u *arg;
2379
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002380 if (in_vim9script()
2381 && (check_for_string_arg(argvars, 0) == FAIL
2382 || check_for_bool_arg(argvars, 1) == FAIL
2383 || check_for_dict_arg(argvars, 2) == FAIL))
2384 return;
2385
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002386 which = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar1b912982020-09-29 21:45:41 +02002387 if (which == NULL)
2388 return;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002389 mode = get_map_mode(&which, 0);
Bram Moolenaar74273e62020-10-01 21:37:21 +02002390 is_abbr = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002391
2392 if (argvars[2].v_type != VAR_DICT)
2393 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002394 emsg(_(e_key_not_present_in_dictionary));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002395 return;
2396 }
2397 d = argvars[2].vval.v_dict;
2398
2399 // Get the values in the same order as above in get_maparg().
2400 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002401 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2402 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002403 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002404 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002405 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002406 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002407 return;
2408 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002409 orig_rhs = rhs;
2410 rhs = replace_termcodes(rhs, &arg_buf,
2411 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002412
2413 noremap = dict_get_number(d, (char_u *)"noremap") ? REMAP_NONE: 0;
2414 if (dict_get_number(d, (char_u *)"script") != 0)
2415 noremap = REMAP_SCRIPT;
2416 expr = dict_get_number(d, (char_u *)"expr") != 0;
2417 silent = dict_get_number(d, (char_u *)"silent") != 0;
2418 sid = dict_get_number(d, (char_u *)"sid");
2419 lnum = dict_get_number(d, (char_u *)"lnum");
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002420 buffer = dict_get_number(d, (char_u *)"buffer");
2421 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2422 // mode from the dict is not used
2423
2424 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002425 {
2426 map_table = curbuf->b_maphash;
2427 abbr_table = &curbuf->b_first_abbr;
2428 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002429
2430 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002431 if (buffer)
2432 {
2433 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2434 if (arg == NULL)
2435 return;
2436 STRCPY(arg, "<buffer>");
2437 STRCPY(arg + 8, lhs);
2438 }
2439 else
2440 {
2441 arg = vim_strsave(lhs);
2442 if (arg == NULL)
2443 return;
2444 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002445 do_map(1, arg, mode, is_abbr);
2446 vim_free(arg);
2447
Bram Moolenaar9c652532020-05-24 13:10:18 +02002448 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
2449 nowait, silent, mode, is_abbr, expr, sid, lnum, 0);
2450 if (lhsrawalt != NULL)
2451 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
2452 nowait, silent, mode, is_abbr, expr, sid, lnum, 1);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002453 vim_free(keys_buf);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002454 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002455}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002456#endif
2457
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002458
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002459#if defined(MSWIN) || defined(MACOS_X)
2460
2461# define VIS_SEL (VISUAL+SELECTMODE) // abbreviation
2462
2463/*
2464 * Default mappings for some often used keys.
2465 */
2466struct initmap
2467{
2468 char_u *arg;
2469 int mode;
2470};
2471
2472# ifdef FEAT_GUI_MSWIN
2473// Use the Windows (CUA) keybindings. (GUI)
2474static struct initmap initmappings[] =
2475{
2476 // paste, copy and cut
2477 {(char_u *)"<S-Insert> \"*P", NORMAL},
2478 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
2479 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
2480 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2481 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2482 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2483 {(char_u *)"<C-X> \"*d", VIS_SEL},
2484 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2485};
2486# endif
2487
2488# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2489// Use the Windows (CUA) keybindings. (Console)
2490static struct initmap cinitmappings[] =
2491{
2492 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
2493 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
2494 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
2495 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
2496
2497 // paste, copy and cut
2498# ifdef FEAT_CLIPBOARD
2499 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P
2500 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
2501 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O*
2502 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2503 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2504 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2505 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2506# else
2507 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P
2508 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
2509 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O"
2510 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2511 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2512 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2513# endif
2514};
2515# endif
2516
2517# if defined(MACOS_X)
2518static struct initmap initmappings[] =
2519{
2520 // Use the Standard MacOS binding.
2521 // paste, copy and cut
2522 {(char_u *)"<D-v> \"*P", NORMAL},
2523 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
2524 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
2525 {(char_u *)"<D-c> \"*y", VIS_SEL},
2526 {(char_u *)"<D-x> \"*d", VIS_SEL},
2527 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2528};
2529# endif
2530
2531# undef VIS_SEL
2532#endif
2533
2534/*
2535 * Set up default mappings.
2536 */
2537 void
2538init_mappings(void)
2539{
2540#if defined(MSWIN) || defined(MACOS_X)
2541 int i;
2542
2543# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2544# ifdef VIMDLL
2545 if (!gui.starting)
2546# endif
2547 {
K.Takataeeec2542021-06-02 13:28:16 +02002548 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002549 add_map(cinitmappings[i].arg, cinitmappings[i].mode);
2550 }
2551# endif
2552# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002553 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002554 add_map(initmappings[i].arg, initmappings[i].mode);
2555# endif
2556#endif
2557}
2558
2559#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2560 || defined(PROTO)
2561/*
2562 * Add a mapping "map" for mode "mode".
2563 * Need to put string in allocated memory, because do_map() will modify it.
2564 */
2565 void
2566add_map(char_u *map, int mode)
2567{
2568 char_u *s;
2569 char_u *cpo_save = p_cpo;
2570
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002571 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002572 s = vim_strsave(map);
2573 if (s != NULL)
2574 {
2575 (void)do_map(0, s, mode, FALSE);
2576 vim_free(s);
2577 }
2578 p_cpo = cpo_save;
2579}
2580#endif
2581
Bram Moolenaare677df82019-09-02 22:31:11 +02002582#if defined(FEAT_LANGMAP) || defined(PROTO)
2583/*
2584 * Any character has an equivalent 'langmap' character. This is used for
2585 * keyboards that have a special language mode that sends characters above
2586 * 128 (although other characters can be translated too). The "to" field is a
2587 * Vim command character. This avoids having to switch the keyboard back to
2588 * ASCII mode when leaving Insert mode.
2589 *
2590 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2591 * commands.
2592 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2593 * same as langmap_mapchar[] for characters >= 256.
2594 *
2595 * Use growarray for 'langmap' chars >= 256
2596 */
2597typedef struct
2598{
2599 int from;
2600 int to;
2601} langmap_entry_T;
2602
2603static garray_T langmap_mapga;
2604
2605/*
2606 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2607 * field. If not found insert a new entry at the appropriate location.
2608 */
2609 static void
2610langmap_set_entry(int from, int to)
2611{
2612 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2613 int a = 0;
2614 int b = langmap_mapga.ga_len;
2615
2616 // Do a binary search for an existing entry.
2617 while (a != b)
2618 {
2619 int i = (a + b) / 2;
2620 int d = entries[i].from - from;
2621
2622 if (d == 0)
2623 {
2624 entries[i].to = to;
2625 return;
2626 }
2627 if (d < 0)
2628 a = i + 1;
2629 else
2630 b = i;
2631 }
2632
2633 if (ga_grow(&langmap_mapga, 1) != OK)
2634 return; // out of memory
2635
2636 // insert new entry at position "a"
2637 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2638 mch_memmove(entries + 1, entries,
2639 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2640 ++langmap_mapga.ga_len;
2641 entries[0].from = from;
2642 entries[0].to = to;
2643}
2644
2645/*
2646 * Apply 'langmap' to multi-byte character "c" and return the result.
2647 */
2648 int
2649langmap_adjust_mb(int c)
2650{
2651 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2652 int a = 0;
2653 int b = langmap_mapga.ga_len;
2654
2655 while (a != b)
2656 {
2657 int i = (a + b) / 2;
2658 int d = entries[i].from - c;
2659
2660 if (d == 0)
2661 return entries[i].to; // found matching entry
2662 if (d < 0)
2663 a = i + 1;
2664 else
2665 b = i;
2666 }
2667 return c; // no entry found, return "c" unmodified
2668}
2669
2670 void
2671langmap_init(void)
2672{
2673 int i;
2674
2675 for (i = 0; i < 256; i++)
2676 langmap_mapchar[i] = i; // we init with a one-to-one map
2677 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2678}
2679
2680/*
2681 * Called when langmap option is set; the language map can be
2682 * changed at any time!
2683 */
2684 void
2685langmap_set(void)
2686{
2687 char_u *p;
2688 char_u *p2;
2689 int from, to;
2690
2691 ga_clear(&langmap_mapga); // clear the previous map first
2692 langmap_init(); // back to one-to-one map
2693
2694 for (p = p_langmap; p[0] != NUL; )
2695 {
2696 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2697 MB_PTR_ADV(p2))
2698 {
2699 if (p2[0] == '\\' && p2[1] != NUL)
2700 ++p2;
2701 }
2702 if (p2[0] == ';')
2703 ++p2; // abcd;ABCD form, p2 points to A
2704 else
2705 p2 = NULL; // aAbBcCdD form, p2 is NULL
2706 while (p[0])
2707 {
2708 if (p[0] == ',')
2709 {
2710 ++p;
2711 break;
2712 }
2713 if (p[0] == '\\' && p[1] != NUL)
2714 ++p;
2715 from = (*mb_ptr2char)(p);
2716 to = NUL;
2717 if (p2 == NULL)
2718 {
2719 MB_PTR_ADV(p);
2720 if (p[0] != ',')
2721 {
2722 if (p[0] == '\\')
2723 ++p;
2724 to = (*mb_ptr2char)(p);
2725 }
2726 }
2727 else
2728 {
2729 if (p2[0] != ',')
2730 {
2731 if (p2[0] == '\\')
2732 ++p2;
2733 to = (*mb_ptr2char)(p2);
2734 }
2735 }
2736 if (to == NUL)
2737 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002738 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002739 transchar(from));
2740 return;
2741 }
2742
2743 if (from >= 256)
2744 langmap_set_entry(from, to);
2745 else
2746 langmap_mapchar[from & 255] = to;
2747
2748 // Advance to next pair
2749 MB_PTR_ADV(p);
2750 if (p2 != NULL)
2751 {
2752 MB_PTR_ADV(p2);
2753 if (*p == ';')
2754 {
2755 p = p2;
2756 if (p[0] != NUL)
2757 {
2758 if (p[0] != ',')
2759 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002760 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02002761 return;
2762 }
2763 ++p;
2764 }
2765 break;
2766 }
2767 }
2768 }
2769 }
2770}
2771#endif
2772
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002773 static void
2774do_exmap(exarg_T *eap, int isabbrev)
2775{
2776 int mode;
2777 char_u *cmdp;
2778
2779 cmdp = eap->cmd;
2780 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
2781
2782 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
2783 eap->arg, mode, isabbrev))
2784 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002785 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002786 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002787 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
2788 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002789 break;
2790 }
2791}
2792
2793/*
2794 * ":abbreviate" and friends.
2795 */
2796 void
2797ex_abbreviate(exarg_T *eap)
2798{
2799 do_exmap(eap, TRUE); // almost the same as mapping
2800}
2801
2802/*
2803 * ":map" and friends.
2804 */
2805 void
2806ex_map(exarg_T *eap)
2807{
2808 // If we are sourcing .exrc or .vimrc in current directory we
2809 // print the mappings for security reasons.
2810 if (secure)
2811 {
2812 secure = 2;
2813 msg_outtrans(eap->cmd);
2814 msg_putchar('\n');
2815 }
2816 do_exmap(eap, FALSE);
2817}
2818
2819/*
2820 * ":unmap" and friends.
2821 */
2822 void
2823ex_unmap(exarg_T *eap)
2824{
2825 do_exmap(eap, FALSE);
2826}
2827
2828/*
2829 * ":mapclear" and friends.
2830 */
2831 void
2832ex_mapclear(exarg_T *eap)
2833{
2834 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
2835}
2836
2837/*
2838 * ":abclear" and friends.
2839 */
2840 void
2841ex_abclear(exarg_T *eap)
2842{
2843 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
2844}