blob: 54222ec30878002f95b43249f75cf1764589f6ed [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar285e3352018-04-18 23:01:13 +020039static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
40static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
41static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Shougo Matsushitafcc1b572024-07-17 20:25:22 +020045#ifdef FEAT_EVAL
46static char_u typedchars[MAXMAPLEN + 1] = { NUL }; // typed chars before map
47static int typedchars_pos = 0;
48#endif
49
Bram Moolenaar071d4272004-06-13 20:20:40 +000050/*
zeertzjq30b6d612023-05-07 17:39:23 +010051 * When block_redo is TRUE the redo buffer will not be changed.
52 * Used by edit() to repeat insertions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 */
54static int block_redo = FALSE;
55
Bram Moolenaar459fd782019-10-13 16:43:39 +020056static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020059 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 *
61 * typebuf.tb_buf[] contains all characters that are not consumed yet.
62 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
64 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
65 * The head of the buffer may contain the result of mappings, abbreviations
66 * and @a commands. The length of this part is typebuf.tb_maplen.
67 * typebuf.tb_silent is the part where <silent> applies.
68 * After the head are characters that come from the terminal.
69 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
70 * should not be considered for abbreviations.
71 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
72 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
73 * contains RM_NONE for the characters that are not to be remapped.
74 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
75 * (typebuf has been put in globals.h, because check_termcode() needs it).
76 */
Bram Moolenaar30613902019-12-01 22:11:18 +010077#define RM_YES 0 // tb_noremap: remap
78#define RM_NONE 1 // tb_noremap: don't remap
79#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
80#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar30613902019-12-01 22:11:18 +010082// typebuf.tb_buf has three parts: room in front (for result of mappings), the
83// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000084// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010086static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
87static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
Mike Williamsaca8f552024-04-07 18:26:22 +020089static size_t last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
Bram Moolenaare32c3c42022-01-15 18:26:04 +000091#ifdef FEAT_EVAL
92mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010093int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000094#endif
95
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020099static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200101static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +0200103static int inchar(char_u *buf, int maxlen, long wait_time);
Shougo Matsushita83678842024-07-11 22:05:12 +0200104#ifdef FEAT_EVAL
105static int do_key_input_pre(int c);
106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108/*
109 * Free and clear a buffer.
110 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200111 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100112free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100114 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
Bram Moolenaar285e3352018-04-18 23:01:13 +0200116 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 {
118 np = p->b_next;
119 vim_free(p);
120 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200121 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000122 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123}
124
125/*
126 * Return the contents of a buffer as a single string.
127 * K_SPECIAL and CSI in the returned string are escaped.
128 */
129 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100130get_buffcont(
131 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100132 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
134 long_u count = 0;
135 char_u *p = NULL;
136 char_u *p2;
137 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200138 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar30613902019-12-01 22:11:18 +0100140 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200141 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 count += (long_u)STRLEN(bp->b_str);
143
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100144 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 {
146 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200147 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 for (str = bp->b_str; *str; )
149 *p2++ = *str++;
150 *p2 = NUL;
151 }
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100152 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153}
154
155/*
156 * Return the contents of the record buffer as a single string
157 * and clear the record buffer.
158 * K_SPECIAL and CSI in the returned string are escaped.
159 */
160 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100161get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
163 char_u *p;
164 size_t len;
165
166 p = get_buffcont(&recordbuff, TRUE);
167 free_buff(&recordbuff);
168
169 /*
170 * Remove the characters that were added the last time, these must be the
171 * (possibly mapped) characters that stopped the recording.
172 */
173 len = STRLEN(p);
Mike Williamsaca8f552024-04-07 18:26:22 +0200174 if (len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
176 len -= last_recorded_len;
177 p[len] = NUL;
178 }
179
180 /*
181 * When stopping recording from Insert mode with CTRL-O q, also remove the
182 * CTRL-O.
183 */
184 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
185 p[len - 1] = NUL;
186
187 return (p);
188}
189
190/*
191 * Return the contents of the redo buffer as a single string.
192 * K_SPECIAL and CSI in the returned string are escaped.
193 */
194 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100195get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000197 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198}
199
200/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000201 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 * K_SPECIAL and CSI should have been escaped already.
203 */
204 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100205add_buff(
206 buffheader_T *buf,
207 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100208 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100210 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200211 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
213 if (slen < 0)
214 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100215 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 return;
217
Bram Moolenaar30613902019-12-01 22:11:18 +0100218 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {
220 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200221 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100223 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100225 iemsg(e_add_to_internal_buffer_that_was_already_read_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 return;
227 }
228 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200229 mch_memmove(buf->bh_first.b_next->b_str,
230 buf->bh_first.b_next->b_str + buf->bh_index,
231 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 buf->bh_index = 0;
233
234 if (buf->bh_space >= (int)slen)
235 {
236 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000237 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 buf->bh_space -= slen;
239 }
240 else
241 {
242 if (slen < MINIMAL_SIZE)
243 len = MINIMAL_SIZE;
244 else
245 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200246 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100248 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000249 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000250 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251
Bram Moolenaar285e3352018-04-18 23:01:13 +0200252 p->b_next = buf->bh_curr->b_next;
253 buf->bh_curr->b_next = p;
254 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256}
257
258/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000259 * Delete "slen" bytes from the end of "buf".
260 * Only works when it was just added.
261 */
262 static void
263delete_buff_tail(buffheader_T *buf, int slen)
264{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000265 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000266
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000267 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000268 return; // nothing to delete
269 len = (int)STRLEN(buf->bh_curr->b_str);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000270 if (len < slen)
271 return;
272
273 buf->bh_curr->b_str[len - slen] = NUL;
274 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000275}
276
277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 * Add number "n" to buffer "buf".
279 */
280 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100281add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283 char_u number[32];
284
285 sprintf((char *)number, "%ld", n);
286 add_buff(buf, number, -1L);
287}
288
289/*
290 * Add character 'c' to buffer "buf".
291 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
292 */
293 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100294add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 char_u bytes[MB_MAXBYTES + 1];
297 int len;
298 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 char_u temp[4];
300
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 if (IS_SPECIAL(c))
302 len = 1;
303 else
304 len = (*mb_char2bytes)(c, bytes);
305 for (i = 0; i < len; ++i)
306 {
307 if (!IS_SPECIAL(c))
308 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
310 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
311 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100312 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 temp[0] = K_SPECIAL;
314 temp[1] = K_SECOND(c);
315 temp[2] = K_THIRD(c);
316 temp[3] = NUL;
317 }
318#ifdef FEAT_GUI
319 else if (c == CSI)
320 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100321 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 temp[0] = CSI;
323 temp[1] = KS_EXTRA;
324 temp[2] = (int)KE_CSI;
325 temp[3] = NUL;
326 }
327#endif
328 else
329 {
330 temp[0] = c;
331 temp[1] = NUL;
332 }
333 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335}
336
Bram Moolenaar30613902019-12-01 22:11:18 +0100337// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200338static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100339
Bram Moolenaar30613902019-12-01 22:11:18 +0100340// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200341static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100342
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100344 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
345 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 * If advance == TRUE go to the next char.
347 * No translation is done K_SPECIAL and CSI are escaped.
348 */
349 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100350read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100352 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100354 c = read_readbuf(&readbuf1, advance);
355 if (c == NUL)
356 c = read_readbuf(&readbuf2, advance);
357 return c;
358}
359
360 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100361read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100362{
363 char_u c;
364 buffblock_T *curr;
365
Bram Moolenaar30613902019-12-01 22:11:18 +0100366 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 return NUL;
368
Bram Moolenaar285e3352018-04-18 23:01:13 +0200369 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100370 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372 if (advance)
373 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100374 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200376 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100378 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
380 }
381 return c;
382}
383
384/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100385 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 */
387 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100388start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200390 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200392 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100393 readbuf1.bh_space = 0;
394 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200395 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100396 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200397 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100398 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400}
401
402/*
403 * Return TRUE if the stuff buffer is empty.
404 */
405 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100406stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200408 return (readbuf1.bh_first.b_next == NULL
409 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100410}
411
Bram Moolenaar113e1072019-01-20 15:30:40 +0100412#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100413/*
414 * Return TRUE if readbuf1 is empty. There may still be redo characters in
415 * redbuf2.
416 */
417 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100418readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100419{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200420 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
424/*
425 * Set a typeahead character that won't be flushed.
426 */
427 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100428typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 typeahead_char = c;
431}
432
433/*
434 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100435 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 * flush all typeahead characters (used when interrupted by a CTRL-C).
437 */
438 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200439flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
441 init_typebuf();
442
443 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100444 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 ;
446
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200447 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200449 // remove mapped characters at the start only
450 typebuf.tb_off += typebuf.tb_maplen;
451 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100452#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
453 if (typebuf.tb_len == 0)
454 typebuf_was_filled = FALSE;
455#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200456 }
457 else
458 {
459 // remove typeahead
460 if (flush_typeahead == FLUSH_INPUT)
461 // We have to get all characters, because we may delete the first
462 // part of an escape sequence. In an xterm we get one char at a
463 // time and we have to get them all.
464 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
465 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 typebuf.tb_off = MAXMAPLEN;
467 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200468#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100469 // Reset the flag that text received from a client or from feedkeys()
470 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200471 typebuf_was_filled = FALSE;
472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 typebuf.tb_maplen = 0;
475 typebuf.tb_silent = 0;
476 cmd_silent = FALSE;
477 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200478 if (++typebuf.tb_change_cnt == 0)
479 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480}
481
482/*
483 * The previous contents of the redo buffer is kept in old_redobuffer.
484 * This is used for the CTRL-O <.> command in insert mode.
485 */
486 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100487ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000489 if (block_redo)
490 return;
491
492 free_buff(&old_redobuff);
493 old_redobuff = redobuff;
494 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495}
496
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100497/*
498 * Discard the contents of the redo buffer and restore the previous redo
499 * buffer.
500 */
501 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100502CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100503{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000504 if (block_redo)
505 return;
506
507 free_buff(&redobuff);
508 redobuff = old_redobuff;
509 old_redobuff.bh_first.b_next = NULL;
510 start_stuff();
511 while (read_readbuffers(TRUE) != NUL)
512 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100513}
514
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515/*
516 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
517 * Used before executing autocommands and user functions.
518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200520saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522 char_u *s;
523
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200524 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200525 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200526 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200527 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaar30613902019-12-01 22:11:18 +0100529 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200530 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000531 if (s == NULL)
532 return;
533
534 add_buff(&redobuff, s, -1L);
535 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536}
537
538/*
539 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
540 * Used after executing autocommands and user functions.
541 */
542 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200543restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200545 free_buff(&redobuff);
546 redobuff = save_redo->sr_redobuff;
547 free_buff(&old_redobuff);
548 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551/*
552 * Append "s" to the redo buffer.
553 * K_SPECIAL and CSI should already have been escaped.
554 */
555 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100556AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 if (!block_redo)
559 add_buff(&redobuff, s, -1L);
560}
561
562/*
563 * Append to Redo buffer literally, escaping special characters with CTRL-V.
564 * K_SPECIAL and CSI are escaped as well.
565 */
566 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100567AppendToRedobuffLit(
568 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100569 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000571 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 int c;
573 char_u *start;
574
575 if (block_redo)
576 return;
577
Bram Moolenaarebefac62005-12-28 22:39:57 +0000578 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100580 // Put a string of normal characters in the redo buffer (that's
581 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000583 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 ++s;
585
Bram Moolenaar30613902019-12-01 22:11:18 +0100586 // Don't put '0' or '^' as last character, just in case a CTRL-D is
587 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
589 --s;
590 if (s > start)
591 add_buff(&redobuff, start, (long)(s - start));
592
Bram Moolenaarebefac62005-12-28 22:39:57 +0000593 if (*s == NUL || (len >= 0 && s - str >= len))
594 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
Bram Moolenaar30613902019-12-01 22:11:18 +0100596 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000597 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100598 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000599 c = mb_cptr2char_adv(&s);
600 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000601 c = *s++;
602 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
603 add_char_buff(&redobuff, Ctrl_V);
604
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000605 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000606 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000607 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000608 else
609 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611}
612
613/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100614 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
615 * and escaping other K_SPECIAL and CSI bytes.
616 */
617 void
618AppendToRedobuffSpec(char_u *s)
619{
zeertzjq30b6d612023-05-07 17:39:23 +0100620 if (block_redo)
621 return;
622
zeertzjq3ab3a862023-05-06 16:22:04 +0100623 while (*s != NUL)
624 {
625 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
626 {
zeertzjq30b6d612023-05-07 17:39:23 +0100627 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100628 add_buff(&redobuff, s, 3L);
629 s += 3;
630 }
631 else
632 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
633 }
634}
635
636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 * Append a character to the redo buffer.
638 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
639 */
640 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100641AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 if (!block_redo)
644 add_char_buff(&redobuff, c);
645}
646
647/*
648 * Append a number to the redo buffer.
649 */
650 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100651AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 if (!block_redo)
654 add_num_buff(&redobuff, n);
655}
656
657/*
658 * Append string "s" to the stuff buffer.
659 * CSI and K_SPECIAL must already have been escaped.
660 */
661 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100662stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100664 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665}
666
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200667/*
668 * Append string "s" to the redo stuff buffer.
669 * CSI and K_SPECIAL must already have been escaped.
670 */
671 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100672stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200673{
674 add_buff(&readbuf2, s, -1L);
675}
676
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200677 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100678stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100680 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681}
682
683#if defined(FEAT_EVAL) || defined(PROTO)
684/*
685 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
686 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200687 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 */
689 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100690stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200692 int c;
693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 while (*s != NUL)
695 {
696 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
697 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100698 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 stuffReadbuffLen(s, 3L);
700 s += 3;
701 }
702 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200703 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100704 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200705 if (c == CAR || c == NL || c == ESC)
706 c = ' ';
707 stuffcharReadbuff(c);
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710}
711#endif
712
713/*
714 * Append a character to the stuff buffer.
715 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
716 */
717 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100718stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721}
722
723/*
724 * Append a number to the stuff buffer.
725 */
726 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100727stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100729 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730}
731
732/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200733 * Stuff a string into the typeahead buffer, such that edit() will insert it
734 * literally ("literally" TRUE) or interpret is as typed characters.
735 */
736 void
737stuffescaped(char_u *arg, int literally)
738{
739 int c;
740 char_u *start;
741
742 while (*arg != NUL)
743 {
744 // Stuff a sequence of normal ASCII characters, that's fast. Also
745 // stuff K_SPECIAL to get the effect of a special key when "literally"
746 // is TRUE.
747 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000748 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200749 || (*arg == K_SPECIAL && !literally))
750 ++arg;
751 if (arg > start)
752 stuffReadbuffLen(start, (long)(arg - start));
753
754 // stuff a single special character
755 if (*arg != NUL)
756 {
757 if (has_mbyte)
758 c = mb_cptr2char_adv(&arg);
759 else
760 c = *arg++;
761 if (literally && ((c < ' ' && c != TAB) || c == DEL))
762 stuffcharReadbuff(Ctrl_V);
763 stuffcharReadbuff(c);
764 }
765 }
766}
767
768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
770 * multibyte characters.
771 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100772 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
773 * otherwise.
774 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 */
776 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100777read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100779 static buffblock_T *bp;
780 static char_u *p;
781 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100782 int n;
783 char_u buf[MB_MAXBYTES + 1];
784 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 if (init)
787 {
788 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200789 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200791 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (bp == NULL)
793 return FAIL;
794 p = bp->b_str;
795 return OK;
796 }
797 if ((c = *p) != NUL)
798 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100799 // Reverse the conversion done by add_char_buff()
800 // For a multi-byte character get all the bytes and return the
801 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
803 n = MB_BYTE2LEN_CHECK(c);
804 else
805 n = 1;
806 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100808 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
810 c = TO_SPECIAL(p[1], p[2]);
811 p += 2;
812 }
813#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100814 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 p += 2;
816#endif
817 if (*++p == NUL && bp->b_next != NULL)
818 {
819 bp = bp->b_next;
820 p = bp->b_str;
821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100823 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
825 if (n != 1)
826 c = (*mb_ptr2char)(buf);
827 break;
828 }
829 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100830 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 }
834
835 return c;
836}
837
838/*
839 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
840 * If old_redo is TRUE, use old_redobuff instead of redobuff.
841 * The escaped K_SPECIAL and CSI are copied without translation.
842 */
843 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100844copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 int c;
847
848 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100849 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100853 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 * Insert the redo count into the command.
855 * If "old_redo" is TRUE, the last but one command is repeated
856 * instead of the last command (inserting text). This is used for
857 * CTRL-O <.> in insert mode
858 *
859 * return FAIL for failure, OK otherwise
860 */
861 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100862start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
864 int c;
865
Bram Moolenaar30613902019-12-01 22:11:18 +0100866 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (read_redo(TRUE, old_redo) == FAIL)
868 return FAIL;
869
870 c = read_redo(FALSE, old_redo);
871
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100872#ifdef FEAT_EVAL
873 if (c == K_SID)
874 {
875 // Copy the <SID>{sid}; sequence
876 add_char_buff(&readbuf2, c);
877 for (;;)
878 {
879 c = read_redo(FALSE, old_redo);
880 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100881 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100882 break;
883 }
884 c = read_redo(FALSE, old_redo);
885 }
886#endif
887
Bram Moolenaar30613902019-12-01 22:11:18 +0100888 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (c == '"')
890 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100891 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 c = read_redo(FALSE, old_redo);
893
Bram Moolenaar30613902019-12-01 22:11:18 +0100894 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (c >= '1' && c < '9')
896 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100897 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200898
Bram Moolenaar30613902019-12-01 22:11:18 +0100899 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200900 if (c == '=')
901 {
902 add_char_buff(&readbuf2, CAR);
903 cmd_silent = TRUE;
904 }
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 c = read_redo(FALSE, old_redo);
907 }
908
Bram Moolenaar30613902019-12-01 22:11:18 +0100909 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
911 VIsual = curwin->w_cursor;
912 VIsual_active = TRUE;
913 VIsual_select = FALSE;
914 VIsual_reselect = TRUE;
915 redo_VIsual_busy = TRUE;
916 c = read_redo(FALSE, old_redo);
917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaar30613902019-12-01 22:11:18 +0100919 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (count)
921 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100922 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100924 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100927 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100928 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 copy_redo(old_redo);
930 return OK;
931}
932
933/*
934 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100935 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * return FAIL for failure, OK otherwise
937 */
938 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100939start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
941 int c;
942
943 if (read_redo(TRUE, FALSE) == FAIL)
944 return FAIL;
945 start_stuff();
946
Bram Moolenaar30613902019-12-01 22:11:18 +0100947 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 while ((c = read_redo(FALSE, FALSE)) != NUL)
949 {
950 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
951 {
952 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100953 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 break;
955 }
956 }
957
Bram Moolenaar30613902019-12-01 22:11:18 +0100958 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 copy_redo(FALSE);
960 block_redo = TRUE;
961 return OK;
962}
963
964 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100965stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 block_redo = FALSE;
968}
969
970/*
971 * Initialize typebuf.tb_buf to point to typebuf_init.
972 * alloc() cannot be used here: In out-of-memory situations it would
973 * be impossible to type anything.
974 */
975 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100976init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000978 if (typebuf.tb_buf != NULL)
979 return;
980
981 typebuf.tb_buf = typebuf_init;
982 typebuf.tb_noremap = noremapbuf_init;
983 typebuf.tb_buflen = TYPELEN_INIT;
984 typebuf.tb_len = 0;
985 typebuf.tb_off = MAXMAPLEN + 4;
986 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988
989/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200990 * Returns TRUE when keys cannot be remapped.
991 */
992 int
993noremap_keys(void)
994{
995 return KeyNoremap & (RM_NONE|RM_SCRIPT);
996}
997
998/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100999 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
1000 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001002 * If "noremap" is REMAP_YES, new string can be mapped again.
1003 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
1004 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001005 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001006 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001008 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001010 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1011 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001013 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 *
1015 * return FAIL for failure, OK otherwise
1016 */
1017 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001018ins_typebuf(
1019 char_u *str,
1020 int noremap,
1021 int offset,
1022 int nottyped,
1023 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 char_u *s1, *s2;
1026 int newlen;
1027 int addlen;
1028 int i;
1029 int newoff;
1030 int val;
1031 int nrm;
1032
1033 init_typebuf();
1034 if (++typebuf.tb_change_cnt == 0)
1035 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001036 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (offset == 0 && addlen <= typebuf.tb_off)
1041 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001042 /*
1043 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 typebuf.tb_off -= addlen;
1046 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1047 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001048 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1049 >= addlen + 3 * (MAXMAPLEN + 4))
1050 {
1051 /*
1052 * Buffer is empty and string fits in the existing buffer.
1053 * Leave some space before and after, if possible.
1054 */
1055 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1056 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 else
1059 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001060 int extra;
1061
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001062 /*
1063 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001064 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001065 * characters. We add some extra room to avoid having to allocate too
1066 * often.
1067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001069 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1070 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001072 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001073 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 setcursor();
1075 return FAIL;
1076 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001077 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001079 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 return FAIL;
1081 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001082 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 vim_free(s1);
1085 return FAIL;
1086 }
1087 typebuf.tb_buflen = newlen;
1088
Bram Moolenaar30613902019-12-01 22:11:18 +01001089 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1091 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001092 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001094 // copy the old chars, after the insertion point, including the NUL at
1095 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 mch_memmove(s1 + newoff + offset + addlen,
1097 typebuf.tb_buf + typebuf.tb_off + offset,
1098 (size_t)(typebuf.tb_len - offset + 1));
1099 if (typebuf.tb_buf != typebuf_init)
1100 vim_free(typebuf.tb_buf);
1101 typebuf.tb_buf = s1;
1102
1103 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1104 (size_t)offset);
1105 mch_memmove(s2 + newoff + offset + addlen,
1106 typebuf.tb_noremap + typebuf.tb_off + offset,
1107 (size_t)(typebuf.tb_len - offset));
1108 if (typebuf.tb_noremap != noremapbuf_init)
1109 vim_free(typebuf.tb_noremap);
1110 typebuf.tb_noremap = s2;
1111
1112 typebuf.tb_off = newoff;
1113 }
1114 typebuf.tb_len += addlen;
1115
Bram Moolenaar30613902019-12-01 22:11:18 +01001116 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (noremap == REMAP_SCRIPT)
1118 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001119 else if (noremap == REMAP_SKIP)
1120 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else
1122 val = RM_NONE;
1123
1124 /*
1125 * Adjust typebuf.tb_noremap[] for the new characters:
1126 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1127 * (sometimes) not remappable
1128 * If noremap == REMAP_YES: all the new characters are mappable
1129 * If noremap > 0: "noremap" characters are not remappable, the rest
1130 * mappable
1131 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001132 if (noremap == REMAP_SKIP)
1133 nrm = 1;
1134 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 nrm = addlen;
1136 else
1137 nrm = noremap;
1138 for (i = 0; i < addlen; ++i)
1139 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1140 (--nrm >= 0) ? val : RM_YES;
1141
Bram Moolenaar30613902019-12-01 22:11:18 +01001142 // tb_maplen and tb_silent only remember the length of mapped and/or
1143 // silent mappings at the start of the buffer, assuming that a mapped
1144 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (nottyped || typebuf.tb_maplen > offset)
1146 typebuf.tb_maplen += addlen;
1147 if (silent || typebuf.tb_silent > offset)
1148 {
1149 typebuf.tb_silent += addlen;
1150 cmd_silent = TRUE;
1151 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001152 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 typebuf.tb_no_abbr_cnt += addlen;
1154
1155 return OK;
1156}
1157
1158/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001159 * Put character "c" back into the typeahead buffer.
1160 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001161 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1162 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001163 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001164 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001165 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001166ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001167{
zeertzjq6cac7702022-01-04 18:01:21 +00001168 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001169 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001170
zeertzjq2e7cba32022-06-10 15:30:32 +01001171 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001172 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001173 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001174}
1175
1176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001178 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001179 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1181 * changed it was reallocated and the old pointer can no longer be used.
1182 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1183 * that was just added.
1184 */
1185 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001186typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001187 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001190#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1191 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#endif
1193 ));
1194}
1195
1196/*
1197 * Return TRUE if there are no characters in the typeahead buffer that have
1198 * not been typed (result from a mapping or come from ":normal").
1199 */
1200 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001201typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203 return typebuf.tb_maplen == 0;
1204}
1205
1206/*
1207 * Return the number of characters that are mapped (or not typed).
1208 */
1209 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001210typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
1212 return typebuf.tb_maplen;
1213}
1214
1215/*
1216 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1217 */
1218 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001219del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 int i;
1222
1223 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 typebuf.tb_len -= len;
1227
1228 /*
1229 * Easy case: Just increase typebuf.tb_off.
1230 */
1231 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1232 >= 3 * MAXMAPLEN + 3)
1233 typebuf.tb_off += len;
1234 /*
1235 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1236 */
1237 else
1238 {
1239 i = typebuf.tb_off + offset;
1240 /*
1241 * Leave some extra room at the end to avoid reallocation.
1242 */
1243 if (typebuf.tb_off > MAXMAPLEN)
1244 {
1245 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1246 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1247 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1248 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1249 typebuf.tb_off = MAXMAPLEN;
1250 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001251 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1253 typebuf.tb_buf + i + len,
1254 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001255 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1257 typebuf.tb_noremap + i + len,
1258 (size_t)(typebuf.tb_len - offset));
1259 }
1260
Bram Moolenaar30613902019-12-01 22:11:18 +01001261 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 if (typebuf.tb_maplen < offset + len)
1264 typebuf.tb_maplen = offset;
1265 else
1266 typebuf.tb_maplen -= len;
1267 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001268 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
1270 if (typebuf.tb_silent < offset + len)
1271 typebuf.tb_silent = offset;
1272 else
1273 typebuf.tb_silent -= len;
1274 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001275 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 {
1277 if (typebuf.tb_no_abbr_cnt < offset + len)
1278 typebuf.tb_no_abbr_cnt = offset;
1279 else
1280 typebuf.tb_no_abbr_cnt -= len;
1281 }
1282
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001283#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001284 // Reset the flag that text received from a client or from feedkeys()
1285 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001286 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287#endif
1288 if (++typebuf.tb_change_cnt == 0)
1289 typebuf.tb_change_cnt = 1;
1290}
1291
zeertzjq094c4392024-04-18 22:09:37 +02001292/*
1293 * State for adding bytes to a recording or 'showcmd'.
1294 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001295typedef struct
1296{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001297 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001298 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001299 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001300 unsigned pending_special;
1301 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001302} gotchars_state_T;
1303
1304/*
1305 * Add a single byte to a recording or 'showcmd'.
1306 * Return TRUE if a full key has been received, FALSE otherwise.
1307 */
1308 static int
1309gotchars_add_byte(gotchars_state_T *state, char_u byte)
1310{
1311 int c = state->buf[state->buflen++] = byte;
1312 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001313 int in_special = state->pending_special > 0;
1314 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001315
zeertzjq6b13e3d2024-04-22 21:04:29 +02001316 if (in_special)
1317 state->pending_special--;
1318 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001319#ifdef FEAT_GUI
1320 || c == CSI
1321#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001322 )
1323 // When receiving a special key sequence, store it until we have all
1324 // the bytes and we can decide what to do with it.
1325 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001326
zeertzjq6b13e3d2024-04-22 21:04:29 +02001327 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001328 goto ret_false;
1329
zeertzjq6b13e3d2024-04-22 21:04:29 +02001330 if (in_mbyte)
1331 state->pending_mbyte--;
1332 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001333 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001334 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001335 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001336 if (state->prev_c == KS_MODIFIER)
1337 // When receiving a modifier, wait for the modified key.
1338 goto ret_false;
1339 c = TO_SPECIAL(state->prev_c, c);
1340 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1341 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1342 // in a recording.
1343 state->buflen = 0;
1344 }
1345 // When receiving a multibyte character, store it until we have all
1346 // the bytes, so that it won't be split between two buffer blocks,
1347 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001348 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001349 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001350
1351 if (state->pending_mbyte > 0)
1352 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001353
1354 retval = TRUE;
1355ret_false:
1356 state->prev_c = c;
1357 return retval;
1358}
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360/*
1361 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001362 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 */
1364 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001365gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001367 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001368 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001369 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001370 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
zeertzjqacdfb8a2024-04-17 21:28:54 +02001372 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001374 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001375 continue;
1376
Bram Moolenaar30613902019-12-01 22:11:18 +01001377 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001378 for (i = 0; i < state.buflen; ++i)
1379 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001381 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001383 state.buf[state.buflen] = NUL;
1384 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001385 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001386 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001388 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 may_sync_undo();
1392
1393#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001394 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 debug_did_msg = FALSE;
1396#endif
1397
Bram Moolenaar30613902019-12-01 22:11:18 +01001398 // Since characters have been typed, consider the following to be in
1399 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 ++maptick;
1401}
1402
1403/*
zeertzjqbf321802024-01-28 19:03:00 +01001404 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001405 */
1406 void
zeertzjqbf321802024-01-28 19:03:00 +01001407gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001408{
zeertzjqbf321802024-01-28 19:03:00 +01001409 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001410 gotchars(nop_buf, 3);
1411}
1412
1413/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001414 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1415 * character back into the typeahead buffer, thus gotchars() will be called
1416 * again.
1417 * Only affects recorded characters.
1418 */
1419 void
1420ungetchars(int len)
1421{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001422 if (reg_recording == 0)
1423 return;
1424
1425 delete_buff_tail(&recordbuff, len);
1426 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001427}
1428
1429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 * Sync undo. Called when typed characters are obtained from the typeahead
1431 * buffer, or when a menu is used.
1432 * Do not sync:
1433 * - In Insert mode, unless cursor key has been used.
1434 * - While reading a script file.
1435 * - When no_u_sync is non-zero.
1436 */
1437 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001438may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
Bram Moolenaar24959102022-05-07 20:01:16 +01001440 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001441 && scriptin[curscript] == NULL)
1442 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443}
1444
1445/*
1446 * Make "typebuf" empty and allocate new buffers.
1447 * Returns FAIL when out of memory.
1448 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001449 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001450alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 typebuf.tb_buf = alloc(TYPELEN_INIT);
1453 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1454 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1455 {
1456 free_typebuf();
1457 return FAIL;
1458 }
1459 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001460 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 typebuf.tb_len = 0;
1462 typebuf.tb_maplen = 0;
1463 typebuf.tb_silent = 0;
1464 typebuf.tb_no_abbr_cnt = 0;
1465 if (++typebuf.tb_change_cnt == 0)
1466 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001467#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1468 typebuf_was_filled = FALSE;
1469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return OK;
1471}
1472
1473/*
1474 * Free the buffers of "typebuf".
1475 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001476 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001477free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001479 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001480 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001481 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001482 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001483 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001484 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001485 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001486 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487}
1488
1489/*
1490 * When doing ":so! file", the current typeahead needs to be saved, and
1491 * restored when "file" has been read completely.
1492 */
1493static typebuf_T saved_typebuf[NSCRIPT];
1494
1495 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001496save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 init_typebuf();
1499 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001500 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (alloc_typebuf() == FAIL)
1502 {
1503 closescript();
1504 return FAIL;
1505 }
1506 return OK;
1507}
1508
Bram Moolenaar30613902019-12-01 22:11:18 +01001509static int old_char = -1; // character put back by vungetc()
1510static int old_mod_mask; // mod_mask for ungotten character
1511static int old_mouse_row; // mouse_row related to old_char
1512static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001513static int old_KeyStuffed; // whether old_char was stuffed
1514
zeertzjq6d4e7252022-04-07 13:58:04 +01001515static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001516{
1517 // If the old character was not stuffed and characters have been added to
1518 // the stuff buffer, need to first get the stuffed characters instead.
1519 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1520}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001521
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522/*
1523 * Save all three kinds of typeahead, so that the user must type at a prompt.
1524 */
1525 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001526save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 tp->save_typebuf = typebuf;
1529 tp->typebuf_valid = (alloc_typebuf() == OK);
1530 if (!tp->typebuf_valid)
1531 typebuf = tp->save_typebuf;
1532
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001533 tp->old_char = old_char;
1534 tp->old_mod_mask = old_mod_mask;
1535 old_char = -1;
1536
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001537 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001538 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001539 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001540 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541# ifdef USE_INPUT_BUF
1542 tp->save_inputbuf = get_input_buf();
1543# endif
1544}
1545
1546/*
1547 * Restore the typeahead to what it was before calling save_typeahead().
1548 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001549 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
1551 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001552restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 if (tp->typebuf_valid)
1555 {
1556 free_typebuf();
1557 typebuf = tp->save_typebuf;
1558 }
1559
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001560 old_char = tp->old_char;
1561 old_mod_mask = tp->old_mod_mask;
1562
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001563 free_buff(&readbuf1);
1564 readbuf1 = tp->save_readbuf1;
1565 free_buff(&readbuf2);
1566 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001568 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569# endif
1570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572/*
1573 * Open a new script file for the ":source!" command.
1574 */
1575 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001576openscript(
1577 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 if (curscript + 1 == NSCRIPT)
1581 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001582 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 return;
1584 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001585
1586 // Disallow sourcing a file in the sandbox, the commands would be executed
1587 // later, possibly outside of the sandbox.
1588 if (check_secure())
1589 return;
1590
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001591#ifdef FEAT_EVAL
1592 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001593 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001594 return;
1595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaar30613902019-12-01 22:11:18 +01001597 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001599 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 expand_env(name, NameBuff, MAXPATHL);
1601 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1602 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001603 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (curscript)
1605 --curscript;
1606 return;
1607 }
1608 if (save_typebuf() == FAIL)
1609 return;
1610
1611 /*
1612 * Execute the commands from the file right now when using ":source!"
1613 * after ":global" or ":argdo" or in a loop. Also when another command
1614 * follows. This means the display won't be updated. Don't do this
1615 * always, "make test" would fail.
1616 */
1617 if (directly)
1618 {
1619 oparg_T oa;
1620 int oldcurscript;
1621 int save_State = State;
1622 int save_restart_edit = restart_edit;
1623 int save_insertmode = p_im;
1624 int save_finish_op = finish_op;
1625 int save_msg_scroll = msg_scroll;
1626
Bram Moolenaar24959102022-05-07 20:01:16 +01001627 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001628 msg_scroll = FALSE; // no msg scrolling in Normal mode
1629 restart_edit = 0; // don't go to Insert mode
1630 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 clear_oparg(&oa);
1632 finish_op = FALSE;
1633
1634 oldcurscript = curscript;
1635 do
1636 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001637 update_topline_cursor(); // update cursor position and topline
1638 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001639 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 while (scriptin[oldcurscript] != NULL);
1642
1643 State = save_State;
1644 msg_scroll = save_msg_scroll;
1645 restart_edit = save_restart_edit;
1646 p_im = save_insertmode;
1647 finish_op = save_finish_op;
1648 }
1649}
1650
1651/*
1652 * Close the currently active input script.
1653 */
1654 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001655closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 free_typebuf();
1658 typebuf = saved_typebuf[curscript];
1659
1660 fclose(scriptin[curscript]);
1661 scriptin[curscript] = NULL;
1662 if (curscript > 0)
1663 --curscript;
1664}
1665
Bram Moolenaarea408852005-06-25 22:49:46 +00001666#if defined(EXITFREE) || defined(PROTO)
1667 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001668close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001669{
1670 while (scriptin[0] != NULL)
1671 closescript();
1672}
1673#endif
1674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675/*
1676 * Return TRUE when reading keys from a script file.
1677 */
1678 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001679using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681 return scriptin[curscript] != NULL;
1682}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001685 * This function is called just before doing a blocking wait. Thus after
1686 * waiting 'updatetime' for a character to arrive.
1687 */
1688 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001689before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001690{
1691 updatescript(0);
1692#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001693 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001694 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001695#endif
1696}
1697
1698/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001699 * updatescript() is called when a character can be written into the script
1700 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 *
1702 * All the changed memfiles are synced if c == 0 or when the number of typed
1703 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1704 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001705 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001706updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 static int count = 0;
1709
1710 if (c && scriptout)
1711 putc(c, scriptout);
1712 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1713 {
1714 ml_sync_all(c == 0, TRUE);
1715 count = 0;
1716 }
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02001717#ifdef FEAT_EVAL
1718 if (typedchars_pos < MAXMAPLEN)
1719 {
1720 typedchars[typedchars_pos] = c;
1721 typedchars_pos++;
1722 }
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724}
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001727 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1728 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001729 */
1730 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001731merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001732{
1733 int c = c_arg;
1734
Bram Moolenaarea83c192023-03-18 17:22:46 +00001735 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001736 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001737 {
1738 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001739 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001740 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001741 if (c == NUL)
1742 c = K_ZERO;
1743 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001744 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001745 // CTRL-6 is equivalent to CTRL-^
1746 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001747#ifdef FEAT_GUI_GTK
1748 // These mappings look arbitrary at the first glance, but in fact
1749 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1750 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1751 // more sense and is consistent with usual terminal behaviour).
1752 else if (c == '2')
1753 c = NUL;
1754 else if (c >= '3' && c <= '7')
1755 c = c ^ 0x28;
1756 else if (c == '8')
1757 c = BS;
1758 else if (c == '?')
1759 c = DEL;
1760#endif
1761 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001762 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001763 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001764
1765 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001766 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001767 && c >= 0 && c <= 127)
1768 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001769 // Some terminals (esp. Kitty) do not include Shift in the character.
1770 // Apply it here to get consistency across terminals. Only do ASCII
1771 // letters, for other characters it depends on the keyboard layout.
1772 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1773 {
1774 c += 'a' - 'A';
1775 *modifiers &= ~MOD_MASK_SHIFT;
1776 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001777 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001778 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001779 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001780
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001781 return c;
1782}
1783
1784/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001785 * Add a single byte to 'showcmd' for a partially matched mapping.
1786 * Call add_to_showcmd() if a full key has been received.
1787 */
1788 static void
1789add_byte_to_showcmd(char_u byte)
1790{
1791 static gotchars_state_T state;
1792 char_u *ptr;
1793 int modifiers = 0;
1794 int c = NUL;
1795
1796 if (!p_sc || msg_silent != 0)
1797 return;
1798
1799 if (!gotchars_add_byte(&state, byte))
1800 return;
1801
1802 state.buf[state.buflen] = NUL;
1803 state.buflen = 0;
1804
1805 ptr = state.buf;
1806 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1807 {
1808 modifiers = ptr[2];
1809 ptr += 3;
1810 }
1811
1812 if (*ptr != NUL)
1813 {
1814 char_u *mb_ptr = mb_unescape(&ptr);
1815
1816 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1817 if (c <= 0x7f)
1818 {
1819 // Merge modifiers into the key to make the result more readable.
1820 int modifiers_after = modifiers;
1821 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1822
1823 if (modifiers_after == 0)
1824 {
1825 modifiers = 0;
1826 c = mod_c;
1827 }
1828 }
1829 }
1830
1831 // TODO: is there a more readable and yet compact representation of
1832 // modifiers and special keys?
1833 if (modifiers != 0)
1834 {
1835 add_to_showcmd(K_SPECIAL);
1836 add_to_showcmd(KS_MODIFIER);
1837 add_to_showcmd(modifiers);
1838 }
1839 if (c != NUL)
1840 add_to_showcmd(c);
1841 while (*ptr != NUL)
1842 add_to_showcmd(*ptr++);
1843}
1844
1845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 * Get the next input character.
1847 * Can return a special key or a multi-byte character.
1848 * Can return NUL when called recursively, use safe_vgetc() if that's not
1849 * wanted.
1850 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1851 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001852 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 */
1854 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001855vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
1857 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001859 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001862#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001863 // Do garbage collection when garbagecollect() was called previously and
1864 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001865 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001866 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001867#endif
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 /*
1870 * If a character was put back with vungetc, it was already processed.
1871 * Return it directly.
1872 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001873 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 c = old_char;
1876 old_char = -1;
1877 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001878 mouse_row = old_mouse_row;
1879 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
zeertzjq81b46a62022-04-09 17:58:49 +01001883 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001884 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001885
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001886 mod_mask = 0;
1887 vgetc_mod_mask = 0;
1888 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001889
1890 // last_recorded_len can be larger than last_vgetc_recorded_len
1891 // if peeking records more
1892 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001893
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001894 for (;;) // this is done twice if there are modifiers
1895 {
1896 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001897
Bram Moolenaar78aed952022-09-24 15:36:35 +01001898 // No mapping after modifier has been read, using an input method
1899 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001900 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001901#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001902 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001903#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001904#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001905 || popup_no_mapping()
1906#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001907 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001909 ++no_mapping;
1910 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001911 // mod_mask value may change, remember we did the increment
1912 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001914 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001915 if (did_inc)
1916 {
1917 --no_mapping;
1918 --allow_keys;
1919 }
1920
Christopher Plewright03193062022-11-22 12:58:27 +00001921 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001922 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001923#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001924 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001925#endif
1926 )
1927 {
1928 int save_allow_keys = allow_keys;
1929
1930 ++no_mapping;
1931 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001932 c2 = vgetorpeek(TRUE); // no mapping for these chars
1933 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001934 --no_mapping;
1935 allow_keys = save_allow_keys;
1936 if (c2 == KS_MODIFIER)
1937 {
1938 mod_mask = c;
1939 continue;
1940 }
1941 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001943 // K_ESC is used to avoid ambiguity with the single Esc
1944 // character that might be the start of an escape sequence.
1945 // Convert it back to a single Esc here.
1946 if (c == K_ESC)
1947 c = ESC;
1948
Bram Moolenaar4f974752019-02-17 17:44:42 +01001949#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001950 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1951 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001952 if (
1953# ifdef VIMDLL
1954 gui.in_use &&
1955# endif
1956 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001958 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001959 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001960
1961 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001962 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001963 {
K.Takata54119102022-02-03 13:33:03 +00001964 name[j] = c;
1965 if (j < 199)
1966 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001967 }
K.Takata54119102022-02-03 13:33:03 +00001968 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001969 gui_make_tearoff(name);
1970 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001973#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001974 // GTK: <F10> normally selects the menu, but it's passed until
1975 // here to allow mapping it. Intercept and invoke the GTK
1976 // behavior if it's not mapped.
1977 if (c == K_F10 && gui.menubar != NULL)
1978 {
1979 gtk_menu_shell_select_first(
1980 GTK_MENU_SHELL(gui.menubar), FALSE);
1981 continue;
1982 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001985 // Handle focus event here, so that the caller doesn't need to
1986 // know about it. Return K_IGNORE so that we loop once (needed
1987 // if 'lazyredraw' is set).
1988 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001989 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001990 ui_focus_change(c == K_FOCUSGAINED);
1991 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001992 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001993
1994 // Translate K_CSI to CSI. The special key is only used to
1995 // avoid it being recognized as the start of a special key.
1996 if (c == K_CSI)
1997 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001999#ifdef FEAT_EVAL
2000 if (c == K_SID)
2001 {
2002 int j;
2003
2004 // Handle <SID>{sid}; Do up to 20 digits for safety.
2005 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002006 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002007 last_used_sid = last_used_sid * 10 + (c - '0');
2008 last_used_map = NULL;
2009 continue;
2010 }
2011#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002012 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002013
zeertzjq90a80022024-07-13 19:06:44 +02002014 // For a multi-byte character get all the bytes and return the
2015 // converted character.
2016 // Note: This will loop until enough bytes are received!
2017 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2018 {
2019 ++no_mapping;
2020 buf[0] = c;
2021 for (i = 1; i < n; ++i)
2022 {
2023 buf[i] = vgetorpeek(TRUE);
2024 if (buf[i] == K_SPECIAL
2025#ifdef FEAT_GUI
2026 || (buf[i] == CSI)
2027#endif
2028 )
2029 {
2030 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2031 // sequence, which represents a K_SPECIAL (0x80),
2032 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2033 // represents a CSI (0x9B),
2034 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2035 // too.
2036 c = vgetorpeek(TRUE);
2037 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2038 buf[i] = CSI;
2039 }
2040 }
2041 --no_mapping;
2042 c = (*mb_ptr2char)(buf);
2043 }
2044
2045 if (vgetc_char == 0)
2046 {
2047 vgetc_mod_mask = mod_mask;
2048 vgetc_char = c;
2049 }
2050
zeertzjqd9be94c2024-07-14 10:20:20 +02002051 // A keypad or special function key was not mapped, use it like
2052 // its ASCII equivalent.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002053 switch (c)
2054 {
2055 case K_KPLUS: c = '+'; break;
2056 case K_KMINUS: c = '-'; break;
2057 case K_KDIVIDE: c = '/'; break;
2058 case K_KMULTIPLY: c = '*'; break;
2059 case K_KENTER: c = CAR; break;
2060 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002061#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002062 // Can be either '.' or a ',',
2063 // depending on the type of keypad.
2064 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002065#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002066 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002067#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002068 case K_K0: c = '0'; break;
2069 case K_K1: c = '1'; break;
2070 case K_K2: c = '2'; break;
2071 case K_K3: c = '3'; break;
2072 case K_K4: c = '4'; break;
2073 case K_K5: c = '5'; break;
2074 case K_K6: c = '6'; break;
2075 case K_K7: c = '7'; break;
2076 case K_K8: c = '8'; break;
2077 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002078
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002079 case K_XHOME:
2080 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002081 {
2082 c = K_S_HOME;
2083 mod_mask = 0;
2084 }
2085 else if (mod_mask == MOD_MASK_CTRL)
2086 {
2087 c = K_C_HOME;
2088 mod_mask = 0;
2089 }
2090 else
2091 c = K_HOME;
2092 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002093 case K_XEND:
2094 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002095 {
2096 c = K_S_END;
2097 mod_mask = 0;
2098 }
2099 else if (mod_mask == MOD_MASK_CTRL)
2100 {
2101 c = K_C_END;
2102 mod_mask = 0;
2103 }
2104 else
2105 c = K_END;
2106 break;
2107
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002108 case K_XUP: c = K_UP; break;
2109 case K_XDOWN: c = K_DOWN; break;
2110 case K_XLEFT: c = K_LEFT; break;
2111 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002114 break;
2115 }
zeertzjq81b46a62022-04-09 17:58:49 +01002116
2117 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002119
2120#ifdef FEAT_EVAL
2121 /*
2122 * In the main loop "may_garbage_collect" can be set to do garbage
2123 * collection in the first next vgetc(). It's disabled after that to
2124 * avoid internally used Lists and Dicts to be freed.
2125 */
2126 may_garbage_collect = FALSE;
2127#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002128
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002129#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002130 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002131 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002132 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002133 bevalexpr_due_set = FALSE;
2134 ui_remove_balloon();
2135 }
2136#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002137#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002138 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2139 // are filtered.
2140 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002141 {
2142 if (c == Ctrl_C)
2143 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002144 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002145 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002146#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002147
Shougo Matsushita83678842024-07-11 22:05:12 +02002148#ifdef FEAT_EVAL
2149 c = do_key_input_pre(c);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002150
2151 // Clear the next typedchars_pos
2152 typedchars_pos = 0;
Shougo Matsushita83678842024-07-11 22:05:12 +02002153#endif
2154
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002155 // Need to process the character before we know it's safe to do something
2156 // else.
2157 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002158 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002159
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002160 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161}
2162
Shougo Matsushita83678842024-07-11 22:05:12 +02002163#ifdef FEAT_EVAL
2164/*
2165 * Handle the InsertCharPre autocommand.
2166 * "c" is the character that was typed.
2167 * Return new input character.
2168 */
2169 static int
2170do_key_input_pre(int c)
2171{
2172 int res = c;
2173 char_u buf[MB_MAXBYTES + 1];
2174 char_u curr_mode[MODE_MAX_LENGTH];
2175 int save_State = State;
2176 save_v_event_T save_v_event;
2177 dict_T *v_event;
2178
2179 // Return quickly when there is nothing to do.
2180 if (!has_keyinputpre())
2181 return res;
2182
2183 if (IS_SPECIAL(c))
2184 {
2185 buf[0] = K_SPECIAL;
2186 buf[1] = KEY2TERMCAP0(c);
2187 buf[2] = KEY2TERMCAP1(c);
2188 buf[3] = NUL;
2189 }
2190 else
2191 buf[(*mb_char2bytes)(c, buf)] = NUL;
2192
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002193 typedchars[typedchars_pos] = NUL;
2194 vim_unescape_csi(typedchars);
2195
Shougo Matsushita83678842024-07-11 22:05:12 +02002196 get_mode(curr_mode);
2197
2198 // Lock the text to avoid weird things from happening.
2199 ++textlock;
2200 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2201
2202 v_event = get_v_event(&save_v_event);
2203 (void)dict_add_bool(v_event, "typed", KeyTyped);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002204 (void)dict_add_string(v_event, "typedchar", typedchars);
Shougo Matsushita83678842024-07-11 22:05:12 +02002205
2206 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2207 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2208 {
2209 // Get the value of v:char. It may be empty or more than one
2210 // character. Only use it when changed, otherwise continue with the
2211 // original character.
2212 char_u *v_char;
2213
2214 v_char = get_vim_var_str(VV_CHAR);
2215
2216 // Convert special bytes when it is special string.
2217 if (STRLEN(v_char) >= 3 && v_char[0] == K_SPECIAL)
2218 res = TERMCAP2KEY(v_char[1], v_char[2]);
2219 else if (STRLEN(v_char) > 0)
2220 res = PTR2CHAR(v_char);
2221 }
2222
2223 restore_v_event(v_event, &save_v_event);
2224
2225 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2226 --textlock;
2227
2228 // Restore the State, it may have been changed.
2229 State = save_State;
2230
2231 return res;
2232}
2233#endif
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235/*
2236 * Like vgetc(), but never return a NUL when called recursively, get a key
2237 * directly from the user (ignoring typeahead).
2238 */
2239 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002240safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 int c;
2243
2244 c = vgetc();
2245 if (c == NUL)
2246 c = get_keystroke();
2247 return c;
2248}
2249
2250/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002251 * Like safe_vgetc(), but loop to handle K_IGNORE.
2252 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002253 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002254 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002255 static int
2256plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002257{
2258 int c;
2259
2260 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002261 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002262 while (c == K_IGNORE
2263 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2264 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002265 return c;
2266}
2267
2268/*
2269 * Like safe_vgetc(), but loop to handle K_IGNORE.
2270 * Also ignore scrollbar events.
2271 */
2272 int
2273plain_vgetc(void)
2274{
2275 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002276
2277 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002278 // Only handle the first pasted character. Drop the rest, since we
2279 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002280 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2281
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002282 return c;
2283}
2284
2285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 * Check if a character is available, such that vgetc() will not block.
2287 * If the next character is a special character or multi-byte, the returned
2288 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002289 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 */
2291 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002292vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002294 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002296 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297}
2298
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002299#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300/*
2301 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2302 * codes.
2303 */
2304 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002305vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 int c;
2308
2309 ++no_mapping;
2310 ++allow_keys;
2311 c = vpeekc();
2312 --no_mapping;
2313 --allow_keys;
2314 return c;
2315}
2316#endif
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318/*
2319 * Check if any character is available, also half an escape sequence.
2320 * Trick: when no typeahead found, but there is something in the typeahead
2321 * buffer, it must be an ESC that is recognized as the start of a key code.
2322 */
2323 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002324vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 int c;
2327
2328 c = vpeekc();
2329 if (c == NUL && typebuf.tb_len > 0)
2330 c = ESC;
2331 return c;
2332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334/*
2335 * Call vpeekc() without causing anything to be mapped.
2336 * Return TRUE if a character is available, FALSE otherwise.
2337 */
2338 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002339char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
2341 int retval;
2342
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002343#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002344 // When test_override("char_avail", 1) was called pretend there is no
2345 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002346 if (disable_char_avail_for_testing)
2347 return FALSE;
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 ++no_mapping;
2350 retval = vpeekc();
2351 --no_mapping;
2352 return (retval != NUL);
2353}
2354
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002355#if defined(FEAT_EVAL) || defined(PROTO)
2356/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002357 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002358 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002359 static void
2360getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002361{
2362 varnumber_T n;
2363 int error = FALSE;
2364
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002365 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2366 return;
2367
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002368#ifdef MESSAGE_QUEUE
2369 // vpeekc() used to check for messages, but that caused problems, invoking
2370 // a callback where it was not expected. Some plugins use getchar(1) in a
2371 // loop to await a message, therefore make sure we check for messages here.
2372 parse_queued_messages();
2373#endif
2374
Bram Moolenaar30613902019-12-01 22:11:18 +01002375 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002376 windgoto(msg_row, msg_col);
2377
2378 ++no_mapping;
2379 ++allow_keys;
2380 for (;;)
2381 {
2382 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002383 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002384 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002385 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002386 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002387 n = vpeekc_any();
2388 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002389 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002390 n = 0;
2391 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002392 // getchar(0) and char avail() != NUL: get a character.
2393 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2394 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002395
Bram Moolenaar15183b42020-09-05 19:59:39 +02002396 if (n == K_IGNORE || n == K_MOUSEMOVE
2397 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002398 continue;
2399 break;
2400 }
2401 --no_mapping;
2402 --allow_keys;
2403
2404 set_vim_var_nr(VV_MOUSE_WIN, 0);
2405 set_vim_var_nr(VV_MOUSE_WINID, 0);
2406 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2407 set_vim_var_nr(VV_MOUSE_COL, 0);
2408
2409 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002410 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002411 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002412 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002413 int i = 0;
2414
Bram Moolenaar30613902019-12-01 22:11:18 +01002415 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002416 if (mod_mask != 0)
2417 {
2418 temp[i++] = K_SPECIAL;
2419 temp[i++] = KS_MODIFIER;
2420 temp[i++] = mod_mask;
2421 }
2422 if (IS_SPECIAL(n))
2423 {
2424 temp[i++] = K_SPECIAL;
2425 temp[i++] = K_SECOND(n);
2426 temp[i++] = K_THIRD(n);
2427 }
2428 else if (has_mbyte)
2429 i += (*mb_char2bytes)(n, temp + i);
2430 else
2431 temp[i++] = n;
2432 temp[i++] = NUL;
2433 rettv->v_type = VAR_STRING;
2434 rettv->vval.v_string = vim_strsave(temp);
2435
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002436 if (is_mouse_key(n))
2437 {
2438 int row = mouse_row;
2439 int col = mouse_col;
2440 win_T *win;
2441 linenr_T lnum;
2442 win_T *wp;
2443 int winnr = 1;
2444
2445 if (row >= 0 && col >= 0)
2446 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002447 // Find the window at the mouse coordinates and compute the
2448 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002449 win = mouse_find_win(&row, &col, FIND_POPUP);
2450 if (win == NULL)
2451 return;
2452 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002453#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002454 if (WIN_IS_POPUP(win))
2455 winnr = 0;
2456 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002457#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002458 for (wp = firstwin; wp != win && wp != NULL;
2459 wp = wp->w_next)
2460 ++winnr;
2461 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2462 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2463 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2464 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2465 }
2466 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002467 }
2468}
2469
2470/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002471 * "getchar()" function
2472 */
2473 void
2474f_getchar(typval_T *argvars, typval_T *rettv)
2475{
2476 getchar_common(argvars, rettv);
2477}
2478
2479/*
2480 * "getcharstr()" function
2481 */
2482 void
2483f_getcharstr(typval_T *argvars, typval_T *rettv)
2484{
2485 getchar_common(argvars, rettv);
2486
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002487 if (rettv->v_type != VAR_NUMBER)
2488 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002489
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002490 char_u temp[7]; // mbyte-char: 6, NUL: 1
2491 varnumber_T n = rettv->vval.v_number;
2492 int i = 0;
2493
2494 if (n != 0)
2495 {
2496 if (has_mbyte)
2497 i += (*mb_char2bytes)(n, temp + i);
2498 else
2499 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002500 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002501 temp[i++] = NUL;
2502 rettv->v_type = VAR_STRING;
2503 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002504}
2505
2506/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002507 * "getcharmod()" function
2508 */
2509 void
2510f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2511{
2512 rettv->vval.v_number = mod_mask;
2513}
2514#endif // FEAT_EVAL
2515
2516#if defined(MESSAGE_QUEUE) || defined(PROTO)
2517# define MAX_REPEAT_PARSE 8
2518
2519/*
2520 * Process messages that have been queued for netbeans or clientserver.
2521 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002522 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002523 * it is safe to do so.
2524 */
2525 void
2526parse_queued_messages(void)
2527{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002528 int old_curwin_id;
2529 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002530 int i;
2531 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002532 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002533 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002534
2535 // Do not handle messages while redrawing, because it may cause buffers to
2536 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002537 // Also bail out when parsing messages was explicitly disabled.
2538 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002539 return;
2540
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002541 // If memory allocation fails during startup we'll exit but curbuf or
2542 // curwin could be NULL.
2543 if (curbuf == NULL || curwin == NULL)
2544 return;
2545
2546 old_curbuf_fnum = curbuf->b_fnum;
2547 old_curwin_id = curwin->w_id;
2548
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002549 ++entered;
2550
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002551 // may_garbage_collect is set in main_loop() to do garbage collection when
2552 // blocking to wait on a character. We don't want that while parsing
2553 // messages, a callback may invoke vgetc() while lists and dicts are in use
2554 // in the call stack.
2555 may_garbage_collect = FALSE;
2556
2557 // Loop when a job ended, but don't keep looping forever.
2558 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2559 {
2560 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002561# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002562 channel_handle_events(FALSE);
2563# endif
2564
2565# ifdef FEAT_NETBEANS_INTG
2566 // Process the queued netbeans messages.
2567 netbeans_parse_messages();
2568# endif
2569# ifdef FEAT_JOB_CHANNEL
2570 // Write any buffer lines still to be written.
2571 channel_write_any_lines();
2572
2573 // Process the messages queued on channels.
2574 channel_parse_messages();
2575# endif
2576# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2577 // Process the queued clientserver messages.
2578 server_parse_messages();
2579# endif
2580# ifdef FEAT_JOB_CHANNEL
2581 // Check if any jobs have ended. If so, repeat the above to handle
2582 // changes, e.g. stdin may have been closed.
2583 if (job_check_ended())
2584 continue;
2585# endif
2586# ifdef FEAT_TERMINAL
2587 free_unused_terminals();
2588# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002589
2590# ifdef FEAT_SOUND_MACOSX
2591 process_cfrunloop();
2592# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002593# ifdef FEAT_SOUND_CANBERRA
2594 if (has_sound_callback_in_queue())
2595 invoke_sound_callback();
2596# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002597#ifdef SIGUSR1
2598 if (got_sigusr1)
2599 {
2600 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2601 got_sigusr1 = FALSE;
2602 }
2603#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002604 break;
2605 }
2606
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002607 // When not nested we'll go back to waiting for a typed character. If it
2608 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002609 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002610 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002611
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002612 may_garbage_collect = save_may_garbage_collect;
2613
2614 // If the current window or buffer changed we need to bail out of the
2615 // waiting loop. E.g. when a job exit callback closes the terminal window.
2616 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002617 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002618
2619 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002620}
2621#endif
2622
2623
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002624typedef enum {
2625 map_result_fail, // failed, break loop
2626 map_result_get, // get a character from typeahead
2627 map_result_retry, // try to map again
2628 map_result_nomatch // no matching mapping, get char
2629} map_result_T;
2630
2631/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002632 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002633 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002634 */
2635 static int
zeertzjqee446032022-04-30 15:02:22 +01002636at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002637{
2638 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2639 int c = *p;
2640
2641 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002642 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002643 && p[1] == KS_MODIFIER
2644 && (p[2] & MOD_MASK_CTRL))
2645 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002646 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2647 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002648}
2649
2650/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002651 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002652 * into just a key, apply that.
2653 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2654 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002655 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002656 */
2657 static int
2658check_simplify_modifier(int max_offset)
2659{
2660 int offset;
2661 char_u *tp;
2662
2663 for (offset = 0; offset < max_offset; ++offset)
2664 {
2665 if (offset + 3 >= typebuf.tb_len)
2666 break;
2667 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002668 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002669 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002670 // A modifier was not used for a mapping, apply it to ASCII keys.
2671 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002672 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002673 int c = tp[3];
2674 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002675
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002676 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002677 {
2678 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002679 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002680
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002681 if (offset == 0)
2682 {
2683 // At the start: remember the character and mod_mask before
2684 // merging, in some cases, e.g. at the hit-return prompt,
2685 // they are put back in the typeahead buffer.
2686 vgetc_char = c;
2687 vgetc_mod_mask = tp[2];
2688 }
zeertzjq12e21e32022-04-27 11:58:01 +01002689 if (IS_SPECIAL(new_c))
2690 {
2691 new_string[0] = K_SPECIAL;
2692 new_string[1] = K_SECOND(new_c);
2693 new_string[2] = K_THIRD(new_c);
2694 len = 3;
2695 }
2696 else
2697 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002698 if (modifier == 0)
2699 {
2700 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002701 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002702 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002703 }
2704 else
2705 {
2706 tp[2] = modifier;
2707 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002708 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002709 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002710 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002711 return len;
2712 }
2713 }
2714 }
2715 return 0;
2716}
2717
2718/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002719 * Return TRUE if the terminal sends modifiers with various keys. This is when
2720 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2721 * enabled.
2722 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002723 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002724key_protocol_enabled(void)
2725{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002726 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2727 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2728 ? modify_otherkeys_state == MOKS_ENABLED
2729 : seenModifyOtherKeys;
2730 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002731}
2732
2733/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002734 * Handle mappings in the typeahead buffer.
2735 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002736 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002737 * - When there is no match yet, return map_result_nomatch, need to get more
2738 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002739 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002740 */
2741 static int
2742handle_mapping(
2743 int *keylenp,
2744 int *timedout,
2745 int *mapdepth)
2746{
2747 mapblock_T *mp = NULL;
2748 mapblock_T *mp2;
2749 mapblock_T *mp_match;
2750 int mp_match_len = 0;
2751 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002752 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002753 int tb_c1;
2754 int mlen;
2755#ifdef FEAT_LANGMAP
2756 int nolmaplen;
2757#endif
2758 int keylen = *keylenp;
2759 int i;
2760 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002761 int is_plug_map = FALSE;
2762
zeertzjqbb404f52022-07-23 06:25:29 +01002763 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002764 if (typebuf.tb_len >= 3
2765 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002766 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2767 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2768 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002769
2770 /*
2771 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002772 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002773 *
2774 * Don't look for mappings if:
2775 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2776 * - maphash_valid not set: no mappings present.
2777 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2778 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002779 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002780 * - waiting for a char with --more--
2781 * - in Ctrl-X mode, and we get a valid char for that mode
2782 */
2783 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2784 if (no_mapping == 0 && is_maphash_valid()
2785 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002786 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002787 || (p_remap
2788 && (typebuf.tb_noremap[typebuf.tb_off]
2789 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002790 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2791 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2792 && State != MODE_ASKMORE
2793 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002794 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002795 {
Christopher Plewright03193062022-11-22 12:58:27 +00002796#ifdef FEAT_GUI
2797 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2798 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002799 {
2800 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2801 // K_SPECIAL KS_MODIFIER {flags}.
2802 tb_c1 = K_SPECIAL;
2803 }
2804#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002805#ifdef FEAT_LANGMAP
2806 if (tb_c1 == K_SPECIAL)
2807 nolmaplen = 2;
2808 else
2809 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002810 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2811 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 nolmaplen = 0;
2813 }
2814#endif
2815 // First try buffer-local mappings.
2816 mp = get_buf_maphash_list(local_State, tb_c1);
2817 mp2 = get_maphash_list(local_State, tb_c1);
2818 if (mp == NULL)
2819 {
2820 // There are no buffer-local mappings.
2821 mp = mp2;
2822 mp2 = NULL;
2823 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002824
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002826 * Loop until a partly matching mapping is found or all (local)
2827 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002828 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002829 * A full match is only accepted if there is no partly match, so "aa"
2830 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002831 */
2832 mp_match = NULL;
2833 mp_match_len = 0;
2834 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002835 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002837 // Only consider an entry if the first character matches and it is
2838 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002839 // Skip ":lmap" mappings if keys were mapped.
2840 if (mp->m_keys[0] == tb_c1
2841 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002842 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002843 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002844 && ((mp->m_mode & MODE_LANGMAP) == 0
2845 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002846 {
2847#ifdef FEAT_LANGMAP
2848 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002849 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002850#endif
2851 // find the match length of this mapping
2852 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2853 {
zeertzjq49660f52022-10-20 17:59:38 +01002854 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002856 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002857 {
2858 if (nomap == 2 && c2 == KS_MODIFIER)
2859 modifiers = 1;
2860 else if (nomap == 1 && modifiers == 1)
2861 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002862 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002863 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002864 else
zeertzjq49660f52022-10-20 17:59:38 +01002865 {
2866 if (c2 == K_SPECIAL)
2867 nomap = 2;
2868 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2869 // Only apply 'langmap' if merging modifiers into
2870 // the key will not result in another character,
2871 // so that 'langmap' behaves consistently in
2872 // different terminals and GUIs.
2873 LANGMAP_ADJUST(c2, TRUE);
2874 modifiers = 0;
2875 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002876#endif
zeertzjq49660f52022-10-20 17:59:38 +01002877 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002878 break;
2879 }
2880
Bram Moolenaareda35f72019-08-03 14:59:44 +02002881 // Don't allow mapping the first byte(s) of a multi-byte char.
2882 // Happens when mapping <M-a> and then changing 'encoding'.
2883 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002884 {
2885 char_u *p1 = mp->m_keys;
2886 char_u *p2 = mb_unescape(&p1);
2887
2888 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002889 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002890 mlen = 0;
2891 }
2892
2893 // Check an entry whether it matches.
2894 // - Full match: mlen == keylen
2895 // - Partly match: mlen == typebuf.tb_len
2896 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002897 if (mlen == keylen || (mlen == typebuf.tb_len
2898 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002899 {
2900 char_u *s;
2901 int n;
2902
Bram Moolenaareda35f72019-08-03 14:59:44 +02002903 // If only script-local mappings are allowed, check if the
2904 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002905 s = typebuf.tb_noremap + typebuf.tb_off;
2906 if (*s == RM_SCRIPT
2907 && (mp->m_keys[0] != K_SPECIAL
2908 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002909 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002910 continue;
2911
Bram Moolenaareda35f72019-08-03 14:59:44 +02002912 // If one of the typed keys cannot be remapped, skip the
2913 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002914 for (n = mlen; --n >= 0; )
2915 if (*s++ & (RM_NONE|RM_ABBR))
2916 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002917 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002918 continue;
2919
2920 if (keylen > typebuf.tb_len)
2921 {
2922 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002923 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002924 {
2925 // break at a partly match
2926 keylen = KEYLEN_PART_MAP;
2927 break;
2928 }
2929 }
2930 else if (keylen > mp_match_len)
2931 {
2932 // found a longer match
2933 mp_match = mp;
2934 mp_match_len = keylen;
2935 }
2936 }
2937 else
Oleg Goncharov56904f92024-07-23 20:34:15 +02002938 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002939 // No match; may have to check for termcode at next
Oleg Goncharov56904f92024-07-23 20:34:15 +02002940 // character.
2941
2942 // If the first character that didn't match is
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002943 // K_SPECIAL then check for a termcode. This isn't perfect
2944 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002945 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002946 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002947 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002948 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2949 }
2950 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2951 want_termcode = 1;
Oleg Goncharov56904f92024-07-23 20:34:15 +02002952
2953 // Check termcode for uppercase character to properly
2954 // process "ESC[27;2;<ascii code>~" control sequences.
2955 if (ASCII_ISUPPER(mp->m_keys[mlen]))
2956 want_termcode = 1;
2957 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002958 }
2959 }
2960
Bram Moolenaareda35f72019-08-03 14:59:44 +02002961 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002962 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002963 {
2964 mp = mp_match;
2965 keylen = mp_match_len;
2966 }
2967 }
2968
2969 /*
2970 * Check for match with 'pastetoggle'
2971 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002972 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002973 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002974 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2975 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002976 break;
2977 if (p_pt[mlen] == NUL) // match
2978 {
2979 // write chars to script file(s)
2980 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002981 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2982 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002983
2984 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002985 set_option_value_give_err((char_u *)"paste",
2986 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002987 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002988 {
2989 msg_col = 0;
2990 msg_row = Rows - 1;
2991 msg_clr_eos(); // clear ruler
2992 }
2993 status_redraw_all();
2994 redraw_statuslines();
2995 showmode();
2996 setcursor();
2997 *keylenp = keylen;
2998 return map_result_retry;
2999 }
3000 // Need more chars for partly match.
3001 if (mlen == typebuf.tb_len)
3002 keylen = KEYLEN_PART_KEY;
3003 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003004 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003005 max_mlen = mlen + 1;
3006 }
3007
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003008 // May check for a terminal code when there is no mapping or only a partial
3009 // mapping. Also check if there is a full mapping with <Esc>, unless timed
3010 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003011 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003012 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
3013 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003014 {
3015 int save_keylen = keylen;
3016
3017 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003018 * When no matching mapping found or found a non-matching mapping that
3019 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003020 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02003021 * - mapping is allowed,
3022 * - keys have not been mapped,
3023 * - and not an ESC sequence, not in insert mode or p_ek is on,
3024 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003025 */
zeertzjq68a573c2022-04-28 14:10:01 +01003026 if (no_mapping == 0 || allow_keys != 0)
3027 {
3028 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003029 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003030 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003031 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003032 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3033 else
3034 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003035
Bram Moolenaar0684e362020-12-03 19:54:42 +01003036 // If no termcode matched but 'pastetoggle' matched partially
3037 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003038 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003039 keylen = KEYLEN_PART_KEY;
3040
Bram Moolenaar975a8802020-06-06 22:36:24 +02003041 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003042 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003043#ifdef FEAT_TERMINAL
3044 check_no_reduce_keys(); // may update the no_reduce_keys flag
3045#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003046 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003047 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003048 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003049 if (keylen < 0)
3050 // ins_typebuf() failed
3051 return map_result_fail;
3052 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003053
Bram Moolenaareda35f72019-08-03 14:59:44 +02003054 // When getting a partial match, but the last characters were not
3055 // typed, don't wait for a typed character to complete the
3056 // termcode. This helps a lot when a ":normal" command ends in an
3057 // ESC.
3058 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003059 keylen = 0;
3060 }
3061 else
3062 keylen = 0;
3063 if (keylen == 0) // no matching terminal code
3064 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003065#ifdef AMIGA
3066 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003067 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003068 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003069 {
3070 char_u *s;
3071
3072 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003073 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3074 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003075 ++s)
3076 ;
3077 if (*s == 'r' || *s == '|') // found one
3078 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003079 del_typebuf(
3080 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003081 // get size and redraw screen
3082 shell_resized();
3083 *keylenp = keylen;
3084 return map_result_retry;
3085 }
3086 if (*s == NUL) // need more characters
3087 keylen = KEYLEN_PART_KEY;
3088 }
3089 if (keylen >= 0)
3090#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003091 // When there was a matching mapping and no termcode could be
3092 // replaced after another one, use that mapping (loop around).
3093 // If there was no mapping at all use the character from the
3094 // typeahead buffer right here.
3095 if (mp == NULL)
3096 {
3097 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003098 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003099 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003100 }
3101
3102 if (keylen > 0) // full matching terminal code
3103 {
3104#if defined(FEAT_GUI) && defined(FEAT_MENU)
3105 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003106 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3107 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003108 {
3109 int idx;
3110
Bram Moolenaareda35f72019-08-03 14:59:44 +02003111 // Using a menu may cause a break in undo! It's like using
3112 // gotchars(), but without recording or writing to a script
3113 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003114 may_sync_undo();
3115 del_typebuf(3, 0);
3116 idx = get_menu_index(current_menu, local_State);
3117 if (idx != MENU_INDEX_INVALID)
3118 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003119 // In Select mode and a Visual mode menu is used: Switch
3120 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003121 // back to Select mode.
3122 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003123 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003124 {
3125 VIsual_select = FALSE;
3126 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003127 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003128 }
3129 ins_typebuf(current_menu->strings[idx],
3130 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003131 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003132 }
3133 }
3134#endif // FEAT_GUI && FEAT_MENU
3135 *keylenp = keylen;
3136 return map_result_retry; // try mapping again
3137 }
3138
Bram Moolenaareda35f72019-08-03 14:59:44 +02003139 // Partial match: get some more characters. When a matching mapping
3140 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003141 if (mp == NULL || keylen < 0)
3142 keylen = KEYLEN_PART_KEY;
3143 else
3144 keylen = mp_match_len;
3145 }
3146
3147 /*
3148 * complete match
3149 */
3150 if (keylen >= 0 && keylen <= typebuf.tb_len)
3151 {
3152 char_u *map_str;
3153
3154#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003155 int save_m_expr;
3156 int save_m_noremap;
3157 int save_m_silent;
3158 char_u *save_m_keys;
zeertzjq9d997ad2024-07-29 21:10:07 +02003159 char_u *save_alt_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003160#else
3161# define save_m_noremap mp->m_noremap
3162# define save_m_silent mp->m_silent
3163#endif
3164
3165 // write chars to script file(s)
3166 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003167 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3168 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003169
3170 cmd_silent = (typebuf.tb_silent > 0);
3171 del_typebuf(keylen, 0); // remove the mapped keys
3172
3173 /*
3174 * Put the replacement string in front of mapstr.
3175 * The depth check catches ":map x y" and ":map y x".
3176 */
3177 if (++*mapdepth >= p_mmd)
3178 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003179 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003180 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003181 redrawcmdline();
3182 else
3183 setcursor();
3184 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003185 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003186 *keylenp = keylen;
3187 return map_result_fail;
3188 }
3189
3190 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003191 * In Select mode and a Visual mode mapping is used: Switch to Visual
3192 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003193 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003194 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003195 {
3196 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003197 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003198 }
3199
3200#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003201 // Copy the values from *mp that are used, because evaluating the
3202 // expression may invoke a function that redefines the mapping, thereby
3203 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003204 save_m_expr = mp->m_expr;
3205 save_m_noremap = mp->m_noremap;
3206 save_m_silent = mp->m_silent;
3207 save_m_keys = NULL; // only saved when needed
zeertzjq9d997ad2024-07-29 21:10:07 +02003208 save_alt_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003209
3210 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003211 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3212 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003213 */
3214 if (mp->m_expr)
3215 {
3216 int save_vgetc_busy = vgetc_busy;
3217 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003218 int was_screen_col = screen_cur_col;
3219 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003220 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003221
3222 vgetc_busy = 0;
3223 may_garbage_collect = FALSE;
3224
3225 save_m_keys = vim_strsave(mp->m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003226 save_alt_m_keys = mp->m_alt != NULL
3227 ? vim_strsave(mp->m_alt->m_keys) : NULL;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003228 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003229
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003230 // The mapping may do anything, but we expect it to take care of
3231 // redrawing. Do put the cursor back where it was.
3232 windgoto(was_screen_row, was_screen_col);
3233 out_flush();
3234
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003235 // If an error was displayed and the expression returns an empty
3236 // string, generate a <Nop> to allow for a redraw.
3237 if (prev_did_emsg != did_emsg
3238 && (map_str == NULL || *map_str == NUL))
3239 {
3240 char_u buf[4];
3241
3242 vim_free(map_str);
3243 buf[0] = K_SPECIAL;
3244 buf[1] = KS_EXTRA;
3245 buf[2] = KE_IGNORE;
3246 buf[3] = NUL;
3247 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01003248 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003249 {
3250 // redraw the command below the error
3251 msg_didout = TRUE;
3252 if (msg_row < cmdline_row)
3253 msg_row = cmdline_row;
3254 redrawcmd();
3255 }
3256 }
3257
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003258 vgetc_busy = save_vgetc_busy;
3259 may_garbage_collect = save_may_garbage_collect;
3260 }
3261 else
3262#endif
3263 map_str = mp->m_str;
3264
3265 /*
3266 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003267 * If 'from' field is the same as the start of the 'to' field, don't
3268 * remap the first character (but do allow abbreviations).
3269 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003270 */
3271 if (map_str == NULL)
3272 i = FAIL;
3273 else
3274 {
3275 int noremap;
3276
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003277#ifdef FEAT_EVAL
3278 last_used_map = mp;
3279 last_used_sid = -1;
3280#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003281 if (save_m_noremap != REMAP_YES)
3282 noremap = save_m_noremap;
3283 else if (
3284#ifdef FEAT_EVAL
zeertzjq9d997ad2024-07-29 21:10:07 +02003285 save_m_expr ?
3286 (save_m_keys != NULL
3287 && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
3288 || (save_alt_m_keys != NULL
3289 && STRNCMP(map_str, save_alt_m_keys,
3290 STRLEN(save_alt_m_keys)) == 0) :
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003291#endif
zeertzjq9d997ad2024-07-29 21:10:07 +02003292 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
3293 || (mp->m_alt != NULL
3294 && STRNCMP(map_str, mp->m_alt->m_keys,
3295 STRLEN(mp->m_alt->m_keys)) == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003296 noremap = REMAP_SKIP;
zeertzjq9d997ad2024-07-29 21:10:07 +02003297 else
3298 noremap = REMAP_YES;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003299 i = ins_typebuf(map_str, noremap,
3300 0, TRUE, cmd_silent || save_m_silent);
3301#ifdef FEAT_EVAL
3302 if (save_m_expr)
3303 vim_free(map_str);
3304#endif
3305 }
3306#ifdef FEAT_EVAL
3307 vim_free(save_m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003308 vim_free(save_alt_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003309#endif
3310 *keylenp = keylen;
3311 if (i == FAIL)
3312 return map_result_fail;
3313 return map_result_retry;
3314 }
3315
3316 *keylenp = keylen;
3317 return map_result_nomatch;
3318}
3319
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003320/*
3321 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003322 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003323 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003326vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 old_char = c;
3329 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003330 old_mouse_row = mouse_row;
3331 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003332 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333}
3334
3335/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003336 * When peeking and not getting a character, reg_executing cannot be cleared
3337 * yet, so set a flag to clear it later.
3338 */
3339 static void
3340check_end_reg_executing(int advance)
3341{
3342 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3343 || pending_end_reg_executing))
3344 {
3345 if (advance)
3346 {
3347 reg_executing = 0;
3348 pending_end_reg_executing = FALSE;
3349 }
3350 else
3351 pending_end_reg_executing = TRUE;
3352 }
3353
3354}
3355
3356/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003357 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * 1. from the stuffbuffer
3359 * This is used for abbreviated commands like "D" -> "d$".
3360 * Also used to redo a command for ".".
3361 * 2. from the typeahead buffer
3362 * Stores text obtained previously but not used yet.
3363 * Also stores the result of mappings.
3364 * Also used for the ":normal" command.
3365 * 3. from the user
3366 * This may do a blocking wait if "advance" is TRUE.
3367 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003368 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003369 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 * KeyTyped is set to TRUE in the case the user typed the key.
3371 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003372 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003373 * Just look whether there is a character available.
3374 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 *
3376 * When "no_mapping" is zero, checks for mappings in the current mode.
3377 * Only returns one byte (of a multi-byte character).
3378 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3379 */
3380 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003381vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003383 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003384 int timedout = FALSE; // waited for more than 'timeoutlen'
3385 // for mapping to complete or
3386 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003387 int mapdepth = 0; // check for recursive mapping
3388 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003391 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392#endif
3393 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003395 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 /*
3398 * This function doesn't work very well when called recursively. This may
3399 * happen though, because of:
3400 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3401 * there is a character available, which calls this function. In that
3402 * case we must return NUL, to indicate no character is available.
3403 * 2. A GUI callback function writes to the screen, causing a
3404 * wait_return().
3405 * Using ":normal" can also do this, but it saves the typeahead buffer,
3406 * thus it should be OK. But don't get a key from the user then.
3407 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003408 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 return NUL;
3410
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003411 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003416 typebuf_was_empty = FALSE;
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 init_typebuf();
3420 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003421 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 do
3423 {
3424/*
3425 * get a character: 1. from the stuffbuffer
3426 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003427 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
3429 c = typeahead_char;
3430 if (advance)
3431 typeahead_char = 0;
3432 }
3433 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003434 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (c != NUL && !got_int)
3436 {
3437 if (advance)
3438 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003439 // KeyTyped = FALSE; When the command that stuffed something
3440 // was typed, behave like the stuffed command was typed.
3441 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 KeyStuffed = TRUE;
3443 }
3444 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003445 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 else
3448 {
3449 /*
3450 * Loop until we either find a matching mapped key, or we
3451 * are sure that it is not a mapped key.
3452 * If a mapped key sequence is found we go back to the start to
3453 * try re-mapping.
3454 */
3455 for (;;)
3456 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003457 long wait_time;
3458 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003459 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003460 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /*
3462 * ui_breakcheck() is slow, don't use it too often when
3463 * inside a mapping. But call it each time for typed
3464 * characters.
3465 */
3466 if (typebuf.tb_maplen)
3467 line_breakcheck();
3468 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003469 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (got_int)
3471 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003472 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003473 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 /*
3476 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003477 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 * Otherwise we behave like having gotten a CTRL-C.
3479 * As a result typing CTRL-C in insert mode will
3480 * really insert a CTRL-C.
3481 */
3482 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003483 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 c = ESC;
3485 else
3486 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003487 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003489 if (advance)
3490 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003491 // Also record this character, it might be needed to
3492 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003493 *typebuf.tb_buf = c;
3494 gotchars(typebuf.tb_buf, 1);
3495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 cmd_silent = FALSE;
3497
3498 break;
3499 }
3500 else if (typebuf.tb_len > 0)
3501 {
3502 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003503 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003505 map_result_T result = handle_mapping(
3506 &keylen, &timedout, &mapdepth);
3507
3508 if (result == map_result_retry)
3509 // try mapping again
3510 continue;
3511 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003512 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003513 // failed, use the outer loop
3514 c = -1;
3515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003517 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519/*
3520 * get a character: 2. from the typeahead buffer
3521 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003522 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003523 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003524 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003525 cmd_silent = (typebuf.tb_silent > 0);
3526 if (typebuf.tb_maplen > 0)
3527 KeyTyped = FALSE;
3528 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003529 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003530 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003531 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003532 gotchars(typebuf.tb_buf
3533 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003534 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003535 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003536 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003537 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003538 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003541 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544/*
3545 * get a character: 3. from the user - handle <Esc> in Insert mode
3546 */
3547 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003548 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003550 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 * typing an <ESC>. If we get something after all, we may
3552 * have to redisplay the mode. That the cursor is in the wrong
3553 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003554 * Do not do this if the kitty keyboard protocol is used, every
3555 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
3557 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 new_wcol = curwin->w_wcol;
3559 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if ( advance
3561 && typebuf.tb_len == 1
3562 && typebuf.tb_buf[typebuf.tb_off] == ESC
3563 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003564 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003567 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003568 && (p_timeout
3569 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003571 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003573 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 char_u *ptr;
3575
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003576 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
3578 unshowmode(TRUE);
3579 mode_deleted = TRUE;
3580 }
3581#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003582 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003583 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 int save_State;
3586
3587 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003588 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 gui_update_cursor(TRUE, FALSE);
3590 State = save_State;
3591 shape_changed = TRUE;
3592 }
3593#endif
3594 validate_cursor();
3595 old_wcol = curwin->w_wcol;
3596 old_wrow = curwin->w_wrow;
3597
Bram Moolenaar30613902019-12-01 22:11:18 +01003598 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (curwin->w_cursor.col != 0)
3600 {
3601 if (curwin->w_wcol > 0)
3602 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003603 // After auto-indenting and no text is following,
3604 // we are expecting to truncate the trailing
3605 // white-space, so find the last non-white
3606 // character -- webb
3607 if (did_ai && *skipwhite(ml_get_curline()
3608 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003610 chartabsize_T cts;
3611
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003612 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003614 init_chartabsize_arg(&cts, curwin,
3615 curwin->w_cursor.lnum, 0, ptr, ptr);
3616 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003618 if (!VIM_ISWHITE(*cts.cts_ptr))
3619 curwin->w_wcol = cts.cts_vcol;
3620 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003622 cts.cts_ptr +=
3623 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003625 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003627 clear_chartabsize_arg(&cts);
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003630 + curwin->w_wcol / curwin->w_width;
3631 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003633 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635 else
3636 {
3637 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 }
3641 else if (curwin->w_p_wrap && curwin->w_wrow)
3642 {
3643 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003644 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3648 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003649 // Correct when the cursor is on the right halve
3650 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 ptr = ml_get_curline();
3652 col -= (*mb_head_off)(ptr, ptr + col);
3653 if ((*mb_ptr2cells)(ptr + col) > 1)
3654 --curwin->w_wcol;
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657 setcursor();
3658 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 new_wcol = curwin->w_wcol;
3660 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 curwin->w_wcol = old_wcol;
3662 curwin->w_wrow = old_wrow;
3663 }
3664 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003665 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003666
Bram Moolenaar30613902019-12-01 22:11:18 +01003667 // Allow mapping for just typed characters. When we get here c
3668 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003669 for (n = 1; n <= c; ++n)
3670 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 typebuf.tb_len += c;
3672
Bram Moolenaar30613902019-12-01 22:11:18 +01003673 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3675 {
3676 timedout = TRUE;
3677 continue;
3678 }
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (ex_normal_busy > 0)
3681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar30613902019-12-01 22:11:18 +01003684 // No typeahead left and inside ":normal". Must return
3685 // something to avoid getting stuck. When an incomplete
3686 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (typebuf.tb_len > 0)
3688 {
3689 timedout = TRUE;
3690 continue;
3691 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003692
Bram Moolenaar30613902019-12-01 22:11:18 +01003693 // When 'insertmode' is set, ESC just beeps in Insert
3694 // mode. Use CTRL-L to make edit() return.
3695 // For the command line only CTRL-C always breaks it.
3696 // For the cmdline window: Alternate between ESC and
3697 // CTRL-C: ESC for most situations and CTRL-C to close the
3698 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003699 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003701#ifdef FEAT_TERMINAL
3702 else if (terminal_is_active())
3703 c = K_CANCEL;
3704#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003705 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003706 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 c = Ctrl_C;
3708 else
3709 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003711 // set a flag to indicate this wasn't a normal char
3712 if (advance)
3713 typebuf_was_empty = TRUE;
3714
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003715 // return from main_loop()
3716 if (pending_exmode_active)
3717 exmode_active = EXMODE_NORMAL;
3718
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003719 // no chars to block abbreviation for
3720 typebuf.tb_no_abbr_cnt = 0;
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 break;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725/*
3726 * get a character: 3. from the user - update display
3727 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003728 // In insert mode a screen update is skipped when characters
3729 // are still available. But when those available characters
3730 // are part of a mapping, and we are going to do a blocking
3731 // wait here. Need to update the screen to display the
3732 // changed text so far. Also for when 'lazyredraw' is set and
3733 // redrawing was postponed because there was something in the
3734 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003735 if (((State & MODE_INSERT) != 0 || p_lz)
3736 && (State & MODE_CMDLINE) == 0
3737 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003740 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742
3743 /*
3744 * If we have a partial match (and are going to wait for more
3745 * input from the user), show the partially matched characters
3746 * to the user with showcmd.
3747 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003748 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003749 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (typebuf.tb_len > 0 && advance && !exmode_active)
3751 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003752 if (((State & (MODE_NORMAL | MODE_INSERT))
3753 || State == MODE_LANGMAP)
3754 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003756 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003757 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3759 + typebuf.tb_len - 1) == 1)
3760 {
3761 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3762 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003763 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003764 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003766 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 old_wcol = curwin->w_wcol;
3768 old_wrow = curwin->w_wrow;
3769 curwin->w_wcol = new_wcol;
3770 curwin->w_wrow = new_wrow;
3771 push_showcmd();
3772 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003773 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3774 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003775 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003776 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 curwin->w_wcol = old_wcol;
3778 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003781 // This looks nice when typing a dead character map.
3782 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003783 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003784 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3786 && cmdline_star == 0
3787#endif
3788 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3789 + typebuf.tb_len - 1) == 1)
3790 {
3791 putcmdline(typebuf.tb_buf[typebuf.tb_off
3792 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003793 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 }
3796
3797/*
3798 * get a character: 3. from the user - get it
3799 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003800 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003801 // timedout may have been set if a mapping with empty RHS
3802 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003803 timedout = FALSE;
3804
Bram Moolenaar652de232019-04-04 20:13:09 +02003805 if (advance)
3806 {
3807 if (typebuf.tb_len == 0
3808 || !(p_timeout
3809 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3810 // blocking wait
3811 wait_time = -1L;
3812 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3813 wait_time = p_ttm;
3814 else
3815 wait_time = p_tm;
3816 }
3817 else
3818 wait_time = 0;
3819
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003820 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3822 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003823 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaareda35f72019-08-03 14:59:44 +02003825 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003827 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003829 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003831 if ((State & MODE_CMDLINE)
3832 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003834 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003835 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837
3838 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003839 continue; // end of input script reached
3840 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 if (!advance)
3843 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003844 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 timedout = TRUE;
3847 continue;
3848 }
3849 }
3850 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003851 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 while (typebuf.tb_buf[typebuf.tb_off
3853 + typebuf.tb_len] != NUL)
3854 typebuf.tb_noremap[typebuf.tb_off
3855 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003856#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003857 // Get IM status right after getting keys, not after the
3858 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 vgetc_im_active = im_get_status();
3860#endif
3861 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003862 } // for (;;)
3863 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaar30613902019-12-01 22:11:18 +01003865 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003866 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 /*
3869 * The "INSERT" message is taken care of here:
3870 * if we return an ESC to exit insert mode, the message is deleted
3871 * if we don't return an ESC but deleted the message before, redisplay it
3872 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003873 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003875 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003878 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 else
3880 unshowmode(FALSE);
3881 }
3882 else if (c != ESC && mode_deleted)
3883 {
3884 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003885 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else
3887 showmode();
3888 }
3889 }
3890#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003891 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003892 if (gui.in_use && shape_changed)
3893 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003895 if (timedout && c == ESC)
3896 {
zeertzjqbf321802024-01-28 19:03:00 +01003897 // When recording there will be no timeout. Add an <Ignore> after the
3898 // ESC to avoid that it forms a key code with following characters.
3899 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003902 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
3904 return c;
3905}
3906
3907/*
3908 * inchar() - get one character from
3909 * 1. a scriptfile
3910 * 2. the keyboard
3911 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003912 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 * NUL terminated (buffer length must be 'maxlen' + 1).
3914 * Minimum for "maxlen" is 3!!!!
3915 *
3916 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3917 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3918 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3919 * otherwise.
3920 *
3921 * If we got an interrupt all input is read until none is available.
3922 *
3923 * If wait_time == 0 there is no waiting for the char.
3924 * If wait_time == n we wait for n msec for a character to arrive.
3925 * If wait_time == -1 we wait forever for a character to arrive.
3926 *
3927 * Return the number of obtained characters.
3928 * Return -1 when end of input script reached.
3929 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003930 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003931inchar(
3932 char_u *buf,
3933 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003934 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
Bram Moolenaar30613902019-12-01 22:11:18 +01003936 int len = 0; // init for GCC
3937 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003939 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar30613902019-12-01 22:11:18 +01003941 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
3943 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003944 out_flush_cursor(FALSE, FALSE);
3945#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3946 if (gui.in_use && postponed_mouseshape)
3947 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#endif
3949 }
3950
3951 /*
3952 * Don't reset these when at the hit-return prompt, otherwise a endless
3953 * recursive loop may result (write error in swapfile, hit-return, timeout
3954 * on char wait, flush swapfile, write error....).
3955 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003956 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003958 did_outofmem_msg = FALSE; // display out of memory message (again)
3959 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003961 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003964 * Get a character from a script file if there is one.
3965 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 */
3967 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003968 while (scriptin[curscript] != NULL && script_char < 0
3969#ifdef FEAT_EVAL
3970 && !ignore_script
3971#endif
3972 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003974#ifdef MESSAGE_QUEUE
3975 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003976#endif
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3979 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003980 // Reached EOF.
3981 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3982 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 closescript();
3984 /*
3985 * When reading script file is interrupted, return an ESC to get
3986 * back to normal mode.
3987 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3988 */
3989 if (got_int)
3990 retesc = TRUE;
3991 else
3992 return -1;
3993 }
3994 else
3995 {
3996 buf[0] = script_char;
3997 len = 1;
3998 }
3999 }
4000
Bram Moolenaar30613902019-12-01 22:11:18 +01004001 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
4003 /*
4004 * If we got an interrupt, skip all previously typed characters and
4005 * return TRUE if quit reading script file.
4006 * Stop reading typeahead when a single CTRL-C was read,
4007 * fill_input_buf() returns this when not able to read from stdin.
4008 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
4009 * and buf may be pointing inside typebuf.tb_buf[].
4010 */
4011 if (got_int)
4012 {
kylo252ae6f1d82022-02-16 19:24:07 +00004013#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u dum[DUM_LEN + 1];
4015
4016 for (;;)
4017 {
4018 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01004019 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 break;
4021 }
4022 return retesc;
4023 }
4024
4025 /*
4026 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004027 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004029 if (wait_time == -1L || wait_time > 10L)
4030 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
4032 /*
4033 * Fill up to a third of the buffer, because each character may be
4034 * tripled below.
4035 */
4036 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
4037 }
4038
Bram Moolenaar30613902019-12-01 22:11:18 +01004039 // If the typebuf was changed further down, it is like nothing was added by
4040 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (typebuf_changed(tb_change_cnt))
4042 return 0;
4043
Bram Moolenaar30613902019-12-01 22:11:18 +01004044 // Note the change in the typeahead buffer, this matters for when
4045 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4046 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004047 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4048 typebuf.tb_change_cnt = 1;
4049
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004050 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051}
4052
4053/*
4054 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004055 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 * Returns the new length.
4057 */
4058 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004059fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 int i;
4062 char_u *p = buf;
4063
4064 /*
4065 * Two characters are special: NUL and K_SPECIAL.
4066 * When compiled With the GUI CSI is also special.
4067 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4068 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4069 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 */
4071 for (i = len; --i >= 0; ++p)
4072 {
4073#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004074 // When the GUI is used any character can come after a CSI, don't
4075 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (gui.in_use && p[0] == CSI && i >= 2)
4077 {
4078 p += 2;
4079 i -= 2;
4080 }
zeertzjq646bb722022-02-16 17:51:47 +00004081# ifndef MSWIN
4082 // When not on MS-Windows and the GUI is not used CSI needs to be
4083 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 else if (!gui.in_use && p[0] == CSI)
4085 {
4086 mch_memmove(p + 3, p + 1, (size_t)i);
4087 *p++ = K_SPECIAL;
4088 *p++ = KS_EXTRA;
4089 *p = (int)KE_CSI;
4090 len += 2;
4091 }
zeertzjq646bb722022-02-16 17:51:47 +00004092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else
4094#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004095 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004096 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004097 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004098#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004099 // Win32 console passes modifiers
4100 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004101# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004102 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004103# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004104 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#endif
4106 ))
4107 {
4108 mch_memmove(p + 3, p + 1, (size_t)i);
4109 p[2] = K_THIRD(p[0]);
4110 p[1] = K_SECOND(p[0]);
4111 p[0] = K_SPECIAL;
4112 p += 2;
4113 len += 2;
4114 }
4115 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004116 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return len;
4118}
4119
4120#if defined(USE_INPUT_BUF) || defined(PROTO)
4121/*
4122 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4123 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004124 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004125 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 */
4127 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004128input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004131# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4132 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133# endif
4134 );
4135}
4136#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004137
4138/*
4139 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4140 * typeahead.
4141 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004142 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004143getcmdkeycmd(
4144 int promptc UNUSED,
4145 void *cookie UNUSED,
4146 int indent UNUSED,
4147 getline_opt_T do_concat UNUSED)
4148{
4149 garray_T line_ga;
4150 int c1 = -1;
4151 int c2;
4152 int cmod = 0;
4153 int aborted = FALSE;
4154
4155 ga_init2(&line_ga, 1, 32);
4156
4157 // no mapping for these characters
4158 no_mapping++;
4159
4160 got_int = FALSE;
4161 while (c1 != NUL && !aborted)
4162 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004163 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004164 {
4165 aborted = TRUE;
4166 break;
4167 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004168
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004169 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004170 {
4171 // incomplete <Cmd> is an error, because there is not much the user
4172 // could do in this state.
4173 emsg(_(e_cmd_mapping_must_end_with_cr));
4174 aborted = TRUE;
4175 break;
4176 }
4177
4178 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004179 c1 = vgetorpeek(TRUE);
4180
Bram Moolenaar957cf672020-11-12 14:21:06 +01004181 // Get two extra bytes for special keys
4182 if (c1 == K_SPECIAL)
4183 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004184 c1 = vgetorpeek(TRUE);
4185 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004186 if (c1 == KS_MODIFIER)
4187 {
4188 cmod = c2;
4189 continue;
4190 }
4191 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004192
4193 // K_ESC is used to avoid ambiguity with the single Esc character
4194 // that might be the start of an escape sequence. Convert it back
4195 // to a single Esc here.
4196 if (c1 == K_ESC)
4197 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004198 }
4199
4200 if (got_int)
4201 aborted = TRUE;
4202 else if (c1 == '\r' || c1 == '\n')
4203 c1 = NUL; // end the line
4204 else if (c1 == ESC)
4205 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004206 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004207 {
4208 // give a nicer error message for this special case
4209 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4210 aborted = TRUE;
4211 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004212 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004213 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004214 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004215 }
4216 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004217 {
4218 if (cmod != 0)
4219 {
4220 ga_append(&line_ga, K_SPECIAL);
4221 ga_append(&line_ga, KS_MODIFIER);
4222 ga_append(&line_ga, cmod);
4223 }
4224 if (IS_SPECIAL(c1))
4225 {
4226 ga_append(&line_ga, K_SPECIAL);
4227 ga_append(&line_ga, K_SECOND(c1));
4228 ga_append(&line_ga, K_THIRD(c1));
4229 }
4230 else
4231 ga_append(&line_ga, c1);
4232 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004233
4234 cmod = 0;
4235 }
4236
4237 no_mapping--;
4238
4239 if (aborted)
4240 ga_clear(&line_ga);
4241
4242 return (char_u *)line_ga.ga_data;
4243}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004244
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004245#if defined(FEAT_EVAL) || defined(PROTO)
4246/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004247 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4248 * is set when redo'ing.
4249 * Put this SID in the redo buffer, so that "." will use the same script
4250 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004251 */
4252 void
4253may_add_last_used_map_to_redobuff(void)
4254{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004255 char_u buf[3 + 20];
4256 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004257
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004258 if (last_used_map != NULL)
4259 sid = last_used_map->m_script_ctx.sc_sid;
4260 if (sid < 0)
4261 sid = last_used_sid;
4262
4263 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004264 return;
4265
4266 // <K_SID>{nr};
4267 buf[0] = K_SPECIAL;
4268 buf[1] = KS_EXTRA;
4269 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004270 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004271 add_buff(&redobuff, buf, -1L);
4272}
4273#endif
4274
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004275 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004276do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004277{
4278 int res;
4279#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004280 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004281
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004282 if (key == K_SCRIPT_COMMAND
4283 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004284 {
4285 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004286 if (last_used_map != NULL)
4287 current_sctx = last_used_map->m_script_ctx;
4288 else
4289 {
4290 current_sctx.sc_sid = last_used_sid;
4291 current_sctx.sc_lnum = 0;
4292 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4293 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004294 }
4295#endif
4296
4297 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4298
4299#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004300 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004301 current_sctx = save_current_sctx;
4302#endif
4303
4304 return res;
4305}
4306
4307#if defined(FEAT_EVAL) || defined(PROTO)
4308 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004309reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004310{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004311 if (last_used_map != mp)
4312 return;
4313
4314 last_used_map = NULL;
4315 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004316}
4317#endif