blob: 05693727f643fea22db15ab56e1a4f91d6e2ff15 [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.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000025 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 *
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
Bram Moolenaarda9591e2009-09-30 13:17:02 +0000132static char_u *eval_map_expr __ARGS((char_u *str, int c));
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000133#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{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000224 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225}
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{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001282 if (typebuf.tb_buf == typebuf_init)
1283 EMSG2(_(e_intern2), "Free typebuf 1");
1284 else
1285 vim_free(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001286 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001287 EMSG2(_(e_intern2), "Free typebuf 2");
1288 else
1289 vim_free(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290}
1291
1292/*
1293 * When doing ":so! file", the current typeahead needs to be saved, and
1294 * restored when "file" has been read completely.
1295 */
1296static typebuf_T saved_typebuf[NSCRIPT];
1297
1298 int
1299save_typebuf()
1300{
1301 init_typebuf();
1302 saved_typebuf[curscript] = typebuf;
1303 /* If out of memory: restore typebuf and close file. */
1304 if (alloc_typebuf() == FAIL)
1305 {
1306 closescript();
1307 return FAIL;
1308 }
1309 return OK;
1310}
1311
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001312static int old_char = -1; /* character put back by vungetc() */
1313static int old_mod_mask; /* mod_mask for ungotten character */
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1316
1317/*
1318 * Save all three kinds of typeahead, so that the user must type at a prompt.
1319 */
1320 void
1321save_typeahead(tp)
1322 tasave_T *tp;
1323{
1324 tp->save_typebuf = typebuf;
1325 tp->typebuf_valid = (alloc_typebuf() == OK);
1326 if (!tp->typebuf_valid)
1327 typebuf = tp->save_typebuf;
1328
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001329 tp->old_char = old_char;
1330 tp->old_mod_mask = old_mod_mask;
1331 old_char = -1;
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 tp->save_stuffbuff = stuffbuff;
1334 stuffbuff.bh_first.b_next = NULL;
1335# ifdef USE_INPUT_BUF
1336 tp->save_inputbuf = get_input_buf();
1337# endif
1338}
1339
1340/*
1341 * Restore the typeahead to what it was before calling save_typeahead().
1342 * The allocated memory is freed, can only be called once!
1343 */
1344 void
1345restore_typeahead(tp)
1346 tasave_T *tp;
1347{
1348 if (tp->typebuf_valid)
1349 {
1350 free_typebuf();
1351 typebuf = tp->save_typebuf;
1352 }
1353
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001354 old_char = tp->old_char;
1355 old_mod_mask = tp->old_mod_mask;
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 free_buff(&stuffbuff);
1358 stuffbuff = tp->save_stuffbuff;
1359# ifdef USE_INPUT_BUF
1360 set_input_buf(tp->save_inputbuf);
1361# endif
1362}
1363#endif
1364
1365/*
1366 * Open a new script file for the ":source!" command.
1367 */
1368 void
1369openscript(name, directly)
1370 char_u *name;
1371 int directly; /* when TRUE execute directly */
1372{
1373 if (curscript + 1 == NSCRIPT)
1374 {
1375 EMSG(_(e_nesting));
1376 return;
1377 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001378#ifdef FEAT_EVAL
1379 if (ignore_script)
1380 /* Not reading from script, also don't open one. Warning message? */
1381 return;
1382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384 if (scriptin[curscript] != NULL) /* already reading script */
1385 ++curscript;
1386 /* use NameBuff for expanded name */
1387 expand_env(name, NameBuff, MAXPATHL);
1388 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1389 {
1390 EMSG2(_(e_notopen), name);
1391 if (curscript)
1392 --curscript;
1393 return;
1394 }
1395 if (save_typebuf() == FAIL)
1396 return;
1397
1398 /*
1399 * Execute the commands from the file right now when using ":source!"
1400 * after ":global" or ":argdo" or in a loop. Also when another command
1401 * follows. This means the display won't be updated. Don't do this
1402 * always, "make test" would fail.
1403 */
1404 if (directly)
1405 {
1406 oparg_T oa;
1407 int oldcurscript;
1408 int save_State = State;
1409 int save_restart_edit = restart_edit;
1410 int save_insertmode = p_im;
1411 int save_finish_op = finish_op;
1412 int save_msg_scroll = msg_scroll;
1413
1414 State = NORMAL;
1415 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1416 restart_edit = 0; /* don't go to Insert mode */
1417 p_im = FALSE; /* don't use 'insertmode' */
1418 clear_oparg(&oa);
1419 finish_op = FALSE;
1420
1421 oldcurscript = curscript;
1422 do
1423 {
1424 update_topline_cursor(); /* update cursor position and topline */
1425 normal_cmd(&oa, FALSE); /* execute one command */
1426 vpeekc(); /* check for end of file */
1427 }
1428 while (scriptin[oldcurscript] != NULL);
1429
1430 State = save_State;
1431 msg_scroll = save_msg_scroll;
1432 restart_edit = save_restart_edit;
1433 p_im = save_insertmode;
1434 finish_op = save_finish_op;
1435 }
1436}
1437
1438/*
1439 * Close the currently active input script.
1440 */
1441 static void
1442closescript()
1443{
1444 free_typebuf();
1445 typebuf = saved_typebuf[curscript];
1446
1447 fclose(scriptin[curscript]);
1448 scriptin[curscript] = NULL;
1449 if (curscript > 0)
1450 --curscript;
1451}
1452
Bram Moolenaarea408852005-06-25 22:49:46 +00001453#if defined(EXITFREE) || defined(PROTO)
1454 void
1455close_all_scripts()
1456{
1457 while (scriptin[0] != NULL)
1458 closescript();
1459}
1460#endif
1461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1463/*
1464 * Return TRUE when reading keys from a script file.
1465 */
1466 int
1467using_script()
1468{
1469 return scriptin[curscript] != NULL;
1470}
1471#endif
1472
1473/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001474 * This function is called just before doing a blocking wait. Thus after
1475 * waiting 'updatetime' for a character to arrive.
1476 */
1477 void
1478before_blocking()
1479{
1480 updatescript(0);
1481#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001482 if (may_garbage_collect)
1483 garbage_collect();
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001484#endif
1485}
1486
1487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 * updatescipt() is called when a character can be written into the script file
1489 * or when we have waited some time for a character (c == 0)
1490 *
1491 * All the changed memfiles are synced if c == 0 or when the number of typed
1492 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1493 */
1494 void
1495updatescript(c)
1496 int c;
1497{
1498 static int count = 0;
1499
1500 if (c && scriptout)
1501 putc(c, scriptout);
1502 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1503 {
1504 ml_sync_all(c == 0, TRUE);
1505 count = 0;
1506 }
1507}
1508
1509#define KL_PART_KEY -1 /* keylen value for incomplete key-code */
1510#define KL_PART_MAP -2 /* keylen value for incomplete mapping */
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
1513 * Get the next input character.
1514 * Can return a special key or a multi-byte character.
1515 * Can return NUL when called recursively, use safe_vgetc() if that's not
1516 * wanted.
1517 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1518 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001519 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
1521 int
1522vgetc()
1523{
1524 int c, c2;
1525#ifdef FEAT_MBYTE
1526 int n;
1527 char_u buf[MB_MAXBYTES];
1528 int i;
1529#endif
1530
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001531#ifdef FEAT_EVAL
1532 /* Do garbage collection when garbagecollect() was called previously and
1533 * we are now at the toplevel. */
1534 if (may_garbage_collect && want_garbage_collect)
1535 garbage_collect();
1536#endif
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
1539 * If a character was put back with vungetc, it was already processed.
1540 * Return it directly.
1541 */
1542 if (old_char != -1)
1543 {
1544 c = old_char;
1545 old_char = -1;
1546 mod_mask = old_mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001550 mod_mask = 0x0;
1551 last_recorded_len = 0;
1552 for (;;) /* this is done twice if there are modifiers */
1553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (mod_mask) /* no mapping after modifier has been read */
1555 {
1556 ++no_mapping;
1557 ++allow_keys;
1558 }
1559 c = vgetorpeek(TRUE);
1560 if (mod_mask)
1561 {
1562 --no_mapping;
1563 --allow_keys;
1564 }
1565
1566 /* Get two extra bytes for special keys */
1567 if (c == K_SPECIAL
1568#ifdef FEAT_GUI
1569 || c == CSI
1570#endif
1571 )
1572 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001573 int save_allow_keys = allow_keys;
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 ++no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001576 allow_keys = 0; /* make sure BS is not found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1578 c = vgetorpeek(TRUE);
1579 --no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001580 allow_keys = save_allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (c2 == KS_MODIFIER)
1582 {
1583 mod_mask = c;
1584 continue;
1585 }
1586 c = TO_SPECIAL(c2, c);
1587
1588#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1589 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1590 * know that a menu was torn off */
1591 if (c == K_TEAROFF)
1592 {
1593 char_u name[200];
1594 int i;
1595
1596 /* get menu path, it ends with a <CR> */
1597 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1598 {
1599 name[i] = c;
1600 if (i < 199)
1601 ++i;
1602 }
1603 name[i] = NUL;
1604 gui_make_tearoff(name);
1605 continue;
1606 }
1607#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001608#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001609 /* GTK: <F10> normally selects the menu, but it's passed until
1610 * here to allow mapping it. Intercept and invoke the GTK
1611 * behavior if it's not mapped. */
1612 if (c == K_F10 && gui.menubar != NULL)
1613 {
1614 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1615 continue;
1616 }
1617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618#ifdef FEAT_GUI
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001619 /* Handle focus event here, so that the caller doesn't need to
1620 * know about it. Return K_IGNORE so that we loop once (needed if
1621 * 'lazyredraw' is set). */
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001622 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1623 {
1624 ui_focus_change(c == K_FOCUSGAINED);
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001625 c = K_IGNORE;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001626 }
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* Translate K_CSI to CSI. The special key is only used to avoid
1629 * it being recognized as the start of a special key. */
1630 if (c == K_CSI)
1631 c = CSI;
1632#endif
1633 }
1634#ifdef MSDOS
1635 /*
1636 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1637 * Delete the 3 here.
1638 */
1639 else if (c == K_NUL && vpeekc() == 3)
1640 (void)vgetorpeek(TRUE);
1641#endif
1642
Bram Moolenaara88d9682005-03-25 21:45:43 +00001643 /* a keypad or special function key was not mapped, use it like
1644 * its ASCII equivalent */
1645 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaara88d9682005-03-25 21:45:43 +00001647 case K_KPLUS: c = '+'; break;
1648 case K_KMINUS: c = '-'; break;
1649 case K_KDIVIDE: c = '/'; break;
1650 case K_KMULTIPLY: c = '*'; break;
1651 case K_KENTER: c = CAR; break;
1652 case K_KPOINT:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001653#ifdef WIN32
Bram Moolenaara88d9682005-03-25 21:45:43 +00001654 /* Can be either '.' or a ',', *
1655 * depending on the type of keypad. */
1656 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001657#else
Bram Moolenaara88d9682005-03-25 21:45:43 +00001658 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001659#endif
Bram Moolenaara88d9682005-03-25 21:45:43 +00001660 case K_K0: c = '0'; break;
1661 case K_K1: c = '1'; break;
1662 case K_K2: c = '2'; break;
1663 case K_K3: c = '3'; break;
1664 case K_K4: c = '4'; break;
1665 case K_K5: c = '5'; break;
1666 case K_K6: c = '6'; break;
1667 case K_K7: c = '7'; break;
1668 case K_K8: c = '8'; break;
1669 case K_K9: c = '9'; break;
1670
1671 case K_XHOME:
1672 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1673 {
1674 c = K_S_HOME;
1675 mod_mask = 0;
1676 }
1677 else if (mod_mask == MOD_MASK_CTRL)
1678 {
1679 c = K_C_HOME;
1680 mod_mask = 0;
1681 }
1682 else
1683 c = K_HOME;
1684 break;
1685 case K_XEND:
1686 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1687 {
1688 c = K_S_END;
1689 mod_mask = 0;
1690 }
1691 else if (mod_mask == MOD_MASK_CTRL)
1692 {
1693 c = K_C_END;
1694 mod_mask = 0;
1695 }
1696 else
1697 c = K_END;
1698 break;
1699
1700 case K_XUP: c = K_UP; break;
1701 case K_XDOWN: c = K_DOWN; break;
1702 case K_XLEFT: c = K_LEFT; break;
1703 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
1705
1706#ifdef FEAT_MBYTE
1707 /* For a multi-byte character get all the bytes and return the
1708 * converted character.
1709 * Note: This will loop until enough bytes are received!
1710 */
1711 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1712 {
1713 ++no_mapping;
1714 buf[0] = c;
1715 for (i = 1; i < n; ++i)
1716 {
1717 buf[i] = vgetorpeek(TRUE);
1718 if (buf[i] == K_SPECIAL
1719#ifdef FEAT_GUI
1720 || buf[i] == CSI
1721#endif
1722 )
1723 {
1724 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1725 * which represents a K_SPECIAL (0x80),
1726 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1727 * a CSI (0x9B),
1728 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1729 c = vgetorpeek(TRUE);
1730 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1731 buf[i] = CSI;
1732 }
1733 }
1734 --no_mapping;
1735 c = (*mb_ptr2char)(buf);
1736 }
1737#endif
1738
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001739 break;
1740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001742
1743#ifdef FEAT_EVAL
1744 /*
1745 * In the main loop "may_garbage_collect" can be set to do garbage
1746 * collection in the first next vgetc(). It's disabled after that to
1747 * avoid internally used Lists and Dicts to be freed.
1748 */
1749 may_garbage_collect = FALSE;
1750#endif
1751
1752 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753}
1754
1755/*
1756 * Like vgetc(), but never return a NUL when called recursively, get a key
1757 * directly from the user (ignoring typeahead).
1758 */
1759 int
1760safe_vgetc()
1761{
1762 int c;
1763
1764 c = vgetc();
1765 if (c == NUL)
1766 c = get_keystroke();
1767 return c;
1768}
1769
1770/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001771 * Like safe_vgetc(), but loop to handle K_IGNORE.
1772 * Also ignore scrollbar events.
1773 */
1774 int
1775plain_vgetc()
1776{
1777 int c;
1778
1779 do
1780 {
1781 c = safe_vgetc();
1782 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1783 return c;
1784}
1785
1786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 * Check if a character is available, such that vgetc() will not block.
1788 * If the next character is a special character or multi-byte, the returned
1789 * character is not valid!.
1790 */
1791 int
1792vpeekc()
1793{
1794 if (old_char != -1)
1795 return old_char;
1796 return vgetorpeek(FALSE);
1797}
1798
1799#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1800/*
1801 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1802 * codes.
1803 */
1804 int
1805vpeekc_nomap()
1806{
1807 int c;
1808
1809 ++no_mapping;
1810 ++allow_keys;
1811 c = vpeekc();
1812 --no_mapping;
1813 --allow_keys;
1814 return c;
1815}
1816#endif
1817
1818#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1819/*
1820 * Check if any character is available, also half an escape sequence.
1821 * Trick: when no typeahead found, but there is something in the typeahead
1822 * buffer, it must be an ESC that is recognized as the start of a key code.
1823 */
1824 int
1825vpeekc_any()
1826{
1827 int c;
1828
1829 c = vpeekc();
1830 if (c == NUL && typebuf.tb_len > 0)
1831 c = ESC;
1832 return c;
1833}
1834#endif
1835
1836/*
1837 * Call vpeekc() without causing anything to be mapped.
1838 * Return TRUE if a character is available, FALSE otherwise.
1839 */
1840 int
1841char_avail()
1842{
1843 int retval;
1844
1845 ++no_mapping;
1846 retval = vpeekc();
1847 --no_mapping;
1848 return (retval != NUL);
1849}
1850
1851 void
1852vungetc(c) /* unget one character (can only be done once!) */
1853 int c;
1854{
1855 old_char = c;
1856 old_mod_mask = mod_mask;
1857}
1858
1859/*
1860 * get a character:
1861 * 1. from the stuffbuffer
1862 * This is used for abbreviated commands like "D" -> "d$".
1863 * Also used to redo a command for ".".
1864 * 2. from the typeahead buffer
1865 * Stores text obtained previously but not used yet.
1866 * Also stores the result of mappings.
1867 * Also used for the ":normal" command.
1868 * 3. from the user
1869 * This may do a blocking wait if "advance" is TRUE.
1870 *
1871 * if "advance" is TRUE (vgetc()):
1872 * really get the character.
1873 * KeyTyped is set to TRUE in the case the user typed the key.
1874 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1875 * if "advance" is FALSE (vpeekc()):
1876 * just look whether there is a character available.
1877 *
1878 * When "no_mapping" is zero, checks for mappings in the current mode.
1879 * Only returns one byte (of a multi-byte character).
1880 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1881 */
1882 static int
1883vgetorpeek(advance)
1884 int advance;
1885{
1886 int c, c1;
1887 int keylen;
1888 char_u *s;
1889 mapblock_T *mp;
1890#ifdef FEAT_LOCALMAP
1891 mapblock_T *mp2;
1892#endif
1893 mapblock_T *mp_match;
1894 int mp_match_len = 0;
1895 int timedout = FALSE; /* waited for more than 1 second
1896 for mapping to complete */
1897 int mapdepth = 0; /* check for recursive mapping */
1898 int mode_deleted = FALSE; /* set when mode has been deleted */
1899 int local_State;
1900 int mlen;
1901 int max_mlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 int i;
Bram Moolenaar4e427192006-03-10 21:34:27 +00001903#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 int new_wcol, new_wrow;
1905#endif
1906#ifdef FEAT_GUI
1907# ifdef FEAT_MENU
1908 int idx;
1909# endif
1910 int shape_changed = FALSE; /* adjusted cursor shape */
1911#endif
1912 int n;
1913#ifdef FEAT_LANGMAP
1914 int nolmaplen;
1915#endif
1916 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00001917 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 /*
1920 * This function doesn't work very well when called recursively. This may
1921 * happen though, because of:
1922 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1923 * there is a character available, which calls this function. In that
1924 * case we must return NUL, to indicate no character is available.
1925 * 2. A GUI callback function writes to the screen, causing a
1926 * wait_return().
1927 * Using ":normal" can also do this, but it saves the typeahead buffer,
1928 * thus it should be OK. But don't get a key from the user then.
1929 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001930 if (vgetc_busy > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#ifdef FEAT_EX_EXTRA
1932 && ex_normal_busy == 0
1933#endif
1934 )
1935 return NUL;
1936
1937 local_State = get_real_state();
1938
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001939 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941 if (advance)
1942 KeyStuffed = FALSE;
1943
1944 init_typebuf();
1945 start_stuff();
1946 if (advance && typebuf.tb_maplen == 0)
1947 Exec_reg = FALSE;
1948 do
1949 {
1950/*
1951 * get a character: 1. from the stuffbuffer
1952 */
1953 if (typeahead_char != 0)
1954 {
1955 c = typeahead_char;
1956 if (advance)
1957 typeahead_char = 0;
1958 }
1959 else
1960 c = read_stuff(advance);
1961 if (c != NUL && !got_int)
1962 {
1963 if (advance)
1964 {
1965 /* KeyTyped = FALSE; When the command that stuffed something
1966 * was typed, behave like the stuffed command was typed.
1967 * needed for CTRL-W CTRl-] to open a fold, for example. */
1968 KeyStuffed = TRUE;
1969 }
1970 if (typebuf.tb_no_abbr_cnt == 0)
1971 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1972 }
1973 else
1974 {
1975 /*
1976 * Loop until we either find a matching mapped key, or we
1977 * are sure that it is not a mapped key.
1978 * If a mapped key sequence is found we go back to the start to
1979 * try re-mapping.
1980 */
1981 for (;;)
1982 {
1983 /*
1984 * ui_breakcheck() is slow, don't use it too often when
1985 * inside a mapping. But call it each time for typed
1986 * characters.
1987 */
1988 if (typebuf.tb_maplen)
1989 line_breakcheck();
1990 else
1991 ui_breakcheck(); /* check for CTRL-C */
1992 keylen = 0;
1993 if (got_int)
1994 {
1995 /* flush all input */
1996 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
1997 typebuf.tb_change_cnt);
1998 /*
1999 * If inchar() returns TRUE (script file was active) or we
2000 * are inside a mapping, get out of insert mode.
2001 * Otherwise we behave like having gotten a CTRL-C.
2002 * As a result typing CTRL-C in insert mode will
2003 * really insert a CTRL-C.
2004 */
2005 if ((c || typebuf.tb_maplen)
2006 && (State & (INSERT + CMDLINE)))
2007 c = ESC;
2008 else
2009 c = Ctrl_C;
2010 flush_buffers(TRUE); /* flush all typeahead */
2011
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002012 if (advance)
2013 {
2014 /* Also record this character, it might be needed to
2015 * get out of Insert mode. */
2016 *typebuf.tb_buf = c;
2017 gotchars(typebuf.tb_buf, 1);
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 cmd_silent = FALSE;
2020
2021 break;
2022 }
2023 else if (typebuf.tb_len > 0)
2024 {
2025 /*
2026 * Check for a mappable key sequence.
2027 * Walk through one maphash[] list until we find an
2028 * entry that matches.
2029 *
2030 * Don't look for mappings if:
2031 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2032 * - maphash_valid not set: no mappings present.
2033 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2034 * - in insert or cmdline mode and 'paste' option set
2035 * - waiting for "hit return to continue" and CR or SPACE
2036 * typed
2037 * - waiting for a char with --more--
2038 * - in Ctrl-X mode, and we get a valid char for that mode
2039 */
2040 mp = NULL;
2041 max_mlen = 0;
2042 c1 = typebuf.tb_buf[typebuf.tb_off];
2043 if (no_mapping == 0 && maphash_valid
2044 && (no_zero_mapping == 0 || c1 != '0')
2045 && (typebuf.tb_maplen == 0
2046 || (p_remap
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002047 && (typebuf.tb_noremap[typebuf.tb_off]
2048 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 && !(p_paste && (State & (INSERT + CMDLINE)))
2050 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2051 && State != ASKMORE
2052 && State != CONFIRM
2053#ifdef FEAT_INS_EXPAND
2054 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002055 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2057#endif
2058 )
2059 {
2060#ifdef FEAT_LANGMAP
2061 if (c1 == K_SPECIAL)
2062 nolmaplen = 2;
2063 else
2064 {
2065 LANGMAP_ADJUST(c1, TRUE);
2066 nolmaplen = 0;
2067 }
2068#endif
2069#ifdef FEAT_LOCALMAP
2070 /* First try buffer-local mappings. */
2071 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2072 mp2 = maphash[MAP_HASH(local_State, c1)];
2073 if (mp == NULL)
2074 {
2075 mp = mp2;
2076 mp2 = NULL;
2077 }
2078#else
2079 mp = maphash[MAP_HASH(local_State, c1)];
2080#endif
2081 /*
2082 * Loop until a partly matching mapping is found or
2083 * all (local) mappings have been checked.
2084 * The longest full match is remembered in "mp_match".
2085 * A full match is only accepted if there is no partly
2086 * match, so "aa" and "aaa" can both be mapped.
2087 */
2088 mp_match = NULL;
2089 mp_match_len = 0;
2090 for ( ; mp != NULL;
2091#ifdef FEAT_LOCALMAP
2092 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2093#endif
2094 (mp = mp->m_next))
2095 {
2096 /*
2097 * Only consider an entry if the first character
2098 * matches and it is for the current state.
2099 * Skip ":lmap" mappings if keys were mapped.
2100 */
2101 if (mp->m_keys[0] == c1
2102 && (mp->m_mode & local_State)
2103 && ((mp->m_mode & LANGMAP) == 0
2104 || typebuf.tb_maplen == 0))
2105 {
2106#ifdef FEAT_LANGMAP
2107 int nomap = nolmaplen;
2108 int c2;
2109#endif
2110 /* find the match length of this mapping */
2111 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2112 {
2113#ifdef FEAT_LANGMAP
2114 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2115 if (nomap > 0)
2116 --nomap;
2117 else if (c2 == K_SPECIAL)
2118 nomap = 2;
2119 else
2120 LANGMAP_ADJUST(c2, TRUE);
2121 if (mp->m_keys[mlen] != c2)
2122#else
2123 if (mp->m_keys[mlen] !=
2124 typebuf.tb_buf[typebuf.tb_off + mlen])
2125#endif
2126 break;
2127 }
2128
2129#ifdef FEAT_MBYTE
2130 /* Don't allow mapping the first byte(s) of a
2131 * multi-byte char. Happens when mapping
2132 * <M-a> and then changing 'encoding'. */
2133 if (has_mbyte && MB_BYTE2LEN(c1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002134 > (*mb_ptr2len)(mp->m_keys))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 mlen = 0;
2136#endif
2137 /*
2138 * Check an entry whether it matches.
2139 * - Full match: mlen == keylen
2140 * - Partly match: mlen == typebuf.tb_len
2141 */
2142 keylen = mp->m_keylen;
2143 if (mlen == keylen
2144 || (mlen == typebuf.tb_len
2145 && typebuf.tb_len < keylen))
2146 {
2147 /*
2148 * If only script-local mappings are
2149 * allowed, check if the mapping starts
2150 * with K_SNR.
2151 */
2152 s = typebuf.tb_noremap + typebuf.tb_off;
2153 if (*s == RM_SCRIPT
2154 && (mp->m_keys[0] != K_SPECIAL
2155 || mp->m_keys[1] != KS_EXTRA
2156 || mp->m_keys[2]
2157 != (int)KE_SNR))
2158 continue;
2159 /*
2160 * If one of the typed keys cannot be
2161 * remapped, skip the entry.
2162 */
2163 for (n = mlen; --n >= 0; )
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002164 if (*s++ & (RM_NONE|RM_ABBR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 break;
2166 if (n >= 0)
2167 continue;
2168
2169 if (keylen > typebuf.tb_len)
2170 {
2171 if (!timedout)
2172 {
2173 /* break at a partly match */
2174 keylen = KL_PART_MAP;
2175 break;
2176 }
2177 }
2178 else if (keylen > mp_match_len)
2179 {
2180 /* found a longer match */
2181 mp_match = mp;
2182 mp_match_len = keylen;
2183 }
2184 }
2185 else
2186 /* No match; may have to check for
2187 * termcode at next character. */
2188 if (max_mlen < mlen)
2189 max_mlen = mlen;
2190 }
2191 }
2192
2193 /* If no partly match found, use the longest full
2194 * match. */
2195 if (keylen != KL_PART_MAP)
2196 {
2197 mp = mp_match;
2198 keylen = mp_match_len;
2199 }
2200 }
2201
2202 /* Check for match with 'pastetoggle' */
2203 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2204 {
2205 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2206 ++mlen)
2207 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2208 + mlen])
2209 break;
2210 if (p_pt[mlen] == NUL) /* match */
2211 {
2212 /* write chars to script file(s) */
2213 if (mlen > typebuf.tb_maplen)
2214 gotchars(typebuf.tb_buf + typebuf.tb_off
2215 + typebuf.tb_maplen,
2216 mlen - typebuf.tb_maplen);
2217
2218 del_typebuf(mlen, 0); /* remove the chars */
2219 set_option_value((char_u *)"paste",
2220 (long)!p_paste, NULL, 0);
2221 if (!(State & INSERT))
2222 {
2223 msg_col = 0;
2224 msg_row = Rows - 1;
2225 msg_clr_eos(); /* clear ruler */
2226 }
2227 showmode();
2228 setcursor();
2229 continue;
2230 }
2231 /* Need more chars for partly match. */
2232 if (mlen == typebuf.tb_len)
Bram Moolenaar4076b792007-04-26 14:48:01 +00002233 keylen = KL_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else if (max_mlen < mlen)
2235 /* no match, may have to check for termcode at
2236 * next character */
2237 max_mlen = mlen + 1;
2238 }
2239
2240 if ((mp == NULL || max_mlen >= mp_match_len)
2241 && keylen != KL_PART_MAP)
2242 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002243 int save_keylen = keylen;
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 /*
2246 * When no matching mapping found or found a
2247 * non-matching mapping that matches at least what the
2248 * matching mapping matched:
2249 * Check if we have a terminal code, when:
2250 * mapping is allowed,
2251 * keys have not been mapped,
2252 * and not an ESC sequence, not in insert mode or
2253 * p_ek is on,
2254 * and when not timed out,
2255 */
2256 if ((no_mapping == 0 || allow_keys != 0)
2257 && (typebuf.tb_maplen == 0
2258 || (p_remap && typebuf.tb_noremap[
2259 typebuf.tb_off] == RM_YES))
2260 && !timedout)
2261 {
2262 keylen = check_termcode(max_mlen + 1, NULL, 0);
2263
Bram Moolenaarf2330482008-06-24 20:19:36 +00002264 /* If no termcode matched but 'pastetoggle'
2265 * matched partially it's like an incomplete key
2266 * sequence. */
2267 if (keylen == 0 && save_keylen == KL_PART_KEY)
2268 keylen = KL_PART_KEY;
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /*
2271 * When getting a partial match, but the last
2272 * characters were not typed, don't wait for a
2273 * typed character to complete the termcode.
2274 * This helps a lot when a ":normal" command ends
2275 * in an ESC.
2276 */
2277 if (keylen < 0
2278 && typebuf.tb_len == typebuf.tb_maplen)
2279 keylen = 0;
2280 }
2281 else
2282 keylen = 0;
2283 if (keylen == 0) /* no matching terminal code */
2284 {
2285#ifdef AMIGA /* check for window bounds report */
2286 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2287 typebuf.tb_off] & 0xff) == CSI)
2288 {
2289 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2290 s < typebuf.tb_buf + typebuf.tb_off
2291 + typebuf.tb_len
2292 && (VIM_ISDIGIT(*s) || *s == ';'
2293 || *s == ' ');
2294 ++s)
2295 ;
2296 if (*s == 'r' || *s == '|') /* found one */
2297 {
2298 del_typebuf((int)(s + 1 -
2299 (typebuf.tb_buf + typebuf.tb_off)), 0);
2300 /* get size and redraw screen */
2301 shell_resized();
2302 continue;
2303 }
2304 if (*s == NUL) /* need more characters */
2305 keylen = KL_PART_KEY;
2306 }
2307 if (keylen >= 0)
2308#endif
2309 /* When there was a matching mapping and no
2310 * termcode could be replaced after another one,
Bram Moolenaarf2330482008-06-24 20:19:36 +00002311 * use that mapping (loop around). If there was
2312 * no mapping use the character from the
2313 * typeahead buffer right here. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (mp == NULL)
2315 {
2316/*
2317 * get a character: 2. from the typeahead buffer
2318 */
2319 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2320 if (advance) /* remove chars from tb_buf */
2321 {
2322 cmd_silent = (typebuf.tb_silent > 0);
2323 if (typebuf.tb_maplen > 0)
2324 KeyTyped = FALSE;
2325 else
2326 {
2327 KeyTyped = TRUE;
2328 /* write char to script file(s) */
2329 gotchars(typebuf.tb_buf
2330 + typebuf.tb_off, 1);
2331 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00002332 KeyNoremap = typebuf.tb_noremap[
2333 typebuf.tb_off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 del_typebuf(1, 0);
2335 }
2336 break; /* got character, break for loop */
2337 }
2338 }
2339 if (keylen > 0) /* full matching terminal code */
2340 {
2341#if defined(FEAT_GUI) && defined(FEAT_MENU)
2342 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2343 && typebuf.tb_buf[typebuf.tb_off + 1]
2344 == KS_MENU)
2345 {
2346 /*
2347 * Using a menu may cause a break in undo!
2348 * It's like using gotchars(), but without
2349 * recording or writing to a script file.
2350 */
2351 may_sync_undo();
2352 del_typebuf(3, 0);
2353 idx = get_menu_index(current_menu, local_State);
2354 if (idx != MENU_INDEX_INVALID)
2355 {
2356# ifdef FEAT_VISUAL
2357 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002358 * In Select mode and a Visual mode menu
2359 * is used: Switch to Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 * temporarily. Append K_SELECT to switch
2361 * back to Select mode.
2362 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002363 if (VIsual_active && VIsual_select
2364 && (current_menu->modes & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 VIsual_select = FALSE;
2367 (void)ins_typebuf(K_SELECT_STRING,
2368 REMAP_NONE, 0, TRUE, FALSE);
2369 }
2370# endif
2371 ins_typebuf(current_menu->strings[idx],
2372 current_menu->noremap[idx],
2373 0, TRUE,
2374 current_menu->silent[idx]);
2375 }
2376 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002377#endif /* FEAT_GUI && FEAT_MENU */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 continue; /* try mapping again */
2379 }
2380
2381 /* Partial match: get some more characters. When a
2382 * matching mapping was found use that one. */
2383 if (mp == NULL || keylen < 0)
2384 keylen = KL_PART_KEY;
2385 else
2386 keylen = mp_match_len;
2387 }
2388
2389 /* complete match */
2390 if (keylen >= 0 && keylen <= typebuf.tb_len)
2391 {
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002392#ifdef FEAT_EVAL
2393 int save_m_expr;
2394 int save_m_noremap;
2395 int save_m_silent;
2396 char_u *save_m_keys;
2397 char_u *save_m_str;
2398#else
2399# define save_m_noremap mp->m_noremap
2400# define save_m_silent mp->m_silent
2401#endif
2402
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 /* write chars to script file(s) */
2404 if (keylen > typebuf.tb_maplen)
2405 gotchars(typebuf.tb_buf + typebuf.tb_off
2406 + typebuf.tb_maplen,
2407 keylen - typebuf.tb_maplen);
2408
2409 cmd_silent = (typebuf.tb_silent > 0);
2410 del_typebuf(keylen, 0); /* remove the mapped keys */
2411
2412 /*
2413 * Put the replacement string in front of mapstr.
2414 * The depth check catches ":map x y" and ":map y x".
2415 */
2416 if (++mapdepth >= p_mmd)
2417 {
2418 EMSG(_("E223: recursive mapping"));
2419 if (State & CMDLINE)
2420 redrawcmdline();
2421 else
2422 setcursor();
2423 flush_buffers(FALSE);
2424 mapdepth = 0; /* for next one */
2425 c = -1;
2426 break;
2427 }
2428
2429#ifdef FEAT_VISUAL
2430 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002431 * In Select mode and a Visual mode mapping is used:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 * Switch to Visual mode temporarily. Append K_SELECT
2433 * to switch back to Select mode.
2434 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002435 if (VIsual_active && VIsual_select
2436 && (mp->m_mode & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 VIsual_select = FALSE;
2439 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2440 0, TRUE, FALSE);
2441 }
2442#endif
2443
Bram Moolenaar4e427192006-03-10 21:34:27 +00002444#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002445 /* Copy the values from *mp that are used, because
2446 * evaluating the expression may invoke a function
2447 * that redefines the mapping, thereby making *mp
2448 * invalid. */
2449 save_m_expr = mp->m_expr;
2450 save_m_noremap = mp->m_noremap;
2451 save_m_silent = mp->m_silent;
2452 save_m_keys = NULL; /* only saved when needed */
2453 save_m_str = NULL; /* only saved when needed */
2454
Bram Moolenaar4e427192006-03-10 21:34:27 +00002455 /*
2456 * Handle ":map <expr>": evaluate the {rhs} as an
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002457 * expression. Save and restore the typeahead so that
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002458 * getchar() can be used. Also save and restore the
2459 * command line for "normal :".
Bram Moolenaar4e427192006-03-10 21:34:27 +00002460 */
2461 if (mp->m_expr)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002462 {
2463 tasave_T tabuf;
2464 int save_vgetc_busy = vgetc_busy;
2465
2466 save_typeahead(&tabuf);
2467 if (tabuf.typebuf_valid)
2468 {
2469 vgetc_busy = 0;
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002470 save_m_keys = vim_strsave(mp->m_keys);
2471 save_m_str = vim_strsave(mp->m_str);
2472 s = eval_map_expr(save_m_str, NUL);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002473 vgetc_busy = save_vgetc_busy;
2474 }
2475 else
2476 s = NULL;
2477 restore_typeahead(&tabuf);
2478 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00002479 else
2480#endif
2481 s = mp->m_str;
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 /*
2484 * Insert the 'to' part in the typebuf.tb_buf.
2485 * If 'from' field is the same as the start of the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002486 * 'to' field, don't remap the first character (but do
2487 * allow abbreviations).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 * If m_noremap is set, don't remap the whole 'to'
2489 * part.
2490 */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002491 if (s == NULL)
2492 i = FAIL;
2493 else
2494 {
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002495 int noremap;
2496
2497 if (save_m_noremap != REMAP_YES)
2498 noremap = save_m_noremap;
2499 else if (
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002500#ifdef FEAT_EVAL
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002501 STRNCMP(s, save_m_keys != NULL
2502 ? save_m_keys : mp->m_keys,
2503 (size_t)keylen)
2504#else
2505 STRNCMP(s, mp->m_keys, (size_t)keylen)
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002506#endif
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002507 != 0)
2508 noremap = REMAP_YES;
2509 else
2510 noremap = REMAP_SKIP;
2511 i = ins_typebuf(s, noremap,
2512 0, TRUE, cmd_silent || save_m_silent);
Bram Moolenaar4e427192006-03-10 21:34:27 +00002513#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002514 if (save_m_expr)
Bram Moolenaar4e427192006-03-10 21:34:27 +00002515 vim_free(s);
2516#endif
2517 }
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002518#ifdef FEAT_EVAL
2519 vim_free(save_m_keys);
2520 vim_free(save_m_str);
2521#endif
Bram Moolenaar4e427192006-03-10 21:34:27 +00002522 if (i == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
2524 c = -1;
2525 break;
2526 }
2527 continue;
2528 }
2529 }
2530
2531/*
2532 * get a character: 3. from the user - handle <Esc> in Insert mode
2533 */
2534 /*
2535 * special case: if we get an <ESC> in insert mode and there
2536 * are no more characters at once, we pretend to go out of
2537 * insert mode. This prevents the one second delay after
2538 * typing an <ESC>. If we get something after all, we may
2539 * have to redisplay the mode. That the cursor is in the wrong
2540 * place does not matter.
2541 */
2542 c = 0;
2543#ifdef FEAT_CMDL_INFO
2544 new_wcol = curwin->w_wcol;
2545 new_wrow = curwin->w_wrow;
2546#endif
2547 if ( advance
2548 && typebuf.tb_len == 1
2549 && typebuf.tb_buf[typebuf.tb_off] == ESC
2550 && !no_mapping
2551#ifdef FEAT_EX_EXTRA
2552 && ex_normal_busy == 0
2553#endif
2554 && typebuf.tb_maplen == 0
2555 && (State & INSERT)
2556 && (p_timeout || (keylen == KL_PART_KEY && p_ttimeout))
2557 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2558 + typebuf.tb_len, 3, 25L,
2559 typebuf.tb_change_cnt)) == 0)
2560 {
2561 colnr_T col = 0, vcol;
2562 char_u *ptr;
2563
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002564 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 unshowmode(TRUE);
2567 mode_deleted = TRUE;
2568 }
2569#ifdef FEAT_GUI
2570 /* may show different cursor shape */
2571 if (gui.in_use)
2572 {
2573 int save_State;
2574
2575 save_State = State;
2576 State = NORMAL;
2577 gui_update_cursor(TRUE, FALSE);
2578 State = save_State;
2579 shape_changed = TRUE;
2580 }
2581#endif
2582 validate_cursor();
2583 old_wcol = curwin->w_wcol;
2584 old_wrow = curwin->w_wrow;
2585
2586 /* move cursor left, if possible */
2587 if (curwin->w_cursor.col != 0)
2588 {
2589 if (curwin->w_wcol > 0)
2590 {
2591 if (did_ai)
2592 {
2593 /*
2594 * We are expecting to truncate the trailing
2595 * white-space, so find the last non-white
2596 * character -- webb
2597 */
2598 col = vcol = curwin->w_wcol = 0;
2599 ptr = ml_get_curline();
2600 while (col < curwin->w_cursor.col)
2601 {
2602 if (!vim_iswhite(ptr[col]))
2603 curwin->w_wcol = vcol;
2604 vcol += lbr_chartabsize(ptr + col,
2605 (colnr_T)vcol);
2606#ifdef FEAT_MBYTE
2607 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002608 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 else
2610#endif
2611 ++col;
2612 }
2613 curwin->w_wrow = curwin->w_cline_row
2614 + curwin->w_wcol / W_WIDTH(curwin);
2615 curwin->w_wcol %= W_WIDTH(curwin);
2616 curwin->w_wcol += curwin_col_off();
2617#ifdef FEAT_MBYTE
2618 col = 0; /* no correction needed */
2619#endif
2620 }
2621 else
2622 {
2623 --curwin->w_wcol;
2624#ifdef FEAT_MBYTE
2625 col = curwin->w_cursor.col - 1;
2626#endif
2627 }
2628 }
2629 else if (curwin->w_p_wrap && curwin->w_wrow)
2630 {
2631 --curwin->w_wrow;
2632 curwin->w_wcol = W_WIDTH(curwin) - 1;
2633#ifdef FEAT_MBYTE
2634 col = curwin->w_cursor.col - 1;
2635#endif
2636 }
2637#ifdef FEAT_MBYTE
2638 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2639 {
2640 /* Correct when the cursor is on the right halve
2641 * of a double-wide character. */
2642 ptr = ml_get_curline();
2643 col -= (*mb_head_off)(ptr, ptr + col);
2644 if ((*mb_ptr2cells)(ptr + col) > 1)
2645 --curwin->w_wcol;
2646 }
2647#endif
2648 }
2649 setcursor();
2650 out_flush();
2651#ifdef FEAT_CMDL_INFO
2652 new_wcol = curwin->w_wcol;
2653 new_wrow = curwin->w_wrow;
2654#endif
2655 curwin->w_wcol = old_wcol;
2656 curwin->w_wrow = old_wrow;
2657 }
2658 if (c < 0)
2659 continue; /* end of input script reached */
2660 typebuf.tb_len += c;
2661
2662 /* buffer full, don't map */
2663 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2664 {
2665 timedout = TRUE;
2666 continue;
2667 }
2668
2669#ifdef FEAT_EX_EXTRA
2670 if (ex_normal_busy > 0)
2671 {
2672# ifdef FEAT_CMDWIN
2673 static int tc = 0;
2674# endif
2675
2676 /* No typeahead left and inside ":normal". Must return
2677 * something to avoid getting stuck. When an incomplete
2678 * mapping is present, behave like it timed out. */
2679 if (typebuf.tb_len > 0)
2680 {
2681 timedout = TRUE;
2682 continue;
2683 }
2684 /* When 'insertmode' is set, ESC just beeps in Insert
2685 * mode. Use CTRL-L to make edit() return.
2686 * For the command line only CTRL-C always breaks it.
2687 * For the cmdline window: Alternate between ESC and
2688 * CTRL-C: ESC for most situations and CTRL-C to close the
2689 * cmdline window. */
2690 if (p_im && (State & INSERT))
2691 c = Ctrl_L;
2692 else if ((State & CMDLINE)
2693# ifdef FEAT_CMDWIN
2694 || (cmdwin_type > 0 && tc == ESC)
2695# endif
2696 )
2697 c = Ctrl_C;
2698 else
2699 c = ESC;
2700# ifdef FEAT_CMDWIN
2701 tc = c;
2702# endif
2703 break;
2704 }
2705#endif
2706
2707/*
2708 * get a character: 3. from the user - update display
2709 */
2710 /* In insert mode a screen update is skipped when characters
2711 * are still available. But when those available characters
2712 * are part of a mapping, and we are going to do a blocking
2713 * wait here. Need to update the screen to display the
2714 * changed text so far. */
2715 if ((State & INSERT) && advance && must_redraw != 0)
2716 {
2717 update_screen(0);
2718 setcursor(); /* put cursor back where it belongs */
2719 }
2720
2721 /*
2722 * If we have a partial match (and are going to wait for more
2723 * input from the user), show the partially matched characters
2724 * to the user with showcmd.
2725 */
2726#ifdef FEAT_CMDL_INFO
2727 i = 0;
2728#endif
2729 c1 = 0;
2730 if (typebuf.tb_len > 0 && advance && !exmode_active)
2731 {
2732 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2733 && State != HITRETURN)
2734 {
2735 /* this looks nice when typing a dead character map */
2736 if (State & INSERT
2737 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2738 + typebuf.tb_len - 1) == 1)
2739 {
2740 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2741 + typebuf.tb_len - 1], FALSE);
2742 setcursor(); /* put cursor back where it belongs */
2743 c1 = 1;
2744 }
2745#ifdef FEAT_CMDL_INFO
2746 /* need to use the col and row from above here */
2747 old_wcol = curwin->w_wcol;
2748 old_wrow = curwin->w_wrow;
2749 curwin->w_wcol = new_wcol;
2750 curwin->w_wrow = new_wrow;
2751 push_showcmd();
2752 if (typebuf.tb_len > SHOWCMD_COLS)
2753 i = typebuf.tb_len - SHOWCMD_COLS;
2754 while (i < typebuf.tb_len)
2755 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2756 + i++]);
2757 curwin->w_wcol = old_wcol;
2758 curwin->w_wrow = old_wrow;
2759#endif
2760 }
2761
2762 /* this looks nice when typing a dead character map */
2763 if ((State & CMDLINE)
2764#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2765 && cmdline_star == 0
2766#endif
2767 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2768 + typebuf.tb_len - 1) == 1)
2769 {
2770 putcmdline(typebuf.tb_buf[typebuf.tb_off
2771 + typebuf.tb_len - 1], FALSE);
2772 c1 = 1;
2773 }
2774 }
2775
2776/*
2777 * get a character: 3. from the user - get it
2778 */
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002779 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2781 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2782 !advance
2783 ? 0
2784 : ((typebuf.tb_len == 0
2785 || !(p_timeout || (p_ttimeout
2786 && keylen == KL_PART_KEY)))
2787 ? -1L
2788 : ((keylen == KL_PART_KEY && p_ttm >= 0)
2789 ? p_ttm
2790 : p_tm)), typebuf.tb_change_cnt);
2791
2792#ifdef FEAT_CMDL_INFO
2793 if (i != 0)
2794 pop_showcmd();
2795#endif
2796 if (c1 == 1)
2797 {
2798 if (State & INSERT)
2799 edit_unputchar();
2800 if (State & CMDLINE)
2801 unputcmdline();
2802 setcursor(); /* put cursor back where it belongs */
2803 }
2804
2805 if (c < 0)
2806 continue; /* end of input script reached */
2807 if (c == NUL) /* no character available */
2808 {
2809 if (!advance)
2810 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002811 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
2813 timedout = TRUE;
2814 continue;
2815 }
2816 }
2817 else
2818 { /* allow mapping for just typed characters */
2819 while (typebuf.tb_buf[typebuf.tb_off
2820 + typebuf.tb_len] != NUL)
2821 typebuf.tb_noremap[typebuf.tb_off
2822 + typebuf.tb_len++] = RM_YES;
2823#ifdef USE_IM_CONTROL
2824 /* Get IM status right after getting keys, not after the
2825 * timeout for a mapping (focus may be lost by then). */
2826 vgetc_im_active = im_get_status();
2827#endif
2828 }
2829 } /* for (;;) */
2830 } /* if (!character from stuffbuf) */
2831
2832 /* if advance is FALSE don't loop on NULs */
2833 } while (c < 0 || (advance && c == NUL));
2834
2835 /*
2836 * The "INSERT" message is taken care of here:
2837 * if we return an ESC to exit insert mode, the message is deleted
2838 * if we don't return an ESC but deleted the message before, redisplay it
2839 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002840 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002842 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
2844 if (typebuf.tb_len && !KeyTyped)
2845 redraw_cmdline = TRUE; /* delete mode later */
2846 else
2847 unshowmode(FALSE);
2848 }
2849 else if (c != ESC && mode_deleted)
2850 {
2851 if (typebuf.tb_len && !KeyTyped)
2852 redraw_cmdline = TRUE; /* show mode later */
2853 else
2854 showmode();
2855 }
2856 }
2857#ifdef FEAT_GUI
2858 /* may unshow different cursor shape */
2859 if (gui.in_use && shape_changed)
2860 gui_update_cursor(TRUE, FALSE);
2861#endif
2862
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002863 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
2865 return c;
2866}
2867
2868/*
2869 * inchar() - get one character from
2870 * 1. a scriptfile
2871 * 2. the keyboard
2872 *
2873 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2874 * NUL terminated (buffer length must be 'maxlen' + 1).
2875 * Minimum for "maxlen" is 3!!!!
2876 *
2877 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2878 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2879 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2880 * otherwise.
2881 *
2882 * If we got an interrupt all input is read until none is available.
2883 *
2884 * If wait_time == 0 there is no waiting for the char.
2885 * If wait_time == n we wait for n msec for a character to arrive.
2886 * If wait_time == -1 we wait forever for a character to arrive.
2887 *
2888 * Return the number of obtained characters.
2889 * Return -1 when end of input script reached.
2890 */
2891 int
2892inchar(buf, maxlen, wait_time, tb_change_cnt)
2893 char_u *buf;
2894 int maxlen;
2895 long wait_time; /* milli seconds */
2896 int tb_change_cnt;
2897{
2898 int len = 0; /* init for GCC */
2899 int retesc = FALSE; /* return ESC with gotint */
2900 int script_char;
2901
2902 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2903 {
2904 cursor_on();
2905 out_flush();
2906#ifdef FEAT_GUI
2907 if (gui.in_use)
2908 {
2909 gui_update_cursor(FALSE, FALSE);
2910# ifdef FEAT_MOUSESHAPE
2911 if (postponed_mouseshape)
2912 update_mouseshape(-1);
2913# endif
2914 }
2915#endif
2916 }
2917
2918 /*
2919 * Don't reset these when at the hit-return prompt, otherwise a endless
2920 * recursive loop may result (write error in swapfile, hit-return, timeout
2921 * on char wait, flush swapfile, write error....).
2922 */
2923 if (State != HITRETURN)
2924 {
2925 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2926 did_swapwrite_msg = FALSE; /* display swap file write error again */
2927 }
2928 undo_off = FALSE; /* restart undo now */
2929
2930 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002931 * Get a character from a script file if there is one.
2932 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 */
2934 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002935 while (scriptin[curscript] != NULL && script_char < 0
2936#ifdef FEAT_EVAL
2937 && !ignore_script
2938#endif
2939 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002941
2942#if defined(FEAT_NETBEANS_INTG)
2943 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002944 netbeans_parse_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00002945#endif
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2948 {
2949 /* Reached EOF.
2950 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2951 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2952 closescript();
2953 /*
2954 * When reading script file is interrupted, return an ESC to get
2955 * back to normal mode.
2956 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2957 */
2958 if (got_int)
2959 retesc = TRUE;
2960 else
2961 return -1;
2962 }
2963 else
2964 {
2965 buf[0] = script_char;
2966 len = 1;
2967 }
2968 }
2969
2970 if (script_char < 0) /* did not get a character from script */
2971 {
2972 /*
2973 * If we got an interrupt, skip all previously typed characters and
2974 * return TRUE if quit reading script file.
2975 * Stop reading typeahead when a single CTRL-C was read,
2976 * fill_input_buf() returns this when not able to read from stdin.
2977 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2978 * and buf may be pointing inside typebuf.tb_buf[].
2979 */
2980 if (got_int)
2981 {
2982#define DUM_LEN MAXMAPLEN * 3 + 3
2983 char_u dum[DUM_LEN + 1];
2984
2985 for (;;)
2986 {
2987 len = ui_inchar(dum, DUM_LEN, 0L, 0);
2988 if (len == 0 || (len == 1 && dum[0] == 3))
2989 break;
2990 }
2991 return retesc;
2992 }
2993
2994 /*
2995 * Always flush the output characters when getting input characters
2996 * from the user.
2997 */
2998 out_flush();
2999
3000 /*
3001 * Fill up to a third of the buffer, because each character may be
3002 * tripled below.
3003 */
3004 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3005 }
3006
3007 if (typebuf_changed(tb_change_cnt))
3008 return 0;
3009
3010 return fix_input_buffer(buf, len, script_char >= 0);
3011}
3012
3013/*
3014 * Fix typed characters for use by vgetc() and check_termcode().
3015 * buf[] must have room to triple the number of bytes!
3016 * Returns the new length.
3017 */
3018 int
3019fix_input_buffer(buf, len, script)
3020 char_u *buf;
3021 int len;
3022 int script; /* TRUE when reading from a script */
3023{
3024 int i;
3025 char_u *p = buf;
3026
3027 /*
3028 * Two characters are special: NUL and K_SPECIAL.
3029 * When compiled With the GUI CSI is also special.
3030 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3031 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3032 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
3033 * Don't replace K_SPECIAL when reading a script file.
3034 */
3035 for (i = len; --i >= 0; ++p)
3036 {
3037#ifdef FEAT_GUI
3038 /* When the GUI is used any character can come after a CSI, don't
3039 * escape it. */
3040 if (gui.in_use && p[0] == CSI && i >= 2)
3041 {
3042 p += 2;
3043 i -= 2;
3044 }
3045 /* When the GUI is not used CSI needs to be escaped. */
3046 else if (!gui.in_use && p[0] == CSI)
3047 {
3048 mch_memmove(p + 3, p + 1, (size_t)i);
3049 *p++ = K_SPECIAL;
3050 *p++ = KS_EXTRA;
3051 *p = (int)KE_CSI;
3052 len += 2;
3053 }
3054 else
3055#endif
3056 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003057#ifdef FEAT_AUTOCMD
3058 /* timeout may generate K_CURSORHOLD */
3059 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
3060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#if defined(WIN3264) && !defined(FEAT_GUI)
3062 /* Win32 console passes modifiers */
3063 && (i < 2 || p[1] != KS_MODIFIER)
3064#endif
3065 ))
3066 {
3067 mch_memmove(p + 3, p + 1, (size_t)i);
3068 p[2] = K_THIRD(p[0]);
3069 p[1] = K_SECOND(p[0]);
3070 p[0] = K_SPECIAL;
3071 p += 2;
3072 len += 2;
3073 }
3074 }
3075 *p = NUL; /* add trailing NUL */
3076 return len;
3077}
3078
3079#if defined(USE_INPUT_BUF) || defined(PROTO)
3080/*
3081 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3082 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003083 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003084 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
3086 int
3087input_available()
3088{
3089 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003090# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3091 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092# endif
3093 );
3094}
3095#endif
3096
3097/*
3098 * map[!] : show all key mappings
3099 * map[!] {lhs} : show key mapping for {lhs}
3100 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3101 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3102 * unmap[!] {lhs} : remove key mapping for {lhs}
3103 * abbr : show all abbreviations
3104 * abbr {lhs} : show abbreviations for {lhs}
3105 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3106 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3107 * unabbr {lhs} : remove abbreviation for {lhs}
3108 *
3109 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3110 *
3111 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3112 * it will be modified.
3113 *
Bram Moolenaar371d5402006-03-20 21:47:49 +00003114 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 * for :map! mode is INSERT + CMDLINE
3116 * for :cmap mode is CMDLINE
3117 * for :imap mode is INSERT
3118 * for :lmap mode is LANGMAP
3119 * for :nmap mode is NORMAL
Bram Moolenaar371d5402006-03-20 21:47:49 +00003120 * for :vmap mode is VISUAL + SELECTMODE
3121 * for :xmap mode is VISUAL
3122 * for :smap mode is SELECTMODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * for :omap mode is OP_PENDING
3124 *
3125 * for :abbr mode is INSERT + CMDLINE
3126 * for :iabbr mode is INSERT
3127 * for :cabbr mode is CMDLINE
3128 *
3129 * Return 0 for success
3130 * 1 for invalid arguments
3131 * 2 for no match
3132 * 4 for out of mem
3133 * 5 for entry not unique
3134 */
3135 int
3136do_map(maptype, arg, mode, abbrev)
3137 int maptype;
3138 char_u *arg;
3139 int mode;
3140 int abbrev; /* not a mapping but an abbreviation */
3141{
3142 char_u *keys;
3143 mapblock_T *mp, **mpp;
3144 char_u *rhs;
3145 char_u *p;
3146 int n;
3147 int len = 0; /* init for GCC */
3148 char_u *newstr;
3149 int hasarg;
3150 int haskey;
3151 int did_it = FALSE;
3152#ifdef FEAT_LOCALMAP
3153 int did_local = FALSE;
3154#endif
3155 int round;
3156 char_u *keys_buf = NULL;
3157 char_u *arg_buf = NULL;
3158 int retval = 0;
3159 int do_backslash;
3160 int hash;
3161 int new_hash;
3162 mapblock_T **abbr_table;
3163 mapblock_T **map_table;
3164 int unique = FALSE;
3165 int silent = FALSE;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003166 int special = FALSE;
Bram Moolenaar4e427192006-03-10 21:34:27 +00003167#ifdef FEAT_EVAL
3168 int expr = FALSE;
3169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 int noremap;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003171 char_u *orig_rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 keys = arg;
3174 map_table = maphash;
3175 abbr_table = &first_abbr;
3176
3177 /* For ":noremap" don't remap, otherwise do remap. */
3178 if (maptype == 2)
3179 noremap = REMAP_NONE;
3180 else
3181 noremap = REMAP_YES;
3182
Bram Moolenaar4e427192006-03-10 21:34:27 +00003183 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 for (;;)
3185 {
3186#ifdef FEAT_LOCALMAP
3187 /*
3188 * Check for "<buffer>": mapping local to buffer.
3189 */
3190 if (STRNCMP(keys, "<buffer>", 8) == 0)
3191 {
3192 keys = skipwhite(keys + 8);
3193 map_table = curbuf->b_maphash;
3194 abbr_table = &curbuf->b_first_abbr;
3195 continue;
3196 }
3197#endif
3198
3199 /*
3200 * Check for "<silent>": don't echo commands.
3201 */
3202 if (STRNCMP(keys, "<silent>", 8) == 0)
3203 {
3204 keys = skipwhite(keys + 8);
3205 silent = TRUE;
3206 continue;
3207 }
3208
Bram Moolenaar9c102382006-05-03 21:26:49 +00003209 /*
3210 * Check for "<special>": accept special keys in <>
3211 */
3212 if (STRNCMP(keys, "<special>", 9) == 0)
3213 {
3214 keys = skipwhite(keys + 9);
3215 special = TRUE;
3216 continue;
3217 }
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#ifdef FEAT_EVAL
3220 /*
3221 * Check for "<script>": remap script-local mappings only
3222 */
3223 if (STRNCMP(keys, "<script>", 8) == 0)
3224 {
3225 keys = skipwhite(keys + 8);
3226 noremap = REMAP_SCRIPT;
3227 continue;
3228 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00003229
3230 /*
3231 * Check for "<expr>": {rhs} is an expression.
3232 */
3233 if (STRNCMP(keys, "<expr>", 6) == 0)
3234 {
3235 keys = skipwhite(keys + 6);
3236 expr = TRUE;
3237 continue;
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#endif
3240 /*
3241 * Check for "<unique>": don't overwrite an existing mapping.
3242 */
3243 if (STRNCMP(keys, "<unique>", 8) == 0)
3244 {
3245 keys = skipwhite(keys + 8);
3246 unique = TRUE;
3247 continue;
3248 }
3249 break;
3250 }
3251
3252 validate_maphash();
3253
3254 /*
3255 * find end of keys and skip CTRL-Vs (and backslashes) in it
3256 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
3257 * with :unmap white space is included in the keys, no argument possible
3258 */
3259 p = keys;
3260 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3261 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3262 {
3263 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3264 p[1] != NUL)
3265 ++p; /* skip CTRL-V or backslash */
3266 ++p;
3267 }
3268 if (*p != NUL)
3269 *p++ = NUL;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 p = skipwhite(p);
3272 rhs = p;
3273 hasarg = (*rhs != NUL);
3274 haskey = (*keys != NUL);
3275
3276 /* check for :unmap without argument */
3277 if (maptype == 1 && !haskey)
3278 {
3279 retval = 1;
3280 goto theend;
3281 }
3282
3283 /*
3284 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3285 * with the appropriate two bytes. If it is a shifted special key, unshift
3286 * it too, giving another two bytes.
3287 * replace_termcodes() may move the result to allocated memory, which
3288 * needs to be freed later (*keys_buf and *arg_buf).
3289 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3290 */
3291 if (haskey)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003292 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (hasarg)
3294 {
Bram Moolenaarbd743252010-10-20 21:23:33 +02003295 orig_rhs = rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3297 rhs = (char_u *)"";
3298 else
Bram Moolenaar9c102382006-05-03 21:26:49 +00003299 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301
3302#ifdef FEAT_FKMAP
3303 /*
Bram Moolenaarbd743252010-10-20 21:23:33 +02003304 * When in right-to-left mode and alternate keymap option set,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 * reverse the character flow in the rhs in Farsi.
3306 */
3307 if (p_altkeymap && curwin->w_p_rl)
3308 lrswap(rhs);
3309#endif
3310
3311 /*
3312 * check arguments and translate function keys
3313 */
3314 if (haskey)
3315 {
3316 len = (int)STRLEN(keys);
3317 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3318 {
3319 retval = 1;
3320 goto theend;
3321 }
3322
3323 if (abbrev && maptype != 1)
3324 {
3325 /*
3326 * If an abbreviation ends in a keyword character, the
3327 * rest must be all keyword-char or all non-keyword-char.
3328 * Otherwise we won't be able to find the start of it in a
3329 * vi-compatible way.
3330 */
3331#ifdef FEAT_MBYTE
3332 if (has_mbyte)
3333 {
3334 int first, last;
3335 int same = -1;
3336
3337 first = vim_iswordp(keys);
3338 last = first;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003339 p = keys + (*mb_ptr2len)(keys);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 n = 1;
3341 while (p < keys + len)
3342 {
3343 ++n; /* nr of (multi-byte) chars */
3344 last = vim_iswordp(p); /* type of last char */
3345 if (same == -1 && last != first)
3346 same = n - 1; /* count of same char type */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003347 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
3349 if (last && n > 2 && same >= 0 && same < n - 1)
3350 {
3351 retval = 1;
3352 goto theend;
3353 }
3354 }
3355 else
3356#endif
3357 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3358 for (n = 0; n < len - 2; ++n)
3359 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3360 {
3361 retval = 1;
3362 goto theend;
3363 }
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00003364 /* An abbreviation cannot contain white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 for (n = 0; n < len; ++n)
3366 if (vim_iswhite(keys[n]))
3367 {
3368 retval = 1;
3369 goto theend;
3370 }
3371 }
3372 }
3373
3374 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3375 no_abbr = FALSE; /* reset flag that indicates there are
3376 no abbreviations */
3377
3378 if (!haskey || (maptype != 1 && !hasarg))
3379 msg_start();
3380
3381#ifdef FEAT_LOCALMAP
3382 /*
3383 * Check if a new local mapping wasn't already defined globally.
3384 */
3385 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3386 {
3387 /* need to loop over all global hash lists */
3388 for (hash = 0; hash < 256 && !got_int; ++hash)
3389 {
3390 if (abbrev)
3391 {
3392 if (hash != 0) /* there is only one abbreviation list */
3393 break;
3394 mp = first_abbr;
3395 }
3396 else
3397 mp = maphash[hash];
3398 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3399 {
3400 /* check entries with the same mode */
3401 if ((mp->m_mode & mode) != 0
3402 && mp->m_keylen == len
3403 && unique
3404 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3405 {
3406 if (abbrev)
3407 EMSG2(_("E224: global abbreviation already exists for %s"),
3408 mp->m_keys);
3409 else
3410 EMSG2(_("E225: global mapping already exists for %s"),
3411 mp->m_keys);
3412 retval = 5;
3413 goto theend;
3414 }
3415 }
3416 }
3417 }
3418
3419 /*
3420 * When listing global mappings, also list buffer-local ones here.
3421 */
3422 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3423 {
3424 /* need to loop over all global hash lists */
3425 for (hash = 0; hash < 256 && !got_int; ++hash)
3426 {
3427 if (abbrev)
3428 {
3429 if (hash != 0) /* there is only one abbreviation list */
3430 break;
3431 mp = curbuf->b_first_abbr;
3432 }
3433 else
3434 mp = curbuf->b_maphash[hash];
3435 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3436 {
3437 /* check entries with the same mode */
3438 if ((mp->m_mode & mode) != 0)
3439 {
3440 if (!haskey) /* show all entries */
3441 {
3442 showmap(mp, TRUE);
3443 did_local = TRUE;
3444 }
3445 else
3446 {
3447 n = mp->m_keylen;
3448 if (STRNCMP(mp->m_keys, keys,
3449 (size_t)(n < len ? n : len)) == 0)
3450 {
3451 showmap(mp, TRUE);
3452 did_local = TRUE;
3453 }
3454 }
3455 }
3456 }
3457 }
3458 }
3459#endif
3460
3461 /*
3462 * Find an entry in the maphash[] list that matches.
3463 * For :unmap we may loop two times: once to try to unmap an entry with a
3464 * matching 'from' part, a second time, if the first fails, to unmap an
3465 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3466 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3467 * "bar" because of the abbreviation.
3468 */
3469 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3470 && !did_it && !got_int; ++round)
3471 {
3472 /* need to loop over all hash lists */
3473 for (hash = 0; hash < 256 && !got_int; ++hash)
3474 {
3475 if (abbrev)
3476 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003477 if (hash > 0) /* there is only one abbreviation list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 break;
3479 mpp = abbr_table;
3480 }
3481 else
3482 mpp = &(map_table[hash]);
3483 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3484 {
3485
3486 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3487 {
3488 mpp = &(mp->m_next);
3489 continue;
3490 }
3491 if (!haskey) /* show all entries */
3492 {
3493 showmap(mp, map_table != maphash);
3494 did_it = TRUE;
3495 }
3496 else /* do we have a match? */
3497 {
3498 if (round) /* second round: Try unmap "rhs" string */
3499 {
3500 n = (int)STRLEN(mp->m_str);
3501 p = mp->m_str;
3502 }
3503 else
3504 {
3505 n = mp->m_keylen;
3506 p = mp->m_keys;
3507 }
3508 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3509 {
3510 if (maptype == 1) /* delete entry */
3511 {
3512 /* Only accept a full match. For abbreviations we
3513 * ignore trailing space when matching with the
3514 * "lhs", since an abbreviation can't have
3515 * trailing space. */
3516 if (n != len && (!abbrev || round || n > len
3517 || *skipwhite(keys + n) != NUL))
3518 {
3519 mpp = &(mp->m_next);
3520 continue;
3521 }
3522 /*
3523 * We reset the indicated mode bits. If nothing is
3524 * left the entry is deleted below.
3525 */
3526 mp->m_mode &= ~mode;
3527 did_it = TRUE; /* remember we did something */
3528 }
3529 else if (!hasarg) /* show matching entry */
3530 {
3531 showmap(mp, map_table != maphash);
3532 did_it = TRUE;
3533 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00003534 else if (n != len) /* new entry is ambiguous */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
3536 mpp = &(mp->m_next);
3537 continue;
3538 }
3539 else if (unique)
3540 {
3541 if (abbrev)
3542 EMSG2(_("E226: abbreviation already exists for %s"),
3543 p);
3544 else
3545 EMSG2(_("E227: mapping already exists for %s"), p);
3546 retval = 5;
3547 goto theend;
3548 }
3549 else /* new rhs for existing entry */
3550 {
3551 mp->m_mode &= ~mode; /* remove mode bits */
3552 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3553 {
3554 newstr = vim_strsave(rhs);
3555 if (newstr == NULL)
3556 {
3557 retval = 4; /* no mem */
3558 goto theend;
3559 }
3560 vim_free(mp->m_str);
3561 mp->m_str = newstr;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003562 vim_free(mp->m_orig_str);
3563 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 mp->m_noremap = noremap;
3565 mp->m_silent = silent;
3566 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003567#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003568 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003569 mp->m_script_ID = current_SID;
3570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 did_it = TRUE;
3572 }
3573 }
3574 if (mp->m_mode == 0) /* entry can be deleted */
3575 {
3576 map_free(mpp);
3577 continue; /* continue with *mpp */
3578 }
3579
3580 /*
3581 * May need to put this entry into another hash list.
3582 */
3583 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3584 if (!abbrev && new_hash != hash)
3585 {
3586 *mpp = mp->m_next;
3587 mp->m_next = map_table[new_hash];
3588 map_table[new_hash] = mp;
3589
3590 continue; /* continue with *mpp */
3591 }
3592 }
3593 }
3594 mpp = &(mp->m_next);
3595 }
3596 }
3597 }
3598
3599 if (maptype == 1) /* delete entry */
3600 {
3601 if (!did_it)
3602 retval = 2; /* no match */
3603 goto theend;
3604 }
3605
3606 if (!haskey || !hasarg) /* print entries */
3607 {
3608 if (!did_it
3609#ifdef FEAT_LOCALMAP
3610 && !did_local
3611#endif
3612 )
3613 {
3614 if (abbrev)
3615 MSG(_("No abbreviation found"));
3616 else
3617 MSG(_("No mapping found"));
3618 }
3619 goto theend; /* listing finished */
3620 }
3621
3622 if (did_it) /* have added the new entry already */
3623 goto theend;
3624
3625 /*
3626 * Get here when adding a new entry to the maphash[] list or abbrlist.
3627 */
3628 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3629 if (mp == NULL)
3630 {
3631 retval = 4; /* no mem */
3632 goto theend;
3633 }
3634
3635 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3636 if (*keys == Ctrl_C)
3637 mapped_ctrl_c = TRUE;
3638
3639 mp->m_keys = vim_strsave(keys);
3640 mp->m_str = vim_strsave(rhs);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003641 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (mp->m_keys == NULL || mp->m_str == NULL)
3643 {
3644 vim_free(mp->m_keys);
3645 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003646 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 vim_free(mp);
3648 retval = 4; /* no mem */
3649 goto theend;
3650 }
3651 mp->m_keylen = (int)STRLEN(mp->m_keys);
3652 mp->m_noremap = noremap;
3653 mp->m_silent = silent;
3654 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003655#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003656 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003657 mp->m_script_ID = current_SID;
3658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 /* add the new entry in front of the abbrlist or maphash[] list */
3661 if (abbrev)
3662 {
3663 mp->m_next = *abbr_table;
3664 *abbr_table = mp;
3665 }
3666 else
3667 {
3668 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3669 mp->m_next = map_table[n];
3670 map_table[n] = mp;
3671 }
3672
3673theend:
3674 vim_free(keys_buf);
3675 vim_free(arg_buf);
3676 return retval;
3677}
3678
3679/*
3680 * Delete one entry from the abbrlist or maphash[].
3681 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3682 */
3683 static void
3684map_free(mpp)
3685 mapblock_T **mpp;
3686{
3687 mapblock_T *mp;
3688
3689 mp = *mpp;
3690 vim_free(mp->m_keys);
3691 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003692 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 *mpp = mp->m_next;
3694 vim_free(mp);
3695}
3696
3697/*
3698 * Initialize maphash[] for first use.
3699 */
3700 static void
3701validate_maphash()
3702{
3703 if (!maphash_valid)
3704 {
3705 vim_memset(maphash, 0, sizeof(maphash));
3706 maphash_valid = TRUE;
3707 }
3708}
3709
3710/*
3711 * Get the mapping mode from the command name.
3712 */
3713 int
3714get_map_mode(cmdp, forceit)
3715 char_u **cmdp;
3716 int forceit;
3717{
3718 char_u *p;
3719 int modec;
3720 int mode;
3721
3722 p = *cmdp;
3723 modec = *p++;
3724 if (modec == 'i')
3725 mode = INSERT; /* :imap */
3726 else if (modec == 'l')
3727 mode = LANGMAP; /* :lmap */
3728 else if (modec == 'c')
3729 mode = CMDLINE; /* :cmap */
3730 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3731 mode = NORMAL; /* :nmap */
3732 else if (modec == 'v')
Bram Moolenaar371d5402006-03-20 21:47:49 +00003733 mode = VISUAL + SELECTMODE; /* :vmap */
3734 else if (modec == 'x')
3735 mode = VISUAL; /* :xmap */
3736 else if (modec == 's')
3737 mode = SELECTMODE; /* :smap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 else if (modec == 'o')
3739 mode = OP_PENDING; /* :omap */
3740 else
3741 {
3742 --p;
3743 if (forceit)
3744 mode = INSERT + CMDLINE; /* :map ! */
3745 else
Bram Moolenaar371d5402006-03-20 21:47:49 +00003746 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748
3749 *cmdp = p;
3750 return mode;
3751}
3752
3753/*
3754 * Clear all mappings or abbreviations.
3755 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 void
3758map_clear(cmdp, arg, forceit, abbr)
3759 char_u *cmdp;
Bram Moolenaard31aca22009-07-14 11:44:30 +00003760 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 int forceit;
3762 int abbr;
3763{
3764 int mode;
3765#ifdef FEAT_LOCALMAP
3766 int local;
3767
3768 local = (STRCMP(arg, "<buffer>") == 0);
3769 if (!local && *arg != NUL)
3770 {
3771 EMSG(_(e_invarg));
3772 return;
3773 }
3774#endif
3775
3776 mode = get_map_mode(&cmdp, forceit);
3777 map_clear_int(curbuf, mode,
3778#ifdef FEAT_LOCALMAP
3779 local,
3780#else
3781 FALSE,
3782#endif
3783 abbr);
3784}
3785
3786/*
3787 * Clear all mappings in "mode".
3788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 void
3790map_clear_int(buf, mode, local, abbr)
Bram Moolenaard31aca22009-07-14 11:44:30 +00003791 buf_T *buf UNUSED; /* buffer for local mappings */
3792 int mode; /* mode in which to delete */
3793 int local UNUSED; /* TRUE for buffer-local mappings */
3794 int abbr; /* TRUE for abbreviations */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 mapblock_T *mp, **mpp;
3797 int hash;
3798 int new_hash;
3799
3800 validate_maphash();
3801
3802 for (hash = 0; hash < 256; ++hash)
3803 {
3804 if (abbr)
3805 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003806 if (hash > 0) /* there is only one abbrlist */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 break;
3808#ifdef FEAT_LOCALMAP
3809 if (local)
3810 mpp = &buf->b_first_abbr;
3811 else
3812#endif
3813 mpp = &first_abbr;
3814 }
3815 else
3816 {
3817#ifdef FEAT_LOCALMAP
3818 if (local)
3819 mpp = &buf->b_maphash[hash];
3820 else
3821#endif
3822 mpp = &maphash[hash];
3823 }
3824 while (*mpp != NULL)
3825 {
3826 mp = *mpp;
3827 if (mp->m_mode & mode)
3828 {
3829 mp->m_mode &= ~mode;
3830 if (mp->m_mode == 0) /* entry can be deleted */
3831 {
3832 map_free(mpp);
3833 continue;
3834 }
3835 /*
3836 * May need to put this entry into another hash list.
3837 */
3838 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3839 if (!abbr && new_hash != hash)
3840 {
3841 *mpp = mp->m_next;
3842#ifdef FEAT_LOCALMAP
3843 if (local)
3844 {
3845 mp->m_next = buf->b_maphash[new_hash];
3846 buf->b_maphash[new_hash] = mp;
3847 }
3848 else
3849#endif
3850 {
3851 mp->m_next = maphash[new_hash];
3852 maphash[new_hash] = mp;
3853 }
3854 continue; /* continue with *mpp */
3855 }
3856 }
3857 mpp = &(mp->m_next);
3858 }
3859 }
3860}
3861
Bram Moolenaarbd743252010-10-20 21:23:33 +02003862/*
3863 * Return characters to represent the map mode in an allocated string.
3864 * Returns NULL when out of memory.
3865 */
3866 char_u *
3867map_mode_to_chars(mode)
3868 int mode;
3869{
3870 garray_T mapmode;
3871
3872 ga_init2(&mapmode, 1, 7);
3873
3874 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3875 ga_append(&mapmode, '!'); /* :map! */
3876 else if (mode & INSERT)
3877 ga_append(&mapmode, 'i'); /* :imap */
3878 else if (mode & LANGMAP)
3879 ga_append(&mapmode, 'l'); /* :lmap */
3880 else if (mode & CMDLINE)
3881 ga_append(&mapmode, 'c'); /* :cmap */
3882 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3883 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3884 ga_append(&mapmode, ' '); /* :map */
3885 else
3886 {
3887 if (mode & NORMAL)
3888 ga_append(&mapmode, 'n'); /* :nmap */
3889 if (mode & OP_PENDING)
3890 ga_append(&mapmode, 'o'); /* :omap */
3891 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3892 ga_append(&mapmode, 'v'); /* :vmap */
3893 else
3894 {
3895 if (mode & VISUAL)
3896 ga_append(&mapmode, 'x'); /* :xmap */
3897 if (mode & SELECTMODE)
3898 ga_append(&mapmode, 's'); /* :smap */
3899 }
3900 }
3901
3902 ga_append(&mapmode, NUL);
3903 return (char_u *)mapmode.ga_data;
3904}
3905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 static void
3907showmap(mp, local)
3908 mapblock_T *mp;
3909 int local; /* TRUE for buffer-local map */
3910{
Bram Moolenaarbd743252010-10-20 21:23:33 +02003911 int len = 1;
3912 char_u *mapchars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 if (msg_didout || msg_silent != 0)
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 msg_putchar('\n');
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003917 if (got_int) /* 'q' typed at MORE prompt */
3918 return;
3919 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003920
3921 mapchars = map_mode_to_chars(mp->m_mode);
3922 if (mapchars != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaarbd743252010-10-20 21:23:33 +02003924 msg_puts(mapchars);
3925 len = STRLEN(mapchars);
3926 vim_free(mapchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 while (++len <= 3)
3930 msg_putchar(' ');
3931
Bram Moolenaarf2330482008-06-24 20:19:36 +00003932 /* Display the LHS. Get length of what we write. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 len = msg_outtrans_special(mp->m_keys, TRUE);
3934 do
3935 {
3936 msg_putchar(' '); /* padd with blanks */
3937 ++len;
3938 } while (len < 12);
3939
3940 if (mp->m_noremap == REMAP_NONE)
3941 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3942 else if (mp->m_noremap == REMAP_SCRIPT)
3943 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3944 else
3945 msg_putchar(' ');
3946
3947 if (local)
3948 msg_putchar('@');
3949 else
3950 msg_putchar(' ');
3951
3952 /* Use FALSE below if we only want things like <Up> to show up as such on
Bram Moolenaarbd743252010-10-20 21:23:33 +02003953 * the rhs, and not M-x etc, TRUE gets both -- webb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (*mp->m_str == NUL)
3955 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3956 else
3957 msg_outtrans_special(mp->m_str, FALSE);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003958#ifdef FEAT_EVAL
3959 if (p_verbose > 0)
3960 last_set_msg(mp->m_script_ID);
3961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 out_flush(); /* show one line at a time */
3963}
3964
3965#if defined(FEAT_EVAL) || defined(PROTO)
3966/*
3967 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3968 * Recognize termcap codes in "str".
3969 * Also checks mappings local to the current buffer.
3970 */
3971 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003972map_to_exists(str, modechars, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char_u *str;
3974 char_u *modechars;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003975 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977 int mode = 0;
3978 char_u *rhs;
3979 char_u *buf;
3980 int retval;
3981
Bram Moolenaar9c102382006-05-03 21:26:49 +00003982 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 if (vim_strchr(modechars, 'n') != NULL)
3985 mode |= NORMAL;
3986 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003987 mode |= VISUAL + SELECTMODE;
3988 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 mode |= VISUAL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003990 if (vim_strchr(modechars, 's') != NULL)
3991 mode |= SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (vim_strchr(modechars, 'o') != NULL)
3993 mode |= OP_PENDING;
3994 if (vim_strchr(modechars, 'i') != NULL)
3995 mode |= INSERT;
3996 if (vim_strchr(modechars, 'l') != NULL)
3997 mode |= LANGMAP;
3998 if (vim_strchr(modechars, 'c') != NULL)
3999 mode |= CMDLINE;
4000
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004001 retval = map_to_exists_mode(rhs, mode, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 vim_free(buf);
4003
4004 return retval;
4005}
4006#endif
4007
4008/*
4009 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
4010 * Also checks mappings local to the current buffer.
4011 */
4012 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004013map_to_exists_mode(rhs, mode, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u *rhs;
4015 int mode;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004016 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
4018 mapblock_T *mp;
4019 int hash;
4020# ifdef FEAT_LOCALMAP
4021 int expand_buffer = FALSE;
4022
4023 validate_maphash();
4024
4025 /* Do it twice: once for global maps and once for local maps. */
4026 for (;;)
4027 {
4028# endif
4029 for (hash = 0; hash < 256; ++hash)
4030 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004031 if (abbr)
4032 {
4033 if (hash > 0) /* there is only one abbr list */
4034 break;
4035#ifdef FEAT_LOCALMAP
4036 if (expand_buffer)
4037 mp = curbuf->b_first_abbr;
4038 else
4039#endif
4040 mp = first_abbr;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042# ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004043 else if (expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 mp = curbuf->b_maphash[hash];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045# endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 mp = maphash[hash];
4048 for (; mp; mp = mp->m_next)
4049 {
4050 if ((mp->m_mode & mode)
4051 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
4052 return TRUE;
4053 }
4054 }
4055# ifdef FEAT_LOCALMAP
4056 if (expand_buffer)
4057 break;
4058 expand_buffer = TRUE;
4059 }
4060# endif
4061
4062 return FALSE;
4063}
4064
4065#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4066/*
4067 * Used below when expanding mapping/abbreviation names.
4068 */
4069static int expand_mapmodes = 0;
4070static int expand_isabbrev = 0;
4071#ifdef FEAT_LOCALMAP
4072static int expand_buffer = FALSE;
4073#endif
4074
4075/*
4076 * Work out what to complete when doing command line completion of mapping
4077 * or abbreviation names.
4078 */
4079 char_u *
4080set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
4081 expand_T *xp;
4082 char_u *cmd;
4083 char_u *arg;
4084 int forceit; /* TRUE if '!' given */
4085 int isabbrev; /* TRUE if abbreviation */
4086 int isunmap; /* TRUE if unmap/unabbrev command */
4087 cmdidx_T cmdidx;
4088{
4089 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
4090 xp->xp_context = EXPAND_NOTHING;
4091 else
4092 {
4093 if (isunmap)
4094 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
4095 else
4096 {
4097 expand_mapmodes = INSERT + CMDLINE;
4098 if (!isabbrev)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004099 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 expand_isabbrev = isabbrev;
4102 xp->xp_context = EXPAND_MAPPINGS;
4103#ifdef FEAT_LOCALMAP
4104 expand_buffer = FALSE;
4105#endif
4106 for (;;)
4107 {
4108#ifdef FEAT_LOCALMAP
4109 if (STRNCMP(arg, "<buffer>", 8) == 0)
4110 {
4111 expand_buffer = TRUE;
4112 arg = skipwhite(arg + 8);
4113 continue;
4114 }
4115#endif
4116 if (STRNCMP(arg, "<unique>", 8) == 0)
4117 {
4118 arg = skipwhite(arg + 8);
4119 continue;
4120 }
4121 if (STRNCMP(arg, "<silent>", 8) == 0)
4122 {
4123 arg = skipwhite(arg + 8);
4124 continue;
4125 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004126#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (STRNCMP(arg, "<script>", 8) == 0)
4128 {
4129 arg = skipwhite(arg + 8);
4130 continue;
4131 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004132 if (STRNCMP(arg, "<expr>", 6) == 0)
4133 {
4134 arg = skipwhite(arg + 6);
4135 continue;
4136 }
4137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 break;
4139 }
4140 xp->xp_pattern = arg;
4141 }
4142
4143 return NULL;
4144}
4145
4146/*
4147 * Find all mapping/abbreviation names that match regexp 'prog'.
4148 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4149 * Return OK if matches found, FAIL otherwise.
4150 */
4151 int
4152ExpandMappings(regmatch, num_file, file)
4153 regmatch_T *regmatch;
4154 int *num_file;
4155 char_u ***file;
4156{
4157 mapblock_T *mp;
4158 int hash;
4159 int count;
4160 int round;
4161 char_u *p;
4162 int i;
4163
4164 validate_maphash();
4165
4166 *num_file = 0; /* return values in case of FAIL */
4167 *file = NULL;
4168
4169 /*
4170 * round == 1: Count the matches.
4171 * round == 2: Build the array to keep the matches.
4172 */
4173 for (round = 1; round <= 2; ++round)
4174 {
4175 count = 0;
4176
Bram Moolenaar4e427192006-03-10 21:34:27 +00004177 for (i = 0; i < 5; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
4179 if (i == 0)
4180 p = (char_u *)"<silent>";
4181 else if (i == 1)
4182 p = (char_u *)"<unique>";
4183#ifdef FEAT_EVAL
4184 else if (i == 2)
4185 p = (char_u *)"<script>";
Bram Moolenaar4e427192006-03-10 21:34:27 +00004186 else if (i == 3)
4187 p = (char_u *)"<expr>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#endif
4189#ifdef FEAT_LOCALMAP
Bram Moolenaar4e427192006-03-10 21:34:27 +00004190 else if (i == 4 && !expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 p = (char_u *)"<buffer>";
4192#endif
4193 else
4194 continue;
4195
4196 if (vim_regexec(regmatch, p, (colnr_T)0))
4197 {
4198 if (round == 1)
4199 ++count;
4200 else
4201 (*file)[count++] = vim_strsave(p);
4202 }
4203 }
4204
4205 for (hash = 0; hash < 256; ++hash)
4206 {
4207 if (expand_isabbrev)
4208 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004209 if (hash > 0) /* only one abbrev list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 break; /* for (hash) */
4211 mp = first_abbr;
4212 }
4213#ifdef FEAT_LOCALMAP
4214 else if (expand_buffer)
4215 mp = curbuf->b_maphash[hash];
4216#endif
4217 else
4218 mp = maphash[hash];
4219 for (; mp; mp = mp->m_next)
4220 {
4221 if (mp->m_mode & expand_mapmodes)
4222 {
4223 p = translate_mapping(mp->m_keys, TRUE);
4224 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4225 {
4226 if (round == 1)
4227 ++count;
4228 else
4229 {
4230 (*file)[count++] = p;
4231 p = NULL;
4232 }
4233 }
4234 vim_free(p);
4235 }
4236 } /* for (mp) */
4237 } /* for (hash) */
4238
4239 if (count == 0) /* no match found */
4240 break; /* for (round) */
4241
4242 if (round == 1)
4243 {
4244 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4245 if (*file == NULL)
4246 return FAIL;
4247 }
4248 } /* for (round) */
4249
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004250 if (count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004252 char_u **ptr1;
4253 char_u **ptr2;
4254 char_u **ptr3;
4255
4256 /* Sort the matches */
4257 sort_strings(*file, count);
4258
4259 /* Remove multiple entries */
4260 ptr1 = *file;
4261 ptr2 = ptr1 + 1;
4262 ptr3 = ptr1 + count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 while (ptr2 < ptr3)
4265 {
4266 if (STRCMP(*ptr1, *ptr2))
4267 *++ptr1 = *ptr2++;
4268 else
4269 {
4270 vim_free(*ptr2++);
4271 count--;
4272 }
4273 }
4274 }
4275
4276 *num_file = count;
4277 return (count == 0 ? FAIL : OK);
4278}
4279#endif /* FEAT_CMDL_COMPL */
4280
4281/*
4282 * Check for an abbreviation.
4283 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4284 * "c" is the character typed before check_abbr was called. It may have
4285 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4286 *
4287 * Historic vi practice: The last character of an abbreviation must be an id
4288 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4289 * characters or all non-id characters. This allows for abbr. "#i" to
4290 * "#include".
4291 *
4292 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4293 * Then there must be white space before the abbr.
4294 *
4295 * return TRUE if there is an abbreviation, FALSE if not
4296 */
4297 int
4298check_abbr(c, ptr, col, mincol)
4299 int c;
4300 char_u *ptr;
4301 int col;
4302 int mincol;
4303{
4304 int len;
4305 int scol; /* starting column of the abbr. */
4306 int j;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004307 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_MBYTE
4309 char_u tb[MB_MAXBYTES + 4];
4310#else
4311 char_u tb[4];
4312#endif
4313 mapblock_T *mp;
4314#ifdef FEAT_LOCALMAP
4315 mapblock_T *mp2;
4316#endif
4317#ifdef FEAT_MBYTE
4318 int clen = 0; /* length in characters */
4319#endif
4320 int is_id = TRUE;
4321 int vim_abbr;
4322
4323 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4324 return FALSE;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00004325 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0)
4326 /* no remapping implies no abbreviation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FALSE;
4328
4329 /*
4330 * Check for word before the cursor: If it ends in a keyword char all
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00004331 * chars before it must be keyword chars or non-keyword chars, but not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 * white space. If it ends in a non-keyword char we accept any characters
4333 * before it except white space.
4334 */
4335 if (col == 0) /* cannot be an abbr. */
4336 return FALSE;
4337
4338#ifdef FEAT_MBYTE
4339 if (has_mbyte)
4340 {
4341 char_u *p;
4342
4343 p = mb_prevptr(ptr, ptr + col);
4344 if (!vim_iswordp(p))
4345 vim_abbr = TRUE; /* Vim added abbr. */
4346 else
4347 {
4348 vim_abbr = FALSE; /* vi compatible abbr. */
4349 if (p > ptr)
4350 is_id = vim_iswordp(mb_prevptr(ptr, p));
4351 }
4352 clen = 1;
4353 while (p > ptr + mincol)
4354 {
4355 p = mb_prevptr(ptr, p);
4356 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4357 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004358 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 break;
4360 }
4361 ++clen;
4362 }
4363 scol = (int)(p - ptr);
4364 }
4365 else
4366#endif
4367 {
4368 if (!vim_iswordc(ptr[col - 1]))
4369 vim_abbr = TRUE; /* Vim added abbr. */
4370 else
4371 {
4372 vim_abbr = FALSE; /* vi compatible abbr. */
4373 if (col > 1)
4374 is_id = vim_iswordc(ptr[col - 2]);
4375 }
4376 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4377 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4378 ;
4379 }
4380
4381 if (scol < mincol)
4382 scol = mincol;
4383 if (scol < col) /* there is a word in front of the cursor */
4384 {
4385 ptr += scol;
4386 len = col - scol;
4387#ifdef FEAT_LOCALMAP
4388 mp = curbuf->b_first_abbr;
4389 mp2 = first_abbr;
4390 if (mp == NULL)
4391 {
4392 mp = mp2;
4393 mp2 = NULL;
4394 }
4395#else
4396 mp = first_abbr;
4397#endif
4398 for ( ; mp;
4399#ifdef FEAT_LOCALMAP
4400 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4401#endif
4402 (mp = mp->m_next))
4403 {
4404 /* find entries with right mode and keys */
4405 if ( (mp->m_mode & State)
4406 && mp->m_keylen == len
4407 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4408 break;
4409 }
4410 if (mp != NULL)
4411 {
4412 /*
4413 * Found a match:
4414 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4415 * This goes from end to start.
4416 *
4417 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4418 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4419 * Characters where IS_SPECIAL() == TRUE: key codes, need
4420 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4421 *
4422 * Character CTRL-] is treated specially - it completes the
4423 * abbreviation, but is not inserted into the input stream.
4424 */
4425 j = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (c != Ctrl_RSB)
4427 {
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004428 /* special key code, split up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (IS_SPECIAL(c) || c == K_SPECIAL)
4430 {
4431 tb[j++] = K_SPECIAL;
4432 tb[j++] = K_SECOND(c);
4433 tb[j++] = K_THIRD(c);
4434 }
4435 else
4436 {
4437 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4438 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4439#ifdef FEAT_MBYTE
4440 if (has_mbyte)
4441 {
4442 /* if ABBR_OFF has been added, remove it here */
4443 if (c >= ABBR_OFF)
4444 c -= ABBR_OFF;
4445 j += (*mb_char2bytes)(c, tb + j);
4446 }
4447 else
4448#endif
4449 tb[j++] = c;
4450 }
4451 tb[j] = NUL;
4452 /* insert the last typed char */
4453 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4454 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004455#ifdef FEAT_EVAL
4456 if (mp->m_expr)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004457 s = eval_map_expr(mp->m_str, c);
Bram Moolenaar4e427192006-03-10 21:34:27 +00004458 else
4459#endif
4460 s = mp->m_str;
4461 if (s != NULL)
4462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 /* insert the to string */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004464 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 /* no abbrev. for these chars */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004466 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4467#ifdef FEAT_EVAL
4468 if (mp->m_expr)
4469 vim_free(s);
4470#endif
4471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 tb[0] = Ctrl_H;
4474 tb[1] = NUL;
4475#ifdef FEAT_MBYTE
4476 if (has_mbyte)
4477 len = clen; /* Delete characters instead of bytes */
4478#endif
4479 while (len-- > 0) /* delete the from string */
4480 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4481 return TRUE;
4482 }
4483 }
4484 return FALSE;
4485}
4486
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004487#ifdef FEAT_EVAL
4488/*
4489 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4490 * special characters.
4491 */
4492 static char_u *
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004493eval_map_expr(str, c)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004494 char_u *str;
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004495 int c; /* NUL or typed character for abbreviation */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004496{
4497 char_u *res;
Bram Moolenaar8424a622006-04-19 21:23:36 +00004498 char_u *p;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004499 char_u *save_cmd;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004500 pos_T save_cursor;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004501
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004502 save_cmd = save_cmdline_alloc();
4503 if (save_cmd == NULL)
4504 return NULL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004505
4506 /* Forbid changing text or using ":normal" to avoid most of the bad side
4507 * effects. Also restore the cursor position. */
4508 ++textlock;
4509#ifdef FEAT_EX_EXTRA
4510 ++ex_normal_lock;
4511#endif
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004512 set_vim_var_char(c); /* set v:char to the typed character */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004513 save_cursor = curwin->w_cursor;
Bram Moolenaar8424a622006-04-19 21:23:36 +00004514 p = eval_to_string(str, NULL, FALSE);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004515 --textlock;
4516#ifdef FEAT_EX_EXTRA
4517 --ex_normal_lock;
4518#endif
4519 curwin->w_cursor = save_cursor;
4520
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004521 restore_cmdline_alloc(save_cmd);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004522 if (p == NULL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004523 return NULL;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004524 res = vim_strsave_escape_csi(p);
4525 vim_free(p);
4526
4527 return res;
4528}
4529#endif
4530
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004531/*
4532 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4533 * can be put in the typeahead buffer.
4534 * Returns NULL when out of memory.
4535 */
4536 char_u *
4537vim_strsave_escape_csi(p)
4538 char_u *p;
4539{
4540 char_u *res;
4541 char_u *s, *d;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004542
4543 /* Need a buffer to hold up to three times as much. */
Bram Moolenaar8424a622006-04-19 21:23:36 +00004544 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004545 if (res != NULL)
4546 {
Bram Moolenaar8424a622006-04-19 21:23:36 +00004547 d = res;
4548 for (s = p; *s != NUL; )
4549 {
4550 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4551 {
4552 /* Copy special key unmodified. */
4553 *d++ = *s++;
4554 *d++ = *s++;
4555 *d++ = *s++;
4556 }
4557 else
4558 {
4559 /* Add character, possibly multi-byte to destination, escaping
4560 * CSI and K_SPECIAL. */
4561 d = add_char2buf(PTR2CHAR(s), d);
4562 mb_ptr_adv(s);
4563 }
4564 }
4565 *d = NUL;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004566 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004567 return res;
4568}
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570/*
Bram Moolenaar81b85872007-03-04 20:22:01 +00004571 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4572 * vim_strsave_escape_csi(). Works in-place.
4573 */
4574 void
4575vim_unescape_csi(p)
4576 char_u *p;
4577{
4578 char_u *s = p, *d = p;
4579
4580 while (*s != NUL)
4581 {
4582 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4583 {
4584 *d++ = K_SPECIAL;
4585 s += 3;
4586 }
4587 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4588 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4589 {
4590 *d++ = CSI;
4591 s += 3;
4592 }
4593 else
4594 *d++ = *s++;
4595 }
4596 *d = NUL;
4597}
4598
4599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * Write map commands for the current mappings to an .exrc file.
4601 * Return FAIL on error, OK otherwise.
4602 */
4603 int
4604makemap(fd, buf)
4605 FILE *fd;
4606 buf_T *buf; /* buffer for local mappings or NULL */
4607{
4608 mapblock_T *mp;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004609 char_u c1, c2, c3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 char_u *p;
4611 char *cmd;
4612 int abbr;
4613 int hash;
4614 int did_cpo = FALSE;
4615 int i;
4616
4617 validate_maphash();
4618
4619 /*
4620 * Do the loop twice: Once for mappings, once for abbreviations.
4621 * Then loop over all map hash lists.
4622 */
4623 for (abbr = 0; abbr < 2; ++abbr)
4624 for (hash = 0; hash < 256; ++hash)
4625 {
4626 if (abbr)
4627 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004628 if (hash > 0) /* there is only one abbr list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630#ifdef FEAT_LOCALMAP
4631 if (buf != NULL)
4632 mp = buf->b_first_abbr;
4633 else
4634#endif
4635 mp = first_abbr;
4636 }
4637 else
4638 {
4639#ifdef FEAT_LOCALMAP
4640 if (buf != NULL)
4641 mp = buf->b_maphash[hash];
4642 else
4643#endif
4644 mp = maphash[hash];
4645 }
4646
4647 for ( ; mp; mp = mp->m_next)
4648 {
4649 /* skip script-local mappings */
4650 if (mp->m_noremap == REMAP_SCRIPT)
4651 continue;
4652
4653 /* skip mappings that contain a <SNR> (script-local thing),
4654 * they probably don't work when loaded again */
4655 for (p = mp->m_str; *p != NUL; ++p)
4656 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4657 && p[2] == (int)KE_SNR)
4658 break;
4659 if (*p != NUL)
4660 continue;
4661
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004662 /* It's possible to create a mapping and then ":unmap" certain
4663 * modes. We recreate this here by mapping the individual
4664 * modes, which requires up to three of them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 c1 = NUL;
4666 c2 = NUL;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004667 c3 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (abbr)
4669 cmd = "abbr";
4670 else
4671 cmd = "map";
4672 switch (mp->m_mode)
4673 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00004674 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 break;
4676 case NORMAL:
4677 c1 = 'n';
4678 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004679 case VISUAL:
4680 c1 = 'x';
4681 break;
4682 case SELECTMODE:
4683 c1 = 's';
4684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 case OP_PENDING:
4686 c1 = 'o';
4687 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004688 case NORMAL + VISUAL:
4689 c1 = 'n';
4690 c2 = 'x';
4691 break;
4692 case NORMAL + SELECTMODE:
4693 c1 = 'n';
4694 c2 = 's';
4695 break;
4696 case NORMAL + OP_PENDING:
4697 c1 = 'n';
4698 c2 = 'o';
4699 break;
4700 case VISUAL + SELECTMODE:
4701 c1 = 'v';
4702 break;
4703 case VISUAL + OP_PENDING:
4704 c1 = 'x';
4705 c2 = 'o';
4706 break;
4707 case SELECTMODE + OP_PENDING:
4708 c1 = 's';
4709 c2 = 'o';
4710 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004711 case NORMAL + VISUAL + SELECTMODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 c1 = 'n';
4713 c2 = 'v';
4714 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004715 case NORMAL + VISUAL + OP_PENDING:
4716 c1 = 'n';
4717 c2 = 'x';
4718 c3 = 'o';
4719 break;
4720 case NORMAL + SELECTMODE + OP_PENDING:
4721 c1 = 'n';
4722 c2 = 's';
4723 c3 = 'o';
4724 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004725 case VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 c1 = 'v';
4727 c2 = 'o';
4728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 case CMDLINE + INSERT:
4730 if (!abbr)
4731 cmd = "map!";
4732 break;
4733 case CMDLINE:
4734 c1 = 'c';
4735 break;
4736 case INSERT:
4737 c1 = 'i';
4738 break;
4739 case LANGMAP:
4740 c1 = 'l';
4741 break;
4742 default:
4743 EMSG(_("E228: makemap: Illegal mode"));
4744 return FAIL;
4745 }
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004746 do /* do this twice if c2 is set, 3 times with c3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 /* When outputting <> form, need to make sure that 'cpo'
4749 * is set to the Vim default. */
4750 if (!did_cpo)
4751 {
4752 if (*mp->m_str == NUL) /* will use <Nop> */
4753 did_cpo = TRUE;
4754 else
4755 for (i = 0; i < 2; ++i)
4756 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4757 if (*p == K_SPECIAL || *p == NL)
4758 did_cpo = TRUE;
4759 if (did_cpo)
4760 {
4761 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4762 || put_eol(fd) < 0
4763 || fprintf(fd, "set cpo&vim") < 0
4764 || put_eol(fd) < 0)
4765 return FAIL;
4766 }
4767 }
4768 if (c1 && putc(c1, fd) < 0)
4769 return FAIL;
4770 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4771 return FAIL;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004772 if (fputs(cmd, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 return FAIL;
4774 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4775 return FAIL;
4776 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4777 return FAIL;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004778#ifdef FEAT_EVAL
4779 if (mp->m_noremap == REMAP_SCRIPT
4780 && fputs("<script>", fd) < 0)
4781 return FAIL;
4782 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4783 return FAIL;
4784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 if ( putc(' ', fd) < 0
4787 || put_escstr(fd, mp->m_keys, 0) == FAIL
4788 || putc(' ', fd) < 0
4789 || put_escstr(fd, mp->m_str, 1) == FAIL
4790 || put_eol(fd) < 0)
4791 return FAIL;
4792 c1 = c2;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004793 c2 = c3;
4794 c3 = NUL;
4795 } while (c1 != NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798
4799 if (did_cpo)
4800 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4801 || put_eol(fd) < 0
4802 || fprintf(fd, "unlet s:cpo_save") < 0
4803 || put_eol(fd) < 0)
4804 return FAIL;
4805 return OK;
4806}
4807
4808/*
4809 * write escape string to file
4810 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4811 *
4812 * return FAIL for failure, OK otherwise
4813 */
4814 int
4815put_escstr(fd, strstart, what)
4816 FILE *fd;
4817 char_u *strstart;
4818 int what;
4819{
4820 char_u *str = strstart;
4821 int c;
4822 int modifiers;
4823
4824 /* :map xx <Nop> */
4825 if (*str == NUL && what == 1)
4826 {
4827 if (fprintf(fd, "<Nop>") < 0)
4828 return FAIL;
4829 return OK;
4830 }
4831
4832 for ( ; *str != NUL; ++str)
4833 {
4834#ifdef FEAT_MBYTE
4835 char_u *p;
4836
4837 /* Check for a multi-byte character, which may contain escaped
4838 * K_SPECIAL and CSI bytes */
4839 p = mb_unescape(&str);
4840 if (p != NULL)
4841 {
4842 while (*p != NUL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004843 if (fputc(*p++, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return FAIL;
4845 --str;
4846 continue;
4847 }
4848#endif
4849
4850 c = *str;
4851 /*
4852 * Special key codes have to be translated to be able to make sense
4853 * when they are read back.
4854 */
4855 if (c == K_SPECIAL && what != 2)
4856 {
4857 modifiers = 0x0;
4858 if (str[1] == KS_MODIFIER)
4859 {
4860 modifiers = str[2];
4861 str += 3;
4862 c = *str;
4863 }
4864 if (c == K_SPECIAL)
4865 {
4866 c = TO_SPECIAL(str[1], str[2]);
4867 str += 2;
4868 }
4869 if (IS_SPECIAL(c) || modifiers) /* special key */
4870 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004871 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 return FAIL;
4873 continue;
4874 }
4875 }
4876
4877 /*
4878 * A '\n' in a map command should be written as <NL>.
4879 * A '\n' in a set command should be written as \^V^J.
4880 */
4881 if (c == NL)
4882 {
4883 if (what == 2)
4884 {
4885 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4886 return FAIL;
4887 }
4888 else
4889 {
4890 if (fprintf(fd, "<NL>") < 0)
4891 return FAIL;
4892 }
4893 continue;
4894 }
4895
4896 /*
4897 * Some characters have to be escaped with CTRL-V to
4898 * prevent them from misinterpreted in DoOneCmd().
4899 * A space, Tab and '"' has to be escaped with a backslash to
4900 * prevent it to be misinterpreted in do_set().
4901 * A space has to be escaped with a CTRL-V when it's at the start of a
4902 * ":map" rhs.
4903 * A '<' has to be escaped with a CTRL-V to prevent it being
4904 * interpreted as the start of a special key name.
4905 * A space in the lhs of a :map needs a CTRL-V.
4906 */
4907 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4908 {
4909 if (putc('\\', fd) < 0)
4910 return FAIL;
4911 }
4912 else if (c < ' ' || c > '~' || c == '|'
4913 || (what == 0 && c == ' ')
4914 || (what == 1 && str == strstart && c == ' ')
4915 || (what != 2 && c == '<'))
4916 {
4917 if (putc(Ctrl_V, fd) < 0)
4918 return FAIL;
4919 }
4920 if (putc(c, fd) < 0)
4921 return FAIL;
4922 }
4923 return OK;
4924}
4925
4926/*
4927 * Check all mappings for the presence of special key codes.
4928 * Used after ":set term=xxx".
4929 */
4930 void
4931check_map_keycodes()
4932{
4933 mapblock_T *mp;
4934 char_u *p;
4935 int i;
4936 char_u buf[3];
4937 char_u *save_name;
4938 int abbr;
4939 int hash;
4940#ifdef FEAT_LOCALMAP
4941 buf_T *bp;
4942#endif
4943
4944 validate_maphash();
4945 save_name = sourcing_name;
4946 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4947
4948#ifdef FEAT_LOCALMAP
4949 /* This this once for each buffer, and then once for global
4950 * mappings/abbreviations with bp == NULL */
4951 for (bp = firstbuf; ; bp = bp->b_next)
4952 {
4953#endif
4954 /*
4955 * Do the loop twice: Once for mappings, once for abbreviations.
4956 * Then loop over all map hash lists.
4957 */
4958 for (abbr = 0; abbr <= 1; ++abbr)
4959 for (hash = 0; hash < 256; ++hash)
4960 {
4961 if (abbr)
4962 {
4963 if (hash) /* there is only one abbr list */
4964 break;
4965#ifdef FEAT_LOCALMAP
4966 if (bp != NULL)
4967 mp = bp->b_first_abbr;
4968 else
4969#endif
4970 mp = first_abbr;
4971 }
4972 else
4973 {
4974#ifdef FEAT_LOCALMAP
4975 if (bp != NULL)
4976 mp = bp->b_maphash[hash];
4977 else
4978#endif
4979 mp = maphash[hash];
4980 }
4981 for ( ; mp != NULL; mp = mp->m_next)
4982 {
4983 for (i = 0; i <= 1; ++i) /* do this twice */
4984 {
4985 if (i == 0)
4986 p = mp->m_keys; /* once for the "from" part */
4987 else
4988 p = mp->m_str; /* and once for the "to" part */
4989 while (*p)
4990 {
4991 if (*p == K_SPECIAL)
4992 {
4993 ++p;
4994 if (*p < 128) /* for "normal" tcap entries */
4995 {
4996 buf[0] = p[0];
4997 buf[1] = p[1];
4998 buf[2] = NUL;
4999 (void)add_termcap_entry(buf, FALSE);
5000 }
5001 ++p;
5002 }
5003 ++p;
5004 }
5005 }
5006 }
5007 }
5008#ifdef FEAT_LOCALMAP
5009 if (bp == NULL)
5010 break;
5011 }
5012#endif
5013 sourcing_name = save_name;
5014}
5015
Bram Moolenaarbd743252010-10-20 21:23:33 +02005016#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017/*
Bram Moolenaarbd743252010-10-20 21:23:33 +02005018 * Check the string "keys" against the lhs of all mappings.
5019 * Return pointer to rhs of mapping (mapblock->m_str).
5020 * NULL when no mapping found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 */
5022 char_u *
Bram Moolenaarbd743252010-10-20 21:23:33 +02005023check_map(keys, mode, exact, ign_mod, abbr, mp_ptr, local_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 char_u *keys;
5025 int mode;
5026 int exact; /* require exact match */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005027 int ign_mod; /* ignore preceding modifier */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005028 int abbr; /* do abbreviations */
Bram Moolenaarbd743252010-10-20 21:23:33 +02005029 mapblock_T **mp_ptr; /* return: pointer to mapblock or NULL */
5030 int *local_ptr; /* return: buffer-local mapping or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
5032 int hash;
5033 int len, minlen;
5034 mapblock_T *mp;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005035 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#ifdef FEAT_LOCALMAP
5037 int local;
5038#endif
5039
5040 validate_maphash();
5041
5042 len = (int)STRLEN(keys);
5043#ifdef FEAT_LOCALMAP
5044 for (local = 1; local >= 0; --local)
5045#endif
5046 /* loop over all hash lists */
5047 for (hash = 0; hash < 256; ++hash)
5048 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005049 if (abbr)
5050 {
5051 if (hash > 0) /* there is only one list. */
5052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005054 if (local)
5055 mp = curbuf->b_first_abbr;
5056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005058 mp = first_abbr;
5059 }
5060#ifdef FEAT_LOCALMAP
5061 else if (local)
5062 mp = curbuf->b_maphash[hash];
5063#endif
5064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 mp = maphash[hash];
5066 for ( ; mp != NULL; mp = mp->m_next)
5067 {
5068 /* skip entries with wrong mode, wrong length and not matching
5069 * ones */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005070 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
5071 {
5072 if (len > mp->m_keylen)
5073 minlen = mp->m_keylen;
5074 else
5075 minlen = len;
5076 s = mp->m_keys;
5077 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
5078 && s[2] != NUL)
5079 {
5080 s += 3;
5081 if (len > mp->m_keylen - 3)
5082 minlen = mp->m_keylen - 3;
5083 }
5084 if (STRNCMP(s, keys, minlen) == 0)
Bram Moolenaarbd743252010-10-20 21:23:33 +02005085 {
5086 if (mp_ptr != NULL)
5087 *mp_ptr = mp;
5088 if (local_ptr != NULL)
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005089#ifdef FEAT_LOCALMAP
Bram Moolenaarbd743252010-10-20 21:23:33 +02005090 *local_ptr = local;
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005091#else
5092 *local_ptr = 0;
5093#endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005094 return mp->m_str;
Bram Moolenaarbd743252010-10-20 21:23:33 +02005095 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 }
5099
5100 return NULL;
5101}
5102#endif
5103
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005104#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar371d5402006-03-20 21:47:49 +00005105
5106#define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108/*
5109 * Default mappings for some often used keys.
5110 */
5111static struct initmap
5112{
5113 char_u *arg;
5114 int mode;
5115} initmappings[] =
5116{
5117#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
5118 /* Use the Windows (CUA) keybindings. */
5119# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 /* paste, copy and cut */
5121 {(char_u *)"<S-Insert> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005122 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005124 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
5125 {(char_u *)"<S-Del> \"*d", VIS_SEL},
5126 {(char_u *)"<C-Del> \"*d", VIS_SEL},
5127 {(char_u *)"<C-X> \"*d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5129# else
Bram Moolenaar371d5402006-03-20 21:47:49 +00005130 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005132 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5134
5135 /* paste, copy and cut */
5136# ifdef FEAT_CLIPBOARD
5137# ifdef DJGPP
5138 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005139 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005141 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142# if 0 /* Shift-Del produces the same code as Del */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005143 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00005145 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5146 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147# else
5148 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005149 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005151 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5152 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5153 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5154 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155# endif
5156# else
5157 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005158 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005160 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5161 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5162 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163# endif
5164# endif
5165#endif
5166
5167#if defined(MACOS)
5168 /* Use the Standard MacOS binding. */
5169 /* paste, copy and cut */
5170 {(char_u *)"<D-v> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005171 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005173 {(char_u *)"<D-c> \"*y", VIS_SEL},
5174 {(char_u *)"<D-x> \"*d", VIS_SEL},
5175 {(char_u *)"<Backspace> \"-d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177};
Bram Moolenaar371d5402006-03-20 21:47:49 +00005178
5179# undef VIS_SEL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
5182/*
5183 * Set up default mappings.
5184 */
5185 void
5186init_mappings()
5187{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005188#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 int i;
5190
5191 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5192 add_map(initmappings[i].arg, initmappings[i].mode);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194}
5195
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005196#if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5197 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198/*
5199 * Add a mapping "map" for mode "mode".
5200 * Need to put string in allocated memory, because do_map() will modify it.
5201 */
5202 void
5203add_map(map, mode)
5204 char_u *map;
5205 int mode;
5206{
5207 char_u *s;
5208 char_u *cpo_save = p_cpo;
5209
5210 p_cpo = (char_u *)""; /* Allow <> notation */
5211 s = vim_strsave(map);
5212 if (s != NULL)
5213 {
5214 (void)do_map(0, s, mode, FALSE);
5215 vim_free(s);
5216 }
5217 p_cpo = cpo_save;
5218}
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005219#endif