blob: 9d54377cc137cf9828f09fe9b6b0b67b7b196039 [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/*
zeertzjq30b6d612023-05-07 17:39:23 +010046 * When block_redo is TRUE the redo buffer will not be changed.
47 * Used by edit() to repeat insertions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 */
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
Mike Williamsaca8f552024-04-07 18:26:22 +020084static size_t 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;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010088int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000089#endif
90
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020094static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020096static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020098static int inchar(char_u *buf, int maxlen, long wait_time);
Shougo Matsushita83678842024-07-11 22:05:12 +020099#ifdef FEAT_EVAL
100static int do_key_input_pre(int c);
101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
103/*
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 Moolenaarf39d9e92023-04-22 22:54:40 +0100139 if ((count > 0 || 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 }
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100147 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148}
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);
Mike Williamsaca8f552024-04-07 18:26:22 +0200169 if (len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 {
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 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100220 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);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000265 if (len < slen)
266 return;
267
268 buf->bh_curr->b_str[len - slen] = NUL;
269 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000270}
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{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000484 if (block_redo)
485 return;
486
487 free_buff(&old_redobuff);
488 old_redobuff = redobuff;
489 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490}
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{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000499 if (block_redo)
500 return;
501
502 free_buff(&redobuff);
503 redobuff = old_redobuff;
504 old_redobuff.bh_first.b_next = NULL;
505 start_stuff();
506 while (read_readbuffers(TRUE) != NUL)
507 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100508}
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);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000526 if (s == NULL)
527 return;
528
529 add_buff(&redobuff, s, -1L);
530 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
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/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100609 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
610 * and escaping other K_SPECIAL and CSI bytes.
611 */
612 void
613AppendToRedobuffSpec(char_u *s)
614{
zeertzjq30b6d612023-05-07 17:39:23 +0100615 if (block_redo)
616 return;
617
zeertzjq3ab3a862023-05-06 16:22:04 +0100618 while (*s != NUL)
619 {
620 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
621 {
zeertzjq30b6d612023-05-07 17:39:23 +0100622 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100623 add_buff(&redobuff, s, 3L);
624 s += 3;
625 }
626 else
627 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
628 }
629}
630
631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 * Append a character to the redo buffer.
633 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
634 */
635 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100636AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 if (!block_redo)
639 add_char_buff(&redobuff, c);
640}
641
642/*
643 * Append a number to the redo buffer.
644 */
645 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100646AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 if (!block_redo)
649 add_num_buff(&redobuff, n);
650}
651
652/*
653 * Append string "s" to the stuff buffer.
654 * CSI and K_SPECIAL must already have been escaped.
655 */
656 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100657stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100659 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200662/*
663 * Append string "s" to the redo stuff buffer.
664 * CSI and K_SPECIAL must already have been escaped.
665 */
666 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100667stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200668{
669 add_buff(&readbuf2, s, -1L);
670}
671
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200672 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100673stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100675 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676}
677
678#if defined(FEAT_EVAL) || defined(PROTO)
679/*
680 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
681 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200682 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 */
684 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100685stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200687 int c;
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 while (*s != NUL)
690 {
691 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
692 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100693 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 stuffReadbuffLen(s, 3L);
695 s += 3;
696 }
697 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200698 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100699 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200700 if (c == CAR || c == NL || c == ESC)
701 c = ' ';
702 stuffcharReadbuff(c);
703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
705}
706#endif
707
708/*
709 * Append a character to the stuff buffer.
710 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
711 */
712 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100713stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100715 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716}
717
718/*
719 * Append a number to the stuff buffer.
720 */
721 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100722stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100724 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200728 * Stuff a string into the typeahead buffer, such that edit() will insert it
729 * literally ("literally" TRUE) or interpret is as typed characters.
730 */
731 void
732stuffescaped(char_u *arg, int literally)
733{
734 int c;
735 char_u *start;
736
737 while (*arg != NUL)
738 {
739 // Stuff a sequence of normal ASCII characters, that's fast. Also
740 // stuff K_SPECIAL to get the effect of a special key when "literally"
741 // is TRUE.
742 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000743 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200744 || (*arg == K_SPECIAL && !literally))
745 ++arg;
746 if (arg > start)
747 stuffReadbuffLen(start, (long)(arg - start));
748
749 // stuff a single special character
750 if (*arg != NUL)
751 {
752 if (has_mbyte)
753 c = mb_cptr2char_adv(&arg);
754 else
755 c = *arg++;
756 if (literally && ((c < ' ' && c != TAB) || c == DEL))
757 stuffcharReadbuff(Ctrl_V);
758 stuffcharReadbuff(c);
759 }
760 }
761}
762
763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
765 * multibyte characters.
766 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100767 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
768 * otherwise.
769 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 */
771 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100772read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100774 static buffblock_T *bp;
775 static char_u *p;
776 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100777 int n;
778 char_u buf[MB_MAXBYTES + 1];
779 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 if (init)
782 {
783 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200784 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200786 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (bp == NULL)
788 return FAIL;
789 p = bp->b_str;
790 return OK;
791 }
792 if ((c = *p) != NUL)
793 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100794 // Reverse the conversion done by add_char_buff()
795 // For a multi-byte character get all the bytes and return the
796 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
798 n = MB_BYTE2LEN_CHECK(c);
799 else
800 n = 1;
801 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100803 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 c = TO_SPECIAL(p[1], p[2]);
806 p += 2;
807 }
808#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100809 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 p += 2;
811#endif
812 if (*++p == NUL && bp->b_next != NULL)
813 {
814 bp = bp->b_next;
815 p = bp->b_str;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100818 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 if (n != 1)
821 c = (*mb_ptr2char)(buf);
822 break;
823 }
824 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100825 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828 }
829
830 return c;
831}
832
833/*
834 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
835 * If old_redo is TRUE, use old_redobuff instead of redobuff.
836 * The escaped K_SPECIAL and CSI are copied without translation.
837 */
838 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100839copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 int c;
842
843 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100844 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
847/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100848 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * Insert the redo count into the command.
850 * If "old_redo" is TRUE, the last but one command is repeated
851 * instead of the last command (inserting text). This is used for
852 * CTRL-O <.> in insert mode
853 *
854 * return FAIL for failure, OK otherwise
855 */
856 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100857start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
859 int c;
860
Bram Moolenaar30613902019-12-01 22:11:18 +0100861 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (read_redo(TRUE, old_redo) == FAIL)
863 return FAIL;
864
865 c = read_redo(FALSE, old_redo);
866
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100867#ifdef FEAT_EVAL
868 if (c == K_SID)
869 {
870 // Copy the <SID>{sid}; sequence
871 add_char_buff(&readbuf2, c);
872 for (;;)
873 {
874 c = read_redo(FALSE, old_redo);
875 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100876 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100877 break;
878 }
879 c = read_redo(FALSE, old_redo);
880 }
881#endif
882
Bram Moolenaar30613902019-12-01 22:11:18 +0100883 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (c == '"')
885 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100886 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 c = read_redo(FALSE, old_redo);
888
Bram Moolenaar30613902019-12-01 22:11:18 +0100889 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (c >= '1' && c < '9')
891 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100892 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200893
Bram Moolenaar30613902019-12-01 22:11:18 +0100894 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200895 if (c == '=')
896 {
897 add_char_buff(&readbuf2, CAR);
898 cmd_silent = TRUE;
899 }
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 c = read_redo(FALSE, old_redo);
902 }
903
Bram Moolenaar30613902019-12-01 22:11:18 +0100904 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906 VIsual = curwin->w_cursor;
907 VIsual_active = TRUE;
908 VIsual_select = FALSE;
909 VIsual_reselect = TRUE;
910 redo_VIsual_busy = TRUE;
911 c = read_redo(FALSE, old_redo);
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaar30613902019-12-01 22:11:18 +0100914 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (count)
916 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100917 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100919 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100922 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100923 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 copy_redo(old_redo);
925 return OK;
926}
927
928/*
929 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100930 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * return FAIL for failure, OK otherwise
932 */
933 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100934start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 int c;
937
938 if (read_redo(TRUE, FALSE) == FAIL)
939 return FAIL;
940 start_stuff();
941
Bram Moolenaar30613902019-12-01 22:11:18 +0100942 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 while ((c = read_redo(FALSE, FALSE)) != NUL)
944 {
945 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
946 {
947 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100948 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 break;
950 }
951 }
952
Bram Moolenaar30613902019-12-01 22:11:18 +0100953 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 copy_redo(FALSE);
955 block_redo = TRUE;
956 return OK;
957}
958
959 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100960stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 block_redo = FALSE;
963}
964
965/*
966 * Initialize typebuf.tb_buf to point to typebuf_init.
967 * alloc() cannot be used here: In out-of-memory situations it would
968 * be impossible to type anything.
969 */
970 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100971init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000973 if (typebuf.tb_buf != NULL)
974 return;
975
976 typebuf.tb_buf = typebuf_init;
977 typebuf.tb_noremap = noremapbuf_init;
978 typebuf.tb_buflen = TYPELEN_INIT;
979 typebuf.tb_len = 0;
980 typebuf.tb_off = MAXMAPLEN + 4;
981 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200985 * Returns TRUE when keys cannot be remapped.
986 */
987 int
988noremap_keys(void)
989{
990 return KeyNoremap & (RM_NONE|RM_SCRIPT);
991}
992
993/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
995 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000997 * If "noremap" is REMAP_YES, new string can be mapped again.
998 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
999 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001000 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001001 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001003 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001005 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1006 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001008 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 *
1010 * return FAIL for failure, OK otherwise
1011 */
1012 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001013ins_typebuf(
1014 char_u *str,
1015 int noremap,
1016 int offset,
1017 int nottyped,
1018 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
1020 char_u *s1, *s2;
1021 int newlen;
1022 int addlen;
1023 int i;
1024 int newoff;
1025 int val;
1026 int nrm;
1027
1028 init_typebuf();
1029 if (++typebuf.tb_change_cnt == 0)
1030 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001031 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (offset == 0 && addlen <= typebuf.tb_off)
1036 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001037 /*
1038 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 typebuf.tb_off -= addlen;
1041 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1042 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001043 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1044 >= addlen + 3 * (MAXMAPLEN + 4))
1045 {
1046 /*
1047 * Buffer is empty and string fits in the existing buffer.
1048 * Leave some space before and after, if possible.
1049 */
1050 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1051 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 else
1054 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001055 int extra;
1056
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001057 /*
1058 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001059 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001060 * characters. We add some extra room to avoid having to allocate too
1061 * often.
1062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001064 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1065 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001067 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001068 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 setcursor();
1070 return FAIL;
1071 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001072 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001074 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 return FAIL;
1076 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001077 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 vim_free(s1);
1080 return FAIL;
1081 }
1082 typebuf.tb_buflen = newlen;
1083
Bram Moolenaar30613902019-12-01 22:11:18 +01001084 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1086 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001087 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001089 // copy the old chars, after the insertion point, including the NUL at
1090 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 mch_memmove(s1 + newoff + offset + addlen,
1092 typebuf.tb_buf + typebuf.tb_off + offset,
1093 (size_t)(typebuf.tb_len - offset + 1));
1094 if (typebuf.tb_buf != typebuf_init)
1095 vim_free(typebuf.tb_buf);
1096 typebuf.tb_buf = s1;
1097
1098 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1099 (size_t)offset);
1100 mch_memmove(s2 + newoff + offset + addlen,
1101 typebuf.tb_noremap + typebuf.tb_off + offset,
1102 (size_t)(typebuf.tb_len - offset));
1103 if (typebuf.tb_noremap != noremapbuf_init)
1104 vim_free(typebuf.tb_noremap);
1105 typebuf.tb_noremap = s2;
1106
1107 typebuf.tb_off = newoff;
1108 }
1109 typebuf.tb_len += addlen;
1110
Bram Moolenaar30613902019-12-01 22:11:18 +01001111 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (noremap == REMAP_SCRIPT)
1113 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001114 else if (noremap == REMAP_SKIP)
1115 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 else
1117 val = RM_NONE;
1118
1119 /*
1120 * Adjust typebuf.tb_noremap[] for the new characters:
1121 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1122 * (sometimes) not remappable
1123 * If noremap == REMAP_YES: all the new characters are mappable
1124 * If noremap > 0: "noremap" characters are not remappable, the rest
1125 * mappable
1126 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001127 if (noremap == REMAP_SKIP)
1128 nrm = 1;
1129 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 nrm = addlen;
1131 else
1132 nrm = noremap;
1133 for (i = 0; i < addlen; ++i)
1134 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1135 (--nrm >= 0) ? val : RM_YES;
1136
Bram Moolenaar30613902019-12-01 22:11:18 +01001137 // tb_maplen and tb_silent only remember the length of mapped and/or
1138 // silent mappings at the start of the buffer, assuming that a mapped
1139 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (nottyped || typebuf.tb_maplen > offset)
1141 typebuf.tb_maplen += addlen;
1142 if (silent || typebuf.tb_silent > offset)
1143 {
1144 typebuf.tb_silent += addlen;
1145 cmd_silent = TRUE;
1146 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001147 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 typebuf.tb_no_abbr_cnt += addlen;
1149
1150 return OK;
1151}
1152
1153/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001154 * Put character "c" back into the typeahead buffer.
1155 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001156 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1157 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001158 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001159 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001160 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001161ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001162{
zeertzjq6cac7702022-01-04 18:01:21 +00001163 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001164 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001165
zeertzjq2e7cba32022-06-10 15:30:32 +01001166 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001167 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001168 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001169}
1170
1171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001173 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001174 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1176 * changed it was reallocated and the old pointer can no longer be used.
1177 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1178 * that was just added.
1179 */
1180 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001181typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001182 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001185#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1186 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#endif
1188 ));
1189}
1190
1191/*
1192 * Return TRUE if there are no characters in the typeahead buffer that have
1193 * not been typed (result from a mapping or come from ":normal").
1194 */
1195 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001196typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 return typebuf.tb_maplen == 0;
1199}
1200
1201/*
1202 * Return the number of characters that are mapped (or not typed).
1203 */
1204 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001205typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 return typebuf.tb_maplen;
1208}
1209
1210/*
1211 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1212 */
1213 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001214del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 int i;
1217
1218 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001219 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
1221 typebuf.tb_len -= len;
1222
1223 /*
1224 * Easy case: Just increase typebuf.tb_off.
1225 */
1226 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1227 >= 3 * MAXMAPLEN + 3)
1228 typebuf.tb_off += len;
1229 /*
1230 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1231 */
1232 else
1233 {
1234 i = typebuf.tb_off + offset;
1235 /*
1236 * Leave some extra room at the end to avoid reallocation.
1237 */
1238 if (typebuf.tb_off > MAXMAPLEN)
1239 {
1240 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1241 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1242 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1243 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1244 typebuf.tb_off = MAXMAPLEN;
1245 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001246 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1248 typebuf.tb_buf + i + len,
1249 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001250 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1252 typebuf.tb_noremap + i + len,
1253 (size_t)(typebuf.tb_len - offset));
1254 }
1255
Bram Moolenaar30613902019-12-01 22:11:18 +01001256 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 if (typebuf.tb_maplen < offset + len)
1259 typebuf.tb_maplen = offset;
1260 else
1261 typebuf.tb_maplen -= len;
1262 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001263 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 if (typebuf.tb_silent < offset + len)
1266 typebuf.tb_silent = offset;
1267 else
1268 typebuf.tb_silent -= len;
1269 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001270 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 if (typebuf.tb_no_abbr_cnt < offset + len)
1273 typebuf.tb_no_abbr_cnt = offset;
1274 else
1275 typebuf.tb_no_abbr_cnt -= len;
1276 }
1277
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001278#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001279 // Reset the flag that text received from a client or from feedkeys()
1280 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001281 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#endif
1283 if (++typebuf.tb_change_cnt == 0)
1284 typebuf.tb_change_cnt = 1;
1285}
1286
zeertzjq094c4392024-04-18 22:09:37 +02001287/*
1288 * State for adding bytes to a recording or 'showcmd'.
1289 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001290typedef struct
1291{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001292 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001293 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001294 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001295 unsigned pending_special;
1296 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001297} gotchars_state_T;
1298
1299/*
1300 * Add a single byte to a recording or 'showcmd'.
1301 * Return TRUE if a full key has been received, FALSE otherwise.
1302 */
1303 static int
1304gotchars_add_byte(gotchars_state_T *state, char_u byte)
1305{
1306 int c = state->buf[state->buflen++] = byte;
1307 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001308 int in_special = state->pending_special > 0;
1309 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001310
zeertzjq6b13e3d2024-04-22 21:04:29 +02001311 if (in_special)
1312 state->pending_special--;
1313 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001314#ifdef FEAT_GUI
1315 || c == CSI
1316#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001317 )
1318 // When receiving a special key sequence, store it until we have all
1319 // the bytes and we can decide what to do with it.
1320 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001321
zeertzjq6b13e3d2024-04-22 21:04:29 +02001322 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001323 goto ret_false;
1324
zeertzjq6b13e3d2024-04-22 21:04:29 +02001325 if (in_mbyte)
1326 state->pending_mbyte--;
1327 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001328 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001329 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001330 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001331 if (state->prev_c == KS_MODIFIER)
1332 // When receiving a modifier, wait for the modified key.
1333 goto ret_false;
1334 c = TO_SPECIAL(state->prev_c, c);
1335 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1336 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1337 // in a recording.
1338 state->buflen = 0;
1339 }
1340 // When receiving a multibyte character, store it until we have all
1341 // the bytes, so that it won't be split between two buffer blocks,
1342 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001343 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001344 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001345
1346 if (state->pending_mbyte > 0)
1347 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001348
1349 retval = TRUE;
1350ret_false:
1351 state->prev_c = c;
1352 return retval;
1353}
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355/*
1356 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001357 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
1359 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001360gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001362 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001363 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001364 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001365 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
zeertzjqacdfb8a2024-04-17 21:28:54 +02001367 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001369 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001370 continue;
1371
Bram Moolenaar30613902019-12-01 22:11:18 +01001372 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001373 for (i = 0; i < state.buflen; ++i)
1374 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001376 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001378 state.buf[state.buflen] = NUL;
1379 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001380 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001381 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001383 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 may_sync_undo();
1387
1388#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001389 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 debug_did_msg = FALSE;
1391#endif
1392
Bram Moolenaar30613902019-12-01 22:11:18 +01001393 // Since characters have been typed, consider the following to be in
1394 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 ++maptick;
1396}
1397
1398/*
zeertzjqbf321802024-01-28 19:03:00 +01001399 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001400 */
1401 void
zeertzjqbf321802024-01-28 19:03:00 +01001402gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001403{
zeertzjqbf321802024-01-28 19:03:00 +01001404 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001405 gotchars(nop_buf, 3);
1406}
1407
1408/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001409 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1410 * character back into the typeahead buffer, thus gotchars() will be called
1411 * again.
1412 * Only affects recorded characters.
1413 */
1414 void
1415ungetchars(int len)
1416{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001417 if (reg_recording == 0)
1418 return;
1419
1420 delete_buff_tail(&recordbuff, len);
1421 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001422}
1423
1424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 * Sync undo. Called when typed characters are obtained from the typeahead
1426 * buffer, or when a menu is used.
1427 * Do not sync:
1428 * - In Insert mode, unless cursor key has been used.
1429 * - While reading a script file.
1430 * - When no_u_sync is non-zero.
1431 */
1432 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001433may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
Bram Moolenaar24959102022-05-07 20:01:16 +01001435 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001436 && scriptin[curscript] == NULL)
1437 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438}
1439
1440/*
1441 * Make "typebuf" empty and allocate new buffers.
1442 * Returns FAIL when out of memory.
1443 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001444 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001445alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 typebuf.tb_buf = alloc(TYPELEN_INIT);
1448 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1449 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1450 {
1451 free_typebuf();
1452 return FAIL;
1453 }
1454 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001455 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 typebuf.tb_len = 0;
1457 typebuf.tb_maplen = 0;
1458 typebuf.tb_silent = 0;
1459 typebuf.tb_no_abbr_cnt = 0;
1460 if (++typebuf.tb_change_cnt == 0)
1461 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001462#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1463 typebuf_was_filled = FALSE;
1464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 return OK;
1466}
1467
1468/*
1469 * Free the buffers of "typebuf".
1470 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001471 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001472free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001474 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001475 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001476 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001477 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001478 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001479 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001480 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001481 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482}
1483
1484/*
1485 * When doing ":so! file", the current typeahead needs to be saved, and
1486 * restored when "file" has been read completely.
1487 */
1488static typebuf_T saved_typebuf[NSCRIPT];
1489
1490 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001491save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 init_typebuf();
1494 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001495 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (alloc_typebuf() == FAIL)
1497 {
1498 closescript();
1499 return FAIL;
1500 }
1501 return OK;
1502}
1503
Bram Moolenaar30613902019-12-01 22:11:18 +01001504static int old_char = -1; // character put back by vungetc()
1505static int old_mod_mask; // mod_mask for ungotten character
1506static int old_mouse_row; // mouse_row related to old_char
1507static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001508static int old_KeyStuffed; // whether old_char was stuffed
1509
zeertzjq6d4e7252022-04-07 13:58:04 +01001510static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001511{
1512 // If the old character was not stuffed and characters have been added to
1513 // the stuff buffer, need to first get the stuffed characters instead.
1514 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1515}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
1518 * Save all three kinds of typeahead, so that the user must type at a prompt.
1519 */
1520 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001521save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 tp->save_typebuf = typebuf;
1524 tp->typebuf_valid = (alloc_typebuf() == OK);
1525 if (!tp->typebuf_valid)
1526 typebuf = tp->save_typebuf;
1527
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001528 tp->old_char = old_char;
1529 tp->old_mod_mask = old_mod_mask;
1530 old_char = -1;
1531
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001532 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001533 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001534 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001535 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536# ifdef USE_INPUT_BUF
1537 tp->save_inputbuf = get_input_buf();
1538# endif
1539}
1540
1541/*
1542 * Restore the typeahead to what it was before calling save_typeahead().
1543 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001544 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 */
1546 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001547restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 if (tp->typebuf_valid)
1550 {
1551 free_typebuf();
1552 typebuf = tp->save_typebuf;
1553 }
1554
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001555 old_char = tp->old_char;
1556 old_mod_mask = tp->old_mod_mask;
1557
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001558 free_buff(&readbuf1);
1559 readbuf1 = tp->save_readbuf1;
1560 free_buff(&readbuf2);
1561 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001563 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564# endif
1565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567/*
1568 * Open a new script file for the ":source!" command.
1569 */
1570 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001571openscript(
1572 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001573 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 if (curscript + 1 == NSCRIPT)
1576 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001577 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 return;
1579 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001580
1581 // Disallow sourcing a file in the sandbox, the commands would be executed
1582 // later, possibly outside of the sandbox.
1583 if (check_secure())
1584 return;
1585
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001586#ifdef FEAT_EVAL
1587 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001588 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001589 return;
1590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar30613902019-12-01 22:11:18 +01001592 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001594 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 expand_env(name, NameBuff, MAXPATHL);
1596 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1597 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001598 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (curscript)
1600 --curscript;
1601 return;
1602 }
1603 if (save_typebuf() == FAIL)
1604 return;
1605
1606 /*
1607 * Execute the commands from the file right now when using ":source!"
1608 * after ":global" or ":argdo" or in a loop. Also when another command
1609 * follows. This means the display won't be updated. Don't do this
1610 * always, "make test" would fail.
1611 */
1612 if (directly)
1613 {
1614 oparg_T oa;
1615 int oldcurscript;
1616 int save_State = State;
1617 int save_restart_edit = restart_edit;
1618 int save_insertmode = p_im;
1619 int save_finish_op = finish_op;
1620 int save_msg_scroll = msg_scroll;
1621
Bram Moolenaar24959102022-05-07 20:01:16 +01001622 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001623 msg_scroll = FALSE; // no msg scrolling in Normal mode
1624 restart_edit = 0; // don't go to Insert mode
1625 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 clear_oparg(&oa);
1627 finish_op = FALSE;
1628
1629 oldcurscript = curscript;
1630 do
1631 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001632 update_topline_cursor(); // update cursor position and topline
1633 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001634 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 while (scriptin[oldcurscript] != NULL);
1637
1638 State = save_State;
1639 msg_scroll = save_msg_scroll;
1640 restart_edit = save_restart_edit;
1641 p_im = save_insertmode;
1642 finish_op = save_finish_op;
1643 }
1644}
1645
1646/*
1647 * Close the currently active input script.
1648 */
1649 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001650closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 free_typebuf();
1653 typebuf = saved_typebuf[curscript];
1654
1655 fclose(scriptin[curscript]);
1656 scriptin[curscript] = NULL;
1657 if (curscript > 0)
1658 --curscript;
1659}
1660
Bram Moolenaarea408852005-06-25 22:49:46 +00001661#if defined(EXITFREE) || defined(PROTO)
1662 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001663close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001664{
1665 while (scriptin[0] != NULL)
1666 closescript();
1667}
1668#endif
1669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670/*
1671 * Return TRUE when reading keys from a script file.
1672 */
1673 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001674using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 return scriptin[curscript] != NULL;
1677}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001680 * This function is called just before doing a blocking wait. Thus after
1681 * waiting 'updatetime' for a character to arrive.
1682 */
1683 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001684before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001685{
1686 updatescript(0);
1687#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001688 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001689 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001690#endif
1691}
1692
1693/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001694 * updatescript() is called when a character can be written into the script
1695 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 *
1697 * All the changed memfiles are synced if c == 0 or when the number of typed
1698 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1699 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001700 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001701updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 static int count = 0;
1704
1705 if (c && scriptout)
1706 putc(c, scriptout);
1707 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1708 {
1709 ml_sync_all(c == 0, TRUE);
1710 count = 0;
1711 }
1712}
1713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001715 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1716 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001717 */
1718 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001719merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001720{
1721 int c = c_arg;
1722
Bram Moolenaarea83c192023-03-18 17:22:46 +00001723 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001724 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001725 {
1726 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001727 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001728 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001729 if (c == NUL)
1730 c = K_ZERO;
1731 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001732 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001733 // CTRL-6 is equivalent to CTRL-^
1734 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001735#ifdef FEAT_GUI_GTK
1736 // These mappings look arbitrary at the first glance, but in fact
1737 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1738 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1739 // more sense and is consistent with usual terminal behaviour).
1740 else if (c == '2')
1741 c = NUL;
1742 else if (c >= '3' && c <= '7')
1743 c = c ^ 0x28;
1744 else if (c == '8')
1745 c = BS;
1746 else if (c == '?')
1747 c = DEL;
1748#endif
1749 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001750 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001751 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001752
1753 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001754 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001755 && c >= 0 && c <= 127)
1756 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001757 // Some terminals (esp. Kitty) do not include Shift in the character.
1758 // Apply it here to get consistency across terminals. Only do ASCII
1759 // letters, for other characters it depends on the keyboard layout.
1760 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1761 {
1762 c += 'a' - 'A';
1763 *modifiers &= ~MOD_MASK_SHIFT;
1764 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001765 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001766 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001767 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001768
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001769 return c;
1770}
1771
1772/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001773 * Add a single byte to 'showcmd' for a partially matched mapping.
1774 * Call add_to_showcmd() if a full key has been received.
1775 */
1776 static void
1777add_byte_to_showcmd(char_u byte)
1778{
1779 static gotchars_state_T state;
1780 char_u *ptr;
1781 int modifiers = 0;
1782 int c = NUL;
1783
1784 if (!p_sc || msg_silent != 0)
1785 return;
1786
1787 if (!gotchars_add_byte(&state, byte))
1788 return;
1789
1790 state.buf[state.buflen] = NUL;
1791 state.buflen = 0;
1792
1793 ptr = state.buf;
1794 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1795 {
1796 modifiers = ptr[2];
1797 ptr += 3;
1798 }
1799
1800 if (*ptr != NUL)
1801 {
1802 char_u *mb_ptr = mb_unescape(&ptr);
1803
1804 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1805 if (c <= 0x7f)
1806 {
1807 // Merge modifiers into the key to make the result more readable.
1808 int modifiers_after = modifiers;
1809 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1810
1811 if (modifiers_after == 0)
1812 {
1813 modifiers = 0;
1814 c = mod_c;
1815 }
1816 }
1817 }
1818
1819 // TODO: is there a more readable and yet compact representation of
1820 // modifiers and special keys?
1821 if (modifiers != 0)
1822 {
1823 add_to_showcmd(K_SPECIAL);
1824 add_to_showcmd(KS_MODIFIER);
1825 add_to_showcmd(modifiers);
1826 }
1827 if (c != NUL)
1828 add_to_showcmd(c);
1829 while (*ptr != NUL)
1830 add_to_showcmd(*ptr++);
1831}
1832
1833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 * Get the next input character.
1835 * Can return a special key or a multi-byte character.
1836 * Can return NUL when called recursively, use safe_vgetc() if that's not
1837 * wanted.
1838 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1839 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001840 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
1842 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001843vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001847 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001850#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001851 // Do garbage collection when garbagecollect() was called previously and
1852 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001853 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001854 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001855#endif
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 /*
1858 * If a character was put back with vungetc, it was already processed.
1859 * Return it directly.
1860 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001861 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
1863 c = old_char;
1864 old_char = -1;
1865 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001866 mouse_row = old_mouse_row;
1867 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
zeertzjq81b46a62022-04-09 17:58:49 +01001871 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001872 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001873
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001874 mod_mask = 0;
1875 vgetc_mod_mask = 0;
1876 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001877
1878 // last_recorded_len can be larger than last_vgetc_recorded_len
1879 // if peeking records more
1880 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001881
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001882 for (;;) // this is done twice if there are modifiers
1883 {
1884 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001885
Bram Moolenaar78aed952022-09-24 15:36:35 +01001886 // No mapping after modifier has been read, using an input method
1887 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001888 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001889#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001890 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001891#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001892#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001893 || popup_no_mapping()
1894#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001895 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001897 ++no_mapping;
1898 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001899 // mod_mask value may change, remember we did the increment
1900 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001902 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001903 if (did_inc)
1904 {
1905 --no_mapping;
1906 --allow_keys;
1907 }
1908
Christopher Plewright03193062022-11-22 12:58:27 +00001909 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001910 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001911#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001912 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001913#endif
1914 )
1915 {
1916 int save_allow_keys = allow_keys;
1917
1918 ++no_mapping;
1919 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001920 c2 = vgetorpeek(TRUE); // no mapping for these chars
1921 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001922 --no_mapping;
1923 allow_keys = save_allow_keys;
1924 if (c2 == KS_MODIFIER)
1925 {
1926 mod_mask = c;
1927 continue;
1928 }
1929 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001931 // K_ESC is used to avoid ambiguity with the single Esc
1932 // character that might be the start of an escape sequence.
1933 // Convert it back to a single Esc here.
1934 if (c == K_ESC)
1935 c = ESC;
1936
Bram Moolenaar4f974752019-02-17 17:44:42 +01001937#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001938 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1939 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001940 if (
1941# ifdef VIMDLL
1942 gui.in_use &&
1943# endif
1944 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001946 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001947 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001948
1949 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001950 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001951 {
K.Takata54119102022-02-03 13:33:03 +00001952 name[j] = c;
1953 if (j < 199)
1954 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001955 }
K.Takata54119102022-02-03 13:33:03 +00001956 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001957 gui_make_tearoff(name);
1958 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001961#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001962 // GTK: <F10> normally selects the menu, but it's passed until
1963 // here to allow mapping it. Intercept and invoke the GTK
1964 // behavior if it's not mapped.
1965 if (c == K_F10 && gui.menubar != NULL)
1966 {
1967 gtk_menu_shell_select_first(
1968 GTK_MENU_SHELL(gui.menubar), FALSE);
1969 continue;
1970 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001973 // Handle focus event here, so that the caller doesn't need to
1974 // know about it. Return K_IGNORE so that we loop once (needed
1975 // if 'lazyredraw' is set).
1976 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001977 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001978 ui_focus_change(c == K_FOCUSGAINED);
1979 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001980 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001981
1982 // Translate K_CSI to CSI. The special key is only used to
1983 // avoid it being recognized as the start of a special key.
1984 if (c == K_CSI)
1985 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001987#ifdef FEAT_EVAL
1988 if (c == K_SID)
1989 {
1990 int j;
1991
1992 // Handle <SID>{sid}; Do up to 20 digits for safety.
1993 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01001994 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001995 last_used_sid = last_used_sid * 10 + (c - '0');
1996 last_used_map = NULL;
1997 continue;
1998 }
1999#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002000 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002001
zeertzjq90a80022024-07-13 19:06:44 +02002002 // For a multi-byte character get all the bytes and return the
2003 // converted character.
2004 // Note: This will loop until enough bytes are received!
2005 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2006 {
2007 ++no_mapping;
2008 buf[0] = c;
2009 for (i = 1; i < n; ++i)
2010 {
2011 buf[i] = vgetorpeek(TRUE);
2012 if (buf[i] == K_SPECIAL
2013#ifdef FEAT_GUI
2014 || (buf[i] == CSI)
2015#endif
2016 )
2017 {
2018 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2019 // sequence, which represents a K_SPECIAL (0x80),
2020 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2021 // represents a CSI (0x9B),
2022 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2023 // too.
2024 c = vgetorpeek(TRUE);
2025 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2026 buf[i] = CSI;
2027 }
2028 }
2029 --no_mapping;
2030 c = (*mb_ptr2char)(buf);
2031 }
2032
2033 if (vgetc_char == 0)
2034 {
2035 vgetc_mod_mask = mod_mask;
2036 vgetc_char = c;
2037 }
2038
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002039 // a keypad or special function key was not mapped, use it like
2040 // its ASCII equivalent
2041 switch (c)
2042 {
2043 case K_KPLUS: c = '+'; break;
2044 case K_KMINUS: c = '-'; break;
2045 case K_KDIVIDE: c = '/'; break;
2046 case K_KMULTIPLY: c = '*'; break;
2047 case K_KENTER: c = CAR; break;
2048 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002049#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002050 // Can be either '.' or a ',',
2051 // depending on the type of keypad.
2052 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002053#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002054 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002055#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002056 case K_K0: c = '0'; break;
2057 case K_K1: c = '1'; break;
2058 case K_K2: c = '2'; break;
2059 case K_K3: c = '3'; break;
2060 case K_K4: c = '4'; break;
2061 case K_K5: c = '5'; break;
2062 case K_K6: c = '6'; break;
2063 case K_K7: c = '7'; break;
2064 case K_K8: c = '8'; break;
2065 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002066
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002067 case K_XHOME:
2068 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002069 {
2070 c = K_S_HOME;
2071 mod_mask = 0;
2072 }
2073 else if (mod_mask == MOD_MASK_CTRL)
2074 {
2075 c = K_C_HOME;
2076 mod_mask = 0;
2077 }
2078 else
2079 c = K_HOME;
2080 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002081 case K_XEND:
2082 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002083 {
2084 c = K_S_END;
2085 mod_mask = 0;
2086 }
2087 else if (mod_mask == MOD_MASK_CTRL)
2088 {
2089 c = K_C_END;
2090 mod_mask = 0;
2091 }
2092 else
2093 c = K_END;
2094 break;
2095
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002096 case K_XUP: c = K_UP; break;
2097 case K_XDOWN: c = K_DOWN; break;
2098 case K_XLEFT: c = K_LEFT; break;
2099 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002102 break;
2103 }
zeertzjq81b46a62022-04-09 17:58:49 +01002104
2105 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002107
2108#ifdef FEAT_EVAL
2109 /*
2110 * In the main loop "may_garbage_collect" can be set to do garbage
2111 * collection in the first next vgetc(). It's disabled after that to
2112 * avoid internally used Lists and Dicts to be freed.
2113 */
2114 may_garbage_collect = FALSE;
2115#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002116
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002117#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002118 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002119 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002120 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002121 bevalexpr_due_set = FALSE;
2122 ui_remove_balloon();
2123 }
2124#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002125#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002126 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2127 // are filtered.
2128 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002129 {
2130 if (c == Ctrl_C)
2131 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002132 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002133 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002134#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002135
Shougo Matsushita83678842024-07-11 22:05:12 +02002136#ifdef FEAT_EVAL
2137 c = do_key_input_pre(c);
2138#endif
2139
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002140 // Need to process the character before we know it's safe to do something
2141 // else.
2142 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002143 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002144
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002145 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146}
2147
Shougo Matsushita83678842024-07-11 22:05:12 +02002148#ifdef FEAT_EVAL
2149/*
2150 * Handle the InsertCharPre autocommand.
2151 * "c" is the character that was typed.
2152 * Return new input character.
2153 */
2154 static int
2155do_key_input_pre(int c)
2156{
2157 int res = c;
2158 char_u buf[MB_MAXBYTES + 1];
2159 char_u curr_mode[MODE_MAX_LENGTH];
2160 int save_State = State;
2161 save_v_event_T save_v_event;
2162 dict_T *v_event;
2163
2164 // Return quickly when there is nothing to do.
2165 if (!has_keyinputpre())
2166 return res;
2167
2168 if (IS_SPECIAL(c))
2169 {
2170 buf[0] = K_SPECIAL;
2171 buf[1] = KEY2TERMCAP0(c);
2172 buf[2] = KEY2TERMCAP1(c);
2173 buf[3] = NUL;
2174 }
2175 else
2176 buf[(*mb_char2bytes)(c, buf)] = NUL;
2177
2178 get_mode(curr_mode);
2179
2180 // Lock the text to avoid weird things from happening.
2181 ++textlock;
2182 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2183
2184 v_event = get_v_event(&save_v_event);
2185 (void)dict_add_bool(v_event, "typed", KeyTyped);
2186
2187 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2188 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2189 {
2190 // Get the value of v:char. It may be empty or more than one
2191 // character. Only use it when changed, otherwise continue with the
2192 // original character.
2193 char_u *v_char;
2194
2195 v_char = get_vim_var_str(VV_CHAR);
2196
2197 // Convert special bytes when it is special string.
2198 if (STRLEN(v_char) >= 3 && v_char[0] == K_SPECIAL)
2199 res = TERMCAP2KEY(v_char[1], v_char[2]);
2200 else if (STRLEN(v_char) > 0)
2201 res = PTR2CHAR(v_char);
2202 }
2203
2204 restore_v_event(v_event, &save_v_event);
2205
2206 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2207 --textlock;
2208
2209 // Restore the State, it may have been changed.
2210 State = save_State;
2211
2212 return res;
2213}
2214#endif
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216/*
2217 * Like vgetc(), but never return a NUL when called recursively, get a key
2218 * directly from the user (ignoring typeahead).
2219 */
2220 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002221safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 int c;
2224
2225 c = vgetc();
2226 if (c == NUL)
2227 c = get_keystroke();
2228 return c;
2229}
2230
2231/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002232 * Like safe_vgetc(), but loop to handle K_IGNORE.
2233 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002234 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002235 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002236 static int
2237plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002238{
2239 int c;
2240
2241 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002242 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002243 while (c == K_IGNORE
2244 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2245 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002246 return c;
2247}
2248
2249/*
2250 * Like safe_vgetc(), but loop to handle K_IGNORE.
2251 * Also ignore scrollbar events.
2252 */
2253 int
2254plain_vgetc(void)
2255{
2256 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002257
2258 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002259 // Only handle the first pasted character. Drop the rest, since we
2260 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002261 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2262
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002263 return c;
2264}
2265
2266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 * Check if a character is available, such that vgetc() will not block.
2268 * If the next character is a special character or multi-byte, the returned
2269 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002270 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 */
2272 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002273vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002275 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002277 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002280#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281/*
2282 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2283 * codes.
2284 */
2285 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002286vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 int c;
2289
2290 ++no_mapping;
2291 ++allow_keys;
2292 c = vpeekc();
2293 --no_mapping;
2294 --allow_keys;
2295 return c;
2296}
2297#endif
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299/*
2300 * Check if any character is available, also half an escape sequence.
2301 * Trick: when no typeahead found, but there is something in the typeahead
2302 * buffer, it must be an ESC that is recognized as the start of a key code.
2303 */
2304 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002305vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 int c;
2308
2309 c = vpeekc();
2310 if (c == NUL && typebuf.tb_len > 0)
2311 c = ESC;
2312 return c;
2313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315/*
2316 * Call vpeekc() without causing anything to be mapped.
2317 * Return TRUE if a character is available, FALSE otherwise.
2318 */
2319 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002320char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 int retval;
2323
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002324#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002325 // When test_override("char_avail", 1) was called pretend there is no
2326 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002327 if (disable_char_avail_for_testing)
2328 return FALSE;
2329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 ++no_mapping;
2331 retval = vpeekc();
2332 --no_mapping;
2333 return (retval != NUL);
2334}
2335
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002336#if defined(FEAT_EVAL) || defined(PROTO)
2337/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002338 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002339 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002340 static void
2341getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002342{
2343 varnumber_T n;
2344 int error = FALSE;
2345
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002346 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2347 return;
2348
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002349#ifdef MESSAGE_QUEUE
2350 // vpeekc() used to check for messages, but that caused problems, invoking
2351 // a callback where it was not expected. Some plugins use getchar(1) in a
2352 // loop to await a message, therefore make sure we check for messages here.
2353 parse_queued_messages();
2354#endif
2355
Bram Moolenaar30613902019-12-01 22:11:18 +01002356 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002357 windgoto(msg_row, msg_col);
2358
2359 ++no_mapping;
2360 ++allow_keys;
2361 for (;;)
2362 {
2363 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002364 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002365 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002366 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002367 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002368 n = vpeekc_any();
2369 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002370 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002371 n = 0;
2372 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002373 // getchar(0) and char avail() != NUL: get a character.
2374 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2375 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002376
Bram Moolenaar15183b42020-09-05 19:59:39 +02002377 if (n == K_IGNORE || n == K_MOUSEMOVE
2378 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002379 continue;
2380 break;
2381 }
2382 --no_mapping;
2383 --allow_keys;
2384
2385 set_vim_var_nr(VV_MOUSE_WIN, 0);
2386 set_vim_var_nr(VV_MOUSE_WINID, 0);
2387 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2388 set_vim_var_nr(VV_MOUSE_COL, 0);
2389
2390 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002391 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002392 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002393 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002394 int i = 0;
2395
Bram Moolenaar30613902019-12-01 22:11:18 +01002396 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002397 if (mod_mask != 0)
2398 {
2399 temp[i++] = K_SPECIAL;
2400 temp[i++] = KS_MODIFIER;
2401 temp[i++] = mod_mask;
2402 }
2403 if (IS_SPECIAL(n))
2404 {
2405 temp[i++] = K_SPECIAL;
2406 temp[i++] = K_SECOND(n);
2407 temp[i++] = K_THIRD(n);
2408 }
2409 else if (has_mbyte)
2410 i += (*mb_char2bytes)(n, temp + i);
2411 else
2412 temp[i++] = n;
2413 temp[i++] = NUL;
2414 rettv->v_type = VAR_STRING;
2415 rettv->vval.v_string = vim_strsave(temp);
2416
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002417 if (is_mouse_key(n))
2418 {
2419 int row = mouse_row;
2420 int col = mouse_col;
2421 win_T *win;
2422 linenr_T lnum;
2423 win_T *wp;
2424 int winnr = 1;
2425
2426 if (row >= 0 && col >= 0)
2427 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002428 // Find the window at the mouse coordinates and compute the
2429 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002430 win = mouse_find_win(&row, &col, FIND_POPUP);
2431 if (win == NULL)
2432 return;
2433 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002434#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002435 if (WIN_IS_POPUP(win))
2436 winnr = 0;
2437 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002438#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002439 for (wp = firstwin; wp != win && wp != NULL;
2440 wp = wp->w_next)
2441 ++winnr;
2442 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2443 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2444 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2445 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2446 }
2447 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002448 }
2449}
2450
2451/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002452 * "getchar()" function
2453 */
2454 void
2455f_getchar(typval_T *argvars, typval_T *rettv)
2456{
2457 getchar_common(argvars, rettv);
2458}
2459
2460/*
2461 * "getcharstr()" function
2462 */
2463 void
2464f_getcharstr(typval_T *argvars, typval_T *rettv)
2465{
2466 getchar_common(argvars, rettv);
2467
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002468 if (rettv->v_type != VAR_NUMBER)
2469 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002470
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002471 char_u temp[7]; // mbyte-char: 6, NUL: 1
2472 varnumber_T n = rettv->vval.v_number;
2473 int i = 0;
2474
2475 if (n != 0)
2476 {
2477 if (has_mbyte)
2478 i += (*mb_char2bytes)(n, temp + i);
2479 else
2480 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002481 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002482 temp[i++] = NUL;
2483 rettv->v_type = VAR_STRING;
2484 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002485}
2486
2487/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002488 * "getcharmod()" function
2489 */
2490 void
2491f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2492{
2493 rettv->vval.v_number = mod_mask;
2494}
2495#endif // FEAT_EVAL
2496
2497#if defined(MESSAGE_QUEUE) || defined(PROTO)
2498# define MAX_REPEAT_PARSE 8
2499
2500/*
2501 * Process messages that have been queued for netbeans or clientserver.
2502 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002503 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002504 * it is safe to do so.
2505 */
2506 void
2507parse_queued_messages(void)
2508{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002509 int old_curwin_id;
2510 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002511 int i;
2512 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002513 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002514 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002515
2516 // Do not handle messages while redrawing, because it may cause buffers to
2517 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002518 // Also bail out when parsing messages was explicitly disabled.
2519 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002520 return;
2521
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002522 // If memory allocation fails during startup we'll exit but curbuf or
2523 // curwin could be NULL.
2524 if (curbuf == NULL || curwin == NULL)
2525 return;
2526
2527 old_curbuf_fnum = curbuf->b_fnum;
2528 old_curwin_id = curwin->w_id;
2529
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002530 ++entered;
2531
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002532 // may_garbage_collect is set in main_loop() to do garbage collection when
2533 // blocking to wait on a character. We don't want that while parsing
2534 // messages, a callback may invoke vgetc() while lists and dicts are in use
2535 // in the call stack.
2536 may_garbage_collect = FALSE;
2537
2538 // Loop when a job ended, but don't keep looping forever.
2539 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2540 {
2541 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002542# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002543 channel_handle_events(FALSE);
2544# endif
2545
2546# ifdef FEAT_NETBEANS_INTG
2547 // Process the queued netbeans messages.
2548 netbeans_parse_messages();
2549# endif
2550# ifdef FEAT_JOB_CHANNEL
2551 // Write any buffer lines still to be written.
2552 channel_write_any_lines();
2553
2554 // Process the messages queued on channels.
2555 channel_parse_messages();
2556# endif
2557# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2558 // Process the queued clientserver messages.
2559 server_parse_messages();
2560# endif
2561# ifdef FEAT_JOB_CHANNEL
2562 // Check if any jobs have ended. If so, repeat the above to handle
2563 // changes, e.g. stdin may have been closed.
2564 if (job_check_ended())
2565 continue;
2566# endif
2567# ifdef FEAT_TERMINAL
2568 free_unused_terminals();
2569# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002570
2571# ifdef FEAT_SOUND_MACOSX
2572 process_cfrunloop();
2573# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002574# ifdef FEAT_SOUND_CANBERRA
2575 if (has_sound_callback_in_queue())
2576 invoke_sound_callback();
2577# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002578#ifdef SIGUSR1
2579 if (got_sigusr1)
2580 {
2581 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2582 got_sigusr1 = FALSE;
2583 }
2584#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002585 break;
2586 }
2587
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002588 // When not nested we'll go back to waiting for a typed character. If it
2589 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002590 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002591 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002592
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002593 may_garbage_collect = save_may_garbage_collect;
2594
2595 // If the current window or buffer changed we need to bail out of the
2596 // waiting loop. E.g. when a job exit callback closes the terminal window.
2597 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002598 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002599
2600 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002601}
2602#endif
2603
2604
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002605typedef enum {
2606 map_result_fail, // failed, break loop
2607 map_result_get, // get a character from typeahead
2608 map_result_retry, // try to map again
2609 map_result_nomatch // no matching mapping, get char
2610} map_result_T;
2611
2612/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002613 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002614 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002615 */
2616 static int
zeertzjqee446032022-04-30 15:02:22 +01002617at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002618{
2619 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2620 int c = *p;
2621
2622 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002623 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002624 && p[1] == KS_MODIFIER
2625 && (p[2] & MOD_MASK_CTRL))
2626 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002627 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2628 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002629}
2630
2631/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002632 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002633 * into just a key, apply that.
2634 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2635 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002636 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002637 */
2638 static int
2639check_simplify_modifier(int max_offset)
2640{
2641 int offset;
2642 char_u *tp;
2643
2644 for (offset = 0; offset < max_offset; ++offset)
2645 {
2646 if (offset + 3 >= typebuf.tb_len)
2647 break;
2648 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002649 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002650 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002651 // A modifier was not used for a mapping, apply it to ASCII keys.
2652 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002653 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002654 int c = tp[3];
2655 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002656
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002657 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002658 {
2659 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002660 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002661
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002662 if (offset == 0)
2663 {
2664 // At the start: remember the character and mod_mask before
2665 // merging, in some cases, e.g. at the hit-return prompt,
2666 // they are put back in the typeahead buffer.
2667 vgetc_char = c;
2668 vgetc_mod_mask = tp[2];
2669 }
zeertzjq12e21e32022-04-27 11:58:01 +01002670 if (IS_SPECIAL(new_c))
2671 {
2672 new_string[0] = K_SPECIAL;
2673 new_string[1] = K_SECOND(new_c);
2674 new_string[2] = K_THIRD(new_c);
2675 len = 3;
2676 }
2677 else
2678 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002679 if (modifier == 0)
2680 {
2681 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002682 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002683 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002684 }
2685 else
2686 {
2687 tp[2] = modifier;
2688 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002689 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002690 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002691 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002692 return len;
2693 }
2694 }
2695 }
2696 return 0;
2697}
2698
2699/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002700 * Return TRUE if the terminal sends modifiers with various keys. This is when
2701 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2702 * enabled.
2703 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002704 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002705key_protocol_enabled(void)
2706{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002707 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2708 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2709 ? modify_otherkeys_state == MOKS_ENABLED
2710 : seenModifyOtherKeys;
2711 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002712}
2713
2714/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002715 * Handle mappings in the typeahead buffer.
2716 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002717 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002718 * - When there is no match yet, return map_result_nomatch, need to get more
2719 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002720 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002721 */
2722 static int
2723handle_mapping(
2724 int *keylenp,
2725 int *timedout,
2726 int *mapdepth)
2727{
2728 mapblock_T *mp = NULL;
2729 mapblock_T *mp2;
2730 mapblock_T *mp_match;
2731 int mp_match_len = 0;
2732 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002733 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002734 int tb_c1;
2735 int mlen;
2736#ifdef FEAT_LANGMAP
2737 int nolmaplen;
2738#endif
2739 int keylen = *keylenp;
2740 int i;
2741 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002742 int is_plug_map = FALSE;
2743
zeertzjqbb404f52022-07-23 06:25:29 +01002744 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002745 if (typebuf.tb_len >= 3
2746 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002747 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2748 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2749 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002750
2751 /*
2752 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002753 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002754 *
2755 * Don't look for mappings if:
2756 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2757 * - maphash_valid not set: no mappings present.
2758 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2759 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002760 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 * - waiting for a char with --more--
2762 * - in Ctrl-X mode, and we get a valid char for that mode
2763 */
2764 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2765 if (no_mapping == 0 && is_maphash_valid()
2766 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002767 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002768 || (p_remap
2769 && (typebuf.tb_noremap[typebuf.tb_off]
2770 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002771 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2772 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2773 && State != MODE_ASKMORE
2774 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002775 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002776 {
Christopher Plewright03193062022-11-22 12:58:27 +00002777#ifdef FEAT_GUI
2778 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2779 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002780 {
2781 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2782 // K_SPECIAL KS_MODIFIER {flags}.
2783 tb_c1 = K_SPECIAL;
2784 }
2785#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002786#ifdef FEAT_LANGMAP
2787 if (tb_c1 == K_SPECIAL)
2788 nolmaplen = 2;
2789 else
2790 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002791 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2792 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002793 nolmaplen = 0;
2794 }
2795#endif
2796 // First try buffer-local mappings.
2797 mp = get_buf_maphash_list(local_State, tb_c1);
2798 mp2 = get_maphash_list(local_State, tb_c1);
2799 if (mp == NULL)
2800 {
2801 // There are no buffer-local mappings.
2802 mp = mp2;
2803 mp2 = NULL;
2804 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002805
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002806 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002807 * Loop until a partly matching mapping is found or all (local)
2808 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002809 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002810 * A full match is only accepted if there is no partly match, so "aa"
2811 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 */
2813 mp_match = NULL;
2814 mp_match_len = 0;
2815 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002816 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002817 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002818 // Only consider an entry if the first character matches and it is
2819 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002820 // Skip ":lmap" mappings if keys were mapped.
2821 if (mp->m_keys[0] == tb_c1
2822 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002823 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002824 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002825 && ((mp->m_mode & MODE_LANGMAP) == 0
2826 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002827 {
2828#ifdef FEAT_LANGMAP
2829 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002830 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002831#endif
2832 // find the match length of this mapping
2833 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2834 {
zeertzjq49660f52022-10-20 17:59:38 +01002835 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002837 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002838 {
2839 if (nomap == 2 && c2 == KS_MODIFIER)
2840 modifiers = 1;
2841 else if (nomap == 1 && modifiers == 1)
2842 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002843 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002844 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002845 else
zeertzjq49660f52022-10-20 17:59:38 +01002846 {
2847 if (c2 == K_SPECIAL)
2848 nomap = 2;
2849 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2850 // Only apply 'langmap' if merging modifiers into
2851 // the key will not result in another character,
2852 // so that 'langmap' behaves consistently in
2853 // different terminals and GUIs.
2854 LANGMAP_ADJUST(c2, TRUE);
2855 modifiers = 0;
2856 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002857#endif
zeertzjq49660f52022-10-20 17:59:38 +01002858 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 break;
2860 }
2861
Bram Moolenaareda35f72019-08-03 14:59:44 +02002862 // Don't allow mapping the first byte(s) of a multi-byte char.
2863 // Happens when mapping <M-a> and then changing 'encoding'.
2864 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865 {
2866 char_u *p1 = mp->m_keys;
2867 char_u *p2 = mb_unescape(&p1);
2868
2869 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002870 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002871 mlen = 0;
2872 }
2873
2874 // Check an entry whether it matches.
2875 // - Full match: mlen == keylen
2876 // - Partly match: mlen == typebuf.tb_len
2877 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002878 if (mlen == keylen || (mlen == typebuf.tb_len
2879 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002880 {
2881 char_u *s;
2882 int n;
2883
Bram Moolenaareda35f72019-08-03 14:59:44 +02002884 // If only script-local mappings are allowed, check if the
2885 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002886 s = typebuf.tb_noremap + typebuf.tb_off;
2887 if (*s == RM_SCRIPT
2888 && (mp->m_keys[0] != K_SPECIAL
2889 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002890 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002891 continue;
2892
Bram Moolenaareda35f72019-08-03 14:59:44 +02002893 // If one of the typed keys cannot be remapped, skip the
2894 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002895 for (n = mlen; --n >= 0; )
2896 if (*s++ & (RM_NONE|RM_ABBR))
2897 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002898 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002899 continue;
2900
2901 if (keylen > typebuf.tb_len)
2902 {
2903 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002904 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002905 {
2906 // break at a partly match
2907 keylen = KEYLEN_PART_MAP;
2908 break;
2909 }
2910 }
2911 else if (keylen > mp_match_len)
2912 {
2913 // found a longer match
2914 mp_match = mp;
2915 mp_match_len = keylen;
2916 }
2917 }
2918 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002919 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002920 // character. If the first character that didn't match is
2921 // K_SPECIAL then check for a termcode. This isn't perfect
2922 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002923 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002924 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002925 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002926 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2927 }
2928 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2929 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002930 }
2931 }
2932
Bram Moolenaareda35f72019-08-03 14:59:44 +02002933 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002934 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002935 {
2936 mp = mp_match;
2937 keylen = mp_match_len;
2938 }
2939 }
2940
2941 /*
2942 * Check for match with 'pastetoggle'
2943 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002944 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002945 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002946 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2947 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002948 break;
2949 if (p_pt[mlen] == NUL) // match
2950 {
2951 // write chars to script file(s)
2952 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002953 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2954 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002955
2956 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002957 set_option_value_give_err((char_u *)"paste",
2958 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002959 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002960 {
2961 msg_col = 0;
2962 msg_row = Rows - 1;
2963 msg_clr_eos(); // clear ruler
2964 }
2965 status_redraw_all();
2966 redraw_statuslines();
2967 showmode();
2968 setcursor();
2969 *keylenp = keylen;
2970 return map_result_retry;
2971 }
2972 // Need more chars for partly match.
2973 if (mlen == typebuf.tb_len)
2974 keylen = KEYLEN_PART_KEY;
2975 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002976 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002977 max_mlen = mlen + 1;
2978 }
2979
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002980 // May check for a terminal code when there is no mapping or only a partial
2981 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2982 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002983 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002984 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2985 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002986 {
2987 int save_keylen = keylen;
2988
2989 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002990 * When no matching mapping found or found a non-matching mapping that
2991 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002992 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002993 * - mapping is allowed,
2994 * - keys have not been mapped,
2995 * - and not an ESC sequence, not in insert mode or p_ek is on,
2996 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002997 */
zeertzjq68a573c2022-04-28 14:10:01 +01002998 if (no_mapping == 0 || allow_keys != 0)
2999 {
3000 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003001 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003002 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003003 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003004 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3005 else
3006 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003007
Bram Moolenaar0684e362020-12-03 19:54:42 +01003008 // If no termcode matched but 'pastetoggle' matched partially
3009 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003010 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003011 keylen = KEYLEN_PART_KEY;
3012
Bram Moolenaar975a8802020-06-06 22:36:24 +02003013 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003014 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003015#ifdef FEAT_TERMINAL
3016 check_no_reduce_keys(); // may update the no_reduce_keys flag
3017#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003018 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003019 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003020 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003021 if (keylen < 0)
3022 // ins_typebuf() failed
3023 return map_result_fail;
3024 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003025
Bram Moolenaareda35f72019-08-03 14:59:44 +02003026 // When getting a partial match, but the last characters were not
3027 // typed, don't wait for a typed character to complete the
3028 // termcode. This helps a lot when a ":normal" command ends in an
3029 // ESC.
3030 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003031 keylen = 0;
3032 }
3033 else
3034 keylen = 0;
3035 if (keylen == 0) // no matching terminal code
3036 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003037#ifdef AMIGA
3038 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003039 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003040 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003041 {
3042 char_u *s;
3043
3044 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003045 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3046 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003047 ++s)
3048 ;
3049 if (*s == 'r' || *s == '|') // found one
3050 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003051 del_typebuf(
3052 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003053 // get size and redraw screen
3054 shell_resized();
3055 *keylenp = keylen;
3056 return map_result_retry;
3057 }
3058 if (*s == NUL) // need more characters
3059 keylen = KEYLEN_PART_KEY;
3060 }
3061 if (keylen >= 0)
3062#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003063 // When there was a matching mapping and no termcode could be
3064 // replaced after another one, use that mapping (loop around).
3065 // If there was no mapping at all use the character from the
3066 // typeahead buffer right here.
3067 if (mp == NULL)
3068 {
3069 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003070 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003071 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003072 }
3073
3074 if (keylen > 0) // full matching terminal code
3075 {
3076#if defined(FEAT_GUI) && defined(FEAT_MENU)
3077 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003078 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3079 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003080 {
3081 int idx;
3082
Bram Moolenaareda35f72019-08-03 14:59:44 +02003083 // Using a menu may cause a break in undo! It's like using
3084 // gotchars(), but without recording or writing to a script
3085 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003086 may_sync_undo();
3087 del_typebuf(3, 0);
3088 idx = get_menu_index(current_menu, local_State);
3089 if (idx != MENU_INDEX_INVALID)
3090 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003091 // In Select mode and a Visual mode menu is used: Switch
3092 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003093 // back to Select mode.
3094 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003095 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003096 {
3097 VIsual_select = FALSE;
3098 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003099 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003100 }
3101 ins_typebuf(current_menu->strings[idx],
3102 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003103 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003104 }
3105 }
3106#endif // FEAT_GUI && FEAT_MENU
3107 *keylenp = keylen;
3108 return map_result_retry; // try mapping again
3109 }
3110
Bram Moolenaareda35f72019-08-03 14:59:44 +02003111 // Partial match: get some more characters. When a matching mapping
3112 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003113 if (mp == NULL || keylen < 0)
3114 keylen = KEYLEN_PART_KEY;
3115 else
3116 keylen = mp_match_len;
3117 }
3118
3119 /*
3120 * complete match
3121 */
3122 if (keylen >= 0 && keylen <= typebuf.tb_len)
3123 {
3124 char_u *map_str;
3125
3126#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003127 int save_m_expr;
3128 int save_m_noremap;
3129 int save_m_silent;
3130 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003131#else
3132# define save_m_noremap mp->m_noremap
3133# define save_m_silent mp->m_silent
3134#endif
3135
3136 // write chars to script file(s)
3137 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003138 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3139 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003140
3141 cmd_silent = (typebuf.tb_silent > 0);
3142 del_typebuf(keylen, 0); // remove the mapped keys
3143
3144 /*
3145 * Put the replacement string in front of mapstr.
3146 * The depth check catches ":map x y" and ":map y x".
3147 */
3148 if (++*mapdepth >= p_mmd)
3149 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003150 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003151 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003152 redrawcmdline();
3153 else
3154 setcursor();
3155 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003156 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003157 *keylenp = keylen;
3158 return map_result_fail;
3159 }
3160
3161 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003162 * In Select mode and a Visual mode mapping is used: Switch to Visual
3163 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003164 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003165 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003166 {
3167 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003168 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003169 }
3170
3171#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003172 // Copy the values from *mp that are used, because evaluating the
3173 // expression may invoke a function that redefines the mapping, thereby
3174 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003175 save_m_expr = mp->m_expr;
3176 save_m_noremap = mp->m_noremap;
3177 save_m_silent = mp->m_silent;
3178 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003179
3180 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003181 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3182 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003183 */
3184 if (mp->m_expr)
3185 {
3186 int save_vgetc_busy = vgetc_busy;
3187 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003188 int was_screen_col = screen_cur_col;
3189 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003190 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003191
3192 vgetc_busy = 0;
3193 may_garbage_collect = FALSE;
3194
3195 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003196 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003197
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003198 // The mapping may do anything, but we expect it to take care of
3199 // redrawing. Do put the cursor back where it was.
3200 windgoto(was_screen_row, was_screen_col);
3201 out_flush();
3202
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003203 // If an error was displayed and the expression returns an empty
3204 // string, generate a <Nop> to allow for a redraw.
3205 if (prev_did_emsg != did_emsg
3206 && (map_str == NULL || *map_str == NUL))
3207 {
3208 char_u buf[4];
3209
3210 vim_free(map_str);
3211 buf[0] = K_SPECIAL;
3212 buf[1] = KS_EXTRA;
3213 buf[2] = KE_IGNORE;
3214 buf[3] = NUL;
3215 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01003216 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003217 {
3218 // redraw the command below the error
3219 msg_didout = TRUE;
3220 if (msg_row < cmdline_row)
3221 msg_row = cmdline_row;
3222 redrawcmd();
3223 }
3224 }
3225
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003226 vgetc_busy = save_vgetc_busy;
3227 may_garbage_collect = save_may_garbage_collect;
3228 }
3229 else
3230#endif
3231 map_str = mp->m_str;
3232
3233 /*
3234 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003235 * If 'from' field is the same as the start of the 'to' field, don't
3236 * remap the first character (but do allow abbreviations).
3237 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003238 */
3239 if (map_str == NULL)
3240 i = FAIL;
3241 else
3242 {
3243 int noremap;
3244
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003245#ifdef FEAT_EVAL
3246 last_used_map = mp;
3247 last_used_sid = -1;
3248#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003249 if (save_m_noremap != REMAP_YES)
3250 noremap = save_m_noremap;
3251 else if (
3252#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003253 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3254 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003255#else
3256 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3257#endif
3258 != 0)
3259 noremap = REMAP_YES;
3260 else
3261 noremap = REMAP_SKIP;
3262 i = ins_typebuf(map_str, noremap,
3263 0, TRUE, cmd_silent || save_m_silent);
3264#ifdef FEAT_EVAL
3265 if (save_m_expr)
3266 vim_free(map_str);
3267#endif
3268 }
3269#ifdef FEAT_EVAL
3270 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003271#endif
3272 *keylenp = keylen;
3273 if (i == FAIL)
3274 return map_result_fail;
3275 return map_result_retry;
3276 }
3277
3278 *keylenp = keylen;
3279 return map_result_nomatch;
3280}
3281
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003282/*
3283 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003284 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003285 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003288vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 old_char = c;
3291 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003292 old_mouse_row = mouse_row;
3293 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003294 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003298 * When peeking and not getting a character, reg_executing cannot be cleared
3299 * yet, so set a flag to clear it later.
3300 */
3301 static void
3302check_end_reg_executing(int advance)
3303{
3304 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3305 || pending_end_reg_executing))
3306 {
3307 if (advance)
3308 {
3309 reg_executing = 0;
3310 pending_end_reg_executing = FALSE;
3311 }
3312 else
3313 pending_end_reg_executing = TRUE;
3314 }
3315
3316}
3317
3318/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003319 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 * 1. from the stuffbuffer
3321 * This is used for abbreviated commands like "D" -> "d$".
3322 * Also used to redo a command for ".".
3323 * 2. from the typeahead buffer
3324 * Stores text obtained previously but not used yet.
3325 * Also stores the result of mappings.
3326 * Also used for the ":normal" command.
3327 * 3. from the user
3328 * This may do a blocking wait if "advance" is TRUE.
3329 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003330 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003331 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 * KeyTyped is set to TRUE in the case the user typed the key.
3333 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003334 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003335 * Just look whether there is a character available.
3336 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 *
3338 * When "no_mapping" is zero, checks for mappings in the current mode.
3339 * Only returns one byte (of a multi-byte character).
3340 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3341 */
3342 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003343vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003345 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003346 int timedout = FALSE; // waited for more than 'timeoutlen'
3347 // for mapping to complete or
3348 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003349 int mapdepth = 0; // check for recursive mapping
3350 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003353 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003357 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 /*
3360 * This function doesn't work very well when called recursively. This may
3361 * happen though, because of:
3362 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3363 * there is a character available, which calls this function. In that
3364 * case we must return NUL, to indicate no character is available.
3365 * 2. A GUI callback function writes to the screen, causing a
3366 * wait_return().
3367 * Using ":normal" can also do this, but it saves the typeahead buffer,
3368 * thus it should be OK. But don't get a key from the user then.
3369 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003370 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 return NUL;
3372
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003373 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003378 typebuf_was_empty = FALSE;
3379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 init_typebuf();
3382 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003383 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 do
3385 {
3386/*
3387 * get a character: 1. from the stuffbuffer
3388 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003389 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 c = typeahead_char;
3392 if (advance)
3393 typeahead_char = 0;
3394 }
3395 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003396 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (c != NUL && !got_int)
3398 {
3399 if (advance)
3400 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003401 // KeyTyped = FALSE; When the command that stuffed something
3402 // was typed, behave like the stuffed command was typed.
3403 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 KeyStuffed = TRUE;
3405 }
3406 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003407 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409 else
3410 {
3411 /*
3412 * Loop until we either find a matching mapped key, or we
3413 * are sure that it is not a mapped key.
3414 * If a mapped key sequence is found we go back to the start to
3415 * try re-mapping.
3416 */
3417 for (;;)
3418 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003419 long wait_time;
3420 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003421 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003422 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 /*
3424 * ui_breakcheck() is slow, don't use it too often when
3425 * inside a mapping. But call it each time for typed
3426 * characters.
3427 */
3428 if (typebuf.tb_maplen)
3429 line_breakcheck();
3430 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003431 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (got_int)
3433 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003434 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003435 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 /*
3438 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003439 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * Otherwise we behave like having gotten a CTRL-C.
3441 * As a result typing CTRL-C in insert mode will
3442 * really insert a CTRL-C.
3443 */
3444 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003445 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 c = ESC;
3447 else
3448 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003449 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003451 if (advance)
3452 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003453 // Also record this character, it might be needed to
3454 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003455 *typebuf.tb_buf = c;
3456 gotchars(typebuf.tb_buf, 1);
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 cmd_silent = FALSE;
3459
3460 break;
3461 }
3462 else if (typebuf.tb_len > 0)
3463 {
3464 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003465 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003467 map_result_T result = handle_mapping(
3468 &keylen, &timedout, &mapdepth);
3469
3470 if (result == map_result_retry)
3471 // try mapping again
3472 continue;
3473 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003474 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003475 // failed, use the outer loop
3476 c = -1;
3477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003479 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481/*
3482 * get a character: 2. from the typeahead buffer
3483 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003484 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003485 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003486 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003487 cmd_silent = (typebuf.tb_silent > 0);
3488 if (typebuf.tb_maplen > 0)
3489 KeyTyped = FALSE;
3490 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003491 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003492 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003493 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003494 gotchars(typebuf.tb_buf
3495 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003496 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003497 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003498 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003499 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003500 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003503 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505
3506/*
3507 * get a character: 3. from the user - handle <Esc> in Insert mode
3508 */
3509 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003510 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003512 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 * typing an <ESC>. If we get something after all, we may
3514 * have to redisplay the mode. That the cursor is in the wrong
3515 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003516 * Do not do this if the kitty keyboard protocol is used, every
3517 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 */
3519 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 new_wcol = curwin->w_wcol;
3521 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 if ( advance
3523 && typebuf.tb_len == 1
3524 && typebuf.tb_buf[typebuf.tb_off] == ESC
3525 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003526 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003529 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003530 && (p_timeout
3531 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003533 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003535 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 char_u *ptr;
3537
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003538 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 unshowmode(TRUE);
3541 mode_deleted = TRUE;
3542 }
3543#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003544 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003545 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 int save_State;
3548
3549 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003550 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 gui_update_cursor(TRUE, FALSE);
3552 State = save_State;
3553 shape_changed = TRUE;
3554 }
3555#endif
3556 validate_cursor();
3557 old_wcol = curwin->w_wcol;
3558 old_wrow = curwin->w_wrow;
3559
Bram Moolenaar30613902019-12-01 22:11:18 +01003560 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (curwin->w_cursor.col != 0)
3562 {
3563 if (curwin->w_wcol > 0)
3564 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003565 // After auto-indenting and no text is following,
3566 // we are expecting to truncate the trailing
3567 // white-space, so find the last non-white
3568 // character -- webb
3569 if (did_ai && *skipwhite(ml_get_curline()
3570 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003572 chartabsize_T cts;
3573
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003574 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003576 init_chartabsize_arg(&cts, curwin,
3577 curwin->w_cursor.lnum, 0, ptr, ptr);
3578 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003580 if (!VIM_ISWHITE(*cts.cts_ptr))
3581 curwin->w_wcol = cts.cts_vcol;
3582 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003584 cts.cts_ptr +=
3585 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003587 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003589 clear_chartabsize_arg(&cts);
3590
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003592 + curwin->w_wcol / curwin->w_width;
3593 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003595 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597 else
3598 {
3599 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602 }
3603 else if (curwin->w_p_wrap && curwin->w_wrow)
3604 {
3605 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003606 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3610 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003611 // Correct when the cursor is on the right halve
3612 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 ptr = ml_get_curline();
3614 col -= (*mb_head_off)(ptr, ptr + col);
3615 if ((*mb_ptr2cells)(ptr + col) > 1)
3616 --curwin->w_wcol;
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 setcursor();
3620 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 new_wcol = curwin->w_wcol;
3622 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 curwin->w_wcol = old_wcol;
3624 curwin->w_wrow = old_wrow;
3625 }
3626 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003627 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003628
Bram Moolenaar30613902019-12-01 22:11:18 +01003629 // Allow mapping for just typed characters. When we get here c
3630 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003631 for (n = 1; n <= c; ++n)
3632 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 typebuf.tb_len += c;
3634
Bram Moolenaar30613902019-12-01 22:11:18 +01003635 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3637 {
3638 timedout = TRUE;
3639 continue;
3640 }
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (ex_normal_busy > 0)
3643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaar30613902019-12-01 22:11:18 +01003646 // No typeahead left and inside ":normal". Must return
3647 // something to avoid getting stuck. When an incomplete
3648 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (typebuf.tb_len > 0)
3650 {
3651 timedout = TRUE;
3652 continue;
3653 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003654
Bram Moolenaar30613902019-12-01 22:11:18 +01003655 // When 'insertmode' is set, ESC just beeps in Insert
3656 // mode. Use CTRL-L to make edit() return.
3657 // For the command line only CTRL-C always breaks it.
3658 // For the cmdline window: Alternate between ESC and
3659 // CTRL-C: ESC for most situations and CTRL-C to close the
3660 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003661 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003663#ifdef FEAT_TERMINAL
3664 else if (terminal_is_active())
3665 c = K_CANCEL;
3666#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003667 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003668 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 c = Ctrl_C;
3670 else
3671 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003673 // set a flag to indicate this wasn't a normal char
3674 if (advance)
3675 typebuf_was_empty = TRUE;
3676
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003677 // return from main_loop()
3678 if (pending_exmode_active)
3679 exmode_active = EXMODE_NORMAL;
3680
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003681 // no chars to block abbreviation for
3682 typebuf.tb_no_abbr_cnt = 0;
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 break;
3685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687/*
3688 * get a character: 3. from the user - update display
3689 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003690 // In insert mode a screen update is skipped when characters
3691 // are still available. But when those available characters
3692 // are part of a mapping, and we are going to do a blocking
3693 // wait here. Need to update the screen to display the
3694 // changed text so far. Also for when 'lazyredraw' is set and
3695 // redrawing was postponed because there was something in the
3696 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003697 if (((State & MODE_INSERT) != 0 || p_lz)
3698 && (State & MODE_CMDLINE) == 0
3699 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
3701 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003702 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704
3705 /*
3706 * If we have a partial match (and are going to wait for more
3707 * input from the user), show the partially matched characters
3708 * to the user with showcmd.
3709 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003710 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003711 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (typebuf.tb_len > 0 && advance && !exmode_active)
3713 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003714 if (((State & (MODE_NORMAL | MODE_INSERT))
3715 || State == MODE_LANGMAP)
3716 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003718 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003719 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3721 + typebuf.tb_len - 1) == 1)
3722 {
3723 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3724 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003725 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003726 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003728 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 old_wcol = curwin->w_wcol;
3730 old_wrow = curwin->w_wrow;
3731 curwin->w_wcol = new_wcol;
3732 curwin->w_wrow = new_wrow;
3733 push_showcmd();
3734 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003735 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3736 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003737 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003738 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 curwin->w_wcol = old_wcol;
3740 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003743 // This looks nice when typing a dead character map.
3744 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003745 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003746 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3748 && cmdline_star == 0
3749#endif
3750 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3751 + typebuf.tb_len - 1) == 1)
3752 {
3753 putcmdline(typebuf.tb_buf[typebuf.tb_off
3754 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003755 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 }
3758
3759/*
3760 * get a character: 3. from the user - get it
3761 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003762 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003763 // timedout may have been set if a mapping with empty RHS
3764 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003765 timedout = FALSE;
3766
Bram Moolenaar652de232019-04-04 20:13:09 +02003767 if (advance)
3768 {
3769 if (typebuf.tb_len == 0
3770 || !(p_timeout
3771 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3772 // blocking wait
3773 wait_time = -1L;
3774 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3775 wait_time = p_ttm;
3776 else
3777 wait_time = p_tm;
3778 }
3779 else
3780 wait_time = 0;
3781
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003782 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3784 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003785 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaareda35f72019-08-03 14:59:44 +02003787 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003789 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003791 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003793 if ((State & MODE_CMDLINE)
3794 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003796 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003797 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799
3800 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003801 continue; // end of input script reached
3802 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
3804 if (!advance)
3805 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003806 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
3808 timedout = TRUE;
3809 continue;
3810 }
3811 }
3812 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003813 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 while (typebuf.tb_buf[typebuf.tb_off
3815 + typebuf.tb_len] != NUL)
3816 typebuf.tb_noremap[typebuf.tb_off
3817 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003818#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003819 // Get IM status right after getting keys, not after the
3820 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 vgetc_im_active = im_get_status();
3822#endif
3823 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003824 } // for (;;)
3825 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar30613902019-12-01 22:11:18 +01003827 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003828 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 /*
3831 * The "INSERT" message is taken care of here:
3832 * if we return an ESC to exit insert mode, the message is deleted
3833 * if we don't return an ESC but deleted the message before, redisplay it
3834 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003835 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003837 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
3839 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003840 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else
3842 unshowmode(FALSE);
3843 }
3844 else if (c != ESC && mode_deleted)
3845 {
3846 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003847 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 else
3849 showmode();
3850 }
3851 }
3852#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003853 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003854 if (gui.in_use && shape_changed)
3855 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003857 if (timedout && c == ESC)
3858 {
zeertzjqbf321802024-01-28 19:03:00 +01003859 // When recording there will be no timeout. Add an <Ignore> after the
3860 // ESC to avoid that it forms a key code with following characters.
3861 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003864 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 return c;
3867}
3868
3869/*
3870 * inchar() - get one character from
3871 * 1. a scriptfile
3872 * 2. the keyboard
3873 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003874 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 * NUL terminated (buffer length must be 'maxlen' + 1).
3876 * Minimum for "maxlen" is 3!!!!
3877 *
3878 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3879 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3880 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3881 * otherwise.
3882 *
3883 * If we got an interrupt all input is read until none is available.
3884 *
3885 * If wait_time == 0 there is no waiting for the char.
3886 * If wait_time == n we wait for n msec for a character to arrive.
3887 * If wait_time == -1 we wait forever for a character to arrive.
3888 *
3889 * Return the number of obtained characters.
3890 * Return -1 when end of input script reached.
3891 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003892 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003893inchar(
3894 char_u *buf,
3895 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003896 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
Bram Moolenaar30613902019-12-01 22:11:18 +01003898 int len = 0; // init for GCC
3899 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003901 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
Bram Moolenaar30613902019-12-01 22:11:18 +01003903 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
3905 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003906 out_flush_cursor(FALSE, FALSE);
3907#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3908 if (gui.in_use && postponed_mouseshape)
3909 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910#endif
3911 }
3912
3913 /*
3914 * Don't reset these when at the hit-return prompt, otherwise a endless
3915 * recursive loop may result (write error in swapfile, hit-return, timeout
3916 * on char wait, flush swapfile, write error....).
3917 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003918 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003920 did_outofmem_msg = FALSE; // display out of memory message (again)
3921 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003923 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003926 * Get a character from a script file if there is one.
3927 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 */
3929 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003930 while (scriptin[curscript] != NULL && script_char < 0
3931#ifdef FEAT_EVAL
3932 && !ignore_script
3933#endif
3934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003936#ifdef MESSAGE_QUEUE
3937 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003938#endif
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3941 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003942 // Reached EOF.
3943 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3944 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 closescript();
3946 /*
3947 * When reading script file is interrupted, return an ESC to get
3948 * back to normal mode.
3949 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3950 */
3951 if (got_int)
3952 retesc = TRUE;
3953 else
3954 return -1;
3955 }
3956 else
3957 {
3958 buf[0] = script_char;
3959 len = 1;
3960 }
3961 }
3962
Bram Moolenaar30613902019-12-01 22:11:18 +01003963 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 /*
3966 * If we got an interrupt, skip all previously typed characters and
3967 * return TRUE if quit reading script file.
3968 * Stop reading typeahead when a single CTRL-C was read,
3969 * fill_input_buf() returns this when not able to read from stdin.
3970 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3971 * and buf may be pointing inside typebuf.tb_buf[].
3972 */
3973 if (got_int)
3974 {
kylo252ae6f1d82022-02-16 19:24:07 +00003975#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 char_u dum[DUM_LEN + 1];
3977
3978 for (;;)
3979 {
3980 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003981 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 break;
3983 }
3984 return retesc;
3985 }
3986
3987 /*
3988 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003989 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003991 if (wait_time == -1L || wait_time > 10L)
3992 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 /*
3995 * Fill up to a third of the buffer, because each character may be
3996 * tripled below.
3997 */
3998 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3999 }
4000
Bram Moolenaar30613902019-12-01 22:11:18 +01004001 // If the typebuf was changed further down, it is like nothing was added by
4002 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (typebuf_changed(tb_change_cnt))
4004 return 0;
4005
Bram Moolenaar30613902019-12-01 22:11:18 +01004006 // Note the change in the typeahead buffer, this matters for when
4007 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4008 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004009 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4010 typebuf.tb_change_cnt = 1;
4011
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004012 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013}
4014
4015/*
4016 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004017 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * Returns the new length.
4019 */
4020 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004021fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
4023 int i;
4024 char_u *p = buf;
4025
4026 /*
4027 * Two characters are special: NUL and K_SPECIAL.
4028 * When compiled With the GUI CSI is also special.
4029 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4030 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4031 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
4033 for (i = len; --i >= 0; ++p)
4034 {
4035#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004036 // When the GUI is used any character can come after a CSI, don't
4037 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (gui.in_use && p[0] == CSI && i >= 2)
4039 {
4040 p += 2;
4041 i -= 2;
4042 }
zeertzjq646bb722022-02-16 17:51:47 +00004043# ifndef MSWIN
4044 // When not on MS-Windows and the GUI is not used CSI needs to be
4045 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 else if (!gui.in_use && p[0] == CSI)
4047 {
4048 mch_memmove(p + 3, p + 1, (size_t)i);
4049 *p++ = K_SPECIAL;
4050 *p++ = KS_EXTRA;
4051 *p = (int)KE_CSI;
4052 len += 2;
4053 }
zeertzjq646bb722022-02-16 17:51:47 +00004054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 else
4056#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004057 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004058 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004059 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004060#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004061 // Win32 console passes modifiers
4062 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004063# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004064 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004065# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004066 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#endif
4068 ))
4069 {
4070 mch_memmove(p + 3, p + 1, (size_t)i);
4071 p[2] = K_THIRD(p[0]);
4072 p[1] = K_SECOND(p[0]);
4073 p[0] = K_SPECIAL;
4074 p += 2;
4075 len += 2;
4076 }
4077 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004078 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 return len;
4080}
4081
4082#if defined(USE_INPUT_BUF) || defined(PROTO)
4083/*
4084 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4085 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004086 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004087 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 */
4089 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004090input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004093# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4094 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095# endif
4096 );
4097}
4098#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004099
4100/*
4101 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4102 * typeahead.
4103 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004104 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004105getcmdkeycmd(
4106 int promptc UNUSED,
4107 void *cookie UNUSED,
4108 int indent UNUSED,
4109 getline_opt_T do_concat UNUSED)
4110{
4111 garray_T line_ga;
4112 int c1 = -1;
4113 int c2;
4114 int cmod = 0;
4115 int aborted = FALSE;
4116
4117 ga_init2(&line_ga, 1, 32);
4118
4119 // no mapping for these characters
4120 no_mapping++;
4121
4122 got_int = FALSE;
4123 while (c1 != NUL && !aborted)
4124 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004125 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004126 {
4127 aborted = TRUE;
4128 break;
4129 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004130
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004131 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004132 {
4133 // incomplete <Cmd> is an error, because there is not much the user
4134 // could do in this state.
4135 emsg(_(e_cmd_mapping_must_end_with_cr));
4136 aborted = TRUE;
4137 break;
4138 }
4139
4140 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004141 c1 = vgetorpeek(TRUE);
4142
Bram Moolenaar957cf672020-11-12 14:21:06 +01004143 // Get two extra bytes for special keys
4144 if (c1 == K_SPECIAL)
4145 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004146 c1 = vgetorpeek(TRUE);
4147 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004148 if (c1 == KS_MODIFIER)
4149 {
4150 cmod = c2;
4151 continue;
4152 }
4153 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004154
4155 // K_ESC is used to avoid ambiguity with the single Esc character
4156 // that might be the start of an escape sequence. Convert it back
4157 // to a single Esc here.
4158 if (c1 == K_ESC)
4159 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004160 }
4161
4162 if (got_int)
4163 aborted = TRUE;
4164 else if (c1 == '\r' || c1 == '\n')
4165 c1 = NUL; // end the line
4166 else if (c1 == ESC)
4167 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004168 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004169 {
4170 // give a nicer error message for this special case
4171 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4172 aborted = TRUE;
4173 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004174 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004175 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004176 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004177 }
4178 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004179 {
4180 if (cmod != 0)
4181 {
4182 ga_append(&line_ga, K_SPECIAL);
4183 ga_append(&line_ga, KS_MODIFIER);
4184 ga_append(&line_ga, cmod);
4185 }
4186 if (IS_SPECIAL(c1))
4187 {
4188 ga_append(&line_ga, K_SPECIAL);
4189 ga_append(&line_ga, K_SECOND(c1));
4190 ga_append(&line_ga, K_THIRD(c1));
4191 }
4192 else
4193 ga_append(&line_ga, c1);
4194 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004195
4196 cmod = 0;
4197 }
4198
4199 no_mapping--;
4200
4201 if (aborted)
4202 ga_clear(&line_ga);
4203
4204 return (char_u *)line_ga.ga_data;
4205}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004206
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004207#if defined(FEAT_EVAL) || defined(PROTO)
4208/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004209 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4210 * is set when redo'ing.
4211 * Put this SID in the redo buffer, so that "." will use the same script
4212 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004213 */
4214 void
4215may_add_last_used_map_to_redobuff(void)
4216{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004217 char_u buf[3 + 20];
4218 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004219
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004220 if (last_used_map != NULL)
4221 sid = last_used_map->m_script_ctx.sc_sid;
4222 if (sid < 0)
4223 sid = last_used_sid;
4224
4225 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004226 return;
4227
4228 // <K_SID>{nr};
4229 buf[0] = K_SPECIAL;
4230 buf[1] = KS_EXTRA;
4231 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004232 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004233 add_buff(&redobuff, buf, -1L);
4234}
4235#endif
4236
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004237 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004238do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004239{
4240 int res;
4241#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004242 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004243
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004244 if (key == K_SCRIPT_COMMAND
4245 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004246 {
4247 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004248 if (last_used_map != NULL)
4249 current_sctx = last_used_map->m_script_ctx;
4250 else
4251 {
4252 current_sctx.sc_sid = last_used_sid;
4253 current_sctx.sc_lnum = 0;
4254 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4255 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004256 }
4257#endif
4258
4259 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4260
4261#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004262 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004263 current_sctx = save_current_sctx;
4264#endif
4265
4266 return res;
4267}
4268
4269#if defined(FEAT_EVAL) || defined(PROTO)
4270 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004271reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004272{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004273 if (last_used_map != mp)
4274 return;
4275
4276 last_used_map = NULL;
4277 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004278}
4279#endif