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