blob: 051dee465e537afa54c879e7995a75de208aad44 [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/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar285e3352018-04-18 23:01:13 +020039static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
40static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
41static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45/*
46 * when block_redo is TRUE redo buffer will not be changed
47 * used by edit() to repeat insertions and 'V' command for redoing
48 */
49static int block_redo = FALSE;
50
Bram Moolenaar459fd782019-10-13 16:43:39 +020051static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020054 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 *
56 * typebuf.tb_buf[] contains all characters that are not consumed yet.
57 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
58 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
59 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
60 * The head of the buffer may contain the result of mappings, abbreviations
61 * and @a commands. The length of this part is typebuf.tb_maplen.
62 * typebuf.tb_silent is the part where <silent> applies.
63 * After the head are characters that come from the terminal.
64 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
65 * should not be considered for abbreviations.
66 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
67 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
68 * contains RM_NONE for the characters that are not to be remapped.
69 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
70 * (typebuf has been put in globals.h, because check_termcode() needs it).
71 */
Bram Moolenaar30613902019-12-01 22:11:18 +010072#define RM_YES 0 // tb_noremap: remap
73#define RM_NONE 1 // tb_noremap: don't remap
74#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
75#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar30613902019-12-01 22:11:18 +010077// typebuf.tb_buf has three parts: room in front (for result of mappings), the
78// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000079// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010081static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
82static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020093static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020095static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020097static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
Bram Moolenaard0fb2d82022-04-04 21:03:52 +010099// flags for vgetorpeek()
100#define VGOP_ADVANCE 1 // really get the character
101#define VGOP_NO_STUFF 2 // do not use the stuff buffer
102
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103/*
104 * Free and clear a buffer.
105 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200106 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100107free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100109 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaar285e3352018-04-18 23:01:13 +0200111 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 {
113 np = p->b_next;
114 vim_free(p);
115 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200116 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000117 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118}
119
120/*
121 * Return the contents of a buffer as a single string.
122 * K_SPECIAL and CSI in the returned string are escaped.
123 */
124 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100125get_buffcont(
126 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100127 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
129 long_u count = 0;
130 char_u *p = NULL;
131 char_u *p2;
132 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200133 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar30613902019-12-01 22:11:18 +0100135 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200136 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 count += (long_u)STRLEN(bp->b_str);
138
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200139 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 {
141 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200142 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 for (str = bp->b_str; *str; )
144 *p2++ = *str++;
145 *p2 = NUL;
146 }
147 return (p);
148}
149
150/*
151 * Return the contents of the record buffer as a single string
152 * and clear the record buffer.
153 * K_SPECIAL and CSI in the returned string are escaped.
154 */
155 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100156get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157{
158 char_u *p;
159 size_t len;
160
161 p = get_buffcont(&recordbuff, TRUE);
162 free_buff(&recordbuff);
163
164 /*
165 * Remove the characters that were added the last time, these must be the
166 * (possibly mapped) characters that stopped the recording.
167 */
168 len = STRLEN(p);
169 if ((int)len >= last_recorded_len)
170 {
171 len -= last_recorded_len;
172 p[len] = NUL;
173 }
174
175 /*
176 * When stopping recording from Insert mode with CTRL-O q, also remove the
177 * CTRL-O.
178 */
179 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
180 p[len - 1] = NUL;
181
182 return (p);
183}
184
185/*
186 * Return the contents of the redo buffer as a single string.
187 * K_SPECIAL and CSI in the returned string are escaped.
188 */
189 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100190get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000192 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193}
194
195/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000196 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 * K_SPECIAL and CSI should have been escaped already.
198 */
199 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100200add_buff(
201 buffheader_T *buf,
202 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100203 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100205 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200206 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
208 if (slen < 0)
209 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100210 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 return;
212
Bram Moolenaar30613902019-12-01 22:11:18 +0100213 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {
215 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200216 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100218 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000220 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 return;
222 }
223 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200224 mch_memmove(buf->bh_first.b_next->b_str,
225 buf->bh_first.b_next->b_str + buf->bh_index,
226 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 buf->bh_index = 0;
228
229 if (buf->bh_space >= (int)slen)
230 {
231 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000232 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 buf->bh_space -= slen;
234 }
235 else
236 {
237 if (slen < MINIMAL_SIZE)
238 len = MINIMAL_SIZE;
239 else
240 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200241 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100243 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000244 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000245 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
Bram Moolenaar285e3352018-04-18 23:01:13 +0200247 p->b_next = buf->bh_curr->b_next;
248 buf->bh_curr->b_next = p;
249 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251}
252
253/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000254 * Delete "slen" bytes from the end of "buf".
255 * Only works when it was just added.
256 */
257 static void
258delete_buff_tail(buffheader_T *buf, int slen)
259{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000260 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000261
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000262 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000263 return; // nothing to delete
264 len = (int)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000265 if (len >= slen)
266 {
267 buf->bh_curr->b_str[len - slen] = NUL;
268 buf->bh_space += slen;
269 }
270}
271
272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 * Add number "n" to buffer "buf".
274 */
275 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100276add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278 char_u number[32];
279
280 sprintf((char *)number, "%ld", n);
281 add_buff(buf, number, -1L);
282}
283
284/*
285 * Add character 'c' to buffer "buf".
286 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
287 */
288 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100289add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 char_u bytes[MB_MAXBYTES + 1];
292 int len;
293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 char_u temp[4];
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 if (IS_SPECIAL(c))
297 len = 1;
298 else
299 len = (*mb_char2bytes)(c, bytes);
300 for (i = 0; i < len; ++i)
301 {
302 if (!IS_SPECIAL(c))
303 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
305 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
306 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100307 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 temp[0] = K_SPECIAL;
309 temp[1] = K_SECOND(c);
310 temp[2] = K_THIRD(c);
311 temp[3] = NUL;
312 }
313#ifdef FEAT_GUI
314 else if (c == CSI)
315 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100316 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 temp[0] = CSI;
318 temp[1] = KS_EXTRA;
319 temp[2] = (int)KE_CSI;
320 temp[3] = NUL;
321 }
322#endif
323 else
324 {
325 temp[0] = c;
326 temp[1] = NUL;
327 }
328 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
Bram Moolenaar30613902019-12-01 22:11:18 +0100332// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200333static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100334
Bram Moolenaar30613902019-12-01 22:11:18 +0100335// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200336static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100337
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100339 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
340 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 * If advance == TRUE go to the next char.
342 * No translation is done K_SPECIAL and CSI are escaped.
343 */
344 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100345read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100347 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100349 c = read_readbuf(&readbuf1, advance);
350 if (c == NUL)
351 c = read_readbuf(&readbuf2, advance);
352 return c;
353}
354
355 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100356read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100357{
358 char_u c;
359 buffblock_T *curr;
360
Bram Moolenaar30613902019-12-01 22:11:18 +0100361 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return NUL;
363
Bram Moolenaar285e3352018-04-18 23:01:13 +0200364 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100365 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 if (advance)
368 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200371 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100373 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 }
376 return c;
377}
378
379/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100380 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 */
382 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100383start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200385 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200387 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100388 readbuf1.bh_space = 0;
389 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200390 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100391 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200392 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100393 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 }
395}
396
397/*
398 * Return TRUE if the stuff buffer is empty.
399 */
400 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100401stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200403 return (readbuf1.bh_first.b_next == NULL
404 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100405}
406
Bram Moolenaar113e1072019-01-20 15:30:40 +0100407#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100408/*
409 * Return TRUE if readbuf1 is empty. There may still be redo characters in
410 * redbuf2.
411 */
412 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100413readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100414{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200415 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419/*
420 * Set a typeahead character that won't be flushed.
421 */
422 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100423typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424{
425 typeahead_char = c;
426}
427
428/*
429 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100430 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * flush all typeahead characters (used when interrupted by a CTRL-C).
432 */
433 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200434flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
436 init_typebuf();
437
438 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100439 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 ;
441
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200442 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200444 // remove mapped characters at the start only
445 typebuf.tb_off += typebuf.tb_maplen;
446 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100447#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
448 if (typebuf.tb_len == 0)
449 typebuf_was_filled = FALSE;
450#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200451 }
452 else
453 {
454 // remove typeahead
455 if (flush_typeahead == FLUSH_INPUT)
456 // We have to get all characters, because we may delete the first
457 // part of an escape sequence. In an xterm we get one char at a
458 // time and we have to get them all.
459 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
460 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 typebuf.tb_off = MAXMAPLEN;
462 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200463#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100464 // Reset the flag that text received from a client or from feedkeys()
465 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200466 typebuf_was_filled = FALSE;
467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 typebuf.tb_maplen = 0;
470 typebuf.tb_silent = 0;
471 cmd_silent = FALSE;
472 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200473 if (++typebuf.tb_change_cnt == 0)
474 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475}
476
477/*
478 * The previous contents of the redo buffer is kept in old_redobuffer.
479 * This is used for the CTRL-O <.> command in insert mode.
480 */
481 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100482ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 if (!block_redo)
485 {
486 free_buff(&old_redobuff);
487 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200488 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 }
490}
491
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100492/*
493 * Discard the contents of the redo buffer and restore the previous redo
494 * buffer.
495 */
496 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100497CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100498{
499 if (!block_redo)
500 {
501 free_buff(&redobuff);
502 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200503 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100504 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100505 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100506 ;
507 }
508}
509
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510/*
511 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
512 * Used before executing autocommands and user functions.
513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200515saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 char_u *s;
518
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200519 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200520 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200521 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200522 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
Bram Moolenaar30613902019-12-01 22:11:18 +0100524 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200525 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
526 if (s != NULL)
527 {
528 add_buff(&redobuff, s, -1L);
529 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
531}
532
533/*
534 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
535 * Used after executing autocommands and user functions.
536 */
537 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200538restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200540 free_buff(&redobuff);
541 redobuff = save_redo->sr_redobuff;
542 free_buff(&old_redobuff);
543 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
546/*
547 * Append "s" to the redo buffer.
548 * K_SPECIAL and CSI should already have been escaped.
549 */
550 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100551AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
553 if (!block_redo)
554 add_buff(&redobuff, s, -1L);
555}
556
557/*
558 * Append to Redo buffer literally, escaping special characters with CTRL-V.
559 * K_SPECIAL and CSI are escaped as well.
560 */
561 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100562AppendToRedobuffLit(
563 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100564 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000566 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 int c;
568 char_u *start;
569
570 if (block_redo)
571 return;
572
Bram Moolenaarebefac62005-12-28 22:39:57 +0000573 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100575 // Put a string of normal characters in the redo buffer (that's
576 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000578 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 ++s;
580
Bram Moolenaar30613902019-12-01 22:11:18 +0100581 // Don't put '0' or '^' as last character, just in case a CTRL-D is
582 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
584 --s;
585 if (s > start)
586 add_buff(&redobuff, start, (long)(s - start));
587
Bram Moolenaarebefac62005-12-28 22:39:57 +0000588 if (*s == NUL || (len >= 0 && s - str >= len))
589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar30613902019-12-01 22:11:18 +0100591 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000592 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100593 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000594 c = mb_cptr2char_adv(&s);
595 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000596 c = *s++;
597 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
598 add_char_buff(&redobuff, Ctrl_V);
599
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000600 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000601 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000602 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000603 else
604 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
606}
607
608/*
609 * Append a character to the redo buffer.
610 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
611 */
612 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100613AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 if (!block_redo)
616 add_char_buff(&redobuff, c);
617}
618
619/*
620 * Append a number to the redo buffer.
621 */
622 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100623AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 if (!block_redo)
626 add_num_buff(&redobuff, n);
627}
628
629/*
630 * Append string "s" to the stuff buffer.
631 * CSI and K_SPECIAL must already have been escaped.
632 */
633 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100634stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100636 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637}
638
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200639/*
640 * Append string "s" to the redo stuff buffer.
641 * CSI and K_SPECIAL must already have been escaped.
642 */
643 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100644stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200645{
646 add_buff(&readbuf2, s, -1L);
647}
648
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200649 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100650stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100652 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655#if defined(FEAT_EVAL) || defined(PROTO)
656/*
657 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
658 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200659 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 */
661 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100662stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200664 int c;
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 while (*s != NUL)
667 {
668 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
669 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100670 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 stuffReadbuffLen(s, 3L);
672 s += 3;
673 }
674 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200675 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200676 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200677 if (c == CAR || c == NL || c == ESC)
678 c = ' ';
679 stuffcharReadbuff(c);
680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682}
683#endif
684
685/*
686 * Append a character to the stuff buffer.
687 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
688 */
689 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100690stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100692 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695/*
696 * Append a number to the stuff buffer.
697 */
698 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100699stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100701 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702}
703
704/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200705 * Stuff a string into the typeahead buffer, such that edit() will insert it
706 * literally ("literally" TRUE) or interpret is as typed characters.
707 */
708 void
709stuffescaped(char_u *arg, int literally)
710{
711 int c;
712 char_u *start;
713
714 while (*arg != NUL)
715 {
716 // Stuff a sequence of normal ASCII characters, that's fast. Also
717 // stuff K_SPECIAL to get the effect of a special key when "literally"
718 // is TRUE.
719 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000720 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200721 || (*arg == K_SPECIAL && !literally))
722 ++arg;
723 if (arg > start)
724 stuffReadbuffLen(start, (long)(arg - start));
725
726 // stuff a single special character
727 if (*arg != NUL)
728 {
729 if (has_mbyte)
730 c = mb_cptr2char_adv(&arg);
731 else
732 c = *arg++;
733 if (literally && ((c < ' ' && c != TAB) || c == DEL))
734 stuffcharReadbuff(Ctrl_V);
735 stuffcharReadbuff(c);
736 }
737 }
738}
739
740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
742 * multibyte characters.
743 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100744 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
745 * otherwise.
746 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 */
748 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100749read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 static buffblock_T *bp;
752 static char_u *p;
753 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100754 int n;
755 char_u buf[MB_MAXBYTES + 1];
756 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 if (init)
759 {
760 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200761 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200763 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 if (bp == NULL)
765 return FAIL;
766 p = bp->b_str;
767 return OK;
768 }
769 if ((c = *p) != NUL)
770 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100771 // Reverse the conversion done by add_char_buff()
772 // For a multi-byte character get all the bytes and return the
773 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
775 n = MB_BYTE2LEN_CHECK(c);
776 else
777 n = 1;
778 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100780 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
782 c = TO_SPECIAL(p[1], p[2]);
783 p += 2;
784 }
785#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100786 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 p += 2;
788#endif
789 if (*++p == NUL && bp->b_next != NULL)
790 {
791 bp = bp->b_next;
792 p = bp->b_str;
793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100795 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
797 if (n != 1)
798 c = (*mb_ptr2char)(buf);
799 break;
800 }
801 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100802 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805 }
806
807 return c;
808}
809
810/*
811 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
812 * If old_redo is TRUE, use old_redobuff instead of redobuff.
813 * The escaped K_SPECIAL and CSI are copied without translation.
814 */
815 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100816copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 int c;
819
820 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100821 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100825 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 * Insert the redo count into the command.
827 * If "old_redo" is TRUE, the last but one command is repeated
828 * instead of the last command (inserting text). This is used for
829 * CTRL-O <.> in insert mode
830 *
831 * return FAIL for failure, OK otherwise
832 */
833 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100834start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 int c;
837
Bram Moolenaar30613902019-12-01 22:11:18 +0100838 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (read_redo(TRUE, old_redo) == FAIL)
840 return FAIL;
841
842 c = read_redo(FALSE, old_redo);
843
Bram Moolenaar30613902019-12-01 22:11:18 +0100844 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (c == '"')
846 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100847 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 c = read_redo(FALSE, old_redo);
849
Bram Moolenaar30613902019-12-01 22:11:18 +0100850 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (c >= '1' && c < '9')
852 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100853 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200854
Bram Moolenaar30613902019-12-01 22:11:18 +0100855 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200856 if (c == '=')
857 {
858 add_char_buff(&readbuf2, CAR);
859 cmd_silent = TRUE;
860 }
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 c = read_redo(FALSE, old_redo);
863 }
864
Bram Moolenaar30613902019-12-01 22:11:18 +0100865 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
867 VIsual = curwin->w_cursor;
868 VIsual_active = TRUE;
869 VIsual_select = FALSE;
870 VIsual_reselect = TRUE;
871 redo_VIsual_busy = TRUE;
872 c = read_redo(FALSE, old_redo);
873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaar30613902019-12-01 22:11:18 +0100875 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (count)
877 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100878 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
882
Bram Moolenaar30613902019-12-01 22:11:18 +0100883 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100884 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 copy_redo(old_redo);
886 return OK;
887}
888
889/*
890 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100891 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 * return FAIL for failure, OK otherwise
893 */
894 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100895start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897 int c;
898
899 if (read_redo(TRUE, FALSE) == FAIL)
900 return FAIL;
901 start_stuff();
902
Bram Moolenaar30613902019-12-01 22:11:18 +0100903 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 while ((c = read_redo(FALSE, FALSE)) != NUL)
905 {
906 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
907 {
908 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100909 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 break;
911 }
912 }
913
Bram Moolenaar30613902019-12-01 22:11:18 +0100914 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 copy_redo(FALSE);
916 block_redo = TRUE;
917 return OK;
918}
919
920 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100921stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 block_redo = FALSE;
924}
925
926/*
927 * Initialize typebuf.tb_buf to point to typebuf_init.
928 * alloc() cannot be used here: In out-of-memory situations it would
929 * be impossible to type anything.
930 */
931 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100932init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 if (typebuf.tb_buf == NULL)
935 {
936 typebuf.tb_buf = typebuf_init;
937 typebuf.tb_noremap = noremapbuf_init;
938 typebuf.tb_buflen = TYPELEN_INIT;
939 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200940 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 typebuf.tb_change_cnt = 1;
942 }
943}
944
945/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200946 * Returns TRUE when keys cannot be remapped.
947 */
948 int
949noremap_keys(void)
950{
951 return KeyNoremap & (RM_NONE|RM_SCRIPT);
952}
953
954/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100955 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
956 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 *
958 * If noremap is REMAP_YES, new string can be mapped again.
959 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000960 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000961 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
963 * script-local mappings.
964 * If noremap is > 0, that many characters of the new string cannot be mapped.
965 *
966 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
967 * offset is non-zero!).
968 *
969 * If silent is TRUE, cmd_silent is set when the characters are obtained.
970 *
971 * return FAIL for failure, OK otherwise
972 */
973 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100974ins_typebuf(
975 char_u *str,
976 int noremap,
977 int offset,
978 int nottyped,
979 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 char_u *s1, *s2;
982 int newlen;
983 int addlen;
984 int i;
985 int newoff;
986 int val;
987 int nrm;
988
989 init_typebuf();
990 if (++typebuf.tb_change_cnt == 0)
991 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200992 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (offset == 0 && addlen <= typebuf.tb_off)
997 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100998 /*
999 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 typebuf.tb_off -= addlen;
1002 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1003 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001004 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1005 >= addlen + 3 * (MAXMAPLEN + 4))
1006 {
1007 /*
1008 * Buffer is empty and string fits in the existing buffer.
1009 * Leave some space before and after, if possible.
1010 */
1011 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1012 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else
1015 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001016 int extra;
1017
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001018 /*
1019 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001020 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001021 * characters. We add some extra room to avoid having to allocate too
1022 * often.
1023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001025 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1026 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001028 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001029 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 setcursor();
1031 return FAIL;
1032 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001033 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001035 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 return FAIL;
1037 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001038 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 vim_free(s1);
1041 return FAIL;
1042 }
1043 typebuf.tb_buflen = newlen;
1044
Bram Moolenaar30613902019-12-01 22:11:18 +01001045 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1047 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001048 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001050 // copy the old chars, after the insertion point, including the NUL at
1051 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 mch_memmove(s1 + newoff + offset + addlen,
1053 typebuf.tb_buf + typebuf.tb_off + offset,
1054 (size_t)(typebuf.tb_len - offset + 1));
1055 if (typebuf.tb_buf != typebuf_init)
1056 vim_free(typebuf.tb_buf);
1057 typebuf.tb_buf = s1;
1058
1059 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1060 (size_t)offset);
1061 mch_memmove(s2 + newoff + offset + addlen,
1062 typebuf.tb_noremap + typebuf.tb_off + offset,
1063 (size_t)(typebuf.tb_len - offset));
1064 if (typebuf.tb_noremap != noremapbuf_init)
1065 vim_free(typebuf.tb_noremap);
1066 typebuf.tb_noremap = s2;
1067
1068 typebuf.tb_off = newoff;
1069 }
1070 typebuf.tb_len += addlen;
1071
Bram Moolenaar30613902019-12-01 22:11:18 +01001072 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (noremap == REMAP_SCRIPT)
1074 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001075 else if (noremap == REMAP_SKIP)
1076 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 else
1078 val = RM_NONE;
1079
1080 /*
1081 * Adjust typebuf.tb_noremap[] for the new characters:
1082 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1083 * (sometimes) not remappable
1084 * If noremap == REMAP_YES: all the new characters are mappable
1085 * If noremap > 0: "noremap" characters are not remappable, the rest
1086 * mappable
1087 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001088 if (noremap == REMAP_SKIP)
1089 nrm = 1;
1090 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 nrm = addlen;
1092 else
1093 nrm = noremap;
1094 for (i = 0; i < addlen; ++i)
1095 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1096 (--nrm >= 0) ? val : RM_YES;
1097
Bram Moolenaar30613902019-12-01 22:11:18 +01001098 // tb_maplen and tb_silent only remember the length of mapped and/or
1099 // silent mappings at the start of the buffer, assuming that a mapped
1100 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (nottyped || typebuf.tb_maplen > offset)
1102 typebuf.tb_maplen += addlen;
1103 if (silent || typebuf.tb_silent > offset)
1104 {
1105 typebuf.tb_silent += addlen;
1106 cmd_silent = TRUE;
1107 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001108 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 typebuf.tb_no_abbr_cnt += addlen;
1110
1111 return OK;
1112}
1113
1114/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001115 * Put character "c" back into the typeahead buffer.
1116 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001117 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1118 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001119 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001120 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001121 int
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001122ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001123{
zeertzjq6cac7702022-01-04 18:01:21 +00001124 char_u buf[MB_MAXBYTES * 3 + 4];
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001125 int len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001126
1127 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 {
1129 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001130 buf[1] = KS_MODIFIER;
1131 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001132 buf[3] = NUL;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001133 len = 3;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001134 }
1135 if (IS_SPECIAL(c))
1136 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001137 buf[len] = K_SPECIAL;
1138 buf[len + 1] = K_SECOND(c);
1139 buf[len + 2] = K_THIRD(c);
1140 buf[len + 3] = NUL;
1141 len += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001142 }
1143 else
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001144 {
zeertzjq646bb722022-02-16 17:51:47 +00001145 char_u *end = add_char2buf(c, buf + len);
1146 *end = NUL;
1147 len = end - buf;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001148 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001149 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001150 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001151}
1152
1153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001155 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001156 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1158 * changed it was reallocated and the old pointer can no longer be used.
1159 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1160 * that was just added.
1161 */
1162 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001163typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001164 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165{
1166 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001167#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1168 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif
1170 ));
1171}
1172
1173/*
1174 * Return TRUE if there are no characters in the typeahead buffer that have
1175 * not been typed (result from a mapping or come from ":normal").
1176 */
1177 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001178typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 return typebuf.tb_maplen == 0;
1181}
1182
1183/*
1184 * Return the number of characters that are mapped (or not typed).
1185 */
1186 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001187typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 return typebuf.tb_maplen;
1190}
1191
1192/*
1193 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1194 */
1195 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001196del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 int i;
1199
1200 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001201 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
1203 typebuf.tb_len -= len;
1204
1205 /*
1206 * Easy case: Just increase typebuf.tb_off.
1207 */
1208 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1209 >= 3 * MAXMAPLEN + 3)
1210 typebuf.tb_off += len;
1211 /*
1212 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1213 */
1214 else
1215 {
1216 i = typebuf.tb_off + offset;
1217 /*
1218 * Leave some extra room at the end to avoid reallocation.
1219 */
1220 if (typebuf.tb_off > MAXMAPLEN)
1221 {
1222 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1223 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1224 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1225 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1226 typebuf.tb_off = MAXMAPLEN;
1227 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001228 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1230 typebuf.tb_buf + i + len,
1231 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001232 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1234 typebuf.tb_noremap + i + len,
1235 (size_t)(typebuf.tb_len - offset));
1236 }
1237
Bram Moolenaar30613902019-12-01 22:11:18 +01001238 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 if (typebuf.tb_maplen < offset + len)
1241 typebuf.tb_maplen = offset;
1242 else
1243 typebuf.tb_maplen -= len;
1244 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001245 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 if (typebuf.tb_silent < offset + len)
1248 typebuf.tb_silent = offset;
1249 else
1250 typebuf.tb_silent -= len;
1251 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001252 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 if (typebuf.tb_no_abbr_cnt < offset + len)
1255 typebuf.tb_no_abbr_cnt = offset;
1256 else
1257 typebuf.tb_no_abbr_cnt -= len;
1258 }
1259
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001260#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001261 // Reset the flag that text received from a client or from feedkeys()
1262 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001263 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#endif
1265 if (++typebuf.tb_change_cnt == 0)
1266 typebuf.tb_change_cnt = 1;
1267}
1268
1269/*
1270 * Write typed characters to script file.
1271 * If recording is on put the character in the recordbuffer.
1272 */
1273 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001274gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001276 char_u *s = chars;
1277 int i;
1278 static char_u buf[4];
1279 static int buflen = 0;
1280 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001282 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001284 buf[buflen++] = *s++;
1285
1286 // When receiving a special key sequence, store it until we have all
1287 // the bytes and we can decide what to do with it.
1288 if (buflen == 1 && buf[0] == K_SPECIAL)
1289 continue;
1290 if (buflen == 2)
1291 continue;
1292 if (buflen == 3 && buf[1] == KS_EXTRA
1293 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1294 {
1295 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1296 // recording.
1297 buflen = 0;
1298 continue;
1299 }
1300
Bram Moolenaar30613902019-12-01 22:11:18 +01001301 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001302 for (i = 0; i < buflen; ++i)
1303 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001305 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001307 buf[buflen] = NUL;
1308 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001309 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001310 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001312 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 may_sync_undo();
1315
1316#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001317 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 debug_did_msg = FALSE;
1319#endif
1320
Bram Moolenaar30613902019-12-01 22:11:18 +01001321 // Since characters have been typed, consider the following to be in
1322 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 ++maptick;
1324}
1325
1326/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001327 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1328 * character back into the typeahead buffer, thus gotchars() will be called
1329 * again.
1330 * Only affects recorded characters.
1331 */
1332 void
1333ungetchars(int len)
1334{
1335 if (reg_recording != 0)
1336 {
1337 delete_buff_tail(&recordbuff, len);
1338 last_recorded_len -= len;
1339 }
1340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Sync undo. Called when typed characters are obtained from the typeahead
1344 * buffer, or when a menu is used.
1345 * Do not sync:
1346 * - In Insert mode, unless cursor key has been used.
1347 * - While reading a script file.
1348 * - When no_u_sync is non-zero.
1349 */
1350 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001351may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001354 && scriptin[curscript] == NULL)
1355 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
1357
1358/*
1359 * Make "typebuf" empty and allocate new buffers.
1360 * Returns FAIL when out of memory.
1361 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001362 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001363alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 typebuf.tb_buf = alloc(TYPELEN_INIT);
1366 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1367 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1368 {
1369 free_typebuf();
1370 return FAIL;
1371 }
1372 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001373 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 typebuf.tb_len = 0;
1375 typebuf.tb_maplen = 0;
1376 typebuf.tb_silent = 0;
1377 typebuf.tb_no_abbr_cnt = 0;
1378 if (++typebuf.tb_change_cnt == 0)
1379 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001380#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1381 typebuf_was_filled = FALSE;
1382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 return OK;
1384}
1385
1386/*
1387 * Free the buffers of "typebuf".
1388 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001389 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001390free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001392 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001393 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001394 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001395 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001396 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001397 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001398 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001399 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400}
1401
1402/*
1403 * When doing ":so! file", the current typeahead needs to be saved, and
1404 * restored when "file" has been read completely.
1405 */
1406static typebuf_T saved_typebuf[NSCRIPT];
1407
1408 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001409save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 init_typebuf();
1412 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001413 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (alloc_typebuf() == FAIL)
1415 {
1416 closescript();
1417 return FAIL;
1418 }
1419 return OK;
1420}
1421
Bram Moolenaar30613902019-12-01 22:11:18 +01001422static int old_char = -1; // character put back by vungetc()
1423static int old_mod_mask; // mod_mask for ungotten character
1424static int old_mouse_row; // mouse_row related to old_char
1425static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427/*
1428 * Save all three kinds of typeahead, so that the user must type at a prompt.
1429 */
1430 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001431save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 tp->save_typebuf = typebuf;
1434 tp->typebuf_valid = (alloc_typebuf() == OK);
1435 if (!tp->typebuf_valid)
1436 typebuf = tp->save_typebuf;
1437
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001438 tp->old_char = old_char;
1439 tp->old_mod_mask = old_mod_mask;
1440 old_char = -1;
1441
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001442 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001443 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001444 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001445 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef USE_INPUT_BUF
1447 tp->save_inputbuf = get_input_buf();
1448# endif
1449}
1450
1451/*
1452 * Restore the typeahead to what it was before calling save_typeahead().
1453 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001454 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001457restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (tp->typebuf_valid)
1460 {
1461 free_typebuf();
1462 typebuf = tp->save_typebuf;
1463 }
1464
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001465 old_char = tp->old_char;
1466 old_mod_mask = tp->old_mod_mask;
1467
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001468 free_buff(&readbuf1);
1469 readbuf1 = tp->save_readbuf1;
1470 free_buff(&readbuf2);
1471 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001473 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474# endif
1475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477/*
1478 * Open a new script file for the ":source!" command.
1479 */
1480 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001481openscript(
1482 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001483 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 if (curscript + 1 == NSCRIPT)
1486 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001487 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return;
1489 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001490
1491 // Disallow sourcing a file in the sandbox, the commands would be executed
1492 // later, possibly outside of the sandbox.
1493 if (check_secure())
1494 return;
1495
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001496#ifdef FEAT_EVAL
1497 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001498 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001499 return;
1500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaar30613902019-12-01 22:11:18 +01001502 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001504 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 expand_env(name, NameBuff, MAXPATHL);
1506 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1507 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001508 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (curscript)
1510 --curscript;
1511 return;
1512 }
1513 if (save_typebuf() == FAIL)
1514 return;
1515
1516 /*
1517 * Execute the commands from the file right now when using ":source!"
1518 * after ":global" or ":argdo" or in a loop. Also when another command
1519 * follows. This means the display won't be updated. Don't do this
1520 * always, "make test" would fail.
1521 */
1522 if (directly)
1523 {
1524 oparg_T oa;
1525 int oldcurscript;
1526 int save_State = State;
1527 int save_restart_edit = restart_edit;
1528 int save_insertmode = p_im;
1529 int save_finish_op = finish_op;
1530 int save_msg_scroll = msg_scroll;
1531
1532 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001533 msg_scroll = FALSE; // no msg scrolling in Normal mode
1534 restart_edit = 0; // don't go to Insert mode
1535 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 clear_oparg(&oa);
1537 finish_op = FALSE;
1538
1539 oldcurscript = curscript;
1540 do
1541 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001542 update_topline_cursor(); // update cursor position and topline
1543 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001544 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 while (scriptin[oldcurscript] != NULL);
1547
1548 State = save_State;
1549 msg_scroll = save_msg_scroll;
1550 restart_edit = save_restart_edit;
1551 p_im = save_insertmode;
1552 finish_op = save_finish_op;
1553 }
1554}
1555
1556/*
1557 * Close the currently active input script.
1558 */
1559 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001560closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 free_typebuf();
1563 typebuf = saved_typebuf[curscript];
1564
1565 fclose(scriptin[curscript]);
1566 scriptin[curscript] = NULL;
1567 if (curscript > 0)
1568 --curscript;
1569}
1570
Bram Moolenaarea408852005-06-25 22:49:46 +00001571#if defined(EXITFREE) || defined(PROTO)
1572 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001573close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001574{
1575 while (scriptin[0] != NULL)
1576 closescript();
1577}
1578#endif
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580/*
1581 * Return TRUE when reading keys from a script file.
1582 */
1583 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001584using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 return scriptin[curscript] != NULL;
1587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001590 * This function is called just before doing a blocking wait. Thus after
1591 * waiting 'updatetime' for a character to arrive.
1592 */
1593 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001594before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001595{
1596 updatescript(0);
1597#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001598 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001599 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001600#endif
1601}
1602
1603/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001604 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * or when we have waited some time for a character (c == 0)
1606 *
1607 * All the changed memfiles are synced if c == 0 or when the number of typed
1608 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1609 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001610 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001611updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 static int count = 0;
1614
1615 if (c && scriptout)
1616 putc(c, scriptout);
1617 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1618 {
1619 ml_sync_all(c == 0, TRUE);
1620 count = 0;
1621 }
1622}
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001625 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001626 * character.
1627 */
1628 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001629merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001630{
1631 int c = c_arg;
1632
Bram Moolenaar975a8802020-06-06 22:36:24 +02001633 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001634 {
1635 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001636 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001637 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001638 // CTRL-6 is equivalent to CTRL-^
1639 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001640#ifdef FEAT_GUI_GTK
1641 // These mappings look arbitrary at the first glance, but in fact
1642 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1643 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1644 // more sense and is consistent with usual terminal behaviour).
1645 else if (c == '2')
1646 c = NUL;
1647 else if (c >= '3' && c <= '7')
1648 c = c ^ 0x28;
1649 else if (c == '8')
1650 c = BS;
1651 else if (c == '?')
1652 c = DEL;
1653#endif
1654 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001655 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001656 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001657 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001658 && c >= 0 && c <= 127)
1659 {
1660 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001661 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001662 }
1663 return c;
1664}
1665
1666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 * Get the next input character.
1668 * Can return a special key or a multi-byte character.
1669 * Can return NUL when called recursively, use safe_vgetc() if that's not
1670 * wanted.
1671 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1672 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001673 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 */
1675 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001676vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677{
1678 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001680 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001683#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001684 // Do garbage collection when garbagecollect() was called previously and
1685 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001686 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001687 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001688#endif
1689
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 /*
1691 * If a character was put back with vungetc, it was already processed.
1692 * Return it directly.
1693 */
1694 if (old_char != -1)
1695 {
1696 c = old_char;
1697 old_char = -1;
1698 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001699 mouse_row = old_mouse_row;
1700 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001704 mod_mask = 0;
1705 vgetc_mod_mask = 0;
1706 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001707 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001708
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001709 for (;;) // this is done twice if there are modifiers
1710 {
1711 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001712
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001713 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001714#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001715 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001716#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001717#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001718 || popup_no_mapping()
1719#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001720 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001722 // no mapping after modifier has been read
1723 ++no_mapping;
1724 ++allow_keys;
1725 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01001727 c = vgetorpeek(VGOP_ADVANCE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001728 if (did_inc)
1729 {
1730 --no_mapping;
1731 --allow_keys;
1732 }
1733
1734 // Get two extra bytes for special keys
1735 if (c == K_SPECIAL
1736#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001737 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001738#endif
1739 )
1740 {
1741 int save_allow_keys = allow_keys;
1742
1743 ++no_mapping;
1744 allow_keys = 0; // make sure BS is not found
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01001745 c2 = vgetorpeek(VGOP_ADVANCE); // no mapping for these chars
1746 c = vgetorpeek(VGOP_ADVANCE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001747 --no_mapping;
1748 allow_keys = save_allow_keys;
1749 if (c2 == KS_MODIFIER)
1750 {
1751 mod_mask = c;
1752 continue;
1753 }
1754 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar4f974752019-02-17 17:44:42 +01001756#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001757 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1758 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001759 if (
1760# ifdef VIMDLL
1761 gui.in_use &&
1762# endif
1763 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001765 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001766 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001767
1768 // get menu path, it ends with a <CR>
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01001769 for (j = 0; (c = vgetorpeek(VGOP_ADVANCE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001770 {
K.Takata54119102022-02-03 13:33:03 +00001771 name[j] = c;
1772 if (j < 199)
1773 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001774 }
K.Takata54119102022-02-03 13:33:03 +00001775 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001776 gui_make_tearoff(name);
1777 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001780#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001781 // GTK: <F10> normally selects the menu, but it's passed until
1782 // here to allow mapping it. Intercept and invoke the GTK
1783 // behavior if it's not mapped.
1784 if (c == K_F10 && gui.menubar != NULL)
1785 {
1786 gtk_menu_shell_select_first(
1787 GTK_MENU_SHELL(gui.menubar), FALSE);
1788 continue;
1789 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001792 // Handle focus event here, so that the caller doesn't need to
1793 // know about it. Return K_IGNORE so that we loop once (needed
1794 // if 'lazyredraw' is set).
1795 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001796 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001797 ui_focus_change(c == K_FOCUSGAINED);
1798 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001799 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001800
1801 // Translate K_CSI to CSI. The special key is only used to
1802 // avoid it being recognized as the start of a special key.
1803 if (c == K_CSI)
1804 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001806 }
1807 // a keypad or special function key was not mapped, use it like
1808 // its ASCII equivalent
1809 switch (c)
1810 {
1811 case K_KPLUS: c = '+'; break;
1812 case K_KMINUS: c = '-'; break;
1813 case K_KDIVIDE: c = '/'; break;
1814 case K_KMULTIPLY: c = '*'; break;
1815 case K_KENTER: c = CAR; break;
1816 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001817#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001818 // Can be either '.' or a ',',
1819 // depending on the type of keypad.
1820 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001821#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001822 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001823#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001824 case K_K0: c = '0'; break;
1825 case K_K1: c = '1'; break;
1826 case K_K2: c = '2'; break;
1827 case K_K3: c = '3'; break;
1828 case K_K4: c = '4'; break;
1829 case K_K5: c = '5'; break;
1830 case K_K6: c = '6'; break;
1831 case K_K7: c = '7'; break;
1832 case K_K8: c = '8'; break;
1833 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001834
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001835 case K_XHOME:
1836 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001837 {
1838 c = K_S_HOME;
1839 mod_mask = 0;
1840 }
1841 else if (mod_mask == MOD_MASK_CTRL)
1842 {
1843 c = K_C_HOME;
1844 mod_mask = 0;
1845 }
1846 else
1847 c = K_HOME;
1848 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001849 case K_XEND:
1850 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001851 {
1852 c = K_S_END;
1853 mod_mask = 0;
1854 }
1855 else if (mod_mask == MOD_MASK_CTRL)
1856 {
1857 c = K_C_END;
1858 mod_mask = 0;
1859 }
1860 else
1861 c = K_END;
1862 break;
1863
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001864 case K_XUP: c = K_UP; break;
1865 case K_XDOWN: c = K_DOWN; break;
1866 case K_XLEFT: c = K_LEFT; break;
1867 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001870 // For a multi-byte character get all the bytes and return the
1871 // converted character.
1872 // Note: This will loop until enough bytes are received!
1873 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1874 {
1875 ++no_mapping;
1876 buf[0] = c;
1877 for (i = 1; i < n; ++i)
1878 {
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01001879 buf[i] = vgetorpeek(VGOP_ADVANCE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001880 if (buf[i] == K_SPECIAL
1881#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001882 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001883#endif
1884 )
1885 {
1886 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1887 // sequence, which represents a K_SPECIAL (0x80),
1888 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1889 // represents a CSI (0x9B),
1890 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1891 // too.
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01001892 c = vgetorpeek(VGOP_ADVANCE);
1893 if (vgetorpeek(VGOP_ADVANCE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001894 buf[i] = CSI;
1895 }
1896 }
1897 --no_mapping;
1898 c = (*mb_ptr2char)(buf);
1899 }
1900
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001901 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001902 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001903 vgetc_mod_mask = mod_mask;
1904 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001905 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001906
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001907 break;
1908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001910
1911#ifdef FEAT_EVAL
1912 /*
1913 * In the main loop "may_garbage_collect" can be set to do garbage
1914 * collection in the first next vgetc(). It's disabled after that to
1915 * avoid internally used Lists and Dicts to be freed.
1916 */
1917 may_garbage_collect = FALSE;
1918#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001919
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001920#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001921 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001922 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001923 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001924 bevalexpr_due_set = FALSE;
1925 ui_remove_balloon();
1926 }
1927#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001928#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001929 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1930 // are filtered.
1931 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001932 {
1933 if (c == Ctrl_C)
1934 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001935 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001936 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001937#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001938
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001939 // Need to process the character before we know it's safe to do something
1940 // else.
1941 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001942 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001943
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001944 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946
1947/*
1948 * Like vgetc(), but never return a NUL when called recursively, get a key
1949 * directly from the user (ignoring typeahead).
1950 */
1951 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001952safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 int c;
1955
1956 c = vgetc();
1957 if (c == NUL)
1958 c = get_keystroke();
1959 return c;
1960}
1961
1962/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001963 * Like safe_vgetc(), but loop to handle K_IGNORE.
1964 * Also ignore scrollbar events.
1965 */
1966 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001967plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001968{
1969 int c;
1970
1971 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001972 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001973 while (c == K_IGNORE
1974 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1975 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001976
1977 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001978 // Only handle the first pasted character. Drop the rest, since we
1979 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001980 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1981
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001982 return c;
1983}
1984
1985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 * Check if a character is available, such that vgetc() will not block.
1987 * If the next character is a special character or multi-byte, the returned
1988 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001989 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
1991 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001992vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 if (old_char != -1)
1995 return old_char;
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01001996 return vgetorpeek(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997}
1998
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001999#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000/*
2001 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2002 * codes.
2003 */
2004 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002005vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 int c;
2008
2009 ++no_mapping;
2010 ++allow_keys;
2011 c = vpeekc();
2012 --no_mapping;
2013 --allow_keys;
2014 return c;
2015}
2016#endif
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018/*
2019 * Check if any character is available, also half an escape sequence.
2020 * Trick: when no typeahead found, but there is something in the typeahead
2021 * buffer, it must be an ESC that is recognized as the start of a key code.
2022 */
2023 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002024vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 int c;
2027
2028 c = vpeekc();
2029 if (c == NUL && typebuf.tb_len > 0)
2030 c = ESC;
2031 return c;
2032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034/*
2035 * Call vpeekc() without causing anything to be mapped.
2036 * Return TRUE if a character is available, FALSE otherwise.
2037 */
2038 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002039char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
2041 int retval;
2042
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002043#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002044 // When test_override("char_avail", 1) was called pretend there is no
2045 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002046 if (disable_char_avail_for_testing)
2047 return FALSE;
2048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 ++no_mapping;
2050 retval = vpeekc();
2051 --no_mapping;
2052 return (retval != NUL);
2053}
2054
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002055#if defined(FEAT_EVAL) || defined(PROTO)
2056/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002057 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002058 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002059 static void
2060getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002061{
2062 varnumber_T n;
2063 int error = FALSE;
2064
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002065 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2066 return;
2067
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002068#ifdef MESSAGE_QUEUE
2069 // vpeekc() used to check for messages, but that caused problems, invoking
2070 // a callback where it was not expected. Some plugins use getchar(1) in a
2071 // loop to await a message, therefore make sure we check for messages here.
2072 parse_queued_messages();
2073#endif
2074
Bram Moolenaar30613902019-12-01 22:11:18 +01002075 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002076 windgoto(msg_row, msg_col);
2077
2078 ++no_mapping;
2079 ++allow_keys;
2080 for (;;)
2081 {
2082 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002083 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002084 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002085 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002086 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002087 n = vpeekc_any();
2088 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002089 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002090 n = 0;
2091 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002092 // getchar(0) and char avail() != NUL: get a character.
2093 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2094 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002095
Bram Moolenaar15183b42020-09-05 19:59:39 +02002096 if (n == K_IGNORE || n == K_MOUSEMOVE
2097 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002098 continue;
2099 break;
2100 }
2101 --no_mapping;
2102 --allow_keys;
2103
2104 set_vim_var_nr(VV_MOUSE_WIN, 0);
2105 set_vim_var_nr(VV_MOUSE_WINID, 0);
2106 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2107 set_vim_var_nr(VV_MOUSE_COL, 0);
2108
2109 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002110 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002111 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002112 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002113 int i = 0;
2114
Bram Moolenaar30613902019-12-01 22:11:18 +01002115 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002116 if (mod_mask != 0)
2117 {
2118 temp[i++] = K_SPECIAL;
2119 temp[i++] = KS_MODIFIER;
2120 temp[i++] = mod_mask;
2121 }
2122 if (IS_SPECIAL(n))
2123 {
2124 temp[i++] = K_SPECIAL;
2125 temp[i++] = K_SECOND(n);
2126 temp[i++] = K_THIRD(n);
2127 }
2128 else if (has_mbyte)
2129 i += (*mb_char2bytes)(n, temp + i);
2130 else
2131 temp[i++] = n;
2132 temp[i++] = NUL;
2133 rettv->v_type = VAR_STRING;
2134 rettv->vval.v_string = vim_strsave(temp);
2135
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002136 if (is_mouse_key(n))
2137 {
2138 int row = mouse_row;
2139 int col = mouse_col;
2140 win_T *win;
2141 linenr_T lnum;
2142 win_T *wp;
2143 int winnr = 1;
2144
2145 if (row >= 0 && col >= 0)
2146 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002147 // Find the window at the mouse coordinates and compute the
2148 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002149 win = mouse_find_win(&row, &col, FIND_POPUP);
2150 if (win == NULL)
2151 return;
2152 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002153#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002154 if (WIN_IS_POPUP(win))
2155 winnr = 0;
2156 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002157#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002158 for (wp = firstwin; wp != win && wp != NULL;
2159 wp = wp->w_next)
2160 ++winnr;
2161 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2162 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2163 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2164 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2165 }
2166 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002167 }
2168}
2169
2170/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002171 * "getchar()" function
2172 */
2173 void
2174f_getchar(typval_T *argvars, typval_T *rettv)
2175{
2176 getchar_common(argvars, rettv);
2177}
2178
2179/*
2180 * "getcharstr()" function
2181 */
2182 void
2183f_getcharstr(typval_T *argvars, typval_T *rettv)
2184{
2185 getchar_common(argvars, rettv);
2186
2187 if (rettv->v_type == VAR_NUMBER)
2188 {
2189 char_u temp[7]; // mbyte-char: 6, NUL: 1
2190 varnumber_T n = rettv->vval.v_number;
2191 int i = 0;
2192
2193 if (n != 0)
2194 {
2195 if (has_mbyte)
2196 i += (*mb_char2bytes)(n, temp + i);
2197 else
2198 temp[i++] = n;
2199 }
2200 temp[i++] = NUL;
2201 rettv->v_type = VAR_STRING;
2202 rettv->vval.v_string = vim_strsave(temp);
2203 }
2204}
2205
2206/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002207 * "getcharmod()" function
2208 */
2209 void
2210f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2211{
2212 rettv->vval.v_number = mod_mask;
2213}
2214#endif // FEAT_EVAL
2215
2216#if defined(MESSAGE_QUEUE) || defined(PROTO)
2217# define MAX_REPEAT_PARSE 8
2218
2219/*
2220 * Process messages that have been queued for netbeans or clientserver.
2221 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002222 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002223 * it is safe to do so.
2224 */
2225 void
2226parse_queued_messages(void)
2227{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002228 int old_curwin_id;
2229 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002230 int i;
2231 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002232 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002233 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002234
2235 // Do not handle messages while redrawing, because it may cause buffers to
2236 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002237 // Also bail out when parsing messages was explicitly disabled.
2238 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002239 return;
2240
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002241 // If memory allocation fails during startup we'll exit but curbuf or
2242 // curwin could be NULL.
2243 if (curbuf == NULL || curwin == NULL)
2244 return;
2245
2246 old_curbuf_fnum = curbuf->b_fnum;
2247 old_curwin_id = curwin->w_id;
2248
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002249 ++entered;
2250
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002251 // may_garbage_collect is set in main_loop() to do garbage collection when
2252 // blocking to wait on a character. We don't want that while parsing
2253 // messages, a callback may invoke vgetc() while lists and dicts are in use
2254 // in the call stack.
2255 may_garbage_collect = FALSE;
2256
2257 // Loop when a job ended, but don't keep looping forever.
2258 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2259 {
2260 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002261# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002262 channel_handle_events(FALSE);
2263# endif
2264
2265# ifdef FEAT_NETBEANS_INTG
2266 // Process the queued netbeans messages.
2267 netbeans_parse_messages();
2268# endif
2269# ifdef FEAT_JOB_CHANNEL
2270 // Write any buffer lines still to be written.
2271 channel_write_any_lines();
2272
2273 // Process the messages queued on channels.
2274 channel_parse_messages();
2275# endif
2276# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2277 // Process the queued clientserver messages.
2278 server_parse_messages();
2279# endif
2280# ifdef FEAT_JOB_CHANNEL
2281 // Check if any jobs have ended. If so, repeat the above to handle
2282 // changes, e.g. stdin may have been closed.
2283 if (job_check_ended())
2284 continue;
2285# endif
2286# ifdef FEAT_TERMINAL
2287 free_unused_terminals();
2288# endif
2289# ifdef FEAT_SOUND_CANBERRA
2290 if (has_sound_callback_in_queue())
2291 invoke_sound_callback();
2292# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002293#ifdef SIGUSR1
2294 if (got_sigusr1)
2295 {
2296 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2297 got_sigusr1 = FALSE;
2298 }
2299#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002300 break;
2301 }
2302
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002303 // When not nested we'll go back to waiting for a typed character. If it
2304 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002305 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002306 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002307
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002308 may_garbage_collect = save_may_garbage_collect;
2309
2310 // If the current window or buffer changed we need to bail out of the
2311 // waiting loop. E.g. when a job exit callback closes the terminal window.
2312 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002313 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002314
2315 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002316}
2317#endif
2318
2319
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002320typedef enum {
2321 map_result_fail, // failed, break loop
2322 map_result_get, // get a character from typeahead
2323 map_result_retry, // try to map again
2324 map_result_nomatch // no matching mapping, get char
2325} map_result_T;
2326
2327/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002328 * Check if the bytes at the start of the typeahead buffer are a character used
2329 * in CTRL-X mode. This includes the form with a CTRL modifier.
2330 */
2331 static int
2332at_ctrl_x_key(void)
2333{
2334 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2335 int c = *p;
2336
2337 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002338 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002339 && p[1] == KS_MODIFIER
2340 && (p[2] & MOD_MASK_CTRL))
2341 c = p[3] & 0x1f;
2342 return vim_is_ctrl_x_key(c);
2343}
2344
2345/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002346 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002347 * into just a key, apply that.
2348 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2349 * + "max_offset"].
2350 * Return the length of the replaced bytes, zero if nothing changed.
2351 */
2352 static int
2353check_simplify_modifier(int max_offset)
2354{
2355 int offset;
2356 char_u *tp;
2357
2358 for (offset = 0; offset < max_offset; ++offset)
2359 {
2360 if (offset + 3 >= typebuf.tb_len)
2361 break;
2362 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2363 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2364 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002365 // A modifier was not used for a mapping, apply it to ASCII keys.
2366 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002367 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002368 int c = tp[3];
2369 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002370
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002371 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002372 {
2373 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002374 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002375
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002376 if (offset == 0)
2377 {
2378 // At the start: remember the character and mod_mask before
2379 // merging, in some cases, e.g. at the hit-return prompt,
2380 // they are put back in the typeahead buffer.
2381 vgetc_char = c;
2382 vgetc_mod_mask = tp[2];
2383 }
2384 len = mb_char2bytes(new_c, new_string);
2385 if (modifier == 0)
2386 {
2387 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002388 NULL, 0, 0) == FAIL)
2389 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002390 }
2391 else
2392 {
2393 tp[2] = modifier;
2394 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2395 NULL, 0, 0) == FAIL)
2396 return -1;
2397 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002398 return len;
2399 }
2400 }
2401 }
2402 return 0;
2403}
2404
2405/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002406 * Handle mappings in the typeahead buffer.
2407 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002408 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002409 * - When there is no match yet, return map_result_nomatch, need to get more
2410 * typeahead.
2411 */
2412 static int
2413handle_mapping(
2414 int *keylenp,
2415 int *timedout,
2416 int *mapdepth)
2417{
2418 mapblock_T *mp = NULL;
2419 mapblock_T *mp2;
2420 mapblock_T *mp_match;
2421 int mp_match_len = 0;
2422 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002423 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002424 int tb_c1;
2425 int mlen;
2426#ifdef FEAT_LANGMAP
2427 int nolmaplen;
2428#endif
2429 int keylen = *keylenp;
2430 int i;
2431 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002432 int is_plug_map = FALSE;
2433
2434 // If typehead starts with <Plug> then remap, even for a "noremap" mapping.
2435 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2436 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2437 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2438 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002439
2440 /*
2441 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002442 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002443 *
2444 * Don't look for mappings if:
2445 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2446 * - maphash_valid not set: no mappings present.
2447 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2448 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002449 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002450 * - waiting for a char with --more--
2451 * - in Ctrl-X mode, and we get a valid char for that mode
2452 */
2453 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2454 if (no_mapping == 0 && is_maphash_valid()
2455 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002456 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002457 || (p_remap
2458 && (typebuf.tb_noremap[typebuf.tb_off]
2459 & (RM_NONE|RM_ABBR)) == 0))
2460 && !(p_paste && (State & (INSERT + CMDLINE)))
2461 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2462 && State != ASKMORE
2463 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002464 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002465 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002466 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002467 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002468#ifdef FEAT_GUI
2469 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2470 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2471 {
2472 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2473 // K_SPECIAL KS_MODIFIER {flags}.
2474 tb_c1 = K_SPECIAL;
2475 }
2476#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002477#ifdef FEAT_LANGMAP
2478 if (tb_c1 == K_SPECIAL)
2479 nolmaplen = 2;
2480 else
2481 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002482 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2483 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002484 nolmaplen = 0;
2485 }
2486#endif
2487 // First try buffer-local mappings.
2488 mp = get_buf_maphash_list(local_State, tb_c1);
2489 mp2 = get_maphash_list(local_State, tb_c1);
2490 if (mp == NULL)
2491 {
2492 // There are no buffer-local mappings.
2493 mp = mp2;
2494 mp2 = NULL;
2495 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002496
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002497 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002498 * Loop until a partly matching mapping is found or all (local)
2499 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002500 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002501 * A full match is only accepted if there is no partly match, so "aa"
2502 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002503 */
2504 mp_match = NULL;
2505 mp_match_len = 0;
2506 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002507 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002508 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002509 // Only consider an entry if the first character matches and it is
2510 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002511 // Skip ":lmap" mappings if keys were mapped.
2512 if (mp->m_keys[0] == tb_c1
2513 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002514 && !(mp->m_simplified && seenModifyOtherKeys
2515 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002516 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002517 {
2518#ifdef FEAT_LANGMAP
2519 int nomap = nolmaplen;
2520 int c2;
2521#endif
2522 // find the match length of this mapping
2523 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2524 {
2525#ifdef FEAT_LANGMAP
2526 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2527 if (nomap > 0)
2528 --nomap;
2529 else if (c2 == K_SPECIAL)
2530 nomap = 2;
2531 else
2532 LANGMAP_ADJUST(c2, TRUE);
2533 if (mp->m_keys[mlen] != c2)
2534#else
2535 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002536 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002537#endif
2538 break;
2539 }
2540
Bram Moolenaareda35f72019-08-03 14:59:44 +02002541 // Don't allow mapping the first byte(s) of a multi-byte char.
2542 // Happens when mapping <M-a> and then changing 'encoding'.
2543 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002544 {
2545 char_u *p1 = mp->m_keys;
2546 char_u *p2 = mb_unescape(&p1);
2547
2548 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002549 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002550 mlen = 0;
2551 }
2552
2553 // Check an entry whether it matches.
2554 // - Full match: mlen == keylen
2555 // - Partly match: mlen == typebuf.tb_len
2556 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002557 if (mlen == keylen || (mlen == typebuf.tb_len
2558 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002559 {
2560 char_u *s;
2561 int n;
2562
Bram Moolenaareda35f72019-08-03 14:59:44 +02002563 // If only script-local mappings are allowed, check if the
2564 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002565 s = typebuf.tb_noremap + typebuf.tb_off;
2566 if (*s == RM_SCRIPT
2567 && (mp->m_keys[0] != K_SPECIAL
2568 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002569 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002570 continue;
2571
Bram Moolenaareda35f72019-08-03 14:59:44 +02002572 // If one of the typed keys cannot be remapped, skip the
2573 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 for (n = mlen; --n >= 0; )
2575 if (*s++ & (RM_NONE|RM_ABBR))
2576 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002577 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002578 continue;
2579
2580 if (keylen > typebuf.tb_len)
2581 {
2582 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002583 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002584 {
2585 // break at a partly match
2586 keylen = KEYLEN_PART_MAP;
2587 break;
2588 }
2589 }
2590 else if (keylen > mp_match_len)
2591 {
2592 // found a longer match
2593 mp_match = mp;
2594 mp_match_len = keylen;
2595 }
2596 }
2597 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002598 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002599 // character. If the first character that didn't match is
2600 // K_SPECIAL then check for a termcode. This isn't perfect
2601 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002602 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002603 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002604 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002605 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2606 }
2607 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2608 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609 }
2610 }
2611
Bram Moolenaareda35f72019-08-03 14:59:44 +02002612 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002613 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002614 {
2615 mp = mp_match;
2616 keylen = mp_match_len;
2617 }
2618 }
2619
2620 /*
2621 * Check for match with 'pastetoggle'
2622 */
2623 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2624 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002625 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2626 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002627 break;
2628 if (p_pt[mlen] == NUL) // match
2629 {
2630 // write chars to script file(s)
2631 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002632 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2633 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002634
2635 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002636 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002637 if (!(State & INSERT))
2638 {
2639 msg_col = 0;
2640 msg_row = Rows - 1;
2641 msg_clr_eos(); // clear ruler
2642 }
2643 status_redraw_all();
2644 redraw_statuslines();
2645 showmode();
2646 setcursor();
2647 *keylenp = keylen;
2648 return map_result_retry;
2649 }
2650 // Need more chars for partly match.
2651 if (mlen == typebuf.tb_len)
2652 keylen = KEYLEN_PART_KEY;
2653 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002654 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002655 max_mlen = mlen + 1;
2656 }
2657
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002658 // May check for a terminal code when there is no mapping or only a partial
2659 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2660 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002661 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002662 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2663 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002664 {
2665 int save_keylen = keylen;
2666
2667 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002668 * When no matching mapping found or found a non-matching mapping that
2669 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002670 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002671 * - mapping is allowed,
2672 * - keys have not been mapped,
2673 * - and not an ESC sequence, not in insert mode or p_ek is on,
2674 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002675 */
2676 if ((no_mapping == 0 || allow_keys != 0)
2677 && (typebuf.tb_maplen == 0
2678 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002679 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002680 && !*timedout)
2681 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002682 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002683
Bram Moolenaar0684e362020-12-03 19:54:42 +01002684 // If no termcode matched but 'pastetoggle' matched partially
2685 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002686 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2687 keylen = KEYLEN_PART_KEY;
2688
Bram Moolenaar975a8802020-06-06 22:36:24 +02002689 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002690 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002691 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002692 keylen = check_simplify_modifier(max_mlen + 1);
2693
Bram Moolenaareda35f72019-08-03 14:59:44 +02002694 // When getting a partial match, but the last characters were not
2695 // typed, don't wait for a typed character to complete the
2696 // termcode. This helps a lot when a ":normal" command ends in an
2697 // ESC.
2698 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002699 keylen = 0;
2700 }
2701 else
2702 keylen = 0;
2703 if (keylen == 0) // no matching terminal code
2704 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002705#ifdef AMIGA
2706 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002707 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002708 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002709 {
2710 char_u *s;
2711
2712 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002713 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2714 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002715 ++s)
2716 ;
2717 if (*s == 'r' || *s == '|') // found one
2718 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002719 del_typebuf(
2720 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002721 // get size and redraw screen
2722 shell_resized();
2723 *keylenp = keylen;
2724 return map_result_retry;
2725 }
2726 if (*s == NUL) // need more characters
2727 keylen = KEYLEN_PART_KEY;
2728 }
2729 if (keylen >= 0)
2730#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002731 // When there was a matching mapping and no termcode could be
2732 // replaced after another one, use that mapping (loop around).
2733 // If there was no mapping at all use the character from the
2734 // typeahead buffer right here.
2735 if (mp == NULL)
2736 {
2737 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002738 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002739 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002740 }
2741
2742 if (keylen > 0) // full matching terminal code
2743 {
2744#if defined(FEAT_GUI) && defined(FEAT_MENU)
2745 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002746 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2747 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002748 {
2749 int idx;
2750
Bram Moolenaareda35f72019-08-03 14:59:44 +02002751 // Using a menu may cause a break in undo! It's like using
2752 // gotchars(), but without recording or writing to a script
2753 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002754 may_sync_undo();
2755 del_typebuf(3, 0);
2756 idx = get_menu_index(current_menu, local_State);
2757 if (idx != MENU_INDEX_INVALID)
2758 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002759 // In Select mode and a Visual mode menu is used: Switch
2760 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 // back to Select mode.
2762 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002763 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002764 {
2765 VIsual_select = FALSE;
2766 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002768 }
2769 ins_typebuf(current_menu->strings[idx],
2770 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002771 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002772 }
2773 }
2774#endif // FEAT_GUI && FEAT_MENU
2775 *keylenp = keylen;
2776 return map_result_retry; // try mapping again
2777 }
2778
Bram Moolenaareda35f72019-08-03 14:59:44 +02002779 // Partial match: get some more characters. When a matching mapping
2780 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002781 if (mp == NULL || keylen < 0)
2782 keylen = KEYLEN_PART_KEY;
2783 else
2784 keylen = mp_match_len;
2785 }
2786
2787 /*
2788 * complete match
2789 */
2790 if (keylen >= 0 && keylen <= typebuf.tb_len)
2791 {
2792 char_u *map_str;
2793
2794#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002795 int save_m_expr;
2796 int save_m_noremap;
2797 int save_m_silent;
2798 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002799#else
2800# define save_m_noremap mp->m_noremap
2801# define save_m_silent mp->m_silent
2802#endif
2803
2804 // write chars to script file(s)
2805 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002806 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2807 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002808
2809 cmd_silent = (typebuf.tb_silent > 0);
2810 del_typebuf(keylen, 0); // remove the mapped keys
2811
2812 /*
2813 * Put the replacement string in front of mapstr.
2814 * The depth check catches ":map x y" and ":map y x".
2815 */
2816 if (++*mapdepth >= p_mmd)
2817 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002818 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002819 if (State & CMDLINE)
2820 redrawcmdline();
2821 else
2822 setcursor();
2823 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002824 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 *keylenp = keylen;
2826 return map_result_fail;
2827 }
2828
2829 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002830 * In Select mode and a Visual mode mapping is used: Switch to Visual
2831 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002832 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002833 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002834 {
2835 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002836 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002837 }
2838
2839#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002840 // Copy the values from *mp that are used, because evaluating the
2841 // expression may invoke a function that redefines the mapping, thereby
2842 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002843 save_m_expr = mp->m_expr;
2844 save_m_noremap = mp->m_noremap;
2845 save_m_silent = mp->m_silent;
2846 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002847
2848 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002849 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2850 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002851 */
2852 if (mp->m_expr)
2853 {
2854 int save_vgetc_busy = vgetc_busy;
2855 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002856 int was_screen_col = screen_cur_col;
2857 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002858 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859
2860 vgetc_busy = 0;
2861 may_garbage_collect = FALSE;
2862
2863 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002864 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002866 // The mapping may do anything, but we expect it to take care of
2867 // redrawing. Do put the cursor back where it was.
2868 windgoto(was_screen_row, was_screen_col);
2869 out_flush();
2870
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002871 // If an error was displayed and the expression returns an empty
2872 // string, generate a <Nop> to allow for a redraw.
2873 if (prev_did_emsg != did_emsg
2874 && (map_str == NULL || *map_str == NUL))
2875 {
2876 char_u buf[4];
2877
2878 vim_free(map_str);
2879 buf[0] = K_SPECIAL;
2880 buf[1] = KS_EXTRA;
2881 buf[2] = KE_IGNORE;
2882 buf[3] = NUL;
2883 map_str = vim_strsave(buf);
2884 if (State & CMDLINE)
2885 {
2886 // redraw the command below the error
2887 msg_didout = TRUE;
2888 if (msg_row < cmdline_row)
2889 msg_row = cmdline_row;
2890 redrawcmd();
2891 }
2892 }
2893
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002894 vgetc_busy = save_vgetc_busy;
2895 may_garbage_collect = save_may_garbage_collect;
2896 }
2897 else
2898#endif
2899 map_str = mp->m_str;
2900
2901 /*
2902 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002903 * If 'from' field is the same as the start of the 'to' field, don't
2904 * remap the first character (but do allow abbreviations).
2905 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002906 */
2907 if (map_str == NULL)
2908 i = FAIL;
2909 else
2910 {
2911 int noremap;
2912
2913 if (save_m_noremap != REMAP_YES)
2914 noremap = save_m_noremap;
2915 else if (
2916#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002917 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2918 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002919#else
2920 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2921#endif
2922 != 0)
2923 noremap = REMAP_YES;
2924 else
2925 noremap = REMAP_SKIP;
2926 i = ins_typebuf(map_str, noremap,
2927 0, TRUE, cmd_silent || save_m_silent);
2928#ifdef FEAT_EVAL
2929 if (save_m_expr)
2930 vim_free(map_str);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002931 last_used_map = mp;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002932#endif
2933 }
2934#ifdef FEAT_EVAL
2935 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002936#endif
2937 *keylenp = keylen;
2938 if (i == FAIL)
2939 return map_result_fail;
2940 return map_result_retry;
2941 }
2942
2943 *keylenp = keylen;
2944 return map_result_nomatch;
2945}
2946
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002947/*
2948 * unget one character (can only be done once!)
2949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002951vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 old_char = c;
2954 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002955 old_mouse_row = mouse_row;
2956 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957}
2958
2959/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002960 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 * 1. from the stuffbuffer
2962 * This is used for abbreviated commands like "D" -> "d$".
2963 * Also used to redo a command for ".".
2964 * 2. from the typeahead buffer
2965 * Stores text obtained previously but not used yet.
2966 * Also stores the result of mappings.
2967 * Also used for the ":normal" command.
2968 * 3. from the user
2969 * This may do a blocking wait if "advance" is TRUE.
2970 *
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01002971 * if "flags & VGOP_ADVANCE" is non-zero (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002972 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 * KeyTyped is set to TRUE in the case the user typed the key.
2974 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01002975 * if "flags & VGOP_ADVANCE" is zero (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002976 * Just look whether there is a character available.
2977 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 *
2979 * When "no_mapping" is zero, checks for mappings in the current mode.
2980 * Only returns one byte (of a multi-byte character).
2981 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2982 */
2983 static int
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01002984vgetorpeek(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01002986 int advance = flags & VGOP_ADVANCE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002988 int timedout = FALSE; // waited for more than 1 second
2989 // for mapping to complete
2990 int mapdepth = 0; // check for recursive mapping
2991 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002992#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 int new_wcol, new_wrow;
2994#endif
2995#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002996 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#endif
2998 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003000 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 /*
3003 * This function doesn't work very well when called recursively. This may
3004 * happen though, because of:
3005 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3006 * there is a character available, which calls this function. In that
3007 * case we must return NUL, to indicate no character is available.
3008 * 2. A GUI callback function writes to the screen, causing a
3009 * wait_return().
3010 * Using ":normal" can also do this, but it saves the typeahead buffer,
3011 * thus it should be OK. But don't get a key from the user then.
3012 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003013 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 return NUL;
3015
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003016 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 if (advance)
3019 KeyStuffed = FALSE;
3020
3021 init_typebuf();
3022 start_stuff();
3023 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02003024 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 do
3026 {
3027/*
3028 * get a character: 1. from the stuffbuffer
3029 */
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01003030 if (flags & VGOP_NO_STUFF)
3031 c = 0;
3032 else if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 c = typeahead_char;
3035 if (advance)
3036 typeahead_char = 0;
3037 }
3038 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003039 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (c != NUL && !got_int)
3041 {
3042 if (advance)
3043 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003044 // KeyTyped = FALSE; When the command that stuffed something
3045 // was typed, behave like the stuffed command was typed.
3046 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 KeyStuffed = TRUE;
3048 }
3049 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003050 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052 else
3053 {
3054 /*
3055 * Loop until we either find a matching mapped key, or we
3056 * are sure that it is not a mapped key.
3057 * If a mapped key sequence is found we go back to the start to
3058 * try re-mapping.
3059 */
3060 for (;;)
3061 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003062 long wait_time;
3063 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003064#ifdef FEAT_CMDL_INFO
3065 int showcmd_idx;
3066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 /*
3068 * ui_breakcheck() is slow, don't use it too often when
3069 * inside a mapping. But call it each time for typed
3070 * characters.
3071 */
3072 if (typebuf.tb_maplen)
3073 line_breakcheck();
3074 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003075 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (got_int)
3077 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003078 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003079 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 /*
3082 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003083 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Otherwise we behave like having gotten a CTRL-C.
3085 * As a result typing CTRL-C in insert mode will
3086 * really insert a CTRL-C.
3087 */
3088 if ((c || typebuf.tb_maplen)
3089 && (State & (INSERT + CMDLINE)))
3090 c = ESC;
3091 else
3092 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003093 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003095 if (advance)
3096 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003097 // Also record this character, it might be needed to
3098 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003099 *typebuf.tb_buf = c;
3100 gotchars(typebuf.tb_buf, 1);
3101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 cmd_silent = FALSE;
3103
3104 break;
3105 }
3106 else if (typebuf.tb_len > 0)
3107 {
3108 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003109 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003111 map_result_T result = handle_mapping(
3112 &keylen, &timedout, &mapdepth);
3113
3114 if (result == map_result_retry)
3115 // try mapping again
3116 continue;
3117 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003118 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003119 // failed, use the outer loop
3120 c = -1;
3121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003123 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125/*
3126 * get a character: 2. from the typeahead buffer
3127 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003128 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003129 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003130 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003131 cmd_silent = (typebuf.tb_silent > 0);
3132 if (typebuf.tb_maplen > 0)
3133 KeyTyped = FALSE;
3134 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003135 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003136 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003137 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003138 gotchars(typebuf.tb_buf
3139 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003140 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003141 KeyNoremap = typebuf.tb_noremap[
3142 typebuf.tb_off];
3143 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003144 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003145 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003148 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
3150
3151/*
3152 * get a character: 3. from the user - handle <Esc> in Insert mode
3153 */
3154 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003155 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 * are no more characters at once, we pretend to go out of
3157 * insert mode. This prevents the one second delay after
3158 * typing an <ESC>. If we get something after all, we may
3159 * have to redisplay the mode. That the cursor is in the wrong
3160 * place does not matter.
3161 */
3162 c = 0;
3163#ifdef FEAT_CMDL_INFO
3164 new_wcol = curwin->w_wcol;
3165 new_wrow = curwin->w_wrow;
3166#endif
3167 if ( advance
3168 && typebuf.tb_len == 1
3169 && typebuf.tb_buf[typebuf.tb_off] == ESC
3170 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 && typebuf.tb_maplen == 0
3173 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003174 && (p_timeout
3175 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003177 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 colnr_T col = 0, vcol;
3180 char_u *ptr;
3181
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003182 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 unshowmode(TRUE);
3185 mode_deleted = TRUE;
3186 }
3187#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003188 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003189 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 int save_State;
3192
3193 save_State = State;
3194 State = NORMAL;
3195 gui_update_cursor(TRUE, FALSE);
3196 State = save_State;
3197 shape_changed = TRUE;
3198 }
3199#endif
3200 validate_cursor();
3201 old_wcol = curwin->w_wcol;
3202 old_wrow = curwin->w_wrow;
3203
Bram Moolenaar30613902019-12-01 22:11:18 +01003204 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (curwin->w_cursor.col != 0)
3206 {
3207 if (curwin->w_wcol > 0)
3208 {
3209 if (did_ai)
3210 {
3211 /*
3212 * We are expecting to truncate the trailing
3213 * white-space, so find the last non-white
3214 * character -- webb
3215 */
3216 col = vcol = curwin->w_wcol = 0;
3217 ptr = ml_get_curline();
3218 while (col < curwin->w_cursor.col)
3219 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003220 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003222 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003223 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003225 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 ++col;
3228 }
3229 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003230 + curwin->w_wcol / curwin->w_width;
3231 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003233 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
3235 else
3236 {
3237 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240 }
3241 else if (curwin->w_p_wrap && curwin->w_wrow)
3242 {
3243 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003244 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3248 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003249 // Correct when the cursor is on the right halve
3250 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 ptr = ml_get_curline();
3252 col -= (*mb_head_off)(ptr, ptr + col);
3253 if ((*mb_ptr2cells)(ptr + col) > 1)
3254 --curwin->w_wcol;
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 setcursor();
3258 out_flush();
3259#ifdef FEAT_CMDL_INFO
3260 new_wcol = curwin->w_wcol;
3261 new_wrow = curwin->w_wrow;
3262#endif
3263 curwin->w_wcol = old_wcol;
3264 curwin->w_wrow = old_wrow;
3265 }
3266 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003267 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003268
Bram Moolenaar30613902019-12-01 22:11:18 +01003269 // Allow mapping for just typed characters. When we get here c
3270 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003271 for (n = 1; n <= c; ++n)
3272 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 typebuf.tb_len += c;
3274
Bram Moolenaar30613902019-12-01 22:11:18 +01003275 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3277 {
3278 timedout = TRUE;
3279 continue;
3280 }
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (ex_normal_busy > 0)
3283 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003284#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar30613902019-12-01 22:11:18 +01003288 // No typeahead left and inside ":normal". Must return
3289 // something to avoid getting stuck. When an incomplete
3290 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (typebuf.tb_len > 0)
3292 {
3293 timedout = TRUE;
3294 continue;
3295 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003296
Bram Moolenaar30613902019-12-01 22:11:18 +01003297 // When 'insertmode' is set, ESC just beeps in Insert
3298 // mode. Use CTRL-L to make edit() return.
3299 // For the command line only CTRL-C always breaks it.
3300 // For the cmdline window: Alternate between ESC and
3301 // CTRL-C: ESC for most situations and CTRL-C to close the
3302 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 if (p_im && (State & INSERT))
3304 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003305#ifdef FEAT_TERMINAL
3306 else if (terminal_is_active())
3307 c = K_CANCEL;
3308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003310#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 )
3314 c = Ctrl_C;
3315 else
3316 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003317#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003319#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003320 // return from main_loop()
3321 if (pending_exmode_active)
3322 exmode_active = EXMODE_NORMAL;
3323
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003324 // no chars to block abbreviation for
3325 typebuf.tb_no_abbr_cnt = 0;
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 break;
3328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
3330/*
3331 * get a character: 3. from the user - update display
3332 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003333 // In insert mode a screen update is skipped when characters
3334 // are still available. But when those available characters
3335 // are part of a mapping, and we are going to do a blocking
3336 // wait here. Need to update the screen to display the
3337 // changed text so far. Also for when 'lazyredraw' is set and
3338 // redrawing was postponed because there was something in the
3339 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003340 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3341 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003344 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
3347 /*
3348 * If we have a partial match (and are going to wait for more
3349 * input from the user), show the partially matched characters
3350 * to the user with showcmd.
3351 */
3352#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003353 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355 c1 = 0;
3356 if (typebuf.tb_len > 0 && advance && !exmode_active)
3357 {
3358 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3359 && State != HITRETURN)
3360 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003361 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (State & INSERT
3363 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3364 + typebuf.tb_len - 1) == 1)
3365 {
3366 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3367 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003368 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 c1 = 1;
3370 }
3371#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003372 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 old_wcol = curwin->w_wcol;
3374 old_wrow = curwin->w_wrow;
3375 curwin->w_wcol = new_wcol;
3376 curwin->w_wrow = new_wrow;
3377 push_showcmd();
3378 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003379 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3380 while (showcmd_idx < typebuf.tb_len)
3381 (void)add_to_showcmd(
3382 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 curwin->w_wcol = old_wcol;
3384 curwin->w_wrow = old_wrow;
3385#endif
3386 }
3387
Bram Moolenaar30613902019-12-01 22:11:18 +01003388 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 if ((State & CMDLINE)
3390#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3391 && cmdline_star == 0
3392#endif
3393 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3394 + typebuf.tb_len - 1) == 1)
3395 {
3396 putcmdline(typebuf.tb_buf[typebuf.tb_off
3397 + typebuf.tb_len - 1], FALSE);
3398 c1 = 1;
3399 }
3400 }
3401
3402/*
3403 * get a character: 3. from the user - get it
3404 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003405 if (typebuf.tb_len == 0)
3406 // timedout may have been set while waiting for a mapping
3407 // that has a <Nop> RHS.
3408 timedout = FALSE;
3409
Bram Moolenaar652de232019-04-04 20:13:09 +02003410 if (advance)
3411 {
3412 if (typebuf.tb_len == 0
3413 || !(p_timeout
3414 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3415 // blocking wait
3416 wait_time = -1L;
3417 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3418 wait_time = p_ttm;
3419 else
3420 wait_time = p_tm;
3421 }
3422 else
3423 wait_time = 0;
3424
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003425 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3427 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003428 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003431 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 pop_showcmd();
3433#endif
3434 if (c1 == 1)
3435 {
3436 if (State & INSERT)
3437 edit_unputchar();
3438 if (State & CMDLINE)
3439 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003440 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003441 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443
3444 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003445 continue; // end of input script reached
3446 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 if (!advance)
3449 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003450 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 timedout = TRUE;
3453 continue;
3454 }
3455 }
3456 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003457 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while (typebuf.tb_buf[typebuf.tb_off
3459 + typebuf.tb_len] != NUL)
3460 typebuf.tb_noremap[typebuf.tb_off
3461 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003462#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003463 // Get IM status right after getting keys, not after the
3464 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 vgetc_im_active = im_get_status();
3466#endif
3467 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003468 } // for (;;)
3469 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar30613902019-12-01 22:11:18 +01003471 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003472 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474 /*
3475 * The "INSERT" message is taken care of here:
3476 * if we return an ESC to exit insert mode, the message is deleted
3477 * if we don't return an ESC but deleted the message before, redisplay it
3478 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003479 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003481 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003484 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 else
3486 unshowmode(FALSE);
3487 }
3488 else if (c != ESC && mode_deleted)
3489 {
3490 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003491 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 else
3493 showmode();
3494 }
3495 }
3496#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003497 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003498 if (gui.in_use && shape_changed)
3499 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003501 if (timedout && c == ESC)
3502 {
3503 char_u nop_buf[3];
3504
3505 // When recording there will be no timeout. Add a <Nop> after the ESC
3506 // to avoid that it forms a key code with following characters.
3507 nop_buf[0] = K_SPECIAL;
3508 nop_buf[1] = KS_EXTRA;
3509 nop_buf[2] = KE_NOP;
3510 gotchars(nop_buf, 3);
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003513 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 return c;
3516}
3517
3518/*
3519 * inchar() - get one character from
3520 * 1. a scriptfile
3521 * 2. the keyboard
3522 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003523 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 * NUL terminated (buffer length must be 'maxlen' + 1).
3525 * Minimum for "maxlen" is 3!!!!
3526 *
3527 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3528 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3529 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3530 * otherwise.
3531 *
3532 * If we got an interrupt all input is read until none is available.
3533 *
3534 * If wait_time == 0 there is no waiting for the char.
3535 * If wait_time == n we wait for n msec for a character to arrive.
3536 * If wait_time == -1 we wait forever for a character to arrive.
3537 *
3538 * Return the number of obtained characters.
3539 * Return -1 when end of input script reached.
3540 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003541 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003542inchar(
3543 char_u *buf,
3544 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003545 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaar30613902019-12-01 22:11:18 +01003547 int len = 0; // init for GCC
3548 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003550 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
Bram Moolenaar30613902019-12-01 22:11:18 +01003552 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
3554 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003555 out_flush_cursor(FALSE, FALSE);
3556#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3557 if (gui.in_use && postponed_mouseshape)
3558 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#endif
3560 }
3561
3562 /*
3563 * Don't reset these when at the hit-return prompt, otherwise a endless
3564 * recursive loop may result (write error in swapfile, hit-return, timeout
3565 * on char wait, flush swapfile, write error....).
3566 */
3567 if (State != HITRETURN)
3568 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003569 did_outofmem_msg = FALSE; // display out of memory message (again)
3570 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003572 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
3574 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003575 * Get a character from a script file if there is one.
3576 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 */
3578 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003579 while (scriptin[curscript] != NULL && script_char < 0
3580#ifdef FEAT_EVAL
3581 && !ignore_script
3582#endif
3583 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003585#ifdef MESSAGE_QUEUE
3586 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003587#endif
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3590 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003591 // Reached EOF.
3592 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3593 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 closescript();
3595 /*
3596 * When reading script file is interrupted, return an ESC to get
3597 * back to normal mode.
3598 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3599 */
3600 if (got_int)
3601 retesc = TRUE;
3602 else
3603 return -1;
3604 }
3605 else
3606 {
3607 buf[0] = script_char;
3608 len = 1;
3609 }
3610 }
3611
Bram Moolenaar30613902019-12-01 22:11:18 +01003612 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 /*
3615 * If we got an interrupt, skip all previously typed characters and
3616 * return TRUE if quit reading script file.
3617 * Stop reading typeahead when a single CTRL-C was read,
3618 * fill_input_buf() returns this when not able to read from stdin.
3619 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3620 * and buf may be pointing inside typebuf.tb_buf[].
3621 */
3622 if (got_int)
3623 {
kylo252ae6f1d82022-02-16 19:24:07 +00003624#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 char_u dum[DUM_LEN + 1];
3626
3627 for (;;)
3628 {
3629 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3630 if (len == 0 || (len == 1 && dum[0] == 3))
3631 break;
3632 }
3633 return retesc;
3634 }
3635
3636 /*
3637 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003638 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003640 if (wait_time == -1L || wait_time > 10L)
3641 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 /*
3644 * Fill up to a third of the buffer, because each character may be
3645 * tripled below.
3646 */
3647 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3648 }
3649
Bram Moolenaar30613902019-12-01 22:11:18 +01003650 // If the typebuf was changed further down, it is like nothing was added by
3651 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (typebuf_changed(tb_change_cnt))
3653 return 0;
3654
Bram Moolenaar30613902019-12-01 22:11:18 +01003655 // Note the change in the typeahead buffer, this matters for when
3656 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3657 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003658 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3659 typebuf.tb_change_cnt = 1;
3660
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003661 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662}
3663
3664/*
3665 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003666 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 * Returns the new length.
3668 */
3669 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003670fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 int i;
3673 char_u *p = buf;
3674
3675 /*
3676 * Two characters are special: NUL and K_SPECIAL.
3677 * When compiled With the GUI CSI is also special.
3678 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3679 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3680 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 */
3682 for (i = len; --i >= 0; ++p)
3683 {
3684#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003685 // When the GUI is used any character can come after a CSI, don't
3686 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (gui.in_use && p[0] == CSI && i >= 2)
3688 {
3689 p += 2;
3690 i -= 2;
3691 }
zeertzjq646bb722022-02-16 17:51:47 +00003692# ifndef MSWIN
3693 // When not on MS-Windows and the GUI is not used CSI needs to be
3694 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 else if (!gui.in_use && p[0] == CSI)
3696 {
3697 mch_memmove(p + 3, p + 1, (size_t)i);
3698 *p++ = K_SPECIAL;
3699 *p++ = KS_EXTRA;
3700 *p = (int)KE_CSI;
3701 len += 2;
3702 }
zeertzjq646bb722022-02-16 17:51:47 +00003703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 else
3705#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003706 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003707 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003708 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003709#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003710 // Win32 console passes modifiers
3711 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003712# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003713 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003714# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003715 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716#endif
3717 ))
3718 {
3719 mch_memmove(p + 3, p + 1, (size_t)i);
3720 p[2] = K_THIRD(p[0]);
3721 p[1] = K_SECOND(p[0]);
3722 p[0] = K_SPECIAL;
3723 p += 2;
3724 len += 2;
3725 }
3726 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003727 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 return len;
3729}
3730
3731#if defined(USE_INPUT_BUF) || defined(PROTO)
3732/*
3733 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3734 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003735 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003736 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 */
3738 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003739input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
3741 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003742# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3743 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744# endif
3745 );
3746}
3747#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003748
3749/*
3750 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3751 * typeahead.
3752 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003753 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003754getcmdkeycmd(
3755 int promptc UNUSED,
3756 void *cookie UNUSED,
3757 int indent UNUSED,
3758 getline_opt_T do_concat UNUSED)
3759{
3760 garray_T line_ga;
3761 int c1 = -1;
3762 int c2;
3763 int cmod = 0;
3764 int aborted = FALSE;
3765
3766 ga_init2(&line_ga, 1, 32);
3767
3768 // no mapping for these characters
3769 no_mapping++;
3770
3771 got_int = FALSE;
3772 while (c1 != NUL && !aborted)
3773 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003774 if (ga_grow(&line_ga, 32) != OK)
3775 {
3776 aborted = TRUE;
3777 break;
3778 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003779
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01003780 if (vgetorpeek(0 | VGOP_NO_STUFF) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003781 {
3782 // incomplete <Cmd> is an error, because there is not much the user
3783 // could do in this state.
3784 emsg(_(e_cmd_mapping_must_end_with_cr));
3785 aborted = TRUE;
3786 break;
3787 }
3788
3789 // Get one character at a time.
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01003790 c1 = vgetorpeek(VGOP_ADVANCE | VGOP_NO_STUFF);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003791
3792 // Get two extra bytes for special keys
3793 if (c1 == K_SPECIAL)
3794 {
Bram Moolenaard0fb2d82022-04-04 21:03:52 +01003795 c1 = vgetorpeek(VGOP_ADVANCE | VGOP_NO_STUFF);
3796 c2 = vgetorpeek(VGOP_ADVANCE | VGOP_NO_STUFF);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003797 if (c1 == KS_MODIFIER)
3798 {
3799 cmod = c2;
3800 continue;
3801 }
3802 c1 = TO_SPECIAL(c1, c2);
3803 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003804 if (c1 == Ctrl_V)
3805 {
3806 // CTRL-V is followed by octal, hex or other characters, reverses
3807 // what AppendToRedobuffLit() does.
3808 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003809 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003810 no_reduce_keys = FALSE;
3811 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003812
3813 if (got_int)
3814 aborted = TRUE;
3815 else if (c1 == '\r' || c1 == '\n')
3816 c1 = NUL; // end the line
3817 else if (c1 == ESC)
3818 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003819 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003820 {
3821 // give a nicer error message for this special case
3822 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3823 aborted = TRUE;
3824 }
3825 else if (IS_SPECIAL(c1))
3826 {
3827 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003828 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003829 else
3830 {
3831 semsg(e_cmd_maping_must_not_include_str_key,
3832 get_special_key_name(c1, cmod));
3833 aborted = TRUE;
3834 }
3835 }
3836 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003837 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003838
3839 cmod = 0;
3840 }
3841
3842 no_mapping--;
3843
3844 if (aborted)
3845 ga_clear(&line_ga);
3846
3847 return (char_u *)line_ga.ga_data;
3848}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003849
3850 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003851do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003852{
3853 int res;
3854#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003855 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003856
3857 if (key == K_SCRIPT_COMMAND && last_used_map != NULL)
3858 {
3859 save_current_sctx = current_sctx;
3860 current_sctx = last_used_map->m_script_ctx;
3861 }
3862#endif
3863
3864 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3865
3866#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003867 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003868 current_sctx = save_current_sctx;
3869#endif
3870
3871 return res;
3872}
3873
3874#if defined(FEAT_EVAL) || defined(PROTO)
3875 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003876reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003877{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003878 if (last_used_map == mp)
3879 last_used_map = NULL;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003880}
3881#endif