blob: af18c6f2498b708c165a0dd92a74f8b9783b7f33 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * getchar.c
12 *
13 * functions related with getting a character from the user/mapping/redo/...
14 *
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
17 */
18
19#include "vim.h"
20
21/*
22 * These buffers are used for storing:
23 * - stuffed characters: A command that is translated into another command.
24 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000025 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 *
27 * The bytes are stored like in the typeahead buffer:
28 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
29 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
30 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
31 * otherwise switching the GUI on would make mappings invalid).
32 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
33 * These translations are also done on multi-byte characters!
34 *
35 * Escaping CSI bytes is done by the system-specific input functions, called
36 * by ui_inchar().
37 * Escaping K_SPECIAL is done by inchar().
38 * Un-escaping is done by vgetc().
39 */
40
41#define MINIMAL_SIZE 20 /* minimal size for b_str */
42
43static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
46static struct buffheader save_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
47static struct buffheader save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
48#endif
49static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
50
51static int typeahead_char = 0; /* typeahead char that's not flushed */
52
53/*
54 * when block_redo is TRUE redo buffer will not be changed
55 * used by edit() to repeat insertions and 'V' command for redoing
56 */
57static int block_redo = FALSE;
58
59/*
60 * Make a hash value for a mapping.
61 * "mode" is the lower 4 bits of the State for the mapping.
62 * "c1" is the first character of the "lhs".
63 * Returns a value between 0 and 255, index in maphash.
64 * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode.
65 */
Bram Moolenaar371d5402006-03-20 21:47:49 +000066#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) ? (c1) : ((c1) ^ 0x80))
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Each mapping is put in one of the 256 hash lists, to speed up finding it.
70 */
71static mapblock_T *(maphash[256]);
72static int maphash_valid = FALSE;
73
74/*
75 * List used for abbreviations.
76 */
77static mapblock_T *first_abbr = NULL; /* first entry in abbrlist */
78
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +000079static int KeyNoremap = 0; /* remapping flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * variables used by vgetorpeek() and flush_buffers()
83 *
84 * typebuf.tb_buf[] contains all characters that are not consumed yet.
85 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
86 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
87 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
88 * The head of the buffer may contain the result of mappings, abbreviations
89 * and @a commands. The length of this part is typebuf.tb_maplen.
90 * typebuf.tb_silent is the part where <silent> applies.
91 * After the head are characters that come from the terminal.
92 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
93 * should not be considered for abbreviations.
94 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
95 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
96 * contains RM_NONE for the characters that are not to be remapped.
97 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
98 * (typebuf has been put in globals.h, because check_termcode() needs it).
99 */
100#define RM_YES 0 /* tb_noremap: remap */
101#define RM_NONE 1 /* tb_noremap: don't remap */
102#define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000103#define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/* typebuf.tb_buf has three parts: room in front (for result of mappings), the
106 * middle for typeahead and room for new characters (which needs to be 3 *
107 * MAXMAPLEN) for the Amiga).
108 */
109#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
110static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
111static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
112
113static int last_recorded_len = 0; /* number of last recorded chars */
114
115static char_u *get_buffcont __ARGS((struct buffheader *, int));
116static void add_buff __ARGS((struct buffheader *, char_u *, long n));
117static void add_num_buff __ARGS((struct buffheader *, long));
118static void add_char_buff __ARGS((struct buffheader *, int));
119static int read_stuff __ARGS((int advance));
120static void start_stuff __ARGS((void));
121static int read_redo __ARGS((int, int));
122static void copy_redo __ARGS((int));
123static void init_typebuf __ARGS((void));
124static void gotchars __ARGS((char_u *, int));
125static void may_sync_undo __ARGS((void));
126static void closescript __ARGS((void));
127static int vgetorpeek __ARGS((int));
128static void map_free __ARGS((mapblock_T **));
129static void validate_maphash __ARGS((void));
130static void showmap __ARGS((mapblock_T *mp, int local));
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000131#ifdef FEAT_EVAL
Bram Moolenaarda9591e2009-09-30 13:17:02 +0000132static char_u *eval_map_expr __ARGS((char_u *str, int c));
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135/*
136 * Free and clear a buffer.
137 */
138 void
139free_buff(buf)
140 struct buffheader *buf;
141{
142 struct buffblock *p, *np;
143
144 for (p = buf->bh_first.b_next; p != NULL; p = np)
145 {
146 np = p->b_next;
147 vim_free(p);
148 }
149 buf->bh_first.b_next = NULL;
150}
151
152/*
153 * Return the contents of a buffer as a single string.
154 * K_SPECIAL and CSI in the returned string are escaped.
155 */
156 static char_u *
157get_buffcont(buffer, dozero)
158 struct buffheader *buffer;
159 int dozero; /* count == zero is not an error */
160{
161 long_u count = 0;
162 char_u *p = NULL;
163 char_u *p2;
164 char_u *str;
165 struct buffblock *bp;
166
167 /* compute the total length of the string */
168 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
169 count += (long_u)STRLEN(bp->b_str);
170
171 if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL)
172 {
173 p2 = p;
174 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
175 for (str = bp->b_str; *str; )
176 *p2++ = *str++;
177 *p2 = NUL;
178 }
179 return (p);
180}
181
182/*
183 * Return the contents of the record buffer as a single string
184 * and clear the record buffer.
185 * K_SPECIAL and CSI in the returned string are escaped.
186 */
187 char_u *
188get_recorded()
189{
190 char_u *p;
191 size_t len;
192
193 p = get_buffcont(&recordbuff, TRUE);
194 free_buff(&recordbuff);
195
196 /*
197 * Remove the characters that were added the last time, these must be the
198 * (possibly mapped) characters that stopped the recording.
199 */
200 len = STRLEN(p);
201 if ((int)len >= last_recorded_len)
202 {
203 len -= last_recorded_len;
204 p[len] = NUL;
205 }
206
207 /*
208 * When stopping recording from Insert mode with CTRL-O q, also remove the
209 * CTRL-O.
210 */
211 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
212 p[len - 1] = NUL;
213
214 return (p);
215}
216
217/*
218 * Return the contents of the redo buffer as a single string.
219 * K_SPECIAL and CSI in the returned string are escaped.
220 */
221 char_u *
222get_inserted()
223{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000224 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225}
226
227/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000228 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 * K_SPECIAL and CSI should have been escaped already.
230 */
231 static void
232add_buff(buf, s, slen)
233 struct buffheader *buf;
234 char_u *s;
235 long slen; /* length of "s" or -1 */
236{
237 struct buffblock *p;
238 long_u len;
239
240 if (slen < 0)
241 slen = (long)STRLEN(s);
242 if (slen == 0) /* don't add empty strings */
243 return;
244
245 if (buf->bh_first.b_next == NULL) /* first add to list */
246 {
247 buf->bh_space = 0;
248 buf->bh_curr = &(buf->bh_first);
249 }
250 else if (buf->bh_curr == NULL) /* buffer has already been read */
251 {
252 EMSG(_("E222: Add to read buffer"));
253 return;
254 }
255 else if (buf->bh_index != 0)
Bram Moolenaarc3b73072007-12-07 16:30:42 +0000256 mch_memmove(buf->bh_first.b_next->b_str,
257 buf->bh_first.b_next->b_str + buf->bh_index,
258 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 buf->bh_index = 0;
260
261 if (buf->bh_space >= (int)slen)
262 {
263 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000264 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 buf->bh_space -= slen;
266 }
267 else
268 {
269 if (slen < MINIMAL_SIZE)
270 len = MINIMAL_SIZE;
271 else
272 len = slen;
273 p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len),
274 TRUE);
275 if (p == NULL)
276 return; /* no space, just forget it */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000277 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000278 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280 p->b_next = buf->bh_curr->b_next;
281 buf->bh_curr->b_next = p;
282 buf->bh_curr = p;
283 }
284 return;
285}
286
287/*
288 * Add number "n" to buffer "buf".
289 */
290 static void
291add_num_buff(buf, n)
292 struct buffheader *buf;
293 long n;
294{
295 char_u number[32];
296
297 sprintf((char *)number, "%ld", n);
298 add_buff(buf, number, -1L);
299}
300
301/*
302 * Add character 'c' to buffer "buf".
303 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
304 */
305 static void
306add_char_buff(buf, c)
307 struct buffheader *buf;
308 int c;
309{
310#ifdef FEAT_MBYTE
311 char_u bytes[MB_MAXBYTES + 1];
312 int len;
313 int i;
314#endif
315 char_u temp[4];
316
317#ifdef FEAT_MBYTE
318 if (IS_SPECIAL(c))
319 len = 1;
320 else
321 len = (*mb_char2bytes)(c, bytes);
322 for (i = 0; i < len; ++i)
323 {
324 if (!IS_SPECIAL(c))
325 c = bytes[i];
326#endif
327
328 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
329 {
330 /* translate special key code into three byte sequence */
331 temp[0] = K_SPECIAL;
332 temp[1] = K_SECOND(c);
333 temp[2] = K_THIRD(c);
334 temp[3] = NUL;
335 }
336#ifdef FEAT_GUI
337 else if (c == CSI)
338 {
339 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
340 temp[0] = CSI;
341 temp[1] = KS_EXTRA;
342 temp[2] = (int)KE_CSI;
343 temp[3] = NUL;
344 }
345#endif
346 else
347 {
348 temp[0] = c;
349 temp[1] = NUL;
350 }
351 add_buff(buf, temp, -1L);
352#ifdef FEAT_MBYTE
353 }
354#endif
355}
356
357/*
358 * Get one byte from the stuff buffer.
359 * If advance == TRUE go to the next char.
360 * No translation is done K_SPECIAL and CSI are escaped.
361 */
362 static int
363read_stuff(advance)
364 int advance;
365{
366 char_u c;
367 struct buffblock *curr;
368
369 if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */
370 return NUL;
371
372 curr = stuffbuff.bh_first.b_next;
373 c = curr->b_str[stuffbuff.bh_index];
374
375 if (advance)
376 {
377 if (curr->b_str[++stuffbuff.bh_index] == NUL)
378 {
379 stuffbuff.bh_first.b_next = curr->b_next;
380 vim_free(curr);
381 stuffbuff.bh_index = 0;
382 }
383 }
384 return c;
385}
386
387/*
388 * Prepare the stuff buffer for reading (if it contains something).
389 */
390 static void
391start_stuff()
392{
393 if (stuffbuff.bh_first.b_next != NULL)
394 {
395 stuffbuff.bh_curr = &(stuffbuff.bh_first);
396 stuffbuff.bh_space = 0;
397 }
398}
399
400/*
401 * Return TRUE if the stuff buffer is empty.
402 */
403 int
404stuff_empty()
405{
406 return (stuffbuff.bh_first.b_next == NULL);
407}
408
409/*
410 * Set a typeahead character that won't be flushed.
411 */
412 void
413typeahead_noflush(c)
414 int c;
415{
416 typeahead_char = c;
417}
418
419/*
420 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100421 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 * flush all typeahead characters (used when interrupted by a CTRL-C).
423 */
424 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100425flush_buffers(flush_typeahead)
426 int flush_typeahead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427{
428 init_typebuf();
429
430 start_stuff();
431 while (read_stuff(TRUE) != NUL)
432 ;
433
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100434 if (flush_typeahead) /* remove all typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
436 /*
437 * We have to get all characters, because we may delete the first part
438 * of an escape sequence.
439 * In an xterm we get one char at a time and we have to get them all.
440 */
441 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L,
442 typebuf.tb_change_cnt) != 0)
443 ;
444 typebuf.tb_off = MAXMAPLEN;
445 typebuf.tb_len = 0;
446 }
447 else /* remove mapped characters only */
448 {
449 typebuf.tb_off += typebuf.tb_maplen;
450 typebuf.tb_len -= typebuf.tb_maplen;
451 }
452 typebuf.tb_maplen = 0;
453 typebuf.tb_silent = 0;
454 cmd_silent = FALSE;
455 typebuf.tb_no_abbr_cnt = 0;
456}
457
458/*
459 * The previous contents of the redo buffer is kept in old_redobuffer.
460 * This is used for the CTRL-O <.> command in insert mode.
461 */
462 void
463ResetRedobuff()
464{
465 if (!block_redo)
466 {
467 free_buff(&old_redobuff);
468 old_redobuff = redobuff;
469 redobuff.bh_first.b_next = NULL;
470 }
471}
472
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100473/*
474 * Discard the contents of the redo buffer and restore the previous redo
475 * buffer.
476 */
477 void
478CancelRedo()
479{
480 if (!block_redo)
481 {
482 free_buff(&redobuff);
483 redobuff = old_redobuff;
484 old_redobuff.bh_first.b_next = NULL;
485 start_stuff();
486 while (read_stuff(TRUE) != NUL)
487 ;
488 }
489}
490
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
492/*
493 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
494 * Used before executing autocommands and user functions.
495 */
496static int save_level = 0;
497
498 void
499saveRedobuff()
500{
501 char_u *s;
502
503 if (save_level++ == 0)
504 {
505 save_redobuff = redobuff;
506 redobuff.bh_first.b_next = NULL;
507 save_old_redobuff = old_redobuff;
508 old_redobuff.bh_first.b_next = NULL;
509
510 /* Make a copy, so that ":normal ." in a function works. */
511 s = get_buffcont(&save_redobuff, FALSE);
512 if (s != NULL)
513 {
514 add_buff(&redobuff, s, -1L);
515 vim_free(s);
516 }
517 }
518}
519
520/*
521 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
522 * Used after executing autocommands and user functions.
523 */
524 void
525restoreRedobuff()
526{
527 if (--save_level == 0)
528 {
529 free_buff(&redobuff);
530 redobuff = save_redobuff;
531 free_buff(&old_redobuff);
532 old_redobuff = save_old_redobuff;
533 }
534}
535#endif
536
537/*
538 * Append "s" to the redo buffer.
539 * K_SPECIAL and CSI should already have been escaped.
540 */
541 void
542AppendToRedobuff(s)
543 char_u *s;
544{
545 if (!block_redo)
546 add_buff(&redobuff, s, -1L);
547}
548
549/*
550 * Append to Redo buffer literally, escaping special characters with CTRL-V.
551 * K_SPECIAL and CSI are escaped as well.
552 */
553 void
Bram Moolenaarebefac62005-12-28 22:39:57 +0000554AppendToRedobuffLit(str, len)
555 char_u *str;
556 int len; /* length of "str" or -1 for up to the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000558 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 int c;
560 char_u *start;
561
562 if (block_redo)
563 return;
564
Bram Moolenaarebefac62005-12-28 22:39:57 +0000565 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
567 /* Put a string of normal characters in the redo buffer (that's
568 * faster). */
569 start = s;
570 while (*s >= ' '
571#ifndef EBCDIC
572 && *s < DEL /* EBCDIC: all chars above space are normal */
573#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000574 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 ++s;
576
577 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
578 * typed next. */
579 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
580 --s;
581 if (s > start)
582 add_buff(&redobuff, start, (long)(s - start));
583
Bram Moolenaarebefac62005-12-28 22:39:57 +0000584 if (*s == NUL || (len >= 0 && s - str >= len))
585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaarebefac62005-12-28 22:39:57 +0000587 /* Handle a special or multibyte character. */
588#ifdef FEAT_MBYTE
589 if (has_mbyte)
590 /* Handle composing chars separately. */
591 c = mb_cptr2char_adv(&s);
592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000594 c = *s++;
595 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
596 add_char_buff(&redobuff, Ctrl_V);
597
598 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
599 if (*s == NUL && c == '0')
600#ifdef EBCDIC
601 add_buff(&redobuff, (char_u *)"xf0", 3L);
602#else
603 add_buff(&redobuff, (char_u *)"048", 3L);
604#endif
605 else
606 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608}
609
610/*
611 * Append a character to the redo buffer.
612 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
613 */
614 void
615AppendCharToRedobuff(c)
616 int c;
617{
618 if (!block_redo)
619 add_char_buff(&redobuff, c);
620}
621
622/*
623 * Append a number to the redo buffer.
624 */
625 void
626AppendNumberToRedobuff(n)
627 long n;
628{
629 if (!block_redo)
630 add_num_buff(&redobuff, n);
631}
632
633/*
634 * Append string "s" to the stuff buffer.
635 * CSI and K_SPECIAL must already have been escaped.
636 */
637 void
638stuffReadbuff(s)
639 char_u *s;
640{
641 add_buff(&stuffbuff, s, -1L);
642}
643
644 void
645stuffReadbuffLen(s, len)
646 char_u *s;
647 long len;
648{
649 add_buff(&stuffbuff, s, len);
650}
651
652#if defined(FEAT_EVAL) || defined(PROTO)
653/*
654 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
655 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200656 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 */
658 void
659stuffReadbuffSpec(s)
660 char_u *s;
661{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200662 int c;
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 while (*s != NUL)
665 {
666 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
667 {
668 /* Insert special key literally. */
669 stuffReadbuffLen(s, 3L);
670 s += 3;
671 }
672 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#ifdef FEAT_MBYTE
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200675 c = mb_ptr2char_adv(&s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200677 c = *s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#endif
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200679 if (c == CAR || c == NL || c == ESC)
680 c = ' ';
681 stuffcharReadbuff(c);
682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684}
685#endif
686
687/*
688 * Append a character to the stuff buffer.
689 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
690 */
691 void
692stuffcharReadbuff(c)
693 int c;
694{
695 add_char_buff(&stuffbuff, c);
696}
697
698/*
699 * Append a number to the stuff buffer.
700 */
701 void
702stuffnumReadbuff(n)
703 long n;
704{
705 add_num_buff(&stuffbuff, n);
706}
707
708/*
709 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
710 * multibyte characters.
711 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100712 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
713 * otherwise.
714 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 */
716 static int
717read_redo(init, old_redo)
718 int init;
719 int old_redo;
720{
721 static struct buffblock *bp;
722 static char_u *p;
723 int c;
724#ifdef FEAT_MBYTE
725 int n;
726 char_u buf[MB_MAXBYTES];
727 int i;
728#endif
729
730 if (init)
731 {
732 if (old_redo)
733 bp = old_redobuff.bh_first.b_next;
734 else
735 bp = redobuff.bh_first.b_next;
736 if (bp == NULL)
737 return FAIL;
738 p = bp->b_str;
739 return OK;
740 }
741 if ((c = *p) != NUL)
742 {
743 /* Reverse the conversion done by add_char_buff() */
744#ifdef FEAT_MBYTE
745 /* For a multi-byte character get all the bytes and return the
746 * converted character. */
747 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
748 n = MB_BYTE2LEN_CHECK(c);
749 else
750 n = 1;
751 for (i = 0; ; ++i)
752#endif
753 {
754 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
755 {
756 c = TO_SPECIAL(p[1], p[2]);
757 p += 2;
758 }
759#ifdef FEAT_GUI
760 if (c == CSI) /* escaped CSI */
761 p += 2;
762#endif
763 if (*++p == NUL && bp->b_next != NULL)
764 {
765 bp = bp->b_next;
766 p = bp->b_str;
767 }
768#ifdef FEAT_MBYTE
769 buf[i] = c;
770 if (i == n - 1) /* last byte of a character */
771 {
772 if (n != 1)
773 c = (*mb_ptr2char)(buf);
774 break;
775 }
776 c = *p;
777 if (c == NUL) /* cannot happen? */
778 break;
779#endif
780 }
781 }
782
783 return c;
784}
785
786/*
787 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
788 * If old_redo is TRUE, use old_redobuff instead of redobuff.
789 * The escaped K_SPECIAL and CSI are copied without translation.
790 */
791 static void
792copy_redo(old_redo)
793 int old_redo;
794{
795 int c;
796
797 while ((c = read_redo(FALSE, old_redo)) != NUL)
798 stuffcharReadbuff(c);
799}
800
801/*
802 * Stuff the redo buffer into the stuffbuff.
803 * Insert the redo count into the command.
804 * If "old_redo" is TRUE, the last but one command is repeated
805 * instead of the last command (inserting text). This is used for
806 * CTRL-O <.> in insert mode
807 *
808 * return FAIL for failure, OK otherwise
809 */
810 int
811start_redo(count, old_redo)
812 long count;
813 int old_redo;
814{
815 int c;
816
817 /* init the pointers; return if nothing to redo */
818 if (read_redo(TRUE, old_redo) == FAIL)
819 return FAIL;
820
821 c = read_redo(FALSE, old_redo);
822
823 /* copy the buffer name, if present */
824 if (c == '"')
825 {
826 add_buff(&stuffbuff, (char_u *)"\"", 1L);
827 c = read_redo(FALSE, old_redo);
828
829 /* if a numbered buffer is used, increment the number */
830 if (c >= '1' && c < '9')
831 ++c;
832 add_char_buff(&stuffbuff, c);
833 c = read_redo(FALSE, old_redo);
834 }
835
836#ifdef FEAT_VISUAL
837 if (c == 'v') /* redo Visual */
838 {
839 VIsual = curwin->w_cursor;
840 VIsual_active = TRUE;
841 VIsual_select = FALSE;
842 VIsual_reselect = TRUE;
843 redo_VIsual_busy = TRUE;
844 c = read_redo(FALSE, old_redo);
845 }
846#endif
847
848 /* try to enter the count (in place of a previous count) */
849 if (count)
850 {
851 while (VIM_ISDIGIT(c)) /* skip "old" count */
852 c = read_redo(FALSE, old_redo);
853 add_num_buff(&stuffbuff, count);
854 }
855
856 /* copy from the redo buffer into the stuff buffer */
857 add_char_buff(&stuffbuff, c);
858 copy_redo(old_redo);
859 return OK;
860}
861
862/*
863 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
864 * the redo buffer into the stuffbuff.
865 * return FAIL for failure, OK otherwise
866 */
867 int
868start_redo_ins()
869{
870 int c;
871
872 if (read_redo(TRUE, FALSE) == FAIL)
873 return FAIL;
874 start_stuff();
875
876 /* skip the count and the command character */
877 while ((c = read_redo(FALSE, FALSE)) != NUL)
878 {
879 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
880 {
881 if (c == 'O' || c == 'o')
882 stuffReadbuff(NL_STR);
883 break;
884 }
885 }
886
887 /* copy the typed text from the redo buffer into the stuff buffer */
888 copy_redo(FALSE);
889 block_redo = TRUE;
890 return OK;
891}
892
893 void
894stop_redo_ins()
895{
896 block_redo = FALSE;
897}
898
899/*
900 * Initialize typebuf.tb_buf to point to typebuf_init.
901 * alloc() cannot be used here: In out-of-memory situations it would
902 * be impossible to type anything.
903 */
904 static void
905init_typebuf()
906{
907 if (typebuf.tb_buf == NULL)
908 {
909 typebuf.tb_buf = typebuf_init;
910 typebuf.tb_noremap = noremapbuf_init;
911 typebuf.tb_buflen = TYPELEN_INIT;
912 typebuf.tb_len = 0;
913 typebuf.tb_off = 0;
914 typebuf.tb_change_cnt = 1;
915 }
916}
917
918/*
919 * insert a string in position 'offset' in the typeahead buffer (for "@r"
920 * and ":normal" command, vgetorpeek() and check_termcode())
921 *
922 * If noremap is REMAP_YES, new string can be mapped again.
923 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000924 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
925 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
927 * script-local mappings.
928 * If noremap is > 0, that many characters of the new string cannot be mapped.
929 *
930 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
931 * offset is non-zero!).
932 *
933 * If silent is TRUE, cmd_silent is set when the characters are obtained.
934 *
935 * return FAIL for failure, OK otherwise
936 */
937 int
938ins_typebuf(str, noremap, offset, nottyped, silent)
939 char_u *str;
940 int noremap;
941 int offset;
942 int nottyped;
943 int silent;
944{
945 char_u *s1, *s2;
946 int newlen;
947 int addlen;
948 int i;
949 int newoff;
950 int val;
951 int nrm;
952
953 init_typebuf();
954 if (++typebuf.tb_change_cnt == 0)
955 typebuf.tb_change_cnt = 1;
956
957 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /*
960 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
961 */
962 if (offset == 0 && addlen <= typebuf.tb_off)
963 {
964 typebuf.tb_off -= addlen;
965 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
966 }
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 /*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000969 * Need to allocate a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
971 * characters. We add some extra room to avoid having to allocate too
972 * often.
973 */
974 else
975 {
976 newoff = MAXMAPLEN + 4;
977 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
978 if (newlen < 0) /* string is getting too long */
979 {
980 EMSG(_(e_toocompl)); /* also calls flush_buffers */
981 setcursor();
982 return FAIL;
983 }
984 s1 = alloc(newlen);
985 if (s1 == NULL) /* out of memory */
986 return FAIL;
987 s2 = alloc(newlen);
988 if (s2 == NULL) /* out of memory */
989 {
990 vim_free(s1);
991 return FAIL;
992 }
993 typebuf.tb_buflen = newlen;
994
995 /* copy the old chars, before the insertion point */
996 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
997 (size_t)offset);
998 /* copy the new chars */
999 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
1000 /* copy the old chars, after the insertion point, including the NUL at
1001 * the end */
1002 mch_memmove(s1 + newoff + offset + addlen,
1003 typebuf.tb_buf + typebuf.tb_off + offset,
1004 (size_t)(typebuf.tb_len - offset + 1));
1005 if (typebuf.tb_buf != typebuf_init)
1006 vim_free(typebuf.tb_buf);
1007 typebuf.tb_buf = s1;
1008
1009 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1010 (size_t)offset);
1011 mch_memmove(s2 + newoff + offset + addlen,
1012 typebuf.tb_noremap + typebuf.tb_off + offset,
1013 (size_t)(typebuf.tb_len - offset));
1014 if (typebuf.tb_noremap != noremapbuf_init)
1015 vim_free(typebuf.tb_noremap);
1016 typebuf.tb_noremap = s2;
1017
1018 typebuf.tb_off = newoff;
1019 }
1020 typebuf.tb_len += addlen;
1021
1022 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
1023 if (noremap == REMAP_SCRIPT)
1024 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001025 else if (noremap == REMAP_SKIP)
1026 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else
1028 val = RM_NONE;
1029
1030 /*
1031 * Adjust typebuf.tb_noremap[] for the new characters:
1032 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1033 * (sometimes) not remappable
1034 * If noremap == REMAP_YES: all the new characters are mappable
1035 * If noremap > 0: "noremap" characters are not remappable, the rest
1036 * mappable
1037 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001038 if (noremap == REMAP_SKIP)
1039 nrm = 1;
1040 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 nrm = addlen;
1042 else
1043 nrm = noremap;
1044 for (i = 0; i < addlen; ++i)
1045 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1046 (--nrm >= 0) ? val : RM_YES;
1047
1048 /* tb_maplen and tb_silent only remember the length of mapped and/or
1049 * silent mappings at the start of the buffer, assuming that a mapped
1050 * sequence doesn't result in typed characters. */
1051 if (nottyped || typebuf.tb_maplen > offset)
1052 typebuf.tb_maplen += addlen;
1053 if (silent || typebuf.tb_silent > offset)
1054 {
1055 typebuf.tb_silent += addlen;
1056 cmd_silent = TRUE;
1057 }
1058 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1059 typebuf.tb_no_abbr_cnt += addlen;
1060
1061 return OK;
1062}
1063
1064/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001065 * Put character "c" back into the typeahead buffer.
1066 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001067 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1068 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001069 */
1070 void
1071ins_char_typebuf(c)
1072 int c;
1073{
1074#ifdef FEAT_MBYTE
1075 char_u buf[MB_MAXBYTES];
1076#else
1077 char_u buf[4];
1078#endif
1079 if (IS_SPECIAL(c))
1080 {
1081 buf[0] = K_SPECIAL;
1082 buf[1] = K_SECOND(c);
1083 buf[2] = K_THIRD(c);
1084 buf[3] = NUL;
1085 }
1086 else
1087 {
1088#ifdef FEAT_MBYTE
1089 buf[(*mb_char2bytes)(c, buf)] = NUL;
1090#else
1091 buf[0] = c;
1092 buf[1] = NUL;
1093#endif
1094 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001095 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001096}
1097
1098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001100 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001101 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1103 * changed it was reallocated and the old pointer can no longer be used.
1104 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1105 * that was just added.
1106 */
1107 int
1108typebuf_changed(tb_change_cnt)
1109 int tb_change_cnt; /* old value of typebuf.tb_change_cnt */
1110{
1111 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001112#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1113 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#endif
1115 ));
1116}
1117
1118/*
1119 * Return TRUE if there are no characters in the typeahead buffer that have
1120 * not been typed (result from a mapping or come from ":normal").
1121 */
1122 int
1123typebuf_typed()
1124{
1125 return typebuf.tb_maplen == 0;
1126}
1127
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001128#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129/*
1130 * Return the number of characters that are mapped (or not typed).
1131 */
1132 int
1133typebuf_maplen()
1134{
1135 return typebuf.tb_maplen;
1136}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
1139/*
1140 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1141 */
1142 void
1143del_typebuf(len, offset)
1144 int len;
1145 int offset;
1146{
1147 int i;
1148
1149 if (len == 0)
1150 return; /* nothing to do */
1151
1152 typebuf.tb_len -= len;
1153
1154 /*
1155 * Easy case: Just increase typebuf.tb_off.
1156 */
1157 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1158 >= 3 * MAXMAPLEN + 3)
1159 typebuf.tb_off += len;
1160 /*
1161 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1162 */
1163 else
1164 {
1165 i = typebuf.tb_off + offset;
1166 /*
1167 * Leave some extra room at the end to avoid reallocation.
1168 */
1169 if (typebuf.tb_off > MAXMAPLEN)
1170 {
1171 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1172 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1173 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1174 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1175 typebuf.tb_off = MAXMAPLEN;
1176 }
1177 /* adjust typebuf.tb_buf (include the NUL at the end) */
1178 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1179 typebuf.tb_buf + i + len,
1180 (size_t)(typebuf.tb_len - offset + 1));
1181 /* adjust typebuf.tb_noremap[] */
1182 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1183 typebuf.tb_noremap + i + len,
1184 (size_t)(typebuf.tb_len - offset));
1185 }
1186
1187 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1188 {
1189 if (typebuf.tb_maplen < offset + len)
1190 typebuf.tb_maplen = offset;
1191 else
1192 typebuf.tb_maplen -= len;
1193 }
1194 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1195 {
1196 if (typebuf.tb_silent < offset + len)
1197 typebuf.tb_silent = offset;
1198 else
1199 typebuf.tb_silent -= len;
1200 }
1201 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1202 {
1203 if (typebuf.tb_no_abbr_cnt < offset + len)
1204 typebuf.tb_no_abbr_cnt = offset;
1205 else
1206 typebuf.tb_no_abbr_cnt -= len;
1207 }
1208
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001209#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 /* Reset the flag that text received from a client or from feedkeys()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001211 * was inserted in the typeahead buffer. */
1212 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
1214 if (++typebuf.tb_change_cnt == 0)
1215 typebuf.tb_change_cnt = 1;
1216}
1217
1218/*
1219 * Write typed characters to script file.
1220 * If recording is on put the character in the recordbuffer.
1221 */
1222 static void
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001223gotchars(chars, len)
1224 char_u *chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 int len;
1226{
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001227 char_u *s = chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 int c;
1229 char_u buf[2];
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001230 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
1232 /* remember how many chars were last recorded */
1233 if (Recording)
1234 last_recorded_len += len;
1235
1236 buf[1] = NUL;
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001237 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 /* Handle one byte at a time; no translation to be done. */
1240 c = *s++;
1241 updatescript(c);
1242
1243 if (Recording)
1244 {
1245 buf[0] = c;
1246 add_buff(&recordbuff, buf, 1L);
1247 }
1248 }
1249 may_sync_undo();
1250
1251#ifdef FEAT_EVAL
1252 /* output "debug mode" message next time in debug mode */
1253 debug_did_msg = FALSE;
1254#endif
1255
1256 /* Since characters have been typed, consider the following to be in
1257 * another mapping. Search string will be kept in history. */
1258 ++maptick;
1259}
1260
1261/*
1262 * Sync undo. Called when typed characters are obtained from the typeahead
1263 * buffer, or when a menu is used.
1264 * Do not sync:
1265 * - In Insert mode, unless cursor key has been used.
1266 * - While reading a script file.
1267 * - When no_u_sync is non-zero.
1268 */
1269 static void
1270may_sync_undo()
1271{
1272 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001273 && scriptin[curscript] == NULL)
1274 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275}
1276
1277/*
1278 * Make "typebuf" empty and allocate new buffers.
1279 * Returns FAIL when out of memory.
1280 */
1281 int
1282alloc_typebuf()
1283{
1284 typebuf.tb_buf = alloc(TYPELEN_INIT);
1285 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1286 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1287 {
1288 free_typebuf();
1289 return FAIL;
1290 }
1291 typebuf.tb_buflen = TYPELEN_INIT;
1292 typebuf.tb_off = 0;
1293 typebuf.tb_len = 0;
1294 typebuf.tb_maplen = 0;
1295 typebuf.tb_silent = 0;
1296 typebuf.tb_no_abbr_cnt = 0;
1297 if (++typebuf.tb_change_cnt == 0)
1298 typebuf.tb_change_cnt = 1;
1299 return OK;
1300}
1301
1302/*
1303 * Free the buffers of "typebuf".
1304 */
1305 void
1306free_typebuf()
1307{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001308 if (typebuf.tb_buf == typebuf_init)
1309 EMSG2(_(e_intern2), "Free typebuf 1");
1310 else
1311 vim_free(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001312 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001313 EMSG2(_(e_intern2), "Free typebuf 2");
1314 else
1315 vim_free(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316}
1317
1318/*
1319 * When doing ":so! file", the current typeahead needs to be saved, and
1320 * restored when "file" has been read completely.
1321 */
1322static typebuf_T saved_typebuf[NSCRIPT];
1323
1324 int
1325save_typebuf()
1326{
1327 init_typebuf();
1328 saved_typebuf[curscript] = typebuf;
1329 /* If out of memory: restore typebuf and close file. */
1330 if (alloc_typebuf() == FAIL)
1331 {
1332 closescript();
1333 return FAIL;
1334 }
1335 return OK;
1336}
1337
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001338static int old_char = -1; /* character put back by vungetc() */
1339static int old_mod_mask; /* mod_mask for ungotten character */
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1342
1343/*
1344 * Save all three kinds of typeahead, so that the user must type at a prompt.
1345 */
1346 void
1347save_typeahead(tp)
1348 tasave_T *tp;
1349{
1350 tp->save_typebuf = typebuf;
1351 tp->typebuf_valid = (alloc_typebuf() == OK);
1352 if (!tp->typebuf_valid)
1353 typebuf = tp->save_typebuf;
1354
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001355 tp->old_char = old_char;
1356 tp->old_mod_mask = old_mod_mask;
1357 old_char = -1;
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 tp->save_stuffbuff = stuffbuff;
1360 stuffbuff.bh_first.b_next = NULL;
1361# ifdef USE_INPUT_BUF
1362 tp->save_inputbuf = get_input_buf();
1363# endif
1364}
1365
1366/*
1367 * Restore the typeahead to what it was before calling save_typeahead().
1368 * The allocated memory is freed, can only be called once!
1369 */
1370 void
1371restore_typeahead(tp)
1372 tasave_T *tp;
1373{
1374 if (tp->typebuf_valid)
1375 {
1376 free_typebuf();
1377 typebuf = tp->save_typebuf;
1378 }
1379
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001380 old_char = tp->old_char;
1381 old_mod_mask = tp->old_mod_mask;
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 free_buff(&stuffbuff);
1384 stuffbuff = tp->save_stuffbuff;
1385# ifdef USE_INPUT_BUF
1386 set_input_buf(tp->save_inputbuf);
1387# endif
1388}
1389#endif
1390
1391/*
1392 * Open a new script file for the ":source!" command.
1393 */
1394 void
1395openscript(name, directly)
1396 char_u *name;
1397 int directly; /* when TRUE execute directly */
1398{
1399 if (curscript + 1 == NSCRIPT)
1400 {
1401 EMSG(_(e_nesting));
1402 return;
1403 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001404#ifdef FEAT_EVAL
1405 if (ignore_script)
1406 /* Not reading from script, also don't open one. Warning message? */
1407 return;
1408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 if (scriptin[curscript] != NULL) /* already reading script */
1411 ++curscript;
1412 /* use NameBuff for expanded name */
1413 expand_env(name, NameBuff, MAXPATHL);
1414 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1415 {
1416 EMSG2(_(e_notopen), name);
1417 if (curscript)
1418 --curscript;
1419 return;
1420 }
1421 if (save_typebuf() == FAIL)
1422 return;
1423
1424 /*
1425 * Execute the commands from the file right now when using ":source!"
1426 * after ":global" or ":argdo" or in a loop. Also when another command
1427 * follows. This means the display won't be updated. Don't do this
1428 * always, "make test" would fail.
1429 */
1430 if (directly)
1431 {
1432 oparg_T oa;
1433 int oldcurscript;
1434 int save_State = State;
1435 int save_restart_edit = restart_edit;
1436 int save_insertmode = p_im;
1437 int save_finish_op = finish_op;
1438 int save_msg_scroll = msg_scroll;
1439
1440 State = NORMAL;
1441 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1442 restart_edit = 0; /* don't go to Insert mode */
1443 p_im = FALSE; /* don't use 'insertmode' */
1444 clear_oparg(&oa);
1445 finish_op = FALSE;
1446
1447 oldcurscript = curscript;
1448 do
1449 {
1450 update_topline_cursor(); /* update cursor position and topline */
1451 normal_cmd(&oa, FALSE); /* execute one command */
1452 vpeekc(); /* check for end of file */
1453 }
1454 while (scriptin[oldcurscript] != NULL);
1455
1456 State = save_State;
1457 msg_scroll = save_msg_scroll;
1458 restart_edit = save_restart_edit;
1459 p_im = save_insertmode;
1460 finish_op = save_finish_op;
1461 }
1462}
1463
1464/*
1465 * Close the currently active input script.
1466 */
1467 static void
1468closescript()
1469{
1470 free_typebuf();
1471 typebuf = saved_typebuf[curscript];
1472
1473 fclose(scriptin[curscript]);
1474 scriptin[curscript] = NULL;
1475 if (curscript > 0)
1476 --curscript;
1477}
1478
Bram Moolenaarea408852005-06-25 22:49:46 +00001479#if defined(EXITFREE) || defined(PROTO)
1480 void
1481close_all_scripts()
1482{
1483 while (scriptin[0] != NULL)
1484 closescript();
1485}
1486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1489/*
1490 * Return TRUE when reading keys from a script file.
1491 */
1492 int
1493using_script()
1494{
1495 return scriptin[curscript] != NULL;
1496}
1497#endif
1498
1499/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001500 * This function is called just before doing a blocking wait. Thus after
1501 * waiting 'updatetime' for a character to arrive.
1502 */
1503 void
1504before_blocking()
1505{
1506 updatescript(0);
1507#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001508 if (may_garbage_collect)
1509 garbage_collect();
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001510#endif
1511}
1512
1513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * updatescipt() is called when a character can be written into the script file
1515 * or when we have waited some time for a character (c == 0)
1516 *
1517 * All the changed memfiles are synced if c == 0 or when the number of typed
1518 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1519 */
1520 void
1521updatescript(c)
1522 int c;
1523{
1524 static int count = 0;
1525
1526 if (c && scriptout)
1527 putc(c, scriptout);
1528 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1529 {
1530 ml_sync_all(c == 0, TRUE);
1531 count = 0;
1532 }
1533}
1534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535/*
1536 * Get the next input character.
1537 * Can return a special key or a multi-byte character.
1538 * Can return NUL when called recursively, use safe_vgetc() if that's not
1539 * wanted.
1540 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1541 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001542 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 int
1545vgetc()
1546{
1547 int c, c2;
1548#ifdef FEAT_MBYTE
1549 int n;
1550 char_u buf[MB_MAXBYTES];
1551 int i;
1552#endif
1553
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001554#ifdef FEAT_EVAL
1555 /* Do garbage collection when garbagecollect() was called previously and
1556 * we are now at the toplevel. */
1557 if (may_garbage_collect && want_garbage_collect)
1558 garbage_collect();
1559#endif
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 /*
1562 * If a character was put back with vungetc, it was already processed.
1563 * Return it directly.
1564 */
1565 if (old_char != -1)
1566 {
1567 c = old_char;
1568 old_char = -1;
1569 mod_mask = old_mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001573 mod_mask = 0x0;
1574 last_recorded_len = 0;
1575 for (;;) /* this is done twice if there are modifiers */
1576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (mod_mask) /* no mapping after modifier has been read */
1578 {
1579 ++no_mapping;
1580 ++allow_keys;
1581 }
1582 c = vgetorpeek(TRUE);
1583 if (mod_mask)
1584 {
1585 --no_mapping;
1586 --allow_keys;
1587 }
1588
1589 /* Get two extra bytes for special keys */
1590 if (c == K_SPECIAL
1591#ifdef FEAT_GUI
1592 || c == CSI
1593#endif
1594 )
1595 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001596 int save_allow_keys = allow_keys;
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 ++no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001599 allow_keys = 0; /* make sure BS is not found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1601 c = vgetorpeek(TRUE);
1602 --no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001603 allow_keys = save_allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (c2 == KS_MODIFIER)
1605 {
1606 mod_mask = c;
1607 continue;
1608 }
1609 c = TO_SPECIAL(c2, c);
1610
1611#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1612 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1613 * know that a menu was torn off */
1614 if (c == K_TEAROFF)
1615 {
1616 char_u name[200];
1617 int i;
1618
1619 /* get menu path, it ends with a <CR> */
1620 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1621 {
1622 name[i] = c;
1623 if (i < 199)
1624 ++i;
1625 }
1626 name[i] = NUL;
1627 gui_make_tearoff(name);
1628 continue;
1629 }
1630#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001631#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632 /* GTK: <F10> normally selects the menu, but it's passed until
1633 * here to allow mapping it. Intercept and invoke the GTK
1634 * behavior if it's not mapped. */
1635 if (c == K_F10 && gui.menubar != NULL)
1636 {
1637 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1638 continue;
1639 }
1640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#ifdef FEAT_GUI
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001642 /* Handle focus event here, so that the caller doesn't need to
1643 * know about it. Return K_IGNORE so that we loop once (needed if
1644 * 'lazyredraw' is set). */
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001645 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1646 {
1647 ui_focus_change(c == K_FOCUSGAINED);
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001648 c = K_IGNORE;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001649 }
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 /* Translate K_CSI to CSI. The special key is only used to avoid
1652 * it being recognized as the start of a special key. */
1653 if (c == K_CSI)
1654 c = CSI;
1655#endif
1656 }
1657#ifdef MSDOS
1658 /*
1659 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1660 * Delete the 3 here.
1661 */
1662 else if (c == K_NUL && vpeekc() == 3)
1663 (void)vgetorpeek(TRUE);
1664#endif
1665
Bram Moolenaara88d9682005-03-25 21:45:43 +00001666 /* a keypad or special function key was not mapped, use it like
1667 * its ASCII equivalent */
1668 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
Bram Moolenaara88d9682005-03-25 21:45:43 +00001670 case K_KPLUS: c = '+'; break;
1671 case K_KMINUS: c = '-'; break;
1672 case K_KDIVIDE: c = '/'; break;
1673 case K_KMULTIPLY: c = '*'; break;
1674 case K_KENTER: c = CAR; break;
1675 case K_KPOINT:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001676#ifdef WIN32
Bram Moolenaara88d9682005-03-25 21:45:43 +00001677 /* Can be either '.' or a ',', *
1678 * depending on the type of keypad. */
1679 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001680#else
Bram Moolenaara88d9682005-03-25 21:45:43 +00001681 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001682#endif
Bram Moolenaara88d9682005-03-25 21:45:43 +00001683 case K_K0: c = '0'; break;
1684 case K_K1: c = '1'; break;
1685 case K_K2: c = '2'; break;
1686 case K_K3: c = '3'; break;
1687 case K_K4: c = '4'; break;
1688 case K_K5: c = '5'; break;
1689 case K_K6: c = '6'; break;
1690 case K_K7: c = '7'; break;
1691 case K_K8: c = '8'; break;
1692 case K_K9: c = '9'; break;
1693
1694 case K_XHOME:
1695 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1696 {
1697 c = K_S_HOME;
1698 mod_mask = 0;
1699 }
1700 else if (mod_mask == MOD_MASK_CTRL)
1701 {
1702 c = K_C_HOME;
1703 mod_mask = 0;
1704 }
1705 else
1706 c = K_HOME;
1707 break;
1708 case K_XEND:
1709 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1710 {
1711 c = K_S_END;
1712 mod_mask = 0;
1713 }
1714 else if (mod_mask == MOD_MASK_CTRL)
1715 {
1716 c = K_C_END;
1717 mod_mask = 0;
1718 }
1719 else
1720 c = K_END;
1721 break;
1722
1723 case K_XUP: c = K_UP; break;
1724 case K_XDOWN: c = K_DOWN; break;
1725 case K_XLEFT: c = K_LEFT; break;
1726 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728
1729#ifdef FEAT_MBYTE
1730 /* For a multi-byte character get all the bytes and return the
1731 * converted character.
1732 * Note: This will loop until enough bytes are received!
1733 */
1734 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1735 {
1736 ++no_mapping;
1737 buf[0] = c;
1738 for (i = 1; i < n; ++i)
1739 {
1740 buf[i] = vgetorpeek(TRUE);
1741 if (buf[i] == K_SPECIAL
1742#ifdef FEAT_GUI
1743 || buf[i] == CSI
1744#endif
1745 )
1746 {
1747 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1748 * which represents a K_SPECIAL (0x80),
1749 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1750 * a CSI (0x9B),
1751 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1752 c = vgetorpeek(TRUE);
1753 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1754 buf[i] = CSI;
1755 }
1756 }
1757 --no_mapping;
1758 c = (*mb_ptr2char)(buf);
1759 }
1760#endif
1761
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001762 break;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001765
1766#ifdef FEAT_EVAL
1767 /*
1768 * In the main loop "may_garbage_collect" can be set to do garbage
1769 * collection in the first next vgetc(). It's disabled after that to
1770 * avoid internally used Lists and Dicts to be freed.
1771 */
1772 may_garbage_collect = FALSE;
1773#endif
1774
1775 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776}
1777
1778/*
1779 * Like vgetc(), but never return a NUL when called recursively, get a key
1780 * directly from the user (ignoring typeahead).
1781 */
1782 int
1783safe_vgetc()
1784{
1785 int c;
1786
1787 c = vgetc();
1788 if (c == NUL)
1789 c = get_keystroke();
1790 return c;
1791}
1792
1793/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001794 * Like safe_vgetc(), but loop to handle K_IGNORE.
1795 * Also ignore scrollbar events.
1796 */
1797 int
1798plain_vgetc()
1799{
1800 int c;
1801
1802 do
1803 {
1804 c = safe_vgetc();
1805 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1806 return c;
1807}
1808
1809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 * Check if a character is available, such that vgetc() will not block.
1811 * If the next character is a special character or multi-byte, the returned
1812 * character is not valid!.
1813 */
1814 int
1815vpeekc()
1816{
1817 if (old_char != -1)
1818 return old_char;
1819 return vgetorpeek(FALSE);
1820}
1821
1822#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1823/*
1824 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1825 * codes.
1826 */
1827 int
1828vpeekc_nomap()
1829{
1830 int c;
1831
1832 ++no_mapping;
1833 ++allow_keys;
1834 c = vpeekc();
1835 --no_mapping;
1836 --allow_keys;
1837 return c;
1838}
1839#endif
1840
1841#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1842/*
1843 * Check if any character is available, also half an escape sequence.
1844 * Trick: when no typeahead found, but there is something in the typeahead
1845 * buffer, it must be an ESC that is recognized as the start of a key code.
1846 */
1847 int
1848vpeekc_any()
1849{
1850 int c;
1851
1852 c = vpeekc();
1853 if (c == NUL && typebuf.tb_len > 0)
1854 c = ESC;
1855 return c;
1856}
1857#endif
1858
1859/*
1860 * Call vpeekc() without causing anything to be mapped.
1861 * Return TRUE if a character is available, FALSE otherwise.
1862 */
1863 int
1864char_avail()
1865{
1866 int retval;
1867
1868 ++no_mapping;
1869 retval = vpeekc();
1870 --no_mapping;
1871 return (retval != NUL);
1872}
1873
1874 void
1875vungetc(c) /* unget one character (can only be done once!) */
1876 int c;
1877{
1878 old_char = c;
1879 old_mod_mask = mod_mask;
1880}
1881
1882/*
1883 * get a character:
1884 * 1. from the stuffbuffer
1885 * This is used for abbreviated commands like "D" -> "d$".
1886 * Also used to redo a command for ".".
1887 * 2. from the typeahead buffer
1888 * Stores text obtained previously but not used yet.
1889 * Also stores the result of mappings.
1890 * Also used for the ":normal" command.
1891 * 3. from the user
1892 * This may do a blocking wait if "advance" is TRUE.
1893 *
1894 * if "advance" is TRUE (vgetc()):
1895 * really get the character.
1896 * KeyTyped is set to TRUE in the case the user typed the key.
1897 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1898 * if "advance" is FALSE (vpeekc()):
1899 * just look whether there is a character available.
1900 *
1901 * When "no_mapping" is zero, checks for mappings in the current mode.
1902 * Only returns one byte (of a multi-byte character).
1903 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1904 */
1905 static int
1906vgetorpeek(advance)
1907 int advance;
1908{
1909 int c, c1;
1910 int keylen;
1911 char_u *s;
1912 mapblock_T *mp;
1913#ifdef FEAT_LOCALMAP
1914 mapblock_T *mp2;
1915#endif
1916 mapblock_T *mp_match;
1917 int mp_match_len = 0;
1918 int timedout = FALSE; /* waited for more than 1 second
1919 for mapping to complete */
1920 int mapdepth = 0; /* check for recursive mapping */
1921 int mode_deleted = FALSE; /* set when mode has been deleted */
1922 int local_State;
1923 int mlen;
1924 int max_mlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 int i;
Bram Moolenaar4e427192006-03-10 21:34:27 +00001926#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 int new_wcol, new_wrow;
1928#endif
1929#ifdef FEAT_GUI
1930# ifdef FEAT_MENU
1931 int idx;
1932# endif
1933 int shape_changed = FALSE; /* adjusted cursor shape */
1934#endif
1935 int n;
1936#ifdef FEAT_LANGMAP
1937 int nolmaplen;
1938#endif
1939 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00001940 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
1942 /*
1943 * This function doesn't work very well when called recursively. This may
1944 * happen though, because of:
1945 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1946 * there is a character available, which calls this function. In that
1947 * case we must return NUL, to indicate no character is available.
1948 * 2. A GUI callback function writes to the screen, causing a
1949 * wait_return().
1950 * Using ":normal" can also do this, but it saves the typeahead buffer,
1951 * thus it should be OK. But don't get a key from the user then.
1952 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001953 if (vgetc_busy > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#ifdef FEAT_EX_EXTRA
1955 && ex_normal_busy == 0
1956#endif
1957 )
1958 return NUL;
1959
1960 local_State = get_real_state();
1961
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001962 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 if (advance)
1965 KeyStuffed = FALSE;
1966
1967 init_typebuf();
1968 start_stuff();
1969 if (advance && typebuf.tb_maplen == 0)
1970 Exec_reg = FALSE;
1971 do
1972 {
1973/*
1974 * get a character: 1. from the stuffbuffer
1975 */
1976 if (typeahead_char != 0)
1977 {
1978 c = typeahead_char;
1979 if (advance)
1980 typeahead_char = 0;
1981 }
1982 else
1983 c = read_stuff(advance);
1984 if (c != NUL && !got_int)
1985 {
1986 if (advance)
1987 {
1988 /* KeyTyped = FALSE; When the command that stuffed something
1989 * was typed, behave like the stuffed command was typed.
1990 * needed for CTRL-W CTRl-] to open a fold, for example. */
1991 KeyStuffed = TRUE;
1992 }
1993 if (typebuf.tb_no_abbr_cnt == 0)
1994 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
1995 }
1996 else
1997 {
1998 /*
1999 * Loop until we either find a matching mapped key, or we
2000 * are sure that it is not a mapped key.
2001 * If a mapped key sequence is found we go back to the start to
2002 * try re-mapping.
2003 */
2004 for (;;)
2005 {
2006 /*
2007 * ui_breakcheck() is slow, don't use it too often when
2008 * inside a mapping. But call it each time for typed
2009 * characters.
2010 */
2011 if (typebuf.tb_maplen)
2012 line_breakcheck();
2013 else
2014 ui_breakcheck(); /* check for CTRL-C */
2015 keylen = 0;
2016 if (got_int)
2017 {
2018 /* flush all input */
2019 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
2020 typebuf.tb_change_cnt);
2021 /*
2022 * If inchar() returns TRUE (script file was active) or we
2023 * are inside a mapping, get out of insert mode.
2024 * Otherwise we behave like having gotten a CTRL-C.
2025 * As a result typing CTRL-C in insert mode will
2026 * really insert a CTRL-C.
2027 */
2028 if ((c || typebuf.tb_maplen)
2029 && (State & (INSERT + CMDLINE)))
2030 c = ESC;
2031 else
2032 c = Ctrl_C;
2033 flush_buffers(TRUE); /* flush all typeahead */
2034
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002035 if (advance)
2036 {
2037 /* Also record this character, it might be needed to
2038 * get out of Insert mode. */
2039 *typebuf.tb_buf = c;
2040 gotchars(typebuf.tb_buf, 1);
2041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 cmd_silent = FALSE;
2043
2044 break;
2045 }
2046 else if (typebuf.tb_len > 0)
2047 {
2048 /*
2049 * Check for a mappable key sequence.
2050 * Walk through one maphash[] list until we find an
2051 * entry that matches.
2052 *
2053 * Don't look for mappings if:
2054 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2055 * - maphash_valid not set: no mappings present.
2056 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2057 * - in insert or cmdline mode and 'paste' option set
2058 * - waiting for "hit return to continue" and CR or SPACE
2059 * typed
2060 * - waiting for a char with --more--
2061 * - in Ctrl-X mode, and we get a valid char for that mode
2062 */
2063 mp = NULL;
2064 max_mlen = 0;
2065 c1 = typebuf.tb_buf[typebuf.tb_off];
2066 if (no_mapping == 0 && maphash_valid
2067 && (no_zero_mapping == 0 || c1 != '0')
2068 && (typebuf.tb_maplen == 0
2069 || (p_remap
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002070 && (typebuf.tb_noremap[typebuf.tb_off]
2071 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 && !(p_paste && (State & (INSERT + CMDLINE)))
2073 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2074 && State != ASKMORE
2075 && State != CONFIRM
2076#ifdef FEAT_INS_EXPAND
2077 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002078 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2080#endif
2081 )
2082 {
2083#ifdef FEAT_LANGMAP
2084 if (c1 == K_SPECIAL)
2085 nolmaplen = 2;
2086 else
2087 {
2088 LANGMAP_ADJUST(c1, TRUE);
2089 nolmaplen = 0;
2090 }
2091#endif
2092#ifdef FEAT_LOCALMAP
2093 /* First try buffer-local mappings. */
2094 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2095 mp2 = maphash[MAP_HASH(local_State, c1)];
2096 if (mp == NULL)
2097 {
2098 mp = mp2;
2099 mp2 = NULL;
2100 }
2101#else
2102 mp = maphash[MAP_HASH(local_State, c1)];
2103#endif
2104 /*
2105 * Loop until a partly matching mapping is found or
2106 * all (local) mappings have been checked.
2107 * The longest full match is remembered in "mp_match".
2108 * A full match is only accepted if there is no partly
2109 * match, so "aa" and "aaa" can both be mapped.
2110 */
2111 mp_match = NULL;
2112 mp_match_len = 0;
2113 for ( ; mp != NULL;
2114#ifdef FEAT_LOCALMAP
2115 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2116#endif
2117 (mp = mp->m_next))
2118 {
2119 /*
2120 * Only consider an entry if the first character
2121 * matches and it is for the current state.
2122 * Skip ":lmap" mappings if keys were mapped.
2123 */
2124 if (mp->m_keys[0] == c1
2125 && (mp->m_mode & local_State)
2126 && ((mp->m_mode & LANGMAP) == 0
2127 || typebuf.tb_maplen == 0))
2128 {
2129#ifdef FEAT_LANGMAP
2130 int nomap = nolmaplen;
2131 int c2;
2132#endif
2133 /* find the match length of this mapping */
2134 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2135 {
2136#ifdef FEAT_LANGMAP
2137 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2138 if (nomap > 0)
2139 --nomap;
2140 else if (c2 == K_SPECIAL)
2141 nomap = 2;
2142 else
2143 LANGMAP_ADJUST(c2, TRUE);
2144 if (mp->m_keys[mlen] != c2)
2145#else
2146 if (mp->m_keys[mlen] !=
2147 typebuf.tb_buf[typebuf.tb_off + mlen])
2148#endif
2149 break;
2150 }
2151
2152#ifdef FEAT_MBYTE
2153 /* Don't allow mapping the first byte(s) of a
2154 * multi-byte char. Happens when mapping
2155 * <M-a> and then changing 'encoding'. */
2156 if (has_mbyte && MB_BYTE2LEN(c1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002157 > (*mb_ptr2len)(mp->m_keys))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 mlen = 0;
2159#endif
2160 /*
2161 * Check an entry whether it matches.
2162 * - Full match: mlen == keylen
2163 * - Partly match: mlen == typebuf.tb_len
2164 */
2165 keylen = mp->m_keylen;
2166 if (mlen == keylen
2167 || (mlen == typebuf.tb_len
2168 && typebuf.tb_len < keylen))
2169 {
2170 /*
2171 * If only script-local mappings are
2172 * allowed, check if the mapping starts
2173 * with K_SNR.
2174 */
2175 s = typebuf.tb_noremap + typebuf.tb_off;
2176 if (*s == RM_SCRIPT
2177 && (mp->m_keys[0] != K_SPECIAL
2178 || mp->m_keys[1] != KS_EXTRA
2179 || mp->m_keys[2]
2180 != (int)KE_SNR))
2181 continue;
2182 /*
2183 * If one of the typed keys cannot be
2184 * remapped, skip the entry.
2185 */
2186 for (n = mlen; --n >= 0; )
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002187 if (*s++ & (RM_NONE|RM_ABBR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 break;
2189 if (n >= 0)
2190 continue;
2191
2192 if (keylen > typebuf.tb_len)
2193 {
2194 if (!timedout)
2195 {
2196 /* break at a partly match */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002197 keylen = KEYLEN_PART_MAP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 break;
2199 }
2200 }
2201 else if (keylen > mp_match_len)
2202 {
2203 /* found a longer match */
2204 mp_match = mp;
2205 mp_match_len = keylen;
2206 }
2207 }
2208 else
2209 /* No match; may have to check for
2210 * termcode at next character. */
2211 if (max_mlen < mlen)
2212 max_mlen = mlen;
2213 }
2214 }
2215
2216 /* If no partly match found, use the longest full
2217 * match. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002218 if (keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 mp = mp_match;
2221 keylen = mp_match_len;
2222 }
2223 }
2224
2225 /* Check for match with 'pastetoggle' */
2226 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2227 {
2228 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2229 ++mlen)
2230 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2231 + mlen])
2232 break;
2233 if (p_pt[mlen] == NUL) /* match */
2234 {
2235 /* write chars to script file(s) */
2236 if (mlen > typebuf.tb_maplen)
2237 gotchars(typebuf.tb_buf + typebuf.tb_off
2238 + typebuf.tb_maplen,
2239 mlen - typebuf.tb_maplen);
2240
2241 del_typebuf(mlen, 0); /* remove the chars */
2242 set_option_value((char_u *)"paste",
2243 (long)!p_paste, NULL, 0);
2244 if (!(State & INSERT))
2245 {
2246 msg_col = 0;
2247 msg_row = Rows - 1;
2248 msg_clr_eos(); /* clear ruler */
2249 }
2250 showmode();
2251 setcursor();
2252 continue;
2253 }
2254 /* Need more chars for partly match. */
2255 if (mlen == typebuf.tb_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002256 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 else if (max_mlen < mlen)
2258 /* no match, may have to check for termcode at
2259 * next character */
2260 max_mlen = mlen + 1;
2261 }
2262
2263 if ((mp == NULL || max_mlen >= mp_match_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002264 && keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002266 int save_keylen = keylen;
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 /*
2269 * When no matching mapping found or found a
2270 * non-matching mapping that matches at least what the
2271 * matching mapping matched:
2272 * Check if we have a terminal code, when:
2273 * mapping is allowed,
2274 * keys have not been mapped,
2275 * and not an ESC sequence, not in insert mode or
2276 * p_ek is on,
2277 * and when not timed out,
2278 */
2279 if ((no_mapping == 0 || allow_keys != 0)
2280 && (typebuf.tb_maplen == 0
2281 || (p_remap && typebuf.tb_noremap[
2282 typebuf.tb_off] == RM_YES))
2283 && !timedout)
2284 {
2285 keylen = check_termcode(max_mlen + 1, NULL, 0);
2286
Bram Moolenaarf2330482008-06-24 20:19:36 +00002287 /* If no termcode matched but 'pastetoggle'
2288 * matched partially it's like an incomplete key
2289 * sequence. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002290 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2291 keylen = KEYLEN_PART_KEY;
Bram Moolenaarf2330482008-06-24 20:19:36 +00002292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 /*
2294 * When getting a partial match, but the last
2295 * characters were not typed, don't wait for a
2296 * typed character to complete the termcode.
2297 * This helps a lot when a ":normal" command ends
2298 * in an ESC.
2299 */
2300 if (keylen < 0
2301 && typebuf.tb_len == typebuf.tb_maplen)
2302 keylen = 0;
2303 }
2304 else
2305 keylen = 0;
2306 if (keylen == 0) /* no matching terminal code */
2307 {
2308#ifdef AMIGA /* check for window bounds report */
2309 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2310 typebuf.tb_off] & 0xff) == CSI)
2311 {
2312 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2313 s < typebuf.tb_buf + typebuf.tb_off
2314 + typebuf.tb_len
2315 && (VIM_ISDIGIT(*s) || *s == ';'
2316 || *s == ' ');
2317 ++s)
2318 ;
2319 if (*s == 'r' || *s == '|') /* found one */
2320 {
2321 del_typebuf((int)(s + 1 -
2322 (typebuf.tb_buf + typebuf.tb_off)), 0);
2323 /* get size and redraw screen */
2324 shell_resized();
2325 continue;
2326 }
2327 if (*s == NUL) /* need more characters */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002328 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330 if (keylen >= 0)
2331#endif
2332 /* When there was a matching mapping and no
2333 * termcode could be replaced after another one,
Bram Moolenaarf2330482008-06-24 20:19:36 +00002334 * use that mapping (loop around). If there was
2335 * no mapping use the character from the
2336 * typeahead buffer right here. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (mp == NULL)
2338 {
2339/*
2340 * get a character: 2. from the typeahead buffer
2341 */
2342 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2343 if (advance) /* remove chars from tb_buf */
2344 {
2345 cmd_silent = (typebuf.tb_silent > 0);
2346 if (typebuf.tb_maplen > 0)
2347 KeyTyped = FALSE;
2348 else
2349 {
2350 KeyTyped = TRUE;
2351 /* write char to script file(s) */
2352 gotchars(typebuf.tb_buf
2353 + typebuf.tb_off, 1);
2354 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00002355 KeyNoremap = typebuf.tb_noremap[
2356 typebuf.tb_off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 del_typebuf(1, 0);
2358 }
2359 break; /* got character, break for loop */
2360 }
2361 }
2362 if (keylen > 0) /* full matching terminal code */
2363 {
2364#if defined(FEAT_GUI) && defined(FEAT_MENU)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002365 if (typebuf.tb_len >= 2
2366 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 && typebuf.tb_buf[typebuf.tb_off + 1]
2368 == KS_MENU)
2369 {
2370 /*
2371 * Using a menu may cause a break in undo!
2372 * It's like using gotchars(), but without
2373 * recording or writing to a script file.
2374 */
2375 may_sync_undo();
2376 del_typebuf(3, 0);
2377 idx = get_menu_index(current_menu, local_State);
2378 if (idx != MENU_INDEX_INVALID)
2379 {
2380# ifdef FEAT_VISUAL
2381 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002382 * In Select mode and a Visual mode menu
2383 * is used: Switch to Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 * temporarily. Append K_SELECT to switch
2385 * back to Select mode.
2386 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002387 if (VIsual_active && VIsual_select
2388 && (current_menu->modes & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
2390 VIsual_select = FALSE;
2391 (void)ins_typebuf(K_SELECT_STRING,
2392 REMAP_NONE, 0, TRUE, FALSE);
2393 }
2394# endif
2395 ins_typebuf(current_menu->strings[idx],
2396 current_menu->noremap[idx],
2397 0, TRUE,
2398 current_menu->silent[idx]);
2399 }
2400 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002401#endif /* FEAT_GUI && FEAT_MENU */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 continue; /* try mapping again */
2403 }
2404
2405 /* Partial match: get some more characters. When a
2406 * matching mapping was found use that one. */
2407 if (mp == NULL || keylen < 0)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002408 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 else
2410 keylen = mp_match_len;
2411 }
2412
2413 /* complete match */
2414 if (keylen >= 0 && keylen <= typebuf.tb_len)
2415 {
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002416#ifdef FEAT_EVAL
2417 int save_m_expr;
2418 int save_m_noremap;
2419 int save_m_silent;
2420 char_u *save_m_keys;
2421 char_u *save_m_str;
2422#else
2423# define save_m_noremap mp->m_noremap
2424# define save_m_silent mp->m_silent
2425#endif
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* write chars to script file(s) */
2428 if (keylen > typebuf.tb_maplen)
2429 gotchars(typebuf.tb_buf + typebuf.tb_off
2430 + typebuf.tb_maplen,
2431 keylen - typebuf.tb_maplen);
2432
2433 cmd_silent = (typebuf.tb_silent > 0);
2434 del_typebuf(keylen, 0); /* remove the mapped keys */
2435
2436 /*
2437 * Put the replacement string in front of mapstr.
2438 * The depth check catches ":map x y" and ":map y x".
2439 */
2440 if (++mapdepth >= p_mmd)
2441 {
2442 EMSG(_("E223: recursive mapping"));
2443 if (State & CMDLINE)
2444 redrawcmdline();
2445 else
2446 setcursor();
2447 flush_buffers(FALSE);
2448 mapdepth = 0; /* for next one */
2449 c = -1;
2450 break;
2451 }
2452
2453#ifdef FEAT_VISUAL
2454 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002455 * In Select mode and a Visual mode mapping is used:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 * Switch to Visual mode temporarily. Append K_SELECT
2457 * to switch back to Select mode.
2458 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002459 if (VIsual_active && VIsual_select
2460 && (mp->m_mode & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 VIsual_select = FALSE;
2463 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2464 0, TRUE, FALSE);
2465 }
2466#endif
2467
Bram Moolenaar4e427192006-03-10 21:34:27 +00002468#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002469 /* Copy the values from *mp that are used, because
2470 * evaluating the expression may invoke a function
2471 * that redefines the mapping, thereby making *mp
2472 * invalid. */
2473 save_m_expr = mp->m_expr;
2474 save_m_noremap = mp->m_noremap;
2475 save_m_silent = mp->m_silent;
2476 save_m_keys = NULL; /* only saved when needed */
2477 save_m_str = NULL; /* only saved when needed */
2478
Bram Moolenaar4e427192006-03-10 21:34:27 +00002479 /*
2480 * Handle ":map <expr>": evaluate the {rhs} as an
Bram Moolenaarb3479bd2011-10-12 22:02:14 +02002481 * expression. Also save and restore the command line
2482 * for "normal :".
Bram Moolenaar4e427192006-03-10 21:34:27 +00002483 */
2484 if (mp->m_expr)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002485 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002486 int save_vgetc_busy = vgetc_busy;
2487
Bram Moolenaarb3479bd2011-10-12 22:02:14 +02002488 vgetc_busy = 0;
2489 save_m_keys = vim_strsave(mp->m_keys);
2490 save_m_str = vim_strsave(mp->m_str);
2491 s = eval_map_expr(save_m_str, NUL);
2492 vgetc_busy = save_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002493 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00002494 else
2495#endif
2496 s = mp->m_str;
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 /*
2499 * Insert the 'to' part in the typebuf.tb_buf.
2500 * If 'from' field is the same as the start of the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002501 * 'to' field, don't remap the first character (but do
2502 * allow abbreviations).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * If m_noremap is set, don't remap the whole 'to'
2504 * part.
2505 */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002506 if (s == NULL)
2507 i = FAIL;
2508 else
2509 {
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002510 int noremap;
2511
2512 if (save_m_noremap != REMAP_YES)
2513 noremap = save_m_noremap;
2514 else if (
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002515#ifdef FEAT_EVAL
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002516 STRNCMP(s, save_m_keys != NULL
2517 ? save_m_keys : mp->m_keys,
2518 (size_t)keylen)
2519#else
2520 STRNCMP(s, mp->m_keys, (size_t)keylen)
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002521#endif
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002522 != 0)
2523 noremap = REMAP_YES;
2524 else
2525 noremap = REMAP_SKIP;
2526 i = ins_typebuf(s, noremap,
2527 0, TRUE, cmd_silent || save_m_silent);
Bram Moolenaar4e427192006-03-10 21:34:27 +00002528#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002529 if (save_m_expr)
Bram Moolenaar4e427192006-03-10 21:34:27 +00002530 vim_free(s);
2531#endif
2532 }
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002533#ifdef FEAT_EVAL
2534 vim_free(save_m_keys);
2535 vim_free(save_m_str);
2536#endif
Bram Moolenaar4e427192006-03-10 21:34:27 +00002537 if (i == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 c = -1;
2540 break;
2541 }
2542 continue;
2543 }
2544 }
2545
2546/*
2547 * get a character: 3. from the user - handle <Esc> in Insert mode
2548 */
2549 /*
2550 * special case: if we get an <ESC> in insert mode and there
2551 * are no more characters at once, we pretend to go out of
2552 * insert mode. This prevents the one second delay after
2553 * typing an <ESC>. If we get something after all, we may
2554 * have to redisplay the mode. That the cursor is in the wrong
2555 * place does not matter.
2556 */
2557 c = 0;
2558#ifdef FEAT_CMDL_INFO
2559 new_wcol = curwin->w_wcol;
2560 new_wrow = curwin->w_wrow;
2561#endif
2562 if ( advance
2563 && typebuf.tb_len == 1
2564 && typebuf.tb_buf[typebuf.tb_off] == ESC
2565 && !no_mapping
2566#ifdef FEAT_EX_EXTRA
2567 && ex_normal_busy == 0
2568#endif
2569 && typebuf.tb_maplen == 0
2570 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002571 && (p_timeout
2572 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2574 + typebuf.tb_len, 3, 25L,
2575 typebuf.tb_change_cnt)) == 0)
2576 {
2577 colnr_T col = 0, vcol;
2578 char_u *ptr;
2579
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002580 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 unshowmode(TRUE);
2583 mode_deleted = TRUE;
2584 }
2585#ifdef FEAT_GUI
2586 /* may show different cursor shape */
2587 if (gui.in_use)
2588 {
2589 int save_State;
2590
2591 save_State = State;
2592 State = NORMAL;
2593 gui_update_cursor(TRUE, FALSE);
2594 State = save_State;
2595 shape_changed = TRUE;
2596 }
2597#endif
2598 validate_cursor();
2599 old_wcol = curwin->w_wcol;
2600 old_wrow = curwin->w_wrow;
2601
2602 /* move cursor left, if possible */
2603 if (curwin->w_cursor.col != 0)
2604 {
2605 if (curwin->w_wcol > 0)
2606 {
2607 if (did_ai)
2608 {
2609 /*
2610 * We are expecting to truncate the trailing
2611 * white-space, so find the last non-white
2612 * character -- webb
2613 */
2614 col = vcol = curwin->w_wcol = 0;
2615 ptr = ml_get_curline();
2616 while (col < curwin->w_cursor.col)
2617 {
2618 if (!vim_iswhite(ptr[col]))
2619 curwin->w_wcol = vcol;
2620 vcol += lbr_chartabsize(ptr + col,
2621 (colnr_T)vcol);
2622#ifdef FEAT_MBYTE
2623 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002624 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else
2626#endif
2627 ++col;
2628 }
2629 curwin->w_wrow = curwin->w_cline_row
2630 + curwin->w_wcol / W_WIDTH(curwin);
2631 curwin->w_wcol %= W_WIDTH(curwin);
2632 curwin->w_wcol += curwin_col_off();
2633#ifdef FEAT_MBYTE
2634 col = 0; /* no correction needed */
2635#endif
2636 }
2637 else
2638 {
2639 --curwin->w_wcol;
2640#ifdef FEAT_MBYTE
2641 col = curwin->w_cursor.col - 1;
2642#endif
2643 }
2644 }
2645 else if (curwin->w_p_wrap && curwin->w_wrow)
2646 {
2647 --curwin->w_wrow;
2648 curwin->w_wcol = W_WIDTH(curwin) - 1;
2649#ifdef FEAT_MBYTE
2650 col = curwin->w_cursor.col - 1;
2651#endif
2652 }
2653#ifdef FEAT_MBYTE
2654 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2655 {
2656 /* Correct when the cursor is on the right halve
2657 * of a double-wide character. */
2658 ptr = ml_get_curline();
2659 col -= (*mb_head_off)(ptr, ptr + col);
2660 if ((*mb_ptr2cells)(ptr + col) > 1)
2661 --curwin->w_wcol;
2662 }
2663#endif
2664 }
2665 setcursor();
2666 out_flush();
2667#ifdef FEAT_CMDL_INFO
2668 new_wcol = curwin->w_wcol;
2669 new_wrow = curwin->w_wrow;
2670#endif
2671 curwin->w_wcol = old_wcol;
2672 curwin->w_wrow = old_wrow;
2673 }
2674 if (c < 0)
2675 continue; /* end of input script reached */
2676 typebuf.tb_len += c;
2677
2678 /* buffer full, don't map */
2679 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2680 {
2681 timedout = TRUE;
2682 continue;
2683 }
2684
2685#ifdef FEAT_EX_EXTRA
2686 if (ex_normal_busy > 0)
2687 {
2688# ifdef FEAT_CMDWIN
2689 static int tc = 0;
2690# endif
2691
2692 /* No typeahead left and inside ":normal". Must return
2693 * something to avoid getting stuck. When an incomplete
2694 * mapping is present, behave like it timed out. */
2695 if (typebuf.tb_len > 0)
2696 {
2697 timedout = TRUE;
2698 continue;
2699 }
2700 /* When 'insertmode' is set, ESC just beeps in Insert
2701 * mode. Use CTRL-L to make edit() return.
2702 * For the command line only CTRL-C always breaks it.
2703 * For the cmdline window: Alternate between ESC and
2704 * CTRL-C: ESC for most situations and CTRL-C to close the
2705 * cmdline window. */
2706 if (p_im && (State & INSERT))
2707 c = Ctrl_L;
2708 else if ((State & CMDLINE)
2709# ifdef FEAT_CMDWIN
2710 || (cmdwin_type > 0 && tc == ESC)
2711# endif
2712 )
2713 c = Ctrl_C;
2714 else
2715 c = ESC;
2716# ifdef FEAT_CMDWIN
2717 tc = c;
2718# endif
2719 break;
2720 }
2721#endif
2722
2723/*
2724 * get a character: 3. from the user - update display
2725 */
2726 /* In insert mode a screen update is skipped when characters
2727 * are still available. But when those available characters
2728 * are part of a mapping, and we are going to do a blocking
2729 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01002730 * changed text so far. Also for when 'lazyredraw' is set and
2731 * redrawing was postponed because there was something in the
2732 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01002733 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
2734 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
2736 update_screen(0);
2737 setcursor(); /* put cursor back where it belongs */
2738 }
2739
2740 /*
2741 * If we have a partial match (and are going to wait for more
2742 * input from the user), show the partially matched characters
2743 * to the user with showcmd.
2744 */
2745#ifdef FEAT_CMDL_INFO
2746 i = 0;
2747#endif
2748 c1 = 0;
2749 if (typebuf.tb_len > 0 && advance && !exmode_active)
2750 {
2751 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2752 && State != HITRETURN)
2753 {
2754 /* this looks nice when typing a dead character map */
2755 if (State & INSERT
2756 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2757 + typebuf.tb_len - 1) == 1)
2758 {
2759 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2760 + typebuf.tb_len - 1], FALSE);
2761 setcursor(); /* put cursor back where it belongs */
2762 c1 = 1;
2763 }
2764#ifdef FEAT_CMDL_INFO
2765 /* need to use the col and row from above here */
2766 old_wcol = curwin->w_wcol;
2767 old_wrow = curwin->w_wrow;
2768 curwin->w_wcol = new_wcol;
2769 curwin->w_wrow = new_wrow;
2770 push_showcmd();
2771 if (typebuf.tb_len > SHOWCMD_COLS)
2772 i = typebuf.tb_len - SHOWCMD_COLS;
2773 while (i < typebuf.tb_len)
2774 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2775 + i++]);
2776 curwin->w_wcol = old_wcol;
2777 curwin->w_wrow = old_wrow;
2778#endif
2779 }
2780
2781 /* this looks nice when typing a dead character map */
2782 if ((State & CMDLINE)
2783#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2784 && cmdline_star == 0
2785#endif
2786 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2787 + typebuf.tb_len - 1) == 1)
2788 {
2789 putcmdline(typebuf.tb_buf[typebuf.tb_off
2790 + typebuf.tb_len - 1], FALSE);
2791 c1 = 1;
2792 }
2793 }
2794
2795/*
2796 * get a character: 3. from the user - get it
2797 */
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002798 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2800 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2801 !advance
2802 ? 0
2803 : ((typebuf.tb_len == 0
2804 || !(p_timeout || (p_ttimeout
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002805 && keylen == KEYLEN_PART_KEY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 ? -1L
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002807 : ((keylen == KEYLEN_PART_KEY && p_ttm >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 ? p_ttm
2809 : p_tm)), typebuf.tb_change_cnt);
2810
2811#ifdef FEAT_CMDL_INFO
2812 if (i != 0)
2813 pop_showcmd();
2814#endif
2815 if (c1 == 1)
2816 {
2817 if (State & INSERT)
2818 edit_unputchar();
2819 if (State & CMDLINE)
2820 unputcmdline();
2821 setcursor(); /* put cursor back where it belongs */
2822 }
2823
2824 if (c < 0)
2825 continue; /* end of input script reached */
2826 if (c == NUL) /* no character available */
2827 {
2828 if (!advance)
2829 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002830 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 timedout = TRUE;
2833 continue;
2834 }
2835 }
2836 else
2837 { /* allow mapping for just typed characters */
2838 while (typebuf.tb_buf[typebuf.tb_off
2839 + typebuf.tb_len] != NUL)
2840 typebuf.tb_noremap[typebuf.tb_off
2841 + typebuf.tb_len++] = RM_YES;
2842#ifdef USE_IM_CONTROL
2843 /* Get IM status right after getting keys, not after the
2844 * timeout for a mapping (focus may be lost by then). */
2845 vgetc_im_active = im_get_status();
2846#endif
2847 }
2848 } /* for (;;) */
2849 } /* if (!character from stuffbuf) */
2850
2851 /* if advance is FALSE don't loop on NULs */
2852 } while (c < 0 || (advance && c == NUL));
2853
2854 /*
2855 * The "INSERT" message is taken care of here:
2856 * if we return an ESC to exit insert mode, the message is deleted
2857 * if we don't return an ESC but deleted the message before, redisplay it
2858 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002859 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002861 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
2863 if (typebuf.tb_len && !KeyTyped)
2864 redraw_cmdline = TRUE; /* delete mode later */
2865 else
2866 unshowmode(FALSE);
2867 }
2868 else if (c != ESC && mode_deleted)
2869 {
2870 if (typebuf.tb_len && !KeyTyped)
2871 redraw_cmdline = TRUE; /* show mode later */
2872 else
2873 showmode();
2874 }
2875 }
2876#ifdef FEAT_GUI
2877 /* may unshow different cursor shape */
2878 if (gui.in_use && shape_changed)
2879 gui_update_cursor(TRUE, FALSE);
2880#endif
2881
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002882 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 return c;
2885}
2886
2887/*
2888 * inchar() - get one character from
2889 * 1. a scriptfile
2890 * 2. the keyboard
2891 *
2892 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2893 * NUL terminated (buffer length must be 'maxlen' + 1).
2894 * Minimum for "maxlen" is 3!!!!
2895 *
2896 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2897 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2898 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2899 * otherwise.
2900 *
2901 * If we got an interrupt all input is read until none is available.
2902 *
2903 * If wait_time == 0 there is no waiting for the char.
2904 * If wait_time == n we wait for n msec for a character to arrive.
2905 * If wait_time == -1 we wait forever for a character to arrive.
2906 *
2907 * Return the number of obtained characters.
2908 * Return -1 when end of input script reached.
2909 */
2910 int
2911inchar(buf, maxlen, wait_time, tb_change_cnt)
2912 char_u *buf;
2913 int maxlen;
2914 long wait_time; /* milli seconds */
2915 int tb_change_cnt;
2916{
2917 int len = 0; /* init for GCC */
2918 int retesc = FALSE; /* return ESC with gotint */
2919 int script_char;
2920
2921 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2922 {
2923 cursor_on();
2924 out_flush();
2925#ifdef FEAT_GUI
2926 if (gui.in_use)
2927 {
2928 gui_update_cursor(FALSE, FALSE);
2929# ifdef FEAT_MOUSESHAPE
2930 if (postponed_mouseshape)
2931 update_mouseshape(-1);
2932# endif
2933 }
2934#endif
2935 }
2936
2937 /*
2938 * Don't reset these when at the hit-return prompt, otherwise a endless
2939 * recursive loop may result (write error in swapfile, hit-return, timeout
2940 * on char wait, flush swapfile, write error....).
2941 */
2942 if (State != HITRETURN)
2943 {
2944 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2945 did_swapwrite_msg = FALSE; /* display swap file write error again */
2946 }
2947 undo_off = FALSE; /* restart undo now */
2948
2949 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002950 * Get a character from a script file if there is one.
2951 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 */
2953 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002954 while (scriptin[curscript] != NULL && script_char < 0
2955#ifdef FEAT_EVAL
2956 && !ignore_script
2957#endif
2958 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002960
2961#if defined(FEAT_NETBEANS_INTG)
2962 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002963 netbeans_parse_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00002964#endif
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2967 {
2968 /* Reached EOF.
2969 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2970 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2971 closescript();
2972 /*
2973 * When reading script file is interrupted, return an ESC to get
2974 * back to normal mode.
2975 * Otherwise return -1, because typebuf.tb_buf[] has changed.
2976 */
2977 if (got_int)
2978 retesc = TRUE;
2979 else
2980 return -1;
2981 }
2982 else
2983 {
2984 buf[0] = script_char;
2985 len = 1;
2986 }
2987 }
2988
2989 if (script_char < 0) /* did not get a character from script */
2990 {
2991 /*
2992 * If we got an interrupt, skip all previously typed characters and
2993 * return TRUE if quit reading script file.
2994 * Stop reading typeahead when a single CTRL-C was read,
2995 * fill_input_buf() returns this when not able to read from stdin.
2996 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
2997 * and buf may be pointing inside typebuf.tb_buf[].
2998 */
2999 if (got_int)
3000 {
3001#define DUM_LEN MAXMAPLEN * 3 + 3
3002 char_u dum[DUM_LEN + 1];
3003
3004 for (;;)
3005 {
3006 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3007 if (len == 0 || (len == 1 && dum[0] == 3))
3008 break;
3009 }
3010 return retesc;
3011 }
3012
3013 /*
3014 * Always flush the output characters when getting input characters
3015 * from the user.
3016 */
3017 out_flush();
3018
3019 /*
3020 * Fill up to a third of the buffer, because each character may be
3021 * tripled below.
3022 */
3023 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3024 }
3025
3026 if (typebuf_changed(tb_change_cnt))
3027 return 0;
3028
3029 return fix_input_buffer(buf, len, script_char >= 0);
3030}
3031
3032/*
3033 * Fix typed characters for use by vgetc() and check_termcode().
3034 * buf[] must have room to triple the number of bytes!
3035 * Returns the new length.
3036 */
3037 int
3038fix_input_buffer(buf, len, script)
3039 char_u *buf;
3040 int len;
3041 int script; /* TRUE when reading from a script */
3042{
3043 int i;
3044 char_u *p = buf;
3045
3046 /*
3047 * Two characters are special: NUL and K_SPECIAL.
3048 * When compiled With the GUI CSI is also special.
3049 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3050 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3051 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
3052 * Don't replace K_SPECIAL when reading a script file.
3053 */
3054 for (i = len; --i >= 0; ++p)
3055 {
3056#ifdef FEAT_GUI
3057 /* When the GUI is used any character can come after a CSI, don't
3058 * escape it. */
3059 if (gui.in_use && p[0] == CSI && i >= 2)
3060 {
3061 p += 2;
3062 i -= 2;
3063 }
3064 /* When the GUI is not used CSI needs to be escaped. */
3065 else if (!gui.in_use && p[0] == CSI)
3066 {
3067 mch_memmove(p + 3, p + 1, (size_t)i);
3068 *p++ = K_SPECIAL;
3069 *p++ = KS_EXTRA;
3070 *p = (int)KE_CSI;
3071 len += 2;
3072 }
3073 else
3074#endif
3075 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003076#ifdef FEAT_AUTOCMD
3077 /* timeout may generate K_CURSORHOLD */
3078 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
3079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#if defined(WIN3264) && !defined(FEAT_GUI)
3081 /* Win32 console passes modifiers */
3082 && (i < 2 || p[1] != KS_MODIFIER)
3083#endif
3084 ))
3085 {
3086 mch_memmove(p + 3, p + 1, (size_t)i);
3087 p[2] = K_THIRD(p[0]);
3088 p[1] = K_SECOND(p[0]);
3089 p[0] = K_SPECIAL;
3090 p += 2;
3091 len += 2;
3092 }
3093 }
3094 *p = NUL; /* add trailing NUL */
3095 return len;
3096}
3097
3098#if defined(USE_INPUT_BUF) || defined(PROTO)
3099/*
3100 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3101 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003102 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003103 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
3105 int
3106input_available()
3107{
3108 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003109# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3110 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111# endif
3112 );
3113}
3114#endif
3115
3116/*
3117 * map[!] : show all key mappings
3118 * map[!] {lhs} : show key mapping for {lhs}
3119 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3120 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3121 * unmap[!] {lhs} : remove key mapping for {lhs}
3122 * abbr : show all abbreviations
3123 * abbr {lhs} : show abbreviations for {lhs}
3124 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3125 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3126 * unabbr {lhs} : remove abbreviation for {lhs}
3127 *
3128 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3129 *
3130 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3131 * it will be modified.
3132 *
Bram Moolenaar371d5402006-03-20 21:47:49 +00003133 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 * for :map! mode is INSERT + CMDLINE
3135 * for :cmap mode is CMDLINE
3136 * for :imap mode is INSERT
3137 * for :lmap mode is LANGMAP
3138 * for :nmap mode is NORMAL
Bram Moolenaar371d5402006-03-20 21:47:49 +00003139 * for :vmap mode is VISUAL + SELECTMODE
3140 * for :xmap mode is VISUAL
3141 * for :smap mode is SELECTMODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * for :omap mode is OP_PENDING
3143 *
3144 * for :abbr mode is INSERT + CMDLINE
3145 * for :iabbr mode is INSERT
3146 * for :cabbr mode is CMDLINE
3147 *
3148 * Return 0 for success
3149 * 1 for invalid arguments
3150 * 2 for no match
3151 * 4 for out of mem
3152 * 5 for entry not unique
3153 */
3154 int
3155do_map(maptype, arg, mode, abbrev)
3156 int maptype;
3157 char_u *arg;
3158 int mode;
3159 int abbrev; /* not a mapping but an abbreviation */
3160{
3161 char_u *keys;
3162 mapblock_T *mp, **mpp;
3163 char_u *rhs;
3164 char_u *p;
3165 int n;
3166 int len = 0; /* init for GCC */
3167 char_u *newstr;
3168 int hasarg;
3169 int haskey;
3170 int did_it = FALSE;
3171#ifdef FEAT_LOCALMAP
3172 int did_local = FALSE;
3173#endif
3174 int round;
3175 char_u *keys_buf = NULL;
3176 char_u *arg_buf = NULL;
3177 int retval = 0;
3178 int do_backslash;
3179 int hash;
3180 int new_hash;
3181 mapblock_T **abbr_table;
3182 mapblock_T **map_table;
3183 int unique = FALSE;
3184 int silent = FALSE;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003185 int special = FALSE;
Bram Moolenaar4e427192006-03-10 21:34:27 +00003186#ifdef FEAT_EVAL
3187 int expr = FALSE;
3188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 int noremap;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003190 char_u *orig_rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 keys = arg;
3193 map_table = maphash;
3194 abbr_table = &first_abbr;
3195
3196 /* For ":noremap" don't remap, otherwise do remap. */
3197 if (maptype == 2)
3198 noremap = REMAP_NONE;
3199 else
3200 noremap = REMAP_YES;
3201
Bram Moolenaar4e427192006-03-10 21:34:27 +00003202 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 for (;;)
3204 {
3205#ifdef FEAT_LOCALMAP
3206 /*
3207 * Check for "<buffer>": mapping local to buffer.
3208 */
3209 if (STRNCMP(keys, "<buffer>", 8) == 0)
3210 {
3211 keys = skipwhite(keys + 8);
3212 map_table = curbuf->b_maphash;
3213 abbr_table = &curbuf->b_first_abbr;
3214 continue;
3215 }
3216#endif
3217
3218 /*
3219 * Check for "<silent>": don't echo commands.
3220 */
3221 if (STRNCMP(keys, "<silent>", 8) == 0)
3222 {
3223 keys = skipwhite(keys + 8);
3224 silent = TRUE;
3225 continue;
3226 }
3227
Bram Moolenaar9c102382006-05-03 21:26:49 +00003228 /*
3229 * Check for "<special>": accept special keys in <>
3230 */
3231 if (STRNCMP(keys, "<special>", 9) == 0)
3232 {
3233 keys = skipwhite(keys + 9);
3234 special = TRUE;
3235 continue;
3236 }
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#ifdef FEAT_EVAL
3239 /*
3240 * Check for "<script>": remap script-local mappings only
3241 */
3242 if (STRNCMP(keys, "<script>", 8) == 0)
3243 {
3244 keys = skipwhite(keys + 8);
3245 noremap = REMAP_SCRIPT;
3246 continue;
3247 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00003248
3249 /*
3250 * Check for "<expr>": {rhs} is an expression.
3251 */
3252 if (STRNCMP(keys, "<expr>", 6) == 0)
3253 {
3254 keys = skipwhite(keys + 6);
3255 expr = TRUE;
3256 continue;
3257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif
3259 /*
3260 * Check for "<unique>": don't overwrite an existing mapping.
3261 */
3262 if (STRNCMP(keys, "<unique>", 8) == 0)
3263 {
3264 keys = skipwhite(keys + 8);
3265 unique = TRUE;
3266 continue;
3267 }
3268 break;
3269 }
3270
3271 validate_maphash();
3272
3273 /*
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003274 * Find end of keys and skip CTRL-Vs (and backslashes) in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003276 * with :unmap white space is included in the keys, no argument possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 p = keys;
3279 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3280 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3281 {
3282 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3283 p[1] != NUL)
3284 ++p; /* skip CTRL-V or backslash */
3285 ++p;
3286 }
3287 if (*p != NUL)
3288 *p++ = NUL;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 p = skipwhite(p);
3291 rhs = p;
3292 hasarg = (*rhs != NUL);
3293 haskey = (*keys != NUL);
3294
3295 /* check for :unmap without argument */
3296 if (maptype == 1 && !haskey)
3297 {
3298 retval = 1;
3299 goto theend;
3300 }
3301
3302 /*
3303 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3304 * with the appropriate two bytes. If it is a shifted special key, unshift
3305 * it too, giving another two bytes.
3306 * replace_termcodes() may move the result to allocated memory, which
3307 * needs to be freed later (*keys_buf and *arg_buf).
3308 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3309 */
3310 if (haskey)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003311 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
Bram Moolenaarb3ae56c2010-10-27 17:39:05 +02003312 orig_rhs = rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 if (hasarg)
3314 {
3315 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3316 rhs = (char_u *)"";
3317 else
Bram Moolenaar9c102382006-05-03 21:26:49 +00003318 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320
3321#ifdef FEAT_FKMAP
3322 /*
Bram Moolenaarbd743252010-10-20 21:23:33 +02003323 * When in right-to-left mode and alternate keymap option set,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * reverse the character flow in the rhs in Farsi.
3325 */
3326 if (p_altkeymap && curwin->w_p_rl)
3327 lrswap(rhs);
3328#endif
3329
3330 /*
3331 * check arguments and translate function keys
3332 */
3333 if (haskey)
3334 {
3335 len = (int)STRLEN(keys);
3336 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3337 {
3338 retval = 1;
3339 goto theend;
3340 }
3341
3342 if (abbrev && maptype != 1)
3343 {
3344 /*
3345 * If an abbreviation ends in a keyword character, the
3346 * rest must be all keyword-char or all non-keyword-char.
3347 * Otherwise we won't be able to find the start of it in a
3348 * vi-compatible way.
3349 */
3350#ifdef FEAT_MBYTE
3351 if (has_mbyte)
3352 {
3353 int first, last;
3354 int same = -1;
3355
3356 first = vim_iswordp(keys);
3357 last = first;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003358 p = keys + (*mb_ptr2len)(keys);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 n = 1;
3360 while (p < keys + len)
3361 {
3362 ++n; /* nr of (multi-byte) chars */
3363 last = vim_iswordp(p); /* type of last char */
3364 if (same == -1 && last != first)
3365 same = n - 1; /* count of same char type */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003366 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 if (last && n > 2 && same >= 0 && same < n - 1)
3369 {
3370 retval = 1;
3371 goto theend;
3372 }
3373 }
3374 else
3375#endif
3376 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3377 for (n = 0; n < len - 2; ++n)
3378 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3379 {
3380 retval = 1;
3381 goto theend;
3382 }
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00003383 /* An abbreviation cannot contain white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 for (n = 0; n < len; ++n)
3385 if (vim_iswhite(keys[n]))
3386 {
3387 retval = 1;
3388 goto theend;
3389 }
3390 }
3391 }
3392
3393 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3394 no_abbr = FALSE; /* reset flag that indicates there are
3395 no abbreviations */
3396
3397 if (!haskey || (maptype != 1 && !hasarg))
3398 msg_start();
3399
3400#ifdef FEAT_LOCALMAP
3401 /*
3402 * Check if a new local mapping wasn't already defined globally.
3403 */
3404 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3405 {
3406 /* need to loop over all global hash lists */
3407 for (hash = 0; hash < 256 && !got_int; ++hash)
3408 {
3409 if (abbrev)
3410 {
3411 if (hash != 0) /* there is only one abbreviation list */
3412 break;
3413 mp = first_abbr;
3414 }
3415 else
3416 mp = maphash[hash];
3417 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3418 {
3419 /* check entries with the same mode */
3420 if ((mp->m_mode & mode) != 0
3421 && mp->m_keylen == len
3422 && unique
3423 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3424 {
3425 if (abbrev)
3426 EMSG2(_("E224: global abbreviation already exists for %s"),
3427 mp->m_keys);
3428 else
3429 EMSG2(_("E225: global mapping already exists for %s"),
3430 mp->m_keys);
3431 retval = 5;
3432 goto theend;
3433 }
3434 }
3435 }
3436 }
3437
3438 /*
3439 * When listing global mappings, also list buffer-local ones here.
3440 */
3441 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3442 {
3443 /* need to loop over all global hash lists */
3444 for (hash = 0; hash < 256 && !got_int; ++hash)
3445 {
3446 if (abbrev)
3447 {
3448 if (hash != 0) /* there is only one abbreviation list */
3449 break;
3450 mp = curbuf->b_first_abbr;
3451 }
3452 else
3453 mp = curbuf->b_maphash[hash];
3454 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3455 {
3456 /* check entries with the same mode */
3457 if ((mp->m_mode & mode) != 0)
3458 {
3459 if (!haskey) /* show all entries */
3460 {
3461 showmap(mp, TRUE);
3462 did_local = TRUE;
3463 }
3464 else
3465 {
3466 n = mp->m_keylen;
3467 if (STRNCMP(mp->m_keys, keys,
3468 (size_t)(n < len ? n : len)) == 0)
3469 {
3470 showmap(mp, TRUE);
3471 did_local = TRUE;
3472 }
3473 }
3474 }
3475 }
3476 }
3477 }
3478#endif
3479
3480 /*
3481 * Find an entry in the maphash[] list that matches.
3482 * For :unmap we may loop two times: once to try to unmap an entry with a
3483 * matching 'from' part, a second time, if the first fails, to unmap an
3484 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3485 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3486 * "bar" because of the abbreviation.
3487 */
3488 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3489 && !did_it && !got_int; ++round)
3490 {
3491 /* need to loop over all hash lists */
3492 for (hash = 0; hash < 256 && !got_int; ++hash)
3493 {
3494 if (abbrev)
3495 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003496 if (hash > 0) /* there is only one abbreviation list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 break;
3498 mpp = abbr_table;
3499 }
3500 else
3501 mpp = &(map_table[hash]);
3502 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3503 {
3504
3505 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3506 {
3507 mpp = &(mp->m_next);
3508 continue;
3509 }
3510 if (!haskey) /* show all entries */
3511 {
3512 showmap(mp, map_table != maphash);
3513 did_it = TRUE;
3514 }
3515 else /* do we have a match? */
3516 {
3517 if (round) /* second round: Try unmap "rhs" string */
3518 {
3519 n = (int)STRLEN(mp->m_str);
3520 p = mp->m_str;
3521 }
3522 else
3523 {
3524 n = mp->m_keylen;
3525 p = mp->m_keys;
3526 }
3527 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3528 {
3529 if (maptype == 1) /* delete entry */
3530 {
3531 /* Only accept a full match. For abbreviations we
3532 * ignore trailing space when matching with the
3533 * "lhs", since an abbreviation can't have
3534 * trailing space. */
3535 if (n != len && (!abbrev || round || n > len
3536 || *skipwhite(keys + n) != NUL))
3537 {
3538 mpp = &(mp->m_next);
3539 continue;
3540 }
3541 /*
3542 * We reset the indicated mode bits. If nothing is
3543 * left the entry is deleted below.
3544 */
3545 mp->m_mode &= ~mode;
3546 did_it = TRUE; /* remember we did something */
3547 }
3548 else if (!hasarg) /* show matching entry */
3549 {
3550 showmap(mp, map_table != maphash);
3551 did_it = TRUE;
3552 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00003553 else if (n != len) /* new entry is ambiguous */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 mpp = &(mp->m_next);
3556 continue;
3557 }
3558 else if (unique)
3559 {
3560 if (abbrev)
3561 EMSG2(_("E226: abbreviation already exists for %s"),
3562 p);
3563 else
3564 EMSG2(_("E227: mapping already exists for %s"), p);
3565 retval = 5;
3566 goto theend;
3567 }
3568 else /* new rhs for existing entry */
3569 {
3570 mp->m_mode &= ~mode; /* remove mode bits */
3571 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3572 {
3573 newstr = vim_strsave(rhs);
3574 if (newstr == NULL)
3575 {
3576 retval = 4; /* no mem */
3577 goto theend;
3578 }
3579 vim_free(mp->m_str);
3580 mp->m_str = newstr;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003581 vim_free(mp->m_orig_str);
3582 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 mp->m_noremap = noremap;
3584 mp->m_silent = silent;
3585 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003586#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003587 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003588 mp->m_script_ID = current_SID;
3589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 did_it = TRUE;
3591 }
3592 }
3593 if (mp->m_mode == 0) /* entry can be deleted */
3594 {
3595 map_free(mpp);
3596 continue; /* continue with *mpp */
3597 }
3598
3599 /*
3600 * May need to put this entry into another hash list.
3601 */
3602 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3603 if (!abbrev && new_hash != hash)
3604 {
3605 *mpp = mp->m_next;
3606 mp->m_next = map_table[new_hash];
3607 map_table[new_hash] = mp;
3608
3609 continue; /* continue with *mpp */
3610 }
3611 }
3612 }
3613 mpp = &(mp->m_next);
3614 }
3615 }
3616 }
3617
3618 if (maptype == 1) /* delete entry */
3619 {
3620 if (!did_it)
3621 retval = 2; /* no match */
3622 goto theend;
3623 }
3624
3625 if (!haskey || !hasarg) /* print entries */
3626 {
3627 if (!did_it
3628#ifdef FEAT_LOCALMAP
3629 && !did_local
3630#endif
3631 )
3632 {
3633 if (abbrev)
3634 MSG(_("No abbreviation found"));
3635 else
3636 MSG(_("No mapping found"));
3637 }
3638 goto theend; /* listing finished */
3639 }
3640
3641 if (did_it) /* have added the new entry already */
3642 goto theend;
3643
3644 /*
3645 * Get here when adding a new entry to the maphash[] list or abbrlist.
3646 */
3647 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3648 if (mp == NULL)
3649 {
3650 retval = 4; /* no mem */
3651 goto theend;
3652 }
3653
3654 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3655 if (*keys == Ctrl_C)
3656 mapped_ctrl_c = TRUE;
3657
3658 mp->m_keys = vim_strsave(keys);
3659 mp->m_str = vim_strsave(rhs);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003660 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (mp->m_keys == NULL || mp->m_str == NULL)
3662 {
3663 vim_free(mp->m_keys);
3664 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003665 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 vim_free(mp);
3667 retval = 4; /* no mem */
3668 goto theend;
3669 }
3670 mp->m_keylen = (int)STRLEN(mp->m_keys);
3671 mp->m_noremap = noremap;
3672 mp->m_silent = silent;
3673 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003674#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003675 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003676 mp->m_script_ID = current_SID;
3677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 /* add the new entry in front of the abbrlist or maphash[] list */
3680 if (abbrev)
3681 {
3682 mp->m_next = *abbr_table;
3683 *abbr_table = mp;
3684 }
3685 else
3686 {
3687 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3688 mp->m_next = map_table[n];
3689 map_table[n] = mp;
3690 }
3691
3692theend:
3693 vim_free(keys_buf);
3694 vim_free(arg_buf);
3695 return retval;
3696}
3697
3698/*
3699 * Delete one entry from the abbrlist or maphash[].
3700 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3701 */
3702 static void
3703map_free(mpp)
3704 mapblock_T **mpp;
3705{
3706 mapblock_T *mp;
3707
3708 mp = *mpp;
3709 vim_free(mp->m_keys);
3710 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003711 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 *mpp = mp->m_next;
3713 vim_free(mp);
3714}
3715
3716/*
3717 * Initialize maphash[] for first use.
3718 */
3719 static void
3720validate_maphash()
3721{
3722 if (!maphash_valid)
3723 {
3724 vim_memset(maphash, 0, sizeof(maphash));
3725 maphash_valid = TRUE;
3726 }
3727}
3728
3729/*
3730 * Get the mapping mode from the command name.
3731 */
3732 int
3733get_map_mode(cmdp, forceit)
3734 char_u **cmdp;
3735 int forceit;
3736{
3737 char_u *p;
3738 int modec;
3739 int mode;
3740
3741 p = *cmdp;
3742 modec = *p++;
3743 if (modec == 'i')
3744 mode = INSERT; /* :imap */
3745 else if (modec == 'l')
3746 mode = LANGMAP; /* :lmap */
3747 else if (modec == 'c')
3748 mode = CMDLINE; /* :cmap */
3749 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3750 mode = NORMAL; /* :nmap */
3751 else if (modec == 'v')
Bram Moolenaar371d5402006-03-20 21:47:49 +00003752 mode = VISUAL + SELECTMODE; /* :vmap */
3753 else if (modec == 'x')
3754 mode = VISUAL; /* :xmap */
3755 else if (modec == 's')
3756 mode = SELECTMODE; /* :smap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 else if (modec == 'o')
3758 mode = OP_PENDING; /* :omap */
3759 else
3760 {
3761 --p;
3762 if (forceit)
3763 mode = INSERT + CMDLINE; /* :map ! */
3764 else
Bram Moolenaar371d5402006-03-20 21:47:49 +00003765 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767
3768 *cmdp = p;
3769 return mode;
3770}
3771
3772/*
3773 * Clear all mappings or abbreviations.
3774 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 void
3777map_clear(cmdp, arg, forceit, abbr)
3778 char_u *cmdp;
Bram Moolenaard31aca22009-07-14 11:44:30 +00003779 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 int forceit;
3781 int abbr;
3782{
3783 int mode;
3784#ifdef FEAT_LOCALMAP
3785 int local;
3786
3787 local = (STRCMP(arg, "<buffer>") == 0);
3788 if (!local && *arg != NUL)
3789 {
3790 EMSG(_(e_invarg));
3791 return;
3792 }
3793#endif
3794
3795 mode = get_map_mode(&cmdp, forceit);
3796 map_clear_int(curbuf, mode,
3797#ifdef FEAT_LOCALMAP
3798 local,
3799#else
3800 FALSE,
3801#endif
3802 abbr);
3803}
3804
3805/*
3806 * Clear all mappings in "mode".
3807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 void
3809map_clear_int(buf, mode, local, abbr)
Bram Moolenaard31aca22009-07-14 11:44:30 +00003810 buf_T *buf UNUSED; /* buffer for local mappings */
3811 int mode; /* mode in which to delete */
3812 int local UNUSED; /* TRUE for buffer-local mappings */
3813 int abbr; /* TRUE for abbreviations */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 mapblock_T *mp, **mpp;
3816 int hash;
3817 int new_hash;
3818
3819 validate_maphash();
3820
3821 for (hash = 0; hash < 256; ++hash)
3822 {
3823 if (abbr)
3824 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003825 if (hash > 0) /* there is only one abbrlist */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 break;
3827#ifdef FEAT_LOCALMAP
3828 if (local)
3829 mpp = &buf->b_first_abbr;
3830 else
3831#endif
3832 mpp = &first_abbr;
3833 }
3834 else
3835 {
3836#ifdef FEAT_LOCALMAP
3837 if (local)
3838 mpp = &buf->b_maphash[hash];
3839 else
3840#endif
3841 mpp = &maphash[hash];
3842 }
3843 while (*mpp != NULL)
3844 {
3845 mp = *mpp;
3846 if (mp->m_mode & mode)
3847 {
3848 mp->m_mode &= ~mode;
3849 if (mp->m_mode == 0) /* entry can be deleted */
3850 {
3851 map_free(mpp);
3852 continue;
3853 }
3854 /*
3855 * May need to put this entry into another hash list.
3856 */
3857 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3858 if (!abbr && new_hash != hash)
3859 {
3860 *mpp = mp->m_next;
3861#ifdef FEAT_LOCALMAP
3862 if (local)
3863 {
3864 mp->m_next = buf->b_maphash[new_hash];
3865 buf->b_maphash[new_hash] = mp;
3866 }
3867 else
3868#endif
3869 {
3870 mp->m_next = maphash[new_hash];
3871 maphash[new_hash] = mp;
3872 }
3873 continue; /* continue with *mpp */
3874 }
3875 }
3876 mpp = &(mp->m_next);
3877 }
3878 }
3879}
3880
Bram Moolenaarbd743252010-10-20 21:23:33 +02003881/*
3882 * Return characters to represent the map mode in an allocated string.
3883 * Returns NULL when out of memory.
3884 */
3885 char_u *
3886map_mode_to_chars(mode)
3887 int mode;
3888{
3889 garray_T mapmode;
3890
3891 ga_init2(&mapmode, 1, 7);
3892
3893 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3894 ga_append(&mapmode, '!'); /* :map! */
3895 else if (mode & INSERT)
3896 ga_append(&mapmode, 'i'); /* :imap */
3897 else if (mode & LANGMAP)
3898 ga_append(&mapmode, 'l'); /* :lmap */
3899 else if (mode & CMDLINE)
3900 ga_append(&mapmode, 'c'); /* :cmap */
3901 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3902 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3903 ga_append(&mapmode, ' '); /* :map */
3904 else
3905 {
3906 if (mode & NORMAL)
3907 ga_append(&mapmode, 'n'); /* :nmap */
3908 if (mode & OP_PENDING)
3909 ga_append(&mapmode, 'o'); /* :omap */
3910 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3911 ga_append(&mapmode, 'v'); /* :vmap */
3912 else
3913 {
3914 if (mode & VISUAL)
3915 ga_append(&mapmode, 'x'); /* :xmap */
3916 if (mode & SELECTMODE)
3917 ga_append(&mapmode, 's'); /* :smap */
3918 }
3919 }
3920
3921 ga_append(&mapmode, NUL);
3922 return (char_u *)mapmode.ga_data;
3923}
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 static void
3926showmap(mp, local)
3927 mapblock_T *mp;
3928 int local; /* TRUE for buffer-local map */
3929{
Bram Moolenaarbd743252010-10-20 21:23:33 +02003930 int len = 1;
3931 char_u *mapchars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
3933 if (msg_didout || msg_silent != 0)
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 msg_putchar('\n');
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003936 if (got_int) /* 'q' typed at MORE prompt */
3937 return;
3938 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003939
3940 mapchars = map_mode_to_chars(mp->m_mode);
3941 if (mapchars != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaarbd743252010-10-20 21:23:33 +02003943 msg_puts(mapchars);
Bram Moolenaar6b5ef062010-10-27 12:18:00 +02003944 len = (int)STRLEN(mapchars);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003945 vim_free(mapchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003947
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 while (++len <= 3)
3949 msg_putchar(' ');
3950
Bram Moolenaarf2330482008-06-24 20:19:36 +00003951 /* Display the LHS. Get length of what we write. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 len = msg_outtrans_special(mp->m_keys, TRUE);
3953 do
3954 {
3955 msg_putchar(' '); /* padd with blanks */
3956 ++len;
3957 } while (len < 12);
3958
3959 if (mp->m_noremap == REMAP_NONE)
3960 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3961 else if (mp->m_noremap == REMAP_SCRIPT)
3962 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3963 else
3964 msg_putchar(' ');
3965
3966 if (local)
3967 msg_putchar('@');
3968 else
3969 msg_putchar(' ');
3970
3971 /* Use FALSE below if we only want things like <Up> to show up as such on
Bram Moolenaarbd743252010-10-20 21:23:33 +02003972 * the rhs, and not M-x etc, TRUE gets both -- webb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (*mp->m_str == NUL)
3974 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
3975 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02003976 {
3977 /* Remove escaping of CSI, because "m_str" is in a format to be used
3978 * as typeahead. */
3979 char_u *s = vim_strsave(mp->m_str);
3980 if (s != NULL)
3981 {
3982 vim_unescape_csi(s);
3983 msg_outtrans_special(s, FALSE);
3984 vim_free(s);
3985 }
3986 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003987#ifdef FEAT_EVAL
3988 if (p_verbose > 0)
3989 last_set_msg(mp->m_script_ID);
3990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 out_flush(); /* show one line at a time */
3992}
3993
3994#if defined(FEAT_EVAL) || defined(PROTO)
3995/*
3996 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
3997 * Recognize termcap codes in "str".
3998 * Also checks mappings local to the current buffer.
3999 */
4000 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004001map_to_exists(str, modechars, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *str;
4003 char_u *modechars;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004004 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 int mode = 0;
4007 char_u *rhs;
4008 char_u *buf;
4009 int retval;
4010
Bram Moolenaar9c102382006-05-03 21:26:49 +00004011 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (vim_strchr(modechars, 'n') != NULL)
4014 mode |= NORMAL;
4015 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004016 mode |= VISUAL + SELECTMODE;
4017 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 mode |= VISUAL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004019 if (vim_strchr(modechars, 's') != NULL)
4020 mode |= SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (vim_strchr(modechars, 'o') != NULL)
4022 mode |= OP_PENDING;
4023 if (vim_strchr(modechars, 'i') != NULL)
4024 mode |= INSERT;
4025 if (vim_strchr(modechars, 'l') != NULL)
4026 mode |= LANGMAP;
4027 if (vim_strchr(modechars, 'c') != NULL)
4028 mode |= CMDLINE;
4029
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004030 retval = map_to_exists_mode(rhs, mode, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 vim_free(buf);
4032
4033 return retval;
4034}
4035#endif
4036
4037/*
4038 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
4039 * Also checks mappings local to the current buffer.
4040 */
4041 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004042map_to_exists_mode(rhs, mode, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 char_u *rhs;
4044 int mode;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004045 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 mapblock_T *mp;
4048 int hash;
4049# ifdef FEAT_LOCALMAP
4050 int expand_buffer = FALSE;
4051
4052 validate_maphash();
4053
4054 /* Do it twice: once for global maps and once for local maps. */
4055 for (;;)
4056 {
4057# endif
4058 for (hash = 0; hash < 256; ++hash)
4059 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004060 if (abbr)
4061 {
4062 if (hash > 0) /* there is only one abbr list */
4063 break;
4064#ifdef FEAT_LOCALMAP
4065 if (expand_buffer)
4066 mp = curbuf->b_first_abbr;
4067 else
4068#endif
4069 mp = first_abbr;
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071# ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004072 else if (expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 mp = curbuf->b_maphash[hash];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074# endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 mp = maphash[hash];
4077 for (; mp; mp = mp->m_next)
4078 {
4079 if ((mp->m_mode & mode)
4080 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
4081 return TRUE;
4082 }
4083 }
4084# ifdef FEAT_LOCALMAP
4085 if (expand_buffer)
4086 break;
4087 expand_buffer = TRUE;
4088 }
4089# endif
4090
4091 return FALSE;
4092}
4093
4094#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4095/*
4096 * Used below when expanding mapping/abbreviation names.
4097 */
4098static int expand_mapmodes = 0;
4099static int expand_isabbrev = 0;
4100#ifdef FEAT_LOCALMAP
4101static int expand_buffer = FALSE;
4102#endif
4103
4104/*
4105 * Work out what to complete when doing command line completion of mapping
4106 * or abbreviation names.
4107 */
4108 char_u *
4109set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
4110 expand_T *xp;
4111 char_u *cmd;
4112 char_u *arg;
4113 int forceit; /* TRUE if '!' given */
4114 int isabbrev; /* TRUE if abbreviation */
4115 int isunmap; /* TRUE if unmap/unabbrev command */
4116 cmdidx_T cmdidx;
4117{
4118 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
4119 xp->xp_context = EXPAND_NOTHING;
4120 else
4121 {
4122 if (isunmap)
4123 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
4124 else
4125 {
4126 expand_mapmodes = INSERT + CMDLINE;
4127 if (!isabbrev)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004128 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 expand_isabbrev = isabbrev;
4131 xp->xp_context = EXPAND_MAPPINGS;
4132#ifdef FEAT_LOCALMAP
4133 expand_buffer = FALSE;
4134#endif
4135 for (;;)
4136 {
4137#ifdef FEAT_LOCALMAP
4138 if (STRNCMP(arg, "<buffer>", 8) == 0)
4139 {
4140 expand_buffer = TRUE;
4141 arg = skipwhite(arg + 8);
4142 continue;
4143 }
4144#endif
4145 if (STRNCMP(arg, "<unique>", 8) == 0)
4146 {
4147 arg = skipwhite(arg + 8);
4148 continue;
4149 }
4150 if (STRNCMP(arg, "<silent>", 8) == 0)
4151 {
4152 arg = skipwhite(arg + 8);
4153 continue;
4154 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004155#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (STRNCMP(arg, "<script>", 8) == 0)
4157 {
4158 arg = skipwhite(arg + 8);
4159 continue;
4160 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004161 if (STRNCMP(arg, "<expr>", 6) == 0)
4162 {
4163 arg = skipwhite(arg + 6);
4164 continue;
4165 }
4166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169 xp->xp_pattern = arg;
4170 }
4171
4172 return NULL;
4173}
4174
4175/*
4176 * Find all mapping/abbreviation names that match regexp 'prog'.
4177 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4178 * Return OK if matches found, FAIL otherwise.
4179 */
4180 int
4181ExpandMappings(regmatch, num_file, file)
4182 regmatch_T *regmatch;
4183 int *num_file;
4184 char_u ***file;
4185{
4186 mapblock_T *mp;
4187 int hash;
4188 int count;
4189 int round;
4190 char_u *p;
4191 int i;
4192
4193 validate_maphash();
4194
4195 *num_file = 0; /* return values in case of FAIL */
4196 *file = NULL;
4197
4198 /*
4199 * round == 1: Count the matches.
4200 * round == 2: Build the array to keep the matches.
4201 */
4202 for (round = 1; round <= 2; ++round)
4203 {
4204 count = 0;
4205
Bram Moolenaar4e427192006-03-10 21:34:27 +00004206 for (i = 0; i < 5; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
4208 if (i == 0)
4209 p = (char_u *)"<silent>";
4210 else if (i == 1)
4211 p = (char_u *)"<unique>";
4212#ifdef FEAT_EVAL
4213 else if (i == 2)
4214 p = (char_u *)"<script>";
Bram Moolenaar4e427192006-03-10 21:34:27 +00004215 else if (i == 3)
4216 p = (char_u *)"<expr>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#endif
4218#ifdef FEAT_LOCALMAP
Bram Moolenaar4e427192006-03-10 21:34:27 +00004219 else if (i == 4 && !expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 p = (char_u *)"<buffer>";
4221#endif
4222 else
4223 continue;
4224
4225 if (vim_regexec(regmatch, p, (colnr_T)0))
4226 {
4227 if (round == 1)
4228 ++count;
4229 else
4230 (*file)[count++] = vim_strsave(p);
4231 }
4232 }
4233
4234 for (hash = 0; hash < 256; ++hash)
4235 {
4236 if (expand_isabbrev)
4237 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004238 if (hash > 0) /* only one abbrev list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break; /* for (hash) */
4240 mp = first_abbr;
4241 }
4242#ifdef FEAT_LOCALMAP
4243 else if (expand_buffer)
4244 mp = curbuf->b_maphash[hash];
4245#endif
4246 else
4247 mp = maphash[hash];
4248 for (; mp; mp = mp->m_next)
4249 {
4250 if (mp->m_mode & expand_mapmodes)
4251 {
4252 p = translate_mapping(mp->m_keys, TRUE);
4253 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4254 {
4255 if (round == 1)
4256 ++count;
4257 else
4258 {
4259 (*file)[count++] = p;
4260 p = NULL;
4261 }
4262 }
4263 vim_free(p);
4264 }
4265 } /* for (mp) */
4266 } /* for (hash) */
4267
4268 if (count == 0) /* no match found */
4269 break; /* for (round) */
4270
4271 if (round == 1)
4272 {
4273 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4274 if (*file == NULL)
4275 return FAIL;
4276 }
4277 } /* for (round) */
4278
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004279 if (count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004281 char_u **ptr1;
4282 char_u **ptr2;
4283 char_u **ptr3;
4284
4285 /* Sort the matches */
4286 sort_strings(*file, count);
4287
4288 /* Remove multiple entries */
4289 ptr1 = *file;
4290 ptr2 = ptr1 + 1;
4291 ptr3 = ptr1 + count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 while (ptr2 < ptr3)
4294 {
4295 if (STRCMP(*ptr1, *ptr2))
4296 *++ptr1 = *ptr2++;
4297 else
4298 {
4299 vim_free(*ptr2++);
4300 count--;
4301 }
4302 }
4303 }
4304
4305 *num_file = count;
4306 return (count == 0 ? FAIL : OK);
4307}
4308#endif /* FEAT_CMDL_COMPL */
4309
4310/*
4311 * Check for an abbreviation.
4312 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4313 * "c" is the character typed before check_abbr was called. It may have
4314 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4315 *
4316 * Historic vi practice: The last character of an abbreviation must be an id
4317 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4318 * characters or all non-id characters. This allows for abbr. "#i" to
4319 * "#include".
4320 *
4321 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4322 * Then there must be white space before the abbr.
4323 *
4324 * return TRUE if there is an abbreviation, FALSE if not
4325 */
4326 int
4327check_abbr(c, ptr, col, mincol)
4328 int c;
4329 char_u *ptr;
4330 int col;
4331 int mincol;
4332{
4333 int len;
4334 int scol; /* starting column of the abbr. */
4335 int j;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004336 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#ifdef FEAT_MBYTE
4338 char_u tb[MB_MAXBYTES + 4];
4339#else
4340 char_u tb[4];
4341#endif
4342 mapblock_T *mp;
4343#ifdef FEAT_LOCALMAP
4344 mapblock_T *mp2;
4345#endif
4346#ifdef FEAT_MBYTE
4347 int clen = 0; /* length in characters */
4348#endif
4349 int is_id = TRUE;
4350 int vim_abbr;
4351
4352 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4353 return FALSE;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00004354 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0)
4355 /* no remapping implies no abbreviation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 return FALSE;
4357
4358 /*
4359 * Check for word before the cursor: If it ends in a keyword char all
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00004360 * chars before it must be keyword chars or non-keyword chars, but not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 * white space. If it ends in a non-keyword char we accept any characters
4362 * before it except white space.
4363 */
4364 if (col == 0) /* cannot be an abbr. */
4365 return FALSE;
4366
4367#ifdef FEAT_MBYTE
4368 if (has_mbyte)
4369 {
4370 char_u *p;
4371
4372 p = mb_prevptr(ptr, ptr + col);
4373 if (!vim_iswordp(p))
4374 vim_abbr = TRUE; /* Vim added abbr. */
4375 else
4376 {
4377 vim_abbr = FALSE; /* vi compatible abbr. */
4378 if (p > ptr)
4379 is_id = vim_iswordp(mb_prevptr(ptr, p));
4380 }
4381 clen = 1;
4382 while (p > ptr + mincol)
4383 {
4384 p = mb_prevptr(ptr, p);
4385 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4386 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004387 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 break;
4389 }
4390 ++clen;
4391 }
4392 scol = (int)(p - ptr);
4393 }
4394 else
4395#endif
4396 {
4397 if (!vim_iswordc(ptr[col - 1]))
4398 vim_abbr = TRUE; /* Vim added abbr. */
4399 else
4400 {
4401 vim_abbr = FALSE; /* vi compatible abbr. */
4402 if (col > 1)
4403 is_id = vim_iswordc(ptr[col - 2]);
4404 }
4405 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4406 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4407 ;
4408 }
4409
4410 if (scol < mincol)
4411 scol = mincol;
4412 if (scol < col) /* there is a word in front of the cursor */
4413 {
4414 ptr += scol;
4415 len = col - scol;
4416#ifdef FEAT_LOCALMAP
4417 mp = curbuf->b_first_abbr;
4418 mp2 = first_abbr;
4419 if (mp == NULL)
4420 {
4421 mp = mp2;
4422 mp2 = NULL;
4423 }
4424#else
4425 mp = first_abbr;
4426#endif
4427 for ( ; mp;
4428#ifdef FEAT_LOCALMAP
4429 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4430#endif
4431 (mp = mp->m_next))
4432 {
4433 /* find entries with right mode and keys */
4434 if ( (mp->m_mode & State)
4435 && mp->m_keylen == len
4436 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4437 break;
4438 }
4439 if (mp != NULL)
4440 {
4441 /*
4442 * Found a match:
4443 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4444 * This goes from end to start.
4445 *
4446 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4447 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4448 * Characters where IS_SPECIAL() == TRUE: key codes, need
4449 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4450 *
4451 * Character CTRL-] is treated specially - it completes the
4452 * abbreviation, but is not inserted into the input stream.
4453 */
4454 j = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (c != Ctrl_RSB)
4456 {
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004457 /* special key code, split up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if (IS_SPECIAL(c) || c == K_SPECIAL)
4459 {
4460 tb[j++] = K_SPECIAL;
4461 tb[j++] = K_SECOND(c);
4462 tb[j++] = K_THIRD(c);
4463 }
4464 else
4465 {
4466 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4467 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4468#ifdef FEAT_MBYTE
4469 if (has_mbyte)
4470 {
4471 /* if ABBR_OFF has been added, remove it here */
4472 if (c >= ABBR_OFF)
4473 c -= ABBR_OFF;
4474 j += (*mb_char2bytes)(c, tb + j);
4475 }
4476 else
4477#endif
4478 tb[j++] = c;
4479 }
4480 tb[j] = NUL;
4481 /* insert the last typed char */
4482 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4483 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004484#ifdef FEAT_EVAL
4485 if (mp->m_expr)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004486 s = eval_map_expr(mp->m_str, c);
Bram Moolenaar4e427192006-03-10 21:34:27 +00004487 else
4488#endif
4489 s = mp->m_str;
4490 if (s != NULL)
4491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 /* insert the to string */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004493 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /* no abbrev. for these chars */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004495 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4496#ifdef FEAT_EVAL
4497 if (mp->m_expr)
4498 vim_free(s);
4499#endif
4500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 tb[0] = Ctrl_H;
4503 tb[1] = NUL;
4504#ifdef FEAT_MBYTE
4505 if (has_mbyte)
4506 len = clen; /* Delete characters instead of bytes */
4507#endif
4508 while (len-- > 0) /* delete the from string */
4509 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4510 return TRUE;
4511 }
4512 }
4513 return FALSE;
4514}
4515
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004516#ifdef FEAT_EVAL
4517/*
4518 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4519 * special characters.
4520 */
4521 static char_u *
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004522eval_map_expr(str, c)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004523 char_u *str;
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004524 int c; /* NUL or typed character for abbreviation */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004525{
4526 char_u *res;
Bram Moolenaar8424a622006-04-19 21:23:36 +00004527 char_u *p;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004528 char_u *expr;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004529 char_u *save_cmd;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004530 pos_T save_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004531 int save_msg_col;
4532 int save_msg_row;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004533
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004534 /* Remove escaping of CSI, because "str" is in a format to be used as
4535 * typeahead. */
4536 expr = vim_strsave(str);
4537 if (expr == NULL)
4538 return NULL;
4539 vim_unescape_csi(expr);
4540
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004541 save_cmd = save_cmdline_alloc();
4542 if (save_cmd == NULL)
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004543 {
4544 vim_free(expr);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004545 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004546 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004547
4548 /* Forbid changing text or using ":normal" to avoid most of the bad side
4549 * effects. Also restore the cursor position. */
4550 ++textlock;
4551#ifdef FEAT_EX_EXTRA
4552 ++ex_normal_lock;
4553#endif
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004554 set_vim_var_char(c); /* set v:char to the typed character */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004555 save_cursor = curwin->w_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004556 save_msg_col = msg_col;
4557 save_msg_row = msg_row;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004558 p = eval_to_string(expr, NULL, FALSE);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004559 --textlock;
4560#ifdef FEAT_EX_EXTRA
4561 --ex_normal_lock;
4562#endif
4563 curwin->w_cursor = save_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004564 msg_col = save_msg_col;
4565 msg_row = save_msg_row;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004566
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004567 restore_cmdline_alloc(save_cmd);
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004568 vim_free(expr);
4569
Bram Moolenaar8424a622006-04-19 21:23:36 +00004570 if (p == NULL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004571 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004572 /* Escape CSI in the result to be able to use the string as typeahead. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004573 res = vim_strsave_escape_csi(p);
4574 vim_free(p);
4575
4576 return res;
4577}
4578#endif
4579
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004580/*
4581 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4582 * can be put in the typeahead buffer.
4583 * Returns NULL when out of memory.
4584 */
4585 char_u *
4586vim_strsave_escape_csi(p)
4587 char_u *p;
4588{
4589 char_u *res;
4590 char_u *s, *d;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004591
4592 /* Need a buffer to hold up to three times as much. */
Bram Moolenaar8424a622006-04-19 21:23:36 +00004593 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004594 if (res != NULL)
4595 {
Bram Moolenaar8424a622006-04-19 21:23:36 +00004596 d = res;
4597 for (s = p; *s != NUL; )
4598 {
4599 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4600 {
4601 /* Copy special key unmodified. */
4602 *d++ = *s++;
4603 *d++ = *s++;
4604 *d++ = *s++;
4605 }
4606 else
4607 {
4608 /* Add character, possibly multi-byte to destination, escaping
4609 * CSI and K_SPECIAL. */
4610 d = add_char2buf(PTR2CHAR(s), d);
4611 mb_ptr_adv(s);
4612 }
4613 }
4614 *d = NUL;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004615 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004616 return res;
4617}
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004618
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619/*
Bram Moolenaar81b85872007-03-04 20:22:01 +00004620 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4621 * vim_strsave_escape_csi(). Works in-place.
4622 */
4623 void
4624vim_unescape_csi(p)
4625 char_u *p;
4626{
4627 char_u *s = p, *d = p;
4628
4629 while (*s != NUL)
4630 {
4631 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4632 {
4633 *d++ = K_SPECIAL;
4634 s += 3;
4635 }
4636 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4637 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4638 {
4639 *d++ = CSI;
4640 s += 3;
4641 }
4642 else
4643 *d++ = *s++;
4644 }
4645 *d = NUL;
4646}
4647
4648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 * Write map commands for the current mappings to an .exrc file.
4650 * Return FAIL on error, OK otherwise.
4651 */
4652 int
4653makemap(fd, buf)
4654 FILE *fd;
4655 buf_T *buf; /* buffer for local mappings or NULL */
4656{
4657 mapblock_T *mp;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004658 char_u c1, c2, c3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u *p;
4660 char *cmd;
4661 int abbr;
4662 int hash;
4663 int did_cpo = FALSE;
4664 int i;
4665
4666 validate_maphash();
4667
4668 /*
4669 * Do the loop twice: Once for mappings, once for abbreviations.
4670 * Then loop over all map hash lists.
4671 */
4672 for (abbr = 0; abbr < 2; ++abbr)
4673 for (hash = 0; hash < 256; ++hash)
4674 {
4675 if (abbr)
4676 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004677 if (hash > 0) /* there is only one abbr list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 break;
4679#ifdef FEAT_LOCALMAP
4680 if (buf != NULL)
4681 mp = buf->b_first_abbr;
4682 else
4683#endif
4684 mp = first_abbr;
4685 }
4686 else
4687 {
4688#ifdef FEAT_LOCALMAP
4689 if (buf != NULL)
4690 mp = buf->b_maphash[hash];
4691 else
4692#endif
4693 mp = maphash[hash];
4694 }
4695
4696 for ( ; mp; mp = mp->m_next)
4697 {
4698 /* skip script-local mappings */
4699 if (mp->m_noremap == REMAP_SCRIPT)
4700 continue;
4701
4702 /* skip mappings that contain a <SNR> (script-local thing),
4703 * they probably don't work when loaded again */
4704 for (p = mp->m_str; *p != NUL; ++p)
4705 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4706 && p[2] == (int)KE_SNR)
4707 break;
4708 if (*p != NUL)
4709 continue;
4710
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004711 /* It's possible to create a mapping and then ":unmap" certain
4712 * modes. We recreate this here by mapping the individual
4713 * modes, which requires up to three of them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 c1 = NUL;
4715 c2 = NUL;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004716 c3 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (abbr)
4718 cmd = "abbr";
4719 else
4720 cmd = "map";
4721 switch (mp->m_mode)
4722 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00004723 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 break;
4725 case NORMAL:
4726 c1 = 'n';
4727 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004728 case VISUAL:
4729 c1 = 'x';
4730 break;
4731 case SELECTMODE:
4732 c1 = 's';
4733 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 case OP_PENDING:
4735 c1 = 'o';
4736 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004737 case NORMAL + VISUAL:
4738 c1 = 'n';
4739 c2 = 'x';
4740 break;
4741 case NORMAL + SELECTMODE:
4742 c1 = 'n';
4743 c2 = 's';
4744 break;
4745 case NORMAL + OP_PENDING:
4746 c1 = 'n';
4747 c2 = 'o';
4748 break;
4749 case VISUAL + SELECTMODE:
4750 c1 = 'v';
4751 break;
4752 case VISUAL + OP_PENDING:
4753 c1 = 'x';
4754 c2 = 'o';
4755 break;
4756 case SELECTMODE + OP_PENDING:
4757 c1 = 's';
4758 c2 = 'o';
4759 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004760 case NORMAL + VISUAL + SELECTMODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 c1 = 'n';
4762 c2 = 'v';
4763 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004764 case NORMAL + VISUAL + OP_PENDING:
4765 c1 = 'n';
4766 c2 = 'x';
4767 c3 = 'o';
4768 break;
4769 case NORMAL + SELECTMODE + OP_PENDING:
4770 c1 = 'n';
4771 c2 = 's';
4772 c3 = 'o';
4773 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004774 case VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 c1 = 'v';
4776 c2 = 'o';
4777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 case CMDLINE + INSERT:
4779 if (!abbr)
4780 cmd = "map!";
4781 break;
4782 case CMDLINE:
4783 c1 = 'c';
4784 break;
4785 case INSERT:
4786 c1 = 'i';
4787 break;
4788 case LANGMAP:
4789 c1 = 'l';
4790 break;
4791 default:
4792 EMSG(_("E228: makemap: Illegal mode"));
4793 return FAIL;
4794 }
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004795 do /* do this twice if c2 is set, 3 times with c3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797 /* When outputting <> form, need to make sure that 'cpo'
4798 * is set to the Vim default. */
4799 if (!did_cpo)
4800 {
4801 if (*mp->m_str == NUL) /* will use <Nop> */
4802 did_cpo = TRUE;
4803 else
4804 for (i = 0; i < 2; ++i)
4805 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4806 if (*p == K_SPECIAL || *p == NL)
4807 did_cpo = TRUE;
4808 if (did_cpo)
4809 {
4810 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4811 || put_eol(fd) < 0
4812 || fprintf(fd, "set cpo&vim") < 0
4813 || put_eol(fd) < 0)
4814 return FAIL;
4815 }
4816 }
4817 if (c1 && putc(c1, fd) < 0)
4818 return FAIL;
4819 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4820 return FAIL;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004821 if (fputs(cmd, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return FAIL;
4823 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4824 return FAIL;
4825 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4826 return FAIL;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004827#ifdef FEAT_EVAL
4828 if (mp->m_noremap == REMAP_SCRIPT
4829 && fputs("<script>", fd) < 0)
4830 return FAIL;
4831 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4832 return FAIL;
4833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 if ( putc(' ', fd) < 0
4836 || put_escstr(fd, mp->m_keys, 0) == FAIL
4837 || putc(' ', fd) < 0
4838 || put_escstr(fd, mp->m_str, 1) == FAIL
4839 || put_eol(fd) < 0)
4840 return FAIL;
4841 c1 = c2;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004842 c2 = c3;
4843 c3 = NUL;
4844 } while (c1 != NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 }
4847
4848 if (did_cpo)
4849 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4850 || put_eol(fd) < 0
4851 || fprintf(fd, "unlet s:cpo_save") < 0
4852 || put_eol(fd) < 0)
4853 return FAIL;
4854 return OK;
4855}
4856
4857/*
4858 * write escape string to file
4859 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4860 *
4861 * return FAIL for failure, OK otherwise
4862 */
4863 int
4864put_escstr(fd, strstart, what)
4865 FILE *fd;
4866 char_u *strstart;
4867 int what;
4868{
4869 char_u *str = strstart;
4870 int c;
4871 int modifiers;
4872
4873 /* :map xx <Nop> */
4874 if (*str == NUL && what == 1)
4875 {
4876 if (fprintf(fd, "<Nop>") < 0)
4877 return FAIL;
4878 return OK;
4879 }
4880
4881 for ( ; *str != NUL; ++str)
4882 {
4883#ifdef FEAT_MBYTE
4884 char_u *p;
4885
4886 /* Check for a multi-byte character, which may contain escaped
4887 * K_SPECIAL and CSI bytes */
4888 p = mb_unescape(&str);
4889 if (p != NULL)
4890 {
4891 while (*p != NUL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004892 if (fputc(*p++, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return FAIL;
4894 --str;
4895 continue;
4896 }
4897#endif
4898
4899 c = *str;
4900 /*
4901 * Special key codes have to be translated to be able to make sense
4902 * when they are read back.
4903 */
4904 if (c == K_SPECIAL && what != 2)
4905 {
4906 modifiers = 0x0;
4907 if (str[1] == KS_MODIFIER)
4908 {
4909 modifiers = str[2];
4910 str += 3;
4911 c = *str;
4912 }
4913 if (c == K_SPECIAL)
4914 {
4915 c = TO_SPECIAL(str[1], str[2]);
4916 str += 2;
4917 }
4918 if (IS_SPECIAL(c) || modifiers) /* special key */
4919 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004920 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return FAIL;
4922 continue;
4923 }
4924 }
4925
4926 /*
4927 * A '\n' in a map command should be written as <NL>.
4928 * A '\n' in a set command should be written as \^V^J.
4929 */
4930 if (c == NL)
4931 {
4932 if (what == 2)
4933 {
4934 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4935 return FAIL;
4936 }
4937 else
4938 {
4939 if (fprintf(fd, "<NL>") < 0)
4940 return FAIL;
4941 }
4942 continue;
4943 }
4944
4945 /*
4946 * Some characters have to be escaped with CTRL-V to
4947 * prevent them from misinterpreted in DoOneCmd().
4948 * A space, Tab and '"' has to be escaped with a backslash to
4949 * prevent it to be misinterpreted in do_set().
4950 * A space has to be escaped with a CTRL-V when it's at the start of a
4951 * ":map" rhs.
4952 * A '<' has to be escaped with a CTRL-V to prevent it being
4953 * interpreted as the start of a special key name.
4954 * A space in the lhs of a :map needs a CTRL-V.
4955 */
4956 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4957 {
4958 if (putc('\\', fd) < 0)
4959 return FAIL;
4960 }
4961 else if (c < ' ' || c > '~' || c == '|'
4962 || (what == 0 && c == ' ')
4963 || (what == 1 && str == strstart && c == ' ')
4964 || (what != 2 && c == '<'))
4965 {
4966 if (putc(Ctrl_V, fd) < 0)
4967 return FAIL;
4968 }
4969 if (putc(c, fd) < 0)
4970 return FAIL;
4971 }
4972 return OK;
4973}
4974
4975/*
4976 * Check all mappings for the presence of special key codes.
4977 * Used after ":set term=xxx".
4978 */
4979 void
4980check_map_keycodes()
4981{
4982 mapblock_T *mp;
4983 char_u *p;
4984 int i;
4985 char_u buf[3];
4986 char_u *save_name;
4987 int abbr;
4988 int hash;
4989#ifdef FEAT_LOCALMAP
4990 buf_T *bp;
4991#endif
4992
4993 validate_maphash();
4994 save_name = sourcing_name;
4995 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
4996
4997#ifdef FEAT_LOCALMAP
4998 /* This this once for each buffer, and then once for global
4999 * mappings/abbreviations with bp == NULL */
5000 for (bp = firstbuf; ; bp = bp->b_next)
5001 {
5002#endif
5003 /*
5004 * Do the loop twice: Once for mappings, once for abbreviations.
5005 * Then loop over all map hash lists.
5006 */
5007 for (abbr = 0; abbr <= 1; ++abbr)
5008 for (hash = 0; hash < 256; ++hash)
5009 {
5010 if (abbr)
5011 {
5012 if (hash) /* there is only one abbr list */
5013 break;
5014#ifdef FEAT_LOCALMAP
5015 if (bp != NULL)
5016 mp = bp->b_first_abbr;
5017 else
5018#endif
5019 mp = first_abbr;
5020 }
5021 else
5022 {
5023#ifdef FEAT_LOCALMAP
5024 if (bp != NULL)
5025 mp = bp->b_maphash[hash];
5026 else
5027#endif
5028 mp = maphash[hash];
5029 }
5030 for ( ; mp != NULL; mp = mp->m_next)
5031 {
5032 for (i = 0; i <= 1; ++i) /* do this twice */
5033 {
5034 if (i == 0)
5035 p = mp->m_keys; /* once for the "from" part */
5036 else
5037 p = mp->m_str; /* and once for the "to" part */
5038 while (*p)
5039 {
5040 if (*p == K_SPECIAL)
5041 {
5042 ++p;
5043 if (*p < 128) /* for "normal" tcap entries */
5044 {
5045 buf[0] = p[0];
5046 buf[1] = p[1];
5047 buf[2] = NUL;
5048 (void)add_termcap_entry(buf, FALSE);
5049 }
5050 ++p;
5051 }
5052 ++p;
5053 }
5054 }
5055 }
5056 }
5057#ifdef FEAT_LOCALMAP
5058 if (bp == NULL)
5059 break;
5060 }
5061#endif
5062 sourcing_name = save_name;
5063}
5064
Bram Moolenaarbd743252010-10-20 21:23:33 +02005065#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066/*
Bram Moolenaarbd743252010-10-20 21:23:33 +02005067 * Check the string "keys" against the lhs of all mappings.
5068 * Return pointer to rhs of mapping (mapblock->m_str).
5069 * NULL when no mapping found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
5071 char_u *
Bram Moolenaarbd743252010-10-20 21:23:33 +02005072check_map(keys, mode, exact, ign_mod, abbr, mp_ptr, local_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 char_u *keys;
5074 int mode;
5075 int exact; /* require exact match */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005076 int ign_mod; /* ignore preceding modifier */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005077 int abbr; /* do abbreviations */
Bram Moolenaarbd743252010-10-20 21:23:33 +02005078 mapblock_T **mp_ptr; /* return: pointer to mapblock or NULL */
5079 int *local_ptr; /* return: buffer-local mapping or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 int hash;
5082 int len, minlen;
5083 mapblock_T *mp;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005084 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085#ifdef FEAT_LOCALMAP
5086 int local;
5087#endif
5088
5089 validate_maphash();
5090
5091 len = (int)STRLEN(keys);
5092#ifdef FEAT_LOCALMAP
5093 for (local = 1; local >= 0; --local)
5094#endif
5095 /* loop over all hash lists */
5096 for (hash = 0; hash < 256; ++hash)
5097 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005098 if (abbr)
5099 {
5100 if (hash > 0) /* there is only one list. */
5101 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005103 if (local)
5104 mp = curbuf->b_first_abbr;
5105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005107 mp = first_abbr;
5108 }
5109#ifdef FEAT_LOCALMAP
5110 else if (local)
5111 mp = curbuf->b_maphash[hash];
5112#endif
5113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 mp = maphash[hash];
5115 for ( ; mp != NULL; mp = mp->m_next)
5116 {
5117 /* skip entries with wrong mode, wrong length and not matching
5118 * ones */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005119 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
5120 {
5121 if (len > mp->m_keylen)
5122 minlen = mp->m_keylen;
5123 else
5124 minlen = len;
5125 s = mp->m_keys;
5126 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
5127 && s[2] != NUL)
5128 {
5129 s += 3;
5130 if (len > mp->m_keylen - 3)
5131 minlen = mp->m_keylen - 3;
5132 }
5133 if (STRNCMP(s, keys, minlen) == 0)
Bram Moolenaarbd743252010-10-20 21:23:33 +02005134 {
5135 if (mp_ptr != NULL)
5136 *mp_ptr = mp;
5137 if (local_ptr != NULL)
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005138#ifdef FEAT_LOCALMAP
Bram Moolenaarbd743252010-10-20 21:23:33 +02005139 *local_ptr = local;
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005140#else
5141 *local_ptr = 0;
5142#endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005143 return mp->m_str;
Bram Moolenaarbd743252010-10-20 21:23:33 +02005144 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 }
5148
5149 return NULL;
5150}
5151#endif
5152
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005153#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar371d5402006-03-20 21:47:49 +00005154
5155#define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
5156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157/*
5158 * Default mappings for some often used keys.
5159 */
5160static struct initmap
5161{
5162 char_u *arg;
5163 int mode;
5164} initmappings[] =
5165{
5166#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
5167 /* Use the Windows (CUA) keybindings. */
5168# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /* paste, copy and cut */
5170 {(char_u *)"<S-Insert> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005171 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005173 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
5174 {(char_u *)"<S-Del> \"*d", VIS_SEL},
5175 {(char_u *)"<C-Del> \"*d", VIS_SEL},
5176 {(char_u *)"<C-X> \"*d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5178# else
Bram Moolenaar371d5402006-03-20 21:47:49 +00005179 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005181 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5183
5184 /* paste, copy and cut */
5185# ifdef FEAT_CLIPBOARD
5186# ifdef DJGPP
5187 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005188 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005190 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191# if 0 /* Shift-Del produces the same code as Del */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005192 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00005194 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5195 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196# else
5197 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005198 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005200 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5201 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5202 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5203 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204# endif
5205# else
5206 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005207 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005209 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5210 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5211 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212# endif
5213# endif
5214#endif
5215
5216#if defined(MACOS)
5217 /* Use the Standard MacOS binding. */
5218 /* paste, copy and cut */
5219 {(char_u *)"<D-v> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005220 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005222 {(char_u *)"<D-c> \"*y", VIS_SEL},
5223 {(char_u *)"<D-x> \"*d", VIS_SEL},
5224 {(char_u *)"<Backspace> \"-d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226};
Bram Moolenaar371d5402006-03-20 21:47:49 +00005227
5228# undef VIS_SEL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
5231/*
5232 * Set up default mappings.
5233 */
5234 void
5235init_mappings()
5236{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005237#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 int i;
5239
5240 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5241 add_map(initmappings[i].arg, initmappings[i].mode);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243}
5244
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005245#if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5246 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247/*
5248 * Add a mapping "map" for mode "mode".
5249 * Need to put string in allocated memory, because do_map() will modify it.
5250 */
5251 void
5252add_map(map, mode)
5253 char_u *map;
5254 int mode;
5255{
5256 char_u *s;
5257 char_u *cpo_save = p_cpo;
5258
5259 p_cpo = (char_u *)""; /* Allow <> notation */
5260 s = vim_strsave(map);
5261 if (s != NULL)
5262 {
5263 (void)do_map(0, s, mode, FALSE);
5264 vim_free(s);
5265 }
5266 p_cpo = cpo_save;
5267}
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005268#endif