blob: fbbf9dcaf126f22fe15bfa255623aef593795bd3 [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 */
Bram Moolenaar24959102022-05-07 20:01:16 +010034#define MAP_HASH(mode, c1) (((mode) & (MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING | MODE_TERMINAL)) ? (c1) : ((c1) ^ 0x80))
Bram Moolenaarb66bab32019-08-01 14:28:24 +020035
36/*
37 * Get the start of the hashed map list for "state" and first character "c".
38 */
39 mapblock_T *
40get_maphash_list(int state, int c)
41{
42 return maphash[MAP_HASH(state, c)];
43}
44
45/*
46 * Get the buffer-local hashed map list for "state" and first character "c".
47 */
48 mapblock_T *
49get_buf_maphash_list(int state, int c)
50{
51 return curbuf->b_maphash[MAP_HASH(state, c)];
52}
53
54 int
55is_maphash_valid(void)
56{
57 return maphash_valid;
58}
59
60/*
61 * Initialize maphash[] for first use.
62 */
63 static void
64validate_maphash(void)
65{
66 if (!maphash_valid)
67 {
Bram Moolenaara80faa82020-04-12 19:37:17 +020068 CLEAR_FIELD(maphash);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020069 maphash_valid = TRUE;
70 }
71}
72
73/*
74 * Delete one entry from the abbrlist or maphash[].
75 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
76 */
77 static void
78map_free(mapblock_T **mpp)
79{
80 mapblock_T *mp;
81
82 mp = *mpp;
83 vim_free(mp->m_keys);
84 vim_free(mp->m_str);
85 vim_free(mp->m_orig_str);
86 *mpp = mp->m_next;
Bram Moolenaard648c012022-01-16 14:58:34 +000087#ifdef FEAT_EVAL
Bram Moolenaarf61c89d2022-01-19 22:51:48 +000088 reset_last_used_map(mp);
Bram Moolenaard648c012022-01-16 14:58:34 +000089#endif
Bram Moolenaar8aa0e6c2022-01-20 11:27:58 +000090 vim_free(mp);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020091}
92
93/*
94 * Return characters to represent the map mode in an allocated string.
95 * Returns NULL when out of memory.
96 */
97 static char_u *
98map_mode_to_chars(int mode)
99{
100 garray_T mapmode;
101
102 ga_init2(&mapmode, 1, 7);
103
Bram Moolenaar24959102022-05-07 20:01:16 +0100104 if ((mode & (MODE_INSERT | MODE_CMDLINE)) == (MODE_INSERT | MODE_CMDLINE))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200105 ga_append(&mapmode, '!'); // :map!
Bram Moolenaar24959102022-05-07 20:01:16 +0100106 else if (mode & MODE_INSERT)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200107 ga_append(&mapmode, 'i'); // :imap
Bram Moolenaar24959102022-05-07 20:01:16 +0100108 else if (mode & MODE_LANGMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200109 ga_append(&mapmode, 'l'); // :lmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100110 else if (mode & MODE_CMDLINE)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200111 ga_append(&mapmode, 'c'); // :cmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100112 else if ((mode
113 & (MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING))
114 == (MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200115 ga_append(&mapmode, ' '); // :map
116 else
117 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100118 if (mode & MODE_NORMAL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200119 ga_append(&mapmode, 'n'); // :nmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100120 if (mode & MODE_OP_PENDING)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200121 ga_append(&mapmode, 'o'); // :omap
Bram Moolenaar24959102022-05-07 20:01:16 +0100122 if (mode & MODE_TERMINAL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200123 ga_append(&mapmode, 't'); // :tmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100124 if ((mode & (MODE_VISUAL | MODE_SELECT)) == (MODE_VISUAL | MODE_SELECT))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200125 ga_append(&mapmode, 'v'); // :vmap
126 else
127 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100128 if (mode & MODE_VISUAL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200129 ga_append(&mapmode, 'x'); // :xmap
Bram Moolenaar24959102022-05-07 20:01:16 +0100130 if (mode & MODE_SELECT)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200131 ga_append(&mapmode, 's'); // :smap
132 }
133 }
134
135 ga_append(&mapmode, NUL);
136 return (char_u *)mapmode.ga_data;
137}
138
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100139/*
140 * Output a line for one mapping.
141 */
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200142 static void
143showmap(
144 mapblock_T *mp,
145 int local) // TRUE for buffer-local map
146{
147 int len = 1;
148 char_u *mapchars;
149
150 if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
151 return;
152
153 if (msg_didout || msg_silent != 0)
154 {
155 msg_putchar('\n');
156 if (got_int) // 'q' typed at MORE prompt
157 return;
158 }
159
160 mapchars = map_mode_to_chars(mp->m_mode);
161 if (mapchars != NULL)
162 {
163 msg_puts((char *)mapchars);
164 len = (int)STRLEN(mapchars);
165 vim_free(mapchars);
166 }
167
168 while (++len <= 3)
169 msg_putchar(' ');
170
171 // Display the LHS. Get length of what we write.
172 len = msg_outtrans_special(mp->m_keys, TRUE, 0);
173 do
174 {
175 msg_putchar(' '); // padd with blanks
176 ++len;
177 } while (len < 12);
178
179 if (mp->m_noremap == REMAP_NONE)
180 msg_puts_attr("*", HL_ATTR(HLF_8));
181 else if (mp->m_noremap == REMAP_SCRIPT)
182 msg_puts_attr("&", HL_ATTR(HLF_8));
183 else
184 msg_putchar(' ');
185
186 if (local)
187 msg_putchar('@');
188 else
189 msg_putchar(' ');
190
191 // Use FALSE below if we only want things like <Up> to show up as such on
192 // the rhs, and not M-x etc, TRUE gets both -- webb
193 if (*mp->m_str == NUL)
194 msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
195 else
zeertzjqac402f42022-05-04 18:51:43 +0100196 msg_outtrans_special(mp->m_str, FALSE, 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200197#ifdef FEAT_EVAL
198 if (p_verbose > 0)
199 last_set_msg(mp->m_script_ctx);
200#endif
Bram Moolenaard288eaa2022-02-16 18:27:55 +0000201 msg_clr_eos();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200202 out_flush(); // show one line at a time
203}
204
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200205 static int
206map_add(
207 mapblock_T **map_table,
208 mapblock_T **abbr_table,
209 char_u *keys,
210 char_u *rhs,
211 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200212 int noremap,
213 int nowait,
214 int silent,
215 int mode,
216 int is_abbr,
217#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200218 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200219 scid_T sid, // -1 to use current_sctx
Bram Moolenaara9528b32022-01-18 20:51:35 +0000220 int scriptversion,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200221 linenr_T lnum,
222#endif
223 int simplified)
224{
Bram Moolenaar94075b22022-01-18 20:30:39 +0000225 mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200226
227 if (mp == NULL)
228 return FAIL;
229
230 // If CTRL-C has been mapped, don't always use it for Interrupting.
231 if (*keys == Ctrl_C)
232 {
233 if (map_table == curbuf->b_maphash)
234 curbuf->b_mapped_ctrl_c |= mode;
235 else
236 mapped_ctrl_c |= mode;
237 }
238
239 mp->m_keys = vim_strsave(keys);
240 mp->m_str = vim_strsave(rhs);
241 mp->m_orig_str = vim_strsave(orig_rhs);
242 if (mp->m_keys == NULL || mp->m_str == NULL)
243 {
244 vim_free(mp->m_keys);
245 vim_free(mp->m_str);
246 vim_free(mp->m_orig_str);
247 vim_free(mp);
248 return FAIL;
249 }
250 mp->m_keylen = (int)STRLEN(mp->m_keys);
251 mp->m_noremap = noremap;
252 mp->m_nowait = nowait;
253 mp->m_silent = silent;
254 mp->m_mode = mode;
255 mp->m_simplified = simplified;
256#ifdef FEAT_EVAL
257 mp->m_expr = expr;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000258 if (sid > 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200259 {
260 mp->m_script_ctx.sc_sid = sid;
261 mp->m_script_ctx.sc_lnum = lnum;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000262 mp->m_script_ctx.sc_version = scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200263 }
264 else
265 {
266 mp->m_script_ctx = current_sctx;
267 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
268 }
269#endif
270
271 // add the new entry in front of the abbrlist or maphash[] list
272 if (is_abbr)
273 {
274 mp->m_next = *abbr_table;
275 *abbr_table = mp;
276 }
277 else
278 {
279 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
280
281 mp->m_next = map_table[n];
282 map_table[n] = mp;
283 }
284 return OK;
285}
286
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200287/*
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100288 * List mappings. When "haskey" is FALSE all mappings, otherwise mappings that
289 * match "keys[keys_len]".
290 */
291 static void
292list_mappings(
293 int keyround,
294 int abbrev,
295 int haskey,
296 char_u *keys,
297 int keys_len,
298 int mode,
299 int *did_local)
300{
301 if (p_verbose > 0 && keyround == 1 && seenModifyOtherKeys)
302 msg_puts(_("Seen modifyOtherKeys: true"));
303
304 // need to loop over all global hash lists
305 for (int hash = 0; hash < 256 && !got_int; ++hash)
306 {
307 mapblock_T *mp;
308
309 if (abbrev)
310 {
311 if (hash != 0) // there is only one abbreviation list
312 break;
313 mp = curbuf->b_first_abbr;
314 }
315 else
316 mp = curbuf->b_maphash[hash];
317 for ( ; mp != NULL && !got_int; mp = mp->m_next)
318 {
319 // check entries with the same mode
320 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
321 {
322 if (!haskey) // show all entries
323 {
324 showmap(mp, TRUE);
325 *did_local = TRUE;
326 }
327 else
328 {
329 int n = mp->m_keylen;
330 if (STRNCMP(mp->m_keys, keys,
331 (size_t)(n < keys_len ? n : keys_len)) == 0)
332 {
333 showmap(mp, TRUE);
334 *did_local = TRUE;
335 }
336 }
337 }
338 }
339 }
340}
341
342/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200343 * map[!] : show all key mappings
344 * map[!] {lhs} : show key mapping for {lhs}
345 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
346 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
347 * unmap[!] {lhs} : remove key mapping for {lhs}
348 * abbr : show all abbreviations
349 * abbr {lhs} : show abbreviations for {lhs}
350 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
351 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
352 * unabbr {lhs} : remove abbreviation for {lhs}
353 *
zeertzjq44068e92022-06-16 11:14:55 +0100354 * maptype: MAPTYPE_MAP for :map
355 * MAPTYPE_UNMAP for :unmap
356 * MAPTYPE_NOREMAP for noremap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200357 *
358 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
359 * it will be modified.
360 *
Bram Moolenaar24959102022-05-07 20:01:16 +0100361 * for :map mode is MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING
362 * for :map! mode is MODE_INSERT | MODE_CMDLINE
363 * for :cmap mode is MODE_CMDLINE
364 * for :imap mode is MODE_INSERT
365 * for :lmap mode is MODE_LANGMAP
366 * for :nmap mode is MODE_NORMAL
367 * for :vmap mode is MODE_VISUAL | MODE_SELECT
368 * for :xmap mode is MODE_VISUAL
369 * for :smap mode is MODE_SELECT
370 * for :omap mode is MODE_OP_PENDING
371 * for :tmap mode is MODE_TERMINAL
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200372 *
Bram Moolenaar24959102022-05-07 20:01:16 +0100373 * for :abbr mode is MODE_INSERT | MODE_CMDLINE
374 * for :iabbr mode is MODE_INSERT
375 * for :cabbr mode is MODE_CMDLINE
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200376 *
377 * Return 0 for success
378 * 1 for invalid arguments
379 * 2 for no match
380 * 4 for out of mem
381 * 5 for entry not unique
382 */
383 int
384do_map(
385 int maptype,
386 char_u *arg,
387 int mode,
388 int abbrev) // not a mapping but an abbreviation
389{
390 char_u *keys;
391 mapblock_T *mp, **mpp;
392 char_u *rhs;
393 char_u *p;
394 int n;
395 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200396 int hasarg;
397 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200398 int do_print;
399 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200400 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200401 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200402 char_u *arg_buf = NULL;
403 int retval = 0;
404 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200405 mapblock_T **abbr_table;
406 mapblock_T **map_table;
407 int unique = FALSE;
408 int nowait = FALSE;
409 int silent = FALSE;
410 int special = FALSE;
411#ifdef FEAT_EVAL
412 int expr = FALSE;
413#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200414 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200415 int noremap;
416 char_u *orig_rhs;
417
418 keys = arg;
419 map_table = maphash;
420 abbr_table = &first_abbr;
421
422 // For ":noremap" don't remap, otherwise do remap.
zeertzjq44068e92022-06-16 11:14:55 +0100423 if (maptype == MAPTYPE_NOREMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200424 noremap = REMAP_NONE;
425 else
426 noremap = REMAP_YES;
427
428 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
429 // any order.
430 for (;;)
431 {
432 // Check for "<buffer>": mapping local to buffer.
433 if (STRNCMP(keys, "<buffer>", 8) == 0)
434 {
435 keys = skipwhite(keys + 8);
436 map_table = curbuf->b_maphash;
437 abbr_table = &curbuf->b_first_abbr;
438 continue;
439 }
440
441 // Check for "<nowait>": don't wait for more characters.
442 if (STRNCMP(keys, "<nowait>", 8) == 0)
443 {
444 keys = skipwhite(keys + 8);
445 nowait = TRUE;
446 continue;
447 }
448
449 // Check for "<silent>": don't echo commands.
450 if (STRNCMP(keys, "<silent>", 8) == 0)
451 {
452 keys = skipwhite(keys + 8);
453 silent = TRUE;
454 continue;
455 }
456
457 // Check for "<special>": accept special keys in <>
458 if (STRNCMP(keys, "<special>", 9) == 0)
459 {
460 keys = skipwhite(keys + 9);
461 special = TRUE;
462 continue;
463 }
464
465#ifdef FEAT_EVAL
466 // Check for "<script>": remap script-local mappings only
467 if (STRNCMP(keys, "<script>", 8) == 0)
468 {
469 keys = skipwhite(keys + 8);
470 noremap = REMAP_SCRIPT;
471 continue;
472 }
473
474 // Check for "<expr>": {rhs} is an expression.
475 if (STRNCMP(keys, "<expr>", 6) == 0)
476 {
477 keys = skipwhite(keys + 6);
478 expr = TRUE;
479 continue;
480 }
481#endif
482 // Check for "<unique>": don't overwrite an existing mapping.
483 if (STRNCMP(keys, "<unique>", 8) == 0)
484 {
485 keys = skipwhite(keys + 8);
486 unique = TRUE;
487 continue;
488 }
489 break;
490 }
491
492 validate_maphash();
493
494 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
495 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
496 // with :unmap white space is included in the keys, no argument possible.
497 p = keys;
498 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
zeertzjq44068e92022-06-16 11:14:55 +0100499 while (*p && (maptype == MAPTYPE_UNMAP || !VIM_ISWHITE(*p)))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200500 {
501 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
502 p[1] != NUL)
503 ++p; // skip CTRL-V or backslash
504 ++p;
505 }
506 if (*p != NUL)
507 *p++ = NUL;
508
509 p = skipwhite(p);
510 rhs = p;
511 hasarg = (*rhs != NUL);
512 haskey = (*keys != NUL);
zeertzjq44068e92022-06-16 11:14:55 +0100513 do_print = !haskey || (maptype != MAPTYPE_UNMAP && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200514
515 // check for :unmap without argument
zeertzjq44068e92022-06-16 11:14:55 +0100516 if (maptype == MAPTYPE_UNMAP && !haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200517 {
518 retval = 1;
519 goto theend;
520 }
521
522 // If mapping has been given as ^V<C_UP> say, then replace the term codes
523 // with the appropriate two bytes. If it is a shifted special key, unshift
524 // it too, giving another two bytes.
525 // replace_termcodes() may move the result to allocated memory, which
526 // needs to be freed later (*keys_buf and *arg_buf).
527 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200528 // If something like <C-H> is simplified to 0x08 then mark it as simplified
529 // and also add a n entry with a modifier, which will work when
530 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200531 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200532 {
533 char_u *new_keys;
534 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
535
536 if (special)
537 flags |= REPTERM_SPECIAL;
538 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
539 if (did_simplify)
540 (void)replace_termcodes(keys, &alt_keys_buf,
541 flags | REPTERM_NO_SIMPLIFY, NULL);
542 keys = new_keys;
543 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200544 orig_rhs = rhs;
545 if (hasarg)
546 {
547 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
548 rhs = (char_u *)"";
549 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200550 rhs = replace_termcodes(rhs, &arg_buf,
551 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200552 }
553
Bram Moolenaar459fd782019-10-13 16:43:39 +0200554 /*
555 * The following is done twice if we have two versions of keys:
556 * "alt_keys_buf" is not NULL.
557 */
558 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200559 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200560 int did_it = FALSE;
561 int did_local = FALSE;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100562 int keyround1_simplified = keyround == 1 && did_simplify;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200563 int round;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200564
565 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200566 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200567 if (alt_keys_buf == NULL)
568 break;
569 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200570 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200571 else if (alt_keys_buf != NULL && do_print)
572 // when printing always use the not-simplified map
573 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200574
Bram Moolenaar459fd782019-10-13 16:43:39 +0200575 // check arguments and translate function keys
576 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200577 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200578 len = (int)STRLEN(keys);
579 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200580 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200581 retval = 1;
582 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200583 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200584
zeertzjq44068e92022-06-16 11:14:55 +0100585 if (abbrev && maptype != MAPTYPE_UNMAP)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200586 {
587 // If an abbreviation ends in a keyword character, the
588 // rest must be all keyword-char or all non-keyword-char.
589 // Otherwise we won't be able to find the start of it in a
590 // vi-compatible way.
591 if (has_mbyte)
592 {
593 int first, last;
594 int same = -1;
595
596 first = vim_iswordp(keys);
597 last = first;
598 p = keys + (*mb_ptr2len)(keys);
599 n = 1;
600 while (p < keys + len)
601 {
602 ++n; // nr of (multi-byte) chars
603 last = vim_iswordp(p); // type of last char
604 if (same == -1 && last != first)
605 same = n - 1; // count of same char type
606 p += (*mb_ptr2len)(p);
607 }
608 if (last && n > 2 && same >= 0 && same < n - 1)
609 {
610 retval = 1;
611 goto theend;
612 }
613 }
614 else if (vim_iswordc(keys[len - 1]))
615 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200616 for (n = 0; n < len - 2; ++n)
617 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
618 {
619 retval = 1;
620 goto theend;
621 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200622 // An abbreviation cannot contain white space.
623 for (n = 0; n < len; ++n)
624 if (VIM_ISWHITE(keys[n]))
625 {
626 retval = 1;
627 goto theend;
628 }
629 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200630 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200631
Bram Moolenaar459fd782019-10-13 16:43:39 +0200632 if (haskey && hasarg && abbrev) // if we will add an abbreviation
633 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200634 // no abbreviations
635
Bram Moolenaar459fd782019-10-13 16:43:39 +0200636 if (do_print)
637 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200638
Bram Moolenaar459fd782019-10-13 16:43:39 +0200639 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200640 if (unique && map_table == curbuf->b_maphash
zeertzjq44068e92022-06-16 11:14:55 +0100641 && haskey && hasarg && maptype != MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200642 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200643 // need to loop over all global hash lists
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100644 for (int hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200645 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200646 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200647 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200648 if (hash != 0) // there is only one abbreviation list
649 break;
650 mp = first_abbr;
651 }
652 else
653 mp = maphash[hash];
654 for ( ; mp != NULL && !got_int; mp = mp->m_next)
655 {
656 // check entries with the same mode
657 if ((mp->m_mode & mode) != 0
658 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200659 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
660 {
661 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000662 semsg(
663 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200664 mp->m_keys);
665 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000666 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200667 mp->m_keys);
668 retval = 5;
669 goto theend;
670 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200671 }
672 }
673 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200674
Bram Moolenaar459fd782019-10-13 16:43:39 +0200675 // When listing global mappings, also list buffer-local ones here.
zeertzjq44068e92022-06-16 11:14:55 +0100676 if (map_table != curbuf->b_maphash && !hasarg
677 && maptype != MAPTYPE_UNMAP)
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100678 list_mappings(keyround, abbrev, haskey, keys, len,
679 mode, &did_local);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200680
Bram Moolenaar459fd782019-10-13 16:43:39 +0200681 // Find an entry in the maphash[] list that matches.
682 // For :unmap we may loop two times: once to try to unmap an entry with
683 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000684 // an entry with a matching 'to' part. This was done to allow
685 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
686 // be replaced by "bar" because of the abbreviation.
zeertzjq44068e92022-06-16 11:14:55 +0100687 for (round = 0; (round == 0 || maptype == MAPTYPE_UNMAP) && round <= 1
Bram Moolenaar459fd782019-10-13 16:43:39 +0200688 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200689 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200690 // need to loop over all hash lists
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100691 for (int hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200692 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200693 if (abbrev)
694 {
695 if (hash > 0) // there is only one abbreviation list
696 break;
697 mpp = abbr_table;
698 }
699 else
700 mpp = &(map_table[hash]);
701 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
702 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200703
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200704 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200705 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200706 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200707 mpp = &(mp->m_next);
708 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200709 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200710 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200711 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200712 if (!mp->m_simplified)
713 {
714 showmap(mp, map_table != maphash);
715 did_it = TRUE;
716 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200717 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200718 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200719 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200720 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200721 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200722 n = (int)STRLEN(mp->m_str);
723 p = mp->m_str;
724 }
725 else
726 {
727 n = mp->m_keylen;
728 p = mp->m_keys;
729 }
730 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
731 {
zeertzjq44068e92022-06-16 11:14:55 +0100732 if (maptype == MAPTYPE_UNMAP)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200733 {
734 // Delete entry.
735 // Only accept a full match. For abbreviations
736 // we ignore trailing space when matching with
737 // the "lhs", since an abbreviation can't have
738 // trailing space.
739 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200740 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200741 {
742 mpp = &(mp->m_next);
743 continue;
744 }
zeertzjqabeb09b2022-04-26 12:29:43 +0100745 // In keyround for simplified keys, don't unmap
746 // a mapping without m_simplified flag.
Bram Moolenaar87f74102022-04-25 18:59:25 +0100747 if (keyround1_simplified && !mp->m_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100748 break;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200749 // We reset the indicated mode bits. If nothing
750 // is left the entry is deleted below.
751 mp->m_mode &= ~mode;
752 did_it = TRUE; // remember we did something
753 }
754 else if (!hasarg) // show matching entry
755 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200756 if (!mp->m_simplified)
757 {
758 showmap(mp, map_table != maphash);
759 did_it = TRUE;
760 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200761 }
762 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200763 {
764 mpp = &(mp->m_next);
765 continue;
766 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200767 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200768 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200769 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000770 semsg(
771 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200772 p);
773 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000774 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200775 p);
776 retval = 5;
777 goto theend;
778 }
779 else
780 {
781 // new rhs for existing entry
782 mp->m_mode &= ~mode; // remove mode bits
783 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200784 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200785 char_u *newstr = vim_strsave(rhs);
786
787 if (newstr == NULL)
788 {
789 retval = 4; // no mem
790 goto theend;
791 }
792 vim_free(mp->m_str);
793 mp->m_str = newstr;
794 vim_free(mp->m_orig_str);
795 mp->m_orig_str = vim_strsave(orig_rhs);
796 mp->m_noremap = noremap;
797 mp->m_nowait = nowait;
798 mp->m_silent = silent;
799 mp->m_mode = mode;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100800 mp->m_simplified = keyround1_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200801#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200802 mp->m_expr = expr;
803 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100804 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200805#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200806 did_it = TRUE;
807 }
808 }
809 if (mp->m_mode == 0) // entry can be deleted
810 {
811 map_free(mpp);
812 continue; // continue with *mpp
813 }
814
815 // May need to put this entry into another hash
816 // list.
Bram Moolenaar9f62ea02022-10-19 13:07:03 +0100817 int new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200818 if (!abbrev && new_hash != hash)
819 {
820 *mpp = mp->m_next;
821 mp->m_next = map_table[new_hash];
822 map_table[new_hash] = mp;
823
824 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200825 }
826 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200827 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200828 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200829 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200830 }
831 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200832
zeertzjq44068e92022-06-16 11:14:55 +0100833 if (maptype == MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200834 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200835 // delete entry
836 if (!did_it)
zeertzjqa4e33322022-04-24 17:07:53 +0100837 {
Bram Moolenaar87f74102022-04-25 18:59:25 +0100838 if (!keyround1_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100839 retval = 2; // no match
840 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200841 else if (*keys == Ctrl_C)
842 {
843 // If CTRL-C has been unmapped, reuse it for Interrupting.
844 if (map_table == curbuf->b_maphash)
845 curbuf->b_mapped_ctrl_c &= ~mode;
846 else
847 mapped_ctrl_c &= ~mode;
848 }
849 continue;
850 }
851
852 if (!haskey || !hasarg)
853 {
854 // print entries
855 if (!did_it && !did_local)
856 {
857 if (abbrev)
858 msg(_("No abbreviation found"));
859 else
860 msg(_("No mapping found"));
861 }
862 goto theend; // listing finished
863 }
864
865 if (did_it)
866 continue; // have added the new entry already
867
868 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200869 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
870 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200871#ifdef FEAT_EVAL
Bram Moolenaara9528b32022-01-18 20:51:35 +0000872 expr, /* sid */ -1, /* scriptversion */ 0, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200873#endif
Bram Moolenaar87f74102022-04-25 18:59:25 +0100874 keyround1_simplified) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200875 {
876 retval = 4; // no mem
877 goto theend;
878 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200879 }
880
881theend:
882 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200883 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200884 vim_free(arg_buf);
885 return retval;
886}
887
888/*
889 * Get the mapping mode from the command name.
890 */
891 static int
892get_map_mode(char_u **cmdp, int forceit)
893{
894 char_u *p;
895 int modec;
896 int mode;
897
898 p = *cmdp;
899 modec = *p++;
900 if (modec == 'i')
Bram Moolenaar24959102022-05-07 20:01:16 +0100901 mode = MODE_INSERT; // :imap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200902 else if (modec == 'l')
Bram Moolenaar24959102022-05-07 20:01:16 +0100903 mode = MODE_LANGMAP; // :lmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200904 else if (modec == 'c')
Bram Moolenaar24959102022-05-07 20:01:16 +0100905 mode = MODE_CMDLINE; // :cmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200906 else if (modec == 'n' && *p != 'o') // avoid :noremap
Bram Moolenaar24959102022-05-07 20:01:16 +0100907 mode = MODE_NORMAL; // :nmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200908 else if (modec == 'v')
Bram Moolenaar24959102022-05-07 20:01:16 +0100909 mode = MODE_VISUAL | MODE_SELECT; // :vmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200910 else if (modec == 'x')
Bram Moolenaar24959102022-05-07 20:01:16 +0100911 mode = MODE_VISUAL; // :xmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200912 else if (modec == 's')
Bram Moolenaar24959102022-05-07 20:01:16 +0100913 mode = MODE_SELECT; // :smap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200914 else if (modec == 'o')
Bram Moolenaar24959102022-05-07 20:01:16 +0100915 mode = MODE_OP_PENDING; // :omap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200916 else if (modec == 't')
Bram Moolenaar24959102022-05-07 20:01:16 +0100917 mode = MODE_TERMINAL; // :tmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200918 else
919 {
920 --p;
921 if (forceit)
Bram Moolenaar24959102022-05-07 20:01:16 +0100922 mode = MODE_INSERT | MODE_CMDLINE; // :map !
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200923 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100924 mode = MODE_VISUAL | MODE_SELECT | MODE_NORMAL | MODE_OP_PENDING;
925 // :map
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200926 }
927
928 *cmdp = p;
929 return mode;
930}
931
932/*
zeertzjqc207fd22022-06-29 10:37:40 +0100933 * Clear all mappings (":mapclear") or abbreviations (":abclear").
934 * "abbr" should be FALSE for mappings, TRUE for abbreviations.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200935 */
936 static void
937map_clear(
938 char_u *cmdp,
zeertzjqc207fd22022-06-29 10:37:40 +0100939 char_u *arg,
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200940 int forceit,
941 int abbr)
942{
943 int mode;
944 int local;
945
946 local = (STRCMP(arg, "<buffer>") == 0);
947 if (!local && *arg != NUL)
948 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000949 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200950 return;
951 }
952
953 mode = get_map_mode(&cmdp, forceit);
zeertzjqc207fd22022-06-29 10:37:40 +0100954 map_clear_mode(curbuf, mode, local, abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200955}
956
957/*
958 * Clear all mappings in "mode".
959 */
960 void
zeertzjqc207fd22022-06-29 10:37:40 +0100961map_clear_mode(
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200962 buf_T *buf, // buffer for local mappings
963 int mode, // mode in which to delete
964 int local, // TRUE for buffer-local mappings
965 int abbr) // TRUE for abbreviations
966{
967 mapblock_T *mp, **mpp;
968 int hash;
969 int new_hash;
970
971 validate_maphash();
972
973 for (hash = 0; hash < 256; ++hash)
974 {
975 if (abbr)
976 {
977 if (hash > 0) // there is only one abbrlist
978 break;
979 if (local)
980 mpp = &buf->b_first_abbr;
981 else
982 mpp = &first_abbr;
983 }
984 else
985 {
986 if (local)
987 mpp = &buf->b_maphash[hash];
988 else
989 mpp = &maphash[hash];
990 }
991 while (*mpp != NULL)
992 {
993 mp = *mpp;
994 if (mp->m_mode & mode)
995 {
996 mp->m_mode &= ~mode;
997 if (mp->m_mode == 0) // entry can be deleted
998 {
999 map_free(mpp);
1000 continue;
1001 }
1002 // May need to put this entry into another hash list.
1003 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
1004 if (!abbr && new_hash != hash)
1005 {
1006 *mpp = mp->m_next;
1007 if (local)
1008 {
1009 mp->m_next = buf->b_maphash[new_hash];
1010 buf->b_maphash[new_hash] = mp;
1011 }
1012 else
1013 {
1014 mp->m_next = maphash[new_hash];
1015 maphash[new_hash] = mp;
1016 }
1017 continue; // continue with *mpp
1018 }
1019 }
1020 mpp = &(mp->m_next);
1021 }
1022 }
1023}
1024
1025#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001026 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001027mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001028{
1029 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001030
1031 if (vim_strchr(modechars, 'n') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001032 mode |= MODE_NORMAL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001033 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001034 mode |= MODE_VISUAL | MODE_SELECT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001035 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001036 mode |= MODE_VISUAL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001037 if (vim_strchr(modechars, 's') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001038 mode |= MODE_SELECT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001039 if (vim_strchr(modechars, 'o') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001040 mode |= MODE_OP_PENDING;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001041 if (vim_strchr(modechars, 'i') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001042 mode |= MODE_INSERT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001043 if (vim_strchr(modechars, 'l') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001044 mode |= MODE_LANGMAP;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001045 if (vim_strchr(modechars, 'c') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001046 mode |= MODE_CMDLINE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001047
Bram Moolenaar581ba392019-09-03 22:08:33 +02001048 return mode;
1049}
1050
1051/*
1052 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1053 * Recognize termcap codes in "str".
1054 * Also checks mappings local to the current buffer.
1055 */
1056 int
1057map_to_exists(char_u *str, char_u *modechars, int abbr)
1058{
1059 char_u *rhs;
1060 char_u *buf;
1061 int retval;
1062
Bram Moolenaar459fd782019-10-13 16:43:39 +02001063 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001064
1065 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001066 vim_free(buf);
1067
1068 return retval;
1069}
1070#endif
1071
1072/*
1073 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1074 * Also checks mappings local to the current buffer.
1075 */
1076 int
1077map_to_exists_mode(char_u *rhs, int mode, int abbr)
1078{
1079 mapblock_T *mp;
1080 int hash;
1081 int exp_buffer = FALSE;
1082
1083 validate_maphash();
1084
1085 // Do it twice: once for global maps and once for local maps.
1086 for (;;)
1087 {
1088 for (hash = 0; hash < 256; ++hash)
1089 {
1090 if (abbr)
1091 {
1092 if (hash > 0) // there is only one abbr list
1093 break;
1094 if (exp_buffer)
1095 mp = curbuf->b_first_abbr;
1096 else
1097 mp = first_abbr;
1098 }
1099 else if (exp_buffer)
1100 mp = curbuf->b_maphash[hash];
1101 else
1102 mp = maphash[hash];
1103 for (; mp; mp = mp->m_next)
1104 {
1105 if ((mp->m_mode & mode)
1106 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1107 return TRUE;
1108 }
1109 }
1110 if (exp_buffer)
1111 break;
1112 exp_buffer = TRUE;
1113 }
1114
1115 return FALSE;
1116}
1117
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001118/*
1119 * Used below when expanding mapping/abbreviation names.
1120 */
1121static int expand_mapmodes = 0;
1122static int expand_isabbrev = 0;
1123static int expand_buffer = FALSE;
1124
1125/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001126 * Translate an internal mapping/abbreviation representation into the
1127 * corresponding external one recognized by :map/:abbrev commands.
1128 * Respects the current B/k/< settings of 'cpoption'.
1129 *
1130 * This function is called when expanding mappings/abbreviations on the
1131 * command-line.
1132 *
1133 * It uses a growarray to build the translation string since the latter can be
1134 * wider than the original description. The caller has to free the string
1135 * afterwards.
1136 *
1137 * Returns NULL when there is a problem.
1138 */
1139 static char_u *
1140translate_mapping(char_u *str)
1141{
1142 garray_T ga;
1143 int c;
1144 int modifiers;
1145 int cpo_bslash;
1146 int cpo_special;
1147
1148 ga_init(&ga);
1149 ga.ga_itemsize = 1;
1150 ga.ga_growsize = 40;
1151
1152 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1153 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1154
1155 for (; *str; ++str)
1156 {
1157 c = *str;
1158 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1159 {
1160 modifiers = 0;
1161 if (str[1] == KS_MODIFIER)
1162 {
1163 str++;
1164 modifiers = *++str;
1165 c = *++str;
1166 }
1167 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1168 {
1169 if (cpo_special)
1170 {
1171 ga_clear(&ga);
1172 return NULL;
1173 }
1174 c = TO_SPECIAL(str[1], str[2]);
1175 if (c == K_ZERO) // display <Nul> as ^@
1176 c = NUL;
1177 str += 2;
1178 }
1179 if (IS_SPECIAL(c) || modifiers) // special key
1180 {
1181 if (cpo_special)
1182 {
1183 ga_clear(&ga);
1184 return NULL;
1185 }
1186 ga_concat(&ga, get_special_key_name(c, modifiers));
1187 continue; // for (str)
1188 }
1189 }
1190 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1191 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1192 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1193 if (c)
1194 ga_append(&ga, c);
1195 }
1196 ga_append(&ga, NUL);
1197 return (char_u *)(ga.ga_data);
1198}
1199
1200/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001201 * Work out what to complete when doing command line completion of mapping
1202 * or abbreviation names.
1203 */
1204 char_u *
1205set_context_in_map_cmd(
1206 expand_T *xp,
1207 char_u *cmd,
1208 char_u *arg,
1209 int forceit, // TRUE if '!' given
1210 int isabbrev, // TRUE if abbreviation
1211 int isunmap, // TRUE if unmap/unabbrev command
1212 cmdidx_T cmdidx)
1213{
1214 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1215 xp->xp_context = EXPAND_NOTHING;
1216 else
1217 {
1218 if (isunmap)
1219 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1220 else
1221 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001222 expand_mapmodes = MODE_INSERT | MODE_CMDLINE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001223 if (!isabbrev)
Bram Moolenaar24959102022-05-07 20:01:16 +01001224 expand_mapmodes += MODE_VISUAL | MODE_SELECT | MODE_NORMAL
1225 | MODE_OP_PENDING;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001226 }
1227 expand_isabbrev = isabbrev;
1228 xp->xp_context = EXPAND_MAPPINGS;
1229 expand_buffer = FALSE;
1230 for (;;)
1231 {
1232 if (STRNCMP(arg, "<buffer>", 8) == 0)
1233 {
1234 expand_buffer = TRUE;
1235 arg = skipwhite(arg + 8);
1236 continue;
1237 }
1238 if (STRNCMP(arg, "<unique>", 8) == 0)
1239 {
1240 arg = skipwhite(arg + 8);
1241 continue;
1242 }
1243 if (STRNCMP(arg, "<nowait>", 8) == 0)
1244 {
1245 arg = skipwhite(arg + 8);
1246 continue;
1247 }
1248 if (STRNCMP(arg, "<silent>", 8) == 0)
1249 {
1250 arg = skipwhite(arg + 8);
1251 continue;
1252 }
1253 if (STRNCMP(arg, "<special>", 9) == 0)
1254 {
1255 arg = skipwhite(arg + 9);
1256 continue;
1257 }
1258#ifdef FEAT_EVAL
1259 if (STRNCMP(arg, "<script>", 8) == 0)
1260 {
1261 arg = skipwhite(arg + 8);
1262 continue;
1263 }
1264 if (STRNCMP(arg, "<expr>", 6) == 0)
1265 {
1266 arg = skipwhite(arg + 6);
1267 continue;
1268 }
1269#endif
1270 break;
1271 }
1272 xp->xp_pattern = arg;
1273 }
1274
1275 return NULL;
1276}
1277
1278/*
1279 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1280 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1281 * Return OK if matches found, FAIL otherwise.
1282 */
1283 int
1284ExpandMappings(
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001285 char_u *pat,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001286 regmatch_T *regmatch,
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001287 int *numMatches,
1288 char_u ***matches)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001289{
1290 mapblock_T *mp;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001291 garray_T ga;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001292 int hash;
1293 int count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001294 char_u *p;
1295 int i;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001296 int fuzzy;
1297 int match;
Yasuhiro Matsumoto09f68a52022-06-18 16:48:36 +01001298 int score = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001299 fuzmatch_str_T *fuzmatch;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001300
1301 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001302
1303 validate_maphash();
1304
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001305 *numMatches = 0; // return values in case of FAIL
1306 *matches = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001307
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001308 if (!fuzzy)
1309 ga_init2(&ga, sizeof(char *), 3);
1310 else
1311 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001312
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001313 // First search in map modifier arguments
1314 for (i = 0; i < 7; ++i)
1315 {
1316 if (i == 0)
1317 p = (char_u *)"<silent>";
1318 else if (i == 1)
1319 p = (char_u *)"<unique>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001320#ifdef FEAT_EVAL
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001321 else if (i == 2)
1322 p = (char_u *)"<script>";
1323 else if (i == 3)
1324 p = (char_u *)"<expr>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001325#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001326 else if (i == 4 && !expand_buffer)
1327 p = (char_u *)"<buffer>";
1328 else if (i == 5)
1329 p = (char_u *)"<nowait>";
1330 else if (i == 6)
1331 p = (char_u *)"<special>";
1332 else
1333 continue;
1334
1335 if (!fuzzy)
1336 match = vim_regexec(regmatch, p, (colnr_T)0);
1337 else
1338 {
1339 score = fuzzy_match_str(p, pat);
1340 match = (score != 0);
1341 }
1342
1343 if (!match)
1344 continue;
1345
1346 if (ga_grow(&ga, 1) == FAIL)
1347 break;
1348
1349 if (fuzzy)
1350 {
1351 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1352 fuzmatch->idx = ga.ga_len;
1353 fuzmatch->str = vim_strsave(p);
1354 fuzmatch->score = score;
1355 }
1356 else
1357 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
1358 ++ga.ga_len;
1359 }
1360
1361 for (hash = 0; hash < 256; ++hash)
1362 {
1363 if (expand_isabbrev)
1364 {
1365 if (hash > 0) // only one abbrev list
1366 break; // for (hash)
1367 mp = first_abbr;
1368 }
1369 else if (expand_buffer)
1370 mp = curbuf->b_maphash[hash];
1371 else
1372 mp = maphash[hash];
1373 for (; mp; mp = mp->m_next)
1374 {
1375 if (!(mp->m_mode & expand_mapmodes))
1376 continue;
1377
1378 p = translate_mapping(mp->m_keys);
1379 if (p == NULL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001380 continue;
1381
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001382 if (!fuzzy)
1383 match = vim_regexec(regmatch, p, (colnr_T)0);
1384 else
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001385 {
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001386 score = fuzzy_match_str(p, pat);
1387 match = (score != 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001388 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001389
1390 if (!match)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001391 {
1392 vim_free(p);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001393 continue;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001394 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001395
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001396 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001397 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001398 vim_free(p);
1399 break;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001400 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001401
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001402 if (fuzzy)
1403 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001404 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1405 fuzmatch->idx = ga.ga_len;
1406 fuzmatch->str = p;
1407 fuzmatch->score = score;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001408 }
1409 else
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001410 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001411
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001412 ++ga.ga_len;
1413 } // for (mp)
1414 } // for (hash)
1415
1416 if (ga.ga_len == 0)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001417 return FAIL;
1418
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001419 if (!fuzzy)
1420 {
1421 *matches = ga.ga_data;
1422 *numMatches = ga.ga_len;
1423 }
1424 else
1425 {
1426 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
1427 FALSE) == FAIL)
1428 return FAIL;
1429 *numMatches = ga.ga_len;
1430 }
1431
1432 count = *numMatches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001433 if (count > 1)
1434 {
1435 char_u **ptr1;
1436 char_u **ptr2;
1437 char_u **ptr3;
1438
1439 // Sort the matches
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001440 // Fuzzy matching already sorts the matches
1441 if (!fuzzy)
1442 sort_strings(*matches, count);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001443
1444 // Remove multiple entries
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001445 ptr1 = *matches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001446 ptr2 = ptr1 + 1;
1447 ptr3 = ptr1 + count;
1448
1449 while (ptr2 < ptr3)
1450 {
1451 if (STRCMP(*ptr1, *ptr2))
1452 *++ptr1 = *ptr2++;
1453 else
1454 {
1455 vim_free(*ptr2++);
1456 count--;
1457 }
1458 }
1459 }
1460
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001461 *numMatches = count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001462 return (count == 0 ? FAIL : OK);
1463}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001464
1465/*
1466 * Check for an abbreviation.
1467 * Cursor is at ptr[col].
1468 * When inserting, mincol is where insert started.
1469 * For the command line, mincol is what is to be skipped over.
1470 * "c" is the character typed before check_abbr was called. It may have
1471 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1472 *
1473 * Historic vi practice: The last character of an abbreviation must be an id
1474 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1475 * characters or all non-id characters. This allows for abbr. "#i" to
1476 * "#include".
1477 *
1478 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1479 * Then there must be white space before the abbr.
1480 *
1481 * return TRUE if there is an abbreviation, FALSE if not
1482 */
1483 int
1484check_abbr(
1485 int c,
1486 char_u *ptr,
1487 int col,
1488 int mincol)
1489{
1490 int len;
1491 int scol; // starting column of the abbr.
1492 int j;
1493 char_u *s;
1494 char_u tb[MB_MAXBYTES + 4];
1495 mapblock_T *mp;
1496 mapblock_T *mp2;
1497 int clen = 0; // length in characters
1498 int is_id = TRUE;
1499 int vim_abbr;
1500
1501 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1502 return FALSE;
1503
1504 // no remapping implies no abbreviation, except for CTRL-]
1505 if (noremap_keys() && c != Ctrl_RSB)
1506 return FALSE;
1507
1508 // Check for word before the cursor: If it ends in a keyword char all
1509 // chars before it must be keyword chars or non-keyword chars, but not
1510 // white space. If it ends in a non-keyword char we accept any characters
1511 // before it except white space.
1512 if (col == 0) // cannot be an abbr.
1513 return FALSE;
1514
1515 if (has_mbyte)
1516 {
1517 char_u *p;
1518
1519 p = mb_prevptr(ptr, ptr + col);
1520 if (!vim_iswordp(p))
1521 vim_abbr = TRUE; // Vim added abbr.
1522 else
1523 {
1524 vim_abbr = FALSE; // vi compatible abbr.
1525 if (p > ptr)
1526 is_id = vim_iswordp(mb_prevptr(ptr, p));
1527 }
1528 clen = 1;
1529 while (p > ptr + mincol)
1530 {
1531 p = mb_prevptr(ptr, p);
1532 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1533 {
1534 p += (*mb_ptr2len)(p);
1535 break;
1536 }
1537 ++clen;
1538 }
1539 scol = (int)(p - ptr);
1540 }
1541 else
1542 {
1543 if (!vim_iswordc(ptr[col - 1]))
1544 vim_abbr = TRUE; // Vim added abbr.
1545 else
1546 {
1547 vim_abbr = FALSE; // vi compatible abbr.
1548 if (col > 1)
1549 is_id = vim_iswordc(ptr[col - 2]);
1550 }
1551 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1552 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1553 ;
1554 }
1555
1556 if (scol < mincol)
1557 scol = mincol;
1558 if (scol < col) // there is a word in front of the cursor
1559 {
1560 ptr += scol;
1561 len = col - scol;
1562 mp = curbuf->b_first_abbr;
1563 mp2 = first_abbr;
1564 if (mp == NULL)
1565 {
1566 mp = mp2;
1567 mp2 = NULL;
1568 }
1569 for ( ; mp; mp->m_next == NULL
1570 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1571 {
1572 int qlen = mp->m_keylen;
1573 char_u *q = mp->m_keys;
1574 int match;
1575
1576 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1577 {
1578 char_u *qe = vim_strsave(mp->m_keys);
1579
1580 // might have CSI escaped mp->m_keys
1581 if (qe != NULL)
1582 {
1583 q = qe;
1584 vim_unescape_csi(q);
1585 qlen = (int)STRLEN(q);
1586 }
1587 }
1588
1589 // find entries with right mode and keys
1590 match = (mp->m_mode & State)
1591 && qlen == len
1592 && !STRNCMP(q, ptr, (size_t)len);
1593 if (q != mp->m_keys)
1594 vim_free(q);
1595 if (match)
1596 break;
1597 }
1598 if (mp != NULL)
1599 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001600 int noremap;
1601 int silent;
1602#ifdef FEAT_EVAL
1603 int expr;
1604#endif
1605
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001606 // Found a match:
1607 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1608 // This goes from end to start.
1609 //
1610 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1611 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1612 // Characters where IS_SPECIAL() == TRUE: key codes, need
1613 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1614 //
1615 // Character CTRL-] is treated specially - it completes the
1616 // abbreviation, but is not inserted into the input stream.
1617 j = 0;
1618 if (c != Ctrl_RSB)
1619 {
1620 // special key code, split up
1621 if (IS_SPECIAL(c) || c == K_SPECIAL)
1622 {
1623 tb[j++] = K_SPECIAL;
1624 tb[j++] = K_SECOND(c);
1625 tb[j++] = K_THIRD(c);
1626 }
1627 else
1628 {
1629 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1630 tb[j++] = Ctrl_V; // special char needs CTRL-V
1631 if (has_mbyte)
1632 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001633 int newlen;
1634 char_u *escaped;
1635
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001636 // if ABBR_OFF has been added, remove it here
1637 if (c >= ABBR_OFF)
1638 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001639 newlen = (*mb_char2bytes)(c, tb + j);
1640 tb[j + newlen] = NUL;
1641 // Need to escape K_SPECIAL.
1642 escaped = vim_strsave_escape_csi(tb + j);
1643 if (escaped != NULL)
1644 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001645 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001646 mch_memmove(tb + j, escaped, newlen);
1647 j += newlen;
1648 vim_free(escaped);
1649 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001650 }
1651 else
1652 tb[j++] = c;
1653 }
1654 tb[j] = NUL;
1655 // insert the last typed char
1656 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1657 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001658
1659 // copy values here, calling eval_map_expr() may make "mp" invalid!
1660 noremap = mp->m_noremap;
1661 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001662#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001663 expr = mp->m_expr;
1664
1665 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001666 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001667 else
1668#endif
1669 s = mp->m_str;
1670 if (s != NULL)
1671 {
1672 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001673 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001674 // no abbrev. for these chars
1675 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1676#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001677 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001678 vim_free(s);
1679#endif
1680 }
1681
1682 tb[0] = Ctrl_H;
1683 tb[1] = NUL;
1684 if (has_mbyte)
1685 len = clen; // Delete characters instead of bytes
1686 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001687 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001688 return TRUE;
1689 }
1690 }
1691 return FALSE;
1692}
1693
1694#ifdef FEAT_EVAL
1695/*
1696 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1697 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001698 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001699 */
1700 char_u *
1701eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001702 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001703 int c) // NUL or typed character for abbreviation
1704{
1705 char_u *res;
1706 char_u *p;
1707 char_u *expr;
1708 pos_T save_cursor;
1709 int save_msg_col;
1710 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001711 scid_T save_sctx_sid = current_sctx.sc_sid;
1712 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001713
1714 // Remove escaping of CSI, because "str" is in a format to be used as
1715 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001716 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001717 if (expr == NULL)
1718 return NULL;
1719 vim_unescape_csi(expr);
1720
1721 // Forbid changing text or using ":normal" to avoid most of the bad side
1722 // effects. Also restore the cursor position.
zeertzjqcfe45652022-05-27 17:26:55 +01001723 ++textlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001724 ++ex_normal_lock;
1725 set_vim_var_char(c); // set v:char to the typed character
1726 save_cursor = curwin->w_cursor;
1727 save_msg_col = msg_col;
1728 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001729 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1730 {
1731 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1732 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1733 }
1734
1735 // Note: the evaluation may make "mp" invalid.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001736 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001737
zeertzjqcfe45652022-05-27 17:26:55 +01001738 --textlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001739 --ex_normal_lock;
1740 curwin->w_cursor = save_cursor;
1741 msg_col = save_msg_col;
1742 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001743 current_sctx.sc_sid = save_sctx_sid;
1744 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001745
1746 vim_free(expr);
1747
1748 if (p == NULL)
1749 return NULL;
1750 // Escape CSI in the result to be able to use the string as typeahead.
1751 res = vim_strsave_escape_csi(p);
1752 vim_free(p);
1753
1754 return res;
1755}
1756#endif
1757
1758/*
1759 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1760 * can be put in the typeahead buffer.
1761 * Returns NULL when out of memory.
1762 */
1763 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001764vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001765{
1766 char_u *res;
1767 char_u *s, *d;
1768
1769 // Need a buffer to hold up to three times as much. Four in case of an
1770 // illegal utf-8 byte:
1771 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1772 res = alloc(STRLEN(p) * 4 + 1);
1773 if (res != NULL)
1774 {
1775 d = res;
1776 for (s = p; *s != NUL; )
1777 {
zeertzjq2cd0f272022-10-04 20:14:28 +01001778 if ((s[0] == K_SPECIAL
1779#ifdef FEAT_GUI
1780 || (gui.in_use && s[0] == CSI)
1781#endif
1782 ) && s[1] != NUL && s[2] != NUL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001783 {
1784 // Copy special key unmodified.
1785 *d++ = *s++;
1786 *d++ = *s++;
1787 *d++ = *s++;
1788 }
1789 else
1790 {
1791 // Add character, possibly multi-byte to destination, escaping
1792 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1793 d = add_char2buf(PTR2CHAR(s), d);
1794 s += MB_CPTR2LEN(s);
1795 }
1796 }
1797 *d = NUL;
1798 }
1799 return res;
1800}
1801
1802/*
1803 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1804 * vim_strsave_escape_csi(). Works in-place.
1805 */
1806 void
1807vim_unescape_csi(char_u *p)
1808{
1809 char_u *s = p, *d = p;
1810
1811 while (*s != NUL)
1812 {
1813 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1814 {
1815 *d++ = K_SPECIAL;
1816 s += 3;
1817 }
1818 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1819 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1820 {
1821 *d++ = CSI;
1822 s += 3;
1823 }
1824 else
1825 *d++ = *s++;
1826 }
1827 *d = NUL;
1828}
1829
1830/*
1831 * Write map commands for the current mappings to an .exrc file.
1832 * Return FAIL on error, OK otherwise.
1833 */
1834 int
1835makemap(
1836 FILE *fd,
1837 buf_T *buf) // buffer for local mappings or NULL
1838{
1839 mapblock_T *mp;
1840 char_u c1, c2, c3;
1841 char_u *p;
1842 char *cmd;
1843 int abbr;
1844 int hash;
1845 int did_cpo = FALSE;
1846 int i;
1847
1848 validate_maphash();
1849
1850 // Do the loop twice: Once for mappings, once for abbreviations.
1851 // Then loop over all map hash lists.
1852 for (abbr = 0; abbr < 2; ++abbr)
1853 for (hash = 0; hash < 256; ++hash)
1854 {
1855 if (abbr)
1856 {
1857 if (hash > 0) // there is only one abbr list
1858 break;
1859 if (buf != NULL)
1860 mp = buf->b_first_abbr;
1861 else
1862 mp = first_abbr;
1863 }
1864 else
1865 {
1866 if (buf != NULL)
1867 mp = buf->b_maphash[hash];
1868 else
1869 mp = maphash[hash];
1870 }
1871
1872 for ( ; mp; mp = mp->m_next)
1873 {
1874 // skip script-local mappings
1875 if (mp->m_noremap == REMAP_SCRIPT)
1876 continue;
1877
1878 // skip mappings that contain a <SNR> (script-local thing),
1879 // they probably don't work when loaded again
1880 for (p = mp->m_str; *p != NUL; ++p)
1881 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1882 && p[2] == (int)KE_SNR)
1883 break;
1884 if (*p != NUL)
1885 continue;
1886
1887 // It's possible to create a mapping and then ":unmap" certain
1888 // modes. We recreate this here by mapping the individual
1889 // modes, which requires up to three of them.
1890 c1 = NUL;
1891 c2 = NUL;
1892 c3 = NUL;
1893 if (abbr)
1894 cmd = "abbr";
1895 else
1896 cmd = "map";
1897 switch (mp->m_mode)
1898 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001899 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT
1900 | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001901 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001902 case MODE_NORMAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001903 c1 = 'n';
1904 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001905 case MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001906 c1 = 'x';
1907 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001908 case MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001909 c1 = 's';
1910 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001911 case MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001912 c1 = 'o';
1913 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001914 case MODE_NORMAL | MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001915 c1 = 'n';
1916 c2 = 'x';
1917 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001918 case MODE_NORMAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001919 c1 = 'n';
1920 c2 = 's';
1921 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001922 case MODE_NORMAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001923 c1 = 'n';
1924 c2 = 'o';
1925 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001926 case MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001927 c1 = 'v';
1928 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001929 case MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001930 c1 = 'x';
1931 c2 = 'o';
1932 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001933 case MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001934 c1 = 's';
1935 c2 = 'o';
1936 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001937 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001938 c1 = 'n';
1939 c2 = 'v';
1940 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001941 case MODE_NORMAL | MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001942 c1 = 'n';
1943 c2 = 'x';
1944 c3 = 'o';
1945 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001946 case MODE_NORMAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001947 c1 = 'n';
1948 c2 = 's';
1949 c3 = 'o';
1950 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001951 case MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001952 c1 = 'v';
1953 c2 = 'o';
1954 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001955 case MODE_CMDLINE | MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001956 if (!abbr)
1957 cmd = "map!";
1958 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001959 case MODE_CMDLINE:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001960 c1 = 'c';
1961 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001962 case MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001963 c1 = 'i';
1964 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001965 case MODE_LANGMAP:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001966 c1 = 'l';
1967 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001968 case MODE_TERMINAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001969 c1 = 't';
1970 break;
1971 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001972 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001973 return FAIL;
1974 }
1975 do // do this twice if c2 is set, 3 times with c3
1976 {
1977 // When outputting <> form, need to make sure that 'cpo'
1978 // is set to the Vim default.
1979 if (!did_cpo)
1980 {
1981 if (*mp->m_str == NUL) // will use <Nop>
1982 did_cpo = TRUE;
1983 else
1984 for (i = 0; i < 2; ++i)
1985 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1986 if (*p == K_SPECIAL || *p == NL)
1987 did_cpo = TRUE;
1988 if (did_cpo)
1989 {
1990 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1991 || put_eol(fd) < 0
1992 || fprintf(fd, "set cpo&vim") < 0
1993 || put_eol(fd) < 0)
1994 return FAIL;
1995 }
1996 }
1997 if (c1 && putc(c1, fd) < 0)
1998 return FAIL;
1999 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
2000 return FAIL;
2001 if (fputs(cmd, fd) < 0)
2002 return FAIL;
2003 if (buf != NULL && fputs(" <buffer>", fd) < 0)
2004 return FAIL;
2005 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
2006 return FAIL;
2007 if (mp->m_silent && fputs(" <silent>", fd) < 0)
2008 return FAIL;
2009#ifdef FEAT_EVAL
2010 if (mp->m_noremap == REMAP_SCRIPT
2011 && fputs("<script>", fd) < 0)
2012 return FAIL;
2013 if (mp->m_expr && fputs(" <expr>", fd) < 0)
2014 return FAIL;
2015#endif
2016
2017 if ( putc(' ', fd) < 0
2018 || put_escstr(fd, mp->m_keys, 0) == FAIL
2019 || putc(' ', fd) < 0
2020 || put_escstr(fd, mp->m_str, 1) == FAIL
2021 || put_eol(fd) < 0)
2022 return FAIL;
2023 c1 = c2;
2024 c2 = c3;
2025 c3 = NUL;
2026 } while (c1 != NUL);
2027 }
2028 }
2029
2030 if (did_cpo)
2031 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
2032 || put_eol(fd) < 0
2033 || fprintf(fd, "unlet s:cpo_save") < 0
2034 || put_eol(fd) < 0)
2035 return FAIL;
2036 return OK;
2037}
2038
2039/*
2040 * write escape string to file
2041 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2042 *
2043 * return FAIL for failure, OK otherwise
2044 */
2045 int
2046put_escstr(FILE *fd, char_u *strstart, int what)
2047{
2048 char_u *str = strstart;
2049 int c;
2050 int modifiers;
2051
2052 // :map xx <Nop>
2053 if (*str == NUL && what == 1)
2054 {
2055 if (fprintf(fd, "<Nop>") < 0)
2056 return FAIL;
2057 return OK;
2058 }
2059
2060 for ( ; *str != NUL; ++str)
2061 {
2062 char_u *p;
2063
2064 // Check for a multi-byte character, which may contain escaped
2065 // K_SPECIAL and CSI bytes
2066 p = mb_unescape(&str);
2067 if (p != NULL)
2068 {
2069 while (*p != NUL)
2070 if (fputc(*p++, fd) < 0)
2071 return FAIL;
2072 --str;
2073 continue;
2074 }
2075
2076 c = *str;
2077 // Special key codes have to be translated to be able to make sense
2078 // when they are read back.
2079 if (c == K_SPECIAL && what != 2)
2080 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002081 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002082 if (str[1] == KS_MODIFIER)
2083 {
2084 modifiers = str[2];
2085 str += 3;
2086 c = *str;
2087 }
2088 if (c == K_SPECIAL)
2089 {
2090 c = TO_SPECIAL(str[1], str[2]);
2091 str += 2;
2092 }
2093 if (IS_SPECIAL(c) || modifiers) // special key
2094 {
2095 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2096 return FAIL;
2097 continue;
2098 }
2099 }
2100
2101 // A '\n' in a map command should be written as <NL>.
2102 // A '\n' in a set command should be written as \^V^J.
2103 if (c == NL)
2104 {
2105 if (what == 2)
2106 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002107 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002108 return FAIL;
2109 }
2110 else
2111 {
2112 if (fprintf(fd, "<NL>") < 0)
2113 return FAIL;
2114 }
2115 continue;
2116 }
2117
2118 // Some characters have to be escaped with CTRL-V to
2119 // prevent them from misinterpreted in DoOneCmd().
2120 // A space, Tab and '"' has to be escaped with a backslash to
2121 // prevent it to be misinterpreted in do_set().
2122 // A space has to be escaped with a CTRL-V when it's at the start of a
2123 // ":map" rhs.
2124 // A '<' has to be escaped with a CTRL-V to prevent it being
2125 // interpreted as the start of a special key name.
2126 // A space in the lhs of a :map needs a CTRL-V.
2127 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2128 {
2129 if (putc('\\', fd) < 0)
2130 return FAIL;
2131 }
2132 else if (c < ' ' || c > '~' || c == '|'
2133 || (what == 0 && c == ' ')
2134 || (what == 1 && str == strstart && c == ' ')
2135 || (what != 2 && c == '<'))
2136 {
2137 if (putc(Ctrl_V, fd) < 0)
2138 return FAIL;
2139 }
2140 if (putc(c, fd) < 0)
2141 return FAIL;
2142 }
2143 return OK;
2144}
2145
2146/*
2147 * Check all mappings for the presence of special key codes.
2148 * Used after ":set term=xxx".
2149 */
2150 void
2151check_map_keycodes(void)
2152{
2153 mapblock_T *mp;
2154 char_u *p;
2155 int i;
2156 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002157 int abbr;
2158 int hash;
2159 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002160 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002161
2162 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002163 // avoids giving error messages
2164 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002165 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002166
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002167 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002168 // mappings/abbreviations with bp == NULL
2169 for (bp = firstbuf; ; bp = bp->b_next)
2170 {
2171 // Do the loop twice: Once for mappings, once for abbreviations.
2172 // Then loop over all map hash lists.
2173 for (abbr = 0; abbr <= 1; ++abbr)
2174 for (hash = 0; hash < 256; ++hash)
2175 {
2176 if (abbr)
2177 {
2178 if (hash) // there is only one abbr list
2179 break;
2180 if (bp != NULL)
2181 mp = bp->b_first_abbr;
2182 else
2183 mp = first_abbr;
2184 }
2185 else
2186 {
2187 if (bp != NULL)
2188 mp = bp->b_maphash[hash];
2189 else
2190 mp = maphash[hash];
2191 }
2192 for ( ; mp != NULL; mp = mp->m_next)
2193 {
2194 for (i = 0; i <= 1; ++i) // do this twice
2195 {
2196 if (i == 0)
2197 p = mp->m_keys; // once for the "from" part
2198 else
2199 p = mp->m_str; // and once for the "to" part
2200 while (*p)
2201 {
2202 if (*p == K_SPECIAL)
2203 {
2204 ++p;
2205 if (*p < 128) // for "normal" tcap entries
2206 {
2207 buf[0] = p[0];
2208 buf[1] = p[1];
2209 buf[2] = NUL;
2210 (void)add_termcap_entry(buf, FALSE);
2211 }
2212 ++p;
2213 }
2214 ++p;
2215 }
2216 }
2217 }
2218 }
2219 if (bp == NULL)
2220 break;
2221 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002222 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002223 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002224}
2225
2226#if defined(FEAT_EVAL) || defined(PROTO)
2227/*
2228 * Check the string "keys" against the lhs of all mappings.
2229 * Return pointer to rhs of mapping (mapblock->m_str).
2230 * NULL when no mapping found.
2231 */
2232 char_u *
2233check_map(
2234 char_u *keys,
2235 int mode,
2236 int exact, // require exact match
2237 int ign_mod, // ignore preceding modifier
2238 int abbr, // do abbreviations
2239 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2240 int *local_ptr) // return: buffer-local mapping or NULL
2241{
2242 int hash;
2243 int len, minlen;
2244 mapblock_T *mp;
2245 char_u *s;
2246 int local;
2247
2248 validate_maphash();
2249
2250 len = (int)STRLEN(keys);
2251 for (local = 1; local >= 0; --local)
2252 // loop over all hash lists
2253 for (hash = 0; hash < 256; ++hash)
2254 {
2255 if (abbr)
2256 {
2257 if (hash > 0) // there is only one list.
2258 break;
2259 if (local)
2260 mp = curbuf->b_first_abbr;
2261 else
2262 mp = first_abbr;
2263 }
2264 else if (local)
2265 mp = curbuf->b_maphash[hash];
2266 else
2267 mp = maphash[hash];
2268 for ( ; mp != NULL; mp = mp->m_next)
2269 {
2270 // skip entries with wrong mode, wrong length and not matching
2271 // ones
2272 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2273 {
2274 if (len > mp->m_keylen)
2275 minlen = mp->m_keylen;
2276 else
2277 minlen = len;
2278 s = mp->m_keys;
2279 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2280 && s[2] != NUL)
2281 {
2282 s += 3;
2283 if (len > mp->m_keylen - 3)
2284 minlen = mp->m_keylen - 3;
2285 }
2286 if (STRNCMP(s, keys, minlen) == 0)
2287 {
2288 if (mp_ptr != NULL)
2289 *mp_ptr = mp;
2290 if (local_ptr != NULL)
2291 *local_ptr = local;
2292 return mp->m_str;
2293 }
2294 }
2295 }
2296 }
2297
2298 return NULL;
2299}
2300
Ernie Rael659c2402022-04-24 18:40:28 +01002301/*
zeertzjqc207fd22022-06-29 10:37:40 +01002302 * "hasmapto()" function
2303 */
2304 void
2305f_hasmapto(typval_T *argvars, typval_T *rettv)
2306{
2307 char_u *name;
2308 char_u *mode;
2309 char_u buf[NUMBUFLEN];
2310 int abbr = FALSE;
2311
2312 if (in_vim9script()
2313 && (check_for_string_arg(argvars, 0) == FAIL
2314 || check_for_opt_string_arg(argvars, 1) == FAIL
2315 || (argvars[1].v_type != VAR_UNKNOWN
2316 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2317 return;
2318
2319 name = tv_get_string(&argvars[0]);
2320 if (argvars[1].v_type == VAR_UNKNOWN)
2321 mode = (char_u *)"nvo";
2322 else
2323 {
2324 mode = tv_get_string_buf(&argvars[1], buf);
2325 if (argvars[2].v_type != VAR_UNKNOWN)
2326 abbr = (int)tv_get_bool(&argvars[2]);
2327 }
2328
2329 if (map_to_exists(name, mode, abbr))
2330 rettv->vval.v_number = TRUE;
2331 else
2332 rettv->vval.v_number = FALSE;
2333}
2334
2335/*
Ernie Rael659c2402022-04-24 18:40:28 +01002336 * Fill in the empty dictionary with items as defined by maparg builtin.
2337 */
2338 static void
2339mapblock2dict(
2340 mapblock_T *mp,
2341 dict_T *dict,
2342 char_u *lhsrawalt, // may be NULL
Ernie Rael51d04d12022-05-04 15:40:22 +01002343 int buffer_local, // false if not buffer local mapping
2344 int abbr) // true if abbreviation
Ernie Rael659c2402022-04-24 18:40:28 +01002345{
zeertzjqcdc83932022-09-12 13:38:41 +01002346 char_u *lhs = str2special_save(mp->m_keys, TRUE, FALSE);
Ernie Rael659c2402022-04-24 18:40:28 +01002347 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2348
2349 dict_add_string(dict, "lhs", lhs);
2350 vim_free(lhs);
2351 dict_add_string(dict, "lhsraw", mp->m_keys);
2352 if (lhsrawalt)
2353 // Also add the value for the simplified entry.
2354 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2355 dict_add_string(dict, "rhs", mp->m_orig_str);
2356 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2357 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2358 ? 1L : 0L);
2359 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2360 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2361 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2362 dict_add_number(dict, "scriptversion",
2363 (long)mp->m_script_ctx.sc_version);
2364 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2365 dict_add_number(dict, "buffer", (long)buffer_local);
2366 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2367 dict_add_string(dict, "mode", mapmode);
Ernie Rael51d04d12022-05-04 15:40:22 +01002368 dict_add_number(dict, "abbr", abbr ? 1L : 0L);
Ernie Raeld8f5f762022-05-10 17:50:39 +01002369 dict_add_number(dict, "mode_bits", mp->m_mode);
Ernie Rael659c2402022-04-24 18:40:28 +01002370
2371 vim_free(mapmode);
2372}
2373
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002374 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002375get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2376{
2377 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002378 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002379 char_u *which;
2380 char_u buf[NUMBUFLEN];
2381 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002382 char_u *alt_keys_buf = NULL;
2383 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002384 char_u *rhs;
2385 int mode;
2386 int abbr = FALSE;
2387 int get_dict = FALSE;
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002388 mapblock_T *mp = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002389 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002390 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002391
2392 // return empty string for failure
2393 rettv->v_type = VAR_STRING;
2394 rettv->vval.v_string = NULL;
2395
2396 keys = tv_get_string(&argvars[0]);
2397 if (*keys == NUL)
2398 return;
2399
2400 if (argvars[1].v_type != VAR_UNKNOWN)
2401 {
2402 which = tv_get_string_buf_chk(&argvars[1], buf);
2403 if (argvars[2].v_type != VAR_UNKNOWN)
2404 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002405 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002406 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002407 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002408 }
2409 }
2410 else
2411 which = (char_u *)"";
2412 if (which == NULL)
2413 return;
2414
2415 mode = get_map_mode(&which, 0);
2416
Bram Moolenaar9c652532020-05-24 13:10:18 +02002417 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2418 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2419 &mp, &buffer_local);
2420 if (did_simplify)
2421 {
2422 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002423 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002424 (void)replace_termcodes(keys, &alt_keys_buf,
2425 flags | REPTERM_NO_SIMPLIFY, NULL);
2426 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2427 &buffer_local);
2428 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002429
2430 if (!get_dict)
2431 {
2432 // Return a string.
2433 if (rhs != NULL)
2434 {
2435 if (*rhs == NUL)
2436 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2437 else
zeertzjqcdc83932022-09-12 13:38:41 +01002438 rettv->vval.v_string = str2special_save(rhs, FALSE, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002439 }
2440
2441 }
Bram Moolenaar93a10962022-06-16 11:42:09 +01002442 else if (rettv_dict_alloc(rettv) == OK && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002443 mapblock2dict(mp, rettv->vval.v_dict,
Ernie Rael51d04d12022-05-04 15:40:22 +01002444 did_simplify ? keys_simplified : NULL,
2445 buffer_local, abbr);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002446
2447 vim_free(keys_buf);
2448 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002449}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002450
2451/*
Ernie Rael09661202022-04-25 14:40:44 +01002452 * "maplist()" function
Ernie Rael659c2402022-04-24 18:40:28 +01002453 */
2454 void
Ernie Rael09661202022-04-25 14:40:44 +01002455f_maplist(typval_T *argvars UNUSED, typval_T *rettv)
Ernie Rael659c2402022-04-24 18:40:28 +01002456{
2457 dict_T *d;
2458 mapblock_T *mp;
2459 int buffer_local;
2460 char_u *keys_buf;
2461 int did_simplify;
2462 int hash;
2463 char_u *lhs;
2464 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Ernie Rael09661202022-04-25 14:40:44 +01002465 int abbr = FALSE;
2466
2467 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2468 return;
2469 if (argvars[0].v_type != VAR_UNKNOWN)
2470 abbr = tv_get_bool(&argvars[0]);
Ernie Rael659c2402022-04-24 18:40:28 +01002471
Bram Moolenaar93a10962022-06-16 11:42:09 +01002472 if (rettv_list_alloc(rettv) == FAIL)
Ernie Rael659c2402022-04-24 18:40:28 +01002473 return;
2474
2475 validate_maphash();
2476
2477 // Do it twice: once for global maps and once for local maps.
2478 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2479 {
2480 for (hash = 0; hash < 256; ++hash)
2481 {
Ernie Rael09661202022-04-25 14:40:44 +01002482 if (abbr)
2483 {
2484 if (hash > 0) // there is only one abbr list
2485 break;
2486 if (buffer_local)
2487 mp = curbuf->b_first_abbr;
2488 else
2489 mp = first_abbr;
2490 }
2491 else if (buffer_local)
Ernie Rael659c2402022-04-24 18:40:28 +01002492 mp = curbuf->b_maphash[hash];
2493 else
2494 mp = maphash[hash];
2495 for (; mp; mp = mp->m_next)
2496 {
2497 if (mp->m_simplified)
2498 continue;
2499 if ((d = dict_alloc()) == NULL)
2500 return;
2501 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2502 return;
2503
2504 keys_buf = NULL;
2505 did_simplify = FALSE;
2506
zeertzjqcdc83932022-09-12 13:38:41 +01002507 lhs = str2special_save(mp->m_keys, TRUE, FALSE);
Ernie Rael659c2402022-04-24 18:40:28 +01002508 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2509 vim_free(lhs);
2510
2511 mapblock2dict(mp, d,
Ernie Rael51d04d12022-05-04 15:40:22 +01002512 did_simplify ? keys_buf : NULL,
2513 buffer_local, abbr);
Ernie Rael659c2402022-04-24 18:40:28 +01002514 vim_free(keys_buf);
2515 }
2516 }
2517 }
2518}
2519
2520/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002521 * "maparg()" function
2522 */
2523 void
2524f_maparg(typval_T *argvars, typval_T *rettv)
2525{
2526 if (in_vim9script()
2527 && (check_for_string_arg(argvars, 0) == FAIL
2528 || check_for_opt_string_arg(argvars, 1) == FAIL
2529 || (argvars[1].v_type != VAR_UNKNOWN
2530 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2531 || (argvars[2].v_type != VAR_UNKNOWN
2532 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2533 return;
2534
2535 get_maparg(argvars, rettv, TRUE);
2536}
2537
2538/*
2539 * "mapcheck()" function
2540 */
2541 void
2542f_mapcheck(typval_T *argvars, typval_T *rettv)
2543{
2544 if (in_vim9script()
2545 && (check_for_string_arg(argvars, 0) == FAIL
2546 || check_for_opt_string_arg(argvars, 1) == FAIL
2547 || (argvars[1].v_type != VAR_UNKNOWN
2548 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2549 return;
2550
2551 get_maparg(argvars, rettv, FALSE);
2552}
2553
2554/*
Ernie Rael51d04d12022-05-04 15:40:22 +01002555 * Get the mapping mode from the mode string.
2556 * It may contain multiple characters, eg "nox", or "!", or ' '
2557 * Return 0 if there is an error.
2558 */
2559 static int
2560get_map_mode_string(char_u *mode_string, int abbr)
2561{
2562 char_u *p = mode_string;
2563 int mode = 0;
2564 int tmode;
2565 int modec;
Bram Moolenaar24959102022-05-07 20:01:16 +01002566 const int MASK_V = MODE_VISUAL | MODE_SELECT;
2567 const int MASK_MAP = MODE_VISUAL | MODE_SELECT | MODE_NORMAL
2568 | MODE_OP_PENDING;
2569 const int MASK_BANG = MODE_INSERT | MODE_CMDLINE;
Ernie Rael51d04d12022-05-04 15:40:22 +01002570
2571 if (*p == NUL)
2572 p = (char_u *)" "; // compatibility
2573 while ((modec = *p++))
2574 {
2575 switch (modec)
2576 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002577 case 'i': tmode = MODE_INSERT; break;
2578 case 'l': tmode = MODE_LANGMAP; break;
2579 case 'c': tmode = MODE_CMDLINE; break;
2580 case 'n': tmode = MODE_NORMAL; break;
2581 case 'x': tmode = MODE_VISUAL; break;
2582 case 's': tmode = MODE_SELECT; break;
2583 case 'o': tmode = MODE_OP_PENDING; break;
2584 case 't': tmode = MODE_TERMINAL; break;
Ernie Rael51d04d12022-05-04 15:40:22 +01002585 case 'v': tmode = MASK_V; break;
2586 case '!': tmode = MASK_BANG; break;
2587 case ' ': tmode = MASK_MAP; break;
2588 default:
2589 return 0; // error, unknown mode character
2590 }
2591 mode |= tmode;
2592 }
2593 if ((abbr && (mode & ~MASK_BANG) != 0)
2594 || (!abbr && (mode & (mode-1)) != 0 // more than one bit set
2595 && (
2596 // false if multiple bits set in mode and mode is fully
2597 // contained in one mask
2598 !(((mode & MASK_BANG) != 0 && (mode & ~MASK_BANG) == 0)
2599 || ((mode & MASK_MAP) != 0 && (mode & ~MASK_MAP) == 0)))))
2600 return 0;
2601
2602 return mode;
2603}
2604
2605/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002606 * "mapset()" function
2607 */
2608 void
2609f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2610{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002611 char_u *which;
2612 int mode;
2613 char_u buf[NUMBUFLEN];
2614 int is_abbr;
2615 dict_T *d;
2616 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002617 char_u *lhsraw;
2618 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002619 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002620 char_u *orig_rhs;
2621 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002622 int noremap;
2623 int expr;
2624 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002625 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002626 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002627 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002628 linenr_T lnum;
2629 mapblock_T **map_table = maphash;
2630 mapblock_T **abbr_table = &first_abbr;
2631 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002632 char_u *arg;
Ernie Rael51d04d12022-05-04 15:40:22 +01002633 int dict_only;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002634
Ernie Rael51d04d12022-05-04 15:40:22 +01002635 // If first arg is a dict, then that's the only arg permitted.
2636 dict_only = argvars[0].v_type == VAR_DICT;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002637 if (in_vim9script()
Ernie Rael51d04d12022-05-04 15:40:22 +01002638 && (check_for_string_or_dict_arg(argvars, 0) == FAIL
2639 || (dict_only && check_for_unknown_arg(argvars, 1) == FAIL)
2640 || (!dict_only
2641 && (check_for_string_arg(argvars, 0) == FAIL
2642 || check_for_bool_arg(argvars, 1) == FAIL
2643 || check_for_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002644 return;
2645
Ernie Rael51d04d12022-05-04 15:40:22 +01002646 if (dict_only)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002647 {
Ernie Rael51d04d12022-05-04 15:40:22 +01002648 d = argvars[0].vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002649 which = dict_get_string(d, "mode", FALSE);
2650 is_abbr = dict_get_bool(d, "abbr", -1);
Ernie Rael51d04d12022-05-04 15:40:22 +01002651 if (which == NULL || is_abbr < 0)
2652 {
2653 emsg(_(e_entries_missing_in_mapset_dict_argument));
2654 return;
2655 }
2656 }
2657 else
2658 {
2659 which = tv_get_string_buf_chk(&argvars[0], buf);
2660 if (which == NULL)
2661 return;
2662 is_abbr = (int)tv_get_bool(&argvars[1]);
2663
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002664 if (check_for_dict_arg(argvars, 2) == FAIL)
Ernie Rael51d04d12022-05-04 15:40:22 +01002665 return;
Ernie Rael51d04d12022-05-04 15:40:22 +01002666 d = argvars[2].vval.v_dict;
2667 }
2668 mode = get_map_mode_string(which, is_abbr);
2669 if (mode == 0)
2670 {
2671 semsg(_(e_illegal_map_mode_string_str), which);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002672 return;
2673 }
Ernie Rael51d04d12022-05-04 15:40:22 +01002674
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002675
2676 // Get the values in the same order as above in get_maparg().
Bram Moolenaard61efa52022-07-23 09:52:04 +01002677 lhs = dict_get_string(d, "lhs", FALSE);
2678 lhsraw = dict_get_string(d, "lhsraw", FALSE);
2679 lhsrawalt = dict_get_string(d, "lhsrawalt", FALSE);
2680 rhs = dict_get_string(d, "rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002681 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002682 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002683 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002684 return;
2685 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002686 orig_rhs = rhs;
zeertzjq92a3d202022-08-31 16:40:17 +01002687 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
2688 rhs = (char_u *)"";
2689 else
2690 rhs = replace_termcodes(rhs, &arg_buf,
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002691 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002692
Bram Moolenaard61efa52022-07-23 09:52:04 +01002693 noremap = dict_get_number(d, "noremap") ? REMAP_NONE: 0;
2694 if (dict_get_number(d, "script") != 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002695 noremap = REMAP_SCRIPT;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002696 expr = dict_get_number(d, "expr") != 0;
2697 silent = dict_get_number(d, "silent") != 0;
2698 sid = dict_get_number(d, "sid");
2699 scriptversion = dict_get_number(d, "scriptversion");
2700 lnum = dict_get_number(d, "lnum");
2701 buffer = dict_get_number(d, "buffer");
2702 nowait = dict_get_number(d, "nowait") != 0;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002703 // mode from the dict is not used
2704
2705 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002706 {
2707 map_table = curbuf->b_maphash;
2708 abbr_table = &curbuf->b_first_abbr;
2709 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002710
2711 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002712 if (buffer)
2713 {
2714 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2715 if (arg == NULL)
2716 return;
2717 STRCPY(arg, "<buffer>");
2718 STRCPY(arg + 8, lhs);
2719 }
2720 else
2721 {
2722 arg = vim_strsave(lhs);
2723 if (arg == NULL)
2724 return;
2725 }
zeertzjq44068e92022-06-16 11:14:55 +01002726 do_map(MAPTYPE_UNMAP, arg, mode, is_abbr);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002727 vim_free(arg);
2728
Bram Moolenaar9c652532020-05-24 13:10:18 +02002729 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002730 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002731 if (lhsrawalt != NULL)
2732 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002733 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2734 lnum, 1);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002735 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002736}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002737#endif
2738
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002739
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002740#if defined(MSWIN) || defined(MACOS_X)
2741
Bram Moolenaar24959102022-05-07 20:01:16 +01002742# define VIS_SEL (MODE_VISUAL | MODE_SELECT) // abbreviation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002743
2744/*
2745 * Default mappings for some often used keys.
2746 */
2747struct initmap
2748{
2749 char_u *arg;
2750 int mode;
2751};
2752
2753# ifdef FEAT_GUI_MSWIN
2754// Use the Windows (CUA) keybindings. (GUI)
2755static struct initmap initmappings[] =
2756{
2757 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002758 {(char_u *)"<S-Insert> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002759 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002760 {(char_u *)"<S-Insert> <C-R><C-O>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002761 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2762 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2763 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2764 {(char_u *)"<C-X> \"*d", VIS_SEL},
2765 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2766};
2767# endif
2768
2769# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2770// Use the Windows (CUA) keybindings. (Console)
2771static struct initmap cinitmappings[] =
2772{
Bram Moolenaar24959102022-05-07 20:01:16 +01002773 {(char_u *)"\316w <C-Home>", MODE_NORMAL | VIS_SEL},
2774 {(char_u *)"\316w <C-Home>", MODE_INSERT | MODE_CMDLINE},
2775 {(char_u *)"\316u <C-End>", MODE_NORMAL | VIS_SEL},
2776 {(char_u *)"\316u <C-End>", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002777
2778 // paste, copy and cut
2779# ifdef FEAT_CLIPBOARD
Bram Moolenaar24959102022-05-07 20:01:16 +01002780 {(char_u *)"\316\324 \"*P", MODE_NORMAL}, // SHIFT-Insert is "*P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002781 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
Bram Moolenaar24959102022-05-07 20:01:16 +01002782 {(char_u *)"\316\324 \022\017*", MODE_INSERT}, // SHIFT-Insert is ^R^O*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002783 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2784 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2785 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2786 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2787# else
Bram Moolenaar24959102022-05-07 20:01:16 +01002788 {(char_u *)"\316\324 P", MODE_NORMAL}, // SHIFT-Insert is P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002789 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
Bram Moolenaar24959102022-05-07 20:01:16 +01002790 {(char_u *)"\316\324 \022\017\"", MODE_INSERT}, // SHIFT-Insert is ^R^O"
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002791 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2792 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2793 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2794# endif
2795};
2796# endif
2797
2798# if defined(MACOS_X)
2799static struct initmap initmappings[] =
2800{
2801 // Use the Standard MacOS binding.
2802 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002803 {(char_u *)"<D-v> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002804 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002805 {(char_u *)"<D-v> <C-R>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002806 {(char_u *)"<D-c> \"*y", VIS_SEL},
2807 {(char_u *)"<D-x> \"*d", VIS_SEL},
2808 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2809};
2810# endif
2811
2812# undef VIS_SEL
2813#endif
2814
2815/*
2816 * Set up default mappings.
2817 */
2818 void
2819init_mappings(void)
2820{
2821#if defined(MSWIN) || defined(MACOS_X)
2822 int i;
2823
2824# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2825# ifdef VIMDLL
2826 if (!gui.starting)
2827# endif
2828 {
K.Takataeeec2542021-06-02 13:28:16 +02002829 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002830 add_map(cinitmappings[i].arg, cinitmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002831 }
2832# endif
2833# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002834 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002835 add_map(initmappings[i].arg, initmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002836# endif
2837#endif
2838}
2839
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002840/*
2841 * Add a mapping "map" for mode "mode".
zeertzjq44068e92022-06-16 11:14:55 +01002842 * When "nore" is TRUE use MAPTYPE_NOREMAP.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002843 * Need to put string in allocated memory, because do_map() will modify it.
2844 */
2845 void
zeertzjq44068e92022-06-16 11:14:55 +01002846add_map(char_u *map, int mode, int nore)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002847{
2848 char_u *s;
2849 char_u *cpo_save = p_cpo;
2850
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002851 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002852 s = vim_strsave(map);
2853 if (s != NULL)
2854 {
zeertzjq44068e92022-06-16 11:14:55 +01002855 (void)do_map(nore ? MAPTYPE_NOREMAP : MAPTYPE_MAP, s, mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002856 vim_free(s);
2857 }
2858 p_cpo = cpo_save;
2859}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002860
Bram Moolenaare677df82019-09-02 22:31:11 +02002861#if defined(FEAT_LANGMAP) || defined(PROTO)
2862/*
2863 * Any character has an equivalent 'langmap' character. This is used for
2864 * keyboards that have a special language mode that sends characters above
2865 * 128 (although other characters can be translated too). The "to" field is a
2866 * Vim command character. This avoids having to switch the keyboard back to
2867 * ASCII mode when leaving Insert mode.
2868 *
2869 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2870 * commands.
2871 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2872 * same as langmap_mapchar[] for characters >= 256.
2873 *
2874 * Use growarray for 'langmap' chars >= 256
2875 */
2876typedef struct
2877{
2878 int from;
2879 int to;
2880} langmap_entry_T;
2881
2882static garray_T langmap_mapga;
2883
2884/*
2885 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2886 * field. If not found insert a new entry at the appropriate location.
2887 */
2888 static void
2889langmap_set_entry(int from, int to)
2890{
2891 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2892 int a = 0;
2893 int b = langmap_mapga.ga_len;
2894
2895 // Do a binary search for an existing entry.
2896 while (a != b)
2897 {
2898 int i = (a + b) / 2;
2899 int d = entries[i].from - from;
2900
2901 if (d == 0)
2902 {
2903 entries[i].to = to;
2904 return;
2905 }
2906 if (d < 0)
2907 a = i + 1;
2908 else
2909 b = i;
2910 }
2911
2912 if (ga_grow(&langmap_mapga, 1) != OK)
2913 return; // out of memory
2914
2915 // insert new entry at position "a"
2916 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2917 mch_memmove(entries + 1, entries,
2918 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2919 ++langmap_mapga.ga_len;
2920 entries[0].from = from;
2921 entries[0].to = to;
2922}
2923
2924/*
2925 * Apply 'langmap' to multi-byte character "c" and return the result.
2926 */
2927 int
2928langmap_adjust_mb(int c)
2929{
2930 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2931 int a = 0;
2932 int b = langmap_mapga.ga_len;
2933
2934 while (a != b)
2935 {
2936 int i = (a + b) / 2;
2937 int d = entries[i].from - c;
2938
2939 if (d == 0)
2940 return entries[i].to; // found matching entry
2941 if (d < 0)
2942 a = i + 1;
2943 else
2944 b = i;
2945 }
2946 return c; // no entry found, return "c" unmodified
2947}
2948
2949 void
2950langmap_init(void)
2951{
2952 int i;
2953
2954 for (i = 0; i < 256; i++)
2955 langmap_mapchar[i] = i; // we init with a one-to-one map
2956 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2957}
2958
2959/*
2960 * Called when langmap option is set; the language map can be
2961 * changed at any time!
2962 */
2963 void
2964langmap_set(void)
2965{
2966 char_u *p;
2967 char_u *p2;
2968 int from, to;
2969
2970 ga_clear(&langmap_mapga); // clear the previous map first
2971 langmap_init(); // back to one-to-one map
2972
2973 for (p = p_langmap; p[0] != NUL; )
2974 {
2975 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2976 MB_PTR_ADV(p2))
2977 {
2978 if (p2[0] == '\\' && p2[1] != NUL)
2979 ++p2;
2980 }
2981 if (p2[0] == ';')
2982 ++p2; // abcd;ABCD form, p2 points to A
2983 else
2984 p2 = NULL; // aAbBcCdD form, p2 is NULL
2985 while (p[0])
2986 {
2987 if (p[0] == ',')
2988 {
2989 ++p;
2990 break;
2991 }
2992 if (p[0] == '\\' && p[1] != NUL)
2993 ++p;
2994 from = (*mb_ptr2char)(p);
2995 to = NUL;
2996 if (p2 == NULL)
2997 {
2998 MB_PTR_ADV(p);
2999 if (p[0] != ',')
3000 {
3001 if (p[0] == '\\')
3002 ++p;
3003 to = (*mb_ptr2char)(p);
3004 }
3005 }
3006 else
3007 {
3008 if (p2[0] != ',')
3009 {
3010 if (p2[0] == '\\')
3011 ++p2;
3012 to = (*mb_ptr2char)(p2);
3013 }
3014 }
3015 if (to == NUL)
3016 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003017 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02003018 transchar(from));
3019 return;
3020 }
3021
3022 if (from >= 256)
3023 langmap_set_entry(from, to);
3024 else
3025 langmap_mapchar[from & 255] = to;
3026
3027 // Advance to next pair
3028 MB_PTR_ADV(p);
3029 if (p2 != NULL)
3030 {
3031 MB_PTR_ADV(p2);
3032 if (*p == ';')
3033 {
3034 p = p2;
3035 if (p[0] != NUL)
3036 {
3037 if (p[0] != ',')
3038 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003039 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02003040 return;
3041 }
3042 ++p;
3043 }
3044 break;
3045 }
3046 }
3047 }
3048 }
3049}
3050#endif
3051
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003052 static void
3053do_exmap(exarg_T *eap, int isabbrev)
3054{
3055 int mode;
3056 char_u *cmdp;
3057
3058 cmdp = eap->cmd;
3059 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
3060
zeertzjq44068e92022-06-16 11:14:55 +01003061 switch (do_map(*cmdp == 'n' ? MAPTYPE_NOREMAP
3062 : *cmdp == 'u' ? MAPTYPE_UNMAP : MAPTYPE_MAP,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003063 eap->arg, mode, isabbrev))
3064 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003065 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003066 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003067 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
3068 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003069 break;
3070 }
3071}
3072
3073/*
3074 * ":abbreviate" and friends.
3075 */
3076 void
3077ex_abbreviate(exarg_T *eap)
3078{
3079 do_exmap(eap, TRUE); // almost the same as mapping
3080}
3081
3082/*
3083 * ":map" and friends.
3084 */
3085 void
3086ex_map(exarg_T *eap)
3087{
3088 // If we are sourcing .exrc or .vimrc in current directory we
3089 // print the mappings for security reasons.
3090 if (secure)
3091 {
3092 secure = 2;
3093 msg_outtrans(eap->cmd);
3094 msg_putchar('\n');
3095 }
3096 do_exmap(eap, FALSE);
3097}
3098
3099/*
3100 * ":unmap" and friends.
3101 */
3102 void
3103ex_unmap(exarg_T *eap)
3104{
3105 do_exmap(eap, FALSE);
3106}
3107
3108/*
3109 * ":mapclear" and friends.
3110 */
3111 void
3112ex_mapclear(exarg_T *eap)
3113{
3114 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
3115}
3116
3117/*
3118 * ":abclear" and friends.
3119 */
3120 void
3121ex_abclear(exarg_T *eap)
3122{
3123 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
3124}