Patch 7.0.207
[MacVim/jjgod.git] / src / getchar.c
blob447805e0f59e63790dae9a6e9b6b4b851e126008
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);
932 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
934 if (offset == 0 && addlen <= typebuf.tb_off)
936 typebuf.tb_off -= addlen;
937 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
940 * Need to allocate new buffer.
941 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
942 * characters. We add some extra room to avoid having to allocate too
943 * often.
945 else
947 newoff = MAXMAPLEN + 4;
948 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
949 if (newlen < 0) /* string is getting too long */
951 EMSG(_(e_toocompl)); /* also calls flush_buffers */
952 setcursor();
953 return FAIL;
955 s1 = alloc(newlen);
956 if (s1 == NULL) /* out of memory */
957 return FAIL;
958 s2 = alloc(newlen);
959 if (s2 == NULL) /* out of memory */
961 vim_free(s1);
962 return FAIL;
964 typebuf.tb_buflen = newlen;
966 /* copy the old chars, before the insertion point */
967 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
968 (size_t)offset);
969 /* copy the new chars */
970 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
971 /* copy the old chars, after the insertion point, including the NUL at
972 * the end */
973 mch_memmove(s1 + newoff + offset + addlen,
974 typebuf.tb_buf + typebuf.tb_off + offset,
975 (size_t)(typebuf.tb_len - offset + 1));
976 if (typebuf.tb_buf != typebuf_init)
977 vim_free(typebuf.tb_buf);
978 typebuf.tb_buf = s1;
980 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
981 (size_t)offset);
982 mch_memmove(s2 + newoff + offset + addlen,
983 typebuf.tb_noremap + typebuf.tb_off + offset,
984 (size_t)(typebuf.tb_len - offset));
985 if (typebuf.tb_noremap != noremapbuf_init)
986 vim_free(typebuf.tb_noremap);
987 typebuf.tb_noremap = s2;
989 typebuf.tb_off = newoff;
991 typebuf.tb_len += addlen;
993 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
994 if (noremap == REMAP_SCRIPT)
995 val = RM_SCRIPT;
996 else if (noremap == REMAP_SKIP)
997 val = RM_ABBR;
998 else
999 val = RM_NONE;
1002 * Adjust typebuf.tb_noremap[] for the new characters:
1003 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1004 * (sometimes) not remappable
1005 * If noremap == REMAP_YES: all the new characters are mappable
1006 * If noremap > 0: "noremap" characters are not remappable, the rest
1007 * mappable
1009 if (noremap == REMAP_SKIP)
1010 nrm = 1;
1011 else if (noremap < 0)
1012 nrm = addlen;
1013 else
1014 nrm = noremap;
1015 for (i = 0; i < addlen; ++i)
1016 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1017 (--nrm >= 0) ? val : RM_YES;
1019 /* tb_maplen and tb_silent only remember the length of mapped and/or
1020 * silent mappings at the start of the buffer, assuming that a mapped
1021 * sequence doesn't result in typed characters. */
1022 if (nottyped || typebuf.tb_maplen > offset)
1023 typebuf.tb_maplen += addlen;
1024 if (silent || typebuf.tb_silent > offset)
1026 typebuf.tb_silent += addlen;
1027 cmd_silent = TRUE;
1029 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1030 typebuf.tb_no_abbr_cnt += addlen;
1032 return OK;
1036 * Put character "c" back into the typeahead buffer.
1037 * Can be used for a character obtained by vgetc() that needs to be put back.
1038 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1039 * the char.
1041 void
1042 ins_char_typebuf(c)
1043 int c;
1045 #ifdef FEAT_MBYTE
1046 char_u buf[MB_MAXBYTES];
1047 #else
1048 char_u buf[4];
1049 #endif
1050 if (IS_SPECIAL(c))
1052 buf[0] = K_SPECIAL;
1053 buf[1] = K_SECOND(c);
1054 buf[2] = K_THIRD(c);
1055 buf[3] = NUL;
1057 else
1059 #ifdef FEAT_MBYTE
1060 buf[(*mb_char2bytes)(c, buf)] = NUL;
1061 #else
1062 buf[0] = c;
1063 buf[1] = NUL;
1064 #endif
1066 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
1070 * Return TRUE if the typeahead buffer was changed (while waiting for a
1071 * character to arrive). Happens when a message was received from a client or
1072 * from feedkeys().
1073 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1074 * changed it was reallocated and the old pointer can no longer be used.
1075 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1076 * that was just added.
1079 typebuf_changed(tb_change_cnt)
1080 int tb_change_cnt; /* old value of typebuf.tb_change_cnt */
1082 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
1083 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1084 || typebuf_was_filled
1085 #endif
1090 * Return TRUE if there are no characters in the typeahead buffer that have
1091 * not been typed (result from a mapping or come from ":normal").
1094 typebuf_typed()
1096 return typebuf.tb_maplen == 0;
1099 #if defined(FEAT_VISUAL) || defined(PROTO)
1101 * Return the number of characters that are mapped (or not typed).
1104 typebuf_maplen()
1106 return typebuf.tb_maplen;
1108 #endif
1111 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1113 void
1114 del_typebuf(len, offset)
1115 int len;
1116 int offset;
1118 int i;
1120 if (len == 0)
1121 return; /* nothing to do */
1123 typebuf.tb_len -= len;
1126 * Easy case: Just increase typebuf.tb_off.
1128 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1129 >= 3 * MAXMAPLEN + 3)
1130 typebuf.tb_off += len;
1132 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1134 else
1136 i = typebuf.tb_off + offset;
1138 * Leave some extra room at the end to avoid reallocation.
1140 if (typebuf.tb_off > MAXMAPLEN)
1142 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1143 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1144 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1145 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1146 typebuf.tb_off = MAXMAPLEN;
1148 /* adjust typebuf.tb_buf (include the NUL at the end) */
1149 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1150 typebuf.tb_buf + i + len,
1151 (size_t)(typebuf.tb_len - offset + 1));
1152 /* adjust typebuf.tb_noremap[] */
1153 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1154 typebuf.tb_noremap + i + len,
1155 (size_t)(typebuf.tb_len - offset));
1158 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1160 if (typebuf.tb_maplen < offset + len)
1161 typebuf.tb_maplen = offset;
1162 else
1163 typebuf.tb_maplen -= len;
1165 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1167 if (typebuf.tb_silent < offset + len)
1168 typebuf.tb_silent = offset;
1169 else
1170 typebuf.tb_silent -= len;
1172 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1174 if (typebuf.tb_no_abbr_cnt < offset + len)
1175 typebuf.tb_no_abbr_cnt = offset;
1176 else
1177 typebuf.tb_no_abbr_cnt -= len;
1180 #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1181 /* Reset the flag that text received from a client or from feedkeys()
1182 * was inserted in the typeahead buffer. */
1183 typebuf_was_filled = FALSE;
1184 #endif
1185 if (++typebuf.tb_change_cnt == 0)
1186 typebuf.tb_change_cnt = 1;
1190 * Write typed characters to script file.
1191 * If recording is on put the character in the recordbuffer.
1193 static void
1194 gotchars(s, len)
1195 char_u *s;
1196 int len;
1198 int c;
1199 char_u buf[2];
1201 /* remember how many chars were last recorded */
1202 if (Recording)
1203 last_recorded_len += len;
1205 buf[1] = NUL;
1206 while (len--)
1208 /* Handle one byte at a time; no translation to be done. */
1209 c = *s++;
1210 updatescript(c);
1212 if (Recording)
1214 buf[0] = c;
1215 add_buff(&recordbuff, buf, 1L);
1218 may_sync_undo();
1220 #ifdef FEAT_EVAL
1221 /* output "debug mode" message next time in debug mode */
1222 debug_did_msg = FALSE;
1223 #endif
1225 /* Since characters have been typed, consider the following to be in
1226 * another mapping. Search string will be kept in history. */
1227 ++maptick;
1231 * Sync undo. Called when typed characters are obtained from the typeahead
1232 * buffer, or when a menu is used.
1233 * Do not sync:
1234 * - In Insert mode, unless cursor key has been used.
1235 * - While reading a script file.
1236 * - When no_u_sync is non-zero.
1238 static void
1239 may_sync_undo()
1241 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
1242 && scriptin[curscript] == NULL)
1243 u_sync(FALSE);
1247 * Make "typebuf" empty and allocate new buffers.
1248 * Returns FAIL when out of memory.
1251 alloc_typebuf()
1253 typebuf.tb_buf = alloc(TYPELEN_INIT);
1254 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1255 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1257 free_typebuf();
1258 return FAIL;
1260 typebuf.tb_buflen = TYPELEN_INIT;
1261 typebuf.tb_off = 0;
1262 typebuf.tb_len = 0;
1263 typebuf.tb_maplen = 0;
1264 typebuf.tb_silent = 0;
1265 typebuf.tb_no_abbr_cnt = 0;
1266 if (++typebuf.tb_change_cnt == 0)
1267 typebuf.tb_change_cnt = 1;
1268 return OK;
1272 * Free the buffers of "typebuf".
1274 void
1275 free_typebuf()
1277 vim_free(typebuf.tb_buf);
1278 vim_free(typebuf.tb_noremap);
1282 * When doing ":so! file", the current typeahead needs to be saved, and
1283 * restored when "file" has been read completely.
1285 static typebuf_T saved_typebuf[NSCRIPT];
1288 save_typebuf()
1290 init_typebuf();
1291 saved_typebuf[curscript] = typebuf;
1292 /* If out of memory: restore typebuf and close file. */
1293 if (alloc_typebuf() == FAIL)
1295 closescript();
1296 return FAIL;
1298 return OK;
1301 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1304 * Save all three kinds of typeahead, so that the user must type at a prompt.
1306 void
1307 save_typeahead(tp)
1308 tasave_T *tp;
1310 tp->save_typebuf = typebuf;
1311 tp->typebuf_valid = (alloc_typebuf() == OK);
1312 if (!tp->typebuf_valid)
1313 typebuf = tp->save_typebuf;
1315 tp->save_stuffbuff = stuffbuff;
1316 stuffbuff.bh_first.b_next = NULL;
1317 # ifdef USE_INPUT_BUF
1318 tp->save_inputbuf = get_input_buf();
1319 # endif
1323 * Restore the typeahead to what it was before calling save_typeahead().
1324 * The allocated memory is freed, can only be called once!
1326 void
1327 restore_typeahead(tp)
1328 tasave_T *tp;
1330 if (tp->typebuf_valid)
1332 free_typebuf();
1333 typebuf = tp->save_typebuf;
1336 free_buff(&stuffbuff);
1337 stuffbuff = tp->save_stuffbuff;
1338 # ifdef USE_INPUT_BUF
1339 set_input_buf(tp->save_inputbuf);
1340 # endif
1342 #endif
1345 * Open a new script file for the ":source!" command.
1347 void
1348 openscript(name, directly)
1349 char_u *name;
1350 int directly; /* when TRUE execute directly */
1352 if (curscript + 1 == NSCRIPT)
1354 EMSG(_(e_nesting));
1355 return;
1358 if (scriptin[curscript] != NULL) /* already reading script */
1359 ++curscript;
1360 /* use NameBuff for expanded name */
1361 expand_env(name, NameBuff, MAXPATHL);
1362 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1364 EMSG2(_(e_notopen), name);
1365 if (curscript)
1366 --curscript;
1367 return;
1369 if (save_typebuf() == FAIL)
1370 return;
1373 * Execute the commands from the file right now when using ":source!"
1374 * after ":global" or ":argdo" or in a loop. Also when another command
1375 * follows. This means the display won't be updated. Don't do this
1376 * always, "make test" would fail.
1378 if (directly)
1380 oparg_T oa;
1381 int oldcurscript;
1382 int save_State = State;
1383 int save_restart_edit = restart_edit;
1384 int save_insertmode = p_im;
1385 int save_finish_op = finish_op;
1386 int save_msg_scroll = msg_scroll;
1388 State = NORMAL;
1389 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1390 restart_edit = 0; /* don't go to Insert mode */
1391 p_im = FALSE; /* don't use 'insertmode' */
1392 clear_oparg(&oa);
1393 finish_op = FALSE;
1395 oldcurscript = curscript;
1398 update_topline_cursor(); /* update cursor position and topline */
1399 normal_cmd(&oa, FALSE); /* execute one command */
1400 vpeekc(); /* check for end of file */
1402 while (scriptin[oldcurscript] != NULL);
1404 State = save_State;
1405 msg_scroll = save_msg_scroll;
1406 restart_edit = save_restart_edit;
1407 p_im = save_insertmode;
1408 finish_op = save_finish_op;
1413 * Close the currently active input script.
1415 static void
1416 closescript()
1418 free_typebuf();
1419 typebuf = saved_typebuf[curscript];
1421 fclose(scriptin[curscript]);
1422 scriptin[curscript] = NULL;
1423 if (curscript > 0)
1424 --curscript;
1427 #if defined(EXITFREE) || defined(PROTO)
1428 void
1429 close_all_scripts()
1431 while (scriptin[0] != NULL)
1432 closescript();
1434 #endif
1436 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1438 * Return TRUE when reading keys from a script file.
1441 using_script()
1443 return scriptin[curscript] != NULL;
1445 #endif
1448 * This function is called just before doing a blocking wait. Thus after
1449 * waiting 'updatetime' for a character to arrive.
1451 void
1452 before_blocking()
1454 updatescript(0);
1455 #ifdef FEAT_EVAL
1456 if (may_garbage_collect)
1457 garbage_collect();
1458 #endif
1462 * updatescipt() is called when a character can be written into the script file
1463 * or when we have waited some time for a character (c == 0)
1465 * All the changed memfiles are synced if c == 0 or when the number of typed
1466 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1468 void
1469 updatescript(c)
1470 int c;
1472 static int count = 0;
1474 if (c && scriptout)
1475 putc(c, scriptout);
1476 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1478 ml_sync_all(c == 0, TRUE);
1479 count = 0;
1483 #define KL_PART_KEY -1 /* keylen value for incomplete key-code */
1484 #define KL_PART_MAP -2 /* keylen value for incomplete mapping */
1486 static int old_char = -1; /* character put back by vungetc() */
1487 static int old_mod_mask; /* mod_mask for ungotten character */
1490 * Get the next input character.
1491 * Can return a special key or a multi-byte character.
1492 * Can return NUL when called recursively, use safe_vgetc() if that's not
1493 * wanted.
1494 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1495 * Collects the bytes of a multibyte character into the whole character.
1496 * Returns the modifers in the global "mod_mask".
1499 vgetc()
1501 int c, c2;
1502 #ifdef FEAT_MBYTE
1503 int n;
1504 char_u buf[MB_MAXBYTES];
1505 int i;
1506 #endif
1508 #ifdef FEAT_EVAL
1509 /* Do garbage collection when garbagecollect() was called previously and
1510 * we are now at the toplevel. */
1511 if (may_garbage_collect && want_garbage_collect)
1512 garbage_collect();
1513 #endif
1516 * If a character was put back with vungetc, it was already processed.
1517 * Return it directly.
1519 if (old_char != -1)
1521 c = old_char;
1522 old_char = -1;
1523 mod_mask = old_mod_mask;
1525 else
1527 mod_mask = 0x0;
1528 last_recorded_len = 0;
1529 for (;;) /* this is done twice if there are modifiers */
1531 if (mod_mask) /* no mapping after modifier has been read */
1533 ++no_mapping;
1534 ++allow_keys;
1536 c = vgetorpeek(TRUE);
1537 if (mod_mask)
1539 --no_mapping;
1540 --allow_keys;
1543 /* Get two extra bytes for special keys */
1544 if (c == K_SPECIAL
1545 #ifdef FEAT_GUI
1546 || c == CSI
1547 #endif
1550 int save_allow_keys = allow_keys;
1552 ++no_mapping;
1553 allow_keys = 0; /* make sure BS is not found */
1554 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1555 c = vgetorpeek(TRUE);
1556 --no_mapping;
1557 allow_keys = save_allow_keys;
1558 if (c2 == KS_MODIFIER)
1560 mod_mask = c;
1561 continue;
1563 c = TO_SPECIAL(c2, c);
1565 #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1566 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1567 * know that a menu was torn off */
1568 if (c == K_TEAROFF)
1570 char_u name[200];
1571 int i;
1573 /* get menu path, it ends with a <CR> */
1574 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1576 name[i] = c;
1577 if (i < 199)
1578 ++i;
1580 name[i] = NUL;
1581 gui_make_tearoff(name);
1582 continue;
1584 #endif
1585 #if defined(FEAT_GUI) && defined(HAVE_GTK2) && defined(FEAT_MENU)
1586 /* GTK: <F10> normally selects the menu, but it's passed until
1587 * here to allow mapping it. Intercept and invoke the GTK
1588 * behavior if it's not mapped. */
1589 if (c == K_F10 && gui.menubar != NULL)
1591 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1592 continue;
1594 #endif
1596 #ifdef FEAT_GUI
1597 /* Translate K_CSI to CSI. The special key is only used to avoid
1598 * it being recognized as the start of a special key. */
1599 if (c == K_CSI)
1600 c = CSI;
1601 #endif
1603 #ifdef MSDOS
1605 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1606 * Delete the 3 here.
1608 else if (c == K_NUL && vpeekc() == 3)
1609 (void)vgetorpeek(TRUE);
1610 #endif
1612 /* a keypad or special function key was not mapped, use it like
1613 * its ASCII equivalent */
1614 switch (c)
1616 case K_KPLUS: c = '+'; break;
1617 case K_KMINUS: c = '-'; break;
1618 case K_KDIVIDE: c = '/'; break;
1619 case K_KMULTIPLY: c = '*'; break;
1620 case K_KENTER: c = CAR; break;
1621 case K_KPOINT:
1622 #ifdef WIN32
1623 /* Can be either '.' or a ',', *
1624 * depending on the type of keypad. */
1625 c = MapVirtualKey(VK_DECIMAL, 2); break;
1626 #else
1627 c = '.'; break;
1628 #endif
1629 case K_K0: c = '0'; break;
1630 case K_K1: c = '1'; break;
1631 case K_K2: c = '2'; break;
1632 case K_K3: c = '3'; break;
1633 case K_K4: c = '4'; break;
1634 case K_K5: c = '5'; break;
1635 case K_K6: c = '6'; break;
1636 case K_K7: c = '7'; break;
1637 case K_K8: c = '8'; break;
1638 case K_K9: c = '9'; break;
1640 case K_XHOME:
1641 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1643 c = K_S_HOME;
1644 mod_mask = 0;
1646 else if (mod_mask == MOD_MASK_CTRL)
1648 c = K_C_HOME;
1649 mod_mask = 0;
1651 else
1652 c = K_HOME;
1653 break;
1654 case K_XEND:
1655 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1657 c = K_S_END;
1658 mod_mask = 0;
1660 else if (mod_mask == MOD_MASK_CTRL)
1662 c = K_C_END;
1663 mod_mask = 0;
1665 else
1666 c = K_END;
1667 break;
1669 case K_XUP: c = K_UP; break;
1670 case K_XDOWN: c = K_DOWN; break;
1671 case K_XLEFT: c = K_LEFT; break;
1672 case K_XRIGHT: c = K_RIGHT; break;
1675 #ifdef FEAT_MBYTE
1676 /* For a multi-byte character get all the bytes and return the
1677 * converted character.
1678 * Note: This will loop until enough bytes are received!
1680 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1682 ++no_mapping;
1683 buf[0] = c;
1684 for (i = 1; i < n; ++i)
1686 buf[i] = vgetorpeek(TRUE);
1687 if (buf[i] == K_SPECIAL
1688 #ifdef FEAT_GUI
1689 || buf[i] == CSI
1690 #endif
1693 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1694 * which represents a K_SPECIAL (0x80),
1695 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1696 * a CSI (0x9B),
1697 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1698 c = vgetorpeek(TRUE);
1699 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1700 buf[i] = CSI;
1703 --no_mapping;
1704 c = (*mb_ptr2char)(buf);
1706 #endif
1708 break;
1712 #ifdef FEAT_EVAL
1714 * In the main loop "may_garbage_collect" can be set to do garbage
1715 * collection in the first next vgetc(). It's disabled after that to
1716 * avoid internally used Lists and Dicts to be freed.
1718 may_garbage_collect = FALSE;
1719 #endif
1721 return c;
1725 * Like vgetc(), but never return a NUL when called recursively, get a key
1726 * directly from the user (ignoring typeahead).
1729 safe_vgetc()
1731 int c;
1733 c = vgetc();
1734 if (c == NUL)
1735 c = get_keystroke();
1736 return c;
1740 * Check if a character is available, such that vgetc() will not block.
1741 * If the next character is a special character or multi-byte, the returned
1742 * character is not valid!.
1745 vpeekc()
1747 if (old_char != -1)
1748 return old_char;
1749 return vgetorpeek(FALSE);
1752 #if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1754 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1755 * codes.
1758 vpeekc_nomap()
1760 int c;
1762 ++no_mapping;
1763 ++allow_keys;
1764 c = vpeekc();
1765 --no_mapping;
1766 --allow_keys;
1767 return c;
1769 #endif
1771 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1773 * Check if any character is available, also half an escape sequence.
1774 * Trick: when no typeahead found, but there is something in the typeahead
1775 * buffer, it must be an ESC that is recognized as the start of a key code.
1778 vpeekc_any()
1780 int c;
1782 c = vpeekc();
1783 if (c == NUL && typebuf.tb_len > 0)
1784 c = ESC;
1785 return c;
1787 #endif
1790 * Call vpeekc() without causing anything to be mapped.
1791 * Return TRUE if a character is available, FALSE otherwise.
1794 char_avail()
1796 int retval;
1798 ++no_mapping;
1799 retval = vpeekc();
1800 --no_mapping;
1801 return (retval != NUL);
1804 void
1805 vungetc(c) /* unget one character (can only be done once!) */
1806 int c;
1808 old_char = c;
1809 old_mod_mask = mod_mask;
1813 * get a character:
1814 * 1. from the stuffbuffer
1815 * This is used for abbreviated commands like "D" -> "d$".
1816 * Also used to redo a command for ".".
1817 * 2. from the typeahead buffer
1818 * Stores text obtained previously but not used yet.
1819 * Also stores the result of mappings.
1820 * Also used for the ":normal" command.
1821 * 3. from the user
1822 * This may do a blocking wait if "advance" is TRUE.
1824 * if "advance" is TRUE (vgetc()):
1825 * really get the character.
1826 * KeyTyped is set to TRUE in the case the user typed the key.
1827 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1828 * if "advance" is FALSE (vpeekc()):
1829 * just look whether there is a character available.
1831 * When "no_mapping" is zero, checks for mappings in the current mode.
1832 * Only returns one byte (of a multi-byte character).
1833 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1835 static int
1836 vgetorpeek(advance)
1837 int advance;
1839 int c, c1;
1840 int keylen;
1841 char_u *s;
1842 mapblock_T *mp;
1843 #ifdef FEAT_LOCALMAP
1844 mapblock_T *mp2;
1845 #endif
1846 mapblock_T *mp_match;
1847 int mp_match_len = 0;
1848 int timedout = FALSE; /* waited for more than 1 second
1849 for mapping to complete */
1850 int mapdepth = 0; /* check for recursive mapping */
1851 int mode_deleted = FALSE; /* set when mode has been deleted */
1852 int local_State;
1853 int mlen;
1854 int max_mlen;
1855 int i;
1856 #ifdef FEAT_CMDL_INFO
1857 int new_wcol, new_wrow;
1858 #endif
1859 #ifdef FEAT_GUI
1860 # ifdef FEAT_MENU
1861 int idx;
1862 # endif
1863 int shape_changed = FALSE; /* adjusted cursor shape */
1864 #endif
1865 int n;
1866 #ifdef FEAT_LANGMAP
1867 int nolmaplen;
1868 #endif
1869 int old_wcol, old_wrow;
1870 int wait_tb_len;
1873 * This function doesn't work very well when called recursively. This may
1874 * happen though, because of:
1875 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1876 * there is a character available, which calls this function. In that
1877 * case we must return NUL, to indicate no character is available.
1878 * 2. A GUI callback function writes to the screen, causing a
1879 * wait_return().
1880 * Using ":normal" can also do this, but it saves the typeahead buffer,
1881 * thus it should be OK. But don't get a key from the user then.
1883 if (vgetc_busy > 0
1884 #ifdef FEAT_EX_EXTRA
1885 && ex_normal_busy == 0
1886 #endif
1888 return NUL;
1890 local_State = get_real_state();
1892 ++vgetc_busy;
1894 if (advance)
1895 KeyStuffed = FALSE;
1897 init_typebuf();
1898 start_stuff();
1899 if (advance && typebuf.tb_maplen == 0)
1900 Exec_reg = FALSE;
1904 * get a character: 1. from the stuffbuffer
1906 if (typeahead_char != 0)
1908 c = typeahead_char;
1909 if (advance)
1910 typeahead_char = 0;
1912 else
1913 c = read_stuff(advance);
1914 if (c != NUL && !got_int)
1916 if (advance)
1918 /* KeyTyped = FALSE; When the command that stuffed something
1919 * was typed, behave like the stuffed command was typed.
1920 * needed for CTRL-W CTRl-] to open a fold, for example. */
1921 KeyStuffed = TRUE;
1923 if (typebuf.tb_no_abbr_cnt == 0)
1924 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1926 else
1929 * Loop until we either find a matching mapped key, or we
1930 * are sure that it is not a mapped key.
1931 * If a mapped key sequence is found we go back to the start to
1932 * try re-mapping.
1934 for (;;)
1937 * ui_breakcheck() is slow, don't use it too often when
1938 * inside a mapping. But call it each time for typed
1939 * characters.
1941 if (typebuf.tb_maplen)
1942 line_breakcheck();
1943 else
1944 ui_breakcheck(); /* check for CTRL-C */
1945 keylen = 0;
1946 if (got_int)
1948 /* flush all input */
1949 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
1950 typebuf.tb_change_cnt);
1952 * If inchar() returns TRUE (script file was active) or we
1953 * are inside a mapping, get out of insert mode.
1954 * Otherwise we behave like having gotten a CTRL-C.
1955 * As a result typing CTRL-C in insert mode will
1956 * really insert a CTRL-C.
1958 if ((c || typebuf.tb_maplen)
1959 && (State & (INSERT + CMDLINE)))
1960 c = ESC;
1961 else
1962 c = Ctrl_C;
1963 flush_buffers(TRUE); /* flush all typeahead */
1965 if (advance)
1967 /* Also record this character, it might be needed to
1968 * get out of Insert mode. */
1969 *typebuf.tb_buf = c;
1970 gotchars(typebuf.tb_buf, 1);
1972 cmd_silent = FALSE;
1974 break;
1976 else if (typebuf.tb_len > 0)
1979 * Check for a mappable key sequence.
1980 * Walk through one maphash[] list until we find an
1981 * entry that matches.
1983 * Don't look for mappings if:
1984 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
1985 * - maphash_valid not set: no mappings present.
1986 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
1987 * - in insert or cmdline mode and 'paste' option set
1988 * - waiting for "hit return to continue" and CR or SPACE
1989 * typed
1990 * - waiting for a char with --more--
1991 * - in Ctrl-X mode, and we get a valid char for that mode
1993 mp = NULL;
1994 max_mlen = 0;
1995 c1 = typebuf.tb_buf[typebuf.tb_off];
1996 if (no_mapping == 0 && maphash_valid
1997 && (no_zero_mapping == 0 || c1 != '0')
1998 && (typebuf.tb_maplen == 0
1999 || (p_remap
2000 && (typebuf.tb_noremap[typebuf.tb_off]
2001 & (RM_NONE|RM_ABBR)) == 0))
2002 && !(p_paste && (State & (INSERT + CMDLINE)))
2003 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2004 && State != ASKMORE
2005 && State != CONFIRM
2006 #ifdef FEAT_INS_EXPAND
2007 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
2008 || ((compl_cont_status & CONT_LOCAL)
2009 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2010 #endif
2013 #ifdef FEAT_LANGMAP
2014 if (c1 == K_SPECIAL)
2015 nolmaplen = 2;
2016 else
2018 LANGMAP_ADJUST(c1, TRUE);
2019 nolmaplen = 0;
2021 #endif
2022 #ifdef FEAT_LOCALMAP
2023 /* First try buffer-local mappings. */
2024 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2025 mp2 = maphash[MAP_HASH(local_State, c1)];
2026 if (mp == NULL)
2028 mp = mp2;
2029 mp2 = NULL;
2031 #else
2032 mp = maphash[MAP_HASH(local_State, c1)];
2033 #endif
2035 * Loop until a partly matching mapping is found or
2036 * all (local) mappings have been checked.
2037 * The longest full match is remembered in "mp_match".
2038 * A full match is only accepted if there is no partly
2039 * match, so "aa" and "aaa" can both be mapped.
2041 mp_match = NULL;
2042 mp_match_len = 0;
2043 for ( ; mp != NULL;
2044 #ifdef FEAT_LOCALMAP
2045 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2046 #endif
2047 (mp = mp->m_next))
2050 * Only consider an entry if the first character
2051 * matches and it is for the current state.
2052 * Skip ":lmap" mappings if keys were mapped.
2054 if (mp->m_keys[0] == c1
2055 && (mp->m_mode & local_State)
2056 && ((mp->m_mode & LANGMAP) == 0
2057 || typebuf.tb_maplen == 0))
2059 #ifdef FEAT_LANGMAP
2060 int nomap = nolmaplen;
2061 int c2;
2062 #endif
2063 /* find the match length of this mapping */
2064 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2066 #ifdef FEAT_LANGMAP
2067 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2068 if (nomap > 0)
2069 --nomap;
2070 else if (c2 == K_SPECIAL)
2071 nomap = 2;
2072 else
2073 LANGMAP_ADJUST(c2, TRUE);
2074 if (mp->m_keys[mlen] != c2)
2075 #else
2076 if (mp->m_keys[mlen] !=
2077 typebuf.tb_buf[typebuf.tb_off + mlen])
2078 #endif
2079 break;
2082 #ifdef FEAT_MBYTE
2083 /* Don't allow mapping the first byte(s) of a
2084 * multi-byte char. Happens when mapping
2085 * <M-a> and then changing 'encoding'. */
2086 if (has_mbyte && MB_BYTE2LEN(c1)
2087 > (*mb_ptr2len)(mp->m_keys))
2088 mlen = 0;
2089 #endif
2091 * Check an entry whether it matches.
2092 * - Full match: mlen == keylen
2093 * - Partly match: mlen == typebuf.tb_len
2095 keylen = mp->m_keylen;
2096 if (mlen == keylen
2097 || (mlen == typebuf.tb_len
2098 && typebuf.tb_len < keylen))
2101 * If only script-local mappings are
2102 * allowed, check if the mapping starts
2103 * with K_SNR.
2105 s = typebuf.tb_noremap + typebuf.tb_off;
2106 if (*s == RM_SCRIPT
2107 && (mp->m_keys[0] != K_SPECIAL
2108 || mp->m_keys[1] != KS_EXTRA
2109 || mp->m_keys[2]
2110 != (int)KE_SNR))
2111 continue;
2113 * If one of the typed keys cannot be
2114 * remapped, skip the entry.
2116 for (n = mlen; --n >= 0; )
2117 if (*s++ & (RM_NONE|RM_ABBR))
2118 break;
2119 if (n >= 0)
2120 continue;
2122 if (keylen > typebuf.tb_len)
2124 if (!timedout)
2126 /* break at a partly match */
2127 keylen = KL_PART_MAP;
2128 break;
2131 else if (keylen > mp_match_len)
2133 /* found a longer match */
2134 mp_match = mp;
2135 mp_match_len = keylen;
2138 else
2139 /* No match; may have to check for
2140 * termcode at next character. */
2141 if (max_mlen < mlen)
2142 max_mlen = mlen;
2146 /* If no partly match found, use the longest full
2147 * match. */
2148 if (keylen != KL_PART_MAP)
2150 mp = mp_match;
2151 keylen = mp_match_len;
2155 /* Check for match with 'pastetoggle' */
2156 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2158 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2159 ++mlen)
2160 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2161 + mlen])
2162 break;
2163 if (p_pt[mlen] == NUL) /* match */
2165 /* write chars to script file(s) */
2166 if (mlen > typebuf.tb_maplen)
2167 gotchars(typebuf.tb_buf + typebuf.tb_off
2168 + typebuf.tb_maplen,
2169 mlen - typebuf.tb_maplen);
2171 del_typebuf(mlen, 0); /* remove the chars */
2172 set_option_value((char_u *)"paste",
2173 (long)!p_paste, NULL, 0);
2174 if (!(State & INSERT))
2176 msg_col = 0;
2177 msg_row = Rows - 1;
2178 msg_clr_eos(); /* clear ruler */
2180 showmode();
2181 setcursor();
2182 continue;
2184 /* Need more chars for partly match. */
2185 if (mlen == typebuf.tb_len)
2186 keylen = KL_PART_MAP;
2187 else if (max_mlen < mlen)
2188 /* no match, may have to check for termcode at
2189 * next character */
2190 max_mlen = mlen + 1;
2193 if ((mp == NULL || max_mlen >= mp_match_len)
2194 && keylen != KL_PART_MAP)
2197 * When no matching mapping found or found a
2198 * non-matching mapping that matches at least what the
2199 * matching mapping matched:
2200 * Check if we have a terminal code, when:
2201 * mapping is allowed,
2202 * keys have not been mapped,
2203 * and not an ESC sequence, not in insert mode or
2204 * p_ek is on,
2205 * and when not timed out,
2207 if ((no_mapping == 0 || allow_keys != 0)
2208 && (typebuf.tb_maplen == 0
2209 || (p_remap && typebuf.tb_noremap[
2210 typebuf.tb_off] == RM_YES))
2211 && !timedout)
2213 keylen = check_termcode(max_mlen + 1, NULL, 0);
2216 * When getting a partial match, but the last
2217 * characters were not typed, don't wait for a
2218 * typed character to complete the termcode.
2219 * This helps a lot when a ":normal" command ends
2220 * in an ESC.
2222 if (keylen < 0
2223 && typebuf.tb_len == typebuf.tb_maplen)
2224 keylen = 0;
2226 else
2227 keylen = 0;
2228 if (keylen == 0) /* no matching terminal code */
2230 #ifdef AMIGA /* check for window bounds report */
2231 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2232 typebuf.tb_off] & 0xff) == CSI)
2234 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2235 s < typebuf.tb_buf + typebuf.tb_off
2236 + typebuf.tb_len
2237 && (VIM_ISDIGIT(*s) || *s == ';'
2238 || *s == ' ');
2239 ++s)
2241 if (*s == 'r' || *s == '|') /* found one */
2243 del_typebuf((int)(s + 1 -
2244 (typebuf.tb_buf + typebuf.tb_off)), 0);
2245 /* get size and redraw screen */
2246 shell_resized();
2247 continue;
2249 if (*s == NUL) /* need more characters */
2250 keylen = KL_PART_KEY;
2252 if (keylen >= 0)
2253 #endif
2254 /* When there was a matching mapping and no
2255 * termcode could be replaced after another one,
2256 * use that mapping. */
2257 if (mp == NULL)
2260 * get a character: 2. from the typeahead buffer
2262 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2263 if (advance) /* remove chars from tb_buf */
2265 cmd_silent = (typebuf.tb_silent > 0);
2266 if (typebuf.tb_maplen > 0)
2267 KeyTyped = FALSE;
2268 else
2270 KeyTyped = TRUE;
2271 /* write char to script file(s) */
2272 gotchars(typebuf.tb_buf
2273 + typebuf.tb_off, 1);
2275 KeyNoremap = typebuf.tb_noremap[
2276 typebuf.tb_off];
2277 del_typebuf(1, 0);
2279 break; /* got character, break for loop */
2282 if (keylen > 0) /* full matching terminal code */
2284 #if defined(FEAT_GUI) && defined(FEAT_MENU)
2285 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2286 && typebuf.tb_buf[typebuf.tb_off + 1]
2287 == KS_MENU)
2290 * Using a menu may cause a break in undo!
2291 * It's like using gotchars(), but without
2292 * recording or writing to a script file.
2294 may_sync_undo();
2295 del_typebuf(3, 0);
2296 idx = get_menu_index(current_menu, local_State);
2297 if (idx != MENU_INDEX_INVALID)
2299 # ifdef FEAT_VISUAL
2301 * In Select mode and a Visual mode menu
2302 * is used: Switch to Visual mode
2303 * temporarily. Append K_SELECT to switch
2304 * back to Select mode.
2306 if (VIsual_active && VIsual_select
2307 && (current_menu->modes & VISUAL))
2309 VIsual_select = FALSE;
2310 (void)ins_typebuf(K_SELECT_STRING,
2311 REMAP_NONE, 0, TRUE, FALSE);
2313 # endif
2314 ins_typebuf(current_menu->strings[idx],
2315 current_menu->noremap[idx],
2316 0, TRUE,
2317 current_menu->silent[idx]);
2320 #endif /* FEAT_GUI */
2321 continue; /* try mapping again */
2324 /* Partial match: get some more characters. When a
2325 * matching mapping was found use that one. */
2326 if (mp == NULL || keylen < 0)
2327 keylen = KL_PART_KEY;
2328 else
2329 keylen = mp_match_len;
2332 /* complete match */
2333 if (keylen >= 0 && keylen <= typebuf.tb_len)
2335 /* write chars to script file(s) */
2336 if (keylen > typebuf.tb_maplen)
2337 gotchars(typebuf.tb_buf + typebuf.tb_off
2338 + typebuf.tb_maplen,
2339 keylen - typebuf.tb_maplen);
2341 cmd_silent = (typebuf.tb_silent > 0);
2342 del_typebuf(keylen, 0); /* remove the mapped keys */
2345 * Put the replacement string in front of mapstr.
2346 * The depth check catches ":map x y" and ":map y x".
2348 if (++mapdepth >= p_mmd)
2350 EMSG(_("E223: recursive mapping"));
2351 if (State & CMDLINE)
2352 redrawcmdline();
2353 else
2354 setcursor();
2355 flush_buffers(FALSE);
2356 mapdepth = 0; /* for next one */
2357 c = -1;
2358 break;
2361 #ifdef FEAT_VISUAL
2363 * In Select mode and a Visual mode mapping is used:
2364 * Switch to Visual mode temporarily. Append K_SELECT
2365 * to switch back to Select mode.
2367 if (VIsual_active && VIsual_select
2368 && (mp->m_mode & VISUAL))
2370 VIsual_select = FALSE;
2371 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2372 0, TRUE, FALSE);
2374 #endif
2376 #ifdef FEAT_EVAL
2378 * Handle ":map <expr>": evaluate the {rhs} as an
2379 * expression. Save and restore the typeahead so that
2380 * getchar() can be used. Also save and restore the
2381 * command line for "normal :".
2383 if (mp->m_expr)
2385 tasave_T tabuf;
2386 int save_vgetc_busy = vgetc_busy;
2388 save_typeahead(&tabuf);
2389 if (tabuf.typebuf_valid)
2391 vgetc_busy = 0;
2392 s = eval_map_expr(mp->m_str);
2393 vgetc_busy = save_vgetc_busy;
2395 else
2396 s = NULL;
2397 restore_typeahead(&tabuf);
2399 else
2400 #endif
2401 s = mp->m_str;
2404 * Insert the 'to' part in the typebuf.tb_buf.
2405 * If 'from' field is the same as the start of the
2406 * 'to' field, don't remap the first character (but do
2407 * allow abbreviations).
2408 * If m_noremap is set, don't remap the whole 'to'
2409 * part.
2411 if (s == NULL)
2412 i = FAIL;
2413 else
2415 i = ins_typebuf(s,
2416 mp->m_noremap != REMAP_YES
2417 ? mp->m_noremap
2418 : STRNCMP(s, mp->m_keys,
2419 (size_t)keylen) != 0
2420 ? REMAP_YES : REMAP_SKIP,
2421 0, TRUE, cmd_silent || mp->m_silent);
2422 #ifdef FEAT_EVAL
2423 if (mp->m_expr)
2424 vim_free(s);
2425 #endif
2427 if (i == FAIL)
2429 c = -1;
2430 break;
2432 continue;
2437 * get a character: 3. from the user - handle <Esc> in Insert mode
2440 * special case: if we get an <ESC> in insert mode and there
2441 * are no more characters at once, we pretend to go out of
2442 * insert mode. This prevents the one second delay after
2443 * typing an <ESC>. If we get something after all, we may
2444 * have to redisplay the mode. That the cursor is in the wrong
2445 * place does not matter.
2447 c = 0;
2448 #ifdef FEAT_CMDL_INFO
2449 new_wcol = curwin->w_wcol;
2450 new_wrow = curwin->w_wrow;
2451 #endif
2452 if ( advance
2453 && typebuf.tb_len == 1
2454 && typebuf.tb_buf[typebuf.tb_off] == ESC
2455 && !no_mapping
2456 #ifdef FEAT_EX_EXTRA
2457 && ex_normal_busy == 0
2458 #endif
2459 && typebuf.tb_maplen == 0
2460 && (State & INSERT)
2461 && (p_timeout || (keylen == KL_PART_KEY && p_ttimeout))
2462 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2463 + typebuf.tb_len, 3, 25L,
2464 typebuf.tb_change_cnt)) == 0)
2466 colnr_T col = 0, vcol;
2467 char_u *ptr;
2469 if (mode_displayed)
2471 unshowmode(TRUE);
2472 mode_deleted = TRUE;
2474 #ifdef FEAT_GUI
2475 /* may show different cursor shape */
2476 if (gui.in_use)
2478 int save_State;
2480 save_State = State;
2481 State = NORMAL;
2482 gui_update_cursor(TRUE, FALSE);
2483 State = save_State;
2484 shape_changed = TRUE;
2486 #endif
2487 validate_cursor();
2488 old_wcol = curwin->w_wcol;
2489 old_wrow = curwin->w_wrow;
2491 /* move cursor left, if possible */
2492 if (curwin->w_cursor.col != 0)
2494 if (curwin->w_wcol > 0)
2496 if (did_ai)
2499 * We are expecting to truncate the trailing
2500 * white-space, so find the last non-white
2501 * character -- webb
2503 col = vcol = curwin->w_wcol = 0;
2504 ptr = ml_get_curline();
2505 while (col < curwin->w_cursor.col)
2507 if (!vim_iswhite(ptr[col]))
2508 curwin->w_wcol = vcol;
2509 vcol += lbr_chartabsize(ptr + col,
2510 (colnr_T)vcol);
2511 #ifdef FEAT_MBYTE
2512 if (has_mbyte)
2513 col += (*mb_ptr2len)(ptr + col);
2514 else
2515 #endif
2516 ++col;
2518 curwin->w_wrow = curwin->w_cline_row
2519 + curwin->w_wcol / W_WIDTH(curwin);
2520 curwin->w_wcol %= W_WIDTH(curwin);
2521 curwin->w_wcol += curwin_col_off();
2522 #ifdef FEAT_MBYTE
2523 col = 0; /* no correction needed */
2524 #endif
2526 else
2528 --curwin->w_wcol;
2529 #ifdef FEAT_MBYTE
2530 col = curwin->w_cursor.col - 1;
2531 #endif
2534 else if (curwin->w_p_wrap && curwin->w_wrow)
2536 --curwin->w_wrow;
2537 curwin->w_wcol = W_WIDTH(curwin) - 1;
2538 #ifdef FEAT_MBYTE
2539 col = curwin->w_cursor.col - 1;
2540 #endif
2542 #ifdef FEAT_MBYTE
2543 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2545 /* Correct when the cursor is on the right halve
2546 * of a double-wide character. */
2547 ptr = ml_get_curline();
2548 col -= (*mb_head_off)(ptr, ptr + col);
2549 if ((*mb_ptr2cells)(ptr + col) > 1)
2550 --curwin->w_wcol;
2552 #endif
2554 setcursor();
2555 out_flush();
2556 #ifdef FEAT_CMDL_INFO
2557 new_wcol = curwin->w_wcol;
2558 new_wrow = curwin->w_wrow;
2559 #endif
2560 curwin->w_wcol = old_wcol;
2561 curwin->w_wrow = old_wrow;
2563 if (c < 0)
2564 continue; /* end of input script reached */
2565 typebuf.tb_len += c;
2567 /* buffer full, don't map */
2568 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2570 timedout = TRUE;
2571 continue;
2574 #ifdef FEAT_EX_EXTRA
2575 if (ex_normal_busy > 0)
2577 # ifdef FEAT_CMDWIN
2578 static int tc = 0;
2579 # endif
2581 /* No typeahead left and inside ":normal". Must return
2582 * something to avoid getting stuck. When an incomplete
2583 * mapping is present, behave like it timed out. */
2584 if (typebuf.tb_len > 0)
2586 timedout = TRUE;
2587 continue;
2589 /* When 'insertmode' is set, ESC just beeps in Insert
2590 * mode. Use CTRL-L to make edit() return.
2591 * For the command line only CTRL-C always breaks it.
2592 * For the cmdline window: Alternate between ESC and
2593 * CTRL-C: ESC for most situations and CTRL-C to close the
2594 * cmdline window. */
2595 if (p_im && (State & INSERT))
2596 c = Ctrl_L;
2597 else if ((State & CMDLINE)
2598 # ifdef FEAT_CMDWIN
2599 || (cmdwin_type > 0 && tc == ESC)
2600 # endif
2602 c = Ctrl_C;
2603 else
2604 c = ESC;
2605 # ifdef FEAT_CMDWIN
2606 tc = c;
2607 # endif
2608 break;
2610 #endif
2613 * get a character: 3. from the user - update display
2615 /* In insert mode a screen update is skipped when characters
2616 * are still available. But when those available characters
2617 * are part of a mapping, and we are going to do a blocking
2618 * wait here. Need to update the screen to display the
2619 * changed text so far. */
2620 if ((State & INSERT) && advance && must_redraw != 0)
2622 update_screen(0);
2623 setcursor(); /* put cursor back where it belongs */
2627 * If we have a partial match (and are going to wait for more
2628 * input from the user), show the partially matched characters
2629 * to the user with showcmd.
2631 #ifdef FEAT_CMDL_INFO
2632 i = 0;
2633 #endif
2634 c1 = 0;
2635 if (typebuf.tb_len > 0 && advance && !exmode_active)
2637 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2638 && State != HITRETURN)
2640 /* this looks nice when typing a dead character map */
2641 if (State & INSERT
2642 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2643 + typebuf.tb_len - 1) == 1)
2645 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2646 + typebuf.tb_len - 1], FALSE);
2647 setcursor(); /* put cursor back where it belongs */
2648 c1 = 1;
2650 #ifdef FEAT_CMDL_INFO
2651 /* need to use the col and row from above here */
2652 old_wcol = curwin->w_wcol;
2653 old_wrow = curwin->w_wrow;
2654 curwin->w_wcol = new_wcol;
2655 curwin->w_wrow = new_wrow;
2656 push_showcmd();
2657 if (typebuf.tb_len > SHOWCMD_COLS)
2658 i = typebuf.tb_len - SHOWCMD_COLS;
2659 while (i < typebuf.tb_len)
2660 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2661 + i++]);
2662 curwin->w_wcol = old_wcol;
2663 curwin->w_wrow = old_wrow;
2664 #endif
2667 /* this looks nice when typing a dead character map */
2668 if ((State & CMDLINE)
2669 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2670 && cmdline_star == 0
2671 #endif
2672 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2673 + typebuf.tb_len - 1) == 1)
2675 putcmdline(typebuf.tb_buf[typebuf.tb_off
2676 + typebuf.tb_len - 1], FALSE);
2677 c1 = 1;
2682 * get a character: 3. from the user - get it
2684 wait_tb_len = typebuf.tb_len;
2685 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2686 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2687 !advance
2689 : ((typebuf.tb_len == 0
2690 || !(p_timeout || (p_ttimeout
2691 && keylen == KL_PART_KEY)))
2692 ? -1L
2693 : ((keylen == KL_PART_KEY && p_ttm >= 0)
2694 ? p_ttm
2695 : p_tm)), typebuf.tb_change_cnt);
2697 #ifdef FEAT_CMDL_INFO
2698 if (i != 0)
2699 pop_showcmd();
2700 #endif
2701 if (c1 == 1)
2703 if (State & INSERT)
2704 edit_unputchar();
2705 if (State & CMDLINE)
2706 unputcmdline();
2707 setcursor(); /* put cursor back where it belongs */
2710 if (c < 0)
2711 continue; /* end of input script reached */
2712 if (c == NUL) /* no character available */
2714 if (!advance)
2715 break;
2716 if (wait_tb_len > 0) /* timed out */
2718 timedout = TRUE;
2719 continue;
2722 else
2723 { /* allow mapping for just typed characters */
2724 while (typebuf.tb_buf[typebuf.tb_off
2725 + typebuf.tb_len] != NUL)
2726 typebuf.tb_noremap[typebuf.tb_off
2727 + typebuf.tb_len++] = RM_YES;
2728 #ifdef USE_IM_CONTROL
2729 /* Get IM status right after getting keys, not after the
2730 * timeout for a mapping (focus may be lost by then). */
2731 vgetc_im_active = im_get_status();
2732 #endif
2734 } /* for (;;) */
2735 } /* if (!character from stuffbuf) */
2737 /* if advance is FALSE don't loop on NULs */
2738 } while (c < 0 || (advance && c == NUL));
2741 * The "INSERT" message is taken care of here:
2742 * if we return an ESC to exit insert mode, the message is deleted
2743 * if we don't return an ESC but deleted the message before, redisplay it
2745 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
2747 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
2749 if (typebuf.tb_len && !KeyTyped)
2750 redraw_cmdline = TRUE; /* delete mode later */
2751 else
2752 unshowmode(FALSE);
2754 else if (c != ESC && mode_deleted)
2756 if (typebuf.tb_len && !KeyTyped)
2757 redraw_cmdline = TRUE; /* show mode later */
2758 else
2759 showmode();
2762 #ifdef FEAT_GUI
2763 /* may unshow different cursor shape */
2764 if (gui.in_use && shape_changed)
2765 gui_update_cursor(TRUE, FALSE);
2766 #endif
2768 --vgetc_busy;
2770 return c;
2774 * inchar() - get one character from
2775 * 1. a scriptfile
2776 * 2. the keyboard
2778 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2779 * NUL terminated (buffer length must be 'maxlen' + 1).
2780 * Minimum for "maxlen" is 3!!!!
2782 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2783 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2784 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2785 * otherwise.
2787 * If we got an interrupt all input is read until none is available.
2789 * If wait_time == 0 there is no waiting for the char.
2790 * If wait_time == n we wait for n msec for a character to arrive.
2791 * If wait_time == -1 we wait forever for a character to arrive.
2793 * Return the number of obtained characters.
2794 * Return -1 when end of input script reached.
2797 inchar(buf, maxlen, wait_time, tb_change_cnt)
2798 char_u *buf;
2799 int maxlen;
2800 long wait_time; /* milli seconds */
2801 int tb_change_cnt;
2803 int len = 0; /* init for GCC */
2804 int retesc = FALSE; /* return ESC with gotint */
2805 int script_char;
2807 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2809 cursor_on();
2810 out_flush();
2811 #ifdef FEAT_GUI
2812 if (gui.in_use)
2814 gui_update_cursor(FALSE, FALSE);
2815 # ifdef FEAT_MOUSESHAPE
2816 if (postponed_mouseshape)
2817 update_mouseshape(-1);
2818 # endif
2820 #endif
2824 * Don't reset these when at the hit-return prompt, otherwise a endless
2825 * recursive loop may result (write error in swapfile, hit-return, timeout
2826 * on char wait, flush swapfile, write error....).
2828 if (State != HITRETURN)
2830 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2831 did_swapwrite_msg = FALSE; /* display swap file write error again */
2833 undo_off = FALSE; /* restart undo now */
2836 * first try script file
2837 * If interrupted: Stop reading script files.
2839 script_char = -1;
2840 while (scriptin[curscript] != NULL && script_char < 0)
2842 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2844 /* Reached EOF.
2845 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2846 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2847 closescript();
2849 * When reading script file is interrupted, return an ESC to get
2850 * back to normal mode.
2851 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2853 if (got_int)
2854 retesc = TRUE;
2855 else
2856 return -1;
2858 else
2860 buf[0] = script_char;
2861 len = 1;
2865 if (script_char < 0) /* did not get a character from script */
2868 * If we got an interrupt, skip all previously typed characters and
2869 * return TRUE if quit reading script file.
2870 * Stop reading typeahead when a single CTRL-C was read,
2871 * fill_input_buf() returns this when not able to read from stdin.
2872 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2873 * and buf may be pointing inside typebuf.tb_buf[].
2875 if (got_int)
2877 #define DUM_LEN MAXMAPLEN * 3 + 3
2878 char_u dum[DUM_LEN + 1];
2880 for (;;)
2882 len = ui_inchar(dum, DUM_LEN, 0L, 0);
2883 if (len == 0 || (len == 1 && dum[0] == 3))
2884 break;
2886 return retesc;
2890 * Always flush the output characters when getting input characters
2891 * from the user.
2893 out_flush();
2896 * Fill up to a third of the buffer, because each character may be
2897 * tripled below.
2899 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
2902 if (typebuf_changed(tb_change_cnt))
2903 return 0;
2905 return fix_input_buffer(buf, len, script_char >= 0);
2909 * Fix typed characters for use by vgetc() and check_termcode().
2910 * buf[] must have room to triple the number of bytes!
2911 * Returns the new length.
2914 fix_input_buffer(buf, len, script)
2915 char_u *buf;
2916 int len;
2917 int script; /* TRUE when reading from a script */
2919 int i;
2920 char_u *p = buf;
2923 * Two characters are special: NUL and K_SPECIAL.
2924 * When compiled With the GUI CSI is also special.
2925 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
2926 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
2927 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
2928 * Don't replace K_SPECIAL when reading a script file.
2930 for (i = len; --i >= 0; ++p)
2932 #ifdef FEAT_GUI
2933 /* When the GUI is used any character can come after a CSI, don't
2934 * escape it. */
2935 if (gui.in_use && p[0] == CSI && i >= 2)
2937 p += 2;
2938 i -= 2;
2940 /* When the GUI is not used CSI needs to be escaped. */
2941 else if (!gui.in_use && p[0] == CSI)
2943 mch_memmove(p + 3, p + 1, (size_t)i);
2944 *p++ = K_SPECIAL;
2945 *p++ = KS_EXTRA;
2946 *p = (int)KE_CSI;
2947 len += 2;
2949 else
2950 #endif
2951 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
2952 #ifdef FEAT_AUTOCMD
2953 /* timeout may generate K_CURSORHOLD */
2954 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
2955 #endif
2956 #if defined(WIN3264) && !defined(FEAT_GUI)
2957 /* Win32 console passes modifiers */
2958 && (i < 2 || p[1] != KS_MODIFIER)
2959 #endif
2962 mch_memmove(p + 3, p + 1, (size_t)i);
2963 p[2] = K_THIRD(p[0]);
2964 p[1] = K_SECOND(p[0]);
2965 p[0] = K_SPECIAL;
2966 p += 2;
2967 len += 2;
2970 *p = NUL; /* add trailing NUL */
2971 return len;
2974 #if defined(USE_INPUT_BUF) || defined(PROTO)
2976 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
2977 * Normally the input buffer would be sufficient, but the server_to_input_buf()
2978 * or feedkeys() may insert characters in the typeahead buffer while we are
2979 * waiting for input to arrive.
2982 input_available()
2984 return (!vim_is_input_buf_empty()
2985 # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
2986 || typebuf_was_filled
2987 # endif
2990 #endif
2993 * map[!] : show all key mappings
2994 * map[!] {lhs} : show key mapping for {lhs}
2995 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
2996 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
2997 * unmap[!] {lhs} : remove key mapping for {lhs}
2998 * abbr : show all abbreviations
2999 * abbr {lhs} : show abbreviations for {lhs}
3000 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3001 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3002 * unabbr {lhs} : remove abbreviation for {lhs}
3004 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3006 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3007 * it will be modified.
3009 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
3010 * for :map! mode is INSERT + CMDLINE
3011 * for :cmap mode is CMDLINE
3012 * for :imap mode is INSERT
3013 * for :lmap mode is LANGMAP
3014 * for :nmap mode is NORMAL
3015 * for :vmap mode is VISUAL + SELECTMODE
3016 * for :xmap mode is VISUAL
3017 * for :smap mode is SELECTMODE
3018 * for :omap mode is OP_PENDING
3020 * for :abbr mode is INSERT + CMDLINE
3021 * for :iabbr mode is INSERT
3022 * for :cabbr mode is CMDLINE
3024 * Return 0 for success
3025 * 1 for invalid arguments
3026 * 2 for no match
3027 * 4 for out of mem
3028 * 5 for entry not unique
3031 do_map(maptype, arg, mode, abbrev)
3032 int maptype;
3033 char_u *arg;
3034 int mode;
3035 int abbrev; /* not a mapping but an abbreviation */
3037 char_u *keys;
3038 mapblock_T *mp, **mpp;
3039 char_u *rhs;
3040 char_u *p;
3041 int n;
3042 int len = 0; /* init for GCC */
3043 char_u *newstr;
3044 int hasarg;
3045 int haskey;
3046 int did_it = FALSE;
3047 #ifdef FEAT_LOCALMAP
3048 int did_local = FALSE;
3049 #endif
3050 int round;
3051 char_u *keys_buf = NULL;
3052 char_u *arg_buf = NULL;
3053 int retval = 0;
3054 int do_backslash;
3055 int hash;
3056 int new_hash;
3057 mapblock_T **abbr_table;
3058 mapblock_T **map_table;
3059 int unique = FALSE;
3060 int silent = FALSE;
3061 int special = FALSE;
3062 #ifdef FEAT_EVAL
3063 int expr = FALSE;
3064 #endif
3065 int noremap;
3067 keys = arg;
3068 map_table = maphash;
3069 abbr_table = &first_abbr;
3071 /* For ":noremap" don't remap, otherwise do remap. */
3072 if (maptype == 2)
3073 noremap = REMAP_NONE;
3074 else
3075 noremap = REMAP_YES;
3077 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
3078 for (;;)
3080 #ifdef FEAT_LOCALMAP
3082 * Check for "<buffer>": mapping local to buffer.
3084 if (STRNCMP(keys, "<buffer>", 8) == 0)
3086 keys = skipwhite(keys + 8);
3087 map_table = curbuf->b_maphash;
3088 abbr_table = &curbuf->b_first_abbr;
3089 continue;
3091 #endif
3094 * Check for "<silent>": don't echo commands.
3096 if (STRNCMP(keys, "<silent>", 8) == 0)
3098 keys = skipwhite(keys + 8);
3099 silent = TRUE;
3100 continue;
3104 * Check for "<special>": accept special keys in <>
3106 if (STRNCMP(keys, "<special>", 9) == 0)
3108 keys = skipwhite(keys + 9);
3109 special = TRUE;
3110 continue;
3113 #ifdef FEAT_EVAL
3115 * Check for "<script>": remap script-local mappings only
3117 if (STRNCMP(keys, "<script>", 8) == 0)
3119 keys = skipwhite(keys + 8);
3120 noremap = REMAP_SCRIPT;
3121 continue;
3125 * Check for "<expr>": {rhs} is an expression.
3127 if (STRNCMP(keys, "<expr>", 6) == 0)
3129 keys = skipwhite(keys + 6);
3130 expr = TRUE;
3131 continue;
3133 #endif
3135 * Check for "<unique>": don't overwrite an existing mapping.
3137 if (STRNCMP(keys, "<unique>", 8) == 0)
3139 keys = skipwhite(keys + 8);
3140 unique = TRUE;
3141 continue;
3143 break;
3146 validate_maphash();
3149 * find end of keys and skip CTRL-Vs (and backslashes) in it
3150 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
3151 * with :unmap white space is included in the keys, no argument possible
3153 p = keys;
3154 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3155 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3157 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3158 p[1] != NUL)
3159 ++p; /* skip CTRL-V or backslash */
3160 ++p;
3162 if (*p != NUL)
3163 *p++ = NUL;
3164 p = skipwhite(p);
3165 rhs = p;
3166 hasarg = (*rhs != NUL);
3167 haskey = (*keys != NUL);
3169 /* check for :unmap without argument */
3170 if (maptype == 1 && !haskey)
3172 retval = 1;
3173 goto theend;
3177 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3178 * with the appropriate two bytes. If it is a shifted special key, unshift
3179 * it too, giving another two bytes.
3180 * replace_termcodes() may move the result to allocated memory, which
3181 * needs to be freed later (*keys_buf and *arg_buf).
3182 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3184 if (haskey)
3185 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
3186 if (hasarg)
3188 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3189 rhs = (char_u *)"";
3190 else
3191 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
3194 #ifdef FEAT_FKMAP
3196 * when in right-to-left mode and alternate keymap option set,
3197 * reverse the character flow in the rhs in Farsi.
3199 if (p_altkeymap && curwin->w_p_rl)
3200 lrswap(rhs);
3201 #endif
3204 * check arguments and translate function keys
3206 if (haskey)
3208 len = (int)STRLEN(keys);
3209 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3211 retval = 1;
3212 goto theend;
3215 if (abbrev && maptype != 1)
3218 * If an abbreviation ends in a keyword character, the
3219 * rest must be all keyword-char or all non-keyword-char.
3220 * Otherwise we won't be able to find the start of it in a
3221 * vi-compatible way.
3223 #ifdef FEAT_MBYTE
3224 if (has_mbyte)
3226 int first, last;
3227 int same = -1;
3229 first = vim_iswordp(keys);
3230 last = first;
3231 p = keys + (*mb_ptr2len)(keys);
3232 n = 1;
3233 while (p < keys + len)
3235 ++n; /* nr of (multi-byte) chars */
3236 last = vim_iswordp(p); /* type of last char */
3237 if (same == -1 && last != first)
3238 same = n - 1; /* count of same char type */
3239 p += (*mb_ptr2len)(p);
3241 if (last && n > 2 && same >= 0 && same < n - 1)
3243 retval = 1;
3244 goto theend;
3247 else
3248 #endif
3249 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3250 for (n = 0; n < len - 2; ++n)
3251 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3253 retval = 1;
3254 goto theend;
3256 /* An abbrevation cannot contain white space. */
3257 for (n = 0; n < len; ++n)
3258 if (vim_iswhite(keys[n]))
3260 retval = 1;
3261 goto theend;
3266 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3267 no_abbr = FALSE; /* reset flag that indicates there are
3268 no abbreviations */
3270 if (!haskey || (maptype != 1 && !hasarg))
3271 msg_start();
3273 #ifdef FEAT_LOCALMAP
3275 * Check if a new local mapping wasn't already defined globally.
3277 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3279 /* need to loop over all global hash lists */
3280 for (hash = 0; hash < 256 && !got_int; ++hash)
3282 if (abbrev)
3284 if (hash != 0) /* there is only one abbreviation list */
3285 break;
3286 mp = first_abbr;
3288 else
3289 mp = maphash[hash];
3290 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3292 /* check entries with the same mode */
3293 if ((mp->m_mode & mode) != 0
3294 && mp->m_keylen == len
3295 && unique
3296 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3298 if (abbrev)
3299 EMSG2(_("E224: global abbreviation already exists for %s"),
3300 mp->m_keys);
3301 else
3302 EMSG2(_("E225: global mapping already exists for %s"),
3303 mp->m_keys);
3304 retval = 5;
3305 goto theend;
3312 * When listing global mappings, also list buffer-local ones here.
3314 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3316 /* need to loop over all global hash lists */
3317 for (hash = 0; hash < 256 && !got_int; ++hash)
3319 if (abbrev)
3321 if (hash != 0) /* there is only one abbreviation list */
3322 break;
3323 mp = curbuf->b_first_abbr;
3325 else
3326 mp = curbuf->b_maphash[hash];
3327 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3329 /* check entries with the same mode */
3330 if ((mp->m_mode & mode) != 0)
3332 if (!haskey) /* show all entries */
3334 showmap(mp, TRUE);
3335 did_local = TRUE;
3337 else
3339 n = mp->m_keylen;
3340 if (STRNCMP(mp->m_keys, keys,
3341 (size_t)(n < len ? n : len)) == 0)
3343 showmap(mp, TRUE);
3344 did_local = TRUE;
3351 #endif
3354 * Find an entry in the maphash[] list that matches.
3355 * For :unmap we may loop two times: once to try to unmap an entry with a
3356 * matching 'from' part, a second time, if the first fails, to unmap an
3357 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3358 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3359 * "bar" because of the abbreviation.
3361 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3362 && !did_it && !got_int; ++round)
3364 /* need to loop over all hash lists */
3365 for (hash = 0; hash < 256 && !got_int; ++hash)
3367 if (abbrev)
3369 if (hash > 0) /* there is only one abbreviation list */
3370 break;
3371 mpp = abbr_table;
3373 else
3374 mpp = &(map_table[hash]);
3375 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3378 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3380 mpp = &(mp->m_next);
3381 continue;
3383 if (!haskey) /* show all entries */
3385 showmap(mp, map_table != maphash);
3386 did_it = TRUE;
3388 else /* do we have a match? */
3390 if (round) /* second round: Try unmap "rhs" string */
3392 n = (int)STRLEN(mp->m_str);
3393 p = mp->m_str;
3395 else
3397 n = mp->m_keylen;
3398 p = mp->m_keys;
3400 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3402 if (maptype == 1) /* delete entry */
3404 /* Only accept a full match. For abbreviations we
3405 * ignore trailing space when matching with the
3406 * "lhs", since an abbreviation can't have
3407 * trailing space. */
3408 if (n != len && (!abbrev || round || n > len
3409 || *skipwhite(keys + n) != NUL))
3411 mpp = &(mp->m_next);
3412 continue;
3415 * We reset the indicated mode bits. If nothing is
3416 * left the entry is deleted below.
3418 mp->m_mode &= ~mode;
3419 did_it = TRUE; /* remember we did something */
3421 else if (!hasarg) /* show matching entry */
3423 showmap(mp, map_table != maphash);
3424 did_it = TRUE;
3426 else if (n != len) /* new entry is ambigious */
3428 mpp = &(mp->m_next);
3429 continue;
3431 else if (unique)
3433 if (abbrev)
3434 EMSG2(_("E226: abbreviation already exists for %s"),
3436 else
3437 EMSG2(_("E227: mapping already exists for %s"), p);
3438 retval = 5;
3439 goto theend;
3441 else /* new rhs for existing entry */
3443 mp->m_mode &= ~mode; /* remove mode bits */
3444 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3446 newstr = vim_strsave(rhs);
3447 if (newstr == NULL)
3449 retval = 4; /* no mem */
3450 goto theend;
3452 vim_free(mp->m_str);
3453 mp->m_str = newstr;
3454 mp->m_noremap = noremap;
3455 mp->m_silent = silent;
3456 mp->m_mode = mode;
3457 #ifdef FEAT_EVAL
3458 mp->m_expr = expr;
3459 mp->m_script_ID = current_SID;
3460 #endif
3461 did_it = TRUE;
3464 if (mp->m_mode == 0) /* entry can be deleted */
3466 map_free(mpp);
3467 continue; /* continue with *mpp */
3471 * May need to put this entry into another hash list.
3473 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3474 if (!abbrev && new_hash != hash)
3476 *mpp = mp->m_next;
3477 mp->m_next = map_table[new_hash];
3478 map_table[new_hash] = mp;
3480 continue; /* continue with *mpp */
3484 mpp = &(mp->m_next);
3489 if (maptype == 1) /* delete entry */
3491 if (!did_it)
3492 retval = 2; /* no match */
3493 goto theend;
3496 if (!haskey || !hasarg) /* print entries */
3498 if (!did_it
3499 #ifdef FEAT_LOCALMAP
3500 && !did_local
3501 #endif
3504 if (abbrev)
3505 MSG(_("No abbreviation found"));
3506 else
3507 MSG(_("No mapping found"));
3509 goto theend; /* listing finished */
3512 if (did_it) /* have added the new entry already */
3513 goto theend;
3516 * Get here when adding a new entry to the maphash[] list or abbrlist.
3518 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3519 if (mp == NULL)
3521 retval = 4; /* no mem */
3522 goto theend;
3525 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3526 if (*keys == Ctrl_C)
3527 mapped_ctrl_c = TRUE;
3529 mp->m_keys = vim_strsave(keys);
3530 mp->m_str = vim_strsave(rhs);
3531 if (mp->m_keys == NULL || mp->m_str == NULL)
3533 vim_free(mp->m_keys);
3534 vim_free(mp->m_str);
3535 vim_free(mp);
3536 retval = 4; /* no mem */
3537 goto theend;
3539 mp->m_keylen = (int)STRLEN(mp->m_keys);
3540 mp->m_noremap = noremap;
3541 mp->m_silent = silent;
3542 mp->m_mode = mode;
3543 #ifdef FEAT_EVAL
3544 mp->m_expr = expr;
3545 mp->m_script_ID = current_SID;
3546 #endif
3548 /* add the new entry in front of the abbrlist or maphash[] list */
3549 if (abbrev)
3551 mp->m_next = *abbr_table;
3552 *abbr_table = mp;
3554 else
3556 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3557 mp->m_next = map_table[n];
3558 map_table[n] = mp;
3561 theend:
3562 vim_free(keys_buf);
3563 vim_free(arg_buf);
3564 return retval;
3568 * Delete one entry from the abbrlist or maphash[].
3569 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3571 static void
3572 map_free(mpp)
3573 mapblock_T **mpp;
3575 mapblock_T *mp;
3577 mp = *mpp;
3578 vim_free(mp->m_keys);
3579 vim_free(mp->m_str);
3580 *mpp = mp->m_next;
3581 vim_free(mp);
3585 * Initialize maphash[] for first use.
3587 static void
3588 validate_maphash()
3590 if (!maphash_valid)
3592 vim_memset(maphash, 0, sizeof(maphash));
3593 maphash_valid = TRUE;
3598 * Get the mapping mode from the command name.
3601 get_map_mode(cmdp, forceit)
3602 char_u **cmdp;
3603 int forceit;
3605 char_u *p;
3606 int modec;
3607 int mode;
3609 p = *cmdp;
3610 modec = *p++;
3611 if (modec == 'i')
3612 mode = INSERT; /* :imap */
3613 else if (modec == 'l')
3614 mode = LANGMAP; /* :lmap */
3615 else if (modec == 'c')
3616 mode = CMDLINE; /* :cmap */
3617 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3618 mode = NORMAL; /* :nmap */
3619 else if (modec == 'v')
3620 mode = VISUAL + SELECTMODE; /* :vmap */
3621 else if (modec == 'x')
3622 mode = VISUAL; /* :xmap */
3623 else if (modec == 's')
3624 mode = SELECTMODE; /* :smap */
3625 else if (modec == 'o')
3626 mode = OP_PENDING; /* :omap */
3627 else
3629 --p;
3630 if (forceit)
3631 mode = INSERT + CMDLINE; /* :map ! */
3632 else
3633 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
3636 *cmdp = p;
3637 return mode;
3641 * Clear all mappings or abbreviations.
3642 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3644 /*ARGSUSED*/
3645 void
3646 map_clear(cmdp, arg, forceit, abbr)
3647 char_u *cmdp;
3648 char_u *arg;
3649 int forceit;
3650 int abbr;
3652 int mode;
3653 #ifdef FEAT_LOCALMAP
3654 int local;
3656 local = (STRCMP(arg, "<buffer>") == 0);
3657 if (!local && *arg != NUL)
3659 EMSG(_(e_invarg));
3660 return;
3662 #endif
3664 mode = get_map_mode(&cmdp, forceit);
3665 map_clear_int(curbuf, mode,
3666 #ifdef FEAT_LOCALMAP
3667 local,
3668 #else
3669 FALSE,
3670 #endif
3671 abbr);
3675 * Clear all mappings in "mode".
3677 /*ARGSUSED*/
3678 void
3679 map_clear_int(buf, mode, local, abbr)
3680 buf_T *buf; /* buffer for local mappings */
3681 int mode; /* mode in which to delete */
3682 int local; /* TRUE for buffer-local mappings */
3683 int abbr; /* TRUE for abbreviations */
3685 mapblock_T *mp, **mpp;
3686 int hash;
3687 int new_hash;
3689 validate_maphash();
3691 for (hash = 0; hash < 256; ++hash)
3693 if (abbr)
3695 if (hash > 0) /* there is only one abbrlist */
3696 break;
3697 #ifdef FEAT_LOCALMAP
3698 if (local)
3699 mpp = &buf->b_first_abbr;
3700 else
3701 #endif
3702 mpp = &first_abbr;
3704 else
3706 #ifdef FEAT_LOCALMAP
3707 if (local)
3708 mpp = &buf->b_maphash[hash];
3709 else
3710 #endif
3711 mpp = &maphash[hash];
3713 while (*mpp != NULL)
3715 mp = *mpp;
3716 if (mp->m_mode & mode)
3718 mp->m_mode &= ~mode;
3719 if (mp->m_mode == 0) /* entry can be deleted */
3721 map_free(mpp);
3722 continue;
3725 * May need to put this entry into another hash list.
3727 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3728 if (!abbr && new_hash != hash)
3730 *mpp = mp->m_next;
3731 #ifdef FEAT_LOCALMAP
3732 if (local)
3734 mp->m_next = buf->b_maphash[new_hash];
3735 buf->b_maphash[new_hash] = mp;
3737 else
3738 #endif
3740 mp->m_next = maphash[new_hash];
3741 maphash[new_hash] = mp;
3743 continue; /* continue with *mpp */
3746 mpp = &(mp->m_next);
3751 static void
3752 showmap(mp, local)
3753 mapblock_T *mp;
3754 int local; /* TRUE for buffer-local map */
3756 int len = 1;
3758 if (msg_didout || msg_silent != 0)
3759 msg_putchar('\n');
3760 if ((mp->m_mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3761 msg_putchar('!'); /* :map! */
3762 else if (mp->m_mode & INSERT)
3763 msg_putchar('i'); /* :imap */
3764 else if (mp->m_mode & LANGMAP)
3765 msg_putchar('l'); /* :lmap */
3766 else if (mp->m_mode & CMDLINE)
3767 msg_putchar('c'); /* :cmap */
3768 else if ((mp->m_mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3769 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3770 msg_putchar(' '); /* :map */
3771 else
3773 len = 0;
3774 if (mp->m_mode & NORMAL)
3776 msg_putchar('n'); /* :nmap */
3777 ++len;
3779 if (mp->m_mode & OP_PENDING)
3781 msg_putchar('o'); /* :omap */
3782 ++len;
3784 if ((mp->m_mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3786 msg_putchar('v'); /* :vmap */
3787 ++len;
3789 else
3791 if (mp->m_mode & VISUAL)
3793 msg_putchar('x'); /* :xmap */
3794 ++len;
3796 if (mp->m_mode & SELECTMODE)
3798 msg_putchar('s'); /* :smap */
3799 ++len;
3803 while (++len <= 3)
3804 msg_putchar(' ');
3806 /* Get length of what we write */
3807 len = msg_outtrans_special(mp->m_keys, TRUE);
3810 msg_putchar(' '); /* padd with blanks */
3811 ++len;
3812 } while (len < 12);
3814 if (mp->m_noremap == REMAP_NONE)
3815 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3816 else if (mp->m_noremap == REMAP_SCRIPT)
3817 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3818 else
3819 msg_putchar(' ');
3821 if (local)
3822 msg_putchar('@');
3823 else
3824 msg_putchar(' ');
3826 /* Use FALSE below if we only want things like <Up> to show up as such on
3827 * the rhs, and not M-x etc, TRUE gets both -- webb
3829 if (*mp->m_str == NUL)
3830 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3831 else
3832 msg_outtrans_special(mp->m_str, FALSE);
3833 #ifdef FEAT_EVAL
3834 if (p_verbose > 0)
3835 last_set_msg(mp->m_script_ID);
3836 #endif
3837 out_flush(); /* show one line at a time */
3840 #if defined(FEAT_EVAL) || defined(PROTO)
3842 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3843 * Recognize termcap codes in "str".
3844 * Also checks mappings local to the current buffer.
3847 map_to_exists(str, modechars, abbr)
3848 char_u *str;
3849 char_u *modechars;
3850 int abbr;
3852 int mode = 0;
3853 char_u *rhs;
3854 char_u *buf;
3855 int retval;
3857 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
3859 if (vim_strchr(modechars, 'n') != NULL)
3860 mode |= NORMAL;
3861 if (vim_strchr(modechars, 'v') != NULL)
3862 mode |= VISUAL + SELECTMODE;
3863 if (vim_strchr(modechars, 'x') != NULL)
3864 mode |= VISUAL;
3865 if (vim_strchr(modechars, 's') != NULL)
3866 mode |= SELECTMODE;
3867 if (vim_strchr(modechars, 'o') != NULL)
3868 mode |= OP_PENDING;
3869 if (vim_strchr(modechars, 'i') != NULL)
3870 mode |= INSERT;
3871 if (vim_strchr(modechars, 'l') != NULL)
3872 mode |= LANGMAP;
3873 if (vim_strchr(modechars, 'c') != NULL)
3874 mode |= CMDLINE;
3876 retval = map_to_exists_mode(rhs, mode, abbr);
3877 vim_free(buf);
3879 return retval;
3881 #endif
3884 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
3885 * Also checks mappings local to the current buffer.
3888 map_to_exists_mode(rhs, mode, abbr)
3889 char_u *rhs;
3890 int mode;
3891 int abbr;
3893 mapblock_T *mp;
3894 int hash;
3895 # ifdef FEAT_LOCALMAP
3896 int expand_buffer = FALSE;
3898 validate_maphash();
3900 /* Do it twice: once for global maps and once for local maps. */
3901 for (;;)
3903 # endif
3904 for (hash = 0; hash < 256; ++hash)
3906 if (abbr)
3908 if (hash > 0) /* there is only one abbr list */
3909 break;
3910 #ifdef FEAT_LOCALMAP
3911 if (expand_buffer)
3912 mp = curbuf->b_first_abbr;
3913 else
3914 #endif
3915 mp = first_abbr;
3917 # ifdef FEAT_LOCALMAP
3918 else if (expand_buffer)
3919 mp = curbuf->b_maphash[hash];
3920 # endif
3921 else
3922 mp = maphash[hash];
3923 for (; mp; mp = mp->m_next)
3925 if ((mp->m_mode & mode)
3926 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
3927 return TRUE;
3930 # ifdef FEAT_LOCALMAP
3931 if (expand_buffer)
3932 break;
3933 expand_buffer = TRUE;
3935 # endif
3937 return FALSE;
3940 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3942 * Used below when expanding mapping/abbreviation names.
3944 static int expand_mapmodes = 0;
3945 static int expand_isabbrev = 0;
3946 #ifdef FEAT_LOCALMAP
3947 static int expand_buffer = FALSE;
3948 #endif
3951 * Work out what to complete when doing command line completion of mapping
3952 * or abbreviation names.
3954 char_u *
3955 set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
3956 expand_T *xp;
3957 char_u *cmd;
3958 char_u *arg;
3959 int forceit; /* TRUE if '!' given */
3960 int isabbrev; /* TRUE if abbreviation */
3961 int isunmap; /* TRUE if unmap/unabbrev command */
3962 cmdidx_T cmdidx;
3964 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
3965 xp->xp_context = EXPAND_NOTHING;
3966 else
3968 if (isunmap)
3969 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
3970 else
3972 expand_mapmodes = INSERT + CMDLINE;
3973 if (!isabbrev)
3974 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
3976 expand_isabbrev = isabbrev;
3977 xp->xp_context = EXPAND_MAPPINGS;
3978 #ifdef FEAT_LOCALMAP
3979 expand_buffer = FALSE;
3980 #endif
3981 for (;;)
3983 #ifdef FEAT_LOCALMAP
3984 if (STRNCMP(arg, "<buffer>", 8) == 0)
3986 expand_buffer = TRUE;
3987 arg = skipwhite(arg + 8);
3988 continue;
3990 #endif
3991 if (STRNCMP(arg, "<unique>", 8) == 0)
3993 arg = skipwhite(arg + 8);
3994 continue;
3996 if (STRNCMP(arg, "<silent>", 8) == 0)
3998 arg = skipwhite(arg + 8);
3999 continue;
4001 #ifdef FEAT_EVAL
4002 if (STRNCMP(arg, "<script>", 8) == 0)
4004 arg = skipwhite(arg + 8);
4005 continue;
4007 if (STRNCMP(arg, "<expr>", 6) == 0)
4009 arg = skipwhite(arg + 6);
4010 continue;
4012 #endif
4013 break;
4015 xp->xp_pattern = arg;
4018 return NULL;
4022 * Find all mapping/abbreviation names that match regexp 'prog'.
4023 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4024 * Return OK if matches found, FAIL otherwise.
4027 ExpandMappings(regmatch, num_file, file)
4028 regmatch_T *regmatch;
4029 int *num_file;
4030 char_u ***file;
4032 mapblock_T *mp;
4033 int hash;
4034 int count;
4035 int round;
4036 char_u *p;
4037 int i;
4039 validate_maphash();
4041 *num_file = 0; /* return values in case of FAIL */
4042 *file = NULL;
4045 * round == 1: Count the matches.
4046 * round == 2: Build the array to keep the matches.
4048 for (round = 1; round <= 2; ++round)
4050 count = 0;
4052 for (i = 0; i < 5; ++i)
4054 if (i == 0)
4055 p = (char_u *)"<silent>";
4056 else if (i == 1)
4057 p = (char_u *)"<unique>";
4058 #ifdef FEAT_EVAL
4059 else if (i == 2)
4060 p = (char_u *)"<script>";
4061 else if (i == 3)
4062 p = (char_u *)"<expr>";
4063 #endif
4064 #ifdef FEAT_LOCALMAP
4065 else if (i == 4 && !expand_buffer)
4066 p = (char_u *)"<buffer>";
4067 #endif
4068 else
4069 continue;
4071 if (vim_regexec(regmatch, p, (colnr_T)0))
4073 if (round == 1)
4074 ++count;
4075 else
4076 (*file)[count++] = vim_strsave(p);
4080 for (hash = 0; hash < 256; ++hash)
4082 if (expand_isabbrev)
4084 if (hash > 0) /* only one abbrev list */
4085 break; /* for (hash) */
4086 mp = first_abbr;
4088 #ifdef FEAT_LOCALMAP
4089 else if (expand_buffer)
4090 mp = curbuf->b_maphash[hash];
4091 #endif
4092 else
4093 mp = maphash[hash];
4094 for (; mp; mp = mp->m_next)
4096 if (mp->m_mode & expand_mapmodes)
4098 p = translate_mapping(mp->m_keys, TRUE);
4099 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4101 if (round == 1)
4102 ++count;
4103 else
4105 (*file)[count++] = p;
4106 p = NULL;
4109 vim_free(p);
4111 } /* for (mp) */
4112 } /* for (hash) */
4114 if (count == 0) /* no match found */
4115 break; /* for (round) */
4117 if (round == 1)
4119 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4120 if (*file == NULL)
4121 return FAIL;
4123 } /* for (round) */
4125 if (count > 1)
4127 char_u **ptr1;
4128 char_u **ptr2;
4129 char_u **ptr3;
4131 /* Sort the matches */
4132 sort_strings(*file, count);
4134 /* Remove multiple entries */
4135 ptr1 = *file;
4136 ptr2 = ptr1 + 1;
4137 ptr3 = ptr1 + count;
4139 while (ptr2 < ptr3)
4141 if (STRCMP(*ptr1, *ptr2))
4142 *++ptr1 = *ptr2++;
4143 else
4145 vim_free(*ptr2++);
4146 count--;
4151 *num_file = count;
4152 return (count == 0 ? FAIL : OK);
4154 #endif /* FEAT_CMDL_COMPL */
4157 * Check for an abbreviation.
4158 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4159 * "c" is the character typed before check_abbr was called. It may have
4160 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4162 * Historic vi practice: The last character of an abbreviation must be an id
4163 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4164 * characters or all non-id characters. This allows for abbr. "#i" to
4165 * "#include".
4167 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4168 * Then there must be white space before the abbr.
4170 * return TRUE if there is an abbreviation, FALSE if not
4173 check_abbr(c, ptr, col, mincol)
4174 int c;
4175 char_u *ptr;
4176 int col;
4177 int mincol;
4179 int len;
4180 int scol; /* starting column of the abbr. */
4181 int j;
4182 char_u *s;
4183 #ifdef FEAT_MBYTE
4184 char_u tb[MB_MAXBYTES + 4];
4185 #else
4186 char_u tb[4];
4187 #endif
4188 mapblock_T *mp;
4189 #ifdef FEAT_LOCALMAP
4190 mapblock_T *mp2;
4191 #endif
4192 #ifdef FEAT_MBYTE
4193 int clen = 0; /* length in characters */
4194 #endif
4195 int is_id = TRUE;
4196 int vim_abbr;
4198 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4199 return FALSE;
4200 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0)
4201 /* no remapping implies no abbreviation */
4202 return FALSE;
4205 * Check for word before the cursor: If it ends in a keyword char all
4206 * chars before it must be al keyword chars or non-keyword chars, but not
4207 * white space. If it ends in a non-keyword char we accept any characters
4208 * before it except white space.
4210 if (col == 0) /* cannot be an abbr. */
4211 return FALSE;
4213 #ifdef FEAT_MBYTE
4214 if (has_mbyte)
4216 char_u *p;
4218 p = mb_prevptr(ptr, ptr + col);
4219 if (!vim_iswordp(p))
4220 vim_abbr = TRUE; /* Vim added abbr. */
4221 else
4223 vim_abbr = FALSE; /* vi compatible abbr. */
4224 if (p > ptr)
4225 is_id = vim_iswordp(mb_prevptr(ptr, p));
4227 clen = 1;
4228 while (p > ptr + mincol)
4230 p = mb_prevptr(ptr, p);
4231 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4233 p += (*mb_ptr2len)(p);
4234 break;
4236 ++clen;
4238 scol = (int)(p - ptr);
4240 else
4241 #endif
4243 if (!vim_iswordc(ptr[col - 1]))
4244 vim_abbr = TRUE; /* Vim added abbr. */
4245 else
4247 vim_abbr = FALSE; /* vi compatible abbr. */
4248 if (col > 1)
4249 is_id = vim_iswordc(ptr[col - 2]);
4251 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4252 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4256 if (scol < mincol)
4257 scol = mincol;
4258 if (scol < col) /* there is a word in front of the cursor */
4260 ptr += scol;
4261 len = col - scol;
4262 #ifdef FEAT_LOCALMAP
4263 mp = curbuf->b_first_abbr;
4264 mp2 = first_abbr;
4265 if (mp == NULL)
4267 mp = mp2;
4268 mp2 = NULL;
4270 #else
4271 mp = first_abbr;
4272 #endif
4273 for ( ; mp;
4274 #ifdef FEAT_LOCALMAP
4275 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4276 #endif
4277 (mp = mp->m_next))
4279 /* find entries with right mode and keys */
4280 if ( (mp->m_mode & State)
4281 && mp->m_keylen == len
4282 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4283 break;
4285 if (mp != NULL)
4288 * Found a match:
4289 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4290 * This goes from end to start.
4292 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4293 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4294 * Characters where IS_SPECIAL() == TRUE: key codes, need
4295 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4297 * Character CTRL-] is treated specially - it completes the
4298 * abbreviation, but is not inserted into the input stream.
4300 j = 0;
4301 /* special key code, split up */
4302 if (c != Ctrl_RSB)
4304 if (IS_SPECIAL(c) || c == K_SPECIAL)
4306 tb[j++] = K_SPECIAL;
4307 tb[j++] = K_SECOND(c);
4308 tb[j++] = K_THIRD(c);
4310 else
4312 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4313 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4314 #ifdef FEAT_MBYTE
4315 if (has_mbyte)
4317 /* if ABBR_OFF has been added, remove it here */
4318 if (c >= ABBR_OFF)
4319 c -= ABBR_OFF;
4320 j += (*mb_char2bytes)(c, tb + j);
4322 else
4323 #endif
4324 tb[j++] = c;
4326 tb[j] = NUL;
4327 /* insert the last typed char */
4328 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4330 #ifdef FEAT_EVAL
4331 if (mp->m_expr)
4332 s = eval_map_expr(mp->m_str);
4333 else
4334 #endif
4335 s = mp->m_str;
4336 if (s != NULL)
4338 /* insert the to string */
4339 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
4340 /* no abbrev. for these chars */
4341 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4342 #ifdef FEAT_EVAL
4343 if (mp->m_expr)
4344 vim_free(s);
4345 #endif
4348 tb[0] = Ctrl_H;
4349 tb[1] = NUL;
4350 #ifdef FEAT_MBYTE
4351 if (has_mbyte)
4352 len = clen; /* Delete characters instead of bytes */
4353 #endif
4354 while (len-- > 0) /* delete the from string */
4355 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4356 return TRUE;
4359 return FALSE;
4362 #ifdef FEAT_EVAL
4364 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4365 * special characters.
4367 static char_u *
4368 eval_map_expr(str)
4369 char_u *str;
4371 char_u *res;
4372 char_u *p;
4373 char_u *save_cmd;
4374 pos_T save_cursor;
4376 save_cmd = save_cmdline_alloc();
4377 if (save_cmd == NULL)
4378 return NULL;
4380 /* Forbid changing text or using ":normal" to avoid most of the bad side
4381 * effects. Also restore the cursor position. */
4382 ++textlock;
4383 #ifdef FEAT_EX_EXTRA
4384 ++ex_normal_lock;
4385 #endif
4386 save_cursor = curwin->w_cursor;
4387 p = eval_to_string(str, NULL, FALSE);
4388 --textlock;
4389 #ifdef FEAT_EX_EXTRA
4390 --ex_normal_lock;
4391 #endif
4392 curwin->w_cursor = save_cursor;
4394 restore_cmdline_alloc(save_cmd);
4395 if (p == NULL)
4396 return NULL;
4397 res = vim_strsave_escape_csi(p);
4398 vim_free(p);
4400 return res;
4402 #endif
4405 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4406 * can be put in the typeahead buffer.
4407 * Returns NULL when out of memory.
4409 char_u *
4410 vim_strsave_escape_csi(p)
4411 char_u *p;
4413 char_u *res;
4414 char_u *s, *d;
4416 /* Need a buffer to hold up to three times as much. */
4417 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
4418 if (res != NULL)
4420 d = res;
4421 for (s = p; *s != NUL; )
4423 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4425 /* Copy special key unmodified. */
4426 *d++ = *s++;
4427 *d++ = *s++;
4428 *d++ = *s++;
4430 else
4432 /* Add character, possibly multi-byte to destination, escaping
4433 * CSI and K_SPECIAL. */
4434 d = add_char2buf(PTR2CHAR(s), d);
4435 mb_ptr_adv(s);
4438 *d = NUL;
4440 return res;
4444 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4445 * vim_strsave_escape_csi(). Works in-place.
4447 void
4448 vim_unescape_csi(p)
4449 char_u *p;
4451 char_u *s = p, *d = p;
4453 while (*s != NUL)
4455 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4457 *d++ = K_SPECIAL;
4458 s += 3;
4460 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4461 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4463 *d++ = CSI;
4464 s += 3;
4466 else
4467 *d++ = *s++;
4469 *d = NUL;
4473 * Write map commands for the current mappings to an .exrc file.
4474 * Return FAIL on error, OK otherwise.
4477 makemap(fd, buf)
4478 FILE *fd;
4479 buf_T *buf; /* buffer for local mappings or NULL */
4481 mapblock_T *mp;
4482 char_u c1, c2;
4483 char_u *p;
4484 char *cmd;
4485 int abbr;
4486 int hash;
4487 int did_cpo = FALSE;
4488 int i;
4490 validate_maphash();
4493 * Do the loop twice: Once for mappings, once for abbreviations.
4494 * Then loop over all map hash lists.
4496 for (abbr = 0; abbr < 2; ++abbr)
4497 for (hash = 0; hash < 256; ++hash)
4499 if (abbr)
4501 if (hash > 0) /* there is only one abbr list */
4502 break;
4503 #ifdef FEAT_LOCALMAP
4504 if (buf != NULL)
4505 mp = buf->b_first_abbr;
4506 else
4507 #endif
4508 mp = first_abbr;
4510 else
4512 #ifdef FEAT_LOCALMAP
4513 if (buf != NULL)
4514 mp = buf->b_maphash[hash];
4515 else
4516 #endif
4517 mp = maphash[hash];
4520 for ( ; mp; mp = mp->m_next)
4522 /* skip script-local mappings */
4523 if (mp->m_noremap == REMAP_SCRIPT)
4524 continue;
4526 /* skip mappings that contain a <SNR> (script-local thing),
4527 * they probably don't work when loaded again */
4528 for (p = mp->m_str; *p != NUL; ++p)
4529 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4530 && p[2] == (int)KE_SNR)
4531 break;
4532 if (*p != NUL)
4533 continue;
4535 c1 = NUL;
4536 c2 = NUL;
4537 if (abbr)
4538 cmd = "abbr";
4539 else
4540 cmd = "map";
4541 switch (mp->m_mode)
4543 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
4544 break;
4545 case NORMAL:
4546 c1 = 'n';
4547 break;
4548 case VISUAL + SELECTMODE:
4549 c1 = 'v';
4550 break;
4551 case VISUAL:
4552 c1 = 'x';
4553 break;
4554 case SELECTMODE:
4555 c1 = 's';
4556 break;
4557 case OP_PENDING:
4558 c1 = 'o';
4559 break;
4560 case NORMAL + VISUAL + SELECTMODE:
4561 c1 = 'n';
4562 c2 = 'v';
4563 break;
4564 case VISUAL + SELECTMODE + OP_PENDING:
4565 c1 = 'v';
4566 c2 = 'o';
4567 break;
4568 case NORMAL + OP_PENDING:
4569 c1 = 'n';
4570 c2 = 'o';
4571 break;
4572 case CMDLINE + INSERT:
4573 if (!abbr)
4574 cmd = "map!";
4575 break;
4576 case CMDLINE:
4577 c1 = 'c';
4578 break;
4579 case INSERT:
4580 c1 = 'i';
4581 break;
4582 case LANGMAP:
4583 c1 = 'l';
4584 break;
4585 default:
4586 EMSG(_("E228: makemap: Illegal mode"));
4587 return FAIL;
4589 do /* may do this twice if c2 is set */
4591 /* When outputting <> form, need to make sure that 'cpo'
4592 * is set to the Vim default. */
4593 if (!did_cpo)
4595 if (*mp->m_str == NUL) /* will use <Nop> */
4596 did_cpo = TRUE;
4597 else
4598 for (i = 0; i < 2; ++i)
4599 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4600 if (*p == K_SPECIAL || *p == NL)
4601 did_cpo = TRUE;
4602 if (did_cpo)
4604 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4605 || put_eol(fd) < 0
4606 || fprintf(fd, "set cpo&vim") < 0
4607 || put_eol(fd) < 0)
4608 return FAIL;
4611 if (c1 && putc(c1, fd) < 0)
4612 return FAIL;
4613 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4614 return FAIL;
4615 if (fprintf(fd, cmd) < 0)
4616 return FAIL;
4617 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4618 return FAIL;
4619 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4620 return FAIL;
4621 #ifdef FEAT_EVAL
4622 if (mp->m_noremap == REMAP_SCRIPT
4623 && fputs("<script>", fd) < 0)
4624 return FAIL;
4625 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4626 return FAIL;
4627 #endif
4629 if ( putc(' ', fd) < 0
4630 || put_escstr(fd, mp->m_keys, 0) == FAIL
4631 || putc(' ', fd) < 0
4632 || put_escstr(fd, mp->m_str, 1) == FAIL
4633 || put_eol(fd) < 0)
4634 return FAIL;
4635 c1 = c2;
4636 c2 = NUL;
4638 while (c1);
4642 if (did_cpo)
4643 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4644 || put_eol(fd) < 0
4645 || fprintf(fd, "unlet s:cpo_save") < 0
4646 || put_eol(fd) < 0)
4647 return FAIL;
4648 return OK;
4652 * write escape string to file
4653 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4655 * return FAIL for failure, OK otherwise
4658 put_escstr(fd, strstart, what)
4659 FILE *fd;
4660 char_u *strstart;
4661 int what;
4663 char_u *str = strstart;
4664 int c;
4665 int modifiers;
4667 /* :map xx <Nop> */
4668 if (*str == NUL && what == 1)
4670 if (fprintf(fd, "<Nop>") < 0)
4671 return FAIL;
4672 return OK;
4675 for ( ; *str != NUL; ++str)
4677 #ifdef FEAT_MBYTE
4678 char_u *p;
4680 /* Check for a multi-byte character, which may contain escaped
4681 * K_SPECIAL and CSI bytes */
4682 p = mb_unescape(&str);
4683 if (p != NULL)
4685 while (*p != NUL)
4686 if (fputc(*p++, fd) < 0)
4687 return FAIL;
4688 --str;
4689 continue;
4691 #endif
4693 c = *str;
4695 * Special key codes have to be translated to be able to make sense
4696 * when they are read back.
4698 if (c == K_SPECIAL && what != 2)
4700 modifiers = 0x0;
4701 if (str[1] == KS_MODIFIER)
4703 modifiers = str[2];
4704 str += 3;
4705 c = *str;
4707 if (c == K_SPECIAL)
4709 c = TO_SPECIAL(str[1], str[2]);
4710 str += 2;
4712 if (IS_SPECIAL(c) || modifiers) /* special key */
4714 if (fprintf(fd, (char *)get_special_key_name(c, modifiers)) < 0)
4715 return FAIL;
4716 continue;
4721 * A '\n' in a map command should be written as <NL>.
4722 * A '\n' in a set command should be written as \^V^J.
4724 if (c == NL)
4726 if (what == 2)
4728 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4729 return FAIL;
4731 else
4733 if (fprintf(fd, "<NL>") < 0)
4734 return FAIL;
4736 continue;
4740 * Some characters have to be escaped with CTRL-V to
4741 * prevent them from misinterpreted in DoOneCmd().
4742 * A space, Tab and '"' has to be escaped with a backslash to
4743 * prevent it to be misinterpreted in do_set().
4744 * A space has to be escaped with a CTRL-V when it's at the start of a
4745 * ":map" rhs.
4746 * A '<' has to be escaped with a CTRL-V to prevent it being
4747 * interpreted as the start of a special key name.
4748 * A space in the lhs of a :map needs a CTRL-V.
4750 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4752 if (putc('\\', fd) < 0)
4753 return FAIL;
4755 else if (c < ' ' || c > '~' || c == '|'
4756 || (what == 0 && c == ' ')
4757 || (what == 1 && str == strstart && c == ' ')
4758 || (what != 2 && c == '<'))
4760 if (putc(Ctrl_V, fd) < 0)
4761 return FAIL;
4763 if (putc(c, fd) < 0)
4764 return FAIL;
4766 return OK;
4770 * Check all mappings for the presence of special key codes.
4771 * Used after ":set term=xxx".
4773 void
4774 check_map_keycodes()
4776 mapblock_T *mp;
4777 char_u *p;
4778 int i;
4779 char_u buf[3];
4780 char_u *save_name;
4781 int abbr;
4782 int hash;
4783 #ifdef FEAT_LOCALMAP
4784 buf_T *bp;
4785 #endif
4787 validate_maphash();
4788 save_name = sourcing_name;
4789 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4791 #ifdef FEAT_LOCALMAP
4792 /* This this once for each buffer, and then once for global
4793 * mappings/abbreviations with bp == NULL */
4794 for (bp = firstbuf; ; bp = bp->b_next)
4796 #endif
4798 * Do the loop twice: Once for mappings, once for abbreviations.
4799 * Then loop over all map hash lists.
4801 for (abbr = 0; abbr <= 1; ++abbr)
4802 for (hash = 0; hash < 256; ++hash)
4804 if (abbr)
4806 if (hash) /* there is only one abbr list */
4807 break;
4808 #ifdef FEAT_LOCALMAP
4809 if (bp != NULL)
4810 mp = bp->b_first_abbr;
4811 else
4812 #endif
4813 mp = first_abbr;
4815 else
4817 #ifdef FEAT_LOCALMAP
4818 if (bp != NULL)
4819 mp = bp->b_maphash[hash];
4820 else
4821 #endif
4822 mp = maphash[hash];
4824 for ( ; mp != NULL; mp = mp->m_next)
4826 for (i = 0; i <= 1; ++i) /* do this twice */
4828 if (i == 0)
4829 p = mp->m_keys; /* once for the "from" part */
4830 else
4831 p = mp->m_str; /* and once for the "to" part */
4832 while (*p)
4834 if (*p == K_SPECIAL)
4836 ++p;
4837 if (*p < 128) /* for "normal" tcap entries */
4839 buf[0] = p[0];
4840 buf[1] = p[1];
4841 buf[2] = NUL;
4842 (void)add_termcap_entry(buf, FALSE);
4844 ++p;
4846 ++p;
4851 #ifdef FEAT_LOCALMAP
4852 if (bp == NULL)
4853 break;
4855 #endif
4856 sourcing_name = save_name;
4859 #ifdef FEAT_EVAL
4861 * Check the string "keys" against the lhs of all mappings
4862 * Return pointer to rhs of mapping (mapblock->m_str)
4863 * NULL otherwise
4865 char_u *
4866 check_map(keys, mode, exact, ign_mod, abbr)
4867 char_u *keys;
4868 int mode;
4869 int exact; /* require exact match */
4870 int ign_mod; /* ignore preceding modifier */
4871 int abbr; /* do abbreviations */
4873 int hash;
4874 int len, minlen;
4875 mapblock_T *mp;
4876 char_u *s;
4877 #ifdef FEAT_LOCALMAP
4878 int local;
4879 #endif
4881 validate_maphash();
4883 len = (int)STRLEN(keys);
4884 #ifdef FEAT_LOCALMAP
4885 for (local = 1; local >= 0; --local)
4886 #endif
4887 /* loop over all hash lists */
4888 for (hash = 0; hash < 256; ++hash)
4890 if (abbr)
4892 if (hash > 0) /* there is only one list. */
4893 break;
4894 #ifdef FEAT_LOCALMAP
4895 if (local)
4896 mp = curbuf->b_first_abbr;
4897 else
4898 #endif
4899 mp = first_abbr;
4901 #ifdef FEAT_LOCALMAP
4902 else if (local)
4903 mp = curbuf->b_maphash[hash];
4904 #endif
4905 else
4906 mp = maphash[hash];
4907 for ( ; mp != NULL; mp = mp->m_next)
4909 /* skip entries with wrong mode, wrong length and not matching
4910 * ones */
4911 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
4913 if (len > mp->m_keylen)
4914 minlen = mp->m_keylen;
4915 else
4916 minlen = len;
4917 s = mp->m_keys;
4918 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
4919 && s[2] != NUL)
4921 s += 3;
4922 if (len > mp->m_keylen - 3)
4923 minlen = mp->m_keylen - 3;
4925 if (STRNCMP(s, keys, minlen) == 0)
4926 return mp->m_str;
4931 return NULL;
4933 #endif
4935 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
4937 #define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
4940 * Default mappings for some often used keys.
4942 static struct initmap
4944 char_u *arg;
4945 int mode;
4946 } initmappings[] =
4948 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4949 /* Use the Windows (CUA) keybindings. */
4950 # ifdef FEAT_GUI
4951 # if 0 /* These are now used to move tab pages */
4952 {(char_u *)"<C-PageUp> H", NORMAL+VIS_SEL},
4953 {(char_u *)"<C-PageUp> <C-O>H",INSERT},
4954 {(char_u *)"<C-PageDown> L$", NORMAL+VIS_SEL},
4955 {(char_u *)"<C-PageDown> <C-O>L<C-O>$", INSERT},
4956 # endif
4958 /* paste, copy and cut */
4959 {(char_u *)"<S-Insert> \"*P", NORMAL},
4960 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
4961 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
4962 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
4963 {(char_u *)"<S-Del> \"*d", VIS_SEL},
4964 {(char_u *)"<C-Del> \"*d", VIS_SEL},
4965 {(char_u *)"<C-X> \"*d", VIS_SEL},
4966 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
4967 # else
4968 # if 0 /* These are now used to move tab pages */
4969 {(char_u *)"\316\204 H", NORMAL+VIS_SEL}, /* CTRL-PageUp is "H" */
4970 {(char_u *)"\316\204 \017H",INSERT}, /* CTRL-PageUp is "^OH"*/
4971 {(char_u *)"\316v L$", NORMAL+VIS_SEL}, /* CTRL-PageDown is "L$" */
4972 {(char_u *)"\316v \017L\017$", INSERT}, /* CTRL-PageDown ="^OL^O$"*/
4973 # endif
4974 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
4975 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
4976 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
4977 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
4979 /* paste, copy and cut */
4980 # ifdef FEAT_CLIPBOARD
4981 # ifdef DJGPP
4982 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
4983 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
4984 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
4985 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
4986 # if 0 /* Shift-Del produces the same code as Del */
4987 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
4988 # endif
4989 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
4990 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
4991 # else
4992 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
4993 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
4994 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
4995 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
4996 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
4997 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
4998 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
4999 # endif
5000 # else
5001 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
5002 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
5003 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
5004 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5005 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5006 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
5007 # endif
5008 # endif
5009 #endif
5011 #if defined(MACOS)
5012 /* Use the Standard MacOS binding. */
5013 /* paste, copy and cut */
5014 {(char_u *)"<D-v> \"*P", NORMAL},
5015 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
5016 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
5017 {(char_u *)"<D-c> \"*y", VIS_SEL},
5018 {(char_u *)"<D-x> \"*d", VIS_SEL},
5019 {(char_u *)"<Backspace> \"-d", VIS_SEL},
5020 #endif
5023 # undef VIS_SEL
5024 #endif
5027 * Set up default mappings.
5029 void
5030 init_mappings()
5032 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
5033 int i;
5035 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5036 add_map(initmappings[i].arg, initmappings[i].mode);
5037 #endif
5040 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5041 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
5043 * Add a mapping "map" for mode "mode".
5044 * Need to put string in allocated memory, because do_map() will modify it.
5046 void
5047 add_map(map, mode)
5048 char_u *map;
5049 int mode;
5051 char_u *s;
5052 char_u *cpo_save = p_cpo;
5054 p_cpo = (char_u *)""; /* Allow <> notation */
5055 s = vim_strsave(map);
5056 if (s != NULL)
5058 (void)do_map(0, s, mode, FALSE);
5059 vim_free(s);
5061 p_cpo = cpo_save;
5063 #endif