blob: 0d1bd8a2816b272ccbcbd1ea00d43e6c3575c6f7 [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;
John Marriott18bacc82025-02-26 19:14:06 +0100171 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
John Marriott18bacc82025-02-26 19:14:06 +0100173 p = get_buffcont(&recordbuff, TRUE, &len);
John Marriottd3c4b7e2025-02-25 20:56:38 +0100174 if (p == NULL)
175 return NULL;
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 free_buff(&recordbuff);
178
179 /*
180 * Remove the characters that were added the last time, these must be the
181 * (possibly mapped) characters that stopped the recording.
182 */
John Marriott18bacc82025-02-26 19:14:06 +0100183 if (len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 {
John Marriott18bacc82025-02-26 19:14:06 +0100185 len -= last_recorded_len;
186 p[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 }
188
189 /*
190 * When stopping recording from Insert mode with CTRL-O q, also remove the
191 * CTRL-O.
192 */
John Marriott18bacc82025-02-26 19:14:06 +0100193 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
194 p[len - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
196 return (p);
197}
198
199/*
200 * Return the contents of the redo buffer as a single string.
201 * K_SPECIAL and CSI in the returned string are escaped.
202 */
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100203 string_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100204get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100206 size_t len = 0;
zeertzjq61b35442025-03-17 21:02:50 +0100207 char_u *str = get_buffcont(&redobuff, FALSE, &len);
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100208 string_T ret = { str, len };
209 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210}
211
212/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000213 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 * K_SPECIAL and CSI should have been escaped already.
215 */
216 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100217add_buff(
218 buffheader_T *buf,
219 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100220 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 if (slen < 0)
223 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100224 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 return;
226
Bram Moolenaar30613902019-12-01 22:11:18 +0100227 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200229 buf->bh_curr = &(buf->bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100230 buf->bh_create_newblock = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100232 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100234 iemsg(e_add_to_internal_buffer_that_was_already_read_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 return;
236 }
237 else if (buf->bh_index != 0)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100238 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200239 mch_memmove(buf->bh_first.b_next->b_str,
240 buf->bh_first.b_next->b_str + buf->bh_index,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100241 (buf->bh_first.b_next->b_strlen - buf->bh_index) + 1);
242 buf->bh_first.b_next->b_strlen -= buf->bh_index;
243 buf->bh_space += buf->bh_index;
244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 buf->bh_index = 0;
246
John Marriotte7a1bbf2024-11-11 20:40:33 +0100247 if (!buf->bh_create_newblock && buf->bh_space >= (int)slen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100249 vim_strncpy(buf->bh_curr->b_str + buf->bh_curr->b_strlen, s, (size_t)slen);
250 buf->bh_curr->b_strlen += slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 buf->bh_space -= slen;
252 }
253 else
254 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100255 long_u len;
256 buffblock_T *p;
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 if (slen < MINIMAL_SIZE)
259 len = MINIMAL_SIZE;
260 else
261 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200262 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100264 return; // no space, just forget it
Bram Moolenaarb6356332005-07-18 21:40:44 +0000265 vim_strncpy(p->b_str, s, (size_t)slen);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100266 p->b_strlen = slen;
267 buf->bh_space = (int)(len - slen);
268 buf->bh_create_newblock = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
Bram Moolenaar285e3352018-04-18 23:01:13 +0200270 p->b_next = buf->bh_curr->b_next;
271 buf->bh_curr->b_next = p;
272 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274}
275
276/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000277 * Delete "slen" bytes from the end of "buf".
278 * Only works when it was just added.
279 */
280 static void
281delete_buff_tail(buffheader_T *buf, int slen)
282{
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000283 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000284 return; // nothing to delete
John Marriotte7a1bbf2024-11-11 20:40:33 +0100285 if (buf->bh_curr->b_strlen < (size_t)slen)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000286 return;
287
John Marriotte7a1bbf2024-11-11 20:40:33 +0100288 buf->bh_curr->b_str[buf->bh_curr->b_strlen - (size_t)slen] = NUL;
289 buf->bh_curr->b_strlen -= slen;
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000290 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000291}
292
293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 * Add number "n" to buffer "buf".
295 */
296 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100297add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298{
299 char_u number[32];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100300 int numberlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
John Marriotte7a1bbf2024-11-11 20:40:33 +0100302 numberlen = vim_snprintf((char *)number, sizeof(number), "%ld", n);
303 add_buff(buf, number, (long)numberlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304}
305
306/*
307 * Add character 'c' to buffer "buf".
308 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
309 */
310 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100311add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 char_u bytes[MB_MAXBYTES + 1];
314 int len;
315 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 char_u temp[4];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100317 long templen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 if (IS_SPECIAL(c))
320 len = 1;
321 else
322 len = (*mb_char2bytes)(c, bytes);
323 for (i = 0; i < len; ++i)
324 {
325 if (!IS_SPECIAL(c))
326 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
328 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
329 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100330 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 temp[0] = K_SPECIAL;
332 temp[1] = K_SECOND(c);
333 temp[2] = K_THIRD(c);
334 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100335 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
337#ifdef FEAT_GUI
338 else if (c == CSI)
339 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100340 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 temp[0] = CSI;
342 temp[1] = KS_EXTRA;
343 temp[2] = (int)KE_CSI;
344 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100345 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 }
347#endif
348 else
349 {
350 temp[0] = c;
351 temp[1] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100352 templen = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100354 add_buff(buf, temp, templen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356}
357
Bram Moolenaar30613902019-12-01 22:11:18 +0100358// First read ahead buffer. Used for translated commands.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100359static buffheader_T readbuf1 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100360
Bram Moolenaar30613902019-12-01 22:11:18 +0100361// Second read ahead buffer. Used for redo.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100362static buffheader_T readbuf2 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100365 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
366 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 * If advance == TRUE go to the next char.
368 * No translation is done K_SPECIAL and CSI are escaped.
369 */
370 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100371read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100373 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100375 c = read_readbuf(&readbuf1, advance);
376 if (c == NUL)
377 c = read_readbuf(&readbuf2, advance);
378 return c;
379}
380
381 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100382read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100383{
384 char_u c;
385 buffblock_T *curr;
386
Bram Moolenaar30613902019-12-01 22:11:18 +0100387 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 return NUL;
389
Bram Moolenaar285e3352018-04-18 23:01:13 +0200390 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100391 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
393 if (advance)
394 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100395 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200397 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100399 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
401 }
402 return c;
403}
404
405/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100406 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 */
408 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100409start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200411 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200413 readbuf1.bh_curr = &(readbuf1.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100414 readbuf1.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100415 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200416 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100417 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200418 readbuf2.bh_curr = &(readbuf2.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100419 readbuf2.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421}
422
423/*
424 * Return TRUE if the stuff buffer is empty.
425 */
426 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100427stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200429 return (readbuf1.bh_first.b_next == NULL
430 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100431}
432
Bram Moolenaar113e1072019-01-20 15:30:40 +0100433#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100434/*
435 * Return TRUE if readbuf1 is empty. There may still be redo characters in
436 * redbuf2.
437 */
438 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100439readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100440{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200441 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445/*
446 * Set a typeahead character that won't be flushed.
447 */
448 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100449typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450{
451 typeahead_char = c;
452}
453
454/*
455 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100456 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 * flush all typeahead characters (used when interrupted by a CTRL-C).
458 */
459 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200460flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461{
462 init_typebuf();
463
464 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100465 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 ;
467
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200468 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Christian Brabandt322ba912024-08-25 21:33:03 +0200470 // remove mapped characters at the start only,
471 // but only when enough space left in typebuf
472 if (typebuf.tb_off + typebuf.tb_maplen >= typebuf.tb_buflen)
473 {
474 typebuf.tb_off = MAXMAPLEN;
475 typebuf.tb_len = 0;
476 }
477 else
478 {
479 typebuf.tb_off += typebuf.tb_maplen;
480 typebuf.tb_len -= typebuf.tb_maplen;
481 }
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100482#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
483 if (typebuf.tb_len == 0)
484 typebuf_was_filled = FALSE;
485#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200486 }
487 else
488 {
489 // remove typeahead
490 if (flush_typeahead == FLUSH_INPUT)
491 // We have to get all characters, because we may delete the first
492 // part of an escape sequence. In an xterm we get one char at a
493 // time and we have to get them all.
494 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
495 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 typebuf.tb_off = MAXMAPLEN;
497 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200498#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100499 // Reset the flag that text received from a client or from feedkeys()
500 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200501 typebuf_was_filled = FALSE;
502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 typebuf.tb_maplen = 0;
505 typebuf.tb_silent = 0;
506 cmd_silent = FALSE;
507 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200508 if (++typebuf.tb_change_cnt == 0)
509 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510}
511
512/*
513 * The previous contents of the redo buffer is kept in old_redobuffer.
514 * This is used for the CTRL-O <.> command in insert mode.
515 */
516 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100517ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000519 if (block_redo)
520 return;
521
522 free_buff(&old_redobuff);
523 old_redobuff = redobuff;
524 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525}
526
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100527/*
528 * Discard the contents of the redo buffer and restore the previous redo
529 * buffer.
530 */
531 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100532CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100533{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000534 if (block_redo)
535 return;
536
537 free_buff(&redobuff);
538 redobuff = old_redobuff;
539 old_redobuff.bh_first.b_next = NULL;
540 start_stuff();
541 while (read_readbuffers(TRUE) != NUL)
542 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100543}
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545/*
546 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
547 * Used before executing autocommands and user functions.
548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200550saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
552 char_u *s;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100553 size_t slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200555 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200556 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200557 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200558 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar30613902019-12-01 22:11:18 +0100560 // Make a copy, so that ":normal ." in a function works.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100561 s = get_buffcont(&save_redo->sr_redobuff, FALSE, &slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000562 if (s == NULL)
563 return;
564
John Marriotte7a1bbf2024-11-11 20:40:33 +0100565 add_buff(&redobuff, s, (long)slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000566 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567}
568
569/*
570 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
571 * Used after executing autocommands and user functions.
572 */
573 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200574restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200576 free_buff(&redobuff);
577 redobuff = save_redo->sr_redobuff;
578 free_buff(&old_redobuff);
579 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582/*
583 * Append "s" to the redo buffer.
584 * K_SPECIAL and CSI should already have been escaped.
585 */
586 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100587AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 if (!block_redo)
590 add_buff(&redobuff, s, -1L);
591}
592
593/*
594 * Append to Redo buffer literally, escaping special characters with CTRL-V.
595 * K_SPECIAL and CSI are escaped as well.
596 */
597 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100598AppendToRedobuffLit(
599 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100600 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000602 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 int c;
604 char_u *start;
605
606 if (block_redo)
607 return;
608
Bram Moolenaarebefac62005-12-28 22:39:57 +0000609 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100611 // Put a string of normal characters in the redo buffer (that's
612 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000614 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 ++s;
616
Bram Moolenaar30613902019-12-01 22:11:18 +0100617 // Don't put '0' or '^' as last character, just in case a CTRL-D is
618 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
620 --s;
621 if (s > start)
622 add_buff(&redobuff, start, (long)(s - start));
623
Bram Moolenaarebefac62005-12-28 22:39:57 +0000624 if (*s == NUL || (len >= 0 && s - str >= len))
625 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaar30613902019-12-01 22:11:18 +0100627 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000628 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100629 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000630 c = mb_cptr2char_adv(&s);
631 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000632 c = *s++;
633 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
634 add_char_buff(&redobuff, Ctrl_V);
635
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000636 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000637 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000638 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000639 else
640 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
642}
643
644/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100645 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
646 * and escaping other K_SPECIAL and CSI bytes.
647 */
648 void
649AppendToRedobuffSpec(char_u *s)
650{
zeertzjq30b6d612023-05-07 17:39:23 +0100651 if (block_redo)
652 return;
653
zeertzjq3ab3a862023-05-06 16:22:04 +0100654 while (*s != NUL)
655 {
656 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
657 {
zeertzjq30b6d612023-05-07 17:39:23 +0100658 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100659 add_buff(&redobuff, s, 3L);
660 s += 3;
661 }
662 else
663 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
664 }
665}
666
667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 * Append a character to the redo buffer.
669 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
670 */
671 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100672AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 if (!block_redo)
675 add_char_buff(&redobuff, c);
676}
677
678/*
679 * Append a number to the redo buffer.
680 */
681 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100682AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 if (!block_redo)
685 add_num_buff(&redobuff, n);
686}
687
688/*
689 * Append string "s" to the stuff buffer.
690 * CSI and K_SPECIAL must already have been escaped.
691 */
692 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100693stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100695 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200698/*
699 * Append string "s" to the redo stuff buffer.
700 * CSI and K_SPECIAL must already have been escaped.
701 */
702 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100703stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200704{
705 add_buff(&readbuf2, s, -1L);
706}
707
John Marriott34954972025-03-16 20:49:52 +0100708 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100709stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100711 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712}
713
714#if defined(FEAT_EVAL) || defined(PROTO)
715/*
716 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
717 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200718 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
720 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100721stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200723 int c;
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 while (*s != NUL)
726 {
727 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
728 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100729 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 stuffReadbuffLen(s, 3L);
731 s += 3;
732 }
733 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200734 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100735 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200736 if (c == CAR || c == NL || c == ESC)
737 c = ' ';
738 stuffcharReadbuff(c);
739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741}
742#endif
743
744/*
745 * Append a character to the stuff buffer.
746 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
747 */
748 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100749stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753
754/*
755 * Append a number to the stuff buffer.
756 */
757 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100758stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100760 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
763/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200764 * Stuff a string into the typeahead buffer, such that edit() will insert it
765 * literally ("literally" TRUE) or interpret is as typed characters.
766 */
767 void
768stuffescaped(char_u *arg, int literally)
769{
770 int c;
771 char_u *start;
772
773 while (*arg != NUL)
774 {
775 // Stuff a sequence of normal ASCII characters, that's fast. Also
776 // stuff K_SPECIAL to get the effect of a special key when "literally"
777 // is TRUE.
778 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000779 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200780 || (*arg == K_SPECIAL && !literally))
781 ++arg;
782 if (arg > start)
783 stuffReadbuffLen(start, (long)(arg - start));
784
785 // stuff a single special character
786 if (*arg != NUL)
787 {
788 if (has_mbyte)
789 c = mb_cptr2char_adv(&arg);
790 else
791 c = *arg++;
792 if (literally && ((c < ' ' && c != TAB) || c == DEL))
793 stuffcharReadbuff(Ctrl_V);
794 stuffcharReadbuff(c);
795 }
796 }
797}
798
799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
801 * multibyte characters.
802 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100803 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
804 * otherwise.
805 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 */
807 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100808read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100810 static buffblock_T *bp;
811 static char_u *p;
812 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100813 int n;
814 char_u buf[MB_MAXBYTES + 1];
815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 if (init)
818 {
819 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200820 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200822 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (bp == NULL)
824 return FAIL;
825 p = bp->b_str;
826 return OK;
827 }
828 if ((c = *p) != NUL)
829 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100830 // Reverse the conversion done by add_char_buff()
831 // For a multi-byte character get all the bytes and return the
832 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
834 n = MB_BYTE2LEN_CHECK(c);
835 else
836 n = 1;
837 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100839 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 c = TO_SPECIAL(p[1], p[2]);
842 p += 2;
843 }
844#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100845 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 p += 2;
847#endif
848 if (*++p == NUL && bp->b_next != NULL)
849 {
850 bp = bp->b_next;
851 p = bp->b_str;
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100854 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
856 if (n != 1)
857 c = (*mb_ptr2char)(buf);
858 break;
859 }
860 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100861 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 }
865
866 return c;
867}
868
869/*
870 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
871 * If old_redo is TRUE, use old_redobuff instead of redobuff.
872 * The escaped K_SPECIAL and CSI are copied without translation.
873 */
874 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100875copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
877 int c;
878
879 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881}
882
883/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100884 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * Insert the redo count into the command.
886 * If "old_redo" is TRUE, the last but one command is repeated
887 * instead of the last command (inserting text). This is used for
888 * CTRL-O <.> in insert mode
889 *
890 * return FAIL for failure, OK otherwise
891 */
892 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100893start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 int c;
896
Bram Moolenaar30613902019-12-01 22:11:18 +0100897 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (read_redo(TRUE, old_redo) == FAIL)
899 return FAIL;
900
901 c = read_redo(FALSE, old_redo);
902
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100903#ifdef FEAT_EVAL
904 if (c == K_SID)
905 {
906 // Copy the <SID>{sid}; sequence
907 add_char_buff(&readbuf2, c);
908 for (;;)
909 {
910 c = read_redo(FALSE, old_redo);
911 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100912 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100913 break;
914 }
915 c = read_redo(FALSE, old_redo);
916 }
917#endif
918
Bram Moolenaar30613902019-12-01 22:11:18 +0100919 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (c == '"')
921 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100922 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 c = read_redo(FALSE, old_redo);
924
Bram Moolenaar30613902019-12-01 22:11:18 +0100925 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (c >= '1' && c < '9')
927 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100928 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200929
Bram Moolenaar30613902019-12-01 22:11:18 +0100930 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200931 if (c == '=')
932 {
933 add_char_buff(&readbuf2, CAR);
934 cmd_silent = TRUE;
935 }
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 c = read_redo(FALSE, old_redo);
938 }
939
Bram Moolenaar30613902019-12-01 22:11:18 +0100940 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 VIsual = curwin->w_cursor;
943 VIsual_active = TRUE;
944 VIsual_select = FALSE;
945 VIsual_reselect = TRUE;
946 redo_VIsual_busy = TRUE;
947 c = read_redo(FALSE, old_redo);
948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
Bram Moolenaar30613902019-12-01 22:11:18 +0100950 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (count)
952 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100953 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100955 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100958 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100959 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 copy_redo(old_redo);
961 return OK;
962}
963
964/*
965 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100966 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 * return FAIL for failure, OK otherwise
968 */
969 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100970start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
972 int c;
973
974 if (read_redo(TRUE, FALSE) == FAIL)
975 return FAIL;
976 start_stuff();
977
Bram Moolenaar30613902019-12-01 22:11:18 +0100978 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 while ((c = read_redo(FALSE, FALSE)) != NUL)
980 {
981 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
982 {
983 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100984 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 break;
986 }
987 }
988
Bram Moolenaar30613902019-12-01 22:11:18 +0100989 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 copy_redo(FALSE);
991 block_redo = TRUE;
992 return OK;
993}
994
995 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100996stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 block_redo = FALSE;
999}
1000
1001/*
1002 * Initialize typebuf.tb_buf to point to typebuf_init.
1003 * alloc() cannot be used here: In out-of-memory situations it would
1004 * be impossible to type anything.
1005 */
1006 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001007init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001009 if (typebuf.tb_buf != NULL)
1010 return;
1011
1012 typebuf.tb_buf = typebuf_init;
1013 typebuf.tb_noremap = noremapbuf_init;
1014 typebuf.tb_buflen = TYPELEN_INIT;
1015 typebuf.tb_len = 0;
1016 typebuf.tb_off = MAXMAPLEN + 4;
1017 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001021 * Returns TRUE when keys cannot be remapped.
1022 */
1023 int
1024noremap_keys(void)
1025{
1026 return KeyNoremap & (RM_NONE|RM_SCRIPT);
1027}
1028
1029/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001030 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
1031 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001033 * If "noremap" is REMAP_YES, new string can be mapped again.
1034 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
1035 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001036 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001037 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001039 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001041 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1042 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001044 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 *
1046 * return FAIL for failure, OK otherwise
1047 */
1048 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001049ins_typebuf(
1050 char_u *str,
1051 int noremap,
1052 int offset,
1053 int nottyped,
1054 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 char_u *s1, *s2;
1057 int newlen;
1058 int addlen;
1059 int i;
1060 int newoff;
1061 int val;
1062 int nrm;
1063
1064 init_typebuf();
1065 if (++typebuf.tb_change_cnt == 0)
1066 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001067 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (offset == 0 && addlen <= typebuf.tb_off)
1072 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001073 /*
1074 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 typebuf.tb_off -= addlen;
1077 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1078 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001079 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1080 >= addlen + 3 * (MAXMAPLEN + 4))
1081 {
1082 /*
1083 * Buffer is empty and string fits in the existing buffer.
1084 * Leave some space before and after, if possible.
1085 */
1086 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1087 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 else
1090 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001091 int extra;
1092
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001093 /*
1094 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001095 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001096 * characters. We add some extra room to avoid having to allocate too
1097 * often.
1098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001100 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1101 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001103 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001104 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 setcursor();
1106 return FAIL;
1107 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001108 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001110 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 return FAIL;
1112 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001113 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 vim_free(s1);
1116 return FAIL;
1117 }
1118 typebuf.tb_buflen = newlen;
1119
Bram Moolenaar30613902019-12-01 22:11:18 +01001120 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1122 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001123 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001125 // copy the old chars, after the insertion point, including the NUL at
1126 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 mch_memmove(s1 + newoff + offset + addlen,
1128 typebuf.tb_buf + typebuf.tb_off + offset,
1129 (size_t)(typebuf.tb_len - offset + 1));
1130 if (typebuf.tb_buf != typebuf_init)
1131 vim_free(typebuf.tb_buf);
1132 typebuf.tb_buf = s1;
1133
1134 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1135 (size_t)offset);
1136 mch_memmove(s2 + newoff + offset + addlen,
1137 typebuf.tb_noremap + typebuf.tb_off + offset,
1138 (size_t)(typebuf.tb_len - offset));
1139 if (typebuf.tb_noremap != noremapbuf_init)
1140 vim_free(typebuf.tb_noremap);
1141 typebuf.tb_noremap = s2;
1142
1143 typebuf.tb_off = newoff;
1144 }
1145 typebuf.tb_len += addlen;
1146
Bram Moolenaar30613902019-12-01 22:11:18 +01001147 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (noremap == REMAP_SCRIPT)
1149 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001150 else if (noremap == REMAP_SKIP)
1151 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 val = RM_NONE;
1154
1155 /*
1156 * Adjust typebuf.tb_noremap[] for the new characters:
1157 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1158 * (sometimes) not remappable
1159 * If noremap == REMAP_YES: all the new characters are mappable
1160 * If noremap > 0: "noremap" characters are not remappable, the rest
1161 * mappable
1162 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001163 if (noremap == REMAP_SKIP)
1164 nrm = 1;
1165 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 nrm = addlen;
1167 else
1168 nrm = noremap;
1169 for (i = 0; i < addlen; ++i)
1170 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1171 (--nrm >= 0) ? val : RM_YES;
1172
Bram Moolenaar30613902019-12-01 22:11:18 +01001173 // tb_maplen and tb_silent only remember the length of mapped and/or
1174 // silent mappings at the start of the buffer, assuming that a mapped
1175 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (nottyped || typebuf.tb_maplen > offset)
1177 typebuf.tb_maplen += addlen;
1178 if (silent || typebuf.tb_silent > offset)
1179 {
1180 typebuf.tb_silent += addlen;
1181 cmd_silent = TRUE;
1182 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001183 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 typebuf.tb_no_abbr_cnt += addlen;
1185
1186 return OK;
1187}
1188
1189/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001190 * Put character "c" back into the typeahead buffer.
1191 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001192 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1193 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001194 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001195 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001196 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001197ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001198{
zeertzjq6cac7702022-01-04 18:01:21 +00001199 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001200 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001201
zeertzjq2e7cba32022-06-10 15:30:32 +01001202 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001203 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001204 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001205}
1206
1207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001209 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1212 * changed it was reallocated and the old pointer can no longer be used.
1213 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1214 * that was just added.
1215 */
1216 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001217typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001218 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
1220 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001221#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1222 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#endif
1224 ));
1225}
1226
1227/*
1228 * Return TRUE if there are no characters in the typeahead buffer that have
1229 * not been typed (result from a mapping or come from ":normal").
1230 */
1231 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001232typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
1234 return typebuf.tb_maplen == 0;
1235}
1236
1237/*
1238 * Return the number of characters that are mapped (or not typed).
1239 */
1240 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001241typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 return typebuf.tb_maplen;
1244}
1245
1246/*
1247 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1248 */
1249 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001250del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252 int i;
1253
1254 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001255 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257 typebuf.tb_len -= len;
1258
1259 /*
1260 * Easy case: Just increase typebuf.tb_off.
1261 */
1262 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1263 >= 3 * MAXMAPLEN + 3)
1264 typebuf.tb_off += len;
1265 /*
1266 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1267 */
1268 else
1269 {
1270 i = typebuf.tb_off + offset;
1271 /*
1272 * Leave some extra room at the end to avoid reallocation.
1273 */
1274 if (typebuf.tb_off > MAXMAPLEN)
1275 {
1276 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1277 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1278 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1279 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1280 typebuf.tb_off = MAXMAPLEN;
1281 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001282 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1284 typebuf.tb_buf + i + len,
1285 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001286 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1288 typebuf.tb_noremap + i + len,
1289 (size_t)(typebuf.tb_len - offset));
1290 }
1291
Bram Moolenaar30613902019-12-01 22:11:18 +01001292 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
1294 if (typebuf.tb_maplen < offset + len)
1295 typebuf.tb_maplen = offset;
1296 else
1297 typebuf.tb_maplen -= len;
1298 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001299 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
1301 if (typebuf.tb_silent < offset + len)
1302 typebuf.tb_silent = offset;
1303 else
1304 typebuf.tb_silent -= len;
1305 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001306 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 if (typebuf.tb_no_abbr_cnt < offset + len)
1309 typebuf.tb_no_abbr_cnt = offset;
1310 else
1311 typebuf.tb_no_abbr_cnt -= len;
1312 }
1313
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001314#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001315 // Reset the flag that text received from a client or from feedkeys()
1316 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001317 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#endif
1319 if (++typebuf.tb_change_cnt == 0)
1320 typebuf.tb_change_cnt = 1;
1321}
1322
zeertzjq094c4392024-04-18 22:09:37 +02001323/*
1324 * State for adding bytes to a recording or 'showcmd'.
1325 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001326typedef struct
1327{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001328 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001329 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001330 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001331 unsigned pending_special;
1332 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001333} gotchars_state_T;
1334
1335/*
1336 * Add a single byte to a recording or 'showcmd'.
1337 * Return TRUE if a full key has been received, FALSE otherwise.
1338 */
1339 static int
1340gotchars_add_byte(gotchars_state_T *state, char_u byte)
1341{
1342 int c = state->buf[state->buflen++] = byte;
1343 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001344 int in_special = state->pending_special > 0;
1345 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001346
zeertzjq6b13e3d2024-04-22 21:04:29 +02001347 if (in_special)
1348 state->pending_special--;
1349 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001350#ifdef FEAT_GUI
1351 || c == CSI
1352#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001353 )
1354 // When receiving a special key sequence, store it until we have all
1355 // the bytes and we can decide what to do with it.
1356 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001357
zeertzjq6b13e3d2024-04-22 21:04:29 +02001358 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001359 goto ret_false;
1360
zeertzjq6b13e3d2024-04-22 21:04:29 +02001361 if (in_mbyte)
1362 state->pending_mbyte--;
1363 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001364 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001365 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001366 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001367 if (state->prev_c == KS_MODIFIER)
1368 // When receiving a modifier, wait for the modified key.
1369 goto ret_false;
1370 c = TO_SPECIAL(state->prev_c, c);
1371 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1372 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1373 // in a recording.
1374 state->buflen = 0;
1375 }
1376 // When receiving a multibyte character, store it until we have all
1377 // the bytes, so that it won't be split between two buffer blocks,
1378 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001379 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001380 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001381
1382 if (state->pending_mbyte > 0)
1383 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001384
1385 retval = TRUE;
1386ret_false:
1387 state->prev_c = c;
1388 return retval;
1389}
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391/*
1392 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001393 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 */
1395 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001396gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001398 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001399 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001400 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001401 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
zeertzjqacdfb8a2024-04-17 21:28:54 +02001403 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001405 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001406 continue;
1407
Bram Moolenaar30613902019-12-01 22:11:18 +01001408 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001409 for (i = 0; i < state.buflen; ++i)
1410 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001412 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001414 state.buf[state.buflen] = NUL;
1415 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001416 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001417 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001419 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 may_sync_undo();
1423
1424#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001425 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 debug_did_msg = FALSE;
1427#endif
1428
Bram Moolenaar30613902019-12-01 22:11:18 +01001429 // Since characters have been typed, consider the following to be in
1430 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 ++maptick;
1432}
1433
1434/*
zeertzjqbf321802024-01-28 19:03:00 +01001435 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001436 */
1437 void
zeertzjqbf321802024-01-28 19:03:00 +01001438gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001439{
zeertzjqbf321802024-01-28 19:03:00 +01001440 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001441 gotchars(nop_buf, 3);
1442}
1443
1444/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001445 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1446 * character back into the typeahead buffer, thus gotchars() will be called
1447 * again.
1448 * Only affects recorded characters.
1449 */
1450 void
1451ungetchars(int len)
1452{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001453 if (reg_recording == 0)
1454 return;
1455
1456 delete_buff_tail(&recordbuff, len);
1457 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001458}
1459
1460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 * Sync undo. Called when typed characters are obtained from the typeahead
1462 * buffer, or when a menu is used.
1463 * Do not sync:
1464 * - In Insert mode, unless cursor key has been used.
1465 * - While reading a script file.
1466 * - When no_u_sync is non-zero.
1467 */
1468 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001469may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar24959102022-05-07 20:01:16 +01001471 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001472 && scriptin[curscript] == NULL)
1473 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474}
1475
1476/*
1477 * Make "typebuf" empty and allocate new buffers.
1478 * Returns FAIL when out of memory.
1479 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001480 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001481alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 typebuf.tb_buf = alloc(TYPELEN_INIT);
1484 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1485 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1486 {
1487 free_typebuf();
1488 return FAIL;
1489 }
1490 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001491 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 typebuf.tb_len = 0;
1493 typebuf.tb_maplen = 0;
1494 typebuf.tb_silent = 0;
1495 typebuf.tb_no_abbr_cnt = 0;
1496 if (++typebuf.tb_change_cnt == 0)
1497 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001498#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1499 typebuf_was_filled = FALSE;
1500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 return OK;
1502}
1503
1504/*
1505 * Free the buffers of "typebuf".
1506 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001507 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001508free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001510 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001511 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001512 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001513 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001514 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001515 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001516 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001517 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518}
1519
1520/*
1521 * When doing ":so! file", the current typeahead needs to be saved, and
1522 * restored when "file" has been read completely.
1523 */
1524static typebuf_T saved_typebuf[NSCRIPT];
1525
1526 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001527save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 init_typebuf();
1530 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001531 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (alloc_typebuf() == FAIL)
1533 {
1534 closescript();
1535 return FAIL;
1536 }
1537 return OK;
1538}
1539
Bram Moolenaar30613902019-12-01 22:11:18 +01001540static int old_char = -1; // character put back by vungetc()
1541static int old_mod_mask; // mod_mask for ungotten character
1542static int old_mouse_row; // mouse_row related to old_char
1543static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001544static int old_KeyStuffed; // whether old_char was stuffed
1545
zeertzjq6d4e7252022-04-07 13:58:04 +01001546static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001547{
1548 // If the old character was not stuffed and characters have been added to
1549 // the stuff buffer, need to first get the stuffed characters instead.
1550 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1551}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553/*
1554 * Save all three kinds of typeahead, so that the user must type at a prompt.
1555 */
1556 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001557save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 tp->save_typebuf = typebuf;
1560 tp->typebuf_valid = (alloc_typebuf() == OK);
1561 if (!tp->typebuf_valid)
1562 typebuf = tp->save_typebuf;
1563
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001564 tp->old_char = old_char;
1565 tp->old_mod_mask = old_mod_mask;
1566 old_char = -1;
1567
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001568 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001569 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001570 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001571 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572# ifdef USE_INPUT_BUF
1573 tp->save_inputbuf = get_input_buf();
1574# endif
1575}
1576
1577/*
1578 * Restore the typeahead to what it was before calling save_typeahead().
1579 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001580 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 */
1582 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001583restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 if (tp->typebuf_valid)
1586 {
1587 free_typebuf();
1588 typebuf = tp->save_typebuf;
1589 }
1590
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001591 old_char = tp->old_char;
1592 old_mod_mask = tp->old_mod_mask;
1593
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001594 free_buff(&readbuf1);
1595 readbuf1 = tp->save_readbuf1;
1596 free_buff(&readbuf2);
1597 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001599 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600# endif
1601}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603/*
1604 * Open a new script file for the ":source!" command.
1605 */
1606 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001607openscript(
1608 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001609 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 if (curscript + 1 == NSCRIPT)
1612 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001613 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 return;
1615 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001616
1617 // Disallow sourcing a file in the sandbox, the commands would be executed
1618 // later, possibly outside of the sandbox.
1619 if (check_secure())
1620 return;
1621
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001622#ifdef FEAT_EVAL
1623 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001624 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001625 return;
1626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar30613902019-12-01 22:11:18 +01001628 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001630 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 expand_env(name, NameBuff, MAXPATHL);
1632 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1633 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001634 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (curscript)
1636 --curscript;
1637 return;
1638 }
1639 if (save_typebuf() == FAIL)
1640 return;
1641
1642 /*
1643 * Execute the commands from the file right now when using ":source!"
1644 * after ":global" or ":argdo" or in a loop. Also when another command
1645 * follows. This means the display won't be updated. Don't do this
1646 * always, "make test" would fail.
1647 */
1648 if (directly)
1649 {
1650 oparg_T oa;
1651 int oldcurscript;
1652 int save_State = State;
1653 int save_restart_edit = restart_edit;
1654 int save_insertmode = p_im;
1655 int save_finish_op = finish_op;
1656 int save_msg_scroll = msg_scroll;
1657
Bram Moolenaar24959102022-05-07 20:01:16 +01001658 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001659 msg_scroll = FALSE; // no msg scrolling in Normal mode
1660 restart_edit = 0; // don't go to Insert mode
1661 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 clear_oparg(&oa);
1663 finish_op = FALSE;
1664
1665 oldcurscript = curscript;
1666 do
1667 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001668 update_topline_cursor(); // update cursor position and topline
1669 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001670 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 while (scriptin[oldcurscript] != NULL);
1673
1674 State = save_State;
1675 msg_scroll = save_msg_scroll;
1676 restart_edit = save_restart_edit;
1677 p_im = save_insertmode;
1678 finish_op = save_finish_op;
1679 }
1680}
1681
1682/*
1683 * Close the currently active input script.
1684 */
1685 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001686closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688 free_typebuf();
1689 typebuf = saved_typebuf[curscript];
1690
1691 fclose(scriptin[curscript]);
1692 scriptin[curscript] = NULL;
1693 if (curscript > 0)
1694 --curscript;
1695}
1696
Bram Moolenaarea408852005-06-25 22:49:46 +00001697#if defined(EXITFREE) || defined(PROTO)
1698 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001699close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001700{
1701 while (scriptin[0] != NULL)
1702 closescript();
1703}
1704#endif
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
1707 * Return TRUE when reading keys from a script file.
1708 */
1709 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001710using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 return scriptin[curscript] != NULL;
1713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001716 * This function is called just before doing a blocking wait. Thus after
1717 * waiting 'updatetime' for a character to arrive.
1718 */
1719 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001720before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001721{
1722 updatescript(0);
1723#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001724 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001725 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001726#endif
1727}
1728
1729/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001730 * updatescript() is called when a character can be written into the script
1731 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 *
1733 * All the changed memfiles are synced if c == 0 or when the number of typed
1734 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1735 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001736 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001737updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 static int count = 0;
1740
1741 if (c && scriptout)
1742 putc(c, scriptout);
1743 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1744 {
1745 ml_sync_all(c == 0, TRUE);
1746 count = 0;
1747 }
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02001748#ifdef FEAT_EVAL
1749 if (typedchars_pos < MAXMAPLEN)
1750 {
1751 typedchars[typedchars_pos] = c;
1752 typedchars_pos++;
1753 }
1754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001758 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1759 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001760 */
1761 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001762merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001763{
1764 int c = c_arg;
1765
Bram Moolenaarea83c192023-03-18 17:22:46 +00001766 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001767 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001768 {
1769 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001770 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001771 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001772 if (c == NUL)
1773 c = K_ZERO;
1774 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001775 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001776 // CTRL-6 is equivalent to CTRL-^
1777 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001778#ifdef FEAT_GUI_GTK
1779 // These mappings look arbitrary at the first glance, but in fact
1780 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1781 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1782 // more sense and is consistent with usual terminal behaviour).
1783 else if (c == '2')
1784 c = NUL;
1785 else if (c >= '3' && c <= '7')
1786 c = c ^ 0x28;
1787 else if (c == '8')
1788 c = BS;
1789 else if (c == '?')
1790 c = DEL;
1791#endif
1792 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001793 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001794 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001795
1796 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001797 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001798 && c >= 0 && c <= 127)
1799 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001800 // Some terminals (esp. Kitty) do not include Shift in the character.
1801 // Apply it here to get consistency across terminals. Only do ASCII
1802 // letters, for other characters it depends on the keyboard layout.
1803 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1804 {
1805 c += 'a' - 'A';
1806 *modifiers &= ~MOD_MASK_SHIFT;
1807 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001808 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001809 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001810 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001811
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001812 return c;
1813}
1814
1815/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001816 * Add a single byte to 'showcmd' for a partially matched mapping.
1817 * Call add_to_showcmd() if a full key has been received.
1818 */
1819 static void
1820add_byte_to_showcmd(char_u byte)
1821{
1822 static gotchars_state_T state;
1823 char_u *ptr;
1824 int modifiers = 0;
1825 int c = NUL;
1826
1827 if (!p_sc || msg_silent != 0)
1828 return;
1829
1830 if (!gotchars_add_byte(&state, byte))
1831 return;
1832
1833 state.buf[state.buflen] = NUL;
1834 state.buflen = 0;
1835
1836 ptr = state.buf;
1837 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1838 {
1839 modifiers = ptr[2];
1840 ptr += 3;
1841 }
1842
1843 if (*ptr != NUL)
1844 {
1845 char_u *mb_ptr = mb_unescape(&ptr);
1846
1847 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1848 if (c <= 0x7f)
1849 {
1850 // Merge modifiers into the key to make the result more readable.
1851 int modifiers_after = modifiers;
1852 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1853
1854 if (modifiers_after == 0)
1855 {
1856 modifiers = 0;
1857 c = mod_c;
1858 }
1859 }
1860 }
1861
1862 // TODO: is there a more readable and yet compact representation of
1863 // modifiers and special keys?
1864 if (modifiers != 0)
1865 {
1866 add_to_showcmd(K_SPECIAL);
1867 add_to_showcmd(KS_MODIFIER);
1868 add_to_showcmd(modifiers);
1869 }
1870 if (c != NUL)
1871 add_to_showcmd(c);
1872 while (*ptr != NUL)
1873 add_to_showcmd(*ptr++);
1874}
1875
1876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 * Get the next input character.
1878 * Can return a special key or a multi-byte character.
1879 * Can return NUL when called recursively, use safe_vgetc() if that's not
1880 * wanted.
1881 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1882 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001883 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
1885 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001886vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887{
1888 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001890 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001893#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001894 // Do garbage collection when garbagecollect() was called previously and
1895 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001896 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001897 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001898#endif
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 /*
1901 * If a character was put back with vungetc, it was already processed.
1902 * Return it directly.
1903 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001904 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 c = old_char;
1907 old_char = -1;
1908 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001909 mouse_row = old_mouse_row;
1910 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
zeertzjq81b46a62022-04-09 17:58:49 +01001914 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001915 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001916
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001917 mod_mask = 0;
1918 vgetc_mod_mask = 0;
1919 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001920
1921 // last_recorded_len can be larger than last_vgetc_recorded_len
1922 // if peeking records more
1923 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001924
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001925 for (;;) // this is done twice if there are modifiers
1926 {
1927 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001928
Bram Moolenaar78aed952022-09-24 15:36:35 +01001929 // No mapping after modifier has been read, using an input method
1930 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001931 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001932#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001933 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001934#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001935#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001936 || popup_no_mapping()
1937#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001938 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001940 ++no_mapping;
1941 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001942 // mod_mask value may change, remember we did the increment
1943 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001945 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001946 if (did_inc)
1947 {
1948 --no_mapping;
1949 --allow_keys;
1950 }
1951
Christopher Plewright03193062022-11-22 12:58:27 +00001952 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001953 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001954#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001955 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001956#endif
1957 )
1958 {
1959 int save_allow_keys = allow_keys;
1960
1961 ++no_mapping;
1962 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001963 c2 = vgetorpeek(TRUE); // no mapping for these chars
1964 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001965 --no_mapping;
1966 allow_keys = save_allow_keys;
1967 if (c2 == KS_MODIFIER)
1968 {
1969 mod_mask = c;
1970 continue;
1971 }
1972 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001974 // K_ESC is used to avoid ambiguity with the single Esc
1975 // character that might be the start of an escape sequence.
1976 // Convert it back to a single Esc here.
1977 if (c == K_ESC)
1978 c = ESC;
1979
Bram Moolenaar4f974752019-02-17 17:44:42 +01001980#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001981 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1982 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001983 if (
1984# ifdef VIMDLL
1985 gui.in_use &&
1986# endif
1987 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001989 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001990 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001991
1992 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001993 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001994 {
K.Takata54119102022-02-03 13:33:03 +00001995 name[j] = c;
1996 if (j < 199)
1997 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001998 }
K.Takata54119102022-02-03 13:33:03 +00001999 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002000 gui_make_tearoff(name);
2001 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002004#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002005 // GTK: <F10> normally selects the menu, but it's passed until
2006 // here to allow mapping it. Intercept and invoke the GTK
2007 // behavior if it's not mapped.
2008 if (c == K_F10 && gui.menubar != NULL)
2009 {
2010 gtk_menu_shell_select_first(
2011 GTK_MENU_SHELL(gui.menubar), FALSE);
2012 continue;
2013 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002016 // Handle focus event here, so that the caller doesn't need to
2017 // know about it. Return K_IGNORE so that we loop once (needed
2018 // if 'lazyredraw' is set).
2019 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002020 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002021 ui_focus_change(c == K_FOCUSGAINED);
2022 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02002023 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002024
2025 // Translate K_CSI to CSI. The special key is only used to
2026 // avoid it being recognized as the start of a special key.
2027 if (c == K_CSI)
2028 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002030#ifdef FEAT_EVAL
2031 if (c == K_SID)
2032 {
2033 int j;
2034
2035 // Handle <SID>{sid}; Do up to 20 digits for safety.
2036 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002037 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002038 last_used_sid = last_used_sid * 10 + (c - '0');
2039 last_used_map = NULL;
2040 continue;
2041 }
2042#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002043 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002044
zeertzjq90a80022024-07-13 19:06:44 +02002045 // For a multi-byte character get all the bytes and return the
2046 // converted character.
2047 // Note: This will loop until enough bytes are received!
2048 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2049 {
2050 ++no_mapping;
2051 buf[0] = c;
2052 for (i = 1; i < n; ++i)
2053 {
2054 buf[i] = vgetorpeek(TRUE);
2055 if (buf[i] == K_SPECIAL
2056#ifdef FEAT_GUI
2057 || (buf[i] == CSI)
2058#endif
2059 )
2060 {
2061 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2062 // sequence, which represents a K_SPECIAL (0x80),
2063 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2064 // represents a CSI (0x9B),
2065 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2066 // too.
2067 c = vgetorpeek(TRUE);
2068 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2069 buf[i] = CSI;
2070 }
2071 }
2072 --no_mapping;
2073 c = (*mb_ptr2char)(buf);
2074 }
2075
2076 if (vgetc_char == 0)
2077 {
2078 vgetc_mod_mask = mod_mask;
2079 vgetc_char = c;
2080 }
2081
zeertzjqd9be94c2024-07-14 10:20:20 +02002082 // A keypad or special function key was not mapped, use it like
2083 // its ASCII equivalent.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002084 switch (c)
2085 {
2086 case K_KPLUS: c = '+'; break;
2087 case K_KMINUS: c = '-'; break;
2088 case K_KDIVIDE: c = '/'; break;
2089 case K_KMULTIPLY: c = '*'; break;
2090 case K_KENTER: c = CAR; break;
2091 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002092#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002093 // Can be either '.' or a ',',
2094 // depending on the type of keypad.
2095 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002096#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002097 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002098#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002099 case K_K0: c = '0'; break;
2100 case K_K1: c = '1'; break;
2101 case K_K2: c = '2'; break;
2102 case K_K3: c = '3'; break;
2103 case K_K4: c = '4'; break;
2104 case K_K5: c = '5'; break;
2105 case K_K6: c = '6'; break;
2106 case K_K7: c = '7'; break;
2107 case K_K8: c = '8'; break;
2108 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002109
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002110 case K_XHOME:
2111 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002112 {
2113 c = K_S_HOME;
2114 mod_mask = 0;
2115 }
2116 else if (mod_mask == MOD_MASK_CTRL)
2117 {
2118 c = K_C_HOME;
2119 mod_mask = 0;
2120 }
2121 else
2122 c = K_HOME;
2123 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002124 case K_XEND:
2125 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002126 {
2127 c = K_S_END;
2128 mod_mask = 0;
2129 }
2130 else if (mod_mask == MOD_MASK_CTRL)
2131 {
2132 c = K_C_END;
2133 mod_mask = 0;
2134 }
2135 else
2136 c = K_END;
2137 break;
2138
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002139 case K_XUP: c = K_UP; break;
2140 case K_XDOWN: c = K_DOWN; break;
2141 case K_XLEFT: c = K_LEFT; break;
2142 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002145 break;
2146 }
zeertzjq81b46a62022-04-09 17:58:49 +01002147
2148 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002150
2151#ifdef FEAT_EVAL
2152 /*
2153 * In the main loop "may_garbage_collect" can be set to do garbage
2154 * collection in the first next vgetc(). It's disabled after that to
2155 * avoid internally used Lists and Dicts to be freed.
2156 */
2157 may_garbage_collect = FALSE;
2158#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002159
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002160#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002161 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002162 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002163 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002164 bevalexpr_due_set = FALSE;
2165 ui_remove_balloon();
2166 }
2167#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002168#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002169 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2170 // are filtered.
2171 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002172 {
2173 if (c == Ctrl_C)
2174 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002175 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002176 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002177#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002178
Shougo Matsushita83678842024-07-11 22:05:12 +02002179#ifdef FEAT_EVAL
2180 c = do_key_input_pre(c);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002181
2182 // Clear the next typedchars_pos
2183 typedchars_pos = 0;
Shougo Matsushita83678842024-07-11 22:05:12 +02002184#endif
2185
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002186 // Need to process the character before we know it's safe to do something
2187 // else.
2188 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002189 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002190
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002191 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192}
2193
Shougo Matsushita83678842024-07-11 22:05:12 +02002194#ifdef FEAT_EVAL
2195/*
2196 * Handle the InsertCharPre autocommand.
2197 * "c" is the character that was typed.
2198 * Return new input character.
2199 */
2200 static int
2201do_key_input_pre(int c)
2202{
2203 int res = c;
2204 char_u buf[MB_MAXBYTES + 1];
2205 char_u curr_mode[MODE_MAX_LENGTH];
2206 int save_State = State;
2207 save_v_event_T save_v_event;
2208 dict_T *v_event;
2209
2210 // Return quickly when there is nothing to do.
2211 if (!has_keyinputpre())
2212 return res;
2213
2214 if (IS_SPECIAL(c))
2215 {
2216 buf[0] = K_SPECIAL;
2217 buf[1] = KEY2TERMCAP0(c);
2218 buf[2] = KEY2TERMCAP1(c);
2219 buf[3] = NUL;
2220 }
2221 else
2222 buf[(*mb_char2bytes)(c, buf)] = NUL;
2223
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002224 typedchars[typedchars_pos] = NUL;
2225 vim_unescape_csi(typedchars);
2226
Shougo Matsushita83678842024-07-11 22:05:12 +02002227 get_mode(curr_mode);
2228
2229 // Lock the text to avoid weird things from happening.
2230 ++textlock;
2231 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2232
2233 v_event = get_v_event(&save_v_event);
2234 (void)dict_add_bool(v_event, "typed", KeyTyped);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002235 (void)dict_add_string(v_event, "typedchar", typedchars);
Shougo Matsushita83678842024-07-11 22:05:12 +02002236
2237 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2238 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2239 {
2240 // Get the value of v:char. It may be empty or more than one
2241 // character. Only use it when changed, otherwise continue with the
2242 // original character.
2243 char_u *v_char;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002244 size_t v_charlen;
Shougo Matsushita83678842024-07-11 22:05:12 +02002245
2246 v_char = get_vim_var_str(VV_CHAR);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002247 v_charlen = STRLEN(v_char);
Shougo Matsushita83678842024-07-11 22:05:12 +02002248
2249 // Convert special bytes when it is special string.
John Marriotte7a1bbf2024-11-11 20:40:33 +01002250 if (v_charlen >= 3 && v_char[0] == K_SPECIAL)
Shougo Matsushita83678842024-07-11 22:05:12 +02002251 res = TERMCAP2KEY(v_char[1], v_char[2]);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002252 else if (v_charlen > 0)
Shougo Matsushita83678842024-07-11 22:05:12 +02002253 res = PTR2CHAR(v_char);
2254 }
2255
2256 restore_v_event(v_event, &save_v_event);
2257
2258 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2259 --textlock;
2260
2261 // Restore the State, it may have been changed.
2262 State = save_State;
2263
2264 return res;
2265}
2266#endif
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268/*
2269 * Like vgetc(), but never return a NUL when called recursively, get a key
2270 * directly from the user (ignoring typeahead).
2271 */
2272 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002273safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 int c;
2276
2277 c = vgetc();
2278 if (c == NUL)
2279 c = get_keystroke();
2280 return c;
2281}
2282
2283/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002284 * Like safe_vgetc(), but loop to handle K_IGNORE.
2285 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002286 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002287 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002288 static int
2289plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002290{
2291 int c;
2292
2293 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002294 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002295 while (c == K_IGNORE
2296 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2297 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002298 return c;
2299}
2300
2301/*
2302 * Like safe_vgetc(), but loop to handle K_IGNORE.
2303 * Also ignore scrollbar events.
2304 */
2305 int
2306plain_vgetc(void)
2307{
2308 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002309
2310 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002311 // Only handle the first pasted character. Drop the rest, since we
2312 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002313 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2314
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002315 return c;
2316}
2317
2318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 * Check if a character is available, such that vgetc() will not block.
2320 * If the next character is a special character or multi-byte, the returned
2321 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002322 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
2324 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002325vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002327 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002329 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330}
2331
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002332#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333/*
2334 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2335 * codes.
2336 */
2337 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002338vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 int c;
2341
2342 ++no_mapping;
2343 ++allow_keys;
2344 c = vpeekc();
2345 --no_mapping;
2346 --allow_keys;
2347 return c;
2348}
2349#endif
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351/*
2352 * Check if any character is available, also half an escape sequence.
2353 * Trick: when no typeahead found, but there is something in the typeahead
2354 * buffer, it must be an ESC that is recognized as the start of a key code.
2355 */
2356 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002357vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 int c;
2360
2361 c = vpeekc();
2362 if (c == NUL && typebuf.tb_len > 0)
2363 c = ESC;
2364 return c;
2365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367/*
2368 * Call vpeekc() without causing anything to be mapped.
2369 * Return TRUE if a character is available, FALSE otherwise.
2370 */
2371 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002372char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 int retval;
2375
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002376#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002377 // When test_override("char_avail", 1) was called pretend there is no
2378 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002379 if (disable_char_avail_for_testing)
2380 return FALSE;
2381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 ++no_mapping;
2383 retval = vpeekc();
2384 --no_mapping;
2385 return (retval != NUL);
2386}
2387
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002388#if defined(FEAT_EVAL) || defined(PROTO)
2389/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002390 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002391 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002392 static void
zeertzjqe0a2ab32025-02-02 09:14:35 +01002393getchar_common(typval_T *argvars, typval_T *rettv, int allow_number)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002394{
zeertzjqedf0f7d2025-02-02 19:01:01 +01002395 varnumber_T n = 0;
2396 int called_emsg_start = called_emsg;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002397 int error = FALSE;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002398 int simplify = TRUE;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002399 char_u cursor_flag = 'm';
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002400
zeertzjqe0a2ab32025-02-02 09:14:35 +01002401 if ((in_vim9script()
2402 && check_for_opt_bool_or_number_arg(argvars, 0) == FAIL)
2403 || (argvars[0].v_type != VAR_UNKNOWN
2404 && check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002405 return;
2406
zeertzjqe0a2ab32025-02-02 09:14:35 +01002407 if (argvars[0].v_type != VAR_UNKNOWN && argvars[1].v_type == VAR_DICT)
2408 {
2409 dict_T *d = argvars[1].vval.v_dict;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002410 char_u *cursor_str;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002411
2412 if (allow_number)
2413 allow_number = dict_get_bool(d, "number", TRUE);
2414 else if (dict_has_key(d, "number"))
zeertzjqe0a2ab32025-02-02 09:14:35 +01002415 semsg(_(e_invalid_argument_str), "number");
zeertzjqe0a2ab32025-02-02 09:14:35 +01002416
2417 simplify = dict_get_bool(d, "simplify", TRUE);
zeertzjqedf0f7d2025-02-02 19:01:01 +01002418
2419 cursor_str = dict_get_string(d, "cursor", FALSE);
2420 if (cursor_str != NULL)
2421 {
2422 if (STRCMP(cursor_str, "hide") != 0
2423 && STRCMP(cursor_str, "keep") != 0
2424 && STRCMP(cursor_str, "msg") != 0)
2425 semsg(_(e_invalid_value_for_argument_str_str), "cursor",
2426 cursor_str);
2427 else
2428 cursor_flag = cursor_str[0];
2429 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002430 }
2431
zeertzjqedf0f7d2025-02-02 19:01:01 +01002432 if (called_emsg != called_emsg_start)
2433 return;
2434
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002435#ifdef MESSAGE_QUEUE
2436 // vpeekc() used to check for messages, but that caused problems, invoking
2437 // a callback where it was not expected. Some plugins use getchar(1) in a
2438 // loop to await a message, therefore make sure we check for messages here.
2439 parse_queued_messages();
2440#endif
2441
zeertzjqedf0f7d2025-02-02 19:01:01 +01002442 if (cursor_flag == 'h')
2443 cursor_sleep();
2444 else if (cursor_flag == 'm')
2445 windgoto(msg_row, msg_col);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002446
2447 ++no_mapping;
2448 ++allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002449 if (!simplify)
2450 ++no_reduce_keys;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002451 for (;;)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002452 {
zeertzjqe0a2ab32025-02-02 09:14:35 +01002453 if (argvars[0].v_type == VAR_UNKNOWN
2454 || (argvars[0].v_type == VAR_NUMBER
2455 && argvars[0].vval.v_number == -1))
Bram Moolenaar30613902019-12-01 22:11:18 +01002456 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002457 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002458 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002459 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002460 n = vpeekc_any();
2461 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002462 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002463 n = 0;
2464 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002465 // getchar(0) and char avail() != NUL: get a character.
2466 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2467 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002468
Bram Moolenaar15183b42020-09-05 19:59:39 +02002469 if (n == K_IGNORE || n == K_MOUSEMOVE
2470 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002471 continue;
2472 break;
2473 }
2474 --no_mapping;
2475 --allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002476 if (!simplify)
2477 --no_reduce_keys;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002478
zeertzjqedf0f7d2025-02-02 19:01:01 +01002479 if (cursor_flag == 'h')
2480 cursor_unsleep();
2481
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002482 set_vim_var_nr(VV_MOUSE_WIN, 0);
2483 set_vim_var_nr(VV_MOUSE_WINID, 0);
2484 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2485 set_vim_var_nr(VV_MOUSE_COL, 0);
2486
zeertzjqe0a2ab32025-02-02 09:14:35 +01002487 if (n != 0 && (!allow_number || IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002488 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002489 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002490 int i = 0;
2491
Bram Moolenaar30613902019-12-01 22:11:18 +01002492 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002493 if (mod_mask != 0)
2494 {
2495 temp[i++] = K_SPECIAL;
2496 temp[i++] = KS_MODIFIER;
2497 temp[i++] = mod_mask;
2498 }
2499 if (IS_SPECIAL(n))
2500 {
2501 temp[i++] = K_SPECIAL;
2502 temp[i++] = K_SECOND(n);
2503 temp[i++] = K_THIRD(n);
2504 }
2505 else if (has_mbyte)
2506 i += (*mb_char2bytes)(n, temp + i);
2507 else
2508 temp[i++] = n;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002509 temp[i] = NUL;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002510 rettv->v_type = VAR_STRING;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002511 rettv->vval.v_string = vim_strnsave(temp, i);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002512
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002513 if (is_mouse_key(n))
2514 {
2515 int row = mouse_row;
2516 int col = mouse_col;
2517 win_T *win;
2518 linenr_T lnum;
2519 win_T *wp;
2520 int winnr = 1;
2521
2522 if (row >= 0 && col >= 0)
2523 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002524 // Find the window at the mouse coordinates and compute the
2525 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002526 win = mouse_find_win(&row, &col, FIND_POPUP);
2527 if (win == NULL)
2528 return;
2529 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002530#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002531 if (WIN_IS_POPUP(win))
2532 winnr = 0;
2533 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002534#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002535 for (wp = firstwin; wp != win && wp != NULL;
2536 wp = wp->w_next)
2537 ++winnr;
2538 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2539 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2540 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2541 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2542 }
2543 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002544 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002545 else if (!allow_number)
2546 rettv->v_type = VAR_STRING;
2547 else
2548 rettv->vval.v_number = n;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002549}
2550
2551/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002552 * "getchar()" function
2553 */
2554 void
2555f_getchar(typval_T *argvars, typval_T *rettv)
2556{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002557 getchar_common(argvars, rettv, TRUE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002558}
2559
2560/*
2561 * "getcharstr()" function
2562 */
2563 void
2564f_getcharstr(typval_T *argvars, typval_T *rettv)
2565{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002566 getchar_common(argvars, rettv, FALSE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002567}
2568
2569/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002570 * "getcharmod()" function
2571 */
2572 void
2573f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2574{
2575 rettv->vval.v_number = mod_mask;
2576}
2577#endif // FEAT_EVAL
2578
2579#if defined(MESSAGE_QUEUE) || defined(PROTO)
2580# define MAX_REPEAT_PARSE 8
2581
2582/*
2583 * Process messages that have been queued for netbeans or clientserver.
2584 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002585 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002586 * it is safe to do so.
2587 */
2588 void
2589parse_queued_messages(void)
2590{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002591 int old_curwin_id;
2592 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002593 int i;
2594 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002595 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002596 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002597
2598 // Do not handle messages while redrawing, because it may cause buffers to
2599 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002600 // Also bail out when parsing messages was explicitly disabled.
2601 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002602 return;
2603
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002604 // If memory allocation fails during startup we'll exit but curbuf or
2605 // curwin could be NULL.
2606 if (curbuf == NULL || curwin == NULL)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02002607 return;
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002608
2609 old_curbuf_fnum = curbuf->b_fnum;
2610 old_curwin_id = curwin->w_id;
2611
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002612 ++entered;
2613
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002614 // may_garbage_collect is set in main_loop() to do garbage collection when
2615 // blocking to wait on a character. We don't want that while parsing
2616 // messages, a callback may invoke vgetc() while lists and dicts are in use
2617 // in the call stack.
2618 may_garbage_collect = FALSE;
2619
2620 // Loop when a job ended, but don't keep looping forever.
2621 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2622 {
2623 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002624# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002625 channel_handle_events(FALSE);
2626# endif
2627
2628# ifdef FEAT_NETBEANS_INTG
2629 // Process the queued netbeans messages.
2630 netbeans_parse_messages();
2631# endif
2632# ifdef FEAT_JOB_CHANNEL
2633 // Write any buffer lines still to be written.
2634 channel_write_any_lines();
2635
2636 // Process the messages queued on channels.
2637 channel_parse_messages();
2638# endif
2639# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2640 // Process the queued clientserver messages.
2641 server_parse_messages();
2642# endif
2643# ifdef FEAT_JOB_CHANNEL
2644 // Check if any jobs have ended. If so, repeat the above to handle
2645 // changes, e.g. stdin may have been closed.
2646 if (job_check_ended())
2647 continue;
2648# endif
2649# ifdef FEAT_TERMINAL
2650 free_unused_terminals();
2651# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002652
2653# ifdef FEAT_SOUND_MACOSX
2654 process_cfrunloop();
2655# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002656# ifdef FEAT_SOUND_CANBERRA
2657 if (has_sound_callback_in_queue())
2658 invoke_sound_callback();
2659# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002660#ifdef SIGUSR1
2661 if (got_sigusr1)
2662 {
2663 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2664 got_sigusr1 = FALSE;
2665 }
2666#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002667 break;
2668 }
2669
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002670 // When not nested we'll go back to waiting for a typed character. If it
2671 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002672 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002673 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002674
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002675 may_garbage_collect = save_may_garbage_collect;
2676
2677 // If the current window or buffer changed we need to bail out of the
2678 // waiting loop. E.g. when a job exit callback closes the terminal window.
2679 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002680 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002681
2682 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002683}
2684#endif
2685
2686
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002687typedef enum {
2688 map_result_fail, // failed, break loop
2689 map_result_get, // get a character from typeahead
2690 map_result_retry, // try to map again
2691 map_result_nomatch // no matching mapping, get char
2692} map_result_T;
2693
2694/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002695 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002696 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002697 */
2698 static int
zeertzjqee446032022-04-30 15:02:22 +01002699at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002700{
2701 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2702 int c = *p;
2703
2704 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002705 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002706 && p[1] == KS_MODIFIER
2707 && (p[2] & MOD_MASK_CTRL))
2708 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002709 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2710 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002711}
2712
2713/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002714 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002715 * into just a key, apply that.
2716 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2717 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002718 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002719 */
2720 static int
2721check_simplify_modifier(int max_offset)
2722{
2723 int offset;
2724 char_u *tp;
2725
2726 for (offset = 0; offset < max_offset; ++offset)
2727 {
2728 if (offset + 3 >= typebuf.tb_len)
2729 break;
2730 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002731 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002732 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002733 // A modifier was not used for a mapping, apply it to ASCII keys.
2734 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002735 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002736 int c = tp[3];
2737 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002738
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002739 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002740 {
2741 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002742 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002743
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002744 if (offset == 0)
2745 {
2746 // At the start: remember the character and mod_mask before
2747 // merging, in some cases, e.g. at the hit-return prompt,
2748 // they are put back in the typeahead buffer.
2749 vgetc_char = c;
2750 vgetc_mod_mask = tp[2];
2751 }
zeertzjq12e21e32022-04-27 11:58:01 +01002752 if (IS_SPECIAL(new_c))
2753 {
2754 new_string[0] = K_SPECIAL;
2755 new_string[1] = K_SECOND(new_c);
2756 new_string[2] = K_THIRD(new_c);
2757 len = 3;
2758 }
2759 else
2760 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002761 if (modifier == 0)
2762 {
2763 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002764 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002765 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002766 }
2767 else
2768 {
2769 tp[2] = modifier;
2770 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002771 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002772 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002773 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002774 return len;
2775 }
2776 }
2777 }
2778 return 0;
2779}
2780
2781/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002782 * Return TRUE if the terminal sends modifiers with various keys. This is when
2783 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2784 * enabled.
2785 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002786 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002787key_protocol_enabled(void)
2788{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002789 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2790 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2791 ? modify_otherkeys_state == MOKS_ENABLED
2792 : seenModifyOtherKeys;
2793 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002794}
2795
2796/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002797 * Handle mappings in the typeahead buffer.
2798 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002799 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002800 * - When there is no match yet, return map_result_nomatch, need to get more
2801 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002802 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002803 */
2804 static int
2805handle_mapping(
2806 int *keylenp,
2807 int *timedout,
2808 int *mapdepth)
2809{
2810 mapblock_T *mp = NULL;
2811 mapblock_T *mp2;
2812 mapblock_T *mp_match;
2813 int mp_match_len = 0;
2814 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002815 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002816 int tb_c1;
2817 int mlen;
2818#ifdef FEAT_LANGMAP
2819 int nolmaplen;
2820#endif
2821 int keylen = *keylenp;
2822 int i;
2823 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002824 int is_plug_map = FALSE;
2825
zeertzjqbb404f52022-07-23 06:25:29 +01002826 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002827 if (typebuf.tb_len >= 3
2828 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002829 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2830 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2831 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002832
2833 /*
2834 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002835 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 *
2837 * Don't look for mappings if:
2838 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2839 * - maphash_valid not set: no mappings present.
2840 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2841 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002842 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002843 * - waiting for a char with --more--
2844 * - in Ctrl-X mode, and we get a valid char for that mode
2845 */
2846 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2847 if (no_mapping == 0 && is_maphash_valid()
2848 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002849 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002850 || (p_remap
2851 && (typebuf.tb_noremap[typebuf.tb_off]
2852 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002853 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2854 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2855 && State != MODE_ASKMORE
2856 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002857 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002858 {
Christopher Plewright03193062022-11-22 12:58:27 +00002859#ifdef FEAT_GUI
2860 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2861 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002862 {
2863 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2864 // K_SPECIAL KS_MODIFIER {flags}.
2865 tb_c1 = K_SPECIAL;
2866 }
2867#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002868#ifdef FEAT_LANGMAP
2869 if (tb_c1 == K_SPECIAL)
2870 nolmaplen = 2;
2871 else
2872 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002873 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2874 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002875 nolmaplen = 0;
2876 }
2877#endif
2878 // First try buffer-local mappings.
2879 mp = get_buf_maphash_list(local_State, tb_c1);
2880 mp2 = get_maphash_list(local_State, tb_c1);
2881 if (mp == NULL)
2882 {
2883 // There are no buffer-local mappings.
2884 mp = mp2;
2885 mp2 = NULL;
2886 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002887
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002888 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002889 * Loop until a partly matching mapping is found or all (local)
2890 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002891 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002892 * A full match is only accepted if there is no partly match, so "aa"
2893 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002894 */
2895 mp_match = NULL;
2896 mp_match_len = 0;
2897 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002898 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002899 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002900 // Only consider an entry if the first character matches and it is
2901 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002902 // Skip ":lmap" mappings if keys were mapped.
2903 if (mp->m_keys[0] == tb_c1
2904 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002905 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002906 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002907 && ((mp->m_mode & MODE_LANGMAP) == 0
2908 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002909 {
2910#ifdef FEAT_LANGMAP
2911 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002912 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002913#endif
2914 // find the match length of this mapping
2915 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2916 {
zeertzjq49660f52022-10-20 17:59:38 +01002917 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002918#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002919 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002920 {
2921 if (nomap == 2 && c2 == KS_MODIFIER)
2922 modifiers = 1;
2923 else if (nomap == 1 && modifiers == 1)
2924 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002925 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002926 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002927 else
zeertzjq49660f52022-10-20 17:59:38 +01002928 {
2929 if (c2 == K_SPECIAL)
2930 nomap = 2;
2931 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2932 // Only apply 'langmap' if merging modifiers into
2933 // the key will not result in another character,
2934 // so that 'langmap' behaves consistently in
2935 // different terminals and GUIs.
2936 LANGMAP_ADJUST(c2, TRUE);
2937 modifiers = 0;
2938 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002939#endif
zeertzjq49660f52022-10-20 17:59:38 +01002940 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002941 break;
2942 }
2943
Bram Moolenaareda35f72019-08-03 14:59:44 +02002944 // Don't allow mapping the first byte(s) of a multi-byte char.
2945 // Happens when mapping <M-a> and then changing 'encoding'.
2946 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002947 {
2948 char_u *p1 = mp->m_keys;
2949 char_u *p2 = mb_unescape(&p1);
2950
2951 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002952 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002953 mlen = 0;
2954 }
2955
2956 // Check an entry whether it matches.
2957 // - Full match: mlen == keylen
2958 // - Partly match: mlen == typebuf.tb_len
2959 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002960 if (mlen == keylen || (mlen == typebuf.tb_len
2961 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002962 {
2963 char_u *s;
2964 int n;
2965
Bram Moolenaareda35f72019-08-03 14:59:44 +02002966 // If only script-local mappings are allowed, check if the
2967 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002968 s = typebuf.tb_noremap + typebuf.tb_off;
2969 if (*s == RM_SCRIPT
2970 && (mp->m_keys[0] != K_SPECIAL
2971 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002972 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002973 continue;
2974
Bram Moolenaareda35f72019-08-03 14:59:44 +02002975 // If one of the typed keys cannot be remapped, skip the
2976 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002977 for (n = mlen; --n >= 0; )
2978 if (*s++ & (RM_NONE|RM_ABBR))
2979 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002980 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002981 continue;
2982
2983 if (keylen > typebuf.tb_len)
2984 {
2985 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002986 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002987 {
2988 // break at a partly match
2989 keylen = KEYLEN_PART_MAP;
2990 break;
2991 }
2992 }
2993 else if (keylen > mp_match_len)
2994 {
2995 // found a longer match
2996 mp_match = mp;
2997 mp_match_len = keylen;
2998 }
2999 }
3000 else
Oleg Goncharov56904f92024-07-23 20:34:15 +02003001 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003002 // No match; may have to check for termcode at next
Oleg Goncharov56904f92024-07-23 20:34:15 +02003003 // character.
3004
3005 // If the first character that didn't match is
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003006 // K_SPECIAL then check for a termcode. This isn't perfect
3007 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003008 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003009 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003010 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003011 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
3012 }
3013 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
3014 want_termcode = 1;
Oleg Goncharov56904f92024-07-23 20:34:15 +02003015
3016 // Check termcode for uppercase character to properly
3017 // process "ESC[27;2;<ascii code>~" control sequences.
3018 if (ASCII_ISUPPER(mp->m_keys[mlen]))
3019 want_termcode = 1;
3020 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003021 }
3022 }
3023
Bram Moolenaareda35f72019-08-03 14:59:44 +02003024 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00003025 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003026 {
3027 mp = mp_match;
3028 keylen = mp_match_len;
3029 }
3030 }
3031
3032 /*
3033 * Check for match with 'pastetoggle'
3034 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003035 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003036 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003037 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
3038 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003039 break;
3040 if (p_pt[mlen] == NUL) // match
3041 {
3042 // write chars to script file(s)
3043 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003044 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3045 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003046
3047 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003048 set_option_value_give_err((char_u *)"paste",
3049 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01003050 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003051 {
3052 msg_col = 0;
3053 msg_row = Rows - 1;
3054 msg_clr_eos(); // clear ruler
3055 }
3056 status_redraw_all();
3057 redraw_statuslines();
3058 showmode();
3059 setcursor();
3060 *keylenp = keylen;
3061 return map_result_retry;
3062 }
3063 // Need more chars for partly match.
3064 if (mlen == typebuf.tb_len)
3065 keylen = KEYLEN_PART_KEY;
3066 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003067 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003068 max_mlen = mlen + 1;
3069 }
3070
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003071 // May check for a terminal code when there is no mapping or only a partial
3072 // mapping. Also check if there is a full mapping with <Esc>, unless timed
3073 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003074 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003075 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
3076 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003077 {
3078 int save_keylen = keylen;
3079
3080 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003081 * When no matching mapping found or found a non-matching mapping that
3082 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003083 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02003084 * - mapping is allowed,
3085 * - keys have not been mapped,
3086 * - and not an ESC sequence, not in insert mode or p_ek is on,
3087 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003088 */
zeertzjq68a573c2022-04-28 14:10:01 +01003089 if (no_mapping == 0 || allow_keys != 0)
3090 {
3091 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003092 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003093 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003094 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003095 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3096 else
3097 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003098
Bram Moolenaar0684e362020-12-03 19:54:42 +01003099 // If no termcode matched but 'pastetoggle' matched partially
3100 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003101 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003102 keylen = KEYLEN_PART_KEY;
3103
Bram Moolenaar975a8802020-06-06 22:36:24 +02003104 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003105 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003106#ifdef FEAT_TERMINAL
3107 check_no_reduce_keys(); // may update the no_reduce_keys flag
3108#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003109 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003110 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003111 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003112 if (keylen < 0)
3113 // ins_typebuf() failed
3114 return map_result_fail;
3115 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003116
Bram Moolenaareda35f72019-08-03 14:59:44 +02003117 // When getting a partial match, but the last characters were not
3118 // typed, don't wait for a typed character to complete the
3119 // termcode. This helps a lot when a ":normal" command ends in an
3120 // ESC.
3121 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003122 keylen = 0;
3123 }
3124 else
3125 keylen = 0;
3126 if (keylen == 0) // no matching terminal code
3127 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003128#ifdef AMIGA
3129 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003130 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003131 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003132 {
3133 char_u *s;
3134
3135 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003136 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3137 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003138 ++s)
3139 ;
3140 if (*s == 'r' || *s == '|') // found one
3141 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003142 del_typebuf(
3143 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003144 // get size and redraw screen
3145 shell_resized();
3146 *keylenp = keylen;
3147 return map_result_retry;
3148 }
3149 if (*s == NUL) // need more characters
3150 keylen = KEYLEN_PART_KEY;
3151 }
3152 if (keylen >= 0)
3153#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003154 // When there was a matching mapping and no termcode could be
3155 // replaced after another one, use that mapping (loop around).
3156 // If there was no mapping at all use the character from the
3157 // typeahead buffer right here.
3158 if (mp == NULL)
3159 {
3160 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003161 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003162 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003163 }
3164
3165 if (keylen > 0) // full matching terminal code
3166 {
3167#if defined(FEAT_GUI) && defined(FEAT_MENU)
3168 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003169 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3170 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003171 {
3172 int idx;
3173
Bram Moolenaareda35f72019-08-03 14:59:44 +02003174 // Using a menu may cause a break in undo! It's like using
3175 // gotchars(), but without recording or writing to a script
3176 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003177 may_sync_undo();
3178 del_typebuf(3, 0);
3179 idx = get_menu_index(current_menu, local_State);
3180 if (idx != MENU_INDEX_INVALID)
3181 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003182 // In Select mode and a Visual mode menu is used: Switch
3183 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003184 // back to Select mode.
3185 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003186 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003187 {
3188 VIsual_select = FALSE;
3189 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003190 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003191 }
3192 ins_typebuf(current_menu->strings[idx],
3193 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003194 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003195 }
3196 }
3197#endif // FEAT_GUI && FEAT_MENU
3198 *keylenp = keylen;
3199 return map_result_retry; // try mapping again
3200 }
3201
Bram Moolenaareda35f72019-08-03 14:59:44 +02003202 // Partial match: get some more characters. When a matching mapping
3203 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003204 if (mp == NULL || keylen < 0)
3205 keylen = KEYLEN_PART_KEY;
3206 else
3207 keylen = mp_match_len;
3208 }
3209
3210 /*
3211 * complete match
3212 */
3213 if (keylen >= 0 && keylen <= typebuf.tb_len)
3214 {
3215 char_u *map_str;
3216
3217#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003218 int save_m_expr;
3219 int save_m_noremap;
3220 int save_m_silent;
3221 char_u *save_m_keys;
zeertzjq9d997ad2024-07-29 21:10:07 +02003222 char_u *save_alt_m_keys;
zeertzjq74011dc2024-07-30 19:17:56 +02003223 int save_alt_m_keylen;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003224#else
3225# define save_m_noremap mp->m_noremap
3226# define save_m_silent mp->m_silent
3227#endif
3228
3229 // write chars to script file(s)
3230 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003231 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3232 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003233
3234 cmd_silent = (typebuf.tb_silent > 0);
3235 del_typebuf(keylen, 0); // remove the mapped keys
3236
3237 /*
3238 * Put the replacement string in front of mapstr.
3239 * The depth check catches ":map x y" and ":map y x".
3240 */
3241 if (++*mapdepth >= p_mmd)
3242 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003243 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003244 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003245 redrawcmdline();
3246 else
3247 setcursor();
3248 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003249 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003250 *keylenp = keylen;
3251 return map_result_fail;
3252 }
3253
3254 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003255 * In Select mode and a Visual mode mapping is used: Switch to Visual
3256 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003257 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003258 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003259 {
3260 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003261 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003262 }
3263
3264#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003265 // Copy the values from *mp that are used, because evaluating the
3266 // expression may invoke a function that redefines the mapping, thereby
3267 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003268 save_m_expr = mp->m_expr;
3269 save_m_noremap = mp->m_noremap;
3270 save_m_silent = mp->m_silent;
3271 save_m_keys = NULL; // only saved when needed
zeertzjq9d997ad2024-07-29 21:10:07 +02003272 save_alt_m_keys = NULL; // only saved when needed
zeertzjq74011dc2024-07-30 19:17:56 +02003273 save_alt_m_keylen = mp->m_alt != NULL ? mp->m_alt->m_keylen : 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003274
3275 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003276 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3277 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003278 */
3279 if (mp->m_expr)
3280 {
3281 int save_vgetc_busy = vgetc_busy;
3282 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003283 int was_screen_col = screen_cur_col;
3284 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003285 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003286
3287 vgetc_busy = 0;
3288 may_garbage_collect = FALSE;
3289
zeertzjq74011dc2024-07-30 19:17:56 +02003290 save_m_keys = vim_strnsave(mp->m_keys, (size_t)mp->m_keylen);
zeertzjq9d997ad2024-07-29 21:10:07 +02003291 save_alt_m_keys = mp->m_alt != NULL
zeertzjq74011dc2024-07-30 19:17:56 +02003292 ? vim_strnsave(mp->m_alt->m_keys,
3293 (size_t)save_alt_m_keylen) : NULL;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003294 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003295
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003296 // The mapping may do anything, but we expect it to take care of
3297 // redrawing. Do put the cursor back where it was.
3298 windgoto(was_screen_row, was_screen_col);
3299 out_flush();
3300
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003301 // If an error was displayed and the expression returns an empty
3302 // string, generate a <Nop> to allow for a redraw.
3303 if (prev_did_emsg != did_emsg
3304 && (map_str == NULL || *map_str == NUL))
3305 {
3306 char_u buf[4];
3307
3308 vim_free(map_str);
3309 buf[0] = K_SPECIAL;
3310 buf[1] = KS_EXTRA;
3311 buf[2] = KE_IGNORE;
3312 buf[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +01003313 map_str = vim_strnsave(buf, 3);
Bram Moolenaar24959102022-05-07 20:01:16 +01003314 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003315 {
3316 // redraw the command below the error
3317 msg_didout = TRUE;
3318 if (msg_row < cmdline_row)
3319 msg_row = cmdline_row;
3320 redrawcmd();
3321 }
3322 }
3323
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003324 vgetc_busy = save_vgetc_busy;
3325 may_garbage_collect = save_may_garbage_collect;
3326 }
3327 else
3328#endif
3329 map_str = mp->m_str;
3330
3331 /*
3332 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003333 * If 'from' field is the same as the start of the 'to' field, don't
3334 * remap the first character (but do allow abbreviations).
3335 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003336 */
3337 if (map_str == NULL)
3338 i = FAIL;
3339 else
3340 {
3341 int noremap;
3342
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003343#ifdef FEAT_EVAL
3344 last_used_map = mp;
3345 last_used_sid = -1;
3346#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003347 if (save_m_noremap != REMAP_YES)
3348 noremap = save_m_noremap;
3349 else if (
3350#ifdef FEAT_EVAL
zeertzjq9d997ad2024-07-29 21:10:07 +02003351 save_m_expr ?
3352 (save_m_keys != NULL
3353 && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
3354 || (save_alt_m_keys != NULL
3355 && STRNCMP(map_str, save_alt_m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003356 (size_t)save_alt_m_keylen) == 0) :
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003357#endif
zeertzjq9d997ad2024-07-29 21:10:07 +02003358 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
3359 || (mp->m_alt != NULL
3360 && STRNCMP(map_str, mp->m_alt->m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003361 (size_t)mp->m_alt->m_keylen) == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003362 noremap = REMAP_SKIP;
zeertzjq9d997ad2024-07-29 21:10:07 +02003363 else
3364 noremap = REMAP_YES;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003365 i = ins_typebuf(map_str, noremap,
3366 0, TRUE, cmd_silent || save_m_silent);
3367#ifdef FEAT_EVAL
3368 if (save_m_expr)
3369 vim_free(map_str);
3370#endif
3371 }
3372#ifdef FEAT_EVAL
3373 vim_free(save_m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003374 vim_free(save_alt_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003375#endif
3376 *keylenp = keylen;
3377 if (i == FAIL)
3378 return map_result_fail;
3379 return map_result_retry;
3380 }
3381
3382 *keylenp = keylen;
3383 return map_result_nomatch;
3384}
3385
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003386/*
3387 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003388 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003389 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003392vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 old_char = c;
3395 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003396 old_mouse_row = mouse_row;
3397 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003398 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399}
3400
3401/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003402 * When peeking and not getting a character, reg_executing cannot be cleared
3403 * yet, so set a flag to clear it later.
3404 */
3405 static void
3406check_end_reg_executing(int advance)
3407{
3408 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3409 || pending_end_reg_executing))
3410 {
3411 if (advance)
3412 {
3413 reg_executing = 0;
3414 pending_end_reg_executing = FALSE;
3415 }
3416 else
3417 pending_end_reg_executing = TRUE;
3418 }
3419
3420}
3421
3422/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003423 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * 1. from the stuffbuffer
3425 * This is used for abbreviated commands like "D" -> "d$".
3426 * Also used to redo a command for ".".
3427 * 2. from the typeahead buffer
3428 * Stores text obtained previously but not used yet.
3429 * Also stores the result of mappings.
3430 * Also used for the ":normal" command.
3431 * 3. from the user
3432 * This may do a blocking wait if "advance" is TRUE.
3433 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003434 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003435 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * KeyTyped is set to TRUE in the case the user typed the key.
3437 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003438 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003439 * Just look whether there is a character available.
3440 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 *
3442 * When "no_mapping" is zero, checks for mappings in the current mode.
3443 * Only returns one byte (of a multi-byte character).
3444 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3445 */
3446 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003447vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003449 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003450 int timedout = FALSE; // waited for more than 'timeoutlen'
3451 // for mapping to complete or
3452 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003453 int mapdepth = 0; // check for recursive mapping
3454 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003457 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#endif
3459 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003461 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
3463 /*
3464 * This function doesn't work very well when called recursively. This may
3465 * happen though, because of:
3466 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3467 * there is a character available, which calls this function. In that
3468 * case we must return NUL, to indicate no character is available.
3469 * 2. A GUI callback function writes to the screen, causing a
3470 * wait_return().
3471 * Using ":normal" can also do this, but it saves the typeahead buffer,
3472 * thus it should be OK. But don't get a key from the user then.
3473 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003474 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return NUL;
3476
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003477 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003482 typebuf_was_empty = FALSE;
3483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 init_typebuf();
3486 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003487 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 do
3489 {
3490/*
3491 * get a character: 1. from the stuffbuffer
3492 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003493 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 c = typeahead_char;
3496 if (advance)
3497 typeahead_char = 0;
3498 }
3499 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003500 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (c != NUL && !got_int)
3502 {
3503 if (advance)
3504 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003505 // KeyTyped = FALSE; When the command that stuffed something
3506 // was typed, behave like the stuffed command was typed.
3507 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 KeyStuffed = TRUE;
3509 }
3510 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003511 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 else
3514 {
3515 /*
3516 * Loop until we either find a matching mapped key, or we
3517 * are sure that it is not a mapped key.
3518 * If a mapped key sequence is found we go back to the start to
3519 * try re-mapping.
3520 */
3521 for (;;)
3522 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003523 long wait_time;
3524 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003525 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003526 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /*
3528 * ui_breakcheck() is slow, don't use it too often when
3529 * inside a mapping. But call it each time for typed
3530 * characters.
3531 */
3532 if (typebuf.tb_maplen)
3533 line_breakcheck();
3534 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003535 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (got_int)
3537 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003538 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003539 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /*
3542 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003543 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 * Otherwise we behave like having gotten a CTRL-C.
3545 * As a result typing CTRL-C in insert mode will
3546 * really insert a CTRL-C.
3547 */
3548 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003549 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 c = ESC;
3551 else
3552 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003553 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003555 if (advance)
3556 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003557 // Also record this character, it might be needed to
3558 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003559 *typebuf.tb_buf = c;
3560 gotchars(typebuf.tb_buf, 1);
3561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 cmd_silent = FALSE;
3563
3564 break;
3565 }
3566 else if (typebuf.tb_len > 0)
3567 {
3568 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003569 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003571 map_result_T result = handle_mapping(
3572 &keylen, &timedout, &mapdepth);
3573
3574 if (result == map_result_retry)
3575 // try mapping again
3576 continue;
3577 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003578 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003579 // failed, use the outer loop
3580 c = -1;
3581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003583 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585/*
3586 * get a character: 2. from the typeahead buffer
3587 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003588 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003589 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003590 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003591 cmd_silent = (typebuf.tb_silent > 0);
3592 if (typebuf.tb_maplen > 0)
3593 KeyTyped = FALSE;
3594 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003595 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003596 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003597 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003598 gotchars(typebuf.tb_buf
3599 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003600 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003601 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003602 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003603 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003604 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003607 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609
3610/*
3611 * get a character: 3. from the user - handle <Esc> in Insert mode
3612 */
3613 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003614 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003616 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 * typing an <ESC>. If we get something after all, we may
3618 * have to redisplay the mode. That the cursor is in the wrong
3619 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003620 * Do not do this if the kitty keyboard protocol is used, every
3621 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
3623 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 new_wcol = curwin->w_wcol;
3625 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if ( advance
3627 && typebuf.tb_len == 1
3628 && typebuf.tb_buf[typebuf.tb_off] == ESC
3629 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003630 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003633 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003634 && (p_timeout
3635 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003637 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003639 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 char_u *ptr;
3641
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003642 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 unshowmode(TRUE);
3645 mode_deleted = TRUE;
3646 }
3647#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003648 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003649 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 int save_State;
3652
3653 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003654 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 gui_update_cursor(TRUE, FALSE);
3656 State = save_State;
3657 shape_changed = TRUE;
3658 }
3659#endif
3660 validate_cursor();
3661 old_wcol = curwin->w_wcol;
3662 old_wrow = curwin->w_wrow;
3663
Bram Moolenaar30613902019-12-01 22:11:18 +01003664 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (curwin->w_cursor.col != 0)
3666 {
3667 if (curwin->w_wcol > 0)
3668 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003669 // After auto-indenting and no text is following,
3670 // we are expecting to truncate the trailing
3671 // white-space, so find the last non-white
3672 // character -- webb
3673 if (did_ai && *skipwhite(ml_get_curline()
3674 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003676 chartabsize_T cts;
3677
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003678 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003680 init_chartabsize_arg(&cts, curwin,
3681 curwin->w_cursor.lnum, 0, ptr, ptr);
3682 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003684 if (!VIM_ISWHITE(*cts.cts_ptr))
3685 curwin->w_wcol = cts.cts_vcol;
3686 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003688 cts.cts_ptr +=
3689 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003691 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003693 clear_chartabsize_arg(&cts);
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003696 + curwin->w_wcol / curwin->w_width;
3697 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003699 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 else
3702 {
3703 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 }
3707 else if (curwin->w_p_wrap && curwin->w_wrow)
3708 {
3709 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003710 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3714 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003715 // Correct when the cursor is on the right halve
3716 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 ptr = ml_get_curline();
3718 col -= (*mb_head_off)(ptr, ptr + col);
3719 if ((*mb_ptr2cells)(ptr + col) > 1)
3720 --curwin->w_wcol;
3721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 setcursor();
3724 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 new_wcol = curwin->w_wcol;
3726 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 curwin->w_wcol = old_wcol;
3728 curwin->w_wrow = old_wrow;
3729 }
3730 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003731 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003732
Bram Moolenaar30613902019-12-01 22:11:18 +01003733 // Allow mapping for just typed characters. When we get here c
3734 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003735 for (n = 1; n <= c; ++n)
3736 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 typebuf.tb_len += c;
3738
Bram Moolenaar30613902019-12-01 22:11:18 +01003739 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3741 {
3742 timedout = TRUE;
3743 continue;
3744 }
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (ex_normal_busy > 0)
3747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar30613902019-12-01 22:11:18 +01003750 // No typeahead left and inside ":normal". Must return
3751 // something to avoid getting stuck. When an incomplete
3752 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (typebuf.tb_len > 0)
3754 {
3755 timedout = TRUE;
3756 continue;
3757 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003758
Bram Moolenaar30613902019-12-01 22:11:18 +01003759 // When 'insertmode' is set, ESC just beeps in Insert
3760 // mode. Use CTRL-L to make edit() return.
3761 // For the command line only CTRL-C always breaks it.
3762 // For the cmdline window: Alternate between ESC and
3763 // CTRL-C: ESC for most situations and CTRL-C to close the
3764 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003765 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003767#ifdef FEAT_TERMINAL
3768 else if (terminal_is_active())
3769 c = K_CANCEL;
3770#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003771 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003772 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 c = Ctrl_C;
3774 else
3775 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003777 // set a flag to indicate this wasn't a normal char
3778 if (advance)
3779 typebuf_was_empty = TRUE;
3780
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003781 // return from main_loop()
3782 if (pending_exmode_active)
3783 exmode_active = EXMODE_NORMAL;
3784
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003785 // no chars to block abbreviation for
3786 typebuf.tb_no_abbr_cnt = 0;
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 break;
3789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791/*
3792 * get a character: 3. from the user - update display
3793 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003794 // In insert mode a screen update is skipped when characters
3795 // are still available. But when those available characters
3796 // are part of a mapping, and we are going to do a blocking
3797 // wait here. Need to update the screen to display the
3798 // changed text so far. Also for when 'lazyredraw' is set and
3799 // redrawing was postponed because there was something in the
3800 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003801 if (((State & MODE_INSERT) != 0 || p_lz)
3802 && (State & MODE_CMDLINE) == 0
3803 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003806 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808
3809 /*
3810 * If we have a partial match (and are going to wait for more
3811 * input from the user), show the partially matched characters
3812 * to the user with showcmd.
3813 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003814 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003815 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 if (typebuf.tb_len > 0 && advance && !exmode_active)
3817 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003818 if (((State & (MODE_NORMAL | MODE_INSERT))
3819 || State == MODE_LANGMAP)
3820 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003822 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003823 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3825 + typebuf.tb_len - 1) == 1)
3826 {
3827 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3828 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003829 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003830 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003832 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 old_wcol = curwin->w_wcol;
3834 old_wrow = curwin->w_wrow;
3835 curwin->w_wcol = new_wcol;
3836 curwin->w_wrow = new_wrow;
3837 push_showcmd();
3838 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003839 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3840 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003841 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003842 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 curwin->w_wcol = old_wcol;
3844 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003847 // This looks nice when typing a dead character map.
3848 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003849 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003850 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3852 && cmdline_star == 0
3853#endif
3854 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3855 + typebuf.tb_len - 1) == 1)
3856 {
3857 putcmdline(typebuf.tb_buf[typebuf.tb_off
3858 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003859 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861 }
3862
3863/*
3864 * get a character: 3. from the user - get it
3865 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003866 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003867 // timedout may have been set if a mapping with empty RHS
3868 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003869 timedout = FALSE;
3870
Bram Moolenaar652de232019-04-04 20:13:09 +02003871 if (advance)
3872 {
3873 if (typebuf.tb_len == 0
3874 || !(p_timeout
3875 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3876 // blocking wait
3877 wait_time = -1L;
3878 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3879 wait_time = p_ttm;
3880 else
3881 wait_time = p_tm;
3882 }
3883 else
3884 wait_time = 0;
3885
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003886 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3888 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003889 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaareda35f72019-08-03 14:59:44 +02003891 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003893 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003895 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003897 if ((State & MODE_CMDLINE)
3898 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003900 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003901 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903
3904 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003905 continue; // end of input script reached
3906 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 if (!advance)
3909 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003910 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
3912 timedout = TRUE;
3913 continue;
3914 }
3915 }
3916 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003917 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 while (typebuf.tb_buf[typebuf.tb_off
3919 + typebuf.tb_len] != NUL)
3920 typebuf.tb_noremap[typebuf.tb_off
3921 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003922#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003923 // Get IM status right after getting keys, not after the
3924 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 vgetc_im_active = im_get_status();
3926#endif
3927 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003928 } // for (;;)
3929 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaar30613902019-12-01 22:11:18 +01003931 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003932 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 /*
3935 * The "INSERT" message is taken care of here:
3936 * if we return an ESC to exit insert mode, the message is deleted
3937 * if we don't return an ESC but deleted the message before, redisplay it
3938 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003939 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003941 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
3943 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003944 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else
3946 unshowmode(FALSE);
3947 }
3948 else if (c != ESC && mode_deleted)
3949 {
3950 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003951 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 else
3953 showmode();
3954 }
3955 }
3956#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003957 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003958 if (gui.in_use && shape_changed)
3959 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003961 if (timedout && c == ESC)
3962 {
zeertzjqbf321802024-01-28 19:03:00 +01003963 // When recording there will be no timeout. Add an <Ignore> after the
3964 // ESC to avoid that it forms a key code with following characters.
3965 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003968 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 return c;
3971}
3972
3973/*
3974 * inchar() - get one character from
3975 * 1. a scriptfile
3976 * 2. the keyboard
3977 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003978 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 * NUL terminated (buffer length must be 'maxlen' + 1).
3980 * Minimum for "maxlen" is 3!!!!
3981 *
3982 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3983 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3984 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3985 * otherwise.
3986 *
3987 * If we got an interrupt all input is read until none is available.
3988 *
3989 * If wait_time == 0 there is no waiting for the char.
3990 * If wait_time == n we wait for n msec for a character to arrive.
3991 * If wait_time == -1 we wait forever for a character to arrive.
3992 *
3993 * Return the number of obtained characters.
3994 * Return -1 when end of input script reached.
3995 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003996 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003997inchar(
3998 char_u *buf,
3999 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004000 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
Bram Moolenaar30613902019-12-01 22:11:18 +01004002 int len = 0; // init for GCC
4003 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004005 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaar30613902019-12-01 22:11:18 +01004007 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004010 out_flush_cursor(FALSE, FALSE);
4011#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
4012 if (gui.in_use && postponed_mouseshape)
4013 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#endif
4015 }
4016
4017 /*
4018 * Don't reset these when at the hit-return prompt, otherwise a endless
4019 * recursive loop may result (write error in swapfile, hit-return, timeout
4020 * on char wait, flush swapfile, write error....).
4021 */
Bram Moolenaar24959102022-05-07 20:01:16 +01004022 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004024 did_outofmem_msg = FALSE; // display out of memory message (again)
4025 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004027 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004030 * Get a character from a script file if there is one.
4031 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
4033 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004034 while (scriptin[curscript] != NULL && script_char < 0
4035#ifdef FEAT_EVAL
4036 && !ignore_script
4037#endif
4038 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02004040#ifdef MESSAGE_QUEUE
4041 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00004042#endif
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
4045 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004046 // Reached EOF.
4047 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
4048 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 closescript();
4050 /*
4051 * When reading script file is interrupted, return an ESC to get
4052 * back to normal mode.
4053 * Otherwise return -1, because typebuf.tb_buf[] has changed.
4054 */
4055 if (got_int)
4056 retesc = TRUE;
4057 else
4058 return -1;
4059 }
4060 else
4061 {
4062 buf[0] = script_char;
4063 len = 1;
4064 }
4065 }
4066
Bram Moolenaar30613902019-12-01 22:11:18 +01004067 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 /*
4070 * If we got an interrupt, skip all previously typed characters and
4071 * return TRUE if quit reading script file.
4072 * Stop reading typeahead when a single CTRL-C was read,
4073 * fill_input_buf() returns this when not able to read from stdin.
4074 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
4075 * and buf may be pointing inside typebuf.tb_buf[].
4076 */
4077 if (got_int)
4078 {
kylo252ae6f1d82022-02-16 19:24:07 +00004079#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 char_u dum[DUM_LEN + 1];
4081
4082 for (;;)
4083 {
4084 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01004085 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 break;
4087 }
4088 return retesc;
4089 }
4090
4091 /*
4092 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004093 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004095 if (wait_time == -1L || wait_time > 10L)
4096 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /*
4099 * Fill up to a third of the buffer, because each character may be
4100 * tripled below.
4101 */
4102 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
4103 }
4104
Bram Moolenaar30613902019-12-01 22:11:18 +01004105 // If the typebuf was changed further down, it is like nothing was added by
4106 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (typebuf_changed(tb_change_cnt))
4108 return 0;
4109
Bram Moolenaar30613902019-12-01 22:11:18 +01004110 // Note the change in the typeahead buffer, this matters for when
4111 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4112 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004113 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4114 typebuf.tb_change_cnt = 1;
4115
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004116 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
4119/*
4120 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004121 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Returns the new length.
4123 */
4124 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004125fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 int i;
4128 char_u *p = buf;
4129
4130 /*
4131 * Two characters are special: NUL and K_SPECIAL.
4132 * When compiled With the GUI CSI is also special.
4133 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4134 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4135 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 */
4137 for (i = len; --i >= 0; ++p)
4138 {
4139#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004140 // When the GUI is used any character can come after a CSI, don't
4141 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 if (gui.in_use && p[0] == CSI && i >= 2)
4143 {
4144 p += 2;
4145 i -= 2;
4146 }
zeertzjq646bb722022-02-16 17:51:47 +00004147# ifndef MSWIN
4148 // When not on MS-Windows and the GUI is not used CSI needs to be
4149 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 else if (!gui.in_use && p[0] == CSI)
4151 {
4152 mch_memmove(p + 3, p + 1, (size_t)i);
4153 *p++ = K_SPECIAL;
4154 *p++ = KS_EXTRA;
4155 *p = (int)KE_CSI;
4156 len += 2;
4157 }
zeertzjq646bb722022-02-16 17:51:47 +00004158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 else
4160#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004161 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004162 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004163 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004164#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004165 // Win32 console passes modifiers
4166 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004167# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004168 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004169# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004170 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#endif
4172 ))
4173 {
4174 mch_memmove(p + 3, p + 1, (size_t)i);
4175 p[2] = K_THIRD(p[0]);
4176 p[1] = K_SECOND(p[0]);
4177 p[0] = K_SPECIAL;
4178 p += 2;
4179 len += 2;
4180 }
4181 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004182 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return len;
4184}
4185
4186#if defined(USE_INPUT_BUF) || defined(PROTO)
4187/*
4188 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4189 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004190 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004191 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
4193 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004194input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004197# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4198 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199# endif
4200 );
4201}
4202#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004203
4204/*
4205 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4206 * typeahead.
4207 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004208 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004209getcmdkeycmd(
4210 int promptc UNUSED,
4211 void *cookie UNUSED,
4212 int indent UNUSED,
4213 getline_opt_T do_concat UNUSED)
4214{
4215 garray_T line_ga;
4216 int c1 = -1;
4217 int c2;
4218 int cmod = 0;
4219 int aborted = FALSE;
4220
4221 ga_init2(&line_ga, 1, 32);
4222
4223 // no mapping for these characters
4224 no_mapping++;
4225
4226 got_int = FALSE;
4227 while (c1 != NUL && !aborted)
4228 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004229 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004230 {
4231 aborted = TRUE;
4232 break;
4233 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004234
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004235 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004236 {
4237 // incomplete <Cmd> is an error, because there is not much the user
4238 // could do in this state.
4239 emsg(_(e_cmd_mapping_must_end_with_cr));
4240 aborted = TRUE;
4241 break;
4242 }
4243
4244 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004245 c1 = vgetorpeek(TRUE);
4246
Bram Moolenaar957cf672020-11-12 14:21:06 +01004247 // Get two extra bytes for special keys
4248 if (c1 == K_SPECIAL)
4249 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004250 c1 = vgetorpeek(TRUE);
4251 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004252 if (c1 == KS_MODIFIER)
4253 {
4254 cmod = c2;
4255 continue;
4256 }
4257 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004258
4259 // K_ESC is used to avoid ambiguity with the single Esc character
4260 // that might be the start of an escape sequence. Convert it back
4261 // to a single Esc here.
4262 if (c1 == K_ESC)
4263 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004264 }
4265
4266 if (got_int)
4267 aborted = TRUE;
4268 else if (c1 == '\r' || c1 == '\n')
4269 c1 = NUL; // end the line
4270 else if (c1 == ESC)
4271 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004272 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004273 {
4274 // give a nicer error message for this special case
4275 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4276 aborted = TRUE;
4277 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004278 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004279 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004280 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004281 }
4282 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004283 {
4284 if (cmod != 0)
4285 {
4286 ga_append(&line_ga, K_SPECIAL);
4287 ga_append(&line_ga, KS_MODIFIER);
4288 ga_append(&line_ga, cmod);
4289 }
4290 if (IS_SPECIAL(c1))
4291 {
4292 ga_append(&line_ga, K_SPECIAL);
4293 ga_append(&line_ga, K_SECOND(c1));
4294 ga_append(&line_ga, K_THIRD(c1));
4295 }
4296 else
4297 ga_append(&line_ga, c1);
4298 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004299
4300 cmod = 0;
4301 }
4302
4303 no_mapping--;
4304
4305 if (aborted)
4306 ga_clear(&line_ga);
4307
4308 return (char_u *)line_ga.ga_data;
4309}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004310
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004311#if defined(FEAT_EVAL) || defined(PROTO)
4312/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004313 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4314 * is set when redo'ing.
4315 * Put this SID in the redo buffer, so that "." will use the same script
4316 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004317 */
4318 void
4319may_add_last_used_map_to_redobuff(void)
4320{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004321 char_u buf[3 + 20];
John Marriotte7a1bbf2024-11-11 20:40:33 +01004322 int buflen;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004323 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004324
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004325 if (last_used_map != NULL)
4326 sid = last_used_map->m_script_ctx.sc_sid;
4327 if (sid < 0)
4328 sid = last_used_sid;
4329
4330 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004331 return;
4332
4333 // <K_SID>{nr};
4334 buf[0] = K_SPECIAL;
4335 buf[1] = KS_EXTRA;
4336 buf[2] = KE_SID;
John Marriotte7a1bbf2024-11-11 20:40:33 +01004337 buflen = 3;
4338
4339 buflen += vim_snprintf((char *)buf + 3, 20, "%d;", sid);
4340 add_buff(&redobuff, buf, (long)buflen);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004341}
4342#endif
4343
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004344 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004345do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004346{
4347 int res;
4348#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004349 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004350
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004351 if (key == K_SCRIPT_COMMAND
4352 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004353 {
4354 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004355 if (last_used_map != NULL)
4356 current_sctx = last_used_map->m_script_ctx;
4357 else
4358 {
4359 current_sctx.sc_sid = last_used_sid;
4360 current_sctx.sc_lnum = 0;
4361 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4362 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004363 }
4364#endif
4365
4366 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4367
4368#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004369 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004370 current_sctx = save_current_sctx;
4371#endif
4372
4373 return res;
4374}
4375
4376#if defined(FEAT_EVAL) || defined(PROTO)
4377 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004378reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004379{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004380 if (last_used_map != mp)
4381 return;
4382
4383 last_used_map = NULL;
4384 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004385}
4386#endif