add on-the-fly code checking
[vim_extended.git] / src / getchar.c
blob4b650f43d072736a8fa866b6289b1712bd518ae1
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * getchar.c
13 * functions related with getting a character from the user/mapping/redo/...
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
19 #include "vim.h"
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.
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!
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().
41 #define MINIMAL_SIZE 20 /* minimal size for b_str */
43 static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44 static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
46 static struct buffheader save_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
47 static struct buffheader save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
48 #endif
49 static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
51 static int typeahead_char = 0; /* typeahead char that's not flushed */
54 * when block_redo is TRUE redo buffer will not be changed
55 * used by edit() to repeat insertions and 'V' command for redoing
57 static int block_redo = FALSE;
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.
66 #define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) ? (c1) : ((c1) ^ 0x80))
69 * Each mapping is put in one of the 256 hash lists, to speed up finding it.
71 static mapblock_T *(maphash[256]);
72 static int maphash_valid = FALSE;
75 * List used for abbreviations.
77 static mapblock_T *first_abbr = NULL; /* first entry in abbrlist */
79 static int KeyNoremap = 0; /* remapping flags */
82 * variables used by vgetorpeek() and flush_buffers()
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).
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 */
103 #define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */
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).
109 #define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
110 static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
111 static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
113 static int last_recorded_len = 0; /* number of last recorded chars */
115 static char_u *get_buffcont __ARGS((struct buffheader *, int));
116 static void add_buff __ARGS((struct buffheader *, char_u *, long n));
117 static void add_num_buff __ARGS((struct buffheader *, long));
118 static void add_char_buff __ARGS((struct buffheader *, int));
119 static int read_stuff __ARGS((int advance));
120 static void start_stuff __ARGS((void));
121 static int read_redo __ARGS((int, int));
122 static void copy_redo __ARGS((int));
123 static void init_typebuf __ARGS((void));
124 static void gotchars __ARGS((char_u *, int));
125 static void may_sync_undo __ARGS((void));
126 static void closescript __ARGS((void));
127 static int vgetorpeek __ARGS((int));
128 static void map_free __ARGS((mapblock_T **));
129 static void validate_maphash __ARGS((void));
130 static void showmap __ARGS((mapblock_T *mp, int local));
131 #ifdef FEAT_EVAL
132 static char_u *eval_map_expr __ARGS((char_u *str));
133 #endif
136 * Free and clear a buffer.
138 void
139 free_buff(buf)
140 struct buffheader *buf;
142 struct buffblock *p, *np;
144 for (p = buf->bh_first.b_next; p != NULL; p = np)
146 np = p->b_next;
147 vim_free(p);
149 buf->bh_first.b_next = NULL;
153 * Return the contents of a buffer as a single string.
154 * K_SPECIAL and CSI in the returned string are escaped.
156 static char_u *
157 get_buffcont(buffer, dozero)
158 struct buffheader *buffer;
159 int dozero; /* count == zero is not an error */
161 long_u count = 0;
162 char_u *p = NULL;
163 char_u *p2;
164 char_u *str;
165 struct buffblock *bp;
167 /* compute the total length of the string */
168 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
169 count += (long_u)STRLEN(bp->b_str);
171 if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL)
173 p2 = p;
174 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
175 for (str = bp->b_str; *str; )
176 *p2++ = *str++;
177 *p2 = NUL;
179 return (p);
183 * Return the contents of the record buffer as a single string
184 * and clear the record buffer.
185 * K_SPECIAL and CSI in the returned string are escaped.
187 char_u *
188 get_recorded()
190 char_u *p;
191 size_t len;
193 p = get_buffcont(&recordbuff, TRUE);
194 free_buff(&recordbuff);
197 * Remove the characters that were added the last time, these must be the
198 * (possibly mapped) characters that stopped the recording.
200 len = STRLEN(p);
201 if ((int)len >= last_recorded_len)
203 len -= last_recorded_len;
204 p[len] = NUL;
208 * When stopping recording from Insert mode with CTRL-O q, also remove the
209 * CTRL-O.
211 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
212 p[len - 1] = NUL;
214 return (p);
218 * Return the contents of the redo buffer as a single string.
219 * K_SPECIAL and CSI in the returned string are escaped.
221 char_u *
222 get_inserted()
224 return get_buffcont(&redobuff, FALSE);
228 * Add string "s" after the current block of buffer "buf".
229 * K_SPECIAL and CSI should have been escaped already.
231 static void
232 add_buff(buf, s, slen)
233 struct buffheader *buf;
234 char_u *s;
235 long slen; /* length of "s" or -1 */
237 struct buffblock *p;
238 long_u len;
240 if (slen < 0)
241 slen = (long)STRLEN(s);
242 if (slen == 0) /* don't add empty strings */
243 return;
245 if (buf->bh_first.b_next == NULL) /* first add to list */
247 buf->bh_space = 0;
248 buf->bh_curr = &(buf->bh_first);
250 else if (buf->bh_curr == NULL) /* buffer has already been read */
252 EMSG(_("E222: Add to read buffer"));
253 return;
255 else if (buf->bh_index != 0)
256 mch_memmove(buf->bh_first.b_next->b_str,
257 buf->bh_first.b_next->b_str + buf->bh_index,
258 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
259 buf->bh_index = 0;
261 if (buf->bh_space >= (int)slen)
263 len = (long_u)STRLEN(buf->bh_curr->b_str);
264 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
265 buf->bh_space -= slen;
267 else
269 if (slen < MINIMAL_SIZE)
270 len = MINIMAL_SIZE;
271 else
272 len = slen;
273 p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len),
274 TRUE);
275 if (p == NULL)
276 return; /* no space, just forget it */
277 buf->bh_space = (int)(len - slen);
278 vim_strncpy(p->b_str, s, (size_t)slen);
280 p->b_next = buf->bh_curr->b_next;
281 buf->bh_curr->b_next = p;
282 buf->bh_curr = p;
284 return;
288 * Add number "n" to buffer "buf".
290 static void
291 add_num_buff(buf, n)
292 struct buffheader *buf;
293 long n;
295 char_u number[32];
297 sprintf((char *)number, "%ld", n);
298 add_buff(buf, number, -1L);
302 * Add character 'c' to buffer "buf".
303 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
305 static void
306 add_char_buff(buf, c)
307 struct buffheader *buf;
308 int c;
310 #ifdef FEAT_MBYTE
311 char_u bytes[MB_MAXBYTES + 1];
312 int len;
313 int i;
314 #endif
315 char_u temp[4];
317 #ifdef FEAT_MBYTE
318 if (IS_SPECIAL(c))
319 len = 1;
320 else
321 len = (*mb_char2bytes)(c, bytes);
322 for (i = 0; i < len; ++i)
324 if (!IS_SPECIAL(c))
325 c = bytes[i];
326 #endif
328 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
330 /* translate special key code into three byte sequence */
331 temp[0] = K_SPECIAL;
332 temp[1] = K_SECOND(c);
333 temp[2] = K_THIRD(c);
334 temp[3] = NUL;
336 #ifdef FEAT_GUI
337 else if (c == CSI)
339 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
340 temp[0] = CSI;
341 temp[1] = KS_EXTRA;
342 temp[2] = (int)KE_CSI;
343 temp[3] = NUL;
345 #endif
346 else
348 temp[0] = c;
349 temp[1] = NUL;
351 add_buff(buf, temp, -1L);
352 #ifdef FEAT_MBYTE
354 #endif
358 * Get one byte from the stuff buffer.
359 * If advance == TRUE go to the next char.
360 * No translation is done K_SPECIAL and CSI are escaped.
362 static int
363 read_stuff(advance)
364 int advance;
366 char_u c;
367 struct buffblock *curr;
369 if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */
370 return NUL;
372 curr = stuffbuff.bh_first.b_next;
373 c = curr->b_str[stuffbuff.bh_index];
375 if (advance)
377 if (curr->b_str[++stuffbuff.bh_index] == NUL)
379 stuffbuff.bh_first.b_next = curr->b_next;
380 vim_free(curr);
381 stuffbuff.bh_index = 0;
384 return c;
388 * Prepare the stuff buffer for reading (if it contains something).
390 static void
391 start_stuff()
393 if (stuffbuff.bh_first.b_next != NULL)
395 stuffbuff.bh_curr = &(stuffbuff.bh_first);
396 stuffbuff.bh_space = 0;
401 * Return TRUE if the stuff buffer is empty.
404 stuff_empty()
406 return (stuffbuff.bh_first.b_next == NULL);
410 * Set a typeahead character that won't be flushed.
412 void
413 typeahead_noflush(c)
414 int c;
416 typeahead_char = c;
420 * Remove the contents of the stuff buffer and the mapped characters in the
421 * typeahead buffer (used in case of an error). If 'typeahead' is true,
422 * flush all typeahead characters (used when interrupted by a CTRL-C).
424 void
425 flush_buffers(typeahead)
426 int typeahead;
428 init_typebuf();
430 start_stuff();
431 while (read_stuff(TRUE) != NUL)
434 if (typeahead) /* remove all typeahead */
437 * We have to get all characters, because we may delete the first part
438 * of an escape sequence.
439 * In an xterm we get one char at a time and we have to get them all.
441 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L,
442 typebuf.tb_change_cnt) != 0)
444 typebuf.tb_off = MAXMAPLEN;
445 typebuf.tb_len = 0;
447 else /* remove mapped characters only */
449 typebuf.tb_off += typebuf.tb_maplen;
450 typebuf.tb_len -= typebuf.tb_maplen;
452 typebuf.tb_maplen = 0;
453 typebuf.tb_silent = 0;
454 cmd_silent = FALSE;
455 typebuf.tb_no_abbr_cnt = 0;
459 * The previous contents of the redo buffer is kept in old_redobuffer.
460 * This is used for the CTRL-O <.> command in insert mode.
462 void
463 ResetRedobuff()
465 if (!block_redo)
467 free_buff(&old_redobuff);
468 old_redobuff = redobuff;
469 redobuff.bh_first.b_next = NULL;
473 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
475 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
476 * Used before executing autocommands and user functions.
478 static int save_level = 0;
480 void
481 saveRedobuff()
483 char_u *s;
485 if (save_level++ == 0)
487 save_redobuff = redobuff;
488 redobuff.bh_first.b_next = NULL;
489 save_old_redobuff = old_redobuff;
490 old_redobuff.bh_first.b_next = NULL;
492 /* Make a copy, so that ":normal ." in a function works. */
493 s = get_buffcont(&save_redobuff, FALSE);
494 if (s != NULL)
496 add_buff(&redobuff, s, -1L);
497 vim_free(s);
503 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
504 * Used after executing autocommands and user functions.
506 void
507 restoreRedobuff()
509 if (--save_level == 0)
511 free_buff(&redobuff);
512 redobuff = save_redobuff;
513 free_buff(&old_redobuff);
514 old_redobuff = save_old_redobuff;
517 #endif
520 * Append "s" to the redo buffer.
521 * K_SPECIAL and CSI should already have been escaped.
523 void
524 AppendToRedobuff(s)
525 char_u *s;
527 if (!block_redo)
528 add_buff(&redobuff, s, -1L);
532 * Append to Redo buffer literally, escaping special characters with CTRL-V.
533 * K_SPECIAL and CSI are escaped as well.
535 void
536 AppendToRedobuffLit(str, len)
537 char_u *str;
538 int len; /* length of "str" or -1 for up to the NUL */
540 char_u *s = str;
541 int c;
542 char_u *start;
544 if (block_redo)
545 return;
547 while (len < 0 ? *s != NUL : s - str < len)
549 /* Put a string of normal characters in the redo buffer (that's
550 * faster). */
551 start = s;
552 while (*s >= ' '
553 #ifndef EBCDIC
554 && *s < DEL /* EBCDIC: all chars above space are normal */
555 #endif
556 && (len < 0 || s - str < len))
557 ++s;
559 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
560 * typed next. */
561 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
562 --s;
563 if (s > start)
564 add_buff(&redobuff, start, (long)(s - start));
566 if (*s == NUL || (len >= 0 && s - str >= len))
567 break;
569 /* Handle a special or multibyte character. */
570 #ifdef FEAT_MBYTE
571 if (has_mbyte)
572 /* Handle composing chars separately. */
573 c = mb_cptr2char_adv(&s);
574 else
575 #endif
576 c = *s++;
577 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
578 add_char_buff(&redobuff, Ctrl_V);
580 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
581 if (*s == NUL && c == '0')
582 #ifdef EBCDIC
583 add_buff(&redobuff, (char_u *)"xf0", 3L);
584 #else
585 add_buff(&redobuff, (char_u *)"048", 3L);
586 #endif
587 else
588 add_char_buff(&redobuff, c);
593 * Append a character to the redo buffer.
594 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
596 void
597 AppendCharToRedobuff(c)
598 int c;
600 if (!block_redo)
601 add_char_buff(&redobuff, c);
605 * Append a number to the redo buffer.
607 void
608 AppendNumberToRedobuff(n)
609 long n;
611 if (!block_redo)
612 add_num_buff(&redobuff, n);
616 * Append string "s" to the stuff buffer.
617 * CSI and K_SPECIAL must already have been escaped.
619 void
620 stuffReadbuff(s)
621 char_u *s;
623 add_buff(&stuffbuff, s, -1L);
626 void
627 stuffReadbuffLen(s, len)
628 char_u *s;
629 long len;
631 add_buff(&stuffbuff, s, len);
634 #if defined(FEAT_EVAL) || defined(PROTO)
636 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
637 * escaping other K_SPECIAL and CSI bytes.
639 void
640 stuffReadbuffSpec(s)
641 char_u *s;
643 while (*s != NUL)
645 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
647 /* Insert special key literally. */
648 stuffReadbuffLen(s, 3L);
649 s += 3;
651 else
652 #ifdef FEAT_MBYTE
653 stuffcharReadbuff(mb_ptr2char_adv(&s));
654 #else
655 stuffcharReadbuff(*s++);
656 #endif
659 #endif
662 * Append a character to the stuff buffer.
663 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
665 void
666 stuffcharReadbuff(c)
667 int c;
669 add_char_buff(&stuffbuff, c);
673 * Append a number to the stuff buffer.
675 void
676 stuffnumReadbuff(n)
677 long n;
679 add_num_buff(&stuffbuff, n);
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
690 static int
691 read_redo(init, old_redo)
692 int init;
693 int old_redo;
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
704 if (init)
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;
715 if ((c = *p) != NUL)
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
728 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
730 c = TO_SPECIAL(p[1], p[2]);
731 p += 2;
733 #ifdef FEAT_GUI
734 if (c == CSI) /* escaped CSI */
735 p += 2;
736 #endif
737 if (*++p == NUL && bp->b_next != NULL)
739 bp = bp->b_next;
740 p = bp->b_str;
742 #ifdef FEAT_MBYTE
743 buf[i] = c;
744 if (i == n - 1) /* last byte of a character */
746 if (n != 1)
747 c = (*mb_ptr2char)(buf);
748 break;
750 c = *p;
751 if (c == NUL) /* cannot happen? */
752 break;
753 #endif
757 return c;
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.
765 static void
766 copy_redo(old_redo)
767 int old_redo;
769 int c;
771 while ((c = read_redo(FALSE, old_redo)) != NUL)
772 stuffcharReadbuff(c);
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
782 * return FAIL for failure, OK otherwise
785 start_redo(count, old_redo)
786 long count;
787 int old_redo;
789 int c;
791 /* init the pointers; return if nothing to redo */
792 if (read_redo(TRUE, old_redo) == FAIL)
793 return FAIL;
795 c = read_redo(FALSE, old_redo);
797 /* copy the buffer name, if present */
798 if (c == '"')
800 add_buff(&stuffbuff, (char_u *)"\"", 1L);
801 c = read_redo(FALSE, old_redo);
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);
810 #ifdef FEAT_VISUAL
811 if (c == 'v') /* redo Visual */
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);
820 #endif
822 /* try to enter the count (in place of a previous count) */
823 if (count)
825 while (VIM_ISDIGIT(c)) /* skip "old" count */
826 c = read_redo(FALSE, old_redo);
827 add_num_buff(&stuffbuff, count);
830 /* copy from the redo buffer into the stuff buffer */
831 add_char_buff(&stuffbuff, c);
832 copy_redo(old_redo);
833 return OK;
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
842 start_redo_ins()
844 int c;
846 if (read_redo(TRUE, FALSE) == FAIL)
847 return FAIL;
848 start_stuff();
850 /* skip the count and the command character */
851 while ((c = read_redo(FALSE, FALSE)) != NUL)
853 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
855 if (c == 'O' || c == 'o')
856 stuffReadbuff(NL_STR);
857 break;
861 /* copy the typed text from the redo buffer into the stuff buffer */
862 copy_redo(FALSE);
863 block_redo = TRUE;
864 return OK;
867 void
868 stop_redo_ins()
870 block_redo = FALSE;
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.
878 static void
879 init_typebuf()
881 if (typebuf.tb_buf == NULL)
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;
893 * insert a string in position 'offset' in the typeahead buffer (for "@r"
894 * and ":normal" command, vgetorpeek() and check_termcode())
896 * If noremap is REMAP_YES, new string can be mapped again.
897 * If noremap is REMAP_NONE, new string cannot be mapped again.
898 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
899 * but abbreviations are allowed.
900 * 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.
904 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
905 * offset is non-zero!).
907 * If silent is TRUE, cmd_silent is set when the characters are obtained.
909 * return FAIL for failure, OK otherwise
912 ins_typebuf(str, noremap, offset, nottyped, silent)
913 char_u *str;
914 int noremap;
915 int offset;
916 int nottyped;
917 int silent;
919 char_u *s1, *s2;
920 int newlen;
921 int addlen;
922 int i;
923 int newoff;
924 int val;
925 int nrm;
927 init_typebuf();
928 if (++typebuf.tb_change_cnt == 0)
929 typebuf.tb_change_cnt = 1;
931 addlen = (int)STRLEN(str);
934 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
936 if (offset == 0 && addlen <= typebuf.tb_off)
938 typebuf.tb_off -= addlen;
939 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
943 * Need to allocate a new buffer.
944 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
945 * characters. We add some extra room to avoid having to allocate too
946 * often.
948 else
950 newoff = MAXMAPLEN + 4;
951 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
952 if (newlen < 0) /* string is getting too long */
954 EMSG(_(e_toocompl)); /* also calls flush_buffers */
955 setcursor();
956 return FAIL;
958 s1 = alloc(newlen);
959 if (s1 == NULL) /* out of memory */
960 return FAIL;
961 s2 = alloc(newlen);
962 if (s2 == NULL) /* out of memory */
964 vim_free(s1);
965 return FAIL;
967 typebuf.tb_buflen = newlen;
969 /* copy the old chars, before the insertion point */
970 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
971 (size_t)offset);
972 /* copy the new chars */
973 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
974 /* copy the old chars, after the insertion point, including the NUL at
975 * the end */
976 mch_memmove(s1 + newoff + offset + addlen,
977 typebuf.tb_buf + typebuf.tb_off + offset,
978 (size_t)(typebuf.tb_len - offset + 1));
979 if (typebuf.tb_buf != typebuf_init)
980 vim_free(typebuf.tb_buf);
981 typebuf.tb_buf = s1;
983 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
984 (size_t)offset);
985 mch_memmove(s2 + newoff + offset + addlen,
986 typebuf.tb_noremap + typebuf.tb_off + offset,
987 (size_t)(typebuf.tb_len - offset));
988 if (typebuf.tb_noremap != noremapbuf_init)
989 vim_free(typebuf.tb_noremap);
990 typebuf.tb_noremap = s2;
992 typebuf.tb_off = newoff;
994 typebuf.tb_len += addlen;
996 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
997 if (noremap == REMAP_SCRIPT)
998 val = RM_SCRIPT;
999 else if (noremap == REMAP_SKIP)
1000 val = RM_ABBR;
1001 else
1002 val = RM_NONE;
1005 * Adjust typebuf.tb_noremap[] for the new characters:
1006 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1007 * (sometimes) not remappable
1008 * If noremap == REMAP_YES: all the new characters are mappable
1009 * If noremap > 0: "noremap" characters are not remappable, the rest
1010 * mappable
1012 if (noremap == REMAP_SKIP)
1013 nrm = 1;
1014 else if (noremap < 0)
1015 nrm = addlen;
1016 else
1017 nrm = noremap;
1018 for (i = 0; i < addlen; ++i)
1019 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1020 (--nrm >= 0) ? val : RM_YES;
1022 /* tb_maplen and tb_silent only remember the length of mapped and/or
1023 * silent mappings at the start of the buffer, assuming that a mapped
1024 * sequence doesn't result in typed characters. */
1025 if (nottyped || typebuf.tb_maplen > offset)
1026 typebuf.tb_maplen += addlen;
1027 if (silent || typebuf.tb_silent > offset)
1029 typebuf.tb_silent += addlen;
1030 cmd_silent = TRUE;
1032 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1033 typebuf.tb_no_abbr_cnt += addlen;
1035 return OK;
1039 * Put character "c" back into the typeahead buffer.
1040 * Can be used for a character obtained by vgetc() that needs to be put back.
1041 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1042 * the char.
1044 void
1045 ins_char_typebuf(c)
1046 int c;
1048 #ifdef FEAT_MBYTE
1049 char_u buf[MB_MAXBYTES];
1050 #else
1051 char_u buf[4];
1052 #endif
1053 if (IS_SPECIAL(c))
1055 buf[0] = K_SPECIAL;
1056 buf[1] = K_SECOND(c);
1057 buf[2] = K_THIRD(c);
1058 buf[3] = NUL;
1060 else
1062 #ifdef FEAT_MBYTE
1063 buf[(*mb_char2bytes)(c, buf)] = NUL;
1064 #else
1065 buf[0] = c;
1066 buf[1] = NUL;
1067 #endif
1069 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
1073 * Return TRUE if the typeahead buffer was changed (while waiting for a
1074 * character to arrive). Happens when a message was received from a client or
1075 * from feedkeys().
1076 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1077 * changed it was reallocated and the old pointer can no longer be used.
1078 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1079 * that was just added.
1082 typebuf_changed(tb_change_cnt)
1083 int tb_change_cnt; /* old value of typebuf.tb_change_cnt */
1085 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
1086 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1087 || typebuf_was_filled
1088 #endif
1093 * Return TRUE if there are no characters in the typeahead buffer that have
1094 * not been typed (result from a mapping or come from ":normal").
1097 typebuf_typed()
1099 return typebuf.tb_maplen == 0;
1102 #if defined(FEAT_VISUAL) || defined(PROTO)
1104 * Return the number of characters that are mapped (or not typed).
1107 typebuf_maplen()
1109 return typebuf.tb_maplen;
1111 #endif
1114 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1116 void
1117 del_typebuf(len, offset)
1118 int len;
1119 int offset;
1121 int i;
1123 if (len == 0)
1124 return; /* nothing to do */
1126 typebuf.tb_len -= len;
1129 * Easy case: Just increase typebuf.tb_off.
1131 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1132 >= 3 * MAXMAPLEN + 3)
1133 typebuf.tb_off += len;
1135 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1137 else
1139 i = typebuf.tb_off + offset;
1141 * Leave some extra room at the end to avoid reallocation.
1143 if (typebuf.tb_off > MAXMAPLEN)
1145 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1146 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1147 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1148 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1149 typebuf.tb_off = MAXMAPLEN;
1151 /* adjust typebuf.tb_buf (include the NUL at the end) */
1152 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1153 typebuf.tb_buf + i + len,
1154 (size_t)(typebuf.tb_len - offset + 1));
1155 /* adjust typebuf.tb_noremap[] */
1156 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1157 typebuf.tb_noremap + i + len,
1158 (size_t)(typebuf.tb_len - offset));
1161 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1163 if (typebuf.tb_maplen < offset + len)
1164 typebuf.tb_maplen = offset;
1165 else
1166 typebuf.tb_maplen -= len;
1168 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1170 if (typebuf.tb_silent < offset + len)
1171 typebuf.tb_silent = offset;
1172 else
1173 typebuf.tb_silent -= len;
1175 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1177 if (typebuf.tb_no_abbr_cnt < offset + len)
1178 typebuf.tb_no_abbr_cnt = offset;
1179 else
1180 typebuf.tb_no_abbr_cnt -= len;
1183 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1184 /* Reset the flag that text received from a client or from feedkeys()
1185 * was inserted in the typeahead buffer. */
1186 typebuf_was_filled = FALSE;
1187 #endif
1188 if (++typebuf.tb_change_cnt == 0)
1189 typebuf.tb_change_cnt = 1;
1193 * Write typed characters to script file.
1194 * If recording is on put the character in the recordbuffer.
1196 static void
1197 gotchars(chars, len)
1198 char_u *chars;
1199 int len;
1201 char_u *s = chars;
1202 int c;
1203 char_u buf[2];
1204 int todo = len;
1206 /* remember how many chars were last recorded */
1207 if (Recording)
1208 last_recorded_len += len;
1210 buf[1] = NUL;
1211 while (todo--)
1213 /* Handle one byte at a time; no translation to be done. */
1214 c = *s++;
1215 updatescript(c);
1217 if (Recording)
1219 buf[0] = c;
1220 add_buff(&recordbuff, buf, 1L);
1223 may_sync_undo();
1225 #ifdef FEAT_EVAL
1226 /* output "debug mode" message next time in debug mode */
1227 debug_did_msg = FALSE;
1228 #endif
1230 /* Since characters have been typed, consider the following to be in
1231 * another mapping. Search string will be kept in history. */
1232 ++maptick;
1236 * Sync undo. Called when typed characters are obtained from the typeahead
1237 * buffer, or when a menu is used.
1238 * Do not sync:
1239 * - In Insert mode, unless cursor key has been used.
1240 * - While reading a script file.
1241 * - When no_u_sync is non-zero.
1243 static void
1244 may_sync_undo()
1246 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
1247 && scriptin[curscript] == NULL)
1248 u_sync(FALSE);
1252 * Make "typebuf" empty and allocate new buffers.
1253 * Returns FAIL when out of memory.
1256 alloc_typebuf()
1258 typebuf.tb_buf = alloc(TYPELEN_INIT);
1259 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1260 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1262 free_typebuf();
1263 return FAIL;
1265 typebuf.tb_buflen = TYPELEN_INIT;
1266 typebuf.tb_off = 0;
1267 typebuf.tb_len = 0;
1268 typebuf.tb_maplen = 0;
1269 typebuf.tb_silent = 0;
1270 typebuf.tb_no_abbr_cnt = 0;
1271 if (++typebuf.tb_change_cnt == 0)
1272 typebuf.tb_change_cnt = 1;
1273 return OK;
1277 * Free the buffers of "typebuf".
1279 void
1280 free_typebuf()
1282 if (typebuf.tb_buf == typebuf_init)
1283 EMSG2(_(e_intern2), "Free typebuf 1");
1284 else
1285 vim_free(typebuf.tb_buf);
1286 if (typebuf.tb_buf == noremapbuf_init)
1287 EMSG2(_(e_intern2), "Free typebuf 2");
1288 else
1289 vim_free(typebuf.tb_noremap);
1293 * When doing ":so! file", the current typeahead needs to be saved, and
1294 * restored when "file" has been read completely.
1296 static typebuf_T saved_typebuf[NSCRIPT];
1299 save_typebuf()
1301 init_typebuf();
1302 saved_typebuf[curscript] = typebuf;
1303 /* If out of memory: restore typebuf and close file. */
1304 if (alloc_typebuf() == FAIL)
1306 closescript();
1307 return FAIL;
1309 return OK;
1312 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1315 * Save all three kinds of typeahead, so that the user must type at a prompt.
1317 void
1318 save_typeahead(tp)
1319 tasave_T *tp;
1321 tp->save_typebuf = typebuf;
1322 tp->typebuf_valid = (alloc_typebuf() == OK);
1323 if (!tp->typebuf_valid)
1324 typebuf = tp->save_typebuf;
1326 tp->save_stuffbuff = stuffbuff;
1327 stuffbuff.bh_first.b_next = NULL;
1328 # ifdef USE_INPUT_BUF
1329 tp->save_inputbuf = get_input_buf();
1330 # endif
1334 * Restore the typeahead to what it was before calling save_typeahead().
1335 * The allocated memory is freed, can only be called once!
1337 void
1338 restore_typeahead(tp)
1339 tasave_T *tp;
1341 if (tp->typebuf_valid)
1343 free_typebuf();
1344 typebuf = tp->save_typebuf;
1347 free_buff(&stuffbuff);
1348 stuffbuff = tp->save_stuffbuff;
1349 # ifdef USE_INPUT_BUF
1350 set_input_buf(tp->save_inputbuf);
1351 # endif
1353 #endif
1356 * Open a new script file for the ":source!" command.
1358 void
1359 openscript(name, directly)
1360 char_u *name;
1361 int directly; /* when TRUE execute directly */
1363 if (curscript + 1 == NSCRIPT)
1365 EMSG(_(e_nesting));
1366 return;
1368 #ifdef FEAT_EVAL
1369 if (ignore_script)
1370 /* Not reading from script, also don't open one. Warning message? */
1371 return;
1372 #endif
1374 if (scriptin[curscript] != NULL) /* already reading script */
1375 ++curscript;
1376 /* use NameBuff for expanded name */
1377 expand_env(name, NameBuff, MAXPATHL);
1378 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1380 EMSG2(_(e_notopen), name);
1381 if (curscript)
1382 --curscript;
1383 return;
1385 if (save_typebuf() == FAIL)
1386 return;
1389 * Execute the commands from the file right now when using ":source!"
1390 * after ":global" or ":argdo" or in a loop. Also when another command
1391 * follows. This means the display won't be updated. Don't do this
1392 * always, "make test" would fail.
1394 if (directly)
1396 oparg_T oa;
1397 int oldcurscript;
1398 int save_State = State;
1399 int save_restart_edit = restart_edit;
1400 int save_insertmode = p_im;
1401 int save_finish_op = finish_op;
1402 int save_msg_scroll = msg_scroll;
1404 State = NORMAL;
1405 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1406 restart_edit = 0; /* don't go to Insert mode */
1407 p_im = FALSE; /* don't use 'insertmode' */
1408 clear_oparg(&oa);
1409 finish_op = FALSE;
1411 oldcurscript = curscript;
1414 update_topline_cursor(); /* update cursor position and topline */
1415 normal_cmd(&oa, FALSE); /* execute one command */
1416 vpeekc(); /* check for end of file */
1418 while (scriptin[oldcurscript] != NULL);
1420 State = save_State;
1421 msg_scroll = save_msg_scroll;
1422 restart_edit = save_restart_edit;
1423 p_im = save_insertmode;
1424 finish_op = save_finish_op;
1429 * Close the currently active input script.
1431 static void
1432 closescript()
1434 free_typebuf();
1435 typebuf = saved_typebuf[curscript];
1437 fclose(scriptin[curscript]);
1438 scriptin[curscript] = NULL;
1439 if (curscript > 0)
1440 --curscript;
1443 #if defined(EXITFREE) || defined(PROTO)
1444 void
1445 close_all_scripts()
1447 while (scriptin[0] != NULL)
1448 closescript();
1450 #endif
1452 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1454 * Return TRUE when reading keys from a script file.
1457 using_script()
1459 return scriptin[curscript] != NULL;
1461 #endif
1464 * This function is called just before doing a blocking wait. Thus after
1465 * waiting 'updatetime' for a character to arrive.
1467 void
1468 before_blocking()
1470 updatescript(0);
1471 #ifdef FEAT_EVAL
1472 if (may_garbage_collect)
1473 garbage_collect();
1474 #endif
1478 * updatescipt() is called when a character can be written into the script file
1479 * or when we have waited some time for a character (c == 0)
1481 * All the changed memfiles are synced if c == 0 or when the number of typed
1482 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1484 void
1485 updatescript(c)
1486 int c;
1488 static int count = 0;
1490 if (c && scriptout)
1491 putc(c, scriptout);
1492 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1494 ml_sync_all(c == 0, TRUE);
1495 count = 0;
1499 #define KL_PART_KEY -1 /* keylen value for incomplete key-code */
1500 #define KL_PART_MAP -2 /* keylen value for incomplete mapping */
1502 static int old_char = -1; /* character put back by vungetc() */
1503 static int old_mod_mask; /* mod_mask for ungotten character */
1506 * Get the next input character.
1507 * Can return a special key or a multi-byte character.
1508 * Can return NUL when called recursively, use safe_vgetc() if that's not
1509 * wanted.
1510 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1511 * Collects the bytes of a multibyte character into the whole character.
1512 * Returns the modifers in the global "mod_mask".
1515 vgetc()
1517 int c, c2;
1518 #ifdef FEAT_MBYTE
1519 int n;
1520 char_u buf[MB_MAXBYTES];
1521 int i;
1522 #endif
1524 #ifdef FEAT_EVAL
1525 /* Do garbage collection when garbagecollect() was called previously and
1526 * we are now at the toplevel. */
1527 if (may_garbage_collect && want_garbage_collect)
1528 garbage_collect();
1529 #endif
1532 * If a character was put back with vungetc, it was already processed.
1533 * Return it directly.
1535 if (old_char != -1)
1537 c = old_char;
1538 old_char = -1;
1539 mod_mask = old_mod_mask;
1541 else
1543 mod_mask = 0x0;
1544 last_recorded_len = 0;
1545 for (;;) /* this is done twice if there are modifiers */
1547 if (mod_mask) /* no mapping after modifier has been read */
1549 ++no_mapping;
1550 ++allow_keys;
1552 c = vgetorpeek(TRUE);
1553 if (mod_mask)
1555 --no_mapping;
1556 --allow_keys;
1559 /* Get two extra bytes for special keys */
1560 if (c == K_SPECIAL
1561 #ifdef FEAT_GUI
1562 || c == CSI
1563 #endif
1566 int save_allow_keys = allow_keys;
1568 ++no_mapping;
1569 allow_keys = 0; /* make sure BS is not found */
1570 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1571 c = vgetorpeek(TRUE);
1572 --no_mapping;
1573 allow_keys = save_allow_keys;
1574 if (c2 == KS_MODIFIER)
1576 mod_mask = c;
1577 continue;
1579 c = TO_SPECIAL(c2, c);
1581 #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1582 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1583 * know that a menu was torn off */
1584 if (c == K_TEAROFF)
1586 char_u name[200];
1587 int i;
1589 /* get menu path, it ends with a <CR> */
1590 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1592 name[i] = c;
1593 if (i < 199)
1594 ++i;
1596 name[i] = NUL;
1597 gui_make_tearoff(name);
1598 continue;
1600 #endif
1601 #if defined(FEAT_GUI) && defined(HAVE_GTK2) && defined(FEAT_MENU)
1602 /* GTK: <F10> normally selects the menu, but it's passed until
1603 * here to allow mapping it. Intercept and invoke the GTK
1604 * behavior if it's not mapped. */
1605 if (c == K_F10 && gui.menubar != NULL)
1607 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1608 continue;
1610 #endif
1611 #ifdef FEAT_GUI
1612 /* Handle focus event here, so that the caller doesn't need to
1613 * know about it. Return K_IGNORE so that we loop once (needed if
1614 * 'lazyredraw' is set). */
1615 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1617 ui_focus_change(c == K_FOCUSGAINED);
1618 c = K_IGNORE;
1621 /* Handle redraw event here, this is used by CodeCheck.*/
1622 if (c == K_REDRAW) {
1623 /* gui_update_screen() does not work because of
1624 * update_screen(0) inside it. */
1625 update_topline();
1626 validate_cursor();
1627 update_screen(SOME_VALID);
1628 setcursor();
1629 cursor_on();
1630 out_flush();
1632 c = K_IGNORE;
1635 /* Translate K_CSI to CSI. The special key is only used to avoid
1636 * it being recognized as the start of a special key. */
1637 if (c == K_CSI)
1638 c = CSI;
1639 #endif
1641 #ifdef MSDOS
1643 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1644 * Delete the 3 here.
1646 else if (c == K_NUL && vpeekc() == 3)
1647 (void)vgetorpeek(TRUE);
1648 #endif
1650 /* a keypad or special function key was not mapped, use it like
1651 * its ASCII equivalent */
1652 switch (c)
1654 case K_KPLUS: c = '+'; break;
1655 case K_KMINUS: c = '-'; break;
1656 case K_KDIVIDE: c = '/'; break;
1657 case K_KMULTIPLY: c = '*'; break;
1658 case K_KENTER: c = CAR; break;
1659 case K_KPOINT:
1660 #ifdef WIN32
1661 /* Can be either '.' or a ',', *
1662 * depending on the type of keypad. */
1663 c = MapVirtualKey(VK_DECIMAL, 2); break;
1664 #else
1665 c = '.'; break;
1666 #endif
1667 case K_K0: c = '0'; break;
1668 case K_K1: c = '1'; break;
1669 case K_K2: c = '2'; break;
1670 case K_K3: c = '3'; break;
1671 case K_K4: c = '4'; break;
1672 case K_K5: c = '5'; break;
1673 case K_K6: c = '6'; break;
1674 case K_K7: c = '7'; break;
1675 case K_K8: c = '8'; break;
1676 case K_K9: c = '9'; break;
1678 case K_XHOME:
1679 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1681 c = K_S_HOME;
1682 mod_mask = 0;
1684 else if (mod_mask == MOD_MASK_CTRL)
1686 c = K_C_HOME;
1687 mod_mask = 0;
1689 else
1690 c = K_HOME;
1691 break;
1692 case K_XEND:
1693 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1695 c = K_S_END;
1696 mod_mask = 0;
1698 else if (mod_mask == MOD_MASK_CTRL)
1700 c = K_C_END;
1701 mod_mask = 0;
1703 else
1704 c = K_END;
1705 break;
1707 case K_XUP: c = K_UP; break;
1708 case K_XDOWN: c = K_DOWN; break;
1709 case K_XLEFT: c = K_LEFT; break;
1710 case K_XRIGHT: c = K_RIGHT; break;
1713 #ifdef FEAT_MBYTE
1714 /* For a multi-byte character get all the bytes and return the
1715 * converted character.
1716 * Note: This will loop until enough bytes are received!
1718 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1720 ++no_mapping;
1721 buf[0] = c;
1722 for (i = 1; i < n; ++i)
1724 buf[i] = vgetorpeek(TRUE);
1725 if (buf[i] == K_SPECIAL
1726 #ifdef FEAT_GUI
1727 || buf[i] == CSI
1728 #endif
1731 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1732 * which represents a K_SPECIAL (0x80),
1733 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1734 * a CSI (0x9B),
1735 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1736 c = vgetorpeek(TRUE);
1737 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1738 buf[i] = CSI;
1741 --no_mapping;
1742 c = (*mb_ptr2char)(buf);
1744 #endif
1746 break;
1750 #ifdef FEAT_EVAL
1752 * In the main loop "may_garbage_collect" can be set to do garbage
1753 * collection in the first next vgetc(). It's disabled after that to
1754 * avoid internally used Lists and Dicts to be freed.
1756 may_garbage_collect = FALSE;
1757 #endif
1759 return c;
1763 * Like vgetc(), but never return a NUL when called recursively, get a key
1764 * directly from the user (ignoring typeahead).
1767 safe_vgetc()
1769 int c;
1771 c = vgetc();
1772 if (c == NUL)
1773 c = get_keystroke();
1774 return c;
1778 * Like safe_vgetc(), but loop to handle K_IGNORE.
1779 * Also ignore scrollbar events.
1782 plain_vgetc()
1784 int c;
1788 c = safe_vgetc();
1789 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1790 return c;
1794 * Check if a character is available, such that vgetc() will not block.
1795 * If the next character is a special character or multi-byte, the returned
1796 * character is not valid!.
1799 vpeekc()
1801 if (old_char != -1)
1802 return old_char;
1803 return vgetorpeek(FALSE);
1806 #if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1808 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1809 * codes.
1812 vpeekc_nomap()
1814 int c;
1816 ++no_mapping;
1817 ++allow_keys;
1818 c = vpeekc();
1819 --no_mapping;
1820 --allow_keys;
1821 return c;
1823 #endif
1825 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1827 * Check if any character is available, also half an escape sequence.
1828 * Trick: when no typeahead found, but there is something in the typeahead
1829 * buffer, it must be an ESC that is recognized as the start of a key code.
1832 vpeekc_any()
1834 int c;
1836 c = vpeekc();
1837 if (c == NUL && typebuf.tb_len > 0)
1838 c = ESC;
1839 return c;
1841 #endif
1844 * Call vpeekc() without causing anything to be mapped.
1845 * Return TRUE if a character is available, FALSE otherwise.
1848 char_avail()
1850 int retval;
1852 ++no_mapping;
1853 retval = vpeekc();
1854 --no_mapping;
1855 return (retval != NUL);
1858 void
1859 vungetc(c) /* unget one character (can only be done once!) */
1860 int c;
1862 old_char = c;
1863 old_mod_mask = mod_mask;
1867 * get a character:
1868 * 1. from the stuffbuffer
1869 * This is used for abbreviated commands like "D" -> "d$".
1870 * Also used to redo a command for ".".
1871 * 2. from the typeahead buffer
1872 * Stores text obtained previously but not used yet.
1873 * Also stores the result of mappings.
1874 * Also used for the ":normal" command.
1875 * 3. from the user
1876 * This may do a blocking wait if "advance" is TRUE.
1878 * if "advance" is TRUE (vgetc()):
1879 * really get the character.
1880 * KeyTyped is set to TRUE in the case the user typed the key.
1881 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1882 * if "advance" is FALSE (vpeekc()):
1883 * just look whether there is a character available.
1885 * When "no_mapping" is zero, checks for mappings in the current mode.
1886 * Only returns one byte (of a multi-byte character).
1887 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1889 static int
1890 vgetorpeek(advance)
1891 int advance;
1893 int c, c1;
1894 int keylen;
1895 char_u *s;
1896 mapblock_T *mp;
1897 #ifdef FEAT_LOCALMAP
1898 mapblock_T *mp2;
1899 #endif
1900 mapblock_T *mp_match;
1901 int mp_match_len = 0;
1902 int timedout = FALSE; /* waited for more than 1 second
1903 for mapping to complete */
1904 int mapdepth = 0; /* check for recursive mapping */
1905 int mode_deleted = FALSE; /* set when mode has been deleted */
1906 int local_State;
1907 int mlen;
1908 int max_mlen;
1909 int i;
1910 #ifdef FEAT_CMDL_INFO
1911 int new_wcol, new_wrow;
1912 #endif
1913 #ifdef FEAT_GUI
1914 # ifdef FEAT_MENU
1915 int idx;
1916 # endif
1917 int shape_changed = FALSE; /* adjusted cursor shape */
1918 #endif
1919 int n;
1920 #ifdef FEAT_LANGMAP
1921 int nolmaplen;
1922 #endif
1923 int old_wcol, old_wrow;
1924 int wait_tb_len;
1927 * This function doesn't work very well when called recursively. This may
1928 * happen though, because of:
1929 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1930 * there is a character available, which calls this function. In that
1931 * case we must return NUL, to indicate no character is available.
1932 * 2. A GUI callback function writes to the screen, causing a
1933 * wait_return().
1934 * Using ":normal" can also do this, but it saves the typeahead buffer,
1935 * thus it should be OK. But don't get a key from the user then.
1937 if (vgetc_busy > 0
1938 #ifdef FEAT_EX_EXTRA
1939 && ex_normal_busy == 0
1940 #endif
1942 return NUL;
1944 local_State = get_real_state();
1946 ++vgetc_busy;
1948 if (advance)
1949 KeyStuffed = FALSE;
1951 init_typebuf();
1952 start_stuff();
1953 if (advance && typebuf.tb_maplen == 0)
1954 Exec_reg = FALSE;
1958 * get a character: 1. from the stuffbuffer
1960 if (typeahead_char != 0)
1962 c = typeahead_char;
1963 if (advance)
1964 typeahead_char = 0;
1966 else
1967 c = read_stuff(advance);
1968 if (c != NUL && !got_int)
1970 if (advance)
1972 /* KeyTyped = FALSE; When the command that stuffed something
1973 * was typed, behave like the stuffed command was typed.
1974 * needed for CTRL-W CTRl-] to open a fold, for example. */
1975 KeyStuffed = TRUE;
1977 if (typebuf.tb_no_abbr_cnt == 0)
1978 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1980 else
1983 * Loop until we either find a matching mapped key, or we
1984 * are sure that it is not a mapped key.
1985 * If a mapped key sequence is found we go back to the start to
1986 * try re-mapping.
1988 for (;;)
1991 * ui_breakcheck() is slow, don't use it too often when
1992 * inside a mapping. But call it each time for typed
1993 * characters.
1995 if (typebuf.tb_maplen)
1996 line_breakcheck();
1997 else
1998 ui_breakcheck(); /* check for CTRL-C */
1999 keylen = 0;
2000 if (got_int)
2002 /* flush all input */
2003 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
2004 typebuf.tb_change_cnt);
2006 * If inchar() returns TRUE (script file was active) or we
2007 * are inside a mapping, get out of insert mode.
2008 * Otherwise we behave like having gotten a CTRL-C.
2009 * As a result typing CTRL-C in insert mode will
2010 * really insert a CTRL-C.
2012 if ((c || typebuf.tb_maplen)
2013 && (State & (INSERT + CMDLINE)))
2014 c = ESC;
2015 else
2016 c = Ctrl_C;
2017 flush_buffers(TRUE); /* flush all typeahead */
2019 if (advance)
2021 /* Also record this character, it might be needed to
2022 * get out of Insert mode. */
2023 *typebuf.tb_buf = c;
2024 gotchars(typebuf.tb_buf, 1);
2026 cmd_silent = FALSE;
2028 break;
2030 else if (typebuf.tb_len > 0)
2033 * Check for a mappable key sequence.
2034 * Walk through one maphash[] list until we find an
2035 * entry that matches.
2037 * Don't look for mappings if:
2038 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2039 * - maphash_valid not set: no mappings present.
2040 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2041 * - in insert or cmdline mode and 'paste' option set
2042 * - waiting for "hit return to continue" and CR or SPACE
2043 * typed
2044 * - waiting for a char with --more--
2045 * - in Ctrl-X mode, and we get a valid char for that mode
2047 mp = NULL;
2048 max_mlen = 0;
2049 c1 = typebuf.tb_buf[typebuf.tb_off];
2050 if (no_mapping == 0 && maphash_valid
2051 && (no_zero_mapping == 0 || c1 != '0')
2052 && (typebuf.tb_maplen == 0
2053 || (p_remap
2054 && (typebuf.tb_noremap[typebuf.tb_off]
2055 & (RM_NONE|RM_ABBR)) == 0))
2056 && !(p_paste && (State & (INSERT + CMDLINE)))
2057 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2058 && State != ASKMORE
2059 && State != CONFIRM
2060 #ifdef FEAT_INS_EXPAND
2061 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
2062 || ((compl_cont_status & CONT_LOCAL)
2063 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2064 #endif
2067 #ifdef FEAT_LANGMAP
2068 if (c1 == K_SPECIAL)
2069 nolmaplen = 2;
2070 else
2072 LANGMAP_ADJUST(c1, TRUE);
2073 nolmaplen = 0;
2075 #endif
2076 #ifdef FEAT_LOCALMAP
2077 /* First try buffer-local mappings. */
2078 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2079 mp2 = maphash[MAP_HASH(local_State, c1)];
2080 if (mp == NULL)
2082 mp = mp2;
2083 mp2 = NULL;
2085 #else
2086 mp = maphash[MAP_HASH(local_State, c1)];
2087 #endif
2089 * Loop until a partly matching mapping is found or
2090 * all (local) mappings have been checked.
2091 * The longest full match is remembered in "mp_match".
2092 * A full match is only accepted if there is no partly
2093 * match, so "aa" and "aaa" can both be mapped.
2095 mp_match = NULL;
2096 mp_match_len = 0;
2097 for ( ; mp != NULL;
2098 #ifdef FEAT_LOCALMAP
2099 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2100 #endif
2101 (mp = mp->m_next))
2104 * Only consider an entry if the first character
2105 * matches and it is for the current state.
2106 * Skip ":lmap" mappings if keys were mapped.
2108 if (mp->m_keys[0] == c1
2109 && (mp->m_mode & local_State)
2110 && ((mp->m_mode & LANGMAP) == 0
2111 || typebuf.tb_maplen == 0))
2113 #ifdef FEAT_LANGMAP
2114 int nomap = nolmaplen;
2115 int c2;
2116 #endif
2117 /* find the match length of this mapping */
2118 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2120 #ifdef FEAT_LANGMAP
2121 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2122 if (nomap > 0)
2123 --nomap;
2124 else if (c2 == K_SPECIAL)
2125 nomap = 2;
2126 else
2127 LANGMAP_ADJUST(c2, TRUE);
2128 if (mp->m_keys[mlen] != c2)
2129 #else
2130 if (mp->m_keys[mlen] !=
2131 typebuf.tb_buf[typebuf.tb_off + mlen])
2132 #endif
2133 break;
2136 #ifdef FEAT_MBYTE
2137 /* Don't allow mapping the first byte(s) of a
2138 * multi-byte char. Happens when mapping
2139 * <M-a> and then changing 'encoding'. */
2140 if (has_mbyte && MB_BYTE2LEN(c1)
2141 > (*mb_ptr2len)(mp->m_keys))
2142 mlen = 0;
2143 #endif
2145 * Check an entry whether it matches.
2146 * - Full match: mlen == keylen
2147 * - Partly match: mlen == typebuf.tb_len
2149 keylen = mp->m_keylen;
2150 if (mlen == keylen
2151 || (mlen == typebuf.tb_len
2152 && typebuf.tb_len < keylen))
2155 * If only script-local mappings are
2156 * allowed, check if the mapping starts
2157 * with K_SNR.
2159 s = typebuf.tb_noremap + typebuf.tb_off;
2160 if (*s == RM_SCRIPT
2161 && (mp->m_keys[0] != K_SPECIAL
2162 || mp->m_keys[1] != KS_EXTRA
2163 || mp->m_keys[2]
2164 != (int)KE_SNR))
2165 continue;
2167 * If one of the typed keys cannot be
2168 * remapped, skip the entry.
2170 for (n = mlen; --n >= 0; )
2171 if (*s++ & (RM_NONE|RM_ABBR))
2172 break;
2173 if (n >= 0)
2174 continue;
2176 if (keylen > typebuf.tb_len)
2178 if (!timedout)
2180 /* break at a partly match */
2181 keylen = KL_PART_MAP;
2182 break;
2185 else if (keylen > mp_match_len)
2187 /* found a longer match */
2188 mp_match = mp;
2189 mp_match_len = keylen;
2192 else
2193 /* No match; may have to check for
2194 * termcode at next character. */
2195 if (max_mlen < mlen)
2196 max_mlen = mlen;
2200 /* If no partly match found, use the longest full
2201 * match. */
2202 if (keylen != KL_PART_MAP)
2204 mp = mp_match;
2205 keylen = mp_match_len;
2209 /* Check for match with 'pastetoggle' */
2210 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2212 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2213 ++mlen)
2214 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2215 + mlen])
2216 break;
2217 if (p_pt[mlen] == NUL) /* match */
2219 /* write chars to script file(s) */
2220 if (mlen > typebuf.tb_maplen)
2221 gotchars(typebuf.tb_buf + typebuf.tb_off
2222 + typebuf.tb_maplen,
2223 mlen - typebuf.tb_maplen);
2225 del_typebuf(mlen, 0); /* remove the chars */
2226 set_option_value((char_u *)"paste",
2227 (long)!p_paste, NULL, 0);
2228 if (!(State & INSERT))
2230 msg_col = 0;
2231 msg_row = Rows - 1;
2232 msg_clr_eos(); /* clear ruler */
2234 showmode();
2235 setcursor();
2236 continue;
2238 /* Need more chars for partly match. */
2239 if (mlen == typebuf.tb_len)
2240 keylen = KL_PART_KEY;
2241 else if (max_mlen < mlen)
2242 /* no match, may have to check for termcode at
2243 * next character */
2244 max_mlen = mlen + 1;
2247 if ((mp == NULL || max_mlen >= mp_match_len)
2248 && keylen != KL_PART_MAP)
2250 int save_keylen = keylen;
2253 * When no matching mapping found or found a
2254 * non-matching mapping that matches at least what the
2255 * matching mapping matched:
2256 * Check if we have a terminal code, when:
2257 * mapping is allowed,
2258 * keys have not been mapped,
2259 * and not an ESC sequence, not in insert mode or
2260 * p_ek is on,
2261 * and when not timed out,
2263 if ((no_mapping == 0 || allow_keys != 0)
2264 && (typebuf.tb_maplen == 0
2265 || (p_remap && typebuf.tb_noremap[
2266 typebuf.tb_off] == RM_YES))
2267 && !timedout)
2269 keylen = check_termcode(max_mlen + 1, NULL, 0);
2271 /* If no termcode matched but 'pastetoggle'
2272 * matched partially it's like an incomplete key
2273 * sequence. */
2274 if (keylen == 0 && save_keylen == KL_PART_KEY)
2275 keylen = KL_PART_KEY;
2278 * When getting a partial match, but the last
2279 * characters were not typed, don't wait for a
2280 * typed character to complete the termcode.
2281 * This helps a lot when a ":normal" command ends
2282 * in an ESC.
2284 if (keylen < 0
2285 && typebuf.tb_len == typebuf.tb_maplen)
2286 keylen = 0;
2288 else
2289 keylen = 0;
2290 if (keylen == 0) /* no matching terminal code */
2292 #ifdef AMIGA /* check for window bounds report */
2293 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2294 typebuf.tb_off] & 0xff) == CSI)
2296 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2297 s < typebuf.tb_buf + typebuf.tb_off
2298 + typebuf.tb_len
2299 && (VIM_ISDIGIT(*s) || *s == ';'
2300 || *s == ' ');
2301 ++s)
2303 if (*s == 'r' || *s == '|') /* found one */
2305 del_typebuf((int)(s + 1 -
2306 (typebuf.tb_buf + typebuf.tb_off)), 0);
2307 /* get size and redraw screen */
2308 shell_resized();
2309 continue;
2311 if (*s == NUL) /* need more characters */
2312 keylen = KL_PART_KEY;
2314 if (keylen >= 0)
2315 #endif
2316 /* When there was a matching mapping and no
2317 * termcode could be replaced after another one,
2318 * use that mapping (loop around). If there was
2319 * no mapping use the character from the
2320 * typeahead buffer right here. */
2321 if (mp == NULL)
2324 * get a character: 2. from the typeahead buffer
2326 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2327 if (advance) /* remove chars from tb_buf */
2329 cmd_silent = (typebuf.tb_silent > 0);
2330 if (typebuf.tb_maplen > 0)
2331 KeyTyped = FALSE;
2332 else
2334 KeyTyped = TRUE;
2335 /* write char to script file(s) */
2336 gotchars(typebuf.tb_buf
2337 + typebuf.tb_off, 1);
2339 KeyNoremap = typebuf.tb_noremap[
2340 typebuf.tb_off];
2341 del_typebuf(1, 0);
2343 break; /* got character, break for loop */
2346 if (keylen > 0) /* full matching terminal code */
2348 #if defined(FEAT_GUI) && defined(FEAT_MENU)
2349 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2350 && typebuf.tb_buf[typebuf.tb_off + 1]
2351 == KS_MENU)
2354 * Using a menu may cause a break in undo!
2355 * It's like using gotchars(), but without
2356 * recording or writing to a script file.
2358 may_sync_undo();
2359 del_typebuf(3, 0);
2360 idx = get_menu_index(current_menu, local_State);
2361 if (idx != MENU_INDEX_INVALID)
2363 # ifdef FEAT_VISUAL
2365 * In Select mode and a Visual mode menu
2366 * is used: Switch to Visual mode
2367 * temporarily. Append K_SELECT to switch
2368 * back to Select mode.
2370 if (VIsual_active && VIsual_select
2371 && (current_menu->modes & VISUAL))
2373 VIsual_select = FALSE;
2374 (void)ins_typebuf(K_SELECT_STRING,
2375 REMAP_NONE, 0, TRUE, FALSE);
2377 # endif
2378 ins_typebuf(current_menu->strings[idx],
2379 current_menu->noremap[idx],
2380 0, TRUE,
2381 current_menu->silent[idx]);
2384 #endif /* FEAT_GUI && FEAT_MENU */
2385 continue; /* try mapping again */
2388 /* Partial match: get some more characters. When a
2389 * matching mapping was found use that one. */
2390 if (mp == NULL || keylen < 0)
2391 keylen = KL_PART_KEY;
2392 else
2393 keylen = mp_match_len;
2396 /* complete match */
2397 if (keylen >= 0 && keylen <= typebuf.tb_len)
2399 /* write chars to script file(s) */
2400 if (keylen > typebuf.tb_maplen)
2401 gotchars(typebuf.tb_buf + typebuf.tb_off
2402 + typebuf.tb_maplen,
2403 keylen - typebuf.tb_maplen);
2405 cmd_silent = (typebuf.tb_silent > 0);
2406 del_typebuf(keylen, 0); /* remove the mapped keys */
2409 * Put the replacement string in front of mapstr.
2410 * The depth check catches ":map x y" and ":map y x".
2412 if (++mapdepth >= p_mmd)
2414 EMSG(_("E223: recursive mapping"));
2415 if (State & CMDLINE)
2416 redrawcmdline();
2417 else
2418 setcursor();
2419 flush_buffers(FALSE);
2420 mapdepth = 0; /* for next one */
2421 c = -1;
2422 break;
2425 #ifdef FEAT_VISUAL
2427 * In Select mode and a Visual mode mapping is used:
2428 * Switch to Visual mode temporarily. Append K_SELECT
2429 * to switch back to Select mode.
2431 if (VIsual_active && VIsual_select
2432 && (mp->m_mode & VISUAL))
2434 VIsual_select = FALSE;
2435 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2436 0, TRUE, FALSE);
2438 #endif
2440 #ifdef FEAT_EVAL
2442 * Handle ":map <expr>": evaluate the {rhs} as an
2443 * expression. Save and restore the typeahead so that
2444 * getchar() can be used. Also save and restore the
2445 * command line for "normal :".
2447 if (mp->m_expr)
2449 tasave_T tabuf;
2450 int save_vgetc_busy = vgetc_busy;
2452 save_typeahead(&tabuf);
2453 if (tabuf.typebuf_valid)
2455 vgetc_busy = 0;
2456 s = eval_map_expr(mp->m_str);
2457 vgetc_busy = save_vgetc_busy;
2459 else
2460 s = NULL;
2461 restore_typeahead(&tabuf);
2463 else
2464 #endif
2465 s = mp->m_str;
2468 * Insert the 'to' part in the typebuf.tb_buf.
2469 * If 'from' field is the same as the start of the
2470 * 'to' field, don't remap the first character (but do
2471 * allow abbreviations).
2472 * If m_noremap is set, don't remap the whole 'to'
2473 * part.
2475 if (s == NULL)
2476 i = FAIL;
2477 else
2479 i = ins_typebuf(s,
2480 mp->m_noremap != REMAP_YES
2481 ? mp->m_noremap
2482 : STRNCMP(s, mp->m_keys,
2483 (size_t)keylen) != 0
2484 ? REMAP_YES : REMAP_SKIP,
2485 0, TRUE, cmd_silent || mp->m_silent);
2486 #ifdef FEAT_EVAL
2487 if (mp->m_expr)
2488 vim_free(s);
2489 #endif
2491 if (i == FAIL)
2493 c = -1;
2494 break;
2496 continue;
2501 * get a character: 3. from the user - handle <Esc> in Insert mode
2504 * special case: if we get an <ESC> in insert mode and there
2505 * are no more characters at once, we pretend to go out of
2506 * insert mode. This prevents the one second delay after
2507 * typing an <ESC>. If we get something after all, we may
2508 * have to redisplay the mode. That the cursor is in the wrong
2509 * place does not matter.
2511 c = 0;
2512 #ifdef FEAT_CMDL_INFO
2513 new_wcol = curwin->w_wcol;
2514 new_wrow = curwin->w_wrow;
2515 #endif
2516 if ( advance
2517 && typebuf.tb_len == 1
2518 && typebuf.tb_buf[typebuf.tb_off] == ESC
2519 && !no_mapping
2520 #ifdef FEAT_EX_EXTRA
2521 && ex_normal_busy == 0
2522 #endif
2523 && typebuf.tb_maplen == 0
2524 && (State & INSERT)
2525 && (p_timeout || (keylen == KL_PART_KEY && p_ttimeout))
2526 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2527 + typebuf.tb_len, 3, 25L,
2528 typebuf.tb_change_cnt)) == 0)
2530 colnr_T col = 0, vcol;
2531 char_u *ptr;
2533 if (mode_displayed)
2535 unshowmode(TRUE);
2536 mode_deleted = TRUE;
2538 #ifdef FEAT_GUI
2539 /* may show different cursor shape */
2540 if (gui.in_use)
2542 int save_State;
2544 save_State = State;
2545 State = NORMAL;
2546 gui_update_cursor(TRUE, FALSE);
2547 State = save_State;
2548 shape_changed = TRUE;
2550 #endif
2551 validate_cursor();
2552 old_wcol = curwin->w_wcol;
2553 old_wrow = curwin->w_wrow;
2555 /* move cursor left, if possible */
2556 if (curwin->w_cursor.col != 0)
2558 if (curwin->w_wcol > 0)
2560 if (did_ai)
2563 * We are expecting to truncate the trailing
2564 * white-space, so find the last non-white
2565 * character -- webb
2567 col = vcol = curwin->w_wcol = 0;
2568 ptr = ml_get_curline();
2569 while (col < curwin->w_cursor.col)
2571 if (!vim_iswhite(ptr[col]))
2572 curwin->w_wcol = vcol;
2573 vcol += lbr_chartabsize(ptr + col,
2574 (colnr_T)vcol);
2575 #ifdef FEAT_MBYTE
2576 if (has_mbyte)
2577 col += (*mb_ptr2len)(ptr + col);
2578 else
2579 #endif
2580 ++col;
2582 curwin->w_wrow = curwin->w_cline_row
2583 + curwin->w_wcol / W_WIDTH(curwin);
2584 curwin->w_wcol %= W_WIDTH(curwin);
2585 curwin->w_wcol += curwin_col_off();
2586 #ifdef FEAT_MBYTE
2587 col = 0; /* no correction needed */
2588 #endif
2590 else
2592 --curwin->w_wcol;
2593 #ifdef FEAT_MBYTE
2594 col = curwin->w_cursor.col - 1;
2595 #endif
2598 else if (curwin->w_p_wrap && curwin->w_wrow)
2600 --curwin->w_wrow;
2601 curwin->w_wcol = W_WIDTH(curwin) - 1;
2602 #ifdef FEAT_MBYTE
2603 col = curwin->w_cursor.col - 1;
2604 #endif
2606 #ifdef FEAT_MBYTE
2607 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2609 /* Correct when the cursor is on the right halve
2610 * of a double-wide character. */
2611 ptr = ml_get_curline();
2612 col -= (*mb_head_off)(ptr, ptr + col);
2613 if ((*mb_ptr2cells)(ptr + col) > 1)
2614 --curwin->w_wcol;
2616 #endif
2618 setcursor();
2619 out_flush();
2620 #ifdef FEAT_CMDL_INFO
2621 new_wcol = curwin->w_wcol;
2622 new_wrow = curwin->w_wrow;
2623 #endif
2624 curwin->w_wcol = old_wcol;
2625 curwin->w_wrow = old_wrow;
2627 if (c < 0)
2628 continue; /* end of input script reached */
2629 typebuf.tb_len += c;
2631 /* buffer full, don't map */
2632 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2634 timedout = TRUE;
2635 continue;
2638 #ifdef FEAT_EX_EXTRA
2639 if (ex_normal_busy > 0)
2641 # ifdef FEAT_CMDWIN
2642 static int tc = 0;
2643 # endif
2645 /* No typeahead left and inside ":normal". Must return
2646 * something to avoid getting stuck. When an incomplete
2647 * mapping is present, behave like it timed out. */
2648 if (typebuf.tb_len > 0)
2650 timedout = TRUE;
2651 continue;
2653 /* When 'insertmode' is set, ESC just beeps in Insert
2654 * mode. Use CTRL-L to make edit() return.
2655 * For the command line only CTRL-C always breaks it.
2656 * For the cmdline window: Alternate between ESC and
2657 * CTRL-C: ESC for most situations and CTRL-C to close the
2658 * cmdline window. */
2659 if (p_im && (State & INSERT))
2660 c = Ctrl_L;
2661 else if ((State & CMDLINE)
2662 # ifdef FEAT_CMDWIN
2663 || (cmdwin_type > 0 && tc == ESC)
2664 # endif
2666 c = Ctrl_C;
2667 else
2668 c = ESC;
2669 # ifdef FEAT_CMDWIN
2670 tc = c;
2671 # endif
2672 break;
2674 #endif
2677 * get a character: 3. from the user - update display
2679 /* In insert mode a screen update is skipped when characters
2680 * are still available. But when those available characters
2681 * are part of a mapping, and we are going to do a blocking
2682 * wait here. Need to update the screen to display the
2683 * changed text so far. */
2684 if ((State & INSERT) && advance && must_redraw != 0)
2686 update_screen(0);
2687 setcursor(); /* put cursor back where it belongs */
2691 * If we have a partial match (and are going to wait for more
2692 * input from the user), show the partially matched characters
2693 * to the user with showcmd.
2695 #ifdef FEAT_CMDL_INFO
2696 i = 0;
2697 #endif
2698 c1 = 0;
2699 if (typebuf.tb_len > 0 && advance && !exmode_active)
2701 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2702 && State != HITRETURN)
2704 /* this looks nice when typing a dead character map */
2705 if (State & INSERT
2706 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2707 + typebuf.tb_len - 1) == 1)
2709 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2710 + typebuf.tb_len - 1], FALSE);
2711 setcursor(); /* put cursor back where it belongs */
2712 c1 = 1;
2714 #ifdef FEAT_CMDL_INFO
2715 /* need to use the col and row from above here */
2716 old_wcol = curwin->w_wcol;
2717 old_wrow = curwin->w_wrow;
2718 curwin->w_wcol = new_wcol;
2719 curwin->w_wrow = new_wrow;
2720 push_showcmd();
2721 if (typebuf.tb_len > SHOWCMD_COLS)
2722 i = typebuf.tb_len - SHOWCMD_COLS;
2723 while (i < typebuf.tb_len)
2724 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2725 + i++]);
2726 curwin->w_wcol = old_wcol;
2727 curwin->w_wrow = old_wrow;
2728 #endif
2731 /* this looks nice when typing a dead character map */
2732 if ((State & CMDLINE)
2733 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2734 && cmdline_star == 0
2735 #endif
2736 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2737 + typebuf.tb_len - 1) == 1)
2739 putcmdline(typebuf.tb_buf[typebuf.tb_off
2740 + typebuf.tb_len - 1], FALSE);
2741 c1 = 1;
2746 * get a character: 3. from the user - get it
2748 wait_tb_len = typebuf.tb_len;
2749 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2750 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2751 !advance
2753 : ((typebuf.tb_len == 0
2754 || !(p_timeout || (p_ttimeout
2755 && keylen == KL_PART_KEY)))
2756 ? -1L
2757 : ((keylen == KL_PART_KEY && p_ttm >= 0)
2758 ? p_ttm
2759 : p_tm)), typebuf.tb_change_cnt);
2761 #ifdef FEAT_CMDL_INFO
2762 if (i != 0)
2763 pop_showcmd();
2764 #endif
2765 if (c1 == 1)
2767 if (State & INSERT)
2768 edit_unputchar();
2769 if (State & CMDLINE)
2770 unputcmdline();
2771 setcursor(); /* put cursor back where it belongs */
2774 if (c < 0)
2775 continue; /* end of input script reached */
2776 if (c == NUL) /* no character available */
2778 if (!advance)
2779 break;
2780 if (wait_tb_len > 0) /* timed out */
2782 timedout = TRUE;
2783 continue;
2786 else
2787 { /* allow mapping for just typed characters */
2788 while (typebuf.tb_buf[typebuf.tb_off
2789 + typebuf.tb_len] != NUL)
2790 typebuf.tb_noremap[typebuf.tb_off
2791 + typebuf.tb_len++] = RM_YES;
2792 #ifdef USE_IM_CONTROL
2793 /* Get IM status right after getting keys, not after the
2794 * timeout for a mapping (focus may be lost by then). */
2795 vgetc_im_active = im_get_status();
2796 #endif
2798 } /* for (;;) */
2799 } /* if (!character from stuffbuf) */
2801 /* if advance is FALSE don't loop on NULs */
2802 } while (c < 0 || (advance && c == NUL));
2805 * The "INSERT" message is taken care of here:
2806 * if we return an ESC to exit insert mode, the message is deleted
2807 * if we don't return an ESC but deleted the message before, redisplay it
2809 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
2811 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
2813 if (typebuf.tb_len && !KeyTyped)
2814 redraw_cmdline = TRUE; /* delete mode later */
2815 else
2816 unshowmode(FALSE);
2818 else if (c != ESC && mode_deleted)
2820 if (typebuf.tb_len && !KeyTyped)
2821 redraw_cmdline = TRUE; /* show mode later */
2822 else
2823 showmode();
2826 #ifdef FEAT_GUI
2827 /* may unshow different cursor shape */
2828 if (gui.in_use && shape_changed)
2829 gui_update_cursor(TRUE, FALSE);
2830 #endif
2832 --vgetc_busy;
2834 return c;
2838 * inchar() - get one character from
2839 * 1. a scriptfile
2840 * 2. the keyboard
2842 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2843 * NUL terminated (buffer length must be 'maxlen' + 1).
2844 * Minimum for "maxlen" is 3!!!!
2846 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2847 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2848 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2849 * otherwise.
2851 * If we got an interrupt all input is read until none is available.
2853 * If wait_time == 0 there is no waiting for the char.
2854 * If wait_time == n we wait for n msec for a character to arrive.
2855 * If wait_time == -1 we wait forever for a character to arrive.
2857 * Return the number of obtained characters.
2858 * Return -1 when end of input script reached.
2861 inchar(buf, maxlen, wait_time, tb_change_cnt)
2862 char_u *buf;
2863 int maxlen;
2864 long wait_time; /* milli seconds */
2865 int tb_change_cnt;
2867 int len = 0; /* init for GCC */
2868 int retesc = FALSE; /* return ESC with gotint */
2869 int script_char;
2871 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2873 cursor_on();
2874 out_flush();
2875 #ifdef FEAT_GUI
2876 if (gui.in_use)
2878 gui_update_cursor(FALSE, FALSE);
2879 # ifdef FEAT_MOUSESHAPE
2880 if (postponed_mouseshape)
2881 update_mouseshape(-1);
2882 # endif
2884 #endif
2888 * Don't reset these when at the hit-return prompt, otherwise a endless
2889 * recursive loop may result (write error in swapfile, hit-return, timeout
2890 * on char wait, flush swapfile, write error....).
2892 if (State != HITRETURN)
2894 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2895 did_swapwrite_msg = FALSE; /* display swap file write error again */
2897 undo_off = FALSE; /* restart undo now */
2900 * Get a character from a script file if there is one.
2901 * If interrupted: Stop reading script files, close them all.
2903 script_char = -1;
2904 while (scriptin[curscript] != NULL && script_char < 0
2905 #ifdef FEAT_EVAL
2906 && !ignore_script
2907 #endif
2911 #if defined(FEAT_NETBEANS_INTG)
2912 /* Process the queued netbeans messages. */
2913 netbeans_parse_messages();
2914 #endif
2916 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2918 /* Reached EOF.
2919 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2920 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2921 closescript();
2923 * When reading script file is interrupted, return an ESC to get
2924 * back to normal mode.
2925 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2927 if (got_int)
2928 retesc = TRUE;
2929 else
2930 return -1;
2932 else
2934 buf[0] = script_char;
2935 len = 1;
2939 if (script_char < 0) /* did not get a character from script */
2942 * If we got an interrupt, skip all previously typed characters and
2943 * return TRUE if quit reading script file.
2944 * Stop reading typeahead when a single CTRL-C was read,
2945 * fill_input_buf() returns this when not able to read from stdin.
2946 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2947 * and buf may be pointing inside typebuf.tb_buf[].
2949 if (got_int)
2951 #define DUM_LEN MAXMAPLEN * 3 + 3
2952 char_u dum[DUM_LEN + 1];
2954 for (;;)
2956 len = ui_inchar(dum, DUM_LEN, 0L, 0);
2957 if (len == 0 || (len == 1 && dum[0] == 3))
2958 break;
2960 return retesc;
2964 * Always flush the output characters when getting input characters
2965 * from the user.
2967 out_flush();
2970 * Fill up to a third of the buffer, because each character may be
2971 * tripled below.
2973 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
2976 if (typebuf_changed(tb_change_cnt))
2977 return 0;
2979 return fix_input_buffer(buf, len, script_char >= 0);
2983 * Fix typed characters for use by vgetc() and check_termcode().
2984 * buf[] must have room to triple the number of bytes!
2985 * Returns the new length.
2988 fix_input_buffer(buf, len, script)
2989 char_u *buf;
2990 int len;
2991 int script; /* TRUE when reading from a script */
2993 int i;
2994 char_u *p = buf;
2997 * Two characters are special: NUL and K_SPECIAL.
2998 * When compiled With the GUI CSI is also special.
2999 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3000 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3001 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
3002 * Don't replace K_SPECIAL when reading a script file.
3004 for (i = len; --i >= 0; ++p)
3006 #ifdef FEAT_GUI
3007 /* When the GUI is used any character can come after a CSI, don't
3008 * escape it. */
3009 if (gui.in_use && p[0] == CSI && i >= 2)
3011 p += 2;
3012 i -= 2;
3014 /* When the GUI is not used CSI needs to be escaped. */
3015 else if (!gui.in_use && p[0] == CSI)
3017 mch_memmove(p + 3, p + 1, (size_t)i);
3018 *p++ = K_SPECIAL;
3019 *p++ = KS_EXTRA;
3020 *p = (int)KE_CSI;
3021 len += 2;
3023 else
3024 #endif
3025 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
3026 #ifdef FEAT_AUTOCMD
3027 /* timeout may generate K_CURSORHOLD */
3028 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
3029 #endif
3030 #if defined(WIN3264) && !defined(FEAT_GUI)
3031 /* Win32 console passes modifiers */
3032 && (i < 2 || p[1] != KS_MODIFIER)
3033 #endif
3036 mch_memmove(p + 3, p + 1, (size_t)i);
3037 p[2] = K_THIRD(p[0]);
3038 p[1] = K_SECOND(p[0]);
3039 p[0] = K_SPECIAL;
3040 p += 2;
3041 len += 2;
3044 *p = NUL; /* add trailing NUL */
3045 return len;
3048 #if defined(USE_INPUT_BUF) || defined(PROTO)
3050 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3051 * Normally the input buffer would be sufficient, but the server_to_input_buf()
3052 * or feedkeys() may insert characters in the typeahead buffer while we are
3053 * waiting for input to arrive.
3056 input_available()
3058 return (!vim_is_input_buf_empty()
3059 # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3060 || typebuf_was_filled
3061 # endif
3064 #endif
3067 * map[!] : show all key mappings
3068 * map[!] {lhs} : show key mapping for {lhs}
3069 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3070 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3071 * unmap[!] {lhs} : remove key mapping for {lhs}
3072 * abbr : show all abbreviations
3073 * abbr {lhs} : show abbreviations for {lhs}
3074 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3075 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3076 * unabbr {lhs} : remove abbreviation for {lhs}
3078 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3080 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3081 * it will be modified.
3083 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
3084 * for :map! mode is INSERT + CMDLINE
3085 * for :cmap mode is CMDLINE
3086 * for :imap mode is INSERT
3087 * for :lmap mode is LANGMAP
3088 * for :nmap mode is NORMAL
3089 * for :vmap mode is VISUAL + SELECTMODE
3090 * for :xmap mode is VISUAL
3091 * for :smap mode is SELECTMODE
3092 * for :omap mode is OP_PENDING
3094 * for :abbr mode is INSERT + CMDLINE
3095 * for :iabbr mode is INSERT
3096 * for :cabbr mode is CMDLINE
3098 * Return 0 for success
3099 * 1 for invalid arguments
3100 * 2 for no match
3101 * 4 for out of mem
3102 * 5 for entry not unique
3105 do_map(maptype, arg, mode, abbrev)
3106 int maptype;
3107 char_u *arg;
3108 int mode;
3109 int abbrev; /* not a mapping but an abbreviation */
3111 char_u *keys;
3112 mapblock_T *mp, **mpp;
3113 char_u *rhs;
3114 char_u *p;
3115 int n;
3116 int len = 0; /* init for GCC */
3117 char_u *newstr;
3118 int hasarg;
3119 int haskey;
3120 int did_it = FALSE;
3121 #ifdef FEAT_LOCALMAP
3122 int did_local = FALSE;
3123 #endif
3124 int round;
3125 char_u *keys_buf = NULL;
3126 char_u *arg_buf = NULL;
3127 int retval = 0;
3128 int do_backslash;
3129 int hash;
3130 int new_hash;
3131 mapblock_T **abbr_table;
3132 mapblock_T **map_table;
3133 int unique = FALSE;
3134 int silent = FALSE;
3135 int special = FALSE;
3136 #ifdef FEAT_EVAL
3137 int expr = FALSE;
3138 #endif
3139 int noremap;
3141 keys = arg;
3142 map_table = maphash;
3143 abbr_table = &first_abbr;
3145 /* For ":noremap" don't remap, otherwise do remap. */
3146 if (maptype == 2)
3147 noremap = REMAP_NONE;
3148 else
3149 noremap = REMAP_YES;
3151 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
3152 for (;;)
3154 #ifdef FEAT_LOCALMAP
3156 * Check for "<buffer>": mapping local to buffer.
3158 if (STRNCMP(keys, "<buffer>", 8) == 0)
3160 keys = skipwhite(keys + 8);
3161 map_table = curbuf->b_maphash;
3162 abbr_table = &curbuf->b_first_abbr;
3163 continue;
3165 #endif
3168 * Check for "<silent>": don't echo commands.
3170 if (STRNCMP(keys, "<silent>", 8) == 0)
3172 keys = skipwhite(keys + 8);
3173 silent = TRUE;
3174 continue;
3178 * Check for "<special>": accept special keys in <>
3180 if (STRNCMP(keys, "<special>", 9) == 0)
3182 keys = skipwhite(keys + 9);
3183 special = TRUE;
3184 continue;
3187 #ifdef FEAT_EVAL
3189 * Check for "<script>": remap script-local mappings only
3191 if (STRNCMP(keys, "<script>", 8) == 0)
3193 keys = skipwhite(keys + 8);
3194 noremap = REMAP_SCRIPT;
3195 continue;
3199 * Check for "<expr>": {rhs} is an expression.
3201 if (STRNCMP(keys, "<expr>", 6) == 0)
3203 keys = skipwhite(keys + 6);
3204 expr = TRUE;
3205 continue;
3207 #endif
3209 * Check for "<unique>": don't overwrite an existing mapping.
3211 if (STRNCMP(keys, "<unique>", 8) == 0)
3213 keys = skipwhite(keys + 8);
3214 unique = TRUE;
3215 continue;
3217 break;
3220 validate_maphash();
3223 * find end of keys and skip CTRL-Vs (and backslashes) in it
3224 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
3225 * with :unmap white space is included in the keys, no argument possible
3227 p = keys;
3228 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3229 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3231 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3232 p[1] != NUL)
3233 ++p; /* skip CTRL-V or backslash */
3234 ++p;
3236 if (*p != NUL)
3237 *p++ = NUL;
3238 p = skipwhite(p);
3239 rhs = p;
3240 hasarg = (*rhs != NUL);
3241 haskey = (*keys != NUL);
3243 /* check for :unmap without argument */
3244 if (maptype == 1 && !haskey)
3246 retval = 1;
3247 goto theend;
3251 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3252 * with the appropriate two bytes. If it is a shifted special key, unshift
3253 * it too, giving another two bytes.
3254 * replace_termcodes() may move the result to allocated memory, which
3255 * needs to be freed later (*keys_buf and *arg_buf).
3256 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3258 if (haskey)
3259 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
3260 if (hasarg)
3262 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3263 rhs = (char_u *)"";
3264 else
3265 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
3268 #ifdef FEAT_FKMAP
3270 * when in right-to-left mode and alternate keymap option set,
3271 * reverse the character flow in the rhs in Farsi.
3273 if (p_altkeymap && curwin->w_p_rl)
3274 lrswap(rhs);
3275 #endif
3278 * check arguments and translate function keys
3280 if (haskey)
3282 len = (int)STRLEN(keys);
3283 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3285 retval = 1;
3286 goto theend;
3289 if (abbrev && maptype != 1)
3292 * If an abbreviation ends in a keyword character, the
3293 * rest must be all keyword-char or all non-keyword-char.
3294 * Otherwise we won't be able to find the start of it in a
3295 * vi-compatible way.
3297 #ifdef FEAT_MBYTE
3298 if (has_mbyte)
3300 int first, last;
3301 int same = -1;
3303 first = vim_iswordp(keys);
3304 last = first;
3305 p = keys + (*mb_ptr2len)(keys);
3306 n = 1;
3307 while (p < keys + len)
3309 ++n; /* nr of (multi-byte) chars */
3310 last = vim_iswordp(p); /* type of last char */
3311 if (same == -1 && last != first)
3312 same = n - 1; /* count of same char type */
3313 p += (*mb_ptr2len)(p);
3315 if (last && n > 2 && same >= 0 && same < n - 1)
3317 retval = 1;
3318 goto theend;
3321 else
3322 #endif
3323 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3324 for (n = 0; n < len - 2; ++n)
3325 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3327 retval = 1;
3328 goto theend;
3330 /* An abbrevation cannot contain white space. */
3331 for (n = 0; n < len; ++n)
3332 if (vim_iswhite(keys[n]))
3334 retval = 1;
3335 goto theend;
3340 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3341 no_abbr = FALSE; /* reset flag that indicates there are
3342 no abbreviations */
3344 if (!haskey || (maptype != 1 && !hasarg))
3345 msg_start();
3347 #ifdef FEAT_LOCALMAP
3349 * Check if a new local mapping wasn't already defined globally.
3351 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3353 /* need to loop over all global hash lists */
3354 for (hash = 0; hash < 256 && !got_int; ++hash)
3356 if (abbrev)
3358 if (hash != 0) /* there is only one abbreviation list */
3359 break;
3360 mp = first_abbr;
3362 else
3363 mp = maphash[hash];
3364 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3366 /* check entries with the same mode */
3367 if ((mp->m_mode & mode) != 0
3368 && mp->m_keylen == len
3369 && unique
3370 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3372 if (abbrev)
3373 EMSG2(_("E224: global abbreviation already exists for %s"),
3374 mp->m_keys);
3375 else
3376 EMSG2(_("E225: global mapping already exists for %s"),
3377 mp->m_keys);
3378 retval = 5;
3379 goto theend;
3386 * When listing global mappings, also list buffer-local ones here.
3388 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3390 /* need to loop over all global hash lists */
3391 for (hash = 0; hash < 256 && !got_int; ++hash)
3393 if (abbrev)
3395 if (hash != 0) /* there is only one abbreviation list */
3396 break;
3397 mp = curbuf->b_first_abbr;
3399 else
3400 mp = curbuf->b_maphash[hash];
3401 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3403 /* check entries with the same mode */
3404 if ((mp->m_mode & mode) != 0)
3406 if (!haskey) /* show all entries */
3408 showmap(mp, TRUE);
3409 did_local = TRUE;
3411 else
3413 n = mp->m_keylen;
3414 if (STRNCMP(mp->m_keys, keys,
3415 (size_t)(n < len ? n : len)) == 0)
3417 showmap(mp, TRUE);
3418 did_local = TRUE;
3425 #endif
3428 * Find an entry in the maphash[] list that matches.
3429 * For :unmap we may loop two times: once to try to unmap an entry with a
3430 * matching 'from' part, a second time, if the first fails, to unmap an
3431 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3432 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3433 * "bar" because of the abbreviation.
3435 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3436 && !did_it && !got_int; ++round)
3438 /* need to loop over all hash lists */
3439 for (hash = 0; hash < 256 && !got_int; ++hash)
3441 if (abbrev)
3443 if (hash > 0) /* there is only one abbreviation list */
3444 break;
3445 mpp = abbr_table;
3447 else
3448 mpp = &(map_table[hash]);
3449 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3452 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3454 mpp = &(mp->m_next);
3455 continue;
3457 if (!haskey) /* show all entries */
3459 showmap(mp, map_table != maphash);
3460 did_it = TRUE;
3462 else /* do we have a match? */
3464 if (round) /* second round: Try unmap "rhs" string */
3466 n = (int)STRLEN(mp->m_str);
3467 p = mp->m_str;
3469 else
3471 n = mp->m_keylen;
3472 p = mp->m_keys;
3474 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3476 if (maptype == 1) /* delete entry */
3478 /* Only accept a full match. For abbreviations we
3479 * ignore trailing space when matching with the
3480 * "lhs", since an abbreviation can't have
3481 * trailing space. */
3482 if (n != len && (!abbrev || round || n > len
3483 || *skipwhite(keys + n) != NUL))
3485 mpp = &(mp->m_next);
3486 continue;
3489 * We reset the indicated mode bits. If nothing is
3490 * left the entry is deleted below.
3492 mp->m_mode &= ~mode;
3493 did_it = TRUE; /* remember we did something */
3495 else if (!hasarg) /* show matching entry */
3497 showmap(mp, map_table != maphash);
3498 did_it = TRUE;
3500 else if (n != len) /* new entry is ambiguous */
3502 mpp = &(mp->m_next);
3503 continue;
3505 else if (unique)
3507 if (abbrev)
3508 EMSG2(_("E226: abbreviation already exists for %s"),
3510 else
3511 EMSG2(_("E227: mapping already exists for %s"), p);
3512 retval = 5;
3513 goto theend;
3515 else /* new rhs for existing entry */
3517 mp->m_mode &= ~mode; /* remove mode bits */
3518 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3520 newstr = vim_strsave(rhs);
3521 if (newstr == NULL)
3523 retval = 4; /* no mem */
3524 goto theend;
3526 vim_free(mp->m_str);
3527 mp->m_str = newstr;
3528 mp->m_noremap = noremap;
3529 mp->m_silent = silent;
3530 mp->m_mode = mode;
3531 #ifdef FEAT_EVAL
3532 mp->m_expr = expr;
3533 mp->m_script_ID = current_SID;
3534 #endif
3535 did_it = TRUE;
3538 if (mp->m_mode == 0) /* entry can be deleted */
3540 map_free(mpp);
3541 continue; /* continue with *mpp */
3545 * May need to put this entry into another hash list.
3547 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3548 if (!abbrev && new_hash != hash)
3550 *mpp = mp->m_next;
3551 mp->m_next = map_table[new_hash];
3552 map_table[new_hash] = mp;
3554 continue; /* continue with *mpp */
3558 mpp = &(mp->m_next);
3563 if (maptype == 1) /* delete entry */
3565 if (!did_it)
3566 retval = 2; /* no match */
3567 goto theend;
3570 if (!haskey || !hasarg) /* print entries */
3572 if (!did_it
3573 #ifdef FEAT_LOCALMAP
3574 && !did_local
3575 #endif
3578 if (abbrev)
3579 MSG(_("No abbreviation found"));
3580 else
3581 MSG(_("No mapping found"));
3583 goto theend; /* listing finished */
3586 if (did_it) /* have added the new entry already */
3587 goto theend;
3590 * Get here when adding a new entry to the maphash[] list or abbrlist.
3592 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3593 if (mp == NULL)
3595 retval = 4; /* no mem */
3596 goto theend;
3599 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3600 if (*keys == Ctrl_C)
3601 mapped_ctrl_c = TRUE;
3603 mp->m_keys = vim_strsave(keys);
3604 mp->m_str = vim_strsave(rhs);
3605 if (mp->m_keys == NULL || mp->m_str == NULL)
3607 vim_free(mp->m_keys);
3608 vim_free(mp->m_str);
3609 vim_free(mp);
3610 retval = 4; /* no mem */
3611 goto theend;
3613 mp->m_keylen = (int)STRLEN(mp->m_keys);
3614 mp->m_noremap = noremap;
3615 mp->m_silent = silent;
3616 mp->m_mode = mode;
3617 #ifdef FEAT_EVAL
3618 mp->m_expr = expr;
3619 mp->m_script_ID = current_SID;
3620 #endif
3622 /* add the new entry in front of the abbrlist or maphash[] list */
3623 if (abbrev)
3625 mp->m_next = *abbr_table;
3626 *abbr_table = mp;
3628 else
3630 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3631 mp->m_next = map_table[n];
3632 map_table[n] = mp;
3635 theend:
3636 vim_free(keys_buf);
3637 vim_free(arg_buf);
3638 return retval;
3642 * Delete one entry from the abbrlist or maphash[].
3643 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3645 static void
3646 map_free(mpp)
3647 mapblock_T **mpp;
3649 mapblock_T *mp;
3651 mp = *mpp;
3652 vim_free(mp->m_keys);
3653 vim_free(mp->m_str);
3654 *mpp = mp->m_next;
3655 vim_free(mp);
3659 * Initialize maphash[] for first use.
3661 static void
3662 validate_maphash()
3664 if (!maphash_valid)
3666 vim_memset(maphash, 0, sizeof(maphash));
3667 maphash_valid = TRUE;
3672 * Get the mapping mode from the command name.
3675 get_map_mode(cmdp, forceit)
3676 char_u **cmdp;
3677 int forceit;
3679 char_u *p;
3680 int modec;
3681 int mode;
3683 p = *cmdp;
3684 modec = *p++;
3685 if (modec == 'i')
3686 mode = INSERT; /* :imap */
3687 else if (modec == 'l')
3688 mode = LANGMAP; /* :lmap */
3689 else if (modec == 'c')
3690 mode = CMDLINE; /* :cmap */
3691 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3692 mode = NORMAL; /* :nmap */
3693 else if (modec == 'v')
3694 mode = VISUAL + SELECTMODE; /* :vmap */
3695 else if (modec == 'x')
3696 mode = VISUAL; /* :xmap */
3697 else if (modec == 's')
3698 mode = SELECTMODE; /* :smap */
3699 else if (modec == 'o')
3700 mode = OP_PENDING; /* :omap */
3701 else
3703 --p;
3704 if (forceit)
3705 mode = INSERT + CMDLINE; /* :map ! */
3706 else
3707 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
3710 *cmdp = p;
3711 return mode;
3715 * Clear all mappings or abbreviations.
3716 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3718 /*ARGSUSED*/
3719 void
3720 map_clear(cmdp, arg, forceit, abbr)
3721 char_u *cmdp;
3722 char_u *arg;
3723 int forceit;
3724 int abbr;
3726 int mode;
3727 #ifdef FEAT_LOCALMAP
3728 int local;
3730 local = (STRCMP(arg, "<buffer>") == 0);
3731 if (!local && *arg != NUL)
3733 EMSG(_(e_invarg));
3734 return;
3736 #endif
3738 mode = get_map_mode(&cmdp, forceit);
3739 map_clear_int(curbuf, mode,
3740 #ifdef FEAT_LOCALMAP
3741 local,
3742 #else
3743 FALSE,
3744 #endif
3745 abbr);
3749 * Clear all mappings in "mode".
3751 /*ARGSUSED*/
3752 void
3753 map_clear_int(buf, mode, local, abbr)
3754 buf_T *buf; /* buffer for local mappings */
3755 int mode; /* mode in which to delete */
3756 int local; /* TRUE for buffer-local mappings */
3757 int abbr; /* TRUE for abbreviations */
3759 mapblock_T *mp, **mpp;
3760 int hash;
3761 int new_hash;
3763 validate_maphash();
3765 for (hash = 0; hash < 256; ++hash)
3767 if (abbr)
3769 if (hash > 0) /* there is only one abbrlist */
3770 break;
3771 #ifdef FEAT_LOCALMAP
3772 if (local)
3773 mpp = &buf->b_first_abbr;
3774 else
3775 #endif
3776 mpp = &first_abbr;
3778 else
3780 #ifdef FEAT_LOCALMAP
3781 if (local)
3782 mpp = &buf->b_maphash[hash];
3783 else
3784 #endif
3785 mpp = &maphash[hash];
3787 while (*mpp != NULL)
3789 mp = *mpp;
3790 if (mp->m_mode & mode)
3792 mp->m_mode &= ~mode;
3793 if (mp->m_mode == 0) /* entry can be deleted */
3795 map_free(mpp);
3796 continue;
3799 * May need to put this entry into another hash list.
3801 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3802 if (!abbr && new_hash != hash)
3804 *mpp = mp->m_next;
3805 #ifdef FEAT_LOCALMAP
3806 if (local)
3808 mp->m_next = buf->b_maphash[new_hash];
3809 buf->b_maphash[new_hash] = mp;
3811 else
3812 #endif
3814 mp->m_next = maphash[new_hash];
3815 maphash[new_hash] = mp;
3817 continue; /* continue with *mpp */
3820 mpp = &(mp->m_next);
3825 static void
3826 showmap(mp, local)
3827 mapblock_T *mp;
3828 int local; /* TRUE for buffer-local map */
3830 int len = 1;
3832 if (msg_didout || msg_silent != 0)
3833 msg_putchar('\n');
3834 if ((mp->m_mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3835 msg_putchar('!'); /* :map! */
3836 else if (mp->m_mode & INSERT)
3837 msg_putchar('i'); /* :imap */
3838 else if (mp->m_mode & LANGMAP)
3839 msg_putchar('l'); /* :lmap */
3840 else if (mp->m_mode & CMDLINE)
3841 msg_putchar('c'); /* :cmap */
3842 else if ((mp->m_mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3843 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3844 msg_putchar(' '); /* :map */
3845 else
3847 len = 0;
3848 if (mp->m_mode & NORMAL)
3850 msg_putchar('n'); /* :nmap */
3851 ++len;
3853 if (mp->m_mode & OP_PENDING)
3855 msg_putchar('o'); /* :omap */
3856 ++len;
3858 if ((mp->m_mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3860 msg_putchar('v'); /* :vmap */
3861 ++len;
3863 else
3865 if (mp->m_mode & VISUAL)
3867 msg_putchar('x'); /* :xmap */
3868 ++len;
3870 if (mp->m_mode & SELECTMODE)
3872 msg_putchar('s'); /* :smap */
3873 ++len;
3877 while (++len <= 3)
3878 msg_putchar(' ');
3880 /* Display the LHS. Get length of what we write. */
3881 len = msg_outtrans_special(mp->m_keys, TRUE);
3884 msg_putchar(' '); /* padd with blanks */
3885 ++len;
3886 } while (len < 12);
3888 if (mp->m_noremap == REMAP_NONE)
3889 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3890 else if (mp->m_noremap == REMAP_SCRIPT)
3891 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3892 else
3893 msg_putchar(' ');
3895 if (local)
3896 msg_putchar('@');
3897 else
3898 msg_putchar(' ');
3900 /* Use FALSE below if we only want things like <Up> to show up as such on
3901 * the rhs, and not M-x etc, TRUE gets both -- webb
3903 if (*mp->m_str == NUL)
3904 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3905 else
3906 msg_outtrans_special(mp->m_str, FALSE);
3907 #ifdef FEAT_EVAL
3908 if (p_verbose > 0)
3909 last_set_msg(mp->m_script_ID);
3910 #endif
3911 out_flush(); /* show one line at a time */
3914 #if defined(FEAT_EVAL) || defined(PROTO)
3916 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3917 * Recognize termcap codes in "str".
3918 * Also checks mappings local to the current buffer.
3921 map_to_exists(str, modechars, abbr)
3922 char_u *str;
3923 char_u *modechars;
3924 int abbr;
3926 int mode = 0;
3927 char_u *rhs;
3928 char_u *buf;
3929 int retval;
3931 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
3933 if (vim_strchr(modechars, 'n') != NULL)
3934 mode |= NORMAL;
3935 if (vim_strchr(modechars, 'v') != NULL)
3936 mode |= VISUAL + SELECTMODE;
3937 if (vim_strchr(modechars, 'x') != NULL)
3938 mode |= VISUAL;
3939 if (vim_strchr(modechars, 's') != NULL)
3940 mode |= SELECTMODE;
3941 if (vim_strchr(modechars, 'o') != NULL)
3942 mode |= OP_PENDING;
3943 if (vim_strchr(modechars, 'i') != NULL)
3944 mode |= INSERT;
3945 if (vim_strchr(modechars, 'l') != NULL)
3946 mode |= LANGMAP;
3947 if (vim_strchr(modechars, 'c') != NULL)
3948 mode |= CMDLINE;
3950 retval = map_to_exists_mode(rhs, mode, abbr);
3951 vim_free(buf);
3953 return retval;
3955 #endif
3958 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
3959 * Also checks mappings local to the current buffer.
3962 map_to_exists_mode(rhs, mode, abbr)
3963 char_u *rhs;
3964 int mode;
3965 int abbr;
3967 mapblock_T *mp;
3968 int hash;
3969 # ifdef FEAT_LOCALMAP
3970 int expand_buffer = FALSE;
3972 validate_maphash();
3974 /* Do it twice: once for global maps and once for local maps. */
3975 for (;;)
3977 # endif
3978 for (hash = 0; hash < 256; ++hash)
3980 if (abbr)
3982 if (hash > 0) /* there is only one abbr list */
3983 break;
3984 #ifdef FEAT_LOCALMAP
3985 if (expand_buffer)
3986 mp = curbuf->b_first_abbr;
3987 else
3988 #endif
3989 mp = first_abbr;
3991 # ifdef FEAT_LOCALMAP
3992 else if (expand_buffer)
3993 mp = curbuf->b_maphash[hash];
3994 # endif
3995 else
3996 mp = maphash[hash];
3997 for (; mp; mp = mp->m_next)
3999 if ((mp->m_mode & mode)
4000 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
4001 return TRUE;
4004 # ifdef FEAT_LOCALMAP
4005 if (expand_buffer)
4006 break;
4007 expand_buffer = TRUE;
4009 # endif
4011 return FALSE;
4014 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4016 * Used below when expanding mapping/abbreviation names.
4018 static int expand_mapmodes = 0;
4019 static int expand_isabbrev = 0;
4020 #ifdef FEAT_LOCALMAP
4021 static int expand_buffer = FALSE;
4022 #endif
4025 * Work out what to complete when doing command line completion of mapping
4026 * or abbreviation names.
4028 char_u *
4029 set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
4030 expand_T *xp;
4031 char_u *cmd;
4032 char_u *arg;
4033 int forceit; /* TRUE if '!' given */
4034 int isabbrev; /* TRUE if abbreviation */
4035 int isunmap; /* TRUE if unmap/unabbrev command */
4036 cmdidx_T cmdidx;
4038 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
4039 xp->xp_context = EXPAND_NOTHING;
4040 else
4042 if (isunmap)
4043 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
4044 else
4046 expand_mapmodes = INSERT + CMDLINE;
4047 if (!isabbrev)
4048 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
4050 expand_isabbrev = isabbrev;
4051 xp->xp_context = EXPAND_MAPPINGS;
4052 #ifdef FEAT_LOCALMAP
4053 expand_buffer = FALSE;
4054 #endif
4055 for (;;)
4057 #ifdef FEAT_LOCALMAP
4058 if (STRNCMP(arg, "<buffer>", 8) == 0)
4060 expand_buffer = TRUE;
4061 arg = skipwhite(arg + 8);
4062 continue;
4064 #endif
4065 if (STRNCMP(arg, "<unique>", 8) == 0)
4067 arg = skipwhite(arg + 8);
4068 continue;
4070 if (STRNCMP(arg, "<silent>", 8) == 0)
4072 arg = skipwhite(arg + 8);
4073 continue;
4075 #ifdef FEAT_EVAL
4076 if (STRNCMP(arg, "<script>", 8) == 0)
4078 arg = skipwhite(arg + 8);
4079 continue;
4081 if (STRNCMP(arg, "<expr>", 6) == 0)
4083 arg = skipwhite(arg + 6);
4084 continue;
4086 #endif
4087 break;
4089 xp->xp_pattern = arg;
4092 return NULL;
4096 * Find all mapping/abbreviation names that match regexp 'prog'.
4097 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4098 * Return OK if matches found, FAIL otherwise.
4101 ExpandMappings(regmatch, num_file, file)
4102 regmatch_T *regmatch;
4103 int *num_file;
4104 char_u ***file;
4106 mapblock_T *mp;
4107 int hash;
4108 int count;
4109 int round;
4110 char_u *p;
4111 int i;
4113 validate_maphash();
4115 *num_file = 0; /* return values in case of FAIL */
4116 *file = NULL;
4119 * round == 1: Count the matches.
4120 * round == 2: Build the array to keep the matches.
4122 for (round = 1; round <= 2; ++round)
4124 count = 0;
4126 for (i = 0; i < 5; ++i)
4128 if (i == 0)
4129 p = (char_u *)"<silent>";
4130 else if (i == 1)
4131 p = (char_u *)"<unique>";
4132 #ifdef FEAT_EVAL
4133 else if (i == 2)
4134 p = (char_u *)"<script>";
4135 else if (i == 3)
4136 p = (char_u *)"<expr>";
4137 #endif
4138 #ifdef FEAT_LOCALMAP
4139 else if (i == 4 && !expand_buffer)
4140 p = (char_u *)"<buffer>";
4141 #endif
4142 else
4143 continue;
4145 if (vim_regexec(regmatch, p, (colnr_T)0))
4147 if (round == 1)
4148 ++count;
4149 else
4150 (*file)[count++] = vim_strsave(p);
4154 for (hash = 0; hash < 256; ++hash)
4156 if (expand_isabbrev)
4158 if (hash > 0) /* only one abbrev list */
4159 break; /* for (hash) */
4160 mp = first_abbr;
4162 #ifdef FEAT_LOCALMAP
4163 else if (expand_buffer)
4164 mp = curbuf->b_maphash[hash];
4165 #endif
4166 else
4167 mp = maphash[hash];
4168 for (; mp; mp = mp->m_next)
4170 if (mp->m_mode & expand_mapmodes)
4172 p = translate_mapping(mp->m_keys, TRUE);
4173 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4175 if (round == 1)
4176 ++count;
4177 else
4179 (*file)[count++] = p;
4180 p = NULL;
4183 vim_free(p);
4185 } /* for (mp) */
4186 } /* for (hash) */
4188 if (count == 0) /* no match found */
4189 break; /* for (round) */
4191 if (round == 1)
4193 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4194 if (*file == NULL)
4195 return FAIL;
4197 } /* for (round) */
4199 if (count > 1)
4201 char_u **ptr1;
4202 char_u **ptr2;
4203 char_u **ptr3;
4205 /* Sort the matches */
4206 sort_strings(*file, count);
4208 /* Remove multiple entries */
4209 ptr1 = *file;
4210 ptr2 = ptr1 + 1;
4211 ptr3 = ptr1 + count;
4213 while (ptr2 < ptr3)
4215 if (STRCMP(*ptr1, *ptr2))
4216 *++ptr1 = *ptr2++;
4217 else
4219 vim_free(*ptr2++);
4220 count--;
4225 *num_file = count;
4226 return (count == 0 ? FAIL : OK);
4228 #endif /* FEAT_CMDL_COMPL */
4231 * Check for an abbreviation.
4232 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4233 * "c" is the character typed before check_abbr was called. It may have
4234 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4236 * Historic vi practice: The last character of an abbreviation must be an id
4237 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4238 * characters or all non-id characters. This allows for abbr. "#i" to
4239 * "#include".
4241 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4242 * Then there must be white space before the abbr.
4244 * return TRUE if there is an abbreviation, FALSE if not
4247 check_abbr(c, ptr, col, mincol)
4248 int c;
4249 char_u *ptr;
4250 int col;
4251 int mincol;
4253 int len;
4254 int scol; /* starting column of the abbr. */
4255 int j;
4256 char_u *s;
4257 #ifdef FEAT_MBYTE
4258 char_u tb[MB_MAXBYTES + 4];
4259 #else
4260 char_u tb[4];
4261 #endif
4262 mapblock_T *mp;
4263 #ifdef FEAT_LOCALMAP
4264 mapblock_T *mp2;
4265 #endif
4266 #ifdef FEAT_MBYTE
4267 int clen = 0; /* length in characters */
4268 #endif
4269 int is_id = TRUE;
4270 int vim_abbr;
4272 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4273 return FALSE;
4274 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0)
4275 /* no remapping implies no abbreviation */
4276 return FALSE;
4279 * Check for word before the cursor: If it ends in a keyword char all
4280 * chars before it must be al keyword chars or non-keyword chars, but not
4281 * white space. If it ends in a non-keyword char we accept any characters
4282 * before it except white space.
4284 if (col == 0) /* cannot be an abbr. */
4285 return FALSE;
4287 #ifdef FEAT_MBYTE
4288 if (has_mbyte)
4290 char_u *p;
4292 p = mb_prevptr(ptr, ptr + col);
4293 if (!vim_iswordp(p))
4294 vim_abbr = TRUE; /* Vim added abbr. */
4295 else
4297 vim_abbr = FALSE; /* vi compatible abbr. */
4298 if (p > ptr)
4299 is_id = vim_iswordp(mb_prevptr(ptr, p));
4301 clen = 1;
4302 while (p > ptr + mincol)
4304 p = mb_prevptr(ptr, p);
4305 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4307 p += (*mb_ptr2len)(p);
4308 break;
4310 ++clen;
4312 scol = (int)(p - ptr);
4314 else
4315 #endif
4317 if (!vim_iswordc(ptr[col - 1]))
4318 vim_abbr = TRUE; /* Vim added abbr. */
4319 else
4321 vim_abbr = FALSE; /* vi compatible abbr. */
4322 if (col > 1)
4323 is_id = vim_iswordc(ptr[col - 2]);
4325 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4326 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4330 if (scol < mincol)
4331 scol = mincol;
4332 if (scol < col) /* there is a word in front of the cursor */
4334 ptr += scol;
4335 len = col - scol;
4336 #ifdef FEAT_LOCALMAP
4337 mp = curbuf->b_first_abbr;
4338 mp2 = first_abbr;
4339 if (mp == NULL)
4341 mp = mp2;
4342 mp2 = NULL;
4344 #else
4345 mp = first_abbr;
4346 #endif
4347 for ( ; mp;
4348 #ifdef FEAT_LOCALMAP
4349 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4350 #endif
4351 (mp = mp->m_next))
4353 /* find entries with right mode and keys */
4354 if ( (mp->m_mode & State)
4355 && mp->m_keylen == len
4356 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4357 break;
4359 if (mp != NULL)
4362 * Found a match:
4363 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4364 * This goes from end to start.
4366 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4367 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4368 * Characters where IS_SPECIAL() == TRUE: key codes, need
4369 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4371 * Character CTRL-] is treated specially - it completes the
4372 * abbreviation, but is not inserted into the input stream.
4374 j = 0;
4375 /* special key code, split up */
4376 if (c != Ctrl_RSB)
4378 if (IS_SPECIAL(c) || c == K_SPECIAL)
4380 tb[j++] = K_SPECIAL;
4381 tb[j++] = K_SECOND(c);
4382 tb[j++] = K_THIRD(c);
4384 else
4386 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4387 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4388 #ifdef FEAT_MBYTE
4389 if (has_mbyte)
4391 /* if ABBR_OFF has been added, remove it here */
4392 if (c >= ABBR_OFF)
4393 c -= ABBR_OFF;
4394 j += (*mb_char2bytes)(c, tb + j);
4396 else
4397 #endif
4398 tb[j++] = c;
4400 tb[j] = NUL;
4401 /* insert the last typed char */
4402 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4404 #ifdef FEAT_EVAL
4405 if (mp->m_expr)
4406 s = eval_map_expr(mp->m_str);
4407 else
4408 #endif
4409 s = mp->m_str;
4410 if (s != NULL)
4412 /* insert the to string */
4413 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
4414 /* no abbrev. for these chars */
4415 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4416 #ifdef FEAT_EVAL
4417 if (mp->m_expr)
4418 vim_free(s);
4419 #endif
4422 tb[0] = Ctrl_H;
4423 tb[1] = NUL;
4424 #ifdef FEAT_MBYTE
4425 if (has_mbyte)
4426 len = clen; /* Delete characters instead of bytes */
4427 #endif
4428 while (len-- > 0) /* delete the from string */
4429 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4430 return TRUE;
4433 return FALSE;
4436 #ifdef FEAT_EVAL
4438 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4439 * special characters.
4441 static char_u *
4442 eval_map_expr(str)
4443 char_u *str;
4445 char_u *res;
4446 char_u *p;
4447 char_u *save_cmd;
4448 pos_T save_cursor;
4450 save_cmd = save_cmdline_alloc();
4451 if (save_cmd == NULL)
4452 return NULL;
4454 /* Forbid changing text or using ":normal" to avoid most of the bad side
4455 * effects. Also restore the cursor position. */
4456 ++textlock;
4457 #ifdef FEAT_EX_EXTRA
4458 ++ex_normal_lock;
4459 #endif
4460 save_cursor = curwin->w_cursor;
4461 p = eval_to_string(str, NULL, FALSE);
4462 --textlock;
4463 #ifdef FEAT_EX_EXTRA
4464 --ex_normal_lock;
4465 #endif
4466 curwin->w_cursor = save_cursor;
4468 restore_cmdline_alloc(save_cmd);
4469 if (p == NULL)
4470 return NULL;
4471 res = vim_strsave_escape_csi(p);
4472 vim_free(p);
4474 return res;
4476 #endif
4479 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4480 * can be put in the typeahead buffer.
4481 * Returns NULL when out of memory.
4483 char_u *
4484 vim_strsave_escape_csi(p)
4485 char_u *p;
4487 char_u *res;
4488 char_u *s, *d;
4490 /* Need a buffer to hold up to three times as much. */
4491 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
4492 if (res != NULL)
4494 d = res;
4495 for (s = p; *s != NUL; )
4497 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4499 /* Copy special key unmodified. */
4500 *d++ = *s++;
4501 *d++ = *s++;
4502 *d++ = *s++;
4504 else
4506 /* Add character, possibly multi-byte to destination, escaping
4507 * CSI and K_SPECIAL. */
4508 d = add_char2buf(PTR2CHAR(s), d);
4509 mb_ptr_adv(s);
4512 *d = NUL;
4514 return res;
4518 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4519 * vim_strsave_escape_csi(). Works in-place.
4521 void
4522 vim_unescape_csi(p)
4523 char_u *p;
4525 char_u *s = p, *d = p;
4527 while (*s != NUL)
4529 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4531 *d++ = K_SPECIAL;
4532 s += 3;
4534 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4535 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4537 *d++ = CSI;
4538 s += 3;
4540 else
4541 *d++ = *s++;
4543 *d = NUL;
4547 * Write map commands for the current mappings to an .exrc file.
4548 * Return FAIL on error, OK otherwise.
4551 makemap(fd, buf)
4552 FILE *fd;
4553 buf_T *buf; /* buffer for local mappings or NULL */
4555 mapblock_T *mp;
4556 char_u c1, c2, c3;
4557 char_u *p;
4558 char *cmd;
4559 int abbr;
4560 int hash;
4561 int did_cpo = FALSE;
4562 int i;
4564 validate_maphash();
4567 * Do the loop twice: Once for mappings, once for abbreviations.
4568 * Then loop over all map hash lists.
4570 for (abbr = 0; abbr < 2; ++abbr)
4571 for (hash = 0; hash < 256; ++hash)
4573 if (abbr)
4575 if (hash > 0) /* there is only one abbr list */
4576 break;
4577 #ifdef FEAT_LOCALMAP
4578 if (buf != NULL)
4579 mp = buf->b_first_abbr;
4580 else
4581 #endif
4582 mp = first_abbr;
4584 else
4586 #ifdef FEAT_LOCALMAP
4587 if (buf != NULL)
4588 mp = buf->b_maphash[hash];
4589 else
4590 #endif
4591 mp = maphash[hash];
4594 for ( ; mp; mp = mp->m_next)
4596 /* skip script-local mappings */
4597 if (mp->m_noremap == REMAP_SCRIPT)
4598 continue;
4600 /* skip mappings that contain a <SNR> (script-local thing),
4601 * they probably don't work when loaded again */
4602 for (p = mp->m_str; *p != NUL; ++p)
4603 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4604 && p[2] == (int)KE_SNR)
4605 break;
4606 if (*p != NUL)
4607 continue;
4609 /* It's possible to create a mapping and then ":unmap" certain
4610 * modes. We recreate this here by mapping the individual
4611 * modes, which requires up to three of them. */
4612 c1 = NUL;
4613 c2 = NUL;
4614 c3 = NUL;
4615 if (abbr)
4616 cmd = "abbr";
4617 else
4618 cmd = "map";
4619 switch (mp->m_mode)
4621 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
4622 break;
4623 case NORMAL:
4624 c1 = 'n';
4625 break;
4626 case VISUAL:
4627 c1 = 'x';
4628 break;
4629 case SELECTMODE:
4630 c1 = 's';
4631 break;
4632 case OP_PENDING:
4633 c1 = 'o';
4634 break;
4635 case NORMAL + VISUAL:
4636 c1 = 'n';
4637 c2 = 'x';
4638 break;
4639 case NORMAL + SELECTMODE:
4640 c1 = 'n';
4641 c2 = 's';
4642 break;
4643 case NORMAL + OP_PENDING:
4644 c1 = 'n';
4645 c2 = 'o';
4646 break;
4647 case VISUAL + SELECTMODE:
4648 c1 = 'v';
4649 break;
4650 case VISUAL + OP_PENDING:
4651 c1 = 'x';
4652 c2 = 'o';
4653 break;
4654 case SELECTMODE + OP_PENDING:
4655 c1 = 's';
4656 c2 = 'o';
4657 break;
4658 case NORMAL + VISUAL + SELECTMODE:
4659 c1 = 'n';
4660 c2 = 'v';
4661 break;
4662 case NORMAL + VISUAL + OP_PENDING:
4663 c1 = 'n';
4664 c2 = 'x';
4665 c3 = 'o';
4666 break;
4667 case NORMAL + SELECTMODE + OP_PENDING:
4668 c1 = 'n';
4669 c2 = 's';
4670 c3 = 'o';
4671 break;
4672 case VISUAL + SELECTMODE + OP_PENDING:
4673 c1 = 'v';
4674 c2 = 'o';
4675 break;
4676 case CMDLINE + INSERT:
4677 if (!abbr)
4678 cmd = "map!";
4679 break;
4680 case CMDLINE:
4681 c1 = 'c';
4682 break;
4683 case INSERT:
4684 c1 = 'i';
4685 break;
4686 case LANGMAP:
4687 c1 = 'l';
4688 break;
4689 default:
4690 EMSG(_("E228: makemap: Illegal mode"));
4691 return FAIL;
4693 do /* do this twice if c2 is set, 3 times with c3 */
4695 /* When outputting <> form, need to make sure that 'cpo'
4696 * is set to the Vim default. */
4697 if (!did_cpo)
4699 if (*mp->m_str == NUL) /* will use <Nop> */
4700 did_cpo = TRUE;
4701 else
4702 for (i = 0; i < 2; ++i)
4703 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4704 if (*p == K_SPECIAL || *p == NL)
4705 did_cpo = TRUE;
4706 if (did_cpo)
4708 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4709 || put_eol(fd) < 0
4710 || fprintf(fd, "set cpo&vim") < 0
4711 || put_eol(fd) < 0)
4712 return FAIL;
4715 if (c1 && putc(c1, fd) < 0)
4716 return FAIL;
4717 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4718 return FAIL;
4719 if (fprintf(fd, cmd) < 0)
4720 return FAIL;
4721 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4722 return FAIL;
4723 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4724 return FAIL;
4725 #ifdef FEAT_EVAL
4726 if (mp->m_noremap == REMAP_SCRIPT
4727 && fputs("<script>", fd) < 0)
4728 return FAIL;
4729 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4730 return FAIL;
4731 #endif
4733 if ( putc(' ', fd) < 0
4734 || put_escstr(fd, mp->m_keys, 0) == FAIL
4735 || putc(' ', fd) < 0
4736 || put_escstr(fd, mp->m_str, 1) == FAIL
4737 || put_eol(fd) < 0)
4738 return FAIL;
4739 c1 = c2;
4740 c2 = c3;
4741 c3 = NUL;
4742 } while (c1 != NUL);
4746 if (did_cpo)
4747 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4748 || put_eol(fd) < 0
4749 || fprintf(fd, "unlet s:cpo_save") < 0
4750 || put_eol(fd) < 0)
4751 return FAIL;
4752 return OK;
4756 * write escape string to file
4757 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4759 * return FAIL for failure, OK otherwise
4762 put_escstr(fd, strstart, what)
4763 FILE *fd;
4764 char_u *strstart;
4765 int what;
4767 char_u *str = strstart;
4768 int c;
4769 int modifiers;
4771 /* :map xx <Nop> */
4772 if (*str == NUL && what == 1)
4774 if (fprintf(fd, "<Nop>") < 0)
4775 return FAIL;
4776 return OK;
4779 for ( ; *str != NUL; ++str)
4781 #ifdef FEAT_MBYTE
4782 char_u *p;
4784 /* Check for a multi-byte character, which may contain escaped
4785 * K_SPECIAL and CSI bytes */
4786 p = mb_unescape(&str);
4787 if (p != NULL)
4789 while (*p != NUL)
4790 if (fputc(*p++, fd) < 0)
4791 return FAIL;
4792 --str;
4793 continue;
4795 #endif
4797 c = *str;
4799 * Special key codes have to be translated to be able to make sense
4800 * when they are read back.
4802 if (c == K_SPECIAL && what != 2)
4804 modifiers = 0x0;
4805 if (str[1] == KS_MODIFIER)
4807 modifiers = str[2];
4808 str += 3;
4809 c = *str;
4811 if (c == K_SPECIAL)
4813 c = TO_SPECIAL(str[1], str[2]);
4814 str += 2;
4816 if (IS_SPECIAL(c) || modifiers) /* special key */
4818 if (fprintf(fd, (char *)get_special_key_name(c, modifiers)) < 0)
4819 return FAIL;
4820 continue;
4825 * A '\n' in a map command should be written as <NL>.
4826 * A '\n' in a set command should be written as \^V^J.
4828 if (c == NL)
4830 if (what == 2)
4832 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4833 return FAIL;
4835 else
4837 if (fprintf(fd, "<NL>") < 0)
4838 return FAIL;
4840 continue;
4844 * Some characters have to be escaped with CTRL-V to
4845 * prevent them from misinterpreted in DoOneCmd().
4846 * A space, Tab and '"' has to be escaped with a backslash to
4847 * prevent it to be misinterpreted in do_set().
4848 * A space has to be escaped with a CTRL-V when it's at the start of a
4849 * ":map" rhs.
4850 * A '<' has to be escaped with a CTRL-V to prevent it being
4851 * interpreted as the start of a special key name.
4852 * A space in the lhs of a :map needs a CTRL-V.
4854 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4856 if (putc('\\', fd) < 0)
4857 return FAIL;
4859 else if (c < ' ' || c > '~' || c == '|'
4860 || (what == 0 && c == ' ')
4861 || (what == 1 && str == strstart && c == ' ')
4862 || (what != 2 && c == '<'))
4864 if (putc(Ctrl_V, fd) < 0)
4865 return FAIL;
4867 if (putc(c, fd) < 0)
4868 return FAIL;
4870 return OK;
4874 * Check all mappings for the presence of special key codes.
4875 * Used after ":set term=xxx".
4877 void
4878 check_map_keycodes()
4880 mapblock_T *mp;
4881 char_u *p;
4882 int i;
4883 char_u buf[3];
4884 char_u *save_name;
4885 int abbr;
4886 int hash;
4887 #ifdef FEAT_LOCALMAP
4888 buf_T *bp;
4889 #endif
4891 validate_maphash();
4892 save_name = sourcing_name;
4893 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4895 #ifdef FEAT_LOCALMAP
4896 /* This this once for each buffer, and then once for global
4897 * mappings/abbreviations with bp == NULL */
4898 for (bp = firstbuf; ; bp = bp->b_next)
4900 #endif
4902 * Do the loop twice: Once for mappings, once for abbreviations.
4903 * Then loop over all map hash lists.
4905 for (abbr = 0; abbr <= 1; ++abbr)
4906 for (hash = 0; hash < 256; ++hash)
4908 if (abbr)
4910 if (hash) /* there is only one abbr list */
4911 break;
4912 #ifdef FEAT_LOCALMAP
4913 if (bp != NULL)
4914 mp = bp->b_first_abbr;
4915 else
4916 #endif
4917 mp = first_abbr;
4919 else
4921 #ifdef FEAT_LOCALMAP
4922 if (bp != NULL)
4923 mp = bp->b_maphash[hash];
4924 else
4925 #endif
4926 mp = maphash[hash];
4928 for ( ; mp != NULL; mp = mp->m_next)
4930 for (i = 0; i <= 1; ++i) /* do this twice */
4932 if (i == 0)
4933 p = mp->m_keys; /* once for the "from" part */
4934 else
4935 p = mp->m_str; /* and once for the "to" part */
4936 while (*p)
4938 if (*p == K_SPECIAL)
4940 ++p;
4941 if (*p < 128) /* for "normal" tcap entries */
4943 buf[0] = p[0];
4944 buf[1] = p[1];
4945 buf[2] = NUL;
4946 (void)add_termcap_entry(buf, FALSE);
4948 ++p;
4950 ++p;
4955 #ifdef FEAT_LOCALMAP
4956 if (bp == NULL)
4957 break;
4959 #endif
4960 sourcing_name = save_name;
4963 #ifdef FEAT_EVAL
4965 * Check the string "keys" against the lhs of all mappings
4966 * Return pointer to rhs of mapping (mapblock->m_str)
4967 * NULL otherwise
4969 char_u *
4970 check_map(keys, mode, exact, ign_mod, abbr)
4971 char_u *keys;
4972 int mode;
4973 int exact; /* require exact match */
4974 int ign_mod; /* ignore preceding modifier */
4975 int abbr; /* do abbreviations */
4977 int hash;
4978 int len, minlen;
4979 mapblock_T *mp;
4980 char_u *s;
4981 #ifdef FEAT_LOCALMAP
4982 int local;
4983 #endif
4985 validate_maphash();
4987 len = (int)STRLEN(keys);
4988 #ifdef FEAT_LOCALMAP
4989 for (local = 1; local >= 0; --local)
4990 #endif
4991 /* loop over all hash lists */
4992 for (hash = 0; hash < 256; ++hash)
4994 if (abbr)
4996 if (hash > 0) /* there is only one list. */
4997 break;
4998 #ifdef FEAT_LOCALMAP
4999 if (local)
5000 mp = curbuf->b_first_abbr;
5001 else
5002 #endif
5003 mp = first_abbr;
5005 #ifdef FEAT_LOCALMAP
5006 else if (local)
5007 mp = curbuf->b_maphash[hash];
5008 #endif
5009 else
5010 mp = maphash[hash];
5011 for ( ; mp != NULL; mp = mp->m_next)
5013 /* skip entries with wrong mode, wrong length and not matching
5014 * ones */
5015 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
5017 if (len > mp->m_keylen)
5018 minlen = mp->m_keylen;
5019 else
5020 minlen = len;
5021 s = mp->m_keys;
5022 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
5023 && s[2] != NUL)
5025 s += 3;
5026 if (len > mp->m_keylen - 3)
5027 minlen = mp->m_keylen - 3;
5029 if (STRNCMP(s, keys, minlen) == 0)
5030 return mp->m_str;
5035 return NULL;
5037 #endif
5039 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
5041 #define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
5044 * Default mappings for some often used keys.
5046 static struct initmap
5048 char_u *arg;
5049 int mode;
5050 } initmappings[] =
5052 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
5053 /* Use the Windows (CUA) keybindings. */
5054 # ifdef FEAT_GUI
5055 # if 0 /* These are now used to move tab pages */
5056 {(char_u *)"<C-PageUp> H", NORMAL+VIS_SEL},
5057 {(char_u *)"<C-PageUp> <C-O>H",INSERT},
5058 {(char_u *)"<C-PageDown> L$", NORMAL+VIS_SEL},
5059 {(char_u *)"<C-PageDown> <C-O>L<C-O>$", INSERT},
5060 # endif
5062 /* paste, copy and cut */
5063 {(char_u *)"<S-Insert> \"*P", NORMAL},
5064 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
5065 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
5066 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
5067 {(char_u *)"<S-Del> \"*d", VIS_SEL},
5068 {(char_u *)"<C-Del> \"*d", VIS_SEL},
5069 {(char_u *)"<C-X> \"*d", VIS_SEL},
5070 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5071 # else
5072 # if 0 /* These are now used to move tab pages */
5073 {(char_u *)"\316\204 H", NORMAL+VIS_SEL}, /* CTRL-PageUp is "H" */
5074 {(char_u *)"\316\204 \017H",INSERT}, /* CTRL-PageUp is "^OH"*/
5075 {(char_u *)"\316v L$", NORMAL+VIS_SEL}, /* CTRL-PageDown is "L$" */
5076 {(char_u *)"\316v \017L\017$", INSERT}, /* CTRL-PageDown ="^OL^O$"*/
5077 # endif
5078 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
5079 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
5080 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
5081 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5083 /* paste, copy and cut */
5084 # ifdef FEAT_CLIPBOARD
5085 # ifdef DJGPP
5086 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
5087 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
5088 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
5089 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5090 # if 0 /* Shift-Del produces the same code as Del */
5091 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5092 # endif
5093 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5094 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
5095 # else
5096 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
5097 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
5098 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
5099 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5100 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5101 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5102 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
5103 # endif
5104 # else
5105 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
5106 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
5107 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
5108 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5109 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5110 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
5111 # endif
5112 # endif
5113 #endif
5115 #if defined(MACOS)
5116 /* Use the Standard MacOS binding. */
5117 /* paste, copy and cut */
5118 {(char_u *)"<D-v> \"*P", NORMAL},
5119 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
5120 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
5121 {(char_u *)"<D-c> \"*y", VIS_SEL},
5122 {(char_u *)"<D-x> \"*d", VIS_SEL},
5123 {(char_u *)"<Backspace> \"-d", VIS_SEL},
5124 #endif
5127 # undef VIS_SEL
5128 #endif
5131 * Set up default mappings.
5133 void
5134 init_mappings()
5136 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
5137 int i;
5139 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5140 add_map(initmappings[i].arg, initmappings[i].mode);
5141 #endif
5144 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5145 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
5147 * Add a mapping "map" for mode "mode".
5148 * Need to put string in allocated memory, because do_map() will modify it.
5150 void
5151 add_map(map, mode)
5152 char_u *map;
5153 int mode;
5155 char_u *s;
5156 char_u *cpo_save = p_cpo;
5158 p_cpo = (char_u *)""; /* Allow <> notation */
5159 s = vim_strsave(map);
5160 if (s != NULL)
5162 (void)do_map(0, s, mode, FALSE);
5163 vim_free(s);
5165 p_cpo = cpo_save;
5167 #endif