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