Fix ":set go+=c" and menu autoenabling bugs.
[MacVim/jjgod.git] / src / getchar.c
blob4d6da38ddaeeb4652b7dcce88804f5fc19025633
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * getchar.c
13 * functions related with getting a character from the user/mapping/redo/...
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
19 #include "vim.h"
22 * These buffers are used for storing:
23 * - stuffed characters: A command that is translated into another command.
24 * - redo characters: will redo the last change.
25 * - recorded chracters: for the "q" command.
27 * The bytes are stored like in the typeahead buffer:
28 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
29 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
30 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
31 * otherwise switching the GUI on would make mappings invalid).
32 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
33 * These translations are also done on multi-byte characters!
35 * Escaping CSI bytes is done by the system-specific input functions, called
36 * by ui_inchar().
37 * Escaping K_SPECIAL is done by inchar().
38 * Un-escaping is done by vgetc().
41 #define MINIMAL_SIZE 20 /* minimal size for b_str */
43 static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44 static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
46 static struct buffheader save_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
47 static struct buffheader save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
48 #endif
49 static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
51 static int typeahead_char = 0; /* typeahead char that's not flushed */
54 * when block_redo is TRUE redo buffer will not be changed
55 * used by edit() to repeat insertions and 'V' command for redoing
57 static int block_redo = FALSE;
60 * Make a hash value for a mapping.
61 * "mode" is the lower 4 bits of the State for the mapping.
62 * "c1" is the first character of the "lhs".
63 * Returns a value between 0 and 255, index in maphash.
64 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode.
66 #define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) ? (c1) : ((c1) ^ 0x80))
69 * Each mapping is put in one of the 256 hash lists, to speed up finding it.
71 static mapblock_T *(maphash[256]);
72 static int maphash_valid = FALSE;
75 * List used for abbreviations.
77 static mapblock_T *first_abbr = NULL; /* first entry in abbrlist */
79 static int KeyNoremap = 0; /* remapping flags */
82 * variables used by vgetorpeek() and flush_buffers()
84 * typebuf.tb_buf[] contains all characters that are not consumed yet.
85 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
86 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
87 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
88 * The head of the buffer may contain the result of mappings, abbreviations
89 * and @a commands. The length of this part is typebuf.tb_maplen.
90 * typebuf.tb_silent is the part where <silent> applies.
91 * After the head are characters that come from the terminal.
92 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
93 * should not be considered for abbreviations.
94 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
95 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
96 * contains RM_NONE for the characters that are not to be remapped.
97 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
98 * (typebuf has been put in globals.h, because check_termcode() needs it).
100 #define RM_YES 0 /* tb_noremap: remap */
101 #define RM_NONE 1 /* tb_noremap: don't remap */
102 #define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */
103 #define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */
105 /* typebuf.tb_buf has three parts: room in front (for result of mappings), the
106 * middle for typeahead and room for new characters (which needs to be 3 *
107 * MAXMAPLEN) for the Amiga).
109 #define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
110 static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
111 static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
113 static int last_recorded_len = 0; /* number of last recorded chars */
115 static char_u *get_buffcont __ARGS((struct buffheader *, int));
116 static void add_buff __ARGS((struct buffheader *, char_u *, long n));
117 static void add_num_buff __ARGS((struct buffheader *, long));
118 static void add_char_buff __ARGS((struct buffheader *, int));
119 static int read_stuff __ARGS((int advance));
120 static void start_stuff __ARGS((void));
121 static int read_redo __ARGS((int, int));
122 static void copy_redo __ARGS((int));
123 static void init_typebuf __ARGS((void));
124 static void gotchars __ARGS((char_u *, int));
125 static void may_sync_undo __ARGS((void));
126 static void closescript __ARGS((void));
127 static int vgetorpeek __ARGS((int));
128 static void map_free __ARGS((mapblock_T **));
129 static void validate_maphash __ARGS((void));
130 static void showmap __ARGS((mapblock_T *mp, int local));
131 #ifdef FEAT_EVAL
132 static char_u *eval_map_expr __ARGS((char_u *str));
133 #endif
136 * Free and clear a buffer.
138 void
139 free_buff(buf)
140 struct buffheader *buf;
142 struct buffblock *p, *np;
144 for (p = buf->bh_first.b_next; p != NULL; p = np)
146 np = p->b_next;
147 vim_free(p);
149 buf->bh_first.b_next = NULL;
153 * Return the contents of a buffer as a single string.
154 * K_SPECIAL and CSI in the returned string are escaped.
156 static char_u *
157 get_buffcont(buffer, dozero)
158 struct buffheader *buffer;
159 int dozero; /* count == zero is not an error */
161 long_u count = 0;
162 char_u *p = NULL;
163 char_u *p2;
164 char_u *str;
165 struct buffblock *bp;
167 /* compute the total length of the string */
168 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
169 count += (long_u)STRLEN(bp->b_str);
171 if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL)
173 p2 = p;
174 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
175 for (str = bp->b_str; *str; )
176 *p2++ = *str++;
177 *p2 = NUL;
179 return (p);
183 * Return the contents of the record buffer as a single string
184 * and clear the record buffer.
185 * K_SPECIAL and CSI in the returned string are escaped.
187 char_u *
188 get_recorded()
190 char_u *p;
191 size_t len;
193 p = get_buffcont(&recordbuff, TRUE);
194 free_buff(&recordbuff);
197 * Remove the characters that were added the last time, these must be the
198 * (possibly mapped) characters that stopped the recording.
200 len = STRLEN(p);
201 if ((int)len >= last_recorded_len)
203 len -= last_recorded_len;
204 p[len] = NUL;
208 * When stopping recording from Insert mode with CTRL-O q, also remove the
209 * CTRL-O.
211 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
212 p[len - 1] = NUL;
214 return (p);
218 * Return the contents of the redo buffer as a single string.
219 * K_SPECIAL and CSI in the returned string are escaped.
221 char_u *
222 get_inserted()
224 return(get_buffcont(&redobuff, FALSE));
228 * Add string "s" after the current block of buffer "buf".
229 * K_SPECIAL and CSI should have been escaped already.
231 static void
232 add_buff(buf, s, slen)
233 struct buffheader *buf;
234 char_u *s;
235 long slen; /* length of "s" or -1 */
237 struct buffblock *p;
238 long_u len;
240 if (slen < 0)
241 slen = (long)STRLEN(s);
242 if (slen == 0) /* don't add empty strings */
243 return;
245 if (buf->bh_first.b_next == NULL) /* first add to list */
247 buf->bh_space = 0;
248 buf->bh_curr = &(buf->bh_first);
250 else if (buf->bh_curr == NULL) /* buffer has already been read */
252 EMSG(_("E222: Add to read buffer"));
253 return;
255 else if (buf->bh_index != 0)
256 STRCPY(buf->bh_first.b_next->b_str,
257 buf->bh_first.b_next->b_str + buf->bh_index);
258 buf->bh_index = 0;
260 if (buf->bh_space >= (int)slen)
262 len = (long_u)STRLEN(buf->bh_curr->b_str);
263 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
264 buf->bh_space -= slen;
266 else
268 if (slen < MINIMAL_SIZE)
269 len = MINIMAL_SIZE;
270 else
271 len = slen;
272 p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len),
273 TRUE);
274 if (p == NULL)
275 return; /* no space, just forget it */
276 buf->bh_space = (int)(len - slen);
277 vim_strncpy(p->b_str, s, (size_t)slen);
279 p->b_next = buf->bh_curr->b_next;
280 buf->bh_curr->b_next = p;
281 buf->bh_curr = p;
283 return;
287 * Add number "n" to buffer "buf".
289 static void
290 add_num_buff(buf, n)
291 struct buffheader *buf;
292 long n;
294 char_u number[32];
296 sprintf((char *)number, "%ld", n);
297 add_buff(buf, number, -1L);
301 * Add character 'c' to buffer "buf".
302 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
304 static void
305 add_char_buff(buf, c)
306 struct buffheader *buf;
307 int c;
309 #ifdef FEAT_MBYTE
310 char_u bytes[MB_MAXBYTES + 1];
311 int len;
312 int i;
313 #endif
314 char_u temp[4];
316 #ifdef FEAT_MBYTE
317 if (IS_SPECIAL(c))
318 len = 1;
319 else
320 len = (*mb_char2bytes)(c, bytes);
321 for (i = 0; i < len; ++i)
323 if (!IS_SPECIAL(c))
324 c = bytes[i];
325 #endif
327 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
329 /* translate special key code into three byte sequence */
330 temp[0] = K_SPECIAL;
331 temp[1] = K_SECOND(c);
332 temp[2] = K_THIRD(c);
333 temp[3] = NUL;
335 #ifdef FEAT_GUI
336 else if (c == CSI)
338 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
339 temp[0] = CSI;
340 temp[1] = KS_EXTRA;
341 temp[2] = (int)KE_CSI;
342 temp[3] = NUL;
344 #endif
345 else
347 temp[0] = c;
348 temp[1] = NUL;
350 add_buff(buf, temp, -1L);
351 #ifdef FEAT_MBYTE
353 #endif
357 * Get one byte from the stuff buffer.
358 * If advance == TRUE go to the next char.
359 * No translation is done K_SPECIAL and CSI are escaped.
361 static int
362 read_stuff(advance)
363 int advance;
365 char_u c;
366 struct buffblock *curr;
368 if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */
369 return NUL;
371 curr = stuffbuff.bh_first.b_next;
372 c = curr->b_str[stuffbuff.bh_index];
374 if (advance)
376 if (curr->b_str[++stuffbuff.bh_index] == NUL)
378 stuffbuff.bh_first.b_next = curr->b_next;
379 vim_free(curr);
380 stuffbuff.bh_index = 0;
383 return c;
387 * Prepare the stuff buffer for reading (if it contains something).
389 static void
390 start_stuff()
392 if (stuffbuff.bh_first.b_next != NULL)
394 stuffbuff.bh_curr = &(stuffbuff.bh_first);
395 stuffbuff.bh_space = 0;
400 * Return TRUE if the stuff buffer is empty.
403 stuff_empty()
405 return (stuffbuff.bh_first.b_next == NULL);
409 * Set a typeahead character that won't be flushed.
411 void
412 typeahead_noflush(c)
413 int c;
415 typeahead_char = c;
419 * Remove the contents of the stuff buffer and the mapped characters in the
420 * typeahead buffer (used in case of an error). If 'typeahead' is true,
421 * flush all typeahead characters (used when interrupted by a CTRL-C).
423 void
424 flush_buffers(typeahead)
425 int typeahead;
427 init_typebuf();
429 start_stuff();
430 while (read_stuff(TRUE) != NUL)
433 if (typeahead) /* remove all typeahead */
436 * We have to get all characters, because we may delete the first part
437 * of an escape sequence.
438 * In an xterm we get one char at a time and we have to get them all.
440 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L,
441 typebuf.tb_change_cnt) != 0)
443 typebuf.tb_off = MAXMAPLEN;
444 typebuf.tb_len = 0;
446 else /* remove mapped characters only */
448 typebuf.tb_off += typebuf.tb_maplen;
449 typebuf.tb_len -= typebuf.tb_maplen;
451 typebuf.tb_maplen = 0;
452 typebuf.tb_silent = 0;
453 cmd_silent = FALSE;
454 typebuf.tb_no_abbr_cnt = 0;
458 * The previous contents of the redo buffer is kept in old_redobuffer.
459 * This is used for the CTRL-O <.> command in insert mode.
461 void
462 ResetRedobuff()
464 if (!block_redo)
466 free_buff(&old_redobuff);
467 old_redobuff = redobuff;
468 redobuff.bh_first.b_next = NULL;
472 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
474 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
475 * Used before executing autocommands and user functions.
477 static int save_level = 0;
479 void
480 saveRedobuff()
482 char_u *s;
484 if (save_level++ == 0)
486 save_redobuff = redobuff;
487 redobuff.bh_first.b_next = NULL;
488 save_old_redobuff = old_redobuff;
489 old_redobuff.bh_first.b_next = NULL;
491 /* Make a copy, so that ":normal ." in a function works. */
492 s = get_buffcont(&save_redobuff, FALSE);
493 if (s != NULL)
495 add_buff(&redobuff, s, -1L);
496 vim_free(s);
502 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
503 * Used after executing autocommands and user functions.
505 void
506 restoreRedobuff()
508 if (--save_level == 0)
510 free_buff(&redobuff);
511 redobuff = save_redobuff;
512 free_buff(&old_redobuff);
513 old_redobuff = save_old_redobuff;
516 #endif
519 * Append "s" to the redo buffer.
520 * K_SPECIAL and CSI should already have been escaped.
522 void
523 AppendToRedobuff(s)
524 char_u *s;
526 if (!block_redo)
527 add_buff(&redobuff, s, -1L);
531 * Append to Redo buffer literally, escaping special characters with CTRL-V.
532 * K_SPECIAL and CSI are escaped as well.
534 void
535 AppendToRedobuffLit(str, len)
536 char_u *str;
537 int len; /* length of "str" or -1 for up to the NUL */
539 char_u *s = str;
540 int c;
541 char_u *start;
543 if (block_redo)
544 return;
546 while (len < 0 ? *s != NUL : s - str < len)
548 /* Put a string of normal characters in the redo buffer (that's
549 * faster). */
550 start = s;
551 while (*s >= ' '
552 #ifndef EBCDIC
553 && *s < DEL /* EBCDIC: all chars above space are normal */
554 #endif
555 && (len < 0 || s - str < len))
556 ++s;
558 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
559 * typed next. */
560 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
561 --s;
562 if (s > start)
563 add_buff(&redobuff, start, (long)(s - start));
565 if (*s == NUL || (len >= 0 && s - str >= len))
566 break;
568 /* Handle a special or multibyte character. */
569 #ifdef FEAT_MBYTE
570 if (has_mbyte)
571 /* Handle composing chars separately. */
572 c = mb_cptr2char_adv(&s);
573 else
574 #endif
575 c = *s++;
576 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
577 add_char_buff(&redobuff, Ctrl_V);
579 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
580 if (*s == NUL && c == '0')
581 #ifdef EBCDIC
582 add_buff(&redobuff, (char_u *)"xf0", 3L);
583 #else
584 add_buff(&redobuff, (char_u *)"048", 3L);
585 #endif
586 else
587 add_char_buff(&redobuff, c);
592 * Append a character to the redo buffer.
593 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
595 void
596 AppendCharToRedobuff(c)
597 int c;
599 if (!block_redo)
600 add_char_buff(&redobuff, c);
604 * Append a number to the redo buffer.
606 void
607 AppendNumberToRedobuff(n)
608 long n;
610 if (!block_redo)
611 add_num_buff(&redobuff, n);
615 * Append string "s" to the stuff buffer.
616 * CSI and K_SPECIAL must already have been escaped.
618 void
619 stuffReadbuff(s)
620 char_u *s;
622 add_buff(&stuffbuff, s, -1L);
625 void
626 stuffReadbuffLen(s, len)
627 char_u *s;
628 long len;
630 add_buff(&stuffbuff, s, len);
633 #if defined(FEAT_EVAL) || defined(PROTO)
635 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
636 * escaping other K_SPECIAL and CSI bytes.
638 void
639 stuffReadbuffSpec(s)
640 char_u *s;
642 while (*s != NUL)
644 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
646 /* Insert special key literally. */
647 stuffReadbuffLen(s, 3L);
648 s += 3;
650 else
651 #ifdef FEAT_MBYTE
652 stuffcharReadbuff(mb_ptr2char_adv(&s));
653 #else
654 stuffcharReadbuff(*s++);
655 #endif
658 #endif
661 * Append a character to the stuff buffer.
662 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
664 void
665 stuffcharReadbuff(c)
666 int c;
668 add_char_buff(&stuffbuff, c);
672 * Append a number to the stuff buffer.
674 void
675 stuffnumReadbuff(n)
676 long n;
678 add_num_buff(&stuffbuff, n);
682 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
683 * multibyte characters.
684 * The redo buffer is left as it is.
685 * if init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
686 * otherwise
687 * if old is TRUE, use old_redobuff instead of redobuff
689 static int
690 read_redo(init, old_redo)
691 int init;
692 int old_redo;
694 static struct buffblock *bp;
695 static char_u *p;
696 int c;
697 #ifdef FEAT_MBYTE
698 int n;
699 char_u buf[MB_MAXBYTES];
700 int i;
701 #endif
703 if (init)
705 if (old_redo)
706 bp = old_redobuff.bh_first.b_next;
707 else
708 bp = redobuff.bh_first.b_next;
709 if (bp == NULL)
710 return FAIL;
711 p = bp->b_str;
712 return OK;
714 if ((c = *p) != NUL)
716 /* Reverse the conversion done by add_char_buff() */
717 #ifdef FEAT_MBYTE
718 /* For a multi-byte character get all the bytes and return the
719 * converted character. */
720 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
721 n = MB_BYTE2LEN_CHECK(c);
722 else
723 n = 1;
724 for (i = 0; ; ++i)
725 #endif
727 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
729 c = TO_SPECIAL(p[1], p[2]);
730 p += 2;
732 #ifdef FEAT_GUI
733 if (c == CSI) /* escaped CSI */
734 p += 2;
735 #endif
736 if (*++p == NUL && bp->b_next != NULL)
738 bp = bp->b_next;
739 p = bp->b_str;
741 #ifdef FEAT_MBYTE
742 buf[i] = c;
743 if (i == n - 1) /* last byte of a character */
745 if (n != 1)
746 c = (*mb_ptr2char)(buf);
747 break;
749 c = *p;
750 if (c == NUL) /* cannot happen? */
751 break;
752 #endif
756 return c;
760 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
761 * If old_redo is TRUE, use old_redobuff instead of redobuff.
762 * The escaped K_SPECIAL and CSI are copied without translation.
764 static void
765 copy_redo(old_redo)
766 int old_redo;
768 int c;
770 while ((c = read_redo(FALSE, old_redo)) != NUL)
771 stuffcharReadbuff(c);
775 * Stuff the redo buffer into the stuffbuff.
776 * Insert the redo count into the command.
777 * If "old_redo" is TRUE, the last but one command is repeated
778 * instead of the last command (inserting text). This is used for
779 * CTRL-O <.> in insert mode
781 * return FAIL for failure, OK otherwise
784 start_redo(count, old_redo)
785 long count;
786 int old_redo;
788 int c;
790 /* init the pointers; return if nothing to redo */
791 if (read_redo(TRUE, old_redo) == FAIL)
792 return FAIL;
794 c = read_redo(FALSE, old_redo);
796 /* copy the buffer name, if present */
797 if (c == '"')
799 add_buff(&stuffbuff, (char_u *)"\"", 1L);
800 c = read_redo(FALSE, old_redo);
802 /* if a numbered buffer is used, increment the number */
803 if (c >= '1' && c < '9')
804 ++c;
805 add_char_buff(&stuffbuff, c);
806 c = read_redo(FALSE, old_redo);
809 #ifdef FEAT_VISUAL
810 if (c == 'v') /* redo Visual */
812 VIsual = curwin->w_cursor;
813 VIsual_active = TRUE;
814 VIsual_select = FALSE;
815 VIsual_reselect = TRUE;
816 redo_VIsual_busy = TRUE;
817 c = read_redo(FALSE, old_redo);
819 #endif
821 /* try to enter the count (in place of a previous count) */
822 if (count)
824 while (VIM_ISDIGIT(c)) /* skip "old" count */
825 c = read_redo(FALSE, old_redo);
826 add_num_buff(&stuffbuff, count);
829 /* copy from the redo buffer into the stuff buffer */
830 add_char_buff(&stuffbuff, c);
831 copy_redo(old_redo);
832 return OK;
836 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
837 * the redo buffer into the stuffbuff.
838 * return FAIL for failure, OK otherwise
841 start_redo_ins()
843 int c;
845 if (read_redo(TRUE, FALSE) == FAIL)
846 return FAIL;
847 start_stuff();
849 /* skip the count and the command character */
850 while ((c = read_redo(FALSE, FALSE)) != NUL)
852 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
854 if (c == 'O' || c == 'o')
855 stuffReadbuff(NL_STR);
856 break;
860 /* copy the typed text from the redo buffer into the stuff buffer */
861 copy_redo(FALSE);
862 block_redo = TRUE;
863 return OK;
866 void
867 stop_redo_ins()
869 block_redo = FALSE;
873 * Initialize typebuf.tb_buf to point to typebuf_init.
874 * alloc() cannot be used here: In out-of-memory situations it would
875 * be impossible to type anything.
877 static void
878 init_typebuf()
880 if (typebuf.tb_buf == NULL)
882 typebuf.tb_buf = typebuf_init;
883 typebuf.tb_noremap = noremapbuf_init;
884 typebuf.tb_buflen = TYPELEN_INIT;
885 typebuf.tb_len = 0;
886 typebuf.tb_off = 0;
887 typebuf.tb_change_cnt = 1;
892 * insert a string in position 'offset' in the typeahead buffer (for "@r"
893 * and ":normal" command, vgetorpeek() and check_termcode())
895 * If noremap is REMAP_YES, new string can be mapped again.
896 * If noremap is REMAP_NONE, new string cannot be mapped again.
897 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
898 * but abbreviations are allowed.
899 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
900 * script-local mappings.
901 * If noremap is > 0, that many characters of the new string cannot be mapped.
903 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
904 * offset is non-zero!).
906 * If silent is TRUE, cmd_silent is set when the characters are obtained.
908 * return FAIL for failure, OK otherwise
911 ins_typebuf(str, noremap, offset, nottyped, silent)
912 char_u *str;
913 int noremap;
914 int offset;
915 int nottyped;
916 int silent;
918 char_u *s1, *s2;
919 int newlen;
920 int addlen;
921 int i;
922 int newoff;
923 int val;
924 int nrm;
926 init_typebuf();
927 if (++typebuf.tb_change_cnt == 0)
928 typebuf.tb_change_cnt = 1;
930 addlen = (int)STRLEN(str);
933 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
935 if (offset == 0 && addlen <= typebuf.tb_off)
937 typebuf.tb_off -= addlen;
938 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
942 * Need to allocate a new buffer.
943 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
944 * characters. We add some extra room to avoid having to allocate too
945 * often.
947 else
949 newoff = MAXMAPLEN + 4;
950 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
951 if (newlen < 0) /* string is getting too long */
953 EMSG(_(e_toocompl)); /* also calls flush_buffers */
954 setcursor();
955 return FAIL;
957 s1 = alloc(newlen);
958 if (s1 == NULL) /* out of memory */
959 return FAIL;
960 s2 = alloc(newlen);
961 if (s2 == NULL) /* out of memory */
963 vim_free(s1);
964 return FAIL;
966 typebuf.tb_buflen = newlen;
968 /* copy the old chars, before the insertion point */
969 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
970 (size_t)offset);
971 /* copy the new chars */
972 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
973 /* copy the old chars, after the insertion point, including the NUL at
974 * the end */
975 mch_memmove(s1 + newoff + offset + addlen,
976 typebuf.tb_buf + typebuf.tb_off + offset,
977 (size_t)(typebuf.tb_len - offset + 1));
978 if (typebuf.tb_buf != typebuf_init)
979 vim_free(typebuf.tb_buf);
980 typebuf.tb_buf = s1;
982 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
983 (size_t)offset);
984 mch_memmove(s2 + newoff + offset + addlen,
985 typebuf.tb_noremap + typebuf.tb_off + offset,
986 (size_t)(typebuf.tb_len - offset));
987 if (typebuf.tb_noremap != noremapbuf_init)
988 vim_free(typebuf.tb_noremap);
989 typebuf.tb_noremap = s2;
991 typebuf.tb_off = newoff;
993 typebuf.tb_len += addlen;
995 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
996 if (noremap == REMAP_SCRIPT)
997 val = RM_SCRIPT;
998 else if (noremap == REMAP_SKIP)
999 val = RM_ABBR;
1000 else
1001 val = RM_NONE;
1004 * Adjust typebuf.tb_noremap[] for the new characters:
1005 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1006 * (sometimes) not remappable
1007 * If noremap == REMAP_YES: all the new characters are mappable
1008 * If noremap > 0: "noremap" characters are not remappable, the rest
1009 * mappable
1011 if (noremap == REMAP_SKIP)
1012 nrm = 1;
1013 else if (noremap < 0)
1014 nrm = addlen;
1015 else
1016 nrm = noremap;
1017 for (i = 0; i < addlen; ++i)
1018 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1019 (--nrm >= 0) ? val : RM_YES;
1021 /* tb_maplen and tb_silent only remember the length of mapped and/or
1022 * silent mappings at the start of the buffer, assuming that a mapped
1023 * sequence doesn't result in typed characters. */
1024 if (nottyped || typebuf.tb_maplen > offset)
1025 typebuf.tb_maplen += addlen;
1026 if (silent || typebuf.tb_silent > offset)
1028 typebuf.tb_silent += addlen;
1029 cmd_silent = TRUE;
1031 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1032 typebuf.tb_no_abbr_cnt += addlen;
1034 return OK;
1038 * Put character "c" back into the typeahead buffer.
1039 * Can be used for a character obtained by vgetc() that needs to be put back.
1040 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1041 * the char.
1043 void
1044 ins_char_typebuf(c)
1045 int c;
1047 #ifdef FEAT_MBYTE
1048 char_u buf[MB_MAXBYTES];
1049 #else
1050 char_u buf[4];
1051 #endif
1052 if (IS_SPECIAL(c))
1054 buf[0] = K_SPECIAL;
1055 buf[1] = K_SECOND(c);
1056 buf[2] = K_THIRD(c);
1057 buf[3] = NUL;
1059 else
1061 #ifdef FEAT_MBYTE
1062 buf[(*mb_char2bytes)(c, buf)] = NUL;
1063 #else
1064 buf[0] = c;
1065 buf[1] = NUL;
1066 #endif
1068 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
1072 * Return TRUE if the typeahead buffer was changed (while waiting for a
1073 * character to arrive). Happens when a message was received from a client or
1074 * from feedkeys().
1075 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1076 * changed it was reallocated and the old pointer can no longer be used.
1077 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1078 * that was just added.
1081 typebuf_changed(tb_change_cnt)
1082 int tb_change_cnt; /* old value of typebuf.tb_change_cnt */
1084 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
1085 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1086 || typebuf_was_filled
1087 #endif
1092 * Return TRUE if there are no characters in the typeahead buffer that have
1093 * not been typed (result from a mapping or come from ":normal").
1096 typebuf_typed()
1098 return typebuf.tb_maplen == 0;
1101 #if defined(FEAT_VISUAL) || defined(PROTO)
1103 * Return the number of characters that are mapped (or not typed).
1106 typebuf_maplen()
1108 return typebuf.tb_maplen;
1110 #endif
1113 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1115 void
1116 del_typebuf(len, offset)
1117 int len;
1118 int offset;
1120 int i;
1122 if (len == 0)
1123 return; /* nothing to do */
1125 typebuf.tb_len -= len;
1128 * Easy case: Just increase typebuf.tb_off.
1130 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1131 >= 3 * MAXMAPLEN + 3)
1132 typebuf.tb_off += len;
1134 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1136 else
1138 i = typebuf.tb_off + offset;
1140 * Leave some extra room at the end to avoid reallocation.
1142 if (typebuf.tb_off > MAXMAPLEN)
1144 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1145 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1146 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1147 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1148 typebuf.tb_off = MAXMAPLEN;
1150 /* adjust typebuf.tb_buf (include the NUL at the end) */
1151 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1152 typebuf.tb_buf + i + len,
1153 (size_t)(typebuf.tb_len - offset + 1));
1154 /* adjust typebuf.tb_noremap[] */
1155 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1156 typebuf.tb_noremap + i + len,
1157 (size_t)(typebuf.tb_len - offset));
1160 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1162 if (typebuf.tb_maplen < offset + len)
1163 typebuf.tb_maplen = offset;
1164 else
1165 typebuf.tb_maplen -= len;
1167 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1169 if (typebuf.tb_silent < offset + len)
1170 typebuf.tb_silent = offset;
1171 else
1172 typebuf.tb_silent -= len;
1174 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1176 if (typebuf.tb_no_abbr_cnt < offset + len)
1177 typebuf.tb_no_abbr_cnt = offset;
1178 else
1179 typebuf.tb_no_abbr_cnt -= len;
1182 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1183 /* Reset the flag that text received from a client or from feedkeys()
1184 * was inserted in the typeahead buffer. */
1185 typebuf_was_filled = FALSE;
1186 #endif
1187 if (++typebuf.tb_change_cnt == 0)
1188 typebuf.tb_change_cnt = 1;
1192 * Write typed characters to script file.
1193 * If recording is on put the character in the recordbuffer.
1195 static void
1196 gotchars(chars, len)
1197 char_u *chars;
1198 int len;
1200 char_u *s = chars;
1201 int c;
1202 char_u buf[2];
1203 int todo = len;
1205 /* remember how many chars were last recorded */
1206 if (Recording)
1207 last_recorded_len += len;
1209 buf[1] = NUL;
1210 while (todo--)
1212 /* Handle one byte at a time; no translation to be done. */
1213 c = *s++;
1214 updatescript(c);
1216 if (Recording)
1218 buf[0] = c;
1219 add_buff(&recordbuff, buf, 1L);
1222 may_sync_undo();
1224 #ifdef FEAT_EVAL
1225 /* output "debug mode" message next time in debug mode */
1226 debug_did_msg = FALSE;
1227 #endif
1229 /* Since characters have been typed, consider the following to be in
1230 * another mapping. Search string will be kept in history. */
1231 ++maptick;
1235 * Sync undo. Called when typed characters are obtained from the typeahead
1236 * buffer, or when a menu is used.
1237 * Do not sync:
1238 * - In Insert mode, unless cursor key has been used.
1239 * - While reading a script file.
1240 * - When no_u_sync is non-zero.
1242 static void
1243 may_sync_undo()
1245 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
1246 && scriptin[curscript] == NULL)
1247 u_sync(FALSE);
1251 * Make "typebuf" empty and allocate new buffers.
1252 * Returns FAIL when out of memory.
1255 alloc_typebuf()
1257 typebuf.tb_buf = alloc(TYPELEN_INIT);
1258 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1259 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1261 free_typebuf();
1262 return FAIL;
1264 typebuf.tb_buflen = TYPELEN_INIT;
1265 typebuf.tb_off = 0;
1266 typebuf.tb_len = 0;
1267 typebuf.tb_maplen = 0;
1268 typebuf.tb_silent = 0;
1269 typebuf.tb_no_abbr_cnt = 0;
1270 if (++typebuf.tb_change_cnt == 0)
1271 typebuf.tb_change_cnt = 1;
1272 return OK;
1276 * Free the buffers of "typebuf".
1278 void
1279 free_typebuf()
1281 vim_free(typebuf.tb_buf);
1282 vim_free(typebuf.tb_noremap);
1286 * When doing ":so! file", the current typeahead needs to be saved, and
1287 * restored when "file" has been read completely.
1289 static typebuf_T saved_typebuf[NSCRIPT];
1292 save_typebuf()
1294 init_typebuf();
1295 saved_typebuf[curscript] = typebuf;
1296 /* If out of memory: restore typebuf and close file. */
1297 if (alloc_typebuf() == FAIL)
1299 closescript();
1300 return FAIL;
1302 return OK;
1305 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1308 * Save all three kinds of typeahead, so that the user must type at a prompt.
1310 void
1311 save_typeahead(tp)
1312 tasave_T *tp;
1314 tp->save_typebuf = typebuf;
1315 tp->typebuf_valid = (alloc_typebuf() == OK);
1316 if (!tp->typebuf_valid)
1317 typebuf = tp->save_typebuf;
1319 tp->save_stuffbuff = stuffbuff;
1320 stuffbuff.bh_first.b_next = NULL;
1321 # ifdef USE_INPUT_BUF
1322 tp->save_inputbuf = get_input_buf();
1323 # endif
1327 * Restore the typeahead to what it was before calling save_typeahead().
1328 * The allocated memory is freed, can only be called once!
1330 void
1331 restore_typeahead(tp)
1332 tasave_T *tp;
1334 if (tp->typebuf_valid)
1336 free_typebuf();
1337 typebuf = tp->save_typebuf;
1340 free_buff(&stuffbuff);
1341 stuffbuff = tp->save_stuffbuff;
1342 # ifdef USE_INPUT_BUF
1343 set_input_buf(tp->save_inputbuf);
1344 # endif
1346 #endif
1349 * Open a new script file for the ":source!" command.
1351 void
1352 openscript(name, directly)
1353 char_u *name;
1354 int directly; /* when TRUE execute directly */
1356 if (curscript + 1 == NSCRIPT)
1358 EMSG(_(e_nesting));
1359 return;
1362 if (scriptin[curscript] != NULL) /* already reading script */
1363 ++curscript;
1364 /* use NameBuff for expanded name */
1365 expand_env(name, NameBuff, MAXPATHL);
1366 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1368 EMSG2(_(e_notopen), name);
1369 if (curscript)
1370 --curscript;
1371 return;
1373 if (save_typebuf() == FAIL)
1374 return;
1377 * Execute the commands from the file right now when using ":source!"
1378 * after ":global" or ":argdo" or in a loop. Also when another command
1379 * follows. This means the display won't be updated. Don't do this
1380 * always, "make test" would fail.
1382 if (directly)
1384 oparg_T oa;
1385 int oldcurscript;
1386 int save_State = State;
1387 int save_restart_edit = restart_edit;
1388 int save_insertmode = p_im;
1389 int save_finish_op = finish_op;
1390 int save_msg_scroll = msg_scroll;
1392 State = NORMAL;
1393 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1394 restart_edit = 0; /* don't go to Insert mode */
1395 p_im = FALSE; /* don't use 'insertmode' */
1396 clear_oparg(&oa);
1397 finish_op = FALSE;
1399 oldcurscript = curscript;
1402 update_topline_cursor(); /* update cursor position and topline */
1403 normal_cmd(&oa, FALSE); /* execute one command */
1404 vpeekc(); /* check for end of file */
1406 while (scriptin[oldcurscript] != NULL);
1408 State = save_State;
1409 msg_scroll = save_msg_scroll;
1410 restart_edit = save_restart_edit;
1411 p_im = save_insertmode;
1412 finish_op = save_finish_op;
1417 * Close the currently active input script.
1419 static void
1420 closescript()
1422 free_typebuf();
1423 typebuf = saved_typebuf[curscript];
1425 fclose(scriptin[curscript]);
1426 scriptin[curscript] = NULL;
1427 if (curscript > 0)
1428 --curscript;
1431 #if defined(EXITFREE) || defined(PROTO)
1432 void
1433 close_all_scripts()
1435 while (scriptin[0] != NULL)
1436 closescript();
1438 #endif
1440 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1442 * Return TRUE when reading keys from a script file.
1445 using_script()
1447 return scriptin[curscript] != NULL;
1449 #endif
1452 * This function is called just before doing a blocking wait. Thus after
1453 * waiting 'updatetime' for a character to arrive.
1455 void
1456 before_blocking()
1458 updatescript(0);
1459 #ifdef FEAT_EVAL
1460 if (may_garbage_collect)
1461 garbage_collect();
1462 #endif
1466 * updatescipt() is called when a character can be written into the script file
1467 * or when we have waited some time for a character (c == 0)
1469 * All the changed memfiles are synced if c == 0 or when the number of typed
1470 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1472 void
1473 updatescript(c)
1474 int c;
1476 static int count = 0;
1478 if (c && scriptout)
1479 putc(c, scriptout);
1480 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1482 ml_sync_all(c == 0, TRUE);
1483 count = 0;
1487 #define KL_PART_KEY -1 /* keylen value for incomplete key-code */
1488 #define KL_PART_MAP -2 /* keylen value for incomplete mapping */
1490 static int old_char = -1; /* character put back by vungetc() */
1491 static int old_mod_mask; /* mod_mask for ungotten character */
1494 * Get the next input character.
1495 * Can return a special key or a multi-byte character.
1496 * Can return NUL when called recursively, use safe_vgetc() if that's not
1497 * wanted.
1498 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1499 * Collects the bytes of a multibyte character into the whole character.
1500 * Returns the modifers in the global "mod_mask".
1503 vgetc()
1505 int c, c2;
1506 #ifdef FEAT_MBYTE
1507 int n;
1508 char_u buf[MB_MAXBYTES];
1509 int i;
1510 #endif
1512 #ifdef FEAT_EVAL
1513 /* Do garbage collection when garbagecollect() was called previously and
1514 * we are now at the toplevel. */
1515 if (may_garbage_collect && want_garbage_collect)
1516 garbage_collect();
1517 #endif
1520 * If a character was put back with vungetc, it was already processed.
1521 * Return it directly.
1523 if (old_char != -1)
1525 c = old_char;
1526 old_char = -1;
1527 mod_mask = old_mod_mask;
1529 else
1531 mod_mask = 0x0;
1532 last_recorded_len = 0;
1533 for (;;) /* this is done twice if there are modifiers */
1535 if (mod_mask) /* no mapping after modifier has been read */
1537 ++no_mapping;
1538 ++allow_keys;
1540 c = vgetorpeek(TRUE);
1541 if (mod_mask)
1543 --no_mapping;
1544 --allow_keys;
1547 /* Get two extra bytes for special keys */
1548 if (c == K_SPECIAL
1549 #ifdef FEAT_GUI
1550 || c == CSI
1551 #endif
1554 int save_allow_keys = allow_keys;
1556 ++no_mapping;
1557 allow_keys = 0; /* make sure BS is not found */
1558 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1559 c = vgetorpeek(TRUE);
1560 --no_mapping;
1561 allow_keys = save_allow_keys;
1562 if (c2 == KS_MODIFIER)
1564 mod_mask = c;
1565 continue;
1567 c = TO_SPECIAL(c2, c);
1569 #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1570 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1571 * know that a menu was torn off */
1572 if (c == K_TEAROFF)
1574 char_u name[200];
1575 int i;
1577 /* get menu path, it ends with a <CR> */
1578 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1580 name[i] = c;
1581 if (i < 199)
1582 ++i;
1584 name[i] = NUL;
1585 gui_make_tearoff(name);
1586 continue;
1588 #endif
1589 #if defined(FEAT_GUI) && defined(HAVE_GTK2) && defined(FEAT_MENU)
1590 /* GTK: <F10> normally selects the menu, but it's passed until
1591 * here to allow mapping it. Intercept and invoke the GTK
1592 * behavior if it's not mapped. */
1593 if (c == K_F10 && gui.menubar != NULL)
1595 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1596 continue;
1598 #endif
1599 #ifdef FEAT_GUI
1600 /* Handle focus event here, so that the caller doesn't need to
1601 * know about it. Return K_IGNORE so that we loop once (needed if
1602 * 'lazyredraw' is set). */
1603 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1605 ui_focus_change(c == K_FOCUSGAINED);
1606 c = K_IGNORE;
1609 /* Translate K_CSI to CSI. The special key is only used to avoid
1610 * it being recognized as the start of a special key. */
1611 if (c == K_CSI)
1612 c = CSI;
1613 #endif
1615 #ifdef MSDOS
1617 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1618 * Delete the 3 here.
1620 else if (c == K_NUL && vpeekc() == 3)
1621 (void)vgetorpeek(TRUE);
1622 #endif
1624 /* a keypad or special function key was not mapped, use it like
1625 * its ASCII equivalent */
1626 switch (c)
1628 case K_KPLUS: c = '+'; break;
1629 case K_KMINUS: c = '-'; break;
1630 case K_KDIVIDE: c = '/'; break;
1631 case K_KMULTIPLY: c = '*'; break;
1632 case K_KENTER: c = CAR; break;
1633 case K_KPOINT:
1634 #ifdef WIN32
1635 /* Can be either '.' or a ',', *
1636 * depending on the type of keypad. */
1637 c = MapVirtualKey(VK_DECIMAL, 2); break;
1638 #else
1639 c = '.'; break;
1640 #endif
1641 case K_K0: c = '0'; break;
1642 case K_K1: c = '1'; break;
1643 case K_K2: c = '2'; break;
1644 case K_K3: c = '3'; break;
1645 case K_K4: c = '4'; break;
1646 case K_K5: c = '5'; break;
1647 case K_K6: c = '6'; break;
1648 case K_K7: c = '7'; break;
1649 case K_K8: c = '8'; break;
1650 case K_K9: c = '9'; break;
1652 case K_XHOME:
1653 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1655 c = K_S_HOME;
1656 mod_mask = 0;
1658 else if (mod_mask == MOD_MASK_CTRL)
1660 c = K_C_HOME;
1661 mod_mask = 0;
1663 else
1664 c = K_HOME;
1665 break;
1666 case K_XEND:
1667 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1669 c = K_S_END;
1670 mod_mask = 0;
1672 else if (mod_mask == MOD_MASK_CTRL)
1674 c = K_C_END;
1675 mod_mask = 0;
1677 else
1678 c = K_END;
1679 break;
1681 case K_XUP: c = K_UP; break;
1682 case K_XDOWN: c = K_DOWN; break;
1683 case K_XLEFT: c = K_LEFT; break;
1684 case K_XRIGHT: c = K_RIGHT; break;
1687 #ifdef FEAT_MBYTE
1688 /* For a multi-byte character get all the bytes and return the
1689 * converted character.
1690 * Note: This will loop until enough bytes are received!
1692 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1694 ++no_mapping;
1695 buf[0] = c;
1696 for (i = 1; i < n; ++i)
1698 buf[i] = vgetorpeek(TRUE);
1699 if (buf[i] == K_SPECIAL
1700 #ifdef FEAT_GUI
1701 || buf[i] == CSI
1702 #endif
1705 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1706 * which represents a K_SPECIAL (0x80),
1707 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1708 * a CSI (0x9B),
1709 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1710 c = vgetorpeek(TRUE);
1711 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1712 buf[i] = CSI;
1715 --no_mapping;
1716 c = (*mb_ptr2char)(buf);
1718 #endif
1720 break;
1724 #ifdef FEAT_EVAL
1726 * In the main loop "may_garbage_collect" can be set to do garbage
1727 * collection in the first next vgetc(). It's disabled after that to
1728 * avoid internally used Lists and Dicts to be freed.
1730 may_garbage_collect = FALSE;
1731 #endif
1733 return c;
1737 * Like vgetc(), but never return a NUL when called recursively, get a key
1738 * directly from the user (ignoring typeahead).
1741 safe_vgetc()
1743 int c;
1745 c = vgetc();
1746 if (c == NUL)
1747 c = get_keystroke();
1748 return c;
1752 * Like safe_vgetc(), but loop to handle K_IGNORE.
1753 * Also ignore scrollbar events.
1756 plain_vgetc()
1758 int c;
1762 c = safe_vgetc();
1763 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1764 return c;
1768 * Check if a character is available, such that vgetc() will not block.
1769 * If the next character is a special character or multi-byte, the returned
1770 * character is not valid!.
1773 vpeekc()
1775 if (old_char != -1)
1776 return old_char;
1777 return vgetorpeek(FALSE);
1780 #if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1782 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1783 * codes.
1786 vpeekc_nomap()
1788 int c;
1790 ++no_mapping;
1791 ++allow_keys;
1792 c = vpeekc();
1793 --no_mapping;
1794 --allow_keys;
1795 return c;
1797 #endif
1799 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1801 * Check if any character is available, also half an escape sequence.
1802 * Trick: when no typeahead found, but there is something in the typeahead
1803 * buffer, it must be an ESC that is recognized as the start of a key code.
1806 vpeekc_any()
1808 int c;
1810 c = vpeekc();
1811 if (c == NUL && typebuf.tb_len > 0)
1812 c = ESC;
1813 return c;
1815 #endif
1818 * Call vpeekc() without causing anything to be mapped.
1819 * Return TRUE if a character is available, FALSE otherwise.
1822 char_avail()
1824 int retval;
1826 ++no_mapping;
1827 retval = vpeekc();
1828 --no_mapping;
1829 return (retval != NUL);
1832 void
1833 vungetc(c) /* unget one character (can only be done once!) */
1834 int c;
1836 old_char = c;
1837 old_mod_mask = mod_mask;
1841 * get a character:
1842 * 1. from the stuffbuffer
1843 * This is used for abbreviated commands like "D" -> "d$".
1844 * Also used to redo a command for ".".
1845 * 2. from the typeahead buffer
1846 * Stores text obtained previously but not used yet.
1847 * Also stores the result of mappings.
1848 * Also used for the ":normal" command.
1849 * 3. from the user
1850 * This may do a blocking wait if "advance" is TRUE.
1852 * if "advance" is TRUE (vgetc()):
1853 * really get the character.
1854 * KeyTyped is set to TRUE in the case the user typed the key.
1855 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1856 * if "advance" is FALSE (vpeekc()):
1857 * just look whether there is a character available.
1859 * When "no_mapping" is zero, checks for mappings in the current mode.
1860 * Only returns one byte (of a multi-byte character).
1861 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1863 static int
1864 vgetorpeek(advance)
1865 int advance;
1867 int c, c1;
1868 int keylen;
1869 char_u *s;
1870 mapblock_T *mp;
1871 #ifdef FEAT_LOCALMAP
1872 mapblock_T *mp2;
1873 #endif
1874 mapblock_T *mp_match;
1875 int mp_match_len = 0;
1876 int timedout = FALSE; /* waited for more than 1 second
1877 for mapping to complete */
1878 int mapdepth = 0; /* check for recursive mapping */
1879 int mode_deleted = FALSE; /* set when mode has been deleted */
1880 int local_State;
1881 int mlen;
1882 int max_mlen;
1883 int i;
1884 #ifdef FEAT_CMDL_INFO
1885 int new_wcol, new_wrow;
1886 #endif
1887 #ifdef FEAT_GUI
1888 # ifdef FEAT_MENU
1889 int idx;
1890 # endif
1891 int shape_changed = FALSE; /* adjusted cursor shape */
1892 #endif
1893 int n;
1894 #ifdef FEAT_LANGMAP
1895 int nolmaplen;
1896 #endif
1897 int old_wcol, old_wrow;
1898 int wait_tb_len;
1901 * This function doesn't work very well when called recursively. This may
1902 * happen though, because of:
1903 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1904 * there is a character available, which calls this function. In that
1905 * case we must return NUL, to indicate no character is available.
1906 * 2. A GUI callback function writes to the screen, causing a
1907 * wait_return().
1908 * Using ":normal" can also do this, but it saves the typeahead buffer,
1909 * thus it should be OK. But don't get a key from the user then.
1911 if (vgetc_busy > 0
1912 #ifdef FEAT_EX_EXTRA
1913 && ex_normal_busy == 0
1914 #endif
1916 return NUL;
1918 local_State = get_real_state();
1920 ++vgetc_busy;
1922 if (advance)
1923 KeyStuffed = FALSE;
1925 init_typebuf();
1926 start_stuff();
1927 if (advance && typebuf.tb_maplen == 0)
1928 Exec_reg = FALSE;
1932 * get a character: 1. from the stuffbuffer
1934 if (typeahead_char != 0)
1936 c = typeahead_char;
1937 if (advance)
1938 typeahead_char = 0;
1940 else
1941 c = read_stuff(advance);
1942 if (c != NUL && !got_int)
1944 if (advance)
1946 /* KeyTyped = FALSE; When the command that stuffed something
1947 * was typed, behave like the stuffed command was typed.
1948 * needed for CTRL-W CTRl-] to open a fold, for example. */
1949 KeyStuffed = TRUE;
1951 if (typebuf.tb_no_abbr_cnt == 0)
1952 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1954 else
1957 * Loop until we either find a matching mapped key, or we
1958 * are sure that it is not a mapped key.
1959 * If a mapped key sequence is found we go back to the start to
1960 * try re-mapping.
1962 for (;;)
1965 * ui_breakcheck() is slow, don't use it too often when
1966 * inside a mapping. But call it each time for typed
1967 * characters.
1969 if (typebuf.tb_maplen)
1970 line_breakcheck();
1971 else
1972 ui_breakcheck(); /* check for CTRL-C */
1973 keylen = 0;
1974 if (got_int)
1976 /* flush all input */
1977 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
1978 typebuf.tb_change_cnt);
1980 * If inchar() returns TRUE (script file was active) or we
1981 * are inside a mapping, get out of insert mode.
1982 * Otherwise we behave like having gotten a CTRL-C.
1983 * As a result typing CTRL-C in insert mode will
1984 * really insert a CTRL-C.
1986 if ((c || typebuf.tb_maplen)
1987 && (State & (INSERT + CMDLINE)))
1988 c = ESC;
1989 else
1990 c = Ctrl_C;
1991 flush_buffers(TRUE); /* flush all typeahead */
1993 if (advance)
1995 /* Also record this character, it might be needed to
1996 * get out of Insert mode. */
1997 *typebuf.tb_buf = c;
1998 gotchars(typebuf.tb_buf, 1);
2000 cmd_silent = FALSE;
2002 break;
2004 else if (typebuf.tb_len > 0)
2007 * Check for a mappable key sequence.
2008 * Walk through one maphash[] list until we find an
2009 * entry that matches.
2011 * Don't look for mappings if:
2012 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2013 * - maphash_valid not set: no mappings present.
2014 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2015 * - in insert or cmdline mode and 'paste' option set
2016 * - waiting for "hit return to continue" and CR or SPACE
2017 * typed
2018 * - waiting for a char with --more--
2019 * - in Ctrl-X mode, and we get a valid char for that mode
2021 mp = NULL;
2022 max_mlen = 0;
2023 c1 = typebuf.tb_buf[typebuf.tb_off];
2024 if (no_mapping == 0 && maphash_valid
2025 && (no_zero_mapping == 0 || c1 != '0')
2026 && (typebuf.tb_maplen == 0
2027 || (p_remap
2028 && (typebuf.tb_noremap[typebuf.tb_off]
2029 & (RM_NONE|RM_ABBR)) == 0))
2030 && !(p_paste && (State & (INSERT + CMDLINE)))
2031 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2032 && State != ASKMORE
2033 && State != CONFIRM
2034 #ifdef FEAT_INS_EXPAND
2035 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
2036 || ((compl_cont_status & CONT_LOCAL)
2037 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2038 #endif
2041 #ifdef FEAT_LANGMAP
2042 if (c1 == K_SPECIAL)
2043 nolmaplen = 2;
2044 else
2046 LANGMAP_ADJUST(c1, TRUE);
2047 nolmaplen = 0;
2049 #endif
2050 #ifdef FEAT_LOCALMAP
2051 /* First try buffer-local mappings. */
2052 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2053 mp2 = maphash[MAP_HASH(local_State, c1)];
2054 if (mp == NULL)
2056 mp = mp2;
2057 mp2 = NULL;
2059 #else
2060 mp = maphash[MAP_HASH(local_State, c1)];
2061 #endif
2063 * Loop until a partly matching mapping is found or
2064 * all (local) mappings have been checked.
2065 * The longest full match is remembered in "mp_match".
2066 * A full match is only accepted if there is no partly
2067 * match, so "aa" and "aaa" can both be mapped.
2069 mp_match = NULL;
2070 mp_match_len = 0;
2071 for ( ; mp != NULL;
2072 #ifdef FEAT_LOCALMAP
2073 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2074 #endif
2075 (mp = mp->m_next))
2078 * Only consider an entry if the first character
2079 * matches and it is for the current state.
2080 * Skip ":lmap" mappings if keys were mapped.
2082 if (mp->m_keys[0] == c1
2083 && (mp->m_mode & local_State)
2084 && ((mp->m_mode & LANGMAP) == 0
2085 || typebuf.tb_maplen == 0))
2087 #ifdef FEAT_LANGMAP
2088 int nomap = nolmaplen;
2089 int c2;
2090 #endif
2091 /* find the match length of this mapping */
2092 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2094 #ifdef FEAT_LANGMAP
2095 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2096 if (nomap > 0)
2097 --nomap;
2098 else if (c2 == K_SPECIAL)
2099 nomap = 2;
2100 else
2101 LANGMAP_ADJUST(c2, TRUE);
2102 if (mp->m_keys[mlen] != c2)
2103 #else
2104 if (mp->m_keys[mlen] !=
2105 typebuf.tb_buf[typebuf.tb_off + mlen])
2106 #endif
2107 break;
2110 #ifdef FEAT_MBYTE
2111 /* Don't allow mapping the first byte(s) of a
2112 * multi-byte char. Happens when mapping
2113 * <M-a> and then changing 'encoding'. */
2114 if (has_mbyte && MB_BYTE2LEN(c1)
2115 > (*mb_ptr2len)(mp->m_keys))
2116 mlen = 0;
2117 #endif
2119 * Check an entry whether it matches.
2120 * - Full match: mlen == keylen
2121 * - Partly match: mlen == typebuf.tb_len
2123 keylen = mp->m_keylen;
2124 if (mlen == keylen
2125 || (mlen == typebuf.tb_len
2126 && typebuf.tb_len < keylen))
2129 * If only script-local mappings are
2130 * allowed, check if the mapping starts
2131 * with K_SNR.
2133 s = typebuf.tb_noremap + typebuf.tb_off;
2134 if (*s == RM_SCRIPT
2135 && (mp->m_keys[0] != K_SPECIAL
2136 || mp->m_keys[1] != KS_EXTRA
2137 || mp->m_keys[2]
2138 != (int)KE_SNR))
2139 continue;
2141 * If one of the typed keys cannot be
2142 * remapped, skip the entry.
2144 for (n = mlen; --n >= 0; )
2145 if (*s++ & (RM_NONE|RM_ABBR))
2146 break;
2147 if (n >= 0)
2148 continue;
2150 if (keylen > typebuf.tb_len)
2152 if (!timedout)
2154 /* break at a partly match */
2155 keylen = KL_PART_MAP;
2156 break;
2159 else if (keylen > mp_match_len)
2161 /* found a longer match */
2162 mp_match = mp;
2163 mp_match_len = keylen;
2166 else
2167 /* No match; may have to check for
2168 * termcode at next character. */
2169 if (max_mlen < mlen)
2170 max_mlen = mlen;
2174 /* If no partly match found, use the longest full
2175 * match. */
2176 if (keylen != KL_PART_MAP)
2178 mp = mp_match;
2179 keylen = mp_match_len;
2183 /* Check for match with 'pastetoggle' */
2184 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2186 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2187 ++mlen)
2188 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2189 + mlen])
2190 break;
2191 if (p_pt[mlen] == NUL) /* match */
2193 /* write chars to script file(s) */
2194 if (mlen > typebuf.tb_maplen)
2195 gotchars(typebuf.tb_buf + typebuf.tb_off
2196 + typebuf.tb_maplen,
2197 mlen - typebuf.tb_maplen);
2199 del_typebuf(mlen, 0); /* remove the chars */
2200 set_option_value((char_u *)"paste",
2201 (long)!p_paste, NULL, 0);
2202 if (!(State & INSERT))
2204 msg_col = 0;
2205 msg_row = Rows - 1;
2206 msg_clr_eos(); /* clear ruler */
2208 showmode();
2209 setcursor();
2210 continue;
2212 /* Need more chars for partly match. */
2213 if (mlen == typebuf.tb_len)
2214 keylen = KL_PART_KEY;
2215 else if (max_mlen < mlen)
2216 /* no match, may have to check for termcode at
2217 * next character */
2218 max_mlen = mlen + 1;
2221 if ((mp == NULL || max_mlen >= mp_match_len)
2222 && keylen != KL_PART_MAP)
2225 * When no matching mapping found or found a
2226 * non-matching mapping that matches at least what the
2227 * matching mapping matched:
2228 * Check if we have a terminal code, when:
2229 * mapping is allowed,
2230 * keys have not been mapped,
2231 * and not an ESC sequence, not in insert mode or
2232 * p_ek is on,
2233 * and when not timed out,
2235 if ((no_mapping == 0 || allow_keys != 0)
2236 && (typebuf.tb_maplen == 0
2237 || (p_remap && typebuf.tb_noremap[
2238 typebuf.tb_off] == RM_YES))
2239 && !timedout)
2241 keylen = check_termcode(max_mlen + 1, NULL, 0);
2244 * When getting a partial match, but the last
2245 * characters were not typed, don't wait for a
2246 * typed character to complete the termcode.
2247 * This helps a lot when a ":normal" command ends
2248 * in an ESC.
2250 if (keylen < 0
2251 && typebuf.tb_len == typebuf.tb_maplen)
2252 keylen = 0;
2254 else
2255 keylen = 0;
2256 if (keylen == 0) /* no matching terminal code */
2258 #ifdef AMIGA /* check for window bounds report */
2259 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2260 typebuf.tb_off] & 0xff) == CSI)
2262 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2263 s < typebuf.tb_buf + typebuf.tb_off
2264 + typebuf.tb_len
2265 && (VIM_ISDIGIT(*s) || *s == ';'
2266 || *s == ' ');
2267 ++s)
2269 if (*s == 'r' || *s == '|') /* found one */
2271 del_typebuf((int)(s + 1 -
2272 (typebuf.tb_buf + typebuf.tb_off)), 0);
2273 /* get size and redraw screen */
2274 shell_resized();
2275 continue;
2277 if (*s == NUL) /* need more characters */
2278 keylen = KL_PART_KEY;
2280 if (keylen >= 0)
2281 #endif
2282 /* When there was a matching mapping and no
2283 * termcode could be replaced after another one,
2284 * use that mapping. */
2285 if (mp == NULL)
2288 * get a character: 2. from the typeahead buffer
2290 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2291 if (advance) /* remove chars from tb_buf */
2293 cmd_silent = (typebuf.tb_silent > 0);
2294 if (typebuf.tb_maplen > 0)
2295 KeyTyped = FALSE;
2296 else
2298 KeyTyped = TRUE;
2299 /* write char to script file(s) */
2300 gotchars(typebuf.tb_buf
2301 + typebuf.tb_off, 1);
2303 KeyNoremap = typebuf.tb_noremap[
2304 typebuf.tb_off];
2305 del_typebuf(1, 0);
2307 break; /* got character, break for loop */
2310 if (keylen > 0) /* full matching terminal code */
2312 #if defined(FEAT_GUI) && defined(FEAT_MENU)
2313 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2314 && typebuf.tb_buf[typebuf.tb_off + 1]
2315 == KS_MENU)
2318 * Using a menu may cause a break in undo!
2319 * It's like using gotchars(), but without
2320 * recording or writing to a script file.
2322 may_sync_undo();
2323 del_typebuf(3, 0);
2324 idx = get_menu_index(current_menu, local_State);
2325 if (idx != MENU_INDEX_INVALID)
2327 # ifdef FEAT_VISUAL
2329 * In Select mode and a Visual mode menu
2330 * is used: Switch to Visual mode
2331 * temporarily. Append K_SELECT to switch
2332 * back to Select mode.
2334 if (VIsual_active && VIsual_select
2335 && (current_menu->modes & VISUAL))
2337 VIsual_select = FALSE;
2338 (void)ins_typebuf(K_SELECT_STRING,
2339 REMAP_NONE, 0, TRUE, FALSE);
2341 # endif
2342 ins_typebuf(current_menu->strings[idx],
2343 current_menu->noremap[idx],
2344 0, TRUE,
2345 current_menu->silent[idx]);
2348 #endif /* FEAT_GUI */
2349 continue; /* try mapping again */
2352 /* Partial match: get some more characters. When a
2353 * matching mapping was found use that one. */
2354 if (mp == NULL || keylen < 0)
2355 keylen = KL_PART_KEY;
2356 else
2357 keylen = mp_match_len;
2360 /* complete match */
2361 if (keylen >= 0 && keylen <= typebuf.tb_len)
2363 /* write chars to script file(s) */
2364 if (keylen > typebuf.tb_maplen)
2365 gotchars(typebuf.tb_buf + typebuf.tb_off
2366 + typebuf.tb_maplen,
2367 keylen - typebuf.tb_maplen);
2369 cmd_silent = (typebuf.tb_silent > 0);
2370 del_typebuf(keylen, 0); /* remove the mapped keys */
2373 * Put the replacement string in front of mapstr.
2374 * The depth check catches ":map x y" and ":map y x".
2376 if (++mapdepth >= p_mmd)
2378 EMSG(_("E223: recursive mapping"));
2379 if (State & CMDLINE)
2380 redrawcmdline();
2381 else
2382 setcursor();
2383 flush_buffers(FALSE);
2384 mapdepth = 0; /* for next one */
2385 c = -1;
2386 break;
2389 #ifdef FEAT_VISUAL
2391 * In Select mode and a Visual mode mapping is used:
2392 * Switch to Visual mode temporarily. Append K_SELECT
2393 * to switch back to Select mode.
2395 if (VIsual_active && VIsual_select
2396 && (mp->m_mode & VISUAL))
2398 VIsual_select = FALSE;
2399 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2400 0, TRUE, FALSE);
2402 #endif
2404 #ifdef FEAT_EVAL
2406 * Handle ":map <expr>": evaluate the {rhs} as an
2407 * expression. Save and restore the typeahead so that
2408 * getchar() can be used. Also save and restore the
2409 * command line for "normal :".
2411 if (mp->m_expr)
2413 tasave_T tabuf;
2414 int save_vgetc_busy = vgetc_busy;
2416 save_typeahead(&tabuf);
2417 if (tabuf.typebuf_valid)
2419 vgetc_busy = 0;
2420 s = eval_map_expr(mp->m_str);
2421 vgetc_busy = save_vgetc_busy;
2423 else
2424 s = NULL;
2425 restore_typeahead(&tabuf);
2427 else
2428 #endif
2429 s = mp->m_str;
2432 * Insert the 'to' part in the typebuf.tb_buf.
2433 * If 'from' field is the same as the start of the
2434 * 'to' field, don't remap the first character (but do
2435 * allow abbreviations).
2436 * If m_noremap is set, don't remap the whole 'to'
2437 * part.
2439 if (s == NULL)
2440 i = FAIL;
2441 else
2443 i = ins_typebuf(s,
2444 mp->m_noremap != REMAP_YES
2445 ? mp->m_noremap
2446 : STRNCMP(s, mp->m_keys,
2447 (size_t)keylen) != 0
2448 ? REMAP_YES : REMAP_SKIP,
2449 0, TRUE, cmd_silent || mp->m_silent);
2450 #ifdef FEAT_EVAL
2451 if (mp->m_expr)
2452 vim_free(s);
2453 #endif
2455 if (i == FAIL)
2457 c = -1;
2458 break;
2460 continue;
2465 * get a character: 3. from the user - handle <Esc> in Insert mode
2468 * special case: if we get an <ESC> in insert mode and there
2469 * are no more characters at once, we pretend to go out of
2470 * insert mode. This prevents the one second delay after
2471 * typing an <ESC>. If we get something after all, we may
2472 * have to redisplay the mode. That the cursor is in the wrong
2473 * place does not matter.
2475 c = 0;
2476 #ifdef FEAT_CMDL_INFO
2477 new_wcol = curwin->w_wcol;
2478 new_wrow = curwin->w_wrow;
2479 #endif
2480 if ( advance
2481 && typebuf.tb_len == 1
2482 && typebuf.tb_buf[typebuf.tb_off] == ESC
2483 && !no_mapping
2484 #ifdef FEAT_EX_EXTRA
2485 && ex_normal_busy == 0
2486 #endif
2487 && typebuf.tb_maplen == 0
2488 && (State & INSERT)
2489 && (p_timeout || (keylen == KL_PART_KEY && p_ttimeout))
2490 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2491 + typebuf.tb_len, 3, 25L,
2492 typebuf.tb_change_cnt)) == 0)
2494 colnr_T col = 0, vcol;
2495 char_u *ptr;
2497 if (mode_displayed)
2499 unshowmode(TRUE);
2500 mode_deleted = TRUE;
2502 #ifdef FEAT_GUI
2503 /* may show different cursor shape */
2504 if (gui.in_use)
2506 int save_State;
2508 save_State = State;
2509 State = NORMAL;
2510 gui_update_cursor(TRUE, FALSE);
2511 State = save_State;
2512 shape_changed = TRUE;
2514 #endif
2515 validate_cursor();
2516 old_wcol = curwin->w_wcol;
2517 old_wrow = curwin->w_wrow;
2519 /* move cursor left, if possible */
2520 if (curwin->w_cursor.col != 0)
2522 if (curwin->w_wcol > 0)
2524 if (did_ai)
2527 * We are expecting to truncate the trailing
2528 * white-space, so find the last non-white
2529 * character -- webb
2531 col = vcol = curwin->w_wcol = 0;
2532 ptr = ml_get_curline();
2533 while (col < curwin->w_cursor.col)
2535 if (!vim_iswhite(ptr[col]))
2536 curwin->w_wcol = vcol;
2537 vcol += lbr_chartabsize(ptr + col,
2538 (colnr_T)vcol);
2539 #ifdef FEAT_MBYTE
2540 if (has_mbyte)
2541 col += (*mb_ptr2len)(ptr + col);
2542 else
2543 #endif
2544 ++col;
2546 curwin->w_wrow = curwin->w_cline_row
2547 + curwin->w_wcol / W_WIDTH(curwin);
2548 curwin->w_wcol %= W_WIDTH(curwin);
2549 curwin->w_wcol += curwin_col_off();
2550 #ifdef FEAT_MBYTE
2551 col = 0; /* no correction needed */
2552 #endif
2554 else
2556 --curwin->w_wcol;
2557 #ifdef FEAT_MBYTE
2558 col = curwin->w_cursor.col - 1;
2559 #endif
2562 else if (curwin->w_p_wrap && curwin->w_wrow)
2564 --curwin->w_wrow;
2565 curwin->w_wcol = W_WIDTH(curwin) - 1;
2566 #ifdef FEAT_MBYTE
2567 col = curwin->w_cursor.col - 1;
2568 #endif
2570 #ifdef FEAT_MBYTE
2571 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2573 /* Correct when the cursor is on the right halve
2574 * of a double-wide character. */
2575 ptr = ml_get_curline();
2576 col -= (*mb_head_off)(ptr, ptr + col);
2577 if ((*mb_ptr2cells)(ptr + col) > 1)
2578 --curwin->w_wcol;
2580 #endif
2582 setcursor();
2583 out_flush();
2584 #ifdef FEAT_CMDL_INFO
2585 new_wcol = curwin->w_wcol;
2586 new_wrow = curwin->w_wrow;
2587 #endif
2588 curwin->w_wcol = old_wcol;
2589 curwin->w_wrow = old_wrow;
2591 if (c < 0)
2592 continue; /* end of input script reached */
2593 typebuf.tb_len += c;
2595 /* buffer full, don't map */
2596 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2598 timedout = TRUE;
2599 continue;
2602 #ifdef FEAT_EX_EXTRA
2603 if (ex_normal_busy > 0)
2605 # ifdef FEAT_CMDWIN
2606 static int tc = 0;
2607 # endif
2609 /* No typeahead left and inside ":normal". Must return
2610 * something to avoid getting stuck. When an incomplete
2611 * mapping is present, behave like it timed out. */
2612 if (typebuf.tb_len > 0)
2614 timedout = TRUE;
2615 continue;
2617 /* When 'insertmode' is set, ESC just beeps in Insert
2618 * mode. Use CTRL-L to make edit() return.
2619 * For the command line only CTRL-C always breaks it.
2620 * For the cmdline window: Alternate between ESC and
2621 * CTRL-C: ESC for most situations and CTRL-C to close the
2622 * cmdline window. */
2623 if (p_im && (State & INSERT))
2624 c = Ctrl_L;
2625 else if ((State & CMDLINE)
2626 # ifdef FEAT_CMDWIN
2627 || (cmdwin_type > 0 && tc == ESC)
2628 # endif
2630 c = Ctrl_C;
2631 else
2632 c = ESC;
2633 # ifdef FEAT_CMDWIN
2634 tc = c;
2635 # endif
2636 break;
2638 #endif
2641 * get a character: 3. from the user - update display
2643 /* In insert mode a screen update is skipped when characters
2644 * are still available. But when those available characters
2645 * are part of a mapping, and we are going to do a blocking
2646 * wait here. Need to update the screen to display the
2647 * changed text so far. */
2648 if ((State & INSERT) && advance && must_redraw != 0)
2650 update_screen(0);
2651 setcursor(); /* put cursor back where it belongs */
2655 * If we have a partial match (and are going to wait for more
2656 * input from the user), show the partially matched characters
2657 * to the user with showcmd.
2659 #ifdef FEAT_CMDL_INFO
2660 i = 0;
2661 #endif
2662 c1 = 0;
2663 if (typebuf.tb_len > 0 && advance && !exmode_active)
2665 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2666 && State != HITRETURN)
2668 /* this looks nice when typing a dead character map */
2669 if (State & INSERT
2670 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2671 + typebuf.tb_len - 1) == 1)
2673 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2674 + typebuf.tb_len - 1], FALSE);
2675 setcursor(); /* put cursor back where it belongs */
2676 c1 = 1;
2678 #ifdef FEAT_CMDL_INFO
2679 /* need to use the col and row from above here */
2680 old_wcol = curwin->w_wcol;
2681 old_wrow = curwin->w_wrow;
2682 curwin->w_wcol = new_wcol;
2683 curwin->w_wrow = new_wrow;
2684 push_showcmd();
2685 if (typebuf.tb_len > SHOWCMD_COLS)
2686 i = typebuf.tb_len - SHOWCMD_COLS;
2687 while (i < typebuf.tb_len)
2688 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2689 + i++]);
2690 curwin->w_wcol = old_wcol;
2691 curwin->w_wrow = old_wrow;
2692 #endif
2695 /* this looks nice when typing a dead character map */
2696 if ((State & CMDLINE)
2697 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2698 && cmdline_star == 0
2699 #endif
2700 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2701 + typebuf.tb_len - 1) == 1)
2703 putcmdline(typebuf.tb_buf[typebuf.tb_off
2704 + typebuf.tb_len - 1], FALSE);
2705 c1 = 1;
2710 * get a character: 3. from the user - get it
2712 wait_tb_len = typebuf.tb_len;
2713 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2714 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2715 !advance
2717 : ((typebuf.tb_len == 0
2718 || !(p_timeout || (p_ttimeout
2719 && keylen == KL_PART_KEY)))
2720 ? -1L
2721 : ((keylen == KL_PART_KEY && p_ttm >= 0)
2722 ? p_ttm
2723 : p_tm)), typebuf.tb_change_cnt);
2725 #ifdef FEAT_CMDL_INFO
2726 if (i != 0)
2727 pop_showcmd();
2728 #endif
2729 if (c1 == 1)
2731 if (State & INSERT)
2732 edit_unputchar();
2733 if (State & CMDLINE)
2734 unputcmdline();
2735 setcursor(); /* put cursor back where it belongs */
2738 if (c < 0)
2739 continue; /* end of input script reached */
2740 if (c == NUL) /* no character available */
2742 if (!advance)
2743 break;
2744 if (wait_tb_len > 0) /* timed out */
2746 timedout = TRUE;
2747 continue;
2750 else
2751 { /* allow mapping for just typed characters */
2752 while (typebuf.tb_buf[typebuf.tb_off
2753 + typebuf.tb_len] != NUL)
2754 typebuf.tb_noremap[typebuf.tb_off
2755 + typebuf.tb_len++] = RM_YES;
2756 #ifdef USE_IM_CONTROL
2757 /* Get IM status right after getting keys, not after the
2758 * timeout for a mapping (focus may be lost by then). */
2759 vgetc_im_active = im_get_status();
2760 #endif
2762 } /* for (;;) */
2763 } /* if (!character from stuffbuf) */
2765 /* if advance is FALSE don't loop on NULs */
2766 } while (c < 0 || (advance && c == NUL));
2769 * The "INSERT" message is taken care of here:
2770 * if we return an ESC to exit insert mode, the message is deleted
2771 * if we don't return an ESC but deleted the message before, redisplay it
2773 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
2775 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
2777 if (typebuf.tb_len && !KeyTyped)
2778 redraw_cmdline = TRUE; /* delete mode later */
2779 else
2780 unshowmode(FALSE);
2782 else if (c != ESC && mode_deleted)
2784 if (typebuf.tb_len && !KeyTyped)
2785 redraw_cmdline = TRUE; /* show mode later */
2786 else
2787 showmode();
2790 #ifdef FEAT_GUI
2791 /* may unshow different cursor shape */
2792 if (gui.in_use && shape_changed)
2793 gui_update_cursor(TRUE, FALSE);
2794 #endif
2796 --vgetc_busy;
2798 return c;
2802 * inchar() - get one character from
2803 * 1. a scriptfile
2804 * 2. the keyboard
2806 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2807 * NUL terminated (buffer length must be 'maxlen' + 1).
2808 * Minimum for "maxlen" is 3!!!!
2810 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2811 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2812 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2813 * otherwise.
2815 * If we got an interrupt all input is read until none is available.
2817 * If wait_time == 0 there is no waiting for the char.
2818 * If wait_time == n we wait for n msec for a character to arrive.
2819 * If wait_time == -1 we wait forever for a character to arrive.
2821 * Return the number of obtained characters.
2822 * Return -1 when end of input script reached.
2825 inchar(buf, maxlen, wait_time, tb_change_cnt)
2826 char_u *buf;
2827 int maxlen;
2828 long wait_time; /* milli seconds */
2829 int tb_change_cnt;
2831 int len = 0; /* init for GCC */
2832 int retesc = FALSE; /* return ESC with gotint */
2833 int script_char;
2835 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2837 cursor_on();
2838 out_flush();
2839 #ifdef FEAT_GUI
2840 if (gui.in_use)
2842 gui_update_cursor(FALSE, FALSE);
2843 # ifdef FEAT_MOUSESHAPE
2844 if (postponed_mouseshape)
2845 update_mouseshape(-1);
2846 # endif
2848 #endif
2852 * Don't reset these when at the hit-return prompt, otherwise a endless
2853 * recursive loop may result (write error in swapfile, hit-return, timeout
2854 * on char wait, flush swapfile, write error....).
2856 if (State != HITRETURN)
2858 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2859 did_swapwrite_msg = FALSE; /* display swap file write error again */
2861 undo_off = FALSE; /* restart undo now */
2864 * first try script file
2865 * If interrupted: Stop reading script files.
2867 script_char = -1;
2868 while (scriptin[curscript] != NULL && script_char < 0)
2870 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2872 /* Reached EOF.
2873 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2874 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2875 closescript();
2877 * When reading script file is interrupted, return an ESC to get
2878 * back to normal mode.
2879 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2881 if (got_int)
2882 retesc = TRUE;
2883 else
2884 return -1;
2886 else
2888 buf[0] = script_char;
2889 len = 1;
2893 if (script_char < 0) /* did not get a character from script */
2896 * If we got an interrupt, skip all previously typed characters and
2897 * return TRUE if quit reading script file.
2898 * Stop reading typeahead when a single CTRL-C was read,
2899 * fill_input_buf() returns this when not able to read from stdin.
2900 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2901 * and buf may be pointing inside typebuf.tb_buf[].
2903 if (got_int)
2905 #define DUM_LEN MAXMAPLEN * 3 + 3
2906 char_u dum[DUM_LEN + 1];
2908 for (;;)
2910 len = ui_inchar(dum, DUM_LEN, 0L, 0);
2911 if (len == 0 || (len == 1 && dum[0] == 3))
2912 break;
2914 return retesc;
2918 * Always flush the output characters when getting input characters
2919 * from the user.
2921 out_flush();
2924 * Fill up to a third of the buffer, because each character may be
2925 * tripled below.
2927 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
2930 if (typebuf_changed(tb_change_cnt))
2931 return 0;
2933 return fix_input_buffer(buf, len, script_char >= 0);
2937 * Fix typed characters for use by vgetc() and check_termcode().
2938 * buf[] must have room to triple the number of bytes!
2939 * Returns the new length.
2942 fix_input_buffer(buf, len, script)
2943 char_u *buf;
2944 int len;
2945 int script; /* TRUE when reading from a script */
2947 int i;
2948 char_u *p = buf;
2951 * Two characters are special: NUL and K_SPECIAL.
2952 * When compiled With the GUI CSI is also special.
2953 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
2954 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
2955 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
2956 * Don't replace K_SPECIAL when reading a script file.
2958 for (i = len; --i >= 0; ++p)
2960 #ifdef FEAT_GUI
2961 /* When the GUI is used any character can come after a CSI, don't
2962 * escape it. */
2963 if (gui.in_use && p[0] == CSI && i >= 2)
2965 p += 2;
2966 i -= 2;
2968 /* When the GUI is not used CSI needs to be escaped. */
2969 else if (!gui.in_use && p[0] == CSI)
2971 mch_memmove(p + 3, p + 1, (size_t)i);
2972 *p++ = K_SPECIAL;
2973 *p++ = KS_EXTRA;
2974 *p = (int)KE_CSI;
2975 len += 2;
2977 else
2978 #endif
2979 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
2980 #ifdef FEAT_AUTOCMD
2981 /* timeout may generate K_CURSORHOLD */
2982 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
2983 #endif
2984 #if defined(WIN3264) && !defined(FEAT_GUI)
2985 /* Win32 console passes modifiers */
2986 && (i < 2 || p[1] != KS_MODIFIER)
2987 #endif
2990 mch_memmove(p + 3, p + 1, (size_t)i);
2991 p[2] = K_THIRD(p[0]);
2992 p[1] = K_SECOND(p[0]);
2993 p[0] = K_SPECIAL;
2994 p += 2;
2995 len += 2;
2998 *p = NUL; /* add trailing NUL */
2999 return len;
3002 #if defined(USE_INPUT_BUF) || defined(PROTO)
3004 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3005 * Normally the input buffer would be sufficient, but the server_to_input_buf()
3006 * or feedkeys() may insert characters in the typeahead buffer while we are
3007 * waiting for input to arrive.
3010 input_available()
3012 return (!vim_is_input_buf_empty()
3013 # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3014 || typebuf_was_filled
3015 # endif
3018 #endif
3021 * map[!] : show all key mappings
3022 * map[!] {lhs} : show key mapping for {lhs}
3023 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3024 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3025 * unmap[!] {lhs} : remove key mapping for {lhs}
3026 * abbr : show all abbreviations
3027 * abbr {lhs} : show abbreviations for {lhs}
3028 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3029 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3030 * unabbr {lhs} : remove abbreviation for {lhs}
3032 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3034 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3035 * it will be modified.
3037 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
3038 * for :map! mode is INSERT + CMDLINE
3039 * for :cmap mode is CMDLINE
3040 * for :imap mode is INSERT
3041 * for :lmap mode is LANGMAP
3042 * for :nmap mode is NORMAL
3043 * for :vmap mode is VISUAL + SELECTMODE
3044 * for :xmap mode is VISUAL
3045 * for :smap mode is SELECTMODE
3046 * for :omap mode is OP_PENDING
3048 * for :abbr mode is INSERT + CMDLINE
3049 * for :iabbr mode is INSERT
3050 * for :cabbr mode is CMDLINE
3052 * Return 0 for success
3053 * 1 for invalid arguments
3054 * 2 for no match
3055 * 4 for out of mem
3056 * 5 for entry not unique
3059 do_map(maptype, arg, mode, abbrev)
3060 int maptype;
3061 char_u *arg;
3062 int mode;
3063 int abbrev; /* not a mapping but an abbreviation */
3065 char_u *keys;
3066 mapblock_T *mp, **mpp;
3067 char_u *rhs;
3068 char_u *p;
3069 int n;
3070 int len = 0; /* init for GCC */
3071 char_u *newstr;
3072 int hasarg;
3073 int haskey;
3074 int did_it = FALSE;
3075 #ifdef FEAT_LOCALMAP
3076 int did_local = FALSE;
3077 #endif
3078 int round;
3079 char_u *keys_buf = NULL;
3080 char_u *arg_buf = NULL;
3081 int retval = 0;
3082 int do_backslash;
3083 int hash;
3084 int new_hash;
3085 mapblock_T **abbr_table;
3086 mapblock_T **map_table;
3087 int unique = FALSE;
3088 int silent = FALSE;
3089 int special = FALSE;
3090 #ifdef FEAT_EVAL
3091 int expr = FALSE;
3092 #endif
3093 int noremap;
3095 keys = arg;
3096 map_table = maphash;
3097 abbr_table = &first_abbr;
3099 /* For ":noremap" don't remap, otherwise do remap. */
3100 if (maptype == 2)
3101 noremap = REMAP_NONE;
3102 else
3103 noremap = REMAP_YES;
3105 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
3106 for (;;)
3108 #ifdef FEAT_LOCALMAP
3110 * Check for "<buffer>": mapping local to buffer.
3112 if (STRNCMP(keys, "<buffer>", 8) == 0)
3114 keys = skipwhite(keys + 8);
3115 map_table = curbuf->b_maphash;
3116 abbr_table = &curbuf->b_first_abbr;
3117 continue;
3119 #endif
3122 * Check for "<silent>": don't echo commands.
3124 if (STRNCMP(keys, "<silent>", 8) == 0)
3126 keys = skipwhite(keys + 8);
3127 silent = TRUE;
3128 continue;
3132 * Check for "<special>": accept special keys in <>
3134 if (STRNCMP(keys, "<special>", 9) == 0)
3136 keys = skipwhite(keys + 9);
3137 special = TRUE;
3138 continue;
3141 #ifdef FEAT_EVAL
3143 * Check for "<script>": remap script-local mappings only
3145 if (STRNCMP(keys, "<script>", 8) == 0)
3147 keys = skipwhite(keys + 8);
3148 noremap = REMAP_SCRIPT;
3149 continue;
3153 * Check for "<expr>": {rhs} is an expression.
3155 if (STRNCMP(keys, "<expr>", 6) == 0)
3157 keys = skipwhite(keys + 6);
3158 expr = TRUE;
3159 continue;
3161 #endif
3163 * Check for "<unique>": don't overwrite an existing mapping.
3165 if (STRNCMP(keys, "<unique>", 8) == 0)
3167 keys = skipwhite(keys + 8);
3168 unique = TRUE;
3169 continue;
3171 break;
3174 validate_maphash();
3177 * find end of keys and skip CTRL-Vs (and backslashes) in it
3178 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
3179 * with :unmap white space is included in the keys, no argument possible
3181 p = keys;
3182 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3183 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3185 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3186 p[1] != NUL)
3187 ++p; /* skip CTRL-V or backslash */
3188 ++p;
3190 if (*p != NUL)
3191 *p++ = NUL;
3192 p = skipwhite(p);
3193 rhs = p;
3194 hasarg = (*rhs != NUL);
3195 haskey = (*keys != NUL);
3197 /* check for :unmap without argument */
3198 if (maptype == 1 && !haskey)
3200 retval = 1;
3201 goto theend;
3205 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3206 * with the appropriate two bytes. If it is a shifted special key, unshift
3207 * it too, giving another two bytes.
3208 * replace_termcodes() may move the result to allocated memory, which
3209 * needs to be freed later (*keys_buf and *arg_buf).
3210 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3212 if (haskey)
3213 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
3214 if (hasarg)
3216 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3217 rhs = (char_u *)"";
3218 else
3219 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
3222 #ifdef FEAT_FKMAP
3224 * when in right-to-left mode and alternate keymap option set,
3225 * reverse the character flow in the rhs in Farsi.
3227 if (p_altkeymap && curwin->w_p_rl)
3228 lrswap(rhs);
3229 #endif
3232 * check arguments and translate function keys
3234 if (haskey)
3236 len = (int)STRLEN(keys);
3237 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3239 retval = 1;
3240 goto theend;
3243 if (abbrev && maptype != 1)
3246 * If an abbreviation ends in a keyword character, the
3247 * rest must be all keyword-char or all non-keyword-char.
3248 * Otherwise we won't be able to find the start of it in a
3249 * vi-compatible way.
3251 #ifdef FEAT_MBYTE
3252 if (has_mbyte)
3254 int first, last;
3255 int same = -1;
3257 first = vim_iswordp(keys);
3258 last = first;
3259 p = keys + (*mb_ptr2len)(keys);
3260 n = 1;
3261 while (p < keys + len)
3263 ++n; /* nr of (multi-byte) chars */
3264 last = vim_iswordp(p); /* type of last char */
3265 if (same == -1 && last != first)
3266 same = n - 1; /* count of same char type */
3267 p += (*mb_ptr2len)(p);
3269 if (last && n > 2 && same >= 0 && same < n - 1)
3271 retval = 1;
3272 goto theend;
3275 else
3276 #endif
3277 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3278 for (n = 0; n < len - 2; ++n)
3279 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3281 retval = 1;
3282 goto theend;
3284 /* An abbrevation cannot contain white space. */
3285 for (n = 0; n < len; ++n)
3286 if (vim_iswhite(keys[n]))
3288 retval = 1;
3289 goto theend;
3294 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3295 no_abbr = FALSE; /* reset flag that indicates there are
3296 no abbreviations */
3298 if (!haskey || (maptype != 1 && !hasarg))
3299 msg_start();
3301 #ifdef FEAT_LOCALMAP
3303 * Check if a new local mapping wasn't already defined globally.
3305 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3307 /* need to loop over all global hash lists */
3308 for (hash = 0; hash < 256 && !got_int; ++hash)
3310 if (abbrev)
3312 if (hash != 0) /* there is only one abbreviation list */
3313 break;
3314 mp = first_abbr;
3316 else
3317 mp = maphash[hash];
3318 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3320 /* check entries with the same mode */
3321 if ((mp->m_mode & mode) != 0
3322 && mp->m_keylen == len
3323 && unique
3324 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3326 if (abbrev)
3327 EMSG2(_("E224: global abbreviation already exists for %s"),
3328 mp->m_keys);
3329 else
3330 EMSG2(_("E225: global mapping already exists for %s"),
3331 mp->m_keys);
3332 retval = 5;
3333 goto theend;
3340 * When listing global mappings, also list buffer-local ones here.
3342 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3344 /* need to loop over all global hash lists */
3345 for (hash = 0; hash < 256 && !got_int; ++hash)
3347 if (abbrev)
3349 if (hash != 0) /* there is only one abbreviation list */
3350 break;
3351 mp = curbuf->b_first_abbr;
3353 else
3354 mp = curbuf->b_maphash[hash];
3355 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3357 /* check entries with the same mode */
3358 if ((mp->m_mode & mode) != 0)
3360 if (!haskey) /* show all entries */
3362 showmap(mp, TRUE);
3363 did_local = TRUE;
3365 else
3367 n = mp->m_keylen;
3368 if (STRNCMP(mp->m_keys, keys,
3369 (size_t)(n < len ? n : len)) == 0)
3371 showmap(mp, TRUE);
3372 did_local = TRUE;
3379 #endif
3382 * Find an entry in the maphash[] list that matches.
3383 * For :unmap we may loop two times: once to try to unmap an entry with a
3384 * matching 'from' part, a second time, if the first fails, to unmap an
3385 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3386 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3387 * "bar" because of the abbreviation.
3389 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3390 && !did_it && !got_int; ++round)
3392 /* need to loop over all hash lists */
3393 for (hash = 0; hash < 256 && !got_int; ++hash)
3395 if (abbrev)
3397 if (hash > 0) /* there is only one abbreviation list */
3398 break;
3399 mpp = abbr_table;
3401 else
3402 mpp = &(map_table[hash]);
3403 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3406 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3408 mpp = &(mp->m_next);
3409 continue;
3411 if (!haskey) /* show all entries */
3413 showmap(mp, map_table != maphash);
3414 did_it = TRUE;
3416 else /* do we have a match? */
3418 if (round) /* second round: Try unmap "rhs" string */
3420 n = (int)STRLEN(mp->m_str);
3421 p = mp->m_str;
3423 else
3425 n = mp->m_keylen;
3426 p = mp->m_keys;
3428 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3430 if (maptype == 1) /* delete entry */
3432 /* Only accept a full match. For abbreviations we
3433 * ignore trailing space when matching with the
3434 * "lhs", since an abbreviation can't have
3435 * trailing space. */
3436 if (n != len && (!abbrev || round || n > len
3437 || *skipwhite(keys + n) != NUL))
3439 mpp = &(mp->m_next);
3440 continue;
3443 * We reset the indicated mode bits. If nothing is
3444 * left the entry is deleted below.
3446 mp->m_mode &= ~mode;
3447 did_it = TRUE; /* remember we did something */
3449 else if (!hasarg) /* show matching entry */
3451 showmap(mp, map_table != maphash);
3452 did_it = TRUE;
3454 else if (n != len) /* new entry is ambiguous */
3456 mpp = &(mp->m_next);
3457 continue;
3459 else if (unique)
3461 if (abbrev)
3462 EMSG2(_("E226: abbreviation already exists for %s"),
3464 else
3465 EMSG2(_("E227: mapping already exists for %s"), p);
3466 retval = 5;
3467 goto theend;
3469 else /* new rhs for existing entry */
3471 mp->m_mode &= ~mode; /* remove mode bits */
3472 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3474 newstr = vim_strsave(rhs);
3475 if (newstr == NULL)
3477 retval = 4; /* no mem */
3478 goto theend;
3480 vim_free(mp->m_str);
3481 mp->m_str = newstr;
3482 mp->m_noremap = noremap;
3483 mp->m_silent = silent;
3484 mp->m_mode = mode;
3485 #ifdef FEAT_EVAL
3486 mp->m_expr = expr;
3487 mp->m_script_ID = current_SID;
3488 #endif
3489 did_it = TRUE;
3492 if (mp->m_mode == 0) /* entry can be deleted */
3494 map_free(mpp);
3495 continue; /* continue with *mpp */
3499 * May need to put this entry into another hash list.
3501 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3502 if (!abbrev && new_hash != hash)
3504 *mpp = mp->m_next;
3505 mp->m_next = map_table[new_hash];
3506 map_table[new_hash] = mp;
3508 continue; /* continue with *mpp */
3512 mpp = &(mp->m_next);
3517 if (maptype == 1) /* delete entry */
3519 if (!did_it)
3520 retval = 2; /* no match */
3521 goto theend;
3524 if (!haskey || !hasarg) /* print entries */
3526 if (!did_it
3527 #ifdef FEAT_LOCALMAP
3528 && !did_local
3529 #endif
3532 if (abbrev)
3533 MSG(_("No abbreviation found"));
3534 else
3535 MSG(_("No mapping found"));
3537 goto theend; /* listing finished */
3540 if (did_it) /* have added the new entry already */
3541 goto theend;
3544 * Get here when adding a new entry to the maphash[] list or abbrlist.
3546 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3547 if (mp == NULL)
3549 retval = 4; /* no mem */
3550 goto theend;
3553 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3554 if (*keys == Ctrl_C)
3555 mapped_ctrl_c = TRUE;
3557 mp->m_keys = vim_strsave(keys);
3558 mp->m_str = vim_strsave(rhs);
3559 if (mp->m_keys == NULL || mp->m_str == NULL)
3561 vim_free(mp->m_keys);
3562 vim_free(mp->m_str);
3563 vim_free(mp);
3564 retval = 4; /* no mem */
3565 goto theend;
3567 mp->m_keylen = (int)STRLEN(mp->m_keys);
3568 mp->m_noremap = noremap;
3569 mp->m_silent = silent;
3570 mp->m_mode = mode;
3571 #ifdef FEAT_EVAL
3572 mp->m_expr = expr;
3573 mp->m_script_ID = current_SID;
3574 #endif
3576 /* add the new entry in front of the abbrlist or maphash[] list */
3577 if (abbrev)
3579 mp->m_next = *abbr_table;
3580 *abbr_table = mp;
3582 else
3584 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3585 mp->m_next = map_table[n];
3586 map_table[n] = mp;
3589 theend:
3590 vim_free(keys_buf);
3591 vim_free(arg_buf);
3592 return retval;
3596 * Delete one entry from the abbrlist or maphash[].
3597 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3599 static void
3600 map_free(mpp)
3601 mapblock_T **mpp;
3603 mapblock_T *mp;
3605 mp = *mpp;
3606 vim_free(mp->m_keys);
3607 vim_free(mp->m_str);
3608 *mpp = mp->m_next;
3609 vim_free(mp);
3613 * Initialize maphash[] for first use.
3615 static void
3616 validate_maphash()
3618 if (!maphash_valid)
3620 vim_memset(maphash, 0, sizeof(maphash));
3621 maphash_valid = TRUE;
3626 * Get the mapping mode from the command name.
3629 get_map_mode(cmdp, forceit)
3630 char_u **cmdp;
3631 int forceit;
3633 char_u *p;
3634 int modec;
3635 int mode;
3637 p = *cmdp;
3638 modec = *p++;
3639 if (modec == 'i')
3640 mode = INSERT; /* :imap */
3641 else if (modec == 'l')
3642 mode = LANGMAP; /* :lmap */
3643 else if (modec == 'c')
3644 mode = CMDLINE; /* :cmap */
3645 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3646 mode = NORMAL; /* :nmap */
3647 else if (modec == 'v')
3648 mode = VISUAL + SELECTMODE; /* :vmap */
3649 else if (modec == 'x')
3650 mode = VISUAL; /* :xmap */
3651 else if (modec == 's')
3652 mode = SELECTMODE; /* :smap */
3653 else if (modec == 'o')
3654 mode = OP_PENDING; /* :omap */
3655 else
3657 --p;
3658 if (forceit)
3659 mode = INSERT + CMDLINE; /* :map ! */
3660 else
3661 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
3664 *cmdp = p;
3665 return mode;
3669 * Clear all mappings or abbreviations.
3670 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3672 /*ARGSUSED*/
3673 void
3674 map_clear(cmdp, arg, forceit, abbr)
3675 char_u *cmdp;
3676 char_u *arg;
3677 int forceit;
3678 int abbr;
3680 int mode;
3681 #ifdef FEAT_LOCALMAP
3682 int local;
3684 local = (STRCMP(arg, "<buffer>") == 0);
3685 if (!local && *arg != NUL)
3687 EMSG(_(e_invarg));
3688 return;
3690 #endif
3692 mode = get_map_mode(&cmdp, forceit);
3693 map_clear_int(curbuf, mode,
3694 #ifdef FEAT_LOCALMAP
3695 local,
3696 #else
3697 FALSE,
3698 #endif
3699 abbr);
3703 * Clear all mappings in "mode".
3705 /*ARGSUSED*/
3706 void
3707 map_clear_int(buf, mode, local, abbr)
3708 buf_T *buf; /* buffer for local mappings */
3709 int mode; /* mode in which to delete */
3710 int local; /* TRUE for buffer-local mappings */
3711 int abbr; /* TRUE for abbreviations */
3713 mapblock_T *mp, **mpp;
3714 int hash;
3715 int new_hash;
3717 validate_maphash();
3719 for (hash = 0; hash < 256; ++hash)
3721 if (abbr)
3723 if (hash > 0) /* there is only one abbrlist */
3724 break;
3725 #ifdef FEAT_LOCALMAP
3726 if (local)
3727 mpp = &buf->b_first_abbr;
3728 else
3729 #endif
3730 mpp = &first_abbr;
3732 else
3734 #ifdef FEAT_LOCALMAP
3735 if (local)
3736 mpp = &buf->b_maphash[hash];
3737 else
3738 #endif
3739 mpp = &maphash[hash];
3741 while (*mpp != NULL)
3743 mp = *mpp;
3744 if (mp->m_mode & mode)
3746 mp->m_mode &= ~mode;
3747 if (mp->m_mode == 0) /* entry can be deleted */
3749 map_free(mpp);
3750 continue;
3753 * May need to put this entry into another hash list.
3755 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3756 if (!abbr && new_hash != hash)
3758 *mpp = mp->m_next;
3759 #ifdef FEAT_LOCALMAP
3760 if (local)
3762 mp->m_next = buf->b_maphash[new_hash];
3763 buf->b_maphash[new_hash] = mp;
3765 else
3766 #endif
3768 mp->m_next = maphash[new_hash];
3769 maphash[new_hash] = mp;
3771 continue; /* continue with *mpp */
3774 mpp = &(mp->m_next);
3779 static void
3780 showmap(mp, local)
3781 mapblock_T *mp;
3782 int local; /* TRUE for buffer-local map */
3784 int len = 1;
3786 if (msg_didout || msg_silent != 0)
3787 msg_putchar('\n');
3788 if ((mp->m_mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3789 msg_putchar('!'); /* :map! */
3790 else if (mp->m_mode & INSERT)
3791 msg_putchar('i'); /* :imap */
3792 else if (mp->m_mode & LANGMAP)
3793 msg_putchar('l'); /* :lmap */
3794 else if (mp->m_mode & CMDLINE)
3795 msg_putchar('c'); /* :cmap */
3796 else if ((mp->m_mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3797 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3798 msg_putchar(' '); /* :map */
3799 else
3801 len = 0;
3802 if (mp->m_mode & NORMAL)
3804 msg_putchar('n'); /* :nmap */
3805 ++len;
3807 if (mp->m_mode & OP_PENDING)
3809 msg_putchar('o'); /* :omap */
3810 ++len;
3812 if ((mp->m_mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3814 msg_putchar('v'); /* :vmap */
3815 ++len;
3817 else
3819 if (mp->m_mode & VISUAL)
3821 msg_putchar('x'); /* :xmap */
3822 ++len;
3824 if (mp->m_mode & SELECTMODE)
3826 msg_putchar('s'); /* :smap */
3827 ++len;
3831 while (++len <= 3)
3832 msg_putchar(' ');
3834 /* Get length of what we write */
3835 len = msg_outtrans_special(mp->m_keys, TRUE);
3838 msg_putchar(' '); /* padd with blanks */
3839 ++len;
3840 } while (len < 12);
3842 if (mp->m_noremap == REMAP_NONE)
3843 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3844 else if (mp->m_noremap == REMAP_SCRIPT)
3845 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3846 else
3847 msg_putchar(' ');
3849 if (local)
3850 msg_putchar('@');
3851 else
3852 msg_putchar(' ');
3854 /* Use FALSE below if we only want things like <Up> to show up as such on
3855 * the rhs, and not M-x etc, TRUE gets both -- webb
3857 if (*mp->m_str == NUL)
3858 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3859 else
3860 msg_outtrans_special(mp->m_str, FALSE);
3861 #ifdef FEAT_EVAL
3862 if (p_verbose > 0)
3863 last_set_msg(mp->m_script_ID);
3864 #endif
3865 out_flush(); /* show one line at a time */
3868 #if defined(FEAT_EVAL) || defined(PROTO)
3870 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3871 * Recognize termcap codes in "str".
3872 * Also checks mappings local to the current buffer.
3875 map_to_exists(str, modechars, abbr)
3876 char_u *str;
3877 char_u *modechars;
3878 int abbr;
3880 int mode = 0;
3881 char_u *rhs;
3882 char_u *buf;
3883 int retval;
3885 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
3887 if (vim_strchr(modechars, 'n') != NULL)
3888 mode |= NORMAL;
3889 if (vim_strchr(modechars, 'v') != NULL)
3890 mode |= VISUAL + SELECTMODE;
3891 if (vim_strchr(modechars, 'x') != NULL)
3892 mode |= VISUAL;
3893 if (vim_strchr(modechars, 's') != NULL)
3894 mode |= SELECTMODE;
3895 if (vim_strchr(modechars, 'o') != NULL)
3896 mode |= OP_PENDING;
3897 if (vim_strchr(modechars, 'i') != NULL)
3898 mode |= INSERT;
3899 if (vim_strchr(modechars, 'l') != NULL)
3900 mode |= LANGMAP;
3901 if (vim_strchr(modechars, 'c') != NULL)
3902 mode |= CMDLINE;
3904 retval = map_to_exists_mode(rhs, mode, abbr);
3905 vim_free(buf);
3907 return retval;
3909 #endif
3912 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
3913 * Also checks mappings local to the current buffer.
3916 map_to_exists_mode(rhs, mode, abbr)
3917 char_u *rhs;
3918 int mode;
3919 int abbr;
3921 mapblock_T *mp;
3922 int hash;
3923 # ifdef FEAT_LOCALMAP
3924 int expand_buffer = FALSE;
3926 validate_maphash();
3928 /* Do it twice: once for global maps and once for local maps. */
3929 for (;;)
3931 # endif
3932 for (hash = 0; hash < 256; ++hash)
3934 if (abbr)
3936 if (hash > 0) /* there is only one abbr list */
3937 break;
3938 #ifdef FEAT_LOCALMAP
3939 if (expand_buffer)
3940 mp = curbuf->b_first_abbr;
3941 else
3942 #endif
3943 mp = first_abbr;
3945 # ifdef FEAT_LOCALMAP
3946 else if (expand_buffer)
3947 mp = curbuf->b_maphash[hash];
3948 # endif
3949 else
3950 mp = maphash[hash];
3951 for (; mp; mp = mp->m_next)
3953 if ((mp->m_mode & mode)
3954 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
3955 return TRUE;
3958 # ifdef FEAT_LOCALMAP
3959 if (expand_buffer)
3960 break;
3961 expand_buffer = TRUE;
3963 # endif
3965 return FALSE;
3968 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3970 * Used below when expanding mapping/abbreviation names.
3972 static int expand_mapmodes = 0;
3973 static int expand_isabbrev = 0;
3974 #ifdef FEAT_LOCALMAP
3975 static int expand_buffer = FALSE;
3976 #endif
3979 * Work out what to complete when doing command line completion of mapping
3980 * or abbreviation names.
3982 char_u *
3983 set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
3984 expand_T *xp;
3985 char_u *cmd;
3986 char_u *arg;
3987 int forceit; /* TRUE if '!' given */
3988 int isabbrev; /* TRUE if abbreviation */
3989 int isunmap; /* TRUE if unmap/unabbrev command */
3990 cmdidx_T cmdidx;
3992 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
3993 xp->xp_context = EXPAND_NOTHING;
3994 else
3996 if (isunmap)
3997 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
3998 else
4000 expand_mapmodes = INSERT + CMDLINE;
4001 if (!isabbrev)
4002 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
4004 expand_isabbrev = isabbrev;
4005 xp->xp_context = EXPAND_MAPPINGS;
4006 #ifdef FEAT_LOCALMAP
4007 expand_buffer = FALSE;
4008 #endif
4009 for (;;)
4011 #ifdef FEAT_LOCALMAP
4012 if (STRNCMP(arg, "<buffer>", 8) == 0)
4014 expand_buffer = TRUE;
4015 arg = skipwhite(arg + 8);
4016 continue;
4018 #endif
4019 if (STRNCMP(arg, "<unique>", 8) == 0)
4021 arg = skipwhite(arg + 8);
4022 continue;
4024 if (STRNCMP(arg, "<silent>", 8) == 0)
4026 arg = skipwhite(arg + 8);
4027 continue;
4029 #ifdef FEAT_EVAL
4030 if (STRNCMP(arg, "<script>", 8) == 0)
4032 arg = skipwhite(arg + 8);
4033 continue;
4035 if (STRNCMP(arg, "<expr>", 6) == 0)
4037 arg = skipwhite(arg + 6);
4038 continue;
4040 #endif
4041 break;
4043 xp->xp_pattern = arg;
4046 return NULL;
4050 * Find all mapping/abbreviation names that match regexp 'prog'.
4051 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4052 * Return OK if matches found, FAIL otherwise.
4055 ExpandMappings(regmatch, num_file, file)
4056 regmatch_T *regmatch;
4057 int *num_file;
4058 char_u ***file;
4060 mapblock_T *mp;
4061 int hash;
4062 int count;
4063 int round;
4064 char_u *p;
4065 int i;
4067 validate_maphash();
4069 *num_file = 0; /* return values in case of FAIL */
4070 *file = NULL;
4073 * round == 1: Count the matches.
4074 * round == 2: Build the array to keep the matches.
4076 for (round = 1; round <= 2; ++round)
4078 count = 0;
4080 for (i = 0; i < 5; ++i)
4082 if (i == 0)
4083 p = (char_u *)"<silent>";
4084 else if (i == 1)
4085 p = (char_u *)"<unique>";
4086 #ifdef FEAT_EVAL
4087 else if (i == 2)
4088 p = (char_u *)"<script>";
4089 else if (i == 3)
4090 p = (char_u *)"<expr>";
4091 #endif
4092 #ifdef FEAT_LOCALMAP
4093 else if (i == 4 && !expand_buffer)
4094 p = (char_u *)"<buffer>";
4095 #endif
4096 else
4097 continue;
4099 if (vim_regexec(regmatch, p, (colnr_T)0))
4101 if (round == 1)
4102 ++count;
4103 else
4104 (*file)[count++] = vim_strsave(p);
4108 for (hash = 0; hash < 256; ++hash)
4110 if (expand_isabbrev)
4112 if (hash > 0) /* only one abbrev list */
4113 break; /* for (hash) */
4114 mp = first_abbr;
4116 #ifdef FEAT_LOCALMAP
4117 else if (expand_buffer)
4118 mp = curbuf->b_maphash[hash];
4119 #endif
4120 else
4121 mp = maphash[hash];
4122 for (; mp; mp = mp->m_next)
4124 if (mp->m_mode & expand_mapmodes)
4126 p = translate_mapping(mp->m_keys, TRUE);
4127 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4129 if (round == 1)
4130 ++count;
4131 else
4133 (*file)[count++] = p;
4134 p = NULL;
4137 vim_free(p);
4139 } /* for (mp) */
4140 } /* for (hash) */
4142 if (count == 0) /* no match found */
4143 break; /* for (round) */
4145 if (round == 1)
4147 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4148 if (*file == NULL)
4149 return FAIL;
4151 } /* for (round) */
4153 if (count > 1)
4155 char_u **ptr1;
4156 char_u **ptr2;
4157 char_u **ptr3;
4159 /* Sort the matches */
4160 sort_strings(*file, count);
4162 /* Remove multiple entries */
4163 ptr1 = *file;
4164 ptr2 = ptr1 + 1;
4165 ptr3 = ptr1 + count;
4167 while (ptr2 < ptr3)
4169 if (STRCMP(*ptr1, *ptr2))
4170 *++ptr1 = *ptr2++;
4171 else
4173 vim_free(*ptr2++);
4174 count--;
4179 *num_file = count;
4180 return (count == 0 ? FAIL : OK);
4182 #endif /* FEAT_CMDL_COMPL */
4185 * Check for an abbreviation.
4186 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4187 * "c" is the character typed before check_abbr was called. It may have
4188 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4190 * Historic vi practice: The last character of an abbreviation must be an id
4191 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4192 * characters or all non-id characters. This allows for abbr. "#i" to
4193 * "#include".
4195 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4196 * Then there must be white space before the abbr.
4198 * return TRUE if there is an abbreviation, FALSE if not
4201 check_abbr(c, ptr, col, mincol)
4202 int c;
4203 char_u *ptr;
4204 int col;
4205 int mincol;
4207 int len;
4208 int scol; /* starting column of the abbr. */
4209 int j;
4210 char_u *s;
4211 #ifdef FEAT_MBYTE
4212 char_u tb[MB_MAXBYTES + 4];
4213 #else
4214 char_u tb[4];
4215 #endif
4216 mapblock_T *mp;
4217 #ifdef FEAT_LOCALMAP
4218 mapblock_T *mp2;
4219 #endif
4220 #ifdef FEAT_MBYTE
4221 int clen = 0; /* length in characters */
4222 #endif
4223 int is_id = TRUE;
4224 int vim_abbr;
4226 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4227 return FALSE;
4228 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0)
4229 /* no remapping implies no abbreviation */
4230 return FALSE;
4233 * Check for word before the cursor: If it ends in a keyword char all
4234 * chars before it must be al keyword chars or non-keyword chars, but not
4235 * white space. If it ends in a non-keyword char we accept any characters
4236 * before it except white space.
4238 if (col == 0) /* cannot be an abbr. */
4239 return FALSE;
4241 #ifdef FEAT_MBYTE
4242 if (has_mbyte)
4244 char_u *p;
4246 p = mb_prevptr(ptr, ptr + col);
4247 if (!vim_iswordp(p))
4248 vim_abbr = TRUE; /* Vim added abbr. */
4249 else
4251 vim_abbr = FALSE; /* vi compatible abbr. */
4252 if (p > ptr)
4253 is_id = vim_iswordp(mb_prevptr(ptr, p));
4255 clen = 1;
4256 while (p > ptr + mincol)
4258 p = mb_prevptr(ptr, p);
4259 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4261 p += (*mb_ptr2len)(p);
4262 break;
4264 ++clen;
4266 scol = (int)(p - ptr);
4268 else
4269 #endif
4271 if (!vim_iswordc(ptr[col - 1]))
4272 vim_abbr = TRUE; /* Vim added abbr. */
4273 else
4275 vim_abbr = FALSE; /* vi compatible abbr. */
4276 if (col > 1)
4277 is_id = vim_iswordc(ptr[col - 2]);
4279 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4280 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4284 if (scol < mincol)
4285 scol = mincol;
4286 if (scol < col) /* there is a word in front of the cursor */
4288 ptr += scol;
4289 len = col - scol;
4290 #ifdef FEAT_LOCALMAP
4291 mp = curbuf->b_first_abbr;
4292 mp2 = first_abbr;
4293 if (mp == NULL)
4295 mp = mp2;
4296 mp2 = NULL;
4298 #else
4299 mp = first_abbr;
4300 #endif
4301 for ( ; mp;
4302 #ifdef FEAT_LOCALMAP
4303 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4304 #endif
4305 (mp = mp->m_next))
4307 /* find entries with right mode and keys */
4308 if ( (mp->m_mode & State)
4309 && mp->m_keylen == len
4310 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4311 break;
4313 if (mp != NULL)
4316 * Found a match:
4317 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4318 * This goes from end to start.
4320 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4321 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4322 * Characters where IS_SPECIAL() == TRUE: key codes, need
4323 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4325 * Character CTRL-] is treated specially - it completes the
4326 * abbreviation, but is not inserted into the input stream.
4328 j = 0;
4329 /* special key code, split up */
4330 if (c != Ctrl_RSB)
4332 if (IS_SPECIAL(c) || c == K_SPECIAL)
4334 tb[j++] = K_SPECIAL;
4335 tb[j++] = K_SECOND(c);
4336 tb[j++] = K_THIRD(c);
4338 else
4340 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4341 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4342 #ifdef FEAT_MBYTE
4343 if (has_mbyte)
4345 /* if ABBR_OFF has been added, remove it here */
4346 if (c >= ABBR_OFF)
4347 c -= ABBR_OFF;
4348 j += (*mb_char2bytes)(c, tb + j);
4350 else
4351 #endif
4352 tb[j++] = c;
4354 tb[j] = NUL;
4355 /* insert the last typed char */
4356 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4358 #ifdef FEAT_EVAL
4359 if (mp->m_expr)
4360 s = eval_map_expr(mp->m_str);
4361 else
4362 #endif
4363 s = mp->m_str;
4364 if (s != NULL)
4366 /* insert the to string */
4367 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
4368 /* no abbrev. for these chars */
4369 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4370 #ifdef FEAT_EVAL
4371 if (mp->m_expr)
4372 vim_free(s);
4373 #endif
4376 tb[0] = Ctrl_H;
4377 tb[1] = NUL;
4378 #ifdef FEAT_MBYTE
4379 if (has_mbyte)
4380 len = clen; /* Delete characters instead of bytes */
4381 #endif
4382 while (len-- > 0) /* delete the from string */
4383 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4384 return TRUE;
4387 return FALSE;
4390 #ifdef FEAT_EVAL
4392 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4393 * special characters.
4395 static char_u *
4396 eval_map_expr(str)
4397 char_u *str;
4399 char_u *res;
4400 char_u *p;
4401 char_u *save_cmd;
4402 pos_T save_cursor;
4404 save_cmd = save_cmdline_alloc();
4405 if (save_cmd == NULL)
4406 return NULL;
4408 /* Forbid changing text or using ":normal" to avoid most of the bad side
4409 * effects. Also restore the cursor position. */
4410 ++textlock;
4411 #ifdef FEAT_EX_EXTRA
4412 ++ex_normal_lock;
4413 #endif
4414 save_cursor = curwin->w_cursor;
4415 p = eval_to_string(str, NULL, FALSE);
4416 --textlock;
4417 #ifdef FEAT_EX_EXTRA
4418 --ex_normal_lock;
4419 #endif
4420 curwin->w_cursor = save_cursor;
4422 restore_cmdline_alloc(save_cmd);
4423 if (p == NULL)
4424 return NULL;
4425 res = vim_strsave_escape_csi(p);
4426 vim_free(p);
4428 return res;
4430 #endif
4433 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4434 * can be put in the typeahead buffer.
4435 * Returns NULL when out of memory.
4437 char_u *
4438 vim_strsave_escape_csi(p)
4439 char_u *p;
4441 char_u *res;
4442 char_u *s, *d;
4444 /* Need a buffer to hold up to three times as much. */
4445 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
4446 if (res != NULL)
4448 d = res;
4449 for (s = p; *s != NUL; )
4451 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4453 /* Copy special key unmodified. */
4454 *d++ = *s++;
4455 *d++ = *s++;
4456 *d++ = *s++;
4458 else
4460 /* Add character, possibly multi-byte to destination, escaping
4461 * CSI and K_SPECIAL. */
4462 d = add_char2buf(PTR2CHAR(s), d);
4463 mb_ptr_adv(s);
4466 *d = NUL;
4468 return res;
4472 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4473 * vim_strsave_escape_csi(). Works in-place.
4475 void
4476 vim_unescape_csi(p)
4477 char_u *p;
4479 char_u *s = p, *d = p;
4481 while (*s != NUL)
4483 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4485 *d++ = K_SPECIAL;
4486 s += 3;
4488 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4489 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4491 *d++ = CSI;
4492 s += 3;
4494 else
4495 *d++ = *s++;
4497 *d = NUL;
4501 * Write map commands for the current mappings to an .exrc file.
4502 * Return FAIL on error, OK otherwise.
4505 makemap(fd, buf)
4506 FILE *fd;
4507 buf_T *buf; /* buffer for local mappings or NULL */
4509 mapblock_T *mp;
4510 char_u c1, c2;
4511 char_u *p;
4512 char *cmd;
4513 int abbr;
4514 int hash;
4515 int did_cpo = FALSE;
4516 int i;
4518 validate_maphash();
4521 * Do the loop twice: Once for mappings, once for abbreviations.
4522 * Then loop over all map hash lists.
4524 for (abbr = 0; abbr < 2; ++abbr)
4525 for (hash = 0; hash < 256; ++hash)
4527 if (abbr)
4529 if (hash > 0) /* there is only one abbr list */
4530 break;
4531 #ifdef FEAT_LOCALMAP
4532 if (buf != NULL)
4533 mp = buf->b_first_abbr;
4534 else
4535 #endif
4536 mp = first_abbr;
4538 else
4540 #ifdef FEAT_LOCALMAP
4541 if (buf != NULL)
4542 mp = buf->b_maphash[hash];
4543 else
4544 #endif
4545 mp = maphash[hash];
4548 for ( ; mp; mp = mp->m_next)
4550 /* skip script-local mappings */
4551 if (mp->m_noremap == REMAP_SCRIPT)
4552 continue;
4554 /* skip mappings that contain a <SNR> (script-local thing),
4555 * they probably don't work when loaded again */
4556 for (p = mp->m_str; *p != NUL; ++p)
4557 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4558 && p[2] == (int)KE_SNR)
4559 break;
4560 if (*p != NUL)
4561 continue;
4563 c1 = NUL;
4564 c2 = NUL;
4565 if (abbr)
4566 cmd = "abbr";
4567 else
4568 cmd = "map";
4569 switch (mp->m_mode)
4571 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
4572 break;
4573 case NORMAL:
4574 c1 = 'n';
4575 break;
4576 case VISUAL + SELECTMODE:
4577 c1 = 'v';
4578 break;
4579 case VISUAL:
4580 c1 = 'x';
4581 break;
4582 case SELECTMODE:
4583 c1 = 's';
4584 break;
4585 case OP_PENDING:
4586 c1 = 'o';
4587 break;
4588 case NORMAL + VISUAL + SELECTMODE:
4589 c1 = 'n';
4590 c2 = 'v';
4591 break;
4592 case VISUAL + SELECTMODE + OP_PENDING:
4593 c1 = 'v';
4594 c2 = 'o';
4595 break;
4596 case NORMAL + OP_PENDING:
4597 c1 = 'n';
4598 c2 = 'o';
4599 break;
4600 case CMDLINE + INSERT:
4601 if (!abbr)
4602 cmd = "map!";
4603 break;
4604 case CMDLINE:
4605 c1 = 'c';
4606 break;
4607 case INSERT:
4608 c1 = 'i';
4609 break;
4610 case LANGMAP:
4611 c1 = 'l';
4612 break;
4613 default:
4614 EMSG(_("E228: makemap: Illegal mode"));
4615 return FAIL;
4617 do /* may do this twice if c2 is set */
4619 /* When outputting <> form, need to make sure that 'cpo'
4620 * is set to the Vim default. */
4621 if (!did_cpo)
4623 if (*mp->m_str == NUL) /* will use <Nop> */
4624 did_cpo = TRUE;
4625 else
4626 for (i = 0; i < 2; ++i)
4627 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4628 if (*p == K_SPECIAL || *p == NL)
4629 did_cpo = TRUE;
4630 if (did_cpo)
4632 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4633 || put_eol(fd) < 0
4634 || fprintf(fd, "set cpo&vim") < 0
4635 || put_eol(fd) < 0)
4636 return FAIL;
4639 if (c1 && putc(c1, fd) < 0)
4640 return FAIL;
4641 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4642 return FAIL;
4643 if (fprintf(fd, cmd) < 0)
4644 return FAIL;
4645 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4646 return FAIL;
4647 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4648 return FAIL;
4649 #ifdef FEAT_EVAL
4650 if (mp->m_noremap == REMAP_SCRIPT
4651 && fputs("<script>", fd) < 0)
4652 return FAIL;
4653 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4654 return FAIL;
4655 #endif
4657 if ( putc(' ', fd) < 0
4658 || put_escstr(fd, mp->m_keys, 0) == FAIL
4659 || putc(' ', fd) < 0
4660 || put_escstr(fd, mp->m_str, 1) == FAIL
4661 || put_eol(fd) < 0)
4662 return FAIL;
4663 c1 = c2;
4664 c2 = NUL;
4666 while (c1);
4670 if (did_cpo)
4671 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4672 || put_eol(fd) < 0
4673 || fprintf(fd, "unlet s:cpo_save") < 0
4674 || put_eol(fd) < 0)
4675 return FAIL;
4676 return OK;
4680 * write escape string to file
4681 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4683 * return FAIL for failure, OK otherwise
4686 put_escstr(fd, strstart, what)
4687 FILE *fd;
4688 char_u *strstart;
4689 int what;
4691 char_u *str = strstart;
4692 int c;
4693 int modifiers;
4695 /* :map xx <Nop> */
4696 if (*str == NUL && what == 1)
4698 if (fprintf(fd, "<Nop>") < 0)
4699 return FAIL;
4700 return OK;
4703 for ( ; *str != NUL; ++str)
4705 #ifdef FEAT_MBYTE
4706 char_u *p;
4708 /* Check for a multi-byte character, which may contain escaped
4709 * K_SPECIAL and CSI bytes */
4710 p = mb_unescape(&str);
4711 if (p != NULL)
4713 while (*p != NUL)
4714 if (fputc(*p++, fd) < 0)
4715 return FAIL;
4716 --str;
4717 continue;
4719 #endif
4721 c = *str;
4723 * Special key codes have to be translated to be able to make sense
4724 * when they are read back.
4726 if (c == K_SPECIAL && what != 2)
4728 modifiers = 0x0;
4729 if (str[1] == KS_MODIFIER)
4731 modifiers = str[2];
4732 str += 3;
4733 c = *str;
4735 if (c == K_SPECIAL)
4737 c = TO_SPECIAL(str[1], str[2]);
4738 str += 2;
4740 if (IS_SPECIAL(c) || modifiers) /* special key */
4742 if (fprintf(fd, (char *)get_special_key_name(c, modifiers)) < 0)
4743 return FAIL;
4744 continue;
4749 * A '\n' in a map command should be written as <NL>.
4750 * A '\n' in a set command should be written as \^V^J.
4752 if (c == NL)
4754 if (what == 2)
4756 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4757 return FAIL;
4759 else
4761 if (fprintf(fd, "<NL>") < 0)
4762 return FAIL;
4764 continue;
4768 * Some characters have to be escaped with CTRL-V to
4769 * prevent them from misinterpreted in DoOneCmd().
4770 * A space, Tab and '"' has to be escaped with a backslash to
4771 * prevent it to be misinterpreted in do_set().
4772 * A space has to be escaped with a CTRL-V when it's at the start of a
4773 * ":map" rhs.
4774 * A '<' has to be escaped with a CTRL-V to prevent it being
4775 * interpreted as the start of a special key name.
4776 * A space in the lhs of a :map needs a CTRL-V.
4778 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4780 if (putc('\\', fd) < 0)
4781 return FAIL;
4783 else if (c < ' ' || c > '~' || c == '|'
4784 || (what == 0 && c == ' ')
4785 || (what == 1 && str == strstart && c == ' ')
4786 || (what != 2 && c == '<'))
4788 if (putc(Ctrl_V, fd) < 0)
4789 return FAIL;
4791 if (putc(c, fd) < 0)
4792 return FAIL;
4794 return OK;
4798 * Check all mappings for the presence of special key codes.
4799 * Used after ":set term=xxx".
4801 void
4802 check_map_keycodes()
4804 mapblock_T *mp;
4805 char_u *p;
4806 int i;
4807 char_u buf[3];
4808 char_u *save_name;
4809 int abbr;
4810 int hash;
4811 #ifdef FEAT_LOCALMAP
4812 buf_T *bp;
4813 #endif
4815 validate_maphash();
4816 save_name = sourcing_name;
4817 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4819 #ifdef FEAT_LOCALMAP
4820 /* This this once for each buffer, and then once for global
4821 * mappings/abbreviations with bp == NULL */
4822 for (bp = firstbuf; ; bp = bp->b_next)
4824 #endif
4826 * Do the loop twice: Once for mappings, once for abbreviations.
4827 * Then loop over all map hash lists.
4829 for (abbr = 0; abbr <= 1; ++abbr)
4830 for (hash = 0; hash < 256; ++hash)
4832 if (abbr)
4834 if (hash) /* there is only one abbr list */
4835 break;
4836 #ifdef FEAT_LOCALMAP
4837 if (bp != NULL)
4838 mp = bp->b_first_abbr;
4839 else
4840 #endif
4841 mp = first_abbr;
4843 else
4845 #ifdef FEAT_LOCALMAP
4846 if (bp != NULL)
4847 mp = bp->b_maphash[hash];
4848 else
4849 #endif
4850 mp = maphash[hash];
4852 for ( ; mp != NULL; mp = mp->m_next)
4854 for (i = 0; i <= 1; ++i) /* do this twice */
4856 if (i == 0)
4857 p = mp->m_keys; /* once for the "from" part */
4858 else
4859 p = mp->m_str; /* and once for the "to" part */
4860 while (*p)
4862 if (*p == K_SPECIAL)
4864 ++p;
4865 if (*p < 128) /* for "normal" tcap entries */
4867 buf[0] = p[0];
4868 buf[1] = p[1];
4869 buf[2] = NUL;
4870 (void)add_termcap_entry(buf, FALSE);
4872 ++p;
4874 ++p;
4879 #ifdef FEAT_LOCALMAP
4880 if (bp == NULL)
4881 break;
4883 #endif
4884 sourcing_name = save_name;
4887 #ifdef FEAT_EVAL
4889 * Check the string "keys" against the lhs of all mappings
4890 * Return pointer to rhs of mapping (mapblock->m_str)
4891 * NULL otherwise
4893 char_u *
4894 check_map(keys, mode, exact, ign_mod, abbr)
4895 char_u *keys;
4896 int mode;
4897 int exact; /* require exact match */
4898 int ign_mod; /* ignore preceding modifier */
4899 int abbr; /* do abbreviations */
4901 int hash;
4902 int len, minlen;
4903 mapblock_T *mp;
4904 char_u *s;
4905 #ifdef FEAT_LOCALMAP
4906 int local;
4907 #endif
4909 validate_maphash();
4911 len = (int)STRLEN(keys);
4912 #ifdef FEAT_LOCALMAP
4913 for (local = 1; local >= 0; --local)
4914 #endif
4915 /* loop over all hash lists */
4916 for (hash = 0; hash < 256; ++hash)
4918 if (abbr)
4920 if (hash > 0) /* there is only one list. */
4921 break;
4922 #ifdef FEAT_LOCALMAP
4923 if (local)
4924 mp = curbuf->b_first_abbr;
4925 else
4926 #endif
4927 mp = first_abbr;
4929 #ifdef FEAT_LOCALMAP
4930 else if (local)
4931 mp = curbuf->b_maphash[hash];
4932 #endif
4933 else
4934 mp = maphash[hash];
4935 for ( ; mp != NULL; mp = mp->m_next)
4937 /* skip entries with wrong mode, wrong length and not matching
4938 * ones */
4939 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
4941 if (len > mp->m_keylen)
4942 minlen = mp->m_keylen;
4943 else
4944 minlen = len;
4945 s = mp->m_keys;
4946 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
4947 && s[2] != NUL)
4949 s += 3;
4950 if (len > mp->m_keylen - 3)
4951 minlen = mp->m_keylen - 3;
4953 if (STRNCMP(s, keys, minlen) == 0)
4954 return mp->m_str;
4959 return NULL;
4961 #endif
4963 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
4965 #define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
4968 * Default mappings for some often used keys.
4970 static struct initmap
4972 char_u *arg;
4973 int mode;
4974 } initmappings[] =
4976 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4977 /* Use the Windows (CUA) keybindings. */
4978 # ifdef FEAT_GUI
4979 # if 0 /* These are now used to move tab pages */
4980 {(char_u *)"<C-PageUp> H", NORMAL+VIS_SEL},
4981 {(char_u *)"<C-PageUp> <C-O>H",INSERT},
4982 {(char_u *)"<C-PageDown> L$", NORMAL+VIS_SEL},
4983 {(char_u *)"<C-PageDown> <C-O>L<C-O>$", INSERT},
4984 # endif
4986 /* paste, copy and cut */
4987 {(char_u *)"<S-Insert> \"*P", NORMAL},
4988 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
4989 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
4990 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
4991 {(char_u *)"<S-Del> \"*d", VIS_SEL},
4992 {(char_u *)"<C-Del> \"*d", VIS_SEL},
4993 {(char_u *)"<C-X> \"*d", VIS_SEL},
4994 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
4995 # else
4996 # if 0 /* These are now used to move tab pages */
4997 {(char_u *)"\316\204 H", NORMAL+VIS_SEL}, /* CTRL-PageUp is "H" */
4998 {(char_u *)"\316\204 \017H",INSERT}, /* CTRL-PageUp is "^OH"*/
4999 {(char_u *)"\316v L$", NORMAL+VIS_SEL}, /* CTRL-PageDown is "L$" */
5000 {(char_u *)"\316v \017L\017$", INSERT}, /* CTRL-PageDown ="^OL^O$"*/
5001 # endif
5002 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
5003 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
5004 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
5005 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5007 /* paste, copy and cut */
5008 # ifdef FEAT_CLIPBOARD
5009 # ifdef DJGPP
5010 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
5011 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
5012 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
5013 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5014 # if 0 /* Shift-Del produces the same code as Del */
5015 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5016 # endif
5017 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5018 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
5019 # else
5020 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
5021 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
5022 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
5023 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5024 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5025 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5026 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
5027 # endif
5028 # else
5029 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
5030 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
5031 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
5032 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5033 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5034 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
5035 # endif
5036 # endif
5037 #endif
5039 #if defined(MACOS)
5040 /* Use the Standard MacOS binding. */
5041 /* paste, copy and cut */
5042 {(char_u *)"<D-v> \"*P", NORMAL},
5043 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
5044 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
5045 {(char_u *)"<D-c> \"*y", VIS_SEL},
5046 {(char_u *)"<D-x> \"*d", VIS_SEL},
5047 {(char_u *)"<Backspace> \"-d", VIS_SEL},
5048 #endif
5051 # undef VIS_SEL
5052 #endif
5055 * Set up default mappings.
5057 void
5058 init_mappings()
5060 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
5061 int i;
5063 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5064 add_map(initmappings[i].arg, initmappings[i].mode);
5065 #endif
5068 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5069 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
5071 * Add a mapping "map" for mode "mode".
5072 * Need to put string in allocated memory, because do_map() will modify it.
5074 void
5075 add_map(map, mode)
5076 char_u *map;
5077 int mode;
5079 char_u *s;
5080 char_u *cpo_save = p_cpo;
5082 p_cpo = (char_u *)""; /* Allow <> notation */
5083 s = vim_strsave(map);
5084 if (s != NULL)
5086 (void)do_map(0, s, mode, FALSE);
5087 vim_free(s);
5089 p_cpo = cpo_save;
5091 #endif