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