blob: 3516f7ae2488a1223fbfada85b5ff09a35f339fb [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 Moolenaarb171fb12020-06-24 20:34:03 +02001714 p = eval_to_string(expr, 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 {
1756 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1757 {
1758 // Copy special key unmodified.
1759 *d++ = *s++;
1760 *d++ = *s++;
1761 *d++ = *s++;
1762 }
1763 else
1764 {
1765 // Add character, possibly multi-byte to destination, escaping
1766 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1767 d = add_char2buf(PTR2CHAR(s), d);
1768 s += MB_CPTR2LEN(s);
1769 }
1770 }
1771 *d = NUL;
1772 }
1773 return res;
1774}
1775
1776/*
1777 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1778 * vim_strsave_escape_csi(). Works in-place.
1779 */
1780 void
1781vim_unescape_csi(char_u *p)
1782{
1783 char_u *s = p, *d = p;
1784
1785 while (*s != NUL)
1786 {
1787 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1788 {
1789 *d++ = K_SPECIAL;
1790 s += 3;
1791 }
1792 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1793 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1794 {
1795 *d++ = CSI;
1796 s += 3;
1797 }
1798 else
1799 *d++ = *s++;
1800 }
1801 *d = NUL;
1802}
1803
1804/*
1805 * Write map commands for the current mappings to an .exrc file.
1806 * Return FAIL on error, OK otherwise.
1807 */
1808 int
1809makemap(
1810 FILE *fd,
1811 buf_T *buf) // buffer for local mappings or NULL
1812{
1813 mapblock_T *mp;
1814 char_u c1, c2, c3;
1815 char_u *p;
1816 char *cmd;
1817 int abbr;
1818 int hash;
1819 int did_cpo = FALSE;
1820 int i;
1821
1822 validate_maphash();
1823
1824 // Do the loop twice: Once for mappings, once for abbreviations.
1825 // Then loop over all map hash lists.
1826 for (abbr = 0; abbr < 2; ++abbr)
1827 for (hash = 0; hash < 256; ++hash)
1828 {
1829 if (abbr)
1830 {
1831 if (hash > 0) // there is only one abbr list
1832 break;
1833 if (buf != NULL)
1834 mp = buf->b_first_abbr;
1835 else
1836 mp = first_abbr;
1837 }
1838 else
1839 {
1840 if (buf != NULL)
1841 mp = buf->b_maphash[hash];
1842 else
1843 mp = maphash[hash];
1844 }
1845
1846 for ( ; mp; mp = mp->m_next)
1847 {
1848 // skip script-local mappings
1849 if (mp->m_noremap == REMAP_SCRIPT)
1850 continue;
1851
1852 // skip mappings that contain a <SNR> (script-local thing),
1853 // they probably don't work when loaded again
1854 for (p = mp->m_str; *p != NUL; ++p)
1855 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1856 && p[2] == (int)KE_SNR)
1857 break;
1858 if (*p != NUL)
1859 continue;
1860
1861 // It's possible to create a mapping and then ":unmap" certain
1862 // modes. We recreate this here by mapping the individual
1863 // modes, which requires up to three of them.
1864 c1 = NUL;
1865 c2 = NUL;
1866 c3 = NUL;
1867 if (abbr)
1868 cmd = "abbr";
1869 else
1870 cmd = "map";
1871 switch (mp->m_mode)
1872 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001873 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT
1874 | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001875 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001876 case MODE_NORMAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001877 c1 = 'n';
1878 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001879 case MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001880 c1 = 'x';
1881 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001882 case MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001883 c1 = 's';
1884 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001885 case MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001886 c1 = 'o';
1887 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001888 case MODE_NORMAL | MODE_VISUAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001889 c1 = 'n';
1890 c2 = 'x';
1891 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001892 case MODE_NORMAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001893 c1 = 'n';
1894 c2 = 's';
1895 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001896 case MODE_NORMAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001897 c1 = 'n';
1898 c2 = 'o';
1899 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001900 case MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001901 c1 = 'v';
1902 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001903 case MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001904 c1 = 'x';
1905 c2 = 'o';
1906 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001907 case MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001908 c1 = 's';
1909 c2 = 'o';
1910 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001911 case MODE_NORMAL | MODE_VISUAL | MODE_SELECT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001912 c1 = 'n';
1913 c2 = 'v';
1914 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001915 case MODE_NORMAL | MODE_VISUAL | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001916 c1 = 'n';
1917 c2 = 'x';
1918 c3 = 'o';
1919 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001920 case MODE_NORMAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001921 c1 = 'n';
1922 c2 = 's';
1923 c3 = 'o';
1924 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001925 case MODE_VISUAL | MODE_SELECT | MODE_OP_PENDING:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001926 c1 = 'v';
1927 c2 = 'o';
1928 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001929 case MODE_CMDLINE | MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001930 if (!abbr)
1931 cmd = "map!";
1932 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001933 case MODE_CMDLINE:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001934 c1 = 'c';
1935 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001936 case MODE_INSERT:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001937 c1 = 'i';
1938 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001939 case MODE_LANGMAP:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001940 c1 = 'l';
1941 break;
Bram Moolenaar24959102022-05-07 20:01:16 +01001942 case MODE_TERMINAL:
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001943 c1 = 't';
1944 break;
1945 default:
Bram Moolenaar6d057012021-12-31 18:49:43 +00001946 iemsg(_(e_makemap_illegal_mode));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001947 return FAIL;
1948 }
1949 do // do this twice if c2 is set, 3 times with c3
1950 {
1951 // When outputting <> form, need to make sure that 'cpo'
1952 // is set to the Vim default.
1953 if (!did_cpo)
1954 {
1955 if (*mp->m_str == NUL) // will use <Nop>
1956 did_cpo = TRUE;
1957 else
1958 for (i = 0; i < 2; ++i)
1959 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1960 if (*p == K_SPECIAL || *p == NL)
1961 did_cpo = TRUE;
1962 if (did_cpo)
1963 {
1964 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1965 || put_eol(fd) < 0
1966 || fprintf(fd, "set cpo&vim") < 0
1967 || put_eol(fd) < 0)
1968 return FAIL;
1969 }
1970 }
1971 if (c1 && putc(c1, fd) < 0)
1972 return FAIL;
1973 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1974 return FAIL;
1975 if (fputs(cmd, fd) < 0)
1976 return FAIL;
1977 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1978 return FAIL;
1979 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1980 return FAIL;
1981 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1982 return FAIL;
1983#ifdef FEAT_EVAL
1984 if (mp->m_noremap == REMAP_SCRIPT
1985 && fputs("<script>", fd) < 0)
1986 return FAIL;
1987 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1988 return FAIL;
1989#endif
1990
1991 if ( putc(' ', fd) < 0
1992 || put_escstr(fd, mp->m_keys, 0) == FAIL
1993 || putc(' ', fd) < 0
1994 || put_escstr(fd, mp->m_str, 1) == FAIL
1995 || put_eol(fd) < 0)
1996 return FAIL;
1997 c1 = c2;
1998 c2 = c3;
1999 c3 = NUL;
2000 } while (c1 != NUL);
2001 }
2002 }
2003
2004 if (did_cpo)
2005 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
2006 || put_eol(fd) < 0
2007 || fprintf(fd, "unlet s:cpo_save") < 0
2008 || put_eol(fd) < 0)
2009 return FAIL;
2010 return OK;
2011}
2012
2013/*
2014 * write escape string to file
2015 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
2016 *
2017 * return FAIL for failure, OK otherwise
2018 */
2019 int
2020put_escstr(FILE *fd, char_u *strstart, int what)
2021{
2022 char_u *str = strstart;
2023 int c;
2024 int modifiers;
2025
2026 // :map xx <Nop>
2027 if (*str == NUL && what == 1)
2028 {
2029 if (fprintf(fd, "<Nop>") < 0)
2030 return FAIL;
2031 return OK;
2032 }
2033
2034 for ( ; *str != NUL; ++str)
2035 {
2036 char_u *p;
2037
2038 // Check for a multi-byte character, which may contain escaped
2039 // K_SPECIAL and CSI bytes
2040 p = mb_unescape(&str);
2041 if (p != NULL)
2042 {
2043 while (*p != NUL)
2044 if (fputc(*p++, fd) < 0)
2045 return FAIL;
2046 --str;
2047 continue;
2048 }
2049
2050 c = *str;
2051 // Special key codes have to be translated to be able to make sense
2052 // when they are read back.
2053 if (c == K_SPECIAL && what != 2)
2054 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02002055 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002056 if (str[1] == KS_MODIFIER)
2057 {
2058 modifiers = str[2];
2059 str += 3;
2060 c = *str;
2061 }
2062 if (c == K_SPECIAL)
2063 {
2064 c = TO_SPECIAL(str[1], str[2]);
2065 str += 2;
2066 }
2067 if (IS_SPECIAL(c) || modifiers) // special key
2068 {
2069 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
2070 return FAIL;
2071 continue;
2072 }
2073 }
2074
2075 // A '\n' in a map command should be written as <NL>.
2076 // A '\n' in a set command should be written as \^V^J.
2077 if (c == NL)
2078 {
2079 if (what == 2)
2080 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002081 if (fprintf(fd, "\\\026\n") < 0)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002082 return FAIL;
2083 }
2084 else
2085 {
2086 if (fprintf(fd, "<NL>") < 0)
2087 return FAIL;
2088 }
2089 continue;
2090 }
2091
2092 // Some characters have to be escaped with CTRL-V to
2093 // prevent them from misinterpreted in DoOneCmd().
2094 // A space, Tab and '"' has to be escaped with a backslash to
2095 // prevent it to be misinterpreted in do_set().
2096 // A space has to be escaped with a CTRL-V when it's at the start of a
2097 // ":map" rhs.
2098 // A '<' has to be escaped with a CTRL-V to prevent it being
2099 // interpreted as the start of a special key name.
2100 // A space in the lhs of a :map needs a CTRL-V.
2101 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2102 {
2103 if (putc('\\', fd) < 0)
2104 return FAIL;
2105 }
2106 else if (c < ' ' || c > '~' || c == '|'
2107 || (what == 0 && c == ' ')
2108 || (what == 1 && str == strstart && c == ' ')
2109 || (what != 2 && c == '<'))
2110 {
2111 if (putc(Ctrl_V, fd) < 0)
2112 return FAIL;
2113 }
2114 if (putc(c, fd) < 0)
2115 return FAIL;
2116 }
2117 return OK;
2118}
2119
2120/*
2121 * Check all mappings for the presence of special key codes.
2122 * Used after ":set term=xxx".
2123 */
2124 void
2125check_map_keycodes(void)
2126{
2127 mapblock_T *mp;
2128 char_u *p;
2129 int i;
2130 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002131 int abbr;
2132 int hash;
2133 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002134 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002135
2136 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002137 // avoids giving error messages
2138 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002139 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002140
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002141 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002142 // mappings/abbreviations with bp == NULL
2143 for (bp = firstbuf; ; bp = bp->b_next)
2144 {
2145 // Do the loop twice: Once for mappings, once for abbreviations.
2146 // Then loop over all map hash lists.
2147 for (abbr = 0; abbr <= 1; ++abbr)
2148 for (hash = 0; hash < 256; ++hash)
2149 {
2150 if (abbr)
2151 {
2152 if (hash) // there is only one abbr list
2153 break;
2154 if (bp != NULL)
2155 mp = bp->b_first_abbr;
2156 else
2157 mp = first_abbr;
2158 }
2159 else
2160 {
2161 if (bp != NULL)
2162 mp = bp->b_maphash[hash];
2163 else
2164 mp = maphash[hash];
2165 }
2166 for ( ; mp != NULL; mp = mp->m_next)
2167 {
2168 for (i = 0; i <= 1; ++i) // do this twice
2169 {
2170 if (i == 0)
2171 p = mp->m_keys; // once for the "from" part
2172 else
2173 p = mp->m_str; // and once for the "to" part
2174 while (*p)
2175 {
2176 if (*p == K_SPECIAL)
2177 {
2178 ++p;
2179 if (*p < 128) // for "normal" tcap entries
2180 {
2181 buf[0] = p[0];
2182 buf[1] = p[1];
2183 buf[2] = NUL;
2184 (void)add_termcap_entry(buf, FALSE);
2185 }
2186 ++p;
2187 }
2188 ++p;
2189 }
2190 }
2191 }
2192 }
2193 if (bp == NULL)
2194 break;
2195 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002196 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002197 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002198}
2199
2200#if defined(FEAT_EVAL) || defined(PROTO)
2201/*
2202 * Check the string "keys" against the lhs of all mappings.
2203 * Return pointer to rhs of mapping (mapblock->m_str).
2204 * NULL when no mapping found.
2205 */
2206 char_u *
2207check_map(
2208 char_u *keys,
2209 int mode,
2210 int exact, // require exact match
2211 int ign_mod, // ignore preceding modifier
2212 int abbr, // do abbreviations
2213 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2214 int *local_ptr) // return: buffer-local mapping or NULL
2215{
2216 int hash;
2217 int len, minlen;
2218 mapblock_T *mp;
2219 char_u *s;
2220 int local;
2221
2222 validate_maphash();
2223
2224 len = (int)STRLEN(keys);
2225 for (local = 1; local >= 0; --local)
2226 // loop over all hash lists
2227 for (hash = 0; hash < 256; ++hash)
2228 {
2229 if (abbr)
2230 {
2231 if (hash > 0) // there is only one list.
2232 break;
2233 if (local)
2234 mp = curbuf->b_first_abbr;
2235 else
2236 mp = first_abbr;
2237 }
2238 else if (local)
2239 mp = curbuf->b_maphash[hash];
2240 else
2241 mp = maphash[hash];
2242 for ( ; mp != NULL; mp = mp->m_next)
2243 {
2244 // skip entries with wrong mode, wrong length and not matching
2245 // ones
2246 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2247 {
2248 if (len > mp->m_keylen)
2249 minlen = mp->m_keylen;
2250 else
2251 minlen = len;
2252 s = mp->m_keys;
2253 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2254 && s[2] != NUL)
2255 {
2256 s += 3;
2257 if (len > mp->m_keylen - 3)
2258 minlen = mp->m_keylen - 3;
2259 }
2260 if (STRNCMP(s, keys, minlen) == 0)
2261 {
2262 if (mp_ptr != NULL)
2263 *mp_ptr = mp;
2264 if (local_ptr != NULL)
2265 *local_ptr = local;
2266 return mp->m_str;
2267 }
2268 }
2269 }
2270 }
2271
2272 return NULL;
2273}
2274
Ernie Rael659c2402022-04-24 18:40:28 +01002275/*
zeertzjqc207fd22022-06-29 10:37:40 +01002276 * "hasmapto()" function
2277 */
2278 void
2279f_hasmapto(typval_T *argvars, typval_T *rettv)
2280{
2281 char_u *name;
2282 char_u *mode;
2283 char_u buf[NUMBUFLEN];
2284 int abbr = FALSE;
2285
2286 if (in_vim9script()
2287 && (check_for_string_arg(argvars, 0) == FAIL
2288 || check_for_opt_string_arg(argvars, 1) == FAIL
2289 || (argvars[1].v_type != VAR_UNKNOWN
2290 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2291 return;
2292
2293 name = tv_get_string(&argvars[0]);
2294 if (argvars[1].v_type == VAR_UNKNOWN)
2295 mode = (char_u *)"nvo";
2296 else
2297 {
2298 mode = tv_get_string_buf(&argvars[1], buf);
2299 if (argvars[2].v_type != VAR_UNKNOWN)
2300 abbr = (int)tv_get_bool(&argvars[2]);
2301 }
2302
2303 if (map_to_exists(name, mode, abbr))
2304 rettv->vval.v_number = TRUE;
2305 else
2306 rettv->vval.v_number = FALSE;
2307}
2308
2309/*
Ernie Rael659c2402022-04-24 18:40:28 +01002310 * Fill in the empty dictionary with items as defined by maparg builtin.
2311 */
2312 static void
2313mapblock2dict(
2314 mapblock_T *mp,
2315 dict_T *dict,
2316 char_u *lhsrawalt, // may be NULL
Ernie Rael51d04d12022-05-04 15:40:22 +01002317 int buffer_local, // false if not buffer local mapping
2318 int abbr) // true if abbreviation
Ernie Rael659c2402022-04-24 18:40:28 +01002319{
2320 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2321 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2322
2323 dict_add_string(dict, "lhs", lhs);
2324 vim_free(lhs);
2325 dict_add_string(dict, "lhsraw", mp->m_keys);
2326 if (lhsrawalt)
2327 // Also add the value for the simplified entry.
2328 dict_add_string(dict, "lhsrawalt", lhsrawalt);
2329 dict_add_string(dict, "rhs", mp->m_orig_str);
2330 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2331 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2332 ? 1L : 0L);
2333 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2334 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2335 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2336 dict_add_number(dict, "scriptversion",
2337 (long)mp->m_script_ctx.sc_version);
2338 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2339 dict_add_number(dict, "buffer", (long)buffer_local);
2340 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2341 dict_add_string(dict, "mode", mapmode);
Ernie Rael51d04d12022-05-04 15:40:22 +01002342 dict_add_number(dict, "abbr", abbr ? 1L : 0L);
Ernie Raeld8f5f762022-05-10 17:50:39 +01002343 dict_add_number(dict, "mode_bits", mp->m_mode);
Ernie Rael659c2402022-04-24 18:40:28 +01002344
2345 vim_free(mapmode);
2346}
2347
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002348 static void
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002349get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2350{
2351 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002352 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002353 char_u *which;
2354 char_u buf[NUMBUFLEN];
2355 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002356 char_u *alt_keys_buf = NULL;
2357 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002358 char_u *rhs;
2359 int mode;
2360 int abbr = FALSE;
2361 int get_dict = FALSE;
zeertzjq2c8a7eb2022-04-26 21:36:21 +01002362 mapblock_T *mp = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002363 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002364 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002365
2366 // return empty string for failure
2367 rettv->v_type = VAR_STRING;
2368 rettv->vval.v_string = NULL;
2369
2370 keys = tv_get_string(&argvars[0]);
2371 if (*keys == NUL)
2372 return;
2373
2374 if (argvars[1].v_type != VAR_UNKNOWN)
2375 {
2376 which = tv_get_string_buf_chk(&argvars[1], buf);
2377 if (argvars[2].v_type != VAR_UNKNOWN)
2378 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002379 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002380 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002381 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002382 }
2383 }
2384 else
2385 which = (char_u *)"";
2386 if (which == NULL)
2387 return;
2388
2389 mode = get_map_mode(&which, 0);
2390
Bram Moolenaar9c652532020-05-24 13:10:18 +02002391 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2392 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2393 &mp, &buffer_local);
2394 if (did_simplify)
2395 {
2396 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002397 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002398 (void)replace_termcodes(keys, &alt_keys_buf,
2399 flags | REPTERM_NO_SIMPLIFY, NULL);
2400 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2401 &buffer_local);
2402 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002403
2404 if (!get_dict)
2405 {
2406 // Return a string.
2407 if (rhs != NULL)
2408 {
2409 if (*rhs == NUL)
2410 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2411 else
2412 rettv->vval.v_string = str2special_save(rhs, FALSE);
2413 }
2414
2415 }
Bram Moolenaar93a10962022-06-16 11:42:09 +01002416 else if (rettv_dict_alloc(rettv) == OK && rhs != NULL)
Ernie Rael659c2402022-04-24 18:40:28 +01002417 mapblock2dict(mp, rettv->vval.v_dict,
Ernie Rael51d04d12022-05-04 15:40:22 +01002418 did_simplify ? keys_simplified : NULL,
2419 buffer_local, abbr);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002420
2421 vim_free(keys_buf);
2422 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002423}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002424
2425/*
Ernie Rael09661202022-04-25 14:40:44 +01002426 * "maplist()" function
Ernie Rael659c2402022-04-24 18:40:28 +01002427 */
2428 void
Ernie Rael09661202022-04-25 14:40:44 +01002429f_maplist(typval_T *argvars UNUSED, typval_T *rettv)
Ernie Rael659c2402022-04-24 18:40:28 +01002430{
2431 dict_T *d;
2432 mapblock_T *mp;
2433 int buffer_local;
2434 char_u *keys_buf;
2435 int did_simplify;
2436 int hash;
2437 char_u *lhs;
2438 const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Ernie Rael09661202022-04-25 14:40:44 +01002439 int abbr = FALSE;
2440
2441 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2442 return;
2443 if (argvars[0].v_type != VAR_UNKNOWN)
2444 abbr = tv_get_bool(&argvars[0]);
Ernie Rael659c2402022-04-24 18:40:28 +01002445
Bram Moolenaar93a10962022-06-16 11:42:09 +01002446 if (rettv_list_alloc(rettv) == FAIL)
Ernie Rael659c2402022-04-24 18:40:28 +01002447 return;
2448
2449 validate_maphash();
2450
2451 // Do it twice: once for global maps and once for local maps.
2452 for (buffer_local = 0; buffer_local <= 1; ++buffer_local)
2453 {
2454 for (hash = 0; hash < 256; ++hash)
2455 {
Ernie Rael09661202022-04-25 14:40:44 +01002456 if (abbr)
2457 {
2458 if (hash > 0) // there is only one abbr list
2459 break;
2460 if (buffer_local)
2461 mp = curbuf->b_first_abbr;
2462 else
2463 mp = first_abbr;
2464 }
2465 else if (buffer_local)
Ernie Rael659c2402022-04-24 18:40:28 +01002466 mp = curbuf->b_maphash[hash];
2467 else
2468 mp = maphash[hash];
2469 for (; mp; mp = mp->m_next)
2470 {
2471 if (mp->m_simplified)
2472 continue;
2473 if ((d = dict_alloc()) == NULL)
2474 return;
2475 if (list_append_dict(rettv->vval.v_list, d) == FAIL)
2476 return;
2477
2478 keys_buf = NULL;
2479 did_simplify = FALSE;
2480
2481 lhs = str2special_save(mp->m_keys, TRUE);
2482 (void)replace_termcodes(lhs, &keys_buf, flags, &did_simplify);
2483 vim_free(lhs);
2484
2485 mapblock2dict(mp, d,
Ernie Rael51d04d12022-05-04 15:40:22 +01002486 did_simplify ? keys_buf : NULL,
2487 buffer_local, abbr);
Ernie Rael659c2402022-04-24 18:40:28 +01002488 vim_free(keys_buf);
2489 }
2490 }
2491 }
2492}
2493
2494/*
Yegappan Lakshmanan4a155042021-07-30 21:32:45 +02002495 * "maparg()" function
2496 */
2497 void
2498f_maparg(typval_T *argvars, typval_T *rettv)
2499{
2500 if (in_vim9script()
2501 && (check_for_string_arg(argvars, 0) == FAIL
2502 || check_for_opt_string_arg(argvars, 1) == FAIL
2503 || (argvars[1].v_type != VAR_UNKNOWN
2504 && (check_for_opt_bool_arg(argvars, 2) == FAIL
2505 || (argvars[2].v_type != VAR_UNKNOWN
2506 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
2507 return;
2508
2509 get_maparg(argvars, rettv, TRUE);
2510}
2511
2512/*
2513 * "mapcheck()" function
2514 */
2515 void
2516f_mapcheck(typval_T *argvars, typval_T *rettv)
2517{
2518 if (in_vim9script()
2519 && (check_for_string_arg(argvars, 0) == FAIL
2520 || check_for_opt_string_arg(argvars, 1) == FAIL
2521 || (argvars[1].v_type != VAR_UNKNOWN
2522 && check_for_opt_bool_arg(argvars, 2) == FAIL)))
2523 return;
2524
2525 get_maparg(argvars, rettv, FALSE);
2526}
2527
2528/*
Ernie Rael51d04d12022-05-04 15:40:22 +01002529 * Get the mapping mode from the mode string.
2530 * It may contain multiple characters, eg "nox", or "!", or ' '
2531 * Return 0 if there is an error.
2532 */
2533 static int
2534get_map_mode_string(char_u *mode_string, int abbr)
2535{
2536 char_u *p = mode_string;
2537 int mode = 0;
2538 int tmode;
2539 int modec;
Bram Moolenaar24959102022-05-07 20:01:16 +01002540 const int MASK_V = MODE_VISUAL | MODE_SELECT;
2541 const int MASK_MAP = MODE_VISUAL | MODE_SELECT | MODE_NORMAL
2542 | MODE_OP_PENDING;
2543 const int MASK_BANG = MODE_INSERT | MODE_CMDLINE;
Ernie Rael51d04d12022-05-04 15:40:22 +01002544
2545 if (*p == NUL)
2546 p = (char_u *)" "; // compatibility
2547 while ((modec = *p++))
2548 {
2549 switch (modec)
2550 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002551 case 'i': tmode = MODE_INSERT; break;
2552 case 'l': tmode = MODE_LANGMAP; break;
2553 case 'c': tmode = MODE_CMDLINE; break;
2554 case 'n': tmode = MODE_NORMAL; break;
2555 case 'x': tmode = MODE_VISUAL; break;
2556 case 's': tmode = MODE_SELECT; break;
2557 case 'o': tmode = MODE_OP_PENDING; break;
2558 case 't': tmode = MODE_TERMINAL; break;
Ernie Rael51d04d12022-05-04 15:40:22 +01002559 case 'v': tmode = MASK_V; break;
2560 case '!': tmode = MASK_BANG; break;
2561 case ' ': tmode = MASK_MAP; break;
2562 default:
2563 return 0; // error, unknown mode character
2564 }
2565 mode |= tmode;
2566 }
2567 if ((abbr && (mode & ~MASK_BANG) != 0)
2568 || (!abbr && (mode & (mode-1)) != 0 // more than one bit set
2569 && (
2570 // false if multiple bits set in mode and mode is fully
2571 // contained in one mask
2572 !(((mode & MASK_BANG) != 0 && (mode & ~MASK_BANG) == 0)
2573 || ((mode & MASK_MAP) != 0 && (mode & ~MASK_MAP) == 0)))))
2574 return 0;
2575
2576 return mode;
2577}
2578
2579/*
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002580 * "mapset()" function
2581 */
2582 void
2583f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2584{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002585 char_u *which;
2586 int mode;
2587 char_u buf[NUMBUFLEN];
2588 int is_abbr;
2589 dict_T *d;
2590 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002591 char_u *lhsraw;
2592 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002593 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002594 char_u *orig_rhs;
2595 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002596 int noremap;
2597 int expr;
2598 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002599 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002600 scid_T sid;
Bram Moolenaara9528b32022-01-18 20:51:35 +00002601 int scriptversion;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002602 linenr_T lnum;
2603 mapblock_T **map_table = maphash;
2604 mapblock_T **abbr_table = &first_abbr;
2605 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002606 char_u *arg;
Ernie Rael51d04d12022-05-04 15:40:22 +01002607 int dict_only;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002608
Ernie Rael51d04d12022-05-04 15:40:22 +01002609 // If first arg is a dict, then that's the only arg permitted.
2610 dict_only = argvars[0].v_type == VAR_DICT;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002611 if (in_vim9script()
Ernie Rael51d04d12022-05-04 15:40:22 +01002612 && (check_for_string_or_dict_arg(argvars, 0) == FAIL
2613 || (dict_only && check_for_unknown_arg(argvars, 1) == FAIL)
2614 || (!dict_only
2615 && (check_for_string_arg(argvars, 0) == FAIL
2616 || check_for_bool_arg(argvars, 1) == FAIL
2617 || check_for_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002618 return;
2619
Ernie Rael51d04d12022-05-04 15:40:22 +01002620 if (dict_only)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002621 {
Ernie Rael51d04d12022-05-04 15:40:22 +01002622 d = argvars[0].vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002623 which = dict_get_string(d, "mode", FALSE);
2624 is_abbr = dict_get_bool(d, "abbr", -1);
Ernie Rael51d04d12022-05-04 15:40:22 +01002625 if (which == NULL || is_abbr < 0)
2626 {
2627 emsg(_(e_entries_missing_in_mapset_dict_argument));
2628 return;
2629 }
2630 }
2631 else
2632 {
2633 which = tv_get_string_buf_chk(&argvars[0], buf);
2634 if (which == NULL)
2635 return;
2636 is_abbr = (int)tv_get_bool(&argvars[1]);
2637
2638 if (argvars[2].v_type != VAR_DICT)
2639 {
2640 emsg(_(e_dictionary_required));
2641 return;
2642 }
2643 d = argvars[2].vval.v_dict;
2644 }
2645 mode = get_map_mode_string(which, is_abbr);
2646 if (mode == 0)
2647 {
2648 semsg(_(e_illegal_map_mode_string_str), which);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002649 return;
2650 }
Ernie Rael51d04d12022-05-04 15:40:22 +01002651
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002652
2653 // Get the values in the same order as above in get_maparg().
Bram Moolenaard61efa52022-07-23 09:52:04 +01002654 lhs = dict_get_string(d, "lhs", FALSE);
2655 lhsraw = dict_get_string(d, "lhsraw", FALSE);
2656 lhsrawalt = dict_get_string(d, "lhsrawalt", FALSE);
2657 rhs = dict_get_string(d, "rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002658 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002659 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002660 emsg(_(e_entries_missing_in_mapset_dict_argument));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002661 return;
2662 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002663 orig_rhs = rhs;
2664 rhs = replace_termcodes(rhs, &arg_buf,
2665 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002666
Bram Moolenaard61efa52022-07-23 09:52:04 +01002667 noremap = dict_get_number(d, "noremap") ? REMAP_NONE: 0;
2668 if (dict_get_number(d, "script") != 0)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002669 noremap = REMAP_SCRIPT;
Bram Moolenaard61efa52022-07-23 09:52:04 +01002670 expr = dict_get_number(d, "expr") != 0;
2671 silent = dict_get_number(d, "silent") != 0;
2672 sid = dict_get_number(d, "sid");
2673 scriptversion = dict_get_number(d, "scriptversion");
2674 lnum = dict_get_number(d, "lnum");
2675 buffer = dict_get_number(d, "buffer");
2676 nowait = dict_get_number(d, "nowait") != 0;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002677 // mode from the dict is not used
2678
2679 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002680 {
2681 map_table = curbuf->b_maphash;
2682 abbr_table = &curbuf->b_first_abbr;
2683 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002684
2685 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002686 if (buffer)
2687 {
2688 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2689 if (arg == NULL)
2690 return;
2691 STRCPY(arg, "<buffer>");
2692 STRCPY(arg + 8, lhs);
2693 }
2694 else
2695 {
2696 arg = vim_strsave(lhs);
2697 if (arg == NULL)
2698 return;
2699 }
zeertzjq44068e92022-06-16 11:14:55 +01002700 do_map(MAPTYPE_UNMAP, arg, mode, is_abbr);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002701 vim_free(arg);
2702
Bram Moolenaar9c652532020-05-24 13:10:18 +02002703 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002704 nowait, silent, mode, is_abbr, expr, sid, scriptversion, lnum, 0);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002705 if (lhsrawalt != NULL)
2706 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
Bram Moolenaara9528b32022-01-18 20:51:35 +00002707 nowait, silent, mode, is_abbr, expr, sid, scriptversion,
2708 lnum, 1);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002709 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002710}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002711#endif
2712
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002713
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002714#if defined(MSWIN) || defined(MACOS_X)
2715
Bram Moolenaar24959102022-05-07 20:01:16 +01002716# define VIS_SEL (MODE_VISUAL | MODE_SELECT) // abbreviation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002717
2718/*
2719 * Default mappings for some often used keys.
2720 */
2721struct initmap
2722{
2723 char_u *arg;
2724 int mode;
2725};
2726
2727# ifdef FEAT_GUI_MSWIN
2728// Use the Windows (CUA) keybindings. (GUI)
2729static struct initmap initmappings[] =
2730{
2731 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002732 {(char_u *)"<S-Insert> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002733 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002734 {(char_u *)"<S-Insert> <C-R><C-O>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002735 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2736 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2737 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2738 {(char_u *)"<C-X> \"*d", VIS_SEL},
2739 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2740};
2741# endif
2742
2743# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2744// Use the Windows (CUA) keybindings. (Console)
2745static struct initmap cinitmappings[] =
2746{
Bram Moolenaar24959102022-05-07 20:01:16 +01002747 {(char_u *)"\316w <C-Home>", MODE_NORMAL | VIS_SEL},
2748 {(char_u *)"\316w <C-Home>", MODE_INSERT | MODE_CMDLINE},
2749 {(char_u *)"\316u <C-End>", MODE_NORMAL | VIS_SEL},
2750 {(char_u *)"\316u <C-End>", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002751
2752 // paste, copy and cut
2753# ifdef FEAT_CLIPBOARD
Bram Moolenaar24959102022-05-07 20:01:16 +01002754 {(char_u *)"\316\324 \"*P", MODE_NORMAL}, // SHIFT-Insert is "*P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002755 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
Bram Moolenaar24959102022-05-07 20:01:16 +01002756 {(char_u *)"\316\324 \022\017*", MODE_INSERT}, // SHIFT-Insert is ^R^O*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002757 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2758 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2759 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2760 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2761# else
Bram Moolenaar24959102022-05-07 20:01:16 +01002762 {(char_u *)"\316\324 P", MODE_NORMAL}, // SHIFT-Insert is P
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002763 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
Bram Moolenaar24959102022-05-07 20:01:16 +01002764 {(char_u *)"\316\324 \022\017\"", MODE_INSERT}, // SHIFT-Insert is ^R^O"
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002765 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2766 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2767 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2768# endif
2769};
2770# endif
2771
2772# if defined(MACOS_X)
2773static struct initmap initmappings[] =
2774{
2775 // Use the Standard MacOS binding.
2776 // paste, copy and cut
Bram Moolenaar24959102022-05-07 20:01:16 +01002777 {(char_u *)"<D-v> \"*P", MODE_NORMAL},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002778 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar24959102022-05-07 20:01:16 +01002779 {(char_u *)"<D-v> <C-R>*", MODE_INSERT | MODE_CMDLINE},
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002780 {(char_u *)"<D-c> \"*y", VIS_SEL},
2781 {(char_u *)"<D-x> \"*d", VIS_SEL},
2782 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2783};
2784# endif
2785
2786# undef VIS_SEL
2787#endif
2788
2789/*
2790 * Set up default mappings.
2791 */
2792 void
2793init_mappings(void)
2794{
2795#if defined(MSWIN) || defined(MACOS_X)
2796 int i;
2797
2798# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2799# ifdef VIMDLL
2800 if (!gui.starting)
2801# endif
2802 {
K.Takataeeec2542021-06-02 13:28:16 +02002803 for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002804 add_map(cinitmappings[i].arg, cinitmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002805 }
2806# endif
2807# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
K.Takataeeec2542021-06-02 13:28:16 +02002808 for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
zeertzjq44068e92022-06-16 11:14:55 +01002809 add_map(initmappings[i].arg, initmappings[i].mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002810# endif
2811#endif
2812}
2813
2814#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2815 || defined(PROTO)
2816/*
2817 * Add a mapping "map" for mode "mode".
zeertzjq44068e92022-06-16 11:14:55 +01002818 * When "nore" is TRUE use MAPTYPE_NOREMAP.
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002819 * Need to put string in allocated memory, because do_map() will modify it.
2820 */
2821 void
zeertzjq44068e92022-06-16 11:14:55 +01002822add_map(char_u *map, int mode, int nore)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002823{
2824 char_u *s;
2825 char_u *cpo_save = p_cpo;
2826
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002827 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002828 s = vim_strsave(map);
2829 if (s != NULL)
2830 {
zeertzjq44068e92022-06-16 11:14:55 +01002831 (void)do_map(nore ? MAPTYPE_NOREMAP : MAPTYPE_MAP, s, mode, FALSE);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002832 vim_free(s);
2833 }
2834 p_cpo = cpo_save;
2835}
2836#endif
2837
Bram Moolenaare677df82019-09-02 22:31:11 +02002838#if defined(FEAT_LANGMAP) || defined(PROTO)
2839/*
2840 * Any character has an equivalent 'langmap' character. This is used for
2841 * keyboards that have a special language mode that sends characters above
2842 * 128 (although other characters can be translated too). The "to" field is a
2843 * Vim command character. This avoids having to switch the keyboard back to
2844 * ASCII mode when leaving Insert mode.
2845 *
2846 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2847 * commands.
2848 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2849 * same as langmap_mapchar[] for characters >= 256.
2850 *
2851 * Use growarray for 'langmap' chars >= 256
2852 */
2853typedef struct
2854{
2855 int from;
2856 int to;
2857} langmap_entry_T;
2858
2859static garray_T langmap_mapga;
2860
2861/*
2862 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2863 * field. If not found insert a new entry at the appropriate location.
2864 */
2865 static void
2866langmap_set_entry(int from, int to)
2867{
2868 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2869 int a = 0;
2870 int b = langmap_mapga.ga_len;
2871
2872 // Do a binary search for an existing entry.
2873 while (a != b)
2874 {
2875 int i = (a + b) / 2;
2876 int d = entries[i].from - from;
2877
2878 if (d == 0)
2879 {
2880 entries[i].to = to;
2881 return;
2882 }
2883 if (d < 0)
2884 a = i + 1;
2885 else
2886 b = i;
2887 }
2888
2889 if (ga_grow(&langmap_mapga, 1) != OK)
2890 return; // out of memory
2891
2892 // insert new entry at position "a"
2893 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2894 mch_memmove(entries + 1, entries,
2895 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2896 ++langmap_mapga.ga_len;
2897 entries[0].from = from;
2898 entries[0].to = to;
2899}
2900
2901/*
2902 * Apply 'langmap' to multi-byte character "c" and return the result.
2903 */
2904 int
2905langmap_adjust_mb(int c)
2906{
2907 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2908 int a = 0;
2909 int b = langmap_mapga.ga_len;
2910
2911 while (a != b)
2912 {
2913 int i = (a + b) / 2;
2914 int d = entries[i].from - c;
2915
2916 if (d == 0)
2917 return entries[i].to; // found matching entry
2918 if (d < 0)
2919 a = i + 1;
2920 else
2921 b = i;
2922 }
2923 return c; // no entry found, return "c" unmodified
2924}
2925
2926 void
2927langmap_init(void)
2928{
2929 int i;
2930
2931 for (i = 0; i < 256; i++)
2932 langmap_mapchar[i] = i; // we init with a one-to-one map
2933 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2934}
2935
2936/*
2937 * Called when langmap option is set; the language map can be
2938 * changed at any time!
2939 */
2940 void
2941langmap_set(void)
2942{
2943 char_u *p;
2944 char_u *p2;
2945 int from, to;
2946
2947 ga_clear(&langmap_mapga); // clear the previous map first
2948 langmap_init(); // back to one-to-one map
2949
2950 for (p = p_langmap; p[0] != NUL; )
2951 {
2952 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2953 MB_PTR_ADV(p2))
2954 {
2955 if (p2[0] == '\\' && p2[1] != NUL)
2956 ++p2;
2957 }
2958 if (p2[0] == ';')
2959 ++p2; // abcd;ABCD form, p2 points to A
2960 else
2961 p2 = NULL; // aAbBcCdD form, p2 is NULL
2962 while (p[0])
2963 {
2964 if (p[0] == ',')
2965 {
2966 ++p;
2967 break;
2968 }
2969 if (p[0] == '\\' && p[1] != NUL)
2970 ++p;
2971 from = (*mb_ptr2char)(p);
2972 to = NUL;
2973 if (p2 == NULL)
2974 {
2975 MB_PTR_ADV(p);
2976 if (p[0] != ',')
2977 {
2978 if (p[0] == '\\')
2979 ++p;
2980 to = (*mb_ptr2char)(p);
2981 }
2982 }
2983 else
2984 {
2985 if (p2[0] != ',')
2986 {
2987 if (p2[0] == '\\')
2988 ++p2;
2989 to = (*mb_ptr2char)(p2);
2990 }
2991 }
2992 if (to == NUL)
2993 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002994 semsg(_(e_langmap_matching_character_missing_for_str),
Bram Moolenaare677df82019-09-02 22:31:11 +02002995 transchar(from));
2996 return;
2997 }
2998
2999 if (from >= 256)
3000 langmap_set_entry(from, to);
3001 else
3002 langmap_mapchar[from & 255] = to;
3003
3004 // Advance to next pair
3005 MB_PTR_ADV(p);
3006 if (p2 != NULL)
3007 {
3008 MB_PTR_ADV(p2);
3009 if (*p == ';')
3010 {
3011 p = p2;
3012 if (p[0] != NUL)
3013 {
3014 if (p[0] != ',')
3015 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003016 semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
Bram Moolenaare677df82019-09-02 22:31:11 +02003017 return;
3018 }
3019 ++p;
3020 }
3021 break;
3022 }
3023 }
3024 }
3025 }
3026}
3027#endif
3028
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003029 static void
3030do_exmap(exarg_T *eap, int isabbrev)
3031{
3032 int mode;
3033 char_u *cmdp;
3034
3035 cmdp = eap->cmd;
3036 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
3037
zeertzjq44068e92022-06-16 11:14:55 +01003038 switch (do_map(*cmdp == 'n' ? MAPTYPE_NOREMAP
3039 : *cmdp == 'u' ? MAPTYPE_UNMAP : MAPTYPE_MAP,
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003040 eap->arg, mode, isabbrev))
3041 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003042 case 1: emsg(_(e_invalid_argument));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003043 break;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003044 case 2: emsg((isabbrev ? _(e_no_such_abbreviation)
3045 : _(e_no_such_mapping)));
Bram Moolenaarb66bab32019-08-01 14:28:24 +02003046 break;
3047 }
3048}
3049
3050/*
3051 * ":abbreviate" and friends.
3052 */
3053 void
3054ex_abbreviate(exarg_T *eap)
3055{
3056 do_exmap(eap, TRUE); // almost the same as mapping
3057}
3058
3059/*
3060 * ":map" and friends.
3061 */
3062 void
3063ex_map(exarg_T *eap)
3064{
3065 // If we are sourcing .exrc or .vimrc in current directory we
3066 // print the mappings for security reasons.
3067 if (secure)
3068 {
3069 secure = 2;
3070 msg_outtrans(eap->cmd);
3071 msg_putchar('\n');
3072 }
3073 do_exmap(eap, FALSE);
3074}
3075
3076/*
3077 * ":unmap" and friends.
3078 */
3079 void
3080ex_unmap(exarg_T *eap)
3081{
3082 do_exmap(eap, FALSE);
3083}
3084
3085/*
3086 * ":mapclear" and friends.
3087 */
3088 void
3089ex_mapclear(exarg_T *eap)
3090{
3091 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
3092}
3093
3094/*
3095 * ":abclear" and friends.
3096 */
3097 void
3098ex_abclear(exarg_T *eap)
3099{
3100 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
3101}