blob: 014e5759fe1b540662f2055e9d6b69d053eb6098 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
John Marriotte7a1bbf2024-11-11 20:40:33 +010039static buffheader_T redobuff = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
40static buffheader_T old_redobuff = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
41static buffheader_T recordbuff = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Shougo Matsushitafcc1b572024-07-17 20:25:22 +020045#ifdef FEAT_EVAL
46static char_u typedchars[MAXMAPLEN + 1] = { NUL }; // typed chars before map
47static int typedchars_pos = 0;
48#endif
49
Bram Moolenaar071d4272004-06-13 20:20:40 +000050/*
zeertzjq30b6d612023-05-07 17:39:23 +010051 * When block_redo is TRUE the redo buffer will not be changed.
52 * Used by edit() to repeat insertions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 */
54static int block_redo = FALSE;
55
Bram Moolenaar459fd782019-10-13 16:43:39 +020056static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020059 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 *
61 * typebuf.tb_buf[] contains all characters that are not consumed yet.
62 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
64 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
65 * The head of the buffer may contain the result of mappings, abbreviations
66 * and @a commands. The length of this part is typebuf.tb_maplen.
67 * typebuf.tb_silent is the part where <silent> applies.
68 * After the head are characters that come from the terminal.
69 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
70 * should not be considered for abbreviations.
71 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
72 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
73 * contains RM_NONE for the characters that are not to be remapped.
74 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
75 * (typebuf has been put in globals.h, because check_termcode() needs it).
76 */
Bram Moolenaar30613902019-12-01 22:11:18 +010077#define RM_YES 0 // tb_noremap: remap
78#define RM_NONE 1 // tb_noremap: don't remap
79#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
80#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar30613902019-12-01 22:11:18 +010082// typebuf.tb_buf has three parts: room in front (for result of mappings), the
83// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000084// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010086static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
87static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
Mike Williamsaca8f552024-04-07 18:26:22 +020089static size_t last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
John Marriottd3c4b7e2025-02-25 20:56:38 +010091static size_t last_get_inserted_len = 0; // length of the string returned from the
92 // last call to get_inserted()
93
Bram Moolenaare32c3c42022-01-15 18:26:04 +000094#ifdef FEAT_EVAL
95mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010096int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000097#endif
98
Bram Moolenaard25c16e2016-01-29 22:13:30 +010099static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100101static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200102static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200104static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100105static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +0200106static int inchar(char_u *buf, int maxlen, long wait_time);
Shougo Matsushita83678842024-07-11 22:05:12 +0200107#ifdef FEAT_EVAL
108static int do_key_input_pre(int c);
109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111/*
112 * Free and clear a buffer.
113 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200114 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100115free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100117 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
Bram Moolenaar285e3352018-04-18 23:01:13 +0200119 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 {
121 np = p->b_next;
122 vim_free(p);
123 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200124 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000125 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126}
127
128/*
129 * Return the contents of a buffer as a single string.
130 * K_SPECIAL and CSI in the returned string are escaped.
131 */
132 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100133get_buffcont(
134 buffheader_T *buffer,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100135 int dozero, // count == zero is not an error
136 size_t *len) // the length of the returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137{
138 long_u count = 0;
139 char_u *p = NULL;
140 char_u *p2;
141 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200142 buffblock_T *bp;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100143 size_t i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar30613902019-12-01 22:11:18 +0100145 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200146 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100147 count += (long_u)bp->b_strlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100149 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 {
151 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200152 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 for (str = bp->b_str; *str; )
154 *p2++ = *str++;
155 *p2 = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100156 i = (size_t)(p2 - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100158
159 if (len != NULL)
160 *len = i;
161
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100162 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163}
164
165/*
166 * Return the contents of the record buffer as a single string
167 * and clear the record buffer.
168 * K_SPECIAL and CSI in the returned string are escaped.
169 */
170 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100171get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
173 char_u *p;
John Marriott18bacc82025-02-26 19:14:06 +0100174 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
John Marriott18bacc82025-02-26 19:14:06 +0100176 p = get_buffcont(&recordbuff, TRUE, &len);
John Marriottd3c4b7e2025-02-25 20:56:38 +0100177 if (p == NULL)
178 return NULL;
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 free_buff(&recordbuff);
181
182 /*
183 * Remove the characters that were added the last time, these must be the
184 * (possibly mapped) characters that stopped the recording.
185 */
John Marriott18bacc82025-02-26 19:14:06 +0100186 if (len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
John Marriott18bacc82025-02-26 19:14:06 +0100188 len -= last_recorded_len;
189 p[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191
192 /*
193 * When stopping recording from Insert mode with CTRL-O q, also remove the
194 * CTRL-O.
195 */
John Marriott18bacc82025-02-26 19:14:06 +0100196 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
197 p[len - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 return (p);
200}
201
202/*
203 * Return the contents of the redo buffer as a single string.
204 * K_SPECIAL and CSI in the returned string are escaped.
205 */
206 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100207get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
John Marriottd3c4b7e2025-02-25 20:56:38 +0100209 return get_buffcont(&redobuff, FALSE, &last_get_inserted_len);
210}
211
212/*
213 * Return the length of string returned from the last call of get_inserted().
214 */
215 size_t
216get_inserted_len(void)
217{
218 return last_get_inserted_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219}
220
221/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000222 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 * K_SPECIAL and CSI should have been escaped already.
224 */
225 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100226add_buff(
227 buffheader_T *buf,
228 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100229 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (slen < 0)
232 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100233 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 return;
235
Bram Moolenaar30613902019-12-01 22:11:18 +0100236 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200238 buf->bh_curr = &(buf->bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100239 buf->bh_create_newblock = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100241 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100243 iemsg(e_add_to_internal_buffer_that_was_already_read_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 return;
245 }
246 else if (buf->bh_index != 0)
John Marriotte7a1bbf2024-11-11 20:40:33 +0100247 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200248 mch_memmove(buf->bh_first.b_next->b_str,
249 buf->bh_first.b_next->b_str + buf->bh_index,
John Marriotte7a1bbf2024-11-11 20:40:33 +0100250 (buf->bh_first.b_next->b_strlen - buf->bh_index) + 1);
251 buf->bh_first.b_next->b_strlen -= buf->bh_index;
252 buf->bh_space += buf->bh_index;
253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 buf->bh_index = 0;
255
John Marriotte7a1bbf2024-11-11 20:40:33 +0100256 if (!buf->bh_create_newblock && buf->bh_space >= (int)slen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100258 vim_strncpy(buf->bh_curr->b_str + buf->bh_curr->b_strlen, s, (size_t)slen);
259 buf->bh_curr->b_strlen += slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 buf->bh_space -= slen;
261 }
262 else
263 {
John Marriotte7a1bbf2024-11-11 20:40:33 +0100264 long_u len;
265 buffblock_T *p;
266
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (slen < MINIMAL_SIZE)
268 len = MINIMAL_SIZE;
269 else
270 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200271 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100273 return; // no space, just forget it
Bram Moolenaarb6356332005-07-18 21:40:44 +0000274 vim_strncpy(p->b_str, s, (size_t)slen);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100275 p->b_strlen = slen;
276 buf->bh_space = (int)(len - slen);
277 buf->bh_create_newblock = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
Bram Moolenaar285e3352018-04-18 23:01:13 +0200279 p->b_next = buf->bh_curr->b_next;
280 buf->bh_curr->b_next = p;
281 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283}
284
285/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000286 * Delete "slen" bytes from the end of "buf".
287 * Only works when it was just added.
288 */
289 static void
290delete_buff_tail(buffheader_T *buf, int slen)
291{
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000292 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000293 return; // nothing to delete
John Marriotte7a1bbf2024-11-11 20:40:33 +0100294 if (buf->bh_curr->b_strlen < (size_t)slen)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000295 return;
296
John Marriotte7a1bbf2024-11-11 20:40:33 +0100297 buf->bh_curr->b_str[buf->bh_curr->b_strlen - (size_t)slen] = NUL;
298 buf->bh_curr->b_strlen -= slen;
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000299 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000300}
301
302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 * Add number "n" to buffer "buf".
304 */
305 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100306add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307{
308 char_u number[32];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100309 int numberlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
John Marriotte7a1bbf2024-11-11 20:40:33 +0100311 numberlen = vim_snprintf((char *)number, sizeof(number), "%ld", n);
312 add_buff(buf, number, (long)numberlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313}
314
315/*
316 * Add character 'c' to buffer "buf".
317 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
318 */
319 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100320add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 char_u bytes[MB_MAXBYTES + 1];
323 int len;
324 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 char_u temp[4];
John Marriotte7a1bbf2024-11-11 20:40:33 +0100326 long templen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (IS_SPECIAL(c))
329 len = 1;
330 else
331 len = (*mb_char2bytes)(c, bytes);
332 for (i = 0; i < len; ++i)
333 {
334 if (!IS_SPECIAL(c))
335 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
338 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100339 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 temp[0] = K_SPECIAL;
341 temp[1] = K_SECOND(c);
342 temp[2] = K_THIRD(c);
343 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100344 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 }
346#ifdef FEAT_GUI
347 else if (c == CSI)
348 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100349 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 temp[0] = CSI;
351 temp[1] = KS_EXTRA;
352 temp[2] = (int)KE_CSI;
353 temp[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100354 templen = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
356#endif
357 else
358 {
359 temp[0] = c;
360 temp[1] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100361 templen = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
John Marriotte7a1bbf2024-11-11 20:40:33 +0100363 add_buff(buf, temp, templen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365}
366
Bram Moolenaar30613902019-12-01 22:11:18 +0100367// First read ahead buffer. Used for translated commands.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100368static buffheader_T readbuf1 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369
Bram Moolenaar30613902019-12-01 22:11:18 +0100370// Second read ahead buffer. Used for redo.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100371static buffheader_T readbuf2 = {{NULL, 0, {NUL}}, NULL, 0, 0, FALSE};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100374 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
375 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 * If advance == TRUE go to the next char.
377 * No translation is done K_SPECIAL and CSI are escaped.
378 */
379 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100380read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100382 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100384 c = read_readbuf(&readbuf1, advance);
385 if (c == NUL)
386 c = read_readbuf(&readbuf2, advance);
387 return c;
388}
389
390 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100391read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100392{
393 char_u c;
394 buffblock_T *curr;
395
Bram Moolenaar30613902019-12-01 22:11:18 +0100396 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 return NUL;
398
Bram Moolenaar285e3352018-04-18 23:01:13 +0200399 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100400 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402 if (advance)
403 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100404 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200406 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100408 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410 }
411 return c;
412}
413
414/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100415 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 */
417 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100418start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200420 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200422 readbuf1.bh_curr = &(readbuf1.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100423 readbuf1.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100424 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200425 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100426 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200427 readbuf2.bh_curr = &(readbuf2.bh_first);
John Marriotte7a1bbf2024-11-11 20:40:33 +0100428 readbuf2.bh_create_newblock = TRUE; // force a new block to be created (see add_buff())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 }
430}
431
432/*
433 * Return TRUE if the stuff buffer is empty.
434 */
435 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100436stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200438 return (readbuf1.bh_first.b_next == NULL
439 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100440}
441
Bram Moolenaar113e1072019-01-20 15:30:40 +0100442#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100443/*
444 * Return TRUE if readbuf1 is empty. There may still be redo characters in
445 * redbuf2.
446 */
447 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100448readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100449{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200450 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
454/*
455 * Set a typeahead character that won't be flushed.
456 */
457 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100458typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 typeahead_char = c;
461}
462
463/*
464 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100465 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 * flush all typeahead characters (used when interrupted by a CTRL-C).
467 */
468 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200469flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 init_typebuf();
472
473 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100474 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 ;
476
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200477 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {
Christian Brabandt322ba912024-08-25 21:33:03 +0200479 // remove mapped characters at the start only,
480 // but only when enough space left in typebuf
481 if (typebuf.tb_off + typebuf.tb_maplen >= typebuf.tb_buflen)
482 {
483 typebuf.tb_off = MAXMAPLEN;
484 typebuf.tb_len = 0;
485 }
486 else
487 {
488 typebuf.tb_off += typebuf.tb_maplen;
489 typebuf.tb_len -= typebuf.tb_maplen;
490 }
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100491#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
492 if (typebuf.tb_len == 0)
493 typebuf_was_filled = FALSE;
494#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200495 }
496 else
497 {
498 // remove typeahead
499 if (flush_typeahead == FLUSH_INPUT)
500 // We have to get all characters, because we may delete the first
501 // part of an escape sequence. In an xterm we get one char at a
502 // time and we have to get them all.
503 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
504 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 typebuf.tb_off = MAXMAPLEN;
506 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200507#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100508 // Reset the flag that text received from a client or from feedkeys()
509 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200510 typebuf_was_filled = FALSE;
511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 typebuf.tb_maplen = 0;
514 typebuf.tb_silent = 0;
515 cmd_silent = FALSE;
516 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200517 if (++typebuf.tb_change_cnt == 0)
518 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
521/*
522 * The previous contents of the redo buffer is kept in old_redobuffer.
523 * This is used for the CTRL-O <.> command in insert mode.
524 */
525 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100526ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000528 if (block_redo)
529 return;
530
531 free_buff(&old_redobuff);
532 old_redobuff = redobuff;
533 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534}
535
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100536/*
537 * Discard the contents of the redo buffer and restore the previous redo
538 * buffer.
539 */
540 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100541CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100542{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000543 if (block_redo)
544 return;
545
546 free_buff(&redobuff);
547 redobuff = old_redobuff;
548 old_redobuff.bh_first.b_next = NULL;
549 start_stuff();
550 while (read_readbuffers(TRUE) != NUL)
551 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100552}
553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554/*
555 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
556 * Used before executing autocommands and user functions.
557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200559saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 char_u *s;
John Marriotte7a1bbf2024-11-11 20:40:33 +0100562 size_t slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200564 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200565 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200566 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200567 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaar30613902019-12-01 22:11:18 +0100569 // Make a copy, so that ":normal ." in a function works.
John Marriotte7a1bbf2024-11-11 20:40:33 +0100570 s = get_buffcont(&save_redo->sr_redobuff, FALSE, &slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000571 if (s == NULL)
572 return;
573
John Marriotte7a1bbf2024-11-11 20:40:33 +0100574 add_buff(&redobuff, s, (long)slen);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000575 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576}
577
578/*
579 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
580 * Used after executing autocommands and user functions.
581 */
582 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200583restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200585 free_buff(&redobuff);
586 redobuff = save_redo->sr_redobuff;
587 free_buff(&old_redobuff);
588 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591/*
592 * Append "s" to the redo buffer.
593 * K_SPECIAL and CSI should already have been escaped.
594 */
595 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100596AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 if (!block_redo)
599 add_buff(&redobuff, s, -1L);
600}
601
602/*
603 * Append to Redo buffer literally, escaping special characters with CTRL-V.
604 * K_SPECIAL and CSI are escaped as well.
605 */
606 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100607AppendToRedobuffLit(
608 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100609 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000611 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 int c;
613 char_u *start;
614
615 if (block_redo)
616 return;
617
Bram Moolenaarebefac62005-12-28 22:39:57 +0000618 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100620 // Put a string of normal characters in the redo buffer (that's
621 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000623 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 ++s;
625
Bram Moolenaar30613902019-12-01 22:11:18 +0100626 // Don't put '0' or '^' as last character, just in case a CTRL-D is
627 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
629 --s;
630 if (s > start)
631 add_buff(&redobuff, start, (long)(s - start));
632
Bram Moolenaarebefac62005-12-28 22:39:57 +0000633 if (*s == NUL || (len >= 0 && s - str >= len))
634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
Bram Moolenaar30613902019-12-01 22:11:18 +0100636 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000637 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100638 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000639 c = mb_cptr2char_adv(&s);
640 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000641 c = *s++;
642 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
643 add_char_buff(&redobuff, Ctrl_V);
644
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000645 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000646 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000647 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000648 else
649 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
651}
652
653/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100654 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
655 * and escaping other K_SPECIAL and CSI bytes.
656 */
657 void
658AppendToRedobuffSpec(char_u *s)
659{
zeertzjq30b6d612023-05-07 17:39:23 +0100660 if (block_redo)
661 return;
662
zeertzjq3ab3a862023-05-06 16:22:04 +0100663 while (*s != NUL)
664 {
665 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
666 {
zeertzjq30b6d612023-05-07 17:39:23 +0100667 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100668 add_buff(&redobuff, s, 3L);
669 s += 3;
670 }
671 else
672 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
673 }
674}
675
676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Append a character to the redo buffer.
678 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
679 */
680 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100681AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 if (!block_redo)
684 add_char_buff(&redobuff, c);
685}
686
687/*
688 * Append a number to the redo buffer.
689 */
690 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100691AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 if (!block_redo)
694 add_num_buff(&redobuff, n);
695}
696
697/*
698 * Append string "s" to the stuff buffer.
699 * CSI and K_SPECIAL must already have been escaped.
700 */
701 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100702stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100704 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705}
706
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200707/*
708 * Append string "s" to the redo stuff buffer.
709 * CSI and K_SPECIAL must already have been escaped.
710 */
711 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100712stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200713{
714 add_buff(&readbuf2, s, -1L);
715}
716
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200717 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100718stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721}
722
723#if defined(FEAT_EVAL) || defined(PROTO)
724/*
725 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
726 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200727 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100730stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200732 int c;
733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 while (*s != NUL)
735 {
736 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
737 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100738 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 stuffReadbuffLen(s, 3L);
740 s += 3;
741 }
742 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200743 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100744 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200745 if (c == CAR || c == NL || c == ESC)
746 c = ' ';
747 stuffcharReadbuff(c);
748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
750}
751#endif
752
753/*
754 * Append a character to the stuff buffer.
755 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
756 */
757 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100758stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100760 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
763/*
764 * Append a number to the stuff buffer.
765 */
766 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100767stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100769 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
772/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200773 * Stuff a string into the typeahead buffer, such that edit() will insert it
774 * literally ("literally" TRUE) or interpret is as typed characters.
775 */
776 void
777stuffescaped(char_u *arg, int literally)
778{
779 int c;
780 char_u *start;
781
782 while (*arg != NUL)
783 {
784 // Stuff a sequence of normal ASCII characters, that's fast. Also
785 // stuff K_SPECIAL to get the effect of a special key when "literally"
786 // is TRUE.
787 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000788 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200789 || (*arg == K_SPECIAL && !literally))
790 ++arg;
791 if (arg > start)
792 stuffReadbuffLen(start, (long)(arg - start));
793
794 // stuff a single special character
795 if (*arg != NUL)
796 {
797 if (has_mbyte)
798 c = mb_cptr2char_adv(&arg);
799 else
800 c = *arg++;
801 if (literally && ((c < ' ' && c != TAB) || c == DEL))
802 stuffcharReadbuff(Ctrl_V);
803 stuffcharReadbuff(c);
804 }
805 }
806}
807
808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
810 * multibyte characters.
811 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100812 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
813 * otherwise.
814 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 */
816 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100817read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100819 static buffblock_T *bp;
820 static char_u *p;
821 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100822 int n;
823 char_u buf[MB_MAXBYTES + 1];
824 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 if (init)
827 {
828 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200829 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200831 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (bp == NULL)
833 return FAIL;
834 p = bp->b_str;
835 return OK;
836 }
837 if ((c = *p) != NUL)
838 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100839 // Reverse the conversion done by add_char_buff()
840 // For a multi-byte character get all the bytes and return the
841 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
843 n = MB_BYTE2LEN_CHECK(c);
844 else
845 n = 1;
846 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100848 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
850 c = TO_SPECIAL(p[1], p[2]);
851 p += 2;
852 }
853#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100854 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 p += 2;
856#endif
857 if (*++p == NUL && bp->b_next != NULL)
858 {
859 bp = bp->b_next;
860 p = bp->b_str;
861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100863 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
865 if (n != 1)
866 c = (*mb_ptr2char)(buf);
867 break;
868 }
869 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100870 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 }
874
875 return c;
876}
877
878/*
879 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
880 * If old_redo is TRUE, use old_redobuff instead of redobuff.
881 * The escaped K_SPECIAL and CSI are copied without translation.
882 */
883 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100884copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
886 int c;
887
888 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100889 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890}
891
892/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100893 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * Insert the redo count into the command.
895 * If "old_redo" is TRUE, the last but one command is repeated
896 * instead of the last command (inserting text). This is used for
897 * CTRL-O <.> in insert mode
898 *
899 * return FAIL for failure, OK otherwise
900 */
901 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100902start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904 int c;
905
Bram Moolenaar30613902019-12-01 22:11:18 +0100906 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (read_redo(TRUE, old_redo) == FAIL)
908 return FAIL;
909
910 c = read_redo(FALSE, old_redo);
911
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100912#ifdef FEAT_EVAL
913 if (c == K_SID)
914 {
915 // Copy the <SID>{sid}; sequence
916 add_char_buff(&readbuf2, c);
917 for (;;)
918 {
919 c = read_redo(FALSE, old_redo);
920 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100921 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100922 break;
923 }
924 c = read_redo(FALSE, old_redo);
925 }
926#endif
927
Bram Moolenaar30613902019-12-01 22:11:18 +0100928 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (c == '"')
930 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100931 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 c = read_redo(FALSE, old_redo);
933
Bram Moolenaar30613902019-12-01 22:11:18 +0100934 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 if (c >= '1' && c < '9')
936 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100937 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200938
Bram Moolenaar30613902019-12-01 22:11:18 +0100939 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200940 if (c == '=')
941 {
942 add_char_buff(&readbuf2, CAR);
943 cmd_silent = TRUE;
944 }
945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 c = read_redo(FALSE, old_redo);
947 }
948
Bram Moolenaar30613902019-12-01 22:11:18 +0100949 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
951 VIsual = curwin->w_cursor;
952 VIsual_active = TRUE;
953 VIsual_select = FALSE;
954 VIsual_reselect = TRUE;
955 redo_VIsual_busy = TRUE;
956 c = read_redo(FALSE, old_redo);
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaar30613902019-12-01 22:11:18 +0100959 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (count)
961 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100962 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100964 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100967 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100968 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 copy_redo(old_redo);
970 return OK;
971}
972
973/*
974 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100975 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * return FAIL for failure, OK otherwise
977 */
978 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100979start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 int c;
982
983 if (read_redo(TRUE, FALSE) == FAIL)
984 return FAIL;
985 start_stuff();
986
Bram Moolenaar30613902019-12-01 22:11:18 +0100987 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 while ((c = read_redo(FALSE, FALSE)) != NUL)
989 {
990 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
991 {
992 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100993 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 break;
995 }
996 }
997
Bram Moolenaar30613902019-12-01 22:11:18 +0100998 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 copy_redo(FALSE);
1000 block_redo = TRUE;
1001 return OK;
1002}
1003
1004 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001005stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 block_redo = FALSE;
1008}
1009
1010/*
1011 * Initialize typebuf.tb_buf to point to typebuf_init.
1012 * alloc() cannot be used here: In out-of-memory situations it would
1013 * be impossible to type anything.
1014 */
1015 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001016init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001018 if (typebuf.tb_buf != NULL)
1019 return;
1020
1021 typebuf.tb_buf = typebuf_init;
1022 typebuf.tb_noremap = noremapbuf_init;
1023 typebuf.tb_buflen = TYPELEN_INIT;
1024 typebuf.tb_len = 0;
1025 typebuf.tb_off = MAXMAPLEN + 4;
1026 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +02001030 * Returns TRUE when keys cannot be remapped.
1031 */
1032 int
1033noremap_keys(void)
1034{
1035 return KeyNoremap & (RM_NONE|RM_SCRIPT);
1036}
1037
1038/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001039 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
1040 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001042 * If "noremap" is REMAP_YES, new string can be mapped again.
1043 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
1044 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001045 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001046 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001048 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001050 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1051 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001053 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 *
1055 * return FAIL for failure, OK otherwise
1056 */
1057 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001058ins_typebuf(
1059 char_u *str,
1060 int noremap,
1061 int offset,
1062 int nottyped,
1063 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
1065 char_u *s1, *s2;
1066 int newlen;
1067 int addlen;
1068 int i;
1069 int newoff;
1070 int val;
1071 int nrm;
1072
1073 init_typebuf();
1074 if (++typebuf.tb_change_cnt == 0)
1075 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001076 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (offset == 0 && addlen <= typebuf.tb_off)
1081 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001082 /*
1083 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 typebuf.tb_off -= addlen;
1086 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1087 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001088 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1089 >= addlen + 3 * (MAXMAPLEN + 4))
1090 {
1091 /*
1092 * Buffer is empty and string fits in the existing buffer.
1093 * Leave some space before and after, if possible.
1094 */
1095 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1096 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 else
1099 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001100 int extra;
1101
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001102 /*
1103 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001104 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001105 * characters. We add some extra room to avoid having to allocate too
1106 * often.
1107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001109 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1110 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001112 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001113 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 setcursor();
1115 return FAIL;
1116 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001117 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001119 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 return FAIL;
1121 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001122 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 vim_free(s1);
1125 return FAIL;
1126 }
1127 typebuf.tb_buflen = newlen;
1128
Bram Moolenaar30613902019-12-01 22:11:18 +01001129 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1131 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001132 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001134 // copy the old chars, after the insertion point, including the NUL at
1135 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 mch_memmove(s1 + newoff + offset + addlen,
1137 typebuf.tb_buf + typebuf.tb_off + offset,
1138 (size_t)(typebuf.tb_len - offset + 1));
1139 if (typebuf.tb_buf != typebuf_init)
1140 vim_free(typebuf.tb_buf);
1141 typebuf.tb_buf = s1;
1142
1143 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1144 (size_t)offset);
1145 mch_memmove(s2 + newoff + offset + addlen,
1146 typebuf.tb_noremap + typebuf.tb_off + offset,
1147 (size_t)(typebuf.tb_len - offset));
1148 if (typebuf.tb_noremap != noremapbuf_init)
1149 vim_free(typebuf.tb_noremap);
1150 typebuf.tb_noremap = s2;
1151
1152 typebuf.tb_off = newoff;
1153 }
1154 typebuf.tb_len += addlen;
1155
Bram Moolenaar30613902019-12-01 22:11:18 +01001156 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (noremap == REMAP_SCRIPT)
1158 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001159 else if (noremap == REMAP_SKIP)
1160 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 else
1162 val = RM_NONE;
1163
1164 /*
1165 * Adjust typebuf.tb_noremap[] for the new characters:
1166 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1167 * (sometimes) not remappable
1168 * If noremap == REMAP_YES: all the new characters are mappable
1169 * If noremap > 0: "noremap" characters are not remappable, the rest
1170 * mappable
1171 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001172 if (noremap == REMAP_SKIP)
1173 nrm = 1;
1174 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 nrm = addlen;
1176 else
1177 nrm = noremap;
1178 for (i = 0; i < addlen; ++i)
1179 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1180 (--nrm >= 0) ? val : RM_YES;
1181
Bram Moolenaar30613902019-12-01 22:11:18 +01001182 // tb_maplen and tb_silent only remember the length of mapped and/or
1183 // silent mappings at the start of the buffer, assuming that a mapped
1184 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (nottyped || typebuf.tb_maplen > offset)
1186 typebuf.tb_maplen += addlen;
1187 if (silent || typebuf.tb_silent > offset)
1188 {
1189 typebuf.tb_silent += addlen;
1190 cmd_silent = TRUE;
1191 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001192 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 typebuf.tb_no_abbr_cnt += addlen;
1194
1195 return OK;
1196}
1197
1198/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001199 * Put character "c" back into the typeahead buffer.
1200 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001201 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1202 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001203 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001204 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001205 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001206ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001207{
zeertzjq6cac7702022-01-04 18:01:21 +00001208 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001209 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001210
zeertzjq2e7cba32022-06-10 15:30:32 +01001211 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001212 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001213 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001214}
1215
1216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001218 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001219 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1221 * changed it was reallocated and the old pointer can no longer be used.
1222 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1223 * that was just added.
1224 */
1225 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001226typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001227 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
1229 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001230#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1231 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233 ));
1234}
1235
1236/*
1237 * Return TRUE if there are no characters in the typeahead buffer that have
1238 * not been typed (result from a mapping or come from ":normal").
1239 */
1240 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001241typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 return typebuf.tb_maplen == 0;
1244}
1245
1246/*
1247 * Return the number of characters that are mapped (or not typed).
1248 */
1249 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001250typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252 return typebuf.tb_maplen;
1253}
1254
1255/*
1256 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1257 */
1258 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001259del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
1261 int i;
1262
1263 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001264 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 typebuf.tb_len -= len;
1267
1268 /*
1269 * Easy case: Just increase typebuf.tb_off.
1270 */
1271 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1272 >= 3 * MAXMAPLEN + 3)
1273 typebuf.tb_off += len;
1274 /*
1275 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1276 */
1277 else
1278 {
1279 i = typebuf.tb_off + offset;
1280 /*
1281 * Leave some extra room at the end to avoid reallocation.
1282 */
1283 if (typebuf.tb_off > MAXMAPLEN)
1284 {
1285 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1286 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1287 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1288 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1289 typebuf.tb_off = MAXMAPLEN;
1290 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001291 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1293 typebuf.tb_buf + i + len,
1294 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001295 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1297 typebuf.tb_noremap + i + len,
1298 (size_t)(typebuf.tb_len - offset));
1299 }
1300
Bram Moolenaar30613902019-12-01 22:11:18 +01001301 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
1303 if (typebuf.tb_maplen < offset + len)
1304 typebuf.tb_maplen = offset;
1305 else
1306 typebuf.tb_maplen -= len;
1307 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001308 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 if (typebuf.tb_silent < offset + len)
1311 typebuf.tb_silent = offset;
1312 else
1313 typebuf.tb_silent -= len;
1314 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001315 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 if (typebuf.tb_no_abbr_cnt < offset + len)
1318 typebuf.tb_no_abbr_cnt = offset;
1319 else
1320 typebuf.tb_no_abbr_cnt -= len;
1321 }
1322
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001323#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001324 // Reset the flag that text received from a client or from feedkeys()
1325 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001326 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327#endif
1328 if (++typebuf.tb_change_cnt == 0)
1329 typebuf.tb_change_cnt = 1;
1330}
1331
zeertzjq094c4392024-04-18 22:09:37 +02001332/*
1333 * State for adding bytes to a recording or 'showcmd'.
1334 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001335typedef struct
1336{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001337 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001338 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001339 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001340 unsigned pending_special;
1341 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001342} gotchars_state_T;
1343
1344/*
1345 * Add a single byte to a recording or 'showcmd'.
1346 * Return TRUE if a full key has been received, FALSE otherwise.
1347 */
1348 static int
1349gotchars_add_byte(gotchars_state_T *state, char_u byte)
1350{
1351 int c = state->buf[state->buflen++] = byte;
1352 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001353 int in_special = state->pending_special > 0;
1354 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001355
zeertzjq6b13e3d2024-04-22 21:04:29 +02001356 if (in_special)
1357 state->pending_special--;
1358 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001359#ifdef FEAT_GUI
1360 || c == CSI
1361#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001362 )
1363 // When receiving a special key sequence, store it until we have all
1364 // the bytes and we can decide what to do with it.
1365 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001366
zeertzjq6b13e3d2024-04-22 21:04:29 +02001367 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001368 goto ret_false;
1369
zeertzjq6b13e3d2024-04-22 21:04:29 +02001370 if (in_mbyte)
1371 state->pending_mbyte--;
1372 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001373 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001374 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001375 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001376 if (state->prev_c == KS_MODIFIER)
1377 // When receiving a modifier, wait for the modified key.
1378 goto ret_false;
1379 c = TO_SPECIAL(state->prev_c, c);
1380 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1381 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1382 // in a recording.
1383 state->buflen = 0;
1384 }
1385 // When receiving a multibyte character, store it until we have all
1386 // the bytes, so that it won't be split between two buffer blocks,
1387 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001388 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001389 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001390
1391 if (state->pending_mbyte > 0)
1392 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001393
1394 retval = TRUE;
1395ret_false:
1396 state->prev_c = c;
1397 return retval;
1398}
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400/*
1401 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001402 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 */
1404 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001405gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001407 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001408 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001409 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001410 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
zeertzjqacdfb8a2024-04-17 21:28:54 +02001412 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001414 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001415 continue;
1416
Bram Moolenaar30613902019-12-01 22:11:18 +01001417 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001418 for (i = 0; i < state.buflen; ++i)
1419 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001421 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001423 state.buf[state.buflen] = NUL;
1424 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001425 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001426 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001428 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 may_sync_undo();
1432
1433#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001434 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 debug_did_msg = FALSE;
1436#endif
1437
Bram Moolenaar30613902019-12-01 22:11:18 +01001438 // Since characters have been typed, consider the following to be in
1439 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 ++maptick;
1441}
1442
1443/*
zeertzjqbf321802024-01-28 19:03:00 +01001444 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001445 */
1446 void
zeertzjqbf321802024-01-28 19:03:00 +01001447gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001448{
zeertzjqbf321802024-01-28 19:03:00 +01001449 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001450 gotchars(nop_buf, 3);
1451}
1452
1453/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001454 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1455 * character back into the typeahead buffer, thus gotchars() will be called
1456 * again.
1457 * Only affects recorded characters.
1458 */
1459 void
1460ungetchars(int len)
1461{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001462 if (reg_recording == 0)
1463 return;
1464
1465 delete_buff_tail(&recordbuff, len);
1466 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001467}
1468
1469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 * Sync undo. Called when typed characters are obtained from the typeahead
1471 * buffer, or when a menu is used.
1472 * Do not sync:
1473 * - In Insert mode, unless cursor key has been used.
1474 * - While reading a script file.
1475 * - When no_u_sync is non-zero.
1476 */
1477 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001478may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar24959102022-05-07 20:01:16 +01001480 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001481 && scriptin[curscript] == NULL)
1482 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483}
1484
1485/*
1486 * Make "typebuf" empty and allocate new buffers.
1487 * Returns FAIL when out of memory.
1488 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001489 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001490alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492 typebuf.tb_buf = alloc(TYPELEN_INIT);
1493 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1494 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1495 {
1496 free_typebuf();
1497 return FAIL;
1498 }
1499 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001500 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 typebuf.tb_len = 0;
1502 typebuf.tb_maplen = 0;
1503 typebuf.tb_silent = 0;
1504 typebuf.tb_no_abbr_cnt = 0;
1505 if (++typebuf.tb_change_cnt == 0)
1506 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001507#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1508 typebuf_was_filled = FALSE;
1509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 return OK;
1511}
1512
1513/*
1514 * Free the buffers of "typebuf".
1515 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001516 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001517free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001519 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001520 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001521 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001522 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001523 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001524 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001525 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001526 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527}
1528
1529/*
1530 * When doing ":so! file", the current typeahead needs to be saved, and
1531 * restored when "file" has been read completely.
1532 */
1533static typebuf_T saved_typebuf[NSCRIPT];
1534
1535 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001536save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 init_typebuf();
1539 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001540 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (alloc_typebuf() == FAIL)
1542 {
1543 closescript();
1544 return FAIL;
1545 }
1546 return OK;
1547}
1548
Bram Moolenaar30613902019-12-01 22:11:18 +01001549static int old_char = -1; // character put back by vungetc()
1550static int old_mod_mask; // mod_mask for ungotten character
1551static int old_mouse_row; // mouse_row related to old_char
1552static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001553static int old_KeyStuffed; // whether old_char was stuffed
1554
zeertzjq6d4e7252022-04-07 13:58:04 +01001555static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001556{
1557 // If the old character was not stuffed and characters have been added to
1558 // the stuff buffer, need to first get the stuffed characters instead.
1559 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1560}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/*
1563 * Save all three kinds of typeahead, so that the user must type at a prompt.
1564 */
1565 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001566save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
1568 tp->save_typebuf = typebuf;
1569 tp->typebuf_valid = (alloc_typebuf() == OK);
1570 if (!tp->typebuf_valid)
1571 typebuf = tp->save_typebuf;
1572
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001573 tp->old_char = old_char;
1574 tp->old_mod_mask = old_mod_mask;
1575 old_char = -1;
1576
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001577 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001578 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001579 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001580 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581# ifdef USE_INPUT_BUF
1582 tp->save_inputbuf = get_input_buf();
1583# endif
1584}
1585
1586/*
1587 * Restore the typeahead to what it was before calling save_typeahead().
1588 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001589 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001592restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594 if (tp->typebuf_valid)
1595 {
1596 free_typebuf();
1597 typebuf = tp->save_typebuf;
1598 }
1599
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001600 old_char = tp->old_char;
1601 old_mod_mask = tp->old_mod_mask;
1602
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001603 free_buff(&readbuf1);
1604 readbuf1 = tp->save_readbuf1;
1605 free_buff(&readbuf2);
1606 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001608 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609# endif
1610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612/*
1613 * Open a new script file for the ":source!" command.
1614 */
1615 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001616openscript(
1617 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001618 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 if (curscript + 1 == NSCRIPT)
1621 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001622 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 return;
1624 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001625
1626 // Disallow sourcing a file in the sandbox, the commands would be executed
1627 // later, possibly outside of the sandbox.
1628 if (check_secure())
1629 return;
1630
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001631#ifdef FEAT_EVAL
1632 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001633 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001634 return;
1635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar30613902019-12-01 22:11:18 +01001637 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001639 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 expand_env(name, NameBuff, MAXPATHL);
1641 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1642 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001643 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (curscript)
1645 --curscript;
1646 return;
1647 }
1648 if (save_typebuf() == FAIL)
1649 return;
1650
1651 /*
1652 * Execute the commands from the file right now when using ":source!"
1653 * after ":global" or ":argdo" or in a loop. Also when another command
1654 * follows. This means the display won't be updated. Don't do this
1655 * always, "make test" would fail.
1656 */
1657 if (directly)
1658 {
1659 oparg_T oa;
1660 int oldcurscript;
1661 int save_State = State;
1662 int save_restart_edit = restart_edit;
1663 int save_insertmode = p_im;
1664 int save_finish_op = finish_op;
1665 int save_msg_scroll = msg_scroll;
1666
Bram Moolenaar24959102022-05-07 20:01:16 +01001667 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001668 msg_scroll = FALSE; // no msg scrolling in Normal mode
1669 restart_edit = 0; // don't go to Insert mode
1670 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 clear_oparg(&oa);
1672 finish_op = FALSE;
1673
1674 oldcurscript = curscript;
1675 do
1676 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001677 update_topline_cursor(); // update cursor position and topline
1678 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001679 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681 while (scriptin[oldcurscript] != NULL);
1682
1683 State = save_State;
1684 msg_scroll = save_msg_scroll;
1685 restart_edit = save_restart_edit;
1686 p_im = save_insertmode;
1687 finish_op = save_finish_op;
1688 }
1689}
1690
1691/*
1692 * Close the currently active input script.
1693 */
1694 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001695closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 free_typebuf();
1698 typebuf = saved_typebuf[curscript];
1699
1700 fclose(scriptin[curscript]);
1701 scriptin[curscript] = NULL;
1702 if (curscript > 0)
1703 --curscript;
1704}
1705
Bram Moolenaarea408852005-06-25 22:49:46 +00001706#if defined(EXITFREE) || defined(PROTO)
1707 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001708close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001709{
1710 while (scriptin[0] != NULL)
1711 closescript();
1712}
1713#endif
1714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715/*
1716 * Return TRUE when reading keys from a script file.
1717 */
1718 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001719using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 return scriptin[curscript] != NULL;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001725 * This function is called just before doing a blocking wait. Thus after
1726 * waiting 'updatetime' for a character to arrive.
1727 */
1728 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001729before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001730{
1731 updatescript(0);
1732#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001733 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001734 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001735#endif
1736}
1737
1738/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001739 * updatescript() is called when a character can be written into the script
1740 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 *
1742 * All the changed memfiles are synced if c == 0 or when the number of typed
1743 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1744 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001745 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001746updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 static int count = 0;
1749
1750 if (c && scriptout)
1751 putc(c, scriptout);
1752 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1753 {
1754 ml_sync_all(c == 0, TRUE);
1755 count = 0;
1756 }
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02001757#ifdef FEAT_EVAL
1758 if (typedchars_pos < MAXMAPLEN)
1759 {
1760 typedchars[typedchars_pos] = c;
1761 typedchars_pos++;
1762 }
1763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001767 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1768 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001769 */
1770 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001771merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001772{
1773 int c = c_arg;
1774
Bram Moolenaarea83c192023-03-18 17:22:46 +00001775 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001776 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001777 {
1778 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001779 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001780 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001781 if (c == NUL)
1782 c = K_ZERO;
1783 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001784 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001785 // CTRL-6 is equivalent to CTRL-^
1786 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001787#ifdef FEAT_GUI_GTK
1788 // These mappings look arbitrary at the first glance, but in fact
1789 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1790 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1791 // more sense and is consistent with usual terminal behaviour).
1792 else if (c == '2')
1793 c = NUL;
1794 else if (c >= '3' && c <= '7')
1795 c = c ^ 0x28;
1796 else if (c == '8')
1797 c = BS;
1798 else if (c == '?')
1799 c = DEL;
1800#endif
1801 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001802 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001803 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001804
1805 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001806 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001807 && c >= 0 && c <= 127)
1808 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001809 // Some terminals (esp. Kitty) do not include Shift in the character.
1810 // Apply it here to get consistency across terminals. Only do ASCII
1811 // letters, for other characters it depends on the keyboard layout.
1812 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1813 {
1814 c += 'a' - 'A';
1815 *modifiers &= ~MOD_MASK_SHIFT;
1816 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001817 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001818 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001819 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001820
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001821 return c;
1822}
1823
1824/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001825 * Add a single byte to 'showcmd' for a partially matched mapping.
1826 * Call add_to_showcmd() if a full key has been received.
1827 */
1828 static void
1829add_byte_to_showcmd(char_u byte)
1830{
1831 static gotchars_state_T state;
1832 char_u *ptr;
1833 int modifiers = 0;
1834 int c = NUL;
1835
1836 if (!p_sc || msg_silent != 0)
1837 return;
1838
1839 if (!gotchars_add_byte(&state, byte))
1840 return;
1841
1842 state.buf[state.buflen] = NUL;
1843 state.buflen = 0;
1844
1845 ptr = state.buf;
1846 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1847 {
1848 modifiers = ptr[2];
1849 ptr += 3;
1850 }
1851
1852 if (*ptr != NUL)
1853 {
1854 char_u *mb_ptr = mb_unescape(&ptr);
1855
1856 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1857 if (c <= 0x7f)
1858 {
1859 // Merge modifiers into the key to make the result more readable.
1860 int modifiers_after = modifiers;
1861 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1862
1863 if (modifiers_after == 0)
1864 {
1865 modifiers = 0;
1866 c = mod_c;
1867 }
1868 }
1869 }
1870
1871 // TODO: is there a more readable and yet compact representation of
1872 // modifiers and special keys?
1873 if (modifiers != 0)
1874 {
1875 add_to_showcmd(K_SPECIAL);
1876 add_to_showcmd(KS_MODIFIER);
1877 add_to_showcmd(modifiers);
1878 }
1879 if (c != NUL)
1880 add_to_showcmd(c);
1881 while (*ptr != NUL)
1882 add_to_showcmd(*ptr++);
1883}
1884
1885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * Get the next input character.
1887 * Can return a special key or a multi-byte character.
1888 * Can return NUL when called recursively, use safe_vgetc() if that's not
1889 * wanted.
1890 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1891 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001892 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 */
1894 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001895vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001899 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001902#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001903 // Do garbage collection when garbagecollect() was called previously and
1904 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001905 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001906 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001907#endif
1908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 /*
1910 * If a character was put back with vungetc, it was already processed.
1911 * Return it directly.
1912 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001913 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 c = old_char;
1916 old_char = -1;
1917 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001918 mouse_row = old_mouse_row;
1919 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
zeertzjq81b46a62022-04-09 17:58:49 +01001923 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001924 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001925
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001926 mod_mask = 0;
1927 vgetc_mod_mask = 0;
1928 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001929
1930 // last_recorded_len can be larger than last_vgetc_recorded_len
1931 // if peeking records more
1932 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001933
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001934 for (;;) // this is done twice if there are modifiers
1935 {
1936 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001937
Bram Moolenaar78aed952022-09-24 15:36:35 +01001938 // No mapping after modifier has been read, using an input method
1939 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001940 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001941#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001942 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001943#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001944#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001945 || popup_no_mapping()
1946#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001947 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001949 ++no_mapping;
1950 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001951 // mod_mask value may change, remember we did the increment
1952 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001954 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001955 if (did_inc)
1956 {
1957 --no_mapping;
1958 --allow_keys;
1959 }
1960
Christopher Plewright03193062022-11-22 12:58:27 +00001961 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001962 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001963#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001964 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001965#endif
1966 )
1967 {
1968 int save_allow_keys = allow_keys;
1969
1970 ++no_mapping;
1971 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001972 c2 = vgetorpeek(TRUE); // no mapping for these chars
1973 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001974 --no_mapping;
1975 allow_keys = save_allow_keys;
1976 if (c2 == KS_MODIFIER)
1977 {
1978 mod_mask = c;
1979 continue;
1980 }
1981 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001983 // K_ESC is used to avoid ambiguity with the single Esc
1984 // character that might be the start of an escape sequence.
1985 // Convert it back to a single Esc here.
1986 if (c == K_ESC)
1987 c = ESC;
1988
Bram Moolenaar4f974752019-02-17 17:44:42 +01001989#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001990 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1991 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001992 if (
1993# ifdef VIMDLL
1994 gui.in_use &&
1995# endif
1996 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001998 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001999 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002000
2001 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002002 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002003 {
K.Takata54119102022-02-03 13:33:03 +00002004 name[j] = c;
2005 if (j < 199)
2006 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002007 }
K.Takata54119102022-02-03 13:33:03 +00002008 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002009 gui_make_tearoff(name);
2010 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002013#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002014 // GTK: <F10> normally selects the menu, but it's passed until
2015 // here to allow mapping it. Intercept and invoke the GTK
2016 // behavior if it's not mapped.
2017 if (c == K_F10 && gui.menubar != NULL)
2018 {
2019 gtk_menu_shell_select_first(
2020 GTK_MENU_SHELL(gui.menubar), FALSE);
2021 continue;
2022 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002025 // Handle focus event here, so that the caller doesn't need to
2026 // know about it. Return K_IGNORE so that we loop once (needed
2027 // if 'lazyredraw' is set).
2028 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002029 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002030 ui_focus_change(c == K_FOCUSGAINED);
2031 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02002032 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002033
2034 // Translate K_CSI to CSI. The special key is only used to
2035 // avoid it being recognized as the start of a special key.
2036 if (c == K_CSI)
2037 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002039#ifdef FEAT_EVAL
2040 if (c == K_SID)
2041 {
2042 int j;
2043
2044 // Handle <SID>{sid}; Do up to 20 digits for safety.
2045 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002046 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002047 last_used_sid = last_used_sid * 10 + (c - '0');
2048 last_used_map = NULL;
2049 continue;
2050 }
2051#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002052 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002053
zeertzjq90a80022024-07-13 19:06:44 +02002054 // For a multi-byte character get all the bytes and return the
2055 // converted character.
2056 // Note: This will loop until enough bytes are received!
2057 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2058 {
2059 ++no_mapping;
2060 buf[0] = c;
2061 for (i = 1; i < n; ++i)
2062 {
2063 buf[i] = vgetorpeek(TRUE);
2064 if (buf[i] == K_SPECIAL
2065#ifdef FEAT_GUI
2066 || (buf[i] == CSI)
2067#endif
2068 )
2069 {
2070 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2071 // sequence, which represents a K_SPECIAL (0x80),
2072 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2073 // represents a CSI (0x9B),
2074 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2075 // too.
2076 c = vgetorpeek(TRUE);
2077 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2078 buf[i] = CSI;
2079 }
2080 }
2081 --no_mapping;
2082 c = (*mb_ptr2char)(buf);
2083 }
2084
2085 if (vgetc_char == 0)
2086 {
2087 vgetc_mod_mask = mod_mask;
2088 vgetc_char = c;
2089 }
2090
zeertzjqd9be94c2024-07-14 10:20:20 +02002091 // A keypad or special function key was not mapped, use it like
2092 // its ASCII equivalent.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002093 switch (c)
2094 {
2095 case K_KPLUS: c = '+'; break;
2096 case K_KMINUS: c = '-'; break;
2097 case K_KDIVIDE: c = '/'; break;
2098 case K_KMULTIPLY: c = '*'; break;
2099 case K_KENTER: c = CAR; break;
2100 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002101#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002102 // Can be either '.' or a ',',
2103 // depending on the type of keypad.
2104 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002105#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002106 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002107#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002108 case K_K0: c = '0'; break;
2109 case K_K1: c = '1'; break;
2110 case K_K2: c = '2'; break;
2111 case K_K3: c = '3'; break;
2112 case K_K4: c = '4'; break;
2113 case K_K5: c = '5'; break;
2114 case K_K6: c = '6'; break;
2115 case K_K7: c = '7'; break;
2116 case K_K8: c = '8'; break;
2117 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002118
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002119 case K_XHOME:
2120 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002121 {
2122 c = K_S_HOME;
2123 mod_mask = 0;
2124 }
2125 else if (mod_mask == MOD_MASK_CTRL)
2126 {
2127 c = K_C_HOME;
2128 mod_mask = 0;
2129 }
2130 else
2131 c = K_HOME;
2132 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002133 case K_XEND:
2134 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002135 {
2136 c = K_S_END;
2137 mod_mask = 0;
2138 }
2139 else if (mod_mask == MOD_MASK_CTRL)
2140 {
2141 c = K_C_END;
2142 mod_mask = 0;
2143 }
2144 else
2145 c = K_END;
2146 break;
2147
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002148 case K_XUP: c = K_UP; break;
2149 case K_XDOWN: c = K_DOWN; break;
2150 case K_XLEFT: c = K_LEFT; break;
2151 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002154 break;
2155 }
zeertzjq81b46a62022-04-09 17:58:49 +01002156
2157 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002159
2160#ifdef FEAT_EVAL
2161 /*
2162 * In the main loop "may_garbage_collect" can be set to do garbage
2163 * collection in the first next vgetc(). It's disabled after that to
2164 * avoid internally used Lists and Dicts to be freed.
2165 */
2166 may_garbage_collect = FALSE;
2167#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002168
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002169#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002170 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002171 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002172 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002173 bevalexpr_due_set = FALSE;
2174 ui_remove_balloon();
2175 }
2176#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002177#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002178 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2179 // are filtered.
2180 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002181 {
2182 if (c == Ctrl_C)
2183 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002184 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002185 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002186#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002187
Shougo Matsushita83678842024-07-11 22:05:12 +02002188#ifdef FEAT_EVAL
2189 c = do_key_input_pre(c);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002190
2191 // Clear the next typedchars_pos
2192 typedchars_pos = 0;
Shougo Matsushita83678842024-07-11 22:05:12 +02002193#endif
2194
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002195 // Need to process the character before we know it's safe to do something
2196 // else.
2197 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002198 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002199
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002200 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201}
2202
Shougo Matsushita83678842024-07-11 22:05:12 +02002203#ifdef FEAT_EVAL
2204/*
2205 * Handle the InsertCharPre autocommand.
2206 * "c" is the character that was typed.
2207 * Return new input character.
2208 */
2209 static int
2210do_key_input_pre(int c)
2211{
2212 int res = c;
2213 char_u buf[MB_MAXBYTES + 1];
2214 char_u curr_mode[MODE_MAX_LENGTH];
2215 int save_State = State;
2216 save_v_event_T save_v_event;
2217 dict_T *v_event;
2218
2219 // Return quickly when there is nothing to do.
2220 if (!has_keyinputpre())
2221 return res;
2222
2223 if (IS_SPECIAL(c))
2224 {
2225 buf[0] = K_SPECIAL;
2226 buf[1] = KEY2TERMCAP0(c);
2227 buf[2] = KEY2TERMCAP1(c);
2228 buf[3] = NUL;
2229 }
2230 else
2231 buf[(*mb_char2bytes)(c, buf)] = NUL;
2232
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002233 typedchars[typedchars_pos] = NUL;
2234 vim_unescape_csi(typedchars);
2235
Shougo Matsushita83678842024-07-11 22:05:12 +02002236 get_mode(curr_mode);
2237
2238 // Lock the text to avoid weird things from happening.
2239 ++textlock;
2240 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2241
2242 v_event = get_v_event(&save_v_event);
2243 (void)dict_add_bool(v_event, "typed", KeyTyped);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002244 (void)dict_add_string(v_event, "typedchar", typedchars);
Shougo Matsushita83678842024-07-11 22:05:12 +02002245
2246 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2247 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2248 {
2249 // Get the value of v:char. It may be empty or more than one
2250 // character. Only use it when changed, otherwise continue with the
2251 // original character.
2252 char_u *v_char;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002253 size_t v_charlen;
Shougo Matsushita83678842024-07-11 22:05:12 +02002254
2255 v_char = get_vim_var_str(VV_CHAR);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002256 v_charlen = STRLEN(v_char);
Shougo Matsushita83678842024-07-11 22:05:12 +02002257
2258 // Convert special bytes when it is special string.
John Marriotte7a1bbf2024-11-11 20:40:33 +01002259 if (v_charlen >= 3 && v_char[0] == K_SPECIAL)
Shougo Matsushita83678842024-07-11 22:05:12 +02002260 res = TERMCAP2KEY(v_char[1], v_char[2]);
John Marriotte7a1bbf2024-11-11 20:40:33 +01002261 else if (v_charlen > 0)
Shougo Matsushita83678842024-07-11 22:05:12 +02002262 res = PTR2CHAR(v_char);
2263 }
2264
2265 restore_v_event(v_event, &save_v_event);
2266
2267 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2268 --textlock;
2269
2270 // Restore the State, it may have been changed.
2271 State = save_State;
2272
2273 return res;
2274}
2275#endif
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277/*
2278 * Like vgetc(), but never return a NUL when called recursively, get a key
2279 * directly from the user (ignoring typeahead).
2280 */
2281 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002282safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 int c;
2285
2286 c = vgetc();
2287 if (c == NUL)
2288 c = get_keystroke();
2289 return c;
2290}
2291
2292/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002293 * Like safe_vgetc(), but loop to handle K_IGNORE.
2294 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002295 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002296 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002297 static int
2298plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002299{
2300 int c;
2301
2302 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002303 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002304 while (c == K_IGNORE
2305 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2306 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002307 return c;
2308}
2309
2310/*
2311 * Like safe_vgetc(), but loop to handle K_IGNORE.
2312 * Also ignore scrollbar events.
2313 */
2314 int
2315plain_vgetc(void)
2316{
2317 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002318
2319 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002320 // Only handle the first pasted character. Drop the rest, since we
2321 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002322 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2323
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002324 return c;
2325}
2326
2327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 * Check if a character is available, such that vgetc() will not block.
2329 * If the next character is a special character or multi-byte, the returned
2330 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002331 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
2333 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002334vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002336 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002338 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339}
2340
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002341#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342/*
2343 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2344 * codes.
2345 */
2346 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002347vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 int c;
2350
2351 ++no_mapping;
2352 ++allow_keys;
2353 c = vpeekc();
2354 --no_mapping;
2355 --allow_keys;
2356 return c;
2357}
2358#endif
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360/*
2361 * Check if any character is available, also half an escape sequence.
2362 * Trick: when no typeahead found, but there is something in the typeahead
2363 * buffer, it must be an ESC that is recognized as the start of a key code.
2364 */
2365 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002366vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 int c;
2369
2370 c = vpeekc();
2371 if (c == NUL && typebuf.tb_len > 0)
2372 c = ESC;
2373 return c;
2374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376/*
2377 * Call vpeekc() without causing anything to be mapped.
2378 * Return TRUE if a character is available, FALSE otherwise.
2379 */
2380 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002381char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383 int retval;
2384
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002385#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002386 // When test_override("char_avail", 1) was called pretend there is no
2387 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002388 if (disable_char_avail_for_testing)
2389 return FALSE;
2390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 ++no_mapping;
2392 retval = vpeekc();
2393 --no_mapping;
2394 return (retval != NUL);
2395}
2396
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002397#if defined(FEAT_EVAL) || defined(PROTO)
2398/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002399 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002400 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002401 static void
zeertzjqe0a2ab32025-02-02 09:14:35 +01002402getchar_common(typval_T *argvars, typval_T *rettv, int allow_number)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002403{
zeertzjqedf0f7d2025-02-02 19:01:01 +01002404 varnumber_T n = 0;
2405 int called_emsg_start = called_emsg;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002406 int error = FALSE;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002407 int simplify = TRUE;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002408 char_u cursor_flag = 'm';
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002409
zeertzjqe0a2ab32025-02-02 09:14:35 +01002410 if ((in_vim9script()
2411 && check_for_opt_bool_or_number_arg(argvars, 0) == FAIL)
2412 || (argvars[0].v_type != VAR_UNKNOWN
2413 && check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002414 return;
2415
zeertzjqe0a2ab32025-02-02 09:14:35 +01002416 if (argvars[0].v_type != VAR_UNKNOWN && argvars[1].v_type == VAR_DICT)
2417 {
2418 dict_T *d = argvars[1].vval.v_dict;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002419 char_u *cursor_str;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002420
2421 if (allow_number)
2422 allow_number = dict_get_bool(d, "number", TRUE);
2423 else if (dict_has_key(d, "number"))
zeertzjqe0a2ab32025-02-02 09:14:35 +01002424 semsg(_(e_invalid_argument_str), "number");
zeertzjqe0a2ab32025-02-02 09:14:35 +01002425
2426 simplify = dict_get_bool(d, "simplify", TRUE);
zeertzjqedf0f7d2025-02-02 19:01:01 +01002427
2428 cursor_str = dict_get_string(d, "cursor", FALSE);
2429 if (cursor_str != NULL)
2430 {
2431 if (STRCMP(cursor_str, "hide") != 0
2432 && STRCMP(cursor_str, "keep") != 0
2433 && STRCMP(cursor_str, "msg") != 0)
2434 semsg(_(e_invalid_value_for_argument_str_str), "cursor",
2435 cursor_str);
2436 else
2437 cursor_flag = cursor_str[0];
2438 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002439 }
2440
zeertzjqedf0f7d2025-02-02 19:01:01 +01002441 if (called_emsg != called_emsg_start)
2442 return;
2443
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002444#ifdef MESSAGE_QUEUE
2445 // vpeekc() used to check for messages, but that caused problems, invoking
2446 // a callback where it was not expected. Some plugins use getchar(1) in a
2447 // loop to await a message, therefore make sure we check for messages here.
2448 parse_queued_messages();
2449#endif
2450
zeertzjqedf0f7d2025-02-02 19:01:01 +01002451 if (cursor_flag == 'h')
2452 cursor_sleep();
2453 else if (cursor_flag == 'm')
2454 windgoto(msg_row, msg_col);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002455
2456 ++no_mapping;
2457 ++allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002458 if (!simplify)
2459 ++no_reduce_keys;
zeertzjqedf0f7d2025-02-02 19:01:01 +01002460 for (;;)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002461 {
zeertzjqe0a2ab32025-02-02 09:14:35 +01002462 if (argvars[0].v_type == VAR_UNKNOWN
2463 || (argvars[0].v_type == VAR_NUMBER
2464 && argvars[0].vval.v_number == -1))
Bram Moolenaar30613902019-12-01 22:11:18 +01002465 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002466 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002467 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002468 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002469 n = vpeekc_any();
2470 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002471 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002472 n = 0;
2473 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002474 // getchar(0) and char avail() != NUL: get a character.
2475 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2476 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002477
Bram Moolenaar15183b42020-09-05 19:59:39 +02002478 if (n == K_IGNORE || n == K_MOUSEMOVE
2479 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002480 continue;
2481 break;
2482 }
2483 --no_mapping;
2484 --allow_keys;
zeertzjqe0a2ab32025-02-02 09:14:35 +01002485 if (!simplify)
2486 --no_reduce_keys;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002487
zeertzjqedf0f7d2025-02-02 19:01:01 +01002488 if (cursor_flag == 'h')
2489 cursor_unsleep();
2490
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002491 set_vim_var_nr(VV_MOUSE_WIN, 0);
2492 set_vim_var_nr(VV_MOUSE_WINID, 0);
2493 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2494 set_vim_var_nr(VV_MOUSE_COL, 0);
2495
zeertzjqe0a2ab32025-02-02 09:14:35 +01002496 if (n != 0 && (!allow_number || IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002497 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002498 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002499 int i = 0;
2500
Bram Moolenaar30613902019-12-01 22:11:18 +01002501 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002502 if (mod_mask != 0)
2503 {
2504 temp[i++] = K_SPECIAL;
2505 temp[i++] = KS_MODIFIER;
2506 temp[i++] = mod_mask;
2507 }
2508 if (IS_SPECIAL(n))
2509 {
2510 temp[i++] = K_SPECIAL;
2511 temp[i++] = K_SECOND(n);
2512 temp[i++] = K_THIRD(n);
2513 }
2514 else if (has_mbyte)
2515 i += (*mb_char2bytes)(n, temp + i);
2516 else
2517 temp[i++] = n;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002518 temp[i] = NUL;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002519 rettv->v_type = VAR_STRING;
John Marriotte7a1bbf2024-11-11 20:40:33 +01002520 rettv->vval.v_string = vim_strnsave(temp, i);
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002521
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002522 if (is_mouse_key(n))
2523 {
2524 int row = mouse_row;
2525 int col = mouse_col;
2526 win_T *win;
2527 linenr_T lnum;
2528 win_T *wp;
2529 int winnr = 1;
2530
2531 if (row >= 0 && col >= 0)
2532 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002533 // Find the window at the mouse coordinates and compute the
2534 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002535 win = mouse_find_win(&row, &col, FIND_POPUP);
2536 if (win == NULL)
2537 return;
2538 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002539#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002540 if (WIN_IS_POPUP(win))
2541 winnr = 0;
2542 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002543#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002544 for (wp = firstwin; wp != win && wp != NULL;
2545 wp = wp->w_next)
2546 ++winnr;
2547 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2548 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2549 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2550 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2551 }
2552 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002553 }
zeertzjqe0a2ab32025-02-02 09:14:35 +01002554 else if (!allow_number)
2555 rettv->v_type = VAR_STRING;
2556 else
2557 rettv->vval.v_number = n;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002558}
2559
2560/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002561 * "getchar()" function
2562 */
2563 void
2564f_getchar(typval_T *argvars, typval_T *rettv)
2565{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002566 getchar_common(argvars, rettv, TRUE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002567}
2568
2569/*
2570 * "getcharstr()" function
2571 */
2572 void
2573f_getcharstr(typval_T *argvars, typval_T *rettv)
2574{
zeertzjqe0a2ab32025-02-02 09:14:35 +01002575 getchar_common(argvars, rettv, FALSE);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002576}
2577
2578/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002579 * "getcharmod()" function
2580 */
2581 void
2582f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2583{
2584 rettv->vval.v_number = mod_mask;
2585}
2586#endif // FEAT_EVAL
2587
2588#if defined(MESSAGE_QUEUE) || defined(PROTO)
2589# define MAX_REPEAT_PARSE 8
2590
2591/*
2592 * Process messages that have been queued for netbeans or clientserver.
2593 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002594 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002595 * it is safe to do so.
2596 */
2597 void
2598parse_queued_messages(void)
2599{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002600 int old_curwin_id;
2601 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002602 int i;
2603 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002604 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002605 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002606
2607 // Do not handle messages while redrawing, because it may cause buffers to
2608 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002609 // Also bail out when parsing messages was explicitly disabled.
2610 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002611 return;
2612
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002613 // If memory allocation fails during startup we'll exit but curbuf or
2614 // curwin could be NULL.
2615 if (curbuf == NULL || curwin == NULL)
2616 return;
2617
2618 old_curbuf_fnum = curbuf->b_fnum;
2619 old_curwin_id = curwin->w_id;
2620
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002621 ++entered;
2622
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002623 // may_garbage_collect is set in main_loop() to do garbage collection when
2624 // blocking to wait on a character. We don't want that while parsing
2625 // messages, a callback may invoke vgetc() while lists and dicts are in use
2626 // in the call stack.
2627 may_garbage_collect = FALSE;
2628
2629 // Loop when a job ended, but don't keep looping forever.
2630 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2631 {
2632 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002633# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002634 channel_handle_events(FALSE);
2635# endif
2636
2637# ifdef FEAT_NETBEANS_INTG
2638 // Process the queued netbeans messages.
2639 netbeans_parse_messages();
2640# endif
2641# ifdef FEAT_JOB_CHANNEL
2642 // Write any buffer lines still to be written.
2643 channel_write_any_lines();
2644
2645 // Process the messages queued on channels.
2646 channel_parse_messages();
2647# endif
2648# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2649 // Process the queued clientserver messages.
2650 server_parse_messages();
2651# endif
2652# ifdef FEAT_JOB_CHANNEL
2653 // Check if any jobs have ended. If so, repeat the above to handle
2654 // changes, e.g. stdin may have been closed.
2655 if (job_check_ended())
2656 continue;
2657# endif
2658# ifdef FEAT_TERMINAL
2659 free_unused_terminals();
2660# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002661
2662# ifdef FEAT_SOUND_MACOSX
2663 process_cfrunloop();
2664# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002665# ifdef FEAT_SOUND_CANBERRA
2666 if (has_sound_callback_in_queue())
2667 invoke_sound_callback();
2668# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002669#ifdef SIGUSR1
2670 if (got_sigusr1)
2671 {
2672 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2673 got_sigusr1 = FALSE;
2674 }
2675#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002676 break;
2677 }
2678
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002679 // When not nested we'll go back to waiting for a typed character. If it
2680 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002681 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002682 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002683
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002684 may_garbage_collect = save_may_garbage_collect;
2685
2686 // If the current window or buffer changed we need to bail out of the
2687 // waiting loop. E.g. when a job exit callback closes the terminal window.
2688 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002689 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002690
2691 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002692}
2693#endif
2694
2695
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002696typedef enum {
2697 map_result_fail, // failed, break loop
2698 map_result_get, // get a character from typeahead
2699 map_result_retry, // try to map again
2700 map_result_nomatch // no matching mapping, get char
2701} map_result_T;
2702
2703/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002704 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002705 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002706 */
2707 static int
zeertzjqee446032022-04-30 15:02:22 +01002708at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002709{
2710 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2711 int c = *p;
2712
2713 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002714 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002715 && p[1] == KS_MODIFIER
2716 && (p[2] & MOD_MASK_CTRL))
2717 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002718 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2719 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002720}
2721
2722/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002723 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002724 * into just a key, apply that.
2725 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2726 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002727 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002728 */
2729 static int
2730check_simplify_modifier(int max_offset)
2731{
2732 int offset;
2733 char_u *tp;
2734
2735 for (offset = 0; offset < max_offset; ++offset)
2736 {
2737 if (offset + 3 >= typebuf.tb_len)
2738 break;
2739 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002740 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002741 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002742 // A modifier was not used for a mapping, apply it to ASCII keys.
2743 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002744 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002745 int c = tp[3];
2746 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002747
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002748 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002749 {
2750 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002751 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002752
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002753 if (offset == 0)
2754 {
2755 // At the start: remember the character and mod_mask before
2756 // merging, in some cases, e.g. at the hit-return prompt,
2757 // they are put back in the typeahead buffer.
2758 vgetc_char = c;
2759 vgetc_mod_mask = tp[2];
2760 }
zeertzjq12e21e32022-04-27 11:58:01 +01002761 if (IS_SPECIAL(new_c))
2762 {
2763 new_string[0] = K_SPECIAL;
2764 new_string[1] = K_SECOND(new_c);
2765 new_string[2] = K_THIRD(new_c);
2766 len = 3;
2767 }
2768 else
2769 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002770 if (modifier == 0)
2771 {
2772 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002773 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002774 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002775 }
2776 else
2777 {
2778 tp[2] = modifier;
2779 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002780 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002781 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002782 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002783 return len;
2784 }
2785 }
2786 }
2787 return 0;
2788}
2789
2790/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002791 * Return TRUE if the terminal sends modifiers with various keys. This is when
2792 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2793 * enabled.
2794 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002795 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002796key_protocol_enabled(void)
2797{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002798 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2799 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2800 ? modify_otherkeys_state == MOKS_ENABLED
2801 : seenModifyOtherKeys;
2802 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002803}
2804
2805/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002806 * Handle mappings in the typeahead buffer.
2807 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002808 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002809 * - When there is no match yet, return map_result_nomatch, need to get more
2810 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002811 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 */
2813 static int
2814handle_mapping(
2815 int *keylenp,
2816 int *timedout,
2817 int *mapdepth)
2818{
2819 mapblock_T *mp = NULL;
2820 mapblock_T *mp2;
2821 mapblock_T *mp_match;
2822 int mp_match_len = 0;
2823 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002824 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 int tb_c1;
2826 int mlen;
2827#ifdef FEAT_LANGMAP
2828 int nolmaplen;
2829#endif
2830 int keylen = *keylenp;
2831 int i;
2832 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002833 int is_plug_map = FALSE;
2834
zeertzjqbb404f52022-07-23 06:25:29 +01002835 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002836 if (typebuf.tb_len >= 3
2837 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002838 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2839 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2840 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002841
2842 /*
2843 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002844 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002845 *
2846 * Don't look for mappings if:
2847 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2848 * - maphash_valid not set: no mappings present.
2849 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2850 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002851 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002852 * - waiting for a char with --more--
2853 * - in Ctrl-X mode, and we get a valid char for that mode
2854 */
2855 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2856 if (no_mapping == 0 && is_maphash_valid()
2857 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002858 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 || (p_remap
2860 && (typebuf.tb_noremap[typebuf.tb_off]
2861 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002862 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2863 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2864 && State != MODE_ASKMORE
2865 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002866 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002867 {
Christopher Plewright03193062022-11-22 12:58:27 +00002868#ifdef FEAT_GUI
2869 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2870 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002871 {
2872 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2873 // K_SPECIAL KS_MODIFIER {flags}.
2874 tb_c1 = K_SPECIAL;
2875 }
2876#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002877#ifdef FEAT_LANGMAP
2878 if (tb_c1 == K_SPECIAL)
2879 nolmaplen = 2;
2880 else
2881 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002882 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2883 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002884 nolmaplen = 0;
2885 }
2886#endif
2887 // First try buffer-local mappings.
2888 mp = get_buf_maphash_list(local_State, tb_c1);
2889 mp2 = get_maphash_list(local_State, tb_c1);
2890 if (mp == NULL)
2891 {
2892 // There are no buffer-local mappings.
2893 mp = mp2;
2894 mp2 = NULL;
2895 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002896
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002897 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002898 * Loop until a partly matching mapping is found or all (local)
2899 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002900 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002901 * A full match is only accepted if there is no partly match, so "aa"
2902 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002903 */
2904 mp_match = NULL;
2905 mp_match_len = 0;
2906 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002907 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002908 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002909 // Only consider an entry if the first character matches and it is
2910 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002911 // Skip ":lmap" mappings if keys were mapped.
2912 if (mp->m_keys[0] == tb_c1
2913 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002914 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002915 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002916 && ((mp->m_mode & MODE_LANGMAP) == 0
2917 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002918 {
2919#ifdef FEAT_LANGMAP
2920 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002921 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002922#endif
2923 // find the match length of this mapping
2924 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2925 {
zeertzjq49660f52022-10-20 17:59:38 +01002926 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002927#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002928 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002929 {
2930 if (nomap == 2 && c2 == KS_MODIFIER)
2931 modifiers = 1;
2932 else if (nomap == 1 && modifiers == 1)
2933 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002934 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002935 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002936 else
zeertzjq49660f52022-10-20 17:59:38 +01002937 {
2938 if (c2 == K_SPECIAL)
2939 nomap = 2;
2940 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2941 // Only apply 'langmap' if merging modifiers into
2942 // the key will not result in another character,
2943 // so that 'langmap' behaves consistently in
2944 // different terminals and GUIs.
2945 LANGMAP_ADJUST(c2, TRUE);
2946 modifiers = 0;
2947 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002948#endif
zeertzjq49660f52022-10-20 17:59:38 +01002949 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002950 break;
2951 }
2952
Bram Moolenaareda35f72019-08-03 14:59:44 +02002953 // Don't allow mapping the first byte(s) of a multi-byte char.
2954 // Happens when mapping <M-a> and then changing 'encoding'.
2955 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002956 {
2957 char_u *p1 = mp->m_keys;
2958 char_u *p2 = mb_unescape(&p1);
2959
2960 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002961 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002962 mlen = 0;
2963 }
2964
2965 // Check an entry whether it matches.
2966 // - Full match: mlen == keylen
2967 // - Partly match: mlen == typebuf.tb_len
2968 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002969 if (mlen == keylen || (mlen == typebuf.tb_len
2970 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002971 {
2972 char_u *s;
2973 int n;
2974
Bram Moolenaareda35f72019-08-03 14:59:44 +02002975 // If only script-local mappings are allowed, check if the
2976 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002977 s = typebuf.tb_noremap + typebuf.tb_off;
2978 if (*s == RM_SCRIPT
2979 && (mp->m_keys[0] != K_SPECIAL
2980 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002981 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002982 continue;
2983
Bram Moolenaareda35f72019-08-03 14:59:44 +02002984 // If one of the typed keys cannot be remapped, skip the
2985 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002986 for (n = mlen; --n >= 0; )
2987 if (*s++ & (RM_NONE|RM_ABBR))
2988 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002989 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002990 continue;
2991
2992 if (keylen > typebuf.tb_len)
2993 {
2994 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002995 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002996 {
2997 // break at a partly match
2998 keylen = KEYLEN_PART_MAP;
2999 break;
3000 }
3001 }
3002 else if (keylen > mp_match_len)
3003 {
3004 // found a longer match
3005 mp_match = mp;
3006 mp_match_len = keylen;
3007 }
3008 }
3009 else
Oleg Goncharov56904f92024-07-23 20:34:15 +02003010 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003011 // No match; may have to check for termcode at next
Oleg Goncharov56904f92024-07-23 20:34:15 +02003012 // character.
3013
3014 // If the first character that didn't match is
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003015 // K_SPECIAL then check for a termcode. This isn't perfect
3016 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003017 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003018 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003019 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003020 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
3021 }
3022 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
3023 want_termcode = 1;
Oleg Goncharov56904f92024-07-23 20:34:15 +02003024
3025 // Check termcode for uppercase character to properly
3026 // process "ESC[27;2;<ascii code>~" control sequences.
3027 if (ASCII_ISUPPER(mp->m_keys[mlen]))
3028 want_termcode = 1;
3029 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003030 }
3031 }
3032
Bram Moolenaareda35f72019-08-03 14:59:44 +02003033 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00003034 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003035 {
3036 mp = mp_match;
3037 keylen = mp_match_len;
3038 }
3039 }
3040
3041 /*
3042 * Check for match with 'pastetoggle'
3043 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003044 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003045 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003046 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
3047 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003048 break;
3049 if (p_pt[mlen] == NUL) // match
3050 {
3051 // write chars to script file(s)
3052 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003053 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3054 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003055
3056 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003057 set_option_value_give_err((char_u *)"paste",
3058 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01003059 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003060 {
3061 msg_col = 0;
3062 msg_row = Rows - 1;
3063 msg_clr_eos(); // clear ruler
3064 }
3065 status_redraw_all();
3066 redraw_statuslines();
3067 showmode();
3068 setcursor();
3069 *keylenp = keylen;
3070 return map_result_retry;
3071 }
3072 // Need more chars for partly match.
3073 if (mlen == typebuf.tb_len)
3074 keylen = KEYLEN_PART_KEY;
3075 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003076 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003077 max_mlen = mlen + 1;
3078 }
3079
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003080 // May check for a terminal code when there is no mapping or only a partial
3081 // mapping. Also check if there is a full mapping with <Esc>, unless timed
3082 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003083 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003084 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
3085 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003086 {
3087 int save_keylen = keylen;
3088
3089 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003090 * When no matching mapping found or found a non-matching mapping that
3091 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003092 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02003093 * - mapping is allowed,
3094 * - keys have not been mapped,
3095 * - and not an ESC sequence, not in insert mode or p_ek is on,
3096 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003097 */
zeertzjq68a573c2022-04-28 14:10:01 +01003098 if (no_mapping == 0 || allow_keys != 0)
3099 {
3100 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003101 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003102 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003103 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003104 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3105 else
3106 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003107
Bram Moolenaar0684e362020-12-03 19:54:42 +01003108 // If no termcode matched but 'pastetoggle' matched partially
3109 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003110 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003111 keylen = KEYLEN_PART_KEY;
3112
Bram Moolenaar975a8802020-06-06 22:36:24 +02003113 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003114 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003115#ifdef FEAT_TERMINAL
3116 check_no_reduce_keys(); // may update the no_reduce_keys flag
3117#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003118 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003119 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003120 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003121 if (keylen < 0)
3122 // ins_typebuf() failed
3123 return map_result_fail;
3124 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003125
Bram Moolenaareda35f72019-08-03 14:59:44 +02003126 // When getting a partial match, but the last characters were not
3127 // typed, don't wait for a typed character to complete the
3128 // termcode. This helps a lot when a ":normal" command ends in an
3129 // ESC.
3130 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003131 keylen = 0;
3132 }
3133 else
3134 keylen = 0;
3135 if (keylen == 0) // no matching terminal code
3136 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003137#ifdef AMIGA
3138 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003139 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003140 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003141 {
3142 char_u *s;
3143
3144 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003145 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3146 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003147 ++s)
3148 ;
3149 if (*s == 'r' || *s == '|') // found one
3150 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003151 del_typebuf(
3152 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003153 // get size and redraw screen
3154 shell_resized();
3155 *keylenp = keylen;
3156 return map_result_retry;
3157 }
3158 if (*s == NUL) // need more characters
3159 keylen = KEYLEN_PART_KEY;
3160 }
3161 if (keylen >= 0)
3162#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003163 // When there was a matching mapping and no termcode could be
3164 // replaced after another one, use that mapping (loop around).
3165 // If there was no mapping at all use the character from the
3166 // typeahead buffer right here.
3167 if (mp == NULL)
3168 {
3169 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003170 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003171 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003172 }
3173
3174 if (keylen > 0) // full matching terminal code
3175 {
3176#if defined(FEAT_GUI) && defined(FEAT_MENU)
3177 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003178 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3179 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003180 {
3181 int idx;
3182
Bram Moolenaareda35f72019-08-03 14:59:44 +02003183 // Using a menu may cause a break in undo! It's like using
3184 // gotchars(), but without recording or writing to a script
3185 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003186 may_sync_undo();
3187 del_typebuf(3, 0);
3188 idx = get_menu_index(current_menu, local_State);
3189 if (idx != MENU_INDEX_INVALID)
3190 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003191 // In Select mode and a Visual mode menu is used: Switch
3192 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003193 // back to Select mode.
3194 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003195 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003196 {
3197 VIsual_select = FALSE;
3198 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003199 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003200 }
3201 ins_typebuf(current_menu->strings[idx],
3202 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003203 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003204 }
3205 }
3206#endif // FEAT_GUI && FEAT_MENU
3207 *keylenp = keylen;
3208 return map_result_retry; // try mapping again
3209 }
3210
Bram Moolenaareda35f72019-08-03 14:59:44 +02003211 // Partial match: get some more characters. When a matching mapping
3212 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003213 if (mp == NULL || keylen < 0)
3214 keylen = KEYLEN_PART_KEY;
3215 else
3216 keylen = mp_match_len;
3217 }
3218
3219 /*
3220 * complete match
3221 */
3222 if (keylen >= 0 && keylen <= typebuf.tb_len)
3223 {
3224 char_u *map_str;
3225
3226#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003227 int save_m_expr;
3228 int save_m_noremap;
3229 int save_m_silent;
3230 char_u *save_m_keys;
zeertzjq9d997ad2024-07-29 21:10:07 +02003231 char_u *save_alt_m_keys;
zeertzjq74011dc2024-07-30 19:17:56 +02003232 int save_alt_m_keylen;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003233#else
3234# define save_m_noremap mp->m_noremap
3235# define save_m_silent mp->m_silent
3236#endif
3237
3238 // write chars to script file(s)
3239 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003240 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3241 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003242
3243 cmd_silent = (typebuf.tb_silent > 0);
3244 del_typebuf(keylen, 0); // remove the mapped keys
3245
3246 /*
3247 * Put the replacement string in front of mapstr.
3248 * The depth check catches ":map x y" and ":map y x".
3249 */
3250 if (++*mapdepth >= p_mmd)
3251 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003252 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003253 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003254 redrawcmdline();
3255 else
3256 setcursor();
3257 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003258 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003259 *keylenp = keylen;
3260 return map_result_fail;
3261 }
3262
3263 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003264 * In Select mode and a Visual mode mapping is used: Switch to Visual
3265 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003266 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003267 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003268 {
3269 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003270 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003271 }
3272
3273#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003274 // Copy the values from *mp that are used, because evaluating the
3275 // expression may invoke a function that redefines the mapping, thereby
3276 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003277 save_m_expr = mp->m_expr;
3278 save_m_noremap = mp->m_noremap;
3279 save_m_silent = mp->m_silent;
3280 save_m_keys = NULL; // only saved when needed
zeertzjq9d997ad2024-07-29 21:10:07 +02003281 save_alt_m_keys = NULL; // only saved when needed
zeertzjq74011dc2024-07-30 19:17:56 +02003282 save_alt_m_keylen = mp->m_alt != NULL ? mp->m_alt->m_keylen : 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003283
3284 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003285 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3286 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003287 */
3288 if (mp->m_expr)
3289 {
3290 int save_vgetc_busy = vgetc_busy;
3291 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003292 int was_screen_col = screen_cur_col;
3293 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003294 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003295
3296 vgetc_busy = 0;
3297 may_garbage_collect = FALSE;
3298
zeertzjq74011dc2024-07-30 19:17:56 +02003299 save_m_keys = vim_strnsave(mp->m_keys, (size_t)mp->m_keylen);
zeertzjq9d997ad2024-07-29 21:10:07 +02003300 save_alt_m_keys = mp->m_alt != NULL
zeertzjq74011dc2024-07-30 19:17:56 +02003301 ? vim_strnsave(mp->m_alt->m_keys,
3302 (size_t)save_alt_m_keylen) : NULL;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003303 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003304
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003305 // The mapping may do anything, but we expect it to take care of
3306 // redrawing. Do put the cursor back where it was.
3307 windgoto(was_screen_row, was_screen_col);
3308 out_flush();
3309
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003310 // If an error was displayed and the expression returns an empty
3311 // string, generate a <Nop> to allow for a redraw.
3312 if (prev_did_emsg != did_emsg
3313 && (map_str == NULL || *map_str == NUL))
3314 {
3315 char_u buf[4];
3316
3317 vim_free(map_str);
3318 buf[0] = K_SPECIAL;
3319 buf[1] = KS_EXTRA;
3320 buf[2] = KE_IGNORE;
3321 buf[3] = NUL;
John Marriotte7a1bbf2024-11-11 20:40:33 +01003322 map_str = vim_strnsave(buf, 3);
Bram Moolenaar24959102022-05-07 20:01:16 +01003323 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003324 {
3325 // redraw the command below the error
3326 msg_didout = TRUE;
3327 if (msg_row < cmdline_row)
3328 msg_row = cmdline_row;
3329 redrawcmd();
3330 }
3331 }
3332
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003333 vgetc_busy = save_vgetc_busy;
3334 may_garbage_collect = save_may_garbage_collect;
3335 }
3336 else
3337#endif
3338 map_str = mp->m_str;
3339
3340 /*
3341 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003342 * If 'from' field is the same as the start of the 'to' field, don't
3343 * remap the first character (but do allow abbreviations).
3344 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003345 */
3346 if (map_str == NULL)
3347 i = FAIL;
3348 else
3349 {
3350 int noremap;
3351
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003352#ifdef FEAT_EVAL
3353 last_used_map = mp;
3354 last_used_sid = -1;
3355#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003356 if (save_m_noremap != REMAP_YES)
3357 noremap = save_m_noremap;
3358 else if (
3359#ifdef FEAT_EVAL
zeertzjq9d997ad2024-07-29 21:10:07 +02003360 save_m_expr ?
3361 (save_m_keys != NULL
3362 && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
3363 || (save_alt_m_keys != NULL
3364 && STRNCMP(map_str, save_alt_m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003365 (size_t)save_alt_m_keylen) == 0) :
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003366#endif
zeertzjq9d997ad2024-07-29 21:10:07 +02003367 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
3368 || (mp->m_alt != NULL
3369 && STRNCMP(map_str, mp->m_alt->m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003370 (size_t)mp->m_alt->m_keylen) == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003371 noremap = REMAP_SKIP;
zeertzjq9d997ad2024-07-29 21:10:07 +02003372 else
3373 noremap = REMAP_YES;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003374 i = ins_typebuf(map_str, noremap,
3375 0, TRUE, cmd_silent || save_m_silent);
3376#ifdef FEAT_EVAL
3377 if (save_m_expr)
3378 vim_free(map_str);
3379#endif
3380 }
3381#ifdef FEAT_EVAL
3382 vim_free(save_m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003383 vim_free(save_alt_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003384#endif
3385 *keylenp = keylen;
3386 if (i == FAIL)
3387 return map_result_fail;
3388 return map_result_retry;
3389 }
3390
3391 *keylenp = keylen;
3392 return map_result_nomatch;
3393}
3394
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003395/*
3396 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003397 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003398 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003401vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
3403 old_char = c;
3404 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003405 old_mouse_row = mouse_row;
3406 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003407 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
3409
3410/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003411 * When peeking and not getting a character, reg_executing cannot be cleared
3412 * yet, so set a flag to clear it later.
3413 */
3414 static void
3415check_end_reg_executing(int advance)
3416{
3417 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3418 || pending_end_reg_executing))
3419 {
3420 if (advance)
3421 {
3422 reg_executing = 0;
3423 pending_end_reg_executing = FALSE;
3424 }
3425 else
3426 pending_end_reg_executing = TRUE;
3427 }
3428
3429}
3430
3431/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003432 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 * 1. from the stuffbuffer
3434 * This is used for abbreviated commands like "D" -> "d$".
3435 * Also used to redo a command for ".".
3436 * 2. from the typeahead buffer
3437 * Stores text obtained previously but not used yet.
3438 * Also stores the result of mappings.
3439 * Also used for the ":normal" command.
3440 * 3. from the user
3441 * This may do a blocking wait if "advance" is TRUE.
3442 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003443 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003444 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * KeyTyped is set to TRUE in the case the user typed the key.
3446 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003447 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003448 * Just look whether there is a character available.
3449 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 *
3451 * When "no_mapping" is zero, checks for mappings in the current mode.
3452 * Only returns one byte (of a multi-byte character).
3453 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3454 */
3455 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003456vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003458 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003459 int timedout = FALSE; // waited for more than 'timeoutlen'
3460 // for mapping to complete or
3461 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003462 int mapdepth = 0; // check for recursive mapping
3463 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003466 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#endif
3468 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003470 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 /*
3473 * This function doesn't work very well when called recursively. This may
3474 * happen though, because of:
3475 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3476 * there is a character available, which calls this function. In that
3477 * case we must return NUL, to indicate no character is available.
3478 * 2. A GUI callback function writes to the screen, causing a
3479 * wait_return().
3480 * Using ":normal" can also do this, but it saves the typeahead buffer,
3481 * thus it should be OK. But don't get a key from the user then.
3482 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003483 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 return NUL;
3485
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003486 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003491 typebuf_was_empty = FALSE;
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 init_typebuf();
3495 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003496 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 do
3498 {
3499/*
3500 * get a character: 1. from the stuffbuffer
3501 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003502 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 c = typeahead_char;
3505 if (advance)
3506 typeahead_char = 0;
3507 }
3508 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003509 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (c != NUL && !got_int)
3511 {
3512 if (advance)
3513 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003514 // KeyTyped = FALSE; When the command that stuffed something
3515 // was typed, behave like the stuffed command was typed.
3516 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 KeyStuffed = TRUE;
3518 }
3519 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003520 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 else
3523 {
3524 /*
3525 * Loop until we either find a matching mapped key, or we
3526 * are sure that it is not a mapped key.
3527 * If a mapped key sequence is found we go back to the start to
3528 * try re-mapping.
3529 */
3530 for (;;)
3531 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003532 long wait_time;
3533 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003534 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003535 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
3537 * ui_breakcheck() is slow, don't use it too often when
3538 * inside a mapping. But call it each time for typed
3539 * characters.
3540 */
3541 if (typebuf.tb_maplen)
3542 line_breakcheck();
3543 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003544 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (got_int)
3546 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003547 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003548 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /*
3551 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003552 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 * Otherwise we behave like having gotten a CTRL-C.
3554 * As a result typing CTRL-C in insert mode will
3555 * really insert a CTRL-C.
3556 */
3557 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003558 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 c = ESC;
3560 else
3561 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003562 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003564 if (advance)
3565 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003566 // Also record this character, it might be needed to
3567 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003568 *typebuf.tb_buf = c;
3569 gotchars(typebuf.tb_buf, 1);
3570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 cmd_silent = FALSE;
3572
3573 break;
3574 }
3575 else if (typebuf.tb_len > 0)
3576 {
3577 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003578 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003580 map_result_T result = handle_mapping(
3581 &keylen, &timedout, &mapdepth);
3582
3583 if (result == map_result_retry)
3584 // try mapping again
3585 continue;
3586 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003587 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003588 // failed, use the outer loop
3589 c = -1;
3590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003592 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594/*
3595 * get a character: 2. from the typeahead buffer
3596 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003597 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003598 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003599 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003600 cmd_silent = (typebuf.tb_silent > 0);
3601 if (typebuf.tb_maplen > 0)
3602 KeyTyped = FALSE;
3603 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003604 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003605 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003606 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003607 gotchars(typebuf.tb_buf
3608 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003609 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003610 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003611 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003612 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003613 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
3615
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003616 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618
3619/*
3620 * get a character: 3. from the user - handle <Esc> in Insert mode
3621 */
3622 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003623 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003625 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * typing an <ESC>. If we get something after all, we may
3627 * have to redisplay the mode. That the cursor is in the wrong
3628 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003629 * Do not do this if the kitty keyboard protocol is used, every
3630 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
3632 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 new_wcol = curwin->w_wcol;
3634 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if ( advance
3636 && typebuf.tb_len == 1
3637 && typebuf.tb_buf[typebuf.tb_off] == ESC
3638 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003639 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003642 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003643 && (p_timeout
3644 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003646 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003648 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 char_u *ptr;
3650
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003651 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 unshowmode(TRUE);
3654 mode_deleted = TRUE;
3655 }
3656#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003657 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003658 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 int save_State;
3661
3662 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003663 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 gui_update_cursor(TRUE, FALSE);
3665 State = save_State;
3666 shape_changed = TRUE;
3667 }
3668#endif
3669 validate_cursor();
3670 old_wcol = curwin->w_wcol;
3671 old_wrow = curwin->w_wrow;
3672
Bram Moolenaar30613902019-12-01 22:11:18 +01003673 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (curwin->w_cursor.col != 0)
3675 {
3676 if (curwin->w_wcol > 0)
3677 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003678 // After auto-indenting and no text is following,
3679 // we are expecting to truncate the trailing
3680 // white-space, so find the last non-white
3681 // character -- webb
3682 if (did_ai && *skipwhite(ml_get_curline()
3683 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003685 chartabsize_T cts;
3686
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003687 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003689 init_chartabsize_arg(&cts, curwin,
3690 curwin->w_cursor.lnum, 0, ptr, ptr);
3691 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003693 if (!VIM_ISWHITE(*cts.cts_ptr))
3694 curwin->w_wcol = cts.cts_vcol;
3695 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003697 cts.cts_ptr +=
3698 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003700 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003702 clear_chartabsize_arg(&cts);
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003705 + curwin->w_wcol / curwin->w_width;
3706 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003708 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710 else
3711 {
3712 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715 }
3716 else if (curwin->w_p_wrap && curwin->w_wrow)
3717 {
3718 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003719 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3723 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003724 // Correct when the cursor is on the right halve
3725 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 ptr = ml_get_curline();
3727 col -= (*mb_head_off)(ptr, ptr + col);
3728 if ((*mb_ptr2cells)(ptr + col) > 1)
3729 --curwin->w_wcol;
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 setcursor();
3733 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 new_wcol = curwin->w_wcol;
3735 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 curwin->w_wcol = old_wcol;
3737 curwin->w_wrow = old_wrow;
3738 }
3739 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003740 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003741
Bram Moolenaar30613902019-12-01 22:11:18 +01003742 // Allow mapping for just typed characters. When we get here c
3743 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003744 for (n = 1; n <= c; ++n)
3745 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 typebuf.tb_len += c;
3747
Bram Moolenaar30613902019-12-01 22:11:18 +01003748 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3750 {
3751 timedout = TRUE;
3752 continue;
3753 }
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 if (ex_normal_busy > 0)
3756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
Bram Moolenaar30613902019-12-01 22:11:18 +01003759 // No typeahead left and inside ":normal". Must return
3760 // something to avoid getting stuck. When an incomplete
3761 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (typebuf.tb_len > 0)
3763 {
3764 timedout = TRUE;
3765 continue;
3766 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003767
Bram Moolenaar30613902019-12-01 22:11:18 +01003768 // When 'insertmode' is set, ESC just beeps in Insert
3769 // mode. Use CTRL-L to make edit() return.
3770 // For the command line only CTRL-C always breaks it.
3771 // For the cmdline window: Alternate between ESC and
3772 // CTRL-C: ESC for most situations and CTRL-C to close the
3773 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003774 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003776#ifdef FEAT_TERMINAL
3777 else if (terminal_is_active())
3778 c = K_CANCEL;
3779#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003780 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003781 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 c = Ctrl_C;
3783 else
3784 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003786 // set a flag to indicate this wasn't a normal char
3787 if (advance)
3788 typebuf_was_empty = TRUE;
3789
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003790 // return from main_loop()
3791 if (pending_exmode_active)
3792 exmode_active = EXMODE_NORMAL;
3793
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003794 // no chars to block abbreviation for
3795 typebuf.tb_no_abbr_cnt = 0;
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 break;
3798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800/*
3801 * get a character: 3. from the user - update display
3802 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003803 // In insert mode a screen update is skipped when characters
3804 // are still available. But when those available characters
3805 // are part of a mapping, and we are going to do a blocking
3806 // wait here. Need to update the screen to display the
3807 // changed text so far. Also for when 'lazyredraw' is set and
3808 // redrawing was postponed because there was something in the
3809 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003810 if (((State & MODE_INSERT) != 0 || p_lz)
3811 && (State & MODE_CMDLINE) == 0
3812 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003815 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817
3818 /*
3819 * If we have a partial match (and are going to wait for more
3820 * input from the user), show the partially matched characters
3821 * to the user with showcmd.
3822 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003823 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003824 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (typebuf.tb_len > 0 && advance && !exmode_active)
3826 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003827 if (((State & (MODE_NORMAL | MODE_INSERT))
3828 || State == MODE_LANGMAP)
3829 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003831 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003832 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3834 + typebuf.tb_len - 1) == 1)
3835 {
3836 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3837 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003838 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003839 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003841 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 old_wcol = curwin->w_wcol;
3843 old_wrow = curwin->w_wrow;
3844 curwin->w_wcol = new_wcol;
3845 curwin->w_wrow = new_wrow;
3846 push_showcmd();
3847 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003848 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3849 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003850 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003851 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 curwin->w_wcol = old_wcol;
3853 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003856 // This looks nice when typing a dead character map.
3857 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003858 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003859 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3861 && cmdline_star == 0
3862#endif
3863 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3864 + typebuf.tb_len - 1) == 1)
3865 {
3866 putcmdline(typebuf.tb_buf[typebuf.tb_off
3867 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003868 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 }
3871
3872/*
3873 * get a character: 3. from the user - get it
3874 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003875 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003876 // timedout may have been set if a mapping with empty RHS
3877 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003878 timedout = FALSE;
3879
Bram Moolenaar652de232019-04-04 20:13:09 +02003880 if (advance)
3881 {
3882 if (typebuf.tb_len == 0
3883 || !(p_timeout
3884 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3885 // blocking wait
3886 wait_time = -1L;
3887 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3888 wait_time = p_ttm;
3889 else
3890 wait_time = p_tm;
3891 }
3892 else
3893 wait_time = 0;
3894
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003895 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3897 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003898 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
Bram Moolenaareda35f72019-08-03 14:59:44 +02003900 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003902 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003904 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003906 if ((State & MODE_CMDLINE)
3907 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003909 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003910 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912
3913 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003914 continue; // end of input script reached
3915 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
3917 if (!advance)
3918 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003919 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921 timedout = TRUE;
3922 continue;
3923 }
3924 }
3925 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003926 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 while (typebuf.tb_buf[typebuf.tb_off
3928 + typebuf.tb_len] != NUL)
3929 typebuf.tb_noremap[typebuf.tb_off
3930 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003931#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003932 // Get IM status right after getting keys, not after the
3933 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 vgetc_im_active = im_get_status();
3935#endif
3936 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003937 } // for (;;)
3938 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
Bram Moolenaar30613902019-12-01 22:11:18 +01003940 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003941 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 /*
3944 * The "INSERT" message is taken care of here:
3945 * if we return an ESC to exit insert mode, the message is deleted
3946 * if we don't return an ESC but deleted the message before, redisplay it
3947 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003948 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003950 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003953 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 else
3955 unshowmode(FALSE);
3956 }
3957 else if (c != ESC && mode_deleted)
3958 {
3959 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003960 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
3962 showmode();
3963 }
3964 }
3965#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003966 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003967 if (gui.in_use && shape_changed)
3968 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003970 if (timedout && c == ESC)
3971 {
zeertzjqbf321802024-01-28 19:03:00 +01003972 // When recording there will be no timeout. Add an <Ignore> after the
3973 // ESC to avoid that it forms a key code with following characters.
3974 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003977 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 return c;
3980}
3981
3982/*
3983 * inchar() - get one character from
3984 * 1. a scriptfile
3985 * 2. the keyboard
3986 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003987 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * NUL terminated (buffer length must be 'maxlen' + 1).
3989 * Minimum for "maxlen" is 3!!!!
3990 *
3991 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3992 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3993 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3994 * otherwise.
3995 *
3996 * If we got an interrupt all input is read until none is available.
3997 *
3998 * If wait_time == 0 there is no waiting for the char.
3999 * If wait_time == n we wait for n msec for a character to arrive.
4000 * If wait_time == -1 we wait forever for a character to arrive.
4001 *
4002 * Return the number of obtained characters.
4003 * Return -1 when end of input script reached.
4004 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02004005 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004006inchar(
4007 char_u *buf,
4008 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004009 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
Bram Moolenaar30613902019-12-01 22:11:18 +01004011 int len = 0; // init for GCC
4012 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004014 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar30613902019-12-01 22:11:18 +01004016 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
4018 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004019 out_flush_cursor(FALSE, FALSE);
4020#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
4021 if (gui.in_use && postponed_mouseshape)
4022 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
4024 }
4025
4026 /*
4027 * Don't reset these when at the hit-return prompt, otherwise a endless
4028 * recursive loop may result (write error in swapfile, hit-return, timeout
4029 * on char wait, flush swapfile, write error....).
4030 */
Bram Moolenaar24959102022-05-07 20:01:16 +01004031 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004033 did_outofmem_msg = FALSE; // display out of memory message (again)
4034 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004036 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004039 * Get a character from a script file if there is one.
4040 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 */
4042 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00004043 while (scriptin[curscript] != NULL && script_char < 0
4044#ifdef FEAT_EVAL
4045 && !ignore_script
4046#endif
4047 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02004049#ifdef MESSAGE_QUEUE
4050 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00004051#endif
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
4054 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004055 // Reached EOF.
4056 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
4057 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 closescript();
4059 /*
4060 * When reading script file is interrupted, return an ESC to get
4061 * back to normal mode.
4062 * Otherwise return -1, because typebuf.tb_buf[] has changed.
4063 */
4064 if (got_int)
4065 retesc = TRUE;
4066 else
4067 return -1;
4068 }
4069 else
4070 {
4071 buf[0] = script_char;
4072 len = 1;
4073 }
4074 }
4075
Bram Moolenaar30613902019-12-01 22:11:18 +01004076 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 /*
4079 * If we got an interrupt, skip all previously typed characters and
4080 * return TRUE if quit reading script file.
4081 * Stop reading typeahead when a single CTRL-C was read,
4082 * fill_input_buf() returns this when not able to read from stdin.
4083 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
4084 * and buf may be pointing inside typebuf.tb_buf[].
4085 */
4086 if (got_int)
4087 {
kylo252ae6f1d82022-02-16 19:24:07 +00004088#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 char_u dum[DUM_LEN + 1];
4090
4091 for (;;)
4092 {
4093 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01004094 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 break;
4096 }
4097 return retesc;
4098 }
4099
4100 /*
4101 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004102 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004104 if (wait_time == -1L || wait_time > 10L)
4105 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 /*
4108 * Fill up to a third of the buffer, because each character may be
4109 * tripled below.
4110 */
4111 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
4112 }
4113
Bram Moolenaar30613902019-12-01 22:11:18 +01004114 // If the typebuf was changed further down, it is like nothing was added by
4115 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (typebuf_changed(tb_change_cnt))
4117 return 0;
4118
Bram Moolenaar30613902019-12-01 22:11:18 +01004119 // Note the change in the typeahead buffer, this matters for when
4120 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4121 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004122 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4123 typebuf.tb_change_cnt = 1;
4124
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004125 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126}
4127
4128/*
4129 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004130 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 * Returns the new length.
4132 */
4133 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004134fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 int i;
4137 char_u *p = buf;
4138
4139 /*
4140 * Two characters are special: NUL and K_SPECIAL.
4141 * When compiled With the GUI CSI is also special.
4142 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4143 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4144 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 */
4146 for (i = len; --i >= 0; ++p)
4147 {
4148#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004149 // When the GUI is used any character can come after a CSI, don't
4150 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 if (gui.in_use && p[0] == CSI && i >= 2)
4152 {
4153 p += 2;
4154 i -= 2;
4155 }
zeertzjq646bb722022-02-16 17:51:47 +00004156# ifndef MSWIN
4157 // When not on MS-Windows and the GUI is not used CSI needs to be
4158 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 else if (!gui.in_use && p[0] == CSI)
4160 {
4161 mch_memmove(p + 3, p + 1, (size_t)i);
4162 *p++ = K_SPECIAL;
4163 *p++ = KS_EXTRA;
4164 *p = (int)KE_CSI;
4165 len += 2;
4166 }
zeertzjq646bb722022-02-16 17:51:47 +00004167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 else
4169#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004170 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004171 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004172 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004173#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004174 // Win32 console passes modifiers
4175 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004176# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004177 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004178# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004179 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180#endif
4181 ))
4182 {
4183 mch_memmove(p + 3, p + 1, (size_t)i);
4184 p[2] = K_THIRD(p[0]);
4185 p[1] = K_SECOND(p[0]);
4186 p[0] = K_SPECIAL;
4187 p += 2;
4188 len += 2;
4189 }
4190 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004191 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return len;
4193}
4194
4195#if defined(USE_INPUT_BUF) || defined(PROTO)
4196/*
4197 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4198 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004199 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004200 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
4202 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004203input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004206# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4207 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208# endif
4209 );
4210}
4211#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004212
4213/*
4214 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4215 * typeahead.
4216 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004217 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004218getcmdkeycmd(
4219 int promptc UNUSED,
4220 void *cookie UNUSED,
4221 int indent UNUSED,
4222 getline_opt_T do_concat UNUSED)
4223{
4224 garray_T line_ga;
4225 int c1 = -1;
4226 int c2;
4227 int cmod = 0;
4228 int aborted = FALSE;
4229
4230 ga_init2(&line_ga, 1, 32);
4231
4232 // no mapping for these characters
4233 no_mapping++;
4234
4235 got_int = FALSE;
4236 while (c1 != NUL && !aborted)
4237 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004238 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004239 {
4240 aborted = TRUE;
4241 break;
4242 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004243
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004244 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004245 {
4246 // incomplete <Cmd> is an error, because there is not much the user
4247 // could do in this state.
4248 emsg(_(e_cmd_mapping_must_end_with_cr));
4249 aborted = TRUE;
4250 break;
4251 }
4252
4253 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004254 c1 = vgetorpeek(TRUE);
4255
Bram Moolenaar957cf672020-11-12 14:21:06 +01004256 // Get two extra bytes for special keys
4257 if (c1 == K_SPECIAL)
4258 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004259 c1 = vgetorpeek(TRUE);
4260 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004261 if (c1 == KS_MODIFIER)
4262 {
4263 cmod = c2;
4264 continue;
4265 }
4266 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004267
4268 // K_ESC is used to avoid ambiguity with the single Esc character
4269 // that might be the start of an escape sequence. Convert it back
4270 // to a single Esc here.
4271 if (c1 == K_ESC)
4272 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004273 }
4274
4275 if (got_int)
4276 aborted = TRUE;
4277 else if (c1 == '\r' || c1 == '\n')
4278 c1 = NUL; // end the line
4279 else if (c1 == ESC)
4280 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004281 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004282 {
4283 // give a nicer error message for this special case
4284 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4285 aborted = TRUE;
4286 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004287 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004288 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004289 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004290 }
4291 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004292 {
4293 if (cmod != 0)
4294 {
4295 ga_append(&line_ga, K_SPECIAL);
4296 ga_append(&line_ga, KS_MODIFIER);
4297 ga_append(&line_ga, cmod);
4298 }
4299 if (IS_SPECIAL(c1))
4300 {
4301 ga_append(&line_ga, K_SPECIAL);
4302 ga_append(&line_ga, K_SECOND(c1));
4303 ga_append(&line_ga, K_THIRD(c1));
4304 }
4305 else
4306 ga_append(&line_ga, c1);
4307 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004308
4309 cmod = 0;
4310 }
4311
4312 no_mapping--;
4313
4314 if (aborted)
4315 ga_clear(&line_ga);
4316
4317 return (char_u *)line_ga.ga_data;
4318}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004319
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004320#if defined(FEAT_EVAL) || defined(PROTO)
4321/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004322 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4323 * is set when redo'ing.
4324 * Put this SID in the redo buffer, so that "." will use the same script
4325 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004326 */
4327 void
4328may_add_last_used_map_to_redobuff(void)
4329{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004330 char_u buf[3 + 20];
John Marriotte7a1bbf2024-11-11 20:40:33 +01004331 int buflen;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004332 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004333
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004334 if (last_used_map != NULL)
4335 sid = last_used_map->m_script_ctx.sc_sid;
4336 if (sid < 0)
4337 sid = last_used_sid;
4338
4339 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004340 return;
4341
4342 // <K_SID>{nr};
4343 buf[0] = K_SPECIAL;
4344 buf[1] = KS_EXTRA;
4345 buf[2] = KE_SID;
John Marriotte7a1bbf2024-11-11 20:40:33 +01004346 buflen = 3;
4347
4348 buflen += vim_snprintf((char *)buf + 3, 20, "%d;", sid);
4349 add_buff(&redobuff, buf, (long)buflen);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004350}
4351#endif
4352
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004353 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004354do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004355{
4356 int res;
4357#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004358 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004359
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004360 if (key == K_SCRIPT_COMMAND
4361 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004362 {
4363 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004364 if (last_used_map != NULL)
4365 current_sctx = last_used_map->m_script_ctx;
4366 else
4367 {
4368 current_sctx.sc_sid = last_used_sid;
4369 current_sctx.sc_lnum = 0;
4370 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4371 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004372 }
4373#endif
4374
4375 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4376
4377#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004378 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004379 current_sctx = save_current_sctx;
4380#endif
4381
4382 return res;
4383}
4384
4385#if defined(FEAT_EVAL) || defined(PROTO)
4386 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004387reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004388{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004389 if (last_used_map != mp)
4390 return;
4391
4392 last_used_map = NULL;
4393 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004394}
4395#endif