blob: fe1cc7e2ef214948ed66b8051e392d50bc89aabc [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar285e3352018-04-18 23:01:13 +020043static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47static int typeahead_char = 0; /* typeahead char that's not flushed */
48
49/*
50 * when block_redo is TRUE redo buffer will not be changed
51 * used by edit() to repeat insertions and 'V' command for redoing
52 */
53static int block_redo = FALSE;
54
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +000055static int KeyNoremap = 0; /* remapping flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020058 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 *
60 * typebuf.tb_buf[] contains all characters that are not consumed yet.
61 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
62 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
64 * The head of the buffer may contain the result of mappings, abbreviations
65 * and @a commands. The length of this part is typebuf.tb_maplen.
66 * typebuf.tb_silent is the part where <silent> applies.
67 * After the head are characters that come from the terminal.
68 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
69 * should not be considered for abbreviations.
70 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
71 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
72 * contains RM_NONE for the characters that are not to be remapped.
73 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
74 * (typebuf has been put in globals.h, because check_termcode() needs it).
75 */
76#define RM_YES 0 /* tb_noremap: remap */
77#define RM_NONE 1 /* tb_noremap: don't remap */
78#define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000079#define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/* typebuf.tb_buf has three parts: room in front (for result of mappings), the
82 * middle for typeahead and room for new characters (which needs to be 3 *
83 * MAXMAPLEN) for the Amiga).
84 */
85#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
86static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
87static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
88
89static int last_recorded_len = 0; /* number of last recorded chars */
90
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void may_sync_undo(void);
94static void closescript(void);
95static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020096static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
98/*
99 * Free and clear a buffer.
100 */
101 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100102free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100104 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
Bram Moolenaar285e3352018-04-18 23:01:13 +0200106 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 {
108 np = p->b_next;
109 vim_free(p);
110 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200111 buf->bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112}
113
114/*
115 * Return the contents of a buffer as a single string.
116 * K_SPECIAL and CSI in the returned string are escaped.
117 */
118 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100119get_buffcont(
120 buffheader_T *buffer,
121 int dozero) /* count == zero is not an error */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 long_u count = 0;
124 char_u *p = NULL;
125 char_u *p2;
126 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200127 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129 /* compute the total length of the string */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200130 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 count += (long_u)STRLEN(bp->b_str);
132
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200133 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200136 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 for (str = bp->b_str; *str; )
138 *p2++ = *str++;
139 *p2 = NUL;
140 }
141 return (p);
142}
143
144/*
145 * Return the contents of the record buffer as a single string
146 * and clear the record buffer.
147 * K_SPECIAL and CSI in the returned string are escaped.
148 */
149 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100150get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151{
152 char_u *p;
153 size_t len;
154
155 p = get_buffcont(&recordbuff, TRUE);
156 free_buff(&recordbuff);
157
158 /*
159 * Remove the characters that were added the last time, these must be the
160 * (possibly mapped) characters that stopped the recording.
161 */
162 len = STRLEN(p);
163 if ((int)len >= last_recorded_len)
164 {
165 len -= last_recorded_len;
166 p[len] = NUL;
167 }
168
169 /*
170 * When stopping recording from Insert mode with CTRL-O q, also remove the
171 * CTRL-O.
172 */
173 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
174 p[len - 1] = NUL;
175
176 return (p);
177}
178
179/*
180 * Return the contents of the redo buffer as a single string.
181 * K_SPECIAL and CSI in the returned string are escaped.
182 */
183 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100184get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000186 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
189/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000190 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 * K_SPECIAL and CSI should have been escaped already.
192 */
193 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100194add_buff(
195 buffheader_T *buf,
196 char_u *s,
197 long slen) /* length of "s" or -1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100199 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200200 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202 if (slen < 0)
203 slen = (long)STRLEN(s);
204 if (slen == 0) /* don't add empty strings */
205 return;
206
Bram Moolenaar285e3352018-04-18 23:01:13 +0200207 if (buf->bh_first.b_next == NULL) /* first add to list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {
209 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200210 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212 else if (buf->bh_curr == NULL) /* buffer has already been read */
213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100214 iemsg(_("E222: Add to read buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 return;
216 }
217 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200218 mch_memmove(buf->bh_first.b_next->b_str,
219 buf->bh_first.b_next->b_str + buf->bh_index,
220 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 buf->bh_index = 0;
222
223 if (buf->bh_space >= (int)slen)
224 {
225 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000226 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 buf->bh_space -= slen;
228 }
229 else
230 {
231 if (slen < MINIMAL_SIZE)
232 len = MINIMAL_SIZE;
233 else
234 len = slen;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200235 p = alloc(sizeof(buffblock_T) + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 if (p == NULL)
237 return; /* no space, just forget it */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000238 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000239 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar285e3352018-04-18 23:01:13 +0200241 p->b_next = buf->bh_curr->b_next;
242 buf->bh_curr->b_next = p;
243 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245 return;
246}
247
248/*
249 * Add number "n" to buffer "buf".
250 */
251 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100252add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 char_u number[32];
255
256 sprintf((char *)number, "%ld", n);
257 add_buff(buf, number, -1L);
258}
259
260/*
261 * Add character 'c' to buffer "buf".
262 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
263 */
264 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100265add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 char_u bytes[MB_MAXBYTES + 1];
268 int len;
269 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 char_u temp[4];
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (IS_SPECIAL(c))
273 len = 1;
274 else
275 len = (*mb_char2bytes)(c, bytes);
276 for (i = 0; i < len; ++i)
277 {
278 if (!IS_SPECIAL(c))
279 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
282 {
283 /* translate special key code into three byte sequence */
284 temp[0] = K_SPECIAL;
285 temp[1] = K_SECOND(c);
286 temp[2] = K_THIRD(c);
287 temp[3] = NUL;
288 }
289#ifdef FEAT_GUI
290 else if (c == CSI)
291 {
292 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
293 temp[0] = CSI;
294 temp[1] = KS_EXTRA;
295 temp[2] = (int)KE_CSI;
296 temp[3] = NUL;
297 }
298#endif
299 else
300 {
301 temp[0] = c;
302 temp[1] = NUL;
303 }
304 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306}
307
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100308/* First read ahead buffer. Used for translated commands. */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200309static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100310
311/* Second read ahead buffer. Used for redo. */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200312static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100315 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
316 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 * If advance == TRUE go to the next char.
318 * No translation is done K_SPECIAL and CSI are escaped.
319 */
320 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100321read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100323 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100325 c = read_readbuf(&readbuf1, advance);
326 if (c == NUL)
327 c = read_readbuf(&readbuf2, advance);
328 return c;
329}
330
331 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100332read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100333{
334 char_u c;
335 buffblock_T *curr;
336
Bram Moolenaar285e3352018-04-18 23:01:13 +0200337 if (buf->bh_first.b_next == NULL) /* buffer is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 return NUL;
339
Bram Moolenaar285e3352018-04-18 23:01:13 +0200340 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100341 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343 if (advance)
344 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100345 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200347 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100349 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351 }
352 return c;
353}
354
355/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100356 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 */
358 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100359start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200361 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200363 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100364 readbuf1.bh_space = 0;
365 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200366 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100367 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371}
372
373/*
374 * Return TRUE if the stuff buffer is empty.
375 */
376 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100377stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200379 return (readbuf1.bh_first.b_next == NULL
380 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100381}
382
Bram Moolenaar113e1072019-01-20 15:30:40 +0100383#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100384/*
385 * Return TRUE if readbuf1 is empty. There may still be redo characters in
386 * redbuf2.
387 */
388 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100389readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200391 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
395/*
396 * Set a typeahead character that won't be flushed.
397 */
398 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100399typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 typeahead_char = c;
402}
403
404/*
405 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100406 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 * flush all typeahead characters (used when interrupted by a CTRL-C).
408 */
409 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200410flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
412 init_typebuf();
413
414 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100415 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 ;
417
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200418 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200420 // remove mapped characters at the start only
421 typebuf.tb_off += typebuf.tb_maplen;
422 typebuf.tb_len -= typebuf.tb_maplen;
423 }
424 else
425 {
426 // remove typeahead
427 if (flush_typeahead == FLUSH_INPUT)
428 // We have to get all characters, because we may delete the first
429 // part of an escape sequence. In an xterm we get one char at a
430 // time and we have to get them all.
431 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
432 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 typebuf.tb_off = MAXMAPLEN;
434 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200435#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
436 /* Reset the flag that text received from a client or from feedkeys()
437 * was inserted in the typeahead buffer. */
438 typebuf_was_filled = FALSE;
439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 typebuf.tb_maplen = 0;
442 typebuf.tb_silent = 0;
443 cmd_silent = FALSE;
444 typebuf.tb_no_abbr_cnt = 0;
445}
446
447/*
448 * The previous contents of the redo buffer is kept in old_redobuffer.
449 * This is used for the CTRL-O <.> command in insert mode.
450 */
451 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100452ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454 if (!block_redo)
455 {
456 free_buff(&old_redobuff);
457 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200458 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 }
460}
461
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100462/*
463 * Discard the contents of the redo buffer and restore the previous redo
464 * buffer.
465 */
466 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100467CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100468{
469 if (!block_redo)
470 {
471 free_buff(&redobuff);
472 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200473 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100474 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100475 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100476 ;
477 }
478}
479
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480/*
481 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
482 * Used before executing autocommands and user functions.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200485saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 char_u *s;
488
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200489 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200490 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200491 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200492 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200494 /* Make a copy, so that ":normal ." in a function works. */
495 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
496 if (s != NULL)
497 {
498 add_buff(&redobuff, s, -1L);
499 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 }
501}
502
503/*
504 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
505 * Used after executing autocommands and user functions.
506 */
507 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200508restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200510 free_buff(&redobuff);
511 redobuff = save_redo->sr_redobuff;
512 free_buff(&old_redobuff);
513 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516/*
517 * Append "s" to the redo buffer.
518 * K_SPECIAL and CSI should already have been escaped.
519 */
520 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100521AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 if (!block_redo)
524 add_buff(&redobuff, s, -1L);
525}
526
527/*
528 * Append to Redo buffer literally, escaping special characters with CTRL-V.
529 * K_SPECIAL and CSI are escaped as well.
530 */
531 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100532AppendToRedobuffLit(
533 char_u *str,
534 int len) /* length of "str" or -1 for up to the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000536 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 int c;
538 char_u *start;
539
540 if (block_redo)
541 return;
542
Bram Moolenaarebefac62005-12-28 22:39:57 +0000543 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {
545 /* Put a string of normal characters in the redo buffer (that's
546 * faster). */
547 start = s;
548 while (*s >= ' '
549#ifndef EBCDIC
550 && *s < DEL /* EBCDIC: all chars above space are normal */
551#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000552 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 ++s;
554
555 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
556 * typed next. */
557 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
558 --s;
559 if (s > start)
560 add_buff(&redobuff, start, (long)(s - start));
561
Bram Moolenaarebefac62005-12-28 22:39:57 +0000562 if (*s == NUL || (len >= 0 && s - str >= len))
563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarebefac62005-12-28 22:39:57 +0000565 /* Handle a special or multibyte character. */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000566 if (has_mbyte)
567 /* Handle composing chars separately. */
568 c = mb_cptr2char_adv(&s);
569 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000570 c = *s++;
571 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
572 add_char_buff(&redobuff, Ctrl_V);
573
574 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
575 if (*s == NUL && c == '0')
576#ifdef EBCDIC
577 add_buff(&redobuff, (char_u *)"xf0", 3L);
578#else
579 add_buff(&redobuff, (char_u *)"048", 3L);
580#endif
581 else
582 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584}
585
586/*
587 * Append a character to the redo buffer.
588 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
589 */
590 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100591AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 if (!block_redo)
594 add_char_buff(&redobuff, c);
595}
596
597/*
598 * Append a number to the redo buffer.
599 */
600 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100601AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 if (!block_redo)
604 add_num_buff(&redobuff, n);
605}
606
607/*
608 * Append string "s" to the stuff buffer.
609 * CSI and K_SPECIAL must already have been escaped.
610 */
611 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100612stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100614 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615}
616
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200617/*
618 * Append string "s" to the redo stuff buffer.
619 * CSI and K_SPECIAL must already have been escaped.
620 */
621 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100622stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200623{
624 add_buff(&readbuf2, s, -1L);
625}
626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100628stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100630 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631}
632
633#if defined(FEAT_EVAL) || defined(PROTO)
634/*
635 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
636 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200637 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100640stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200642 int c;
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 while (*s != NUL)
645 {
646 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
647 {
648 /* Insert special key literally. */
649 stuffReadbuffLen(s, 3L);
650 s += 3;
651 }
652 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200653 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200654 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 if (c == CAR || c == NL || c == ESC)
656 c = ' ';
657 stuffcharReadbuff(c);
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660}
661#endif
662
663/*
664 * Append a character to the stuff buffer.
665 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
666 */
667 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100668stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100670 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671}
672
673/*
674 * Append a number to the stuff buffer.
675 */
676 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100677stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100679 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680}
681
682/*
683 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
684 * multibyte characters.
685 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100686 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
687 * otherwise.
688 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 */
690 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100691read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100693 static buffblock_T *bp;
694 static char_u *p;
695 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100696 int n;
697 char_u buf[MB_MAXBYTES + 1];
698 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
700 if (init)
701 {
702 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200703 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200705 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (bp == NULL)
707 return FAIL;
708 p = bp->b_str;
709 return OK;
710 }
711 if ((c = *p) != NUL)
712 {
713 /* Reverse the conversion done by add_char_buff() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 /* For a multi-byte character get all the bytes and return the
715 * converted character. */
716 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
717 n = MB_BYTE2LEN_CHECK(c);
718 else
719 n = 1;
720 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
722 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
723 {
724 c = TO_SPECIAL(p[1], p[2]);
725 p += 2;
726 }
727#ifdef FEAT_GUI
728 if (c == CSI) /* escaped CSI */
729 p += 2;
730#endif
731 if (*++p == NUL && bp->b_next != NULL)
732 {
733 bp = bp->b_next;
734 p = bp->b_str;
735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 buf[i] = c;
737 if (i == n - 1) /* last byte of a character */
738 {
739 if (n != 1)
740 c = (*mb_ptr2char)(buf);
741 break;
742 }
743 c = *p;
744 if (c == NUL) /* cannot happen? */
745 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747 }
748
749 return c;
750}
751
752/*
753 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
754 * If old_redo is TRUE, use old_redobuff instead of redobuff.
755 * The escaped K_SPECIAL and CSI are copied without translation.
756 */
757 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100758copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 int c;
761
762 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100763 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764}
765
766/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100767 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 * Insert the redo count into the command.
769 * If "old_redo" is TRUE, the last but one command is repeated
770 * instead of the last command (inserting text). This is used for
771 * CTRL-O <.> in insert mode
772 *
773 * return FAIL for failure, OK otherwise
774 */
775 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100776start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 int c;
779
780 /* init the pointers; return if nothing to redo */
781 if (read_redo(TRUE, old_redo) == FAIL)
782 return FAIL;
783
784 c = read_redo(FALSE, old_redo);
785
786 /* copy the buffer name, if present */
787 if (c == '"')
788 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100789 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 c = read_redo(FALSE, old_redo);
791
792 /* if a numbered buffer is used, increment the number */
793 if (c >= '1' && c < '9')
794 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100795 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200796
797 /* the expression register should be re-evaluated */
798 if (c == '=')
799 {
800 add_char_buff(&readbuf2, CAR);
801 cmd_silent = TRUE;
802 }
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 c = read_redo(FALSE, old_redo);
805 }
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (c == 'v') /* redo Visual */
808 {
809 VIsual = curwin->w_cursor;
810 VIsual_active = TRUE;
811 VIsual_select = FALSE;
812 VIsual_reselect = TRUE;
813 redo_VIsual_busy = TRUE;
814 c = read_redo(FALSE, old_redo);
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /* try to enter the count (in place of a previous count) */
818 if (count)
819 {
820 while (VIM_ISDIGIT(c)) /* skip "old" count */
821 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100822 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824
825 /* copy from the redo buffer into the stuff buffer */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100826 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 copy_redo(old_redo);
828 return OK;
829}
830
831/*
832 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100833 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * return FAIL for failure, OK otherwise
835 */
836 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100837start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 int c;
840
841 if (read_redo(TRUE, FALSE) == FAIL)
842 return FAIL;
843 start_stuff();
844
845 /* skip the count and the command character */
846 while ((c = read_redo(FALSE, FALSE)) != NUL)
847 {
848 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
849 {
850 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100851 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 break;
853 }
854 }
855
856 /* copy the typed text from the redo buffer into the stuff buffer */
857 copy_redo(FALSE);
858 block_redo = TRUE;
859 return OK;
860}
861
862 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100863stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 block_redo = FALSE;
866}
867
868/*
869 * Initialize typebuf.tb_buf to point to typebuf_init.
870 * alloc() cannot be used here: In out-of-memory situations it would
871 * be impossible to type anything.
872 */
873 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100874init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 if (typebuf.tb_buf == NULL)
877 {
878 typebuf.tb_buf = typebuf_init;
879 typebuf.tb_noremap = noremapbuf_init;
880 typebuf.tb_buflen = TYPELEN_INIT;
881 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200882 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 typebuf.tb_change_cnt = 1;
884 }
885}
886
887/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200888 * Returns TRUE when keys cannot be remapped.
889 */
890 int
891noremap_keys(void)
892{
893 return KeyNoremap & (RM_NONE|RM_SCRIPT);
894}
895
896/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100897 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
898 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 *
900 * If noremap is REMAP_YES, new string can be mapped again.
901 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000902 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
903 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
905 * script-local mappings.
906 * If noremap is > 0, that many characters of the new string cannot be mapped.
907 *
908 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
909 * offset is non-zero!).
910 *
911 * If silent is TRUE, cmd_silent is set when the characters are obtained.
912 *
913 * return FAIL for failure, OK otherwise
914 */
915 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100916ins_typebuf(
917 char_u *str,
918 int noremap,
919 int offset,
920 int nottyped,
921 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 char_u *s1, *s2;
924 int newlen;
925 int addlen;
926 int i;
927 int newoff;
928 int val;
929 int nrm;
930
931 init_typebuf();
932 if (++typebuf.tb_change_cnt == 0)
933 typebuf.tb_change_cnt = 1;
934
935 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (offset == 0 && addlen <= typebuf.tb_off)
938 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100939 /*
940 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 typebuf.tb_off -= addlen;
943 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
944 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200945 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
946 >= addlen + 3 * (MAXMAPLEN + 4))
947 {
948 /*
949 * Buffer is empty and string fits in the existing buffer.
950 * Leave some space before and after, if possible.
951 */
952 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
953 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 else
956 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100957 /*
958 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200959 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100960 * characters. We add some extra room to avoid having to allocate too
961 * often.
962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 newoff = MAXMAPLEN + 4;
964 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
965 if (newlen < 0) /* string is getting too long */
966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 emsg(_(e_toocompl)); /* also calls flush_buffers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 setcursor();
969 return FAIL;
970 }
971 s1 = alloc(newlen);
972 if (s1 == NULL) /* out of memory */
973 return FAIL;
974 s2 = alloc(newlen);
975 if (s2 == NULL) /* out of memory */
976 {
977 vim_free(s1);
978 return FAIL;
979 }
980 typebuf.tb_buflen = newlen;
981
982 /* copy the old chars, before the insertion point */
983 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
984 (size_t)offset);
985 /* copy the new chars */
986 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
987 /* copy the old chars, after the insertion point, including the NUL at
988 * the end */
989 mch_memmove(s1 + newoff + offset + addlen,
990 typebuf.tb_buf + typebuf.tb_off + offset,
991 (size_t)(typebuf.tb_len - offset + 1));
992 if (typebuf.tb_buf != typebuf_init)
993 vim_free(typebuf.tb_buf);
994 typebuf.tb_buf = s1;
995
996 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
997 (size_t)offset);
998 mch_memmove(s2 + newoff + offset + addlen,
999 typebuf.tb_noremap + typebuf.tb_off + offset,
1000 (size_t)(typebuf.tb_len - offset));
1001 if (typebuf.tb_noremap != noremapbuf_init)
1002 vim_free(typebuf.tb_noremap);
1003 typebuf.tb_noremap = s2;
1004
1005 typebuf.tb_off = newoff;
1006 }
1007 typebuf.tb_len += addlen;
1008
1009 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
1010 if (noremap == REMAP_SCRIPT)
1011 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001012 else if (noremap == REMAP_SKIP)
1013 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else
1015 val = RM_NONE;
1016
1017 /*
1018 * Adjust typebuf.tb_noremap[] for the new characters:
1019 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1020 * (sometimes) not remappable
1021 * If noremap == REMAP_YES: all the new characters are mappable
1022 * If noremap > 0: "noremap" characters are not remappable, the rest
1023 * mappable
1024 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001025 if (noremap == REMAP_SKIP)
1026 nrm = 1;
1027 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 nrm = addlen;
1029 else
1030 nrm = noremap;
1031 for (i = 0; i < addlen; ++i)
1032 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1033 (--nrm >= 0) ? val : RM_YES;
1034
1035 /* tb_maplen and tb_silent only remember the length of mapped and/or
1036 * silent mappings at the start of the buffer, assuming that a mapped
1037 * sequence doesn't result in typed characters. */
1038 if (nottyped || typebuf.tb_maplen > offset)
1039 typebuf.tb_maplen += addlen;
1040 if (silent || typebuf.tb_silent > offset)
1041 {
1042 typebuf.tb_silent += addlen;
1043 cmd_silent = TRUE;
1044 }
1045 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1046 typebuf.tb_no_abbr_cnt += addlen;
1047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001052 * Put character "c" back into the typeahead buffer.
1053 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001054 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1055 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001056 */
1057 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001058ins_char_typebuf(int c)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001059{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001060 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001061 if (IS_SPECIAL(c))
1062 {
1063 buf[0] = K_SPECIAL;
1064 buf[1] = K_SECOND(c);
1065 buf[2] = K_THIRD(c);
1066 buf[3] = NUL;
1067 }
1068 else
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001069 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001070 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001071}
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001075 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001076 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1078 * changed it was reallocated and the old pointer can no longer be used.
1079 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1080 * that was just added.
1081 */
1082 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001083typebuf_changed(
1084 int tb_change_cnt) /* old value of typebuf.tb_change_cnt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001087#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1088 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#endif
1090 ));
1091}
1092
1093/*
1094 * Return TRUE if there are no characters in the typeahead buffer that have
1095 * not been typed (result from a mapping or come from ":normal").
1096 */
1097 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001098typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 return typebuf.tb_maplen == 0;
1101}
1102
1103/*
1104 * Return the number of characters that are mapped (or not typed).
1105 */
1106 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001107typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 return typebuf.tb_maplen;
1110}
1111
1112/*
1113 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1114 */
1115 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001116del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 int i;
1119
1120 if (len == 0)
1121 return; /* nothing to do */
1122
1123 typebuf.tb_len -= len;
1124
1125 /*
1126 * Easy case: Just increase typebuf.tb_off.
1127 */
1128 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1129 >= 3 * MAXMAPLEN + 3)
1130 typebuf.tb_off += len;
1131 /*
1132 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1133 */
1134 else
1135 {
1136 i = typebuf.tb_off + offset;
1137 /*
1138 * Leave some extra room at the end to avoid reallocation.
1139 */
1140 if (typebuf.tb_off > MAXMAPLEN)
1141 {
1142 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1143 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1144 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1145 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1146 typebuf.tb_off = MAXMAPLEN;
1147 }
1148 /* adjust typebuf.tb_buf (include the NUL at the end) */
1149 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1150 typebuf.tb_buf + i + len,
1151 (size_t)(typebuf.tb_len - offset + 1));
1152 /* adjust typebuf.tb_noremap[] */
1153 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1154 typebuf.tb_noremap + i + len,
1155 (size_t)(typebuf.tb_len - offset));
1156 }
1157
1158 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1159 {
1160 if (typebuf.tb_maplen < offset + len)
1161 typebuf.tb_maplen = offset;
1162 else
1163 typebuf.tb_maplen -= len;
1164 }
1165 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1166 {
1167 if (typebuf.tb_silent < offset + len)
1168 typebuf.tb_silent = offset;
1169 else
1170 typebuf.tb_silent -= len;
1171 }
1172 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1173 {
1174 if (typebuf.tb_no_abbr_cnt < offset + len)
1175 typebuf.tb_no_abbr_cnt = offset;
1176 else
1177 typebuf.tb_no_abbr_cnt -= len;
1178 }
1179
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001180#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001181 /* Reset the flag that text received from a client or from feedkeys()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001182 * was inserted in the typeahead buffer. */
1183 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185 if (++typebuf.tb_change_cnt == 0)
1186 typebuf.tb_change_cnt = 1;
1187}
1188
1189/*
1190 * Write typed characters to script file.
1191 * If recording is on put the character in the recordbuffer.
1192 */
1193 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001194gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001196 char_u *s = chars;
1197 int i;
1198 static char_u buf[4];
1199 static int buflen = 0;
1200 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001202 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001204 buf[buflen++] = *s++;
1205
1206 // When receiving a special key sequence, store it until we have all
1207 // the bytes and we can decide what to do with it.
1208 if (buflen == 1 && buf[0] == K_SPECIAL)
1209 continue;
1210 if (buflen == 2)
1211 continue;
1212 if (buflen == 3 && buf[1] == KS_EXTRA
1213 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1214 {
1215 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1216 // recording.
1217 buflen = 0;
1218 continue;
1219 }
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 /* Handle one byte at a time; no translation to be done. */
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001222 for (i = 0; i < buflen; ++i)
1223 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001225 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001227 buf[buflen] = NUL;
1228 add_buff(&recordbuff, buf, (long)buflen);
1229 /* remember how many chars were last recorded */
1230 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001232 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234 may_sync_undo();
1235
1236#ifdef FEAT_EVAL
1237 /* output "debug mode" message next time in debug mode */
1238 debug_did_msg = FALSE;
1239#endif
1240
1241 /* Since characters have been typed, consider the following to be in
1242 * another mapping. Search string will be kept in history. */
1243 ++maptick;
1244}
1245
1246/*
1247 * Sync undo. Called when typed characters are obtained from the typeahead
1248 * buffer, or when a menu is used.
1249 * Do not sync:
1250 * - In Insert mode, unless cursor key has been used.
1251 * - While reading a script file.
1252 * - When no_u_sync is non-zero.
1253 */
1254 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001255may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001258 && scriptin[curscript] == NULL)
1259 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260}
1261
1262/*
1263 * Make "typebuf" empty and allocate new buffers.
1264 * Returns FAIL when out of memory.
1265 */
1266 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001267alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 typebuf.tb_buf = alloc(TYPELEN_INIT);
1270 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1271 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1272 {
1273 free_typebuf();
1274 return FAIL;
1275 }
1276 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001277 typebuf.tb_off = MAXMAPLEN + 4; /* can insert without realloc */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 typebuf.tb_len = 0;
1279 typebuf.tb_maplen = 0;
1280 typebuf.tb_silent = 0;
1281 typebuf.tb_no_abbr_cnt = 0;
1282 if (++typebuf.tb_change_cnt == 0)
1283 typebuf.tb_change_cnt = 1;
1284 return OK;
1285}
1286
1287/*
1288 * Free the buffers of "typebuf".
1289 */
1290 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001291free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001293 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001294 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001295 else
1296 vim_free(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001297 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001298 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001299 else
1300 vim_free(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
1303/*
1304 * When doing ":so! file", the current typeahead needs to be saved, and
1305 * restored when "file" has been read completely.
1306 */
1307static typebuf_T saved_typebuf[NSCRIPT];
1308
1309 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001310save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 init_typebuf();
1313 saved_typebuf[curscript] = typebuf;
1314 /* If out of memory: restore typebuf and close file. */
1315 if (alloc_typebuf() == FAIL)
1316 {
1317 closescript();
1318 return FAIL;
1319 }
1320 return OK;
1321}
1322
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001323static int old_char = -1; /* character put back by vungetc() */
1324static int old_mod_mask; /* mod_mask for ungotten character */
Bram Moolenaarb8978712013-03-16 21:42:16 +01001325#ifdef FEAT_MOUSE
1326static int old_mouse_row; /* mouse_row related to old_char */
1327static int old_mouse_col; /* mouse_col related to old_char */
1328#endif
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330/*
1331 * Save all three kinds of typeahead, so that the user must type at a prompt.
1332 */
1333 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001334save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 tp->save_typebuf = typebuf;
1337 tp->typebuf_valid = (alloc_typebuf() == OK);
1338 if (!tp->typebuf_valid)
1339 typebuf = tp->save_typebuf;
1340
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001341 tp->old_char = old_char;
1342 tp->old_mod_mask = old_mod_mask;
1343 old_char = -1;
1344
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001345 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001346 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001347 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001348 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349# ifdef USE_INPUT_BUF
1350 tp->save_inputbuf = get_input_buf();
1351# endif
1352}
1353
1354/*
1355 * Restore the typeahead to what it was before calling save_typeahead().
1356 * The allocated memory is freed, can only be called once!
1357 */
1358 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001359restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 if (tp->typebuf_valid)
1362 {
1363 free_typebuf();
1364 typebuf = tp->save_typebuf;
1365 }
1366
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001367 old_char = tp->old_char;
1368 old_mod_mask = tp->old_mod_mask;
1369
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001370 free_buff(&readbuf1);
1371 readbuf1 = tp->save_readbuf1;
1372 free_buff(&readbuf2);
1373 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374# ifdef USE_INPUT_BUF
1375 set_input_buf(tp->save_inputbuf);
1376# endif
1377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379/*
1380 * Open a new script file for the ":source!" command.
1381 */
1382 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001383openscript(
1384 char_u *name,
1385 int directly) /* when TRUE execute directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 if (curscript + 1 == NSCRIPT)
1388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001389 emsg(_(e_nesting));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return;
1391 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001392
1393 // Disallow sourcing a file in the sandbox, the commands would be executed
1394 // later, possibly outside of the sandbox.
1395 if (check_secure())
1396 return;
1397
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001398#ifdef FEAT_EVAL
1399 if (ignore_script)
1400 /* Not reading from script, also don't open one. Warning message? */
1401 return;
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 if (scriptin[curscript] != NULL) /* already reading script */
1405 ++curscript;
1406 /* use NameBuff for expanded name */
1407 expand_env(name, NameBuff, MAXPATHL);
1408 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001410 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (curscript)
1412 --curscript;
1413 return;
1414 }
1415 if (save_typebuf() == FAIL)
1416 return;
1417
1418 /*
1419 * Execute the commands from the file right now when using ":source!"
1420 * after ":global" or ":argdo" or in a loop. Also when another command
1421 * follows. This means the display won't be updated. Don't do this
1422 * always, "make test" would fail.
1423 */
1424 if (directly)
1425 {
1426 oparg_T oa;
1427 int oldcurscript;
1428 int save_State = State;
1429 int save_restart_edit = restart_edit;
1430 int save_insertmode = p_im;
1431 int save_finish_op = finish_op;
1432 int save_msg_scroll = msg_scroll;
1433
1434 State = NORMAL;
1435 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1436 restart_edit = 0; /* don't go to Insert mode */
1437 p_im = FALSE; /* don't use 'insertmode' */
1438 clear_oparg(&oa);
1439 finish_op = FALSE;
1440
1441 oldcurscript = curscript;
1442 do
1443 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001444 update_topline_cursor(); // update cursor position and topline
1445 normal_cmd(&oa, FALSE); // execute one command
1446 vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448 while (scriptin[oldcurscript] != NULL);
1449
1450 State = save_State;
1451 msg_scroll = save_msg_scroll;
1452 restart_edit = save_restart_edit;
1453 p_im = save_insertmode;
1454 finish_op = save_finish_op;
1455 }
1456}
1457
1458/*
1459 * Close the currently active input script.
1460 */
1461 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001462closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 free_typebuf();
1465 typebuf = saved_typebuf[curscript];
1466
1467 fclose(scriptin[curscript]);
1468 scriptin[curscript] = NULL;
1469 if (curscript > 0)
1470 --curscript;
1471}
1472
Bram Moolenaarea408852005-06-25 22:49:46 +00001473#if defined(EXITFREE) || defined(PROTO)
1474 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001475close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001476{
1477 while (scriptin[0] != NULL)
1478 closescript();
1479}
1480#endif
1481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1483/*
1484 * Return TRUE when reading keys from a script file.
1485 */
1486 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001487using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 return scriptin[curscript] != NULL;
1490}
1491#endif
1492
1493/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001494 * This function is called just before doing a blocking wait. Thus after
1495 * waiting 'updatetime' for a character to arrive.
1496 */
1497 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001498before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001499{
1500 updatescript(0);
1501#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001502 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001503 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001504#endif
1505}
1506
1507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * updatescipt() is called when a character can be written into the script file
1509 * or when we have waited some time for a character (c == 0)
1510 *
1511 * All the changed memfiles are synced if c == 0 or when the number of typed
1512 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1513 */
1514 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001515updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 static int count = 0;
1518
1519 if (c && scriptout)
1520 putc(c, scriptout);
1521 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1522 {
1523 ml_sync_all(c == 0, TRUE);
1524 count = 0;
1525 }
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
1529 * Get the next input character.
1530 * Can return a special key or a multi-byte character.
1531 * Can return NUL when called recursively, use safe_vgetc() if that's not
1532 * wanted.
1533 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1534 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001535 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
1537 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001538vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001542 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001545#ifdef FEAT_EVAL
1546 /* Do garbage collection when garbagecollect() was called previously and
1547 * we are now at the toplevel. */
1548 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001549 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001550#endif
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 /*
1553 * If a character was put back with vungetc, it was already processed.
1554 * Return it directly.
1555 */
1556 if (old_char != -1)
1557 {
1558 c = old_char;
1559 old_char = -1;
1560 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001561#ifdef FEAT_MOUSE
1562 mouse_row = old_mouse_row;
1563 mouse_col = old_mouse_col;
1564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001568 mod_mask = 0x0;
1569 last_recorded_len = 0;
1570 for (;;) // this is done twice if there are modifiers
1571 {
1572 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001573
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001574 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001575#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001576 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001577#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001578 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001580 // no mapping after modifier has been read
1581 ++no_mapping;
1582 ++allow_keys;
1583 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001585 c = vgetorpeek(TRUE);
1586 if (did_inc)
1587 {
1588 --no_mapping;
1589 --allow_keys;
1590 }
1591
1592 // Get two extra bytes for special keys
1593 if (c == K_SPECIAL
1594#ifdef FEAT_GUI
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001595 || (gui.in_use && c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001596#endif
1597 )
1598 {
1599 int save_allow_keys = allow_keys;
1600
1601 ++no_mapping;
1602 allow_keys = 0; // make sure BS is not found
1603 c2 = vgetorpeek(TRUE); // no mapping for these chars
1604 c = vgetorpeek(TRUE);
1605 --no_mapping;
1606 allow_keys = save_allow_keys;
1607 if (c2 == KS_MODIFIER)
1608 {
1609 mod_mask = c;
1610 continue;
1611 }
1612 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar4f974752019-02-17 17:44:42 +01001614#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001615 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1616 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001617 if (
1618# ifdef VIMDLL
1619 gui.in_use &&
1620# endif
1621 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001623 char_u name[200];
1624 int i;
1625
1626 // get menu path, it ends with a <CR>
1627 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1628 {
1629 name[i] = c;
1630 if (i < 199)
1631 ++i;
1632 }
1633 name[i] = NUL;
1634 gui_make_tearoff(name);
1635 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001638#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001639 // GTK: <F10> normally selects the menu, but it's passed until
1640 // here to allow mapping it. Intercept and invoke the GTK
1641 // behavior if it's not mapped.
1642 if (c == K_F10 && gui.menubar != NULL)
1643 {
1644 gtk_menu_shell_select_first(
1645 GTK_MENU_SHELL(gui.menubar), FALSE);
1646 continue;
1647 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#ifdef FEAT_GUI
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001650 if (gui.in_use)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001651 {
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001652 // Handle focus event here, so that the caller doesn't
1653 // need to know about it. Return K_IGNORE so that we loop
1654 // once (needed if 'lazyredraw' is set).
1655 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1656 {
1657 ui_focus_change(c == K_FOCUSGAINED);
1658 c = K_IGNORE;
1659 }
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001660
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001661 // Translate K_CSI to CSI. The special key is only used
1662 // to avoid it being recognized as the start of a special
1663 // key.
1664 if (c == K_CSI)
1665 c = CSI;
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001668 }
1669 // a keypad or special function key was not mapped, use it like
1670 // its ASCII equivalent
1671 switch (c)
1672 {
1673 case K_KPLUS: c = '+'; break;
1674 case K_KMINUS: c = '-'; break;
1675 case K_KDIVIDE: c = '/'; break;
1676 case K_KMULTIPLY: c = '*'; break;
1677 case K_KENTER: c = CAR; break;
1678 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001679#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001680 // Can be either '.' or a ',',
1681 // depending on the type of keypad.
1682 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001683#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001684 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001685#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001686 case K_K0: c = '0'; break;
1687 case K_K1: c = '1'; break;
1688 case K_K2: c = '2'; break;
1689 case K_K3: c = '3'; break;
1690 case K_K4: c = '4'; break;
1691 case K_K5: c = '5'; break;
1692 case K_K6: c = '6'; break;
1693 case K_K7: c = '7'; break;
1694 case K_K8: c = '8'; break;
1695 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001696
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001697 case K_XHOME:
1698 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001699 {
1700 c = K_S_HOME;
1701 mod_mask = 0;
1702 }
1703 else if (mod_mask == MOD_MASK_CTRL)
1704 {
1705 c = K_C_HOME;
1706 mod_mask = 0;
1707 }
1708 else
1709 c = K_HOME;
1710 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001711 case K_XEND:
1712 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001713 {
1714 c = K_S_END;
1715 mod_mask = 0;
1716 }
1717 else if (mod_mask == MOD_MASK_CTRL)
1718 {
1719 c = K_C_END;
1720 mod_mask = 0;
1721 }
1722 else
1723 c = K_END;
1724 break;
1725
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001726 case K_XUP: c = K_UP; break;
1727 case K_XDOWN: c = K_DOWN; break;
1728 case K_XLEFT: c = K_LEFT; break;
1729 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001732 // For a multi-byte character get all the bytes and return the
1733 // converted character.
1734 // Note: This will loop until enough bytes are received!
1735 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1736 {
1737 ++no_mapping;
1738 buf[0] = c;
1739 for (i = 1; i < n; ++i)
1740 {
1741 buf[i] = vgetorpeek(TRUE);
1742 if (buf[i] == K_SPECIAL
1743#ifdef FEAT_GUI
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001744 || (
1745# ifdef VIMDLL
1746 gui.in_use &&
1747# endif
1748 buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001749#endif
1750 )
1751 {
1752 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1753 // sequence, which represents a K_SPECIAL (0x80),
1754 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1755 // represents a CSI (0x9B),
1756 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1757 // too.
1758 c = vgetorpeek(TRUE);
1759 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1760 buf[i] = CSI;
1761 }
1762 }
1763 --no_mapping;
1764 c = (*mb_ptr2char)(buf);
1765 }
1766
1767 break;
1768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001770
1771#ifdef FEAT_EVAL
1772 /*
1773 * In the main loop "may_garbage_collect" can be set to do garbage
1774 * collection in the first next vgetc(). It's disabled after that to
1775 * avoid internally used Lists and Dicts to be freed.
1776 */
1777 may_garbage_collect = FALSE;
1778#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001779#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001780 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001781 {
1782 /* Don't trigger 'balloonexpr' unless only the mouse was moved. */
1783 bevalexpr_due_set = FALSE;
1784 ui_remove_balloon();
1785 }
1786#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001787#ifdef FEAT_TEXT_PROP
1788 if (popup_do_filter(c))
1789 c = K_IGNORE;
1790#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001791
1792 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
1795/*
1796 * Like vgetc(), but never return a NUL when called recursively, get a key
1797 * directly from the user (ignoring typeahead).
1798 */
1799 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001800safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 int c;
1803
1804 c = vgetc();
1805 if (c == NUL)
1806 c = get_keystroke();
1807 return c;
1808}
1809
1810/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001811 * Like safe_vgetc(), but loop to handle K_IGNORE.
1812 * Also ignore scrollbar events.
1813 */
1814 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001815plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001816{
1817 int c;
1818
1819 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001820 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001821 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001822
1823 if (c == K_PS)
1824 /* Only handle the first pasted character. Drop the rest, since we
1825 * don't know what to do with it. */
1826 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1827
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001828 return c;
1829}
1830
1831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 * Check if a character is available, such that vgetc() will not block.
1833 * If the next character is a special character or multi-byte, the returned
1834 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001835 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001838vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 if (old_char != -1)
1841 return old_char;
1842 return vgetorpeek(FALSE);
1843}
1844
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001845#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
1847 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1848 * codes.
1849 */
1850 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001851vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 int c;
1854
1855 ++no_mapping;
1856 ++allow_keys;
1857 c = vpeekc();
1858 --no_mapping;
1859 --allow_keys;
1860 return c;
1861}
1862#endif
1863
Bram Moolenaar9a665ba2014-05-22 18:59:58 +02001864#if defined(FEAT_INS_EXPAND) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
1866 * Check if any character is available, also half an escape sequence.
1867 * Trick: when no typeahead found, but there is something in the typeahead
1868 * buffer, it must be an ESC that is recognized as the start of a key code.
1869 */
1870 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001871vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
1873 int c;
1874
1875 c = vpeekc();
1876 if (c == NUL && typebuf.tb_len > 0)
1877 c = ESC;
1878 return c;
1879}
1880#endif
1881
1882/*
1883 * Call vpeekc() without causing anything to be mapped.
1884 * Return TRUE if a character is available, FALSE otherwise.
1885 */
1886 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001887char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 int retval;
1890
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001891#ifdef FEAT_EVAL
Bram Moolenaard0573012017-10-28 21:11:06 +02001892 /* When test_override("char_avail", 1) was called pretend there is no
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001893 * typeahead. */
1894 if (disable_char_avail_for_testing)
1895 return FALSE;
1896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 ++no_mapping;
1898 retval = vpeekc();
1899 --no_mapping;
1900 return (retval != NUL);
1901}
1902
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001903typedef enum {
1904 map_result_fail, // failed, break loop
1905 map_result_get, // get a character from typeahead
1906 map_result_retry, // try to map again
1907 map_result_nomatch // no matching mapping, get char
1908} map_result_T;
1909
1910/*
1911 * Handle mappings in the typeahead buffer.
1912 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02001913 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001914 * - When there is no match yet, return map_result_nomatch, need to get more
1915 * typeahead.
1916 */
1917 static int
1918handle_mapping(
1919 int *keylenp,
1920 int *timedout,
1921 int *mapdepth)
1922{
1923 mapblock_T *mp = NULL;
1924 mapblock_T *mp2;
1925 mapblock_T *mp_match;
1926 int mp_match_len = 0;
1927 int max_mlen = 0;
1928 int tb_c1;
1929 int mlen;
1930#ifdef FEAT_LANGMAP
1931 int nolmaplen;
1932#endif
1933 int keylen = *keylenp;
1934 int i;
1935 int local_State = get_real_state();
1936
1937 /*
1938 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02001939 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001940 *
1941 * Don't look for mappings if:
1942 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
1943 * - maphash_valid not set: no mappings present.
1944 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
1945 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02001946 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001947 * - waiting for a char with --more--
1948 * - in Ctrl-X mode, and we get a valid char for that mode
1949 */
1950 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
1951 if (no_mapping == 0 && is_maphash_valid()
1952 && (no_zero_mapping == 0 || tb_c1 != '0')
1953 && (typebuf.tb_maplen == 0
1954 || (p_remap
1955 && (typebuf.tb_noremap[typebuf.tb_off]
1956 & (RM_NONE|RM_ABBR)) == 0))
1957 && !(p_paste && (State & (INSERT + CMDLINE)))
1958 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
1959 && State != ASKMORE
1960 && State != CONFIRM
1961#ifdef FEAT_INS_EXPAND
Bram Moolenaareda35f72019-08-03 14:59:44 +02001962 && !((ctrl_x_mode_not_default() && vim_is_ctrl_x_key(tb_c1))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001963 || ((compl_cont_status & CONT_LOCAL)
1964 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P)))
1965#endif
1966 )
1967 {
1968#ifdef FEAT_LANGMAP
1969 if (tb_c1 == K_SPECIAL)
1970 nolmaplen = 2;
1971 else
1972 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02001973 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
1974 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001975 nolmaplen = 0;
1976 }
1977#endif
1978 // First try buffer-local mappings.
1979 mp = get_buf_maphash_list(local_State, tb_c1);
1980 mp2 = get_maphash_list(local_State, tb_c1);
1981 if (mp == NULL)
1982 {
1983 // There are no buffer-local mappings.
1984 mp = mp2;
1985 mp2 = NULL;
1986 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02001987
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001988 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02001989 * Loop until a partly matching mapping is found or all (local)
1990 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001991 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02001992 * A full match is only accepted if there is no partly match, so "aa"
1993 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001994 */
1995 mp_match = NULL;
1996 mp_match_len = 0;
1997 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02001998 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02001999 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002000 // Only consider an entry if the first character matches and it is
2001 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002002 // Skip ":lmap" mappings if keys were mapped.
2003 if (mp->m_keys[0] == tb_c1
2004 && (mp->m_mode & local_State)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002005 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002006 {
2007#ifdef FEAT_LANGMAP
2008 int nomap = nolmaplen;
2009 int c2;
2010#endif
2011 // find the match length of this mapping
2012 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2013 {
2014#ifdef FEAT_LANGMAP
2015 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2016 if (nomap > 0)
2017 --nomap;
2018 else if (c2 == K_SPECIAL)
2019 nomap = 2;
2020 else
2021 LANGMAP_ADJUST(c2, TRUE);
2022 if (mp->m_keys[mlen] != c2)
2023#else
2024 if (mp->m_keys[mlen] !=
2025 typebuf.tb_buf[typebuf.tb_off + mlen])
2026#endif
2027 break;
2028 }
2029
Bram Moolenaareda35f72019-08-03 14:59:44 +02002030 // Don't allow mapping the first byte(s) of a multi-byte char.
2031 // Happens when mapping <M-a> and then changing 'encoding'.
2032 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002033 {
2034 char_u *p1 = mp->m_keys;
2035 char_u *p2 = mb_unescape(&p1);
2036
2037 if (has_mbyte && p2 != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002038 && MB_BYTE2LEN(tb_c1) > MB_PTR2LEN(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002039 mlen = 0;
2040 }
2041
2042 // Check an entry whether it matches.
2043 // - Full match: mlen == keylen
2044 // - Partly match: mlen == typebuf.tb_len
2045 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002046 if (mlen == keylen || (mlen == typebuf.tb_len
2047 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002048 {
2049 char_u *s;
2050 int n;
2051
Bram Moolenaareda35f72019-08-03 14:59:44 +02002052 // If only script-local mappings are allowed, check if the
2053 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002054 s = typebuf.tb_noremap + typebuf.tb_off;
2055 if (*s == RM_SCRIPT
2056 && (mp->m_keys[0] != K_SPECIAL
2057 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002058 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002059 continue;
2060
Bram Moolenaareda35f72019-08-03 14:59:44 +02002061 // If one of the typed keys cannot be remapped, skip the
2062 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002063 for (n = mlen; --n >= 0; )
2064 if (*s++ & (RM_NONE|RM_ABBR))
2065 break;
2066 if (n >= 0)
2067 continue;
2068
2069 if (keylen > typebuf.tb_len)
2070 {
2071 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002072 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002073 {
2074 // break at a partly match
2075 keylen = KEYLEN_PART_MAP;
2076 break;
2077 }
2078 }
2079 else if (keylen > mp_match_len)
2080 {
2081 // found a longer match
2082 mp_match = mp;
2083 mp_match_len = keylen;
2084 }
2085 }
2086 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002087 // No match; may have to check for termcode at next
2088 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002089 if (max_mlen < mlen)
2090 max_mlen = mlen;
2091 }
2092 }
2093
Bram Moolenaareda35f72019-08-03 14:59:44 +02002094 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002095 if (keylen != KEYLEN_PART_MAP)
2096 {
2097 mp = mp_match;
2098 keylen = mp_match_len;
2099 }
2100 }
2101
2102 /*
2103 * Check for match with 'pastetoggle'
2104 */
2105 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2106 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002107 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2108 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002109 break;
2110 if (p_pt[mlen] == NUL) // match
2111 {
2112 // write chars to script file(s)
2113 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002114 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2115 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002116
2117 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002118 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002119 if (!(State & INSERT))
2120 {
2121 msg_col = 0;
2122 msg_row = Rows - 1;
2123 msg_clr_eos(); // clear ruler
2124 }
2125 status_redraw_all();
2126 redraw_statuslines();
2127 showmode();
2128 setcursor();
2129 *keylenp = keylen;
2130 return map_result_retry;
2131 }
2132 // Need more chars for partly match.
2133 if (mlen == typebuf.tb_len)
2134 keylen = KEYLEN_PART_KEY;
2135 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002136 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002137 max_mlen = mlen + 1;
2138 }
2139
Bram Moolenaareda35f72019-08-03 14:59:44 +02002140 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002141 {
2142 int save_keylen = keylen;
2143
2144 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002145 * When no matching mapping found or found a non-matching mapping that
2146 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002147 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002148 * - mapping is allowed,
2149 * - keys have not been mapped,
2150 * - and not an ESC sequence, not in insert mode or p_ek is on,
2151 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002152 */
2153 if ((no_mapping == 0 || allow_keys != 0)
2154 && (typebuf.tb_maplen == 0
2155 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002156 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002157 && !*timedout)
2158 {
2159 keylen = check_termcode(max_mlen + 1,
2160 NULL, 0, NULL);
2161
Bram Moolenaareda35f72019-08-03 14:59:44 +02002162 // If no termcode matched but 'pastetoggle' matched partially it's
2163 // like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002164 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2165 keylen = KEYLEN_PART_KEY;
2166
Bram Moolenaareda35f72019-08-03 14:59:44 +02002167 // When getting a partial match, but the last characters were not
2168 // typed, don't wait for a typed character to complete the
2169 // termcode. This helps a lot when a ":normal" command ends in an
2170 // ESC.
2171 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002172 keylen = 0;
2173 }
2174 else
2175 keylen = 0;
2176 if (keylen == 0) // no matching terminal code
2177 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002178#ifdef AMIGA
2179 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002180 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002181 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002182 {
2183 char_u *s;
2184
2185 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002186 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2187 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002188 ++s)
2189 ;
2190 if (*s == 'r' || *s == '|') // found one
2191 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002192 del_typebuf(
2193 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002194 // get size and redraw screen
2195 shell_resized();
2196 *keylenp = keylen;
2197 return map_result_retry;
2198 }
2199 if (*s == NUL) // need more characters
2200 keylen = KEYLEN_PART_KEY;
2201 }
2202 if (keylen >= 0)
2203#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002204 // When there was a matching mapping and no termcode could be
2205 // replaced after another one, use that mapping (loop around).
2206 // If there was no mapping at all use the character from the
2207 // typeahead buffer right here.
2208 if (mp == NULL)
2209 {
2210 *keylenp = keylen;
2211 return map_result_get; // got character, break for loop
2212 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002213 }
2214
2215 if (keylen > 0) // full matching terminal code
2216 {
2217#if defined(FEAT_GUI) && defined(FEAT_MENU)
2218 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002219 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2220 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002221 {
2222 int idx;
2223
Bram Moolenaareda35f72019-08-03 14:59:44 +02002224 // Using a menu may cause a break in undo! It's like using
2225 // gotchars(), but without recording or writing to a script
2226 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002227 may_sync_undo();
2228 del_typebuf(3, 0);
2229 idx = get_menu_index(current_menu, local_State);
2230 if (idx != MENU_INDEX_INVALID)
2231 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002232 // In Select mode and a Visual mode menu is used: Switch
2233 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002234 // back to Select mode.
2235 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002236 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002237 {
2238 VIsual_select = FALSE;
2239 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002240 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002241 }
2242 ins_typebuf(current_menu->strings[idx],
2243 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002244 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002245 }
2246 }
2247#endif // FEAT_GUI && FEAT_MENU
2248 *keylenp = keylen;
2249 return map_result_retry; // try mapping again
2250 }
2251
Bram Moolenaareda35f72019-08-03 14:59:44 +02002252 // Partial match: get some more characters. When a matching mapping
2253 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002254 if (mp == NULL || keylen < 0)
2255 keylen = KEYLEN_PART_KEY;
2256 else
2257 keylen = mp_match_len;
2258 }
2259
2260 /*
2261 * complete match
2262 */
2263 if (keylen >= 0 && keylen <= typebuf.tb_len)
2264 {
2265 char_u *map_str;
2266
2267#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002268 int save_m_expr;
2269 int save_m_noremap;
2270 int save_m_silent;
2271 char_u *save_m_keys;
2272 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002273#else
2274# define save_m_noremap mp->m_noremap
2275# define save_m_silent mp->m_silent
2276#endif
2277
2278 // write chars to script file(s)
2279 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002280 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2281 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002282
2283 cmd_silent = (typebuf.tb_silent > 0);
2284 del_typebuf(keylen, 0); // remove the mapped keys
2285
2286 /*
2287 * Put the replacement string in front of mapstr.
2288 * The depth check catches ":map x y" and ":map y x".
2289 */
2290 if (++*mapdepth >= p_mmd)
2291 {
2292 emsg(_("E223: recursive mapping"));
2293 if (State & CMDLINE)
2294 redrawcmdline();
2295 else
2296 setcursor();
2297 flush_buffers(FLUSH_MINIMAL);
2298 *mapdepth = 0; /* for next one */
2299 *keylenp = keylen;
2300 return map_result_fail;
2301 }
2302
2303 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002304 * In Select mode and a Visual mode mapping is used: Switch to Visual
2305 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002306 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002307 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002308 {
2309 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002310 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002311 }
2312
2313#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002314 // Copy the values from *mp that are used, because evaluating the
2315 // expression may invoke a function that redefines the mapping, thereby
2316 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002317 save_m_expr = mp->m_expr;
2318 save_m_noremap = mp->m_noremap;
2319 save_m_silent = mp->m_silent;
2320 save_m_keys = NULL; // only saved when needed
2321 save_m_str = NULL; // only saved when needed
2322
2323 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002324 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2325 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002326 */
2327 if (mp->m_expr)
2328 {
2329 int save_vgetc_busy = vgetc_busy;
2330 int save_may_garbage_collect = may_garbage_collect;
2331
2332 vgetc_busy = 0;
2333 may_garbage_collect = FALSE;
2334
2335 save_m_keys = vim_strsave(mp->m_keys);
2336 save_m_str = vim_strsave(mp->m_str);
2337 map_str = eval_map_expr(save_m_str, NUL);
2338
2339 vgetc_busy = save_vgetc_busy;
2340 may_garbage_collect = save_may_garbage_collect;
2341 }
2342 else
2343#endif
2344 map_str = mp->m_str;
2345
2346 /*
2347 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002348 * If 'from' field is the same as the start of the 'to' field, don't
2349 * remap the first character (but do allow abbreviations).
2350 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002351 */
2352 if (map_str == NULL)
2353 i = FAIL;
2354 else
2355 {
2356 int noremap;
2357
2358 if (save_m_noremap != REMAP_YES)
2359 noremap = save_m_noremap;
2360 else if (
2361#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002362 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2363 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002364#else
2365 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2366#endif
2367 != 0)
2368 noremap = REMAP_YES;
2369 else
2370 noremap = REMAP_SKIP;
2371 i = ins_typebuf(map_str, noremap,
2372 0, TRUE, cmd_silent || save_m_silent);
2373#ifdef FEAT_EVAL
2374 if (save_m_expr)
2375 vim_free(map_str);
2376#endif
2377 }
2378#ifdef FEAT_EVAL
2379 vim_free(save_m_keys);
2380 vim_free(save_m_str);
2381#endif
2382 *keylenp = keylen;
2383 if (i == FAIL)
2384 return map_result_fail;
2385 return map_result_retry;
2386 }
2387
2388 *keylenp = keylen;
2389 return map_result_nomatch;
2390}
2391
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002392/*
2393 * unget one character (can only be done once!)
2394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002396vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 old_char = c;
2399 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002400#ifdef FEAT_MOUSE
2401 old_mouse_row = mouse_row;
2402 old_mouse_col = mouse_col;
2403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404}
2405
2406/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002407 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 * 1. from the stuffbuffer
2409 * This is used for abbreviated commands like "D" -> "d$".
2410 * Also used to redo a command for ".".
2411 * 2. from the typeahead buffer
2412 * Stores text obtained previously but not used yet.
2413 * Also stores the result of mappings.
2414 * Also used for the ":normal" command.
2415 * 3. from the user
2416 * This may do a blocking wait if "advance" is TRUE.
2417 *
2418 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002419 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 * KeyTyped is set to TRUE in the case the user typed the key.
2421 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2422 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002423 * Just look whether there is a character available.
2424 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 *
2426 * When "no_mapping" is zero, checks for mappings in the current mode.
2427 * Only returns one byte (of a multi-byte character).
2428 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2429 */
2430 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002431vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 int c, c1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int timedout = FALSE; /* waited for more than 1 second
2435 for mapping to complete */
2436 int mapdepth = 0; /* check for recursive mapping */
2437 int mode_deleted = FALSE; /* set when mode has been deleted */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002438#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 int new_wcol, new_wrow;
2440#endif
2441#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 int shape_changed = FALSE; /* adjusted cursor shape */
2443#endif
2444 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002446 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448 /*
2449 * This function doesn't work very well when called recursively. This may
2450 * happen though, because of:
2451 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2452 * there is a character available, which calls this function. In that
2453 * case we must return NUL, to indicate no character is available.
2454 * 2. A GUI callback function writes to the screen, causing a
2455 * wait_return().
2456 * Using ":normal" can also do this, but it saves the typeahead buffer,
2457 * thus it should be OK. But don't get a key from the user then.
2458 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002459 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 return NUL;
2461
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002462 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
2464 if (advance)
2465 KeyStuffed = FALSE;
2466
2467 init_typebuf();
2468 start_stuff();
2469 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002470 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 do
2472 {
2473/*
2474 * get a character: 1. from the stuffbuffer
2475 */
2476 if (typeahead_char != 0)
2477 {
2478 c = typeahead_char;
2479 if (advance)
2480 typeahead_char = 0;
2481 }
2482 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002483 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (c != NUL && !got_int)
2485 {
2486 if (advance)
2487 {
2488 /* KeyTyped = FALSE; When the command that stuffed something
2489 * was typed, behave like the stuffed command was typed.
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01002490 * needed for CTRL-W CTRL-] to open a fold, for example. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 KeyStuffed = TRUE;
2492 }
2493 if (typebuf.tb_no_abbr_cnt == 0)
2494 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
2495 }
2496 else
2497 {
2498 /*
2499 * Loop until we either find a matching mapped key, or we
2500 * are sure that it is not a mapped key.
2501 * If a mapped key sequence is found we go back to the start to
2502 * try re-mapping.
2503 */
2504 for (;;)
2505 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002506 long wait_time;
2507 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002508#ifdef FEAT_CMDL_INFO
2509 int showcmd_idx;
2510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /*
2512 * ui_breakcheck() is slow, don't use it too often when
2513 * inside a mapping. But call it each time for typed
2514 * characters.
2515 */
2516 if (typebuf.tb_maplen)
2517 line_breakcheck();
2518 else
2519 ui_breakcheck(); /* check for CTRL-C */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (got_int)
2521 {
2522 /* flush all input */
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002523 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002527 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 * Otherwise we behave like having gotten a CTRL-C.
2529 * As a result typing CTRL-C in insert mode will
2530 * really insert a CTRL-C.
2531 */
2532 if ((c || typebuf.tb_maplen)
2533 && (State & (INSERT + CMDLINE)))
2534 c = ESC;
2535 else
2536 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002537 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002539 if (advance)
2540 {
2541 /* Also record this character, it might be needed to
2542 * get out of Insert mode. */
2543 *typebuf.tb_buf = c;
2544 gotchars(typebuf.tb_buf, 1);
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 cmd_silent = FALSE;
2547
2548 break;
2549 }
2550 else if (typebuf.tb_len > 0)
2551 {
2552 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002553 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002555 map_result_T result = handle_mapping(
2556 &keylen, &timedout, &mapdepth);
2557
2558 if (result == map_result_retry)
2559 // try mapping again
2560 continue;
2561 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002562 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002563 // failed, use the outer loop
2564 c = -1;
2565 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002567 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569/*
2570 * get a character: 2. from the typeahead buffer
2571 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002572 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2573 if (advance) /* remove chars from tb_buf */
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002574 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 cmd_silent = (typebuf.tb_silent > 0);
2576 if (typebuf.tb_maplen > 0)
2577 KeyTyped = FALSE;
2578 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002579 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002580 KeyTyped = TRUE;
2581 /* write char to script file(s) */
2582 gotchars(typebuf.tb_buf
2583 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002584 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585 KeyNoremap = typebuf.tb_noremap[
2586 typebuf.tb_off];
2587 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002588 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002592 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595/*
2596 * get a character: 3. from the user - handle <Esc> in Insert mode
2597 */
2598 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02002599 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 * are no more characters at once, we pretend to go out of
2601 * insert mode. This prevents the one second delay after
2602 * typing an <ESC>. If we get something after all, we may
2603 * have to redisplay the mode. That the cursor is in the wrong
2604 * place does not matter.
2605 */
2606 c = 0;
2607#ifdef FEAT_CMDL_INFO
2608 new_wcol = curwin->w_wcol;
2609 new_wrow = curwin->w_wrow;
2610#endif
2611 if ( advance
2612 && typebuf.tb_len == 1
2613 && typebuf.tb_buf[typebuf.tb_off] == ESC
2614 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 && typebuf.tb_maplen == 0
2617 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002618 && (p_timeout
2619 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002621 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 colnr_T col = 0, vcol;
2624 char_u *ptr;
2625
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002626 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
2628 unshowmode(TRUE);
2629 mode_deleted = TRUE;
2630 }
2631#ifdef FEAT_GUI
Bram Moolenaarf085f422017-06-07 20:39:47 +02002632 /* may show a different cursor shape */
2633 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 int save_State;
2636
2637 save_State = State;
2638 State = NORMAL;
2639 gui_update_cursor(TRUE, FALSE);
2640 State = save_State;
2641 shape_changed = TRUE;
2642 }
2643#endif
2644 validate_cursor();
2645 old_wcol = curwin->w_wcol;
2646 old_wrow = curwin->w_wrow;
2647
2648 /* move cursor left, if possible */
2649 if (curwin->w_cursor.col != 0)
2650 {
2651 if (curwin->w_wcol > 0)
2652 {
2653 if (did_ai)
2654 {
2655 /*
2656 * We are expecting to truncate the trailing
2657 * white-space, so find the last non-white
2658 * character -- webb
2659 */
2660 col = vcol = curwin->w_wcol = 0;
2661 ptr = ml_get_curline();
2662 while (col < curwin->w_cursor.col)
2663 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002664 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002666 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002669 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 ++col;
2672 }
2673 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02002674 + curwin->w_wcol / curwin->w_width;
2675 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 curwin->w_wcol += curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 col = 0; /* no correction needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 }
2679 else
2680 {
2681 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684 }
2685 else if (curwin->w_p_wrap && curwin->w_wrow)
2686 {
2687 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02002688 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2692 {
2693 /* Correct when the cursor is on the right halve
2694 * of a double-wide character. */
2695 ptr = ml_get_curline();
2696 col -= (*mb_head_off)(ptr, ptr + col);
2697 if ((*mb_ptr2cells)(ptr + col) > 1)
2698 --curwin->w_wcol;
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 setcursor();
2702 out_flush();
2703#ifdef FEAT_CMDL_INFO
2704 new_wcol = curwin->w_wcol;
2705 new_wrow = curwin->w_wrow;
2706#endif
2707 curwin->w_wcol = old_wcol;
2708 curwin->w_wrow = old_wrow;
2709 }
2710 if (c < 0)
2711 continue; /* end of input script reached */
Bram Moolenaar20c38922014-07-23 20:41:14 +02002712
2713 /* Allow mapping for just typed characters. When we get here c
2714 * is the number of extra bytes and typebuf.tb_len is 1. */
2715 for (n = 1; n <= c; ++n)
2716 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 typebuf.tb_len += c;
2718
2719 /* buffer full, don't map */
2720 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2721 {
2722 timedout = TRUE;
2723 continue;
2724 }
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if (ex_normal_busy > 0)
2727 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01002728#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /* No typeahead left and inside ":normal". Must return
2733 * something to avoid getting stuck. When an incomplete
2734 * mapping is present, behave like it timed out. */
2735 if (typebuf.tb_len > 0)
2736 {
2737 timedout = TRUE;
2738 continue;
2739 }
2740 /* When 'insertmode' is set, ESC just beeps in Insert
2741 * mode. Use CTRL-L to make edit() return.
2742 * For the command line only CTRL-C always breaks it.
2743 * For the cmdline window: Alternate between ESC and
2744 * CTRL-C: ESC for most situations and CTRL-C to close the
2745 * cmdline window. */
2746 if (p_im && (State & INSERT))
2747 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002748#ifdef FEAT_TERMINAL
2749 else if (terminal_is_active())
2750 c = K_CANCEL;
2751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01002753#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01002755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 )
2757 c = Ctrl_C;
2758 else
2759 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002760#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 break;
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766/*
2767 * get a character: 3. from the user - update display
2768 */
2769 /* In insert mode a screen update is skipped when characters
2770 * are still available. But when those available characters
2771 * are part of a mapping, and we are going to do a blocking
2772 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01002773 * changed text so far. Also for when 'lazyredraw' is set and
2774 * redrawing was postponed because there was something in the
2775 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01002776 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
2777 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 update_screen(0);
2780 setcursor(); /* put cursor back where it belongs */
2781 }
2782
2783 /*
2784 * If we have a partial match (and are going to wait for more
2785 * input from the user), show the partially matched characters
2786 * to the user with showcmd.
2787 */
2788#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02002789 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#endif
2791 c1 = 0;
2792 if (typebuf.tb_len > 0 && advance && !exmode_active)
2793 {
2794 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2795 && State != HITRETURN)
2796 {
2797 /* this looks nice when typing a dead character map */
2798 if (State & INSERT
2799 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2800 + typebuf.tb_len - 1) == 1)
2801 {
2802 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2803 + typebuf.tb_len - 1], FALSE);
2804 setcursor(); /* put cursor back where it belongs */
2805 c1 = 1;
2806 }
2807#ifdef FEAT_CMDL_INFO
2808 /* need to use the col and row from above here */
2809 old_wcol = curwin->w_wcol;
2810 old_wrow = curwin->w_wrow;
2811 curwin->w_wcol = new_wcol;
2812 curwin->w_wrow = new_wrow;
2813 push_showcmd();
2814 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002815 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
2816 while (showcmd_idx < typebuf.tb_len)
2817 (void)add_to_showcmd(
2818 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 curwin->w_wcol = old_wcol;
2820 curwin->w_wrow = old_wrow;
2821#endif
2822 }
2823
2824 /* this looks nice when typing a dead character map */
2825 if ((State & CMDLINE)
2826#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2827 && cmdline_star == 0
2828#endif
2829 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2830 + typebuf.tb_len - 1) == 1)
2831 {
2832 putcmdline(typebuf.tb_buf[typebuf.tb_off
2833 + typebuf.tb_len - 1], FALSE);
2834 c1 = 1;
2835 }
2836 }
2837
2838/*
2839 * get a character: 3. from the user - get it
2840 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02002841 if (typebuf.tb_len == 0)
2842 // timedout may have been set while waiting for a mapping
2843 // that has a <Nop> RHS.
2844 timedout = FALSE;
2845
Bram Moolenaar652de232019-04-04 20:13:09 +02002846 if (advance)
2847 {
2848 if (typebuf.tb_len == 0
2849 || !(p_timeout
2850 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
2851 // blocking wait
2852 wait_time = -1L;
2853 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
2854 wait_time = p_ttm;
2855 else
2856 wait_time = p_tm;
2857 }
2858 else
2859 wait_time = 0;
2860
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002861 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2863 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02002864 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02002867 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 pop_showcmd();
2869#endif
2870 if (c1 == 1)
2871 {
2872 if (State & INSERT)
2873 edit_unputchar();
2874 if (State & CMDLINE)
2875 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02002876 else
2877 setcursor(); /* put cursor back where it belongs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
2879
2880 if (c < 0)
2881 continue; /* end of input script reached */
2882 if (c == NUL) /* no character available */
2883 {
2884 if (!advance)
2885 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002886 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
2888 timedout = TRUE;
2889 continue;
2890 }
2891 }
2892 else
2893 { /* allow mapping for just typed characters */
2894 while (typebuf.tb_buf[typebuf.tb_off
2895 + typebuf.tb_len] != NUL)
2896 typebuf.tb_noremap[typebuf.tb_off
2897 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002898#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /* Get IM status right after getting keys, not after the
2900 * timeout for a mapping (focus may be lost by then). */
2901 vgetc_im_active = im_get_status();
2902#endif
2903 }
2904 } /* for (;;) */
2905 } /* if (!character from stuffbuf) */
2906
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002907 /* if advance is FALSE don't loop on NULs */
2908 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 /*
2911 * The "INSERT" message is taken care of here:
2912 * if we return an ESC to exit insert mode, the message is deleted
2913 * if we don't return an ESC but deleted the message before, redisplay it
2914 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002915 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002917 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 if (typebuf.tb_len && !KeyTyped)
2920 redraw_cmdline = TRUE; /* delete mode later */
2921 else
2922 unshowmode(FALSE);
2923 }
2924 else if (c != ESC && mode_deleted)
2925 {
2926 if (typebuf.tb_len && !KeyTyped)
2927 redraw_cmdline = TRUE; /* show mode later */
2928 else
2929 showmode();
2930 }
2931 }
2932#ifdef FEAT_GUI
2933 /* may unshow different cursor shape */
Bram Moolenaarf085f422017-06-07 20:39:47 +02002934 if (gui.in_use && shape_changed)
2935 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01002937 if (timedout && c == ESC)
2938 {
2939 char_u nop_buf[3];
2940
2941 // When recording there will be no timeout. Add a <Nop> after the ESC
2942 // to avoid that it forms a key code with following characters.
2943 nop_buf[0] = K_SPECIAL;
2944 nop_buf[1] = KS_EXTRA;
2945 nop_buf[2] = KE_NOP;
2946 gotchars(nop_buf, 3);
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002949 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 return c;
2952}
2953
2954/*
2955 * inchar() - get one character from
2956 * 1. a scriptfile
2957 * 2. the keyboard
2958 *
2959 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2960 * NUL terminated (buffer length must be 'maxlen' + 1).
2961 * Minimum for "maxlen" is 3!!!!
2962 *
2963 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2964 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2965 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
2966 * otherwise.
2967 *
2968 * If we got an interrupt all input is read until none is available.
2969 *
2970 * If wait_time == 0 there is no waiting for the char.
2971 * If wait_time == n we wait for n msec for a character to arrive.
2972 * If wait_time == -1 we wait forever for a character to arrive.
2973 *
2974 * Return the number of obtained characters.
2975 * Return -1 when end of input script reached.
2976 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02002977 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002978inchar(
2979 char_u *buf,
2980 int maxlen,
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002981 long wait_time) /* milli seconds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 int len = 0; /* init for GCC */
2984 int retesc = FALSE; /* return ESC with gotint */
2985 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002986 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
2988 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
2989 {
2990 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002991 out_flush_cursor(FALSE, FALSE);
2992#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
2993 if (gui.in_use && postponed_mouseshape)
2994 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
2996 }
2997
2998 /*
2999 * Don't reset these when at the hit-return prompt, otherwise a endless
3000 * recursive loop may result (write error in swapfile, hit-return, timeout
3001 * on char wait, flush swapfile, write error....).
3002 */
3003 if (State != HITRETURN)
3004 {
3005 did_outofmem_msg = FALSE; /* display out of memory message (again) */
3006 did_swapwrite_msg = FALSE; /* display swap file write error again */
3007 }
3008 undo_off = FALSE; /* restart undo now */
3009
3010 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003011 * Get a character from a script file if there is one.
3012 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 */
3014 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003015 while (scriptin[curscript] != NULL && script_char < 0
3016#ifdef FEAT_EVAL
3017 && !ignore_script
3018#endif
3019 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003021
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003022#ifdef MESSAGE_QUEUE
3023 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003024#endif
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3027 {
3028 /* Reached EOF.
3029 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3030 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
3031 closescript();
3032 /*
3033 * When reading script file is interrupted, return an ESC to get
3034 * back to normal mode.
3035 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3036 */
3037 if (got_int)
3038 retesc = TRUE;
3039 else
3040 return -1;
3041 }
3042 else
3043 {
3044 buf[0] = script_char;
3045 len = 1;
3046 }
3047 }
3048
3049 if (script_char < 0) /* did not get a character from script */
3050 {
3051 /*
3052 * If we got an interrupt, skip all previously typed characters and
3053 * return TRUE if quit reading script file.
3054 * Stop reading typeahead when a single CTRL-C was read,
3055 * fill_input_buf() returns this when not able to read from stdin.
3056 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3057 * and buf may be pointing inside typebuf.tb_buf[].
3058 */
3059 if (got_int)
3060 {
3061#define DUM_LEN MAXMAPLEN * 3 + 3
3062 char_u dum[DUM_LEN + 1];
3063
3064 for (;;)
3065 {
3066 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3067 if (len == 0 || (len == 1 && dum[0] == 3))
3068 break;
3069 }
3070 return retesc;
3071 }
3072
3073 /*
3074 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003075 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003077 if (wait_time == -1L || wait_time > 10L)
3078 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 /*
3081 * Fill up to a third of the buffer, because each character may be
3082 * tripled below.
3083 */
3084 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3085 }
3086
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003087 /* If the typebuf was changed further down, it is like nothing was added by
3088 * this call. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (typebuf_changed(tb_change_cnt))
3090 return 0;
3091
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003092 /* Note the change in the typeahead buffer, this matters for when
3093 * vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3094 * function. */
3095 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3096 typebuf.tb_change_cnt = 1;
3097
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003098 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
3101/*
3102 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003103 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * Returns the new length.
3105 */
3106 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003107fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 int i;
3110 char_u *p = buf;
3111
3112 /*
3113 * Two characters are special: NUL and K_SPECIAL.
3114 * When compiled With the GUI CSI is also special.
3115 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3116 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3117 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 */
3119 for (i = len; --i >= 0; ++p)
3120 {
3121#ifdef FEAT_GUI
3122 /* When the GUI is used any character can come after a CSI, don't
3123 * escape it. */
3124 if (gui.in_use && p[0] == CSI && i >= 2)
3125 {
3126 p += 2;
3127 i -= 2;
3128 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003129# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 /* When the GUI is not used CSI needs to be escaped. */
3131 else if (!gui.in_use && p[0] == CSI)
3132 {
3133 mch_memmove(p + 3, p + 1, (size_t)i);
3134 *p++ = K_SPECIAL;
3135 *p++ = KS_EXTRA;
3136 *p = (int)KE_CSI;
3137 len += 2;
3138 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 else
3141#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003142 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003143 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003144 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003145#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003146 // Win32 console passes modifiers
3147 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003148# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003149 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003150# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003151 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
3153 ))
3154 {
3155 mch_memmove(p + 3, p + 1, (size_t)i);
3156 p[2] = K_THIRD(p[0]);
3157 p[1] = K_SECOND(p[0]);
3158 p[0] = K_SPECIAL;
3159 p += 2;
3160 len += 2;
3161 }
3162 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003163 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 return len;
3165}
3166
3167#if defined(USE_INPUT_BUF) || defined(PROTO)
3168/*
3169 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3170 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003171 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003172 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
3174 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003175input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003178# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3179 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180# endif
3181 );
3182}
3183#endif