blob: bc30d6c99bfff1317f7a339e275669ff6e907cc4 [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.
25 * - recorded chracters: for the "q" command.
26 *
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 */
66#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + OP_PENDING)) ? (c1) : ((c1) ^ 0x80))
67
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
79static int KeyNoremap = FALSE; /* remapping disabled */
80
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));
131
132/*
133 * Free and clear a buffer.
134 */
135 void
136free_buff(buf)
137 struct buffheader *buf;
138{
139 struct buffblock *p, *np;
140
141 for (p = buf->bh_first.b_next; p != NULL; p = np)
142 {
143 np = p->b_next;
144 vim_free(p);
145 }
146 buf->bh_first.b_next = NULL;
147}
148
149/*
150 * Return the contents of a buffer as a single string.
151 * K_SPECIAL and CSI in the returned string are escaped.
152 */
153 static char_u *
154get_buffcont(buffer, dozero)
155 struct buffheader *buffer;
156 int dozero; /* count == zero is not an error */
157{
158 long_u count = 0;
159 char_u *p = NULL;
160 char_u *p2;
161 char_u *str;
162 struct buffblock *bp;
163
164 /* compute the total length of the string */
165 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
166 count += (long_u)STRLEN(bp->b_str);
167
168 if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL)
169 {
170 p2 = p;
171 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
172 for (str = bp->b_str; *str; )
173 *p2++ = *str++;
174 *p2 = NUL;
175 }
176 return (p);
177}
178
179/*
180 * Return the contents of the record buffer as a single string
181 * and clear the record buffer.
182 * K_SPECIAL and CSI in the returned string are escaped.
183 */
184 char_u *
185get_recorded()
186{
187 char_u *p;
188 size_t len;
189
190 p = get_buffcont(&recordbuff, TRUE);
191 free_buff(&recordbuff);
192
193 /*
194 * Remove the characters that were added the last time, these must be the
195 * (possibly mapped) characters that stopped the recording.
196 */
197 len = STRLEN(p);
198 if ((int)len >= last_recorded_len)
199 {
200 len -= last_recorded_len;
201 p[len] = NUL;
202 }
203
204 /*
205 * When stopping recording from Insert mode with CTRL-O q, also remove the
206 * CTRL-O.
207 */
208 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
209 p[len - 1] = NUL;
210
211 return (p);
212}
213
214/*
215 * Return the contents of the redo buffer as a single string.
216 * K_SPECIAL and CSI in the returned string are escaped.
217 */
218 char_u *
219get_inserted()
220{
221 return(get_buffcont(&redobuff, FALSE));
222}
223
224/*
225 * add string "s" after the current block of buffer "buf"
226 * K_SPECIAL and CSI should have been escaped already.
227 */
228 static void
229add_buff(buf, s, slen)
230 struct buffheader *buf;
231 char_u *s;
232 long slen; /* length of "s" or -1 */
233{
234 struct buffblock *p;
235 long_u len;
236
237 if (slen < 0)
238 slen = (long)STRLEN(s);
239 if (slen == 0) /* don't add empty strings */
240 return;
241
242 if (buf->bh_first.b_next == NULL) /* first add to list */
243 {
244 buf->bh_space = 0;
245 buf->bh_curr = &(buf->bh_first);
246 }
247 else if (buf->bh_curr == NULL) /* buffer has already been read */
248 {
249 EMSG(_("E222: Add to read buffer"));
250 return;
251 }
252 else if (buf->bh_index != 0)
253 STRCPY(buf->bh_first.b_next->b_str,
254 buf->bh_first.b_next->b_str + buf->bh_index);
255 buf->bh_index = 0;
256
257 if (buf->bh_space >= (int)slen)
258 {
259 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000260 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 buf->bh_space -= slen;
262 }
263 else
264 {
265 if (slen < MINIMAL_SIZE)
266 len = MINIMAL_SIZE;
267 else
268 len = slen;
269 p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len),
270 TRUE);
271 if (p == NULL)
272 return; /* no space, just forget it */
273 buf->bh_space = len - slen;
Bram Moolenaarb6356332005-07-18 21:40:44 +0000274 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
276 p->b_next = buf->bh_curr->b_next;
277 buf->bh_curr->b_next = p;
278 buf->bh_curr = p;
279 }
280 return;
281}
282
283/*
284 * Add number "n" to buffer "buf".
285 */
286 static void
287add_num_buff(buf, n)
288 struct buffheader *buf;
289 long n;
290{
291 char_u number[32];
292
293 sprintf((char *)number, "%ld", n);
294 add_buff(buf, number, -1L);
295}
296
297/*
298 * Add character 'c' to buffer "buf".
299 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
300 */
301 static void
302add_char_buff(buf, c)
303 struct buffheader *buf;
304 int c;
305{
306#ifdef FEAT_MBYTE
307 char_u bytes[MB_MAXBYTES + 1];
308 int len;
309 int i;
310#endif
311 char_u temp[4];
312
313#ifdef FEAT_MBYTE
314 if (IS_SPECIAL(c))
315 len = 1;
316 else
317 len = (*mb_char2bytes)(c, bytes);
318 for (i = 0; i < len; ++i)
319 {
320 if (!IS_SPECIAL(c))
321 c = bytes[i];
322#endif
323
324 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
325 {
326 /* translate special key code into three byte sequence */
327 temp[0] = K_SPECIAL;
328 temp[1] = K_SECOND(c);
329 temp[2] = K_THIRD(c);
330 temp[3] = NUL;
331 }
332#ifdef FEAT_GUI
333 else if (c == CSI)
334 {
335 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
336 temp[0] = CSI;
337 temp[1] = KS_EXTRA;
338 temp[2] = (int)KE_CSI;
339 temp[3] = NUL;
340 }
341#endif
342 else
343 {
344 temp[0] = c;
345 temp[1] = NUL;
346 }
347 add_buff(buf, temp, -1L);
348#ifdef FEAT_MBYTE
349 }
350#endif
351}
352
353/*
354 * Get one byte from the stuff buffer.
355 * If advance == TRUE go to the next char.
356 * No translation is done K_SPECIAL and CSI are escaped.
357 */
358 static int
359read_stuff(advance)
360 int advance;
361{
362 char_u c;
363 struct buffblock *curr;
364
365 if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */
366 return NUL;
367
368 curr = stuffbuff.bh_first.b_next;
369 c = curr->b_str[stuffbuff.bh_index];
370
371 if (advance)
372 {
373 if (curr->b_str[++stuffbuff.bh_index] == NUL)
374 {
375 stuffbuff.bh_first.b_next = curr->b_next;
376 vim_free(curr);
377 stuffbuff.bh_index = 0;
378 }
379 }
380 return c;
381}
382
383/*
384 * Prepare the stuff buffer for reading (if it contains something).
385 */
386 static void
387start_stuff()
388{
389 if (stuffbuff.bh_first.b_next != NULL)
390 {
391 stuffbuff.bh_curr = &(stuffbuff.bh_first);
392 stuffbuff.bh_space = 0;
393 }
394}
395
396/*
397 * Return TRUE if the stuff buffer is empty.
398 */
399 int
400stuff_empty()
401{
402 return (stuffbuff.bh_first.b_next == NULL);
403}
404
405/*
406 * Set a typeahead character that won't be flushed.
407 */
408 void
409typeahead_noflush(c)
410 int c;
411{
412 typeahead_char = c;
413}
414
415/*
416 * Remove the contents of the stuff buffer and the mapped characters in the
417 * typeahead buffer (used in case of an error). If 'typeahead' is true,
418 * flush all typeahead characters (used when interrupted by a CTRL-C).
419 */
420 void
421flush_buffers(typeahead)
422 int typeahead;
423{
424 init_typebuf();
425
426 start_stuff();
427 while (read_stuff(TRUE) != NUL)
428 ;
429
430 if (typeahead) /* remove all typeahead */
431 {
432 /*
433 * We have to get all characters, because we may delete the first part
434 * of an escape sequence.
435 * In an xterm we get one char at a time and we have to get them all.
436 */
437 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L,
438 typebuf.tb_change_cnt) != 0)
439 ;
440 typebuf.tb_off = MAXMAPLEN;
441 typebuf.tb_len = 0;
442 }
443 else /* remove mapped characters only */
444 {
445 typebuf.tb_off += typebuf.tb_maplen;
446 typebuf.tb_len -= typebuf.tb_maplen;
447 }
448 typebuf.tb_maplen = 0;
449 typebuf.tb_silent = 0;
450 cmd_silent = FALSE;
451 typebuf.tb_no_abbr_cnt = 0;
452}
453
454/*
455 * The previous contents of the redo buffer is kept in old_redobuffer.
456 * This is used for the CTRL-O <.> command in insert mode.
457 */
458 void
459ResetRedobuff()
460{
461 if (!block_redo)
462 {
463 free_buff(&old_redobuff);
464 old_redobuff = redobuff;
465 redobuff.bh_first.b_next = NULL;
466 }
467}
468
469#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
470/*
471 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
472 * Used before executing autocommands and user functions.
473 */
474static int save_level = 0;
475
476 void
477saveRedobuff()
478{
479 char_u *s;
480
481 if (save_level++ == 0)
482 {
483 save_redobuff = redobuff;
484 redobuff.bh_first.b_next = NULL;
485 save_old_redobuff = old_redobuff;
486 old_redobuff.bh_first.b_next = NULL;
487
488 /* Make a copy, so that ":normal ." in a function works. */
489 s = get_buffcont(&save_redobuff, FALSE);
490 if (s != NULL)
491 {
492 add_buff(&redobuff, s, -1L);
493 vim_free(s);
494 }
495 }
496}
497
498/*
499 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
500 * Used after executing autocommands and user functions.
501 */
502 void
503restoreRedobuff()
504{
505 if (--save_level == 0)
506 {
507 free_buff(&redobuff);
508 redobuff = save_redobuff;
509 free_buff(&old_redobuff);
510 old_redobuff = save_old_redobuff;
511 }
512}
513#endif
514
515/*
516 * Append "s" to the redo buffer.
517 * K_SPECIAL and CSI should already have been escaped.
518 */
519 void
520AppendToRedobuff(s)
521 char_u *s;
522{
523 if (!block_redo)
524 add_buff(&redobuff, s, -1L);
525}
526
527/*
528 * Append to Redo buffer literally, escaping special characters with CTRL-V.
529 * K_SPECIAL and CSI are escaped as well.
530 */
531 void
532AppendToRedobuffLit(s)
533 char_u *s;
534{
535 int c;
536 char_u *start;
537
538 if (block_redo)
539 return;
540
541 while (*s != NUL)
542 {
543 /* Put a string of normal characters in the redo buffer (that's
544 * faster). */
545 start = s;
546 while (*s >= ' '
547#ifndef EBCDIC
548 && *s < DEL /* EBCDIC: all chars above space are normal */
549#endif
550 )
551 ++s;
552
553 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
554 * typed next. */
555 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
556 --s;
557 if (s > start)
558 add_buff(&redobuff, start, (long)(s - start));
559
560 if (*s != NUL)
561 {
562 /* Handle a special or multibyte character. */
563#ifdef FEAT_MBYTE
564 if (has_mbyte)
565 {
566 c = (*mb_ptr2char)(s);
567 if (enc_utf8)
568 /* Handle composing chars as well. */
569 s += utf_ptr2len_check(s);
570 else
571 s += (*mb_ptr2len_check)(s);
572 }
573 else
574#endif
575 c = *s++;
576 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
577 add_char_buff(&redobuff, Ctrl_V);
578
579 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
580 if (*s == NUL && c == '0')
581#ifdef EBCDIC
582 add_buff(&redobuff, (char_u *)"xf0", 3L);
583#else
584 add_buff(&redobuff, (char_u *)"048", 3L);
585#endif
586 else
587 add_char_buff(&redobuff, c);
588 }
589 }
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.
638 */
639 void
640stuffReadbuffSpec(s)
641 char_u *s;
642{
643 while (*s != NUL)
644 {
645 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
646 {
647 /* Insert special key literally. */
648 stuffReadbuffLen(s, 3L);
649 s += 3;
650 }
651 else
652#ifdef FEAT_MBYTE
653 stuffcharReadbuff(mb_ptr2char_adv(&s));
654#else
655 stuffcharReadbuff(*s++);
656#endif
657 }
658}
659#endif
660
661/*
662 * Append a character to the stuff buffer.
663 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
664 */
665 void
666stuffcharReadbuff(c)
667 int c;
668{
669 add_char_buff(&stuffbuff, c);
670}
671
672/*
673 * Append a number to the stuff buffer.
674 */
675 void
676stuffnumReadbuff(n)
677 long n;
678{
679 add_num_buff(&stuffbuff, n);
680}
681
682/*
683 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
684 * multibyte characters.
685 * The redo buffer is left as it is.
686 * if init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
687 * otherwise
688 * if old is TRUE, use old_redobuff instead of redobuff
689 */
690 static int
691read_redo(init, old_redo)
692 int init;
693 int old_redo;
694{
695 static struct buffblock *bp;
696 static char_u *p;
697 int c;
698#ifdef FEAT_MBYTE
699 int n;
700 char_u buf[MB_MAXBYTES];
701 int i;
702#endif
703
704 if (init)
705 {
706 if (old_redo)
707 bp = old_redobuff.bh_first.b_next;
708 else
709 bp = redobuff.bh_first.b_next;
710 if (bp == NULL)
711 return FAIL;
712 p = bp->b_str;
713 return OK;
714 }
715 if ((c = *p) != NUL)
716 {
717 /* Reverse the conversion done by add_char_buff() */
718#ifdef FEAT_MBYTE
719 /* For a multi-byte character get all the bytes and return the
720 * converted character. */
721 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
722 n = MB_BYTE2LEN_CHECK(c);
723 else
724 n = 1;
725 for (i = 0; ; ++i)
726#endif
727 {
728 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
729 {
730 c = TO_SPECIAL(p[1], p[2]);
731 p += 2;
732 }
733#ifdef FEAT_GUI
734 if (c == CSI) /* escaped CSI */
735 p += 2;
736#endif
737 if (*++p == NUL && bp->b_next != NULL)
738 {
739 bp = bp->b_next;
740 p = bp->b_str;
741 }
742#ifdef FEAT_MBYTE
743 buf[i] = c;
744 if (i == n - 1) /* last byte of a character */
745 {
746 if (n != 1)
747 c = (*mb_ptr2char)(buf);
748 break;
749 }
750 c = *p;
751 if (c == NUL) /* cannot happen? */
752 break;
753#endif
754 }
755 }
756
757 return c;
758}
759
760/*
761 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
762 * If old_redo is TRUE, use old_redobuff instead of redobuff.
763 * The escaped K_SPECIAL and CSI are copied without translation.
764 */
765 static void
766copy_redo(old_redo)
767 int old_redo;
768{
769 int c;
770
771 while ((c = read_redo(FALSE, old_redo)) != NUL)
772 stuffcharReadbuff(c);
773}
774
775/*
776 * Stuff the redo buffer into the stuffbuff.
777 * Insert the redo count into the command.
778 * If "old_redo" is TRUE, the last but one command is repeated
779 * instead of the last command (inserting text). This is used for
780 * CTRL-O <.> in insert mode
781 *
782 * return FAIL for failure, OK otherwise
783 */
784 int
785start_redo(count, old_redo)
786 long count;
787 int old_redo;
788{
789 int c;
790
791 /* init the pointers; return if nothing to redo */
792 if (read_redo(TRUE, old_redo) == FAIL)
793 return FAIL;
794
795 c = read_redo(FALSE, old_redo);
796
797 /* copy the buffer name, if present */
798 if (c == '"')
799 {
800 add_buff(&stuffbuff, (char_u *)"\"", 1L);
801 c = read_redo(FALSE, old_redo);
802
803 /* if a numbered buffer is used, increment the number */
804 if (c >= '1' && c < '9')
805 ++c;
806 add_char_buff(&stuffbuff, c);
807 c = read_redo(FALSE, old_redo);
808 }
809
810#ifdef FEAT_VISUAL
811 if (c == 'v') /* redo Visual */
812 {
813 VIsual = curwin->w_cursor;
814 VIsual_active = TRUE;
815 VIsual_select = FALSE;
816 VIsual_reselect = TRUE;
817 redo_VIsual_busy = TRUE;
818 c = read_redo(FALSE, old_redo);
819 }
820#endif
821
822 /* try to enter the count (in place of a previous count) */
823 if (count)
824 {
825 while (VIM_ISDIGIT(c)) /* skip "old" count */
826 c = read_redo(FALSE, old_redo);
827 add_num_buff(&stuffbuff, count);
828 }
829
830 /* copy from the redo buffer into the stuff buffer */
831 add_char_buff(&stuffbuff, c);
832 copy_redo(old_redo);
833 return OK;
834}
835
836/*
837 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
838 * the redo buffer into the stuffbuff.
839 * return FAIL for failure, OK otherwise
840 */
841 int
842start_redo_ins()
843{
844 int c;
845
846 if (read_redo(TRUE, FALSE) == FAIL)
847 return FAIL;
848 start_stuff();
849
850 /* skip the count and the command character */
851 while ((c = read_redo(FALSE, FALSE)) != NUL)
852 {
853 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
854 {
855 if (c == 'O' || c == 'o')
856 stuffReadbuff(NL_STR);
857 break;
858 }
859 }
860
861 /* copy the typed text from the redo buffer into the stuff buffer */
862 copy_redo(FALSE);
863 block_redo = TRUE;
864 return OK;
865}
866
867 void
868stop_redo_ins()
869{
870 block_redo = FALSE;
871}
872
873/*
874 * Initialize typebuf.tb_buf to point to typebuf_init.
875 * alloc() cannot be used here: In out-of-memory situations it would
876 * be impossible to type anything.
877 */
878 static void
879init_typebuf()
880{
881 if (typebuf.tb_buf == NULL)
882 {
883 typebuf.tb_buf = typebuf_init;
884 typebuf.tb_noremap = noremapbuf_init;
885 typebuf.tb_buflen = TYPELEN_INIT;
886 typebuf.tb_len = 0;
887 typebuf.tb_off = 0;
888 typebuf.tb_change_cnt = 1;
889 }
890}
891
892/*
893 * insert a string in position 'offset' in the typeahead buffer (for "@r"
894 * and ":normal" command, vgetorpeek() and check_termcode())
895 *
896 * If noremap is REMAP_YES, new string can be mapped again.
897 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000898 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
899 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
901 * script-local mappings.
902 * If noremap is > 0, that many characters of the new string cannot be mapped.
903 *
904 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
905 * offset is non-zero!).
906 *
907 * If silent is TRUE, cmd_silent is set when the characters are obtained.
908 *
909 * return FAIL for failure, OK otherwise
910 */
911 int
912ins_typebuf(str, noremap, offset, nottyped, silent)
913 char_u *str;
914 int noremap;
915 int offset;
916 int nottyped;
917 int silent;
918{
919 char_u *s1, *s2;
920 int newlen;
921 int addlen;
922 int i;
923 int newoff;
924 int val;
925 int nrm;
926
927 init_typebuf();
928 if (++typebuf.tb_change_cnt == 0)
929 typebuf.tb_change_cnt = 1;
930
931 addlen = (int)STRLEN(str);
932 /*
933 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
934 */
935 if (offset == 0 && addlen <= typebuf.tb_off)
936 {
937 typebuf.tb_off -= addlen;
938 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
939 }
940 /*
941 * Need to allocate new buffer.
942 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
943 * characters. We add some extra room to avoid having to allocate too
944 * often.
945 */
946 else
947 {
948 newoff = MAXMAPLEN + 4;
949 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
950 if (newlen < 0) /* string is getting too long */
951 {
952 EMSG(_(e_toocompl)); /* also calls flush_buffers */
953 setcursor();
954 return FAIL;
955 }
956 s1 = alloc(newlen);
957 if (s1 == NULL) /* out of memory */
958 return FAIL;
959 s2 = alloc(newlen);
960 if (s2 == NULL) /* out of memory */
961 {
962 vim_free(s1);
963 return FAIL;
964 }
965 typebuf.tb_buflen = newlen;
966
967 /* copy the old chars, before the insertion point */
968 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
969 (size_t)offset);
970 /* copy the new chars */
971 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
972 /* copy the old chars, after the insertion point, including the NUL at
973 * the end */
974 mch_memmove(s1 + newoff + offset + addlen,
975 typebuf.tb_buf + typebuf.tb_off + offset,
976 (size_t)(typebuf.tb_len - offset + 1));
977 if (typebuf.tb_buf != typebuf_init)
978 vim_free(typebuf.tb_buf);
979 typebuf.tb_buf = s1;
980
981 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
982 (size_t)offset);
983 mch_memmove(s2 + newoff + offset + addlen,
984 typebuf.tb_noremap + typebuf.tb_off + offset,
985 (size_t)(typebuf.tb_len - offset));
986 if (typebuf.tb_noremap != noremapbuf_init)
987 vim_free(typebuf.tb_noremap);
988 typebuf.tb_noremap = s2;
989
990 typebuf.tb_off = newoff;
991 }
992 typebuf.tb_len += addlen;
993
994 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
995 if (noremap == REMAP_SCRIPT)
996 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000997 else if (noremap == REMAP_SKIP)
998 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 else
1000 val = RM_NONE;
1001
1002 /*
1003 * Adjust typebuf.tb_noremap[] for the new characters:
1004 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1005 * (sometimes) not remappable
1006 * If noremap == REMAP_YES: all the new characters are mappable
1007 * If noremap > 0: "noremap" characters are not remappable, the rest
1008 * mappable
1009 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001010 if (noremap == REMAP_SKIP)
1011 nrm = 1;
1012 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 nrm = addlen;
1014 else
1015 nrm = noremap;
1016 for (i = 0; i < addlen; ++i)
1017 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1018 (--nrm >= 0) ? val : RM_YES;
1019
1020 /* tb_maplen and tb_silent only remember the length of mapped and/or
1021 * silent mappings at the start of the buffer, assuming that a mapped
1022 * sequence doesn't result in typed characters. */
1023 if (nottyped || typebuf.tb_maplen > offset)
1024 typebuf.tb_maplen += addlen;
1025 if (silent || typebuf.tb_silent > offset)
1026 {
1027 typebuf.tb_silent += addlen;
1028 cmd_silent = TRUE;
1029 }
1030 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1031 typebuf.tb_no_abbr_cnt += addlen;
1032
1033 return OK;
1034}
1035
1036/*
1037 * Return TRUE if the typeahead buffer was changed (while waiting for a
1038 * character to arrive). Happens when a message was received from a client.
1039 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1040 * changed it was reallocated and the old pointer can no longer be used.
1041 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1042 * that was just added.
1043 */
1044 int
1045typebuf_changed(tb_change_cnt)
1046 int tb_change_cnt; /* old value of typebuf.tb_change_cnt */
1047{
1048 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
1049#ifdef FEAT_CLIENTSERVER
1050 || received_from_client
1051#endif
1052 ));
1053}
1054
1055/*
1056 * Return TRUE if there are no characters in the typeahead buffer that have
1057 * not been typed (result from a mapping or come from ":normal").
1058 */
1059 int
1060typebuf_typed()
1061{
1062 return typebuf.tb_maplen == 0;
1063}
1064
1065/*
1066 * Return the number of characters that are mapped (or not typed).
1067 */
1068 int
1069typebuf_maplen()
1070{
1071 return typebuf.tb_maplen;
1072}
1073
1074/*
1075 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1076 */
1077 void
1078del_typebuf(len, offset)
1079 int len;
1080 int offset;
1081{
1082 int i;
1083
1084 if (len == 0)
1085 return; /* nothing to do */
1086
1087 typebuf.tb_len -= len;
1088
1089 /*
1090 * Easy case: Just increase typebuf.tb_off.
1091 */
1092 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1093 >= 3 * MAXMAPLEN + 3)
1094 typebuf.tb_off += len;
1095 /*
1096 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1097 */
1098 else
1099 {
1100 i = typebuf.tb_off + offset;
1101 /*
1102 * Leave some extra room at the end to avoid reallocation.
1103 */
1104 if (typebuf.tb_off > MAXMAPLEN)
1105 {
1106 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1107 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1108 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1109 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1110 typebuf.tb_off = MAXMAPLEN;
1111 }
1112 /* adjust typebuf.tb_buf (include the NUL at the end) */
1113 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1114 typebuf.tb_buf + i + len,
1115 (size_t)(typebuf.tb_len - offset + 1));
1116 /* adjust typebuf.tb_noremap[] */
1117 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1118 typebuf.tb_noremap + i + len,
1119 (size_t)(typebuf.tb_len - offset));
1120 }
1121
1122 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1123 {
1124 if (typebuf.tb_maplen < offset + len)
1125 typebuf.tb_maplen = offset;
1126 else
1127 typebuf.tb_maplen -= len;
1128 }
1129 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1130 {
1131 if (typebuf.tb_silent < offset + len)
1132 typebuf.tb_silent = offset;
1133 else
1134 typebuf.tb_silent -= len;
1135 }
1136 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1137 {
1138 if (typebuf.tb_no_abbr_cnt < offset + len)
1139 typebuf.tb_no_abbr_cnt = offset;
1140 else
1141 typebuf.tb_no_abbr_cnt -= len;
1142 }
1143
1144#ifdef FEAT_CLIENTSERVER
1145 /* Reset the flag that text received from a client was inserted in the
1146 * typeahead buffer. */
1147 received_from_client = FALSE;
1148#endif
1149 if (++typebuf.tb_change_cnt == 0)
1150 typebuf.tb_change_cnt = 1;
1151}
1152
1153/*
1154 * Write typed characters to script file.
1155 * If recording is on put the character in the recordbuffer.
1156 */
1157 static void
1158gotchars(s, len)
1159 char_u *s;
1160 int len;
1161{
1162 int c;
1163 char_u buf[2];
1164
1165 /* remember how many chars were last recorded */
1166 if (Recording)
1167 last_recorded_len += len;
1168
1169 buf[1] = NUL;
1170 while (len--)
1171 {
1172 /* Handle one byte at a time; no translation to be done. */
1173 c = *s++;
1174 updatescript(c);
1175
1176 if (Recording)
1177 {
1178 buf[0] = c;
1179 add_buff(&recordbuff, buf, 1L);
1180 }
1181 }
1182 may_sync_undo();
1183
1184#ifdef FEAT_EVAL
1185 /* output "debug mode" message next time in debug mode */
1186 debug_did_msg = FALSE;
1187#endif
1188
1189 /* Since characters have been typed, consider the following to be in
1190 * another mapping. Search string will be kept in history. */
1191 ++maptick;
1192}
1193
1194/*
1195 * Sync undo. Called when typed characters are obtained from the typeahead
1196 * buffer, or when a menu is used.
1197 * Do not sync:
1198 * - In Insert mode, unless cursor key has been used.
1199 * - While reading a script file.
1200 * - When no_u_sync is non-zero.
1201 */
1202 static void
1203may_sync_undo()
1204{
1205 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
1206 && scriptin[curscript] == NULL && no_u_sync == 0)
1207 u_sync();
1208}
1209
1210/*
1211 * Make "typebuf" empty and allocate new buffers.
1212 * Returns FAIL when out of memory.
1213 */
1214 int
1215alloc_typebuf()
1216{
1217 typebuf.tb_buf = alloc(TYPELEN_INIT);
1218 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1219 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1220 {
1221 free_typebuf();
1222 return FAIL;
1223 }
1224 typebuf.tb_buflen = TYPELEN_INIT;
1225 typebuf.tb_off = 0;
1226 typebuf.tb_len = 0;
1227 typebuf.tb_maplen = 0;
1228 typebuf.tb_silent = 0;
1229 typebuf.tb_no_abbr_cnt = 0;
1230 if (++typebuf.tb_change_cnt == 0)
1231 typebuf.tb_change_cnt = 1;
1232 return OK;
1233}
1234
1235/*
1236 * Free the buffers of "typebuf".
1237 */
1238 void
1239free_typebuf()
1240{
1241 vim_free(typebuf.tb_buf);
1242 vim_free(typebuf.tb_noremap);
1243}
1244
1245/*
1246 * When doing ":so! file", the current typeahead needs to be saved, and
1247 * restored when "file" has been read completely.
1248 */
1249static typebuf_T saved_typebuf[NSCRIPT];
1250
1251 int
1252save_typebuf()
1253{
1254 init_typebuf();
1255 saved_typebuf[curscript] = typebuf;
1256 /* If out of memory: restore typebuf and close file. */
1257 if (alloc_typebuf() == FAIL)
1258 {
1259 closescript();
1260 return FAIL;
1261 }
1262 return OK;
1263}
1264
1265#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1266
1267/*
1268 * Save all three kinds of typeahead, so that the user must type at a prompt.
1269 */
1270 void
1271save_typeahead(tp)
1272 tasave_T *tp;
1273{
1274 tp->save_typebuf = typebuf;
1275 tp->typebuf_valid = (alloc_typebuf() == OK);
1276 if (!tp->typebuf_valid)
1277 typebuf = tp->save_typebuf;
1278
1279 tp->save_stuffbuff = stuffbuff;
1280 stuffbuff.bh_first.b_next = NULL;
1281# ifdef USE_INPUT_BUF
1282 tp->save_inputbuf = get_input_buf();
1283# endif
1284}
1285
1286/*
1287 * Restore the typeahead to what it was before calling save_typeahead().
1288 * The allocated memory is freed, can only be called once!
1289 */
1290 void
1291restore_typeahead(tp)
1292 tasave_T *tp;
1293{
1294 if (tp->typebuf_valid)
1295 {
1296 free_typebuf();
1297 typebuf = tp->save_typebuf;
1298 }
1299
1300 free_buff(&stuffbuff);
1301 stuffbuff = tp->save_stuffbuff;
1302# ifdef USE_INPUT_BUF
1303 set_input_buf(tp->save_inputbuf);
1304# endif
1305}
1306#endif
1307
1308/*
1309 * Open a new script file for the ":source!" command.
1310 */
1311 void
1312openscript(name, directly)
1313 char_u *name;
1314 int directly; /* when TRUE execute directly */
1315{
1316 if (curscript + 1 == NSCRIPT)
1317 {
1318 EMSG(_(e_nesting));
1319 return;
1320 }
1321
1322 if (scriptin[curscript] != NULL) /* already reading script */
1323 ++curscript;
1324 /* use NameBuff for expanded name */
1325 expand_env(name, NameBuff, MAXPATHL);
1326 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1327 {
1328 EMSG2(_(e_notopen), name);
1329 if (curscript)
1330 --curscript;
1331 return;
1332 }
1333 if (save_typebuf() == FAIL)
1334 return;
1335
1336 /*
1337 * Execute the commands from the file right now when using ":source!"
1338 * after ":global" or ":argdo" or in a loop. Also when another command
1339 * follows. This means the display won't be updated. Don't do this
1340 * always, "make test" would fail.
1341 */
1342 if (directly)
1343 {
1344 oparg_T oa;
1345 int oldcurscript;
1346 int save_State = State;
1347 int save_restart_edit = restart_edit;
1348 int save_insertmode = p_im;
1349 int save_finish_op = finish_op;
1350 int save_msg_scroll = msg_scroll;
1351
1352 State = NORMAL;
1353 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1354 restart_edit = 0; /* don't go to Insert mode */
1355 p_im = FALSE; /* don't use 'insertmode' */
1356 clear_oparg(&oa);
1357 finish_op = FALSE;
1358
1359 oldcurscript = curscript;
1360 do
1361 {
1362 update_topline_cursor(); /* update cursor position and topline */
1363 normal_cmd(&oa, FALSE); /* execute one command */
1364 vpeekc(); /* check for end of file */
1365 }
1366 while (scriptin[oldcurscript] != NULL);
1367
1368 State = save_State;
1369 msg_scroll = save_msg_scroll;
1370 restart_edit = save_restart_edit;
1371 p_im = save_insertmode;
1372 finish_op = save_finish_op;
1373 }
1374}
1375
1376/*
1377 * Close the currently active input script.
1378 */
1379 static void
1380closescript()
1381{
1382 free_typebuf();
1383 typebuf = saved_typebuf[curscript];
1384
1385 fclose(scriptin[curscript]);
1386 scriptin[curscript] = NULL;
1387 if (curscript > 0)
1388 --curscript;
1389}
1390
Bram Moolenaarea408852005-06-25 22:49:46 +00001391#if defined(EXITFREE) || defined(PROTO)
1392 void
1393close_all_scripts()
1394{
1395 while (scriptin[0] != NULL)
1396 closescript();
1397}
1398#endif
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1401/*
1402 * Return TRUE when reading keys from a script file.
1403 */
1404 int
1405using_script()
1406{
1407 return scriptin[curscript] != NULL;
1408}
1409#endif
1410
1411/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001412 * This function is called just before doing a blocking wait. Thus after
1413 * waiting 'updatetime' for a character to arrive.
1414 */
1415 void
1416before_blocking()
1417{
1418 updatescript(0);
1419#ifdef FEAT_EVAL
1420 garbage_collect();
1421#endif
1422}
1423
1424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 * updatescipt() is called when a character can be written into the script file
1426 * or when we have waited some time for a character (c == 0)
1427 *
1428 * All the changed memfiles are synced if c == 0 or when the number of typed
1429 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1430 */
1431 void
1432updatescript(c)
1433 int c;
1434{
1435 static int count = 0;
1436
1437 if (c && scriptout)
1438 putc(c, scriptout);
1439 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1440 {
1441 ml_sync_all(c == 0, TRUE);
1442 count = 0;
1443 }
1444}
1445
1446#define KL_PART_KEY -1 /* keylen value for incomplete key-code */
1447#define KL_PART_MAP -2 /* keylen value for incomplete mapping */
1448
1449static int old_char = -1; /* character put back by vungetc() */
1450static int old_mod_mask; /* mod_mask for ungotten character */
1451
1452/*
1453 * Get the next input character.
1454 * Can return a special key or a multi-byte character.
1455 * Can return NUL when called recursively, use safe_vgetc() if that's not
1456 * wanted.
1457 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1458 * Collects the bytes of a multibyte character into the whole character.
1459 * Returns the modifers in the global "mod_mask".
1460 */
1461 int
1462vgetc()
1463{
1464 int c, c2;
1465#ifdef FEAT_MBYTE
1466 int n;
1467 char_u buf[MB_MAXBYTES];
1468 int i;
1469#endif
1470
1471 /*
1472 * If a character was put back with vungetc, it was already processed.
1473 * Return it directly.
1474 */
1475 if (old_char != -1)
1476 {
1477 c = old_char;
1478 old_char = -1;
1479 mod_mask = old_mod_mask;
1480 return c;
1481 }
1482
1483 mod_mask = 0x0;
1484 last_recorded_len = 0;
1485 for (;;) /* this is done twice if there are modifiers */
1486 {
1487 if (mod_mask) /* no mapping after modifier has been read */
1488 {
1489 ++no_mapping;
1490 ++allow_keys;
1491 }
1492 c = vgetorpeek(TRUE);
1493 if (mod_mask)
1494 {
1495 --no_mapping;
1496 --allow_keys;
1497 }
1498
1499 /* Get two extra bytes for special keys */
1500 if (c == K_SPECIAL
1501#ifdef FEAT_GUI
1502 || c == CSI
1503#endif
1504 )
1505 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001506 int save_allow_keys = allow_keys;
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 ++no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001509 allow_keys = 0; /* make sure BS is not found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1511 c = vgetorpeek(TRUE);
1512 --no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001513 allow_keys = save_allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (c2 == KS_MODIFIER)
1515 {
1516 mod_mask = c;
1517 continue;
1518 }
1519 c = TO_SPECIAL(c2, c);
1520
1521#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1522 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1523 * know that a menu was torn off */
1524 if (c == K_TEAROFF)
1525 {
1526 char_u name[200];
1527 int i;
1528
1529 /* get menu path, it ends with a <CR> */
1530 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1531 {
1532 name[i] = c;
1533 if (i < 199)
1534 ++i;
1535 }
1536 name[i] = NUL;
1537 gui_make_tearoff(name);
1538 continue;
1539 }
1540#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001541#if defined(HAVE_GTK2) && defined(FEAT_MENU)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001542 /* GTK: <F10> normally selects the menu, but it's passed until
1543 * here to allow mapping it. Intercept and invoke the GTK
1544 * behavior if it's not mapped. */
1545 if (c == K_F10 && gui.menubar != NULL)
1546 {
1547 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1548 continue;
1549 }
1550#endif
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#ifdef FEAT_GUI
1553 /* Translate K_CSI to CSI. The special key is only used to avoid
1554 * it being recognized as the start of a special key. */
1555 if (c == K_CSI)
1556 c = CSI;
1557#endif
1558 }
1559#ifdef MSDOS
1560 /*
1561 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1562 * Delete the 3 here.
1563 */
1564 else if (c == K_NUL && vpeekc() == 3)
1565 (void)vgetorpeek(TRUE);
1566#endif
1567
Bram Moolenaara88d9682005-03-25 21:45:43 +00001568 /* a keypad or special function key was not mapped, use it like
1569 * its ASCII equivalent */
1570 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
Bram Moolenaara88d9682005-03-25 21:45:43 +00001572 case K_KPLUS: c = '+'; break;
1573 case K_KMINUS: c = '-'; break;
1574 case K_KDIVIDE: c = '/'; break;
1575 case K_KMULTIPLY: c = '*'; break;
1576 case K_KENTER: c = CAR; break;
1577 case K_KPOINT:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578#ifdef WIN32
Bram Moolenaara88d9682005-03-25 21:45:43 +00001579 /* Can be either '.' or a ',', *
1580 * depending on the type of keypad. */
1581 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001582#else
Bram Moolenaara88d9682005-03-25 21:45:43 +00001583 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001584#endif
Bram Moolenaara88d9682005-03-25 21:45:43 +00001585 case K_K0: c = '0'; break;
1586 case K_K1: c = '1'; break;
1587 case K_K2: c = '2'; break;
1588 case K_K3: c = '3'; break;
1589 case K_K4: c = '4'; break;
1590 case K_K5: c = '5'; break;
1591 case K_K6: c = '6'; break;
1592 case K_K7: c = '7'; break;
1593 case K_K8: c = '8'; break;
1594 case K_K9: c = '9'; break;
1595
1596 case K_XHOME:
1597 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1598 {
1599 c = K_S_HOME;
1600 mod_mask = 0;
1601 }
1602 else if (mod_mask == MOD_MASK_CTRL)
1603 {
1604 c = K_C_HOME;
1605 mod_mask = 0;
1606 }
1607 else
1608 c = K_HOME;
1609 break;
1610 case K_XEND:
1611 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1612 {
1613 c = K_S_END;
1614 mod_mask = 0;
1615 }
1616 else if (mod_mask == MOD_MASK_CTRL)
1617 {
1618 c = K_C_END;
1619 mod_mask = 0;
1620 }
1621 else
1622 c = K_END;
1623 break;
1624
1625 case K_XUP: c = K_UP; break;
1626 case K_XDOWN: c = K_DOWN; break;
1627 case K_XLEFT: c = K_LEFT; break;
1628 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630
1631#ifdef FEAT_MBYTE
1632 /* For a multi-byte character get all the bytes and return the
1633 * converted character.
1634 * Note: This will loop until enough bytes are received!
1635 */
1636 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1637 {
1638 ++no_mapping;
1639 buf[0] = c;
1640 for (i = 1; i < n; ++i)
1641 {
1642 buf[i] = vgetorpeek(TRUE);
1643 if (buf[i] == K_SPECIAL
1644#ifdef FEAT_GUI
1645 || buf[i] == CSI
1646#endif
1647 )
1648 {
1649 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1650 * which represents a K_SPECIAL (0x80),
1651 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1652 * a CSI (0x9B),
1653 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1654 c = vgetorpeek(TRUE);
1655 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1656 buf[i] = CSI;
1657 }
1658 }
1659 --no_mapping;
1660 c = (*mb_ptr2char)(buf);
1661 }
1662#endif
1663
1664 return c;
1665 }
1666}
1667
1668/*
1669 * Like vgetc(), but never return a NUL when called recursively, get a key
1670 * directly from the user (ignoring typeahead).
1671 */
1672 int
1673safe_vgetc()
1674{
1675 int c;
1676
1677 c = vgetc();
1678 if (c == NUL)
1679 c = get_keystroke();
1680 return c;
1681}
1682
1683/*
1684 * Check if a character is available, such that vgetc() will not block.
1685 * If the next character is a special character or multi-byte, the returned
1686 * character is not valid!.
1687 */
1688 int
1689vpeekc()
1690{
1691 if (old_char != -1)
1692 return old_char;
1693 return vgetorpeek(FALSE);
1694}
1695
1696#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1697/*
1698 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1699 * codes.
1700 */
1701 int
1702vpeekc_nomap()
1703{
1704 int c;
1705
1706 ++no_mapping;
1707 ++allow_keys;
1708 c = vpeekc();
1709 --no_mapping;
1710 --allow_keys;
1711 return c;
1712}
1713#endif
1714
1715#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1716/*
1717 * Check if any character is available, also half an escape sequence.
1718 * Trick: when no typeahead found, but there is something in the typeahead
1719 * buffer, it must be an ESC that is recognized as the start of a key code.
1720 */
1721 int
1722vpeekc_any()
1723{
1724 int c;
1725
1726 c = vpeekc();
1727 if (c == NUL && typebuf.tb_len > 0)
1728 c = ESC;
1729 return c;
1730}
1731#endif
1732
1733/*
1734 * Call vpeekc() without causing anything to be mapped.
1735 * Return TRUE if a character is available, FALSE otherwise.
1736 */
1737 int
1738char_avail()
1739{
1740 int retval;
1741
1742 ++no_mapping;
1743 retval = vpeekc();
1744 --no_mapping;
1745 return (retval != NUL);
1746}
1747
1748 void
1749vungetc(c) /* unget one character (can only be done once!) */
1750 int c;
1751{
1752 old_char = c;
1753 old_mod_mask = mod_mask;
1754}
1755
1756/*
1757 * get a character:
1758 * 1. from the stuffbuffer
1759 * This is used for abbreviated commands like "D" -> "d$".
1760 * Also used to redo a command for ".".
1761 * 2. from the typeahead buffer
1762 * Stores text obtained previously but not used yet.
1763 * Also stores the result of mappings.
1764 * Also used for the ":normal" command.
1765 * 3. from the user
1766 * This may do a blocking wait if "advance" is TRUE.
1767 *
1768 * if "advance" is TRUE (vgetc()):
1769 * really get the character.
1770 * KeyTyped is set to TRUE in the case the user typed the key.
1771 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1772 * if "advance" is FALSE (vpeekc()):
1773 * just look whether there is a character available.
1774 *
1775 * When "no_mapping" is zero, checks for mappings in the current mode.
1776 * Only returns one byte (of a multi-byte character).
1777 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1778 */
1779 static int
1780vgetorpeek(advance)
1781 int advance;
1782{
1783 int c, c1;
1784 int keylen;
1785 char_u *s;
1786 mapblock_T *mp;
1787#ifdef FEAT_LOCALMAP
1788 mapblock_T *mp2;
1789#endif
1790 mapblock_T *mp_match;
1791 int mp_match_len = 0;
1792 int timedout = FALSE; /* waited for more than 1 second
1793 for mapping to complete */
1794 int mapdepth = 0; /* check for recursive mapping */
1795 int mode_deleted = FALSE; /* set when mode has been deleted */
1796 int local_State;
1797 int mlen;
1798 int max_mlen;
1799#ifdef FEAT_CMDL_INFO
1800 int i;
1801 int new_wcol, new_wrow;
1802#endif
1803#ifdef FEAT_GUI
1804# ifdef FEAT_MENU
1805 int idx;
1806# endif
1807 int shape_changed = FALSE; /* adjusted cursor shape */
1808#endif
1809 int n;
1810#ifdef FEAT_LANGMAP
1811 int nolmaplen;
1812#endif
1813 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00001814 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
1816 /*
1817 * This function doesn't work very well when called recursively. This may
1818 * happen though, because of:
1819 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1820 * there is a character available, which calls this function. In that
1821 * case we must return NUL, to indicate no character is available.
1822 * 2. A GUI callback function writes to the screen, causing a
1823 * wait_return().
1824 * Using ":normal" can also do this, but it saves the typeahead buffer,
1825 * thus it should be OK. But don't get a key from the user then.
1826 */
1827 if (vgetc_busy
1828#ifdef FEAT_EX_EXTRA
1829 && ex_normal_busy == 0
1830#endif
1831 )
1832 return NUL;
1833
1834 local_State = get_real_state();
1835
1836 vgetc_busy = TRUE;
1837
1838 if (advance)
1839 KeyStuffed = FALSE;
1840
1841 init_typebuf();
1842 start_stuff();
1843 if (advance && typebuf.tb_maplen == 0)
1844 Exec_reg = FALSE;
1845 do
1846 {
1847/*
1848 * get a character: 1. from the stuffbuffer
1849 */
1850 if (typeahead_char != 0)
1851 {
1852 c = typeahead_char;
1853 if (advance)
1854 typeahead_char = 0;
1855 }
1856 else
1857 c = read_stuff(advance);
1858 if (c != NUL && !got_int)
1859 {
1860 if (advance)
1861 {
1862 /* KeyTyped = FALSE; When the command that stuffed something
1863 * was typed, behave like the stuffed command was typed.
1864 * needed for CTRL-W CTRl-] to open a fold, for example. */
1865 KeyStuffed = TRUE;
1866 }
1867 if (typebuf.tb_no_abbr_cnt == 0)
1868 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1869 }
1870 else
1871 {
1872 /*
1873 * Loop until we either find a matching mapped key, or we
1874 * are sure that it is not a mapped key.
1875 * If a mapped key sequence is found we go back to the start to
1876 * try re-mapping.
1877 */
1878 for (;;)
1879 {
1880 /*
1881 * ui_breakcheck() is slow, don't use it too often when
1882 * inside a mapping. But call it each time for typed
1883 * characters.
1884 */
1885 if (typebuf.tb_maplen)
1886 line_breakcheck();
1887 else
1888 ui_breakcheck(); /* check for CTRL-C */
1889 keylen = 0;
1890 if (got_int)
1891 {
1892 /* flush all input */
1893 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
1894 typebuf.tb_change_cnt);
1895 /*
1896 * If inchar() returns TRUE (script file was active) or we
1897 * are inside a mapping, get out of insert mode.
1898 * Otherwise we behave like having gotten a CTRL-C.
1899 * As a result typing CTRL-C in insert mode will
1900 * really insert a CTRL-C.
1901 */
1902 if ((c || typebuf.tb_maplen)
1903 && (State & (INSERT + CMDLINE)))
1904 c = ESC;
1905 else
1906 c = Ctrl_C;
1907 flush_buffers(TRUE); /* flush all typeahead */
1908
1909 /* Also record this character, it might be needed to
1910 * get out of Insert mode. */
1911 *typebuf.tb_buf = c;
1912 gotchars(typebuf.tb_buf, 1);
1913 cmd_silent = FALSE;
1914
1915 break;
1916 }
1917 else if (typebuf.tb_len > 0)
1918 {
1919 /*
1920 * Check for a mappable key sequence.
1921 * Walk through one maphash[] list until we find an
1922 * entry that matches.
1923 *
1924 * Don't look for mappings if:
1925 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
1926 * - maphash_valid not set: no mappings present.
1927 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
1928 * - in insert or cmdline mode and 'paste' option set
1929 * - waiting for "hit return to continue" and CR or SPACE
1930 * typed
1931 * - waiting for a char with --more--
1932 * - in Ctrl-X mode, and we get a valid char for that mode
1933 */
1934 mp = NULL;
1935 max_mlen = 0;
1936 c1 = typebuf.tb_buf[typebuf.tb_off];
1937 if (no_mapping == 0 && maphash_valid
1938 && (no_zero_mapping == 0 || c1 != '0')
1939 && (typebuf.tb_maplen == 0
1940 || (p_remap
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001941 && (typebuf.tb_noremap[typebuf.tb_off]
1942 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 && !(p_paste && (State & (INSERT + CMDLINE)))
1944 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
1945 && State != ASKMORE
1946 && State != CONFIRM
1947#ifdef FEAT_INS_EXPAND
1948 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
1949 || ((continue_status & CONT_LOCAL)
1950 && (c1 == Ctrl_N || c1 == Ctrl_P)))
1951#endif
1952 )
1953 {
1954#ifdef FEAT_LANGMAP
1955 if (c1 == K_SPECIAL)
1956 nolmaplen = 2;
1957 else
1958 {
1959 LANGMAP_ADJUST(c1, TRUE);
1960 nolmaplen = 0;
1961 }
1962#endif
1963#ifdef FEAT_LOCALMAP
1964 /* First try buffer-local mappings. */
1965 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
1966 mp2 = maphash[MAP_HASH(local_State, c1)];
1967 if (mp == NULL)
1968 {
1969 mp = mp2;
1970 mp2 = NULL;
1971 }
1972#else
1973 mp = maphash[MAP_HASH(local_State, c1)];
1974#endif
1975 /*
1976 * Loop until a partly matching mapping is found or
1977 * all (local) mappings have been checked.
1978 * The longest full match is remembered in "mp_match".
1979 * A full match is only accepted if there is no partly
1980 * match, so "aa" and "aaa" can both be mapped.
1981 */
1982 mp_match = NULL;
1983 mp_match_len = 0;
1984 for ( ; mp != NULL;
1985#ifdef FEAT_LOCALMAP
1986 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
1987#endif
1988 (mp = mp->m_next))
1989 {
1990 /*
1991 * Only consider an entry if the first character
1992 * matches and it is for the current state.
1993 * Skip ":lmap" mappings if keys were mapped.
1994 */
1995 if (mp->m_keys[0] == c1
1996 && (mp->m_mode & local_State)
1997 && ((mp->m_mode & LANGMAP) == 0
1998 || typebuf.tb_maplen == 0))
1999 {
2000#ifdef FEAT_LANGMAP
2001 int nomap = nolmaplen;
2002 int c2;
2003#endif
2004 /* find the match length of this mapping */
2005 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2006 {
2007#ifdef FEAT_LANGMAP
2008 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2009 if (nomap > 0)
2010 --nomap;
2011 else if (c2 == K_SPECIAL)
2012 nomap = 2;
2013 else
2014 LANGMAP_ADJUST(c2, TRUE);
2015 if (mp->m_keys[mlen] != c2)
2016#else
2017 if (mp->m_keys[mlen] !=
2018 typebuf.tb_buf[typebuf.tb_off + mlen])
2019#endif
2020 break;
2021 }
2022
2023#ifdef FEAT_MBYTE
2024 /* Don't allow mapping the first byte(s) of a
2025 * multi-byte char. Happens when mapping
2026 * <M-a> and then changing 'encoding'. */
2027 if (has_mbyte && MB_BYTE2LEN(c1)
2028 > (*mb_ptr2len_check)(mp->m_keys))
2029 mlen = 0;
2030#endif
2031 /*
2032 * Check an entry whether it matches.
2033 * - Full match: mlen == keylen
2034 * - Partly match: mlen == typebuf.tb_len
2035 */
2036 keylen = mp->m_keylen;
2037 if (mlen == keylen
2038 || (mlen == typebuf.tb_len
2039 && typebuf.tb_len < keylen))
2040 {
2041 /*
2042 * If only script-local mappings are
2043 * allowed, check if the mapping starts
2044 * with K_SNR.
2045 */
2046 s = typebuf.tb_noremap + typebuf.tb_off;
2047 if (*s == RM_SCRIPT
2048 && (mp->m_keys[0] != K_SPECIAL
2049 || mp->m_keys[1] != KS_EXTRA
2050 || mp->m_keys[2]
2051 != (int)KE_SNR))
2052 continue;
2053 /*
2054 * If one of the typed keys cannot be
2055 * remapped, skip the entry.
2056 */
2057 for (n = mlen; --n >= 0; )
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002058 if (*s++ & (RM_NONE|RM_ABBR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 break;
2060 if (n >= 0)
2061 continue;
2062
2063 if (keylen > typebuf.tb_len)
2064 {
2065 if (!timedout)
2066 {
2067 /* break at a partly match */
2068 keylen = KL_PART_MAP;
2069 break;
2070 }
2071 }
2072 else if (keylen > mp_match_len)
2073 {
2074 /* found a longer match */
2075 mp_match = mp;
2076 mp_match_len = keylen;
2077 }
2078 }
2079 else
2080 /* No match; may have to check for
2081 * termcode at next character. */
2082 if (max_mlen < mlen)
2083 max_mlen = mlen;
2084 }
2085 }
2086
2087 /* If no partly match found, use the longest full
2088 * match. */
2089 if (keylen != KL_PART_MAP)
2090 {
2091 mp = mp_match;
2092 keylen = mp_match_len;
2093 }
2094 }
2095
2096 /* Check for match with 'pastetoggle' */
2097 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2098 {
2099 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2100 ++mlen)
2101 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2102 + mlen])
2103 break;
2104 if (p_pt[mlen] == NUL) /* match */
2105 {
2106 /* write chars to script file(s) */
2107 if (mlen > typebuf.tb_maplen)
2108 gotchars(typebuf.tb_buf + typebuf.tb_off
2109 + typebuf.tb_maplen,
2110 mlen - typebuf.tb_maplen);
2111
2112 del_typebuf(mlen, 0); /* remove the chars */
2113 set_option_value((char_u *)"paste",
2114 (long)!p_paste, NULL, 0);
2115 if (!(State & INSERT))
2116 {
2117 msg_col = 0;
2118 msg_row = Rows - 1;
2119 msg_clr_eos(); /* clear ruler */
2120 }
2121 showmode();
2122 setcursor();
2123 continue;
2124 }
2125 /* Need more chars for partly match. */
2126 if (mlen == typebuf.tb_len)
2127 keylen = KL_PART_MAP;
2128 else if (max_mlen < mlen)
2129 /* no match, may have to check for termcode at
2130 * next character */
2131 max_mlen = mlen + 1;
2132 }
2133
2134 if ((mp == NULL || max_mlen >= mp_match_len)
2135 && keylen != KL_PART_MAP)
2136 {
2137 /*
2138 * When no matching mapping found or found a
2139 * non-matching mapping that matches at least what the
2140 * matching mapping matched:
2141 * Check if we have a terminal code, when:
2142 * mapping is allowed,
2143 * keys have not been mapped,
2144 * and not an ESC sequence, not in insert mode or
2145 * p_ek is on,
2146 * and when not timed out,
2147 */
2148 if ((no_mapping == 0 || allow_keys != 0)
2149 && (typebuf.tb_maplen == 0
2150 || (p_remap && typebuf.tb_noremap[
2151 typebuf.tb_off] == RM_YES))
2152 && !timedout)
2153 {
2154 keylen = check_termcode(max_mlen + 1, NULL, 0);
2155
2156 /*
2157 * When getting a partial match, but the last
2158 * characters were not typed, don't wait for a
2159 * typed character to complete the termcode.
2160 * This helps a lot when a ":normal" command ends
2161 * in an ESC.
2162 */
2163 if (keylen < 0
2164 && typebuf.tb_len == typebuf.tb_maplen)
2165 keylen = 0;
2166 }
2167 else
2168 keylen = 0;
2169 if (keylen == 0) /* no matching terminal code */
2170 {
2171#ifdef AMIGA /* check for window bounds report */
2172 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2173 typebuf.tb_off] & 0xff) == CSI)
2174 {
2175 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2176 s < typebuf.tb_buf + typebuf.tb_off
2177 + typebuf.tb_len
2178 && (VIM_ISDIGIT(*s) || *s == ';'
2179 || *s == ' ');
2180 ++s)
2181 ;
2182 if (*s == 'r' || *s == '|') /* found one */
2183 {
2184 del_typebuf((int)(s + 1 -
2185 (typebuf.tb_buf + typebuf.tb_off)), 0);
2186 /* get size and redraw screen */
2187 shell_resized();
2188 continue;
2189 }
2190 if (*s == NUL) /* need more characters */
2191 keylen = KL_PART_KEY;
2192 }
2193 if (keylen >= 0)
2194#endif
2195 /* When there was a matching mapping and no
2196 * termcode could be replaced after another one,
2197 * use that mapping. */
2198 if (mp == NULL)
2199 {
2200/*
2201 * get a character: 2. from the typeahead buffer
2202 */
2203 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2204 if (advance) /* remove chars from tb_buf */
2205 {
2206 cmd_silent = (typebuf.tb_silent > 0);
2207 if (typebuf.tb_maplen > 0)
2208 KeyTyped = FALSE;
2209 else
2210 {
2211 KeyTyped = TRUE;
2212 /* write char to script file(s) */
2213 gotchars(typebuf.tb_buf
2214 + typebuf.tb_off, 1);
2215 }
2216 KeyNoremap = (typebuf.tb_noremap[
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002217 typebuf.tb_off]
2218 & (RM_NONE|RM_SCRIPT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 del_typebuf(1, 0);
2220 }
2221 break; /* got character, break for loop */
2222 }
2223 }
2224 if (keylen > 0) /* full matching terminal code */
2225 {
2226#if defined(FEAT_GUI) && defined(FEAT_MENU)
2227 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2228 && typebuf.tb_buf[typebuf.tb_off + 1]
2229 == KS_MENU)
2230 {
2231 /*
2232 * Using a menu may cause a break in undo!
2233 * It's like using gotchars(), but without
2234 * recording or writing to a script file.
2235 */
2236 may_sync_undo();
2237 del_typebuf(3, 0);
2238 idx = get_menu_index(current_menu, local_State);
2239 if (idx != MENU_INDEX_INVALID)
2240 {
2241# ifdef FEAT_VISUAL
2242 /*
2243 * In Select mode, a Visual mode menu is
2244 * used. Switch to Visual mode
2245 * temporarily. Append K_SELECT to switch
2246 * back to Select mode.
2247 */
2248 if (VIsual_active && VIsual_select)
2249 {
2250 VIsual_select = FALSE;
2251 (void)ins_typebuf(K_SELECT_STRING,
2252 REMAP_NONE, 0, TRUE, FALSE);
2253 }
2254# endif
2255 ins_typebuf(current_menu->strings[idx],
2256 current_menu->noremap[idx],
2257 0, TRUE,
2258 current_menu->silent[idx]);
2259 }
2260 }
2261#endif /* FEAT_GUI */
2262 continue; /* try mapping again */
2263 }
2264
2265 /* Partial match: get some more characters. When a
2266 * matching mapping was found use that one. */
2267 if (mp == NULL || keylen < 0)
2268 keylen = KL_PART_KEY;
2269 else
2270 keylen = mp_match_len;
2271 }
2272
2273 /* complete match */
2274 if (keylen >= 0 && keylen <= typebuf.tb_len)
2275 {
2276 /* write chars to script file(s) */
2277 if (keylen > typebuf.tb_maplen)
2278 gotchars(typebuf.tb_buf + typebuf.tb_off
2279 + typebuf.tb_maplen,
2280 keylen - typebuf.tb_maplen);
2281
2282 cmd_silent = (typebuf.tb_silent > 0);
2283 del_typebuf(keylen, 0); /* remove the mapped keys */
2284
2285 /*
2286 * Put the replacement string in front of mapstr.
2287 * The depth check catches ":map x y" and ":map y x".
2288 */
2289 if (++mapdepth >= p_mmd)
2290 {
2291 EMSG(_("E223: recursive mapping"));
2292 if (State & CMDLINE)
2293 redrawcmdline();
2294 else
2295 setcursor();
2296 flush_buffers(FALSE);
2297 mapdepth = 0; /* for next one */
2298 c = -1;
2299 break;
2300 }
2301
2302#ifdef FEAT_VISUAL
2303 /*
2304 * In Select mode, a Visual mode mapping is used.
2305 * Switch to Visual mode temporarily. Append K_SELECT
2306 * to switch back to Select mode.
2307 */
2308 if (VIsual_active && VIsual_select)
2309 {
2310 VIsual_select = FALSE;
2311 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2312 0, TRUE, FALSE);
2313 }
2314#endif
2315
2316 /*
2317 * Insert the 'to' part in the typebuf.tb_buf.
2318 * If 'from' field is the same as the start of the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002319 * 'to' field, don't remap the first character (but do
2320 * allow abbreviations).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 * If m_noremap is set, don't remap the whole 'to'
2322 * part.
2323 */
2324 if (ins_typebuf(mp->m_str,
2325 mp->m_noremap != REMAP_YES
2326 ? mp->m_noremap
2327 : STRNCMP(mp->m_str, mp->m_keys,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002328 (size_t)keylen) != 0
2329 ? REMAP_YES : REMAP_SKIP,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 0, TRUE, cmd_silent || mp->m_silent) == FAIL)
2331 {
2332 c = -1;
2333 break;
2334 }
2335 continue;
2336 }
2337 }
2338
2339/*
2340 * get a character: 3. from the user - handle <Esc> in Insert mode
2341 */
2342 /*
2343 * special case: if we get an <ESC> in insert mode and there
2344 * are no more characters at once, we pretend to go out of
2345 * insert mode. This prevents the one second delay after
2346 * typing an <ESC>. If we get something after all, we may
2347 * have to redisplay the mode. That the cursor is in the wrong
2348 * place does not matter.
2349 */
2350 c = 0;
2351#ifdef FEAT_CMDL_INFO
2352 new_wcol = curwin->w_wcol;
2353 new_wrow = curwin->w_wrow;
2354#endif
2355 if ( advance
2356 && typebuf.tb_len == 1
2357 && typebuf.tb_buf[typebuf.tb_off] == ESC
2358 && !no_mapping
2359#ifdef FEAT_EX_EXTRA
2360 && ex_normal_busy == 0
2361#endif
2362 && typebuf.tb_maplen == 0
2363 && (State & INSERT)
2364 && (p_timeout || (keylen == KL_PART_KEY && p_ttimeout))
2365 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2366 + typebuf.tb_len, 3, 25L,
2367 typebuf.tb_change_cnt)) == 0)
2368 {
2369 colnr_T col = 0, vcol;
2370 char_u *ptr;
2371
2372 if (p_smd)
2373 {
2374 unshowmode(TRUE);
2375 mode_deleted = TRUE;
2376 }
2377#ifdef FEAT_GUI
2378 /* may show different cursor shape */
2379 if (gui.in_use)
2380 {
2381 int save_State;
2382
2383 save_State = State;
2384 State = NORMAL;
2385 gui_update_cursor(TRUE, FALSE);
2386 State = save_State;
2387 shape_changed = TRUE;
2388 }
2389#endif
2390 validate_cursor();
2391 old_wcol = curwin->w_wcol;
2392 old_wrow = curwin->w_wrow;
2393
2394 /* move cursor left, if possible */
2395 if (curwin->w_cursor.col != 0)
2396 {
2397 if (curwin->w_wcol > 0)
2398 {
2399 if (did_ai)
2400 {
2401 /*
2402 * We are expecting to truncate the trailing
2403 * white-space, so find the last non-white
2404 * character -- webb
2405 */
2406 col = vcol = curwin->w_wcol = 0;
2407 ptr = ml_get_curline();
2408 while (col < curwin->w_cursor.col)
2409 {
2410 if (!vim_iswhite(ptr[col]))
2411 curwin->w_wcol = vcol;
2412 vcol += lbr_chartabsize(ptr + col,
2413 (colnr_T)vcol);
2414#ifdef FEAT_MBYTE
2415 if (has_mbyte)
2416 col += (*mb_ptr2len_check)(ptr + col);
2417 else
2418#endif
2419 ++col;
2420 }
2421 curwin->w_wrow = curwin->w_cline_row
2422 + curwin->w_wcol / W_WIDTH(curwin);
2423 curwin->w_wcol %= W_WIDTH(curwin);
2424 curwin->w_wcol += curwin_col_off();
2425#ifdef FEAT_MBYTE
2426 col = 0; /* no correction needed */
2427#endif
2428 }
2429 else
2430 {
2431 --curwin->w_wcol;
2432#ifdef FEAT_MBYTE
2433 col = curwin->w_cursor.col - 1;
2434#endif
2435 }
2436 }
2437 else if (curwin->w_p_wrap && curwin->w_wrow)
2438 {
2439 --curwin->w_wrow;
2440 curwin->w_wcol = W_WIDTH(curwin) - 1;
2441#ifdef FEAT_MBYTE
2442 col = curwin->w_cursor.col - 1;
2443#endif
2444 }
2445#ifdef FEAT_MBYTE
2446 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2447 {
2448 /* Correct when the cursor is on the right halve
2449 * of a double-wide character. */
2450 ptr = ml_get_curline();
2451 col -= (*mb_head_off)(ptr, ptr + col);
2452 if ((*mb_ptr2cells)(ptr + col) > 1)
2453 --curwin->w_wcol;
2454 }
2455#endif
2456 }
2457 setcursor();
2458 out_flush();
2459#ifdef FEAT_CMDL_INFO
2460 new_wcol = curwin->w_wcol;
2461 new_wrow = curwin->w_wrow;
2462#endif
2463 curwin->w_wcol = old_wcol;
2464 curwin->w_wrow = old_wrow;
2465 }
2466 if (c < 0)
2467 continue; /* end of input script reached */
2468 typebuf.tb_len += c;
2469
2470 /* buffer full, don't map */
2471 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2472 {
2473 timedout = TRUE;
2474 continue;
2475 }
2476
2477#ifdef FEAT_EX_EXTRA
2478 if (ex_normal_busy > 0)
2479 {
2480# ifdef FEAT_CMDWIN
2481 static int tc = 0;
2482# endif
2483
2484 /* No typeahead left and inside ":normal". Must return
2485 * something to avoid getting stuck. When an incomplete
2486 * mapping is present, behave like it timed out. */
2487 if (typebuf.tb_len > 0)
2488 {
2489 timedout = TRUE;
2490 continue;
2491 }
2492 /* When 'insertmode' is set, ESC just beeps in Insert
2493 * mode. Use CTRL-L to make edit() return.
2494 * For the command line only CTRL-C always breaks it.
2495 * For the cmdline window: Alternate between ESC and
2496 * CTRL-C: ESC for most situations and CTRL-C to close the
2497 * cmdline window. */
2498 if (p_im && (State & INSERT))
2499 c = Ctrl_L;
2500 else if ((State & CMDLINE)
2501# ifdef FEAT_CMDWIN
2502 || (cmdwin_type > 0 && tc == ESC)
2503# endif
2504 )
2505 c = Ctrl_C;
2506 else
2507 c = ESC;
2508# ifdef FEAT_CMDWIN
2509 tc = c;
2510# endif
2511 break;
2512 }
2513#endif
2514
2515/*
2516 * get a character: 3. from the user - update display
2517 */
2518 /* In insert mode a screen update is skipped when characters
2519 * are still available. But when those available characters
2520 * are part of a mapping, and we are going to do a blocking
2521 * wait here. Need to update the screen to display the
2522 * changed text so far. */
2523 if ((State & INSERT) && advance && must_redraw != 0)
2524 {
2525 update_screen(0);
2526 setcursor(); /* put cursor back where it belongs */
2527 }
2528
2529 /*
2530 * If we have a partial match (and are going to wait for more
2531 * input from the user), show the partially matched characters
2532 * to the user with showcmd.
2533 */
2534#ifdef FEAT_CMDL_INFO
2535 i = 0;
2536#endif
2537 c1 = 0;
2538 if (typebuf.tb_len > 0 && advance && !exmode_active)
2539 {
2540 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2541 && State != HITRETURN)
2542 {
2543 /* this looks nice when typing a dead character map */
2544 if (State & INSERT
2545 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2546 + typebuf.tb_len - 1) == 1)
2547 {
2548 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2549 + typebuf.tb_len - 1], FALSE);
2550 setcursor(); /* put cursor back where it belongs */
2551 c1 = 1;
2552 }
2553#ifdef FEAT_CMDL_INFO
2554 /* need to use the col and row from above here */
2555 old_wcol = curwin->w_wcol;
2556 old_wrow = curwin->w_wrow;
2557 curwin->w_wcol = new_wcol;
2558 curwin->w_wrow = new_wrow;
2559 push_showcmd();
2560 if (typebuf.tb_len > SHOWCMD_COLS)
2561 i = typebuf.tb_len - SHOWCMD_COLS;
2562 while (i < typebuf.tb_len)
2563 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2564 + i++]);
2565 curwin->w_wcol = old_wcol;
2566 curwin->w_wrow = old_wrow;
2567#endif
2568 }
2569
2570 /* this looks nice when typing a dead character map */
2571 if ((State & CMDLINE)
2572#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2573 && cmdline_star == 0
2574#endif
2575 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2576 + typebuf.tb_len - 1) == 1)
2577 {
2578 putcmdline(typebuf.tb_buf[typebuf.tb_off
2579 + typebuf.tb_len - 1], FALSE);
2580 c1 = 1;
2581 }
2582 }
2583
2584/*
2585 * get a character: 3. from the user - get it
2586 */
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002587 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2589 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2590 !advance
2591 ? 0
2592 : ((typebuf.tb_len == 0
2593 || !(p_timeout || (p_ttimeout
2594 && keylen == KL_PART_KEY)))
2595 ? -1L
2596 : ((keylen == KL_PART_KEY && p_ttm >= 0)
2597 ? p_ttm
2598 : p_tm)), typebuf.tb_change_cnt);
2599
2600#ifdef FEAT_CMDL_INFO
2601 if (i != 0)
2602 pop_showcmd();
2603#endif
2604 if (c1 == 1)
2605 {
2606 if (State & INSERT)
2607 edit_unputchar();
2608 if (State & CMDLINE)
2609 unputcmdline();
2610 setcursor(); /* put cursor back where it belongs */
2611 }
2612
2613 if (c < 0)
2614 continue; /* end of input script reached */
2615 if (c == NUL) /* no character available */
2616 {
2617 if (!advance)
2618 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002619 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 timedout = TRUE;
2622 continue;
2623 }
2624 }
2625 else
2626 { /* allow mapping for just typed characters */
2627 while (typebuf.tb_buf[typebuf.tb_off
2628 + typebuf.tb_len] != NUL)
2629 typebuf.tb_noremap[typebuf.tb_off
2630 + typebuf.tb_len++] = RM_YES;
2631#ifdef USE_IM_CONTROL
2632 /* Get IM status right after getting keys, not after the
2633 * timeout for a mapping (focus may be lost by then). */
2634 vgetc_im_active = im_get_status();
2635#endif
2636 }
2637 } /* for (;;) */
2638 } /* if (!character from stuffbuf) */
2639
2640 /* if advance is FALSE don't loop on NULs */
2641 } while (c < 0 || (advance && c == NUL));
2642
2643 /*
2644 * The "INSERT" message is taken care of here:
2645 * if we return an ESC to exit insert mode, the message is deleted
2646 * if we don't return an ESC but deleted the message before, redisplay it
2647 */
2648 if (advance && p_smd && (State & INSERT))
2649 {
2650 if (c == ESC && !mode_deleted && !no_mapping)
2651 {
2652 if (typebuf.tb_len && !KeyTyped)
2653 redraw_cmdline = TRUE; /* delete mode later */
2654 else
2655 unshowmode(FALSE);
2656 }
2657 else if (c != ESC && mode_deleted)
2658 {
2659 if (typebuf.tb_len && !KeyTyped)
2660 redraw_cmdline = TRUE; /* show mode later */
2661 else
2662 showmode();
2663 }
2664 }
2665#ifdef FEAT_GUI
2666 /* may unshow different cursor shape */
2667 if (gui.in_use && shape_changed)
2668 gui_update_cursor(TRUE, FALSE);
2669#endif
2670
2671 vgetc_busy = FALSE;
2672
2673 return c;
2674}
2675
2676/*
2677 * inchar() - get one character from
2678 * 1. a scriptfile
2679 * 2. the keyboard
2680 *
2681 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2682 * NUL terminated (buffer length must be 'maxlen' + 1).
2683 * Minimum for "maxlen" is 3!!!!
2684 *
2685 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2686 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2687 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2688 * otherwise.
2689 *
2690 * If we got an interrupt all input is read until none is available.
2691 *
2692 * If wait_time == 0 there is no waiting for the char.
2693 * If wait_time == n we wait for n msec for a character to arrive.
2694 * If wait_time == -1 we wait forever for a character to arrive.
2695 *
2696 * Return the number of obtained characters.
2697 * Return -1 when end of input script reached.
2698 */
2699 int
2700inchar(buf, maxlen, wait_time, tb_change_cnt)
2701 char_u *buf;
2702 int maxlen;
2703 long wait_time; /* milli seconds */
2704 int tb_change_cnt;
2705{
2706 int len = 0; /* init for GCC */
2707 int retesc = FALSE; /* return ESC with gotint */
2708 int script_char;
2709
2710 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2711 {
2712 cursor_on();
2713 out_flush();
2714#ifdef FEAT_GUI
2715 if (gui.in_use)
2716 {
2717 gui_update_cursor(FALSE, FALSE);
2718# ifdef FEAT_MOUSESHAPE
2719 if (postponed_mouseshape)
2720 update_mouseshape(-1);
2721# endif
2722 }
2723#endif
2724 }
2725
2726 /*
2727 * Don't reset these when at the hit-return prompt, otherwise a endless
2728 * recursive loop may result (write error in swapfile, hit-return, timeout
2729 * on char wait, flush swapfile, write error....).
2730 */
2731 if (State != HITRETURN)
2732 {
2733 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2734 did_swapwrite_msg = FALSE; /* display swap file write error again */
2735 }
2736 undo_off = FALSE; /* restart undo now */
2737
2738 /*
2739 * first try script file
2740 * If interrupted: Stop reading script files.
2741 */
2742 script_char = -1;
2743 while (scriptin[curscript] != NULL && script_char < 0)
2744 {
2745 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2746 {
2747 /* Reached EOF.
2748 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2749 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2750 closescript();
2751 /*
2752 * When reading script file is interrupted, return an ESC to get
2753 * back to normal mode.
2754 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2755 */
2756 if (got_int)
2757 retesc = TRUE;
2758 else
2759 return -1;
2760 }
2761 else
2762 {
2763 buf[0] = script_char;
2764 len = 1;
2765 }
2766 }
2767
2768 if (script_char < 0) /* did not get a character from script */
2769 {
2770 /*
2771 * If we got an interrupt, skip all previously typed characters and
2772 * return TRUE if quit reading script file.
2773 * Stop reading typeahead when a single CTRL-C was read,
2774 * fill_input_buf() returns this when not able to read from stdin.
2775 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2776 * and buf may be pointing inside typebuf.tb_buf[].
2777 */
2778 if (got_int)
2779 {
2780#define DUM_LEN MAXMAPLEN * 3 + 3
2781 char_u dum[DUM_LEN + 1];
2782
2783 for (;;)
2784 {
2785 len = ui_inchar(dum, DUM_LEN, 0L, 0);
2786 if (len == 0 || (len == 1 && dum[0] == 3))
2787 break;
2788 }
2789 return retesc;
2790 }
2791
2792 /*
2793 * Always flush the output characters when getting input characters
2794 * from the user.
2795 */
2796 out_flush();
2797
2798 /*
2799 * Fill up to a third of the buffer, because each character may be
2800 * tripled below.
2801 */
2802 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
2803 }
2804
2805 if (typebuf_changed(tb_change_cnt))
2806 return 0;
2807
2808 return fix_input_buffer(buf, len, script_char >= 0);
2809}
2810
2811/*
2812 * Fix typed characters for use by vgetc() and check_termcode().
2813 * buf[] must have room to triple the number of bytes!
2814 * Returns the new length.
2815 */
2816 int
2817fix_input_buffer(buf, len, script)
2818 char_u *buf;
2819 int len;
2820 int script; /* TRUE when reading from a script */
2821{
2822 int i;
2823 char_u *p = buf;
2824
2825 /*
2826 * Two characters are special: NUL and K_SPECIAL.
2827 * When compiled With the GUI CSI is also special.
2828 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
2829 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
2830 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
2831 * Don't replace K_SPECIAL when reading a script file.
2832 */
2833 for (i = len; --i >= 0; ++p)
2834 {
2835#ifdef FEAT_GUI
2836 /* When the GUI is used any character can come after a CSI, don't
2837 * escape it. */
2838 if (gui.in_use && p[0] == CSI && i >= 2)
2839 {
2840 p += 2;
2841 i -= 2;
2842 }
2843 /* When the GUI is not used CSI needs to be escaped. */
2844 else if (!gui.in_use && p[0] == CSI)
2845 {
2846 mch_memmove(p + 3, p + 1, (size_t)i);
2847 *p++ = K_SPECIAL;
2848 *p++ = KS_EXTRA;
2849 *p = (int)KE_CSI;
2850 len += 2;
2851 }
2852 else
2853#endif
2854 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002855#ifdef FEAT_AUTOCMD
2856 /* timeout may generate K_CURSORHOLD */
2857 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
2858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859#if defined(WIN3264) && !defined(FEAT_GUI)
2860 /* Win32 console passes modifiers */
2861 && (i < 2 || p[1] != KS_MODIFIER)
2862#endif
2863 ))
2864 {
2865 mch_memmove(p + 3, p + 1, (size_t)i);
2866 p[2] = K_THIRD(p[0]);
2867 p[1] = K_SECOND(p[0]);
2868 p[0] = K_SPECIAL;
2869 p += 2;
2870 len += 2;
2871 }
2872 }
2873 *p = NUL; /* add trailing NUL */
2874 return len;
2875}
2876
2877#if defined(USE_INPUT_BUF) || defined(PROTO)
2878/*
2879 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
2880 * Normally the input buffer would be sufficient, but the server_to_input_buf()
2881 * may insert characters in the typeahead buffer while we are waiting for
2882 * input to arrive.
2883 */
2884 int
2885input_available()
2886{
2887 return (!vim_is_input_buf_empty()
2888# ifdef FEAT_CLIENTSERVER
2889 || received_from_client
2890# endif
2891 );
2892}
2893#endif
2894
2895/*
2896 * map[!] : show all key mappings
2897 * map[!] {lhs} : show key mapping for {lhs}
2898 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
2899 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
2900 * unmap[!] {lhs} : remove key mapping for {lhs}
2901 * abbr : show all abbreviations
2902 * abbr {lhs} : show abbreviations for {lhs}
2903 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
2904 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
2905 * unabbr {lhs} : remove abbreviation for {lhs}
2906 *
2907 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
2908 *
2909 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
2910 * it will be modified.
2911 *
2912 * for :map mode is NORMAL + VISUAL + OP_PENDING
2913 * for :map! mode is INSERT + CMDLINE
2914 * for :cmap mode is CMDLINE
2915 * for :imap mode is INSERT
2916 * for :lmap mode is LANGMAP
2917 * for :nmap mode is NORMAL
2918 * for :vmap mode is VISUAL
2919 * for :omap mode is OP_PENDING
2920 *
2921 * for :abbr mode is INSERT + CMDLINE
2922 * for :iabbr mode is INSERT
2923 * for :cabbr mode is CMDLINE
2924 *
2925 * Return 0 for success
2926 * 1 for invalid arguments
2927 * 2 for no match
2928 * 4 for out of mem
2929 * 5 for entry not unique
2930 */
2931 int
2932do_map(maptype, arg, mode, abbrev)
2933 int maptype;
2934 char_u *arg;
2935 int mode;
2936 int abbrev; /* not a mapping but an abbreviation */
2937{
2938 char_u *keys;
2939 mapblock_T *mp, **mpp;
2940 char_u *rhs;
2941 char_u *p;
2942 int n;
2943 int len = 0; /* init for GCC */
2944 char_u *newstr;
2945 int hasarg;
2946 int haskey;
2947 int did_it = FALSE;
2948#ifdef FEAT_LOCALMAP
2949 int did_local = FALSE;
2950#endif
2951 int round;
2952 char_u *keys_buf = NULL;
2953 char_u *arg_buf = NULL;
2954 int retval = 0;
2955 int do_backslash;
2956 int hash;
2957 int new_hash;
2958 mapblock_T **abbr_table;
2959 mapblock_T **map_table;
2960 int unique = FALSE;
2961 int silent = FALSE;
2962 int noremap;
2963
2964 keys = arg;
2965 map_table = maphash;
2966 abbr_table = &first_abbr;
2967
2968 /* For ":noremap" don't remap, otherwise do remap. */
2969 if (maptype == 2)
2970 noremap = REMAP_NONE;
2971 else
2972 noremap = REMAP_YES;
2973
2974 /* Accept <buffer>, <silent>, <script> and <unique> in any order. */
2975 for (;;)
2976 {
2977#ifdef FEAT_LOCALMAP
2978 /*
2979 * Check for "<buffer>": mapping local to buffer.
2980 */
2981 if (STRNCMP(keys, "<buffer>", 8) == 0)
2982 {
2983 keys = skipwhite(keys + 8);
2984 map_table = curbuf->b_maphash;
2985 abbr_table = &curbuf->b_first_abbr;
2986 continue;
2987 }
2988#endif
2989
2990 /*
2991 * Check for "<silent>": don't echo commands.
2992 */
2993 if (STRNCMP(keys, "<silent>", 8) == 0)
2994 {
2995 keys = skipwhite(keys + 8);
2996 silent = TRUE;
2997 continue;
2998 }
2999
3000#ifdef FEAT_EVAL
3001 /*
3002 * Check for "<script>": remap script-local mappings only
3003 */
3004 if (STRNCMP(keys, "<script>", 8) == 0)
3005 {
3006 keys = skipwhite(keys + 8);
3007 noremap = REMAP_SCRIPT;
3008 continue;
3009 }
3010#endif
3011 /*
3012 * Check for "<unique>": don't overwrite an existing mapping.
3013 */
3014 if (STRNCMP(keys, "<unique>", 8) == 0)
3015 {
3016 keys = skipwhite(keys + 8);
3017 unique = TRUE;
3018 continue;
3019 }
3020 break;
3021 }
3022
3023 validate_maphash();
3024
3025 /*
3026 * find end of keys and skip CTRL-Vs (and backslashes) in it
3027 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
3028 * with :unmap white space is included in the keys, no argument possible
3029 */
3030 p = keys;
3031 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3032 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3033 {
3034 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3035 p[1] != NUL)
3036 ++p; /* skip CTRL-V or backslash */
3037 ++p;
3038 }
3039 if (*p != NUL)
3040 *p++ = NUL;
3041 p = skipwhite(p);
3042 rhs = p;
3043 hasarg = (*rhs != NUL);
3044 haskey = (*keys != NUL);
3045
3046 /* check for :unmap without argument */
3047 if (maptype == 1 && !haskey)
3048 {
3049 retval = 1;
3050 goto theend;
3051 }
3052
3053 /*
3054 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3055 * with the appropriate two bytes. If it is a shifted special key, unshift
3056 * it too, giving another two bytes.
3057 * replace_termcodes() may move the result to allocated memory, which
3058 * needs to be freed later (*keys_buf and *arg_buf).
3059 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3060 */
3061 if (haskey)
3062 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
3063 if (hasarg)
3064 {
3065 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3066 rhs = (char_u *)"";
3067 else
3068 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE);
3069 }
3070
3071#ifdef FEAT_FKMAP
3072 /*
3073 * when in right-to-left mode and alternate keymap option set,
3074 * reverse the character flow in the rhs in Farsi.
3075 */
3076 if (p_altkeymap && curwin->w_p_rl)
3077 lrswap(rhs);
3078#endif
3079
3080 /*
3081 * check arguments and translate function keys
3082 */
3083 if (haskey)
3084 {
3085 len = (int)STRLEN(keys);
3086 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3087 {
3088 retval = 1;
3089 goto theend;
3090 }
3091
3092 if (abbrev && maptype != 1)
3093 {
3094 /*
3095 * If an abbreviation ends in a keyword character, the
3096 * rest must be all keyword-char or all non-keyword-char.
3097 * Otherwise we won't be able to find the start of it in a
3098 * vi-compatible way.
3099 */
3100#ifdef FEAT_MBYTE
3101 if (has_mbyte)
3102 {
3103 int first, last;
3104 int same = -1;
3105
3106 first = vim_iswordp(keys);
3107 last = first;
3108 p = keys + mb_ptr2len_check(keys);
3109 n = 1;
3110 while (p < keys + len)
3111 {
3112 ++n; /* nr of (multi-byte) chars */
3113 last = vim_iswordp(p); /* type of last char */
3114 if (same == -1 && last != first)
3115 same = n - 1; /* count of same char type */
3116 p += mb_ptr2len_check(p);
3117 }
3118 if (last && n > 2 && same >= 0 && same < n - 1)
3119 {
3120 retval = 1;
3121 goto theend;
3122 }
3123 }
3124 else
3125#endif
3126 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3127 for (n = 0; n < len - 2; ++n)
3128 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3129 {
3130 retval = 1;
3131 goto theend;
3132 }
3133 /* An abbrevation cannot contain white space. */
3134 for (n = 0; n < len; ++n)
3135 if (vim_iswhite(keys[n]))
3136 {
3137 retval = 1;
3138 goto theend;
3139 }
3140 }
3141 }
3142
3143 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3144 no_abbr = FALSE; /* reset flag that indicates there are
3145 no abbreviations */
3146
3147 if (!haskey || (maptype != 1 && !hasarg))
3148 msg_start();
3149
3150#ifdef FEAT_LOCALMAP
3151 /*
3152 * Check if a new local mapping wasn't already defined globally.
3153 */
3154 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3155 {
3156 /* need to loop over all global hash lists */
3157 for (hash = 0; hash < 256 && !got_int; ++hash)
3158 {
3159 if (abbrev)
3160 {
3161 if (hash != 0) /* there is only one abbreviation list */
3162 break;
3163 mp = first_abbr;
3164 }
3165 else
3166 mp = maphash[hash];
3167 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3168 {
3169 /* check entries with the same mode */
3170 if ((mp->m_mode & mode) != 0
3171 && mp->m_keylen == len
3172 && unique
3173 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3174 {
3175 if (abbrev)
3176 EMSG2(_("E224: global abbreviation already exists for %s"),
3177 mp->m_keys);
3178 else
3179 EMSG2(_("E225: global mapping already exists for %s"),
3180 mp->m_keys);
3181 retval = 5;
3182 goto theend;
3183 }
3184 }
3185 }
3186 }
3187
3188 /*
3189 * When listing global mappings, also list buffer-local ones here.
3190 */
3191 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3192 {
3193 /* need to loop over all global hash lists */
3194 for (hash = 0; hash < 256 && !got_int; ++hash)
3195 {
3196 if (abbrev)
3197 {
3198 if (hash != 0) /* there is only one abbreviation list */
3199 break;
3200 mp = curbuf->b_first_abbr;
3201 }
3202 else
3203 mp = curbuf->b_maphash[hash];
3204 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3205 {
3206 /* check entries with the same mode */
3207 if ((mp->m_mode & mode) != 0)
3208 {
3209 if (!haskey) /* show all entries */
3210 {
3211 showmap(mp, TRUE);
3212 did_local = TRUE;
3213 }
3214 else
3215 {
3216 n = mp->m_keylen;
3217 if (STRNCMP(mp->m_keys, keys,
3218 (size_t)(n < len ? n : len)) == 0)
3219 {
3220 showmap(mp, TRUE);
3221 did_local = TRUE;
3222 }
3223 }
3224 }
3225 }
3226 }
3227 }
3228#endif
3229
3230 /*
3231 * Find an entry in the maphash[] list that matches.
3232 * For :unmap we may loop two times: once to try to unmap an entry with a
3233 * matching 'from' part, a second time, if the first fails, to unmap an
3234 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3235 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3236 * "bar" because of the abbreviation.
3237 */
3238 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3239 && !did_it && !got_int; ++round)
3240 {
3241 /* need to loop over all hash lists */
3242 for (hash = 0; hash < 256 && !got_int; ++hash)
3243 {
3244 if (abbrev)
3245 {
3246 if (hash != 0) /* there is only one abbreviation list */
3247 break;
3248 mpp = abbr_table;
3249 }
3250 else
3251 mpp = &(map_table[hash]);
3252 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3253 {
3254
3255 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3256 {
3257 mpp = &(mp->m_next);
3258 continue;
3259 }
3260 if (!haskey) /* show all entries */
3261 {
3262 showmap(mp, map_table != maphash);
3263 did_it = TRUE;
3264 }
3265 else /* do we have a match? */
3266 {
3267 if (round) /* second round: Try unmap "rhs" string */
3268 {
3269 n = (int)STRLEN(mp->m_str);
3270 p = mp->m_str;
3271 }
3272 else
3273 {
3274 n = mp->m_keylen;
3275 p = mp->m_keys;
3276 }
3277 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3278 {
3279 if (maptype == 1) /* delete entry */
3280 {
3281 /* Only accept a full match. For abbreviations we
3282 * ignore trailing space when matching with the
3283 * "lhs", since an abbreviation can't have
3284 * trailing space. */
3285 if (n != len && (!abbrev || round || n > len
3286 || *skipwhite(keys + n) != NUL))
3287 {
3288 mpp = &(mp->m_next);
3289 continue;
3290 }
3291 /*
3292 * We reset the indicated mode bits. If nothing is
3293 * left the entry is deleted below.
3294 */
3295 mp->m_mode &= ~mode;
3296 did_it = TRUE; /* remember we did something */
3297 }
3298 else if (!hasarg) /* show matching entry */
3299 {
3300 showmap(mp, map_table != maphash);
3301 did_it = TRUE;
3302 }
3303 else if (n != len) /* new entry is ambigious */
3304 {
3305 mpp = &(mp->m_next);
3306 continue;
3307 }
3308 else if (unique)
3309 {
3310 if (abbrev)
3311 EMSG2(_("E226: abbreviation already exists for %s"),
3312 p);
3313 else
3314 EMSG2(_("E227: mapping already exists for %s"), p);
3315 retval = 5;
3316 goto theend;
3317 }
3318 else /* new rhs for existing entry */
3319 {
3320 mp->m_mode &= ~mode; /* remove mode bits */
3321 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3322 {
3323 newstr = vim_strsave(rhs);
3324 if (newstr == NULL)
3325 {
3326 retval = 4; /* no mem */
3327 goto theend;
3328 }
3329 vim_free(mp->m_str);
3330 mp->m_str = newstr;
3331 mp->m_noremap = noremap;
3332 mp->m_silent = silent;
3333 mp->m_mode = mode;
3334 did_it = TRUE;
3335 }
3336 }
3337 if (mp->m_mode == 0) /* entry can be deleted */
3338 {
3339 map_free(mpp);
3340 continue; /* continue with *mpp */
3341 }
3342
3343 /*
3344 * May need to put this entry into another hash list.
3345 */
3346 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3347 if (!abbrev && new_hash != hash)
3348 {
3349 *mpp = mp->m_next;
3350 mp->m_next = map_table[new_hash];
3351 map_table[new_hash] = mp;
3352
3353 continue; /* continue with *mpp */
3354 }
3355 }
3356 }
3357 mpp = &(mp->m_next);
3358 }
3359 }
3360 }
3361
3362 if (maptype == 1) /* delete entry */
3363 {
3364 if (!did_it)
3365 retval = 2; /* no match */
3366 goto theend;
3367 }
3368
3369 if (!haskey || !hasarg) /* print entries */
3370 {
3371 if (!did_it
3372#ifdef FEAT_LOCALMAP
3373 && !did_local
3374#endif
3375 )
3376 {
3377 if (abbrev)
3378 MSG(_("No abbreviation found"));
3379 else
3380 MSG(_("No mapping found"));
3381 }
3382 goto theend; /* listing finished */
3383 }
3384
3385 if (did_it) /* have added the new entry already */
3386 goto theend;
3387
3388 /*
3389 * Get here when adding a new entry to the maphash[] list or abbrlist.
3390 */
3391 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3392 if (mp == NULL)
3393 {
3394 retval = 4; /* no mem */
3395 goto theend;
3396 }
3397
3398 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3399 if (*keys == Ctrl_C)
3400 mapped_ctrl_c = TRUE;
3401
3402 mp->m_keys = vim_strsave(keys);
3403 mp->m_str = vim_strsave(rhs);
3404 if (mp->m_keys == NULL || mp->m_str == NULL)
3405 {
3406 vim_free(mp->m_keys);
3407 vim_free(mp->m_str);
3408 vim_free(mp);
3409 retval = 4; /* no mem */
3410 goto theend;
3411 }
3412 mp->m_keylen = (int)STRLEN(mp->m_keys);
3413 mp->m_noremap = noremap;
3414 mp->m_silent = silent;
3415 mp->m_mode = mode;
3416
3417 /* add the new entry in front of the abbrlist or maphash[] list */
3418 if (abbrev)
3419 {
3420 mp->m_next = *abbr_table;
3421 *abbr_table = mp;
3422 }
3423 else
3424 {
3425 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3426 mp->m_next = map_table[n];
3427 map_table[n] = mp;
3428 }
3429
3430theend:
3431 vim_free(keys_buf);
3432 vim_free(arg_buf);
3433 return retval;
3434}
3435
3436/*
3437 * Delete one entry from the abbrlist or maphash[].
3438 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3439 */
3440 static void
3441map_free(mpp)
3442 mapblock_T **mpp;
3443{
3444 mapblock_T *mp;
3445
3446 mp = *mpp;
3447 vim_free(mp->m_keys);
3448 vim_free(mp->m_str);
3449 *mpp = mp->m_next;
3450 vim_free(mp);
3451}
3452
3453/*
3454 * Initialize maphash[] for first use.
3455 */
3456 static void
3457validate_maphash()
3458{
3459 if (!maphash_valid)
3460 {
3461 vim_memset(maphash, 0, sizeof(maphash));
3462 maphash_valid = TRUE;
3463 }
3464}
3465
3466/*
3467 * Get the mapping mode from the command name.
3468 */
3469 int
3470get_map_mode(cmdp, forceit)
3471 char_u **cmdp;
3472 int forceit;
3473{
3474 char_u *p;
3475 int modec;
3476 int mode;
3477
3478 p = *cmdp;
3479 modec = *p++;
3480 if (modec == 'i')
3481 mode = INSERT; /* :imap */
3482 else if (modec == 'l')
3483 mode = LANGMAP; /* :lmap */
3484 else if (modec == 'c')
3485 mode = CMDLINE; /* :cmap */
3486 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3487 mode = NORMAL; /* :nmap */
3488 else if (modec == 'v')
3489 mode = VISUAL; /* :vmap */
3490 else if (modec == 'o')
3491 mode = OP_PENDING; /* :omap */
3492 else
3493 {
3494 --p;
3495 if (forceit)
3496 mode = INSERT + CMDLINE; /* :map ! */
3497 else
3498 mode = VISUAL + NORMAL + OP_PENDING;/* :map */
3499 }
3500
3501 *cmdp = p;
3502 return mode;
3503}
3504
3505/*
3506 * Clear all mappings or abbreviations.
3507 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3508 */
3509/*ARGSUSED*/
3510 void
3511map_clear(cmdp, arg, forceit, abbr)
3512 char_u *cmdp;
3513 char_u *arg;
3514 int forceit;
3515 int abbr;
3516{
3517 int mode;
3518#ifdef FEAT_LOCALMAP
3519 int local;
3520
3521 local = (STRCMP(arg, "<buffer>") == 0);
3522 if (!local && *arg != NUL)
3523 {
3524 EMSG(_(e_invarg));
3525 return;
3526 }
3527#endif
3528
3529 mode = get_map_mode(&cmdp, forceit);
3530 map_clear_int(curbuf, mode,
3531#ifdef FEAT_LOCALMAP
3532 local,
3533#else
3534 FALSE,
3535#endif
3536 abbr);
3537}
3538
3539/*
3540 * Clear all mappings in "mode".
3541 */
3542/*ARGSUSED*/
3543 void
3544map_clear_int(buf, mode, local, abbr)
3545 buf_T *buf; /* buffer for local mappings */
3546 int mode; /* mode in which to delete */
3547 int local; /* TRUE for buffer-local mappings */
3548 int abbr; /* TRUE for abbreviations */
3549{
3550 mapblock_T *mp, **mpp;
3551 int hash;
3552 int new_hash;
3553
3554 validate_maphash();
3555
3556 for (hash = 0; hash < 256; ++hash)
3557 {
3558 if (abbr)
3559 {
3560 if (hash) /* there is only one abbrlist */
3561 break;
3562#ifdef FEAT_LOCALMAP
3563 if (local)
3564 mpp = &buf->b_first_abbr;
3565 else
3566#endif
3567 mpp = &first_abbr;
3568 }
3569 else
3570 {
3571#ifdef FEAT_LOCALMAP
3572 if (local)
3573 mpp = &buf->b_maphash[hash];
3574 else
3575#endif
3576 mpp = &maphash[hash];
3577 }
3578 while (*mpp != NULL)
3579 {
3580 mp = *mpp;
3581 if (mp->m_mode & mode)
3582 {
3583 mp->m_mode &= ~mode;
3584 if (mp->m_mode == 0) /* entry can be deleted */
3585 {
3586 map_free(mpp);
3587 continue;
3588 }
3589 /*
3590 * May need to put this entry into another hash list.
3591 */
3592 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3593 if (!abbr && new_hash != hash)
3594 {
3595 *mpp = mp->m_next;
3596#ifdef FEAT_LOCALMAP
3597 if (local)
3598 {
3599 mp->m_next = buf->b_maphash[new_hash];
3600 buf->b_maphash[new_hash] = mp;
3601 }
3602 else
3603#endif
3604 {
3605 mp->m_next = maphash[new_hash];
3606 maphash[new_hash] = mp;
3607 }
3608 continue; /* continue with *mpp */
3609 }
3610 }
3611 mpp = &(mp->m_next);
3612 }
3613 }
3614}
3615
3616 static void
3617showmap(mp, local)
3618 mapblock_T *mp;
3619 int local; /* TRUE for buffer-local map */
3620{
3621 int len = 1;
3622
3623 if (msg_didout || msg_silent != 0)
3624 msg_putchar('\n');
3625 if ((mp->m_mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3626 msg_putchar('!'); /* :map! */
3627 else if (mp->m_mode & INSERT)
3628 msg_putchar('i'); /* :imap */
3629 else if (mp->m_mode & LANGMAP)
3630 msg_putchar('l'); /* :lmap */
3631 else if (mp->m_mode & CMDLINE)
3632 msg_putchar('c'); /* :cmap */
3633 else if ((mp->m_mode & (NORMAL + VISUAL + OP_PENDING))
3634 == NORMAL + VISUAL + OP_PENDING)
3635 msg_putchar(' '); /* :map */
3636 else
3637 {
3638 len = 0;
3639 if (mp->m_mode & NORMAL)
3640 {
3641 msg_putchar('n'); /* :nmap */
3642 ++len;
3643 }
3644 if (mp->m_mode & OP_PENDING)
3645 {
3646 msg_putchar('o'); /* :omap */
3647 ++len;
3648 }
3649 if (mp->m_mode & VISUAL)
3650 {
3651 msg_putchar('v'); /* :vmap */
3652 ++len;
3653 }
3654 }
3655 while (++len <= 3)
3656 msg_putchar(' ');
3657
3658 /* Get length of what we write */
3659 len = msg_outtrans_special(mp->m_keys, TRUE);
3660 do
3661 {
3662 msg_putchar(' '); /* padd with blanks */
3663 ++len;
3664 } while (len < 12);
3665
3666 if (mp->m_noremap == REMAP_NONE)
3667 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3668 else if (mp->m_noremap == REMAP_SCRIPT)
3669 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3670 else
3671 msg_putchar(' ');
3672
3673 if (local)
3674 msg_putchar('@');
3675 else
3676 msg_putchar(' ');
3677
3678 /* Use FALSE below if we only want things like <Up> to show up as such on
3679 * the rhs, and not M-x etc, TRUE gets both -- webb
3680 */
3681 if (*mp->m_str == NUL)
3682 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3683 else
3684 msg_outtrans_special(mp->m_str, FALSE);
3685 out_flush(); /* show one line at a time */
3686}
3687
3688#if defined(FEAT_EVAL) || defined(PROTO)
3689/*
3690 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3691 * Recognize termcap codes in "str".
3692 * Also checks mappings local to the current buffer.
3693 */
3694 int
3695map_to_exists(str, modechars)
3696 char_u *str;
3697 char_u *modechars;
3698{
3699 int mode = 0;
3700 char_u *rhs;
3701 char_u *buf;
3702 int retval;
3703
3704 rhs = replace_termcodes(str, &buf, FALSE, TRUE);
3705
3706 if (vim_strchr(modechars, 'n') != NULL)
3707 mode |= NORMAL;
3708 if (vim_strchr(modechars, 'v') != NULL)
3709 mode |= VISUAL;
3710 if (vim_strchr(modechars, 'o') != NULL)
3711 mode |= OP_PENDING;
3712 if (vim_strchr(modechars, 'i') != NULL)
3713 mode |= INSERT;
3714 if (vim_strchr(modechars, 'l') != NULL)
3715 mode |= LANGMAP;
3716 if (vim_strchr(modechars, 'c') != NULL)
3717 mode |= CMDLINE;
3718
3719 retval = map_to_exists_mode(rhs, mode);
3720 vim_free(buf);
3721
3722 return retval;
3723}
3724#endif
3725
3726/*
3727 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
3728 * Also checks mappings local to the current buffer.
3729 */
3730 int
3731map_to_exists_mode(rhs, mode)
3732 char_u *rhs;
3733 int mode;
3734{
3735 mapblock_T *mp;
3736 int hash;
3737# ifdef FEAT_LOCALMAP
3738 int expand_buffer = FALSE;
3739
3740 validate_maphash();
3741
3742 /* Do it twice: once for global maps and once for local maps. */
3743 for (;;)
3744 {
3745# endif
3746 for (hash = 0; hash < 256; ++hash)
3747 {
3748# ifdef FEAT_LOCALMAP
3749 if (expand_buffer)
3750 mp = curbuf->b_maphash[hash];
3751 else
3752# endif
3753 mp = maphash[hash];
3754 for (; mp; mp = mp->m_next)
3755 {
3756 if ((mp->m_mode & mode)
3757 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
3758 return TRUE;
3759 }
3760 }
3761# ifdef FEAT_LOCALMAP
3762 if (expand_buffer)
3763 break;
3764 expand_buffer = TRUE;
3765 }
3766# endif
3767
3768 return FALSE;
3769}
3770
3771#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3772/*
3773 * Used below when expanding mapping/abbreviation names.
3774 */
3775static int expand_mapmodes = 0;
3776static int expand_isabbrev = 0;
3777#ifdef FEAT_LOCALMAP
3778static int expand_buffer = FALSE;
3779#endif
3780
3781/*
3782 * Work out what to complete when doing command line completion of mapping
3783 * or abbreviation names.
3784 */
3785 char_u *
3786set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
3787 expand_T *xp;
3788 char_u *cmd;
3789 char_u *arg;
3790 int forceit; /* TRUE if '!' given */
3791 int isabbrev; /* TRUE if abbreviation */
3792 int isunmap; /* TRUE if unmap/unabbrev command */
3793 cmdidx_T cmdidx;
3794{
3795 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
3796 xp->xp_context = EXPAND_NOTHING;
3797 else
3798 {
3799 if (isunmap)
3800 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
3801 else
3802 {
3803 expand_mapmodes = INSERT + CMDLINE;
3804 if (!isabbrev)
3805 expand_mapmodes += VISUAL + NORMAL + OP_PENDING;
3806 }
3807 expand_isabbrev = isabbrev;
3808 xp->xp_context = EXPAND_MAPPINGS;
3809#ifdef FEAT_LOCALMAP
3810 expand_buffer = FALSE;
3811#endif
3812 for (;;)
3813 {
3814#ifdef FEAT_LOCALMAP
3815 if (STRNCMP(arg, "<buffer>", 8) == 0)
3816 {
3817 expand_buffer = TRUE;
3818 arg = skipwhite(arg + 8);
3819 continue;
3820 }
3821#endif
3822 if (STRNCMP(arg, "<unique>", 8) == 0)
3823 {
3824 arg = skipwhite(arg + 8);
3825 continue;
3826 }
3827 if (STRNCMP(arg, "<silent>", 8) == 0)
3828 {
3829 arg = skipwhite(arg + 8);
3830 continue;
3831 }
3832 if (STRNCMP(arg, "<script>", 8) == 0)
3833 {
3834 arg = skipwhite(arg + 8);
3835 continue;
3836 }
3837 break;
3838 }
3839 xp->xp_pattern = arg;
3840 }
3841
3842 return NULL;
3843}
3844
3845/*
3846 * Find all mapping/abbreviation names that match regexp 'prog'.
3847 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
3848 * Return OK if matches found, FAIL otherwise.
3849 */
3850 int
3851ExpandMappings(regmatch, num_file, file)
3852 regmatch_T *regmatch;
3853 int *num_file;
3854 char_u ***file;
3855{
3856 mapblock_T *mp;
3857 int hash;
3858 int count;
3859 int round;
3860 char_u *p;
3861 int i;
3862
3863 validate_maphash();
3864
3865 *num_file = 0; /* return values in case of FAIL */
3866 *file = NULL;
3867
3868 /*
3869 * round == 1: Count the matches.
3870 * round == 2: Build the array to keep the matches.
3871 */
3872 for (round = 1; round <= 2; ++round)
3873 {
3874 count = 0;
3875
3876 for (i = 0; i < 4; ++i)
3877 {
3878 if (i == 0)
3879 p = (char_u *)"<silent>";
3880 else if (i == 1)
3881 p = (char_u *)"<unique>";
3882#ifdef FEAT_EVAL
3883 else if (i == 2)
3884 p = (char_u *)"<script>";
3885#endif
3886#ifdef FEAT_LOCALMAP
3887 else if (i == 3 && !expand_buffer)
3888 p = (char_u *)"<buffer>";
3889#endif
3890 else
3891 continue;
3892
3893 if (vim_regexec(regmatch, p, (colnr_T)0))
3894 {
3895 if (round == 1)
3896 ++count;
3897 else
3898 (*file)[count++] = vim_strsave(p);
3899 }
3900 }
3901
3902 for (hash = 0; hash < 256; ++hash)
3903 {
3904 if (expand_isabbrev)
3905 {
3906 if (hash) /* only one abbrev list */
3907 break; /* for (hash) */
3908 mp = first_abbr;
3909 }
3910#ifdef FEAT_LOCALMAP
3911 else if (expand_buffer)
3912 mp = curbuf->b_maphash[hash];
3913#endif
3914 else
3915 mp = maphash[hash];
3916 for (; mp; mp = mp->m_next)
3917 {
3918 if (mp->m_mode & expand_mapmodes)
3919 {
3920 p = translate_mapping(mp->m_keys, TRUE);
3921 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
3922 {
3923 if (round == 1)
3924 ++count;
3925 else
3926 {
3927 (*file)[count++] = p;
3928 p = NULL;
3929 }
3930 }
3931 vim_free(p);
3932 }
3933 } /* for (mp) */
3934 } /* for (hash) */
3935
3936 if (count == 0) /* no match found */
3937 break; /* for (round) */
3938
3939 if (round == 1)
3940 {
3941 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
3942 if (*file == NULL)
3943 return FAIL;
3944 }
3945 } /* for (round) */
3946
3947 /* Sort the matches */
3948 sort_strings(*file, count);
3949
3950 /* Remove multiple entries */
3951 {
3952 char_u **ptr1 = *file;
3953 char_u **ptr2 = ptr1 + 1;
3954 char_u **ptr3 = ptr1 + count;
3955
3956 while (ptr2 < ptr3)
3957 {
3958 if (STRCMP(*ptr1, *ptr2))
3959 *++ptr1 = *ptr2++;
3960 else
3961 {
3962 vim_free(*ptr2++);
3963 count--;
3964 }
3965 }
3966 }
3967
3968 *num_file = count;
3969 return (count == 0 ? FAIL : OK);
3970}
3971#endif /* FEAT_CMDL_COMPL */
3972
3973/*
3974 * Check for an abbreviation.
3975 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
3976 * "c" is the character typed before check_abbr was called. It may have
3977 * ABBR_OFF added to avoid prepending a CTRL-V to it.
3978 *
3979 * Historic vi practice: The last character of an abbreviation must be an id
3980 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
3981 * characters or all non-id characters. This allows for abbr. "#i" to
3982 * "#include".
3983 *
3984 * Vim addition: Allow for abbreviations that end in a non-keyword character.
3985 * Then there must be white space before the abbr.
3986 *
3987 * return TRUE if there is an abbreviation, FALSE if not
3988 */
3989 int
3990check_abbr(c, ptr, col, mincol)
3991 int c;
3992 char_u *ptr;
3993 int col;
3994 int mincol;
3995{
3996 int len;
3997 int scol; /* starting column of the abbr. */
3998 int j;
3999#ifdef FEAT_MBYTE
4000 char_u tb[MB_MAXBYTES + 4];
4001#else
4002 char_u tb[4];
4003#endif
4004 mapblock_T *mp;
4005#ifdef FEAT_LOCALMAP
4006 mapblock_T *mp2;
4007#endif
4008#ifdef FEAT_MBYTE
4009 int clen = 0; /* length in characters */
4010#endif
4011 int is_id = TRUE;
4012 int vim_abbr;
4013
4014 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4015 return FALSE;
4016 if (KeyNoremap) /* no remapping implies no abbreviation */
4017 return FALSE;
4018
4019 /*
4020 * Check for word before the cursor: If it ends in a keyword char all
4021 * chars before it must be al keyword chars or non-keyword chars, but not
4022 * white space. If it ends in a non-keyword char we accept any characters
4023 * before it except white space.
4024 */
4025 if (col == 0) /* cannot be an abbr. */
4026 return FALSE;
4027
4028#ifdef FEAT_MBYTE
4029 if (has_mbyte)
4030 {
4031 char_u *p;
4032
4033 p = mb_prevptr(ptr, ptr + col);
4034 if (!vim_iswordp(p))
4035 vim_abbr = TRUE; /* Vim added abbr. */
4036 else
4037 {
4038 vim_abbr = FALSE; /* vi compatible abbr. */
4039 if (p > ptr)
4040 is_id = vim_iswordp(mb_prevptr(ptr, p));
4041 }
4042 clen = 1;
4043 while (p > ptr + mincol)
4044 {
4045 p = mb_prevptr(ptr, p);
4046 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4047 {
4048 p += (*mb_ptr2len_check)(p);
4049 break;
4050 }
4051 ++clen;
4052 }
4053 scol = (int)(p - ptr);
4054 }
4055 else
4056#endif
4057 {
4058 if (!vim_iswordc(ptr[col - 1]))
4059 vim_abbr = TRUE; /* Vim added abbr. */
4060 else
4061 {
4062 vim_abbr = FALSE; /* vi compatible abbr. */
4063 if (col > 1)
4064 is_id = vim_iswordc(ptr[col - 2]);
4065 }
4066 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4067 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4068 ;
4069 }
4070
4071 if (scol < mincol)
4072 scol = mincol;
4073 if (scol < col) /* there is a word in front of the cursor */
4074 {
4075 ptr += scol;
4076 len = col - scol;
4077#ifdef FEAT_LOCALMAP
4078 mp = curbuf->b_first_abbr;
4079 mp2 = first_abbr;
4080 if (mp == NULL)
4081 {
4082 mp = mp2;
4083 mp2 = NULL;
4084 }
4085#else
4086 mp = first_abbr;
4087#endif
4088 for ( ; mp;
4089#ifdef FEAT_LOCALMAP
4090 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4091#endif
4092 (mp = mp->m_next))
4093 {
4094 /* find entries with right mode and keys */
4095 if ( (mp->m_mode & State)
4096 && mp->m_keylen == len
4097 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4098 break;
4099 }
4100 if (mp != NULL)
4101 {
4102 /*
4103 * Found a match:
4104 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4105 * This goes from end to start.
4106 *
4107 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4108 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4109 * Characters where IS_SPECIAL() == TRUE: key codes, need
4110 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4111 *
4112 * Character CTRL-] is treated specially - it completes the
4113 * abbreviation, but is not inserted into the input stream.
4114 */
4115 j = 0;
4116 /* special key code, split up */
4117 if (c != Ctrl_RSB)
4118 {
4119 if (IS_SPECIAL(c) || c == K_SPECIAL)
4120 {
4121 tb[j++] = K_SPECIAL;
4122 tb[j++] = K_SECOND(c);
4123 tb[j++] = K_THIRD(c);
4124 }
4125 else
4126 {
4127 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4128 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4129#ifdef FEAT_MBYTE
4130 if (has_mbyte)
4131 {
4132 /* if ABBR_OFF has been added, remove it here */
4133 if (c >= ABBR_OFF)
4134 c -= ABBR_OFF;
4135 j += (*mb_char2bytes)(c, tb + j);
4136 }
4137 else
4138#endif
4139 tb[j++] = c;
4140 }
4141 tb[j] = NUL;
4142 /* insert the last typed char */
4143 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4144 }
4145 /* insert the to string */
4146 (void)ins_typebuf(mp->m_str, mp->m_noremap, 0, TRUE, mp->m_silent);
4147 /* no abbrev. for these chars */
4148 typebuf.tb_no_abbr_cnt += (int)STRLEN(mp->m_str) + j + 1;
4149
4150 tb[0] = Ctrl_H;
4151 tb[1] = NUL;
4152#ifdef FEAT_MBYTE
4153 if (has_mbyte)
4154 len = clen; /* Delete characters instead of bytes */
4155#endif
4156 while (len-- > 0) /* delete the from string */
4157 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4158 return TRUE;
4159 }
4160 }
4161 return FALSE;
4162}
4163
4164/*
4165 * Write map commands for the current mappings to an .exrc file.
4166 * Return FAIL on error, OK otherwise.
4167 */
4168 int
4169makemap(fd, buf)
4170 FILE *fd;
4171 buf_T *buf; /* buffer for local mappings or NULL */
4172{
4173 mapblock_T *mp;
4174 char_u c1, c2;
4175 char_u *p;
4176 char *cmd;
4177 int abbr;
4178 int hash;
4179 int did_cpo = FALSE;
4180 int i;
4181
4182 validate_maphash();
4183
4184 /*
4185 * Do the loop twice: Once for mappings, once for abbreviations.
4186 * Then loop over all map hash lists.
4187 */
4188 for (abbr = 0; abbr < 2; ++abbr)
4189 for (hash = 0; hash < 256; ++hash)
4190 {
4191 if (abbr)
4192 {
4193 if (hash) /* there is only one abbr list */
4194 break;
4195#ifdef FEAT_LOCALMAP
4196 if (buf != NULL)
4197 mp = buf->b_first_abbr;
4198 else
4199#endif
4200 mp = first_abbr;
4201 }
4202 else
4203 {
4204#ifdef FEAT_LOCALMAP
4205 if (buf != NULL)
4206 mp = buf->b_maphash[hash];
4207 else
4208#endif
4209 mp = maphash[hash];
4210 }
4211
4212 for ( ; mp; mp = mp->m_next)
4213 {
4214 /* skip script-local mappings */
4215 if (mp->m_noremap == REMAP_SCRIPT)
4216 continue;
4217
4218 /* skip mappings that contain a <SNR> (script-local thing),
4219 * they probably don't work when loaded again */
4220 for (p = mp->m_str; *p != NUL; ++p)
4221 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4222 && p[2] == (int)KE_SNR)
4223 break;
4224 if (*p != NUL)
4225 continue;
4226
4227 c1 = NUL;
4228 c2 = NUL;
4229 if (abbr)
4230 cmd = "abbr";
4231 else
4232 cmd = "map";
4233 switch (mp->m_mode)
4234 {
4235 case NORMAL + VISUAL + OP_PENDING:
4236 break;
4237 case NORMAL:
4238 c1 = 'n';
4239 break;
4240 case VISUAL:
4241 c1 = 'v';
4242 break;
4243 case OP_PENDING:
4244 c1 = 'o';
4245 break;
4246 case NORMAL + VISUAL:
4247 c1 = 'n';
4248 c2 = 'v';
4249 break;
4250 case VISUAL + OP_PENDING:
4251 c1 = 'v';
4252 c2 = 'o';
4253 break;
4254 case NORMAL + OP_PENDING:
4255 c1 = 'n';
4256 c2 = 'o';
4257 break;
4258 case CMDLINE + INSERT:
4259 if (!abbr)
4260 cmd = "map!";
4261 break;
4262 case CMDLINE:
4263 c1 = 'c';
4264 break;
4265 case INSERT:
4266 c1 = 'i';
4267 break;
4268 case LANGMAP:
4269 c1 = 'l';
4270 break;
4271 default:
4272 EMSG(_("E228: makemap: Illegal mode"));
4273 return FAIL;
4274 }
4275 do /* may do this twice if c2 is set */
4276 {
4277 /* When outputting <> form, need to make sure that 'cpo'
4278 * is set to the Vim default. */
4279 if (!did_cpo)
4280 {
4281 if (*mp->m_str == NUL) /* will use <Nop> */
4282 did_cpo = TRUE;
4283 else
4284 for (i = 0; i < 2; ++i)
4285 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4286 if (*p == K_SPECIAL || *p == NL)
4287 did_cpo = TRUE;
4288 if (did_cpo)
4289 {
4290 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4291 || put_eol(fd) < 0
4292 || fprintf(fd, "set cpo&vim") < 0
4293 || put_eol(fd) < 0)
4294 return FAIL;
4295 }
4296 }
4297 if (c1 && putc(c1, fd) < 0)
4298 return FAIL;
4299 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4300 return FAIL;
4301 if (fprintf(fd, cmd) < 0)
4302 return FAIL;
4303 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4304 return FAIL;
4305 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4306 return FAIL;
4307
4308 if ( putc(' ', fd) < 0
4309 || put_escstr(fd, mp->m_keys, 0) == FAIL
4310 || putc(' ', fd) < 0
4311 || put_escstr(fd, mp->m_str, 1) == FAIL
4312 || put_eol(fd) < 0)
4313 return FAIL;
4314 c1 = c2;
4315 c2 = NUL;
4316 }
4317 while (c1);
4318 }
4319 }
4320
4321 if (did_cpo)
4322 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4323 || put_eol(fd) < 0
4324 || fprintf(fd, "unlet s:cpo_save") < 0
4325 || put_eol(fd) < 0)
4326 return FAIL;
4327 return OK;
4328}
4329
4330/*
4331 * write escape string to file
4332 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4333 *
4334 * return FAIL for failure, OK otherwise
4335 */
4336 int
4337put_escstr(fd, strstart, what)
4338 FILE *fd;
4339 char_u *strstart;
4340 int what;
4341{
4342 char_u *str = strstart;
4343 int c;
4344 int modifiers;
4345
4346 /* :map xx <Nop> */
4347 if (*str == NUL && what == 1)
4348 {
4349 if (fprintf(fd, "<Nop>") < 0)
4350 return FAIL;
4351 return OK;
4352 }
4353
4354 for ( ; *str != NUL; ++str)
4355 {
4356#ifdef FEAT_MBYTE
4357 char_u *p;
4358
4359 /* Check for a multi-byte character, which may contain escaped
4360 * K_SPECIAL and CSI bytes */
4361 p = mb_unescape(&str);
4362 if (p != NULL)
4363 {
4364 while (*p != NUL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004365 if (fputc(*p++, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 return FAIL;
4367 --str;
4368 continue;
4369 }
4370#endif
4371
4372 c = *str;
4373 /*
4374 * Special key codes have to be translated to be able to make sense
4375 * when they are read back.
4376 */
4377 if (c == K_SPECIAL && what != 2)
4378 {
4379 modifiers = 0x0;
4380 if (str[1] == KS_MODIFIER)
4381 {
4382 modifiers = str[2];
4383 str += 3;
4384 c = *str;
4385 }
4386 if (c == K_SPECIAL)
4387 {
4388 c = TO_SPECIAL(str[1], str[2]);
4389 str += 2;
4390 }
4391 if (IS_SPECIAL(c) || modifiers) /* special key */
4392 {
4393 if (fprintf(fd, (char *)get_special_key_name(c, modifiers)) < 0)
4394 return FAIL;
4395 continue;
4396 }
4397 }
4398
4399 /*
4400 * A '\n' in a map command should be written as <NL>.
4401 * A '\n' in a set command should be written as \^V^J.
4402 */
4403 if (c == NL)
4404 {
4405 if (what == 2)
4406 {
4407 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4408 return FAIL;
4409 }
4410 else
4411 {
4412 if (fprintf(fd, "<NL>") < 0)
4413 return FAIL;
4414 }
4415 continue;
4416 }
4417
4418 /*
4419 * Some characters have to be escaped with CTRL-V to
4420 * prevent them from misinterpreted in DoOneCmd().
4421 * A space, Tab and '"' has to be escaped with a backslash to
4422 * prevent it to be misinterpreted in do_set().
4423 * A space has to be escaped with a CTRL-V when it's at the start of a
4424 * ":map" rhs.
4425 * A '<' has to be escaped with a CTRL-V to prevent it being
4426 * interpreted as the start of a special key name.
4427 * A space in the lhs of a :map needs a CTRL-V.
4428 */
4429 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4430 {
4431 if (putc('\\', fd) < 0)
4432 return FAIL;
4433 }
4434 else if (c < ' ' || c > '~' || c == '|'
4435 || (what == 0 && c == ' ')
4436 || (what == 1 && str == strstart && c == ' ')
4437 || (what != 2 && c == '<'))
4438 {
4439 if (putc(Ctrl_V, fd) < 0)
4440 return FAIL;
4441 }
4442 if (putc(c, fd) < 0)
4443 return FAIL;
4444 }
4445 return OK;
4446}
4447
4448/*
4449 * Check all mappings for the presence of special key codes.
4450 * Used after ":set term=xxx".
4451 */
4452 void
4453check_map_keycodes()
4454{
4455 mapblock_T *mp;
4456 char_u *p;
4457 int i;
4458 char_u buf[3];
4459 char_u *save_name;
4460 int abbr;
4461 int hash;
4462#ifdef FEAT_LOCALMAP
4463 buf_T *bp;
4464#endif
4465
4466 validate_maphash();
4467 save_name = sourcing_name;
4468 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4469
4470#ifdef FEAT_LOCALMAP
4471 /* This this once for each buffer, and then once for global
4472 * mappings/abbreviations with bp == NULL */
4473 for (bp = firstbuf; ; bp = bp->b_next)
4474 {
4475#endif
4476 /*
4477 * Do the loop twice: Once for mappings, once for abbreviations.
4478 * Then loop over all map hash lists.
4479 */
4480 for (abbr = 0; abbr <= 1; ++abbr)
4481 for (hash = 0; hash < 256; ++hash)
4482 {
4483 if (abbr)
4484 {
4485 if (hash) /* there is only one abbr list */
4486 break;
4487#ifdef FEAT_LOCALMAP
4488 if (bp != NULL)
4489 mp = bp->b_first_abbr;
4490 else
4491#endif
4492 mp = first_abbr;
4493 }
4494 else
4495 {
4496#ifdef FEAT_LOCALMAP
4497 if (bp != NULL)
4498 mp = bp->b_maphash[hash];
4499 else
4500#endif
4501 mp = maphash[hash];
4502 }
4503 for ( ; mp != NULL; mp = mp->m_next)
4504 {
4505 for (i = 0; i <= 1; ++i) /* do this twice */
4506 {
4507 if (i == 0)
4508 p = mp->m_keys; /* once for the "from" part */
4509 else
4510 p = mp->m_str; /* and once for the "to" part */
4511 while (*p)
4512 {
4513 if (*p == K_SPECIAL)
4514 {
4515 ++p;
4516 if (*p < 128) /* for "normal" tcap entries */
4517 {
4518 buf[0] = p[0];
4519 buf[1] = p[1];
4520 buf[2] = NUL;
4521 (void)add_termcap_entry(buf, FALSE);
4522 }
4523 ++p;
4524 }
4525 ++p;
4526 }
4527 }
4528 }
4529 }
4530#ifdef FEAT_LOCALMAP
4531 if (bp == NULL)
4532 break;
4533 }
4534#endif
4535 sourcing_name = save_name;
4536}
4537
4538#ifdef FEAT_EVAL
4539/*
4540 * Check the string "keys" against the lhs of all mappings
4541 * Return pointer to rhs of mapping (mapblock->m_str)
4542 * NULL otherwise
4543 */
4544 char_u *
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004545check_map(keys, mode, exact, ign_mod)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 char_u *keys;
4547 int mode;
4548 int exact; /* require exact match */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004549 int ign_mod; /* ignore preceding modifier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
4551 int hash;
4552 int len, minlen;
4553 mapblock_T *mp;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004554 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#ifdef FEAT_LOCALMAP
4556 int local;
4557#endif
4558
4559 validate_maphash();
4560
4561 len = (int)STRLEN(keys);
4562#ifdef FEAT_LOCALMAP
4563 for (local = 1; local >= 0; --local)
4564#endif
4565 /* loop over all hash lists */
4566 for (hash = 0; hash < 256; ++hash)
4567 {
4568#ifdef FEAT_LOCALMAP
4569 if (local)
4570 mp = curbuf->b_maphash[hash];
4571 else
4572#endif
4573 mp = maphash[hash];
4574 for ( ; mp != NULL; mp = mp->m_next)
4575 {
4576 /* skip entries with wrong mode, wrong length and not matching
4577 * ones */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004578 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
4579 {
4580 if (len > mp->m_keylen)
4581 minlen = mp->m_keylen;
4582 else
4583 minlen = len;
4584 s = mp->m_keys;
4585 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
4586 && s[2] != NUL)
4587 {
4588 s += 3;
4589 if (len > mp->m_keylen - 3)
4590 minlen = mp->m_keylen - 3;
4591 }
4592 if (STRNCMP(s, keys, minlen) == 0)
4593 return mp->m_str;
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597
4598 return NULL;
4599}
4600#endif
4601
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004602#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603/*
4604 * Default mappings for some often used keys.
4605 */
4606static struct initmap
4607{
4608 char_u *arg;
4609 int mode;
4610} initmappings[] =
4611{
4612#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4613 /* Use the Windows (CUA) keybindings. */
4614# ifdef FEAT_GUI
4615 {(char_u *)"<C-PageUp> H", NORMAL+VISUAL},
4616 {(char_u *)"<C-PageUp> <C-O>H",INSERT},
4617 {(char_u *)"<C-PageDown> L$", NORMAL+VISUAL},
4618 {(char_u *)"<C-PageDown> <C-O>L<C-O>$", INSERT},
4619
4620 /* paste, copy and cut */
4621 {(char_u *)"<S-Insert> \"*P", NORMAL},
4622 {(char_u *)"<S-Insert> \"-d\"*P", VISUAL},
4623 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
4624 {(char_u *)"<C-Insert> \"*y", VISUAL},
4625 {(char_u *)"<S-Del> \"*d", VISUAL},
4626 {(char_u *)"<C-Del> \"*d", VISUAL},
4627 {(char_u *)"<C-X> \"*d", VISUAL},
4628 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
4629# else
4630 {(char_u *)"\316\204 H", NORMAL+VISUAL}, /* CTRL-PageUp is "H" */
4631 {(char_u *)"\316\204 \017H",INSERT}, /* CTRL-PageUp is "^OH"*/
4632 {(char_u *)"\316v L$", NORMAL+VISUAL}, /* CTRL-PageDown is "L$" */
4633 {(char_u *)"\316v \017L\017$", INSERT}, /* CTRL-PageDown ="^OL^O$"*/
4634 {(char_u *)"\316w <C-Home>", NORMAL+VISUAL},
4635 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
4636 {(char_u *)"\316u <C-End>", NORMAL+VISUAL},
4637 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
4638
4639 /* paste, copy and cut */
4640# ifdef FEAT_CLIPBOARD
4641# ifdef DJGPP
4642 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
4643 {(char_u *)"\316\122 \"-d\"*P", VISUAL}, /* SHIFT-Insert is "-d"*P */
4644 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
4645 {(char_u *)"\316\222 \"*y", VISUAL}, /* CTRL-Insert is "*y */
4646# if 0 /* Shift-Del produces the same code as Del */
4647 {(char_u *)"\316\123 \"*d", VISUAL}, /* SHIFT-Del is "*d */
4648# endif
4649 {(char_u *)"\316\223 \"*d", VISUAL}, /* CTRL-Del is "*d */
4650 {(char_u *)"\030 \"-d", VISUAL}, /* CTRL-X is "-d */
4651# else
4652 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
4653 {(char_u *)"\316\324 \"-d\"*P", VISUAL}, /* SHIFT-Insert is "-d"*P */
4654 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
4655 {(char_u *)"\316\325 \"*y", VISUAL}, /* CTRL-Insert is "*y */
4656 {(char_u *)"\316\327 \"*d", VISUAL}, /* SHIFT-Del is "*d */
4657 {(char_u *)"\316\330 \"*d", VISUAL}, /* CTRL-Del is "*d */
4658 {(char_u *)"\030 \"-d", VISUAL}, /* CTRL-X is "-d */
4659# endif
4660# else
4661 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
4662 {(char_u *)"\316\324 \"-dP", VISUAL}, /* SHIFT-Insert is "-dP */
4663 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
4664 {(char_u *)"\316\325 y", VISUAL}, /* CTRL-Insert is y */
4665 {(char_u *)"\316\327 d", VISUAL}, /* SHIFT-Del is d */
4666 {(char_u *)"\316\330 d", VISUAL}, /* CTRL-Del is d */
4667# endif
4668# endif
4669#endif
4670
4671#if defined(MACOS)
4672 /* Use the Standard MacOS binding. */
4673 /* paste, copy and cut */
4674 {(char_u *)"<D-v> \"*P", NORMAL},
4675 {(char_u *)"<D-v> \"-d\"*P", VISUAL},
4676 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
4677 {(char_u *)"<D-c> \"*y", VISUAL},
4678 {(char_u *)"<D-x> \"*d", VISUAL},
4679 {(char_u *)"<Backspace> \"-d", VISUAL},
4680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681};
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684/*
4685 * Set up default mappings.
4686 */
4687 void
4688init_mappings()
4689{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004690#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int i;
4692
4693 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
4694 add_map(initmappings[i].arg, initmappings[i].mode);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696}
4697
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004698#if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
4699 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700/*
4701 * Add a mapping "map" for mode "mode".
4702 * Need to put string in allocated memory, because do_map() will modify it.
4703 */
4704 void
4705add_map(map, mode)
4706 char_u *map;
4707 int mode;
4708{
4709 char_u *s;
4710 char_u *cpo_save = p_cpo;
4711
4712 p_cpo = (char_u *)""; /* Allow <> notation */
4713 s = vim_strsave(map);
4714 if (s != NULL)
4715 {
4716 (void)do_map(0, s, mode, FALSE);
4717 vim_free(s);
4718 }
4719 p_cpo = cpo_save;
4720}
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004721#endif