blob: f3dbe4a849768e50ee8a50948fa806a560e1fd93 [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;
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020093static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020095static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020097static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99/*
100 * Free and clear a buffer.
101 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200102 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100103free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100105 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar285e3352018-04-18 23:01:13 +0200107 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 {
109 np = p->b_next;
110 vim_free(p);
111 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200112 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000113 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114}
115
116/*
117 * Return the contents of a buffer as a single string.
118 * K_SPECIAL and CSI in the returned string are escaped.
119 */
120 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100121get_buffcont(
122 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100123 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 long_u count = 0;
126 char_u *p = NULL;
127 char_u *p2;
128 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200129 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
Bram Moolenaar30613902019-12-01 22:11:18 +0100131 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200132 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 count += (long_u)STRLEN(bp->b_str);
134
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200135 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 {
137 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200138 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 for (str = bp->b_str; *str; )
140 *p2++ = *str++;
141 *p2 = NUL;
142 }
143 return (p);
144}
145
146/*
147 * Return the contents of the record buffer as a single string
148 * and clear the record buffer.
149 * K_SPECIAL and CSI in the returned string are escaped.
150 */
151 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100152get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153{
154 char_u *p;
155 size_t len;
156
157 p = get_buffcont(&recordbuff, TRUE);
158 free_buff(&recordbuff);
159
160 /*
161 * Remove the characters that were added the last time, these must be the
162 * (possibly mapped) characters that stopped the recording.
163 */
164 len = STRLEN(p);
165 if ((int)len >= last_recorded_len)
166 {
167 len -= last_recorded_len;
168 p[len] = NUL;
169 }
170
171 /*
172 * When stopping recording from Insert mode with CTRL-O q, also remove the
173 * CTRL-O.
174 */
175 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
176 p[len - 1] = NUL;
177
178 return (p);
179}
180
181/*
182 * Return the contents of the redo buffer as a single string.
183 * K_SPECIAL and CSI in the returned string are escaped.
184 */
185 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100186get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000188 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189}
190
191/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000192 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 * K_SPECIAL and CSI should have been escaped already.
194 */
195 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100196add_buff(
197 buffheader_T *buf,
198 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100199 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100201 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200202 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204 if (slen < 0)
205 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100206 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 return;
208
Bram Moolenaar30613902019-12-01 22:11:18 +0100209 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
211 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200212 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100214 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000216 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 return;
218 }
219 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200220 mch_memmove(buf->bh_first.b_next->b_str,
221 buf->bh_first.b_next->b_str + buf->bh_index,
222 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 buf->bh_index = 0;
224
225 if (buf->bh_space >= (int)slen)
226 {
227 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000228 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 buf->bh_space -= slen;
230 }
231 else
232 {
233 if (slen < MINIMAL_SIZE)
234 len = MINIMAL_SIZE;
235 else
236 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200237 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100239 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000240 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000241 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
Bram Moolenaar285e3352018-04-18 23:01:13 +0200243 p->b_next = buf->bh_curr->b_next;
244 buf->bh_curr->b_next = p;
245 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247}
248
249/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000250 * Delete "slen" bytes from the end of "buf".
251 * Only works when it was just added.
252 */
253 static void
254delete_buff_tail(buffheader_T *buf, int slen)
255{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000256 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000257
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000258 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000259 return; // nothing to delete
260 len = (int)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000261 if (len >= slen)
262 {
263 buf->bh_curr->b_str[len - slen] = NUL;
264 buf->bh_space += slen;
265 }
266}
267
268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 * Add number "n" to buffer "buf".
270 */
271 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100272add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274 char_u number[32];
275
276 sprintf((char *)number, "%ld", n);
277 add_buff(buf, number, -1L);
278}
279
280/*
281 * Add character 'c' to buffer "buf".
282 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
283 */
284 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100285add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 char_u bytes[MB_MAXBYTES + 1];
288 int len;
289 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 char_u temp[4];
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (IS_SPECIAL(c))
293 len = 1;
294 else
295 len = (*mb_char2bytes)(c, bytes);
296 for (i = 0; i < len; ++i)
297 {
298 if (!IS_SPECIAL(c))
299 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300
301 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
302 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100303 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 temp[0] = K_SPECIAL;
305 temp[1] = K_SECOND(c);
306 temp[2] = K_THIRD(c);
307 temp[3] = NUL;
308 }
309#ifdef FEAT_GUI
310 else if (c == CSI)
311 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100312 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 temp[0] = CSI;
314 temp[1] = KS_EXTRA;
315 temp[2] = (int)KE_CSI;
316 temp[3] = NUL;
317 }
318#endif
319 else
320 {
321 temp[0] = c;
322 temp[1] = NUL;
323 }
324 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326}
327
Bram Moolenaar30613902019-12-01 22:11:18 +0100328// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200329static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100330
Bram Moolenaar30613902019-12-01 22:11:18 +0100331// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200332static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100335 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
336 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 * If advance == TRUE go to the next char.
338 * No translation is done K_SPECIAL and CSI are escaped.
339 */
340 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100341read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100343 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100345 c = read_readbuf(&readbuf1, advance);
346 if (c == NUL)
347 c = read_readbuf(&readbuf2, advance);
348 return c;
349}
350
351 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100352read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100353{
354 char_u c;
355 buffblock_T *curr;
356
Bram Moolenaar30613902019-12-01 22:11:18 +0100357 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 return NUL;
359
Bram Moolenaar285e3352018-04-18 23:01:13 +0200360 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100361 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 if (advance)
364 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100365 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200367 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 }
372 return c;
373}
374
375/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100376 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 */
378 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100379start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200381 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200383 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100384 readbuf1.bh_space = 0;
385 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200386 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100387 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200388 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100389 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 }
391}
392
393/*
394 * Return TRUE if the stuff buffer is empty.
395 */
396 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100397stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200399 return (readbuf1.bh_first.b_next == NULL
400 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100401}
402
Bram Moolenaar113e1072019-01-20 15:30:40 +0100403#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100404/*
405 * Return TRUE if readbuf1 is empty. There may still be redo characters in
406 * redbuf2.
407 */
408 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100409readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100410{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200411 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415/*
416 * Set a typeahead character that won't be flushed.
417 */
418 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100419typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420{
421 typeahead_char = c;
422}
423
424/*
425 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100426 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 * flush all typeahead characters (used when interrupted by a CTRL-C).
428 */
429 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200430flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431{
432 init_typebuf();
433
434 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100435 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 ;
437
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200438 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200440 // remove mapped characters at the start only
441 typebuf.tb_off += typebuf.tb_maplen;
442 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100443#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
444 if (typebuf.tb_len == 0)
445 typebuf_was_filled = FALSE;
446#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200447 }
448 else
449 {
450 // remove typeahead
451 if (flush_typeahead == FLUSH_INPUT)
452 // We have to get all characters, because we may delete the first
453 // part of an escape sequence. In an xterm we get one char at a
454 // time and we have to get them all.
455 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
456 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 typebuf.tb_off = MAXMAPLEN;
458 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200459#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100460 // Reset the flag that text received from a client or from feedkeys()
461 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200462 typebuf_was_filled = FALSE;
463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 typebuf.tb_maplen = 0;
466 typebuf.tb_silent = 0;
467 cmd_silent = FALSE;
468 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200469 if (++typebuf.tb_change_cnt == 0)
470 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471}
472
473/*
474 * The previous contents of the redo buffer is kept in old_redobuffer.
475 * This is used for the CTRL-O <.> command in insert mode.
476 */
477 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100478ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 if (!block_redo)
481 {
482 free_buff(&old_redobuff);
483 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200484 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 }
486}
487
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100488/*
489 * Discard the contents of the redo buffer and restore the previous redo
490 * buffer.
491 */
492 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100493CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100494{
495 if (!block_redo)
496 {
497 free_buff(&redobuff);
498 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200499 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100500 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100501 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100502 ;
503 }
504}
505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506/*
507 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
508 * Used before executing autocommands and user functions.
509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200511saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
513 char_u *s;
514
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200515 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200516 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200517 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200518 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
Bram Moolenaar30613902019-12-01 22:11:18 +0100520 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200521 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
522 if (s != NULL)
523 {
524 add_buff(&redobuff, s, -1L);
525 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527}
528
529/*
530 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
531 * Used after executing autocommands and user functions.
532 */
533 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200534restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200536 free_buff(&redobuff);
537 redobuff = save_redo->sr_redobuff;
538 free_buff(&old_redobuff);
539 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542/*
543 * Append "s" to the redo buffer.
544 * K_SPECIAL and CSI should already have been escaped.
545 */
546 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100547AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 if (!block_redo)
550 add_buff(&redobuff, s, -1L);
551}
552
553/*
554 * Append to Redo buffer literally, escaping special characters with CTRL-V.
555 * K_SPECIAL and CSI are escaped as well.
556 */
557 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100558AppendToRedobuffLit(
559 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100560 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000562 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 int c;
564 char_u *start;
565
566 if (block_redo)
567 return;
568
Bram Moolenaarebefac62005-12-28 22:39:57 +0000569 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100571 // Put a string of normal characters in the redo buffer (that's
572 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000574 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 ++s;
576
Bram Moolenaar30613902019-12-01 22:11:18 +0100577 // Don't put '0' or '^' as last character, just in case a CTRL-D is
578 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
580 --s;
581 if (s > start)
582 add_buff(&redobuff, start, (long)(s - start));
583
Bram Moolenaarebefac62005-12-28 22:39:57 +0000584 if (*s == NUL || (len >= 0 && s - str >= len))
585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar30613902019-12-01 22:11:18 +0100587 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000588 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100589 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000590 c = mb_cptr2char_adv(&s);
591 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000592 c = *s++;
593 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
594 add_char_buff(&redobuff, Ctrl_V);
595
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000596 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000597 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000598 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000599 else
600 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602}
603
604/*
605 * Append a character to the redo buffer.
606 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
607 */
608 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100609AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (!block_redo)
612 add_char_buff(&redobuff, c);
613}
614
615/*
616 * Append a number to the redo buffer.
617 */
618 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100619AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 if (!block_redo)
622 add_num_buff(&redobuff, n);
623}
624
625/*
626 * Append string "s" to the stuff buffer.
627 * CSI and K_SPECIAL must already have been escaped.
628 */
629 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100630stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100632 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200635/*
636 * Append string "s" to the redo stuff buffer.
637 * CSI and K_SPECIAL must already have been escaped.
638 */
639 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100640stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200641{
642 add_buff(&readbuf2, s, -1L);
643}
644
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200645 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100646stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100648 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649}
650
651#if defined(FEAT_EVAL) || defined(PROTO)
652/*
653 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
654 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 */
657 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100658stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200660 int c;
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 while (*s != NUL)
663 {
664 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
665 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100666 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 stuffReadbuffLen(s, 3L);
668 s += 3;
669 }
670 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200671 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100672 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200673 if (c == CAR || c == NL || c == ESC)
674 c = ' ';
675 stuffcharReadbuff(c);
676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678}
679#endif
680
681/*
682 * Append a character to the stuff buffer.
683 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
684 */
685 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100686stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100688 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689}
690
691/*
692 * Append a number to the stuff buffer.
693 */
694 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100695stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100697 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200701 * Stuff a string into the typeahead buffer, such that edit() will insert it
702 * literally ("literally" TRUE) or interpret is as typed characters.
703 */
704 void
705stuffescaped(char_u *arg, int literally)
706{
707 int c;
708 char_u *start;
709
710 while (*arg != NUL)
711 {
712 // Stuff a sequence of normal ASCII characters, that's fast. Also
713 // stuff K_SPECIAL to get the effect of a special key when "literally"
714 // is TRUE.
715 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000716 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200717 || (*arg == K_SPECIAL && !literally))
718 ++arg;
719 if (arg > start)
720 stuffReadbuffLen(start, (long)(arg - start));
721
722 // stuff a single special character
723 if (*arg != NUL)
724 {
725 if (has_mbyte)
726 c = mb_cptr2char_adv(&arg);
727 else
728 c = *arg++;
729 if (literally && ((c < ' ' && c != TAB) || c == DEL))
730 stuffcharReadbuff(Ctrl_V);
731 stuffcharReadbuff(c);
732 }
733 }
734}
735
736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
738 * multibyte characters.
739 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100740 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
741 * otherwise.
742 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
744 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100745read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100747 static buffblock_T *bp;
748 static char_u *p;
749 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100750 int n;
751 char_u buf[MB_MAXBYTES + 1];
752 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754 if (init)
755 {
756 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200757 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200759 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (bp == NULL)
761 return FAIL;
762 p = bp->b_str;
763 return OK;
764 }
765 if ((c = *p) != NUL)
766 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100767 // Reverse the conversion done by add_char_buff()
768 // For a multi-byte character get all the bytes and return the
769 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
771 n = MB_BYTE2LEN_CHECK(c);
772 else
773 n = 1;
774 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100776 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
778 c = TO_SPECIAL(p[1], p[2]);
779 p += 2;
780 }
781#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100782 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 p += 2;
784#endif
785 if (*++p == NUL && bp->b_next != NULL)
786 {
787 bp = bp->b_next;
788 p = bp->b_str;
789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100791 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
793 if (n != 1)
794 c = (*mb_ptr2char)(buf);
795 break;
796 }
797 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100798 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 }
802
803 return c;
804}
805
806/*
807 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
808 * If old_redo is TRUE, use old_redobuff instead of redobuff.
809 * The escaped K_SPECIAL and CSI are copied without translation.
810 */
811 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100812copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 int c;
815
816 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100817 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100821 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Insert the redo count into the command.
823 * If "old_redo" is TRUE, the last but one command is repeated
824 * instead of the last command (inserting text). This is used for
825 * CTRL-O <.> in insert mode
826 *
827 * return FAIL for failure, OK otherwise
828 */
829 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100830start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
832 int c;
833
Bram Moolenaar30613902019-12-01 22:11:18 +0100834 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (read_redo(TRUE, old_redo) == FAIL)
836 return FAIL;
837
838 c = read_redo(FALSE, old_redo);
839
Bram Moolenaar30613902019-12-01 22:11:18 +0100840 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (c == '"')
842 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100843 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 c = read_redo(FALSE, old_redo);
845
Bram Moolenaar30613902019-12-01 22:11:18 +0100846 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (c >= '1' && c < '9')
848 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100849 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200850
Bram Moolenaar30613902019-12-01 22:11:18 +0100851 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200852 if (c == '=')
853 {
854 add_char_buff(&readbuf2, CAR);
855 cmd_silent = TRUE;
856 }
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 c = read_redo(FALSE, old_redo);
859 }
860
Bram Moolenaar30613902019-12-01 22:11:18 +0100861 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
863 VIsual = curwin->w_cursor;
864 VIsual_active = TRUE;
865 VIsual_select = FALSE;
866 VIsual_reselect = TRUE;
867 redo_VIsual_busy = TRUE;
868 c = read_redo(FALSE, old_redo);
869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaar30613902019-12-01 22:11:18 +0100871 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (count)
873 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100874 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100876 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878
Bram Moolenaar30613902019-12-01 22:11:18 +0100879 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 copy_redo(old_redo);
882 return OK;
883}
884
885/*
886 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100887 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * return FAIL for failure, OK otherwise
889 */
890 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100891start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 int c;
894
895 if (read_redo(TRUE, FALSE) == FAIL)
896 return FAIL;
897 start_stuff();
898
Bram Moolenaar30613902019-12-01 22:11:18 +0100899 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 while ((c = read_redo(FALSE, FALSE)) != NUL)
901 {
902 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
903 {
904 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100905 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 break;
907 }
908 }
909
Bram Moolenaar30613902019-12-01 22:11:18 +0100910 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 copy_redo(FALSE);
912 block_redo = TRUE;
913 return OK;
914}
915
916 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100917stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 block_redo = FALSE;
920}
921
922/*
923 * Initialize typebuf.tb_buf to point to typebuf_init.
924 * alloc() cannot be used here: In out-of-memory situations it would
925 * be impossible to type anything.
926 */
927 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100928init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 if (typebuf.tb_buf == NULL)
931 {
932 typebuf.tb_buf = typebuf_init;
933 typebuf.tb_noremap = noremapbuf_init;
934 typebuf.tb_buflen = TYPELEN_INIT;
935 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200936 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 typebuf.tb_change_cnt = 1;
938 }
939}
940
941/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200942 * Returns TRUE when keys cannot be remapped.
943 */
944 int
945noremap_keys(void)
946{
947 return KeyNoremap & (RM_NONE|RM_SCRIPT);
948}
949
950/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100951 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
952 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *
954 * If noremap is REMAP_YES, new string can be mapped again.
955 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000956 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000957 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
959 * script-local mappings.
960 * If noremap is > 0, that many characters of the new string cannot be mapped.
961 *
962 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
963 * offset is non-zero!).
964 *
965 * If silent is TRUE, cmd_silent is set when the characters are obtained.
966 *
967 * return FAIL for failure, OK otherwise
968 */
969 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100970ins_typebuf(
971 char_u *str,
972 int noremap,
973 int offset,
974 int nottyped,
975 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 char_u *s1, *s2;
978 int newlen;
979 int addlen;
980 int i;
981 int newoff;
982 int val;
983 int nrm;
984
985 init_typebuf();
986 if (++typebuf.tb_change_cnt == 0)
987 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200988 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (offset == 0 && addlen <= typebuf.tb_off)
993 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100994 /*
995 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 typebuf.tb_off -= addlen;
998 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
999 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001000 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1001 >= addlen + 3 * (MAXMAPLEN + 4))
1002 {
1003 /*
1004 * Buffer is empty and string fits in the existing buffer.
1005 * Leave some space before and after, if possible.
1006 */
1007 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1008 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else
1011 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001012 int extra;
1013
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001014 /*
1015 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001016 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001017 * characters. We add some extra room to avoid having to allocate too
1018 * often.
1019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001021 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1022 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001024 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001025 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 setcursor();
1027 return FAIL;
1028 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001029 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001031 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 return FAIL;
1033 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001034 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 vim_free(s1);
1037 return FAIL;
1038 }
1039 typebuf.tb_buflen = newlen;
1040
Bram Moolenaar30613902019-12-01 22:11:18 +01001041 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1043 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001044 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001046 // copy the old chars, after the insertion point, including the NUL at
1047 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 mch_memmove(s1 + newoff + offset + addlen,
1049 typebuf.tb_buf + typebuf.tb_off + offset,
1050 (size_t)(typebuf.tb_len - offset + 1));
1051 if (typebuf.tb_buf != typebuf_init)
1052 vim_free(typebuf.tb_buf);
1053 typebuf.tb_buf = s1;
1054
1055 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1056 (size_t)offset);
1057 mch_memmove(s2 + newoff + offset + addlen,
1058 typebuf.tb_noremap + typebuf.tb_off + offset,
1059 (size_t)(typebuf.tb_len - offset));
1060 if (typebuf.tb_noremap != noremapbuf_init)
1061 vim_free(typebuf.tb_noremap);
1062 typebuf.tb_noremap = s2;
1063
1064 typebuf.tb_off = newoff;
1065 }
1066 typebuf.tb_len += addlen;
1067
Bram Moolenaar30613902019-12-01 22:11:18 +01001068 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (noremap == REMAP_SCRIPT)
1070 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001071 else if (noremap == REMAP_SKIP)
1072 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 else
1074 val = RM_NONE;
1075
1076 /*
1077 * Adjust typebuf.tb_noremap[] for the new characters:
1078 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1079 * (sometimes) not remappable
1080 * If noremap == REMAP_YES: all the new characters are mappable
1081 * If noremap > 0: "noremap" characters are not remappable, the rest
1082 * mappable
1083 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001084 if (noremap == REMAP_SKIP)
1085 nrm = 1;
1086 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 nrm = addlen;
1088 else
1089 nrm = noremap;
1090 for (i = 0; i < addlen; ++i)
1091 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1092 (--nrm >= 0) ? val : RM_YES;
1093
Bram Moolenaar30613902019-12-01 22:11:18 +01001094 // tb_maplen and tb_silent only remember the length of mapped and/or
1095 // silent mappings at the start of the buffer, assuming that a mapped
1096 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (nottyped || typebuf.tb_maplen > offset)
1098 typebuf.tb_maplen += addlen;
1099 if (silent || typebuf.tb_silent > offset)
1100 {
1101 typebuf.tb_silent += addlen;
1102 cmd_silent = TRUE;
1103 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001104 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 typebuf.tb_no_abbr_cnt += addlen;
1106
1107 return OK;
1108}
1109
1110/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001111 * Put character "c" back into the typeahead buffer.
1112 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001113 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1114 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001115 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001116 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001117 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001118ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001119{
zeertzjq6cac7702022-01-04 18:01:21 +00001120 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001121 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001122
zeertzjq2e7cba32022-06-10 15:30:32 +01001123 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001124 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001125 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001126}
1127
1128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001130 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001131 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1133 * changed it was reallocated and the old pointer can no longer be used.
1134 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1135 * that was just added.
1136 */
1137 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001138typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001139 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001142#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1143 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145 ));
1146}
1147
1148/*
1149 * Return TRUE if there are no characters in the typeahead buffer that have
1150 * not been typed (result from a mapping or come from ":normal").
1151 */
1152 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001153typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155 return typebuf.tb_maplen == 0;
1156}
1157
1158/*
1159 * Return the number of characters that are mapped (or not typed).
1160 */
1161 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001162typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
1164 return typebuf.tb_maplen;
1165}
1166
1167/*
1168 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1169 */
1170 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001171del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
1173 int i;
1174
1175 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001176 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 typebuf.tb_len -= len;
1179
1180 /*
1181 * Easy case: Just increase typebuf.tb_off.
1182 */
1183 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1184 >= 3 * MAXMAPLEN + 3)
1185 typebuf.tb_off += len;
1186 /*
1187 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1188 */
1189 else
1190 {
1191 i = typebuf.tb_off + offset;
1192 /*
1193 * Leave some extra room at the end to avoid reallocation.
1194 */
1195 if (typebuf.tb_off > MAXMAPLEN)
1196 {
1197 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1198 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1199 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1200 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1201 typebuf.tb_off = MAXMAPLEN;
1202 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001203 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1205 typebuf.tb_buf + i + len,
1206 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001207 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1209 typebuf.tb_noremap + i + len,
1210 (size_t)(typebuf.tb_len - offset));
1211 }
1212
Bram Moolenaar30613902019-12-01 22:11:18 +01001213 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
1215 if (typebuf.tb_maplen < offset + len)
1216 typebuf.tb_maplen = offset;
1217 else
1218 typebuf.tb_maplen -= len;
1219 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001220 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
1222 if (typebuf.tb_silent < offset + len)
1223 typebuf.tb_silent = offset;
1224 else
1225 typebuf.tb_silent -= len;
1226 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001227 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
1229 if (typebuf.tb_no_abbr_cnt < offset + len)
1230 typebuf.tb_no_abbr_cnt = offset;
1231 else
1232 typebuf.tb_no_abbr_cnt -= len;
1233 }
1234
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001235#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001236 // Reset the flag that text received from a client or from feedkeys()
1237 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001238 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#endif
1240 if (++typebuf.tb_change_cnt == 0)
1241 typebuf.tb_change_cnt = 1;
1242}
1243
1244/*
1245 * Write typed characters to script file.
1246 * If recording is on put the character in the recordbuffer.
1247 */
1248 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001249gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001251 char_u *s = chars;
1252 int i;
1253 static char_u buf[4];
1254 static int buflen = 0;
1255 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001257 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001259 buf[buflen++] = *s++;
1260
1261 // When receiving a special key sequence, store it until we have all
1262 // the bytes and we can decide what to do with it.
1263 if (buflen == 1 && buf[0] == K_SPECIAL)
1264 continue;
1265 if (buflen == 2)
1266 continue;
1267 if (buflen == 3 && buf[1] == KS_EXTRA
1268 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1269 {
1270 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1271 // recording.
1272 buflen = 0;
1273 continue;
1274 }
1275
Bram Moolenaar30613902019-12-01 22:11:18 +01001276 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001277 for (i = 0; i < buflen; ++i)
1278 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001280 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001282 buf[buflen] = NUL;
1283 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001284 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001285 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001287 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 may_sync_undo();
1290
1291#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001292 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 debug_did_msg = FALSE;
1294#endif
1295
Bram Moolenaar30613902019-12-01 22:11:18 +01001296 // Since characters have been typed, consider the following to be in
1297 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 ++maptick;
1299}
1300
1301/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001302 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1303 * character back into the typeahead buffer, thus gotchars() will be called
1304 * again.
1305 * Only affects recorded characters.
1306 */
1307 void
1308ungetchars(int len)
1309{
1310 if (reg_recording != 0)
1311 {
1312 delete_buff_tail(&recordbuff, len);
1313 last_recorded_len -= len;
1314 }
1315}
1316
1317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 * Sync undo. Called when typed characters are obtained from the typeahead
1319 * buffer, or when a menu is used.
1320 * Do not sync:
1321 * - In Insert mode, unless cursor key has been used.
1322 * - While reading a script file.
1323 * - When no_u_sync is non-zero.
1324 */
1325 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001326may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
Bram Moolenaar24959102022-05-07 20:01:16 +01001328 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001329 && scriptin[curscript] == NULL)
1330 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331}
1332
1333/*
1334 * Make "typebuf" empty and allocate new buffers.
1335 * Returns FAIL when out of memory.
1336 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001337 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001338alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 typebuf.tb_buf = alloc(TYPELEN_INIT);
1341 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1342 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1343 {
1344 free_typebuf();
1345 return FAIL;
1346 }
1347 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001348 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 typebuf.tb_len = 0;
1350 typebuf.tb_maplen = 0;
1351 typebuf.tb_silent = 0;
1352 typebuf.tb_no_abbr_cnt = 0;
1353 if (++typebuf.tb_change_cnt == 0)
1354 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001355#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1356 typebuf_was_filled = FALSE;
1357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 return OK;
1359}
1360
1361/*
1362 * Free the buffers of "typebuf".
1363 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001364 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001365free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001367 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001368 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001369 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001370 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001371 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001372 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001373 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001374 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375}
1376
1377/*
1378 * When doing ":so! file", the current typeahead needs to be saved, and
1379 * restored when "file" has been read completely.
1380 */
1381static typebuf_T saved_typebuf[NSCRIPT];
1382
1383 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001384save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 init_typebuf();
1387 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001388 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (alloc_typebuf() == FAIL)
1390 {
1391 closescript();
1392 return FAIL;
1393 }
1394 return OK;
1395}
1396
Bram Moolenaar30613902019-12-01 22:11:18 +01001397static int old_char = -1; // character put back by vungetc()
1398static int old_mod_mask; // mod_mask for ungotten character
1399static int old_mouse_row; // mouse_row related to old_char
1400static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001401static int old_KeyStuffed; // whether old_char was stuffed
1402
zeertzjq6d4e7252022-04-07 13:58:04 +01001403static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001404{
1405 // If the old character was not stuffed and characters have been added to
1406 // the stuff buffer, need to first get the stuffed characters instead.
1407 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1408}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410/*
1411 * Save all three kinds of typeahead, so that the user must type at a prompt.
1412 */
1413 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001414save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 tp->save_typebuf = typebuf;
1417 tp->typebuf_valid = (alloc_typebuf() == OK);
1418 if (!tp->typebuf_valid)
1419 typebuf = tp->save_typebuf;
1420
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001421 tp->old_char = old_char;
1422 tp->old_mod_mask = old_mod_mask;
1423 old_char = -1;
1424
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001425 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001426 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001427 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001428 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429# ifdef USE_INPUT_BUF
1430 tp->save_inputbuf = get_input_buf();
1431# endif
1432}
1433
1434/*
1435 * Restore the typeahead to what it was before calling save_typeahead().
1436 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001437 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 */
1439 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001440restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 if (tp->typebuf_valid)
1443 {
1444 free_typebuf();
1445 typebuf = tp->save_typebuf;
1446 }
1447
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001448 old_char = tp->old_char;
1449 old_mod_mask = tp->old_mod_mask;
1450
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001451 free_buff(&readbuf1);
1452 readbuf1 = tp->save_readbuf1;
1453 free_buff(&readbuf2);
1454 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001456 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457# endif
1458}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460/*
1461 * Open a new script file for the ":source!" command.
1462 */
1463 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001464openscript(
1465 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001466 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 if (curscript + 1 == NSCRIPT)
1469 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001470 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return;
1472 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001473
1474 // Disallow sourcing a file in the sandbox, the commands would be executed
1475 // later, possibly outside of the sandbox.
1476 if (check_secure())
1477 return;
1478
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001479#ifdef FEAT_EVAL
1480 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001481 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001482 return;
1483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
Bram Moolenaar30613902019-12-01 22:11:18 +01001485 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001487 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 expand_env(name, NameBuff, MAXPATHL);
1489 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1490 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001491 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (curscript)
1493 --curscript;
1494 return;
1495 }
1496 if (save_typebuf() == FAIL)
1497 return;
1498
1499 /*
1500 * Execute the commands from the file right now when using ":source!"
1501 * after ":global" or ":argdo" or in a loop. Also when another command
1502 * follows. This means the display won't be updated. Don't do this
1503 * always, "make test" would fail.
1504 */
1505 if (directly)
1506 {
1507 oparg_T oa;
1508 int oldcurscript;
1509 int save_State = State;
1510 int save_restart_edit = restart_edit;
1511 int save_insertmode = p_im;
1512 int save_finish_op = finish_op;
1513 int save_msg_scroll = msg_scroll;
1514
Bram Moolenaar24959102022-05-07 20:01:16 +01001515 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001516 msg_scroll = FALSE; // no msg scrolling in Normal mode
1517 restart_edit = 0; // don't go to Insert mode
1518 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 clear_oparg(&oa);
1520 finish_op = FALSE;
1521
1522 oldcurscript = curscript;
1523 do
1524 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001525 update_topline_cursor(); // update cursor position and topline
1526 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001527 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 while (scriptin[oldcurscript] != NULL);
1530
1531 State = save_State;
1532 msg_scroll = save_msg_scroll;
1533 restart_edit = save_restart_edit;
1534 p_im = save_insertmode;
1535 finish_op = save_finish_op;
1536 }
1537}
1538
1539/*
1540 * Close the currently active input script.
1541 */
1542 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001543closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 free_typebuf();
1546 typebuf = saved_typebuf[curscript];
1547
1548 fclose(scriptin[curscript]);
1549 scriptin[curscript] = NULL;
1550 if (curscript > 0)
1551 --curscript;
1552}
1553
Bram Moolenaarea408852005-06-25 22:49:46 +00001554#if defined(EXITFREE) || defined(PROTO)
1555 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001556close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001557{
1558 while (scriptin[0] != NULL)
1559 closescript();
1560}
1561#endif
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
1564 * Return TRUE when reading keys from a script file.
1565 */
1566 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001567using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 return scriptin[curscript] != NULL;
1570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001573 * This function is called just before doing a blocking wait. Thus after
1574 * waiting 'updatetime' for a character to arrive.
1575 */
1576 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001577before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001578{
1579 updatescript(0);
1580#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001581 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001582 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001583#endif
1584}
1585
1586/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001587 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 * or when we have waited some time for a character (c == 0)
1589 *
1590 * All the changed memfiles are synced if c == 0 or when the number of typed
1591 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1592 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001593 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001594updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 static int count = 0;
1597
1598 if (c && scriptout)
1599 putc(c, scriptout);
1600 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1601 {
1602 ml_sync_all(c == 0, TRUE);
1603 count = 0;
1604 }
1605}
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001608 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001609 * character.
1610 */
1611 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001612merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001613{
1614 int c = c_arg;
1615
zeertzjqbad8a012022-04-29 16:44:00 +01001616 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001617 {
1618 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001619 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001620 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001621 if (c == NUL)
1622 c = K_ZERO;
1623 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001624 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001625 // CTRL-6 is equivalent to CTRL-^
1626 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001627#ifdef FEAT_GUI_GTK
1628 // These mappings look arbitrary at the first glance, but in fact
1629 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1630 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1631 // more sense and is consistent with usual terminal behaviour).
1632 else if (c == '2')
1633 c = NUL;
1634 else if (c >= '3' && c <= '7')
1635 c = c ^ 0x28;
1636 else if (c == '8')
1637 c = BS;
1638 else if (c == '?')
1639 c = DEL;
1640#endif
1641 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001642 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001643 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001644 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001645 && c >= 0 && c <= 127)
1646 {
1647 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001648 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001649 }
1650 return c;
1651}
1652
1653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * Get the next input character.
1655 * Can return a special key or a multi-byte character.
1656 * Can return NUL when called recursively, use safe_vgetc() if that's not
1657 * wanted.
1658 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1659 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001660 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
1662 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001663vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001667 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001670#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001671 // Do garbage collection when garbagecollect() was called previously and
1672 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001673 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001674 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001675#endif
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 /*
1678 * If a character was put back with vungetc, it was already processed.
1679 * Return it directly.
1680 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001681 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
1683 c = old_char;
1684 old_char = -1;
1685 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001686 mouse_row = old_mouse_row;
1687 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
zeertzjq81b46a62022-04-09 17:58:49 +01001691 // number of characters recorded from the last vgetc() call
1692 static int last_vgetc_recorded_len = 0;
1693
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001694 mod_mask = 0;
1695 vgetc_mod_mask = 0;
1696 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001697
1698 // last_recorded_len can be larger than last_vgetc_recorded_len
1699 // if peeking records more
1700 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001701
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001702 for (;;) // this is done twice if there are modifiers
1703 {
1704 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001705
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001706 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001707#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001708 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001709#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001710#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001711 || popup_no_mapping()
1712#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001713 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001715 // no mapping after modifier has been read
1716 ++no_mapping;
1717 ++allow_keys;
1718 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001720 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001721 if (did_inc)
1722 {
1723 --no_mapping;
1724 --allow_keys;
1725 }
1726
1727 // Get two extra bytes for special keys
1728 if (c == K_SPECIAL
1729#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001730 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001731#endif
1732 )
1733 {
1734 int save_allow_keys = allow_keys;
1735
1736 ++no_mapping;
1737 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001738 c2 = vgetorpeek(TRUE); // no mapping for these chars
1739 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001740 --no_mapping;
1741 allow_keys = save_allow_keys;
1742 if (c2 == KS_MODIFIER)
1743 {
1744 mod_mask = c;
1745 continue;
1746 }
1747 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar4f974752019-02-17 17:44:42 +01001749#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001750 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1751 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001752 if (
1753# ifdef VIMDLL
1754 gui.in_use &&
1755# endif
1756 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001758 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001759 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001760
1761 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001762 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001763 {
K.Takata54119102022-02-03 13:33:03 +00001764 name[j] = c;
1765 if (j < 199)
1766 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001767 }
K.Takata54119102022-02-03 13:33:03 +00001768 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001769 gui_make_tearoff(name);
1770 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001773#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001774 // GTK: <F10> normally selects the menu, but it's passed until
1775 // here to allow mapping it. Intercept and invoke the GTK
1776 // behavior if it's not mapped.
1777 if (c == K_F10 && gui.menubar != NULL)
1778 {
1779 gtk_menu_shell_select_first(
1780 GTK_MENU_SHELL(gui.menubar), FALSE);
1781 continue;
1782 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001785 // Handle focus event here, so that the caller doesn't need to
1786 // know about it. Return K_IGNORE so that we loop once (needed
1787 // if 'lazyredraw' is set).
1788 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001789 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001790 ui_focus_change(c == K_FOCUSGAINED);
1791 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001792 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001793
1794 // Translate K_CSI to CSI. The special key is only used to
1795 // avoid it being recognized as the start of a special key.
1796 if (c == K_CSI)
1797 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001799 }
1800 // a keypad or special function key was not mapped, use it like
1801 // its ASCII equivalent
1802 switch (c)
1803 {
1804 case K_KPLUS: c = '+'; break;
1805 case K_KMINUS: c = '-'; break;
1806 case K_KDIVIDE: c = '/'; break;
1807 case K_KMULTIPLY: c = '*'; break;
1808 case K_KENTER: c = CAR; break;
1809 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001810#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001811 // Can be either '.' or a ',',
1812 // depending on the type of keypad.
1813 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001814#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001815 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001816#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001817 case K_K0: c = '0'; break;
1818 case K_K1: c = '1'; break;
1819 case K_K2: c = '2'; break;
1820 case K_K3: c = '3'; break;
1821 case K_K4: c = '4'; break;
1822 case K_K5: c = '5'; break;
1823 case K_K6: c = '6'; break;
1824 case K_K7: c = '7'; break;
1825 case K_K8: c = '8'; break;
1826 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001827
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001828 case K_XHOME:
1829 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001830 {
1831 c = K_S_HOME;
1832 mod_mask = 0;
1833 }
1834 else if (mod_mask == MOD_MASK_CTRL)
1835 {
1836 c = K_C_HOME;
1837 mod_mask = 0;
1838 }
1839 else
1840 c = K_HOME;
1841 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001842 case K_XEND:
1843 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001844 {
1845 c = K_S_END;
1846 mod_mask = 0;
1847 }
1848 else if (mod_mask == MOD_MASK_CTRL)
1849 {
1850 c = K_C_END;
1851 mod_mask = 0;
1852 }
1853 else
1854 c = K_END;
1855 break;
1856
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001857 case K_XUP: c = K_UP; break;
1858 case K_XDOWN: c = K_DOWN; break;
1859 case K_XLEFT: c = K_LEFT; break;
1860 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001863 // For a multi-byte character get all the bytes and return the
1864 // converted character.
1865 // Note: This will loop until enough bytes are received!
1866 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1867 {
1868 ++no_mapping;
1869 buf[0] = c;
1870 for (i = 1; i < n; ++i)
1871 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001872 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001873 if (buf[i] == K_SPECIAL
1874#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001875 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001876#endif
1877 )
1878 {
1879 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1880 // sequence, which represents a K_SPECIAL (0x80),
1881 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1882 // represents a CSI (0x9B),
1883 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1884 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001885 c = vgetorpeek(TRUE);
1886 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001887 buf[i] = CSI;
1888 }
1889 }
1890 --no_mapping;
1891 c = (*mb_ptr2char)(buf);
1892 }
1893
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001894 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001895 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001896 vgetc_mod_mask = mod_mask;
1897 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001898 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001899
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001900 break;
1901 }
zeertzjq81b46a62022-04-09 17:58:49 +01001902
1903 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001905
1906#ifdef FEAT_EVAL
1907 /*
1908 * In the main loop "may_garbage_collect" can be set to do garbage
1909 * collection in the first next vgetc(). It's disabled after that to
1910 * avoid internally used Lists and Dicts to be freed.
1911 */
1912 may_garbage_collect = FALSE;
1913#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001914
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001915#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001916 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001917 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001918 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001919 bevalexpr_due_set = FALSE;
1920 ui_remove_balloon();
1921 }
1922#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001923#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001924 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1925 // are filtered.
1926 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001927 {
1928 if (c == Ctrl_C)
1929 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001930 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001931 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001932#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001933
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001934 // Need to process the character before we know it's safe to do something
1935 // else.
1936 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001937 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001938
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001939 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941
1942/*
1943 * Like vgetc(), but never return a NUL when called recursively, get a key
1944 * directly from the user (ignoring typeahead).
1945 */
1946 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001947safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 int c;
1950
1951 c = vgetc();
1952 if (c == NUL)
1953 c = get_keystroke();
1954 return c;
1955}
1956
1957/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001958 * Like safe_vgetc(), but loop to handle K_IGNORE.
1959 * Also ignore scrollbar events.
1960 */
1961 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001962plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001963{
1964 int c;
1965
1966 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001967 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001968 while (c == K_IGNORE
1969 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1970 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001971
1972 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001973 // Only handle the first pasted character. Drop the rest, since we
1974 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001975 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1976
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001977 return c;
1978}
1979
1980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 * Check if a character is available, such that vgetc() will not block.
1982 * If the next character is a special character or multi-byte, the returned
1983 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001984 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 */
1986 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001987vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
zeertzjq0f68e6c2022-04-05 13:17:01 +01001989 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001991 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992}
1993
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001994#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995/*
1996 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1997 * codes.
1998 */
1999 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002000vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 int c;
2003
2004 ++no_mapping;
2005 ++allow_keys;
2006 c = vpeekc();
2007 --no_mapping;
2008 --allow_keys;
2009 return c;
2010}
2011#endif
2012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013/*
2014 * Check if any character is available, also half an escape sequence.
2015 * Trick: when no typeahead found, but there is something in the typeahead
2016 * buffer, it must be an ESC that is recognized as the start of a key code.
2017 */
2018 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002019vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
2021 int c;
2022
2023 c = vpeekc();
2024 if (c == NUL && typebuf.tb_len > 0)
2025 c = ESC;
2026 return c;
2027}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029/*
2030 * Call vpeekc() without causing anything to be mapped.
2031 * Return TRUE if a character is available, FALSE otherwise.
2032 */
2033 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002034char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 int retval;
2037
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002038#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002039 // When test_override("char_avail", 1) was called pretend there is no
2040 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002041 if (disable_char_avail_for_testing)
2042 return FALSE;
2043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 ++no_mapping;
2045 retval = vpeekc();
2046 --no_mapping;
2047 return (retval != NUL);
2048}
2049
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002050#if defined(FEAT_EVAL) || defined(PROTO)
2051/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002052 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002053 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002054 static void
2055getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002056{
2057 varnumber_T n;
2058 int error = FALSE;
2059
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002060 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2061 return;
2062
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002063#ifdef MESSAGE_QUEUE
2064 // vpeekc() used to check for messages, but that caused problems, invoking
2065 // a callback where it was not expected. Some plugins use getchar(1) in a
2066 // loop to await a message, therefore make sure we check for messages here.
2067 parse_queued_messages();
2068#endif
2069
Bram Moolenaar30613902019-12-01 22:11:18 +01002070 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002071 windgoto(msg_row, msg_col);
2072
2073 ++no_mapping;
2074 ++allow_keys;
2075 for (;;)
2076 {
2077 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002078 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002079 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002080 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002081 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002082 n = vpeekc_any();
2083 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002084 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002085 n = 0;
2086 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002087 // getchar(0) and char avail() != NUL: get a character.
2088 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2089 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002090
Bram Moolenaar15183b42020-09-05 19:59:39 +02002091 if (n == K_IGNORE || n == K_MOUSEMOVE
2092 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002093 continue;
2094 break;
2095 }
2096 --no_mapping;
2097 --allow_keys;
2098
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002099 // redraw the screen after getchar()
2100 if (p_ch == 0)
2101 update_screen(CLEAR);
2102
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002103 set_vim_var_nr(VV_MOUSE_WIN, 0);
2104 set_vim_var_nr(VV_MOUSE_WINID, 0);
2105 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2106 set_vim_var_nr(VV_MOUSE_COL, 0);
2107
2108 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002109 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002110 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002111 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002112 int i = 0;
2113
Bram Moolenaar30613902019-12-01 22:11:18 +01002114 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002115 if (mod_mask != 0)
2116 {
2117 temp[i++] = K_SPECIAL;
2118 temp[i++] = KS_MODIFIER;
2119 temp[i++] = mod_mask;
2120 }
2121 if (IS_SPECIAL(n))
2122 {
2123 temp[i++] = K_SPECIAL;
2124 temp[i++] = K_SECOND(n);
2125 temp[i++] = K_THIRD(n);
2126 }
2127 else if (has_mbyte)
2128 i += (*mb_char2bytes)(n, temp + i);
2129 else
2130 temp[i++] = n;
2131 temp[i++] = NUL;
2132 rettv->v_type = VAR_STRING;
2133 rettv->vval.v_string = vim_strsave(temp);
2134
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002135 if (is_mouse_key(n))
2136 {
2137 int row = mouse_row;
2138 int col = mouse_col;
2139 win_T *win;
2140 linenr_T lnum;
2141 win_T *wp;
2142 int winnr = 1;
2143
2144 if (row >= 0 && col >= 0)
2145 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002146 // Find the window at the mouse coordinates and compute the
2147 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002148 win = mouse_find_win(&row, &col, FIND_POPUP);
2149 if (win == NULL)
2150 return;
2151 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002152#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153 if (WIN_IS_POPUP(win))
2154 winnr = 0;
2155 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002156#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002157 for (wp = firstwin; wp != win && wp != NULL;
2158 wp = wp->w_next)
2159 ++winnr;
2160 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2161 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2162 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2163 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2164 }
2165 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002166 }
2167}
2168
2169/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002170 * "getchar()" function
2171 */
2172 void
2173f_getchar(typval_T *argvars, typval_T *rettv)
2174{
2175 getchar_common(argvars, rettv);
2176}
2177
2178/*
2179 * "getcharstr()" function
2180 */
2181 void
2182f_getcharstr(typval_T *argvars, typval_T *rettv)
2183{
2184 getchar_common(argvars, rettv);
2185
2186 if (rettv->v_type == VAR_NUMBER)
2187 {
2188 char_u temp[7]; // mbyte-char: 6, NUL: 1
2189 varnumber_T n = rettv->vval.v_number;
2190 int i = 0;
2191
2192 if (n != 0)
2193 {
2194 if (has_mbyte)
2195 i += (*mb_char2bytes)(n, temp + i);
2196 else
2197 temp[i++] = n;
2198 }
2199 temp[i++] = NUL;
2200 rettv->v_type = VAR_STRING;
2201 rettv->vval.v_string = vim_strsave(temp);
2202 }
2203}
2204
2205/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002206 * "getcharmod()" function
2207 */
2208 void
2209f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2210{
2211 rettv->vval.v_number = mod_mask;
2212}
2213#endif // FEAT_EVAL
2214
2215#if defined(MESSAGE_QUEUE) || defined(PROTO)
2216# define MAX_REPEAT_PARSE 8
2217
2218/*
2219 * Process messages that have been queued for netbeans or clientserver.
2220 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002221 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002222 * it is safe to do so.
2223 */
2224 void
2225parse_queued_messages(void)
2226{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002227 int old_curwin_id;
2228 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002229 int i;
2230 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002231 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002232 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002233
2234 // Do not handle messages while redrawing, because it may cause buffers to
2235 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002236 // Also bail out when parsing messages was explicitly disabled.
2237 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002238 return;
2239
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002240 // If memory allocation fails during startup we'll exit but curbuf or
2241 // curwin could be NULL.
2242 if (curbuf == NULL || curwin == NULL)
2243 return;
2244
2245 old_curbuf_fnum = curbuf->b_fnum;
2246 old_curwin_id = curwin->w_id;
2247
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002248 ++entered;
2249
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002250 // may_garbage_collect is set in main_loop() to do garbage collection when
2251 // blocking to wait on a character. We don't want that while parsing
2252 // messages, a callback may invoke vgetc() while lists and dicts are in use
2253 // in the call stack.
2254 may_garbage_collect = FALSE;
2255
2256 // Loop when a job ended, but don't keep looping forever.
2257 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2258 {
2259 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002260# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002261 channel_handle_events(FALSE);
2262# endif
2263
2264# ifdef FEAT_NETBEANS_INTG
2265 // Process the queued netbeans messages.
2266 netbeans_parse_messages();
2267# endif
2268# ifdef FEAT_JOB_CHANNEL
2269 // Write any buffer lines still to be written.
2270 channel_write_any_lines();
2271
2272 // Process the messages queued on channels.
2273 channel_parse_messages();
2274# endif
2275# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2276 // Process the queued clientserver messages.
2277 server_parse_messages();
2278# endif
2279# ifdef FEAT_JOB_CHANNEL
2280 // Check if any jobs have ended. If so, repeat the above to handle
2281 // changes, e.g. stdin may have been closed.
2282 if (job_check_ended())
2283 continue;
2284# endif
2285# ifdef FEAT_TERMINAL
2286 free_unused_terminals();
2287# endif
2288# ifdef FEAT_SOUND_CANBERRA
2289 if (has_sound_callback_in_queue())
2290 invoke_sound_callback();
2291# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002292#ifdef SIGUSR1
2293 if (got_sigusr1)
2294 {
2295 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2296 got_sigusr1 = FALSE;
2297 }
2298#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002299 break;
2300 }
2301
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002302 // When not nested we'll go back to waiting for a typed character. If it
2303 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002304 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002305 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002306
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002307 may_garbage_collect = save_may_garbage_collect;
2308
2309 // If the current window or buffer changed we need to bail out of the
2310 // waiting loop. E.g. when a job exit callback closes the terminal window.
2311 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002312 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002313
2314 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002315}
2316#endif
2317
2318
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002319typedef enum {
2320 map_result_fail, // failed, break loop
2321 map_result_get, // get a character from typeahead
2322 map_result_retry, // try to map again
2323 map_result_nomatch // no matching mapping, get char
2324} map_result_T;
2325
2326/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002327 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002328 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002329 */
2330 static int
zeertzjqee446032022-04-30 15:02:22 +01002331at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002332{
2333 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2334 int c = *p;
2335
2336 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002337 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002338 && p[1] == KS_MODIFIER
2339 && (p[2] & MOD_MASK_CTRL))
2340 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002341 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2342 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002343}
2344
2345/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002346 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002347 * into just a key, apply that.
2348 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2349 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002350 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002351 */
2352 static int
2353check_simplify_modifier(int max_offset)
2354{
2355 int offset;
2356 char_u *tp;
2357
2358 for (offset = 0; offset < max_offset; ++offset)
2359 {
2360 if (offset + 3 >= typebuf.tb_len)
2361 break;
2362 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2363 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2364 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002365 // A modifier was not used for a mapping, apply it to ASCII keys.
2366 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002367 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002368 int c = tp[3];
2369 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002370
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002371 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002372 {
2373 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002374 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002375
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002376 if (offset == 0)
2377 {
2378 // At the start: remember the character and mod_mask before
2379 // merging, in some cases, e.g. at the hit-return prompt,
2380 // they are put back in the typeahead buffer.
2381 vgetc_char = c;
2382 vgetc_mod_mask = tp[2];
2383 }
zeertzjq12e21e32022-04-27 11:58:01 +01002384 if (IS_SPECIAL(new_c))
2385 {
2386 new_string[0] = K_SPECIAL;
2387 new_string[1] = K_SECOND(new_c);
2388 new_string[2] = K_THIRD(new_c);
2389 len = 3;
2390 }
2391 else
2392 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002393 if (modifier == 0)
2394 {
2395 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002396 NULL, 0, NULL) == FAIL)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002397 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002398 }
2399 else
2400 {
2401 tp[2] = modifier;
2402 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002403 NULL, 0, NULL) == FAIL)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002404 return -1;
2405 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002406 return len;
2407 }
2408 }
2409 }
2410 return 0;
2411}
2412
2413/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002414 * Handle mappings in the typeahead buffer.
2415 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002416 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002417 * - When there is no match yet, return map_result_nomatch, need to get more
2418 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002419 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002420 */
2421 static int
2422handle_mapping(
2423 int *keylenp,
2424 int *timedout,
2425 int *mapdepth)
2426{
2427 mapblock_T *mp = NULL;
2428 mapblock_T *mp2;
2429 mapblock_T *mp_match;
2430 int mp_match_len = 0;
2431 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002432 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002433 int tb_c1;
2434 int mlen;
2435#ifdef FEAT_LANGMAP
2436 int nolmaplen;
2437#endif
2438 int keylen = *keylenp;
2439 int i;
2440 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002441 int is_plug_map = FALSE;
2442
zeertzjqbb404f52022-07-23 06:25:29 +01002443 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002444 if (typebuf.tb_len >= 3
2445 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002446 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2447 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2448 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002449
2450 /*
2451 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002452 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002453 *
2454 * Don't look for mappings if:
2455 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2456 * - maphash_valid not set: no mappings present.
2457 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2458 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002459 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002460 * - waiting for a char with --more--
2461 * - in Ctrl-X mode, and we get a valid char for that mode
2462 */
2463 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2464 if (no_mapping == 0 && is_maphash_valid()
2465 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002466 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002467 || (p_remap
2468 && (typebuf.tb_noremap[typebuf.tb_off]
2469 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002470 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2471 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2472 && State != MODE_ASKMORE
2473 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002474 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002475 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002476#ifdef FEAT_GUI
2477 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2478 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2479 {
2480 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2481 // K_SPECIAL KS_MODIFIER {flags}.
2482 tb_c1 = K_SPECIAL;
2483 }
2484#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002485#ifdef FEAT_LANGMAP
2486 if (tb_c1 == K_SPECIAL)
2487 nolmaplen = 2;
2488 else
2489 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002490 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2491 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002492 nolmaplen = 0;
2493 }
2494#endif
2495 // First try buffer-local mappings.
2496 mp = get_buf_maphash_list(local_State, tb_c1);
2497 mp2 = get_maphash_list(local_State, tb_c1);
2498 if (mp == NULL)
2499 {
2500 // There are no buffer-local mappings.
2501 mp = mp2;
2502 mp2 = NULL;
2503 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002504
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002506 * Loop until a partly matching mapping is found or all (local)
2507 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002508 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002509 * A full match is only accepted if there is no partly match, so "aa"
2510 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002511 */
2512 mp_match = NULL;
2513 mp_match_len = 0;
2514 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002515 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002516 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002517 // Only consider an entry if the first character matches and it is
2518 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002519 // Skip ":lmap" mappings if keys were mapped.
2520 if (mp->m_keys[0] == tb_c1
2521 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002522 && !(mp->m_simplified && seenModifyOtherKeys
2523 && typebuf.tb_maplen == 0)
Bram Moolenaar24959102022-05-07 20:01:16 +01002524 && ((mp->m_mode & MODE_LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002525 {
2526#ifdef FEAT_LANGMAP
2527 int nomap = nolmaplen;
2528 int c2;
2529#endif
2530 // find the match length of this mapping
2531 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2532 {
2533#ifdef FEAT_LANGMAP
2534 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2535 if (nomap > 0)
2536 --nomap;
2537 else if (c2 == K_SPECIAL)
2538 nomap = 2;
2539 else
2540 LANGMAP_ADJUST(c2, TRUE);
2541 if (mp->m_keys[mlen] != c2)
2542#else
2543 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002544 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002545#endif
2546 break;
2547 }
2548
Bram Moolenaareda35f72019-08-03 14:59:44 +02002549 // Don't allow mapping the first byte(s) of a multi-byte char.
2550 // Happens when mapping <M-a> and then changing 'encoding'.
2551 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002552 {
2553 char_u *p1 = mp->m_keys;
2554 char_u *p2 = mb_unescape(&p1);
2555
2556 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002557 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002558 mlen = 0;
2559 }
2560
2561 // Check an entry whether it matches.
2562 // - Full match: mlen == keylen
2563 // - Partly match: mlen == typebuf.tb_len
2564 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002565 if (mlen == keylen || (mlen == typebuf.tb_len
2566 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002567 {
2568 char_u *s;
2569 int n;
2570
Bram Moolenaareda35f72019-08-03 14:59:44 +02002571 // If only script-local mappings are allowed, check if the
2572 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002573 s = typebuf.tb_noremap + typebuf.tb_off;
2574 if (*s == RM_SCRIPT
2575 && (mp->m_keys[0] != K_SPECIAL
2576 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002577 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002578 continue;
2579
Bram Moolenaareda35f72019-08-03 14:59:44 +02002580 // If one of the typed keys cannot be remapped, skip the
2581 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582 for (n = mlen; --n >= 0; )
2583 if (*s++ & (RM_NONE|RM_ABBR))
2584 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002585 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002586 continue;
2587
2588 if (keylen > typebuf.tb_len)
2589 {
2590 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002591 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002592 {
2593 // break at a partly match
2594 keylen = KEYLEN_PART_MAP;
2595 break;
2596 }
2597 }
2598 else if (keylen > mp_match_len)
2599 {
2600 // found a longer match
2601 mp_match = mp;
2602 mp_match_len = keylen;
2603 }
2604 }
2605 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002606 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002607 // character. If the first character that didn't match is
2608 // K_SPECIAL then check for a termcode. This isn't perfect
2609 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002610 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002611 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002612 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002613 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2614 }
2615 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2616 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002617 }
2618 }
2619
Bram Moolenaareda35f72019-08-03 14:59:44 +02002620 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002621 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002622 {
2623 mp = mp_match;
2624 keylen = mp_match_len;
2625 }
2626 }
2627
2628 /*
2629 * Check for match with 'pastetoggle'
2630 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002631 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002632 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002633 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2634 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002635 break;
2636 if (p_pt[mlen] == NUL) // match
2637 {
2638 // write chars to script file(s)
2639 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002640 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2641 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002642
2643 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002644 set_option_value_give_err((char_u *)"paste",
2645 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002646 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002647 {
2648 msg_col = 0;
2649 msg_row = Rows - 1;
2650 msg_clr_eos(); // clear ruler
2651 }
2652 status_redraw_all();
2653 redraw_statuslines();
2654 showmode();
2655 setcursor();
2656 *keylenp = keylen;
2657 return map_result_retry;
2658 }
2659 // Need more chars for partly match.
2660 if (mlen == typebuf.tb_len)
2661 keylen = KEYLEN_PART_KEY;
2662 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002663 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002664 max_mlen = mlen + 1;
2665 }
2666
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002667 // May check for a terminal code when there is no mapping or only a partial
2668 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2669 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002670 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002671 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2672 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002673 {
2674 int save_keylen = keylen;
2675
2676 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002677 * When no matching mapping found or found a non-matching mapping that
2678 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002679 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002680 * - mapping is allowed,
2681 * - keys have not been mapped,
2682 * - and not an ESC sequence, not in insert mode or p_ek is on,
2683 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002684 */
zeertzjq68a573c2022-04-28 14:10:01 +01002685 if (no_mapping == 0 || allow_keys != 0)
2686 {
2687 if ((typebuf.tb_maplen == 0
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002689 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002690 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002691 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2692 else
2693 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002694
Bram Moolenaar0684e362020-12-03 19:54:42 +01002695 // If no termcode matched but 'pastetoggle' matched partially
2696 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002697 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002698 keylen = KEYLEN_PART_KEY;
2699
Bram Moolenaar975a8802020-06-06 22:36:24 +02002700 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002701 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002702 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002703 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002704 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002705 if (keylen < 0)
2706 // ins_typebuf() failed
2707 return map_result_fail;
2708 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002709
Bram Moolenaareda35f72019-08-03 14:59:44 +02002710 // When getting a partial match, but the last characters were not
2711 // typed, don't wait for a typed character to complete the
2712 // termcode. This helps a lot when a ":normal" command ends in an
2713 // ESC.
2714 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002715 keylen = 0;
2716 }
2717 else
2718 keylen = 0;
2719 if (keylen == 0) // no matching terminal code
2720 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002721#ifdef AMIGA
2722 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002723 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002724 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002725 {
2726 char_u *s;
2727
2728 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002729 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2730 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002731 ++s)
2732 ;
2733 if (*s == 'r' || *s == '|') // found one
2734 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002735 del_typebuf(
2736 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002737 // get size and redraw screen
2738 shell_resized();
2739 *keylenp = keylen;
2740 return map_result_retry;
2741 }
2742 if (*s == NUL) // need more characters
2743 keylen = KEYLEN_PART_KEY;
2744 }
2745 if (keylen >= 0)
2746#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002747 // When there was a matching mapping and no termcode could be
2748 // replaced after another one, use that mapping (loop around).
2749 // If there was no mapping at all use the character from the
2750 // typeahead buffer right here.
2751 if (mp == NULL)
2752 {
2753 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002754 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002755 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002756 }
2757
2758 if (keylen > 0) // full matching terminal code
2759 {
2760#if defined(FEAT_GUI) && defined(FEAT_MENU)
2761 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002762 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2763 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002764 {
2765 int idx;
2766
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 // Using a menu may cause a break in undo! It's like using
2768 // gotchars(), but without recording or writing to a script
2769 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002770 may_sync_undo();
2771 del_typebuf(3, 0);
2772 idx = get_menu_index(current_menu, local_State);
2773 if (idx != MENU_INDEX_INVALID)
2774 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002775 // In Select mode and a Visual mode menu is used: Switch
2776 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002777 // back to Select mode.
2778 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002779 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002780 {
2781 VIsual_select = FALSE;
2782 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002783 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002784 }
2785 ins_typebuf(current_menu->strings[idx],
2786 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002787 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002788 }
2789 }
2790#endif // FEAT_GUI && FEAT_MENU
2791 *keylenp = keylen;
2792 return map_result_retry; // try mapping again
2793 }
2794
Bram Moolenaareda35f72019-08-03 14:59:44 +02002795 // Partial match: get some more characters. When a matching mapping
2796 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002797 if (mp == NULL || keylen < 0)
2798 keylen = KEYLEN_PART_KEY;
2799 else
2800 keylen = mp_match_len;
2801 }
2802
2803 /*
2804 * complete match
2805 */
2806 if (keylen >= 0 && keylen <= typebuf.tb_len)
2807 {
2808 char_u *map_str;
2809
2810#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002811 int save_m_expr;
2812 int save_m_noremap;
2813 int save_m_silent;
2814 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002815#else
2816# define save_m_noremap mp->m_noremap
2817# define save_m_silent mp->m_silent
2818#endif
2819
2820 // write chars to script file(s)
2821 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002822 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2823 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002824
2825 cmd_silent = (typebuf.tb_silent > 0);
2826 del_typebuf(keylen, 0); // remove the mapped keys
2827
2828 /*
2829 * Put the replacement string in front of mapstr.
2830 * The depth check catches ":map x y" and ":map y x".
2831 */
2832 if (++*mapdepth >= p_mmd)
2833 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002834 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002835 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 redrawcmdline();
2837 else
2838 setcursor();
2839 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002840 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002841 *keylenp = keylen;
2842 return map_result_fail;
2843 }
2844
2845 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002846 * In Select mode and a Visual mode mapping is used: Switch to Visual
2847 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002848 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002849 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002850 {
2851 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002852 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002853 }
2854
2855#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002856 // Copy the values from *mp that are used, because evaluating the
2857 // expression may invoke a function that redefines the mapping, thereby
2858 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 save_m_expr = mp->m_expr;
2860 save_m_noremap = mp->m_noremap;
2861 save_m_silent = mp->m_silent;
2862 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002863
2864 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002865 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2866 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002867 */
2868 if (mp->m_expr)
2869 {
2870 int save_vgetc_busy = vgetc_busy;
2871 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002872 int was_screen_col = screen_cur_col;
2873 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002874 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002875
2876 vgetc_busy = 0;
2877 may_garbage_collect = FALSE;
2878
2879 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002880 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002881
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002882 // The mapping may do anything, but we expect it to take care of
2883 // redrawing. Do put the cursor back where it was.
2884 windgoto(was_screen_row, was_screen_col);
2885 out_flush();
2886
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002887 // If an error was displayed and the expression returns an empty
2888 // string, generate a <Nop> to allow for a redraw.
2889 if (prev_did_emsg != did_emsg
2890 && (map_str == NULL || *map_str == NUL))
2891 {
2892 char_u buf[4];
2893
2894 vim_free(map_str);
2895 buf[0] = K_SPECIAL;
2896 buf[1] = KS_EXTRA;
2897 buf[2] = KE_IGNORE;
2898 buf[3] = NUL;
2899 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01002900 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002901 {
2902 // redraw the command below the error
2903 msg_didout = TRUE;
2904 if (msg_row < cmdline_row)
2905 msg_row = cmdline_row;
2906 redrawcmd();
2907 }
2908 }
2909
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002910 vgetc_busy = save_vgetc_busy;
2911 may_garbage_collect = save_may_garbage_collect;
2912 }
2913 else
2914#endif
2915 map_str = mp->m_str;
2916
2917 /*
2918 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002919 * If 'from' field is the same as the start of the 'to' field, don't
2920 * remap the first character (but do allow abbreviations).
2921 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002922 */
2923 if (map_str == NULL)
2924 i = FAIL;
2925 else
2926 {
2927 int noremap;
2928
2929 if (save_m_noremap != REMAP_YES)
2930 noremap = save_m_noremap;
2931 else if (
2932#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002933 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2934 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002935#else
2936 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2937#endif
2938 != 0)
2939 noremap = REMAP_YES;
2940 else
2941 noremap = REMAP_SKIP;
2942 i = ins_typebuf(map_str, noremap,
2943 0, TRUE, cmd_silent || save_m_silent);
2944#ifdef FEAT_EVAL
2945 if (save_m_expr)
2946 vim_free(map_str);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002947 last_used_map = mp;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002948#endif
2949 }
2950#ifdef FEAT_EVAL
2951 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002952#endif
2953 *keylenp = keylen;
2954 if (i == FAIL)
2955 return map_result_fail;
2956 return map_result_retry;
2957 }
2958
2959 *keylenp = keylen;
2960 return map_result_nomatch;
2961}
2962
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002963/*
2964 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01002965 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01002966 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002969vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 old_char = c;
2972 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002973 old_mouse_row = mouse_row;
2974 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01002975 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978/*
zeertzjq6d4e7252022-04-07 13:58:04 +01002979 * When peeking and not getting a character, reg_executing cannot be cleared
2980 * yet, so set a flag to clear it later.
2981 */
2982 static void
2983check_end_reg_executing(int advance)
2984{
2985 if (reg_executing != 0 && (typebuf.tb_maplen == 0
2986 || pending_end_reg_executing))
2987 {
2988 if (advance)
2989 {
2990 reg_executing = 0;
2991 pending_end_reg_executing = FALSE;
2992 }
2993 else
2994 pending_end_reg_executing = TRUE;
2995 }
2996
2997}
2998
2999/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003000 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 * 1. from the stuffbuffer
3002 * This is used for abbreviated commands like "D" -> "d$".
3003 * Also used to redo a command for ".".
3004 * 2. from the typeahead buffer
3005 * Stores text obtained previously but not used yet.
3006 * Also stores the result of mappings.
3007 * Also used for the ":normal" command.
3008 * 3. from the user
3009 * This may do a blocking wait if "advance" is TRUE.
3010 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003011 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003012 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 * KeyTyped is set to TRUE in the case the user typed the key.
3014 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003015 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003016 * Just look whether there is a character available.
3017 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 *
3019 * When "no_mapping" is zero, checks for mappings in the current mode.
3020 * Only returns one byte (of a multi-byte character).
3021 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3022 */
3023 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003024vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 int c, c1;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003027 int timedout = FALSE; // waited for more than 'timeoutlen'
3028 // for mapping to complete or
3029 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003030 int mapdepth = 0; // check for recursive mapping
3031 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00003032#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 int new_wcol, new_wrow;
3034#endif
3035#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003036 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037#endif
3038 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003040 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 /*
3043 * This function doesn't work very well when called recursively. This may
3044 * happen though, because of:
3045 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3046 * there is a character available, which calls this function. In that
3047 * case we must return NUL, to indicate no character is available.
3048 * 2. A GUI callback function writes to the screen, causing a
3049 * wait_return().
3050 * Using ":normal" can also do this, but it saves the typeahead buffer,
3051 * thus it should be OK. But don't get a key from the user then.
3052 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003053 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 return NUL;
3055
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003056 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
3058 if (advance)
3059 KeyStuffed = FALSE;
3060
3061 init_typebuf();
3062 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003063 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 do
3065 {
3066/*
3067 * get a character: 1. from the stuffbuffer
3068 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003069 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 c = typeahead_char;
3072 if (advance)
3073 typeahead_char = 0;
3074 }
3075 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003076 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (c != NUL && !got_int)
3078 {
3079 if (advance)
3080 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003081 // KeyTyped = FALSE; When the command that stuffed something
3082 // was typed, behave like the stuffed command was typed.
3083 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 KeyStuffed = TRUE;
3085 }
3086 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003087 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089 else
3090 {
3091 /*
3092 * Loop until we either find a matching mapped key, or we
3093 * are sure that it is not a mapped key.
3094 * If a mapped key sequence is found we go back to the start to
3095 * try re-mapping.
3096 */
3097 for (;;)
3098 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003099 long wait_time;
3100 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003101#ifdef FEAT_CMDL_INFO
3102 int showcmd_idx;
3103#endif
zeertzjq6d4e7252022-04-07 13:58:04 +01003104 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 /*
3106 * ui_breakcheck() is slow, don't use it too often when
3107 * inside a mapping. But call it each time for typed
3108 * characters.
3109 */
3110 if (typebuf.tb_maplen)
3111 line_breakcheck();
3112 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003113 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (got_int)
3115 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003116 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003117 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 /*
3120 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003121 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * Otherwise we behave like having gotten a CTRL-C.
3123 * As a result typing CTRL-C in insert mode will
3124 * really insert a CTRL-C.
3125 */
3126 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003127 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 c = ESC;
3129 else
3130 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003131 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003133 if (advance)
3134 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003135 // Also record this character, it might be needed to
3136 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003137 *typebuf.tb_buf = c;
3138 gotchars(typebuf.tb_buf, 1);
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 cmd_silent = FALSE;
3141
3142 break;
3143 }
3144 else if (typebuf.tb_len > 0)
3145 {
3146 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003147 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003149 map_result_T result = handle_mapping(
3150 &keylen, &timedout, &mapdepth);
3151
3152 if (result == map_result_retry)
3153 // try mapping again
3154 continue;
3155 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003156 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003157 // failed, use the outer loop
3158 c = -1;
3159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003161 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163/*
3164 * get a character: 2. from the typeahead buffer
3165 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003166 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003167 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003168 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003169 cmd_silent = (typebuf.tb_silent > 0);
3170 if (typebuf.tb_maplen > 0)
3171 KeyTyped = FALSE;
3172 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003173 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003174 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003175 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003176 gotchars(typebuf.tb_buf
3177 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003178 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003179 KeyNoremap = typebuf.tb_noremap[
3180 typebuf.tb_off];
3181 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003182 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003183 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003186 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
3188
3189/*
3190 * get a character: 3. from the user - handle <Esc> in Insert mode
3191 */
3192 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003193 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * are no more characters at once, we pretend to go out of
3195 * insert mode. This prevents the one second delay after
3196 * typing an <ESC>. If we get something after all, we may
3197 * have to redisplay the mode. That the cursor is in the wrong
3198 * place does not matter.
3199 */
3200 c = 0;
3201#ifdef FEAT_CMDL_INFO
3202 new_wcol = curwin->w_wcol;
3203 new_wrow = curwin->w_wrow;
3204#endif
3205 if ( advance
3206 && typebuf.tb_len == 1
3207 && typebuf.tb_buf[typebuf.tb_off] == ESC
3208 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003211 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003212 && (p_timeout
3213 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003215 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003217 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 char_u *ptr;
3219
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003220 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
3222 unshowmode(TRUE);
3223 mode_deleted = TRUE;
3224 }
3225#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003226 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003227 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 int save_State;
3230
3231 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003232 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 gui_update_cursor(TRUE, FALSE);
3234 State = save_State;
3235 shape_changed = TRUE;
3236 }
3237#endif
3238 validate_cursor();
3239 old_wcol = curwin->w_wcol;
3240 old_wrow = curwin->w_wrow;
3241
Bram Moolenaar30613902019-12-01 22:11:18 +01003242 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (curwin->w_cursor.col != 0)
3244 {
3245 if (curwin->w_wcol > 0)
3246 {
3247 if (did_ai)
3248 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003249 chartabsize_T cts;
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 /*
3252 * We are expecting to truncate the trailing
3253 * white-space, so find the last non-white
3254 * character -- webb
3255 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003256 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003258 init_chartabsize_arg(&cts, curwin,
3259 curwin->w_cursor.lnum, 0, ptr, ptr);
3260 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003262 if (!VIM_ISWHITE(*cts.cts_ptr))
3263 curwin->w_wcol = cts.cts_vcol;
3264 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003266 cts.cts_ptr +=
3267 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003269 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003271 clear_chartabsize_arg(&cts);
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003274 + curwin->w_wcol / curwin->w_width;
3275 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003277 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 else
3280 {
3281 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284 }
3285 else if (curwin->w_p_wrap && curwin->w_wrow)
3286 {
3287 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003288 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3292 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003293 // Correct when the cursor is on the right halve
3294 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 ptr = ml_get_curline();
3296 col -= (*mb_head_off)(ptr, ptr + col);
3297 if ((*mb_ptr2cells)(ptr + col) > 1)
3298 --curwin->w_wcol;
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 setcursor();
3302 out_flush();
3303#ifdef FEAT_CMDL_INFO
3304 new_wcol = curwin->w_wcol;
3305 new_wrow = curwin->w_wrow;
3306#endif
3307 curwin->w_wcol = old_wcol;
3308 curwin->w_wrow = old_wrow;
3309 }
3310 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003311 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003312
Bram Moolenaar30613902019-12-01 22:11:18 +01003313 // Allow mapping for just typed characters. When we get here c
3314 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003315 for (n = 1; n <= c; ++n)
3316 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 typebuf.tb_len += c;
3318
Bram Moolenaar30613902019-12-01 22:11:18 +01003319 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3321 {
3322 timedout = TRUE;
3323 continue;
3324 }
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (ex_normal_busy > 0)
3327 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003328#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar30613902019-12-01 22:11:18 +01003332 // No typeahead left and inside ":normal". Must return
3333 // something to avoid getting stuck. When an incomplete
3334 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (typebuf.tb_len > 0)
3336 {
3337 timedout = TRUE;
3338 continue;
3339 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003340
Bram Moolenaar30613902019-12-01 22:11:18 +01003341 // When 'insertmode' is set, ESC just beeps in Insert
3342 // mode. Use CTRL-L to make edit() return.
3343 // For the command line only CTRL-C always breaks it.
3344 // For the cmdline window: Alternate between ESC and
3345 // CTRL-C: ESC for most situations and CTRL-C to close the
3346 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003347 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003349#ifdef FEAT_TERMINAL
3350 else if (terminal_is_active())
3351 c = K_CANCEL;
3352#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003353 else if ((State & MODE_CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003354#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 )
3358 c = Ctrl_C;
3359 else
3360 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003361#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003363#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003364 // return from main_loop()
3365 if (pending_exmode_active)
3366 exmode_active = EXMODE_NORMAL;
3367
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003368 // no chars to block abbreviation for
3369 typebuf.tb_no_abbr_cnt = 0;
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 break;
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374/*
3375 * get a character: 3. from the user - update display
3376 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003377 // In insert mode a screen update is skipped when characters
3378 // are still available. But when those available characters
3379 // are part of a mapping, and we are going to do a blocking
3380 // wait here. Need to update the screen to display the
3381 // changed text so far. Also for when 'lazyredraw' is set and
3382 // redrawing was postponed because there was something in the
3383 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003384 if (((State & MODE_INSERT) != 0 || p_lz)
3385 && (State & MODE_CMDLINE) == 0
3386 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003389 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391
3392 /*
3393 * If we have a partial match (and are going to wait for more
3394 * input from the user), show the partially matched characters
3395 * to the user with showcmd.
3396 */
3397#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003398 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#endif
3400 c1 = 0;
3401 if (typebuf.tb_len > 0 && advance && !exmode_active)
3402 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003403 if (((State & (MODE_NORMAL | MODE_INSERT))
3404 || State == MODE_LANGMAP)
3405 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003407 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003408 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3410 + typebuf.tb_len - 1) == 1)
3411 {
3412 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3413 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003414 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 c1 = 1;
3416 }
3417#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003418 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 old_wcol = curwin->w_wcol;
3420 old_wrow = curwin->w_wrow;
3421 curwin->w_wcol = new_wcol;
3422 curwin->w_wrow = new_wrow;
3423 push_showcmd();
3424 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003425 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3426 while (showcmd_idx < typebuf.tb_len)
3427 (void)add_to_showcmd(
3428 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 curwin->w_wcol = old_wcol;
3430 curwin->w_wrow = old_wrow;
3431#endif
3432 }
3433
Bram Moolenaar30613902019-12-01 22:11:18 +01003434 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003435 if ((State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3437 && cmdline_star == 0
3438#endif
3439 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3440 + typebuf.tb_len - 1) == 1)
3441 {
3442 putcmdline(typebuf.tb_buf[typebuf.tb_off
3443 + typebuf.tb_len - 1], FALSE);
3444 c1 = 1;
3445 }
3446 }
3447
3448/*
3449 * get a character: 3. from the user - get it
3450 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003451 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003452 // timedout may have been set if a mapping with empty RHS
3453 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003454 timedout = FALSE;
3455
Bram Moolenaar652de232019-04-04 20:13:09 +02003456 if (advance)
3457 {
3458 if (typebuf.tb_len == 0
3459 || !(p_timeout
3460 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3461 // blocking wait
3462 wait_time = -1L;
3463 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3464 wait_time = p_ttm;
3465 else
3466 wait_time = p_tm;
3467 }
3468 else
3469 wait_time = 0;
3470
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003471 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3473 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003474 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003477 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 pop_showcmd();
3479#endif
3480 if (c1 == 1)
3481 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003482 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 edit_unputchar();
Bram Moolenaar24959102022-05-07 20:01:16 +01003484 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003486 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003487 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489
3490 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003491 continue; // end of input script reached
3492 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
3494 if (!advance)
3495 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003496 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 timedout = TRUE;
3499 continue;
3500 }
3501 }
3502 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003503 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 while (typebuf.tb_buf[typebuf.tb_off
3505 + typebuf.tb_len] != NUL)
3506 typebuf.tb_noremap[typebuf.tb_off
3507 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003508#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003509 // Get IM status right after getting keys, not after the
3510 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 vgetc_im_active = im_get_status();
3512#endif
3513 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003514 } // for (;;)
3515 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar30613902019-12-01 22:11:18 +01003517 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003518 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
3520 /*
3521 * The "INSERT" message is taken care of here:
3522 * if we return an ESC to exit insert mode, the message is deleted
3523 * if we don't return an ESC but deleted the message before, redisplay it
3524 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003525 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003527 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003530 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 else
3532 unshowmode(FALSE);
3533 }
3534 else if (c != ESC && mode_deleted)
3535 {
3536 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003537 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 else
3539 showmode();
3540 }
3541 }
3542#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003543 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003544 if (gui.in_use && shape_changed)
3545 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003547 if (timedout && c == ESC)
3548 {
3549 char_u nop_buf[3];
3550
3551 // When recording there will be no timeout. Add a <Nop> after the ESC
3552 // to avoid that it forms a key code with following characters.
3553 nop_buf[0] = K_SPECIAL;
3554 nop_buf[1] = KS_EXTRA;
3555 nop_buf[2] = KE_NOP;
3556 gotchars(nop_buf, 3);
3557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003559 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 return c;
3562}
3563
3564/*
3565 * inchar() - get one character from
3566 * 1. a scriptfile
3567 * 2. the keyboard
3568 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003569 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * NUL terminated (buffer length must be 'maxlen' + 1).
3571 * Minimum for "maxlen" is 3!!!!
3572 *
3573 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3574 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3575 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3576 * otherwise.
3577 *
3578 * If we got an interrupt all input is read until none is available.
3579 *
3580 * If wait_time == 0 there is no waiting for the char.
3581 * If wait_time == n we wait for n msec for a character to arrive.
3582 * If wait_time == -1 we wait forever for a character to arrive.
3583 *
3584 * Return the number of obtained characters.
3585 * Return -1 when end of input script reached.
3586 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003587 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003588inchar(
3589 char_u *buf,
3590 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003591 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
Bram Moolenaar30613902019-12-01 22:11:18 +01003593 int len = 0; // init for GCC
3594 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003596 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar30613902019-12-01 22:11:18 +01003598 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003601 out_flush_cursor(FALSE, FALSE);
3602#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3603 if (gui.in_use && postponed_mouseshape)
3604 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605#endif
3606 }
3607
3608 /*
3609 * Don't reset these when at the hit-return prompt, otherwise a endless
3610 * recursive loop may result (write error in swapfile, hit-return, timeout
3611 * on char wait, flush swapfile, write error....).
3612 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003613 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003615 did_outofmem_msg = FALSE; // display out of memory message (again)
3616 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003618 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003621 * Get a character from a script file if there is one.
3622 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 */
3624 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003625 while (scriptin[curscript] != NULL && script_char < 0
3626#ifdef FEAT_EVAL
3627 && !ignore_script
3628#endif
3629 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003631#ifdef MESSAGE_QUEUE
3632 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003633#endif
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3636 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003637 // Reached EOF.
3638 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3639 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 closescript();
3641 /*
3642 * When reading script file is interrupted, return an ESC to get
3643 * back to normal mode.
3644 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3645 */
3646 if (got_int)
3647 retesc = TRUE;
3648 else
3649 return -1;
3650 }
3651 else
3652 {
3653 buf[0] = script_char;
3654 len = 1;
3655 }
3656 }
3657
Bram Moolenaar30613902019-12-01 22:11:18 +01003658 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 /*
3661 * If we got an interrupt, skip all previously typed characters and
3662 * return TRUE if quit reading script file.
3663 * Stop reading typeahead when a single CTRL-C was read,
3664 * fill_input_buf() returns this when not able to read from stdin.
3665 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3666 * and buf may be pointing inside typebuf.tb_buf[].
3667 */
3668 if (got_int)
3669 {
kylo252ae6f1d82022-02-16 19:24:07 +00003670#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u dum[DUM_LEN + 1];
3672
3673 for (;;)
3674 {
3675 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003676 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 break;
3678 }
3679 return retesc;
3680 }
3681
3682 /*
3683 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003684 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003686 if (wait_time == -1L || wait_time > 10L)
3687 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
3689 /*
3690 * Fill up to a third of the buffer, because each character may be
3691 * tripled below.
3692 */
3693 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3694 }
3695
Bram Moolenaar30613902019-12-01 22:11:18 +01003696 // If the typebuf was changed further down, it is like nothing was added by
3697 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (typebuf_changed(tb_change_cnt))
3699 return 0;
3700
Bram Moolenaar30613902019-12-01 22:11:18 +01003701 // Note the change in the typeahead buffer, this matters for when
3702 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3703 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003704 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3705 typebuf.tb_change_cnt = 1;
3706
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003707 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708}
3709
3710/*
3711 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003712 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * Returns the new length.
3714 */
3715 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003716fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 int i;
3719 char_u *p = buf;
3720
3721 /*
3722 * Two characters are special: NUL and K_SPECIAL.
3723 * When compiled With the GUI CSI is also special.
3724 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3725 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3726 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 for (i = len; --i >= 0; ++p)
3729 {
3730#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003731 // When the GUI is used any character can come after a CSI, don't
3732 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (gui.in_use && p[0] == CSI && i >= 2)
3734 {
3735 p += 2;
3736 i -= 2;
3737 }
zeertzjq646bb722022-02-16 17:51:47 +00003738# ifndef MSWIN
3739 // When not on MS-Windows and the GUI is not used CSI needs to be
3740 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 else if (!gui.in_use && p[0] == CSI)
3742 {
3743 mch_memmove(p + 3, p + 1, (size_t)i);
3744 *p++ = K_SPECIAL;
3745 *p++ = KS_EXTRA;
3746 *p = (int)KE_CSI;
3747 len += 2;
3748 }
zeertzjq646bb722022-02-16 17:51:47 +00003749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 else
3751#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003752 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003753 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003754 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003755#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003756 // Win32 console passes modifiers
3757 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003758# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003759 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003760# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003761 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#endif
3763 ))
3764 {
3765 mch_memmove(p + 3, p + 1, (size_t)i);
3766 p[2] = K_THIRD(p[0]);
3767 p[1] = K_SECOND(p[0]);
3768 p[0] = K_SPECIAL;
3769 p += 2;
3770 len += 2;
3771 }
3772 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003773 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 return len;
3775}
3776
3777#if defined(USE_INPUT_BUF) || defined(PROTO)
3778/*
3779 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3780 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003781 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003782 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 */
3784 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003785input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003788# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3789 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790# endif
3791 );
3792}
3793#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003794
3795/*
3796 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3797 * typeahead.
3798 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003799 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003800getcmdkeycmd(
3801 int promptc UNUSED,
3802 void *cookie UNUSED,
3803 int indent UNUSED,
3804 getline_opt_T do_concat UNUSED)
3805{
3806 garray_T line_ga;
3807 int c1 = -1;
3808 int c2;
3809 int cmod = 0;
3810 int aborted = FALSE;
3811
3812 ga_init2(&line_ga, 1, 32);
3813
3814 // no mapping for these characters
3815 no_mapping++;
3816
3817 got_int = FALSE;
3818 while (c1 != NUL && !aborted)
3819 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003820 if (ga_grow(&line_ga, 32) != OK)
3821 {
3822 aborted = TRUE;
3823 break;
3824 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003825
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003826 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003827 {
3828 // incomplete <Cmd> is an error, because there is not much the user
3829 // could do in this state.
3830 emsg(_(e_cmd_mapping_must_end_with_cr));
3831 aborted = TRUE;
3832 break;
3833 }
3834
3835 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003836 c1 = vgetorpeek(TRUE);
3837
Bram Moolenaar957cf672020-11-12 14:21:06 +01003838 // Get two extra bytes for special keys
3839 if (c1 == K_SPECIAL)
3840 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003841 c1 = vgetorpeek(TRUE);
3842 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003843 if (c1 == KS_MODIFIER)
3844 {
3845 cmod = c2;
3846 continue;
3847 }
3848 c1 = TO_SPECIAL(c1, c2);
3849 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003850 if (c1 == Ctrl_V)
3851 {
3852 // CTRL-V is followed by octal, hex or other characters, reverses
3853 // what AppendToRedobuffLit() does.
3854 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003855 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003856 no_reduce_keys = FALSE;
3857 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003858
3859 if (got_int)
3860 aborted = TRUE;
3861 else if (c1 == '\r' || c1 == '\n')
3862 c1 = NUL; // end the line
3863 else if (c1 == ESC)
3864 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003865 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003866 {
3867 // give a nicer error message for this special case
3868 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3869 aborted = TRUE;
3870 }
3871 else if (IS_SPECIAL(c1))
3872 {
3873 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003874 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003875 else
3876 {
3877 semsg(e_cmd_maping_must_not_include_str_key,
3878 get_special_key_name(c1, cmod));
3879 aborted = TRUE;
3880 }
3881 }
3882 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003883 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003884
3885 cmod = 0;
3886 }
3887
3888 no_mapping--;
3889
3890 if (aborted)
3891 ga_clear(&line_ga);
3892
3893 return (char_u *)line_ga.ga_data;
3894}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003895
3896 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003897do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003898{
3899 int res;
3900#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003901 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003902
3903 if (key == K_SCRIPT_COMMAND && last_used_map != NULL)
3904 {
3905 save_current_sctx = current_sctx;
3906 current_sctx = last_used_map->m_script_ctx;
3907 }
3908#endif
3909
3910 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3911
3912#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003913 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003914 current_sctx = save_current_sctx;
3915#endif
3916
3917 return res;
3918}
3919
3920#if defined(FEAT_EVAL) || defined(PROTO)
3921 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003922reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003923{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003924 if (last_used_map == mp)
3925 last_used_map = NULL;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003926}
3927#endif