blob: 697eb3142e3667876dc267ef2622bee2eebab558 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
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 * getchar.c
12 *
13 * functions related with getting a character from the user/mapping/redo/...
14 *
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
17 */
18
19#include "vim.h"
20
21/*
22 * These buffers are used for storing:
23 * - stuffed characters: A command that is translated into another command.
24 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000025 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 *
27 * The bytes are stored like in the typeahead buffer:
28 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
29 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
30 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
31 * otherwise switching the GUI on would make mappings invalid).
32 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
33 * These translations are also done on multi-byte characters!
34 *
35 * Escaping CSI bytes is done by the system-specific input functions, called
36 * by ui_inchar().
37 * Escaping K_SPECIAL is done by inchar().
38 * Un-escaping is done by vgetc().
39 */
40
41#define MINIMAL_SIZE 20 /* minimal size for b_str */
42
43static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
46static struct buffheader save_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
47static struct buffheader save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
48#endif
49static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
50
51static int typeahead_char = 0; /* typeahead char that's not flushed */
52
53/*
54 * when block_redo is TRUE redo buffer will not be changed
55 * used by edit() to repeat insertions and 'V' command for redoing
56 */
57static int block_redo = FALSE;
58
59/*
60 * Make a hash value for a mapping.
61 * "mode" is the lower 4 bits of the State for the mapping.
62 * "c1" is the first character of the "lhs".
63 * Returns a value between 0 and 255, index in maphash.
64 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode.
65 */
Bram Moolenaar371d5402006-03-20 21:47:49 +000066#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) ? (c1) : ((c1) ^ 0x80))
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Each mapping is put in one of the 256 hash lists, to speed up finding it.
70 */
71static mapblock_T *(maphash[256]);
72static int maphash_valid = FALSE;
73
74/*
75 * List used for abbreviations.
76 */
77static mapblock_T *first_abbr = NULL; /* first entry in abbrlist */
78
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +000079static int KeyNoremap = 0; /* remapping flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * variables used by vgetorpeek() and flush_buffers()
83 *
84 * typebuf.tb_buf[] contains all characters that are not consumed yet.
85 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
86 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
87 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
88 * The head of the buffer may contain the result of mappings, abbreviations
89 * and @a commands. The length of this part is typebuf.tb_maplen.
90 * typebuf.tb_silent is the part where <silent> applies.
91 * After the head are characters that come from the terminal.
92 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
93 * should not be considered for abbreviations.
94 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
95 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
96 * contains RM_NONE for the characters that are not to be remapped.
97 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
98 * (typebuf has been put in globals.h, because check_termcode() needs it).
99 */
100#define RM_YES 0 /* tb_noremap: remap */
101#define RM_NONE 1 /* tb_noremap: don't remap */
102#define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000103#define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/* typebuf.tb_buf has three parts: room in front (for result of mappings), the
106 * middle for typeahead and room for new characters (which needs to be 3 *
107 * MAXMAPLEN) for the Amiga).
108 */
109#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
110static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
111static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
112
113static int last_recorded_len = 0; /* number of last recorded chars */
114
115static char_u *get_buffcont __ARGS((struct buffheader *, int));
116static void add_buff __ARGS((struct buffheader *, char_u *, long n));
117static void add_num_buff __ARGS((struct buffheader *, long));
118static void add_char_buff __ARGS((struct buffheader *, int));
119static int read_stuff __ARGS((int advance));
120static void start_stuff __ARGS((void));
121static int read_redo __ARGS((int, int));
122static void copy_redo __ARGS((int));
123static void init_typebuf __ARGS((void));
124static void gotchars __ARGS((char_u *, int));
125static void may_sync_undo __ARGS((void));
126static void closescript __ARGS((void));
127static int vgetorpeek __ARGS((int));
128static void map_free __ARGS((mapblock_T **));
129static void validate_maphash __ARGS((void));
130static void showmap __ARGS((mapblock_T *mp, int local));
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000131#ifdef FEAT_EVAL
Bram Moolenaarda9591e2009-09-30 13:17:02 +0000132static char_u *eval_map_expr __ARGS((char_u *str, int c));
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135/*
136 * Free and clear a buffer.
137 */
138 void
139free_buff(buf)
140 struct buffheader *buf;
141{
142 struct buffblock *p, *np;
143
144 for (p = buf->bh_first.b_next; p != NULL; p = np)
145 {
146 np = p->b_next;
147 vim_free(p);
148 }
149 buf->bh_first.b_next = NULL;
150}
151
152/*
153 * Return the contents of a buffer as a single string.
154 * K_SPECIAL and CSI in the returned string are escaped.
155 */
156 static char_u *
157get_buffcont(buffer, dozero)
158 struct buffheader *buffer;
159 int dozero; /* count == zero is not an error */
160{
161 long_u count = 0;
162 char_u *p = NULL;
163 char_u *p2;
164 char_u *str;
165 struct buffblock *bp;
166
167 /* compute the total length of the string */
168 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
169 count += (long_u)STRLEN(bp->b_str);
170
171 if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL)
172 {
173 p2 = p;
174 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
175 for (str = bp->b_str; *str; )
176 *p2++ = *str++;
177 *p2 = NUL;
178 }
179 return (p);
180}
181
182/*
183 * Return the contents of the record buffer as a single string
184 * and clear the record buffer.
185 * K_SPECIAL and CSI in the returned string are escaped.
186 */
187 char_u *
188get_recorded()
189{
190 char_u *p;
191 size_t len;
192
193 p = get_buffcont(&recordbuff, TRUE);
194 free_buff(&recordbuff);
195
196 /*
197 * Remove the characters that were added the last time, these must be the
198 * (possibly mapped) characters that stopped the recording.
199 */
200 len = STRLEN(p);
201 if ((int)len >= last_recorded_len)
202 {
203 len -= last_recorded_len;
204 p[len] = NUL;
205 }
206
207 /*
208 * When stopping recording from Insert mode with CTRL-O q, also remove the
209 * CTRL-O.
210 */
211 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
212 p[len - 1] = NUL;
213
214 return (p);
215}
216
217/*
218 * Return the contents of the redo buffer as a single string.
219 * K_SPECIAL and CSI in the returned string are escaped.
220 */
221 char_u *
222get_inserted()
223{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000224 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225}
226
227/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000228 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 * K_SPECIAL and CSI should have been escaped already.
230 */
231 static void
232add_buff(buf, s, slen)
233 struct buffheader *buf;
234 char_u *s;
235 long slen; /* length of "s" or -1 */
236{
237 struct buffblock *p;
238 long_u len;
239
240 if (slen < 0)
241 slen = (long)STRLEN(s);
242 if (slen == 0) /* don't add empty strings */
243 return;
244
245 if (buf->bh_first.b_next == NULL) /* first add to list */
246 {
247 buf->bh_space = 0;
248 buf->bh_curr = &(buf->bh_first);
249 }
250 else if (buf->bh_curr == NULL) /* buffer has already been read */
251 {
252 EMSG(_("E222: Add to read buffer"));
253 return;
254 }
255 else if (buf->bh_index != 0)
Bram Moolenaarc3b73072007-12-07 16:30:42 +0000256 mch_memmove(buf->bh_first.b_next->b_str,
257 buf->bh_first.b_next->b_str + buf->bh_index,
258 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 buf->bh_index = 0;
260
261 if (buf->bh_space >= (int)slen)
262 {
263 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000264 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 buf->bh_space -= slen;
266 }
267 else
268 {
269 if (slen < MINIMAL_SIZE)
270 len = MINIMAL_SIZE;
271 else
272 len = slen;
273 p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len),
274 TRUE);
275 if (p == NULL)
276 return; /* no space, just forget it */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000277 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000278 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280 p->b_next = buf->bh_curr->b_next;
281 buf->bh_curr->b_next = p;
282 buf->bh_curr = p;
283 }
284 return;
285}
286
287/*
288 * Add number "n" to buffer "buf".
289 */
290 static void
291add_num_buff(buf, n)
292 struct buffheader *buf;
293 long n;
294{
295 char_u number[32];
296
297 sprintf((char *)number, "%ld", n);
298 add_buff(buf, number, -1L);
299}
300
301/*
302 * Add character 'c' to buffer "buf".
303 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
304 */
305 static void
306add_char_buff(buf, c)
307 struct buffheader *buf;
308 int c;
309{
310#ifdef FEAT_MBYTE
311 char_u bytes[MB_MAXBYTES + 1];
312 int len;
313 int i;
314#endif
315 char_u temp[4];
316
317#ifdef FEAT_MBYTE
318 if (IS_SPECIAL(c))
319 len = 1;
320 else
321 len = (*mb_char2bytes)(c, bytes);
322 for (i = 0; i < len; ++i)
323 {
324 if (!IS_SPECIAL(c))
325 c = bytes[i];
326#endif
327
328 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
329 {
330 /* translate special key code into three byte sequence */
331 temp[0] = K_SPECIAL;
332 temp[1] = K_SECOND(c);
333 temp[2] = K_THIRD(c);
334 temp[3] = NUL;
335 }
336#ifdef FEAT_GUI
337 else if (c == CSI)
338 {
339 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
340 temp[0] = CSI;
341 temp[1] = KS_EXTRA;
342 temp[2] = (int)KE_CSI;
343 temp[3] = NUL;
344 }
345#endif
346 else
347 {
348 temp[0] = c;
349 temp[1] = NUL;
350 }
351 add_buff(buf, temp, -1L);
352#ifdef FEAT_MBYTE
353 }
354#endif
355}
356
357/*
358 * Get one byte from the stuff buffer.
359 * If advance == TRUE go to the next char.
360 * No translation is done K_SPECIAL and CSI are escaped.
361 */
362 static int
363read_stuff(advance)
364 int advance;
365{
366 char_u c;
367 struct buffblock *curr;
368
369 if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */
370 return NUL;
371
372 curr = stuffbuff.bh_first.b_next;
373 c = curr->b_str[stuffbuff.bh_index];
374
375 if (advance)
376 {
377 if (curr->b_str[++stuffbuff.bh_index] == NUL)
378 {
379 stuffbuff.bh_first.b_next = curr->b_next;
380 vim_free(curr);
381 stuffbuff.bh_index = 0;
382 }
383 }
384 return c;
385}
386
387/*
388 * Prepare the stuff buffer for reading (if it contains something).
389 */
390 static void
391start_stuff()
392{
393 if (stuffbuff.bh_first.b_next != NULL)
394 {
395 stuffbuff.bh_curr = &(stuffbuff.bh_first);
396 stuffbuff.bh_space = 0;
397 }
398}
399
400/*
401 * Return TRUE if the stuff buffer is empty.
402 */
403 int
404stuff_empty()
405{
406 return (stuffbuff.bh_first.b_next == NULL);
407}
408
409/*
410 * Set a typeahead character that won't be flushed.
411 */
412 void
413typeahead_noflush(c)
414 int c;
415{
416 typeahead_char = c;
417}
418
419/*
420 * Remove the contents of the stuff buffer and the mapped characters in the
421 * typeahead buffer (used in case of an error). If 'typeahead' is true,
422 * flush all typeahead characters (used when interrupted by a CTRL-C).
423 */
424 void
425flush_buffers(typeahead)
426 int typeahead;
427{
428 init_typebuf();
429
430 start_stuff();
431 while (read_stuff(TRUE) != NUL)
432 ;
433
434 if (typeahead) /* remove all typeahead */
435 {
436 /*
437 * We have to get all characters, because we may delete the first part
438 * of an escape sequence.
439 * In an xterm we get one char at a time and we have to get them all.
440 */
441 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L,
442 typebuf.tb_change_cnt) != 0)
443 ;
444 typebuf.tb_off = MAXMAPLEN;
445 typebuf.tb_len = 0;
446 }
447 else /* remove mapped characters only */
448 {
449 typebuf.tb_off += typebuf.tb_maplen;
450 typebuf.tb_len -= typebuf.tb_maplen;
451 }
452 typebuf.tb_maplen = 0;
453 typebuf.tb_silent = 0;
454 cmd_silent = FALSE;
455 typebuf.tb_no_abbr_cnt = 0;
456}
457
458/*
459 * The previous contents of the redo buffer is kept in old_redobuffer.
460 * This is used for the CTRL-O <.> command in insert mode.
461 */
462 void
463ResetRedobuff()
464{
465 if (!block_redo)
466 {
467 free_buff(&old_redobuff);
468 old_redobuff = redobuff;
469 redobuff.bh_first.b_next = NULL;
470 }
471}
472
473#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
474/*
475 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
476 * Used before executing autocommands and user functions.
477 */
478static int save_level = 0;
479
480 void
481saveRedobuff()
482{
483 char_u *s;
484
485 if (save_level++ == 0)
486 {
487 save_redobuff = redobuff;
488 redobuff.bh_first.b_next = NULL;
489 save_old_redobuff = old_redobuff;
490 old_redobuff.bh_first.b_next = NULL;
491
492 /* Make a copy, so that ":normal ." in a function works. */
493 s = get_buffcont(&save_redobuff, FALSE);
494 if (s != NULL)
495 {
496 add_buff(&redobuff, s, -1L);
497 vim_free(s);
498 }
499 }
500}
501
502/*
503 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
504 * Used after executing autocommands and user functions.
505 */
506 void
507restoreRedobuff()
508{
509 if (--save_level == 0)
510 {
511 free_buff(&redobuff);
512 redobuff = save_redobuff;
513 free_buff(&old_redobuff);
514 old_redobuff = save_old_redobuff;
515 }
516}
517#endif
518
519/*
520 * Append "s" to the redo buffer.
521 * K_SPECIAL and CSI should already have been escaped.
522 */
523 void
524AppendToRedobuff(s)
525 char_u *s;
526{
527 if (!block_redo)
528 add_buff(&redobuff, s, -1L);
529}
530
531/*
532 * Append to Redo buffer literally, escaping special characters with CTRL-V.
533 * K_SPECIAL and CSI are escaped as well.
534 */
535 void
Bram Moolenaarebefac62005-12-28 22:39:57 +0000536AppendToRedobuffLit(str, len)
537 char_u *str;
538 int len; /* length of "str" or -1 for up to the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000540 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 int c;
542 char_u *start;
543
544 if (block_redo)
545 return;
546
Bram Moolenaarebefac62005-12-28 22:39:57 +0000547 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
549 /* Put a string of normal characters in the redo buffer (that's
550 * faster). */
551 start = s;
552 while (*s >= ' '
553#ifndef EBCDIC
554 && *s < DEL /* EBCDIC: all chars above space are normal */
555#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000556 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 ++s;
558
559 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
560 * typed next. */
561 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
562 --s;
563 if (s > start)
564 add_buff(&redobuff, start, (long)(s - start));
565
Bram Moolenaarebefac62005-12-28 22:39:57 +0000566 if (*s == NUL || (len >= 0 && s - str >= len))
567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaarebefac62005-12-28 22:39:57 +0000569 /* Handle a special or multibyte character. */
570#ifdef FEAT_MBYTE
571 if (has_mbyte)
572 /* Handle composing chars separately. */
573 c = mb_cptr2char_adv(&s);
574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000576 c = *s++;
577 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
578 add_char_buff(&redobuff, Ctrl_V);
579
580 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
581 if (*s == NUL && c == '0')
582#ifdef EBCDIC
583 add_buff(&redobuff, (char_u *)"xf0", 3L);
584#else
585 add_buff(&redobuff, (char_u *)"048", 3L);
586#endif
587 else
588 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
590}
591
592/*
593 * Append a character to the redo buffer.
594 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
595 */
596 void
597AppendCharToRedobuff(c)
598 int c;
599{
600 if (!block_redo)
601 add_char_buff(&redobuff, c);
602}
603
604/*
605 * Append a number to the redo buffer.
606 */
607 void
608AppendNumberToRedobuff(n)
609 long n;
610{
611 if (!block_redo)
612 add_num_buff(&redobuff, n);
613}
614
615/*
616 * Append string "s" to the stuff buffer.
617 * CSI and K_SPECIAL must already have been escaped.
618 */
619 void
620stuffReadbuff(s)
621 char_u *s;
622{
623 add_buff(&stuffbuff, s, -1L);
624}
625
626 void
627stuffReadbuffLen(s, len)
628 char_u *s;
629 long len;
630{
631 add_buff(&stuffbuff, s, len);
632}
633
634#if defined(FEAT_EVAL) || defined(PROTO)
635/*
636 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
637 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200638 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 */
640 void
641stuffReadbuffSpec(s)
642 char_u *s;
643{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200644 int c;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 while (*s != NUL)
647 {
648 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
649 {
650 /* Insert special key literally. */
651 stuffReadbuffLen(s, 3L);
652 s += 3;
653 }
654 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#ifdef FEAT_MBYTE
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200657 c = mb_ptr2char_adv(&s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200659 c = *s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#endif
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200661 if (c == CAR || c == NL || c == ESC)
662 c = ' ';
663 stuffcharReadbuff(c);
664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666}
667#endif
668
669/*
670 * Append a character to the stuff buffer.
671 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
672 */
673 void
674stuffcharReadbuff(c)
675 int c;
676{
677 add_char_buff(&stuffbuff, c);
678}
679
680/*
681 * Append a number to the stuff buffer.
682 */
683 void
684stuffnumReadbuff(n)
685 long n;
686{
687 add_num_buff(&stuffbuff, n);
688}
689
690/*
691 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
692 * multibyte characters.
693 * The redo buffer is left as it is.
694 * if init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
695 * otherwise
696 * if old is TRUE, use old_redobuff instead of redobuff
697 */
698 static int
699read_redo(init, old_redo)
700 int init;
701 int old_redo;
702{
703 static struct buffblock *bp;
704 static char_u *p;
705 int c;
706#ifdef FEAT_MBYTE
707 int n;
708 char_u buf[MB_MAXBYTES];
709 int i;
710#endif
711
712 if (init)
713 {
714 if (old_redo)
715 bp = old_redobuff.bh_first.b_next;
716 else
717 bp = redobuff.bh_first.b_next;
718 if (bp == NULL)
719 return FAIL;
720 p = bp->b_str;
721 return OK;
722 }
723 if ((c = *p) != NUL)
724 {
725 /* Reverse the conversion done by add_char_buff() */
726#ifdef FEAT_MBYTE
727 /* For a multi-byte character get all the bytes and return the
728 * converted character. */
729 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
730 n = MB_BYTE2LEN_CHECK(c);
731 else
732 n = 1;
733 for (i = 0; ; ++i)
734#endif
735 {
736 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
737 {
738 c = TO_SPECIAL(p[1], p[2]);
739 p += 2;
740 }
741#ifdef FEAT_GUI
742 if (c == CSI) /* escaped CSI */
743 p += 2;
744#endif
745 if (*++p == NUL && bp->b_next != NULL)
746 {
747 bp = bp->b_next;
748 p = bp->b_str;
749 }
750#ifdef FEAT_MBYTE
751 buf[i] = c;
752 if (i == n - 1) /* last byte of a character */
753 {
754 if (n != 1)
755 c = (*mb_ptr2char)(buf);
756 break;
757 }
758 c = *p;
759 if (c == NUL) /* cannot happen? */
760 break;
761#endif
762 }
763 }
764
765 return c;
766}
767
768/*
769 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
770 * If old_redo is TRUE, use old_redobuff instead of redobuff.
771 * The escaped K_SPECIAL and CSI are copied without translation.
772 */
773 static void
774copy_redo(old_redo)
775 int old_redo;
776{
777 int c;
778
779 while ((c = read_redo(FALSE, old_redo)) != NUL)
780 stuffcharReadbuff(c);
781}
782
783/*
784 * Stuff the redo buffer into the stuffbuff.
785 * Insert the redo count into the command.
786 * If "old_redo" is TRUE, the last but one command is repeated
787 * instead of the last command (inserting text). This is used for
788 * CTRL-O <.> in insert mode
789 *
790 * return FAIL for failure, OK otherwise
791 */
792 int
793start_redo(count, old_redo)
794 long count;
795 int old_redo;
796{
797 int c;
798
799 /* init the pointers; return if nothing to redo */
800 if (read_redo(TRUE, old_redo) == FAIL)
801 return FAIL;
802
803 c = read_redo(FALSE, old_redo);
804
805 /* copy the buffer name, if present */
806 if (c == '"')
807 {
808 add_buff(&stuffbuff, (char_u *)"\"", 1L);
809 c = read_redo(FALSE, old_redo);
810
811 /* if a numbered buffer is used, increment the number */
812 if (c >= '1' && c < '9')
813 ++c;
814 add_char_buff(&stuffbuff, c);
815 c = read_redo(FALSE, old_redo);
816 }
817
818#ifdef FEAT_VISUAL
819 if (c == 'v') /* redo Visual */
820 {
821 VIsual = curwin->w_cursor;
822 VIsual_active = TRUE;
823 VIsual_select = FALSE;
824 VIsual_reselect = TRUE;
825 redo_VIsual_busy = TRUE;
826 c = read_redo(FALSE, old_redo);
827 }
828#endif
829
830 /* try to enter the count (in place of a previous count) */
831 if (count)
832 {
833 while (VIM_ISDIGIT(c)) /* skip "old" count */
834 c = read_redo(FALSE, old_redo);
835 add_num_buff(&stuffbuff, count);
836 }
837
838 /* copy from the redo buffer into the stuff buffer */
839 add_char_buff(&stuffbuff, c);
840 copy_redo(old_redo);
841 return OK;
842}
843
844/*
845 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
846 * the redo buffer into the stuffbuff.
847 * return FAIL for failure, OK otherwise
848 */
849 int
850start_redo_ins()
851{
852 int c;
853
854 if (read_redo(TRUE, FALSE) == FAIL)
855 return FAIL;
856 start_stuff();
857
858 /* skip the count and the command character */
859 while ((c = read_redo(FALSE, FALSE)) != NUL)
860 {
861 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
862 {
863 if (c == 'O' || c == 'o')
864 stuffReadbuff(NL_STR);
865 break;
866 }
867 }
868
869 /* copy the typed text from the redo buffer into the stuff buffer */
870 copy_redo(FALSE);
871 block_redo = TRUE;
872 return OK;
873}
874
875 void
876stop_redo_ins()
877{
878 block_redo = FALSE;
879}
880
881/*
882 * Initialize typebuf.tb_buf to point to typebuf_init.
883 * alloc() cannot be used here: In out-of-memory situations it would
884 * be impossible to type anything.
885 */
886 static void
887init_typebuf()
888{
889 if (typebuf.tb_buf == NULL)
890 {
891 typebuf.tb_buf = typebuf_init;
892 typebuf.tb_noremap = noremapbuf_init;
893 typebuf.tb_buflen = TYPELEN_INIT;
894 typebuf.tb_len = 0;
895 typebuf.tb_off = 0;
896 typebuf.tb_change_cnt = 1;
897 }
898}
899
900/*
901 * insert a string in position 'offset' in the typeahead buffer (for "@r"
902 * and ":normal" command, vgetorpeek() and check_termcode())
903 *
904 * If noremap is REMAP_YES, new string can be mapped again.
905 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000906 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
907 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
909 * script-local mappings.
910 * If noremap is > 0, that many characters of the new string cannot be mapped.
911 *
912 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
913 * offset is non-zero!).
914 *
915 * If silent is TRUE, cmd_silent is set when the characters are obtained.
916 *
917 * return FAIL for failure, OK otherwise
918 */
919 int
920ins_typebuf(str, noremap, offset, nottyped, silent)
921 char_u *str;
922 int noremap;
923 int offset;
924 int nottyped;
925 int silent;
926{
927 char_u *s1, *s2;
928 int newlen;
929 int addlen;
930 int i;
931 int newoff;
932 int val;
933 int nrm;
934
935 init_typebuf();
936 if (++typebuf.tb_change_cnt == 0)
937 typebuf.tb_change_cnt = 1;
938
939 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 /*
942 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
943 */
944 if (offset == 0 && addlen <= typebuf.tb_off)
945 {
946 typebuf.tb_off -= addlen;
947 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
948 }
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 /*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000951 * Need to allocate a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
953 * characters. We add some extra room to avoid having to allocate too
954 * often.
955 */
956 else
957 {
958 newoff = MAXMAPLEN + 4;
959 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
960 if (newlen < 0) /* string is getting too long */
961 {
962 EMSG(_(e_toocompl)); /* also calls flush_buffers */
963 setcursor();
964 return FAIL;
965 }
966 s1 = alloc(newlen);
967 if (s1 == NULL) /* out of memory */
968 return FAIL;
969 s2 = alloc(newlen);
970 if (s2 == NULL) /* out of memory */
971 {
972 vim_free(s1);
973 return FAIL;
974 }
975 typebuf.tb_buflen = newlen;
976
977 /* copy the old chars, before the insertion point */
978 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
979 (size_t)offset);
980 /* copy the new chars */
981 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
982 /* copy the old chars, after the insertion point, including the NUL at
983 * the end */
984 mch_memmove(s1 + newoff + offset + addlen,
985 typebuf.tb_buf + typebuf.tb_off + offset,
986 (size_t)(typebuf.tb_len - offset + 1));
987 if (typebuf.tb_buf != typebuf_init)
988 vim_free(typebuf.tb_buf);
989 typebuf.tb_buf = s1;
990
991 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
992 (size_t)offset);
993 mch_memmove(s2 + newoff + offset + addlen,
994 typebuf.tb_noremap + typebuf.tb_off + offset,
995 (size_t)(typebuf.tb_len - offset));
996 if (typebuf.tb_noremap != noremapbuf_init)
997 vim_free(typebuf.tb_noremap);
998 typebuf.tb_noremap = s2;
999
1000 typebuf.tb_off = newoff;
1001 }
1002 typebuf.tb_len += addlen;
1003
1004 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
1005 if (noremap == REMAP_SCRIPT)
1006 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001007 else if (noremap == REMAP_SKIP)
1008 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 else
1010 val = RM_NONE;
1011
1012 /*
1013 * Adjust typebuf.tb_noremap[] for the new characters:
1014 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1015 * (sometimes) not remappable
1016 * If noremap == REMAP_YES: all the new characters are mappable
1017 * If noremap > 0: "noremap" characters are not remappable, the rest
1018 * mappable
1019 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001020 if (noremap == REMAP_SKIP)
1021 nrm = 1;
1022 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 nrm = addlen;
1024 else
1025 nrm = noremap;
1026 for (i = 0; i < addlen; ++i)
1027 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1028 (--nrm >= 0) ? val : RM_YES;
1029
1030 /* tb_maplen and tb_silent only remember the length of mapped and/or
1031 * silent mappings at the start of the buffer, assuming that a mapped
1032 * sequence doesn't result in typed characters. */
1033 if (nottyped || typebuf.tb_maplen > offset)
1034 typebuf.tb_maplen += addlen;
1035 if (silent || typebuf.tb_silent > offset)
1036 {
1037 typebuf.tb_silent += addlen;
1038 cmd_silent = TRUE;
1039 }
1040 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1041 typebuf.tb_no_abbr_cnt += addlen;
1042
1043 return OK;
1044}
1045
1046/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001047 * Put character "c" back into the typeahead buffer.
1048 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001049 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1050 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001051 */
1052 void
1053ins_char_typebuf(c)
1054 int c;
1055{
1056#ifdef FEAT_MBYTE
1057 char_u buf[MB_MAXBYTES];
1058#else
1059 char_u buf[4];
1060#endif
1061 if (IS_SPECIAL(c))
1062 {
1063 buf[0] = K_SPECIAL;
1064 buf[1] = K_SECOND(c);
1065 buf[2] = K_THIRD(c);
1066 buf[3] = NUL;
1067 }
1068 else
1069 {
1070#ifdef FEAT_MBYTE
1071 buf[(*mb_char2bytes)(c, buf)] = NUL;
1072#else
1073 buf[0] = c;
1074 buf[1] = NUL;
1075#endif
1076 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001077 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001078}
1079
1080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001082 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001083 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1085 * changed it was reallocated and the old pointer can no longer be used.
1086 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1087 * that was just added.
1088 */
1089 int
1090typebuf_changed(tb_change_cnt)
1091 int tb_change_cnt; /* old value of typebuf.tb_change_cnt */
1092{
1093 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001094#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1095 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#endif
1097 ));
1098}
1099
1100/*
1101 * Return TRUE if there are no characters in the typeahead buffer that have
1102 * not been typed (result from a mapping or come from ":normal").
1103 */
1104 int
1105typebuf_typed()
1106{
1107 return typebuf.tb_maplen == 0;
1108}
1109
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001110#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111/*
1112 * Return the number of characters that are mapped (or not typed).
1113 */
1114 int
1115typebuf_maplen()
1116{
1117 return typebuf.tb_maplen;
1118}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121/*
1122 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1123 */
1124 void
1125del_typebuf(len, offset)
1126 int len;
1127 int offset;
1128{
1129 int i;
1130
1131 if (len == 0)
1132 return; /* nothing to do */
1133
1134 typebuf.tb_len -= len;
1135
1136 /*
1137 * Easy case: Just increase typebuf.tb_off.
1138 */
1139 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1140 >= 3 * MAXMAPLEN + 3)
1141 typebuf.tb_off += len;
1142 /*
1143 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1144 */
1145 else
1146 {
1147 i = typebuf.tb_off + offset;
1148 /*
1149 * Leave some extra room at the end to avoid reallocation.
1150 */
1151 if (typebuf.tb_off > MAXMAPLEN)
1152 {
1153 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1154 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1155 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1156 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1157 typebuf.tb_off = MAXMAPLEN;
1158 }
1159 /* adjust typebuf.tb_buf (include the NUL at the end) */
1160 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1161 typebuf.tb_buf + i + len,
1162 (size_t)(typebuf.tb_len - offset + 1));
1163 /* adjust typebuf.tb_noremap[] */
1164 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1165 typebuf.tb_noremap + i + len,
1166 (size_t)(typebuf.tb_len - offset));
1167 }
1168
1169 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1170 {
1171 if (typebuf.tb_maplen < offset + len)
1172 typebuf.tb_maplen = offset;
1173 else
1174 typebuf.tb_maplen -= len;
1175 }
1176 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1177 {
1178 if (typebuf.tb_silent < offset + len)
1179 typebuf.tb_silent = offset;
1180 else
1181 typebuf.tb_silent -= len;
1182 }
1183 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1184 {
1185 if (typebuf.tb_no_abbr_cnt < offset + len)
1186 typebuf.tb_no_abbr_cnt = offset;
1187 else
1188 typebuf.tb_no_abbr_cnt -= len;
1189 }
1190
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001191#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001192 /* Reset the flag that text received from a client or from feedkeys()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001193 * was inserted in the typeahead buffer. */
1194 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196 if (++typebuf.tb_change_cnt == 0)
1197 typebuf.tb_change_cnt = 1;
1198}
1199
1200/*
1201 * Write typed characters to script file.
1202 * If recording is on put the character in the recordbuffer.
1203 */
1204 static void
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001205gotchars(chars, len)
1206 char_u *chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 int len;
1208{
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001209 char_u *s = chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 int c;
1211 char_u buf[2];
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001212 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214 /* remember how many chars were last recorded */
1215 if (Recording)
1216 last_recorded_len += len;
1217
1218 buf[1] = NUL;
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001219 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
1221 /* Handle one byte at a time; no translation to be done. */
1222 c = *s++;
1223 updatescript(c);
1224
1225 if (Recording)
1226 {
1227 buf[0] = c;
1228 add_buff(&recordbuff, buf, 1L);
1229 }
1230 }
1231 may_sync_undo();
1232
1233#ifdef FEAT_EVAL
1234 /* output "debug mode" message next time in debug mode */
1235 debug_did_msg = FALSE;
1236#endif
1237
1238 /* Since characters have been typed, consider the following to be in
1239 * another mapping. Search string will be kept in history. */
1240 ++maptick;
1241}
1242
1243/*
1244 * Sync undo. Called when typed characters are obtained from the typeahead
1245 * buffer, or when a menu is used.
1246 * Do not sync:
1247 * - In Insert mode, unless cursor key has been used.
1248 * - While reading a script file.
1249 * - When no_u_sync is non-zero.
1250 */
1251 static void
1252may_sync_undo()
1253{
1254 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001255 && scriptin[curscript] == NULL)
1256 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257}
1258
1259/*
1260 * Make "typebuf" empty and allocate new buffers.
1261 * Returns FAIL when out of memory.
1262 */
1263 int
1264alloc_typebuf()
1265{
1266 typebuf.tb_buf = alloc(TYPELEN_INIT);
1267 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1268 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1269 {
1270 free_typebuf();
1271 return FAIL;
1272 }
1273 typebuf.tb_buflen = TYPELEN_INIT;
1274 typebuf.tb_off = 0;
1275 typebuf.tb_len = 0;
1276 typebuf.tb_maplen = 0;
1277 typebuf.tb_silent = 0;
1278 typebuf.tb_no_abbr_cnt = 0;
1279 if (++typebuf.tb_change_cnt == 0)
1280 typebuf.tb_change_cnt = 1;
1281 return OK;
1282}
1283
1284/*
1285 * Free the buffers of "typebuf".
1286 */
1287 void
1288free_typebuf()
1289{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001290 if (typebuf.tb_buf == typebuf_init)
1291 EMSG2(_(e_intern2), "Free typebuf 1");
1292 else
1293 vim_free(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001294 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001295 EMSG2(_(e_intern2), "Free typebuf 2");
1296 else
1297 vim_free(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298}
1299
1300/*
1301 * When doing ":so! file", the current typeahead needs to be saved, and
1302 * restored when "file" has been read completely.
1303 */
1304static typebuf_T saved_typebuf[NSCRIPT];
1305
1306 int
1307save_typebuf()
1308{
1309 init_typebuf();
1310 saved_typebuf[curscript] = typebuf;
1311 /* If out of memory: restore typebuf and close file. */
1312 if (alloc_typebuf() == FAIL)
1313 {
1314 closescript();
1315 return FAIL;
1316 }
1317 return OK;
1318}
1319
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001320static int old_char = -1; /* character put back by vungetc() */
1321static int old_mod_mask; /* mod_mask for ungotten character */
1322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1324
1325/*
1326 * Save all three kinds of typeahead, so that the user must type at a prompt.
1327 */
1328 void
1329save_typeahead(tp)
1330 tasave_T *tp;
1331{
1332 tp->save_typebuf = typebuf;
1333 tp->typebuf_valid = (alloc_typebuf() == OK);
1334 if (!tp->typebuf_valid)
1335 typebuf = tp->save_typebuf;
1336
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001337 tp->old_char = old_char;
1338 tp->old_mod_mask = old_mod_mask;
1339 old_char = -1;
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 tp->save_stuffbuff = stuffbuff;
1342 stuffbuff.bh_first.b_next = NULL;
1343# ifdef USE_INPUT_BUF
1344 tp->save_inputbuf = get_input_buf();
1345# endif
1346}
1347
1348/*
1349 * Restore the typeahead to what it was before calling save_typeahead().
1350 * The allocated memory is freed, can only be called once!
1351 */
1352 void
1353restore_typeahead(tp)
1354 tasave_T *tp;
1355{
1356 if (tp->typebuf_valid)
1357 {
1358 free_typebuf();
1359 typebuf = tp->save_typebuf;
1360 }
1361
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001362 old_char = tp->old_char;
1363 old_mod_mask = tp->old_mod_mask;
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 free_buff(&stuffbuff);
1366 stuffbuff = tp->save_stuffbuff;
1367# ifdef USE_INPUT_BUF
1368 set_input_buf(tp->save_inputbuf);
1369# endif
1370}
1371#endif
1372
1373/*
1374 * Open a new script file for the ":source!" command.
1375 */
1376 void
1377openscript(name, directly)
1378 char_u *name;
1379 int directly; /* when TRUE execute directly */
1380{
1381 if (curscript + 1 == NSCRIPT)
1382 {
1383 EMSG(_(e_nesting));
1384 return;
1385 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001386#ifdef FEAT_EVAL
1387 if (ignore_script)
1388 /* Not reading from script, also don't open one. Warning message? */
1389 return;
1390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 if (scriptin[curscript] != NULL) /* already reading script */
1393 ++curscript;
1394 /* use NameBuff for expanded name */
1395 expand_env(name, NameBuff, MAXPATHL);
1396 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1397 {
1398 EMSG2(_(e_notopen), name);
1399 if (curscript)
1400 --curscript;
1401 return;
1402 }
1403 if (save_typebuf() == FAIL)
1404 return;
1405
1406 /*
1407 * Execute the commands from the file right now when using ":source!"
1408 * after ":global" or ":argdo" or in a loop. Also when another command
1409 * follows. This means the display won't be updated. Don't do this
1410 * always, "make test" would fail.
1411 */
1412 if (directly)
1413 {
1414 oparg_T oa;
1415 int oldcurscript;
1416 int save_State = State;
1417 int save_restart_edit = restart_edit;
1418 int save_insertmode = p_im;
1419 int save_finish_op = finish_op;
1420 int save_msg_scroll = msg_scroll;
1421
1422 State = NORMAL;
1423 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1424 restart_edit = 0; /* don't go to Insert mode */
1425 p_im = FALSE; /* don't use 'insertmode' */
1426 clear_oparg(&oa);
1427 finish_op = FALSE;
1428
1429 oldcurscript = curscript;
1430 do
1431 {
1432 update_topline_cursor(); /* update cursor position and topline */
1433 normal_cmd(&oa, FALSE); /* execute one command */
1434 vpeekc(); /* check for end of file */
1435 }
1436 while (scriptin[oldcurscript] != NULL);
1437
1438 State = save_State;
1439 msg_scroll = save_msg_scroll;
1440 restart_edit = save_restart_edit;
1441 p_im = save_insertmode;
1442 finish_op = save_finish_op;
1443 }
1444}
1445
1446/*
1447 * Close the currently active input script.
1448 */
1449 static void
1450closescript()
1451{
1452 free_typebuf();
1453 typebuf = saved_typebuf[curscript];
1454
1455 fclose(scriptin[curscript]);
1456 scriptin[curscript] = NULL;
1457 if (curscript > 0)
1458 --curscript;
1459}
1460
Bram Moolenaarea408852005-06-25 22:49:46 +00001461#if defined(EXITFREE) || defined(PROTO)
1462 void
1463close_all_scripts()
1464{
1465 while (scriptin[0] != NULL)
1466 closescript();
1467}
1468#endif
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1471/*
1472 * Return TRUE when reading keys from a script file.
1473 */
1474 int
1475using_script()
1476{
1477 return scriptin[curscript] != NULL;
1478}
1479#endif
1480
1481/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001482 * This function is called just before doing a blocking wait. Thus after
1483 * waiting 'updatetime' for a character to arrive.
1484 */
1485 void
1486before_blocking()
1487{
1488 updatescript(0);
1489#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001490 if (may_garbage_collect)
1491 garbage_collect();
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001492#endif
1493}
1494
1495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 * updatescipt() is called when a character can be written into the script file
1497 * or when we have waited some time for a character (c == 0)
1498 *
1499 * All the changed memfiles are synced if c == 0 or when the number of typed
1500 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1501 */
1502 void
1503updatescript(c)
1504 int c;
1505{
1506 static int count = 0;
1507
1508 if (c && scriptout)
1509 putc(c, scriptout);
1510 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1511 {
1512 ml_sync_all(c == 0, TRUE);
1513 count = 0;
1514 }
1515}
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
1518 * Get the next input character.
1519 * Can return a special key or a multi-byte character.
1520 * Can return NUL when called recursively, use safe_vgetc() if that's not
1521 * wanted.
1522 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1523 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001524 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
1526 int
1527vgetc()
1528{
1529 int c, c2;
1530#ifdef FEAT_MBYTE
1531 int n;
1532 char_u buf[MB_MAXBYTES];
1533 int i;
1534#endif
1535
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001536#ifdef FEAT_EVAL
1537 /* Do garbage collection when garbagecollect() was called previously and
1538 * we are now at the toplevel. */
1539 if (may_garbage_collect && want_garbage_collect)
1540 garbage_collect();
1541#endif
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /*
1544 * If a character was put back with vungetc, it was already processed.
1545 * Return it directly.
1546 */
1547 if (old_char != -1)
1548 {
1549 c = old_char;
1550 old_char = -1;
1551 mod_mask = old_mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001553 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001555 mod_mask = 0x0;
1556 last_recorded_len = 0;
1557 for (;;) /* this is done twice if there are modifiers */
1558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (mod_mask) /* no mapping after modifier has been read */
1560 {
1561 ++no_mapping;
1562 ++allow_keys;
1563 }
1564 c = vgetorpeek(TRUE);
1565 if (mod_mask)
1566 {
1567 --no_mapping;
1568 --allow_keys;
1569 }
1570
1571 /* Get two extra bytes for special keys */
1572 if (c == K_SPECIAL
1573#ifdef FEAT_GUI
1574 || c == CSI
1575#endif
1576 )
1577 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001578 int save_allow_keys = allow_keys;
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 ++no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001581 allow_keys = 0; /* make sure BS is not found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1583 c = vgetorpeek(TRUE);
1584 --no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001585 allow_keys = save_allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (c2 == KS_MODIFIER)
1587 {
1588 mod_mask = c;
1589 continue;
1590 }
1591 c = TO_SPECIAL(c2, c);
1592
1593#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1594 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1595 * know that a menu was torn off */
1596 if (c == K_TEAROFF)
1597 {
1598 char_u name[200];
1599 int i;
1600
1601 /* get menu path, it ends with a <CR> */
1602 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1603 {
1604 name[i] = c;
1605 if (i < 199)
1606 ++i;
1607 }
1608 name[i] = NUL;
1609 gui_make_tearoff(name);
1610 continue;
1611 }
1612#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001613#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001614 /* GTK: <F10> normally selects the menu, but it's passed until
1615 * here to allow mapping it. Intercept and invoke the GTK
1616 * behavior if it's not mapped. */
1617 if (c == K_F10 && gui.menubar != NULL)
1618 {
1619 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1620 continue;
1621 }
1622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#ifdef FEAT_GUI
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001624 /* Handle focus event here, so that the caller doesn't need to
1625 * know about it. Return K_IGNORE so that we loop once (needed if
1626 * 'lazyredraw' is set). */
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001627 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1628 {
1629 ui_focus_change(c == K_FOCUSGAINED);
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001630 c = K_IGNORE;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001631 }
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /* Translate K_CSI to CSI. The special key is only used to avoid
1634 * it being recognized as the start of a special key. */
1635 if (c == K_CSI)
1636 c = CSI;
1637#endif
1638 }
1639#ifdef MSDOS
1640 /*
1641 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1642 * Delete the 3 here.
1643 */
1644 else if (c == K_NUL && vpeekc() == 3)
1645 (void)vgetorpeek(TRUE);
1646#endif
1647
Bram Moolenaara88d9682005-03-25 21:45:43 +00001648 /* a keypad or special function key was not mapped, use it like
1649 * its ASCII equivalent */
1650 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
Bram Moolenaara88d9682005-03-25 21:45:43 +00001652 case K_KPLUS: c = '+'; break;
1653 case K_KMINUS: c = '-'; break;
1654 case K_KDIVIDE: c = '/'; break;
1655 case K_KMULTIPLY: c = '*'; break;
1656 case K_KENTER: c = CAR; break;
1657 case K_KPOINT:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001658#ifdef WIN32
Bram Moolenaara88d9682005-03-25 21:45:43 +00001659 /* Can be either '.' or a ',', *
1660 * depending on the type of keypad. */
1661 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001662#else
Bram Moolenaara88d9682005-03-25 21:45:43 +00001663 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001664#endif
Bram Moolenaara88d9682005-03-25 21:45:43 +00001665 case K_K0: c = '0'; break;
1666 case K_K1: c = '1'; break;
1667 case K_K2: c = '2'; break;
1668 case K_K3: c = '3'; break;
1669 case K_K4: c = '4'; break;
1670 case K_K5: c = '5'; break;
1671 case K_K6: c = '6'; break;
1672 case K_K7: c = '7'; break;
1673 case K_K8: c = '8'; break;
1674 case K_K9: c = '9'; break;
1675
1676 case K_XHOME:
1677 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1678 {
1679 c = K_S_HOME;
1680 mod_mask = 0;
1681 }
1682 else if (mod_mask == MOD_MASK_CTRL)
1683 {
1684 c = K_C_HOME;
1685 mod_mask = 0;
1686 }
1687 else
1688 c = K_HOME;
1689 break;
1690 case K_XEND:
1691 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1692 {
1693 c = K_S_END;
1694 mod_mask = 0;
1695 }
1696 else if (mod_mask == MOD_MASK_CTRL)
1697 {
1698 c = K_C_END;
1699 mod_mask = 0;
1700 }
1701 else
1702 c = K_END;
1703 break;
1704
1705 case K_XUP: c = K_UP; break;
1706 case K_XDOWN: c = K_DOWN; break;
1707 case K_XLEFT: c = K_LEFT; break;
1708 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710
1711#ifdef FEAT_MBYTE
1712 /* For a multi-byte character get all the bytes and return the
1713 * converted character.
1714 * Note: This will loop until enough bytes are received!
1715 */
1716 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1717 {
1718 ++no_mapping;
1719 buf[0] = c;
1720 for (i = 1; i < n; ++i)
1721 {
1722 buf[i] = vgetorpeek(TRUE);
1723 if (buf[i] == K_SPECIAL
1724#ifdef FEAT_GUI
1725 || buf[i] == CSI
1726#endif
1727 )
1728 {
1729 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1730 * which represents a K_SPECIAL (0x80),
1731 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1732 * a CSI (0x9B),
1733 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1734 c = vgetorpeek(TRUE);
1735 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1736 buf[i] = CSI;
1737 }
1738 }
1739 --no_mapping;
1740 c = (*mb_ptr2char)(buf);
1741 }
1742#endif
1743
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001744 break;
1745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001747
1748#ifdef FEAT_EVAL
1749 /*
1750 * In the main loop "may_garbage_collect" can be set to do garbage
1751 * collection in the first next vgetc(). It's disabled after that to
1752 * avoid internally used Lists and Dicts to be freed.
1753 */
1754 may_garbage_collect = FALSE;
1755#endif
1756
1757 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760/*
1761 * Like vgetc(), but never return a NUL when called recursively, get a key
1762 * directly from the user (ignoring typeahead).
1763 */
1764 int
1765safe_vgetc()
1766{
1767 int c;
1768
1769 c = vgetc();
1770 if (c == NUL)
1771 c = get_keystroke();
1772 return c;
1773}
1774
1775/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001776 * Like safe_vgetc(), but loop to handle K_IGNORE.
1777 * Also ignore scrollbar events.
1778 */
1779 int
1780plain_vgetc()
1781{
1782 int c;
1783
1784 do
1785 {
1786 c = safe_vgetc();
1787 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1788 return c;
1789}
1790
1791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 * Check if a character is available, such that vgetc() will not block.
1793 * If the next character is a special character or multi-byte, the returned
1794 * character is not valid!.
1795 */
1796 int
1797vpeekc()
1798{
1799 if (old_char != -1)
1800 return old_char;
1801 return vgetorpeek(FALSE);
1802}
1803
1804#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1805/*
1806 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1807 * codes.
1808 */
1809 int
1810vpeekc_nomap()
1811{
1812 int c;
1813
1814 ++no_mapping;
1815 ++allow_keys;
1816 c = vpeekc();
1817 --no_mapping;
1818 --allow_keys;
1819 return c;
1820}
1821#endif
1822
1823#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1824/*
1825 * Check if any character is available, also half an escape sequence.
1826 * Trick: when no typeahead found, but there is something in the typeahead
1827 * buffer, it must be an ESC that is recognized as the start of a key code.
1828 */
1829 int
1830vpeekc_any()
1831{
1832 int c;
1833
1834 c = vpeekc();
1835 if (c == NUL && typebuf.tb_len > 0)
1836 c = ESC;
1837 return c;
1838}
1839#endif
1840
1841/*
1842 * Call vpeekc() without causing anything to be mapped.
1843 * Return TRUE if a character is available, FALSE otherwise.
1844 */
1845 int
1846char_avail()
1847{
1848 int retval;
1849
1850 ++no_mapping;
1851 retval = vpeekc();
1852 --no_mapping;
1853 return (retval != NUL);
1854}
1855
1856 void
1857vungetc(c) /* unget one character (can only be done once!) */
1858 int c;
1859{
1860 old_char = c;
1861 old_mod_mask = mod_mask;
1862}
1863
1864/*
1865 * get a character:
1866 * 1. from the stuffbuffer
1867 * This is used for abbreviated commands like "D" -> "d$".
1868 * Also used to redo a command for ".".
1869 * 2. from the typeahead buffer
1870 * Stores text obtained previously but not used yet.
1871 * Also stores the result of mappings.
1872 * Also used for the ":normal" command.
1873 * 3. from the user
1874 * This may do a blocking wait if "advance" is TRUE.
1875 *
1876 * if "advance" is TRUE (vgetc()):
1877 * really get the character.
1878 * KeyTyped is set to TRUE in the case the user typed the key.
1879 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1880 * if "advance" is FALSE (vpeekc()):
1881 * just look whether there is a character available.
1882 *
1883 * When "no_mapping" is zero, checks for mappings in the current mode.
1884 * Only returns one byte (of a multi-byte character).
1885 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1886 */
1887 static int
1888vgetorpeek(advance)
1889 int advance;
1890{
1891 int c, c1;
1892 int keylen;
1893 char_u *s;
1894 mapblock_T *mp;
1895#ifdef FEAT_LOCALMAP
1896 mapblock_T *mp2;
1897#endif
1898 mapblock_T *mp_match;
1899 int mp_match_len = 0;
1900 int timedout = FALSE; /* waited for more than 1 second
1901 for mapping to complete */
1902 int mapdepth = 0; /* check for recursive mapping */
1903 int mode_deleted = FALSE; /* set when mode has been deleted */
1904 int local_State;
1905 int mlen;
1906 int max_mlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 int i;
Bram Moolenaar4e427192006-03-10 21:34:27 +00001908#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 int new_wcol, new_wrow;
1910#endif
1911#ifdef FEAT_GUI
1912# ifdef FEAT_MENU
1913 int idx;
1914# endif
1915 int shape_changed = FALSE; /* adjusted cursor shape */
1916#endif
1917 int n;
1918#ifdef FEAT_LANGMAP
1919 int nolmaplen;
1920#endif
1921 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00001922 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 /*
1925 * This function doesn't work very well when called recursively. This may
1926 * happen though, because of:
1927 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1928 * there is a character available, which calls this function. In that
1929 * case we must return NUL, to indicate no character is available.
1930 * 2. A GUI callback function writes to the screen, causing a
1931 * wait_return().
1932 * Using ":normal" can also do this, but it saves the typeahead buffer,
1933 * thus it should be OK. But don't get a key from the user then.
1934 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001935 if (vgetc_busy > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_EX_EXTRA
1937 && ex_normal_busy == 0
1938#endif
1939 )
1940 return NUL;
1941
1942 local_State = get_real_state();
1943
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001944 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 if (advance)
1947 KeyStuffed = FALSE;
1948
1949 init_typebuf();
1950 start_stuff();
1951 if (advance && typebuf.tb_maplen == 0)
1952 Exec_reg = FALSE;
1953 do
1954 {
1955/*
1956 * get a character: 1. from the stuffbuffer
1957 */
1958 if (typeahead_char != 0)
1959 {
1960 c = typeahead_char;
1961 if (advance)
1962 typeahead_char = 0;
1963 }
1964 else
1965 c = read_stuff(advance);
1966 if (c != NUL && !got_int)
1967 {
1968 if (advance)
1969 {
1970 /* KeyTyped = FALSE; When the command that stuffed something
1971 * was typed, behave like the stuffed command was typed.
1972 * needed for CTRL-W CTRl-] to open a fold, for example. */
1973 KeyStuffed = TRUE;
1974 }
1975 if (typebuf.tb_no_abbr_cnt == 0)
1976 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1977 }
1978 else
1979 {
1980 /*
1981 * Loop until we either find a matching mapped key, or we
1982 * are sure that it is not a mapped key.
1983 * If a mapped key sequence is found we go back to the start to
1984 * try re-mapping.
1985 */
1986 for (;;)
1987 {
1988 /*
1989 * ui_breakcheck() is slow, don't use it too often when
1990 * inside a mapping. But call it each time for typed
1991 * characters.
1992 */
1993 if (typebuf.tb_maplen)
1994 line_breakcheck();
1995 else
1996 ui_breakcheck(); /* check for CTRL-C */
1997 keylen = 0;
1998 if (got_int)
1999 {
2000 /* flush all input */
2001 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
2002 typebuf.tb_change_cnt);
2003 /*
2004 * If inchar() returns TRUE (script file was active) or we
2005 * are inside a mapping, get out of insert mode.
2006 * Otherwise we behave like having gotten a CTRL-C.
2007 * As a result typing CTRL-C in insert mode will
2008 * really insert a CTRL-C.
2009 */
2010 if ((c || typebuf.tb_maplen)
2011 && (State & (INSERT + CMDLINE)))
2012 c = ESC;
2013 else
2014 c = Ctrl_C;
2015 flush_buffers(TRUE); /* flush all typeahead */
2016
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002017 if (advance)
2018 {
2019 /* Also record this character, it might be needed to
2020 * get out of Insert mode. */
2021 *typebuf.tb_buf = c;
2022 gotchars(typebuf.tb_buf, 1);
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 cmd_silent = FALSE;
2025
2026 break;
2027 }
2028 else if (typebuf.tb_len > 0)
2029 {
2030 /*
2031 * Check for a mappable key sequence.
2032 * Walk through one maphash[] list until we find an
2033 * entry that matches.
2034 *
2035 * Don't look for mappings if:
2036 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2037 * - maphash_valid not set: no mappings present.
2038 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2039 * - in insert or cmdline mode and 'paste' option set
2040 * - waiting for "hit return to continue" and CR or SPACE
2041 * typed
2042 * - waiting for a char with --more--
2043 * - in Ctrl-X mode, and we get a valid char for that mode
2044 */
2045 mp = NULL;
2046 max_mlen = 0;
2047 c1 = typebuf.tb_buf[typebuf.tb_off];
2048 if (no_mapping == 0 && maphash_valid
2049 && (no_zero_mapping == 0 || c1 != '0')
2050 && (typebuf.tb_maplen == 0
2051 || (p_remap
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002052 && (typebuf.tb_noremap[typebuf.tb_off]
2053 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 && !(p_paste && (State & (INSERT + CMDLINE)))
2055 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2056 && State != ASKMORE
2057 && State != CONFIRM
2058#ifdef FEAT_INS_EXPAND
2059 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002060 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2062#endif
2063 )
2064 {
2065#ifdef FEAT_LANGMAP
2066 if (c1 == K_SPECIAL)
2067 nolmaplen = 2;
2068 else
2069 {
2070 LANGMAP_ADJUST(c1, TRUE);
2071 nolmaplen = 0;
2072 }
2073#endif
2074#ifdef FEAT_LOCALMAP
2075 /* First try buffer-local mappings. */
2076 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2077 mp2 = maphash[MAP_HASH(local_State, c1)];
2078 if (mp == NULL)
2079 {
2080 mp = mp2;
2081 mp2 = NULL;
2082 }
2083#else
2084 mp = maphash[MAP_HASH(local_State, c1)];
2085#endif
2086 /*
2087 * Loop until a partly matching mapping is found or
2088 * all (local) mappings have been checked.
2089 * The longest full match is remembered in "mp_match".
2090 * A full match is only accepted if there is no partly
2091 * match, so "aa" and "aaa" can both be mapped.
2092 */
2093 mp_match = NULL;
2094 mp_match_len = 0;
2095 for ( ; mp != NULL;
2096#ifdef FEAT_LOCALMAP
2097 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2098#endif
2099 (mp = mp->m_next))
2100 {
2101 /*
2102 * Only consider an entry if the first character
2103 * matches and it is for the current state.
2104 * Skip ":lmap" mappings if keys were mapped.
2105 */
2106 if (mp->m_keys[0] == c1
2107 && (mp->m_mode & local_State)
2108 && ((mp->m_mode & LANGMAP) == 0
2109 || typebuf.tb_maplen == 0))
2110 {
2111#ifdef FEAT_LANGMAP
2112 int nomap = nolmaplen;
2113 int c2;
2114#endif
2115 /* find the match length of this mapping */
2116 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2117 {
2118#ifdef FEAT_LANGMAP
2119 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2120 if (nomap > 0)
2121 --nomap;
2122 else if (c2 == K_SPECIAL)
2123 nomap = 2;
2124 else
2125 LANGMAP_ADJUST(c2, TRUE);
2126 if (mp->m_keys[mlen] != c2)
2127#else
2128 if (mp->m_keys[mlen] !=
2129 typebuf.tb_buf[typebuf.tb_off + mlen])
2130#endif
2131 break;
2132 }
2133
2134#ifdef FEAT_MBYTE
2135 /* Don't allow mapping the first byte(s) of a
2136 * multi-byte char. Happens when mapping
2137 * <M-a> and then changing 'encoding'. */
2138 if (has_mbyte && MB_BYTE2LEN(c1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002139 > (*mb_ptr2len)(mp->m_keys))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 mlen = 0;
2141#endif
2142 /*
2143 * Check an entry whether it matches.
2144 * - Full match: mlen == keylen
2145 * - Partly match: mlen == typebuf.tb_len
2146 */
2147 keylen = mp->m_keylen;
2148 if (mlen == keylen
2149 || (mlen == typebuf.tb_len
2150 && typebuf.tb_len < keylen))
2151 {
2152 /*
2153 * If only script-local mappings are
2154 * allowed, check if the mapping starts
2155 * with K_SNR.
2156 */
2157 s = typebuf.tb_noremap + typebuf.tb_off;
2158 if (*s == RM_SCRIPT
2159 && (mp->m_keys[0] != K_SPECIAL
2160 || mp->m_keys[1] != KS_EXTRA
2161 || mp->m_keys[2]
2162 != (int)KE_SNR))
2163 continue;
2164 /*
2165 * If one of the typed keys cannot be
2166 * remapped, skip the entry.
2167 */
2168 for (n = mlen; --n >= 0; )
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002169 if (*s++ & (RM_NONE|RM_ABBR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 break;
2171 if (n >= 0)
2172 continue;
2173
2174 if (keylen > typebuf.tb_len)
2175 {
2176 if (!timedout)
2177 {
2178 /* break at a partly match */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002179 keylen = KEYLEN_PART_MAP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 break;
2181 }
2182 }
2183 else if (keylen > mp_match_len)
2184 {
2185 /* found a longer match */
2186 mp_match = mp;
2187 mp_match_len = keylen;
2188 }
2189 }
2190 else
2191 /* No match; may have to check for
2192 * termcode at next character. */
2193 if (max_mlen < mlen)
2194 max_mlen = mlen;
2195 }
2196 }
2197
2198 /* If no partly match found, use the longest full
2199 * match. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002200 if (keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
2202 mp = mp_match;
2203 keylen = mp_match_len;
2204 }
2205 }
2206
2207 /* Check for match with 'pastetoggle' */
2208 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2209 {
2210 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2211 ++mlen)
2212 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2213 + mlen])
2214 break;
2215 if (p_pt[mlen] == NUL) /* match */
2216 {
2217 /* write chars to script file(s) */
2218 if (mlen > typebuf.tb_maplen)
2219 gotchars(typebuf.tb_buf + typebuf.tb_off
2220 + typebuf.tb_maplen,
2221 mlen - typebuf.tb_maplen);
2222
2223 del_typebuf(mlen, 0); /* remove the chars */
2224 set_option_value((char_u *)"paste",
2225 (long)!p_paste, NULL, 0);
2226 if (!(State & INSERT))
2227 {
2228 msg_col = 0;
2229 msg_row = Rows - 1;
2230 msg_clr_eos(); /* clear ruler */
2231 }
2232 showmode();
2233 setcursor();
2234 continue;
2235 }
2236 /* Need more chars for partly match. */
2237 if (mlen == typebuf.tb_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002238 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 else if (max_mlen < mlen)
2240 /* no match, may have to check for termcode at
2241 * next character */
2242 max_mlen = mlen + 1;
2243 }
2244
2245 if ((mp == NULL || max_mlen >= mp_match_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002246 && keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002248 int save_keylen = keylen;
2249
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 /*
2251 * When no matching mapping found or found a
2252 * non-matching mapping that matches at least what the
2253 * matching mapping matched:
2254 * Check if we have a terminal code, when:
2255 * mapping is allowed,
2256 * keys have not been mapped,
2257 * and not an ESC sequence, not in insert mode or
2258 * p_ek is on,
2259 * and when not timed out,
2260 */
2261 if ((no_mapping == 0 || allow_keys != 0)
2262 && (typebuf.tb_maplen == 0
2263 || (p_remap && typebuf.tb_noremap[
2264 typebuf.tb_off] == RM_YES))
2265 && !timedout)
2266 {
2267 keylen = check_termcode(max_mlen + 1, NULL, 0);
2268
Bram Moolenaarf2330482008-06-24 20:19:36 +00002269 /* If no termcode matched but 'pastetoggle'
2270 * matched partially it's like an incomplete key
2271 * sequence. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002272 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2273 keylen = KEYLEN_PART_KEY;
Bram Moolenaarf2330482008-06-24 20:19:36 +00002274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 /*
2276 * When getting a partial match, but the last
2277 * characters were not typed, don't wait for a
2278 * typed character to complete the termcode.
2279 * This helps a lot when a ":normal" command ends
2280 * in an ESC.
2281 */
2282 if (keylen < 0
2283 && typebuf.tb_len == typebuf.tb_maplen)
2284 keylen = 0;
2285 }
2286 else
2287 keylen = 0;
2288 if (keylen == 0) /* no matching terminal code */
2289 {
2290#ifdef AMIGA /* check for window bounds report */
2291 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2292 typebuf.tb_off] & 0xff) == CSI)
2293 {
2294 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2295 s < typebuf.tb_buf + typebuf.tb_off
2296 + typebuf.tb_len
2297 && (VIM_ISDIGIT(*s) || *s == ';'
2298 || *s == ' ');
2299 ++s)
2300 ;
2301 if (*s == 'r' || *s == '|') /* found one */
2302 {
2303 del_typebuf((int)(s + 1 -
2304 (typebuf.tb_buf + typebuf.tb_off)), 0);
2305 /* get size and redraw screen */
2306 shell_resized();
2307 continue;
2308 }
2309 if (*s == NUL) /* need more characters */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002310 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 if (keylen >= 0)
2313#endif
2314 /* When there was a matching mapping and no
2315 * termcode could be replaced after another one,
Bram Moolenaarf2330482008-06-24 20:19:36 +00002316 * use that mapping (loop around). If there was
2317 * no mapping use the character from the
2318 * typeahead buffer right here. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (mp == NULL)
2320 {
2321/*
2322 * get a character: 2. from the typeahead buffer
2323 */
2324 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2325 if (advance) /* remove chars from tb_buf */
2326 {
2327 cmd_silent = (typebuf.tb_silent > 0);
2328 if (typebuf.tb_maplen > 0)
2329 KeyTyped = FALSE;
2330 else
2331 {
2332 KeyTyped = TRUE;
2333 /* write char to script file(s) */
2334 gotchars(typebuf.tb_buf
2335 + typebuf.tb_off, 1);
2336 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00002337 KeyNoremap = typebuf.tb_noremap[
2338 typebuf.tb_off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 del_typebuf(1, 0);
2340 }
2341 break; /* got character, break for loop */
2342 }
2343 }
2344 if (keylen > 0) /* full matching terminal code */
2345 {
2346#if defined(FEAT_GUI) && defined(FEAT_MENU)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002347 if (typebuf.tb_len >= 2
2348 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 && typebuf.tb_buf[typebuf.tb_off + 1]
2350 == KS_MENU)
2351 {
2352 /*
2353 * Using a menu may cause a break in undo!
2354 * It's like using gotchars(), but without
2355 * recording or writing to a script file.
2356 */
2357 may_sync_undo();
2358 del_typebuf(3, 0);
2359 idx = get_menu_index(current_menu, local_State);
2360 if (idx != MENU_INDEX_INVALID)
2361 {
2362# ifdef FEAT_VISUAL
2363 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002364 * In Select mode and a Visual mode menu
2365 * is used: Switch to Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * temporarily. Append K_SELECT to switch
2367 * back to Select mode.
2368 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002369 if (VIsual_active && VIsual_select
2370 && (current_menu->modes & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 VIsual_select = FALSE;
2373 (void)ins_typebuf(K_SELECT_STRING,
2374 REMAP_NONE, 0, TRUE, FALSE);
2375 }
2376# endif
2377 ins_typebuf(current_menu->strings[idx],
2378 current_menu->noremap[idx],
2379 0, TRUE,
2380 current_menu->silent[idx]);
2381 }
2382 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002383#endif /* FEAT_GUI && FEAT_MENU */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 continue; /* try mapping again */
2385 }
2386
2387 /* Partial match: get some more characters. When a
2388 * matching mapping was found use that one. */
2389 if (mp == NULL || keylen < 0)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002390 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392 keylen = mp_match_len;
2393 }
2394
2395 /* complete match */
2396 if (keylen >= 0 && keylen <= typebuf.tb_len)
2397 {
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002398#ifdef FEAT_EVAL
2399 int save_m_expr;
2400 int save_m_noremap;
2401 int save_m_silent;
2402 char_u *save_m_keys;
2403 char_u *save_m_str;
2404#else
2405# define save_m_noremap mp->m_noremap
2406# define save_m_silent mp->m_silent
2407#endif
2408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 /* write chars to script file(s) */
2410 if (keylen > typebuf.tb_maplen)
2411 gotchars(typebuf.tb_buf + typebuf.tb_off
2412 + typebuf.tb_maplen,
2413 keylen - typebuf.tb_maplen);
2414
2415 cmd_silent = (typebuf.tb_silent > 0);
2416 del_typebuf(keylen, 0); /* remove the mapped keys */
2417
2418 /*
2419 * Put the replacement string in front of mapstr.
2420 * The depth check catches ":map x y" and ":map y x".
2421 */
2422 if (++mapdepth >= p_mmd)
2423 {
2424 EMSG(_("E223: recursive mapping"));
2425 if (State & CMDLINE)
2426 redrawcmdline();
2427 else
2428 setcursor();
2429 flush_buffers(FALSE);
2430 mapdepth = 0; /* for next one */
2431 c = -1;
2432 break;
2433 }
2434
2435#ifdef FEAT_VISUAL
2436 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002437 * In Select mode and a Visual mode mapping is used:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Switch to Visual mode temporarily. Append K_SELECT
2439 * to switch back to Select mode.
2440 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002441 if (VIsual_active && VIsual_select
2442 && (mp->m_mode & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 VIsual_select = FALSE;
2445 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2446 0, TRUE, FALSE);
2447 }
2448#endif
2449
Bram Moolenaar4e427192006-03-10 21:34:27 +00002450#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002451 /* Copy the values from *mp that are used, because
2452 * evaluating the expression may invoke a function
2453 * that redefines the mapping, thereby making *mp
2454 * invalid. */
2455 save_m_expr = mp->m_expr;
2456 save_m_noremap = mp->m_noremap;
2457 save_m_silent = mp->m_silent;
2458 save_m_keys = NULL; /* only saved when needed */
2459 save_m_str = NULL; /* only saved when needed */
2460
Bram Moolenaar4e427192006-03-10 21:34:27 +00002461 /*
2462 * Handle ":map <expr>": evaluate the {rhs} as an
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002463 * expression. Save and restore the typeahead so that
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002464 * getchar() can be used. Also save and restore the
2465 * command line for "normal :".
Bram Moolenaar4e427192006-03-10 21:34:27 +00002466 */
2467 if (mp->m_expr)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002468 {
2469 tasave_T tabuf;
2470 int save_vgetc_busy = vgetc_busy;
2471
2472 save_typeahead(&tabuf);
2473 if (tabuf.typebuf_valid)
2474 {
2475 vgetc_busy = 0;
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002476 save_m_keys = vim_strsave(mp->m_keys);
2477 save_m_str = vim_strsave(mp->m_str);
2478 s = eval_map_expr(save_m_str, NUL);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002479 vgetc_busy = save_vgetc_busy;
2480 }
2481 else
2482 s = NULL;
2483 restore_typeahead(&tabuf);
2484 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00002485 else
2486#endif
2487 s = mp->m_str;
2488
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 /*
2490 * Insert the 'to' part in the typebuf.tb_buf.
2491 * If 'from' field is the same as the start of the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002492 * 'to' field, don't remap the first character (but do
2493 * allow abbreviations).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 * If m_noremap is set, don't remap the whole 'to'
2495 * part.
2496 */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002497 if (s == NULL)
2498 i = FAIL;
2499 else
2500 {
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002501 int noremap;
2502
2503 if (save_m_noremap != REMAP_YES)
2504 noremap = save_m_noremap;
2505 else if (
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002506#ifdef FEAT_EVAL
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002507 STRNCMP(s, save_m_keys != NULL
2508 ? save_m_keys : mp->m_keys,
2509 (size_t)keylen)
2510#else
2511 STRNCMP(s, mp->m_keys, (size_t)keylen)
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002512#endif
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002513 != 0)
2514 noremap = REMAP_YES;
2515 else
2516 noremap = REMAP_SKIP;
2517 i = ins_typebuf(s, noremap,
2518 0, TRUE, cmd_silent || save_m_silent);
Bram Moolenaar4e427192006-03-10 21:34:27 +00002519#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002520 if (save_m_expr)
Bram Moolenaar4e427192006-03-10 21:34:27 +00002521 vim_free(s);
2522#endif
2523 }
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002524#ifdef FEAT_EVAL
2525 vim_free(save_m_keys);
2526 vim_free(save_m_str);
2527#endif
Bram Moolenaar4e427192006-03-10 21:34:27 +00002528 if (i == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 c = -1;
2531 break;
2532 }
2533 continue;
2534 }
2535 }
2536
2537/*
2538 * get a character: 3. from the user - handle <Esc> in Insert mode
2539 */
2540 /*
2541 * special case: if we get an <ESC> in insert mode and there
2542 * are no more characters at once, we pretend to go out of
2543 * insert mode. This prevents the one second delay after
2544 * typing an <ESC>. If we get something after all, we may
2545 * have to redisplay the mode. That the cursor is in the wrong
2546 * place does not matter.
2547 */
2548 c = 0;
2549#ifdef FEAT_CMDL_INFO
2550 new_wcol = curwin->w_wcol;
2551 new_wrow = curwin->w_wrow;
2552#endif
2553 if ( advance
2554 && typebuf.tb_len == 1
2555 && typebuf.tb_buf[typebuf.tb_off] == ESC
2556 && !no_mapping
2557#ifdef FEAT_EX_EXTRA
2558 && ex_normal_busy == 0
2559#endif
2560 && typebuf.tb_maplen == 0
2561 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002562 && (p_timeout
2563 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2565 + typebuf.tb_len, 3, 25L,
2566 typebuf.tb_change_cnt)) == 0)
2567 {
2568 colnr_T col = 0, vcol;
2569 char_u *ptr;
2570
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002571 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 unshowmode(TRUE);
2574 mode_deleted = TRUE;
2575 }
2576#ifdef FEAT_GUI
2577 /* may show different cursor shape */
2578 if (gui.in_use)
2579 {
2580 int save_State;
2581
2582 save_State = State;
2583 State = NORMAL;
2584 gui_update_cursor(TRUE, FALSE);
2585 State = save_State;
2586 shape_changed = TRUE;
2587 }
2588#endif
2589 validate_cursor();
2590 old_wcol = curwin->w_wcol;
2591 old_wrow = curwin->w_wrow;
2592
2593 /* move cursor left, if possible */
2594 if (curwin->w_cursor.col != 0)
2595 {
2596 if (curwin->w_wcol > 0)
2597 {
2598 if (did_ai)
2599 {
2600 /*
2601 * We are expecting to truncate the trailing
2602 * white-space, so find the last non-white
2603 * character -- webb
2604 */
2605 col = vcol = curwin->w_wcol = 0;
2606 ptr = ml_get_curline();
2607 while (col < curwin->w_cursor.col)
2608 {
2609 if (!vim_iswhite(ptr[col]))
2610 curwin->w_wcol = vcol;
2611 vcol += lbr_chartabsize(ptr + col,
2612 (colnr_T)vcol);
2613#ifdef FEAT_MBYTE
2614 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002615 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 else
2617#endif
2618 ++col;
2619 }
2620 curwin->w_wrow = curwin->w_cline_row
2621 + curwin->w_wcol / W_WIDTH(curwin);
2622 curwin->w_wcol %= W_WIDTH(curwin);
2623 curwin->w_wcol += curwin_col_off();
2624#ifdef FEAT_MBYTE
2625 col = 0; /* no correction needed */
2626#endif
2627 }
2628 else
2629 {
2630 --curwin->w_wcol;
2631#ifdef FEAT_MBYTE
2632 col = curwin->w_cursor.col - 1;
2633#endif
2634 }
2635 }
2636 else if (curwin->w_p_wrap && curwin->w_wrow)
2637 {
2638 --curwin->w_wrow;
2639 curwin->w_wcol = W_WIDTH(curwin) - 1;
2640#ifdef FEAT_MBYTE
2641 col = curwin->w_cursor.col - 1;
2642#endif
2643 }
2644#ifdef FEAT_MBYTE
2645 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2646 {
2647 /* Correct when the cursor is on the right halve
2648 * of a double-wide character. */
2649 ptr = ml_get_curline();
2650 col -= (*mb_head_off)(ptr, ptr + col);
2651 if ((*mb_ptr2cells)(ptr + col) > 1)
2652 --curwin->w_wcol;
2653 }
2654#endif
2655 }
2656 setcursor();
2657 out_flush();
2658#ifdef FEAT_CMDL_INFO
2659 new_wcol = curwin->w_wcol;
2660 new_wrow = curwin->w_wrow;
2661#endif
2662 curwin->w_wcol = old_wcol;
2663 curwin->w_wrow = old_wrow;
2664 }
2665 if (c < 0)
2666 continue; /* end of input script reached */
2667 typebuf.tb_len += c;
2668
2669 /* buffer full, don't map */
2670 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2671 {
2672 timedout = TRUE;
2673 continue;
2674 }
2675
2676#ifdef FEAT_EX_EXTRA
2677 if (ex_normal_busy > 0)
2678 {
2679# ifdef FEAT_CMDWIN
2680 static int tc = 0;
2681# endif
2682
2683 /* No typeahead left and inside ":normal". Must return
2684 * something to avoid getting stuck. When an incomplete
2685 * mapping is present, behave like it timed out. */
2686 if (typebuf.tb_len > 0)
2687 {
2688 timedout = TRUE;
2689 continue;
2690 }
2691 /* When 'insertmode' is set, ESC just beeps in Insert
2692 * mode. Use CTRL-L to make edit() return.
2693 * For the command line only CTRL-C always breaks it.
2694 * For the cmdline window: Alternate between ESC and
2695 * CTRL-C: ESC for most situations and CTRL-C to close the
2696 * cmdline window. */
2697 if (p_im && (State & INSERT))
2698 c = Ctrl_L;
2699 else if ((State & CMDLINE)
2700# ifdef FEAT_CMDWIN
2701 || (cmdwin_type > 0 && tc == ESC)
2702# endif
2703 )
2704 c = Ctrl_C;
2705 else
2706 c = ESC;
2707# ifdef FEAT_CMDWIN
2708 tc = c;
2709# endif
2710 break;
2711 }
2712#endif
2713
2714/*
2715 * get a character: 3. from the user - update display
2716 */
2717 /* In insert mode a screen update is skipped when characters
2718 * are still available. But when those available characters
2719 * are part of a mapping, and we are going to do a blocking
2720 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01002721 * changed text so far. Also for when 'lazyredraw' is set and
2722 * redrawing was postponed because there was something in the
2723 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01002724 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
2725 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 update_screen(0);
2728 setcursor(); /* put cursor back where it belongs */
2729 }
2730
2731 /*
2732 * If we have a partial match (and are going to wait for more
2733 * input from the user), show the partially matched characters
2734 * to the user with showcmd.
2735 */
2736#ifdef FEAT_CMDL_INFO
2737 i = 0;
2738#endif
2739 c1 = 0;
2740 if (typebuf.tb_len > 0 && advance && !exmode_active)
2741 {
2742 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2743 && State != HITRETURN)
2744 {
2745 /* this looks nice when typing a dead character map */
2746 if (State & INSERT
2747 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2748 + typebuf.tb_len - 1) == 1)
2749 {
2750 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2751 + typebuf.tb_len - 1], FALSE);
2752 setcursor(); /* put cursor back where it belongs */
2753 c1 = 1;
2754 }
2755#ifdef FEAT_CMDL_INFO
2756 /* need to use the col and row from above here */
2757 old_wcol = curwin->w_wcol;
2758 old_wrow = curwin->w_wrow;
2759 curwin->w_wcol = new_wcol;
2760 curwin->w_wrow = new_wrow;
2761 push_showcmd();
2762 if (typebuf.tb_len > SHOWCMD_COLS)
2763 i = typebuf.tb_len - SHOWCMD_COLS;
2764 while (i < typebuf.tb_len)
2765 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2766 + i++]);
2767 curwin->w_wcol = old_wcol;
2768 curwin->w_wrow = old_wrow;
2769#endif
2770 }
2771
2772 /* this looks nice when typing a dead character map */
2773 if ((State & CMDLINE)
2774#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2775 && cmdline_star == 0
2776#endif
2777 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2778 + typebuf.tb_len - 1) == 1)
2779 {
2780 putcmdline(typebuf.tb_buf[typebuf.tb_off
2781 + typebuf.tb_len - 1], FALSE);
2782 c1 = 1;
2783 }
2784 }
2785
2786/*
2787 * get a character: 3. from the user - get it
2788 */
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002789 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2791 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2792 !advance
2793 ? 0
2794 : ((typebuf.tb_len == 0
2795 || !(p_timeout || (p_ttimeout
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002796 && keylen == KEYLEN_PART_KEY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 ? -1L
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002798 : ((keylen == KEYLEN_PART_KEY && p_ttm >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 ? p_ttm
2800 : p_tm)), typebuf.tb_change_cnt);
2801
2802#ifdef FEAT_CMDL_INFO
2803 if (i != 0)
2804 pop_showcmd();
2805#endif
2806 if (c1 == 1)
2807 {
2808 if (State & INSERT)
2809 edit_unputchar();
2810 if (State & CMDLINE)
2811 unputcmdline();
2812 setcursor(); /* put cursor back where it belongs */
2813 }
2814
2815 if (c < 0)
2816 continue; /* end of input script reached */
2817 if (c == NUL) /* no character available */
2818 {
2819 if (!advance)
2820 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002821 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
2823 timedout = TRUE;
2824 continue;
2825 }
2826 }
2827 else
2828 { /* allow mapping for just typed characters */
2829 while (typebuf.tb_buf[typebuf.tb_off
2830 + typebuf.tb_len] != NUL)
2831 typebuf.tb_noremap[typebuf.tb_off
2832 + typebuf.tb_len++] = RM_YES;
2833#ifdef USE_IM_CONTROL
2834 /* Get IM status right after getting keys, not after the
2835 * timeout for a mapping (focus may be lost by then). */
2836 vgetc_im_active = im_get_status();
2837#endif
2838 }
2839 } /* for (;;) */
2840 } /* if (!character from stuffbuf) */
2841
2842 /* if advance is FALSE don't loop on NULs */
2843 } while (c < 0 || (advance && c == NUL));
2844
2845 /*
2846 * The "INSERT" message is taken care of here:
2847 * if we return an ESC to exit insert mode, the message is deleted
2848 * if we don't return an ESC but deleted the message before, redisplay it
2849 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002850 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002852 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
2854 if (typebuf.tb_len && !KeyTyped)
2855 redraw_cmdline = TRUE; /* delete mode later */
2856 else
2857 unshowmode(FALSE);
2858 }
2859 else if (c != ESC && mode_deleted)
2860 {
2861 if (typebuf.tb_len && !KeyTyped)
2862 redraw_cmdline = TRUE; /* show mode later */
2863 else
2864 showmode();
2865 }
2866 }
2867#ifdef FEAT_GUI
2868 /* may unshow different cursor shape */
2869 if (gui.in_use && shape_changed)
2870 gui_update_cursor(TRUE, FALSE);
2871#endif
2872
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002873 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875 return c;
2876}
2877
2878/*
2879 * inchar() - get one character from
2880 * 1. a scriptfile
2881 * 2. the keyboard
2882 *
2883 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2884 * NUL terminated (buffer length must be 'maxlen' + 1).
2885 * Minimum for "maxlen" is 3!!!!
2886 *
2887 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2888 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2889 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2890 * otherwise.
2891 *
2892 * If we got an interrupt all input is read until none is available.
2893 *
2894 * If wait_time == 0 there is no waiting for the char.
2895 * If wait_time == n we wait for n msec for a character to arrive.
2896 * If wait_time == -1 we wait forever for a character to arrive.
2897 *
2898 * Return the number of obtained characters.
2899 * Return -1 when end of input script reached.
2900 */
2901 int
2902inchar(buf, maxlen, wait_time, tb_change_cnt)
2903 char_u *buf;
2904 int maxlen;
2905 long wait_time; /* milli seconds */
2906 int tb_change_cnt;
2907{
2908 int len = 0; /* init for GCC */
2909 int retesc = FALSE; /* return ESC with gotint */
2910 int script_char;
2911
2912 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2913 {
2914 cursor_on();
2915 out_flush();
2916#ifdef FEAT_GUI
2917 if (gui.in_use)
2918 {
2919 gui_update_cursor(FALSE, FALSE);
2920# ifdef FEAT_MOUSESHAPE
2921 if (postponed_mouseshape)
2922 update_mouseshape(-1);
2923# endif
2924 }
2925#endif
2926 }
2927
2928 /*
2929 * Don't reset these when at the hit-return prompt, otherwise a endless
2930 * recursive loop may result (write error in swapfile, hit-return, timeout
2931 * on char wait, flush swapfile, write error....).
2932 */
2933 if (State != HITRETURN)
2934 {
2935 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2936 did_swapwrite_msg = FALSE; /* display swap file write error again */
2937 }
2938 undo_off = FALSE; /* restart undo now */
2939
2940 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002941 * Get a character from a script file if there is one.
2942 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 */
2944 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002945 while (scriptin[curscript] != NULL && script_char < 0
2946#ifdef FEAT_EVAL
2947 && !ignore_script
2948#endif
2949 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002951
2952#if defined(FEAT_NETBEANS_INTG)
2953 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002954 netbeans_parse_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00002955#endif
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2958 {
2959 /* Reached EOF.
2960 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2961 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2962 closescript();
2963 /*
2964 * When reading script file is interrupted, return an ESC to get
2965 * back to normal mode.
2966 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2967 */
2968 if (got_int)
2969 retesc = TRUE;
2970 else
2971 return -1;
2972 }
2973 else
2974 {
2975 buf[0] = script_char;
2976 len = 1;
2977 }
2978 }
2979
2980 if (script_char < 0) /* did not get a character from script */
2981 {
2982 /*
2983 * If we got an interrupt, skip all previously typed characters and
2984 * return TRUE if quit reading script file.
2985 * Stop reading typeahead when a single CTRL-C was read,
2986 * fill_input_buf() returns this when not able to read from stdin.
2987 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2988 * and buf may be pointing inside typebuf.tb_buf[].
2989 */
2990 if (got_int)
2991 {
2992#define DUM_LEN MAXMAPLEN * 3 + 3
2993 char_u dum[DUM_LEN + 1];
2994
2995 for (;;)
2996 {
2997 len = ui_inchar(dum, DUM_LEN, 0L, 0);
2998 if (len == 0 || (len == 1 && dum[0] == 3))
2999 break;
3000 }
3001 return retesc;
3002 }
3003
3004 /*
3005 * Always flush the output characters when getting input characters
3006 * from the user.
3007 */
3008 out_flush();
3009
3010 /*
3011 * Fill up to a third of the buffer, because each character may be
3012 * tripled below.
3013 */
3014 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3015 }
3016
3017 if (typebuf_changed(tb_change_cnt))
3018 return 0;
3019
3020 return fix_input_buffer(buf, len, script_char >= 0);
3021}
3022
3023/*
3024 * Fix typed characters for use by vgetc() and check_termcode().
3025 * buf[] must have room to triple the number of bytes!
3026 * Returns the new length.
3027 */
3028 int
3029fix_input_buffer(buf, len, script)
3030 char_u *buf;
3031 int len;
3032 int script; /* TRUE when reading from a script */
3033{
3034 int i;
3035 char_u *p = buf;
3036
3037 /*
3038 * Two characters are special: NUL and K_SPECIAL.
3039 * When compiled With the GUI CSI is also special.
3040 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3041 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3042 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
3043 * Don't replace K_SPECIAL when reading a script file.
3044 */
3045 for (i = len; --i >= 0; ++p)
3046 {
3047#ifdef FEAT_GUI
3048 /* When the GUI is used any character can come after a CSI, don't
3049 * escape it. */
3050 if (gui.in_use && p[0] == CSI && i >= 2)
3051 {
3052 p += 2;
3053 i -= 2;
3054 }
3055 /* When the GUI is not used CSI needs to be escaped. */
3056 else if (!gui.in_use && p[0] == CSI)
3057 {
3058 mch_memmove(p + 3, p + 1, (size_t)i);
3059 *p++ = K_SPECIAL;
3060 *p++ = KS_EXTRA;
3061 *p = (int)KE_CSI;
3062 len += 2;
3063 }
3064 else
3065#endif
3066 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003067#ifdef FEAT_AUTOCMD
3068 /* timeout may generate K_CURSORHOLD */
3069 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
3070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#if defined(WIN3264) && !defined(FEAT_GUI)
3072 /* Win32 console passes modifiers */
3073 && (i < 2 || p[1] != KS_MODIFIER)
3074#endif
3075 ))
3076 {
3077 mch_memmove(p + 3, p + 1, (size_t)i);
3078 p[2] = K_THIRD(p[0]);
3079 p[1] = K_SECOND(p[0]);
3080 p[0] = K_SPECIAL;
3081 p += 2;
3082 len += 2;
3083 }
3084 }
3085 *p = NUL; /* add trailing NUL */
3086 return len;
3087}
3088
3089#if defined(USE_INPUT_BUF) || defined(PROTO)
3090/*
3091 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3092 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003093 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003094 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 */
3096 int
3097input_available()
3098{
3099 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003100# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3101 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102# endif
3103 );
3104}
3105#endif
3106
3107/*
3108 * map[!] : show all key mappings
3109 * map[!] {lhs} : show key mapping for {lhs}
3110 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3111 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3112 * unmap[!] {lhs} : remove key mapping for {lhs}
3113 * abbr : show all abbreviations
3114 * abbr {lhs} : show abbreviations for {lhs}
3115 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3116 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3117 * unabbr {lhs} : remove abbreviation for {lhs}
3118 *
3119 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3120 *
3121 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3122 * it will be modified.
3123 *
Bram Moolenaar371d5402006-03-20 21:47:49 +00003124 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * for :map! mode is INSERT + CMDLINE
3126 * for :cmap mode is CMDLINE
3127 * for :imap mode is INSERT
3128 * for :lmap mode is LANGMAP
3129 * for :nmap mode is NORMAL
Bram Moolenaar371d5402006-03-20 21:47:49 +00003130 * for :vmap mode is VISUAL + SELECTMODE
3131 * for :xmap mode is VISUAL
3132 * for :smap mode is SELECTMODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 * for :omap mode is OP_PENDING
3134 *
3135 * for :abbr mode is INSERT + CMDLINE
3136 * for :iabbr mode is INSERT
3137 * for :cabbr mode is CMDLINE
3138 *
3139 * Return 0 for success
3140 * 1 for invalid arguments
3141 * 2 for no match
3142 * 4 for out of mem
3143 * 5 for entry not unique
3144 */
3145 int
3146do_map(maptype, arg, mode, abbrev)
3147 int maptype;
3148 char_u *arg;
3149 int mode;
3150 int abbrev; /* not a mapping but an abbreviation */
3151{
3152 char_u *keys;
3153 mapblock_T *mp, **mpp;
3154 char_u *rhs;
3155 char_u *p;
3156 int n;
3157 int len = 0; /* init for GCC */
3158 char_u *newstr;
3159 int hasarg;
3160 int haskey;
3161 int did_it = FALSE;
3162#ifdef FEAT_LOCALMAP
3163 int did_local = FALSE;
3164#endif
3165 int round;
3166 char_u *keys_buf = NULL;
3167 char_u *arg_buf = NULL;
3168 int retval = 0;
3169 int do_backslash;
3170 int hash;
3171 int new_hash;
3172 mapblock_T **abbr_table;
3173 mapblock_T **map_table;
3174 int unique = FALSE;
3175 int silent = FALSE;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003176 int special = FALSE;
Bram Moolenaar4e427192006-03-10 21:34:27 +00003177#ifdef FEAT_EVAL
3178 int expr = FALSE;
3179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 int noremap;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003181 char_u *orig_rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 keys = arg;
3184 map_table = maphash;
3185 abbr_table = &first_abbr;
3186
3187 /* For ":noremap" don't remap, otherwise do remap. */
3188 if (maptype == 2)
3189 noremap = REMAP_NONE;
3190 else
3191 noremap = REMAP_YES;
3192
Bram Moolenaar4e427192006-03-10 21:34:27 +00003193 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 for (;;)
3195 {
3196#ifdef FEAT_LOCALMAP
3197 /*
3198 * Check for "<buffer>": mapping local to buffer.
3199 */
3200 if (STRNCMP(keys, "<buffer>", 8) == 0)
3201 {
3202 keys = skipwhite(keys + 8);
3203 map_table = curbuf->b_maphash;
3204 abbr_table = &curbuf->b_first_abbr;
3205 continue;
3206 }
3207#endif
3208
3209 /*
3210 * Check for "<silent>": don't echo commands.
3211 */
3212 if (STRNCMP(keys, "<silent>", 8) == 0)
3213 {
3214 keys = skipwhite(keys + 8);
3215 silent = TRUE;
3216 continue;
3217 }
3218
Bram Moolenaar9c102382006-05-03 21:26:49 +00003219 /*
3220 * Check for "<special>": accept special keys in <>
3221 */
3222 if (STRNCMP(keys, "<special>", 9) == 0)
3223 {
3224 keys = skipwhite(keys + 9);
3225 special = TRUE;
3226 continue;
3227 }
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229#ifdef FEAT_EVAL
3230 /*
3231 * Check for "<script>": remap script-local mappings only
3232 */
3233 if (STRNCMP(keys, "<script>", 8) == 0)
3234 {
3235 keys = skipwhite(keys + 8);
3236 noremap = REMAP_SCRIPT;
3237 continue;
3238 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00003239
3240 /*
3241 * Check for "<expr>": {rhs} is an expression.
3242 */
3243 if (STRNCMP(keys, "<expr>", 6) == 0)
3244 {
3245 keys = skipwhite(keys + 6);
3246 expr = TRUE;
3247 continue;
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#endif
3250 /*
3251 * Check for "<unique>": don't overwrite an existing mapping.
3252 */
3253 if (STRNCMP(keys, "<unique>", 8) == 0)
3254 {
3255 keys = skipwhite(keys + 8);
3256 unique = TRUE;
3257 continue;
3258 }
3259 break;
3260 }
3261
3262 validate_maphash();
3263
3264 /*
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003265 * Find end of keys and skip CTRL-Vs (and backslashes) in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003267 * with :unmap white space is included in the keys, no argument possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 p = keys;
3270 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3271 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3272 {
3273 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3274 p[1] != NUL)
3275 ++p; /* skip CTRL-V or backslash */
3276 ++p;
3277 }
3278 if (*p != NUL)
3279 *p++ = NUL;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 p = skipwhite(p);
3282 rhs = p;
3283 hasarg = (*rhs != NUL);
3284 haskey = (*keys != NUL);
3285
3286 /* check for :unmap without argument */
3287 if (maptype == 1 && !haskey)
3288 {
3289 retval = 1;
3290 goto theend;
3291 }
3292
3293 /*
3294 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3295 * with the appropriate two bytes. If it is a shifted special key, unshift
3296 * it too, giving another two bytes.
3297 * replace_termcodes() may move the result to allocated memory, which
3298 * needs to be freed later (*keys_buf and *arg_buf).
3299 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3300 */
3301 if (haskey)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003302 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
Bram Moolenaarb3ae56c2010-10-27 17:39:05 +02003303 orig_rhs = rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (hasarg)
3305 {
3306 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3307 rhs = (char_u *)"";
3308 else
Bram Moolenaar9c102382006-05-03 21:26:49 +00003309 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311
3312#ifdef FEAT_FKMAP
3313 /*
Bram Moolenaarbd743252010-10-20 21:23:33 +02003314 * When in right-to-left mode and alternate keymap option set,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 * reverse the character flow in the rhs in Farsi.
3316 */
3317 if (p_altkeymap && curwin->w_p_rl)
3318 lrswap(rhs);
3319#endif
3320
3321 /*
3322 * check arguments and translate function keys
3323 */
3324 if (haskey)
3325 {
3326 len = (int)STRLEN(keys);
3327 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3328 {
3329 retval = 1;
3330 goto theend;
3331 }
3332
3333 if (abbrev && maptype != 1)
3334 {
3335 /*
3336 * If an abbreviation ends in a keyword character, the
3337 * rest must be all keyword-char or all non-keyword-char.
3338 * Otherwise we won't be able to find the start of it in a
3339 * vi-compatible way.
3340 */
3341#ifdef FEAT_MBYTE
3342 if (has_mbyte)
3343 {
3344 int first, last;
3345 int same = -1;
3346
3347 first = vim_iswordp(keys);
3348 last = first;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003349 p = keys + (*mb_ptr2len)(keys);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 n = 1;
3351 while (p < keys + len)
3352 {
3353 ++n; /* nr of (multi-byte) chars */
3354 last = vim_iswordp(p); /* type of last char */
3355 if (same == -1 && last != first)
3356 same = n - 1; /* count of same char type */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003357 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359 if (last && n > 2 && same >= 0 && same < n - 1)
3360 {
3361 retval = 1;
3362 goto theend;
3363 }
3364 }
3365 else
3366#endif
3367 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3368 for (n = 0; n < len - 2; ++n)
3369 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3370 {
3371 retval = 1;
3372 goto theend;
3373 }
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00003374 /* An abbreviation cannot contain white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 for (n = 0; n < len; ++n)
3376 if (vim_iswhite(keys[n]))
3377 {
3378 retval = 1;
3379 goto theend;
3380 }
3381 }
3382 }
3383
3384 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3385 no_abbr = FALSE; /* reset flag that indicates there are
3386 no abbreviations */
3387
3388 if (!haskey || (maptype != 1 && !hasarg))
3389 msg_start();
3390
3391#ifdef FEAT_LOCALMAP
3392 /*
3393 * Check if a new local mapping wasn't already defined globally.
3394 */
3395 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3396 {
3397 /* need to loop over all global hash lists */
3398 for (hash = 0; hash < 256 && !got_int; ++hash)
3399 {
3400 if (abbrev)
3401 {
3402 if (hash != 0) /* there is only one abbreviation list */
3403 break;
3404 mp = first_abbr;
3405 }
3406 else
3407 mp = maphash[hash];
3408 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3409 {
3410 /* check entries with the same mode */
3411 if ((mp->m_mode & mode) != 0
3412 && mp->m_keylen == len
3413 && unique
3414 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3415 {
3416 if (abbrev)
3417 EMSG2(_("E224: global abbreviation already exists for %s"),
3418 mp->m_keys);
3419 else
3420 EMSG2(_("E225: global mapping already exists for %s"),
3421 mp->m_keys);
3422 retval = 5;
3423 goto theend;
3424 }
3425 }
3426 }
3427 }
3428
3429 /*
3430 * When listing global mappings, also list buffer-local ones here.
3431 */
3432 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3433 {
3434 /* need to loop over all global hash lists */
3435 for (hash = 0; hash < 256 && !got_int; ++hash)
3436 {
3437 if (abbrev)
3438 {
3439 if (hash != 0) /* there is only one abbreviation list */
3440 break;
3441 mp = curbuf->b_first_abbr;
3442 }
3443 else
3444 mp = curbuf->b_maphash[hash];
3445 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3446 {
3447 /* check entries with the same mode */
3448 if ((mp->m_mode & mode) != 0)
3449 {
3450 if (!haskey) /* show all entries */
3451 {
3452 showmap(mp, TRUE);
3453 did_local = TRUE;
3454 }
3455 else
3456 {
3457 n = mp->m_keylen;
3458 if (STRNCMP(mp->m_keys, keys,
3459 (size_t)(n < len ? n : len)) == 0)
3460 {
3461 showmap(mp, TRUE);
3462 did_local = TRUE;
3463 }
3464 }
3465 }
3466 }
3467 }
3468 }
3469#endif
3470
3471 /*
3472 * Find an entry in the maphash[] list that matches.
3473 * For :unmap we may loop two times: once to try to unmap an entry with a
3474 * matching 'from' part, a second time, if the first fails, to unmap an
3475 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3476 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3477 * "bar" because of the abbreviation.
3478 */
3479 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3480 && !did_it && !got_int; ++round)
3481 {
3482 /* need to loop over all hash lists */
3483 for (hash = 0; hash < 256 && !got_int; ++hash)
3484 {
3485 if (abbrev)
3486 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003487 if (hash > 0) /* there is only one abbreviation list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 break;
3489 mpp = abbr_table;
3490 }
3491 else
3492 mpp = &(map_table[hash]);
3493 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3494 {
3495
3496 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3497 {
3498 mpp = &(mp->m_next);
3499 continue;
3500 }
3501 if (!haskey) /* show all entries */
3502 {
3503 showmap(mp, map_table != maphash);
3504 did_it = TRUE;
3505 }
3506 else /* do we have a match? */
3507 {
3508 if (round) /* second round: Try unmap "rhs" string */
3509 {
3510 n = (int)STRLEN(mp->m_str);
3511 p = mp->m_str;
3512 }
3513 else
3514 {
3515 n = mp->m_keylen;
3516 p = mp->m_keys;
3517 }
3518 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3519 {
3520 if (maptype == 1) /* delete entry */
3521 {
3522 /* Only accept a full match. For abbreviations we
3523 * ignore trailing space when matching with the
3524 * "lhs", since an abbreviation can't have
3525 * trailing space. */
3526 if (n != len && (!abbrev || round || n > len
3527 || *skipwhite(keys + n) != NUL))
3528 {
3529 mpp = &(mp->m_next);
3530 continue;
3531 }
3532 /*
3533 * We reset the indicated mode bits. If nothing is
3534 * left the entry is deleted below.
3535 */
3536 mp->m_mode &= ~mode;
3537 did_it = TRUE; /* remember we did something */
3538 }
3539 else if (!hasarg) /* show matching entry */
3540 {
3541 showmap(mp, map_table != maphash);
3542 did_it = TRUE;
3543 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00003544 else if (n != len) /* new entry is ambiguous */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 mpp = &(mp->m_next);
3547 continue;
3548 }
3549 else if (unique)
3550 {
3551 if (abbrev)
3552 EMSG2(_("E226: abbreviation already exists for %s"),
3553 p);
3554 else
3555 EMSG2(_("E227: mapping already exists for %s"), p);
3556 retval = 5;
3557 goto theend;
3558 }
3559 else /* new rhs for existing entry */
3560 {
3561 mp->m_mode &= ~mode; /* remove mode bits */
3562 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3563 {
3564 newstr = vim_strsave(rhs);
3565 if (newstr == NULL)
3566 {
3567 retval = 4; /* no mem */
3568 goto theend;
3569 }
3570 vim_free(mp->m_str);
3571 mp->m_str = newstr;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003572 vim_free(mp->m_orig_str);
3573 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 mp->m_noremap = noremap;
3575 mp->m_silent = silent;
3576 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003577#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003578 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003579 mp->m_script_ID = current_SID;
3580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 did_it = TRUE;
3582 }
3583 }
3584 if (mp->m_mode == 0) /* entry can be deleted */
3585 {
3586 map_free(mpp);
3587 continue; /* continue with *mpp */
3588 }
3589
3590 /*
3591 * May need to put this entry into another hash list.
3592 */
3593 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3594 if (!abbrev && new_hash != hash)
3595 {
3596 *mpp = mp->m_next;
3597 mp->m_next = map_table[new_hash];
3598 map_table[new_hash] = mp;
3599
3600 continue; /* continue with *mpp */
3601 }
3602 }
3603 }
3604 mpp = &(mp->m_next);
3605 }
3606 }
3607 }
3608
3609 if (maptype == 1) /* delete entry */
3610 {
3611 if (!did_it)
3612 retval = 2; /* no match */
3613 goto theend;
3614 }
3615
3616 if (!haskey || !hasarg) /* print entries */
3617 {
3618 if (!did_it
3619#ifdef FEAT_LOCALMAP
3620 && !did_local
3621#endif
3622 )
3623 {
3624 if (abbrev)
3625 MSG(_("No abbreviation found"));
3626 else
3627 MSG(_("No mapping found"));
3628 }
3629 goto theend; /* listing finished */
3630 }
3631
3632 if (did_it) /* have added the new entry already */
3633 goto theend;
3634
3635 /*
3636 * Get here when adding a new entry to the maphash[] list or abbrlist.
3637 */
3638 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3639 if (mp == NULL)
3640 {
3641 retval = 4; /* no mem */
3642 goto theend;
3643 }
3644
3645 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3646 if (*keys == Ctrl_C)
3647 mapped_ctrl_c = TRUE;
3648
3649 mp->m_keys = vim_strsave(keys);
3650 mp->m_str = vim_strsave(rhs);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003651 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (mp->m_keys == NULL || mp->m_str == NULL)
3653 {
3654 vim_free(mp->m_keys);
3655 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003656 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 vim_free(mp);
3658 retval = 4; /* no mem */
3659 goto theend;
3660 }
3661 mp->m_keylen = (int)STRLEN(mp->m_keys);
3662 mp->m_noremap = noremap;
3663 mp->m_silent = silent;
3664 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003665#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003666 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003667 mp->m_script_ID = current_SID;
3668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 /* add the new entry in front of the abbrlist or maphash[] list */
3671 if (abbrev)
3672 {
3673 mp->m_next = *abbr_table;
3674 *abbr_table = mp;
3675 }
3676 else
3677 {
3678 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3679 mp->m_next = map_table[n];
3680 map_table[n] = mp;
3681 }
3682
3683theend:
3684 vim_free(keys_buf);
3685 vim_free(arg_buf);
3686 return retval;
3687}
3688
3689/*
3690 * Delete one entry from the abbrlist or maphash[].
3691 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3692 */
3693 static void
3694map_free(mpp)
3695 mapblock_T **mpp;
3696{
3697 mapblock_T *mp;
3698
3699 mp = *mpp;
3700 vim_free(mp->m_keys);
3701 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003702 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 *mpp = mp->m_next;
3704 vim_free(mp);
3705}
3706
3707/*
3708 * Initialize maphash[] for first use.
3709 */
3710 static void
3711validate_maphash()
3712{
3713 if (!maphash_valid)
3714 {
3715 vim_memset(maphash, 0, sizeof(maphash));
3716 maphash_valid = TRUE;
3717 }
3718}
3719
3720/*
3721 * Get the mapping mode from the command name.
3722 */
3723 int
3724get_map_mode(cmdp, forceit)
3725 char_u **cmdp;
3726 int forceit;
3727{
3728 char_u *p;
3729 int modec;
3730 int mode;
3731
3732 p = *cmdp;
3733 modec = *p++;
3734 if (modec == 'i')
3735 mode = INSERT; /* :imap */
3736 else if (modec == 'l')
3737 mode = LANGMAP; /* :lmap */
3738 else if (modec == 'c')
3739 mode = CMDLINE; /* :cmap */
3740 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3741 mode = NORMAL; /* :nmap */
3742 else if (modec == 'v')
Bram Moolenaar371d5402006-03-20 21:47:49 +00003743 mode = VISUAL + SELECTMODE; /* :vmap */
3744 else if (modec == 'x')
3745 mode = VISUAL; /* :xmap */
3746 else if (modec == 's')
3747 mode = SELECTMODE; /* :smap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else if (modec == 'o')
3749 mode = OP_PENDING; /* :omap */
3750 else
3751 {
3752 --p;
3753 if (forceit)
3754 mode = INSERT + CMDLINE; /* :map ! */
3755 else
Bram Moolenaar371d5402006-03-20 21:47:49 +00003756 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758
3759 *cmdp = p;
3760 return mode;
3761}
3762
3763/*
3764 * Clear all mappings or abbreviations.
3765 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 void
3768map_clear(cmdp, arg, forceit, abbr)
3769 char_u *cmdp;
Bram Moolenaard31aca22009-07-14 11:44:30 +00003770 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 int forceit;
3772 int abbr;
3773{
3774 int mode;
3775#ifdef FEAT_LOCALMAP
3776 int local;
3777
3778 local = (STRCMP(arg, "<buffer>") == 0);
3779 if (!local && *arg != NUL)
3780 {
3781 EMSG(_(e_invarg));
3782 return;
3783 }
3784#endif
3785
3786 mode = get_map_mode(&cmdp, forceit);
3787 map_clear_int(curbuf, mode,
3788#ifdef FEAT_LOCALMAP
3789 local,
3790#else
3791 FALSE,
3792#endif
3793 abbr);
3794}
3795
3796/*
3797 * Clear all mappings in "mode".
3798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 void
3800map_clear_int(buf, mode, local, abbr)
Bram Moolenaard31aca22009-07-14 11:44:30 +00003801 buf_T *buf UNUSED; /* buffer for local mappings */
3802 int mode; /* mode in which to delete */
3803 int local UNUSED; /* TRUE for buffer-local mappings */
3804 int abbr; /* TRUE for abbreviations */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 mapblock_T *mp, **mpp;
3807 int hash;
3808 int new_hash;
3809
3810 validate_maphash();
3811
3812 for (hash = 0; hash < 256; ++hash)
3813 {
3814 if (abbr)
3815 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003816 if (hash > 0) /* there is only one abbrlist */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 break;
3818#ifdef FEAT_LOCALMAP
3819 if (local)
3820 mpp = &buf->b_first_abbr;
3821 else
3822#endif
3823 mpp = &first_abbr;
3824 }
3825 else
3826 {
3827#ifdef FEAT_LOCALMAP
3828 if (local)
3829 mpp = &buf->b_maphash[hash];
3830 else
3831#endif
3832 mpp = &maphash[hash];
3833 }
3834 while (*mpp != NULL)
3835 {
3836 mp = *mpp;
3837 if (mp->m_mode & mode)
3838 {
3839 mp->m_mode &= ~mode;
3840 if (mp->m_mode == 0) /* entry can be deleted */
3841 {
3842 map_free(mpp);
3843 continue;
3844 }
3845 /*
3846 * May need to put this entry into another hash list.
3847 */
3848 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3849 if (!abbr && new_hash != hash)
3850 {
3851 *mpp = mp->m_next;
3852#ifdef FEAT_LOCALMAP
3853 if (local)
3854 {
3855 mp->m_next = buf->b_maphash[new_hash];
3856 buf->b_maphash[new_hash] = mp;
3857 }
3858 else
3859#endif
3860 {
3861 mp->m_next = maphash[new_hash];
3862 maphash[new_hash] = mp;
3863 }
3864 continue; /* continue with *mpp */
3865 }
3866 }
3867 mpp = &(mp->m_next);
3868 }
3869 }
3870}
3871
Bram Moolenaarbd743252010-10-20 21:23:33 +02003872/*
3873 * Return characters to represent the map mode in an allocated string.
3874 * Returns NULL when out of memory.
3875 */
3876 char_u *
3877map_mode_to_chars(mode)
3878 int mode;
3879{
3880 garray_T mapmode;
3881
3882 ga_init2(&mapmode, 1, 7);
3883
3884 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3885 ga_append(&mapmode, '!'); /* :map! */
3886 else if (mode & INSERT)
3887 ga_append(&mapmode, 'i'); /* :imap */
3888 else if (mode & LANGMAP)
3889 ga_append(&mapmode, 'l'); /* :lmap */
3890 else if (mode & CMDLINE)
3891 ga_append(&mapmode, 'c'); /* :cmap */
3892 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3893 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3894 ga_append(&mapmode, ' '); /* :map */
3895 else
3896 {
3897 if (mode & NORMAL)
3898 ga_append(&mapmode, 'n'); /* :nmap */
3899 if (mode & OP_PENDING)
3900 ga_append(&mapmode, 'o'); /* :omap */
3901 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3902 ga_append(&mapmode, 'v'); /* :vmap */
3903 else
3904 {
3905 if (mode & VISUAL)
3906 ga_append(&mapmode, 'x'); /* :xmap */
3907 if (mode & SELECTMODE)
3908 ga_append(&mapmode, 's'); /* :smap */
3909 }
3910 }
3911
3912 ga_append(&mapmode, NUL);
3913 return (char_u *)mapmode.ga_data;
3914}
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 static void
3917showmap(mp, local)
3918 mapblock_T *mp;
3919 int local; /* TRUE for buffer-local map */
3920{
Bram Moolenaarbd743252010-10-20 21:23:33 +02003921 int len = 1;
3922 char_u *mapchars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924 if (msg_didout || msg_silent != 0)
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 msg_putchar('\n');
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003927 if (got_int) /* 'q' typed at MORE prompt */
3928 return;
3929 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003930
3931 mapchars = map_mode_to_chars(mp->m_mode);
3932 if (mapchars != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
Bram Moolenaarbd743252010-10-20 21:23:33 +02003934 msg_puts(mapchars);
Bram Moolenaar6b5ef062010-10-27 12:18:00 +02003935 len = (int)STRLEN(mapchars);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003936 vim_free(mapchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 while (++len <= 3)
3940 msg_putchar(' ');
3941
Bram Moolenaarf2330482008-06-24 20:19:36 +00003942 /* Display the LHS. Get length of what we write. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 len = msg_outtrans_special(mp->m_keys, TRUE);
3944 do
3945 {
3946 msg_putchar(' '); /* padd with blanks */
3947 ++len;
3948 } while (len < 12);
3949
3950 if (mp->m_noremap == REMAP_NONE)
3951 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3952 else if (mp->m_noremap == REMAP_SCRIPT)
3953 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3954 else
3955 msg_putchar(' ');
3956
3957 if (local)
3958 msg_putchar('@');
3959 else
3960 msg_putchar(' ');
3961
3962 /* Use FALSE below if we only want things like <Up> to show up as such on
Bram Moolenaarbd743252010-10-20 21:23:33 +02003963 * the rhs, and not M-x etc, TRUE gets both -- webb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if (*mp->m_str == NUL)
3965 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3966 else
3967 msg_outtrans_special(mp->m_str, FALSE);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003968#ifdef FEAT_EVAL
3969 if (p_verbose > 0)
3970 last_set_msg(mp->m_script_ID);
3971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 out_flush(); /* show one line at a time */
3973}
3974
3975#if defined(FEAT_EVAL) || defined(PROTO)
3976/*
3977 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3978 * Recognize termcap codes in "str".
3979 * Also checks mappings local to the current buffer.
3980 */
3981 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003982map_to_exists(str, modechars, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 char_u *str;
3984 char_u *modechars;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003985 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 int mode = 0;
3988 char_u *rhs;
3989 char_u *buf;
3990 int retval;
3991
Bram Moolenaar9c102382006-05-03 21:26:49 +00003992 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 if (vim_strchr(modechars, 'n') != NULL)
3995 mode |= NORMAL;
3996 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003997 mode |= VISUAL + SELECTMODE;
3998 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 mode |= VISUAL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004000 if (vim_strchr(modechars, 's') != NULL)
4001 mode |= SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (vim_strchr(modechars, 'o') != NULL)
4003 mode |= OP_PENDING;
4004 if (vim_strchr(modechars, 'i') != NULL)
4005 mode |= INSERT;
4006 if (vim_strchr(modechars, 'l') != NULL)
4007 mode |= LANGMAP;
4008 if (vim_strchr(modechars, 'c') != NULL)
4009 mode |= CMDLINE;
4010
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004011 retval = map_to_exists_mode(rhs, mode, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 vim_free(buf);
4013
4014 return retval;
4015}
4016#endif
4017
4018/*
4019 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
4020 * Also checks mappings local to the current buffer.
4021 */
4022 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004023map_to_exists_mode(rhs, mode, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 char_u *rhs;
4025 int mode;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004026 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 mapblock_T *mp;
4029 int hash;
4030# ifdef FEAT_LOCALMAP
4031 int expand_buffer = FALSE;
4032
4033 validate_maphash();
4034
4035 /* Do it twice: once for global maps and once for local maps. */
4036 for (;;)
4037 {
4038# endif
4039 for (hash = 0; hash < 256; ++hash)
4040 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004041 if (abbr)
4042 {
4043 if (hash > 0) /* there is only one abbr list */
4044 break;
4045#ifdef FEAT_LOCALMAP
4046 if (expand_buffer)
4047 mp = curbuf->b_first_abbr;
4048 else
4049#endif
4050 mp = first_abbr;
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052# ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004053 else if (expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 mp = curbuf->b_maphash[hash];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055# endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 mp = maphash[hash];
4058 for (; mp; mp = mp->m_next)
4059 {
4060 if ((mp->m_mode & mode)
4061 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
4062 return TRUE;
4063 }
4064 }
4065# ifdef FEAT_LOCALMAP
4066 if (expand_buffer)
4067 break;
4068 expand_buffer = TRUE;
4069 }
4070# endif
4071
4072 return FALSE;
4073}
4074
4075#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4076/*
4077 * Used below when expanding mapping/abbreviation names.
4078 */
4079static int expand_mapmodes = 0;
4080static int expand_isabbrev = 0;
4081#ifdef FEAT_LOCALMAP
4082static int expand_buffer = FALSE;
4083#endif
4084
4085/*
4086 * Work out what to complete when doing command line completion of mapping
4087 * or abbreviation names.
4088 */
4089 char_u *
4090set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
4091 expand_T *xp;
4092 char_u *cmd;
4093 char_u *arg;
4094 int forceit; /* TRUE if '!' given */
4095 int isabbrev; /* TRUE if abbreviation */
4096 int isunmap; /* TRUE if unmap/unabbrev command */
4097 cmdidx_T cmdidx;
4098{
4099 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
4100 xp->xp_context = EXPAND_NOTHING;
4101 else
4102 {
4103 if (isunmap)
4104 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
4105 else
4106 {
4107 expand_mapmodes = INSERT + CMDLINE;
4108 if (!isabbrev)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004109 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 expand_isabbrev = isabbrev;
4112 xp->xp_context = EXPAND_MAPPINGS;
4113#ifdef FEAT_LOCALMAP
4114 expand_buffer = FALSE;
4115#endif
4116 for (;;)
4117 {
4118#ifdef FEAT_LOCALMAP
4119 if (STRNCMP(arg, "<buffer>", 8) == 0)
4120 {
4121 expand_buffer = TRUE;
4122 arg = skipwhite(arg + 8);
4123 continue;
4124 }
4125#endif
4126 if (STRNCMP(arg, "<unique>", 8) == 0)
4127 {
4128 arg = skipwhite(arg + 8);
4129 continue;
4130 }
4131 if (STRNCMP(arg, "<silent>", 8) == 0)
4132 {
4133 arg = skipwhite(arg + 8);
4134 continue;
4135 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004136#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (STRNCMP(arg, "<script>", 8) == 0)
4138 {
4139 arg = skipwhite(arg + 8);
4140 continue;
4141 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004142 if (STRNCMP(arg, "<expr>", 6) == 0)
4143 {
4144 arg = skipwhite(arg + 6);
4145 continue;
4146 }
4147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 break;
4149 }
4150 xp->xp_pattern = arg;
4151 }
4152
4153 return NULL;
4154}
4155
4156/*
4157 * Find all mapping/abbreviation names that match regexp 'prog'.
4158 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4159 * Return OK if matches found, FAIL otherwise.
4160 */
4161 int
4162ExpandMappings(regmatch, num_file, file)
4163 regmatch_T *regmatch;
4164 int *num_file;
4165 char_u ***file;
4166{
4167 mapblock_T *mp;
4168 int hash;
4169 int count;
4170 int round;
4171 char_u *p;
4172 int i;
4173
4174 validate_maphash();
4175
4176 *num_file = 0; /* return values in case of FAIL */
4177 *file = NULL;
4178
4179 /*
4180 * round == 1: Count the matches.
4181 * round == 2: Build the array to keep the matches.
4182 */
4183 for (round = 1; round <= 2; ++round)
4184 {
4185 count = 0;
4186
Bram Moolenaar4e427192006-03-10 21:34:27 +00004187 for (i = 0; i < 5; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 if (i == 0)
4190 p = (char_u *)"<silent>";
4191 else if (i == 1)
4192 p = (char_u *)"<unique>";
4193#ifdef FEAT_EVAL
4194 else if (i == 2)
4195 p = (char_u *)"<script>";
Bram Moolenaar4e427192006-03-10 21:34:27 +00004196 else if (i == 3)
4197 p = (char_u *)"<expr>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198#endif
4199#ifdef FEAT_LOCALMAP
Bram Moolenaar4e427192006-03-10 21:34:27 +00004200 else if (i == 4 && !expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 p = (char_u *)"<buffer>";
4202#endif
4203 else
4204 continue;
4205
4206 if (vim_regexec(regmatch, p, (colnr_T)0))
4207 {
4208 if (round == 1)
4209 ++count;
4210 else
4211 (*file)[count++] = vim_strsave(p);
4212 }
4213 }
4214
4215 for (hash = 0; hash < 256; ++hash)
4216 {
4217 if (expand_isabbrev)
4218 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004219 if (hash > 0) /* only one abbrev list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 break; /* for (hash) */
4221 mp = first_abbr;
4222 }
4223#ifdef FEAT_LOCALMAP
4224 else if (expand_buffer)
4225 mp = curbuf->b_maphash[hash];
4226#endif
4227 else
4228 mp = maphash[hash];
4229 for (; mp; mp = mp->m_next)
4230 {
4231 if (mp->m_mode & expand_mapmodes)
4232 {
4233 p = translate_mapping(mp->m_keys, TRUE);
4234 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4235 {
4236 if (round == 1)
4237 ++count;
4238 else
4239 {
4240 (*file)[count++] = p;
4241 p = NULL;
4242 }
4243 }
4244 vim_free(p);
4245 }
4246 } /* for (mp) */
4247 } /* for (hash) */
4248
4249 if (count == 0) /* no match found */
4250 break; /* for (round) */
4251
4252 if (round == 1)
4253 {
4254 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4255 if (*file == NULL)
4256 return FAIL;
4257 }
4258 } /* for (round) */
4259
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004260 if (count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004262 char_u **ptr1;
4263 char_u **ptr2;
4264 char_u **ptr3;
4265
4266 /* Sort the matches */
4267 sort_strings(*file, count);
4268
4269 /* Remove multiple entries */
4270 ptr1 = *file;
4271 ptr2 = ptr1 + 1;
4272 ptr3 = ptr1 + count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 while (ptr2 < ptr3)
4275 {
4276 if (STRCMP(*ptr1, *ptr2))
4277 *++ptr1 = *ptr2++;
4278 else
4279 {
4280 vim_free(*ptr2++);
4281 count--;
4282 }
4283 }
4284 }
4285
4286 *num_file = count;
4287 return (count == 0 ? FAIL : OK);
4288}
4289#endif /* FEAT_CMDL_COMPL */
4290
4291/*
4292 * Check for an abbreviation.
4293 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4294 * "c" is the character typed before check_abbr was called. It may have
4295 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4296 *
4297 * Historic vi practice: The last character of an abbreviation must be an id
4298 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4299 * characters or all non-id characters. This allows for abbr. "#i" to
4300 * "#include".
4301 *
4302 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4303 * Then there must be white space before the abbr.
4304 *
4305 * return TRUE if there is an abbreviation, FALSE if not
4306 */
4307 int
4308check_abbr(c, ptr, col, mincol)
4309 int c;
4310 char_u *ptr;
4311 int col;
4312 int mincol;
4313{
4314 int len;
4315 int scol; /* starting column of the abbr. */
4316 int j;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004317 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#ifdef FEAT_MBYTE
4319 char_u tb[MB_MAXBYTES + 4];
4320#else
4321 char_u tb[4];
4322#endif
4323 mapblock_T *mp;
4324#ifdef FEAT_LOCALMAP
4325 mapblock_T *mp2;
4326#endif
4327#ifdef FEAT_MBYTE
4328 int clen = 0; /* length in characters */
4329#endif
4330 int is_id = TRUE;
4331 int vim_abbr;
4332
4333 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4334 return FALSE;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00004335 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0)
4336 /* no remapping implies no abbreviation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return FALSE;
4338
4339 /*
4340 * Check for word before the cursor: If it ends in a keyword char all
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00004341 * chars before it must be keyword chars or non-keyword chars, but not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 * white space. If it ends in a non-keyword char we accept any characters
4343 * before it except white space.
4344 */
4345 if (col == 0) /* cannot be an abbr. */
4346 return FALSE;
4347
4348#ifdef FEAT_MBYTE
4349 if (has_mbyte)
4350 {
4351 char_u *p;
4352
4353 p = mb_prevptr(ptr, ptr + col);
4354 if (!vim_iswordp(p))
4355 vim_abbr = TRUE; /* Vim added abbr. */
4356 else
4357 {
4358 vim_abbr = FALSE; /* vi compatible abbr. */
4359 if (p > ptr)
4360 is_id = vim_iswordp(mb_prevptr(ptr, p));
4361 }
4362 clen = 1;
4363 while (p > ptr + mincol)
4364 {
4365 p = mb_prevptr(ptr, p);
4366 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004368 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 break;
4370 }
4371 ++clen;
4372 }
4373 scol = (int)(p - ptr);
4374 }
4375 else
4376#endif
4377 {
4378 if (!vim_iswordc(ptr[col - 1]))
4379 vim_abbr = TRUE; /* Vim added abbr. */
4380 else
4381 {
4382 vim_abbr = FALSE; /* vi compatible abbr. */
4383 if (col > 1)
4384 is_id = vim_iswordc(ptr[col - 2]);
4385 }
4386 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4387 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4388 ;
4389 }
4390
4391 if (scol < mincol)
4392 scol = mincol;
4393 if (scol < col) /* there is a word in front of the cursor */
4394 {
4395 ptr += scol;
4396 len = col - scol;
4397#ifdef FEAT_LOCALMAP
4398 mp = curbuf->b_first_abbr;
4399 mp2 = first_abbr;
4400 if (mp == NULL)
4401 {
4402 mp = mp2;
4403 mp2 = NULL;
4404 }
4405#else
4406 mp = first_abbr;
4407#endif
4408 for ( ; mp;
4409#ifdef FEAT_LOCALMAP
4410 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4411#endif
4412 (mp = mp->m_next))
4413 {
4414 /* find entries with right mode and keys */
4415 if ( (mp->m_mode & State)
4416 && mp->m_keylen == len
4417 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4418 break;
4419 }
4420 if (mp != NULL)
4421 {
4422 /*
4423 * Found a match:
4424 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4425 * This goes from end to start.
4426 *
4427 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4428 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4429 * Characters where IS_SPECIAL() == TRUE: key codes, need
4430 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4431 *
4432 * Character CTRL-] is treated specially - it completes the
4433 * abbreviation, but is not inserted into the input stream.
4434 */
4435 j = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 if (c != Ctrl_RSB)
4437 {
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004438 /* special key code, split up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (IS_SPECIAL(c) || c == K_SPECIAL)
4440 {
4441 tb[j++] = K_SPECIAL;
4442 tb[j++] = K_SECOND(c);
4443 tb[j++] = K_THIRD(c);
4444 }
4445 else
4446 {
4447 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4448 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4449#ifdef FEAT_MBYTE
4450 if (has_mbyte)
4451 {
4452 /* if ABBR_OFF has been added, remove it here */
4453 if (c >= ABBR_OFF)
4454 c -= ABBR_OFF;
4455 j += (*mb_char2bytes)(c, tb + j);
4456 }
4457 else
4458#endif
4459 tb[j++] = c;
4460 }
4461 tb[j] = NUL;
4462 /* insert the last typed char */
4463 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4464 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004465#ifdef FEAT_EVAL
4466 if (mp->m_expr)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004467 s = eval_map_expr(mp->m_str, c);
Bram Moolenaar4e427192006-03-10 21:34:27 +00004468 else
4469#endif
4470 s = mp->m_str;
4471 if (s != NULL)
4472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 /* insert the to string */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004474 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 /* no abbrev. for these chars */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004476 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4477#ifdef FEAT_EVAL
4478 if (mp->m_expr)
4479 vim_free(s);
4480#endif
4481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
4483 tb[0] = Ctrl_H;
4484 tb[1] = NUL;
4485#ifdef FEAT_MBYTE
4486 if (has_mbyte)
4487 len = clen; /* Delete characters instead of bytes */
4488#endif
4489 while (len-- > 0) /* delete the from string */
4490 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4491 return TRUE;
4492 }
4493 }
4494 return FALSE;
4495}
4496
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004497#ifdef FEAT_EVAL
4498/*
4499 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4500 * special characters.
4501 */
4502 static char_u *
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004503eval_map_expr(str, c)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004504 char_u *str;
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004505 int c; /* NUL or typed character for abbreviation */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004506{
4507 char_u *res;
Bram Moolenaar8424a622006-04-19 21:23:36 +00004508 char_u *p;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004509 char_u *expr;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004510 char_u *save_cmd;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004511 pos_T save_cursor;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004512
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004513 /* Remove escaping of CSI, because "str" is in a format to be used as
4514 * typeahead. */
4515 expr = vim_strsave(str);
4516 if (expr == NULL)
4517 return NULL;
4518 vim_unescape_csi(expr);
4519
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004520 save_cmd = save_cmdline_alloc();
4521 if (save_cmd == NULL)
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004522 {
4523 vim_free(expr);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004524 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004525 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004526
4527 /* Forbid changing text or using ":normal" to avoid most of the bad side
4528 * effects. Also restore the cursor position. */
4529 ++textlock;
4530#ifdef FEAT_EX_EXTRA
4531 ++ex_normal_lock;
4532#endif
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004533 set_vim_var_char(c); /* set v:char to the typed character */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004534 save_cursor = curwin->w_cursor;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004535 p = eval_to_string(expr, NULL, FALSE);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004536 --textlock;
4537#ifdef FEAT_EX_EXTRA
4538 --ex_normal_lock;
4539#endif
4540 curwin->w_cursor = save_cursor;
4541
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004542 restore_cmdline_alloc(save_cmd);
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004543 vim_free(expr);
4544
Bram Moolenaar8424a622006-04-19 21:23:36 +00004545 if (p == NULL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004546 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004547 /* Escape CSI in the result to be able to use the string as typeahead. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004548 res = vim_strsave_escape_csi(p);
4549 vim_free(p);
4550
4551 return res;
4552}
4553#endif
4554
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004555/*
4556 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4557 * can be put in the typeahead buffer.
4558 * Returns NULL when out of memory.
4559 */
4560 char_u *
4561vim_strsave_escape_csi(p)
4562 char_u *p;
4563{
4564 char_u *res;
4565 char_u *s, *d;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004566
4567 /* Need a buffer to hold up to three times as much. */
Bram Moolenaar8424a622006-04-19 21:23:36 +00004568 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004569 if (res != NULL)
4570 {
Bram Moolenaar8424a622006-04-19 21:23:36 +00004571 d = res;
4572 for (s = p; *s != NUL; )
4573 {
4574 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4575 {
4576 /* Copy special key unmodified. */
4577 *d++ = *s++;
4578 *d++ = *s++;
4579 *d++ = *s++;
4580 }
4581 else
4582 {
4583 /* Add character, possibly multi-byte to destination, escaping
4584 * CSI and K_SPECIAL. */
4585 d = add_char2buf(PTR2CHAR(s), d);
4586 mb_ptr_adv(s);
4587 }
4588 }
4589 *d = NUL;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004590 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004591 return res;
4592}
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004593
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594/*
Bram Moolenaar81b85872007-03-04 20:22:01 +00004595 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4596 * vim_strsave_escape_csi(). Works in-place.
4597 */
4598 void
4599vim_unescape_csi(p)
4600 char_u *p;
4601{
4602 char_u *s = p, *d = p;
4603
4604 while (*s != NUL)
4605 {
4606 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4607 {
4608 *d++ = K_SPECIAL;
4609 s += 3;
4610 }
4611 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4612 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4613 {
4614 *d++ = CSI;
4615 s += 3;
4616 }
4617 else
4618 *d++ = *s++;
4619 }
4620 *d = NUL;
4621}
4622
4623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 * Write map commands for the current mappings to an .exrc file.
4625 * Return FAIL on error, OK otherwise.
4626 */
4627 int
4628makemap(fd, buf)
4629 FILE *fd;
4630 buf_T *buf; /* buffer for local mappings or NULL */
4631{
4632 mapblock_T *mp;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004633 char_u c1, c2, c3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 char_u *p;
4635 char *cmd;
4636 int abbr;
4637 int hash;
4638 int did_cpo = FALSE;
4639 int i;
4640
4641 validate_maphash();
4642
4643 /*
4644 * Do the loop twice: Once for mappings, once for abbreviations.
4645 * Then loop over all map hash lists.
4646 */
4647 for (abbr = 0; abbr < 2; ++abbr)
4648 for (hash = 0; hash < 256; ++hash)
4649 {
4650 if (abbr)
4651 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004652 if (hash > 0) /* there is only one abbr list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 break;
4654#ifdef FEAT_LOCALMAP
4655 if (buf != NULL)
4656 mp = buf->b_first_abbr;
4657 else
4658#endif
4659 mp = first_abbr;
4660 }
4661 else
4662 {
4663#ifdef FEAT_LOCALMAP
4664 if (buf != NULL)
4665 mp = buf->b_maphash[hash];
4666 else
4667#endif
4668 mp = maphash[hash];
4669 }
4670
4671 for ( ; mp; mp = mp->m_next)
4672 {
4673 /* skip script-local mappings */
4674 if (mp->m_noremap == REMAP_SCRIPT)
4675 continue;
4676
4677 /* skip mappings that contain a <SNR> (script-local thing),
4678 * they probably don't work when loaded again */
4679 for (p = mp->m_str; *p != NUL; ++p)
4680 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4681 && p[2] == (int)KE_SNR)
4682 break;
4683 if (*p != NUL)
4684 continue;
4685
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004686 /* It's possible to create a mapping and then ":unmap" certain
4687 * modes. We recreate this here by mapping the individual
4688 * modes, which requires up to three of them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 c1 = NUL;
4690 c2 = NUL;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004691 c3 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 if (abbr)
4693 cmd = "abbr";
4694 else
4695 cmd = "map";
4696 switch (mp->m_mode)
4697 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00004698 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 break;
4700 case NORMAL:
4701 c1 = 'n';
4702 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004703 case VISUAL:
4704 c1 = 'x';
4705 break;
4706 case SELECTMODE:
4707 c1 = 's';
4708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 case OP_PENDING:
4710 c1 = 'o';
4711 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004712 case NORMAL + VISUAL:
4713 c1 = 'n';
4714 c2 = 'x';
4715 break;
4716 case NORMAL + SELECTMODE:
4717 c1 = 'n';
4718 c2 = 's';
4719 break;
4720 case NORMAL + OP_PENDING:
4721 c1 = 'n';
4722 c2 = 'o';
4723 break;
4724 case VISUAL + SELECTMODE:
4725 c1 = 'v';
4726 break;
4727 case VISUAL + OP_PENDING:
4728 c1 = 'x';
4729 c2 = 'o';
4730 break;
4731 case SELECTMODE + OP_PENDING:
4732 c1 = 's';
4733 c2 = 'o';
4734 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004735 case NORMAL + VISUAL + SELECTMODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 c1 = 'n';
4737 c2 = 'v';
4738 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004739 case NORMAL + VISUAL + OP_PENDING:
4740 c1 = 'n';
4741 c2 = 'x';
4742 c3 = 'o';
4743 break;
4744 case NORMAL + SELECTMODE + OP_PENDING:
4745 c1 = 'n';
4746 c2 = 's';
4747 c3 = 'o';
4748 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004749 case VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 c1 = 'v';
4751 c2 = 'o';
4752 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 case CMDLINE + INSERT:
4754 if (!abbr)
4755 cmd = "map!";
4756 break;
4757 case CMDLINE:
4758 c1 = 'c';
4759 break;
4760 case INSERT:
4761 c1 = 'i';
4762 break;
4763 case LANGMAP:
4764 c1 = 'l';
4765 break;
4766 default:
4767 EMSG(_("E228: makemap: Illegal mode"));
4768 return FAIL;
4769 }
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004770 do /* do this twice if c2 is set, 3 times with c3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 /* When outputting <> form, need to make sure that 'cpo'
4773 * is set to the Vim default. */
4774 if (!did_cpo)
4775 {
4776 if (*mp->m_str == NUL) /* will use <Nop> */
4777 did_cpo = TRUE;
4778 else
4779 for (i = 0; i < 2; ++i)
4780 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4781 if (*p == K_SPECIAL || *p == NL)
4782 did_cpo = TRUE;
4783 if (did_cpo)
4784 {
4785 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4786 || put_eol(fd) < 0
4787 || fprintf(fd, "set cpo&vim") < 0
4788 || put_eol(fd) < 0)
4789 return FAIL;
4790 }
4791 }
4792 if (c1 && putc(c1, fd) < 0)
4793 return FAIL;
4794 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4795 return FAIL;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004796 if (fputs(cmd, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 return FAIL;
4798 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4799 return FAIL;
4800 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4801 return FAIL;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004802#ifdef FEAT_EVAL
4803 if (mp->m_noremap == REMAP_SCRIPT
4804 && fputs("<script>", fd) < 0)
4805 return FAIL;
4806 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4807 return FAIL;
4808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
4810 if ( putc(' ', fd) < 0
4811 || put_escstr(fd, mp->m_keys, 0) == FAIL
4812 || putc(' ', fd) < 0
4813 || put_escstr(fd, mp->m_str, 1) == FAIL
4814 || put_eol(fd) < 0)
4815 return FAIL;
4816 c1 = c2;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004817 c2 = c3;
4818 c3 = NUL;
4819 } while (c1 != NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821 }
4822
4823 if (did_cpo)
4824 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4825 || put_eol(fd) < 0
4826 || fprintf(fd, "unlet s:cpo_save") < 0
4827 || put_eol(fd) < 0)
4828 return FAIL;
4829 return OK;
4830}
4831
4832/*
4833 * write escape string to file
4834 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4835 *
4836 * return FAIL for failure, OK otherwise
4837 */
4838 int
4839put_escstr(fd, strstart, what)
4840 FILE *fd;
4841 char_u *strstart;
4842 int what;
4843{
4844 char_u *str = strstart;
4845 int c;
4846 int modifiers;
4847
4848 /* :map xx <Nop> */
4849 if (*str == NUL && what == 1)
4850 {
4851 if (fprintf(fd, "<Nop>") < 0)
4852 return FAIL;
4853 return OK;
4854 }
4855
4856 for ( ; *str != NUL; ++str)
4857 {
4858#ifdef FEAT_MBYTE
4859 char_u *p;
4860
4861 /* Check for a multi-byte character, which may contain escaped
4862 * K_SPECIAL and CSI bytes */
4863 p = mb_unescape(&str);
4864 if (p != NULL)
4865 {
4866 while (*p != NUL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004867 if (fputc(*p++, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 return FAIL;
4869 --str;
4870 continue;
4871 }
4872#endif
4873
4874 c = *str;
4875 /*
4876 * Special key codes have to be translated to be able to make sense
4877 * when they are read back.
4878 */
4879 if (c == K_SPECIAL && what != 2)
4880 {
4881 modifiers = 0x0;
4882 if (str[1] == KS_MODIFIER)
4883 {
4884 modifiers = str[2];
4885 str += 3;
4886 c = *str;
4887 }
4888 if (c == K_SPECIAL)
4889 {
4890 c = TO_SPECIAL(str[1], str[2]);
4891 str += 2;
4892 }
4893 if (IS_SPECIAL(c) || modifiers) /* special key */
4894 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004895 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return FAIL;
4897 continue;
4898 }
4899 }
4900
4901 /*
4902 * A '\n' in a map command should be written as <NL>.
4903 * A '\n' in a set command should be written as \^V^J.
4904 */
4905 if (c == NL)
4906 {
4907 if (what == 2)
4908 {
4909 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4910 return FAIL;
4911 }
4912 else
4913 {
4914 if (fprintf(fd, "<NL>") < 0)
4915 return FAIL;
4916 }
4917 continue;
4918 }
4919
4920 /*
4921 * Some characters have to be escaped with CTRL-V to
4922 * prevent them from misinterpreted in DoOneCmd().
4923 * A space, Tab and '"' has to be escaped with a backslash to
4924 * prevent it to be misinterpreted in do_set().
4925 * A space has to be escaped with a CTRL-V when it's at the start of a
4926 * ":map" rhs.
4927 * A '<' has to be escaped with a CTRL-V to prevent it being
4928 * interpreted as the start of a special key name.
4929 * A space in the lhs of a :map needs a CTRL-V.
4930 */
4931 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4932 {
4933 if (putc('\\', fd) < 0)
4934 return FAIL;
4935 }
4936 else if (c < ' ' || c > '~' || c == '|'
4937 || (what == 0 && c == ' ')
4938 || (what == 1 && str == strstart && c == ' ')
4939 || (what != 2 && c == '<'))
4940 {
4941 if (putc(Ctrl_V, fd) < 0)
4942 return FAIL;
4943 }
4944 if (putc(c, fd) < 0)
4945 return FAIL;
4946 }
4947 return OK;
4948}
4949
4950/*
4951 * Check all mappings for the presence of special key codes.
4952 * Used after ":set term=xxx".
4953 */
4954 void
4955check_map_keycodes()
4956{
4957 mapblock_T *mp;
4958 char_u *p;
4959 int i;
4960 char_u buf[3];
4961 char_u *save_name;
4962 int abbr;
4963 int hash;
4964#ifdef FEAT_LOCALMAP
4965 buf_T *bp;
4966#endif
4967
4968 validate_maphash();
4969 save_name = sourcing_name;
4970 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4971
4972#ifdef FEAT_LOCALMAP
4973 /* This this once for each buffer, and then once for global
4974 * mappings/abbreviations with bp == NULL */
4975 for (bp = firstbuf; ; bp = bp->b_next)
4976 {
4977#endif
4978 /*
4979 * Do the loop twice: Once for mappings, once for abbreviations.
4980 * Then loop over all map hash lists.
4981 */
4982 for (abbr = 0; abbr <= 1; ++abbr)
4983 for (hash = 0; hash < 256; ++hash)
4984 {
4985 if (abbr)
4986 {
4987 if (hash) /* there is only one abbr list */
4988 break;
4989#ifdef FEAT_LOCALMAP
4990 if (bp != NULL)
4991 mp = bp->b_first_abbr;
4992 else
4993#endif
4994 mp = first_abbr;
4995 }
4996 else
4997 {
4998#ifdef FEAT_LOCALMAP
4999 if (bp != NULL)
5000 mp = bp->b_maphash[hash];
5001 else
5002#endif
5003 mp = maphash[hash];
5004 }
5005 for ( ; mp != NULL; mp = mp->m_next)
5006 {
5007 for (i = 0; i <= 1; ++i) /* do this twice */
5008 {
5009 if (i == 0)
5010 p = mp->m_keys; /* once for the "from" part */
5011 else
5012 p = mp->m_str; /* and once for the "to" part */
5013 while (*p)
5014 {
5015 if (*p == K_SPECIAL)
5016 {
5017 ++p;
5018 if (*p < 128) /* for "normal" tcap entries */
5019 {
5020 buf[0] = p[0];
5021 buf[1] = p[1];
5022 buf[2] = NUL;
5023 (void)add_termcap_entry(buf, FALSE);
5024 }
5025 ++p;
5026 }
5027 ++p;
5028 }
5029 }
5030 }
5031 }
5032#ifdef FEAT_LOCALMAP
5033 if (bp == NULL)
5034 break;
5035 }
5036#endif
5037 sourcing_name = save_name;
5038}
5039
Bram Moolenaarbd743252010-10-20 21:23:33 +02005040#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041/*
Bram Moolenaarbd743252010-10-20 21:23:33 +02005042 * Check the string "keys" against the lhs of all mappings.
5043 * Return pointer to rhs of mapping (mapblock->m_str).
5044 * NULL when no mapping found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
5046 char_u *
Bram Moolenaarbd743252010-10-20 21:23:33 +02005047check_map(keys, mode, exact, ign_mod, abbr, mp_ptr, local_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 char_u *keys;
5049 int mode;
5050 int exact; /* require exact match */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005051 int ign_mod; /* ignore preceding modifier */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005052 int abbr; /* do abbreviations */
Bram Moolenaarbd743252010-10-20 21:23:33 +02005053 mapblock_T **mp_ptr; /* return: pointer to mapblock or NULL */
5054 int *local_ptr; /* return: buffer-local mapping or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 int hash;
5057 int len, minlen;
5058 mapblock_T *mp;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060#ifdef FEAT_LOCALMAP
5061 int local;
5062#endif
5063
5064 validate_maphash();
5065
5066 len = (int)STRLEN(keys);
5067#ifdef FEAT_LOCALMAP
5068 for (local = 1; local >= 0; --local)
5069#endif
5070 /* loop over all hash lists */
5071 for (hash = 0; hash < 256; ++hash)
5072 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005073 if (abbr)
5074 {
5075 if (hash > 0) /* there is only one list. */
5076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005078 if (local)
5079 mp = curbuf->b_first_abbr;
5080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005082 mp = first_abbr;
5083 }
5084#ifdef FEAT_LOCALMAP
5085 else if (local)
5086 mp = curbuf->b_maphash[hash];
5087#endif
5088 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 mp = maphash[hash];
5090 for ( ; mp != NULL; mp = mp->m_next)
5091 {
5092 /* skip entries with wrong mode, wrong length and not matching
5093 * ones */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005094 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
5095 {
5096 if (len > mp->m_keylen)
5097 minlen = mp->m_keylen;
5098 else
5099 minlen = len;
5100 s = mp->m_keys;
5101 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
5102 && s[2] != NUL)
5103 {
5104 s += 3;
5105 if (len > mp->m_keylen - 3)
5106 minlen = mp->m_keylen - 3;
5107 }
5108 if (STRNCMP(s, keys, minlen) == 0)
Bram Moolenaarbd743252010-10-20 21:23:33 +02005109 {
5110 if (mp_ptr != NULL)
5111 *mp_ptr = mp;
5112 if (local_ptr != NULL)
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005113#ifdef FEAT_LOCALMAP
Bram Moolenaarbd743252010-10-20 21:23:33 +02005114 *local_ptr = local;
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005115#else
5116 *local_ptr = 0;
5117#endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005118 return mp->m_str;
Bram Moolenaarbd743252010-10-20 21:23:33 +02005119 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122 }
5123
5124 return NULL;
5125}
5126#endif
5127
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005128#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar371d5402006-03-20 21:47:49 +00005129
5130#define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
5131
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132/*
5133 * Default mappings for some often used keys.
5134 */
5135static struct initmap
5136{
5137 char_u *arg;
5138 int mode;
5139} initmappings[] =
5140{
5141#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
5142 /* Use the Windows (CUA) keybindings. */
5143# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 /* paste, copy and cut */
5145 {(char_u *)"<S-Insert> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005146 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005148 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
5149 {(char_u *)"<S-Del> \"*d", VIS_SEL},
5150 {(char_u *)"<C-Del> \"*d", VIS_SEL},
5151 {(char_u *)"<C-X> \"*d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5153# else
Bram Moolenaar371d5402006-03-20 21:47:49 +00005154 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005156 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5158
5159 /* paste, copy and cut */
5160# ifdef FEAT_CLIPBOARD
5161# ifdef DJGPP
5162 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005163 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005165 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166# if 0 /* Shift-Del produces the same code as Del */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005167 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00005169 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5170 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171# else
5172 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005173 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005175 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5176 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5177 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5178 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179# endif
5180# else
5181 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005182 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005184 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5185 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5186 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187# endif
5188# endif
5189#endif
5190
5191#if defined(MACOS)
5192 /* Use the Standard MacOS binding. */
5193 /* paste, copy and cut */
5194 {(char_u *)"<D-v> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005195 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005197 {(char_u *)"<D-c> \"*y", VIS_SEL},
5198 {(char_u *)"<D-x> \"*d", VIS_SEL},
5199 {(char_u *)"<Backspace> \"-d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201};
Bram Moolenaar371d5402006-03-20 21:47:49 +00005202
5203# undef VIS_SEL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206/*
5207 * Set up default mappings.
5208 */
5209 void
5210init_mappings()
5211{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005212#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 int i;
5214
5215 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5216 add_map(initmappings[i].arg, initmappings[i].mode);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218}
5219
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005220#if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5221 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222/*
5223 * Add a mapping "map" for mode "mode".
5224 * Need to put string in allocated memory, because do_map() will modify it.
5225 */
5226 void
5227add_map(map, mode)
5228 char_u *map;
5229 int mode;
5230{
5231 char_u *s;
5232 char_u *cpo_save = p_cpo;
5233
5234 p_cpo = (char_u *)""; /* Allow <> notation */
5235 s = vim_strsave(map);
5236 if (s != NULL)
5237 {
5238 (void)do_map(0, s, mode, FALSE);
5239 vim_free(s);
5240 }
5241 p_cpo = cpo_save;
5242}
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005243#endif