blob: 3b4aad05768c6c69b1d57b727b679f9da7846f11 [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
John Marriotte7a1bbf2024-11-11 20:40:33 +010039static buffheader_T redobuff = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
40static buffheader_T old_redobuff = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
41static buffheader_T recordbuff = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
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
Shougo Matsushitafcc1b572024-07-17 20:25:22 +020045#ifdef FEAT_EVAL
46static char_u typedchars[MAXMAPLEN + 1] = { NUL }; // typed chars before map
47static int typedchars_pos = 0;
48#endif
49
Bram Moolenaar071d4272004-06-13 20:20:40 +000050/*
zeertzjq30b6d612023-05-07 17:39:23 +010051 * When block_redo is TRUE the redo buffer will not be changed.
52 * Used by edit() to repeat insertions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 */
54static int block_redo = FALSE;
55
Bram Moolenaar459fd782019-10-13 16:43:39 +020056static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020059 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 *
61 * typebuf.tb_buf[] contains all characters that are not consumed yet.
62 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
64 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
65 * The head of the buffer may contain the result of mappings, abbreviations
66 * and @a commands. The length of this part is typebuf.tb_maplen.
67 * typebuf.tb_silent is the part where <silent> applies.
68 * After the head are characters that come from the terminal.
69 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
70 * should not be considered for abbreviations.
71 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
72 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
73 * contains RM_NONE for the characters that are not to be remapped.
74 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
75 * (typebuf has been put in globals.h, because check_termcode() needs it).
76 */
Bram Moolenaar30613902019-12-01 22:11:18 +010077#define RM_YES 0 // tb_noremap: remap
78#define RM_NONE 1 // tb_noremap: don't remap
79#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
80#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar30613902019-12-01 22:11:18 +010082// typebuf.tb_buf has three parts: room in front (for result of mappings), the
83// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000084// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010086static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
87static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
Mike Williamsaca8f552024-04-07 18:26:22 +020089static size_t last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
John Marriottd3c4b7e2025-02-25 20:56:38 +010091static size_t last_get_recorded_len = 0; // length of the string returned from the
92 // last call to get_recorded()
93static size_t last_get_inserted_len = 0; // length of the string returned from the
94 // last call to get_inserted()
95
Bram Moolenaare32c3c42022-01-15 18:26:04 +000096#ifdef FEAT_EVAL
97mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010098int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000099#endif
100
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100101static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200104static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100105static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200106static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100107static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +0200108static int inchar(char_u *buf, int maxlen, long wait_time);
Shougo Matsushita83678842024-07-11 22:05:12 +0200109#ifdef FEAT_EVAL
110static int do_key_input_pre(int c);
111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
113/*
114 * Free and clear a buffer.
115 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200116 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100117free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100119 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar285e3352018-04-18 23:01:13 +0200121 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 {
123 np = p->b_next;
124 vim_free(p);
125 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200126 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000127 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128}
129
130/*
131 * Return the contents of a buffer as a single string.
132 * K_SPECIAL and CSI in the returned string are escaped.
133 */
134 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100135get_buffcont(
136 buffheader_T *buffer,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100137 int dozero, // count == zero is not an error
138 size_t *len) // the length of the returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 long_u count = 0;
141 char_u *p = NULL;
142 char_u *p2;
143 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200144 buffblock_T *bp;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100145 size_t i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaar30613902019-12-01 22:11:18 +0100147 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200148 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100149 count += (long_u)bp->b_strlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100151 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {
153 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200154 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 for (str = bp->b_str; *str; )
156 *p2++ = *str++;
157 *p2 = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100158 i = (size_t)(p2 - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100160
161 if (len != NULL)
162 *len = i;
163
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100164 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165}
166
167/*
168 * Return the contents of the record buffer as a single string
169 * and clear the record buffer.
170 * K_SPECIAL and CSI in the returned string are escaped.
171 */
172 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100173get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174{
175 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
John Marriottd3c4b7e2025-02-25 20:56:38 +0100177 p = get_buffcont(&recordbuff, TRUE, &last_get_recorded_len);
178 if (p == NULL)
179 return NULL;
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 free_buff(&recordbuff);
182
183 /*
184 * Remove the characters that were added the last time, these must be the
185 * (possibly mapped) characters that stopped the recording.
186 */
John Marriottd3c4b7e2025-02-25 20:56:38 +0100187 if (last_get_recorded_len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 {
John Marriottd3c4b7e2025-02-25 20:56:38 +0100189 last_get_recorded_len -= last_recorded_len;
190 p[last_get_recorded_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 }
192
193 /*
194 * When stopping recording from Insert mode with CTRL-O q, also remove the
195 * CTRL-O.
196 */
John Marriottd3c4b7e2025-02-25 20:56:38 +0100197 if (last_get_recorded_len > 0 && restart_edit != 0
198 && p[last_get_recorded_len - 1] == Ctrl_O)
199 {
200 --last_get_recorded_len;
201 p[last_get_recorded_len] = NUL;
202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204 return (p);
205}
206
207/*
John Marriottd3c4b7e2025-02-25 20:56:38 +0100208 * Return the length of string returned from the last call of get_recorded().
209 */
210 size_t
211get_recorded_len(void)
212{
213 return last_get_recorded_len;
214}
215
216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 * Return the contents of the redo buffer as a single string.
218 * K_SPECIAL and CSI in the returned string are escaped.
219 */
220 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100221get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
John Marriottd3c4b7e2025-02-25 20:56:38 +0100223 return get_buffcont(&redobuff, FALSE, &last_get_inserted_len);
224}
225
226/*
227 * Return the length of string returned from the last call of get_inserted().
228 */
229 size_t
230get_inserted_len(void)
231{
232 return last_get_inserted_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000236 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 * K_SPECIAL and CSI should have been escaped already.
238 */
239 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100240add_buff(
241 buffheader_T *buf,
242 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100243 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 if (slen < 0)
246 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100247 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 return;
249
Bram Moolenaar30613902019-12-01 22:11:18 +0100250 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200252 buf->bh_curr = &(buf->bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100253 buf->bh_create_newblock = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100255 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100257 iemsg(e_add_to_internal_buffer_that_was_already_read_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 return;
259 }
260 else if (buf->bh_index != 0)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100261 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200262 mch_memmove(buf->bh_first.b_next->b_str,
263 buf->bh_first.b_next->b_str + buf->bh_index,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100264 (buf->bh_first.b_next->b_strlen - buf->bh_index) + 1);
265 buf->bh_first.b_next->b_strlen -= buf->bh_index;
266 buf->bh_space += buf->bh_index;
267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 buf->bh_index = 0;
269
John Marriotte7a1bbf2024-11-11 20:40:33 +0100270 if (!buf->bh_create_newblock && buf->bh_space >= (int)slen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100272 vim_strncpy(buf->bh_curr->b_str + buf->bh_curr->b_strlen, s, (size_t)slen);
273 buf->bh_curr->b_strlen += slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 buf->bh_space -= slen;
275 }
276 else
277 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100278 long_u len;
279 buffblock_T *p;
280
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (slen < MINIMAL_SIZE)
282 len = MINIMAL_SIZE;
283 else
284 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200285 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100287 return; // no space, just forget it
Bram Moolenaarb6356332005-07-18 21:40:44 +0000288 vim_strncpy(p->b_str, s, (size_t)slen);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100289 p->b_strlen = slen;
290 buf->bh_space = (int)(len - slen);
291 buf->bh_create_newblock = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaar285e3352018-04-18 23:01:13 +0200293 p->b_next = buf->bh_curr->b_next;
294 buf->bh_curr->b_next = p;
295 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297}
298
299/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000300 * Delete "slen" bytes from the end of "buf".
301 * Only works when it was just added.
302 */
303 static void
304delete_buff_tail(buffheader_T *buf, int slen)
305{
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000306 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000307 return; // nothing to delete
John Marriotte7a1bbf2024-11-11 20:40:33 +0100308 if (buf->bh_curr->b_strlen < (size_t)slen)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000309 return;
310
John Marriotte7a1bbf2024-11-11 20:40:33 +0100311 buf->bh_curr->b_str[buf->bh_curr->b_strlen - (size_t)slen] = NUL;
312 buf->bh_curr->b_strlen -= slen;
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000313 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000314}
315
316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 * Add number "n" to buffer "buf".
318 */
319 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100320add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 char_u number[32];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100323 int numberlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
John Marriotte7a1bbf2024-11-11 20:40:33 +0100325 numberlen = vim_snprintf((char *)number, sizeof(number), "%ld", n);
326 add_buff(buf, number, (long)numberlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327}
328
329/*
330 * Add character 'c' to buffer "buf".
331 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
332 */
333 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100334add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 char_u bytes[MB_MAXBYTES + 1];
337 int len;
338 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 char_u temp[4];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100340 long templen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 if (IS_SPECIAL(c))
343 len = 1;
344 else
345 len = (*mb_char2bytes)(c, bytes);
346 for (i = 0; i < len; ++i)
347 {
348 if (!IS_SPECIAL(c))
349 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
352 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100353 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 temp[0] = K_SPECIAL;
355 temp[1] = K_SECOND(c);
356 temp[2] = K_THIRD(c);
357 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100358 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360#ifdef FEAT_GUI
361 else if (c == CSI)
362 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100363 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 temp[0] = CSI;
365 temp[1] = KS_EXTRA;
366 temp[2] = (int)KE_CSI;
367 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100368 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
370#endif
371 else
372 {
373 temp[0] = c;
374 temp[1] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100375 templen = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100377 add_buff(buf, temp, templen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379}
380
Bram Moolenaar30613902019-12-01 22:11:18 +0100381// First read ahead buffer. Used for translated commands.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100382static buffheader_T readbuf1 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100383
Bram Moolenaar30613902019-12-01 22:11:18 +0100384// Second read ahead buffer. Used for redo.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100385static buffheader_T readbuf2 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100388 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
389 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 * If advance == TRUE go to the next char.
391 * No translation is done K_SPECIAL and CSI are escaped.
392 */
393 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100394read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100396 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100398 c = read_readbuf(&readbuf1, advance);
399 if (c == NUL)
400 c = read_readbuf(&readbuf2, advance);
401 return c;
402}
403
404 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100405read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100406{
407 char_u c;
408 buffblock_T *curr;
409
Bram Moolenaar30613902019-12-01 22:11:18 +0100410 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 return NUL;
412
Bram Moolenaar285e3352018-04-18 23:01:13 +0200413 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100414 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416 if (advance)
417 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100418 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200420 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100422 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 }
424 }
425 return c;
426}
427
428/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100429 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 */
431 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100432start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200434 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200436 readbuf1.bh_curr = &(readbuf1.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100437 readbuf1.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100438 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200439 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100440 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200441 readbuf2.bh_curr = &(readbuf2.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100442 readbuf2.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 }
444}
445
446/*
447 * Return TRUE if the stuff buffer is empty.
448 */
449 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100450stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200452 return (readbuf1.bh_first.b_next == NULL
453 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100454}
455
Bram Moolenaar113e1072019-01-20 15:30:40 +0100456#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100457/*
458 * Return TRUE if readbuf1 is empty. There may still be redo characters in
459 * redbuf2.
460 */
461 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100462readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100463{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200464 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468/*
469 * Set a typeahead character that won't be flushed.
470 */
471 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100472typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 typeahead_char = c;
475}
476
477/*
478 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100479 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 * flush all typeahead characters (used when interrupted by a CTRL-C).
481 */
482 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200483flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484{
485 init_typebuf();
486
487 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100488 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 ;
490
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200491 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 {
Christian Brabandt322ba912024-08-25 21:33:03 +0200493 // remove mapped characters at the start only,
494 // but only when enough space left in typebuf
495 if (typebuf.tb_off + typebuf.tb_maplen >= typebuf.tb_buflen)
496 {
497 typebuf.tb_off = MAXMAPLEN;
498 typebuf.tb_len = 0;
499 }
500 else
501 {
502 typebuf.tb_off += typebuf.tb_maplen;
503 typebuf.tb_len -= typebuf.tb_maplen;
504 }
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100505#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
506 if (typebuf.tb_len == 0)
507 typebuf_was_filled = FALSE;
508#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200509 }
510 else
511 {
512 // remove typeahead
513 if (flush_typeahead == FLUSH_INPUT)
514 // We have to get all characters, because we may delete the first
515 // part of an escape sequence. In an xterm we get one char at a
516 // time and we have to get them all.
517 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
518 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 typebuf.tb_off = MAXMAPLEN;
520 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200521#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100522 // Reset the flag that text received from a client or from feedkeys()
523 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200524 typebuf_was_filled = FALSE;
525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 typebuf.tb_maplen = 0;
528 typebuf.tb_silent = 0;
529 cmd_silent = FALSE;
530 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200531 if (++typebuf.tb_change_cnt == 0)
532 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533}
534
535/*
536 * The previous contents of the redo buffer is kept in old_redobuffer.
537 * This is used for the CTRL-O <.> command in insert mode.
538 */
539 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100540ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000542 if (block_redo)
543 return;
544
545 free_buff(&old_redobuff);
546 old_redobuff = redobuff;
547 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548}
549
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100550/*
551 * Discard the contents of the redo buffer and restore the previous redo
552 * buffer.
553 */
554 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100555CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100556{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000557 if (block_redo)
558 return;
559
560 free_buff(&redobuff);
561 redobuff = old_redobuff;
562 old_redobuff.bh_first.b_next = NULL;
563 start_stuff();
564 while (read_readbuffers(TRUE) != NUL)
565 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100566}
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568/*
569 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
570 * Used before executing autocommands and user functions.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200573saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575 char_u *s;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100576 size_t slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200578 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200579 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200580 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200581 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582
Bram Moolenaar30613902019-12-01 22:11:18 +0100583 // Make a copy, so that ":normal ." in a function works.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100584 s = get_buffcont(&save_redo->sr_redobuff, FALSE, &slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000585 if (s == NULL)
586 return;
587
John Marriotte7a1bbf2024-11-11 20:40:33 +0100588 add_buff(&redobuff, s, (long)slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000589 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590}
591
592/*
593 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
594 * Used after executing autocommands and user functions.
595 */
596 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200597restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200599 free_buff(&redobuff);
600 redobuff = save_redo->sr_redobuff;
601 free_buff(&old_redobuff);
602 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
605/*
606 * Append "s" to the redo buffer.
607 * K_SPECIAL and CSI should already have been escaped.
608 */
609 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100610AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 if (!block_redo)
613 add_buff(&redobuff, s, -1L);
614}
615
616/*
617 * Append to Redo buffer literally, escaping special characters with CTRL-V.
618 * K_SPECIAL and CSI are escaped as well.
619 */
620 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100621AppendToRedobuffLit(
622 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100623 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000625 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int c;
627 char_u *start;
628
629 if (block_redo)
630 return;
631
Bram Moolenaarebefac62005-12-28 22:39:57 +0000632 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100634 // Put a string of normal characters in the redo buffer (that's
635 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000637 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ++s;
639
Bram Moolenaar30613902019-12-01 22:11:18 +0100640 // Don't put '0' or '^' as last character, just in case a CTRL-D is
641 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
643 --s;
644 if (s > start)
645 add_buff(&redobuff, start, (long)(s - start));
646
Bram Moolenaarebefac62005-12-28 22:39:57 +0000647 if (*s == NUL || (len >= 0 && s - str >= len))
648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar30613902019-12-01 22:11:18 +0100650 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000651 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100652 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000653 c = mb_cptr2char_adv(&s);
654 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000655 c = *s++;
656 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
657 add_char_buff(&redobuff, Ctrl_V);
658
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000659 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000660 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000661 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000662 else
663 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665}
666
667/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100668 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
669 * and escaping other K_SPECIAL and CSI bytes.
670 */
671 void
672AppendToRedobuffSpec(char_u *s)
673{
zeertzjq30b6d612023-05-07 17:39:23 +0100674 if (block_redo)
675 return;
676
zeertzjq3ab3a862023-05-06 16:22:04 +0100677 while (*s != NUL)
678 {
679 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
680 {
zeertzjq30b6d612023-05-07 17:39:23 +0100681 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100682 add_buff(&redobuff, s, 3L);
683 s += 3;
684 }
685 else
686 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
687 }
688}
689
690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 * Append a character to the redo buffer.
692 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
693 */
694 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100695AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696{
697 if (!block_redo)
698 add_char_buff(&redobuff, c);
699}
700
701/*
702 * Append a number to the redo buffer.
703 */
704 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100705AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
707 if (!block_redo)
708 add_num_buff(&redobuff, n);
709}
710
711/*
712 * Append string "s" to the stuff buffer.
713 * CSI and K_SPECIAL must already have been escaped.
714 */
715 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100716stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719}
720
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200721/*
722 * Append string "s" to the redo stuff buffer.
723 * CSI and K_SPECIAL must already have been escaped.
724 */
725 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100726stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200727{
728 add_buff(&readbuf2, s, -1L);
729}
730
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200731 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100732stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100734 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735}
736
737#if defined(FEAT_EVAL) || defined(PROTO)
738/*
739 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
740 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200741 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
743 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100744stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200746 int c;
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 while (*s != NUL)
749 {
750 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
751 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100752 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 stuffReadbuffLen(s, 3L);
754 s += 3;
755 }
756 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200757 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100758 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200759 if (c == CAR || c == NL || c == ESC)
760 c = ' ';
761 stuffcharReadbuff(c);
762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764}
765#endif
766
767/*
768 * Append a character to the stuff buffer.
769 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
770 */
771 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100772stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100774 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775}
776
777/*
778 * Append a number to the stuff buffer.
779 */
780 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100781stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100783 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200787 * Stuff a string into the typeahead buffer, such that edit() will insert it
788 * literally ("literally" TRUE) or interpret is as typed characters.
789 */
790 void
791stuffescaped(char_u *arg, int literally)
792{
793 int c;
794 char_u *start;
795
796 while (*arg != NUL)
797 {
798 // Stuff a sequence of normal ASCII characters, that's fast. Also
799 // stuff K_SPECIAL to get the effect of a special key when "literally"
800 // is TRUE.
801 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000802 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200803 || (*arg == K_SPECIAL && !literally))
804 ++arg;
805 if (arg > start)
806 stuffReadbuffLen(start, (long)(arg - start));
807
808 // stuff a single special character
809 if (*arg != NUL)
810 {
811 if (has_mbyte)
812 c = mb_cptr2char_adv(&arg);
813 else
814 c = *arg++;
815 if (literally && ((c < ' ' && c != TAB) || c == DEL))
816 stuffcharReadbuff(Ctrl_V);
817 stuffcharReadbuff(c);
818 }
819 }
820}
821
822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
824 * multibyte characters.
825 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100826 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
827 * otherwise.
828 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 */
830 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100831read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100833 static buffblock_T *bp;
834 static char_u *p;
835 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100836 int n;
837 char_u buf[MB_MAXBYTES + 1];
838 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840 if (init)
841 {
842 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200843 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200845 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (bp == NULL)
847 return FAIL;
848 p = bp->b_str;
849 return OK;
850 }
851 if ((c = *p) != NUL)
852 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100853 // Reverse the conversion done by add_char_buff()
854 // For a multi-byte character get all the bytes and return the
855 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
857 n = MB_BYTE2LEN_CHECK(c);
858 else
859 n = 1;
860 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100862 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
864 c = TO_SPECIAL(p[1], p[2]);
865 p += 2;
866 }
867#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100868 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 p += 2;
870#endif
871 if (*++p == NUL && bp->b_next != NULL)
872 {
873 bp = bp->b_next;
874 p = bp->b_str;
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100877 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
879 if (n != 1)
880 c = (*mb_ptr2char)(buf);
881 break;
882 }
883 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100884 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 }
888
889 return c;
890}
891
892/*
893 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
894 * If old_redo is TRUE, use old_redobuff instead of redobuff.
895 * The escaped K_SPECIAL and CSI are copied without translation.
896 */
897 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100898copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 int c;
901
902 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100903 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904}
905
906/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100907 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 * Insert the redo count into the command.
909 * If "old_redo" is TRUE, the last but one command is repeated
910 * instead of the last command (inserting text). This is used for
911 * CTRL-O <.> in insert mode
912 *
913 * return FAIL for failure, OK otherwise
914 */
915 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100916start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 int c;
919
Bram Moolenaar30613902019-12-01 22:11:18 +0100920 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (read_redo(TRUE, old_redo) == FAIL)
922 return FAIL;
923
924 c = read_redo(FALSE, old_redo);
925
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100926#ifdef FEAT_EVAL
927 if (c == K_SID)
928 {
929 // Copy the <SID>{sid}; sequence
930 add_char_buff(&readbuf2, c);
931 for (;;)
932 {
933 c = read_redo(FALSE, old_redo);
934 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100935 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100936 break;
937 }
938 c = read_redo(FALSE, old_redo);
939 }
940#endif
941
Bram Moolenaar30613902019-12-01 22:11:18 +0100942 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (c == '"')
944 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100945 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 c = read_redo(FALSE, old_redo);
947
Bram Moolenaar30613902019-12-01 22:11:18 +0100948 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (c >= '1' && c < '9')
950 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100951 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200952
Bram Moolenaar30613902019-12-01 22:11:18 +0100953 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200954 if (c == '=')
955 {
956 add_char_buff(&readbuf2, CAR);
957 cmd_silent = TRUE;
958 }
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 c = read_redo(FALSE, old_redo);
961 }
962
Bram Moolenaar30613902019-12-01 22:11:18 +0100963 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
965 VIsual = curwin->w_cursor;
966 VIsual_active = TRUE;
967 VIsual_select = FALSE;
968 VIsual_reselect = TRUE;
969 redo_VIsual_busy = TRUE;
970 c = read_redo(FALSE, old_redo);
971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Bram Moolenaar30613902019-12-01 22:11:18 +0100973 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (count)
975 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100976 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100978 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100981 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100982 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 copy_redo(old_redo);
984 return OK;
985}
986
987/*
988 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100989 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 * return FAIL for failure, OK otherwise
991 */
992 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100993start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995 int c;
996
997 if (read_redo(TRUE, FALSE) == FAIL)
998 return FAIL;
999 start_stuff();
1000
Bram Moolenaar30613902019-12-01 22:11:18 +01001001 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 while ((c = read_redo(FALSE, FALSE)) != NUL)
1003 {
1004 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
1005 {
1006 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001007 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 break;
1009 }
1010 }
1011
Bram Moolenaar30613902019-12-01 22:11:18 +01001012 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 copy_redo(FALSE);
1014 block_redo = TRUE;
1015 return OK;
1016}
1017
1018 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001019stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 block_redo = FALSE;
1022}
1023
1024/*
1025 * Initialize typebuf.tb_buf to point to typebuf_init.
1026 * alloc() cannot be used here: In out-of-memory situations it would
1027 * be impossible to type anything.
1028 */
1029 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001030init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001032 if (typebuf.tb_buf != NULL)
1033 return;
1034
1035 typebuf.tb_buf = typebuf_init;
1036 typebuf.tb_noremap = noremapbuf_init;
1037 typebuf.tb_buflen = TYPELEN_INIT;
1038 typebuf.tb_len = 0;
1039 typebuf.tb_off = MAXMAPLEN + 4;
1040 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041}
1042
1043/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001044 * Returns TRUE when keys cannot be remapped.
1045 */
1046 int
1047noremap_keys(void)
1048{
1049 return KeyNoremap & (RM_NONE|RM_SCRIPT);
1050}
1051
1052/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001053 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
1054 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001056 * If "noremap" is REMAP_YES, new string can be mapped again.
1057 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
1058 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001059 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001060 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001062 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001064 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1065 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001067 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 *
1069 * return FAIL for failure, OK otherwise
1070 */
1071 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001072ins_typebuf(
1073 char_u *str,
1074 int noremap,
1075 int offset,
1076 int nottyped,
1077 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
1079 char_u *s1, *s2;
1080 int newlen;
1081 int addlen;
1082 int i;
1083 int newoff;
1084 int val;
1085 int nrm;
1086
1087 init_typebuf();
1088 if (++typebuf.tb_change_cnt == 0)
1089 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001090 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (offset == 0 && addlen <= typebuf.tb_off)
1095 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001096 /*
1097 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 typebuf.tb_off -= addlen;
1100 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1101 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001102 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1103 >= addlen + 3 * (MAXMAPLEN + 4))
1104 {
1105 /*
1106 * Buffer is empty and string fits in the existing buffer.
1107 * Leave some space before and after, if possible.
1108 */
1109 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1110 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 else
1113 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001114 int extra;
1115
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001116 /*
1117 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001118 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001119 * characters. We add some extra room to avoid having to allocate too
1120 * often.
1121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001123 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1124 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001126 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001127 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 setcursor();
1129 return FAIL;
1130 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001131 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001133 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 return FAIL;
1135 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001136 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 vim_free(s1);
1139 return FAIL;
1140 }
1141 typebuf.tb_buflen = newlen;
1142
Bram Moolenaar30613902019-12-01 22:11:18 +01001143 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1145 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001146 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001148 // copy the old chars, after the insertion point, including the NUL at
1149 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 mch_memmove(s1 + newoff + offset + addlen,
1151 typebuf.tb_buf + typebuf.tb_off + offset,
1152 (size_t)(typebuf.tb_len - offset + 1));
1153 if (typebuf.tb_buf != typebuf_init)
1154 vim_free(typebuf.tb_buf);
1155 typebuf.tb_buf = s1;
1156
1157 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1158 (size_t)offset);
1159 mch_memmove(s2 + newoff + offset + addlen,
1160 typebuf.tb_noremap + typebuf.tb_off + offset,
1161 (size_t)(typebuf.tb_len - offset));
1162 if (typebuf.tb_noremap != noremapbuf_init)
1163 vim_free(typebuf.tb_noremap);
1164 typebuf.tb_noremap = s2;
1165
1166 typebuf.tb_off = newoff;
1167 }
1168 typebuf.tb_len += addlen;
1169
Bram Moolenaar30613902019-12-01 22:11:18 +01001170 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (noremap == REMAP_SCRIPT)
1172 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001173 else if (noremap == REMAP_SKIP)
1174 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else
1176 val = RM_NONE;
1177
1178 /*
1179 * Adjust typebuf.tb_noremap[] for the new characters:
1180 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1181 * (sometimes) not remappable
1182 * If noremap == REMAP_YES: all the new characters are mappable
1183 * If noremap > 0: "noremap" characters are not remappable, the rest
1184 * mappable
1185 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001186 if (noremap == REMAP_SKIP)
1187 nrm = 1;
1188 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 nrm = addlen;
1190 else
1191 nrm = noremap;
1192 for (i = 0; i < addlen; ++i)
1193 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1194 (--nrm >= 0) ? val : RM_YES;
1195
Bram Moolenaar30613902019-12-01 22:11:18 +01001196 // tb_maplen and tb_silent only remember the length of mapped and/or
1197 // silent mappings at the start of the buffer, assuming that a mapped
1198 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (nottyped || typebuf.tb_maplen > offset)
1200 typebuf.tb_maplen += addlen;
1201 if (silent || typebuf.tb_silent > offset)
1202 {
1203 typebuf.tb_silent += addlen;
1204 cmd_silent = TRUE;
1205 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001206 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 typebuf.tb_no_abbr_cnt += addlen;
1208
1209 return OK;
1210}
1211
1212/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001213 * Put character "c" back into the typeahead buffer.
1214 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001215 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1216 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001217 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001218 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001219 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001220ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001221{
zeertzjq6cac7702022-01-04 18:01:21 +00001222 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001223 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001224
zeertzjq2e7cba32022-06-10 15:30:32 +01001225 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001226 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001227 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001228}
1229
1230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001232 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001233 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1235 * changed it was reallocated and the old pointer can no longer be used.
1236 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1237 * that was just added.
1238 */
1239 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001240typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001241 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001244#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1245 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#endif
1247 ));
1248}
1249
1250/*
1251 * Return TRUE if there are no characters in the typeahead buffer that have
1252 * not been typed (result from a mapping or come from ":normal").
1253 */
1254 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001255typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 return typebuf.tb_maplen == 0;
1258}
1259
1260/*
1261 * Return the number of characters that are mapped (or not typed).
1262 */
1263 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001264typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 return typebuf.tb_maplen;
1267}
1268
1269/*
1270 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1271 */
1272 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001273del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int i;
1276
1277 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001278 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280 typebuf.tb_len -= len;
1281
1282 /*
1283 * Easy case: Just increase typebuf.tb_off.
1284 */
1285 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1286 >= 3 * MAXMAPLEN + 3)
1287 typebuf.tb_off += len;
1288 /*
1289 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1290 */
1291 else
1292 {
1293 i = typebuf.tb_off + offset;
1294 /*
1295 * Leave some extra room at the end to avoid reallocation.
1296 */
1297 if (typebuf.tb_off > MAXMAPLEN)
1298 {
1299 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1300 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1301 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1302 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1303 typebuf.tb_off = MAXMAPLEN;
1304 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001305 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1307 typebuf.tb_buf + i + len,
1308 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001309 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1311 typebuf.tb_noremap + i + len,
1312 (size_t)(typebuf.tb_len - offset));
1313 }
1314
Bram Moolenaar30613902019-12-01 22:11:18 +01001315 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 if (typebuf.tb_maplen < offset + len)
1318 typebuf.tb_maplen = offset;
1319 else
1320 typebuf.tb_maplen -= len;
1321 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001322 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
1324 if (typebuf.tb_silent < offset + len)
1325 typebuf.tb_silent = offset;
1326 else
1327 typebuf.tb_silent -= len;
1328 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001329 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 if (typebuf.tb_no_abbr_cnt < offset + len)
1332 typebuf.tb_no_abbr_cnt = offset;
1333 else
1334 typebuf.tb_no_abbr_cnt -= len;
1335 }
1336
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001337#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001338 // Reset the flag that text received from a client or from feedkeys()
1339 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001340 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#endif
1342 if (++typebuf.tb_change_cnt == 0)
1343 typebuf.tb_change_cnt = 1;
1344}
1345
zeertzjq094c4392024-04-18 22:09:37 +02001346/*
1347 * State for adding bytes to a recording or 'showcmd'.
1348 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001349typedef struct
1350{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001351 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001352 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001353 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001354 unsigned pending_special;
1355 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001356} gotchars_state_T;
1357
1358/*
1359 * Add a single byte to a recording or 'showcmd'.
1360 * Return TRUE if a full key has been received, FALSE otherwise.
1361 */
1362 static int
1363gotchars_add_byte(gotchars_state_T *state, char_u byte)
1364{
1365 int c = state->buf[state->buflen++] = byte;
1366 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001367 int in_special = state->pending_special > 0;
1368 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001369
zeertzjq6b13e3d2024-04-22 21:04:29 +02001370 if (in_special)
1371 state->pending_special--;
1372 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001373#ifdef FEAT_GUI
1374 || c == CSI
1375#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001376 )
1377 // When receiving a special key sequence, store it until we have all
1378 // the bytes and we can decide what to do with it.
1379 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001380
zeertzjq6b13e3d2024-04-22 21:04:29 +02001381 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001382 goto ret_false;
1383
zeertzjq6b13e3d2024-04-22 21:04:29 +02001384 if (in_mbyte)
1385 state->pending_mbyte--;
1386 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001387 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001388 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001389 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001390 if (state->prev_c == KS_MODIFIER)
1391 // When receiving a modifier, wait for the modified key.
1392 goto ret_false;
1393 c = TO_SPECIAL(state->prev_c, c);
1394 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1395 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1396 // in a recording.
1397 state->buflen = 0;
1398 }
1399 // When receiving a multibyte character, store it until we have all
1400 // the bytes, so that it won't be split between two buffer blocks,
1401 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001402 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001403 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001404
1405 if (state->pending_mbyte > 0)
1406 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001407
1408 retval = TRUE;
1409ret_false:
1410 state->prev_c = c;
1411 return retval;
1412}
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001416 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 */
1418 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001419gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001421 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001422 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001423 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001424 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
zeertzjqacdfb8a2024-04-17 21:28:54 +02001426 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001428 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001429 continue;
1430
Bram Moolenaar30613902019-12-01 22:11:18 +01001431 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001432 for (i = 0; i < state.buflen; ++i)
1433 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001435 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001437 state.buf[state.buflen] = NUL;
1438 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001439 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001440 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001442 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 may_sync_undo();
1446
1447#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001448 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 debug_did_msg = FALSE;
1450#endif
1451
Bram Moolenaar30613902019-12-01 22:11:18 +01001452 // Since characters have been typed, consider the following to be in
1453 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 ++maptick;
1455}
1456
1457/*
zeertzjqbf321802024-01-28 19:03:00 +01001458 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001459 */
1460 void
zeertzjqbf321802024-01-28 19:03:00 +01001461gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001462{
zeertzjqbf321802024-01-28 19:03:00 +01001463 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001464 gotchars(nop_buf, 3);
1465}
1466
1467/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001468 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1469 * character back into the typeahead buffer, thus gotchars() will be called
1470 * again.
1471 * Only affects recorded characters.
1472 */
1473 void
1474ungetchars(int len)
1475{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001476 if (reg_recording == 0)
1477 return;
1478
1479 delete_buff_tail(&recordbuff, len);
1480 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001481}
1482
1483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 * Sync undo. Called when typed characters are obtained from the typeahead
1485 * buffer, or when a menu is used.
1486 * Do not sync:
1487 * - In Insert mode, unless cursor key has been used.
1488 * - While reading a script file.
1489 * - When no_u_sync is non-zero.
1490 */
1491 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001492may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
Bram Moolenaar24959102022-05-07 20:01:16 +01001494 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001495 && scriptin[curscript] == NULL)
1496 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497}
1498
1499/*
1500 * Make "typebuf" empty and allocate new buffers.
1501 * Returns FAIL when out of memory.
1502 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001503 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001504alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 typebuf.tb_buf = alloc(TYPELEN_INIT);
1507 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1508 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1509 {
1510 free_typebuf();
1511 return FAIL;
1512 }
1513 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001514 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 typebuf.tb_len = 0;
1516 typebuf.tb_maplen = 0;
1517 typebuf.tb_silent = 0;
1518 typebuf.tb_no_abbr_cnt = 0;
1519 if (++typebuf.tb_change_cnt == 0)
1520 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001521#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1522 typebuf_was_filled = FALSE;
1523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 return OK;
1525}
1526
1527/*
1528 * Free the buffers of "typebuf".
1529 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001530 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001531free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001533 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001534 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001535 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001536 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001537 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001538 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001539 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001540 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541}
1542
1543/*
1544 * When doing ":so! file", the current typeahead needs to be saved, and
1545 * restored when "file" has been read completely.
1546 */
1547static typebuf_T saved_typebuf[NSCRIPT];
1548
1549 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001550save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 init_typebuf();
1553 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001554 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (alloc_typebuf() == FAIL)
1556 {
1557 closescript();
1558 return FAIL;
1559 }
1560 return OK;
1561}
1562
Bram Moolenaar30613902019-12-01 22:11:18 +01001563static int old_char = -1; // character put back by vungetc()
1564static int old_mod_mask; // mod_mask for ungotten character
1565static int old_mouse_row; // mouse_row related to old_char
1566static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001567static int old_KeyStuffed; // whether old_char was stuffed
1568
zeertzjq6d4e7252022-04-07 13:58:04 +01001569static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001570{
1571 // If the old character was not stuffed and characters have been added to
1572 // the stuff buffer, need to first get the stuffed characters instead.
1573 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1574}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576/*
1577 * Save all three kinds of typeahead, so that the user must type at a prompt.
1578 */
1579 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001580save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 tp->save_typebuf = typebuf;
1583 tp->typebuf_valid = (alloc_typebuf() == OK);
1584 if (!tp->typebuf_valid)
1585 typebuf = tp->save_typebuf;
1586
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001587 tp->old_char = old_char;
1588 tp->old_mod_mask = old_mod_mask;
1589 old_char = -1;
1590
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001591 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001592 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001593 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001594 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595# ifdef USE_INPUT_BUF
1596 tp->save_inputbuf = get_input_buf();
1597# endif
1598}
1599
1600/*
1601 * Restore the typeahead to what it was before calling save_typeahead().
1602 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001603 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
1605 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001606restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 if (tp->typebuf_valid)
1609 {
1610 free_typebuf();
1611 typebuf = tp->save_typebuf;
1612 }
1613
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001614 old_char = tp->old_char;
1615 old_mod_mask = tp->old_mod_mask;
1616
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001617 free_buff(&readbuf1);
1618 readbuf1 = tp->save_readbuf1;
1619 free_buff(&readbuf2);
1620 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001622 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623# endif
1624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626/*
1627 * Open a new script file for the ":source!" command.
1628 */
1629 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001630openscript(
1631 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001632 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 if (curscript + 1 == NSCRIPT)
1635 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001636 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 return;
1638 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001639
1640 // Disallow sourcing a file in the sandbox, the commands would be executed
1641 // later, possibly outside of the sandbox.
1642 if (check_secure())
1643 return;
1644
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001645#ifdef FEAT_EVAL
1646 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001647 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001648 return;
1649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
Bram Moolenaar30613902019-12-01 22:11:18 +01001651 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001653 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 expand_env(name, NameBuff, MAXPATHL);
1655 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1656 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001657 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (curscript)
1659 --curscript;
1660 return;
1661 }
1662 if (save_typebuf() == FAIL)
1663 return;
1664
1665 /*
1666 * Execute the commands from the file right now when using ":source!"
1667 * after ":global" or ":argdo" or in a loop. Also when another command
1668 * follows. This means the display won't be updated. Don't do this
1669 * always, "make test" would fail.
1670 */
1671 if (directly)
1672 {
1673 oparg_T oa;
1674 int oldcurscript;
1675 int save_State = State;
1676 int save_restart_edit = restart_edit;
1677 int save_insertmode = p_im;
1678 int save_finish_op = finish_op;
1679 int save_msg_scroll = msg_scroll;
1680
Bram Moolenaar24959102022-05-07 20:01:16 +01001681 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001682 msg_scroll = FALSE; // no msg scrolling in Normal mode
1683 restart_edit = 0; // don't go to Insert mode
1684 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 clear_oparg(&oa);
1686 finish_op = FALSE;
1687
1688 oldcurscript = curscript;
1689 do
1690 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001691 update_topline_cursor(); // update cursor position and topline
1692 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001693 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 while (scriptin[oldcurscript] != NULL);
1696
1697 State = save_State;
1698 msg_scroll = save_msg_scroll;
1699 restart_edit = save_restart_edit;
1700 p_im = save_insertmode;
1701 finish_op = save_finish_op;
1702 }
1703}
1704
1705/*
1706 * Close the currently active input script.
1707 */
1708 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001709closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 free_typebuf();
1712 typebuf = saved_typebuf[curscript];
1713
1714 fclose(scriptin[curscript]);
1715 scriptin[curscript] = NULL;
1716 if (curscript > 0)
1717 --curscript;
1718}
1719
Bram Moolenaarea408852005-06-25 22:49:46 +00001720#if defined(EXITFREE) || defined(PROTO)
1721 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001722close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001723{
1724 while (scriptin[0] != NULL)
1725 closescript();
1726}
1727#endif
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729/*
1730 * Return TRUE when reading keys from a script file.
1731 */
1732 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001733using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 return scriptin[curscript] != NULL;
1736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
1738/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001739 * This function is called just before doing a blocking wait. Thus after
1740 * waiting 'updatetime' for a character to arrive.
1741 */
1742 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001743before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001744{
1745 updatescript(0);
1746#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001747 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001748 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001749#endif
1750}
1751
1752/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001753 * updatescript() is called when a character can be written into the script
1754 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 *
1756 * All the changed memfiles are synced if c == 0 or when the number of typed
1757 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1758 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001759 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001760updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 static int count = 0;
1763
1764 if (c && scriptout)
1765 putc(c, scriptout);
1766 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1767 {
1768 ml_sync_all(c == 0, TRUE);
1769 count = 0;
1770 }
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02001771#ifdef FEAT_EVAL
1772 if (typedchars_pos < MAXMAPLEN)
1773 {
1774 typedchars[typedchars_pos] = c;
1775 typedchars_pos++;
1776 }
1777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778}
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001781 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1782 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001783 */
1784 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001785merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001786{
1787 int c = c_arg;
1788
Bram Moolenaarea83c192023-03-18 17:22:46 +00001789 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001790 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001791 {
1792 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001793 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001794 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001795 if (c == NUL)
1796 c = K_ZERO;
1797 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001798 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001799 // CTRL-6 is equivalent to CTRL-^
1800 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001801#ifdef FEAT_GUI_GTK
1802 // These mappings look arbitrary at the first glance, but in fact
1803 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1804 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1805 // more sense and is consistent with usual terminal behaviour).
1806 else if (c == '2')
1807 c = NUL;
1808 else if (c >= '3' && c <= '7')
1809 c = c ^ 0x28;
1810 else if (c == '8')
1811 c = BS;
1812 else if (c == '?')
1813 c = DEL;
1814#endif
1815 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001816 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001817 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001818
1819 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001820 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001821 && c >= 0 && c <= 127)
1822 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001823 // Some terminals (esp. Kitty) do not include Shift in the character.
1824 // Apply it here to get consistency across terminals. Only do ASCII
1825 // letters, for other characters it depends on the keyboard layout.
1826 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1827 {
1828 c += 'a' - 'A';
1829 *modifiers &= ~MOD_MASK_SHIFT;
1830 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001831 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001832 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001833 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001834
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001835 return c;
1836}
1837
1838/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001839 * Add a single byte to 'showcmd' for a partially matched mapping.
1840 * Call add_to_showcmd() if a full key has been received.
1841 */
1842 static void
1843add_byte_to_showcmd(char_u byte)
1844{
1845 static gotchars_state_T state;
1846 char_u *ptr;
1847 int modifiers = 0;
1848 int c = NUL;
1849
1850 if (!p_sc || msg_silent != 0)
1851 return;
1852
1853 if (!gotchars_add_byte(&state, byte))
1854 return;
1855
1856 state.buf[state.buflen] = NUL;
1857 state.buflen = 0;
1858
1859 ptr = state.buf;
1860 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1861 {
1862 modifiers = ptr[2];
1863 ptr += 3;
1864 }
1865
1866 if (*ptr != NUL)
1867 {
1868 char_u *mb_ptr = mb_unescape(&ptr);
1869
1870 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1871 if (c <= 0x7f)
1872 {
1873 // Merge modifiers into the key to make the result more readable.
1874 int modifiers_after = modifiers;
1875 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1876
1877 if (modifiers_after == 0)
1878 {
1879 modifiers = 0;
1880 c = mod_c;
1881 }
1882 }
1883 }
1884
1885 // TODO: is there a more readable and yet compact representation of
1886 // modifiers and special keys?
1887 if (modifiers != 0)
1888 {
1889 add_to_showcmd(K_SPECIAL);
1890 add_to_showcmd(KS_MODIFIER);
1891 add_to_showcmd(modifiers);
1892 }
1893 if (c != NUL)
1894 add_to_showcmd(c);
1895 while (*ptr != NUL)
1896 add_to_showcmd(*ptr++);
1897}
1898
1899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 * Get the next input character.
1901 * Can return a special key or a multi-byte character.
1902 * Can return NUL when called recursively, use safe_vgetc() if that's not
1903 * wanted.
1904 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1905 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001906 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001909vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
1911 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001913 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001916#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001917 // Do garbage collection when garbagecollect() was called previously and
1918 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001919 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001920 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 /*
1924 * If a character was put back with vungetc, it was already processed.
1925 * Return it directly.
1926 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001927 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 c = old_char;
1930 old_char = -1;
1931 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001932 mouse_row = old_mouse_row;
1933 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
zeertzjq81b46a62022-04-09 17:58:49 +01001937 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001938 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001939
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001940 mod_mask = 0;
1941 vgetc_mod_mask = 0;
1942 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001943
1944 // last_recorded_len can be larger than last_vgetc_recorded_len
1945 // if peeking records more
1946 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001947
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001948 for (;;) // this is done twice if there are modifiers
1949 {
1950 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001951
Bram Moolenaar78aed952022-09-24 15:36:35 +01001952 // No mapping after modifier has been read, using an input method
1953 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001954 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001955#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001956 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001957#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001958#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001959 || popup_no_mapping()
1960#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001963 ++no_mapping;
1964 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001965 // mod_mask value may change, remember we did the increment
1966 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001968 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001969 if (did_inc)
1970 {
1971 --no_mapping;
1972 --allow_keys;
1973 }
1974
Christopher Plewright03193062022-11-22 12:58:27 +00001975 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001976 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001977#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001978 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001979#endif
1980 )
1981 {
1982 int save_allow_keys = allow_keys;
1983
1984 ++no_mapping;
1985 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001986 c2 = vgetorpeek(TRUE); // no mapping for these chars
1987 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001988 --no_mapping;
1989 allow_keys = save_allow_keys;
1990 if (c2 == KS_MODIFIER)
1991 {
1992 mod_mask = c;
1993 continue;
1994 }
1995 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001997 // K_ESC is used to avoid ambiguity with the single Esc
1998 // character that might be the start of an escape sequence.
1999 // Convert it back to a single Esc here.
2000 if (c == K_ESC)
2001 c = ESC;
2002
Bram Moolenaar4f974752019-02-17 17:44:42 +01002003#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002004 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
2005 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002006 if (
2007# ifdef VIMDLL
2008 gui.in_use &&
2009# endif
2010 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002012 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00002013 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002014
2015 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002016 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002017 {
K.Takata54119102022-02-03 13:33:03 +00002018 name[j] = c;
2019 if (j < 199)
2020 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002021 }
K.Takata54119102022-02-03 13:33:03 +00002022 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002023 gui_make_tearoff(name);
2024 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002027#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002028 // GTK: <F10> normally selects the menu, but it's passed until
2029 // here to allow mapping it. Intercept and invoke the GTK
2030 // behavior if it's not mapped.
2031 if (c == K_F10 && gui.menubar != NULL)
2032 {
2033 gtk_menu_shell_select_first(
2034 GTK_MENU_SHELL(gui.menubar), FALSE);
2035 continue;
2036 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002039 // Handle focus event here, so that the caller doesn't need to
2040 // know about it. Return K_IGNORE so that we loop once (needed
2041 // if 'lazyredraw' is set).
2042 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002043 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002044 ui_focus_change(c == K_FOCUSGAINED);
2045 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02002046 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002047
2048 // Translate K_CSI to CSI. The special key is only used to
2049 // avoid it being recognized as the start of a special key.
2050 if (c == K_CSI)
2051 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002053#ifdef FEAT_EVAL
2054 if (c == K_SID)
2055 {
2056 int j;
2057
2058 // Handle <SID>{sid}; Do up to 20 digits for safety.
2059 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002060 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002061 last_used_sid = last_used_sid * 10 + (c - '0');
2062 last_used_map = NULL;
2063 continue;
2064 }
2065#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002066 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002067
zeertzjq90a80022024-07-13 19:06:44 +02002068 // For a multi-byte character get all the bytes and return the
2069 // converted character.
2070 // Note: This will loop until enough bytes are received!
2071 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2072 {
2073 ++no_mapping;
2074 buf[0] = c;
2075 for (i = 1; i < n; ++i)
2076 {
2077 buf[i] = vgetorpeek(TRUE);
2078 if (buf[i] == K_SPECIAL
2079#ifdef FEAT_GUI
2080 || (buf[i] == CSI)
2081#endif
2082 )
2083 {
2084 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2085 // sequence, which represents a K_SPECIAL (0x80),
2086 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2087 // represents a CSI (0x9B),
2088 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2089 // too.
2090 c = vgetorpeek(TRUE);
2091 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2092 buf[i] = CSI;
2093 }
2094 }
2095 --no_mapping;
2096 c = (*mb_ptr2char)(buf);
2097 }
2098
2099 if (vgetc_char == 0)
2100 {
2101 vgetc_mod_mask = mod_mask;
2102 vgetc_char = c;
2103 }
2104
zeertzjqd9be94c2024-07-14 10:20:20 +02002105 // A keypad or special function key was not mapped, use it like
2106 // its ASCII equivalent.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002107 switch (c)
2108 {
2109 case K_KPLUS: c = '+'; break;
2110 case K_KMINUS: c = '-'; break;
2111 case K_KDIVIDE: c = '/'; break;
2112 case K_KMULTIPLY: c = '*'; break;
2113 case K_KENTER: c = CAR; break;
2114 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002115#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002116 // Can be either '.' or a ',',
2117 // depending on the type of keypad.
2118 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002119#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002120 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002121#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002122 case K_K0: c = '0'; break;
2123 case K_K1: c = '1'; break;
2124 case K_K2: c = '2'; break;
2125 case K_K3: c = '3'; break;
2126 case K_K4: c = '4'; break;
2127 case K_K5: c = '5'; break;
2128 case K_K6: c = '6'; break;
2129 case K_K7: c = '7'; break;
2130 case K_K8: c = '8'; break;
2131 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002132
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002133 case K_XHOME:
2134 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002135 {
2136 c = K_S_HOME;
2137 mod_mask = 0;
2138 }
2139 else if (mod_mask == MOD_MASK_CTRL)
2140 {
2141 c = K_C_HOME;
2142 mod_mask = 0;
2143 }
2144 else
2145 c = K_HOME;
2146 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002147 case K_XEND:
2148 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002149 {
2150 c = K_S_END;
2151 mod_mask = 0;
2152 }
2153 else if (mod_mask == MOD_MASK_CTRL)
2154 {
2155 c = K_C_END;
2156 mod_mask = 0;
2157 }
2158 else
2159 c = K_END;
2160 break;
2161
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002162 case K_XUP: c = K_UP; break;
2163 case K_XDOWN: c = K_DOWN; break;
2164 case K_XLEFT: c = K_LEFT; break;
2165 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002168 break;
2169 }
zeertzjq81b46a62022-04-09 17:58:49 +01002170
2171 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002173
2174#ifdef FEAT_EVAL
2175 /*
2176 * In the main loop "may_garbage_collect" can be set to do garbage
2177 * collection in the first next vgetc(). It's disabled after that to
2178 * avoid internally used Lists and Dicts to be freed.
2179 */
2180 may_garbage_collect = FALSE;
2181#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002182
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002183#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002184 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002185 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002186 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002187 bevalexpr_due_set = FALSE;
2188 ui_remove_balloon();
2189 }
2190#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002191#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002192 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2193 // are filtered.
2194 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002195 {
2196 if (c == Ctrl_C)
2197 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002198 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002199 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002200#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002201
Shougo Matsushita83678842024-07-11 22:05:12 +02002202#ifdef FEAT_EVAL
2203 c = do_key_input_pre(c);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002204
2205 // Clear the next typedchars_pos
2206 typedchars_pos = 0;
Shougo Matsushita83678842024-07-11 22:05:12 +02002207#endif
2208
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002209 // Need to process the character before we know it's safe to do something
2210 // else.
2211 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002212 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002213
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002214 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
Shougo Matsushita83678842024-07-11 22:05:12 +02002217#ifdef FEAT_EVAL
2218/*
2219 * Handle the InsertCharPre autocommand.
2220 * "c" is the character that was typed.
2221 * Return new input character.
2222 */
2223 static int
2224do_key_input_pre(int c)
2225{
2226 int res = c;
2227 char_u buf[MB_MAXBYTES + 1];
2228 char_u curr_mode[MODE_MAX_LENGTH];
2229 int save_State = State;
2230 save_v_event_T save_v_event;
2231 dict_T *v_event;
2232
2233 // Return quickly when there is nothing to do.
2234 if (!has_keyinputpre())
2235 return res;
2236
2237 if (IS_SPECIAL(c))
2238 {
2239 buf[0] = K_SPECIAL;
2240 buf[1] = KEY2TERMCAP0(c);
2241 buf[2] = KEY2TERMCAP1(c);
2242 buf[3] = NUL;
2243 }
2244 else
2245 buf[(*mb_char2bytes)(c, buf)] = NUL;
2246
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002247 typedchars[typedchars_pos] = NUL;
2248 vim_unescape_csi(typedchars);
2249
Shougo Matsushita83678842024-07-11 22:05:12 +02002250 get_mode(curr_mode);
2251
2252 // Lock the text to avoid weird things from happening.
2253 ++textlock;
2254 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2255
2256 v_event = get_v_event(&save_v_event);
2257 (void)dict_add_bool(v_event, "typed", KeyTyped);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002258 (void)dict_add_string(v_event, "typedchar", typedchars);
Shougo Matsushita83678842024-07-11 22:05:12 +02002259
2260 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2261 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2262 {
2263 // Get the value of v:char. It may be empty or more than one
2264 // character. Only use it when changed, otherwise continue with the
2265 // original character.
2266 char_u *v_char;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002267 size_t v_charlen;
Shougo Matsushita83678842024-07-11 22:05:12 +02002268
2269 v_char = get_vim_var_str(VV_CHAR);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002270 v_charlen = STRLEN(v_char);
Shougo Matsushita83678842024-07-11 22:05:12 +02002271
2272 // Convert special bytes when it is special string.
John Marriotte7a1bbf2024-11-11 20:40:33 +01002273 if (v_charlen >= 3 && v_char[0] == K_SPECIAL)
Shougo Matsushita83678842024-07-11 22:05:12 +02002274 res = TERMCAP2KEY(v_char[1], v_char[2]);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002275 else if (v_charlen > 0)
Shougo Matsushita83678842024-07-11 22:05:12 +02002276 res = PTR2CHAR(v_char);
2277 }
2278
2279 restore_v_event(v_event, &save_v_event);
2280
2281 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2282 --textlock;
2283
2284 // Restore the State, it may have been changed.
2285 State = save_State;
2286
2287 return res;
2288}
2289#endif
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291/*
2292 * Like vgetc(), but never return a NUL when called recursively, get a key
2293 * directly from the user (ignoring typeahead).
2294 */
2295 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002296safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 int c;
2299
2300 c = vgetc();
2301 if (c == NUL)
2302 c = get_keystroke();
2303 return c;
2304}
2305
2306/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002307 * Like safe_vgetc(), but loop to handle K_IGNORE.
2308 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002309 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002310 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002311 static int
2312plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002313{
2314 int c;
2315
2316 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002317 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002318 while (c == K_IGNORE
2319 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2320 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002321 return c;
2322}
2323
2324/*
2325 * Like safe_vgetc(), but loop to handle K_IGNORE.
2326 * Also ignore scrollbar events.
2327 */
2328 int
2329plain_vgetc(void)
2330{
2331 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002332
2333 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002334 // Only handle the first pasted character. Drop the rest, since we
2335 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002336 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2337
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002338 return c;
2339}
2340
2341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 * Check if a character is available, such that vgetc() will not block.
2343 * If the next character is a special character or multi-byte, the returned
2344 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002345 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 */
2347 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002348vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002350 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002352 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353}
2354
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002355#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356/*
2357 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2358 * codes.
2359 */
2360 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002361vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 int c;
2364
2365 ++no_mapping;
2366 ++allow_keys;
2367 c = vpeekc();
2368 --no_mapping;
2369 --allow_keys;
2370 return c;
2371}
2372#endif
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374/*
2375 * Check if any character is available, also half an escape sequence.
2376 * Trick: when no typeahead found, but there is something in the typeahead
2377 * buffer, it must be an ESC that is recognized as the start of a key code.
2378 */
2379 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002380vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 int c;
2383
2384 c = vpeekc();
2385 if (c == NUL && typebuf.tb_len > 0)
2386 c = ESC;
2387 return c;
2388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390/*
2391 * Call vpeekc() without causing anything to be mapped.
2392 * Return TRUE if a character is available, FALSE otherwise.
2393 */
2394 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002395char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 int retval;
2398
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002399#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002400 // When test_override("char_avail", 1) was called pretend there is no
2401 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002402 if (disable_char_avail_for_testing)
2403 return FALSE;
2404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 ++no_mapping;
2406 retval = vpeekc();
2407 --no_mapping;
2408 return (retval != NUL);
2409}
2410
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002411#if defined(FEAT_EVAL) || defined(PROTO)
2412/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002413 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002414 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002415 static void
zeertzjqe0a2ab32025-02-02 09:14:35 +01002416getchar_common(typval_T *argvars, typval_T *rettv, int allow_number)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002417{
zeertzjqedf0f7d2025-02-02 19:01:01 +01002418 varnumber_T n = 0;
2419 int called_emsg_start = called_emsg;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002420 int error = FALSE;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002421 int simplify = TRUE;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002422 char_u cursor_flag = 'm';
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002423
zeertzjqe0a2ab32025-02-02 09:14:35 +01002424 if ((in_vim9script()
2425 && check_for_opt_bool_or_number_arg(argvars, 0) == FAIL)
2426 || (argvars[0].v_type != VAR_UNKNOWN
2427 && check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002428 return;
2429
zeertzjqe0a2ab32025-02-02 09:14:35 +01002430 if (argvars[0].v_type != VAR_UNKNOWN && argvars[1].v_type == VAR_DICT)
2431 {
2432 dict_T *d = argvars[1].vval.v_dict;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002433 char_u *cursor_str;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002434
2435 if (allow_number)
2436 allow_number = dict_get_bool(d, "number", TRUE);
2437 else if (dict_has_key(d, "number"))
zeertzjqe0a2ab32025-02-02 09:14:35 +01002438 semsg(_(e_invalid_argument_str), "number");
zeertzjqe0a2ab32025-02-02 09:14:35 +01002439
2440 simplify = dict_get_bool(d, "simplify", TRUE);
zeertzjqedf0f7d2025-02-02 19:01:01 +01002441
2442 cursor_str = dict_get_string(d, "cursor", FALSE);
2443 if (cursor_str != NULL)
2444 {
2445 if (STRCMP(cursor_str, "hide") != 0
2446 && STRCMP(cursor_str, "keep") != 0
2447 && STRCMP(cursor_str, "msg") != 0)
2448 semsg(_(e_invalid_value_for_argument_str_str), "cursor",
2449 cursor_str);
2450 else
2451 cursor_flag = cursor_str[0];
2452 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002453 }
2454
zeertzjqedf0f7d2025-02-02 19:01:01 +01002455 if (called_emsg != called_emsg_start)
2456 return;
2457
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002458#ifdef MESSAGE_QUEUE
2459 // vpeekc() used to check for messages, but that caused problems, invoking
2460 // a callback where it was not expected. Some plugins use getchar(1) in a
2461 // loop to await a message, therefore make sure we check for messages here.
2462 parse_queued_messages();
2463#endif
2464
zeertzjqedf0f7d2025-02-02 19:01:01 +01002465 if (cursor_flag == 'h')
2466 cursor_sleep();
2467 else if (cursor_flag == 'm')
2468 windgoto(msg_row, msg_col);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002469
2470 ++no_mapping;
2471 ++allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002472 if (!simplify)
2473 ++no_reduce_keys;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002474 for (;;)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002475 {
zeertzjqe0a2ab32025-02-02 09:14:35 +01002476 if (argvars[0].v_type == VAR_UNKNOWN
2477 || (argvars[0].v_type == VAR_NUMBER
2478 && argvars[0].vval.v_number == -1))
Bram Moolenaar30613902019-12-01 22:11:18 +01002479 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002480 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002481 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002482 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002483 n = vpeekc_any();
2484 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002485 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002486 n = 0;
2487 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002488 // getchar(0) and char avail() != NUL: get a character.
2489 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2490 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002491
Bram Moolenaar15183b42020-09-05 19:59:39 +02002492 if (n == K_IGNORE || n == K_MOUSEMOVE
2493 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002494 continue;
2495 break;
2496 }
2497 --no_mapping;
2498 --allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002499 if (!simplify)
2500 --no_reduce_keys;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002501
zeertzjqedf0f7d2025-02-02 19:01:01 +01002502 if (cursor_flag == 'h')
2503 cursor_unsleep();
2504
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002505 set_vim_var_nr(VV_MOUSE_WIN, 0);
2506 set_vim_var_nr(VV_MOUSE_WINID, 0);
2507 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2508 set_vim_var_nr(VV_MOUSE_COL, 0);
2509
zeertzjqe0a2ab32025-02-02 09:14:35 +01002510 if (n != 0 && (!allow_number || IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002511 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002512 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002513 int i = 0;
2514
Bram Moolenaar30613902019-12-01 22:11:18 +01002515 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002516 if (mod_mask != 0)
2517 {
2518 temp[i++] = K_SPECIAL;
2519 temp[i++] = KS_MODIFIER;
2520 temp[i++] = mod_mask;
2521 }
2522 if (IS_SPECIAL(n))
2523 {
2524 temp[i++] = K_SPECIAL;
2525 temp[i++] = K_SECOND(n);
2526 temp[i++] = K_THIRD(n);
2527 }
2528 else if (has_mbyte)
2529 i += (*mb_char2bytes)(n, temp + i);
2530 else
2531 temp[i++] = n;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002532 temp[i] = NUL;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002533 rettv->v_type = VAR_STRING;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002534 rettv->vval.v_string = vim_strnsave(temp, i);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002535
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002536 if (is_mouse_key(n))
2537 {
2538 int row = mouse_row;
2539 int col = mouse_col;
2540 win_T *win;
2541 linenr_T lnum;
2542 win_T *wp;
2543 int winnr = 1;
2544
2545 if (row >= 0 && col >= 0)
2546 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002547 // Find the window at the mouse coordinates and compute the
2548 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002549 win = mouse_find_win(&row, &col, FIND_POPUP);
2550 if (win == NULL)
2551 return;
2552 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002553#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002554 if (WIN_IS_POPUP(win))
2555 winnr = 0;
2556 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002557#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002558 for (wp = firstwin; wp != win && wp != NULL;
2559 wp = wp->w_next)
2560 ++winnr;
2561 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2562 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2563 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2564 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2565 }
2566 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002567 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002568 else if (!allow_number)
2569 rettv->v_type = VAR_STRING;
2570 else
2571 rettv->vval.v_number = n;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002572}
2573
2574/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002575 * "getchar()" function
2576 */
2577 void
2578f_getchar(typval_T *argvars, typval_T *rettv)
2579{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002580 getchar_common(argvars, rettv, TRUE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002581}
2582
2583/*
2584 * "getcharstr()" function
2585 */
2586 void
2587f_getcharstr(typval_T *argvars, typval_T *rettv)
2588{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002589 getchar_common(argvars, rettv, FALSE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002590}
2591
2592/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002593 * "getcharmod()" function
2594 */
2595 void
2596f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2597{
2598 rettv->vval.v_number = mod_mask;
2599}
2600#endif // FEAT_EVAL
2601
2602#if defined(MESSAGE_QUEUE) || defined(PROTO)
2603# define MAX_REPEAT_PARSE 8
2604
2605/*
2606 * Process messages that have been queued for netbeans or clientserver.
2607 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002608 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002609 * it is safe to do so.
2610 */
2611 void
2612parse_queued_messages(void)
2613{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002614 int old_curwin_id;
2615 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002616 int i;
2617 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002618 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002619 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002620
2621 // Do not handle messages while redrawing, because it may cause buffers to
2622 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002623 // Also bail out when parsing messages was explicitly disabled.
2624 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002625 return;
2626
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002627 // If memory allocation fails during startup we'll exit but curbuf or
2628 // curwin could be NULL.
2629 if (curbuf == NULL || curwin == NULL)
2630 return;
2631
2632 old_curbuf_fnum = curbuf->b_fnum;
2633 old_curwin_id = curwin->w_id;
2634
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002635 ++entered;
2636
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002637 // may_garbage_collect is set in main_loop() to do garbage collection when
2638 // blocking to wait on a character. We don't want that while parsing
2639 // messages, a callback may invoke vgetc() while lists and dicts are in use
2640 // in the call stack.
2641 may_garbage_collect = FALSE;
2642
2643 // Loop when a job ended, but don't keep looping forever.
2644 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2645 {
2646 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002647# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002648 channel_handle_events(FALSE);
2649# endif
2650
2651# ifdef FEAT_NETBEANS_INTG
2652 // Process the queued netbeans messages.
2653 netbeans_parse_messages();
2654# endif
2655# ifdef FEAT_JOB_CHANNEL
2656 // Write any buffer lines still to be written.
2657 channel_write_any_lines();
2658
2659 // Process the messages queued on channels.
2660 channel_parse_messages();
2661# endif
2662# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2663 // Process the queued clientserver messages.
2664 server_parse_messages();
2665# endif
2666# ifdef FEAT_JOB_CHANNEL
2667 // Check if any jobs have ended. If so, repeat the above to handle
2668 // changes, e.g. stdin may have been closed.
2669 if (job_check_ended())
2670 continue;
2671# endif
2672# ifdef FEAT_TERMINAL
2673 free_unused_terminals();
2674# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002675
2676# ifdef FEAT_SOUND_MACOSX
2677 process_cfrunloop();
2678# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002679# ifdef FEAT_SOUND_CANBERRA
2680 if (has_sound_callback_in_queue())
2681 invoke_sound_callback();
2682# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002683#ifdef SIGUSR1
2684 if (got_sigusr1)
2685 {
2686 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2687 got_sigusr1 = FALSE;
2688 }
2689#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002690 break;
2691 }
2692
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002693 // When not nested we'll go back to waiting for a typed character. If it
2694 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002695 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002696 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002697
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002698 may_garbage_collect = save_may_garbage_collect;
2699
2700 // If the current window or buffer changed we need to bail out of the
2701 // waiting loop. E.g. when a job exit callback closes the terminal window.
2702 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002703 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002704
2705 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002706}
2707#endif
2708
2709
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002710typedef enum {
2711 map_result_fail, // failed, break loop
2712 map_result_get, // get a character from typeahead
2713 map_result_retry, // try to map again
2714 map_result_nomatch // no matching mapping, get char
2715} map_result_T;
2716
2717/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002718 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002719 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002720 */
2721 static int
zeertzjqee446032022-04-30 15:02:22 +01002722at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002723{
2724 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2725 int c = *p;
2726
2727 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002728 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002729 && p[1] == KS_MODIFIER
2730 && (p[2] & MOD_MASK_CTRL))
2731 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002732 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2733 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002734}
2735
2736/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002737 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002738 * into just a key, apply that.
2739 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2740 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002741 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002742 */
2743 static int
2744check_simplify_modifier(int max_offset)
2745{
2746 int offset;
2747 char_u *tp;
2748
2749 for (offset = 0; offset < max_offset; ++offset)
2750 {
2751 if (offset + 3 >= typebuf.tb_len)
2752 break;
2753 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002754 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002755 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002756 // A modifier was not used for a mapping, apply it to ASCII keys.
2757 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002758 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002759 int c = tp[3];
2760 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002761
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002762 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002763 {
2764 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002765 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002766
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002767 if (offset == 0)
2768 {
2769 // At the start: remember the character and mod_mask before
2770 // merging, in some cases, e.g. at the hit-return prompt,
2771 // they are put back in the typeahead buffer.
2772 vgetc_char = c;
2773 vgetc_mod_mask = tp[2];
2774 }
zeertzjq12e21e32022-04-27 11:58:01 +01002775 if (IS_SPECIAL(new_c))
2776 {
2777 new_string[0] = K_SPECIAL;
2778 new_string[1] = K_SECOND(new_c);
2779 new_string[2] = K_THIRD(new_c);
2780 len = 3;
2781 }
2782 else
2783 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002784 if (modifier == 0)
2785 {
2786 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002787 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002788 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002789 }
2790 else
2791 {
2792 tp[2] = modifier;
2793 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002794 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002795 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002796 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002797 return len;
2798 }
2799 }
2800 }
2801 return 0;
2802}
2803
2804/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002805 * Return TRUE if the terminal sends modifiers with various keys. This is when
2806 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2807 * enabled.
2808 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002809 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002810key_protocol_enabled(void)
2811{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002812 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2813 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2814 ? modify_otherkeys_state == MOKS_ENABLED
2815 : seenModifyOtherKeys;
2816 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002817}
2818
2819/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002820 * Handle mappings in the typeahead buffer.
2821 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002822 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002823 * - When there is no match yet, return map_result_nomatch, need to get more
2824 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002825 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002826 */
2827 static int
2828handle_mapping(
2829 int *keylenp,
2830 int *timedout,
2831 int *mapdepth)
2832{
2833 mapblock_T *mp = NULL;
2834 mapblock_T *mp2;
2835 mapblock_T *mp_match;
2836 int mp_match_len = 0;
2837 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002838 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002839 int tb_c1;
2840 int mlen;
2841#ifdef FEAT_LANGMAP
2842 int nolmaplen;
2843#endif
2844 int keylen = *keylenp;
2845 int i;
2846 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002847 int is_plug_map = FALSE;
2848
zeertzjqbb404f52022-07-23 06:25:29 +01002849 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002850 if (typebuf.tb_len >= 3
2851 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002852 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2853 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2854 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855
2856 /*
2857 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002858 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 *
2860 * Don't look for mappings if:
2861 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2862 * - maphash_valid not set: no mappings present.
2863 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2864 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002865 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002866 * - waiting for a char with --more--
2867 * - in Ctrl-X mode, and we get a valid char for that mode
2868 */
2869 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2870 if (no_mapping == 0 && is_maphash_valid()
2871 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002872 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002873 || (p_remap
2874 && (typebuf.tb_noremap[typebuf.tb_off]
2875 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002876 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2877 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2878 && State != MODE_ASKMORE
2879 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002880 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002881 {
Christopher Plewright03193062022-11-22 12:58:27 +00002882#ifdef FEAT_GUI
2883 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2884 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002885 {
2886 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2887 // K_SPECIAL KS_MODIFIER {flags}.
2888 tb_c1 = K_SPECIAL;
2889 }
2890#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002891#ifdef FEAT_LANGMAP
2892 if (tb_c1 == K_SPECIAL)
2893 nolmaplen = 2;
2894 else
2895 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002896 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2897 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002898 nolmaplen = 0;
2899 }
2900#endif
2901 // First try buffer-local mappings.
2902 mp = get_buf_maphash_list(local_State, tb_c1);
2903 mp2 = get_maphash_list(local_State, tb_c1);
2904 if (mp == NULL)
2905 {
2906 // There are no buffer-local mappings.
2907 mp = mp2;
2908 mp2 = NULL;
2909 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002910
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002911 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002912 * Loop until a partly matching mapping is found or all (local)
2913 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002914 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002915 * A full match is only accepted if there is no partly match, so "aa"
2916 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002917 */
2918 mp_match = NULL;
2919 mp_match_len = 0;
2920 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002921 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002922 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002923 // Only consider an entry if the first character matches and it is
2924 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002925 // Skip ":lmap" mappings if keys were mapped.
2926 if (mp->m_keys[0] == tb_c1
2927 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002928 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002929 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002930 && ((mp->m_mode & MODE_LANGMAP) == 0
2931 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002932 {
2933#ifdef FEAT_LANGMAP
2934 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002935 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002936#endif
2937 // find the match length of this mapping
2938 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2939 {
zeertzjq49660f52022-10-20 17:59:38 +01002940 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002941#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002942 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002943 {
2944 if (nomap == 2 && c2 == KS_MODIFIER)
2945 modifiers = 1;
2946 else if (nomap == 1 && modifiers == 1)
2947 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002948 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002949 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002950 else
zeertzjq49660f52022-10-20 17:59:38 +01002951 {
2952 if (c2 == K_SPECIAL)
2953 nomap = 2;
2954 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2955 // Only apply 'langmap' if merging modifiers into
2956 // the key will not result in another character,
2957 // so that 'langmap' behaves consistently in
2958 // different terminals and GUIs.
2959 LANGMAP_ADJUST(c2, TRUE);
2960 modifiers = 0;
2961 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002962#endif
zeertzjq49660f52022-10-20 17:59:38 +01002963 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002964 break;
2965 }
2966
Bram Moolenaareda35f72019-08-03 14:59:44 +02002967 // Don't allow mapping the first byte(s) of a multi-byte char.
2968 // Happens when mapping <M-a> and then changing 'encoding'.
2969 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002970 {
2971 char_u *p1 = mp->m_keys;
2972 char_u *p2 = mb_unescape(&p1);
2973
2974 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002975 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002976 mlen = 0;
2977 }
2978
2979 // Check an entry whether it matches.
2980 // - Full match: mlen == keylen
2981 // - Partly match: mlen == typebuf.tb_len
2982 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002983 if (mlen == keylen || (mlen == typebuf.tb_len
2984 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002985 {
2986 char_u *s;
2987 int n;
2988
Bram Moolenaareda35f72019-08-03 14:59:44 +02002989 // If only script-local mappings are allowed, check if the
2990 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002991 s = typebuf.tb_noremap + typebuf.tb_off;
2992 if (*s == RM_SCRIPT
2993 && (mp->m_keys[0] != K_SPECIAL
2994 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002995 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002996 continue;
2997
Bram Moolenaareda35f72019-08-03 14:59:44 +02002998 // If one of the typed keys cannot be remapped, skip the
2999 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003000 for (n = mlen; --n >= 0; )
3001 if (*s++ & (RM_NONE|RM_ABBR))
3002 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00003003 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003004 continue;
3005
3006 if (keylen > typebuf.tb_len)
3007 {
3008 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003009 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003010 {
3011 // break at a partly match
3012 keylen = KEYLEN_PART_MAP;
3013 break;
3014 }
3015 }
3016 else if (keylen > mp_match_len)
3017 {
3018 // found a longer match
3019 mp_match = mp;
3020 mp_match_len = keylen;
3021 }
3022 }
3023 else
Oleg Goncharov56904f92024-07-23 20:34:15 +02003024 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003025 // No match; may have to check for termcode at next
Oleg Goncharov56904f92024-07-23 20:34:15 +02003026 // character.
3027
3028 // If the first character that didn't match is
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003029 // K_SPECIAL then check for a termcode. This isn't perfect
3030 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003031 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003032 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003033 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003034 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
3035 }
3036 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
3037 want_termcode = 1;
Oleg Goncharov56904f92024-07-23 20:34:15 +02003038
3039 // Check termcode for uppercase character to properly
3040 // process "ESC[27;2;<ascii code>~" control sequences.
3041 if (ASCII_ISUPPER(mp->m_keys[mlen]))
3042 want_termcode = 1;
3043 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003044 }
3045 }
3046
Bram Moolenaareda35f72019-08-03 14:59:44 +02003047 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00003048 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003049 {
3050 mp = mp_match;
3051 keylen = mp_match_len;
3052 }
3053 }
3054
3055 /*
3056 * Check for match with 'pastetoggle'
3057 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003058 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003059 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003060 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
3061 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003062 break;
3063 if (p_pt[mlen] == NUL) // match
3064 {
3065 // write chars to script file(s)
3066 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003067 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3068 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003069
3070 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003071 set_option_value_give_err((char_u *)"paste",
3072 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01003073 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003074 {
3075 msg_col = 0;
3076 msg_row = Rows - 1;
3077 msg_clr_eos(); // clear ruler
3078 }
3079 status_redraw_all();
3080 redraw_statuslines();
3081 showmode();
3082 setcursor();
3083 *keylenp = keylen;
3084 return map_result_retry;
3085 }
3086 // Need more chars for partly match.
3087 if (mlen == typebuf.tb_len)
3088 keylen = KEYLEN_PART_KEY;
3089 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003090 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003091 max_mlen = mlen + 1;
3092 }
3093
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003094 // May check for a terminal code when there is no mapping or only a partial
3095 // mapping. Also check if there is a full mapping with <Esc>, unless timed
3096 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003097 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003098 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
3099 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003100 {
3101 int save_keylen = keylen;
3102
3103 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003104 * When no matching mapping found or found a non-matching mapping that
3105 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003106 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02003107 * - mapping is allowed,
3108 * - keys have not been mapped,
3109 * - and not an ESC sequence, not in insert mode or p_ek is on,
3110 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003111 */
zeertzjq68a573c2022-04-28 14:10:01 +01003112 if (no_mapping == 0 || allow_keys != 0)
3113 {
3114 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003115 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003116 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003117 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003118 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3119 else
3120 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003121
Bram Moolenaar0684e362020-12-03 19:54:42 +01003122 // If no termcode matched but 'pastetoggle' matched partially
3123 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003124 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003125 keylen = KEYLEN_PART_KEY;
3126
Bram Moolenaar975a8802020-06-06 22:36:24 +02003127 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003128 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003129#ifdef FEAT_TERMINAL
3130 check_no_reduce_keys(); // may update the no_reduce_keys flag
3131#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003132 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003133 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003134 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003135 if (keylen < 0)
3136 // ins_typebuf() failed
3137 return map_result_fail;
3138 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003139
Bram Moolenaareda35f72019-08-03 14:59:44 +02003140 // When getting a partial match, but the last characters were not
3141 // typed, don't wait for a typed character to complete the
3142 // termcode. This helps a lot when a ":normal" command ends in an
3143 // ESC.
3144 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003145 keylen = 0;
3146 }
3147 else
3148 keylen = 0;
3149 if (keylen == 0) // no matching terminal code
3150 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003151#ifdef AMIGA
3152 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003153 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003154 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003155 {
3156 char_u *s;
3157
3158 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003159 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3160 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003161 ++s)
3162 ;
3163 if (*s == 'r' || *s == '|') // found one
3164 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003165 del_typebuf(
3166 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003167 // get size and redraw screen
3168 shell_resized();
3169 *keylenp = keylen;
3170 return map_result_retry;
3171 }
3172 if (*s == NUL) // need more characters
3173 keylen = KEYLEN_PART_KEY;
3174 }
3175 if (keylen >= 0)
3176#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003177 // When there was a matching mapping and no termcode could be
3178 // replaced after another one, use that mapping (loop around).
3179 // If there was no mapping at all use the character from the
3180 // typeahead buffer right here.
3181 if (mp == NULL)
3182 {
3183 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003184 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003185 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003186 }
3187
3188 if (keylen > 0) // full matching terminal code
3189 {
3190#if defined(FEAT_GUI) && defined(FEAT_MENU)
3191 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003192 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3193 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003194 {
3195 int idx;
3196
Bram Moolenaareda35f72019-08-03 14:59:44 +02003197 // Using a menu may cause a break in undo! It's like using
3198 // gotchars(), but without recording or writing to a script
3199 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003200 may_sync_undo();
3201 del_typebuf(3, 0);
3202 idx = get_menu_index(current_menu, local_State);
3203 if (idx != MENU_INDEX_INVALID)
3204 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003205 // In Select mode and a Visual mode menu is used: Switch
3206 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003207 // back to Select mode.
3208 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003209 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003210 {
3211 VIsual_select = FALSE;
3212 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003213 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003214 }
3215 ins_typebuf(current_menu->strings[idx],
3216 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003217 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003218 }
3219 }
3220#endif // FEAT_GUI && FEAT_MENU
3221 *keylenp = keylen;
3222 return map_result_retry; // try mapping again
3223 }
3224
Bram Moolenaareda35f72019-08-03 14:59:44 +02003225 // Partial match: get some more characters. When a matching mapping
3226 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003227 if (mp == NULL || keylen < 0)
3228 keylen = KEYLEN_PART_KEY;
3229 else
3230 keylen = mp_match_len;
3231 }
3232
3233 /*
3234 * complete match
3235 */
3236 if (keylen >= 0 && keylen <= typebuf.tb_len)
3237 {
3238 char_u *map_str;
3239
3240#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003241 int save_m_expr;
3242 int save_m_noremap;
3243 int save_m_silent;
3244 char_u *save_m_keys;
zeertzjq9d997ad2024-07-29 21:10:07 +02003245 char_u *save_alt_m_keys;
zeertzjq74011dc2024-07-30 19:17:56 +02003246 int save_alt_m_keylen;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003247#else
3248# define save_m_noremap mp->m_noremap
3249# define save_m_silent mp->m_silent
3250#endif
3251
3252 // write chars to script file(s)
3253 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003254 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3255 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003256
3257 cmd_silent = (typebuf.tb_silent > 0);
3258 del_typebuf(keylen, 0); // remove the mapped keys
3259
3260 /*
3261 * Put the replacement string in front of mapstr.
3262 * The depth check catches ":map x y" and ":map y x".
3263 */
3264 if (++*mapdepth >= p_mmd)
3265 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003266 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003267 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003268 redrawcmdline();
3269 else
3270 setcursor();
3271 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003272 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003273 *keylenp = keylen;
3274 return map_result_fail;
3275 }
3276
3277 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003278 * In Select mode and a Visual mode mapping is used: Switch to Visual
3279 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003280 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003281 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003282 {
3283 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003284 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003285 }
3286
3287#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003288 // Copy the values from *mp that are used, because evaluating the
3289 // expression may invoke a function that redefines the mapping, thereby
3290 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003291 save_m_expr = mp->m_expr;
3292 save_m_noremap = mp->m_noremap;
3293 save_m_silent = mp->m_silent;
3294 save_m_keys = NULL; // only saved when needed
zeertzjq9d997ad2024-07-29 21:10:07 +02003295 save_alt_m_keys = NULL; // only saved when needed
zeertzjq74011dc2024-07-30 19:17:56 +02003296 save_alt_m_keylen = mp->m_alt != NULL ? mp->m_alt->m_keylen : 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003297
3298 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003299 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3300 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003301 */
3302 if (mp->m_expr)
3303 {
3304 int save_vgetc_busy = vgetc_busy;
3305 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003306 int was_screen_col = screen_cur_col;
3307 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003308 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003309
3310 vgetc_busy = 0;
3311 may_garbage_collect = FALSE;
3312
zeertzjq74011dc2024-07-30 19:17:56 +02003313 save_m_keys = vim_strnsave(mp->m_keys, (size_t)mp->m_keylen);
zeertzjq9d997ad2024-07-29 21:10:07 +02003314 save_alt_m_keys = mp->m_alt != NULL
zeertzjq74011dc2024-07-30 19:17:56 +02003315 ? vim_strnsave(mp->m_alt->m_keys,
3316 (size_t)save_alt_m_keylen) : NULL;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003317 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003318
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003319 // The mapping may do anything, but we expect it to take care of
3320 // redrawing. Do put the cursor back where it was.
3321 windgoto(was_screen_row, was_screen_col);
3322 out_flush();
3323
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003324 // If an error was displayed and the expression returns an empty
3325 // string, generate a <Nop> to allow for a redraw.
3326 if (prev_did_emsg != did_emsg
3327 && (map_str == NULL || *map_str == NUL))
3328 {
3329 char_u buf[4];
3330
3331 vim_free(map_str);
3332 buf[0] = K_SPECIAL;
3333 buf[1] = KS_EXTRA;
3334 buf[2] = KE_IGNORE;
3335 buf[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +01003336 map_str = vim_strnsave(buf, 3);
Bram Moolenaar24959102022-05-07 20:01:16 +01003337 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003338 {
3339 // redraw the command below the error
3340 msg_didout = TRUE;
3341 if (msg_row < cmdline_row)
3342 msg_row = cmdline_row;
3343 redrawcmd();
3344 }
3345 }
3346
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003347 vgetc_busy = save_vgetc_busy;
3348 may_garbage_collect = save_may_garbage_collect;
3349 }
3350 else
3351#endif
3352 map_str = mp->m_str;
3353
3354 /*
3355 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003356 * If 'from' field is the same as the start of the 'to' field, don't
3357 * remap the first character (but do allow abbreviations).
3358 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003359 */
3360 if (map_str == NULL)
3361 i = FAIL;
3362 else
3363 {
3364 int noremap;
3365
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003366#ifdef FEAT_EVAL
3367 last_used_map = mp;
3368 last_used_sid = -1;
3369#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003370 if (save_m_noremap != REMAP_YES)
3371 noremap = save_m_noremap;
3372 else if (
3373#ifdef FEAT_EVAL
zeertzjq9d997ad2024-07-29 21:10:07 +02003374 save_m_expr ?
3375 (save_m_keys != NULL
3376 && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
3377 || (save_alt_m_keys != NULL
3378 && STRNCMP(map_str, save_alt_m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003379 (size_t)save_alt_m_keylen) == 0) :
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003380#endif
zeertzjq9d997ad2024-07-29 21:10:07 +02003381 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
3382 || (mp->m_alt != NULL
3383 && STRNCMP(map_str, mp->m_alt->m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003384 (size_t)mp->m_alt->m_keylen) == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003385 noremap = REMAP_SKIP;
zeertzjq9d997ad2024-07-29 21:10:07 +02003386 else
3387 noremap = REMAP_YES;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003388 i = ins_typebuf(map_str, noremap,
3389 0, TRUE, cmd_silent || save_m_silent);
3390#ifdef FEAT_EVAL
3391 if (save_m_expr)
3392 vim_free(map_str);
3393#endif
3394 }
3395#ifdef FEAT_EVAL
3396 vim_free(save_m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003397 vim_free(save_alt_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003398#endif
3399 *keylenp = keylen;
3400 if (i == FAIL)
3401 return map_result_fail;
3402 return map_result_retry;
3403 }
3404
3405 *keylenp = keylen;
3406 return map_result_nomatch;
3407}
3408
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003409/*
3410 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003411 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003412 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003415vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 old_char = c;
3418 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003419 old_mouse_row = mouse_row;
3420 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003421 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422}
3423
3424/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003425 * When peeking and not getting a character, reg_executing cannot be cleared
3426 * yet, so set a flag to clear it later.
3427 */
3428 static void
3429check_end_reg_executing(int advance)
3430{
3431 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3432 || pending_end_reg_executing))
3433 {
3434 if (advance)
3435 {
3436 reg_executing = 0;
3437 pending_end_reg_executing = FALSE;
3438 }
3439 else
3440 pending_end_reg_executing = TRUE;
3441 }
3442
3443}
3444
3445/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003446 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 * 1. from the stuffbuffer
3448 * This is used for abbreviated commands like "D" -> "d$".
3449 * Also used to redo a command for ".".
3450 * 2. from the typeahead buffer
3451 * Stores text obtained previously but not used yet.
3452 * Also stores the result of mappings.
3453 * Also used for the ":normal" command.
3454 * 3. from the user
3455 * This may do a blocking wait if "advance" is TRUE.
3456 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003457 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003458 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 * KeyTyped is set to TRUE in the case the user typed the key.
3460 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003461 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003462 * Just look whether there is a character available.
3463 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 *
3465 * When "no_mapping" is zero, checks for mappings in the current mode.
3466 * Only returns one byte (of a multi-byte character).
3467 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3468 */
3469 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003470vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003472 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003473 int timedout = FALSE; // waited for more than 'timeoutlen'
3474 // for mapping to complete or
3475 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003476 int mapdepth = 0; // check for recursive mapping
3477 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003480 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
3482 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003484 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 /*
3487 * This function doesn't work very well when called recursively. This may
3488 * happen though, because of:
3489 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3490 * there is a character available, which calls this function. In that
3491 * case we must return NUL, to indicate no character is available.
3492 * 2. A GUI callback function writes to the screen, causing a
3493 * wait_return().
3494 * Using ":normal" can also do this, but it saves the typeahead buffer,
3495 * thus it should be OK. But don't get a key from the user then.
3496 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003497 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return NUL;
3499
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003500 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003505 typebuf_was_empty = FALSE;
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 init_typebuf();
3509 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003510 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 do
3512 {
3513/*
3514 * get a character: 1. from the stuffbuffer
3515 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003516 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 c = typeahead_char;
3519 if (advance)
3520 typeahead_char = 0;
3521 }
3522 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003523 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (c != NUL && !got_int)
3525 {
3526 if (advance)
3527 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003528 // KeyTyped = FALSE; When the command that stuffed something
3529 // was typed, behave like the stuffed command was typed.
3530 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 KeyStuffed = TRUE;
3532 }
3533 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003534 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 else
3537 {
3538 /*
3539 * Loop until we either find a matching mapped key, or we
3540 * are sure that it is not a mapped key.
3541 * If a mapped key sequence is found we go back to the start to
3542 * try re-mapping.
3543 */
3544 for (;;)
3545 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003546 long wait_time;
3547 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003548 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003549 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /*
3551 * ui_breakcheck() is slow, don't use it too often when
3552 * inside a mapping. But call it each time for typed
3553 * characters.
3554 */
3555 if (typebuf.tb_maplen)
3556 line_breakcheck();
3557 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003558 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (got_int)
3560 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003561 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003562 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 /*
3565 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003566 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 * Otherwise we behave like having gotten a CTRL-C.
3568 * As a result typing CTRL-C in insert mode will
3569 * really insert a CTRL-C.
3570 */
3571 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003572 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 c = ESC;
3574 else
3575 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003576 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003578 if (advance)
3579 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003580 // Also record this character, it might be needed to
3581 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003582 *typebuf.tb_buf = c;
3583 gotchars(typebuf.tb_buf, 1);
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 cmd_silent = FALSE;
3586
3587 break;
3588 }
3589 else if (typebuf.tb_len > 0)
3590 {
3591 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003592 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003594 map_result_T result = handle_mapping(
3595 &keylen, &timedout, &mapdepth);
3596
3597 if (result == map_result_retry)
3598 // try mapping again
3599 continue;
3600 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003601 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003602 // failed, use the outer loop
3603 c = -1;
3604 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003606 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608/*
3609 * get a character: 2. from the typeahead buffer
3610 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003611 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003612 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003613 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003614 cmd_silent = (typebuf.tb_silent > 0);
3615 if (typebuf.tb_maplen > 0)
3616 KeyTyped = FALSE;
3617 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003618 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003619 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003620 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003621 gotchars(typebuf.tb_buf
3622 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003623 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003624 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003625 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003626 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003627 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003630 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632
3633/*
3634 * get a character: 3. from the user - handle <Esc> in Insert mode
3635 */
3636 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003637 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003639 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * typing an <ESC>. If we get something after all, we may
3641 * have to redisplay the mode. That the cursor is in the wrong
3642 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003643 * Do not do this if the kitty keyboard protocol is used, every
3644 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
3646 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 new_wcol = curwin->w_wcol;
3648 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if ( advance
3650 && typebuf.tb_len == 1
3651 && typebuf.tb_buf[typebuf.tb_off] == ESC
3652 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003653 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003656 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003657 && (p_timeout
3658 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003660 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003662 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 char_u *ptr;
3664
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003665 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 unshowmode(TRUE);
3668 mode_deleted = TRUE;
3669 }
3670#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003671 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003672 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 int save_State;
3675
3676 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003677 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 gui_update_cursor(TRUE, FALSE);
3679 State = save_State;
3680 shape_changed = TRUE;
3681 }
3682#endif
3683 validate_cursor();
3684 old_wcol = curwin->w_wcol;
3685 old_wrow = curwin->w_wrow;
3686
Bram Moolenaar30613902019-12-01 22:11:18 +01003687 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (curwin->w_cursor.col != 0)
3689 {
3690 if (curwin->w_wcol > 0)
3691 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003692 // After auto-indenting and no text is following,
3693 // we are expecting to truncate the trailing
3694 // white-space, so find the last non-white
3695 // character -- webb
3696 if (did_ai && *skipwhite(ml_get_curline()
3697 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003699 chartabsize_T cts;
3700
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003701 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003703 init_chartabsize_arg(&cts, curwin,
3704 curwin->w_cursor.lnum, 0, ptr, ptr);
3705 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003707 if (!VIM_ISWHITE(*cts.cts_ptr))
3708 curwin->w_wcol = cts.cts_vcol;
3709 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003711 cts.cts_ptr +=
3712 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003714 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003716 clear_chartabsize_arg(&cts);
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003719 + curwin->w_wcol / curwin->w_width;
3720 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003722 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724 else
3725 {
3726 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 }
3730 else if (curwin->w_p_wrap && curwin->w_wrow)
3731 {
3732 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003733 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3737 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003738 // Correct when the cursor is on the right halve
3739 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 ptr = ml_get_curline();
3741 col -= (*mb_head_off)(ptr, ptr + col);
3742 if ((*mb_ptr2cells)(ptr + col) > 1)
3743 --curwin->w_wcol;
3744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 setcursor();
3747 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 new_wcol = curwin->w_wcol;
3749 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 curwin->w_wcol = old_wcol;
3751 curwin->w_wrow = old_wrow;
3752 }
3753 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003754 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003755
Bram Moolenaar30613902019-12-01 22:11:18 +01003756 // Allow mapping for just typed characters. When we get here c
3757 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003758 for (n = 1; n <= c; ++n)
3759 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 typebuf.tb_len += c;
3761
Bram Moolenaar30613902019-12-01 22:11:18 +01003762 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3764 {
3765 timedout = TRUE;
3766 continue;
3767 }
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (ex_normal_busy > 0)
3770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaar30613902019-12-01 22:11:18 +01003773 // No typeahead left and inside ":normal". Must return
3774 // something to avoid getting stuck. When an incomplete
3775 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (typebuf.tb_len > 0)
3777 {
3778 timedout = TRUE;
3779 continue;
3780 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003781
Bram Moolenaar30613902019-12-01 22:11:18 +01003782 // When 'insertmode' is set, ESC just beeps in Insert
3783 // mode. Use CTRL-L to make edit() return.
3784 // For the command line only CTRL-C always breaks it.
3785 // For the cmdline window: Alternate between ESC and
3786 // CTRL-C: ESC for most situations and CTRL-C to close the
3787 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003788 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003790#ifdef FEAT_TERMINAL
3791 else if (terminal_is_active())
3792 c = K_CANCEL;
3793#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003794 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003795 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 c = Ctrl_C;
3797 else
3798 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003800 // set a flag to indicate this wasn't a normal char
3801 if (advance)
3802 typebuf_was_empty = TRUE;
3803
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003804 // return from main_loop()
3805 if (pending_exmode_active)
3806 exmode_active = EXMODE_NORMAL;
3807
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003808 // no chars to block abbreviation for
3809 typebuf.tb_no_abbr_cnt = 0;
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 break;
3812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814/*
3815 * get a character: 3. from the user - update display
3816 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003817 // In insert mode a screen update is skipped when characters
3818 // are still available. But when those available characters
3819 // are part of a mapping, and we are going to do a blocking
3820 // wait here. Need to update the screen to display the
3821 // changed text so far. Also for when 'lazyredraw' is set and
3822 // redrawing was postponed because there was something in the
3823 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003824 if (((State & MODE_INSERT) != 0 || p_lz)
3825 && (State & MODE_CMDLINE) == 0
3826 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003829 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831
3832 /*
3833 * If we have a partial match (and are going to wait for more
3834 * input from the user), show the partially matched characters
3835 * to the user with showcmd.
3836 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003837 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003838 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (typebuf.tb_len > 0 && advance && !exmode_active)
3840 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003841 if (((State & (MODE_NORMAL | MODE_INSERT))
3842 || State == MODE_LANGMAP)
3843 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003845 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003846 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3848 + typebuf.tb_len - 1) == 1)
3849 {
3850 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3851 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003852 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003853 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003855 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 old_wcol = curwin->w_wcol;
3857 old_wrow = curwin->w_wrow;
3858 curwin->w_wcol = new_wcol;
3859 curwin->w_wrow = new_wrow;
3860 push_showcmd();
3861 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003862 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3863 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003864 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003865 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 curwin->w_wcol = old_wcol;
3867 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003870 // This looks nice when typing a dead character map.
3871 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003872 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003873 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3875 && cmdline_star == 0
3876#endif
3877 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3878 + typebuf.tb_len - 1) == 1)
3879 {
3880 putcmdline(typebuf.tb_buf[typebuf.tb_off
3881 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003882 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884 }
3885
3886/*
3887 * get a character: 3. from the user - get it
3888 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003889 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003890 // timedout may have been set if a mapping with empty RHS
3891 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003892 timedout = FALSE;
3893
Bram Moolenaar652de232019-04-04 20:13:09 +02003894 if (advance)
3895 {
3896 if (typebuf.tb_len == 0
3897 || !(p_timeout
3898 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3899 // blocking wait
3900 wait_time = -1L;
3901 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3902 wait_time = p_ttm;
3903 else
3904 wait_time = p_tm;
3905 }
3906 else
3907 wait_time = 0;
3908
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003909 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3911 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003912 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaareda35f72019-08-03 14:59:44 +02003914 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003916 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003918 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003920 if ((State & MODE_CMDLINE)
3921 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003923 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003924 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926
3927 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003928 continue; // end of input script reached
3929 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
3931 if (!advance)
3932 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003933 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
3935 timedout = TRUE;
3936 continue;
3937 }
3938 }
3939 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003940 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 while (typebuf.tb_buf[typebuf.tb_off
3942 + typebuf.tb_len] != NUL)
3943 typebuf.tb_noremap[typebuf.tb_off
3944 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003945#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003946 // Get IM status right after getting keys, not after the
3947 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 vgetc_im_active = im_get_status();
3949#endif
3950 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003951 } // for (;;)
3952 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
Bram Moolenaar30613902019-12-01 22:11:18 +01003954 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003955 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957 /*
3958 * The "INSERT" message is taken care of here:
3959 * if we return an ESC to exit insert mode, the message is deleted
3960 * if we don't return an ESC but deleted the message before, redisplay it
3961 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003962 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003964 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
3966 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003967 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 else
3969 unshowmode(FALSE);
3970 }
3971 else if (c != ESC && mode_deleted)
3972 {
3973 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003974 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 else
3976 showmode();
3977 }
3978 }
3979#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003980 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003981 if (gui.in_use && shape_changed)
3982 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003984 if (timedout && c == ESC)
3985 {
zeertzjqbf321802024-01-28 19:03:00 +01003986 // When recording there will be no timeout. Add an <Ignore> after the
3987 // ESC to avoid that it forms a key code with following characters.
3988 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003991 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 return c;
3994}
3995
3996/*
3997 * inchar() - get one character from
3998 * 1. a scriptfile
3999 * 2. the keyboard
4000 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01004001 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 * NUL terminated (buffer length must be 'maxlen' + 1).
4003 * Minimum for "maxlen" is 3!!!!
4004 *
4005 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
4006 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
4007 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
4008 * otherwise.
4009 *
4010 * If we got an interrupt all input is read until none is available.
4011 *
4012 * If wait_time == 0 there is no waiting for the char.
4013 * If wait_time == n we wait for n msec for a character to arrive.
4014 * If wait_time == -1 we wait forever for a character to arrive.
4015 *
4016 * Return the number of obtained characters.
4017 * Return -1 when end of input script reached.
4018 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02004019 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004020inchar(
4021 char_u *buf,
4022 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004023 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024{
Bram Moolenaar30613902019-12-01 22:11:18 +01004025 int len = 0; // init for GCC
4026 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004028 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
Bram Moolenaar30613902019-12-01 22:11:18 +01004030 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
4032 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004033 out_flush_cursor(FALSE, FALSE);
4034#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
4035 if (gui.in_use && postponed_mouseshape)
4036 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#endif
4038 }
4039
4040 /*
4041 * Don't reset these when at the hit-return prompt, otherwise a endless
4042 * recursive loop may result (write error in swapfile, hit-return, timeout
4043 * on char wait, flush swapfile, write error....).
4044 */
Bram Moolenaar24959102022-05-07 20:01:16 +01004045 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004047 did_outofmem_msg = FALSE; // display out of memory message (again)
4048 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004050 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004053 * Get a character from a script file if there is one.
4054 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 */
4056 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004057 while (scriptin[curscript] != NULL && script_char < 0
4058#ifdef FEAT_EVAL
4059 && !ignore_script
4060#endif
4061 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02004063#ifdef MESSAGE_QUEUE
4064 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00004065#endif
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
4068 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004069 // Reached EOF.
4070 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
4071 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 closescript();
4073 /*
4074 * When reading script file is interrupted, return an ESC to get
4075 * back to normal mode.
4076 * Otherwise return -1, because typebuf.tb_buf[] has changed.
4077 */
4078 if (got_int)
4079 retesc = TRUE;
4080 else
4081 return -1;
4082 }
4083 else
4084 {
4085 buf[0] = script_char;
4086 len = 1;
4087 }
4088 }
4089
Bram Moolenaar30613902019-12-01 22:11:18 +01004090 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
4092 /*
4093 * If we got an interrupt, skip all previously typed characters and
4094 * return TRUE if quit reading script file.
4095 * Stop reading typeahead when a single CTRL-C was read,
4096 * fill_input_buf() returns this when not able to read from stdin.
4097 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
4098 * and buf may be pointing inside typebuf.tb_buf[].
4099 */
4100 if (got_int)
4101 {
kylo252ae6f1d82022-02-16 19:24:07 +00004102#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 char_u dum[DUM_LEN + 1];
4104
4105 for (;;)
4106 {
4107 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01004108 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 break;
4110 }
4111 return retesc;
4112 }
4113
4114 /*
4115 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004116 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004118 if (wait_time == -1L || wait_time > 10L)
4119 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 /*
4122 * Fill up to a third of the buffer, because each character may be
4123 * tripled below.
4124 */
4125 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
4126 }
4127
Bram Moolenaar30613902019-12-01 22:11:18 +01004128 // If the typebuf was changed further down, it is like nothing was added by
4129 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (typebuf_changed(tb_change_cnt))
4131 return 0;
4132
Bram Moolenaar30613902019-12-01 22:11:18 +01004133 // Note the change in the typeahead buffer, this matters for when
4134 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4135 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004136 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4137 typebuf.tb_change_cnt = 1;
4138
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004139 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140}
4141
4142/*
4143 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004144 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 * Returns the new length.
4146 */
4147 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004148fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
4150 int i;
4151 char_u *p = buf;
4152
4153 /*
4154 * Two characters are special: NUL and K_SPECIAL.
4155 * When compiled With the GUI CSI is also special.
4156 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4157 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4158 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 */
4160 for (i = len; --i >= 0; ++p)
4161 {
4162#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004163 // When the GUI is used any character can come after a CSI, don't
4164 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (gui.in_use && p[0] == CSI && i >= 2)
4166 {
4167 p += 2;
4168 i -= 2;
4169 }
zeertzjq646bb722022-02-16 17:51:47 +00004170# ifndef MSWIN
4171 // When not on MS-Windows and the GUI is not used CSI needs to be
4172 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else if (!gui.in_use && p[0] == CSI)
4174 {
4175 mch_memmove(p + 3, p + 1, (size_t)i);
4176 *p++ = K_SPECIAL;
4177 *p++ = KS_EXTRA;
4178 *p = (int)KE_CSI;
4179 len += 2;
4180 }
zeertzjq646bb722022-02-16 17:51:47 +00004181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 else
4183#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004184 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004185 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004186 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004187#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004188 // Win32 console passes modifiers
4189 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004190# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004191 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004192# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004193 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#endif
4195 ))
4196 {
4197 mch_memmove(p + 3, p + 1, (size_t)i);
4198 p[2] = K_THIRD(p[0]);
4199 p[1] = K_SECOND(p[0]);
4200 p[0] = K_SPECIAL;
4201 p += 2;
4202 len += 2;
4203 }
4204 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004205 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return len;
4207}
4208
4209#if defined(USE_INPUT_BUF) || defined(PROTO)
4210/*
4211 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4212 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004213 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004214 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 */
4216 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004217input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
4219 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004220# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4221 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222# endif
4223 );
4224}
4225#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004226
4227/*
4228 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4229 * typeahead.
4230 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004231 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004232getcmdkeycmd(
4233 int promptc UNUSED,
4234 void *cookie UNUSED,
4235 int indent UNUSED,
4236 getline_opt_T do_concat UNUSED)
4237{
4238 garray_T line_ga;
4239 int c1 = -1;
4240 int c2;
4241 int cmod = 0;
4242 int aborted = FALSE;
4243
4244 ga_init2(&line_ga, 1, 32);
4245
4246 // no mapping for these characters
4247 no_mapping++;
4248
4249 got_int = FALSE;
4250 while (c1 != NUL && !aborted)
4251 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004252 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004253 {
4254 aborted = TRUE;
4255 break;
4256 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004257
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004258 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004259 {
4260 // incomplete <Cmd> is an error, because there is not much the user
4261 // could do in this state.
4262 emsg(_(e_cmd_mapping_must_end_with_cr));
4263 aborted = TRUE;
4264 break;
4265 }
4266
4267 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004268 c1 = vgetorpeek(TRUE);
4269
Bram Moolenaar957cf672020-11-12 14:21:06 +01004270 // Get two extra bytes for special keys
4271 if (c1 == K_SPECIAL)
4272 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004273 c1 = vgetorpeek(TRUE);
4274 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004275 if (c1 == KS_MODIFIER)
4276 {
4277 cmod = c2;
4278 continue;
4279 }
4280 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004281
4282 // K_ESC is used to avoid ambiguity with the single Esc character
4283 // that might be the start of an escape sequence. Convert it back
4284 // to a single Esc here.
4285 if (c1 == K_ESC)
4286 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004287 }
4288
4289 if (got_int)
4290 aborted = TRUE;
4291 else if (c1 == '\r' || c1 == '\n')
4292 c1 = NUL; // end the line
4293 else if (c1 == ESC)
4294 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004295 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004296 {
4297 // give a nicer error message for this special case
4298 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4299 aborted = TRUE;
4300 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004301 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004302 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004303 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004304 }
4305 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004306 {
4307 if (cmod != 0)
4308 {
4309 ga_append(&line_ga, K_SPECIAL);
4310 ga_append(&line_ga, KS_MODIFIER);
4311 ga_append(&line_ga, cmod);
4312 }
4313 if (IS_SPECIAL(c1))
4314 {
4315 ga_append(&line_ga, K_SPECIAL);
4316 ga_append(&line_ga, K_SECOND(c1));
4317 ga_append(&line_ga, K_THIRD(c1));
4318 }
4319 else
4320 ga_append(&line_ga, c1);
4321 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004322
4323 cmod = 0;
4324 }
4325
4326 no_mapping--;
4327
4328 if (aborted)
4329 ga_clear(&line_ga);
4330
4331 return (char_u *)line_ga.ga_data;
4332}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004333
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004334#if defined(FEAT_EVAL) || defined(PROTO)
4335/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004336 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4337 * is set when redo'ing.
4338 * Put this SID in the redo buffer, so that "." will use the same script
4339 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004340 */
4341 void
4342may_add_last_used_map_to_redobuff(void)
4343{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004344 char_u buf[3 + 20];
John Marriotte7a1bbf2024-11-11 20:40:33 +01004345 int buflen;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004346 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004347
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004348 if (last_used_map != NULL)
4349 sid = last_used_map->m_script_ctx.sc_sid;
4350 if (sid < 0)
4351 sid = last_used_sid;
4352
4353 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004354 return;
4355
4356 // <K_SID>{nr};
4357 buf[0] = K_SPECIAL;
4358 buf[1] = KS_EXTRA;
4359 buf[2] = KE_SID;
John Marriotte7a1bbf2024-11-11 20:40:33 +01004360 buflen = 3;
4361
4362 buflen += vim_snprintf((char *)buf + 3, 20, "%d;", sid);
4363 add_buff(&redobuff, buf, (long)buflen);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004364}
4365#endif
4366
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004367 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004368do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004369{
4370 int res;
4371#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004372 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004373
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004374 if (key == K_SCRIPT_COMMAND
4375 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004376 {
4377 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004378 if (last_used_map != NULL)
4379 current_sctx = last_used_map->m_script_ctx;
4380 else
4381 {
4382 current_sctx.sc_sid = last_used_sid;
4383 current_sctx.sc_lnum = 0;
4384 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4385 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004386 }
4387#endif
4388
4389 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4390
4391#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004392 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004393 current_sctx = save_current_sctx;
4394#endif
4395
4396 return res;
4397}
4398
4399#if defined(FEAT_EVAL) || defined(PROTO)
4400 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004401reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004402{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004403 if (last_used_map != mp)
4404 return;
4405
4406 last_used_map = NULL;
4407 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004408}
4409#endif