blob: 1ed2fbf1a2dfda150c7c200df7490abc3b661ff8 [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);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000262 if (len >= slen)
263 {
264 buf->bh_curr->b_str[len - slen] = NUL;
265 buf->bh_space += slen;
266 }
267}
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{
481 if (!block_redo)
482 {
483 free_buff(&old_redobuff);
484 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200485 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 }
487}
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{
496 if (!block_redo)
497 {
498 free_buff(&redobuff);
499 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200500 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100501 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100502 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100503 ;
504 }
505}
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);
523 if (s != NULL)
524 {
525 add_buff(&redobuff, s, -1L);
526 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528}
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{
947 if (typebuf.tb_buf == NULL)
948 {
949 typebuf.tb_buf = typebuf_init;
950 typebuf.tb_noremap = noremapbuf_init;
951 typebuf.tb_buflen = TYPELEN_INIT;
952 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200953 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 typebuf.tb_change_cnt = 1;
955 }
956}
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 *
971 * If noremap is REMAP_YES, new string can be mapped again.
972 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000973 * 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 Moolenaar071d4272004-06-13 20:20:40 +0000975 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
976 * script-local mappings.
977 * If noremap is > 0, that many characters of the new string cannot be mapped.
978 *
979 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
980 * offset is non-zero!).
981 *
982 * If silent is TRUE, cmd_silent is set when the characters are obtained.
983 *
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{
1327 if (reg_recording != 0)
1328 {
1329 delete_buff_tail(&recordbuff, len);
1330 last_recorded_len -= len;
1331 }
1332}
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 Moolenaar32aa1022019-11-02 22:54:41 +01001604 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * or when we have waited some time for a character (c == 0)
1606 *
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
1746 // Get two extra bytes for special keys
1747 if (c == K_SPECIAL
1748#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +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 Moolenaar4f974752019-02-17 17:44:42 +01001768#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001769 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1770 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001771 if (
1772# ifdef VIMDLL
1773 gui.in_use &&
1774# endif
1775 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001777 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001778 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001779
1780 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001781 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001782 {
K.Takata54119102022-02-03 13:33:03 +00001783 name[j] = c;
1784 if (j < 199)
1785 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001786 }
K.Takata54119102022-02-03 13:33:03 +00001787 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001788 gui_make_tearoff(name);
1789 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001792#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001793 // GTK: <F10> normally selects the menu, but it's passed until
1794 // here to allow mapping it. Intercept and invoke the GTK
1795 // behavior if it's not mapped.
1796 if (c == K_F10 && gui.menubar != NULL)
1797 {
1798 gtk_menu_shell_select_first(
1799 GTK_MENU_SHELL(gui.menubar), FALSE);
1800 continue;
1801 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001804 // Handle focus event here, so that the caller doesn't need to
1805 // know about it. Return K_IGNORE so that we loop once (needed
1806 // if 'lazyredraw' is set).
1807 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001808 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001809 ui_focus_change(c == K_FOCUSGAINED);
1810 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001811 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001812
1813 // Translate K_CSI to CSI. The special key is only used to
1814 // avoid it being recognized as the start of a special key.
1815 if (c == K_CSI)
1816 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001818#ifdef FEAT_EVAL
1819 if (c == K_SID)
1820 {
1821 int j;
1822
1823 // Handle <SID>{sid}; Do up to 20 digits for safety.
1824 last_used_sid = 0;
1825 for (j = 0; j < 20 && isdigit(c = vgetorpeek(TRUE)); ++j)
1826 last_used_sid = last_used_sid * 10 + (c - '0');
1827 last_used_map = NULL;
1828 continue;
1829 }
1830#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001831 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001832
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001833 // a keypad or special function key was not mapped, use it like
1834 // its ASCII equivalent
1835 switch (c)
1836 {
1837 case K_KPLUS: c = '+'; break;
1838 case K_KMINUS: c = '-'; break;
1839 case K_KDIVIDE: c = '/'; break;
1840 case K_KMULTIPLY: c = '*'; break;
1841 case K_KENTER: c = CAR; break;
1842 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001843#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001844 // Can be either '.' or a ',',
1845 // depending on the type of keypad.
1846 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001847#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001848 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001849#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001850 case K_K0: c = '0'; break;
1851 case K_K1: c = '1'; break;
1852 case K_K2: c = '2'; break;
1853 case K_K3: c = '3'; break;
1854 case K_K4: c = '4'; break;
1855 case K_K5: c = '5'; break;
1856 case K_K6: c = '6'; break;
1857 case K_K7: c = '7'; break;
1858 case K_K8: c = '8'; break;
1859 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001860
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001861 case K_XHOME:
1862 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001863 {
1864 c = K_S_HOME;
1865 mod_mask = 0;
1866 }
1867 else if (mod_mask == MOD_MASK_CTRL)
1868 {
1869 c = K_C_HOME;
1870 mod_mask = 0;
1871 }
1872 else
1873 c = K_HOME;
1874 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001875 case K_XEND:
1876 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001877 {
1878 c = K_S_END;
1879 mod_mask = 0;
1880 }
1881 else if (mod_mask == MOD_MASK_CTRL)
1882 {
1883 c = K_C_END;
1884 mod_mask = 0;
1885 }
1886 else
1887 c = K_END;
1888 break;
1889
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001890 case K_XUP: c = K_UP; break;
1891 case K_XDOWN: c = K_DOWN; break;
1892 case K_XLEFT: c = K_LEFT; break;
1893 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001896 // For a multi-byte character get all the bytes and return the
1897 // converted character.
1898 // Note: This will loop until enough bytes are received!
1899 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1900 {
1901 ++no_mapping;
1902 buf[0] = c;
1903 for (i = 1; i < n; ++i)
1904 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001905 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001906 if (buf[i] == K_SPECIAL
1907#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001908 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001909#endif
1910 )
1911 {
1912 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1913 // sequence, which represents a K_SPECIAL (0x80),
1914 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1915 // represents a CSI (0x9B),
1916 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1917 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001918 c = vgetorpeek(TRUE);
1919 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001920 buf[i] = CSI;
1921 }
1922 }
1923 --no_mapping;
1924 c = (*mb_ptr2char)(buf);
1925 }
1926
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001927 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001928 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001929 vgetc_mod_mask = mod_mask;
1930 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001931 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001932
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001933 break;
1934 }
zeertzjq81b46a62022-04-09 17:58:49 +01001935
1936 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001938
1939#ifdef FEAT_EVAL
1940 /*
1941 * In the main loop "may_garbage_collect" can be set to do garbage
1942 * collection in the first next vgetc(). It's disabled after that to
1943 * avoid internally used Lists and Dicts to be freed.
1944 */
1945 may_garbage_collect = FALSE;
1946#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001947
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001948#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001949 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001950 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001951 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001952 bevalexpr_due_set = FALSE;
1953 ui_remove_balloon();
1954 }
1955#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001956#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001957 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1958 // are filtered.
1959 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001960 {
1961 if (c == Ctrl_C)
1962 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001963 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001964 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001965#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001966
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001967 // Need to process the character before we know it's safe to do something
1968 // else.
1969 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001970 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001971
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001972 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
1975/*
1976 * Like vgetc(), but never return a NUL when called recursively, get a key
1977 * directly from the user (ignoring typeahead).
1978 */
1979 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001980safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 int c;
1983
1984 c = vgetc();
1985 if (c == NUL)
1986 c = get_keystroke();
1987 return c;
1988}
1989
1990/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001991 * Like safe_vgetc(), but loop to handle K_IGNORE.
1992 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01001993 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001994 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01001995 static int
1996plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001997{
1998 int c;
1999
2000 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002001 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002002 while (c == K_IGNORE
2003 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2004 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002005 return c;
2006}
2007
2008/*
2009 * Like safe_vgetc(), but loop to handle K_IGNORE.
2010 * Also ignore scrollbar events.
2011 */
2012 int
2013plain_vgetc(void)
2014{
2015 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002016
2017 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002018 // Only handle the first pasted character. Drop the rest, since we
2019 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002020 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2021
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002022 return c;
2023}
2024
2025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * Check if a character is available, such that vgetc() will not block.
2027 * If the next character is a special character or multi-byte, the returned
2028 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002029 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
2031 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002032vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002034 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002036 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037}
2038
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002039#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040/*
2041 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2042 * codes.
2043 */
2044 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002045vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 int c;
2048
2049 ++no_mapping;
2050 ++allow_keys;
2051 c = vpeekc();
2052 --no_mapping;
2053 --allow_keys;
2054 return c;
2055}
2056#endif
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058/*
2059 * Check if any character is available, also half an escape sequence.
2060 * Trick: when no typeahead found, but there is something in the typeahead
2061 * buffer, it must be an ESC that is recognized as the start of a key code.
2062 */
2063 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002064vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 int c;
2067
2068 c = vpeekc();
2069 if (c == NUL && typebuf.tb_len > 0)
2070 c = ESC;
2071 return c;
2072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074/*
2075 * Call vpeekc() without causing anything to be mapped.
2076 * Return TRUE if a character is available, FALSE otherwise.
2077 */
2078 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002079char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 int retval;
2082
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002083#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002084 // When test_override("char_avail", 1) was called pretend there is no
2085 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002086 if (disable_char_avail_for_testing)
2087 return FALSE;
2088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 ++no_mapping;
2090 retval = vpeekc();
2091 --no_mapping;
2092 return (retval != NUL);
2093}
2094
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002095#if defined(FEAT_EVAL) || defined(PROTO)
2096/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002097 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002098 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002099 static void
2100getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002101{
2102 varnumber_T n;
2103 int error = FALSE;
2104
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002105 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2106 return;
2107
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002108#ifdef MESSAGE_QUEUE
2109 // vpeekc() used to check for messages, but that caused problems, invoking
2110 // a callback where it was not expected. Some plugins use getchar(1) in a
2111 // loop to await a message, therefore make sure we check for messages here.
2112 parse_queued_messages();
2113#endif
2114
Bram Moolenaar30613902019-12-01 22:11:18 +01002115 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002116 windgoto(msg_row, msg_col);
2117
2118 ++no_mapping;
2119 ++allow_keys;
2120 for (;;)
2121 {
2122 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002123 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002124 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002125 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002126 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002127 n = vpeekc_any();
2128 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002129 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002130 n = 0;
2131 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002132 // getchar(0) and char avail() != NUL: get a character.
2133 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2134 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002135
Bram Moolenaar15183b42020-09-05 19:59:39 +02002136 if (n == K_IGNORE || n == K_MOUSEMOVE
2137 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002138 continue;
2139 break;
2140 }
2141 --no_mapping;
2142 --allow_keys;
2143
2144 set_vim_var_nr(VV_MOUSE_WIN, 0);
2145 set_vim_var_nr(VV_MOUSE_WINID, 0);
2146 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2147 set_vim_var_nr(VV_MOUSE_COL, 0);
2148
2149 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002150 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002151 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002152 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153 int i = 0;
2154
Bram Moolenaar30613902019-12-01 22:11:18 +01002155 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002156 if (mod_mask != 0)
2157 {
2158 temp[i++] = K_SPECIAL;
2159 temp[i++] = KS_MODIFIER;
2160 temp[i++] = mod_mask;
2161 }
2162 if (IS_SPECIAL(n))
2163 {
2164 temp[i++] = K_SPECIAL;
2165 temp[i++] = K_SECOND(n);
2166 temp[i++] = K_THIRD(n);
2167 }
2168 else if (has_mbyte)
2169 i += (*mb_char2bytes)(n, temp + i);
2170 else
2171 temp[i++] = n;
2172 temp[i++] = NUL;
2173 rettv->v_type = VAR_STRING;
2174 rettv->vval.v_string = vim_strsave(temp);
2175
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002176 if (is_mouse_key(n))
2177 {
2178 int row = mouse_row;
2179 int col = mouse_col;
2180 win_T *win;
2181 linenr_T lnum;
2182 win_T *wp;
2183 int winnr = 1;
2184
2185 if (row >= 0 && col >= 0)
2186 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002187 // Find the window at the mouse coordinates and compute the
2188 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002189 win = mouse_find_win(&row, &col, FIND_POPUP);
2190 if (win == NULL)
2191 return;
2192 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002193#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002194 if (WIN_IS_POPUP(win))
2195 winnr = 0;
2196 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002197#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002198 for (wp = firstwin; wp != win && wp != NULL;
2199 wp = wp->w_next)
2200 ++winnr;
2201 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2202 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2203 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2204 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2205 }
2206 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002207 }
2208}
2209
2210/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002211 * "getchar()" function
2212 */
2213 void
2214f_getchar(typval_T *argvars, typval_T *rettv)
2215{
2216 getchar_common(argvars, rettv);
2217}
2218
2219/*
2220 * "getcharstr()" function
2221 */
2222 void
2223f_getcharstr(typval_T *argvars, typval_T *rettv)
2224{
2225 getchar_common(argvars, rettv);
2226
2227 if (rettv->v_type == VAR_NUMBER)
2228 {
2229 char_u temp[7]; // mbyte-char: 6, NUL: 1
2230 varnumber_T n = rettv->vval.v_number;
2231 int i = 0;
2232
2233 if (n != 0)
2234 {
2235 if (has_mbyte)
2236 i += (*mb_char2bytes)(n, temp + i);
2237 else
2238 temp[i++] = n;
2239 }
2240 temp[i++] = NUL;
2241 rettv->v_type = VAR_STRING;
2242 rettv->vval.v_string = vim_strsave(temp);
2243 }
2244}
2245
2246/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002247 * "getcharmod()" function
2248 */
2249 void
2250f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2251{
2252 rettv->vval.v_number = mod_mask;
2253}
2254#endif // FEAT_EVAL
2255
2256#if defined(MESSAGE_QUEUE) || defined(PROTO)
2257# define MAX_REPEAT_PARSE 8
2258
2259/*
2260 * Process messages that have been queued for netbeans or clientserver.
2261 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002262 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002263 * it is safe to do so.
2264 */
2265 void
2266parse_queued_messages(void)
2267{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002268 int old_curwin_id;
2269 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002270 int i;
2271 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002272 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002273 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002274
2275 // Do not handle messages while redrawing, because it may cause buffers to
2276 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002277 // Also bail out when parsing messages was explicitly disabled.
2278 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002279 return;
2280
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002281 // If memory allocation fails during startup we'll exit but curbuf or
2282 // curwin could be NULL.
2283 if (curbuf == NULL || curwin == NULL)
2284 return;
2285
2286 old_curbuf_fnum = curbuf->b_fnum;
2287 old_curwin_id = curwin->w_id;
2288
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002289 ++entered;
2290
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002291 // may_garbage_collect is set in main_loop() to do garbage collection when
2292 // blocking to wait on a character. We don't want that while parsing
2293 // messages, a callback may invoke vgetc() while lists and dicts are in use
2294 // in the call stack.
2295 may_garbage_collect = FALSE;
2296
2297 // Loop when a job ended, but don't keep looping forever.
2298 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2299 {
2300 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002301# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002302 channel_handle_events(FALSE);
2303# endif
2304
2305# ifdef FEAT_NETBEANS_INTG
2306 // Process the queued netbeans messages.
2307 netbeans_parse_messages();
2308# endif
2309# ifdef FEAT_JOB_CHANNEL
2310 // Write any buffer lines still to be written.
2311 channel_write_any_lines();
2312
2313 // Process the messages queued on channels.
2314 channel_parse_messages();
2315# endif
2316# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2317 // Process the queued clientserver messages.
2318 server_parse_messages();
2319# endif
2320# ifdef FEAT_JOB_CHANNEL
2321 // Check if any jobs have ended. If so, repeat the above to handle
2322 // changes, e.g. stdin may have been closed.
2323 if (job_check_ended())
2324 continue;
2325# endif
2326# ifdef FEAT_TERMINAL
2327 free_unused_terminals();
2328# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002329
2330# ifdef FEAT_SOUND_MACOSX
2331 process_cfrunloop();
2332# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002333# ifdef FEAT_SOUND_CANBERRA
2334 if (has_sound_callback_in_queue())
2335 invoke_sound_callback();
2336# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002337#ifdef SIGUSR1
2338 if (got_sigusr1)
2339 {
2340 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2341 got_sigusr1 = FALSE;
2342 }
2343#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002344 break;
2345 }
2346
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002347 // When not nested we'll go back to waiting for a typed character. If it
2348 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002349 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002350 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002351
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002352 may_garbage_collect = save_may_garbage_collect;
2353
2354 // If the current window or buffer changed we need to bail out of the
2355 // waiting loop. E.g. when a job exit callback closes the terminal window.
2356 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002357 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002358
2359 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002360}
2361#endif
2362
2363
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002364typedef enum {
2365 map_result_fail, // failed, break loop
2366 map_result_get, // get a character from typeahead
2367 map_result_retry, // try to map again
2368 map_result_nomatch // no matching mapping, get char
2369} map_result_T;
2370
2371/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002372 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002373 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002374 */
2375 static int
zeertzjqee446032022-04-30 15:02:22 +01002376at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002377{
2378 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2379 int c = *p;
2380
2381 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002382 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002383 && p[1] == KS_MODIFIER
2384 && (p[2] & MOD_MASK_CTRL))
2385 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002386 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2387 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002388}
2389
2390/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002391 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002392 * into just a key, apply that.
2393 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2394 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002395 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002396 */
2397 static int
2398check_simplify_modifier(int max_offset)
2399{
2400 int offset;
2401 char_u *tp;
2402
2403 for (offset = 0; offset < max_offset; ++offset)
2404 {
2405 if (offset + 3 >= typebuf.tb_len)
2406 break;
2407 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002408 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002409 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002410 // A modifier was not used for a mapping, apply it to ASCII keys.
2411 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002412 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002413 int c = tp[3];
2414 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002415
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002416 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002417 {
2418 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002419 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002420
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002421 if (offset == 0)
2422 {
2423 // At the start: remember the character and mod_mask before
2424 // merging, in some cases, e.g. at the hit-return prompt,
2425 // they are put back in the typeahead buffer.
2426 vgetc_char = c;
2427 vgetc_mod_mask = tp[2];
2428 }
zeertzjq12e21e32022-04-27 11:58:01 +01002429 if (IS_SPECIAL(new_c))
2430 {
2431 new_string[0] = K_SPECIAL;
2432 new_string[1] = K_SECOND(new_c);
2433 new_string[2] = K_THIRD(new_c);
2434 len = 3;
2435 }
2436 else
2437 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002438 if (modifier == 0)
2439 {
2440 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002441 NULL, 0, NULL) == FAIL)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002442 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002443 }
2444 else
2445 {
2446 tp[2] = modifier;
2447 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002448 NULL, 0, NULL) == FAIL)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002449 return -1;
2450 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002451 return len;
2452 }
2453 }
2454 }
2455 return 0;
2456}
2457
2458/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002459 * Handle mappings in the typeahead buffer.
2460 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002461 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002462 * - When there is no match yet, return map_result_nomatch, need to get more
2463 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002464 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002465 */
2466 static int
2467handle_mapping(
2468 int *keylenp,
2469 int *timedout,
2470 int *mapdepth)
2471{
2472 mapblock_T *mp = NULL;
2473 mapblock_T *mp2;
2474 mapblock_T *mp_match;
2475 int mp_match_len = 0;
2476 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002477 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002478 int tb_c1;
2479 int mlen;
2480#ifdef FEAT_LANGMAP
2481 int nolmaplen;
2482#endif
2483 int keylen = *keylenp;
2484 int i;
2485 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002486 int is_plug_map = FALSE;
2487
zeertzjqbb404f52022-07-23 06:25:29 +01002488 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002489 if (typebuf.tb_len >= 3
2490 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002491 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2492 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2493 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002494
2495 /*
2496 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002497 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002498 *
2499 * Don't look for mappings if:
2500 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2501 * - maphash_valid not set: no mappings present.
2502 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2503 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002504 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 * - waiting for a char with --more--
2506 * - in Ctrl-X mode, and we get a valid char for that mode
2507 */
2508 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2509 if (no_mapping == 0 && is_maphash_valid()
2510 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002511 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002512 || (p_remap
2513 && (typebuf.tb_noremap[typebuf.tb_off]
2514 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002515 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2516 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2517 && State != MODE_ASKMORE
2518 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002519 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002520 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002521#ifdef FEAT_GUI
2522 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2523 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2524 {
2525 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2526 // K_SPECIAL KS_MODIFIER {flags}.
2527 tb_c1 = K_SPECIAL;
2528 }
2529#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002530#ifdef FEAT_LANGMAP
2531 if (tb_c1 == K_SPECIAL)
2532 nolmaplen = 2;
2533 else
2534 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002535 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2536 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002537 nolmaplen = 0;
2538 }
2539#endif
2540 // First try buffer-local mappings.
2541 mp = get_buf_maphash_list(local_State, tb_c1);
2542 mp2 = get_maphash_list(local_State, tb_c1);
2543 if (mp == NULL)
2544 {
2545 // There are no buffer-local mappings.
2546 mp = mp2;
2547 mp2 = NULL;
2548 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002549
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002550 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002551 * Loop until a partly matching mapping is found or all (local)
2552 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002553 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002554 * A full match is only accepted if there is no partly match, so "aa"
2555 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002556 */
2557 mp_match = NULL;
2558 mp_match_len = 0;
2559 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002560 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002561 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002562 // Only consider an entry if the first character matches and it is
2563 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002564 // Skip ":lmap" mappings if keys were mapped.
2565 if (mp->m_keys[0] == tb_c1
2566 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002567 && !(mp->m_simplified && seenModifyOtherKeys
2568 && typebuf.tb_maplen == 0)
Bram Moolenaar24959102022-05-07 20:01:16 +01002569 && ((mp->m_mode & MODE_LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002570 {
2571#ifdef FEAT_LANGMAP
2572 int nomap = nolmaplen;
2573 int c2;
2574#endif
2575 // find the match length of this mapping
2576 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2577 {
2578#ifdef FEAT_LANGMAP
2579 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2580 if (nomap > 0)
2581 --nomap;
2582 else if (c2 == K_SPECIAL)
2583 nomap = 2;
2584 else
2585 LANGMAP_ADJUST(c2, TRUE);
2586 if (mp->m_keys[mlen] != c2)
2587#else
2588 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002589 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002590#endif
2591 break;
2592 }
2593
Bram Moolenaareda35f72019-08-03 14:59:44 +02002594 // Don't allow mapping the first byte(s) of a multi-byte char.
2595 // Happens when mapping <M-a> and then changing 'encoding'.
2596 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002597 {
2598 char_u *p1 = mp->m_keys;
2599 char_u *p2 = mb_unescape(&p1);
2600
2601 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002602 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002603 mlen = 0;
2604 }
2605
2606 // Check an entry whether it matches.
2607 // - Full match: mlen == keylen
2608 // - Partly match: mlen == typebuf.tb_len
2609 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002610 if (mlen == keylen || (mlen == typebuf.tb_len
2611 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002612 {
2613 char_u *s;
2614 int n;
2615
Bram Moolenaareda35f72019-08-03 14:59:44 +02002616 // If only script-local mappings are allowed, check if the
2617 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002618 s = typebuf.tb_noremap + typebuf.tb_off;
2619 if (*s == RM_SCRIPT
2620 && (mp->m_keys[0] != K_SPECIAL
2621 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002622 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002623 continue;
2624
Bram Moolenaareda35f72019-08-03 14:59:44 +02002625 // If one of the typed keys cannot be remapped, skip the
2626 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002627 for (n = mlen; --n >= 0; )
2628 if (*s++ & (RM_NONE|RM_ABBR))
2629 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002630 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002631 continue;
2632
2633 if (keylen > typebuf.tb_len)
2634 {
2635 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002636 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002637 {
2638 // break at a partly match
2639 keylen = KEYLEN_PART_MAP;
2640 break;
2641 }
2642 }
2643 else if (keylen > mp_match_len)
2644 {
2645 // found a longer match
2646 mp_match = mp;
2647 mp_match_len = keylen;
2648 }
2649 }
2650 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002651 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002652 // character. If the first character that didn't match is
2653 // K_SPECIAL then check for a termcode. This isn't perfect
2654 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002655 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002656 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002657 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002658 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2659 }
2660 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2661 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002662 }
2663 }
2664
Bram Moolenaareda35f72019-08-03 14:59:44 +02002665 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002666 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002667 {
2668 mp = mp_match;
2669 keylen = mp_match_len;
2670 }
2671 }
2672
2673 /*
2674 * Check for match with 'pastetoggle'
2675 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002676 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002677 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002678 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2679 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002680 break;
2681 if (p_pt[mlen] == NUL) // match
2682 {
2683 // write chars to script file(s)
2684 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002685 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2686 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002687
2688 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002689 set_option_value_give_err((char_u *)"paste",
2690 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002691 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002692 {
2693 msg_col = 0;
2694 msg_row = Rows - 1;
2695 msg_clr_eos(); // clear ruler
2696 }
2697 status_redraw_all();
2698 redraw_statuslines();
2699 showmode();
2700 setcursor();
2701 *keylenp = keylen;
2702 return map_result_retry;
2703 }
2704 // Need more chars for partly match.
2705 if (mlen == typebuf.tb_len)
2706 keylen = KEYLEN_PART_KEY;
2707 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002708 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002709 max_mlen = mlen + 1;
2710 }
2711
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002712 // May check for a terminal code when there is no mapping or only a partial
2713 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2714 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002715 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002716 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2717 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002718 {
2719 int save_keylen = keylen;
2720
2721 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002722 * When no matching mapping found or found a non-matching mapping that
2723 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002724 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002725 * - mapping is allowed,
2726 * - keys have not been mapped,
2727 * - and not an ESC sequence, not in insert mode or p_ek is on,
2728 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002729 */
zeertzjq68a573c2022-04-28 14:10:01 +01002730 if (no_mapping == 0 || allow_keys != 0)
2731 {
2732 if ((typebuf.tb_maplen == 0
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002733 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002734 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002735 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002736 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2737 else
2738 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002739
Bram Moolenaar0684e362020-12-03 19:54:42 +01002740 // If no termcode matched but 'pastetoggle' matched partially
2741 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002742 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 keylen = KEYLEN_PART_KEY;
2744
Bram Moolenaar975a8802020-06-06 22:36:24 +02002745 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002746 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002747 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002748 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002749 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002750 if (keylen < 0)
2751 // ins_typebuf() failed
2752 return map_result_fail;
2753 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002754
Bram Moolenaareda35f72019-08-03 14:59:44 +02002755 // When getting a partial match, but the last characters were not
2756 // typed, don't wait for a typed character to complete the
2757 // termcode. This helps a lot when a ":normal" command ends in an
2758 // ESC.
2759 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002760 keylen = 0;
2761 }
2762 else
2763 keylen = 0;
2764 if (keylen == 0) // no matching terminal code
2765 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002766#ifdef AMIGA
2767 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002768 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002769 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002770 {
2771 char_u *s;
2772
2773 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002774 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2775 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002776 ++s)
2777 ;
2778 if (*s == 'r' || *s == '|') // found one
2779 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002780 del_typebuf(
2781 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002782 // get size and redraw screen
2783 shell_resized();
2784 *keylenp = keylen;
2785 return map_result_retry;
2786 }
2787 if (*s == NUL) // need more characters
2788 keylen = KEYLEN_PART_KEY;
2789 }
2790 if (keylen >= 0)
2791#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002792 // When there was a matching mapping and no termcode could be
2793 // replaced after another one, use that mapping (loop around).
2794 // If there was no mapping at all use the character from the
2795 // typeahead buffer right here.
2796 if (mp == NULL)
2797 {
2798 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002799 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002800 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002801 }
2802
2803 if (keylen > 0) // full matching terminal code
2804 {
2805#if defined(FEAT_GUI) && defined(FEAT_MENU)
2806 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002807 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2808 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002809 {
2810 int idx;
2811
Bram Moolenaareda35f72019-08-03 14:59:44 +02002812 // Using a menu may cause a break in undo! It's like using
2813 // gotchars(), but without recording or writing to a script
2814 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002815 may_sync_undo();
2816 del_typebuf(3, 0);
2817 idx = get_menu_index(current_menu, local_State);
2818 if (idx != MENU_INDEX_INVALID)
2819 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002820 // In Select mode and a Visual mode menu is used: Switch
2821 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002822 // back to Select mode.
2823 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002824 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 {
2826 VIsual_select = FALSE;
2827 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002828 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002829 }
2830 ins_typebuf(current_menu->strings[idx],
2831 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002832 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002833 }
2834 }
2835#endif // FEAT_GUI && FEAT_MENU
2836 *keylenp = keylen;
2837 return map_result_retry; // try mapping again
2838 }
2839
Bram Moolenaareda35f72019-08-03 14:59:44 +02002840 // Partial match: get some more characters. When a matching mapping
2841 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002842 if (mp == NULL || keylen < 0)
2843 keylen = KEYLEN_PART_KEY;
2844 else
2845 keylen = mp_match_len;
2846 }
2847
2848 /*
2849 * complete match
2850 */
2851 if (keylen >= 0 && keylen <= typebuf.tb_len)
2852 {
2853 char_u *map_str;
2854
2855#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002856 int save_m_expr;
2857 int save_m_noremap;
2858 int save_m_silent;
2859 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002860#else
2861# define save_m_noremap mp->m_noremap
2862# define save_m_silent mp->m_silent
2863#endif
2864
2865 // write chars to script file(s)
2866 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002867 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2868 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002869
2870 cmd_silent = (typebuf.tb_silent > 0);
2871 del_typebuf(keylen, 0); // remove the mapped keys
2872
2873 /*
2874 * Put the replacement string in front of mapstr.
2875 * The depth check catches ":map x y" and ":map y x".
2876 */
2877 if (++*mapdepth >= p_mmd)
2878 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002879 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002880 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002881 redrawcmdline();
2882 else
2883 setcursor();
2884 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002885 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002886 *keylenp = keylen;
2887 return map_result_fail;
2888 }
2889
2890 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002891 * In Select mode and a Visual mode mapping is used: Switch to Visual
2892 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002893 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002894 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002895 {
2896 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002897 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002898 }
2899
2900#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002901 // Copy the values from *mp that are used, because evaluating the
2902 // expression may invoke a function that redefines the mapping, thereby
2903 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002904 save_m_expr = mp->m_expr;
2905 save_m_noremap = mp->m_noremap;
2906 save_m_silent = mp->m_silent;
2907 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002908
2909 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002910 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2911 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002912 */
2913 if (mp->m_expr)
2914 {
2915 int save_vgetc_busy = vgetc_busy;
2916 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002917 int was_screen_col = screen_cur_col;
2918 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002919 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002920
2921 vgetc_busy = 0;
2922 may_garbage_collect = FALSE;
2923
2924 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002925 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002926
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002927 // The mapping may do anything, but we expect it to take care of
2928 // redrawing. Do put the cursor back where it was.
2929 windgoto(was_screen_row, was_screen_col);
2930 out_flush();
2931
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002932 // If an error was displayed and the expression returns an empty
2933 // string, generate a <Nop> to allow for a redraw.
2934 if (prev_did_emsg != did_emsg
2935 && (map_str == NULL || *map_str == NUL))
2936 {
2937 char_u buf[4];
2938
2939 vim_free(map_str);
2940 buf[0] = K_SPECIAL;
2941 buf[1] = KS_EXTRA;
2942 buf[2] = KE_IGNORE;
2943 buf[3] = NUL;
2944 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01002945 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002946 {
2947 // redraw the command below the error
2948 msg_didout = TRUE;
2949 if (msg_row < cmdline_row)
2950 msg_row = cmdline_row;
2951 redrawcmd();
2952 }
2953 }
2954
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002955 vgetc_busy = save_vgetc_busy;
2956 may_garbage_collect = save_may_garbage_collect;
2957 }
2958 else
2959#endif
2960 map_str = mp->m_str;
2961
2962 /*
2963 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002964 * If 'from' field is the same as the start of the 'to' field, don't
2965 * remap the first character (but do allow abbreviations).
2966 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002967 */
2968 if (map_str == NULL)
2969 i = FAIL;
2970 else
2971 {
2972 int noremap;
2973
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002974#ifdef FEAT_EVAL
2975 last_used_map = mp;
2976 last_used_sid = -1;
2977#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002978 if (save_m_noremap != REMAP_YES)
2979 noremap = save_m_noremap;
2980 else if (
2981#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002982 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2983 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002984#else
2985 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2986#endif
2987 != 0)
2988 noremap = REMAP_YES;
2989 else
2990 noremap = REMAP_SKIP;
2991 i = ins_typebuf(map_str, noremap,
2992 0, TRUE, cmd_silent || save_m_silent);
2993#ifdef FEAT_EVAL
2994 if (save_m_expr)
2995 vim_free(map_str);
2996#endif
2997 }
2998#ifdef FEAT_EVAL
2999 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003000#endif
3001 *keylenp = keylen;
3002 if (i == FAIL)
3003 return map_result_fail;
3004 return map_result_retry;
3005 }
3006
3007 *keylenp = keylen;
3008 return map_result_nomatch;
3009}
3010
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003011/*
3012 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003013 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003014 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003017vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 old_char = c;
3020 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003021 old_mouse_row = mouse_row;
3022 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003023 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024}
3025
3026/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003027 * When peeking and not getting a character, reg_executing cannot be cleared
3028 * yet, so set a flag to clear it later.
3029 */
3030 static void
3031check_end_reg_executing(int advance)
3032{
3033 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3034 || pending_end_reg_executing))
3035 {
3036 if (advance)
3037 {
3038 reg_executing = 0;
3039 pending_end_reg_executing = FALSE;
3040 }
3041 else
3042 pending_end_reg_executing = TRUE;
3043 }
3044
3045}
3046
3047/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003048 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 * 1. from the stuffbuffer
3050 * This is used for abbreviated commands like "D" -> "d$".
3051 * Also used to redo a command for ".".
3052 * 2. from the typeahead buffer
3053 * Stores text obtained previously but not used yet.
3054 * Also stores the result of mappings.
3055 * Also used for the ":normal" command.
3056 * 3. from the user
3057 * This may do a blocking wait if "advance" is TRUE.
3058 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003059 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003060 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 * KeyTyped is set to TRUE in the case the user typed the key.
3062 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003063 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003064 * Just look whether there is a character available.
3065 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 *
3067 * When "no_mapping" is zero, checks for mappings in the current mode.
3068 * Only returns one byte (of a multi-byte character).
3069 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3070 */
3071 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003072vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 int c, c1;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003075 int timedout = FALSE; // waited for more than 'timeoutlen'
3076 // for mapping to complete or
3077 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003078 int mapdepth = 0; // check for recursive mapping
3079 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003082 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083#endif
3084 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003086 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 /*
3089 * This function doesn't work very well when called recursively. This may
3090 * happen though, because of:
3091 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3092 * there is a character available, which calls this function. In that
3093 * case we must return NUL, to indicate no character is available.
3094 * 2. A GUI callback function writes to the screen, causing a
3095 * wait_return().
3096 * Using ":normal" can also do this, but it saves the typeahead buffer,
3097 * thus it should be OK. But don't get a key from the user then.
3098 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003099 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 return NUL;
3101
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003102 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003107 typebuf_was_empty = FALSE;
3108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 init_typebuf();
3111 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003112 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 do
3114 {
3115/*
3116 * get a character: 1. from the stuffbuffer
3117 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003118 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
3120 c = typeahead_char;
3121 if (advance)
3122 typeahead_char = 0;
3123 }
3124 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003125 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (c != NUL && !got_int)
3127 {
3128 if (advance)
3129 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003130 // KeyTyped = FALSE; When the command that stuffed something
3131 // was typed, behave like the stuffed command was typed.
3132 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 KeyStuffed = TRUE;
3134 }
3135 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003136 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138 else
3139 {
3140 /*
3141 * Loop until we either find a matching mapped key, or we
3142 * are sure that it is not a mapped key.
3143 * If a mapped key sequence is found we go back to the start to
3144 * try re-mapping.
3145 */
3146 for (;;)
3147 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003148 long wait_time;
3149 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003150 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003151 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /*
3153 * ui_breakcheck() is slow, don't use it too often when
3154 * inside a mapping. But call it each time for typed
3155 * characters.
3156 */
3157 if (typebuf.tb_maplen)
3158 line_breakcheck();
3159 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003160 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (got_int)
3162 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003163 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003164 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /*
3167 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003168 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * Otherwise we behave like having gotten a CTRL-C.
3170 * As a result typing CTRL-C in insert mode will
3171 * really insert a CTRL-C.
3172 */
3173 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003174 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 c = ESC;
3176 else
3177 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003178 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003180 if (advance)
3181 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003182 // Also record this character, it might be needed to
3183 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003184 *typebuf.tb_buf = c;
3185 gotchars(typebuf.tb_buf, 1);
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 cmd_silent = FALSE;
3188
3189 break;
3190 }
3191 else if (typebuf.tb_len > 0)
3192 {
3193 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003194 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003196 map_result_T result = handle_mapping(
3197 &keylen, &timedout, &mapdepth);
3198
3199 if (result == map_result_retry)
3200 // try mapping again
3201 continue;
3202 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003203 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003204 // failed, use the outer loop
3205 c = -1;
3206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003208 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210/*
3211 * get a character: 2. from the typeahead buffer
3212 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003213 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003214 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003215 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003216 cmd_silent = (typebuf.tb_silent > 0);
3217 if (typebuf.tb_maplen > 0)
3218 KeyTyped = FALSE;
3219 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003220 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003221 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003222 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003223 gotchars(typebuf.tb_buf
3224 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003225 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003226 KeyNoremap = typebuf.tb_noremap[
3227 typebuf.tb_off];
3228 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003229 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003230 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003233 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
3235
3236/*
3237 * get a character: 3. from the user - handle <Esc> in Insert mode
3238 */
3239 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003240 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * are no more characters at once, we pretend to go out of
3242 * insert mode. This prevents the one second delay after
3243 * typing an <ESC>. If we get something after all, we may
3244 * have to redisplay the mode. That the cursor is in the wrong
3245 * place does not matter.
3246 */
3247 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 new_wcol = curwin->w_wcol;
3249 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if ( advance
3251 && typebuf.tb_len == 1
3252 && typebuf.tb_buf[typebuf.tb_off] == ESC
3253 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003256 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003257 && (p_timeout
3258 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003260 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003262 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 char_u *ptr;
3264
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003265 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 unshowmode(TRUE);
3268 mode_deleted = TRUE;
3269 }
3270#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003271 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003272 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 int save_State;
3275
3276 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003277 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 gui_update_cursor(TRUE, FALSE);
3279 State = save_State;
3280 shape_changed = TRUE;
3281 }
3282#endif
3283 validate_cursor();
3284 old_wcol = curwin->w_wcol;
3285 old_wrow = curwin->w_wrow;
3286
Bram Moolenaar30613902019-12-01 22:11:18 +01003287 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (curwin->w_cursor.col != 0)
3289 {
3290 if (curwin->w_wcol > 0)
3291 {
3292 if (did_ai)
3293 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003294 chartabsize_T cts;
3295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 /*
3297 * We are expecting to truncate the trailing
3298 * white-space, so find the last non-white
3299 * character -- webb
3300 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003301 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003303 init_chartabsize_arg(&cts, curwin,
3304 curwin->w_cursor.lnum, 0, ptr, ptr);
3305 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003307 if (!VIM_ISWHITE(*cts.cts_ptr))
3308 curwin->w_wcol = cts.cts_vcol;
3309 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003311 cts.cts_ptr +=
3312 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003314 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003316 clear_chartabsize_arg(&cts);
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003319 + curwin->w_wcol / curwin->w_width;
3320 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003322 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324 else
3325 {
3326 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 }
3330 else if (curwin->w_p_wrap && curwin->w_wrow)
3331 {
3332 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003333 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3337 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003338 // Correct when the cursor is on the right halve
3339 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 ptr = ml_get_curline();
3341 col -= (*mb_head_off)(ptr, ptr + col);
3342 if ((*mb_ptr2cells)(ptr + col) > 1)
3343 --curwin->w_wcol;
3344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346 setcursor();
3347 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 new_wcol = curwin->w_wcol;
3349 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 curwin->w_wcol = old_wcol;
3351 curwin->w_wrow = old_wrow;
3352 }
3353 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003354 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003355
Bram Moolenaar30613902019-12-01 22:11:18 +01003356 // Allow mapping for just typed characters. When we get here c
3357 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003358 for (n = 1; n <= c; ++n)
3359 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 typebuf.tb_len += c;
3361
Bram Moolenaar30613902019-12-01 22:11:18 +01003362 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3364 {
3365 timedout = TRUE;
3366 continue;
3367 }
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (ex_normal_busy > 0)
3370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
Bram Moolenaar30613902019-12-01 22:11:18 +01003373 // No typeahead left and inside ":normal". Must return
3374 // something to avoid getting stuck. When an incomplete
3375 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (typebuf.tb_len > 0)
3377 {
3378 timedout = TRUE;
3379 continue;
3380 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003381
Bram Moolenaar30613902019-12-01 22:11:18 +01003382 // When 'insertmode' is set, ESC just beeps in Insert
3383 // mode. Use CTRL-L to make edit() return.
3384 // For the command line only CTRL-C always breaks it.
3385 // For the cmdline window: Alternate between ESC and
3386 // CTRL-C: ESC for most situations and CTRL-C to close the
3387 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003388 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003390#ifdef FEAT_TERMINAL
3391 else if (terminal_is_active())
3392 c = K_CANCEL;
3393#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003394 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003395 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 c = Ctrl_C;
3397 else
3398 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003400 // set a flag to indicate this wasn't a normal char
3401 if (advance)
3402 typebuf_was_empty = TRUE;
3403
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003404 // return from main_loop()
3405 if (pending_exmode_active)
3406 exmode_active = EXMODE_NORMAL;
3407
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003408 // no chars to block abbreviation for
3409 typebuf.tb_no_abbr_cnt = 0;
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 break;
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414/*
3415 * get a character: 3. from the user - update display
3416 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003417 // In insert mode a screen update is skipped when characters
3418 // are still available. But when those available characters
3419 // are part of a mapping, and we are going to do a blocking
3420 // wait here. Need to update the screen to display the
3421 // changed text so far. Also for when 'lazyredraw' is set and
3422 // redrawing was postponed because there was something in the
3423 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003424 if (((State & MODE_INSERT) != 0 || p_lz)
3425 && (State & MODE_CMDLINE) == 0
3426 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
3428 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003429 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431
3432 /*
3433 * If we have a partial match (and are going to wait for more
3434 * input from the user), show the partially matched characters
3435 * to the user with showcmd.
3436 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003437 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 c1 = 0;
3439 if (typebuf.tb_len > 0 && advance && !exmode_active)
3440 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003441 if (((State & (MODE_NORMAL | MODE_INSERT))
3442 || State == MODE_LANGMAP)
3443 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003445 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003446 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3448 + typebuf.tb_len - 1) == 1)
3449 {
3450 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3451 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003452 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 c1 = 1;
3454 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003455 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 old_wcol = curwin->w_wcol;
3457 old_wrow = curwin->w_wrow;
3458 curwin->w_wcol = new_wcol;
3459 curwin->w_wrow = new_wrow;
3460 push_showcmd();
3461 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003462 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3463 while (showcmd_idx < typebuf.tb_len)
3464 (void)add_to_showcmd(
3465 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 curwin->w_wcol = old_wcol;
3467 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469
Bram Moolenaar30613902019-12-01 22:11:18 +01003470 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003471 if ((State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3473 && cmdline_star == 0
3474#endif
3475 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3476 + typebuf.tb_len - 1) == 1)
3477 {
3478 putcmdline(typebuf.tb_buf[typebuf.tb_off
3479 + typebuf.tb_len - 1], FALSE);
3480 c1 = 1;
3481 }
3482 }
3483
3484/*
3485 * get a character: 3. from the user - get it
3486 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003487 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003488 // timedout may have been set if a mapping with empty RHS
3489 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003490 timedout = FALSE;
3491
Bram Moolenaar652de232019-04-04 20:13:09 +02003492 if (advance)
3493 {
3494 if (typebuf.tb_len == 0
3495 || !(p_timeout
3496 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3497 // blocking wait
3498 wait_time = -1L;
3499 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3500 wait_time = p_ttm;
3501 else
3502 wait_time = p_tm;
3503 }
3504 else
3505 wait_time = 0;
3506
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003507 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3509 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003510 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaareda35f72019-08-03 14:59:44 +02003512 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 pop_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (c1 == 1)
3515 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003516 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 edit_unputchar();
Bram Moolenaar24959102022-05-07 20:01:16 +01003518 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003520 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003521 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523
3524 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003525 continue; // end of input script reached
3526 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 if (!advance)
3529 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003530 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 timedout = TRUE;
3533 continue;
3534 }
3535 }
3536 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003537 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 while (typebuf.tb_buf[typebuf.tb_off
3539 + typebuf.tb_len] != NUL)
3540 typebuf.tb_noremap[typebuf.tb_off
3541 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003542#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003543 // Get IM status right after getting keys, not after the
3544 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 vgetc_im_active = im_get_status();
3546#endif
3547 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003548 } // for (;;)
3549 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
Bram Moolenaar30613902019-12-01 22:11:18 +01003551 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003552 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
3554 /*
3555 * The "INSERT" message is taken care of here:
3556 * if we return an ESC to exit insert mode, the message is deleted
3557 * if we don't return an ESC but deleted the message before, redisplay it
3558 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003559 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003561 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003564 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
3566 unshowmode(FALSE);
3567 }
3568 else if (c != ESC && mode_deleted)
3569 {
3570 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003571 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 else
3573 showmode();
3574 }
3575 }
3576#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003577 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003578 if (gui.in_use && shape_changed)
3579 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003581 if (timedout && c == ESC)
3582 {
3583 char_u nop_buf[3];
3584
3585 // When recording there will be no timeout. Add a <Nop> after the ESC
3586 // to avoid that it forms a key code with following characters.
3587 nop_buf[0] = K_SPECIAL;
3588 nop_buf[1] = KS_EXTRA;
3589 nop_buf[2] = KE_NOP;
3590 gotchars(nop_buf, 3);
3591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003593 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595 return c;
3596}
3597
3598/*
3599 * inchar() - get one character from
3600 * 1. a scriptfile
3601 * 2. the keyboard
3602 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003603 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * NUL terminated (buffer length must be 'maxlen' + 1).
3605 * Minimum for "maxlen" is 3!!!!
3606 *
3607 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3608 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3609 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3610 * otherwise.
3611 *
3612 * If we got an interrupt all input is read until none is available.
3613 *
3614 * If wait_time == 0 there is no waiting for the char.
3615 * If wait_time == n we wait for n msec for a character to arrive.
3616 * If wait_time == -1 we wait forever for a character to arrive.
3617 *
3618 * Return the number of obtained characters.
3619 * Return -1 when end of input script reached.
3620 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003621 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003622inchar(
3623 char_u *buf,
3624 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003625 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
Bram Moolenaar30613902019-12-01 22:11:18 +01003627 int len = 0; // init for GCC
3628 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003630 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar30613902019-12-01 22:11:18 +01003632 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003635 out_flush_cursor(FALSE, FALSE);
3636#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3637 if (gui.in_use && postponed_mouseshape)
3638 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
3640 }
3641
3642 /*
3643 * Don't reset these when at the hit-return prompt, otherwise a endless
3644 * recursive loop may result (write error in swapfile, hit-return, timeout
3645 * on char wait, flush swapfile, write error....).
3646 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003647 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003649 did_outofmem_msg = FALSE; // display out of memory message (again)
3650 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003652 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003655 * Get a character from a script file if there is one.
3656 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
3658 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003659 while (scriptin[curscript] != NULL && script_char < 0
3660#ifdef FEAT_EVAL
3661 && !ignore_script
3662#endif
3663 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003665#ifdef MESSAGE_QUEUE
3666 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003667#endif
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3670 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003671 // Reached EOF.
3672 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3673 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 closescript();
3675 /*
3676 * When reading script file is interrupted, return an ESC to get
3677 * back to normal mode.
3678 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3679 */
3680 if (got_int)
3681 retesc = TRUE;
3682 else
3683 return -1;
3684 }
3685 else
3686 {
3687 buf[0] = script_char;
3688 len = 1;
3689 }
3690 }
3691
Bram Moolenaar30613902019-12-01 22:11:18 +01003692 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 /*
3695 * If we got an interrupt, skip all previously typed characters and
3696 * return TRUE if quit reading script file.
3697 * Stop reading typeahead when a single CTRL-C was read,
3698 * fill_input_buf() returns this when not able to read from stdin.
3699 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3700 * and buf may be pointing inside typebuf.tb_buf[].
3701 */
3702 if (got_int)
3703 {
kylo252ae6f1d82022-02-16 19:24:07 +00003704#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 char_u dum[DUM_LEN + 1];
3706
3707 for (;;)
3708 {
3709 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003710 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 break;
3712 }
3713 return retesc;
3714 }
3715
3716 /*
3717 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003718 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003720 if (wait_time == -1L || wait_time > 10L)
3721 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 /*
3724 * Fill up to a third of the buffer, because each character may be
3725 * tripled below.
3726 */
3727 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3728 }
3729
Bram Moolenaar30613902019-12-01 22:11:18 +01003730 // If the typebuf was changed further down, it is like nothing was added by
3731 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (typebuf_changed(tb_change_cnt))
3733 return 0;
3734
Bram Moolenaar30613902019-12-01 22:11:18 +01003735 // Note the change in the typeahead buffer, this matters for when
3736 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3737 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003738 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3739 typebuf.tb_change_cnt = 1;
3740
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003741 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742}
3743
3744/*
3745 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003746 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 * Returns the new length.
3748 */
3749 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003750fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752 int i;
3753 char_u *p = buf;
3754
3755 /*
3756 * Two characters are special: NUL and K_SPECIAL.
3757 * When compiled With the GUI CSI is also special.
3758 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3759 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3760 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
3762 for (i = len; --i >= 0; ++p)
3763 {
3764#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003765 // When the GUI is used any character can come after a CSI, don't
3766 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (gui.in_use && p[0] == CSI && i >= 2)
3768 {
3769 p += 2;
3770 i -= 2;
3771 }
zeertzjq646bb722022-02-16 17:51:47 +00003772# ifndef MSWIN
3773 // When not on MS-Windows and the GUI is not used CSI needs to be
3774 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 else if (!gui.in_use && p[0] == CSI)
3776 {
3777 mch_memmove(p + 3, p + 1, (size_t)i);
3778 *p++ = K_SPECIAL;
3779 *p++ = KS_EXTRA;
3780 *p = (int)KE_CSI;
3781 len += 2;
3782 }
zeertzjq646bb722022-02-16 17:51:47 +00003783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 else
3785#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003786 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003787 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003788 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003789#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003790 // Win32 console passes modifiers
3791 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003792# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003793 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003794# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003795 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#endif
3797 ))
3798 {
3799 mch_memmove(p + 3, p + 1, (size_t)i);
3800 p[2] = K_THIRD(p[0]);
3801 p[1] = K_SECOND(p[0]);
3802 p[0] = K_SPECIAL;
3803 p += 2;
3804 len += 2;
3805 }
3806 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003807 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return len;
3809}
3810
3811#if defined(USE_INPUT_BUF) || defined(PROTO)
3812/*
3813 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3814 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003815 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003816 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 */
3818 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003819input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003822# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3823 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824# endif
3825 );
3826}
3827#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003828
3829/*
3830 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3831 * typeahead.
3832 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003833 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003834getcmdkeycmd(
3835 int promptc UNUSED,
3836 void *cookie UNUSED,
3837 int indent UNUSED,
3838 getline_opt_T do_concat UNUSED)
3839{
3840 garray_T line_ga;
3841 int c1 = -1;
3842 int c2;
3843 int cmod = 0;
3844 int aborted = FALSE;
3845
3846 ga_init2(&line_ga, 1, 32);
3847
3848 // no mapping for these characters
3849 no_mapping++;
3850
3851 got_int = FALSE;
3852 while (c1 != NUL && !aborted)
3853 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003854 if (ga_grow(&line_ga, 32) != OK)
3855 {
3856 aborted = TRUE;
3857 break;
3858 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003859
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003860 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003861 {
3862 // incomplete <Cmd> is an error, because there is not much the user
3863 // could do in this state.
3864 emsg(_(e_cmd_mapping_must_end_with_cr));
3865 aborted = TRUE;
3866 break;
3867 }
3868
3869 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003870 c1 = vgetorpeek(TRUE);
3871
Bram Moolenaar957cf672020-11-12 14:21:06 +01003872 // Get two extra bytes for special keys
3873 if (c1 == K_SPECIAL)
3874 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003875 c1 = vgetorpeek(TRUE);
3876 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003877 if (c1 == KS_MODIFIER)
3878 {
3879 cmod = c2;
3880 continue;
3881 }
3882 c1 = TO_SPECIAL(c1, c2);
3883 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003884 if (c1 == Ctrl_V)
3885 {
3886 // CTRL-V is followed by octal, hex or other characters, reverses
3887 // what AppendToRedobuffLit() does.
3888 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003889 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003890 no_reduce_keys = FALSE;
3891 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003892
3893 if (got_int)
3894 aborted = TRUE;
3895 else if (c1 == '\r' || c1 == '\n')
3896 c1 = NUL; // end the line
3897 else if (c1 == ESC)
3898 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003899 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003900 {
3901 // give a nicer error message for this special case
3902 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3903 aborted = TRUE;
3904 }
3905 else if (IS_SPECIAL(c1))
3906 {
3907 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003908 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003909 else
3910 {
3911 semsg(e_cmd_maping_must_not_include_str_key,
3912 get_special_key_name(c1, cmod));
3913 aborted = TRUE;
3914 }
3915 }
3916 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003917 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003918
3919 cmod = 0;
3920 }
3921
3922 no_mapping--;
3923
3924 if (aborted)
3925 ga_clear(&line_ga);
3926
3927 return (char_u *)line_ga.ga_data;
3928}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003929
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003930#if defined(FEAT_EVAL) || defined(PROTO)
3931/*
3932 * If there was a mapping put info about it in the redo buffer, so that "."
3933 * will use the same script context. We only need the SID.
3934 */
3935 void
3936may_add_last_used_map_to_redobuff(void)
3937{
3938 char_u buf[3 + 20];
3939
3940 if (last_used_map == NULL || last_used_map->m_script_ctx.sc_sid < 0)
3941 return;
3942
3943 // <K_SID>{nr};
3944 buf[0] = K_SPECIAL;
3945 buf[1] = KS_EXTRA;
3946 buf[2] = KE_SID;
3947 vim_snprintf((char *)buf + 3, 20, "%d;",
3948 last_used_map->m_script_ctx.sc_sid);
3949 add_buff(&redobuff, buf, -1L);
3950}
3951#endif
3952
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003953 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003954do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003955{
3956 int res;
3957#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003958 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003959
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003960 if (key == K_SCRIPT_COMMAND
3961 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003962 {
3963 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003964 if (last_used_map != NULL)
3965 current_sctx = last_used_map->m_script_ctx;
3966 else
3967 {
3968 current_sctx.sc_sid = last_used_sid;
3969 current_sctx.sc_lnum = 0;
3970 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
3971 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003972 }
3973#endif
3974
3975 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3976
3977#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003978 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003979 current_sctx = save_current_sctx;
3980#endif
3981
3982 return res;
3983}
3984
3985#if defined(FEAT_EVAL) || defined(PROTO)
3986 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003987reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003988{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003989 if (last_used_map == mp)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003990 {
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003991 last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003992 last_used_sid = -1;
3993 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003994}
3995#endif