blob: e99da373b70e8e9b231f310b492811ad13b1b9ae [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
139 static void
140showmap(
141 mapblock_T *mp,
142 int local) // TRUE for buffer-local map
143{
144 int len = 1;
145 char_u *mapchars;
146
147 if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
148 return;
149
150 if (msg_didout || msg_silent != 0)
151 {
152 msg_putchar('\n');
153 if (got_int) // 'q' typed at MORE prompt
154 return;
155 }
156
157 mapchars = map_mode_to_chars(mp->m_mode);
158 if (mapchars != NULL)
159 {
160 msg_puts((char *)mapchars);
161 len = (int)STRLEN(mapchars);
162 vim_free(mapchars);
163 }
164
165 while (++len <= 3)
166 msg_putchar(' ');
167
168 // Display the LHS. Get length of what we write.
169 len = msg_outtrans_special(mp->m_keys, TRUE, 0);
170 do
171 {
172 msg_putchar(' '); // padd with blanks
173 ++len;
174 } while (len < 12);
175
176 if (mp->m_noremap == REMAP_NONE)
177 msg_puts_attr("*", HL_ATTR(HLF_8));
178 else if (mp->m_noremap == REMAP_SCRIPT)
179 msg_puts_attr("&", HL_ATTR(HLF_8));
180 else
181 msg_putchar(' ');
182
183 if (local)
184 msg_putchar('@');
185 else
186 msg_putchar(' ');
187
188 // Use FALSE below if we only want things like <Up> to show up as such on
189 // the rhs, and not M-x etc, TRUE gets both -- webb
190 if (*mp->m_str == NUL)
191 msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
192 else
zeertzjqac402f42022-05-04 18:51:43 +0100193 msg_outtrans_special(mp->m_str, FALSE, 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200194#ifdef FEAT_EVAL
195 if (p_verbose > 0)
196 last_set_msg(mp->m_script_ctx);
197#endif
Bram Moolenaard288eaa2022-02-16 18:27:55 +0000198 msg_clr_eos();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200199 out_flush(); // show one line at a time
200}
201
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200202 static int
203map_add(
204 mapblock_T **map_table,
205 mapblock_T **abbr_table,
206 char_u *keys,
207 char_u *rhs,
208 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200209 int noremap,
210 int nowait,
211 int silent,
212 int mode,
213 int is_abbr,
214#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200215 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200216 scid_T sid, // -1 to use current_sctx
Bram Moolenaara9528b32022-01-18 20:51:35 +0000217 int scriptversion,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200218 linenr_T lnum,
219#endif
220 int simplified)
221{
Bram Moolenaar94075b22022-01-18 20:30:39 +0000222 mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200223
224 if (mp == NULL)
225 return FAIL;
226
227 // If CTRL-C has been mapped, don't always use it for Interrupting.
228 if (*keys == Ctrl_C)
229 {
230 if (map_table == curbuf->b_maphash)
231 curbuf->b_mapped_ctrl_c |= mode;
232 else
233 mapped_ctrl_c |= mode;
234 }
235
236 mp->m_keys = vim_strsave(keys);
237 mp->m_str = vim_strsave(rhs);
238 mp->m_orig_str = vim_strsave(orig_rhs);
239 if (mp->m_keys == NULL || mp->m_str == NULL)
240 {
241 vim_free(mp->m_keys);
242 vim_free(mp->m_str);
243 vim_free(mp->m_orig_str);
244 vim_free(mp);
245 return FAIL;
246 }
247 mp->m_keylen = (int)STRLEN(mp->m_keys);
248 mp->m_noremap = noremap;
249 mp->m_nowait = nowait;
250 mp->m_silent = silent;
251 mp->m_mode = mode;
252 mp->m_simplified = simplified;
253#ifdef FEAT_EVAL
254 mp->m_expr = expr;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000255 if (sid > 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200256 {
257 mp->m_script_ctx.sc_sid = sid;
258 mp->m_script_ctx.sc_lnum = lnum;
Bram Moolenaara9528b32022-01-18 20:51:35 +0000259 mp->m_script_ctx.sc_version = scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200260 }
261 else
262 {
263 mp->m_script_ctx = current_sctx;
264 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
265 }
266#endif
267
268 // add the new entry in front of the abbrlist or maphash[] list
269 if (is_abbr)
270 {
271 mp->m_next = *abbr_table;
272 *abbr_table = mp;
273 }
274 else
275 {
276 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
277
278 mp->m_next = map_table[n];
279 map_table[n] = mp;
280 }
281 return OK;
282}
283
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200284/*
285 * map[!] : show all key mappings
286 * map[!] {lhs} : show key mapping for {lhs}
287 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
288 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
289 * unmap[!] {lhs} : remove key mapping for {lhs}
290 * abbr : show all abbreviations
291 * abbr {lhs} : show abbreviations for {lhs}
292 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
293 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
294 * unabbr {lhs} : remove abbreviation for {lhs}
295 *
zeertzjq44068e92022-06-16 11:14:55 +0100296 * maptype: MAPTYPE_MAP for :map
297 * MAPTYPE_UNMAP for :unmap
298 * MAPTYPE_NOREMAP for noremap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200299 *
300 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
301 * it will be modified.
302 *
Bram Moolenaar24959102022-05-07 20:01:16 +0100303 * for :map mode is MODE_NORMAL | MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING
304 * for :map! mode is MODE_INSERT | MODE_CMDLINE
305 * for :cmap mode is MODE_CMDLINE
306 * for :imap mode is MODE_INSERT
307 * for :lmap mode is MODE_LANGMAP
308 * for :nmap mode is MODE_NORMAL
309 * for :vmap mode is MODE_VISUAL | MODE_SELECT
310 * for :xmap mode is MODE_VISUAL
311 * for :smap mode is MODE_SELECT
312 * for :omap mode is MODE_OP_PENDING
313 * for :tmap mode is MODE_TERMINAL
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200314 *
Bram Moolenaar24959102022-05-07 20:01:16 +0100315 * for :abbr mode is MODE_INSERT | MODE_CMDLINE
316 * for :iabbr mode is MODE_INSERT
317 * for :cabbr mode is MODE_CMDLINE
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200318 *
319 * Return 0 for success
320 * 1 for invalid arguments
321 * 2 for no match
322 * 4 for out of mem
323 * 5 for entry not unique
324 */
325 int
326do_map(
327 int maptype,
328 char_u *arg,
329 int mode,
330 int abbrev) // not a mapping but an abbreviation
331{
332 char_u *keys;
333 mapblock_T *mp, **mpp;
334 char_u *rhs;
335 char_u *p;
336 int n;
337 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200338 int hasarg;
339 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200340 int do_print;
341 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200342 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200343 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200344 char_u *arg_buf = NULL;
345 int retval = 0;
346 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200347 mapblock_T **abbr_table;
348 mapblock_T **map_table;
349 int unique = FALSE;
350 int nowait = FALSE;
351 int silent = FALSE;
352 int special = FALSE;
353#ifdef FEAT_EVAL
354 int expr = FALSE;
355#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200356 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200357 int noremap;
358 char_u *orig_rhs;
359
360 keys = arg;
361 map_table = maphash;
362 abbr_table = &first_abbr;
363
364 // For ":noremap" don't remap, otherwise do remap.
zeertzjq44068e92022-06-16 11:14:55 +0100365 if (maptype == MAPTYPE_NOREMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200366 noremap = REMAP_NONE;
367 else
368 noremap = REMAP_YES;
369
370 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
371 // any order.
372 for (;;)
373 {
374 // Check for "<buffer>": mapping local to buffer.
375 if (STRNCMP(keys, "<buffer>", 8) == 0)
376 {
377 keys = skipwhite(keys + 8);
378 map_table = curbuf->b_maphash;
379 abbr_table = &curbuf->b_first_abbr;
380 continue;
381 }
382
383 // Check for "<nowait>": don't wait for more characters.
384 if (STRNCMP(keys, "<nowait>", 8) == 0)
385 {
386 keys = skipwhite(keys + 8);
387 nowait = TRUE;
388 continue;
389 }
390
391 // Check for "<silent>": don't echo commands.
392 if (STRNCMP(keys, "<silent>", 8) == 0)
393 {
394 keys = skipwhite(keys + 8);
395 silent = TRUE;
396 continue;
397 }
398
399 // Check for "<special>": accept special keys in <>
400 if (STRNCMP(keys, "<special>", 9) == 0)
401 {
402 keys = skipwhite(keys + 9);
403 special = TRUE;
404 continue;
405 }
406
407#ifdef FEAT_EVAL
408 // Check for "<script>": remap script-local mappings only
409 if (STRNCMP(keys, "<script>", 8) == 0)
410 {
411 keys = skipwhite(keys + 8);
412 noremap = REMAP_SCRIPT;
413 continue;
414 }
415
416 // Check for "<expr>": {rhs} is an expression.
417 if (STRNCMP(keys, "<expr>", 6) == 0)
418 {
419 keys = skipwhite(keys + 6);
420 expr = TRUE;
421 continue;
422 }
423#endif
424 // Check for "<unique>": don't overwrite an existing mapping.
425 if (STRNCMP(keys, "<unique>", 8) == 0)
426 {
427 keys = skipwhite(keys + 8);
428 unique = TRUE;
429 continue;
430 }
431 break;
432 }
433
434 validate_maphash();
435
436 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
437 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
438 // with :unmap white space is included in the keys, no argument possible.
439 p = keys;
440 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
zeertzjq44068e92022-06-16 11:14:55 +0100441 while (*p && (maptype == MAPTYPE_UNMAP || !VIM_ISWHITE(*p)))
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200442 {
443 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
444 p[1] != NUL)
445 ++p; // skip CTRL-V or backslash
446 ++p;
447 }
448 if (*p != NUL)
449 *p++ = NUL;
450
451 p = skipwhite(p);
452 rhs = p;
453 hasarg = (*rhs != NUL);
454 haskey = (*keys != NUL);
zeertzjq44068e92022-06-16 11:14:55 +0100455 do_print = !haskey || (maptype != MAPTYPE_UNMAP && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200456
457 // check for :unmap without argument
zeertzjq44068e92022-06-16 11:14:55 +0100458 if (maptype == MAPTYPE_UNMAP && !haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200459 {
460 retval = 1;
461 goto theend;
462 }
463
464 // If mapping has been given as ^V<C_UP> say, then replace the term codes
465 // with the appropriate two bytes. If it is a shifted special key, unshift
466 // it too, giving another two bytes.
467 // replace_termcodes() may move the result to allocated memory, which
468 // needs to be freed later (*keys_buf and *arg_buf).
469 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200470 // If something like <C-H> is simplified to 0x08 then mark it as simplified
471 // and also add a n entry with a modifier, which will work when
472 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200473 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200474 {
475 char_u *new_keys;
476 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
477
478 if (special)
479 flags |= REPTERM_SPECIAL;
480 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
481 if (did_simplify)
482 (void)replace_termcodes(keys, &alt_keys_buf,
483 flags | REPTERM_NO_SIMPLIFY, NULL);
484 keys = new_keys;
485 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200486 orig_rhs = rhs;
487 if (hasarg)
488 {
489 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
490 rhs = (char_u *)"";
491 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200492 rhs = replace_termcodes(rhs, &arg_buf,
493 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200494 }
495
Bram Moolenaar459fd782019-10-13 16:43:39 +0200496 /*
497 * The following is done twice if we have two versions of keys:
498 * "alt_keys_buf" is not NULL.
499 */
500 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200501 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200502 int did_it = FALSE;
503 int did_local = FALSE;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100504 int keyround1_simplified = keyround == 1 && did_simplify;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200505 int round;
506 int hash;
507 int new_hash;
508
509 if (keyround == 2)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200510 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200511 if (alt_keys_buf == NULL)
512 break;
513 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200514 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200515 else if (alt_keys_buf != NULL && do_print)
516 // when printing always use the not-simplified map
517 keys = alt_keys_buf;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200518
Bram Moolenaar459fd782019-10-13 16:43:39 +0200519 // check arguments and translate function keys
520 if (haskey)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200521 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200522 len = (int)STRLEN(keys);
523 if (len > MAXMAPLEN) // maximum length of MAXMAPLEN chars
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200524 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200525 retval = 1;
526 goto theend;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200527 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200528
zeertzjq44068e92022-06-16 11:14:55 +0100529 if (abbrev && maptype != MAPTYPE_UNMAP)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200530 {
531 // If an abbreviation ends in a keyword character, the
532 // rest must be all keyword-char or all non-keyword-char.
533 // Otherwise we won't be able to find the start of it in a
534 // vi-compatible way.
535 if (has_mbyte)
536 {
537 int first, last;
538 int same = -1;
539
540 first = vim_iswordp(keys);
541 last = first;
542 p = keys + (*mb_ptr2len)(keys);
543 n = 1;
544 while (p < keys + len)
545 {
546 ++n; // nr of (multi-byte) chars
547 last = vim_iswordp(p); // type of last char
548 if (same == -1 && last != first)
549 same = n - 1; // count of same char type
550 p += (*mb_ptr2len)(p);
551 }
552 if (last && n > 2 && same >= 0 && same < n - 1)
553 {
554 retval = 1;
555 goto theend;
556 }
557 }
558 else if (vim_iswordc(keys[len - 1]))
559 // ends in keyword char
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200560 for (n = 0; n < len - 2; ++n)
561 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
562 {
563 retval = 1;
564 goto theend;
565 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200566 // An abbreviation cannot contain white space.
567 for (n = 0; n < len; ++n)
568 if (VIM_ISWHITE(keys[n]))
569 {
570 retval = 1;
571 goto theend;
572 }
573 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200574 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200575
Bram Moolenaar459fd782019-10-13 16:43:39 +0200576 if (haskey && hasarg && abbrev) // if we will add an abbreviation
577 no_abbr = FALSE; // reset flag that indicates there are
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200578 // no abbreviations
579
Bram Moolenaar459fd782019-10-13 16:43:39 +0200580 if (do_print)
581 msg_start();
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200582
Bram Moolenaar459fd782019-10-13 16:43:39 +0200583 // Check if a new local mapping wasn't already defined globally.
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200584 if (unique && map_table == curbuf->b_maphash
zeertzjq44068e92022-06-16 11:14:55 +0100585 && haskey && hasarg && maptype != MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200586 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200587 // need to loop over all global hash lists
588 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200589 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200590 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200591 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200592 if (hash != 0) // there is only one abbreviation list
593 break;
594 mp = first_abbr;
595 }
596 else
597 mp = maphash[hash];
598 for ( ; mp != NULL && !got_int; mp = mp->m_next)
599 {
600 // check entries with the same mode
601 if ((mp->m_mode & mode) != 0
602 && mp->m_keylen == len
Bram Moolenaar459fd782019-10-13 16:43:39 +0200603 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
604 {
605 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000606 semsg(
607 _(e_global_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200608 mp->m_keys);
609 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000610 semsg(_(e_global_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200611 mp->m_keys);
612 retval = 5;
613 goto theend;
614 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200615 }
616 }
617 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200618
Bram Moolenaar459fd782019-10-13 16:43:39 +0200619 // When listing global mappings, also list buffer-local ones here.
zeertzjq44068e92022-06-16 11:14:55 +0100620 if (map_table != curbuf->b_maphash && !hasarg
621 && maptype != MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200622 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200623 // need to loop over all global hash lists
624 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200625 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200626 if (abbrev)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200627 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200628 if (hash != 0) // there is only one abbreviation list
629 break;
630 mp = curbuf->b_first_abbr;
631 }
632 else
633 mp = curbuf->b_maphash[hash];
634 for ( ; mp != NULL && !got_int; mp = mp->m_next)
635 {
636 // check entries with the same mode
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200637 if (!mp->m_simplified && (mp->m_mode & mode) != 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200638 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200639 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200640 {
641 showmap(mp, TRUE);
642 did_local = TRUE;
643 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200644 else
645 {
646 n = mp->m_keylen;
647 if (STRNCMP(mp->m_keys, keys,
648 (size_t)(n < len ? n : len)) == 0)
649 {
650 showmap(mp, TRUE);
651 did_local = TRUE;
652 }
653 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200654 }
655 }
656 }
657 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200658
Bram Moolenaar459fd782019-10-13 16:43:39 +0200659 // Find an entry in the maphash[] list that matches.
660 // For :unmap we may loop two times: once to try to unmap an entry with
661 // a matching 'from' part, a second time, if the first fails, to unmap
zeertzjqa3f83fe2021-11-22 12:47:39 +0000662 // an entry with a matching 'to' part. This was done to allow
663 // ":ab foo bar" to be unmapped by typing ":unab foo", where "foo" will
664 // be replaced by "bar" because of the abbreviation.
zeertzjq44068e92022-06-16 11:14:55 +0100665 for (round = 0; (round == 0 || maptype == MAPTYPE_UNMAP) && round <= 1
Bram Moolenaar459fd782019-10-13 16:43:39 +0200666 && !did_it && !got_int; ++round)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200667 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200668 // need to loop over all hash lists
669 for (hash = 0; hash < 256 && !got_int; ++hash)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200670 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200671 if (abbrev)
672 {
673 if (hash > 0) // there is only one abbreviation list
674 break;
675 mpp = abbr_table;
676 }
677 else
678 mpp = &(map_table[hash]);
679 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
680 {
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200681
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200682 if ((mp->m_mode & mode) == 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200683 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200684 // skip entries with wrong mode
Bram Moolenaar459fd782019-10-13 16:43:39 +0200685 mpp = &(mp->m_next);
686 continue;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200687 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200688 if (!haskey) // show all entries
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200689 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200690 if (!mp->m_simplified)
691 {
692 showmap(mp, map_table != maphash);
693 did_it = TRUE;
694 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200695 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200696 else // do we have a match?
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200697 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200698 if (round) // second round: Try unmap "rhs" string
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200699 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200700 n = (int)STRLEN(mp->m_str);
701 p = mp->m_str;
702 }
703 else
704 {
705 n = mp->m_keylen;
706 p = mp->m_keys;
707 }
708 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
709 {
zeertzjq44068e92022-06-16 11:14:55 +0100710 if (maptype == MAPTYPE_UNMAP)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200711 {
712 // Delete entry.
713 // Only accept a full match. For abbreviations
714 // we ignore trailing space when matching with
715 // the "lhs", since an abbreviation can't have
716 // trailing space.
717 if (n != len && (!abbrev || round || n > len
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200718 || *skipwhite(keys + n) != NUL))
Bram Moolenaar459fd782019-10-13 16:43:39 +0200719 {
720 mpp = &(mp->m_next);
721 continue;
722 }
zeertzjqabeb09b2022-04-26 12:29:43 +0100723 // In keyround for simplified keys, don't unmap
724 // a mapping without m_simplified flag.
Bram Moolenaar87f74102022-04-25 18:59:25 +0100725 if (keyround1_simplified && !mp->m_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100726 break;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200727 // We reset the indicated mode bits. If nothing
728 // is left the entry is deleted below.
729 mp->m_mode &= ~mode;
730 did_it = TRUE; // remember we did something
731 }
732 else if (!hasarg) // show matching entry
733 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200734 if (!mp->m_simplified)
735 {
736 showmap(mp, map_table != maphash);
737 did_it = TRUE;
738 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200739 }
740 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200741 {
742 mpp = &(mp->m_next);
743 continue;
744 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200745 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200746 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200747 if (abbrev)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000748 semsg(
749 _(e_abbreviation_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200750 p);
751 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000752 semsg(_(e_mapping_already_exists_for_str),
Bram Moolenaar459fd782019-10-13 16:43:39 +0200753 p);
754 retval = 5;
755 goto theend;
756 }
757 else
758 {
759 // new rhs for existing entry
760 mp->m_mode &= ~mode; // remove mode bits
761 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200762 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200763 char_u *newstr = vim_strsave(rhs);
764
765 if (newstr == NULL)
766 {
767 retval = 4; // no mem
768 goto theend;
769 }
770 vim_free(mp->m_str);
771 mp->m_str = newstr;
772 vim_free(mp->m_orig_str);
773 mp->m_orig_str = vim_strsave(orig_rhs);
774 mp->m_noremap = noremap;
775 mp->m_nowait = nowait;
776 mp->m_silent = silent;
777 mp->m_mode = mode;
Bram Moolenaar87f74102022-04-25 18:59:25 +0100778 mp->m_simplified = keyround1_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200779#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200780 mp->m_expr = expr;
781 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100782 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200783#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200784 did_it = TRUE;
785 }
786 }
787 if (mp->m_mode == 0) // entry can be deleted
788 {
789 map_free(mpp);
790 continue; // continue with *mpp
791 }
792
793 // May need to put this entry into another hash
794 // list.
795 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
796 if (!abbrev && new_hash != hash)
797 {
798 *mpp = mp->m_next;
799 mp->m_next = map_table[new_hash];
800 map_table[new_hash] = mp;
801
802 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200803 }
804 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200805 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200806 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200807 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200808 }
809 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200810
zeertzjq44068e92022-06-16 11:14:55 +0100811 if (maptype == MAPTYPE_UNMAP)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200812 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200813 // delete entry
814 if (!did_it)
zeertzjqa4e33322022-04-24 17:07:53 +0100815 {
Bram Moolenaar87f74102022-04-25 18:59:25 +0100816 if (!keyround1_simplified)
zeertzjqa4e33322022-04-24 17:07:53 +0100817 retval = 2; // no match
818 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200819 else if (*keys == Ctrl_C)
820 {
821 // If CTRL-C has been unmapped, reuse it for Interrupting.
822 if (map_table == curbuf->b_maphash)
823 curbuf->b_mapped_ctrl_c &= ~mode;
824 else
825 mapped_ctrl_c &= ~mode;
826 }
827 continue;
828 }
829
830 if (!haskey || !hasarg)
831 {
832 // print entries
833 if (!did_it && !did_local)
834 {
835 if (abbrev)
836 msg(_("No abbreviation found"));
837 else
838 msg(_("No mapping found"));
839 }
840 goto theend; // listing finished
841 }
842
843 if (did_it)
844 continue; // have added the new entry already
845
846 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200847 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
848 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200849#ifdef FEAT_EVAL
Bram Moolenaara9528b32022-01-18 20:51:35 +0000850 expr, /* sid */ -1, /* scriptversion */ 0, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200851#endif
Bram Moolenaar87f74102022-04-25 18:59:25 +0100852 keyround1_simplified) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200853 {
854 retval = 4; // no mem
855 goto theend;
856 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200857 }
858
859theend:
860 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200861 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200862 vim_free(arg_buf);
863 return retval;
864}
865
866/*
867 * Get the mapping mode from the command name.
868 */
869 static int
870get_map_mode(char_u **cmdp, int forceit)
871{
872 char_u *p;
873 int modec;
874 int mode;
875
876 p = *cmdp;
877 modec = *p++;
878 if (modec == 'i')
Bram Moolenaar24959102022-05-07 20:01:16 +0100879 mode = MODE_INSERT; // :imap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200880 else if (modec == 'l')
Bram Moolenaar24959102022-05-07 20:01:16 +0100881 mode = MODE_LANGMAP; // :lmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200882 else if (modec == 'c')
Bram Moolenaar24959102022-05-07 20:01:16 +0100883 mode = MODE_CMDLINE; // :cmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200884 else if (modec == 'n' && *p != 'o') // avoid :noremap
Bram Moolenaar24959102022-05-07 20:01:16 +0100885 mode = MODE_NORMAL; // :nmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200886 else if (modec == 'v')
Bram Moolenaar24959102022-05-07 20:01:16 +0100887 mode = MODE_VISUAL | MODE_SELECT; // :vmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200888 else if (modec == 'x')
Bram Moolenaar24959102022-05-07 20:01:16 +0100889 mode = MODE_VISUAL; // :xmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200890 else if (modec == 's')
Bram Moolenaar24959102022-05-07 20:01:16 +0100891 mode = MODE_SELECT; // :smap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200892 else if (modec == 'o')
Bram Moolenaar24959102022-05-07 20:01:16 +0100893 mode = MODE_OP_PENDING; // :omap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200894 else if (modec == 't')
Bram Moolenaar24959102022-05-07 20:01:16 +0100895 mode = MODE_TERMINAL; // :tmap
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200896 else
897 {
898 --p;
899 if (forceit)
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 mode = MODE_INSERT | MODE_CMDLINE; // :map !
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200901 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100902 mode = MODE_VISUAL | MODE_SELECT | MODE_NORMAL | MODE_OP_PENDING;
903 // :map
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200904 }
905
906 *cmdp = p;
907 return mode;
908}
909
910/*
zeertzjqc207fd22022-06-29 10:37:40 +0100911 * Clear all mappings (":mapclear") or abbreviations (":abclear").
912 * "abbr" should be FALSE for mappings, TRUE for abbreviations.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200913 */
914 static void
915map_clear(
916 char_u *cmdp,
zeertzjqc207fd22022-06-29 10:37:40 +0100917 char_u *arg,
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200918 int forceit,
919 int abbr)
920{
921 int mode;
922 int local;
923
924 local = (STRCMP(arg, "<buffer>") == 0);
925 if (!local && *arg != NUL)
926 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000927 emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200928 return;
929 }
930
931 mode = get_map_mode(&cmdp, forceit);
zeertzjqc207fd22022-06-29 10:37:40 +0100932 map_clear_mode(curbuf, mode, local, abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200933}
934
935/*
936 * Clear all mappings in "mode".
937 */
938 void
zeertzjqc207fd22022-06-29 10:37:40 +0100939map_clear_mode(
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200940 buf_T *buf, // buffer for local mappings
941 int mode, // mode in which to delete
942 int local, // TRUE for buffer-local mappings
943 int abbr) // TRUE for abbreviations
944{
945 mapblock_T *mp, **mpp;
946 int hash;
947 int new_hash;
948
949 validate_maphash();
950
951 for (hash = 0; hash < 256; ++hash)
952 {
953 if (abbr)
954 {
955 if (hash > 0) // there is only one abbrlist
956 break;
957 if (local)
958 mpp = &buf->b_first_abbr;
959 else
960 mpp = &first_abbr;
961 }
962 else
963 {
964 if (local)
965 mpp = &buf->b_maphash[hash];
966 else
967 mpp = &maphash[hash];
968 }
969 while (*mpp != NULL)
970 {
971 mp = *mpp;
972 if (mp->m_mode & mode)
973 {
974 mp->m_mode &= ~mode;
975 if (mp->m_mode == 0) // entry can be deleted
976 {
977 map_free(mpp);
978 continue;
979 }
980 // May need to put this entry into another hash list.
981 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
982 if (!abbr && new_hash != hash)
983 {
984 *mpp = mp->m_next;
985 if (local)
986 {
987 mp->m_next = buf->b_maphash[new_hash];
988 buf->b_maphash[new_hash] = mp;
989 }
990 else
991 {
992 mp->m_next = maphash[new_hash];
993 maphash[new_hash] = mp;
994 }
995 continue; // continue with *mpp
996 }
997 }
998 mpp = &(mp->m_next);
999 }
1000 }
1001}
1002
1003#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001004 int
Bram Moolenaar581ba392019-09-03 22:08:33 +02001005mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001006{
1007 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001008
1009 if (vim_strchr(modechars, 'n') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001010 mode |= MODE_NORMAL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001011 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001012 mode |= MODE_VISUAL | MODE_SELECT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001013 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001014 mode |= MODE_VISUAL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001015 if (vim_strchr(modechars, 's') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001016 mode |= MODE_SELECT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001017 if (vim_strchr(modechars, 'o') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001018 mode |= MODE_OP_PENDING;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001019 if (vim_strchr(modechars, 'i') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001020 mode |= MODE_INSERT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001021 if (vim_strchr(modechars, 'l') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001022 mode |= MODE_LANGMAP;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001023 if (vim_strchr(modechars, 'c') != NULL)
Bram Moolenaar24959102022-05-07 20:01:16 +01001024 mode |= MODE_CMDLINE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001025
Bram Moolenaar581ba392019-09-03 22:08:33 +02001026 return mode;
1027}
1028
1029/*
1030 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1031 * Recognize termcap codes in "str".
1032 * Also checks mappings local to the current buffer.
1033 */
1034 int
1035map_to_exists(char_u *str, char_u *modechars, int abbr)
1036{
1037 char_u *rhs;
1038 char_u *buf;
1039 int retval;
1040
Bram Moolenaar459fd782019-10-13 16:43:39 +02001041 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001042
1043 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001044 vim_free(buf);
1045
1046 return retval;
1047}
1048#endif
1049
1050/*
1051 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1052 * Also checks mappings local to the current buffer.
1053 */
1054 int
1055map_to_exists_mode(char_u *rhs, int mode, int abbr)
1056{
1057 mapblock_T *mp;
1058 int hash;
1059 int exp_buffer = FALSE;
1060
1061 validate_maphash();
1062
1063 // Do it twice: once for global maps and once for local maps.
1064 for (;;)
1065 {
1066 for (hash = 0; hash < 256; ++hash)
1067 {
1068 if (abbr)
1069 {
1070 if (hash > 0) // there is only one abbr list
1071 break;
1072 if (exp_buffer)
1073 mp = curbuf->b_first_abbr;
1074 else
1075 mp = first_abbr;
1076 }
1077 else if (exp_buffer)
1078 mp = curbuf->b_maphash[hash];
1079 else
1080 mp = maphash[hash];
1081 for (; mp; mp = mp->m_next)
1082 {
1083 if ((mp->m_mode & mode)
1084 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1085 return TRUE;
1086 }
1087 }
1088 if (exp_buffer)
1089 break;
1090 exp_buffer = TRUE;
1091 }
1092
1093 return FALSE;
1094}
1095
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001096/*
1097 * Used below when expanding mapping/abbreviation names.
1098 */
1099static int expand_mapmodes = 0;
1100static int expand_isabbrev = 0;
1101static int expand_buffer = FALSE;
1102
1103/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001104 * Translate an internal mapping/abbreviation representation into the
1105 * corresponding external one recognized by :map/:abbrev commands.
1106 * Respects the current B/k/< settings of 'cpoption'.
1107 *
1108 * This function is called when expanding mappings/abbreviations on the
1109 * command-line.
1110 *
1111 * It uses a growarray to build the translation string since the latter can be
1112 * wider than the original description. The caller has to free the string
1113 * afterwards.
1114 *
1115 * Returns NULL when there is a problem.
1116 */
1117 static char_u *
1118translate_mapping(char_u *str)
1119{
1120 garray_T ga;
1121 int c;
1122 int modifiers;
1123 int cpo_bslash;
1124 int cpo_special;
1125
1126 ga_init(&ga);
1127 ga.ga_itemsize = 1;
1128 ga.ga_growsize = 40;
1129
1130 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1131 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1132
1133 for (; *str; ++str)
1134 {
1135 c = *str;
1136 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1137 {
1138 modifiers = 0;
1139 if (str[1] == KS_MODIFIER)
1140 {
1141 str++;
1142 modifiers = *++str;
1143 c = *++str;
1144 }
1145 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1146 {
1147 if (cpo_special)
1148 {
1149 ga_clear(&ga);
1150 return NULL;
1151 }
1152 c = TO_SPECIAL(str[1], str[2]);
1153 if (c == K_ZERO) // display <Nul> as ^@
1154 c = NUL;
1155 str += 2;
1156 }
1157 if (IS_SPECIAL(c) || modifiers) // special key
1158 {
1159 if (cpo_special)
1160 {
1161 ga_clear(&ga);
1162 return NULL;
1163 }
1164 ga_concat(&ga, get_special_key_name(c, modifiers));
1165 continue; // for (str)
1166 }
1167 }
1168 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1169 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1170 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1171 if (c)
1172 ga_append(&ga, c);
1173 }
1174 ga_append(&ga, NUL);
1175 return (char_u *)(ga.ga_data);
1176}
1177
1178/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001179 * Work out what to complete when doing command line completion of mapping
1180 * or abbreviation names.
1181 */
1182 char_u *
1183set_context_in_map_cmd(
1184 expand_T *xp,
1185 char_u *cmd,
1186 char_u *arg,
1187 int forceit, // TRUE if '!' given
1188 int isabbrev, // TRUE if abbreviation
1189 int isunmap, // TRUE if unmap/unabbrev command
1190 cmdidx_T cmdidx)
1191{
1192 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1193 xp->xp_context = EXPAND_NOTHING;
1194 else
1195 {
1196 if (isunmap)
1197 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1198 else
1199 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001200 expand_mapmodes = MODE_INSERT | MODE_CMDLINE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001201 if (!isabbrev)
Bram Moolenaar24959102022-05-07 20:01:16 +01001202 expand_mapmodes += MODE_VISUAL | MODE_SELECT | MODE_NORMAL
1203 | MODE_OP_PENDING;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001204 }
1205 expand_isabbrev = isabbrev;
1206 xp->xp_context = EXPAND_MAPPINGS;
1207 expand_buffer = FALSE;
1208 for (;;)
1209 {
1210 if (STRNCMP(arg, "<buffer>", 8) == 0)
1211 {
1212 expand_buffer = TRUE;
1213 arg = skipwhite(arg + 8);
1214 continue;
1215 }
1216 if (STRNCMP(arg, "<unique>", 8) == 0)
1217 {
1218 arg = skipwhite(arg + 8);
1219 continue;
1220 }
1221 if (STRNCMP(arg, "<nowait>", 8) == 0)
1222 {
1223 arg = skipwhite(arg + 8);
1224 continue;
1225 }
1226 if (STRNCMP(arg, "<silent>", 8) == 0)
1227 {
1228 arg = skipwhite(arg + 8);
1229 continue;
1230 }
1231 if (STRNCMP(arg, "<special>", 9) == 0)
1232 {
1233 arg = skipwhite(arg + 9);
1234 continue;
1235 }
1236#ifdef FEAT_EVAL
1237 if (STRNCMP(arg, "<script>", 8) == 0)
1238 {
1239 arg = skipwhite(arg + 8);
1240 continue;
1241 }
1242 if (STRNCMP(arg, "<expr>", 6) == 0)
1243 {
1244 arg = skipwhite(arg + 6);
1245 continue;
1246 }
1247#endif
1248 break;
1249 }
1250 xp->xp_pattern = arg;
1251 }
1252
1253 return NULL;
1254}
1255
1256/*
1257 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1258 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1259 * Return OK if matches found, FAIL otherwise.
1260 */
1261 int
1262ExpandMappings(
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001263 char_u *pat,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001264 regmatch_T *regmatch,
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001265 int *numMatches,
1266 char_u ***matches)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001267{
1268 mapblock_T *mp;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001269 garray_T ga;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001270 int hash;
1271 int count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001272 char_u *p;
1273 int i;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001274 int fuzzy;
1275 int match;
Yasuhiro Matsumoto09f68a52022-06-18 16:48:36 +01001276 int score = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001277 fuzmatch_str_T *fuzmatch;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001278
1279 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001280
1281 validate_maphash();
1282
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001283 *numMatches = 0; // return values in case of FAIL
1284 *matches = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001285
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001286 if (!fuzzy)
1287 ga_init2(&ga, sizeof(char *), 3);
1288 else
1289 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001290
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001291 // First search in map modifier arguments
1292 for (i = 0; i < 7; ++i)
1293 {
1294 if (i == 0)
1295 p = (char_u *)"<silent>";
1296 else if (i == 1)
1297 p = (char_u *)"<unique>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001298#ifdef FEAT_EVAL
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001299 else if (i == 2)
1300 p = (char_u *)"<script>";
1301 else if (i == 3)
1302 p = (char_u *)"<expr>";
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001303#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001304 else if (i == 4 && !expand_buffer)
1305 p = (char_u *)"<buffer>";
1306 else if (i == 5)
1307 p = (char_u *)"<nowait>";
1308 else if (i == 6)
1309 p = (char_u *)"<special>";
1310 else
1311 continue;
1312
1313 if (!fuzzy)
1314 match = vim_regexec(regmatch, p, (colnr_T)0);
1315 else
1316 {
1317 score = fuzzy_match_str(p, pat);
1318 match = (score != 0);
1319 }
1320
1321 if (!match)
1322 continue;
1323
1324 if (ga_grow(&ga, 1) == FAIL)
1325 break;
1326
1327 if (fuzzy)
1328 {
1329 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1330 fuzmatch->idx = ga.ga_len;
1331 fuzmatch->str = vim_strsave(p);
1332 fuzmatch->score = score;
1333 }
1334 else
1335 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
1336 ++ga.ga_len;
1337 }
1338
1339 for (hash = 0; hash < 256; ++hash)
1340 {
1341 if (expand_isabbrev)
1342 {
1343 if (hash > 0) // only one abbrev list
1344 break; // for (hash)
1345 mp = first_abbr;
1346 }
1347 else if (expand_buffer)
1348 mp = curbuf->b_maphash[hash];
1349 else
1350 mp = maphash[hash];
1351 for (; mp; mp = mp->m_next)
1352 {
1353 if (!(mp->m_mode & expand_mapmodes))
1354 continue;
1355
1356 p = translate_mapping(mp->m_keys);
1357 if (p == NULL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001358 continue;
1359
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001360 if (!fuzzy)
1361 match = vim_regexec(regmatch, p, (colnr_T)0);
1362 else
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001363 {
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001364 score = fuzzy_match_str(p, pat);
1365 match = (score != 0);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001366 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001367
1368 if (!match)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001369 {
1370 vim_free(p);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001371 continue;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001372 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001373
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001374 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001375 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001376 vim_free(p);
1377 break;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001378 }
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001379
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001380 if (fuzzy)
1381 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001382 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
1383 fuzmatch->idx = ga.ga_len;
1384 fuzmatch->str = p;
1385 fuzmatch->score = score;
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001386 }
1387 else
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001388 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001389
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001390 ++ga.ga_len;
1391 } // for (mp)
1392 } // for (hash)
1393
1394 if (ga.ga_len == 0)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001395 return FAIL;
1396
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00001397 if (!fuzzy)
1398 {
1399 *matches = ga.ga_data;
1400 *numMatches = ga.ga_len;
1401 }
1402 else
1403 {
1404 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
1405 FALSE) == FAIL)
1406 return FAIL;
1407 *numMatches = ga.ga_len;
1408 }
1409
1410 count = *numMatches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001411 if (count > 1)
1412 {
1413 char_u **ptr1;
1414 char_u **ptr2;
1415 char_u **ptr3;
1416
1417 // Sort the matches
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001418 // Fuzzy matching already sorts the matches
1419 if (!fuzzy)
1420 sort_strings(*matches, count);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001421
1422 // Remove multiple entries
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001423 ptr1 = *matches;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001424 ptr2 = ptr1 + 1;
1425 ptr3 = ptr1 + count;
1426
1427 while (ptr2 < ptr3)
1428 {
1429 if (STRCMP(*ptr1, *ptr2))
1430 *++ptr1 = *ptr2++;
1431 else
1432 {
1433 vim_free(*ptr2++);
1434 count--;
1435 }
1436 }
1437 }
1438
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001439 *numMatches = count;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001440 return (count == 0 ? FAIL : OK);
1441}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001442
1443/*
1444 * Check for an abbreviation.
1445 * Cursor is at ptr[col].
1446 * When inserting, mincol is where insert started.
1447 * For the command line, mincol is what is to be skipped over.
1448 * "c" is the character typed before check_abbr was called. It may have
1449 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1450 *
1451 * Historic vi practice: The last character of an abbreviation must be an id
1452 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1453 * characters or all non-id characters. This allows for abbr. "#i" to
1454 * "#include".
1455 *
1456 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1457 * Then there must be white space before the abbr.
1458 *
1459 * return TRUE if there is an abbreviation, FALSE if not
1460 */
1461 int
1462check_abbr(
1463 int c,
1464 char_u *ptr,
1465 int col,
1466 int mincol)
1467{
1468 int len;
1469 int scol; // starting column of the abbr.
1470 int j;
1471 char_u *s;
1472 char_u tb[MB_MAXBYTES + 4];
1473 mapblock_T *mp;
1474 mapblock_T *mp2;
1475 int clen = 0; // length in characters
1476 int is_id = TRUE;
1477 int vim_abbr;
1478
1479 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1480 return FALSE;
1481
1482 // no remapping implies no abbreviation, except for CTRL-]
1483 if (noremap_keys() && c != Ctrl_RSB)
1484 return FALSE;
1485
1486 // Check for word before the cursor: If it ends in a keyword char all
1487 // chars before it must be keyword chars or non-keyword chars, but not
1488 // white space. If it ends in a non-keyword char we accept any characters
1489 // before it except white space.
1490 if (col == 0) // cannot be an abbr.
1491 return FALSE;
1492
1493 if (has_mbyte)
1494 {
1495 char_u *p;
1496
1497 p = mb_prevptr(ptr, ptr + col);
1498 if (!vim_iswordp(p))
1499 vim_abbr = TRUE; // Vim added abbr.
1500 else
1501 {
1502 vim_abbr = FALSE; // vi compatible abbr.
1503 if (p > ptr)
1504 is_id = vim_iswordp(mb_prevptr(ptr, p));
1505 }
1506 clen = 1;
1507 while (p > ptr + mincol)
1508 {
1509 p = mb_prevptr(ptr, p);
1510 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1511 {
1512 p += (*mb_ptr2len)(p);
1513 break;
1514 }
1515 ++clen;
1516 }
1517 scol = (int)(p - ptr);
1518 }
1519 else
1520 {
1521 if (!vim_iswordc(ptr[col - 1]))
1522 vim_abbr = TRUE; // Vim added abbr.
1523 else
1524 {
1525 vim_abbr = FALSE; // vi compatible abbr.
1526 if (col > 1)
1527 is_id = vim_iswordc(ptr[col - 2]);
1528 }
1529 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1530 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1531 ;
1532 }
1533
1534 if (scol < mincol)
1535 scol = mincol;
1536 if (scol < col) // there is a word in front of the cursor
1537 {
1538 ptr += scol;
1539 len = col - scol;
1540 mp = curbuf->b_first_abbr;
1541 mp2 = first_abbr;
1542 if (mp == NULL)
1543 {
1544 mp = mp2;
1545 mp2 = NULL;
1546 }
1547 for ( ; mp; mp->m_next == NULL
1548 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1549 {
1550 int qlen = mp->m_keylen;
1551 char_u *q = mp->m_keys;
1552 int match;
1553
1554 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1555 {
1556 char_u *qe = vim_strsave(mp->m_keys);
1557
1558 // might have CSI escaped mp->m_keys
1559 if (qe != NULL)
1560 {
1561 q = qe;
1562 vim_unescape_csi(q);
1563 qlen = (int)STRLEN(q);
1564 }
1565 }
1566
1567 // find entries with right mode and keys
1568 match = (mp->m_mode & State)
1569 && qlen == len
1570 && !STRNCMP(q, ptr, (size_t)len);
1571 if (q != mp->m_keys)
1572 vim_free(q);
1573 if (match)
1574 break;
1575 }
1576 if (mp != NULL)
1577 {
Bram Moolenaar94075b22022-01-18 20:30:39 +00001578 int noremap;
1579 int silent;
1580#ifdef FEAT_EVAL
1581 int expr;
1582#endif
1583
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001584 // Found a match:
1585 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1586 // This goes from end to start.
1587 //
1588 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1589 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1590 // Characters where IS_SPECIAL() == TRUE: key codes, need
1591 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1592 //
1593 // Character CTRL-] is treated specially - it completes the
1594 // abbreviation, but is not inserted into the input stream.
1595 j = 0;
1596 if (c != Ctrl_RSB)
1597 {
1598 // special key code, split up
1599 if (IS_SPECIAL(c) || c == K_SPECIAL)
1600 {
1601 tb[j++] = K_SPECIAL;
1602 tb[j++] = K_SECOND(c);
1603 tb[j++] = K_THIRD(c);
1604 }
1605 else
1606 {
1607 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1608 tb[j++] = Ctrl_V; // special char needs CTRL-V
1609 if (has_mbyte)
1610 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001611 int newlen;
1612 char_u *escaped;
1613
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001614 // if ABBR_OFF has been added, remove it here
1615 if (c >= ABBR_OFF)
1616 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001617 newlen = (*mb_char2bytes)(c, tb + j);
1618 tb[j + newlen] = NUL;
1619 // Need to escape K_SPECIAL.
1620 escaped = vim_strsave_escape_csi(tb + j);
1621 if (escaped != NULL)
1622 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001623 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001624 mch_memmove(tb + j, escaped, newlen);
1625 j += newlen;
1626 vim_free(escaped);
1627 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001628 }
1629 else
1630 tb[j++] = c;
1631 }
1632 tb[j] = NUL;
1633 // insert the last typed char
1634 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1635 }
Bram Moolenaar94075b22022-01-18 20:30:39 +00001636
1637 // copy values here, calling eval_map_expr() may make "mp" invalid!
1638 noremap = mp->m_noremap;
1639 silent = mp->m_silent;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001640#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001641 expr = mp->m_expr;
1642
1643 if (expr)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001644 s = eval_map_expr(mp, c);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001645 else
1646#endif
1647 s = mp->m_str;
1648 if (s != NULL)
1649 {
1650 // insert the to string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001651 (void)ins_typebuf(s, noremap, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001652 // no abbrev. for these chars
1653 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1654#ifdef FEAT_EVAL
Bram Moolenaar94075b22022-01-18 20:30:39 +00001655 if (expr)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001656 vim_free(s);
1657#endif
1658 }
1659
1660 tb[0] = Ctrl_H;
1661 tb[1] = NUL;
1662 if (has_mbyte)
1663 len = clen; // Delete characters instead of bytes
1664 while (len-- > 0) // delete the from string
Bram Moolenaar94075b22022-01-18 20:30:39 +00001665 (void)ins_typebuf(tb, 1, 0, TRUE, silent);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001666 return TRUE;
1667 }
1668 }
1669 return FALSE;
1670}
1671
1672#ifdef FEAT_EVAL
1673/*
1674 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1675 * special characters.
Bram Moolenaar94075b22022-01-18 20:30:39 +00001676 * Careful: after this "mp" will be invalid if the mapping was deleted.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001677 */
1678 char_u *
1679eval_map_expr(
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001680 mapblock_T *mp,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001681 int c) // NUL or typed character for abbreviation
1682{
1683 char_u *res;
1684 char_u *p;
1685 char_u *expr;
1686 pos_T save_cursor;
1687 int save_msg_col;
1688 int save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001689 scid_T save_sctx_sid = current_sctx.sc_sid;
1690 int save_sctx_version = current_sctx.sc_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001691
1692 // Remove escaping of CSI, because "str" is in a format to be used as
1693 // typeahead.
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001694 expr = vim_strsave(mp->m_str);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001695 if (expr == NULL)
1696 return NULL;
1697 vim_unescape_csi(expr);
1698
1699 // Forbid changing text or using ":normal" to avoid most of the bad side
1700 // effects. Also restore the cursor position.
zeertzjqcfe45652022-05-27 17:26:55 +01001701 ++textlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001702 ++ex_normal_lock;
1703 set_vim_var_char(c); // set v:char to the typed character
1704 save_cursor = curwin->w_cursor;
1705 save_msg_col = msg_col;
1706 save_msg_row = msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001707 if (mp->m_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
1708 {
1709 current_sctx.sc_sid = mp->m_script_ctx.sc_sid;
1710 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1711 }
1712
1713 // Note: the evaluation may make "mp" invalid.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001714 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001715
zeertzjqcfe45652022-05-27 17:26:55 +01001716 --textlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001717 --ex_normal_lock;
1718 curwin->w_cursor = save_cursor;
1719 msg_col = save_msg_col;
1720 msg_row = save_msg_row;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001721 current_sctx.sc_sid = save_sctx_sid;
1722 current_sctx.sc_version = save_sctx_version;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001723
1724 vim_free(expr);
1725
1726 if (p == NULL)
1727 return NULL;
1728 // Escape CSI in the result to be able to use the string as typeahead.
1729 res = vim_strsave_escape_csi(p);
1730 vim_free(p);
1731
1732 return res;
1733}
1734#endif
1735
1736/*
1737 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1738 * can be put in the typeahead buffer.
1739 * Returns NULL when out of memory.
1740 */
1741 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001742vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001743{
1744 char_u *res;
1745 char_u *s, *d;
1746
1747 // Need a buffer to hold up to three times as much. Four in case of an
1748 // illegal utf-8 byte:
1749 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1750 res = alloc(STRLEN(p) * 4 + 1);
1751 if (res != NULL)
1752 {
1753 d = res;
1754 for (s = p; *s != NUL; )
1755 {
zeertzjq2cd0f272022-10-04 20:14:28 +01001756 if ((s[0] == K_SPECIAL
1757#ifdef FEAT_GUI
1758 || (gui.in_use && s[0] == CSI)
1759#endif
1760 ) && s[1] != NUL && s[2] != NUL)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001761 {
1762 // Copy special key unmodified.
1763 *d++ = *s++;
1764 *d++ = *s++;
1765 *d++ = *s++;
1766 }
1767 else
1768 {
1769 // Add character, possibly multi-byte to destination, escaping
1770 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1771 d = add_char2buf(PTR2CHAR(s), d);
1772 s += MB_CPTR2LEN(s);
1773 }
1774 }
1775 *d = NUL;
1776 }
1777 return res;
1778}
1779
1780/*
1781 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1782 * vim_strsave_escape_csi(). Works in-place.
1783 */
1784 void
1785vim_unescape_csi(char_u *p)
1786{
1787 char_u *s = p, *d = p;
1788
1789 while (*s != NUL)
1790 {
1791 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1792 {
1793 *d++ = K_SPECIAL;
1794 s += 3;
1795 }
1796 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1797 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1798 {
1799 *d++ = CSI;
1800 s += 3;
1801 }
1802 else
1803 *d++ = *s++;
1804 }
1805 *d = NUL;
1806}
1807
1808/*
1809 * Write map commands for the current mappings to an .exrc file.
1810 * Return FAIL on error, OK otherwise.
1811 */
1812 int
1813makemap(
1814 FILE *fd,
1815 buf_T *buf) // buffer for local mappings or NULL
1816{
1817 mapblock_T *mp;
1818 char_u c1, c2, c3;
1819 char_u *p;
1820 char *cmd;
1821 int abbr;
1822 int hash;
1823 int did_cpo = FALSE;
1824 int i;
1825
1826 validate_maphash();
1827
1828 // Do the loop twice: Once for mappings, once for abbreviations.
1829 // Then loop over all map hash lists.
1830 for (abbr = 0; abbr < 2; ++abbr)
1831 for (hash = 0; hash < 256; ++hash)
1832 {
1833 if (abbr)
1834 {
1835 if (hash > 0) // there is only one abbr list
1836 break;
1837 if (buf != NULL)
1838 mp = buf->b_first_abbr;
1839 else
1840 mp = first_abbr;
1841 }
1842 else
1843 {
1844 if (buf != NULL)
1845 mp = buf->b_maphash[hash];
1846 else
1847 mp = maphash[hash];
1848 }
1849
1850 for ( ; mp; mp = mp->m_next)
1851 {
1852 // skip script-local mappings
1853 if (mp->m_noremap == REMAP_SCRIPT)
1854 continue;
1855
1856 // skip mappings that contain a <SNR> (script-local thing),
1857 // they probably don't work when loaded again
1858 for (p = mp->m_str; *p != NUL; ++p)
1859 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1860 && p[2] == (int)KE_SNR)
1861 break;
1862 if (*p != NUL)
1863 continue;
1864
1865 // It's possible to create a mapping and then ":unmap" certain
1866 // modes. We recreate this here by mapping the individual
1867 // modes, which requires up to three of them.
1868 c1 = NUL;
1869 c2 = NUL;
1870 c3 = NUL;
1871 if (abbr)
1872 cmd = "abbr";
1873 else
1874 cmd = "map";
1875 switch (mp->m_mode)
1876 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001877 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT
1878 | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001879 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001880 case MODE_NORMAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001881 c1 = 'n';
1882 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001883 case MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001884 c1 = 'x';
1885 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001886 case MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001887 c1 = 's';
1888 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001889 case MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001890 c1 = 'o';
1891 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001892 case MODE_NORMAL | MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001893 c1 = 'n';
1894 c2 = 'x';
1895 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001896 case MODE_NORMAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001897 c1 = 'n';
1898 c2 = 's';
1899 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001900 case MODE_NORMAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001901 c1 = 'n';
1902 c2 = 'o';
1903 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001904 case MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001905 c1 = 'v';
1906 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001907 case MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001908 c1 = 'x';
1909 c2 = 'o';
1910 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001911 case MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001912 c1 = 's';
1913 c2 = 'o';
1914 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001915 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001916 c1 = 'n';
1917 c2 = 'v';
1918 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001919 case MODE_NORMAL | MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001920 c1 = 'n';
1921 c2 = 'x';
1922 c3 = 'o';
1923 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001924 case MODE_NORMAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001925 c1 = 'n';
1926 c2 = 's';
1927 c3 = 'o';
1928 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001929 case MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001930 c1 = 'v';
1931 c2 = 'o';
1932 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001933 case MODE_CMDLINE | MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001934 if (!abbr)
1935 cmd = "map!";
1936 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001937 case MODE_CMDLINE:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001938 c1 = 'c';
1939 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001940 case MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001941 c1 = 'i';
1942 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001943 case MODE_LANGMAP:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001944 c1 = 'l';
1945 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001946 case MODE_TERMINAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001947 c1 = 't';
1948 break;
1949 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001950 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001951 return FAIL;
1952 }
1953 do // do this twice if c2 is set, 3 times with c3
1954 {
1955 // When outputting <> form, need to make sure that 'cpo'
1956 // is set to the Vim default.
1957 if (!did_cpo)
1958 {
1959 if (*mp->m_str == NUL) // will use <Nop>
1960 did_cpo = TRUE;
1961 else
1962 for (i = 0; i < 2; ++i)
1963 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1964 if (*p == K_SPECIAL || *p == NL)
1965 did_cpo = TRUE;
1966 if (did_cpo)
1967 {
1968 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1969 || put_eol(fd) < 0
1970 || fprintf(fd, "set cpo&vim") < 0
1971 || put_eol(fd) < 0)
1972 return FAIL;
1973 }
1974 }
1975 if (c1 && putc(c1, fd) < 0)
1976 return FAIL;
1977 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1978 return FAIL;
1979 if (fputs(cmd, fd) < 0)
1980 return FAIL;
1981 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1982 return FAIL;
1983 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1984 return FAIL;
1985 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1986 return FAIL;
1987#ifdef FEAT_EVAL
1988 if (mp->m_noremap == REMAP_SCRIPT
1989 && fputs("<script>", fd) < 0)
1990 return FAIL;
1991 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1992 return FAIL;
1993#endif
1994
1995 if ( putc(' ', fd) < 0
1996 || put_escstr(fd, mp->m_keys, 0) == FAIL
1997 || putc(' ', fd) < 0
1998 || put_escstr(fd, mp->m_str, 1) == FAIL
1999 || put_eol(fd) < 0)
2000 return FAIL;
2001 c1 = c2;
2002 c2 = c3;
2003 c3 = NUL;
2004 } while (c1 != NUL);
2005 }
2006 }
2007
2008 if (did_cpo)
2009 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
2010 || put_eol(fd) < 0
2011 || fprintf(fd, "unlet s:cpo_save") < 0
2012 || put_eol(fd) < 0)
2013 return FAIL;
2014 return OK;
2015}
2016
2017/*
2018 * write escape string to file
2019 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2020 *
2021 * return FAIL for failure, OK otherwise
2022 */
2023 int
2024put_escstr(FILE *fd, char_u *strstart, int what)
2025{
2026 char_u *str = strstart;
2027 int c;
2028 int modifiers;
2029
2030 // :map xx <Nop>
2031 if (*str == NUL && what == 1)
2032 {
2033 if (fprintf(fd, "<Nop>") < 0)
2034 return FAIL;
2035 return OK;
2036 }
2037
2038 for ( ; *str != NUL; ++str)
2039 {
2040 char_u *p;
2041
2042 // Check for a multi-byte character, which may contain escaped
2043 // K_SPECIAL and CSI bytes
2044 p = mb_unescape(&str);
2045 if (p != NULL)
2046 {
2047 while (*p != NUL)
2048 if (fputc(*p++, fd) < 0)
2049 return FAIL;
2050 --str;
2051 continue;
2052 }
2053
2054 c = *str;
2055 // Special key codes have to be translated to be able to make sense
2056 // when they are read back.
2057 if (c == K_SPECIAL && what != 2)
2058 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002059 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002060 if (str[1] == KS_MODIFIER)
2061 {
2062 modifiers = str[2];
2063 str += 3;
2064 c = *str;
2065 }
2066 if (c == K_SPECIAL)
2067 {
2068 c = TO_SPECIAL(str[1], str[2]);
2069 str += 2;
2070 }
2071 if (IS_SPECIAL(c) || modifiers) // special key
2072 {
2073 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2074 return FAIL;
2075 continue;
2076 }
2077 }
2078
2079 // A '\n' in a map command should be written as <NL>.
2080 // A '\n' in a set command should be written as \^V^J.
2081 if (c == NL)
2082 {
2083 if (what == 2)
2084 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002085 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002086 return FAIL;
2087 }
2088 else
2089 {
2090 if (fprintf(fd, "<NL>") < 0)
2091 return FAIL;
2092 }
2093 continue;
2094 }
2095
2096 // Some characters have to be escaped with CTRL-V to
2097 // prevent them from misinterpreted in DoOneCmd().
2098 // A space, Tab and '"' has to be escaped with a backslash to
2099 // prevent it to be misinterpreted in do_set().
2100 // A space has to be escaped with a CTRL-V when it's at the start of a
2101 // ":map" rhs.
2102 // A '<' has to be escaped with a CTRL-V to prevent it being
2103 // interpreted as the start of a special key name.
2104 // A space in the lhs of a :map needs a CTRL-V.
2105 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2106 {
2107 if (putc('\\', fd) < 0)
2108 return FAIL;
2109 }
2110 else if (c < ' ' || c > '~' || c == '|'
2111 || (what == 0 && c == ' ')
2112 || (what == 1 && str == strstart && c == ' ')
2113 || (what != 2 && c == '<'))
2114 {
2115 if (putc(Ctrl_V, fd) < 0)
2116 return FAIL;
2117 }
2118 if (putc(c, fd) < 0)
2119 return FAIL;
2120 }
2121 return OK;
2122}
2123
2124/*
2125 * Check all mappings for the presence of special key codes.
2126 * Used after ":set term=xxx".
2127 */
2128 void
2129check_map_keycodes(void)
2130{
2131 mapblock_T *mp;
2132 char_u *p;
2133 int i;
2134 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002135 int abbr;
2136 int hash;
2137 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002138 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002139
2140 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002141 // avoids giving error messages
2142 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002143 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002144
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002145 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002146 // mappings/abbreviations with bp == NULL
2147 for (bp = firstbuf; ; bp = bp->b_next)
2148 {
2149 // Do the loop twice: Once for mappings, once for abbreviations.
2150 // Then loop over all map hash lists.
2151 for (abbr = 0; abbr <= 1; ++abbr)
2152 for (hash = 0; hash < 256; ++hash)
2153 {
2154 if (abbr)
2155 {
2156 if (hash) // there is only one abbr list
2157 break;
2158 if (bp != NULL)
2159 mp = bp->b_first_abbr;
2160 else
2161 mp = first_abbr;
2162 }
2163 else
2164 {
2165 if (bp != NULL)
2166 mp = bp->b_maphash[hash];
2167 else
2168 mp = maphash[hash];
2169 }
2170 for ( ; mp != NULL; mp = mp->m_next)
2171 {
2172 for (i = 0; i <= 1; ++i) // do this twice
2173 {
2174 if (i == 0)
2175 p = mp->m_keys; // once for the "from" part
2176 else
2177 p = mp->m_str; // and once for the "to" part
2178 while (*p)
2179 {
2180 if (*p == K_SPECIAL)
2181 {
2182 ++p;
2183 if (*p < 128) // for "normal" tcap entries
2184 {
2185 buf[0] = p[0];
2186 buf[1] = p[1];
2187 buf[2] = NUL;
2188 (void)add_termcap_entry(buf, FALSE);
2189 }
2190 ++p;
2191 }
2192 ++p;
2193 }
2194 }
2195 }
2196 }
2197 if (bp == NULL)
2198 break;
2199 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002200 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002201 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002202}
2203
2204#if defined(FEAT_EVAL) || defined(PROTO)
2205/*
2206 * Check the string "keys" against the lhs of all mappings.
2207 * Return pointer to rhs of mapping (mapblock->m_str).
2208 * NULL when no mapping found.
2209 */
2210 char_u *
2211check_map(
2212 char_u *keys,
2213 int mode,
2214 int exact, // require exact match
2215 int ign_mod, // ignore preceding modifier
2216 int abbr, // do abbreviations
2217 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2218 int *local_ptr) // return: buffer-local mapping or NULL
2219{
2220 int hash;
2221 int len, minlen;
2222 mapblock_T *mp;
2223 char_u *s;
2224 int local;
2225
2226 validate_maphash();
2227
2228 len = (int)STRLEN(keys);
2229 for (local = 1; local >= 0; --local)
2230 // loop over all hash lists
2231 for (hash = 0; hash < 256; ++hash)
2232 {
2233 if (abbr)
2234 {
2235 if (hash > 0) // there is only one list.
2236 break;
2237 if (local)
2238 mp = curbuf->b_first_abbr;
2239 else
2240 mp = first_abbr;
2241 }
2242 else if (local)
2243 mp = curbuf->b_maphash[hash];
2244 else
2245 mp = maphash[hash];
2246 for ( ; mp != NULL; mp = mp->m_next)
2247 {
2248 // skip entries with wrong mode, wrong length and not matching
2249 // ones
2250 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2251 {
2252 if (len > mp->m_keylen)
2253 minlen = mp->m_keylen;
2254 else
2255 minlen = len;
2256 s = mp->m_keys;
2257 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2258 && s[2] != NUL)
2259 {
2260 s += 3;
2261 if (len > mp->m_keylen - 3)
2262 minlen = mp->m_keylen - 3;
2263 }
2264 if (STRNCMP(s, keys, minlen) == 0)
2265 {
2266 if (mp_ptr != NULL)
2267 *mp_ptr = mp;
2268 if (local_ptr != NULL)
2269 *local_ptr = local;
2270 return mp->m_str;
2271 }
2272 }
2273 }
2274 }
2275
2276 return NULL;
2277}
2278
Ernie Rael659c2402022-04-24 18:40:28 +01002279/*
zeertzjqc207fd22022-06-29 10:37:40 +01002280 * "hasmapto()" function
2281 */
2282 void
2283f_hasmapto(typval_T *argvars, typval_T *rettv)
2284{
2285 char_u *name;
2286 char_u *mode;
2287 char_u buf[NUMBUFLEN];
2288 int abbr = FALSE;
2289
2290 if (in_vim9script()
2291 && (check_for_string_arg(argvars, 0) == FAIL
2292 || check_for_opt_string_arg(argvars, 1) == FAIL
2293 || (argvars[1].v_type != VAR_UNKNOWN
2294 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2295 return;
2296
2297 name = tv_get_string(&argvars[0]);
2298 if (argvars[1].v_type == VAR_UNKNOWN)
2299 mode = (char_u *)"nvo";
2300 else
2301 {
2302 mode = tv_get_string_buf(&argvars[1], buf);
2303 if (argvars[2].v_type != VAR_UNKNOWN)
2304 abbr = (int)tv_get_bool(&argvars[2]);
2305 }
2306
2307 if (map_to_exists(name, mode, abbr))
2308 rettv->vval.v_number = TRUE;
2309 else
2310 rettv->vval.v_number = FALSE;
2311}
2312
2313/*
Ernie Rael659c2402022-04-24 18:40:28 +01002314 * Fill in the empty dictionary with items as defined by maparg builtin.
2315 */
2316 static void
2317mapblock2dict(
2318 mapblock_T *mp,
2319 dict_T *dict,
2320 char_u *lhsrawalt, // may be NULL
Ernie Rael51d04d12022-05-04 15:40:22 +01002321 int buffer_local, // false if not buffer local mapping
2322 int abbr) // true if abbreviation
Ernie Rael659c2402022-04-24 18:40:28 +01002323{
zeertzjqcdc83932022-09-12 13:38:41 +01002324 char_u *lhs = str2special_save(mp->m_keys, TRUE, FALSE);
Ernie Rael659c2402022-04-24 18:40:28 +01002325 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2326
2327 dict_add_string(dict, "lhs", lhs);
2328 vim_free(lhs);
2329 dict_add_string(dict, "lhsraw", mp->m_keys);
2330 if (lhsrawalt)
2331 // Also add the value for the simplified entry.
2332 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2333 dict_add_string(dict, "rhs", mp->m_orig_str);
2334 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2335 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2336 ? 1L : 0L);
2337 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2338 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2339 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2340 dict_add_number(dict, "scriptversion",
2341 (long)mp->m_script_ctx.sc_version);
2342 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2343 dict_add_number(dict, "buffer", (long)buffer_local);
2344 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2345 dict_add_string(dict, "mode", mapmode);
Ernie Rael51d04d12022-05-04 15:40:22 +01002346 dict_add_number(dict, "abbr", abbr ? 1L : 0L);
Ernie Raeld8f5f762022-05-10 17:50:39 +01002347 dict_add_number(dict, "mode_bits", mp->m_mode);
Ernie Rael659c2402022-04-24 18:40:28 +01002348
2349 vim_free(mapmode);
2350}
2351
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002352 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002353get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2354{
2355 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002356 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002357 char_u *which;
2358 char_u buf[NUMBUFLEN];
2359 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002360 char_u *alt_keys_buf = NULL;
2361 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002362 char_u *rhs;
2363 int mode;
2364 int abbr = FALSE;
2365 int get_dict = FALSE;
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002366 mapblock_T *mp = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002367 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002368 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002369
2370 // return empty string for failure
2371 rettv->v_type = VAR_STRING;
2372 rettv->vval.v_string = NULL;
2373
2374 keys = tv_get_string(&argvars[0]);
2375 if (*keys == NUL)
2376 return;
2377
2378 if (argvars[1].v_type != VAR_UNKNOWN)
2379 {
2380 which = tv_get_string_buf_chk(&argvars[1], buf);
2381 if (argvars[2].v_type != VAR_UNKNOWN)
2382 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002383 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002384 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002385 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002386 }
2387 }
2388 else
2389 which = (char_u *)"";
2390 if (which == NULL)
2391 return;
2392
2393 mode = get_map_mode(&which, 0);
2394
Bram Moolenaar9c652532020-05-24 13:10:18 +02002395 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2396 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2397 &mp, &buffer_local);
2398 if (did_simplify)
2399 {
2400 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002401 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002402 (void)replace_termcodes(keys, &alt_keys_buf,
2403 flags | REPTERM_NO_SIMPLIFY, NULL);
2404 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2405 &buffer_local);
2406 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002407
2408 if (!get_dict)
2409 {
2410 // Return a string.
2411 if (rhs != NULL)
2412 {
2413 if (*rhs == NUL)
2414 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2415 else
zeertzjqcdc83932022-09-12 13:38:41 +01002416 rettv->vval.v_string = str2special_save(rhs, FALSE, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002417 }
2418
2419 }
Bram Moolenaar93a10962022-06-16 11:42:09 +01002420 else if (rettv_dict_alloc(rettv) == OK && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002421 mapblock2dict(mp, rettv->vval.v_dict,
Ernie Rael51d04d12022-05-04 15:40:22 +01002422 did_simplify ? keys_simplified : NULL,
2423 buffer_local, abbr);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002424
2425 vim_free(keys_buf);
2426 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002427}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002428
2429/*
Ernie Rael09661202022-04-25 14:40:44 +01002430 * "maplist()" function
Ernie Rael659c2402022-04-24 18:40:28 +01002431 */
2432 void
Ernie Rael09661202022-04-25 14:40:44 +01002433f_maplist(typval_T *argvars UNUSED, typval_T *rettv)
Ernie Rael659c2402022-04-24 18:40:28 +01002434{
2435 dict_T *d;
2436 mapblock_T *mp;
2437 int buffer_local;
2438 char_u *keys_buf;
2439 int did_simplify;
2440 int hash;
2441 char_u *lhs;
2442 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Ernie Rael09661202022-04-25 14:40:44 +01002443 int abbr = FALSE;
2444
2445 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2446 return;
2447 if (argvars[0].v_type != VAR_UNKNOWN)
2448 abbr = tv_get_bool(&argvars[0]);
Ernie Rael659c2402022-04-24 18:40:28 +01002449
Bram Moolenaar93a10962022-06-16 11:42:09 +01002450 if (rettv_list_alloc(rettv) == FAIL)
Ernie Rael659c2402022-04-24 18:40:28 +01002451 return;
2452
2453 validate_maphash();
2454
2455 // Do it twice: once for global maps and once for local maps.
2456 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2457 {
2458 for (hash = 0; hash < 256; ++hash)
2459 {
Ernie Rael09661202022-04-25 14:40:44 +01002460 if (abbr)
2461 {
2462 if (hash > 0) // there is only one abbr list
2463 break;
2464 if (buffer_local)
2465 mp = curbuf->b_first_abbr;
2466 else
2467 mp = first_abbr;
2468 }
2469 else if (buffer_local)
Ernie Rael659c2402022-04-24 18:40:28 +01002470 mp = curbuf->b_maphash[hash];
2471 else
2472 mp = maphash[hash];
2473 for (; mp; mp = mp->m_next)
2474 {
2475 if (mp->m_simplified)
2476 continue;
2477 if ((d = dict_alloc()) == NULL)
2478 return;
2479 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2480 return;
2481
2482 keys_buf = NULL;
2483 did_simplify = FALSE;
2484
zeertzjqcdc83932022-09-12 13:38:41 +01002485 lhs = str2special_save(mp->m_keys, TRUE, FALSE);
Ernie Rael659c2402022-04-24 18:40:28 +01002486 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2487 vim_free(lhs);
2488
2489 mapblock2dict(mp, d,
Ernie Rael51d04d12022-05-04 15:40:22 +01002490 did_simplify ? keys_buf : NULL,
2491 buffer_local, abbr);
Ernie Rael659c2402022-04-24 18:40:28 +01002492 vim_free(keys_buf);
2493 }
2494 }
2495 }
2496}
2497
2498/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002499 * "maparg()" function
2500 */
2501 void
2502f_maparg(typval_T *argvars, typval_T *rettv)
2503{
2504 if (in_vim9script()
2505 && (check_for_string_arg(argvars, 0) == FAIL
2506 || check_for_opt_string_arg(argvars, 1) == FAIL
2507 || (argvars[1].v_type != VAR_UNKNOWN
2508 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2509 || (argvars[2].v_type != VAR_UNKNOWN
2510 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2511 return;
2512
2513 get_maparg(argvars, rettv, TRUE);
2514}
2515
2516/*
2517 * "mapcheck()" function
2518 */
2519 void
2520f_mapcheck(typval_T *argvars, typval_T *rettv)
2521{
2522 if (in_vim9script()
2523 && (check_for_string_arg(argvars, 0) == FAIL
2524 || check_for_opt_string_arg(argvars, 1) == FAIL
2525 || (argvars[1].v_type != VAR_UNKNOWN
2526 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2527 return;
2528
2529 get_maparg(argvars, rettv, FALSE);
2530}
2531
2532/*
Ernie Rael51d04d12022-05-04 15:40:22 +01002533 * Get the mapping mode from the mode string.
2534 * It may contain multiple characters, eg "nox", or "!", or ' '
2535 * Return 0 if there is an error.
2536 */
2537 static int
2538get_map_mode_string(char_u *mode_string, int abbr)
2539{
2540 char_u *p = mode_string;
2541 int mode = 0;
2542 int tmode;
2543 int modec;
Bram Moolenaar24959102022-05-07 20:01:16 +01002544 const int MASK_V = MODE_VISUAL | MODE_SELECT;
2545 const int MASK_MAP = MODE_VISUAL | MODE_SELECT | MODE_NORMAL
2546 | MODE_OP_PENDING;
2547 const int MASK_BANG = MODE_INSERT | MODE_CMDLINE;
Ernie Rael51d04d12022-05-04 15:40:22 +01002548
2549 if (*p == NUL)
2550 p = (char_u *)" "; // compatibility
2551 while ((modec = *p++))
2552 {
2553 switch (modec)
2554 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002555 case 'i': tmode = MODE_INSERT; break;
2556 case 'l': tmode = MODE_LANGMAP; break;
2557 case 'c': tmode = MODE_CMDLINE; break;
2558 case 'n': tmode = MODE_NORMAL; break;
2559 case 'x': tmode = MODE_VISUAL; break;
2560 case 's': tmode = MODE_SELECT; break;
2561 case 'o': tmode = MODE_OP_PENDING; break;
2562 case 't': tmode = MODE_TERMINAL; break;
Ernie Rael51d04d12022-05-04 15:40:22 +01002563 case 'v': tmode = MASK_V; break;
2564 case '!': tmode = MASK_BANG; break;
2565 case ' ': tmode = MASK_MAP; break;
2566 default:
2567 return 0; // error, unknown mode character
2568 }
2569 mode |= tmode;
2570 }
2571 if ((abbr && (mode & ~MASK_BANG) != 0)
2572 || (!abbr && (mode & (mode-1)) != 0 // more than one bit set
2573 && (
2574 // false if multiple bits set in mode and mode is fully
2575 // contained in one mask
2576 !(((mode & MASK_BANG) != 0 && (mode & ~MASK_BANG) == 0)
2577 || ((mode & MASK_MAP) != 0 && (mode & ~MASK_MAP) == 0)))))
2578 return 0;
2579
2580 return mode;
2581}
2582
2583/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002584 * "mapset()" function
2585 */
2586 void
2587f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2588{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002589 char_u *which;
2590 int mode;
2591 char_u buf[NUMBUFLEN];
2592 int is_abbr;
2593 dict_T *d;
2594 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002595 char_u *lhsraw;
2596 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002597 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002598 char_u *orig_rhs;
2599 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002600 int noremap;
2601 int expr;
2602 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002603 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002604 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002605 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002606 linenr_T lnum;
2607 mapblock_T **map_table = maphash;
2608 mapblock_T **abbr_table = &first_abbr;
2609 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002610 char_u *arg;
Ernie Rael51d04d12022-05-04 15:40:22 +01002611 int dict_only;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002612
Ernie Rael51d04d12022-05-04 15:40:22 +01002613 // If first arg is a dict, then that's the only arg permitted.
2614 dict_only = argvars[0].v_type == VAR_DICT;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002615 if (in_vim9script()
Ernie Rael51d04d12022-05-04 15:40:22 +01002616 && (check_for_string_or_dict_arg(argvars, 0) == FAIL
2617 || (dict_only && check_for_unknown_arg(argvars, 1) == FAIL)
2618 || (!dict_only
2619 && (check_for_string_arg(argvars, 0) == FAIL
2620 || check_for_bool_arg(argvars, 1) == FAIL
2621 || check_for_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002622 return;
2623
Ernie Rael51d04d12022-05-04 15:40:22 +01002624 if (dict_only)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002625 {
Ernie Rael51d04d12022-05-04 15:40:22 +01002626 d = argvars[0].vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002627 which = dict_get_string(d, "mode", FALSE);
2628 is_abbr = dict_get_bool(d, "abbr", -1);
Ernie Rael51d04d12022-05-04 15:40:22 +01002629 if (which == NULL || is_abbr < 0)
2630 {
2631 emsg(_(e_entries_missing_in_mapset_dict_argument));
2632 return;
2633 }
2634 }
2635 else
2636 {
2637 which = tv_get_string_buf_chk(&argvars[0], buf);
2638 if (which == NULL)
2639 return;
2640 is_abbr = (int)tv_get_bool(&argvars[1]);
2641
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002642 if (check_for_dict_arg(argvars, 2) == FAIL)
Ernie Rael51d04d12022-05-04 15:40:22 +01002643 return;
Ernie Rael51d04d12022-05-04 15:40:22 +01002644 d = argvars[2].vval.v_dict;
2645 }
2646 mode = get_map_mode_string(which, is_abbr);
2647 if (mode == 0)
2648 {
2649 semsg(_(e_illegal_map_mode_string_str), which);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002650 return;
2651 }
Ernie Rael51d04d12022-05-04 15:40:22 +01002652
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002653
2654 // Get the values in the same order as above in get_maparg().
Bram Moolenaard61efa52022-07-23 09:52:04 +01002655 lhs = dict_get_string(d, "lhs", FALSE);
2656 lhsraw = dict_get_string(d, "lhsraw", FALSE);
2657 lhsrawalt = dict_get_string(d, "lhsrawalt", FALSE);
2658 rhs = dict_get_string(d, "rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002659 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002660 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002661 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002662 return;
2663 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002664 orig_rhs = rhs;
zeertzjq92a3d202022-08-31 16:40:17 +01002665 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
2666 rhs = (char_u *)"";
2667 else
2668 rhs = replace_termcodes(rhs, &arg_buf,
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002669 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002670
Bram Moolenaard61efa52022-07-23 09:52:04 +01002671 noremap = dict_get_number(d, "noremap") ? REMAP_NONE: 0;
2672 if (dict_get_number(d, "script") != 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002673 noremap = REMAP_SCRIPT;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002674 expr = dict_get_number(d, "expr") != 0;
2675 silent = dict_get_number(d, "silent") != 0;
2676 sid = dict_get_number(d, "sid");
2677 scriptversion = dict_get_number(d, "scriptversion");
2678 lnum = dict_get_number(d, "lnum");
2679 buffer = dict_get_number(d, "buffer");
2680 nowait = dict_get_number(d, "nowait") != 0;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002681 // mode from the dict is not used
2682
2683 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002684 {
2685 map_table = curbuf->b_maphash;
2686 abbr_table = &curbuf->b_first_abbr;
2687 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002688
2689 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002690 if (buffer)
2691 {
2692 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2693 if (arg == NULL)
2694 return;
2695 STRCPY(arg, "<buffer>");
2696 STRCPY(arg + 8, lhs);
2697 }
2698 else
2699 {
2700 arg = vim_strsave(lhs);
2701 if (arg == NULL)
2702 return;
2703 }
zeertzjq44068e92022-06-16 11:14:55 +01002704 do_map(MAPTYPE_UNMAP, arg, mode, is_abbr);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002705 vim_free(arg);
2706
Bram Moolenaar9c652532020-05-24 13:10:18 +02002707 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002708 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002709 if (lhsrawalt != NULL)
2710 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002711 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2712 lnum, 1);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002713 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002714}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002715#endif
2716
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002717
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002718#if defined(MSWIN) || defined(MACOS_X)
2719
Bram Moolenaar24959102022-05-07 20:01:16 +01002720# define VIS_SEL (MODE_VISUAL | MODE_SELECT) // abbreviation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002721
2722/*
2723 * Default mappings for some often used keys.
2724 */
2725struct initmap
2726{
2727 char_u *arg;
2728 int mode;
2729};
2730
2731# ifdef FEAT_GUI_MSWIN
2732// Use the Windows (CUA) keybindings. (GUI)
2733static struct initmap initmappings[] =
2734{
2735 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002736 {(char_u *)"<S-Insert> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002737 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002738 {(char_u *)"<S-Insert> <C-R><C-O>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002739 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2740 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2741 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2742 {(char_u *)"<C-X> \"*d", VIS_SEL},
2743 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2744};
2745# endif
2746
2747# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2748// Use the Windows (CUA) keybindings. (Console)
2749static struct initmap cinitmappings[] =
2750{
Bram Moolenaar24959102022-05-07 20:01:16 +01002751 {(char_u *)"\316w <C-Home>", MODE_NORMAL | VIS_SEL},
2752 {(char_u *)"\316w <C-Home>", MODE_INSERT | MODE_CMDLINE},
2753 {(char_u *)"\316u <C-End>", MODE_NORMAL | VIS_SEL},
2754 {(char_u *)"\316u <C-End>", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002755
2756 // paste, copy and cut
2757# ifdef FEAT_CLIPBOARD
Bram Moolenaar24959102022-05-07 20:01:16 +01002758 {(char_u *)"\316\324 \"*P", MODE_NORMAL}, // SHIFT-Insert is "*P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002759 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
Bram Moolenaar24959102022-05-07 20:01:16 +01002760 {(char_u *)"\316\324 \022\017*", MODE_INSERT}, // SHIFT-Insert is ^R^O*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002761 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2762 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2763 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2764 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2765# else
Bram Moolenaar24959102022-05-07 20:01:16 +01002766 {(char_u *)"\316\324 P", MODE_NORMAL}, // SHIFT-Insert is P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002767 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
Bram Moolenaar24959102022-05-07 20:01:16 +01002768 {(char_u *)"\316\324 \022\017\"", MODE_INSERT}, // SHIFT-Insert is ^R^O"
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002769 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2770 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2771 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2772# endif
2773};
2774# endif
2775
2776# if defined(MACOS_X)
2777static struct initmap initmappings[] =
2778{
2779 // Use the Standard MacOS binding.
2780 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002781 {(char_u *)"<D-v> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002782 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002783 {(char_u *)"<D-v> <C-R>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002784 {(char_u *)"<D-c> \"*y", VIS_SEL},
2785 {(char_u *)"<D-x> \"*d", VIS_SEL},
2786 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2787};
2788# endif
2789
2790# undef VIS_SEL
2791#endif
2792
2793/*
2794 * Set up default mappings.
2795 */
2796 void
2797init_mappings(void)
2798{
2799#if defined(MSWIN) || defined(MACOS_X)
2800 int i;
2801
2802# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2803# ifdef VIMDLL
2804 if (!gui.starting)
2805# endif
2806 {
K.Takataeeec2542021-06-02 13:28:16 +02002807 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002808 add_map(cinitmappings[i].arg, cinitmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002809 }
2810# endif
2811# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002812 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002813 add_map(initmappings[i].arg, initmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002814# endif
2815#endif
2816}
2817
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002818/*
2819 * Add a mapping "map" for mode "mode".
zeertzjq44068e92022-06-16 11:14:55 +01002820 * When "nore" is TRUE use MAPTYPE_NOREMAP.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002821 * Need to put string in allocated memory, because do_map() will modify it.
2822 */
2823 void
zeertzjq44068e92022-06-16 11:14:55 +01002824add_map(char_u *map, int mode, int nore)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002825{
2826 char_u *s;
2827 char_u *cpo_save = p_cpo;
2828
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002829 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002830 s = vim_strsave(map);
2831 if (s != NULL)
2832 {
zeertzjq44068e92022-06-16 11:14:55 +01002833 (void)do_map(nore ? MAPTYPE_NOREMAP : MAPTYPE_MAP, s, mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002834 vim_free(s);
2835 }
2836 p_cpo = cpo_save;
2837}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002838
Bram Moolenaare677df82019-09-02 22:31:11 +02002839#if defined(FEAT_LANGMAP) || defined(PROTO)
2840/*
2841 * Any character has an equivalent 'langmap' character. This is used for
2842 * keyboards that have a special language mode that sends characters above
2843 * 128 (although other characters can be translated too). The "to" field is a
2844 * Vim command character. This avoids having to switch the keyboard back to
2845 * ASCII mode when leaving Insert mode.
2846 *
2847 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2848 * commands.
2849 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2850 * same as langmap_mapchar[] for characters >= 256.
2851 *
2852 * Use growarray for 'langmap' chars >= 256
2853 */
2854typedef struct
2855{
2856 int from;
2857 int to;
2858} langmap_entry_T;
2859
2860static garray_T langmap_mapga;
2861
2862/*
2863 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2864 * field. If not found insert a new entry at the appropriate location.
2865 */
2866 static void
2867langmap_set_entry(int from, int to)
2868{
2869 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2870 int a = 0;
2871 int b = langmap_mapga.ga_len;
2872
2873 // Do a binary search for an existing entry.
2874 while (a != b)
2875 {
2876 int i = (a + b) / 2;
2877 int d = entries[i].from - from;
2878
2879 if (d == 0)
2880 {
2881 entries[i].to = to;
2882 return;
2883 }
2884 if (d < 0)
2885 a = i + 1;
2886 else
2887 b = i;
2888 }
2889
2890 if (ga_grow(&langmap_mapga, 1) != OK)
2891 return; // out of memory
2892
2893 // insert new entry at position "a"
2894 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2895 mch_memmove(entries + 1, entries,
2896 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2897 ++langmap_mapga.ga_len;
2898 entries[0].from = from;
2899 entries[0].to = to;
2900}
2901
2902/*
2903 * Apply 'langmap' to multi-byte character "c" and return the result.
2904 */
2905 int
2906langmap_adjust_mb(int c)
2907{
2908 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2909 int a = 0;
2910 int b = langmap_mapga.ga_len;
2911
2912 while (a != b)
2913 {
2914 int i = (a + b) / 2;
2915 int d = entries[i].from - c;
2916
2917 if (d == 0)
2918 return entries[i].to; // found matching entry
2919 if (d < 0)
2920 a = i + 1;
2921 else
2922 b = i;
2923 }
2924 return c; // no entry found, return "c" unmodified
2925}
2926
2927 void
2928langmap_init(void)
2929{
2930 int i;
2931
2932 for (i = 0; i < 256; i++)
2933 langmap_mapchar[i] = i; // we init with a one-to-one map
2934 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2935}
2936
2937/*
2938 * Called when langmap option is set; the language map can be
2939 * changed at any time!
2940 */
2941 void
2942langmap_set(void)
2943{
2944 char_u *p;
2945 char_u *p2;
2946 int from, to;
2947
2948 ga_clear(&langmap_mapga); // clear the previous map first
2949 langmap_init(); // back to one-to-one map
2950
2951 for (p = p_langmap; p[0] != NUL; )
2952 {
2953 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2954 MB_PTR_ADV(p2))
2955 {
2956 if (p2[0] == '\\' && p2[1] != NUL)
2957 ++p2;
2958 }
2959 if (p2[0] == ';')
2960 ++p2; // abcd;ABCD form, p2 points to A
2961 else
2962 p2 = NULL; // aAbBcCdD form, p2 is NULL
2963 while (p[0])
2964 {
2965 if (p[0] == ',')
2966 {
2967 ++p;
2968 break;
2969 }
2970 if (p[0] == '\\' && p[1] != NUL)
2971 ++p;
2972 from = (*mb_ptr2char)(p);
2973 to = NUL;
2974 if (p2 == NULL)
2975 {
2976 MB_PTR_ADV(p);
2977 if (p[0] != ',')
2978 {
2979 if (p[0] == '\\')
2980 ++p;
2981 to = (*mb_ptr2char)(p);
2982 }
2983 }
2984 else
2985 {
2986 if (p2[0] != ',')
2987 {
2988 if (p2[0] == '\\')
2989 ++p2;
2990 to = (*mb_ptr2char)(p2);
2991 }
2992 }
2993 if (to == NUL)
2994 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002995 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002996 transchar(from));
2997 return;
2998 }
2999
3000 if (from >= 256)
3001 langmap_set_entry(from, to);
3002 else
3003 langmap_mapchar[from & 255] = to;
3004
3005 // Advance to next pair
3006 MB_PTR_ADV(p);
3007 if (p2 != NULL)
3008 {
3009 MB_PTR_ADV(p2);
3010 if (*p == ';')
3011 {
3012 p = p2;
3013 if (p[0] != NUL)
3014 {
3015 if (p[0] != ',')
3016 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003017 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02003018 return;
3019 }
3020 ++p;
3021 }
3022 break;
3023 }
3024 }
3025 }
3026 }
3027}
3028#endif
3029
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003030 static void
3031do_exmap(exarg_T *eap, int isabbrev)
3032{
3033 int mode;
3034 char_u *cmdp;
3035
3036 cmdp = eap->cmd;
3037 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
3038
zeertzjq44068e92022-06-16 11:14:55 +01003039 switch (do_map(*cmdp == 'n' ? MAPTYPE_NOREMAP
3040 : *cmdp == 'u' ? MAPTYPE_UNMAP : MAPTYPE_MAP,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003041 eap->arg, mode, isabbrev))
3042 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003043 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003044 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003045 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
3046 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003047 break;
3048 }
3049}
3050
3051/*
3052 * ":abbreviate" and friends.
3053 */
3054 void
3055ex_abbreviate(exarg_T *eap)
3056{
3057 do_exmap(eap, TRUE); // almost the same as mapping
3058}
3059
3060/*
3061 * ":map" and friends.
3062 */
3063 void
3064ex_map(exarg_T *eap)
3065{
3066 // If we are sourcing .exrc or .vimrc in current directory we
3067 // print the mappings for security reasons.
3068 if (secure)
3069 {
3070 secure = 2;
3071 msg_outtrans(eap->cmd);
3072 msg_putchar('\n');
3073 }
3074 do_exmap(eap, FALSE);
3075}
3076
3077/*
3078 * ":unmap" and friends.
3079 */
3080 void
3081ex_unmap(exarg_T *eap)
3082{
3083 do_exmap(eap, FALSE);
3084}
3085
3086/*
3087 * ":mapclear" and friends.
3088 */
3089 void
3090ex_mapclear(exarg_T *eap)
3091{
3092 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
3093}
3094
3095/*
3096 * ":abclear" and friends.
3097 */
3098 void
3099ex_abclear(exarg_T *eap)
3100{
3101 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
3102}