blob: 7a5756b8e100ffb03064d0ababe0249b25879859 [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
Bram Moolenaar0a36fec2014-02-11 15:10:43 +010043static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +010046static buffheader_T save_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
47static buffheader_T save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#endif
Bram Moolenaar0a36fec2014-02-11 15:10:43 +010049static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
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
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100115static char_u *get_buffcont(buffheader_T *, int);
116static void add_buff(buffheader_T *, char_u *, long n);
117static void add_num_buff(buffheader_T *, long);
118static void add_char_buff(buffheader_T *, int);
119static int read_readbuffers(int advance);
120static int read_readbuf(buffheader_T *buf, int advance);
121static void start_stuff(void);
122static int read_redo(int, int);
123static void copy_redo(int);
124static void init_typebuf(void);
125static void gotchars(char_u *, int);
126static void may_sync_undo(void);
127static void closescript(void);
128static int vgetorpeek(int);
129static void map_free(mapblock_T **);
130static void validate_maphash(void);
131static void showmap(mapblock_T *mp, int local);
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000132#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100133static char_u *eval_map_expr(char_u *str, int c);
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136/*
137 * Free and clear a buffer.
138 */
139 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100140free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100142 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
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 *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100157get_buffcont(
158 buffheader_T *buffer,
159 int dozero) /* count == zero is not an error */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160{
161 long_u count = 0;
162 char_u *p = NULL;
163 char_u *p2;
164 char_u *str;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100165 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
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 *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100188get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189{
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 *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100222get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100232add_buff(
233 buffheader_T *buf,
234 char_u *s,
235 long slen) /* length of "s" or -1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100237 buffblock_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 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;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100273 p = (buffblock_T *)lalloc((long_u)(sizeof(buffblock_T) + len),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100291add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292{
293 char_u number[32];
294
295 sprintf((char *)number, "%ld", n);
296 add_buff(buf, number, -1L);
297}
298
299/*
300 * Add character 'c' to buffer "buf".
301 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
302 */
303 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100304add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305{
306#ifdef FEAT_MBYTE
307 char_u bytes[MB_MAXBYTES + 1];
308 int len;
309 int i;
310#endif
311 char_u temp[4];
312
313#ifdef FEAT_MBYTE
314 if (IS_SPECIAL(c))
315 len = 1;
316 else
317 len = (*mb_char2bytes)(c, bytes);
318 for (i = 0; i < len; ++i)
319 {
320 if (!IS_SPECIAL(c))
321 c = bytes[i];
322#endif
323
324 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
325 {
326 /* translate special key code into three byte sequence */
327 temp[0] = K_SPECIAL;
328 temp[1] = K_SECOND(c);
329 temp[2] = K_THIRD(c);
330 temp[3] = NUL;
331 }
332#ifdef FEAT_GUI
333 else if (c == CSI)
334 {
335 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
336 temp[0] = CSI;
337 temp[1] = KS_EXTRA;
338 temp[2] = (int)KE_CSI;
339 temp[3] = NUL;
340 }
341#endif
342 else
343 {
344 temp[0] = c;
345 temp[1] = NUL;
346 }
347 add_buff(buf, temp, -1L);
348#ifdef FEAT_MBYTE
349 }
350#endif
351}
352
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100353/* First read ahead buffer. Used for translated commands. */
354static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
355
356/* Second read ahead buffer. Used for redo. */
357static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100360 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
361 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 * If advance == TRUE go to the next char.
363 * No translation is done K_SPECIAL and CSI are escaped.
364 */
365 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100366read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100368 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100370 c = read_readbuf(&readbuf1, advance);
371 if (c == NUL)
372 c = read_readbuf(&readbuf2, advance);
373 return c;
374}
375
376 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100377read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100378{
379 char_u c;
380 buffblock_T *curr;
381
382 if (buf->bh_first.b_next == NULL) /* buffer is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 return NUL;
384
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385 curr = buf->bh_first.b_next;
386 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
388 if (advance)
389 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100392 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100394 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 }
396 }
397 return c;
398}
399
400/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100401 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
403 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100404start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100406 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100408 readbuf1.bh_curr = &(readbuf1.bh_first);
409 readbuf1.bh_space = 0;
410 }
411 if (readbuf2.bh_first.b_next != NULL)
412 {
413 readbuf2.bh_curr = &(readbuf2.bh_first);
414 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 }
416}
417
418/*
419 * Return TRUE if the stuff buffer is empty.
420 */
421 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100422stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100424 return (readbuf1.bh_first.b_next == NULL
425 && readbuf2.bh_first.b_next == NULL);
426}
427
428/*
429 * Return TRUE if readbuf1 is empty. There may still be redo characters in
430 * redbuf2.
431 */
432 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100433readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100434{
435 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436}
437
438/*
439 * Set a typeahead character that won't be flushed.
440 */
441 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100442typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443{
444 typeahead_char = c;
445}
446
447/*
448 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100449 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 * flush all typeahead characters (used when interrupted by a CTRL-C).
451 */
452 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100453flush_buffers(int flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 init_typebuf();
456
457 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100458 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 ;
460
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100461 if (flush_typeahead) /* remove all typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {
463 /*
464 * We have to get all characters, because we may delete the first part
465 * of an escape sequence.
466 * In an xterm we get one char at a time and we have to get them all.
467 */
468 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L,
469 typebuf.tb_change_cnt) != 0)
470 ;
471 typebuf.tb_off = MAXMAPLEN;
472 typebuf.tb_len = 0;
473 }
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200474 else /* remove mapped characters at the start only */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 {
476 typebuf.tb_off += typebuf.tb_maplen;
477 typebuf.tb_len -= typebuf.tb_maplen;
478 }
479 typebuf.tb_maplen = 0;
480 typebuf.tb_silent = 0;
481 cmd_silent = FALSE;
482 typebuf.tb_no_abbr_cnt = 0;
483}
484
485/*
486 * The previous contents of the redo buffer is kept in old_redobuffer.
487 * This is used for the CTRL-O <.> command in insert mode.
488 */
489 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100490ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 if (!block_redo)
493 {
494 free_buff(&old_redobuff);
495 old_redobuff = redobuff;
496 redobuff.bh_first.b_next = NULL;
497 }
498}
499
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100500/*
501 * Discard the contents of the redo buffer and restore the previous redo
502 * buffer.
503 */
504 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100505CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100506{
507 if (!block_redo)
508 {
509 free_buff(&redobuff);
510 redobuff = old_redobuff;
511 old_redobuff.bh_first.b_next = NULL;
512 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100513 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100514 ;
515 }
516}
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
519/*
520 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
521 * Used before executing autocommands and user functions.
522 */
523static int save_level = 0;
524
525 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100526saveRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 char_u *s;
529
530 if (save_level++ == 0)
531 {
532 save_redobuff = redobuff;
533 redobuff.bh_first.b_next = NULL;
534 save_old_redobuff = old_redobuff;
535 old_redobuff.bh_first.b_next = NULL;
536
537 /* Make a copy, so that ":normal ." in a function works. */
538 s = get_buffcont(&save_redobuff, FALSE);
539 if (s != NULL)
540 {
541 add_buff(&redobuff, s, -1L);
542 vim_free(s);
543 }
544 }
545}
546
547/*
548 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
549 * Used after executing autocommands and user functions.
550 */
551 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100552restoreRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 if (--save_level == 0)
555 {
556 free_buff(&redobuff);
557 redobuff = save_redobuff;
558 free_buff(&old_redobuff);
559 old_redobuff = save_old_redobuff;
560 }
561}
562#endif
563
564/*
565 * Append "s" to the redo buffer.
566 * K_SPECIAL and CSI should already have been escaped.
567 */
568 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100569AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 if (!block_redo)
572 add_buff(&redobuff, s, -1L);
573}
574
575/*
576 * Append to Redo buffer literally, escaping special characters with CTRL-V.
577 * K_SPECIAL and CSI are escaped as well.
578 */
579 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100580AppendToRedobuffLit(
581 char_u *str,
582 int len) /* length of "str" or -1 for up to the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000584 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 int c;
586 char_u *start;
587
588 if (block_redo)
589 return;
590
Bram Moolenaarebefac62005-12-28 22:39:57 +0000591 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 /* Put a string of normal characters in the redo buffer (that's
594 * faster). */
595 start = s;
596 while (*s >= ' '
597#ifndef EBCDIC
598 && *s < DEL /* EBCDIC: all chars above space are normal */
599#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000600 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 ++s;
602
603 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
604 * typed next. */
605 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
606 --s;
607 if (s > start)
608 add_buff(&redobuff, start, (long)(s - start));
609
Bram Moolenaarebefac62005-12-28 22:39:57 +0000610 if (*s == NUL || (len >= 0 && s - str >= len))
611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaarebefac62005-12-28 22:39:57 +0000613 /* Handle a special or multibyte character. */
614#ifdef FEAT_MBYTE
615 if (has_mbyte)
616 /* Handle composing chars separately. */
617 c = mb_cptr2char_adv(&s);
618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000620 c = *s++;
621 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
622 add_char_buff(&redobuff, Ctrl_V);
623
624 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
625 if (*s == NUL && c == '0')
626#ifdef EBCDIC
627 add_buff(&redobuff, (char_u *)"xf0", 3L);
628#else
629 add_buff(&redobuff, (char_u *)"048", 3L);
630#endif
631 else
632 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634}
635
636/*
637 * Append a character to the redo buffer.
638 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
639 */
640 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100641AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 if (!block_redo)
644 add_char_buff(&redobuff, c);
645}
646
647/*
648 * Append a number to the redo buffer.
649 */
650 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100651AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 if (!block_redo)
654 add_num_buff(&redobuff, n);
655}
656
657/*
658 * Append string "s" to the stuff buffer.
659 * CSI and K_SPECIAL must already have been escaped.
660 */
661 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100662stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100664 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665}
666
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200667/*
668 * Append string "s" to the redo stuff buffer.
669 * CSI and K_SPECIAL must already have been escaped.
670 */
671 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100672stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200673{
674 add_buff(&readbuf2, s, -1L);
675}
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100678stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100680 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681}
682
683#if defined(FEAT_EVAL) || defined(PROTO)
684/*
685 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
686 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200687 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 */
689 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100690stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200692 int c;
693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 while (*s != NUL)
695 {
696 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
697 {
698 /* Insert special key literally. */
699 stuffReadbuffLen(s, 3L);
700 s += 3;
701 }
702 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_MBYTE
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200705 c = mb_ptr2char_adv(&s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200707 c = *s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200709 if (c == CAR || c == NL || c == ESC)
710 c = ' ';
711 stuffcharReadbuff(c);
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
714}
715#endif
716
717/*
718 * Append a character to the stuff buffer.
719 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
720 */
721 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100722stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100724 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727/*
728 * Append a number to the stuff buffer.
729 */
730 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100731stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100733 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734}
735
736/*
737 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
738 * multibyte characters.
739 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100740 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
741 * otherwise.
742 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
744 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100745read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100747 static buffblock_T *bp;
748 static char_u *p;
749 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef FEAT_MBYTE
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 int n;
752 char_u buf[MB_MAXBYTES + 1];
753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#endif
755
756 if (init)
757 {
758 if (old_redo)
759 bp = old_redobuff.bh_first.b_next;
760 else
761 bp = redobuff.bh_first.b_next;
762 if (bp == NULL)
763 return FAIL;
764 p = bp->b_str;
765 return OK;
766 }
767 if ((c = *p) != NUL)
768 {
769 /* Reverse the conversion done by add_char_buff() */
770#ifdef FEAT_MBYTE
771 /* For a multi-byte character get all the bytes and return the
772 * converted character. */
773 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
774 n = MB_BYTE2LEN_CHECK(c);
775 else
776 n = 1;
777 for (i = 0; ; ++i)
778#endif
779 {
780 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
781 {
782 c = TO_SPECIAL(p[1], p[2]);
783 p += 2;
784 }
785#ifdef FEAT_GUI
786 if (c == CSI) /* escaped CSI */
787 p += 2;
788#endif
789 if (*++p == NUL && bp->b_next != NULL)
790 {
791 bp = bp->b_next;
792 p = bp->b_str;
793 }
794#ifdef FEAT_MBYTE
795 buf[i] = c;
796 if (i == n - 1) /* last byte of a character */
797 {
798 if (n != 1)
799 c = (*mb_ptr2char)(buf);
800 break;
801 }
802 c = *p;
803 if (c == NUL) /* cannot happen? */
804 break;
805#endif
806 }
807 }
808
809 return c;
810}
811
812/*
813 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
814 * If old_redo is TRUE, use old_redobuff instead of redobuff.
815 * The escaped K_SPECIAL and CSI are copied without translation.
816 */
817 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100818copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 int c;
821
822 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100823 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825
826/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100827 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * Insert the redo count into the command.
829 * If "old_redo" is TRUE, the last but one command is repeated
830 * instead of the last command (inserting text). This is used for
831 * CTRL-O <.> in insert mode
832 *
833 * return FAIL for failure, OK otherwise
834 */
835 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100836start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 int c;
839
840 /* init the pointers; return if nothing to redo */
841 if (read_redo(TRUE, old_redo) == FAIL)
842 return FAIL;
843
844 c = read_redo(FALSE, old_redo);
845
846 /* copy the buffer name, if present */
847 if (c == '"')
848 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100849 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 c = read_redo(FALSE, old_redo);
851
852 /* if a numbered buffer is used, increment the number */
853 if (c >= '1' && c < '9')
854 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100855 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 c = read_redo(FALSE, old_redo);
857 }
858
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (c == 'v') /* redo Visual */
860 {
861 VIsual = curwin->w_cursor;
862 VIsual_active = TRUE;
863 VIsual_select = FALSE;
864 VIsual_reselect = TRUE;
865 redo_VIsual_busy = TRUE;
866 c = read_redo(FALSE, old_redo);
867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 /* try to enter the count (in place of a previous count) */
870 if (count)
871 {
872 while (VIM_ISDIGIT(c)) /* skip "old" count */
873 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100874 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
877 /* copy from the redo buffer into the stuff buffer */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100878 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 copy_redo(old_redo);
880 return OK;
881}
882
883/*
884 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100885 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * return FAIL for failure, OK otherwise
887 */
888 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100889start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 int c;
892
893 if (read_redo(TRUE, FALSE) == FAIL)
894 return FAIL;
895 start_stuff();
896
897 /* skip the count and the command character */
898 while ((c = read_redo(FALSE, FALSE)) != NUL)
899 {
900 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
901 {
902 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100903 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 break;
905 }
906 }
907
908 /* copy the typed text from the redo buffer into the stuff buffer */
909 copy_redo(FALSE);
910 block_redo = TRUE;
911 return OK;
912}
913
914 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100915stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917 block_redo = FALSE;
918}
919
920/*
921 * Initialize typebuf.tb_buf to point to typebuf_init.
922 * alloc() cannot be used here: In out-of-memory situations it would
923 * be impossible to type anything.
924 */
925 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100926init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 if (typebuf.tb_buf == NULL)
929 {
930 typebuf.tb_buf = typebuf_init;
931 typebuf.tb_noremap = noremapbuf_init;
932 typebuf.tb_buflen = TYPELEN_INIT;
933 typebuf.tb_len = 0;
934 typebuf.tb_off = 0;
935 typebuf.tb_change_cnt = 1;
936 }
937}
938
939/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100940 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
941 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 *
943 * If noremap is REMAP_YES, new string can be mapped again.
944 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000945 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
946 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
948 * script-local mappings.
949 * If noremap is > 0, that many characters of the new string cannot be mapped.
950 *
951 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
952 * offset is non-zero!).
953 *
954 * If silent is TRUE, cmd_silent is set when the characters are obtained.
955 *
956 * return FAIL for failure, OK otherwise
957 */
958 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100959ins_typebuf(
960 char_u *str,
961 int noremap,
962 int offset,
963 int nottyped,
964 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 char_u *s1, *s2;
967 int newlen;
968 int addlen;
969 int i;
970 int newoff;
971 int val;
972 int nrm;
973
974 init_typebuf();
975 if (++typebuf.tb_change_cnt == 0)
976 typebuf.tb_change_cnt = 1;
977
978 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /*
981 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
982 */
983 if (offset == 0 && addlen <= typebuf.tb_off)
984 {
985 typebuf.tb_off -= addlen;
986 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
987 }
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 /*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000990 * Need to allocate a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
992 * characters. We add some extra room to avoid having to allocate too
993 * often.
994 */
995 else
996 {
997 newoff = MAXMAPLEN + 4;
998 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
999 if (newlen < 0) /* string is getting too long */
1000 {
1001 EMSG(_(e_toocompl)); /* also calls flush_buffers */
1002 setcursor();
1003 return FAIL;
1004 }
1005 s1 = alloc(newlen);
1006 if (s1 == NULL) /* out of memory */
1007 return FAIL;
1008 s2 = alloc(newlen);
1009 if (s2 == NULL) /* out of memory */
1010 {
1011 vim_free(s1);
1012 return FAIL;
1013 }
1014 typebuf.tb_buflen = newlen;
1015
1016 /* copy the old chars, before the insertion point */
1017 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1018 (size_t)offset);
1019 /* copy the new chars */
1020 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
1021 /* copy the old chars, after the insertion point, including the NUL at
1022 * the end */
1023 mch_memmove(s1 + newoff + offset + addlen,
1024 typebuf.tb_buf + typebuf.tb_off + offset,
1025 (size_t)(typebuf.tb_len - offset + 1));
1026 if (typebuf.tb_buf != typebuf_init)
1027 vim_free(typebuf.tb_buf);
1028 typebuf.tb_buf = s1;
1029
1030 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1031 (size_t)offset);
1032 mch_memmove(s2 + newoff + offset + addlen,
1033 typebuf.tb_noremap + typebuf.tb_off + offset,
1034 (size_t)(typebuf.tb_len - offset));
1035 if (typebuf.tb_noremap != noremapbuf_init)
1036 vim_free(typebuf.tb_noremap);
1037 typebuf.tb_noremap = s2;
1038
1039 typebuf.tb_off = newoff;
1040 }
1041 typebuf.tb_len += addlen;
1042
1043 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
1044 if (noremap == REMAP_SCRIPT)
1045 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001046 else if (noremap == REMAP_SKIP)
1047 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 else
1049 val = RM_NONE;
1050
1051 /*
1052 * Adjust typebuf.tb_noremap[] for the new characters:
1053 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1054 * (sometimes) not remappable
1055 * If noremap == REMAP_YES: all the new characters are mappable
1056 * If noremap > 0: "noremap" characters are not remappable, the rest
1057 * mappable
1058 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001059 if (noremap == REMAP_SKIP)
1060 nrm = 1;
1061 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 nrm = addlen;
1063 else
1064 nrm = noremap;
1065 for (i = 0; i < addlen; ++i)
1066 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1067 (--nrm >= 0) ? val : RM_YES;
1068
1069 /* tb_maplen and tb_silent only remember the length of mapped and/or
1070 * silent mappings at the start of the buffer, assuming that a mapped
1071 * sequence doesn't result in typed characters. */
1072 if (nottyped || typebuf.tb_maplen > offset)
1073 typebuf.tb_maplen += addlen;
1074 if (silent || typebuf.tb_silent > offset)
1075 {
1076 typebuf.tb_silent += addlen;
1077 cmd_silent = TRUE;
1078 }
1079 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1080 typebuf.tb_no_abbr_cnt += addlen;
1081
1082 return OK;
1083}
1084
1085/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001086 * Put character "c" back into the typeahead buffer.
1087 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001088 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1089 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001090 */
1091 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001092ins_char_typebuf(int c)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001093{
1094#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001095 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001096#else
1097 char_u buf[4];
1098#endif
1099 if (IS_SPECIAL(c))
1100 {
1101 buf[0] = K_SPECIAL;
1102 buf[1] = K_SECOND(c);
1103 buf[2] = K_THIRD(c);
1104 buf[3] = NUL;
1105 }
1106 else
1107 {
1108#ifdef FEAT_MBYTE
1109 buf[(*mb_char2bytes)(c, buf)] = NUL;
1110#else
1111 buf[0] = c;
1112 buf[1] = NUL;
1113#endif
1114 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001115 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001116}
1117
1118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001120 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001121 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1123 * changed it was reallocated and the old pointer can no longer be used.
1124 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1125 * that was just added.
1126 */
1127 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001128typebuf_changed(
1129 int tb_change_cnt) /* old value of typebuf.tb_change_cnt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001132#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1133 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135 ));
1136}
1137
1138/*
1139 * Return TRUE if there are no characters in the typeahead buffer that have
1140 * not been typed (result from a mapping or come from ":normal").
1141 */
1142 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001143typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144{
1145 return typebuf.tb_maplen == 0;
1146}
1147
1148/*
1149 * Return the number of characters that are mapped (or not typed).
1150 */
1151 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001152typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
1154 return typebuf.tb_maplen;
1155}
1156
1157/*
1158 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1159 */
1160 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001161del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
1163 int i;
1164
1165 if (len == 0)
1166 return; /* nothing to do */
1167
1168 typebuf.tb_len -= len;
1169
1170 /*
1171 * Easy case: Just increase typebuf.tb_off.
1172 */
1173 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1174 >= 3 * MAXMAPLEN + 3)
1175 typebuf.tb_off += len;
1176 /*
1177 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1178 */
1179 else
1180 {
1181 i = typebuf.tb_off + offset;
1182 /*
1183 * Leave some extra room at the end to avoid reallocation.
1184 */
1185 if (typebuf.tb_off > MAXMAPLEN)
1186 {
1187 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1188 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1189 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1190 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1191 typebuf.tb_off = MAXMAPLEN;
1192 }
1193 /* adjust typebuf.tb_buf (include the NUL at the end) */
1194 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1195 typebuf.tb_buf + i + len,
1196 (size_t)(typebuf.tb_len - offset + 1));
1197 /* adjust typebuf.tb_noremap[] */
1198 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1199 typebuf.tb_noremap + i + len,
1200 (size_t)(typebuf.tb_len - offset));
1201 }
1202
1203 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1204 {
1205 if (typebuf.tb_maplen < offset + len)
1206 typebuf.tb_maplen = offset;
1207 else
1208 typebuf.tb_maplen -= len;
1209 }
1210 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1211 {
1212 if (typebuf.tb_silent < offset + len)
1213 typebuf.tb_silent = offset;
1214 else
1215 typebuf.tb_silent -= len;
1216 }
1217 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1218 {
1219 if (typebuf.tb_no_abbr_cnt < offset + len)
1220 typebuf.tb_no_abbr_cnt = offset;
1221 else
1222 typebuf.tb_no_abbr_cnt -= len;
1223 }
1224
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001225#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001226 /* Reset the flag that text received from a client or from feedkeys()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001227 * was inserted in the typeahead buffer. */
1228 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#endif
1230 if (++typebuf.tb_change_cnt == 0)
1231 typebuf.tb_change_cnt = 1;
1232}
1233
1234/*
1235 * Write typed characters to script file.
1236 * If recording is on put the character in the recordbuffer.
1237 */
1238 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001239gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240{
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001241 char_u *s = chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 int c;
1243 char_u buf[2];
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001244 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /* remember how many chars were last recorded */
1247 if (Recording)
1248 last_recorded_len += len;
1249
1250 buf[1] = NUL;
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001251 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253 /* Handle one byte at a time; no translation to be done. */
1254 c = *s++;
1255 updatescript(c);
1256
1257 if (Recording)
1258 {
1259 buf[0] = c;
1260 add_buff(&recordbuff, buf, 1L);
1261 }
1262 }
1263 may_sync_undo();
1264
1265#ifdef FEAT_EVAL
1266 /* output "debug mode" message next time in debug mode */
1267 debug_did_msg = FALSE;
1268#endif
1269
1270 /* Since characters have been typed, consider the following to be in
1271 * another mapping. Search string will be kept in history. */
1272 ++maptick;
1273}
1274
1275/*
1276 * Sync undo. Called when typed characters are obtained from the typeahead
1277 * buffer, or when a menu is used.
1278 * Do not sync:
1279 * - In Insert mode, unless cursor key has been used.
1280 * - While reading a script file.
1281 * - When no_u_sync is non-zero.
1282 */
1283 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001284may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001287 && scriptin[curscript] == NULL)
1288 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289}
1290
1291/*
1292 * Make "typebuf" empty and allocate new buffers.
1293 * Returns FAIL when out of memory.
1294 */
1295 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001296alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 typebuf.tb_buf = alloc(TYPELEN_INIT);
1299 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1300 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1301 {
1302 free_typebuf();
1303 return FAIL;
1304 }
1305 typebuf.tb_buflen = TYPELEN_INIT;
1306 typebuf.tb_off = 0;
1307 typebuf.tb_len = 0;
1308 typebuf.tb_maplen = 0;
1309 typebuf.tb_silent = 0;
1310 typebuf.tb_no_abbr_cnt = 0;
1311 if (++typebuf.tb_change_cnt == 0)
1312 typebuf.tb_change_cnt = 1;
1313 return OK;
1314}
1315
1316/*
1317 * Free the buffers of "typebuf".
1318 */
1319 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001320free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001322 if (typebuf.tb_buf == typebuf_init)
1323 EMSG2(_(e_intern2), "Free typebuf 1");
1324 else
1325 vim_free(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001326 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001327 EMSG2(_(e_intern2), "Free typebuf 2");
1328 else
1329 vim_free(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330}
1331
1332/*
1333 * When doing ":so! file", the current typeahead needs to be saved, and
1334 * restored when "file" has been read completely.
1335 */
1336static typebuf_T saved_typebuf[NSCRIPT];
1337
1338 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001339save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
1341 init_typebuf();
1342 saved_typebuf[curscript] = typebuf;
1343 /* If out of memory: restore typebuf and close file. */
1344 if (alloc_typebuf() == FAIL)
1345 {
1346 closescript();
1347 return FAIL;
1348 }
1349 return OK;
1350}
1351
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001352static int old_char = -1; /* character put back by vungetc() */
1353static int old_mod_mask; /* mod_mask for ungotten character */
Bram Moolenaarb8978712013-03-16 21:42:16 +01001354#ifdef FEAT_MOUSE
1355static int old_mouse_row; /* mouse_row related to old_char */
1356static int old_mouse_col; /* mouse_col related to old_char */
1357#endif
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1360
1361/*
1362 * Save all three kinds of typeahead, so that the user must type at a prompt.
1363 */
1364 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001365save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
1367 tp->save_typebuf = typebuf;
1368 tp->typebuf_valid = (alloc_typebuf() == OK);
1369 if (!tp->typebuf_valid)
1370 typebuf = tp->save_typebuf;
1371
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001372 tp->old_char = old_char;
1373 tp->old_mod_mask = old_mod_mask;
1374 old_char = -1;
1375
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001376 tp->save_readbuf1 = readbuf1;
1377 readbuf1.bh_first.b_next = NULL;
1378 tp->save_readbuf2 = readbuf2;
1379 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380# ifdef USE_INPUT_BUF
1381 tp->save_inputbuf = get_input_buf();
1382# endif
1383}
1384
1385/*
1386 * Restore the typeahead to what it was before calling save_typeahead().
1387 * The allocated memory is freed, can only be called once!
1388 */
1389 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001390restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 if (tp->typebuf_valid)
1393 {
1394 free_typebuf();
1395 typebuf = tp->save_typebuf;
1396 }
1397
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001398 old_char = tp->old_char;
1399 old_mod_mask = tp->old_mod_mask;
1400
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001401 free_buff(&readbuf1);
1402 readbuf1 = tp->save_readbuf1;
1403 free_buff(&readbuf2);
1404 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405# ifdef USE_INPUT_BUF
1406 set_input_buf(tp->save_inputbuf);
1407# endif
1408}
1409#endif
1410
1411/*
1412 * Open a new script file for the ":source!" command.
1413 */
1414 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001415openscript(
1416 char_u *name,
1417 int directly) /* when TRUE execute directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 if (curscript + 1 == NSCRIPT)
1420 {
1421 EMSG(_(e_nesting));
1422 return;
1423 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001424#ifdef FEAT_EVAL
1425 if (ignore_script)
1426 /* Not reading from script, also don't open one. Warning message? */
1427 return;
1428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 if (scriptin[curscript] != NULL) /* already reading script */
1431 ++curscript;
1432 /* use NameBuff for expanded name */
1433 expand_env(name, NameBuff, MAXPATHL);
1434 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1435 {
1436 EMSG2(_(e_notopen), name);
1437 if (curscript)
1438 --curscript;
1439 return;
1440 }
1441 if (save_typebuf() == FAIL)
1442 return;
1443
1444 /*
1445 * Execute the commands from the file right now when using ":source!"
1446 * after ":global" or ":argdo" or in a loop. Also when another command
1447 * follows. This means the display won't be updated. Don't do this
1448 * always, "make test" would fail.
1449 */
1450 if (directly)
1451 {
1452 oparg_T oa;
1453 int oldcurscript;
1454 int save_State = State;
1455 int save_restart_edit = restart_edit;
1456 int save_insertmode = p_im;
1457 int save_finish_op = finish_op;
1458 int save_msg_scroll = msg_scroll;
1459
1460 State = NORMAL;
1461 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1462 restart_edit = 0; /* don't go to Insert mode */
1463 p_im = FALSE; /* don't use 'insertmode' */
1464 clear_oparg(&oa);
1465 finish_op = FALSE;
1466
1467 oldcurscript = curscript;
1468 do
1469 {
1470 update_topline_cursor(); /* update cursor position and topline */
1471 normal_cmd(&oa, FALSE); /* execute one command */
1472 vpeekc(); /* check for end of file */
1473 }
1474 while (scriptin[oldcurscript] != NULL);
1475
1476 State = save_State;
1477 msg_scroll = save_msg_scroll;
1478 restart_edit = save_restart_edit;
1479 p_im = save_insertmode;
1480 finish_op = save_finish_op;
1481 }
1482}
1483
1484/*
1485 * Close the currently active input script.
1486 */
1487 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001488closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 free_typebuf();
1491 typebuf = saved_typebuf[curscript];
1492
1493 fclose(scriptin[curscript]);
1494 scriptin[curscript] = NULL;
1495 if (curscript > 0)
1496 --curscript;
1497}
1498
Bram Moolenaarea408852005-06-25 22:49:46 +00001499#if defined(EXITFREE) || defined(PROTO)
1500 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001501close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001502{
1503 while (scriptin[0] != NULL)
1504 closescript();
1505}
1506#endif
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1509/*
1510 * Return TRUE when reading keys from a script file.
1511 */
1512 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001513using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 return scriptin[curscript] != NULL;
1516}
1517#endif
1518
1519/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001520 * This function is called just before doing a blocking wait. Thus after
1521 * waiting 'updatetime' for a character to arrive.
1522 */
1523 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001524before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001525{
1526 updatescript(0);
1527#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001528 if (may_garbage_collect)
1529 garbage_collect();
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001530#endif
1531}
1532
1533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 * updatescipt() is called when a character can be written into the script file
1535 * or when we have waited some time for a character (c == 0)
1536 *
1537 * All the changed memfiles are synced if c == 0 or when the number of typed
1538 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1539 */
1540 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001541updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 static int count = 0;
1544
1545 if (c && scriptout)
1546 putc(c, scriptout);
1547 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1548 {
1549 ml_sync_all(c == 0, TRUE);
1550 count = 0;
1551 }
1552}
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
1555 * Get the next input character.
1556 * Can return a special key or a multi-byte character.
1557 * Can return NUL when called recursively, use safe_vgetc() if that's not
1558 * wanted.
1559 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1560 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001561 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
1563 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001564vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 int c, c2;
1567#ifdef FEAT_MBYTE
1568 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001569 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 int i;
1571#endif
1572
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001573#ifdef FEAT_EVAL
1574 /* Do garbage collection when garbagecollect() was called previously and
1575 * we are now at the toplevel. */
1576 if (may_garbage_collect && want_garbage_collect)
1577 garbage_collect();
1578#endif
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 /*
1581 * If a character was put back with vungetc, it was already processed.
1582 * Return it directly.
1583 */
1584 if (old_char != -1)
1585 {
1586 c = old_char;
1587 old_char = -1;
1588 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001589#ifdef FEAT_MOUSE
1590 mouse_row = old_mouse_row;
1591 mouse_col = old_mouse_col;
1592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001596 mod_mask = 0x0;
1597 last_recorded_len = 0;
1598 for (;;) /* this is done twice if there are modifiers */
1599 {
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001600 int did_inc = FALSE;
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (mod_mask) /* no mapping after modifier has been read */
1603 {
1604 ++no_mapping;
1605 ++allow_keys;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001606 did_inc = TRUE; /* mod_mask may change value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 c = vgetorpeek(TRUE);
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001609 if (did_inc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 --no_mapping;
1612 --allow_keys;
1613 }
1614
1615 /* Get two extra bytes for special keys */
1616 if (c == K_SPECIAL
1617#ifdef FEAT_GUI
1618 || c == CSI
1619#endif
1620 )
1621 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001622 int save_allow_keys = allow_keys;
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 ++no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001625 allow_keys = 0; /* make sure BS is not found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 c2 = vgetorpeek(TRUE); /* no mapping for these chars */
1627 c = vgetorpeek(TRUE);
1628 --no_mapping;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001629 allow_keys = save_allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (c2 == KS_MODIFIER)
1631 {
1632 mod_mask = c;
1633 continue;
1634 }
1635 c = TO_SPECIAL(c2, c);
1636
1637#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
1638 /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1639 * know that a menu was torn off */
1640 if (c == K_TEAROFF)
1641 {
1642 char_u name[200];
1643 int i;
1644
1645 /* get menu path, it ends with a <CR> */
1646 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1647 {
1648 name[i] = c;
1649 if (i < 199)
1650 ++i;
1651 }
1652 name[i] = NUL;
1653 gui_make_tearoff(name);
1654 continue;
1655 }
1656#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001657#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001658 /* GTK: <F10> normally selects the menu, but it's passed until
1659 * here to allow mapping it. Intercept and invoke the GTK
1660 * behavior if it's not mapped. */
1661 if (c == K_F10 && gui.menubar != NULL)
1662 {
1663 gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
1664 continue;
1665 }
1666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#ifdef FEAT_GUI
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001668 /* Handle focus event here, so that the caller doesn't need to
1669 * know about it. Return K_IGNORE so that we loop once (needed if
1670 * 'lazyredraw' is set). */
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001671 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1672 {
1673 ui_focus_change(c == K_FOCUSGAINED);
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001674 c = K_IGNORE;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001675 }
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 /* Translate K_CSI to CSI. The special key is only used to avoid
1678 * it being recognized as the start of a special key. */
1679 if (c == K_CSI)
1680 c = CSI;
1681#endif
1682 }
1683#ifdef MSDOS
1684 /*
1685 * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
1686 * Delete the 3 here.
1687 */
1688 else if (c == K_NUL && vpeekc() == 3)
1689 (void)vgetorpeek(TRUE);
1690#endif
1691
Bram Moolenaara88d9682005-03-25 21:45:43 +00001692 /* a keypad or special function key was not mapped, use it like
1693 * its ASCII equivalent */
1694 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
Bram Moolenaara88d9682005-03-25 21:45:43 +00001696 case K_KPLUS: c = '+'; break;
1697 case K_KMINUS: c = '-'; break;
1698 case K_KDIVIDE: c = '/'; break;
1699 case K_KMULTIPLY: c = '*'; break;
1700 case K_KENTER: c = CAR; break;
1701 case K_KPOINT:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001702#ifdef WIN32
Bram Moolenaara88d9682005-03-25 21:45:43 +00001703 /* Can be either '.' or a ',', *
1704 * depending on the type of keypad. */
1705 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001706#else
Bram Moolenaara88d9682005-03-25 21:45:43 +00001707 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001708#endif
Bram Moolenaara88d9682005-03-25 21:45:43 +00001709 case K_K0: c = '0'; break;
1710 case K_K1: c = '1'; break;
1711 case K_K2: c = '2'; break;
1712 case K_K3: c = '3'; break;
1713 case K_K4: c = '4'; break;
1714 case K_K5: c = '5'; break;
1715 case K_K6: c = '6'; break;
1716 case K_K7: c = '7'; break;
1717 case K_K8: c = '8'; break;
1718 case K_K9: c = '9'; break;
1719
1720 case K_XHOME:
1721 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
1722 {
1723 c = K_S_HOME;
1724 mod_mask = 0;
1725 }
1726 else if (mod_mask == MOD_MASK_CTRL)
1727 {
1728 c = K_C_HOME;
1729 mod_mask = 0;
1730 }
1731 else
1732 c = K_HOME;
1733 break;
1734 case K_XEND:
1735 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
1736 {
1737 c = K_S_END;
1738 mod_mask = 0;
1739 }
1740 else if (mod_mask == MOD_MASK_CTRL)
1741 {
1742 c = K_C_END;
1743 mod_mask = 0;
1744 }
1745 else
1746 c = K_END;
1747 break;
1748
1749 case K_XUP: c = K_UP; break;
1750 case K_XDOWN: c = K_DOWN; break;
1751 case K_XLEFT: c = K_LEFT; break;
1752 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754
1755#ifdef FEAT_MBYTE
1756 /* For a multi-byte character get all the bytes and return the
1757 * converted character.
1758 * Note: This will loop until enough bytes are received!
1759 */
1760 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1761 {
1762 ++no_mapping;
1763 buf[0] = c;
1764 for (i = 1; i < n; ++i)
1765 {
1766 buf[i] = vgetorpeek(TRUE);
1767 if (buf[i] == K_SPECIAL
1768#ifdef FEAT_GUI
1769 || buf[i] == CSI
1770#endif
1771 )
1772 {
1773 /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
1774 * which represents a K_SPECIAL (0x80),
1775 * or a CSI - KS_EXTRA - KE_CSI sequence, which represents
1776 * a CSI (0x9B),
1777 * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */
1778 c = vgetorpeek(TRUE);
1779 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1780 buf[i] = CSI;
1781 }
1782 }
1783 --no_mapping;
1784 c = (*mb_ptr2char)(buf);
1785 }
1786#endif
1787
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001788 break;
1789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001791
1792#ifdef FEAT_EVAL
1793 /*
1794 * In the main loop "may_garbage_collect" can be set to do garbage
1795 * collection in the first next vgetc(). It's disabled after that to
1796 * avoid internally used Lists and Dicts to be freed.
1797 */
1798 may_garbage_collect = FALSE;
1799#endif
1800
1801 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802}
1803
1804/*
1805 * Like vgetc(), but never return a NUL when called recursively, get a key
1806 * directly from the user (ignoring typeahead).
1807 */
1808 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001809safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 int c;
1812
1813 c = vgetc();
1814 if (c == NUL)
1815 c = get_keystroke();
1816 return c;
1817}
1818
1819/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001820 * Like safe_vgetc(), but loop to handle K_IGNORE.
1821 * Also ignore scrollbar events.
1822 */
1823 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001824plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001825{
1826 int c;
1827
1828 do
1829 {
1830 c = safe_vgetc();
1831 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
1832 return c;
1833}
1834
1835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 * Check if a character is available, such that vgetc() will not block.
1837 * If the next character is a special character or multi-byte, the returned
1838 * character is not valid!.
1839 */
1840 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001841vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
1843 if (old_char != -1)
1844 return old_char;
1845 return vgetorpeek(FALSE);
1846}
1847
1848#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1849/*
1850 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1851 * codes.
1852 */
1853 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001854vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 int c;
1857
1858 ++no_mapping;
1859 ++allow_keys;
1860 c = vpeekc();
1861 --no_mapping;
1862 --allow_keys;
1863 return c;
1864}
1865#endif
1866
Bram Moolenaar9a665ba2014-05-22 18:59:58 +02001867#if defined(FEAT_INS_EXPAND) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
1869 * Check if any character is available, also half an escape sequence.
1870 * Trick: when no typeahead found, but there is something in the typeahead
1871 * buffer, it must be an ESC that is recognized as the start of a key code.
1872 */
1873 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001874vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
1876 int c;
1877
1878 c = vpeekc();
1879 if (c == NUL && typebuf.tb_len > 0)
1880 c = ESC;
1881 return c;
1882}
1883#endif
1884
1885/*
1886 * Call vpeekc() without causing anything to be mapped.
1887 * Return TRUE if a character is available, FALSE otherwise.
1888 */
1889 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001890char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int retval;
1893
1894 ++no_mapping;
1895 retval = vpeekc();
1896 --no_mapping;
1897 return (retval != NUL);
1898}
1899
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001900/*
1901 * unget one character (can only be done once!)
1902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001904vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 old_char = c;
1907 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001908#ifdef FEAT_MOUSE
1909 old_mouse_row = mouse_row;
1910 old_mouse_col = mouse_col;
1911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912}
1913
1914/*
1915 * get a character:
1916 * 1. from the stuffbuffer
1917 * This is used for abbreviated commands like "D" -> "d$".
1918 * Also used to redo a command for ".".
1919 * 2. from the typeahead buffer
1920 * Stores text obtained previously but not used yet.
1921 * Also stores the result of mappings.
1922 * Also used for the ":normal" command.
1923 * 3. from the user
1924 * This may do a blocking wait if "advance" is TRUE.
1925 *
1926 * if "advance" is TRUE (vgetc()):
1927 * really get the character.
1928 * KeyTyped is set to TRUE in the case the user typed the key.
1929 * KeyStuffed is TRUE if the character comes from the stuff buffer.
1930 * if "advance" is FALSE (vpeekc()):
1931 * just look whether there is a character available.
1932 *
1933 * When "no_mapping" is zero, checks for mappings in the current mode.
1934 * Only returns one byte (of a multi-byte character).
1935 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
1936 */
1937 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001938vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939{
1940 int c, c1;
1941 int keylen;
1942 char_u *s;
1943 mapblock_T *mp;
1944#ifdef FEAT_LOCALMAP
1945 mapblock_T *mp2;
1946#endif
1947 mapblock_T *mp_match;
1948 int mp_match_len = 0;
1949 int timedout = FALSE; /* waited for more than 1 second
1950 for mapping to complete */
1951 int mapdepth = 0; /* check for recursive mapping */
1952 int mode_deleted = FALSE; /* set when mode has been deleted */
1953 int local_State;
1954 int mlen;
1955 int max_mlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 int i;
Bram Moolenaar4e427192006-03-10 21:34:27 +00001957#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 int new_wcol, new_wrow;
1959#endif
1960#ifdef FEAT_GUI
1961# ifdef FEAT_MENU
1962 int idx;
1963# endif
1964 int shape_changed = FALSE; /* adjusted cursor shape */
1965#endif
1966 int n;
1967#ifdef FEAT_LANGMAP
1968 int nolmaplen;
1969#endif
1970 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00001971 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 /*
1974 * This function doesn't work very well when called recursively. This may
1975 * happen though, because of:
1976 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
1977 * there is a character available, which calls this function. In that
1978 * case we must return NUL, to indicate no character is available.
1979 * 2. A GUI callback function writes to the screen, causing a
1980 * wait_return().
1981 * Using ":normal" can also do this, but it saves the typeahead buffer,
1982 * thus it should be OK. But don't get a key from the user then.
1983 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001984 if (vgetc_busy > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985#ifdef FEAT_EX_EXTRA
1986 && ex_normal_busy == 0
1987#endif
1988 )
1989 return NUL;
1990
1991 local_State = get_real_state();
1992
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001993 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 if (advance)
1996 KeyStuffed = FALSE;
1997
1998 init_typebuf();
1999 start_stuff();
2000 if (advance && typebuf.tb_maplen == 0)
2001 Exec_reg = FALSE;
2002 do
2003 {
2004/*
2005 * get a character: 1. from the stuffbuffer
2006 */
2007 if (typeahead_char != 0)
2008 {
2009 c = typeahead_char;
2010 if (advance)
2011 typeahead_char = 0;
2012 }
2013 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002014 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (c != NUL && !got_int)
2016 {
2017 if (advance)
2018 {
2019 /* KeyTyped = FALSE; When the command that stuffed something
2020 * was typed, behave like the stuffed command was typed.
2021 * needed for CTRL-W CTRl-] to open a fold, for example. */
2022 KeyStuffed = TRUE;
2023 }
2024 if (typebuf.tb_no_abbr_cnt == 0)
2025 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
2026 }
2027 else
2028 {
2029 /*
2030 * Loop until we either find a matching mapped key, or we
2031 * are sure that it is not a mapped key.
2032 * If a mapped key sequence is found we go back to the start to
2033 * try re-mapping.
2034 */
2035 for (;;)
2036 {
2037 /*
2038 * ui_breakcheck() is slow, don't use it too often when
2039 * inside a mapping. But call it each time for typed
2040 * characters.
2041 */
2042 if (typebuf.tb_maplen)
2043 line_breakcheck();
2044 else
2045 ui_breakcheck(); /* check for CTRL-C */
2046 keylen = 0;
2047 if (got_int)
2048 {
2049 /* flush all input */
2050 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L,
2051 typebuf.tb_change_cnt);
2052 /*
2053 * If inchar() returns TRUE (script file was active) or we
2054 * are inside a mapping, get out of insert mode.
2055 * Otherwise we behave like having gotten a CTRL-C.
2056 * As a result typing CTRL-C in insert mode will
2057 * really insert a CTRL-C.
2058 */
2059 if ((c || typebuf.tb_maplen)
2060 && (State & (INSERT + CMDLINE)))
2061 c = ESC;
2062 else
2063 c = Ctrl_C;
2064 flush_buffers(TRUE); /* flush all typeahead */
2065
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002066 if (advance)
2067 {
2068 /* Also record this character, it might be needed to
2069 * get out of Insert mode. */
2070 *typebuf.tb_buf = c;
2071 gotchars(typebuf.tb_buf, 1);
2072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 cmd_silent = FALSE;
2074
2075 break;
2076 }
2077 else if (typebuf.tb_len > 0)
2078 {
2079 /*
2080 * Check for a mappable key sequence.
2081 * Walk through one maphash[] list until we find an
2082 * entry that matches.
2083 *
2084 * Don't look for mappings if:
2085 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2086 * - maphash_valid not set: no mappings present.
2087 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2088 * - in insert or cmdline mode and 'paste' option set
2089 * - waiting for "hit return to continue" and CR or SPACE
2090 * typed
2091 * - waiting for a char with --more--
2092 * - in Ctrl-X mode, and we get a valid char for that mode
2093 */
2094 mp = NULL;
2095 max_mlen = 0;
2096 c1 = typebuf.tb_buf[typebuf.tb_off];
2097 if (no_mapping == 0 && maphash_valid
2098 && (no_zero_mapping == 0 || c1 != '0')
2099 && (typebuf.tb_maplen == 0
2100 || (p_remap
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002101 && (typebuf.tb_noremap[typebuf.tb_off]
2102 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 && !(p_paste && (State & (INSERT + CMDLINE)))
2104 && !(State == HITRETURN && (c1 == CAR || c1 == ' '))
2105 && State != ASKMORE
2106 && State != CONFIRM
2107#ifdef FEAT_INS_EXPAND
2108 && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002109 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 && (c1 == Ctrl_N || c1 == Ctrl_P)))
2111#endif
2112 )
2113 {
2114#ifdef FEAT_LANGMAP
2115 if (c1 == K_SPECIAL)
2116 nolmaplen = 2;
2117 else
2118 {
Bram Moolenaarae94b772015-07-10 17:19:30 +02002119 LANGMAP_ADJUST(c1,
Bram Moolenaar25281632016-01-21 23:32:32 +01002120 (State & (CMDLINE | INSERT)) == 0
2121 && get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 nolmaplen = 0;
2123 }
2124#endif
2125#ifdef FEAT_LOCALMAP
2126 /* First try buffer-local mappings. */
2127 mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
2128 mp2 = maphash[MAP_HASH(local_State, c1)];
2129 if (mp == NULL)
2130 {
Bram Moolenaar72179e12013-06-29 13:58:31 +02002131 /* There are no buffer-local mappings. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 mp = mp2;
2133 mp2 = NULL;
2134 }
2135#else
2136 mp = maphash[MAP_HASH(local_State, c1)];
2137#endif
2138 /*
2139 * Loop until a partly matching mapping is found or
2140 * all (local) mappings have been checked.
2141 * The longest full match is remembered in "mp_match".
2142 * A full match is only accepted if there is no partly
2143 * match, so "aa" and "aaa" can both be mapped.
2144 */
2145 mp_match = NULL;
2146 mp_match_len = 0;
2147 for ( ; mp != NULL;
2148#ifdef FEAT_LOCALMAP
2149 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
2150#endif
2151 (mp = mp->m_next))
2152 {
2153 /*
2154 * Only consider an entry if the first character
2155 * matches and it is for the current state.
2156 * Skip ":lmap" mappings if keys were mapped.
2157 */
2158 if (mp->m_keys[0] == c1
2159 && (mp->m_mode & local_State)
2160 && ((mp->m_mode & LANGMAP) == 0
2161 || typebuf.tb_maplen == 0))
2162 {
2163#ifdef FEAT_LANGMAP
2164 int nomap = nolmaplen;
2165 int c2;
2166#endif
2167 /* find the match length of this mapping */
2168 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2169 {
2170#ifdef FEAT_LANGMAP
2171 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2172 if (nomap > 0)
2173 --nomap;
2174 else if (c2 == K_SPECIAL)
2175 nomap = 2;
2176 else
2177 LANGMAP_ADJUST(c2, TRUE);
2178 if (mp->m_keys[mlen] != c2)
2179#else
2180 if (mp->m_keys[mlen] !=
2181 typebuf.tb_buf[typebuf.tb_off + mlen])
2182#endif
2183 break;
2184 }
2185
2186#ifdef FEAT_MBYTE
2187 /* Don't allow mapping the first byte(s) of a
2188 * multi-byte char. Happens when mapping
Bram Moolenaar1d9ff432014-03-12 20:17:51 +01002189 * <M-a> and then changing 'encoding'. Beware
2190 * that 0x80 is escaped. */
2191 {
2192 char_u *p1 = mp->m_keys;
2193 char_u *p2 = mb_unescape(&p1);
2194
2195 if (has_mbyte && p2 != NULL
2196 && MB_BYTE2LEN(c1) > MB_PTR2LEN(p2))
2197 mlen = 0;
2198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#endif
2200 /*
2201 * Check an entry whether it matches.
2202 * - Full match: mlen == keylen
2203 * - Partly match: mlen == typebuf.tb_len
2204 */
2205 keylen = mp->m_keylen;
2206 if (mlen == keylen
2207 || (mlen == typebuf.tb_len
2208 && typebuf.tb_len < keylen))
2209 {
2210 /*
2211 * If only script-local mappings are
2212 * allowed, check if the mapping starts
2213 * with K_SNR.
2214 */
2215 s = typebuf.tb_noremap + typebuf.tb_off;
2216 if (*s == RM_SCRIPT
2217 && (mp->m_keys[0] != K_SPECIAL
2218 || mp->m_keys[1] != KS_EXTRA
2219 || mp->m_keys[2]
2220 != (int)KE_SNR))
2221 continue;
2222 /*
2223 * If one of the typed keys cannot be
2224 * remapped, skip the entry.
2225 */
2226 for (n = mlen; --n >= 0; )
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002227 if (*s++ & (RM_NONE|RM_ABBR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 break;
2229 if (n >= 0)
2230 continue;
2231
2232 if (keylen > typebuf.tb_len)
2233 {
Bram Moolenaar72179e12013-06-29 13:58:31 +02002234 if (!timedout && !(mp_match != NULL
2235 && mp_match->m_nowait))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 /* break at a partly match */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002238 keylen = KEYLEN_PART_MAP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 break;
2240 }
2241 }
2242 else if (keylen > mp_match_len)
2243 {
2244 /* found a longer match */
2245 mp_match = mp;
2246 mp_match_len = keylen;
2247 }
2248 }
2249 else
2250 /* No match; may have to check for
2251 * termcode at next character. */
2252 if (max_mlen < mlen)
2253 max_mlen = mlen;
2254 }
2255 }
2256
2257 /* If no partly match found, use the longest full
2258 * match. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002259 if (keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 mp = mp_match;
2262 keylen = mp_match_len;
2263 }
2264 }
2265
2266 /* Check for match with 'pastetoggle' */
2267 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2268 {
2269 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2270 ++mlen)
2271 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2272 + mlen])
2273 break;
2274 if (p_pt[mlen] == NUL) /* match */
2275 {
2276 /* write chars to script file(s) */
2277 if (mlen > typebuf.tb_maplen)
2278 gotchars(typebuf.tb_buf + typebuf.tb_off
2279 + typebuf.tb_maplen,
2280 mlen - typebuf.tb_maplen);
2281
2282 del_typebuf(mlen, 0); /* remove the chars */
2283 set_option_value((char_u *)"paste",
2284 (long)!p_paste, NULL, 0);
2285 if (!(State & INSERT))
2286 {
2287 msg_col = 0;
2288 msg_row = Rows - 1;
2289 msg_clr_eos(); /* clear ruler */
2290 }
Bram Moolenaar06811f32014-02-15 16:17:07 +01002291#ifdef FEAT_WINDOWS
2292 status_redraw_all();
2293 redraw_statuslines();
2294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 showmode();
2296 setcursor();
2297 continue;
2298 }
2299 /* Need more chars for partly match. */
2300 if (mlen == typebuf.tb_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002301 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 else if (max_mlen < mlen)
2303 /* no match, may have to check for termcode at
2304 * next character */
2305 max_mlen = mlen + 1;
2306 }
2307
2308 if ((mp == NULL || max_mlen >= mp_match_len)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002309 && keylen != KEYLEN_PART_MAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002311 int save_keylen = keylen;
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 /*
2314 * When no matching mapping found or found a
2315 * non-matching mapping that matches at least what the
2316 * matching mapping matched:
2317 * Check if we have a terminal code, when:
2318 * mapping is allowed,
2319 * keys have not been mapped,
2320 * and not an ESC sequence, not in insert mode or
2321 * p_ek is on,
2322 * and when not timed out,
2323 */
2324 if ((no_mapping == 0 || allow_keys != 0)
2325 && (typebuf.tb_maplen == 0
2326 || (p_remap && typebuf.tb_noremap[
2327 typebuf.tb_off] == RM_YES))
2328 && !timedout)
2329 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01002330 keylen = check_termcode(max_mlen + 1,
2331 NULL, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
Bram Moolenaarf2330482008-06-24 20:19:36 +00002333 /* If no termcode matched but 'pastetoggle'
2334 * matched partially it's like an incomplete key
2335 * sequence. */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002336 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2337 keylen = KEYLEN_PART_KEY;
Bram Moolenaarf2330482008-06-24 20:19:36 +00002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /*
2340 * When getting a partial match, but the last
2341 * characters were not typed, don't wait for a
2342 * typed character to complete the termcode.
2343 * This helps a lot when a ":normal" command ends
2344 * in an ESC.
2345 */
2346 if (keylen < 0
2347 && typebuf.tb_len == typebuf.tb_maplen)
2348 keylen = 0;
2349 }
2350 else
2351 keylen = 0;
2352 if (keylen == 0) /* no matching terminal code */
2353 {
2354#ifdef AMIGA /* check for window bounds report */
2355 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2356 typebuf.tb_off] & 0xff) == CSI)
2357 {
2358 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2359 s < typebuf.tb_buf + typebuf.tb_off
2360 + typebuf.tb_len
2361 && (VIM_ISDIGIT(*s) || *s == ';'
2362 || *s == ' ');
2363 ++s)
2364 ;
2365 if (*s == 'r' || *s == '|') /* found one */
2366 {
2367 del_typebuf((int)(s + 1 -
2368 (typebuf.tb_buf + typebuf.tb_off)), 0);
2369 /* get size and redraw screen */
2370 shell_resized();
2371 continue;
2372 }
2373 if (*s == NUL) /* need more characters */
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002374 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376 if (keylen >= 0)
2377#endif
2378 /* When there was a matching mapping and no
2379 * termcode could be replaced after another one,
Bram Moolenaarf2330482008-06-24 20:19:36 +00002380 * use that mapping (loop around). If there was
2381 * no mapping use the character from the
2382 * typeahead buffer right here. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (mp == NULL)
2384 {
2385/*
2386 * get a character: 2. from the typeahead buffer
2387 */
2388 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2389 if (advance) /* remove chars from tb_buf */
2390 {
2391 cmd_silent = (typebuf.tb_silent > 0);
2392 if (typebuf.tb_maplen > 0)
2393 KeyTyped = FALSE;
2394 else
2395 {
2396 KeyTyped = TRUE;
2397 /* write char to script file(s) */
2398 gotchars(typebuf.tb_buf
2399 + typebuf.tb_off, 1);
2400 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00002401 KeyNoremap = typebuf.tb_noremap[
2402 typebuf.tb_off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 del_typebuf(1, 0);
2404 }
2405 break; /* got character, break for loop */
2406 }
2407 }
2408 if (keylen > 0) /* full matching terminal code */
2409 {
2410#if defined(FEAT_GUI) && defined(FEAT_MENU)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002411 if (typebuf.tb_len >= 2
2412 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 && typebuf.tb_buf[typebuf.tb_off + 1]
2414 == KS_MENU)
2415 {
2416 /*
2417 * Using a menu may cause a break in undo!
2418 * It's like using gotchars(), but without
2419 * recording or writing to a script file.
2420 */
2421 may_sync_undo();
2422 del_typebuf(3, 0);
2423 idx = get_menu_index(current_menu, local_State);
2424 if (idx != MENU_INDEX_INVALID)
2425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002427 * In Select mode and a Visual mode menu
2428 * is used: Switch to Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 * temporarily. Append K_SELECT to switch
2430 * back to Select mode.
2431 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002432 if (VIsual_active && VIsual_select
2433 && (current_menu->modes & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 VIsual_select = FALSE;
2436 (void)ins_typebuf(K_SELECT_STRING,
2437 REMAP_NONE, 0, TRUE, FALSE);
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 ins_typebuf(current_menu->strings[idx],
2440 current_menu->noremap[idx],
2441 0, TRUE,
2442 current_menu->silent[idx]);
2443 }
2444 }
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002445#endif /* FEAT_GUI && FEAT_MENU */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 continue; /* try mapping again */
2447 }
2448
2449 /* Partial match: get some more characters. When a
2450 * matching mapping was found use that one. */
2451 if (mp == NULL || keylen < 0)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002452 keylen = KEYLEN_PART_KEY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else
2454 keylen = mp_match_len;
2455 }
2456
2457 /* complete match */
2458 if (keylen >= 0 && keylen <= typebuf.tb_len)
2459 {
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002460#ifdef FEAT_EVAL
2461 int save_m_expr;
2462 int save_m_noremap;
2463 int save_m_silent;
2464 char_u *save_m_keys;
2465 char_u *save_m_str;
2466#else
2467# define save_m_noremap mp->m_noremap
2468# define save_m_silent mp->m_silent
2469#endif
2470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 /* write chars to script file(s) */
2472 if (keylen > typebuf.tb_maplen)
2473 gotchars(typebuf.tb_buf + typebuf.tb_off
2474 + typebuf.tb_maplen,
2475 keylen - typebuf.tb_maplen);
2476
2477 cmd_silent = (typebuf.tb_silent > 0);
2478 del_typebuf(keylen, 0); /* remove the mapped keys */
2479
2480 /*
2481 * Put the replacement string in front of mapstr.
2482 * The depth check catches ":map x y" and ":map y x".
2483 */
2484 if (++mapdepth >= p_mmd)
2485 {
2486 EMSG(_("E223: recursive mapping"));
2487 if (State & CMDLINE)
2488 redrawcmdline();
2489 else
2490 setcursor();
2491 flush_buffers(FALSE);
2492 mapdepth = 0; /* for next one */
2493 c = -1;
2494 break;
2495 }
2496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 /*
Bram Moolenaar371d5402006-03-20 21:47:49 +00002498 * In Select mode and a Visual mode mapping is used:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * Switch to Visual mode temporarily. Append K_SELECT
2500 * to switch back to Select mode.
2501 */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002502 if (VIsual_active && VIsual_select
2503 && (mp->m_mode & VISUAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 VIsual_select = FALSE;
2506 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2507 0, TRUE, FALSE);
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar4e427192006-03-10 21:34:27 +00002510#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002511 /* Copy the values from *mp that are used, because
2512 * evaluating the expression may invoke a function
2513 * that redefines the mapping, thereby making *mp
2514 * invalid. */
2515 save_m_expr = mp->m_expr;
2516 save_m_noremap = mp->m_noremap;
2517 save_m_silent = mp->m_silent;
2518 save_m_keys = NULL; /* only saved when needed */
2519 save_m_str = NULL; /* only saved when needed */
2520
Bram Moolenaar4e427192006-03-10 21:34:27 +00002521 /*
2522 * Handle ":map <expr>": evaluate the {rhs} as an
Bram Moolenaarb3479bd2011-10-12 22:02:14 +02002523 * expression. Also save and restore the command line
2524 * for "normal :".
Bram Moolenaar4e427192006-03-10 21:34:27 +00002525 */
2526 if (mp->m_expr)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002527 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002528 int save_vgetc_busy = vgetc_busy;
2529
Bram Moolenaarb3479bd2011-10-12 22:02:14 +02002530 vgetc_busy = 0;
2531 save_m_keys = vim_strsave(mp->m_keys);
2532 save_m_str = vim_strsave(mp->m_str);
2533 s = eval_map_expr(save_m_str, NUL);
2534 vgetc_busy = save_vgetc_busy;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002535 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00002536 else
2537#endif
2538 s = mp->m_str;
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 /*
2541 * Insert the 'to' part in the typebuf.tb_buf.
2542 * If 'from' field is the same as the start of the
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002543 * 'to' field, don't remap the first character (but do
2544 * allow abbreviations).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 * If m_noremap is set, don't remap the whole 'to'
2546 * part.
2547 */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002548 if (s == NULL)
2549 i = FAIL;
2550 else
2551 {
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002552 int noremap;
2553
2554 if (save_m_noremap != REMAP_YES)
2555 noremap = save_m_noremap;
2556 else if (
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002557#ifdef FEAT_EVAL
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002558 STRNCMP(s, save_m_keys != NULL
2559 ? save_m_keys : mp->m_keys,
2560 (size_t)keylen)
2561#else
2562 STRNCMP(s, mp->m_keys, (size_t)keylen)
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002563#endif
Bram Moolenaard21d9a62010-01-28 22:58:16 +01002564 != 0)
2565 noremap = REMAP_YES;
2566 else
2567 noremap = REMAP_SKIP;
2568 i = ins_typebuf(s, noremap,
2569 0, TRUE, cmd_silent || save_m_silent);
Bram Moolenaar4e427192006-03-10 21:34:27 +00002570#ifdef FEAT_EVAL
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002571 if (save_m_expr)
Bram Moolenaar4e427192006-03-10 21:34:27 +00002572 vim_free(s);
2573#endif
2574 }
Bram Moolenaar0dbf7202010-01-27 17:31:43 +01002575#ifdef FEAT_EVAL
2576 vim_free(save_m_keys);
2577 vim_free(save_m_str);
2578#endif
Bram Moolenaar4e427192006-03-10 21:34:27 +00002579 if (i == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 c = -1;
2582 break;
2583 }
2584 continue;
2585 }
2586 }
2587
2588/*
2589 * get a character: 3. from the user - handle <Esc> in Insert mode
2590 */
2591 /*
2592 * special case: if we get an <ESC> in insert mode and there
2593 * are no more characters at once, we pretend to go out of
2594 * insert mode. This prevents the one second delay after
2595 * typing an <ESC>. If we get something after all, we may
2596 * have to redisplay the mode. That the cursor is in the wrong
2597 * place does not matter.
2598 */
2599 c = 0;
2600#ifdef FEAT_CMDL_INFO
2601 new_wcol = curwin->w_wcol;
2602 new_wrow = curwin->w_wrow;
2603#endif
2604 if ( advance
2605 && typebuf.tb_len == 1
2606 && typebuf.tb_buf[typebuf.tb_off] == ESC
2607 && !no_mapping
2608#ifdef FEAT_EX_EXTRA
2609 && ex_normal_busy == 0
2610#endif
2611 && typebuf.tb_maplen == 0
2612 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002613 && (p_timeout
2614 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
2616 + typebuf.tb_len, 3, 25L,
2617 typebuf.tb_change_cnt)) == 0)
2618 {
2619 colnr_T col = 0, vcol;
2620 char_u *ptr;
2621
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002622 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
2624 unshowmode(TRUE);
2625 mode_deleted = TRUE;
2626 }
2627#ifdef FEAT_GUI
2628 /* may show different cursor shape */
2629 if (gui.in_use)
2630 {
2631 int save_State;
2632
2633 save_State = State;
2634 State = NORMAL;
2635 gui_update_cursor(TRUE, FALSE);
2636 State = save_State;
2637 shape_changed = TRUE;
2638 }
2639#endif
2640 validate_cursor();
2641 old_wcol = curwin->w_wcol;
2642 old_wrow = curwin->w_wrow;
2643
2644 /* move cursor left, if possible */
2645 if (curwin->w_cursor.col != 0)
2646 {
2647 if (curwin->w_wcol > 0)
2648 {
2649 if (did_ai)
2650 {
2651 /*
2652 * We are expecting to truncate the trailing
2653 * white-space, so find the last non-white
2654 * character -- webb
2655 */
2656 col = vcol = curwin->w_wcol = 0;
2657 ptr = ml_get_curline();
2658 while (col < curwin->w_cursor.col)
2659 {
2660 if (!vim_iswhite(ptr[col]))
2661 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002662 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 (colnr_T)vcol);
2664#ifdef FEAT_MBYTE
2665 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002666 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 else
2668#endif
2669 ++col;
2670 }
2671 curwin->w_wrow = curwin->w_cline_row
2672 + curwin->w_wcol / W_WIDTH(curwin);
2673 curwin->w_wcol %= W_WIDTH(curwin);
2674 curwin->w_wcol += curwin_col_off();
2675#ifdef FEAT_MBYTE
2676 col = 0; /* no correction needed */
2677#endif
2678 }
2679 else
2680 {
2681 --curwin->w_wcol;
2682#ifdef FEAT_MBYTE
2683 col = curwin->w_cursor.col - 1;
2684#endif
2685 }
2686 }
2687 else if (curwin->w_p_wrap && curwin->w_wrow)
2688 {
2689 --curwin->w_wrow;
2690 curwin->w_wcol = W_WIDTH(curwin) - 1;
2691#ifdef FEAT_MBYTE
2692 col = curwin->w_cursor.col - 1;
2693#endif
2694 }
2695#ifdef FEAT_MBYTE
2696 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2697 {
2698 /* Correct when the cursor is on the right halve
2699 * of a double-wide character. */
2700 ptr = ml_get_curline();
2701 col -= (*mb_head_off)(ptr, ptr + col);
2702 if ((*mb_ptr2cells)(ptr + col) > 1)
2703 --curwin->w_wcol;
2704 }
2705#endif
2706 }
2707 setcursor();
2708 out_flush();
2709#ifdef FEAT_CMDL_INFO
2710 new_wcol = curwin->w_wcol;
2711 new_wrow = curwin->w_wrow;
2712#endif
2713 curwin->w_wcol = old_wcol;
2714 curwin->w_wrow = old_wrow;
2715 }
2716 if (c < 0)
2717 continue; /* end of input script reached */
Bram Moolenaar20c38922014-07-23 20:41:14 +02002718
2719 /* Allow mapping for just typed characters. When we get here c
2720 * is the number of extra bytes and typebuf.tb_len is 1. */
2721 for (n = 1; n <= c; ++n)
2722 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 typebuf.tb_len += c;
2724
2725 /* buffer full, don't map */
2726 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2727 {
2728 timedout = TRUE;
2729 continue;
2730 }
2731
2732#ifdef FEAT_EX_EXTRA
2733 if (ex_normal_busy > 0)
2734 {
2735# ifdef FEAT_CMDWIN
2736 static int tc = 0;
2737# endif
2738
2739 /* No typeahead left and inside ":normal". Must return
2740 * something to avoid getting stuck. When an incomplete
2741 * mapping is present, behave like it timed out. */
2742 if (typebuf.tb_len > 0)
2743 {
2744 timedout = TRUE;
2745 continue;
2746 }
2747 /* When 'insertmode' is set, ESC just beeps in Insert
2748 * mode. Use CTRL-L to make edit() return.
2749 * For the command line only CTRL-C always breaks it.
2750 * For the cmdline window: Alternate between ESC and
2751 * CTRL-C: ESC for most situations and CTRL-C to close the
2752 * cmdline window. */
2753 if (p_im && (State & INSERT))
2754 c = Ctrl_L;
2755 else if ((State & CMDLINE)
2756# ifdef FEAT_CMDWIN
2757 || (cmdwin_type > 0 && tc == ESC)
2758# endif
2759 )
2760 c = Ctrl_C;
2761 else
2762 c = ESC;
2763# ifdef FEAT_CMDWIN
2764 tc = c;
2765# endif
2766 break;
2767 }
2768#endif
2769
2770/*
2771 * get a character: 3. from the user - update display
2772 */
2773 /* In insert mode a screen update is skipped when characters
2774 * are still available. But when those available characters
2775 * are part of a mapping, and we are going to do a blocking
2776 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01002777 * changed text so far. Also for when 'lazyredraw' is set and
2778 * redrawing was postponed because there was something in the
2779 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01002780 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
2781 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
2783 update_screen(0);
2784 setcursor(); /* put cursor back where it belongs */
2785 }
2786
2787 /*
2788 * If we have a partial match (and are going to wait for more
2789 * input from the user), show the partially matched characters
2790 * to the user with showcmd.
2791 */
2792#ifdef FEAT_CMDL_INFO
2793 i = 0;
2794#endif
2795 c1 = 0;
2796 if (typebuf.tb_len > 0 && advance && !exmode_active)
2797 {
2798 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2799 && State != HITRETURN)
2800 {
2801 /* this looks nice when typing a dead character map */
2802 if (State & INSERT
2803 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2804 + typebuf.tb_len - 1) == 1)
2805 {
2806 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2807 + typebuf.tb_len - 1], FALSE);
2808 setcursor(); /* put cursor back where it belongs */
2809 c1 = 1;
2810 }
2811#ifdef FEAT_CMDL_INFO
2812 /* need to use the col and row from above here */
2813 old_wcol = curwin->w_wcol;
2814 old_wrow = curwin->w_wrow;
2815 curwin->w_wcol = new_wcol;
2816 curwin->w_wrow = new_wrow;
2817 push_showcmd();
2818 if (typebuf.tb_len > SHOWCMD_COLS)
2819 i = typebuf.tb_len - SHOWCMD_COLS;
2820 while (i < typebuf.tb_len)
2821 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2822 + i++]);
2823 curwin->w_wcol = old_wcol;
2824 curwin->w_wrow = old_wrow;
2825#endif
2826 }
2827
2828 /* this looks nice when typing a dead character map */
2829 if ((State & CMDLINE)
2830#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2831 && cmdline_star == 0
2832#endif
2833 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2834 + typebuf.tb_len - 1) == 1)
2835 {
2836 putcmdline(typebuf.tb_buf[typebuf.tb_off
2837 + typebuf.tb_len - 1], FALSE);
2838 c1 = 1;
2839 }
2840 }
2841
2842/*
2843 * get a character: 3. from the user - get it
2844 */
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002845 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2847 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
2848 !advance
2849 ? 0
2850 : ((typebuf.tb_len == 0
2851 || !(p_timeout || (p_ttimeout
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002852 && keylen == KEYLEN_PART_KEY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 ? -1L
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002854 : ((keylen == KEYLEN_PART_KEY && p_ttm >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 ? p_ttm
2856 : p_tm)), typebuf.tb_change_cnt);
2857
2858#ifdef FEAT_CMDL_INFO
2859 if (i != 0)
2860 pop_showcmd();
2861#endif
2862 if (c1 == 1)
2863 {
2864 if (State & INSERT)
2865 edit_unputchar();
2866 if (State & CMDLINE)
2867 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02002868 else
2869 setcursor(); /* put cursor back where it belongs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871
2872 if (c < 0)
2873 continue; /* end of input script reached */
2874 if (c == NUL) /* no character available */
2875 {
2876 if (!advance)
2877 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002878 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 timedout = TRUE;
2881 continue;
2882 }
2883 }
2884 else
2885 { /* allow mapping for just typed characters */
2886 while (typebuf.tb_buf[typebuf.tb_off
2887 + typebuf.tb_len] != NUL)
2888 typebuf.tb_noremap[typebuf.tb_off
2889 + typebuf.tb_len++] = RM_YES;
2890#ifdef USE_IM_CONTROL
2891 /* Get IM status right after getting keys, not after the
2892 * timeout for a mapping (focus may be lost by then). */
2893 vgetc_im_active = im_get_status();
2894#endif
2895 }
2896 } /* for (;;) */
2897 } /* if (!character from stuffbuf) */
2898
2899 /* if advance is FALSE don't loop on NULs */
2900 } while (c < 0 || (advance && c == NUL));
2901
2902 /*
2903 * The "INSERT" message is taken care of here:
2904 * if we return an ESC to exit insert mode, the message is deleted
2905 * if we don't return an ESC but deleted the message before, redisplay it
2906 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002907 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002909 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 if (typebuf.tb_len && !KeyTyped)
2912 redraw_cmdline = TRUE; /* delete mode later */
2913 else
2914 unshowmode(FALSE);
2915 }
2916 else if (c != ESC && mode_deleted)
2917 {
2918 if (typebuf.tb_len && !KeyTyped)
2919 redraw_cmdline = TRUE; /* show mode later */
2920 else
2921 showmode();
2922 }
2923 }
2924#ifdef FEAT_GUI
2925 /* may unshow different cursor shape */
2926 if (gui.in_use && shape_changed)
2927 gui_update_cursor(TRUE, FALSE);
2928#endif
2929
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002930 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 return c;
2933}
2934
2935/*
2936 * inchar() - get one character from
2937 * 1. a scriptfile
2938 * 2. the keyboard
2939 *
2940 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2941 * NUL terminated (buffer length must be 'maxlen' + 1).
2942 * Minimum for "maxlen" is 3!!!!
2943 *
2944 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2945 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2946 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2947 * otherwise.
2948 *
2949 * If we got an interrupt all input is read until none is available.
2950 *
2951 * If wait_time == 0 there is no waiting for the char.
2952 * If wait_time == n we wait for n msec for a character to arrive.
2953 * If wait_time == -1 we wait forever for a character to arrive.
2954 *
2955 * Return the number of obtained characters.
2956 * Return -1 when end of input script reached.
2957 */
2958 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002959inchar(
2960 char_u *buf,
2961 int maxlen,
2962 long wait_time, /* milli seconds */
2963 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 int len = 0; /* init for GCC */
2966 int retesc = FALSE; /* return ESC with gotint */
2967 int script_char;
2968
2969 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2970 {
2971 cursor_on();
2972 out_flush();
2973#ifdef FEAT_GUI
2974 if (gui.in_use)
2975 {
2976 gui_update_cursor(FALSE, FALSE);
2977# ifdef FEAT_MOUSESHAPE
2978 if (postponed_mouseshape)
2979 update_mouseshape(-1);
2980# endif
2981 }
2982#endif
2983 }
2984
2985 /*
2986 * Don't reset these when at the hit-return prompt, otherwise a endless
2987 * recursive loop may result (write error in swapfile, hit-return, timeout
2988 * on char wait, flush swapfile, write error....).
2989 */
2990 if (State != HITRETURN)
2991 {
2992 did_outofmem_msg = FALSE; /* display out of memory message (again) */
2993 did_swapwrite_msg = FALSE; /* display swap file write error again */
2994 }
2995 undo_off = FALSE; /* restart undo now */
2996
2997 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00002998 * Get a character from a script file if there is one.
2999 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 */
3001 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003002 while (scriptin[curscript] != NULL && script_char < 0
3003#ifdef FEAT_EVAL
3004 && !ignore_script
3005#endif
3006 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003008
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003009#ifdef MESSAGE_QUEUE
3010 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003011#endif
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3014 {
3015 /* Reached EOF.
3016 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3017 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
3018 closescript();
3019 /*
3020 * When reading script file is interrupted, return an ESC to get
3021 * back to normal mode.
3022 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3023 */
3024 if (got_int)
3025 retesc = TRUE;
3026 else
3027 return -1;
3028 }
3029 else
3030 {
3031 buf[0] = script_char;
3032 len = 1;
3033 }
3034 }
3035
3036 if (script_char < 0) /* did not get a character from script */
3037 {
3038 /*
3039 * If we got an interrupt, skip all previously typed characters and
3040 * return TRUE if quit reading script file.
3041 * Stop reading typeahead when a single CTRL-C was read,
3042 * fill_input_buf() returns this when not able to read from stdin.
3043 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3044 * and buf may be pointing inside typebuf.tb_buf[].
3045 */
3046 if (got_int)
3047 {
3048#define DUM_LEN MAXMAPLEN * 3 + 3
3049 char_u dum[DUM_LEN + 1];
3050
3051 for (;;)
3052 {
3053 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3054 if (len == 0 || (len == 1 && dum[0] == 3))
3055 break;
3056 }
3057 return retesc;
3058 }
3059
3060 /*
3061 * Always flush the output characters when getting input characters
3062 * from the user.
3063 */
3064 out_flush();
3065
3066 /*
3067 * Fill up to a third of the buffer, because each character may be
3068 * tripled below.
3069 */
3070 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3071 }
3072
3073 if (typebuf_changed(tb_change_cnt))
3074 return 0;
3075
3076 return fix_input_buffer(buf, len, script_char >= 0);
3077}
3078
3079/*
3080 * Fix typed characters for use by vgetc() and check_termcode().
3081 * buf[] must have room to triple the number of bytes!
3082 * Returns the new length.
3083 */
3084 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003085fix_input_buffer(
3086 char_u *buf,
3087 int len,
3088 int script) /* TRUE when reading from a script */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 int i;
3091 char_u *p = buf;
3092
3093 /*
3094 * Two characters are special: NUL and K_SPECIAL.
3095 * When compiled With the GUI CSI is also special.
3096 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3097 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3098 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
3099 * Don't replace K_SPECIAL when reading a script file.
3100 */
3101 for (i = len; --i >= 0; ++p)
3102 {
3103#ifdef FEAT_GUI
3104 /* When the GUI is used any character can come after a CSI, don't
3105 * escape it. */
3106 if (gui.in_use && p[0] == CSI && i >= 2)
3107 {
3108 p += 2;
3109 i -= 2;
3110 }
3111 /* When the GUI is not used CSI needs to be escaped. */
3112 else if (!gui.in_use && p[0] == CSI)
3113 {
3114 mch_memmove(p + 3, p + 1, (size_t)i);
3115 *p++ = K_SPECIAL;
3116 *p++ = KS_EXTRA;
3117 *p = (int)KE_CSI;
3118 len += 2;
3119 }
3120 else
3121#endif
3122 if (p[0] == NUL || (p[0] == K_SPECIAL && !script
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003123#ifdef FEAT_AUTOCMD
3124 /* timeout may generate K_CURSORHOLD */
3125 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
3126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#if defined(WIN3264) && !defined(FEAT_GUI)
3128 /* Win32 console passes modifiers */
3129 && (i < 2 || p[1] != KS_MODIFIER)
3130#endif
3131 ))
3132 {
3133 mch_memmove(p + 3, p + 1, (size_t)i);
3134 p[2] = K_THIRD(p[0]);
3135 p[1] = K_SECOND(p[0]);
3136 p[0] = K_SPECIAL;
3137 p += 2;
3138 len += 2;
3139 }
3140 }
3141 *p = NUL; /* add trailing NUL */
3142 return len;
3143}
3144
3145#if defined(USE_INPUT_BUF) || defined(PROTO)
3146/*
3147 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3148 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003149 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003150 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 */
3152 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003153input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003156# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3157 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158# endif
3159 );
3160}
3161#endif
3162
3163/*
3164 * map[!] : show all key mappings
3165 * map[!] {lhs} : show key mapping for {lhs}
3166 * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
3167 * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
3168 * unmap[!] {lhs} : remove key mapping for {lhs}
3169 * abbr : show all abbreviations
3170 * abbr {lhs} : show abbreviations for {lhs}
3171 * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
3172 * noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
3173 * unabbr {lhs} : remove abbreviation for {lhs}
3174 *
3175 * maptype: 0 for :map, 1 for :unmap, 2 for noremap.
3176 *
3177 * arg is pointer to any arguments. Note: arg cannot be a read-only string,
3178 * it will be modified.
3179 *
Bram Moolenaar371d5402006-03-20 21:47:49 +00003180 * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 * for :map! mode is INSERT + CMDLINE
3182 * for :cmap mode is CMDLINE
3183 * for :imap mode is INSERT
3184 * for :lmap mode is LANGMAP
3185 * for :nmap mode is NORMAL
Bram Moolenaar371d5402006-03-20 21:47:49 +00003186 * for :vmap mode is VISUAL + SELECTMODE
3187 * for :xmap mode is VISUAL
3188 * for :smap mode is SELECTMODE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 * for :omap mode is OP_PENDING
3190 *
3191 * for :abbr mode is INSERT + CMDLINE
3192 * for :iabbr mode is INSERT
3193 * for :cabbr mode is CMDLINE
3194 *
3195 * Return 0 for success
3196 * 1 for invalid arguments
3197 * 2 for no match
3198 * 4 for out of mem
3199 * 5 for entry not unique
3200 */
3201 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003202do_map(
3203 int maptype,
3204 char_u *arg,
3205 int mode,
3206 int abbrev) /* not a mapping but an abbreviation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 char_u *keys;
3209 mapblock_T *mp, **mpp;
3210 char_u *rhs;
3211 char_u *p;
3212 int n;
3213 int len = 0; /* init for GCC */
3214 char_u *newstr;
3215 int hasarg;
3216 int haskey;
3217 int did_it = FALSE;
3218#ifdef FEAT_LOCALMAP
3219 int did_local = FALSE;
3220#endif
3221 int round;
3222 char_u *keys_buf = NULL;
3223 char_u *arg_buf = NULL;
3224 int retval = 0;
3225 int do_backslash;
3226 int hash;
3227 int new_hash;
3228 mapblock_T **abbr_table;
3229 mapblock_T **map_table;
3230 int unique = FALSE;
Bram Moolenaar72179e12013-06-29 13:58:31 +02003231 int nowait = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 int silent = FALSE;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003233 int special = FALSE;
Bram Moolenaar4e427192006-03-10 21:34:27 +00003234#ifdef FEAT_EVAL
3235 int expr = FALSE;
3236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 int noremap;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003238 char_u *orig_rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 keys = arg;
3241 map_table = maphash;
3242 abbr_table = &first_abbr;
3243
3244 /* For ":noremap" don't remap, otherwise do remap. */
3245 if (maptype == 2)
3246 noremap = REMAP_NONE;
3247 else
3248 noremap = REMAP_YES;
3249
Bram Moolenaar72179e12013-06-29 13:58:31 +02003250 /* Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in
3251 * any order. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 for (;;)
3253 {
3254#ifdef FEAT_LOCALMAP
3255 /*
3256 * Check for "<buffer>": mapping local to buffer.
3257 */
3258 if (STRNCMP(keys, "<buffer>", 8) == 0)
3259 {
3260 keys = skipwhite(keys + 8);
3261 map_table = curbuf->b_maphash;
3262 abbr_table = &curbuf->b_first_abbr;
3263 continue;
3264 }
3265#endif
3266
3267 /*
Bram Moolenaar72179e12013-06-29 13:58:31 +02003268 * Check for "<nowait>": don't wait for more characters.
3269 */
3270 if (STRNCMP(keys, "<nowait>", 8) == 0)
3271 {
3272 keys = skipwhite(keys + 8);
3273 nowait = TRUE;
3274 continue;
3275 }
3276
3277 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 * Check for "<silent>": don't echo commands.
3279 */
3280 if (STRNCMP(keys, "<silent>", 8) == 0)
3281 {
3282 keys = skipwhite(keys + 8);
3283 silent = TRUE;
3284 continue;
3285 }
3286
Bram Moolenaar9c102382006-05-03 21:26:49 +00003287 /*
3288 * Check for "<special>": accept special keys in <>
3289 */
3290 if (STRNCMP(keys, "<special>", 9) == 0)
3291 {
3292 keys = skipwhite(keys + 9);
3293 special = TRUE;
3294 continue;
3295 }
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#ifdef FEAT_EVAL
3298 /*
3299 * Check for "<script>": remap script-local mappings only
3300 */
3301 if (STRNCMP(keys, "<script>", 8) == 0)
3302 {
3303 keys = skipwhite(keys + 8);
3304 noremap = REMAP_SCRIPT;
3305 continue;
3306 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00003307
3308 /*
3309 * Check for "<expr>": {rhs} is an expression.
3310 */
3311 if (STRNCMP(keys, "<expr>", 6) == 0)
3312 {
3313 keys = skipwhite(keys + 6);
3314 expr = TRUE;
3315 continue;
3316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#endif
3318 /*
3319 * Check for "<unique>": don't overwrite an existing mapping.
3320 */
3321 if (STRNCMP(keys, "<unique>", 8) == 0)
3322 {
3323 keys = skipwhite(keys + 8);
3324 unique = TRUE;
3325 continue;
3326 }
3327 break;
3328 }
3329
3330 validate_maphash();
3331
3332 /*
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003333 * Find end of keys and skip CTRL-Vs (and backslashes) in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02003335 * with :unmap white space is included in the keys, no argument possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 */
3337 p = keys;
3338 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
3339 while (*p && (maptype == 1 || !vim_iswhite(*p)))
3340 {
3341 if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
3342 p[1] != NUL)
3343 ++p; /* skip CTRL-V or backslash */
3344 ++p;
3345 }
3346 if (*p != NUL)
3347 *p++ = NUL;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 p = skipwhite(p);
3350 rhs = p;
3351 hasarg = (*rhs != NUL);
3352 haskey = (*keys != NUL);
3353
3354 /* check for :unmap without argument */
3355 if (maptype == 1 && !haskey)
3356 {
3357 retval = 1;
3358 goto theend;
3359 }
3360
3361 /*
3362 * If mapping has been given as ^V<C_UP> say, then replace the term codes
3363 * with the appropriate two bytes. If it is a shifted special key, unshift
3364 * it too, giving another two bytes.
3365 * replace_termcodes() may move the result to allocated memory, which
3366 * needs to be freed later (*keys_buf and *arg_buf).
3367 * replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
3368 */
3369 if (haskey)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003370 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
Bram Moolenaarb3ae56c2010-10-27 17:39:05 +02003371 orig_rhs = rhs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (hasarg)
3373 {
3374 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */
3375 rhs = (char_u *)"";
3376 else
Bram Moolenaar9c102382006-05-03 21:26:49 +00003377 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379
3380#ifdef FEAT_FKMAP
3381 /*
Bram Moolenaarbd743252010-10-20 21:23:33 +02003382 * When in right-to-left mode and alternate keymap option set,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 * reverse the character flow in the rhs in Farsi.
3384 */
3385 if (p_altkeymap && curwin->w_p_rl)
3386 lrswap(rhs);
3387#endif
3388
3389 /*
3390 * check arguments and translate function keys
3391 */
3392 if (haskey)
3393 {
3394 len = (int)STRLEN(keys);
3395 if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
3396 {
3397 retval = 1;
3398 goto theend;
3399 }
3400
3401 if (abbrev && maptype != 1)
3402 {
3403 /*
3404 * If an abbreviation ends in a keyword character, the
3405 * rest must be all keyword-char or all non-keyword-char.
3406 * Otherwise we won't be able to find the start of it in a
3407 * vi-compatible way.
3408 */
3409#ifdef FEAT_MBYTE
3410 if (has_mbyte)
3411 {
3412 int first, last;
3413 int same = -1;
3414
3415 first = vim_iswordp(keys);
3416 last = first;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003417 p = keys + (*mb_ptr2len)(keys);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 n = 1;
3419 while (p < keys + len)
3420 {
3421 ++n; /* nr of (multi-byte) chars */
3422 last = vim_iswordp(p); /* type of last char */
3423 if (same == -1 && last != first)
3424 same = n - 1; /* count of same char type */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003425 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427 if (last && n > 2 && same >= 0 && same < n - 1)
3428 {
3429 retval = 1;
3430 goto theend;
3431 }
3432 }
3433 else
3434#endif
3435 if (vim_iswordc(keys[len - 1])) /* ends in keyword char */
3436 for (n = 0; n < len - 2; ++n)
3437 if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
3438 {
3439 retval = 1;
3440 goto theend;
3441 }
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00003442 /* An abbreviation cannot contain white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 for (n = 0; n < len; ++n)
3444 if (vim_iswhite(keys[n]))
3445 {
3446 retval = 1;
3447 goto theend;
3448 }
3449 }
3450 }
3451
3452 if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
3453 no_abbr = FALSE; /* reset flag that indicates there are
3454 no abbreviations */
3455
3456 if (!haskey || (maptype != 1 && !hasarg))
3457 msg_start();
3458
3459#ifdef FEAT_LOCALMAP
3460 /*
3461 * Check if a new local mapping wasn't already defined globally.
3462 */
3463 if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
3464 {
3465 /* need to loop over all global hash lists */
3466 for (hash = 0; hash < 256 && !got_int; ++hash)
3467 {
3468 if (abbrev)
3469 {
3470 if (hash != 0) /* there is only one abbreviation list */
3471 break;
3472 mp = first_abbr;
3473 }
3474 else
3475 mp = maphash[hash];
3476 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3477 {
3478 /* check entries with the same mode */
3479 if ((mp->m_mode & mode) != 0
3480 && mp->m_keylen == len
3481 && unique
3482 && STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
3483 {
3484 if (abbrev)
3485 EMSG2(_("E224: global abbreviation already exists for %s"),
3486 mp->m_keys);
3487 else
3488 EMSG2(_("E225: global mapping already exists for %s"),
3489 mp->m_keys);
3490 retval = 5;
3491 goto theend;
3492 }
3493 }
3494 }
3495 }
3496
3497 /*
3498 * When listing global mappings, also list buffer-local ones here.
3499 */
3500 if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
3501 {
3502 /* need to loop over all global hash lists */
3503 for (hash = 0; hash < 256 && !got_int; ++hash)
3504 {
3505 if (abbrev)
3506 {
3507 if (hash != 0) /* there is only one abbreviation list */
3508 break;
3509 mp = curbuf->b_first_abbr;
3510 }
3511 else
3512 mp = curbuf->b_maphash[hash];
3513 for ( ; mp != NULL && !got_int; mp = mp->m_next)
3514 {
3515 /* check entries with the same mode */
3516 if ((mp->m_mode & mode) != 0)
3517 {
3518 if (!haskey) /* show all entries */
3519 {
3520 showmap(mp, TRUE);
3521 did_local = TRUE;
3522 }
3523 else
3524 {
3525 n = mp->m_keylen;
3526 if (STRNCMP(mp->m_keys, keys,
3527 (size_t)(n < len ? n : len)) == 0)
3528 {
3529 showmap(mp, TRUE);
3530 did_local = TRUE;
3531 }
3532 }
3533 }
3534 }
3535 }
3536 }
3537#endif
3538
3539 /*
3540 * Find an entry in the maphash[] list that matches.
3541 * For :unmap we may loop two times: once to try to unmap an entry with a
3542 * matching 'from' part, a second time, if the first fails, to unmap an
3543 * entry with a matching 'to' part. This was done to allow ":ab foo bar"
3544 * to be unmapped by typing ":unab foo", where "foo" will be replaced by
3545 * "bar" because of the abbreviation.
3546 */
3547 for (round = 0; (round == 0 || maptype == 1) && round <= 1
3548 && !did_it && !got_int; ++round)
3549 {
3550 /* need to loop over all hash lists */
3551 for (hash = 0; hash < 256 && !got_int; ++hash)
3552 {
3553 if (abbrev)
3554 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003555 if (hash > 0) /* there is only one abbreviation list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 break;
3557 mpp = abbr_table;
3558 }
3559 else
3560 mpp = &(map_table[hash]);
3561 for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
3562 {
3563
3564 if (!(mp->m_mode & mode)) /* skip entries with wrong mode */
3565 {
3566 mpp = &(mp->m_next);
3567 continue;
3568 }
3569 if (!haskey) /* show all entries */
3570 {
3571 showmap(mp, map_table != maphash);
3572 did_it = TRUE;
3573 }
3574 else /* do we have a match? */
3575 {
3576 if (round) /* second round: Try unmap "rhs" string */
3577 {
3578 n = (int)STRLEN(mp->m_str);
3579 p = mp->m_str;
3580 }
3581 else
3582 {
3583 n = mp->m_keylen;
3584 p = mp->m_keys;
3585 }
3586 if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
3587 {
3588 if (maptype == 1) /* delete entry */
3589 {
3590 /* Only accept a full match. For abbreviations we
3591 * ignore trailing space when matching with the
3592 * "lhs", since an abbreviation can't have
3593 * trailing space. */
3594 if (n != len && (!abbrev || round || n > len
3595 || *skipwhite(keys + n) != NUL))
3596 {
3597 mpp = &(mp->m_next);
3598 continue;
3599 }
3600 /*
3601 * We reset the indicated mode bits. If nothing is
3602 * left the entry is deleted below.
3603 */
3604 mp->m_mode &= ~mode;
3605 did_it = TRUE; /* remember we did something */
3606 }
3607 else if (!hasarg) /* show matching entry */
3608 {
3609 showmap(mp, map_table != maphash);
3610 did_it = TRUE;
3611 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00003612 else if (n != len) /* new entry is ambiguous */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 mpp = &(mp->m_next);
3615 continue;
3616 }
3617 else if (unique)
3618 {
3619 if (abbrev)
3620 EMSG2(_("E226: abbreviation already exists for %s"),
3621 p);
3622 else
3623 EMSG2(_("E227: mapping already exists for %s"), p);
3624 retval = 5;
3625 goto theend;
3626 }
3627 else /* new rhs for existing entry */
3628 {
3629 mp->m_mode &= ~mode; /* remove mode bits */
3630 if (mp->m_mode == 0 && !did_it) /* reuse entry */
3631 {
3632 newstr = vim_strsave(rhs);
3633 if (newstr == NULL)
3634 {
3635 retval = 4; /* no mem */
3636 goto theend;
3637 }
3638 vim_free(mp->m_str);
3639 mp->m_str = newstr;
Bram Moolenaarbd743252010-10-20 21:23:33 +02003640 vim_free(mp->m_orig_str);
3641 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 mp->m_noremap = noremap;
Bram Moolenaar72179e12013-06-29 13:58:31 +02003643 mp->m_nowait = nowait;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 mp->m_silent = silent;
3645 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003646#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003647 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003648 mp->m_script_ID = current_SID;
3649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 did_it = TRUE;
3651 }
3652 }
3653 if (mp->m_mode == 0) /* entry can be deleted */
3654 {
3655 map_free(mpp);
3656 continue; /* continue with *mpp */
3657 }
3658
3659 /*
3660 * May need to put this entry into another hash list.
3661 */
3662 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3663 if (!abbrev && new_hash != hash)
3664 {
3665 *mpp = mp->m_next;
3666 mp->m_next = map_table[new_hash];
3667 map_table[new_hash] = mp;
3668
3669 continue; /* continue with *mpp */
3670 }
3671 }
3672 }
3673 mpp = &(mp->m_next);
3674 }
3675 }
3676 }
3677
3678 if (maptype == 1) /* delete entry */
3679 {
3680 if (!did_it)
3681 retval = 2; /* no match */
Bram Moolenaar9a95bdc2014-10-09 13:36:16 +02003682 else if (*keys == Ctrl_C)
Bram Moolenaar651863c2015-01-14 12:44:41 +01003683 {
Bram Moolenaar9a95bdc2014-10-09 13:36:16 +02003684 /* If CTRL-C has been unmapped, reuse it for Interrupting. */
Bram Moolenaar43579732015-01-14 14:08:44 +01003685#ifdef FEAT_LOCALMAP
Bram Moolenaar651863c2015-01-14 12:44:41 +01003686 if (map_table == curbuf->b_maphash)
3687 curbuf->b_mapped_ctrl_c &= ~mode;
3688 else
Bram Moolenaar43579732015-01-14 14:08:44 +01003689#endif
Bram Moolenaar651863c2015-01-14 12:44:41 +01003690 mapped_ctrl_c &= ~mode;
3691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 goto theend;
3693 }
3694
3695 if (!haskey || !hasarg) /* print entries */
3696 {
3697 if (!did_it
3698#ifdef FEAT_LOCALMAP
3699 && !did_local
3700#endif
3701 )
3702 {
3703 if (abbrev)
3704 MSG(_("No abbreviation found"));
3705 else
3706 MSG(_("No mapping found"));
3707 }
3708 goto theend; /* listing finished */
3709 }
3710
3711 if (did_it) /* have added the new entry already */
3712 goto theend;
3713
3714 /*
3715 * Get here when adding a new entry to the maphash[] list or abbrlist.
3716 */
3717 mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
3718 if (mp == NULL)
3719 {
3720 retval = 4; /* no mem */
3721 goto theend;
3722 }
3723
Bram Moolenaar9a95bdc2014-10-09 13:36:16 +02003724 /* If CTRL-C has been mapped, don't always use it for Interrupting. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if (*keys == Ctrl_C)
Bram Moolenaar651863c2015-01-14 12:44:41 +01003726 {
Bram Moolenaar43579732015-01-14 14:08:44 +01003727#ifdef FEAT_LOCALMAP
Bram Moolenaar651863c2015-01-14 12:44:41 +01003728 if (map_table == curbuf->b_maphash)
3729 curbuf->b_mapped_ctrl_c |= mode;
3730 else
Bram Moolenaar43579732015-01-14 14:08:44 +01003731#endif
Bram Moolenaar651863c2015-01-14 12:44:41 +01003732 mapped_ctrl_c |= mode;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 mp->m_keys = vim_strsave(keys);
3736 mp->m_str = vim_strsave(rhs);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003737 mp->m_orig_str = vim_strsave(orig_rhs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (mp->m_keys == NULL || mp->m_str == NULL)
3739 {
3740 vim_free(mp->m_keys);
3741 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003742 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 vim_free(mp);
3744 retval = 4; /* no mem */
3745 goto theend;
3746 }
3747 mp->m_keylen = (int)STRLEN(mp->m_keys);
3748 mp->m_noremap = noremap;
Bram Moolenaar72179e12013-06-29 13:58:31 +02003749 mp->m_nowait = nowait;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 mp->m_silent = silent;
3751 mp->m_mode = mode;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003752#ifdef FEAT_EVAL
Bram Moolenaar4e427192006-03-10 21:34:27 +00003753 mp->m_expr = expr;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003754 mp->m_script_ID = current_SID;
3755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
3757 /* add the new entry in front of the abbrlist or maphash[] list */
3758 if (abbrev)
3759 {
3760 mp->m_next = *abbr_table;
3761 *abbr_table = mp;
3762 }
3763 else
3764 {
3765 n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3766 mp->m_next = map_table[n];
3767 map_table[n] = mp;
3768 }
3769
3770theend:
3771 vim_free(keys_buf);
3772 vim_free(arg_buf);
3773 return retval;
3774}
3775
3776/*
3777 * Delete one entry from the abbrlist or maphash[].
3778 * "mpp" is a pointer to the m_next field of the PREVIOUS entry!
3779 */
3780 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003781map_free(mapblock_T **mpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 mapblock_T *mp;
3784
3785 mp = *mpp;
3786 vim_free(mp->m_keys);
3787 vim_free(mp->m_str);
Bram Moolenaarbd743252010-10-20 21:23:33 +02003788 vim_free(mp->m_orig_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 *mpp = mp->m_next;
3790 vim_free(mp);
3791}
3792
3793/*
3794 * Initialize maphash[] for first use.
3795 */
3796 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003797validate_maphash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
3799 if (!maphash_valid)
3800 {
3801 vim_memset(maphash, 0, sizeof(maphash));
3802 maphash_valid = TRUE;
3803 }
3804}
3805
3806/*
3807 * Get the mapping mode from the command name.
3808 */
3809 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003810get_map_mode(char_u **cmdp, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 char_u *p;
3813 int modec;
3814 int mode;
3815
3816 p = *cmdp;
3817 modec = *p++;
3818 if (modec == 'i')
3819 mode = INSERT; /* :imap */
3820 else if (modec == 'l')
3821 mode = LANGMAP; /* :lmap */
3822 else if (modec == 'c')
3823 mode = CMDLINE; /* :cmap */
3824 else if (modec == 'n' && *p != 'o') /* avoid :noremap */
3825 mode = NORMAL; /* :nmap */
3826 else if (modec == 'v')
Bram Moolenaar371d5402006-03-20 21:47:49 +00003827 mode = VISUAL + SELECTMODE; /* :vmap */
3828 else if (modec == 'x')
3829 mode = VISUAL; /* :xmap */
3830 else if (modec == 's')
3831 mode = SELECTMODE; /* :smap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else if (modec == 'o')
3833 mode = OP_PENDING; /* :omap */
3834 else
3835 {
3836 --p;
3837 if (forceit)
3838 mode = INSERT + CMDLINE; /* :map ! */
3839 else
Bram Moolenaar371d5402006-03-20 21:47:49 +00003840 mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842
3843 *cmdp = p;
3844 return mode;
3845}
3846
3847/*
3848 * Clear all mappings or abbreviations.
3849 * 'abbr' should be FALSE for mappings, TRUE for abbreviations.
3850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003852map_clear(
3853 char_u *cmdp,
3854 char_u *arg UNUSED,
3855 int forceit,
3856 int abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 int mode;
3859#ifdef FEAT_LOCALMAP
3860 int local;
3861
3862 local = (STRCMP(arg, "<buffer>") == 0);
3863 if (!local && *arg != NUL)
3864 {
3865 EMSG(_(e_invarg));
3866 return;
3867 }
3868#endif
3869
3870 mode = get_map_mode(&cmdp, forceit);
3871 map_clear_int(curbuf, mode,
3872#ifdef FEAT_LOCALMAP
3873 local,
3874#else
3875 FALSE,
3876#endif
3877 abbr);
3878}
3879
3880/*
3881 * Clear all mappings in "mode".
3882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003884map_clear_int(
3885 buf_T *buf UNUSED, /* buffer for local mappings */
3886 int mode, /* mode in which to delete */
3887 int local UNUSED, /* TRUE for buffer-local mappings */
3888 int abbr) /* TRUE for abbreviations */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 mapblock_T *mp, **mpp;
3891 int hash;
3892 int new_hash;
3893
3894 validate_maphash();
3895
3896 for (hash = 0; hash < 256; ++hash)
3897 {
3898 if (abbr)
3899 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003900 if (hash > 0) /* there is only one abbrlist */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 break;
3902#ifdef FEAT_LOCALMAP
3903 if (local)
3904 mpp = &buf->b_first_abbr;
3905 else
3906#endif
3907 mpp = &first_abbr;
3908 }
3909 else
3910 {
3911#ifdef FEAT_LOCALMAP
3912 if (local)
3913 mpp = &buf->b_maphash[hash];
3914 else
3915#endif
3916 mpp = &maphash[hash];
3917 }
3918 while (*mpp != NULL)
3919 {
3920 mp = *mpp;
3921 if (mp->m_mode & mode)
3922 {
3923 mp->m_mode &= ~mode;
3924 if (mp->m_mode == 0) /* entry can be deleted */
3925 {
3926 map_free(mpp);
3927 continue;
3928 }
3929 /*
3930 * May need to put this entry into another hash list.
3931 */
3932 new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
3933 if (!abbr && new_hash != hash)
3934 {
3935 *mpp = mp->m_next;
3936#ifdef FEAT_LOCALMAP
3937 if (local)
3938 {
3939 mp->m_next = buf->b_maphash[new_hash];
3940 buf->b_maphash[new_hash] = mp;
3941 }
3942 else
3943#endif
3944 {
3945 mp->m_next = maphash[new_hash];
3946 maphash[new_hash] = mp;
3947 }
3948 continue; /* continue with *mpp */
3949 }
3950 }
3951 mpp = &(mp->m_next);
3952 }
3953 }
3954}
3955
Bram Moolenaarbd743252010-10-20 21:23:33 +02003956/*
3957 * Return characters to represent the map mode in an allocated string.
3958 * Returns NULL when out of memory.
3959 */
3960 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003961map_mode_to_chars(int mode)
Bram Moolenaarbd743252010-10-20 21:23:33 +02003962{
3963 garray_T mapmode;
3964
3965 ga_init2(&mapmode, 1, 7);
3966
3967 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
3968 ga_append(&mapmode, '!'); /* :map! */
3969 else if (mode & INSERT)
3970 ga_append(&mapmode, 'i'); /* :imap */
3971 else if (mode & LANGMAP)
3972 ga_append(&mapmode, 'l'); /* :lmap */
3973 else if (mode & CMDLINE)
3974 ga_append(&mapmode, 'c'); /* :cmap */
3975 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
3976 == NORMAL + VISUAL + SELECTMODE + OP_PENDING)
3977 ga_append(&mapmode, ' '); /* :map */
3978 else
3979 {
3980 if (mode & NORMAL)
3981 ga_append(&mapmode, 'n'); /* :nmap */
3982 if (mode & OP_PENDING)
3983 ga_append(&mapmode, 'o'); /* :omap */
3984 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
3985 ga_append(&mapmode, 'v'); /* :vmap */
3986 else
3987 {
3988 if (mode & VISUAL)
3989 ga_append(&mapmode, 'x'); /* :xmap */
3990 if (mode & SELECTMODE)
3991 ga_append(&mapmode, 's'); /* :smap */
3992 }
3993 }
3994
3995 ga_append(&mapmode, NUL);
3996 return (char_u *)mapmode.ga_data;
3997}
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004000showmap(
4001 mapblock_T *mp,
4002 int local) /* TRUE for buffer-local map */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaarbd743252010-10-20 21:23:33 +02004004 int len = 1;
4005 char_u *mapchars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 if (msg_didout || msg_silent != 0)
Bram Moolenaarfa47a922009-02-22 22:43:27 +00004008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 msg_putchar('\n');
Bram Moolenaarfa47a922009-02-22 22:43:27 +00004010 if (got_int) /* 'q' typed at MORE prompt */
4011 return;
4012 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02004013
4014 mapchars = map_mode_to_chars(mp->m_mode);
4015 if (mapchars != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaarbd743252010-10-20 21:23:33 +02004017 msg_puts(mapchars);
Bram Moolenaar6b5ef062010-10-27 12:18:00 +02004018 len = (int)STRLEN(mapchars);
Bram Moolenaarbd743252010-10-20 21:23:33 +02004019 vim_free(mapchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
Bram Moolenaarbd743252010-10-20 21:23:33 +02004021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 while (++len <= 3)
4023 msg_putchar(' ');
4024
Bram Moolenaarf2330482008-06-24 20:19:36 +00004025 /* Display the LHS. Get length of what we write. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 len = msg_outtrans_special(mp->m_keys, TRUE);
4027 do
4028 {
4029 msg_putchar(' '); /* padd with blanks */
4030 ++len;
4031 } while (len < 12);
4032
4033 if (mp->m_noremap == REMAP_NONE)
4034 msg_puts_attr((char_u *)"*", hl_attr(HLF_8));
4035 else if (mp->m_noremap == REMAP_SCRIPT)
4036 msg_puts_attr((char_u *)"&", hl_attr(HLF_8));
4037 else
4038 msg_putchar(' ');
4039
4040 if (local)
4041 msg_putchar('@');
4042 else
4043 msg_putchar(' ');
4044
4045 /* Use FALSE below if we only want things like <Up> to show up as such on
Bram Moolenaarbd743252010-10-20 21:23:33 +02004046 * the rhs, and not M-x etc, TRUE gets both -- webb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (*mp->m_str == NUL)
4048 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8));
4049 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02004050 {
4051 /* Remove escaping of CSI, because "m_str" is in a format to be used
4052 * as typeahead. */
4053 char_u *s = vim_strsave(mp->m_str);
4054 if (s != NULL)
4055 {
4056 vim_unescape_csi(s);
4057 msg_outtrans_special(s, FALSE);
4058 vim_free(s);
4059 }
4060 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004061#ifdef FEAT_EVAL
4062 if (p_verbose > 0)
4063 last_set_msg(mp->m_script_ID);
4064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 out_flush(); /* show one line at a time */
4066}
4067
4068#if defined(FEAT_EVAL) || defined(PROTO)
4069/*
4070 * Return TRUE if a map exists that has "str" in the rhs for mode "modechars".
4071 * Recognize termcap codes in "str".
4072 * Also checks mappings local to the current buffer.
4073 */
4074 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004075map_to_exists(char_u *str, char_u *modechars, int abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 int mode = 0;
4078 char_u *rhs;
4079 char_u *buf;
4080 int retval;
4081
Bram Moolenaar9c102382006-05-03 21:26:49 +00004082 rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 if (vim_strchr(modechars, 'n') != NULL)
4085 mode |= NORMAL;
4086 if (vim_strchr(modechars, 'v') != NULL)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004087 mode |= VISUAL + SELECTMODE;
4088 if (vim_strchr(modechars, 'x') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 mode |= VISUAL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004090 if (vim_strchr(modechars, 's') != NULL)
4091 mode |= SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (vim_strchr(modechars, 'o') != NULL)
4093 mode |= OP_PENDING;
4094 if (vim_strchr(modechars, 'i') != NULL)
4095 mode |= INSERT;
4096 if (vim_strchr(modechars, 'l') != NULL)
4097 mode |= LANGMAP;
4098 if (vim_strchr(modechars, 'c') != NULL)
4099 mode |= CMDLINE;
4100
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004101 retval = map_to_exists_mode(rhs, mode, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 vim_free(buf);
4103
4104 return retval;
4105}
4106#endif
4107
4108/*
4109 * Return TRUE if a map exists that has "str" in the rhs for mode "mode".
4110 * Also checks mappings local to the current buffer.
4111 */
4112 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004113map_to_exists_mode(char_u *rhs, int mode, int abbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
4115 mapblock_T *mp;
4116 int hash;
4117# ifdef FEAT_LOCALMAP
4118 int expand_buffer = FALSE;
4119
4120 validate_maphash();
4121
4122 /* Do it twice: once for global maps and once for local maps. */
4123 for (;;)
4124 {
4125# endif
4126 for (hash = 0; hash < 256; ++hash)
4127 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004128 if (abbr)
4129 {
4130 if (hash > 0) /* there is only one abbr list */
4131 break;
4132#ifdef FEAT_LOCALMAP
4133 if (expand_buffer)
4134 mp = curbuf->b_first_abbr;
4135 else
4136#endif
4137 mp = first_abbr;
4138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139# ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004140 else if (expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 mp = curbuf->b_maphash[hash];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142# endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 mp = maphash[hash];
4145 for (; mp; mp = mp->m_next)
4146 {
4147 if ((mp->m_mode & mode)
4148 && strstr((char *)mp->m_str, (char *)rhs) != NULL)
4149 return TRUE;
4150 }
4151 }
4152# ifdef FEAT_LOCALMAP
4153 if (expand_buffer)
4154 break;
4155 expand_buffer = TRUE;
4156 }
4157# endif
4158
4159 return FALSE;
4160}
4161
4162#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4163/*
4164 * Used below when expanding mapping/abbreviation names.
4165 */
4166static int expand_mapmodes = 0;
4167static int expand_isabbrev = 0;
4168#ifdef FEAT_LOCALMAP
4169static int expand_buffer = FALSE;
4170#endif
4171
4172/*
4173 * Work out what to complete when doing command line completion of mapping
4174 * or abbreviation names.
4175 */
4176 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004177set_context_in_map_cmd(
4178 expand_T *xp,
4179 char_u *cmd,
4180 char_u *arg,
4181 int forceit, /* TRUE if '!' given */
4182 int isabbrev, /* TRUE if abbreviation */
4183 int isunmap, /* TRUE if unmap/unabbrev command */
4184 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
4186 if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
4187 xp->xp_context = EXPAND_NOTHING;
4188 else
4189 {
4190 if (isunmap)
4191 expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
4192 else
4193 {
4194 expand_mapmodes = INSERT + CMDLINE;
4195 if (!isabbrev)
Bram Moolenaar371d5402006-03-20 21:47:49 +00004196 expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 expand_isabbrev = isabbrev;
4199 xp->xp_context = EXPAND_MAPPINGS;
4200#ifdef FEAT_LOCALMAP
4201 expand_buffer = FALSE;
4202#endif
4203 for (;;)
4204 {
4205#ifdef FEAT_LOCALMAP
4206 if (STRNCMP(arg, "<buffer>", 8) == 0)
4207 {
4208 expand_buffer = TRUE;
4209 arg = skipwhite(arg + 8);
4210 continue;
4211 }
4212#endif
4213 if (STRNCMP(arg, "<unique>", 8) == 0)
4214 {
4215 arg = skipwhite(arg + 8);
4216 continue;
4217 }
Bram Moolenaar72179e12013-06-29 13:58:31 +02004218 if (STRNCMP(arg, "<nowait>", 8) == 0)
4219 {
4220 arg = skipwhite(arg + 8);
4221 continue;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 if (STRNCMP(arg, "<silent>", 8) == 0)
4224 {
4225 arg = skipwhite(arg + 8);
4226 continue;
4227 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004228#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (STRNCMP(arg, "<script>", 8) == 0)
4230 {
4231 arg = skipwhite(arg + 8);
4232 continue;
4233 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004234 if (STRNCMP(arg, "<expr>", 6) == 0)
4235 {
4236 arg = skipwhite(arg + 6);
4237 continue;
4238 }
4239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 break;
4241 }
4242 xp->xp_pattern = arg;
4243 }
4244
4245 return NULL;
4246}
4247
4248/*
4249 * Find all mapping/abbreviation names that match regexp 'prog'.
4250 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
4251 * Return OK if matches found, FAIL otherwise.
4252 */
4253 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004254ExpandMappings(
4255 regmatch_T *regmatch,
4256 int *num_file,
4257 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
4259 mapblock_T *mp;
4260 int hash;
4261 int count;
4262 int round;
4263 char_u *p;
4264 int i;
4265
4266 validate_maphash();
4267
4268 *num_file = 0; /* return values in case of FAIL */
4269 *file = NULL;
4270
4271 /*
4272 * round == 1: Count the matches.
4273 * round == 2: Build the array to keep the matches.
4274 */
4275 for (round = 1; round <= 2; ++round)
4276 {
4277 count = 0;
4278
Bram Moolenaar72179e12013-06-29 13:58:31 +02004279 for (i = 0; i < 6; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 if (i == 0)
4282 p = (char_u *)"<silent>";
4283 else if (i == 1)
4284 p = (char_u *)"<unique>";
4285#ifdef FEAT_EVAL
4286 else if (i == 2)
4287 p = (char_u *)"<script>";
Bram Moolenaar4e427192006-03-10 21:34:27 +00004288 else if (i == 3)
4289 p = (char_u *)"<expr>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290#endif
4291#ifdef FEAT_LOCALMAP
Bram Moolenaar4e427192006-03-10 21:34:27 +00004292 else if (i == 4 && !expand_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 p = (char_u *)"<buffer>";
4294#endif
Bram Moolenaar72179e12013-06-29 13:58:31 +02004295 else if (i == 5)
4296 p = (char_u *)"<nowait>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 else
4298 continue;
4299
4300 if (vim_regexec(regmatch, p, (colnr_T)0))
4301 {
4302 if (round == 1)
4303 ++count;
4304 else
4305 (*file)[count++] = vim_strsave(p);
4306 }
4307 }
4308
4309 for (hash = 0; hash < 256; ++hash)
4310 {
4311 if (expand_isabbrev)
4312 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004313 if (hash > 0) /* only one abbrev list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 break; /* for (hash) */
4315 mp = first_abbr;
4316 }
4317#ifdef FEAT_LOCALMAP
4318 else if (expand_buffer)
4319 mp = curbuf->b_maphash[hash];
4320#endif
4321 else
4322 mp = maphash[hash];
4323 for (; mp; mp = mp->m_next)
4324 {
4325 if (mp->m_mode & expand_mapmodes)
4326 {
4327 p = translate_mapping(mp->m_keys, TRUE);
4328 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
4329 {
4330 if (round == 1)
4331 ++count;
4332 else
4333 {
4334 (*file)[count++] = p;
4335 p = NULL;
4336 }
4337 }
4338 vim_free(p);
4339 }
4340 } /* for (mp) */
4341 } /* for (hash) */
4342
4343 if (count == 0) /* no match found */
4344 break; /* for (round) */
4345
4346 if (round == 1)
4347 {
4348 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4349 if (*file == NULL)
4350 return FAIL;
4351 }
4352 } /* for (round) */
4353
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004354 if (count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004356 char_u **ptr1;
4357 char_u **ptr2;
4358 char_u **ptr3;
4359
4360 /* Sort the matches */
4361 sort_strings(*file, count);
4362
4363 /* Remove multiple entries */
4364 ptr1 = *file;
4365 ptr2 = ptr1 + 1;
4366 ptr3 = ptr1 + count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 while (ptr2 < ptr3)
4369 {
4370 if (STRCMP(*ptr1, *ptr2))
4371 *++ptr1 = *ptr2++;
4372 else
4373 {
4374 vim_free(*ptr2++);
4375 count--;
4376 }
4377 }
4378 }
4379
4380 *num_file = count;
4381 return (count == 0 ? FAIL : OK);
4382}
4383#endif /* FEAT_CMDL_COMPL */
4384
4385/*
4386 * Check for an abbreviation.
4387 * Cursor is at ptr[col]. When inserting, mincol is where insert started.
4388 * "c" is the character typed before check_abbr was called. It may have
4389 * ABBR_OFF added to avoid prepending a CTRL-V to it.
4390 *
4391 * Historic vi practice: The last character of an abbreviation must be an id
4392 * character ([a-zA-Z0-9_]). The characters in front of it must be all id
4393 * characters or all non-id characters. This allows for abbr. "#i" to
4394 * "#include".
4395 *
4396 * Vim addition: Allow for abbreviations that end in a non-keyword character.
4397 * Then there must be white space before the abbr.
4398 *
4399 * return TRUE if there is an abbreviation, FALSE if not
4400 */
4401 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004402check_abbr(
4403 int c,
4404 char_u *ptr,
4405 int col,
4406 int mincol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
4408 int len;
4409 int scol; /* starting column of the abbr. */
4410 int j;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004411 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 char_u tb[MB_MAXBYTES + 4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 mapblock_T *mp;
4414#ifdef FEAT_LOCALMAP
4415 mapblock_T *mp2;
4416#endif
4417#ifdef FEAT_MBYTE
4418 int clen = 0; /* length in characters */
4419#endif
4420 int is_id = TRUE;
4421 int vim_abbr;
4422
4423 if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */
4424 return FALSE;
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02004425
4426 /* no remapping implies no abbreviation, except for CTRL-] */
4427 if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0 && c != Ctrl_RSB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 return FALSE;
4429
4430 /*
4431 * Check for word before the cursor: If it ends in a keyword char all
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00004432 * chars before it must be keyword chars or non-keyword chars, but not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * white space. If it ends in a non-keyword char we accept any characters
4434 * before it except white space.
4435 */
4436 if (col == 0) /* cannot be an abbr. */
4437 return FALSE;
4438
4439#ifdef FEAT_MBYTE
4440 if (has_mbyte)
4441 {
4442 char_u *p;
4443
4444 p = mb_prevptr(ptr, ptr + col);
4445 if (!vim_iswordp(p))
4446 vim_abbr = TRUE; /* Vim added abbr. */
4447 else
4448 {
4449 vim_abbr = FALSE; /* vi compatible abbr. */
4450 if (p > ptr)
4451 is_id = vim_iswordp(mb_prevptr(ptr, p));
4452 }
4453 clen = 1;
4454 while (p > ptr + mincol)
4455 {
4456 p = mb_prevptr(ptr, p);
4457 if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
4458 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004459 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 break;
4461 }
4462 ++clen;
4463 }
4464 scol = (int)(p - ptr);
4465 }
4466 else
4467#endif
4468 {
4469 if (!vim_iswordc(ptr[col - 1]))
4470 vim_abbr = TRUE; /* Vim added abbr. */
4471 else
4472 {
4473 vim_abbr = FALSE; /* vi compatible abbr. */
4474 if (col > 1)
4475 is_id = vim_iswordc(ptr[col - 2]);
4476 }
4477 for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
4478 && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
4479 ;
4480 }
4481
4482 if (scol < mincol)
4483 scol = mincol;
4484 if (scol < col) /* there is a word in front of the cursor */
4485 {
4486 ptr += scol;
4487 len = col - scol;
4488#ifdef FEAT_LOCALMAP
4489 mp = curbuf->b_first_abbr;
4490 mp2 = first_abbr;
4491 if (mp == NULL)
4492 {
4493 mp = mp2;
4494 mp2 = NULL;
4495 }
4496#else
4497 mp = first_abbr;
4498#endif
4499 for ( ; mp;
4500#ifdef FEAT_LOCALMAP
4501 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
4502#endif
4503 (mp = mp->m_next))
4504 {
Bram Moolenaar4920a442014-10-21 19:35:31 +02004505 int qlen = mp->m_keylen;
4506 char_u *q = mp->m_keys;
4507 int match;
4508
4509 if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
4510 {
4511 /* might have CSI escaped mp->m_keys */
4512 q = vim_strsave(mp->m_keys);
4513 if (q != NULL)
4514 {
4515 vim_unescape_csi(q);
4516 qlen = (int)STRLEN(q);
4517 }
4518 }
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /* find entries with right mode and keys */
Bram Moolenaar4920a442014-10-21 19:35:31 +02004521 match = (mp->m_mode & State)
Bram Moolenaarbdef5182014-10-21 16:22:17 +02004522 && qlen == len
Bram Moolenaar4920a442014-10-21 19:35:31 +02004523 && !STRNCMP(q, ptr, (size_t)len);
4524 if (q != mp->m_keys)
4525 vim_free(q);
4526 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 break;
4528 }
4529 if (mp != NULL)
4530 {
4531 /*
4532 * Found a match:
4533 * Insert the rest of the abbreviation in typebuf.tb_buf[].
4534 * This goes from end to start.
4535 *
4536 * Characters 0x000 - 0x100: normal chars, may need CTRL-V,
4537 * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER
4538 * Characters where IS_SPECIAL() == TRUE: key codes, need
4539 * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
4540 *
4541 * Character CTRL-] is treated specially - it completes the
4542 * abbreviation, but is not inserted into the input stream.
4543 */
4544 j = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (c != Ctrl_RSB)
4546 {
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004547 /* special key code, split up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (IS_SPECIAL(c) || c == K_SPECIAL)
4549 {
4550 tb[j++] = K_SPECIAL;
4551 tb[j++] = K_SECOND(c);
4552 tb[j++] = K_THIRD(c);
4553 }
4554 else
4555 {
4556 if (c < ABBR_OFF && (c < ' ' || c > '~'))
4557 tb[j++] = Ctrl_V; /* special char needs CTRL-V */
4558#ifdef FEAT_MBYTE
4559 if (has_mbyte)
4560 {
4561 /* if ABBR_OFF has been added, remove it here */
4562 if (c >= ABBR_OFF)
4563 c -= ABBR_OFF;
4564 j += (*mb_char2bytes)(c, tb + j);
4565 }
4566 else
4567#endif
4568 tb[j++] = c;
4569 }
4570 tb[j] = NUL;
4571 /* insert the last typed char */
4572 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4573 }
Bram Moolenaar4e427192006-03-10 21:34:27 +00004574#ifdef FEAT_EVAL
4575 if (mp->m_expr)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004576 s = eval_map_expr(mp->m_str, c);
Bram Moolenaar4e427192006-03-10 21:34:27 +00004577 else
4578#endif
4579 s = mp->m_str;
4580 if (s != NULL)
4581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 /* insert the to string */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004583 (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 /* no abbrev. for these chars */
Bram Moolenaar4e427192006-03-10 21:34:27 +00004585 typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
4586#ifdef FEAT_EVAL
4587 if (mp->m_expr)
4588 vim_free(s);
4589#endif
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 tb[0] = Ctrl_H;
4593 tb[1] = NUL;
4594#ifdef FEAT_MBYTE
4595 if (has_mbyte)
4596 len = clen; /* Delete characters instead of bytes */
4597#endif
4598 while (len-- > 0) /* delete the from string */
4599 (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
4600 return TRUE;
4601 }
4602 }
4603 return FALSE;
4604}
4605
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004606#ifdef FEAT_EVAL
4607/*
4608 * Evaluate the RHS of a mapping or abbreviations and take care of escaping
4609 * special characters.
4610 */
4611 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004612eval_map_expr(
4613 char_u *str,
4614 int c) /* NUL or typed character for abbreviation */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004615{
4616 char_u *res;
Bram Moolenaar8424a622006-04-19 21:23:36 +00004617 char_u *p;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004618 char_u *expr;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004619 char_u *save_cmd;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004620 pos_T save_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004621 int save_msg_col;
4622 int save_msg_row;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004623
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004624 /* Remove escaping of CSI, because "str" is in a format to be used as
4625 * typeahead. */
4626 expr = vim_strsave(str);
4627 if (expr == NULL)
4628 return NULL;
4629 vim_unescape_csi(expr);
4630
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004631 save_cmd = save_cmdline_alloc();
4632 if (save_cmd == NULL)
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004633 {
4634 vim_free(expr);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004635 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004636 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004637
4638 /* Forbid changing text or using ":normal" to avoid most of the bad side
4639 * effects. Also restore the cursor position. */
4640 ++textlock;
4641#ifdef FEAT_EX_EXTRA
4642 ++ex_normal_lock;
4643#endif
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004644 set_vim_var_char(c); /* set v:char to the typed character */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004645 save_cursor = curwin->w_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004646 save_msg_col = msg_col;
4647 save_msg_row = msg_row;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004648 p = eval_to_string(expr, NULL, FALSE);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004649 --textlock;
4650#ifdef FEAT_EX_EXTRA
4651 --ex_normal_lock;
4652#endif
4653 curwin->w_cursor = save_cursor;
Bram Moolenaar63760642011-12-23 14:54:04 +01004654 msg_col = save_msg_col;
4655 msg_row = save_msg_row;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004656
Bram Moolenaarc1b52862006-04-28 22:32:28 +00004657 restore_cmdline_alloc(save_cmd);
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004658 vim_free(expr);
4659
Bram Moolenaar8424a622006-04-19 21:23:36 +00004660 if (p == NULL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004661 return NULL;
Bram Moolenaarf6f4a012011-08-17 17:18:20 +02004662 /* Escape CSI in the result to be able to use the string as typeahead. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004663 res = vim_strsave_escape_csi(p);
4664 vim_free(p);
4665
4666 return res;
4667}
4668#endif
4669
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004670/*
4671 * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result
4672 * can be put in the typeahead buffer.
4673 * Returns NULL when out of memory.
4674 */
4675 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004676vim_strsave_escape_csi(
4677 char_u *p)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004678{
4679 char_u *res;
4680 char_u *s, *d;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004681
4682 /* Need a buffer to hold up to three times as much. */
Bram Moolenaar8424a622006-04-19 21:23:36 +00004683 res = alloc((unsigned)(STRLEN(p) * 3) + 1);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004684 if (res != NULL)
4685 {
Bram Moolenaar8424a622006-04-19 21:23:36 +00004686 d = res;
4687 for (s = p; *s != NUL; )
4688 {
4689 if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
4690 {
4691 /* Copy special key unmodified. */
4692 *d++ = *s++;
4693 *d++ = *s++;
4694 *d++ = *s++;
4695 }
4696 else
4697 {
Bram Moolenaar229f8db2013-05-06 05:50:28 +02004698#ifdef FEAT_MBYTE
4699 int len = mb_char2len(PTR2CHAR(s));
4700 int len2 = mb_ptr2len(s);
4701#endif
Bram Moolenaar8424a622006-04-19 21:23:36 +00004702 /* Add character, possibly multi-byte to destination, escaping
4703 * CSI and K_SPECIAL. */
4704 d = add_char2buf(PTR2CHAR(s), d);
Bram Moolenaar229f8db2013-05-06 05:50:28 +02004705#ifdef FEAT_MBYTE
4706 while (len < len2)
4707 {
4708 /* add following combining char */
4709 d = add_char2buf(PTR2CHAR(s + len), d);
4710 len += mb_char2len(PTR2CHAR(s + len));
4711 }
4712#endif
Bram Moolenaar8424a622006-04-19 21:23:36 +00004713 mb_ptr_adv(s);
4714 }
4715 }
4716 *d = NUL;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004717 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004718 return res;
4719}
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721/*
Bram Moolenaar81b85872007-03-04 20:22:01 +00004722 * Remove escaping from CSI and K_SPECIAL characters. Reverse of
4723 * vim_strsave_escape_csi(). Works in-place.
4724 */
4725 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004726vim_unescape_csi(char_u *p)
Bram Moolenaar81b85872007-03-04 20:22:01 +00004727{
4728 char_u *s = p, *d = p;
4729
4730 while (*s != NUL)
4731 {
4732 if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
4733 {
4734 *d++ = K_SPECIAL;
4735 s += 3;
4736 }
4737 else if ((s[0] == K_SPECIAL || s[0] == CSI)
4738 && s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
4739 {
4740 *d++ = CSI;
4741 s += 3;
4742 }
4743 else
4744 *d++ = *s++;
4745 }
4746 *d = NUL;
4747}
4748
4749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 * Write map commands for the current mappings to an .exrc file.
4751 * Return FAIL on error, OK otherwise.
4752 */
4753 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004754makemap(
4755 FILE *fd,
4756 buf_T *buf) /* buffer for local mappings or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 mapblock_T *mp;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004759 char_u c1, c2, c3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 char_u *p;
4761 char *cmd;
4762 int abbr;
4763 int hash;
4764 int did_cpo = FALSE;
4765 int i;
4766
4767 validate_maphash();
4768
4769 /*
4770 * Do the loop twice: Once for mappings, once for abbreviations.
4771 * Then loop over all map hash lists.
4772 */
4773 for (abbr = 0; abbr < 2; ++abbr)
4774 for (hash = 0; hash < 256; ++hash)
4775 {
4776 if (abbr)
4777 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004778 if (hash > 0) /* there is only one abbr list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 break;
4780#ifdef FEAT_LOCALMAP
4781 if (buf != NULL)
4782 mp = buf->b_first_abbr;
4783 else
4784#endif
4785 mp = first_abbr;
4786 }
4787 else
4788 {
4789#ifdef FEAT_LOCALMAP
4790 if (buf != NULL)
4791 mp = buf->b_maphash[hash];
4792 else
4793#endif
4794 mp = maphash[hash];
4795 }
4796
4797 for ( ; mp; mp = mp->m_next)
4798 {
4799 /* skip script-local mappings */
4800 if (mp->m_noremap == REMAP_SCRIPT)
4801 continue;
4802
4803 /* skip mappings that contain a <SNR> (script-local thing),
4804 * they probably don't work when loaded again */
4805 for (p = mp->m_str; *p != NUL; ++p)
4806 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
4807 && p[2] == (int)KE_SNR)
4808 break;
4809 if (*p != NUL)
4810 continue;
4811
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004812 /* It's possible to create a mapping and then ":unmap" certain
4813 * modes. We recreate this here by mapping the individual
4814 * modes, which requires up to three of them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 c1 = NUL;
4816 c2 = NUL;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004817 c3 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (abbr)
4819 cmd = "abbr";
4820 else
4821 cmd = "map";
4822 switch (mp->m_mode)
4823 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00004824 case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 break;
4826 case NORMAL:
4827 c1 = 'n';
4828 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004829 case VISUAL:
4830 c1 = 'x';
4831 break;
4832 case SELECTMODE:
4833 c1 = 's';
4834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 case OP_PENDING:
4836 c1 = 'o';
4837 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004838 case NORMAL + VISUAL:
4839 c1 = 'n';
4840 c2 = 'x';
4841 break;
4842 case NORMAL + SELECTMODE:
4843 c1 = 'n';
4844 c2 = 's';
4845 break;
4846 case NORMAL + OP_PENDING:
4847 c1 = 'n';
4848 c2 = 'o';
4849 break;
4850 case VISUAL + SELECTMODE:
4851 c1 = 'v';
4852 break;
4853 case VISUAL + OP_PENDING:
4854 c1 = 'x';
4855 c2 = 'o';
4856 break;
4857 case SELECTMODE + OP_PENDING:
4858 c1 = 's';
4859 c2 = 'o';
4860 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004861 case NORMAL + VISUAL + SELECTMODE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 c1 = 'n';
4863 c2 = 'v';
4864 break;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004865 case NORMAL + VISUAL + OP_PENDING:
4866 c1 = 'n';
4867 c2 = 'x';
4868 c3 = 'o';
4869 break;
4870 case NORMAL + SELECTMODE + OP_PENDING:
4871 c1 = 'n';
4872 c2 = 's';
4873 c3 = 'o';
4874 break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00004875 case VISUAL + SELECTMODE + OP_PENDING:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 c1 = 'v';
4877 c2 = 'o';
4878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 case CMDLINE + INSERT:
4880 if (!abbr)
4881 cmd = "map!";
4882 break;
4883 case CMDLINE:
4884 c1 = 'c';
4885 break;
4886 case INSERT:
4887 c1 = 'i';
4888 break;
4889 case LANGMAP:
4890 c1 = 'l';
4891 break;
4892 default:
4893 EMSG(_("E228: makemap: Illegal mode"));
4894 return FAIL;
4895 }
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004896 do /* do this twice if c2 is set, 3 times with c3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
4898 /* When outputting <> form, need to make sure that 'cpo'
4899 * is set to the Vim default. */
4900 if (!did_cpo)
4901 {
4902 if (*mp->m_str == NUL) /* will use <Nop> */
4903 did_cpo = TRUE;
4904 else
4905 for (i = 0; i < 2; ++i)
4906 for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
4907 if (*p == K_SPECIAL || *p == NL)
4908 did_cpo = TRUE;
4909 if (did_cpo)
4910 {
4911 if (fprintf(fd, "let s:cpo_save=&cpo") < 0
4912 || put_eol(fd) < 0
4913 || fprintf(fd, "set cpo&vim") < 0
4914 || put_eol(fd) < 0)
4915 return FAIL;
4916 }
4917 }
4918 if (c1 && putc(c1, fd) < 0)
4919 return FAIL;
4920 if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
4921 return FAIL;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004922 if (fputs(cmd, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924 if (buf != NULL && fputs(" <buffer>", fd) < 0)
4925 return FAIL;
Bram Moolenaar72179e12013-06-29 13:58:31 +02004926 if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
4927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (mp->m_silent && fputs(" <silent>", fd) < 0)
4929 return FAIL;
Bram Moolenaar4e427192006-03-10 21:34:27 +00004930#ifdef FEAT_EVAL
4931 if (mp->m_noremap == REMAP_SCRIPT
4932 && fputs("<script>", fd) < 0)
4933 return FAIL;
4934 if (mp->m_expr && fputs(" <expr>", fd) < 0)
4935 return FAIL;
4936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 if ( putc(' ', fd) < 0
4939 || put_escstr(fd, mp->m_keys, 0) == FAIL
4940 || putc(' ', fd) < 0
4941 || put_escstr(fd, mp->m_str, 1) == FAIL
4942 || put_eol(fd) < 0)
4943 return FAIL;
4944 c1 = c2;
Bram Moolenaar01b9a402008-07-22 16:58:47 +00004945 c2 = c3;
4946 c3 = NUL;
4947 } while (c1 != NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950
4951 if (did_cpo)
4952 if (fprintf(fd, "let &cpo=s:cpo_save") < 0
4953 || put_eol(fd) < 0
4954 || fprintf(fd, "unlet s:cpo_save") < 0
4955 || put_eol(fd) < 0)
4956 return FAIL;
4957 return OK;
4958}
4959
4960/*
4961 * write escape string to file
4962 * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set
4963 *
4964 * return FAIL for failure, OK otherwise
4965 */
4966 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004967put_escstr(FILE *fd, char_u *strstart, int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
4969 char_u *str = strstart;
4970 int c;
4971 int modifiers;
4972
4973 /* :map xx <Nop> */
4974 if (*str == NUL && what == 1)
4975 {
4976 if (fprintf(fd, "<Nop>") < 0)
4977 return FAIL;
4978 return OK;
4979 }
4980
4981 for ( ; *str != NUL; ++str)
4982 {
4983#ifdef FEAT_MBYTE
4984 char_u *p;
4985
4986 /* Check for a multi-byte character, which may contain escaped
4987 * K_SPECIAL and CSI bytes */
4988 p = mb_unescape(&str);
4989 if (p != NULL)
4990 {
4991 while (*p != NUL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004992 if (fputc(*p++, fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 return FAIL;
4994 --str;
4995 continue;
4996 }
4997#endif
4998
4999 c = *str;
5000 /*
5001 * Special key codes have to be translated to be able to make sense
5002 * when they are read back.
5003 */
5004 if (c == K_SPECIAL && what != 2)
5005 {
5006 modifiers = 0x0;
5007 if (str[1] == KS_MODIFIER)
5008 {
5009 modifiers = str[2];
5010 str += 3;
5011 c = *str;
5012 }
5013 if (c == K_SPECIAL)
5014 {
5015 c = TO_SPECIAL(str[1], str[2]);
5016 str += 2;
5017 }
5018 if (IS_SPECIAL(c) || modifiers) /* special key */
5019 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005020 if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 return FAIL;
5022 continue;
5023 }
5024 }
5025
5026 /*
5027 * A '\n' in a map command should be written as <NL>.
5028 * A '\n' in a set command should be written as \^V^J.
5029 */
5030 if (c == NL)
5031 {
5032 if (what == 2)
5033 {
5034 if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
5035 return FAIL;
5036 }
5037 else
5038 {
5039 if (fprintf(fd, "<NL>") < 0)
5040 return FAIL;
5041 }
5042 continue;
5043 }
5044
5045 /*
5046 * Some characters have to be escaped with CTRL-V to
5047 * prevent them from misinterpreted in DoOneCmd().
5048 * A space, Tab and '"' has to be escaped with a backslash to
5049 * prevent it to be misinterpreted in do_set().
5050 * A space has to be escaped with a CTRL-V when it's at the start of a
5051 * ":map" rhs.
5052 * A '<' has to be escaped with a CTRL-V to prevent it being
5053 * interpreted as the start of a special key name.
5054 * A space in the lhs of a :map needs a CTRL-V.
5055 */
5056 if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\'))
5057 {
5058 if (putc('\\', fd) < 0)
5059 return FAIL;
5060 }
5061 else if (c < ' ' || c > '~' || c == '|'
5062 || (what == 0 && c == ' ')
5063 || (what == 1 && str == strstart && c == ' ')
5064 || (what != 2 && c == '<'))
5065 {
5066 if (putc(Ctrl_V, fd) < 0)
5067 return FAIL;
5068 }
5069 if (putc(c, fd) < 0)
5070 return FAIL;
5071 }
5072 return OK;
5073}
5074
5075/*
5076 * Check all mappings for the presence of special key codes.
5077 * Used after ":set term=xxx".
5078 */
5079 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005080check_map_keycodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 mapblock_T *mp;
5083 char_u *p;
5084 int i;
5085 char_u buf[3];
5086 char_u *save_name;
5087 int abbr;
5088 int hash;
5089#ifdef FEAT_LOCALMAP
5090 buf_T *bp;
5091#endif
5092
5093 validate_maphash();
5094 save_name = sourcing_name;
5095 sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */
5096
5097#ifdef FEAT_LOCALMAP
5098 /* This this once for each buffer, and then once for global
5099 * mappings/abbreviations with bp == NULL */
5100 for (bp = firstbuf; ; bp = bp->b_next)
5101 {
5102#endif
5103 /*
5104 * Do the loop twice: Once for mappings, once for abbreviations.
5105 * Then loop over all map hash lists.
5106 */
5107 for (abbr = 0; abbr <= 1; ++abbr)
5108 for (hash = 0; hash < 256; ++hash)
5109 {
5110 if (abbr)
5111 {
5112 if (hash) /* there is only one abbr list */
5113 break;
5114#ifdef FEAT_LOCALMAP
5115 if (bp != NULL)
5116 mp = bp->b_first_abbr;
5117 else
5118#endif
5119 mp = first_abbr;
5120 }
5121 else
5122 {
5123#ifdef FEAT_LOCALMAP
5124 if (bp != NULL)
5125 mp = bp->b_maphash[hash];
5126 else
5127#endif
5128 mp = maphash[hash];
5129 }
5130 for ( ; mp != NULL; mp = mp->m_next)
5131 {
5132 for (i = 0; i <= 1; ++i) /* do this twice */
5133 {
5134 if (i == 0)
5135 p = mp->m_keys; /* once for the "from" part */
5136 else
5137 p = mp->m_str; /* and once for the "to" part */
5138 while (*p)
5139 {
5140 if (*p == K_SPECIAL)
5141 {
5142 ++p;
5143 if (*p < 128) /* for "normal" tcap entries */
5144 {
5145 buf[0] = p[0];
5146 buf[1] = p[1];
5147 buf[2] = NUL;
5148 (void)add_termcap_entry(buf, FALSE);
5149 }
5150 ++p;
5151 }
5152 ++p;
5153 }
5154 }
5155 }
5156 }
5157#ifdef FEAT_LOCALMAP
5158 if (bp == NULL)
5159 break;
5160 }
5161#endif
5162 sourcing_name = save_name;
5163}
5164
Bram Moolenaarbd743252010-10-20 21:23:33 +02005165#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166/*
Bram Moolenaarbd743252010-10-20 21:23:33 +02005167 * Check the string "keys" against the lhs of all mappings.
5168 * Return pointer to rhs of mapping (mapblock->m_str).
5169 * NULL when no mapping found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 */
5171 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005172check_map(
5173 char_u *keys,
5174 int mode,
5175 int exact, /* require exact match */
5176 int ign_mod, /* ignore preceding modifier */
5177 int abbr, /* do abbreviations */
5178 mapblock_T **mp_ptr, /* return: pointer to mapblock or NULL */
5179 int *local_ptr) /* return: buffer-local mapping or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
5181 int hash;
5182 int len, minlen;
5183 mapblock_T *mp;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005184 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185#ifdef FEAT_LOCALMAP
5186 int local;
5187#endif
5188
5189 validate_maphash();
5190
5191 len = (int)STRLEN(keys);
5192#ifdef FEAT_LOCALMAP
5193 for (local = 1; local >= 0; --local)
5194#endif
5195 /* loop over all hash lists */
5196 for (hash = 0; hash < 256; ++hash)
5197 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005198 if (abbr)
5199 {
5200 if (hash > 0) /* there is only one list. */
5201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#ifdef FEAT_LOCALMAP
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005203 if (local)
5204 mp = curbuf->b_first_abbr;
5205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005207 mp = first_abbr;
5208 }
5209#ifdef FEAT_LOCALMAP
5210 else if (local)
5211 mp = curbuf->b_maphash[hash];
5212#endif
5213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 mp = maphash[hash];
5215 for ( ; mp != NULL; mp = mp->m_next)
5216 {
5217 /* skip entries with wrong mode, wrong length and not matching
5218 * ones */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005219 if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
5220 {
5221 if (len > mp->m_keylen)
5222 minlen = mp->m_keylen;
5223 else
5224 minlen = len;
5225 s = mp->m_keys;
5226 if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
5227 && s[2] != NUL)
5228 {
5229 s += 3;
5230 if (len > mp->m_keylen - 3)
5231 minlen = mp->m_keylen - 3;
5232 }
5233 if (STRNCMP(s, keys, minlen) == 0)
Bram Moolenaarbd743252010-10-20 21:23:33 +02005234 {
5235 if (mp_ptr != NULL)
5236 *mp_ptr = mp;
5237 if (local_ptr != NULL)
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005238#ifdef FEAT_LOCALMAP
Bram Moolenaarbd743252010-10-20 21:23:33 +02005239 *local_ptr = local;
Bram Moolenaarb8e86702010-10-22 22:13:52 +02005240#else
5241 *local_ptr = 0;
5242#endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005243 return mp->m_str;
Bram Moolenaarbd743252010-10-20 21:23:33 +02005244 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247 }
5248
5249 return NULL;
5250}
5251#endif
5252
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005253#if defined(MSDOS) || defined(MSWIN) || defined(MACOS)
Bram Moolenaar371d5402006-03-20 21:47:49 +00005254
5255#define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */
5256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257/*
5258 * Default mappings for some often used keys.
5259 */
5260static struct initmap
5261{
5262 char_u *arg;
5263 int mode;
5264} initmappings[] =
5265{
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005266#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 /* Use the Windows (CUA) keybindings. */
5268# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 /* paste, copy and cut */
5270 {(char_u *)"<S-Insert> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005271 {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005273 {(char_u *)"<C-Insert> \"*y", VIS_SEL},
5274 {(char_u *)"<S-Del> \"*d", VIS_SEL},
5275 {(char_u *)"<C-Del> \"*d", VIS_SEL},
5276 {(char_u *)"<C-X> \"*d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */
5278# else
Bram Moolenaar371d5402006-03-20 21:47:49 +00005279 {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005281 {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {(char_u *)"\316u <C-End>", INSERT+CMDLINE},
5283
5284 /* paste, copy and cut */
5285# ifdef FEAT_CLIPBOARD
5286# ifdef DJGPP
5287 {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005288 {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005290 {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291# if 0 /* Shift-Del produces the same code as Del */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005292 {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00005294 {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5295 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296# else
5297 {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */
Bram Moolenaar09092152010-08-08 16:38:42 +02005298 {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005300 {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */
5301 {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */
5302 {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */
5303 {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304# endif
5305# else
5306 {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005307 {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */
Bram Moolenaar371d5402006-03-20 21:47:49 +00005309 {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */
5310 {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */
5311 {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312# endif
5313# endif
5314#endif
5315
5316#if defined(MACOS)
5317 /* Use the Standard MacOS binding. */
5318 /* paste, copy and cut */
5319 {(char_u *)"<D-v> \"*P", NORMAL},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005320 {(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
Bram Moolenaar371d5402006-03-20 21:47:49 +00005322 {(char_u *)"<D-c> \"*y", VIS_SEL},
5323 {(char_u *)"<D-x> \"*d", VIS_SEL},
5324 {(char_u *)"<Backspace> \"-d", VIS_SEL},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326};
Bram Moolenaar371d5402006-03-20 21:47:49 +00005327
5328# undef VIS_SEL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331/*
5332 * Set up default mappings.
5333 */
5334 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005335init_mappings(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005337#if defined(MSDOS) || defined(MSWIN) ||defined(MACOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 int i;
5339
5340 for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
5341 add_map(initmappings[i].arg, initmappings[i].mode);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343}
5344
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005345#if defined(MSDOS) || defined(MSWIN) \
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005346 || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347/*
5348 * Add a mapping "map" for mode "mode".
5349 * Need to put string in allocated memory, because do_map() will modify it.
5350 */
5351 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005352add_map(char_u *map, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 char_u *s;
5355 char_u *cpo_save = p_cpo;
5356
5357 p_cpo = (char_u *)""; /* Allow <> notation */
5358 s = vim_strsave(map);
5359 if (s != NULL)
5360 {
5361 (void)do_map(0, s, mode, FALSE);
5362 vim_free(s);
5363 }
5364 p_cpo = cpo_save;
5365}
Bram Moolenaar52b4b552005-03-07 23:00:57 +00005366#endif