blob: 2fb9baaf85c14fad247929c32cd332c84f7c5656 [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
45/*
46 * when block_redo is TRUE redo buffer will not be changed
47 * used by edit() to repeat insertions and 'V' command for redoing
48 */
49static int block_redo = FALSE;
50
Bram Moolenaar459fd782019-10-13 16:43:39 +020051static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020054 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 *
56 * typebuf.tb_buf[] contains all characters that are not consumed yet.
57 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
58 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
59 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
60 * The head of the buffer may contain the result of mappings, abbreviations
61 * and @a commands. The length of this part is typebuf.tb_maplen.
62 * typebuf.tb_silent is the part where <silent> applies.
63 * After the head are characters that come from the terminal.
64 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
65 * should not be considered for abbreviations.
66 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
67 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
68 * contains RM_NONE for the characters that are not to be remapped.
69 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
70 * (typebuf has been put in globals.h, because check_termcode() needs it).
71 */
Bram Moolenaar30613902019-12-01 22:11:18 +010072#define RM_YES 0 // tb_noremap: remap
73#define RM_NONE 1 // tb_noremap: don't remap
74#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
75#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar30613902019-12-01 22:11:18 +010077// typebuf.tb_buf has three parts: room in front (for result of mappings), the
78// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000079// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010081static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
82static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010088int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000089#endif
90
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020094static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020096static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020098static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100/*
101 * Free and clear a buffer.
102 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200103 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100104free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100106 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar285e3352018-04-18 23:01:13 +0200108 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 {
110 np = p->b_next;
111 vim_free(p);
112 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200113 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000114 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115}
116
117/*
118 * Return the contents of a buffer as a single string.
119 * K_SPECIAL and CSI in the returned string are escaped.
120 */
121 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100122get_buffcont(
123 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100124 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 long_u count = 0;
127 char_u *p = NULL;
128 char_u *p2;
129 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200130 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar30613902019-12-01 22:11:18 +0100132 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200133 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 count += (long_u)STRLEN(bp->b_str);
135
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200136 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
138 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200139 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 for (str = bp->b_str; *str; )
141 *p2++ = *str++;
142 *p2 = NUL;
143 }
144 return (p);
145}
146
147/*
148 * Return the contents of the record buffer as a single string
149 * and clear the record buffer.
150 * K_SPECIAL and CSI in the returned string are escaped.
151 */
152 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100153get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 char_u *p;
156 size_t len;
157
158 p = get_buffcont(&recordbuff, TRUE);
159 free_buff(&recordbuff);
160
161 /*
162 * Remove the characters that were added the last time, these must be the
163 * (possibly mapped) characters that stopped the recording.
164 */
165 len = STRLEN(p);
166 if ((int)len >= last_recorded_len)
167 {
168 len -= last_recorded_len;
169 p[len] = NUL;
170 }
171
172 /*
173 * When stopping recording from Insert mode with CTRL-O q, also remove the
174 * CTRL-O.
175 */
176 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
177 p[len - 1] = NUL;
178
179 return (p);
180}
181
182/*
183 * Return the contents of the redo buffer as a single string.
184 * K_SPECIAL and CSI in the returned string are escaped.
185 */
186 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100187get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000189 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
192/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000193 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 * K_SPECIAL and CSI should have been escaped already.
195 */
196 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100197add_buff(
198 buffheader_T *buf,
199 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100200 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100202 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200203 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205 if (slen < 0)
206 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100207 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 return;
209
Bram Moolenaar30613902019-12-01 22:11:18 +0100210 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200213 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100215 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000217 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 return;
219 }
220 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200221 mch_memmove(buf->bh_first.b_next->b_str,
222 buf->bh_first.b_next->b_str + buf->bh_index,
223 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 buf->bh_index = 0;
225
226 if (buf->bh_space >= (int)slen)
227 {
228 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000229 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 buf->bh_space -= slen;
231 }
232 else
233 {
234 if (slen < MINIMAL_SIZE)
235 len = MINIMAL_SIZE;
236 else
237 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200238 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100240 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000241 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000242 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar285e3352018-04-18 23:01:13 +0200244 p->b_next = buf->bh_curr->b_next;
245 buf->bh_curr->b_next = p;
246 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248}
249
250/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000251 * Delete "slen" bytes from the end of "buf".
252 * Only works when it was just added.
253 */
254 static void
255delete_buff_tail(buffheader_T *buf, int slen)
256{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000257 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000258
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000259 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000260 return; // nothing to delete
261 len = (int)STRLEN(buf->bh_curr->b_str);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000262 if (len < slen)
263 return;
264
265 buf->bh_curr->b_str[len - slen] = NUL;
266 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000267}
268
269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 * Add number "n" to buffer "buf".
271 */
272 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100273add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
275 char_u number[32];
276
277 sprintf((char *)number, "%ld", n);
278 add_buff(buf, number, -1L);
279}
280
281/*
282 * Add character 'c' to buffer "buf".
283 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
284 */
285 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100286add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 char_u bytes[MB_MAXBYTES + 1];
289 int len;
290 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 char_u temp[4];
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 if (IS_SPECIAL(c))
294 len = 1;
295 else
296 len = (*mb_char2bytes)(c, bytes);
297 for (i = 0; i < len; ++i)
298 {
299 if (!IS_SPECIAL(c))
300 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
303 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100304 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 temp[0] = K_SPECIAL;
306 temp[1] = K_SECOND(c);
307 temp[2] = K_THIRD(c);
308 temp[3] = NUL;
309 }
310#ifdef FEAT_GUI
311 else if (c == CSI)
312 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100313 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 temp[0] = CSI;
315 temp[1] = KS_EXTRA;
316 temp[2] = (int)KE_CSI;
317 temp[3] = NUL;
318 }
319#endif
320 else
321 {
322 temp[0] = c;
323 temp[1] = NUL;
324 }
325 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327}
328
Bram Moolenaar30613902019-12-01 22:11:18 +0100329// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200330static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100331
Bram Moolenaar30613902019-12-01 22:11:18 +0100332// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200333static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100336 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
337 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 * If advance == TRUE go to the next char.
339 * No translation is done K_SPECIAL and CSI are escaped.
340 */
341 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100342read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100344 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100346 c = read_readbuf(&readbuf1, advance);
347 if (c == NUL)
348 c = read_readbuf(&readbuf2, advance);
349 return c;
350}
351
352 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100353read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100354{
355 char_u c;
356 buffblock_T *curr;
357
Bram Moolenaar30613902019-12-01 22:11:18 +0100358 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return NUL;
360
Bram Moolenaar285e3352018-04-18 23:01:13 +0200361 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100362 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364 if (advance)
365 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100366 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100370 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 }
373 return c;
374}
375
376/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100377 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 */
379 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100380start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200382 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200384 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385 readbuf1.bh_space = 0;
386 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200387 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100388 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200389 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
392}
393
394/*
395 * Return TRUE if the stuff buffer is empty.
396 */
397 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100398stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200400 return (readbuf1.bh_first.b_next == NULL
401 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100402}
403
Bram Moolenaar113e1072019-01-20 15:30:40 +0100404#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100405/*
406 * Return TRUE if readbuf1 is empty. There may still be redo characters in
407 * redbuf2.
408 */
409 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100410readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100411{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200412 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416/*
417 * Set a typeahead character that won't be flushed.
418 */
419 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100420typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421{
422 typeahead_char = c;
423}
424
425/*
426 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100427 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 * flush all typeahead characters (used when interrupted by a CTRL-C).
429 */
430 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200431flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
433 init_typebuf();
434
435 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100436 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 ;
438
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200439 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200441 // remove mapped characters at the start only
442 typebuf.tb_off += typebuf.tb_maplen;
443 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100444#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
445 if (typebuf.tb_len == 0)
446 typebuf_was_filled = FALSE;
447#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200448 }
449 else
450 {
451 // remove typeahead
452 if (flush_typeahead == FLUSH_INPUT)
453 // We have to get all characters, because we may delete the first
454 // part of an escape sequence. In an xterm we get one char at a
455 // time and we have to get them all.
456 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
457 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 typebuf.tb_off = MAXMAPLEN;
459 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200460#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100461 // Reset the flag that text received from a client or from feedkeys()
462 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200463 typebuf_was_filled = FALSE;
464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 typebuf.tb_maplen = 0;
467 typebuf.tb_silent = 0;
468 cmd_silent = FALSE;
469 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200470 if (++typebuf.tb_change_cnt == 0)
471 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472}
473
474/*
475 * The previous contents of the redo buffer is kept in old_redobuffer.
476 * This is used for the CTRL-O <.> command in insert mode.
477 */
478 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100479ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000481 if (block_redo)
482 return;
483
484 free_buff(&old_redobuff);
485 old_redobuff = redobuff;
486 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487}
488
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100489/*
490 * Discard the contents of the redo buffer and restore the previous redo
491 * buffer.
492 */
493 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100494CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100495{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000496 if (block_redo)
497 return;
498
499 free_buff(&redobuff);
500 redobuff = old_redobuff;
501 old_redobuff.bh_first.b_next = NULL;
502 start_stuff();
503 while (read_readbuffers(TRUE) != NUL)
504 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100505}
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507/*
508 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
509 * Used before executing autocommands and user functions.
510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200512saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
514 char_u *s;
515
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200516 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200517 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200518 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200519 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
Bram Moolenaar30613902019-12-01 22:11:18 +0100521 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200522 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000523 if (s == NULL)
524 return;
525
526 add_buff(&redobuff, s, -1L);
527 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528}
529
530/*
531 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
532 * Used after executing autocommands and user functions.
533 */
534 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200535restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200537 free_buff(&redobuff);
538 redobuff = save_redo->sr_redobuff;
539 free_buff(&old_redobuff);
540 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543/*
544 * Append "s" to the redo buffer.
545 * K_SPECIAL and CSI should already have been escaped.
546 */
547 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100548AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 if (!block_redo)
551 add_buff(&redobuff, s, -1L);
552}
553
554/*
555 * Append to Redo buffer literally, escaping special characters with CTRL-V.
556 * K_SPECIAL and CSI are escaped as well.
557 */
558 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100559AppendToRedobuffLit(
560 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100561 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000563 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 int c;
565 char_u *start;
566
567 if (block_redo)
568 return;
569
Bram Moolenaarebefac62005-12-28 22:39:57 +0000570 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100572 // Put a string of normal characters in the redo buffer (that's
573 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000575 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 ++s;
577
Bram Moolenaar30613902019-12-01 22:11:18 +0100578 // Don't put '0' or '^' as last character, just in case a CTRL-D is
579 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
581 --s;
582 if (s > start)
583 add_buff(&redobuff, start, (long)(s - start));
584
Bram Moolenaarebefac62005-12-28 22:39:57 +0000585 if (*s == NUL || (len >= 0 && s - str >= len))
586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar30613902019-12-01 22:11:18 +0100588 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000589 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100590 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000591 c = mb_cptr2char_adv(&s);
592 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000593 c = *s++;
594 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
595 add_char_buff(&redobuff, Ctrl_V);
596
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000597 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000598 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000599 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000600 else
601 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603}
604
605/*
606 * Append a character to the redo buffer.
607 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
608 */
609 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100610AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 if (!block_redo)
613 add_char_buff(&redobuff, c);
614}
615
616/*
617 * Append a number to the redo buffer.
618 */
619 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100620AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 if (!block_redo)
623 add_num_buff(&redobuff, n);
624}
625
626/*
627 * Append string "s" to the stuff buffer.
628 * CSI and K_SPECIAL must already have been escaped.
629 */
630 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100631stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100633 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634}
635
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200636/*
637 * Append string "s" to the redo stuff buffer.
638 * CSI and K_SPECIAL must already have been escaped.
639 */
640 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100641stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200642{
643 add_buff(&readbuf2, s, -1L);
644}
645
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200646 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100647stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100649 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650}
651
652#if defined(FEAT_EVAL) || defined(PROTO)
653/*
654 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
655 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200656 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 */
658 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100659stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200661 int c;
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 while (*s != NUL)
664 {
665 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
666 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100667 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 stuffReadbuffLen(s, 3L);
669 s += 3;
670 }
671 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200672 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100673 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200674 if (c == CAR || c == NL || c == ESC)
675 c = ' ';
676 stuffcharReadbuff(c);
677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679}
680#endif
681
682/*
683 * Append a character to the stuff buffer.
684 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
685 */
686 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100687stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100689 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
692/*
693 * Append a number to the stuff buffer.
694 */
695 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100696stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100698 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
701/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200702 * Stuff a string into the typeahead buffer, such that edit() will insert it
703 * literally ("literally" TRUE) or interpret is as typed characters.
704 */
705 void
706stuffescaped(char_u *arg, int literally)
707{
708 int c;
709 char_u *start;
710
711 while (*arg != NUL)
712 {
713 // Stuff a sequence of normal ASCII characters, that's fast. Also
714 // stuff K_SPECIAL to get the effect of a special key when "literally"
715 // is TRUE.
716 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000717 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200718 || (*arg == K_SPECIAL && !literally))
719 ++arg;
720 if (arg > start)
721 stuffReadbuffLen(start, (long)(arg - start));
722
723 // stuff a single special character
724 if (*arg != NUL)
725 {
726 if (has_mbyte)
727 c = mb_cptr2char_adv(&arg);
728 else
729 c = *arg++;
730 if (literally && ((c < ' ' && c != TAB) || c == DEL))
731 stuffcharReadbuff(Ctrl_V);
732 stuffcharReadbuff(c);
733 }
734 }
735}
736
737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
739 * multibyte characters.
740 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100741 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
742 * otherwise.
743 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
745 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100746read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100748 static buffblock_T *bp;
749 static char_u *p;
750 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 int n;
752 char_u buf[MB_MAXBYTES + 1];
753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 if (init)
756 {
757 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200758 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200760 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (bp == NULL)
762 return FAIL;
763 p = bp->b_str;
764 return OK;
765 }
766 if ((c = *p) != NUL)
767 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100768 // Reverse the conversion done by add_char_buff()
769 // For a multi-byte character get all the bytes and return the
770 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
772 n = MB_BYTE2LEN_CHECK(c);
773 else
774 n = 1;
775 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100777 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
779 c = TO_SPECIAL(p[1], p[2]);
780 p += 2;
781 }
782#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100783 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 p += 2;
785#endif
786 if (*++p == NUL && bp->b_next != NULL)
787 {
788 bp = bp->b_next;
789 p = bp->b_str;
790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100792 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 if (n != 1)
795 c = (*mb_ptr2char)(buf);
796 break;
797 }
798 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100799 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802 }
803
804 return c;
805}
806
807/*
808 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
809 * If old_redo is TRUE, use old_redobuff instead of redobuff.
810 * The escaped K_SPECIAL and CSI are copied without translation.
811 */
812 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100813copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 int c;
816
817 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100818 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100822 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Insert the redo count into the command.
824 * If "old_redo" is TRUE, the last but one command is repeated
825 * instead of the last command (inserting text). This is used for
826 * CTRL-O <.> in insert mode
827 *
828 * return FAIL for failure, OK otherwise
829 */
830 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100831start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
833 int c;
834
Bram Moolenaar30613902019-12-01 22:11:18 +0100835 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (read_redo(TRUE, old_redo) == FAIL)
837 return FAIL;
838
839 c = read_redo(FALSE, old_redo);
840
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100841#ifdef FEAT_EVAL
842 if (c == K_SID)
843 {
844 // Copy the <SID>{sid}; sequence
845 add_char_buff(&readbuf2, c);
846 for (;;)
847 {
848 c = read_redo(FALSE, old_redo);
849 add_char_buff(&readbuf2, c);
850 if (!isdigit(c))
851 break;
852 }
853 c = read_redo(FALSE, old_redo);
854 }
855#endif
856
Bram Moolenaar30613902019-12-01 22:11:18 +0100857 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (c == '"')
859 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100860 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 c = read_redo(FALSE, old_redo);
862
Bram Moolenaar30613902019-12-01 22:11:18 +0100863 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (c >= '1' && c < '9')
865 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100866 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200867
Bram Moolenaar30613902019-12-01 22:11:18 +0100868 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200869 if (c == '=')
870 {
871 add_char_buff(&readbuf2, CAR);
872 cmd_silent = TRUE;
873 }
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 c = read_redo(FALSE, old_redo);
876 }
877
Bram Moolenaar30613902019-12-01 22:11:18 +0100878 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
880 VIsual = curwin->w_cursor;
881 VIsual_active = TRUE;
882 VIsual_select = FALSE;
883 VIsual_reselect = TRUE;
884 redo_VIsual_busy = TRUE;
885 c = read_redo(FALSE, old_redo);
886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
Bram Moolenaar30613902019-12-01 22:11:18 +0100888 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (count)
890 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100891 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100893 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100896 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100897 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 copy_redo(old_redo);
899 return OK;
900}
901
902/*
903 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100904 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * return FAIL for failure, OK otherwise
906 */
907 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100908start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 int c;
911
912 if (read_redo(TRUE, FALSE) == FAIL)
913 return FAIL;
914 start_stuff();
915
Bram Moolenaar30613902019-12-01 22:11:18 +0100916 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 while ((c = read_redo(FALSE, FALSE)) != NUL)
918 {
919 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
920 {
921 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100922 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 break;
924 }
925 }
926
Bram Moolenaar30613902019-12-01 22:11:18 +0100927 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 copy_redo(FALSE);
929 block_redo = TRUE;
930 return OK;
931}
932
933 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100934stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 block_redo = FALSE;
937}
938
939/*
940 * Initialize typebuf.tb_buf to point to typebuf_init.
941 * alloc() cannot be used here: In out-of-memory situations it would
942 * be impossible to type anything.
943 */
944 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100945init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000947 if (typebuf.tb_buf != NULL)
948 return;
949
950 typebuf.tb_buf = typebuf_init;
951 typebuf.tb_noremap = noremapbuf_init;
952 typebuf.tb_buflen = TYPELEN_INIT;
953 typebuf.tb_len = 0;
954 typebuf.tb_off = MAXMAPLEN + 4;
955 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200959 * Returns TRUE when keys cannot be remapped.
960 */
961 int
962noremap_keys(void)
963{
964 return KeyNoremap & (RM_NONE|RM_SCRIPT);
965}
966
967/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100968 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
969 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000971 * If "noremap" is REMAP_YES, new string can be mapped again.
972 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
973 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000974 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000975 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000977 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000979 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
980 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000982 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 *
984 * return FAIL for failure, OK otherwise
985 */
986 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100987ins_typebuf(
988 char_u *str,
989 int noremap,
990 int offset,
991 int nottyped,
992 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 char_u *s1, *s2;
995 int newlen;
996 int addlen;
997 int i;
998 int newoff;
999 int val;
1000 int nrm;
1001
1002 init_typebuf();
1003 if (++typebuf.tb_change_cnt == 0)
1004 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001005 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (offset == 0 && addlen <= typebuf.tb_off)
1010 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001011 /*
1012 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 typebuf.tb_off -= addlen;
1015 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1016 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001017 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1018 >= addlen + 3 * (MAXMAPLEN + 4))
1019 {
1020 /*
1021 * Buffer is empty and string fits in the existing buffer.
1022 * Leave some space before and after, if possible.
1023 */
1024 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1025 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else
1028 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001029 int extra;
1030
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001031 /*
1032 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001033 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001034 * characters. We add some extra room to avoid having to allocate too
1035 * often.
1036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001038 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1039 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001041 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001042 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 setcursor();
1044 return FAIL;
1045 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001046 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001048 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 return FAIL;
1050 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 vim_free(s1);
1054 return FAIL;
1055 }
1056 typebuf.tb_buflen = newlen;
1057
Bram Moolenaar30613902019-12-01 22:11:18 +01001058 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1060 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001061 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001063 // copy the old chars, after the insertion point, including the NUL at
1064 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 mch_memmove(s1 + newoff + offset + addlen,
1066 typebuf.tb_buf + typebuf.tb_off + offset,
1067 (size_t)(typebuf.tb_len - offset + 1));
1068 if (typebuf.tb_buf != typebuf_init)
1069 vim_free(typebuf.tb_buf);
1070 typebuf.tb_buf = s1;
1071
1072 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1073 (size_t)offset);
1074 mch_memmove(s2 + newoff + offset + addlen,
1075 typebuf.tb_noremap + typebuf.tb_off + offset,
1076 (size_t)(typebuf.tb_len - offset));
1077 if (typebuf.tb_noremap != noremapbuf_init)
1078 vim_free(typebuf.tb_noremap);
1079 typebuf.tb_noremap = s2;
1080
1081 typebuf.tb_off = newoff;
1082 }
1083 typebuf.tb_len += addlen;
1084
Bram Moolenaar30613902019-12-01 22:11:18 +01001085 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (noremap == REMAP_SCRIPT)
1087 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001088 else if (noremap == REMAP_SKIP)
1089 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 else
1091 val = RM_NONE;
1092
1093 /*
1094 * Adjust typebuf.tb_noremap[] for the new characters:
1095 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1096 * (sometimes) not remappable
1097 * If noremap == REMAP_YES: all the new characters are mappable
1098 * If noremap > 0: "noremap" characters are not remappable, the rest
1099 * mappable
1100 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001101 if (noremap == REMAP_SKIP)
1102 nrm = 1;
1103 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 nrm = addlen;
1105 else
1106 nrm = noremap;
1107 for (i = 0; i < addlen; ++i)
1108 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1109 (--nrm >= 0) ? val : RM_YES;
1110
Bram Moolenaar30613902019-12-01 22:11:18 +01001111 // tb_maplen and tb_silent only remember the length of mapped and/or
1112 // silent mappings at the start of the buffer, assuming that a mapped
1113 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (nottyped || typebuf.tb_maplen > offset)
1115 typebuf.tb_maplen += addlen;
1116 if (silent || typebuf.tb_silent > offset)
1117 {
1118 typebuf.tb_silent += addlen;
1119 cmd_silent = TRUE;
1120 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001121 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 typebuf.tb_no_abbr_cnt += addlen;
1123
1124 return OK;
1125}
1126
1127/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 * Put character "c" back into the typeahead buffer.
1129 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001130 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1131 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001132 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001133 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001134 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001135ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001136{
zeertzjq6cac7702022-01-04 18:01:21 +00001137 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001138 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001139
zeertzjq2e7cba32022-06-10 15:30:32 +01001140 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001141 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001142 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001143}
1144
1145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001147 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001148 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1150 * changed it was reallocated and the old pointer can no longer be used.
1151 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1152 * that was just added.
1153 */
1154 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001155typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001156 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001159#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1160 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161#endif
1162 ));
1163}
1164
1165/*
1166 * Return TRUE if there are no characters in the typeahead buffer that have
1167 * not been typed (result from a mapping or come from ":normal").
1168 */
1169 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001170typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 return typebuf.tb_maplen == 0;
1173}
1174
1175/*
1176 * Return the number of characters that are mapped (or not typed).
1177 */
1178 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001179typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 return typebuf.tb_maplen;
1182}
1183
1184/*
1185 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1186 */
1187 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001188del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 int i;
1191
1192 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001193 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 typebuf.tb_len -= len;
1196
1197 /*
1198 * Easy case: Just increase typebuf.tb_off.
1199 */
1200 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1201 >= 3 * MAXMAPLEN + 3)
1202 typebuf.tb_off += len;
1203 /*
1204 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1205 */
1206 else
1207 {
1208 i = typebuf.tb_off + offset;
1209 /*
1210 * Leave some extra room at the end to avoid reallocation.
1211 */
1212 if (typebuf.tb_off > MAXMAPLEN)
1213 {
1214 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1215 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1216 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1217 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1218 typebuf.tb_off = MAXMAPLEN;
1219 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001220 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1222 typebuf.tb_buf + i + len,
1223 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1226 typebuf.tb_noremap + i + len,
1227 (size_t)(typebuf.tb_len - offset));
1228 }
1229
Bram Moolenaar30613902019-12-01 22:11:18 +01001230 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 if (typebuf.tb_maplen < offset + len)
1233 typebuf.tb_maplen = offset;
1234 else
1235 typebuf.tb_maplen -= len;
1236 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001237 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (typebuf.tb_silent < offset + len)
1240 typebuf.tb_silent = offset;
1241 else
1242 typebuf.tb_silent -= len;
1243 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 if (typebuf.tb_no_abbr_cnt < offset + len)
1247 typebuf.tb_no_abbr_cnt = offset;
1248 else
1249 typebuf.tb_no_abbr_cnt -= len;
1250 }
1251
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001252#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001253 // Reset the flag that text received from a client or from feedkeys()
1254 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001255 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257 if (++typebuf.tb_change_cnt == 0)
1258 typebuf.tb_change_cnt = 1;
1259}
1260
1261/*
1262 * Write typed characters to script file.
1263 * If recording is on put the character in the recordbuffer.
1264 */
1265 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001266gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001268 char_u *s = chars;
1269 int i;
1270 static char_u buf[4];
1271 static int buflen = 0;
1272 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001274 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001276 buf[buflen++] = *s++;
1277
1278 // When receiving a special key sequence, store it until we have all
1279 // the bytes and we can decide what to do with it.
1280 if (buflen == 1 && buf[0] == K_SPECIAL)
1281 continue;
1282 if (buflen == 2)
1283 continue;
1284 if (buflen == 3 && buf[1] == KS_EXTRA
1285 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1286 {
1287 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1288 // recording.
1289 buflen = 0;
1290 continue;
1291 }
1292
Bram Moolenaar30613902019-12-01 22:11:18 +01001293 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001294 for (i = 0; i < buflen; ++i)
1295 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001297 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001299 buf[buflen] = NUL;
1300 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001301 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001302 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001304 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 may_sync_undo();
1307
1308#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001309 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 debug_did_msg = FALSE;
1311#endif
1312
Bram Moolenaar30613902019-12-01 22:11:18 +01001313 // Since characters have been typed, consider the following to be in
1314 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ++maptick;
1316}
1317
1318/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001319 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1320 * character back into the typeahead buffer, thus gotchars() will be called
1321 * again.
1322 * Only affects recorded characters.
1323 */
1324 void
1325ungetchars(int len)
1326{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001327 if (reg_recording == 0)
1328 return;
1329
1330 delete_buff_tail(&recordbuff, len);
1331 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001332}
1333
1334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Sync undo. Called when typed characters are obtained from the typeahead
1336 * buffer, or when a menu is used.
1337 * Do not sync:
1338 * - In Insert mode, unless cursor key has been used.
1339 * - While reading a script file.
1340 * - When no_u_sync is non-zero.
1341 */
1342 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001343may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar24959102022-05-07 20:01:16 +01001345 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001346 && scriptin[curscript] == NULL)
1347 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348}
1349
1350/*
1351 * Make "typebuf" empty and allocate new buffers.
1352 * Returns FAIL when out of memory.
1353 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001354 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001355alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 typebuf.tb_buf = alloc(TYPELEN_INIT);
1358 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1359 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1360 {
1361 free_typebuf();
1362 return FAIL;
1363 }
1364 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001365 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 typebuf.tb_len = 0;
1367 typebuf.tb_maplen = 0;
1368 typebuf.tb_silent = 0;
1369 typebuf.tb_no_abbr_cnt = 0;
1370 if (++typebuf.tb_change_cnt == 0)
1371 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001372#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1373 typebuf_was_filled = FALSE;
1374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 return OK;
1376}
1377
1378/*
1379 * Free the buffers of "typebuf".
1380 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001381 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001382free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001384 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001385 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001386 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001387 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001388 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001389 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001390 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001391 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
1393
1394/*
1395 * When doing ":so! file", the current typeahead needs to be saved, and
1396 * restored when "file" has been read completely.
1397 */
1398static typebuf_T saved_typebuf[NSCRIPT];
1399
1400 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001401save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 init_typebuf();
1404 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001405 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (alloc_typebuf() == FAIL)
1407 {
1408 closescript();
1409 return FAIL;
1410 }
1411 return OK;
1412}
1413
Bram Moolenaar30613902019-12-01 22:11:18 +01001414static int old_char = -1; // character put back by vungetc()
1415static int old_mod_mask; // mod_mask for ungotten character
1416static int old_mouse_row; // mouse_row related to old_char
1417static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001418static int old_KeyStuffed; // whether old_char was stuffed
1419
zeertzjq6d4e7252022-04-07 13:58:04 +01001420static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001421{
1422 // If the old character was not stuffed and characters have been added to
1423 // the stuff buffer, need to first get the stuffed characters instead.
1424 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1425}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427/*
1428 * Save all three kinds of typeahead, so that the user must type at a prompt.
1429 */
1430 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001431save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 tp->save_typebuf = typebuf;
1434 tp->typebuf_valid = (alloc_typebuf() == OK);
1435 if (!tp->typebuf_valid)
1436 typebuf = tp->save_typebuf;
1437
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001438 tp->old_char = old_char;
1439 tp->old_mod_mask = old_mod_mask;
1440 old_char = -1;
1441
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001442 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001443 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001444 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001445 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef USE_INPUT_BUF
1447 tp->save_inputbuf = get_input_buf();
1448# endif
1449}
1450
1451/*
1452 * Restore the typeahead to what it was before calling save_typeahead().
1453 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001454 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001457restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (tp->typebuf_valid)
1460 {
1461 free_typebuf();
1462 typebuf = tp->save_typebuf;
1463 }
1464
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001465 old_char = tp->old_char;
1466 old_mod_mask = tp->old_mod_mask;
1467
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001468 free_buff(&readbuf1);
1469 readbuf1 = tp->save_readbuf1;
1470 free_buff(&readbuf2);
1471 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001473 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474# endif
1475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477/*
1478 * Open a new script file for the ":source!" command.
1479 */
1480 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001481openscript(
1482 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001483 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 if (curscript + 1 == NSCRIPT)
1486 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001487 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return;
1489 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001490
1491 // Disallow sourcing a file in the sandbox, the commands would be executed
1492 // later, possibly outside of the sandbox.
1493 if (check_secure())
1494 return;
1495
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001496#ifdef FEAT_EVAL
1497 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001498 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001499 return;
1500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaar30613902019-12-01 22:11:18 +01001502 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001504 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 expand_env(name, NameBuff, MAXPATHL);
1506 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1507 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001508 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (curscript)
1510 --curscript;
1511 return;
1512 }
1513 if (save_typebuf() == FAIL)
1514 return;
1515
1516 /*
1517 * Execute the commands from the file right now when using ":source!"
1518 * after ":global" or ":argdo" or in a loop. Also when another command
1519 * follows. This means the display won't be updated. Don't do this
1520 * always, "make test" would fail.
1521 */
1522 if (directly)
1523 {
1524 oparg_T oa;
1525 int oldcurscript;
1526 int save_State = State;
1527 int save_restart_edit = restart_edit;
1528 int save_insertmode = p_im;
1529 int save_finish_op = finish_op;
1530 int save_msg_scroll = msg_scroll;
1531
Bram Moolenaar24959102022-05-07 20:01:16 +01001532 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001533 msg_scroll = FALSE; // no msg scrolling in Normal mode
1534 restart_edit = 0; // don't go to Insert mode
1535 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 clear_oparg(&oa);
1537 finish_op = FALSE;
1538
1539 oldcurscript = curscript;
1540 do
1541 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001542 update_topline_cursor(); // update cursor position and topline
1543 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001544 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 while (scriptin[oldcurscript] != NULL);
1547
1548 State = save_State;
1549 msg_scroll = save_msg_scroll;
1550 restart_edit = save_restart_edit;
1551 p_im = save_insertmode;
1552 finish_op = save_finish_op;
1553 }
1554}
1555
1556/*
1557 * Close the currently active input script.
1558 */
1559 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001560closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 free_typebuf();
1563 typebuf = saved_typebuf[curscript];
1564
1565 fclose(scriptin[curscript]);
1566 scriptin[curscript] = NULL;
1567 if (curscript > 0)
1568 --curscript;
1569}
1570
Bram Moolenaarea408852005-06-25 22:49:46 +00001571#if defined(EXITFREE) || defined(PROTO)
1572 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001573close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001574{
1575 while (scriptin[0] != NULL)
1576 closescript();
1577}
1578#endif
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580/*
1581 * Return TRUE when reading keys from a script file.
1582 */
1583 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001584using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 return scriptin[curscript] != NULL;
1587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001590 * This function is called just before doing a blocking wait. Thus after
1591 * waiting 'updatetime' for a character to arrive.
1592 */
1593 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001594before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001595{
1596 updatescript(0);
1597#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001598 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001599 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001600#endif
1601}
1602
1603/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001604 * updatescript() is called when a character can be written into the script
1605 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 *
1607 * All the changed memfiles are synced if c == 0 or when the number of typed
1608 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1609 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001610 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001611updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 static int count = 0;
1614
1615 if (c && scriptout)
1616 putc(c, scriptout);
1617 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1618 {
1619 ml_sync_all(c == 0, TRUE);
1620 count = 0;
1621 }
1622}
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001625 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001626 * character.
1627 */
1628 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001629merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001630{
1631 int c = c_arg;
1632
zeertzjqbad8a012022-04-29 16:44:00 +01001633 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001634 {
1635 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001636 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001637 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001638 if (c == NUL)
1639 c = K_ZERO;
1640 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001641 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001642 // CTRL-6 is equivalent to CTRL-^
1643 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001644#ifdef FEAT_GUI_GTK
1645 // These mappings look arbitrary at the first glance, but in fact
1646 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1647 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1648 // more sense and is consistent with usual terminal behaviour).
1649 else if (c == '2')
1650 c = NUL;
1651 else if (c >= '3' && c <= '7')
1652 c = c ^ 0x28;
1653 else if (c == '8')
1654 c = BS;
1655 else if (c == '?')
1656 c = DEL;
1657#endif
1658 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001659 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001660 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001661 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001662 && c >= 0 && c <= 127)
1663 {
1664 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001665 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001666 }
1667 return c;
1668}
1669
1670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 * Get the next input character.
1672 * Can return a special key or a multi-byte character.
1673 * Can return NUL when called recursively, use safe_vgetc() if that's not
1674 * wanted.
1675 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1676 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001677 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
1679 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001680vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001684 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001687#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001688 // Do garbage collection when garbagecollect() was called previously and
1689 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001690 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001691 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001692#endif
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 /*
1695 * If a character was put back with vungetc, it was already processed.
1696 * Return it directly.
1697 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001698 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 c = old_char;
1701 old_char = -1;
1702 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001703 mouse_row = old_mouse_row;
1704 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
zeertzjq81b46a62022-04-09 17:58:49 +01001708 // number of characters recorded from the last vgetc() call
1709 static int last_vgetc_recorded_len = 0;
1710
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001711 mod_mask = 0;
1712 vgetc_mod_mask = 0;
1713 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001714
1715 // last_recorded_len can be larger than last_vgetc_recorded_len
1716 // if peeking records more
1717 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001718
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001719 for (;;) // this is done twice if there are modifiers
1720 {
1721 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001722
Bram Moolenaar78aed952022-09-24 15:36:35 +01001723 // No mapping after modifier has been read, using an input method
1724 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001725 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001726#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001727 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001728#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001729#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001730 || popup_no_mapping()
1731#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001732 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001734 ++no_mapping;
1735 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001736 // mod_mask value may change, remember we did the increment
1737 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001739 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001740 if (did_inc)
1741 {
1742 --no_mapping;
1743 --allow_keys;
1744 }
1745
Christopher Plewright03193062022-11-22 12:58:27 +00001746 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001747 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001748#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001749 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001750#endif
1751 )
1752 {
1753 int save_allow_keys = allow_keys;
1754
1755 ++no_mapping;
1756 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001757 c2 = vgetorpeek(TRUE); // no mapping for these chars
1758 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001759 --no_mapping;
1760 allow_keys = save_allow_keys;
1761 if (c2 == KS_MODIFIER)
1762 {
1763 mod_mask = c;
1764 continue;
1765 }
1766 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001768 // K_ESC is used to avoid ambiguity with the single Esc
1769 // character that might be the start of an escape sequence.
1770 // Convert it back to a single Esc here.
1771 if (c == K_ESC)
1772 c = ESC;
1773
Bram Moolenaar4f974752019-02-17 17:44:42 +01001774#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001775 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1776 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001777 if (
1778# ifdef VIMDLL
1779 gui.in_use &&
1780# endif
1781 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001783 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001784 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001785
1786 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001787 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001788 {
K.Takata54119102022-02-03 13:33:03 +00001789 name[j] = c;
1790 if (j < 199)
1791 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001792 }
K.Takata54119102022-02-03 13:33:03 +00001793 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001794 gui_make_tearoff(name);
1795 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001798#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001799 // GTK: <F10> normally selects the menu, but it's passed until
1800 // here to allow mapping it. Intercept and invoke the GTK
1801 // behavior if it's not mapped.
1802 if (c == K_F10 && gui.menubar != NULL)
1803 {
1804 gtk_menu_shell_select_first(
1805 GTK_MENU_SHELL(gui.menubar), FALSE);
1806 continue;
1807 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001810 // Handle focus event here, so that the caller doesn't need to
1811 // know about it. Return K_IGNORE so that we loop once (needed
1812 // if 'lazyredraw' is set).
1813 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001814 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001815 ui_focus_change(c == K_FOCUSGAINED);
1816 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001817 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001818
1819 // Translate K_CSI to CSI. The special key is only used to
1820 // avoid it being recognized as the start of a special key.
1821 if (c == K_CSI)
1822 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001824#ifdef FEAT_EVAL
1825 if (c == K_SID)
1826 {
1827 int j;
1828
1829 // Handle <SID>{sid}; Do up to 20 digits for safety.
1830 last_used_sid = 0;
1831 for (j = 0; j < 20 && isdigit(c = vgetorpeek(TRUE)); ++j)
1832 last_used_sid = last_used_sid * 10 + (c - '0');
1833 last_used_map = NULL;
1834 continue;
1835 }
1836#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001837 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001838
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001839 // a keypad or special function key was not mapped, use it like
1840 // its ASCII equivalent
1841 switch (c)
1842 {
1843 case K_KPLUS: c = '+'; break;
1844 case K_KMINUS: c = '-'; break;
1845 case K_KDIVIDE: c = '/'; break;
1846 case K_KMULTIPLY: c = '*'; break;
1847 case K_KENTER: c = CAR; break;
1848 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001849#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001850 // Can be either '.' or a ',',
1851 // depending on the type of keypad.
1852 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001853#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001854 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001855#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001856 case K_K0: c = '0'; break;
1857 case K_K1: c = '1'; break;
1858 case K_K2: c = '2'; break;
1859 case K_K3: c = '3'; break;
1860 case K_K4: c = '4'; break;
1861 case K_K5: c = '5'; break;
1862 case K_K6: c = '6'; break;
1863 case K_K7: c = '7'; break;
1864 case K_K8: c = '8'; break;
1865 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001866
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001867 case K_XHOME:
1868 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001869 {
1870 c = K_S_HOME;
1871 mod_mask = 0;
1872 }
1873 else if (mod_mask == MOD_MASK_CTRL)
1874 {
1875 c = K_C_HOME;
1876 mod_mask = 0;
1877 }
1878 else
1879 c = K_HOME;
1880 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001881 case K_XEND:
1882 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001883 {
1884 c = K_S_END;
1885 mod_mask = 0;
1886 }
1887 else if (mod_mask == MOD_MASK_CTRL)
1888 {
1889 c = K_C_END;
1890 mod_mask = 0;
1891 }
1892 else
1893 c = K_END;
1894 break;
1895
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001896 case K_XUP: c = K_UP; break;
1897 case K_XDOWN: c = K_DOWN; break;
1898 case K_XLEFT: c = K_LEFT; break;
1899 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001902 // For a multi-byte character get all the bytes and return the
1903 // converted character.
1904 // Note: This will loop until enough bytes are received!
1905 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1906 {
1907 ++no_mapping;
1908 buf[0] = c;
1909 for (i = 1; i < n; ++i)
1910 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001911 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001912 if (buf[i] == K_SPECIAL
1913#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001914 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001915#endif
1916 )
1917 {
1918 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1919 // sequence, which represents a K_SPECIAL (0x80),
1920 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1921 // represents a CSI (0x9B),
1922 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1923 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001924 c = vgetorpeek(TRUE);
1925 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001926 buf[i] = CSI;
1927 }
1928 }
1929 --no_mapping;
1930 c = (*mb_ptr2char)(buf);
1931 }
1932
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001933 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001934 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001935 vgetc_mod_mask = mod_mask;
1936 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001937 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001938
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001939 break;
1940 }
zeertzjq81b46a62022-04-09 17:58:49 +01001941
1942 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001944
1945#ifdef FEAT_EVAL
1946 /*
1947 * In the main loop "may_garbage_collect" can be set to do garbage
1948 * collection in the first next vgetc(). It's disabled after that to
1949 * avoid internally used Lists and Dicts to be freed.
1950 */
1951 may_garbage_collect = FALSE;
1952#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001953
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001954#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001955 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001956 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001957 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001958 bevalexpr_due_set = FALSE;
1959 ui_remove_balloon();
1960 }
1961#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001962#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001963 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1964 // are filtered.
1965 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001966 {
1967 if (c == Ctrl_C)
1968 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001969 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001970 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001971#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001972
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001973 // Need to process the character before we know it's safe to do something
1974 // else.
1975 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001976 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001977
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001978 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979}
1980
1981/*
1982 * Like vgetc(), but never return a NUL when called recursively, get a key
1983 * directly from the user (ignoring typeahead).
1984 */
1985 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001986safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 int c;
1989
1990 c = vgetc();
1991 if (c == NUL)
1992 c = get_keystroke();
1993 return c;
1994}
1995
1996/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001997 * Like safe_vgetc(), but loop to handle K_IGNORE.
1998 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01001999 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002000 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002001 static int
2002plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002003{
2004 int c;
2005
2006 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002007 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002008 while (c == K_IGNORE
2009 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2010 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002011 return c;
2012}
2013
2014/*
2015 * Like safe_vgetc(), but loop to handle K_IGNORE.
2016 * Also ignore scrollbar events.
2017 */
2018 int
2019plain_vgetc(void)
2020{
2021 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002022
2023 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002024 // Only handle the first pasted character. Drop the rest, since we
2025 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002026 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2027
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002028 return c;
2029}
2030
2031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 * Check if a character is available, such that vgetc() will not block.
2033 * If the next character is a special character or multi-byte, the returned
2034 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002035 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 */
2037 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002038vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002040 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002042 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043}
2044
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002045#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046/*
2047 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2048 * codes.
2049 */
2050 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002051vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
2053 int c;
2054
2055 ++no_mapping;
2056 ++allow_keys;
2057 c = vpeekc();
2058 --no_mapping;
2059 --allow_keys;
2060 return c;
2061}
2062#endif
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064/*
2065 * Check if any character is available, also half an escape sequence.
2066 * Trick: when no typeahead found, but there is something in the typeahead
2067 * buffer, it must be an ESC that is recognized as the start of a key code.
2068 */
2069 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002070vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071{
2072 int c;
2073
2074 c = vpeekc();
2075 if (c == NUL && typebuf.tb_len > 0)
2076 c = ESC;
2077 return c;
2078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080/*
2081 * Call vpeekc() without causing anything to be mapped.
2082 * Return TRUE if a character is available, FALSE otherwise.
2083 */
2084 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002085char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
2087 int retval;
2088
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002089#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002090 // When test_override("char_avail", 1) was called pretend there is no
2091 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002092 if (disable_char_avail_for_testing)
2093 return FALSE;
2094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 ++no_mapping;
2096 retval = vpeekc();
2097 --no_mapping;
2098 return (retval != NUL);
2099}
2100
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002101#if defined(FEAT_EVAL) || defined(PROTO)
2102/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002103 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002104 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002105 static void
2106getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002107{
2108 varnumber_T n;
2109 int error = FALSE;
2110
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002111 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2112 return;
2113
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002114#ifdef MESSAGE_QUEUE
2115 // vpeekc() used to check for messages, but that caused problems, invoking
2116 // a callback where it was not expected. Some plugins use getchar(1) in a
2117 // loop to await a message, therefore make sure we check for messages here.
2118 parse_queued_messages();
2119#endif
2120
Bram Moolenaar30613902019-12-01 22:11:18 +01002121 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002122 windgoto(msg_row, msg_col);
2123
2124 ++no_mapping;
2125 ++allow_keys;
2126 for (;;)
2127 {
2128 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002129 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002130 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002131 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002132 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002133 n = vpeekc_any();
2134 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002135 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002136 n = 0;
2137 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002138 // getchar(0) and char avail() != NUL: get a character.
2139 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2140 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002141
Bram Moolenaar15183b42020-09-05 19:59:39 +02002142 if (n == K_IGNORE || n == K_MOUSEMOVE
2143 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002144 continue;
2145 break;
2146 }
2147 --no_mapping;
2148 --allow_keys;
2149
2150 set_vim_var_nr(VV_MOUSE_WIN, 0);
2151 set_vim_var_nr(VV_MOUSE_WINID, 0);
2152 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2153 set_vim_var_nr(VV_MOUSE_COL, 0);
2154
2155 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002156 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002157 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002158 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002159 int i = 0;
2160
Bram Moolenaar30613902019-12-01 22:11:18 +01002161 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002162 if (mod_mask != 0)
2163 {
2164 temp[i++] = K_SPECIAL;
2165 temp[i++] = KS_MODIFIER;
2166 temp[i++] = mod_mask;
2167 }
2168 if (IS_SPECIAL(n))
2169 {
2170 temp[i++] = K_SPECIAL;
2171 temp[i++] = K_SECOND(n);
2172 temp[i++] = K_THIRD(n);
2173 }
2174 else if (has_mbyte)
2175 i += (*mb_char2bytes)(n, temp + i);
2176 else
2177 temp[i++] = n;
2178 temp[i++] = NUL;
2179 rettv->v_type = VAR_STRING;
2180 rettv->vval.v_string = vim_strsave(temp);
2181
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002182 if (is_mouse_key(n))
2183 {
2184 int row = mouse_row;
2185 int col = mouse_col;
2186 win_T *win;
2187 linenr_T lnum;
2188 win_T *wp;
2189 int winnr = 1;
2190
2191 if (row >= 0 && col >= 0)
2192 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002193 // Find the window at the mouse coordinates and compute the
2194 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002195 win = mouse_find_win(&row, &col, FIND_POPUP);
2196 if (win == NULL)
2197 return;
2198 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002199#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002200 if (WIN_IS_POPUP(win))
2201 winnr = 0;
2202 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002203#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002204 for (wp = firstwin; wp != win && wp != NULL;
2205 wp = wp->w_next)
2206 ++winnr;
2207 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2208 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2209 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2210 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2211 }
2212 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002213 }
2214}
2215
2216/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002217 * "getchar()" function
2218 */
2219 void
2220f_getchar(typval_T *argvars, typval_T *rettv)
2221{
2222 getchar_common(argvars, rettv);
2223}
2224
2225/*
2226 * "getcharstr()" function
2227 */
2228 void
2229f_getcharstr(typval_T *argvars, typval_T *rettv)
2230{
2231 getchar_common(argvars, rettv);
2232
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002233 if (rettv->v_type != VAR_NUMBER)
2234 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002235
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002236 char_u temp[7]; // mbyte-char: 6, NUL: 1
2237 varnumber_T n = rettv->vval.v_number;
2238 int i = 0;
2239
2240 if (n != 0)
2241 {
2242 if (has_mbyte)
2243 i += (*mb_char2bytes)(n, temp + i);
2244 else
2245 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002246 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002247 temp[i++] = NUL;
2248 rettv->v_type = VAR_STRING;
2249 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002250}
2251
2252/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002253 * "getcharmod()" function
2254 */
2255 void
2256f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2257{
2258 rettv->vval.v_number = mod_mask;
2259}
2260#endif // FEAT_EVAL
2261
2262#if defined(MESSAGE_QUEUE) || defined(PROTO)
2263# define MAX_REPEAT_PARSE 8
2264
2265/*
2266 * Process messages that have been queued for netbeans or clientserver.
2267 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002268 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002269 * it is safe to do so.
2270 */
2271 void
2272parse_queued_messages(void)
2273{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002274 int old_curwin_id;
2275 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002276 int i;
2277 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002278 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002279 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002280
2281 // Do not handle messages while redrawing, because it may cause buffers to
2282 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002283 // Also bail out when parsing messages was explicitly disabled.
2284 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002285 return;
2286
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002287 // If memory allocation fails during startup we'll exit but curbuf or
2288 // curwin could be NULL.
2289 if (curbuf == NULL || curwin == NULL)
2290 return;
2291
2292 old_curbuf_fnum = curbuf->b_fnum;
2293 old_curwin_id = curwin->w_id;
2294
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002295 ++entered;
2296
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002297 // may_garbage_collect is set in main_loop() to do garbage collection when
2298 // blocking to wait on a character. We don't want that while parsing
2299 // messages, a callback may invoke vgetc() while lists and dicts are in use
2300 // in the call stack.
2301 may_garbage_collect = FALSE;
2302
2303 // Loop when a job ended, but don't keep looping forever.
2304 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2305 {
2306 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002307# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002308 channel_handle_events(FALSE);
2309# endif
2310
2311# ifdef FEAT_NETBEANS_INTG
2312 // Process the queued netbeans messages.
2313 netbeans_parse_messages();
2314# endif
2315# ifdef FEAT_JOB_CHANNEL
2316 // Write any buffer lines still to be written.
2317 channel_write_any_lines();
2318
2319 // Process the messages queued on channels.
2320 channel_parse_messages();
2321# endif
2322# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2323 // Process the queued clientserver messages.
2324 server_parse_messages();
2325# endif
2326# ifdef FEAT_JOB_CHANNEL
2327 // Check if any jobs have ended. If so, repeat the above to handle
2328 // changes, e.g. stdin may have been closed.
2329 if (job_check_ended())
2330 continue;
2331# endif
2332# ifdef FEAT_TERMINAL
2333 free_unused_terminals();
2334# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002335
2336# ifdef FEAT_SOUND_MACOSX
2337 process_cfrunloop();
2338# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002339# ifdef FEAT_SOUND_CANBERRA
2340 if (has_sound_callback_in_queue())
2341 invoke_sound_callback();
2342# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002343#ifdef SIGUSR1
2344 if (got_sigusr1)
2345 {
2346 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2347 got_sigusr1 = FALSE;
2348 }
2349#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002350 break;
2351 }
2352
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002353 // When not nested we'll go back to waiting for a typed character. If it
2354 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002355 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002356 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002357
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002358 may_garbage_collect = save_may_garbage_collect;
2359
2360 // If the current window or buffer changed we need to bail out of the
2361 // waiting loop. E.g. when a job exit callback closes the terminal window.
2362 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002363 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002364
2365 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002366}
2367#endif
2368
2369
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002370typedef enum {
2371 map_result_fail, // failed, break loop
2372 map_result_get, // get a character from typeahead
2373 map_result_retry, // try to map again
2374 map_result_nomatch // no matching mapping, get char
2375} map_result_T;
2376
2377/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002378 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002379 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002380 */
2381 static int
zeertzjqee446032022-04-30 15:02:22 +01002382at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002383{
2384 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2385 int c = *p;
2386
2387 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002388 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002389 && p[1] == KS_MODIFIER
2390 && (p[2] & MOD_MASK_CTRL))
2391 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002392 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2393 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002394}
2395
2396/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002397 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002398 * into just a key, apply that.
2399 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2400 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002401 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002402 */
2403 static int
2404check_simplify_modifier(int max_offset)
2405{
2406 int offset;
2407 char_u *tp;
2408
2409 for (offset = 0; offset < max_offset; ++offset)
2410 {
2411 if (offset + 3 >= typebuf.tb_len)
2412 break;
2413 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002414 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002415 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002416 // A modifier was not used for a mapping, apply it to ASCII keys.
2417 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002418 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002419 int c = tp[3];
2420 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002421
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002422 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002423 {
2424 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002425 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002426
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002427 if (offset == 0)
2428 {
2429 // At the start: remember the character and mod_mask before
2430 // merging, in some cases, e.g. at the hit-return prompt,
2431 // they are put back in the typeahead buffer.
2432 vgetc_char = c;
2433 vgetc_mod_mask = tp[2];
2434 }
zeertzjq12e21e32022-04-27 11:58:01 +01002435 if (IS_SPECIAL(new_c))
2436 {
2437 new_string[0] = K_SPECIAL;
2438 new_string[1] = K_SECOND(new_c);
2439 new_string[2] = K_THIRD(new_c);
2440 len = 3;
2441 }
2442 else
2443 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002444 if (modifier == 0)
2445 {
2446 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002447 NULL, 0, NULL) == FAIL)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002448 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002449 }
2450 else
2451 {
2452 tp[2] = modifier;
2453 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002454 NULL, 0, NULL) == FAIL)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002455 return -1;
2456 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002457 return len;
2458 }
2459 }
2460 }
2461 return 0;
2462}
2463
2464/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002465 * Return TRUE if the terminal sends modifiers with various keys. This is when
2466 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2467 * enabled.
2468 */
2469 static int
2470key_protocol_enabled(void)
2471{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002472 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2473 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2474 ? modify_otherkeys_state == MOKS_ENABLED
2475 : seenModifyOtherKeys;
2476 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002477}
2478
2479/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002480 * Handle mappings in the typeahead buffer.
2481 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002482 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002483 * - When there is no match yet, return map_result_nomatch, need to get more
2484 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002485 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002486 */
2487 static int
2488handle_mapping(
2489 int *keylenp,
2490 int *timedout,
2491 int *mapdepth)
2492{
2493 mapblock_T *mp = NULL;
2494 mapblock_T *mp2;
2495 mapblock_T *mp_match;
2496 int mp_match_len = 0;
2497 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002498 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002499 int tb_c1;
2500 int mlen;
2501#ifdef FEAT_LANGMAP
2502 int nolmaplen;
2503#endif
2504 int keylen = *keylenp;
2505 int i;
2506 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002507 int is_plug_map = FALSE;
2508
zeertzjqbb404f52022-07-23 06:25:29 +01002509 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002510 if (typebuf.tb_len >= 3
2511 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002512 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2513 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2514 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002515
2516 /*
2517 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002518 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002519 *
2520 * Don't look for mappings if:
2521 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2522 * - maphash_valid not set: no mappings present.
2523 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2524 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002525 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002526 * - waiting for a char with --more--
2527 * - in Ctrl-X mode, and we get a valid char for that mode
2528 */
2529 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2530 if (no_mapping == 0 && is_maphash_valid()
2531 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002532 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002533 || (p_remap
2534 && (typebuf.tb_noremap[typebuf.tb_off]
2535 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002536 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2537 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2538 && State != MODE_ASKMORE
2539 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002540 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002541 {
Christopher Plewright03193062022-11-22 12:58:27 +00002542#ifdef FEAT_GUI
2543 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2544 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002545 {
2546 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2547 // K_SPECIAL KS_MODIFIER {flags}.
2548 tb_c1 = K_SPECIAL;
2549 }
2550#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002551#ifdef FEAT_LANGMAP
2552 if (tb_c1 == K_SPECIAL)
2553 nolmaplen = 2;
2554 else
2555 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002556 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2557 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002558 nolmaplen = 0;
2559 }
2560#endif
2561 // First try buffer-local mappings.
2562 mp = get_buf_maphash_list(local_State, tb_c1);
2563 mp2 = get_maphash_list(local_State, tb_c1);
2564 if (mp == NULL)
2565 {
2566 // There are no buffer-local mappings.
2567 mp = mp2;
2568 mp2 = NULL;
2569 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002570
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002571 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002572 * Loop until a partly matching mapping is found or all (local)
2573 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002575 * A full match is only accepted if there is no partly match, so "aa"
2576 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002577 */
2578 mp_match = NULL;
2579 mp_match_len = 0;
2580 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002581 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002583 // Only consider an entry if the first character matches and it is
2584 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585 // Skip ":lmap" mappings if keys were mapped.
2586 if (mp->m_keys[0] == tb_c1
2587 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002588 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002589 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002590 && ((mp->m_mode & MODE_LANGMAP) == 0
2591 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002592 {
2593#ifdef FEAT_LANGMAP
2594 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002595 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002596#endif
2597 // find the match length of this mapping
2598 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2599 {
zeertzjq49660f52022-10-20 17:59:38 +01002600 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002601#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002602 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002603 {
2604 if (nomap == 2 && c2 == KS_MODIFIER)
2605 modifiers = 1;
2606 else if (nomap == 1 && modifiers == 1)
2607 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002608 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002609 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002610 else
zeertzjq49660f52022-10-20 17:59:38 +01002611 {
2612 if (c2 == K_SPECIAL)
2613 nomap = 2;
2614 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2615 // Only apply 'langmap' if merging modifiers into
2616 // the key will not result in another character,
2617 // so that 'langmap' behaves consistently in
2618 // different terminals and GUIs.
2619 LANGMAP_ADJUST(c2, TRUE);
2620 modifiers = 0;
2621 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002622#endif
zeertzjq49660f52022-10-20 17:59:38 +01002623 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002624 break;
2625 }
2626
Bram Moolenaareda35f72019-08-03 14:59:44 +02002627 // Don't allow mapping the first byte(s) of a multi-byte char.
2628 // Happens when mapping <M-a> and then changing 'encoding'.
2629 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002630 {
2631 char_u *p1 = mp->m_keys;
2632 char_u *p2 = mb_unescape(&p1);
2633
2634 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002635 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002636 mlen = 0;
2637 }
2638
2639 // Check an entry whether it matches.
2640 // - Full match: mlen == keylen
2641 // - Partly match: mlen == typebuf.tb_len
2642 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002643 if (mlen == keylen || (mlen == typebuf.tb_len
2644 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002645 {
2646 char_u *s;
2647 int n;
2648
Bram Moolenaareda35f72019-08-03 14:59:44 +02002649 // If only script-local mappings are allowed, check if the
2650 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002651 s = typebuf.tb_noremap + typebuf.tb_off;
2652 if (*s == RM_SCRIPT
2653 && (mp->m_keys[0] != K_SPECIAL
2654 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002655 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002656 continue;
2657
Bram Moolenaareda35f72019-08-03 14:59:44 +02002658 // If one of the typed keys cannot be remapped, skip the
2659 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002660 for (n = mlen; --n >= 0; )
2661 if (*s++ & (RM_NONE|RM_ABBR))
2662 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002663 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002664 continue;
2665
2666 if (keylen > typebuf.tb_len)
2667 {
2668 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002669 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002670 {
2671 // break at a partly match
2672 keylen = KEYLEN_PART_MAP;
2673 break;
2674 }
2675 }
2676 else if (keylen > mp_match_len)
2677 {
2678 // found a longer match
2679 mp_match = mp;
2680 mp_match_len = keylen;
2681 }
2682 }
2683 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002684 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002685 // character. If the first character that didn't match is
2686 // K_SPECIAL then check for a termcode. This isn't perfect
2687 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002689 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002690 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002691 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2692 }
2693 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2694 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002695 }
2696 }
2697
Bram Moolenaareda35f72019-08-03 14:59:44 +02002698 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002699 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002700 {
2701 mp = mp_match;
2702 keylen = mp_match_len;
2703 }
2704 }
2705
2706 /*
2707 * Check for match with 'pastetoggle'
2708 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002709 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002710 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002711 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2712 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002713 break;
2714 if (p_pt[mlen] == NUL) // match
2715 {
2716 // write chars to script file(s)
2717 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002718 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2719 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002720
2721 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002722 set_option_value_give_err((char_u *)"paste",
2723 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002724 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002725 {
2726 msg_col = 0;
2727 msg_row = Rows - 1;
2728 msg_clr_eos(); // clear ruler
2729 }
2730 status_redraw_all();
2731 redraw_statuslines();
2732 showmode();
2733 setcursor();
2734 *keylenp = keylen;
2735 return map_result_retry;
2736 }
2737 // Need more chars for partly match.
2738 if (mlen == typebuf.tb_len)
2739 keylen = KEYLEN_PART_KEY;
2740 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002741 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002742 max_mlen = mlen + 1;
2743 }
2744
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002745 // May check for a terminal code when there is no mapping or only a partial
2746 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2747 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002748 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002749 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2750 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002751 {
2752 int save_keylen = keylen;
2753
2754 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002755 * When no matching mapping found or found a non-matching mapping that
2756 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002757 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002758 * - mapping is allowed,
2759 * - keys have not been mapped,
2760 * - and not an ESC sequence, not in insert mode or p_ek is on,
2761 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002762 */
zeertzjq68a573c2022-04-28 14:10:01 +01002763 if (no_mapping == 0 || allow_keys != 0)
2764 {
2765 if ((typebuf.tb_maplen == 0
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002766 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002768 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002769 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2770 else
2771 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002772
Bram Moolenaar0684e362020-12-03 19:54:42 +01002773 // If no termcode matched but 'pastetoggle' matched partially
2774 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002775 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002776 keylen = KEYLEN_PART_KEY;
2777
Bram Moolenaar975a8802020-06-06 22:36:24 +02002778 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002779 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002780#ifdef FEAT_TERMINAL
2781 check_no_reduce_keys(); // may update the no_reduce_keys flag
2782#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002783 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002784 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002785 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002786 if (keylen < 0)
2787 // ins_typebuf() failed
2788 return map_result_fail;
2789 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002790
Bram Moolenaareda35f72019-08-03 14:59:44 +02002791 // When getting a partial match, but the last characters were not
2792 // typed, don't wait for a typed character to complete the
2793 // termcode. This helps a lot when a ":normal" command ends in an
2794 // ESC.
2795 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002796 keylen = 0;
2797 }
2798 else
2799 keylen = 0;
2800 if (keylen == 0) // no matching terminal code
2801 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002802#ifdef AMIGA
2803 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002804 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002805 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002806 {
2807 char_u *s;
2808
2809 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002810 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2811 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 ++s)
2813 ;
2814 if (*s == 'r' || *s == '|') // found one
2815 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002816 del_typebuf(
2817 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002818 // get size and redraw screen
2819 shell_resized();
2820 *keylenp = keylen;
2821 return map_result_retry;
2822 }
2823 if (*s == NUL) // need more characters
2824 keylen = KEYLEN_PART_KEY;
2825 }
2826 if (keylen >= 0)
2827#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002828 // When there was a matching mapping and no termcode could be
2829 // replaced after another one, use that mapping (loop around).
2830 // If there was no mapping at all use the character from the
2831 // typeahead buffer right here.
2832 if (mp == NULL)
2833 {
2834 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002835 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002836 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002837 }
2838
2839 if (keylen > 0) // full matching terminal code
2840 {
2841#if defined(FEAT_GUI) && defined(FEAT_MENU)
2842 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002843 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2844 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002845 {
2846 int idx;
2847
Bram Moolenaareda35f72019-08-03 14:59:44 +02002848 // Using a menu may cause a break in undo! It's like using
2849 // gotchars(), but without recording or writing to a script
2850 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002851 may_sync_undo();
2852 del_typebuf(3, 0);
2853 idx = get_menu_index(current_menu, local_State);
2854 if (idx != MENU_INDEX_INVALID)
2855 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002856 // In Select mode and a Visual mode menu is used: Switch
2857 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002858 // back to Select mode.
2859 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002860 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002861 {
2862 VIsual_select = FALSE;
2863 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002864 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865 }
2866 ins_typebuf(current_menu->strings[idx],
2867 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002868 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002869 }
2870 }
2871#endif // FEAT_GUI && FEAT_MENU
2872 *keylenp = keylen;
2873 return map_result_retry; // try mapping again
2874 }
2875
Bram Moolenaareda35f72019-08-03 14:59:44 +02002876 // Partial match: get some more characters. When a matching mapping
2877 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002878 if (mp == NULL || keylen < 0)
2879 keylen = KEYLEN_PART_KEY;
2880 else
2881 keylen = mp_match_len;
2882 }
2883
2884 /*
2885 * complete match
2886 */
2887 if (keylen >= 0 && keylen <= typebuf.tb_len)
2888 {
2889 char_u *map_str;
2890
2891#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002892 int save_m_expr;
2893 int save_m_noremap;
2894 int save_m_silent;
2895 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002896#else
2897# define save_m_noremap mp->m_noremap
2898# define save_m_silent mp->m_silent
2899#endif
2900
2901 // write chars to script file(s)
2902 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002903 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2904 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002905
2906 cmd_silent = (typebuf.tb_silent > 0);
2907 del_typebuf(keylen, 0); // remove the mapped keys
2908
2909 /*
2910 * Put the replacement string in front of mapstr.
2911 * The depth check catches ":map x y" and ":map y x".
2912 */
2913 if (++*mapdepth >= p_mmd)
2914 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002915 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002916 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002917 redrawcmdline();
2918 else
2919 setcursor();
2920 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002921 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002922 *keylenp = keylen;
2923 return map_result_fail;
2924 }
2925
2926 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002927 * In Select mode and a Visual mode mapping is used: Switch to Visual
2928 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002929 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002930 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002931 {
2932 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002933 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002934 }
2935
2936#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002937 // Copy the values from *mp that are used, because evaluating the
2938 // expression may invoke a function that redefines the mapping, thereby
2939 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002940 save_m_expr = mp->m_expr;
2941 save_m_noremap = mp->m_noremap;
2942 save_m_silent = mp->m_silent;
2943 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002944
2945 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002946 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2947 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002948 */
2949 if (mp->m_expr)
2950 {
2951 int save_vgetc_busy = vgetc_busy;
2952 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002953 int was_screen_col = screen_cur_col;
2954 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002955 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002956
2957 vgetc_busy = 0;
2958 may_garbage_collect = FALSE;
2959
2960 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002961 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002962
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002963 // The mapping may do anything, but we expect it to take care of
2964 // redrawing. Do put the cursor back where it was.
2965 windgoto(was_screen_row, was_screen_col);
2966 out_flush();
2967
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002968 // If an error was displayed and the expression returns an empty
2969 // string, generate a <Nop> to allow for a redraw.
2970 if (prev_did_emsg != did_emsg
2971 && (map_str == NULL || *map_str == NUL))
2972 {
2973 char_u buf[4];
2974
2975 vim_free(map_str);
2976 buf[0] = K_SPECIAL;
2977 buf[1] = KS_EXTRA;
2978 buf[2] = KE_IGNORE;
2979 buf[3] = NUL;
2980 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01002981 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002982 {
2983 // redraw the command below the error
2984 msg_didout = TRUE;
2985 if (msg_row < cmdline_row)
2986 msg_row = cmdline_row;
2987 redrawcmd();
2988 }
2989 }
2990
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002991 vgetc_busy = save_vgetc_busy;
2992 may_garbage_collect = save_may_garbage_collect;
2993 }
2994 else
2995#endif
2996 map_str = mp->m_str;
2997
2998 /*
2999 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003000 * If 'from' field is the same as the start of the 'to' field, don't
3001 * remap the first character (but do allow abbreviations).
3002 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003003 */
3004 if (map_str == NULL)
3005 i = FAIL;
3006 else
3007 {
3008 int noremap;
3009
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003010#ifdef FEAT_EVAL
3011 last_used_map = mp;
3012 last_used_sid = -1;
3013#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003014 if (save_m_noremap != REMAP_YES)
3015 noremap = save_m_noremap;
3016 else if (
3017#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003018 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3019 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003020#else
3021 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3022#endif
3023 != 0)
3024 noremap = REMAP_YES;
3025 else
3026 noremap = REMAP_SKIP;
3027 i = ins_typebuf(map_str, noremap,
3028 0, TRUE, cmd_silent || save_m_silent);
3029#ifdef FEAT_EVAL
3030 if (save_m_expr)
3031 vim_free(map_str);
3032#endif
3033 }
3034#ifdef FEAT_EVAL
3035 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003036#endif
3037 *keylenp = keylen;
3038 if (i == FAIL)
3039 return map_result_fail;
3040 return map_result_retry;
3041 }
3042
3043 *keylenp = keylen;
3044 return map_result_nomatch;
3045}
3046
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003047/*
3048 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003049 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003050 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003053vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 old_char = c;
3056 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003057 old_mouse_row = mouse_row;
3058 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003059 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060}
3061
3062/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003063 * When peeking and not getting a character, reg_executing cannot be cleared
3064 * yet, so set a flag to clear it later.
3065 */
3066 static void
3067check_end_reg_executing(int advance)
3068{
3069 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3070 || pending_end_reg_executing))
3071 {
3072 if (advance)
3073 {
3074 reg_executing = 0;
3075 pending_end_reg_executing = FALSE;
3076 }
3077 else
3078 pending_end_reg_executing = TRUE;
3079 }
3080
3081}
3082
3083/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003084 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * 1. from the stuffbuffer
3086 * This is used for abbreviated commands like "D" -> "d$".
3087 * Also used to redo a command for ".".
3088 * 2. from the typeahead buffer
3089 * Stores text obtained previously but not used yet.
3090 * Also stores the result of mappings.
3091 * Also used for the ":normal" command.
3092 * 3. from the user
3093 * This may do a blocking wait if "advance" is TRUE.
3094 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003095 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003096 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 * KeyTyped is set to TRUE in the case the user typed the key.
3098 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003099 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003100 * Just look whether there is a character available.
3101 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 *
3103 * When "no_mapping" is zero, checks for mappings in the current mode.
3104 * Only returns one byte (of a multi-byte character).
3105 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3106 */
3107 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003108vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int c, c1;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003111 int timedout = FALSE; // waited for more than 'timeoutlen'
3112 // for mapping to complete or
3113 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003114 int mapdepth = 0; // check for recursive mapping
3115 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003118 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003122 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /*
3125 * This function doesn't work very well when called recursively. This may
3126 * happen though, because of:
3127 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3128 * there is a character available, which calls this function. In that
3129 * case we must return NUL, to indicate no character is available.
3130 * 2. A GUI callback function writes to the screen, causing a
3131 * wait_return().
3132 * Using ":normal" can also do this, but it saves the typeahead buffer,
3133 * thus it should be OK. But don't get a key from the user then.
3134 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003135 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 return NUL;
3137
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003138 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003143 typebuf_was_empty = FALSE;
3144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 init_typebuf();
3147 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003148 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 do
3150 {
3151/*
3152 * get a character: 1. from the stuffbuffer
3153 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003154 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
3156 c = typeahead_char;
3157 if (advance)
3158 typeahead_char = 0;
3159 }
3160 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003161 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (c != NUL && !got_int)
3163 {
3164 if (advance)
3165 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003166 // KeyTyped = FALSE; When the command that stuffed something
3167 // was typed, behave like the stuffed command was typed.
3168 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 KeyStuffed = TRUE;
3170 }
3171 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003172 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174 else
3175 {
3176 /*
3177 * Loop until we either find a matching mapped key, or we
3178 * are sure that it is not a mapped key.
3179 * If a mapped key sequence is found we go back to the start to
3180 * try re-mapping.
3181 */
3182 for (;;)
3183 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003184 long wait_time;
3185 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003186 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003187 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 /*
3189 * ui_breakcheck() is slow, don't use it too often when
3190 * inside a mapping. But call it each time for typed
3191 * characters.
3192 */
3193 if (typebuf.tb_maplen)
3194 line_breakcheck();
3195 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003196 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (got_int)
3198 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003199 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003200 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 /*
3203 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003204 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 * Otherwise we behave like having gotten a CTRL-C.
3206 * As a result typing CTRL-C in insert mode will
3207 * really insert a CTRL-C.
3208 */
3209 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003210 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 c = ESC;
3212 else
3213 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003214 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003216 if (advance)
3217 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003218 // Also record this character, it might be needed to
3219 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003220 *typebuf.tb_buf = c;
3221 gotchars(typebuf.tb_buf, 1);
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 cmd_silent = FALSE;
3224
3225 break;
3226 }
3227 else if (typebuf.tb_len > 0)
3228 {
3229 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003230 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003232 map_result_T result = handle_mapping(
3233 &keylen, &timedout, &mapdepth);
3234
3235 if (result == map_result_retry)
3236 // try mapping again
3237 continue;
3238 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003239 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003240 // failed, use the outer loop
3241 c = -1;
3242 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003244 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
3247 * get a character: 2. from the typeahead buffer
3248 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003249 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003250 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003251 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003252 cmd_silent = (typebuf.tb_silent > 0);
3253 if (typebuf.tb_maplen > 0)
3254 KeyTyped = FALSE;
3255 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003256 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003257 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003258 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003259 gotchars(typebuf.tb_buf
3260 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003261 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003262 KeyNoremap = typebuf.tb_noremap[
3263 typebuf.tb_off];
3264 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003265 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003266 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003269 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271
3272/*
3273 * get a character: 3. from the user - handle <Esc> in Insert mode
3274 */
3275 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003276 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003278 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 * typing an <ESC>. If we get something after all, we may
3280 * have to redisplay the mode. That the cursor is in the wrong
3281 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003282 * Do not do this if the kitty keyboard protocol is used, every
3283 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 */
3285 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 new_wcol = curwin->w_wcol;
3287 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if ( advance
3289 && typebuf.tb_len == 1
3290 && typebuf.tb_buf[typebuf.tb_off] == ESC
3291 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003292 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003295 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003296 && (p_timeout
3297 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003299 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003301 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 char_u *ptr;
3303
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003304 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 unshowmode(TRUE);
3307 mode_deleted = TRUE;
3308 }
3309#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003310 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003311 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 int save_State;
3314
3315 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003316 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 gui_update_cursor(TRUE, FALSE);
3318 State = save_State;
3319 shape_changed = TRUE;
3320 }
3321#endif
3322 validate_cursor();
3323 old_wcol = curwin->w_wcol;
3324 old_wrow = curwin->w_wrow;
3325
Bram Moolenaar30613902019-12-01 22:11:18 +01003326 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (curwin->w_cursor.col != 0)
3328 {
3329 if (curwin->w_wcol > 0)
3330 {
3331 if (did_ai)
3332 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003333 chartabsize_T cts;
3334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 /*
3336 * We are expecting to truncate the trailing
3337 * white-space, so find the last non-white
3338 * character -- webb
3339 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003340 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003342 init_chartabsize_arg(&cts, curwin,
3343 curwin->w_cursor.lnum, 0, ptr, ptr);
3344 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003346 if (!VIM_ISWHITE(*cts.cts_ptr))
3347 curwin->w_wcol = cts.cts_vcol;
3348 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003350 cts.cts_ptr +=
3351 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003353 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003355 clear_chartabsize_arg(&cts);
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003358 + curwin->w_wcol / curwin->w_width;
3359 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003361 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363 else
3364 {
3365 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 }
3369 else if (curwin->w_p_wrap && curwin->w_wrow)
3370 {
3371 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003372 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3376 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003377 // Correct when the cursor is on the right halve
3378 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 ptr = ml_get_curline();
3380 col -= (*mb_head_off)(ptr, ptr + col);
3381 if ((*mb_ptr2cells)(ptr + col) > 1)
3382 --curwin->w_wcol;
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 setcursor();
3386 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 new_wcol = curwin->w_wcol;
3388 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 curwin->w_wcol = old_wcol;
3390 curwin->w_wrow = old_wrow;
3391 }
3392 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003393 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003394
Bram Moolenaar30613902019-12-01 22:11:18 +01003395 // Allow mapping for just typed characters. When we get here c
3396 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003397 for (n = 1; n <= c; ++n)
3398 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 typebuf.tb_len += c;
3400
Bram Moolenaar30613902019-12-01 22:11:18 +01003401 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3403 {
3404 timedout = TRUE;
3405 continue;
3406 }
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (ex_normal_busy > 0)
3409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar30613902019-12-01 22:11:18 +01003412 // No typeahead left and inside ":normal". Must return
3413 // something to avoid getting stuck. When an incomplete
3414 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (typebuf.tb_len > 0)
3416 {
3417 timedout = TRUE;
3418 continue;
3419 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003420
Bram Moolenaar30613902019-12-01 22:11:18 +01003421 // When 'insertmode' is set, ESC just beeps in Insert
3422 // mode. Use CTRL-L to make edit() return.
3423 // For the command line only CTRL-C always breaks it.
3424 // For the cmdline window: Alternate between ESC and
3425 // CTRL-C: ESC for most situations and CTRL-C to close the
3426 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003427 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003429#ifdef FEAT_TERMINAL
3430 else if (terminal_is_active())
3431 c = K_CANCEL;
3432#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003433 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003434 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 c = Ctrl_C;
3436 else
3437 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003439 // set a flag to indicate this wasn't a normal char
3440 if (advance)
3441 typebuf_was_empty = TRUE;
3442
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003443 // return from main_loop()
3444 if (pending_exmode_active)
3445 exmode_active = EXMODE_NORMAL;
3446
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003447 // no chars to block abbreviation for
3448 typebuf.tb_no_abbr_cnt = 0;
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 break;
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453/*
3454 * get a character: 3. from the user - update display
3455 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003456 // In insert mode a screen update is skipped when characters
3457 // are still available. But when those available characters
3458 // are part of a mapping, and we are going to do a blocking
3459 // wait here. Need to update the screen to display the
3460 // changed text so far. Also for when 'lazyredraw' is set and
3461 // redrawing was postponed because there was something in the
3462 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003463 if (((State & MODE_INSERT) != 0 || p_lz)
3464 && (State & MODE_CMDLINE) == 0
3465 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003468 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470
3471 /*
3472 * If we have a partial match (and are going to wait for more
3473 * input from the user), show the partially matched characters
3474 * to the user with showcmd.
3475 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003476 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 c1 = 0;
3478 if (typebuf.tb_len > 0 && advance && !exmode_active)
3479 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003480 if (((State & (MODE_NORMAL | MODE_INSERT))
3481 || State == MODE_LANGMAP)
3482 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003484 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003485 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3487 + typebuf.tb_len - 1) == 1)
3488 {
3489 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3490 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003491 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 c1 = 1;
3493 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003494 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 old_wcol = curwin->w_wcol;
3496 old_wrow = curwin->w_wrow;
3497 curwin->w_wcol = new_wcol;
3498 curwin->w_wrow = new_wrow;
3499 push_showcmd();
3500 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003501 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3502 while (showcmd_idx < typebuf.tb_len)
3503 (void)add_to_showcmd(
3504 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 curwin->w_wcol = old_wcol;
3506 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508
Bram Moolenaar30613902019-12-01 22:11:18 +01003509 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003510 if ((State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3512 && cmdline_star == 0
3513#endif
3514 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3515 + typebuf.tb_len - 1) == 1)
3516 {
3517 putcmdline(typebuf.tb_buf[typebuf.tb_off
3518 + typebuf.tb_len - 1], FALSE);
3519 c1 = 1;
3520 }
3521 }
3522
3523/*
3524 * get a character: 3. from the user - get it
3525 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003526 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003527 // timedout may have been set if a mapping with empty RHS
3528 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003529 timedout = FALSE;
3530
Bram Moolenaar652de232019-04-04 20:13:09 +02003531 if (advance)
3532 {
3533 if (typebuf.tb_len == 0
3534 || !(p_timeout
3535 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3536 // blocking wait
3537 wait_time = -1L;
3538 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3539 wait_time = p_ttm;
3540 else
3541 wait_time = p_tm;
3542 }
3543 else
3544 wait_time = 0;
3545
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003546 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3548 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003549 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
Bram Moolenaareda35f72019-08-03 14:59:44 +02003551 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 pop_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (c1 == 1)
3554 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003555 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 edit_unputchar();
Bram Moolenaar24959102022-05-07 20:01:16 +01003557 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003559 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003560 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
3563 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003564 continue; // end of input script reached
3565 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
3567 if (!advance)
3568 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003569 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 timedout = TRUE;
3572 continue;
3573 }
3574 }
3575 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003576 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 while (typebuf.tb_buf[typebuf.tb_off
3578 + typebuf.tb_len] != NUL)
3579 typebuf.tb_noremap[typebuf.tb_off
3580 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003581#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003582 // Get IM status right after getting keys, not after the
3583 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 vgetc_im_active = im_get_status();
3585#endif
3586 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003587 } // for (;;)
3588 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar30613902019-12-01 22:11:18 +01003590 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003591 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 /*
3594 * The "INSERT" message is taken care of here:
3595 * if we return an ESC to exit insert mode, the message is deleted
3596 * if we don't return an ESC but deleted the message before, redisplay it
3597 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003598 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003600 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003603 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 else
3605 unshowmode(FALSE);
3606 }
3607 else if (c != ESC && mode_deleted)
3608 {
3609 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003610 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 else
3612 showmode();
3613 }
3614 }
3615#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003616 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003617 if (gui.in_use && shape_changed)
3618 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003620 if (timedout && c == ESC)
3621 {
3622 char_u nop_buf[3];
3623
3624 // When recording there will be no timeout. Add a <Nop> after the ESC
3625 // to avoid that it forms a key code with following characters.
3626 nop_buf[0] = K_SPECIAL;
3627 nop_buf[1] = KS_EXTRA;
3628 nop_buf[2] = KE_NOP;
3629 gotchars(nop_buf, 3);
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003632 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 return c;
3635}
3636
3637/*
3638 * inchar() - get one character from
3639 * 1. a scriptfile
3640 * 2. the keyboard
3641 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003642 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * NUL terminated (buffer length must be 'maxlen' + 1).
3644 * Minimum for "maxlen" is 3!!!!
3645 *
3646 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3647 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3648 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3649 * otherwise.
3650 *
3651 * If we got an interrupt all input is read until none is available.
3652 *
3653 * If wait_time == 0 there is no waiting for the char.
3654 * If wait_time == n we wait for n msec for a character to arrive.
3655 * If wait_time == -1 we wait forever for a character to arrive.
3656 *
3657 * Return the number of obtained characters.
3658 * Return -1 when end of input script reached.
3659 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003660 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003661inchar(
3662 char_u *buf,
3663 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003664 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
Bram Moolenaar30613902019-12-01 22:11:18 +01003666 int len = 0; // init for GCC
3667 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003669 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar30613902019-12-01 22:11:18 +01003671 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003674 out_flush_cursor(FALSE, FALSE);
3675#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3676 if (gui.in_use && postponed_mouseshape)
3677 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678#endif
3679 }
3680
3681 /*
3682 * Don't reset these when at the hit-return prompt, otherwise a endless
3683 * recursive loop may result (write error in swapfile, hit-return, timeout
3684 * on char wait, flush swapfile, write error....).
3685 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003686 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003688 did_outofmem_msg = FALSE; // display out of memory message (again)
3689 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003691 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003694 * Get a character from a script file if there is one.
3695 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003698 while (scriptin[curscript] != NULL && script_char < 0
3699#ifdef FEAT_EVAL
3700 && !ignore_script
3701#endif
3702 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003704#ifdef MESSAGE_QUEUE
3705 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003706#endif
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3709 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003710 // Reached EOF.
3711 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3712 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 closescript();
3714 /*
3715 * When reading script file is interrupted, return an ESC to get
3716 * back to normal mode.
3717 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3718 */
3719 if (got_int)
3720 retesc = TRUE;
3721 else
3722 return -1;
3723 }
3724 else
3725 {
3726 buf[0] = script_char;
3727 len = 1;
3728 }
3729 }
3730
Bram Moolenaar30613902019-12-01 22:11:18 +01003731 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
3733 /*
3734 * If we got an interrupt, skip all previously typed characters and
3735 * return TRUE if quit reading script file.
3736 * Stop reading typeahead when a single CTRL-C was read,
3737 * fill_input_buf() returns this when not able to read from stdin.
3738 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3739 * and buf may be pointing inside typebuf.tb_buf[].
3740 */
3741 if (got_int)
3742 {
kylo252ae6f1d82022-02-16 19:24:07 +00003743#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 char_u dum[DUM_LEN + 1];
3745
3746 for (;;)
3747 {
3748 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003749 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 break;
3751 }
3752 return retesc;
3753 }
3754
3755 /*
3756 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003757 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003759 if (wait_time == -1L || wait_time > 10L)
3760 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 /*
3763 * Fill up to a third of the buffer, because each character may be
3764 * tripled below.
3765 */
3766 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3767 }
3768
Bram Moolenaar30613902019-12-01 22:11:18 +01003769 // If the typebuf was changed further down, it is like nothing was added by
3770 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (typebuf_changed(tb_change_cnt))
3772 return 0;
3773
Bram Moolenaar30613902019-12-01 22:11:18 +01003774 // Note the change in the typeahead buffer, this matters for when
3775 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3776 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003777 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3778 typebuf.tb_change_cnt = 1;
3779
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003780 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781}
3782
3783/*
3784 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003785 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 * Returns the new length.
3787 */
3788 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003789fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
3791 int i;
3792 char_u *p = buf;
3793
3794 /*
3795 * Two characters are special: NUL and K_SPECIAL.
3796 * When compiled With the GUI CSI is also special.
3797 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3798 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3799 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
3801 for (i = len; --i >= 0; ++p)
3802 {
3803#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003804 // When the GUI is used any character can come after a CSI, don't
3805 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (gui.in_use && p[0] == CSI && i >= 2)
3807 {
3808 p += 2;
3809 i -= 2;
3810 }
zeertzjq646bb722022-02-16 17:51:47 +00003811# ifndef MSWIN
3812 // When not on MS-Windows and the GUI is not used CSI needs to be
3813 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 else if (!gui.in_use && p[0] == CSI)
3815 {
3816 mch_memmove(p + 3, p + 1, (size_t)i);
3817 *p++ = K_SPECIAL;
3818 *p++ = KS_EXTRA;
3819 *p = (int)KE_CSI;
3820 len += 2;
3821 }
zeertzjq646bb722022-02-16 17:51:47 +00003822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 else
3824#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003825 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003826 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003827 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003828#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003829 // Win32 console passes modifiers
3830 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003831# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003832 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003833# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003834 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#endif
3836 ))
3837 {
3838 mch_memmove(p + 3, p + 1, (size_t)i);
3839 p[2] = K_THIRD(p[0]);
3840 p[1] = K_SECOND(p[0]);
3841 p[0] = K_SPECIAL;
3842 p += 2;
3843 len += 2;
3844 }
3845 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003846 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return len;
3848}
3849
3850#if defined(USE_INPUT_BUF) || defined(PROTO)
3851/*
3852 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3853 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003854 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003855 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 */
3857 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003858input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003861# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3862 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863# endif
3864 );
3865}
3866#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003867
3868/*
3869 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3870 * typeahead.
3871 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003872 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003873getcmdkeycmd(
3874 int promptc UNUSED,
3875 void *cookie UNUSED,
3876 int indent UNUSED,
3877 getline_opt_T do_concat UNUSED)
3878{
3879 garray_T line_ga;
3880 int c1 = -1;
3881 int c2;
3882 int cmod = 0;
3883 int aborted = FALSE;
3884
3885 ga_init2(&line_ga, 1, 32);
3886
3887 // no mapping for these characters
3888 no_mapping++;
3889
3890 got_int = FALSE;
3891 while (c1 != NUL && !aborted)
3892 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003893 if (ga_grow(&line_ga, 32) != OK)
3894 {
3895 aborted = TRUE;
3896 break;
3897 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003898
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003899 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003900 {
3901 // incomplete <Cmd> is an error, because there is not much the user
3902 // could do in this state.
3903 emsg(_(e_cmd_mapping_must_end_with_cr));
3904 aborted = TRUE;
3905 break;
3906 }
3907
3908 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003909 c1 = vgetorpeek(TRUE);
3910
Bram Moolenaar957cf672020-11-12 14:21:06 +01003911 // Get two extra bytes for special keys
3912 if (c1 == K_SPECIAL)
3913 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003914 c1 = vgetorpeek(TRUE);
3915 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003916 if (c1 == KS_MODIFIER)
3917 {
3918 cmod = c2;
3919 continue;
3920 }
3921 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00003922
3923 // K_ESC is used to avoid ambiguity with the single Esc character
3924 // that might be the start of an escape sequence. Convert it back
3925 // to a single Esc here.
3926 if (c1 == K_ESC)
3927 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01003928 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003929 if (c1 == Ctrl_V)
3930 {
3931 // CTRL-V is followed by octal, hex or other characters, reverses
3932 // what AppendToRedobuffLit() does.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003933 ++no_reduce_keys; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003934 c1 = get_literal(TRUE);
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003935 --no_reduce_keys;
Bram Moolenaar4a441202020-11-28 14:43:26 +01003936 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003937
3938 if (got_int)
3939 aborted = TRUE;
3940 else if (c1 == '\r' || c1 == '\n')
3941 c1 = NUL; // end the line
3942 else if (c1 == ESC)
3943 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003944 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003945 {
3946 // give a nicer error message for this special case
3947 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3948 aborted = TRUE;
3949 }
3950 else if (IS_SPECIAL(c1))
3951 {
3952 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003953 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003954 else
3955 {
dundargocc57b5bc2022-11-02 13:30:51 +00003956 semsg(e_cmd_mapping_must_not_include_str_key,
Bram Moolenaar957cf672020-11-12 14:21:06 +01003957 get_special_key_name(c1, cmod));
3958 aborted = TRUE;
3959 }
3960 }
3961 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003962 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003963
3964 cmod = 0;
3965 }
3966
3967 no_mapping--;
3968
3969 if (aborted)
3970 ga_clear(&line_ga);
3971
3972 return (char_u *)line_ga.ga_data;
3973}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003974
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003975#if defined(FEAT_EVAL) || defined(PROTO)
3976/*
3977 * If there was a mapping put info about it in the redo buffer, so that "."
3978 * will use the same script context. We only need the SID.
3979 */
3980 void
3981may_add_last_used_map_to_redobuff(void)
3982{
3983 char_u buf[3 + 20];
3984
3985 if (last_used_map == NULL || last_used_map->m_script_ctx.sc_sid < 0)
3986 return;
3987
3988 // <K_SID>{nr};
3989 buf[0] = K_SPECIAL;
3990 buf[1] = KS_EXTRA;
3991 buf[2] = KE_SID;
3992 vim_snprintf((char *)buf + 3, 20, "%d;",
3993 last_used_map->m_script_ctx.sc_sid);
3994 add_buff(&redobuff, buf, -1L);
3995}
3996#endif
3997
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003998 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003999do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004000{
4001 int res;
4002#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004003 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004004
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004005 if (key == K_SCRIPT_COMMAND
4006 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004007 {
4008 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004009 if (last_used_map != NULL)
4010 current_sctx = last_used_map->m_script_ctx;
4011 else
4012 {
4013 current_sctx.sc_sid = last_used_sid;
4014 current_sctx.sc_lnum = 0;
4015 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4016 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004017 }
4018#endif
4019
4020 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4021
4022#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004023 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004024 current_sctx = save_current_sctx;
4025#endif
4026
4027 return res;
4028}
4029
4030#if defined(FEAT_EVAL) || defined(PROTO)
4031 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004032reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004033{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004034 if (last_used_map != mp)
4035 return;
4036
4037 last_used_map = NULL;
4038 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004039}
4040#endif