blob: 07383da2cab01577c2e1decd2774ed7eaaa2113c [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 Moolenaar459fd782019-10-13 16:43:39 +020055static 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);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020094static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020096static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020098static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100/*
101 * Free and clear a buffer.
102 */
103 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100104free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100106 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar285e3352018-04-18 23:01:13 +0200108 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 {
110 np = p->b_next;
111 vim_free(p);
112 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200113 buf->bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114}
115
116/*
117 * Return the contents of a buffer as a single string.
118 * K_SPECIAL and CSI in the returned string are escaped.
119 */
120 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100121get_buffcont(
122 buffheader_T *buffer,
123 int dozero) /* count == zero is not an error */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 long_u count = 0;
126 char_u *p = NULL;
127 char_u *p2;
128 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200129 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131 /* compute the total length of the string */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200132 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 count += (long_u)STRLEN(bp->b_str);
134
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200135 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 {
137 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200138 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 for (str = bp->b_str; *str; )
140 *p2++ = *str++;
141 *p2 = NUL;
142 }
143 return (p);
144}
145
146/*
147 * Return the contents of the record buffer as a single string
148 * and clear the record buffer.
149 * K_SPECIAL and CSI in the returned string are escaped.
150 */
151 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100152get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153{
154 char_u *p;
155 size_t len;
156
157 p = get_buffcont(&recordbuff, TRUE);
158 free_buff(&recordbuff);
159
160 /*
161 * Remove the characters that were added the last time, these must be the
162 * (possibly mapped) characters that stopped the recording.
163 */
164 len = STRLEN(p);
165 if ((int)len >= last_recorded_len)
166 {
167 len -= last_recorded_len;
168 p[len] = NUL;
169 }
170
171 /*
172 * When stopping recording from Insert mode with CTRL-O q, also remove the
173 * CTRL-O.
174 */
175 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
176 p[len - 1] = NUL;
177
178 return (p);
179}
180
181/*
182 * Return the contents of the redo buffer as a single string.
183 * K_SPECIAL and CSI in the returned string are escaped.
184 */
185 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100186get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000188 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189}
190
191/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000192 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 * K_SPECIAL and CSI should have been escaped already.
194 */
195 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100196add_buff(
197 buffheader_T *buf,
198 char_u *s,
199 long slen) /* length of "s" or -1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100201 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200202 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204 if (slen < 0)
205 slen = (long)STRLEN(s);
206 if (slen == 0) /* don't add empty strings */
207 return;
208
Bram Moolenaar285e3352018-04-18 23:01:13 +0200209 if (buf->bh_first.b_next == NULL) /* first add to list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
211 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200212 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 }
214 else if (buf->bh_curr == NULL) /* buffer has already been read */
215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100216 iemsg(_("E222: Add to read buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 return;
218 }
219 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200220 mch_memmove(buf->bh_first.b_next->b_str,
221 buf->bh_first.b_next->b_str + buf->bh_index,
222 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 buf->bh_index = 0;
224
225 if (buf->bh_space >= (int)slen)
226 {
227 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000228 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 buf->bh_space -= slen;
230 }
231 else
232 {
233 if (slen < MINIMAL_SIZE)
234 len = MINIMAL_SIZE;
235 else
236 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200237 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (p == NULL)
239 return; /* no space, just forget it */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000240 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000241 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
Bram Moolenaar285e3352018-04-18 23:01:13 +0200243 p->b_next = buf->bh_curr->b_next;
244 buf->bh_curr->b_next = p;
245 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 }
247 return;
248}
249
250/*
251 * Add number "n" to buffer "buf".
252 */
253 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100254add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
256 char_u number[32];
257
258 sprintf((char *)number, "%ld", n);
259 add_buff(buf, number, -1L);
260}
261
262/*
263 * Add character 'c' to buffer "buf".
264 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
265 */
266 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100267add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 char_u bytes[MB_MAXBYTES + 1];
270 int len;
271 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 char_u temp[4];
273
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (IS_SPECIAL(c))
275 len = 1;
276 else
277 len = (*mb_char2bytes)(c, bytes);
278 for (i = 0; i < len; ++i)
279 {
280 if (!IS_SPECIAL(c))
281 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
284 {
285 /* translate special key code into three byte sequence */
286 temp[0] = K_SPECIAL;
287 temp[1] = K_SECOND(c);
288 temp[2] = K_THIRD(c);
289 temp[3] = NUL;
290 }
291#ifdef FEAT_GUI
292 else if (c == CSI)
293 {
294 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
295 temp[0] = CSI;
296 temp[1] = KS_EXTRA;
297 temp[2] = (int)KE_CSI;
298 temp[3] = NUL;
299 }
300#endif
301 else
302 {
303 temp[0] = c;
304 temp[1] = NUL;
305 }
306 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308}
309
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100310/* First read ahead buffer. Used for translated commands. */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200311static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100312
313/* Second read ahead buffer. Used for redo. */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200314static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100315
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100317 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
318 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 * If advance == TRUE go to the next char.
320 * No translation is done K_SPECIAL and CSI are escaped.
321 */
322 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100323read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100325 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100327 c = read_readbuf(&readbuf1, advance);
328 if (c == NUL)
329 c = read_readbuf(&readbuf2, advance);
330 return c;
331}
332
333 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100334read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100335{
336 char_u c;
337 buffblock_T *curr;
338
Bram Moolenaar285e3352018-04-18 23:01:13 +0200339 if (buf->bh_first.b_next == NULL) /* buffer is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 return NUL;
341
Bram Moolenaar285e3352018-04-18 23:01:13 +0200342 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100343 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
345 if (advance)
346 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100347 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200349 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100351 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 }
353 }
354 return c;
355}
356
357/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100358 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 */
360 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100361start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200363 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200365 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100366 readbuf1.bh_space = 0;
367 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200370 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100371 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 }
373}
374
375/*
376 * Return TRUE if the stuff buffer is empty.
377 */
378 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100379stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200381 return (readbuf1.bh_first.b_next == NULL
382 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100383}
384
Bram Moolenaar113e1072019-01-20 15:30:40 +0100385#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100386/*
387 * Return TRUE if readbuf1 is empty. There may still be redo characters in
388 * redbuf2.
389 */
390 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100391readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100392{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200393 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
397/*
398 * Set a typeahead character that won't be flushed.
399 */
400 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100401typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
403 typeahead_char = c;
404}
405
406/*
407 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100408 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * flush all typeahead characters (used when interrupted by a CTRL-C).
410 */
411 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200412flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
414 init_typebuf();
415
416 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100417 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 ;
419
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200420 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200422 // remove mapped characters at the start only
423 typebuf.tb_off += typebuf.tb_maplen;
424 typebuf.tb_len -= typebuf.tb_maplen;
425 }
426 else
427 {
428 // remove typeahead
429 if (flush_typeahead == FLUSH_INPUT)
430 // We have to get all characters, because we may delete the first
431 // part of an escape sequence. In an xterm we get one char at a
432 // time and we have to get them all.
433 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
434 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 typebuf.tb_off = MAXMAPLEN;
436 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200437#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
438 /* Reset the flag that text received from a client or from feedkeys()
439 * was inserted in the typeahead buffer. */
440 typebuf_was_filled = FALSE;
441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 typebuf.tb_maplen = 0;
444 typebuf.tb_silent = 0;
445 cmd_silent = FALSE;
446 typebuf.tb_no_abbr_cnt = 0;
447}
448
449/*
450 * The previous contents of the redo buffer is kept in old_redobuffer.
451 * This is used for the CTRL-O <.> command in insert mode.
452 */
453 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100454ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
456 if (!block_redo)
457 {
458 free_buff(&old_redobuff);
459 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200460 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 }
462}
463
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100464/*
465 * Discard the contents of the redo buffer and restore the previous redo
466 * buffer.
467 */
468 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100469CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100470{
471 if (!block_redo)
472 {
473 free_buff(&redobuff);
474 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200475 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100476 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100477 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100478 ;
479 }
480}
481
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482/*
483 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
484 * Used before executing autocommands and user functions.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200487saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
489 char_u *s;
490
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200491 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200492 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200493 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200494 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200496 /* Make a copy, so that ":normal ." in a function works. */
497 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
498 if (s != NULL)
499 {
500 add_buff(&redobuff, s, -1L);
501 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503}
504
505/*
506 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
507 * Used after executing autocommands and user functions.
508 */
509 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200510restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200512 free_buff(&redobuff);
513 redobuff = save_redo->sr_redobuff;
514 free_buff(&old_redobuff);
515 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518/*
519 * Append "s" to the redo buffer.
520 * K_SPECIAL and CSI should already have been escaped.
521 */
522 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100523AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 if (!block_redo)
526 add_buff(&redobuff, s, -1L);
527}
528
529/*
530 * Append to Redo buffer literally, escaping special characters with CTRL-V.
531 * K_SPECIAL and CSI are escaped as well.
532 */
533 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100534AppendToRedobuffLit(
535 char_u *str,
536 int len) /* length of "str" or -1 for up to the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000538 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 int c;
540 char_u *start;
541
542 if (block_redo)
543 return;
544
Bram Moolenaarebefac62005-12-28 22:39:57 +0000545 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 /* Put a string of normal characters in the redo buffer (that's
548 * faster). */
549 start = s;
550 while (*s >= ' '
551#ifndef EBCDIC
552 && *s < DEL /* EBCDIC: all chars above space are normal */
553#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000554 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 ++s;
556
557 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
558 * typed next. */
559 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
560 --s;
561 if (s > start)
562 add_buff(&redobuff, start, (long)(s - start));
563
Bram Moolenaarebefac62005-12-28 22:39:57 +0000564 if (*s == NUL || (len >= 0 && s - str >= len))
565 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaarebefac62005-12-28 22:39:57 +0000567 /* Handle a special or multibyte character. */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000568 if (has_mbyte)
569 /* Handle composing chars separately. */
570 c = mb_cptr2char_adv(&s);
571 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000572 c = *s++;
573 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
574 add_char_buff(&redobuff, Ctrl_V);
575
576 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
577 if (*s == NUL && c == '0')
578#ifdef EBCDIC
579 add_buff(&redobuff, (char_u *)"xf0", 3L);
580#else
581 add_buff(&redobuff, (char_u *)"048", 3L);
582#endif
583 else
584 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 }
586}
587
588/*
589 * Append a character to the redo buffer.
590 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
591 */
592 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100593AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 if (!block_redo)
596 add_char_buff(&redobuff, c);
597}
598
599/*
600 * Append a number to the redo buffer.
601 */
602 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100603AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 if (!block_redo)
606 add_num_buff(&redobuff, n);
607}
608
609/*
610 * Append string "s" to the stuff buffer.
611 * CSI and K_SPECIAL must already have been escaped.
612 */
613 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100614stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100616 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617}
618
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200619/*
620 * Append string "s" to the redo stuff buffer.
621 * CSI and K_SPECIAL must already have been escaped.
622 */
623 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100624stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200625{
626 add_buff(&readbuf2, s, -1L);
627}
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100630stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100632 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
635#if defined(FEAT_EVAL) || defined(PROTO)
636/*
637 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
638 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200639 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
641 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100642stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200644 int c;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 while (*s != NUL)
647 {
648 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
649 {
650 /* Insert special key literally. */
651 stuffReadbuffLen(s, 3L);
652 s += 3;
653 }
654 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200656 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200657 if (c == CAR || c == NL || c == ESC)
658 c = ' ';
659 stuffcharReadbuff(c);
660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662}
663#endif
664
665/*
666 * Append a character to the stuff buffer.
667 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
668 */
669 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100670stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100672 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674
675/*
676 * Append a number to the stuff buffer.
677 */
678 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100679stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100681 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682}
683
684/*
685 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
686 * multibyte characters.
687 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100688 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
689 * otherwise.
690 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 */
692 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100693read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100695 static buffblock_T *bp;
696 static char_u *p;
697 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100698 int n;
699 char_u buf[MB_MAXBYTES + 1];
700 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702 if (init)
703 {
704 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200705 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200707 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (bp == NULL)
709 return FAIL;
710 p = bp->b_str;
711 return OK;
712 }
713 if ((c = *p) != NUL)
714 {
715 /* Reverse the conversion done by add_char_buff() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 /* For a multi-byte character get all the bytes and return the
717 * converted character. */
718 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
719 n = MB_BYTE2LEN_CHECK(c);
720 else
721 n = 1;
722 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
724 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
725 {
726 c = TO_SPECIAL(p[1], p[2]);
727 p += 2;
728 }
729#ifdef FEAT_GUI
730 if (c == CSI) /* escaped CSI */
731 p += 2;
732#endif
733 if (*++p == NUL && bp->b_next != NULL)
734 {
735 bp = bp->b_next;
736 p = bp->b_str;
737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 buf[i] = c;
739 if (i == n - 1) /* last byte of a character */
740 {
741 if (n != 1)
742 c = (*mb_ptr2char)(buf);
743 break;
744 }
745 c = *p;
746 if (c == NUL) /* cannot happen? */
747 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 }
750
751 return c;
752}
753
754/*
755 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
756 * If old_redo is TRUE, use old_redobuff instead of redobuff.
757 * The escaped K_SPECIAL and CSI are copied without translation.
758 */
759 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100760copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
762 int c;
763
764 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100765 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766}
767
768/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100769 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 * Insert the redo count into the command.
771 * If "old_redo" is TRUE, the last but one command is repeated
772 * instead of the last command (inserting text). This is used for
773 * CTRL-O <.> in insert mode
774 *
775 * return FAIL for failure, OK otherwise
776 */
777 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100778start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
780 int c;
781
782 /* init the pointers; return if nothing to redo */
783 if (read_redo(TRUE, old_redo) == FAIL)
784 return FAIL;
785
786 c = read_redo(FALSE, old_redo);
787
788 /* copy the buffer name, if present */
789 if (c == '"')
790 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100791 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 c = read_redo(FALSE, old_redo);
793
794 /* if a numbered buffer is used, increment the number */
795 if (c >= '1' && c < '9')
796 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100797 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200798
799 /* the expression register should be re-evaluated */
800 if (c == '=')
801 {
802 add_char_buff(&readbuf2, CAR);
803 cmd_silent = TRUE;
804 }
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 c = read_redo(FALSE, old_redo);
807 }
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 if (c == 'v') /* redo Visual */
810 {
811 VIsual = curwin->w_cursor;
812 VIsual_active = TRUE;
813 VIsual_select = FALSE;
814 VIsual_reselect = TRUE;
815 redo_VIsual_busy = TRUE;
816 c = read_redo(FALSE, old_redo);
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 /* try to enter the count (in place of a previous count) */
820 if (count)
821 {
822 while (VIM_ISDIGIT(c)) /* skip "old" count */
823 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100824 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
826
827 /* copy from the redo buffer into the stuff buffer */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100828 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 copy_redo(old_redo);
830 return OK;
831}
832
833/*
834 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100835 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * return FAIL for failure, OK otherwise
837 */
838 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100839start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 int c;
842
843 if (read_redo(TRUE, FALSE) == FAIL)
844 return FAIL;
845 start_stuff();
846
847 /* skip the count and the command character */
848 while ((c = read_redo(FALSE, FALSE)) != NUL)
849 {
850 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
851 {
852 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100853 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 break;
855 }
856 }
857
858 /* copy the typed text from the redo buffer into the stuff buffer */
859 copy_redo(FALSE);
860 block_redo = TRUE;
861 return OK;
862}
863
864 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100865stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
867 block_redo = FALSE;
868}
869
870/*
871 * Initialize typebuf.tb_buf to point to typebuf_init.
872 * alloc() cannot be used here: In out-of-memory situations it would
873 * be impossible to type anything.
874 */
875 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100876init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
878 if (typebuf.tb_buf == NULL)
879 {
880 typebuf.tb_buf = typebuf_init;
881 typebuf.tb_noremap = noremapbuf_init;
882 typebuf.tb_buflen = TYPELEN_INIT;
883 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200884 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 typebuf.tb_change_cnt = 1;
886 }
887}
888
889/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200890 * Returns TRUE when keys cannot be remapped.
891 */
892 int
893noremap_keys(void)
894{
895 return KeyNoremap & (RM_NONE|RM_SCRIPT);
896}
897
898/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100899 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
900 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 *
902 * If noremap is REMAP_YES, new string can be mapped again.
903 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000904 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
905 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
907 * script-local mappings.
908 * If noremap is > 0, that many characters of the new string cannot be mapped.
909 *
910 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
911 * offset is non-zero!).
912 *
913 * If silent is TRUE, cmd_silent is set when the characters are obtained.
914 *
915 * return FAIL for failure, OK otherwise
916 */
917 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100918ins_typebuf(
919 char_u *str,
920 int noremap,
921 int offset,
922 int nottyped,
923 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
925 char_u *s1, *s2;
926 int newlen;
927 int addlen;
928 int i;
929 int newoff;
930 int val;
931 int nrm;
932
933 init_typebuf();
934 if (++typebuf.tb_change_cnt == 0)
935 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200936 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
938 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (offset == 0 && addlen <= typebuf.tb_off)
941 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100942 /*
943 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 typebuf.tb_off -= addlen;
946 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
947 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200948 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
949 >= addlen + 3 * (MAXMAPLEN + 4))
950 {
951 /*
952 * Buffer is empty and string fits in the existing buffer.
953 * Leave some space before and after, if possible.
954 */
955 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
956 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 else
959 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100960 /*
961 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200962 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100963 * characters. We add some extra room to avoid having to allocate too
964 * often.
965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 newoff = MAXMAPLEN + 4;
967 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
968 if (newlen < 0) /* string is getting too long */
969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100970 emsg(_(e_toocompl)); /* also calls flush_buffers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 setcursor();
972 return FAIL;
973 }
974 s1 = alloc(newlen);
975 if (s1 == NULL) /* out of memory */
976 return FAIL;
977 s2 = alloc(newlen);
978 if (s2 == NULL) /* out of memory */
979 {
980 vim_free(s1);
981 return FAIL;
982 }
983 typebuf.tb_buflen = newlen;
984
985 /* copy the old chars, before the insertion point */
986 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
987 (size_t)offset);
988 /* copy the new chars */
989 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
990 /* copy the old chars, after the insertion point, including the NUL at
991 * the end */
992 mch_memmove(s1 + newoff + offset + addlen,
993 typebuf.tb_buf + typebuf.tb_off + offset,
994 (size_t)(typebuf.tb_len - offset + 1));
995 if (typebuf.tb_buf != typebuf_init)
996 vim_free(typebuf.tb_buf);
997 typebuf.tb_buf = s1;
998
999 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1000 (size_t)offset);
1001 mch_memmove(s2 + newoff + offset + addlen,
1002 typebuf.tb_noremap + typebuf.tb_off + offset,
1003 (size_t)(typebuf.tb_len - offset));
1004 if (typebuf.tb_noremap != noremapbuf_init)
1005 vim_free(typebuf.tb_noremap);
1006 typebuf.tb_noremap = s2;
1007
1008 typebuf.tb_off = newoff;
1009 }
1010 typebuf.tb_len += addlen;
1011
1012 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
1013 if (noremap == REMAP_SCRIPT)
1014 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001015 else if (noremap == REMAP_SKIP)
1016 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 else
1018 val = RM_NONE;
1019
1020 /*
1021 * Adjust typebuf.tb_noremap[] for the new characters:
1022 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1023 * (sometimes) not remappable
1024 * If noremap == REMAP_YES: all the new characters are mappable
1025 * If noremap > 0: "noremap" characters are not remappable, the rest
1026 * mappable
1027 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001028 if (noremap == REMAP_SKIP)
1029 nrm = 1;
1030 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 nrm = addlen;
1032 else
1033 nrm = noremap;
1034 for (i = 0; i < addlen; ++i)
1035 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1036 (--nrm >= 0) ? val : RM_YES;
1037
1038 /* tb_maplen and tb_silent only remember the length of mapped and/or
1039 * silent mappings at the start of the buffer, assuming that a mapped
1040 * sequence doesn't result in typed characters. */
1041 if (nottyped || typebuf.tb_maplen > offset)
1042 typebuf.tb_maplen += addlen;
1043 if (silent || typebuf.tb_silent > offset)
1044 {
1045 typebuf.tb_silent += addlen;
1046 cmd_silent = TRUE;
1047 }
1048 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1049 typebuf.tb_no_abbr_cnt += addlen;
1050
1051 return OK;
1052}
1053
1054/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001055 * Put character "c" back into the typeahead buffer.
1056 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001057 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1058 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001059 */
1060 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001061ins_char_typebuf(int c)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001062{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001063 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001064 if (IS_SPECIAL(c))
1065 {
1066 buf[0] = K_SPECIAL;
1067 buf[1] = K_SECOND(c);
1068 buf[2] = K_THIRD(c);
1069 buf[3] = NUL;
1070 }
1071 else
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001072 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001073 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001074}
1075
1076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001078 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001079 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1081 * changed it was reallocated and the old pointer can no longer be used.
1082 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1083 * that was just added.
1084 */
1085 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001086typebuf_changed(
1087 int tb_change_cnt) /* old value of typebuf.tb_change_cnt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001090#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1091 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093 ));
1094}
1095
1096/*
1097 * Return TRUE if there are no characters in the typeahead buffer that have
1098 * not been typed (result from a mapping or come from ":normal").
1099 */
1100 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001101typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 return typebuf.tb_maplen == 0;
1104}
1105
1106/*
1107 * Return the number of characters that are mapped (or not typed).
1108 */
1109 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001110typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 return typebuf.tb_maplen;
1113}
1114
1115/*
1116 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1117 */
1118 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001119del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
1121 int i;
1122
1123 if (len == 0)
1124 return; /* nothing to do */
1125
1126 typebuf.tb_len -= len;
1127
1128 /*
1129 * Easy case: Just increase typebuf.tb_off.
1130 */
1131 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1132 >= 3 * MAXMAPLEN + 3)
1133 typebuf.tb_off += len;
1134 /*
1135 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1136 */
1137 else
1138 {
1139 i = typebuf.tb_off + offset;
1140 /*
1141 * Leave some extra room at the end to avoid reallocation.
1142 */
1143 if (typebuf.tb_off > MAXMAPLEN)
1144 {
1145 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1146 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1147 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1148 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1149 typebuf.tb_off = MAXMAPLEN;
1150 }
1151 /* adjust typebuf.tb_buf (include the NUL at the end) */
1152 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1153 typebuf.tb_buf + i + len,
1154 (size_t)(typebuf.tb_len - offset + 1));
1155 /* adjust typebuf.tb_noremap[] */
1156 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1157 typebuf.tb_noremap + i + len,
1158 (size_t)(typebuf.tb_len - offset));
1159 }
1160
1161 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1162 {
1163 if (typebuf.tb_maplen < offset + len)
1164 typebuf.tb_maplen = offset;
1165 else
1166 typebuf.tb_maplen -= len;
1167 }
1168 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1169 {
1170 if (typebuf.tb_silent < offset + len)
1171 typebuf.tb_silent = offset;
1172 else
1173 typebuf.tb_silent -= len;
1174 }
1175 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1176 {
1177 if (typebuf.tb_no_abbr_cnt < offset + len)
1178 typebuf.tb_no_abbr_cnt = offset;
1179 else
1180 typebuf.tb_no_abbr_cnt -= len;
1181 }
1182
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001183#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001184 /* Reset the flag that text received from a client or from feedkeys()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001185 * was inserted in the typeahead buffer. */
1186 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#endif
1188 if (++typebuf.tb_change_cnt == 0)
1189 typebuf.tb_change_cnt = 1;
1190}
1191
1192/*
1193 * Write typed characters to script file.
1194 * If recording is on put the character in the recordbuffer.
1195 */
1196 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001197gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001199 char_u *s = chars;
1200 int i;
1201 static char_u buf[4];
1202 static int buflen = 0;
1203 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001205 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001207 buf[buflen++] = *s++;
1208
1209 // When receiving a special key sequence, store it until we have all
1210 // the bytes and we can decide what to do with it.
1211 if (buflen == 1 && buf[0] == K_SPECIAL)
1212 continue;
1213 if (buflen == 2)
1214 continue;
1215 if (buflen == 3 && buf[1] == KS_EXTRA
1216 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1217 {
1218 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1219 // recording.
1220 buflen = 0;
1221 continue;
1222 }
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 /* Handle one byte at a time; no translation to be done. */
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001225 for (i = 0; i < buflen; ++i)
1226 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001228 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001230 buf[buflen] = NUL;
1231 add_buff(&recordbuff, buf, (long)buflen);
1232 /* remember how many chars were last recorded */
1233 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001235 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237 may_sync_undo();
1238
1239#ifdef FEAT_EVAL
1240 /* output "debug mode" message next time in debug mode */
1241 debug_did_msg = FALSE;
1242#endif
1243
1244 /* Since characters have been typed, consider the following to be in
1245 * another mapping. Search string will be kept in history. */
1246 ++maptick;
1247}
1248
1249/*
1250 * Sync undo. Called when typed characters are obtained from the typeahead
1251 * buffer, or when a menu is used.
1252 * Do not sync:
1253 * - In Insert mode, unless cursor key has been used.
1254 * - While reading a script file.
1255 * - When no_u_sync is non-zero.
1256 */
1257 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001258may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
1260 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001261 && scriptin[curscript] == NULL)
1262 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263}
1264
1265/*
1266 * Make "typebuf" empty and allocate new buffers.
1267 * Returns FAIL when out of memory.
1268 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001269 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001270alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 typebuf.tb_buf = alloc(TYPELEN_INIT);
1273 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1274 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1275 {
1276 free_typebuf();
1277 return FAIL;
1278 }
1279 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001280 typebuf.tb_off = MAXMAPLEN + 4; /* can insert without realloc */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 typebuf.tb_len = 0;
1282 typebuf.tb_maplen = 0;
1283 typebuf.tb_silent = 0;
1284 typebuf.tb_no_abbr_cnt = 0;
1285 if (++typebuf.tb_change_cnt == 0)
1286 typebuf.tb_change_cnt = 1;
1287 return OK;
1288}
1289
1290/*
1291 * Free the buffers of "typebuf".
1292 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001293 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001294free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001296 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001297 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001298 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001299 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001300 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001301 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001302 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001303 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304}
1305
1306/*
1307 * When doing ":so! file", the current typeahead needs to be saved, and
1308 * restored when "file" has been read completely.
1309 */
1310static typebuf_T saved_typebuf[NSCRIPT];
1311
1312 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001313save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 init_typebuf();
1316 saved_typebuf[curscript] = typebuf;
1317 /* If out of memory: restore typebuf and close file. */
1318 if (alloc_typebuf() == FAIL)
1319 {
1320 closescript();
1321 return FAIL;
1322 }
1323 return OK;
1324}
1325
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001326static int old_char = -1; /* character put back by vungetc() */
1327static int old_mod_mask; /* mod_mask for ungotten character */
Bram Moolenaarb8978712013-03-16 21:42:16 +01001328static int old_mouse_row; /* mouse_row related to old_char */
1329static int old_mouse_col; /* mouse_col related to old_char */
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331/*
1332 * Save all three kinds of typeahead, so that the user must type at a prompt.
1333 */
1334 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001335save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 tp->save_typebuf = typebuf;
1338 tp->typebuf_valid = (alloc_typebuf() == OK);
1339 if (!tp->typebuf_valid)
1340 typebuf = tp->save_typebuf;
1341
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001342 tp->old_char = old_char;
1343 tp->old_mod_mask = old_mod_mask;
1344 old_char = -1;
1345
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001346 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001347 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001348 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001349 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350# ifdef USE_INPUT_BUF
1351 tp->save_inputbuf = get_input_buf();
1352# endif
1353}
1354
1355/*
1356 * Restore the typeahead to what it was before calling save_typeahead().
1357 * The allocated memory is freed, can only be called once!
1358 */
1359 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001360restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 if (tp->typebuf_valid)
1363 {
1364 free_typebuf();
1365 typebuf = tp->save_typebuf;
1366 }
1367
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001368 old_char = tp->old_char;
1369 old_mod_mask = tp->old_mod_mask;
1370
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001371 free_buff(&readbuf1);
1372 readbuf1 = tp->save_readbuf1;
1373 free_buff(&readbuf2);
1374 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375# ifdef USE_INPUT_BUF
1376 set_input_buf(tp->save_inputbuf);
1377# endif
1378}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380/*
1381 * Open a new script file for the ":source!" command.
1382 */
1383 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001384openscript(
1385 char_u *name,
1386 int directly) /* when TRUE execute directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 if (curscript + 1 == NSCRIPT)
1389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001390 emsg(_(e_nesting));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 return;
1392 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001393
1394 // Disallow sourcing a file in the sandbox, the commands would be executed
1395 // later, possibly outside of the sandbox.
1396 if (check_secure())
1397 return;
1398
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001399#ifdef FEAT_EVAL
1400 if (ignore_script)
1401 /* Not reading from script, also don't open one. Warning message? */
1402 return;
1403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 if (scriptin[curscript] != NULL) /* already reading script */
1406 ++curscript;
1407 /* use NameBuff for expanded name */
1408 expand_env(name, NameBuff, MAXPATHL);
1409 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001411 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (curscript)
1413 --curscript;
1414 return;
1415 }
1416 if (save_typebuf() == FAIL)
1417 return;
1418
1419 /*
1420 * Execute the commands from the file right now when using ":source!"
1421 * after ":global" or ":argdo" or in a loop. Also when another command
1422 * follows. This means the display won't be updated. Don't do this
1423 * always, "make test" would fail.
1424 */
1425 if (directly)
1426 {
1427 oparg_T oa;
1428 int oldcurscript;
1429 int save_State = State;
1430 int save_restart_edit = restart_edit;
1431 int save_insertmode = p_im;
1432 int save_finish_op = finish_op;
1433 int save_msg_scroll = msg_scroll;
1434
1435 State = NORMAL;
1436 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1437 restart_edit = 0; /* don't go to Insert mode */
1438 p_im = FALSE; /* don't use 'insertmode' */
1439 clear_oparg(&oa);
1440 finish_op = FALSE;
1441
1442 oldcurscript = curscript;
1443 do
1444 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001445 update_topline_cursor(); // update cursor position and topline
1446 normal_cmd(&oa, FALSE); // execute one command
1447 vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449 while (scriptin[oldcurscript] != NULL);
1450
1451 State = save_State;
1452 msg_scroll = save_msg_scroll;
1453 restart_edit = save_restart_edit;
1454 p_im = save_insertmode;
1455 finish_op = save_finish_op;
1456 }
1457}
1458
1459/*
1460 * Close the currently active input script.
1461 */
1462 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001463closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 free_typebuf();
1466 typebuf = saved_typebuf[curscript];
1467
1468 fclose(scriptin[curscript]);
1469 scriptin[curscript] = NULL;
1470 if (curscript > 0)
1471 --curscript;
1472}
1473
Bram Moolenaarea408852005-06-25 22:49:46 +00001474#if defined(EXITFREE) || defined(PROTO)
1475 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001476close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001477{
1478 while (scriptin[0] != NULL)
1479 closescript();
1480}
1481#endif
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001493 * This function is called just before doing a blocking wait. Thus after
1494 * waiting 'updatetime' for a character to arrive.
1495 */
1496 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001497before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001498{
1499 updatescript(0);
1500#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001501 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001502 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001503#endif
1504}
1505
1506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * updatescipt() is called when a character can be written into the script file
1508 * or when we have waited some time for a character (c == 0)
1509 *
1510 * All the changed memfiles are synced if c == 0 or when the number of typed
1511 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1512 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001513 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001514updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 static int count = 0;
1517
1518 if (c && scriptout)
1519 putc(c, scriptout);
1520 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1521 {
1522 ml_sync_all(c == 0, TRUE);
1523 count = 0;
1524 }
1525}
1526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527/*
1528 * Get the next input character.
1529 * Can return a special key or a multi-byte character.
1530 * Can return NUL when called recursively, use safe_vgetc() if that's not
1531 * wanted.
1532 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1533 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001534 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 */
1536 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001537vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001541 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001544#ifdef FEAT_EVAL
1545 /* Do garbage collection when garbagecollect() was called previously and
1546 * we are now at the toplevel. */
1547 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001548 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001549#endif
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 /*
1552 * If a character was put back with vungetc, it was already processed.
1553 * Return it directly.
1554 */
1555 if (old_char != -1)
1556 {
1557 c = old_char;
1558 old_char = -1;
1559 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001560 mouse_row = old_mouse_row;
1561 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001565 mod_mask = 0x0;
1566 last_recorded_len = 0;
1567 for (;;) // this is done twice if there are modifiers
1568 {
1569 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001570
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001571 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001572#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001573 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001574#endif
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001575#if defined(FEAT_TEXT_PROP)
1576 || popup_no_mapping()
1577#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
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001767 if (!no_reduce_keys)
1768 {
1769 // A modifier was not used for a mapping, apply it to ASCII
Bram Moolenaar459fd782019-10-13 16:43:39 +02001770 // keys. Shift would already have been applied.
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001771 if ((mod_mask & MOD_MASK_CTRL)
1772 && ((c >= '`' && c <= 0x7f)
1773 || (c >= '@' && c <= '_')))
1774 {
1775 c &= 0x1f;
1776 mod_mask &= ~MOD_MASK_CTRL;
1777 }
1778 if ((mod_mask & (MOD_MASK_META | MOD_MASK_ALT))
1779 && c >= 0 && c <= 127)
1780 {
1781 c += 0x80;
1782 mod_mask &= ~(MOD_MASK_META|MOD_MASK_ALT);
1783 }
1784 }
1785
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001786 break;
1787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001789
1790#ifdef FEAT_EVAL
1791 /*
1792 * In the main loop "may_garbage_collect" can be set to do garbage
1793 * collection in the first next vgetc(). It's disabled after that to
1794 * avoid internally used Lists and Dicts to be freed.
1795 */
1796 may_garbage_collect = FALSE;
1797#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001798
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001799#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001800 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001801 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001802 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001803 bevalexpr_due_set = FALSE;
1804 ui_remove_balloon();
1805 }
1806#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001807#ifdef FEAT_TEXT_PROP
1808 if (popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001809 {
1810 if (c == Ctrl_C)
1811 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001812 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001813 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001814#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001815
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001816 // Need to process the character before we know it's safe to do something
1817 // else.
1818 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001819 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001820
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001821 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822}
1823
1824/*
1825 * Like vgetc(), but never return a NUL when called recursively, get a key
1826 * directly from the user (ignoring typeahead).
1827 */
1828 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001829safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 int c;
1832
1833 c = vgetc();
1834 if (c == NUL)
1835 c = get_keystroke();
1836 return c;
1837}
1838
1839/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001840 * Like safe_vgetc(), but loop to handle K_IGNORE.
1841 * Also ignore scrollbar events.
1842 */
1843 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001844plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001845{
1846 int c;
1847
1848 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001849 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001850 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001851
1852 if (c == K_PS)
1853 /* Only handle the first pasted character. Drop the rest, since we
1854 * don't know what to do with it. */
1855 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1856
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001857 return c;
1858}
1859
1860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 * Check if a character is available, such that vgetc() will not block.
1862 * If the next character is a special character or multi-byte, the returned
1863 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001864 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 */
1866 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001867vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868{
1869 if (old_char != -1)
1870 return old_char;
1871 return vgetorpeek(FALSE);
1872}
1873
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001874#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
1876 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1877 * codes.
1878 */
1879 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001880vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882 int c;
1883
1884 ++no_mapping;
1885 ++allow_keys;
1886 c = vpeekc();
1887 --no_mapping;
1888 --allow_keys;
1889 return c;
1890}
1891#endif
1892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893/*
1894 * Check if any character is available, also half an escape sequence.
1895 * Trick: when no typeahead found, but there is something in the typeahead
1896 * buffer, it must be an ESC that is recognized as the start of a key code.
1897 */
1898 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001899vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901 int c;
1902
1903 c = vpeekc();
1904 if (c == NUL && typebuf.tb_len > 0)
1905 c = ESC;
1906 return c;
1907}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909/*
1910 * Call vpeekc() without causing anything to be mapped.
1911 * Return TRUE if a character is available, FALSE otherwise.
1912 */
1913 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001914char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
1916 int retval;
1917
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001918#ifdef FEAT_EVAL
Bram Moolenaard0573012017-10-28 21:11:06 +02001919 /* When test_override("char_avail", 1) was called pretend there is no
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001920 * typeahead. */
1921 if (disable_char_avail_for_testing)
1922 return FALSE;
1923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 ++no_mapping;
1925 retval = vpeekc();
1926 --no_mapping;
1927 return (retval != NUL);
1928}
1929
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001930#if defined(FEAT_EVAL) || defined(PROTO)
1931/*
1932 * "getchar()" function
1933 */
1934 void
1935f_getchar(typval_T *argvars, typval_T *rettv)
1936{
1937 varnumber_T n;
1938 int error = FALSE;
1939
1940#ifdef MESSAGE_QUEUE
1941 // vpeekc() used to check for messages, but that caused problems, invoking
1942 // a callback where it was not expected. Some plugins use getchar(1) in a
1943 // loop to await a message, therefore make sure we check for messages here.
1944 parse_queued_messages();
1945#endif
1946
1947 /* Position the cursor. Needed after a message that ends in a space. */
1948 windgoto(msg_row, msg_col);
1949
1950 ++no_mapping;
1951 ++allow_keys;
1952 for (;;)
1953 {
1954 if (argvars[0].v_type == VAR_UNKNOWN)
1955 /* getchar(): blocking wait. */
1956 n = plain_vgetc();
1957 else if (tv_get_number_chk(&argvars[0], &error) == 1)
1958 /* getchar(1): only check if char avail */
1959 n = vpeekc_any();
1960 else if (error || vpeekc_any() == NUL)
1961 /* illegal argument or getchar(0) and no char avail: return zero */
1962 n = 0;
1963 else
1964 /* getchar(0) and char avail: return char */
1965 n = plain_vgetc();
1966
1967 if (n == K_IGNORE)
1968 continue;
1969 break;
1970 }
1971 --no_mapping;
1972 --allow_keys;
1973
1974 set_vim_var_nr(VV_MOUSE_WIN, 0);
1975 set_vim_var_nr(VV_MOUSE_WINID, 0);
1976 set_vim_var_nr(VV_MOUSE_LNUM, 0);
1977 set_vim_var_nr(VV_MOUSE_COL, 0);
1978
1979 rettv->vval.v_number = n;
1980 if (IS_SPECIAL(n) || mod_mask != 0)
1981 {
1982 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
1983 int i = 0;
1984
1985 /* Turn a special key into three bytes, plus modifier. */
1986 if (mod_mask != 0)
1987 {
1988 temp[i++] = K_SPECIAL;
1989 temp[i++] = KS_MODIFIER;
1990 temp[i++] = mod_mask;
1991 }
1992 if (IS_SPECIAL(n))
1993 {
1994 temp[i++] = K_SPECIAL;
1995 temp[i++] = K_SECOND(n);
1996 temp[i++] = K_THIRD(n);
1997 }
1998 else if (has_mbyte)
1999 i += (*mb_char2bytes)(n, temp + i);
2000 else
2001 temp[i++] = n;
2002 temp[i++] = NUL;
2003 rettv->v_type = VAR_STRING;
2004 rettv->vval.v_string = vim_strsave(temp);
2005
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002006 if (is_mouse_key(n))
2007 {
2008 int row = mouse_row;
2009 int col = mouse_col;
2010 win_T *win;
2011 linenr_T lnum;
2012 win_T *wp;
2013 int winnr = 1;
2014
2015 if (row >= 0 && col >= 0)
2016 {
2017 /* Find the window at the mouse coordinates and compute the
2018 * text position. */
2019 win = mouse_find_win(&row, &col, FIND_POPUP);
2020 if (win == NULL)
2021 return;
2022 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002023#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002024 if (WIN_IS_POPUP(win))
2025 winnr = 0;
2026 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002027#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002028 for (wp = firstwin; wp != win && wp != NULL;
2029 wp = wp->w_next)
2030 ++winnr;
2031 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2032 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2033 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2034 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2035 }
2036 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002037 }
2038}
2039
2040/*
2041 * "getcharmod()" function
2042 */
2043 void
2044f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2045{
2046 rettv->vval.v_number = mod_mask;
2047}
2048#endif // FEAT_EVAL
2049
2050#if defined(MESSAGE_QUEUE) || defined(PROTO)
2051# define MAX_REPEAT_PARSE 8
2052
2053/*
2054 * Process messages that have been queued for netbeans or clientserver.
2055 * Also check if any jobs have ended.
2056 * These functions can call arbitrary vimscript and should only be called when
2057 * it is safe to do so.
2058 */
2059 void
2060parse_queued_messages(void)
2061{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002062 int old_curwin_id;
2063 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002064 int i;
2065 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002066 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002067 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002068
2069 // Do not handle messages while redrawing, because it may cause buffers to
2070 // change or be wiped while they are being redrawn.
2071 if (updating_screen)
2072 return;
2073
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002074 // If memory allocation fails during startup we'll exit but curbuf or
2075 // curwin could be NULL.
2076 if (curbuf == NULL || curwin == NULL)
2077 return;
2078
2079 old_curbuf_fnum = curbuf->b_fnum;
2080 old_curwin_id = curwin->w_id;
2081
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002082 ++entered;
2083
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002084 // may_garbage_collect is set in main_loop() to do garbage collection when
2085 // blocking to wait on a character. We don't want that while parsing
2086 // messages, a callback may invoke vgetc() while lists and dicts are in use
2087 // in the call stack.
2088 may_garbage_collect = FALSE;
2089
2090 // Loop when a job ended, but don't keep looping forever.
2091 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2092 {
2093 // For Win32 mch_breakcheck() does not check for input, do it here.
2094# if defined(MSWIN) && defined(FEAT_JOB_CHANNEL)
2095 channel_handle_events(FALSE);
2096# endif
2097
2098# ifdef FEAT_NETBEANS_INTG
2099 // Process the queued netbeans messages.
2100 netbeans_parse_messages();
2101# endif
2102# ifdef FEAT_JOB_CHANNEL
2103 // Write any buffer lines still to be written.
2104 channel_write_any_lines();
2105
2106 // Process the messages queued on channels.
2107 channel_parse_messages();
2108# endif
2109# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2110 // Process the queued clientserver messages.
2111 server_parse_messages();
2112# endif
2113# ifdef FEAT_JOB_CHANNEL
2114 // Check if any jobs have ended. If so, repeat the above to handle
2115 // changes, e.g. stdin may have been closed.
2116 if (job_check_ended())
2117 continue;
2118# endif
2119# ifdef FEAT_TERMINAL
2120 free_unused_terminals();
2121# endif
2122# ifdef FEAT_SOUND_CANBERRA
2123 if (has_sound_callback_in_queue())
2124 invoke_sound_callback();
2125# endif
2126 break;
2127 }
2128
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002129 // When not nested we'll go back to waiting for a typed character. If it
2130 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002131 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002132 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002133
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002134 may_garbage_collect = save_may_garbage_collect;
2135
2136 // If the current window or buffer changed we need to bail out of the
2137 // waiting loop. E.g. when a job exit callback closes the terminal window.
2138 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
2139 ins_char_typebuf(K_IGNORE);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002140
2141 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002142}
2143#endif
2144
2145
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002146typedef enum {
2147 map_result_fail, // failed, break loop
2148 map_result_get, // get a character from typeahead
2149 map_result_retry, // try to map again
2150 map_result_nomatch // no matching mapping, get char
2151} map_result_T;
2152
2153/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002154 * Check if the bytes at the start of the typeahead buffer are a character used
2155 * in CTRL-X mode. This includes the form with a CTRL modifier.
2156 */
2157 static int
2158at_ctrl_x_key(void)
2159{
2160 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2161 int c = *p;
2162
2163 if (typebuf.tb_len > 3
2164 && c == K_SPECIAL
2165 && p[1] == KS_MODIFIER
2166 && (p[2] & MOD_MASK_CTRL))
2167 c = p[3] & 0x1f;
2168 return vim_is_ctrl_x_key(c);
2169}
2170
2171/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002172 * Handle mappings in the typeahead buffer.
2173 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002174 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002175 * - When there is no match yet, return map_result_nomatch, need to get more
2176 * typeahead.
2177 */
2178 static int
2179handle_mapping(
2180 int *keylenp,
2181 int *timedout,
2182 int *mapdepth)
2183{
2184 mapblock_T *mp = NULL;
2185 mapblock_T *mp2;
2186 mapblock_T *mp_match;
2187 int mp_match_len = 0;
2188 int max_mlen = 0;
2189 int tb_c1;
2190 int mlen;
2191#ifdef FEAT_LANGMAP
2192 int nolmaplen;
2193#endif
2194 int keylen = *keylenp;
2195 int i;
2196 int local_State = get_real_state();
2197
2198 /*
2199 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002200 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002201 *
2202 * Don't look for mappings if:
2203 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2204 * - maphash_valid not set: no mappings present.
2205 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2206 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002207 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002208 * - waiting for a char with --more--
2209 * - in Ctrl-X mode, and we get a valid char for that mode
2210 */
2211 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2212 if (no_mapping == 0 && is_maphash_valid()
2213 && (no_zero_mapping == 0 || tb_c1 != '0')
2214 && (typebuf.tb_maplen == 0
2215 || (p_remap
2216 && (typebuf.tb_noremap[typebuf.tb_off]
2217 & (RM_NONE|RM_ABBR)) == 0))
2218 && !(p_paste && (State & (INSERT + CMDLINE)))
2219 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2220 && State != ASKMORE
2221 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002222 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002223 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002224 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002225 {
2226#ifdef FEAT_LANGMAP
2227 if (tb_c1 == K_SPECIAL)
2228 nolmaplen = 2;
2229 else
2230 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002231 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2232 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002233 nolmaplen = 0;
2234 }
2235#endif
2236 // First try buffer-local mappings.
2237 mp = get_buf_maphash_list(local_State, tb_c1);
2238 mp2 = get_maphash_list(local_State, tb_c1);
2239 if (mp == NULL)
2240 {
2241 // There are no buffer-local mappings.
2242 mp = mp2;
2243 mp2 = NULL;
2244 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002245
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002246 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002247 * Loop until a partly matching mapping is found or all (local)
2248 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002249 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002250 * A full match is only accepted if there is no partly match, so "aa"
2251 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002252 */
2253 mp_match = NULL;
2254 mp_match_len = 0;
2255 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002256 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002257 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002258 // Only consider an entry if the first character matches and it is
2259 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002260 // Skip ":lmap" mappings if keys were mapped.
2261 if (mp->m_keys[0] == tb_c1
2262 && (mp->m_mode & local_State)
Bram Moolenaar459fd782019-10-13 16:43:39 +02002263 && !(mp->m_simplified && seenModifyOtherKeys)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002264 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002265 {
2266#ifdef FEAT_LANGMAP
2267 int nomap = nolmaplen;
2268 int c2;
2269#endif
2270 // find the match length of this mapping
2271 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2272 {
2273#ifdef FEAT_LANGMAP
2274 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2275 if (nomap > 0)
2276 --nomap;
2277 else if (c2 == K_SPECIAL)
2278 nomap = 2;
2279 else
2280 LANGMAP_ADJUST(c2, TRUE);
2281 if (mp->m_keys[mlen] != c2)
2282#else
2283 if (mp->m_keys[mlen] !=
2284 typebuf.tb_buf[typebuf.tb_off + mlen])
2285#endif
2286 break;
2287 }
2288
Bram Moolenaareda35f72019-08-03 14:59:44 +02002289 // Don't allow mapping the first byte(s) of a multi-byte char.
2290 // Happens when mapping <M-a> and then changing 'encoding'.
2291 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002292 {
2293 char_u *p1 = mp->m_keys;
2294 char_u *p2 = mb_unescape(&p1);
2295
2296 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002297 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002298 mlen = 0;
2299 }
2300
2301 // Check an entry whether it matches.
2302 // - Full match: mlen == keylen
2303 // - Partly match: mlen == typebuf.tb_len
2304 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002305 if (mlen == keylen || (mlen == typebuf.tb_len
2306 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002307 {
2308 char_u *s;
2309 int n;
2310
Bram Moolenaareda35f72019-08-03 14:59:44 +02002311 // If only script-local mappings are allowed, check if the
2312 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002313 s = typebuf.tb_noremap + typebuf.tb_off;
2314 if (*s == RM_SCRIPT
2315 && (mp->m_keys[0] != K_SPECIAL
2316 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002317 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002318 continue;
2319
Bram Moolenaareda35f72019-08-03 14:59:44 +02002320 // If one of the typed keys cannot be remapped, skip the
2321 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002322 for (n = mlen; --n >= 0; )
2323 if (*s++ & (RM_NONE|RM_ABBR))
2324 break;
2325 if (n >= 0)
2326 continue;
2327
2328 if (keylen > typebuf.tb_len)
2329 {
2330 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002331 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002332 {
2333 // break at a partly match
2334 keylen = KEYLEN_PART_MAP;
2335 break;
2336 }
2337 }
2338 else if (keylen > mp_match_len)
2339 {
2340 // found a longer match
2341 mp_match = mp;
2342 mp_match_len = keylen;
2343 }
2344 }
2345 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002346 // No match; may have to check for termcode at next
2347 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002348 if (max_mlen < mlen)
2349 max_mlen = mlen;
2350 }
2351 }
2352
Bram Moolenaareda35f72019-08-03 14:59:44 +02002353 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002354 if (keylen != KEYLEN_PART_MAP)
2355 {
2356 mp = mp_match;
2357 keylen = mp_match_len;
2358 }
2359 }
2360
2361 /*
2362 * Check for match with 'pastetoggle'
2363 */
2364 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2365 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002366 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2367 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002368 break;
2369 if (p_pt[mlen] == NUL) // match
2370 {
2371 // write chars to script file(s)
2372 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002373 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2374 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002375
2376 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002377 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002378 if (!(State & INSERT))
2379 {
2380 msg_col = 0;
2381 msg_row = Rows - 1;
2382 msg_clr_eos(); // clear ruler
2383 }
2384 status_redraw_all();
2385 redraw_statuslines();
2386 showmode();
2387 setcursor();
2388 *keylenp = keylen;
2389 return map_result_retry;
2390 }
2391 // Need more chars for partly match.
2392 if (mlen == typebuf.tb_len)
2393 keylen = KEYLEN_PART_KEY;
2394 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002395 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002396 max_mlen = mlen + 1;
2397 }
2398
Bram Moolenaareda35f72019-08-03 14:59:44 +02002399 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002400 {
2401 int save_keylen = keylen;
2402
2403 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002404 * When no matching mapping found or found a non-matching mapping that
2405 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002406 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002407 * - mapping is allowed,
2408 * - keys have not been mapped,
2409 * - and not an ESC sequence, not in insert mode or p_ek is on,
2410 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002411 */
2412 if ((no_mapping == 0 || allow_keys != 0)
2413 && (typebuf.tb_maplen == 0
2414 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002415 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002416 && !*timedout)
2417 {
2418 keylen = check_termcode(max_mlen + 1,
2419 NULL, 0, NULL);
2420
Bram Moolenaareda35f72019-08-03 14:59:44 +02002421 // If no termcode matched but 'pastetoggle' matched partially it's
2422 // like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002423 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2424 keylen = KEYLEN_PART_KEY;
2425
Bram Moolenaareda35f72019-08-03 14:59:44 +02002426 // When getting a partial match, but the last characters were not
2427 // typed, don't wait for a typed character to complete the
2428 // termcode. This helps a lot when a ":normal" command ends in an
2429 // ESC.
2430 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002431 keylen = 0;
2432 }
2433 else
2434 keylen = 0;
2435 if (keylen == 0) // no matching terminal code
2436 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002437#ifdef AMIGA
2438 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002439 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002440 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002441 {
2442 char_u *s;
2443
2444 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002445 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2446 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002447 ++s)
2448 ;
2449 if (*s == 'r' || *s == '|') // found one
2450 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002451 del_typebuf(
2452 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002453 // get size and redraw screen
2454 shell_resized();
2455 *keylenp = keylen;
2456 return map_result_retry;
2457 }
2458 if (*s == NUL) // need more characters
2459 keylen = KEYLEN_PART_KEY;
2460 }
2461 if (keylen >= 0)
2462#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002463 // When there was a matching mapping and no termcode could be
2464 // replaced after another one, use that mapping (loop around).
2465 // If there was no mapping at all use the character from the
2466 // typeahead buffer right here.
2467 if (mp == NULL)
2468 {
2469 *keylenp = keylen;
2470 return map_result_get; // got character, break for loop
2471 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002472 }
2473
2474 if (keylen > 0) // full matching terminal code
2475 {
2476#if defined(FEAT_GUI) && defined(FEAT_MENU)
2477 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002478 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2479 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002480 {
2481 int idx;
2482
Bram Moolenaareda35f72019-08-03 14:59:44 +02002483 // Using a menu may cause a break in undo! It's like using
2484 // gotchars(), but without recording or writing to a script
2485 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002486 may_sync_undo();
2487 del_typebuf(3, 0);
2488 idx = get_menu_index(current_menu, local_State);
2489 if (idx != MENU_INDEX_INVALID)
2490 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002491 // In Select mode and a Visual mode menu is used: Switch
2492 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002493 // back to Select mode.
2494 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002495 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002496 {
2497 VIsual_select = FALSE;
2498 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002499 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002500 }
2501 ins_typebuf(current_menu->strings[idx],
2502 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002503 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002504 }
2505 }
2506#endif // FEAT_GUI && FEAT_MENU
2507 *keylenp = keylen;
2508 return map_result_retry; // try mapping again
2509 }
2510
Bram Moolenaareda35f72019-08-03 14:59:44 +02002511 // Partial match: get some more characters. When a matching mapping
2512 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002513 if (mp == NULL || keylen < 0)
2514 keylen = KEYLEN_PART_KEY;
2515 else
2516 keylen = mp_match_len;
2517 }
2518
2519 /*
2520 * complete match
2521 */
2522 if (keylen >= 0 && keylen <= typebuf.tb_len)
2523 {
2524 char_u *map_str;
2525
2526#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002527 int save_m_expr;
2528 int save_m_noremap;
2529 int save_m_silent;
2530 char_u *save_m_keys;
2531 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002532#else
2533# define save_m_noremap mp->m_noremap
2534# define save_m_silent mp->m_silent
2535#endif
2536
2537 // write chars to script file(s)
2538 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002539 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2540 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002541
2542 cmd_silent = (typebuf.tb_silent > 0);
2543 del_typebuf(keylen, 0); // remove the mapped keys
2544
2545 /*
2546 * Put the replacement string in front of mapstr.
2547 * The depth check catches ":map x y" and ":map y x".
2548 */
2549 if (++*mapdepth >= p_mmd)
2550 {
2551 emsg(_("E223: recursive mapping"));
2552 if (State & CMDLINE)
2553 redrawcmdline();
2554 else
2555 setcursor();
2556 flush_buffers(FLUSH_MINIMAL);
2557 *mapdepth = 0; /* for next one */
2558 *keylenp = keylen;
2559 return map_result_fail;
2560 }
2561
2562 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002563 * In Select mode and a Visual mode mapping is used: Switch to Visual
2564 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002565 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002566 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002567 {
2568 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002569 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002570 }
2571
2572#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002573 // Copy the values from *mp that are used, because evaluating the
2574 // expression may invoke a function that redefines the mapping, thereby
2575 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002576 save_m_expr = mp->m_expr;
2577 save_m_noremap = mp->m_noremap;
2578 save_m_silent = mp->m_silent;
2579 save_m_keys = NULL; // only saved when needed
2580 save_m_str = NULL; // only saved when needed
2581
2582 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002583 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2584 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585 */
2586 if (mp->m_expr)
2587 {
2588 int save_vgetc_busy = vgetc_busy;
2589 int save_may_garbage_collect = may_garbage_collect;
2590
2591 vgetc_busy = 0;
2592 may_garbage_collect = FALSE;
2593
2594 save_m_keys = vim_strsave(mp->m_keys);
2595 save_m_str = vim_strsave(mp->m_str);
2596 map_str = eval_map_expr(save_m_str, NUL);
2597
2598 vgetc_busy = save_vgetc_busy;
2599 may_garbage_collect = save_may_garbage_collect;
2600 }
2601 else
2602#endif
2603 map_str = mp->m_str;
2604
2605 /*
2606 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002607 * If 'from' field is the same as the start of the 'to' field, don't
2608 * remap the first character (but do allow abbreviations).
2609 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002610 */
2611 if (map_str == NULL)
2612 i = FAIL;
2613 else
2614 {
2615 int noremap;
2616
2617 if (save_m_noremap != REMAP_YES)
2618 noremap = save_m_noremap;
2619 else if (
2620#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002621 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2622 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002623#else
2624 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2625#endif
2626 != 0)
2627 noremap = REMAP_YES;
2628 else
2629 noremap = REMAP_SKIP;
2630 i = ins_typebuf(map_str, noremap,
2631 0, TRUE, cmd_silent || save_m_silent);
2632#ifdef FEAT_EVAL
2633 if (save_m_expr)
2634 vim_free(map_str);
2635#endif
2636 }
2637#ifdef FEAT_EVAL
2638 vim_free(save_m_keys);
2639 vim_free(save_m_str);
2640#endif
2641 *keylenp = keylen;
2642 if (i == FAIL)
2643 return map_result_fail;
2644 return map_result_retry;
2645 }
2646
2647 *keylenp = keylen;
2648 return map_result_nomatch;
2649}
2650
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002651/*
2652 * unget one character (can only be done once!)
2653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002655vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 old_char = c;
2658 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002659 old_mouse_row = mouse_row;
2660 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661}
2662
2663/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002664 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 * 1. from the stuffbuffer
2666 * This is used for abbreviated commands like "D" -> "d$".
2667 * Also used to redo a command for ".".
2668 * 2. from the typeahead buffer
2669 * Stores text obtained previously but not used yet.
2670 * Also stores the result of mappings.
2671 * Also used for the ":normal" command.
2672 * 3. from the user
2673 * This may do a blocking wait if "advance" is TRUE.
2674 *
2675 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002676 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 * KeyTyped is set to TRUE in the case the user typed the key.
2678 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2679 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002680 * Just look whether there is a character available.
2681 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 *
2683 * When "no_mapping" is zero, checks for mappings in the current mode.
2684 * Only returns one byte (of a multi-byte character).
2685 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2686 */
2687 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002688vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 int c, c1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 int timedout = FALSE; /* waited for more than 1 second
2692 for mapping to complete */
2693 int mapdepth = 0; /* check for recursive mapping */
2694 int mode_deleted = FALSE; /* set when mode has been deleted */
Bram Moolenaar4e427192006-03-10 21:34:27 +00002695#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 int new_wcol, new_wrow;
2697#endif
2698#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 int shape_changed = FALSE; /* adjusted cursor shape */
2700#endif
2701 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002703 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /*
2706 * This function doesn't work very well when called recursively. This may
2707 * happen though, because of:
2708 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2709 * there is a character available, which calls this function. In that
2710 * case we must return NUL, to indicate no character is available.
2711 * 2. A GUI callback function writes to the screen, causing a
2712 * wait_return().
2713 * Using ":normal" can also do this, but it saves the typeahead buffer,
2714 * thus it should be OK. But don't get a key from the user then.
2715 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002716 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 return NUL;
2718
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002719 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 if (advance)
2722 KeyStuffed = FALSE;
2723
2724 init_typebuf();
2725 start_stuff();
2726 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002727 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 do
2729 {
2730/*
2731 * get a character: 1. from the stuffbuffer
2732 */
2733 if (typeahead_char != 0)
2734 {
2735 c = typeahead_char;
2736 if (advance)
2737 typeahead_char = 0;
2738 }
2739 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002740 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (c != NUL && !got_int)
2742 {
2743 if (advance)
2744 {
2745 /* KeyTyped = FALSE; When the command that stuffed something
2746 * was typed, behave like the stuffed command was typed.
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01002747 * needed for CTRL-W CTRL-] to open a fold, for example. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 KeyStuffed = TRUE;
2749 }
2750 if (typebuf.tb_no_abbr_cnt == 0)
2751 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
2752 }
2753 else
2754 {
2755 /*
2756 * Loop until we either find a matching mapped key, or we
2757 * are sure that it is not a mapped key.
2758 * If a mapped key sequence is found we go back to the start to
2759 * try re-mapping.
2760 */
2761 for (;;)
2762 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002763 long wait_time;
2764 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002765#ifdef FEAT_CMDL_INFO
2766 int showcmd_idx;
2767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 /*
2769 * ui_breakcheck() is slow, don't use it too often when
2770 * inside a mapping. But call it each time for typed
2771 * characters.
2772 */
2773 if (typebuf.tb_maplen)
2774 line_breakcheck();
2775 else
2776 ui_breakcheck(); /* check for CTRL-C */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (got_int)
2778 {
2779 /* flush all input */
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002780 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 /*
2783 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002784 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 * Otherwise we behave like having gotten a CTRL-C.
2786 * As a result typing CTRL-C in insert mode will
2787 * really insert a CTRL-C.
2788 */
2789 if ((c || typebuf.tb_maplen)
2790 && (State & (INSERT + CMDLINE)))
2791 c = ESC;
2792 else
2793 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002794 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002796 if (advance)
2797 {
2798 /* Also record this character, it might be needed to
2799 * get out of Insert mode. */
2800 *typebuf.tb_buf = c;
2801 gotchars(typebuf.tb_buf, 1);
2802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 cmd_silent = FALSE;
2804
2805 break;
2806 }
2807 else if (typebuf.tb_len > 0)
2808 {
2809 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002810 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 map_result_T result = handle_mapping(
2813 &keylen, &timedout, &mapdepth);
2814
2815 if (result == map_result_retry)
2816 // try mapping again
2817 continue;
2818 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002819 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002820 // failed, use the outer loop
2821 c = -1;
2822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002824 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826/*
2827 * get a character: 2. from the typeahead buffer
2828 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002829 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2830 if (advance) /* remove chars from tb_buf */
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002831 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002832 cmd_silent = (typebuf.tb_silent > 0);
2833 if (typebuf.tb_maplen > 0)
2834 KeyTyped = FALSE;
2835 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002836 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002837 KeyTyped = TRUE;
2838 /* write char to script file(s) */
2839 gotchars(typebuf.tb_buf
2840 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002841 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002842 KeyNoremap = typebuf.tb_noremap[
2843 typebuf.tb_off];
2844 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002845 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002849 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851
2852/*
2853 * get a character: 3. from the user - handle <Esc> in Insert mode
2854 */
2855 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02002856 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 * are no more characters at once, we pretend to go out of
2858 * insert mode. This prevents the one second delay after
2859 * typing an <ESC>. If we get something after all, we may
2860 * have to redisplay the mode. That the cursor is in the wrong
2861 * place does not matter.
2862 */
2863 c = 0;
2864#ifdef FEAT_CMDL_INFO
2865 new_wcol = curwin->w_wcol;
2866 new_wrow = curwin->w_wrow;
2867#endif
2868 if ( advance
2869 && typebuf.tb_len == 1
2870 && typebuf.tb_buf[typebuf.tb_off] == ESC
2871 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 && typebuf.tb_maplen == 0
2874 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002875 && (p_timeout
2876 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002878 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 colnr_T col = 0, vcol;
2881 char_u *ptr;
2882
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002883 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 unshowmode(TRUE);
2886 mode_deleted = TRUE;
2887 }
2888#ifdef FEAT_GUI
Bram Moolenaarf085f422017-06-07 20:39:47 +02002889 /* may show a different cursor shape */
2890 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
2892 int save_State;
2893
2894 save_State = State;
2895 State = NORMAL;
2896 gui_update_cursor(TRUE, FALSE);
2897 State = save_State;
2898 shape_changed = TRUE;
2899 }
2900#endif
2901 validate_cursor();
2902 old_wcol = curwin->w_wcol;
2903 old_wrow = curwin->w_wrow;
2904
2905 /* move cursor left, if possible */
2906 if (curwin->w_cursor.col != 0)
2907 {
2908 if (curwin->w_wcol > 0)
2909 {
2910 if (did_ai)
2911 {
2912 /*
2913 * We are expecting to truncate the trailing
2914 * white-space, so find the last non-white
2915 * character -- webb
2916 */
2917 col = vcol = curwin->w_wcol = 0;
2918 ptr = ml_get_curline();
2919 while (col < curwin->w_cursor.col)
2920 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002921 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002923 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002926 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 ++col;
2929 }
2930 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02002931 + curwin->w_wcol / curwin->w_width;
2932 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 curwin->w_wcol += curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 col = 0; /* no correction needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 else
2937 {
2938 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 }
2942 else if (curwin->w_p_wrap && curwin->w_wrow)
2943 {
2944 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02002945 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2949 {
2950 /* Correct when the cursor is on the right halve
2951 * of a double-wide character. */
2952 ptr = ml_get_curline();
2953 col -= (*mb_head_off)(ptr, ptr + col);
2954 if ((*mb_ptr2cells)(ptr + col) > 1)
2955 --curwin->w_wcol;
2956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958 setcursor();
2959 out_flush();
2960#ifdef FEAT_CMDL_INFO
2961 new_wcol = curwin->w_wcol;
2962 new_wrow = curwin->w_wrow;
2963#endif
2964 curwin->w_wcol = old_wcol;
2965 curwin->w_wrow = old_wrow;
2966 }
2967 if (c < 0)
2968 continue; /* end of input script reached */
Bram Moolenaar20c38922014-07-23 20:41:14 +02002969
2970 /* Allow mapping for just typed characters. When we get here c
2971 * is the number of extra bytes and typebuf.tb_len is 1. */
2972 for (n = 1; n <= c; ++n)
2973 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 typebuf.tb_len += c;
2975
2976 /* buffer full, don't map */
2977 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2978 {
2979 timedout = TRUE;
2980 continue;
2981 }
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (ex_normal_busy > 0)
2984 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01002985#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989 /* No typeahead left and inside ":normal". Must return
2990 * something to avoid getting stuck. When an incomplete
2991 * mapping is present, behave like it timed out. */
2992 if (typebuf.tb_len > 0)
2993 {
2994 timedout = TRUE;
2995 continue;
2996 }
2997 /* When 'insertmode' is set, ESC just beeps in Insert
2998 * mode. Use CTRL-L to make edit() return.
2999 * For the command line only CTRL-C always breaks it.
3000 * For the cmdline window: Alternate between ESC and
3001 * CTRL-C: ESC for most situations and CTRL-C to close the
3002 * cmdline window. */
3003 if (p_im && (State & INSERT))
3004 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003005#ifdef FEAT_TERMINAL
3006 else if (terminal_is_active())
3007 c = K_CANCEL;
3008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003010#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 )
3014 c = Ctrl_C;
3015 else
3016 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003017#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 break;
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023/*
3024 * get a character: 3. from the user - update display
3025 */
3026 /* In insert mode a screen update is skipped when characters
3027 * are still available. But when those available characters
3028 * are part of a mapping, and we are going to do a blocking
3029 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003030 * changed text so far. Also for when 'lazyredraw' is set and
3031 * redrawing was postponed because there was something in the
3032 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003033 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3034 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 update_screen(0);
3037 setcursor(); /* put cursor back where it belongs */
3038 }
3039
3040 /*
3041 * If we have a partial match (and are going to wait for more
3042 * input from the user), show the partially matched characters
3043 * to the user with showcmd.
3044 */
3045#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003046 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#endif
3048 c1 = 0;
3049 if (typebuf.tb_len > 0 && advance && !exmode_active)
3050 {
3051 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3052 && State != HITRETURN)
3053 {
3054 /* this looks nice when typing a dead character map */
3055 if (State & INSERT
3056 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3057 + typebuf.tb_len - 1) == 1)
3058 {
3059 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3060 + typebuf.tb_len - 1], FALSE);
3061 setcursor(); /* put cursor back where it belongs */
3062 c1 = 1;
3063 }
3064#ifdef FEAT_CMDL_INFO
3065 /* need to use the col and row from above here */
3066 old_wcol = curwin->w_wcol;
3067 old_wrow = curwin->w_wrow;
3068 curwin->w_wcol = new_wcol;
3069 curwin->w_wrow = new_wrow;
3070 push_showcmd();
3071 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003072 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3073 while (showcmd_idx < typebuf.tb_len)
3074 (void)add_to_showcmd(
3075 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 curwin->w_wcol = old_wcol;
3077 curwin->w_wrow = old_wrow;
3078#endif
3079 }
3080
3081 /* this looks nice when typing a dead character map */
3082 if ((State & CMDLINE)
3083#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3084 && cmdline_star == 0
3085#endif
3086 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3087 + typebuf.tb_len - 1) == 1)
3088 {
3089 putcmdline(typebuf.tb_buf[typebuf.tb_off
3090 + typebuf.tb_len - 1], FALSE);
3091 c1 = 1;
3092 }
3093 }
3094
3095/*
3096 * get a character: 3. from the user - get it
3097 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003098 if (typebuf.tb_len == 0)
3099 // timedout may have been set while waiting for a mapping
3100 // that has a <Nop> RHS.
3101 timedout = FALSE;
3102
Bram Moolenaar652de232019-04-04 20:13:09 +02003103 if (advance)
3104 {
3105 if (typebuf.tb_len == 0
3106 || !(p_timeout
3107 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3108 // blocking wait
3109 wait_time = -1L;
3110 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3111 wait_time = p_ttm;
3112 else
3113 wait_time = p_tm;
3114 }
3115 else
3116 wait_time = 0;
3117
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003118 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3120 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003121 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
3123#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003124 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 pop_showcmd();
3126#endif
3127 if (c1 == 1)
3128 {
3129 if (State & INSERT)
3130 edit_unputchar();
3131 if (State & CMDLINE)
3132 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003133 else
3134 setcursor(); /* put cursor back where it belongs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136
3137 if (c < 0)
3138 continue; /* end of input script reached */
3139 if (c == NUL) /* no character available */
3140 {
3141 if (!advance)
3142 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003143 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 timedout = TRUE;
3146 continue;
3147 }
3148 }
3149 else
3150 { /* allow mapping for just typed characters */
3151 while (typebuf.tb_buf[typebuf.tb_off
3152 + typebuf.tb_len] != NUL)
3153 typebuf.tb_noremap[typebuf.tb_off
3154 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003155#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /* Get IM status right after getting keys, not after the
3157 * timeout for a mapping (focus may be lost by then). */
3158 vgetc_im_active = im_get_status();
3159#endif
3160 }
3161 } /* for (;;) */
3162 } /* if (!character from stuffbuf) */
3163
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003164 /* if advance is FALSE don't loop on NULs */
3165 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 /*
3168 * The "INSERT" message is taken care of here:
3169 * if we return an ESC to exit insert mode, the message is deleted
3170 * if we don't return an ESC but deleted the message before, redisplay it
3171 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003172 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003174 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 if (typebuf.tb_len && !KeyTyped)
3177 redraw_cmdline = TRUE; /* delete mode later */
3178 else
3179 unshowmode(FALSE);
3180 }
3181 else if (c != ESC && mode_deleted)
3182 {
3183 if (typebuf.tb_len && !KeyTyped)
3184 redraw_cmdline = TRUE; /* show mode later */
3185 else
3186 showmode();
3187 }
3188 }
3189#ifdef FEAT_GUI
3190 /* may unshow different cursor shape */
Bram Moolenaarf085f422017-06-07 20:39:47 +02003191 if (gui.in_use && shape_changed)
3192 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003194 if (timedout && c == ESC)
3195 {
3196 char_u nop_buf[3];
3197
3198 // When recording there will be no timeout. Add a <Nop> after the ESC
3199 // to avoid that it forms a key code with following characters.
3200 nop_buf[0] = K_SPECIAL;
3201 nop_buf[1] = KS_EXTRA;
3202 nop_buf[2] = KE_NOP;
3203 gotchars(nop_buf, 3);
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003206 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
3208 return c;
3209}
3210
3211/*
3212 * inchar() - get one character from
3213 * 1. a scriptfile
3214 * 2. the keyboard
3215 *
3216 * As much characters as we can get (upto 'maxlen') are put in "buf" and
3217 * NUL terminated (buffer length must be 'maxlen' + 1).
3218 * Minimum for "maxlen" is 3!!!!
3219 *
3220 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3221 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3222 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3223 * otherwise.
3224 *
3225 * If we got an interrupt all input is read until none is available.
3226 *
3227 * If wait_time == 0 there is no waiting for the char.
3228 * If wait_time == n we wait for n msec for a character to arrive.
3229 * If wait_time == -1 we wait forever for a character to arrive.
3230 *
3231 * Return the number of obtained characters.
3232 * Return -1 when end of input script reached.
3233 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003234 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003235inchar(
3236 char_u *buf,
3237 int maxlen,
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003238 long wait_time) /* milli seconds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 int len = 0; /* init for GCC */
3241 int retesc = FALSE; /* return ESC with gotint */
3242 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003243 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
3246 {
3247 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003248 out_flush_cursor(FALSE, FALSE);
3249#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3250 if (gui.in_use && postponed_mouseshape)
3251 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif
3253 }
3254
3255 /*
3256 * Don't reset these when at the hit-return prompt, otherwise a endless
3257 * recursive loop may result (write error in swapfile, hit-return, timeout
3258 * on char wait, flush swapfile, write error....).
3259 */
3260 if (State != HITRETURN)
3261 {
3262 did_outofmem_msg = FALSE; /* display out of memory message (again) */
3263 did_swapwrite_msg = FALSE; /* display swap file write error again */
3264 }
3265 undo_off = FALSE; /* restart undo now */
3266
3267 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003268 * Get a character from a script file if there is one.
3269 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003272 while (scriptin[curscript] != NULL && script_char < 0
3273#ifdef FEAT_EVAL
3274 && !ignore_script
3275#endif
3276 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003278#ifdef MESSAGE_QUEUE
3279 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003280#endif
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3283 {
3284 /* Reached EOF.
3285 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3286 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
3287 closescript();
3288 /*
3289 * When reading script file is interrupted, return an ESC to get
3290 * back to normal mode.
3291 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3292 */
3293 if (got_int)
3294 retesc = TRUE;
3295 else
3296 return -1;
3297 }
3298 else
3299 {
3300 buf[0] = script_char;
3301 len = 1;
3302 }
3303 }
3304
3305 if (script_char < 0) /* did not get a character from script */
3306 {
3307 /*
3308 * If we got an interrupt, skip all previously typed characters and
3309 * return TRUE if quit reading script file.
3310 * Stop reading typeahead when a single CTRL-C was read,
3311 * fill_input_buf() returns this when not able to read from stdin.
3312 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3313 * and buf may be pointing inside typebuf.tb_buf[].
3314 */
3315 if (got_int)
3316 {
3317#define DUM_LEN MAXMAPLEN * 3 + 3
3318 char_u dum[DUM_LEN + 1];
3319
3320 for (;;)
3321 {
3322 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3323 if (len == 0 || (len == 1 && dum[0] == 3))
3324 break;
3325 }
3326 return retesc;
3327 }
3328
3329 /*
3330 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003331 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003333 if (wait_time == -1L || wait_time > 10L)
3334 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /*
3337 * Fill up to a third of the buffer, because each character may be
3338 * tripled below.
3339 */
3340 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3341 }
3342
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003343 /* If the typebuf was changed further down, it is like nothing was added by
3344 * this call. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (typebuf_changed(tb_change_cnt))
3346 return 0;
3347
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003348 /* Note the change in the typeahead buffer, this matters for when
3349 * vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3350 * function. */
3351 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3352 typebuf.tb_change_cnt = 1;
3353
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003354 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355}
3356
3357/*
3358 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003359 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * Returns the new length.
3361 */
3362 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003363fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 int i;
3366 char_u *p = buf;
3367
3368 /*
3369 * Two characters are special: NUL and K_SPECIAL.
3370 * When compiled With the GUI CSI is also special.
3371 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3372 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3373 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 */
3375 for (i = len; --i >= 0; ++p)
3376 {
3377#ifdef FEAT_GUI
3378 /* When the GUI is used any character can come after a CSI, don't
3379 * escape it. */
3380 if (gui.in_use && p[0] == CSI && i >= 2)
3381 {
3382 p += 2;
3383 i -= 2;
3384 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003385# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 /* When the GUI is not used CSI needs to be escaped. */
3387 else if (!gui.in_use && p[0] == CSI)
3388 {
3389 mch_memmove(p + 3, p + 1, (size_t)i);
3390 *p++ = K_SPECIAL;
3391 *p++ = KS_EXTRA;
3392 *p = (int)KE_CSI;
3393 len += 2;
3394 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 else
3397#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003398 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003399 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003400 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003401#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003402 // Win32 console passes modifiers
3403 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003404# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003405 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003406# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003407 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#endif
3409 ))
3410 {
3411 mch_memmove(p + 3, p + 1, (size_t)i);
3412 p[2] = K_THIRD(p[0]);
3413 p[1] = K_SECOND(p[0]);
3414 p[0] = K_SPECIAL;
3415 p += 2;
3416 len += 2;
3417 }
3418 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003419 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return len;
3421}
3422
3423#if defined(USE_INPUT_BUF) || defined(PROTO)
3424/*
3425 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3426 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003427 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003428 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 */
3430 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003431input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
3433 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003434# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3435 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436# endif
3437 );
3438}
3439#endif