blob: 96e180f4ae1a99ee1407b919e14fd6b758a4d602 [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 {
Christian Brabandt322ba912024-08-25 21:33:03 +0200449 // remove mapped characters at the start only,
450 // but only when enough space left in typebuf
451 if (typebuf.tb_off + typebuf.tb_maplen >= typebuf.tb_buflen)
452 {
453 typebuf.tb_off = MAXMAPLEN;
454 typebuf.tb_len = 0;
455 }
456 else
457 {
458 typebuf.tb_off += typebuf.tb_maplen;
459 typebuf.tb_len -= typebuf.tb_maplen;
460 }
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100461#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
462 if (typebuf.tb_len == 0)
463 typebuf_was_filled = FALSE;
464#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200465 }
466 else
467 {
468 // remove typeahead
469 if (flush_typeahead == FLUSH_INPUT)
470 // We have to get all characters, because we may delete the first
471 // part of an escape sequence. In an xterm we get one char at a
472 // time and we have to get them all.
473 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
474 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 typebuf.tb_off = MAXMAPLEN;
476 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200477#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100478 // Reset the flag that text received from a client or from feedkeys()
479 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200480 typebuf_was_filled = FALSE;
481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 typebuf.tb_maplen = 0;
484 typebuf.tb_silent = 0;
485 cmd_silent = FALSE;
486 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200487 if (++typebuf.tb_change_cnt == 0)
488 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489}
490
491/*
492 * The previous contents of the redo buffer is kept in old_redobuffer.
493 * This is used for the CTRL-O <.> command in insert mode.
494 */
495 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100496ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000498 if (block_redo)
499 return;
500
501 free_buff(&old_redobuff);
502 old_redobuff = redobuff;
503 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504}
505
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100506/*
507 * Discard the contents of the redo buffer and restore the previous redo
508 * buffer.
509 */
510 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100511CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100512{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000513 if (block_redo)
514 return;
515
516 free_buff(&redobuff);
517 redobuff = old_redobuff;
518 old_redobuff.bh_first.b_next = NULL;
519 start_stuff();
520 while (read_readbuffers(TRUE) != NUL)
521 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100522}
523
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524/*
525 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
526 * Used before executing autocommands and user functions.
527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200529saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 char_u *s;
532
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200533 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200534 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200535 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200536 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaar30613902019-12-01 22:11:18 +0100538 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200539 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000540 if (s == NULL)
541 return;
542
543 add_buff(&redobuff, s, -1L);
544 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545}
546
547/*
548 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
549 * Used after executing autocommands and user functions.
550 */
551 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200552restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200554 free_buff(&redobuff);
555 redobuff = save_redo->sr_redobuff;
556 free_buff(&old_redobuff);
557 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560/*
561 * Append "s" to the redo buffer.
562 * K_SPECIAL and CSI should already have been escaped.
563 */
564 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100565AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 if (!block_redo)
568 add_buff(&redobuff, s, -1L);
569}
570
571/*
572 * Append to Redo buffer literally, escaping special characters with CTRL-V.
573 * K_SPECIAL and CSI are escaped as well.
574 */
575 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100576AppendToRedobuffLit(
577 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100578 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000580 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 int c;
582 char_u *start;
583
584 if (block_redo)
585 return;
586
Bram Moolenaarebefac62005-12-28 22:39:57 +0000587 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100589 // Put a string of normal characters in the redo buffer (that's
590 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000592 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 ++s;
594
Bram Moolenaar30613902019-12-01 22:11:18 +0100595 // Don't put '0' or '^' as last character, just in case a CTRL-D is
596 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
598 --s;
599 if (s > start)
600 add_buff(&redobuff, start, (long)(s - start));
601
Bram Moolenaarebefac62005-12-28 22:39:57 +0000602 if (*s == NUL || (len >= 0 && s - str >= len))
603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaar30613902019-12-01 22:11:18 +0100605 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000606 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100607 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000608 c = mb_cptr2char_adv(&s);
609 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000610 c = *s++;
611 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
612 add_char_buff(&redobuff, Ctrl_V);
613
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000614 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000615 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000616 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000617 else
618 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
620}
621
622/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100623 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
624 * and escaping other K_SPECIAL and CSI bytes.
625 */
626 void
627AppendToRedobuffSpec(char_u *s)
628{
zeertzjq30b6d612023-05-07 17:39:23 +0100629 if (block_redo)
630 return;
631
zeertzjq3ab3a862023-05-06 16:22:04 +0100632 while (*s != NUL)
633 {
634 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
635 {
zeertzjq30b6d612023-05-07 17:39:23 +0100636 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100637 add_buff(&redobuff, s, 3L);
638 s += 3;
639 }
640 else
641 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
642 }
643}
644
645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * Append a character to the redo buffer.
647 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
648 */
649 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100650AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 if (!block_redo)
653 add_char_buff(&redobuff, c);
654}
655
656/*
657 * Append a number to the redo buffer.
658 */
659 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100660AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
662 if (!block_redo)
663 add_num_buff(&redobuff, n);
664}
665
666/*
667 * Append string "s" to the stuff buffer.
668 * CSI and K_SPECIAL must already have been escaped.
669 */
670 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100671stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100673 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674}
675
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200676/*
677 * Append string "s" to the redo stuff buffer.
678 * CSI and K_SPECIAL must already have been escaped.
679 */
680 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100681stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200682{
683 add_buff(&readbuf2, s, -1L);
684}
685
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200686 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100687stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100689 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
692#if defined(FEAT_EVAL) || defined(PROTO)
693/*
694 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
695 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200696 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
698 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100699stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200701 int c;
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 while (*s != NUL)
704 {
705 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
706 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100707 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 stuffReadbuffLen(s, 3L);
709 s += 3;
710 }
711 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200712 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100713 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200714 if (c == CAR || c == NL || c == ESC)
715 c = ' ';
716 stuffcharReadbuff(c);
717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719}
720#endif
721
722/*
723 * Append a character to the stuff buffer.
724 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
725 */
726 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100727stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100729 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730}
731
732/*
733 * Append a number to the stuff buffer.
734 */
735 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100736stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100738 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739}
740
741/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200742 * Stuff a string into the typeahead buffer, such that edit() will insert it
743 * literally ("literally" TRUE) or interpret is as typed characters.
744 */
745 void
746stuffescaped(char_u *arg, int literally)
747{
748 int c;
749 char_u *start;
750
751 while (*arg != NUL)
752 {
753 // Stuff a sequence of normal ASCII characters, that's fast. Also
754 // stuff K_SPECIAL to get the effect of a special key when "literally"
755 // is TRUE.
756 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000757 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200758 || (*arg == K_SPECIAL && !literally))
759 ++arg;
760 if (arg > start)
761 stuffReadbuffLen(start, (long)(arg - start));
762
763 // stuff a single special character
764 if (*arg != NUL)
765 {
766 if (has_mbyte)
767 c = mb_cptr2char_adv(&arg);
768 else
769 c = *arg++;
770 if (literally && ((c < ' ' && c != TAB) || c == DEL))
771 stuffcharReadbuff(Ctrl_V);
772 stuffcharReadbuff(c);
773 }
774 }
775}
776
777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
779 * multibyte characters.
780 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100781 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
782 * otherwise.
783 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
785 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100786read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100788 static buffblock_T *bp;
789 static char_u *p;
790 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100791 int n;
792 char_u buf[MB_MAXBYTES + 1];
793 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795 if (init)
796 {
797 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200798 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200800 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if (bp == NULL)
802 return FAIL;
803 p = bp->b_str;
804 return OK;
805 }
806 if ((c = *p) != NUL)
807 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100808 // Reverse the conversion done by add_char_buff()
809 // For a multi-byte character get all the bytes and return the
810 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
812 n = MB_BYTE2LEN_CHECK(c);
813 else
814 n = 1;
815 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100817 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
819 c = TO_SPECIAL(p[1], p[2]);
820 p += 2;
821 }
822#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100823 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 p += 2;
825#endif
826 if (*++p == NUL && bp->b_next != NULL)
827 {
828 bp = bp->b_next;
829 p = bp->b_str;
830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100832 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 if (n != 1)
835 c = (*mb_ptr2char)(buf);
836 break;
837 }
838 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100839 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843
844 return c;
845}
846
847/*
848 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
849 * If old_redo is TRUE, use old_redobuff instead of redobuff.
850 * The escaped K_SPECIAL and CSI are copied without translation.
851 */
852 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100853copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
855 int c;
856
857 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100858 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100862 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * Insert the redo count into the command.
864 * If "old_redo" is TRUE, the last but one command is repeated
865 * instead of the last command (inserting text). This is used for
866 * CTRL-O <.> in insert mode
867 *
868 * return FAIL for failure, OK otherwise
869 */
870 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100871start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 int c;
874
Bram Moolenaar30613902019-12-01 22:11:18 +0100875 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (read_redo(TRUE, old_redo) == FAIL)
877 return FAIL;
878
879 c = read_redo(FALSE, old_redo);
880
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100881#ifdef FEAT_EVAL
882 if (c == K_SID)
883 {
884 // Copy the <SID>{sid}; sequence
885 add_char_buff(&readbuf2, c);
886 for (;;)
887 {
888 c = read_redo(FALSE, old_redo);
889 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100890 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100891 break;
892 }
893 c = read_redo(FALSE, old_redo);
894 }
895#endif
896
Bram Moolenaar30613902019-12-01 22:11:18 +0100897 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (c == '"')
899 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100900 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 c = read_redo(FALSE, old_redo);
902
Bram Moolenaar30613902019-12-01 22:11:18 +0100903 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (c >= '1' && c < '9')
905 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100906 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200907
Bram Moolenaar30613902019-12-01 22:11:18 +0100908 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200909 if (c == '=')
910 {
911 add_char_buff(&readbuf2, CAR);
912 cmd_silent = TRUE;
913 }
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 c = read_redo(FALSE, old_redo);
916 }
917
Bram Moolenaar30613902019-12-01 22:11:18 +0100918 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 VIsual = curwin->w_cursor;
921 VIsual_active = TRUE;
922 VIsual_select = FALSE;
923 VIsual_reselect = TRUE;
924 redo_VIsual_busy = TRUE;
925 c = read_redo(FALSE, old_redo);
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar30613902019-12-01 22:11:18 +0100928 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (count)
930 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100931 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100933 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100936 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100937 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 copy_redo(old_redo);
939 return OK;
940}
941
942/*
943 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100944 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 * return FAIL for failure, OK otherwise
946 */
947 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100948start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 int c;
951
952 if (read_redo(TRUE, FALSE) == FAIL)
953 return FAIL;
954 start_stuff();
955
Bram Moolenaar30613902019-12-01 22:11:18 +0100956 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 while ((c = read_redo(FALSE, FALSE)) != NUL)
958 {
959 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
960 {
961 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100962 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 break;
964 }
965 }
966
Bram Moolenaar30613902019-12-01 22:11:18 +0100967 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 copy_redo(FALSE);
969 block_redo = TRUE;
970 return OK;
971}
972
973 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100974stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 block_redo = FALSE;
977}
978
979/*
980 * Initialize typebuf.tb_buf to point to typebuf_init.
981 * alloc() cannot be used here: In out-of-memory situations it would
982 * be impossible to type anything.
983 */
984 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100985init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000987 if (typebuf.tb_buf != NULL)
988 return;
989
990 typebuf.tb_buf = typebuf_init;
991 typebuf.tb_noremap = noremapbuf_init;
992 typebuf.tb_buflen = TYPELEN_INIT;
993 typebuf.tb_len = 0;
994 typebuf.tb_off = MAXMAPLEN + 4;
995 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200999 * Returns TRUE when keys cannot be remapped.
1000 */
1001 int
1002noremap_keys(void)
1003{
1004 return KeyNoremap & (RM_NONE|RM_SCRIPT);
1005}
1006
1007/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001008 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
1009 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001011 * If "noremap" is REMAP_YES, new string can be mapped again.
1012 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
1013 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001014 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001015 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001017 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001019 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1020 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001022 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 *
1024 * return FAIL for failure, OK otherwise
1025 */
1026 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001027ins_typebuf(
1028 char_u *str,
1029 int noremap,
1030 int offset,
1031 int nottyped,
1032 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 char_u *s1, *s2;
1035 int newlen;
1036 int addlen;
1037 int i;
1038 int newoff;
1039 int val;
1040 int nrm;
1041
1042 init_typebuf();
1043 if (++typebuf.tb_change_cnt == 0)
1044 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001045 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (offset == 0 && addlen <= typebuf.tb_off)
1050 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001051 /*
1052 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 typebuf.tb_off -= addlen;
1055 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1056 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001057 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1058 >= addlen + 3 * (MAXMAPLEN + 4))
1059 {
1060 /*
1061 * Buffer is empty and string fits in the existing buffer.
1062 * Leave some space before and after, if possible.
1063 */
1064 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1065 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 else
1068 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001069 int extra;
1070
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001071 /*
1072 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001073 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001074 * characters. We add some extra room to avoid having to allocate too
1075 * often.
1076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001078 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1079 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001081 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001082 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 setcursor();
1084 return FAIL;
1085 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001086 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001088 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 return FAIL;
1090 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001091 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
1093 vim_free(s1);
1094 return FAIL;
1095 }
1096 typebuf.tb_buflen = newlen;
1097
Bram Moolenaar30613902019-12-01 22:11:18 +01001098 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1100 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001101 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001103 // copy the old chars, after the insertion point, including the NUL at
1104 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 mch_memmove(s1 + newoff + offset + addlen,
1106 typebuf.tb_buf + typebuf.tb_off + offset,
1107 (size_t)(typebuf.tb_len - offset + 1));
1108 if (typebuf.tb_buf != typebuf_init)
1109 vim_free(typebuf.tb_buf);
1110 typebuf.tb_buf = s1;
1111
1112 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1113 (size_t)offset);
1114 mch_memmove(s2 + newoff + offset + addlen,
1115 typebuf.tb_noremap + typebuf.tb_off + offset,
1116 (size_t)(typebuf.tb_len - offset));
1117 if (typebuf.tb_noremap != noremapbuf_init)
1118 vim_free(typebuf.tb_noremap);
1119 typebuf.tb_noremap = s2;
1120
1121 typebuf.tb_off = newoff;
1122 }
1123 typebuf.tb_len += addlen;
1124
Bram Moolenaar30613902019-12-01 22:11:18 +01001125 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (noremap == REMAP_SCRIPT)
1127 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001128 else if (noremap == REMAP_SKIP)
1129 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 else
1131 val = RM_NONE;
1132
1133 /*
1134 * Adjust typebuf.tb_noremap[] for the new characters:
1135 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1136 * (sometimes) not remappable
1137 * If noremap == REMAP_YES: all the new characters are mappable
1138 * If noremap > 0: "noremap" characters are not remappable, the rest
1139 * mappable
1140 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001141 if (noremap == REMAP_SKIP)
1142 nrm = 1;
1143 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 nrm = addlen;
1145 else
1146 nrm = noremap;
1147 for (i = 0; i < addlen; ++i)
1148 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1149 (--nrm >= 0) ? val : RM_YES;
1150
Bram Moolenaar30613902019-12-01 22:11:18 +01001151 // tb_maplen and tb_silent only remember the length of mapped and/or
1152 // silent mappings at the start of the buffer, assuming that a mapped
1153 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 if (nottyped || typebuf.tb_maplen > offset)
1155 typebuf.tb_maplen += addlen;
1156 if (silent || typebuf.tb_silent > offset)
1157 {
1158 typebuf.tb_silent += addlen;
1159 cmd_silent = TRUE;
1160 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001161 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 typebuf.tb_no_abbr_cnt += addlen;
1163
1164 return OK;
1165}
1166
1167/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001168 * Put character "c" back into the typeahead buffer.
1169 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001170 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1171 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001172 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001173 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001174 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001175ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001176{
zeertzjq6cac7702022-01-04 18:01:21 +00001177 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001178 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001179
zeertzjq2e7cba32022-06-10 15:30:32 +01001180 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001181 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001182 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001183}
1184
1185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001187 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001188 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1190 * changed it was reallocated and the old pointer can no longer be used.
1191 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1192 * that was just added.
1193 */
1194 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001195typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001196 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001199#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1200 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#endif
1202 ));
1203}
1204
1205/*
1206 * Return TRUE if there are no characters in the typeahead buffer that have
1207 * not been typed (result from a mapping or come from ":normal").
1208 */
1209 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001210typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
1212 return typebuf.tb_maplen == 0;
1213}
1214
1215/*
1216 * Return the number of characters that are mapped (or not typed).
1217 */
1218 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001219typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 return typebuf.tb_maplen;
1222}
1223
1224/*
1225 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1226 */
1227 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001228del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 int i;
1231
1232 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001233 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235 typebuf.tb_len -= len;
1236
1237 /*
1238 * Easy case: Just increase typebuf.tb_off.
1239 */
1240 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1241 >= 3 * MAXMAPLEN + 3)
1242 typebuf.tb_off += len;
1243 /*
1244 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1245 */
1246 else
1247 {
1248 i = typebuf.tb_off + offset;
1249 /*
1250 * Leave some extra room at the end to avoid reallocation.
1251 */
1252 if (typebuf.tb_off > MAXMAPLEN)
1253 {
1254 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1255 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1256 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1257 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1258 typebuf.tb_off = MAXMAPLEN;
1259 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001260 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1262 typebuf.tb_buf + i + len,
1263 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001264 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1266 typebuf.tb_noremap + i + len,
1267 (size_t)(typebuf.tb_len - offset));
1268 }
1269
Bram Moolenaar30613902019-12-01 22:11:18 +01001270 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 if (typebuf.tb_maplen < offset + len)
1273 typebuf.tb_maplen = offset;
1274 else
1275 typebuf.tb_maplen -= len;
1276 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001277 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 if (typebuf.tb_silent < offset + len)
1280 typebuf.tb_silent = offset;
1281 else
1282 typebuf.tb_silent -= len;
1283 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001284 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
1286 if (typebuf.tb_no_abbr_cnt < offset + len)
1287 typebuf.tb_no_abbr_cnt = offset;
1288 else
1289 typebuf.tb_no_abbr_cnt -= len;
1290 }
1291
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001292#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001293 // Reset the flag that text received from a client or from feedkeys()
1294 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001295 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296#endif
1297 if (++typebuf.tb_change_cnt == 0)
1298 typebuf.tb_change_cnt = 1;
1299}
1300
zeertzjq094c4392024-04-18 22:09:37 +02001301/*
1302 * State for adding bytes to a recording or 'showcmd'.
1303 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001304typedef struct
1305{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001306 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq6b13e3d2024-04-22 21:04:29 +02001307 int prev_c;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001308 size_t buflen;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001309 unsigned pending_special;
1310 unsigned pending_mbyte;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001311} gotchars_state_T;
1312
1313/*
1314 * Add a single byte to a recording or 'showcmd'.
1315 * Return TRUE if a full key has been received, FALSE otherwise.
1316 */
1317 static int
1318gotchars_add_byte(gotchars_state_T *state, char_u byte)
1319{
1320 int c = state->buf[state->buflen++] = byte;
1321 int retval = FALSE;
zeertzjq6b13e3d2024-04-22 21:04:29 +02001322 int in_special = state->pending_special > 0;
1323 int in_mbyte = state->pending_mbyte > 0;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001324
zeertzjq6b13e3d2024-04-22 21:04:29 +02001325 if (in_special)
1326 state->pending_special--;
1327 else if (c == K_SPECIAL
zeertzjqacdfb8a2024-04-17 21:28:54 +02001328#ifdef FEAT_GUI
1329 || c == CSI
1330#endif
zeertzjq6b13e3d2024-04-22 21:04:29 +02001331 )
1332 // When receiving a special key sequence, store it until we have all
1333 // the bytes and we can decide what to do with it.
1334 state->pending_special = 2;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001335
zeertzjq6b13e3d2024-04-22 21:04:29 +02001336 if (state->pending_special > 0)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001337 goto ret_false;
1338
zeertzjq6b13e3d2024-04-22 21:04:29 +02001339 if (in_mbyte)
1340 state->pending_mbyte--;
1341 else
zeertzjqacdfb8a2024-04-17 21:28:54 +02001342 {
zeertzjq6b13e3d2024-04-22 21:04:29 +02001343 if (in_special)
zeertzjqacdfb8a2024-04-17 21:28:54 +02001344 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001345 if (state->prev_c == KS_MODIFIER)
1346 // When receiving a modifier, wait for the modified key.
1347 goto ret_false;
1348 c = TO_SPECIAL(state->prev_c, c);
1349 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1350 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1351 // in a recording.
1352 state->buflen = 0;
1353 }
1354 // When receiving a multibyte character, store it until we have all
1355 // the bytes, so that it won't be split between two buffer blocks,
1356 // and delete_buff_tail() will work properly.
zeertzjq6b13e3d2024-04-22 21:04:29 +02001357 state->pending_mbyte = MB_BYTE2LEN_CHECK(c) - 1;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001358 }
zeertzjq6b13e3d2024-04-22 21:04:29 +02001359
1360 if (state->pending_mbyte > 0)
1361 goto ret_false;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001362
1363 retval = TRUE;
1364ret_false:
1365 state->prev_c = c;
1366 return retval;
1367}
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369/*
1370 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001371 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 */
1373 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001374gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001376 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001377 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001378 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001379 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
zeertzjqacdfb8a2024-04-17 21:28:54 +02001381 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001383 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001384 continue;
1385
Bram Moolenaar30613902019-12-01 22:11:18 +01001386 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001387 for (i = 0; i < state.buflen; ++i)
1388 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001390 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001392 state.buf[state.buflen] = NUL;
1393 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001394 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001395 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001397 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 may_sync_undo();
1401
1402#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001403 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 debug_did_msg = FALSE;
1405#endif
1406
Bram Moolenaar30613902019-12-01 22:11:18 +01001407 // Since characters have been typed, consider the following to be in
1408 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 ++maptick;
1410}
1411
1412/*
zeertzjqbf321802024-01-28 19:03:00 +01001413 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001414 */
1415 void
zeertzjqbf321802024-01-28 19:03:00 +01001416gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001417{
zeertzjqbf321802024-01-28 19:03:00 +01001418 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001419 gotchars(nop_buf, 3);
1420}
1421
1422/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001423 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1424 * character back into the typeahead buffer, thus gotchars() will be called
1425 * again.
1426 * Only affects recorded characters.
1427 */
1428 void
1429ungetchars(int len)
1430{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001431 if (reg_recording == 0)
1432 return;
1433
1434 delete_buff_tail(&recordbuff, len);
1435 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001436}
1437
1438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 * Sync undo. Called when typed characters are obtained from the typeahead
1440 * buffer, or when a menu is used.
1441 * Do not sync:
1442 * - In Insert mode, unless cursor key has been used.
1443 * - While reading a script file.
1444 * - When no_u_sync is non-zero.
1445 */
1446 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001447may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
Bram Moolenaar24959102022-05-07 20:01:16 +01001449 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001450 && scriptin[curscript] == NULL)
1451 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452}
1453
1454/*
1455 * Make "typebuf" empty and allocate new buffers.
1456 * Returns FAIL when out of memory.
1457 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001458 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001459alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 typebuf.tb_buf = alloc(TYPELEN_INIT);
1462 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1463 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1464 {
1465 free_typebuf();
1466 return FAIL;
1467 }
1468 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001469 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 typebuf.tb_len = 0;
1471 typebuf.tb_maplen = 0;
1472 typebuf.tb_silent = 0;
1473 typebuf.tb_no_abbr_cnt = 0;
1474 if (++typebuf.tb_change_cnt == 0)
1475 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001476#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1477 typebuf_was_filled = FALSE;
1478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 return OK;
1480}
1481
1482/*
1483 * Free the buffers of "typebuf".
1484 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001485 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001486free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001488 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001489 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001490 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001491 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001492 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001493 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001494 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001495 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496}
1497
1498/*
1499 * When doing ":so! file", the current typeahead needs to be saved, and
1500 * restored when "file" has been read completely.
1501 */
1502static typebuf_T saved_typebuf[NSCRIPT];
1503
1504 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001505save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 init_typebuf();
1508 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001509 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if (alloc_typebuf() == FAIL)
1511 {
1512 closescript();
1513 return FAIL;
1514 }
1515 return OK;
1516}
1517
Bram Moolenaar30613902019-12-01 22:11:18 +01001518static int old_char = -1; // character put back by vungetc()
1519static int old_mod_mask; // mod_mask for ungotten character
1520static int old_mouse_row; // mouse_row related to old_char
1521static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001522static int old_KeyStuffed; // whether old_char was stuffed
1523
zeertzjq6d4e7252022-04-07 13:58:04 +01001524static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001525{
1526 // If the old character was not stuffed and characters have been added to
1527 // the stuff buffer, need to first get the stuffed characters instead.
1528 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1529}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531/*
1532 * Save all three kinds of typeahead, so that the user must type at a prompt.
1533 */
1534 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001535save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 tp->save_typebuf = typebuf;
1538 tp->typebuf_valid = (alloc_typebuf() == OK);
1539 if (!tp->typebuf_valid)
1540 typebuf = tp->save_typebuf;
1541
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001542 tp->old_char = old_char;
1543 tp->old_mod_mask = old_mod_mask;
1544 old_char = -1;
1545
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001546 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001547 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001548 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001549 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550# ifdef USE_INPUT_BUF
1551 tp->save_inputbuf = get_input_buf();
1552# endif
1553}
1554
1555/*
1556 * Restore the typeahead to what it was before calling save_typeahead().
1557 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001558 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
1560 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001561restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 if (tp->typebuf_valid)
1564 {
1565 free_typebuf();
1566 typebuf = tp->save_typebuf;
1567 }
1568
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001569 old_char = tp->old_char;
1570 old_mod_mask = tp->old_mod_mask;
1571
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001572 free_buff(&readbuf1);
1573 readbuf1 = tp->save_readbuf1;
1574 free_buff(&readbuf2);
1575 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001577 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578# endif
1579}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581/*
1582 * Open a new script file for the ":source!" command.
1583 */
1584 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001585openscript(
1586 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001587 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
1589 if (curscript + 1 == NSCRIPT)
1590 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001591 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return;
1593 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001594
1595 // Disallow sourcing a file in the sandbox, the commands would be executed
1596 // later, possibly outside of the sandbox.
1597 if (check_secure())
1598 return;
1599
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001600#ifdef FEAT_EVAL
1601 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001602 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001603 return;
1604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar30613902019-12-01 22:11:18 +01001606 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001608 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 expand_env(name, NameBuff, MAXPATHL);
1610 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1611 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001612 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (curscript)
1614 --curscript;
1615 return;
1616 }
1617 if (save_typebuf() == FAIL)
1618 return;
1619
1620 /*
1621 * Execute the commands from the file right now when using ":source!"
1622 * after ":global" or ":argdo" or in a loop. Also when another command
1623 * follows. This means the display won't be updated. Don't do this
1624 * always, "make test" would fail.
1625 */
1626 if (directly)
1627 {
1628 oparg_T oa;
1629 int oldcurscript;
1630 int save_State = State;
1631 int save_restart_edit = restart_edit;
1632 int save_insertmode = p_im;
1633 int save_finish_op = finish_op;
1634 int save_msg_scroll = msg_scroll;
1635
Bram Moolenaar24959102022-05-07 20:01:16 +01001636 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001637 msg_scroll = FALSE; // no msg scrolling in Normal mode
1638 restart_edit = 0; // don't go to Insert mode
1639 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 clear_oparg(&oa);
1641 finish_op = FALSE;
1642
1643 oldcurscript = curscript;
1644 do
1645 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001646 update_topline_cursor(); // update cursor position and topline
1647 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001648 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
1650 while (scriptin[oldcurscript] != NULL);
1651
1652 State = save_State;
1653 msg_scroll = save_msg_scroll;
1654 restart_edit = save_restart_edit;
1655 p_im = save_insertmode;
1656 finish_op = save_finish_op;
1657 }
1658}
1659
1660/*
1661 * Close the currently active input script.
1662 */
1663 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001664closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 free_typebuf();
1667 typebuf = saved_typebuf[curscript];
1668
1669 fclose(scriptin[curscript]);
1670 scriptin[curscript] = NULL;
1671 if (curscript > 0)
1672 --curscript;
1673}
1674
Bram Moolenaarea408852005-06-25 22:49:46 +00001675#if defined(EXITFREE) || defined(PROTO)
1676 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001677close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001678{
1679 while (scriptin[0] != NULL)
1680 closescript();
1681}
1682#endif
1683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684/*
1685 * Return TRUE when reading keys from a script file.
1686 */
1687 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001688using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 return scriptin[curscript] != NULL;
1691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001694 * This function is called just before doing a blocking wait. Thus after
1695 * waiting 'updatetime' for a character to arrive.
1696 */
1697 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001698before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001699{
1700 updatescript(0);
1701#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001702 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001703 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001704#endif
1705}
1706
1707/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001708 * updatescript() is called when a character can be written into the script
1709 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
1711 * All the changed memfiles are synced if c == 0 or when the number of typed
1712 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1713 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001714 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001715updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 static int count = 0;
1718
1719 if (c && scriptout)
1720 putc(c, scriptout);
1721 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1722 {
1723 ml_sync_all(c == 0, TRUE);
1724 count = 0;
1725 }
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02001726#ifdef FEAT_EVAL
1727 if (typedchars_pos < MAXMAPLEN)
1728 {
1729 typedchars[typedchars_pos] = c;
1730 typedchars_pos++;
1731 }
1732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733}
1734
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001736 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1737 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001738 */
1739 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001740merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001741{
1742 int c = c_arg;
1743
Bram Moolenaarea83c192023-03-18 17:22:46 +00001744 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001745 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001746 {
1747 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001748 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001749 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001750 if (c == NUL)
1751 c = K_ZERO;
1752 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001753 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001754 // CTRL-6 is equivalent to CTRL-^
1755 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001756#ifdef FEAT_GUI_GTK
1757 // These mappings look arbitrary at the first glance, but in fact
1758 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1759 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1760 // more sense and is consistent with usual terminal behaviour).
1761 else if (c == '2')
1762 c = NUL;
1763 else if (c >= '3' && c <= '7')
1764 c = c ^ 0x28;
1765 else if (c == '8')
1766 c = BS;
1767 else if (c == '?')
1768 c = DEL;
1769#endif
1770 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001771 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001772 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001773
1774 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001775 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001776 && c >= 0 && c <= 127)
1777 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001778 // Some terminals (esp. Kitty) do not include Shift in the character.
1779 // Apply it here to get consistency across terminals. Only do ASCII
1780 // letters, for other characters it depends on the keyboard layout.
1781 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1782 {
1783 c += 'a' - 'A';
1784 *modifiers &= ~MOD_MASK_SHIFT;
1785 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001786 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001787 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001788 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001789
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001790 return c;
1791}
1792
1793/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001794 * Add a single byte to 'showcmd' for a partially matched mapping.
1795 * Call add_to_showcmd() if a full key has been received.
1796 */
1797 static void
1798add_byte_to_showcmd(char_u byte)
1799{
1800 static gotchars_state_T state;
1801 char_u *ptr;
1802 int modifiers = 0;
1803 int c = NUL;
1804
1805 if (!p_sc || msg_silent != 0)
1806 return;
1807
1808 if (!gotchars_add_byte(&state, byte))
1809 return;
1810
1811 state.buf[state.buflen] = NUL;
1812 state.buflen = 0;
1813
1814 ptr = state.buf;
1815 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1816 {
1817 modifiers = ptr[2];
1818 ptr += 3;
1819 }
1820
1821 if (*ptr != NUL)
1822 {
1823 char_u *mb_ptr = mb_unescape(&ptr);
1824
1825 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1826 if (c <= 0x7f)
1827 {
1828 // Merge modifiers into the key to make the result more readable.
1829 int modifiers_after = modifiers;
1830 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1831
1832 if (modifiers_after == 0)
1833 {
1834 modifiers = 0;
1835 c = mod_c;
1836 }
1837 }
1838 }
1839
1840 // TODO: is there a more readable and yet compact representation of
1841 // modifiers and special keys?
1842 if (modifiers != 0)
1843 {
1844 add_to_showcmd(K_SPECIAL);
1845 add_to_showcmd(KS_MODIFIER);
1846 add_to_showcmd(modifiers);
1847 }
1848 if (c != NUL)
1849 add_to_showcmd(c);
1850 while (*ptr != NUL)
1851 add_to_showcmd(*ptr++);
1852}
1853
1854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 * Get the next input character.
1856 * Can return a special key or a multi-byte character.
1857 * Can return NUL when called recursively, use safe_vgetc() if that's not
1858 * wanted.
1859 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1860 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001861 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 */
1863 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001864vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
1866 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001868 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001871#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001872 // Do garbage collection when garbagecollect() was called previously and
1873 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001874 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001875 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001876#endif
1877
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 /*
1879 * If a character was put back with vungetc, it was already processed.
1880 * Return it directly.
1881 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001882 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 c = old_char;
1885 old_char = -1;
1886 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001887 mouse_row = old_mouse_row;
1888 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
zeertzjq81b46a62022-04-09 17:58:49 +01001892 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001893 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001894
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001895 mod_mask = 0;
1896 vgetc_mod_mask = 0;
1897 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001898
1899 // last_recorded_len can be larger than last_vgetc_recorded_len
1900 // if peeking records more
1901 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001902
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001903 for (;;) // this is done twice if there are modifiers
1904 {
1905 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001906
Bram Moolenaar78aed952022-09-24 15:36:35 +01001907 // No mapping after modifier has been read, using an input method
1908 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001909 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001910#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001911 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001912#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001913#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001914 || popup_no_mapping()
1915#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001916 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001918 ++no_mapping;
1919 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001920 // mod_mask value may change, remember we did the increment
1921 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001923 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001924 if (did_inc)
1925 {
1926 --no_mapping;
1927 --allow_keys;
1928 }
1929
Christopher Plewright03193062022-11-22 12:58:27 +00001930 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001931 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001932#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001933 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001934#endif
1935 )
1936 {
1937 int save_allow_keys = allow_keys;
1938
1939 ++no_mapping;
1940 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001941 c2 = vgetorpeek(TRUE); // no mapping for these chars
1942 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001943 --no_mapping;
1944 allow_keys = save_allow_keys;
1945 if (c2 == KS_MODIFIER)
1946 {
1947 mod_mask = c;
1948 continue;
1949 }
1950 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001952 // K_ESC is used to avoid ambiguity with the single Esc
1953 // character that might be the start of an escape sequence.
1954 // Convert it back to a single Esc here.
1955 if (c == K_ESC)
1956 c = ESC;
1957
Bram Moolenaar4f974752019-02-17 17:44:42 +01001958#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001959 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1960 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001961 if (
1962# ifdef VIMDLL
1963 gui.in_use &&
1964# endif
1965 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001967 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001968 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001969
1970 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001971 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001972 {
K.Takata54119102022-02-03 13:33:03 +00001973 name[j] = c;
1974 if (j < 199)
1975 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001976 }
K.Takata54119102022-02-03 13:33:03 +00001977 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001978 gui_make_tearoff(name);
1979 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001982#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001983 // GTK: <F10> normally selects the menu, but it's passed until
1984 // here to allow mapping it. Intercept and invoke the GTK
1985 // behavior if it's not mapped.
1986 if (c == K_F10 && gui.menubar != NULL)
1987 {
1988 gtk_menu_shell_select_first(
1989 GTK_MENU_SHELL(gui.menubar), FALSE);
1990 continue;
1991 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001994 // Handle focus event here, so that the caller doesn't need to
1995 // know about it. Return K_IGNORE so that we loop once (needed
1996 // if 'lazyredraw' is set).
1997 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001998 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001999 ui_focus_change(c == K_FOCUSGAINED);
2000 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02002001 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002002
2003 // Translate K_CSI to CSI. The special key is only used to
2004 // avoid it being recognized as the start of a special key.
2005 if (c == K_CSI)
2006 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002008#ifdef FEAT_EVAL
2009 if (c == K_SID)
2010 {
2011 int j;
2012
2013 // Handle <SID>{sid}; Do up to 20 digits for safety.
2014 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002015 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002016 last_used_sid = last_used_sid * 10 + (c - '0');
2017 last_used_map = NULL;
2018 continue;
2019 }
2020#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002021 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002022
zeertzjq90a80022024-07-13 19:06:44 +02002023 // For a multi-byte character get all the bytes and return the
2024 // converted character.
2025 // Note: This will loop until enough bytes are received!
2026 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2027 {
2028 ++no_mapping;
2029 buf[0] = c;
2030 for (i = 1; i < n; ++i)
2031 {
2032 buf[i] = vgetorpeek(TRUE);
2033 if (buf[i] == K_SPECIAL
2034#ifdef FEAT_GUI
2035 || (buf[i] == CSI)
2036#endif
2037 )
2038 {
2039 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2040 // sequence, which represents a K_SPECIAL (0x80),
2041 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2042 // represents a CSI (0x9B),
2043 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2044 // too.
2045 c = vgetorpeek(TRUE);
2046 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
2047 buf[i] = CSI;
2048 }
2049 }
2050 --no_mapping;
2051 c = (*mb_ptr2char)(buf);
2052 }
2053
2054 if (vgetc_char == 0)
2055 {
2056 vgetc_mod_mask = mod_mask;
2057 vgetc_char = c;
2058 }
2059
zeertzjqd9be94c2024-07-14 10:20:20 +02002060 // A keypad or special function key was not mapped, use it like
2061 // its ASCII equivalent.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002062 switch (c)
2063 {
2064 case K_KPLUS: c = '+'; break;
2065 case K_KMINUS: c = '-'; break;
2066 case K_KDIVIDE: c = '/'; break;
2067 case K_KMULTIPLY: c = '*'; break;
2068 case K_KENTER: c = CAR; break;
2069 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002070#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002071 // Can be either '.' or a ',',
2072 // depending on the type of keypad.
2073 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002074#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002075 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002076#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002077 case K_K0: c = '0'; break;
2078 case K_K1: c = '1'; break;
2079 case K_K2: c = '2'; break;
2080 case K_K3: c = '3'; break;
2081 case K_K4: c = '4'; break;
2082 case K_K5: c = '5'; break;
2083 case K_K6: c = '6'; break;
2084 case K_K7: c = '7'; break;
2085 case K_K8: c = '8'; break;
2086 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002087
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002088 case K_XHOME:
2089 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002090 {
2091 c = K_S_HOME;
2092 mod_mask = 0;
2093 }
2094 else if (mod_mask == MOD_MASK_CTRL)
2095 {
2096 c = K_C_HOME;
2097 mod_mask = 0;
2098 }
2099 else
2100 c = K_HOME;
2101 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002102 case K_XEND:
2103 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002104 {
2105 c = K_S_END;
2106 mod_mask = 0;
2107 }
2108 else if (mod_mask == MOD_MASK_CTRL)
2109 {
2110 c = K_C_END;
2111 mod_mask = 0;
2112 }
2113 else
2114 c = K_END;
2115 break;
2116
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002117 case K_XUP: c = K_UP; break;
2118 case K_XDOWN: c = K_DOWN; break;
2119 case K_XLEFT: c = K_LEFT; break;
2120 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002123 break;
2124 }
zeertzjq81b46a62022-04-09 17:58:49 +01002125
2126 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002128
2129#ifdef FEAT_EVAL
2130 /*
2131 * In the main loop "may_garbage_collect" can be set to do garbage
2132 * collection in the first next vgetc(). It's disabled after that to
2133 * avoid internally used Lists and Dicts to be freed.
2134 */
2135 may_garbage_collect = FALSE;
2136#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002137
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002138#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002139 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002140 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002141 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002142 bevalexpr_due_set = FALSE;
2143 ui_remove_balloon();
2144 }
2145#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002146#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002147 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2148 // are filtered.
2149 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002150 {
2151 if (c == Ctrl_C)
2152 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002153 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002154 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002155#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002156
Shougo Matsushita83678842024-07-11 22:05:12 +02002157#ifdef FEAT_EVAL
2158 c = do_key_input_pre(c);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002159
2160 // Clear the next typedchars_pos
2161 typedchars_pos = 0;
Shougo Matsushita83678842024-07-11 22:05:12 +02002162#endif
2163
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002164 // Need to process the character before we know it's safe to do something
2165 // else.
2166 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002167 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002168
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002169 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170}
2171
Shougo Matsushita83678842024-07-11 22:05:12 +02002172#ifdef FEAT_EVAL
2173/*
2174 * Handle the InsertCharPre autocommand.
2175 * "c" is the character that was typed.
2176 * Return new input character.
2177 */
2178 static int
2179do_key_input_pre(int c)
2180{
2181 int res = c;
2182 char_u buf[MB_MAXBYTES + 1];
2183 char_u curr_mode[MODE_MAX_LENGTH];
2184 int save_State = State;
2185 save_v_event_T save_v_event;
2186 dict_T *v_event;
2187
2188 // Return quickly when there is nothing to do.
2189 if (!has_keyinputpre())
2190 return res;
2191
2192 if (IS_SPECIAL(c))
2193 {
2194 buf[0] = K_SPECIAL;
2195 buf[1] = KEY2TERMCAP0(c);
2196 buf[2] = KEY2TERMCAP1(c);
2197 buf[3] = NUL;
2198 }
2199 else
2200 buf[(*mb_char2bytes)(c, buf)] = NUL;
2201
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002202 typedchars[typedchars_pos] = NUL;
2203 vim_unescape_csi(typedchars);
2204
Shougo Matsushita83678842024-07-11 22:05:12 +02002205 get_mode(curr_mode);
2206
2207 // Lock the text to avoid weird things from happening.
2208 ++textlock;
2209 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
2210
2211 v_event = get_v_event(&save_v_event);
2212 (void)dict_add_bool(v_event, "typed", KeyTyped);
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02002213 (void)dict_add_string(v_event, "typedchar", typedchars);
Shougo Matsushita83678842024-07-11 22:05:12 +02002214
2215 if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
2216 && STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
2217 {
2218 // Get the value of v:char. It may be empty or more than one
2219 // character. Only use it when changed, otherwise continue with the
2220 // original character.
2221 char_u *v_char;
2222
2223 v_char = get_vim_var_str(VV_CHAR);
2224
2225 // Convert special bytes when it is special string.
2226 if (STRLEN(v_char) >= 3 && v_char[0] == K_SPECIAL)
2227 res = TERMCAP2KEY(v_char[1], v_char[2]);
2228 else if (STRLEN(v_char) > 0)
2229 res = PTR2CHAR(v_char);
2230 }
2231
2232 restore_v_event(v_event, &save_v_event);
2233
2234 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
2235 --textlock;
2236
2237 // Restore the State, it may have been changed.
2238 State = save_State;
2239
2240 return res;
2241}
2242#endif
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244/*
2245 * Like vgetc(), but never return a NUL when called recursively, get a key
2246 * directly from the user (ignoring typeahead).
2247 */
2248 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002249safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 int c;
2252
2253 c = vgetc();
2254 if (c == NUL)
2255 c = get_keystroke();
2256 return c;
2257}
2258
2259/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002260 * Like safe_vgetc(), but loop to handle K_IGNORE.
2261 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002262 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002263 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002264 static int
2265plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002266{
2267 int c;
2268
2269 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002270 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002271 while (c == K_IGNORE
2272 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2273 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002274 return c;
2275}
2276
2277/*
2278 * Like safe_vgetc(), but loop to handle K_IGNORE.
2279 * Also ignore scrollbar events.
2280 */
2281 int
2282plain_vgetc(void)
2283{
2284 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002285
2286 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002287 // Only handle the first pasted character. Drop the rest, since we
2288 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002289 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2290
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002291 return c;
2292}
2293
2294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 * Check if a character is available, such that vgetc() will not block.
2296 * If the next character is a special character or multi-byte, the returned
2297 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002298 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
2300 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002301vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002303 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002305 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306}
2307
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002308#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309/*
2310 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2311 * codes.
2312 */
2313 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002314vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
2316 int c;
2317
2318 ++no_mapping;
2319 ++allow_keys;
2320 c = vpeekc();
2321 --no_mapping;
2322 --allow_keys;
2323 return c;
2324}
2325#endif
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327/*
2328 * Check if any character is available, also half an escape sequence.
2329 * Trick: when no typeahead found, but there is something in the typeahead
2330 * buffer, it must be an ESC that is recognized as the start of a key code.
2331 */
2332 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002333vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 int c;
2336
2337 c = vpeekc();
2338 if (c == NUL && typebuf.tb_len > 0)
2339 c = ESC;
2340 return c;
2341}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343/*
2344 * Call vpeekc() without causing anything to be mapped.
2345 * Return TRUE if a character is available, FALSE otherwise.
2346 */
2347 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002348char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
2350 int retval;
2351
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002352#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002353 // When test_override("char_avail", 1) was called pretend there is no
2354 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002355 if (disable_char_avail_for_testing)
2356 return FALSE;
2357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 ++no_mapping;
2359 retval = vpeekc();
2360 --no_mapping;
2361 return (retval != NUL);
2362}
2363
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002364#if defined(FEAT_EVAL) || defined(PROTO)
2365/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002366 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002367 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002368 static void
2369getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002370{
2371 varnumber_T n;
2372 int error = FALSE;
2373
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002374 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2375 return;
2376
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002377#ifdef MESSAGE_QUEUE
2378 // vpeekc() used to check for messages, but that caused problems, invoking
2379 // a callback where it was not expected. Some plugins use getchar(1) in a
2380 // loop to await a message, therefore make sure we check for messages here.
2381 parse_queued_messages();
2382#endif
2383
Bram Moolenaar30613902019-12-01 22:11:18 +01002384 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002385 windgoto(msg_row, msg_col);
2386
2387 ++no_mapping;
2388 ++allow_keys;
2389 for (;;)
2390 {
2391 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002392 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002393 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002394 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002395 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002396 n = vpeekc_any();
2397 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002398 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002399 n = 0;
2400 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002401 // getchar(0) and char avail() != NUL: get a character.
2402 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2403 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002404
Bram Moolenaar15183b42020-09-05 19:59:39 +02002405 if (n == K_IGNORE || n == K_MOUSEMOVE
2406 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002407 continue;
2408 break;
2409 }
2410 --no_mapping;
2411 --allow_keys;
2412
2413 set_vim_var_nr(VV_MOUSE_WIN, 0);
2414 set_vim_var_nr(VV_MOUSE_WINID, 0);
2415 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2416 set_vim_var_nr(VV_MOUSE_COL, 0);
2417
2418 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002419 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002420 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002421 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002422 int i = 0;
2423
Bram Moolenaar30613902019-12-01 22:11:18 +01002424 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002425 if (mod_mask != 0)
2426 {
2427 temp[i++] = K_SPECIAL;
2428 temp[i++] = KS_MODIFIER;
2429 temp[i++] = mod_mask;
2430 }
2431 if (IS_SPECIAL(n))
2432 {
2433 temp[i++] = K_SPECIAL;
2434 temp[i++] = K_SECOND(n);
2435 temp[i++] = K_THIRD(n);
2436 }
2437 else if (has_mbyte)
2438 i += (*mb_char2bytes)(n, temp + i);
2439 else
2440 temp[i++] = n;
2441 temp[i++] = NUL;
2442 rettv->v_type = VAR_STRING;
2443 rettv->vval.v_string = vim_strsave(temp);
2444
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002445 if (is_mouse_key(n))
2446 {
2447 int row = mouse_row;
2448 int col = mouse_col;
2449 win_T *win;
2450 linenr_T lnum;
2451 win_T *wp;
2452 int winnr = 1;
2453
2454 if (row >= 0 && col >= 0)
2455 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002456 // Find the window at the mouse coordinates and compute the
2457 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002458 win = mouse_find_win(&row, &col, FIND_POPUP);
2459 if (win == NULL)
2460 return;
2461 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002462#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002463 if (WIN_IS_POPUP(win))
2464 winnr = 0;
2465 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002466#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002467 for (wp = firstwin; wp != win && wp != NULL;
2468 wp = wp->w_next)
2469 ++winnr;
2470 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2471 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2472 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2473 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2474 }
2475 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002476 }
2477}
2478
2479/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002480 * "getchar()" function
2481 */
2482 void
2483f_getchar(typval_T *argvars, typval_T *rettv)
2484{
2485 getchar_common(argvars, rettv);
2486}
2487
2488/*
2489 * "getcharstr()" function
2490 */
2491 void
2492f_getcharstr(typval_T *argvars, typval_T *rettv)
2493{
2494 getchar_common(argvars, rettv);
2495
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002496 if (rettv->v_type != VAR_NUMBER)
2497 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002498
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002499 char_u temp[7]; // mbyte-char: 6, NUL: 1
2500 varnumber_T n = rettv->vval.v_number;
2501 int i = 0;
2502
2503 if (n != 0)
2504 {
2505 if (has_mbyte)
2506 i += (*mb_char2bytes)(n, temp + i);
2507 else
2508 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002509 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002510 temp[i++] = NUL;
2511 rettv->v_type = VAR_STRING;
2512 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002513}
2514
2515/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002516 * "getcharmod()" function
2517 */
2518 void
2519f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2520{
2521 rettv->vval.v_number = mod_mask;
2522}
2523#endif // FEAT_EVAL
2524
2525#if defined(MESSAGE_QUEUE) || defined(PROTO)
2526# define MAX_REPEAT_PARSE 8
2527
2528/*
2529 * Process messages that have been queued for netbeans or clientserver.
2530 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002531 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002532 * it is safe to do so.
2533 */
2534 void
2535parse_queued_messages(void)
2536{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002537 int old_curwin_id;
2538 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002539 int i;
2540 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002541 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002542 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002543
2544 // Do not handle messages while redrawing, because it may cause buffers to
2545 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002546 // Also bail out when parsing messages was explicitly disabled.
2547 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002548 return;
2549
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002550 // If memory allocation fails during startup we'll exit but curbuf or
2551 // curwin could be NULL.
2552 if (curbuf == NULL || curwin == NULL)
2553 return;
2554
2555 old_curbuf_fnum = curbuf->b_fnum;
2556 old_curwin_id = curwin->w_id;
2557
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002558 ++entered;
2559
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002560 // may_garbage_collect is set in main_loop() to do garbage collection when
2561 // blocking to wait on a character. We don't want that while parsing
2562 // messages, a callback may invoke vgetc() while lists and dicts are in use
2563 // in the call stack.
2564 may_garbage_collect = FALSE;
2565
2566 // Loop when a job ended, but don't keep looping forever.
2567 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2568 {
2569 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002570# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002571 channel_handle_events(FALSE);
2572# endif
2573
2574# ifdef FEAT_NETBEANS_INTG
2575 // Process the queued netbeans messages.
2576 netbeans_parse_messages();
2577# endif
2578# ifdef FEAT_JOB_CHANNEL
2579 // Write any buffer lines still to be written.
2580 channel_write_any_lines();
2581
2582 // Process the messages queued on channels.
2583 channel_parse_messages();
2584# endif
2585# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2586 // Process the queued clientserver messages.
2587 server_parse_messages();
2588# endif
2589# ifdef FEAT_JOB_CHANNEL
2590 // Check if any jobs have ended. If so, repeat the above to handle
2591 // changes, e.g. stdin may have been closed.
2592 if (job_check_ended())
2593 continue;
2594# endif
2595# ifdef FEAT_TERMINAL
2596 free_unused_terminals();
2597# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002598
2599# ifdef FEAT_SOUND_MACOSX
2600 process_cfrunloop();
2601# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002602# ifdef FEAT_SOUND_CANBERRA
2603 if (has_sound_callback_in_queue())
2604 invoke_sound_callback();
2605# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002606#ifdef SIGUSR1
2607 if (got_sigusr1)
2608 {
2609 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2610 got_sigusr1 = FALSE;
2611 }
2612#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002613 break;
2614 }
2615
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002616 // When not nested we'll go back to waiting for a typed character. If it
2617 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002618 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002619 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002620
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002621 may_garbage_collect = save_may_garbage_collect;
2622
2623 // If the current window or buffer changed we need to bail out of the
2624 // waiting loop. E.g. when a job exit callback closes the terminal window.
2625 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002626 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002627
2628 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002629}
2630#endif
2631
2632
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002633typedef enum {
2634 map_result_fail, // failed, break loop
2635 map_result_get, // get a character from typeahead
2636 map_result_retry, // try to map again
2637 map_result_nomatch // no matching mapping, get char
2638} map_result_T;
2639
2640/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002641 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002642 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002643 */
2644 static int
zeertzjqee446032022-04-30 15:02:22 +01002645at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002646{
2647 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2648 int c = *p;
2649
2650 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002651 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002652 && p[1] == KS_MODIFIER
2653 && (p[2] & MOD_MASK_CTRL))
2654 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002655 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2656 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002657}
2658
2659/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002660 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002661 * into just a key, apply that.
2662 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2663 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002664 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002665 */
2666 static int
2667check_simplify_modifier(int max_offset)
2668{
2669 int offset;
2670 char_u *tp;
2671
2672 for (offset = 0; offset < max_offset; ++offset)
2673 {
2674 if (offset + 3 >= typebuf.tb_len)
2675 break;
2676 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002677 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002678 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002679 // A modifier was not used for a mapping, apply it to ASCII keys.
2680 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002681 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002682 int c = tp[3];
2683 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002684
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002685 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002686 {
2687 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002688 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002689
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002690 if (offset == 0)
2691 {
2692 // At the start: remember the character and mod_mask before
2693 // merging, in some cases, e.g. at the hit-return prompt,
2694 // they are put back in the typeahead buffer.
2695 vgetc_char = c;
2696 vgetc_mod_mask = tp[2];
2697 }
zeertzjq12e21e32022-04-27 11:58:01 +01002698 if (IS_SPECIAL(new_c))
2699 {
2700 new_string[0] = K_SPECIAL;
2701 new_string[1] = K_SECOND(new_c);
2702 new_string[2] = K_THIRD(new_c);
2703 len = 3;
2704 }
2705 else
2706 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002707 if (modifier == 0)
2708 {
2709 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002710 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002711 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002712 }
2713 else
2714 {
2715 tp[2] = modifier;
2716 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002717 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002718 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002719 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002720 return len;
2721 }
2722 }
2723 }
2724 return 0;
2725}
2726
2727/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002728 * Return TRUE if the terminal sends modifiers with various keys. This is when
2729 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2730 * enabled.
2731 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002732 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002733key_protocol_enabled(void)
2734{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002735 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2736 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2737 ? modify_otherkeys_state == MOKS_ENABLED
2738 : seenModifyOtherKeys;
2739 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002740}
2741
2742/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 * Handle mappings in the typeahead buffer.
2744 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002745 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002746 * - When there is no match yet, return map_result_nomatch, need to get more
2747 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002748 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002749 */
2750 static int
2751handle_mapping(
2752 int *keylenp,
2753 int *timedout,
2754 int *mapdepth)
2755{
2756 mapblock_T *mp = NULL;
2757 mapblock_T *mp2;
2758 mapblock_T *mp_match;
2759 int mp_match_len = 0;
2760 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002761 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002762 int tb_c1;
2763 int mlen;
2764#ifdef FEAT_LANGMAP
2765 int nolmaplen;
2766#endif
2767 int keylen = *keylenp;
2768 int i;
2769 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002770 int is_plug_map = FALSE;
2771
zeertzjqbb404f52022-07-23 06:25:29 +01002772 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002773 if (typebuf.tb_len >= 3
2774 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002775 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2776 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2777 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002778
2779 /*
2780 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002781 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002782 *
2783 * Don't look for mappings if:
2784 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2785 * - maphash_valid not set: no mappings present.
2786 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2787 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002788 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002789 * - waiting for a char with --more--
2790 * - in Ctrl-X mode, and we get a valid char for that mode
2791 */
2792 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2793 if (no_mapping == 0 && is_maphash_valid()
2794 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002795 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002796 || (p_remap
2797 && (typebuf.tb_noremap[typebuf.tb_off]
2798 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002799 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2800 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2801 && State != MODE_ASKMORE
2802 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002803 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002804 {
Christopher Plewright03193062022-11-22 12:58:27 +00002805#ifdef FEAT_GUI
2806 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2807 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002808 {
2809 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2810 // K_SPECIAL KS_MODIFIER {flags}.
2811 tb_c1 = K_SPECIAL;
2812 }
2813#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002814#ifdef FEAT_LANGMAP
2815 if (tb_c1 == K_SPECIAL)
2816 nolmaplen = 2;
2817 else
2818 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002819 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2820 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002821 nolmaplen = 0;
2822 }
2823#endif
2824 // First try buffer-local mappings.
2825 mp = get_buf_maphash_list(local_State, tb_c1);
2826 mp2 = get_maphash_list(local_State, tb_c1);
2827 if (mp == NULL)
2828 {
2829 // There are no buffer-local mappings.
2830 mp = mp2;
2831 mp2 = NULL;
2832 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002833
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002834 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002835 * Loop until a partly matching mapping is found or all (local)
2836 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002837 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002838 * A full match is only accepted if there is no partly match, so "aa"
2839 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002840 */
2841 mp_match = NULL;
2842 mp_match_len = 0;
2843 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002844 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002845 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002846 // Only consider an entry if the first character matches and it is
2847 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002848 // Skip ":lmap" mappings if keys were mapped.
2849 if (mp->m_keys[0] == tb_c1
2850 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002851 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002852 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002853 && ((mp->m_mode & MODE_LANGMAP) == 0
2854 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855 {
2856#ifdef FEAT_LANGMAP
2857 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002858 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859#endif
2860 // find the match length of this mapping
2861 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2862 {
zeertzjq49660f52022-10-20 17:59:38 +01002863 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002864#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002866 {
2867 if (nomap == 2 && c2 == KS_MODIFIER)
2868 modifiers = 1;
2869 else if (nomap == 1 && modifiers == 1)
2870 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002871 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002872 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002873 else
zeertzjq49660f52022-10-20 17:59:38 +01002874 {
2875 if (c2 == K_SPECIAL)
2876 nomap = 2;
2877 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2878 // Only apply 'langmap' if merging modifiers into
2879 // the key will not result in another character,
2880 // so that 'langmap' behaves consistently in
2881 // different terminals and GUIs.
2882 LANGMAP_ADJUST(c2, TRUE);
2883 modifiers = 0;
2884 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002885#endif
zeertzjq49660f52022-10-20 17:59:38 +01002886 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002887 break;
2888 }
2889
Bram Moolenaareda35f72019-08-03 14:59:44 +02002890 // Don't allow mapping the first byte(s) of a multi-byte char.
2891 // Happens when mapping <M-a> and then changing 'encoding'.
2892 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002893 {
2894 char_u *p1 = mp->m_keys;
2895 char_u *p2 = mb_unescape(&p1);
2896
2897 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002898 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002899 mlen = 0;
2900 }
2901
2902 // Check an entry whether it matches.
2903 // - Full match: mlen == keylen
2904 // - Partly match: mlen == typebuf.tb_len
2905 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002906 if (mlen == keylen || (mlen == typebuf.tb_len
2907 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002908 {
2909 char_u *s;
2910 int n;
2911
Bram Moolenaareda35f72019-08-03 14:59:44 +02002912 // If only script-local mappings are allowed, check if the
2913 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002914 s = typebuf.tb_noremap + typebuf.tb_off;
2915 if (*s == RM_SCRIPT
2916 && (mp->m_keys[0] != K_SPECIAL
2917 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002918 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002919 continue;
2920
Bram Moolenaareda35f72019-08-03 14:59:44 +02002921 // If one of the typed keys cannot be remapped, skip the
2922 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002923 for (n = mlen; --n >= 0; )
2924 if (*s++ & (RM_NONE|RM_ABBR))
2925 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002926 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002927 continue;
2928
2929 if (keylen > typebuf.tb_len)
2930 {
2931 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002932 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002933 {
2934 // break at a partly match
2935 keylen = KEYLEN_PART_MAP;
2936 break;
2937 }
2938 }
2939 else if (keylen > mp_match_len)
2940 {
2941 // found a longer match
2942 mp_match = mp;
2943 mp_match_len = keylen;
2944 }
2945 }
2946 else
Oleg Goncharov56904f92024-07-23 20:34:15 +02002947 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002948 // No match; may have to check for termcode at next
Oleg Goncharov56904f92024-07-23 20:34:15 +02002949 // character.
2950
2951 // If the first character that didn't match is
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002952 // K_SPECIAL then check for a termcode. This isn't perfect
2953 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002954 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002955 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002956 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002957 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2958 }
2959 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2960 want_termcode = 1;
Oleg Goncharov56904f92024-07-23 20:34:15 +02002961
2962 // Check termcode for uppercase character to properly
2963 // process "ESC[27;2;<ascii code>~" control sequences.
2964 if (ASCII_ISUPPER(mp->m_keys[mlen]))
2965 want_termcode = 1;
2966 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002967 }
2968 }
2969
Bram Moolenaareda35f72019-08-03 14:59:44 +02002970 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002971 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002972 {
2973 mp = mp_match;
2974 keylen = mp_match_len;
2975 }
2976 }
2977
2978 /*
2979 * Check for match with 'pastetoggle'
2980 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002981 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002982 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002983 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2984 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002985 break;
2986 if (p_pt[mlen] == NUL) // match
2987 {
2988 // write chars to script file(s)
2989 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002990 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2991 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002992
2993 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002994 set_option_value_give_err((char_u *)"paste",
2995 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002996 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002997 {
2998 msg_col = 0;
2999 msg_row = Rows - 1;
3000 msg_clr_eos(); // clear ruler
3001 }
3002 status_redraw_all();
3003 redraw_statuslines();
3004 showmode();
3005 setcursor();
3006 *keylenp = keylen;
3007 return map_result_retry;
3008 }
3009 // Need more chars for partly match.
3010 if (mlen == typebuf.tb_len)
3011 keylen = KEYLEN_PART_KEY;
3012 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003013 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003014 max_mlen = mlen + 1;
3015 }
3016
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003017 // May check for a terminal code when there is no mapping or only a partial
3018 // mapping. Also check if there is a full mapping with <Esc>, unless timed
3019 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00003020 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00003021 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
3022 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003023 {
3024 int save_keylen = keylen;
3025
3026 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003027 * When no matching mapping found or found a non-matching mapping that
3028 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003029 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02003030 * - mapping is allowed,
3031 * - keys have not been mapped,
3032 * - and not an ESC sequence, not in insert mode or p_ek is on,
3033 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003034 */
zeertzjq68a573c2022-04-28 14:10:01 +01003035 if (no_mapping == 0 || allow_keys != 0)
3036 {
3037 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01003038 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003039 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01003040 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01003041 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
3042 else
3043 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003044
Bram Moolenaar0684e362020-12-03 19:54:42 +01003045 // If no termcode matched but 'pastetoggle' matched partially
3046 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01003047 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003048 keylen = KEYLEN_PART_KEY;
3049
Bram Moolenaar975a8802020-06-06 22:36:24 +02003050 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00003051 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003052#ifdef FEAT_TERMINAL
3053 check_no_reduce_keys(); // may update the no_reduce_keys flag
3054#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02003055 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01003056 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02003057 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01003058 if (keylen < 0)
3059 // ins_typebuf() failed
3060 return map_result_fail;
3061 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02003062
Bram Moolenaareda35f72019-08-03 14:59:44 +02003063 // When getting a partial match, but the last characters were not
3064 // typed, don't wait for a typed character to complete the
3065 // termcode. This helps a lot when a ":normal" command ends in an
3066 // ESC.
3067 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003068 keylen = 0;
3069 }
3070 else
3071 keylen = 0;
3072 if (keylen == 0) // no matching terminal code
3073 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003074#ifdef AMIGA
3075 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003076 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02003077 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003078 {
3079 char_u *s;
3080
3081 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003082 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
3083 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003084 ++s)
3085 ;
3086 if (*s == 'r' || *s == '|') // found one
3087 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003088 del_typebuf(
3089 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003090 // get size and redraw screen
3091 shell_resized();
3092 *keylenp = keylen;
3093 return map_result_retry;
3094 }
3095 if (*s == NUL) // need more characters
3096 keylen = KEYLEN_PART_KEY;
3097 }
3098 if (keylen >= 0)
3099#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02003100 // When there was a matching mapping and no termcode could be
3101 // replaced after another one, use that mapping (loop around).
3102 // If there was no mapping at all use the character from the
3103 // typeahead buffer right here.
3104 if (mp == NULL)
3105 {
3106 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003107 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003108 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003109 }
3110
3111 if (keylen > 0) // full matching terminal code
3112 {
3113#if defined(FEAT_GUI) && defined(FEAT_MENU)
3114 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003115 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3116 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003117 {
3118 int idx;
3119
Bram Moolenaareda35f72019-08-03 14:59:44 +02003120 // Using a menu may cause a break in undo! It's like using
3121 // gotchars(), but without recording or writing to a script
3122 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003123 may_sync_undo();
3124 del_typebuf(3, 0);
3125 idx = get_menu_index(current_menu, local_State);
3126 if (idx != MENU_INDEX_INVALID)
3127 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003128 // In Select mode and a Visual mode menu is used: Switch
3129 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003130 // back to Select mode.
3131 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003132 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003133 {
3134 VIsual_select = FALSE;
3135 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003136 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003137 }
3138 ins_typebuf(current_menu->strings[idx],
3139 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003140 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003141 }
3142 }
3143#endif // FEAT_GUI && FEAT_MENU
3144 *keylenp = keylen;
3145 return map_result_retry; // try mapping again
3146 }
3147
Bram Moolenaareda35f72019-08-03 14:59:44 +02003148 // Partial match: get some more characters. When a matching mapping
3149 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003150 if (mp == NULL || keylen < 0)
3151 keylen = KEYLEN_PART_KEY;
3152 else
3153 keylen = mp_match_len;
3154 }
3155
3156 /*
3157 * complete match
3158 */
3159 if (keylen >= 0 && keylen <= typebuf.tb_len)
3160 {
3161 char_u *map_str;
3162
3163#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003164 int save_m_expr;
3165 int save_m_noremap;
3166 int save_m_silent;
3167 char_u *save_m_keys;
zeertzjq9d997ad2024-07-29 21:10:07 +02003168 char_u *save_alt_m_keys;
zeertzjq74011dc2024-07-30 19:17:56 +02003169 int save_alt_m_keylen;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003170#else
3171# define save_m_noremap mp->m_noremap
3172# define save_m_silent mp->m_silent
3173#endif
3174
3175 // write chars to script file(s)
3176 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003177 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3178 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003179
3180 cmd_silent = (typebuf.tb_silent > 0);
3181 del_typebuf(keylen, 0); // remove the mapped keys
3182
3183 /*
3184 * Put the replacement string in front of mapstr.
3185 * The depth check catches ":map x y" and ":map y x".
3186 */
3187 if (++*mapdepth >= p_mmd)
3188 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003189 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003190 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003191 redrawcmdline();
3192 else
3193 setcursor();
3194 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003195 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003196 *keylenp = keylen;
3197 return map_result_fail;
3198 }
3199
3200 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003201 * In Select mode and a Visual mode mapping is used: Switch to Visual
3202 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003203 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003204 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003205 {
3206 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003207 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003208 }
3209
3210#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003211 // Copy the values from *mp that are used, because evaluating the
3212 // expression may invoke a function that redefines the mapping, thereby
3213 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003214 save_m_expr = mp->m_expr;
3215 save_m_noremap = mp->m_noremap;
3216 save_m_silent = mp->m_silent;
3217 save_m_keys = NULL; // only saved when needed
zeertzjq9d997ad2024-07-29 21:10:07 +02003218 save_alt_m_keys = NULL; // only saved when needed
zeertzjq74011dc2024-07-30 19:17:56 +02003219 save_alt_m_keylen = mp->m_alt != NULL ? mp->m_alt->m_keylen : 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003220
3221 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003222 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3223 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003224 */
3225 if (mp->m_expr)
3226 {
3227 int save_vgetc_busy = vgetc_busy;
3228 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003229 int was_screen_col = screen_cur_col;
3230 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003231 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003232
3233 vgetc_busy = 0;
3234 may_garbage_collect = FALSE;
3235
zeertzjq74011dc2024-07-30 19:17:56 +02003236 save_m_keys = vim_strnsave(mp->m_keys, (size_t)mp->m_keylen);
zeertzjq9d997ad2024-07-29 21:10:07 +02003237 save_alt_m_keys = mp->m_alt != NULL
zeertzjq74011dc2024-07-30 19:17:56 +02003238 ? vim_strnsave(mp->m_alt->m_keys,
3239 (size_t)save_alt_m_keylen) : NULL;
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003240 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003241
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003242 // The mapping may do anything, but we expect it to take care of
3243 // redrawing. Do put the cursor back where it was.
3244 windgoto(was_screen_row, was_screen_col);
3245 out_flush();
3246
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003247 // If an error was displayed and the expression returns an empty
3248 // string, generate a <Nop> to allow for a redraw.
3249 if (prev_did_emsg != did_emsg
3250 && (map_str == NULL || *map_str == NUL))
3251 {
3252 char_u buf[4];
3253
3254 vim_free(map_str);
3255 buf[0] = K_SPECIAL;
3256 buf[1] = KS_EXTRA;
3257 buf[2] = KE_IGNORE;
3258 buf[3] = NUL;
3259 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01003260 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003261 {
3262 // redraw the command below the error
3263 msg_didout = TRUE;
3264 if (msg_row < cmdline_row)
3265 msg_row = cmdline_row;
3266 redrawcmd();
3267 }
3268 }
3269
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003270 vgetc_busy = save_vgetc_busy;
3271 may_garbage_collect = save_may_garbage_collect;
3272 }
3273 else
3274#endif
3275 map_str = mp->m_str;
3276
3277 /*
3278 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003279 * If 'from' field is the same as the start of the 'to' field, don't
3280 * remap the first character (but do allow abbreviations).
3281 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003282 */
3283 if (map_str == NULL)
3284 i = FAIL;
3285 else
3286 {
3287 int noremap;
3288
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003289#ifdef FEAT_EVAL
3290 last_used_map = mp;
3291 last_used_sid = -1;
3292#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003293 if (save_m_noremap != REMAP_YES)
3294 noremap = save_m_noremap;
3295 else if (
3296#ifdef FEAT_EVAL
zeertzjq9d997ad2024-07-29 21:10:07 +02003297 save_m_expr ?
3298 (save_m_keys != NULL
3299 && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
3300 || (save_alt_m_keys != NULL
3301 && STRNCMP(map_str, save_alt_m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003302 (size_t)save_alt_m_keylen) == 0) :
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003303#endif
zeertzjq9d997ad2024-07-29 21:10:07 +02003304 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
3305 || (mp->m_alt != NULL
3306 && STRNCMP(map_str, mp->m_alt->m_keys,
zeertzjq74011dc2024-07-30 19:17:56 +02003307 (size_t)mp->m_alt->m_keylen) == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003308 noremap = REMAP_SKIP;
zeertzjq9d997ad2024-07-29 21:10:07 +02003309 else
3310 noremap = REMAP_YES;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003311 i = ins_typebuf(map_str, noremap,
3312 0, TRUE, cmd_silent || save_m_silent);
3313#ifdef FEAT_EVAL
3314 if (save_m_expr)
3315 vim_free(map_str);
3316#endif
3317 }
3318#ifdef FEAT_EVAL
3319 vim_free(save_m_keys);
zeertzjq9d997ad2024-07-29 21:10:07 +02003320 vim_free(save_alt_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003321#endif
3322 *keylenp = keylen;
3323 if (i == FAIL)
3324 return map_result_fail;
3325 return map_result_retry;
3326 }
3327
3328 *keylenp = keylen;
3329 return map_result_nomatch;
3330}
3331
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003332/*
3333 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003334 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003335 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003338vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 old_char = c;
3341 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003342 old_mouse_row = mouse_row;
3343 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003344 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003348 * When peeking and not getting a character, reg_executing cannot be cleared
3349 * yet, so set a flag to clear it later.
3350 */
3351 static void
3352check_end_reg_executing(int advance)
3353{
3354 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3355 || pending_end_reg_executing))
3356 {
3357 if (advance)
3358 {
3359 reg_executing = 0;
3360 pending_end_reg_executing = FALSE;
3361 }
3362 else
3363 pending_end_reg_executing = TRUE;
3364 }
3365
3366}
3367
3368/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003369 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 * 1. from the stuffbuffer
3371 * This is used for abbreviated commands like "D" -> "d$".
3372 * Also used to redo a command for ".".
3373 * 2. from the typeahead buffer
3374 * Stores text obtained previously but not used yet.
3375 * Also stores the result of mappings.
3376 * Also used for the ":normal" command.
3377 * 3. from the user
3378 * This may do a blocking wait if "advance" is TRUE.
3379 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003380 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003381 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 * KeyTyped is set to TRUE in the case the user typed the key.
3383 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003384 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003385 * Just look whether there is a character available.
3386 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 *
3388 * When "no_mapping" is zero, checks for mappings in the current mode.
3389 * Only returns one byte (of a multi-byte character).
3390 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3391 */
3392 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003393vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003395 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003396 int timedout = FALSE; // waited for more than 'timeoutlen'
3397 // for mapping to complete or
3398 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003399 int mapdepth = 0; // check for recursive mapping
3400 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003403 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif
3405 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003407 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
3409 /*
3410 * This function doesn't work very well when called recursively. This may
3411 * happen though, because of:
3412 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3413 * there is a character available, which calls this function. In that
3414 * case we must return NUL, to indicate no character is available.
3415 * 2. A GUI callback function writes to the screen, causing a
3416 * wait_return().
3417 * Using ":normal" can also do this, but it saves the typeahead buffer,
3418 * thus it should be OK. But don't get a key from the user then.
3419 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003420 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 return NUL;
3422
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003423 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003426 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003428 typebuf_was_empty = FALSE;
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 init_typebuf();
3432 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003433 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 do
3435 {
3436/*
3437 * get a character: 1. from the stuffbuffer
3438 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003439 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441 c = typeahead_char;
3442 if (advance)
3443 typeahead_char = 0;
3444 }
3445 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003446 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (c != NUL && !got_int)
3448 {
3449 if (advance)
3450 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003451 // KeyTyped = FALSE; When the command that stuffed something
3452 // was typed, behave like the stuffed command was typed.
3453 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 KeyStuffed = TRUE;
3455 }
3456 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003457 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 else
3460 {
3461 /*
3462 * Loop until we either find a matching mapped key, or we
3463 * are sure that it is not a mapped key.
3464 * If a mapped key sequence is found we go back to the start to
3465 * try re-mapping.
3466 */
3467 for (;;)
3468 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003469 long wait_time;
3470 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003471 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003472 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 /*
3474 * ui_breakcheck() is slow, don't use it too often when
3475 * inside a mapping. But call it each time for typed
3476 * characters.
3477 */
3478 if (typebuf.tb_maplen)
3479 line_breakcheck();
3480 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003481 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (got_int)
3483 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003484 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003485 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 /*
3488 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003489 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * Otherwise we behave like having gotten a CTRL-C.
3491 * As a result typing CTRL-C in insert mode will
3492 * really insert a CTRL-C.
3493 */
3494 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003495 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 c = ESC;
3497 else
3498 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003499 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003501 if (advance)
3502 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003503 // Also record this character, it might be needed to
3504 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003505 *typebuf.tb_buf = c;
3506 gotchars(typebuf.tb_buf, 1);
3507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 cmd_silent = FALSE;
3509
3510 break;
3511 }
3512 else if (typebuf.tb_len > 0)
3513 {
3514 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003515 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003517 map_result_T result = handle_mapping(
3518 &keylen, &timedout, &mapdepth);
3519
3520 if (result == map_result_retry)
3521 // try mapping again
3522 continue;
3523 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003524 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003525 // failed, use the outer loop
3526 c = -1;
3527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003529 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531/*
3532 * get a character: 2. from the typeahead buffer
3533 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003534 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003535 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003536 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003537 cmd_silent = (typebuf.tb_silent > 0);
3538 if (typebuf.tb_maplen > 0)
3539 KeyTyped = FALSE;
3540 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003541 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003542 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003543 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003544 gotchars(typebuf.tb_buf
3545 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003546 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003547 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003548 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003549 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003550 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003553 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555
3556/*
3557 * get a character: 3. from the user - handle <Esc> in Insert mode
3558 */
3559 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003560 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003562 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 * typing an <ESC>. If we get something after all, we may
3564 * have to redisplay the mode. That the cursor is in the wrong
3565 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003566 * Do not do this if the kitty keyboard protocol is used, every
3567 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 */
3569 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 new_wcol = curwin->w_wcol;
3571 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if ( advance
3573 && typebuf.tb_len == 1
3574 && typebuf.tb_buf[typebuf.tb_off] == ESC
3575 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003576 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003579 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003580 && (p_timeout
3581 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003583 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003585 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *ptr;
3587
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003588 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 unshowmode(TRUE);
3591 mode_deleted = TRUE;
3592 }
3593#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003594 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003595 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 int save_State;
3598
3599 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003600 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 gui_update_cursor(TRUE, FALSE);
3602 State = save_State;
3603 shape_changed = TRUE;
3604 }
3605#endif
3606 validate_cursor();
3607 old_wcol = curwin->w_wcol;
3608 old_wrow = curwin->w_wrow;
3609
Bram Moolenaar30613902019-12-01 22:11:18 +01003610 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (curwin->w_cursor.col != 0)
3612 {
3613 if (curwin->w_wcol > 0)
3614 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003615 // After auto-indenting and no text is following,
3616 // we are expecting to truncate the trailing
3617 // white-space, so find the last non-white
3618 // character -- webb
3619 if (did_ai && *skipwhite(ml_get_curline()
3620 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003622 chartabsize_T cts;
3623
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003624 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003626 init_chartabsize_arg(&cts, curwin,
3627 curwin->w_cursor.lnum, 0, ptr, ptr);
3628 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003630 if (!VIM_ISWHITE(*cts.cts_ptr))
3631 curwin->w_wcol = cts.cts_vcol;
3632 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003634 cts.cts_ptr +=
3635 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003637 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003639 clear_chartabsize_arg(&cts);
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003642 + curwin->w_wcol / curwin->w_width;
3643 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003645 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
3647 else
3648 {
3649 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652 }
3653 else if (curwin->w_p_wrap && curwin->w_wrow)
3654 {
3655 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003656 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3660 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003661 // Correct when the cursor is on the right halve
3662 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 ptr = ml_get_curline();
3664 col -= (*mb_head_off)(ptr, ptr + col);
3665 if ((*mb_ptr2cells)(ptr + col) > 1)
3666 --curwin->w_wcol;
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669 setcursor();
3670 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 new_wcol = curwin->w_wcol;
3672 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 curwin->w_wcol = old_wcol;
3674 curwin->w_wrow = old_wrow;
3675 }
3676 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003677 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003678
Bram Moolenaar30613902019-12-01 22:11:18 +01003679 // Allow mapping for just typed characters. When we get here c
3680 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003681 for (n = 1; n <= c; ++n)
3682 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 typebuf.tb_len += c;
3684
Bram Moolenaar30613902019-12-01 22:11:18 +01003685 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3687 {
3688 timedout = TRUE;
3689 continue;
3690 }
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (ex_normal_busy > 0)
3693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
Bram Moolenaar30613902019-12-01 22:11:18 +01003696 // No typeahead left and inside ":normal". Must return
3697 // something to avoid getting stuck. When an incomplete
3698 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (typebuf.tb_len > 0)
3700 {
3701 timedout = TRUE;
3702 continue;
3703 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003704
Bram Moolenaar30613902019-12-01 22:11:18 +01003705 // When 'insertmode' is set, ESC just beeps in Insert
3706 // mode. Use CTRL-L to make edit() return.
3707 // For the command line only CTRL-C always breaks it.
3708 // For the cmdline window: Alternate between ESC and
3709 // CTRL-C: ESC for most situations and CTRL-C to close the
3710 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003711 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003713#ifdef FEAT_TERMINAL
3714 else if (terminal_is_active())
3715 c = K_CANCEL;
3716#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003717 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003718 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 c = Ctrl_C;
3720 else
3721 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003723 // set a flag to indicate this wasn't a normal char
3724 if (advance)
3725 typebuf_was_empty = TRUE;
3726
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003727 // return from main_loop()
3728 if (pending_exmode_active)
3729 exmode_active = EXMODE_NORMAL;
3730
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003731 // no chars to block abbreviation for
3732 typebuf.tb_no_abbr_cnt = 0;
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 break;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737/*
3738 * get a character: 3. from the user - update display
3739 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003740 // In insert mode a screen update is skipped when characters
3741 // are still available. But when those available characters
3742 // are part of a mapping, and we are going to do a blocking
3743 // wait here. Need to update the screen to display the
3744 // changed text so far. Also for when 'lazyredraw' is set and
3745 // redrawing was postponed because there was something in the
3746 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003747 if (((State & MODE_INSERT) != 0 || p_lz)
3748 && (State & MODE_CMDLINE) == 0
3749 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003752 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754
3755 /*
3756 * If we have a partial match (and are going to wait for more
3757 * input from the user), show the partially matched characters
3758 * to the user with showcmd.
3759 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003760 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003761 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (typebuf.tb_len > 0 && advance && !exmode_active)
3763 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003764 if (((State & (MODE_NORMAL | MODE_INSERT))
3765 || State == MODE_LANGMAP)
3766 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003768 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003769 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3771 + typebuf.tb_len - 1) == 1)
3772 {
3773 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3774 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003775 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003776 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003778 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 old_wcol = curwin->w_wcol;
3780 old_wrow = curwin->w_wrow;
3781 curwin->w_wcol = new_wcol;
3782 curwin->w_wrow = new_wrow;
3783 push_showcmd();
3784 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003785 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3786 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003787 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003788 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 curwin->w_wcol = old_wcol;
3790 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003793 // This looks nice when typing a dead character map.
3794 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003795 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003796 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3798 && cmdline_star == 0
3799#endif
3800 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3801 + typebuf.tb_len - 1) == 1)
3802 {
3803 putcmdline(typebuf.tb_buf[typebuf.tb_off
3804 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003805 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807 }
3808
3809/*
3810 * get a character: 3. from the user - get it
3811 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003812 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003813 // timedout may have been set if a mapping with empty RHS
3814 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003815 timedout = FALSE;
3816
Bram Moolenaar652de232019-04-04 20:13:09 +02003817 if (advance)
3818 {
3819 if (typebuf.tb_len == 0
3820 || !(p_timeout
3821 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3822 // blocking wait
3823 wait_time = -1L;
3824 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3825 wait_time = p_ttm;
3826 else
3827 wait_time = p_tm;
3828 }
3829 else
3830 wait_time = 0;
3831
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003832 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3834 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003835 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
Bram Moolenaareda35f72019-08-03 14:59:44 +02003837 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003839 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003841 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003843 if ((State & MODE_CMDLINE)
3844 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003846 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003847 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849
3850 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003851 continue; // end of input script reached
3852 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 if (!advance)
3855 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003856 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
3858 timedout = TRUE;
3859 continue;
3860 }
3861 }
3862 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003863 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 while (typebuf.tb_buf[typebuf.tb_off
3865 + typebuf.tb_len] != NUL)
3866 typebuf.tb_noremap[typebuf.tb_off
3867 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003868#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003869 // Get IM status right after getting keys, not after the
3870 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 vgetc_im_active = im_get_status();
3872#endif
3873 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003874 } // for (;;)
3875 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaar30613902019-12-01 22:11:18 +01003877 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003878 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 /*
3881 * The "INSERT" message is taken care of here:
3882 * if we return an ESC to exit insert mode, the message is deleted
3883 * if we don't return an ESC but deleted the message before, redisplay it
3884 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003885 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003887 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
3889 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003890 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else
3892 unshowmode(FALSE);
3893 }
3894 else if (c != ESC && mode_deleted)
3895 {
3896 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003897 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
3899 showmode();
3900 }
3901 }
3902#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003903 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003904 if (gui.in_use && shape_changed)
3905 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003907 if (timedout && c == ESC)
3908 {
zeertzjqbf321802024-01-28 19:03:00 +01003909 // When recording there will be no timeout. Add an <Ignore> after the
3910 // ESC to avoid that it forms a key code with following characters.
3911 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003914 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 return c;
3917}
3918
3919/*
3920 * inchar() - get one character from
3921 * 1. a scriptfile
3922 * 2. the keyboard
3923 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003924 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 * NUL terminated (buffer length must be 'maxlen' + 1).
3926 * Minimum for "maxlen" is 3!!!!
3927 *
3928 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3929 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3930 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3931 * otherwise.
3932 *
3933 * If we got an interrupt all input is read until none is available.
3934 *
3935 * If wait_time == 0 there is no waiting for the char.
3936 * If wait_time == n we wait for n msec for a character to arrive.
3937 * If wait_time == -1 we wait forever for a character to arrive.
3938 *
3939 * Return the number of obtained characters.
3940 * Return -1 when end of input script reached.
3941 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003942 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003943inchar(
3944 char_u *buf,
3945 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003946 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaar30613902019-12-01 22:11:18 +01003948 int len = 0; // init for GCC
3949 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003951 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar30613902019-12-01 22:11:18 +01003953 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
3955 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003956 out_flush_cursor(FALSE, FALSE);
3957#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3958 if (gui.in_use && postponed_mouseshape)
3959 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#endif
3961 }
3962
3963 /*
3964 * Don't reset these when at the hit-return prompt, otherwise a endless
3965 * recursive loop may result (write error in swapfile, hit-return, timeout
3966 * on char wait, flush swapfile, write error....).
3967 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003968 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003970 did_outofmem_msg = FALSE; // display out of memory message (again)
3971 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003973 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003976 * Get a character from a script file if there is one.
3977 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 */
3979 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003980 while (scriptin[curscript] != NULL && script_char < 0
3981#ifdef FEAT_EVAL
3982 && !ignore_script
3983#endif
3984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003986#ifdef MESSAGE_QUEUE
3987 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003988#endif
3989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3991 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003992 // Reached EOF.
3993 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3994 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 closescript();
3996 /*
3997 * When reading script file is interrupted, return an ESC to get
3998 * back to normal mode.
3999 * Otherwise return -1, because typebuf.tb_buf[] has changed.
4000 */
4001 if (got_int)
4002 retesc = TRUE;
4003 else
4004 return -1;
4005 }
4006 else
4007 {
4008 buf[0] = script_char;
4009 len = 1;
4010 }
4011 }
4012
Bram Moolenaar30613902019-12-01 22:11:18 +01004013 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
4015 /*
4016 * If we got an interrupt, skip all previously typed characters and
4017 * return TRUE if quit reading script file.
4018 * Stop reading typeahead when a single CTRL-C was read,
4019 * fill_input_buf() returns this when not able to read from stdin.
4020 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
4021 * and buf may be pointing inside typebuf.tb_buf[].
4022 */
4023 if (got_int)
4024 {
kylo252ae6f1d82022-02-16 19:24:07 +00004025#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 char_u dum[DUM_LEN + 1];
4027
4028 for (;;)
4029 {
4030 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01004031 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 break;
4033 }
4034 return retesc;
4035 }
4036
4037 /*
4038 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004039 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004041 if (wait_time == -1L || wait_time > 10L)
4042 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044 /*
4045 * Fill up to a third of the buffer, because each character may be
4046 * tripled below.
4047 */
4048 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
4049 }
4050
Bram Moolenaar30613902019-12-01 22:11:18 +01004051 // If the typebuf was changed further down, it is like nothing was added by
4052 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (typebuf_changed(tb_change_cnt))
4054 return 0;
4055
Bram Moolenaar30613902019-12-01 22:11:18 +01004056 // Note the change in the typeahead buffer, this matters for when
4057 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
4058 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02004059 if (len > 0 && ++typebuf.tb_change_cnt == 0)
4060 typebuf.tb_change_cnt = 1;
4061
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004062 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063}
4064
4065/*
4066 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004067 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 * Returns the new length.
4069 */
4070 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004071fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 int i;
4074 char_u *p = buf;
4075
4076 /*
4077 * Two characters are special: NUL and K_SPECIAL.
4078 * When compiled With the GUI CSI is also special.
4079 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
4080 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
4081 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 */
4083 for (i = len; --i >= 0; ++p)
4084 {
4085#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01004086 // When the GUI is used any character can come after a CSI, don't
4087 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 if (gui.in_use && p[0] == CSI && i >= 2)
4089 {
4090 p += 2;
4091 i -= 2;
4092 }
zeertzjq646bb722022-02-16 17:51:47 +00004093# ifndef MSWIN
4094 // When not on MS-Windows and the GUI is not used CSI needs to be
4095 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 else if (!gui.in_use && p[0] == CSI)
4097 {
4098 mch_memmove(p + 3, p + 1, (size_t)i);
4099 *p++ = K_SPECIAL;
4100 *p++ = KS_EXTRA;
4101 *p = (int)KE_CSI;
4102 len += 2;
4103 }
zeertzjq646bb722022-02-16 17:51:47 +00004104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 else
4106#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02004107 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004108 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00004109 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004110#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004111 // Win32 console passes modifiers
4112 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004113# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004114 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004115# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004116 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#endif
4118 ))
4119 {
4120 mch_memmove(p + 3, p + 1, (size_t)i);
4121 p[2] = K_THIRD(p[0]);
4122 p[1] = K_SECOND(p[0]);
4123 p[0] = K_SPECIAL;
4124 p += 2;
4125 len += 2;
4126 }
4127 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004128 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 return len;
4130}
4131
4132#if defined(USE_INPUT_BUF) || defined(PROTO)
4133/*
4134 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4135 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004136 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004137 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
4139 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004140input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004143# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4144 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145# endif
4146 );
4147}
4148#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004149
4150/*
4151 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4152 * typeahead.
4153 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004154 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004155getcmdkeycmd(
4156 int promptc UNUSED,
4157 void *cookie UNUSED,
4158 int indent UNUSED,
4159 getline_opt_T do_concat UNUSED)
4160{
4161 garray_T line_ga;
4162 int c1 = -1;
4163 int c2;
4164 int cmod = 0;
4165 int aborted = FALSE;
4166
4167 ga_init2(&line_ga, 1, 32);
4168
4169 // no mapping for these characters
4170 no_mapping++;
4171
4172 got_int = FALSE;
4173 while (c1 != NUL && !aborted)
4174 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004175 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004176 {
4177 aborted = TRUE;
4178 break;
4179 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004180
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004181 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004182 {
4183 // incomplete <Cmd> is an error, because there is not much the user
4184 // could do in this state.
4185 emsg(_(e_cmd_mapping_must_end_with_cr));
4186 aborted = TRUE;
4187 break;
4188 }
4189
4190 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004191 c1 = vgetorpeek(TRUE);
4192
Bram Moolenaar957cf672020-11-12 14:21:06 +01004193 // Get two extra bytes for special keys
4194 if (c1 == K_SPECIAL)
4195 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004196 c1 = vgetorpeek(TRUE);
4197 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004198 if (c1 == KS_MODIFIER)
4199 {
4200 cmod = c2;
4201 continue;
4202 }
4203 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004204
4205 // K_ESC is used to avoid ambiguity with the single Esc character
4206 // that might be the start of an escape sequence. Convert it back
4207 // to a single Esc here.
4208 if (c1 == K_ESC)
4209 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004210 }
4211
4212 if (got_int)
4213 aborted = TRUE;
4214 else if (c1 == '\r' || c1 == '\n')
4215 c1 = NUL; // end the line
4216 else if (c1 == ESC)
4217 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004218 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004219 {
4220 // give a nicer error message for this special case
4221 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4222 aborted = TRUE;
4223 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004224 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004225 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004226 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004227 }
4228 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004229 {
4230 if (cmod != 0)
4231 {
4232 ga_append(&line_ga, K_SPECIAL);
4233 ga_append(&line_ga, KS_MODIFIER);
4234 ga_append(&line_ga, cmod);
4235 }
4236 if (IS_SPECIAL(c1))
4237 {
4238 ga_append(&line_ga, K_SPECIAL);
4239 ga_append(&line_ga, K_SECOND(c1));
4240 ga_append(&line_ga, K_THIRD(c1));
4241 }
4242 else
4243 ga_append(&line_ga, c1);
4244 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004245
4246 cmod = 0;
4247 }
4248
4249 no_mapping--;
4250
4251 if (aborted)
4252 ga_clear(&line_ga);
4253
4254 return (char_u *)line_ga.ga_data;
4255}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004256
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004257#if defined(FEAT_EVAL) || defined(PROTO)
4258/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004259 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4260 * is set when redo'ing.
4261 * Put this SID in the redo buffer, so that "." will use the same script
4262 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004263 */
4264 void
4265may_add_last_used_map_to_redobuff(void)
4266{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004267 char_u buf[3 + 20];
4268 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004269
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004270 if (last_used_map != NULL)
4271 sid = last_used_map->m_script_ctx.sc_sid;
4272 if (sid < 0)
4273 sid = last_used_sid;
4274
4275 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004276 return;
4277
4278 // <K_SID>{nr};
4279 buf[0] = K_SPECIAL;
4280 buf[1] = KS_EXTRA;
4281 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004282 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004283 add_buff(&redobuff, buf, -1L);
4284}
4285#endif
4286
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004287 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004288do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004289{
4290 int res;
4291#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004292 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004293
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004294 if (key == K_SCRIPT_COMMAND
4295 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004296 {
4297 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004298 if (last_used_map != NULL)
4299 current_sctx = last_used_map->m_script_ctx;
4300 else
4301 {
4302 current_sctx.sc_sid = last_used_sid;
4303 current_sctx.sc_lnum = 0;
4304 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4305 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004306 }
4307#endif
4308
4309 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4310
4311#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004312 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004313 current_sctx = save_current_sctx;
4314#endif
4315
4316 return res;
4317}
4318
4319#if defined(FEAT_EVAL) || defined(PROTO)
4320 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004321reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004322{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004323 if (last_used_map != mp)
4324 return;
4325
4326 last_used_map = NULL;
4327 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004328}
4329#endif