blob: e24a6abcf7e1f7dade4fcc61a369878ce92d383d [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 }
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200447 else /* remove mapped characters at the start only */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {
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;
Bram Moolenaar9a920d82012-06-01 15:21:02 +0200726 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 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
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001075 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001076#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 */
Bram Moolenaarb8978712013-03-16 21:42:16 +01001340#ifdef FEAT_MOUSE
1341static int old_mouse_row; /* mouse_row related to old_char */
1342static int old_mouse_col; /* mouse_col related to old_char */
1343#endif
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1346
1347/*
1348 * Save all three kinds of typeahead, so that the user must type at a prompt.
1349 */
1350 void
1351save_typeahead(tp)
1352 tasave_T *tp;
1353{
1354 tp->save_typebuf = typebuf;
1355 tp->typebuf_valid = (alloc_typebuf() == OK);
1356 if (!tp->typebuf_valid)
1357 typebuf = tp->save_typebuf;
1358
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001359 tp->old_char = old_char;
1360 tp->old_mod_mask = old_mod_mask;
1361 old_char = -1;
1362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 tp->save_stuffbuff = stuffbuff;
1364 stuffbuff.bh_first.b_next = NULL;
1365# ifdef USE_INPUT_BUF
1366 tp->save_inputbuf = get_input_buf();
1367# endif
1368}
1369
1370/*
1371 * Restore the typeahead to what it was before calling save_typeahead().
1372 * The allocated memory is freed, can only be called once!
1373 */
1374 void
1375restore_typeahead(tp)
1376 tasave_T *tp;
1377{
1378 if (tp->typebuf_valid)
1379 {
1380 free_typebuf();
1381 typebuf = tp->save_typebuf;
1382 }
1383
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001384 old_char = tp->old_char;
1385 old_mod_mask = tp->old_mod_mask;
1386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 free_buff(&stuffbuff);
1388 stuffbuff = tp->save_stuffbuff;
1389# ifdef USE_INPUT_BUF
1390 set_input_buf(tp->save_inputbuf);
1391# endif
1392}
1393#endif
1394
1395/*
1396 * Open a new script file for the ":source!" command.
1397 */
1398 void
1399openscript(name, directly)
1400 char_u *name;
1401 int directly; /* when TRUE execute directly */
1402{
1403 if (curscript + 1 == NSCRIPT)
1404 {
1405 EMSG(_(e_nesting));
1406 return;
1407 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001408#ifdef FEAT_EVAL
1409 if (ignore_script)
1410 /* Not reading from script, also don't open one. Warning message? */
1411 return;
1412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
1414 if (scriptin[curscript] != NULL) /* already reading script */
1415 ++curscript;
1416 /* use NameBuff for expanded name */
1417 expand_env(name, NameBuff, MAXPATHL);
1418 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1419 {
1420 EMSG2(_(e_notopen), name);
1421 if (curscript)
1422 --curscript;
1423 return;
1424 }
1425 if (save_typebuf() == FAIL)
1426 return;
1427
1428 /*
1429 * Execute the commands from the file right now when using ":source!"
1430 * after ":global" or ":argdo" or in a loop. Also when another command
1431 * follows. This means the display won't be updated. Don't do this
1432 * always, "make test" would fail.
1433 */
1434 if (directly)
1435 {
1436 oparg_T oa;
1437 int oldcurscript;
1438 int save_State = State;
1439 int save_restart_edit = restart_edit;
1440 int save_insertmode = p_im;
1441 int save_finish_op = finish_op;
1442 int save_msg_scroll = msg_scroll;
1443
1444 State = NORMAL;
1445 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1446 restart_edit = 0; /* don't go to Insert mode */
1447 p_im = FALSE; /* don't use 'insertmode' */
1448 clear_oparg(&oa);
1449 finish_op = FALSE;
1450
1451 oldcurscript = curscript;
1452 do
1453 {
1454 update_topline_cursor(); /* update cursor position and topline */
1455 normal_cmd(&oa, FALSE); /* execute one command */
1456 vpeekc(); /* check for end of file */
1457 }
1458 while (scriptin[oldcurscript] != NULL);
1459
1460 State = save_State;
1461 msg_scroll = save_msg_scroll;
1462 restart_edit = save_restart_edit;
1463 p_im = save_insertmode;
1464 finish_op = save_finish_op;
1465 }
1466}
1467
1468/*
1469 * Close the currently active input script.
1470 */
1471 static void
1472closescript()
1473{
1474 free_typebuf();
1475 typebuf = saved_typebuf[curscript];
1476
1477 fclose(scriptin[curscript]);
1478 scriptin[curscript] = NULL;
1479 if (curscript > 0)
1480 --curscript;
1481}
1482
Bram Moolenaarea408852005-06-25 22:49:46 +00001483#if defined(EXITFREE) || defined(PROTO)
1484 void
1485close_all_scripts()
1486{
1487 while (scriptin[0] != NULL)
1488 closescript();
1489}
1490#endif
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1493/*
1494 * Return TRUE when reading keys from a script file.
1495 */
1496 int
1497using_script()
1498{
1499 return scriptin[curscript] != NULL;
1500}
1501#endif
1502
1503/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001504 * This function is called just before doing a blocking wait. Thus after
1505 * waiting 'updatetime' for a character to arrive.
1506 */
1507 void
1508before_blocking()
1509{
1510 updatescript(0);
1511#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001512 if (may_garbage_collect)
1513 garbage_collect();
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001514#endif
1515}
1516
1517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 * updatescipt() is called when a character can be written into the script file
1519 * or when we have waited some time for a character (c == 0)
1520 *
1521 * All the changed memfiles are synced if c == 0 or when the number of typed
1522 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1523 */
1524 void
1525updatescript(c)
1526 int c;
1527{
1528 static int count = 0;
1529
1530 if (c && scriptout)
1531 putc(c, scriptout);
1532 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1533 {
1534 ml_sync_all(c == 0, TRUE);
1535 count = 0;
1536 }
1537}
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
1540 * Get the next input character.
1541 * Can return a special key or a multi-byte character.
1542 * Can return NUL when called recursively, use safe_vgetc() if that's not
1543 * wanted.
1544 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1545 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001546 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 int
1549vgetc()
1550{
1551 int c, c2;
1552#ifdef FEAT_MBYTE
1553 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001554 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int i;
1556#endif
1557
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001558#ifdef FEAT_EVAL
1559 /* Do garbage collection when garbagecollect() was called previously and
1560 * we are now at the toplevel. */
1561 if (may_garbage_collect && want_garbage_collect)
1562 garbage_collect();
1563#endif
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 /*
1566 * If a character was put back with vungetc, it was already processed.
1567 * Return it directly.
1568 */
1569 if (old_char != -1)
1570 {
1571 c = old_char;
1572 old_char = -1;
1573 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001574#ifdef FEAT_MOUSE
1575 mouse_row = old_mouse_row;
1576 mouse_col = old_mouse_col;
1577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001579 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001581 mod_mask = 0x0;
1582 last_recorded_len = 0;
1583 for (;;) /* this is done twice if there are modifiers */
1584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (mod_mask) /* no mapping after modifier has been read */
1586 {
1587 ++no_mapping;
1588 ++allow_keys;
1589 }
1590 c = vgetorpeek(TRUE);
1591 if (mod_mask)
1592 {
1593 --no_mapping;
1594 --allow_keys;
1595 }
1596
1597 /* Get two extra bytes for special keys */
1598 if (c == K_SPECIAL
1599#ifdef FEAT_GUI
1600 || c == CSI
1601#endif
1602 )
1603 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001604 int save_allow_keys = allow_keys;
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 ++no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001607 allow_keys = 0; /* make sure BS is not found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1609 c = vgetorpeek(TRUE);
1610 --no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001611 allow_keys = save_allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (c2 == KS_MODIFIER)
1613 {
1614 mod_mask = c;
1615 continue;
1616 }
1617 c = TO_SPECIAL(c2, c);
1618
1619#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1620 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1621 * know that a menu was torn off */
1622 if (c == K_TEAROFF)
1623 {
1624 char_u name[200];
1625 int i;
1626
1627 /* get menu path, it ends with a <CR> */
1628 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1629 {
1630 name[i] = c;
1631 if (i < 199)
1632 ++i;
1633 }
1634 name[i] = NUL;
1635 gui_make_tearoff(name);
1636 continue;
1637 }
1638#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001639#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001640 /* GTK: <F10> normally selects the menu, but it's passed until
1641 * here to allow mapping it. Intercept and invoke the GTK
1642 * behavior if it's not mapped. */
1643 if (c == K_F10 && gui.menubar != NULL)
1644 {
1645 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1646 continue;
1647 }
1648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#ifdef FEAT_GUI
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001650 /* Handle focus event here, so that the caller doesn't need to
1651 * know about it. Return K_IGNORE so that we loop once (needed if
1652 * 'lazyredraw' is set). */
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001653 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1654 {
1655 ui_focus_change(c == K_FOCUSGAINED);
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001656 c = K_IGNORE;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001657 }
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 /* Translate K_CSI to CSI. The special key is only used to avoid
1660 * it being recognized as the start of a special key. */
1661 if (c == K_CSI)
1662 c = CSI;
1663#endif
1664 }
1665#ifdef MSDOS
1666 /*
1667 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1668 * Delete the 3 here.
1669 */
1670 else if (c == K_NUL && vpeekc() == 3)
1671 (void)vgetorpeek(TRUE);
1672#endif
1673
Bram Moolenaara88d9682005-03-25 21:45:43 +00001674 /* a keypad or special function key was not mapped, use it like
1675 * its ASCII equivalent */
1676 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
Bram Moolenaara88d9682005-03-25 21:45:43 +00001678 case K_KPLUS: c = '+'; break;
1679 case K_KMINUS: c = '-'; break;
1680 case K_KDIVIDE: c = '/'; break;
1681 case K_KMULTIPLY: c = '*'; break;
1682 case K_KENTER: c = CAR; break;
1683 case K_KPOINT:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001684#ifdef WIN32
Bram Moolenaara88d9682005-03-25 21:45:43 +00001685 /* Can be either '.' or a ',', *
1686 * depending on the type of keypad. */
1687 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001688#else
Bram Moolenaara88d9682005-03-25 21:45:43 +00001689 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001690#endif
Bram Moolenaara88d9682005-03-25 21:45:43 +00001691 case K_K0: c = '0'; break;
1692 case K_K1: c = '1'; break;
1693 case K_K2: c = '2'; break;
1694 case K_K3: c = '3'; break;
1695 case K_K4: c = '4'; break;
1696 case K_K5: c = '5'; break;
1697 case K_K6: c = '6'; break;
1698 case K_K7: c = '7'; break;
1699 case K_K8: c = '8'; break;
1700 case K_K9: c = '9'; break;
1701
1702 case K_XHOME:
1703 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1704 {
1705 c = K_S_HOME;
1706 mod_mask = 0;
1707 }
1708 else if (mod_mask == MOD_MASK_CTRL)
1709 {
1710 c = K_C_HOME;
1711 mod_mask = 0;
1712 }
1713 else
1714 c = K_HOME;
1715 break;
1716 case K_XEND:
1717 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1718 {
1719 c = K_S_END;
1720 mod_mask = 0;
1721 }
1722 else if (mod_mask == MOD_MASK_CTRL)
1723 {
1724 c = K_C_END;
1725 mod_mask = 0;
1726 }
1727 else
1728 c = K_END;
1729 break;
1730
1731 case K_XUP: c = K_UP; break;
1732 case K_XDOWN: c = K_DOWN; break;
1733 case K_XLEFT: c = K_LEFT; break;
1734 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
1736
1737#ifdef FEAT_MBYTE
1738 /* For a multi-byte character get all the bytes and return the
1739 * converted character.
1740 * Note: This will loop until enough bytes are received!
1741 */
1742 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1743 {
1744 ++no_mapping;
1745 buf[0] = c;
1746 for (i = 1; i < n; ++i)
1747 {
1748 buf[i] = vgetorpeek(TRUE);
1749 if (buf[i] == K_SPECIAL
1750#ifdef FEAT_GUI
1751 || buf[i] == CSI
1752#endif
1753 )
1754 {
1755 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1756 * which represents a K_SPECIAL (0x80),
1757 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1758 * a CSI (0x9B),
1759 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1760 c = vgetorpeek(TRUE);
1761 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1762 buf[i] = CSI;
1763 }
1764 }
1765 --no_mapping;
1766 c = (*mb_ptr2char)(buf);
1767 }
1768#endif
1769
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001770 break;
1771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001773
1774#ifdef FEAT_EVAL
1775 /*
1776 * In the main loop "may_garbage_collect" can be set to do garbage
1777 * collection in the first next vgetc(). It's disabled after that to
1778 * avoid internally used Lists and Dicts to be freed.
1779 */
1780 may_garbage_collect = FALSE;
1781#endif
1782
1783 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784}
1785
1786/*
1787 * Like vgetc(), but never return a NUL when called recursively, get a key
1788 * directly from the user (ignoring typeahead).
1789 */
1790 int
1791safe_vgetc()
1792{
1793 int c;
1794
1795 c = vgetc();
1796 if (c == NUL)
1797 c = get_keystroke();
1798 return c;
1799}
1800
1801/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001802 * Like safe_vgetc(), but loop to handle K_IGNORE.
1803 * Also ignore scrollbar events.
1804 */
1805 int
1806plain_vgetc()
1807{
1808 int c;
1809
1810 do
1811 {
1812 c = safe_vgetc();
1813 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1814 return c;
1815}
1816
1817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 * Check if a character is available, such that vgetc() will not block.
1819 * If the next character is a special character or multi-byte, the returned
1820 * character is not valid!.
1821 */
1822 int
1823vpeekc()
1824{
1825 if (old_char != -1)
1826 return old_char;
1827 return vgetorpeek(FALSE);
1828}
1829
1830#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1831/*
1832 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1833 * codes.
1834 */
1835 int
1836vpeekc_nomap()
1837{
1838 int c;
1839
1840 ++no_mapping;
1841 ++allow_keys;
1842 c = vpeekc();
1843 --no_mapping;
1844 --allow_keys;
1845 return c;
1846}
1847#endif
1848
1849#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1850/*
1851 * Check if any character is available, also half an escape sequence.
1852 * Trick: when no typeahead found, but there is something in the typeahead
1853 * buffer, it must be an ESC that is recognized as the start of a key code.
1854 */
1855 int
1856vpeekc_any()
1857{
1858 int c;
1859
1860 c = vpeekc();
1861 if (c == NUL && typebuf.tb_len > 0)
1862 c = ESC;
1863 return c;
1864}
1865#endif
1866
1867/*
1868 * Call vpeekc() without causing anything to be mapped.
1869 * Return TRUE if a character is available, FALSE otherwise.
1870 */
1871 int
1872char_avail()
1873{
1874 int retval;
1875
1876 ++no_mapping;
1877 retval = vpeekc();
1878 --no_mapping;
1879 return (retval != NUL);
1880}
1881
1882 void
1883vungetc(c) /* unget one character (can only be done once!) */
1884 int c;
1885{
1886 old_char = c;
1887 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001888#ifdef FEAT_MOUSE
1889 old_mouse_row = mouse_row;
1890 old_mouse_col = mouse_col;
1891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892}
1893
1894/*
1895 * get a character:
1896 * 1. from the stuffbuffer
1897 * This is used for abbreviated commands like "D" -> "d$".
1898 * Also used to redo a command for ".".
1899 * 2. from the typeahead buffer
1900 * Stores text obtained previously but not used yet.
1901 * Also stores the result of mappings.
1902 * Also used for the ":normal" command.
1903 * 3. from the user
1904 * This may do a blocking wait if "advance" is TRUE.
1905 *
1906 * if "advance" is TRUE (vgetc()):
1907 * really get the character.
1908 * KeyTyped is set to TRUE in the case the user typed the key.
1909 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1910 * if "advance" is FALSE (vpeekc()):
1911 * just look whether there is a character available.
1912 *
1913 * When "no_mapping" is zero, checks for mappings in the current mode.
1914 * Only returns one byte (of a multi-byte character).
1915 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1916 */
1917 static int
1918vgetorpeek(advance)
1919 int advance;
1920{
1921 int c, c1;
1922 int keylen;
1923 char_u *s;
1924 mapblock_T *mp;
1925#ifdef FEAT_LOCALMAP
1926 mapblock_T *mp2;
Bram Moolenaar0825c002013-06-12 21:00:26 +02001927 int expecting_global_mappings;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#endif
1929 mapblock_T *mp_match;
1930 int mp_match_len = 0;
1931 int timedout = FALSE; /* waited for more than 1 second
1932 for mapping to complete */
1933 int mapdepth = 0; /* check for recursive mapping */
1934 int mode_deleted = FALSE; /* set when mode has been deleted */
1935 int local_State;
1936 int mlen;
1937 int max_mlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 int i;
Bram Moolenaar4e427192006-03-10 21:34:27 +00001939#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 int new_wcol, new_wrow;
1941#endif
1942#ifdef FEAT_GUI
1943# ifdef FEAT_MENU
1944 int idx;
1945# endif
1946 int shape_changed = FALSE; /* adjusted cursor shape */
1947#endif
1948 int n;
1949#ifdef FEAT_LANGMAP
1950 int nolmaplen;
1951#endif
1952 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00001953 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 /*
1956 * This function doesn't work very well when called recursively. This may
1957 * happen though, because of:
1958 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1959 * there is a character available, which calls this function. In that
1960 * case we must return NUL, to indicate no character is available.
1961 * 2. A GUI callback function writes to the screen, causing a
1962 * wait_return().
1963 * Using ":normal" can also do this, but it saves the typeahead buffer,
1964 * thus it should be OK. But don't get a key from the user then.
1965 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001966 if (vgetc_busy > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#ifdef FEAT_EX_EXTRA
1968 && ex_normal_busy == 0
1969#endif
1970 )
1971 return NUL;
1972
1973 local_State = get_real_state();
1974
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001975 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 if (advance)
1978 KeyStuffed = FALSE;
1979
1980 init_typebuf();
1981 start_stuff();
1982 if (advance && typebuf.tb_maplen == 0)
1983 Exec_reg = FALSE;
1984 do
1985 {
1986/*
1987 * get a character: 1. from the stuffbuffer
1988 */
1989 if (typeahead_char != 0)
1990 {
1991 c = typeahead_char;
1992 if (advance)
1993 typeahead_char = 0;
1994 }
1995 else
1996 c = read_stuff(advance);
1997 if (c != NUL && !got_int)
1998 {
1999 if (advance)
2000 {
2001 /* KeyTyped = FALSE; When the command that stuffed something
2002 * was typed, behave like the stuffed command was typed.
2003 * needed for CTRL-W CTRl-] to open a fold, for example. */
2004 KeyStuffed = TRUE;
2005 }
2006 if (typebuf.tb_no_abbr_cnt == 0)
2007 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
2008 }
2009 else
2010 {
2011 /*
2012 * Loop until we either find a matching mapped key, or we
2013 * are sure that it is not a mapped key.
2014 * If a mapped key sequence is found we go back to the start to
2015 * try re-mapping.
2016 */
2017 for (;;)
2018 {
2019 /*
2020 * ui_breakcheck() is slow, don't use it too often when
2021 * inside a mapping. But call it each time for typed
2022 * characters.
2023 */
2024 if (typebuf.tb_maplen)
2025 line_breakcheck();
2026 else
2027 ui_breakcheck(); /* check for CTRL-C */
2028 keylen = 0;
2029 if (got_int)
2030 {
2031 /* flush all input */
2032 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
2033 typebuf.tb_change_cnt);
2034 /*
2035 * If inchar() returns TRUE (script file was active) or we
2036 * are inside a mapping, get out of insert mode.
2037 * Otherwise we behave like having gotten a CTRL-C.
2038 * As a result typing CTRL-C in insert mode will
2039 * really insert a CTRL-C.
2040 */
2041 if ((c || typebuf.tb_maplen)
2042 && (State & (INSERT + CMDLINE)))
2043 c = ESC;
2044 else
2045 c = Ctrl_C;
2046 flush_buffers(TRUE); /* flush all typeahead */
2047
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002048 if (advance)
2049 {
2050 /* Also record this character, it might be needed to
2051 * get out of Insert mode. */
2052 *typebuf.tb_buf = c;
2053 gotchars(typebuf.tb_buf, 1);
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 cmd_silent = FALSE;
2056
2057 break;
2058 }
2059 else if (typebuf.tb_len > 0)
2060 {
2061 /*
2062 * Check for a mappable key sequence.
2063 * Walk through one maphash[] list until we find an
2064 * entry that matches.
2065 *
2066 * Don't look for mappings if:
2067 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2068 * - maphash_valid not set: no mappings present.
2069 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2070 * - in insert or cmdline mode and 'paste' option set
2071 * - waiting for "hit return to continue" and CR or SPACE
2072 * typed
2073 * - waiting for a char with --more--
2074 * - in Ctrl-X mode, and we get a valid char for that mode
2075 */
2076 mp = NULL;
2077 max_mlen = 0;
2078 c1 = typebuf.tb_buf[typebuf.tb_off];
2079 if (no_mapping == 0 && maphash_valid
2080 && (no_zero_mapping == 0 || c1 != '0')
2081 && (typebuf.tb_maplen == 0
2082 || (p_remap
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002083 && (typebuf.tb_noremap[typebuf.tb_off]
2084 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 && !(p_paste && (State & (INSERT + CMDLINE)))
2086 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2087 && State != ASKMORE
2088 && State != CONFIRM
2089#ifdef FEAT_INS_EXPAND
2090 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002091 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2093#endif
2094 )
2095 {
2096#ifdef FEAT_LANGMAP
2097 if (c1 == K_SPECIAL)
2098 nolmaplen = 2;
2099 else
2100 {
2101 LANGMAP_ADJUST(c1, TRUE);
2102 nolmaplen = 0;
2103 }
2104#endif
2105#ifdef FEAT_LOCALMAP
2106 /* First try buffer-local mappings. */
2107 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2108 mp2 = maphash[MAP_HASH(local_State, c1)];
Bram Moolenaar0825c002013-06-12 21:00:26 +02002109 expecting_global_mappings = (mp && mp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (mp == NULL)
2111 {
2112 mp = mp2;
2113 mp2 = NULL;
2114 }
2115#else
2116 mp = maphash[MAP_HASH(local_State, c1)];
2117#endif
2118 /*
2119 * Loop until a partly matching mapping is found or
2120 * all (local) mappings have been checked.
2121 * The longest full match is remembered in "mp_match".
2122 * A full match is only accepted if there is no partly
2123 * match, so "aa" and "aaa" can both be mapped.
2124 */
2125 mp_match = NULL;
2126 mp_match_len = 0;
2127 for ( ; mp != NULL;
2128#ifdef FEAT_LOCALMAP
2129 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2130#endif
2131 (mp = mp->m_next))
2132 {
Bram Moolenaar0825c002013-06-12 21:00:26 +02002133#ifdef FEAT_LOCALMAP
2134 if (expecting_global_mappings && mp2 == NULL)
2135 {
2136 /* This is the first global mapping. If we've
2137 * got a complete buffer-local match, use it. */
2138 if (mp_match)
2139 break;
2140 expecting_global_mappings = FALSE;
2141 }
2142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 /*
2144 * Only consider an entry if the first character
2145 * matches and it is for the current state.
2146 * Skip ":lmap" mappings if keys were mapped.
2147 */
2148 if (mp->m_keys[0] == c1
2149 && (mp->m_mode & local_State)
2150 && ((mp->m_mode & LANGMAP) == 0
2151 || typebuf.tb_maplen == 0))
2152 {
2153#ifdef FEAT_LANGMAP
2154 int nomap = nolmaplen;
2155 int c2;
2156#endif
2157 /* find the match length of this mapping */
2158 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2159 {
2160#ifdef FEAT_LANGMAP
2161 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2162 if (nomap > 0)
2163 --nomap;
2164 else if (c2 == K_SPECIAL)
2165 nomap = 2;
2166 else
2167 LANGMAP_ADJUST(c2, TRUE);
2168 if (mp->m_keys[mlen] != c2)
2169#else
2170 if (mp->m_keys[mlen] !=
2171 typebuf.tb_buf[typebuf.tb_off + mlen])
2172#endif
2173 break;
2174 }
2175
2176#ifdef FEAT_MBYTE
2177 /* Don't allow mapping the first byte(s) of a
2178 * multi-byte char. Happens when mapping
2179 * <M-a> and then changing 'encoding'. */
2180 if (has_mbyte && MB_BYTE2LEN(c1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002181 > (*mb_ptr2len)(mp->m_keys))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 mlen = 0;
2183#endif
2184 /*
2185 * Check an entry whether it matches.
2186 * - Full match: mlen == keylen
2187 * - Partly match: mlen == typebuf.tb_len
2188 */
2189 keylen = mp->m_keylen;
2190 if (mlen == keylen
2191 || (mlen == typebuf.tb_len
2192 && typebuf.tb_len < keylen))
2193 {
2194 /*
2195 * If only script-local mappings are
2196 * allowed, check if the mapping starts
2197 * with K_SNR.
2198 */
2199 s = typebuf.tb_noremap + typebuf.tb_off;
2200 if (*s == RM_SCRIPT
2201 && (mp->m_keys[0] != K_SPECIAL
2202 || mp->m_keys[1] != KS_EXTRA
2203 || mp->m_keys[2]
2204 != (int)KE_SNR))
2205 continue;
2206 /*
2207 * If one of the typed keys cannot be
2208 * remapped, skip the entry.
2209 */
2210 for (n = mlen; --n >= 0; )
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002211 if (*s++ & (RM_NONE|RM_ABBR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 break;
2213 if (n >= 0)
2214 continue;
2215
2216 if (keylen > typebuf.tb_len)
2217 {
2218 if (!timedout)
2219 {
2220 /* break at a partly match */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002221 keylen = KEYLEN_PART_MAP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 break;
2223 }
2224 }
2225 else if (keylen > mp_match_len)
2226 {
2227 /* found a longer match */
2228 mp_match = mp;
2229 mp_match_len = keylen;
2230 }
2231 }
2232 else
2233 /* No match; may have to check for
2234 * termcode at next character. */
2235 if (max_mlen < mlen)
2236 max_mlen = mlen;
2237 }
2238 }
2239
2240 /* If no partly match found, use the longest full
2241 * match. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002242 if (keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 mp = mp_match;
2245 keylen = mp_match_len;
2246 }
2247 }
2248
2249 /* Check for match with 'pastetoggle' */
2250 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2251 {
2252 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2253 ++mlen)
2254 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2255 + mlen])
2256 break;
2257 if (p_pt[mlen] == NUL) /* match */
2258 {
2259 /* write chars to script file(s) */
2260 if (mlen > typebuf.tb_maplen)
2261 gotchars(typebuf.tb_buf + typebuf.tb_off
2262 + typebuf.tb_maplen,
2263 mlen - typebuf.tb_maplen);
2264
2265 del_typebuf(mlen, 0); /* remove the chars */
2266 set_option_value((char_u *)"paste",
2267 (long)!p_paste, NULL, 0);
2268 if (!(State & INSERT))
2269 {
2270 msg_col = 0;
2271 msg_row = Rows - 1;
2272 msg_clr_eos(); /* clear ruler */
2273 }
2274 showmode();
2275 setcursor();
2276 continue;
2277 }
2278 /* Need more chars for partly match. */
2279 if (mlen == typebuf.tb_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002280 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else if (max_mlen < mlen)
2282 /* no match, may have to check for termcode at
2283 * next character */
2284 max_mlen = mlen + 1;
2285 }
2286
2287 if ((mp == NULL || max_mlen >= mp_match_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002288 && keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002290 int save_keylen = keylen;
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 /*
2293 * When no matching mapping found or found a
2294 * non-matching mapping that matches at least what the
2295 * matching mapping matched:
2296 * Check if we have a terminal code, when:
2297 * mapping is allowed,
2298 * keys have not been mapped,
2299 * and not an ESC sequence, not in insert mode or
2300 * p_ek is on,
2301 * and when not timed out,
2302 */
2303 if ((no_mapping == 0 || allow_keys != 0)
2304 && (typebuf.tb_maplen == 0
2305 || (p_remap && typebuf.tb_noremap[
2306 typebuf.tb_off] == RM_YES))
2307 && !timedout)
2308 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01002309 keylen = check_termcode(max_mlen + 1,
2310 NULL, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaarf2330482008-06-24 20:19:36 +00002312 /* If no termcode matched but 'pastetoggle'
2313 * matched partially it's like an incomplete key
2314 * sequence. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002315 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2316 keylen = KEYLEN_PART_KEY;
Bram Moolenaarf2330482008-06-24 20:19:36 +00002317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /*
2319 * When getting a partial match, but the last
2320 * characters were not typed, don't wait for a
2321 * typed character to complete the termcode.
2322 * This helps a lot when a ":normal" command ends
2323 * in an ESC.
2324 */
2325 if (keylen < 0
2326 && typebuf.tb_len == typebuf.tb_maplen)
2327 keylen = 0;
2328 }
2329 else
2330 keylen = 0;
2331 if (keylen == 0) /* no matching terminal code */
2332 {
2333#ifdef AMIGA /* check for window bounds report */
2334 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2335 typebuf.tb_off] & 0xff) == CSI)
2336 {
2337 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2338 s < typebuf.tb_buf + typebuf.tb_off
2339 + typebuf.tb_len
2340 && (VIM_ISDIGIT(*s) || *s == ';'
2341 || *s == ' ');
2342 ++s)
2343 ;
2344 if (*s == 'r' || *s == '|') /* found one */
2345 {
2346 del_typebuf((int)(s + 1 -
2347 (typebuf.tb_buf + typebuf.tb_off)), 0);
2348 /* get size and redraw screen */
2349 shell_resized();
2350 continue;
2351 }
2352 if (*s == NUL) /* need more characters */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002353 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355 if (keylen >= 0)
2356#endif
2357 /* When there was a matching mapping and no
2358 * termcode could be replaced after another one,
Bram Moolenaarf2330482008-06-24 20:19:36 +00002359 * use that mapping (loop around). If there was
2360 * no mapping use the character from the
2361 * typeahead buffer right here. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (mp == NULL)
2363 {
2364/*
2365 * get a character: 2. from the typeahead buffer
2366 */
2367 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2368 if (advance) /* remove chars from tb_buf */
2369 {
2370 cmd_silent = (typebuf.tb_silent > 0);
2371 if (typebuf.tb_maplen > 0)
2372 KeyTyped = FALSE;
2373 else
2374 {
2375 KeyTyped = TRUE;
2376 /* write char to script file(s) */
2377 gotchars(typebuf.tb_buf
2378 + typebuf.tb_off, 1);
2379 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00002380 KeyNoremap = typebuf.tb_noremap[
2381 typebuf.tb_off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 del_typebuf(1, 0);
2383 }
2384 break; /* got character, break for loop */
2385 }
2386 }
2387 if (keylen > 0) /* full matching terminal code */
2388 {
2389#if defined(FEAT_GUI) && defined(FEAT_MENU)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002390 if (typebuf.tb_len >= 2
2391 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 && typebuf.tb_buf[typebuf.tb_off + 1]
2393 == KS_MENU)
2394 {
2395 /*
2396 * Using a menu may cause a break in undo!
2397 * It's like using gotchars(), but without
2398 * recording or writing to a script file.
2399 */
2400 may_sync_undo();
2401 del_typebuf(3, 0);
2402 idx = get_menu_index(current_menu, local_State);
2403 if (idx != MENU_INDEX_INVALID)
2404 {
2405# ifdef FEAT_VISUAL
2406 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002407 * In Select mode and a Visual mode menu
2408 * is used: Switch to Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * temporarily. Append K_SELECT to switch
2410 * back to Select mode.
2411 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002412 if (VIsual_active && VIsual_select
2413 && (current_menu->modes & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 VIsual_select = FALSE;
2416 (void)ins_typebuf(K_SELECT_STRING,
2417 REMAP_NONE, 0, TRUE, FALSE);
2418 }
2419# endif
2420 ins_typebuf(current_menu->strings[idx],
2421 current_menu->noremap[idx],
2422 0, TRUE,
2423 current_menu->silent[idx]);
2424 }
2425 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002426#endif /* FEAT_GUI && FEAT_MENU */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 continue; /* try mapping again */
2428 }
2429
2430 /* Partial match: get some more characters. When a
2431 * matching mapping was found use that one. */
2432 if (mp == NULL || keylen < 0)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002433 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 else
2435 keylen = mp_match_len;
2436 }
2437
2438 /* complete match */
2439 if (keylen >= 0 && keylen <= typebuf.tb_len)
2440 {
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002441#ifdef FEAT_EVAL
2442 int save_m_expr;
2443 int save_m_noremap;
2444 int save_m_silent;
2445 char_u *save_m_keys;
2446 char_u *save_m_str;
2447#else
2448# define save_m_noremap mp->m_noremap
2449# define save_m_silent mp->m_silent
2450#endif
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 /* write chars to script file(s) */
2453 if (keylen > typebuf.tb_maplen)
2454 gotchars(typebuf.tb_buf + typebuf.tb_off
2455 + typebuf.tb_maplen,
2456 keylen - typebuf.tb_maplen);
2457
2458 cmd_silent = (typebuf.tb_silent > 0);
2459 del_typebuf(keylen, 0); /* remove the mapped keys */
2460
2461 /*
2462 * Put the replacement string in front of mapstr.
2463 * The depth check catches ":map x y" and ":map y x".
2464 */
2465 if (++mapdepth >= p_mmd)
2466 {
2467 EMSG(_("E223: recursive mapping"));
2468 if (State & CMDLINE)
2469 redrawcmdline();
2470 else
2471 setcursor();
2472 flush_buffers(FALSE);
2473 mapdepth = 0; /* for next one */
2474 c = -1;
2475 break;
2476 }
2477
2478#ifdef FEAT_VISUAL
2479 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002480 * In Select mode and a Visual mode mapping is used:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 * Switch to Visual mode temporarily. Append K_SELECT
2482 * to switch back to Select mode.
2483 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002484 if (VIsual_active && VIsual_select
2485 && (mp->m_mode & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 VIsual_select = FALSE;
2488 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2489 0, TRUE, FALSE);
2490 }
2491#endif
2492
Bram Moolenaar4e427192006-03-10 21:34:27 +00002493#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002494 /* Copy the values from *mp that are used, because
2495 * evaluating the expression may invoke a function
2496 * that redefines the mapping, thereby making *mp
2497 * invalid. */
2498 save_m_expr = mp->m_expr;
2499 save_m_noremap = mp->m_noremap;
2500 save_m_silent = mp->m_silent;
2501 save_m_keys = NULL; /* only saved when needed */
2502 save_m_str = NULL; /* only saved when needed */
2503
Bram Moolenaar4e427192006-03-10 21:34:27 +00002504 /*
2505 * Handle ":map <expr>": evaluate the {rhs} as an
Bram Moolenaarb3479bd2011-10-12 22:02:14 +02002506 * expression. Also save and restore the command line
2507 * for "normal :".
Bram Moolenaar4e427192006-03-10 21:34:27 +00002508 */
2509 if (mp->m_expr)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002510 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002511 int save_vgetc_busy = vgetc_busy;
2512
Bram Moolenaarb3479bd2011-10-12 22:02:14 +02002513 vgetc_busy = 0;
2514 save_m_keys = vim_strsave(mp->m_keys);
2515 save_m_str = vim_strsave(mp->m_str);
2516 s = eval_map_expr(save_m_str, NUL);
2517 vgetc_busy = save_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002518 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00002519 else
2520#endif
2521 s = mp->m_str;
2522
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 /*
2524 * Insert the 'to' part in the typebuf.tb_buf.
2525 * If 'from' field is the same as the start of the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002526 * 'to' field, don't remap the first character (but do
2527 * allow abbreviations).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 * If m_noremap is set, don't remap the whole 'to'
2529 * part.
2530 */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002531 if (s == NULL)
2532 i = FAIL;
2533 else
2534 {
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002535 int noremap;
2536
2537 if (save_m_noremap != REMAP_YES)
2538 noremap = save_m_noremap;
2539 else if (
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002540#ifdef FEAT_EVAL
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002541 STRNCMP(s, save_m_keys != NULL
2542 ? save_m_keys : mp->m_keys,
2543 (size_t)keylen)
2544#else
2545 STRNCMP(s, mp->m_keys, (size_t)keylen)
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002546#endif
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002547 != 0)
2548 noremap = REMAP_YES;
2549 else
2550 noremap = REMAP_SKIP;
2551 i = ins_typebuf(s, noremap,
2552 0, TRUE, cmd_silent || save_m_silent);
Bram Moolenaar4e427192006-03-10 21:34:27 +00002553#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002554 if (save_m_expr)
Bram Moolenaar4e427192006-03-10 21:34:27 +00002555 vim_free(s);
2556#endif
2557 }
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002558#ifdef FEAT_EVAL
2559 vim_free(save_m_keys);
2560 vim_free(save_m_str);
2561#endif
Bram Moolenaar4e427192006-03-10 21:34:27 +00002562 if (i == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 c = -1;
2565 break;
2566 }
2567 continue;
2568 }
2569 }
2570
2571/*
2572 * get a character: 3. from the user - handle <Esc> in Insert mode
2573 */
2574 /*
2575 * special case: if we get an <ESC> in insert mode and there
2576 * are no more characters at once, we pretend to go out of
2577 * insert mode. This prevents the one second delay after
2578 * typing an <ESC>. If we get something after all, we may
2579 * have to redisplay the mode. That the cursor is in the wrong
2580 * place does not matter.
2581 */
2582 c = 0;
2583#ifdef FEAT_CMDL_INFO
2584 new_wcol = curwin->w_wcol;
2585 new_wrow = curwin->w_wrow;
2586#endif
2587 if ( advance
2588 && typebuf.tb_len == 1
2589 && typebuf.tb_buf[typebuf.tb_off] == ESC
2590 && !no_mapping
2591#ifdef FEAT_EX_EXTRA
2592 && ex_normal_busy == 0
2593#endif
2594 && typebuf.tb_maplen == 0
2595 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002596 && (p_timeout
2597 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2599 + typebuf.tb_len, 3, 25L,
2600 typebuf.tb_change_cnt)) == 0)
2601 {
2602 colnr_T col = 0, vcol;
2603 char_u *ptr;
2604
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002605 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 unshowmode(TRUE);
2608 mode_deleted = TRUE;
2609 }
2610#ifdef FEAT_GUI
2611 /* may show different cursor shape */
2612 if (gui.in_use)
2613 {
2614 int save_State;
2615
2616 save_State = State;
2617 State = NORMAL;
2618 gui_update_cursor(TRUE, FALSE);
2619 State = save_State;
2620 shape_changed = TRUE;
2621 }
2622#endif
2623 validate_cursor();
2624 old_wcol = curwin->w_wcol;
2625 old_wrow = curwin->w_wrow;
2626
2627 /* move cursor left, if possible */
2628 if (curwin->w_cursor.col != 0)
2629 {
2630 if (curwin->w_wcol > 0)
2631 {
2632 if (did_ai)
2633 {
2634 /*
2635 * We are expecting to truncate the trailing
2636 * white-space, so find the last non-white
2637 * character -- webb
2638 */
2639 col = vcol = curwin->w_wcol = 0;
2640 ptr = ml_get_curline();
2641 while (col < curwin->w_cursor.col)
2642 {
2643 if (!vim_iswhite(ptr[col]))
2644 curwin->w_wcol = vcol;
2645 vcol += lbr_chartabsize(ptr + col,
2646 (colnr_T)vcol);
2647#ifdef FEAT_MBYTE
2648 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002649 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 else
2651#endif
2652 ++col;
2653 }
2654 curwin->w_wrow = curwin->w_cline_row
2655 + curwin->w_wcol / W_WIDTH(curwin);
2656 curwin->w_wcol %= W_WIDTH(curwin);
2657 curwin->w_wcol += curwin_col_off();
2658#ifdef FEAT_MBYTE
2659 col = 0; /* no correction needed */
2660#endif
2661 }
2662 else
2663 {
2664 --curwin->w_wcol;
2665#ifdef FEAT_MBYTE
2666 col = curwin->w_cursor.col - 1;
2667#endif
2668 }
2669 }
2670 else if (curwin->w_p_wrap && curwin->w_wrow)
2671 {
2672 --curwin->w_wrow;
2673 curwin->w_wcol = W_WIDTH(curwin) - 1;
2674#ifdef FEAT_MBYTE
2675 col = curwin->w_cursor.col - 1;
2676#endif
2677 }
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2680 {
2681 /* Correct when the cursor is on the right halve
2682 * of a double-wide character. */
2683 ptr = ml_get_curline();
2684 col -= (*mb_head_off)(ptr, ptr + col);
2685 if ((*mb_ptr2cells)(ptr + col) > 1)
2686 --curwin->w_wcol;
2687 }
2688#endif
2689 }
2690 setcursor();
2691 out_flush();
2692#ifdef FEAT_CMDL_INFO
2693 new_wcol = curwin->w_wcol;
2694 new_wrow = curwin->w_wrow;
2695#endif
2696 curwin->w_wcol = old_wcol;
2697 curwin->w_wrow = old_wrow;
2698 }
2699 if (c < 0)
2700 continue; /* end of input script reached */
2701 typebuf.tb_len += c;
2702
2703 /* buffer full, don't map */
2704 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2705 {
2706 timedout = TRUE;
2707 continue;
2708 }
2709
2710#ifdef FEAT_EX_EXTRA
2711 if (ex_normal_busy > 0)
2712 {
2713# ifdef FEAT_CMDWIN
2714 static int tc = 0;
2715# endif
2716
2717 /* No typeahead left and inside ":normal". Must return
2718 * something to avoid getting stuck. When an incomplete
2719 * mapping is present, behave like it timed out. */
2720 if (typebuf.tb_len > 0)
2721 {
2722 timedout = TRUE;
2723 continue;
2724 }
2725 /* When 'insertmode' is set, ESC just beeps in Insert
2726 * mode. Use CTRL-L to make edit() return.
2727 * For the command line only CTRL-C always breaks it.
2728 * For the cmdline window: Alternate between ESC and
2729 * CTRL-C: ESC for most situations and CTRL-C to close the
2730 * cmdline window. */
2731 if (p_im && (State & INSERT))
2732 c = Ctrl_L;
2733 else if ((State & CMDLINE)
2734# ifdef FEAT_CMDWIN
2735 || (cmdwin_type > 0 && tc == ESC)
2736# endif
2737 )
2738 c = Ctrl_C;
2739 else
2740 c = ESC;
2741# ifdef FEAT_CMDWIN
2742 tc = c;
2743# endif
2744 break;
2745 }
2746#endif
2747
2748/*
2749 * get a character: 3. from the user - update display
2750 */
2751 /* In insert mode a screen update is skipped when characters
2752 * are still available. But when those available characters
2753 * are part of a mapping, and we are going to do a blocking
2754 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01002755 * changed text so far. Also for when 'lazyredraw' is set and
2756 * redrawing was postponed because there was something in the
2757 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01002758 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
2759 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 update_screen(0);
2762 setcursor(); /* put cursor back where it belongs */
2763 }
2764
2765 /*
2766 * If we have a partial match (and are going to wait for more
2767 * input from the user), show the partially matched characters
2768 * to the user with showcmd.
2769 */
2770#ifdef FEAT_CMDL_INFO
2771 i = 0;
2772#endif
2773 c1 = 0;
2774 if (typebuf.tb_len > 0 && advance && !exmode_active)
2775 {
2776 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2777 && State != HITRETURN)
2778 {
2779 /* this looks nice when typing a dead character map */
2780 if (State & INSERT
2781 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2782 + typebuf.tb_len - 1) == 1)
2783 {
2784 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2785 + typebuf.tb_len - 1], FALSE);
2786 setcursor(); /* put cursor back where it belongs */
2787 c1 = 1;
2788 }
2789#ifdef FEAT_CMDL_INFO
2790 /* need to use the col and row from above here */
2791 old_wcol = curwin->w_wcol;
2792 old_wrow = curwin->w_wrow;
2793 curwin->w_wcol = new_wcol;
2794 curwin->w_wrow = new_wrow;
2795 push_showcmd();
2796 if (typebuf.tb_len > SHOWCMD_COLS)
2797 i = typebuf.tb_len - SHOWCMD_COLS;
2798 while (i < typebuf.tb_len)
2799 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2800 + i++]);
2801 curwin->w_wcol = old_wcol;
2802 curwin->w_wrow = old_wrow;
2803#endif
2804 }
2805
2806 /* this looks nice when typing a dead character map */
2807 if ((State & CMDLINE)
2808#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2809 && cmdline_star == 0
2810#endif
2811 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2812 + typebuf.tb_len - 1) == 1)
2813 {
2814 putcmdline(typebuf.tb_buf[typebuf.tb_off
2815 + typebuf.tb_len - 1], FALSE);
2816 c1 = 1;
2817 }
2818 }
2819
2820/*
2821 * get a character: 3. from the user - get it
2822 */
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002823 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2825 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2826 !advance
2827 ? 0
2828 : ((typebuf.tb_len == 0
2829 || !(p_timeout || (p_ttimeout
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002830 && keylen == KEYLEN_PART_KEY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 ? -1L
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002832 : ((keylen == KEYLEN_PART_KEY && p_ttm >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 ? p_ttm
2834 : p_tm)), typebuf.tb_change_cnt);
2835
2836#ifdef FEAT_CMDL_INFO
2837 if (i != 0)
2838 pop_showcmd();
2839#endif
2840 if (c1 == 1)
2841 {
2842 if (State & INSERT)
2843 edit_unputchar();
2844 if (State & CMDLINE)
2845 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02002846 else
2847 setcursor(); /* put cursor back where it belongs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
2850 if (c < 0)
2851 continue; /* end of input script reached */
2852 if (c == NUL) /* no character available */
2853 {
2854 if (!advance)
2855 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002856 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 timedout = TRUE;
2859 continue;
2860 }
2861 }
2862 else
2863 { /* allow mapping for just typed characters */
2864 while (typebuf.tb_buf[typebuf.tb_off
2865 + typebuf.tb_len] != NUL)
2866 typebuf.tb_noremap[typebuf.tb_off
2867 + typebuf.tb_len++] = RM_YES;
2868#ifdef USE_IM_CONTROL
2869 /* Get IM status right after getting keys, not after the
2870 * timeout for a mapping (focus may be lost by then). */
2871 vgetc_im_active = im_get_status();
2872#endif
2873 }
2874 } /* for (;;) */
2875 } /* if (!character from stuffbuf) */
2876
2877 /* if advance is FALSE don't loop on NULs */
2878 } while (c < 0 || (advance && c == NUL));
2879
2880 /*
2881 * The "INSERT" message is taken care of here:
2882 * if we return an ESC to exit insert mode, the message is deleted
2883 * if we don't return an ESC but deleted the message before, redisplay it
2884 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002885 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002887 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
2889 if (typebuf.tb_len && !KeyTyped)
2890 redraw_cmdline = TRUE; /* delete mode later */
2891 else
2892 unshowmode(FALSE);
2893 }
2894 else if (c != ESC && mode_deleted)
2895 {
2896 if (typebuf.tb_len && !KeyTyped)
2897 redraw_cmdline = TRUE; /* show mode later */
2898 else
2899 showmode();
2900 }
2901 }
2902#ifdef FEAT_GUI
2903 /* may unshow different cursor shape */
2904 if (gui.in_use && shape_changed)
2905 gui_update_cursor(TRUE, FALSE);
2906#endif
2907
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002908 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 return c;
2911}
2912
2913/*
2914 * inchar() - get one character from
2915 * 1. a scriptfile
2916 * 2. the keyboard
2917 *
2918 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2919 * NUL terminated (buffer length must be 'maxlen' + 1).
2920 * Minimum for "maxlen" is 3!!!!
2921 *
2922 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2923 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2924 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2925 * otherwise.
2926 *
2927 * If we got an interrupt all input is read until none is available.
2928 *
2929 * If wait_time == 0 there is no waiting for the char.
2930 * If wait_time == n we wait for n msec for a character to arrive.
2931 * If wait_time == -1 we wait forever for a character to arrive.
2932 *
2933 * Return the number of obtained characters.
2934 * Return -1 when end of input script reached.
2935 */
2936 int
2937inchar(buf, maxlen, wait_time, tb_change_cnt)
2938 char_u *buf;
2939 int maxlen;
2940 long wait_time; /* milli seconds */
2941 int tb_change_cnt;
2942{
2943 int len = 0; /* init for GCC */
2944 int retesc = FALSE; /* return ESC with gotint */
2945 int script_char;
2946
2947 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2948 {
2949 cursor_on();
2950 out_flush();
2951#ifdef FEAT_GUI
2952 if (gui.in_use)
2953 {
2954 gui_update_cursor(FALSE, FALSE);
2955# ifdef FEAT_MOUSESHAPE
2956 if (postponed_mouseshape)
2957 update_mouseshape(-1);
2958# endif
2959 }
2960#endif
2961 }
2962
2963 /*
2964 * Don't reset these when at the hit-return prompt, otherwise a endless
2965 * recursive loop may result (write error in swapfile, hit-return, timeout
2966 * on char wait, flush swapfile, write error....).
2967 */
2968 if (State != HITRETURN)
2969 {
2970 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2971 did_swapwrite_msg = FALSE; /* display swap file write error again */
2972 }
2973 undo_off = FALSE; /* restart undo now */
2974
2975 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002976 * Get a character from a script file if there is one.
2977 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 */
2979 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002980 while (scriptin[curscript] != NULL && script_char < 0
2981#ifdef FEAT_EVAL
2982 && !ignore_script
2983#endif
2984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002986
2987#if defined(FEAT_NETBEANS_INTG)
2988 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002989 netbeans_parse_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00002990#endif
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
2993 {
2994 /* Reached EOF.
2995 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
2996 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
2997 closescript();
2998 /*
2999 * When reading script file is interrupted, return an ESC to get
3000 * back to normal mode.
3001 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3002 */
3003 if (got_int)
3004 retesc = TRUE;
3005 else
3006 return -1;
3007 }
3008 else
3009 {
3010 buf[0] = script_char;
3011 len = 1;
3012 }
3013 }
3014
3015 if (script_char < 0) /* did not get a character from script */
3016 {
3017 /*
3018 * If we got an interrupt, skip all previously typed characters and
3019 * return TRUE if quit reading script file.
3020 * Stop reading typeahead when a single CTRL-C was read,
3021 * fill_input_buf() returns this when not able to read from stdin.
3022 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3023 * and buf may be pointing inside typebuf.tb_buf[].
3024 */
3025 if (got_int)
3026 {
3027#define DUM_LEN MAXMAPLEN * 3 + 3
3028 char_u dum[DUM_LEN + 1];
3029
3030 for (;;)
3031 {
3032 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3033 if (len == 0 || (len == 1 && dum[0] == 3))
3034 break;
3035 }
3036 return retesc;
3037 }
3038
3039 /*
3040 * Always flush the output characters when getting input characters
3041 * from the user.
3042 */
3043 out_flush();
3044
3045 /*
3046 * Fill up to a third of the buffer, because each character may be
3047 * tripled below.
3048 */
3049 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3050 }
3051
3052 if (typebuf_changed(tb_change_cnt))
3053 return 0;
3054
3055 return fix_input_buffer(buf, len, script_char >= 0);
3056}
3057
3058/*
3059 * Fix typed characters for use by vgetc() and check_termcode().
3060 * buf[] must have room to triple the number of bytes!
3061 * Returns the new length.
3062 */
3063 int
3064fix_input_buffer(buf, len, script)
3065 char_u *buf;
3066 int len;
3067 int script; /* TRUE when reading from a script */
3068{
3069 int i;
3070 char_u *p = buf;
3071
3072 /*
3073 * Two characters are special: NUL and K_SPECIAL.
3074 * When compiled With the GUI CSI is also special.
3075 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3076 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3077 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
3078 * Don't replace K_SPECIAL when reading a script file.
3079 */
3080 for (i = len; --i >= 0; ++p)
3081 {
3082#ifdef FEAT_GUI
3083 /* When the GUI is used any character can come after a CSI, don't
3084 * escape it. */
3085 if (gui.in_use && p[0] == CSI && i >= 2)
3086 {
3087 p += 2;
3088 i -= 2;
3089 }
3090 /* When the GUI is not used CSI needs to be escaped. */
3091 else if (!gui.in_use && p[0] == CSI)
3092 {
3093 mch_memmove(p + 3, p + 1, (size_t)i);
3094 *p++ = K_SPECIAL;
3095 *p++ = KS_EXTRA;
3096 *p = (int)KE_CSI;
3097 len += 2;
3098 }
3099 else
3100#endif
3101 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003102#ifdef FEAT_AUTOCMD
3103 /* timeout may generate K_CURSORHOLD */
3104 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
3105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106#if defined(WIN3264) && !defined(FEAT_GUI)
3107 /* Win32 console passes modifiers */
3108 && (i < 2 || p[1] != KS_MODIFIER)
3109#endif
3110 ))
3111 {
3112 mch_memmove(p + 3, p + 1, (size_t)i);
3113 p[2] = K_THIRD(p[0]);
3114 p[1] = K_SECOND(p[0]);
3115 p[0] = K_SPECIAL;
3116 p += 2;
3117 len += 2;
3118 }
3119 }
3120 *p = NUL; /* add trailing NUL */
3121 return len;
3122}
3123
3124#if defined(USE_INPUT_BUF) || defined(PROTO)
3125/*
3126 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3127 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003128 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003129 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 */
3131 int
3132input_available()
3133{
3134 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003135# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3136 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137# endif
3138 );
3139}
3140#endif
3141
3142/*
3143 * map[!] : show all key mappings
3144 * map[!] {lhs} : show key mapping for {lhs}
3145 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3146 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3147 * unmap[!] {lhs} : remove key mapping for {lhs}
3148 * abbr : show all abbreviations
3149 * abbr {lhs} : show abbreviations for {lhs}
3150 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3151 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3152 * unabbr {lhs} : remove abbreviation for {lhs}
3153 *
3154 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3155 *
3156 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3157 * it will be modified.
3158 *
Bram Moolenaar371d5402006-03-20 21:47:49 +00003159 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 * for :map! mode is INSERT + CMDLINE
3161 * for :cmap mode is CMDLINE
3162 * for :imap mode is INSERT
3163 * for :lmap mode is LANGMAP
3164 * for :nmap mode is NORMAL
Bram Moolenaar371d5402006-03-20 21:47:49 +00003165 * for :vmap mode is VISUAL + SELECTMODE
3166 * for :xmap mode is VISUAL
3167 * for :smap mode is SELECTMODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 * for :omap mode is OP_PENDING
3169 *
3170 * for :abbr mode is INSERT + CMDLINE
3171 * for :iabbr mode is INSERT
3172 * for :cabbr mode is CMDLINE
3173 *
3174 * Return 0 for success
3175 * 1 for invalid arguments
3176 * 2 for no match
3177 * 4 for out of mem
3178 * 5 for entry not unique
3179 */
3180 int
3181do_map(maptype, arg, mode, abbrev)
3182 int maptype;
3183 char_u *arg;
3184 int mode;
3185 int abbrev; /* not a mapping but an abbreviation */
3186{
3187 char_u *keys;
3188 mapblock_T *mp, **mpp;
3189 char_u *rhs;
3190 char_u *p;
3191 int n;
3192 int len = 0; /* init for GCC */
3193 char_u *newstr;
3194 int hasarg;
3195 int haskey;
3196 int did_it = FALSE;
3197#ifdef FEAT_LOCALMAP
3198 int did_local = FALSE;
3199#endif
3200 int round;
3201 char_u *keys_buf = NULL;
3202 char_u *arg_buf = NULL;
3203 int retval = 0;
3204 int do_backslash;
3205 int hash;
3206 int new_hash;
3207 mapblock_T **abbr_table;
3208 mapblock_T **map_table;
3209 int unique = FALSE;
3210 int silent = FALSE;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003211 int special = FALSE;
Bram Moolenaar4e427192006-03-10 21:34:27 +00003212#ifdef FEAT_EVAL
3213 int expr = FALSE;
3214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int noremap;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003216 char_u *orig_rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 keys = arg;
3219 map_table = maphash;
3220 abbr_table = &first_abbr;
3221
3222 /* For ":noremap" don't remap, otherwise do remap. */
3223 if (maptype == 2)
3224 noremap = REMAP_NONE;
3225 else
3226 noremap = REMAP_YES;
3227
Bram Moolenaar4e427192006-03-10 21:34:27 +00003228 /* Accept <buffer>, <silent>, <expr> <script> and <unique> in any order. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 for (;;)
3230 {
3231#ifdef FEAT_LOCALMAP
3232 /*
3233 * Check for "<buffer>": mapping local to buffer.
3234 */
3235 if (STRNCMP(keys, "<buffer>", 8) == 0)
3236 {
3237 keys = skipwhite(keys + 8);
3238 map_table = curbuf->b_maphash;
3239 abbr_table = &curbuf->b_first_abbr;
3240 continue;
3241 }
3242#endif
3243
3244 /*
3245 * Check for "<silent>": don't echo commands.
3246 */
3247 if (STRNCMP(keys, "<silent>", 8) == 0)
3248 {
3249 keys = skipwhite(keys + 8);
3250 silent = TRUE;
3251 continue;
3252 }
3253
Bram Moolenaar9c102382006-05-03 21:26:49 +00003254 /*
3255 * Check for "<special>": accept special keys in <>
3256 */
3257 if (STRNCMP(keys, "<special>", 9) == 0)
3258 {
3259 keys = skipwhite(keys + 9);
3260 special = TRUE;
3261 continue;
3262 }
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#ifdef FEAT_EVAL
3265 /*
3266 * Check for "<script>": remap script-local mappings only
3267 */
3268 if (STRNCMP(keys, "<script>", 8) == 0)
3269 {
3270 keys = skipwhite(keys + 8);
3271 noremap = REMAP_SCRIPT;
3272 continue;
3273 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00003274
3275 /*
3276 * Check for "<expr>": {rhs} is an expression.
3277 */
3278 if (STRNCMP(keys, "<expr>", 6) == 0)
3279 {
3280 keys = skipwhite(keys + 6);
3281 expr = TRUE;
3282 continue;
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#endif
3285 /*
3286 * Check for "<unique>": don't overwrite an existing mapping.
3287 */
3288 if (STRNCMP(keys, "<unique>", 8) == 0)
3289 {
3290 keys = skipwhite(keys + 8);
3291 unique = TRUE;
3292 continue;
3293 }
3294 break;
3295 }
3296
3297 validate_maphash();
3298
3299 /*
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003300 * Find end of keys and skip CTRL-Vs (and backslashes) in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003302 * with :unmap white space is included in the keys, no argument possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 */
3304 p = keys;
3305 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3306 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3307 {
3308 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3309 p[1] != NUL)
3310 ++p; /* skip CTRL-V or backslash */
3311 ++p;
3312 }
3313 if (*p != NUL)
3314 *p++ = NUL;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 p = skipwhite(p);
3317 rhs = p;
3318 hasarg = (*rhs != NUL);
3319 haskey = (*keys != NUL);
3320
3321 /* check for :unmap without argument */
3322 if (maptype == 1 && !haskey)
3323 {
3324 retval = 1;
3325 goto theend;
3326 }
3327
3328 /*
3329 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3330 * with the appropriate two bytes. If it is a shifted special key, unshift
3331 * it too, giving another two bytes.
3332 * replace_termcodes() may move the result to allocated memory, which
3333 * needs to be freed later (*keys_buf and *arg_buf).
3334 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3335 */
3336 if (haskey)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003337 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
Bram Moolenaarb3ae56c2010-10-27 17:39:05 +02003338 orig_rhs = rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (hasarg)
3340 {
3341 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3342 rhs = (char_u *)"";
3343 else
Bram Moolenaar9c102382006-05-03 21:26:49 +00003344 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
3347#ifdef FEAT_FKMAP
3348 /*
Bram Moolenaarbd743252010-10-20 21:23:33 +02003349 * When in right-to-left mode and alternate keymap option set,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * reverse the character flow in the rhs in Farsi.
3351 */
3352 if (p_altkeymap && curwin->w_p_rl)
3353 lrswap(rhs);
3354#endif
3355
3356 /*
3357 * check arguments and translate function keys
3358 */
3359 if (haskey)
3360 {
3361 len = (int)STRLEN(keys);
3362 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3363 {
3364 retval = 1;
3365 goto theend;
3366 }
3367
3368 if (abbrev && maptype != 1)
3369 {
3370 /*
3371 * If an abbreviation ends in a keyword character, the
3372 * rest must be all keyword-char or all non-keyword-char.
3373 * Otherwise we won't be able to find the start of it in a
3374 * vi-compatible way.
3375 */
3376#ifdef FEAT_MBYTE
3377 if (has_mbyte)
3378 {
3379 int first, last;
3380 int same = -1;
3381
3382 first = vim_iswordp(keys);
3383 last = first;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003384 p = keys + (*mb_ptr2len)(keys);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 n = 1;
3386 while (p < keys + len)
3387 {
3388 ++n; /* nr of (multi-byte) chars */
3389 last = vim_iswordp(p); /* type of last char */
3390 if (same == -1 && last != first)
3391 same = n - 1; /* count of same char type */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394 if (last && n > 2 && same >= 0 && same < n - 1)
3395 {
3396 retval = 1;
3397 goto theend;
3398 }
3399 }
3400 else
3401#endif
3402 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3403 for (n = 0; n < len - 2; ++n)
3404 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3405 {
3406 retval = 1;
3407 goto theend;
3408 }
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00003409 /* An abbreviation cannot contain white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 for (n = 0; n < len; ++n)
3411 if (vim_iswhite(keys[n]))
3412 {
3413 retval = 1;
3414 goto theend;
3415 }
3416 }
3417 }
3418
3419 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3420 no_abbr = FALSE; /* reset flag that indicates there are
3421 no abbreviations */
3422
3423 if (!haskey || (maptype != 1 && !hasarg))
3424 msg_start();
3425
3426#ifdef FEAT_LOCALMAP
3427 /*
3428 * Check if a new local mapping wasn't already defined globally.
3429 */
3430 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3431 {
3432 /* need to loop over all global hash lists */
3433 for (hash = 0; hash < 256 && !got_int; ++hash)
3434 {
3435 if (abbrev)
3436 {
3437 if (hash != 0) /* there is only one abbreviation list */
3438 break;
3439 mp = first_abbr;
3440 }
3441 else
3442 mp = maphash[hash];
3443 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3444 {
3445 /* check entries with the same mode */
3446 if ((mp->m_mode & mode) != 0
3447 && mp->m_keylen == len
3448 && unique
3449 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3450 {
3451 if (abbrev)
3452 EMSG2(_("E224: global abbreviation already exists for %s"),
3453 mp->m_keys);
3454 else
3455 EMSG2(_("E225: global mapping already exists for %s"),
3456 mp->m_keys);
3457 retval = 5;
3458 goto theend;
3459 }
3460 }
3461 }
3462 }
3463
3464 /*
3465 * When listing global mappings, also list buffer-local ones here.
3466 */
3467 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3468 {
3469 /* need to loop over all global hash lists */
3470 for (hash = 0; hash < 256 && !got_int; ++hash)
3471 {
3472 if (abbrev)
3473 {
3474 if (hash != 0) /* there is only one abbreviation list */
3475 break;
3476 mp = curbuf->b_first_abbr;
3477 }
3478 else
3479 mp = curbuf->b_maphash[hash];
3480 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3481 {
3482 /* check entries with the same mode */
3483 if ((mp->m_mode & mode) != 0)
3484 {
3485 if (!haskey) /* show all entries */
3486 {
3487 showmap(mp, TRUE);
3488 did_local = TRUE;
3489 }
3490 else
3491 {
3492 n = mp->m_keylen;
3493 if (STRNCMP(mp->m_keys, keys,
3494 (size_t)(n < len ? n : len)) == 0)
3495 {
3496 showmap(mp, TRUE);
3497 did_local = TRUE;
3498 }
3499 }
3500 }
3501 }
3502 }
3503 }
3504#endif
3505
3506 /*
3507 * Find an entry in the maphash[] list that matches.
3508 * For :unmap we may loop two times: once to try to unmap an entry with a
3509 * matching 'from' part, a second time, if the first fails, to unmap an
3510 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3511 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3512 * "bar" because of the abbreviation.
3513 */
3514 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3515 && !did_it && !got_int; ++round)
3516 {
3517 /* need to loop over all hash lists */
3518 for (hash = 0; hash < 256 && !got_int; ++hash)
3519 {
3520 if (abbrev)
3521 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003522 if (hash > 0) /* there is only one abbreviation list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 break;
3524 mpp = abbr_table;
3525 }
3526 else
3527 mpp = &(map_table[hash]);
3528 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3529 {
3530
3531 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3532 {
3533 mpp = &(mp->m_next);
3534 continue;
3535 }
3536 if (!haskey) /* show all entries */
3537 {
3538 showmap(mp, map_table != maphash);
3539 did_it = TRUE;
3540 }
3541 else /* do we have a match? */
3542 {
3543 if (round) /* second round: Try unmap "rhs" string */
3544 {
3545 n = (int)STRLEN(mp->m_str);
3546 p = mp->m_str;
3547 }
3548 else
3549 {
3550 n = mp->m_keylen;
3551 p = mp->m_keys;
3552 }
3553 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3554 {
3555 if (maptype == 1) /* delete entry */
3556 {
3557 /* Only accept a full match. For abbreviations we
3558 * ignore trailing space when matching with the
3559 * "lhs", since an abbreviation can't have
3560 * trailing space. */
3561 if (n != len && (!abbrev || round || n > len
3562 || *skipwhite(keys + n) != NUL))
3563 {
3564 mpp = &(mp->m_next);
3565 continue;
3566 }
3567 /*
3568 * We reset the indicated mode bits. If nothing is
3569 * left the entry is deleted below.
3570 */
3571 mp->m_mode &= ~mode;
3572 did_it = TRUE; /* remember we did something */
3573 }
3574 else if (!hasarg) /* show matching entry */
3575 {
3576 showmap(mp, map_table != maphash);
3577 did_it = TRUE;
3578 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00003579 else if (n != len) /* new entry is ambiguous */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 mpp = &(mp->m_next);
3582 continue;
3583 }
3584 else if (unique)
3585 {
3586 if (abbrev)
3587 EMSG2(_("E226: abbreviation already exists for %s"),
3588 p);
3589 else
3590 EMSG2(_("E227: mapping already exists for %s"), p);
3591 retval = 5;
3592 goto theend;
3593 }
3594 else /* new rhs for existing entry */
3595 {
3596 mp->m_mode &= ~mode; /* remove mode bits */
3597 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3598 {
3599 newstr = vim_strsave(rhs);
3600 if (newstr == NULL)
3601 {
3602 retval = 4; /* no mem */
3603 goto theend;
3604 }
3605 vim_free(mp->m_str);
3606 mp->m_str = newstr;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003607 vim_free(mp->m_orig_str);
3608 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 mp->m_noremap = noremap;
3610 mp->m_silent = silent;
3611 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003612#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003613 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003614 mp->m_script_ID = current_SID;
3615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 did_it = TRUE;
3617 }
3618 }
3619 if (mp->m_mode == 0) /* entry can be deleted */
3620 {
3621 map_free(mpp);
3622 continue; /* continue with *mpp */
3623 }
3624
3625 /*
3626 * May need to put this entry into another hash list.
3627 */
3628 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3629 if (!abbrev && new_hash != hash)
3630 {
3631 *mpp = mp->m_next;
3632 mp->m_next = map_table[new_hash];
3633 map_table[new_hash] = mp;
3634
3635 continue; /* continue with *mpp */
3636 }
3637 }
3638 }
3639 mpp = &(mp->m_next);
3640 }
3641 }
3642 }
3643
3644 if (maptype == 1) /* delete entry */
3645 {
3646 if (!did_it)
3647 retval = 2; /* no match */
3648 goto theend;
3649 }
3650
3651 if (!haskey || !hasarg) /* print entries */
3652 {
3653 if (!did_it
3654#ifdef FEAT_LOCALMAP
3655 && !did_local
3656#endif
3657 )
3658 {
3659 if (abbrev)
3660 MSG(_("No abbreviation found"));
3661 else
3662 MSG(_("No mapping found"));
3663 }
3664 goto theend; /* listing finished */
3665 }
3666
3667 if (did_it) /* have added the new entry already */
3668 goto theend;
3669
3670 /*
3671 * Get here when adding a new entry to the maphash[] list or abbrlist.
3672 */
3673 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3674 if (mp == NULL)
3675 {
3676 retval = 4; /* no mem */
3677 goto theend;
3678 }
3679
3680 /* If CTRL-C has been mapped, don't always use it for Interrupting */
3681 if (*keys == Ctrl_C)
3682 mapped_ctrl_c = TRUE;
3683
3684 mp->m_keys = vim_strsave(keys);
3685 mp->m_str = vim_strsave(rhs);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003686 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (mp->m_keys == NULL || mp->m_str == NULL)
3688 {
3689 vim_free(mp->m_keys);
3690 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003691 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 vim_free(mp);
3693 retval = 4; /* no mem */
3694 goto theend;
3695 }
3696 mp->m_keylen = (int)STRLEN(mp->m_keys);
3697 mp->m_noremap = noremap;
3698 mp->m_silent = silent;
3699 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003700#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003701 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003702 mp->m_script_ID = current_SID;
3703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 /* add the new entry in front of the abbrlist or maphash[] list */
3706 if (abbrev)
3707 {
3708 mp->m_next = *abbr_table;
3709 *abbr_table = mp;
3710 }
3711 else
3712 {
3713 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3714 mp->m_next = map_table[n];
3715 map_table[n] = mp;
3716 }
3717
3718theend:
3719 vim_free(keys_buf);
3720 vim_free(arg_buf);
3721 return retval;
3722}
3723
3724/*
3725 * Delete one entry from the abbrlist or maphash[].
3726 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3727 */
3728 static void
3729map_free(mpp)
3730 mapblock_T **mpp;
3731{
3732 mapblock_T *mp;
3733
3734 mp = *mpp;
3735 vim_free(mp->m_keys);
3736 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003737 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 *mpp = mp->m_next;
3739 vim_free(mp);
3740}
3741
3742/*
3743 * Initialize maphash[] for first use.
3744 */
3745 static void
3746validate_maphash()
3747{
3748 if (!maphash_valid)
3749 {
3750 vim_memset(maphash, 0, sizeof(maphash));
3751 maphash_valid = TRUE;
3752 }
3753}
3754
3755/*
3756 * Get the mapping mode from the command name.
3757 */
3758 int
3759get_map_mode(cmdp, forceit)
3760 char_u **cmdp;
3761 int forceit;
3762{
3763 char_u *p;
3764 int modec;
3765 int mode;
3766
3767 p = *cmdp;
3768 modec = *p++;
3769 if (modec == 'i')
3770 mode = INSERT; /* :imap */
3771 else if (modec == 'l')
3772 mode = LANGMAP; /* :lmap */
3773 else if (modec == 'c')
3774 mode = CMDLINE; /* :cmap */
3775 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3776 mode = NORMAL; /* :nmap */
3777 else if (modec == 'v')
Bram Moolenaar371d5402006-03-20 21:47:49 +00003778 mode = VISUAL + SELECTMODE; /* :vmap */
3779 else if (modec == 'x')
3780 mode = VISUAL; /* :xmap */
3781 else if (modec == 's')
3782 mode = SELECTMODE; /* :smap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 else if (modec == 'o')
3784 mode = OP_PENDING; /* :omap */
3785 else
3786 {
3787 --p;
3788 if (forceit)
3789 mode = INSERT + CMDLINE; /* :map ! */
3790 else
Bram Moolenaar371d5402006-03-20 21:47:49 +00003791 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793
3794 *cmdp = p;
3795 return mode;
3796}
3797
3798/*
3799 * Clear all mappings or abbreviations.
3800 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 void
3803map_clear(cmdp, arg, forceit, abbr)
3804 char_u *cmdp;
Bram Moolenaard31aca22009-07-14 11:44:30 +00003805 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 int forceit;
3807 int abbr;
3808{
3809 int mode;
3810#ifdef FEAT_LOCALMAP
3811 int local;
3812
3813 local = (STRCMP(arg, "<buffer>") == 0);
3814 if (!local && *arg != NUL)
3815 {
3816 EMSG(_(e_invarg));
3817 return;
3818 }
3819#endif
3820
3821 mode = get_map_mode(&cmdp, forceit);
3822 map_clear_int(curbuf, mode,
3823#ifdef FEAT_LOCALMAP
3824 local,
3825#else
3826 FALSE,
3827#endif
3828 abbr);
3829}
3830
3831/*
3832 * Clear all mappings in "mode".
3833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 void
3835map_clear_int(buf, mode, local, abbr)
Bram Moolenaard31aca22009-07-14 11:44:30 +00003836 buf_T *buf UNUSED; /* buffer for local mappings */
3837 int mode; /* mode in which to delete */
3838 int local UNUSED; /* TRUE for buffer-local mappings */
3839 int abbr; /* TRUE for abbreviations */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
3841 mapblock_T *mp, **mpp;
3842 int hash;
3843 int new_hash;
3844
3845 validate_maphash();
3846
3847 for (hash = 0; hash < 256; ++hash)
3848 {
3849 if (abbr)
3850 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003851 if (hash > 0) /* there is only one abbrlist */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 break;
3853#ifdef FEAT_LOCALMAP
3854 if (local)
3855 mpp = &buf->b_first_abbr;
3856 else
3857#endif
3858 mpp = &first_abbr;
3859 }
3860 else
3861 {
3862#ifdef FEAT_LOCALMAP
3863 if (local)
3864 mpp = &buf->b_maphash[hash];
3865 else
3866#endif
3867 mpp = &maphash[hash];
3868 }
3869 while (*mpp != NULL)
3870 {
3871 mp = *mpp;
3872 if (mp->m_mode & mode)
3873 {
3874 mp->m_mode &= ~mode;
3875 if (mp->m_mode == 0) /* entry can be deleted */
3876 {
3877 map_free(mpp);
3878 continue;
3879 }
3880 /*
3881 * May need to put this entry into another hash list.
3882 */
3883 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3884 if (!abbr && new_hash != hash)
3885 {
3886 *mpp = mp->m_next;
3887#ifdef FEAT_LOCALMAP
3888 if (local)
3889 {
3890 mp->m_next = buf->b_maphash[new_hash];
3891 buf->b_maphash[new_hash] = mp;
3892 }
3893 else
3894#endif
3895 {
3896 mp->m_next = maphash[new_hash];
3897 maphash[new_hash] = mp;
3898 }
3899 continue; /* continue with *mpp */
3900 }
3901 }
3902 mpp = &(mp->m_next);
3903 }
3904 }
3905}
3906
Bram Moolenaarbd743252010-10-20 21:23:33 +02003907/*
3908 * Return characters to represent the map mode in an allocated string.
3909 * Returns NULL when out of memory.
3910 */
3911 char_u *
3912map_mode_to_chars(mode)
3913 int mode;
3914{
3915 garray_T mapmode;
3916
3917 ga_init2(&mapmode, 1, 7);
3918
3919 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3920 ga_append(&mapmode, '!'); /* :map! */
3921 else if (mode & INSERT)
3922 ga_append(&mapmode, 'i'); /* :imap */
3923 else if (mode & LANGMAP)
3924 ga_append(&mapmode, 'l'); /* :lmap */
3925 else if (mode & CMDLINE)
3926 ga_append(&mapmode, 'c'); /* :cmap */
3927 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3928 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3929 ga_append(&mapmode, ' '); /* :map */
3930 else
3931 {
3932 if (mode & NORMAL)
3933 ga_append(&mapmode, 'n'); /* :nmap */
3934 if (mode & OP_PENDING)
3935 ga_append(&mapmode, 'o'); /* :omap */
3936 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3937 ga_append(&mapmode, 'v'); /* :vmap */
3938 else
3939 {
3940 if (mode & VISUAL)
3941 ga_append(&mapmode, 'x'); /* :xmap */
3942 if (mode & SELECTMODE)
3943 ga_append(&mapmode, 's'); /* :smap */
3944 }
3945 }
3946
3947 ga_append(&mapmode, NUL);
3948 return (char_u *)mapmode.ga_data;
3949}
3950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 static void
3952showmap(mp, local)
3953 mapblock_T *mp;
3954 int local; /* TRUE for buffer-local map */
3955{
Bram Moolenaarbd743252010-10-20 21:23:33 +02003956 int len = 1;
3957 char_u *mapchars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 if (msg_didout || msg_silent != 0)
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 msg_putchar('\n');
Bram Moolenaarfa47a922009-02-22 22:43:27 +00003962 if (got_int) /* 'q' typed at MORE prompt */
3963 return;
3964 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003965
3966 mapchars = map_mode_to_chars(mp->m_mode);
3967 if (mapchars != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
Bram Moolenaarbd743252010-10-20 21:23:33 +02003969 msg_puts(mapchars);
Bram Moolenaar6b5ef062010-10-27 12:18:00 +02003970 len = (int)STRLEN(mapchars);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003971 vim_free(mapchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02003973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 while (++len <= 3)
3975 msg_putchar(' ');
3976
Bram Moolenaarf2330482008-06-24 20:19:36 +00003977 /* Display the LHS. Get length of what we write. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 len = msg_outtrans_special(mp->m_keys, TRUE);
3979 do
3980 {
3981 msg_putchar(' '); /* padd with blanks */
3982 ++len;
3983 } while (len < 12);
3984
3985 if (mp->m_noremap == REMAP_NONE)
3986 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
3987 else if (mp->m_noremap == REMAP_SCRIPT)
3988 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
3989 else
3990 msg_putchar(' ');
3991
3992 if (local)
3993 msg_putchar('@');
3994 else
3995 msg_putchar(' ');
3996
3997 /* Use FALSE below if we only want things like <Up> to show up as such on
Bram Moolenaarbd743252010-10-20 21:23:33 +02003998 * the rhs, and not M-x etc, TRUE gets both -- webb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (*mp->m_str == NUL)
4000 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
4001 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02004002 {
4003 /* Remove escaping of CSI, because "m_str" is in a format to be used
4004 * as typeahead. */
4005 char_u *s = vim_strsave(mp->m_str);
4006 if (s != NULL)
4007 {
4008 vim_unescape_csi(s);
4009 msg_outtrans_special(s, FALSE);
4010 vim_free(s);
4011 }
4012 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004013#ifdef FEAT_EVAL
4014 if (p_verbose > 0)
4015 last_set_msg(mp->m_script_ID);
4016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 out_flush(); /* show one line at a time */
4018}
4019
4020#if defined(FEAT_EVAL) || defined(PROTO)
4021/*
4022 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
4023 * Recognize termcap codes in "str".
4024 * Also checks mappings local to the current buffer.
4025 */
4026 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004027map_to_exists(str, modechars, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *str;
4029 char_u *modechars;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004030 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 int mode = 0;
4033 char_u *rhs;
4034 char_u *buf;
4035 int retval;
4036
Bram Moolenaar9c102382006-05-03 21:26:49 +00004037 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 if (vim_strchr(modechars, 'n') != NULL)
4040 mode |= NORMAL;
4041 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004042 mode |= VISUAL + SELECTMODE;
4043 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 mode |= VISUAL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004045 if (vim_strchr(modechars, 's') != NULL)
4046 mode |= SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (vim_strchr(modechars, 'o') != NULL)
4048 mode |= OP_PENDING;
4049 if (vim_strchr(modechars, 'i') != NULL)
4050 mode |= INSERT;
4051 if (vim_strchr(modechars, 'l') != NULL)
4052 mode |= LANGMAP;
4053 if (vim_strchr(modechars, 'c') != NULL)
4054 mode |= CMDLINE;
4055
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004056 retval = map_to_exists_mode(rhs, mode, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 vim_free(buf);
4058
4059 return retval;
4060}
4061#endif
4062
4063/*
4064 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
4065 * Also checks mappings local to the current buffer.
4066 */
4067 int
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004068map_to_exists_mode(rhs, mode, abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 char_u *rhs;
4070 int mode;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004071 int abbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 mapblock_T *mp;
4074 int hash;
4075# ifdef FEAT_LOCALMAP
4076 int expand_buffer = FALSE;
4077
4078 validate_maphash();
4079
4080 /* Do it twice: once for global maps and once for local maps. */
4081 for (;;)
4082 {
4083# endif
4084 for (hash = 0; hash < 256; ++hash)
4085 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004086 if (abbr)
4087 {
4088 if (hash > 0) /* there is only one abbr list */
4089 break;
4090#ifdef FEAT_LOCALMAP
4091 if (expand_buffer)
4092 mp = curbuf->b_first_abbr;
4093 else
4094#endif
4095 mp = first_abbr;
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097# ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004098 else if (expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 mp = curbuf->b_maphash[hash];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100# endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 mp = maphash[hash];
4103 for (; mp; mp = mp->m_next)
4104 {
4105 if ((mp->m_mode & mode)
4106 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
4107 return TRUE;
4108 }
4109 }
4110# ifdef FEAT_LOCALMAP
4111 if (expand_buffer)
4112 break;
4113 expand_buffer = TRUE;
4114 }
4115# endif
4116
4117 return FALSE;
4118}
4119
4120#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4121/*
4122 * Used below when expanding mapping/abbreviation names.
4123 */
4124static int expand_mapmodes = 0;
4125static int expand_isabbrev = 0;
4126#ifdef FEAT_LOCALMAP
4127static int expand_buffer = FALSE;
4128#endif
4129
4130/*
4131 * Work out what to complete when doing command line completion of mapping
4132 * or abbreviation names.
4133 */
4134 char_u *
4135set_context_in_map_cmd(xp, cmd, arg, forceit, isabbrev, isunmap, cmdidx)
4136 expand_T *xp;
4137 char_u *cmd;
4138 char_u *arg;
4139 int forceit; /* TRUE if '!' given */
4140 int isabbrev; /* TRUE if abbreviation */
4141 int isunmap; /* TRUE if unmap/unabbrev command */
4142 cmdidx_T cmdidx;
4143{
4144 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
4145 xp->xp_context = EXPAND_NOTHING;
4146 else
4147 {
4148 if (isunmap)
4149 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
4150 else
4151 {
4152 expand_mapmodes = INSERT + CMDLINE;
4153 if (!isabbrev)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004154 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 expand_isabbrev = isabbrev;
4157 xp->xp_context = EXPAND_MAPPINGS;
4158#ifdef FEAT_LOCALMAP
4159 expand_buffer = FALSE;
4160#endif
4161 for (;;)
4162 {
4163#ifdef FEAT_LOCALMAP
4164 if (STRNCMP(arg, "<buffer>", 8) == 0)
4165 {
4166 expand_buffer = TRUE;
4167 arg = skipwhite(arg + 8);
4168 continue;
4169 }
4170#endif
4171 if (STRNCMP(arg, "<unique>", 8) == 0)
4172 {
4173 arg = skipwhite(arg + 8);
4174 continue;
4175 }
4176 if (STRNCMP(arg, "<silent>", 8) == 0)
4177 {
4178 arg = skipwhite(arg + 8);
4179 continue;
4180 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004181#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (STRNCMP(arg, "<script>", 8) == 0)
4183 {
4184 arg = skipwhite(arg + 8);
4185 continue;
4186 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004187 if (STRNCMP(arg, "<expr>", 6) == 0)
4188 {
4189 arg = skipwhite(arg + 6);
4190 continue;
4191 }
4192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 break;
4194 }
4195 xp->xp_pattern = arg;
4196 }
4197
4198 return NULL;
4199}
4200
4201/*
4202 * Find all mapping/abbreviation names that match regexp 'prog'.
4203 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4204 * Return OK if matches found, FAIL otherwise.
4205 */
4206 int
4207ExpandMappings(regmatch, num_file, file)
4208 regmatch_T *regmatch;
4209 int *num_file;
4210 char_u ***file;
4211{
4212 mapblock_T *mp;
4213 int hash;
4214 int count;
4215 int round;
4216 char_u *p;
4217 int i;
4218
4219 validate_maphash();
4220
4221 *num_file = 0; /* return values in case of FAIL */
4222 *file = NULL;
4223
4224 /*
4225 * round == 1: Count the matches.
4226 * round == 2: Build the array to keep the matches.
4227 */
4228 for (round = 1; round <= 2; ++round)
4229 {
4230 count = 0;
4231
Bram Moolenaar4e427192006-03-10 21:34:27 +00004232 for (i = 0; i < 5; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
4234 if (i == 0)
4235 p = (char_u *)"<silent>";
4236 else if (i == 1)
4237 p = (char_u *)"<unique>";
4238#ifdef FEAT_EVAL
4239 else if (i == 2)
4240 p = (char_u *)"<script>";
Bram Moolenaar4e427192006-03-10 21:34:27 +00004241 else if (i == 3)
4242 p = (char_u *)"<expr>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#endif
4244#ifdef FEAT_LOCALMAP
Bram Moolenaar4e427192006-03-10 21:34:27 +00004245 else if (i == 4 && !expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 p = (char_u *)"<buffer>";
4247#endif
4248 else
4249 continue;
4250
4251 if (vim_regexec(regmatch, p, (colnr_T)0))
4252 {
4253 if (round == 1)
4254 ++count;
4255 else
4256 (*file)[count++] = vim_strsave(p);
4257 }
4258 }
4259
4260 for (hash = 0; hash < 256; ++hash)
4261 {
4262 if (expand_isabbrev)
4263 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004264 if (hash > 0) /* only one abbrev list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 break; /* for (hash) */
4266 mp = first_abbr;
4267 }
4268#ifdef FEAT_LOCALMAP
4269 else if (expand_buffer)
4270 mp = curbuf->b_maphash[hash];
4271#endif
4272 else
4273 mp = maphash[hash];
4274 for (; mp; mp = mp->m_next)
4275 {
4276 if (mp->m_mode & expand_mapmodes)
4277 {
4278 p = translate_mapping(mp->m_keys, TRUE);
4279 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4280 {
4281 if (round == 1)
4282 ++count;
4283 else
4284 {
4285 (*file)[count++] = p;
4286 p = NULL;
4287 }
4288 }
4289 vim_free(p);
4290 }
4291 } /* for (mp) */
4292 } /* for (hash) */
4293
4294 if (count == 0) /* no match found */
4295 break; /* for (round) */
4296
4297 if (round == 1)
4298 {
4299 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4300 if (*file == NULL)
4301 return FAIL;
4302 }
4303 } /* for (round) */
4304
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004305 if (count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004307 char_u **ptr1;
4308 char_u **ptr2;
4309 char_u **ptr3;
4310
4311 /* Sort the matches */
4312 sort_strings(*file, count);
4313
4314 /* Remove multiple entries */
4315 ptr1 = *file;
4316 ptr2 = ptr1 + 1;
4317 ptr3 = ptr1 + count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 while (ptr2 < ptr3)
4320 {
4321 if (STRCMP(*ptr1, *ptr2))
4322 *++ptr1 = *ptr2++;
4323 else
4324 {
4325 vim_free(*ptr2++);
4326 count--;
4327 }
4328 }
4329 }
4330
4331 *num_file = count;
4332 return (count == 0 ? FAIL : OK);
4333}
4334#endif /* FEAT_CMDL_COMPL */
4335
4336/*
4337 * Check for an abbreviation.
4338 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4339 * "c" is the character typed before check_abbr was called. It may have
4340 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4341 *
4342 * Historic vi practice: The last character of an abbreviation must be an id
4343 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4344 * characters or all non-id characters. This allows for abbr. "#i" to
4345 * "#include".
4346 *
4347 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4348 * Then there must be white space before the abbr.
4349 *
4350 * return TRUE if there is an abbreviation, FALSE if not
4351 */
4352 int
4353check_abbr(c, ptr, col, mincol)
4354 int c;
4355 char_u *ptr;
4356 int col;
4357 int mincol;
4358{
4359 int len;
4360 int scol; /* starting column of the abbr. */
4361 int j;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004362 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 char_u tb[MB_MAXBYTES + 4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 mapblock_T *mp;
4365#ifdef FEAT_LOCALMAP
4366 mapblock_T *mp2;
4367#endif
4368#ifdef FEAT_MBYTE
4369 int clen = 0; /* length in characters */
4370#endif
4371 int is_id = TRUE;
4372 int vim_abbr;
4373
4374 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4375 return FALSE;
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02004376
4377 /* no remapping implies no abbreviation, except for CTRL-] */
4378 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0 && c != Ctrl_RSB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 return FALSE;
4380
4381 /*
4382 * Check for word before the cursor: If it ends in a keyword char all
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00004383 * chars before it must be keyword chars or non-keyword chars, but not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 * white space. If it ends in a non-keyword char we accept any characters
4385 * before it except white space.
4386 */
4387 if (col == 0) /* cannot be an abbr. */
4388 return FALSE;
4389
4390#ifdef FEAT_MBYTE
4391 if (has_mbyte)
4392 {
4393 char_u *p;
4394
4395 p = mb_prevptr(ptr, ptr + col);
4396 if (!vim_iswordp(p))
4397 vim_abbr = TRUE; /* Vim added abbr. */
4398 else
4399 {
4400 vim_abbr = FALSE; /* vi compatible abbr. */
4401 if (p > ptr)
4402 is_id = vim_iswordp(mb_prevptr(ptr, p));
4403 }
4404 clen = 1;
4405 while (p > ptr + mincol)
4406 {
4407 p = mb_prevptr(ptr, p);
4408 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4409 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004410 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 break;
4412 }
4413 ++clen;
4414 }
4415 scol = (int)(p - ptr);
4416 }
4417 else
4418#endif
4419 {
4420 if (!vim_iswordc(ptr[col - 1]))
4421 vim_abbr = TRUE; /* Vim added abbr. */
4422 else
4423 {
4424 vim_abbr = FALSE; /* vi compatible abbr. */
4425 if (col > 1)
4426 is_id = vim_iswordc(ptr[col - 2]);
4427 }
4428 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4429 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4430 ;
4431 }
4432
4433 if (scol < mincol)
4434 scol = mincol;
4435 if (scol < col) /* there is a word in front of the cursor */
4436 {
4437 ptr += scol;
4438 len = col - scol;
4439#ifdef FEAT_LOCALMAP
4440 mp = curbuf->b_first_abbr;
4441 mp2 = first_abbr;
4442 if (mp == NULL)
4443 {
4444 mp = mp2;
4445 mp2 = NULL;
4446 }
4447#else
4448 mp = first_abbr;
4449#endif
4450 for ( ; mp;
4451#ifdef FEAT_LOCALMAP
4452 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4453#endif
4454 (mp = mp->m_next))
4455 {
4456 /* find entries with right mode and keys */
4457 if ( (mp->m_mode & State)
4458 && mp->m_keylen == len
4459 && !STRNCMP(mp->m_keys, ptr, (size_t)len))
4460 break;
4461 }
4462 if (mp != NULL)
4463 {
4464 /*
4465 * Found a match:
4466 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4467 * This goes from end to start.
4468 *
4469 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4470 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4471 * Characters where IS_SPECIAL() == TRUE: key codes, need
4472 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4473 *
4474 * Character CTRL-] is treated specially - it completes the
4475 * abbreviation, but is not inserted into the input stream.
4476 */
4477 j = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (c != Ctrl_RSB)
4479 {
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004480 /* special key code, split up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (IS_SPECIAL(c) || c == K_SPECIAL)
4482 {
4483 tb[j++] = K_SPECIAL;
4484 tb[j++] = K_SECOND(c);
4485 tb[j++] = K_THIRD(c);
4486 }
4487 else
4488 {
4489 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4490 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4491#ifdef FEAT_MBYTE
4492 if (has_mbyte)
4493 {
4494 /* if ABBR_OFF has been added, remove it here */
4495 if (c >= ABBR_OFF)
4496 c -= ABBR_OFF;
4497 j += (*mb_char2bytes)(c, tb + j);
4498 }
4499 else
4500#endif
4501 tb[j++] = c;
4502 }
4503 tb[j] = NUL;
4504 /* insert the last typed char */
4505 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4506 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004507#ifdef FEAT_EVAL
4508 if (mp->m_expr)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004509 s = eval_map_expr(mp->m_str, c);
Bram Moolenaar4e427192006-03-10 21:34:27 +00004510 else
4511#endif
4512 s = mp->m_str;
4513 if (s != NULL)
4514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* insert the to string */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004516 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 /* no abbrev. for these chars */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004518 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4519#ifdef FEAT_EVAL
4520 if (mp->m_expr)
4521 vim_free(s);
4522#endif
4523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 tb[0] = Ctrl_H;
4526 tb[1] = NUL;
4527#ifdef FEAT_MBYTE
4528 if (has_mbyte)
4529 len = clen; /* Delete characters instead of bytes */
4530#endif
4531 while (len-- > 0) /* delete the from string */
4532 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4533 return TRUE;
4534 }
4535 }
4536 return FALSE;
4537}
4538
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004539#ifdef FEAT_EVAL
4540/*
4541 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4542 * special characters.
4543 */
4544 static char_u *
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004545eval_map_expr(str, c)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004546 char_u *str;
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004547 int c; /* NUL or typed character for abbreviation */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004548{
4549 char_u *res;
Bram Moolenaar8424a622006-04-19 21:23:36 +00004550 char_u *p;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004551 char_u *expr;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004552 char_u *save_cmd;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004553 pos_T save_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004554 int save_msg_col;
4555 int save_msg_row;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004556
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004557 /* Remove escaping of CSI, because "str" is in a format to be used as
4558 * typeahead. */
4559 expr = vim_strsave(str);
4560 if (expr == NULL)
4561 return NULL;
4562 vim_unescape_csi(expr);
4563
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004564 save_cmd = save_cmdline_alloc();
4565 if (save_cmd == NULL)
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004566 {
4567 vim_free(expr);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004568 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004569 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004570
4571 /* Forbid changing text or using ":normal" to avoid most of the bad side
4572 * effects. Also restore the cursor position. */
4573 ++textlock;
4574#ifdef FEAT_EX_EXTRA
4575 ++ex_normal_lock;
4576#endif
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004577 set_vim_var_char(c); /* set v:char to the typed character */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004578 save_cursor = curwin->w_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004579 save_msg_col = msg_col;
4580 save_msg_row = msg_row;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004581 p = eval_to_string(expr, NULL, FALSE);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004582 --textlock;
4583#ifdef FEAT_EX_EXTRA
4584 --ex_normal_lock;
4585#endif
4586 curwin->w_cursor = save_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004587 msg_col = save_msg_col;
4588 msg_row = save_msg_row;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004589
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004590 restore_cmdline_alloc(save_cmd);
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004591 vim_free(expr);
4592
Bram Moolenaar8424a622006-04-19 21:23:36 +00004593 if (p == NULL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004594 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004595 /* Escape CSI in the result to be able to use the string as typeahead. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004596 res = vim_strsave_escape_csi(p);
4597 vim_free(p);
4598
4599 return res;
4600}
4601#endif
4602
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004603/*
4604 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4605 * can be put in the typeahead buffer.
4606 * Returns NULL when out of memory.
4607 */
4608 char_u *
4609vim_strsave_escape_csi(p)
4610 char_u *p;
4611{
4612 char_u *res;
4613 char_u *s, *d;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004614
4615 /* Need a buffer to hold up to three times as much. */
Bram Moolenaar8424a622006-04-19 21:23:36 +00004616 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004617 if (res != NULL)
4618 {
Bram Moolenaar8424a622006-04-19 21:23:36 +00004619 d = res;
4620 for (s = p; *s != NUL; )
4621 {
4622 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4623 {
4624 /* Copy special key unmodified. */
4625 *d++ = *s++;
4626 *d++ = *s++;
4627 *d++ = *s++;
4628 }
4629 else
4630 {
Bram Moolenaar229f8db2013-05-06 05:50:28 +02004631#ifdef FEAT_MBYTE
4632 int len = mb_char2len(PTR2CHAR(s));
4633 int len2 = mb_ptr2len(s);
4634#endif
Bram Moolenaar8424a622006-04-19 21:23:36 +00004635 /* Add character, possibly multi-byte to destination, escaping
4636 * CSI and K_SPECIAL. */
4637 d = add_char2buf(PTR2CHAR(s), d);
Bram Moolenaar229f8db2013-05-06 05:50:28 +02004638#ifdef FEAT_MBYTE
4639 while (len < len2)
4640 {
4641 /* add following combining char */
4642 d = add_char2buf(PTR2CHAR(s + len), d);
4643 len += mb_char2len(PTR2CHAR(s + len));
4644 }
4645#endif
Bram Moolenaar8424a622006-04-19 21:23:36 +00004646 mb_ptr_adv(s);
4647 }
4648 }
4649 *d = NUL;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004650 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004651 return res;
4652}
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654/*
Bram Moolenaar81b85872007-03-04 20:22:01 +00004655 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4656 * vim_strsave_escape_csi(). Works in-place.
4657 */
4658 void
4659vim_unescape_csi(p)
4660 char_u *p;
4661{
4662 char_u *s = p, *d = p;
4663
4664 while (*s != NUL)
4665 {
4666 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4667 {
4668 *d++ = K_SPECIAL;
4669 s += 3;
4670 }
4671 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4672 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4673 {
4674 *d++ = CSI;
4675 s += 3;
4676 }
4677 else
4678 *d++ = *s++;
4679 }
4680 *d = NUL;
4681}
4682
4683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 * Write map commands for the current mappings to an .exrc file.
4685 * Return FAIL on error, OK otherwise.
4686 */
4687 int
4688makemap(fd, buf)
4689 FILE *fd;
4690 buf_T *buf; /* buffer for local mappings or NULL */
4691{
4692 mapblock_T *mp;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004693 char_u c1, c2, c3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u *p;
4695 char *cmd;
4696 int abbr;
4697 int hash;
4698 int did_cpo = FALSE;
4699 int i;
4700
4701 validate_maphash();
4702
4703 /*
4704 * Do the loop twice: Once for mappings, once for abbreviations.
4705 * Then loop over all map hash lists.
4706 */
4707 for (abbr = 0; abbr < 2; ++abbr)
4708 for (hash = 0; hash < 256; ++hash)
4709 {
4710 if (abbr)
4711 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004712 if (hash > 0) /* there is only one abbr list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714#ifdef FEAT_LOCALMAP
4715 if (buf != NULL)
4716 mp = buf->b_first_abbr;
4717 else
4718#endif
4719 mp = first_abbr;
4720 }
4721 else
4722 {
4723#ifdef FEAT_LOCALMAP
4724 if (buf != NULL)
4725 mp = buf->b_maphash[hash];
4726 else
4727#endif
4728 mp = maphash[hash];
4729 }
4730
4731 for ( ; mp; mp = mp->m_next)
4732 {
4733 /* skip script-local mappings */
4734 if (mp->m_noremap == REMAP_SCRIPT)
4735 continue;
4736
4737 /* skip mappings that contain a <SNR> (script-local thing),
4738 * they probably don't work when loaded again */
4739 for (p = mp->m_str; *p != NUL; ++p)
4740 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4741 && p[2] == (int)KE_SNR)
4742 break;
4743 if (*p != NUL)
4744 continue;
4745
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004746 /* It's possible to create a mapping and then ":unmap" certain
4747 * modes. We recreate this here by mapping the individual
4748 * modes, which requires up to three of them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 c1 = NUL;
4750 c2 = NUL;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004751 c3 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (abbr)
4753 cmd = "abbr";
4754 else
4755 cmd = "map";
4756 switch (mp->m_mode)
4757 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00004758 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 break;
4760 case NORMAL:
4761 c1 = 'n';
4762 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004763 case VISUAL:
4764 c1 = 'x';
4765 break;
4766 case SELECTMODE:
4767 c1 = 's';
4768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 case OP_PENDING:
4770 c1 = 'o';
4771 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004772 case NORMAL + VISUAL:
4773 c1 = 'n';
4774 c2 = 'x';
4775 break;
4776 case NORMAL + SELECTMODE:
4777 c1 = 'n';
4778 c2 = 's';
4779 break;
4780 case NORMAL + OP_PENDING:
4781 c1 = 'n';
4782 c2 = 'o';
4783 break;
4784 case VISUAL + SELECTMODE:
4785 c1 = 'v';
4786 break;
4787 case VISUAL + OP_PENDING:
4788 c1 = 'x';
4789 c2 = 'o';
4790 break;
4791 case SELECTMODE + OP_PENDING:
4792 c1 = 's';
4793 c2 = 'o';
4794 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004795 case NORMAL + VISUAL + SELECTMODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 c1 = 'n';
4797 c2 = 'v';
4798 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004799 case NORMAL + VISUAL + OP_PENDING:
4800 c1 = 'n';
4801 c2 = 'x';
4802 c3 = 'o';
4803 break;
4804 case NORMAL + SELECTMODE + OP_PENDING:
4805 c1 = 'n';
4806 c2 = 's';
4807 c3 = 'o';
4808 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004809 case VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 c1 = 'v';
4811 c2 = 'o';
4812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 case CMDLINE + INSERT:
4814 if (!abbr)
4815 cmd = "map!";
4816 break;
4817 case CMDLINE:
4818 c1 = 'c';
4819 break;
4820 case INSERT:
4821 c1 = 'i';
4822 break;
4823 case LANGMAP:
4824 c1 = 'l';
4825 break;
4826 default:
4827 EMSG(_("E228: makemap: Illegal mode"));
4828 return FAIL;
4829 }
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004830 do /* do this twice if c2 is set, 3 times with c3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832 /* When outputting <> form, need to make sure that 'cpo'
4833 * is set to the Vim default. */
4834 if (!did_cpo)
4835 {
4836 if (*mp->m_str == NUL) /* will use <Nop> */
4837 did_cpo = TRUE;
4838 else
4839 for (i = 0; i < 2; ++i)
4840 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4841 if (*p == K_SPECIAL || *p == NL)
4842 did_cpo = TRUE;
4843 if (did_cpo)
4844 {
4845 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4846 || put_eol(fd) < 0
4847 || fprintf(fd, "set cpo&vim") < 0
4848 || put_eol(fd) < 0)
4849 return FAIL;
4850 }
4851 }
4852 if (c1 && putc(c1, fd) < 0)
4853 return FAIL;
4854 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4855 return FAIL;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004856 if (fputs(cmd, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 return FAIL;
4858 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4859 return FAIL;
4860 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4861 return FAIL;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004862#ifdef FEAT_EVAL
4863 if (mp->m_noremap == REMAP_SCRIPT
4864 && fputs("<script>", fd) < 0)
4865 return FAIL;
4866 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4867 return FAIL;
4868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 if ( putc(' ', fd) < 0
4871 || put_escstr(fd, mp->m_keys, 0) == FAIL
4872 || putc(' ', fd) < 0
4873 || put_escstr(fd, mp->m_str, 1) == FAIL
4874 || put_eol(fd) < 0)
4875 return FAIL;
4876 c1 = c2;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004877 c2 = c3;
4878 c3 = NUL;
4879 } while (c1 != NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 }
4882
4883 if (did_cpo)
4884 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4885 || put_eol(fd) < 0
4886 || fprintf(fd, "unlet s:cpo_save") < 0
4887 || put_eol(fd) < 0)
4888 return FAIL;
4889 return OK;
4890}
4891
4892/*
4893 * write escape string to file
4894 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4895 *
4896 * return FAIL for failure, OK otherwise
4897 */
4898 int
4899put_escstr(fd, strstart, what)
4900 FILE *fd;
4901 char_u *strstart;
4902 int what;
4903{
4904 char_u *str = strstart;
4905 int c;
4906 int modifiers;
4907
4908 /* :map xx <Nop> */
4909 if (*str == NUL && what == 1)
4910 {
4911 if (fprintf(fd, "<Nop>") < 0)
4912 return FAIL;
4913 return OK;
4914 }
4915
4916 for ( ; *str != NUL; ++str)
4917 {
4918#ifdef FEAT_MBYTE
4919 char_u *p;
4920
4921 /* Check for a multi-byte character, which may contain escaped
4922 * K_SPECIAL and CSI bytes */
4923 p = mb_unescape(&str);
4924 if (p != NULL)
4925 {
4926 while (*p != NUL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004927 if (fputc(*p++, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 return FAIL;
4929 --str;
4930 continue;
4931 }
4932#endif
4933
4934 c = *str;
4935 /*
4936 * Special key codes have to be translated to be able to make sense
4937 * when they are read back.
4938 */
4939 if (c == K_SPECIAL && what != 2)
4940 {
4941 modifiers = 0x0;
4942 if (str[1] == KS_MODIFIER)
4943 {
4944 modifiers = str[2];
4945 str += 3;
4946 c = *str;
4947 }
4948 if (c == K_SPECIAL)
4949 {
4950 c = TO_SPECIAL(str[1], str[2]);
4951 str += 2;
4952 }
4953 if (IS_SPECIAL(c) || modifiers) /* special key */
4954 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004955 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 return FAIL;
4957 continue;
4958 }
4959 }
4960
4961 /*
4962 * A '\n' in a map command should be written as <NL>.
4963 * A '\n' in a set command should be written as \^V^J.
4964 */
4965 if (c == NL)
4966 {
4967 if (what == 2)
4968 {
4969 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
4970 return FAIL;
4971 }
4972 else
4973 {
4974 if (fprintf(fd, "<NL>") < 0)
4975 return FAIL;
4976 }
4977 continue;
4978 }
4979
4980 /*
4981 * Some characters have to be escaped with CTRL-V to
4982 * prevent them from misinterpreted in DoOneCmd().
4983 * A space, Tab and '"' has to be escaped with a backslash to
4984 * prevent it to be misinterpreted in do_set().
4985 * A space has to be escaped with a CTRL-V when it's at the start of a
4986 * ":map" rhs.
4987 * A '<' has to be escaped with a CTRL-V to prevent it being
4988 * interpreted as the start of a special key name.
4989 * A space in the lhs of a :map needs a CTRL-V.
4990 */
4991 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
4992 {
4993 if (putc('\\', fd) < 0)
4994 return FAIL;
4995 }
4996 else if (c < ' ' || c > '~' || c == '|'
4997 || (what == 0 && c == ' ')
4998 || (what == 1 && str == strstart && c == ' ')
4999 || (what != 2 && c == '<'))
5000 {
5001 if (putc(Ctrl_V, fd) < 0)
5002 return FAIL;
5003 }
5004 if (putc(c, fd) < 0)
5005 return FAIL;
5006 }
5007 return OK;
5008}
5009
5010/*
5011 * Check all mappings for the presence of special key codes.
5012 * Used after ":set term=xxx".
5013 */
5014 void
5015check_map_keycodes()
5016{
5017 mapblock_T *mp;
5018 char_u *p;
5019 int i;
5020 char_u buf[3];
5021 char_u *save_name;
5022 int abbr;
5023 int hash;
5024#ifdef FEAT_LOCALMAP
5025 buf_T *bp;
5026#endif
5027
5028 validate_maphash();
5029 save_name = sourcing_name;
5030 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
5031
5032#ifdef FEAT_LOCALMAP
5033 /* This this once for each buffer, and then once for global
5034 * mappings/abbreviations with bp == NULL */
5035 for (bp = firstbuf; ; bp = bp->b_next)
5036 {
5037#endif
5038 /*
5039 * Do the loop twice: Once for mappings, once for abbreviations.
5040 * Then loop over all map hash lists.
5041 */
5042 for (abbr = 0; abbr <= 1; ++abbr)
5043 for (hash = 0; hash < 256; ++hash)
5044 {
5045 if (abbr)
5046 {
5047 if (hash) /* there is only one abbr list */
5048 break;
5049#ifdef FEAT_LOCALMAP
5050 if (bp != NULL)
5051 mp = bp->b_first_abbr;
5052 else
5053#endif
5054 mp = first_abbr;
5055 }
5056 else
5057 {
5058#ifdef FEAT_LOCALMAP
5059 if (bp != NULL)
5060 mp = bp->b_maphash[hash];
5061 else
5062#endif
5063 mp = maphash[hash];
5064 }
5065 for ( ; mp != NULL; mp = mp->m_next)
5066 {
5067 for (i = 0; i <= 1; ++i) /* do this twice */
5068 {
5069 if (i == 0)
5070 p = mp->m_keys; /* once for the "from" part */
5071 else
5072 p = mp->m_str; /* and once for the "to" part */
5073 while (*p)
5074 {
5075 if (*p == K_SPECIAL)
5076 {
5077 ++p;
5078 if (*p < 128) /* for "normal" tcap entries */
5079 {
5080 buf[0] = p[0];
5081 buf[1] = p[1];
5082 buf[2] = NUL;
5083 (void)add_termcap_entry(buf, FALSE);
5084 }
5085 ++p;
5086 }
5087 ++p;
5088 }
5089 }
5090 }
5091 }
5092#ifdef FEAT_LOCALMAP
5093 if (bp == NULL)
5094 break;
5095 }
5096#endif
5097 sourcing_name = save_name;
5098}
5099
Bram Moolenaarbd743252010-10-20 21:23:33 +02005100#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101/*
Bram Moolenaarbd743252010-10-20 21:23:33 +02005102 * Check the string "keys" against the lhs of all mappings.
5103 * Return pointer to rhs of mapping (mapblock->m_str).
5104 * NULL when no mapping found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 */
5106 char_u *
Bram Moolenaarbd743252010-10-20 21:23:33 +02005107check_map(keys, mode, exact, ign_mod, abbr, mp_ptr, local_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 char_u *keys;
5109 int mode;
5110 int exact; /* require exact match */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005111 int ign_mod; /* ignore preceding modifier */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005112 int abbr; /* do abbreviations */
Bram Moolenaarbd743252010-10-20 21:23:33 +02005113 mapblock_T **mp_ptr; /* return: pointer to mapblock or NULL */
5114 int *local_ptr; /* return: buffer-local mapping or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
5116 int hash;
5117 int len, minlen;
5118 mapblock_T *mp;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005119 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_LOCALMAP
5121 int local;
5122#endif
5123
5124 validate_maphash();
5125
5126 len = (int)STRLEN(keys);
5127#ifdef FEAT_LOCALMAP
5128 for (local = 1; local >= 0; --local)
5129#endif
5130 /* loop over all hash lists */
5131 for (hash = 0; hash < 256; ++hash)
5132 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005133 if (abbr)
5134 {
5135 if (hash > 0) /* there is only one list. */
5136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137#ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005138 if (local)
5139 mp = curbuf->b_first_abbr;
5140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005142 mp = first_abbr;
5143 }
5144#ifdef FEAT_LOCALMAP
5145 else if (local)
5146 mp = curbuf->b_maphash[hash];
5147#endif
5148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 mp = maphash[hash];
5150 for ( ; mp != NULL; mp = mp->m_next)
5151 {
5152 /* skip entries with wrong mode, wrong length and not matching
5153 * ones */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005154 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
5155 {
5156 if (len > mp->m_keylen)
5157 minlen = mp->m_keylen;
5158 else
5159 minlen = len;
5160 s = mp->m_keys;
5161 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
5162 && s[2] != NUL)
5163 {
5164 s += 3;
5165 if (len > mp->m_keylen - 3)
5166 minlen = mp->m_keylen - 3;
5167 }
5168 if (STRNCMP(s, keys, minlen) == 0)
Bram Moolenaarbd743252010-10-20 21:23:33 +02005169 {
5170 if (mp_ptr != NULL)
5171 *mp_ptr = mp;
5172 if (local_ptr != NULL)
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005173#ifdef FEAT_LOCALMAP
Bram Moolenaarbd743252010-10-20 21:23:33 +02005174 *local_ptr = local;
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005175#else
5176 *local_ptr = 0;
5177#endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005178 return mp->m_str;
Bram Moolenaarbd743252010-10-20 21:23:33 +02005179 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 }
5183
5184 return NULL;
5185}
5186#endif
5187
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005188#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar371d5402006-03-20 21:47:49 +00005189
5190#define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192/*
5193 * Default mappings for some often used keys.
5194 */
5195static struct initmap
5196{
5197 char_u *arg;
5198 int mode;
5199} initmappings[] =
5200{
5201#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
5202 /* Use the Windows (CUA) keybindings. */
5203# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /* paste, copy and cut */
5205 {(char_u *)"<S-Insert> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005206 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005208 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
5209 {(char_u *)"<S-Del> \"*d", VIS_SEL},
5210 {(char_u *)"<C-Del> \"*d", VIS_SEL},
5211 {(char_u *)"<C-X> \"*d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5213# else
Bram Moolenaar371d5402006-03-20 21:47:49 +00005214 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005216 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5218
5219 /* paste, copy and cut */
5220# ifdef FEAT_CLIPBOARD
5221# ifdef DJGPP
5222 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005223 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005225 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226# if 0 /* Shift-Del produces the same code as Del */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005227 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00005229 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5230 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231# else
5232 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005233 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005235 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5236 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5237 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5238 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239# endif
5240# else
5241 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005242 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005244 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5245 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5246 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247# endif
5248# endif
5249#endif
5250
5251#if defined(MACOS)
5252 /* Use the Standard MacOS binding. */
5253 /* paste, copy and cut */
5254 {(char_u *)"<D-v> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005255 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005257 {(char_u *)"<D-c> \"*y", VIS_SEL},
5258 {(char_u *)"<D-x> \"*d", VIS_SEL},
5259 {(char_u *)"<Backspace> \"-d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261};
Bram Moolenaar371d5402006-03-20 21:47:49 +00005262
5263# undef VIS_SEL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266/*
5267 * Set up default mappings.
5268 */
5269 void
5270init_mappings()
5271{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005272#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 int i;
5274
5275 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5276 add_map(initmappings[i].arg, initmappings[i].mode);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278}
5279
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005280#if defined(MSDOS) || defined(MSWIN) || defined(OS2) \
5281 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282/*
5283 * Add a mapping "map" for mode "mode".
5284 * Need to put string in allocated memory, because do_map() will modify it.
5285 */
5286 void
5287add_map(map, mode)
5288 char_u *map;
5289 int mode;
5290{
5291 char_u *s;
5292 char_u *cpo_save = p_cpo;
5293
5294 p_cpo = (char_u *)""; /* Allow <> notation */
5295 s = vim_strsave(map);
5296 if (s != NULL)
5297 {
5298 (void)do_map(0, s, mode, FALSE);
5299 vim_free(s);
5300 }
5301 p_cpo = cpo_save;
5302}
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005303#endif