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