blob: 2fa68a789f004bd7c6729994c4754fcfbb6399dd [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 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000971 * If "noremap" is REMAP_YES, new string can be mapped again.
972 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
973 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000974 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000975 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000977 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000979 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
980 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000982 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 *
984 * return FAIL for failure, OK otherwise
985 */
986 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100987ins_typebuf(
988 char_u *str,
989 int noremap,
990 int offset,
991 int nottyped,
992 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 char_u *s1, *s2;
995 int newlen;
996 int addlen;
997 int i;
998 int newoff;
999 int val;
1000 int nrm;
1001
1002 init_typebuf();
1003 if (++typebuf.tb_change_cnt == 0)
1004 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001005 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (offset == 0 && addlen <= typebuf.tb_off)
1010 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001011 /*
1012 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 typebuf.tb_off -= addlen;
1015 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1016 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001017 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1018 >= addlen + 3 * (MAXMAPLEN + 4))
1019 {
1020 /*
1021 * Buffer is empty and string fits in the existing buffer.
1022 * Leave some space before and after, if possible.
1023 */
1024 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1025 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else
1028 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001029 int extra;
1030
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001031 /*
1032 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001033 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001034 * characters. We add some extra room to avoid having to allocate too
1035 * often.
1036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001038 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1039 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001041 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001042 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 setcursor();
1044 return FAIL;
1045 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001046 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001048 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 return FAIL;
1050 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 vim_free(s1);
1054 return FAIL;
1055 }
1056 typebuf.tb_buflen = newlen;
1057
Bram Moolenaar30613902019-12-01 22:11:18 +01001058 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1060 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001061 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001063 // copy the old chars, after the insertion point, including the NUL at
1064 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 mch_memmove(s1 + newoff + offset + addlen,
1066 typebuf.tb_buf + typebuf.tb_off + offset,
1067 (size_t)(typebuf.tb_len - offset + 1));
1068 if (typebuf.tb_buf != typebuf_init)
1069 vim_free(typebuf.tb_buf);
1070 typebuf.tb_buf = s1;
1071
1072 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1073 (size_t)offset);
1074 mch_memmove(s2 + newoff + offset + addlen,
1075 typebuf.tb_noremap + typebuf.tb_off + offset,
1076 (size_t)(typebuf.tb_len - offset));
1077 if (typebuf.tb_noremap != noremapbuf_init)
1078 vim_free(typebuf.tb_noremap);
1079 typebuf.tb_noremap = s2;
1080
1081 typebuf.tb_off = newoff;
1082 }
1083 typebuf.tb_len += addlen;
1084
Bram Moolenaar30613902019-12-01 22:11:18 +01001085 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (noremap == REMAP_SCRIPT)
1087 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001088 else if (noremap == REMAP_SKIP)
1089 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 else
1091 val = RM_NONE;
1092
1093 /*
1094 * Adjust typebuf.tb_noremap[] for the new characters:
1095 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1096 * (sometimes) not remappable
1097 * If noremap == REMAP_YES: all the new characters are mappable
1098 * If noremap > 0: "noremap" characters are not remappable, the rest
1099 * mappable
1100 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001101 if (noremap == REMAP_SKIP)
1102 nrm = 1;
1103 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 nrm = addlen;
1105 else
1106 nrm = noremap;
1107 for (i = 0; i < addlen; ++i)
1108 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1109 (--nrm >= 0) ? val : RM_YES;
1110
Bram Moolenaar30613902019-12-01 22:11:18 +01001111 // tb_maplen and tb_silent only remember the length of mapped and/or
1112 // silent mappings at the start of the buffer, assuming that a mapped
1113 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (nottyped || typebuf.tb_maplen > offset)
1115 typebuf.tb_maplen += addlen;
1116 if (silent || typebuf.tb_silent > offset)
1117 {
1118 typebuf.tb_silent += addlen;
1119 cmd_silent = TRUE;
1120 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001121 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 typebuf.tb_no_abbr_cnt += addlen;
1123
1124 return OK;
1125}
1126
1127/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 * Put character "c" back into the typeahead buffer.
1129 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001130 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1131 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001132 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001133 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001134 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001135ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001136{
zeertzjq6cac7702022-01-04 18:01:21 +00001137 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001138 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001139
zeertzjq2e7cba32022-06-10 15:30:32 +01001140 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001141 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001142 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001143}
1144
1145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001147 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001148 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1150 * changed it was reallocated and the old pointer can no longer be used.
1151 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1152 * that was just added.
1153 */
1154 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001155typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001156 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001159#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1160 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161#endif
1162 ));
1163}
1164
1165/*
1166 * Return TRUE if there are no characters in the typeahead buffer that have
1167 * not been typed (result from a mapping or come from ":normal").
1168 */
1169 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001170typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 return typebuf.tb_maplen == 0;
1173}
1174
1175/*
1176 * Return the number of characters that are mapped (or not typed).
1177 */
1178 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001179typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 return typebuf.tb_maplen;
1182}
1183
1184/*
1185 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1186 */
1187 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001188del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 int i;
1191
1192 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001193 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 typebuf.tb_len -= len;
1196
1197 /*
1198 * Easy case: Just increase typebuf.tb_off.
1199 */
1200 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1201 >= 3 * MAXMAPLEN + 3)
1202 typebuf.tb_off += len;
1203 /*
1204 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1205 */
1206 else
1207 {
1208 i = typebuf.tb_off + offset;
1209 /*
1210 * Leave some extra room at the end to avoid reallocation.
1211 */
1212 if (typebuf.tb_off > MAXMAPLEN)
1213 {
1214 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1215 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1216 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1217 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1218 typebuf.tb_off = MAXMAPLEN;
1219 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001220 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1222 typebuf.tb_buf + i + len,
1223 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1226 typebuf.tb_noremap + i + len,
1227 (size_t)(typebuf.tb_len - offset));
1228 }
1229
Bram Moolenaar30613902019-12-01 22:11:18 +01001230 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 if (typebuf.tb_maplen < offset + len)
1233 typebuf.tb_maplen = offset;
1234 else
1235 typebuf.tb_maplen -= len;
1236 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001237 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (typebuf.tb_silent < offset + len)
1240 typebuf.tb_silent = offset;
1241 else
1242 typebuf.tb_silent -= len;
1243 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 if (typebuf.tb_no_abbr_cnt < offset + len)
1247 typebuf.tb_no_abbr_cnt = offset;
1248 else
1249 typebuf.tb_no_abbr_cnt -= len;
1250 }
1251
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001252#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001253 // Reset the flag that text received from a client or from feedkeys()
1254 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001255 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257 if (++typebuf.tb_change_cnt == 0)
1258 typebuf.tb_change_cnt = 1;
1259}
1260
1261/*
1262 * Write typed characters to script file.
1263 * If recording is on put the character in the recordbuffer.
1264 */
1265 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001266gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001268 char_u *s = chars;
1269 int i;
1270 static char_u buf[4];
1271 static int buflen = 0;
1272 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001274 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001276 buf[buflen++] = *s++;
1277
1278 // When receiving a special key sequence, store it until we have all
1279 // the bytes and we can decide what to do with it.
1280 if (buflen == 1 && buf[0] == K_SPECIAL)
1281 continue;
1282 if (buflen == 2)
1283 continue;
1284 if (buflen == 3 && buf[1] == KS_EXTRA
1285 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1286 {
1287 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1288 // recording.
1289 buflen = 0;
1290 continue;
1291 }
1292
Bram Moolenaar30613902019-12-01 22:11:18 +01001293 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001294 for (i = 0; i < buflen; ++i)
1295 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001297 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001299 buf[buflen] = NUL;
1300 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001301 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001302 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001304 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 may_sync_undo();
1307
1308#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001309 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 debug_did_msg = FALSE;
1311#endif
1312
Bram Moolenaar30613902019-12-01 22:11:18 +01001313 // Since characters have been typed, consider the following to be in
1314 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ++maptick;
1316}
1317
1318/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001319 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1320 * character back into the typeahead buffer, thus gotchars() will be called
1321 * again.
1322 * Only affects recorded characters.
1323 */
1324 void
1325ungetchars(int len)
1326{
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 Moolenaar88456cd2022-11-18 22:14:09 +00001604 * updatescript() is called when a character can be written into the script
1605 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 *
1607 * All the changed memfiles are synced if c == 0 or when the number of typed
1608 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1609 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001610 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001611updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 static int count = 0;
1614
1615 if (c && scriptout)
1616 putc(c, scriptout);
1617 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1618 {
1619 ml_sync_all(c == 0, TRUE);
1620 count = 0;
1621 }
1622}
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001625 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001626 * character.
1627 */
1628 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001629merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001630{
1631 int c = c_arg;
1632
zeertzjqbad8a012022-04-29 16:44:00 +01001633 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001634 {
1635 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001636 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001637 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001638 if (c == NUL)
1639 c = K_ZERO;
1640 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001641 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001642 // CTRL-6 is equivalent to CTRL-^
1643 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001644#ifdef FEAT_GUI_GTK
1645 // These mappings look arbitrary at the first glance, but in fact
1646 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1647 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1648 // more sense and is consistent with usual terminal behaviour).
1649 else if (c == '2')
1650 c = NUL;
1651 else if (c >= '3' && c <= '7')
1652 c = c ^ 0x28;
1653 else if (c == '8')
1654 c = BS;
1655 else if (c == '?')
1656 c = DEL;
1657#endif
1658 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001659 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001660 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001661 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001662 && c >= 0 && c <= 127)
1663 {
1664 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001665 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001666 }
1667 return c;
1668}
1669
1670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 * Get the next input character.
1672 * Can return a special key or a multi-byte character.
1673 * Can return NUL when called recursively, use safe_vgetc() if that's not
1674 * wanted.
1675 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1676 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001677 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
1679 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001680vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001684 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001687#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001688 // Do garbage collection when garbagecollect() was called previously and
1689 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001690 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001691 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001692#endif
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 /*
1695 * If a character was put back with vungetc, it was already processed.
1696 * Return it directly.
1697 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001698 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 c = old_char;
1701 old_char = -1;
1702 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001703 mouse_row = old_mouse_row;
1704 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
zeertzjq81b46a62022-04-09 17:58:49 +01001708 // number of characters recorded from the last vgetc() call
1709 static int last_vgetc_recorded_len = 0;
1710
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001711 mod_mask = 0;
1712 vgetc_mod_mask = 0;
1713 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001714
1715 // last_recorded_len can be larger than last_vgetc_recorded_len
1716 // if peeking records more
1717 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001718
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001719 for (;;) // this is done twice if there are modifiers
1720 {
1721 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001722
Bram Moolenaar78aed952022-09-24 15:36:35 +01001723 // No mapping after modifier has been read, using an input method
1724 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001725 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001726#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001727 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001728#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001729#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001730 || popup_no_mapping()
1731#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001732 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001734 ++no_mapping;
1735 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001736 // mod_mask value may change, remember we did the increment
1737 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001739 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001740 if (did_inc)
1741 {
1742 --no_mapping;
1743 --allow_keys;
1744 }
1745
1746 // Get two extra bytes for special keys
1747 if (c == K_SPECIAL
Christopher Plewright605d02a2022-10-19 11:54:46 +01001748#if defined(FEAT_GUI) || defined(MSWIN)
1749 // GUI codes start with CSI; MS-Windows sends mouse scroll
1750 // events with CSI.
1751 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001752#endif
1753 )
1754 {
1755 int save_allow_keys = allow_keys;
1756
1757 ++no_mapping;
1758 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001759 c2 = vgetorpeek(TRUE); // no mapping for these chars
1760 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001761 --no_mapping;
1762 allow_keys = save_allow_keys;
1763 if (c2 == KS_MODIFIER)
1764 {
1765 mod_mask = c;
1766 continue;
1767 }
1768 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar4f974752019-02-17 17:44:42 +01001770#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001771 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1772 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001773 if (
1774# ifdef VIMDLL
1775 gui.in_use &&
1776# endif
1777 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001779 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001780 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001781
1782 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001783 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001784 {
K.Takata54119102022-02-03 13:33:03 +00001785 name[j] = c;
1786 if (j < 199)
1787 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001788 }
K.Takata54119102022-02-03 13:33:03 +00001789 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001790 gui_make_tearoff(name);
1791 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001794#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001795 // GTK: <F10> normally selects the menu, but it's passed until
1796 // here to allow mapping it. Intercept and invoke the GTK
1797 // behavior if it's not mapped.
1798 if (c == K_F10 && gui.menubar != NULL)
1799 {
1800 gtk_menu_shell_select_first(
1801 GTK_MENU_SHELL(gui.menubar), FALSE);
1802 continue;
1803 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001806 // Handle focus event here, so that the caller doesn't need to
1807 // know about it. Return K_IGNORE so that we loop once (needed
1808 // if 'lazyredraw' is set).
1809 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001810 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001811 ui_focus_change(c == K_FOCUSGAINED);
1812 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001813 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001814
1815 // Translate K_CSI to CSI. The special key is only used to
1816 // avoid it being recognized as the start of a special key.
1817 if (c == K_CSI)
1818 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001820#ifdef FEAT_EVAL
1821 if (c == K_SID)
1822 {
1823 int j;
1824
1825 // Handle <SID>{sid}; Do up to 20 digits for safety.
1826 last_used_sid = 0;
1827 for (j = 0; j < 20 && isdigit(c = vgetorpeek(TRUE)); ++j)
1828 last_used_sid = last_used_sid * 10 + (c - '0');
1829 last_used_map = NULL;
1830 continue;
1831 }
1832#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001833 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001834
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001835 // a keypad or special function key was not mapped, use it like
1836 // its ASCII equivalent
1837 switch (c)
1838 {
1839 case K_KPLUS: c = '+'; break;
1840 case K_KMINUS: c = '-'; break;
1841 case K_KDIVIDE: c = '/'; break;
1842 case K_KMULTIPLY: c = '*'; break;
1843 case K_KENTER: c = CAR; break;
1844 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001845#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001846 // Can be either '.' or a ',',
1847 // depending on the type of keypad.
1848 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001849#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001850 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001851#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001852 case K_K0: c = '0'; break;
1853 case K_K1: c = '1'; break;
1854 case K_K2: c = '2'; break;
1855 case K_K3: c = '3'; break;
1856 case K_K4: c = '4'; break;
1857 case K_K5: c = '5'; break;
1858 case K_K6: c = '6'; break;
1859 case K_K7: c = '7'; break;
1860 case K_K8: c = '8'; break;
1861 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001862
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001863 case K_XHOME:
1864 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001865 {
1866 c = K_S_HOME;
1867 mod_mask = 0;
1868 }
1869 else if (mod_mask == MOD_MASK_CTRL)
1870 {
1871 c = K_C_HOME;
1872 mod_mask = 0;
1873 }
1874 else
1875 c = K_HOME;
1876 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001877 case K_XEND:
1878 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001879 {
1880 c = K_S_END;
1881 mod_mask = 0;
1882 }
1883 else if (mod_mask == MOD_MASK_CTRL)
1884 {
1885 c = K_C_END;
1886 mod_mask = 0;
1887 }
1888 else
1889 c = K_END;
1890 break;
1891
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001892 case K_XUP: c = K_UP; break;
1893 case K_XDOWN: c = K_DOWN; break;
1894 case K_XLEFT: c = K_LEFT; break;
1895 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001898 // For a multi-byte character get all the bytes and return the
1899 // converted character.
1900 // Note: This will loop until enough bytes are received!
1901 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1902 {
1903 ++no_mapping;
1904 buf[0] = c;
1905 for (i = 1; i < n; ++i)
1906 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001907 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001908 if (buf[i] == K_SPECIAL
1909#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001910 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001911#endif
1912 )
1913 {
1914 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1915 // sequence, which represents a K_SPECIAL (0x80),
1916 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1917 // represents a CSI (0x9B),
1918 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1919 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001920 c = vgetorpeek(TRUE);
1921 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001922 buf[i] = CSI;
1923 }
1924 }
1925 --no_mapping;
1926 c = (*mb_ptr2char)(buf);
1927 }
1928
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001929 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001930 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001931 vgetc_mod_mask = mod_mask;
1932 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001933 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001934
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001935 break;
1936 }
zeertzjq81b46a62022-04-09 17:58:49 +01001937
1938 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001940
1941#ifdef FEAT_EVAL
1942 /*
1943 * In the main loop "may_garbage_collect" can be set to do garbage
1944 * collection in the first next vgetc(). It's disabled after that to
1945 * avoid internally used Lists and Dicts to be freed.
1946 */
1947 may_garbage_collect = FALSE;
1948#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001949
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001950#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001951 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001952 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001953 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001954 bevalexpr_due_set = FALSE;
1955 ui_remove_balloon();
1956 }
1957#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001958#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001959 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1960 // are filtered.
1961 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001962 {
1963 if (c == Ctrl_C)
1964 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001965 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001966 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001967#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001968
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001969 // Need to process the character before we know it's safe to do something
1970 // else.
1971 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001972 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001973
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001974 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975}
1976
1977/*
1978 * Like vgetc(), but never return a NUL when called recursively, get a key
1979 * directly from the user (ignoring typeahead).
1980 */
1981 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001982safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 int c;
1985
1986 c = vgetc();
1987 if (c == NUL)
1988 c = get_keystroke();
1989 return c;
1990}
1991
1992/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001993 * Like safe_vgetc(), but loop to handle K_IGNORE.
1994 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01001995 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001996 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01001997 static int
1998plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001999{
2000 int c;
2001
2002 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002003 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002004 while (c == K_IGNORE
2005 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2006 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002007 return c;
2008}
2009
2010/*
2011 * Like safe_vgetc(), but loop to handle K_IGNORE.
2012 * Also ignore scrollbar events.
2013 */
2014 int
2015plain_vgetc(void)
2016{
2017 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002018
2019 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002020 // Only handle the first pasted character. Drop the rest, since we
2021 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002022 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2023
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002024 return c;
2025}
2026
2027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 * Check if a character is available, such that vgetc() will not block.
2029 * If the next character is a special character or multi-byte, the returned
2030 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002031 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
2033 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002034vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002036 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002038 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039}
2040
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002041#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042/*
2043 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2044 * codes.
2045 */
2046 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002047vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 int c;
2050
2051 ++no_mapping;
2052 ++allow_keys;
2053 c = vpeekc();
2054 --no_mapping;
2055 --allow_keys;
2056 return c;
2057}
2058#endif
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060/*
2061 * Check if any character is available, also half an escape sequence.
2062 * Trick: when no typeahead found, but there is something in the typeahead
2063 * buffer, it must be an ESC that is recognized as the start of a key code.
2064 */
2065 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002066vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 int c;
2069
2070 c = vpeekc();
2071 if (c == NUL && typebuf.tb_len > 0)
2072 c = ESC;
2073 return c;
2074}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076/*
2077 * Call vpeekc() without causing anything to be mapped.
2078 * Return TRUE if a character is available, FALSE otherwise.
2079 */
2080 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002081char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 int retval;
2084
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002085#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002086 // When test_override("char_avail", 1) was called pretend there is no
2087 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002088 if (disable_char_avail_for_testing)
2089 return FALSE;
2090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 ++no_mapping;
2092 retval = vpeekc();
2093 --no_mapping;
2094 return (retval != NUL);
2095}
2096
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002097#if defined(FEAT_EVAL) || defined(PROTO)
2098/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002099 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002100 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002101 static void
2102getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002103{
2104 varnumber_T n;
2105 int error = FALSE;
2106
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002107 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2108 return;
2109
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002110#ifdef MESSAGE_QUEUE
2111 // vpeekc() used to check for messages, but that caused problems, invoking
2112 // a callback where it was not expected. Some plugins use getchar(1) in a
2113 // loop to await a message, therefore make sure we check for messages here.
2114 parse_queued_messages();
2115#endif
2116
Bram Moolenaar30613902019-12-01 22:11:18 +01002117 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002118 windgoto(msg_row, msg_col);
2119
2120 ++no_mapping;
2121 ++allow_keys;
2122 for (;;)
2123 {
2124 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002125 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002126 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002127 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002128 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002129 n = vpeekc_any();
2130 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002131 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002132 n = 0;
2133 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002134 // getchar(0) and char avail() != NUL: get a character.
2135 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2136 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002137
Bram Moolenaar15183b42020-09-05 19:59:39 +02002138 if (n == K_IGNORE || n == K_MOUSEMOVE
2139 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002140 continue;
2141 break;
2142 }
2143 --no_mapping;
2144 --allow_keys;
2145
2146 set_vim_var_nr(VV_MOUSE_WIN, 0);
2147 set_vim_var_nr(VV_MOUSE_WINID, 0);
2148 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2149 set_vim_var_nr(VV_MOUSE_COL, 0);
2150
2151 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002152 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002154 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002155 int i = 0;
2156
Bram Moolenaar30613902019-12-01 22:11:18 +01002157 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002158 if (mod_mask != 0)
2159 {
2160 temp[i++] = K_SPECIAL;
2161 temp[i++] = KS_MODIFIER;
2162 temp[i++] = mod_mask;
2163 }
2164 if (IS_SPECIAL(n))
2165 {
2166 temp[i++] = K_SPECIAL;
2167 temp[i++] = K_SECOND(n);
2168 temp[i++] = K_THIRD(n);
2169 }
2170 else if (has_mbyte)
2171 i += (*mb_char2bytes)(n, temp + i);
2172 else
2173 temp[i++] = n;
2174 temp[i++] = NUL;
2175 rettv->v_type = VAR_STRING;
2176 rettv->vval.v_string = vim_strsave(temp);
2177
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002178 if (is_mouse_key(n))
2179 {
2180 int row = mouse_row;
2181 int col = mouse_col;
2182 win_T *win;
2183 linenr_T lnum;
2184 win_T *wp;
2185 int winnr = 1;
2186
2187 if (row >= 0 && col >= 0)
2188 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002189 // Find the window at the mouse coordinates and compute the
2190 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002191 win = mouse_find_win(&row, &col, FIND_POPUP);
2192 if (win == NULL)
2193 return;
2194 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002195#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002196 if (WIN_IS_POPUP(win))
2197 winnr = 0;
2198 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002199#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002200 for (wp = firstwin; wp != win && wp != NULL;
2201 wp = wp->w_next)
2202 ++winnr;
2203 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2204 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2205 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2206 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2207 }
2208 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002209 }
2210}
2211
2212/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002213 * "getchar()" function
2214 */
2215 void
2216f_getchar(typval_T *argvars, typval_T *rettv)
2217{
2218 getchar_common(argvars, rettv);
2219}
2220
2221/*
2222 * "getcharstr()" function
2223 */
2224 void
2225f_getcharstr(typval_T *argvars, typval_T *rettv)
2226{
2227 getchar_common(argvars, rettv);
2228
2229 if (rettv->v_type == VAR_NUMBER)
2230 {
2231 char_u temp[7]; // mbyte-char: 6, NUL: 1
2232 varnumber_T n = rettv->vval.v_number;
2233 int i = 0;
2234
2235 if (n != 0)
2236 {
2237 if (has_mbyte)
2238 i += (*mb_char2bytes)(n, temp + i);
2239 else
2240 temp[i++] = n;
2241 }
2242 temp[i++] = NUL;
2243 rettv->v_type = VAR_STRING;
2244 rettv->vval.v_string = vim_strsave(temp);
2245 }
2246}
2247
2248/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002249 * "getcharmod()" function
2250 */
2251 void
2252f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2253{
2254 rettv->vval.v_number = mod_mask;
2255}
2256#endif // FEAT_EVAL
2257
2258#if defined(MESSAGE_QUEUE) || defined(PROTO)
2259# define MAX_REPEAT_PARSE 8
2260
2261/*
2262 * Process messages that have been queued for netbeans or clientserver.
2263 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002264 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002265 * it is safe to do so.
2266 */
2267 void
2268parse_queued_messages(void)
2269{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002270 int old_curwin_id;
2271 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002272 int i;
2273 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002274 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002275 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002276
2277 // Do not handle messages while redrawing, because it may cause buffers to
2278 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002279 // Also bail out when parsing messages was explicitly disabled.
2280 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002281 return;
2282
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002283 // If memory allocation fails during startup we'll exit but curbuf or
2284 // curwin could be NULL.
2285 if (curbuf == NULL || curwin == NULL)
2286 return;
2287
2288 old_curbuf_fnum = curbuf->b_fnum;
2289 old_curwin_id = curwin->w_id;
2290
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002291 ++entered;
2292
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002293 // may_garbage_collect is set in main_loop() to do garbage collection when
2294 // blocking to wait on a character. We don't want that while parsing
2295 // messages, a callback may invoke vgetc() while lists and dicts are in use
2296 // in the call stack.
2297 may_garbage_collect = FALSE;
2298
2299 // Loop when a job ended, but don't keep looping forever.
2300 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2301 {
2302 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002303# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002304 channel_handle_events(FALSE);
2305# endif
2306
2307# ifdef FEAT_NETBEANS_INTG
2308 // Process the queued netbeans messages.
2309 netbeans_parse_messages();
2310# endif
2311# ifdef FEAT_JOB_CHANNEL
2312 // Write any buffer lines still to be written.
2313 channel_write_any_lines();
2314
2315 // Process the messages queued on channels.
2316 channel_parse_messages();
2317# endif
2318# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2319 // Process the queued clientserver messages.
2320 server_parse_messages();
2321# endif
2322# ifdef FEAT_JOB_CHANNEL
2323 // Check if any jobs have ended. If so, repeat the above to handle
2324 // changes, e.g. stdin may have been closed.
2325 if (job_check_ended())
2326 continue;
2327# endif
2328# ifdef FEAT_TERMINAL
2329 free_unused_terminals();
2330# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002331
2332# ifdef FEAT_SOUND_MACOSX
2333 process_cfrunloop();
2334# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002335# ifdef FEAT_SOUND_CANBERRA
2336 if (has_sound_callback_in_queue())
2337 invoke_sound_callback();
2338# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002339#ifdef SIGUSR1
2340 if (got_sigusr1)
2341 {
2342 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2343 got_sigusr1 = FALSE;
2344 }
2345#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002346 break;
2347 }
2348
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002349 // When not nested we'll go back to waiting for a typed character. If it
2350 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002351 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002352 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002353
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002354 may_garbage_collect = save_may_garbage_collect;
2355
2356 // If the current window or buffer changed we need to bail out of the
2357 // waiting loop. E.g. when a job exit callback closes the terminal window.
2358 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002359 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002360
2361 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002362}
2363#endif
2364
2365
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002366typedef enum {
2367 map_result_fail, // failed, break loop
2368 map_result_get, // get a character from typeahead
2369 map_result_retry, // try to map again
2370 map_result_nomatch // no matching mapping, get char
2371} map_result_T;
2372
2373/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002374 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002375 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002376 */
2377 static int
zeertzjqee446032022-04-30 15:02:22 +01002378at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002379{
2380 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2381 int c = *p;
2382
2383 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002384 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002385 && p[1] == KS_MODIFIER
2386 && (p[2] & MOD_MASK_CTRL))
2387 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002388 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2389 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002390}
2391
2392/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002393 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002394 * into just a key, apply that.
2395 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2396 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002397 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002398 */
2399 static int
2400check_simplify_modifier(int max_offset)
2401{
2402 int offset;
2403 char_u *tp;
2404
2405 for (offset = 0; offset < max_offset; ++offset)
2406 {
2407 if (offset + 3 >= typebuf.tb_len)
2408 break;
2409 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002410 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002411 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002412 // A modifier was not used for a mapping, apply it to ASCII keys.
2413 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002414 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002415 int c = tp[3];
2416 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002417
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002418 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002419 {
2420 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002421 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002422
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002423 if (offset == 0)
2424 {
2425 // At the start: remember the character and mod_mask before
2426 // merging, in some cases, e.g. at the hit-return prompt,
2427 // they are put back in the typeahead buffer.
2428 vgetc_char = c;
2429 vgetc_mod_mask = tp[2];
2430 }
zeertzjq12e21e32022-04-27 11:58:01 +01002431 if (IS_SPECIAL(new_c))
2432 {
2433 new_string[0] = K_SPECIAL;
2434 new_string[1] = K_SECOND(new_c);
2435 new_string[2] = K_THIRD(new_c);
2436 len = 3;
2437 }
2438 else
2439 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002440 if (modifier == 0)
2441 {
2442 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002443 NULL, 0, NULL) == FAIL)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002444 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002445 }
2446 else
2447 {
2448 tp[2] = modifier;
2449 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002450 NULL, 0, NULL) == FAIL)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002451 return -1;
2452 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002453 return len;
2454 }
2455 }
2456 }
2457 return 0;
2458}
2459
2460/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002461 * Handle mappings in the typeahead buffer.
2462 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002463 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002464 * - When there is no match yet, return map_result_nomatch, need to get more
2465 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002466 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002467 */
2468 static int
2469handle_mapping(
2470 int *keylenp,
2471 int *timedout,
2472 int *mapdepth)
2473{
2474 mapblock_T *mp = NULL;
2475 mapblock_T *mp2;
2476 mapblock_T *mp_match;
2477 int mp_match_len = 0;
2478 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002479 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002480 int tb_c1;
2481 int mlen;
2482#ifdef FEAT_LANGMAP
2483 int nolmaplen;
2484#endif
2485 int keylen = *keylenp;
2486 int i;
2487 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002488 int is_plug_map = FALSE;
2489
zeertzjqbb404f52022-07-23 06:25:29 +01002490 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002491 if (typebuf.tb_len >= 3
2492 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002493 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2494 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2495 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002496
2497 /*
2498 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002499 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002500 *
2501 * Don't look for mappings if:
2502 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2503 * - maphash_valid not set: no mappings present.
2504 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2505 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002506 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002507 * - waiting for a char with --more--
2508 * - in Ctrl-X mode, and we get a valid char for that mode
2509 */
2510 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2511 if (no_mapping == 0 && is_maphash_valid()
2512 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002513 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002514 || (p_remap
2515 && (typebuf.tb_noremap[typebuf.tb_off]
2516 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002517 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2518 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2519 && State != MODE_ASKMORE
2520 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002521 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002522 {
Christopher Plewright4c366782022-10-20 13:11:15 +01002523#if defined(FEAT_GUI) || defined(MSWIN)
2524 if (tb_c1 == CSI
2525# if !defined(MSWIN)
2526 && gui.in_use
2527# endif
2528 && typebuf.tb_len >= 2
2529 && (typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER
2530# if defined(MSWIN)
2531 || (typebuf.tb_len >= 3
Christopher Plewright7fa02bc2022-10-21 13:03:33 +01002532# ifdef FEAT_GUI
2533 && !gui.in_use
2534# endif
Christopher Plewright4c366782022-10-20 13:11:15 +01002535 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2536 && (typebuf.tb_buf[typebuf.tb_off + 2] == KE_MOUSEUP
2537 || typebuf.tb_buf[typebuf.tb_off + 2] == KE_MOUSEDOWN
2538 || typebuf.tb_buf[typebuf.tb_off + 2] == KE_MOUSELEFT
2539 || typebuf.tb_buf[typebuf.tb_off + 2] == KE_MOUSERIGHT)
2540 )
2541# endif
2542 )
2543 )
Bram Moolenaarc9983702020-05-28 21:03:53 +02002544 {
2545 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2546 // K_SPECIAL KS_MODIFIER {flags}.
Christopher Plewright4c366782022-10-20 13:11:15 +01002547 // MS-Windows sends mouse scroll events CSI KS_EXTRA {what}, but
Christopher Plewright7fa02bc2022-10-21 13:03:33 +01002548 // non-GUI mappings expect K_SPECIAL KS_EXTRA {what}.
Bram Moolenaarc9983702020-05-28 21:03:53 +02002549 tb_c1 = K_SPECIAL;
2550 }
2551#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002552#ifdef FEAT_LANGMAP
2553 if (tb_c1 == K_SPECIAL)
2554 nolmaplen = 2;
2555 else
2556 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002557 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2558 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002559 nolmaplen = 0;
2560 }
2561#endif
2562 // First try buffer-local mappings.
2563 mp = get_buf_maphash_list(local_State, tb_c1);
2564 mp2 = get_maphash_list(local_State, tb_c1);
2565 if (mp == NULL)
2566 {
2567 // There are no buffer-local mappings.
2568 mp = mp2;
2569 mp2 = NULL;
2570 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002571
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002572 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002573 * Loop until a partly matching mapping is found or all (local)
2574 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002576 * A full match is only accepted if there is no partly match, so "aa"
2577 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002578 */
2579 mp_match = NULL;
2580 mp_match_len = 0;
2581 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002582 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002583 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002584 // Only consider an entry if the first character matches and it is
2585 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002586 // Skip ":lmap" mappings if keys were mapped.
2587 if (mp->m_keys[0] == tb_c1
2588 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002589 && !(mp->m_simplified && seenModifyOtherKeys
2590 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002591 && ((mp->m_mode & MODE_LANGMAP) == 0
2592 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002593 {
2594#ifdef FEAT_LANGMAP
2595 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002596 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002597#endif
2598 // find the match length of this mapping
2599 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2600 {
zeertzjq49660f52022-10-20 17:59:38 +01002601 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002602#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002603 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002604 {
2605 if (nomap == 2 && c2 == KS_MODIFIER)
2606 modifiers = 1;
2607 else if (nomap == 1 && modifiers == 1)
2608 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002610 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002611 else
zeertzjq49660f52022-10-20 17:59:38 +01002612 {
2613 if (c2 == K_SPECIAL)
2614 nomap = 2;
2615 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2616 // Only apply 'langmap' if merging modifiers into
2617 // the key will not result in another character,
2618 // so that 'langmap' behaves consistently in
2619 // different terminals and GUIs.
2620 LANGMAP_ADJUST(c2, TRUE);
2621 modifiers = 0;
2622 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002623#endif
zeertzjq49660f52022-10-20 17:59:38 +01002624 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002625 break;
2626 }
2627
Bram Moolenaareda35f72019-08-03 14:59:44 +02002628 // Don't allow mapping the first byte(s) of a multi-byte char.
2629 // Happens when mapping <M-a> and then changing 'encoding'.
2630 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002631 {
2632 char_u *p1 = mp->m_keys;
2633 char_u *p2 = mb_unescape(&p1);
2634
2635 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002636 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002637 mlen = 0;
2638 }
2639
2640 // Check an entry whether it matches.
2641 // - Full match: mlen == keylen
2642 // - Partly match: mlen == typebuf.tb_len
2643 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002644 if (mlen == keylen || (mlen == typebuf.tb_len
2645 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002646 {
2647 char_u *s;
2648 int n;
2649
Bram Moolenaareda35f72019-08-03 14:59:44 +02002650 // If only script-local mappings are allowed, check if the
2651 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002652 s = typebuf.tb_noremap + typebuf.tb_off;
2653 if (*s == RM_SCRIPT
2654 && (mp->m_keys[0] != K_SPECIAL
2655 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002656 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002657 continue;
2658
Bram Moolenaareda35f72019-08-03 14:59:44 +02002659 // If one of the typed keys cannot be remapped, skip the
2660 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002661 for (n = mlen; --n >= 0; )
2662 if (*s++ & (RM_NONE|RM_ABBR))
2663 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002664 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002665 continue;
2666
2667 if (keylen > typebuf.tb_len)
2668 {
2669 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002670 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002671 {
2672 // break at a partly match
2673 keylen = KEYLEN_PART_MAP;
2674 break;
2675 }
2676 }
2677 else if (keylen > mp_match_len)
2678 {
2679 // found a longer match
2680 mp_match = mp;
2681 mp_match_len = keylen;
2682 }
2683 }
2684 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002685 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002686 // character. If the first character that didn't match is
2687 // K_SPECIAL then check for a termcode. This isn't perfect
2688 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002689 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002690 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002691 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002692 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2693 }
2694 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2695 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002696 }
2697 }
2698
Bram Moolenaareda35f72019-08-03 14:59:44 +02002699 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002700 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002701 {
2702 mp = mp_match;
2703 keylen = mp_match_len;
2704 }
2705 }
2706
2707 /*
2708 * Check for match with 'pastetoggle'
2709 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002710 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002711 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002712 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2713 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002714 break;
2715 if (p_pt[mlen] == NUL) // match
2716 {
2717 // write chars to script file(s)
2718 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002719 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2720 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002721
2722 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002723 set_option_value_give_err((char_u *)"paste",
2724 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002725 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002726 {
2727 msg_col = 0;
2728 msg_row = Rows - 1;
2729 msg_clr_eos(); // clear ruler
2730 }
2731 status_redraw_all();
2732 redraw_statuslines();
2733 showmode();
2734 setcursor();
2735 *keylenp = keylen;
2736 return map_result_retry;
2737 }
2738 // Need more chars for partly match.
2739 if (mlen == typebuf.tb_len)
2740 keylen = KEYLEN_PART_KEY;
2741 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002742 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 max_mlen = mlen + 1;
2744 }
2745
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002746 // May check for a terminal code when there is no mapping or only a partial
2747 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2748 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002749 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002750 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2751 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002752 {
2753 int save_keylen = keylen;
2754
2755 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002756 * When no matching mapping found or found a non-matching mapping that
2757 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002758 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002759 * - mapping is allowed,
2760 * - keys have not been mapped,
2761 * - and not an ESC sequence, not in insert mode or p_ek is on,
2762 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002763 */
zeertzjq68a573c2022-04-28 14:10:01 +01002764 if (no_mapping == 0 || allow_keys != 0)
2765 {
2766 if ((typebuf.tb_maplen == 0
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002767 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002768 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002769 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002770 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2771 else
2772 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002773
Bram Moolenaar0684e362020-12-03 19:54:42 +01002774 // If no termcode matched but 'pastetoggle' matched partially
2775 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002776 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002777 keylen = KEYLEN_PART_KEY;
2778
Bram Moolenaar975a8802020-06-06 22:36:24 +02002779 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002780 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002781#ifdef FEAT_TERMINAL
2782 check_no_reduce_keys(); // may update the no_reduce_keys flag
2783#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002784 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002785 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002786 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002787 if (keylen < 0)
2788 // ins_typebuf() failed
2789 return map_result_fail;
2790 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002791
Bram Moolenaareda35f72019-08-03 14:59:44 +02002792 // When getting a partial match, but the last characters were not
2793 // typed, don't wait for a typed character to complete the
2794 // termcode. This helps a lot when a ":normal" command ends in an
2795 // ESC.
2796 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002797 keylen = 0;
2798 }
2799 else
2800 keylen = 0;
2801 if (keylen == 0) // no matching terminal code
2802 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002803#ifdef AMIGA
2804 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002805 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002806 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002807 {
2808 char_u *s;
2809
2810 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002811 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2812 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002813 ++s)
2814 ;
2815 if (*s == 'r' || *s == '|') // found one
2816 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002817 del_typebuf(
2818 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002819 // get size and redraw screen
2820 shell_resized();
2821 *keylenp = keylen;
2822 return map_result_retry;
2823 }
2824 if (*s == NUL) // need more characters
2825 keylen = KEYLEN_PART_KEY;
2826 }
2827 if (keylen >= 0)
2828#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002829 // When there was a matching mapping and no termcode could be
2830 // replaced after another one, use that mapping (loop around).
2831 // If there was no mapping at all use the character from the
2832 // typeahead buffer right here.
2833 if (mp == NULL)
2834 {
2835 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002836 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002837 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002838 }
2839
2840 if (keylen > 0) // full matching terminal code
2841 {
2842#if defined(FEAT_GUI) && defined(FEAT_MENU)
2843 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002844 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2845 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002846 {
2847 int idx;
2848
Bram Moolenaareda35f72019-08-03 14:59:44 +02002849 // Using a menu may cause a break in undo! It's like using
2850 // gotchars(), but without recording or writing to a script
2851 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002852 may_sync_undo();
2853 del_typebuf(3, 0);
2854 idx = get_menu_index(current_menu, local_State);
2855 if (idx != MENU_INDEX_INVALID)
2856 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002857 // In Select mode and a Visual mode menu is used: Switch
2858 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 // back to Select mode.
2860 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002861 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002862 {
2863 VIsual_select = FALSE;
2864 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002865 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002866 }
2867 ins_typebuf(current_menu->strings[idx],
2868 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002869 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002870 }
2871 }
2872#endif // FEAT_GUI && FEAT_MENU
2873 *keylenp = keylen;
2874 return map_result_retry; // try mapping again
2875 }
2876
Bram Moolenaareda35f72019-08-03 14:59:44 +02002877 // Partial match: get some more characters. When a matching mapping
2878 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002879 if (mp == NULL || keylen < 0)
2880 keylen = KEYLEN_PART_KEY;
2881 else
2882 keylen = mp_match_len;
2883 }
2884
2885 /*
2886 * complete match
2887 */
2888 if (keylen >= 0 && keylen <= typebuf.tb_len)
2889 {
2890 char_u *map_str;
2891
2892#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002893 int save_m_expr;
2894 int save_m_noremap;
2895 int save_m_silent;
2896 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002897#else
2898# define save_m_noremap mp->m_noremap
2899# define save_m_silent mp->m_silent
2900#endif
2901
2902 // write chars to script file(s)
2903 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002904 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2905 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002906
2907 cmd_silent = (typebuf.tb_silent > 0);
2908 del_typebuf(keylen, 0); // remove the mapped keys
2909
2910 /*
2911 * Put the replacement string in front of mapstr.
2912 * The depth check catches ":map x y" and ":map y x".
2913 */
2914 if (++*mapdepth >= p_mmd)
2915 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002916 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002917 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002918 redrawcmdline();
2919 else
2920 setcursor();
2921 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002922 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002923 *keylenp = keylen;
2924 return map_result_fail;
2925 }
2926
2927 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002928 * In Select mode and a Visual mode mapping is used: Switch to Visual
2929 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002930 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002931 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002932 {
2933 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002934 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002935 }
2936
2937#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002938 // Copy the values from *mp that are used, because evaluating the
2939 // expression may invoke a function that redefines the mapping, thereby
2940 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002941 save_m_expr = mp->m_expr;
2942 save_m_noremap = mp->m_noremap;
2943 save_m_silent = mp->m_silent;
2944 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002945
2946 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002947 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2948 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002949 */
2950 if (mp->m_expr)
2951 {
2952 int save_vgetc_busy = vgetc_busy;
2953 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002954 int was_screen_col = screen_cur_col;
2955 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002956 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002957
2958 vgetc_busy = 0;
2959 may_garbage_collect = FALSE;
2960
2961 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002962 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002963
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002964 // The mapping may do anything, but we expect it to take care of
2965 // redrawing. Do put the cursor back where it was.
2966 windgoto(was_screen_row, was_screen_col);
2967 out_flush();
2968
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002969 // If an error was displayed and the expression returns an empty
2970 // string, generate a <Nop> to allow for a redraw.
2971 if (prev_did_emsg != did_emsg
2972 && (map_str == NULL || *map_str == NUL))
2973 {
2974 char_u buf[4];
2975
2976 vim_free(map_str);
2977 buf[0] = K_SPECIAL;
2978 buf[1] = KS_EXTRA;
2979 buf[2] = KE_IGNORE;
2980 buf[3] = NUL;
2981 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01002982 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002983 {
2984 // redraw the command below the error
2985 msg_didout = TRUE;
2986 if (msg_row < cmdline_row)
2987 msg_row = cmdline_row;
2988 redrawcmd();
2989 }
2990 }
2991
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002992 vgetc_busy = save_vgetc_busy;
2993 may_garbage_collect = save_may_garbage_collect;
2994 }
2995 else
2996#endif
2997 map_str = mp->m_str;
2998
2999 /*
3000 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003001 * If 'from' field is the same as the start of the 'to' field, don't
3002 * remap the first character (but do allow abbreviations).
3003 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003004 */
3005 if (map_str == NULL)
3006 i = FAIL;
3007 else
3008 {
3009 int noremap;
3010
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003011#ifdef FEAT_EVAL
3012 last_used_map = mp;
3013 last_used_sid = -1;
3014#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003015 if (save_m_noremap != REMAP_YES)
3016 noremap = save_m_noremap;
3017 else if (
3018#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003019 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3020 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003021#else
3022 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3023#endif
3024 != 0)
3025 noremap = REMAP_YES;
3026 else
3027 noremap = REMAP_SKIP;
3028 i = ins_typebuf(map_str, noremap,
3029 0, TRUE, cmd_silent || save_m_silent);
3030#ifdef FEAT_EVAL
3031 if (save_m_expr)
3032 vim_free(map_str);
3033#endif
3034 }
3035#ifdef FEAT_EVAL
3036 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003037#endif
3038 *keylenp = keylen;
3039 if (i == FAIL)
3040 return map_result_fail;
3041 return map_result_retry;
3042 }
3043
3044 *keylenp = keylen;
3045 return map_result_nomatch;
3046}
3047
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003048/*
3049 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003050 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003051 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003054vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
3056 old_char = c;
3057 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003058 old_mouse_row = mouse_row;
3059 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003060 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061}
3062
3063/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003064 * When peeking and not getting a character, reg_executing cannot be cleared
3065 * yet, so set a flag to clear it later.
3066 */
3067 static void
3068check_end_reg_executing(int advance)
3069{
3070 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3071 || pending_end_reg_executing))
3072 {
3073 if (advance)
3074 {
3075 reg_executing = 0;
3076 pending_end_reg_executing = FALSE;
3077 }
3078 else
3079 pending_end_reg_executing = TRUE;
3080 }
3081
3082}
3083
3084/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003085 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * 1. from the stuffbuffer
3087 * This is used for abbreviated commands like "D" -> "d$".
3088 * Also used to redo a command for ".".
3089 * 2. from the typeahead buffer
3090 * Stores text obtained previously but not used yet.
3091 * Also stores the result of mappings.
3092 * Also used for the ":normal" command.
3093 * 3. from the user
3094 * This may do a blocking wait if "advance" is TRUE.
3095 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003096 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003097 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 * KeyTyped is set to TRUE in the case the user typed the key.
3099 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003100 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003101 * Just look whether there is a character available.
3102 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 *
3104 * When "no_mapping" is zero, checks for mappings in the current mode.
3105 * Only returns one byte (of a multi-byte character).
3106 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3107 */
3108 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003109vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 int c, c1;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003112 int timedout = FALSE; // waited for more than 'timeoutlen'
3113 // for mapping to complete or
3114 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003115 int mapdepth = 0; // check for recursive mapping
3116 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003119 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#endif
3121 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003123 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125 /*
3126 * This function doesn't work very well when called recursively. This may
3127 * happen though, because of:
3128 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3129 * there is a character available, which calls this function. In that
3130 * case we must return NUL, to indicate no character is available.
3131 * 2. A GUI callback function writes to the screen, causing a
3132 * wait_return().
3133 * Using ":normal" can also do this, but it saves the typeahead buffer,
3134 * thus it should be OK. But don't get a key from the user then.
3135 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003136 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 return NUL;
3138
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003139 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003144 typebuf_was_empty = FALSE;
3145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 init_typebuf();
3148 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003149 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 do
3151 {
3152/*
3153 * get a character: 1. from the stuffbuffer
3154 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003155 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 c = typeahead_char;
3158 if (advance)
3159 typeahead_char = 0;
3160 }
3161 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003162 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (c != NUL && !got_int)
3164 {
3165 if (advance)
3166 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003167 // KeyTyped = FALSE; When the command that stuffed something
3168 // was typed, behave like the stuffed command was typed.
3169 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 KeyStuffed = TRUE;
3171 }
3172 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003173 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 else
3176 {
3177 /*
3178 * Loop until we either find a matching mapped key, or we
3179 * are sure that it is not a mapped key.
3180 * If a mapped key sequence is found we go back to the start to
3181 * try re-mapping.
3182 */
3183 for (;;)
3184 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003185 long wait_time;
3186 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003187 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003188 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 /*
3190 * ui_breakcheck() is slow, don't use it too often when
3191 * inside a mapping. But call it each time for typed
3192 * characters.
3193 */
3194 if (typebuf.tb_maplen)
3195 line_breakcheck();
3196 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003197 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (got_int)
3199 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003200 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003201 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 /*
3204 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003205 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 * Otherwise we behave like having gotten a CTRL-C.
3207 * As a result typing CTRL-C in insert mode will
3208 * really insert a CTRL-C.
3209 */
3210 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003211 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 c = ESC;
3213 else
3214 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003215 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003217 if (advance)
3218 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003219 // Also record this character, it might be needed to
3220 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003221 *typebuf.tb_buf = c;
3222 gotchars(typebuf.tb_buf, 1);
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 cmd_silent = FALSE;
3225
3226 break;
3227 }
3228 else if (typebuf.tb_len > 0)
3229 {
3230 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003231 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003233 map_result_T result = handle_mapping(
3234 &keylen, &timedout, &mapdepth);
3235
3236 if (result == map_result_retry)
3237 // try mapping again
3238 continue;
3239 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003240 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003241 // failed, use the outer loop
3242 c = -1;
3243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003245 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247/*
3248 * get a character: 2. from the typeahead buffer
3249 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003250 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003251 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003252 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003253 cmd_silent = (typebuf.tb_silent > 0);
3254 if (typebuf.tb_maplen > 0)
3255 KeyTyped = FALSE;
3256 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003257 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003258 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003259 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003260 gotchars(typebuf.tb_buf
3261 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003262 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003263 KeyNoremap = typebuf.tb_noremap[
3264 typebuf.tb_off];
3265 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003266 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003267 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003270 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272
3273/*
3274 * get a character: 3. from the user - handle <Esc> in Insert mode
3275 */
3276 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003277 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 * are no more characters at once, we pretend to go out of
3279 * insert mode. This prevents the one second delay after
3280 * typing an <ESC>. If we get something after all, we may
3281 * have to redisplay the mode. That the cursor is in the wrong
3282 * place does not matter.
3283 */
3284 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 new_wcol = curwin->w_wcol;
3286 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if ( advance
3288 && typebuf.tb_len == 1
3289 && typebuf.tb_buf[typebuf.tb_off] == ESC
3290 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003293 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003294 && (p_timeout
3295 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003297 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003299 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 char_u *ptr;
3301
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003302 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 unshowmode(TRUE);
3305 mode_deleted = TRUE;
3306 }
3307#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003308 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003309 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 int save_State;
3312
3313 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003314 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 gui_update_cursor(TRUE, FALSE);
3316 State = save_State;
3317 shape_changed = TRUE;
3318 }
3319#endif
3320 validate_cursor();
3321 old_wcol = curwin->w_wcol;
3322 old_wrow = curwin->w_wrow;
3323
Bram Moolenaar30613902019-12-01 22:11:18 +01003324 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (curwin->w_cursor.col != 0)
3326 {
3327 if (curwin->w_wcol > 0)
3328 {
3329 if (did_ai)
3330 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003331 chartabsize_T cts;
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 /*
3334 * We are expecting to truncate the trailing
3335 * white-space, so find the last non-white
3336 * character -- webb
3337 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003338 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003340 init_chartabsize_arg(&cts, curwin,
3341 curwin->w_cursor.lnum, 0, ptr, ptr);
3342 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003344 if (!VIM_ISWHITE(*cts.cts_ptr))
3345 curwin->w_wcol = cts.cts_vcol;
3346 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003348 cts.cts_ptr +=
3349 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003351 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003353 clear_chartabsize_arg(&cts);
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003356 + curwin->w_wcol / curwin->w_width;
3357 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003359 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 else
3362 {
3363 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 }
3367 else if (curwin->w_p_wrap && curwin->w_wrow)
3368 {
3369 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003370 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3374 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003375 // Correct when the cursor is on the right halve
3376 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 ptr = ml_get_curline();
3378 col -= (*mb_head_off)(ptr, ptr + col);
3379 if ((*mb_ptr2cells)(ptr + col) > 1)
3380 --curwin->w_wcol;
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383 setcursor();
3384 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 new_wcol = curwin->w_wcol;
3386 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 curwin->w_wcol = old_wcol;
3388 curwin->w_wrow = old_wrow;
3389 }
3390 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003391 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003392
Bram Moolenaar30613902019-12-01 22:11:18 +01003393 // Allow mapping for just typed characters. When we get here c
3394 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003395 for (n = 1; n <= c; ++n)
3396 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 typebuf.tb_len += c;
3398
Bram Moolenaar30613902019-12-01 22:11:18 +01003399 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3401 {
3402 timedout = TRUE;
3403 continue;
3404 }
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (ex_normal_busy > 0)
3407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar30613902019-12-01 22:11:18 +01003410 // No typeahead left and inside ":normal". Must return
3411 // something to avoid getting stuck. When an incomplete
3412 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (typebuf.tb_len > 0)
3414 {
3415 timedout = TRUE;
3416 continue;
3417 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003418
Bram Moolenaar30613902019-12-01 22:11:18 +01003419 // When 'insertmode' is set, ESC just beeps in Insert
3420 // mode. Use CTRL-L to make edit() return.
3421 // For the command line only CTRL-C always breaks it.
3422 // For the cmdline window: Alternate between ESC and
3423 // CTRL-C: ESC for most situations and CTRL-C to close the
3424 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003425 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003427#ifdef FEAT_TERMINAL
3428 else if (terminal_is_active())
3429 c = K_CANCEL;
3430#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003431 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003432 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 c = Ctrl_C;
3434 else
3435 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003437 // set a flag to indicate this wasn't a normal char
3438 if (advance)
3439 typebuf_was_empty = TRUE;
3440
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003441 // return from main_loop()
3442 if (pending_exmode_active)
3443 exmode_active = EXMODE_NORMAL;
3444
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003445 // no chars to block abbreviation for
3446 typebuf.tb_no_abbr_cnt = 0;
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 break;
3449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451/*
3452 * get a character: 3. from the user - update display
3453 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003454 // In insert mode a screen update is skipped when characters
3455 // are still available. But when those available characters
3456 // are part of a mapping, and we are going to do a blocking
3457 // wait here. Need to update the screen to display the
3458 // changed text so far. Also for when 'lazyredraw' is set and
3459 // redrawing was postponed because there was something in the
3460 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003461 if (((State & MODE_INSERT) != 0 || p_lz)
3462 && (State & MODE_CMDLINE) == 0
3463 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003466 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468
3469 /*
3470 * If we have a partial match (and are going to wait for more
3471 * input from the user), show the partially matched characters
3472 * to the user with showcmd.
3473 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003474 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 c1 = 0;
3476 if (typebuf.tb_len > 0 && advance && !exmode_active)
3477 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003478 if (((State & (MODE_NORMAL | MODE_INSERT))
3479 || State == MODE_LANGMAP)
3480 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003482 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003483 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3485 + typebuf.tb_len - 1) == 1)
3486 {
3487 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3488 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003489 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 c1 = 1;
3491 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003492 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 old_wcol = curwin->w_wcol;
3494 old_wrow = curwin->w_wrow;
3495 curwin->w_wcol = new_wcol;
3496 curwin->w_wrow = new_wrow;
3497 push_showcmd();
3498 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003499 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3500 while (showcmd_idx < typebuf.tb_len)
3501 (void)add_to_showcmd(
3502 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 curwin->w_wcol = old_wcol;
3504 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506
Bram Moolenaar30613902019-12-01 22:11:18 +01003507 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003508 if ((State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3510 && cmdline_star == 0
3511#endif
3512 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3513 + typebuf.tb_len - 1) == 1)
3514 {
3515 putcmdline(typebuf.tb_buf[typebuf.tb_off
3516 + typebuf.tb_len - 1], FALSE);
3517 c1 = 1;
3518 }
3519 }
3520
3521/*
3522 * get a character: 3. from the user - get it
3523 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003524 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003525 // timedout may have been set if a mapping with empty RHS
3526 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003527 timedout = FALSE;
3528
Bram Moolenaar652de232019-04-04 20:13:09 +02003529 if (advance)
3530 {
3531 if (typebuf.tb_len == 0
3532 || !(p_timeout
3533 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3534 // blocking wait
3535 wait_time = -1L;
3536 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3537 wait_time = p_ttm;
3538 else
3539 wait_time = p_tm;
3540 }
3541 else
3542 wait_time = 0;
3543
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003544 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3546 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003547 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaareda35f72019-08-03 14:59:44 +02003549 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 pop_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (c1 == 1)
3552 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003553 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 edit_unputchar();
Bram Moolenaar24959102022-05-07 20:01:16 +01003555 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003557 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003558 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003562 continue; // end of input script reached
3563 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (!advance)
3566 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003567 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 timedout = TRUE;
3570 continue;
3571 }
3572 }
3573 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003574 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 while (typebuf.tb_buf[typebuf.tb_off
3576 + typebuf.tb_len] != NUL)
3577 typebuf.tb_noremap[typebuf.tb_off
3578 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003579#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003580 // Get IM status right after getting keys, not after the
3581 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 vgetc_im_active = im_get_status();
3583#endif
3584 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003585 } // for (;;)
3586 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
Bram Moolenaar30613902019-12-01 22:11:18 +01003588 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003589 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 /*
3592 * The "INSERT" message is taken care of here:
3593 * if we return an ESC to exit insert mode, the message is deleted
3594 * if we don't return an ESC but deleted the message before, redisplay it
3595 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003596 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003598 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003601 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
3603 unshowmode(FALSE);
3604 }
3605 else if (c != ESC && mode_deleted)
3606 {
3607 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003608 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 else
3610 showmode();
3611 }
3612 }
3613#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003614 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003615 if (gui.in_use && shape_changed)
3616 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003618 if (timedout && c == ESC)
3619 {
3620 char_u nop_buf[3];
3621
3622 // When recording there will be no timeout. Add a <Nop> after the ESC
3623 // to avoid that it forms a key code with following characters.
3624 nop_buf[0] = K_SPECIAL;
3625 nop_buf[1] = KS_EXTRA;
3626 nop_buf[2] = KE_NOP;
3627 gotchars(nop_buf, 3);
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003630 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 return c;
3633}
3634
3635/*
3636 * inchar() - get one character from
3637 * 1. a scriptfile
3638 * 2. the keyboard
3639 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003640 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 * NUL terminated (buffer length must be 'maxlen' + 1).
3642 * Minimum for "maxlen" is 3!!!!
3643 *
3644 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3645 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3646 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3647 * otherwise.
3648 *
3649 * If we got an interrupt all input is read until none is available.
3650 *
3651 * If wait_time == 0 there is no waiting for the char.
3652 * If wait_time == n we wait for n msec for a character to arrive.
3653 * If wait_time == -1 we wait forever for a character to arrive.
3654 *
3655 * Return the number of obtained characters.
3656 * Return -1 when end of input script reached.
3657 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003658 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003659inchar(
3660 char_u *buf,
3661 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003662 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
Bram Moolenaar30613902019-12-01 22:11:18 +01003664 int len = 0; // init for GCC
3665 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003667 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar30613902019-12-01 22:11:18 +01003669 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
3671 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003672 out_flush_cursor(FALSE, FALSE);
3673#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3674 if (gui.in_use && postponed_mouseshape)
3675 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676#endif
3677 }
3678
3679 /*
3680 * Don't reset these when at the hit-return prompt, otherwise a endless
3681 * recursive loop may result (write error in swapfile, hit-return, timeout
3682 * on char wait, flush swapfile, write error....).
3683 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003684 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003686 did_outofmem_msg = FALSE; // display out of memory message (again)
3687 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003689 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003692 * Get a character from a script file if there is one.
3693 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 */
3695 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003696 while (scriptin[curscript] != NULL && script_char < 0
3697#ifdef FEAT_EVAL
3698 && !ignore_script
3699#endif
3700 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003702#ifdef MESSAGE_QUEUE
3703 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003704#endif
3705
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3707 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003708 // Reached EOF.
3709 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3710 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 closescript();
3712 /*
3713 * When reading script file is interrupted, return an ESC to get
3714 * back to normal mode.
3715 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3716 */
3717 if (got_int)
3718 retesc = TRUE;
3719 else
3720 return -1;
3721 }
3722 else
3723 {
3724 buf[0] = script_char;
3725 len = 1;
3726 }
3727 }
3728
Bram Moolenaar30613902019-12-01 22:11:18 +01003729 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 /*
3732 * If we got an interrupt, skip all previously typed characters and
3733 * return TRUE if quit reading script file.
3734 * Stop reading typeahead when a single CTRL-C was read,
3735 * fill_input_buf() returns this when not able to read from stdin.
3736 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3737 * and buf may be pointing inside typebuf.tb_buf[].
3738 */
3739 if (got_int)
3740 {
kylo252ae6f1d82022-02-16 19:24:07 +00003741#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 char_u dum[DUM_LEN + 1];
3743
3744 for (;;)
3745 {
3746 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003747 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 break;
3749 }
3750 return retesc;
3751 }
3752
3753 /*
3754 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003755 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003757 if (wait_time == -1L || wait_time > 10L)
3758 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /*
3761 * Fill up to a third of the buffer, because each character may be
3762 * tripled below.
3763 */
3764 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3765 }
3766
Bram Moolenaar30613902019-12-01 22:11:18 +01003767 // If the typebuf was changed further down, it is like nothing was added by
3768 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (typebuf_changed(tb_change_cnt))
3770 return 0;
3771
Bram Moolenaar30613902019-12-01 22:11:18 +01003772 // Note the change in the typeahead buffer, this matters for when
3773 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3774 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003775 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3776 typebuf.tb_change_cnt = 1;
3777
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003778 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779}
3780
3781/*
3782 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003783 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 * Returns the new length.
3785 */
3786 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003787fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788{
3789 int i;
3790 char_u *p = buf;
3791
3792 /*
3793 * Two characters are special: NUL and K_SPECIAL.
3794 * When compiled With the GUI CSI is also special.
3795 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3796 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3797 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 */
3799 for (i = len; --i >= 0; ++p)
3800 {
3801#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003802 // When the GUI is used any character can come after a CSI, don't
3803 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (gui.in_use && p[0] == CSI && i >= 2)
3805 {
3806 p += 2;
3807 i -= 2;
3808 }
zeertzjq646bb722022-02-16 17:51:47 +00003809# ifndef MSWIN
3810 // When not on MS-Windows and the GUI is not used CSI needs to be
3811 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 else if (!gui.in_use && p[0] == CSI)
3813 {
3814 mch_memmove(p + 3, p + 1, (size_t)i);
3815 *p++ = K_SPECIAL;
3816 *p++ = KS_EXTRA;
3817 *p = (int)KE_CSI;
3818 len += 2;
3819 }
zeertzjq646bb722022-02-16 17:51:47 +00003820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 else
3822#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003823 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003824 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003825 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003826#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003827 // Win32 console passes modifiers
3828 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003829# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003830 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003831# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003832 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#endif
3834 ))
3835 {
3836 mch_memmove(p + 3, p + 1, (size_t)i);
3837 p[2] = K_THIRD(p[0]);
3838 p[1] = K_SECOND(p[0]);
3839 p[0] = K_SPECIAL;
3840 p += 2;
3841 len += 2;
3842 }
3843 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003844 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 return len;
3846}
3847
3848#if defined(USE_INPUT_BUF) || defined(PROTO)
3849/*
3850 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3851 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003852 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003853 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
3855 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003856input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003859# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3860 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861# endif
3862 );
3863}
3864#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003865
3866/*
3867 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3868 * typeahead.
3869 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003870 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003871getcmdkeycmd(
3872 int promptc UNUSED,
3873 void *cookie UNUSED,
3874 int indent UNUSED,
3875 getline_opt_T do_concat UNUSED)
3876{
3877 garray_T line_ga;
3878 int c1 = -1;
3879 int c2;
3880 int cmod = 0;
3881 int aborted = FALSE;
3882
3883 ga_init2(&line_ga, 1, 32);
3884
3885 // no mapping for these characters
3886 no_mapping++;
3887
3888 got_int = FALSE;
3889 while (c1 != NUL && !aborted)
3890 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003891 if (ga_grow(&line_ga, 32) != OK)
3892 {
3893 aborted = TRUE;
3894 break;
3895 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003896
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003897 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003898 {
3899 // incomplete <Cmd> is an error, because there is not much the user
3900 // could do in this state.
3901 emsg(_(e_cmd_mapping_must_end_with_cr));
3902 aborted = TRUE;
3903 break;
3904 }
3905
3906 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003907 c1 = vgetorpeek(TRUE);
3908
Bram Moolenaar957cf672020-11-12 14:21:06 +01003909 // Get two extra bytes for special keys
3910 if (c1 == K_SPECIAL)
3911 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003912 c1 = vgetorpeek(TRUE);
3913 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003914 if (c1 == KS_MODIFIER)
3915 {
3916 cmod = c2;
3917 continue;
3918 }
3919 c1 = TO_SPECIAL(c1, c2);
3920 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003921 if (c1 == Ctrl_V)
3922 {
3923 // CTRL-V is followed by octal, hex or other characters, reverses
3924 // what AppendToRedobuffLit() does.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003925 ++no_reduce_keys; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003926 c1 = get_literal(TRUE);
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003927 --no_reduce_keys;
Bram Moolenaar4a441202020-11-28 14:43:26 +01003928 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003929
3930 if (got_int)
3931 aborted = TRUE;
3932 else if (c1 == '\r' || c1 == '\n')
3933 c1 = NUL; // end the line
3934 else if (c1 == ESC)
3935 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003936 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003937 {
3938 // give a nicer error message for this special case
3939 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3940 aborted = TRUE;
3941 }
3942 else if (IS_SPECIAL(c1))
3943 {
3944 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003945 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003946 else
3947 {
dundargocc57b5bc2022-11-02 13:30:51 +00003948 semsg(e_cmd_mapping_must_not_include_str_key,
Bram Moolenaar957cf672020-11-12 14:21:06 +01003949 get_special_key_name(c1, cmod));
3950 aborted = TRUE;
3951 }
3952 }
3953 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003954 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003955
3956 cmod = 0;
3957 }
3958
3959 no_mapping--;
3960
3961 if (aborted)
3962 ga_clear(&line_ga);
3963
3964 return (char_u *)line_ga.ga_data;
3965}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003966
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003967#if defined(FEAT_EVAL) || defined(PROTO)
3968/*
3969 * If there was a mapping put info about it in the redo buffer, so that "."
3970 * will use the same script context. We only need the SID.
3971 */
3972 void
3973may_add_last_used_map_to_redobuff(void)
3974{
3975 char_u buf[3 + 20];
3976
3977 if (last_used_map == NULL || last_used_map->m_script_ctx.sc_sid < 0)
3978 return;
3979
3980 // <K_SID>{nr};
3981 buf[0] = K_SPECIAL;
3982 buf[1] = KS_EXTRA;
3983 buf[2] = KE_SID;
3984 vim_snprintf((char *)buf + 3, 20, "%d;",
3985 last_used_map->m_script_ctx.sc_sid);
3986 add_buff(&redobuff, buf, -1L);
3987}
3988#endif
3989
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003990 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003991do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003992{
3993 int res;
3994#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003995 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003996
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003997 if (key == K_SCRIPT_COMMAND
3998 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003999 {
4000 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004001 if (last_used_map != NULL)
4002 current_sctx = last_used_map->m_script_ctx;
4003 else
4004 {
4005 current_sctx.sc_sid = last_used_sid;
4006 current_sctx.sc_lnum = 0;
4007 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4008 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004009 }
4010#endif
4011
4012 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4013
4014#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004015 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004016 current_sctx = save_current_sctx;
4017#endif
4018
4019 return res;
4020}
4021
4022#if defined(FEAT_EVAL) || defined(PROTO)
4023 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004024reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004025{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004026 if (last_used_map == mp)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004027 {
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004028 last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004029 last_used_sid = -1;
4030 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004031}
4032#endif