blob: 6ad938d7ed6c9aa4bf80be541d7d4489d53f4ce3 [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{
229 mapblock_T *mp = ALLOC_ONE(mapblock_T);
230
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 {
1518 // Found a match:
1519 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1520 // This goes from end to start.
1521 //
1522 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1523 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1524 // Characters where IS_SPECIAL() == TRUE: key codes, need
1525 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1526 //
1527 // Character CTRL-] is treated specially - it completes the
1528 // abbreviation, but is not inserted into the input stream.
1529 j = 0;
1530 if (c != Ctrl_RSB)
1531 {
1532 // special key code, split up
1533 if (IS_SPECIAL(c) || c == K_SPECIAL)
1534 {
1535 tb[j++] = K_SPECIAL;
1536 tb[j++] = K_SECOND(c);
1537 tb[j++] = K_THIRD(c);
1538 }
1539 else
1540 {
1541 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1542 tb[j++] = Ctrl_V; // special char needs CTRL-V
1543 if (has_mbyte)
1544 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001545 int newlen;
1546 char_u *escaped;
1547
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001548 // if ABBR_OFF has been added, remove it here
1549 if (c >= ABBR_OFF)
1550 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001551 newlen = (*mb_char2bytes)(c, tb + j);
1552 tb[j + newlen] = NUL;
1553 // Need to escape K_SPECIAL.
1554 escaped = vim_strsave_escape_csi(tb + j);
1555 if (escaped != NULL)
1556 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001557 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001558 mch_memmove(tb + j, escaped, newlen);
1559 j += newlen;
1560 vim_free(escaped);
1561 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001562 }
1563 else
1564 tb[j++] = c;
1565 }
1566 tb[j] = NUL;
1567 // insert the last typed char
1568 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1569 }
1570#ifdef FEAT_EVAL
1571 if (mp->m_expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001572 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001573 else
1574#endif
1575 s = mp->m_str;
1576 if (s != NULL)
1577 {
1578 // insert the to string
1579 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
1580 // no abbrev. for these chars
1581 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1582#ifdef FEAT_EVAL
1583 if (mp->m_expr)
1584 vim_free(s);
1585#endif
1586 }
1587
1588 tb[0] = Ctrl_H;
1589 tb[1] = NUL;
1590 if (has_mbyte)
1591 len = clen; // Delete characters instead of bytes
1592 while (len-- > 0) // delete the from string
1593 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1594 return TRUE;
1595 }
1596 }
1597 return FALSE;
1598}
1599
1600#ifdef FEAT_EVAL
1601/*
1602 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1603 * special characters.
1604 */
1605 char_u *
1606eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001607 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001608 int c) // NUL or typed character for abbreviation
1609{
1610 char_u *res;
1611 char_u *p;
1612 char_u *expr;
1613 pos_T save_cursor;
1614 int save_msg_col;
1615 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001616 scid_T save_sctx_sid = current_sctx.sc_sid;
1617 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001618
1619 // Remove escaping of CSI, because "str" is in a format to be used as
1620 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001621 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001622 if (expr == NULL)
1623 return NULL;
1624 vim_unescape_csi(expr);
1625
1626 // Forbid changing text or using ":normal" to avoid most of the bad side
1627 // effects. Also restore the cursor position.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001628 ++textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001629 ++ex_normal_lock;
1630 set_vim_var_char(c); // set v:char to the typed character
1631 save_cursor = curwin->w_cursor;
1632 save_msg_col = msg_col;
1633 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001634 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1635 {
1636 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1637 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1638 }
1639
1640 // Note: the evaluation may make "mp" invalid.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001641 p = eval_to_string(expr, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001642
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001643 --textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001644 --ex_normal_lock;
1645 curwin->w_cursor = save_cursor;
1646 msg_col = save_msg_col;
1647 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001648 current_sctx.sc_sid = save_sctx_sid;
1649 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001650
1651 vim_free(expr);
1652
1653 if (p == NULL)
1654 return NULL;
1655 // Escape CSI in the result to be able to use the string as typeahead.
1656 res = vim_strsave_escape_csi(p);
1657 vim_free(p);
1658
1659 return res;
1660}
1661#endif
1662
1663/*
1664 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1665 * can be put in the typeahead buffer.
1666 * Returns NULL when out of memory.
1667 */
1668 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001669vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001670{
1671 char_u *res;
1672 char_u *s, *d;
1673
1674 // Need a buffer to hold up to three times as much. Four in case of an
1675 // illegal utf-8 byte:
1676 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1677 res = alloc(STRLEN(p) * 4 + 1);
1678 if (res != NULL)
1679 {
1680 d = res;
1681 for (s = p; *s != NUL; )
1682 {
1683 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1684 {
1685 // Copy special key unmodified.
1686 *d++ = *s++;
1687 *d++ = *s++;
1688 *d++ = *s++;
1689 }
1690 else
1691 {
1692 // Add character, possibly multi-byte to destination, escaping
1693 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1694 d = add_char2buf(PTR2CHAR(s), d);
1695 s += MB_CPTR2LEN(s);
1696 }
1697 }
1698 *d = NUL;
1699 }
1700 return res;
1701}
1702
1703/*
1704 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1705 * vim_strsave_escape_csi(). Works in-place.
1706 */
1707 void
1708vim_unescape_csi(char_u *p)
1709{
1710 char_u *s = p, *d = p;
1711
1712 while (*s != NUL)
1713 {
1714 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1715 {
1716 *d++ = K_SPECIAL;
1717 s += 3;
1718 }
1719 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1720 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1721 {
1722 *d++ = CSI;
1723 s += 3;
1724 }
1725 else
1726 *d++ = *s++;
1727 }
1728 *d = NUL;
1729}
1730
1731/*
1732 * Write map commands for the current mappings to an .exrc file.
1733 * Return FAIL on error, OK otherwise.
1734 */
1735 int
1736makemap(
1737 FILE *fd,
1738 buf_T *buf) // buffer for local mappings or NULL
1739{
1740 mapblock_T *mp;
1741 char_u c1, c2, c3;
1742 char_u *p;
1743 char *cmd;
1744 int abbr;
1745 int hash;
1746 int did_cpo = FALSE;
1747 int i;
1748
1749 validate_maphash();
1750
1751 // Do the loop twice: Once for mappings, once for abbreviations.
1752 // Then loop over all map hash lists.
1753 for (abbr = 0; abbr < 2; ++abbr)
1754 for (hash = 0; hash < 256; ++hash)
1755 {
1756 if (abbr)
1757 {
1758 if (hash > 0) // there is only one abbr list
1759 break;
1760 if (buf != NULL)
1761 mp = buf->b_first_abbr;
1762 else
1763 mp = first_abbr;
1764 }
1765 else
1766 {
1767 if (buf != NULL)
1768 mp = buf->b_maphash[hash];
1769 else
1770 mp = maphash[hash];
1771 }
1772
1773 for ( ; mp; mp = mp->m_next)
1774 {
1775 // skip script-local mappings
1776 if (mp->m_noremap == REMAP_SCRIPT)
1777 continue;
1778
1779 // skip mappings that contain a <SNR> (script-local thing),
1780 // they probably don't work when loaded again
1781 for (p = mp->m_str; *p != NUL; ++p)
1782 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1783 && p[2] == (int)KE_SNR)
1784 break;
1785 if (*p != NUL)
1786 continue;
1787
1788 // It's possible to create a mapping and then ":unmap" certain
1789 // modes. We recreate this here by mapping the individual
1790 // modes, which requires up to three of them.
1791 c1 = NUL;
1792 c2 = NUL;
1793 c3 = NUL;
1794 if (abbr)
1795 cmd = "abbr";
1796 else
1797 cmd = "map";
1798 switch (mp->m_mode)
1799 {
1800 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
1801 break;
1802 case NORMAL:
1803 c1 = 'n';
1804 break;
1805 case VISUAL:
1806 c1 = 'x';
1807 break;
1808 case SELECTMODE:
1809 c1 = 's';
1810 break;
1811 case OP_PENDING:
1812 c1 = 'o';
1813 break;
1814 case NORMAL + VISUAL:
1815 c1 = 'n';
1816 c2 = 'x';
1817 break;
1818 case NORMAL + SELECTMODE:
1819 c1 = 'n';
1820 c2 = 's';
1821 break;
1822 case NORMAL + OP_PENDING:
1823 c1 = 'n';
1824 c2 = 'o';
1825 break;
1826 case VISUAL + SELECTMODE:
1827 c1 = 'v';
1828 break;
1829 case VISUAL + OP_PENDING:
1830 c1 = 'x';
1831 c2 = 'o';
1832 break;
1833 case SELECTMODE + OP_PENDING:
1834 c1 = 's';
1835 c2 = 'o';
1836 break;
1837 case NORMAL + VISUAL + SELECTMODE:
1838 c1 = 'n';
1839 c2 = 'v';
1840 break;
1841 case NORMAL + VISUAL + OP_PENDING:
1842 c1 = 'n';
1843 c2 = 'x';
1844 c3 = 'o';
1845 break;
1846 case NORMAL + SELECTMODE + OP_PENDING:
1847 c1 = 'n';
1848 c2 = 's';
1849 c3 = 'o';
1850 break;
1851 case VISUAL + SELECTMODE + OP_PENDING:
1852 c1 = 'v';
1853 c2 = 'o';
1854 break;
1855 case CMDLINE + INSERT:
1856 if (!abbr)
1857 cmd = "map!";
1858 break;
1859 case CMDLINE:
1860 c1 = 'c';
1861 break;
1862 case INSERT:
1863 c1 = 'i';
1864 break;
1865 case LANGMAP:
1866 c1 = 'l';
1867 break;
1868 case TERMINAL:
1869 c1 = 't';
1870 break;
1871 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001872 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001873 return FAIL;
1874 }
1875 do // do this twice if c2 is set, 3 times with c3
1876 {
1877 // When outputting <> form, need to make sure that 'cpo'
1878 // is set to the Vim default.
1879 if (!did_cpo)
1880 {
1881 if (*mp->m_str == NUL) // will use <Nop>
1882 did_cpo = TRUE;
1883 else
1884 for (i = 0; i < 2; ++i)
1885 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1886 if (*p == K_SPECIAL || *p == NL)
1887 did_cpo = TRUE;
1888 if (did_cpo)
1889 {
1890 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1891 || put_eol(fd) < 0
1892 || fprintf(fd, "set cpo&vim") < 0
1893 || put_eol(fd) < 0)
1894 return FAIL;
1895 }
1896 }
1897 if (c1 && putc(c1, fd) < 0)
1898 return FAIL;
1899 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1900 return FAIL;
1901 if (fputs(cmd, fd) < 0)
1902 return FAIL;
1903 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1904 return FAIL;
1905 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1906 return FAIL;
1907 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1908 return FAIL;
1909#ifdef FEAT_EVAL
1910 if (mp->m_noremap == REMAP_SCRIPT
1911 && fputs("<script>", fd) < 0)
1912 return FAIL;
1913 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1914 return FAIL;
1915#endif
1916
1917 if ( putc(' ', fd) < 0
1918 || put_escstr(fd, mp->m_keys, 0) == FAIL
1919 || putc(' ', fd) < 0
1920 || put_escstr(fd, mp->m_str, 1) == FAIL
1921 || put_eol(fd) < 0)
1922 return FAIL;
1923 c1 = c2;
1924 c2 = c3;
1925 c3 = NUL;
1926 } while (c1 != NUL);
1927 }
1928 }
1929
1930 if (did_cpo)
1931 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
1932 || put_eol(fd) < 0
1933 || fprintf(fd, "unlet s:cpo_save") < 0
1934 || put_eol(fd) < 0)
1935 return FAIL;
1936 return OK;
1937}
1938
1939/*
1940 * write escape string to file
1941 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
1942 *
1943 * return FAIL for failure, OK otherwise
1944 */
1945 int
1946put_escstr(FILE *fd, char_u *strstart, int what)
1947{
1948 char_u *str = strstart;
1949 int c;
1950 int modifiers;
1951
1952 // :map xx <Nop>
1953 if (*str == NUL && what == 1)
1954 {
1955 if (fprintf(fd, "<Nop>") < 0)
1956 return FAIL;
1957 return OK;
1958 }
1959
1960 for ( ; *str != NUL; ++str)
1961 {
1962 char_u *p;
1963
1964 // Check for a multi-byte character, which may contain escaped
1965 // K_SPECIAL and CSI bytes
1966 p = mb_unescape(&str);
1967 if (p != NULL)
1968 {
1969 while (*p != NUL)
1970 if (fputc(*p++, fd) < 0)
1971 return FAIL;
1972 --str;
1973 continue;
1974 }
1975
1976 c = *str;
1977 // Special key codes have to be translated to be able to make sense
1978 // when they are read back.
1979 if (c == K_SPECIAL && what != 2)
1980 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02001981 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001982 if (str[1] == KS_MODIFIER)
1983 {
1984 modifiers = str[2];
1985 str += 3;
1986 c = *str;
1987 }
1988 if (c == K_SPECIAL)
1989 {
1990 c = TO_SPECIAL(str[1], str[2]);
1991 str += 2;
1992 }
1993 if (IS_SPECIAL(c) || modifiers) // special key
1994 {
1995 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
1996 return FAIL;
1997 continue;
1998 }
1999 }
2000
2001 // A '\n' in a map command should be written as <NL>.
2002 // A '\n' in a set command should be written as \^V^J.
2003 if (c == NL)
2004 {
2005 if (what == 2)
2006 {
2007 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
2008 return FAIL;
2009 }
2010 else
2011 {
2012 if (fprintf(fd, "<NL>") < 0)
2013 return FAIL;
2014 }
2015 continue;
2016 }
2017
2018 // Some characters have to be escaped with CTRL-V to
2019 // prevent them from misinterpreted in DoOneCmd().
2020 // A space, Tab and '"' has to be escaped with a backslash to
2021 // prevent it to be misinterpreted in do_set().
2022 // A space has to be escaped with a CTRL-V when it's at the start of a
2023 // ":map" rhs.
2024 // A '<' has to be escaped with a CTRL-V to prevent it being
2025 // interpreted as the start of a special key name.
2026 // A space in the lhs of a :map needs a CTRL-V.
2027 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2028 {
2029 if (putc('\\', fd) < 0)
2030 return FAIL;
2031 }
2032 else if (c < ' ' || c > '~' || c == '|'
2033 || (what == 0 && c == ' ')
2034 || (what == 1 && str == strstart && c == ' ')
2035 || (what != 2 && c == '<'))
2036 {
2037 if (putc(Ctrl_V, fd) < 0)
2038 return FAIL;
2039 }
2040 if (putc(c, fd) < 0)
2041 return FAIL;
2042 }
2043 return OK;
2044}
2045
2046/*
2047 * Check all mappings for the presence of special key codes.
2048 * Used after ":set term=xxx".
2049 */
2050 void
2051check_map_keycodes(void)
2052{
2053 mapblock_T *mp;
2054 char_u *p;
2055 int i;
2056 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002057 int abbr;
2058 int hash;
2059 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002060 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002061
2062 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002063 // avoids giving error messages
2064 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002065 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002066
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002067 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002068 // mappings/abbreviations with bp == NULL
2069 for (bp = firstbuf; ; bp = bp->b_next)
2070 {
2071 // Do the loop twice: Once for mappings, once for abbreviations.
2072 // Then loop over all map hash lists.
2073 for (abbr = 0; abbr <= 1; ++abbr)
2074 for (hash = 0; hash < 256; ++hash)
2075 {
2076 if (abbr)
2077 {
2078 if (hash) // there is only one abbr list
2079 break;
2080 if (bp != NULL)
2081 mp = bp->b_first_abbr;
2082 else
2083 mp = first_abbr;
2084 }
2085 else
2086 {
2087 if (bp != NULL)
2088 mp = bp->b_maphash[hash];
2089 else
2090 mp = maphash[hash];
2091 }
2092 for ( ; mp != NULL; mp = mp->m_next)
2093 {
2094 for (i = 0; i <= 1; ++i) // do this twice
2095 {
2096 if (i == 0)
2097 p = mp->m_keys; // once for the "from" part
2098 else
2099 p = mp->m_str; // and once for the "to" part
2100 while (*p)
2101 {
2102 if (*p == K_SPECIAL)
2103 {
2104 ++p;
2105 if (*p < 128) // for "normal" tcap entries
2106 {
2107 buf[0] = p[0];
2108 buf[1] = p[1];
2109 buf[2] = NUL;
2110 (void)add_termcap_entry(buf, FALSE);
2111 }
2112 ++p;
2113 }
2114 ++p;
2115 }
2116 }
2117 }
2118 }
2119 if (bp == NULL)
2120 break;
2121 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002122 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002123 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002124}
2125
2126#if defined(FEAT_EVAL) || defined(PROTO)
2127/*
2128 * Check the string "keys" against the lhs of all mappings.
2129 * Return pointer to rhs of mapping (mapblock->m_str).
2130 * NULL when no mapping found.
2131 */
2132 char_u *
2133check_map(
2134 char_u *keys,
2135 int mode,
2136 int exact, // require exact match
2137 int ign_mod, // ignore preceding modifier
2138 int abbr, // do abbreviations
2139 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2140 int *local_ptr) // return: buffer-local mapping or NULL
2141{
2142 int hash;
2143 int len, minlen;
2144 mapblock_T *mp;
2145 char_u *s;
2146 int local;
2147
2148 validate_maphash();
2149
2150 len = (int)STRLEN(keys);
2151 for (local = 1; local >= 0; --local)
2152 // loop over all hash lists
2153 for (hash = 0; hash < 256; ++hash)
2154 {
2155 if (abbr)
2156 {
2157 if (hash > 0) // there is only one list.
2158 break;
2159 if (local)
2160 mp = curbuf->b_first_abbr;
2161 else
2162 mp = first_abbr;
2163 }
2164 else if (local)
2165 mp = curbuf->b_maphash[hash];
2166 else
2167 mp = maphash[hash];
2168 for ( ; mp != NULL; mp = mp->m_next)
2169 {
2170 // skip entries with wrong mode, wrong length and not matching
2171 // ones
2172 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2173 {
2174 if (len > mp->m_keylen)
2175 minlen = mp->m_keylen;
2176 else
2177 minlen = len;
2178 s = mp->m_keys;
2179 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2180 && s[2] != NUL)
2181 {
2182 s += 3;
2183 if (len > mp->m_keylen - 3)
2184 minlen = mp->m_keylen - 3;
2185 }
2186 if (STRNCMP(s, keys, minlen) == 0)
2187 {
2188 if (mp_ptr != NULL)
2189 *mp_ptr = mp;
2190 if (local_ptr != NULL)
2191 *local_ptr = local;
2192 return mp->m_str;
2193 }
2194 }
2195 }
2196 }
2197
2198 return NULL;
2199}
2200
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002201 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002202get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2203{
2204 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002205 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002206 char_u *which;
2207 char_u buf[NUMBUFLEN];
2208 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002209 char_u *alt_keys_buf = NULL;
2210 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002211 char_u *rhs;
2212 int mode;
2213 int abbr = FALSE;
2214 int get_dict = FALSE;
2215 mapblock_T *mp;
Bram Moolenaara55ba062020-05-27 21:29:04 +02002216 mapblock_T *mp_simplified = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002217 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002218 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002219
2220 // return empty string for failure
2221 rettv->v_type = VAR_STRING;
2222 rettv->vval.v_string = NULL;
2223
2224 keys = tv_get_string(&argvars[0]);
2225 if (*keys == NUL)
2226 return;
2227
2228 if (argvars[1].v_type != VAR_UNKNOWN)
2229 {
2230 which = tv_get_string_buf_chk(&argvars[1], buf);
2231 if (argvars[2].v_type != VAR_UNKNOWN)
2232 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002233 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002234 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002235 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002236 }
2237 }
2238 else
2239 which = (char_u *)"";
2240 if (which == NULL)
2241 return;
2242
2243 mode = get_map_mode(&which, 0);
2244
Bram Moolenaar9c652532020-05-24 13:10:18 +02002245 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2246 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2247 &mp, &buffer_local);
2248 if (did_simplify)
2249 {
2250 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002251 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002252 // The "rhs" and "buffer_local" values are not expected to change.
2253 mp_simplified = mp;
2254 (void)replace_termcodes(keys, &alt_keys_buf,
2255 flags | REPTERM_NO_SIMPLIFY, NULL);
2256 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2257 &buffer_local);
2258 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002259
2260 if (!get_dict)
2261 {
2262 // Return a string.
2263 if (rhs != NULL)
2264 {
2265 if (*rhs == NUL)
2266 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2267 else
2268 rettv->vval.v_string = str2special_save(rhs, FALSE);
2269 }
2270
2271 }
2272 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
2273 {
2274 // Return a dictionary.
2275 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2276 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2277 dict_T *dict = rettv->vval.v_dict;
2278
2279 dict_add_string(dict, "lhs", lhs);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002280 vim_free(lhs);
2281 dict_add_string(dict, "lhsraw", mp->m_keys);
2282 if (did_simplify)
2283 // Also add the value for the simplified entry.
2284 dict_add_string(dict, "lhsrawalt", mp_simplified->m_keys);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002285 dict_add_string(dict, "rhs", mp->m_orig_str);
2286 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +02002287 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2288 ? 1L : 0L);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002289 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2290 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2291 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2292 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2293 dict_add_number(dict, "buffer", (long)buffer_local);
2294 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2295 dict_add_string(dict, "mode", mapmode);
2296
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002297 vim_free(mapmode);
2298 }
Bram Moolenaar9c652532020-05-24 13:10:18 +02002299
2300 vim_free(keys_buf);
2301 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002302}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002303
2304/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002305 * "maparg()" function
2306 */
2307 void
2308f_maparg(typval_T *argvars, typval_T *rettv)
2309{
2310 if (in_vim9script()
2311 && (check_for_string_arg(argvars, 0) == FAIL
2312 || check_for_opt_string_arg(argvars, 1) == FAIL
2313 || (argvars[1].v_type != VAR_UNKNOWN
2314 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2315 || (argvars[2].v_type != VAR_UNKNOWN
2316 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2317 return;
2318
2319 get_maparg(argvars, rettv, TRUE);
2320}
2321
2322/*
2323 * "mapcheck()" function
2324 */
2325 void
2326f_mapcheck(typval_T *argvars, typval_T *rettv)
2327{
2328 if (in_vim9script()
2329 && (check_for_string_arg(argvars, 0) == FAIL
2330 || check_for_opt_string_arg(argvars, 1) == FAIL
2331 || (argvars[1].v_type != VAR_UNKNOWN
2332 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2333 return;
2334
2335 get_maparg(argvars, rettv, FALSE);
2336}
2337
2338/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002339 * "mapset()" function
2340 */
2341 void
2342f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2343{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002344 char_u *keys_buf = NULL;
2345 char_u *which;
2346 int mode;
2347 char_u buf[NUMBUFLEN];
2348 int is_abbr;
2349 dict_T *d;
2350 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002351 char_u *lhsraw;
2352 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002353 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002354 char_u *orig_rhs;
2355 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002356 int noremap;
2357 int expr;
2358 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002359 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002360 scid_T sid;
2361 linenr_T lnum;
2362 mapblock_T **map_table = maphash;
2363 mapblock_T **abbr_table = &first_abbr;
2364 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002365 char_u *arg;
2366
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002367 if (in_vim9script()
2368 && (check_for_string_arg(argvars, 0) == FAIL
2369 || check_for_bool_arg(argvars, 1) == FAIL
2370 || check_for_dict_arg(argvars, 2) == FAIL))
2371 return;
2372
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002373 which = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar1b912982020-09-29 21:45:41 +02002374 if (which == NULL)
2375 return;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002376 mode = get_map_mode(&which, 0);
Bram Moolenaar74273e62020-10-01 21:37:21 +02002377 is_abbr = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002378
2379 if (argvars[2].v_type != VAR_DICT)
2380 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002381 emsg(_(e_key_not_present_in_dictionary));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002382 return;
2383 }
2384 d = argvars[2].vval.v_dict;
2385
2386 // Get the values in the same order as above in get_maparg().
2387 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002388 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2389 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002390 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002391 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002392 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002393 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002394 return;
2395 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002396 orig_rhs = rhs;
2397 rhs = replace_termcodes(rhs, &arg_buf,
2398 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002399
2400 noremap = dict_get_number(d, (char_u *)"noremap") ? REMAP_NONE: 0;
2401 if (dict_get_number(d, (char_u *)"script") != 0)
2402 noremap = REMAP_SCRIPT;
2403 expr = dict_get_number(d, (char_u *)"expr") != 0;
2404 silent = dict_get_number(d, (char_u *)"silent") != 0;
2405 sid = dict_get_number(d, (char_u *)"sid");
2406 lnum = dict_get_number(d, (char_u *)"lnum");
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002407 buffer = dict_get_number(d, (char_u *)"buffer");
2408 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2409 // mode from the dict is not used
2410
2411 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002412 {
2413 map_table = curbuf->b_maphash;
2414 abbr_table = &curbuf->b_first_abbr;
2415 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002416
2417 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002418 if (buffer)
2419 {
2420 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2421 if (arg == NULL)
2422 return;
2423 STRCPY(arg, "<buffer>");
2424 STRCPY(arg + 8, lhs);
2425 }
2426 else
2427 {
2428 arg = vim_strsave(lhs);
2429 if (arg == NULL)
2430 return;
2431 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002432 do_map(1, arg, mode, is_abbr);
2433 vim_free(arg);
2434
Bram Moolenaar9c652532020-05-24 13:10:18 +02002435 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
2436 nowait, silent, mode, is_abbr, expr, sid, lnum, 0);
2437 if (lhsrawalt != NULL)
2438 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
2439 nowait, silent, mode, is_abbr, expr, sid, lnum, 1);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002440 vim_free(keys_buf);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002441 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002442}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002443#endif
2444
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002445
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002446#if defined(MSWIN) || defined(MACOS_X)
2447
2448# define VIS_SEL (VISUAL+SELECTMODE) // abbreviation
2449
2450/*
2451 * Default mappings for some often used keys.
2452 */
2453struct initmap
2454{
2455 char_u *arg;
2456 int mode;
2457};
2458
2459# ifdef FEAT_GUI_MSWIN
2460// Use the Windows (CUA) keybindings. (GUI)
2461static struct initmap initmappings[] =
2462{
2463 // paste, copy and cut
2464 {(char_u *)"<S-Insert> \"*P", NORMAL},
2465 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
2466 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
2467 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2468 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2469 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2470 {(char_u *)"<C-X> \"*d", VIS_SEL},
2471 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2472};
2473# endif
2474
2475# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2476// Use the Windows (CUA) keybindings. (Console)
2477static struct initmap cinitmappings[] =
2478{
2479 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
2480 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
2481 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
2482 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
2483
2484 // paste, copy and cut
2485# ifdef FEAT_CLIPBOARD
2486 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P
2487 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
2488 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O*
2489 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2490 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2491 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2492 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2493# else
2494 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P
2495 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
2496 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O"
2497 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2498 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2499 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2500# endif
2501};
2502# endif
2503
2504# if defined(MACOS_X)
2505static struct initmap initmappings[] =
2506{
2507 // Use the Standard MacOS binding.
2508 // paste, copy and cut
2509 {(char_u *)"<D-v> \"*P", NORMAL},
2510 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
2511 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
2512 {(char_u *)"<D-c> \"*y", VIS_SEL},
2513 {(char_u *)"<D-x> \"*d", VIS_SEL},
2514 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2515};
2516# endif
2517
2518# undef VIS_SEL
2519#endif
2520
2521/*
2522 * Set up default mappings.
2523 */
2524 void
2525init_mappings(void)
2526{
2527#if defined(MSWIN) || defined(MACOS_X)
2528 int i;
2529
2530# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2531# ifdef VIMDLL
2532 if (!gui.starting)
2533# endif
2534 {
K.Takataeeec2542021-06-02 13:28:16 +02002535 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002536 add_map(cinitmappings[i].arg, cinitmappings[i].mode);
2537 }
2538# endif
2539# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002540 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002541 add_map(initmappings[i].arg, initmappings[i].mode);
2542# endif
2543#endif
2544}
2545
2546#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2547 || defined(PROTO)
2548/*
2549 * Add a mapping "map" for mode "mode".
2550 * Need to put string in allocated memory, because do_map() will modify it.
2551 */
2552 void
2553add_map(char_u *map, int mode)
2554{
2555 char_u *s;
2556 char_u *cpo_save = p_cpo;
2557
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002558 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002559 s = vim_strsave(map);
2560 if (s != NULL)
2561 {
2562 (void)do_map(0, s, mode, FALSE);
2563 vim_free(s);
2564 }
2565 p_cpo = cpo_save;
2566}
2567#endif
2568
Bram Moolenaare677df82019-09-02 22:31:11 +02002569#if defined(FEAT_LANGMAP) || defined(PROTO)
2570/*
2571 * Any character has an equivalent 'langmap' character. This is used for
2572 * keyboards that have a special language mode that sends characters above
2573 * 128 (although other characters can be translated too). The "to" field is a
2574 * Vim command character. This avoids having to switch the keyboard back to
2575 * ASCII mode when leaving Insert mode.
2576 *
2577 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2578 * commands.
2579 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2580 * same as langmap_mapchar[] for characters >= 256.
2581 *
2582 * Use growarray for 'langmap' chars >= 256
2583 */
2584typedef struct
2585{
2586 int from;
2587 int to;
2588} langmap_entry_T;
2589
2590static garray_T langmap_mapga;
2591
2592/*
2593 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2594 * field. If not found insert a new entry at the appropriate location.
2595 */
2596 static void
2597langmap_set_entry(int from, int to)
2598{
2599 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2600 int a = 0;
2601 int b = langmap_mapga.ga_len;
2602
2603 // Do a binary search for an existing entry.
2604 while (a != b)
2605 {
2606 int i = (a + b) / 2;
2607 int d = entries[i].from - from;
2608
2609 if (d == 0)
2610 {
2611 entries[i].to = to;
2612 return;
2613 }
2614 if (d < 0)
2615 a = i + 1;
2616 else
2617 b = i;
2618 }
2619
2620 if (ga_grow(&langmap_mapga, 1) != OK)
2621 return; // out of memory
2622
2623 // insert new entry at position "a"
2624 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2625 mch_memmove(entries + 1, entries,
2626 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2627 ++langmap_mapga.ga_len;
2628 entries[0].from = from;
2629 entries[0].to = to;
2630}
2631
2632/*
2633 * Apply 'langmap' to multi-byte character "c" and return the result.
2634 */
2635 int
2636langmap_adjust_mb(int c)
2637{
2638 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2639 int a = 0;
2640 int b = langmap_mapga.ga_len;
2641
2642 while (a != b)
2643 {
2644 int i = (a + b) / 2;
2645 int d = entries[i].from - c;
2646
2647 if (d == 0)
2648 return entries[i].to; // found matching entry
2649 if (d < 0)
2650 a = i + 1;
2651 else
2652 b = i;
2653 }
2654 return c; // no entry found, return "c" unmodified
2655}
2656
2657 void
2658langmap_init(void)
2659{
2660 int i;
2661
2662 for (i = 0; i < 256; i++)
2663 langmap_mapchar[i] = i; // we init with a one-to-one map
2664 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2665}
2666
2667/*
2668 * Called when langmap option is set; the language map can be
2669 * changed at any time!
2670 */
2671 void
2672langmap_set(void)
2673{
2674 char_u *p;
2675 char_u *p2;
2676 int from, to;
2677
2678 ga_clear(&langmap_mapga); // clear the previous map first
2679 langmap_init(); // back to one-to-one map
2680
2681 for (p = p_langmap; p[0] != NUL; )
2682 {
2683 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2684 MB_PTR_ADV(p2))
2685 {
2686 if (p2[0] == '\\' && p2[1] != NUL)
2687 ++p2;
2688 }
2689 if (p2[0] == ';')
2690 ++p2; // abcd;ABCD form, p2 points to A
2691 else
2692 p2 = NULL; // aAbBcCdD form, p2 is NULL
2693 while (p[0])
2694 {
2695 if (p[0] == ',')
2696 {
2697 ++p;
2698 break;
2699 }
2700 if (p[0] == '\\' && p[1] != NUL)
2701 ++p;
2702 from = (*mb_ptr2char)(p);
2703 to = NUL;
2704 if (p2 == NULL)
2705 {
2706 MB_PTR_ADV(p);
2707 if (p[0] != ',')
2708 {
2709 if (p[0] == '\\')
2710 ++p;
2711 to = (*mb_ptr2char)(p);
2712 }
2713 }
2714 else
2715 {
2716 if (p2[0] != ',')
2717 {
2718 if (p2[0] == '\\')
2719 ++p2;
2720 to = (*mb_ptr2char)(p2);
2721 }
2722 }
2723 if (to == NUL)
2724 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002725 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002726 transchar(from));
2727 return;
2728 }
2729
2730 if (from >= 256)
2731 langmap_set_entry(from, to);
2732 else
2733 langmap_mapchar[from & 255] = to;
2734
2735 // Advance to next pair
2736 MB_PTR_ADV(p);
2737 if (p2 != NULL)
2738 {
2739 MB_PTR_ADV(p2);
2740 if (*p == ';')
2741 {
2742 p = p2;
2743 if (p[0] != NUL)
2744 {
2745 if (p[0] != ',')
2746 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002747 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02002748 return;
2749 }
2750 ++p;
2751 }
2752 break;
2753 }
2754 }
2755 }
2756 }
2757}
2758#endif
2759
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002760 static void
2761do_exmap(exarg_T *eap, int isabbrev)
2762{
2763 int mode;
2764 char_u *cmdp;
2765
2766 cmdp = eap->cmd;
2767 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
2768
2769 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
2770 eap->arg, mode, isabbrev))
2771 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002772 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002773 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002774 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
2775 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002776 break;
2777 }
2778}
2779
2780/*
2781 * ":abbreviate" and friends.
2782 */
2783 void
2784ex_abbreviate(exarg_T *eap)
2785{
2786 do_exmap(eap, TRUE); // almost the same as mapping
2787}
2788
2789/*
2790 * ":map" and friends.
2791 */
2792 void
2793ex_map(exarg_T *eap)
2794{
2795 // If we are sourcing .exrc or .vimrc in current directory we
2796 // print the mappings for security reasons.
2797 if (secure)
2798 {
2799 secure = 2;
2800 msg_outtrans(eap->cmd);
2801 msg_putchar('\n');
2802 }
2803 do_exmap(eap, FALSE);
2804}
2805
2806/*
2807 * ":unmap" and friends.
2808 */
2809 void
2810ex_unmap(exarg_T *eap)
2811{
2812 do_exmap(eap, FALSE);
2813}
2814
2815/*
2816 * ":mapclear" and friends.
2817 */
2818 void
2819ex_mapclear(exarg_T *eap)
2820{
2821 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
2822}
2823
2824/*
2825 * ":abclear" and friends.
2826 */
2827 void
2828ex_abclear(exarg_T *eap)
2829{
2830 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
2831}