blob: 06f4ad43541b1114fb9581680db531ec43a7bef5 [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
Bram Moolenaare32c3c42022-01-15 18:26:04 +000091#ifdef FEAT_EVAL
92mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010093int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000094#endif
95
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020099static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200101static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +0200103static int inchar(char_u *buf, int maxlen, long wait_time);
Shougo Matsushita83678842024-07-11 22:05:12 +0200104#ifdef FEAT_EVAL
105static int do_key_input_pre(int c);
106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108/*
109 * Free and clear a buffer.
110 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200111 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100112free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100114 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
Bram Moolenaar285e3352018-04-18 23:01:13 +0200116 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 {
118 np = p->b_next;
119 vim_free(p);
120 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200121 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000122 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123}
124
125/*
126 * Return the contents of a buffer as a single string.
127 * K_SPECIAL and CSI in the returned string are escaped.
128 */
129 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100130get_buffcont(
131 buffheader_T *buffer,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100132 int dozero, // count == zero is not an error
133 size_t *len) // the length of the returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134{
135 long_u count = 0;
136 char_u *p = NULL;
137 char_u *p2;
138 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200139 buffblock_T *bp;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100140 size_t i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar30613902019-12-01 22:11:18 +0100142 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200143 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100144 count += (long_u)bp->b_strlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100146 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 {
148 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200149 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 for (str = bp->b_str; *str; )
151 *p2++ = *str++;
152 *p2 = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100153 i = (size_t)(p2 - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100155
156 if (len != NULL)
157 *len = i;
158
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100159 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160}
161
162/*
163 * Return the contents of the record buffer as a single string
164 * and clear the record buffer.
165 * K_SPECIAL and CSI in the returned string are escaped.
166 */
167 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100168get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
170 char_u *p;
171 size_t len;
172
John Marriotte7a1bbf2024-11-11 20:40:33 +0100173 p = get_buffcont(&recordbuff, TRUE, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 free_buff(&recordbuff);
175
176 /*
177 * Remove the characters that were added the last time, these must be the
178 * (possibly mapped) characters that stopped the recording.
179 */
Mike Williamsaca8f552024-04-07 18:26:22 +0200180 if (len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 {
182 len -= last_recorded_len;
183 p[len] = NUL;
184 }
185
186 /*
187 * When stopping recording from Insert mode with CTRL-O q, also remove the
188 * CTRL-O.
189 */
190 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
191 p[len - 1] = NUL;
192
193 return (p);
194}
195
196/*
197 * Return the contents of the redo buffer as a single string.
198 * K_SPECIAL and CSI in the returned string are escaped.
199 */
200 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100201get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
John Marriotte7a1bbf2024-11-11 20:40:33 +0100203 return get_buffcont(&redobuff, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
206/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000207 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 * K_SPECIAL and CSI should have been escaped already.
209 */
210 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100211add_buff(
212 buffheader_T *buf,
213 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100214 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (slen < 0)
217 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100218 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 return;
220
Bram Moolenaar30613902019-12-01 22:11:18 +0100221 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200223 buf->bh_curr = &(buf->bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100224 buf->bh_create_newblock = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100226 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100228 iemsg(e_add_to_internal_buffer_that_was_already_read_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 return;
230 }
231 else if (buf->bh_index != 0)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100232 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200233 mch_memmove(buf->bh_first.b_next->b_str,
234 buf->bh_first.b_next->b_str + buf->bh_index,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100235 (buf->bh_first.b_next->b_strlen - buf->bh_index) + 1);
236 buf->bh_first.b_next->b_strlen -= buf->bh_index;
237 buf->bh_space += buf->bh_index;
238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 buf->bh_index = 0;
240
John Marriotte7a1bbf2024-11-11 20:40:33 +0100241 if (!buf->bh_create_newblock && buf->bh_space >= (int)slen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100243 vim_strncpy(buf->bh_curr->b_str + buf->bh_curr->b_strlen, s, (size_t)slen);
244 buf->bh_curr->b_strlen += slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 buf->bh_space -= slen;
246 }
247 else
248 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100249 long_u len;
250 buffblock_T *p;
251
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 if (slen < MINIMAL_SIZE)
253 len = MINIMAL_SIZE;
254 else
255 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200256 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100258 return; // no space, just forget it
Bram Moolenaarb6356332005-07-18 21:40:44 +0000259 vim_strncpy(p->b_str, s, (size_t)slen);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100260 p->b_strlen = slen;
261 buf->bh_space = (int)(len - slen);
262 buf->bh_create_newblock = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
Bram Moolenaar285e3352018-04-18 23:01:13 +0200264 p->b_next = buf->bh_curr->b_next;
265 buf->bh_curr->b_next = p;
266 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268}
269
270/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000271 * Delete "slen" bytes from the end of "buf".
272 * Only works when it was just added.
273 */
274 static void
275delete_buff_tail(buffheader_T *buf, int slen)
276{
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000277 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000278 return; // nothing to delete
John Marriotte7a1bbf2024-11-11 20:40:33 +0100279 if (buf->bh_curr->b_strlen < (size_t)slen)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000280 return;
281
John Marriotte7a1bbf2024-11-11 20:40:33 +0100282 buf->bh_curr->b_str[buf->bh_curr->b_strlen - (size_t)slen] = NUL;
283 buf->bh_curr->b_strlen -= slen;
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000284 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000285}
286
287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 * Add number "n" to buffer "buf".
289 */
290 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100291add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292{
293 char_u number[32];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100294 int numberlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295
John Marriotte7a1bbf2024-11-11 20:40:33 +0100296 numberlen = vim_snprintf((char *)number, sizeof(number), "%ld", n);
297 add_buff(buf, number, (long)numberlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298}
299
300/*
301 * Add character 'c' to buffer "buf".
302 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
303 */
304 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100305add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 char_u bytes[MB_MAXBYTES + 1];
308 int len;
309 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 char_u temp[4];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100311 long templen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 if (IS_SPECIAL(c))
314 len = 1;
315 else
316 len = (*mb_char2bytes)(c, bytes);
317 for (i = 0; i < len; ++i)
318 {
319 if (!IS_SPECIAL(c))
320 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
322 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
323 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100324 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 temp[0] = K_SPECIAL;
326 temp[1] = K_SECOND(c);
327 temp[2] = K_THIRD(c);
328 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100329 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331#ifdef FEAT_GUI
332 else if (c == CSI)
333 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100334 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 temp[0] = CSI;
336 temp[1] = KS_EXTRA;
337 temp[2] = (int)KE_CSI;
338 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100339 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 }
341#endif
342 else
343 {
344 temp[0] = c;
345 temp[1] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100346 templen = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100348 add_buff(buf, temp, templen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350}
351
Bram Moolenaar30613902019-12-01 22:11:18 +0100352// First read ahead buffer. Used for translated commands.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100353static buffheader_T readbuf1 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100354
Bram Moolenaar30613902019-12-01 22:11:18 +0100355// Second read ahead buffer. Used for redo.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100356static buffheader_T readbuf2 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100359 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
360 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 * If advance == TRUE go to the next char.
362 * No translation is done K_SPECIAL and CSI are escaped.
363 */
364 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100365read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100367 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 c = read_readbuf(&readbuf1, advance);
370 if (c == NUL)
371 c = read_readbuf(&readbuf2, advance);
372 return c;
373}
374
375 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100376read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100377{
378 char_u c;
379 buffblock_T *curr;
380
Bram Moolenaar30613902019-12-01 22:11:18 +0100381 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 return NUL;
383
Bram Moolenaar285e3352018-04-18 23:01:13 +0200384 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387 if (advance)
388 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100389 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200391 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100393 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 }
395 }
396 return c;
397}
398
399/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100400 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
402 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100403start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200405 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200407 readbuf1.bh_curr = &(readbuf1.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100408 readbuf1.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100409 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200410 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100411 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200412 readbuf2.bh_curr = &(readbuf2.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100413 readbuf2.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 }
415}
416
417/*
418 * Return TRUE if the stuff buffer is empty.
419 */
420 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100421stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200423 return (readbuf1.bh_first.b_next == NULL
424 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100425}
426
Bram Moolenaar113e1072019-01-20 15:30:40 +0100427#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100428/*
429 * Return TRUE if readbuf1 is empty. There may still be redo characters in
430 * redbuf2.
431 */
432 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100433readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100434{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200435 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
439/*
440 * Set a typeahead character that won't be flushed.
441 */
442 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100443typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444{
445 typeahead_char = c;
446}
447
448/*
449 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100450 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 * flush all typeahead characters (used when interrupted by a CTRL-C).
452 */
453 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200454flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
456 init_typebuf();
457
458 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100459 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 ;
461
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200462 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
Christian Brabandt322ba912024-08-25 21:33:03 +0200464 // remove mapped characters at the start only,
465 // but only when enough space left in typebuf
466 if (typebuf.tb_off + typebuf.tb_maplen >= typebuf.tb_buflen)
467 {
468 typebuf.tb_off = MAXMAPLEN;
469 typebuf.tb_len = 0;
470 }
471 else
472 {
473 typebuf.tb_off += typebuf.tb_maplen;
474 typebuf.tb_len -= typebuf.tb_maplen;
475 }
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100476#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
477 if (typebuf.tb_len == 0)
478 typebuf_was_filled = FALSE;
479#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200480 }
481 else
482 {
483 // remove typeahead
484 if (flush_typeahead == FLUSH_INPUT)
485 // We have to get all characters, because we may delete the first
486 // part of an escape sequence. In an xterm we get one char at a
487 // time and we have to get them all.
488 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
489 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 typebuf.tb_off = MAXMAPLEN;
491 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200492#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100493 // Reset the flag that text received from a client or from feedkeys()
494 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200495 typebuf_was_filled = FALSE;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 typebuf.tb_maplen = 0;
499 typebuf.tb_silent = 0;
500 cmd_silent = FALSE;
501 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200502 if (++typebuf.tb_change_cnt == 0)
503 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504}
505
506/*
507 * The previous contents of the redo buffer is kept in old_redobuffer.
508 * This is used for the CTRL-O <.> command in insert mode.
509 */
510 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100511ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000513 if (block_redo)
514 return;
515
516 free_buff(&old_redobuff);
517 old_redobuff = redobuff;
518 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100521/*
522 * Discard the contents of the redo buffer and restore the previous redo
523 * buffer.
524 */
525 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100526CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100527{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000528 if (block_redo)
529 return;
530
531 free_buff(&redobuff);
532 redobuff = old_redobuff;
533 old_redobuff.bh_first.b_next = NULL;
534 start_stuff();
535 while (read_readbuffers(TRUE) != NUL)
536 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100537}
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/*
540 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
541 * Used before executing autocommands and user functions.
542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200544saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *s;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100547 size_t slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200549 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200550 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200551 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200552 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar30613902019-12-01 22:11:18 +0100554 // Make a copy, so that ":normal ." in a function works.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100555 s = get_buffcont(&save_redo->sr_redobuff, FALSE, &slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000556 if (s == NULL)
557 return;
558
John Marriotte7a1bbf2024-11-11 20:40:33 +0100559 add_buff(&redobuff, s, (long)slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000560 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561}
562
563/*
564 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
565 * Used after executing autocommands and user functions.
566 */
567 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200568restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200570 free_buff(&redobuff);
571 redobuff = save_redo->sr_redobuff;
572 free_buff(&old_redobuff);
573 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576/*
577 * Append "s" to the redo buffer.
578 * K_SPECIAL and CSI should already have been escaped.
579 */
580 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100581AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 if (!block_redo)
584 add_buff(&redobuff, s, -1L);
585}
586
587/*
588 * Append to Redo buffer literally, escaping special characters with CTRL-V.
589 * K_SPECIAL and CSI are escaped as well.
590 */
591 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100592AppendToRedobuffLit(
593 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100594 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000596 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 int c;
598 char_u *start;
599
600 if (block_redo)
601 return;
602
Bram Moolenaarebefac62005-12-28 22:39:57 +0000603 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100605 // Put a string of normal characters in the redo buffer (that's
606 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000608 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 ++s;
610
Bram Moolenaar30613902019-12-01 22:11:18 +0100611 // Don't put '0' or '^' as last character, just in case a CTRL-D is
612 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
614 --s;
615 if (s > start)
616 add_buff(&redobuff, start, (long)(s - start));
617
Bram Moolenaarebefac62005-12-28 22:39:57 +0000618 if (*s == NUL || (len >= 0 && s - str >= len))
619 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaar30613902019-12-01 22:11:18 +0100621 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000622 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100623 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000624 c = mb_cptr2char_adv(&s);
625 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000626 c = *s++;
627 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
628 add_char_buff(&redobuff, Ctrl_V);
629
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000630 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000631 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000632 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000633 else
634 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636}
637
638/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100639 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
640 * and escaping other K_SPECIAL and CSI bytes.
641 */
642 void
643AppendToRedobuffSpec(char_u *s)
644{
zeertzjq30b6d612023-05-07 17:39:23 +0100645 if (block_redo)
646 return;
647
zeertzjq3ab3a862023-05-06 16:22:04 +0100648 while (*s != NUL)
649 {
650 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
651 {
zeertzjq30b6d612023-05-07 17:39:23 +0100652 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100653 add_buff(&redobuff, s, 3L);
654 s += 3;
655 }
656 else
657 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
658 }
659}
660
661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 * Append a character to the redo buffer.
663 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
664 */
665 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100666AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 if (!block_redo)
669 add_char_buff(&redobuff, c);
670}
671
672/*
673 * Append a number to the redo buffer.
674 */
675 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100676AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 if (!block_redo)
679 add_num_buff(&redobuff, n);
680}
681
682/*
683 * Append string "s" to the stuff buffer.
684 * CSI and K_SPECIAL must already have been escaped.
685 */
686 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100687stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100689 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200692/*
693 * Append string "s" to the redo stuff buffer.
694 * CSI and K_SPECIAL must already have been escaped.
695 */
696 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100697stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200698{
699 add_buff(&readbuf2, s, -1L);
700}
701
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200702 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100703stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100705 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706}
707
708#if defined(FEAT_EVAL) || defined(PROTO)
709/*
710 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
711 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200712 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 */
714 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100715stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200717 int c;
718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 while (*s != NUL)
720 {
721 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
722 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100723 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 stuffReadbuffLen(s, 3L);
725 s += 3;
726 }
727 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200728 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100729 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200730 if (c == CAR || c == NL || c == ESC)
731 c = ' ';
732 stuffcharReadbuff(c);
733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735}
736#endif
737
738/*
739 * Append a character to the stuff buffer.
740 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
741 */
742 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100743stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100745 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746}
747
748/*
749 * Append a number to the stuff buffer.
750 */
751 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100752stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100754 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755}
756
757/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200758 * Stuff a string into the typeahead buffer, such that edit() will insert it
759 * literally ("literally" TRUE) or interpret is as typed characters.
760 */
761 void
762stuffescaped(char_u *arg, int literally)
763{
764 int c;
765 char_u *start;
766
767 while (*arg != NUL)
768 {
769 // Stuff a sequence of normal ASCII characters, that's fast. Also
770 // stuff K_SPECIAL to get the effect of a special key when "literally"
771 // is TRUE.
772 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000773 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200774 || (*arg == K_SPECIAL && !literally))
775 ++arg;
776 if (arg > start)
777 stuffReadbuffLen(start, (long)(arg - start));
778
779 // stuff a single special character
780 if (*arg != NUL)
781 {
782 if (has_mbyte)
783 c = mb_cptr2char_adv(&arg);
784 else
785 c = *arg++;
786 if (literally && ((c < ' ' && c != TAB) || c == DEL))
787 stuffcharReadbuff(Ctrl_V);
788 stuffcharReadbuff(c);
789 }
790 }
791}
792
793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
795 * multibyte characters.
796 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100797 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
798 * otherwise.
799 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 */
801 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100802read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100804 static buffblock_T *bp;
805 static char_u *p;
806 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100807 int n;
808 char_u buf[MB_MAXBYTES + 1];
809 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 if (init)
812 {
813 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200814 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200816 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (bp == NULL)
818 return FAIL;
819 p = bp->b_str;
820 return OK;
821 }
822 if ((c = *p) != NUL)
823 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100824 // Reverse the conversion done by add_char_buff()
825 // For a multi-byte character get all the bytes and return the
826 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
828 n = MB_BYTE2LEN_CHECK(c);
829 else
830 n = 1;
831 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100833 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 c = TO_SPECIAL(p[1], p[2]);
836 p += 2;
837 }
838#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100839 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 p += 2;
841#endif
842 if (*++p == NUL && bp->b_next != NULL)
843 {
844 bp = bp->b_next;
845 p = bp->b_str;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100848 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
850 if (n != 1)
851 c = (*mb_ptr2char)(buf);
852 break;
853 }
854 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100855 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858 }
859
860 return c;
861}
862
863/*
864 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
865 * If old_redo is TRUE, use old_redobuff instead of redobuff.
866 * The escaped K_SPECIAL and CSI are copied without translation.
867 */
868 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100869copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 int c;
872
873 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100874 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875}
876
877/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100878 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * Insert the redo count into the command.
880 * If "old_redo" is TRUE, the last but one command is repeated
881 * instead of the last command (inserting text). This is used for
882 * CTRL-O <.> in insert mode
883 *
884 * return FAIL for failure, OK otherwise
885 */
886 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100887start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 int c;
890
Bram Moolenaar30613902019-12-01 22:11:18 +0100891 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (read_redo(TRUE, old_redo) == FAIL)
893 return FAIL;
894
895 c = read_redo(FALSE, old_redo);
896
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100897#ifdef FEAT_EVAL
898 if (c == K_SID)
899 {
900 // Copy the <SID>{sid}; sequence
901 add_char_buff(&readbuf2, c);
902 for (;;)
903 {
904 c = read_redo(FALSE, old_redo);
905 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100906 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100907 break;
908 }
909 c = read_redo(FALSE, old_redo);
910 }
911#endif
912
Bram Moolenaar30613902019-12-01 22:11:18 +0100913 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (c == '"')
915 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100916 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 c = read_redo(FALSE, old_redo);
918
Bram Moolenaar30613902019-12-01 22:11:18 +0100919 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (c >= '1' && c < '9')
921 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100922 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200923
Bram Moolenaar30613902019-12-01 22:11:18 +0100924 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200925 if (c == '=')
926 {
927 add_char_buff(&readbuf2, CAR);
928 cmd_silent = TRUE;
929 }
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 c = read_redo(FALSE, old_redo);
932 }
933
Bram Moolenaar30613902019-12-01 22:11:18 +0100934 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
936 VIsual = curwin->w_cursor;
937 VIsual_active = TRUE;
938 VIsual_select = FALSE;
939 VIsual_reselect = TRUE;
940 redo_VIsual_busy = TRUE;
941 c = read_redo(FALSE, old_redo);
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Bram Moolenaar30613902019-12-01 22:11:18 +0100944 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (count)
946 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100947 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100949 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100952 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100953 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 copy_redo(old_redo);
955 return OK;
956}
957
958/*
959 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100960 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 * return FAIL for failure, OK otherwise
962 */
963 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100964start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 int c;
967
968 if (read_redo(TRUE, FALSE) == FAIL)
969 return FAIL;
970 start_stuff();
971
Bram Moolenaar30613902019-12-01 22:11:18 +0100972 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 while ((c = read_redo(FALSE, FALSE)) != NUL)
974 {
975 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
976 {
977 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100978 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 break;
980 }
981 }
982
Bram Moolenaar30613902019-12-01 22:11:18 +0100983 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 copy_redo(FALSE);
985 block_redo = TRUE;
986 return OK;
987}
988
989 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100990stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 block_redo = FALSE;
993}
994
995/*
996 * Initialize typebuf.tb_buf to point to typebuf_init.
997 * alloc() cannot be used here: In out-of-memory situations it would
998 * be impossible to type anything.
999 */
1000 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001001init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001003 if (typebuf.tb_buf != NULL)
1004 return;
1005
1006 typebuf.tb_buf = typebuf_init;
1007 typebuf.tb_noremap = noremapbuf_init;
1008 typebuf.tb_buflen = TYPELEN_INIT;
1009 typebuf.tb_len = 0;
1010 typebuf.tb_off = MAXMAPLEN + 4;
1011 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012}
1013
1014/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001015 * Returns TRUE when keys cannot be remapped.
1016 */
1017 int
1018noremap_keys(void)
1019{
1020 return KeyNoremap & (RM_NONE|RM_SCRIPT);
1021}
1022
1023/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001024 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
1025 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001027 * If "noremap" is REMAP_YES, new string can be mapped again.
1028 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
1029 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001030 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001031 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001033 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001035 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1036 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001038 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 *
1040 * return FAIL for failure, OK otherwise
1041 */
1042 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001043ins_typebuf(
1044 char_u *str,
1045 int noremap,
1046 int offset,
1047 int nottyped,
1048 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 char_u *s1, *s2;
1051 int newlen;
1052 int addlen;
1053 int i;
1054 int newoff;
1055 int val;
1056 int nrm;
1057
1058 init_typebuf();
1059 if (++typebuf.tb_change_cnt == 0)
1060 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001061 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (offset == 0 && addlen <= typebuf.tb_off)
1066 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001067 /*
1068 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 typebuf.tb_off -= addlen;
1071 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1072 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001073 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1074 >= addlen + 3 * (MAXMAPLEN + 4))
1075 {
1076 /*
1077 * Buffer is empty and string fits in the existing buffer.
1078 * Leave some space before and after, if possible.
1079 */
1080 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1081 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 else
1084 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001085 int extra;
1086
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001087 /*
1088 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001089 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001090 * characters. We add some extra room to avoid having to allocate too
1091 * often.
1092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001094 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1095 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001097 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001098 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 setcursor();
1100 return FAIL;
1101 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001102 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001104 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 return FAIL;
1106 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001107 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109 vim_free(s1);
1110 return FAIL;
1111 }
1112 typebuf.tb_buflen = newlen;
1113
Bram Moolenaar30613902019-12-01 22:11:18 +01001114 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1116 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001117 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001119 // copy the old chars, after the insertion point, including the NUL at
1120 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 mch_memmove(s1 + newoff + offset + addlen,
1122 typebuf.tb_buf + typebuf.tb_off + offset,
1123 (size_t)(typebuf.tb_len - offset + 1));
1124 if (typebuf.tb_buf != typebuf_init)
1125 vim_free(typebuf.tb_buf);
1126 typebuf.tb_buf = s1;
1127
1128 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1129 (size_t)offset);
1130 mch_memmove(s2 + newoff + offset + addlen,
1131 typebuf.tb_noremap + typebuf.tb_off + offset,
1132 (size_t)(typebuf.tb_len - offset));
1133 if (typebuf.tb_noremap != noremapbuf_init)
1134 vim_free(typebuf.tb_noremap);
1135 typebuf.tb_noremap = s2;
1136
1137 typebuf.tb_off = newoff;
1138 }
1139 typebuf.tb_len += addlen;
1140
Bram Moolenaar30613902019-12-01 22:11:18 +01001141 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (noremap == REMAP_SCRIPT)
1143 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001144 else if (noremap == REMAP_SKIP)
1145 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 else
1147 val = RM_NONE;
1148
1149 /*
1150 * Adjust typebuf.tb_noremap[] for the new characters:
1151 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1152 * (sometimes) not remappable
1153 * If noremap == REMAP_YES: all the new characters are mappable
1154 * If noremap > 0: "noremap" characters are not remappable, the rest
1155 * mappable
1156 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001157 if (noremap == REMAP_SKIP)
1158 nrm = 1;
1159 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 nrm = addlen;
1161 else
1162 nrm = noremap;
1163 for (i = 0; i < addlen; ++i)
1164 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1165 (--nrm >= 0) ? val : RM_YES;
1166
Bram Moolenaar30613902019-12-01 22:11:18 +01001167 // tb_maplen and tb_silent only remember the length of mapped and/or
1168 // silent mappings at the start of the buffer, assuming that a mapped
1169 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (nottyped || typebuf.tb_maplen > offset)
1171 typebuf.tb_maplen += addlen;
1172 if (silent || typebuf.tb_silent > offset)
1173 {
1174 typebuf.tb_silent += addlen;
1175 cmd_silent = TRUE;
1176 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001177 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 typebuf.tb_no_abbr_cnt += addlen;
1179
1180 return OK;
1181}
1182
1183/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001184 * Put character "c" back into the typeahead buffer.
1185 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001186 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1187 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001188 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001189 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001190 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001191ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001192{
zeertzjq6cac7702022-01-04 18:01:21 +00001193 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001194 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001195
zeertzjq2e7cba32022-06-10 15:30:32 +01001196 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001197 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001198 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001199}
1200
1201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001203 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001204 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1206 * changed it was reallocated and the old pointer can no longer be used.
1207 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1208 * that was just added.
1209 */
1210 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001211typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001212 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213{
1214 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001215#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1216 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
1218 ));
1219}
1220
1221/*
1222 * Return TRUE if there are no characters in the typeahead buffer that have
1223 * not been typed (result from a mapping or come from ":normal").
1224 */
1225 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001226typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227{
1228 return typebuf.tb_maplen == 0;
1229}
1230
1231/*
1232 * Return the number of characters that are mapped (or not typed).
1233 */
1234 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001235typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
1237 return typebuf.tb_maplen;
1238}
1239
1240/*
1241 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1242 */
1243 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001244del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 int i;
1247
1248 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001249 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251 typebuf.tb_len -= len;
1252
1253 /*
1254 * Easy case: Just increase typebuf.tb_off.
1255 */
1256 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1257 >= 3 * MAXMAPLEN + 3)
1258 typebuf.tb_off += len;
1259 /*
1260 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1261 */
1262 else
1263 {
1264 i = typebuf.tb_off + offset;
1265 /*
1266 * Leave some extra room at the end to avoid reallocation.
1267 */
1268 if (typebuf.tb_off > MAXMAPLEN)
1269 {
1270 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1271 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1272 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1273 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1274 typebuf.tb_off = MAXMAPLEN;
1275 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001276 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1278 typebuf.tb_buf + i + len,
1279 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001280 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1282 typebuf.tb_noremap + i + len,
1283 (size_t)(typebuf.tb_len - offset));
1284 }
1285
Bram Moolenaar30613902019-12-01 22:11:18 +01001286 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {
1288 if (typebuf.tb_maplen < offset + len)
1289 typebuf.tb_maplen = offset;
1290 else
1291 typebuf.tb_maplen -= len;
1292 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001293 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {
1295 if (typebuf.tb_silent < offset + len)
1296 typebuf.tb_silent = offset;
1297 else
1298 typebuf.tb_silent -= len;
1299 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001300 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
1302 if (typebuf.tb_no_abbr_cnt < offset + len)
1303 typebuf.tb_no_abbr_cnt = offset;
1304 else
1305 typebuf.tb_no_abbr_cnt -= len;
1306 }
1307
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001308#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001309 // Reset the flag that text received from a client or from feedkeys()
1310 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001311 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#endif
1313 if (++typebuf.tb_change_cnt == 0)
1314 typebuf.tb_change_cnt = 1;
1315}
1316
zeertzjq094c4392024-04-18 22:09:37 +02001317/*
1318 * State for adding bytes to a recording or 'showcmd'.
1319 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001320typedef struct
1321{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001322 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001323 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001324 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001325 unsigned pending_special;
1326 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001327} gotchars_state_T;
1328
1329/*
1330 * Add a single byte to a recording or 'showcmd'.
1331 * Return TRUE if a full key has been received, FALSE otherwise.
1332 */
1333 static int
1334gotchars_add_byte(gotchars_state_T *state, char_u byte)
1335{
1336 int c = state->buf[state->buflen++] = byte;
1337 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001338 int in_special = state->pending_special > 0;
1339 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001340
zeertzjq6b13e3d2024-04-22 21:04:29 +02001341 if (in_special)
1342 state->pending_special--;
1343 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001344#ifdef FEAT_GUI
1345 || c == CSI
1346#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001347 )
1348 // When receiving a special key sequence, store it until we have all
1349 // the bytes and we can decide what to do with it.
1350 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001351
zeertzjq6b13e3d2024-04-22 21:04:29 +02001352 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001353 goto ret_false;
1354
zeertzjq6b13e3d2024-04-22 21:04:29 +02001355 if (in_mbyte)
1356 state->pending_mbyte--;
1357 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001358 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001359 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001360 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001361 if (state->prev_c == KS_MODIFIER)
1362 // When receiving a modifier, wait for the modified key.
1363 goto ret_false;
1364 c = TO_SPECIAL(state->prev_c, c);
1365 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1366 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1367 // in a recording.
1368 state->buflen = 0;
1369 }
1370 // When receiving a multibyte character, store it until we have all
1371 // the bytes, so that it won't be split between two buffer blocks,
1372 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001373 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001374 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001375
1376 if (state->pending_mbyte > 0)
1377 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001378
1379 retval = TRUE;
1380ret_false:
1381 state->prev_c = c;
1382 return retval;
1383}
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385/*
1386 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001387 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 */
1389 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001390gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001392 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001393 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001394 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001395 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
zeertzjqacdfb8a2024-04-17 21:28:54 +02001397 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001399 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001400 continue;
1401
Bram Moolenaar30613902019-12-01 22:11:18 +01001402 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001403 for (i = 0; i < state.buflen; ++i)
1404 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001406 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001408 state.buf[state.buflen] = NUL;
1409 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001410 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001411 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001413 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 may_sync_undo();
1417
1418#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001419 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 debug_did_msg = FALSE;
1421#endif
1422
Bram Moolenaar30613902019-12-01 22:11:18 +01001423 // Since characters have been typed, consider the following to be in
1424 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 ++maptick;
1426}
1427
1428/*
zeertzjqbf321802024-01-28 19:03:00 +01001429 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001430 */
1431 void
zeertzjqbf321802024-01-28 19:03:00 +01001432gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001433{
zeertzjqbf321802024-01-28 19:03:00 +01001434 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001435 gotchars(nop_buf, 3);
1436}
1437
1438/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001439 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1440 * character back into the typeahead buffer, thus gotchars() will be called
1441 * again.
1442 * Only affects recorded characters.
1443 */
1444 void
1445ungetchars(int len)
1446{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001447 if (reg_recording == 0)
1448 return;
1449
1450 delete_buff_tail(&recordbuff, len);
1451 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001452}
1453
1454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 * Sync undo. Called when typed characters are obtained from the typeahead
1456 * buffer, or when a menu is used.
1457 * Do not sync:
1458 * - In Insert mode, unless cursor key has been used.
1459 * - While reading a script file.
1460 * - When no_u_sync is non-zero.
1461 */
1462 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001463may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar24959102022-05-07 20:01:16 +01001465 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001466 && scriptin[curscript] == NULL)
1467 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468}
1469
1470/*
1471 * Make "typebuf" empty and allocate new buffers.
1472 * Returns FAIL when out of memory.
1473 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001474 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001475alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 typebuf.tb_buf = alloc(TYPELEN_INIT);
1478 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1479 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1480 {
1481 free_typebuf();
1482 return FAIL;
1483 }
1484 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001485 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 typebuf.tb_len = 0;
1487 typebuf.tb_maplen = 0;
1488 typebuf.tb_silent = 0;
1489 typebuf.tb_no_abbr_cnt = 0;
1490 if (++typebuf.tb_change_cnt == 0)
1491 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001492#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1493 typebuf_was_filled = FALSE;
1494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return OK;
1496}
1497
1498/*
1499 * Free the buffers of "typebuf".
1500 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001501 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001502free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001504 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001505 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001506 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001507 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001508 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001509 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001510 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001511 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513
1514/*
1515 * When doing ":so! file", the current typeahead needs to be saved, and
1516 * restored when "file" has been read completely.
1517 */
1518static typebuf_T saved_typebuf[NSCRIPT];
1519
1520 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001521save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 init_typebuf();
1524 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001525 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (alloc_typebuf() == FAIL)
1527 {
1528 closescript();
1529 return FAIL;
1530 }
1531 return OK;
1532}
1533
Bram Moolenaar30613902019-12-01 22:11:18 +01001534static int old_char = -1; // character put back by vungetc()
1535static int old_mod_mask; // mod_mask for ungotten character
1536static int old_mouse_row; // mouse_row related to old_char
1537static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001538static int old_KeyStuffed; // whether old_char was stuffed
1539
zeertzjq6d4e7252022-04-07 13:58:04 +01001540static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001541{
1542 // If the old character was not stuffed and characters have been added to
1543 // the stuff buffer, need to first get the stuffed characters instead.
1544 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1545}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547/*
1548 * Save all three kinds of typeahead, so that the user must type at a prompt.
1549 */
1550 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001551save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 tp->save_typebuf = typebuf;
1554 tp->typebuf_valid = (alloc_typebuf() == OK);
1555 if (!tp->typebuf_valid)
1556 typebuf = tp->save_typebuf;
1557
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001558 tp->old_char = old_char;
1559 tp->old_mod_mask = old_mod_mask;
1560 old_char = -1;
1561
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001562 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001563 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001564 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001565 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566# ifdef USE_INPUT_BUF
1567 tp->save_inputbuf = get_input_buf();
1568# endif
1569}
1570
1571/*
1572 * Restore the typeahead to what it was before calling save_typeahead().
1573 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001574 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 */
1576 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001577restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 if (tp->typebuf_valid)
1580 {
1581 free_typebuf();
1582 typebuf = tp->save_typebuf;
1583 }
1584
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001585 old_char = tp->old_char;
1586 old_mod_mask = tp->old_mod_mask;
1587
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001588 free_buff(&readbuf1);
1589 readbuf1 = tp->save_readbuf1;
1590 free_buff(&readbuf2);
1591 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001593 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594# endif
1595}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
1597/*
1598 * Open a new script file for the ":source!" command.
1599 */
1600 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001601openscript(
1602 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001603 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605 if (curscript + 1 == NSCRIPT)
1606 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001607 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return;
1609 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001610
1611 // Disallow sourcing a file in the sandbox, the commands would be executed
1612 // later, possibly outside of the sandbox.
1613 if (check_secure())
1614 return;
1615
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001616#ifdef FEAT_EVAL
1617 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001618 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001619 return;
1620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar30613902019-12-01 22:11:18 +01001622 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001624 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 expand_env(name, NameBuff, MAXPATHL);
1626 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1627 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001628 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (curscript)
1630 --curscript;
1631 return;
1632 }
1633 if (save_typebuf() == FAIL)
1634 return;
1635
1636 /*
1637 * Execute the commands from the file right now when using ":source!"
1638 * after ":global" or ":argdo" or in a loop. Also when another command
1639 * follows. This means the display won't be updated. Don't do this
1640 * always, "make test" would fail.
1641 */
1642 if (directly)
1643 {
1644 oparg_T oa;
1645 int oldcurscript;
1646 int save_State = State;
1647 int save_restart_edit = restart_edit;
1648 int save_insertmode = p_im;
1649 int save_finish_op = finish_op;
1650 int save_msg_scroll = msg_scroll;
1651
Bram Moolenaar24959102022-05-07 20:01:16 +01001652 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001653 msg_scroll = FALSE; // no msg scrolling in Normal mode
1654 restart_edit = 0; // don't go to Insert mode
1655 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 clear_oparg(&oa);
1657 finish_op = FALSE;
1658
1659 oldcurscript = curscript;
1660 do
1661 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001662 update_topline_cursor(); // update cursor position and topline
1663 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001664 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 while (scriptin[oldcurscript] != NULL);
1667
1668 State = save_State;
1669 msg_scroll = save_msg_scroll;
1670 restart_edit = save_restart_edit;
1671 p_im = save_insertmode;
1672 finish_op = save_finish_op;
1673 }
1674}
1675
1676/*
1677 * Close the currently active input script.
1678 */
1679 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001680closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 free_typebuf();
1683 typebuf = saved_typebuf[curscript];
1684
1685 fclose(scriptin[curscript]);
1686 scriptin[curscript] = NULL;
1687 if (curscript > 0)
1688 --curscript;
1689}
1690
Bram Moolenaarea408852005-06-25 22:49:46 +00001691#if defined(EXITFREE) || defined(PROTO)
1692 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001693close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001694{
1695 while (scriptin[0] != NULL)
1696 closescript();
1697}
1698#endif
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Return TRUE when reading keys from a script file.
1702 */
1703 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001704using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 return scriptin[curscript] != NULL;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
1709/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001710 * This function is called just before doing a blocking wait. Thus after
1711 * waiting 'updatetime' for a character to arrive.
1712 */
1713 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001714before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001715{
1716 updatescript(0);
1717#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001718 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001719 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001720#endif
1721}
1722
1723/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001724 * updatescript() is called when a character can be written into the script
1725 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 *
1727 * All the changed memfiles are synced if c == 0 or when the number of typed
1728 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1729 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001730 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001731updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 static int count = 0;
1734
1735 if (c && scriptout)
1736 putc(c, scriptout);
1737 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1738 {
1739 ml_sync_all(c == 0, TRUE);
1740 count = 0;
1741 }
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02001742#ifdef FEAT_EVAL
1743 if (typedchars_pos < MAXMAPLEN)
1744 {
1745 typedchars[typedchars_pos] = c;
1746 typedchars_pos++;
1747 }
1748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001752 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1753 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001754 */
1755 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001756merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001757{
1758 int c = c_arg;
1759
Bram Moolenaarea83c192023-03-18 17:22:46 +00001760 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001761 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001762 {
1763 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001764 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001765 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001766 if (c == NUL)
1767 c = K_ZERO;
1768 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001769 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001770 // CTRL-6 is equivalent to CTRL-^
1771 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001772#ifdef FEAT_GUI_GTK
1773 // These mappings look arbitrary at the first glance, but in fact
1774 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1775 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1776 // more sense and is consistent with usual terminal behaviour).
1777 else if (c == '2')
1778 c = NUL;
1779 else if (c >= '3' && c <= '7')
1780 c = c ^ 0x28;
1781 else if (c == '8')
1782 c = BS;
1783 else if (c == '?')
1784 c = DEL;
1785#endif
1786 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001787 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001788 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001789
1790 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001791 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001792 && c >= 0 && c <= 127)
1793 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001794 // Some terminals (esp. Kitty) do not include Shift in the character.
1795 // Apply it here to get consistency across terminals. Only do ASCII
1796 // letters, for other characters it depends on the keyboard layout.
1797 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1798 {
1799 c += 'a' - 'A';
1800 *modifiers &= ~MOD_MASK_SHIFT;
1801 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001802 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001803 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001804 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001805
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001806 return c;
1807}
1808
1809/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001810 * Add a single byte to 'showcmd' for a partially matched mapping.
1811 * Call add_to_showcmd() if a full key has been received.
1812 */
1813 static void
1814add_byte_to_showcmd(char_u byte)
1815{
1816 static gotchars_state_T state;
1817 char_u *ptr;
1818 int modifiers = 0;
1819 int c = NUL;
1820
1821 if (!p_sc || msg_silent != 0)
1822 return;
1823
1824 if (!gotchars_add_byte(&state, byte))
1825 return;
1826
1827 state.buf[state.buflen] = NUL;
1828 state.buflen = 0;
1829
1830 ptr = state.buf;
1831 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1832 {
1833 modifiers = ptr[2];
1834 ptr += 3;
1835 }
1836
1837 if (*ptr != NUL)
1838 {
1839 char_u *mb_ptr = mb_unescape(&ptr);
1840
1841 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1842 if (c <= 0x7f)
1843 {
1844 // Merge modifiers into the key to make the result more readable.
1845 int modifiers_after = modifiers;
1846 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1847
1848 if (modifiers_after == 0)
1849 {
1850 modifiers = 0;
1851 c = mod_c;
1852 }
1853 }
1854 }
1855
1856 // TODO: is there a more readable and yet compact representation of
1857 // modifiers and special keys?
1858 if (modifiers != 0)
1859 {
1860 add_to_showcmd(K_SPECIAL);
1861 add_to_showcmd(KS_MODIFIER);
1862 add_to_showcmd(modifiers);
1863 }
1864 if (c != NUL)
1865 add_to_showcmd(c);
1866 while (*ptr != NUL)
1867 add_to_showcmd(*ptr++);
1868}
1869
1870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 * Get the next input character.
1872 * Can return a special key or a multi-byte character.
1873 * Can return NUL when called recursively, use safe_vgetc() if that's not
1874 * wanted.
1875 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1876 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001877 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 */
1879 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001880vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001884 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001887#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001888 // Do garbage collection when garbagecollect() was called previously and
1889 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001890 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001891 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001892#endif
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 /*
1895 * If a character was put back with vungetc, it was already processed.
1896 * Return it directly.
1897 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001898 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 c = old_char;
1901 old_char = -1;
1902 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001903 mouse_row = old_mouse_row;
1904 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001906 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
zeertzjq81b46a62022-04-09 17:58:49 +01001908 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001909 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001910
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001911 mod_mask = 0;
1912 vgetc_mod_mask = 0;
1913 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001914
1915 // last_recorded_len can be larger than last_vgetc_recorded_len
1916 // if peeking records more
1917 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001918
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001919 for (;;) // this is done twice if there are modifiers
1920 {
1921 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001922
Bram Moolenaar78aed952022-09-24 15:36:35 +01001923 // No mapping after modifier has been read, using an input method
1924 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001925 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001926#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001927 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001928#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001929#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001930 || popup_no_mapping()
1931#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001932 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001934 ++no_mapping;
1935 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001936 // mod_mask value may change, remember we did the increment
1937 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001939 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001940 if (did_inc)
1941 {
1942 --no_mapping;
1943 --allow_keys;
1944 }
1945
Christopher Plewright03193062022-11-22 12:58:27 +00001946 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001947 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001948#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001949 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001950#endif
1951 )
1952 {
1953 int save_allow_keys = allow_keys;
1954
1955 ++no_mapping;
1956 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001957 c2 = vgetorpeek(TRUE); // no mapping for these chars
1958 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001959 --no_mapping;
1960 allow_keys = save_allow_keys;
1961 if (c2 == KS_MODIFIER)
1962 {
1963 mod_mask = c;
1964 continue;
1965 }
1966 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001968 // K_ESC is used to avoid ambiguity with the single Esc
1969 // character that might be the start of an escape sequence.
1970 // Convert it back to a single Esc here.
1971 if (c == K_ESC)
1972 c = ESC;
1973
Bram Moolenaar4f974752019-02-17 17:44:42 +01001974#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001975 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1976 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001977 if (
1978# ifdef VIMDLL
1979 gui.in_use &&
1980# endif
1981 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001983 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001984 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001985
1986 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001987 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001988 {
K.Takata54119102022-02-03 13:33:03 +00001989 name[j] = c;
1990 if (j < 199)
1991 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001992 }
K.Takata54119102022-02-03 13:33:03 +00001993 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001994 gui_make_tearoff(name);
1995 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001998#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001999 // GTK: <F10> normally selects the menu, but it's passed until
2000 // here to allow mapping it. Intercept and invoke the GTK
2001 // behavior if it's not mapped.
2002 if (c == K_F10 && gui.menubar != NULL)
2003 {
2004 gtk_menu_shell_select_first(
2005 GTK_MENU_SHELL(gui.menubar), FALSE);
2006 continue;
2007 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002010 // Handle focus event here, so that the caller doesn't need to
2011 // know about it. Return K_IGNORE so that we loop once (needed
2012 // if 'lazyredraw' is set).
2013 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002014 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002015 ui_focus_change(c == K_FOCUSGAINED);
2016 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02002017 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002018
2019 // Translate K_CSI to CSI. The special key is only used to
2020 // avoid it being recognized as the start of a special key.
2021 if (c == K_CSI)
2022 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002024#ifdef FEAT_EVAL
2025 if (c == K_SID)
2026 {
2027 int j;
2028
2029 // Handle <SID>{sid}; Do up to 20 digits for safety.
2030 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002031 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002032 last_used_sid = last_used_sid * 10 + (c - '0');
2033 last_used_map = NULL;
2034 continue;
2035 }
2036#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002037 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002038
zeertzjq90a80022024-07-13 19:06:44 +02002039 // For a multi-byte character get all the bytes and return the
2040 // converted character.
2041 // Note: This will loop until enough bytes are received!
2042 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2043 {
2044 ++no_mapping;
2045 buf[0] = c;
2046 for (i = 1; i < n; ++i)
2047 {
2048 buf[i] = vgetorpeek(TRUE);
2049 if (buf[i] == K_SPECIAL
2050#ifdef FEAT_GUI
2051 || (buf[i] == CSI)
2052#endif
2053 )
2054 {
2055 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2056 // sequence, which represents a K_SPECIAL (0x80),
2057 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2058 // represents a CSI (0x9B),
2059 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2060 // too.
2061 c = vgetorpeek(TRUE);
2062 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2063 buf[i] = CSI;
2064 }
2065 }
2066 --no_mapping;
2067 c = (*mb_ptr2char)(buf);
2068 }
2069
2070 if (vgetc_char == 0)
2071 {
2072 vgetc_mod_mask = mod_mask;
2073 vgetc_char = c;
2074 }
2075
zeertzjqd9be94c2024-07-14 10:20:20 +02002076 // A keypad or special function key was not mapped, use it like
2077 // its ASCII equivalent.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002078 switch (c)
2079 {
2080 case K_KPLUS: c = '+'; break;
2081 case K_KMINUS: c = '-'; break;
2082 case K_KDIVIDE: c = '/'; break;
2083 case K_KMULTIPLY: c = '*'; break;
2084 case K_KENTER: c = CAR; break;
2085 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002086#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002087 // Can be either '.' or a ',',
2088 // depending on the type of keypad.
2089 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002090#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002091 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002092#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002093 case K_K0: c = '0'; break;
2094 case K_K1: c = '1'; break;
2095 case K_K2: c = '2'; break;
2096 case K_K3: c = '3'; break;
2097 case K_K4: c = '4'; break;
2098 case K_K5: c = '5'; break;
2099 case K_K6: c = '6'; break;
2100 case K_K7: c = '7'; break;
2101 case K_K8: c = '8'; break;
2102 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002103
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002104 case K_XHOME:
2105 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002106 {
2107 c = K_S_HOME;
2108 mod_mask = 0;
2109 }
2110 else if (mod_mask == MOD_MASK_CTRL)
2111 {
2112 c = K_C_HOME;
2113 mod_mask = 0;
2114 }
2115 else
2116 c = K_HOME;
2117 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002118 case K_XEND:
2119 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002120 {
2121 c = K_S_END;
2122 mod_mask = 0;
2123 }
2124 else if (mod_mask == MOD_MASK_CTRL)
2125 {
2126 c = K_C_END;
2127 mod_mask = 0;
2128 }
2129 else
2130 c = K_END;
2131 break;
2132
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002133 case K_XUP: c = K_UP; break;
2134 case K_XDOWN: c = K_DOWN; break;
2135 case K_XLEFT: c = K_LEFT; break;
2136 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002139 break;
2140 }
zeertzjq81b46a62022-04-09 17:58:49 +01002141
2142 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002144
2145#ifdef FEAT_EVAL
2146 /*
2147 * In the main loop "may_garbage_collect" can be set to do garbage
2148 * collection in the first next vgetc(). It's disabled after that to
2149 * avoid internally used Lists and Dicts to be freed.
2150 */
2151 may_garbage_collect = FALSE;
2152#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002153
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002154#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002155 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002156 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002157 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002158 bevalexpr_due_set = FALSE;
2159 ui_remove_balloon();
2160 }
2161#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002162#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002163 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2164 // are filtered.
2165 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002166 {
2167 if (c == Ctrl_C)
2168 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002169 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002170 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002171#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002172
Shougo Matsushita83678842024-07-11 22:05:12 +02002173#ifdef FEAT_EVAL
2174 c = do_key_input_pre(c);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002175
2176 // Clear the next typedchars_pos
2177 typedchars_pos = 0;
Shougo Matsushita83678842024-07-11 22:05:12 +02002178#endif
2179
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002180 // Need to process the character before we know it's safe to do something
2181 // else.
2182 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002183 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002184
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002185 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186}
2187
Shougo Matsushita83678842024-07-11 22:05:12 +02002188#ifdef FEAT_EVAL
2189/*
2190 * Handle the InsertCharPre autocommand.
2191 * "c" is the character that was typed.
2192 * Return new input character.
2193 */
2194 static int
2195do_key_input_pre(int c)
2196{
2197 int res = c;
2198 char_u buf[MB_MAXBYTES + 1];
2199 char_u curr_mode[MODE_MAX_LENGTH];
2200 int save_State = State;
2201 save_v_event_T save_v_event;
2202 dict_T *v_event;
2203
2204 // Return quickly when there is nothing to do.
2205 if (!has_keyinputpre())
2206 return res;
2207
2208 if (IS_SPECIAL(c))
2209 {
2210 buf[0] = K_SPECIAL;
2211 buf[1] = KEY2TERMCAP0(c);
2212 buf[2] = KEY2TERMCAP1(c);
2213 buf[3] = NUL;
2214 }
2215 else
2216 buf[(*mb_char2bytes)(c, buf)] = NUL;
2217
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002218 typedchars[typedchars_pos] = NUL;
2219 vim_unescape_csi(typedchars);
2220
Shougo Matsushita83678842024-07-11 22:05:12 +02002221 get_mode(curr_mode);
2222
2223 // Lock the text to avoid weird things from happening.
2224 ++textlock;
2225 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2226
2227 v_event = get_v_event(&save_v_event);
2228 (void)dict_add_bool(v_event, "typed", KeyTyped);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002229 (void)dict_add_string(v_event, "typedchar", typedchars);
Shougo Matsushita83678842024-07-11 22:05:12 +02002230
2231 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2232 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2233 {
2234 // Get the value of v:char. It may be empty or more than one
2235 // character. Only use it when changed, otherwise continue with the
2236 // original character.
2237 char_u *v_char;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002238 size_t v_charlen;
Shougo Matsushita83678842024-07-11 22:05:12 +02002239
2240 v_char = get_vim_var_str(VV_CHAR);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002241 v_charlen = STRLEN(v_char);
Shougo Matsushita83678842024-07-11 22:05:12 +02002242
2243 // Convert special bytes when it is special string.
John Marriotte7a1bbf2024-11-11 20:40:33 +01002244 if (v_charlen >= 3 && v_char[0] == K_SPECIAL)
Shougo Matsushita83678842024-07-11 22:05:12 +02002245 res = TERMCAP2KEY(v_char[1], v_char[2]);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002246 else if (v_charlen > 0)
Shougo Matsushita83678842024-07-11 22:05:12 +02002247 res = PTR2CHAR(v_char);
2248 }
2249
2250 restore_v_event(v_event, &save_v_event);
2251
2252 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2253 --textlock;
2254
2255 // Restore the State, it may have been changed.
2256 State = save_State;
2257
2258 return res;
2259}
2260#endif
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262/*
2263 * Like vgetc(), but never return a NUL when called recursively, get a key
2264 * directly from the user (ignoring typeahead).
2265 */
2266 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002267safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 int c;
2270
2271 c = vgetc();
2272 if (c == NUL)
2273 c = get_keystroke();
2274 return c;
2275}
2276
2277/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002278 * Like safe_vgetc(), but loop to handle K_IGNORE.
2279 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002280 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002281 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002282 static int
2283plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002284{
2285 int c;
2286
2287 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002288 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002289 while (c == K_IGNORE
2290 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2291 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002292 return c;
2293}
2294
2295/*
2296 * Like safe_vgetc(), but loop to handle K_IGNORE.
2297 * Also ignore scrollbar events.
2298 */
2299 int
2300plain_vgetc(void)
2301{
2302 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002303
2304 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002305 // Only handle the first pasted character. Drop the rest, since we
2306 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002307 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2308
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002309 return c;
2310}
2311
2312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * Check if a character is available, such that vgetc() will not block.
2314 * If the next character is a special character or multi-byte, the returned
2315 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002316 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 */
2318 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002319vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002321 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002323 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324}
2325
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002326#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327/*
2328 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2329 * codes.
2330 */
2331 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002332vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 int c;
2335
2336 ++no_mapping;
2337 ++allow_keys;
2338 c = vpeekc();
2339 --no_mapping;
2340 --allow_keys;
2341 return c;
2342}
2343#endif
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345/*
2346 * Check if any character is available, also half an escape sequence.
2347 * Trick: when no typeahead found, but there is something in the typeahead
2348 * buffer, it must be an ESC that is recognized as the start of a key code.
2349 */
2350 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002351vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
2353 int c;
2354
2355 c = vpeekc();
2356 if (c == NUL && typebuf.tb_len > 0)
2357 c = ESC;
2358 return c;
2359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361/*
2362 * Call vpeekc() without causing anything to be mapped.
2363 * Return TRUE if a character is available, FALSE otherwise.
2364 */
2365 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002366char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 int retval;
2369
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002370#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002371 // When test_override("char_avail", 1) was called pretend there is no
2372 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002373 if (disable_char_avail_for_testing)
2374 return FALSE;
2375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 ++no_mapping;
2377 retval = vpeekc();
2378 --no_mapping;
2379 return (retval != NUL);
2380}
2381
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002382#if defined(FEAT_EVAL) || defined(PROTO)
2383/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002384 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002385 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002386 static void
zeertzjqe0a2ab32025-02-02 09:14:35 +01002387getchar_common(typval_T *argvars, typval_T *rettv, int allow_number)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002388{
2389 varnumber_T n;
2390 int error = FALSE;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002391 int simplify = TRUE;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002392
zeertzjqe0a2ab32025-02-02 09:14:35 +01002393 if ((in_vim9script()
2394 && check_for_opt_bool_or_number_arg(argvars, 0) == FAIL)
2395 || (argvars[0].v_type != VAR_UNKNOWN
2396 && check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002397 return;
2398
zeertzjqe0a2ab32025-02-02 09:14:35 +01002399 if (argvars[0].v_type != VAR_UNKNOWN && argvars[1].v_type == VAR_DICT)
2400 {
2401 dict_T *d = argvars[1].vval.v_dict;
2402
2403 if (allow_number)
2404 allow_number = dict_get_bool(d, "number", TRUE);
2405 else if (dict_has_key(d, "number"))
2406 {
2407 semsg(_(e_invalid_argument_str), "number");
2408 error = TRUE;
2409 }
2410
2411 simplify = dict_get_bool(d, "simplify", TRUE);
2412 }
2413
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002414#ifdef MESSAGE_QUEUE
2415 // vpeekc() used to check for messages, but that caused problems, invoking
2416 // a callback where it was not expected. Some plugins use getchar(1) in a
2417 // loop to await a message, therefore make sure we check for messages here.
2418 parse_queued_messages();
2419#endif
2420
Bram Moolenaar30613902019-12-01 22:11:18 +01002421 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002422 windgoto(msg_row, msg_col);
2423
2424 ++no_mapping;
2425 ++allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002426 if (!simplify)
2427 ++no_reduce_keys;
2428 while (!error)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002429 {
zeertzjqe0a2ab32025-02-02 09:14:35 +01002430 if (argvars[0].v_type == VAR_UNKNOWN
2431 || (argvars[0].v_type == VAR_NUMBER
2432 && argvars[0].vval.v_number == -1))
Bram Moolenaar30613902019-12-01 22:11:18 +01002433 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002434 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002435 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002436 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002437 n = vpeekc_any();
2438 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002439 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002440 n = 0;
2441 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002442 // getchar(0) and char avail() != NUL: get a character.
2443 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2444 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002445
Bram Moolenaar15183b42020-09-05 19:59:39 +02002446 if (n == K_IGNORE || n == K_MOUSEMOVE
2447 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002448 continue;
2449 break;
2450 }
2451 --no_mapping;
2452 --allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002453 if (!simplify)
2454 --no_reduce_keys;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002455
2456 set_vim_var_nr(VV_MOUSE_WIN, 0);
2457 set_vim_var_nr(VV_MOUSE_WINID, 0);
2458 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2459 set_vim_var_nr(VV_MOUSE_COL, 0);
2460
zeertzjqe0a2ab32025-02-02 09:14:35 +01002461 if (n != 0 && (!allow_number || IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002462 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002463 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002464 int i = 0;
2465
Bram Moolenaar30613902019-12-01 22:11:18 +01002466 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002467 if (mod_mask != 0)
2468 {
2469 temp[i++] = K_SPECIAL;
2470 temp[i++] = KS_MODIFIER;
2471 temp[i++] = mod_mask;
2472 }
2473 if (IS_SPECIAL(n))
2474 {
2475 temp[i++] = K_SPECIAL;
2476 temp[i++] = K_SECOND(n);
2477 temp[i++] = K_THIRD(n);
2478 }
2479 else if (has_mbyte)
2480 i += (*mb_char2bytes)(n, temp + i);
2481 else
2482 temp[i++] = n;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002483 temp[i] = NUL;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002484 rettv->v_type = VAR_STRING;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002485 rettv->vval.v_string = vim_strnsave(temp, i);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002486
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002487 if (is_mouse_key(n))
2488 {
2489 int row = mouse_row;
2490 int col = mouse_col;
2491 win_T *win;
2492 linenr_T lnum;
2493 win_T *wp;
2494 int winnr = 1;
2495
2496 if (row >= 0 && col >= 0)
2497 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002498 // Find the window at the mouse coordinates and compute the
2499 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002500 win = mouse_find_win(&row, &col, FIND_POPUP);
2501 if (win == NULL)
2502 return;
2503 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002504#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002505 if (WIN_IS_POPUP(win))
2506 winnr = 0;
2507 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002508#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002509 for (wp = firstwin; wp != win && wp != NULL;
2510 wp = wp->w_next)
2511 ++winnr;
2512 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2513 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2514 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2515 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2516 }
2517 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002518 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002519 else if (!allow_number)
2520 rettv->v_type = VAR_STRING;
2521 else
2522 rettv->vval.v_number = n;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002523}
2524
2525/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002526 * "getchar()" function
2527 */
2528 void
2529f_getchar(typval_T *argvars, typval_T *rettv)
2530{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002531 getchar_common(argvars, rettv, TRUE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002532}
2533
2534/*
2535 * "getcharstr()" function
2536 */
2537 void
2538f_getcharstr(typval_T *argvars, typval_T *rettv)
2539{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002540 getchar_common(argvars, rettv, FALSE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002541}
2542
2543/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002544 * "getcharmod()" function
2545 */
2546 void
2547f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2548{
2549 rettv->vval.v_number = mod_mask;
2550}
2551#endif // FEAT_EVAL
2552
2553#if defined(MESSAGE_QUEUE) || defined(PROTO)
2554# define MAX_REPEAT_PARSE 8
2555
2556/*
2557 * Process messages that have been queued for netbeans or clientserver.
2558 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002559 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002560 * it is safe to do so.
2561 */
2562 void
2563parse_queued_messages(void)
2564{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002565 int old_curwin_id;
2566 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002567 int i;
2568 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002569 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002570 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002571
2572 // Do not handle messages while redrawing, because it may cause buffers to
2573 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002574 // Also bail out when parsing messages was explicitly disabled.
2575 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002576 return;
2577
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002578 // If memory allocation fails during startup we'll exit but curbuf or
2579 // curwin could be NULL.
2580 if (curbuf == NULL || curwin == NULL)
2581 return;
2582
2583 old_curbuf_fnum = curbuf->b_fnum;
2584 old_curwin_id = curwin->w_id;
2585
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002586 ++entered;
2587
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002588 // may_garbage_collect is set in main_loop() to do garbage collection when
2589 // blocking to wait on a character. We don't want that while parsing
2590 // messages, a callback may invoke vgetc() while lists and dicts are in use
2591 // in the call stack.
2592 may_garbage_collect = FALSE;
2593
2594 // Loop when a job ended, but don't keep looping forever.
2595 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2596 {
2597 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002598# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002599 channel_handle_events(FALSE);
2600# endif
2601
2602# ifdef FEAT_NETBEANS_INTG
2603 // Process the queued netbeans messages.
2604 netbeans_parse_messages();
2605# endif
2606# ifdef FEAT_JOB_CHANNEL
2607 // Write any buffer lines still to be written.
2608 channel_write_any_lines();
2609
2610 // Process the messages queued on channels.
2611 channel_parse_messages();
2612# endif
2613# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2614 // Process the queued clientserver messages.
2615 server_parse_messages();
2616# endif
2617# ifdef FEAT_JOB_CHANNEL
2618 // Check if any jobs have ended. If so, repeat the above to handle
2619 // changes, e.g. stdin may have been closed.
2620 if (job_check_ended())
2621 continue;
2622# endif
2623# ifdef FEAT_TERMINAL
2624 free_unused_terminals();
2625# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002626
2627# ifdef FEAT_SOUND_MACOSX
2628 process_cfrunloop();
2629# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002630# ifdef FEAT_SOUND_CANBERRA
2631 if (has_sound_callback_in_queue())
2632 invoke_sound_callback();
2633# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002634#ifdef SIGUSR1
2635 if (got_sigusr1)
2636 {
2637 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2638 got_sigusr1 = FALSE;
2639 }
2640#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002641 break;
2642 }
2643
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002644 // When not nested we'll go back to waiting for a typed character. If it
2645 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002646 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002647 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002648
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002649 may_garbage_collect = save_may_garbage_collect;
2650
2651 // If the current window or buffer changed we need to bail out of the
2652 // waiting loop. E.g. when a job exit callback closes the terminal window.
2653 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002654 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002655
2656 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002657}
2658#endif
2659
2660
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002661typedef enum {
2662 map_result_fail, // failed, break loop
2663 map_result_get, // get a character from typeahead
2664 map_result_retry, // try to map again
2665 map_result_nomatch // no matching mapping, get char
2666} map_result_T;
2667
2668/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002669 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002670 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002671 */
2672 static int
zeertzjqee446032022-04-30 15:02:22 +01002673at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002674{
2675 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2676 int c = *p;
2677
2678 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002679 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002680 && p[1] == KS_MODIFIER
2681 && (p[2] & MOD_MASK_CTRL))
2682 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002683 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2684 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002685}
2686
2687/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002688 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002689 * into just a key, apply that.
2690 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2691 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002692 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002693 */
2694 static int
2695check_simplify_modifier(int max_offset)
2696{
2697 int offset;
2698 char_u *tp;
2699
2700 for (offset = 0; offset < max_offset; ++offset)
2701 {
2702 if (offset + 3 >= typebuf.tb_len)
2703 break;
2704 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002705 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002706 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002707 // A modifier was not used for a mapping, apply it to ASCII keys.
2708 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002709 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002710 int c = tp[3];
2711 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002712
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002713 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002714 {
2715 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002716 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002717
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002718 if (offset == 0)
2719 {
2720 // At the start: remember the character and mod_mask before
2721 // merging, in some cases, e.g. at the hit-return prompt,
2722 // they are put back in the typeahead buffer.
2723 vgetc_char = c;
2724 vgetc_mod_mask = tp[2];
2725 }
zeertzjq12e21e32022-04-27 11:58:01 +01002726 if (IS_SPECIAL(new_c))
2727 {
2728 new_string[0] = K_SPECIAL;
2729 new_string[1] = K_SECOND(new_c);
2730 new_string[2] = K_THIRD(new_c);
2731 len = 3;
2732 }
2733 else
2734 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002735 if (modifier == 0)
2736 {
2737 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002738 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002739 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002740 }
2741 else
2742 {
2743 tp[2] = modifier;
2744 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002745 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002746 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002747 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002748 return len;
2749 }
2750 }
2751 }
2752 return 0;
2753}
2754
2755/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002756 * Return TRUE if the terminal sends modifiers with various keys. This is when
2757 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2758 * enabled.
2759 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002760 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002761key_protocol_enabled(void)
2762{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002763 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2764 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2765 ? modify_otherkeys_state == MOKS_ENABLED
2766 : seenModifyOtherKeys;
2767 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002768}
2769
2770/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002771 * Handle mappings in the typeahead buffer.
2772 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002773 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002774 * - When there is no match yet, return map_result_nomatch, need to get more
2775 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002776 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002777 */
2778 static int
2779handle_mapping(
2780 int *keylenp,
2781 int *timedout,
2782 int *mapdepth)
2783{
2784 mapblock_T *mp = NULL;
2785 mapblock_T *mp2;
2786 mapblock_T *mp_match;
2787 int mp_match_len = 0;
2788 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002789 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002790 int tb_c1;
2791 int mlen;
2792#ifdef FEAT_LANGMAP
2793 int nolmaplen;
2794#endif
2795 int keylen = *keylenp;
2796 int i;
2797 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002798 int is_plug_map = FALSE;
2799
zeertzjqbb404f52022-07-23 06:25:29 +01002800 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002801 if (typebuf.tb_len >= 3
2802 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002803 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2804 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2805 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002806
2807 /*
2808 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002809 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002810 *
2811 * Don't look for mappings if:
2812 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2813 * - maphash_valid not set: no mappings present.
2814 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2815 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002816 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002817 * - waiting for a char with --more--
2818 * - in Ctrl-X mode, and we get a valid char for that mode
2819 */
2820 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2821 if (no_mapping == 0 && is_maphash_valid()
2822 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002823 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002824 || (p_remap
2825 && (typebuf.tb_noremap[typebuf.tb_off]
2826 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002827 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2828 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2829 && State != MODE_ASKMORE
2830 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002831 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002832 {
Christopher Plewright03193062022-11-22 12:58:27 +00002833#ifdef FEAT_GUI
2834 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2835 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002836 {
2837 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2838 // K_SPECIAL KS_MODIFIER {flags}.
2839 tb_c1 = K_SPECIAL;
2840 }
2841#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002842#ifdef FEAT_LANGMAP
2843 if (tb_c1 == K_SPECIAL)
2844 nolmaplen = 2;
2845 else
2846 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002847 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2848 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002849 nolmaplen = 0;
2850 }
2851#endif
2852 // First try buffer-local mappings.
2853 mp = get_buf_maphash_list(local_State, tb_c1);
2854 mp2 = get_maphash_list(local_State, tb_c1);
2855 if (mp == NULL)
2856 {
2857 // There are no buffer-local mappings.
2858 mp = mp2;
2859 mp2 = NULL;
2860 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002861
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002862 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002863 * Loop until a partly matching mapping is found or all (local)
2864 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002866 * A full match is only accepted if there is no partly match, so "aa"
2867 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002868 */
2869 mp_match = NULL;
2870 mp_match_len = 0;
2871 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002872 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002873 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002874 // Only consider an entry if the first character matches and it is
2875 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002876 // Skip ":lmap" mappings if keys were mapped.
2877 if (mp->m_keys[0] == tb_c1
2878 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002879 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002880 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002881 && ((mp->m_mode & MODE_LANGMAP) == 0
2882 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002883 {
2884#ifdef FEAT_LANGMAP
2885 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002886 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002887#endif
2888 // find the match length of this mapping
2889 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2890 {
zeertzjq49660f52022-10-20 17:59:38 +01002891 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002892#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002893 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002894 {
2895 if (nomap == 2 && c2 == KS_MODIFIER)
2896 modifiers = 1;
2897 else if (nomap == 1 && modifiers == 1)
2898 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002899 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002900 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002901 else
zeertzjq49660f52022-10-20 17:59:38 +01002902 {
2903 if (c2 == K_SPECIAL)
2904 nomap = 2;
2905 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2906 // Only apply 'langmap' if merging modifiers into
2907 // the key will not result in another character,
2908 // so that 'langmap' behaves consistently in
2909 // different terminals and GUIs.
2910 LANGMAP_ADJUST(c2, TRUE);
2911 modifiers = 0;
2912 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002913#endif
zeertzjq49660f52022-10-20 17:59:38 +01002914 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002915 break;
2916 }
2917
Bram Moolenaareda35f72019-08-03 14:59:44 +02002918 // Don't allow mapping the first byte(s) of a multi-byte char.
2919 // Happens when mapping <M-a> and then changing 'encoding'.
2920 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002921 {
2922 char_u *p1 = mp->m_keys;
2923 char_u *p2 = mb_unescape(&p1);
2924
2925 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002926 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002927 mlen = 0;
2928 }
2929
2930 // Check an entry whether it matches.
2931 // - Full match: mlen == keylen
2932 // - Partly match: mlen == typebuf.tb_len
2933 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002934 if (mlen == keylen || (mlen == typebuf.tb_len
2935 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002936 {
2937 char_u *s;
2938 int n;
2939
Bram Moolenaareda35f72019-08-03 14:59:44 +02002940 // If only script-local mappings are allowed, check if the
2941 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002942 s = typebuf.tb_noremap + typebuf.tb_off;
2943 if (*s == RM_SCRIPT
2944 && (mp->m_keys[0] != K_SPECIAL
2945 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002946 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002947 continue;
2948
Bram Moolenaareda35f72019-08-03 14:59:44 +02002949 // If one of the typed keys cannot be remapped, skip the
2950 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002951 for (n = mlen; --n >= 0; )
2952 if (*s++ & (RM_NONE|RM_ABBR))
2953 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002954 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002955 continue;
2956
2957 if (keylen > typebuf.tb_len)
2958 {
2959 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002960 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002961 {
2962 // break at a partly match
2963 keylen = KEYLEN_PART_MAP;
2964 break;
2965 }
2966 }
2967 else if (keylen > mp_match_len)
2968 {
2969 // found a longer match
2970 mp_match = mp;
2971 mp_match_len = keylen;
2972 }
2973 }
2974 else
Oleg Goncharov56904f92024-07-23 20:34:15 +02002975 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002976 // No match; may have to check for termcode at next
Oleg Goncharov56904f92024-07-23 20:34:15 +02002977 // character.
2978
2979 // If the first character that didn't match is
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002980 // K_SPECIAL then check for a termcode. This isn't perfect
2981 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002982 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002983 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002984 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002985 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2986 }
2987 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2988 want_termcode = 1;
Oleg Goncharov56904f92024-07-23 20:34:15 +02002989
2990 // Check termcode for uppercase character to properly
2991 // process "ESC[27;2;<ascii code>~" control sequences.
2992 if (ASCII_ISUPPER(mp->m_keys[mlen]))
2993 want_termcode = 1;
2994 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002995 }
2996 }
2997
Bram Moolenaareda35f72019-08-03 14:59:44 +02002998 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002999 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003000 {
3001 mp = mp_match;
3002 keylen = mp_match_len;
3003 }
3004 }
3005
3006 /*
3007 * Check for match with 'pastetoggle'
3008 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003009 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003010 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003011 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
3012 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003013 break;
3014 if (p_pt[mlen] == NUL) // match
3015 {
3016 // write chars to script file(s)
3017 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003018 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3019 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003020
3021 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003022 set_option_value_give_err((char_u *)"paste",
3023 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01003024 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003025 {
3026 msg_col = 0;
3027 msg_row = Rows - 1;
3028 msg_clr_eos(); // clear ruler
3029 }
3030 status_redraw_all();
3031 redraw_statuslines();
3032 showmode();
3033 setcursor();
3034 *keylenp = keylen;
3035 return map_result_retry;
3036 }
3037 // Need more chars for partly match.
3038 if (mlen == typebuf.tb_len)
3039 keylen = KEYLEN_PART_KEY;
3040 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003041 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003042 max_mlen = mlen + 1;
3043 }
3044
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003045 // May check for a terminal code when there is no mapping or only a partial
3046 // mapping. Also check if there is a full mapping with <Esc>, unless timed
3047 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003048 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003049 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
3050 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003051 {
3052 int save_keylen = keylen;
3053
3054 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003055 * When no matching mapping found or found a non-matching mapping that
3056 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003057 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02003058 * - mapping is allowed,
3059 * - keys have not been mapped,
3060 * - and not an ESC sequence, not in insert mode or p_ek is on,
3061 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003062 */
zeertzjq68a573c2022-04-28 14:10:01 +01003063 if (no_mapping == 0 || allow_keys != 0)
3064 {
3065 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003066 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003067 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003068 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003069 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3070 else
3071 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003072
Bram Moolenaar0684e362020-12-03 19:54:42 +01003073 // If no termcode matched but 'pastetoggle' matched partially
3074 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003075 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003076 keylen = KEYLEN_PART_KEY;
3077
Bram Moolenaar975a8802020-06-06 22:36:24 +02003078 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003079 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003080#ifdef FEAT_TERMINAL
3081 check_no_reduce_keys(); // may update the no_reduce_keys flag
3082#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003083 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003084 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003085 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003086 if (keylen < 0)
3087 // ins_typebuf() failed
3088 return map_result_fail;
3089 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003090
Bram Moolenaareda35f72019-08-03 14:59:44 +02003091 // When getting a partial match, but the last characters were not
3092 // typed, don't wait for a typed character to complete the
3093 // termcode. This helps a lot when a ":normal" command ends in an
3094 // ESC.
3095 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003096 keylen = 0;
3097 }
3098 else
3099 keylen = 0;
3100 if (keylen == 0) // no matching terminal code
3101 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003102#ifdef AMIGA
3103 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003104 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003105 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003106 {
3107 char_u *s;
3108
3109 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003110 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3111 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003112 ++s)
3113 ;
3114 if (*s == 'r' || *s == '|') // found one
3115 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003116 del_typebuf(
3117 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003118 // get size and redraw screen
3119 shell_resized();
3120 *keylenp = keylen;
3121 return map_result_retry;
3122 }
3123 if (*s == NUL) // need more characters
3124 keylen = KEYLEN_PART_KEY;
3125 }
3126 if (keylen >= 0)
3127#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003128 // When there was a matching mapping and no termcode could be
3129 // replaced after another one, use that mapping (loop around).
3130 // If there was no mapping at all use the character from the
3131 // typeahead buffer right here.
3132 if (mp == NULL)
3133 {
3134 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003135 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003136 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003137 }
3138
3139 if (keylen > 0) // full matching terminal code
3140 {
3141#if defined(FEAT_GUI) && defined(FEAT_MENU)
3142 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003143 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3144 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003145 {
3146 int idx;
3147
Bram Moolenaareda35f72019-08-03 14:59:44 +02003148 // Using a menu may cause a break in undo! It's like using
3149 // gotchars(), but without recording or writing to a script
3150 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003151 may_sync_undo();
3152 del_typebuf(3, 0);
3153 idx = get_menu_index(current_menu, local_State);
3154 if (idx != MENU_INDEX_INVALID)
3155 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003156 // In Select mode and a Visual mode menu is used: Switch
3157 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003158 // back to Select mode.
3159 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003160 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003161 {
3162 VIsual_select = FALSE;
3163 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003164 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003165 }
3166 ins_typebuf(current_menu->strings[idx],
3167 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003168 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003169 }
3170 }
3171#endif // FEAT_GUI && FEAT_MENU
3172 *keylenp = keylen;
3173 return map_result_retry; // try mapping again
3174 }
3175
Bram Moolenaareda35f72019-08-03 14:59:44 +02003176 // Partial match: get some more characters. When a matching mapping
3177 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003178 if (mp == NULL || keylen < 0)
3179 keylen = KEYLEN_PART_KEY;
3180 else
3181 keylen = mp_match_len;
3182 }
3183
3184 /*
3185 * complete match
3186 */
3187 if (keylen >= 0 && keylen <= typebuf.tb_len)
3188 {
3189 char_u *map_str;
3190
3191#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003192 int save_m_expr;
3193 int save_m_noremap;
3194 int save_m_silent;
3195 char_u *save_m_keys;
zeertzjq9d997ad2024-07-29 21:10:07 +02003196 char_u *save_alt_m_keys;
zeertzjq74011dc2024-07-30 19:17:56 +02003197 int save_alt_m_keylen;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003198#else
3199# define save_m_noremap mp->m_noremap
3200# define save_m_silent mp->m_silent
3201#endif
3202
3203 // write chars to script file(s)
3204 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003205 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3206 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003207
3208 cmd_silent = (typebuf.tb_silent > 0);
3209 del_typebuf(keylen, 0); // remove the mapped keys
3210
3211 /*
3212 * Put the replacement string in front of mapstr.
3213 * The depth check catches ":map x y" and ":map y x".
3214 */
3215 if (++*mapdepth >= p_mmd)
3216 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003217 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003218 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003219 redrawcmdline();
3220 else
3221 setcursor();
3222 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003223 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003224 *keylenp = keylen;
3225 return map_result_fail;
3226 }
3227
3228 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003229 * In Select mode and a Visual mode mapping is used: Switch to Visual
3230 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003231 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003232 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003233 {
3234 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003235 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003236 }
3237
3238#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003239 // Copy the values from *mp that are used, because evaluating the
3240 // expression may invoke a function that redefines the mapping, thereby
3241 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003242 save_m_expr = mp->m_expr;
3243 save_m_noremap = mp->m_noremap;
3244 save_m_silent = mp->m_silent;
3245 save_m_keys = NULL; // only saved when needed
zeertzjq9d997ad2024-07-29 21:10:07 +02003246 save_alt_m_keys = NULL; // only saved when needed
zeertzjq74011dc2024-07-30 19:17:56 +02003247 save_alt_m_keylen = mp->m_alt != NULL ? mp->m_alt->m_keylen : 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003248
3249 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003250 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3251 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003252 */
3253 if (mp->m_expr)
3254 {
3255 int save_vgetc_busy = vgetc_busy;
3256 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003257 int was_screen_col = screen_cur_col;
3258 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003259 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003260
3261 vgetc_busy = 0;
3262 may_garbage_collect = FALSE;
3263
zeertzjq74011dc2024-07-30 19:17:56 +02003264 save_m_keys = vim_strnsave(mp->m_keys, (size_t)mp->m_keylen);
zeertzjq9d997ad2024-07-29 21:10:07 +02003265 save_alt_m_keys = mp->m_alt != NULL
zeertzjq74011dc2024-07-30 19:17:56 +02003266 ? vim_strnsave(mp->m_alt->m_keys,
3267 (size_t)save_alt_m_keylen) : NULL;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003268 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003269
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003270 // The mapping may do anything, but we expect it to take care of
3271 // redrawing. Do put the cursor back where it was.
3272 windgoto(was_screen_row, was_screen_col);
3273 out_flush();
3274
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003275 // If an error was displayed and the expression returns an empty
3276 // string, generate a <Nop> to allow for a redraw.
3277 if (prev_did_emsg != did_emsg
3278 && (map_str == NULL || *map_str == NUL))
3279 {
3280 char_u buf[4];
3281
3282 vim_free(map_str);
3283 buf[0] = K_SPECIAL;
3284 buf[1] = KS_EXTRA;
3285 buf[2] = KE_IGNORE;
3286 buf[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +01003287 map_str = vim_strnsave(buf, 3);
Bram Moolenaar24959102022-05-07 20:01:16 +01003288 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003289 {
3290 // redraw the command below the error
3291 msg_didout = TRUE;
3292 if (msg_row < cmdline_row)
3293 msg_row = cmdline_row;
3294 redrawcmd();
3295 }
3296 }
3297
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003298 vgetc_busy = save_vgetc_busy;
3299 may_garbage_collect = save_may_garbage_collect;
3300 }
3301 else
3302#endif
3303 map_str = mp->m_str;
3304
3305 /*
3306 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003307 * If 'from' field is the same as the start of the 'to' field, don't
3308 * remap the first character (but do allow abbreviations).
3309 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003310 */
3311 if (map_str == NULL)
3312 i = FAIL;
3313 else
3314 {
3315 int noremap;
3316
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003317#ifdef FEAT_EVAL
3318 last_used_map = mp;
3319 last_used_sid = -1;
3320#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003321 if (save_m_noremap != REMAP_YES)
3322 noremap = save_m_noremap;
3323 else if (
3324#ifdef FEAT_EVAL
zeertzjq9d997ad2024-07-29 21:10:07 +02003325 save_m_expr ?
3326 (save_m_keys != NULL
3327 && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
3328 || (save_alt_m_keys != NULL
3329 && STRNCMP(map_str, save_alt_m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003330 (size_t)save_alt_m_keylen) == 0) :
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003331#endif
zeertzjq9d997ad2024-07-29 21:10:07 +02003332 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
3333 || (mp->m_alt != NULL
3334 && STRNCMP(map_str, mp->m_alt->m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003335 (size_t)mp->m_alt->m_keylen) == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003336 noremap = REMAP_SKIP;
zeertzjq9d997ad2024-07-29 21:10:07 +02003337 else
3338 noremap = REMAP_YES;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003339 i = ins_typebuf(map_str, noremap,
3340 0, TRUE, cmd_silent || save_m_silent);
3341#ifdef FEAT_EVAL
3342 if (save_m_expr)
3343 vim_free(map_str);
3344#endif
3345 }
3346#ifdef FEAT_EVAL
3347 vim_free(save_m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003348 vim_free(save_alt_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003349#endif
3350 *keylenp = keylen;
3351 if (i == FAIL)
3352 return map_result_fail;
3353 return map_result_retry;
3354 }
3355
3356 *keylenp = keylen;
3357 return map_result_nomatch;
3358}
3359
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003360/*
3361 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003362 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003363 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003366vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 old_char = c;
3369 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003370 old_mouse_row = mouse_row;
3371 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003372 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373}
3374
3375/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003376 * When peeking and not getting a character, reg_executing cannot be cleared
3377 * yet, so set a flag to clear it later.
3378 */
3379 static void
3380check_end_reg_executing(int advance)
3381{
3382 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3383 || pending_end_reg_executing))
3384 {
3385 if (advance)
3386 {
3387 reg_executing = 0;
3388 pending_end_reg_executing = FALSE;
3389 }
3390 else
3391 pending_end_reg_executing = TRUE;
3392 }
3393
3394}
3395
3396/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003397 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 * 1. from the stuffbuffer
3399 * This is used for abbreviated commands like "D" -> "d$".
3400 * Also used to redo a command for ".".
3401 * 2. from the typeahead buffer
3402 * Stores text obtained previously but not used yet.
3403 * Also stores the result of mappings.
3404 * Also used for the ":normal" command.
3405 * 3. from the user
3406 * This may do a blocking wait if "advance" is TRUE.
3407 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003408 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003409 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * KeyTyped is set to TRUE in the case the user typed the key.
3411 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003412 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003413 * Just look whether there is a character available.
3414 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 *
3416 * When "no_mapping" is zero, checks for mappings in the current mode.
3417 * Only returns one byte (of a multi-byte character).
3418 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3419 */
3420 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003421vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003423 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003424 int timedout = FALSE; // waited for more than 'timeoutlen'
3425 // for mapping to complete or
3426 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003427 int mapdepth = 0; // check for recursive mapping
3428 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003431 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432#endif
3433 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003435 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 /*
3438 * This function doesn't work very well when called recursively. This may
3439 * happen though, because of:
3440 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3441 * there is a character available, which calls this function. In that
3442 * case we must return NUL, to indicate no character is available.
3443 * 2. A GUI callback function writes to the screen, causing a
3444 * wait_return().
3445 * Using ":normal" can also do this, but it saves the typeahead buffer,
3446 * thus it should be OK. But don't get a key from the user then.
3447 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003448 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 return NUL;
3450
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003451 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003456 typebuf_was_empty = FALSE;
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 init_typebuf();
3460 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003461 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 do
3463 {
3464/*
3465 * get a character: 1. from the stuffbuffer
3466 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003467 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 c = typeahead_char;
3470 if (advance)
3471 typeahead_char = 0;
3472 }
3473 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003474 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (c != NUL && !got_int)
3476 {
3477 if (advance)
3478 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003479 // KeyTyped = FALSE; When the command that stuffed something
3480 // was typed, behave like the stuffed command was typed.
3481 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 KeyStuffed = TRUE;
3483 }
3484 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003485 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 else
3488 {
3489 /*
3490 * Loop until we either find a matching mapped key, or we
3491 * are sure that it is not a mapped key.
3492 * If a mapped key sequence is found we go back to the start to
3493 * try re-mapping.
3494 */
3495 for (;;)
3496 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003497 long wait_time;
3498 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003499 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003500 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 /*
3502 * ui_breakcheck() is slow, don't use it too often when
3503 * inside a mapping. But call it each time for typed
3504 * characters.
3505 */
3506 if (typebuf.tb_maplen)
3507 line_breakcheck();
3508 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003509 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (got_int)
3511 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003512 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003513 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 /*
3516 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003517 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 * Otherwise we behave like having gotten a CTRL-C.
3519 * As a result typing CTRL-C in insert mode will
3520 * really insert a CTRL-C.
3521 */
3522 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003523 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 c = ESC;
3525 else
3526 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003527 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003529 if (advance)
3530 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003531 // Also record this character, it might be needed to
3532 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003533 *typebuf.tb_buf = c;
3534 gotchars(typebuf.tb_buf, 1);
3535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 cmd_silent = FALSE;
3537
3538 break;
3539 }
3540 else if (typebuf.tb_len > 0)
3541 {
3542 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003543 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003545 map_result_T result = handle_mapping(
3546 &keylen, &timedout, &mapdepth);
3547
3548 if (result == map_result_retry)
3549 // try mapping again
3550 continue;
3551 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003552 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003553 // failed, use the outer loop
3554 c = -1;
3555 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003557 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559/*
3560 * get a character: 2. from the typeahead buffer
3561 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003562 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003563 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003564 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003565 cmd_silent = (typebuf.tb_silent > 0);
3566 if (typebuf.tb_maplen > 0)
3567 KeyTyped = FALSE;
3568 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003569 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003570 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003571 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003572 gotchars(typebuf.tb_buf
3573 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003574 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003575 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003576 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003577 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003578 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003581 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
3584/*
3585 * get a character: 3. from the user - handle <Esc> in Insert mode
3586 */
3587 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003588 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003590 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 * typing an <ESC>. If we get something after all, we may
3592 * have to redisplay the mode. That the cursor is in the wrong
3593 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003594 * Do not do this if the kitty keyboard protocol is used, every
3595 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 */
3597 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 new_wcol = curwin->w_wcol;
3599 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if ( advance
3601 && typebuf.tb_len == 1
3602 && typebuf.tb_buf[typebuf.tb_off] == ESC
3603 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003604 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003607 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003608 && (p_timeout
3609 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003611 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003613 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *ptr;
3615
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003616 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 unshowmode(TRUE);
3619 mode_deleted = TRUE;
3620 }
3621#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003622 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003623 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 int save_State;
3626
3627 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003628 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 gui_update_cursor(TRUE, FALSE);
3630 State = save_State;
3631 shape_changed = TRUE;
3632 }
3633#endif
3634 validate_cursor();
3635 old_wcol = curwin->w_wcol;
3636 old_wrow = curwin->w_wrow;
3637
Bram Moolenaar30613902019-12-01 22:11:18 +01003638 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (curwin->w_cursor.col != 0)
3640 {
3641 if (curwin->w_wcol > 0)
3642 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003643 // After auto-indenting and no text is following,
3644 // we are expecting to truncate the trailing
3645 // white-space, so find the last non-white
3646 // character -- webb
3647 if (did_ai && *skipwhite(ml_get_curline()
3648 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003650 chartabsize_T cts;
3651
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003652 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003654 init_chartabsize_arg(&cts, curwin,
3655 curwin->w_cursor.lnum, 0, ptr, ptr);
3656 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003658 if (!VIM_ISWHITE(*cts.cts_ptr))
3659 curwin->w_wcol = cts.cts_vcol;
3660 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003662 cts.cts_ptr +=
3663 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003665 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003667 clear_chartabsize_arg(&cts);
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003670 + curwin->w_wcol / curwin->w_width;
3671 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003673 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 else
3676 {
3677 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681 else if (curwin->w_p_wrap && curwin->w_wrow)
3682 {
3683 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003684 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3688 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003689 // Correct when the cursor is on the right halve
3690 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 ptr = ml_get_curline();
3692 col -= (*mb_head_off)(ptr, ptr + col);
3693 if ((*mb_ptr2cells)(ptr + col) > 1)
3694 --curwin->w_wcol;
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 setcursor();
3698 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 new_wcol = curwin->w_wcol;
3700 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 curwin->w_wcol = old_wcol;
3702 curwin->w_wrow = old_wrow;
3703 }
3704 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003705 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003706
Bram Moolenaar30613902019-12-01 22:11:18 +01003707 // Allow mapping for just typed characters. When we get here c
3708 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003709 for (n = 1; n <= c; ++n)
3710 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 typebuf.tb_len += c;
3712
Bram Moolenaar30613902019-12-01 22:11:18 +01003713 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3715 {
3716 timedout = TRUE;
3717 continue;
3718 }
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (ex_normal_busy > 0)
3721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaar30613902019-12-01 22:11:18 +01003724 // No typeahead left and inside ":normal". Must return
3725 // something to avoid getting stuck. When an incomplete
3726 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (typebuf.tb_len > 0)
3728 {
3729 timedout = TRUE;
3730 continue;
3731 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003732
Bram Moolenaar30613902019-12-01 22:11:18 +01003733 // When 'insertmode' is set, ESC just beeps in Insert
3734 // mode. Use CTRL-L to make edit() return.
3735 // For the command line only CTRL-C always breaks it.
3736 // For the cmdline window: Alternate between ESC and
3737 // CTRL-C: ESC for most situations and CTRL-C to close the
3738 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003739 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003741#ifdef FEAT_TERMINAL
3742 else if (terminal_is_active())
3743 c = K_CANCEL;
3744#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003745 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003746 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 c = Ctrl_C;
3748 else
3749 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003751 // set a flag to indicate this wasn't a normal char
3752 if (advance)
3753 typebuf_was_empty = TRUE;
3754
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003755 // return from main_loop()
3756 if (pending_exmode_active)
3757 exmode_active = EXMODE_NORMAL;
3758
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003759 // no chars to block abbreviation for
3760 typebuf.tb_no_abbr_cnt = 0;
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 break;
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765/*
3766 * get a character: 3. from the user - update display
3767 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003768 // In insert mode a screen update is skipped when characters
3769 // are still available. But when those available characters
3770 // are part of a mapping, and we are going to do a blocking
3771 // wait here. Need to update the screen to display the
3772 // changed text so far. Also for when 'lazyredraw' is set and
3773 // redrawing was postponed because there was something in the
3774 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003775 if (((State & MODE_INSERT) != 0 || p_lz)
3776 && (State & MODE_CMDLINE) == 0
3777 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
3779 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003780 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782
3783 /*
3784 * If we have a partial match (and are going to wait for more
3785 * input from the user), show the partially matched characters
3786 * to the user with showcmd.
3787 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003788 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003789 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (typebuf.tb_len > 0 && advance && !exmode_active)
3791 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003792 if (((State & (MODE_NORMAL | MODE_INSERT))
3793 || State == MODE_LANGMAP)
3794 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003796 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003797 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3799 + typebuf.tb_len - 1) == 1)
3800 {
3801 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3802 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003803 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003804 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003806 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 old_wcol = curwin->w_wcol;
3808 old_wrow = curwin->w_wrow;
3809 curwin->w_wcol = new_wcol;
3810 curwin->w_wrow = new_wrow;
3811 push_showcmd();
3812 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003813 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3814 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003815 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003816 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 curwin->w_wcol = old_wcol;
3818 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003821 // This looks nice when typing a dead character map.
3822 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003823 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003824 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3826 && cmdline_star == 0
3827#endif
3828 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3829 + typebuf.tb_len - 1) == 1)
3830 {
3831 putcmdline(typebuf.tb_buf[typebuf.tb_off
3832 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003833 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 }
3836
3837/*
3838 * get a character: 3. from the user - get it
3839 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003840 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003841 // timedout may have been set if a mapping with empty RHS
3842 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003843 timedout = FALSE;
3844
Bram Moolenaar652de232019-04-04 20:13:09 +02003845 if (advance)
3846 {
3847 if (typebuf.tb_len == 0
3848 || !(p_timeout
3849 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3850 // blocking wait
3851 wait_time = -1L;
3852 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3853 wait_time = p_ttm;
3854 else
3855 wait_time = p_tm;
3856 }
3857 else
3858 wait_time = 0;
3859
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003860 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3862 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003863 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaareda35f72019-08-03 14:59:44 +02003865 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003867 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003869 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003871 if ((State & MODE_CMDLINE)
3872 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003874 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003875 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877
3878 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003879 continue; // end of input script reached
3880 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
3882 if (!advance)
3883 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003884 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
3886 timedout = TRUE;
3887 continue;
3888 }
3889 }
3890 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003891 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 while (typebuf.tb_buf[typebuf.tb_off
3893 + typebuf.tb_len] != NUL)
3894 typebuf.tb_noremap[typebuf.tb_off
3895 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003896#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003897 // Get IM status right after getting keys, not after the
3898 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 vgetc_im_active = im_get_status();
3900#endif
3901 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003902 } // for (;;)
3903 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaar30613902019-12-01 22:11:18 +01003905 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003906 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 /*
3909 * The "INSERT" message is taken care of here:
3910 * if we return an ESC to exit insert mode, the message is deleted
3911 * if we don't return an ESC but deleted the message before, redisplay it
3912 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003913 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003915 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
3917 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003918 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 unshowmode(FALSE);
3921 }
3922 else if (c != ESC && mode_deleted)
3923 {
3924 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003925 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 else
3927 showmode();
3928 }
3929 }
3930#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003931 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003932 if (gui.in_use && shape_changed)
3933 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003935 if (timedout && c == ESC)
3936 {
zeertzjqbf321802024-01-28 19:03:00 +01003937 // When recording there will be no timeout. Add an <Ignore> after the
3938 // ESC to avoid that it forms a key code with following characters.
3939 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003942 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 return c;
3945}
3946
3947/*
3948 * inchar() - get one character from
3949 * 1. a scriptfile
3950 * 2. the keyboard
3951 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003952 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 * NUL terminated (buffer length must be 'maxlen' + 1).
3954 * Minimum for "maxlen" is 3!!!!
3955 *
3956 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3957 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3958 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3959 * otherwise.
3960 *
3961 * If we got an interrupt all input is read until none is available.
3962 *
3963 * If wait_time == 0 there is no waiting for the char.
3964 * If wait_time == n we wait for n msec for a character to arrive.
3965 * If wait_time == -1 we wait forever for a character to arrive.
3966 *
3967 * Return the number of obtained characters.
3968 * Return -1 when end of input script reached.
3969 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003970 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003971inchar(
3972 char_u *buf,
3973 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003974 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
Bram Moolenaar30613902019-12-01 22:11:18 +01003976 int len = 0; // init for GCC
3977 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003979 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
Bram Moolenaar30613902019-12-01 22:11:18 +01003981 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003984 out_flush_cursor(FALSE, FALSE);
3985#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3986 if (gui.in_use && postponed_mouseshape)
3987 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988#endif
3989 }
3990
3991 /*
3992 * Don't reset these when at the hit-return prompt, otherwise a endless
3993 * recursive loop may result (write error in swapfile, hit-return, timeout
3994 * on char wait, flush swapfile, write error....).
3995 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003996 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003998 did_outofmem_msg = FALSE; // display out of memory message (again)
3999 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004001 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004004 * Get a character from a script file if there is one.
4005 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 */
4007 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004008 while (scriptin[curscript] != NULL && script_char < 0
4009#ifdef FEAT_EVAL
4010 && !ignore_script
4011#endif
4012 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02004014#ifdef MESSAGE_QUEUE
4015 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00004016#endif
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
4019 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004020 // Reached EOF.
4021 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
4022 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 closescript();
4024 /*
4025 * When reading script file is interrupted, return an ESC to get
4026 * back to normal mode.
4027 * Otherwise return -1, because typebuf.tb_buf[] has changed.
4028 */
4029 if (got_int)
4030 retesc = TRUE;
4031 else
4032 return -1;
4033 }
4034 else
4035 {
4036 buf[0] = script_char;
4037 len = 1;
4038 }
4039 }
4040
Bram Moolenaar30613902019-12-01 22:11:18 +01004041 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 /*
4044 * If we got an interrupt, skip all previously typed characters and
4045 * return TRUE if quit reading script file.
4046 * Stop reading typeahead when a single CTRL-C was read,
4047 * fill_input_buf() returns this when not able to read from stdin.
4048 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
4049 * and buf may be pointing inside typebuf.tb_buf[].
4050 */
4051 if (got_int)
4052 {
kylo252ae6f1d82022-02-16 19:24:07 +00004053#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u dum[DUM_LEN + 1];
4055
4056 for (;;)
4057 {
4058 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01004059 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 break;
4061 }
4062 return retesc;
4063 }
4064
4065 /*
4066 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004067 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004069 if (wait_time == -1L || wait_time > 10L)
4070 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /*
4073 * Fill up to a third of the buffer, because each character may be
4074 * tripled below.
4075 */
4076 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
4077 }
4078
Bram Moolenaar30613902019-12-01 22:11:18 +01004079 // If the typebuf was changed further down, it is like nothing was added by
4080 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (typebuf_changed(tb_change_cnt))
4082 return 0;
4083
Bram Moolenaar30613902019-12-01 22:11:18 +01004084 // Note the change in the typeahead buffer, this matters for when
4085 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4086 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004087 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4088 typebuf.tb_change_cnt = 1;
4089
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004090 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091}
4092
4093/*
4094 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004095 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 * Returns the new length.
4097 */
4098 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004099fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100{
4101 int i;
4102 char_u *p = buf;
4103
4104 /*
4105 * Two characters are special: NUL and K_SPECIAL.
4106 * When compiled With the GUI CSI is also special.
4107 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4108 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4109 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 */
4111 for (i = len; --i >= 0; ++p)
4112 {
4113#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004114 // When the GUI is used any character can come after a CSI, don't
4115 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (gui.in_use && p[0] == CSI && i >= 2)
4117 {
4118 p += 2;
4119 i -= 2;
4120 }
zeertzjq646bb722022-02-16 17:51:47 +00004121# ifndef MSWIN
4122 // When not on MS-Windows and the GUI is not used CSI needs to be
4123 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 else if (!gui.in_use && p[0] == CSI)
4125 {
4126 mch_memmove(p + 3, p + 1, (size_t)i);
4127 *p++ = K_SPECIAL;
4128 *p++ = KS_EXTRA;
4129 *p = (int)KE_CSI;
4130 len += 2;
4131 }
zeertzjq646bb722022-02-16 17:51:47 +00004132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 else
4134#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004135 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004136 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004137 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004138#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004139 // Win32 console passes modifiers
4140 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004141# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004142 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004143# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004144 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#endif
4146 ))
4147 {
4148 mch_memmove(p + 3, p + 1, (size_t)i);
4149 p[2] = K_THIRD(p[0]);
4150 p[1] = K_SECOND(p[0]);
4151 p[0] = K_SPECIAL;
4152 p += 2;
4153 len += 2;
4154 }
4155 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004156 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return len;
4158}
4159
4160#if defined(USE_INPUT_BUF) || defined(PROTO)
4161/*
4162 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4163 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004164 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004165 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
4167 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004168input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004171# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4172 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173# endif
4174 );
4175}
4176#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004177
4178/*
4179 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4180 * typeahead.
4181 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004182 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004183getcmdkeycmd(
4184 int promptc UNUSED,
4185 void *cookie UNUSED,
4186 int indent UNUSED,
4187 getline_opt_T do_concat UNUSED)
4188{
4189 garray_T line_ga;
4190 int c1 = -1;
4191 int c2;
4192 int cmod = 0;
4193 int aborted = FALSE;
4194
4195 ga_init2(&line_ga, 1, 32);
4196
4197 // no mapping for these characters
4198 no_mapping++;
4199
4200 got_int = FALSE;
4201 while (c1 != NUL && !aborted)
4202 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004203 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004204 {
4205 aborted = TRUE;
4206 break;
4207 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004208
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004209 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004210 {
4211 // incomplete <Cmd> is an error, because there is not much the user
4212 // could do in this state.
4213 emsg(_(e_cmd_mapping_must_end_with_cr));
4214 aborted = TRUE;
4215 break;
4216 }
4217
4218 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004219 c1 = vgetorpeek(TRUE);
4220
Bram Moolenaar957cf672020-11-12 14:21:06 +01004221 // Get two extra bytes for special keys
4222 if (c1 == K_SPECIAL)
4223 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004224 c1 = vgetorpeek(TRUE);
4225 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004226 if (c1 == KS_MODIFIER)
4227 {
4228 cmod = c2;
4229 continue;
4230 }
4231 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004232
4233 // K_ESC is used to avoid ambiguity with the single Esc character
4234 // that might be the start of an escape sequence. Convert it back
4235 // to a single Esc here.
4236 if (c1 == K_ESC)
4237 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004238 }
4239
4240 if (got_int)
4241 aborted = TRUE;
4242 else if (c1 == '\r' || c1 == '\n')
4243 c1 = NUL; // end the line
4244 else if (c1 == ESC)
4245 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004246 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004247 {
4248 // give a nicer error message for this special case
4249 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4250 aborted = TRUE;
4251 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004252 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004253 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004254 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004255 }
4256 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004257 {
4258 if (cmod != 0)
4259 {
4260 ga_append(&line_ga, K_SPECIAL);
4261 ga_append(&line_ga, KS_MODIFIER);
4262 ga_append(&line_ga, cmod);
4263 }
4264 if (IS_SPECIAL(c1))
4265 {
4266 ga_append(&line_ga, K_SPECIAL);
4267 ga_append(&line_ga, K_SECOND(c1));
4268 ga_append(&line_ga, K_THIRD(c1));
4269 }
4270 else
4271 ga_append(&line_ga, c1);
4272 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004273
4274 cmod = 0;
4275 }
4276
4277 no_mapping--;
4278
4279 if (aborted)
4280 ga_clear(&line_ga);
4281
4282 return (char_u *)line_ga.ga_data;
4283}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004284
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004285#if defined(FEAT_EVAL) || defined(PROTO)
4286/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004287 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4288 * is set when redo'ing.
4289 * Put this SID in the redo buffer, so that "." will use the same script
4290 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004291 */
4292 void
4293may_add_last_used_map_to_redobuff(void)
4294{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004295 char_u buf[3 + 20];
John Marriotte7a1bbf2024-11-11 20:40:33 +01004296 int buflen;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004297 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004298
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004299 if (last_used_map != NULL)
4300 sid = last_used_map->m_script_ctx.sc_sid;
4301 if (sid < 0)
4302 sid = last_used_sid;
4303
4304 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004305 return;
4306
4307 // <K_SID>{nr};
4308 buf[0] = K_SPECIAL;
4309 buf[1] = KS_EXTRA;
4310 buf[2] = KE_SID;
John Marriotte7a1bbf2024-11-11 20:40:33 +01004311 buflen = 3;
4312
4313 buflen += vim_snprintf((char *)buf + 3, 20, "%d;", sid);
4314 add_buff(&redobuff, buf, (long)buflen);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004315}
4316#endif
4317
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004318 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004319do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004320{
4321 int res;
4322#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004323 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004324
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004325 if (key == K_SCRIPT_COMMAND
4326 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004327 {
4328 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004329 if (last_used_map != NULL)
4330 current_sctx = last_used_map->m_script_ctx;
4331 else
4332 {
4333 current_sctx.sc_sid = last_used_sid;
4334 current_sctx.sc_lnum = 0;
4335 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4336 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004337 }
4338#endif
4339
4340 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4341
4342#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004343 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004344 current_sctx = save_current_sctx;
4345#endif
4346
4347 return res;
4348}
4349
4350#if defined(FEAT_EVAL) || defined(PROTO)
4351 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004352reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004353{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004354 if (last_used_map != mp)
4355 return;
4356
4357 last_used_map = NULL;
4358 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004359}
4360#endif