blob: 2e792feaa0fae8eb5692d5fc98f8549c41e47086 [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/*
11 * map.c: functions for maps and abbreviations
12 */
13
14#include "vim.h"
15
16/*
17 * List used for abbreviations.
18 */
19static mapblock_T *first_abbr = NULL; // first entry in abbrlist
20
21/*
22 * Each mapping is put in one of the 256 hash lists, to speed up finding it.
23 */
24static mapblock_T *(maphash[256]);
25static int maphash_valid = FALSE;
26
27/*
28 * Make a hash value for a mapping.
29 * "mode" is the lower 4 bits of the State for the mapping.
30 * "c1" is the first character of the "lhs".
31 * Returns a value between 0 and 255, index in maphash.
32 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode.
33 */
34#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING + TERMINAL)) ? (c1) : ((c1) ^ 0x80))
35
36/*
37 * Get the start of the hashed map list for "state" and first character "c".
38 */
39 mapblock_T *
40get_maphash_list(int state, int c)
41{
42 return maphash[MAP_HASH(state, c)];
43}
44
45/*
46 * Get the buffer-local hashed map list for "state" and first character "c".
47 */
48 mapblock_T *
49get_buf_maphash_list(int state, int c)
50{
51 return curbuf->b_maphash[MAP_HASH(state, c)];
52}
53
54 int
55is_maphash_valid(void)
56{
57 return maphash_valid;
58}
59
60/*
61 * Initialize maphash[] for first use.
62 */
63 static void
64validate_maphash(void)
65{
66 if (!maphash_valid)
67 {
Bram Moolenaara80faa82020-04-12 19:37:17 +020068 CLEAR_FIELD(maphash);
Bram Moolenaarb66bab32019-08-01 14:28:24 +020069 maphash_valid = TRUE;
70 }
71}
72
73/*
74 * Delete one entry from the abbrlist or maphash[].
75 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
76 */
77 static void
78map_free(mapblock_T **mpp)
79{
80 mapblock_T *mp;
81
82 mp = *mpp;
83 vim_free(mp->m_keys);
84 vim_free(mp->m_str);
85 vim_free(mp->m_orig_str);
86 *mpp = mp->m_next;
87 vim_free(mp);
88}
89
90/*
91 * Return characters to represent the map mode in an allocated string.
92 * Returns NULL when out of memory.
93 */
94 static char_u *
95map_mode_to_chars(int mode)
96{
97 garray_T mapmode;
98
99 ga_init2(&mapmode, 1, 7);
100
101 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
102 ga_append(&mapmode, '!'); // :map!
103 else if (mode & INSERT)
104 ga_append(&mapmode, 'i'); // :imap
105 else if (mode & LANGMAP)
106 ga_append(&mapmode, 'l'); // :lmap
107 else if (mode & CMDLINE)
108 ga_append(&mapmode, 'c'); // :cmap
109 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
110 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
111 ga_append(&mapmode, ' '); // :map
112 else
113 {
114 if (mode & NORMAL)
115 ga_append(&mapmode, 'n'); // :nmap
116 if (mode & OP_PENDING)
117 ga_append(&mapmode, 'o'); // :omap
118 if (mode & TERMINAL)
119 ga_append(&mapmode, 't'); // :tmap
120 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
121 ga_append(&mapmode, 'v'); // :vmap
122 else
123 {
124 if (mode & VISUAL)
125 ga_append(&mapmode, 'x'); // :xmap
126 if (mode & SELECTMODE)
127 ga_append(&mapmode, 's'); // :smap
128 }
129 }
130
131 ga_append(&mapmode, NUL);
132 return (char_u *)mapmode.ga_data;
133}
134
135 static void
136showmap(
137 mapblock_T *mp,
138 int local) // TRUE for buffer-local map
139{
140 int len = 1;
141 char_u *mapchars;
142
143 if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
144 return;
145
146 if (msg_didout || msg_silent != 0)
147 {
148 msg_putchar('\n');
149 if (got_int) // 'q' typed at MORE prompt
150 return;
151 }
152
153 mapchars = map_mode_to_chars(mp->m_mode);
154 if (mapchars != NULL)
155 {
156 msg_puts((char *)mapchars);
157 len = (int)STRLEN(mapchars);
158 vim_free(mapchars);
159 }
160
161 while (++len <= 3)
162 msg_putchar(' ');
163
164 // Display the LHS. Get length of what we write.
165 len = msg_outtrans_special(mp->m_keys, TRUE, 0);
166 do
167 {
168 msg_putchar(' '); // padd with blanks
169 ++len;
170 } while (len < 12);
171
172 if (mp->m_noremap == REMAP_NONE)
173 msg_puts_attr("*", HL_ATTR(HLF_8));
174 else if (mp->m_noremap == REMAP_SCRIPT)
175 msg_puts_attr("&", HL_ATTR(HLF_8));
176 else
177 msg_putchar(' ');
178
179 if (local)
180 msg_putchar('@');
181 else
182 msg_putchar(' ');
183
184 // Use FALSE below if we only want things like <Up> to show up as such on
185 // the rhs, and not M-x etc, TRUE gets both -- webb
186 if (*mp->m_str == NUL)
187 msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
188 else
189 {
190 // Remove escaping of CSI, because "m_str" is in a format to be used
191 // as typeahead.
192 char_u *s = vim_strsave(mp->m_str);
193 if (s != NULL)
194 {
195 vim_unescape_csi(s);
196 msg_outtrans_special(s, FALSE, 0);
197 vim_free(s);
198 }
199 }
200#ifdef FEAT_EVAL
201 if (p_verbose > 0)
202 last_set_msg(mp->m_script_ctx);
203#endif
204 out_flush(); // show one line at a time
205}
206
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200207 static int
208map_add(
209 mapblock_T **map_table,
210 mapblock_T **abbr_table,
211 char_u *keys,
212 char_u *rhs,
213 char_u *orig_rhs,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200214 int noremap,
215 int nowait,
216 int silent,
217 int mode,
218 int is_abbr,
219#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200220 int expr,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200221 scid_T sid, // -1 to use current_sctx
222 linenr_T lnum,
223#endif
224 int simplified)
225{
226 mapblock_T *mp = ALLOC_ONE(mapblock_T);
227
228 if (mp == NULL)
229 return FAIL;
230
231 // If CTRL-C has been mapped, don't always use it for Interrupting.
232 if (*keys == Ctrl_C)
233 {
234 if (map_table == curbuf->b_maphash)
235 curbuf->b_mapped_ctrl_c |= mode;
236 else
237 mapped_ctrl_c |= mode;
238 }
239
240 mp->m_keys = vim_strsave(keys);
241 mp->m_str = vim_strsave(rhs);
242 mp->m_orig_str = vim_strsave(orig_rhs);
243 if (mp->m_keys == NULL || mp->m_str == NULL)
244 {
245 vim_free(mp->m_keys);
246 vim_free(mp->m_str);
247 vim_free(mp->m_orig_str);
248 vim_free(mp);
249 return FAIL;
250 }
251 mp->m_keylen = (int)STRLEN(mp->m_keys);
252 mp->m_noremap = noremap;
253 mp->m_nowait = nowait;
254 mp->m_silent = silent;
255 mp->m_mode = mode;
256 mp->m_simplified = simplified;
257#ifdef FEAT_EVAL
258 mp->m_expr = expr;
259 if (sid >= 0)
260 {
261 mp->m_script_ctx.sc_sid = sid;
262 mp->m_script_ctx.sc_lnum = lnum;
263 }
264 else
265 {
266 mp->m_script_ctx = current_sctx;
267 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
268 }
269#endif
270
271 // add the new entry in front of the abbrlist or maphash[] list
272 if (is_abbr)
273 {
274 mp->m_next = *abbr_table;
275 *abbr_table = mp;
276 }
277 else
278 {
279 int n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
280
281 mp->m_next = map_table[n];
282 map_table[n] = mp;
283 }
284 return OK;
285}
286
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200287/*
288 * map[!] : show all key mappings
289 * map[!] {lhs} : show key mapping for {lhs}
290 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
291 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
292 * unmap[!] {lhs} : remove key mapping for {lhs}
293 * abbr : show all abbreviations
294 * abbr {lhs} : show abbreviations for {lhs}
295 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
296 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
297 * unabbr {lhs} : remove abbreviation for {lhs}
298 *
299 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
300 *
301 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
302 * it will be modified.
303 *
304 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
305 * for :map! mode is INSERT + CMDLINE
306 * for :cmap mode is CMDLINE
307 * for :imap mode is INSERT
308 * for :lmap mode is LANGMAP
309 * for :nmap mode is NORMAL
310 * for :vmap mode is VISUAL + SELECTMODE
311 * for :xmap mode is VISUAL
312 * for :smap mode is SELECTMODE
313 * for :omap mode is OP_PENDING
314 * for :tmap mode is TERMINAL
315 *
316 * for :abbr mode is INSERT + CMDLINE
317 * for :iabbr mode is INSERT
318 * for :cabbr mode is CMDLINE
319 *
320 * Return 0 for success
321 * 1 for invalid arguments
322 * 2 for no match
323 * 4 for out of mem
324 * 5 for entry not unique
325 */
326 int
327do_map(
328 int maptype,
329 char_u *arg,
330 int mode,
331 int abbrev) // not a mapping but an abbreviation
332{
333 char_u *keys;
334 mapblock_T *mp, **mpp;
335 char_u *rhs;
336 char_u *p;
337 int n;
338 int len = 0; // init for GCC
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200339 int hasarg;
340 int haskey;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200341 int do_print;
342 int keyround;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200343 char_u *keys_buf = NULL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200344 char_u *alt_keys_buf = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200345 char_u *arg_buf = NULL;
346 int retval = 0;
347 int do_backslash;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200348 mapblock_T **abbr_table;
349 mapblock_T **map_table;
350 int unique = FALSE;
351 int nowait = FALSE;
352 int silent = FALSE;
353 int special = FALSE;
354#ifdef FEAT_EVAL
355 int expr = FALSE;
356#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200357 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200358 int noremap;
359 char_u *orig_rhs;
360
361 keys = arg;
362 map_table = maphash;
363 abbr_table = &first_abbr;
364
365 // For ":noremap" don't remap, otherwise do remap.
366 if (maptype == 2)
367 noremap = REMAP_NONE;
368 else
369 noremap = REMAP_YES;
370
371 // Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
372 // any order.
373 for (;;)
374 {
375 // Check for "<buffer>": mapping local to buffer.
376 if (STRNCMP(keys, "<buffer>", 8) == 0)
377 {
378 keys = skipwhite(keys + 8);
379 map_table = curbuf->b_maphash;
380 abbr_table = &curbuf->b_first_abbr;
381 continue;
382 }
383
384 // Check for "<nowait>": don't wait for more characters.
385 if (STRNCMP(keys, "<nowait>", 8) == 0)
386 {
387 keys = skipwhite(keys + 8);
388 nowait = TRUE;
389 continue;
390 }
391
392 // Check for "<silent>": don't echo commands.
393 if (STRNCMP(keys, "<silent>", 8) == 0)
394 {
395 keys = skipwhite(keys + 8);
396 silent = TRUE;
397 continue;
398 }
399
400 // Check for "<special>": accept special keys in <>
401 if (STRNCMP(keys, "<special>", 9) == 0)
402 {
403 keys = skipwhite(keys + 9);
404 special = TRUE;
405 continue;
406 }
407
408#ifdef FEAT_EVAL
409 // Check for "<script>": remap script-local mappings only
410 if (STRNCMP(keys, "<script>", 8) == 0)
411 {
412 keys = skipwhite(keys + 8);
413 noremap = REMAP_SCRIPT;
414 continue;
415 }
416
417 // Check for "<expr>": {rhs} is an expression.
418 if (STRNCMP(keys, "<expr>", 6) == 0)
419 {
420 keys = skipwhite(keys + 6);
421 expr = TRUE;
422 continue;
423 }
424#endif
425 // Check for "<unique>": don't overwrite an existing mapping.
426 if (STRNCMP(keys, "<unique>", 8) == 0)
427 {
428 keys = skipwhite(keys + 8);
429 unique = TRUE;
430 continue;
431 }
432 break;
433 }
434
435 validate_maphash();
436
437 // Find end of keys and skip CTRL-Vs (and backslashes) in it.
438 // Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
439 // with :unmap white space is included in the keys, no argument possible.
440 p = keys;
441 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
442 while (*p && (maptype == 1 || !VIM_ISWHITE(*p)))
443 {
444 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
445 p[1] != NUL)
446 ++p; // skip CTRL-V or backslash
447 ++p;
448 }
449 if (*p != NUL)
450 *p++ = NUL;
451
452 p = skipwhite(p);
453 rhs = p;
454 hasarg = (*rhs != NUL);
455 haskey = (*keys != NUL);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200456 do_print = !haskey || (maptype != 1 && !hasarg);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200457
458 // check for :unmap without argument
459 if (maptype == 1 && !haskey)
460 {
461 retval = 1;
462 goto theend;
463 }
464
465 // If mapping has been given as ^V<C_UP> say, then replace the term codes
466 // with the appropriate two bytes. If it is a shifted special key, unshift
467 // it too, giving another two bytes.
468 // replace_termcodes() may move the result to allocated memory, which
469 // needs to be freed later (*keys_buf and *arg_buf).
470 // replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
Bram Moolenaar459fd782019-10-13 16:43:39 +0200471 // If something like <C-H> is simplified to 0x08 then mark it as simplified
472 // and also add a n entry with a modifier, which will work when
473 // modifyOtherKeys is working.
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200474 if (haskey)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200475 {
476 char_u *new_keys;
477 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
478
479 if (special)
480 flags |= REPTERM_SPECIAL;
481 new_keys = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
482 if (did_simplify)
483 (void)replace_termcodes(keys, &alt_keys_buf,
484 flags | REPTERM_NO_SIMPLIFY, NULL);
485 keys = new_keys;
486 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200487 orig_rhs = rhs;
488 if (hasarg)
489 {
490 if (STRICMP(rhs, "<nop>") == 0) // "<Nop>" means nothing
491 rhs = (char_u *)"";
492 else
Bram Moolenaar459fd782019-10-13 16:43:39 +0200493 rhs = replace_termcodes(rhs, &arg_buf,
494 REPTERM_DO_LT | (special ? REPTERM_SPECIAL : 0), NULL);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200495 }
496
Bram Moolenaar459fd782019-10-13 16:43:39 +0200497 /*
498 * The following is done twice if we have two versions of keys:
499 * "alt_keys_buf" is not NULL.
500 */
501 for (keyround = 1; keyround <= 2; ++keyround)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200502 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200503 int did_it = FALSE;
504 int did_local = FALSE;
505 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
529 if (abbrev && maptype != 1)
530 {
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
585 && haskey && hasarg && maptype != 1)
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)
606 semsg(_(
607 "E224: global abbreviation already exists for %s"),
608 mp->m_keys);
609 else
610 semsg(_(
611 "E225: global mapping already exists for %s"),
612 mp->m_keys);
613 retval = 5;
614 goto theend;
615 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200616 }
617 }
618 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200619
Bram Moolenaar459fd782019-10-13 16:43:39 +0200620 // When listing global mappings, also list buffer-local ones here.
621 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
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
662 // an entry with a matching 'to' part. This was done to allow ":ab foo
663 // bar" to be unmapped by typing ":unab foo", where "foo" will be
664 // replaced by "bar" because of the abbreviation.
665 for (round = 0; (round == 0 || maptype == 1) && round <= 1
666 && !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 {
710 if (maptype == 1)
711 {
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 }
723 // We reset the indicated mode bits. If nothing
724 // is left the entry is deleted below.
725 mp->m_mode &= ~mode;
726 did_it = TRUE; // remember we did something
727 }
728 else if (!hasarg) // show matching entry
729 {
Bram Moolenaarfafb4b12019-10-16 18:34:57 +0200730 if (!mp->m_simplified)
731 {
732 showmap(mp, map_table != maphash);
733 did_it = TRUE;
734 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200735 }
736 else if (n != len) // new entry is ambiguous
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200737 {
738 mpp = &(mp->m_next);
739 continue;
740 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200741 else if (unique)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200742 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200743 if (abbrev)
744 semsg(_(
745 "E226: abbreviation already exists for %s"),
746 p);
747 else
748 semsg(_(
749 "E227: mapping already exists for %s"),
750 p);
751 retval = 5;
752 goto theend;
753 }
754 else
755 {
756 // new rhs for existing entry
757 mp->m_mode &= ~mode; // remove mode bits
758 if (mp->m_mode == 0 && !did_it) // reuse entry
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200759 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200760 char_u *newstr = vim_strsave(rhs);
761
762 if (newstr == NULL)
763 {
764 retval = 4; // no mem
765 goto theend;
766 }
767 vim_free(mp->m_str);
768 mp->m_str = newstr;
769 vim_free(mp->m_orig_str);
770 mp->m_orig_str = vim_strsave(orig_rhs);
771 mp->m_noremap = noremap;
772 mp->m_nowait = nowait;
773 mp->m_silent = silent;
774 mp->m_mode = mode;
775 mp->m_simplified =
776 did_simplify && keyround == 1;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200777#ifdef FEAT_EVAL
Bram Moolenaar459fd782019-10-13 16:43:39 +0200778 mp->m_expr = expr;
779 mp->m_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100780 mp->m_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200781#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +0200782 did_it = TRUE;
783 }
784 }
785 if (mp->m_mode == 0) // entry can be deleted
786 {
787 map_free(mpp);
788 continue; // continue with *mpp
789 }
790
791 // May need to put this entry into another hash
792 // list.
793 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
794 if (!abbrev && new_hash != hash)
795 {
796 *mpp = mp->m_next;
797 mp->m_next = map_table[new_hash];
798 map_table[new_hash] = mp;
799
800 continue; // continue with *mpp
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200801 }
802 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200803 }
Bram Moolenaar459fd782019-10-13 16:43:39 +0200804 mpp = &(mp->m_next);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200805 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200806 }
807 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200808
Bram Moolenaar459fd782019-10-13 16:43:39 +0200809 if (maptype == 1)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200810 {
Bram Moolenaar459fd782019-10-13 16:43:39 +0200811 // delete entry
812 if (!did_it)
813 retval = 2; // no match
814 else if (*keys == Ctrl_C)
815 {
816 // If CTRL-C has been unmapped, reuse it for Interrupting.
817 if (map_table == curbuf->b_maphash)
818 curbuf->b_mapped_ctrl_c &= ~mode;
819 else
820 mapped_ctrl_c &= ~mode;
821 }
822 continue;
823 }
824
825 if (!haskey || !hasarg)
826 {
827 // print entries
828 if (!did_it && !did_local)
829 {
830 if (abbrev)
831 msg(_("No abbreviation found"));
832 else
833 msg(_("No mapping found"));
834 }
835 goto theend; // listing finished
836 }
837
838 if (did_it)
839 continue; // have added the new entry already
840
841 // Get here when adding a new entry to the maphash[] list or abbrlist.
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200842 if (map_add(map_table, abbr_table, keys, rhs, orig_rhs,
843 noremap, nowait, silent, mode, abbrev,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200844#ifdef FEAT_EVAL
Bram Moolenaar5a80f8a2020-05-22 13:38:18 +0200845 expr, /* sid */ -1, /* lnum */ 0,
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200846#endif
847 did_simplify && keyround == 1) == FAIL)
Bram Moolenaar459fd782019-10-13 16:43:39 +0200848 {
849 retval = 4; // no mem
850 goto theend;
851 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200852 }
853
854theend:
855 vim_free(keys_buf);
Bram Moolenaar459fd782019-10-13 16:43:39 +0200856 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200857 vim_free(arg_buf);
858 return retval;
859}
860
861/*
862 * Get the mapping mode from the command name.
863 */
864 static int
865get_map_mode(char_u **cmdp, int forceit)
866{
867 char_u *p;
868 int modec;
869 int mode;
870
871 p = *cmdp;
872 modec = *p++;
873 if (modec == 'i')
874 mode = INSERT; // :imap
875 else if (modec == 'l')
876 mode = LANGMAP; // :lmap
877 else if (modec == 'c')
878 mode = CMDLINE; // :cmap
879 else if (modec == 'n' && *p != 'o') // avoid :noremap
880 mode = NORMAL; // :nmap
881 else if (modec == 'v')
882 mode = VISUAL + SELECTMODE; // :vmap
883 else if (modec == 'x')
884 mode = VISUAL; // :xmap
885 else if (modec == 's')
886 mode = SELECTMODE; // :smap
887 else if (modec == 'o')
888 mode = OP_PENDING; // :omap
889 else if (modec == 't')
890 mode = TERMINAL; // :tmap
891 else
892 {
893 --p;
894 if (forceit)
895 mode = INSERT + CMDLINE; // :map !
896 else
897 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;// :map
898 }
899
900 *cmdp = p;
901 return mode;
902}
903
904/*
905 * Clear all mappings or abbreviations.
906 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
907 */
908 static void
909map_clear(
910 char_u *cmdp,
911 char_u *arg UNUSED,
912 int forceit,
913 int abbr)
914{
915 int mode;
916 int local;
917
918 local = (STRCMP(arg, "<buffer>") == 0);
919 if (!local && *arg != NUL)
920 {
921 emsg(_(e_invarg));
922 return;
923 }
924
925 mode = get_map_mode(&cmdp, forceit);
926 map_clear_int(curbuf, mode, local, abbr);
927}
928
929/*
930 * Clear all mappings in "mode".
931 */
932 void
933map_clear_int(
934 buf_T *buf, // buffer for local mappings
935 int mode, // mode in which to delete
936 int local, // TRUE for buffer-local mappings
937 int abbr) // TRUE for abbreviations
938{
939 mapblock_T *mp, **mpp;
940 int hash;
941 int new_hash;
942
943 validate_maphash();
944
945 for (hash = 0; hash < 256; ++hash)
946 {
947 if (abbr)
948 {
949 if (hash > 0) // there is only one abbrlist
950 break;
951 if (local)
952 mpp = &buf->b_first_abbr;
953 else
954 mpp = &first_abbr;
955 }
956 else
957 {
958 if (local)
959 mpp = &buf->b_maphash[hash];
960 else
961 mpp = &maphash[hash];
962 }
963 while (*mpp != NULL)
964 {
965 mp = *mpp;
966 if (mp->m_mode & mode)
967 {
968 mp->m_mode &= ~mode;
969 if (mp->m_mode == 0) // entry can be deleted
970 {
971 map_free(mpp);
972 continue;
973 }
974 // May need to put this entry into another hash list.
975 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
976 if (!abbr && new_hash != hash)
977 {
978 *mpp = mp->m_next;
979 if (local)
980 {
981 mp->m_next = buf->b_maphash[new_hash];
982 buf->b_maphash[new_hash] = mp;
983 }
984 else
985 {
986 mp->m_next = maphash[new_hash];
987 maphash[new_hash] = mp;
988 }
989 continue; // continue with *mpp
990 }
991 }
992 mpp = &(mp->m_next);
993 }
994 }
995}
996
997#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200998 int
Bram Moolenaar581ba392019-09-03 22:08:33 +0200999mode_str2flags(char_u *modechars)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001000{
1001 int mode = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001002
1003 if (vim_strchr(modechars, 'n') != NULL)
1004 mode |= NORMAL;
1005 if (vim_strchr(modechars, 'v') != NULL)
1006 mode |= VISUAL + SELECTMODE;
1007 if (vim_strchr(modechars, 'x') != NULL)
1008 mode |= VISUAL;
1009 if (vim_strchr(modechars, 's') != NULL)
1010 mode |= SELECTMODE;
1011 if (vim_strchr(modechars, 'o') != NULL)
1012 mode |= OP_PENDING;
1013 if (vim_strchr(modechars, 'i') != NULL)
1014 mode |= INSERT;
1015 if (vim_strchr(modechars, 'l') != NULL)
1016 mode |= LANGMAP;
1017 if (vim_strchr(modechars, 'c') != NULL)
1018 mode |= CMDLINE;
1019
Bram Moolenaar581ba392019-09-03 22:08:33 +02001020 return mode;
1021}
1022
1023/*
1024 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
1025 * Recognize termcap codes in "str".
1026 * Also checks mappings local to the current buffer.
1027 */
1028 int
1029map_to_exists(char_u *str, char_u *modechars, int abbr)
1030{
1031 char_u *rhs;
1032 char_u *buf;
1033 int retval;
1034
Bram Moolenaar459fd782019-10-13 16:43:39 +02001035 rhs = replace_termcodes(str, &buf, REPTERM_DO_LT, NULL);
Bram Moolenaar581ba392019-09-03 22:08:33 +02001036
1037 retval = map_to_exists_mode(rhs, mode_str2flags(modechars), abbr);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001038 vim_free(buf);
1039
1040 return retval;
1041}
1042#endif
1043
1044/*
1045 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
1046 * Also checks mappings local to the current buffer.
1047 */
1048 int
1049map_to_exists_mode(char_u *rhs, int mode, int abbr)
1050{
1051 mapblock_T *mp;
1052 int hash;
1053 int exp_buffer = FALSE;
1054
1055 validate_maphash();
1056
1057 // Do it twice: once for global maps and once for local maps.
1058 for (;;)
1059 {
1060 for (hash = 0; hash < 256; ++hash)
1061 {
1062 if (abbr)
1063 {
1064 if (hash > 0) // there is only one abbr list
1065 break;
1066 if (exp_buffer)
1067 mp = curbuf->b_first_abbr;
1068 else
1069 mp = first_abbr;
1070 }
1071 else if (exp_buffer)
1072 mp = curbuf->b_maphash[hash];
1073 else
1074 mp = maphash[hash];
1075 for (; mp; mp = mp->m_next)
1076 {
1077 if ((mp->m_mode & mode)
1078 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
1079 return TRUE;
1080 }
1081 }
1082 if (exp_buffer)
1083 break;
1084 exp_buffer = TRUE;
1085 }
1086
1087 return FALSE;
1088}
1089
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001090/*
1091 * Used below when expanding mapping/abbreviation names.
1092 */
1093static int expand_mapmodes = 0;
1094static int expand_isabbrev = 0;
1095static int expand_buffer = FALSE;
1096
1097/*
Bram Moolenaar7f51bbe2020-01-24 20:21:19 +01001098 * Translate an internal mapping/abbreviation representation into the
1099 * corresponding external one recognized by :map/:abbrev commands.
1100 * Respects the current B/k/< settings of 'cpoption'.
1101 *
1102 * This function is called when expanding mappings/abbreviations on the
1103 * command-line.
1104 *
1105 * It uses a growarray to build the translation string since the latter can be
1106 * wider than the original description. The caller has to free the string
1107 * afterwards.
1108 *
1109 * Returns NULL when there is a problem.
1110 */
1111 static char_u *
1112translate_mapping(char_u *str)
1113{
1114 garray_T ga;
1115 int c;
1116 int modifiers;
1117 int cpo_bslash;
1118 int cpo_special;
1119
1120 ga_init(&ga);
1121 ga.ga_itemsize = 1;
1122 ga.ga_growsize = 40;
1123
1124 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1125 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1126
1127 for (; *str; ++str)
1128 {
1129 c = *str;
1130 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1131 {
1132 modifiers = 0;
1133 if (str[1] == KS_MODIFIER)
1134 {
1135 str++;
1136 modifiers = *++str;
1137 c = *++str;
1138 }
1139 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1140 {
1141 if (cpo_special)
1142 {
1143 ga_clear(&ga);
1144 return NULL;
1145 }
1146 c = TO_SPECIAL(str[1], str[2]);
1147 if (c == K_ZERO) // display <Nul> as ^@
1148 c = NUL;
1149 str += 2;
1150 }
1151 if (IS_SPECIAL(c) || modifiers) // special key
1152 {
1153 if (cpo_special)
1154 {
1155 ga_clear(&ga);
1156 return NULL;
1157 }
1158 ga_concat(&ga, get_special_key_name(c, modifiers));
1159 continue; // for (str)
1160 }
1161 }
1162 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1163 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1164 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1165 if (c)
1166 ga_append(&ga, c);
1167 }
1168 ga_append(&ga, NUL);
1169 return (char_u *)(ga.ga_data);
1170}
1171
1172/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001173 * Work out what to complete when doing command line completion of mapping
1174 * or abbreviation names.
1175 */
1176 char_u *
1177set_context_in_map_cmd(
1178 expand_T *xp,
1179 char_u *cmd,
1180 char_u *arg,
1181 int forceit, // TRUE if '!' given
1182 int isabbrev, // TRUE if abbreviation
1183 int isunmap, // TRUE if unmap/unabbrev command
1184 cmdidx_T cmdidx)
1185{
1186 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
1187 xp->xp_context = EXPAND_NOTHING;
1188 else
1189 {
1190 if (isunmap)
1191 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
1192 else
1193 {
1194 expand_mapmodes = INSERT + CMDLINE;
1195 if (!isabbrev)
1196 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
1197 }
1198 expand_isabbrev = isabbrev;
1199 xp->xp_context = EXPAND_MAPPINGS;
1200 expand_buffer = FALSE;
1201 for (;;)
1202 {
1203 if (STRNCMP(arg, "<buffer>", 8) == 0)
1204 {
1205 expand_buffer = TRUE;
1206 arg = skipwhite(arg + 8);
1207 continue;
1208 }
1209 if (STRNCMP(arg, "<unique>", 8) == 0)
1210 {
1211 arg = skipwhite(arg + 8);
1212 continue;
1213 }
1214 if (STRNCMP(arg, "<nowait>", 8) == 0)
1215 {
1216 arg = skipwhite(arg + 8);
1217 continue;
1218 }
1219 if (STRNCMP(arg, "<silent>", 8) == 0)
1220 {
1221 arg = skipwhite(arg + 8);
1222 continue;
1223 }
1224 if (STRNCMP(arg, "<special>", 9) == 0)
1225 {
1226 arg = skipwhite(arg + 9);
1227 continue;
1228 }
1229#ifdef FEAT_EVAL
1230 if (STRNCMP(arg, "<script>", 8) == 0)
1231 {
1232 arg = skipwhite(arg + 8);
1233 continue;
1234 }
1235 if (STRNCMP(arg, "<expr>", 6) == 0)
1236 {
1237 arg = skipwhite(arg + 6);
1238 continue;
1239 }
1240#endif
1241 break;
1242 }
1243 xp->xp_pattern = arg;
1244 }
1245
1246 return NULL;
1247}
1248
1249/*
1250 * Find all mapping/abbreviation names that match regexp "regmatch"'.
1251 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1252 * Return OK if matches found, FAIL otherwise.
1253 */
1254 int
1255ExpandMappings(
1256 regmatch_T *regmatch,
1257 int *num_file,
1258 char_u ***file)
1259{
1260 mapblock_T *mp;
1261 int hash;
1262 int count;
1263 int round;
1264 char_u *p;
1265 int i;
1266
1267 validate_maphash();
1268
1269 *num_file = 0; // return values in case of FAIL
1270 *file = NULL;
1271
1272 // round == 1: Count the matches.
1273 // round == 2: Build the array to keep the matches.
1274 for (round = 1; round <= 2; ++round)
1275 {
1276 count = 0;
1277
1278 for (i = 0; i < 7; ++i)
1279 {
1280 if (i == 0)
1281 p = (char_u *)"<silent>";
1282 else if (i == 1)
1283 p = (char_u *)"<unique>";
1284#ifdef FEAT_EVAL
1285 else if (i == 2)
1286 p = (char_u *)"<script>";
1287 else if (i == 3)
1288 p = (char_u *)"<expr>";
1289#endif
1290 else if (i == 4 && !expand_buffer)
1291 p = (char_u *)"<buffer>";
1292 else if (i == 5)
1293 p = (char_u *)"<nowait>";
1294 else if (i == 6)
1295 p = (char_u *)"<special>";
1296 else
1297 continue;
1298
1299 if (vim_regexec(regmatch, p, (colnr_T)0))
1300 {
1301 if (round == 1)
1302 ++count;
1303 else
1304 (*file)[count++] = vim_strsave(p);
1305 }
1306 }
1307
1308 for (hash = 0; hash < 256; ++hash)
1309 {
1310 if (expand_isabbrev)
1311 {
1312 if (hash > 0) // only one abbrev list
1313 break; // for (hash)
1314 mp = first_abbr;
1315 }
1316 else if (expand_buffer)
1317 mp = curbuf->b_maphash[hash];
1318 else
1319 mp = maphash[hash];
1320 for (; mp; mp = mp->m_next)
1321 {
1322 if (mp->m_mode & expand_mapmodes)
1323 {
1324 p = translate_mapping(mp->m_keys);
1325 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
1326 {
1327 if (round == 1)
1328 ++count;
1329 else
1330 {
1331 (*file)[count++] = p;
1332 p = NULL;
1333 }
1334 }
1335 vim_free(p);
1336 }
1337 } // for (mp)
1338 } // for (hash)
1339
1340 if (count == 0) // no match found
1341 break; // for (round)
1342
1343 if (round == 1)
1344 {
1345 *file = ALLOC_MULT(char_u *, count);
1346 if (*file == NULL)
1347 return FAIL;
1348 }
1349 } // for (round)
1350
1351 if (count > 1)
1352 {
1353 char_u **ptr1;
1354 char_u **ptr2;
1355 char_u **ptr3;
1356
1357 // Sort the matches
1358 sort_strings(*file, count);
1359
1360 // Remove multiple entries
1361 ptr1 = *file;
1362 ptr2 = ptr1 + 1;
1363 ptr3 = ptr1 + count;
1364
1365 while (ptr2 < ptr3)
1366 {
1367 if (STRCMP(*ptr1, *ptr2))
1368 *++ptr1 = *ptr2++;
1369 else
1370 {
1371 vim_free(*ptr2++);
1372 count--;
1373 }
1374 }
1375 }
1376
1377 *num_file = count;
1378 return (count == 0 ? FAIL : OK);
1379}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001380
1381/*
1382 * Check for an abbreviation.
1383 * Cursor is at ptr[col].
1384 * When inserting, mincol is where insert started.
1385 * For the command line, mincol is what is to be skipped over.
1386 * "c" is the character typed before check_abbr was called. It may have
1387 * ABBR_OFF added to avoid prepending a CTRL-V to it.
1388 *
1389 * Historic vi practice: The last character of an abbreviation must be an id
1390 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
1391 * characters or all non-id characters. This allows for abbr. "#i" to
1392 * "#include".
1393 *
1394 * Vim addition: Allow for abbreviations that end in a non-keyword character.
1395 * Then there must be white space before the abbr.
1396 *
1397 * return TRUE if there is an abbreviation, FALSE if not
1398 */
1399 int
1400check_abbr(
1401 int c,
1402 char_u *ptr,
1403 int col,
1404 int mincol)
1405{
1406 int len;
1407 int scol; // starting column of the abbr.
1408 int j;
1409 char_u *s;
1410 char_u tb[MB_MAXBYTES + 4];
1411 mapblock_T *mp;
1412 mapblock_T *mp2;
1413 int clen = 0; // length in characters
1414 int is_id = TRUE;
1415 int vim_abbr;
1416
1417 if (typebuf.tb_no_abbr_cnt) // abbrev. are not recursive
1418 return FALSE;
1419
1420 // no remapping implies no abbreviation, except for CTRL-]
1421 if (noremap_keys() && c != Ctrl_RSB)
1422 return FALSE;
1423
1424 // Check for word before the cursor: If it ends in a keyword char all
1425 // chars before it must be keyword chars or non-keyword chars, but not
1426 // white space. If it ends in a non-keyword char we accept any characters
1427 // before it except white space.
1428 if (col == 0) // cannot be an abbr.
1429 return FALSE;
1430
1431 if (has_mbyte)
1432 {
1433 char_u *p;
1434
1435 p = mb_prevptr(ptr, ptr + col);
1436 if (!vim_iswordp(p))
1437 vim_abbr = TRUE; // Vim added abbr.
1438 else
1439 {
1440 vim_abbr = FALSE; // vi compatible abbr.
1441 if (p > ptr)
1442 is_id = vim_iswordp(mb_prevptr(ptr, p));
1443 }
1444 clen = 1;
1445 while (p > ptr + mincol)
1446 {
1447 p = mb_prevptr(ptr, p);
1448 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
1449 {
1450 p += (*mb_ptr2len)(p);
1451 break;
1452 }
1453 ++clen;
1454 }
1455 scol = (int)(p - ptr);
1456 }
1457 else
1458 {
1459 if (!vim_iswordc(ptr[col - 1]))
1460 vim_abbr = TRUE; // Vim added abbr.
1461 else
1462 {
1463 vim_abbr = FALSE; // vi compatible abbr.
1464 if (col > 1)
1465 is_id = vim_iswordc(ptr[col - 2]);
1466 }
1467 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
1468 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
1469 ;
1470 }
1471
1472 if (scol < mincol)
1473 scol = mincol;
1474 if (scol < col) // there is a word in front of the cursor
1475 {
1476 ptr += scol;
1477 len = col - scol;
1478 mp = curbuf->b_first_abbr;
1479 mp2 = first_abbr;
1480 if (mp == NULL)
1481 {
1482 mp = mp2;
1483 mp2 = NULL;
1484 }
1485 for ( ; mp; mp->m_next == NULL
1486 ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
1487 {
1488 int qlen = mp->m_keylen;
1489 char_u *q = mp->m_keys;
1490 int match;
1491
1492 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
1493 {
1494 char_u *qe = vim_strsave(mp->m_keys);
1495
1496 // might have CSI escaped mp->m_keys
1497 if (qe != NULL)
1498 {
1499 q = qe;
1500 vim_unescape_csi(q);
1501 qlen = (int)STRLEN(q);
1502 }
1503 }
1504
1505 // find entries with right mode and keys
1506 match = (mp->m_mode & State)
1507 && qlen == len
1508 && !STRNCMP(q, ptr, (size_t)len);
1509 if (q != mp->m_keys)
1510 vim_free(q);
1511 if (match)
1512 break;
1513 }
1514 if (mp != NULL)
1515 {
1516 // Found a match:
1517 // Insert the rest of the abbreviation in typebuf.tb_buf[].
1518 // This goes from end to start.
1519 //
1520 // Characters 0x000 - 0x100: normal chars, may need CTRL-V,
1521 // except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
1522 // Characters where IS_SPECIAL() == TRUE: key codes, need
1523 // K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
1524 //
1525 // Character CTRL-] is treated specially - it completes the
1526 // abbreviation, but is not inserted into the input stream.
1527 j = 0;
1528 if (c != Ctrl_RSB)
1529 {
1530 // special key code, split up
1531 if (IS_SPECIAL(c) || c == K_SPECIAL)
1532 {
1533 tb[j++] = K_SPECIAL;
1534 tb[j++] = K_SECOND(c);
1535 tb[j++] = K_THIRD(c);
1536 }
1537 else
1538 {
1539 if (c < ABBR_OFF && (c < ' ' || c > '~'))
1540 tb[j++] = Ctrl_V; // special char needs CTRL-V
1541 if (has_mbyte)
1542 {
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001543 int newlen;
1544 char_u *escaped;
1545
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001546 // if ABBR_OFF has been added, remove it here
1547 if (c >= ABBR_OFF)
1548 c -= ABBR_OFF;
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001549 newlen = (*mb_char2bytes)(c, tb + j);
1550 tb[j + newlen] = NUL;
1551 // Need to escape K_SPECIAL.
1552 escaped = vim_strsave_escape_csi(tb + j);
1553 if (escaped != NULL)
1554 {
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02001555 newlen = (int)STRLEN(escaped);
Bram Moolenaar4934ed32021-04-30 19:43:11 +02001556 mch_memmove(tb + j, escaped, newlen);
1557 j += newlen;
1558 vim_free(escaped);
1559 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001560 }
1561 else
1562 tb[j++] = c;
1563 }
1564 tb[j] = NUL;
1565 // insert the last typed char
1566 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1567 }
1568#ifdef FEAT_EVAL
1569 if (mp->m_expr)
1570 s = eval_map_expr(mp->m_str, c);
1571 else
1572#endif
1573 s = mp->m_str;
1574 if (s != NULL)
1575 {
1576 // insert the to string
1577 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
1578 // no abbrev. for these chars
1579 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
1580#ifdef FEAT_EVAL
1581 if (mp->m_expr)
1582 vim_free(s);
1583#endif
1584 }
1585
1586 tb[0] = Ctrl_H;
1587 tb[1] = NUL;
1588 if (has_mbyte)
1589 len = clen; // Delete characters instead of bytes
1590 while (len-- > 0) // delete the from string
1591 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
1592 return TRUE;
1593 }
1594 }
1595 return FALSE;
1596}
1597
1598#ifdef FEAT_EVAL
1599/*
1600 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
1601 * special characters.
1602 */
1603 char_u *
1604eval_map_expr(
1605 char_u *str,
1606 int c) // NUL or typed character for abbreviation
1607{
1608 char_u *res;
1609 char_u *p;
1610 char_u *expr;
1611 pos_T save_cursor;
1612 int save_msg_col;
1613 int save_msg_row;
1614
1615 // Remove escaping of CSI, because "str" is in a format to be used as
1616 // typeahead.
1617 expr = vim_strsave(str);
1618 if (expr == NULL)
1619 return NULL;
1620 vim_unescape_csi(expr);
1621
1622 // Forbid changing text or using ":normal" to avoid most of the bad side
1623 // effects. Also restore the cursor position.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001624 ++textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001625 ++ex_normal_lock;
1626 set_vim_var_char(c); // set v:char to the typed character
1627 save_cursor = curwin->w_cursor;
1628 save_msg_col = msg_col;
1629 save_msg_row = msg_row;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001630 p = eval_to_string(expr, FALSE);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001631 --textwinlock;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001632 --ex_normal_lock;
1633 curwin->w_cursor = save_cursor;
1634 msg_col = save_msg_col;
1635 msg_row = save_msg_row;
1636
1637 vim_free(expr);
1638
1639 if (p == NULL)
1640 return NULL;
1641 // Escape CSI in the result to be able to use the string as typeahead.
1642 res = vim_strsave_escape_csi(p);
1643 vim_free(p);
1644
1645 return res;
1646}
1647#endif
1648
1649/*
1650 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
1651 * can be put in the typeahead buffer.
1652 * Returns NULL when out of memory.
1653 */
1654 char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01001655vim_strsave_escape_csi(char_u *p)
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001656{
1657 char_u *res;
1658 char_u *s, *d;
1659
1660 // Need a buffer to hold up to three times as much. Four in case of an
1661 // illegal utf-8 byte:
1662 // 0xc0 -> 0xc3 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER
1663 res = alloc(STRLEN(p) * 4 + 1);
1664 if (res != NULL)
1665 {
1666 d = res;
1667 for (s = p; *s != NUL; )
1668 {
1669 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
1670 {
1671 // Copy special key unmodified.
1672 *d++ = *s++;
1673 *d++ = *s++;
1674 *d++ = *s++;
1675 }
1676 else
1677 {
1678 // Add character, possibly multi-byte to destination, escaping
1679 // CSI and K_SPECIAL. Be careful, it can be an illegal byte!
1680 d = add_char2buf(PTR2CHAR(s), d);
1681 s += MB_CPTR2LEN(s);
1682 }
1683 }
1684 *d = NUL;
1685 }
1686 return res;
1687}
1688
1689/*
1690 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
1691 * vim_strsave_escape_csi(). Works in-place.
1692 */
1693 void
1694vim_unescape_csi(char_u *p)
1695{
1696 char_u *s = p, *d = p;
1697
1698 while (*s != NUL)
1699 {
1700 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
1701 {
1702 *d++ = K_SPECIAL;
1703 s += 3;
1704 }
1705 else if ((s[0] == K_SPECIAL || s[0] == CSI)
1706 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
1707 {
1708 *d++ = CSI;
1709 s += 3;
1710 }
1711 else
1712 *d++ = *s++;
1713 }
1714 *d = NUL;
1715}
1716
1717/*
1718 * Write map commands for the current mappings to an .exrc file.
1719 * Return FAIL on error, OK otherwise.
1720 */
1721 int
1722makemap(
1723 FILE *fd,
1724 buf_T *buf) // buffer for local mappings or NULL
1725{
1726 mapblock_T *mp;
1727 char_u c1, c2, c3;
1728 char_u *p;
1729 char *cmd;
1730 int abbr;
1731 int hash;
1732 int did_cpo = FALSE;
1733 int i;
1734
1735 validate_maphash();
1736
1737 // Do the loop twice: Once for mappings, once for abbreviations.
1738 // Then loop over all map hash lists.
1739 for (abbr = 0; abbr < 2; ++abbr)
1740 for (hash = 0; hash < 256; ++hash)
1741 {
1742 if (abbr)
1743 {
1744 if (hash > 0) // there is only one abbr list
1745 break;
1746 if (buf != NULL)
1747 mp = buf->b_first_abbr;
1748 else
1749 mp = first_abbr;
1750 }
1751 else
1752 {
1753 if (buf != NULL)
1754 mp = buf->b_maphash[hash];
1755 else
1756 mp = maphash[hash];
1757 }
1758
1759 for ( ; mp; mp = mp->m_next)
1760 {
1761 // skip script-local mappings
1762 if (mp->m_noremap == REMAP_SCRIPT)
1763 continue;
1764
1765 // skip mappings that contain a <SNR> (script-local thing),
1766 // they probably don't work when loaded again
1767 for (p = mp->m_str; *p != NUL; ++p)
1768 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
1769 && p[2] == (int)KE_SNR)
1770 break;
1771 if (*p != NUL)
1772 continue;
1773
1774 // It's possible to create a mapping and then ":unmap" certain
1775 // modes. We recreate this here by mapping the individual
1776 // modes, which requires up to three of them.
1777 c1 = NUL;
1778 c2 = NUL;
1779 c3 = NUL;
1780 if (abbr)
1781 cmd = "abbr";
1782 else
1783 cmd = "map";
1784 switch (mp->m_mode)
1785 {
1786 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
1787 break;
1788 case NORMAL:
1789 c1 = 'n';
1790 break;
1791 case VISUAL:
1792 c1 = 'x';
1793 break;
1794 case SELECTMODE:
1795 c1 = 's';
1796 break;
1797 case OP_PENDING:
1798 c1 = 'o';
1799 break;
1800 case NORMAL + VISUAL:
1801 c1 = 'n';
1802 c2 = 'x';
1803 break;
1804 case NORMAL + SELECTMODE:
1805 c1 = 'n';
1806 c2 = 's';
1807 break;
1808 case NORMAL + OP_PENDING:
1809 c1 = 'n';
1810 c2 = 'o';
1811 break;
1812 case VISUAL + SELECTMODE:
1813 c1 = 'v';
1814 break;
1815 case VISUAL + OP_PENDING:
1816 c1 = 'x';
1817 c2 = 'o';
1818 break;
1819 case SELECTMODE + OP_PENDING:
1820 c1 = 's';
1821 c2 = 'o';
1822 break;
1823 case NORMAL + VISUAL + SELECTMODE:
1824 c1 = 'n';
1825 c2 = 'v';
1826 break;
1827 case NORMAL + VISUAL + OP_PENDING:
1828 c1 = 'n';
1829 c2 = 'x';
1830 c3 = 'o';
1831 break;
1832 case NORMAL + SELECTMODE + OP_PENDING:
1833 c1 = 'n';
1834 c2 = 's';
1835 c3 = 'o';
1836 break;
1837 case VISUAL + SELECTMODE + OP_PENDING:
1838 c1 = 'v';
1839 c2 = 'o';
1840 break;
1841 case CMDLINE + INSERT:
1842 if (!abbr)
1843 cmd = "map!";
1844 break;
1845 case CMDLINE:
1846 c1 = 'c';
1847 break;
1848 case INSERT:
1849 c1 = 'i';
1850 break;
1851 case LANGMAP:
1852 c1 = 'l';
1853 break;
1854 case TERMINAL:
1855 c1 = 't';
1856 break;
1857 default:
1858 iemsg(_("E228: makemap: Illegal mode"));
1859 return FAIL;
1860 }
1861 do // do this twice if c2 is set, 3 times with c3
1862 {
1863 // When outputting <> form, need to make sure that 'cpo'
1864 // is set to the Vim default.
1865 if (!did_cpo)
1866 {
1867 if (*mp->m_str == NUL) // will use <Nop>
1868 did_cpo = TRUE;
1869 else
1870 for (i = 0; i < 2; ++i)
1871 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
1872 if (*p == K_SPECIAL || *p == NL)
1873 did_cpo = TRUE;
1874 if (did_cpo)
1875 {
1876 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
1877 || put_eol(fd) < 0
1878 || fprintf(fd, "set cpo&vim") < 0
1879 || put_eol(fd) < 0)
1880 return FAIL;
1881 }
1882 }
1883 if (c1 && putc(c1, fd) < 0)
1884 return FAIL;
1885 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
1886 return FAIL;
1887 if (fputs(cmd, fd) < 0)
1888 return FAIL;
1889 if (buf != NULL && fputs(" <buffer>", fd) < 0)
1890 return FAIL;
1891 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
1892 return FAIL;
1893 if (mp->m_silent && fputs(" <silent>", fd) < 0)
1894 return FAIL;
1895#ifdef FEAT_EVAL
1896 if (mp->m_noremap == REMAP_SCRIPT
1897 && fputs("<script>", fd) < 0)
1898 return FAIL;
1899 if (mp->m_expr && fputs(" <expr>", fd) < 0)
1900 return FAIL;
1901#endif
1902
1903 if ( putc(' ', fd) < 0
1904 || put_escstr(fd, mp->m_keys, 0) == FAIL
1905 || putc(' ', fd) < 0
1906 || put_escstr(fd, mp->m_str, 1) == FAIL
1907 || put_eol(fd) < 0)
1908 return FAIL;
1909 c1 = c2;
1910 c2 = c3;
1911 c3 = NUL;
1912 } while (c1 != NUL);
1913 }
1914 }
1915
1916 if (did_cpo)
1917 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
1918 || put_eol(fd) < 0
1919 || fprintf(fd, "unlet s:cpo_save") < 0
1920 || put_eol(fd) < 0)
1921 return FAIL;
1922 return OK;
1923}
1924
1925/*
1926 * write escape string to file
1927 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
1928 *
1929 * return FAIL for failure, OK otherwise
1930 */
1931 int
1932put_escstr(FILE *fd, char_u *strstart, int what)
1933{
1934 char_u *str = strstart;
1935 int c;
1936 int modifiers;
1937
1938 // :map xx <Nop>
1939 if (*str == NUL && what == 1)
1940 {
1941 if (fprintf(fd, "<Nop>") < 0)
1942 return FAIL;
1943 return OK;
1944 }
1945
1946 for ( ; *str != NUL; ++str)
1947 {
1948 char_u *p;
1949
1950 // Check for a multi-byte character, which may contain escaped
1951 // K_SPECIAL and CSI bytes
1952 p = mb_unescape(&str);
1953 if (p != NULL)
1954 {
1955 while (*p != NUL)
1956 if (fputc(*p++, fd) < 0)
1957 return FAIL;
1958 --str;
1959 continue;
1960 }
1961
1962 c = *str;
1963 // Special key codes have to be translated to be able to make sense
1964 // when they are read back.
1965 if (c == K_SPECIAL && what != 2)
1966 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +02001967 modifiers = 0;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001968 if (str[1] == KS_MODIFIER)
1969 {
1970 modifiers = str[2];
1971 str += 3;
1972 c = *str;
1973 }
1974 if (c == K_SPECIAL)
1975 {
1976 c = TO_SPECIAL(str[1], str[2]);
1977 str += 2;
1978 }
1979 if (IS_SPECIAL(c) || modifiers) // special key
1980 {
1981 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
1982 return FAIL;
1983 continue;
1984 }
1985 }
1986
1987 // A '\n' in a map command should be written as <NL>.
1988 // A '\n' in a set command should be written as \^V^J.
1989 if (c == NL)
1990 {
1991 if (what == 2)
1992 {
1993 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
1994 return FAIL;
1995 }
1996 else
1997 {
1998 if (fprintf(fd, "<NL>") < 0)
1999 return FAIL;
2000 }
2001 continue;
2002 }
2003
2004 // Some characters have to be escaped with CTRL-V to
2005 // prevent them from misinterpreted in DoOneCmd().
2006 // A space, Tab and '"' has to be escaped with a backslash to
2007 // prevent it to be misinterpreted in do_set().
2008 // A space has to be escaped with a CTRL-V when it's at the start of a
2009 // ":map" rhs.
2010 // A '<' has to be escaped with a CTRL-V to prevent it being
2011 // interpreted as the start of a special key name.
2012 // A space in the lhs of a :map needs a CTRL-V.
2013 if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
2014 {
2015 if (putc('\\', fd) < 0)
2016 return FAIL;
2017 }
2018 else if (c < ' ' || c > '~' || c == '|'
2019 || (what == 0 && c == ' ')
2020 || (what == 1 && str == strstart && c == ' ')
2021 || (what != 2 && c == '<'))
2022 {
2023 if (putc(Ctrl_V, fd) < 0)
2024 return FAIL;
2025 }
2026 if (putc(c, fd) < 0)
2027 return FAIL;
2028 }
2029 return OK;
2030}
2031
2032/*
2033 * Check all mappings for the presence of special key codes.
2034 * Used after ":set term=xxx".
2035 */
2036 void
2037check_map_keycodes(void)
2038{
2039 mapblock_T *mp;
2040 char_u *p;
2041 int i;
2042 char_u buf[3];
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002043 int abbr;
2044 int hash;
2045 buf_T *bp;
Bram Moolenaare31ee862020-01-07 20:59:34 +01002046 ESTACK_CHECK_DECLARATION
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002047
2048 validate_maphash();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002049 // avoids giving error messages
2050 estack_push(ETYPE_INTERNAL, (char_u *)"mappings", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002051 ESTACK_CHECK_SETUP
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002052
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002053 // Do this once for each buffer, and then once for global
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002054 // mappings/abbreviations with bp == NULL
2055 for (bp = firstbuf; ; bp = bp->b_next)
2056 {
2057 // Do the loop twice: Once for mappings, once for abbreviations.
2058 // Then loop over all map hash lists.
2059 for (abbr = 0; abbr <= 1; ++abbr)
2060 for (hash = 0; hash < 256; ++hash)
2061 {
2062 if (abbr)
2063 {
2064 if (hash) // there is only one abbr list
2065 break;
2066 if (bp != NULL)
2067 mp = bp->b_first_abbr;
2068 else
2069 mp = first_abbr;
2070 }
2071 else
2072 {
2073 if (bp != NULL)
2074 mp = bp->b_maphash[hash];
2075 else
2076 mp = maphash[hash];
2077 }
2078 for ( ; mp != NULL; mp = mp->m_next)
2079 {
2080 for (i = 0; i <= 1; ++i) // do this twice
2081 {
2082 if (i == 0)
2083 p = mp->m_keys; // once for the "from" part
2084 else
2085 p = mp->m_str; // and once for the "to" part
2086 while (*p)
2087 {
2088 if (*p == K_SPECIAL)
2089 {
2090 ++p;
2091 if (*p < 128) // for "normal" tcap entries
2092 {
2093 buf[0] = p[0];
2094 buf[1] = p[1];
2095 buf[2] = NUL;
2096 (void)add_termcap_entry(buf, FALSE);
2097 }
2098 ++p;
2099 }
2100 ++p;
2101 }
2102 }
2103 }
2104 }
2105 if (bp == NULL)
2106 break;
2107 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01002108 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002109 estack_pop();
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002110}
2111
2112#if defined(FEAT_EVAL) || defined(PROTO)
2113/*
2114 * Check the string "keys" against the lhs of all mappings.
2115 * Return pointer to rhs of mapping (mapblock->m_str).
2116 * NULL when no mapping found.
2117 */
2118 char_u *
2119check_map(
2120 char_u *keys,
2121 int mode,
2122 int exact, // require exact match
2123 int ign_mod, // ignore preceding modifier
2124 int abbr, // do abbreviations
2125 mapblock_T **mp_ptr, // return: pointer to mapblock or NULL
2126 int *local_ptr) // return: buffer-local mapping or NULL
2127{
2128 int hash;
2129 int len, minlen;
2130 mapblock_T *mp;
2131 char_u *s;
2132 int local;
2133
2134 validate_maphash();
2135
2136 len = (int)STRLEN(keys);
2137 for (local = 1; local >= 0; --local)
2138 // loop over all hash lists
2139 for (hash = 0; hash < 256; ++hash)
2140 {
2141 if (abbr)
2142 {
2143 if (hash > 0) // there is only one list.
2144 break;
2145 if (local)
2146 mp = curbuf->b_first_abbr;
2147 else
2148 mp = first_abbr;
2149 }
2150 else if (local)
2151 mp = curbuf->b_maphash[hash];
2152 else
2153 mp = maphash[hash];
2154 for ( ; mp != NULL; mp = mp->m_next)
2155 {
2156 // skip entries with wrong mode, wrong length and not matching
2157 // ones
2158 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
2159 {
2160 if (len > mp->m_keylen)
2161 minlen = mp->m_keylen;
2162 else
2163 minlen = len;
2164 s = mp->m_keys;
2165 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
2166 && s[2] != NUL)
2167 {
2168 s += 3;
2169 if (len > mp->m_keylen - 3)
2170 minlen = mp->m_keylen - 3;
2171 }
2172 if (STRNCMP(s, keys, minlen) == 0)
2173 {
2174 if (mp_ptr != NULL)
2175 *mp_ptr = mp;
2176 if (local_ptr != NULL)
2177 *local_ptr = local;
2178 return mp->m_str;
2179 }
2180 }
2181 }
2182 }
2183
2184 return NULL;
2185}
2186
2187 void
2188get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2189{
2190 char_u *keys;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002191 char_u *keys_simplified;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002192 char_u *which;
2193 char_u buf[NUMBUFLEN];
2194 char_u *keys_buf = NULL;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002195 char_u *alt_keys_buf = NULL;
2196 int did_simplify = FALSE;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002197 char_u *rhs;
2198 int mode;
2199 int abbr = FALSE;
2200 int get_dict = FALSE;
2201 mapblock_T *mp;
Bram Moolenaara55ba062020-05-27 21:29:04 +02002202 mapblock_T *mp_simplified = NULL;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002203 int buffer_local;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002204 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002205
2206 // return empty string for failure
2207 rettv->v_type = VAR_STRING;
2208 rettv->vval.v_string = NULL;
2209
2210 keys = tv_get_string(&argvars[0]);
2211 if (*keys == NUL)
2212 return;
2213
2214 if (argvars[1].v_type != VAR_UNKNOWN)
2215 {
2216 which = tv_get_string_buf_chk(&argvars[1], buf);
2217 if (argvars[2].v_type != VAR_UNKNOWN)
2218 {
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002219 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002220 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02002221 get_dict = (int)tv_get_bool(&argvars[3]);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002222 }
2223 }
2224 else
2225 which = (char_u *)"";
2226 if (which == NULL)
2227 return;
2228
2229 mode = get_map_mode(&which, 0);
2230
Bram Moolenaar9c652532020-05-24 13:10:18 +02002231 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2232 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2233 &mp, &buffer_local);
2234 if (did_simplify)
2235 {
2236 // When the lhs is being simplified the not-simplified keys are
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002237 // preferred for printing, like in do_map().
Bram Moolenaar9c652532020-05-24 13:10:18 +02002238 // The "rhs" and "buffer_local" values are not expected to change.
2239 mp_simplified = mp;
2240 (void)replace_termcodes(keys, &alt_keys_buf,
2241 flags | REPTERM_NO_SIMPLIFY, NULL);
2242 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2243 &buffer_local);
2244 }
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002245
2246 if (!get_dict)
2247 {
2248 // Return a string.
2249 if (rhs != NULL)
2250 {
2251 if (*rhs == NUL)
2252 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
2253 else
2254 rettv->vval.v_string = str2special_save(rhs, FALSE);
2255 }
2256
2257 }
2258 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
2259 {
2260 // Return a dictionary.
2261 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2262 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2263 dict_T *dict = rettv->vval.v_dict;
2264
2265 dict_add_string(dict, "lhs", lhs);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002266 vim_free(lhs);
2267 dict_add_string(dict, "lhsraw", mp->m_keys);
2268 if (did_simplify)
2269 // Also add the value for the simplified entry.
2270 dict_add_string(dict, "lhsrawalt", mp_simplified->m_keys);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002271 dict_add_string(dict, "rhs", mp->m_orig_str);
2272 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +02002273 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2274 ? 1L : 0L);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002275 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2276 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
2277 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2278 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2279 dict_add_number(dict, "buffer", (long)buffer_local);
2280 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2281 dict_add_string(dict, "mode", mapmode);
2282
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002283 vim_free(mapmode);
2284 }
Bram Moolenaar9c652532020-05-24 13:10:18 +02002285
2286 vim_free(keys_buf);
2287 vim_free(alt_keys_buf);
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002288}
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002289
2290/*
2291 * "mapset()" function
2292 */
2293 void
2294f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2295{
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002296 char_u *keys_buf = NULL;
2297 char_u *which;
2298 int mode;
2299 char_u buf[NUMBUFLEN];
2300 int is_abbr;
2301 dict_T *d;
2302 char_u *lhs;
Bram Moolenaar9c652532020-05-24 13:10:18 +02002303 char_u *lhsraw;
2304 char_u *lhsrawalt;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002305 char_u *rhs;
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002306 char_u *orig_rhs;
2307 char_u *arg_buf = NULL;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002308 int noremap;
2309 int expr;
2310 int silent;
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002311 int buffer;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002312 scid_T sid;
2313 linenr_T lnum;
2314 mapblock_T **map_table = maphash;
2315 mapblock_T **abbr_table = &first_abbr;
2316 int nowait;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002317 char_u *arg;
2318
2319 which = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar1b912982020-09-29 21:45:41 +02002320 if (which == NULL)
2321 return;
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002322 mode = get_map_mode(&which, 0);
Bram Moolenaar74273e62020-10-01 21:37:21 +02002323 is_abbr = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002324
2325 if (argvars[2].v_type != VAR_DICT)
2326 {
2327 emsg(_(e_dictkey));
2328 return;
2329 }
2330 d = argvars[2].vval.v_dict;
2331
2332 // Get the values in the same order as above in get_maparg().
2333 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002334 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2335 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002336 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
Bram Moolenaar9c652532020-05-24 13:10:18 +02002337 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002338 {
Bram Moolenaar9c652532020-05-24 13:10:18 +02002339 emsg(_("E460: entries missing in mapset() dict argument"));
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002340 return;
2341 }
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002342 orig_rhs = rhs;
2343 rhs = replace_termcodes(rhs, &arg_buf,
2344 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002345
2346 noremap = dict_get_number(d, (char_u *)"noremap") ? REMAP_NONE: 0;
2347 if (dict_get_number(d, (char_u *)"script") != 0)
2348 noremap = REMAP_SCRIPT;
2349 expr = dict_get_number(d, (char_u *)"expr") != 0;
2350 silent = dict_get_number(d, (char_u *)"silent") != 0;
2351 sid = dict_get_number(d, (char_u *)"sid");
2352 lnum = dict_get_number(d, (char_u *)"lnum");
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002353 buffer = dict_get_number(d, (char_u *)"buffer");
2354 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2355 // mode from the dict is not used
2356
2357 if (buffer)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002358 {
2359 map_table = curbuf->b_maphash;
2360 abbr_table = &curbuf->b_first_abbr;
2361 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002362
2363 // Delete any existing mapping for this lhs and mode.
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +02002364 if (buffer)
2365 {
2366 arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
2367 if (arg == NULL)
2368 return;
2369 STRCPY(arg, "<buffer>");
2370 STRCPY(arg + 8, lhs);
2371 }
2372 else
2373 {
2374 arg = vim_strsave(lhs);
2375 if (arg == NULL)
2376 return;
2377 }
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002378 do_map(1, arg, mode, is_abbr);
2379 vim_free(arg);
2380
Bram Moolenaar9c652532020-05-24 13:10:18 +02002381 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
2382 nowait, silent, mode, is_abbr, expr, sid, lnum, 0);
2383 if (lhsrawalt != NULL)
2384 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
2385 nowait, silent, mode, is_abbr, expr, sid, lnum, 1);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002386 vim_free(keys_buf);
Bram Moolenaarc94c1462020-05-22 20:01:06 +02002387 vim_free(arg_buf);
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002388}
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002389#endif
2390
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02002391
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002392#if defined(MSWIN) || defined(MACOS_X)
2393
2394# define VIS_SEL (VISUAL+SELECTMODE) // abbreviation
2395
2396/*
2397 * Default mappings for some often used keys.
2398 */
2399struct initmap
2400{
2401 char_u *arg;
2402 int mode;
2403};
2404
2405# ifdef FEAT_GUI_MSWIN
2406// Use the Windows (CUA) keybindings. (GUI)
2407static struct initmap initmappings[] =
2408{
2409 // paste, copy and cut
2410 {(char_u *)"<S-Insert> \"*P", NORMAL},
2411 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
2412 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
2413 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
2414 {(char_u *)"<S-Del> \"*d", VIS_SEL},
2415 {(char_u *)"<C-Del> \"*d", VIS_SEL},
2416 {(char_u *)"<C-X> \"*d", VIS_SEL},
2417 // Missing: CTRL-C (cancel) and CTRL-V (block selection)
2418};
2419# endif
2420
2421# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
2422// Use the Windows (CUA) keybindings. (Console)
2423static struct initmap cinitmappings[] =
2424{
2425 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
2426 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
2427 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
2428 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
2429
2430 // paste, copy and cut
2431# ifdef FEAT_CLIPBOARD
2432 {(char_u *)"\316\324 \"*P", NORMAL}, // SHIFT-Insert is "*P
2433 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, // SHIFT-Insert is "-d"*P
2434 {(char_u *)"\316\324 \022\017*", INSERT}, // SHIFT-Insert is ^R^O*
2435 {(char_u *)"\316\325 \"*y", VIS_SEL}, // CTRL-Insert is "*y
2436 {(char_u *)"\316\327 \"*d", VIS_SEL}, // SHIFT-Del is "*d
2437 {(char_u *)"\316\330 \"*d", VIS_SEL}, // CTRL-Del is "*d
2438 {(char_u *)"\030 \"*d", VIS_SEL}, // CTRL-X is "*d
2439# else
2440 {(char_u *)"\316\324 P", NORMAL}, // SHIFT-Insert is P
2441 {(char_u *)"\316\324 \"-dP", VIS_SEL}, // SHIFT-Insert is "-dP
2442 {(char_u *)"\316\324 \022\017\"", INSERT}, // SHIFT-Insert is ^R^O"
2443 {(char_u *)"\316\325 y", VIS_SEL}, // CTRL-Insert is y
2444 {(char_u *)"\316\327 d", VIS_SEL}, // SHIFT-Del is d
2445 {(char_u *)"\316\330 d", VIS_SEL}, // CTRL-Del is d
2446# endif
2447};
2448# endif
2449
2450# if defined(MACOS_X)
2451static struct initmap initmappings[] =
2452{
2453 // Use the Standard MacOS binding.
2454 // paste, copy and cut
2455 {(char_u *)"<D-v> \"*P", NORMAL},
2456 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
2457 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
2458 {(char_u *)"<D-c> \"*y", VIS_SEL},
2459 {(char_u *)"<D-x> \"*d", VIS_SEL},
2460 {(char_u *)"<Backspace> \"-d", VIS_SEL},
2461};
2462# endif
2463
2464# undef VIS_SEL
2465#endif
2466
2467/*
2468 * Set up default mappings.
2469 */
2470 void
2471init_mappings(void)
2472{
2473#if defined(MSWIN) || defined(MACOS_X)
2474 int i;
2475
2476# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2477# ifdef VIMDLL
2478 if (!gui.starting)
2479# endif
2480 {
2481 for (i = 0;
2482 i < (int)(sizeof(cinitmappings) / sizeof(struct initmap)); ++i)
2483 add_map(cinitmappings[i].arg, cinitmappings[i].mode);
2484 }
2485# endif
2486# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
2487 for (i = 0; i < (int)(sizeof(initmappings) / sizeof(struct initmap)); ++i)
2488 add_map(initmappings[i].arg, initmappings[i].mode);
2489# endif
2490#endif
2491}
2492
2493#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
2494 || defined(PROTO)
2495/*
2496 * Add a mapping "map" for mode "mode".
2497 * Need to put string in allocated memory, because do_map() will modify it.
2498 */
2499 void
2500add_map(char_u *map, int mode)
2501{
2502 char_u *s;
2503 char_u *cpo_save = p_cpo;
2504
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002505 p_cpo = empty_option; // Allow <> notation
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002506 s = vim_strsave(map);
2507 if (s != NULL)
2508 {
2509 (void)do_map(0, s, mode, FALSE);
2510 vim_free(s);
2511 }
2512 p_cpo = cpo_save;
2513}
2514#endif
2515
Bram Moolenaare677df82019-09-02 22:31:11 +02002516#if defined(FEAT_LANGMAP) || defined(PROTO)
2517/*
2518 * Any character has an equivalent 'langmap' character. This is used for
2519 * keyboards that have a special language mode that sends characters above
2520 * 128 (although other characters can be translated too). The "to" field is a
2521 * Vim command character. This avoids having to switch the keyboard back to
2522 * ASCII mode when leaving Insert mode.
2523 *
2524 * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
2525 * commands.
2526 * langmap_mapga.ga_data is a sorted table of langmap_entry_T. This does the
2527 * same as langmap_mapchar[] for characters >= 256.
2528 *
2529 * Use growarray for 'langmap' chars >= 256
2530 */
2531typedef struct
2532{
2533 int from;
2534 int to;
2535} langmap_entry_T;
2536
2537static garray_T langmap_mapga;
2538
2539/*
2540 * Search for an entry in "langmap_mapga" for "from". If found set the "to"
2541 * field. If not found insert a new entry at the appropriate location.
2542 */
2543 static void
2544langmap_set_entry(int from, int to)
2545{
2546 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2547 int a = 0;
2548 int b = langmap_mapga.ga_len;
2549
2550 // Do a binary search for an existing entry.
2551 while (a != b)
2552 {
2553 int i = (a + b) / 2;
2554 int d = entries[i].from - from;
2555
2556 if (d == 0)
2557 {
2558 entries[i].to = to;
2559 return;
2560 }
2561 if (d < 0)
2562 a = i + 1;
2563 else
2564 b = i;
2565 }
2566
2567 if (ga_grow(&langmap_mapga, 1) != OK)
2568 return; // out of memory
2569
2570 // insert new entry at position "a"
2571 entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
2572 mch_memmove(entries + 1, entries,
2573 (langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
2574 ++langmap_mapga.ga_len;
2575 entries[0].from = from;
2576 entries[0].to = to;
2577}
2578
2579/*
2580 * Apply 'langmap' to multi-byte character "c" and return the result.
2581 */
2582 int
2583langmap_adjust_mb(int c)
2584{
2585 langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
2586 int a = 0;
2587 int b = langmap_mapga.ga_len;
2588
2589 while (a != b)
2590 {
2591 int i = (a + b) / 2;
2592 int d = entries[i].from - c;
2593
2594 if (d == 0)
2595 return entries[i].to; // found matching entry
2596 if (d < 0)
2597 a = i + 1;
2598 else
2599 b = i;
2600 }
2601 return c; // no entry found, return "c" unmodified
2602}
2603
2604 void
2605langmap_init(void)
2606{
2607 int i;
2608
2609 for (i = 0; i < 256; i++)
2610 langmap_mapchar[i] = i; // we init with a one-to-one map
2611 ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
2612}
2613
2614/*
2615 * Called when langmap option is set; the language map can be
2616 * changed at any time!
2617 */
2618 void
2619langmap_set(void)
2620{
2621 char_u *p;
2622 char_u *p2;
2623 int from, to;
2624
2625 ga_clear(&langmap_mapga); // clear the previous map first
2626 langmap_init(); // back to one-to-one map
2627
2628 for (p = p_langmap; p[0] != NUL; )
2629 {
2630 for (p2 = p; p2[0] != NUL && p2[0] != ',' && p2[0] != ';';
2631 MB_PTR_ADV(p2))
2632 {
2633 if (p2[0] == '\\' && p2[1] != NUL)
2634 ++p2;
2635 }
2636 if (p2[0] == ';')
2637 ++p2; // abcd;ABCD form, p2 points to A
2638 else
2639 p2 = NULL; // aAbBcCdD form, p2 is NULL
2640 while (p[0])
2641 {
2642 if (p[0] == ',')
2643 {
2644 ++p;
2645 break;
2646 }
2647 if (p[0] == '\\' && p[1] != NUL)
2648 ++p;
2649 from = (*mb_ptr2char)(p);
2650 to = NUL;
2651 if (p2 == NULL)
2652 {
2653 MB_PTR_ADV(p);
2654 if (p[0] != ',')
2655 {
2656 if (p[0] == '\\')
2657 ++p;
2658 to = (*mb_ptr2char)(p);
2659 }
2660 }
2661 else
2662 {
2663 if (p2[0] != ',')
2664 {
2665 if (p2[0] == '\\')
2666 ++p2;
2667 to = (*mb_ptr2char)(p2);
2668 }
2669 }
2670 if (to == NUL)
2671 {
2672 semsg(_("E357: 'langmap': Matching character missing for %s"),
2673 transchar(from));
2674 return;
2675 }
2676
2677 if (from >= 256)
2678 langmap_set_entry(from, to);
2679 else
2680 langmap_mapchar[from & 255] = to;
2681
2682 // Advance to next pair
2683 MB_PTR_ADV(p);
2684 if (p2 != NULL)
2685 {
2686 MB_PTR_ADV(p2);
2687 if (*p == ';')
2688 {
2689 p = p2;
2690 if (p[0] != NUL)
2691 {
2692 if (p[0] != ',')
2693 {
2694 semsg(_("E358: 'langmap': Extra characters after semicolon: %s"), p);
2695 return;
2696 }
2697 ++p;
2698 }
2699 break;
2700 }
2701 }
2702 }
2703 }
2704}
2705#endif
2706
Bram Moolenaarb66bab32019-08-01 14:28:24 +02002707 static void
2708do_exmap(exarg_T *eap, int isabbrev)
2709{
2710 int mode;
2711 char_u *cmdp;
2712
2713 cmdp = eap->cmd;
2714 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
2715
2716 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
2717 eap->arg, mode, isabbrev))
2718 {
2719 case 1: emsg(_(e_invarg));
2720 break;
2721 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
2722 break;
2723 }
2724}
2725
2726/*
2727 * ":abbreviate" and friends.
2728 */
2729 void
2730ex_abbreviate(exarg_T *eap)
2731{
2732 do_exmap(eap, TRUE); // almost the same as mapping
2733}
2734
2735/*
2736 * ":map" and friends.
2737 */
2738 void
2739ex_map(exarg_T *eap)
2740{
2741 // If we are sourcing .exrc or .vimrc in current directory we
2742 // print the mappings for security reasons.
2743 if (secure)
2744 {
2745 secure = 2;
2746 msg_outtrans(eap->cmd);
2747 msg_putchar('\n');
2748 }
2749 do_exmap(eap, FALSE);
2750}
2751
2752/*
2753 * ":unmap" and friends.
2754 */
2755 void
2756ex_unmap(exarg_T *eap)
2757{
2758 do_exmap(eap, FALSE);
2759}
2760
2761/*
2762 * ":mapclear" and friends.
2763 */
2764 void
2765ex_mapclear(exarg_T *eap)
2766{
2767 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
2768}
2769
2770/*
2771 * ":abclear" and friends.
2772 */
2773 void
2774ex_abclear(exarg_T *eap)
2775{
2776 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
2777}