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.
13 * functions related with getting a character from the user/mapping/redo/...
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
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
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};
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
));
132 static char_u
*eval_map_expr
__ARGS((char_u
*str
));
136 * Free and clear a buffer.
140 struct buffheader
*buf
;
142 struct buffblock
*p
, *np
;
144 for (p
= buf
->bh_first
.b_next
; p
!= NULL
; p
= np
)
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.
157 get_buffcont(buffer
, dozero
)
158 struct buffheader
*buffer
;
159 int dozero
; /* count == zero is not an error */
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
)
174 for (bp
= buffer
->bh_first
.b_next
; bp
!= NULL
; bp
= bp
->b_next
)
175 for (str
= bp
->b_str
; *str
; )
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.
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.
201 if ((int)len
>= last_recorded_len
)
203 len
-= last_recorded_len
;
208 * When stopping recording from Insert mode with CTRL-O q, also remove the
211 if (len
> 0 && restart_edit
!= 0 && p
[len
- 1] == Ctrl_O
)
218 * Return the contents of the redo buffer as a single string.
219 * K_SPECIAL and CSI in the returned string are escaped.
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.
232 add_buff(buf
, s
, slen
)
233 struct buffheader
*buf
;
235 long slen
; /* length of "s" or -1 */
241 slen
= (long)STRLEN(s
);
242 if (slen
== 0) /* don't add empty strings */
245 if (buf
->bh_first
.b_next
== NULL
) /* first add to list */
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"));
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);
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
;
269 if (slen
< MINIMAL_SIZE
)
273 p
= (struct buffblock
*)lalloc((long_u
)(sizeof(struct buffblock
) + len
),
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
;
288 * Add number "n" to buffer "buf".
292 struct buffheader
*buf
;
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.
306 add_char_buff(buf
, c
)
307 struct buffheader
*buf
;
311 char_u bytes
[MB_MAXBYTES
+ 1];
321 len
= (*mb_char2bytes
)(c
, bytes
);
322 for (i
= 0; i
< len
; ++i
)
328 if (IS_SPECIAL(c
) || c
== K_SPECIAL
|| c
== NUL
)
330 /* translate special key code into three byte sequence */
332 temp
[1] = K_SECOND(c
);
333 temp
[2] = K_THIRD(c
);
339 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
342 temp
[2] = (int)KE_CSI
;
351 add_buff(buf
, temp
, -1L);
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.
367 struct buffblock
*curr
;
369 if (stuffbuff
.bh_first
.b_next
== NULL
) /* buffer is empty */
372 curr
= stuffbuff
.bh_first
.b_next
;
373 c
= curr
->b_str
[stuffbuff
.bh_index
];
377 if (curr
->b_str
[++stuffbuff
.bh_index
] == NUL
)
379 stuffbuff
.bh_first
.b_next
= curr
->b_next
;
381 stuffbuff
.bh_index
= 0;
388 * Prepare the stuff buffer for reading (if it contains something).
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.
406 return (stuffbuff
.bh_first
.b_next
== NULL
);
410 * Set a typeahead character that won't be flushed.
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).
425 flush_buffers(typeahead
)
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
;
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;
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.
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;
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
);
496 add_buff(&redobuff
, s
, -1L);
503 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
504 * Used after executing autocommands and user functions.
509 if (--save_level
== 0)
511 free_buff(&redobuff
);
512 redobuff
= save_redobuff
;
513 free_buff(&old_redobuff
);
514 old_redobuff
= save_old_redobuff
;
520 * Append "s" to the redo buffer.
521 * K_SPECIAL and CSI should already have been escaped.
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.
536 AppendToRedobuffLit(str
, len
)
538 int len
; /* length of "str" or -1 for up to the NUL */
547 while (len
< 0 ? *s
!= NUL
: s
- str
< len
)
549 /* Put a string of normal characters in the redo buffer (that's
554 && *s
< DEL
/* EBCDIC: all chars above space are normal */
556 && (len
< 0 || s
- str
< len
))
559 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
561 if (*s
== NUL
&& (s
[-1] == '0' || s
[-1] == '^'))
564 add_buff(&redobuff
, start
, (long)(s
- start
));
566 if (*s
== NUL
|| (len
>= 0 && s
- str
>= len
))
569 /* Handle a special or multibyte character. */
572 /* Handle composing chars separately. */
573 c
= mb_cptr2char_adv(&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')
583 add_buff(&redobuff
, (char_u
*)"xf0", 3L);
585 add_buff(&redobuff
, (char_u
*)"048", 3L);
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.
597 AppendCharToRedobuff(c
)
601 add_char_buff(&redobuff
, c
);
605 * Append a number to the redo buffer.
608 AppendNumberToRedobuff(n
)
612 add_num_buff(&redobuff
, n
);
616 * Append string "s" to the stuff buffer.
617 * CSI and K_SPECIAL must already have been escaped.
623 add_buff(&stuffbuff
, s
, -1L);
627 stuffReadbuffLen(s
, 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.
645 if (*s
== K_SPECIAL
&& s
[1] != NUL
&& s
[2] != NUL
)
647 /* Insert special key literally. */
648 stuffReadbuffLen(s
, 3L);
653 stuffcharReadbuff(mb_ptr2char_adv(&s
));
655 stuffcharReadbuff(*s
++);
662 * Append a character to the stuff buffer.
663 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
669 add_char_buff(&stuffbuff
, c
);
673 * Append a number to the stuff buffer.
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
688 * if old is TRUE, use old_redobuff instead of redobuff
691 read_redo(init
, old_redo
)
695 static struct buffblock
*bp
;
700 char_u buf
[MB_MAXBYTES
];
707 bp
= old_redobuff
.bh_first
.b_next
;
709 bp
= redobuff
.bh_first
.b_next
;
717 /* Reverse the conversion done by add_char_buff() */
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
);
728 if (c
== K_SPECIAL
) /* special key or escaped K_SPECIAL */
730 c
= TO_SPECIAL(p
[1], p
[2]);
734 if (c
== CSI
) /* escaped CSI */
737 if (*++p
== NUL
&& bp
->b_next
!= NULL
)
744 if (i
== n
- 1) /* last byte of a character */
747 c
= (*mb_ptr2char
)(buf
);
751 if (c
== NUL
) /* cannot happen? */
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.
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
)
791 /* init the pointers; return if nothing to redo */
792 if (read_redo(TRUE
, old_redo
) == FAIL
)
795 c
= read_redo(FALSE
, old_redo
);
797 /* copy the buffer name, if present */
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')
806 add_char_buff(&stuffbuff
, c
);
807 c
= read_redo(FALSE
, old_redo
);
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
);
822 /* try to enter the count (in place of a previous 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
);
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
846 if (read_redo(TRUE
, FALSE
) == FAIL
)
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
);
861 /* copy the typed text from the redo buffer into the stuff buffer */
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.
881 if (typebuf
.tb_buf
== NULL
)
883 typebuf
.tb_buf
= typebuf_init
;
884 typebuf
.tb_noremap
= noremapbuf_init
;
885 typebuf
.tb_buflen
= TYPELEN_INIT
;
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
)
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
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 */
959 if (s1
== NULL
) /* out of memory */
962 if (s2
== NULL
) /* out of memory */
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
,
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
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
);
983 mch_memmove(s2
+ newoff
, typebuf
.tb_noremap
+ typebuf
.tb_off
,
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
)
999 else if (noremap
== REMAP_SKIP
)
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
1012 if (noremap
== REMAP_SKIP
)
1014 else if (noremap
< 0)
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
;
1032 if (typebuf
.tb_no_abbr_cnt
&& offset
== 0) /* and not used for abbrev.s */
1033 typebuf
.tb_no_abbr_cnt
+= addlen
;
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
1049 char_u buf
[MB_MAXBYTES
];
1056 buf
[1] = K_SECOND(c
);
1057 buf
[2] = K_THIRD(c
);
1063 buf
[(*mb_char2bytes
)(c
, buf
)] = NUL
;
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
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
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").
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).
1109 return typebuf
.tb_maplen
;
1114 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1117 del_typebuf(len
, offset
)
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[]
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
;
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
;
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
;
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
;
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.
1197 gotchars(chars
, len
)
1206 /* remember how many chars were last recorded */
1208 last_recorded_len
+= len
;
1213 /* Handle one byte at a time; no translation to be done. */
1220 add_buff(&recordbuff
, buf
, 1L);
1226 /* output "debug mode" message next time in debug mode */
1227 debug_did_msg
= FALSE
;
1230 /* Since characters have been typed, consider the following to be in
1231 * another mapping. Search string will be kept in history. */
1236 * Sync undo. Called when typed characters are obtained from the typeahead
1237 * buffer, or when a menu is used.
1239 * - In Insert mode, unless cursor key has been used.
1240 * - While reading a script file.
1241 * - When no_u_sync is non-zero.
1246 if ((!(State
& (INSERT
+ CMDLINE
)) || arrow_used
)
1247 && scriptin
[curscript
] == NULL
)
1252 * Make "typebuf" empty and allocate new buffers.
1253 * Returns FAIL when out of memory.
1258 typebuf
.tb_buf
= alloc(TYPELEN_INIT
);
1259 typebuf
.tb_noremap
= alloc(TYPELEN_INIT
);
1260 if (typebuf
.tb_buf
== NULL
|| typebuf
.tb_noremap
== NULL
)
1265 typebuf
.tb_buflen
= TYPELEN_INIT
;
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;
1277 * Free the buffers of "typebuf".
1282 if (typebuf
.tb_buf
== typebuf_init
)
1283 EMSG2(_(e_intern2
), "Free typebuf 1");
1285 vim_free(typebuf
.tb_buf
);
1286 if (typebuf
.tb_buf
== noremapbuf_init
)
1287 EMSG2(_(e_intern2
), "Free typebuf 2");
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
];
1302 saved_typebuf
[curscript
] = typebuf
;
1303 /* If out of memory: restore typebuf and close file. */
1304 if (alloc_typebuf() == FAIL
)
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.
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();
1334 * Restore the typeahead to what it was before calling save_typeahead().
1335 * The allocated memory is freed, can only be called once!
1338 restore_typeahead(tp
)
1341 if (tp
->typebuf_valid
)
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
);
1356 * Open a new script file for the ":source!" command.
1359 openscript(name
, directly
)
1361 int directly
; /* when TRUE execute directly */
1363 if (curscript
+ 1 == NSCRIPT
)
1370 /* Not reading from script, also don't open one. Warning message? */
1374 if (scriptin
[curscript
] != NULL
) /* already reading script */
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
);
1385 if (save_typebuf() == FAIL
)
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.
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
;
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' */
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
);
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.
1435 typebuf
= saved_typebuf
[curscript
];
1437 fclose(scriptin
[curscript
]);
1438 scriptin
[curscript
] = NULL
;
1443 #if defined(EXITFREE) || defined(PROTO)
1447 while (scriptin
[0] != NULL
)
1452 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1454 * Return TRUE when reading keys from a script file.
1459 return scriptin
[curscript
] != NULL
;
1464 * This function is called just before doing a blocking wait. Thus after
1465 * waiting 'updatetime' for a character to arrive.
1472 if (may_garbage_collect
)
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.
1488 static int count
= 0;
1492 if (c
== 0 || (p_uc
> 0 && ++count
>= p_uc
))
1494 ml_sync_all(c
== 0, TRUE
);
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
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".
1520 char_u buf
[MB_MAXBYTES
];
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
)
1532 * If a character was put back with vungetc, it was already processed.
1533 * Return it directly.
1539 mod_mask
= old_mod_mask
;
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 */
1552 c
= vgetorpeek(TRUE
);
1559 /* Get two extra bytes for special keys */
1566 int save_allow_keys
= allow_keys
;
1569 allow_keys
= 0; /* make sure BS is not found */
1570 c2
= vgetorpeek(TRUE
); /* no mapping for these chars */
1571 c
= vgetorpeek(TRUE
);
1573 allow_keys
= save_allow_keys
;
1574 if (c2
== KS_MODIFIER
)
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 */
1589 /* get menu path, it ends with a <CR> */
1590 for (i
= 0; (c
= vgetorpeek(TRUE
)) != '\r'; )
1597 gui_make_tearoff(name
);
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
);
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
);
1621 /* Translate K_CSI to CSI. The special key is only used to avoid
1622 * it being recognized as the start of a special key. */
1629 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1630 * Delete the 3 here.
1632 else if (c
== K_NUL
&& vpeekc() == 3)
1633 (void)vgetorpeek(TRUE
);
1636 /* a keypad or special function key was not mapped, use it like
1637 * its ASCII equivalent */
1640 case K_KPLUS
: c
= '+'; break;
1641 case K_KMINUS
: c
= '-'; break;
1642 case K_KDIVIDE
: c
= '/'; break;
1643 case K_KMULTIPLY
: c
= '*'; break;
1644 case K_KENTER
: c
= CAR
; break;
1647 /* Can be either '.' or a ',', *
1648 * depending on the type of keypad. */
1649 c
= MapVirtualKey(VK_DECIMAL
, 2); break;
1653 case K_K0
: c
= '0'; break;
1654 case K_K1
: c
= '1'; break;
1655 case K_K2
: c
= '2'; break;
1656 case K_K3
: c
= '3'; break;
1657 case K_K4
: c
= '4'; break;
1658 case K_K5
: c
= '5'; break;
1659 case K_K6
: c
= '6'; break;
1660 case K_K7
: c
= '7'; break;
1661 case K_K8
: c
= '8'; break;
1662 case K_K9
: c
= '9'; break;
1665 case K_ZHOME
: if (mod_mask
== MOD_MASK_SHIFT
)
1670 else if (mod_mask
== MOD_MASK_CTRL
)
1679 case K_ZEND
: if (mod_mask
== MOD_MASK_SHIFT
)
1684 else if (mod_mask
== MOD_MASK_CTRL
)
1693 case K_XUP
: c
= K_UP
; break;
1694 case K_XDOWN
: c
= K_DOWN
; break;
1695 case K_XLEFT
: c
= K_LEFT
; break;
1696 case K_XRIGHT
: c
= K_RIGHT
; break;
1700 /* For a multi-byte character get all the bytes and return the
1701 * converted character.
1702 * Note: This will loop until enough bytes are received!
1704 if (has_mbyte
&& (n
= MB_BYTE2LEN_CHECK(c
)) > 1)
1708 for (i
= 1; i
< n
; ++i
)
1710 buf
[i
] = vgetorpeek(TRUE
);
1711 if (buf
[i
] == K_SPECIAL
1717 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1718 * which represents a K_SPECIAL (0x80),
1719 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1721 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1722 c
= vgetorpeek(TRUE
);
1723 if (vgetorpeek(TRUE
) == (int)KE_CSI
&& c
== KS_EXTRA
)
1728 c
= (*mb_ptr2char
)(buf
);
1738 * In the main loop "may_garbage_collect" can be set to do garbage
1739 * collection in the first next vgetc(). It's disabled after that to
1740 * avoid internally used Lists and Dicts to be freed.
1742 may_garbage_collect
= FALSE
;
1749 * Like vgetc(), but never return a NUL when called recursively, get a key
1750 * directly from the user (ignoring typeahead).
1759 c
= get_keystroke();
1764 * Like safe_vgetc(), but loop to handle K_IGNORE.
1765 * Also ignore scrollbar events.
1775 } while (c
== K_IGNORE
|| c
== K_VER_SCROLLBAR
|| c
== K_HOR_SCROLLBAR
);
1780 * Check if a character is available, such that vgetc() will not block.
1781 * If the next character is a special character or multi-byte, the returned
1782 * character is not valid!.
1789 return vgetorpeek(FALSE
);
1792 #if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1794 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1811 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1813 * Check if any character is available, also half an escape sequence.
1814 * Trick: when no typeahead found, but there is something in the typeahead
1815 * buffer, it must be an ESC that is recognized as the start of a key code.
1823 if (c
== NUL
&& typebuf
.tb_len
> 0)
1830 * Call vpeekc() without causing anything to be mapped.
1831 * Return TRUE if a character is available, FALSE otherwise.
1841 return (retval
!= NUL
);
1845 vungetc(c
) /* unget one character (can only be done once!) */
1849 old_mod_mask
= mod_mask
;
1854 * 1. from the stuffbuffer
1855 * This is used for abbreviated commands like "D" -> "d$".
1856 * Also used to redo a command for ".".
1857 * 2. from the typeahead buffer
1858 * Stores text obtained previously but not used yet.
1859 * Also stores the result of mappings.
1860 * Also used for the ":normal" command.
1862 * This may do a blocking wait if "advance" is TRUE.
1864 * if "advance" is TRUE (vgetc()):
1865 * really get the character.
1866 * KeyTyped is set to TRUE in the case the user typed the key.
1867 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1868 * if "advance" is FALSE (vpeekc()):
1869 * just look whether there is a character available.
1871 * When "no_mapping" is zero, checks for mappings in the current mode.
1872 * Only returns one byte (of a multi-byte character).
1873 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1883 #ifdef FEAT_LOCALMAP
1886 mapblock_T
*mp_match
;
1887 int mp_match_len
= 0;
1888 int timedout
= FALSE
; /* waited for more than 1 second
1889 for mapping to complete */
1890 int mapdepth
= 0; /* check for recursive mapping */
1891 int mode_deleted
= FALSE
; /* set when mode has been deleted */
1896 #ifdef FEAT_CMDL_INFO
1897 int new_wcol
, new_wrow
;
1903 int shape_changed
= FALSE
; /* adjusted cursor shape */
1909 int old_wcol
, old_wrow
;
1913 * This function doesn't work very well when called recursively. This may
1914 * happen though, because of:
1915 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1916 * there is a character available, which calls this function. In that
1917 * case we must return NUL, to indicate no character is available.
1918 * 2. A GUI callback function writes to the screen, causing a
1920 * Using ":normal" can also do this, but it saves the typeahead buffer,
1921 * thus it should be OK. But don't get a key from the user then.
1924 #ifdef FEAT_EX_EXTRA
1925 && ex_normal_busy
== 0
1930 local_State
= get_real_state();
1939 if (advance
&& typebuf
.tb_maplen
== 0)
1944 * get a character: 1. from the stuffbuffer
1946 if (typeahead_char
!= 0)
1953 c
= read_stuff(advance
);
1954 if (c
!= NUL
&& !got_int
)
1958 /* KeyTyped = FALSE; When the command that stuffed something
1959 * was typed, behave like the stuffed command was typed.
1960 * needed for CTRL-W CTRl-] to open a fold, for example. */
1963 if (typebuf
.tb_no_abbr_cnt
== 0)
1964 typebuf
.tb_no_abbr_cnt
= 1; /* no abbreviations now */
1969 * Loop until we either find a matching mapped key, or we
1970 * are sure that it is not a mapped key.
1971 * If a mapped key sequence is found we go back to the start to
1977 * ui_breakcheck() is slow, don't use it too often when
1978 * inside a mapping. But call it each time for typed
1981 if (typebuf
.tb_maplen
)
1984 ui_breakcheck(); /* check for CTRL-C */
1988 /* flush all input */
1989 c
= inchar(typebuf
.tb_buf
, typebuf
.tb_buflen
- 1, 0L,
1990 typebuf
.tb_change_cnt
);
1992 * If inchar() returns TRUE (script file was active) or we
1993 * are inside a mapping, get out of insert mode.
1994 * Otherwise we behave like having gotten a CTRL-C.
1995 * As a result typing CTRL-C in insert mode will
1996 * really insert a CTRL-C.
1998 if ((c
|| typebuf
.tb_maplen
)
1999 && (State
& (INSERT
+ CMDLINE
)))
2003 flush_buffers(TRUE
); /* flush all typeahead */
2007 /* Also record this character, it might be needed to
2008 * get out of Insert mode. */
2009 *typebuf
.tb_buf
= c
;
2010 gotchars(typebuf
.tb_buf
, 1);
2016 else if (typebuf
.tb_len
> 0)
2019 * Check for a mappable key sequence.
2020 * Walk through one maphash[] list until we find an
2021 * entry that matches.
2023 * Don't look for mappings if:
2024 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2025 * - maphash_valid not set: no mappings present.
2026 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2027 * - in insert or cmdline mode and 'paste' option set
2028 * - waiting for "hit return to continue" and CR or SPACE
2030 * - waiting for a char with --more--
2031 * - in Ctrl-X mode, and we get a valid char for that mode
2035 c1
= typebuf
.tb_buf
[typebuf
.tb_off
];
2036 if (no_mapping
== 0 && maphash_valid
2037 && (no_zero_mapping
== 0 || c1
!= '0')
2038 && (typebuf
.tb_maplen
== 0
2040 && (typebuf
.tb_noremap
[typebuf
.tb_off
]
2041 & (RM_NONE
|RM_ABBR
)) == 0))
2042 && !(p_paste
&& (State
& (INSERT
+ CMDLINE
)))
2043 && !(State
== HITRETURN
&& (c1
== CAR
|| c1
== ' '))
2046 #ifdef FEAT_INS_EXPAND
2047 && !((ctrl_x_mode
!= 0 && vim_is_ctrl_x_key(c1
))
2048 || ((compl_cont_status
& CONT_LOCAL
)
2049 && (c1
== Ctrl_N
|| c1
== Ctrl_P
)))
2054 if (c1
== K_SPECIAL
)
2058 LANGMAP_ADJUST(c1
, TRUE
);
2062 #ifdef FEAT_LOCALMAP
2063 /* First try buffer-local mappings. */
2064 mp
= curbuf
->b_maphash
[MAP_HASH(local_State
, c1
)];
2065 mp2
= maphash
[MAP_HASH(local_State
, c1
)];
2072 mp
= maphash
[MAP_HASH(local_State
, c1
)];
2075 * Loop until a partly matching mapping is found or
2076 * all (local) mappings have been checked.
2077 * The longest full match is remembered in "mp_match".
2078 * A full match is only accepted if there is no partly
2079 * match, so "aa" and "aaa" can both be mapped.
2084 #ifdef FEAT_LOCALMAP
2085 mp
->m_next
== NULL
? (mp
= mp2
, mp2
= NULL
) :
2090 * Only consider an entry if the first character
2091 * matches and it is for the current state.
2092 * Skip ":lmap" mappings if keys were mapped.
2094 if (mp
->m_keys
[0] == c1
2095 && (mp
->m_mode
& local_State
)
2096 && ((mp
->m_mode
& LANGMAP
) == 0
2097 || typebuf
.tb_maplen
== 0))
2100 int nomap
= nolmaplen
;
2103 /* find the match length of this mapping */
2104 for (mlen
= 1; mlen
< typebuf
.tb_len
; ++mlen
)
2107 c2
= typebuf
.tb_buf
[typebuf
.tb_off
+ mlen
];
2110 else if (c2
== K_SPECIAL
)
2113 LANGMAP_ADJUST(c2
, TRUE
);
2114 if (mp
->m_keys
[mlen
] != c2
)
2116 if (mp
->m_keys
[mlen
] !=
2117 typebuf
.tb_buf
[typebuf
.tb_off
+ mlen
])
2123 /* Don't allow mapping the first byte(s) of a
2124 * multi-byte char. Happens when mapping
2125 * <M-a> and then changing 'encoding'. */
2126 if (has_mbyte
&& MB_BYTE2LEN(c1
)
2127 > (*mb_ptr2len
)(mp
->m_keys
))
2131 * Check an entry whether it matches.
2132 * - Full match: mlen == keylen
2133 * - Partly match: mlen == typebuf.tb_len
2135 keylen
= mp
->m_keylen
;
2137 || (mlen
== typebuf
.tb_len
2138 && typebuf
.tb_len
< keylen
))
2141 * If only script-local mappings are
2142 * allowed, check if the mapping starts
2145 s
= typebuf
.tb_noremap
+ typebuf
.tb_off
;
2147 && (mp
->m_keys
[0] != K_SPECIAL
2148 || mp
->m_keys
[1] != KS_EXTRA
2153 * If one of the typed keys cannot be
2154 * remapped, skip the entry.
2156 for (n
= mlen
; --n
>= 0; )
2157 if (*s
++ & (RM_NONE
|RM_ABBR
))
2162 if (keylen
> typebuf
.tb_len
)
2166 /* break at a partly match */
2167 keylen
= KL_PART_MAP
;
2171 else if (keylen
> mp_match_len
)
2173 /* found a longer match */
2175 mp_match_len
= keylen
;
2179 /* No match; may have to check for
2180 * termcode at next character. */
2181 if (max_mlen
< mlen
)
2186 /* If no partly match found, use the longest full
2188 if (keylen
!= KL_PART_MAP
)
2191 keylen
= mp_match_len
;
2195 /* Check for match with 'pastetoggle' */
2196 if (*p_pt
!= NUL
&& mp
== NULL
&& (State
& (INSERT
|NORMAL
)))
2198 for (mlen
= 0; mlen
< typebuf
.tb_len
&& p_pt
[mlen
];
2200 if (p_pt
[mlen
] != typebuf
.tb_buf
[typebuf
.tb_off
2203 if (p_pt
[mlen
] == NUL
) /* match */
2205 /* write chars to script file(s) */
2206 if (mlen
> typebuf
.tb_maplen
)
2207 gotchars(typebuf
.tb_buf
+ typebuf
.tb_off
2208 + typebuf
.tb_maplen
,
2209 mlen
- typebuf
.tb_maplen
);
2211 del_typebuf(mlen
, 0); /* remove the chars */
2212 set_option_value((char_u
*)"paste",
2213 (long)!p_paste
, NULL
, 0);
2214 if (!(State
& INSERT
))
2218 msg_clr_eos(); /* clear ruler */
2224 /* Need more chars for partly match. */
2225 if (mlen
== typebuf
.tb_len
)
2226 keylen
= KL_PART_KEY
;
2227 else if (max_mlen
< mlen
)
2228 /* no match, may have to check for termcode at
2230 max_mlen
= mlen
+ 1;
2233 if ((mp
== NULL
|| max_mlen
>= mp_match_len
)
2234 && keylen
!= KL_PART_MAP
)
2237 * When no matching mapping found or found a
2238 * non-matching mapping that matches at least what the
2239 * matching mapping matched:
2240 * Check if we have a terminal code, when:
2241 * mapping is allowed,
2242 * keys have not been mapped,
2243 * and not an ESC sequence, not in insert mode or
2245 * and when not timed out,
2247 if ((no_mapping
== 0 || allow_keys
!= 0)
2248 && (typebuf
.tb_maplen
== 0
2249 || (p_remap
&& typebuf
.tb_noremap
[
2250 typebuf
.tb_off
] == RM_YES
))
2253 keylen
= check_termcode(max_mlen
+ 1, NULL
, 0);
2256 * When getting a partial match, but the last
2257 * characters were not typed, don't wait for a
2258 * typed character to complete the termcode.
2259 * This helps a lot when a ":normal" command ends
2263 && typebuf
.tb_len
== typebuf
.tb_maplen
)
2268 if (keylen
== 0) /* no matching terminal code */
2270 #ifdef AMIGA /* check for window bounds report */
2271 if (typebuf
.tb_maplen
== 0 && (typebuf
.tb_buf
[
2272 typebuf
.tb_off
] & 0xff) == CSI
)
2274 for (s
= typebuf
.tb_buf
+ typebuf
.tb_off
+ 1;
2275 s
< typebuf
.tb_buf
+ typebuf
.tb_off
2277 && (VIM_ISDIGIT(*s
) || *s
== ';'
2281 if (*s
== 'r' || *s
== '|') /* found one */
2283 del_typebuf((int)(s
+ 1 -
2284 (typebuf
.tb_buf
+ typebuf
.tb_off
)), 0);
2285 /* get size and redraw screen */
2289 if (*s
== NUL
) /* need more characters */
2290 keylen
= KL_PART_KEY
;
2294 /* When there was a matching mapping and no
2295 * termcode could be replaced after another one,
2296 * use that mapping. */
2300 * get a character: 2. from the typeahead buffer
2302 c
= typebuf
.tb_buf
[typebuf
.tb_off
] & 255;
2303 if (advance
) /* remove chars from tb_buf */
2305 cmd_silent
= (typebuf
.tb_silent
> 0);
2306 if (typebuf
.tb_maplen
> 0)
2311 /* write char to script file(s) */
2312 gotchars(typebuf
.tb_buf
2313 + typebuf
.tb_off
, 1);
2315 KeyNoremap
= typebuf
.tb_noremap
[
2319 break; /* got character, break for loop */
2322 if (keylen
> 0) /* full matching terminal code */
2324 #if defined(FEAT_GUI) && defined(FEAT_MENU)
2325 if (typebuf
.tb_buf
[typebuf
.tb_off
] == K_SPECIAL
2326 && typebuf
.tb_buf
[typebuf
.tb_off
+ 1]
2330 * Using a menu may cause a break in undo!
2331 * It's like using gotchars(), but without
2332 * recording or writing to a script file.
2336 idx
= get_menu_index(current_menu
, local_State
);
2337 if (idx
!= MENU_INDEX_INVALID
)
2341 * In Select mode and a Visual mode menu
2342 * is used: Switch to Visual mode
2343 * temporarily. Append K_SELECT to switch
2344 * back to Select mode.
2346 if (VIsual_active
&& VIsual_select
2347 && (current_menu
->modes
& VISUAL
))
2349 VIsual_select
= FALSE
;
2350 (void)ins_typebuf(K_SELECT_STRING
,
2351 REMAP_NONE
, 0, TRUE
, FALSE
);
2354 ins_typebuf(current_menu
->strings
[idx
],
2355 current_menu
->noremap
[idx
],
2357 current_menu
->silent
[idx
]);
2360 #endif /* FEAT_GUI && FEAT_MENU */
2361 continue; /* try mapping again */
2364 /* Partial match: get some more characters. When a
2365 * matching mapping was found use that one. */
2366 if (mp
== NULL
|| keylen
< 0)
2367 keylen
= KL_PART_KEY
;
2369 keylen
= mp_match_len
;
2372 /* complete match */
2373 if (keylen
>= 0 && keylen
<= typebuf
.tb_len
)
2375 /* write chars to script file(s) */
2376 if (keylen
> typebuf
.tb_maplen
)
2377 gotchars(typebuf
.tb_buf
+ typebuf
.tb_off
2378 + typebuf
.tb_maplen
,
2379 keylen
- typebuf
.tb_maplen
);
2381 cmd_silent
= (typebuf
.tb_silent
> 0);
2382 del_typebuf(keylen
, 0); /* remove the mapped keys */
2385 * Put the replacement string in front of mapstr.
2386 * The depth check catches ":map x y" and ":map y x".
2388 if (++mapdepth
>= p_mmd
)
2390 EMSG(_("E223: recursive mapping"));
2391 if (State
& CMDLINE
)
2395 flush_buffers(FALSE
);
2396 mapdepth
= 0; /* for next one */
2403 * In Select mode and a Visual mode mapping is used:
2404 * Switch to Visual mode temporarily. Append K_SELECT
2405 * to switch back to Select mode.
2407 if (VIsual_active
&& VIsual_select
2408 && (mp
->m_mode
& VISUAL
))
2410 VIsual_select
= FALSE
;
2411 (void)ins_typebuf(K_SELECT_STRING
, REMAP_NONE
,
2418 * Handle ":map <expr>": evaluate the {rhs} as an
2419 * expression. Save and restore the typeahead so that
2420 * getchar() can be used. Also save and restore the
2421 * command line for "normal :".
2426 int save_vgetc_busy
= vgetc_busy
;
2428 save_typeahead(&tabuf
);
2429 if (tabuf
.typebuf_valid
)
2432 s
= eval_map_expr(mp
->m_str
);
2433 vgetc_busy
= save_vgetc_busy
;
2437 restore_typeahead(&tabuf
);
2444 * Insert the 'to' part in the typebuf.tb_buf.
2445 * If 'from' field is the same as the start of the
2446 * 'to' field, don't remap the first character (but do
2447 * allow abbreviations).
2448 * If m_noremap is set, don't remap the whole 'to'
2456 mp
->m_noremap
!= REMAP_YES
2458 : STRNCMP(s
, mp
->m_keys
,
2459 (size_t)keylen
) != 0
2460 ? REMAP_YES
: REMAP_SKIP
,
2461 0, TRUE
, cmd_silent
|| mp
->m_silent
);
2477 * get a character: 3. from the user - handle <Esc> in Insert mode
2480 * special case: if we get an <ESC> in insert mode and there
2481 * are no more characters at once, we pretend to go out of
2482 * insert mode. This prevents the one second delay after
2483 * typing an <ESC>. If we get something after all, we may
2484 * have to redisplay the mode. That the cursor is in the wrong
2485 * place does not matter.
2488 #ifdef FEAT_CMDL_INFO
2489 new_wcol
= curwin
->w_wcol
;
2490 new_wrow
= curwin
->w_wrow
;
2493 && typebuf
.tb_len
== 1
2494 && typebuf
.tb_buf
[typebuf
.tb_off
] == ESC
2496 #ifdef FEAT_EX_EXTRA
2497 && ex_normal_busy
== 0
2499 && typebuf
.tb_maplen
== 0
2501 && (p_timeout
|| (keylen
== KL_PART_KEY
&& p_ttimeout
))
2502 && (c
= inchar(typebuf
.tb_buf
+ typebuf
.tb_off
2503 + typebuf
.tb_len
, 3, 25L,
2504 typebuf
.tb_change_cnt
)) == 0)
2506 colnr_T col
= 0, vcol
;
2512 mode_deleted
= TRUE
;
2515 /* may show different cursor shape */
2522 gui_update_cursor(TRUE
, FALSE
);
2524 shape_changed
= TRUE
;
2528 old_wcol
= curwin
->w_wcol
;
2529 old_wrow
= curwin
->w_wrow
;
2531 /* move cursor left, if possible */
2532 if (curwin
->w_cursor
.col
!= 0)
2534 if (curwin
->w_wcol
> 0)
2539 * We are expecting to truncate the trailing
2540 * white-space, so find the last non-white
2543 col
= vcol
= curwin
->w_wcol
= 0;
2544 ptr
= ml_get_curline();
2545 while (col
< curwin
->w_cursor
.col
)
2547 if (!vim_iswhite(ptr
[col
]))
2548 curwin
->w_wcol
= vcol
;
2549 vcol
+= lbr_chartabsize(ptr
+ col
,
2553 col
+= (*mb_ptr2len
)(ptr
+ col
);
2558 curwin
->w_wrow
= curwin
->w_cline_row
2559 + curwin
->w_wcol
/ W_WIDTH(curwin
);
2560 curwin
->w_wcol
%= W_WIDTH(curwin
);
2561 curwin
->w_wcol
+= curwin_col_off();
2563 col
= 0; /* no correction needed */
2570 col
= curwin
->w_cursor
.col
- 1;
2574 else if (curwin
->w_p_wrap
&& curwin
->w_wrow
)
2577 curwin
->w_wcol
= W_WIDTH(curwin
) - 1;
2579 col
= curwin
->w_cursor
.col
- 1;
2583 if (has_mbyte
&& col
> 0 && curwin
->w_wcol
> 0)
2585 /* Correct when the cursor is on the right halve
2586 * of a double-wide character. */
2587 ptr
= ml_get_curline();
2588 col
-= (*mb_head_off
)(ptr
, ptr
+ col
);
2589 if ((*mb_ptr2cells
)(ptr
+ col
) > 1)
2596 #ifdef FEAT_CMDL_INFO
2597 new_wcol
= curwin
->w_wcol
;
2598 new_wrow
= curwin
->w_wrow
;
2600 curwin
->w_wcol
= old_wcol
;
2601 curwin
->w_wrow
= old_wrow
;
2604 continue; /* end of input script reached */
2605 typebuf
.tb_len
+= c
;
2607 /* buffer full, don't map */
2608 if (typebuf
.tb_len
>= typebuf
.tb_maplen
+ MAXMAPLEN
)
2614 #ifdef FEAT_EX_EXTRA
2615 if (ex_normal_busy
> 0)
2621 /* No typeahead left and inside ":normal". Must return
2622 * something to avoid getting stuck. When an incomplete
2623 * mapping is present, behave like it timed out. */
2624 if (typebuf
.tb_len
> 0)
2629 /* When 'insertmode' is set, ESC just beeps in Insert
2630 * mode. Use CTRL-L to make edit() return.
2631 * For the command line only CTRL-C always breaks it.
2632 * For the cmdline window: Alternate between ESC and
2633 * CTRL-C: ESC for most situations and CTRL-C to close the
2634 * cmdline window. */
2635 if (p_im
&& (State
& INSERT
))
2637 else if ((State
& CMDLINE
)
2639 || (cmdwin_type
> 0 && tc
== ESC
)
2653 * get a character: 3. from the user - update display
2655 /* In insert mode a screen update is skipped when characters
2656 * are still available. But when those available characters
2657 * are part of a mapping, and we are going to do a blocking
2658 * wait here. Need to update the screen to display the
2659 * changed text so far. */
2660 if ((State
& INSERT
) && advance
&& must_redraw
!= 0)
2663 setcursor(); /* put cursor back where it belongs */
2667 * If we have a partial match (and are going to wait for more
2668 * input from the user), show the partially matched characters
2669 * to the user with showcmd.
2671 #ifdef FEAT_CMDL_INFO
2675 if (typebuf
.tb_len
> 0 && advance
&& !exmode_active
)
2677 if (((State
& (NORMAL
| INSERT
)) || State
== LANGMAP
)
2678 && State
!= HITRETURN
)
2680 /* this looks nice when typing a dead character map */
2682 && ptr2cells(typebuf
.tb_buf
+ typebuf
.tb_off
2683 + typebuf
.tb_len
- 1) == 1)
2685 edit_putchar(typebuf
.tb_buf
[typebuf
.tb_off
2686 + typebuf
.tb_len
- 1], FALSE
);
2687 setcursor(); /* put cursor back where it belongs */
2690 #ifdef FEAT_CMDL_INFO
2691 /* need to use the col and row from above here */
2692 old_wcol
= curwin
->w_wcol
;
2693 old_wrow
= curwin
->w_wrow
;
2694 curwin
->w_wcol
= new_wcol
;
2695 curwin
->w_wrow
= new_wrow
;
2697 if (typebuf
.tb_len
> SHOWCMD_COLS
)
2698 i
= typebuf
.tb_len
- SHOWCMD_COLS
;
2699 while (i
< typebuf
.tb_len
)
2700 (void)add_to_showcmd(typebuf
.tb_buf
[typebuf
.tb_off
2702 curwin
->w_wcol
= old_wcol
;
2703 curwin
->w_wrow
= old_wrow
;
2707 /* this looks nice when typing a dead character map */
2708 if ((State
& CMDLINE
)
2709 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2710 && cmdline_star
== 0
2712 && ptr2cells(typebuf
.tb_buf
+ typebuf
.tb_off
2713 + typebuf
.tb_len
- 1) == 1)
2715 putcmdline(typebuf
.tb_buf
[typebuf
.tb_off
2716 + typebuf
.tb_len
- 1], FALSE
);
2722 * get a character: 3. from the user - get it
2724 wait_tb_len
= typebuf
.tb_len
;
2725 c
= inchar(typebuf
.tb_buf
+ typebuf
.tb_off
+ typebuf
.tb_len
,
2726 typebuf
.tb_buflen
- typebuf
.tb_off
- typebuf
.tb_len
- 1,
2729 : ((typebuf
.tb_len
== 0
2730 || !(p_timeout
|| (p_ttimeout
2731 && keylen
== KL_PART_KEY
)))
2733 : ((keylen
== KL_PART_KEY
&& p_ttm
>= 0)
2735 : p_tm
)), typebuf
.tb_change_cnt
);
2737 #ifdef FEAT_CMDL_INFO
2745 if (State
& CMDLINE
)
2747 setcursor(); /* put cursor back where it belongs */
2751 continue; /* end of input script reached */
2752 if (c
== NUL
) /* no character available */
2756 if (wait_tb_len
> 0) /* timed out */
2763 { /* allow mapping for just typed characters */
2764 while (typebuf
.tb_buf
[typebuf
.tb_off
2765 + typebuf
.tb_len
] != NUL
)
2766 typebuf
.tb_noremap
[typebuf
.tb_off
2767 + typebuf
.tb_len
++] = RM_YES
;
2768 #ifdef USE_IM_CONTROL
2769 /* Get IM status right after getting keys, not after the
2770 * timeout for a mapping (focus may be lost by then). */
2771 vgetc_im_active
= im_get_status();
2775 } /* if (!character from stuffbuf) */
2777 /* if advance is FALSE don't loop on NULs */
2778 } while (c
< 0 || (advance
&& c
== NUL
));
2781 * The "INSERT" message is taken care of here:
2782 * if we return an ESC to exit insert mode, the message is deleted
2783 * if we don't return an ESC but deleted the message before, redisplay it
2785 if (advance
&& p_smd
&& msg_silent
== 0 && (State
& INSERT
))
2787 if (c
== ESC
&& !mode_deleted
&& !no_mapping
&& mode_displayed
)
2789 if (typebuf
.tb_len
&& !KeyTyped
)
2790 redraw_cmdline
= TRUE
; /* delete mode later */
2794 else if (c
!= ESC
&& mode_deleted
)
2796 if (typebuf
.tb_len
&& !KeyTyped
)
2797 redraw_cmdline
= TRUE
; /* show mode later */
2803 /* may unshow different cursor shape */
2804 if (gui
.in_use
&& shape_changed
)
2805 gui_update_cursor(TRUE
, FALSE
);
2814 * inchar() - get one character from
2818 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2819 * NUL terminated (buffer length must be 'maxlen' + 1).
2820 * Minimum for "maxlen" is 3!!!!
2822 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2823 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2824 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2827 * If we got an interrupt all input is read until none is available.
2829 * If wait_time == 0 there is no waiting for the char.
2830 * If wait_time == n we wait for n msec for a character to arrive.
2831 * If wait_time == -1 we wait forever for a character to arrive.
2833 * Return the number of obtained characters.
2834 * Return -1 when end of input script reached.
2837 inchar(buf
, maxlen
, wait_time
, tb_change_cnt
)
2840 long wait_time
; /* milli seconds */
2843 int len
= 0; /* init for GCC */
2844 int retesc
= FALSE
; /* return ESC with gotint */
2847 if (wait_time
== -1L || wait_time
> 100L) /* flush output before waiting */
2854 gui_update_cursor(FALSE
, FALSE
);
2855 # ifdef FEAT_MOUSESHAPE
2856 if (postponed_mouseshape
)
2857 update_mouseshape(-1);
2864 * Don't reset these when at the hit-return prompt, otherwise a endless
2865 * recursive loop may result (write error in swapfile, hit-return, timeout
2866 * on char wait, flush swapfile, write error....).
2868 if (State
!= HITRETURN
)
2870 did_outofmem_msg
= FALSE
; /* display out of memory message (again) */
2871 did_swapwrite_msg
= FALSE
; /* display swap file write error again */
2873 undo_off
= FALSE
; /* restart undo now */
2876 * Get a character from a script file if there is one.
2877 * If interrupted: Stop reading script files, close them all.
2880 while (scriptin
[curscript
] != NULL
&& script_char
< 0
2886 if (got_int
|| (script_char
= getc(scriptin
[curscript
])) < 0)
2889 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2890 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2893 * When reading script file is interrupted, return an ESC to get
2894 * back to normal mode.
2895 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2904 buf
[0] = script_char
;
2909 if (script_char
< 0) /* did not get a character from script */
2912 * If we got an interrupt, skip all previously typed characters and
2913 * return TRUE if quit reading script file.
2914 * Stop reading typeahead when a single CTRL-C was read,
2915 * fill_input_buf() returns this when not able to read from stdin.
2916 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2917 * and buf may be pointing inside typebuf.tb_buf[].
2921 #define DUM_LEN MAXMAPLEN * 3 + 3
2922 char_u dum
[DUM_LEN
+ 1];
2926 len
= ui_inchar(dum
, DUM_LEN
, 0L, 0);
2927 if (len
== 0 || (len
== 1 && dum
[0] == 3))
2934 * Always flush the output characters when getting input characters
2940 * Fill up to a third of the buffer, because each character may be
2943 len
= ui_inchar(buf
, maxlen
/ 3, wait_time
, tb_change_cnt
);
2946 if (typebuf_changed(tb_change_cnt
))
2949 return fix_input_buffer(buf
, len
, script_char
>= 0);
2953 * Fix typed characters for use by vgetc() and check_termcode().
2954 * buf[] must have room to triple the number of bytes!
2955 * Returns the new length.
2958 fix_input_buffer(buf
, len
, script
)
2961 int script
; /* TRUE when reading from a script */
2967 * Two characters are special: NUL and K_SPECIAL.
2968 * When compiled With the GUI CSI is also special.
2969 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
2970 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
2971 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
2972 * Don't replace K_SPECIAL when reading a script file.
2974 for (i
= len
; --i
>= 0; ++p
)
2977 /* When the GUI is used any character can come after a CSI, don't
2979 if (gui
.in_use
&& p
[0] == CSI
&& i
>= 2)
2984 /* When the GUI is not used CSI needs to be escaped. */
2985 else if (!gui
.in_use
&& p
[0] == CSI
)
2987 mch_memmove(p
+ 3, p
+ 1, (size_t)i
);
2995 if (p
[0] == NUL
|| (p
[0] == K_SPECIAL
&& !script
2997 /* timeout may generate K_CURSORHOLD */
2998 && (i
< 2 || p
[1] != KS_EXTRA
|| p
[2] != (int)KE_CURSORHOLD
)
3000 #if defined(WIN3264) && !defined(FEAT_GUI)
3001 /* Win32 console passes modifiers */
3002 && (i
< 2 || p
[1] != KS_MODIFIER
)
3006 mch_memmove(p
+ 3, p
+ 1, (size_t)i
);
3007 p
[2] = K_THIRD(p
[0]);
3008 p
[1] = K_SECOND(p
[0]);
3014 *p
= NUL
; /* add trailing NUL */
3018 #if defined(USE_INPUT_BUF) || defined(PROTO)
3020 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3021 * Normally the input buffer would be sufficient, but the server_to_input_buf()
3022 * or feedkeys() may insert characters in the typeahead buffer while we are
3023 * waiting for input to arrive.
3028 return (!vim_is_input_buf_empty()
3029 # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3030 || typebuf_was_filled
3037 * map[!] : show all key mappings
3038 * map[!] {lhs} : show key mapping for {lhs}
3039 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3040 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3041 * unmap[!] {lhs} : remove key mapping for {lhs}
3042 * abbr : show all abbreviations
3043 * abbr {lhs} : show abbreviations for {lhs}
3044 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3045 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3046 * unabbr {lhs} : remove abbreviation for {lhs}
3048 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3050 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3051 * it will be modified.
3053 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
3054 * for :map! mode is INSERT + CMDLINE
3055 * for :cmap mode is CMDLINE
3056 * for :imap mode is INSERT
3057 * for :lmap mode is LANGMAP
3058 * for :nmap mode is NORMAL
3059 * for :vmap mode is VISUAL + SELECTMODE
3060 * for :xmap mode is VISUAL
3061 * for :smap mode is SELECTMODE
3062 * for :omap mode is OP_PENDING
3064 * for :abbr mode is INSERT + CMDLINE
3065 * for :iabbr mode is INSERT
3066 * for :cabbr mode is CMDLINE
3068 * Return 0 for success
3069 * 1 for invalid arguments
3072 * 5 for entry not unique
3075 do_map(maptype
, arg
, mode
, abbrev
)
3079 int abbrev
; /* not a mapping but an abbreviation */
3082 mapblock_T
*mp
, **mpp
;
3086 int len
= 0; /* init for GCC */
3091 #ifdef FEAT_LOCALMAP
3092 int did_local
= FALSE
;
3095 char_u
*keys_buf
= NULL
;
3096 char_u
*arg_buf
= NULL
;
3101 mapblock_T
**abbr_table
;
3102 mapblock_T
**map_table
;
3105 int special
= FALSE
;
3112 map_table
= maphash
;
3113 abbr_table
= &first_abbr
;
3115 /* For ":noremap" don't remap, otherwise do remap. */
3117 noremap
= REMAP_NONE
;
3119 noremap
= REMAP_YES
;
3121 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
3124 #ifdef FEAT_LOCALMAP
3126 * Check for "<buffer>": mapping local to buffer.
3128 if (STRNCMP(keys
, "<buffer>", 8) == 0)
3130 keys
= skipwhite(keys
+ 8);
3131 map_table
= curbuf
->b_maphash
;
3132 abbr_table
= &curbuf
->b_first_abbr
;
3138 * Check for "<silent>": don't echo commands.
3140 if (STRNCMP(keys
, "<silent>", 8) == 0)
3142 keys
= skipwhite(keys
+ 8);
3148 * Check for "<special>": accept special keys in <>
3150 if (STRNCMP(keys
, "<special>", 9) == 0)
3152 keys
= skipwhite(keys
+ 9);
3159 * Check for "<script>": remap script-local mappings only
3161 if (STRNCMP(keys
, "<script>", 8) == 0)
3163 keys
= skipwhite(keys
+ 8);
3164 noremap
= REMAP_SCRIPT
;
3169 * Check for "<expr>": {rhs} is an expression.
3171 if (STRNCMP(keys
, "<expr>", 6) == 0)
3173 keys
= skipwhite(keys
+ 6);
3179 * Check for "<unique>": don't overwrite an existing mapping.
3181 if (STRNCMP(keys
, "<unique>", 8) == 0)
3183 keys
= skipwhite(keys
+ 8);
3193 * find end of keys and skip CTRL-Vs (and backslashes) in it
3194 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
3195 * with :unmap white space is included in the keys, no argument possible
3198 do_backslash
= (vim_strchr(p_cpo
, CPO_BSLASH
) == NULL
);
3199 while (*p
&& (maptype
== 1 || !vim_iswhite(*p
)))
3201 if ((p
[0] == Ctrl_V
|| (do_backslash
&& p
[0] == '\\')) &&
3203 ++p
; /* skip CTRL-V or backslash */
3210 hasarg
= (*rhs
!= NUL
);
3211 haskey
= (*keys
!= NUL
);
3213 /* check for :unmap without argument */
3214 if (maptype
== 1 && !haskey
)
3221 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3222 * with the appropriate two bytes. If it is a shifted special key, unshift
3223 * it too, giving another two bytes.
3224 * replace_termcodes() may move the result to allocated memory, which
3225 * needs to be freed later (*keys_buf and *arg_buf).
3226 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3229 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, special
);
3232 if (STRICMP(rhs
, "<nop>") == 0) /* "<Nop>" means nothing */
3235 rhs
= replace_termcodes(rhs
, &arg_buf
, FALSE
, TRUE
, special
);
3240 * when in right-to-left mode and alternate keymap option set,
3241 * reverse the character flow in the rhs in Farsi.
3243 if (p_altkeymap
&& curwin
->w_p_rl
)
3248 * check arguments and translate function keys
3252 len
= (int)STRLEN(keys
);
3253 if (len
> MAXMAPLEN
) /* maximum length of MAXMAPLEN chars */
3259 if (abbrev
&& maptype
!= 1)
3262 * If an abbreviation ends in a keyword character, the
3263 * rest must be all keyword-char or all non-keyword-char.
3264 * Otherwise we won't be able to find the start of it in a
3265 * vi-compatible way.
3273 first
= vim_iswordp(keys
);
3275 p
= keys
+ (*mb_ptr2len
)(keys
);
3277 while (p
< keys
+ len
)
3279 ++n
; /* nr of (multi-byte) chars */
3280 last
= vim_iswordp(p
); /* type of last char */
3281 if (same
== -1 && last
!= first
)
3282 same
= n
- 1; /* count of same char type */
3283 p
+= (*mb_ptr2len
)(p
);
3285 if (last
&& n
> 2 && same
>= 0 && same
< n
- 1)
3293 if (vim_iswordc(keys
[len
- 1])) /* ends in keyword char */
3294 for (n
= 0; n
< len
- 2; ++n
)
3295 if (vim_iswordc(keys
[n
]) != vim_iswordc(keys
[len
- 2]))
3300 /* An abbrevation cannot contain white space. */
3301 for (n
= 0; n
< len
; ++n
)
3302 if (vim_iswhite(keys
[n
]))
3310 if (haskey
&& hasarg
&& abbrev
) /* if we will add an abbreviation */
3311 no_abbr
= FALSE
; /* reset flag that indicates there are
3314 if (!haskey
|| (maptype
!= 1 && !hasarg
))
3317 #ifdef FEAT_LOCALMAP
3319 * Check if a new local mapping wasn't already defined globally.
3321 if (map_table
== curbuf
->b_maphash
&& haskey
&& hasarg
&& maptype
!= 1)
3323 /* need to loop over all global hash lists */
3324 for (hash
= 0; hash
< 256 && !got_int
; ++hash
)
3328 if (hash
!= 0) /* there is only one abbreviation list */
3334 for ( ; mp
!= NULL
&& !got_int
; mp
= mp
->m_next
)
3336 /* check entries with the same mode */
3337 if ((mp
->m_mode
& mode
) != 0
3338 && mp
->m_keylen
== len
3340 && STRNCMP(mp
->m_keys
, keys
, (size_t)len
) == 0)
3343 EMSG2(_("E224: global abbreviation already exists for %s"),
3346 EMSG2(_("E225: global mapping already exists for %s"),
3356 * When listing global mappings, also list buffer-local ones here.
3358 if (map_table
!= curbuf
->b_maphash
&& !hasarg
&& maptype
!= 1)
3360 /* need to loop over all global hash lists */
3361 for (hash
= 0; hash
< 256 && !got_int
; ++hash
)
3365 if (hash
!= 0) /* there is only one abbreviation list */
3367 mp
= curbuf
->b_first_abbr
;
3370 mp
= curbuf
->b_maphash
[hash
];
3371 for ( ; mp
!= NULL
&& !got_int
; mp
= mp
->m_next
)
3373 /* check entries with the same mode */
3374 if ((mp
->m_mode
& mode
) != 0)
3376 if (!haskey
) /* show all entries */
3384 if (STRNCMP(mp
->m_keys
, keys
,
3385 (size_t)(n
< len
? n
: len
)) == 0)
3398 * Find an entry in the maphash[] list that matches.
3399 * For :unmap we may loop two times: once to try to unmap an entry with a
3400 * matching 'from' part, a second time, if the first fails, to unmap an
3401 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3402 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3403 * "bar" because of the abbreviation.
3405 for (round
= 0; (round
== 0 || maptype
== 1) && round
<= 1
3406 && !did_it
&& !got_int
; ++round
)
3408 /* need to loop over all hash lists */
3409 for (hash
= 0; hash
< 256 && !got_int
; ++hash
)
3413 if (hash
> 0) /* there is only one abbreviation list */
3418 mpp
= &(map_table
[hash
]);
3419 for (mp
= *mpp
; mp
!= NULL
&& !got_int
; mp
= *mpp
)
3422 if (!(mp
->m_mode
& mode
)) /* skip entries with wrong mode */
3424 mpp
= &(mp
->m_next
);
3427 if (!haskey
) /* show all entries */
3429 showmap(mp
, map_table
!= maphash
);
3432 else /* do we have a match? */
3434 if (round
) /* second round: Try unmap "rhs" string */
3436 n
= (int)STRLEN(mp
->m_str
);
3444 if (STRNCMP(p
, keys
, (size_t)(n
< len
? n
: len
)) == 0)
3446 if (maptype
== 1) /* delete entry */
3448 /* Only accept a full match. For abbreviations we
3449 * ignore trailing space when matching with the
3450 * "lhs", since an abbreviation can't have
3451 * trailing space. */
3452 if (n
!= len
&& (!abbrev
|| round
|| n
> len
3453 || *skipwhite(keys
+ n
) != NUL
))
3455 mpp
= &(mp
->m_next
);
3459 * We reset the indicated mode bits. If nothing is
3460 * left the entry is deleted below.
3462 mp
->m_mode
&= ~mode
;
3463 did_it
= TRUE
; /* remember we did something */
3465 else if (!hasarg
) /* show matching entry */
3467 showmap(mp
, map_table
!= maphash
);
3470 else if (n
!= len
) /* new entry is ambiguous */
3472 mpp
= &(mp
->m_next
);
3478 EMSG2(_("E226: abbreviation already exists for %s"),
3481 EMSG2(_("E227: mapping already exists for %s"), p
);
3485 else /* new rhs for existing entry */
3487 mp
->m_mode
&= ~mode
; /* remove mode bits */
3488 if (mp
->m_mode
== 0 && !did_it
) /* reuse entry */
3490 newstr
= vim_strsave(rhs
);
3493 retval
= 4; /* no mem */
3496 vim_free(mp
->m_str
);
3498 mp
->m_noremap
= noremap
;
3499 mp
->m_silent
= silent
;
3503 mp
->m_script_ID
= current_SID
;
3508 if (mp
->m_mode
== 0) /* entry can be deleted */
3511 continue; /* continue with *mpp */
3515 * May need to put this entry into another hash list.
3517 new_hash
= MAP_HASH(mp
->m_mode
, mp
->m_keys
[0]);
3518 if (!abbrev
&& new_hash
!= hash
)
3521 mp
->m_next
= map_table
[new_hash
];
3522 map_table
[new_hash
] = mp
;
3524 continue; /* continue with *mpp */
3528 mpp
= &(mp
->m_next
);
3533 if (maptype
== 1) /* delete entry */
3536 retval
= 2; /* no match */
3540 if (!haskey
|| !hasarg
) /* print entries */
3543 #ifdef FEAT_LOCALMAP
3549 MSG(_("No abbreviation found"));
3551 MSG(_("No mapping found"));
3553 goto theend
; /* listing finished */
3556 if (did_it
) /* have added the new entry already */
3560 * Get here when adding a new entry to the maphash[] list or abbrlist.
3562 mp
= (mapblock_T
*)alloc((unsigned)sizeof(mapblock_T
));
3565 retval
= 4; /* no mem */
3569 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3570 if (*keys
== Ctrl_C
)
3571 mapped_ctrl_c
= TRUE
;
3573 mp
->m_keys
= vim_strsave(keys
);
3574 mp
->m_str
= vim_strsave(rhs
);
3575 if (mp
->m_keys
== NULL
|| mp
->m_str
== NULL
)
3577 vim_free(mp
->m_keys
);
3578 vim_free(mp
->m_str
);
3580 retval
= 4; /* no mem */
3583 mp
->m_keylen
= (int)STRLEN(mp
->m_keys
);
3584 mp
->m_noremap
= noremap
;
3585 mp
->m_silent
= silent
;
3589 mp
->m_script_ID
= current_SID
;
3592 /* add the new entry in front of the abbrlist or maphash[] list */
3595 mp
->m_next
= *abbr_table
;
3600 n
= MAP_HASH(mp
->m_mode
, mp
->m_keys
[0]);
3601 mp
->m_next
= map_table
[n
];
3612 * Delete one entry from the abbrlist or maphash[].
3613 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3622 vim_free(mp
->m_keys
);
3623 vim_free(mp
->m_str
);
3629 * Initialize maphash[] for first use.
3636 vim_memset(maphash
, 0, sizeof(maphash
));
3637 maphash_valid
= TRUE
;
3642 * Get the mapping mode from the command name.
3645 get_map_mode(cmdp
, forceit
)
3656 mode
= INSERT
; /* :imap */
3657 else if (modec
== 'l')
3658 mode
= LANGMAP
; /* :lmap */
3659 else if (modec
== 'c')
3660 mode
= CMDLINE
; /* :cmap */
3661 else if (modec
== 'n' && *p
!= 'o') /* avoid :noremap */
3662 mode
= NORMAL
; /* :nmap */
3663 else if (modec
== 'v')
3664 mode
= VISUAL
+ SELECTMODE
; /* :vmap */
3665 else if (modec
== 'x')
3666 mode
= VISUAL
; /* :xmap */
3667 else if (modec
== 's')
3668 mode
= SELECTMODE
; /* :smap */
3669 else if (modec
== 'o')
3670 mode
= OP_PENDING
; /* :omap */
3675 mode
= INSERT
+ CMDLINE
; /* :map ! */
3677 mode
= VISUAL
+ SELECTMODE
+ NORMAL
+ OP_PENDING
;/* :map */
3685 * Clear all mappings or abbreviations.
3686 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3690 map_clear(cmdp
, arg
, forceit
, abbr
)
3697 #ifdef FEAT_LOCALMAP
3700 local
= (STRCMP(arg
, "<buffer>") == 0);
3701 if (!local
&& *arg
!= NUL
)
3708 mode
= get_map_mode(&cmdp
, forceit
);
3709 map_clear_int(curbuf
, mode
,
3710 #ifdef FEAT_LOCALMAP
3719 * Clear all mappings in "mode".
3723 map_clear_int(buf
, mode
, local
, abbr
)
3724 buf_T
*buf
; /* buffer for local mappings */
3725 int mode
; /* mode in which to delete */
3726 int local
; /* TRUE for buffer-local mappings */
3727 int abbr
; /* TRUE for abbreviations */
3729 mapblock_T
*mp
, **mpp
;
3735 for (hash
= 0; hash
< 256; ++hash
)
3739 if (hash
> 0) /* there is only one abbrlist */
3741 #ifdef FEAT_LOCALMAP
3743 mpp
= &buf
->b_first_abbr
;
3750 #ifdef FEAT_LOCALMAP
3752 mpp
= &buf
->b_maphash
[hash
];
3755 mpp
= &maphash
[hash
];
3757 while (*mpp
!= NULL
)
3760 if (mp
->m_mode
& mode
)
3762 mp
->m_mode
&= ~mode
;
3763 if (mp
->m_mode
== 0) /* entry can be deleted */
3769 * May need to put this entry into another hash list.
3771 new_hash
= MAP_HASH(mp
->m_mode
, mp
->m_keys
[0]);
3772 if (!abbr
&& new_hash
!= hash
)
3775 #ifdef FEAT_LOCALMAP
3778 mp
->m_next
= buf
->b_maphash
[new_hash
];
3779 buf
->b_maphash
[new_hash
] = mp
;
3784 mp
->m_next
= maphash
[new_hash
];
3785 maphash
[new_hash
] = mp
;
3787 continue; /* continue with *mpp */
3790 mpp
= &(mp
->m_next
);
3798 int local
; /* TRUE for buffer-local map */
3802 if (msg_didout
|| msg_silent
!= 0)
3804 if ((mp
->m_mode
& (INSERT
+ CMDLINE
)) == INSERT
+ CMDLINE
)
3805 msg_putchar('!'); /* :map! */
3806 else if (mp
->m_mode
& INSERT
)
3807 msg_putchar('i'); /* :imap */
3808 else if (mp
->m_mode
& LANGMAP
)
3809 msg_putchar('l'); /* :lmap */
3810 else if (mp
->m_mode
& CMDLINE
)
3811 msg_putchar('c'); /* :cmap */
3812 else if ((mp
->m_mode
& (NORMAL
+ VISUAL
+ SELECTMODE
+ OP_PENDING
))
3813 == NORMAL
+ VISUAL
+ SELECTMODE
+ OP_PENDING
)
3814 msg_putchar(' '); /* :map */
3818 if (mp
->m_mode
& NORMAL
)
3820 msg_putchar('n'); /* :nmap */
3823 if (mp
->m_mode
& OP_PENDING
)
3825 msg_putchar('o'); /* :omap */
3828 if ((mp
->m_mode
& (VISUAL
+ SELECTMODE
)) == VISUAL
+ SELECTMODE
)
3830 msg_putchar('v'); /* :vmap */
3835 if (mp
->m_mode
& VISUAL
)
3837 msg_putchar('x'); /* :xmap */
3840 if (mp
->m_mode
& SELECTMODE
)
3842 msg_putchar('s'); /* :smap */
3850 /* Get length of what we write */
3851 len
= msg_outtrans_special(mp
->m_keys
, TRUE
);
3854 msg_putchar(' '); /* padd with blanks */
3858 if (mp
->m_noremap
== REMAP_NONE
)
3859 msg_puts_attr((char_u
*)"*", hl_attr(HLF_8
));
3860 else if (mp
->m_noremap
== REMAP_SCRIPT
)
3861 msg_puts_attr((char_u
*)"&", hl_attr(HLF_8
));
3870 /* Use FALSE below if we only want things like <Up> to show up as such on
3871 * the rhs, and not M-x etc, TRUE gets both -- webb
3873 if (*mp
->m_str
== NUL
)
3874 msg_puts_attr((char_u
*)"<Nop>", hl_attr(HLF_8
));
3876 msg_outtrans_special(mp
->m_str
, FALSE
);
3879 last_set_msg(mp
->m_script_ID
);
3881 out_flush(); /* show one line at a time */
3884 #if defined(FEAT_EVAL) || defined(PROTO)
3886 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3887 * Recognize termcap codes in "str".
3888 * Also checks mappings local to the current buffer.
3891 map_to_exists(str
, modechars
, abbr
)
3901 rhs
= replace_termcodes(str
, &buf
, FALSE
, TRUE
, FALSE
);
3903 if (vim_strchr(modechars
, 'n') != NULL
)
3905 if (vim_strchr(modechars
, 'v') != NULL
)
3906 mode
|= VISUAL
+ SELECTMODE
;
3907 if (vim_strchr(modechars
, 'x') != NULL
)
3909 if (vim_strchr(modechars
, 's') != NULL
)
3911 if (vim_strchr(modechars
, 'o') != NULL
)
3913 if (vim_strchr(modechars
, 'i') != NULL
)
3915 if (vim_strchr(modechars
, 'l') != NULL
)
3917 if (vim_strchr(modechars
, 'c') != NULL
)
3920 retval
= map_to_exists_mode(rhs
, mode
, abbr
);
3928 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
3929 * Also checks mappings local to the current buffer.
3932 map_to_exists_mode(rhs
, mode
, abbr
)
3939 # ifdef FEAT_LOCALMAP
3940 int expand_buffer
= FALSE
;
3944 /* Do it twice: once for global maps and once for local maps. */
3948 for (hash
= 0; hash
< 256; ++hash
)
3952 if (hash
> 0) /* there is only one abbr list */
3954 #ifdef FEAT_LOCALMAP
3956 mp
= curbuf
->b_first_abbr
;
3961 # ifdef FEAT_LOCALMAP
3962 else if (expand_buffer
)
3963 mp
= curbuf
->b_maphash
[hash
];
3967 for (; mp
; mp
= mp
->m_next
)
3969 if ((mp
->m_mode
& mode
)
3970 && strstr((char *)mp
->m_str
, (char *)rhs
) != NULL
)
3974 # ifdef FEAT_LOCALMAP
3977 expand_buffer
= TRUE
;
3984 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3986 * Used below when expanding mapping/abbreviation names.
3988 static int expand_mapmodes
= 0;
3989 static int expand_isabbrev
= 0;
3990 #ifdef FEAT_LOCALMAP
3991 static int expand_buffer
= FALSE
;
3995 * Work out what to complete when doing command line completion of mapping
3996 * or abbreviation names.
3999 set_context_in_map_cmd(xp
, cmd
, arg
, forceit
, isabbrev
, isunmap
, cmdidx
)
4003 int forceit
; /* TRUE if '!' given */
4004 int isabbrev
; /* TRUE if abbreviation */
4005 int isunmap
; /* TRUE if unmap/unabbrev command */
4008 if (forceit
&& cmdidx
!= CMD_map
&& cmdidx
!= CMD_unmap
)
4009 xp
->xp_context
= EXPAND_NOTHING
;
4013 expand_mapmodes
= get_map_mode(&cmd
, forceit
|| isabbrev
);
4016 expand_mapmodes
= INSERT
+ CMDLINE
;
4018 expand_mapmodes
+= VISUAL
+ SELECTMODE
+ NORMAL
+ OP_PENDING
;
4020 expand_isabbrev
= isabbrev
;
4021 xp
->xp_context
= EXPAND_MAPPINGS
;
4022 #ifdef FEAT_LOCALMAP
4023 expand_buffer
= FALSE
;
4027 #ifdef FEAT_LOCALMAP
4028 if (STRNCMP(arg
, "<buffer>", 8) == 0)
4030 expand_buffer
= TRUE
;
4031 arg
= skipwhite(arg
+ 8);
4035 if (STRNCMP(arg
, "<unique>", 8) == 0)
4037 arg
= skipwhite(arg
+ 8);
4040 if (STRNCMP(arg
, "<silent>", 8) == 0)
4042 arg
= skipwhite(arg
+ 8);
4046 if (STRNCMP(arg
, "<script>", 8) == 0)
4048 arg
= skipwhite(arg
+ 8);
4051 if (STRNCMP(arg
, "<expr>", 6) == 0)
4053 arg
= skipwhite(arg
+ 6);
4059 xp
->xp_pattern
= arg
;
4066 * Find all mapping/abbreviation names that match regexp 'prog'.
4067 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4068 * Return OK if matches found, FAIL otherwise.
4071 ExpandMappings(regmatch
, num_file
, file
)
4072 regmatch_T
*regmatch
;
4085 *num_file
= 0; /* return values in case of FAIL */
4089 * round == 1: Count the matches.
4090 * round == 2: Build the array to keep the matches.
4092 for (round
= 1; round
<= 2; ++round
)
4096 for (i
= 0; i
< 5; ++i
)
4099 p
= (char_u
*)"<silent>";
4101 p
= (char_u
*)"<unique>";
4104 p
= (char_u
*)"<script>";
4106 p
= (char_u
*)"<expr>";
4108 #ifdef FEAT_LOCALMAP
4109 else if (i
== 4 && !expand_buffer
)
4110 p
= (char_u
*)"<buffer>";
4115 if (vim_regexec(regmatch
, p
, (colnr_T
)0))
4120 (*file
)[count
++] = vim_strsave(p
);
4124 for (hash
= 0; hash
< 256; ++hash
)
4126 if (expand_isabbrev
)
4128 if (hash
> 0) /* only one abbrev list */
4129 break; /* for (hash) */
4132 #ifdef FEAT_LOCALMAP
4133 else if (expand_buffer
)
4134 mp
= curbuf
->b_maphash
[hash
];
4138 for (; mp
; mp
= mp
->m_next
)
4140 if (mp
->m_mode
& expand_mapmodes
)
4142 p
= translate_mapping(mp
->m_keys
, TRUE
);
4143 if (p
!= NULL
&& vim_regexec(regmatch
, p
, (colnr_T
)0))
4149 (*file
)[count
++] = p
;
4158 if (count
== 0) /* no match found */
4159 break; /* for (round) */
4163 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
4175 /* Sort the matches */
4176 sort_strings(*file
, count
);
4178 /* Remove multiple entries */
4181 ptr3
= ptr1
+ count
;
4185 if (STRCMP(*ptr1
, *ptr2
))
4196 return (count
== 0 ? FAIL
: OK
);
4198 #endif /* FEAT_CMDL_COMPL */
4201 * Check for an abbreviation.
4202 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4203 * "c" is the character typed before check_abbr was called. It may have
4204 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4206 * Historic vi practice: The last character of an abbreviation must be an id
4207 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4208 * characters or all non-id characters. This allows for abbr. "#i" to
4211 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4212 * Then there must be white space before the abbr.
4214 * return TRUE if there is an abbreviation, FALSE if not
4217 check_abbr(c
, ptr
, col
, mincol
)
4224 int scol
; /* starting column of the abbr. */
4228 char_u tb
[MB_MAXBYTES
+ 4];
4233 #ifdef FEAT_LOCALMAP
4237 int clen
= 0; /* length in characters */
4242 if (typebuf
.tb_no_abbr_cnt
) /* abbrev. are not recursive */
4244 if ((KeyNoremap
& (RM_NONE
|RM_SCRIPT
)) != 0)
4245 /* no remapping implies no abbreviation */
4249 * Check for word before the cursor: If it ends in a keyword char all
4250 * chars before it must be al keyword chars or non-keyword chars, but not
4251 * white space. If it ends in a non-keyword char we accept any characters
4252 * before it except white space.
4254 if (col
== 0) /* cannot be an abbr. */
4262 p
= mb_prevptr(ptr
, ptr
+ col
);
4263 if (!vim_iswordp(p
))
4264 vim_abbr
= TRUE
; /* Vim added abbr. */
4267 vim_abbr
= FALSE
; /* vi compatible abbr. */
4269 is_id
= vim_iswordp(mb_prevptr(ptr
, p
));
4272 while (p
> ptr
+ mincol
)
4274 p
= mb_prevptr(ptr
, p
);
4275 if (vim_isspace(*p
) || (!vim_abbr
&& is_id
!= vim_iswordp(p
)))
4277 p
+= (*mb_ptr2len
)(p
);
4282 scol
= (int)(p
- ptr
);
4287 if (!vim_iswordc(ptr
[col
- 1]))
4288 vim_abbr
= TRUE
; /* Vim added abbr. */
4291 vim_abbr
= FALSE
; /* vi compatible abbr. */
4293 is_id
= vim_iswordc(ptr
[col
- 2]);
4295 for (scol
= col
- 1; scol
> 0 && !vim_isspace(ptr
[scol
- 1])
4296 && (vim_abbr
|| is_id
== vim_iswordc(ptr
[scol
- 1])); --scol
)
4302 if (scol
< col
) /* there is a word in front of the cursor */
4306 #ifdef FEAT_LOCALMAP
4307 mp
= curbuf
->b_first_abbr
;
4318 #ifdef FEAT_LOCALMAP
4319 mp
->m_next
== NULL
? (mp
= mp2
, mp2
= NULL
) :
4323 /* find entries with right mode and keys */
4324 if ( (mp
->m_mode
& State
)
4325 && mp
->m_keylen
== len
4326 && !STRNCMP(mp
->m_keys
, ptr
, (size_t)len
))
4333 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4334 * This goes from end to start.
4336 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4337 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4338 * Characters where IS_SPECIAL() == TRUE: key codes, need
4339 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4341 * Character CTRL-] is treated specially - it completes the
4342 * abbreviation, but is not inserted into the input stream.
4345 /* special key code, split up */
4348 if (IS_SPECIAL(c
) || c
== K_SPECIAL
)
4350 tb
[j
++] = K_SPECIAL
;
4351 tb
[j
++] = K_SECOND(c
);
4352 tb
[j
++] = K_THIRD(c
);
4356 if (c
< ABBR_OFF
&& (c
< ' ' || c
> '~'))
4357 tb
[j
++] = Ctrl_V
; /* special char needs CTRL-V */
4361 /* if ABBR_OFF has been added, remove it here */
4364 j
+= (*mb_char2bytes
)(c
, tb
+ j
);
4371 /* insert the last typed char */
4372 (void)ins_typebuf(tb
, 1, 0, TRUE
, mp
->m_silent
);
4376 s
= eval_map_expr(mp
->m_str
);
4382 /* insert the to string */
4383 (void)ins_typebuf(s
, mp
->m_noremap
, 0, TRUE
, mp
->m_silent
);
4384 /* no abbrev. for these chars */
4385 typebuf
.tb_no_abbr_cnt
+= (int)STRLEN(s
) + j
+ 1;
4396 len
= clen
; /* Delete characters instead of bytes */
4398 while (len
-- > 0) /* delete the from string */
4399 (void)ins_typebuf(tb
, 1, 0, TRUE
, mp
->m_silent
);
4408 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4409 * special characters.
4420 save_cmd
= save_cmdline_alloc();
4421 if (save_cmd
== NULL
)
4424 /* Forbid changing text or using ":normal" to avoid most of the bad side
4425 * effects. Also restore the cursor position. */
4427 #ifdef FEAT_EX_EXTRA
4430 save_cursor
= curwin
->w_cursor
;
4431 p
= eval_to_string(str
, NULL
, FALSE
);
4433 #ifdef FEAT_EX_EXTRA
4436 curwin
->w_cursor
= save_cursor
;
4438 restore_cmdline_alloc(save_cmd
);
4441 res
= vim_strsave_escape_csi(p
);
4449 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4450 * can be put in the typeahead buffer.
4451 * Returns NULL when out of memory.
4454 vim_strsave_escape_csi(p
)
4460 /* Need a buffer to hold up to three times as much. */
4461 res
= alloc((unsigned)(STRLEN(p
) * 3) + 1);
4465 for (s
= p
; *s
!= NUL
; )
4467 if (s
[0] == K_SPECIAL
&& s
[1] != NUL
&& s
[2] != NUL
)
4469 /* Copy special key unmodified. */
4476 /* Add character, possibly multi-byte to destination, escaping
4477 * CSI and K_SPECIAL. */
4478 d
= add_char2buf(PTR2CHAR(s
), d
);
4488 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4489 * vim_strsave_escape_csi(). Works in-place.
4495 char_u
*s
= p
, *d
= p
;
4499 if (s
[0] == K_SPECIAL
&& s
[1] == KS_SPECIAL
&& s
[2] == KE_FILLER
)
4504 else if ((s
[0] == K_SPECIAL
|| s
[0] == CSI
)
4505 && s
[1] == KS_EXTRA
&& s
[2] == (int)KE_CSI
)
4517 * Write map commands for the current mappings to an .exrc file.
4518 * Return FAIL on error, OK otherwise.
4523 buf_T
*buf
; /* buffer for local mappings or NULL */
4531 int did_cpo
= FALSE
;
4537 * Do the loop twice: Once for mappings, once for abbreviations.
4538 * Then loop over all map hash lists.
4540 for (abbr
= 0; abbr
< 2; ++abbr
)
4541 for (hash
= 0; hash
< 256; ++hash
)
4545 if (hash
> 0) /* there is only one abbr list */
4547 #ifdef FEAT_LOCALMAP
4549 mp
= buf
->b_first_abbr
;
4556 #ifdef FEAT_LOCALMAP
4558 mp
= buf
->b_maphash
[hash
];
4564 for ( ; mp
; mp
= mp
->m_next
)
4566 /* skip script-local mappings */
4567 if (mp
->m_noremap
== REMAP_SCRIPT
)
4570 /* skip mappings that contain a <SNR> (script-local thing),
4571 * they probably don't work when loaded again */
4572 for (p
= mp
->m_str
; *p
!= NUL
; ++p
)
4573 if (p
[0] == K_SPECIAL
&& p
[1] == KS_EXTRA
4574 && p
[2] == (int)KE_SNR
)
4587 case NORMAL
+ VISUAL
+ SELECTMODE
+ OP_PENDING
:
4592 case VISUAL
+ SELECTMODE
:
4604 case NORMAL
+ VISUAL
+ SELECTMODE
:
4608 case VISUAL
+ SELECTMODE
+ OP_PENDING
:
4612 case NORMAL
+ OP_PENDING
:
4616 case CMDLINE
+ INSERT
:
4630 EMSG(_("E228: makemap: Illegal mode"));
4633 do /* may do this twice if c2 is set */
4635 /* When outputting <> form, need to make sure that 'cpo'
4636 * is set to the Vim default. */
4639 if (*mp
->m_str
== NUL
) /* will use <Nop> */
4642 for (i
= 0; i
< 2; ++i
)
4643 for (p
= (i
? mp
->m_str
: mp
->m_keys
); *p
; ++p
)
4644 if (*p
== K_SPECIAL
|| *p
== NL
)
4648 if (fprintf(fd
, "let s:cpo_save=&cpo") < 0
4650 || fprintf(fd
, "set cpo&vim") < 0
4655 if (c1
&& putc(c1
, fd
) < 0)
4657 if (mp
->m_noremap
!= REMAP_YES
&& fprintf(fd
, "nore") < 0)
4659 if (fprintf(fd
, cmd
) < 0)
4661 if (buf
!= NULL
&& fputs(" <buffer>", fd
) < 0)
4663 if (mp
->m_silent
&& fputs(" <silent>", fd
) < 0)
4666 if (mp
->m_noremap
== REMAP_SCRIPT
4667 && fputs("<script>", fd
) < 0)
4669 if (mp
->m_expr
&& fputs(" <expr>", fd
) < 0)
4673 if ( putc(' ', fd
) < 0
4674 || put_escstr(fd
, mp
->m_keys
, 0) == FAIL
4675 || putc(' ', fd
) < 0
4676 || put_escstr(fd
, mp
->m_str
, 1) == FAIL
4687 if (fprintf(fd
, "let &cpo=s:cpo_save") < 0
4689 || fprintf(fd
, "unlet s:cpo_save") < 0
4696 * write escape string to file
4697 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4699 * return FAIL for failure, OK otherwise
4702 put_escstr(fd
, strstart
, what
)
4707 char_u
*str
= strstart
;
4712 if (*str
== NUL
&& what
== 1)
4714 if (fprintf(fd
, "<Nop>") < 0)
4719 for ( ; *str
!= NUL
; ++str
)
4724 /* Check for a multi-byte character, which may contain escaped
4725 * K_SPECIAL and CSI bytes */
4726 p
= mb_unescape(&str
);
4730 if (fputc(*p
++, fd
) < 0)
4739 * Special key codes have to be translated to be able to make sense
4740 * when they are read back.
4742 if (c
== K_SPECIAL
&& what
!= 2)
4745 if (str
[1] == KS_MODIFIER
)
4753 c
= TO_SPECIAL(str
[1], str
[2]);
4756 if (IS_SPECIAL(c
) || modifiers
) /* special key */
4758 if (fprintf(fd
, (char *)get_special_key_name(c
, modifiers
)) < 0)
4765 * A '\n' in a map command should be written as <NL>.
4766 * A '\n' in a set command should be written as \^V^J.
4772 if (fprintf(fd
, IF_EB("\\\026\n", "\\" CTRL_V_STR
"\n")) < 0)
4777 if (fprintf(fd
, "<NL>") < 0)
4784 * Some characters have to be escaped with CTRL-V to
4785 * prevent them from misinterpreted in DoOneCmd().
4786 * A space, Tab and '"' has to be escaped with a backslash to
4787 * prevent it to be misinterpreted in do_set().
4788 * A space has to be escaped with a CTRL-V when it's at the start of a
4790 * A '<' has to be escaped with a CTRL-V to prevent it being
4791 * interpreted as the start of a special key name.
4792 * A space in the lhs of a :map needs a CTRL-V.
4794 if (what
== 2 && (vim_iswhite(c
) || c
== '"' || c
== '\\'))
4796 if (putc('\\', fd
) < 0)
4799 else if (c
< ' ' || c
> '~' || c
== '|'
4800 || (what
== 0 && c
== ' ')
4801 || (what
== 1 && str
== strstart
&& c
== ' ')
4802 || (what
!= 2 && c
== '<'))
4804 if (putc(Ctrl_V
, fd
) < 0)
4807 if (putc(c
, fd
) < 0)
4814 * Check all mappings for the presence of special key codes.
4815 * Used after ":set term=xxx".
4818 check_map_keycodes()
4827 #ifdef FEAT_LOCALMAP
4832 save_name
= sourcing_name
;
4833 sourcing_name
= (char_u
*)"mappings"; /* avoids giving error messages */
4835 #ifdef FEAT_LOCALMAP
4836 /* This this once for each buffer, and then once for global
4837 * mappings/abbreviations with bp == NULL */
4838 for (bp
= firstbuf
; ; bp
= bp
->b_next
)
4842 * Do the loop twice: Once for mappings, once for abbreviations.
4843 * Then loop over all map hash lists.
4845 for (abbr
= 0; abbr
<= 1; ++abbr
)
4846 for (hash
= 0; hash
< 256; ++hash
)
4850 if (hash
) /* there is only one abbr list */
4852 #ifdef FEAT_LOCALMAP
4854 mp
= bp
->b_first_abbr
;
4861 #ifdef FEAT_LOCALMAP
4863 mp
= bp
->b_maphash
[hash
];
4868 for ( ; mp
!= NULL
; mp
= mp
->m_next
)
4870 for (i
= 0; i
<= 1; ++i
) /* do this twice */
4873 p
= mp
->m_keys
; /* once for the "from" part */
4875 p
= mp
->m_str
; /* and once for the "to" part */
4878 if (*p
== K_SPECIAL
)
4881 if (*p
< 128) /* for "normal" tcap entries */
4886 (void)add_termcap_entry(buf
, FALSE
);
4895 #ifdef FEAT_LOCALMAP
4900 sourcing_name
= save_name
;
4905 * Check the string "keys" against the lhs of all mappings
4906 * Return pointer to rhs of mapping (mapblock->m_str)
4910 check_map(keys
, mode
, exact
, ign_mod
, abbr
)
4913 int exact
; /* require exact match */
4914 int ign_mod
; /* ignore preceding modifier */
4915 int abbr
; /* do abbreviations */
4921 #ifdef FEAT_LOCALMAP
4927 len
= (int)STRLEN(keys
);
4928 #ifdef FEAT_LOCALMAP
4929 for (local
= 1; local
>= 0; --local
)
4931 /* loop over all hash lists */
4932 for (hash
= 0; hash
< 256; ++hash
)
4936 if (hash
> 0) /* there is only one list. */
4938 #ifdef FEAT_LOCALMAP
4940 mp
= curbuf
->b_first_abbr
;
4945 #ifdef FEAT_LOCALMAP
4947 mp
= curbuf
->b_maphash
[hash
];
4951 for ( ; mp
!= NULL
; mp
= mp
->m_next
)
4953 /* skip entries with wrong mode, wrong length and not matching
4955 if ((mp
->m_mode
& mode
) && (!exact
|| mp
->m_keylen
== len
))
4957 if (len
> mp
->m_keylen
)
4958 minlen
= mp
->m_keylen
;
4962 if (ign_mod
&& s
[0] == K_SPECIAL
&& s
[1] == KS_MODIFIER
4966 if (len
> mp
->m_keylen
- 3)
4967 minlen
= mp
->m_keylen
- 3;
4969 if (STRNCMP(s
, keys
, minlen
) == 0)
4979 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
4981 #define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
4984 * Default mappings for some often used keys.
4986 static struct initmap
4992 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4993 /* Use the Windows (CUA) keybindings. */
4995 # if 0 /* These are now used to move tab pages */
4996 {(char_u
*)"<C-PageUp> H", NORMAL
+VIS_SEL
},
4997 {(char_u
*)"<C-PageUp> <C-O>H",INSERT
},
4998 {(char_u
*)"<C-PageDown> L$", NORMAL
+VIS_SEL
},
4999 {(char_u
*)"<C-PageDown> <C-O>L<C-O>$", INSERT
},
5002 /* paste, copy and cut */
5003 {(char_u
*)"<S-Insert> \"*P", NORMAL
},
5004 {(char_u
*)"<S-Insert> \"-d\"*P", VIS_SEL
},
5005 {(char_u
*)"<S-Insert> <C-R><C-O>*", INSERT
+CMDLINE
},
5006 {(char_u
*)"<C-Insert> \"*y", VIS_SEL
},
5007 {(char_u
*)"<S-Del> \"*d", VIS_SEL
},
5008 {(char_u
*)"<C-Del> \"*d", VIS_SEL
},
5009 {(char_u
*)"<C-X> \"*d", VIS_SEL
},
5010 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5012 # if 0 /* These are now used to move tab pages */
5013 {(char_u
*)"\316\204 H", NORMAL
+VIS_SEL
}, /* CTRL-PageUp is "H" */
5014 {(char_u
*)"\316\204 \017H",INSERT
}, /* CTRL-PageUp is "^OH"*/
5015 {(char_u
*)"\316v L$", NORMAL
+VIS_SEL
}, /* CTRL-PageDown is "L$" */
5016 {(char_u
*)"\316v \017L\017$", INSERT
}, /* CTRL-PageDown ="^OL^O$"*/
5018 {(char_u
*)"\316w <C-Home>", NORMAL
+VIS_SEL
},
5019 {(char_u
*)"\316w <C-Home>", INSERT
+CMDLINE
},
5020 {(char_u
*)"\316u <C-End>", NORMAL
+VIS_SEL
},
5021 {(char_u
*)"\316u <C-End>", INSERT
+CMDLINE
},
5023 /* paste, copy and cut */
5024 # ifdef FEAT_CLIPBOARD
5026 {(char_u
*)"\316\122 \"*P", NORMAL
}, /* SHIFT-Insert is "*P */
5027 {(char_u
*)"\316\122 \"-d\"*P", VIS_SEL
}, /* SHIFT-Insert is "-d"*P */
5028 {(char_u
*)"\316\122 \022\017*", INSERT
}, /* SHIFT-Insert is ^R^O* */
5029 {(char_u
*)"\316\222 \"*y", VIS_SEL
}, /* CTRL-Insert is "*y */
5030 # if 0 /* Shift-Del produces the same code as Del */
5031 {(char_u
*)"\316\123 \"*d", VIS_SEL
}, /* SHIFT-Del is "*d */
5033 {(char_u
*)"\316\223 \"*d", VIS_SEL
}, /* CTRL-Del is "*d */
5034 {(char_u
*)"\030 \"-d", VIS_SEL
}, /* CTRL-X is "-d */
5036 {(char_u
*)"\316\324 \"*P", NORMAL
}, /* SHIFT-Insert is "*P */
5037 {(char_u
*)"\316\324 \"-d\"*P", VIS_SEL
}, /* SHIFT-Insert is "-d"*P */
5038 {(char_u
*)"\316\324 \022\017*", INSERT
}, /* SHIFT-Insert is ^R^O* */
5039 {(char_u
*)"\316\325 \"*y", VIS_SEL
}, /* CTRL-Insert is "*y */
5040 {(char_u
*)"\316\327 \"*d", VIS_SEL
}, /* SHIFT-Del is "*d */
5041 {(char_u
*)"\316\330 \"*d", VIS_SEL
}, /* CTRL-Del is "*d */
5042 {(char_u
*)"\030 \"-d", VIS_SEL
}, /* CTRL-X is "-d */
5045 {(char_u
*)"\316\324 P", NORMAL
}, /* SHIFT-Insert is P */
5046 {(char_u
*)"\316\324 \"-dP", VIS_SEL
}, /* SHIFT-Insert is "-dP */
5047 {(char_u
*)"\316\324 \022\017\"", INSERT
}, /* SHIFT-Insert is ^R^O" */
5048 {(char_u
*)"\316\325 y", VIS_SEL
}, /* CTRL-Insert is y */
5049 {(char_u
*)"\316\327 d", VIS_SEL
}, /* SHIFT-Del is d */
5050 {(char_u
*)"\316\330 d", VIS_SEL
}, /* CTRL-Del is d */
5056 /* Use the Standard MacOS binding. */
5057 /* paste, copy and cut */
5058 {(char_u
*)"<D-v> \"*P", NORMAL
},
5059 {(char_u
*)"<D-v> \"-d\"*P", VIS_SEL
},
5060 {(char_u
*)"<D-v> <C-R>*", INSERT
+CMDLINE
},
5061 {(char_u
*)"<D-c> \"*y", VIS_SEL
},
5062 {(char_u
*)"<D-x> \"*d", VIS_SEL
},
5063 {(char_u
*)"<Backspace> \"-d", VIS_SEL
},
5071 * Set up default mappings.
5076 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
5079 for (i
= 0; i
< sizeof(initmappings
) / sizeof(struct initmap
); ++i
)
5080 add_map(initmappings
[i
].arg
, initmappings
[i
].mode
);
5084 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5085 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
5087 * Add a mapping "map" for mode "mode".
5088 * Need to put string in allocated memory, because do_map() will modify it.
5096 char_u
*cpo_save
= p_cpo
;
5098 p_cpo
= (char_u
*)""; /* Allow <> notation */
5099 s
= vim_strsave(map
);
5102 (void)do_map(0, s
, mode
, FALSE
);