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